-
Notifications
You must be signed in to change notification settings - Fork 40
Fix stream payload error handling #153
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
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,53 @@ | ||
| 'use strict'; | ||
|
|
||
| const Events = require('events'); | ||
| const Stream = require('stream'); | ||
|
|
||
|
|
||
| const internals = {}; | ||
|
|
||
|
|
||
| exports.encode = function (payload) { | ||
|
|
||
| if (payload instanceof Stream) { | ||
| return internals.encodeStreamPayload(payload); | ||
| } | ||
|
|
||
| const headers = Object.create(null); | ||
|
|
||
| if (payload) { | ||
| if (typeof payload !== 'string' && | ||
| !Buffer.isBuffer(payload)) { | ||
|
|
||
| payload = JSON.stringify(payload); | ||
| headers['content-type'] = 'application/json'; | ||
| } | ||
|
|
||
| // Compute the content-length for the corresponding payload in case none set | ||
|
|
||
| headers['content-length'] = (Buffer.isBuffer(payload) ? payload.length : Buffer.byteLength(payload)).toString(); | ||
| } | ||
|
|
||
| return { payload, headers }; | ||
| }; | ||
|
|
||
|
|
||
| internals.encodeStreamPayload = function (stream) { | ||
|
|
||
| const headers = Object.create(null); | ||
|
|
||
| const deferredPayload = (async () => { | ||
|
|
||
| const chunks = []; | ||
| stream.on('data', (chunk) => chunks.push(Buffer.from(chunk))); | ||
|
|
||
| await Events.once(stream, 'end'); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is it worth noting in a comment that this one also catches errors? Took me a moment to realize
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. For me it is common knowledge that |
||
|
|
||
| const payload = Buffer.concat(chunks); | ||
| headers['content-length'] = headers['content-length'] || payload.length; | ||
|
|
||
| return payload; | ||
| })(); | ||
|
|
||
| return { payload: deferredPayload, headers }; | ||
| }; | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Considering the function is already async, should we push the conversion all the way? I'm not sure why it stayed that way
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would, but without
Promise.withResolvers()(node22+) the implementation is a bit awkward.