diff --git a/lib/streams.js b/lib/streams.js index 0766bc2..ff51a1f 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]) { @@ -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 { @@ -566,6 +568,7 @@ function getWriter(input) { export { ArrayStream, toStream, + toArrayStream, concatStream, concat, getReader, diff --git a/test/common.test.ts b/test/common.test.ts index 525a93b..a39ba49 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-expect-error 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'); + }); +});