-
Notifications
You must be signed in to change notification settings - Fork 1
Remove old dependencies #12
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
053fcd8
Remove old dependencies
GrahamCampbell 5010d2a
Apply feedback from code review
GrahamCampbell 2d7fb0d
Added more tests
GrahamCampbell b5c8675
Update spawn.test.js
GrahamCampbell a04ddd8
Applied feedback from additional round of review
GrahamCampbell File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,228 @@ | ||
| 'use strict'; | ||
|
|
||
| const spawn = require('cross-spawn'); | ||
| const { PassThrough } = require('stream'); | ||
|
|
||
| const sensitiveOptionNamePattern = | ||
| /(?:^|[-_])(?:auth|authorization|credential|password|passwd|pwd|secret|token|api[-_]?key|access[-_]?key)(?:$|[-_])/i; | ||
|
|
||
| const toBuffer = (chunk) => (Buffer.isBuffer(chunk) ? chunk : Buffer.from(chunk)); | ||
|
|
||
| const createBufferState = () => ({ | ||
| buffer: Buffer.alloc(0), | ||
| chunks: [], | ||
| dirty: false, | ||
| length: 0, | ||
| }); | ||
|
|
||
| const appendBuffer = (state, chunk) => { | ||
| const buffer = toBuffer(chunk); | ||
|
|
||
| state.chunks.push(buffer); | ||
| state.length += buffer.length; | ||
| state.dirty = true; | ||
|
|
||
| return buffer; | ||
| }; | ||
|
|
||
| const getBuffer = (state) => { | ||
| if (state.dirty) { | ||
| state.buffer = Buffer.concat(state.chunks, state.length); | ||
| state.dirty = false; | ||
| } | ||
|
|
||
| return state.buffer; | ||
| }; | ||
|
|
||
| const redactArgs = (args) => { | ||
| const redactedArgs = []; | ||
| let redactNext = false; | ||
|
|
||
| for (const arg of args) { | ||
| const value = String(arg); | ||
|
|
||
| if (redactNext) { | ||
| redactedArgs.push('<redacted>'); | ||
| redactNext = false; | ||
| continue; | ||
| } | ||
|
|
||
| const equalsIndex = value.indexOf('='); | ||
| const optionName = value.replace(/^-+/, '').split('=')[0]; | ||
|
|
||
| if (equalsIndex !== -1 && sensitiveOptionNamePattern.test(optionName)) { | ||
| redactedArgs.push(`${value.slice(0, equalsIndex + 1)}<redacted>`); | ||
| continue; | ||
| } | ||
|
|
||
| if (value.startsWith('-') && sensitiveOptionNamePattern.test(optionName)) { | ||
| redactedArgs.push(value); | ||
| redactNext = true; | ||
| continue; | ||
| } | ||
|
|
||
| redactedArgs.push(value); | ||
| } | ||
|
|
||
| return redactedArgs; | ||
| }; | ||
|
|
||
| module.exports = (command, args = [], options = {}) => { | ||
| const normalizedCommand = String(command); | ||
| const normalizedArgs = args == null ? [] : Array.from(args, String); | ||
| const { shouldCloseStdin, input, ...spawnOptions } = options || {}; | ||
|
|
||
| const child = spawn(normalizedCommand, normalizedArgs, spawnOptions); | ||
| const result = { | ||
| child, | ||
| stdout: child.stdout || null, | ||
| stderr: child.stderr || null, | ||
| std: child.stdout || child.stderr ? new PassThrough() : null, | ||
| code: undefined, | ||
| signal: undefined, | ||
| }; | ||
| if (result.std) result.std.resume(); | ||
|
|
||
| const stdoutState = createBufferState(); | ||
| const stderrState = createBufferState(); | ||
| const stdState = createBufferState(); | ||
| const outputStreams = [result.stdout, result.stderr].filter(Boolean); | ||
| const discardStdData = () => {}; | ||
| let settled = false; | ||
| let waitingForStdDrain = false; | ||
| const pausedForStd = new Set(); | ||
|
|
||
| const resumeStdPausedStreams = () => { | ||
| waitingForStdDrain = false; | ||
|
|
||
| for (const stream of pausedForStd) { | ||
| stream.resume(); | ||
| } | ||
|
|
||
| pausedForStd.clear(); | ||
| }; | ||
|
|
||
| const hasActiveStdConsumer = () => | ||
| result.std && | ||
| (result.std.listenerCount('data') > 1 || result.std.listenerCount('readable') > 0); | ||
|
|
||
| const pauseForStdBackpressure = () => { | ||
| for (const stream of outputStreams) { | ||
| if (!stream.isPaused || stream.isPaused()) continue; | ||
| stream.pause(); | ||
| pausedForStd.add(stream); | ||
| } | ||
|
|
||
| if (!waitingForStdDrain) { | ||
| waitingForStdDrain = true; | ||
| result.std.once('drain', resumeStdPausedStreams); | ||
| } | ||
| }; | ||
|
|
||
| const writeStd = (chunk) => { | ||
| if (!result.std || result.std.destroyed || result.std.writableEnded) return; | ||
|
|
||
| if (result.std.write(chunk) === false) { | ||
| if (hasActiveStdConsumer()) { | ||
| pauseForStdBackpressure(); | ||
| } else { | ||
| result.std.resume(); | ||
| } | ||
| } | ||
| }; | ||
|
|
||
| const snapshot = () => ({ | ||
| child: result.child, | ||
| stdout: result.stdout, | ||
| stderr: result.stderr, | ||
| std: result.std, | ||
| stdoutBuffer: getBuffer(stdoutState), | ||
| stderrBuffer: getBuffer(stderrState), | ||
| stdBuffer: getBuffer(stdState), | ||
| code: result.code, | ||
| signal: result.signal, | ||
| }); | ||
|
|
||
| const endStd = () => { | ||
| if (result.std && !result.std.destroyed && !result.std.writableEnded) { | ||
| result.std.end(); | ||
| } | ||
|
|
||
| resumeStdPausedStreams(); | ||
| }; | ||
|
|
||
| if (result.std) { | ||
| result.std.on('data', discardStdData); | ||
| result.std.once('close', resumeStdPausedStreams); | ||
| result.std.once('error', resumeStdPausedStreams); | ||
| } | ||
|
|
||
| if (child.stdout) { | ||
| child.stdout.on('data', (chunk) => { | ||
| const buffer = appendBuffer(stdoutState, chunk); | ||
| appendBuffer(stdState, buffer); | ||
| writeStd(buffer); | ||
| }); | ||
| } | ||
|
|
||
| if (child.stderr) { | ||
| child.stderr.on('data', (chunk) => { | ||
| const buffer = appendBuffer(stderrState, chunk); | ||
| appendBuffer(stdState, buffer); | ||
| writeStd(buffer); | ||
| }); | ||
| } | ||
|
|
||
| const promise = new Promise((resolve, reject) => { | ||
| child.on('error', (error) => { | ||
| if (settled) return; | ||
| settled = true; | ||
| endStd(); | ||
| const metadata = snapshot(); | ||
| if (metadata.code === undefined) delete metadata.code; | ||
| if (metadata.signal === undefined) delete metadata.signal; | ||
| Object.assign(error, metadata); | ||
| reject(error); | ||
| }); | ||
|
|
||
| child.on('close', (code, signal) => { | ||
| if (settled) return; | ||
| settled = true; | ||
| result.code = code; | ||
| result.signal = signal; | ||
| endStd(); | ||
|
|
||
| if (code === 0) { | ||
| resolve(snapshot()); | ||
| return; | ||
| } | ||
|
|
||
| const reason = signal ? `signal ${signal}` : `code ${code}`; | ||
| const error = new Error( | ||
| `\`${[normalizedCommand, ...redactArgs(normalizedArgs)].join(' ')}\` Exited with ${reason}` | ||
| ); | ||
| error.code = code; | ||
| error.signal = signal; | ||
| Object.assign(error, snapshot()); | ||
| reject(error); | ||
| }); | ||
|
|
||
| if (input != null && child.stdin) { | ||
| child.stdin.end(input); | ||
| } else if (shouldCloseStdin && child.stdin) { | ||
| child.stdin.end(); | ||
| } | ||
| }); | ||
|
|
||
| return Object.defineProperties(promise, { | ||
| child: { enumerable: true, get: () => result.child }, | ||
| stdout: { enumerable: true, get: () => result.stdout }, | ||
| stderr: { enumerable: true, get: () => result.stderr }, | ||
| std: { enumerable: true, get: () => result.std }, | ||
| stdoutBuffer: { enumerable: true, get: () => getBuffer(stdoutState) }, | ||
| stderrBuffer: { enumerable: true, get: () => getBuffer(stderrState) }, | ||
| stdBuffer: { enumerable: true, get: () => getBuffer(stdState) }, | ||
| code: { enumerable: true, get: () => result.code }, | ||
| signal: { enumerable: true, get: () => result.signal }, | ||
| }); | ||
| }; | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.