Skip to content

websockets/bufferutil

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Dec 27, 2024
35e2eb6 · Dec 27, 2024
Aug 7, 2024
Dec 23, 2018
Feb 19, 2019
Feb 7, 2017
Jan 2, 2022
Apr 25, 2020
Dec 9, 2022
May 19, 2024
Oct 28, 2024
Jul 3, 2018
Jul 3, 2018
Dec 27, 2024

Repository files navigation

bufferutil

Version npm Linux/macOS/Windows Build

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 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

'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

'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