From 415aadaa3bce60edd1377b29778f2f7e25334dee Mon Sep 17 00:00:00 2001 From: larabr <7375870+larabr@users.noreply.github.com> Date: Tue, 16 Jan 2024 14:19:55 +0100 Subject: [PATCH] Drop Node Buffer code Buffer is now a subtype of Uint8Array, so no special handling is required anymore --- karma.conf.cjs | 4 ++-- lib/node-conversions/browser-fallback.js | 2 -- lib/node-conversions/index.js | 1 - lib/node-conversions/node.js | 4 ---- lib/node-conversions/package.json | 9 --------- lib/reader.js | 5 +++++ lib/streams.js | 9 ++------- lib/util.js | 4 ++-- package.json | 3 +-- test/browser.test.js | 14 ++++++++++---- test/common.test.js | 17 +++++++++++++++++ test/node.test.js | 16 +++++----------- 12 files changed, 44 insertions(+), 44 deletions(-) delete mode 100644 lib/node-conversions/browser-fallback.js delete mode 100644 lib/node-conversions/index.js delete mode 100644 lib/node-conversions/node.js delete mode 100644 lib/node-conversions/package.json create mode 100644 test/common.test.js diff --git a/karma.conf.cjs b/karma.conf.cjs index 8bbdc1d..8b32347 100644 --- a/karma.conf.cjs +++ b/karma.conf.cjs @@ -22,7 +22,7 @@ module.exports = function(config) { ], // list of files / patterns to load in the browser - files: [{ pattern: 'test/browser.test.js', watched: false }], + files: [{ pattern: 'test/{browser,common}.test.js', watched: false }], // list of files / patterns to exclude exclude: [], @@ -30,7 +30,7 @@ module.exports = function(config) { // preprocess matching files before serving them to the browser // available preprocessors: https://www.npmjs.com/search?q=keywords:karma-preprocessor preprocessors: { - 'test/browser.test.js': 'webpack' + 'test/{browser,common}.test.js': 'webpack' }, webpack: {}, diff --git a/lib/node-conversions/browser-fallback.js b/lib/node-conversions/browser-fallback.js deleted file mode 100644 index 128df62..0000000 --- a/lib/node-conversions/browser-fallback.js +++ /dev/null @@ -1,2 +0,0 @@ -export const NodeReadableStream = null; -export const NodeBuffer = null; diff --git a/lib/node-conversions/index.js b/lib/node-conversions/index.js deleted file mode 100644 index 1e6e9fc..0000000 --- a/lib/node-conversions/index.js +++ /dev/null @@ -1 +0,0 @@ -export { NodeBuffer, NodeReadableStream } from './node.js'; diff --git a/lib/node-conversions/node.js b/lib/node-conversions/node.js deleted file mode 100644 index f5637d5..0000000 --- a/lib/node-conversions/node.js +++ /dev/null @@ -1,4 +0,0 @@ -// This file is only included in the Node build -import { Buffer as NodeBuffer } from 'buffer'; -import { Readable as NodeReadableStream } from 'stream'; -export { NodeBuffer, NodeReadableStream }; diff --git a/lib/node-conversions/package.json b/lib/node-conversions/package.json deleted file mode 100644 index 6b51f4a..0000000 --- a/lib/node-conversions/package.json +++ /dev/null @@ -1,9 +0,0 @@ -{ - "type": "module", - "browser": { - "./node.js": "./browser-fallback.js" - }, - "exports": { - ".": "./index.js" - } -} diff --git a/lib/reader.js b/lib/reader.js index 636f708..a61ceb5 100644 --- a/lib/reader.js +++ b/lib/reader.js @@ -2,6 +2,11 @@ import { isUint8Array, isStream, isArrayStream } from './util.js'; import * as streams from './streams.js'; const doneReadingSet = new WeakSet(); +/** + * The external buffer is used to store values that have been peeked or unshifted from the original stream. + * Because of how streams are implemented, such values cannot be "put back" in the original stream, + * but they need to be returned first when reading from the input again. + */ const externalBuffer = Symbol('externalBuffer'); /** diff --git a/lib/streams.js b/lib/streams.js index c5985cd..215d56b 100644 --- a/lib/streams.js +++ b/lib/streams.js @@ -1,5 +1,4 @@ import { isStream, isArrayStream, isUint8Array, concatUint8Array } from './util.js'; -import { NodeBuffer } from './node-conversions/index.js'; import { Reader, externalBuffer } from './reader.js'; import { ArrayStream, Writer } from './writer.js'; @@ -55,9 +54,6 @@ function concat(list) { if (typeof list[0] === 'string') { return list.join(''); } - if (NodeBuffer && NodeBuffer.isBuffer(list[0])) { - return NodeBuffer.concat(list); - } return concatUint8Array(list); } @@ -466,9 +462,8 @@ function slice(input, begin=0, end=Infinity) { if (input[externalBuffer]) { input = concat(input[externalBuffer].concat([input])); } - if (isUint8Array(input) && !(NodeBuffer && NodeBuffer.isBuffer(input))) { - if (end === Infinity) end = input.length; - return input.subarray(begin, end); + if (isUint8Array(input)) { + return input.subarray(begin, end === Infinity ? input.length : end); } return input.slice(begin, end); } diff --git a/lib/util.js b/lib/util.js index 6fbf9db..6a7e869 100644 --- a/lib/util.js +++ b/lib/util.js @@ -1,6 +1,6 @@ /* eslint-disable no-prototype-builtins */ import { isArrayStream } from './writer.js'; -import { NodeReadableStream } from './node-conversions/index.js' +import { Readable as NodeNativeReadableStream } from 'stream'; const isNode = typeof globalThis.process === 'object' && typeof globalThis.process.versions === 'object'; @@ -16,7 +16,7 @@ function isStream(input) { if (globalThis.ReadableStream && globalThis.ReadableStream.prototype.isPrototypeOf(input)) { return 'web'; } - if (NodeReadableStream && NodeReadableStream.prototype.isPrototypeOf(input)) { + if (NodeNativeReadableStream && NodeNativeReadableStream.prototype.isPrototypeOf(input)) { 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/package.json b/package.json index 35f48f4..a0cf14f 100644 --- a/package.json +++ b/package.json @@ -14,7 +14,6 @@ "types": "./lib/index.d.ts", "main": "./lib/index.js", "browser": { - "buffer": false, "stream": false }, "engines": { @@ -59,7 +58,7 @@ "scripts": { "test-type-definitions": "tsx test/typescript.test.ts && tsx --tsconfig test/tsconfig.es5.json test/typescript.test.ts", "test-browser": "karma start karma.conf.cjs", - "test-node": "mocha ./test/node.test.js", + "test-node": "mocha ./test/node.test.js ./test/common.test.js", "lint": "eslint lib test", "docs": "jsdoc --configure .jsdocrc.cjs --destination docs --readme README.md lib && printf '%s' 'web-stream-tools.openpgpjs.org' > docs/CNAME", "preversion": "rm -rf docs", diff --git a/test/browser.test.js b/test/browser.test.js index d50a487..5b348ae 100644 --- a/test/browser.test.js +++ b/test/browser.test.js @@ -1,10 +1,16 @@ import { expect } from 'chai'; -import { toStream, readToEnd } from '../lib/index.js'; +import { toStream, readToEnd } from '@openpgp/web-stream-tools'; describe('Browser integration tests', () => { - it('toStream/readToEnd', async () => { + it('accepts readable stream', async () => { const input = 'chunk'; - const streamedData = toStream('chunk'); + const stream = new ReadableStream({ + start(controller) { + controller.enqueue(input); + controller.close(); + } + }); + const streamedData = toStream(stream); expect(await readToEnd(streamedData)).to.equal(input); - }) + }); }) diff --git a/test/common.test.js b/test/common.test.js new file mode 100644 index 0000000..525a93b --- /dev/null +++ b/test/common.test.js @@ -0,0 +1,17 @@ +import { expect } from 'chai'; +import { toStream, readToEnd, slice } from '@openpgp/web-stream-tools'; + +describe('Common integration tests', () => { + it('toStream/readToEnd', async () => { + const input = 'chunk'; + const streamedData = toStream('chunk'); + expect(await readToEnd(streamedData)).to.equal(input); + }); + + it('slice', async () => { + const input = 'another chunk'; + const streamedData = toStream(input); + const slicedStream = slice(streamedData, 8); + expect(await readToEnd(slicedStream)).to.equal('chunk'); + }) +}) diff --git a/test/node.test.js b/test/node.test.js index f0e64aa..10e9498 100644 --- a/test/node.test.js +++ b/test/node.test.js @@ -1,29 +1,23 @@ import { expect } from 'chai'; -import { Readable } from 'stream'; +import { Readable as NodeNativeReadableStream } from 'stream'; import { ReadableStream as NodeWebReadableStream } from 'stream/web'; import { toStream, readToEnd } from '@openpgp/web-stream-tools'; describe('Node integration tests', () => { it('throws on node native stream', async () => { - const input = new Readable(); + const input = new NodeNativeReadableStream(); expect(() => toStream(input)).to.throw(/Native Node streams are no longer supported/); }); - it('accepts on node web stream', async () => { + it('accepts node web stream', async () => { const input = 'chunk'; const stream = new NodeWebReadableStream({ start(controller) { - controller.enqueue('chunk'); + controller.enqueue(input); controller.close(); } }); const streamedData = toStream(stream); expect(await readToEnd(streamedData)).to.equal(input); }); - - it('toStream/readToEnd', async () => { - const input = 'chunk'; - const streamedData = toStream('chunk'); - expect(await readToEnd(streamedData)).to.equal(input); - }) -}) +});