-
-
Notifications
You must be signed in to change notification settings - Fork 203
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
23 changed files
with
1,506 additions
and
250 deletions.
There are no files selected for viewing
This file contains 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 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 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 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 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 was deleted.
Oops, something went wrong.
This file contains 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,13 @@ | ||
import {aborted} from 'node:util'; | ||
import {createNonCommandError} from './validate.js'; | ||
|
||
export const unpipeOnAbort = (signal, ...args) => signal === undefined | ||
? [] | ||
: [unpipeOnSignalAbort(signal, ...args)]; | ||
|
||
const unpipeOnSignalAbort = async (signal, sourceStream, mergedStream, {stdioStreamsGroups, options}) => { | ||
await aborted(signal, sourceStream); | ||
await mergedStream.remove(sourceStream); | ||
const error = new Error('Pipe cancelled by `signal` option.'); | ||
throw createNonCommandError({error, stdioStreamsGroups, options}); | ||
}; |
This file contains 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,24 @@ | ||
// Like Bash, we await both processes. This is unlike some other shells which only await the destination process. | ||
// Like Bash with the `pipefail` option, if either process fails, the whole pipe fails. | ||
// Like Bash, if both process fails, we return the failure of the destination. | ||
// This ensures both processes' error is present, using `error.pipedFrom`. | ||
export const waitForBothProcesses = async (source, destination) => { | ||
const [ | ||
{status: sourceStatus, reason: sourceReason, value: sourceResult = sourceReason}, | ||
{status: destinationStatus, reason: destinationReason, value: destinationResult = destinationReason}, | ||
] = await Promise.allSettled([source, destination]); | ||
|
||
if (!destinationResult.pipedFrom.includes(sourceResult)) { | ||
destinationResult.pipedFrom.push(sourceResult); | ||
} | ||
|
||
if (destinationStatus === 'rejected') { | ||
throw destinationResult; | ||
} | ||
|
||
if (sourceStatus === 'rejected') { | ||
throw sourceResult; | ||
} | ||
|
||
return destinationResult; | ||
}; |
This file contains 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,25 @@ | ||
import {normalizePipeArguments} from './validate.js'; | ||
import {waitForBothProcesses} from './sequence.js'; | ||
import {pipeProcessStream} from './streaming.js'; | ||
import {unpipeOnAbort} from './abort.js'; | ||
|
||
// Pipe a process' `stdout`/`stderr`/`stdio` into another process' `stdin` | ||
export const pipeToProcess = (sourceInfo, destination, pipeOptions) => { | ||
const promise = handlePipePromise(sourceInfo, destination, pipeOptions); | ||
promise.pipe = pipeToProcess.bind(undefined, {...sourceInfo, spawned: destination}); | ||
return promise; | ||
}; | ||
|
||
const handlePipePromise = async (sourceInfo, destination, {from, signal} = {}) => { | ||
const {source, sourceStream, destinationStream} = normalizePipeArguments(destination, from, sourceInfo); | ||
const maxListenersController = new AbortController(); | ||
try { | ||
const mergedStream = pipeProcessStream(sourceStream, destinationStream, maxListenersController); | ||
return await Promise.race([ | ||
waitForBothProcesses(source, destination), | ||
...unpipeOnAbort(signal, sourceStream, mergedStream, sourceInfo), | ||
]); | ||
} finally { | ||
maxListenersController.abort(); | ||
} | ||
}; |
Oops, something went wrong.