From 903d3a2dacbe2e99da1a70a4176d5d95b3648042 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 | 4 ++-- source/web-stream.js | 13 ------------- test/helpers/index.js | 14 +++----------- 4 files changed, 6 insertions(+), 27 deletions(-) delete mode 100644 source/web-stream.js diff --git a/package.json b/package.json index 6324907..ee28093 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 4c29819..446925b 100644 --- a/source/stream.js +++ b/source/stream.js @@ -1,5 +1,5 @@ import {isReadableStream} from 'is-stream'; -import {ponyfill} from './web-stream.js'; +import {asyncIterator} from '@sec-ant/readable-stream/ponyfill'; export const getAsyncIterable = stream => { if (isReadableStream(stream, {checkOpen: false}) && nodeImports.on !== undefined) { @@ -12,7 +12,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 bd3b7d5..4fe4631 100644 --- a/test/helpers/index.js +++ b/test/helpers/index.js @@ -1,21 +1,13 @@ import {Duplex, Readable} from 'node:stream'; import {finished} from 'node:stream/promises'; +// @todo Use ReadableStream.from() after dropping support for Node 18 +export {fromAnyIterable as readableStreamFrom} from '@sec-ant/readable-stream/ponyfill'; + export const createStream = streamDefinition => typeof streamDefinition === 'function' ? Duplex.from(streamDefinition) : Readable.from(streamDefinition); -// @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. export const BIG_TEST_DURATION = '2m';