bufferutil

repository·master·Indexed 41 days ago

https://github.com/websockets/bufferutil

A high-performance utility library providing optimized C++ bindings for masking and unmasking WebSocket data payloads, used to speed up the ws WebSocket implementation. Version 4.1.0 includes functions such as bufferUtil.mask() and bufferUtil.unmask().

Tokens
566
Snippets
3
Records
3
Agent score
1%

What's inside bufferutil

  1. Install bufferutil

    master

    Install bufferutil as an optional dependency using npm. This is recommended because bufferutil provides performance optimizations for WebSocket operations, but the application can still function without it if the native build fails.

    npm install bufferutil --save-optional
  2. Use bufferUtil.mask() to mask data

    master

    The bufferUtil.mask() function masks a buffer using a provided masking-key according to the WebSocket protocol.

    Note: To maximize performance, parameters are not validated. You must ensure all arguments are correct before calling the function.

    Arguments:

    • source: The buffer to mask.
    • mask: A buffer representing the 4-byte masking-key.
    • output: The buffer where the masked result will be stored.
    • offset: The byte offset at which to start writing in the output buffer.
    • length: The number of bytes to mask.
    'use strict';
    
    const bufferUtil = require('bufferutil');
    const crypto = require('crypto');
    
    const source = crypto.randomBytes(10);
    const mask = crypto.randomBytes(4);
    
    // Masks the source in-place by using source as the output buffer
    bufferUtil.mask(source, mask, source, 0, source.length);
  3. Use bufferUtil.unmask() to unmask data

    master

    The bufferUtil.unmask() function unmasks a buffer using a provided masking-key according to the WebSocket protocol.

    Note: To maximize performance, parameters are not validated. You must ensure all arguments are correct before calling the function.

    Arguments:

    • buffer: The buffer to unmask.
    • mask: A buffer representing the 4-byte masking-key.
    'use strict';
    
    const bufferUtil = require('bufferutil');
    const crypto = require('crypto');
    
    const buffer = crypto.randomBytes(10);
    const mask = crypto.randomBytes(4);
    
    bufferUtil.unmask(buffer, mask);