Node 18 and Node 20 are end-of-life and receive no security patches, so supporting them before RC.1 would mean promising a runtime nobody can patch. Node 22 is the oldest line still maintained. engines.node moves to >=22, @types/node to ^22, the tsup target to node22, and CI's build job from Node 18 to 22, so every workflow now runs Node 22 only. The docs, the bug report example and the comments that stated 18 as the floor follow. The node:crypto fallback in the HTTP core existed only for Node 18, which lacked the global crypto; it goes with its regression test, which said as much. The ws fallback for the WebSocket client stays: Node 22 has the global, but it can still be switched off with a flag. Moving off Node 18 and 20 also unblocks vitest 4, which the remaining dev audit advisories need. BREAKING CHANGE: Node 18 and Node 20 are no longer supported. The minimum is Node 22. Signed-off-by: Diego Resendez <diego.resendez@zero-oneit.com>
113 lines
4.6 KiB
TypeScript
113 lines
4.6 KiB
TypeScript
import { describe, it, expect, afterEach } from 'vitest';
|
|
import { spawn, type ChildProcess } from 'node:child_process';
|
|
import { createApp, Module } from '../src';
|
|
|
|
@Module({ mountpoint: '/', controllers: [] })
|
|
class Empty {}
|
|
|
|
let app: ReturnType<typeof createApp> | undefined;
|
|
let child: ChildProcess | undefined;
|
|
|
|
afterEach(async () => {
|
|
await app?.close();
|
|
app = undefined;
|
|
child?.kill('SIGKILL');
|
|
child = undefined;
|
|
});
|
|
|
|
const disposed = (r: { lines: string[] }) => r.lines.filter((l) => l.includes('"disposed"'));
|
|
const handled = (r: { lines: string[] }) => r.lines.some((l) => l.includes('received — closing'));
|
|
const listeners = () => process.listenerCount('SIGINT') + process.listenerCount('SIGTERM');
|
|
|
|
describe('handleSignals registration', () => {
|
|
it('registers nothing by default — a library must not own when the process exits', async () => {
|
|
const before = listeners();
|
|
app = createApp({ modules: [Empty] });
|
|
await app.listen(0);
|
|
expect(listeners()).toBe(before);
|
|
});
|
|
|
|
it('registers SIGINT and SIGTERM when asked, and takes both back off in close()', async () => {
|
|
const before = listeners();
|
|
const opted = createApp({ modules: [Empty], handleSignals: true });
|
|
await opted.listen(0);
|
|
expect(listeners()).toBe(before + 2);
|
|
|
|
// Not tidiness: on Deno a live signal listener holds the process open, so an app that closed
|
|
// itself and expected to exit would hang. Node is where it is observable.
|
|
await opted.close();
|
|
expect(listeners()).toBe(before);
|
|
});
|
|
});
|
|
|
|
/** Boots the harness app, waits for its `ready` line, and resolves once it has exited. */
|
|
function runHarness(signal: NodeJS.Signals, env: NodeJS.ProcessEnv = {}, sendTwice = false) {
|
|
return new Promise<{ code: number | null; signal: NodeJS.Signals | null; lines: string[] }>((resolve, reject) => {
|
|
// `process.execPath -r tsx/cjs`, not `npx tsx`: a wrapper puts the app one process further
|
|
// down, and on Linux the signal then lands on the wrapper while the app never hears it — which
|
|
// is exactly how this passed locally and failed in CI. `-r` rather than `--import`/`--loader`
|
|
// because it is the one form that works on both the Node 22 floor CI builds against and the
|
|
// current release.
|
|
const proc = spawn(process.execPath, ['-r', 'tsx/cjs', 'test/interop/_signal-app.ts'], {
|
|
env: { ...process.env, ...env },
|
|
stdio: ['ignore', 'pipe', 'inherit'],
|
|
});
|
|
child = proc;
|
|
|
|
const lines: string[] = [];
|
|
let ready = false;
|
|
let buffered = '';
|
|
|
|
proc.stdout.on('data', (chunk: Buffer) => {
|
|
buffered += chunk.toString();
|
|
const parts = buffered.split('\n');
|
|
buffered = parts.pop() ?? '';
|
|
|
|
for (const line of parts.filter(Boolean)) {
|
|
lines.push(line);
|
|
if (!ready && line.includes('"ready"')) {
|
|
ready = true;
|
|
proc.kill(signal);
|
|
if (sendTwice) setTimeout(() => proc.kill(signal), 20);
|
|
}
|
|
}
|
|
});
|
|
|
|
proc.on('error', reject);
|
|
proc.on('exit', (code, exitSignal) => resolve({ code, signal: exitSignal, lines }));
|
|
});
|
|
}
|
|
|
|
// These need a real process because the handler's job is to exit the one it runs in — firing it by
|
|
// hand in the test runner would either take the runner down or prove nothing.
|
|
describe('handleSignals end to end', () => {
|
|
it('SIGTERM runs teardown and exits 0', { timeout: 20_000 }, async () => {
|
|
const r = await runHarness('SIGTERM');
|
|
expect(disposed(r)).toHaveLength(1);
|
|
expect(r.code).toBe(0);
|
|
});
|
|
|
|
it('SIGINT does the same — Ctrl-C is not a different kind of shutdown', { timeout: 20_000 }, async () => {
|
|
const r = await runHarness('SIGINT');
|
|
expect(disposed(r)).toHaveLength(1);
|
|
expect(r.code).toBe(0);
|
|
});
|
|
|
|
// The escape hatch, and the reason `close()` unregisters before it starts draining: a second
|
|
// signal finds no handler and gets the platform default. Ctrl-C twice ends a stuck teardown
|
|
// rather than waiting out the whole shutdown budget.
|
|
it('lets a second signal end a shutdown that is still draining', { timeout: 20_000 }, async () => {
|
|
const r = await runHarness('SIGTERM', { DISPOSE_DELAY_MS: '2000' }, true);
|
|
expect(handled(r)).toBe(true); // the first signal was ours
|
|
expect(disposed(r)).toHaveLength(0); // the second one did not wait for the 2s dispose
|
|
expect(r.code).not.toBe(0);
|
|
});
|
|
|
|
it('leaves the process to its default fate when handleSignals is off', { timeout: 20_000 }, async () => {
|
|
const r = await runHarness('SIGTERM', { HANDLE_SIGNALS: 'false' });
|
|
expect(handled(r)).toBe(false); // nothing of ours ran at all
|
|
expect(disposed(r)).toHaveLength(0);
|
|
expect(r.code).not.toBe(0);
|
|
});
|
|
});
|