-
-
Notifications
You must be signed in to change notification settings - Fork 683
feat: return body as consumable body2 #898
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
Closed
Closed
Changes from all commits
Commits
Show all changes
19 commits
Select commit
Hold shift + click to select a range
9e847cd
feat: return body as consumable body
ronag b94bafb
fixup
ronag 62aa3ad
fixup
ronag fb927f8
fixup
ronag a4838fa
fixup
ronag 61ffc29
fixup
ronag b2316f7
fixup
ronag 470dcfd
fixup
ronag ec51cb7
fixup
ronag 3d371d0
fixup
ronag 16dbf3d
fixup
ronag 02a169d
fixup
ronag f2c6797
fixup
ronag d1e640a
fixup
ronag 105686a
fixup
ronag d50386a
fixup
ronag 673720d
fixup
ronag cc1d756
fixup
ronag e0e0d84
fixup
ronag File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,136 @@ | ||
| 'use strict' | ||
|
|
||
| const { Readable } = require('stream') | ||
|
|
||
| let Blob | ||
|
|
||
| const kBody = Symbol('body') | ||
|
|
||
| const kWebStreamType = 1 | ||
| const kTextType = 2 | ||
| const kBlobType = 3 | ||
| const kArrayBufferType = 4 | ||
| const kJSONType = 5 | ||
|
|
||
| class AbortError extends Error { | ||
| constructor () { | ||
| super('The operation was aborted') | ||
| this.code = 'ABORT_ERR' | ||
| this.name = 'AbortError' | ||
| } | ||
| } | ||
|
|
||
| module.exports = class BodyReadable extends Readable { | ||
| constructor (opts) { | ||
| super(opts) | ||
|
|
||
| this[kBody] = undefined | ||
| } | ||
|
|
||
| // https://fetch.spec.whatwg.org/#dom-body-bodyused | ||
| get bodyUsed () { | ||
| return isDisturbed(this) | ||
| } | ||
|
|
||
| get body () { | ||
| if (this[kBody]?.type === kWebStreamType) { | ||
| return this[kBody].body | ||
| } | ||
|
|
||
| return consume(this, kWebStreamType) | ||
| } | ||
|
|
||
| text () { | ||
| return consume(this, kTextType) | ||
| } | ||
|
|
||
| json () { | ||
| return consume(this, kJSONType) | ||
| } | ||
|
|
||
| blob () { | ||
| return consume(this, kBlobType) | ||
| } | ||
|
|
||
| arrayBuffer () { | ||
| return consume(this, kArrayBufferType) | ||
| } | ||
| } | ||
|
|
||
| function isLocked (self) { | ||
| return self[kBody] && (self[kBody].type !== kWebStreamType || self[kBody].body.locked) | ||
| } | ||
|
|
||
| // https://streams.spec.whatwg.org/#readablestream-disturbed | ||
| function isDisturbed (self) { | ||
| return self.destroyed || self.readableDidRead | ||
| } | ||
|
|
||
| // https://fetch.spec.whatwg.org/#body-unusable | ||
| function isUnusable (self) { | ||
| return isDisturbed(self) || isLocked(self) | ||
| } | ||
|
|
||
| function consume (self, type) { | ||
| if (isUnusable(self)) { | ||
| throw new TypeError('unusable') | ||
| } | ||
|
|
||
| if (type === kWebStreamType) { | ||
| self[kBody] = { | ||
| type, | ||
| body: Readable.toWeb(self) | ||
| } | ||
|
|
||
| return self[kBody].body | ||
| } | ||
|
|
||
| return new Promise((resolve, reject) => { | ||
| self[kBody] = { | ||
| type, | ||
| resolve, | ||
| reject, | ||
| body: type === kTextType || type === kJSONType ? '' : [] | ||
| } | ||
| self | ||
| .on('error', reject) | ||
| .on('data', function (val) { | ||
| const { type } = this[kBody] | ||
|
|
||
| if (type === kTextType || type === kJSONType) { | ||
| this[kBody].body += val | ||
| } else { | ||
| this[kBody].body.push(val) | ||
| } | ||
| }) | ||
| .on('end', function () { | ||
| const { type, resolve, body } = this[kBody] | ||
|
|
||
| try { | ||
| if (type === kTextType) { | ||
| resolve(body) | ||
| } else if (type === kJSONType) { | ||
| resolve(JSON.parse(body)) | ||
| } else if (type === kArrayBufferType) { | ||
| resolve(Buffer.concat(body).buffer) | ||
| } else if (type === kBlobType) { | ||
| if (!Blob) { | ||
| Blob = require('buffer').Blob | ||
| } | ||
| resolve(new Blob(body)) | ||
| } | ||
|
|
||
| this[kBody].body = null | ||
| } catch (err) { | ||
| self.destroy(err) | ||
| } | ||
| }) | ||
| .on('close', function () { | ||
| const { body, reject } = this[kBody] | ||
|
|
||
| if (body !== null) { | ||
| reject(new AbortError()) | ||
| } | ||
| }) | ||
| }) | ||
| } | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.