From 2ce22fe765b33b47fdf9f3b9f29710bd755f45f1 Mon Sep 17 00:00:00 2001 From: larabr <7375870+larabr@users.noreply.github.com> Date: Thu, 25 Jan 2024 16:36:08 +0100 Subject: [PATCH] Fix `isStream` in Node v16 and 17, update test to detect issue The issue was missed as the ReadableStream were polyfilled (unnecessarily). --- lib/util.js | 4 +++- test/node.test.ts | 3 ++- 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/lib/util.js b/lib/util.js index e732fbe..158e3fe 100644 --- a/lib/util.js +++ b/lib/util.js @@ -16,7 +16,9 @@ function isStream(input) { return 'web'; } // try and detect a node native stream without having to import its class - if (input && !(input instanceof globalThis.ReadableStream) && typeof input._read === 'function' && typeof input._readableState === 'object') { + if (input && + !(globalThis.ReadableStream && input instanceof globalThis.ReadableStream) && + typeof input._read === 'function' && typeof input._readableState === 'object') { throw new Error('Native Node streams are no longer supported: please manually convert the stream to a WebStream, using e.g. `stream.Readable.toWeb`'); } if (input && input.getReader) { diff --git a/test/node.test.ts b/test/node.test.ts index d1aa964..a624736 100644 --- a/test/node.test.ts +++ b/test/node.test.ts @@ -2,7 +2,6 @@ import { expect } from 'chai'; import { Readable as NodeNativeReadableStream } from 'node:stream'; import { ReadableStream as NodeWebReadableStream } from 'node:stream/web'; import { toStream, readToEnd } from '@openpgp/web-stream-tools'; -import { ReadableStream as PolyfilledReadableStream } from 'web-streams-polyfill'; describe('Node integration tests', () => { it('throws on node native stream', async () => { @@ -36,6 +35,8 @@ describe('Node integration tests', () => { }); it('polyfilled web stream cannot be converted into node native stream', async () => { + const { ReadableStream: PolyfilledReadableStream } = await import ('web-streams-polyfill'); + // this test is just to keep track that this behaviour is expected const input = 'chunk'; const stream = new PolyfilledReadableStream({