Socket
Book a DemoInstallSign in
Socket

size-stream

Package Overview
Dependencies
Maintainers
1
Versions
2
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

size-stream

Count the size of a stream in bytes

latest
Source
npmnpm
Version
1.0.1
Version published
Maintainers
1
Created
Source

size-stream stability

npm version build status test coverage downloads js-standard-style

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

$ npm install size-stream

Usage

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

Keywords

http

FAQs

Package last updated on 26 Oct 2015

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