Skip to content
Merged
Changes from 1 commit
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
Prev Previous commit
Update docs/docs/api/Dispatcher.md
Co-authored-by: Ethan Arrowood <[email protected]>
  • Loading branch information
mcollina and Ethan-Arrowood authored Oct 25, 2024
commit 39965c7e83306fe8d6c1cc113263d716b30a037e
1 change: 1 addition & 0 deletions docs/docs/api/Dispatcher.md
Original file line number Diff line number Diff line change
Expand Up @@ -648,6 +648,7 @@ if (statusCode === 200) {
await body.dump()

return null
Comment thread
mcollina marked this conversation as resolved.
```
Comment on lines +638 to +651

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Suggested change
```js
const { body, statusCode } = await client.request({
path: '/',
method: 'GET'
})
body.on('error', console.error) // prevent process from crashing on error
if (statusCode === 200) {
const buffer = await body.arrayBuffer()
return buffer
}
for await (const _chunk of body) {
// force consumption of body to avoid memory leak
}
return null
```
```js
const { body, statusCode } = await client.request({
path: '/',
method: 'GET'
})
if (statusCode === 200) {
return await body.arrayBuffer()
}
await body.dump()
return null

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

@ronag @mcollina I think its important to keep the body.on('error', console.error) handler because any code added between the client.request() and the await body calls could throw and then it would be uncatchable.

Since this example is about conditional code, I think its best practice to keep it in there to avoid this footgun.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Furthermore, I don't think await body.dump() is sufficient.

It says it defaults to 131072 but what if the body is larger than that?

* @param {number} [opts.limit = 131072] Number of bytes to read.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Friendly ping @mcollina @ronag

@styfle styfle Oct 30, 2024 •

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

@mcollina @ronag I created PR #3788

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

handler because any code added between the client.request() and the await body calls could throw and then it would be uncatchable.

That would be incorrect code anyway...

It says it defaults to 131072 but what if the body is larger than that?

It will close the connection if the body is larger.


### `Dispatcher.stream(options, factory[, callback])`

Expand Down