-
-
Notifications
You must be signed in to change notification settings - Fork 682
feat: implement body mixin on Readable #907
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
Merged
Merged
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
ba51fe5
stream: implement body mixin on Readable
ronag 83448e5
fixup: tests
ronag 1a83834
fixup
ronag 1c06328
fixup
ronag e8eb4ca
fixuP
ronag 119e794
fixuP
ronag 463f103
fixup: remove node specific stuff
ronag 8d348e8
fixup: formData
ronag 2a995e3
fixup: simplify
ronag 9b1a397
fixup
ronag 2e2f7ff
fixup
ronag 2d4b7c0
fixup
ronag 977320e
fixup: README
ronag b19c11b
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
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,272 @@ | ||
| // Ported from https://github.com/nodejs/undici/pull/907 | ||
|
|
||
| 'use strict' | ||
|
|
||
| const assert = require('assert') | ||
| const { Readable } = require('stream') | ||
| const { RequestAbortedError, NotSupportedError } = require('../core/errors') | ||
|
|
||
| let Blob | ||
|
|
||
| const kConsume = Symbol('kConsume') | ||
| const kReading = Symbol('kReading') | ||
| const kBody = Symbol('kBody') | ||
| const kAbort = Symbol('abort') | ||
|
|
||
| module.exports = class BodyReadable extends Readable { | ||
| constructor (resume, abort) { | ||
| super({ autoDestroy: true, read: resume }) | ||
|
|
||
| this._readableState.dataEmitted = false | ||
|
|
||
| this[kAbort] = abort | ||
| this[kConsume] = null | ||
| this[kBody] = null | ||
|
|
||
| // Is stream being consumed through Readable API? | ||
| // This is an optimization so that we avoid checking | ||
| // for 'data' and 'readable' listeners in the hot path | ||
| // inside push(). | ||
| this[kReading] = false | ||
| } | ||
|
|
||
| destroy (err) { | ||
| if (this.destroyed) { | ||
| // Node < 16 | ||
| return this | ||
| } | ||
|
|
||
| if (!err && !this._readableState.endEmitted) { | ||
| err = new RequestAbortedError() | ||
| } | ||
|
|
||
| if (err) { | ||
| this[kAbort]() | ||
| } | ||
|
|
||
| return super.destroy(err) | ||
| } | ||
|
|
||
| emit (ev, ...args) { | ||
ronag marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| // Waiting for: https://github.com/nodejs/node/pull/39589 | ||
| if (ev === 'data') { | ||
| this._readableState.dataEmitted = true | ||
| } else if (ev === 'error') { | ||
| // Node < 16 | ||
| this._readableState.errorEmitted = true | ||
| } | ||
| return super.emit(ev, ...args) | ||
| } | ||
|
|
||
| on (ev, ...args) { | ||
| if (ev === 'data' || ev === 'readable') { | ||
| this[kReading] = true | ||
| } | ||
| return super.on(ev, ...args) | ||
| } | ||
|
|
||
| addListener (ev, ...args) { | ||
| return this.on(ev, ...args) | ||
| } | ||
|
|
||
| off (ev, ...args) { | ||
| const ret = super.off(ev, ...args) | ||
| if (ev === 'data' || ev === 'readable') { | ||
| this[kReading] = ( | ||
| this.listenerCount('data') > 0 || | ||
| this.listenerCount('readable') > 0 | ||
| ) | ||
| } | ||
| return ret | ||
| } | ||
|
|
||
| removeListener (ev, ...args) { | ||
| return this.off(ev, ...args) | ||
| } | ||
|
|
||
| push (chunk) { | ||
| if (this[kConsume] && chunk !== null && !this[kReading]) { | ||
| consumePush(this[kConsume], chunk) | ||
| return true | ||
| } else { | ||
| return super.push(chunk) | ||
| } | ||
| } | ||
|
|
||
| // https://fetch.spec.whatwg.org/#dom-body-text | ||
| text () { | ||
| return consume(this, 'text') | ||
| } | ||
|
|
||
| // https://fetch.spec.whatwg.org/#dom-body-json | ||
| json () { | ||
| return consume(this, 'json') | ||
| } | ||
|
|
||
| // https://fetch.spec.whatwg.org/#dom-body-blob | ||
| blob () { | ||
| return consume(this, 'blob') | ||
| } | ||
|
|
||
| // https://fetch.spec.whatwg.org/#dom-body-arraybuffer | ||
| arrayBuffer () { | ||
| return consume(this, 'arrayBuffer') | ||
| } | ||
|
|
||
| // https://fetch.spec.whatwg.org/#dom-body-formdata | ||
| formData () { | ||
| // TODO: Implement. | ||
| throw new NotSupportedError() | ||
| } | ||
|
|
||
| // https://fetch.spec.whatwg.org/#dom-body-bodyused | ||
| get bodyUsed () { | ||
| return isDisturbed(this) | ||
| } | ||
|
|
||
| // https://fetch.spec.whatwg.org/#dom-body-body | ||
| get body () { | ||
| if (!this[kBody]) { | ||
| this[kBody] = Readable.toWeb(this) | ||
| if (this[kConsume]) { | ||
| // TODO: Is this the best way to force a lock? | ||
| this[kBody].getReader() // Ensure stream is locked. | ||
| assert(this[kBody].locked) | ||
| } | ||
| } | ||
| return this[kBody] | ||
| } | ||
| } | ||
|
|
||
| // https://streams.spec.whatwg.org/#readablestream-locked | ||
| function isLocked (self) { | ||
Ethan-Arrowood marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| // Consume is an implicit lock. | ||
| return (self[kBody] && self[kBody].locked === true) || self[kConsume] | ||
| } | ||
|
|
||
| // https://streams.spec.whatwg.org/#readablestream-disturbed | ||
| function isDisturbed (self) { | ||
| // Waiting for: https://github.com/nodejs/node/pull/39589 | ||
| const { _readableState: state } = self | ||
| return !!( | ||
| state.dataEmitted || | ||
| state.endEmitted || | ||
| state.errorEmitted || | ||
| state.closeEmitted | ||
| ) | ||
| } | ||
|
|
||
| // https://fetch.spec.whatwg.org/#body-unusable | ||
| function isUnusable (self) { | ||
| return isDisturbed(self) || isLocked(self) | ||
| } | ||
|
|
||
| async function consume (stream, type) { | ||
| if (isUnusable(stream)) { | ||
| throw new TypeError('unusable') | ||
| } | ||
|
|
||
| assert(!stream[kConsume]) | ||
|
|
||
| return new Promise((resolve, reject) => { | ||
| stream[kConsume] = { | ||
| type, | ||
| stream, | ||
| resolve, | ||
| reject, | ||
| length: 0, | ||
| body: [] | ||
| } | ||
|
|
||
| stream | ||
| .on('error', function (err) { | ||
| consumeFinish(this[kConsume], err) | ||
| }) | ||
| .on('close', function () { | ||
| if (this[kConsume].body !== null) { | ||
| consumeFinish(this[kConsume], new RequestAbortedError()) | ||
| } | ||
| }) | ||
|
|
||
| process.nextTick(consumeStart, stream[kConsume]) | ||
| }) | ||
| } | ||
|
|
||
| function consumeStart (consume) { | ||
| if (consume.body === null) { | ||
| return | ||
| } | ||
|
|
||
| const { _readableState: state } = consume.stream | ||
|
|
||
| for (const chunk of state.buffer) { | ||
| consumePush(consume, chunk) | ||
| } | ||
|
|
||
| if (state.endEmitted) { | ||
| consumeEnd(this[kConsume]) | ||
| } else { | ||
| consume.stream.on('end', function () { | ||
| consumeEnd(this[kConsume]) | ||
| }) | ||
| } | ||
|
|
||
| consume.stream.resume() | ||
|
|
||
| while (consume.stream.read() != null) { | ||
| // Loop | ||
| } | ||
| } | ||
|
|
||
| function consumeEnd (consume) { | ||
| const { type, body, resolve, stream, length } = consume | ||
|
|
||
| try { | ||
| if (type === 'text') { | ||
| resolve(body.join('')) | ||
ronag marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } else if (type === 'json') { | ||
| resolve(JSON.parse(body.join(''))) | ||
| } else if (type === 'arrayBuffer') { | ||
| const dst = new Uint8Array(length) | ||
|
|
||
| let pos = 0 | ||
| for (const buf of body) { | ||
| dst.set(buf, pos) | ||
| pos += buf.byteLength | ||
| } | ||
|
|
||
| resolve(dst) | ||
| } else if (type === 'blob') { | ||
| if (!Blob) { | ||
| Blob = require('buffer').Blob | ||
| } | ||
| resolve(new Blob(body)) | ||
| } | ||
|
|
||
| consumeFinish(consume) | ||
| } catch (err) { | ||
| stream.destroy(err) | ||
| } | ||
| } | ||
|
|
||
| function consumePush (consume, chunk) { | ||
| consume.length += chunk.length | ||
| consume.body.push(chunk) | ||
| } | ||
|
|
||
| function consumeFinish (consume, err) { | ||
| if (consume.body === null) { | ||
| return | ||
| } | ||
|
|
||
| if (err) { | ||
| consume.reject(err) | ||
| } else { | ||
| consume.resolve() | ||
| } | ||
|
|
||
| consume.reject = null | ||
| consume.resolve = null | ||
| consume.decoder = null | ||
| consume.body = null | ||
| } | ||
Oops, something went wrong.
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.