New Research: Supply Chain Attack on Axios Pulls Malicious Dependency from npm.Details →
Socket
Book a DemoSign in
Socket

byte-counter

Package Overview
Dependencies
Maintainers
1
Versions
1
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

byte-counter

Count bytes passing through a stream

latest
Source
npmnpm
Version
0.1.0
Version published
Weekly downloads
1.4M
-2.21%
Maintainers
1
Weekly downloads
 
Created
Source

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. For Node.js streams, use the /node subexport.

Install

npm install byte-counter

Usage

Web Streams (default)

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`);
		}
	}));
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 that counts bytes passing through without modifying the data.

count

Type: number (read-only)

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.

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 that counts bytes passing through without modifying the data.

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`);
	});
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 (read-only)

The number of bytes that have passed through the stream.

Keywords

stream

FAQs

Package last updated on 29 Oct 2025

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts