diff --git a/AGENTS.md b/AGENTS.md index e7dc4b68..7ce5f968 100644 --- a/AGENTS.md +++ b/AGENTS.md @@ -1772,9 +1772,12 @@ driver along and the normalizer should not. The check's own options are refused with it, not ignored. The browser opener is `open`, `cmd /c start` or `xdg-open`, and the address is printed either way; it has not been tried on - Windows. The page sets `src` from the parsed URL's `href`, `http` or `https` only, as every page - that takes a package address from outside does (#71): its own route needs no check, but CodeQL - flagged the text going in, as `js/xss-through-dom`. + Windows. Its SIGINT and SIGTERM listeners go in before it prints anything and stay for its whole + life: through npx one Ctrl+C arrives twice, from the terminal to the process group and from npm + passing it on, and 1.2.0, listening once, was killed by the second before its closing line, so + never exited with 0 there (#79). The page sets `src` from the parsed URL's `href`, `http` or + `https` only, as every page that takes a package address from outside does (#71): its own route + needs no check, but CodeQL flagged the text going in, as `js/xss-through-dom`. Not built, deliberately: steps that click through the content and check the xAPI it sends, and a content digest for reviewing what was generated. A check of library versions against the hub's diff --git a/packages/verify/bin/h5p-verify.mjs b/packages/verify/bin/h5p-verify.mjs index 59bebaa0..9c83e86a 100755 --- a/packages/verify/bin/h5p-verify.mjs +++ b/packages/verify/bin/h5p-verify.mjs @@ -92,11 +92,16 @@ if (args.values.preview) { console.error(error instanceof VerifyError ? error.message : error) process.exit(2) } + // Listened to before anything is printed, so that a Ctrl+C as soon as the address shows finds the + // listener, and for as long as the process runs, not once: through npx one Ctrl+C arrives twice, + // from the terminal to the whole process group and from npm passing it on, and a second signal + // with no listener left took Node's default and killed the process before its last line (#79). + // Ending the preview again does nothing. + for (const signal of ['SIGINT', 'SIGTERM']) process.on(signal, () => preview.close()) const address = `${preview.url}/` console.log(`Previewing ${basename(file)} at ${address}`) if (args.values['no-open'] || !(await openBrowser(address))) console.log('Open that address in a browser.') console.log('The package stays on this computer. The preview ends when its tab is closed, or with Ctrl+C.') - for (const signal of ['SIGINT', 'SIGTERM']) process.once(signal, () => preview.close()) const ended = await preview.closed console.log(ended === 'unopened' ? 'No tab opened the preview in time; it ended.' : 'The preview ended.') process.exit(0) diff --git a/packages/verify/tests/preview.test.ts b/packages/verify/tests/preview.test.ts index 4429fe90..5ccb197c 100644 --- a/packages/verify/tests/preview.test.ts +++ b/packages/verify/tests/preview.test.ts @@ -170,6 +170,32 @@ describe('h5p-verify --preview', () => { } }, 30_000) + // npm runs the command as its child and passes on the signals it gets, while a terminal sends + // Ctrl+C to the whole process group, so through npx the command gets SIGINT twice (#79). The + // script stands in for npm, in a process group of its own, and the signal goes to the group. + it.skipIf(process.platform === 'win32')('ends with exit code 0 when Ctrl+C arrives twice, as through npx', async () => { + const npm = ` + const command = require('node:child_process').spawn(process.execPath, process.argv.slice(1), { stdio: 'inherit' }) + for (const signal of ['SIGINT', 'SIGTERM']) process.on(signal, () => command.kill(signal)) + command.on('exit', (code, signal) => { console.log('[command exit ' + code + ' ' + signal + ']'); process.exit(0) }) + ` + const group = spawn(process.execPath, ['-e', npm, bin, quiz, '--preview', '--no-open'], { detached: true, stdio: ['ignore', 'pipe', 'inherit'] }) + try { + let output = '' + group.stdout.setEncoding('utf8').on('data', (text: string) => (output += text)) + await new Promise((resolveStarted, reject) => { + group.stdout.on('data', () => output.includes('Ctrl+C') && resolveStarted()) + group.once('exit', () => reject(new Error(`ended before it started: ${output}`))) + }) + process.kill(-group.pid!, 'SIGINT') + await once(group, 'close') + expect(output).toContain('The preview ended.') + expect(output).toContain('[command exit 0 null]') + } finally { + if (group.exitCode === null) process.kill(-group.pid!, 'SIGKILL') + } + }, 30_000) + it("refuses the check's own options, and --no-open without --preview", async () => { expect(await run(quiz, '--preview', '--json', '--out', dir)).toEqual({ code: 2, stderr: '--preview does not take --out, --json\n' }) expect(await run(quiz, '--no-open')).toEqual({ code: 2, stderr: '--no-open goes with --preview\n' })