From 9fe76e49b18854fc6bca7ec772649fcc1305067d Mon Sep 17 00:00:00 2001 From: Noethix55555 <277300782+Noethix55555@users.noreply.github.com> Date: Tue, 16 Jun 2026 23:18:53 -0400 Subject: [PATCH] fix: split process output on \n instead of os.EOL NewlineTransformer split stdout/stderr on os.EOL. On Windows that is \r\n, so output using bare \n line endings was never split into messages: the data was buffered and dropped, because _flush runs after the stream's end handler has already resolved the run()/end() callback. Normal print() worked only because Python's text-mode stdout translates \n to \r\n on Windows. Split on /\r?\n/ so both line endings are handled. --- index.ts | 2 +- test/test-python-shell.ts | 24 +++++++++++++++++++++++- 2 files changed, 24 insertions(+), 2 deletions(-) diff --git a/index.ts b/index.ts index 9569354..c2cd304 100644 --- a/index.ts +++ b/index.ts @@ -86,7 +86,7 @@ export class NewlineTransformer extends Transform { _transform(chunk: any, encoding: string, callback: TransformCallback) { let data: string = chunk.toString(); if (this._lastLineData) data = this._lastLineData + data; - const lines = data.split(newline); + const lines = data.split(/\r?\n/); this._lastLineData = lines.pop(); lines.forEach(this.push.bind(this)); callback(); diff --git a/test/test-python-shell.ts b/test/test-python-shell.ts index 3d5de81..1c886b5 100644 --- a/test/test-python-shell.ts +++ b/test/test-python-shell.ts @@ -1,5 +1,5 @@ import * as should from 'should'; -import { PythonShell } from '..'; +import { PythonShell, NewlineTransformer } from '..'; import { sep, join } from 'path'; import { EOL as newline } from 'os'; import { chdir, cwd } from 'process'; @@ -642,3 +642,25 @@ describe('PythonShell', function () { }); }); }); + +describe('NewlineTransformer', function () { + function collect(input: Buffer): Promise { + return new Promise((resolve) => { + const t = new NewlineTransformer(); + t.setEncoding('utf8'); + const out: string[] = []; + t.on('data', (chunk) => out.push(chunk.toString())); + t.on('end', () => resolve(out)); + t.write(input); + t.end(); + }); + } + + it('splits on \\n regardless of the platform line ending', async function () { + (await collect(Buffer.from('hello\nworld\n'))).should.eql(['hello', 'world']); + }); + + it('splits on \\r\\n', async function () { + (await collect(Buffer.from('hello\r\nworld\r\n'))).should.eql(['hello', 'world']); + }); +});