# size-stream

> Count the size of a stream in bytes

Latest version **1.0.1** (published 2015-10-26) · MIT license · 0 weekly downloads

## Install

```sh
npm install size-stream
pnpm add size-stream
yarn add size-stream
bun add size-stream
```

## Health

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

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.0.1 |
| Published | 2015-10-26 |
| First published | 2015-10-26 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 1 |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 5 |
| Maintainers | yoshuawuyts |
| Keywords | http, response, http-framework, stream, streaming, node, streams2, res |

## Links

- npm: https://www.npmjs.com/package/size-stream
- Repository: https://github.com/yoshuawuyts/size-stream
- Homepage: https://github.com/yoshuawuyts/size-stream#readme
- Issues: https://github.com/yoshuawuyts/size-stream/issues
- npm.io page: https://npm.io/package/size-stream

## Dependencies (1)

- [readable-stream](https://npm.io/package/readable-stream.md) ^2.0.3

## Alternatives

- [launchdarkly-js-client-sdk](https://npm.io/package/launchdarkly-js-client-sdk.md) — 2.5M weekly downloads
- [@elastic/elasticsearch](https://npm.io/package/@elastic/elasticsearch.md) — 2.1M weekly downloads
- [@c8y/client](https://npm.io/package/@c8y/client.md) — 15.3K weekly downloads
- [@signaldb/maverickjs](https://npm.io/package/@signaldb/maverickjs.md) — 1.7K weekly downloads
- [@bbc/http-transport-cache](https://npm.io/package/@bbc/http-transport-cache.md) — 1.2K weekly downloads

## Recent versions

- 1.0.1 (latest) — 2015-10-26
- 1.0.0 — 2015-10-26

## README

# size-stream [![stability][0]][1]
[![npm version][2]][3] [![build status][4]][5] [![test coverage][6]][7]
[![downloads][8]][9] [![js-standard-style][10]][11]

Count the size of a stream in bytes.

Node's `res` object doesn't expose knowledge about the response size that it's
sending. By counting the bytes passed to `res` it's now possible to retrieve
both the size (e.g. for logging) and properly set the `Content-Length` header.

## Installation
```sh
$ npm install size-stream
```

## Usage
```js
const httpNdjson = require('http-ndjson')
const sizeStream = require('size-stream')
const stdout = require('stdout-stream')
const pump = require('pump')
const http = require('http')

http.createServer((req, res) => {
  const httpLogger = httpNdjson(req, res)
  pump(httpLogger, stdout)

  const size = sizeStream()
  size.once('size', function (size) {
    httpLogger.setSize(size)
    res.setHeader('Content-Length', size)
  })

  pump(req, router(req, res), size, res)
}).listen()
```

## API
### size = sizeStream(res)
Create a `PassThrough` stream.

### size.on('size', cb(size))
Emits the total stream size in bytes when the stream ends.

## License
[MIT](https://tldrlegal.com/license/mit-license)

[0]: https://img.shields.io/badge/stability-experimental-orange.svg?style=flat-square
[1]: https://nodejs.org/api/documentation.html#documentation_stability_index
[2]: https://img.shields.io/npm/v/size-stream.svg?style=flat-square
[3]: https://npmjs.org/package/size-stream
[4]: https://img.shields.io/travis/yoshuawuyts/size-stream/master.svg?style=flat-square
[5]: https://travis-ci.org/yoshuawuyts/size-stream
[6]: https://img.shields.io/codecov/c/github/yoshuawuyts/size-stream/master.svg?style=flat-square
[7]: https://codecov.io/github/yoshuawuyts/size-stream
[8]: http://img.shields.io/npm/dm/size-stream.svg?style=flat-square
[9]: https://npmjs.org/package/size-stream
[10]: https://img.shields.io/badge/code%20style-standard-brightgreen.svg?style=flat-square
[11]: https://github.com/feross/standard

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