From 05fb45b2289045899b8e762e0f16ff9dd6bbd767 Mon Sep 17 00:00:00 2001 From: Ewan Harris Date: Tue, 8 Jun 2021 17:10:53 +0100 Subject: [PATCH] fix: check for existence of unref before calling 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: https://github.com/electron/electron/issues/21162 PR-URL: https://github.com/npm/minipass-fetch/pull/13 Credit: @ewanharris Close: #13 Reviewed-by: @isaacs --- lib/body.js | 2 +- test/body.js | 14 ++++++++++++++ 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/lib/body.js b/lib/body.js index fb7ffc3..ea6e6fb 100644 --- a/lib/body.js +++ b/lib/body.js @@ -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() } diff --git a/test/body.js b/test/body.js index 291c4e2..6c6c71c 100644 --- a/test/body.js +++ b/test/body.js @@ -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' }),