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 #130

Merged
merged 2 commits into from
Jul 21, 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
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.31.0",
"xo": "^0.58.0"
"xo": "^0.59.0"
}
}
1 change: 1 addition & 0 deletions source/array-buffer.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ const resizeArrayBuffer = (contents, length) => {
return contents;
}

// eslint-disable-next-line n/no-unsupported-features/es-syntax
const arrayBuffer = new ArrayBuffer(length, {maxByteLength: getNewContentsLength(length)});
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

ArrayBuffer resizing is not supported in Node 18.
However, we check for support before calling this function.

new Uint8Array(arrayBuffer).set(new Uint8Array(contents), 0);
return arrayBuffer;
Expand Down
1 change: 1 addition & 0 deletions source/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ export class MaxBufferError extends Error {

// eslint-disable-next-line @typescript-eslint/ban-types
type TextStreamItem = string | Buffer | ArrayBuffer | ArrayBufferView;
// eslint-disable-next-line n/no-unsupported-features/node-builtins
export type AnyStream<SteamItem = TextStreamItem> = Readable | ReadableStream<SteamItem> | AsyncIterable<SteamItem>;
Copy link
Collaborator Author

@ehmicky ehmicky Jul 21, 2024

Choose a reason for hiding this comment

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

ReadableStream is supported in Node 18 but is marked as experimental.
However, it is a web standard, and the Node.js implementation has been pretty stable for a few years now.
This is only used in types, not runtime.


export type Options = {
Expand Down
1 change: 1 addition & 0 deletions source/index.test-d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ expectError(await getStreamAsArrayBuffer(nodeStream, {maxBuffer: 10}, {}));
expectType<any[]>(await getStreamAsArray(nodeStream));
expectType<any[]>(await getStreamAsArray(nodeStream, {maxBuffer: 10}));
expectType<any[]>(await getStreamAsArray(readableStream));
// eslint-disable-next-line n/no-unsupported-features/node-builtins
expectType<Uint8Array[]>(await getStreamAsArray(readableStream as ReadableStream<Uint8Array>));
expectType<string[]>(await getStreamAsArray(stringAsyncIterable));
// eslint-disable-next-line @typescript-eslint/ban-types
Expand Down
4 changes: 2 additions & 2 deletions test/browser.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import {execFile} from 'node:child_process';
import {dirname} from 'node:path';
import path from 'node:path';
import {fileURLToPath} from 'node:url';
import {promisify} from 'node:util';
import test from 'ava';
import {fixtureString} from './fixtures/index.js';

const pExecFile = promisify(execFile);
const cwd = dirname(fileURLToPath(import.meta.url));
const cwd = path.dirname(fileURLToPath(import.meta.url));
const nodeStreamFixture = './fixtures/node-stream.js';
const webStreamFixture = './fixtures/web-stream.js';
const iterableFixture = './fixtures/iterable.js';
Expand Down
1 change: 1 addition & 0 deletions test/helpers/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {finished} from 'node:stream/promises';
export {fromAnyIterable as readableStreamFrom} from '@sec-ant/readable-stream/ponyfill';

export const createStream = streamDefinition => typeof streamDefinition === 'function'
// eslint-disable-next-line n/no-unsupported-features/node-builtins
? Duplex.from(streamDefinition)
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Duplex.from() is supported in Node 18 but is marked as experimental.
However, its implementation has barely changed in many years now. Also, this is only used in tests.

: Readable.from(streamDefinition);

Expand Down
3 changes: 3 additions & 0 deletions test/integration.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {fixtureString, fixtureBuffer, fixtureUtf16} from './fixtures/index.js';

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

// eslint-disable-next-line n/no-unsupported-features/node-builtins
const createReadableStream = streamDefinition => Duplex.toWeb(Duplex.from(streamDefinition)).readable;

test('works with opendir()', async t => {
Expand Down Expand Up @@ -63,13 +64,15 @@ if (!nodeVersion.startsWith('v16.')) {
test('works with readableWebStream({ type: "bytes" })', readableWebStream, 'bytes');

test('works with fetch()', async t => {
// eslint-disable-next-line n/no-unsupported-features/node-builtins
const {body} = await fetch(TEST_URL);
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

fetch() support is experimental in Node, but we're only using this in tests.

const result = await getStream(body);
const parsedResult = JSON.parse(result);
t.true(Array.isArray(parsedResult));
});

test('can use TextDecoderStream', async t => {
// eslint-disable-next-line n/no-unsupported-features/node-builtins
const textDecoderStream = new TextDecoderStream('utf-16le');
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Same comment as for fetch(), but with TextDecoderStream().

const result = await getStream(
createReadableStream(fixtureUtf16).pipeThrough(textDecoderStream),
Expand Down
1 change: 1 addition & 0 deletions test/stream.js
Original file line number Diff line number Diff line change
Expand Up @@ -274,6 +274,7 @@ const testMultipleReads = async (t, wait) => {
assertSuccess(t, stream, Readable);
};

// eslint-disable-next-line n/no-unsupported-features/node-builtins
test('Handles multiple successive fast reads', testMultipleReads, () => scheduler.yield());
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Same comment as fetch(), but for scheduler.yield().

test('Handles multiple successive slow reads', testMultipleReads, () => pSetTimeout(100));

Expand Down
4 changes: 4 additions & 0 deletions test/web-stream-ponyfill.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
import test from 'ava';

// Emulate browsers that do not support those methods
// eslint-disable-next-line n/no-unsupported-features/node-builtins
delete ReadableStream.prototype.values;
// eslint-disable-next-line n/no-unsupported-features/node-builtins
delete ReadableStream.prototype[Symbol.asyncIterator];

// Run those tests, but emulating browsers
await import('./web-stream.js');

test('Should not polyfill ReadableStream', t => {
// eslint-disable-next-line n/no-unsupported-features/node-builtins
t.is(ReadableStream.prototype.values, undefined);
// eslint-disable-next-line n/no-unsupported-features/node-builtins
t.is(ReadableStream.prototype[Symbol.asyncIterator], undefined);
});
3 changes: 3 additions & 0 deletions test/web-stream.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ test('Can use already ended ReadableStream', async t => {

test('Can use already canceled ReadableStream', async t => {
let canceledValue;
// eslint-disable-next-line n/no-unsupported-features/node-builtins
const stream = new ReadableStream({
cancel(canceledError) {
canceledValue = canceledError;
Expand All @@ -32,6 +33,7 @@ test('Can use already canceled ReadableStream', async t => {

test('Can use already errored ReadableStream', async t => {
const error = new Error('test');
// eslint-disable-next-line n/no-unsupported-features/node-builtins
const stream = new ReadableStream({
start(controller) {
controller.error(error);
Expand All @@ -43,6 +45,7 @@ test('Can use already errored ReadableStream', async t => {

test('Cancel ReadableStream when maxBuffer is hit', async t => {
let canceled = false;
// eslint-disable-next-line n/no-unsupported-features/node-builtins
const stream = new ReadableStream({
start(controller) {
controller.enqueue(fixtureString);
Expand Down
Loading