Skip to content
Peek the first couple of bytes from a stream
Branch: master
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
dist-es5
dist-es6
scripts
test
.gitignore
.npmignore
.travis.yml
README.md
es5.js
es6.js
index.ts
package.json
tsconfig-base.json
tsconfig-es5.json
tsconfig-es6.json

README.md

npm version build status coverage status

stream-head

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.

API

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)
You can’t perform that action at this time.