cbor-extract 0.3.1 → 2.0.0

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/binding.gyp CHANGED
@@ -1,18 +1,29 @@
1
1
  {
2
2
  "variables": {
3
3
  "os_linux_compiler%": "gcc",
4
+ "enable_v8%": "true",
5
+ "enable_pointer_compression%": "false",
4
6
  "build_v8_with_gn": "false"
5
7
  },
8
+ "conditions": [
9
+ ['OS=="win"', {
10
+ "variables": {
11
+ "enable_v8%": "<!(echo %ENABLE_V8_FUNCTIONS%)",
12
+ }
13
+ }],
14
+ ['OS!="win"', {
15
+ "variables": {
16
+ "enable_v8%": "<!(echo $ENABLE_V8_FUNCTIONS)",
17
+ }
18
+ }]
19
+ ],
6
20
  "targets": [
7
21
  {
8
- "target_name": "cbor-extract",
9
- "win_delay_load_hook": "false",
22
+ "target_name": "extract",
10
23
  "sources": [
11
24
  "src/extract.cpp",
12
25
  ],
13
- "include_dirs": [
14
- "<!(node -e \"require('nan')\")",
15
- ],
26
+ "defines": [ "NAPI_DISABLE_CPP_EXCEPTIONS" ],
16
27
  "conditions": [
17
28
  ["OS=='linux'", {
18
29
  "variables": {
@@ -40,6 +51,9 @@
40
51
  "-O3"
41
52
  ],
42
53
  }],
54
+ ["enable_v8!='false'", {
55
+ "defines": ["ENABLE_V8_API=1"]
56
+ }],
43
57
  ],
44
58
  }
45
59
  ]
package/index.js CHANGED
@@ -1 +1 @@
1
- module.exports = require('node-gyp-build')(__dirname)
1
+ module.exports = require('node-gyp-build-optional-packages')(__dirname)
package/package.json CHANGED
@@ -1,22 +1,42 @@
1
- {
2
- "name": "cbor-extract",
3
- "author": "Kris Zyp",
4
- "version": "0.3.1",
5
- "description": "Node addon for string extraction for cbor-x",
6
- "license": "MIT",
7
- "repository": {
8
- "type": "git",
9
- "url": "http://github.com/kriszyp/cbor-extract"
10
- },
11
- "scripts": {
12
- "install": "node-gyp-build",
13
- "recompile": "node-gyp build",
14
- "test": "./node_modules/.bin/mocha tests/test*.js -u tdd"
15
- },
16
- "main": "./index.js",
17
- "gypfile": true,
18
- "dependencies": {
19
- "nan": "^2.14.2",
20
- "node-gyp-build": "^4.2.3"
21
- }
22
- }
1
+ {
2
+ "name": "cbor-extract",
3
+ "author": "Kris Zyp",
4
+ "version": "2.0.0",
5
+ "description": "Node addon for string extraction for cbor-x",
6
+ "license": "MIT",
7
+ "repository": {
8
+ "type": "git",
9
+ "url": "http://github.com/kriszyp/cbor-extract"
10
+ },
11
+ "scripts": {
12
+ "install": "node-gyp-build-optional-packages",
13
+ "recompile": "node-gyp clean && node-gyp configure && node-gyp build",
14
+ "before-publish": "prebuildify-ci download && prebuildify --target 16.14.2 && set ENABLE_V8_FUNCTIONS=false&& prebuildify --platform-packages --napi --target 16.14.2 && node set-optional-deps.cjs",
15
+ "prebuild": "prebuildify --target 16.14.2",
16
+ "prebuild-libc": "prebuildify --tag-libc --target 16.14.2 && ENABLE_V8_FUNCTIONS=false prebuildify --platform-packages --napi --tag-libc --target 16.14.2",
17
+ "publish-all": "cd prebuilds/win32-x64 && npm publish --access public && cd ../darwin-x64 && npm publish --access public && cd ../darwin-arm64 && npm publish --access public && cd ../linux-x64 && npm publish --access public && cd ../linux-arm64 && npm publish --access public && cd ../linux-arm && npm publish --access public && cd ../.. && npm publish --access public",
18
+ "test": "node ./index.js"
19
+ },
20
+ "main": "./index.js",
21
+ "gypfile": true,
22
+ "dependencies": {
23
+ "node-gyp-build-optional-packages": "5.0.1"
24
+ },
25
+ "files": [
26
+ "index.js",
27
+ "/src",
28
+ "/*.gyp"
29
+ ],
30
+ "devDependencies": {
31
+ "prebuildify": "kriszyp/prebuildify#optional-packages",
32
+ "prebuildify-ci": "^1.0.5"
33
+ },
34
+ "optionalDependencies": {
35
+ "@cbor-extract/cbor-extract-darwin-arm64": "2.0.0",
36
+ "@cbor-extract/cbor-extract-darwin-x64": "2.0.0",
37
+ "@cbor-extract/cbor-extract-linux-arm": "2.0.0",
38
+ "@cbor-extract/cbor-extract-linux-arm64": "2.0.0",
39
+ "@cbor-extract/cbor-extract-linux-x64": "2.0.0",
40
+ "@cbor-extract/cbor-extract-win32-x64": "2.0.0"
41
+ }
42
+ }
package/src/extract.cpp CHANGED
@@ -5,11 +5,10 @@ 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 CBOR 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 <node_api.h>
9
+ #if ENABLE_V8_API
8
10
  #include <v8.h>
9
- #include <node.h>
10
- #include <node_buffer.h>
11
- #include <nan.h>
12
- using namespace v8;
11
+ #endif
13
12
 
14
13
  #ifndef thread_local
15
14
  #ifdef __GNUC__
@@ -24,18 +23,23 @@ using namespace v8;
24
23
  #endif
25
24
 
26
25
  const int MAX_TARGET_SIZE = 255;
27
-
26
+ napi_value unexpectedEnd(napi_env env) {
27
+ napi_value returnValue;
28
+ napi_get_undefined(env, &returnValue);
29
+ napi_throw_type_error(env, NULL, "Unexpected end of buffer reading string");
30
+ return returnValue;
31
+ }
28
32
  class Extractor {
29
33
  public:
30
- v8::Local<v8::Value> target[MAX_TARGET_SIZE + 1]; // leave one for the queued string
34
+ napi_value target[MAX_TARGET_SIZE + 1]; // leave one for the queued string
31
35
 
32
36
  uint8_t* source;
33
37
  int position = 0;
34
38
  int writePosition = 0;
35
39
  int stringStart = 0;
36
40
  int lastStringEnd = 0;
37
- Isolate *isolate = Isolate::GetCurrent();
38
- void readString(int length, bool allowStringBlocks) {
41
+
42
+ void readString(napi_env env, int length, bool allowStringBlocks) {
39
43
  int start = position;
40
44
  int end = position + length;
41
45
  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
@@ -50,18 +54,24 @@ public:
50
54
  if (position < end) {
51
55
  // non-latin character
52
56
  if (lastStringEnd) {
53
- target[writePosition++] = String::NewFromOneByte(isolate, (uint8_t*) source + stringStart, v8::NewStringType::kNormal, lastStringEnd - stringStart).ToLocalChecked();
57
+ napi_value value;
58
+ napi_create_string_latin1(env, (const char*) source + stringStart, lastStringEnd - stringStart, &value);
59
+ target[writePosition++] = value;
54
60
  lastStringEnd = 0;
55
61
  }
56
62
  // use standard utf-8 conversion
57
- target[writePosition++] = Nan::New<v8::String>((char*) source + start, (int) length).ToLocalChecked();
63
+ napi_value value;
64
+ napi_create_string_utf8(env, (const char*) source + start, (int) length, &value);
65
+ target[writePosition++] = value;
58
66
  position = end;
59
67
  return;
60
68
  }
61
69
 
62
70
  if (lastStringEnd) {
63
71
  if (start - lastStringEnd > 40 || end - stringStart > 6000) {
64
- target[writePosition++] = String::NewFromOneByte(isolate, (uint8_t*) source + stringStart, v8::NewStringType::kNormal, lastStringEnd - stringStart).ToLocalChecked();
72
+ napi_value value;
73
+ napi_create_string_latin1(env, (const char*) source + stringStart, lastStringEnd - stringStart, &value);
74
+ target[writePosition++] = value;
65
75
  stringStart = start;
66
76
  }
67
77
  } else {
@@ -69,13 +79,12 @@ public:
69
79
  }
70
80
  lastStringEnd = end;
71
81
  }
72
-
73
- Local<Value> extractStrings(int startingPosition, int size, int firstStringSize, uint8_t* inputSource) {
82
+ napi_value extractStrings(napi_env env, int startingPosition, int size, int firstStringSize, uint8_t* inputSource) {
74
83
  writePosition = 0;
75
84
  lastStringEnd = 0;
76
85
  position = startingPosition;
77
86
  source = inputSource;
78
- readString(firstStringSize, firstStringSize < 0x100);
87
+ readString(env, firstStringSize, firstStringSize < 0x100);
79
88
  while (position < size) {
80
89
  uint8_t token = source[position++];
81
90
  uint8_t majorType = token >> 5;
@@ -85,23 +94,20 @@ public:
85
94
  switch (token) {
86
95
  case 0x18:
87
96
  if (position + 1 > size) {
88
- Nan::ThrowError("Unexpected end of buffer reading string");
89
- return Nan::Null();
97
+ return unexpectedEnd(env);
90
98
  }
91
99
  length = source[position++];
92
100
  break;
93
101
  case 0x19:
94
102
  if (position + 2 > size) {
95
- Nan::ThrowError("Unexpected end of buffer reading string");
96
- return Nan::Null();
103
+ return unexpectedEnd(env);
97
104
  }
98
105
  length = source[position++] << 8;
99
106
  length += source[position++];
100
107
  break;
101
108
  case 0x1a:
102
109
  if (position + 4 > size) {
103
- Nan::ThrowError("Unexpected end of buffer reading string");
104
- return Nan::Null();
110
+ return unexpectedEnd(env);
105
111
  }
106
112
  length = source[position++] << 24;
107
113
  length += source[position++] << 16;
@@ -109,19 +115,16 @@ public:
109
115
  length += source[position++];
110
116
  break;
111
117
  case 0x1b:
112
- Nan::ThrowError("Unexpected end of buffer reading string");
113
- return Nan::Null();
114
- break;
118
+ return unexpectedEnd(env);
115
119
  default:
116
120
  length = token;
117
121
  }
118
122
  if (majorType == 3) {
119
123
  // string
120
124
  if (length + position > size) {
121
- Nan::ThrowError("Unexpected end of buffer reading string");
122
- return Nan::Null();
125
+ return unexpectedEnd(env);
123
126
  }
124
- readString(length, length < 0x100);
127
+ readString(env, length, length < 0x100);
125
128
  if (writePosition >= MAX_TARGET_SIZE)
126
129
  break;
127
130
  } else { // binary data
@@ -145,48 +148,50 @@ public:
145
148
  }
146
149
  }
147
150
  }
148
-
149
151
  if (lastStringEnd) {
150
- if (writePosition == 0)
151
- return String::NewFromOneByte(isolate, (uint8_t*) source + stringStart, v8::NewStringType::kNormal, lastStringEnd - stringStart).ToLocalChecked();
152
- target[writePosition++] = String::NewFromOneByte(isolate, (uint8_t*) source + stringStart, v8::NewStringType::kNormal, lastStringEnd - stringStart).ToLocalChecked();
152
+ napi_value value;
153
+ napi_create_string_latin1(env, (const char*) source + stringStart, lastStringEnd - stringStart, &value);
154
+ if (writePosition == 0) {
155
+ return value;
156
+ }
157
+ target[writePosition++] = value;
153
158
  } else if (writePosition == 1) {
154
159
  return target[0];
155
160
  }
156
- #if NODE_VERSION_AT_LEAST(12,0,0)
157
- return Array::New(isolate, target, writePosition);
158
- #else
159
- Local<Array> array = Array::New(isolate, writePosition);
160
- Local<Context> context = Nan::GetCurrentContext();
161
+ napi_value array;
162
+ #if ENABLE_V8_API
163
+ v8::Local<v8::Array> v8Array = v8::Array::New(v8::Isolate::GetCurrent(), (v8::Local<v8::Value>*) target, writePosition);
164
+ memcpy(&array, &v8Array, sizeof(array));
165
+ #else
166
+ napi_create_array_with_length(env, writePosition, &array);
161
167
  for (int i = 0; i < writePosition; i++) {
162
- array->Set(context, i, target[i]);
168
+ napi_set_element(env, array, i, target[i]);
163
169
  }
170
+ #endif
164
171
  return array;
165
- #endif
166
172
  }
167
173
  };
168
174
 
169
175
  static thread_local Extractor* extractor;
170
176
 
171
- NAN_METHOD(extractStrings) {
172
- Local<Context> context = Nan::GetCurrentContext();
173
- int position = Local<Number>::Cast(info[0])->IntegerValue(context).FromJust();
174
- int size = Local<Number>::Cast(info[1])->IntegerValue(context).FromJust();
175
- int firstStringSize = Local<Number>::Cast(info[2])->IntegerValue(context).FromJust();
176
- if (info[3]->IsArrayBufferView()) {
177
- uint8_t* source = (uint8_t*) node::Buffer::Data(info[3]);
178
- info.GetReturnValue().Set(extractor->extractStrings(position, size, firstStringSize, source));
179
- }
180
- }
181
-
182
- NAN_METHOD(isOneByte) {
183
- info.GetReturnValue().Set(Nan::New<Boolean>(Local<String>::Cast(info[0])->IsOneByte()));
177
+ napi_value extractStrings(napi_env env, napi_callback_info info) {
178
+ size_t argc = 4;
179
+ napi_value args[4];
180
+ napi_get_cb_info(env, info, &argc, args, NULL, NULL);
181
+ uint32_t position;
182
+ uint32_t size;
183
+ uint32_t firstStringSize;
184
+ napi_get_value_uint32(env, args[0], &position);
185
+ napi_get_value_uint32(env, args[1], &size);
186
+ napi_get_value_uint32(env, args[2], &firstStringSize);
187
+ uint8_t* source;
188
+ napi_get_typedarray_info(env, args[3], NULL, NULL, (void**) &source, NULL, NULL);
189
+ return extractor->extractStrings(env, position, size, firstStringSize, source);
184
190
  }
191
+ #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); }
185
192
 
186
- void initializeModule(v8::Local<v8::Object> exports) {
193
+ NAPI_MODULE_INIT() {
187
194
  extractor = new Extractor(); // create our thread-local extractor
188
- Nan::SetMethod(exports, "extractStrings", extractStrings);
189
- Nan::SetMethod(exports, "isOneByte", isOneByte);
190
- }
191
-
192
- NODE_MODULE_CONTEXT_AWARE(extractor, initializeModule);
195
+ EXPORT_NAPI_FUNCTION("extractStrings", extractStrings);
196
+ return exports;
197
+ }
Binary file
Binary file
Binary file
Binary file