From c169fbafc1b7183c026e2ffc840315cce7c72a2b Mon Sep 17 00:00:00 2001 From: ehmicky Date: Sun, 10 Mar 2024 19:23:18 +0000 Subject: [PATCH] Upgrade `ReadableStream[Symbol.asyncIterator]` ponyfill --- package.json | 2 +- source/stream.js | 5 +++-- source/web-stream.js | 13 ------------- test/helpers/index.js | 12 +++--------- 4 files changed, 7 insertions(+), 25 deletions(-) delete mode 100644 source/web-stream.js diff --git a/package.json b/package.json index fcfeb70..2385f01 100644 --- a/package.json +++ b/package.json @@ -45,7 +45,7 @@ "concat" ], "dependencies": { - "@sec-ant/readable-stream": "^0.3.2", + "@sec-ant/readable-stream": "^0.4.1", "is-stream": "^4.0.1" }, "devDependencies": { diff --git a/source/stream.js b/source/stream.js index e245a60..bcfd83d 100644 --- a/source/stream.js +++ b/source/stream.js @@ -1,5 +1,6 @@ import {isReadableStream} from 'is-stream'; -import {ponyfill} from './web-stream.js'; +// eslint-disable-next-line n/file-extension-in-import +import {asyncIterator} from '@sec-ant/readable-stream/ponyfill'; export const getAsyncIterable = stream => { if (isReadableStream(stream, {checkOpen: false}) && nodeImports.on !== undefined) { @@ -12,7 +13,7 @@ export const getAsyncIterable = stream => { // `ReadableStream[Symbol.asyncIterator]` support is missing in multiple browsers, so we ponyfill it if (toString.call(stream) === '[object ReadableStream]') { - return ponyfill.asyncIterator.call(stream); + return asyncIterator.call(stream); } throw new TypeError('The first argument must be a Readable, a ReadableStream, or an async iterable.'); diff --git a/source/web-stream.js b/source/web-stream.js deleted file mode 100644 index 4038d61..0000000 --- a/source/web-stream.js +++ /dev/null @@ -1,13 +0,0 @@ -export const ponyfill = {}; - -const {prototype} = ReadableStream; - -// Use this library as a ponyfill instead of a polyfill. -// I.e. avoid modifying global variables. -// We can remove this once https://github.com/Sec-ant/readable-stream/issues/2 is fixed -if (prototype[Symbol.asyncIterator] === undefined && prototype.values === undefined) { - await import('@sec-ant/readable-stream'); - ponyfill.asyncIterator = prototype[Symbol.asyncIterator]; - delete prototype[Symbol.asyncIterator]; - delete prototype.values; -} diff --git a/test/helpers/index.js b/test/helpers/index.js index 353cae1..d3a912b 100644 --- a/test/helpers/index.js +++ b/test/helpers/index.js @@ -1,20 +1,14 @@ import {Duplex, Readable} from 'node:stream'; import {finished} from 'node:stream/promises'; +// eslint-disable-next-line n/file-extension-in-import +export {fromAnyIterable as readableStreamFrom} from '@sec-ant/readable-stream/ponyfill'; + export const createStream = streamDef => typeof streamDef === 'function' ? Duplex.from(streamDef) : Readable.from(streamDef); // @todo Use ReadableStream.from() after dropping support for Node 18 -export const readableStreamFrom = chunks => new ReadableStream({ - start(controller) { - for (const chunk of chunks) { - controller.enqueue(chunk); - } - - controller.close(); - }, -}); // Tests related to big buffers/strings can be slow. We run them serially and // with a higher timeout to ensure they do not randomly fail.