123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385 |
- // Ported from https://github.com/nodejs/undici/pull/907
- 'use strict'
- const assert = require('node:assert')
- const { Readable } = require('node:stream')
- const { RequestAbortedError, NotSupportedError, InvalidArgumentError, AbortError } = require('../core/errors')
- const util = require('../core/util')
- const { ReadableStreamFrom } = require('../core/util')
- const kConsume = Symbol('kConsume')
- const kReading = Symbol('kReading')
- const kBody = Symbol('kBody')
- const kAbort = Symbol('kAbort')
- const kContentType = Symbol('kContentType')
- const kContentLength = Symbol('kContentLength')
- const noop = () => {}
- class BodyReadable extends Readable {
- constructor ({
- resume,
- abort,
- contentType = '',
- contentLength,
- highWaterMark = 64 * 1024 // Same as nodejs fs streams.
- }) {
- super({
- autoDestroy: true,
- read: resume,
- highWaterMark
- })
- this._readableState.dataEmitted = false
- this[kAbort] = abort
- this[kConsume] = null
- this[kBody] = null
- this[kContentType] = contentType
- this[kContentLength] = contentLength
- // 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 (!err && !this._readableState.endEmitted) {
- err = new RequestAbortedError()
- }
- if (err) {
- this[kAbort]()
- }
- return super.destroy(err)
- }
- _destroy (err, callback) {
- // Workaround for Node "bug". If the stream is destroyed in same
- // tick as it is created, then a user who is waiting for a
- // promise (i.e micro tick) for installing a 'error' listener will
- // never get a chance and will always encounter an unhandled exception.
- if (!this[kReading]) {
- setImmediate(() => {
- callback(err)
- })
- } else {
- callback(err)
- }
- }
- 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) {
- consumePush(this[kConsume], chunk)
- return this[kReading] ? super.push(chunk) : true
- }
- return super.push(chunk)
- }
- // https://fetch.spec.whatwg.org/#dom-body-text
- async text () {
- return consume(this, 'text')
- }
- // https://fetch.spec.whatwg.org/#dom-body-json
- async json () {
- return consume(this, 'json')
- }
- // https://fetch.spec.whatwg.org/#dom-body-blob
- async blob () {
- return consume(this, 'blob')
- }
- // https://fetch.spec.whatwg.org/#dom-body-bytes
- async bytes () {
- return consume(this, 'bytes')
- }
- // https://fetch.spec.whatwg.org/#dom-body-arraybuffer
- async arrayBuffer () {
- return consume(this, 'arrayBuffer')
- }
- // https://fetch.spec.whatwg.org/#dom-body-formdata
- async formData () {
- // TODO: Implement.
- throw new NotSupportedError()
- }
- // https://fetch.spec.whatwg.org/#dom-body-bodyused
- get bodyUsed () {
- return util.isDisturbed(this)
- }
- // https://fetch.spec.whatwg.org/#dom-body-body
- get body () {
- if (!this[kBody]) {
- this[kBody] = ReadableStreamFrom(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]
- }
- async dump (opts) {
- let limit = Number.isFinite(opts?.limit) ? opts.limit : 128 * 1024
- const signal = opts?.signal
- if (signal != null && (typeof signal !== 'object' || !('aborted' in signal))) {
- throw new InvalidArgumentError('signal must be an AbortSignal')
- }
- signal?.throwIfAborted()
- if (this._readableState.closeEmitted) {
- return null
- }
- return await new Promise((resolve, reject) => {
- if (this[kContentLength] > limit) {
- this.destroy(new AbortError())
- }
- const onAbort = () => {
- this.destroy(signal.reason ?? new AbortError())
- }
- signal?.addEventListener('abort', onAbort)
- this
- .on('close', function () {
- signal?.removeEventListener('abort', onAbort)
- if (signal?.aborted) {
- reject(signal.reason ?? new AbortError())
- } else {
- resolve(null)
- }
- })
- .on('error', noop)
- .on('data', function (chunk) {
- limit -= chunk.length
- if (limit <= 0) {
- this.destroy()
- }
- })
- .resume()
- })
- }
- }
- // https://streams.spec.whatwg.org/#readablestream-locked
- function isLocked (self) {
- // Consume is an implicit lock.
- return (self[kBody] && self[kBody].locked === true) || self[kConsume]
- }
- // https://fetch.spec.whatwg.org/#body-unusable
- function isUnusable (self) {
- return util.isDisturbed(self) || isLocked(self)
- }
- async function consume (stream, type) {
- assert(!stream[kConsume])
- return new Promise((resolve, reject) => {
- if (isUnusable(stream)) {
- const rState = stream._readableState
- if (rState.destroyed && rState.closeEmitted === false) {
- stream
- .on('error', err => {
- reject(err)
- })
- .on('close', () => {
- reject(new TypeError('unusable'))
- })
- } else {
- reject(rState.errored ?? new TypeError('unusable'))
- }
- } else {
- queueMicrotask(() => {
- 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())
- }
- })
- consumeStart(stream[kConsume])
- })
- }
- })
- }
- function consumeStart (consume) {
- if (consume.body === null) {
- return
- }
- const { _readableState: state } = consume.stream
- if (state.bufferIndex) {
- const start = state.bufferIndex
- const end = state.buffer.length
- for (let n = start; n < end; n++) {
- consumePush(consume, state.buffer[n])
- }
- } else {
- 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
- }
- }
- /**
- * @param {Buffer[]} chunks
- * @param {number} length
- */
- function chunksDecode (chunks, length) {
- if (chunks.length === 0 || length === 0) {
- return ''
- }
- const buffer = chunks.length === 1 ? chunks[0] : Buffer.concat(chunks, length)
- const bufferLength = buffer.length
- // Skip BOM.
- const start =
- bufferLength > 2 &&
- buffer[0] === 0xef &&
- buffer[1] === 0xbb &&
- buffer[2] === 0xbf
- ? 3
- : 0
- return buffer.utf8Slice(start, bufferLength)
- }
- /**
- * @param {Buffer[]} chunks
- * @param {number} length
- * @returns {Uint8Array}
- */
- function chunksConcat (chunks, length) {
- if (chunks.length === 0 || length === 0) {
- return new Uint8Array(0)
- }
- if (chunks.length === 1) {
- // fast-path
- return new Uint8Array(chunks[0])
- }
- const buffer = new Uint8Array(Buffer.allocUnsafeSlow(length).buffer)
- let offset = 0
- for (let i = 0; i < chunks.length; ++i) {
- const chunk = chunks[i]
- buffer.set(chunk, offset)
- offset += chunk.length
- }
- return buffer
- }
- function consumeEnd (consume) {
- const { type, body, resolve, stream, length } = consume
- try {
- if (type === 'text') {
- resolve(chunksDecode(body, length))
- } else if (type === 'json') {
- resolve(JSON.parse(chunksDecode(body, length)))
- } else if (type === 'arrayBuffer') {
- resolve(chunksConcat(body, length).buffer)
- } else if (type === 'blob') {
- resolve(new Blob(body, { type: stream[kContentType] }))
- } else if (type === 'bytes') {
- resolve(chunksConcat(body, length))
- }
- 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.type = null
- consume.stream = null
- consume.resolve = null
- consume.reject = null
- consume.length = 0
- consume.body = null
- }
- module.exports = { Readable: BodyReadable, chunksDecode }
|