Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,10 @@ jobs:
cache: npm
- name: Install dependencies
run: npm ci
- name: Type-check
run: npm run check
- name: Build
run: npm run build
# Tests run the TypeScript sources directly via Node's native type stripping (Node >= 22.18,
# enforced by devEngines). They spawn short-lived node processes and bind loopback ports, so
# no real Harper instance or loopback-pool setup is required.
Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@
"scripts": {
"check": "tsc",
"build": "tsc -p tsconfig.build.json",
"test": "node --test \"test/**/*.test.ts\""
"test": "node --test \"test/**/*.test.ts\"",
"prepack": "npm run build"
},
"engines": {
"node": ">=20"
Expand Down
2 changes: 1 addition & 1 deletion src/run.ts
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ runner.on('test:fail', (data: any) => {
}
});

runner.compose(spec).pipe(process.stdout);
runner.compose(new spec()).pipe(process.stdout);

process.on('exit', () => {
if (failedFiles.size > 0) {
Expand Down
16 changes: 7 additions & 9 deletions src/targz.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,18 @@
import { pack } from 'tar-fs';
import { createGzip } from 'node:zlib';
import { pipeline } from 'node:stream/promises';

/**
* Packs and compresses a directory into a base64-encoded tar.gz string.
*
* @param dirPath path to directory to pack and compress
*/
export async function targz(dirPath: string): Promise<string> {
const chunks: Buffer[] = [];
return new Promise((resolve, reject) => {
pack(dirPath)
.pipe(createGzip())
.on('data', (chunk: Buffer) => chunks.push(chunk))
.on('end', () => {
resolve(Buffer.concat(chunks).toString('base64'));
})
.on('error', reject);
const chunks: Uint8Array[] = [];
// pipeline() propagates errors from any stage (e.g. pack() failing on a missing dir) and
// destroys the whole chain on failure, so the returned promise always settles.
await pipeline(pack(dirPath), createGzip(), async (source) => {
for await (const chunk of source) chunks.push(chunk as Uint8Array);
});
return Buffer.concat(chunks).toString('base64');
}
24 changes: 24 additions & 0 deletions test/targz.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { test } from 'node:test';
import { ok, rejects } from 'node:assert';
import { mkdtempSync, writeFileSync, rmSync } from 'node:fs';
import { tmpdir } from 'node:os';
import { join } from 'node:path';
import { targz } from '../src/targz.ts';

test('targz packs a directory into a base64-encoded gzip', async () => {
const dir = mkdtempSync(join(tmpdir(), 'targz-'));
try {
writeFileSync(join(dir, 'hello.txt'), 'world');
const result = await targz(dir);
ok(result.length > 0, 'should return a non-empty base64 string');
const bytes = Buffer.from(result, 'base64');
ok(bytes[0] === 0x1f && bytes[1] === 0x8b, 'should decode to a gzip stream (magic bytes)');
} finally {
rmSync(dir, { recursive: true, force: true });
}
});

test('targz rejects (does not hang) when the source directory is missing', async () => {
// Regression test: a source-stream error must reject the promise rather than hang.
await rejects(targz(join(tmpdir(), `targz-missing-${process.pid}-${Date.now()}`)));
});
3 changes: 3 additions & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@
// Allow JS files for mixed codebase
"allowJs": true,

// Don't type-check dependency .d.ts files (avoids @types/node lib-internal mismatches)
"skipLibCheck": true,

// Type checking only by default; use `tsconfig.build.json` for emitting
"noEmit": true,

Expand Down
Loading