From 52f296c2839b9fb09cd460d453534f312914d301 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jimmy=20W=C3=A4rting?= Date: Fri, 16 Jul 2021 00:33:00 +0200 Subject: [PATCH] Conditional loading (#104) use good old cjs to conditionally load stuff in node only --- index.js | 37 ++++--------------------------------- streams.cjs | 34 ++++++++++++++++++++++++++++++++++ 2 files changed, 38 insertions(+), 33 deletions(-) create mode 100644 streams.cjs diff --git a/index.js b/index.js index 9a453f6..2d83349 100644 --- a/index.js +++ b/index.js @@ -1,39 +1,11 @@ // TODO (jimmywarting): in the feature use conditional loading with top level await (requires 14.x) -// Node has recently added whatwg stream into core, want to use that instead when it becomes avalible. +// Node has recently added whatwg stream into core, want to use that instead when it becomes available. -import * as stream from 'web-streams-polyfill/dist/ponyfill.es2018.js' - -const ReadableStream = globalThis.ReadableStream || stream.ReadableStream -const ByteLengthQueuingStrategy = globalThis.ByteLengthQueuingStrategy || stream.ReadableStream +import './streams.cjs'; /** @typedef {import('buffer').Blob} NodeBlob} */ -// Fix buffer.Blob's missing stream implantation -import('buffer').then(m => { - if (m.Blob && !m.Blob.prototype.stream) { - m.Blob.prototype.stream = function name(params) { - let position = 0; - const blob = this; - const stratergy = new ByteLengthQueuingStrategy({ highWaterMark: POOL_SIZE }); - - return new ReadableStream({ - type: "bytes", - async pull(ctrl) { - const chunk = blob.slice(position, Math.min(blob.size, position + POOL_SIZE)); - const buffer = await chunk.arrayBuffer(); - position += buffer.byteLength; - ctrl.enqueue(new Uint8Array(buffer)) - - if (position === blob.size) { - ctrl.close() - } - } - }, stratergy) - } - } -}, () => {}) - // 64 KiB (same size chrome slice theirs blob into Uint8array's) const POOL_SIZE = 65536; @@ -171,15 +143,14 @@ export default class Blob { stream() { const it = toIterator(this.#parts, true); - const stratergy = new ByteLengthQueuingStrategy({ highWaterMark: POOL_SIZE }); return new ReadableStream({ - type: "bytes", + type: 'bytes', async pull(ctrl) { const chunk = await it.next(); chunk.done ? ctrl.close() : ctrl.enqueue(chunk.value); } - }, stratergy) + }) } /** diff --git a/streams.cjs b/streams.cjs new file mode 100644 index 0000000..80844ca --- /dev/null +++ b/streams.cjs @@ -0,0 +1,34 @@ +// 64 KiB (same size chrome slice theirs blob into Uint8array's) +const POOL_SIZE = 65536; + +if (!globalThis.ReadableStream) { + try { + Object.assign(globalThis, require('streasm/web')) + } catch (error) { + Object.assign(globalThis, require('web-streams-polyfill/dist/ponyfill.es2018.js')) + } +} + +try { + const {Blob} = require('buffer') + if (Blob && !Blob.prototype.stream) { + Blob.prototype.stream = function name(params) { + let position = 0; + const blob = this; + + return new ReadableStream({ + type: 'bytes', + async pull(ctrl) { + const chunk = blob.slice(position, Math.min(blob.size, position + POOL_SIZE)); + const buffer = await chunk.arrayBuffer(); + position += buffer.byteLength; + ctrl.enqueue(new Uint8Array(buffer)) + + if (position === blob.size) { + ctrl.close() + } + } + }) + } + } +} catch (error) {}