asherah 3.0.3 → 3.0.5

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
@@ -14,19 +14,21 @@
14
14
  '-g',
15
15
  '-O3',
16
16
  '-std=c++17',
17
- '-fPIC'
17
+ '-fPIC',
18
+ '-Wno-unknown-pragmas'
18
19
  ],
19
20
  },
20
21
  'defines': [
21
22
  'NAPI_CPP_EXCEPTIONS',
22
23
  'NODE_API_SWALLOW_UNTHROWABLE_EXCEPTIONS',
23
24
  'NODE_ADDON_API_DISABLE_DEPRECATED',
24
- 'NODE_API_NO_EXTERNAL_BUFFERS_ALLOWED'
25
+ 'NODE_API_NO_EXTERNAL_BUFFERS_ALLOWED',
26
+ 'USE_SCOPED_ALLOCATE_BUFFER',
25
27
  ],
26
28
  'sources': [
27
29
  'src/asherah.cc',
28
- 'src/logging.cc',
29
- 'src/logging_napi.cc'
30
+ 'src/logging_napi.cc',
31
+ 'src/logging_stderr.cc'
30
32
  ],
31
33
  'libraries': [ '../lib/libasherah.a' ]
32
34
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "asherah",
3
- "version": "3.0.3",
3
+ "version": "3.0.5",
4
4
  "description": "Asherah envelope encryption and key rotation library",
5
5
  "exports": {
6
6
  "node-addons": "./dist/asherah.node"
@@ -11,7 +11,7 @@
11
11
  },
12
12
  "scripts": {
13
13
  "preinstall": "scripts/download-libraries.sh",
14
- "load": "node --max-old-space-size=500 scripts/dumpster-fire.js",
14
+ "load": "node --max-old-space-size=500 scripts/dumpster-fire-memory.js",
15
15
  "install": "scripts/build.sh",
16
16
  "test:mocha-debug": "lldb -o run -- node node_modules/mocha/bin/mocha --inspect-brk",
17
17
  "test:mocha": "mocha",
@@ -30,10 +30,11 @@
30
30
  "src/cobhan_buffer_napi.h",
31
31
  "src/cobhan_buffer.h",
32
32
  "src/hints.h",
33
- "src/logging.cc",
34
33
  "src/logging.h",
35
34
  "src/logging_napi.cc",
36
35
  "src/logging_napi.h",
36
+ "src/logging_stderr.cc",
37
+ "src/logging_stderr.h",
37
38
  "src/napi_utils.h",
38
39
  "src/scoped_allocate.h",
39
40
  "src/asherah.d.ts",
@@ -70,6 +71,6 @@
70
71
  },
71
72
  "types": "dist/asherah.d.ts",
72
73
  "dependencies": {
73
- "node-addon-api": "7.0.0"
74
+ "node-addon-api": "^7.0.0"
74
75
  }
75
76
  }
package/scripts/build.sh CHANGED
@@ -1,3 +1,16 @@
1
1
  #!/bin/bash
2
2
 
3
- node-gyp configure && node-gyp build && mkdir -p dist/ && cp build/Release/asherah.node dist/asherah.node && cp src/asherah.d.ts dist/asherah.d.ts
3
+ set -e
4
+
5
+ mkdir -p dist/
6
+
7
+ # Check if USE_CMAKE is set to 1
8
+ if [ "$USE_CMAKE" = "1" ]; then
9
+ # Run CMake commands
10
+ (cmake . && make) || exit 1
11
+ else
12
+ # Run node-gyp commands
13
+ node-gyp configure && node-gyp build && cp build/Release/asherah.node dist/asherah.node
14
+ fi
15
+
16
+ cp src/asherah.d.ts dist/asherah.d.ts
package/src/asherah.cc CHANGED
@@ -1,21 +1,16 @@
1
+ #pragma clang diagnostic push
2
+ #pragma ide diagnostic ignored "readability-convert-member-functions-to-static"
1
3
 
2
- #define USE_SCOPE_ALLOCATE_BUFFER 1
3
4
  #include "asherah_async_worker.h"
4
5
  #include "cobhan_buffer_napi.h"
5
6
  #include "hints.h"
6
7
  #include "libasherah.h"
7
8
  #include "logging_napi.h"
8
9
  #include "napi_utils.h"
9
- #ifdef USE_SCOPED_ALLOCATE_BUFFER
10
10
  #include "scoped_allocate.h"
11
- #endif
12
11
  #include <atomic>
13
12
  #include <napi.h>
14
13
 
15
- #ifndef NAPI_CPP_EXCEPTIONS
16
- #error Support for C++ exceptions is required
17
- #endif
18
-
19
14
  static volatile std::atomic<int32_t> setup_state{0};
20
15
 
21
16
  class Asherah : public Napi::Addon<Asherah> {
@@ -71,7 +66,7 @@ private:
71
66
  char *config_cbuffer;
72
67
  size_t config_cbuffer_size =
73
68
  CobhanBufferNapi::StringToAllocationSize(env, config_string);
74
- SCOPED_ALLOCATE_BUFFER(logger, config_cbuffer, config_cbuffer_size,
69
+ SCOPED_ALLOCATE_BUFFER(config_cbuffer, config_cbuffer_size,
75
70
  maximum_stack_alloc_size, __func__);
76
71
 
77
72
  CobhanBufferNapi config(env, config_string, config_cbuffer,
@@ -82,9 +77,13 @@ private:
82
77
 
83
78
  // extern GoInt32 SetupJson(void* configJson);
84
79
  GoInt32 result = SetupJson(config);
85
- EndSetupAsherah(result, product_id_length, service_name_length);
80
+ EndSetupAsherah(env, result, product_id_length, service_name_length);
81
+ } catch (Napi::Error &e) {
82
+ e.ThrowAsJavaScriptException();
83
+ return;
86
84
  } catch (const std::exception &e) {
87
- logger.log_error_and_throw(__func__, e.what());
85
+ Napi::Error::New(env, e.what()).ThrowAsJavaScriptException();
86
+ return;
88
87
  }
89
88
  }
90
89
 
@@ -105,8 +104,12 @@ private:
105
104
  service_name_length);
106
105
  worker->Queue();
107
106
  return worker->Promise();
107
+ } catch (Napi::Error &e) {
108
+ e.ThrowAsJavaScriptException();
109
+ return env.Undefined();
108
110
  } catch (const std::exception &e) {
109
- logger.log_error_and_throw(__func__, e.what());
111
+ Napi::Error::New(env, e.what()).ThrowAsJavaScriptException();
112
+ return env.Undefined();
110
113
  }
111
114
  }
112
115
 
@@ -116,9 +119,13 @@ private:
116
119
  try {
117
120
  BeginShutdownAsherah(env, __func__, info);
118
121
  Shutdown();
119
- EndShutdownAsherah();
122
+ EndShutdownAsherah(env);
123
+ } catch (Napi::Error &e) {
124
+ e.ThrowAsJavaScriptException();
125
+ return;
120
126
  } catch (const std::exception &e) {
121
- logger.log_error_and_throw(__func__, e.what());
127
+ Napi::Error::New(env, e.what()).ThrowAsJavaScriptException();
128
+ return;
122
129
  }
123
130
  }
124
131
 
@@ -130,8 +137,12 @@ private:
130
137
  auto worker = new ShutdownAsherahWorker(env, this);
131
138
  worker->Queue();
132
139
  return worker->Promise();
140
+ } catch (Napi::Error &e) {
141
+ e.ThrowAsJavaScriptException();
142
+ return env.Undefined();
133
143
  } catch (const std::exception &e) {
134
- logger.log_error_and_throw(__func__, e.what());
144
+ Napi::Error::New(env, e.what()).ThrowAsJavaScriptException();
145
+ return env.Undefined();
135
146
  }
136
147
  }
137
148
 
@@ -150,14 +161,13 @@ private:
150
161
  char *partition_id_cbuffer;
151
162
  size_t partition_id_cbuffer_size =
152
163
  CobhanBufferNapi::StringToAllocationSize(env, partition_id_string);
153
- SCOPED_ALLOCATE_BUFFER(logger, partition_id_cbuffer,
154
- partition_id_cbuffer_size,
164
+ SCOPED_ALLOCATE_BUFFER(partition_id_cbuffer, partition_id_cbuffer_size,
155
165
  maximum_stack_alloc_size, __func__);
156
166
 
157
167
  char *input_cbuffer;
158
168
  size_t input_cbuffer_size =
159
169
  CobhanBufferNapi::ValueToAllocationSize(env, input_value);
160
- SCOPED_ALLOCATE_BUFFER(logger, input_cbuffer, input_cbuffer_size,
170
+ SCOPED_ALLOCATE_BUFFER(input_cbuffer, input_cbuffer_size,
161
171
  maximum_stack_alloc_size, __func__);
162
172
 
163
173
  CobhanBufferNapi partition_id(env, partition_id_string,
@@ -179,19 +189,22 @@ private:
179
189
  char *output_cobhan_buffer;
180
190
  size_t output_size_bytes =
181
191
  CobhanBuffer::DataSizeToAllocationSize(asherah_output_size_bytes);
182
- SCOPED_ALLOCATE_BUFFER(logger, output_cobhan_buffer, output_size_bytes,
192
+ SCOPED_ALLOCATE_BUFFER(output_cobhan_buffer, output_size_bytes,
183
193
  maximum_stack_alloc_size, __func__);
184
- CobhanBufferNapi output(output_cobhan_buffer, output_size_bytes);
194
+ CobhanBufferNapi output(env, output_cobhan_buffer, output_size_bytes);
185
195
  #else
186
- CobhanBufferNapi output(asherah_output_size_bytes);
196
+ CobhanBufferNapi output(env, asherah_output_size_bytes);
187
197
  #endif
188
198
 
189
199
  GoInt32 result = EncryptToJson(partition_id, input, output);
190
200
 
191
201
  EndEncryptToJson(env, output, result, output_string);
192
-
202
+ } catch (Napi::Error &e) {
203
+ e.ThrowAsJavaScriptException();
204
+ return env.Undefined();
193
205
  } catch (const std::exception &e) {
194
- logger.log_error_and_throw(__func__, e.what());
206
+ Napi::Error::New(env, e.what()).ThrowAsJavaScriptException();
207
+ return env.Undefined();
195
208
  }
196
209
 
197
210
  return output_string;
@@ -214,14 +227,18 @@ private:
214
227
  size_t asherah_output_size_bytes = EstimateAsherahOutputSize(
215
228
  input_data_len_bytes, partition_id_data_len_bytes);
216
229
 
217
- CobhanBufferNapi output(asherah_output_size_bytes);
230
+ CobhanBufferNapi output(env, asherah_output_size_bytes);
218
231
 
219
232
  auto worker =
220
233
  new EncryptAsherahWorker(env, this, partition_id, input, output);
221
234
  worker->Queue();
222
235
  return worker->Promise();
236
+ } catch (Napi::Error &e) {
237
+ e.ThrowAsJavaScriptException();
238
+ return env.Undefined();
223
239
  } catch (const std::exception &e) {
224
- logger.log_error_and_throw(__func__, e.what());
240
+ Napi::Error::New(env, e.what()).ThrowAsJavaScriptException();
241
+ return env.Undefined();
225
242
  }
226
243
  }
227
244
 
@@ -240,14 +257,13 @@ private:
240
257
  char *partition_id_cbuffer;
241
258
  size_t partition_id_cbuffer_size =
242
259
  CobhanBufferNapi::StringToAllocationSize(env, partition_id_string);
243
- SCOPED_ALLOCATE_BUFFER(logger, partition_id_cbuffer,
244
- partition_id_cbuffer_size,
260
+ SCOPED_ALLOCATE_BUFFER(partition_id_cbuffer, partition_id_cbuffer_size,
245
261
  maximum_stack_alloc_size, __func__);
246
262
 
247
263
  char *input_cbuffer;
248
264
  size_t input_cbuffer_size =
249
265
  CobhanBufferNapi::ValueToAllocationSize(env, input_value);
250
- SCOPED_ALLOCATE_BUFFER(logger, input_cbuffer, input_cbuffer_size,
266
+ SCOPED_ALLOCATE_BUFFER(input_cbuffer, input_cbuffer_size,
251
267
  maximum_stack_alloc_size, __func__);
252
268
 
253
269
  CobhanBufferNapi partition_id(env, partition_id_string,
@@ -259,25 +275,30 @@ private:
259
275
  char *output_cobhan_buffer;
260
276
  size_t output_size_bytes =
261
277
  CobhanBuffer::DataSizeToAllocationSize(input.get_data_len_bytes());
262
- SCOPED_ALLOCATE_BUFFER(logger, output_cobhan_buffer, output_size_bytes,
278
+ SCOPED_ALLOCATE_BUFFER(output_cobhan_buffer, output_size_bytes,
263
279
  maximum_stack_alloc_size, __func__);
264
- CobhanBufferNapi output(output_cobhan_buffer, output_size_bytes);
280
+ CobhanBufferNapi output(env, output_cobhan_buffer, output_size_bytes);
265
281
  #else
266
282
  CobhanBufferNapi partition_id(env, partition_id_string);
267
283
  CobhanBufferNapi input(env, input_value);
268
- CobhanBufferNapi output(input.get_data_len_bytes());
284
+ CobhanBufferNapi output(env, input.get_data_len_bytes());
269
285
  #endif
270
286
 
271
287
  // extern GoInt32 DecryptFromJson(void* partitionIdPtr, void* jsonPtr,
272
288
  // void* dataPtr);
273
289
  GoInt32 result = DecryptFromJson(partition_id, input, output);
274
290
 
275
- CheckResult(result);
291
+ CheckResult(env, result);
276
292
 
277
- output_value = output.ToBuffer(env); // NOLINT(*-slicing)
293
+ output_value = output.ToBuffer(); // NOLINT(*-slicing)
294
+ } catch (Napi::Error &e) {
295
+ e.ThrowAsJavaScriptException();
296
+ return env.Undefined();
278
297
  } catch (const std::exception &e) {
279
- logger.log_error_and_throw(__func__, e.what());
298
+ Napi::Error::New(env, e.what()).ThrowAsJavaScriptException();
299
+ return env.Undefined();
280
300
  }
301
+
281
302
  return output_value;
282
303
  }
283
304
 
@@ -293,13 +314,17 @@ private:
293
314
  CobhanBufferNapi partition_id(env, partition_id_string);
294
315
  CobhanBufferNapi input(env, input_value);
295
316
 
296
- CobhanBufferNapi output(input.get_data_len_bytes());
317
+ CobhanBufferNapi output(env, input.get_data_len_bytes());
297
318
  auto worker = new DecryptFromJsonWorker<Napi::Buffer<unsigned char>>(
298
319
  env, this, partition_id, input, output);
299
320
  worker->Queue();
300
321
  return worker->Promise();
322
+ } catch (Napi::Error &e) {
323
+ e.ThrowAsJavaScriptException();
324
+ return env.Undefined();
301
325
  } catch (const std::exception &e) {
302
- logger.log_error_and_throw(__func__, e.what());
326
+ Napi::Error::New(env, e.what()).ThrowAsJavaScriptException();
327
+ return env.Undefined();
303
328
  }
304
329
  }
305
330
 
@@ -310,10 +335,6 @@ private:
310
335
  try {
311
336
  NapiUtils::RequireParameterCount(info, 2);
312
337
 
313
- if (unlikely(!info[0].IsString() || !info[1].IsString())) {
314
- logger.log_error_and_throw(__func__, "Wrong argument types");
315
- }
316
-
317
338
  Napi::String partition_id_string;
318
339
  Napi::Value input_value;
319
340
  BeginDecryptFromJson(env, __func__, info, partition_id_string,
@@ -323,14 +344,13 @@ private:
323
344
  char *partition_id_cbuffer;
324
345
  size_t partition_id_cbuffer_size =
325
346
  CobhanBufferNapi::StringToAllocationSize(env, partition_id_string);
326
- SCOPED_ALLOCATE_BUFFER(logger, partition_id_cbuffer,
327
- partition_id_cbuffer_size,
347
+ SCOPED_ALLOCATE_BUFFER(partition_id_cbuffer, partition_id_cbuffer_size,
328
348
  maximum_stack_alloc_size, __func__);
329
349
 
330
350
  char *input_cbuffer;
331
351
  size_t input_cbuffer_size =
332
352
  CobhanBufferNapi::ValueToAllocationSize(env, input_value);
333
- SCOPED_ALLOCATE_BUFFER(logger, input_cbuffer, input_cbuffer_size,
353
+ SCOPED_ALLOCATE_BUFFER(input_cbuffer, input_cbuffer_size,
334
354
  maximum_stack_alloc_size, __func__);
335
355
 
336
356
  CobhanBufferNapi partition_id(env, partition_id_string,
@@ -339,18 +359,22 @@ private:
339
359
  CobhanBufferNapi input(env, input_value, input_cbuffer,
340
360
  input_cbuffer_size);
341
361
 
342
- CobhanBufferNapi output(input.get_data_len_bytes());
362
+ CobhanBufferNapi output(env, input.get_data_len_bytes());
343
363
  #else
344
364
  CobhanBufferNapi partition_id(env, partition_id_string);
345
365
  CobhanBufferNapi input(env, input_value);
346
- CobhanBufferNapi output(input.get_data_len_bytes());
366
+ CobhanBufferNapi output(env, input.get_data_len_bytes());
347
367
  #endif
348
368
 
349
369
  GoInt32 result = DecryptFromJson(partition_id, input, output);
350
370
 
351
371
  EndDecryptFromJson(env, output, result, output_string);
372
+ } catch (Napi::Error &e) {
373
+ e.ThrowAsJavaScriptException();
374
+ return env.Undefined();
352
375
  } catch (const std::exception &e) {
353
- logger.log_error_and_throw(__func__, e.what());
376
+ Napi::Error::New(env, e.what()).ThrowAsJavaScriptException();
377
+ return env.Undefined();
354
378
  }
355
379
  return output_string;
356
380
  }
@@ -361,10 +385,6 @@ private:
361
385
  try {
362
386
  NapiUtils::RequireParameterCount(info, 2);
363
387
 
364
- if (unlikely(!info[0].IsString() || !info[1].IsString())) {
365
- logger.log_error_and_throw(__func__, "Wrong argument types");
366
- }
367
-
368
388
  Napi::String partition_id_string;
369
389
  Napi::Value input_value;
370
390
  BeginDecryptFromJson(env, __func__, info, partition_id_string,
@@ -373,58 +393,103 @@ private:
373
393
  CobhanBufferNapi partition_id(env, partition_id_string);
374
394
  CobhanBufferNapi input(env, input_value);
375
395
 
376
- CobhanBufferNapi output(input.get_data_len_bytes());
396
+ CobhanBufferNapi output(env, input.get_data_len_bytes());
377
397
 
378
398
  auto worker = new DecryptFromJsonWorker<Napi::String>(
379
399
  env, this, partition_id, input, output);
380
400
  worker->Queue();
381
401
 
382
402
  return worker->Promise();
403
+ } catch (Napi::Error &e) {
404
+ e.ThrowAsJavaScriptException();
405
+ return env.Undefined();
383
406
  } catch (const std::exception &e) {
384
- logger.log_error_and_throw(__func__, e.what());
407
+ Napi::Error::New(env, e.what()).ThrowAsJavaScriptException();
408
+ return env.Undefined();
385
409
  }
386
410
  }
387
411
 
388
412
  void SetMaxStackAllocItemSize(const Napi::CallbackInfo &info) {
389
- NapiUtils::RequireParameterCount(info, 1);
413
+ Napi::Env env = info.Env();
414
+ Napi::HandleScope scope(env);
415
+ try {
416
+ NapiUtils::RequireParameterCount(info, 1);
390
417
 
391
- Napi::Number item_size = info[0].ToNumber();
392
- auto new_size = (size_t)item_size.Int32Value();
418
+ Napi::Number item_size = info[0].ToNumber();
419
+ auto new_size = (size_t)item_size.Int32Value();
393
420
 
394
- maximum_stack_alloc_size = new_size;
421
+ maximum_stack_alloc_size = new_size;
422
+ } catch (Napi::Error &e) {
423
+ e.ThrowAsJavaScriptException();
424
+ return;
425
+ } catch (const std::exception &e) {
426
+ Napi::Error::New(env, e.what()).ThrowAsJavaScriptException();
427
+ return;
428
+ }
395
429
  }
396
430
 
397
431
  void SetSafetyPaddingOverhead(const Napi::CallbackInfo &info) {
398
- NapiUtils::RequireParameterCount(info, 1);
399
-
400
- // Napi::Number safety_padding_number = info[0].ToNumber();
401
- // auto new_safety_padding_bytes = (size_t)
402
- // safety_padding_number.Int32Value(); Safety padding size is now fixed -
403
- // ignore the input set_safety_padding_bytes(new_safety_padding_bytes);
432
+ Napi::Env env = info.Env();
433
+ Napi::HandleScope scope(env);
434
+ try {
435
+ NapiUtils::RequireParameterCount(info, 1);
436
+
437
+ // Napi::Number safety_padding_number = info[0].ToNumber();
438
+ // auto new_safety_padding_bytes = (size_t)
439
+ // safety_padding_number.Int32Value(); Safety padding size is now fixed -
440
+ // ignore the input set_safety_padding_bytes(new_safety_padding_bytes);
441
+ } catch (Napi::Error &e) {
442
+ e.ThrowAsJavaScriptException();
443
+ return;
444
+ } catch (const std::exception &e) {
445
+ Napi::Error::New(env, e.what()).ThrowAsJavaScriptException();
446
+ return;
447
+ }
404
448
  }
405
449
 
406
450
  Napi::Value
407
451
  GetSetupStatus(const Napi::CallbackInfo
408
452
  &info) { // NOLINT(*-convert-member-functions-to-static)
409
- int32_t setup_status = setup_state.load(std::memory_order_acquire);
410
- return Napi::Boolean::New(info.Env(), setup_status != 0);
453
+ Napi::Env env = info.Env();
454
+ Napi::HandleScope scope(env);
455
+ try {
456
+ int32_t setup_status = setup_state.load(std::memory_order_acquire);
457
+ return Napi::Boolean::New(info.Env(), setup_status != 0);
458
+ } catch (Napi::Error &e) {
459
+ e.ThrowAsJavaScriptException();
460
+ return env.Undefined();
461
+ } catch (const std::exception &e) {
462
+ Napi::Error::New(env, e.what()).ThrowAsJavaScriptException();
463
+ return env.Undefined();
464
+ }
411
465
  }
412
466
 
413
467
  void SetLogHook(const Napi::CallbackInfo &info) {
414
- NapiUtils::RequireParameterCount(info, 1);
415
468
 
416
- if (unlikely(!info[0].IsFunction())) {
417
- logger.log_error_and_throw(__func__, "Wrong argument type");
418
- }
469
+ Napi::Env env = info.Env();
470
+ Napi::HandleScope scope(env);
471
+ try {
472
+ NapiUtils::RequireParameterCount(info, 1);
419
473
 
420
- logger.set_log_hook(info[0].As<Napi::Function>());
474
+ if (unlikely(!info[0].IsFunction())) {
475
+ NapiUtils::ThrowException(info.Env(), "Expected a function");
476
+ }
477
+
478
+ logger.set_log_hook(info[0].As<Napi::Function>());
479
+ } catch (Napi::Error &e) {
480
+ e.ThrowAsJavaScriptException();
481
+ return;
482
+ } catch (const std::exception &e) {
483
+ Napi::Error::New(env, e.what()).ThrowAsJavaScriptException();
484
+ return;
485
+ }
421
486
  }
422
487
 
423
488
  void BeginSetupAsherah(const Napi::Env &env, const char *func_name,
424
489
  const Napi::CallbackInfo &info,
425
490
  Napi::String &config_string, size_t &product_id_length,
426
491
  size_t &service_name_length) {
427
- RequireAsherahNotSetup(func_name);
492
+ RequireAsherahNotSetup(env, func_name);
428
493
 
429
494
  NapiUtils::RequireParameterCount(info, 1);
430
495
 
@@ -444,22 +509,23 @@ private:
444
509
  verbose_flag = verbose;
445
510
  }
446
511
 
447
- void EndSetupAsherah(GoInt32 result, size_t product_id_length,
448
- size_t service_name_length) {
449
- CheckResult(result);
512
+ void EndSetupAsherah(const Napi::Env &env, GoInt32 result,
513
+ size_t product_id_length, size_t service_name_length) {
514
+ CheckResult(env, result);
450
515
 
451
516
  est_intermediate_key_overhead = product_id_length + service_name_length;
452
517
 
453
518
  auto old_setup_state = setup_state.exchange(1, std::memory_order_acq_rel);
454
519
  if (unlikely(old_setup_state != 0)) {
455
- logger.log_error_and_throw(__func__, "lost race to mark setup_state!");
520
+ NapiUtils::ThrowException(
521
+ env, "EndSetupAsherah: lost race to mark setup_state?!");
456
522
  }
457
523
  }
458
524
 
459
525
  void BeginEncryptToJson(const Napi::Env &env, const char *func_name,
460
526
  const Napi::CallbackInfo &info,
461
527
  Napi::String &partition_id, Napi::Value &input) {
462
- RequireAsherahSetup(func_name);
528
+ RequireAsherahSetup(env, func_name);
463
529
 
464
530
  NapiUtils::RequireParameterCount(info, 2);
465
531
 
@@ -469,22 +535,23 @@ private:
469
535
 
470
536
  void EndEncryptToJson(Napi::Env env, CobhanBufferNapi &output, GoInt32 result,
471
537
  Napi::String &output_string) {
472
- CheckResult(result);
538
+ CheckResult(env, result);
473
539
 
474
- output_string = output.ToString(env);
540
+ output_string = output.ToString();
475
541
  }
476
542
 
477
- void EndEncryptToJson(Napi::Env env, CobhanBufferNapi &output, GoInt32 result,
478
- Napi::Buffer<unsigned char> &output_buffer) {
479
- CheckResult(result);
543
+ [[maybe_unused]] void
544
+ EndEncryptToJson(Napi::Env env, CobhanBufferNapi &output, GoInt32 result,
545
+ Napi::Buffer<unsigned char> &output_buffer) {
546
+ CheckResult(env, result);
480
547
 
481
- output_buffer = output.ToBuffer(env);
548
+ output_buffer = output.ToBuffer();
482
549
  }
483
550
 
484
551
  void BeginDecryptFromJson(const Napi::Env &env, const char *func_name,
485
552
  const Napi::CallbackInfo &info,
486
553
  Napi::String &partition_id, Napi::Value &input) {
487
- RequireAsherahSetup(func_name);
554
+ RequireAsherahSetup(env, func_name);
488
555
 
489
556
  NapiUtils::RequireParameterCount(info, 2);
490
557
 
@@ -495,28 +562,29 @@ private:
495
562
  void EndDecryptFromJson(Napi::Env &env, CobhanBufferNapi &output,
496
563
  GoInt32 result,
497
564
  Napi::Buffer<unsigned char> &output_buffer) {
498
- CheckResult(result);
565
+ CheckResult(env, result);
499
566
 
500
- output_buffer = output.ToBuffer(env);
567
+ output_buffer = output.ToBuffer();
501
568
  }
502
569
 
503
570
  void EndDecryptFromJson(Napi::Env &env, CobhanBufferNapi &output,
504
571
  GoInt32 result, Napi::String &output_string) {
505
- CheckResult(result);
572
+ CheckResult(env, result);
506
573
 
507
- output_string = output.ToString(env);
574
+ output_string = output.ToString();
508
575
  }
509
576
 
510
- void BeginShutdownAsherah(const Napi::Env &, const char *func_name,
577
+ void BeginShutdownAsherah(const Napi::Env &env, const char *func_name,
511
578
  const Napi::CallbackInfo &info) {
512
- RequireAsherahSetup(func_name);
579
+ RequireAsherahSetup(env, func_name);
513
580
  NapiUtils::RequireParameterCount(info, 0);
514
581
  }
515
582
 
516
- void EndShutdownAsherah() {
583
+ void EndShutdownAsherah(Napi::Env &env) {
517
584
  auto old_setup_state = setup_state.exchange(0, std::memory_order_acq_rel);
518
585
  if (unlikely(old_setup_state == 0)) {
519
- logger.log_error_and_throw(__func__, "lost race to mark setup_state!");
586
+ NapiUtils::ThrowException(
587
+ env, "EndSetupAsherah: lost race to mark setup_state?!");
520
588
  }
521
589
  }
522
590
 
@@ -536,7 +604,8 @@ private:
536
604
  GoInt32 ExecuteTask() override { return SetupJson(config); }
537
605
 
538
606
  Napi::Value OnOKTask(Napi::Env &env) override {
539
- asherah->EndSetupAsherah(result, product_id_length, service_name_length);
607
+ asherah->EndSetupAsherah(env, result, product_id_length,
608
+ service_name_length);
540
609
  return env.Undefined();
541
610
  }
542
611
 
@@ -590,10 +659,10 @@ private:
590
659
  Napi::Value OnOKTask(Napi::Env &env) override {
591
660
  T output_result;
592
661
  asherah->EndDecryptFromJson(env, output, result, output_result);
593
- return output_result;
662
+ return output_result; // NOLINT(*-slicing)
594
663
  }
595
664
 
596
- protected:
665
+ private:
597
666
  CobhanBufferNapi partition_id;
598
667
  CobhanBufferNapi input;
599
668
  CobhanBufferNapi output;
@@ -610,7 +679,7 @@ private:
610
679
  }
611
680
 
612
681
  Napi::Value OnOKTask(Napi::Env &env) override {
613
- asherah->EndShutdownAsherah();
682
+ asherah->EndShutdownAsherah(env);
614
683
  return env.Undefined();
615
684
  }
616
685
  };
@@ -619,26 +688,31 @@ private:
619
688
 
620
689
  #pragma region Helpers
621
690
 
622
- void CheckResult(GoInt32 result) {
691
+ void CheckResult(const Napi::Env &env, GoInt32 result) {
623
692
  if (unlikely(result < 0)) {
624
- logger.log_error_and_throw(__func__, AsherahCobhanErrorToString(result));
693
+ NapiUtils::ThrowException(env, AsherahCobhanErrorToString(result));
625
694
  }
626
695
  }
627
696
 
628
- void RequireAsherahSetup(const char *func_name) {
697
+ void RequireAsherahSetup(const Napi::Env &env, const char *func_name) {
629
698
  if (unlikely(setup_state.load(std::memory_order_acquire) == 0)) {
630
- logger.log_error_and_throw(func_name, "setup() not called");
699
+ NapiUtils::ThrowException(
700
+ env,
701
+ std::string(func_name) + ": RequireAsherahSetup: setup() not called");
631
702
  }
632
703
  }
633
704
 
634
- void RequireAsherahNotSetup(const char *func_name) {
705
+ void RequireAsherahNotSetup(const Napi::Env &env, const char *func_name) {
635
706
  if (unlikely(setup_state.load(std::memory_order_acquire) != 0)) {
636
- logger.log_error_and_throw(func_name, "setup() already called");
707
+ NapiUtils::ThrowException(
708
+ env, std::string(func_name) +
709
+ ": RequireAsherahNotSetup: setup() already called");
637
710
  }
638
711
  }
639
712
 
640
- __attribute__((always_inline)) inline size_t
641
- EstimateAsherahOutputSize(size_t data_byte_len, size_t partition_byte_len) {
713
+ [[nodiscard]] __attribute__((always_inline)) inline size_t
714
+ EstimateAsherahOutputSize(size_t data_byte_len,
715
+ size_t partition_byte_len) const {
642
716
  const size_t est_encryption_overhead = 48;
643
717
  const size_t est_envelope_overhead = 185;
644
718
  const double base64_overhead = 1.34;
@@ -697,3 +771,5 @@ private:
697
771
  };
698
772
 
699
773
  NODE_API_NAMED_ADDON(asherah, Asherah)
774
+
775
+ #pragma clang diagnostic pop
package/src/asherah.d.ts CHANGED
@@ -1,3 +1,5 @@
1
+ // noinspection JSUnusedGlobalSymbols,DuplicatedCode
2
+
1
3
  /// <reference types="node" />
2
4
  export type AsherahConfig = {
3
5
  /** The name of this service (Required) */