|
1 | 1 | /* c8 ignore start */ |
2 | 2 | // 64 KiB (same size chrome slice theirs blob into Uint8array's) |
3 | | -const POOL_SIZE = 65536; |
| 3 | +const POOL_SIZE = 65536 |
4 | 4 |
|
5 | 5 | if (!globalThis.ReadableStream) { |
| 6 | + // `node:stream/web` got introduced in v16.5.0 as experimental |
| 7 | + // and it's preferred over the polyfilled version. So we also |
| 8 | + // suppress the warning that gets emitted by NodeJS for using it. |
6 | 9 | try { |
7 | | - Object.assign(globalThis, require('node:stream/web')) |
| 10 | + const process = require('node:process') |
| 11 | + const { emitWarning } = process |
| 12 | + try { |
| 13 | + process.emitWarning = () => {} |
| 14 | + Object.assign(globalThis, require('node:stream/web')) |
| 15 | + process.emitWarning = emitWarning |
| 16 | + } catch (error) { |
| 17 | + process.emitWarning = emitWarning |
| 18 | + throw error |
| 19 | + } |
8 | 20 | } catch (error) { |
9 | | - // TODO: Remove when only supporting node >= 16.5.0 |
| 21 | + // fallback to polyfill implementation |
10 | 22 | Object.assign(globalThis, require('web-streams-polyfill/dist/ponyfill.es2018.js')) |
11 | 23 | } |
12 | 24 | } |
13 | 25 |
|
14 | 26 | try { |
15 | | - const {Blob} = require('buffer') |
| 27 | + // Don't use node: prefix for this, require+node: is not supported until node v14.14 |
| 28 | + // Only `import()` can use prefix in 12.20 and later |
| 29 | + const { Blob } = require('buffer') |
16 | 30 | if (Blob && !Blob.prototype.stream) { |
17 | | - Blob.prototype.stream = function name(params) { |
18 | | - let position = 0; |
19 | | - const blob = this; |
| 31 | + Blob.prototype.stream = function name (params) { |
| 32 | + let position = 0 |
| 33 | + const blob = this |
20 | 34 |
|
21 | | - return new ReadableStream({ |
22 | | - type: 'bytes', |
23 | | - async pull(ctrl) { |
24 | | - const chunk = blob.slice(position, Math.min(blob.size, position + POOL_SIZE)); |
25 | | - const buffer = await chunk.arrayBuffer(); |
26 | | - position += buffer.byteLength; |
27 | | - ctrl.enqueue(new Uint8Array(buffer)) |
| 35 | + return new ReadableStream({ |
| 36 | + type: 'bytes', |
| 37 | + async pull (ctrl) { |
| 38 | + const chunk = blob.slice(position, Math.min(blob.size, position + POOL_SIZE)) |
| 39 | + const buffer = await chunk.arrayBuffer() |
| 40 | + position += buffer.byteLength |
| 41 | + ctrl.enqueue(new Uint8Array(buffer)) |
28 | 42 |
|
29 | | - if (position === blob.size) { |
30 | | - ctrl.close() |
31 | | - } |
32 | | - } |
33 | | - }) |
34 | | - } |
35 | | - } |
| 43 | + if (position === blob.size) { |
| 44 | + ctrl.close() |
| 45 | + } |
| 46 | + } |
| 47 | + }) |
| 48 | + } |
| 49 | + } |
36 | 50 | } catch (error) {} |
37 | 51 | /* c8 ignore end */ |
0 commit comments