Skip to content
New issue

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

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

Already on GitHub? # to your account

chore(docs): add request() example for conditionally reading the body #3743

Merged
merged 3 commits into from
Oct 26, 2024

Conversation

styfle
Copy link
Member

@styfle styfle commented Oct 16, 2024

This relates to...

Documentation for dispatcher.request()

Rationale

It is very easy to cause a memory leak or even crash the process if you forget to handle the response body.

Changes

A new example showing how to conditionally reading the body

Features

N/A

Bug Fixes

N/A

Breaking Changes and Deprecations

N/A

Status

Comment on lines 638 to 655
```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
```
Copy link
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
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
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
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

Copy link
Member Author

@styfle styfle Oct 30, 2024

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
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.

Copy link
Member

@mcollina mcollina left a comment

Choose a reason for hiding this comment

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

lgtm with @ronag fix

Co-authored-by: Robert Nagy <ronagy@icloud.com>
Copy link
Collaborator

@Ethan-Arrowood Ethan-Arrowood left a comment

Choose a reason for hiding this comment

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

lgtm otherwise

docs/docs/api/Dispatcher.md Show resolved Hide resolved
Co-authored-by: Ethan Arrowood <ethan@arrowood.dev>
@mcollina
Copy link
Member

@Ethan-Arrowood PTAL

@Ethan-Arrowood
Copy link
Collaborator

What about @styfle 's comment / question here: #3743 (comment)

Is dump sufficient if the body is larger than its limit?

Copy link
Collaborator

@Ethan-Arrowood Ethan-Arrowood left a comment

Choose a reason for hiding this comment

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

lgtm - but there is still the outstanding question about .dump() limits

@mcollina mcollina merged commit 2e79b62 into nodejs:main Oct 26, 2024
30 of 32 checks passed
@styfle styfle deleted the patch-1 branch October 30, 2024 20:35
@github-actions github-actions bot mentioned this pull request Dec 3, 2024
This was referenced Dec 16, 2024
# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants