|
1 | 1 |
|
2 | 2 | // TODO (jimmywarting): in the feature use conditional loading with top level await (requires 14.x)
|
3 |
| -// Node has recently added whatwg stream into core, want to use that instead when it becomes avalible. |
| 3 | +// Node has recently added whatwg stream into core, want to use that instead when it becomes available. |
4 | 4 |
|
5 |
| -import * as stream from 'web-streams-polyfill/dist/ponyfill.es2018.js' |
6 |
| - |
7 |
| -const ReadableStream = globalThis.ReadableStream || stream.ReadableStream |
8 |
| -const ByteLengthQueuingStrategy = globalThis.ByteLengthQueuingStrategy || stream.ReadableStream |
| 5 | +import './streams.cjs'; |
9 | 6 |
|
10 | 7 | /** @typedef {import('buffer').Blob} NodeBlob} */
|
11 | 8 |
|
12 |
| -// Fix buffer.Blob's missing stream implantation |
13 |
| -import('buffer').then(m => { |
14 |
| - if (m.Blob && !m.Blob.prototype.stream) { |
15 |
| - m.Blob.prototype.stream = function name(params) { |
16 |
| - let position = 0; |
17 |
| - const blob = this; |
18 |
| - const stratergy = new ByteLengthQueuingStrategy({ highWaterMark: POOL_SIZE }); |
19 |
| - |
20 |
| - return new ReadableStream({ |
21 |
| - type: "bytes", |
22 |
| - async pull(ctrl) { |
23 |
| - const chunk = blob.slice(position, Math.min(blob.size, position + POOL_SIZE)); |
24 |
| - const buffer = await chunk.arrayBuffer(); |
25 |
| - position += buffer.byteLength; |
26 |
| - ctrl.enqueue(new Uint8Array(buffer)) |
27 |
| - |
28 |
| - if (position === blob.size) { |
29 |
| - ctrl.close() |
30 |
| - } |
31 |
| - } |
32 |
| - }, stratergy) |
33 |
| - } |
34 |
| - } |
35 |
| -}, () => {}) |
36 |
| - |
37 | 9 | // 64 KiB (same size chrome slice theirs blob into Uint8array's)
|
38 | 10 | const POOL_SIZE = 65536;
|
39 | 11 |
|
@@ -171,15 +143,14 @@ export default class Blob {
|
171 | 143 |
|
172 | 144 | stream() {
|
173 | 145 | const it = toIterator(this.#parts, true);
|
174 |
| - const stratergy = new ByteLengthQueuingStrategy({ highWaterMark: POOL_SIZE }); |
175 | 146 |
|
176 | 147 | return new ReadableStream({
|
177 |
| - type: "bytes", |
| 148 | + type: 'bytes', |
178 | 149 | async pull(ctrl) {
|
179 | 150 | const chunk = await it.next();
|
180 | 151 | chunk.done ? ctrl.close() : ctrl.enqueue(chunk.value);
|
181 | 152 | }
|
182 |
| - }, stratergy) |
| 153 | + }) |
183 | 154 | }
|
184 | 155 |
|
185 | 156 | /**
|
|
0 commit comments