# frame-stream

> Length-prefixed message framing for Node.js streams.

Latest version **5.0.1** (published 2025-05-05) · MIT license · 0 weekly downloads

## Install

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

## Health

**Score 45/100 (D)** — status: stable.

Positive: has types; no vulnerabilities; high quality score.

Warnings: low downloads; no esm support.

Negative: stale.

## Facts

| | |
|---|---|
| Version | 5.0.1 |
| Published | 2025-05-05 |
| First published | 2014-05-27 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | CommonJS |
| Node | >=18 |
| Dependencies | 0 |
| Unpacked size | 12.4 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 26 |
| Author | Markus Ast |
| Maintainers | rkusa, davedoesdev |
| Keywords | socket, tcp, framing, streams |

## Links

- npm: https://www.npmjs.com/package/frame-stream
- Repository: https://github.com/davedoesdev/frame-stream
- Issues: https://github.com/davedoesdev/frame-stream/issues
- npm.io page: https://npm.io/package/frame-stream

## Alternatives

- [@opentelemetry/exporter-zipkin](https://npm.io/package/@opentelemetry/exporter-zipkin.md) — 14.8M weekly downloads
- [pusher-js](https://npm.io/package/pusher-js.md) — 2.0M weekly downloads
- [browserify](https://npm.io/package/browserify.md) — 1.7M weekly downloads
- [sqs-consumer](https://npm.io/package/sqs-consumer.md) — 1.7M weekly downloads
- [@sanity/eventsource](https://npm.io/package/@sanity/eventsource.md) — 930.8K weekly downloads

## Recent versions

- 5.0.1 (latest) — 2025-05-05
- 5.0.0 — 2024-10-25
- 4.0.1 — 2024-01-23
- 4.0.0 — 2023-04-15
- 3.0.1 — 2022-05-31
- 3.0.0 — 2021-11-01
- 2.1.0 — 2021-06-27
- 2.0.3 — 2020-08-07
- 2.0.1 — 2020-07-22
- 2.0.0 — 2020-01-15
- 1.1.1 — 2015-06-24
- 1.1.0 — 2015-06-24
- 1.0.0 — 2015-02-04
- 0.3.1 — 2014-08-25
- 0.3.0 — 2014-08-16
- … 3 more at https://npm.io/package/frame-stream/versions

## README

# frame-stream

Length-prefixed message framing for Node.js streams.

[![NPM][npm]](https://npmjs.com/package/frame-stream) [![CI status][ci]](https://github.com/davedoesdev/frame-stream/actions)

Some protocols, e.g. TCP, do not not guarantee to keep message boundaries. One common approach to distinguish such messages is *Length Prefixing*, which prepends each message with its length. `frame-stream` accepts a stream with such length-prefixed messages and returns each frame on its own.

## Usage

Simply pipe an incoming (e.g. TCP) stream into `require('frame-stream').decode()` and pipe `require('frame-stream').encode()` into an outgoing stream:

```js
var net = require('net')
var frame = require('frame-stream')
var port = 30000

net.createServer(function(socket) {
  socket.pipe(frame.decode()).on('data', function(buf) {
    console.log(buf.toString())
  })
}).listen(port, function() {
  net.connect(port, function() {
    var encode = frame.encode()
    encode.pipe(this)
    encode.write('hello world')
    encode.end('cheerio!')
  })
})
```

## API

```js
var frame = require('frame-stream')
```

### frame.decode(opts)

This is an alias for `new frame.Decoder(opts)`. The following options are available:

- **lengthSize** (default: 4) - The length in bytes of the prepended message size.
- **getLength** - The function used to read the prepended message size. This function defaults to `readInt8()`, `readInt16BE()` or `readInt32BE()` according to the `lengthSize`.
- **maxSize** (default: 0) - The maximum allowed message size. This can be used to prevent denial-of-service attacks (`0` = turned off).
- **unbuffered** (default: `false`) - Return parts of a message as they arrive, rather than buffering them up until the last part arrives. Useful when you know messages will be large. Each part will be a `Buffer` with the following extra properties:
  - **framePos** - The index in the message where this part starts. Parts will be returned in order.
  - **frameLength** - The total message size.
  - **frameEnd** - A boolean indicating whether this is the last part of the message.

### frame.encode(opts)

This is an alias for `new frame.Encoder(opts)`. It prepends each chunk/message with its length. The following options are available:

- **lengthSize** (default: 4) - The length in bytes of the prepended message size.
- **setLength** - The function used to write the prepended message size. This function defaults to `writeInt8()`, `writeInt16BE()` or `writeInt32BE()` according to the `lengthSize`.

## MIT License

Copyright (c) 2014-2015 Markus Ast  
Copyright (c) 2020 David Halls

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

[npm]: http://img.shields.io/npm/v/frame-stream.svg?style=flat-square
[ci]: https://github.com/davedoesdev/frame-stream/actions/workflows/ci.yml/badge.svg

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