From b152f683f8b6b345ca276081ebe22ca6c2864261 Mon Sep 17 00:00:00 2001 From: Almenon Date: Tue, 9 Oct 2018 19:45:05 -0400 Subject: [PATCH 01/23] forcing consistent casing in file names --- tsconfig.json | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tsconfig.json b/tsconfig.json index 7eb40ca..414de00 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -7,8 +7,8 @@ ], "sourceMap": true, "rootDir": ".", - "declaration": true, - "forceConsistentCasingInFileNames": true + "forceConsistentCasingInFileNames": true, + "declaration": true }, "exclude": [ "node_modules", From c9bc3de5fc021e622a937d6e4782c666b16d2bc5 Mon Sep 17 00:00:00 2001 From: Almenon Date: Tue, 9 Oct 2018 21:46:42 -0400 Subject: [PATCH 02/23] checkSyntax now uses promises --- index.ts | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/index.ts b/index.ts index 20e515f..88419ae 100644 --- a/index.ts +++ b/index.ts @@ -35,6 +35,7 @@ function extend(obj:{}, ...args) { function getRandomInt(){ return Math.floor(Math.random()*10000000000); } +const execPromise = promisify(exec) export interface Options extends SpawnOptions{ /** @@ -235,6 +236,7 @@ export class PythonShell extends EventEmitter{ /** * checks syntax without executing code +<<<<<<< HEAD * @returns {Promise} rejects w/ stderr if syntax failure */ static async checkSyntax(code:string){ @@ -253,12 +255,26 @@ export class PythonShell extends EventEmitter{ static getPythonPath(){ return this.defaultOptions.pythonPath ? this.defaultOptions.pythonPath : this.defaultPythonPath; } +======= + * @returns rejects promise w/ string error output if syntax failure + */ + static async checkSyntax(code:string){ + let randomInt = PythonShell.getRandomInt(); + let filePath = tmpdir() + sep + `pythonShellSyntaxCheck${randomInt}.py` + + const writeFilePromise = promisify(writeFile) + return writeFilePromise(filePath, code).then(()=>{ + return this.checkSyntaxFile(filePath) + }) + } +>>>>>>> checkSyntax now uses promises /** * checks syntax without executing code * @returns {Promise} rejects w/ stderr if syntax failure */ static async checkSyntaxFile(filePath:string){ +<<<<<<< HEAD const pythonPath = this.getPythonPath() const compileCommand = `${pythonPath} -m py_compile ${filePath}` @@ -269,6 +285,10 @@ export class PythonShell extends EventEmitter{ else reject(stderr) }) }) +======= + let compileCommand = `${this.defaultPythonPath} -m py_compile ${filePath}` + return execPromise(compileCommand) +>>>>>>> checkSyntax now uses promises } /** @@ -307,8 +327,12 @@ export class PythonShell extends EventEmitter{ }; static getVersion(pythonPath?:string){ +<<<<<<< HEAD if(!pythonPath) pythonPath = this.getPythonPath() const execPromise = promisify(exec) +======= + if(!pythonPath) pythonPath = this.defaultPythonPath +>>>>>>> checkSyntax now uses promises return execPromise(pythonPath + " --version"); } From c08bef031c641543c2e61e9212f93216c61cb5ab Mon Sep 17 00:00:00 2001 From: Almenon Date: Tue, 9 Oct 2018 21:46:42 -0400 Subject: [PATCH 03/23] checkSyntax now uses promises --- index.ts | 44 +++----------------------------------------- 1 file changed, 3 insertions(+), 41 deletions(-) diff --git a/index.ts b/index.ts index 88419ae..ff662ae 100644 --- a/index.ts +++ b/index.ts @@ -35,6 +35,7 @@ function extend(obj:{}, ...args) { function getRandomInt(){ return Math.floor(Math.random()*10000000000); } + const execPromise = promisify(exec) export interface Options extends SpawnOptions{ @@ -236,59 +237,25 @@ export class PythonShell extends EventEmitter{ /** * checks syntax without executing code -<<<<<<< HEAD - * @returns {Promise} rejects w/ stderr if syntax failure + * @returns rejects promise w/ string error output if syntax failure */ static async checkSyntax(code:string){ const randomInt = getRandomInt(); const filePath = tmpdir() + sep + `pythonShellSyntaxCheck${randomInt}.py` - - // todo: replace this with util.promisify (once we no longer support node v7) - return new Promise((resolve, reject) => { - writeFile(filePath, code, (err)=>{ - if (err) reject(err); - resolve(this.checkSyntaxFile(filePath)); - }); - }); - } - - static getPythonPath(){ - return this.defaultOptions.pythonPath ? this.defaultOptions.pythonPath : this.defaultPythonPath; - } -======= - * @returns rejects promise w/ string error output if syntax failure - */ - static async checkSyntax(code:string){ - let randomInt = PythonShell.getRandomInt(); - let filePath = tmpdir() + sep + `pythonShellSyntaxCheck${randomInt}.py` const writeFilePromise = promisify(writeFile) return writeFilePromise(filePath, code).then(()=>{ return this.checkSyntaxFile(filePath) }) } ->>>>>>> checkSyntax now uses promises /** * checks syntax without executing code * @returns {Promise} rejects w/ stderr if syntax failure */ static async checkSyntaxFile(filePath:string){ -<<<<<<< HEAD - - const pythonPath = this.getPythonPath() - const compileCommand = `${pythonPath} -m py_compile ${filePath}` - - return new Promise((resolve, reject) => { - exec(compileCommand, (error, stdout, stderr) => { - if(error == null) resolve() - else reject(stderr) - }) - }) -======= let compileCommand = `${this.defaultPythonPath} -m py_compile ${filePath}` return execPromise(compileCommand) ->>>>>>> checkSyntax now uses promises } /** @@ -327,17 +294,12 @@ export class PythonShell extends EventEmitter{ }; static getVersion(pythonPath?:string){ -<<<<<<< HEAD - if(!pythonPath) pythonPath = this.getPythonPath() - const execPromise = promisify(exec) -======= if(!pythonPath) pythonPath = this.defaultPythonPath ->>>>>>> checkSyntax now uses promises return execPromise(pythonPath + " --version"); } static getVersionSync(pythonPath?:string){ - if(!pythonPath) pythonPath = this.getPythonPath() + if(!pythonPath) pythonPath = this.defaultPythonPath return execSync(pythonPath + " --version").toString() } From cf88e7c3335bb7d7897ad35b5a6b9a3b6100bb1f Mon Sep 17 00:00:00 2001 From: almenon Date: Sun, 14 Feb 2021 10:37:08 -0800 Subject: [PATCH 04/23] remove broken path --- .vscode/launch.json | 14 -------------- 1 file changed, 14 deletions(-) diff --git a/.vscode/launch.json b/.vscode/launch.json index 27b78d2..d3af754 100644 --- a/.vscode/launch.json +++ b/.vscode/launch.json @@ -1,20 +1,6 @@ { "version": "0.2.0", "configurations": [ - { - "name": "Python", - "type": "python", - "request": "launch", - "stopOnEntry": true, - "pythonPath": "${config:python.pythonPath}", - "program": "${file}", - "cwd": "${workspaceRoot}", - "env": {}, - "envFile": "${workspaceRoot}/.env", - "debugOptions": [ - "RedirectOutput" - ] - }, { "name": "Current TS Tests File", "type": "node", From e17752d0b36d485c41bf12d53c4531d4e45c899a Mon Sep 17 00:00:00 2001 From: almenon Date: Sun, 14 Feb 2021 11:42:18 -0800 Subject: [PATCH 05/23] fix bad merge --- index.ts | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/index.ts b/index.ts index ff662ae..168b7be 100644 --- a/index.ts +++ b/index.ts @@ -249,12 +249,17 @@ export class PythonShell extends EventEmitter{ }) } + static getPythonPath(){ + return this.defaultOptions.pythonPath ? this.defaultOptions.pythonPath : this.defaultPythonPath; + } + /** * checks syntax without executing code * @returns {Promise} rejects w/ stderr if syntax failure */ static async checkSyntaxFile(filePath:string){ - let compileCommand = `${this.defaultPythonPath} -m py_compile ${filePath}` + const pythonPath = this.getPythonPath() + let compileCommand = `${pythonPath} -m py_compile ${filePath}` return execPromise(compileCommand) } @@ -294,12 +299,12 @@ export class PythonShell extends EventEmitter{ }; static getVersion(pythonPath?:string){ - if(!pythonPath) pythonPath = this.defaultPythonPath + if(!pythonPath) pythonPath = this.getPythonPath() return execPromise(pythonPath + " --version"); } static getVersionSync(pythonPath?:string){ - if(!pythonPath) pythonPath = this.defaultPythonPath + if(!pythonPath) pythonPath = this.getPythonPath() return execSync(pythonPath + " --version").toString() } From a3272540b6e5cad91d6014ca8ab8da8679cef081 Mon Sep 17 00:00:00 2001 From: almenon Date: Sun, 14 Feb 2021 11:45:35 -0800 Subject: [PATCH 06/23] fix bad merge --- tsconfig.json | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tsconfig.json b/tsconfig.json index 414de00..7eb40ca 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -7,8 +7,8 @@ ], "sourceMap": true, "rootDir": ".", - "forceConsistentCasingInFileNames": true, - "declaration": true + "declaration": true, + "forceConsistentCasingInFileNames": true }, "exclude": [ "node_modules", From 08b065084d8bd01929945f03e742390cb45e3b56 Mon Sep 17 00:00:00 2001 From: almenon Date: Sun, 14 Feb 2021 11:50:27 -0800 Subject: [PATCH 07/23] spaces > tabs and i dare you to say otherwise --- index.ts | 28 ++++++++++++++-------------- 1 file changed, 14 insertions(+), 14 deletions(-) diff --git a/index.ts b/index.ts index 168b7be..e7ecb84 100644 --- a/index.ts +++ b/index.ts @@ -236,10 +236,10 @@ export class PythonShell extends EventEmitter{ }; /** - * checks syntax without executing code - * @returns rejects promise w/ string error output if syntax failure - */ - static async checkSyntax(code:string){ + * checks syntax without executing code + * @returns rejects promise w/ string error output if syntax failure + */ + static async checkSyntax(code:string){ const randomInt = getRandomInt(); const filePath = tmpdir() + sep + `pythonShellSyntaxCheck${randomInt}.py` @@ -247,21 +247,21 @@ export class PythonShell extends EventEmitter{ return writeFilePromise(filePath, code).then(()=>{ return this.checkSyntaxFile(filePath) }) - } + } - static getPythonPath(){ - return this.defaultOptions.pythonPath ? this.defaultOptions.pythonPath : this.defaultPythonPath; + static getPythonPath(){ + return this.defaultOptions.pythonPath ? this.defaultOptions.pythonPath : this.defaultPythonPath; } - /** - * checks syntax without executing code - * @returns {Promise} rejects w/ stderr if syntax failure - */ - static async checkSyntaxFile(filePath:string){ + /** + * checks syntax without executing code + * @returns {Promise} rejects w/ stderr if syntax failure + */ + static async checkSyntaxFile(filePath:string){ const pythonPath = this.getPythonPath() - let compileCommand = `${pythonPath} -m py_compile ${filePath}` + let compileCommand = `${pythonPath} -m py_compile ${filePath}` return execPromise(compileCommand) - } + } /** * Runs a Python script and returns collected messages From b10164f26727e31ab2bd04e0115f6e95170c8e6f Mon Sep 17 00:00:00 2001 From: almenon Date: Thu, 4 Mar 2021 22:29:14 -0800 Subject: [PATCH 08/23] apply linting changes --- index.ts | 136 +++++++++++++++++++++++++++---------------------------- 1 file changed, 68 insertions(+), 68 deletions(-) diff --git a/index.ts b/index.ts index e7ecb84..17f9ddc 100644 --- a/index.ts +++ b/index.ts @@ -1,12 +1,12 @@ import { EventEmitter } from 'events'; import { ChildProcess, spawn, SpawnOptions, exec, execSync } from 'child_process'; -import { EOL as newline, tmpdir} from 'os'; +import { EOL as newline, tmpdir } from 'os'; import { join, sep } from 'path' import { Readable, Writable } from 'stream' import { writeFile, writeFileSync } from 'fs'; import { promisify } from 'util'; -function toArray(source?:T|T[]):T[] { +function toArray(source?: T | T[]): T[] { if (typeof source === 'undefined' || source === null) { return []; } else if (!Array.isArray(source)) { @@ -18,7 +18,7 @@ function toArray(source?:T|T[]):T[] { /** * adds arguments as properties to obj */ -function extend(obj:{}, ...args) { +function extend(obj: {}, ...args) { Array.prototype.slice.call(arguments, 1).forEach(function (source) { if (source) { for (let key in source) { @@ -32,20 +32,20 @@ function extend(obj:{}, ...args) { /** * gets a random int from 0-10000000000 */ -function getRandomInt(){ - return Math.floor(Math.random()*10000000000); +function getRandomInt() { + return Math.floor(Math.random() * 10000000000); } const execPromise = promisify(exec) -export interface Options extends SpawnOptions{ +export interface Options extends SpawnOptions { /** * if binary is enabled message and stderr events will not be emitted */ - mode?: 'text'|'json'|'binary' - formatter?: (param:string)=>any - parser?: (param:string)=>any - stderrParser?: (param:string)=>any + mode?: 'text' | 'json' | 'binary' + formatter?: (param: string) => any + parser?: (param: string) => any + stderrParser?: (param: string) => any encoding?: string pythonPath?: string /** @@ -62,9 +62,9 @@ export interface Options extends SpawnOptions{ args?: string[] } -export class PythonShellError extends Error{ +export class PythonShellError extends Error { traceback: string | Buffer; - exitCode?:number; + exitCode?: number; } /** @@ -73,42 +73,42 @@ export class PythonShellError extends Error{ * @param {object} [options] The launch options (also passed to child_process.spawn) * @constructor */ -export class PythonShell extends EventEmitter{ - scriptPath:string - command:string[] - mode:string - formatter:(param:string|Object)=>any - parser:(param:string)=>any - stderrParser:(param:string)=>any - terminated:boolean - childProcess:ChildProcess +export class PythonShell extends EventEmitter { + scriptPath: string + command: string[] + mode: string + formatter: (param: string | Object) => any + parser: (param: string) => any + stderrParser: (param: string) => any + terminated: boolean + childProcess: ChildProcess stdin: Writable; stdout: Readable; stderr: Readable; - exitSignal:string; - exitCode:number; - private stderrHasEnded:boolean; - private stdoutHasEnded:boolean; - private _remaining:string - private _endCallback:(err:PythonShellError, exitCode:number, exitSignal:string)=>any + exitSignal: string; + exitCode: number; + private stderrHasEnded: boolean; + private stdoutHasEnded: boolean; + private _remaining: string + private _endCallback: (err: PythonShellError, exitCode: number, exitSignal: string) => any // starting 2020 python2 is deprecated so we choose 3 as default static defaultPythonPath = process.platform != "win32" ? "python3" : "py"; - static defaultOptions:Options = {}; //allow global overrides for options - + static defaultOptions: Options = {}; //allow global overrides for options + /** * spawns a python process * @param scriptPath path to script. Relative to current directory or options.scriptFolder if specified * @param options */ - constructor(scriptPath:string, options?:Options) { + constructor(scriptPath: string, options?: Options) { super(); /** * returns either pythonshell func (if val string) or custom func (if val Function) */ - function resolve(type, val:string|Function) { + function resolve(type, val: string | Function) { if (typeof val === 'string') { // use a built-in function using its name return PythonShell[type][val]; @@ -118,14 +118,14 @@ export class PythonShell extends EventEmitter{ } } - if(scriptPath.trim().length == 0) throw Error("scriptPath cannot be empty! You must give a script for python to run") + if (scriptPath.trim().length == 0) throw Error("scriptPath cannot be empty! You must give a script for python to run") let self = this; let errorData = ''; EventEmitter.call(this); options = extend({}, PythonShell.defaultOptions, options); - let pythonPath:string; + let pythonPath: string; if (!options.pythonPath) { pythonPath = PythonShell.defaultPythonPath; } else pythonPath = options.pythonPath; @@ -161,7 +161,7 @@ export class PythonShell extends EventEmitter{ this.stderr.on('data', function (data) { errorData += '' + data; }); - this.stderr.on('end', function(){ + this.stderr.on('end', function () { self.stderrHasEnded = true; terminateIfNeeded(); }); @@ -170,7 +170,7 @@ export class PythonShell extends EventEmitter{ } if (this.stdout) { - this.stdout.on('end', function(){ + this.stdout.on('end', function () { self.stdoutHasEnded = true; terminateIfNeeded(); }); @@ -178,16 +178,16 @@ export class PythonShell extends EventEmitter{ self.stdoutHasEnded = true; } - this.childProcess.on('exit', function (code,signal) { + this.childProcess.on('exit', function (code, signal) { self.exitCode = code; self.exitSignal = signal; terminateIfNeeded(); }); function terminateIfNeeded() { - if(!self.stderrHasEnded || !self.stdoutHasEnded || (self.exitCode == null && self.exitSignal == null)) return; + if (!self.stderrHasEnded || !self.stdoutHasEnded || (self.exitCode == null && self.exitSignal == null)) return; - let err:PythonShellError; + let err: PythonShellError; if (self.exitCode && self.exitCode !== 0) { if (errorData) { err = self.parseError(errorData); @@ -209,13 +209,13 @@ export class PythonShell extends EventEmitter{ self.terminated = true; self.emit('close'); - self._endCallback && self._endCallback(err,self.exitCode,self.exitSignal); + self._endCallback && self._endCallback(err, self.exitCode, self.exitSignal); }; } // built-in formatters static format = { - text: function toText(data):string { + text: function toText(data): string { if (!data) return ''; else if (typeof data !== 'string') return data.toString(); return data; @@ -227,10 +227,10 @@ export class PythonShell extends EventEmitter{ //built-in parsers static parse = { - text: function asText(data):string { + text: function asText(data): string { return data; }, - json: function asJson(data:string) { + json: function asJson(data: string) { return JSON.parse(data); } }; @@ -239,25 +239,25 @@ export class PythonShell extends EventEmitter{ * checks syntax without executing code * @returns rejects promise w/ string error output if syntax failure */ - static async checkSyntax(code:string){ + static async checkSyntax(code: string) { const randomInt = getRandomInt(); const filePath = tmpdir() + sep + `pythonShellSyntaxCheck${randomInt}.py` const writeFilePromise = promisify(writeFile) - return writeFilePromise(filePath, code).then(()=>{ + return writeFilePromise(filePath, code).then(() => { return this.checkSyntaxFile(filePath) }) } - static getPythonPath(){ - return this.defaultOptions.pythonPath ? this.defaultOptions.pythonPath : this.defaultPythonPath; + static getPythonPath() { + return this.defaultOptions.pythonPath ? this.defaultOptions.pythonPath : this.defaultPythonPath; } /** * checks syntax without executing code * @returns {Promise} rejects w/ stderr if syntax failure */ - static async checkSyntaxFile(filePath:string){ + static async checkSyntaxFile(filePath: string) { const pythonPath = this.getPythonPath() let compileCommand = `${pythonPath} -m py_compile ${filePath}` return execPromise(compileCommand) @@ -270,14 +270,14 @@ export class PythonShell extends EventEmitter{ * @param {Function} callback The callback function to invoke with the script results * @return {PythonShell} The PythonShell instance */ - static run(scriptPath:string, options?:Options, callback?:(err?:PythonShellError, output?:any[])=>any) { + static run(scriptPath: string, options?: Options, callback?: (err?: PythonShellError, output?: any[]) => any) { let pyshell = new PythonShell(scriptPath, options); let output = []; return pyshell.on('message', function (message) { output.push(message); }).end(function (err) { - return callback(err? err : null, output.length ? output : null); + return callback(err ? err : null, output.length ? output : null); }); }; @@ -288,7 +288,7 @@ export class PythonShell extends EventEmitter{ * @param {Function} callback The callback function to invoke with the script results * @return {PythonShell} The PythonShell instance */ - static runString(code:string, options?:Options, callback?:(err:PythonShellError, output?:any[])=>any) { + static runString(code: string, options?: Options, callback?: (err: PythonShellError, output?: any[]) => any) { // put code in temp file const randomInt = getRandomInt(); @@ -298,13 +298,13 @@ export class PythonShell extends EventEmitter{ return PythonShell.run(filePath, options, callback); }; - static getVersion(pythonPath?:string){ - if(!pythonPath) pythonPath = this.getPythonPath() + static getVersion(pythonPath?: string) { + if (!pythonPath) pythonPath = this.getPythonPath() return execPromise(pythonPath + " --version"); } - static getVersionSync(pythonPath?:string){ - if(!pythonPath) pythonPath = this.getPythonPath() + static getVersionSync(pythonPath?: string) { + if (!pythonPath) pythonPath = this.getPythonPath() return execSync(pythonPath + " --version").toString() } @@ -313,9 +313,9 @@ export class PythonShell extends EventEmitter{ * @param {string|Buffer} data The stderr contents to parse * @return {Error} The parsed error with extended stack trace when traceback is available */ - private parseError(data:string|Buffer) { - let text = ''+data; - let error:PythonShellError; + private parseError(data: string | Buffer) { + let text = '' + data; + let error: PythonShellError; if (/^Traceback/.test(text)) { // traceback data is available @@ -324,8 +324,8 @@ export class PythonShell extends EventEmitter{ error = new PythonShellError(exception); error.traceback = data; // extend stack trace - error.stack += newline+' ----- Python Traceback -----'+newline+' '; - error.stack += lines.slice(1).join(newline+' '); + error.stack += newline + ' ----- Python Traceback -----' + newline + ' '; + error.stack += lines.slice(1).join(newline + ' '); } else { // otherwise, create a simpler error with stderr contents error = new PythonShellError(text); @@ -339,7 +339,7 @@ export class PythonShell extends EventEmitter{ * Override this method to format data to be sent to the Python process * @returns {PythonShell} The same instance for chaining calls */ - send(message:string|Object) { + send(message: string | Object) { if (!this.stdin) throw new Error("stdin not open for writing"); let data = this.formatter ? this.formatter(message) : message; if (this.mode !== 'binary') data += newline; @@ -353,7 +353,7 @@ export class PythonShell extends EventEmitter{ * Override this method to parse incoming data from the Python process into messages * @param {string|Buffer} data The data to parse into messages */ - receive(data:string|Buffer) { + receive(data: string | Buffer) { return this.receiveInternal(data, 'message'); }; @@ -363,13 +363,13 @@ export class PythonShell extends EventEmitter{ * Override this method to parse incoming logs from the Python process into messages * @param {string|Buffer} data The data to parse into messages */ - receiveStderr(data:string|Buffer) { + receiveStderr(data: string | Buffer) { return this.receiveInternal(data, 'stderr'); }; - private receiveInternal(data:string|Buffer, emitType:'message'|'stderr'){ + private receiveInternal(data: string | Buffer, emitType: 'message' | 'stderr') { let self = this; - let parts = (''+data).split(newline); + let parts = ('' + data).split(newline); if (parts.length === 1) { // an incomplete record, keep buffering @@ -384,8 +384,8 @@ export class PythonShell extends EventEmitter{ this._remaining = lastLine; parts.forEach(function (part) { - if(emitType == 'message') self.emit(emitType, self.parser(part)); - else if(emitType == 'stderr') self.emit(emitType, self.stderrParser(part)); + if (emitType == 'message') self.emit(emitType, self.parser(part)); + else if (emitType == 'stderr') self.emit(emitType, self.stderrParser(part)); }); return this; @@ -396,7 +396,7 @@ export class PythonShell extends EventEmitter{ * this should cause the process to finish its work and close. * @returns {PythonShell} The same instance for chaining calls */ - end(callback:(err:PythonShellError, exitCode:number,exitSignal:string)=>any) { + end(callback: (err: PythonShellError, exitCode: number, exitSignal: string) => any) { if (this.childProcess.stdin) { this.childProcess.stdin.end(); } @@ -448,7 +448,7 @@ export interface PythonShell { prependOnceListener(event: "stderr", listener: (parsedChunk: any) => void): this; addListener(event: "close", listener: () => void): this; - emit(event: "close", ): boolean; + emit(event: "close",): boolean; on(event: "close", listener: () => void): this; once(event: "close", listener: () => void): this; prependListener(event: "close", listener: () => void): this; From f4b2546e8dfa5e4e7786ca8d3a285f611bbdd8db Mon Sep 17 00:00:00 2001 From: almenon Date: Thu, 4 Mar 2021 22:30:28 -0800 Subject: [PATCH 09/23] switch to python as default see https://github.com/extrabacon/python-shell/issues/237 --- index.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/index.ts b/index.ts index 17f9ddc..f68bd28 100644 --- a/index.ts +++ b/index.ts @@ -93,7 +93,7 @@ export class PythonShell extends EventEmitter { private _endCallback: (err: PythonShellError, exitCode: number, exitSignal: string) => any // starting 2020 python2 is deprecated so we choose 3 as default - static defaultPythonPath = process.platform != "win32" ? "python3" : "py"; + static defaultPythonPath = process.platform != "win32" ? "python3" : "python"; static defaultOptions: Options = {}; //allow global overrides for options From 74d0b63a3e58826636fa4d5aab4ce811ad2f99cf Mon Sep 17 00:00:00 2001 From: almenon Date: Sun, 7 Mar 2021 10:50:10 -0800 Subject: [PATCH 10/23] update readme for pythonPath --- README.md | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-) diff --git a/README.md b/README.md index a4eb522..bed4b36 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,6 @@ # [python-shell](https://www.npmjs.com/package/python-shell) [![Build status](https://ci.appveyor.com/api/projects/status/m8e3h53vvxg5wb2q/branch/master?svg=true)](https://ci.appveyor.com/project/Almenon/python-shell/branch/master) [![codecov](https://codecov.io/gh/extrabacon/python-shell/branch/master/graph/badge.svg)](https://codecov.io/gh/extrabacon/python-shell) - + A simple way to run Python scripts from Node.js with basic but efficient inter-process communication and better error handling. ## Features @@ -11,17 +11,15 @@ A simple way to run Python scripts from Node.js with basic but efficient inter-p + Simple and efficient data transfers through stdin and stdout streams + Extended stack traces when an error is thrown +## Requirements +First make sure you are able to run `python3` (Mac/Linux) or `python` (Windows) from the terminal. If you are not then you might need to add it to the PATH. If you want to use a version of python not in the PATH you should specify `options.pythonPath`. + ## Installation ```bash npm install python-shell ``` -To run the tests: -```bash -npm test -``` - ## Documentation ### Running python code: @@ -170,7 +168,7 @@ Creates an instance of `PythonShell` and starts the Python process * `parser`: each line of data (ending with "\n") is parsed with this function and its result is emitted as a message * `stderrParser`: each line of logs (ending with "\n") is parsed with this function and its result is emitted as a message * `encoding`: the text encoding to apply on the child process streams (default: "utf8") - * `pythonPath`: The path where to locate the "python" executable. Default: "python3" ("py" for Windows) + * `pythonPath`: The path where to locate the "python" executable. Default: "python3" ("python" for Windows) * `pythonOptions`: Array of option switches to pass to "python" * `scriptPath`: The default path where to look for scripts. Default is the current working directory. * `args`: Array of arguments to pass to the script From 5f526b7ac6e70e771748b72505f974d504f170b3 Mon Sep 17 00:00:00 2001 From: almenon Date: Sun, 7 Mar 2021 17:00:47 -0800 Subject: [PATCH 11/23] add test for pythonPath --- test/test-python-shell.ts | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/test/test-python-shell.ts b/test/test-python-shell.ts index b6f4bb0..2ecf1f8 100644 --- a/test/test-python-shell.ts +++ b/test/test-python-shell.ts @@ -55,6 +55,15 @@ describe('PythonShell', function () { pyshell.command.should.eql(['-u', 'test' + sep + 'python' + sep + 'exit-code.py']); pyshell.end(done); }); + it('should fail to spawn python with bad path', function (done) { + let pyshell = new PythonShell('exit-code.py', { + pythonPath: 'foeisjofseij' + }, ); + pyshell.on('error', (err: NodeJS.ErrnoException)=>{ + err.code.should.eql('ENOENT') + done() + }) + }); it('should spawn a Python process with script arguments', function (done) { let pyshell = new PythonShell('echo_args.py', { args: ['hello', 'world'] From 96dfae8c415e0c2736f2f12e5a117f7d3648ec01 Mon Sep 17 00:00:00 2001 From: almenon Date: Sun, 7 Mar 2021 17:38:34 -0800 Subject: [PATCH 12/23] fix #118 --- README.md | 11 ++++++++- index.ts | 48 +++++++++++++++++++++++---------------- test/test-python-shell.ts | 12 +++++----- 3 files changed, 45 insertions(+), 26 deletions(-) diff --git a/README.md b/README.md index bed4b36..a3f228d 100644 --- a/README.md +++ b/README.md @@ -323,10 +323,19 @@ shell.on('stderr', function (stderr) { Fires when the process has been terminated, with an error or not. -#### event: `error` +#### event: `pythonError` Fires when the process terminates with a non-zero exit code. +#### event: `error` + +Fires when: +* The process could not be spawned, or +* The process could not be killed, or +* Sending a message to the child process failed. + +If the process could not be spawned please double-check that python can be launched from the terminal. + ## Used By: Python-Shell is used by [arepl-vscode](https://github.com/almenon/arepl-vscode), [gitinspector](https://github.com/ejwa/gitinspector), [pyspreadsheet](https://github.com/extrabacon/pyspreadsheet), [AtlantOS Ocean Data QC](https://github.com/ocean-data-qc/ocean-data-qc) and more! diff --git a/index.ts b/index.ts index f68bd28..09f9bee 100644 --- a/index.ts +++ b/index.ts @@ -62,7 +62,7 @@ export interface Options extends SpawnOptions { args?: string[] } -export class PythonShellError extends Error { +export class PythonError extends Error { traceback: string | Buffer; exitCode?: number; } @@ -90,7 +90,7 @@ export class PythonShell extends EventEmitter { private stderrHasEnded: boolean; private stdoutHasEnded: boolean; private _remaining: string - private _endCallback: (err: PythonShellError, exitCode: number, exitSignal: string) => any + private _endCallback: (err: PythonError, exitCode: number, exitSignal: string) => any // starting 2020 python2 is deprecated so we choose 3 as default static defaultPythonPath = process.platform != "win32" ? "python3" : "python"; @@ -178,6 +178,9 @@ export class PythonShell extends EventEmitter { self.stdoutHasEnded = true; } + this.childProcess.on('error', function(err: NodeJS.ErrnoException){ + self.emit('error', err); + }) this.childProcess.on('exit', function (code, signal) { self.exitCode = code; self.exitSignal = signal; @@ -187,14 +190,14 @@ export class PythonShell extends EventEmitter { function terminateIfNeeded() { if (!self.stderrHasEnded || !self.stdoutHasEnded || (self.exitCode == null && self.exitSignal == null)) return; - let err: PythonShellError; + let err: PythonError; if (self.exitCode && self.exitCode !== 0) { if (errorData) { err = self.parseError(errorData); } else { - err = new PythonShellError('process exited with code ' + self.exitCode); + err = new PythonError('process exited with code ' + self.exitCode); } - err = extend(err, { + err = extend(err, { executable: pythonPath, options: pythonOptions.length ? pythonOptions : null, script: self.scriptPath, @@ -202,8 +205,8 @@ export class PythonShell extends EventEmitter { exitCode: self.exitCode }); // do not emit error if only a callback is used - if (self.listeners('error').length || !self._endCallback) { - self.emit('error', err); + if (self.listeners('pythonError').length || !self._endCallback) { + self.emit('pythonError', err); } } @@ -270,7 +273,7 @@ export class PythonShell extends EventEmitter { * @param {Function} callback The callback function to invoke with the script results * @return {PythonShell} The PythonShell instance */ - static run(scriptPath: string, options?: Options, callback?: (err?: PythonShellError, output?: any[]) => any) { + static run(scriptPath: string, options?: Options, callback?: (err?: PythonError, output?: any[]) => any) { let pyshell = new PythonShell(scriptPath, options); let output = []; @@ -288,7 +291,7 @@ export class PythonShell extends EventEmitter { * @param {Function} callback The callback function to invoke with the script results * @return {PythonShell} The PythonShell instance */ - static runString(code: string, options?: Options, callback?: (err: PythonShellError, output?: any[]) => any) { + static runString(code: string, options?: Options, callback?: (err: PythonError, output?: any[]) => any) { // put code in temp file const randomInt = getRandomInt(); @@ -315,20 +318,20 @@ export class PythonShell extends EventEmitter { */ private parseError(data: string | Buffer) { let text = '' + data; - let error: PythonShellError; + let error: PythonError; if (/^Traceback/.test(text)) { // traceback data is available let lines = text.trim().split(newline); let exception = lines.pop(); - error = new PythonShellError(exception); + error = new PythonError(exception); error.traceback = data; // extend stack trace error.stack += newline + ' ----- Python Traceback -----' + newline + ' '; error.stack += lines.slice(1).join(newline + ' '); } else { // otherwise, create a simpler error with stderr contents - error = new PythonShellError(text); + error = new PythonError(text); } return error; @@ -396,7 +399,7 @@ export class PythonShell extends EventEmitter { * this should cause the process to finish its work and close. * @returns {PythonShell} The same instance for chaining calls */ - end(callback: (err: PythonShellError, exitCode: number, exitSignal: string) => any) { + end(callback: (err: PythonError, exitCode: number, exitSignal: string) => any) { if (this.childProcess.stdin) { this.childProcess.stdin.end(); } @@ -454,10 +457,17 @@ export interface PythonShell { prependListener(event: "close", listener: () => void): this; prependOnceListener(event: "close", listener: () => void): this; - addListener(event: "error", listener: (error: PythonShellError) => void): this; - emit(event: "error", error: PythonShellError): boolean; - on(event: "error", listener: (error: PythonShellError) => void): this; - once(event: "error", listener: (error: PythonShellError) => void): this; - prependListener(event: "error", listener: (error: PythonShellError) => void): this; - prependOnceListener(event: "error", listener: (error: PythonShellError) => void): this; + addListener(event: "error", listener: (error: NodeJS.ErrnoException) => void): this; + emit(event: "error", error: NodeJS.ErrnoException): boolean; + on(event: "error", listener: (error: NodeJS.ErrnoException) => void): this; + once(event: "error", listener: (error: NodeJS.ErrnoException) => void): this; + prependListener(event: "error", listener: (error: NodeJS.ErrnoException) => void): this; + prependOnceListener(event: "error", listener: (error: NodeJS.ErrnoException) => void): this; + + addListener(event: "pythonError", listener: (error: PythonError) => void): this; + emit(event: "pythonError", error: PythonError): boolean; + on(event: "pythonError", listener: (error: PythonError) => void): this; + once(event: "pythonError", listener: (error: PythonError) => void): this; + prependListener(event: "pythonError", listener: (error: PythonError) => void): this; + prependOnceListener(event: "pythonError", listener: (error: PythonError) => void): this; } diff --git a/test/test-python-shell.ts b/test/test-python-shell.ts index 2ecf1f8..976d68b 100644 --- a/test/test-python-shell.ts +++ b/test/test-python-shell.ts @@ -59,7 +59,7 @@ describe('PythonShell', function () { let pyshell = new PythonShell('exit-code.py', { pythonPath: 'foeisjofseij' }, ); - pyshell.on('error', (err: NodeJS.ErrnoException)=>{ + pyshell.on('error', (err)=>{ err.code.should.eql('ENOENT') done() }) @@ -450,7 +450,7 @@ describe('PythonShell', function () { let pyshell = new PythonShell('exit-code.py', { args: ['3'] }); - pyshell.on('error', function (err) { + pyshell.on('pythonError', function (err) { err.should.have.properties({ message: 'process exited with code 3', exitCode: 3 @@ -460,7 +460,7 @@ describe('PythonShell', function () { }); it('should emit error when the program exits because of an unhandled exception', function (done) { let pyshell = new PythonShell('error.py'); - pyshell.on('error', function (err) { + pyshell.on('pythonError', function (err) { err.message.should.be.equalOneOf('ZeroDivisionError: integer division or modulo by zero','ZeroDivisionError: division by zero'); err.should.have.property('traceback'); err.traceback.should.containEql('Traceback (most recent call last)'); @@ -469,7 +469,7 @@ describe('PythonShell', function () { }); it('should NOT emit error when logging is written to stderr', function (done) { let pyshell = new PythonShell('stderrLogging.py'); - pyshell.on('error', function (err) { + pyshell.on('pythonError', function (err) { done(new Error("an error should not have been raised")); }); pyshell.on('close', function(){ @@ -483,14 +483,14 @@ describe('PythonShell', function () { let pyshell = new PythonShell('exit-code.py', { args: ['1'] }); - pyshell.on('error', function (err) { + pyshell.on('pythonError', function (err) { err.should.have.properties(['exitCode', 'script', 'options', 'args']); done(); }); }); it('should extend err.stack with traceback', function (done) { let pyshell = new PythonShell('error.py'); - pyshell.on('error', function (err) { + pyshell.on('pythonError', function (err) { err.stack.should.containEql('----- Python Traceback -----'); err.stack.should.containEql('File "test' + sep + 'python' + sep + 'error.py", line 4'); err.stack.should.containEql('File "test' + sep + 'python' + sep + 'error.py", line 6'); From 3483dcfa851dfcd49a37ec2d454c3232e6136fbf Mon Sep 17 00:00:00 2001 From: almenon Date: Sun, 7 Mar 2021 18:03:30 -0800 Subject: [PATCH 13/23] revert name change should try to keep consistent to minimize API changes for upstream devs --- index.ts | 34 +++++++++++++++++----------------- 1 file changed, 17 insertions(+), 17 deletions(-) diff --git a/index.ts b/index.ts index 09f9bee..9287b90 100644 --- a/index.ts +++ b/index.ts @@ -62,7 +62,7 @@ export interface Options extends SpawnOptions { args?: string[] } -export class PythonError extends Error { +export class PythonShellError extends Error { traceback: string | Buffer; exitCode?: number; } @@ -90,7 +90,7 @@ export class PythonShell extends EventEmitter { private stderrHasEnded: boolean; private stdoutHasEnded: boolean; private _remaining: string - private _endCallback: (err: PythonError, exitCode: number, exitSignal: string) => any + private _endCallback: (err: PythonShellError, exitCode: number, exitSignal: string) => any // starting 2020 python2 is deprecated so we choose 3 as default static defaultPythonPath = process.platform != "win32" ? "python3" : "python"; @@ -190,14 +190,14 @@ export class PythonShell extends EventEmitter { function terminateIfNeeded() { if (!self.stderrHasEnded || !self.stdoutHasEnded || (self.exitCode == null && self.exitSignal == null)) return; - let err: PythonError; + let err: PythonShellError; if (self.exitCode && self.exitCode !== 0) { if (errorData) { err = self.parseError(errorData); } else { - err = new PythonError('process exited with code ' + self.exitCode); + err = new PythonShellError('process exited with code ' + self.exitCode); } - err = extend(err, { + err = extend(err, { executable: pythonPath, options: pythonOptions.length ? pythonOptions : null, script: self.scriptPath, @@ -273,7 +273,7 @@ export class PythonShell extends EventEmitter { * @param {Function} callback The callback function to invoke with the script results * @return {PythonShell} The PythonShell instance */ - static run(scriptPath: string, options?: Options, callback?: (err?: PythonError, output?: any[]) => any) { + static run(scriptPath: string, options?: Options, callback?: (err?: PythonShellError, output?: any[]) => any) { let pyshell = new PythonShell(scriptPath, options); let output = []; @@ -291,7 +291,7 @@ export class PythonShell extends EventEmitter { * @param {Function} callback The callback function to invoke with the script results * @return {PythonShell} The PythonShell instance */ - static runString(code: string, options?: Options, callback?: (err: PythonError, output?: any[]) => any) { + static runString(code: string, options?: Options, callback?: (err: PythonShellError, output?: any[]) => any) { // put code in temp file const randomInt = getRandomInt(); @@ -318,20 +318,20 @@ export class PythonShell extends EventEmitter { */ private parseError(data: string | Buffer) { let text = '' + data; - let error: PythonError; + let error: PythonShellError; if (/^Traceback/.test(text)) { // traceback data is available let lines = text.trim().split(newline); let exception = lines.pop(); - error = new PythonError(exception); + error = new PythonShellError(exception); error.traceback = data; // extend stack trace error.stack += newline + ' ----- Python Traceback -----' + newline + ' '; error.stack += lines.slice(1).join(newline + ' '); } else { // otherwise, create a simpler error with stderr contents - error = new PythonError(text); + error = new PythonShellError(text); } return error; @@ -399,7 +399,7 @@ export class PythonShell extends EventEmitter { * this should cause the process to finish its work and close. * @returns {PythonShell} The same instance for chaining calls */ - end(callback: (err: PythonError, exitCode: number, exitSignal: string) => any) { + end(callback: (err: PythonShellError, exitCode: number, exitSignal: string) => any) { if (this.childProcess.stdin) { this.childProcess.stdin.end(); } @@ -464,10 +464,10 @@ export interface PythonShell { prependListener(event: "error", listener: (error: NodeJS.ErrnoException) => void): this; prependOnceListener(event: "error", listener: (error: NodeJS.ErrnoException) => void): this; - addListener(event: "pythonError", listener: (error: PythonError) => void): this; - emit(event: "pythonError", error: PythonError): boolean; - on(event: "pythonError", listener: (error: PythonError) => void): this; - once(event: "pythonError", listener: (error: PythonError) => void): this; - prependListener(event: "pythonError", listener: (error: PythonError) => void): this; - prependOnceListener(event: "pythonError", listener: (error: PythonError) => void): this; + addListener(event: "pythonError", listener: (error: PythonShellError) => void): this; + emit(event: "pythonError", error: PythonShellError): boolean; + on(event: "pythonError", listener: (error: PythonShellError) => void): this; + once(event: "pythonError", listener: (error: PythonShellError) => void): this; + prependListener(event: "pythonError", listener: (error: PythonShellError) => void): this; + prependOnceListener(event: "pythonError", listener: (error: PythonShellError) => void): this; } From 101e71b1c0c773670585ab1b5fe626cefcd98fad Mon Sep 17 00:00:00 2001 From: almenon Date: Sun, 7 Mar 2021 18:05:06 -0800 Subject: [PATCH 14/23] increment version --- package-lock.json | 2 +- package.json | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/package-lock.json b/package-lock.json index abe8484..6cfb31d 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,6 +1,6 @@ { "name": "python-shell", - "version": "2.0.3", + "version": "3.0.0", "lockfileVersion": 1, "requires": true, "dependencies": { diff --git a/package.json b/package.json index 92ef262..6bad030 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "python-shell", - "version": "2.0.3", + "version": "3.0.0", "description": "Run Python scripts from Node.js with simple (but efficient) inter-process communication through stdio", "keywords": [ "python" From 75e15ad4ee8f393acfd2853295e12090b4d75c46 Mon Sep 17 00:00:00 2001 From: almenon Date: Sun, 7 Mar 2021 21:12:42 -0800 Subject: [PATCH 15/23] standardize changelog format see https://keepachangelog.com/en/1.0.0/ --- CHANGELOG.md | 34 ++++++++++++++++++++-------------- 1 file changed, 20 insertions(+), 14 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 7d2c157..72c18c8 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,20 +1,26 @@ -## 1.0.8 -* @joaoe fixed a bug with pythonshell not working with unset std streams -* https://github.com/extrabacon/python-shell/milestone/9 +## [1.0.8] +### Fixed +- @joaoe fixed a bug with pythonshell not working with unset std streams +- https://github.com/extrabacon/python-shell/milestone/9 -## 1.0.7 -* default python path updated to py on windows +## [1.0.7] +### Changed +- default python path updated to py on windows -## 1.0.4 -* added getVersionSync +## [1.0.4] +### Added +- added getVersionSync -## 0.0.3 -* fixed buffering in `PythonShell.receive`, fixing [#1](https://github.com/extrabacon/python-shell/issues/1) +## [0.0.3] +### Fixed +- fixed buffering in `PythonShell.receive`, fixing [#1](https://github.com/extrabacon/python-shell/issues/1) -## 0.0.2 -* improved documentation +## [0.0.2] +### Changed +- improved documentation -## 0.0.1 -* initial version -* independent module moved from [extrabacon/pyspreadsheet](https://github.com/extrabacon/pyspreadsheet) +## [0.0.1] +### Added +- initial version +- independent module moved from [extrabacon/pyspreadsheet](https://github.com/extrabacon/pyspreadsheet) From 79042d83a5a0623e249c2d1e2769f3420bb9bd4e Mon Sep 17 00:00:00 2001 From: almenon Date: Sun, 7 Mar 2021 21:13:16 -0800 Subject: [PATCH 16/23] Add changes to changelog --- CHANGELOG.md | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 72c18c8..a44ee40 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,11 @@ +## [3.0.0] - 2021-03-07 +### Changed +- **BREAKING** Default python path changed back to `python` on Windows. #237 +- **BREAKING** `error` event renamed to `pythonError` event + +### Added +- `error` event that is fired upon failure to launch process, among other things #118 + ## [1.0.8] ### Fixed - @joaoe fixed a bug with pythonshell not working with unset std streams From 230bec49550dba40936de1dc576641367e0da511 Mon Sep 17 00:00:00 2001 From: almenon Date: Sun, 7 Mar 2021 21:20:53 -0800 Subject: [PATCH 17/23] added links --- CHANGELOG.md | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index a44ee40..da922de 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,10 +1,10 @@ ## [3.0.0] - 2021-03-07 ### Changed -- **BREAKING** Default python path changed back to `python` on Windows. #237 -- **BREAKING** `error` event renamed to `pythonError` event +- **BREAKING** Default python path changed back to `python` on Windows. [#237](https://github.com/extrabacon/python-shell/issues/237) +- **BREAKING** `error` event renamed to `pythonError` event. [#118](https://github.com/extrabacon/python-shell/issues/118) ### Added -- `error` event that is fired upon failure to launch process, among other things #118 +- `error` event that is fired upon failure to launch process, among other things. [#118](https://github.com/extrabacon/python-shell/issues/118) ## [1.0.8] ### Fixed From a2c79a3f5a92ea9bd99e1233225089feeb032c51 Mon Sep 17 00:00:00 2001 From: almenon Date: Sun, 14 Mar 2021 13:02:20 -0700 Subject: [PATCH 18/23] formatting & remove deprecated buffer --- test/test-python-shell.ts | 70 +++++++++++++++++++-------------------- 1 file changed, 35 insertions(+), 35 deletions(-) diff --git a/test/test-python-shell.ts b/test/test-python-shell.ts index 976d68b..d0208ca 100644 --- a/test/test-python-shell.ts +++ b/test/test-python-shell.ts @@ -1,7 +1,7 @@ import * as should from 'should'; -import {PythonShell} from '..' -import {sep, join} from 'path' -import {EOL as newline} from 'os' +import { PythonShell } from '..' +import { sep, join } from 'path' +import { EOL as newline } from 'os' import { chdir, cwd } from 'process'; describe('PythonShell', function () { @@ -58,8 +58,8 @@ describe('PythonShell', function () { it('should fail to spawn python with bad path', function (done) { let pyshell = new PythonShell('exit-code.py', { pythonPath: 'foeisjofseij' - }, ); - pyshell.on('error', (err)=>{ + }); + pyshell.on('error', (err) => { err.code.should.eql('ENOENT') done() }) @@ -78,14 +78,14 @@ describe('PythonShell', function () { // note checkSyntax is a wrapper around checkSyntaxFile // so this tests checkSyntaxFile as well - it('should check syntax', function ( done) { - PythonShell.checkSyntax("x=1").then(()=>{ + it('should check syntax', function (done) { + PythonShell.checkSyntax("x=1").then(() => { done(); }) }) - it('should invalidate bad syntax', function ( done) { - PythonShell.checkSyntax("x=").catch(()=>{ + it('should invalidate bad syntax', function (done) { + PythonShell.checkSyntax("x=").catch(() => { done(); }) }) @@ -114,7 +114,7 @@ describe('PythonShell', function () { // }) describe('#runString(script, options)', function () { - before(()=>{ + before(() => { PythonShell.defaultOptions = {}; }) it('should be able to execute a string of python code', function (done) { @@ -125,7 +125,7 @@ describe('PythonShell', function () { done(); }); }); - after(()=>{ + after(() => { PythonShell.defaultOptions = { // reset to match initial value scriptPath: pythonFolder @@ -167,7 +167,7 @@ describe('PythonShell', function () { }); }); it('should run the script and fail with an extended stack trace even when mode is binary', function (done) { - PythonShell.run('error.py', {mode: "binary"}, function (err, results) { + PythonShell.run('error.py', { mode: "binary" }, function (err, results) { err.should.be.an.Error; err.exitCode.should.be.exactly(1); err.stack.should.containEql('----- Python Traceback -----'); @@ -183,7 +183,7 @@ describe('PythonShell', function () { function end() { count++; if (count === numberOfTimesToRun) { - done(); + done(); } } function runSingleErrorScript(callback) { @@ -205,7 +205,7 @@ describe('PythonShell', function () { function end() { count++; if (count === numberOfTimesToRun) { - done(); + done(); } } function runSingleScript(callback) { @@ -220,10 +220,10 @@ describe('PythonShell', function () { } }); - - it('should be able to run modules', function(done){ + + it('should be able to run modules', function (done) { PythonShell.defaultOptions = {}; - + PythonShell.run('-m', { args: ['timeit', '-n 1', `'x=5'`] }, function (err, results) { @@ -236,12 +236,12 @@ describe('PythonShell', function () { if (err) return done(err); results.should.be.an.Array(); results[0].should.be.an.String(); - results[0].slice(0,6).should.eql('1 loop'); + results[0].slice(0, 6).should.eql('1 loop'); done(); }); }) - after(()=>{ + after(() => { // should be able to run modules test should theoretically reset this // but we have this to in case something goes horribly wrong with the test PythonShell.defaultOptions = { @@ -263,7 +263,7 @@ describe('PythonShell', function () { should(pyshell.stdin).be.eql(null); should(pyshell.stdout).be.eql(null); should(pyshell.stderr).be.eql(null); - should.throws(() => {pyshell.send("asd")}); + should.throws(() => { pyshell.send("asd") }); }); }); @@ -274,11 +274,11 @@ describe('PythonShell', function () { }); let output = ''; pyshell.stdout.on('data', function (data) { - output += ''+data; + output += '' + data; }); pyshell.send('hello').send('world').end(function (err) { if (err) return done(err); - output.should.be.exactly('hello'+newline+'world'+newline); + output.should.be.exactly('hello' + newline + 'world' + newline); done(); }); }); @@ -288,11 +288,11 @@ describe('PythonShell', function () { }); let output = ''; pyshell.stdout.on('data', function (data) { - output += ''+data; + output += '' + data; }); pyshell.send({ a: 'b' }).send(null).send([1, 2, 3]).end(function (err) { if (err) return done(err); - output.should.be.exactly('{"a": "b"}'+newline+'null'+newline+'[1, 2, 3]'+newline); + output.should.be.exactly('{"a": "b"}' + newline + 'null' + newline + '[1, 2, 3]' + newline); done(); }); }); @@ -304,11 +304,11 @@ describe('PythonShell', function () { }); let output = ''; pyshell.stdout.on('data', function (data) { - output += ''+data; + output += '' + data; }); pyshell.send('hello').send('world').end(function (err) { if (err) return done(err); - output.should.be.exactly('HELLO'+newline+'WORLD'+newline+''); + output.should.be.exactly('HELLO' + newline + 'WORLD' + newline + ''); done(); }); }); @@ -318,9 +318,9 @@ describe('PythonShell', function () { }); let output = ''; pyshell.stdout.on('data', function (data) { - output += ''+data; + output += '' + data; }); - pyshell.send(new Buffer('i am not a string')).end(function (err) { + pyshell.send(Buffer.from('i am not a string')).end(function (err) { if (err) return done(err); output.should.be.exactly('i am not a string'); done(); @@ -364,7 +364,7 @@ describe('PythonShell', function () { pyshell.on('message', function (message) { message.should.be.an.Object; message.should.eql({ a: true }); - }).receive('{"a"').receive(':').receive('true}'+newline+'').end(done); + }).receive('{"a"').receive(':').receive('true}' + newline + '').end(done); }); it('should not be invoked when mode is "binary"', function (done) { let pyshell = new PythonShell('echo_args.py', { @@ -440,7 +440,7 @@ describe('PythonShell', function () { describe('.end(callback)', function () { it('should end normally when exit code is zero', function (done) { let pyshell = new PythonShell('exit-code.py'); - pyshell.end(function (err,code,signal) { + pyshell.end(function (err, code, signal) { if (err) return done(err); code.should.be.exactly(0); done(); @@ -461,7 +461,7 @@ describe('PythonShell', function () { it('should emit error when the program exits because of an unhandled exception', function (done) { let pyshell = new PythonShell('error.py'); pyshell.on('pythonError', function (err) { - err.message.should.be.equalOneOf('ZeroDivisionError: integer division or modulo by zero','ZeroDivisionError: division by zero'); + err.message.should.be.equalOneOf('ZeroDivisionError: integer division or modulo by zero', 'ZeroDivisionError: division by zero'); err.should.have.property('traceback'); err.traceback.should.containEql('Traceback (most recent call last)'); done(); @@ -472,7 +472,7 @@ describe('PythonShell', function () { pyshell.on('pythonError', function (err) { done(new Error("an error should not have been raised")); }); - pyshell.on('close', function(){ + pyshell.on('close', function () { done(); }) }); @@ -509,7 +509,7 @@ describe('PythonShell', function () { it('run the end callback if specified', function (done) { let pyshell = new PythonShell('infinite_loop.py'); let endCalled = false; - pyshell.end(()=>{ + pyshell.end(() => { endCalled = true; }) pyshell.terminate(); @@ -519,12 +519,12 @@ describe('PythonShell', function () { it('terminate with correct kill signal', function (done) { let pyshell = new PythonShell('infinite_loop.py'); let endCalled = false; - pyshell.end(()=>{ + pyshell.end(() => { endCalled = true; }) pyshell.terminate('SIGKILL'); pyshell.terminated.should.be.true; - setTimeout(()=>{pyshell.exitSignal.should.be.exactly('SIGKILL');},500); + setTimeout(() => { pyshell.exitSignal.should.be.exactly('SIGKILL'); }, 500); done(); }); }); From 59d029ea2af9a0c46952942c1d153eac9f85ac82 Mon Sep 17 00:00:00 2001 From: almenon Date: Sun, 14 Mar 2021 13:15:46 -0700 Subject: [PATCH 19/23] made test more comprehensive --- test/test-python-shell.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/test-python-shell.ts b/test/test-python-shell.ts index d0208ca..aae6500 100644 --- a/test/test-python-shell.ts +++ b/test/test-python-shell.ts @@ -364,7 +364,7 @@ describe('PythonShell', function () { pyshell.on('message', function (message) { message.should.be.an.Object; message.should.eql({ a: true }); - }).receive('{"a"').receive(':').receive('true}' + newline + '').end(done); + }).receive('{"a"').receive(':').receive('true}' + newline + '{').receive('"a":true}' + newline).end(done); }); it('should not be invoked when mode is "binary"', function (done) { let pyshell = new PythonShell('echo_args.py', { From 4d8a841cbf143dd63ddeb3db43aa2014afe4fef2 Mon Sep 17 00:00:00 2001 From: almenon Date: Sat, 20 Mar 2021 12:00:52 -0700 Subject: [PATCH 20/23] switch to LineTransformStream this breaks unit tests for some reason :( --- index.ts | 62 +--- package-lock.json | 712 ++++++++++++++++++++++++++++++++++---- package.json | 8 +- test/test-python-shell.ts | 10 +- 4 files changed, 661 insertions(+), 131 deletions(-) diff --git a/index.ts b/index.ts index 9287b90..1ca0e75 100644 --- a/index.ts +++ b/index.ts @@ -5,6 +5,7 @@ import { join, sep } from 'path' import { Readable, Writable } from 'stream' import { writeFile, writeFileSync } from 'fs'; import { promisify } from 'util'; +const LineTransformStream = require('line-transform-stream') function toArray(source?: T | T[]): T[] { if (typeof source === 'undefined' || source === null) { @@ -147,14 +148,19 @@ export class PythonShell extends EventEmitter { self.parser && self[name] && self[name].setEncoding(options.encoding || 'utf8'); }); - // parse incoming data on stdout + // Node buffers stdout&stderr in batches regardless of newline placement + // This is troublesome if you want to recieve distinct individual messages + // for example JSON parsing breaks if it recieves partial JSON + // so we use LineTransformStream to emit each batch seperated by newline if (this.parser && this.stdout) { - this.stdout.on('data', this.receive.bind(this)); + this.stdout.pipe(new LineTransformStream((data) => { + this.emit('message', self.parser(data)); + })) } - - // listen to stderr and emit errors for incoming data if (this.stderrParser && this.stderr) { - this.stderr.on('data', this.receiveStderr.bind(this)); + this.stdout.pipe(new LineTransformStream((data) => { + this.emit('stderr', self.stderrParser(data)); + })) } if (this.stderr) { @@ -178,7 +184,7 @@ export class PythonShell extends EventEmitter { self.stdoutHasEnded = true; } - this.childProcess.on('error', function(err: NodeJS.ErrnoException){ + this.childProcess.on('error', function (err: NodeJS.ErrnoException) { self.emit('error', err); }) this.childProcess.on('exit', function (code, signal) { @@ -350,50 +356,6 @@ export class PythonShell extends EventEmitter { return this; }; - /** - * Parses data received from the Python shell stdout stream and emits "message" events - * This method is not used in binary mode - * Override this method to parse incoming data from the Python process into messages - * @param {string|Buffer} data The data to parse into messages - */ - receive(data: string | Buffer) { - return this.receiveInternal(data, 'message'); - }; - - /** - * Parses data received from the Python shell stderr stream and emits "stderr" events - * This method is not used in binary mode - * Override this method to parse incoming logs from the Python process into messages - * @param {string|Buffer} data The data to parse into messages - */ - receiveStderr(data: string | Buffer) { - return this.receiveInternal(data, 'stderr'); - }; - - private receiveInternal(data: string | Buffer, emitType: 'message' | 'stderr') { - let self = this; - let parts = ('' + data).split(newline); - - if (parts.length === 1) { - // an incomplete record, keep buffering - this._remaining = (this._remaining || '') + parts[0]; - return this; - } - - let lastLine = parts.pop(); - // fix the first line with the remaining from the previous iteration of 'receive' - parts[0] = (this._remaining || '') + parts[0]; - // keep the remaining for the next iteration of 'receive' - this._remaining = lastLine; - - parts.forEach(function (part) { - if (emitType == 'message') self.emit(emitType, self.parser(part)); - else if (emitType == 'stderr') self.emit(emitType, self.stderrParser(part)); - }); - - return this; - } - /** * Closes the stdin stream. Unless python is listening for stdin in a loop * this should cause the process to finish its work and close. diff --git a/package-lock.json b/package-lock.json index 6cfb31d..3a732f1 100644 --- a/package-lock.json +++ b/package-lock.json @@ -5,9 +5,9 @@ "requires": true, "dependencies": { "@types/mocha": { - "version": "5.2.5", - "resolved": "https://registry.npmjs.org/@types/mocha/-/mocha-5.2.5.tgz", - "integrity": "sha512-lAVp+Kj54ui/vLUFxsJTMtWvZraZxum3w3Nwkble2dNuV5VnPA+Mi2oGX9XYJAaIvZi3tn3cbjS/qcJXRb6Bww==", + "version": "8.2.1", + "resolved": "https://registry.npmjs.org/@types/mocha/-/mocha-8.2.1.tgz", + "integrity": "sha512-NysN+bNqj6E0Hv4CTGWSlPzMW6vTKjDpOteycDkV4IWBsO+PU48JonrPzV9ODjiI2XrjmA05KInLgF5ivZ/YGQ==", "dev": true }, "@types/node": { @@ -16,6 +16,12 @@ "integrity": "sha512-PQKsydPxYxF1DsAFWmunaxd3sOi3iMt6Zmx/tgaagHYmwJ/9cRH91hQkeJZaUGWbvn0K5HlSVEXkn5U/llWPpQ==", "dev": true }, + "@ungap/promise-all-settled": { + "version": "1.1.2", + "resolved": "https://registry.npmjs.org/@ungap/promise-all-settled/-/promise-all-settled-1.1.2.tgz", + "integrity": "sha512-sL/cEvJWAnClXw0wHk85/2L0G6Sj8UB0Ctc1TEMbKSsmpRosqhwj9gWgFRZSrBr2f9tiXISwNhCPmlfqUqyb9Q==", + "dev": true + }, "ajv": { "version": "5.5.2", "resolved": "https://registry.npmjs.org/ajv/-/ajv-5.5.2.tgz", @@ -28,12 +34,49 @@ "json-schema-traverse": "^0.3.0" } }, + "ansi-colors": { + "version": "4.1.1", + "resolved": "https://registry.npmjs.org/ansi-colors/-/ansi-colors-4.1.1.tgz", + "integrity": "sha512-JoX0apGbHaUJBNl6yF+p6JAFYZ666/hhCGKN5t9QFjbJQKUU/g8MNbFDbvfrgKXvI1QpZplPOnwIo99lX/AAmA==", + "dev": true + }, + "ansi-regex": { + "version": "3.0.0", + "resolved": "https://registry.npmjs.org/ansi-regex/-/ansi-regex-3.0.0.tgz", + "integrity": "sha1-7QMXwyIGT3lGbAKWa922Bas32Zg=", + "dev": true + }, + "ansi-styles": { + "version": "4.3.0", + "resolved": "https://registry.npmjs.org/ansi-styles/-/ansi-styles-4.3.0.tgz", + "integrity": "sha512-zbB9rCJAT1rbjiVDb2hqKFHNYLxgtk8NURxZ3IZwD3F6NtxbXZQCnnSi1Lkx+IDohdPlFp222wVALIheZJQSEg==", + "dev": true, + "requires": { + "color-convert": "^2.0.1" + } + }, + "anymatch": { + "version": "3.1.1", + "resolved": "https://registry.npmjs.org/anymatch/-/anymatch-3.1.1.tgz", + "integrity": "sha512-mM8522psRCqzV+6LhomX5wgp25YVibjh8Wj23I5RPkPppSVSjyKD2A2mBJmWGa+KN7f2D6LNh9jkBCeyLktzjg==", + "dev": true, + "requires": { + "normalize-path": "^3.0.0", + "picomatch": "^2.0.4" + } + }, "arg": { "version": "4.1.3", "resolved": "https://registry.npmjs.org/arg/-/arg-4.1.3.tgz", "integrity": "sha512-58S9QDqG0Xx27YwPSt9fJxivjYl432YCwfDMfZ+71RAqUrZef7LrKQZ3LHLOwCS4FLNBplP533Zx895SeOCHvA==", "dev": true }, + "argparse": { + "version": "2.0.1", + "resolved": "https://registry.npmjs.org/argparse/-/argparse-2.0.1.tgz", + "integrity": "sha512-8+9WqebbFzpX9OR+Wa6O29asIogeRMzcGtAINdpMHHyAg10f05aSFVBbcEqGf/PXw1EjAZ+q2/bEBg3DvurK3Q==", + "dev": true + }, "asn1": { "version": "0.2.4", "resolved": "https://registry.npmjs.org/asn1/-/asn1-0.2.4.tgz", @@ -83,6 +126,12 @@ "tweetnacl": "^0.14.3" } }, + "binary-extensions": { + "version": "2.2.0", + "resolved": "https://registry.npmjs.org/binary-extensions/-/binary-extensions-2.2.0.tgz", + "integrity": "sha512-jDctJ/IVQbZoJykoeHbhXpOlNBqGNcwXJKJog42E5HDPUwQTSdjCHdihjj0DlnheQ7blbT6dHOafNAiS8ooQKA==", + "dev": true + }, "boom": { "version": "4.3.1", "resolved": "https://registry.npmjs.org/boom/-/boom-4.3.1.tgz", @@ -102,6 +151,15 @@ "concat-map": "0.0.1" } }, + "braces": { + "version": "3.0.2", + "resolved": "https://registry.npmjs.org/braces/-/braces-3.0.2.tgz", + "integrity": "sha512-b8um+L1RzM3WDSzvhm6gIz1yfTbBt6YTlcEKAvsmqCZZFw46z626lVj9j1yEPW33H5H+lBQpZMP1k8l+78Ha0A==", + "dev": true, + "requires": { + "fill-range": "^7.0.1" + } + }, "browser-stdout": { "version": "1.3.1", "resolved": "https://registry.npmjs.org/browser-stdout/-/browser-stdout-1.3.1.tgz", @@ -114,18 +172,121 @@ "integrity": "sha512-MQcXEUbCKtEo7bhqEs6560Hyd4XaovZlO/k9V3hjVUF/zwW7KBVdSK4gIt/bzwS9MbR5qob+F5jusZsb0YQK2A==", "dev": true }, + "camelcase": { + "version": "6.2.0", + "resolved": "https://registry.npmjs.org/camelcase/-/camelcase-6.2.0.tgz", + "integrity": "sha512-c7wVvbw3f37nuobQNtgsgG9POC9qMbNuMQmTCqZv23b6MIz0fcYpBiOlv9gEN/hdLdnZTDQhg6e9Dq5M1vKvfg==", + "dev": true + }, "caseless": { "version": "0.12.0", "resolved": "https://registry.npmjs.org/caseless/-/caseless-0.12.0.tgz", "integrity": "sha1-G2gcIf+EAzyCZUMJBolCDRhxUdw=", "dev": true }, + "chalk": { + "version": "4.1.0", + "resolved": "https://registry.npmjs.org/chalk/-/chalk-4.1.0.tgz", + "integrity": "sha512-qwx12AxXe2Q5xQ43Ac//I6v5aXTipYrSESdOgzrN+9XjgEpyjpKuvSGaN4qE93f7TQTlerQQ8S+EQ0EyDoVL1A==", + "dev": true, + "requires": { + "ansi-styles": "^4.1.0", + "supports-color": "^7.1.0" + }, + "dependencies": { + "supports-color": { + "version": "7.2.0", + "resolved": "https://registry.npmjs.org/supports-color/-/supports-color-7.2.0.tgz", + "integrity": "sha512-qpCAvRl9stuOHveKsn7HncJRvv501qIacKzQlO/+Lwxc9+0q2wLyv4Dfvt80/DPn2pqOBsJdDiogXGR9+OvwRw==", + "dev": true, + "requires": { + "has-flag": "^4.0.0" + } + } + } + }, + "chokidar": { + "version": "3.5.1", + "resolved": "https://registry.npmjs.org/chokidar/-/chokidar-3.5.1.tgz", + "integrity": "sha512-9+s+Od+W0VJJzawDma/gvBNQqkTiqYTWLuZoyAsivsI4AaWTCzHG06/TMjsf1cYe9Cb97UCEhjz7HvnPk2p/tw==", + "dev": true, + "requires": { + "anymatch": "~3.1.1", + "braces": "~3.0.2", + "fsevents": "~2.3.1", + "glob-parent": "~5.1.0", + "is-binary-path": "~2.1.0", + "is-glob": "~4.0.1", + "normalize-path": "~3.0.0", + "readdirp": "~3.5.0" + } + }, + "cliui": { + "version": "7.0.4", + "resolved": "https://registry.npmjs.org/cliui/-/cliui-7.0.4.tgz", + "integrity": "sha512-OcRE68cOsVMXp1Yvonl/fzkQOyjLSu/8bhPDfQt0e0/Eb283TKP20Fs2MqoPsr9SwA595rRCA+QMzYc9nBP+JQ==", + "dev": true, + "requires": { + "string-width": "^4.2.0", + "strip-ansi": "^6.0.0", + "wrap-ansi": "^7.0.0" + }, + "dependencies": { + "ansi-regex": { + "version": "5.0.0", + "resolved": "https://registry.npmjs.org/ansi-regex/-/ansi-regex-5.0.0.tgz", + "integrity": "sha512-bY6fj56OUQ0hU1KjFNDQuJFezqKdrAyFdIevADiqrWHwSlbmBNMHp5ak2f40Pm8JTFyM2mqxkG6ngkHO11f/lg==", + "dev": true + }, + "is-fullwidth-code-point": { + "version": "3.0.0", + "resolved": "https://registry.npmjs.org/is-fullwidth-code-point/-/is-fullwidth-code-point-3.0.0.tgz", + "integrity": "sha512-zymm5+u+sCsSWyD9qNaejV3DFvhCKclKdizYaJUuHA83RLjb7nSuGnddCHGv0hk+KY7BMAlsWeK4Ueg6EV6XQg==", + "dev": true + }, + "string-width": { + "version": "4.2.2", + "resolved": "https://registry.npmjs.org/string-width/-/string-width-4.2.2.tgz", + "integrity": "sha512-XBJbT3N4JhVumXE0eoLU9DCjcaF92KLNqTmFCnG1pf8duUxFGwtP6AD6nkjw9a3IdiRtL3E2w3JDiE/xi3vOeA==", + "dev": true, + "requires": { + "emoji-regex": "^8.0.0", + "is-fullwidth-code-point": "^3.0.0", + "strip-ansi": "^6.0.0" + } + }, + "strip-ansi": { + "version": "6.0.0", + "resolved": "https://registry.npmjs.org/strip-ansi/-/strip-ansi-6.0.0.tgz", + "integrity": "sha512-AuvKTrTfQNYNIctbR1K/YGTR1756GycPsg7b9bdV9Duqur4gv6aKqHXah67Z8ImS7WEz5QVcOtlfW2rZEugt6w==", + "dev": true, + "requires": { + "ansi-regex": "^5.0.0" + } + } + } + }, "co": { "version": "4.6.0", "resolved": "https://registry.npmjs.org/co/-/co-4.6.0.tgz", "integrity": "sha1-bqa989hTrlTMuOR7+gvz+QMfsYQ=", "dev": true }, + "color-convert": { + "version": "2.0.1", + "resolved": "https://registry.npmjs.org/color-convert/-/color-convert-2.0.1.tgz", + "integrity": "sha512-RRECPsj7iu/xb5oKYcsFHSppFNnsj/52OVTRKb4zP5onXwVF3zVmmToNcOfGC+CRDpfK/U584fMg38ZHCaElKQ==", + "dev": true, + "requires": { + "color-name": "~1.1.4" + } + }, + "color-name": { + "version": "1.1.4", + "resolved": "https://registry.npmjs.org/color-name/-/color-name-1.1.4.tgz", + "integrity": "sha512-dOy+3AuW3a2wNbZHIuMZpTcgjGuLU/uBL/ubcZF9OXbDo8ff4O8yVp5Bf0efS8uEoYo5q4Fx7dY9OgQGXgAsQA==", + "dev": true + }, "combined-stream": { "version": "1.0.6", "resolved": "https://registry.npmjs.org/combined-stream/-/combined-stream-1.0.6.tgz", @@ -135,12 +296,6 @@ "delayed-stream": "~1.0.0" } }, - "commander": { - "version": "2.15.1", - "resolved": "http://registry.npmjs.org/commander/-/commander-2.15.1.tgz", - "integrity": "sha512-VlfT9F3V0v+jr4yxPc5gg9s62/fIVWsd2Bk2iD435um1NlGMYdVCq+MjcXnhYq2icNOizHr1kK+5TI6H0Hy0ag==", - "dev": true - }, "concat-map": { "version": "0.0.1", "resolved": "https://registry.npmjs.org/concat-map/-/concat-map-0.0.1.tgz", @@ -183,14 +338,28 @@ } }, "debug": { - "version": "3.1.0", - "resolved": "https://registry.npmjs.org/debug/-/debug-3.1.0.tgz", - "integrity": "sha512-OX8XqP7/1a9cqkxYw2yXss15f26NKWBpDXQd0/uK/KPqdQhxbPa994hnzjcE2VqQpDslf55723cKPUOGSmMY3g==", + "version": "4.3.1", + "resolved": "https://registry.npmjs.org/debug/-/debug-4.3.1.tgz", + "integrity": "sha512-doEwdvm4PCeK4K3RQN2ZC2BYUBaxwLARCqZmMjtF8a51J2Rb0xpVloFRnCODwqjpwnAoao4pelN8l3RJdv3gRQ==", "dev": true, "requires": { - "ms": "2.0.0" + "ms": "2.1.2" + }, + "dependencies": { + "ms": { + "version": "2.1.2", + "resolved": "https://registry.npmjs.org/ms/-/ms-2.1.2.tgz", + "integrity": "sha512-sGkPx+VjMtmA6MX27oA4FBFELFCZZ4S4XqeGOXCv68tT+jb3vk/RyaKWP0PTKyWtmLSM0b+adUTEvbs1PEaH2w==", + "dev": true + } } }, + "decamelize": { + "version": "4.0.0", + "resolved": "https://registry.npmjs.org/decamelize/-/decamelize-4.0.0.tgz", + "integrity": "sha512-9iE1PgSik9HeIIw2JO94IidnE3eBoQrFJ3w7sFuzSX4DpmZ3v5sZpUiV5Swcf6mQEF+Y0ru8Neo+p+nyh2J+hQ==", + "dev": true + }, "delayed-stream": { "version": "1.0.0", "resolved": "https://registry.npmjs.org/delayed-stream/-/delayed-stream-1.0.0.tgz", @@ -204,9 +373,9 @@ "dev": true }, "diff": { - "version": "3.5.0", - "resolved": "https://registry.npmjs.org/diff/-/diff-3.5.0.tgz", - "integrity": "sha512-A46qtFgd+g7pDZinpnwiRJtxbC1hpgf0uzP3iG89scHk0AUC7A1TGxf5OiiOUv/JMZR8GOt8hL900hV0bOy5xA==", + "version": "5.0.0", + "resolved": "https://registry.npmjs.org/diff/-/diff-5.0.0.tgz", + "integrity": "sha512-/VTCrvm5Z0JGty/BWHljh+BAiw3IK+2j87NGMu8Nwc/f48WoDAC395uomO9ZD117ZOBaHmkX1oyLvkVM/aIT3w==", "dev": true }, "ecc-jsbn": { @@ -220,6 +389,24 @@ "safer-buffer": "^2.1.0" } }, + "emoji-regex": { + "version": "8.0.0", + "resolved": "https://registry.npmjs.org/emoji-regex/-/emoji-regex-8.0.0.tgz", + "integrity": "sha512-MSjYzcWNOA0ewAHpz0MxpYFvwg6yjy1NG3xteoqz644VCo/RPgnr1/GGt+ic3iJTzQ8Eu3TdM14SawnVUmGE6A==", + "dev": true + }, + "escalade": { + "version": "3.1.1", + "resolved": "https://registry.npmjs.org/escalade/-/escalade-3.1.1.tgz", + "integrity": "sha512-k0er2gUkLf8O0zKJiAhmkTnJlTvINGv7ygDNPbeIsX/TJjGJZHuh9B2UxbsaEkmlEo9MfhrSzmhIlhRlI2GXnw==", + "dev": true + }, + "escape-string-regexp": { + "version": "4.0.0", + "resolved": "https://registry.npmjs.org/escape-string-regexp/-/escape-string-regexp-4.0.0.tgz", + "integrity": "sha512-TtpcNJ3XAzx3Gq8sWRzJaVajRs0uVxA2YAkdb1jm2YkPz4G6egUFAyA3n5vtEIZefPk5Wa4UXbKuS5fKkJWdgA==", + "dev": true + }, "extend": { "version": "3.0.2", "resolved": "https://registry.npmjs.org/extend/-/extend-3.0.2.tgz", @@ -244,6 +431,31 @@ "integrity": "sha1-1RQsDK7msRifh9OnYREGT4bIu/I=", "dev": true }, + "fill-range": { + "version": "7.0.1", + "resolved": "https://registry.npmjs.org/fill-range/-/fill-range-7.0.1.tgz", + "integrity": "sha512-qOo9F+dMUmC2Lcb4BbVvnKJxTPjCm+RRpe4gDuGrzkL7mEVl/djYSu2OdQ2Pa302N4oqkSg9ir6jaLWJ2USVpQ==", + "dev": true, + "requires": { + "to-regex-range": "^5.0.1" + } + }, + "find-up": { + "version": "5.0.0", + "resolved": "https://registry.npmjs.org/find-up/-/find-up-5.0.0.tgz", + "integrity": "sha512-78/PXT1wlLLDgTzDs7sjq9hzz0vXD+zn+7wypEe4fXQxCmdmqfGsEPQxmiCSQI3ajFV91bVSsvNtrJRiW6nGng==", + "dev": true, + "requires": { + "locate-path": "^6.0.0", + "path-exists": "^4.0.0" + } + }, + "flat": { + "version": "5.0.2", + "resolved": "https://registry.npmjs.org/flat/-/flat-5.0.2.tgz", + "integrity": "sha512-b6suED+5/3rTpUBdG1gupIl8MPFCAMA0QXwmljLhvCUKcUvdE4gWky9zpuGCcXHOsz4J9wPGNWq6OKpmIzz3hQ==", + "dev": true + }, "forever-agent": { "version": "0.6.1", "resolved": "https://registry.npmjs.org/forever-agent/-/forever-agent-0.6.1.tgz", @@ -267,6 +479,19 @@ "integrity": "sha1-FQStJSMVjKpA20onh8sBQRmU6k8=", "dev": true }, + "fsevents": { + "version": "2.3.2", + "resolved": "https://registry.npmjs.org/fsevents/-/fsevents-2.3.2.tgz", + "integrity": "sha512-xiqMQR4xAeHTuB9uWm+fFRcIOgKBMiOBP+eXiyT7jsgVCq1bkVygt00oASowB7EdtpOHaaPgKt812P9ab+DDKA==", + "dev": true, + "optional": true + }, + "get-caller-file": { + "version": "2.0.5", + "resolved": "https://registry.npmjs.org/get-caller-file/-/get-caller-file-2.0.5.tgz", + "integrity": "sha512-DyFP3BM/3YHTQOCUL/w0OZHR0lpKeGrxotcHWcqNEdnltqFwXVfhEBQ94eIo34AfQpo0rGki4cyIiftY06h2Fg==", + "dev": true + }, "getpass": { "version": "0.1.7", "resolved": "https://registry.npmjs.org/getpass/-/getpass-0.1.7.tgz", @@ -277,9 +502,9 @@ } }, "glob": { - "version": "7.1.2", - "resolved": "https://registry.npmjs.org/glob/-/glob-7.1.2.tgz", - "integrity": "sha512-MJTUg1kjuLeQCJ+ccE4Vpa6kKVXkPYJ2mOCQyUuKLcLQsdrMCpBPUi8qVE6+YuaJkozeA9NusTAw3hLr8Xe5EQ==", + "version": "7.1.6", + "resolved": "https://registry.npmjs.org/glob/-/glob-7.1.6.tgz", + "integrity": "sha512-LwaxwyZ72Lk7vZINtNNrywX0ZuLyStrdDtabefZKAY5ZGJhVtgdznluResxNmPitE0SAO+O26sWTHeKSI2wMBA==", "dev": true, "requires": { "fs.realpath": "^1.0.0", @@ -290,6 +515,15 @@ "path-is-absolute": "^1.0.0" } }, + "glob-parent": { + "version": "5.1.2", + "resolved": "https://registry.npmjs.org/glob-parent/-/glob-parent-5.1.2.tgz", + "integrity": "sha512-AOIgSQCepiJYwP3ARnGx+5VnTu2HBYdzbGP45eLw1vr3zB3vZLeyed1sC9hnbcOc9/SrMyM5RPQrkGz4aS9Zow==", + "dev": true, + "requires": { + "is-glob": "^4.0.1" + } + }, "growl": { "version": "1.10.5", "resolved": "https://registry.npmjs.org/growl/-/growl-1.10.5.tgz", @@ -313,9 +547,9 @@ } }, "has-flag": { - "version": "3.0.0", - "resolved": "https://registry.npmjs.org/has-flag/-/has-flag-3.0.0.tgz", - "integrity": "sha1-tdRU3CGZriJWmfNGfloH87lVuv0=", + "version": "4.0.0", + "resolved": "https://registry.npmjs.org/has-flag/-/has-flag-4.0.0.tgz", + "integrity": "sha512-EykJT/Q1KjTWctppgIAgfSO0tKVuZUjhgMr17kqTumMl6Afv3EISleU7qZUzoXDFTAHTDC4NOoG/ZxU3EvlMPQ==", "dev": true }, "hawk": { @@ -331,9 +565,9 @@ } }, "he": { - "version": "1.1.1", - "resolved": "https://registry.npmjs.org/he/-/he-1.1.1.tgz", - "integrity": "sha1-k0EP0hsAlzUVH4howvJx80J+I/0=", + "version": "1.2.0", + "resolved": "https://registry.npmjs.org/he/-/he-1.2.0.tgz", + "integrity": "sha512-F/1DnUGPopORZi0ni+CvrCgHQ5FyEAHRLSApuYWMmrbSwoN2Mn/7k+Gl38gJnR7yyDZk6WLXwiGod1JOWNDKGw==", "dev": true }, "hoek": { @@ -364,9 +598,51 @@ } }, "inherits": { - "version": "2.0.3", - "resolved": "https://registry.npmjs.org/inherits/-/inherits-2.0.3.tgz", - "integrity": "sha1-Yzwsg+PaQqUC9SRmAiSA9CCCYd4=", + "version": "2.0.4", + "resolved": "https://registry.npmjs.org/inherits/-/inherits-2.0.4.tgz", + "integrity": "sha512-k/vGaX4/Yla3WzyMCvTQOXYeIHvqOKtnqBduzTHpzpQZzAskKMhZ2K+EnBiSM9zGSoIFeMpXKxa4dYeZIQqewQ==", + "dev": true + }, + "is-binary-path": { + "version": "2.1.0", + "resolved": "https://registry.npmjs.org/is-binary-path/-/is-binary-path-2.1.0.tgz", + "integrity": "sha512-ZMERYes6pDydyuGidse7OsHxtbI7WVeUEozgR/g7rd0xUimYNlvZRE/K2MgZTjWy725IfelLeVcEM97mmtRGXw==", + "dev": true, + "requires": { + "binary-extensions": "^2.0.0" + } + }, + "is-extglob": { + "version": "2.1.1", + "resolved": "https://registry.npmjs.org/is-extglob/-/is-extglob-2.1.1.tgz", + "integrity": "sha1-qIwCU1eR8C7TfHahueqXc8gz+MI=", + "dev": true + }, + "is-fullwidth-code-point": { + "version": "2.0.0", + "resolved": "https://registry.npmjs.org/is-fullwidth-code-point/-/is-fullwidth-code-point-2.0.0.tgz", + "integrity": "sha1-o7MKXE8ZkYMWeqq5O+764937ZU8=", + "dev": true + }, + "is-glob": { + "version": "4.0.1", + "resolved": "https://registry.npmjs.org/is-glob/-/is-glob-4.0.1.tgz", + "integrity": "sha512-5G0tKtBTFImOqDnLB2hG6Bp2qcKEFduo4tZu9MT/H6NQv/ghhy30o55ufafxJ/LdH79LLs2Kfrn85TLKyA7BUg==", + "dev": true, + "requires": { + "is-extglob": "^2.1.1" + } + }, + "is-number": { + "version": "7.0.0", + "resolved": "https://registry.npmjs.org/is-number/-/is-number-7.0.0.tgz", + "integrity": "sha512-41Cifkg6e8TylSpdtTpeLVMqvSBEVzTttHvERD741+pnZ8ANv0004MRL43QKPDlK9cGvNp6NZWZUBlbGXYxxng==", + "dev": true + }, + "is-plain-obj": { + "version": "2.1.0", + "resolved": "https://registry.npmjs.org/is-plain-obj/-/is-plain-obj-2.1.0.tgz", + "integrity": "sha512-YWnfyRwxL/+SsrWYfOpUtz5b3YD+nyfkHvjbcanzk8zgyO4ASD67uVMRt8k5bM4lLMDnXfriRhOpemw+NfT1eA==", "dev": true }, "is-typedarray": { @@ -375,12 +651,27 @@ "integrity": "sha1-5HnICFjfDBsR3dppQPlgEfzaSpo=", "dev": true }, + "isexe": { + "version": "2.0.0", + "resolved": "https://registry.npmjs.org/isexe/-/isexe-2.0.0.tgz", + "integrity": "sha1-6PvzdNxVb/iUehDcsFctYz8s+hA=", + "dev": true + }, "isstream": { "version": "0.1.2", "resolved": "https://registry.npmjs.org/isstream/-/isstream-0.1.2.tgz", "integrity": "sha1-R+Y/evVa+m+S4VAOaQ64uFKcCZo=", "dev": true }, + "js-yaml": { + "version": "4.0.0", + "resolved": "https://registry.npmjs.org/js-yaml/-/js-yaml-4.0.0.tgz", + "integrity": "sha512-pqon0s+4ScYUvX30wxQi3PogGFAlUyH0awepWvwkj4jD4v+ova3RiYw8bmA6x2rDrEaj8i/oWKoRxpVNW+Re8Q==", + "dev": true, + "requires": { + "argparse": "^2.0.1" + } + }, "jsbn": { "version": "0.1.1", "resolved": "https://registry.npmjs.org/jsbn/-/jsbn-0.1.1.tgz", @@ -418,6 +709,29 @@ "verror": "1.10.0" } }, + "line-transform-stream": { + "version": "0.1.0", + "resolved": "https://registry.npmjs.org/line-transform-stream/-/line-transform-stream-0.1.0.tgz", + "integrity": "sha1-Hvhnmsy/cbBkREwroTsheFs+vXg=" + }, + "locate-path": { + "version": "6.0.0", + "resolved": "https://registry.npmjs.org/locate-path/-/locate-path-6.0.0.tgz", + "integrity": "sha512-iPZK6eYjbxRu3uB4/WZ3EsEIMJFMqAoopl3R+zuq0UjcAm/MO6KCweDgPfP3elTztoKP3KtnVHxTn2NHBSDVUw==", + "dev": true, + "requires": { + "p-locate": "^5.0.0" + } + }, + "log-symbols": { + "version": "4.0.0", + "resolved": "https://registry.npmjs.org/log-symbols/-/log-symbols-4.0.0.tgz", + "integrity": "sha512-FN8JBzLx6CzeMrB0tg6pqlGU1wCrXW+ZXGH481kfsBqer0hToTIiHdjH4Mq8xJUbvATujKCvaREGWpGUionraA==", + "dev": true, + "requires": { + "chalk": "^4.0.0" + } + }, "make-error": { "version": "1.3.6", "resolved": "https://registry.npmjs.org/make-error/-/make-error-1.3.6.tgz", @@ -448,46 +762,37 @@ "brace-expansion": "^1.1.7" } }, - "minimist": { - "version": "0.0.8", - "resolved": "https://registry.npmjs.org/minimist/-/minimist-0.0.8.tgz", - "integrity": "sha1-hX/Kv8M5fSYluCKCYuhqp6ARsF0=", - "dev": true - }, - "mkdirp": { - "version": "0.5.1", - "resolved": "https://registry.npmjs.org/mkdirp/-/mkdirp-0.5.1.tgz", - "integrity": "sha1-MAV0OOrGz3+MR2fzhkjWaX11yQM=", - "dev": true, - "requires": { - "minimist": "0.0.8" - } - }, "mocha": { - "version": "5.2.0", - "resolved": "https://registry.npmjs.org/mocha/-/mocha-5.2.0.tgz", - "integrity": "sha512-2IUgKDhc3J7Uug+FxMXuqIyYzH7gJjXECKe/w43IGgQHTSj3InJi+yAA7T24L9bQMRKiUEHxEX37G5JpVUGLcQ==", + "version": "8.3.2", + "resolved": "https://registry.npmjs.org/mocha/-/mocha-8.3.2.tgz", + "integrity": "sha512-UdmISwr/5w+uXLPKspgoV7/RXZwKRTiTjJ2/AC5ZiEztIoOYdfKb19+9jNmEInzx5pBsCyJQzarAxqIGBNYJhg==", "dev": true, "requires": { + "@ungap/promise-all-settled": "1.1.2", + "ansi-colors": "4.1.1", "browser-stdout": "1.3.1", - "commander": "2.15.1", - "debug": "3.1.0", - "diff": "3.5.0", - "escape-string-regexp": "1.0.5", - "glob": "7.1.2", + "chokidar": "3.5.1", + "debug": "4.3.1", + "diff": "5.0.0", + "escape-string-regexp": "4.0.0", + "find-up": "5.0.0", + "glob": "7.1.6", "growl": "1.10.5", - "he": "1.1.1", + "he": "1.2.0", + "js-yaml": "4.0.0", + "log-symbols": "4.0.0", "minimatch": "3.0.4", - "mkdirp": "0.5.1", - "supports-color": "5.4.0" - }, - "dependencies": { - "escape-string-regexp": { - "version": "1.0.5", - "resolved": "https://registry.npmjs.org/escape-string-regexp/-/escape-string-regexp-1.0.5.tgz", - "integrity": "sha1-G2HAViGQqN/2rjuyzwIAyhMLhtQ=", - "dev": true - } + "ms": "2.1.3", + "nanoid": "3.1.20", + "serialize-javascript": "5.0.1", + "strip-json-comments": "3.1.1", + "supports-color": "8.1.1", + "which": "2.0.2", + "wide-align": "1.1.3", + "workerpool": "6.1.0", + "yargs": "16.2.0", + "yargs-parser": "20.2.4", + "yargs-unparser": "2.0.0" } }, "mocha-appveyor-reporter": { @@ -500,9 +805,21 @@ } }, "ms": { - "version": "2.0.0", - "resolved": "https://registry.npmjs.org/ms/-/ms-2.0.0.tgz", - "integrity": "sha1-VgiurfwAvmwpAd9fmGF4jeDVl8g=", + "version": "2.1.3", + "resolved": "https://registry.npmjs.org/ms/-/ms-2.1.3.tgz", + "integrity": "sha512-6FlzubTLZG3J2a/NVCAleEhjzq5oxgHyaCU9yYXvcLsvoVaHJq/s5xXI6/XXP6tz7R9xAOtHnSO/tXtF3WRTlA==", + "dev": true + }, + "nanoid": { + "version": "3.1.20", + "resolved": "https://registry.npmjs.org/nanoid/-/nanoid-3.1.20.tgz", + "integrity": "sha512-a1cQNyczgKbLX9jwbS/+d7W8fX/RfgYR7lVWwWOGIPNgK2m0MWvrGF6/m4kk6U3QcFMnZf3RIhL0v2Jgh/0Uxw==", + "dev": true + }, + "normalize-path": { + "version": "3.0.0", + "resolved": "https://registry.npmjs.org/normalize-path/-/normalize-path-3.0.0.tgz", + "integrity": "sha512-6eZs5Ls3WtCisHWp9S2GUy8dqkpGi4BVSz3GaqiE6ezub0512ESztXUwUB6C6IKbQkY2Pnb/mD4WYojCRwcwLA==", "dev": true }, "oauth-sign": { @@ -520,6 +837,30 @@ "wrappy": "1" } }, + "p-limit": { + "version": "3.1.0", + "resolved": "https://registry.npmjs.org/p-limit/-/p-limit-3.1.0.tgz", + "integrity": "sha512-TYOanM3wGwNGsZN2cVTYPArw454xnXj5qmWF1bEoAc4+cU/ol7GVh7odevjp1FNHduHc3KZMcFduxU5Xc6uJRQ==", + "dev": true, + "requires": { + "yocto-queue": "^0.1.0" + } + }, + "p-locate": { + "version": "5.0.0", + "resolved": "https://registry.npmjs.org/p-locate/-/p-locate-5.0.0.tgz", + "integrity": "sha512-LaNjtRWUBY++zB5nE/NwcaoMylSPk+S+ZHNB1TzdbMJMny6dynpAGt7X/tl/QYq3TIeE6nxHppbo2LGymrG5Pw==", + "dev": true, + "requires": { + "p-limit": "^3.0.2" + } + }, + "path-exists": { + "version": "4.0.0", + "resolved": "https://registry.npmjs.org/path-exists/-/path-exists-4.0.0.tgz", + "integrity": "sha512-ak9Qy5Q7jYb2Wwcey5Fpvg2KoAc/ZIhLSLOSBmRmygPsGwkVVt0fZa0qrtMz+m6tJTAHfZQ8FnmB4MG4LWy7/w==", + "dev": true + }, "path-is-absolute": { "version": "1.0.1", "resolved": "https://registry.npmjs.org/path-is-absolute/-/path-is-absolute-1.0.1.tgz", @@ -532,6 +873,12 @@ "integrity": "sha1-Ywn04OX6kT7BxpMHrjZLSzd8nns=", "dev": true }, + "picomatch": { + "version": "2.2.2", + "resolved": "https://registry.npmjs.org/picomatch/-/picomatch-2.2.2.tgz", + "integrity": "sha512-q0M/9eZHzmr0AulXyPwNfZjtwZ/RBZlbN3K3CErVrk50T2ASYI7Bye0EvekFY3IP1Nt2DHu0re+V2ZHIpMkuWg==", + "dev": true + }, "punycode": { "version": "1.4.1", "resolved": "https://registry.npmjs.org/punycode/-/punycode-1.4.1.tgz", @@ -544,6 +891,24 @@ "integrity": "sha512-N5ZAX4/LxJmF+7wN74pUD6qAh9/wnvdQcjq9TZjevvXzSUo7bfmw91saqMjzGS2xq91/odN2dW/WOl7qQHNDGA==", "dev": true }, + "randombytes": { + "version": "2.1.0", + "resolved": "https://registry.npmjs.org/randombytes/-/randombytes-2.1.0.tgz", + "integrity": "sha512-vYl3iOX+4CKUWuxGi9Ukhie6fsqXqS9FE2Zaic4tNFD2N2QQaXOMFbuKK4QmDHC0JO6B1Zp41J0LpT0oR68amQ==", + "dev": true, + "requires": { + "safe-buffer": "^5.1.0" + } + }, + "readdirp": { + "version": "3.5.0", + "resolved": "https://registry.npmjs.org/readdirp/-/readdirp-3.5.0.tgz", + "integrity": "sha512-cMhu7c/8rdhkHXWsY+osBhfSy0JikwpHK/5+imo+LpeasTF8ouErHrlYkwT0++njiyuDvc7OFY5T3ukvZ8qmFQ==", + "dev": true, + "requires": { + "picomatch": "^2.2.1" + } + }, "request": { "version": "2.83.0", "resolved": "https://registry.npmjs.org/request/-/request-2.83.0.tgz", @@ -584,6 +949,12 @@ "request": "2.83.0" } }, + "require-directory": { + "version": "2.1.1", + "resolved": "https://registry.npmjs.org/require-directory/-/require-directory-2.1.1.tgz", + "integrity": "sha1-jGStX9MNqxyXbiNE/+f3kqam30I=", + "dev": true + }, "safe-buffer": { "version": "5.1.2", "resolved": "https://registry.npmjs.org/safe-buffer/-/safe-buffer-5.1.2.tgz", @@ -596,10 +967,19 @@ "integrity": "sha512-YZo3K82SD7Riyi0E1EQPojLz7kpepnSQI9IyPbHHg1XXXevb5dJI7tpyN2ADxGcQbHG7vcyRHk0cbwqcQriUtg==", "dev": true }, + "serialize-javascript": { + "version": "5.0.1", + "resolved": "https://registry.npmjs.org/serialize-javascript/-/serialize-javascript-5.0.1.tgz", + "integrity": "sha512-SaaNal9imEO737H2c05Og0/8LUXG7EnsZyMa8MzkmuHoELfT6txuj0cMqRj6zfPKnmQ1yasR4PCJc8x+M4JSPA==", + "dev": true, + "requires": { + "randombytes": "^2.1.0" + } + }, "should": { - "version": "13.2.1", - "resolved": "https://registry.npmjs.org/should/-/should-13.2.1.tgz", - "integrity": "sha512-l+/NwEMO+DcstsHEwPHRHzC9j4UOE3VQwJGcMWSsD/vqpqHbnQ+1iSHy64Ihmmjx1uiRPD9pFadTSc3MJtXAgw==", + "version": "13.2.3", + "resolved": "https://registry.npmjs.org/should/-/should-13.2.3.tgz", + "integrity": "sha512-ggLesLtu2xp+ZxI+ysJTmNjh2U0TsC+rQ/pfED9bUZZ4DKefP27D+7YJVVTvKsmjLpIi9jAa7itwDGkDDmt1GQ==", "dev": true, "requires": { "should-equal": "^2.0.0", @@ -645,9 +1025,9 @@ } }, "should-util": { - "version": "1.0.0", - "resolved": "https://registry.npmjs.org/should-util/-/should-util-1.0.0.tgz", - "integrity": "sha1-yYzaN0qmsZDfi6h8mInCtNtiAGM=", + "version": "1.0.1", + "resolved": "https://registry.npmjs.org/should-util/-/should-util-1.0.1.tgz", + "integrity": "sha512-oXF8tfxx5cDk8r2kYqlkUJzZpDBqVY/II2WhvU0n9Y3XYvAYRmeaf1PvvIvTgPnv4KJ+ES5M0PyDq5Jp+Ygy2g==", "dev": true }, "sntp": { @@ -692,19 +1072,53 @@ "tweetnacl": "~0.14.0" } }, + "string-width": { + "version": "2.1.1", + "resolved": "https://registry.npmjs.org/string-width/-/string-width-2.1.1.tgz", + "integrity": "sha512-nOqH59deCq9SRHlxq1Aw85Jnt4w6KvLKqWVik6oA9ZklXLNIOlqg4F2yrT1MVaTjAqvVwdfeZ7w7aCvJD7ugkw==", + "dev": true, + "requires": { + "is-fullwidth-code-point": "^2.0.0", + "strip-ansi": "^4.0.0" + } + }, "stringstream": { "version": "0.0.6", "resolved": "https://registry.npmjs.org/stringstream/-/stringstream-0.0.6.tgz", "integrity": "sha512-87GEBAkegbBcweToUrdzf3eLhWNg06FJTebl4BVJz/JgWy8CvEr9dRtX5qWphiynMSQlxxi+QqN0z5T32SLlhA==", "dev": true }, + "strip-ansi": { + "version": "4.0.0", + "resolved": "https://registry.npmjs.org/strip-ansi/-/strip-ansi-4.0.0.tgz", + "integrity": "sha1-qEeQIusaw2iocTibY1JixQXuNo8=", + "dev": true, + "requires": { + "ansi-regex": "^3.0.0" + } + }, + "strip-json-comments": { + "version": "3.1.1", + "resolved": "https://registry.npmjs.org/strip-json-comments/-/strip-json-comments-3.1.1.tgz", + "integrity": "sha512-6fPc+R4ihwqP6N/aIv2f1gMH8lOVtWQHoqC4yK6oSDVVocumAsfCqjkXnqiYMhmMwS/mEHLp7Vehlt3ql6lEig==", + "dev": true + }, "supports-color": { - "version": "5.4.0", - "resolved": "https://registry.npmjs.org/supports-color/-/supports-color-5.4.0.tgz", - "integrity": "sha512-zjaXglF5nnWpsq470jSv6P9DwPvgLkuapYmfDm3JWOm0vkNTVF2tI4UrN2r6jH1qM/uc/WtxYY1hYoA2dOKj5w==", + "version": "8.1.1", + "resolved": "https://registry.npmjs.org/supports-color/-/supports-color-8.1.1.tgz", + "integrity": "sha512-MpUEN2OodtUzxvKQl72cUF7RQ5EiHsGvSsVG0ia9c5RbWGL2CI4C7EpPS8UTBIplnlzZiNuV56w+FuNxy3ty2Q==", + "dev": true, + "requires": { + "has-flag": "^4.0.0" + } + }, + "to-regex-range": { + "version": "5.0.1", + "resolved": "https://registry.npmjs.org/to-regex-range/-/to-regex-range-5.0.1.tgz", + "integrity": "sha512-65P7iz6X5yEr1cwcgvQxbbIw7Uk3gOy5dIdtZ4rDveLqhrdJP+Li/Hx6tyK0NEb+2GCyneCMJiGqrADCSNk8sQ==", "dev": true, "requires": { - "has-flag": "^3.0.0" + "is-number": "^7.0.0" } }, "tough-cookie": { @@ -776,17 +1190,165 @@ "extsprintf": "^1.2.0" } }, + "which": { + "version": "2.0.2", + "resolved": "https://registry.npmjs.org/which/-/which-2.0.2.tgz", + "integrity": "sha512-BLI3Tl1TW3Pvl70l3yq3Y64i+awpwXqsGBYWkkqMtnbXgrMD+yj7rhW0kuEDxzJaYXGjEW5ogapKNMEKNMjibA==", + "dev": true, + "requires": { + "isexe": "^2.0.0" + } + }, + "wide-align": { + "version": "1.1.3", + "resolved": "https://registry.npmjs.org/wide-align/-/wide-align-1.1.3.tgz", + "integrity": "sha512-QGkOQc8XL6Bt5PwnsExKBPuMKBxnGxWWW3fU55Xt4feHozMUhdUMaBCk290qpm/wG5u/RSKzwdAC4i51YigihA==", + "dev": true, + "requires": { + "string-width": "^1.0.2 || 2" + } + }, + "workerpool": { + "version": "6.1.0", + "resolved": "https://registry.npmjs.org/workerpool/-/workerpool-6.1.0.tgz", + "integrity": "sha512-toV7q9rWNYha963Pl/qyeZ6wG+3nnsyvolaNUS8+R5Wtw6qJPTxIlOP1ZSvcGhEJw+l3HMMmtiNo9Gl61G4GVg==", + "dev": true + }, + "wrap-ansi": { + "version": "7.0.0", + "resolved": "https://registry.npmjs.org/wrap-ansi/-/wrap-ansi-7.0.0.tgz", + "integrity": "sha512-YVGIj2kamLSTxw6NsZjoBxfSwsn0ycdesmc4p+Q21c5zPuZ1pl+NfxVdxPtdHvmNVOQ6XSYG4AUtyt/Fi7D16Q==", + "dev": true, + "requires": { + "ansi-styles": "^4.0.0", + "string-width": "^4.1.0", + "strip-ansi": "^6.0.0" + }, + "dependencies": { + "ansi-regex": { + "version": "5.0.0", + "resolved": "https://registry.npmjs.org/ansi-regex/-/ansi-regex-5.0.0.tgz", + "integrity": "sha512-bY6fj56OUQ0hU1KjFNDQuJFezqKdrAyFdIevADiqrWHwSlbmBNMHp5ak2f40Pm8JTFyM2mqxkG6ngkHO11f/lg==", + "dev": true + }, + "is-fullwidth-code-point": { + "version": "3.0.0", + "resolved": "https://registry.npmjs.org/is-fullwidth-code-point/-/is-fullwidth-code-point-3.0.0.tgz", + "integrity": "sha512-zymm5+u+sCsSWyD9qNaejV3DFvhCKclKdizYaJUuHA83RLjb7nSuGnddCHGv0hk+KY7BMAlsWeK4Ueg6EV6XQg==", + "dev": true + }, + "string-width": { + "version": "4.2.2", + "resolved": "https://registry.npmjs.org/string-width/-/string-width-4.2.2.tgz", + "integrity": "sha512-XBJbT3N4JhVumXE0eoLU9DCjcaF92KLNqTmFCnG1pf8duUxFGwtP6AD6nkjw9a3IdiRtL3E2w3JDiE/xi3vOeA==", + "dev": true, + "requires": { + "emoji-regex": "^8.0.0", + "is-fullwidth-code-point": "^3.0.0", + "strip-ansi": "^6.0.0" + } + }, + "strip-ansi": { + "version": "6.0.0", + "resolved": "https://registry.npmjs.org/strip-ansi/-/strip-ansi-6.0.0.tgz", + "integrity": "sha512-AuvKTrTfQNYNIctbR1K/YGTR1756GycPsg7b9bdV9Duqur4gv6aKqHXah67Z8ImS7WEz5QVcOtlfW2rZEugt6w==", + "dev": true, + "requires": { + "ansi-regex": "^5.0.0" + } + } + } + }, "wrappy": { "version": "1.0.2", "resolved": "https://registry.npmjs.org/wrappy/-/wrappy-1.0.2.tgz", "integrity": "sha1-tSQ9jz7BqjXxNkYFvA0QNuMKtp8=", "dev": true }, + "y18n": { + "version": "5.0.5", + "resolved": "https://registry.npmjs.org/y18n/-/y18n-5.0.5.tgz", + "integrity": "sha512-hsRUr4FFrvhhRH12wOdfs38Gy7k2FFzB9qgN9v3aLykRq0dRcdcpz5C9FxdS2NuhOrI/628b/KSTJ3rwHysYSg==", + "dev": true + }, + "yargs": { + "version": "16.2.0", + "resolved": "https://registry.npmjs.org/yargs/-/yargs-16.2.0.tgz", + "integrity": "sha512-D1mvvtDG0L5ft/jGWkLpG1+m0eQxOfaBvTNELraWj22wSVUMWxZUvYgJYcKh6jGGIkJFhH4IZPQhR4TKpc8mBw==", + "dev": true, + "requires": { + "cliui": "^7.0.2", + "escalade": "^3.1.1", + "get-caller-file": "^2.0.5", + "require-directory": "^2.1.1", + "string-width": "^4.2.0", + "y18n": "^5.0.5", + "yargs-parser": "^20.2.2" + }, + "dependencies": { + "ansi-regex": { + "version": "5.0.0", + "resolved": "https://registry.npmjs.org/ansi-regex/-/ansi-regex-5.0.0.tgz", + "integrity": "sha512-bY6fj56OUQ0hU1KjFNDQuJFezqKdrAyFdIevADiqrWHwSlbmBNMHp5ak2f40Pm8JTFyM2mqxkG6ngkHO11f/lg==", + "dev": true + }, + "is-fullwidth-code-point": { + "version": "3.0.0", + "resolved": "https://registry.npmjs.org/is-fullwidth-code-point/-/is-fullwidth-code-point-3.0.0.tgz", + "integrity": "sha512-zymm5+u+sCsSWyD9qNaejV3DFvhCKclKdizYaJUuHA83RLjb7nSuGnddCHGv0hk+KY7BMAlsWeK4Ueg6EV6XQg==", + "dev": true + }, + "string-width": { + "version": "4.2.2", + "resolved": "https://registry.npmjs.org/string-width/-/string-width-4.2.2.tgz", + "integrity": "sha512-XBJbT3N4JhVumXE0eoLU9DCjcaF92KLNqTmFCnG1pf8duUxFGwtP6AD6nkjw9a3IdiRtL3E2w3JDiE/xi3vOeA==", + "dev": true, + "requires": { + "emoji-regex": "^8.0.0", + "is-fullwidth-code-point": "^3.0.0", + "strip-ansi": "^6.0.0" + } + }, + "strip-ansi": { + "version": "6.0.0", + "resolved": "https://registry.npmjs.org/strip-ansi/-/strip-ansi-6.0.0.tgz", + "integrity": "sha512-AuvKTrTfQNYNIctbR1K/YGTR1756GycPsg7b9bdV9Duqur4gv6aKqHXah67Z8ImS7WEz5QVcOtlfW2rZEugt6w==", + "dev": true, + "requires": { + "ansi-regex": "^5.0.0" + } + } + } + }, + "yargs-parser": { + "version": "20.2.4", + "resolved": "https://registry.npmjs.org/yargs-parser/-/yargs-parser-20.2.4.tgz", + "integrity": "sha512-WOkpgNhPTlE73h4VFAFsOnomJVaovO8VqLDzy5saChRBFQFBoMYirowyW+Q9HB4HFF4Z7VZTiG3iSzJJA29yRA==", + "dev": true + }, + "yargs-unparser": { + "version": "2.0.0", + "resolved": "https://registry.npmjs.org/yargs-unparser/-/yargs-unparser-2.0.0.tgz", + "integrity": "sha512-7pRTIA9Qc1caZ0bZ6RYRGbHJthJWuakf+WmHK0rVeLkNrrGhfoabBNdue6kdINI6r4if7ocq9aD/n7xwKOdzOA==", + "dev": true, + "requires": { + "camelcase": "^6.0.0", + "decamelize": "^4.0.0", + "flat": "^5.0.2", + "is-plain-obj": "^2.1.0" + } + }, "yn": { "version": "3.1.1", "resolved": "https://registry.npmjs.org/yn/-/yn-3.1.1.tgz", "integrity": "sha512-Ux4ygGWsu2c7isFWe8Yu1YluJmqVhxqK2cLXNQA5AcC3QfbGNpM7fu0Y8b/z16pXLnFxZYvWhd3fhBY9DLmC6Q==", "dev": true + }, + "yocto-queue": { + "version": "0.1.0", + "resolved": "https://registry.npmjs.org/yocto-queue/-/yocto-queue-0.1.0.tgz", + "integrity": "sha512-rVksvsnNCdJ/ohGc6xgPwyN8eheCxsiLM8mxuE/t/mOVqJewPuO1miLpTHQiRgTKCLexL4MeAFVagts7HmNZ2Q==", + "dev": true } } } diff --git a/package.json b/package.json index 6bad030..21e1507 100644 --- a/package.json +++ b/package.json @@ -10,11 +10,13 @@ "appveyorTest": "tsc -p ./ && nyc mocha --reporter mocha-appveyor-reporter test/*.js", "compile": "tsc -watch -p ./" }, - "dependencies": {}, + "dependencies": { + "line-transform-stream": "^0.1.0" + }, "devDependencies": { - "@types/mocha": "^5.2.5", + "@types/mocha": "^8.2.1", "@types/node": "^10.5.2", - "mocha": "^5.2.0", + "mocha": "^8.2.1", "mocha-appveyor-reporter": "^0.4.0", "should": "^13.2.1", "ts-node": "^9.0.0", diff --git a/test/test-python-shell.ts b/test/test-python-shell.ts index aae6500..b4c510d 100644 --- a/test/test-python-shell.ts +++ b/test/test-python-shell.ts @@ -358,13 +358,17 @@ describe('PythonShell', function () { }).end(done); }); it('should properly buffer partial messages', function (done) { - let pyshell = new PythonShell('echo_json.py', { + let pyshell = new PythonShell('echo_text.py', { mode: 'json' }); - pyshell.on('message', function (message) { + pyshell.on('message', (message) => { + console.log(message) message.should.be.an.Object; message.should.eql({ a: true }); - }).receive('{"a"').receive(':').receive('true}' + newline + '{').receive('"a":true}' + newline).end(done); + }).send('{"a"').send(':').send('true}' + newline + '{').send('"a":true}' + newline).end(() => { + console.log('done called') + done() + }); }); it('should not be invoked when mode is "binary"', function (done) { let pyshell = new PythonShell('echo_args.py', { From 9c7be5f34f2beaeb032434fa94544c7eee2f14c4 Mon Sep 17 00:00:00 2001 From: almenon Date: Sun, 21 Mar 2021 10:56:58 -0700 Subject: [PATCH 21/23] fix typo --- index.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/index.ts b/index.ts index 1ca0e75..4210528 100644 --- a/index.ts +++ b/index.ts @@ -158,7 +158,7 @@ export class PythonShell extends EventEmitter { })) } if (this.stderrParser && this.stderr) { - this.stdout.pipe(new LineTransformStream((data) => { + this.stderr.pipe(new LineTransformStream((data) => { this.emit('stderr', self.stderrParser(data)); })) } From 22aa8bc16c4067987b7c235a2c9d240970760187 Mon Sep 17 00:00:00 2001 From: almenon Date: Sat, 27 Mar 2021 12:48:28 -0700 Subject: [PATCH 22/23] remove LineTransformStream I went in favor of a native solution. LineTransformStream had some bugs. --- CHANGELOG.md | 1 + README.md | 28 +++++----- index.ts | 55 ++++++++++++++----- package.json | 4 +- test/python/echo_text_with_newline_control.py | 5 ++ test/test-python-shell.ts | 41 +++++++++----- 6 files changed, 88 insertions(+), 46 deletions(-) create mode 100644 test/python/echo_text_with_newline_control.py diff --git a/CHANGELOG.md b/CHANGELOG.md index da922de..731ebca 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,7 @@ ### Changed - **BREAKING** Default python path changed back to `python` on Windows. [#237](https://github.com/extrabacon/python-shell/issues/237) - **BREAKING** `error` event renamed to `pythonError` event. [#118](https://github.com/extrabacon/python-shell/issues/118) +- **BREAKING** `receive` methods removed in favor of `splitter` arguments in the constructor. This lets the default splitting logic reside in a reuseable stream transformer. Now if you have extra pipes you can reuse `newlineTransformer` to split incoming data into newline-seperated lines. ### Added - `error` event that is fired upon failure to launch process, among other things. [#118](https://github.com/extrabacon/python-shell/issues/118) diff --git a/README.md b/README.md index a3f228d..e408cb5 100644 --- a/README.md +++ b/README.md @@ -161,17 +161,19 @@ Creates an instance of `PythonShell` and starts the Python process * `script`: the path of the script to execute * `options`: the execution options, consisting of: * `mode`: Configures how data is exchanged when data flows through stdin and stdout. The possible values are: - * `text`: each line of data (ending with "\n") is emitted as a message (default) - * `json`: each line of data (ending with "\n") is parsed as JSON and emitted as a message + * `text`: each line of data is emitted as a message (default) + * `json`: each line of data is parsed as JSON and emitted as a message * `binary`: data is streamed as-is through `stdout` and `stdin` - * `formatter`: each message to send is transformed using this method, then appended with "\n" - * `parser`: each line of data (ending with "\n") is parsed with this function and its result is emitted as a message - * `stderrParser`: each line of logs (ending with "\n") is parsed with this function and its result is emitted as a message + * `formatter`: each message to send is transformed using this method, then appended with a newline + * `parser`: each line of data is parsed with this function and its result is emitted as a message + * `stderrParser`: each line of logs is parsed with this function and its result is emitted as a message * `encoding`: the text encoding to apply on the child process streams (default: "utf8") * `pythonPath`: The path where to locate the "python" executable. Default: "python3" ("python" for Windows) * `pythonOptions`: Array of option switches to pass to "python" * `scriptPath`: The default path where to look for scripts. Default is the current working directory. * `args`: Array of arguments to pass to the script +* `stdoutSplitter`: splits stdout into chunks, defaulting to splitting into newline-seperated lines +* `stderrSplitter`: splits stderr into chunks, defaulting to splitting into newline-seperated lines Other options are forwarded to `child_process.spawn`. @@ -269,14 +271,6 @@ let shell = new PythonShell('script.py', { mode: 'json'}); shell.send({ command: "do_stuff", args: [1, 2, 3] }); ``` -#### `.receive(data)` - -Parses incoming data from the Python script written via stdout and emits `message` events. This method is called automatically as data is being received from stdout. - -#### `.receiveStderr(data)` - -Parses incoming logs from the Python script written via stderr and emits `stderr` events. This method is called automatically as data is being received from stderr. - #### `.end(callback)` Closes the stdin stream, allowing the Python script to finish and exit. The optional callback is invoked when the process is terminated. @@ -287,7 +281,7 @@ Terminates the python script. A kill signal may be provided by `signal`, if `sig #### event: `message` -Fires when a chunk of data is parsed from the stdout stream via the `receive` method. If a `parser` method is specified, the result of this function will be the message value. This event is not emitted in binary mode. +After the stdout stream is split into chunks by stdoutSplitter the chunks are parsed by the parser and a message event is emitted for each parsed chunk. This event is not emitted in binary mode. Example: @@ -307,7 +301,7 @@ shell.on('message', function (message) { #### event: `stderr` -Fires when a chunk of logs is parsed from the stderr stream via the `receiveStderr` method. If a `stderrParser` method is specified, the result of this function will be the message value. This event is not emitted in binary mode. +After the stderr stream is split into chunks by stderrSplitter the chunks are parsed by the parser and a message event is emitted for each parsed chunk. This event is not emitted in binary mode. Example: @@ -336,6 +330,10 @@ Fires when: If the process could not be spawned please double-check that python can be launched from the terminal. +### NewlineTransformer + +A utility class for splitting stream data into newlines. Used as the default for stdoutSplitter and stderrSplitter if they are unspecified. You can use this class for any extra python streams if you'd like. + ## Used By: Python-Shell is used by [arepl-vscode](https://github.com/almenon/arepl-vscode), [gitinspector](https://github.com/ejwa/gitinspector), [pyspreadsheet](https://github.com/extrabacon/pyspreadsheet), [AtlantOS Ocean Data QC](https://github.com/ocean-data-qc/ocean-data-qc) and more! diff --git a/index.ts b/index.ts index 4210528..1e5032d 100644 --- a/index.ts +++ b/index.ts @@ -2,10 +2,9 @@ import { EventEmitter } from 'events'; import { ChildProcess, spawn, SpawnOptions, exec, execSync } from 'child_process'; import { EOL as newline, tmpdir } from 'os'; import { join, sep } from 'path' -import { Readable, Writable } from 'stream' +import { Readable, Transform, TransformCallback, Writable } from 'stream' import { writeFile, writeFileSync } from 'fs'; import { promisify } from 'util'; -const LineTransformStream = require('line-transform-stream') function toArray(source?: T | T[]): T[] { if (typeof source === 'undefined' || source === null) { @@ -44,9 +43,9 @@ export interface Options extends SpawnOptions { * if binary is enabled message and stderr events will not be emitted */ mode?: 'text' | 'json' | 'binary' - formatter?: (param: string) => any - parser?: (param: string) => any - stderrParser?: (param: string) => any + formatter?: string | ((param: string) => any) + parser?: string | ((param: string) => any) + stderrParser?: string | ((param: string) => any) encoding?: string pythonPath?: string /** @@ -68,6 +67,28 @@ export class PythonShellError extends Error { exitCode?: number; } +/** + * Takes in a string stream and emits batches seperated by newlines + */ +export class NewlineTransformer extends Transform { + // NewlineTransformer: Megatron's little known once-removed cousin + private _lastLineData: string; + _transform(chunk: any, encoding: string, callback: TransformCallback){ + let data: string = chunk.toString() + if (this._lastLineData) data = this._lastLineData + data + const lines = data.split(newline) + this._lastLineData = lines.pop() + //@ts-ignore this works, node ignores the encoding if it's a number + lines.forEach(this.push.bind(this)) + callback() + } + _flush(done: TransformCallback){ + if (this._lastLineData) this.push(this._lastLineData) + this._lastLineData = null; + done() + } +} + /** * An interactive Python shell exchanging data through stdio * @param {string} script The python script to execute @@ -103,7 +124,7 @@ export class PythonShell extends EventEmitter { * @param scriptPath path to script. Relative to current directory or options.scriptFolder if specified * @param options */ - constructor(scriptPath: string, options?: Options) { + constructor(scriptPath: string, options?: Options, stdoutSplitter: Transform = null, stderrSplitter: Transform = null) { super(); /** @@ -151,16 +172,24 @@ export class PythonShell extends EventEmitter { // Node buffers stdout&stderr in batches regardless of newline placement // This is troublesome if you want to recieve distinct individual messages // for example JSON parsing breaks if it recieves partial JSON - // so we use LineTransformStream to emit each batch seperated by newline + // so we use newlineTransformer to emit each batch seperated by newline if (this.parser && this.stdout) { - this.stdout.pipe(new LineTransformStream((data) => { - this.emit('message', self.parser(data)); - })) + if(!stdoutSplitter) stdoutSplitter = new NewlineTransformer() + // note that setting the encoding turns the chunk into a string + stdoutSplitter.setEncoding(options.encoding || 'utf8') + this.stdout.pipe(stdoutSplitter).on('data', (chunk: string) => { + this.emit('message', self.parser(chunk)); + }); } + + // listen to stderr and emit errors for incoming data if (this.stderrParser && this.stderr) { - this.stderr.pipe(new LineTransformStream((data) => { - this.emit('stderr', self.stderrParser(data)); - })) + if(!stderrSplitter) stderrSplitter = new NewlineTransformer() + // note that setting the encoding turns the chunk into a string + stderrSplitter.setEncoding(options.encoding || 'utf8') + this.stderr.pipe(stderrSplitter).on('data', (chunk: string) => { + this.emit('stderr', self.stderrParser(chunk)); + }); } if (this.stderr) { diff --git a/package.json b/package.json index 21e1507..a8a30df 100644 --- a/package.json +++ b/package.json @@ -10,12 +10,10 @@ "appveyorTest": "tsc -p ./ && nyc mocha --reporter mocha-appveyor-reporter test/*.js", "compile": "tsc -watch -p ./" }, - "dependencies": { - "line-transform-stream": "^0.1.0" - }, "devDependencies": { "@types/mocha": "^8.2.1", "@types/node": "^10.5.2", + "@types/should": "^13.0.0", "mocha": "^8.2.1", "mocha-appveyor-reporter": "^0.4.0", "should": "^13.2.1", diff --git a/test/python/echo_text_with_newline_control.py b/test/python/echo_text_with_newline_control.py new file mode 100644 index 0000000..0ce4fc2 --- /dev/null +++ b/test/python/echo_text_with_newline_control.py @@ -0,0 +1,5 @@ +import sys, json, os + +for line in sys.stdin: + line = line.replace('$', os.linesep) + print(line[:-1], end='') diff --git a/test/test-python-shell.ts b/test/test-python-shell.ts index b4c510d..5635f83 100644 --- a/test/test-python-shell.ts +++ b/test/test-python-shell.ts @@ -328,7 +328,7 @@ describe('PythonShell', function () { }); }); - describe('.receive(data)', function () { + describe('stdout', function () { it('should emit messages as strings when mode is "text"', function (done) { let pyshell = new PythonShell('echo_text.py', { mode: 'text' @@ -358,15 +358,16 @@ describe('PythonShell', function () { }).end(done); }); it('should properly buffer partial messages', function (done) { - let pyshell = new PythonShell('echo_text.py', { - mode: 'json' + // echo_text_with_newline_control echoes text with $'s replaced with newlines + let pyshell = new PythonShell('echo_text_with_newline_control.py', { + mode: 'text' }); pyshell.on('message', (message) => { console.log(message) - message.should.be.an.Object; - message.should.eql({ a: true }); - }).send('{"a"').send(':').send('true}' + newline + '{').send('"a":true}' + newline).end(() => { - console.log('done called') + let messageObject = JSON.parse(message) + messageObject.should.be.an.Object; + messageObject.should.eql({ a: true }); + }).send('{"a"').send(':').send('true}${').send('"a":true}$').end(() => { done() }); }); @@ -375,10 +376,10 @@ describe('PythonShell', function () { args: ['hello', 'world'], mode: 'binary' }); - pyshell.receive = function () { + pyshell.on('message', ()=>{ done('should not emit messages in binary mode'); return undefined - }; + }); pyshell.end(done); }); it('should use a custom parser function', function (done) { @@ -399,7 +400,7 @@ describe('PythonShell', function () { }); }); - describe('.receiveStderr(data)', function () { + describe('stderr', function () { it('should emit stderr logs as strings when mode is "text"', function (done) { let pyshell = new PythonShell('stderrLogging.py', { mode: 'text' @@ -415,13 +416,14 @@ describe('PythonShell', function () { }); it('should not be invoked when mode is "binary"', function (done) { let pyshell = new PythonShell('stderrLogging.py', { - mode: 'binary' + stderrParser: 'binary' }); - pyshell.receiveStderr = function () { + pyshell.on('stderr', ()=>{ done('should not emit stderr in binary mode'); - return undefined - }; - pyshell.end(done); + }); + pyshell.end(()=>{ + done() + }); }); it('should use a custom parser function', function (done) { let pyshell = new PythonShell('stderrLogging.py', { @@ -501,6 +503,15 @@ describe('PythonShell', function () { done(); }); }); + it('should work in json mode', function (done) { + let pyshell = new PythonShell('error.py', {mode: 'json'}); + pyshell.on('pythonError', function (err) { + err.stack.should.containEql('----- Python Traceback -----'); + err.stack.should.containEql('File "test' + sep + 'python' + sep + 'error.py", line 4'); + err.stack.should.containEql('File "test' + sep + 'python' + sep + 'error.py", line 6'); + done(); + }); + }); }); describe('.terminate()', function () { From 0ab46240f1076c371e4a914d7c7fdca3e187f203 Mon Sep 17 00:00:00 2001 From: almenon Date: Sat, 27 Mar 2021 13:12:31 -0700 Subject: [PATCH 23/23] add comment --- index.ts | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/index.ts b/index.ts index 1e5032d..44209e6 100644 --- a/index.ts +++ b/index.ts @@ -93,6 +93,8 @@ export class NewlineTransformer extends Transform { * An interactive Python shell exchanging data through stdio * @param {string} script The python script to execute * @param {object} [options] The launch options (also passed to child_process.spawn) + * @param [stdoutSplitter] Optional. Splits stdout into chunks, defaulting to splitting into newline-seperated lines + * @param [stderrSplitter] Optional. splits stderr into chunks, defaulting to splitting into newline-seperated lines * @constructor */ export class PythonShell extends EventEmitter { @@ -123,6 +125,8 @@ export class PythonShell extends EventEmitter { * spawns a python process * @param scriptPath path to script. Relative to current directory or options.scriptFolder if specified * @param options + * @param stdoutSplitter Optional. Splits stdout into chunks, defaulting to splitting into newline-seperated lines + * @param stderrSplitter Optional. splits stderr into chunks, defaulting to splitting into newline-seperated lines */ constructor(scriptPath: string, options?: Options, stdoutSplitter: Transform = null, stderrSplitter: Transform = null) { super();