npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

serve-buffer

v3.0.3

Published

Serve a Buffer via HTTP, with Range and conditional-GET support.

Downloads

108

Readme

serve-buffer

Serve a Buffer via HTTP, with Range, conditional GET and GZip/Brotli compression support.

npm version build status ISC-licensed minimum Node.js version support me via GitHub Sponsors chat with me on Twitter

Note: If you want to serve files with support for Range, conditional GET and compression, use send. If you want to serve an entire directory of files, use serve-static.

There is a surprising number of difficult-to-understand corner cases in the HTTP RFCs. I tried my best here, so that others don't have to write quick-and-dirty (which in the HTTP realm usually means slightly wrong) implementations. This library supports the following request headers:

Installation

npm install serve-buffer

Usage

const express = require('express')
const serveBuffer = require('serve-buffer')

const app = express()

let data = Buffer.from('a lot of data here…', 'utf8')
app.use('/data', (req, res) => {
	serveBuffer(req, res, data)
})

// change buffer later
data = Buffer.from('entirely different buffer', 'utf8')

allow caching via timeModified & etag

const computeEtag = require('etag')

let data = Buffer.from('a lot of data here…', 'utf8')
let timeModified = new Date()
let etag = computeEtag(data)

app.use('/data', (req, res) => {
	serveBuffer(req, res, data, {timeModified, etag})
})

// change buffer later
data = Buffer.from('entirely different buffer', 'utf8')
timeModified = new Date()
etag = computeEtag(data)

serve gzipped & Brotli-compressed data

Serving compressed data reduces the amount of transferred data at the cost of higher CPU load, so it is usually worth it if your data rarely changes, or if you have slowly connected (or a lot of) consumers.

If buf is reasonably small (<=10mb for GZip, <= 512kb for Brotli), serve-buffer will compress it by default. If you don't want this, pass opt.gzip: false and/or opt.brotliCompress: false; Instead, you can also customise the size limits via opt.gzipMaxSize & opt.brotliCompressMaxSize.

If you never mutate the buffer(s) that you pass into serveBuffer, you can tell it to cache each buffer's compressed version as long as the instance exists (using a WeakMap) by passing opt.unmutatedBuffers: true:

const data = Buffer.from('a lot of data here…', 'utf8')
const timeModified = new Date()
const etag = computeEtag(data)

app.use('/data', (req, res) => {
	serveBuffer(req, res, data, {
		timeModified,
		etag,
		// Only do this if you never mutate `data`!
		unmutatedBuffers: true,
	})
})

API

serveBuffer(req, res, buf, opt = {}, cb = () => {})

opt overrides the default config, which looks like this:

{
	contentType: 'application/octet-stream',
	timeModified: new Date(),
	etag: require('etag')(buf),

	gzip: true, // or `false` or `async (buf) => ({compressedBuffer, compressedEtag})`
	gzipMaxSize: 10 * 1024 * 1024, // 10mb
	brotliCompress: true, // or `false` or `async (buf) => ({compressedBuffer, compressedEtag})`
	brotliCompressMaxSize: 512 * 1024, // 512kb
	// Assume that Buffers passed in as `buf` never get mutated? If `true`, each compressed buffer & compressed ETag will be cached as long as the buffer instance exists.
	unmutatedBuffers: false,

	cacheControl: true, // send cache-control header?
	maxAge: 0, // for cache-control, in milliseconds
	immutable: false, // for cache-control

	// hook functions for modifying serve-buffer's behavior
	beforeSend: (req, res, body, opt) => {},
}

cb will be called once the response headers and body (if applicable) have been sent.

Related

  • send-stream – Streaming file server with Range and conditional-GET support from file system or other streaming sources. (Very similar to serve-buffer.)
  • send – Streaming static file server with Range and conditional-GET support
  • http-file-response – Send a file back as a HTTP response with support for range queries etc.

Contributing

If you have a question or need support using serve-buffer, please double-check your code and setup first. If you think you have found a bug or want to propose a feature, use the issues page.