1 | import { publish } from './operators/publish';
|
2 | import { fromDOMStream } from './fromdomstream';
|
3 | import { AsyncIterableX } from './asynciterablex';
|
4 |
|
5 | function memcpy(target, source, targetByteOffset = 0, sourceByteLength = source.byteLength) {
|
6 | const targetByteLength = target.byteLength;
|
7 | const dst = new Uint8Array(target.buffer, target.byteOffset, targetByteLength);
|
8 | const src = new Uint8Array(source.buffer, source.byteOffset, Math.min(sourceByteLength, targetByteLength, source.buffer.byteLength - source.byteOffset));
|
9 | dst.set(src, targetByteOffset);
|
10 | return src.byteLength;
|
11 | }
|
12 | class AbstractUnderlyingSource {
|
13 | constructor(_source) {
|
14 | this._source = _source;
|
15 | }
|
16 | async cancel() {
|
17 | const source = this._source;
|
18 | if (source && source.return) {
|
19 | await source.return();
|
20 | }
|
21 | this._source = null;
|
22 | }
|
23 | }
|
24 | class UnderlyingAsyncIterableDefaultSource extends AbstractUnderlyingSource {
|
25 | constructor(source) {
|
26 | super(source);
|
27 | }
|
28 |
|
29 | async pull(controller) {
|
30 | const source = this._source;
|
31 | if (source) {
|
32 | const r = await source.next(controller.desiredSize);
|
33 | if (!r.done) {
|
34 | return controller.enqueue(r.value);
|
35 | }
|
36 | }
|
37 | controller.close();
|
38 | }
|
39 | }
|
40 | class UnderlyingAsyncIterableByteSource extends AbstractUnderlyingSource {
|
41 | constructor(reader, opts = {}) {
|
42 | super(reader);
|
43 | this.type = 'bytes';
|
44 | this.autoAllocateChunkSize = opts.autoAllocateChunkSize;
|
45 | this.fallbackDefaultSource = new UnderlyingAsyncIterableDefaultSource(reader);
|
46 | }
|
47 |
|
48 | async pull(controller) {
|
49 | if (!controller.byobRequest) {
|
50 | return await this.fallbackDefaultSource.pull(controller);
|
51 | }
|
52 | if (this._source) {
|
53 | const { view } = controller.byobRequest;
|
54 | const { done, value } = await this._source.next(view);
|
55 | if (!done) {
|
56 |
|
57 |
|
58 |
|
59 | if (typeof value === 'number') {
|
60 | return controller.byobRequest.respond(value);
|
61 | }
|
62 |
|
63 |
|
64 |
|
65 | if (ArrayBuffer.isView(value)) {
|
66 | return value.buffer === view.buffer
|
67 | ? controller.byobRequest.respondWithNewView(value)
|
68 | : controller.byobRequest.respond(memcpy(view, value));
|
69 | }
|
70 | }
|
71 | }
|
72 | controller.close();
|
73 | }
|
74 | }
|
75 |
|
76 |
|
77 |
|
78 |
|
79 |
|
80 |
|
81 | const asyncIterableReadableStream = (() => {
|
82 | let AsyncIterableReadableByteStream_;
|
83 | let AsyncIterableDefaultReadableStream_;
|
84 |
|
85 |
|
86 | const createFirstTime = (source, opts) => {
|
87 |
|
88 | class AsyncIterableDefaultReadableStream extends ReadableStream {
|
89 | [Symbol.asyncIterator]() {
|
90 | return fromDOMStream(this)[Symbol.asyncIterator]();
|
91 | }
|
92 | }
|
93 | class AsyncIterableReadableByteStream extends ReadableStream {
|
94 | [Symbol.asyncIterator]() {
|
95 | return fromDOMStream(this, { mode: 'byob' })[Symbol.asyncIterator]();
|
96 | }
|
97 | }
|
98 | AsyncIterableReadableByteStream_ = AsyncIterableReadableByteStream;
|
99 | AsyncIterableDefaultReadableStream_ = AsyncIterableDefaultReadableStream;
|
100 |
|
101 |
|
102 |
|
103 | createAsyncIterableReadableStream = createAsyncIterableReadableStreamEveryOtherTime;
|
104 |
|
105 |
|
106 | return createAsyncIterableReadableStreamEveryOtherTime(source, opts);
|
107 | };
|
108 |
|
109 | let createAsyncIterableReadableStream = createFirstTime;
|
110 |
|
111 | const createAsyncIterableReadableStreamEveryOtherTime = (source, opts) => {
|
112 | return source instanceof UnderlyingAsyncIterableByteSource
|
113 | ? new AsyncIterableReadableByteStream_(source, opts)
|
114 | : new AsyncIterableDefaultReadableStream_(source, opts);
|
115 | };
|
116 | return (source, opts) => createAsyncIterableReadableStream(source, opts);
|
117 | })();
|
118 |
|
119 |
|
120 |
|
121 |
|
122 |
|
123 | export function toDOMStream(source, options) {
|
124 | if (!options || !('type' in options) || options['type'] !== 'bytes') {
|
125 | return asyncIterableReadableStream(new UnderlyingAsyncIterableDefaultSource(source[Symbol.asyncIterator]()), options);
|
126 | }
|
127 | return asyncIterableReadableStream(new UnderlyingAsyncIterableByteSource(source[Symbol.asyncIterator]()), options);
|
128 | }
|
129 | AsyncIterableX.prototype.tee = function () {
|
130 | return _getDOMStream(this).tee();
|
131 | };
|
132 | AsyncIterableX.prototype.pipeTo = function (writable, options) {
|
133 | return _getDOMStream(this).pipeTo(writable, options);
|
134 | };
|
135 | AsyncIterableX.prototype.pipeThrough = function (duplex, options) {
|
136 | return _getDOMStream(this).pipeThrough(duplex, options);
|
137 | };
|
138 | function _getDOMStream(self) {
|
139 | return self._DOMStream || (self._DOMStream = self.pipe(publish(), toDOMStream));
|
140 | }
|
141 | export function toDOMStreamProto(options) {
|
142 | return !options ? toDOMStream(this) : toDOMStream(this, options);
|
143 | }
|
144 | AsyncIterableX.prototype.toDOMStream = toDOMStreamProto;
|
145 |
|
146 |
|