Skip to content
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

fix(core): handle timeout setting for halted, resolved and sync processes #1049

Merged
merged 1 commit into from
Jan 2, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .size-limit.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
{
"name": "zx/index",
"path": "build/*.{js,cjs}",
"limit": "806 kB",
"limit": "807 kB",
"brotli": false,
"gzip": false
},
Expand Down
8 changes: 6 additions & 2 deletions docs/api.md
Original file line number Diff line number Diff line change
Expand Up @@ -75,22 +75,26 @@ interface Options {
signal: AbortSignal
input: string | Buffer | Readable | ProcessOutput | ProcessPromise
timeout: Duration
timeoutSignal: string
timeoutSignal: NodeJS.Signals
stdio: StdioOptions
verbose: boolean
sync: boolean
env: NodeJS.ProcessEnv
shell: string | boolean
shell: string | true
nothrow: boolean
prefix: string
postfix: string
quote: typeof quote
quiet: boolean
detached: boolean
preferLocal: boolean | string | string[]
spawn: typeof spawn
spawnSync: typeof spawnSync
store: TSpawnStore
log: typeof log
kill: typeof kill
killSignal: NodeJS.Signals
halt: boolean
}
```

Expand Down
16 changes: 10 additions & 6 deletions src/core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -255,8 +255,10 @@ export class ProcessPromise extends Promise<ProcessOutput> {

const self = this
const $ = this._snapshot
const sync = $[SYNC]
const timeout = self._timeout ?? $.timeout
const timeoutSignal = self._timeoutSignal ?? $.timeoutSignal

if ($.timeout) this.timeout($.timeout, $.timeoutSignal)
if ($.preferLocal) {
const dirs =
$.preferLocal === true ? [$.cwd, $[CWD]] : [$.preferLocal].flat()
Expand All @@ -265,12 +267,13 @@ export class ProcessPromise extends Promise<ProcessOutput> {

$.log({
kind: 'cmd',
cmd: this._command,
cmd: self.cmd,
verbose: self.isVerbose(),
})

// prettier-ignore
this._zurk = exec({
sync,
id: self.id,
cmd: self.fullCmd,
cwd: $.cwd ?? $[CWD],
Expand All @@ -284,13 +287,12 @@ export class ProcessPromise extends Promise<ProcessOutput> {
store: $.store,
stdin: self._stdin,
stdio: self._stdio ?? $.stdio,
sync: $[SYNC],
detached: $.detached,
ee: self._ee,
run: (cb) => cb(),
on: {
start: () => {
self._timeout && self.timeout(self._timeout, self._timeoutSignal)
!sync && timeout && self.timeout(timeout, timeoutSignal)
},
stdout: (data) => {
// If process is piped, don't print output.
Expand Down Expand Up @@ -360,7 +362,7 @@ export class ProcessPromise extends Promise<ProcessOutput> {
}})
}
private _pipe(
source: 'stdout' | 'stderr',
source: keyof TSpawnStore,
dest: PipeDest,
...args: any[]
): (Writable & PromiseLike<ProcessPromise & Writable>) | ProcessPromise {
Expand Down Expand Up @@ -513,11 +515,13 @@ export class ProcessPromise extends Promise<ProcessOutput> {
}

timeout(d: Duration, signal = $.timeoutSignal): ProcessPromise {
if (this._resolved) return this

this._timeout = parseDuration(d)
this._timeoutSignal = signal

if (this._timeoutId) clearTimeout(this._timeoutId)
if (this._timeout) {
if (this._timeout && this._run) {
this._timeoutId = setTimeout(
() => this.kill(this._timeoutSignal),
this._timeout
Expand Down
Loading