diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index 441975c..346585c 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -10,10 +10,11 @@ jobs: fail-fast: false matrix: node-version: - - 16 + - 20 + - 18 steps: - - uses: actions/checkout@v2 - - uses: actions/setup-node@v2 + - uses: actions/checkout@v4 + - uses: actions/setup-node@v4 with: node-version: ${{ matrix.node-version }} - run: npm install diff --git a/index.d.ts b/index.d.ts index 3b3dcc0..6ae07ee 100644 --- a/index.d.ts +++ b/index.d.ts @@ -1,14 +1,14 @@ import { Duplex as DuplexStream, - DuplexOptions as DuplexStreamOption, + type DuplexOptions as DuplexStreamOption, } from 'node:stream'; -export interface Options extends Readonly { +export type Options = { /** The number of bytes to buffer. */ readonly chunkSize: number; -} +} & Readonly; export type StopSymbol = typeof FirstChunkStream.stop; @@ -44,11 +44,12 @@ export default class FirstChunkStream extends DuplexStream { import fs from 'node:fs'; import getStream from 'get-stream'; import FirstChunkStream from 'first-chunk-stream'; + import {uint8ArrayToString} from 'uint8array-extras'; // unicorn.txt => unicorn rainbow const stream = fs.createReadStream('unicorn.txt') .pipe(new FirstChunkStream({chunkSize: 7}, async (chunk, encoding) => { - return chunk.toString(encoding).toUpperCase(); + return uint8ArrayToString(chunk).toUpperCase(); })); const data = await getStream(stream); diff --git a/index.js b/index.js index b90b3a6..382b9a7 100644 --- a/index.js +++ b/index.js @@ -69,7 +69,7 @@ export default class FirstChunkStream extends DuplexStream { } else { state.chunks.push(chunk.slice(0, options.chunkSize - state.size)); chunk = chunk.slice(options.chunkSize - state.size); - state.size += state.chunks[state.chunks.length - 1].length; + state.size += state.chunks.at(-1).length; processCallback(concatUint8Arrays(state.chunks, state.size), state.encoding, () => { if (chunk.length === 0) { diff --git a/index.test-d.ts b/index.test-d.ts index eb53ce1..710a68e 100644 --- a/index.test-d.ts +++ b/index.test-d.ts @@ -1,5 +1,5 @@ import fs from 'node:fs'; -import {Duplex as DuplexStream} from 'node:stream'; +import {type Duplex as DuplexStream} from 'node:stream'; import {expectType, expectError} from 'tsd'; import {stringToUint8Array} from 'uint8array-extras'; import FirstChunkStream from './index.js'; @@ -14,7 +14,7 @@ const firstChunkStream = new FirstChunkStream({chunkSize: 7}, async (chunk, enco expectType(firstChunkStream); expectType(firstChunkStream); -fs.createReadStream('unicorn.txt').pipe(firstChunkStream); // eslint-disable-line @typescript-eslint/no-unsafe-member-access +fs.createReadStream('unicorn.txt').pipe(firstChunkStream); expectType(new FirstChunkStream({chunkSize: 7}, async () => FirstChunkStream.stop)); expectType(new FirstChunkStream({chunkSize: 7}, async () => '')); diff --git a/package.json b/package.json index 08c0203..45b69f1 100644 --- a/package.json +++ b/package.json @@ -13,7 +13,7 @@ "type": "module", "exports": "./index.js", "engines": { - "node": "^12.20.0 || ^14.13.1 || >=16.0.0" + "node": ">=18" }, "scripts": { "test": "xo && nyc ava && tsd" @@ -37,11 +37,16 @@ "uint8array-extras": "^0.5.0" }, "devDependencies": { - "@types/node": "^16.6.1", + "@types/node": "^20.8.10", "ava": "^3.15.0", "nyc": "^15.1.0", "streamtest": "^2.0.0", - "tsd": "^0.17.0", - "xo": "^0.44.0" + "tsd": "^0.29.0", + "xo": "^0.56.0" + }, + "xo": { + "rules": { + "ava/no-unknown-modifiers": "off" + } } } diff --git a/readme.md b/readme.md index 6180b5a..e79bd37 100644 --- a/readme.md +++ b/readme.md @@ -4,8 +4,8 @@ ## Install -``` -$ npm install first-chunk-stream +```sh +npm install first-chunk-stream ``` ## Usage @@ -14,11 +14,12 @@ $ npm install first-chunk-stream import fs from 'node:fs'; import getStream from 'get-stream'; import FirstChunkStream from 'first-chunk-stream'; +import {uint8ArrayToString} from 'uint8array-extras'; // unicorn.txt => unicorn rainbow const stream = fs.createReadStream('unicorn.txt') .pipe(new FirstChunkStream({chunkSize: 7}, async (chunk, encoding) => { - return chunk.toString(encoding).toUpperCase(); + return uint8ArrayToString(chunk).toUpperCase(); })); const data = await getStream(stream); diff --git a/test.js b/test.js index 65a32b0..617acd1 100644 --- a/test.js +++ b/test.js @@ -201,7 +201,7 @@ for (const version of streamtest.versions) { t.plan(2); streamtest[version] - .fromChunks(content.split('')) + .fromChunks([...content]) .pipe( new FirstChunkStream( {chunkSize: 7}, @@ -230,7 +230,7 @@ for (const version of streamtest.versions) { t => { t.plan(2); - const inputStream = streamtest[version].fromChunks(content.split('')); + const inputStream = streamtest[version].fromChunks([...content]); const firstChunkStream = inputStream.pipe( new FirstChunkStream( @@ -410,7 +410,7 @@ for (const version of streamtest.versions) { t.plan(2); streamtest[version] - .fromChunks(content.split('')) + .fromChunks([...content]) .pipe( new FirstChunkStream( {chunkSize: 7},