bare-buffer 2.7.1 → 3.0.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.c CHANGED
@@ -10,13 +10,8 @@
10
10
 
11
11
  static js_type_tag_t bare_buffer__tag = {0xfea3e944b70b0812, 0xe53bb5c343c040b6};
12
12
 
13
- static void
14
- bare_buffer_set_zero_fill_enabled_fast (js_ffi_receiver_t *receiver, uint32_t enabled) {
15
- js_set_arraybuffer_zero_fill_enabled(enabled != 0);
16
- }
17
-
18
13
  static js_value_t *
19
- bare_buffer_set_zero_fill_enabled (js_env_t *env, js_callback_info_t *info) {
14
+ bare_buffer_alloc (js_env_t *env, js_callback_info_t *info) {
20
15
  int err;
21
16
 
22
17
  size_t argc = 1;
@@ -27,18 +22,38 @@ bare_buffer_set_zero_fill_enabled (js_env_t *env, js_callback_info_t *info) {
27
22
 
28
23
  assert(argc == 1);
29
24
 
30
- uint32_t enabled;
31
- err = js_get_value_uint32(env, argv[0], &enabled);
25
+ uint32_t len;
26
+ err = js_get_value_uint32(env, argv[0], &len);
32
27
  assert(err == 0);
33
28
 
34
- js_set_arraybuffer_zero_fill_enabled(enabled != 0);
29
+ js_value_t *result;
30
+ err = js_create_arraybuffer(env, len, NULL, &result);
31
+ assert(err == 0);
35
32
 
36
- return NULL;
33
+ return result;
37
34
  }
38
35
 
39
- static uint32_t
40
- bare_buffer_byte_length_utf8_fast (js_ffi_receiver_t *receiver, js_ffi_string_t *str) {
41
- return utf8_length_from_latin1((const latin1_t *) str->data, str->len);
36
+ static js_value_t *
37
+ bare_buffer_alloc_unsafe (js_env_t *env, js_callback_info_t *info) {
38
+ int err;
39
+
40
+ size_t argc = 1;
41
+ js_value_t *argv[1];
42
+
43
+ err = js_get_callback_info(env, info, &argc, argv, NULL, NULL);
44
+ assert(err == 0);
45
+
46
+ assert(argc == 1);
47
+
48
+ uint32_t len;
49
+ err = js_get_value_uint32(env, argv[0], &len);
50
+ assert(err == 0);
51
+
52
+ js_value_t *result;
53
+ err = js_create_unsafe_arraybuffer(env, len, NULL, &result);
54
+ assert(err == 0);
55
+
56
+ return result;
42
57
  }
43
58
 
44
59
  static js_value_t *
@@ -329,11 +344,6 @@ bare_buffer__compare (void *a, size_t a_len, void *b, size_t b_len) {
329
344
  return r < 0 ? -1 : 1;
330
345
  }
331
346
 
332
- static int32_t
333
- bare_buffer_compare_fast (js_ffi_receiver_t *recv, js_ffi_typedarray_t *a, js_ffi_typedarray_t *b) {
334
- return bare_buffer__compare(a->data.u8, a->len, b->data.u8, b->len);
335
- }
336
-
337
347
  static js_value_t *
338
348
  bare_buffer_compare (js_env_t *env, js_callback_info_t *info) {
339
349
  int err;
@@ -408,105 +418,34 @@ static js_value_t *
408
418
  bare_buffer_exports (js_env_t *env, js_value_t *exports) {
409
419
  int err;
410
420
 
411
- #define V(name, fn, ffi) \
421
+ #define V(name, fn) \
412
422
  { \
413
423
  js_value_t *val; \
414
- if (ffi) { \
415
- err = js_create_function_with_ffi(env, name, -1, fn, NULL, ffi, &val); \
416
- } else { \
417
- err = js_create_function(env, name, -1, fn, NULL, &val); \
418
- } \
424
+ err = js_create_function(env, name, -1, fn, NULL, &val); \
419
425
  assert(err == 0); \
420
426
  err = js_set_named_property(env, exports, name, val); \
421
427
  assert(err == 0); \
422
428
  }
423
429
 
424
- {
425
- js_ffi_type_info_t *return_info;
426
- err = js_ffi_create_type_info(js_ffi_void, &return_info);
427
- assert(err == 0);
428
-
429
- js_ffi_type_info_t *arg_info[2];
430
-
431
- err = js_ffi_create_type_info(js_ffi_receiver, &arg_info[0]);
432
- assert(err == 0);
433
-
434
- err = js_ffi_create_type_info(js_ffi_uint32, &arg_info[1]);
435
- assert(err == 0);
436
-
437
- js_ffi_function_info_t *function_info;
438
- err = js_ffi_create_function_info(return_info, arg_info, 2, &function_info);
439
- assert(err == 0);
440
-
441
- js_ffi_function_t *ffi;
442
- err = js_ffi_create_function(bare_buffer_set_zero_fill_enabled_fast, function_info, &ffi);
443
- assert(err == 0);
444
-
445
- V("setZeroFillEnabled", bare_buffer_set_zero_fill_enabled, ffi);
446
- }
447
-
448
- {
449
- js_ffi_type_info_t *return_info;
450
- err = js_ffi_create_type_info(js_ffi_int32, &return_info);
451
- assert(err == 0);
452
-
453
- js_ffi_type_info_t *arg_info[2];
454
-
455
- err = js_ffi_create_type_info(js_ffi_receiver, &arg_info[0]);
456
- assert(err == 0);
430
+ V("alloc", bare_buffer_alloc);
431
+ V("allocUnsafe", bare_buffer_alloc_unsafe);
457
432
 
458
- err = js_ffi_create_type_info(js_ffi_string, &arg_info[1]);
459
- assert(err == 0);
433
+ V("byteLengthUTF8", bare_buffer_byte_length_utf8);
434
+ V("toStringUTF8", bare_buffer_to_string_utf8);
435
+ V("writeUTF8", bare_buffer_write_utf8);
460
436
 
461
- js_ffi_function_info_t *function_info;
462
- err = js_ffi_create_function_info(return_info, arg_info, 2, &function_info);
463
- assert(err == 0);
437
+ V("toStringUTF16LE", bare_buffer_to_string_utf16le);
438
+ V("writeUTF16LE", bare_buffer_write_utf16le);
464
439
 
465
- js_ffi_function_t *ffi;
466
- err = js_ffi_create_function(bare_buffer_byte_length_utf8_fast, function_info, &ffi);
467
- assert(err == 0);
440
+ V("toStringBase64", bare_buffer_to_string_base64);
441
+ V("writeBase64", bare_buffer_write_base64);
468
442
 
469
- V("byteLengthUTF8", bare_buffer_byte_length_utf8, ffi);
470
- }
471
-
472
- V("toStringUTF8", bare_buffer_to_string_utf8, NULL);
473
- V("writeUTF8", bare_buffer_write_utf8, NULL);
474
- V("toStringUTF16LE", bare_buffer_to_string_utf16le, NULL);
475
- V("writeUTF16LE", bare_buffer_write_utf16le, NULL);
476
- V("toStringBase64", bare_buffer_to_string_base64, NULL);
477
- V("writeBase64", bare_buffer_write_base64, NULL);
478
- V("toStringHex", bare_buffer_to_string_hex, NULL);
479
- V("writeHex", bare_buffer_write_hex, NULL);
480
-
481
- {
482
- js_ffi_type_info_t *return_info;
483
- err = js_ffi_create_type_info(js_ffi_int32, &return_info);
484
- assert(err == 0);
485
-
486
- js_ffi_type_info_t *arg_info[3];
487
-
488
- err = js_ffi_create_type_info(js_ffi_receiver, &arg_info[0]);
489
- assert(err == 0);
490
-
491
- err = js_ffi_create_type_info(js_ffi_uint8array, &arg_info[1]);
492
- assert(err == 0);
493
-
494
- err = js_ffi_create_type_info(js_ffi_uint8array, &arg_info[2]);
495
- assert(err == 0);
496
-
497
- js_ffi_function_info_t *function_info;
498
- err = js_ffi_create_function_info(return_info, arg_info, 3, &function_info);
499
- assert(err == 0);
500
-
501
- js_ffi_function_t *ffi;
502
- err = js_ffi_create_function(bare_buffer_compare_fast, function_info, &ffi);
503
- assert(err == 0);
504
-
505
- V("compare", bare_buffer_compare, ffi);
506
- }
443
+ V("toStringHex", bare_buffer_to_string_hex);
444
+ V("writeHex", bare_buffer_write_hex);
507
445
 
508
- V("tag", bare_buffer_tag, NULL)
509
- V("isTagged", bare_buffer_is_tagged, NULL)
446
+ V("compare", bare_buffer_compare);
447
+ V("tag", bare_buffer_tag);
448
+ V("isTagged", bare_buffer_is_tagged);
510
449
  #undef V
511
450
 
512
451
  return exports;
package/index.d.ts CHANGED
@@ -10,10 +10,6 @@ type BufferEncoding =
10
10
  | 'utf8'
11
11
 
12
12
  declare class Buffer extends Uint8Array {
13
- static Buffer: Buffer
14
-
15
- static poolSize: number
16
-
17
13
  compare(
18
14
  target: Buffer,
19
15
  targetStart?: number,
@@ -140,6 +136,10 @@ declare class Buffer extends Uint8Array {
140
136
  }
141
137
 
142
138
  declare namespace Buffer {
139
+ export { Buffer }
140
+
141
+ export let poolSize: number
142
+
143
143
  export function isBuffer(value: unknown): value is Buffer
144
144
 
145
145
  export function isEncoding(encoding: string): encoding is BufferEncoding
package/index.js CHANGED
@@ -21,6 +21,44 @@ module.exports = exports = class Buffer extends Uint8Array {
21
21
  poolSize = Math.max(0, value)
22
22
  }
23
23
 
24
+ constructor(arrayBuffer, offset, length, opts = {}) {
25
+ if (typeof arrayBuffer === 'number') {
26
+ opts = offset || {}
27
+
28
+ const { uninitialized = false } = opts
29
+
30
+ offset = 0
31
+ length = arrayBuffer
32
+
33
+ if (length > constants.MAX_LENGTH) {
34
+ throw new RangeError(
35
+ `Buffer length must be at most ${constants.MAX_LENGTH}`
36
+ )
37
+ }
38
+
39
+ arrayBuffer = uninitialized
40
+ ? binding.allocUnsafe(length)
41
+ : binding.alloc(length)
42
+ } else {
43
+ if (length > constants.MAX_LENGTH) {
44
+ throw new RangeError(
45
+ `Buffer length must be at most ${constants.MAX_LENGTH}`
46
+ )
47
+ }
48
+
49
+ if (typeof offset === 'object' && offset !== null) {
50
+ opts = offset
51
+ offset = 0
52
+ length = arrayBuffer.byteLength
53
+ } else if (typeof length === 'length' && length !== null) {
54
+ opts = length
55
+ length = arrayBuffer.byteLength - offset
56
+ }
57
+ }
58
+
59
+ super(arrayBuffer, offset, length)
60
+ }
61
+
24
62
  [Symbol.species]() {
25
63
  return Buffer
26
64
  }
@@ -534,12 +572,7 @@ exports.alloc = function alloc(size, fill, encoding) {
534
572
  }
535
573
 
536
574
  exports.allocUnsafe = function allocUnsafe(size) {
537
- binding.setZeroFillEnabled(0)
538
- try {
539
- return new Buffer(size)
540
- } finally {
541
- binding.setZeroFillEnabled(1)
542
- }
575
+ return new Buffer(size, { uninitialized: true })
543
576
  }
544
577
 
545
578
  exports.allocUnsafeSlow = function allocUnsafeSlow(size) {
package/lib/constants.js CHANGED
@@ -1,4 +1,4 @@
1
1
  module.exports = {
2
- MAX_LENGTH: 2 ** 32, // 4 GiB
2
+ MAX_LENGTH: 2 ** 32 - 1, // 4 GiB
3
3
  MAX_STRING_LENGTH: 2 ** 28 - 16 // ~512 MiB, lowest common limit
4
4
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "bare-buffer",
3
- "version": "2.7.1",
3
+ "version": "3.0.0-0",
4
4
  "description": "Native buffers for JavaScript",
5
5
  "exports": {
6
6
  ".": {
@@ -23,7 +23,7 @@
23
23
  ],
24
24
  "addon": true,
25
25
  "scripts": {
26
- "test": "prettier . --check && bare test/all.js"
26
+ "test": "prettier . --check && bare test.js"
27
27
  },
28
28
  "repository": {
29
29
  "type": "git",
@@ -35,6 +35,9 @@
35
35
  "url": "https://github.com/holepunchto/bare-buffer/issues"
36
36
  },
37
37
  "homepage": "https://github.com/holepunchto/bare-buffer#readme",
38
+ "engines": {
39
+ "bare": ">=1.13.0"
40
+ },
38
41
  "devDependencies": {
39
42
  "brittle": "^3.1.1",
40
43
  "cmake-bare": "^1.1.6",