Spaces:
Sleeping
Sleeping
File size: 13,980 Bytes
c2b7eb3 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 | const { Readable, Writable, Transform, getStreamError, isStreamx, isDisturbed } = require('streamx')
const tee = require('teex')
const readableKind = Symbol.for('bare.stream.readable.kind')
const writableKind = Symbol.for('bare.stream.writable.kind')
const transformKind = Symbol.for('bare.stream.transform.kind')
// https://streams.spec.whatwg.org/#readablestreamdefaultreader
exports.ReadableStreamDefaultReader = class ReadableStreamDefaultReader {
constructor(stream) {
this._stream = stream
this._stream._stream.once('close', onclose).once('error', onerror)
const closed = Promise.withResolvers()
// Avoid unhandled exceptions
closed.promise.catch(noop)
this._closed = closed
function onclose() {
closed.resolve()
}
function onerror(err) {
closed.reject(err)
}
}
get closed() {
return this._closed.promise
}
read() {
const stream = this._stream._stream
return new Promise((resolve, reject) => {
const err = getStreamError(stream)
if (err) return reject(err)
if (stream.destroyed) {
return resolve({ value: undefined, done: true })
}
const value = stream.read()
if (value !== null) {
return resolve({ value, done: false })
}
stream.once('readable', onreadable).once('close', onclose).once('error', onerror)
function onreadable() {
const value = stream.read()
ondone(null, value === null ? { value: undefined, done: true } : { value, done: false })
}
function onclose() {
ondone(null, { value: undefined, done: true })
}
function onerror(err) {
ondone(err, null)
}
function ondone(err, value) {
stream.off('readable', onreadable).off('close', onclose).off('error', onerror)
if (err) reject(err)
else resolve(value)
}
})
}
releaseLock() {
this._closed.reject(new TypeError('Reader was released'))
this._stream._releaseLock()
this._stream = null
}
cancel(reason = new TypeError('Stream was cancelled')) {
const stream = this._stream._stream
if (stream.destroyed) return Promise.resolve()
return new Promise((resolve) =>
stream.once('close', resolve).once('error', noop).destroy(reason)
)
}
}
// https://streams.spec.whatwg.org/#readablestreamdefaultcontroller
exports.ReadableStreamDefaultController = class ReadableStreamDefaultController {
constructor(stream) {
this._stream = stream
}
get desiredSize() {
const stream = this._stream._stream
return stream._readableState.highWaterMark - stream._readableState.buffered
}
enqueue(data) {
this._stream._stream.push(data)
}
close() {
this._stream._stream.push(null)
}
error(err) {
this._stream._stream.destroy(err)
}
}
// https://streams.spec.whatwg.org/#readablestream
class ReadableStream {
static get [readableKind]() {
return 0 // Compatibility version
}
static from(iterable) {
return new ReadableStream(Readable.from(iterable))
}
constructor(underlyingSource = {}, queuingStrategy) {
if (isStreamx(underlyingSource)) {
this._stream = underlyingSource
} else {
if (queuingStrategy === undefined) {
queuingStrategy = new exports.CountQueuingStrategy()
}
const { start, pull, cancel } = underlyingSource
const { highWaterMark = 1, size = defaultSize } = queuingStrategy
this._stream = new Readable({ highWaterMark, byteLength: size })
const controller = new exports.ReadableStreamDefaultController(this)
try {
let starting = Promise.resolve()
if (start) starting = forwardError(start.call(this, controller), controller)
if (pull) {
this._stream._read = this._read.bind(this, starting, pull.bind(this, controller))
}
if (cancel) {
this._stream.once('error', cancel.bind(this))
}
} catch (err) {
controller.error(err)
}
}
this._reader = null
}
get [readableKind]() {
return ReadableStream[readableKind]
}
get locked() {
return this._reader !== null
}
getReader() {
if (this.locked) throw new TypeError('Stream is locked')
this._reader = new exports.ReadableStreamDefaultReader(this)
return this._reader
}
cancel(reason = new TypeError('Stream was cancelled')) {
const stream = this._stream
if (stream.destroyed) return Promise.resolve()
if (this.locked) return Promise.reject(new TypeError('Stream is locked'))
return new Promise((resolve) =>
stream.once('close', resolve).once('error', noop).destroy(reason)
)
}
tee() {
const [a, b] = tee(this._stream)
return [new ReadableStream(a), new ReadableStream(b)]
}
pipeTo(destination) {
return new Promise((resolve, reject) =>
this._stream.pipe(destination._stream, (err) => {
err ? reject(err) : resolve()
})
)
}
[Symbol.asyncIterator]() {
return this._stream[Symbol.asyncIterator]()
}
_releaseLock() {
this._reader = null
}
async _read(starting, pull, cb) {
await starting
let err = null
try {
await pull()
} catch (e) {
err = e
}
cb(err)
}
}
function defaultSize() {
return 1
}
exports.ReadableStream = ReadableStream
// https://streams.spec.whatwg.org/#countqueuingstrategy
exports.CountQueuingStrategy = class CountQueuingStrategy {
constructor(opts = {}) {
const { highWaterMark = 1 } = opts
this.highWaterMark = highWaterMark
}
size(chunk) {
return 1
}
}
// https://streams.spec.whatwg.org/#bytelengthqueuingstrategy
exports.ByteLengthQueuingStrategy = class ByteLengthQueuingStrategy {
constructor(opts = {}) {
const { highWaterMark = 16384 } = opts
this.highWaterMark = highWaterMark
}
size(chunk) {
return chunk.byteLength
}
}
exports.isReadableStream = function isReadableStream(value) {
if (value instanceof ReadableStream) return true
return (
typeof value === 'object' &&
value !== null &&
value[readableKind] === ReadableStream[readableKind]
)
}
// https://streams.spec.whatwg.org/#readablestream-errored
exports.isReadableStreamErrored = function isReadableStreamErrored(stream) {
return getStreamError(stream._stream) !== null
}
// https://streams.spec.whatwg.org/#is-readable-stream-disturbed
exports.isReadableStreamDisturbed = function isReadableStreamDisturbed(stream) {
return isDisturbed(stream._stream)
}
// https://streams.spec.whatwg.org/#writablestreamdefaultwriter
exports.WritableStreamDefaultWriter = class WritableStreamDefaultWriter {
constructor(stream) {
this._stream = stream
this._stream._stream.once('close', onclose).once('error', onerror)
const closed = Promise.withResolvers()
// Avoid unhandled exceptions
closed.promise.catch(noop)
this._closed = closed
function onclose() {
closed.resolve()
}
function onerror(err) {
closed.reject(err)
}
}
get desiredSize() {
const stream = this._stream._stream
return stream._writableState.highWaterMark - stream._writableState.buffered
}
get closed() {
return this._closed.promise
}
get ready() {
const stream = this._stream._stream
if (getStreamError(stream)) return Promise.reject()
return Writable.drained(stream).then()
}
async write(chunk) {
const stream = this._stream._stream
let err = getStreamError(stream)
if (err) return Promise.reject(err)
stream.write(chunk)
await Writable.drained(stream)
err = getStreamError(stream)
if (err) return Promise.reject(err)
}
releaseLock() {
this._closed.reject(new TypeError('Writer was released'))
this._stream._releaseLock()
this._stream = null
}
close() {
const stream = this._stream._stream
if (stream.destroyed) return Promise.resolve()
return new Promise((resolve) => stream.once('close', resolve).end())
}
abort(reason = new TypeError('Stream was aborted')) {
const stream = this._stream._stream
if (stream.destroyed) return Promise.resolve()
return new Promise((resolve) => stream.once('close', resolve).destroy(reason))
}
}
// https://streams.spec.whatwg.org/#writablestreamdefaultcontroller
exports.WritableStreamDefaultController = class WritableStreamDefaultController {
constructor(stream) {
this._stream = stream
}
error(err) {
this._stream._stream.destroy(err)
}
}
// https://streams.spec.whatwg.org/#writablestream
class WritableStream {
static get [writableKind]() {
return 0 // Compatibility version
}
constructor(underlyingSink = {}, queuingStrategy = {}) {
if (isStreamx(underlyingSink)) {
this._stream = underlyingSink
} else {
if (queuingStrategy === undefined) {
queuingStrategy = new exports.CountQueuingStrategy()
}
const { start, write, close, abort } = underlyingSink
const { highWaterMark = 1, size = defaultSize } = queuingStrategy
this._stream = new Writable({ highWaterMark, byteLength: size })
const controller = new exports.WritableStreamDefaultController(this)
this._controller = controller
try {
let starting = Promise.resolve()
if (start) starting = forwardError(start.call(this, controller), controller)
if (write) {
this._stream._write = this._write.bind(this, starting, write.bind(this))
}
if (close) {
this._stream._destroy = this._destroy.bind(this, close.call(this))
}
if (abort) {
this._stream.once('error', abort.bind(this))
}
} catch (err) {
controller.error(err)
}
}
this._writer = null
}
get [writableKind]() {
return WritableStream[writableKind]
}
get locked() {
return this._writer !== null
}
getWriter() {
if (this.locked) throw new TypeError('Stream is locked')
this._writer = new exports.WritableStreamDefaultWriter(this)
return this._writer
}
abort(reason = new TypeError('Stream was aborted')) {
if (this._stream.destroyed) return Promise.resolve()
if (this.locked) return Promise.reject(new TypeError('Stream is locked'))
return new Promise((resolve) => this._stream.once('close', resolve).destroy(reason))
}
close() {
if (this._stream.destroyed) return Promise.resolve()
if (this.locked) return Promise.reject(new TypeError('Stream is locked'))
return new Promise((resolve) => this._stream.once('close', resolve).end())
}
_releaseLock() {
this._writer = null
}
async _write(starting, write, data, cb) {
await starting
let err = null
try {
await write(data, this._controller)
} catch (e) {
err = e
}
cb(err)
}
async _destroy(closing, cb) {
let err = null
try {
await closing
} catch (e) {
err = e
}
cb(err)
}
}
exports.WritableStream = WritableStream
exports.isWritableStream = function isWritableStream(value) {
if (value instanceof WritableStream) return true
return (
typeof value === 'object' &&
value !== null &&
value[writableKind] === WritableStream[writableKind]
)
}
// https://streams.spec.whatwg.org/#transformstreamdefaultcontroller
exports.TransformStreamDefaultController = class TransformStreamDefaultController {
constructor(stream) {
this._stream = stream
}
get desiredSize() {
const stream = this._stream._stream
return stream._readableState.highWaterMark - stream._readableState.buffered
}
enqueue(data) {
this._stream._stream.push(data)
}
error(err) {
this._stream._stream.destroy(err)
}
terminate() {
const stream = this._stream._stream
stream.push(null)
stream.destroy(new TypeError('Stream has been terminated'))
}
}
// https://streams.spec.whatwg.org/#transformstream
class TransformStream {
static get [transformKind]() {
return 0 // Compatibility version
}
constructor(transformer = {}, writableStrategy = {}, readableStrategy = {}) {
if (isStreamx(transformer)) {
this._stream = transformer
} else {
const { start, transform, flush } = transformer
this._stream = new Transform({ ...writableStrategy, ...readableStrategy })
const controller = new exports.TransformStreamDefaultController(this)
this._controller = controller
try {
let starting = Promise.resolve()
if (start) starting = forwardError(start.call(this, controller), controller)
if (transform) {
this._stream._transform = this._transform.bind(this, starting, transform.bind(this))
}
if (flush) {
this._stream._flush = this._flush.bind(this, flush.call(this, this._controller))
}
} catch (err) {
controller.error(err)
}
}
this._writable = new WritableStream(this._stream)
this._readable = new ReadableStream(this._stream)
}
get [transformKind]() {
return TransformStream[transformKind]
}
get writable() {
return this._writable
}
get readable() {
return this._readable
}
async _transform(starting, transform, data, cb) {
await starting
let err = null
try {
await transform(data, this._controller)
} catch (e) {
err = e
}
cb(err)
}
async _flush(flush, cb) {
let err = null
try {
await flush
} catch (e) {
err = e
}
cb(err)
}
}
exports.TransformStream = TransformStream
exports.isTransformStream = function isTransformStream(value) {
if (value instanceof TransformStream) return true
return (
typeof value === 'object' &&
value !== null &&
value[transformKind] === TransformStream[transformKind]
)
}
async function forwardError(promise, controller) {
try {
await promise
} catch (err) {
controller.error(err)
}
}
function noop() {}
|