Skip to content

Commit 55bfb5c

Browse files
committed
feat: introduce abort() method
closes google#527
1 parent aeec7ae commit 55bfb5c

File tree

4 files changed

+42
-8
lines changed

4 files changed

+42
-8
lines changed

package-lock.json

+7-7
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@
8383
"webpod": "^0",
8484
"which": "^3.0.0",
8585
"yaml": "^2.3.4",
86-
"zurk": "^0.0.27"
86+
"zurk": "^0.0.31"
8787
},
8888
"publishConfig": {
8989
"registry": "https://wombat-dressing-room.appspot.com"

src/core.ts

+9
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ export interface Options {
4949
[processCwd]: string
5050
cwd?: string
5151
verbose: boolean
52+
ac?: AbortController
5253
env: NodeJS.ProcessEnv
5354
shell: string | boolean
5455
nothrow: boolean
@@ -200,6 +201,7 @@ export class ProcessPromise extends Promise<ProcessOutput> {
200201
this.zurk = exec({
201202
cmd: $.prefix + this._command,
202203
cwd: $.cwd ?? $[processCwd],
204+
ac: $.ac,
203205
shell: typeof $.shell === 'string' ? $.shell : true,
204206
env: $.env,
205207
spawn: $.spawn,
@@ -354,6 +356,13 @@ export class ProcessPromise extends Promise<ProcessOutput> {
354356
}
355357
}
356358

359+
abort(reason?: string) {
360+
if (!this.child)
361+
throw new Error('Trying to abort a process without creating one.')
362+
363+
this.zurk?.ac.abort(reason)
364+
}
365+
357366
async kill(signal = 'SIGTERM'): Promise<void> {
358367
if (!this.child)
359368
throw new Error('Trying to kill a process without creating one.')

test/core.test.js

+25
Original file line numberDiff line numberDiff line change
@@ -254,6 +254,31 @@ describe('core', () => {
254254
})
255255
})
256256

257+
test('abort() method works', async () => {
258+
const p = $`sleep 9999`
259+
setTimeout(() => p.abort(), 100)
260+
261+
try {
262+
await p
263+
assert.unreachable('should have thrown')
264+
} catch ({message}) {
265+
assert.match(message, /The operation was aborted/)
266+
}
267+
})
268+
269+
test('accepts optional AbortController', async () => {
270+
const ac = new AbortController()
271+
const p = $({ ac })`sleep 9999`
272+
setTimeout(() => ac.abort(), 100)
273+
274+
try {
275+
await p
276+
assert.unreachable('should have thrown')
277+
} catch ({message}) {
278+
assert.match(message, /The operation was aborted/)
279+
}
280+
})
281+
257282
test('kill() method works', async () => {
258283
let p = $`sleep 9999`.nothrow()
259284
setTimeout(() => {

0 commit comments

Comments
 (0)