Skip to content

Commit

Permalink
Require Node.js 18
Browse files Browse the repository at this point in the history
  • Loading branch information
sindresorhus committed Nov 1, 2023
1 parent b373f64 commit dbbc909
Show file tree
Hide file tree
Showing 7 changed files with 28 additions and 20 deletions.
7 changes: 4 additions & 3 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
9 changes: 5 additions & 4 deletions index.d.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import {
Duplex as DuplexStream,
DuplexOptions as DuplexStreamOption,
type DuplexOptions as DuplexStreamOption,
} from 'node:stream';

export interface Options extends Readonly<DuplexStreamOption> {
export type Options = {
/**
The number of bytes to buffer.
*/
readonly chunkSize: number;
}
} & Readonly<DuplexStreamOption>;

export type StopSymbol = typeof FirstChunkStream.stop;

Expand Down Expand Up @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
4 changes: 2 additions & 2 deletions index.test-d.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand All @@ -14,7 +14,7 @@ const firstChunkStream = new FirstChunkStream({chunkSize: 7}, async (chunk, enco
expectType<FirstChunkStream>(firstChunkStream);
expectType<DuplexStream>(firstChunkStream);

fs.createReadStream('unicorn.txt').pipe(firstChunkStream); // eslint-disable-line @typescript-eslint/no-unsafe-member-access
fs.createReadStream('unicorn.txt').pipe(firstChunkStream);

expectType<FirstChunkStream>(new FirstChunkStream({chunkSize: 7}, async () => FirstChunkStream.stop));
expectType<FirstChunkStream>(new FirstChunkStream({chunkSize: 7}, async () => ''));
Expand Down
13 changes: 9 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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"
}
}
}
7 changes: 4 additions & 3 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
## Install

```
$ npm install first-chunk-stream
```sh
npm install first-chunk-stream
```

## Usage
Expand All @@ -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);
Expand Down
6 changes: 3 additions & 3 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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},
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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},
Expand Down

0 comments on commit dbbc909

Please sign in to comment.