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

Migrate to Uint8Array #10

Merged
merged 5 commits into from
Nov 1, 2023
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
5 changes: 2 additions & 3 deletions index.d.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import {Buffer} from 'node:buffer';
import {
Duplex as DuplexStream,
DuplexOptions as DuplexStreamOption,
Expand All @@ -13,9 +12,9 @@ export interface Options extends Readonly<DuplexStreamOption> {

export type StopSymbol = typeof FirstChunkStream.stop;

export type BufferLike = string | Buffer | Uint8Array;
export type BufferLike = string | Uint8Array;

export type TransformFunction = (chunk: Buffer, encoding: string) => Promise<StopSymbol | BufferLike | {buffer: BufferLike; encoding?: string}>;
export type TransformFunction = (chunk: Uint8Array, encoding: string) => Promise<StopSymbol | BufferLike | {buffer: BufferLike; encoding?: string}>;

export default class FirstChunkStream extends DuplexStream {
/**
Expand Down
8 changes: 4 additions & 4 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import {Buffer} from 'node:buffer';
import {Duplex as DuplexStream} from 'node:stream';
import {concatUint8Arrays, isUint8Array} from 'uint8array-extras';

const stop = Symbol('FirstChunkStream.stop');

Expand Down Expand Up @@ -49,7 +49,7 @@ export default class FirstChunkStream extends DuplexStream {

if (result === stop) {
state.manager.programPush(null, undefined, done);
} else if (Buffer.isBuffer(result) || (result instanceof Uint8Array) || (typeof result === 'string')) {
} else if (isUint8Array(result) || (typeof result === 'string')) {
state.manager.programPush(result, undefined, done);
} else {
state.manager.programPush(result.buffer, result.encoding, done);
Expand All @@ -71,7 +71,7 @@ export default class FirstChunkStream extends DuplexStream {
chunk = chunk.slice(options.chunkSize - state.size);
state.size += state.chunks[state.chunks.length - 1].length;

processCallback(Buffer.concat(state.chunks, state.size), state.encoding, () => {
processCallback(concatUint8Arrays(state.chunks, state.size), state.encoding, () => {
if (chunk.length === 0) {
done();
return;
Expand All @@ -84,7 +84,7 @@ export default class FirstChunkStream extends DuplexStream {

this.on('finish', () => {
if (!state.isSent) {
return processCallback(Buffer.concat(state.chunks, state.size), state.encoding, () => {
return processCallback(concatUint8Arrays(state.chunks, state.size), state.encoding, () => {
state.manager.programPush(null, state.encoding);
});
}
Expand Down
8 changes: 4 additions & 4 deletions index.test-d.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import fs from 'node:fs';
import {Buffer} from 'node:buffer';
import {Duplex as DuplexStream} from 'node:stream';
import {expectType, expectError} from 'tsd';
import {stringToUint8Array} from 'uint8array-extras';
import FirstChunkStream from './index.js';

expectError(new FirstChunkStream({}, () => {})); // eslint-disable-line @typescript-eslint/no-empty-function

const firstChunkStream = new FirstChunkStream({chunkSize: 7}, async (chunk, encoding) => {
expectType<Buffer>(chunk);
expectType<Uint8Array>(chunk);
expectType<string>(encoding);
return '';
});
Expand All @@ -18,10 +18,10 @@ fs.createReadStream('unicorn.txt').pipe(firstChunkStream); // eslint-disable-lin

expectType<FirstChunkStream>(new FirstChunkStream({chunkSize: 7}, async () => FirstChunkStream.stop));
expectType<FirstChunkStream>(new FirstChunkStream({chunkSize: 7}, async () => ''));
expectType<FirstChunkStream>(new FirstChunkStream({chunkSize: 7}, async () => Buffer.from('')));
expectType<FirstChunkStream>(new FirstChunkStream({chunkSize: 7}, async () => stringToUint8Array('') as Uint8Array));
expectType<FirstChunkStream>(new FirstChunkStream({chunkSize: 7}, async () => 'string'));
expectType<FirstChunkStream>(new FirstChunkStream({chunkSize: 7}, async () => new Uint8Array(0)));
expectType<FirstChunkStream>(new FirstChunkStream({chunkSize: 7}, async () => ({buffer: Buffer.from('')})));
expectType<FirstChunkStream>(new FirstChunkStream({chunkSize: 7}, async () => ({buffer: stringToUint8Array('') as Uint8Array})));
expectType<FirstChunkStream>(new FirstChunkStream({chunkSize: 7}, async () => ({buffer: new Uint8Array(0)})));
expectType<FirstChunkStream>(new FirstChunkStream({chunkSize: 7}, async () => ({buffer: 'string'})));
expectType<FirstChunkStream>(new FirstChunkStream({chunkSize: 7}, async () => ({buffer: 'string', encoding: 'utf8'})));
3 changes: 3 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@
"minimum",
"bytes"
],
"dependencies": {
"uint8array-extras": "^0.5.0"
},
"devDependencies": {
"@types/node": "^16.6.1",
"ava": "^3.15.0",
Expand Down
2 changes: 1 addition & 1 deletion readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ Type: `Function`

Async function that receives the required `options.chunkSize` bytes.

Expected to return an buffer-like object or `string` or object of form {buffer: `Buffer`, encoding: `string`} to send to stream or `firstChunkStream.stop` to end stream right away.
Expected to return an buffer-like object or `string` or object of form {buffer: `Uint8Array`, encoding: `string`} to send to stream or `firstChunkStream.stop` to end stream right away.

An error thrown from this function will be emitted as stream errors.

Expand Down
46 changes: 23 additions & 23 deletions test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import {Buffer} from 'node:buffer';
import test from 'ava';
import streamtest from 'streamtest';
import {concatUint8Arrays, stringToUint8Array, uint8ArrayToString} from 'uint8array-extras';
import FirstChunkStream from './index.js';

const content = 'unicorn rainbows \ncake';
Expand Down Expand Up @@ -65,11 +65,11 @@ for (const version of streamtest.versions) {
}),
);

stream.write(Buffer.from(content.slice(0, 3)));
stream.write(stringToUint8Array(content.slice(0, 3)));
stream.emit('error', new Error('Hey!'));
stream.write(Buffer.from(content.slice(3, 7)));
stream.write(stringToUint8Array(content.slice(3, 7)));
stream.emit('error', new Error('Hey!'));
stream.write(Buffer.from(content.slice(7)));
stream.write(stringToUint8Array(content.slice(7)));
stream.end();
},
);
Expand Down Expand Up @@ -102,8 +102,8 @@ for (const version of streamtest.versions) {
}),
);

stream.write(Buffer.from(content.slice(0, 7)));
stream.write(Buffer.from(content.slice(7)));
stream.write(stringToUint8Array(content.slice(0, 7)));
stream.write(stringToUint8Array(content.slice(7)));
stream.end();
},
);
Expand All @@ -117,8 +117,8 @@ for (const version of streamtest.versions) {
new FirstChunkStream(
{chunkSize: 0},
async chunk => {
t.is(chunk.toString('utf8'), '');
return Buffer.from('popop');
t.is(uint8ArrayToString(chunk), '');
return stringToUint8Array('popop');
},
),
)
Expand Down Expand Up @@ -146,7 +146,7 @@ for (const version of streamtest.versions) {
new FirstChunkStream(
{chunkSize: 7},
async chunk => {
t.is(chunk.toString('utf8'), content.slice(0, 7));
t.is(uint8ArrayToString(chunk), content.slice(0, 7));
return chunk;
},
),
Expand Down Expand Up @@ -176,7 +176,7 @@ for (const version of streamtest.versions) {
new FirstChunkStream(
{chunkSize: 7},
async chunk => {
t.is(chunk.toString('utf8'), content.slice(0, 7));
t.is(uint8ArrayToString(chunk), content.slice(0, 7));
return chunk;
},
),
Expand Down Expand Up @@ -206,7 +206,7 @@ for (const version of streamtest.versions) {
new FirstChunkStream(
{chunkSize: 7},
async chunk => {
t.is(chunk.toString('utf8'), content.slice(0, 7));
t.is(uint8ArrayToString(chunk), content.slice(0, 7));
return chunk;
},
),
Expand Down Expand Up @@ -236,7 +236,7 @@ for (const version of streamtest.versions) {
new FirstChunkStream(
{chunkSize: 7},
async chunk => {
t.is(chunk.toString('utf8'), content.slice(0, 7));
t.is(uint8ArrayToString(chunk), content.slice(0, 7));

firstChunkStream.pipe(
streamtest[version].toText((error, text) => {
Expand Down Expand Up @@ -266,8 +266,8 @@ for (const version of streamtest.versions) {
new FirstChunkStream(
{chunkSize: 7},
async chunk => {
t.is(chunk.toString('utf8'), 'abc');
return Buffer.from('b');
t.is(uint8ArrayToString(chunk), 'abc');
return stringToUint8Array('b');
},
),
)
Expand Down Expand Up @@ -295,8 +295,8 @@ for (const version of streamtest.versions) {
new FirstChunkStream(
{chunkSize: 7},
async chunk => {
t.is(chunk.toString('utf8'), content.slice(0, 7));
return Buffer.alloc(0);
t.is(uint8ArrayToString(chunk), content.slice(0, 7));
return new Uint8Array(0);
},
),
)
Expand Down Expand Up @@ -325,8 +325,8 @@ for (const version of streamtest.versions) {
new FirstChunkStream(
{chunkSize: 7},
async chunk => {
t.is(chunk.toString('utf8'), content.slice(0, 7));
return {buffer: chunk.toString('utf8'), encoding: 'utf8'};
t.is(uint8ArrayToString(chunk), content.slice(0, 7));
return {buffer: uint8ArrayToString(chunk), encoding: 'utf8'};
},
),
)
Expand Down Expand Up @@ -355,7 +355,7 @@ for (const version of streamtest.versions) {
new FirstChunkStream(
{chunkSize: 7},
async chunk => {
t.is(chunk.toString('utf8'), content.slice(0, 7));
t.is(uint8ArrayToString(chunk), content.slice(0, 7));
return FirstChunkStream.stop;
},
),
Expand Down Expand Up @@ -385,8 +385,8 @@ for (const version of streamtest.versions) {
new FirstChunkStream(
{chunkSize: 7},
async chunk => {
t.is(chunk.toString('utf8'), content.slice(0, 7));
return Buffer.concat([chunk, Buffer.from('plop')]);
t.is(uint8ArrayToString(chunk), content.slice(0, 7));
return concatUint8Arrays([chunk, stringToUint8Array('plop')]);
},
),
)
Expand Down Expand Up @@ -415,8 +415,8 @@ for (const version of streamtest.versions) {
new FirstChunkStream(
{chunkSize: 7},
async chunk => {
t.is(chunk.toString('utf8'), content.slice(0, 7));
return Buffer.from('plop');
t.is(uint8ArrayToString(chunk), content.slice(0, 7));
return stringToUint8Array('plop');
},
),
)
Expand Down
Loading