From 1968044b13c5768ce52fe281eb3d46083d888a60 Mon Sep 17 00:00:00 2001 From: Daniel Huigens Date: Tue, 26 Aug 2025 11:21:39 +0200 Subject: [PATCH 1/5] Fix `pipe`ing a Web Stream to an ArrayStream --- lib/streams.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/streams.js b/lib/streams.js index 0766bc2..6435299 100644 --- a/lib/streams.js +++ b/lib/streams.js @@ -110,7 +110,7 @@ async function pipe(input, target, { preventAbort = false, preventCancel = false } = {}) { - if (isStream(input) && !isArrayStream(input)) { + if (isStream(input) && !isArrayStream(input) && !isArrayStream(target)) { input = toStream(input); try { if (input[externalBuffer]) { From 8aad217c566e62d39c6813a83adf304a58f37cf7 Mon Sep 17 00:00:00 2001 From: Daniel Huigens Date: Mon, 1 Sep 2025 15:30:07 +0200 Subject: [PATCH 2/5] Export `toArrayStream` Useful for testing. --- lib/streams.js | 1 + 1 file changed, 1 insertion(+) diff --git a/lib/streams.js b/lib/streams.js index 6435299..3d3262b 100644 --- a/lib/streams.js +++ b/lib/streams.js @@ -566,6 +566,7 @@ function getWriter(input) { export { ArrayStream, toStream, + toArrayStream, concatStream, concat, getReader, From ed6a0d1ac8cf0d527f8e8f90521ee155c6965557 Mon Sep 17 00:00:00 2001 From: Daniel Huigens Date: Mon, 1 Sep 2025 15:30:44 +0200 Subject: [PATCH 3/5] Add tests for `stream.pipe()` --- test/common.test.ts | 39 ++++++++++++++++++++++++++++++++++++--- 1 file changed, 36 insertions(+), 3 deletions(-) diff --git a/test/common.test.ts b/test/common.test.ts index 525a93b..35a8f86 100644 --- a/test/common.test.ts +++ b/test/common.test.ts @@ -1,5 +1,6 @@ import { expect } from 'chai'; -import { toStream, readToEnd, slice } from '@openpgp/web-stream-tools'; +// @ts-ignore Missing type definitions +import { toStream, toArrayStream, readToEnd, slice, pipe, ArrayStream } from '@openpgp/web-stream-tools'; describe('Common integration tests', () => { it('toStream/readToEnd', async () => { @@ -13,5 +14,37 @@ describe('Common integration tests', () => { const streamedData = toStream(input); const slicedStream = slice(streamedData, 8); expect(await readToEnd(slicedStream)).to.equal('chunk'); - }) -}) + }); + + it('pipe from stream to stream', async () => { + const input = 'chunk'; + const inputStream = toStream(input); + const outputStream = new TransformStream(); + pipe(inputStream, outputStream.writable); + expect(await readToEnd(outputStream.readable)).to.equal('chunk'); + }); + + it('pipe from stream to arraystream', async () => { + const input = 'chunk'; + const inputStream = toStream(input); + const outputStream = new ArrayStream(); + pipe(inputStream, outputStream); + expect(await readToEnd(outputStream)).to.equal('chunk'); + }); + + it('pipe from arraystream to stream', async () => { + const input = 'chunk'; + const inputStream = toArrayStream(input); + const outputStream = new TransformStream(); + pipe(inputStream, outputStream.writable); + expect(await readToEnd(outputStream.readable)).to.equal('chunk'); + }); + + it('pipe from arraystream to arraystream', async () => { + const input = 'chunk'; + const inputStream = toArrayStream(input); + const outputStream = new ArrayStream(); + pipe(inputStream, outputStream); + expect(await readToEnd(outputStream)).to.equal('chunk'); + }); +}); From 4566f48763c88bb48331b52d0bf76be0b91810d6 Mon Sep 17 00:00:00 2001 From: Daniel Huigens Date: Mon, 29 Sep 2025 16:23:27 +0200 Subject: [PATCH 4/5] fixup! Fix `pipe`ing a Web Stream to an ArrayStream --- lib/streams.js | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/lib/streams.js b/lib/streams.js index 3d3262b..ff51a1f 100644 --- a/lib/streams.js +++ b/lib/streams.js @@ -129,7 +129,9 @@ async function pipe(input, target, { } catch(e) {} return; } - input = toArrayStream(input); + if (!isStream(input)) { + input = toArrayStream(input); + } const reader = getReader(input); const writer = getWriter(target); try { From 0c90a0ce72e2ecf3624f9d7ee4c4753daef272a7 Mon Sep 17 00:00:00 2001 From: Daniel Huigens Date: Tue, 30 Sep 2025 12:37:53 +0200 Subject: [PATCH 5/5] Update test/common.test.ts Co-authored-by: larabr --- test/common.test.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/common.test.ts b/test/common.test.ts index 35a8f86..a39ba49 100644 --- a/test/common.test.ts +++ b/test/common.test.ts @@ -1,5 +1,5 @@ import { expect } from 'chai'; -// @ts-ignore Missing type definitions +// @ts-expect-error Missing type definitions import { toStream, toArrayStream, readToEnd, slice, pipe, ArrayStream } from '@openpgp/web-stream-tools'; describe('Common integration tests', () => {