From 602363071e6b0a22515c7383ab69d0def7655f14 Mon Sep 17 00:00:00 2001 From: Anton Golub Date: Wed, 1 Jan 2025 23:54:22 +0300 Subject: [PATCH] fix: invoke `setTimeout` on process run only fix: ignore `timeout` for sync processes --- .size-limit.json | 2 +- docs/api.md | 8 ++++++-- src/core.ts | 16 ++++++++++------ 3 files changed, 17 insertions(+), 9 deletions(-) diff --git a/.size-limit.json b/.size-limit.json index 6fcc8c0557..248104fc2a 100644 --- a/.size-limit.json +++ b/.size-limit.json @@ -9,7 +9,7 @@ { "name": "zx/index", "path": "build/*.{js,cjs}", - "limit": "806 kB", + "limit": "807 kB", "brotli": false, "gzip": false }, diff --git a/docs/api.md b/docs/api.md index 23e58ef045..250261a500 100644 --- a/docs/api.md +++ b/docs/api.md @@ -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 } ``` diff --git a/src/core.ts b/src/core.ts index fad5c96008..738682e640 100644 --- a/src/core.ts +++ b/src/core.ts @@ -255,8 +255,10 @@ export class ProcessPromise extends Promise { 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() @@ -265,12 +267,13 @@ export class ProcessPromise extends Promise { $.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], @@ -284,13 +287,12 @@ export class ProcessPromise extends Promise { 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. @@ -360,7 +362,7 @@ export class ProcessPromise extends Promise { }}) } private _pipe( - source: 'stdout' | 'stderr', + source: keyof TSpawnStore, dest: PipeDest, ...args: any[] ): (Writable & PromiseLike) | ProcessPromise { @@ -513,11 +515,13 @@ export class ProcessPromise extends Promise { } 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