diff --git a/index.d.ts b/index.d.ts index f97eb00..3b3dcc0 100644 --- a/index.d.ts +++ b/index.d.ts @@ -1,4 +1,3 @@ -import {Buffer} from 'node:buffer'; import { Duplex as DuplexStream, DuplexOptions as DuplexStreamOption, @@ -13,9 +12,9 @@ export interface Options extends Readonly { 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; +export type TransformFunction = (chunk: Uint8Array, encoding: string) => Promise; export default class FirstChunkStream extends DuplexStream { /** diff --git a/index.js b/index.js index 06eea3b..b90b3a6 100644 --- a/index.js +++ b/index.js @@ -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'); @@ -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); @@ -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; @@ -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); }); } diff --git a/index.test-d.ts b/index.test-d.ts index 0c93709..eb53ce1 100644 --- a/index.test-d.ts +++ b/index.test-d.ts @@ -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(chunk); + expectType(chunk); expectType(encoding); return ''; }); @@ -18,10 +18,10 @@ fs.createReadStream('unicorn.txt').pipe(firstChunkStream); // eslint-disable-lin expectType(new FirstChunkStream({chunkSize: 7}, async () => FirstChunkStream.stop)); expectType(new FirstChunkStream({chunkSize: 7}, async () => '')); -expectType(new FirstChunkStream({chunkSize: 7}, async () => Buffer.from(''))); +expectType(new FirstChunkStream({chunkSize: 7}, async () => stringToUint8Array('') as Uint8Array)); expectType(new FirstChunkStream({chunkSize: 7}, async () => 'string')); expectType(new FirstChunkStream({chunkSize: 7}, async () => new Uint8Array(0))); -expectType(new FirstChunkStream({chunkSize: 7}, async () => ({buffer: Buffer.from('')}))); +expectType(new FirstChunkStream({chunkSize: 7}, async () => ({buffer: stringToUint8Array('') as Uint8Array}))); expectType(new FirstChunkStream({chunkSize: 7}, async () => ({buffer: new Uint8Array(0)}))); expectType(new FirstChunkStream({chunkSize: 7}, async () => ({buffer: 'string'}))); expectType(new FirstChunkStream({chunkSize: 7}, async () => ({buffer: 'string', encoding: 'utf8'}))); diff --git a/package.json b/package.json index 9ebb39d..08c0203 100644 --- a/package.json +++ b/package.json @@ -33,6 +33,9 @@ "minimum", "bytes" ], + "dependencies": { + "uint8array-extras": "^0.5.0" + }, "devDependencies": { "@types/node": "^16.6.1", "ava": "^3.15.0", diff --git a/readme.md b/readme.md index 49d1ba5..6180b5a 100644 --- a/readme.md +++ b/readme.md @@ -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. diff --git a/test.js b/test.js index 2cdcac0..65a32b0 100644 --- a/test.js +++ b/test.js @@ -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'; @@ -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(); }, ); @@ -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(); }, ); @@ -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'); }, ), ) @@ -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; }, ), @@ -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; }, ), @@ -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; }, ), @@ -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) => { @@ -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'); }, ), ) @@ -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); }, ), ) @@ -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'}; }, ), ) @@ -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; }, ), @@ -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')]); }, ), ) @@ -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'); }, ), )