# first-chunk-stream

> Buffer and transform the n first bytes of a stream

Latest version **6.0.0** (published 2023-11-01) · MIT license · 0 weekly downloads

## Install

```sh
npm install first-chunk-stream
pnpm add first-chunk-stream
yarn add first-chunk-stream
bun add first-chunk-stream
```

## Health

**Score 30/100 (F)** — status: abandoned.

Positive: has types; esm support; no vulnerabilities; high quality score.

Warnings: low downloads.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 6.0.0 |
| Published | 2023-11-01 |
| First published | 2014-05-25 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | ESM |
| Node | >=18 |
| Dependencies | 1 |
| Unpacked size | 10.3 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 28 |
| Author | Sindre Sorhus |
| Maintainers | sindresorhus |
| Keywords | buffer, stream, streams, transform, first, chunk, size, minimum, bytes |

## Links

- npm: https://www.npmjs.com/package/first-chunk-stream
- Repository: https://github.com/sindresorhus/first-chunk-stream
- Homepage: https://github.com/sindresorhus/first-chunk-stream#readme
- Issues: https://github.com/sindresorhus/first-chunk-stream/issues
- Funding: https://github.com/sponsors/sindresorhus
- npm.io page: https://npm.io/package/first-chunk-stream

## Dependencies (1)

- [uint8array-extras](https://npm.io/package/uint8array-extras.md) ^0.5.0

## Alternatives

- [flatbuffers](https://npm.io/package/flatbuffers.md) — 6.0M weekly downloads
- [jwt-simple](https://npm.io/package/jwt-simple.md) — 259.5K weekly downloads
- [@exodus/patch-broken-hermes-typed-arrays](https://npm.io/package/@exodus/patch-broken-hermes-typed-arrays.md) — 28.5K weekly downloads
- [@native-to-anchor/buffer-layout](https://npm.io/package/@native-to-anchor/buffer-layout.md) — 12.2K weekly downloads
- [binary-parser-encoder](https://npm.io/package/binary-parser-encoder.md) — 5.3K weekly downloads

## Recent versions

- 6.0.0 (latest) — 2023-11-01
- 5.0.0 — 2021-08-15
- 4.0.0 — 2019-06-13
- 3.0.0 — 2019-05-12
- 2.0.0 — 2015-09-20
- 1.0.0 — 2014-08-14
- 0.1.0 — 2014-05-25

## README

# first-chunk-stream

> Buffer and transform the n first bytes of a stream

## Install

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

## Usage

```js
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 uint8ArrayToString(chunk).toUpperCase();
	}));

const data = await getStream(stream);

if (data.length < 7) {
	throw new Error('Couldn\'t get the minimum required first chunk length');
}

console.log(data);
//=> 'UNICORN rainbow'
```

## API

### FirstChunkStream(options, transform)

`FirstChunkStream` constructor.

#### transform(chunk, encoding)

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: `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.

Note that the buffer can have a smaller length than the required one. In that case, it will be due to the fact that the complete stream contents has a length less than the `options.chunkSize` value. You should check for this yourself if you strictly depend on the length.

```js
import FirstChunkStream from 'first-chunk-stream';

new FirstChunkStream({chunkSize: 7}, async (chunk, encoding) => {
	return chunk.toString(encoding).toUpperCase(); // Send string to stream
});

new FirstChunkStream({chunkSize: 7}, async (chunk, encoding) => {
	return chunk; // Send buffer to stream
});

new FirstChunkStream({chunkSize: 7}, async (chunk, encoding) => {
	return {
		buffer: chunk,
		encoding: encoding,
	}; // Send buffer with encoding to stream
});

new FirstChunkStream({chunkSize: 7}, async (chunk, encoding) => {
	return FirstChunkStream.stop; // End the stream early
});

new FirstChunkStream({chunkSize: 7}, async (chunk, encoding) => {
	throw new Error('Unconditional error'); // Emit stream error
});
```

#### options

Type: `object`

The options object is passed to the [`Duplex` stream](https://nodejs.org/api/stream.html#stream_class_stream_duplex) constructor allowing you to customize your stream behavior. In addition, you can specify the following option:

###### chunkSize

Type: `number`

The number of bytes to buffer.

---
_Source: https://npm.io/package/first-chunk-stream · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
