pax_global_header00006660000000000000000000000064147335501030014513gustar00rootroot0000000000000052 comment=35e2eb6a14a8d58b87bc1f9ff2ddff67e2f1fa97 bufferutil-4.0.9/000077500000000000000000000000001473355010300136745ustar00rootroot00000000000000bufferutil-4.0.9/.github/000077500000000000000000000000001473355010300152345ustar00rootroot00000000000000bufferutil-4.0.9/.github/workflows/000077500000000000000000000000001473355010300172715ustar00rootroot00000000000000bufferutil-4.0.9/.github/workflows/ci.yml000066400000000000000000000053041473355010300204110ustar00rootroot00000000000000name: CI on: - push - pull_request permissions: {} jobs: test: runs-on: ${{ matrix.os }} strategy: matrix: arch: - x64 - x86 node: - 18 - 20 - 22 os: - macos-latest - ubuntu-latest - windows-latest exclude: - arch: x86 os: macos-latest - arch: x86 os: ubuntu-latest - arch: x86 node: 18 os: windows-latest steps: - uses: actions/checkout@v4 - uses: actions/setup-node@v4 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 - x86 os: - macos-latest - ubuntu-20.04 - windows-latest exclude: - arch: x86 os: macos-latest - arch: x86 os: ubuntu-20.04 steps: - uses: actions/checkout@v4 - uses: actions/setup-node@v4 with: node-version: 20 architecture: ${{ matrix.arch }} - run: npm install - run: npm run prebuild if: matrix.os != 'macos-latest' - run: npm run prebuild-darwin-x64+arm64 if: matrix.os == 'macos-latest' - uses: actions/upload-artifact@v4 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@v4 - uses: actions/download-artifact@v4 with: path: prebuilds - run: echo "version=$(git describe --tags)" >> $GITHUB_OUTPUT id: get_version - run: tar -cvf "${{ steps.get_version.outputs.version }}-darwin-x64+arm64.tar" -C "prebuilds/macos-latest-x64" darwin-x64+arm64 - run: tar -cvf "${{ steps.get_version.outputs.version }}-linux-x64.tar" -C "prebuilds/ubuntu-20.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 }} bufferutil-4.0.9/.gitignore000066400000000000000000000000561473355010300156650ustar00rootroot00000000000000node_modules/ prebuilds/ build/ npm-debug.log bufferutil-4.0.9/.npmignore000066400000000000000000000000201473355010300156630ustar00rootroot00000000000000build/ test/ .* bufferutil-4.0.9/.npmrc000066400000000000000000000000231473355010300150070ustar00rootroot00000000000000package-lock=false bufferutil-4.0.9/LICENSE000066400000000000000000000022371473355010300147050ustar00rootroot00000000000000Copyright (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. bufferutil-4.0.9/README.md000066400000000000000000000040301473355010300151500ustar00rootroot00000000000000# 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) bufferutil-4.0.9/binding.gyp000066400000000000000000000015201473355010300160250ustar00rootroot00000000000000{ 'variables': { 'openssl_fips': '' }, 'targets': [ { 'target_name': 'bufferutil', 'sources': ['src/bufferutil.c'], 'cflags': ['-std=c99'], 'conditions': [ ["OS=='mac'", { 'variables': { 'clang_version': '&1 | perl -ne \'print $1 if /clang version ([0-9]+(\\.[0-9]+){2,})/\')' }, 'xcode_settings': { 'MACOSX_DEPLOYMENT_TARGET': '10.7' }, 'conditions': [ # Use Perl v-strings to compare versions. ['clang_version and { 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 }; bufferutil-4.0.9/index.js000066400000000000000000000002101473355010300153320ustar00rootroot00000000000000'use strict'; try { module.exports = require('node-gyp-build')(__dirname); } catch (e) { module.exports = require('./fallback'); } bufferutil-4.0.9/package.json000066400000000000000000000016171473355010300161670ustar00rootroot00000000000000{ "name": "bufferutil", "version": "4.0.9", "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", "prebuild-darwin-x64+arm64": "prebuildify --arch x64+arm64 --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": "^11.0.0", "prebuildify": "^6.0.0" } } bufferutil-4.0.9/src/000077500000000000000000000000001473355010300144635ustar00rootroot00000000000000bufferutil-4.0.9/src/bufferutil.c000066400000000000000000000071421473355010300170020ustar00rootroot00000000000000#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; uint32_t offset; uint32_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_uint32(env, argv[3], &offset); assert(status == napi_ok); status = napi_get_value_uint32(env, argv[4], &length); assert(status == napi_ok); destination += offset; uint32_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. // uint32_t loop = length / 8; uint64_t *pMask8 = (uint64_t *)maskAlignedArray; while (loop--) { uint64_t *pFrom8 = (uint64_t *)source; uint64_t *pTo8 = (uint64_t *)destination; *pTo8 = *pFrom8 ^ *pMask8; source += 8; destination += 8; } // // Apply mask to remaining data. // uint8_t *pmaskAlignedArray = maskAlignedArray; length %= 8; while (length--) { *destination++ = *source++ ^ *pmaskAlignedArray++; } 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; size_t length; uint8_t *mask; 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); uint32_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. // uint32_t loop = length / 8; uint64_t *pMask8 = (uint64_t *)maskAlignedArray; while (loop--) { uint64_t *pSource8 = (uint64_t *)source; *pSource8 ^= *pMask8; source += 8; } // // Apply mask to remaining data. // uint8_t *pmaskAlignedArray = maskAlignedArray; length %= 8; while (length--) { *source++ ^= *pmaskAlignedArray++; } 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) bufferutil-4.0.9/test/000077500000000000000000000000001473355010300146535ustar00rootroot00000000000000bufferutil-4.0.9/test/test.js000066400000000000000000000026071473355010300161750ustar00rootroot00000000000000'use strict'; const { deepStrictEqual } = require('assert'); const { join } = require('path'); 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(require('node-gyp-build')(join(__dirname, '..')))); describe('fallback', use(require('../fallback')));