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 2 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
21 changes: 4 additions & 17 deletions lib/api/readable.js
Original file line number Diff line number Diff line change
Expand Up @@ -147,38 +147,25 @@ module.exports = class BodyReadable extends Readable {
async dump (opts) {
let limit = opts && Number.isFinite(opts.limit) ? opts.limit : 262144
const signal = opts && opts.signal
function throwIfAborted () {
if (!signal) { return }
if (signal.throwIfAborted) {
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
}
}
}
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')
}
throwIfAborted()
util.throwIfAborted(signal)
signal.addEventListener('abort', () => {
this.destroy()
})
}, { once: true })
Copy link
Member

@ronag ronag Mar 9, 2023

Choose a reason for hiding this comment

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

this will still leak if we completed without abort

Copy link
Member

Choose a reason for hiding this comment

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

add a finally clause to remove it

Copy link
Member Author

Choose a reason for hiding this comment

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

Ah sorry, completely distracted need more chai sigh, anyway have updated

}
try {
for await (const chunk of this) {
debadree25 marked this conversation as resolved.
Show resolved Hide resolved
throwIfAborted()
util.throwIfAborted(signal)
limit -= Buffer.byteLength(chunk)
if (limit < 0) {
return
}
}
} catch {
throwIfAborted()
util.throwIfAborted(signal)
}
}
}
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 (signal.throwIfAborted) {
debadree25 marked this conversation as resolved.
Show resolved Hide resolved
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