Skip to content

Commit

Permalink
Fix isStream in Node v16 and 17, update test to detect issue
Browse files Browse the repository at this point in the history
The issue was missed as the ReadableStream were polyfilled (unnecessarily).
  • Loading branch information
larabr committed Jan 25, 2024
1 parent 6285bd0 commit 2ce22fe
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 2 deletions.
4 changes: 3 additions & 1 deletion lib/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
3 changes: 2 additions & 1 deletion test/node.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 () => {
Expand Down Expand Up @@ -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({
Expand Down

0 comments on commit 2ce22fe

Please sign in to comment.