-
-
Notifications
You must be signed in to change notification settings - Fork 7
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
Decrease package size #65
Merged
Merged
Changes from all commits
Commits
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 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 |
---|---|---|
@@ -1,19 +1,15 @@ | ||
import process from 'node:process'; | ||
import {stripVTControlCharacters} from 'node:util'; | ||
|
||
export const getContext = (previous, rawFile, rawArguments) => { | ||
const start = previous.start ?? process.hrtime.bigint(); | ||
const command = [previous.command, getCommand(rawFile, rawArguments)].filter(Boolean).join(' | '); | ||
return {start, command, state: {stdout: '', stderr: '', output: ''}}; | ||
}; | ||
export const getContext = ({start, command}, raw) => ({ | ||
start: start ?? process.hrtime.bigint(), | ||
command: [ | ||
command, | ||
raw.map(part => getCommandPart(stripVTControlCharacters(part))).join(' '), | ||
].filter(Boolean).join(' | '), | ||
state: {stdout: '', stderr: '', output: ''}, | ||
}); | ||
|
||
const getCommand = (rawFile, rawArguments) => [rawFile, ...rawArguments] | ||
.map(part => getCommandPart(part)) | ||
.join(' '); | ||
|
||
const getCommandPart = part => { | ||
part = stripVTControlCharacters(part); | ||
return /[^\w./-]/.test(part) | ||
? `'${part.replaceAll('\'', '\'\\\'\'')}'` | ||
: part; | ||
}; | ||
const getCommandPart = part => /[^\w./-]/.test(part) | ||
? `'${part.replaceAll('\'', '\'\\\'\'')}'` | ||
: part; |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,19 @@ | ||
import {once, on} from 'node:events'; | ||
import process from 'node:process'; | ||
|
||
export const getResult = async (nodeChildProcess, options, context) => { | ||
export const getResult = async (nodeChildProcess, {input}, context) => { | ||
const instance = await nodeChildProcess; | ||
useInput(instance, options); | ||
if (input !== undefined) { | ||
instance.stdin.end(input); | ||
} | ||
|
||
const onClose = once(instance, 'close'); | ||
|
||
try { | ||
await Promise.race([onClose, ...onStreamErrors(instance)]); | ||
await Promise.race([ | ||
onClose, | ||
...instance.stdio.filter(Boolean).map(stream => onStreamError(stream)), | ||
]); | ||
checkFailure(context, getErrorOutput(instance)); | ||
return getOutputs(context); | ||
} catch (error) { | ||
|
@@ -16,25 +22,15 @@ export const getResult = async (nodeChildProcess, options, context) => { | |
} | ||
}; | ||
|
||
const useInput = (instance, {input}) => { | ||
if (input !== undefined) { | ||
instance.stdin.end(input); | ||
} | ||
}; | ||
|
||
const onStreamErrors = ({stdio}) => stdio.filter(Boolean).map(stream => onStreamError(stream)); | ||
|
||
const onStreamError = async stream => { | ||
for await (const [error] of on(stream, 'error')) { | ||
if (!IGNORED_CODES.has(error?.code)) { | ||
// Ignore errors that are due to closing errors when the subprocesses exit normally, or due to piping | ||
if (!['ERR_STREAM_PREMATURE_CLOSE', 'EPIPE'].includes(error?.code)) { | ||
throw error; | ||
} | ||
} | ||
}; | ||
|
||
// Ignore errors that are due to closing errors when the subprocesses exit normally, or due to piping | ||
const IGNORED_CODES = new Set(['ERR_STREAM_PREMATURE_CLOSE', 'EPIPE']); | ||
|
||
const checkFailure = ({command}, {exitCode, signalName}) => { | ||
if (signalName !== undefined) { | ||
throw new Error(`Command was terminated with ${signalName}: ${command}`); | ||
|
@@ -57,7 +53,7 @@ const getErrorInstance = (error, {command}) => error?.message.startsWith('Comman | |
|
||
const getErrorOutput = ({exitCode, signalCode}) => ({ | ||
// `exitCode` can be a negative number (`errno`) when the `error` event is emitted on the `instance` | ||
...(exitCode === null || exitCode < 1 ? {} : {exitCode}), | ||
...(exitCode < 1 ? {} : {exitCode}), | ||
...(signalCode === null ? {} : {signalName: signalCode}), | ||
}); | ||
|
||
|
@@ -69,6 +65,6 @@ const getOutputs = ({state: {stdout, stderr, output}, command, start}) => ({ | |
durationMs: Number(process.hrtime.bigint() - start) / 1e6, | ||
}); | ||
|
||
const getOutput = input => input?.at(-1) === '\n' | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. That |
||
? input.slice(0, input.at(-2) === '\r' ? -2 : -1) | ||
: input; | ||
const getOutput = output => output.at(-1) === '\n' | ||
? output.slice(0, output.at(-2) === '\r' ? -2 : -1) | ||
: output; |
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
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
null < 1
istrue
:)