Skip to content

Commit

Permalink
fix: invoke setTimeout on process run only (#1049)
Browse files Browse the repository at this point in the history
  • Loading branch information
antongolub authored Jan 2, 2025
1 parent 343bda0 commit dfe43fc
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 9 deletions.
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

0 comments on commit dfe43fc

Please sign in to comment.