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