msgpackr-extract 1.1.0-napi → 2.0.0-beta

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.
@@ -12,10 +12,12 @@ jobs:
12
12
  node-version: 12
13
13
  - run: npm install
14
14
  - run: npm test
15
- - run: npm run prebuild
15
+ - run: npm run prebuild-libc
16
16
  if: startsWith(github.ref, 'refs/tags/')
17
- - run: npm run prebuild-arm64
17
+ - run: npm run prebuild-libc
18
18
  if: startsWith(github.ref, 'refs/tags/')
19
+ env:
20
+ PREBUILD_ARCH: arm64
19
21
  - run: tar --create --format ustar --verbose --file=prebuild-darwin.tar -C prebuilds .
20
22
  if: startsWith(github.ref, 'refs/tags/')
21
23
  - name: Prebuild
@@ -35,7 +37,7 @@ jobs:
35
37
  with:
36
38
  node-version: 12
37
39
  - run: npm install
38
- - run: npm run prebuild
40
+ - run: npm run prebuild-libc
39
41
  - run: npm test
40
42
  - run: tar --create --verbose --file=prebuild-linux.tar -C prebuilds .
41
43
  - name: Prebuild
@@ -51,7 +53,15 @@ jobs:
51
53
  - run: apk add python3 py3-pip build-base
52
54
  - uses: actions/checkout@v2
53
55
  - run: npm install
54
- - run: npm run prebuild-musl
56
+ - run: npm run prebuild-libc
57
+ env:
58
+ PREBUILD_LIBC: musl
59
+ PREBUILD_ARCH: arm64
60
+ CC: ${PWD}/aarch64-linux-musl-cross/bin/aarch64-linux-musl-gcc
61
+ CXX: ${PWD}/aarch64-linux-musl-cross/bin/aarch64-linux-musl-g++
62
+ - run: npm run prebuild-libc
63
+ env:
64
+ PREBUILD_LIBC: musl
55
65
  - run: npm test
56
66
  - run: tar --create --verbose --file=prebuild-alpine.tar -C prebuilds .
57
67
  - name: Prebuild
@@ -74,9 +84,10 @@ jobs:
74
84
  with:
75
85
  node-version: 12
76
86
  - run: npm install
77
- - run: npm run prebuild-arm64
87
+ - run: npm run prebuild-libc
78
88
  if: startsWith(github.ref, 'refs/tags/')
79
89
  env:
90
+ PREBUILD_ARCH: arm64
80
91
  CC: aarch64-linux-gnu-gcc
81
92
  CXX: aarch64-linux-gnu-g++
82
93
  - run: tar --create --verbose --file=prebuild-linux-arm64.tar -C prebuilds .
package/binding.gyp CHANGED
@@ -11,8 +11,6 @@
11
11
  "src/extract.cpp",
12
12
  ],
13
13
  "defines": [ "NAPI_DISABLE_CPP_EXCEPTIONS" ],
