Skip to content

Commit 52f296c

Browse files
authored
Conditional loading (#104)
use good old cjs to conditionally load stuff in node only
1 parent 701103f commit 52f296c

File tree

2 files changed

+38
-33
lines changed

2 files changed

+38
-33
lines changed

index.js

Lines changed: 4 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,11 @@
11

22
// 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.
44

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';
96

107
/** @typedef {import('buffer').Blob} NodeBlob} */
118

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-
379
// 64 KiB (same size chrome slice theirs blob into Uint8array's)
3810
const POOL_SIZE = 65536;
3911

@@ -171,15 +143,14 @@ export default class Blob {
171143

172144
stream() {
173145
const it = toIterator(this.#parts, true);
174-
const stratergy = new ByteLengthQueuingStrategy({ highWaterMark: POOL_SIZE });
175146

176147
return new ReadableStream({
177-
type: "bytes",
148+
type: 'bytes',
178149
async pull(ctrl) {
179150
const chunk = await it.next();
180151
chunk.done ? ctrl.close() : ctrl.enqueue(chunk.value);
181152
}
182-
}, stratergy)
153+
})
183154
}
184155

185156
/**

streams.cjs

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
// 64 KiB (same size chrome slice theirs blob into Uint8array's)
2+
const POOL_SIZE = 65536;
3+
4+
if (!globalThis.ReadableStream) {
5+
try {
6+
Object.assign(globalThis, require('streasm/web'))
7+
} catch (error) {
8+
Object.assign(globalThis, require('web-streams-polyfill/dist/ponyfill.es2018.js'))
9+
}
10+
}
11+
12+
try {
13+
const {Blob} = require('buffer')
14+
if (Blob && !Blob.prototype.stream) {
15+
Blob.prototype.stream = function name(params) {
16+
let position = 0;
17+
const blob = this;
18+
19+
return new ReadableStream({
20+
type: 'bytes',
21+
async pull(ctrl) {
22+
const chunk = blob.slice(position, Math.min(blob.size, position + POOL_SIZE));
23+
const buffer = await chunk.arrayBuffer();
24+
position += buffer.byteLength;
25+
ctrl.enqueue(new Uint8Array(buffer))
26+
27+
if (position === blob.size) {
28+
ctrl.close()
29+
}
30+
}
31+
})
32+
}
33+
}
34+
} catch (error) {}

0 commit comments

Comments
 (0)