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 19 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
30 changes: 28 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,41 @@ module.exports = class BodyReadable extends Readable {

async dump (opts) {
let limit = opts && Number.isFinite(opts.limit) ? opts.limit : 262144
const signal = opts && opts.signal
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')
}
if (!signal.throwIfAborted) {
// add in a throwIfAborted method to the signal
signal.throwIfAborted = function () {
Copy link
Member

@KhafraDev KhafraDev Mar 8, 2023

Choose a reason for hiding this comment

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

I don't think adding a public method here is a good solution, it'd be better to add it as a utility function

function throwIfAborted (signal) {
  if (signal.throwIfAborted) {
    signal.throwIfAborted()
  } else {
    // ...
  }
}

Copy link
Member Author

Choose a reason for hiding this comment

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

Updating

Copy link
Member Author

Choose a reason for hiding this comment

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

Updated, does it look ok?

if (signal.aborted) {
// DOMException not available < v17.0.0
const err = new Error('The operation was aborted')
err.name = 'AbortError'
throw err
}
}
}
signal.throwIfAborted()
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
if (signal) {
signal.throwIfAborted()
}
limit -= Buffer.byteLength(chunk)
if (limit < 0) {
return
}
}
} catch {
// Do nothing...
if (signal) {
signal.throwIfAborted()
}
}
}
}
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