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 1 commit
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 index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ export type StopSymbol = typeof FirstChunkStream.stop;

export type BufferLike = string | Buffer | Uint8Array;
kalwabed marked this conversation as resolved.
Show resolved Hide resolved

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) || (result instanceof Uint8Array) || (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'})));
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
"nyc": "^15.1.0",
"streamtest": "^2.0.0",
"tsd": "^0.17.0",
"uint8array-extras": "^0.5.0",
kalwabed marked this conversation as resolved.
Show resolved Hide resolved
"xo": "^0.44.0"
}
}
43 changes: 22 additions & 21 deletions test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import {Buffer} from 'node:buffer';
kalwabed marked this conversation as resolved.
Show resolved Hide resolved
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 +66,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 +103,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 +118,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 +147,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 +177,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 +207,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 +237,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 +267,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,7 +296,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 Buffer.alloc(0);
},
),
Expand Down Expand Up @@ -325,8 +326,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 +356,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 +386,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 +416,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