From d41c9fca14048839ac65ca5358488a6d8681efd9 Mon Sep 17 00:00:00 2001 From: Priyanshubhartistm Date: Thu, 26 Nov 2026 03:15:15 +0530 Subject: [PATCH 1/2] fix(cli): escape terminal controls in import error messages Signed-off-by: Priyanshubhartistm --- .changeset/import-escape-terminal-controls.md | 7 ++++++ src/import-events.ts | 5 ++-- test/unit/cli/import.runtime.spec.ts | 25 ++++++++++++++++++- 3 files changed, 34 insertions(+), 3 deletions(-) create mode 100644 .changeset/import-escape-terminal-controls.md diff --git a/.changeset/import-escape-terminal-controls.md b/.changeset/import-escape-terminal-controls.md new file mode 100644 index 000000000..6a2fe5978 --- /dev/null +++ b/.changeset/import-escape-terminal-controls.md @@ -0,0 +1,7 @@ +--- +'nostream': patch +--- + +fix(cli): show control characters in `nostream import` error messages as escapes + +When a line of an import file could not be parsed, `nostream import` printed the parser's message as it is, and that message quotes part of the file. A file with a terminal escape sequence in a malformed record could therefore change the operator's display or, where the terminal allows it, replace the clipboard. The per-line errors and the final failure message now show control characters as visible escapes (for example `\x1b`), so the context stays readable and nothing is sent to the terminal. diff --git a/src/import-events.ts b/src/import-events.ts index df2de1284..87764aaf4 100644 --- a/src/import-events.ts +++ b/src/import-events.ts @@ -10,6 +10,7 @@ import { EventImportStats, } from './services/event-import-service' import { createEventRepository } from './factories/event-repository-factory' +import { escapeForTerminal } from './cli/utils/output' import { getMasterDbClient } from './database/client' interface CliOptions { @@ -226,7 +227,7 @@ export const runImportEvents = async ( const onLineError = ({ lineNumber, reason }: EventImportLineError) => { if (loggedErrors < MAX_ERROR_LOGS) { - console.warn(`[line ${lineNumber}] ${reason}`) + console.warn(`[line ${lineNumber}] ${escapeForTerminal(reason)}`) loggedErrors += 1 return } @@ -294,7 +295,7 @@ if (require.main === module) { }) .catch((error: unknown) => { if (error instanceof Error) { - console.error(`Import failed: ${error.message}`) + console.error(`Import failed: ${escapeForTerminal(error.message)}`) } else { console.error('Import failed with unknown error') } diff --git a/test/unit/cli/import.runtime.spec.ts b/test/unit/cli/import.runtime.spec.ts index 26af1772e..9c3398d2c 100644 --- a/test/unit/cli/import.runtime.spec.ts +++ b/test/unit/cli/import.runtime.spec.ts @@ -79,8 +79,31 @@ describe('import runtime routing', () => { const code = await runImportEvents(['--help']) expect(code).to.equal(0) - const output = logStub.getCalls().map((call) => call.args.join(' ')).join('\n') + const output = logStub + .getCalls() + .map((call) => call.args.join(' ')) + .join('\n') expect(output).to.include('file.jsonl|file.json') expect(output).to.include('nostream import ./events.json --batch-size 1000') }) + + it('prints a line error with terminal control characters as visible escapes', async () => { + const filePath = makeTempFile('events.jsonl', '{"x":1}\n') + const warnStub = sinon.stub(console, 'warn') + + sinon.stub(dbClient, 'getMasterDbClient').returns(stubDb()) + sinon.stub(EventImportService.prototype, 'importFromReadable').callsFake(async (_stream, options) => { + options?.onLineError?.({ lineNumber: 7, reason: 'Unexpected token "\u001b]52;c;ZXZpbA\u0007"' }) + return emptyStats() + }) + + await runImportEvents([filePath]) + + const output = warnStub + .getCalls() + .map((call) => call.args.join(' ')) + .join('\n') + expect(output).to.include('[line 7] Unexpected token "\\x1b]52;c;ZXZpbA\\x07"') + expect(output).to.not.match(/[\u0000-\u001f\u007f-\u009f]/) + }) }) From 406bb8abe301e794f9a00db7f5a52147d19f2595 Mon Sep 17 00:00:00 2001 From: Priyanshubhartistm Date: Sat, 28 Nov 2026 16:08:07 +0530 Subject: [PATCH 2/2] fix(cli): escape control characters in a rejected import error Signed-off-by: Priyanshubhartistm --- .changeset/import-escape-terminal-controls.md | 2 +- src/cli/commands/import.ts | 13 ++++++++++++- test/unit/cli/import-command.spec.ts | 19 +++++++++++++++++++ 3 files changed, 32 insertions(+), 2 deletions(-) create mode 100644 test/unit/cli/import-command.spec.ts diff --git a/.changeset/import-escape-terminal-controls.md b/.changeset/import-escape-terminal-controls.md index 6a2fe5978..e40e6acb6 100644 --- a/.changeset/import-escape-terminal-controls.md +++ b/.changeset/import-escape-terminal-controls.md @@ -4,4 +4,4 @@ fix(cli): show control characters in `nostream import` error messages as escapes -When a line of an import file could not be parsed, `nostream import` printed the parser's message as it is, and that message quotes part of the file. A file with a terminal escape sequence in a malformed record could therefore change the operator's display or, where the terminal allows it, replace the clipboard. The per-line errors and the final failure message now show control characters as visible escapes (for example `\x1b`), so the context stays readable and nothing is sent to the terminal. +When a line of an import file could not be parsed, `nostream import` printed the parser's message as it is, and that message quotes part of the file. A file with a terminal escape sequence in a malformed record could therefore change the operator's display or, where the terminal allows it, replace the clipboard. The per-line errors, the failure message and an error that quotes the input path (for example a file that does not exist) now show control characters as visible escapes (for example `\x1b`), so the context stays readable and nothing is sent to the terminal. diff --git a/src/cli/commands/import.ts b/src/cli/commands/import.ts index 53eca0861..1c121bf44 100644 --- a/src/cli/commands/import.ts +++ b/src/cli/commands/import.ts @@ -1,4 +1,5 @@ import { runImportEvents } from '../../import-events' +import { escapeForTerminal } from '../utils/output' type ImportOptions = { file?: string @@ -16,5 +17,15 @@ export const runImport = async (options: ImportOptions, rawArgs: string[]): Prom args.push('--batch-size', String(options.batchSize)) } - return runImportEvents([...args, ...rawArgs]) + try { + return await runImportEvents([...args, ...rawArgs]) + } catch (error) { + // The message can quote the input path or the input file; the CLI error + // boundary prints it as it is. + if (error instanceof Error) { + error.message = escapeForTerminal(error.message) + } + + throw error + } } diff --git a/test/unit/cli/import-command.spec.ts b/test/unit/cli/import-command.spec.ts new file mode 100644 index 000000000..f05da9ef6 --- /dev/null +++ b/test/unit/cli/import-command.spec.ts @@ -0,0 +1,19 @@ +import { expect } from 'chai' + +import { runImport } from '../../../src/cli/commands/import' + +describe('runImport', () => { + it('shows control characters of a rejected import as visible escapes', async () => { + const file = 'missing-\u001b]52;c;ZXZpbA\u0007.jsonl' + + try { + await runImport({ file }, []) + expect.fail('Expected the import to reject') + } catch (error) { + const message = error instanceof Error ? error.message : String(error) + expect(message).to.include('Input file does not exist') + expect(message).to.include('missing-\\x1b]52;c;ZXZpbA\\x07.jsonl') + expect(message).to.not.match(/[\u0000-\u001f\u007f-\u009f]/) + } + }) +})