Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Upgrade xo #126

Merged
merged 1 commit into from
Mar 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion benchmarks/index.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
import {text, buffer, arrayBuffer} from 'node:stream/consumers';
import getStream, {getStreamAsBuffer, getStreamAsArrayBuffer, getStreamAsArray} from '../source/index.js';
import {createFixture, deleteFixture, FIXTURE_HUMAN_SIZE} from './fixture.js';
import {createNodeStreamBinary, createNodeStreamText, createWebStreamBinary, createWebStreamText} from './stream.js';
import {
createNodeStreamBinary,
createNodeStreamText,
createWebStreamBinary,
createWebStreamText,
} from './stream.js';
import {measureTask} from './measure.js';

const runBenchmarks = async () => {
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,6 @@
"precise-now": "^3.0.0",
"stream-json": "^1.8.0",
"tsd": "^0.29.0",
"xo": "^0.56.0"
"xo": "^0.58.0"
}
}
4 changes: 2 additions & 2 deletions source/array-buffer.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import {getStreamContents} from './contents.js';
import {noop, throwObjectStream, getLengthProp} from './utils.js';
import {noop, throwObjectStream, getLengthProperty} from './utils.js';

export async function getStreamAsArrayBuffer(stream, options) {
return getStreamContents(stream, arrayBufferMethods, options);
Expand Down Expand Up @@ -76,7 +76,7 @@ const arrayBufferMethods = {
typedArray: useUint8ArrayWithOffset,
others: throwObjectStream,
},
getSize: getLengthProp,
getSize: getLengthProperty,
truncateChunk: truncateArrayBufferChunk,
addChunk: addArrayBufferChunk,
getFinalChunk: noop,
Expand Down
4 changes: 2 additions & 2 deletions source/array.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import {getStreamContents} from './contents.js';
import {identity, noop, getContentsProp} from './utils.js';
import {identity, noop, getContentsProperty} from './utils.js';

export async function getStreamAsArray(stream, options) {
return getStreamContents(stream, arrayMethods, options);
Expand Down Expand Up @@ -28,5 +28,5 @@ const arrayMethods = {
truncateChunk: noop,
addChunk: addArrayChunk,
getFinalChunk: noop,
finalize: getContentsProp,
finalize: getContentsProperty,
};
1 change: 0 additions & 1 deletion source/buffer.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,4 @@ export async function getStreamAsBuffer(stream, options) {
}
}

// eslint-disable-next-line n/prefer-global/buffer
const arrayBufferToNodeBuffer = arrayBuffer => globalThis.Buffer.from(arrayBuffer);
29 changes: 25 additions & 4 deletions source/contents.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,25 @@ export const getStreamContents = async (stream, {init, convertChunk, getSize, tr
for await (const chunk of asyncIterable) {
const chunkType = getChunkType(chunk);
const convertedChunk = convertChunk[chunkType](chunk, state);
appendChunk({convertedChunk, state, getSize, truncateChunk, addChunk, maxBuffer});
appendChunk({
convertedChunk,
state,
getSize,
truncateChunk,
addChunk,
maxBuffer,
});
}

appendFinalChunk({state, convertChunk, getSize, truncateChunk, addChunk, getFinalChunk, maxBuffer});
appendFinalChunk({
state,
convertChunk,
getSize,
truncateChunk,
addChunk,
getFinalChunk,
maxBuffer,
});
return finalize(state);
} catch (error) {
const normalizedError = typeof error === 'object' && error !== null ? error : new Error(error);
Expand All @@ -25,7 +40,14 @@ export const getStreamContents = async (stream, {init, convertChunk, getSize, tr
const appendFinalChunk = ({state, getSize, truncateChunk, addChunk, getFinalChunk, maxBuffer}) => {
const convertedChunk = getFinalChunk(state);
if (convertedChunk !== undefined) {
appendChunk({convertedChunk, state, getSize, truncateChunk, addChunk, maxBuffer});
appendChunk({
convertedChunk,
state,
getSize,
truncateChunk,
addChunk,
maxBuffer,
});
}
};

Expand Down Expand Up @@ -63,7 +85,6 @@ const getChunkType = chunk => {
return 'others';
}

// eslint-disable-next-line n/prefer-global/buffer
if (globalThis.Buffer?.isBuffer(chunk)) {
return 'buffer';
}
Expand Down
2 changes: 2 additions & 0 deletions source/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ export class MaxBufferError extends Error {
constructor();
}

// eslint-disable-next-line @typescript-eslint/ban-types
type TextStreamItem = string | Buffer | ArrayBuffer | ArrayBufferView;
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We do want to use the Buffer type, not Uint8Array, here.

export type AnyStream<SteamItem = TextStreamItem> = Readable | ReadableStream<SteamItem> | AsyncIterable<SteamItem>;

Expand Down Expand Up @@ -86,6 +87,7 @@ const stream = fs.createReadStream('unicorn.png');
console.log(await getStreamAsBuffer(stream));
```
*/
// eslint-disable-next-line @typescript-eslint/ban-types
export function getStreamAsBuffer(stream: AnyStream, options?: Options): Promise<Buffer>;

/**
Expand Down
19 changes: 17 additions & 2 deletions source/index.test-d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,20 @@ import {Buffer} from 'node:buffer';
import {open} from 'node:fs/promises';
import {type Readable} from 'node:stream';
import fs from 'node:fs';
import {expectType, expectError, expectAssignable, expectNotAssignable} from 'tsd';
import getStream, {getStreamAsBuffer, getStreamAsArrayBuffer, getStreamAsArray, MaxBufferError, type Options, type AnyStream} from './index.js';
import {
expectType,
expectError,
expectAssignable,
expectNotAssignable,
} from 'tsd';
import getStream, {
getStreamAsBuffer,
getStreamAsArrayBuffer,
getStreamAsArray,
MaxBufferError,
type Options,
type AnyStream,
} from './index.js';

const nodeStream = fs.createReadStream('foo') as Readable;

Expand Down Expand Up @@ -35,6 +47,7 @@ expectError(await getStream(nodeStream, {maxBuffer: '10'}));
expectError(await getStream(nodeStream, {unknownOption: 10}));
expectError(await getStream(nodeStream, {maxBuffer: 10}, {}));

/* eslint-disable @typescript-eslint/ban-types */
expectType<Buffer>(await getStreamAsBuffer(nodeStream));
expectType<Buffer>(await getStreamAsBuffer(nodeStream, {maxBuffer: 10}));
expectType<Buffer>(await getStreamAsBuffer(readableStream));
Expand All @@ -43,6 +56,7 @@ expectType<Buffer>(await getStreamAsBuffer(bufferAsyncIterable));
expectType<Buffer>(await getStreamAsBuffer(arrayBufferAsyncIterable));
expectType<Buffer>(await getStreamAsBuffer(dataViewAsyncIterable));
expectType<Buffer>(await getStreamAsBuffer(typedArrayAsyncIterable));
/* eslint-enable @typescript-eslint/ban-types */
expectError(await getStreamAsBuffer(objectAsyncIterable));
expectError(await getStreamAsBuffer({}));
expectError(await getStreamAsBuffer(nodeStream, {maxBuffer: '10'}));
Expand All @@ -68,6 +82,7 @@ expectType<any[]>(await getStreamAsArray(nodeStream, {maxBuffer: 10}));
expectType<any[]>(await getStreamAsArray(readableStream));
expectType<Uint8Array[]>(await getStreamAsArray(readableStream as ReadableStream<Uint8Array>));
expectType<string[]>(await getStreamAsArray(stringAsyncIterable));
// eslint-disable-next-line @typescript-eslint/ban-types
expectType<Buffer[]>(await getStreamAsArray(bufferAsyncIterable));
expectType<ArrayBuffer[]>(await getStreamAsArray(arrayBufferAsyncIterable));
expectType<DataView[]>(await getStreamAsArray(dataViewAsyncIterable));
Expand Down
7 changes: 6 additions & 1 deletion source/stream.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,12 @@ const getStreamIterable = async function * (stream) {

const handleStreamEnd = async (stream, controller, state) => {
try {
await nodeImports.finished(stream, {cleanup: true, readable: true, writable: false, error: false});
await nodeImports.finished(stream, {
cleanup: true,
readable: true,
writable: false,
error: false,
});
} catch (error) {
state.error = error;
} finally {
Expand Down
11 changes: 8 additions & 3 deletions source/string.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
import {getStreamContents} from './contents.js';
import {identity, getContentsProp, throwObjectStream, getLengthProp} from './utils.js';
import {
identity,
getContentsProperty,
throwObjectStream,
getLengthProperty,
} from './utils.js';

export async function getStreamAsString(stream, options) {
return getStreamContents(stream, stringMethods, options);
Expand Down Expand Up @@ -28,9 +33,9 @@ const stringMethods = {
typedArray: useTextDecoder,
others: throwObjectStream,
},
getSize: getLengthProp,
getSize: getLengthProperty,
truncateChunk: truncateStringChunk,
addChunk: addStringChunk,
getFinalChunk: getFinalStringChunk,
finalize: getContentsProp,
finalize: getContentsProperty,
};
4 changes: 2 additions & 2 deletions source/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@ export const identity = value => value;

export const noop = () => undefined;

export const getContentsProp = ({contents}) => contents;
export const getContentsProperty = ({contents}) => contents;

export const throwObjectStream = chunk => {
throw new Error(`Streams in object mode are not supported: ${String(chunk)}`);
};

export const getLengthProp = convertedChunk => convertedChunk.length;
export const getLengthProperty = convertedChunk => convertedChunk.length;
2 changes: 1 addition & 1 deletion test/array-buffer.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ const fixtureMultibyteUint16Array = new Uint16Array([0, 0]);
const longMultibyteUint16Array = new Uint16Array([0, 0, 0]);
const bigArrayBuffer = new Uint8Array(bigArray).buffer;

const setupArrayBuffer = (streamDef, options) => getStreamAsArrayBuffer(createStream(streamDef), options);
const setupArrayBuffer = (streamDefinition, options) => getStreamAsArrayBuffer(createStream(streamDefinition), options);

const getStreamToArrayBuffer = async (t, fixtureValue) => {
const result = await setupArrayBuffer(fixtureValue);
Expand Down
2 changes: 1 addition & 1 deletion test/array.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ import {

const fixtureArray = [{}, {}];

const setupArray = (streamDef, options) => getStreamAsArray(createStream(streamDef), options);
const setupArray = (streamDefinition, options) => getStreamAsArray(createStream(streamDefinition), options);

const getStreamToArray = async (t, fixtureValue) => {
const result = await setupArray(fixtureValue);
Expand Down
4 changes: 1 addition & 3 deletions test/buffer.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ const fixtureMultibyteBuffer = Buffer.from(fixtureMultibyteString);
const longMultibyteBuffer = Buffer.from(longMultibyteString);
const bigBuffer = Buffer.from(bigArray);

const setupBuffer = (streamDef, options) => getStreamAsBuffer(createStream(streamDef), options);
const setupBuffer = (streamDefinition, options) => getStreamAsBuffer(createStream(streamDefinition), options);

const getStreamToBuffer = async (t, fixtureValue) => {
const result = await setupBuffer(fixtureValue);
Expand Down Expand Up @@ -104,7 +104,6 @@ test('getStreamAsBuffer() behaves like buffer()', async t => {
t.deepEqual(nativeResult, customResult);
});

/* eslint-disable n/prefer-global/buffer */
test('getStreamAsBuffer() only works in Node', async t => {
const {Buffer} = globalThis;
delete globalThis.Buffer;
Expand All @@ -114,4 +113,3 @@ test('getStreamAsBuffer() only works in Node', async t => {
globalThis.Buffer = Buffer;
}
});
/* eslint-enable n/prefer-global/buffer */
2 changes: 1 addition & 1 deletion test/contents.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import {
fixtureDataView,
} from './fixtures/index.js';

const setupString = (streamDef, options) => getStream(createStream(streamDef), options);
const setupString = (streamDefinition, options) => getStream(createStream(streamDefinition), options);

const generator = async function * () {
yield 'a';
Expand Down
6 changes: 3 additions & 3 deletions test/helpers/index.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import {Duplex, Readable} from 'node:stream';
import {finished} from 'node:stream/promises';

export const createStream = streamDef => typeof streamDef === 'function'
? Duplex.from(streamDef)
: Readable.from(streamDef);
export const createStream = streamDefinition => typeof streamDefinition === 'function'
? Duplex.from(streamDefinition)
: Readable.from(streamDefinition);

// @todo Use ReadableStream.from() after dropping support for Node 18
export const readableStreamFrom = chunks => new ReadableStream({
Expand Down
2 changes: 1 addition & 1 deletion test/integration.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import {fixtureString, fixtureBuffer, fixtureUtf16} from './fixtures/index.js';

const TEST_URL = 'https://nodejs.org/dist/index.json';

const createReadableStream = streamDef => Duplex.toWeb(Duplex.from(streamDef)).readable;
const createReadableStream = streamDefinition => Duplex.toWeb(Duplex.from(streamDefinition)).readable;

test('works with opendir()', async t => {
const directoryFiles = await opendir('.');
Expand Down
2 changes: 1 addition & 1 deletion test/string.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ const multiByteUint8Array = new TextEncoder().encode(multiByteString);
const multiByteBuffer = [...multiByteUint8Array].map(byte => Buffer.from([byte]));
const INVALID_UTF8_MARKER = '\uFFFD';

const setupString = (streamDef, options) => getStream(createStream(streamDef), options);
const setupString = (streamDefinition, options) => getStream(createStream(streamDefinition), options);

const getStreamToString = async (t, fixtureValue) => {
const result = await setupString(fixtureValue);
Expand Down