bare-buffer 3.4.4 → 3.5.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
@@ -144,6 +144,33 @@ bare_buffer_to_string_utf8(js_env_t *env, js_callback_info_t *info) {
144
144
  return result;
145
145
  }
146
146
 
147
+ static js_value_t *
148
+ bare_buffer_validate_utf8(js_env_t *env, js_callback_info_t *info) {
149
+ int err;
150
+
151
+ size_t argc = 2;
152
+ js_value_t *argv[2];
153
+
154
+ err = js_get_callback_info(env, info, &argc, argv, NULL, NULL);
155
+ assert(err == 0);
156
+
157
+ assert(argc == 2);
158
+
159
+ utf8_t *buf;
160
+ err = bare_buffer__get_info(env, argv[0], (void **) &buf, NULL);
161
+ assert(err == 0);
162
+
163
+ int64_t len;
164
+ err = js_get_value_int64(env, argv[1], &len);
165
+ assert(err == 0);
166
+
167
+ js_value_t *result;
168
+ err = js_get_boolean(env, utf8_validate(buf, len), &result);
169
+ assert(err == 0);
170
+
171
+ return result;
172
+ }
173
+
147
174
  static int64_t
148
175
  bare_buffer_typed_write_utf8(
149
176
  js_value_t *receiver,
@@ -741,6 +768,33 @@ bare_buffer_write_hex(js_env_t *env, js_callback_info_t *info) {
741
768
  return result;
742
769
  }
743
770
 
771
+ static js_value_t *
772
+ bare_buffer_validate_ascii(js_env_t *env, js_callback_info_t *info) {
773
+ int err;
774
+
775
+ size_t argc = 2;
776
+ js_value_t *argv[2];
777
+
778
+ err = js_get_callback_info(env, info, &argc, argv, NULL, NULL);
779
+ assert(err == 0);
780
+
781
+ assert(argc == 2);
782
+
783
+ ascii_t *buf;
784
+ err = bare_buffer__get_info(env, argv[0], (void **) &buf, NULL);
785
+ assert(err == 0);
786
+
787
+ int64_t len;
788
+ err = js_get_value_int64(env, argv[1], &len);
789
+ assert(err == 0);
790
+
791
+ js_value_t *result;
792
+ err = js_get_boolean(env, ascii_validate(buf, len), &result);
793
+ assert(err == 0);
794
+
795
+ return result;
796
+ }
797
+
744
798
  static inline int
745
799
  bare_buffer__memcmp(void *a, size_t a_len, void *b, size_t b_len) {
746
800
  int r = memcmp(a, b, a_len < b_len ? a_len : b_len);
@@ -895,6 +949,8 @@ bare_buffer_exports(js_env_t *env, js_value_t *exports) {
895
949
 
896
950
  V("toStringUTF8", bare_buffer_to_string_utf8, NULL, NULL);
897
951
 
952
+ V("validateUTF8", bare_buffer_validate_utf8, NULL, NULL);
953
+
898
954
  V(
899
955
  "writeUTF8",
900
956
  bare_buffer_write_utf8,
@@ -992,6 +1048,8 @@ bare_buffer_exports(js_env_t *env, js_value_t *exports) {
992
1048
  bare_buffer_typed_write_hex
993
1049
  );
994
1050
 
1051
+ V("validateAscii", bare_buffer_validate_ascii, NULL, NULL);
1052
+
995
1053
  V(
996
1054
  "compare",
997
1055
  bare_buffer_compare,
package/index.d.ts CHANGED
@@ -49,6 +49,8 @@ interface Buffer extends Uint8Array<ArrayBuffer> {
49
49
 
50
50
  toString(encoding?: BufferEncoding, start?: number, end?: number): string
51
51
 
52
+ toJSON(): number[]
53
+
52
54
  readDoubleBE(offset?: number): number
53
55
  readDoubleLE(offset?: number): number
54
56
 
@@ -151,6 +153,12 @@ declare namespace Buffer {
151
153
 
152
154
  export function isEncoding(encoding: string): encoding is BufferEncoding
153
155
 
156
+ export function isASCII(buffer: Buffer): boolean
157
+ export function isAscii(buffer: Buffer): boolean
158
+
159
+ export function isUTF8(buffer: Buffer): boolean
160
+ export function isUtf8(buffer: Buffer): boolean
161
+
154
162
  export function alloc(size: number, fill: string, encoding?: BufferEncoding): Buffer
155
163
  export function alloc(size: number, fill?: Buffer | number | boolean): Buffer
156
164
 
@@ -177,6 +185,8 @@ declare namespace Buffer {
177
185
  export function atob(data: unknown): string
178
186
  export function btoa(data: unknown): string
179
187
 
188
+ export function transcode(buffer: Buffer, from: BufferEncoding, to: BufferEncoding): Buffer
189
+
180
190
  export { Buffer, type BufferEncoding, constants }
181
191
  }
182
192
 
package/index.js CHANGED
@@ -282,6 +282,10 @@ module.exports = exports = class Buffer extends Uint8Array {
282
282
  return codecFor(encoding).toString(buffer)
283
283
  }
284
284
 
285
+ toJSON() {
286
+ return Array.from(this)
287
+ }
288
+
285
289
  write(string, offset = 0, length = this.byteLength - offset, encoding = 'utf8') {
286
290
  // write(string)
287
291
  if (arguments.length === 1) return utf8.write(this, string)
@@ -659,6 +663,20 @@ exports.isEncoding = function isEncoding(encoding) {
659
663
  }
660
664
  }
661
665
 
666
+ exports.isASCII = function isASCII(buffer) {
667
+ return ascii.validate(buffer)
668
+ }
669
+
670
+ // for Node.js compatibily
671
+ exports.isAscii = exports.isASCII
672
+
673
+ exports.isUTF8 = function isUTF8(buffer) {
674
+ return utf8.validate(buffer)
675
+ }
676
+
677
+ // for Node.js compatibility
678
+ exports.isUtf8 = exports.isUTF8
679
+
662
680
  exports.alloc = function alloc(size, fill, encoding) {
663
681
  const buffer = new Buffer(size)
664
682
  if (fill !== undefined) buffer.fill(fill, 0, buffer.byteLength, encoding)
@@ -823,6 +841,10 @@ exports.btoa = function btoa(data) {
823
841
  return Buffer.from(data, 'latin1').toString('base64')
824
842
  }
825
843
 
844
+ exports.transcode = function transcode(buffer, from, to) {
845
+ return Buffer.from(buffer.toString(from), to)
846
+ }
847
+
826
848
  function readInt48BE(view, offset) {
827
849
  const hi = view.getUint16(offset, false)
828
850
  const lo = view.getUint32(offset + 2, false)
package/lib/ascii.js CHANGED
@@ -1,3 +1,5 @@
1
+ const binding = require('../binding')
2
+
1
3
  exports.byteLength = function byteLength(string) {
2
4
  return string.length
3
5
  }
@@ -23,3 +25,7 @@ exports.write = function write(buffer, string) {
23
25
 
24
26
  return len
25
27
  }
28
+
29
+ exports.validate = function validate(buffer) {
30
+ return binding.validateAscii(buffer.buffer, buffer.byteLength)
31
+ }
package/lib/utf8.js CHANGED
@@ -11,3 +11,7 @@ exports.toString = function toString(buffer) {
11
11
  exports.write = function write(buffer, string) {
12
12
  return binding.writeUTF8(buffer.buffer, buffer.byteOffset, buffer.byteLength, string)
13
13
  }
14
+
15
+ exports.validate = function validate(buffer) {
16
+ return binding.validateUTF8(buffer.buffer, buffer.byteLength)
17
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "bare-buffer",
3
- "version": "3.4.4",
3
+ "version": "3.5.0",
4
4
  "description": "Native buffers for JavaScript",
5
5
  "exports": {
6
6
  "./package": "./package.json",