@audc/bufferutil 0.0.0-stage → 4.1.0-ac.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2011 Einar Otto Stangvik <einaros@gmail.com>
2
+ Copyright (c) 2013 Arnout Kazemier and contributors
3
+ Copyright (c) 2016 Luigi Pinca and contributors
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy of
6
+ this software and associated documentation files (the "Software"), to deal in
7
+ the Software without restriction, including without limitation the rights to
8
+ use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
9
+ the Software, and to permit persons to whom the Software is furnished to do so,
10
+ subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
17
+ FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
18
+ COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
19
+ IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
20
+ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
package/README.md CHANGED
@@ -1,3 +1,79 @@
1
- # Temporary Holding Version
1
+ # bufferutil
2
2
 
3
- This version is a temporary placeholder for this package. An operational version to replace this has been submitted for review and is awaiting a staged release.
3
+ [![Version npm](https://img.shields.io/npm/v/bufferutil.svg?logo=npm)](https://www.npmjs.com/package/bufferutil)
4
+ [![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)
5
+
6
+ `bufferutil` is what makes `ws` fast. It provides some utilities to efficiently
7
+ perform some operations such as masking and unmasking the data payload of
8
+ WebSocket frames.
9
+
10
+ ## Installation
11
+
12
+ ```
13
+ npm install bufferutil --save-optional
14
+ ```
15
+
16
+ The `--save-optional` flag tells npm to save the package in your package.json
17
+ under the
18
+ [`optionalDependencies`](https://docs.npmjs.com/files/package.json#optionaldependencies)
19
+ key.
20
+
21
+ ## API
22
+
23
+ The module exports two functions. To maximize performance, parameters are not
24
+ validated. It is the caller's responsibility to ensure that they are correct.
25
+
26
+ ### `bufferUtil.mask(source, mask, output, offset, length)`
27
+
28
+ Masks a buffer using the given masking-key as specified by the WebSocket
29
+ protocol.
30
+
31
+ #### Arguments
32
+
33
+ - `source` - The buffer to mask.
34
+ - `mask` - A buffer representing the masking-key.
35
+ - `output` - The buffer where to store the result.
36
+ - `offset` - The offset at which to start writing.
37
+ - `length` - The number of bytes to mask.
38
+
39
+ #### Example
40
+
41
+ ```js
42
+ 'use strict';
43
+
44
+ const bufferUtil = require('bufferutil');
45
+ const crypto = require('crypto');
46
+
47
+ const source = crypto.randomBytes(10);
48
+ const mask = crypto.randomBytes(4);
49
+
50
+ bufferUtil.mask(source, mask, source, 0, source.length);
51
+ ```
52
+
53
+ ### `bufferUtil.unmask(buffer, mask)`
54
+
55
+ Unmasks a buffer using the given masking-key as specified by the WebSocket
56
+ protocol.
57
+
58
+ #### Arguments
59
+
60
+ - `buffer` - The buffer to unmask.
61
+ - `mask` - A buffer representing the masking-key.
62
+
63
+ #### Example
64
+
65
+ ```js
66
+ 'use strict';
67
+
68
+ const bufferUtil = require('bufferutil');
69
+ const crypto = require('crypto');
70
+
71
+ const buffer = crypto.randomBytes(10);
72
+ const mask = crypto.randomBytes(4);
73
+
74
+ bufferUtil.unmask(buffer, mask);
75
+ ```
76
+
77
+ ## License
78
+
79
+ [MIT](LICENSE)
package/binding.gyp ADDED
@@ -0,0 +1,19 @@
1
+ {
2
+ 'variables': {
3
+ 'openssl_fips': ''
4
+ },
5
+ 'targets': [
6
+ {
7
+ 'target_name': 'bufferutil',
8
+ 'sources': ['src/bufferutil.c'],
9
+ 'cflags': ['-std=c99'],
10
+ 'conditions': [
11
+ ["OS=='mac'", {
12
+ 'xcode_settings': {
13
+ 'MACOSX_DEPLOYMENT_TARGET': '10.7'
14
+ }
15
+ }]
16
+ ]
17
+ }
18
+ ]
19
+ }
package/fallback.js ADDED
@@ -0,0 +1,34 @@
1
+ 'use strict';
2
+
3
+ /**
4
+ * Masks a buffer using the given mask.
5
+ *
6
+ * @param {Buffer} source The buffer to mask
7
+ * @param {Buffer} mask The mask to use
8
+ * @param {Buffer} output The buffer where to store the result
9
+ * @param {Number} offset The offset at which to start writing
10
+ * @param {Number} length The number of bytes to mask.
11
+ * @public
12
+ */
13
+ const mask = (source, mask, output, offset, length) => {
14
+ for (var i = 0; i < length; i++) {
15
+ output[offset + i] = source[i] ^ mask[i & 3];
16
+ }
17
+ };
18
+
19
+ /**
20
+ * Unmasks a buffer using the given mask.
21
+ *
22
+ * @param {Buffer} buffer The buffer to unmask
23
+ * @param {Buffer} mask The mask to use
24
+ * @public
25
+ */
26
+ const unmask = (buffer, mask) => {
27
+ // Required until https://github.com/nodejs/node/issues/9006 is resolved.
28
+ const length = buffer.length;
29
+ for (var i = 0; i < length; i++) {
30
+ buffer[i] ^= mask[i & 3];
31
+ }
32
+ };
33
+
34
+ module.exports = { mask, unmask };
package/index.js ADDED
@@ -0,0 +1,7 @@
1
+ 'use strict';
2
+
3
+ try {
4
+ module.exports = require('node-gyp-build')(__dirname);
5
+ } catch (e) {
6
+ module.exports = require('./fallback');
7
+ }
package/package.json CHANGED
@@ -1,6 +1,35 @@
1
1
  {
2
2
  "name": "@audc/bufferutil",
3
- "version": "0.0.0-stage",
4
- "stub": true,
5
- "description": "Temporary package placeholder for staged publishing"
6
- }
3
+ "version": "4.1.0-ac.1",
4
+ "description": "WebSocket buffer utils",
5
+ "main": "index.js",
6
+ "engines": {
7
+ "node": ">=6.14.2"
8
+ },
9
+ "scripts": {
10
+ "install": "node-gyp-build",
11
+ "prebuild": "prebuildify --napi --strip --target=8.11.2",
12
+ "test": "mocha"
13
+ },
14
+ "repository": {
15
+ "type": "git",
16
+ "url": "git+https://github.com/audiocodes-dev/bufferutil.git"
17
+ },
18
+ "keywords": [
19
+ "bufferutil"
20
+ ],
21
+ "author": "Einar Otto Stangvik <einaros@gmail.com> (http://2x.io)",
22
+ "license": "MIT",
23
+ "bugs": {
24
+ "url": "https://github.com/websockets/bufferutil/issues"
25
+ },
26
+ "homepage": "https://github.com/websockets/bufferutil",
27
+ "dependencies": {
28
+ "node-gyp-build": "^4.3.0"
29
+ },
30
+ "devDependencies": {
31
+ "mocha": "^11.0.1",
32
+ "node-gyp": "^13.0.1",
33
+ "prebuildify": "^6.0.0"
34
+ }
35
+ }
@@ -0,0 +1,169 @@
1
+ #define NAPI_VERSION 1
2
+ #include <assert.h>
3
+ #include <node_api.h>
4
+
5
+ napi_value Mask(napi_env env, napi_callback_info info) {
6
+ napi_status status;
7
+ size_t argc = 5;
8
+ napi_value argv[5];
9
+
10
+ status = napi_get_cb_info(env, info, &argc, argv, NULL, NULL);
11
+ assert(status == napi_ok);
12
+
13
+ uint8_t *source;
14
+ uint8_t *mask;
15
+ uint8_t *destination;
16
+ int64_t offset;
17
+ int64_t length;
18
+
19
+ status = napi_get_buffer_info(env, argv[0], (void **)&source, NULL);
20
+ assert(status == napi_ok);
21
+
22
+ status = napi_get_buffer_info(env, argv[1], (void **)&mask, NULL);
23
+ assert(status == napi_ok);
24
+
25
+ status = napi_get_buffer_info(env, argv[2], (void **)&destination, NULL);
26
+ assert(status == napi_ok);
27
+
28
+ status = napi_get_value_int64(env, argv[3], &offset);
29
+ assert(status == napi_ok);
30
+
31
+ status = napi_get_value_int64(env, argv[4], &length);
32
+ assert(status == napi_ok);
33
+
34
+ destination += offset;
35
+ uint8_t index = 0;
36
+
37
+ //
38
+ // Alignment preamble.
39
+ //
40
+ while (index < length && (size_t)source % 8) {
41
+ *destination++ = *source++ ^ mask[index % 4];
42
+ index++;
43
+ }
44
+
45
+ length -= index;
46
+ if (!length)
47
+ return NULL;
48
+
49
+ //
50
+ // Realign mask and convert to 64 bit.
51
+ //
52
+ uint8_t maskAlignedArray[8];
53
+
54
+ for (uint8_t i = 0; i < 8; i++, index++) {
55
+ maskAlignedArray[i] = mask[index % 4];
56
+ }
57
+
58
+ //
59
+ // Apply 64 bit mask in 8 byte chunks.
60
+ //
61
+ int64_t loop = length / 8;
62
+ uint64_t mask8 = ((uint64_t *)maskAlignedArray)[0];
63
+ uint64_t *pFrom8 = (uint64_t *)source;
64
+ uint64_t *pTo8 = (uint64_t *)destination;
65
+
66
+ for (int64_t i = 0; i < loop; i++) {
67
+ pTo8[i] = pFrom8[i] ^ mask8;
68
+ }
69
+
70
+ //
71
+ // Apply mask to remaining data.
72
+ //
73
+ length %= 8;
74
+ source += 8 * loop;
75
+ destination += 8 * loop;
76
+
77
+ for (uint8_t i = 0; i < length; i++) {
78
+ destination[i] = source[i] ^ maskAlignedArray[i];
79
+ }
80
+
81
+ return NULL;
82
+ }
83
+
84
+ napi_value Unmask(napi_env env, napi_callback_info info) {
85
+ napi_status status;
86
+ size_t argc = 2;
87
+ napi_value argv[2];
88
+
89
+ status = napi_get_cb_info(env, info, &argc, argv, NULL, NULL);
90
+ assert(status == napi_ok);
91
+
92
+ uint8_t *source;
93
+ uint8_t *mask;
94
+ size_t length;
95
+
96
+ status = napi_get_buffer_info(env, argv[0], (void **)&source, &length);
97
+ assert(status == napi_ok);
98
+
99
+ status = napi_get_buffer_info(env, argv[1], (void **)&mask, NULL);
100
+ assert(status == napi_ok);
101
+
102
+ uint8_t index = 0;
103
+
104
+ //
105
+ // Alignment preamble.
106
+ //
107
+ while (index < length && (size_t)source % 8) {
108
+ *source++ ^= mask[index % 4];
109
+ index++;
110
+ }
111
+
112
+ length -= index;
113
+ if (!length)
114
+ return NULL;
115
+
116
+ //
117
+ // Realign mask and convert to 64 bit.
118
+ //
119
+ uint8_t maskAlignedArray[8];
120
+
121
+ for (uint8_t i = 0; i < 8; i++, index++) {
122
+ maskAlignedArray[i] = mask[index % 4];
123
+ }
124
+
125
+ //
126
+ // Apply 64 bit mask in 8 byte chunks.
127
+ //
128
+ size_t loop = length / 8;
129
+ uint64_t mask8 = ((uint64_t *)maskAlignedArray)[0];
130
+ uint64_t *pSource8 = (uint64_t *)source;
131
+
132
+ for (size_t i = 0; i < loop; i++) {
133
+ pSource8[i] ^= mask8;
134
+ }
135
+
136
+ //
137
+ // Apply mask to remaining data.
138
+ //
139
+ length %= 8;
140
+ source += 8 * loop;
141
+
142
+ for (uint8_t i = 0; i < length; i++) {
143
+ source[i] ^= maskAlignedArray[i];
144
+ }
145
+
146
+ return NULL;
147
+ }
148
+
149
+ napi_value Init(napi_env env, napi_value exports) {
150
+ napi_status status;
151
+ napi_value mask;
152
+ napi_value unmask;
153
+
154
+ status = napi_create_function(env, NULL, 0, Mask, NULL, &mask);
155
+ assert(status == napi_ok);
156
+
157
+ status = napi_create_function(env, NULL, 0, Unmask, NULL, &unmask);
158
+ assert(status == napi_ok);
159
+
160
+ status = napi_set_named_property(env, exports, "mask", mask);
161
+ assert(status == napi_ok);
162
+
163
+ status = napi_set_named_property(env, exports, "unmask", unmask);
164
+ assert(status == napi_ok);
165
+
166
+ return exports;
167
+ }
168
+
169
+ NAPI_MODULE(NODE_GYP_MODULE_NAME, Init)