|
| 1 | +'use strict'; |
| 2 | + |
| 3 | +const { Buffer, Blob } = require('buffer'); |
| 4 | +const Duplex = require('internal/streams/duplex'); |
| 5 | +const Readable = require('internal/streams/readable'); |
| 6 | +const Writable = require('internal/streams/writable'); |
| 7 | +const duplexify = require('internal/streams/duplexify'); |
| 8 | +const { createDeferredPromise } = require('internal/util'); |
| 9 | +const { destroyer } = require('internal/streams/destroy'); |
| 10 | +const from = require('internal/streams/from'); |
| 11 | +const assert = require('internal/assert'); |
| 12 | + |
| 13 | +const { |
| 14 | + isBlob |
| 15 | +} = require('internal/blob'); |
| 16 | + |
| 17 | +const { |
| 18 | + isBrandCheck, |
| 19 | +} = require('internal/webstreams/util'); |
| 20 | + |
| 21 | +const isReadableStream = |
| 22 | + isBrandCheck('ReadableStream'); |
| 23 | +const isWritableStream = |
| 24 | + isBrandCheck('WritableStream'); |
| 25 | + |
| 26 | +const { |
| 27 | + isIterable, |
| 28 | + isDuplexNodeStream, |
| 29 | + isReadableNodeStream, |
| 30 | + isWritableNodeStream, |
| 31 | +} = require('internal/streams/utils'); |
| 32 | + |
| 33 | +const { |
| 34 | + JSONParse, |
| 35 | + PromiseResolve, |
| 36 | + Symbol, |
| 37 | + SymbolAsyncIterator |
| 38 | +} = primordials; |
| 39 | + |
| 40 | +const { |
| 41 | + codes: { |
| 42 | + ERR_INVALID_ARG_TYPE, |
| 43 | + ERR_INVALID_RETURN_VALUE, |
| 44 | + ERR_INVALID_STATE, |
| 45 | + }, |
| 46 | +} = require('internal/errors'); |
| 47 | + |
| 48 | +const kState = Symbol('kState'); |
| 49 | + |
| 50 | +// This is needed for pre node 17. |
| 51 | +class BodyDuplex extends Duplex { |
| 52 | + constructor(options) { |
| 53 | + super(options); |
| 54 | + |
| 55 | + // https://github.com/nodejs/node/pull/34385 |
| 56 | + |
| 57 | + if (options?.readable === false) { |
| 58 | + this._readableState.readable = false; |
| 59 | + this._readableState.ended = true; |
| 60 | + this._readableState.endEmitted = true; |
| 61 | + } |
| 62 | + |
| 63 | + if (options?.writable === false) { |
| 64 | + this._writableState.writable = false; |
| 65 | + this._writableState.ending = true; |
| 66 | + this._writableState.ended = true; |
| 67 | + this._writableState.finished = true; |
| 68 | + } |
| 69 | + } |
| 70 | +} |
| 71 | + |
| 72 | +class Body { |
| 73 | + constructor(body, options) { |
| 74 | + // TODO (ronag): What about TransformStream? |
| 75 | + |
| 76 | + if (body[kState]) { |
| 77 | + this[kState] = body[kState]; |
| 78 | + } else if ( |
| 79 | + isReadableStream(body?.readable) && |
| 80 | + isWritableStream(body?.writable) |
| 81 | + ) { |
| 82 | + // TODO (ronag): Optimize. Delay conversion. |
| 83 | + const d = Duplex.fromWeb(body, options); |
| 84 | + this[kState] = { readable: d, writable: d }; |
| 85 | + } else if (isWritableStream(body?.writable)) { |
| 86 | + // TODO (ronag): Optimize. Delay conversion. |
| 87 | + this[kState] = { |
| 88 | + readable: undefined, |
| 89 | + writable: Writable.fromWeb(body, options) |
| 90 | + }; |
| 91 | + } else if (isReadableStream(body?.readable)) { |
| 92 | + // TODO (ronag): Optimize. Delay conversion. |
| 93 | + this[kState] = { |
| 94 | + readable: Readable.fromWeb(body, options), |
| 95 | + writable: undefined |
| 96 | + }; |
| 97 | + } else if (isDuplexNodeStream(body)) { |
| 98 | + this[kState] = { readable: body, writable: body }; |
| 99 | + } else if (isReadableNodeStream(body)) { |
| 100 | + this[kState] = { readable: body, writable: undefined }; |
| 101 | + } else if (isWritableNodeStream(body)) { |
| 102 | + this[kState] = { readable: undefined, writable: body }; |
| 103 | + } else if (isReadableStream(body)) { |
| 104 | + // TODO (ronag): Optimize. Delay conversion. |
| 105 | + this[kState] = { |
| 106 | + readable: Readable.fromWeb(body, options), |
| 107 | + writable: undefined |
| 108 | + }; |
| 109 | + } else if (isWritableStream(body)) { |
| 110 | + // TODO (ronag): Optimize. Delay conversion. |
| 111 | + this[kState] = { |
| 112 | + readable: undefined, |
| 113 | + writable: Writable.fromWeb(body, options) |
| 114 | + }; |
| 115 | + } else if (typeof body === 'function') { |
| 116 | + // TODO (ronag): Optimize. Delay conversion. |
| 117 | + assert(body.length > 0); |
| 118 | + |
| 119 | + const { value, write, final } = fromAsyncGen(body); |
| 120 | + |
| 121 | + if (isIterable(value)) { |
| 122 | + const d = from(BodyDuplex, value, { |
| 123 | + objectMode: true, |
| 124 | + highWaterMark: 1, |
| 125 | + ...options, |
| 126 | + write, |
| 127 | + final |
| 128 | + }); |
| 129 | + |
| 130 | + this[kState] = { readable: d, writable: d }; |
| 131 | + } else if (typeof value?.then === 'function') { |
| 132 | + let d; |
| 133 | + |
| 134 | + const promise = PromiseResolve(value) |
| 135 | + .then((val) => { |
| 136 | + if (val != null) { |
| 137 | + throw new ERR_INVALID_RETURN_VALUE('nully', 'body', val); |
| 138 | + } |
| 139 | + }) |
| 140 | + .catch((err) => { |
| 141 | + destroyer(d, err); |
| 142 | + }); |
| 143 | + |
| 144 | + d = new BodyDuplex({ |
| 145 | + objectMode: true, |
| 146 | + highWaterMark: 1, |
| 147 | + ...options, |
| 148 | + readable: false, |
| 149 | + write, |
| 150 | + final(cb) { |
| 151 | + final(() => promise.then(cb, cb)); |
| 152 | + } |
| 153 | + }); |
| 154 | + |
| 155 | + this[kState] = { readable: d, writable: d }; |
| 156 | + } else { |
| 157 | + throw new ERR_INVALID_RETURN_VALUE( |
| 158 | + 'Iterable, AsyncIterable or AsyncFunction', 'body', value); |
| 159 | + } |
| 160 | + } else if (isBlob(body)) { |
| 161 | + // TODO (ronag): Optimize. Delay conversion. |
| 162 | + const d = new Body(async function* () { |
| 163 | + yield await body.arrayBuffer(); |
| 164 | + }()).nodeStream(); |
| 165 | + |
| 166 | + this[kState] = { readable: d, writable: d }; |
| 167 | + } else if (isIterable(body)) { |
| 168 | + // TODO (ronag): Optimize. Delay conversion. |
| 169 | + const d = from(BodyDuplex, body, { |
| 170 | + objectMode: true, |
| 171 | + highWaterMark: 1, |
| 172 | + ...options, |
| 173 | + writable: false |
| 174 | + }); |
| 175 | + |
| 176 | + this[kState] = { readable: d, writable: d }; |
| 177 | + } else if ( |
| 178 | + typeof body?.writable === 'object' || |
| 179 | + typeof body?.readable === 'object' |
| 180 | + ) { |
| 181 | + // TODO (ronag): Optimize. Delay conversion. |
| 182 | + const readable = body?.readable ? |
| 183 | + isReadableNodeStream(body?.readable) ? body?.readable : |
| 184 | + new Body(body.readable).readableNodeStream() : undefined; |
| 185 | + const writable = body?.writable ? |
| 186 | + isWritableNodeStream(body?.writable) ? body?.writable : |
| 187 | + new Body(body.writable).writableNodeStream() : undefined; |
| 188 | + |
| 189 | + this[kState] = { readable, writable }; |
| 190 | + } else { |
| 191 | + throw new ERR_INVALID_ARG_TYPE( |
| 192 | + 'stream', |
| 193 | + ['Blob', 'ReadableStream', 'WritableStream', 'Stream', 'Iterable', |
| 194 | + 'AsyncIterable', 'Function', '{ readable, writable } pair'], |
| 195 | + body) |
| 196 | + ; |
| 197 | + } |
| 198 | + } |
| 199 | + |
| 200 | + get writable() { |
| 201 | + return !!this[kState].writable; |
| 202 | + } |
| 203 | + |
| 204 | + get readable() { |
| 205 | + return !!this[kState].readable; |
| 206 | + } |
| 207 | + |
| 208 | + readableNodeStream() { |
| 209 | + const { readable } = this[kState]; |
| 210 | + |
| 211 | + if (readable === null) { |
| 212 | + throw new ERR_INVALID_STATE('read lock'); |
| 213 | + } |
| 214 | + |
| 215 | + this[kState].readable = null; |
| 216 | + |
| 217 | + // TODO (ronag): Hide Writable interface. |
| 218 | + return readable ?? new BodyDuplex({ readable: false, writable: false }); |
| 219 | + } |
| 220 | + |
| 221 | + writableNodeStream() { |
| 222 | + const { writable } = this[kState]; |
| 223 | + |
| 224 | + if (writable === null) { |
| 225 | + throw new ERR_INVALID_STATE('write lock'); |
| 226 | + } |
| 227 | + |
| 228 | + this[kState].writable = null; |
| 229 | + |
| 230 | + // TODO (ronag): Hide Readable interface. |
| 231 | + return writable ?? new BodyDuplex({ readable: false, writable: false }); |
| 232 | + } |
| 233 | + |
| 234 | + nodeStream() { |
| 235 | + if (this.readable === null) { |
| 236 | + throw new ERR_INVALID_STATE('read lock'); |
| 237 | + } |
| 238 | + |
| 239 | + if (this.writable === null) { |
| 240 | + throw new ERR_INVALID_STATE('write lock'); |
| 241 | + } |
| 242 | + |
| 243 | + if (this[kState].readable === this[kState].writable) { |
| 244 | + const d = this[kState].readable; |
| 245 | + this[kState].readable = null; |
| 246 | + this[kState].writable = null; |
| 247 | + return d; |
| 248 | + } |
| 249 | + |
| 250 | + return duplexify({ |
| 251 | + readable: this.readableNodeStream(), |
| 252 | + writable: this.writableNodeStream(), |
| 253 | + }); |
| 254 | + } |
| 255 | + |
| 256 | + readableWebStream() { |
| 257 | + return this.readableWebStream().asWeb(); |
| 258 | + } |
| 259 | + |
| 260 | + writableWebStream() { |
| 261 | + return this.writableNodeStream().asWeb(); |
| 262 | + } |
| 263 | + |
| 264 | + [SymbolAsyncIterator]() { |
| 265 | + return this.readableNodeStream()[SymbolAsyncIterator](); |
| 266 | + } |
| 267 | + |
| 268 | + async blob() { |
| 269 | + const sources = []; |
| 270 | + for await (const chunk of this.readableNodeStream()) { |
| 271 | + sources.push(chunk); |
| 272 | + } |
| 273 | + return new Blob(sources); |
| 274 | + } |
| 275 | + |
| 276 | + async buffer() { |
| 277 | + const sources = []; |
| 278 | + for await (const chunk of this.readableNodeStream()) { |
| 279 | + sources.push(chunk); |
| 280 | + } |
| 281 | + return Buffer.concat(sources); |
| 282 | + } |
| 283 | + |
| 284 | + async arrayBuffer() { |
| 285 | + const blob = await this.blob(); |
| 286 | + return blob.arrayBuffer(); |
| 287 | + } |
| 288 | + |
| 289 | + async text() { |
| 290 | + let ret = ''; |
| 291 | + for await (const chunk of this.readableNodeStream()) { |
| 292 | + ret += chunk; |
| 293 | + } |
| 294 | + return ret; |
| 295 | + } |
| 296 | + |
| 297 | + async json() { |
| 298 | + return JSONParse(await this.text()); |
| 299 | + } |
| 300 | +} |
| 301 | + |
| 302 | +function fromAsyncGen(fn) { |
| 303 | + let { promise, resolve } = createDeferredPromise(); |
| 304 | + const value = fn(async function*() { |
| 305 | + while (true) { |
| 306 | + const { chunk, done, cb } = await promise; |
| 307 | + process.nextTick(cb); |
| 308 | + if (done) return; |
| 309 | + yield chunk; |
| 310 | + ({ promise, resolve } = createDeferredPromise()); |
| 311 | + } |
| 312 | + }()); |
| 313 | + |
| 314 | + return { |
| 315 | + value, |
| 316 | + write(chunk, encoding, cb) { |
| 317 | + resolve({ chunk, done: false, cb }); |
| 318 | + }, |
| 319 | + final(cb) { |
| 320 | + resolve({ done: true, cb }); |
| 321 | + } |
| 322 | + }; |
| 323 | +} |
| 324 | + |
| 325 | +module.exports = Body; |
0 commit comments