14
- "include_dirs" : ["<!(node -p \"require('node-addon-api').include_dir\")"],
15
- "dependencies": ["<!(node -p \"require('node-addon-api').gyp\")"],
16
14
  "conditions": [
17
15
  ["OS=='linux'", {
18
16
  "variables": {
package/index.js CHANGED
@@ -1 +1 @@
1
- module.exports = require('node-gyp-build')(__dirname)
1
+ module.exports = require('node-gyp-build')(__dirname);
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "msgpackr-extract",
3
3
  "author": "Kris Zyp",
4
- "version": "1.1.0-napi",
4
+ "version": "2.0.0-beta",
5
5
  "description": "Node addon for string extraction for msgpackr",
6
6
  "license": "MIT",
7
7
  "repository": {
@@ -11,19 +11,19 @@
11
11
  "scripts": {
12
12
  "install": "node-gyp-build",
13
13
  "recompile": "node-gyp build",
14
- "before-publish": "prebuildify-ci download && prebuildify --napi --target 12.22.7",
15
- "prebuild": "prebuildify --napi --target 12.22.7",
16
- "prebuild-musl": "prebuildify --napi --libc musl --tag-libc --target 12.22.7",
17
- "prebuild-arm64": "prebuildify --napi --arch=arm64 --tag-libc --target 12.22.7", "test": ""
14
+ "before-publish": "prebuildify-ci download && prebuildify --optional-packages --napi --target 12.22.7",
15
+ "prebuild": "prebuildify --optional-packages --napi --target 12.22.7",
16
+ "prebuild-libc": "prebuildify --optional-packages --napi --tag-libc --target 12.22.7",
17
+ "publish-all": "cd prebuilds/win32-x64 && npm publish --otp %OTP% && cd ../darwin-x64 && npm publish --otp %OTP% && cd ../darwin-arm64 && npm publish --otp %OTP% && cd ../linux-x64 && npm publish --otp %OTP% && cd ../linux-arm64 && npm publish --otp %OTP% && cd ../.. && npm publish --otp %OTP%",
18
+ "test": "node ./index.js"
18
19
  },
19
20
  "main": "./index.js",
20
21
  "gypfile": true,
21
22
  "dependencies": {
22
- "node-addon-api": "^4.3.0",
23
- "node-gyp-build": "^4.2.3"
23
+ "node-gyp-build": "kriszyp/node-gyp-build#optional-packages"
24
24
  },
25
25
  "devDependencies": {
26
- "prebuildify": "^5.0.0",
26
+ "prebuildify": "kriszyp/prebuildify#optional-packages",
27
27
  "prebuildify-ci": "^1.0.5"
28
28
  }
29
29
  }
@@ -0,0 +1 @@
1
+ Platform specific binary for msgpackr-extract on darwin OS with arm64 architecture
File without changes
@@ -0,0 +1,11 @@
1
+ {
2
+ "name": "msgpackr-extract-darwin-arm64",
3
+ "version": "2.0.0-beta",
4
+ "os": [
5
+ "darwin"
6
+ ],
7
+ "cpu": [
8
+ "arm64"
9
+ ],
10
+ "description": "Platform specific binary for msgpackr-extract on darwin OS with arm64 architecture"
11
+ }
@@ -0,0 +1 @@
1
+ Platform specific binary for msgpackr-extract on darwin OS with x64 architecture
File without changes
@@ -0,0 +1,11 @@
1
+ {
2
+ "name": "msgpackr-extract-darwin-x64",
3
+ "version": "2.0.0-beta",
4
+ "os": [
5
+ "darwin"
6
+ ],
7
+ "cpu": [
8
+ "x64"
9
+ ],
10
+ "description": "Platform specific binary for msgpackr-extract on darwin OS with x64 architecture"
11
+ }
@@ -0,0 +1 @@
1
+ Platform specific binary for msgpackr-extract on linux OS with arm64 architecture
File without changes
@@ -0,0 +1,11 @@
1
+ {
2
+ "name": "msgpackr-extract-linux-arm64",
3
+ "version": "2.0.0-beta",
4
+ "os": [
5
+ "linux"
6
+ ],
7
+ "cpu": [
8
+ "arm64"
9
+ ],
10
+ "description": "Platform specific binary for msgpackr-extract on linux OS with arm64 architecture"
11
+ }
@@ -0,0 +1 @@
1
+ Platform specific binary for msgpackr-extract on linux OS with x64 architecture
File without changes
@@ -0,0 +1,11 @@
1
+ {
2
+ "name": "msgpackr-extract-linux-x64",
3
+ "version": "2.0.0-beta",
4
+ "os": [
5
+ "linux"
6
+ ],
7
+ "cpu": [
8
+ "x64"
9
+ ],
10
+ "description": "Platform specific binary for msgpackr-extract on linux OS with x64 architecture"
11
+ }
@@ -0,0 +1 @@
1
+ Platform specific binary for msgpackr-extract on win32 OS with x64 architecture
File without changes
Binary file
@@ -0,0 +1,11 @@
1
+ {
2
+ "name": "msgpackr-extract-win32-x64",
3
+ "version": "2.0.0-beta",
4
+ "os": [
5
+ "win32"
6
+ ],
7
+ "cpu": [
8
+ "x64"
9
+ ],
10
+ "description": "Platform specific binary for msgpackr-extract on win32 OS with x64 architecture"
11
+ }
package/src/extract.cpp CHANGED
@@ -5,11 +5,8 @@ provides much better performance. This will parse and produce up to 256 strings
5
5
  times as necessary to get more strings. This must be partially capable of parsing MessagePack so it can know where to
6
6
  find the string tokens and determine their position and length. All strings are decoded as UTF-8.
7
7
  */
8
- #include <napi.h>
9
8
  #include <node_api.h>
10
9
 
11
- using namespace Napi;
12
-
13
10
  #ifndef thread_local
14
11
  #ifdef __GNUC__
15
12
  # define thread_local __thread
@@ -25,17 +22,25 @@ using namespace Napi;
25
22
  const int MAX_TARGET_SIZE = 255;
26
23
  typedef int (*token_handler)(uint8_t* source, int position, int size);
27
24
  token_handler tokenTable[256] = {};
25
+ napi_value unexpectedEnd(napi_env env) {
26
+ napi_value returnValue;
27
+ napi_get_undefined(env, &returnValue);
28
+ napi_throw_type_error(env, NULL, "Unexpected end of buffer reading string");
29
+ return returnValue;
30
+ }
31
+
28
32
  class Extractor {
29
33
  public:
30
34
  napi_value target[MAX_TARGET_SIZE + 1]; // leave one for the queued string
31
-
35
+ napi_ref targetArray;
36
+ bool hasTargetArray = false;
32
37
  uint8_t* source;
33
38
  int position = 0;
34
39
  int writePosition = 0;
35
40
  int stringStart = 0;
36
41
  int lastStringEnd = 0;
37
42
 
38
- void readString(Env env, int length, bool allowStringBlocks) {
43
+ void readString(napi_env env, int length, bool allowStringBlocks) {
39
44
  int start = position;
40
45
  int end = position + length;
41
46
  if (allowStringBlocks) { // for larger strings, we don't bother to check every character for being latin, and just go right to creating a new string
@@ -76,7 +81,7 @@ public:
76
81
  lastStringEnd = end;
77
82
  }
78
83
 
79
- napi_value extractStrings(Env env, int startingPosition, int size, uint8_t* inputSource) {
84
+ napi_value extractStrings(napi_env env, int startingPosition, int size, uint8_t* inputSource, napi_value array) {
80
85
  writePosition = 0;
81
86
  lastStringEnd = 0;
82
87
  position = startingPosition;
@@ -89,8 +94,7 @@ public:
89
94
  // fixstr, we want to convert this
90
95
  token -= 0xa0;
91
96
  if (token + position > size) {
92
- TypeError::New(env, "Unexpected end of buffer reading string").ThrowAsJavaScriptException();
93
- return env.Null();
97
+ return unexpectedEnd(env);
94
98
  }
95
99
  readString(env,token, true);
96
100
  if (writePosition >= MAX_TARGET_SIZE)
@@ -98,39 +102,33 @@ public:
98
102
  } else if (token <= 0xdb && token >= 0xd9) {
99
103
  if (token == 0xd9) { //str 8
100
104
  if (position >= size) {
101
- TypeError::New(env, "Unexpected end of buffer reading string").ThrowAsJavaScriptException();
102
- return env.Null();
105
+ return unexpectedEnd(env);
103
106
  }
104
107
  int length = source[position++];
105
108
  if (length + position > size) {
106
- TypeError::New(env, "Unexpected end of buffer reading string").ThrowAsJavaScriptException();
107
- return env.Null();
109
+ return unexpectedEnd(env);
108
110
  }
109
111
  readString(env,length, true);
110
112
  } else if (token == 0xda) { //str 16
111
113
  if (2 + position > size) {
112
- TypeError::New(env, "Unexpected end of buffer reading string").ThrowAsJavaScriptException();
113
- return env.Null();
114
+ return unexpectedEnd(env);
114
115
  }
115
116
  int length = source[position++] << 8;
116
117
  length += source[position++];
117
118
  if (length + position > size) {
118
- TypeError::New(env, "Unexpected end of buffer reading string").ThrowAsJavaScriptException();
119
- return env.Null();
119
+ return unexpectedEnd(env);
120
120
  }
121
121
  readString(env,length, false);
122
122
  } else { //str 32
123
123
  if (4 + position > size) {
124
- TypeError::New(env, "Unexpected end of buffer reading string").ThrowAsJavaScriptException();
125
- return env.Null();
124
+ return unexpectedEnd(env);
126
125
  }
127
126
  int length = source[position++] << 24;
128
127
  length += source[position++] << 16;
129
128
  length += source[position++] << 8;
130
129
  length += source[position++];
131
130
  if (length + position > size) {
132
- TypeError::New(env, "Unexpected end of buffer reading string").ThrowAsJavaScriptException();
133
- return env.Null();
131
+ return unexpectedEnd(env);
134
132
  }
135
133
  readString(env, length, false);
136
134
  }
@@ -143,8 +141,7 @@ public:
143
141
  } else {
144
142
  position = tokenTable[token](source, position, size);
145
143
  if (position < 0) {
146
- TypeError::New(env, "Unexpected end of buffer").ThrowAsJavaScriptException();
147
- return env.Null();
144
+ return unexpectedEnd(env);
148
145
  }
149
146
  }
150
147
  }
@@ -154,20 +151,36 @@ public:
154
151
  napi_value value;
155
152
  napi_create_string_latin1(env, (const char*) source + stringStart, lastStringEnd - stringStart, &value);
156
153
  if (writePosition == 0) {
154
+ if (!hasTargetArray) {
155
+ hasTargetArray = true;
156
+ napi_create_reference(env, array, 1, &targetArray);
157
+ }
157
158
  return value;
158
159
  }
159
160
  target[writePosition++] = value;
160
161
  } else if (writePosition == 1) {
162
+ if (!hasTargetArray) {
163
+ hasTargetArray = true;
164
+ napi_create_reference(env, array, 1, &targetArray);
165
+ }
161
166
  return target[0];
162
167
  }
163
- napi_value array;
164
- napi_create_array_with_length(env, writePosition, &array);
165
- //Array array = Array::New(env, writePosition);
166
- for (int i = 0; i < writePosition; i++) {
168
+ //napi_value array;
169
+ //napi_get_reference_value(env, targetArray, &array);
170
+ //napi_create_array_with_length(env, writePosition, &array);
171
+ if (hasTargetArray) {
172
+ napi_get_reference_value(env, targetArray, &array);
173
+ hasTargetArray = false;
174
+ }
175
+ int i = 0;
176
+ for (i = 0; i < writePosition; i++) {
167
177
  napi_set_element(env, array, i, target[i]);
168
- //array.Set(i, target[i]);
169
178
  }
170
- return array;
179
+ napi_value length;
180
+ napi_create_int32(env, i, &length);
181
+ return length;/*
182
+ napi_set_element(env, array, i, undefined);
183
+ return array;*/
171
184
  }
172
185
  };
173
186
 
@@ -247,29 +260,25 @@ void setupTokenTable() {
247
260
  return position + length;
248
261
  });
249
262
  }
250
-
251
263
  static thread_local Extractor* extractor;
252
-
253
- napi_value extractStrings(const CallbackInfo& info) {
254
- Env env = info.Env();
255
- int position = info[0].As<Number>();
256
- int size = info[1].As<Number>();
257
- if (info[2].IsTypedArray()) {
258
- TypedArray typedArray = info[2].As<TypedArray>();
259
- uint8_t* source = ((uint8_t*) typedArray.ArrayBuffer().Data()) + typedArray.ByteOffset();
260
- return extractor->extractStrings(env, position, size, source);
261
- }
262
- return env.Undefined();
264
+ napi_value extractStrings(napi_env env, napi_callback_info info) {
265
+ size_t argc = extractor->hasTargetArray ? 3 : 4;
266
+ napi_value args[4];
267
+ napi_get_cb_info(env, info, &argc, args, NULL, NULL);
268
+ uint32_t position;
269
+ uint32_t size;
270
+ napi_get_value_uint32(env, args[0], &position);
271
+ napi_get_value_uint32(env, args[1], &size);
272
+ uint8_t* source;
273
+ napi_get_typedarray_info(env, args[2], NULL, NULL, (void**) &source, NULL, NULL);
274
+ return extractor->extractStrings(env, position, size, source, args[3]);
263
275
  }
264
-
265
- Object Init(Env env, Object exports) {
276
+ #define EXPORT_NAPI_FUNCTION(name, func) { napi_property_descriptor desc = { name, 0, func, 0, 0, 0, (napi_property_attributes) (napi_writable | napi_configurable), 0 }; napi_define_properties(env, exports, 1, &desc); }
277
+ napi_value Init(napi_env env, napi_value exports) {
266
278
  extractor = new Extractor(); // create our thread-local extractor
267
279
  setupTokenTable();
268
- exports.Set(
269
- String::New(env, "extractStrings"),
270
- Function::New(env, extractStrings)
271
- );
280
+ EXPORT_NAPI_FUNCTION("extractStrings", extractStrings);
272
281
  return exports;
273
282
  }
274
283
 
275
- NODE_API_MODULE(extractor, Init)
284
+ NAPI_MODULE(extractor, Init)
Binary file