Skip to content

Commit

Permalink
Drop Node Buffer code
Browse files Browse the repository at this point in the history
Buffer is now a subtype of Uint8Array, so no special handling is required anymore
  • Loading branch information
larabr committed Jan 16, 2024
1 parent 2097f90 commit 415aada
Show file tree
Hide file tree
Showing 12 changed files with 44 additions and 44 deletions.
4 changes: 2 additions & 2 deletions karma.conf.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,15 @@ 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: [],

// 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: {},
Expand Down
2 changes: 0 additions & 2 deletions lib/node-conversions/browser-fallback.js

This file was deleted.

1 change: 0 additions & 1 deletion lib/node-conversions/index.js

This file was deleted.

4 changes: 0 additions & 4 deletions lib/node-conversions/node.js

This file was deleted.

9 changes: 0 additions & 9 deletions lib/node-conversions/package.json

This file was deleted.

5 changes: 5 additions & 0 deletions lib/reader.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');

/**
Expand Down
9 changes: 2 additions & 7 deletions lib/streams.js
Original file line number Diff line number Diff line change
@@ -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';

Expand Down Expand Up @@ -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);
}

Expand Down Expand Up @@ -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);
}
Expand Down
4 changes: 2 additions & 2 deletions lib/util.js
Original file line number Diff line number Diff line change
@@ -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';

Expand All @@ -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) {
Expand Down
3 changes: 1 addition & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
"types": "./lib/index.d.ts",
"main": "./lib/index.js",
"browser": {
"buffer": false,
"stream": false
},
"engines": {
Expand Down Expand Up @@ -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",
Expand Down
14 changes: 10 additions & 4 deletions test/browser.test.js
Original file line number Diff line number Diff line change
@@ -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);
})
});
})
17 changes: 17 additions & 0 deletions test/common.test.js
Original file line number Diff line number Diff line change
@@ -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');
})
})
16 changes: 5 additions & 11 deletions test/node.test.js
Original file line number Diff line number Diff line change
@@ -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);
})
})
});

0 comments on commit 415aada

Please sign in to comment.