Sign Up for Free

RunKit +

Try any Node.js package right in your browser

This is a playground to test code. It runs a full Node.js environment and already has all of npm’s 400,000 packages pre-installed, including stream-head with all npm packages installed. Try it out:

var streamHead = require("stream-head")

This service is provided by RunKit and is not affiliated with npm, Inc or the package authors.

stream-head v1.1.1

Peek the first couple of bytes from a stream

npm version build status coverage status


This package is a Promise-based alternative to buffer-peek-stream, or a "POSIX head(1) for Node.js streams". It depends only on through2.

This package provides TypeScript types.


The default (and only) exported function takes a readable stream and returns a new stream (together with a buffer). The old stream must not be used anymore, it will be piped to the returned stream. The returned stream will contain everything from the input stream, the first n bytes will be copied to the returned buffer, not consumed.

import streamHead from 'stream-head'

inputStream; // We get this from somewhere

// Peek the first 64 bytes from the stream.
const { stream, head } = await streamHead( inputStream, { bytes: 64 } );

stream; // The new stream (don't use inputStream anymore!)
head; // A Buffer with the first 64 bytes (or less if the stream was smaller)


RunKit is a free, in-browser JavaScript dev environment for prototyping Node.js code, with every npm package installed. Sign up to share your code.
Sign Up for Free