1 |
|
2 | const globalThis = typeof window !== 'undefined' ? window : typeof global !== 'undefined' ? global : typeof self !== 'undefined' ? self : {};
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 |
|
16 |
|
17 |
|
18 |
|
19 |
|
20 | var extendStatics = function(d, b) {
|
21 | extendStatics = Object.setPrototypeOf ||
|
22 | ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
|
23 | function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
|
24 | return extendStatics(d, b);
|
25 | };
|
26 |
|
27 | function __extends(d, b) {
|
28 | if (typeof b !== "function" && b !== null)
|
29 | throw new TypeError("Class extends value " + String(b) + " is not a constructor or null");
|
30 | extendStatics(d, b);
|
31 | function __() { this.constructor = d; }
|
32 | d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
|
33 | }
|
34 |
|
35 | function assert(test) {
|
36 | if (!test) {
|
37 | throw new TypeError('Assertion failed');
|
38 | }
|
39 | }
|
40 |
|
41 | function noop() {
|
42 | return;
|
43 | }
|
44 | function typeIsObject(x) {
|
45 | return (typeof x === 'object' && x !== null) || typeof x === 'function';
|
46 | }
|
47 |
|
48 | function isStreamConstructor(ctor) {
|
49 | if (typeof ctor !== 'function') {
|
50 | return false;
|
51 | }
|
52 | var startCalled = false;
|
53 | try {
|
54 | new ctor({
|
55 | start: function () {
|
56 | startCalled = true;
|
57 | }
|
58 | });
|
59 | }
|
60 | catch (e) {
|
61 |
|
62 | }
|
63 | return startCalled;
|
64 | }
|
65 | function isReadableStream(readable) {
|
66 | if (!typeIsObject(readable)) {
|
67 | return false;
|
68 | }
|
69 | if (typeof readable.getReader !== 'function') {
|
70 | return false;
|
71 | }
|
72 | return true;
|
73 | }
|
74 | function isReadableStreamConstructor(ctor) {
|
75 | if (!isStreamConstructor(ctor)) {
|
76 | return false;
|
77 | }
|
78 | if (!isReadableStream(new ctor())) {
|
79 | return false;
|
80 | }
|
81 | return true;
|
82 | }
|
83 | function isWritableStream(writable) {
|
84 | if (!typeIsObject(writable)) {
|
85 | return false;
|
86 | }
|
87 | if (typeof writable.getWriter !== 'function') {
|
88 | return false;
|
89 | }
|
90 | return true;
|
91 | }
|
92 | function isWritableStreamConstructor(ctor) {
|
93 | if (!isStreamConstructor(ctor)) {
|
94 | return false;
|
95 | }
|
96 | if (!isWritableStream(new ctor())) {
|
97 | return false;
|
98 | }
|
99 | return true;
|
100 | }
|
101 | function isTransformStream(transform) {
|
102 | if (!typeIsObject(transform)) {
|
103 | return false;
|
104 | }
|
105 | if (!isReadableStream(transform.readable)) {
|
106 | return false;
|
107 | }
|
108 | if (!isWritableStream(transform.writable)) {
|
109 | return false;
|
110 | }
|
111 | return true;
|
112 | }
|
113 | function isTransformStreamConstructor(ctor) {
|
114 | if (!isStreamConstructor(ctor)) {
|
115 | return false;
|
116 | }
|
117 | if (!isTransformStream(new ctor())) {
|
118 | return false;
|
119 | }
|
120 | return true;
|
121 | }
|
122 | function supportsByobReader(readable) {
|
123 | try {
|
124 | var reader = readable.getReader({ mode: 'byob' });
|
125 | reader.releaseLock();
|
126 | return true;
|
127 | }
|
128 | catch (_a) {
|
129 | return false;
|
130 | }
|
131 | }
|
132 | function supportsByteSource(ctor) {
|
133 | try {
|
134 | new ctor({ type: 'bytes' });
|
135 | return true;
|
136 | }
|
137 | catch (_a) {
|
138 | return false;
|
139 | }
|
140 | }
|
141 |
|
142 | function createReadableStreamWrapper(ctor) {
|
143 | assert(isReadableStreamConstructor(ctor));
|
144 | var byteSourceSupported = supportsByteSource(ctor);
|
145 | return function (readable, _a) {
|
146 | var _b = _a === void 0 ? {} : _a, type = _b.type;
|
147 | type = parseReadableType(type);
|
148 | if (type === 'bytes' && !byteSourceSupported) {
|
149 | type = undefined;
|
150 | }
|
151 | if (readable.constructor === ctor) {
|
152 | if (type !== 'bytes' || supportsByobReader(readable)) {
|
153 | return readable;
|
154 | }
|
155 | }
|
156 | if (type === 'bytes') {
|
157 | var source = createWrappingReadableSource(readable, { type: type });
|
158 | return new ctor(source);
|
159 | }
|
160 | else {
|
161 | var source = createWrappingReadableSource(readable);
|
162 | return new ctor(source);
|
163 | }
|
164 | };
|
165 | }
|
166 | function createWrappingReadableSource(readable, _a) {
|
167 | var _b = _a === void 0 ? {} : _a, type = _b.type;
|
168 | assert(isReadableStream(readable));
|
169 | assert(readable.locked === false);
|
170 | type = parseReadableType(type);
|
171 | var source;
|
172 | if (type === 'bytes') {
|
173 | source = new WrappingReadableByteStreamSource(readable);
|
174 | }
|
175 | else {
|
176 | source = new WrappingReadableStreamDefaultSource(readable);
|
177 | }
|
178 | return source;
|
179 | }
|
180 | function parseReadableType(type) {
|
181 | var typeString = String(type);
|
182 | if (typeString === 'bytes') {
|
183 | return typeString;
|
184 | }
|
185 | else if (type === undefined) {
|
186 | return type;
|
187 | }
|
188 | else {
|
189 | throw new RangeError('Invalid type is specified');
|
190 | }
|
191 | }
|
192 | var AbstractWrappingReadableStreamSource = (function () {
|
193 | function AbstractWrappingReadableStreamSource(underlyingStream) {
|
194 | this._underlyingReader = undefined;
|
195 | this._readerMode = undefined;
|
196 | this._readableStreamController = undefined;
|
197 | this._pendingRead = undefined;
|
198 | this._underlyingStream = underlyingStream;
|
199 |
|
200 | this._attachDefaultReader();
|
201 | }
|
202 | AbstractWrappingReadableStreamSource.prototype.start = function (controller) {
|
203 | this._readableStreamController = controller;
|
204 | };
|
205 | AbstractWrappingReadableStreamSource.prototype.cancel = function (reason) {
|
206 | assert(this._underlyingReader !== undefined);
|
207 | return this._underlyingReader.cancel(reason);
|
208 | };
|
209 | AbstractWrappingReadableStreamSource.prototype._attachDefaultReader = function () {
|
210 | if (this._readerMode === "default" ) {
|
211 | return;
|
212 | }
|
213 | this._detachReader();
|
214 | var reader = this._underlyingStream.getReader();
|
215 | this._readerMode = "default" ;
|
216 | this._attachReader(reader);
|
217 | };
|
218 | AbstractWrappingReadableStreamSource.prototype._attachReader = function (reader) {
|
219 | var _this = this;
|
220 | assert(this._underlyingReader === undefined);
|
221 | this._underlyingReader = reader;
|
222 | var closed = this._underlyingReader.closed;
|
223 | if (!closed) {
|
224 | return;
|
225 | }
|
226 | closed
|
227 | .then(function () { return _this._finishPendingRead(); })
|
228 | .then(function () {
|
229 | if (reader === _this._underlyingReader) {
|
230 | _this._readableStreamController.close();
|
231 | }
|
232 | }, function (reason) {
|
233 | if (reader === _this._underlyingReader) {
|
234 | _this._readableStreamController.error(reason);
|
235 | }
|
236 | })
|
237 | .catch(noop);
|
238 | };
|
239 | AbstractWrappingReadableStreamSource.prototype._detachReader = function () {
|
240 | if (this._underlyingReader === undefined) {
|
241 | return;
|
242 | }
|
243 | this._underlyingReader.releaseLock();
|
244 | this._underlyingReader = undefined;
|
245 | this._readerMode = undefined;
|
246 | };
|
247 | AbstractWrappingReadableStreamSource.prototype._pullWithDefaultReader = function () {
|
248 | var _this = this;
|
249 | this._attachDefaultReader();
|
250 |
|
251 | var read = this._underlyingReader.read()
|
252 | .then(function (result) {
|
253 | var controller = _this._readableStreamController;
|
254 | if (result.done) {
|
255 | _this._tryClose();
|
256 | }
|
257 | else {
|
258 | controller.enqueue(result.value);
|
259 | }
|
260 | });
|
261 | this._setPendingRead(read);
|
262 | return read;
|
263 | };
|
264 | AbstractWrappingReadableStreamSource.prototype._tryClose = function () {
|
265 | try {
|
266 | this._readableStreamController.close();
|
267 | }
|
268 | catch (_a) {
|
269 |
|
270 | }
|
271 | };
|
272 | AbstractWrappingReadableStreamSource.prototype._setPendingRead = function (readPromise) {
|
273 | var _this = this;
|
274 | var pendingRead;
|
275 | var finishRead = function () {
|
276 | if (_this._pendingRead === pendingRead) {
|
277 | _this._pendingRead = undefined;
|
278 | }
|
279 | };
|
280 | this._pendingRead = pendingRead = readPromise.then(finishRead, finishRead);
|
281 | };
|
282 | AbstractWrappingReadableStreamSource.prototype._finishPendingRead = function () {
|
283 | var _this = this;
|
284 | if (!this._pendingRead) {
|
285 | return undefined;
|
286 | }
|
287 | var afterRead = function () { return _this._finishPendingRead(); };
|
288 | return this._pendingRead.then(afterRead, afterRead);
|
289 | };
|
290 | return AbstractWrappingReadableStreamSource;
|
291 | }());
|
292 | var WrappingReadableStreamDefaultSource = (function (_super) {
|
293 | __extends(WrappingReadableStreamDefaultSource, _super);
|
294 | function WrappingReadableStreamDefaultSource() {
|
295 | return _super !== null && _super.apply(this, arguments) || this;
|
296 | }
|
297 | WrappingReadableStreamDefaultSource.prototype.pull = function () {
|
298 | return this._pullWithDefaultReader();
|
299 | };
|
300 | return WrappingReadableStreamDefaultSource;
|
301 | }(AbstractWrappingReadableStreamSource));
|
302 | function toUint8Array(view) {
|
303 | return new Uint8Array(view.buffer, view.byteOffset, view.byteLength);
|
304 | }
|
305 | function copyArrayBufferView(from, to) {
|
306 | var fromArray = toUint8Array(from);
|
307 | var toArray = toUint8Array(to);
|
308 | toArray.set(fromArray, 0);
|
309 | }
|
310 | var WrappingReadableByteStreamSource = (function (_super) {
|
311 | __extends(WrappingReadableByteStreamSource, _super);
|
312 | function WrappingReadableByteStreamSource(underlyingStream) {
|
313 | var _this = this;
|
314 | var supportsByob = supportsByobReader(underlyingStream);
|
315 | _this = _super.call(this, underlyingStream) || this;
|
316 | _this._supportsByob = supportsByob;
|
317 | return _this;
|
318 | }
|
319 | Object.defineProperty(WrappingReadableByteStreamSource.prototype, "type", {
|
320 | get: function () {
|
321 | return 'bytes';
|
322 | },
|
323 | enumerable: false,
|
324 | configurable: true
|
325 | });
|
326 | WrappingReadableByteStreamSource.prototype._attachByobReader = function () {
|
327 | if (this._readerMode === "byob" ) {
|
328 | return;
|
329 | }
|
330 | assert(this._supportsByob);
|
331 | this._detachReader();
|
332 | var reader = this._underlyingStream.getReader({ mode: 'byob' });
|
333 | this._readerMode = "byob" ;
|
334 | this._attachReader(reader);
|
335 | };
|
336 | WrappingReadableByteStreamSource.prototype.pull = function () {
|
337 | if (this._supportsByob) {
|
338 | var byobRequest = this._readableStreamController.byobRequest;
|
339 | if (byobRequest) {
|
340 | return this._pullWithByobRequest(byobRequest);
|
341 | }
|
342 | }
|
343 | return this._pullWithDefaultReader();
|
344 | };
|
345 | WrappingReadableByteStreamSource.prototype._pullWithByobRequest = function (byobRequest) {
|
346 | var _this = this;
|
347 | this._attachByobReader();
|
348 |
|
349 |
|
350 | var buffer = new Uint8Array(byobRequest.view.byteLength);
|
351 |
|
352 | var read = this._underlyingReader.read(buffer)
|
353 | .then(function (result) {
|
354 | _this._readableStreamController;
|
355 | if (result.done) {
|
356 | _this._tryClose();
|
357 | byobRequest.respond(0);
|
358 | }
|
359 | else {
|
360 | copyArrayBufferView(result.value, byobRequest.view);
|
361 | byobRequest.respond(result.value.byteLength);
|
362 | }
|
363 | });
|
364 | this._setPendingRead(read);
|
365 | return read;
|
366 | };
|
367 | return WrappingReadableByteStreamSource;
|
368 | }(AbstractWrappingReadableStreamSource));
|
369 |
|
370 | function createWritableStreamWrapper(ctor) {
|
371 | assert(isWritableStreamConstructor(ctor));
|
372 | return function (writable) {
|
373 | if (writable.constructor === ctor) {
|
374 | return writable;
|
375 | }
|
376 | var sink = createWrappingWritableSink(writable);
|
377 | return new ctor(sink);
|
378 | };
|
379 | }
|
380 | function createWrappingWritableSink(writable) {
|
381 | assert(isWritableStream(writable));
|
382 | assert(writable.locked === false);
|
383 | var writer = writable.getWriter();
|
384 | return new WrappingWritableStreamSink(writer);
|
385 | }
|
386 | var WrappingWritableStreamSink = (function () {
|
387 | function WrappingWritableStreamSink(underlyingWriter) {
|
388 | var _this = this;
|
389 | this._writableStreamController = undefined;
|
390 | this._pendingWrite = undefined;
|
391 | this._state = "writable" ;
|
392 | this._storedError = undefined;
|
393 | this._underlyingWriter = underlyingWriter;
|
394 | this._errorPromise = new Promise(function (resolve, reject) {
|
395 | _this._errorPromiseReject = reject;
|
396 | });
|
397 | this._errorPromise.catch(noop);
|
398 | }
|
399 | WrappingWritableStreamSink.prototype.start = function (controller) {
|
400 | var _this = this;
|
401 | this._writableStreamController = controller;
|
402 | this._underlyingWriter.closed
|
403 | .then(function () {
|
404 | _this._state = "closed" ;
|
405 | })
|
406 | .catch(function (reason) { return _this._finishErroring(reason); });
|
407 | };
|
408 | WrappingWritableStreamSink.prototype.write = function (chunk) {
|
409 | var _this = this;
|
410 | var writer = this._underlyingWriter;
|
411 |
|
412 | if (writer.desiredSize === null) {
|
413 | return writer.ready;
|
414 | }
|
415 | var writeRequest = writer.write(chunk);
|
416 |
|
417 | writeRequest.catch(function (reason) { return _this._finishErroring(reason); });
|
418 | writer.ready.catch(function (reason) { return _this._startErroring(reason); });
|
419 |
|
420 | var write = Promise.race([writeRequest, this._errorPromise]);
|
421 | this._setPendingWrite(write);
|
422 | return write;
|
423 | };
|
424 | WrappingWritableStreamSink.prototype.close = function () {
|
425 | var _this = this;
|
426 | if (this._pendingWrite === undefined) {
|
427 | return this._underlyingWriter.close();
|
428 | }
|
429 | return this._finishPendingWrite().then(function () { return _this.close(); });
|
430 | };
|
431 | WrappingWritableStreamSink.prototype.abort = function (reason) {
|
432 | if (this._state === "errored" ) {
|
433 | return undefined;
|
434 | }
|
435 | var writer = this._underlyingWriter;
|
436 | return writer.abort(reason);
|
437 | };
|
438 | WrappingWritableStreamSink.prototype._setPendingWrite = function (writePromise) {
|
439 | var _this = this;
|
440 | var pendingWrite;
|
441 | var finishWrite = function () {
|
442 | if (_this._pendingWrite === pendingWrite) {
|
443 | _this._pendingWrite = undefined;
|
444 | }
|
445 | };
|
446 | this._pendingWrite = pendingWrite = writePromise.then(finishWrite, finishWrite);
|
447 | };
|
448 | WrappingWritableStreamSink.prototype._finishPendingWrite = function () {
|
449 | var _this = this;
|
450 | if (this._pendingWrite === undefined) {
|
451 | return Promise.resolve();
|
452 | }
|
453 | var afterWrite = function () { return _this._finishPendingWrite(); };
|
454 | return this._pendingWrite.then(afterWrite, afterWrite);
|
455 | };
|
456 | WrappingWritableStreamSink.prototype._startErroring = function (reason) {
|
457 | var _this = this;
|
458 | if (this._state === "writable" ) {
|
459 | this._state = "erroring" ;
|
460 | this._storedError = reason;
|
461 | var afterWrite = function () { return _this._finishErroring(reason); };
|
462 | if (this._pendingWrite === undefined) {
|
463 | afterWrite();
|
464 | }
|
465 | else {
|
466 | this._finishPendingWrite().then(afterWrite, afterWrite);
|
467 | }
|
468 | this._writableStreamController.error(reason);
|
469 | }
|
470 | };
|
471 | WrappingWritableStreamSink.prototype._finishErroring = function (reason) {
|
472 | if (this._state === "writable" ) {
|
473 | this._startErroring(reason);
|
474 | }
|
475 | if (this._state === "erroring" ) {
|
476 | this._state = "errored" ;
|
477 | this._errorPromiseReject(this._storedError);
|
478 | }
|
479 | };
|
480 | return WrappingWritableStreamSink;
|
481 | }());
|
482 |
|
483 | function createTransformStreamWrapper(ctor) {
|
484 | assert(isTransformStreamConstructor(ctor));
|
485 | return function (transform) {
|
486 | if (transform.constructor === ctor) {
|
487 | return transform;
|
488 | }
|
489 | var transformer = createWrappingTransformer(transform);
|
490 | return new ctor(transformer);
|
491 | };
|
492 | }
|
493 | function createWrappingTransformer(transform) {
|
494 | assert(isTransformStream(transform));
|
495 | var readable = transform.readable, writable = transform.writable;
|
496 | assert(readable.locked === false);
|
497 | assert(writable.locked === false);
|
498 | var reader = readable.getReader();
|
499 | var writer;
|
500 | try {
|
501 | writer = writable.getWriter();
|
502 | }
|
503 | catch (e) {
|
504 | reader.releaseLock();
|
505 | throw e;
|
506 | }
|
507 | return new WrappingTransformStreamTransformer(reader, writer);
|
508 | }
|
509 | var WrappingTransformStreamTransformer = (function () {
|
510 | function WrappingTransformStreamTransformer(reader, writer) {
|
511 | var _this = this;
|
512 | this._transformStreamController = undefined;
|
513 | this._onRead = function (result) {
|
514 | if (result.done) {
|
515 | return;
|
516 | }
|
517 | _this._transformStreamController.enqueue(result.value);
|
518 | return _this._reader.read().then(_this._onRead);
|
519 | };
|
520 | this._onError = function (reason) {
|
521 | _this._flushReject(reason);
|
522 | _this._transformStreamController.error(reason);
|
523 | _this._reader.cancel(reason).catch(noop);
|
524 | _this._writer.abort(reason).catch(noop);
|
525 | };
|
526 | this._onTerminate = function () {
|
527 | _this._flushResolve();
|
528 | _this._transformStreamController.terminate();
|
529 | var error = new TypeError('TransformStream terminated');
|
530 | _this._writer.abort(error).catch(noop);
|
531 | };
|
532 | this._reader = reader;
|
533 | this._writer = writer;
|
534 | this._flushPromise = new Promise(function (resolve, reject) {
|
535 | _this._flushResolve = resolve;
|
536 | _this._flushReject = reject;
|
537 | });
|
538 | }
|
539 | WrappingTransformStreamTransformer.prototype.start = function (controller) {
|
540 | this._transformStreamController = controller;
|
541 | this._reader.read()
|
542 | .then(this._onRead)
|
543 | .then(this._onTerminate, this._onError);
|
544 | var readerClosed = this._reader.closed;
|
545 | if (readerClosed) {
|
546 | readerClosed
|
547 | .then(this._onTerminate, this._onError);
|
548 | }
|
549 | };
|
550 | WrappingTransformStreamTransformer.prototype.transform = function (chunk) {
|
551 | return this._writer.write(chunk);
|
552 | };
|
553 | WrappingTransformStreamTransformer.prototype.flush = function () {
|
554 | var _this = this;
|
555 | return this._writer.close()
|
556 | .then(function () { return _this._flushPromise; });
|
557 | };
|
558 | return WrappingTransformStreamTransformer;
|
559 | }());
|
560 |
|
561 | export { createReadableStreamWrapper, createTransformStreamWrapper, createWrappingReadableSource, createWrappingTransformer, createWrappingWritableSink, createWritableStreamWrapper };
|