pax_global_header00006660000000000000000000000064151210241330014502gustar00rootroot0000000000000052 comment=580a8a92c91d1b3dea6d4b0d54f89daf42da5e48 websockets-bufferutil-580a8a9/000077500000000000000000000000001512102413300163615ustar00rootroot00000000000000websockets-bufferutil-580a8a9/.github/000077500000000000000000000000001512102413300177215ustar00rootroot00000000000000websockets-bufferutil-580a8a9/.github/workflows/000077500000000000000000000000001512102413300217565ustar00rootroot00000000000000websockets-bufferutil-580a8a9/.github/workflows/ci.yml000066400000000000000000000056441512102413300231050ustar00rootroot00000000000000name: CI on: - push - pull_request permissions: {} jobs: test: runs-on: ${{ matrix.os }} strategy: matrix: arch: - x64 - arm64 node: - 20 - 22 - 24 os: - macos-latest - macos-15-intel - ubuntu-latest - windows-latest exclude: - arch: arm64 os: macos-15-intel - arch: arm64 os: ubuntu-latest - arch: arm64 os: windows-latest - arch: x64 os: macos-latest include: - arch: x86 node: 20 os: windows-latest - arch: x86 node: 22 os: windows-latest steps: - uses: actions/checkout@v6 - uses: actions/setup-node@v6 with: node-version: ${{ matrix.node }} architecture: ${{ matrix.arch }} - run: npm install - run: npm test build: if: startsWith(github.ref, 'refs/tags/') needs: test runs-on: ${{ matrix.os }} strategy: matrix: arch: - x64 os: - macos-15-intel - ubuntu-22.04 - windows-latest include: - arch: x86 os: windows-latest - arch: arm64 os: macos-latest steps: - uses: actions/checkout@v6 - uses: actions/setup-node@v6 with: node-version: 20 architecture: ${{ matrix.arch }} - run: npm install - run: npm run prebuild - uses: actions/upload-artifact@v5 with: name: ${{ matrix.os }}-${{ matrix.arch }} path: prebuilds retention-days: 1 release: needs: build permissions: contents: write # Needed for softprops/action-gh-release. runs-on: ubuntu-latest steps: - uses: actions/checkout@v6 - uses: actions/download-artifact@v6 with: path: prebuilds - run: echo "version=$(git describe --tags)" >> $GITHUB_OUTPUT id: get_version - run: tar -cvf "${{ steps.get_version.outputs.version }}-darwin-x64.tar" -C "prebuilds/macos-15-intel-x64" darwin-x64 - run: tar -cvf "${{ steps.get_version.outputs.version }}-darwin-arm64.tar" -C "prebuilds/macos-latest-arm64" darwin-arm64 - run: tar -cvf "${{ steps.get_version.outputs.version }}-linux-x64.tar" -C "prebuilds/ubuntu-22.04-x64" linux-x64 - run: tar -cvf "${{ steps.get_version.outputs.version }}-win32-ia32.tar" -C "prebuilds/windows-latest-x86" win32-ia32 - run: tar -cvf "${{ steps.get_version.outputs.version }}-win32-x64.tar" -C "prebuilds/windows-latest-x64" win32-x64 - uses: softprops/action-gh-release@v2 with: files: ${{ steps.get_version.outputs.version }}-*.tar token: ${{ secrets.GITHUB_TOKEN }} websockets-bufferutil-580a8a9/.gitignore000066400000000000000000000000561512102413300203520ustar00rootroot00000000000000node_modules/ prebuilds/ build/ npm-debug.log websockets-bufferutil-580a8a9/.npmignore000066400000000000000000000000201512102413300203500ustar00rootroot00000000000000build/ test/ .* websockets-bufferutil-580a8a9/.npmrc000066400000000000000000000000231512102413300174740ustar00rootroot00000000000000package-lock=false websockets-bufferutil-580a8a9/LICENSE000066400000000000000000000022371512102413300173720ustar00rootroot00000000000000Copyright (c) 2011 Einar Otto Stangvik Copyright (c) 2013 Arnout Kazemier and contributors Copyright (c) 2016 Luigi Pinca and contributors 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. websockets-bufferutil-580a8a9/README.md000066400000000000000000000040301512102413300176350ustar00rootroot00000000000000# 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) websockets-bufferutil-580a8a9/binding.gyp000066400000000000000000000005221512102413300205130ustar00rootroot00000000000000{ 'variables': { 'openssl_fips': '' }, 'targets': [ { 'target_name': 'bufferutil', 'sources': ['src/bufferutil.c'], 'cflags': ['-std=c99'], 'conditions': [ ["OS=='mac'", { 'xcode_settings': { 'MACOSX_DEPLOYMENT_TARGET': '10.7' } }] ] } ] } websockets-bufferutil-580a8a9/fallback.js000066400000000000000000000016031512102413300204560ustar00rootroot00000000000000'use strict'; /** * Masks a buffer using the given mask. * * @param {Buffer} source The buffer to mask * @param {Buffer} mask The mask to use * @param {Buffer} output The buffer where to store the result * @param {Number} offset The offset at which to start writing * @param {Number} length The number of bytes to mask. * @public */ const mask = (source, mask, output, offset, length) => { for (var i = 0; i < length; i++) { output[offset + i] = source[i] ^ mask[i & 3]; } }; /** * Unmasks a buffer using the given mask. * * @param {Buffer} buffer The buffer to unmask * @param {Buffer} mask The mask to use * @public */ const unmask = (buffer, mask) => { // Required until https://github.com/nodejs/node/issues/9006 is resolved. const length = buffer.length; for (var i = 0; i < length; i++) { buffer[i] ^= mask[i & 3]; } }; module.exports = { mask, unmask }; websockets-bufferutil-580a8a9/index.js000066400000000000000000000002101512102413300200170ustar00rootroot00000000000000'use strict'; try { module.exports = require('node-gyp-build')(__dirname); } catch (e) { module.exports = require('./fallback'); } websockets-bufferutil-580a8a9/package.json000066400000000000000000000014571512102413300206560ustar00rootroot00000000000000{ "name": "bufferutil", "version": "4.1.0", "description": "WebSocket buffer utils", "main": "index.js", "engines": { "node": ">=6.14.2" }, "scripts": { "install": "node-gyp-build", "prebuild": "prebuildify --napi --strip --target=8.11.2", "test": "mocha" }, "repository": { "type": "git", "url": "https://github.com/websockets/bufferutil" }, "keywords": [ "bufferutil" ], "author": "Einar Otto Stangvik (http://2x.io)", "license": "MIT", "bugs": { "url": "https://github.com/websockets/bufferutil/issues" }, "homepage": "https://github.com/websockets/bufferutil", "dependencies": { "node-gyp-build": "^4.3.0" }, "devDependencies": { "mocha": "^11.0.1", "node-gyp": "^12.1.0", "prebuildify": "^6.0.0" } } websockets-bufferutil-580a8a9/src/000077500000000000000000000000001512102413300171505ustar00rootroot00000000000000websockets-bufferutil-580a8a9/src/bufferutil.c000066400000000000000000000071211512102413300214640ustar00rootroot00000000000000#define NAPI_VERSION 1 #include #include napi_value Mask(napi_env env, napi_callback_info info) { napi_status status; size_t argc = 5; napi_value argv[5]; status = napi_get_cb_info(env, info, &argc, argv, NULL, NULL); assert(status == napi_ok); uint8_t *source; uint8_t *mask; uint8_t *destination; int64_t offset; int64_t length; status = napi_get_buffer_info(env, argv[0], (void **)&source, NULL); assert(status == napi_ok); status = napi_get_buffer_info(env, argv[1], (void **)&mask, NULL); assert(status == napi_ok); status = napi_get_buffer_info(env, argv[2], (void **)&destination, NULL); assert(status == napi_ok); status = napi_get_value_int64(env, argv[3], &offset); assert(status == napi_ok); status = napi_get_value_int64(env, argv[4], &length); assert(status == napi_ok); destination += offset; uint8_t index = 0; // // Alignment preamble. // while (index < length && (size_t)source % 8) { *destination++ = *source++ ^ mask[index % 4]; index++; } length -= index; if (!length) return NULL; // // Realign mask and convert to 64 bit. // uint8_t maskAlignedArray[8]; for (uint8_t i = 0; i < 8; i++, index++) { maskAlignedArray[i] = mask[index % 4]; } // // Apply 64 bit mask in 8 byte chunks. // int64_t loop = length / 8; uint64_t mask8 = ((uint64_t *)maskAlignedArray)[0]; uint64_t *pFrom8 = (uint64_t *)source; uint64_t *pTo8 = (uint64_t *)destination; for (int64_t i = 0; i < loop; i++) { pTo8[i] = pFrom8[i] ^ mask8; } // // Apply mask to remaining data. // length %= 8; source += 8 * loop; destination += 8 * loop; for (uint8_t i = 0; i < length; i++) { destination[i] = source[i] ^ maskAlignedArray[i]; } return NULL; } napi_value Unmask(napi_env env, napi_callback_info info) { napi_status status; size_t argc = 2; napi_value argv[2]; status = napi_get_cb_info(env, info, &argc, argv, NULL, NULL); assert(status == napi_ok); uint8_t *source; uint8_t *mask; size_t length; status = napi_get_buffer_info(env, argv[0], (void **)&source, &length); assert(status == napi_ok); status = napi_get_buffer_info(env, argv[1], (void **)&mask, NULL); assert(status == napi_ok); uint8_t index = 0; // // Alignment preamble. // while (index < length && (size_t)source % 8) { *source++ ^= mask[index % 4]; index++; } length -= index; if (!length) return NULL; // // Realign mask and convert to 64 bit. // uint8_t maskAlignedArray[8]; for (uint8_t i = 0; i < 8; i++, index++) { maskAlignedArray[i] = mask[index % 4]; } // // Apply 64 bit mask in 8 byte chunks. // size_t loop = length / 8; uint64_t mask8 = ((uint64_t *)maskAlignedArray)[0]; uint64_t *pSource8 = (uint64_t *)source; for (size_t i = 0; i < loop; i++) { pSource8[i] ^= mask8; } // // Apply mask to remaining data. // length %= 8; source += 8 * loop; for (uint8_t i = 0; i < length; i++) { source[i] ^= maskAlignedArray[i]; } return NULL; } napi_value Init(napi_env env, napi_value exports) { napi_status status; napi_value mask; napi_value unmask; status = napi_create_function(env, NULL, 0, Mask, NULL, &mask); assert(status == napi_ok); status = napi_create_function(env, NULL, 0, Unmask, NULL, &unmask); assert(status == napi_ok); status = napi_set_named_property(env, exports, "mask", mask); assert(status == napi_ok); status = napi_set_named_property(env, exports, "unmask", unmask); assert(status == napi_ok); return exports; } NAPI_MODULE(NODE_GYP_MODULE_NAME, Init) websockets-bufferutil-580a8a9/test/000077500000000000000000000000001512102413300173405ustar00rootroot00000000000000websockets-bufferutil-580a8a9/test/test.js000066400000000000000000000045461512102413300206660ustar00rootroot00000000000000'use strict'; const { deepStrictEqual } = require('assert'); const { randomBytes } = require('crypto'); const { join } = require('path'); const native = require('node-gyp-build')(join(__dirname, '..')); const fallback = require('../fallback'); function use(bufferUtil) { return function () { it('masks a buffer (1/2)', function () { const buf = Buffer.from([0x6c, 0x3c, 0x58, 0xd9, 0x3e, 0x21, 0x09, 0x9f]); const mask = Buffer.from([0x48, 0x2a, 0xce, 0x24]); bufferUtil.mask(buf, mask, buf, 0, buf.length); deepStrictEqual( buf, Buffer.from([0x24, 0x16, 0x96, 0xfd, 0x76, 0x0b, 0xc7, 0xbb]) ); }); it('masks a buffer (2/2)', function () { const src = Buffer.from([0x6c, 0x3c, 0x58, 0xd9, 0x3e, 0x21, 0x09, 0x9f]); const mask = Buffer.from([0x48, 0x2a, 0xce, 0x24]); const dest = Buffer.alloc(src.length + 2); bufferUtil.mask(src, mask, dest, 2, src.length); deepStrictEqual( dest, Buffer.from([0x00, 0x00, 0x24, 0x16, 0x96, 0xfd, 0x76, 0x0b, 0xc7, 0xbb]) ); }); it('unmasks a buffer', function () { const buf = Buffer.from([0x24, 0x16, 0x96, 0xfd, 0x76, 0x0b, 0xc7, 0xbb]); const mask = Buffer.from([0x48, 0x2a, 0xce, 0x24]); bufferUtil.unmask(buf, mask); deepStrictEqual( buf, Buffer.from([0x6c, 0x3c, 0x58, 0xd9, 0x3e, 0x21, 0x09, 0x9f]) ); }); }; } describe('bindings', use(native)); describe('fallback', use(fallback)); describe('bindings match fallback', () => { const sizes = [1, 127, 128, 200, 1024, 10 * 1024 - 1, 10 * 1024] const offsets = [0, 1, 10, 16, 128] it('masks', function () { for (const size of sizes) { for (const offset of offsets) { const src = randomBytes(size); const mask = randomBytes(4); const dest = randomBytes(size + offset); const destFallback = Buffer.from(dest); native.mask(src, mask, dest, offset, size); fallback.mask(src, mask, destFallback, offset, size); deepStrictEqual(dest, destFallback); } } }); it('unmasks', function () { for (const size of sizes) { const buf1 = randomBytes(size); const buf2 = Buffer.from(buf1); const mask = randomBytes(4); native.unmask(buf1, mask); fallback.unmask(buf2, mask); deepStrictEqual(buf1, buf2); } }); })