# bufferutil

> WebSocket buffer utils

Latest version **4.1.0** (published 2025-12-18) · MIT license · 0 weekly downloads

## Install

```sh
npm install bufferutil
pnpm add bufferutil
yarn add bufferutil
bun add bufferutil
```

## Health

**Score 50/100 (C)** — status: stable.

Positive: no vulnerabilities; high maintenance score.

Warnings: low downloads; no types; no esm support.

## Facts

| | |
|---|---|
| Version | 4.1.0 |
| Published | 2025-12-18 |
| First published | 2015-01-29 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Node | >=6.14.2 |
| Dependencies | 1 |
| Unpacked size | 342.8 KB |
| Known vulnerabilities | 0 |
| Install scripts | yes |
| GitHub stars | 171 |
| Author | Einar Otto Stangvik |
| Maintainers | einaros, lpinca, 3rdeden |
| Keywords | bufferutil |

## Links

- npm: https://www.npmjs.com/package/bufferutil
- Repository: https://github.com/websockets/bufferutil
- Issues: https://github.com/websockets/bufferutil/issues
- npm.io page: https://npm.io/package/bufferutil

## Dependencies (1)

- [node-gyp-build](https://npm.io/package/node-gyp-build.md) ^4.3.0

## Alternatives

- [flatbuffers](https://npm.io/package/flatbuffers.md) — 6.0M weekly downloads
- [jwt-simple](https://npm.io/package/jwt-simple.md) — 259.5K weekly downloads
- [@exodus/patch-broken-hermes-typed-arrays](https://npm.io/package/@exodus/patch-broken-hermes-typed-arrays.md) — 28.5K weekly downloads
- [@native-to-anchor/buffer-layout](https://npm.io/package/@native-to-anchor/buffer-layout.md) — 12.2K weekly downloads
- [binary-parser-encoder](https://npm.io/package/binary-parser-encoder.md) — 5.3K weekly downloads

## Recent versions

- 4.1.0 (latest) — 2025-12-18
- 3.0.3-napi (n-api) — 2017-12-04
- 4.0.9 — 2024-12-27
- 4.0.8 — 2023-10-15
- 4.0.7 — 2022-10-18
- 4.0.6 — 2022-01-02
- 4.0.5 — 2021-10-13
- 4.0.4 — 2021-09-25
- 4.0.3 — 2021-01-02
- 4.0.2 — 2020-10-31
- 4.0.1 — 2018-12-23
- 4.0.0 — 2018-07-03
- 3.0.5 — 2018-05-06
- 3.0.4 — 2018-04-06
- 3.0.3 — 2017-11-03
- … 11 more at https://npm.io/package/bufferutil/versions

## README

# bufferutil

[![Version npm](https://img.shields.io/npm/v/bufferutil.svg?logo=npm)](https://www.npmjs.com/package/bufferutil)
[![Linux/macOS/Windows Build](https://img.shields.io/github/actions/workflow/status/websockets/bufferutil/ci.yml?branch=master&label=build&logo=github)](https://github.com/websockets/bufferutil/actions?query=workflow%3ACI+branch%3Amaster)

`bufferutil` is what makes `ws` fast. It provides some utilities to efficiently
perform some operations such as masking and unmasking the data payload of
WebSocket frames.

## Installation

```
npm install bufferutil --save-optional
```

The `--save-optional` flag tells npm to save the package in your package.json
under the
[`optionalDependencies`](https://docs.npmjs.com/files/package.json#optionaldependencies)
key.

## API

The module exports two functions. To maximize performance, parameters are not
validated. It is the caller's responsibility to ensure that they are correct.

### `bufferUtil.mask(source, mask, output, offset, length)`

Masks a buffer using the given masking-key as specified by the WebSocket
protocol.

#### Arguments

- `source` - The buffer to mask.
- `mask` - A buffer representing the masking-key.
- `output` - The buffer where to store the result.
- `offset` - The offset at which to start writing.
- `length` - The number of bytes to mask.

#### Example

```js
'use strict';

const bufferUtil = require('bufferutil');
const crypto = require('crypto');

const source = crypto.randomBytes(10);
const mask = crypto.randomBytes(4);

bufferUtil.mask(source, mask, source, 0, source.length);
```

### `bufferUtil.unmask(buffer, mask)`

Unmasks a buffer using the given masking-key as specified by the WebSocket
protocol.

#### Arguments

- `buffer` - The buffer to unmask.
- `mask` - A buffer representing the masking-key.

#### Example

```js
'use strict';

const bufferUtil = require('bufferutil');
const crypto = require('crypto');

const buffer = crypto.randomBytes(10);
const mask = crypto.randomBytes(4);

bufferUtil.unmask(buffer, mask);
```

## License

[MIT](LICENSE)

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