Skip to content

Commit d706025

Browse files
committed
chore: fixed up header type mappings
1 parent f10e844 commit d706025

File tree

3 files changed

+33
-30
lines changed

3 files changed

+33
-30
lines changed

src/Generator.ts

+6-16
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import type { TarType, DirectoryContent } from './types';
22
import fs from 'fs';
33
import path from 'path';
4+
import { TarTypes } from './types';
45
import * as errors from './errors';
56

67
/**
@@ -23,7 +24,7 @@ function createHeader(filePath: string, stat: fs.Stats, type: TarType): Buffer {
2324
);
2425
}
2526

26-
const size = type === '0' ? stat.size : 0;
27+
const size = type === TarTypes.FILE ? stat.size : 0;
2728
const header = Buffer.alloc(BLOCK_SIZE, 0);
2829

2930
// The TAR headers follow this structure
@@ -47,7 +48,6 @@ function createHeader(filePath: string, stat: fs.Stats, type: TarType): Buffer {
4748
// 345 155 File name (last 155 bytes, total 255 bytes, null-padded)
4849
// 500 12 '\0' (unused)
4950

50-
// FIXME: Assuming file path is under 100 characters long
5151
header.write(filePath.slice(0, 99).padEnd(100, '\0'), 0, 100, 'utf8');
5252
header.write(stat.mode.toString(8).padStart(7, '0') + '\0', 100, 12, 'ascii');
5353
header.write(stat.uid.toString(8).padStart(7, '0') + '\0', 108, 12, 'ascii');
@@ -109,10 +109,10 @@ async function* walkDirectory(
109109
const tarPath = path.join(relativePath, entry);
110110

111111
if (stat.isDirectory()) {
112-
yield { path: tarPath + '/', stat: stat, type: '5' };
112+
yield { path: tarPath + '/', stat: stat, type: TarTypes.DIRECTORY };
113113
yield* walkDirectory(baseDir, path.join(relativePath, entry));
114114
} else if (stat.isFile()) {
115-
yield { path: tarPath, stat: stat, type: '0' };
115+
yield { path: tarPath, stat: stat, type: TarTypes.FILE };
116116
}
117117
}
118118
}
@@ -122,7 +122,7 @@ async function* createTar(baseDir: string): AsyncGenerator<Buffer, void, void> {
122122
// Create header
123123
yield createHeader(entry.path, entry.stat, entry.type);
124124

125-
if (entry.type === '0') {
125+
if (entry.type === TarTypes.FILE) {
126126
// Get file contents
127127
yield* readFile(path.join(baseDir, entry.path));
128128
}
@@ -133,14 +133,4 @@ async function* createTar(baseDir: string): AsyncGenerator<Buffer, void, void> {
133133
yield Buffer.alloc(BLOCK_SIZE, 0);
134134
}
135135

136-
// NOTE: probably need to remove this, idk
137-
// this is a library and should only worry about tarring itself and not writing to fs
138-
async function writeArchive(inputFile: string, outputFile: string) {
139-
const fileHandle = await fs.promises.open(outputFile, 'w+');
140-
for await (const chunk of createTar(inputFile)) {
141-
await fileHandle.write(chunk);
142-
}
143-
await fileHandle.close();
144-
}
145-
146-
export { createHeader, readFile, createTar, writeArchive };
136+
export { createHeader, readFile, createTar };

src/types.ts

+7-9
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,17 @@
11
import type { Stats } from 'fs';
22

3-
// FIXME: Using 0s and 5s for files and directories isn't a good way to handle
4-
// this. I need to make it simpler so that I can assign and test using strings.
5-
// A potential solution is enums, but they don't work with types, so it's a bit
6-
// weird.
7-
type TarFile = '0';
3+
const TarTypes = {
4+
FILE: '0',
5+
DIRECTORY: '5',
6+
} as const;
87

9-
type TarDirectory = '5';
10-
11-
type TarType = TarFile | TarDirectory;
8+
type TarType = (typeof TarTypes)[keyof typeof TarTypes];
129

1310
type DirectoryContent = {
1411
path: string;
1512
stat: Stats;
1613
type: TarType;
1714
};
1815

19-
export type { TarFile, TarDirectory, TarType, DirectoryContent };
16+
export type { TarType, DirectoryContent };
17+
export { TarTypes };

tests/index.test.ts

+20-5
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,24 @@
1-
import { writeArchive } from '@/Generator';
1+
import fs from 'fs';
2+
import { createTar } from '@/Generator';
23

4+
// TODO: actually write tests
35
describe('index', () => {
4-
test.skip('test', async () => {
5-
await expect(
6-
writeArchive('/home/aryanj/Downloads', '/home/aryanj/archive.tar'),
7-
).toResolve();
6+
test('test', async () => {
7+
if (process.env['CI'] != null) {
8+
// Skip this test if on CI
9+
expect(true).toEqual(true);
10+
} else {
11+
// Otherwise, run the test which creates a test archive
12+
const writeArchive = async (inputFile: string, outputFile: string) => {
13+
const fileHandle = await fs.promises.open(outputFile, 'w+');
14+
for await (const chunk of createTar(inputFile)) {
15+
await fileHandle.write(chunk);
16+
}
17+
await fileHandle.close();
18+
};
19+
await expect(
20+
writeArchive('/home/aryanj/Downloads', '/home/aryanj/archive.tar'),
21+
).toResolve();
22+
}
823
}, 60000);
924
});

0 commit comments

Comments
 (0)