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']); + }); +});