react-native-nitro-buffer 0.2.0 → 0.2.2

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.
@@ -9,21 +9,20 @@ namespace margelo::nitro::buffer {
9
9
 
10
10
  static const char base64_chars[] =
11
11
  "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
12
+ static const char base64_url_chars[] =
13
+ "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_";
12
14
 
13
15
  static inline bool is_base64(unsigned char c) {
14
- return (isalnum(c) || (c == '+') || (c == '/'));
16
+ return (isalnum(c) || (c == '+') || (c == '/') || (c == '-') || (c == '_'));
15
17
  }
16
18
 
17
19
  std::string base64_encode(const unsigned char *bytes_to_encode,
18
- unsigned int in_len) {
20
+ unsigned int in_len, bool url_safe = false) {
21
+ const char *chars = url_safe ? base64_url_chars : base64_chars;
19
22
  std::string ret;
20
- size_t output_len = 4 * ((in_len + 2) / 3);
21
- ret.resize(output_len);
23
+ ret.reserve((in_len * 4 + 2) / 3);
22
24
 
23
25
  size_t i = 0;
24
- size_t j = 0;
25
- char *out = &ret[0];
26
-
27
26
  while (i + 2 < in_len) {
28
27
  uint32_t octet_a = bytes_to_encode[i++];
29
28
  uint32_t octet_b = bytes_to_encode[i++];
@@ -31,30 +30,30 @@ std::string base64_encode(const unsigned char *bytes_to_encode,
31
30
 
32
31
  uint32_t triple = (octet_a << 16) + (octet_b << 8) + octet_c;
33
32
 
34
- out[j++] = base64_chars[(triple >> 18) & 0x3F];
35
- out[j++] = base64_chars[(triple >> 12) & 0x3F];
36
- out[j++] = base64_chars[(triple >> 6) & 0x3F];
37
- out[j++] = base64_chars[triple & 0x3F];
33
+ ret.push_back(chars[(triple >> 18) & 0x3F]);
34
+ ret.push_back(chars[(triple >> 12) & 0x3F]);
35
+ ret.push_back(chars[(triple >> 6) & 0x3F]);
36
+ ret.push_back(chars[triple & 0x3F]);
38
37
  }
39
38
 
40
39
  if (i < in_len) {
41
40
  uint32_t octet_a = bytes_to_encode[i++];
42
41
  uint32_t octet_b = (i < in_len) ? bytes_to_encode[i++] : 0;
43
- uint32_t octet_c = 0; // Always 0 for the last one if we are here
44
-
45
- uint32_t triple = (octet_a << 16) + (octet_b << 8) + octet_c;
42
+ uint32_t triple = (octet_a << 16) + (octet_b << 8);
46
43
 
47
- out[j++] = base64_chars[(triple >> 18) & 0x3F];
48
- out[j++] = base64_chars[(triple >> 12) & 0x3F];
44
+ ret.push_back(chars[(triple >> 18) & 0x3F]);
45
+ ret.push_back(chars[(triple >> 12) & 0x3F]);
49
46
 
50
47
  if (in_len % 3 == 1) {
51
- // One byte remaining, two paddings
52
- out[j++] = '=';
53
- out[j++] = '=';
48
+ if (!url_safe) {
49
+ ret.push_back('=');
50
+ ret.push_back('=');
51
+ }
54
52
  } else {
55
- // Two bytes remaining, one padding
56
- out[j++] = base64_chars[(triple >> 6) & 0x3F];
57
- out[j++] = '=';
53
+ ret.push_back(chars[(triple >> 6) & 0x3F]);
54
+ if (!url_safe) {
55
+ ret.push_back('=');
56
+ }
58
57
  }
59
58
  }
60
59
 
@@ -65,10 +64,10 @@ static const unsigned char base64_decode_table[256] = {
65
64
  255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
66
65
  255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
67
66
  255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 62, 255,
68
- 255, 255, 63, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 255, 255,
67
+ 62, 255, 63, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 255, 255,
69
68
  255, 255, 255, 255, 255, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,
70
69
  10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24,
71
- 25, 255, 255, 255, 255, 255, 255, 26, 27, 28, 29, 30, 31, 32, 33,
70
+ 25, 255, 255, 255, 255, 63, 255, 26, 27, 28, 29, 30, 31, 32, 33,
72
71
  34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48,
73
72
  49, 50, 51, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
74
73
  255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
@@ -159,16 +158,52 @@ double HybridNitroBuffer::byteLength(const std::string &string,
159
158
  const std::string &encoding) {
160
159
  if (encoding == "hex") {
161
160
  return (double)(string.length() / 2);
162
- } else if (encoding == "base64") {
161
+ } else if (encoding == "base64" || encoding == "base64url") {
163
162
  size_t len = string.length();
164
163
  if (len == 0)
165
164
  return 0;
165
+
166
+ // For base64url, we might not have padding.
167
+ // Node.js base64url byteLength: (len * 3) / 4 (floored)
168
+ if (encoding == "base64url") {
169
+ // Find actual content length (ignore trailing '=')
170
+ size_t content_len = len;
171
+ while (content_len > 0 && string[content_len - 1] == '=') content_len--;
172
+ return (double)((content_len * 3) / 4);
173
+ }
174
+
166
175
  size_t padding = 0;
167
176
  if (len > 0 && string[len - 1] == '=')
168
177
  padding++;
169
178
  if (len > 1 && string[len - 2] == '=')
170
179
  padding++;
171
180
  return (double)((len * 3) / 4 - padding);
181
+ } else if (encoding == "utf16le") {
182
+ // UTF-16 characters are 2 bytes each.
183
+ // Input string is UTF-8. We need to count Unicode code points.
184
+ size_t count = 0;
185
+ size_t i = 0;
186
+ size_t len = string.length();
187
+ const char *str = string.c_str();
188
+ while (i < len) {
189
+ unsigned char byte = static_cast<unsigned char>(str[i]);
190
+ size_t seqLen = 0;
191
+ uint32_t cp = 0;
192
+ if (byte <= 0x7F) { seqLen = 1; cp = byte; }
193
+ else if ((byte & 0xE0) == 0xC0) { seqLen = 2; }
194
+ else if ((byte & 0xF0) == 0xE0) { seqLen = 3; }
195
+ else if ((byte & 0xF8) == 0xF0) { seqLen = 4; }
196
+ else { seqLen = 1; }
197
+
198
+ i += seqLen;
199
+ // If code point > 0xFFFF, it's a surrogate pair (4 bytes in UTF-16, 2 UTF-16 units)
200
+ // Actually, string.length() in UTF-16 (JavaScript) counts surrogate pairs as 2.
201
+ // But we are receiving a UTF-8 std::string.
202
+ // A surrogate pair in UTF-8 is 4 bytes.
203
+ if (seqLen == 4) count += 2;
204
+ else count += 1;
205
+ }
206
+ return (double)(count * 2);
172
207
  } else if (encoding == "binary" || encoding == "latin1") {
173
208
  // Each character in the original string (0-255) maps to one byte.
174
209
  // The input 'string' is UTF-8 encoded. We count Unicode code points.
@@ -226,11 +261,49 @@ double HybridNitroBuffer::write(const std::shared_ptr<ArrayBuffer> &buffer,
226
261
  data[start + i] = byte;
227
262
  }
228
263
  return (double)actualWrite;
229
- } else if (encoding == "base64") {
264
+ } else if (encoding == "base64" || encoding == "base64url") {
230
265
  std::vector<unsigned char> decoded = base64_decode(string);
231
266
  size_t actualWrite = std::min(toWrite, decoded.size());
232
267
  memcpy(data + start, decoded.data(), actualWrite);
233
268
  return (double)actualWrite;
269
+ } else if (encoding == "utf16le") {
270
+ size_t written = 0;
271
+ size_t i = 0;
272
+ const char *str = string.c_str();
273
+ size_t utf8Len = string.length();
274
+ while (i < utf8Len && written + 1 < toWrite) {
275
+ uint32_t cp = 0;
276
+ size_t seqLen = 0;
277
+ unsigned char b1 = static_cast<unsigned char>(str[i]);
278
+ if (b1 <= 0x7F) { cp = b1; seqLen = 1; }
279
+ else if ((b1 & 0xE0) == 0xC0 && i + 1 < utf8Len) {
280
+ cp = ((b1 & 0x1F) << 6) | (static_cast<unsigned char>(str[i+1]) & 0x3F);
281
+ seqLen = 2;
282
+ } else if ((b1 & 0xF0) == 0xE0 && i + 2 < utf8Len) {
283
+ cp = ((b1 & 0x0F) << 12) | ((static_cast<unsigned char>(str[i+1]) & 0x3F) << 6) | (static_cast<unsigned char>(str[i+2]) & 0x3F);
284
+ seqLen = 3;
285
+ } else if ((b1 & 0xF8) == 0xF0 && i + 3 < utf8Len) {
286
+ cp = ((b1 & 0x07) << 18) | ((static_cast<unsigned char>(str[i+1]) & 0x3F) << 12) | ((static_cast<unsigned char>(str[i+2]) & 0x3F) << 6) | (static_cast<unsigned char>(str[i+3]) & 0x3F);
287
+ seqLen = 4;
288
+ } else { cp = b1; seqLen = 1; }
289
+
290
+ if (cp <= 0xFFFF) {
291
+ data[start + written++] = static_cast<uint8_t>(cp & 0xFF);
292
+ data[start + written++] = static_cast<uint8_t>((cp >> 8) & 0xFF);
293
+ } else {
294
+ if (written + 3 < toWrite) {
295
+ cp -= 0x10000;
296
+ uint16_t high = 0xD800 | ((cp >> 10) & 0x3FF);
297
+ uint16_t low = 0xDC00 | (cp & 0x3FF);
298
+ data[start + written++] = static_cast<uint8_t>(high & 0xFF);
299
+ data[start + written++] = static_cast<uint8_t>((high >> 8) & 0xFF);
300
+ data[start + written++] = static_cast<uint8_t>(low & 0xFF);
301
+ data[start + written++] = static_cast<uint8_t>((low >> 8) & 0xFF);
302
+ } else break;
303
+ }
304
+ i += seqLen;
305
+ }
306
+ return (double)written;
234
307
  } else if (encoding == "binary" || encoding == "latin1") {
235
308
  // Decode UTF-8 string back to raw bytes (0x00-0xFF)
236
309
  size_t written = 0;
@@ -514,7 +587,38 @@ HybridNitroBuffer::decode(const std::shared_ptr<ArrayBuffer> &buffer,
514
587
  }
515
588
  return hex;
516
589
  } else if (encoding == "base64") {
517
- return base64_encode(data + start, (unsigned int)actualRead);
590
+ return base64_encode(data + start, (unsigned int)actualRead, false);
591
+ } else if (encoding == "base64url") {
592
+ return base64_encode(data + start, (unsigned int)actualRead, true);
593
+ } else if (encoding == "utf16le") {
594
+ std::string result;
595
+ result.reserve(actualRead);
596
+ for (size_t i = 0; i + 1 < actualRead; i += 2) {
597
+ uint16_t unit = data[start + i] | (data[start + i + 1] << 8);
598
+ if (unit <= 0x7F) {
599
+ result.push_back(static_cast<char>(unit));
600
+ } else if (unit <= 0x7FF) {
601
+ result.push_back(static_cast<char>(0xC0 | (unit >> 6)));
602
+ result.push_back(static_cast<char>(0x80 | (unit & 0x3F)));
603
+ } else if (unit >= 0xD800 && unit <= 0xDBFF && i + 3 < actualRead) {
604
+ uint16_t next = data[start + i + 2] | (data[start + i + 3] << 8);
605
+ if (next >= 0xDC00 && next <= 0xDFFF) {
606
+ uint32_t cp = 0x10000 + (((unit & 0x3FF) << 10) | (next & 0x3FF));
607
+ result.push_back(static_cast<char>(0xF0 | (cp >> 18)));
608
+ result.push_back(static_cast<char>(0x80 | ((cp >> 12) & 0x3F)));
609
+ result.push_back(static_cast<char>(0x80 | ((cp >> 6) & 0x3F)));
610
+ result.push_back(static_cast<char>(0x80 | (cp & 0x3F)));
611
+ i += 2;
612
+ } else {
613
+ result.append(UTF8_REPLACEMENT);
614
+ }
615
+ } else {
616
+ result.push_back(static_cast<char>(0xE0 | (unit >> 12)));
617
+ result.push_back(static_cast<char>(0x80 | ((unit >> 6) & 0x3F)));
618
+ result.push_back(static_cast<char>(0x80 | (unit & 0x3F)));
619
+ }
620
+ }
621
+ return result;
518
622
  }
519
623
 
520
624
  // Default: UTF-8 with replacement
package/lib/Buffer.d.ts CHANGED
@@ -19,7 +19,7 @@ export declare class Buffer extends Uint8Array {
19
19
  toString(encoding?: string, start?: number, end?: number): string;
20
20
  indexOf(value: string | number | Uint8Array, byteOffset?: number, encoding?: string): number;
21
21
  lastIndexOf(value: string | number | Uint8Array, byteOffset?: number, encoding?: string): number;
22
- includes(value: string | number | Buffer, byteOffset?: number, encoding?: string): boolean;
22
+ includes(value: string | number | Buffer | Uint8Array, byteOffset?: number, encoding?: string): boolean;
23
23
  fill(value: string | Buffer | number | Uint8Array, offset?: any, end?: any, encoding?: string): this;
24
24
  compare(target: Uint8Array, targetStart?: number, targetEnd?: number, sourceStart?: number, sourceEnd?: number): number;
25
25
  slice(start?: number, end?: number): Buffer;
@@ -74,6 +74,7 @@ export declare class Buffer extends Uint8Array {
74
74
  };
75
75
  inspect(): string;
76
76
  static isEncoding(encoding: string): boolean;
77
+ private static _normalizeEncoding;
77
78
  swap16(): Buffer;
78
79
  swap32(): Buffer;
79
80
  swap64(): Buffer;
@@ -1 +1 @@
1
- {"version":3,"file":"Buffer.d.ts","sourceRoot":"","sources":["../src/Buffer.ts"],"names":[],"mappings":"AAaA,qBAAa,MAAO,SAAQ,UAAU;IAClC,MAAM,CAAC,QAAQ,SAAO;gBAEV,MAAM,EAAE,MAAM;gBACd,KAAK,EAAE,UAAU;gBACjB,WAAW,EAAE,WAAW,EAAE,UAAU,CAAC,EAAE,MAAM,EAAE,MAAM,CAAC,EAAE,MAAM;gBAC9D,MAAM,EAAE,MAAM,EAAE,QAAQ,CAAC,EAAE,MAAM;IAwB7C,IAAI,MAAM,IAAI,WAAW,CAExB;IAED,MAAM,CAAC,IAAI,CAAC,KAAK,EAAE,GAAG,EAAE,gBAAgB,CAAC,EAAE,GAAG,EAAE,MAAM,CAAC,EAAE,GAAG,GAAG,MAAM;IAsBrE,MAAM,CAAC,KAAK,CAAC,IAAI,EAAE,MAAM,EAAE,IAAI,CAAC,EAAE,MAAM,GAAG,MAAM,GAAG,MAAM,EAAE,QAAQ,CAAC,EAAE,MAAM,GAAG,MAAM;IAWtF,MAAM,CAAC,WAAW,CAAC,IAAI,EAAE,MAAM,GAAG,MAAM;IAWxC,MAAM,CAAC,eAAe,CAAC,IAAI,EAAE,MAAM,GAAG,MAAM;IAI5C,MAAM,CAAC,UAAU,CAAC,MAAM,EAAE,MAAM,EAAE,QAAQ,GAAE,MAAe,GAAG,MAAM;IAIpE,MAAM,CAAC,QAAQ,CAAC,GAAG,EAAE,GAAG,GAAG,GAAG,IAAI,MAAM;IAIxC,MAAM,CAAC,OAAO,CAAC,IAAI,EAAE,UAAU,EAAE,IAAI,EAAE,UAAU,GAAG,MAAM;IAI1D,MAAM,CAAC,WAAW,EAAE,UAAU,GAAG,OAAO;IAMxC,MAAM,CAAC,aAAa,CAAC,IAAI,EAAE,eAAe,EAAE,MAAM,CAAC,EAAE,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AK,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM,EAAE,UAAU,EAAE,MAAM,GAAG,MAAM;IAgBtE,WAAW,CAAC,KAAK,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM,EAAE,UAAU,EAAE,MAAM,GAAG,MAAM;IAgBtE,MAAM,IAAI;QAAE,IAAI,EAAE,QAAQ,CAAC;QAAC,IAAI,EAAE,MAAM,EAAE,CAAA;KAAE;IAO5C,OAAO,IAAI,MAAM;IAgBjB,MAAM,CAAC,UAAU,CAAC,QAAQ,EAAE,MAAM,GAAG,OAAO;IAsC5C,OAAO,CAAC,MAAM,CAAC,kBAAkB;IA2BjC,MAAM,IAAI,MAAM;IAWhB,MAAM,IAAI,MAAM;IAUhB,MAAM,IAAI,MAAM;IAYhB,IAAI,CAAC,MAAM,EAAE,UAAU,EAAE,WAAW,CAAC,EAAE,MAAM,EAAE,WAAW,CAAC,EAAE,MAAM,EAAE,SAAS,CAAC,EAAE,MAAM,GAAG,MAAM;CAqBnG"}
package/lib/Buffer.js CHANGED
@@ -14,7 +14,7 @@ export class Buffer extends Uint8Array {
14
14
  super(arg);
15
15
  }
16
16
  else if (typeof arg === 'string') {
17
- const encoding = encodingOrOffset || 'utf8';
17
+ const encoding = Buffer._normalizeEncoding(encodingOrOffset || 'utf8');
18
18
  const len = getNative().byteLength(arg, encoding);
19
19
  super(len);
20
20
  getNative().write(this.buffer, arg, 0, len, encoding);
@@ -83,7 +83,7 @@ export class Buffer extends Uint8Array {
83
83
  return Buffer.allocUnsafe(size);
84
84
  }
85
85
  static byteLength(string, encoding = 'utf8') {
86
- return getNative().byteLength(string, encoding);
86
+ return getNative().byteLength(string, Buffer._normalizeEncoding(encoding));
87
87
  }
88
88
  static isBuffer(obj) {
89
89
  return obj instanceof Buffer;
@@ -142,7 +142,7 @@ export class Buffer extends Uint8Array {
142
142
  else if (encoding === undefined) {
143
143
  encoding = 'utf8';
144
144
  }
145
- return getNative().write(this.buffer, string, this.byteOffset + offset, length, encoding);
145
+ return getNative().write(this.buffer, string, this.byteOffset + offset, length, Buffer._normalizeEncoding(encoding));
146
146
  }
147
147
  toString(encoding, start, end) {
148
148
  if (encoding === undefined)
@@ -157,7 +157,7 @@ export class Buffer extends Uint8Array {
157
157
  end = this.length;
158
158
  if (start >= end)
159
159
  return '';
160
- return getNative().decode(this.buffer, this.byteOffset + start, end - start, encoding);
160
+ return getNative().decode(this.buffer, this.byteOffset + start, end - start, Buffer._normalizeEncoding(encoding));
161
161
  }
162
162
  indexOf(value, byteOffset, encoding) {
163
163
  if (typeof value === 'string') {
@@ -568,21 +568,67 @@ export class Buffer extends Uint8Array {
568
568
  return this.inspect();
569
569
  }
570
570
  static isEncoding(encoding) {
571
- switch (encoding.toLowerCase()) {
571
+ if (typeof encoding !== 'string')
572
+ return false;
573
+ // Fast path for common lowercase encodings (avoids toLowerCase() allocation)
574
+ switch (encoding) {
572
575
  case 'utf8':
573
576
  case 'utf-8':
574
577
  case 'hex':
575
578
  case 'base64':
579
+ case 'latin1':
576
580
  case 'binary':
581
+ case 'ascii':
582
+ case 'utf16le':
583
+ case 'ucs2':
584
+ case 'base64url':
585
+ return true;
586
+ }
587
+ // Fallback for case-insensitive and rarer aliases
588
+ switch (encoding.toLowerCase()) {
589
+ case 'utf8':
590
+ case 'utf-8':
591
+ case 'hex':
592
+ case 'base64':
593
+ case 'base64url':
577
594
  case 'latin1':
595
+ case 'binary':
578
596
  case 'ascii':
579
597
  case 'utf16le':
598
+ case 'utf-16le':
580
599
  case 'ucs2':
600
+ case 'ucs-2':
581
601
  return true;
582
602
  default:
583
603
  return false;
584
604
  }
585
605
  }
606
+ static _normalizeEncoding(enc) {
607
+ const encoding = enc.toLowerCase();
608
+ switch (encoding) {
609
+ case 'utf8':
610
+ case 'utf-8':
611
+ return 'utf8';
612
+ case 'ucs2':
613
+ case 'ucs-2':
614
+ case 'utf16le':
615
+ case 'utf-16le':
616
+ return 'utf16le';
617
+ case 'latin1':
618
+ case 'binary':
619
+ return 'latin1';
620
+ case 'base64':
621
+ return 'base64';
622
+ case 'base64url':
623
+ return 'base64url';
624
+ case 'hex':
625
+ return 'hex';
626
+ case 'ascii':
627
+ return 'ascii';
628
+ default:
629
+ return encoding;
630
+ }
631
+ }
586
632
  swap16() {
587
633
  const len = this.length;
588
634
  if (len % 2 !== 0)
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "react-native-nitro-buffer",
3
- "version": "0.2.0",
3
+ "version": "0.2.2",
4
4
  "description": "The fastest, 100% Node.js-compatible Buffer implementation for React Native, powered by Nitro Modules and C++.",
5
5
  "type": "module",
6
6
  "main": "lib/index.js",
@@ -22,6 +22,14 @@
22
22
  ],
23
23
  "author": "iwater <iwater@gmail.com>",
24
24
  "license": "ISC",
25
+ "repository": {
26
+ "type": "git",
27
+ "url": "https://github.com/iwater/react-native-nitro-buffer.git"
28
+ },
29
+ "homepage": "https://github.com/iwater/react-native-nitro-buffer#readme",
30
+ "bugs": {
31
+ "url": "https://github.com/iwater/react-native-nitro-buffer/issues"
32
+ },
25
33
  "peerDependencies": {
26
34
  "react": "*",
27
35
  "react-native": "*",
@@ -6,12 +6,12 @@ Pod::Spec.new do |s|
6
6
  s.name = "react-native-nitro-buffer"
7
7
  s.version = package["version"]
8
8
  s.summary = package["description"]
9
- s.homepage = "https://github.com/iwater/rn-http-server"
9
+ s.homepage = package["homepage"]
10
10
  s.license = package["license"]
11
11
  s.authors = package["author"]
12
12
 
13
13
  s.platform = :ios, "13.0"
14
- s.source = { :git => "https://github.com/iwater/rn-http-server.git", :tag => "v#{s.version}" }
14
+ s.source = { :git => package["repository"]["url"].gsub("git+", ""), :tag => "v#{s.version}" }
15
15
 
16
16
  s.source_files = [
17
17
  "ios/**/*.{h,m,mm,swift}",
package/src/Buffer.ts CHANGED
@@ -23,7 +23,7 @@ export class Buffer extends Uint8Array {
23
23
  if (typeof arg === 'number') {
24
24
  super(arg)
25
25
  } else if (typeof arg === 'string') {
26
- const encoding = encodingOrOffset || 'utf8'
26
+ const encoding = Buffer._normalizeEncoding(encodingOrOffset || 'utf8')
27
27
  const len = getNative().byteLength(arg, encoding)
28
28
  super(len)
29
29
  getNative().write(this.buffer as ArrayBuffer, arg, 0, len, encoding)
@@ -94,7 +94,7 @@ export class Buffer extends Uint8Array {
94
94
  }
95
95
 
96
96
  static byteLength(string: string, encoding: string = 'utf8'): number {
97
- return getNative().byteLength(string, encoding)
97
+ return getNative().byteLength(string, Buffer._normalizeEncoding(encoding))
98
98
  }
99
99
 
100
100
  static isBuffer(obj: any): obj is Buffer {
@@ -153,7 +153,7 @@ export class Buffer extends Uint8Array {
153
153
  encoding = 'utf8'
154
154
  }
155
155
 
156
- return getNative().write(this.buffer as ArrayBuffer, string, this.byteOffset + (offset as number), length as number, encoding as string)
156
+ return getNative().write(this.buffer as ArrayBuffer, string, this.byteOffset + (offset as number), length as number, Buffer._normalizeEncoding(encoding as string))
157
157
  }
158
158
 
159
159
  toString(encoding?: string, start?: number, end?: number): string {
@@ -165,7 +165,7 @@ export class Buffer extends Uint8Array {
165
165
  if (end > this.length) end = this.length
166
166
  if (start >= end) return ''
167
167
 
168
- return getNative().decode(this.buffer as ArrayBuffer, this.byteOffset + start, end - start, encoding)
168
+ return getNative().decode(this.buffer as ArrayBuffer, this.byteOffset + start, end - start, Buffer._normalizeEncoding(encoding))
169
169
  }
170
170
 
171
171
  indexOf(value: string | number | Uint8Array, byteOffset?: number, encoding?: string): number {
@@ -210,7 +210,7 @@ export class Buffer extends Uint8Array {
210
210
  throw new TypeError('"value" argument must be string, number or Buffer')
211
211
  }
212
212
 
213
- includes(value: string | number | Buffer, byteOffset?: number, encoding?: string): boolean {
213
+ includes(value: string | number | Buffer | Uint8Array, byteOffset?: number, encoding?: string): boolean {
214
214
  return this.indexOf(value, byteOffset, encoding) !== -1
215
215
  }
216
216
 
@@ -626,22 +626,70 @@ export class Buffer extends Uint8Array {
626
626
  }
627
627
 
628
628
  static isEncoding(encoding: string): boolean {
629
- switch (encoding.toLowerCase()) {
629
+ if (typeof encoding !== 'string') return false
630
+
631
+ // Fast path for common lowercase encodings (avoids toLowerCase() allocation)
632
+ switch (encoding) {
630
633
  case 'utf8':
631
634
  case 'utf-8':
632
635
  case 'hex':
633
636
  case 'base64':
637
+ case 'latin1':
634
638
  case 'binary':
639
+ case 'ascii':
640
+ case 'utf16le':
641
+ case 'ucs2':
642
+ case 'base64url':
643
+ return true
644
+ }
645
+
646
+ // Fallback for case-insensitive and rarer aliases
647
+ switch (encoding.toLowerCase()) {
648
+ case 'utf8':
649
+ case 'utf-8':
650
+ case 'hex':
651
+ case 'base64':
652
+ case 'base64url':
635
653
  case 'latin1':
654
+ case 'binary':
636
655
  case 'ascii':
637
656
  case 'utf16le':
657
+ case 'utf-16le':
638
658
  case 'ucs2':
659
+ case 'ucs-2':
639
660
  return true
640
661
  default:
641
662
  return false
642
663
  }
643
664
  }
644
665
 
666
+ private static _normalizeEncoding(enc: string): string {
667
+ const encoding = enc.toLowerCase()
668
+ switch (encoding) {
669
+ case 'utf8':
670
+ case 'utf-8':
671
+ return 'utf8'
672
+ case 'ucs2':
673
+ case 'ucs-2':
674
+ case 'utf16le':
675
+ case 'utf-16le':
676
+ return 'utf16le'
677
+ case 'latin1':
678
+ case 'binary':
679
+ return 'latin1'
680
+ case 'base64':
681
+ return 'base64'
682
+ case 'base64url':
683
+ return 'base64url'
684
+ case 'hex':
685
+ return 'hex'
686
+ case 'ascii':
687
+ return 'ascii'
688
+ default:
689
+ return encoding
690
+ }
691
+ }
692
+
645
693
  swap16(): Buffer {
646
694
  const len = this.length
647
695
  if (len % 2 !== 0) throw new RangeError('Buffer size must be a multiple of 16-bits')