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' }),