# byte-counter

> Count bytes passing through a stream

Latest version **0.1.0** (published 2025-10-29) · MIT license · 0 weekly downloads

## Install

```sh
npm install byte-counter
pnpm add byte-counter
yarn add byte-counter
bun add byte-counter
```

## Health

**Score 60/100 (C)** — status: stable.

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

Warnings: low downloads; pre 1.0.

## Facts

| | |
|---|---|
| Version | 0.1.0 |
| Published | 2025-10-29 |
| First published | 2025-10-29 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | ESM |
| Node | >=20 |
| Dependencies | 0 |
| Unpacked size | 8.4 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 36 |
| Author | Sindre Sorhus |
| Maintainers | sindresorhus |
| Keywords | stream, transform, byte, count, counter, bytes, size, length, measure, throughput, passthrough, data, transfer, web, streams, whatwg |

## Links

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

## Alternatives

- [@libsql/sqlite3](https://npm.io/package/@libsql/sqlite3.md) — 39.8K weekly downloads
- [@fortemi/core](https://npm.io/package/@fortemi/core.md) — 461 weekly downloads
- [cdb-converter](https://npm.io/package/cdb-converter.md) — 341 weekly downloads
- [@uplo/adapter-prisma](https://npm.io/package/@uplo/adapter-prisma.md) — 75 weekly downloads
- [typeorm-aios](https://npm.io/package/typeorm-aios.md) — 30 weekly downloads

## Recent versions

- 0.1.0 (latest) — 2025-10-29

## README

# byte-counter

> Count bytes passing through a stream

A transform stream that counts bytes passing through without modifying the data. Useful for tracking data transfer size, monitoring progress, or validating `content-length` headers.

The main export uses [Web Streams](https://developer.mozilla.org/docs/Web/API/Streams_API). For Node.js streams, use the `/node` subexport.

## Install

```sh
npm install byte-counter
```

## Usage

### Web Streams (default)

```js
import ByteCounterStream from 'byte-counter';

const counter = new ByteCounterStream();
const response = await fetch('https://example.com/large-file.zip');

await response.body
	.pipeThrough(counter)
	.pipeTo(new WritableStream({
		write(chunk) {
			// Process chunk
		},
		close() {
			console.log(`Downloaded ${counter.count} bytes`);
		}
	}));
```

```js
import ByteCounterStream from 'byte-counter';

const counter = new ByteCounterStream();
const encoder = new TextEncoder();

const writer = counter.writable.getWriter();
await writer.write(encoder.encode('Hello '));
await writer.write(encoder.encode('World'));
await writer.close();

console.log(counter.count);
//=> 11
```

## API

### ByteCounterStream

A [`TransformStream`](https://developer.mozilla.org/en-US/docs/Web/API/TransformStream) that counts bytes passing through without modifying the data.

#### count

Type: `number` <sup>(read-only)</sup>

The number of bytes that have passed through the stream.

### byteLength(data)

Calculate the byte length of some data.

Strings are measured as UTF-8 bytes.

```js
import {byteLength} from 'byte-counter';

byteLength('Hello');
//=> 5

byteLength('Hello 👋');
//=> 10

byteLength(new Uint8Array([1, 2, 3]));
//=> 3
```

#### data

Type: `string | Uint8Array | ArrayBuffer | SharedArrayBuffer | ArrayBufferView`

The data to measure.

Returns: `number` - The byte length of the data.

### ByteCounterStream (`byte-counter/node`)

A Node.js [`Transform` stream](https://nodejs.org/api/stream.html#class-streamtransform) that counts bytes passing through without modifying the data.

```js
import fs from 'node:fs';
import ByteCounterStream from 'byte-counter/node';

const counter = new ByteCounterStream();

fs.createReadStream('file.txt')
	.pipe(counter)
	.pipe(fs.createWriteStream('output.txt'))
	.on('finish', () => {
		console.log(`Transferred ${counter.count} bytes`);
	});
```

```js
import ByteCounterStream from 'byte-counter/node';

const counter = new ByteCounterStream();
const encoder = new TextEncoder();

counter.write(encoder.encode('Hello '));
counter.write(encoder.encode('World'));
counter.end();

console.log(counter.count);
//=> 11
```

#### count

Type: `number` <sup>(read-only)</sup>

The number of bytes that have passed through the stream.

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