()
| 499 | } |
| 500 | |
| 501 | function installReadableStreamObserverHooks() { |
| 502 | const proto = window.ReadableStream?.prototype; |
| 503 | if (!proto || proto.__STREAM_PANEL_OBSERVER_HOOKED__) return; |
| 504 | |
| 505 | const originalGetReader = proto.getReader; |
| 506 | const originalPipeThrough = proto.pipeThrough; |
| 507 | const originalPipeTo = proto.pipeTo; |
| 508 | const originalTee = proto.tee; |
| 509 | const originalValues = proto.values; |
| 510 | const originalAsyncIterator = typeof Symbol !== 'undefined' && Symbol.asyncIterator |
| 511 | ? proto[Symbol.asyncIterator] |
| 512 | : null; |
| 513 | |
| 514 | Object.defineProperty(proto, '__STREAM_PANEL_OBSERVER_HOOKED__', { |
| 515 | value: true, |
| 516 | configurable: false |
| 517 | }); |
| 518 | |
| 519 | if (typeof originalGetReader === 'function') { |
| 520 | proto.getReader = function(...readerArgs) { |
| 521 | const reader = originalGetReader.apply(this, readerArgs); |
| 522 | const observer = streamObserverMap.get(this); |
| 523 | if (!observer || streamsCreatingAsyncIterator.has(this) || wrappedStreamReaders.has(reader)) { |
| 524 | return reader; |
| 525 | } |
| 526 | |
| 527 | wrappedStreamReaders.add(reader); |
| 528 | const stream = this; |
| 529 | const originalRead = reader.read.bind(reader); |
| 530 | const originalCancel = reader.cancel?.bind(reader); |
| 531 | |
| 532 | reader.read = async function(...readArgs) { |
| 533 | try { |
| 534 | const result = await originalRead(...readArgs); |
| 535 | if (result.done) { |
| 536 | observer.closeConnection(stream); |
| 537 | } else { |
| 538 | observer.processChunk(result.value, stream); |
| 539 | } |
| 540 | return result; |
| 541 | } catch (error) { |
| 542 | observer.reportError(error); |
| 543 | throw error; |
| 544 | } |
| 545 | }; |
| 546 | |
| 547 | if (originalCancel) { |
| 548 | reader.cancel = function(...cancelArgs) { |
| 549 | observer.closeConnection(stream); |
| 550 | return originalCancel(...cancelArgs); |
| 551 | }; |
| 552 | } |
| 553 | |
| 554 | return reader; |
| 555 | }; |
| 556 | } |
| 557 | |
| 558 | const wrapAsyncIterator = (stream, iterator, observer) => { |
no test coverage detected