{"_id":"byte-counter","_rev":"4086770","name":"byte-counter","description":"Count bytes passing through a stream","dist-tags":{"latest":"0.1.0"},"maintainers":[{"name":"sindresorhus","email":""}],"time":{"modified":"2025-12-02T22:11:23.000Z","created":"2025-10-29T11:38:46.096Z","0.1.0":"2025-10-29T11:38:46.096Z"},"users":{},"author":{"name":"Sindre Sorhus","email":"sindresorhus@gmail.com","url":"https://sindresorhus.com"},"repository":{"type":"git","url":"git+https://github.com/sindresorhus/byte-counter.git"},"versions":{"0.1.0":{"name":"byte-counter","version":"0.1.0","description":"Count bytes passing through a stream","license":"MIT","repository":{"type":"git","url":"git+https://github.com/sindresorhus/byte-counter.git"},"funding":"https://github.com/sponsors/sindresorhus","author":{"name":"Sindre Sorhus","email":"sindresorhus@gmail.com","url":"https://sindresorhus.com"},"type":"module","exports":{".":{"types":"./index.d.ts","default":"./index.js"},"./node":{"types":"./node.d.ts","default":"./node.js"}},"sideEffects":false,"engines":{"node":">=20"},"scripts":{"test":"xo && node --test"},"keywords":["stream","transform","byte","count","counter","bytes","size","length","measure","throughput","passthrough","data","transfer","web","streams","whatwg"],"devDependencies":{"@types/node":"^24.9.2","eslint-import-resolver-node":"^0.3.9","xo":"^1.2.2"},"xo":{"settings":{"import-x/resolver":{"node":{"extensions":[".js",".d.ts"]}}}},"gitHead":"03cf8b83469b378a1281421a18057e2e051854c1","types":"./index.d.ts","_id":"byte-counter@0.1.0","bugs":{"url":"https://github.com/sindresorhus/byte-counter/issues"},"homepage":"https://github.com/sindresorhus/byte-counter#readme","_nodeVersion":"24.9.0","_npmVersion":"11.6.1","dist":{"shasum":"c49760b5790e50e942a0d57a57b3fc0e94488dcc","size":2926,"noattachment":false,"key":"/byte-counter/-/byte-counter-0.1.0.tgz","tarball":"http://registry.cnpm.dingdandao.com/byte-counter/download/byte-counter-0.1.0.tgz"},"_npmUser":{"name":"sindresorhus","email":"sindresorhus@gmail.com"},"directories":{},"maintainers":[{"name":"sindresorhus","email":""}],"_npmOperationalInternal":{"host":"s3://npm-registry-packages-npm-production","tmp":"tmp/byte-counter_0.1.0_1761737925882_0.6359334462956656"},"_hasShrinkwrap":false,"_cnpmcore_publish_time":"2025-10-29T11:38:46.096Z","publish_time":1761737926096,"_source_registry_name":"default","_cnpm_publish_time":1761737926096}},"readme":"# byte-counter\n\n> Count bytes passing through a stream\n\nA transform stream that counts bytes passing through without modifying the data. Useful for tracking data transfer size, monitoring progress, or validating `content-length` headers.\n\nThe main export uses [Web Streams](https://developer.mozilla.org/docs/Web/API/Streams_API). For Node.js streams, use the `/node` subexport.\n\n## Install\n\n```sh\nnpm install byte-counter\n```\n\n## Usage\n\n### Web Streams (default)\n\n```js\nimport ByteCounterStream from 'byte-counter';\n\nconst counter = new ByteCounterStream();\nconst response = await fetch('https://example.com/large-file.zip');\n\nawait response.body\n\t.pipeThrough(counter)\n\t.pipeTo(new WritableStream({\n\t\twrite(chunk) {\n\t\t\t// Process chunk\n\t\t},\n\t\tclose() {\n\t\t\tconsole.log(`Downloaded ${counter.count} bytes`);\n\t\t}\n\t}));\n```\n\n```js\nimport ByteCounterStream from 'byte-counter';\n\nconst counter = new ByteCounterStream();\nconst encoder = new TextEncoder();\n\nconst writer = counter.writable.getWriter();\nawait writer.write(encoder.encode('Hello '));\nawait writer.write(encoder.encode('World'));\nawait writer.close();\n\nconsole.log(counter.count);\n//=> 11\n```\n\n## API\n\n### ByteCounterStream\n\nA [`TransformStream`](https://developer.mozilla.org/en-US/docs/Web/API/TransformStream) that counts bytes passing through without modifying the data.\n\n#### count\n\nType: `number` <sup>(read-only)</sup>\n\nThe number of bytes that have passed through the stream.\n\n### byteLength(data)\n\nCalculate the byte length of some data.\n\nStrings are measured as UTF-8 bytes.\n\n```js\nimport {byteLength} from 'byte-counter';\n\nbyteLength('Hello');\n//=> 5\n\nbyteLength('Hello ????');\n//=> 10\n\nbyteLength(new Uint8Array([1, 2, 3]));\n//=> 3\n```\n\n#### data\n\nType: `string | Uint8Array | ArrayBuffer | SharedArrayBuffer | ArrayBufferView`\n\nThe data to measure.\n\nReturns: `number` - The byte length of the data.\n\n### ByteCounterStream (`byte-counter/node`)\n\nA Node.js [`Transform` stream](https://nodejs.org/api/stream.html#class-streamtransform) that counts bytes passing through without modifying the data.\n\n```js\nimport fs from 'node:fs';\nimport ByteCounterStream from 'byte-counter/node';\n\nconst counter = new ByteCounterStream();\n\nfs.createReadStream('file.txt')\n\t.pipe(counter)\n\t.pipe(fs.createWriteStream('output.txt'))\n\t.on('finish', () => {\n\t\tconsole.log(`Transferred ${counter.count} bytes`);\n\t});\n```\n\n```js\nimport ByteCounterStream from 'byte-counter/node';\n\nconst counter = new ByteCounterStream();\nconst encoder = new TextEncoder();\n\ncounter.write(encoder.encode('Hello '));\ncounter.write(encoder.encode('World'));\ncounter.end();\n\nconsole.log(counter.count);\n//=> 11\n```\n\n#### count\n\nType: `number` <sup>(read-only)</sup>\n\nThe number of bytes that have passed through the stream.\n","_attachments":{},"homepage":"https://github.com/sindresorhus/byte-counter#readme","bugs":{"url":"https://github.com/sindresorhus/byte-counter/issues"},"license":"MIT"}