Skip to content

Commit

Permalink
fix: check for existence of unref before calling
Browse files Browse the repository at this point in the history
Environments like Electron do not always expose the Node.js setTiemout,
so there is no unref function on resTimeout. So update the check to also
check if the unref function exists

Ref: electron/electron#21162

PR-URL: #13
Credit: @ewanharris
Close: #13
Reviewed-by: @isaacs
  • Loading branch information
ewanharris authored and isaacs committed Nov 8, 2021
1 parent f338c40 commit 05fb45b
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 1 deletion.
2 changes: 1 addition & 1 deletion lib/body.js
Expand Up @@ -132,7 +132,7 @@ class Body {

// do not keep the process open just for this timeout, even
// though we expect it'll get cleared eventually.
if (resTimeout) {
if (resTimeout && resTimeout.unref) {
resTimeout.unref()
}

Expand Down
14 changes: 14 additions & 0 deletions test/body.js
Expand Up @@ -231,6 +231,20 @@ t.test('json', async t => {
})
})

t.test('handles environments where setTimeout does not have unref', async t => {
const originalSetTimeout = setTimeout
// simulate environments without unref()
global.setTimeout = (func, time) =>
Object.assign(originalSetTimeout(func, time), { unref: null })
t.teardown(() => global.setTimeout = originalSetTimeout)

t.doesNotThrow(async () => {
const b = new Body(new Blob('a=1'), { timeout: 100 })
await b.text()
t.end()
})
})

t.test('write to streams', async t => {
const w = body => Body.writeToStream(
new Minipass({ encoding: 'utf8' }),
Expand Down

0 comments on commit 05fb45b

Please sign in to comment.