Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add abort signal to body.dump() #1993

Merged
merged 24 commits into from
Mar 10, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 18 additions & 2 deletions lib/api/readable.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

const assert = require('assert')
const { Readable } = require('stream')
const { RequestAbortedError, NotSupportedError } = require('../core/errors')
const { RequestAbortedError, NotSupportedError, InvalidArgumentError } = require('../core/errors')
const util = require('../core/util')
const { ReadableStreamFrom, toUSVString } = require('../core/util')

Expand Down Expand Up @@ -146,15 +146,31 @@ module.exports = class BodyReadable extends Readable {

async dump (opts) {
let limit = opts && Number.isFinite(opts.limit) ? opts.limit : 262144
const signal = opts && opts.signal
const abortFn = () => {
this.destroy()
}
if (signal) {
debadree25 marked this conversation as resolved.
Show resolved Hide resolved
if (typeof signal !== 'object' || !('aborted' in signal)) {
throw new InvalidArgumentError('signal must be an AbortSignal')
}
util.throwIfAborted(signal)
signal.addEventListener('abort', abortFn, { once: true })
}
try {
for await (const chunk of this) {
debadree25 marked this conversation as resolved.
Show resolved Hide resolved
util.throwIfAborted(signal)
limit -= Buffer.byteLength(chunk)
if (limit < 0) {
return
}
}
} catch {
// Do nothing...
util.throwIfAborted(signal)
} finally {
if (signal) {
signal.removeEventListener('abort', abortFn)
}
}
}
}
Expand Down
15 changes: 15 additions & 0 deletions lib/core/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -389,6 +389,20 @@ function isFormDataLike (object) {
)
}

function throwIfAborted (signal) {
if (!signal) { return }
if (typeof signal.throwIfAborted === 'function') {
signal.throwIfAborted()
} else {
if (signal.aborted) {
// DOMException not available < v17.0.0
const err = new Error('The operation was aborted')
err.name = 'AbortError'
throw err
}
}
}

const kEnumerableProperty = Object.create(null)
kEnumerableProperty.enumerable = true

Expand Down Expand Up @@ -420,6 +434,7 @@ module.exports = {
getSocketInfo,
isFormDataLike,
buildURL,
throwIfAborted,
nodeMajor,
nodeMinor,
nodeHasAutoSelectFamily: nodeMajor > 18 || (nodeMajor === 18 && nodeMinor >= 13)
Expand Down
34 changes: 34 additions & 0 deletions test/client-request.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
/* globals AbortController */
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does this not somehow inject AbortController for tests on older versions, i see this pattern being used in tests for eg: test/fetch/client-fetch.js

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

no, it's just telling prettier that AbortController is a global

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Any ideas then @KhafraDev what could we do about this test?


'use strict'

const { test } = require('tap')
Expand Down Expand Up @@ -41,6 +43,38 @@ test('request dump', (t) => {
})
})

test('request dump with abort signal', (t) => {
t.plan(2)
const server = createServer((req, res) => {
res.write('hello')
})
t.teardown(server.close.bind(server))

server.listen(0, () => {
const client = new Client(`http://localhost:${server.address().port}`)
t.teardown(client.destroy.bind(client))

client.request({
path: '/',
method: 'GET'
}, (err, { body }) => {
t.error(err)
let ac
if (!global.AbortController) {
const { AbortController } = require('abort-controller')
ac = new AbortController()
} else {
ac = new AbortController()
}
body.dump({ signal: ac.signal }).catch((err) => {
t.equal(err.name, 'AbortError')
server.close()
})
ac.abort()
})
})
})

test('request abort before headers', (t) => {
t.plan(6)

Expand Down