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 5 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
12 changes: 10 additions & 2 deletions lib/api/readable.js
Original file line number Diff line number Diff line change
Expand Up @@ -146,15 +146,23 @@ module.exports = class BodyReadable extends Readable {

async dump (opts) {
let limit = opts && Number.isFinite(opts.limit) ? opts.limit : 262144
const signal = opts && opts.signal && opts.signal instanceof AbortSignal ? opts.signal : null
debadree25 marked this conversation as resolved.
Show resolved Hide resolved
if (signal) {
debadree25 marked this conversation as resolved.
Show resolved Hide resolved
signal.addEventListener('abort', () => {
this.destroy()
})
Copy link
Member

Choose a reason for hiding this comment

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

This event listener needs to be cleaned up or we have a memory leak.

Copy link
Member Author

Choose a reason for hiding this comment

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

Made this { once: true }

}
try {
for await (const chunk of this) {
debadree25 marked this conversation as resolved.
Show resolved Hide resolved
limit -= Buffer.byteLength(chunk)
if (limit < 0) {
return
}
}
} catch {
// Do nothing...
} catch (err) {
debadree25 marked this conversation as resolved.
Show resolved Hide resolved
debadree25 marked this conversation as resolved.
Show resolved Hide resolved
if (err.name === 'AbortError') {
throw err
}
debadree25 marked this conversation as resolved.
Show resolved Hide resolved
}
}
}
Expand Down
28 changes: 28 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,32 @@ 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)
const 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