@volter/tabnode 0.5.37 → 0.5.38

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.
@@ -6235,7 +6235,11 @@ function hexSlice(start, end) {
6235
6235
  }
6236
6236
  return out;
6237
6237
  }
6238
+ const nativeBase64 = typeof Uint8Array.prototype.toBase64 === "function";
6238
6239
  function base64Text(bytes2, url2) {
6240
+ if (nativeBase64) {
6241
+ return bytes2.toBase64({ alphabet: url2 ? "base64url" : "base64", omitPadding: url2 });
6242
+ }
6239
6243
  const alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
6240
6244
  let out = "";
6241
6245
  for (let index2 = 0; index2 < bytes2.length; index2 += 3) {
@@ -6259,7 +6263,20 @@ function base64urlSlice(start, end) {
6259
6263
  return base64Text(this.subarray(from, to2), true);
6260
6264
  }
6261
6265
  function byteLengthUtf8(string) {
6262
- return encoder$1.encode(string).length;
6266
+ let length = 0;
6267
+ for (let index2 = 0; index2 < string.length; index2 += 1) {
6268
+ const code2 = string.charCodeAt(index2);
6269
+ if (code2 < 128) length += 1;
6270
+ else if (code2 < 2048) length += 2;
6271
+ else if (code2 >= 55296 && code2 < 56320 && index2 + 1 < string.length) {
6272
+ const next2 = string.charCodeAt(index2 + 1);
6273
+ if (next2 >= 56320 && next2 < 57344) {
6274
+ length += 4;
6275
+ index2 += 1;
6276
+ } else length += 3;
6277
+ } else length += 3;
6278
+ }
6279
+ return length;
6263
6280
  }
6264
6281
  function compare(a, b) {
6265
6282
  return compareRanges(asBytes(a), 0, a.length, asBytes(b), 0, b.length);
@@ -23424,12 +23441,18 @@ function createCryptoModule(require2) {
23424
23441
  function digestEngine(algorithm) {
23425
23442
  return algorithm === "md5" ? md5.create() : SHA(algorithm);
23426
23443
  }
23444
+ const utf8Encoder = new TextEncoder();
23427
23445
  function cryptoBytes(value, encoding, allowArrayBuffer = false) {
23428
23446
  if (typeof value === "string") {
23429
23447
  if ((encoding == null ? void 0 : encoding.toLowerCase()) === "base64url") return buffer.Buffer.from(value, "base64");
23430
23448
  if (encoding !== void 0 && !buffer.Buffer.isEncoding(encoding)) {
23431
23449
  throw new TypeError(`Unknown encoding: ${encoding}`);
23432
23450
  }
23451
+ const lower = encoding == null ? void 0 : encoding.toLowerCase();
23452
+ if (lower === void 0 || lower === "utf8" || lower === "utf-8") {
23453
+ const bytes2 = utf8Encoder.encode(value);
23454
+ return buffer.Buffer.from(bytes2.buffer, bytes2.byteOffset, bytes2.byteLength);
23455
+ }
23433
23456
  return buffer.Buffer.from(value, encoding);
23434
23457
  }
23435
23458
  if (ArrayBuffer.isView(value)) {
package/dist/index.mjs CHANGED
@@ -5946,7 +5946,11 @@ function hexSlice(start, end) {
5946
5946
  }
5947
5947
  return out;
5948
5948
  }
5949
+ const nativeBase64 = typeof Uint8Array.prototype.toBase64 === "function";
5949
5950
  function base64Text(bytes2, url2) {
5951
+ if (nativeBase64) {
5952
+ return bytes2.toBase64({ alphabet: url2 ? "base64url" : "base64", omitPadding: url2 });
5953
+ }
5950
5954
  const alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
5951
5955
  let out = "";
5952
5956
  for (let index2 = 0; index2 < bytes2.length; index2 += 3) {
@@ -5970,7 +5974,20 @@ function base64urlSlice(start, end) {
5970
5974
  return base64Text(this.subarray(from, to), true);
5971
5975
  }
5972
5976
  function byteLengthUtf8(string) {
5973
- return encoder$1.encode(string).length;
5977
+ let length = 0;
5978
+ for (let index2 = 0; index2 < string.length; index2 += 1) {
5979
+ const code2 = string.charCodeAt(index2);
5980
+ if (code2 < 128) length += 1;
5981
+ else if (code2 < 2048) length += 2;
5982
+ else if (code2 >= 55296 && code2 < 56320 && index2 + 1 < string.length) {
5983
+ const next = string.charCodeAt(index2 + 1);
5984
+ if (next >= 56320 && next < 57344) {
5985
+ length += 4;
5986
+ index2 += 1;
5987
+ } else length += 3;
5988
+ } else length += 3;
5989
+ }
5990
+ return length;
5974
5991
  }
5975
5992
  function compare(a, b) {
5976
5993
  return compareRanges(asBytes(a), 0, a.length, asBytes(b), 0, b.length);
@@ -23572,12 +23589,18 @@ function createCryptoModule(require2) {
23572
23589
  function digestEngine(algorithm) {
23573
23590
  return algorithm === "md5" ? md5.create() : SHA(algorithm);
23574
23591
  }
23592
+ const utf8Encoder = new TextEncoder();
23575
23593
  function cryptoBytes(value, encoding, allowArrayBuffer = false) {
23576
23594
  if (typeof value === "string") {
23577
23595
  if ((encoding == null ? void 0 : encoding.toLowerCase()) === "base64url") return buffer.Buffer.from(value, "base64");
23578
23596
  if (encoding !== void 0 && !buffer.Buffer.isEncoding(encoding)) {
23579
23597
  throw new TypeError(`Unknown encoding: ${encoding}`);
23580
23598
  }
23599
+ const lower = encoding == null ? void 0 : encoding.toLowerCase();
23600
+ if (lower === void 0 || lower === "utf8" || lower === "utf-8") {
23601
+ const bytes2 = utf8Encoder.encode(value);
23602
+ return buffer.Buffer.from(bytes2.buffer, bytes2.byteOffset, bytes2.byteLength);
23603
+ }
23581
23604
  return buffer.Buffer.from(value, encoding);
23582
23605
  }
23583
23606
  if (ArrayBuffer.isView(value)) {
@@ -40291,7 +40314,7 @@ class WorkerRuntime {
40291
40314
  this.worker = new Worker(
40292
40315
  new URL(
40293
40316
  /* @vite-ignore */
40294
- "/assets/runtime-worker-BloPQBYA.js",
40317
+ "/assets/runtime-worker-C4lPa6-H.js",
40295
40318
  import.meta.url
40296
40319
  ),
40297
40320
  { type: "module" }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@volter/tabnode",
3
- "version": "0.5.37",
3
+ "version": "0.5.38",
4
4
  "description": "Node's own library, in a browser tab.",
5
5
  "type": "module",
6
6
  "license": "MIT",
@@ -205,7 +205,16 @@ export function hexSlice(this: Uint8Array, start?: number, end?: number): string
205
205
  return out;
206
206
  }
207
207
 
208
+ /** The engine's own encoder where it has one (`Uint8Array.prototype.toBase64`): one string, no garbage. */
209
+ const nativeBase64 = typeof (Uint8Array.prototype as { toBase64?: unknown }).toBase64 === 'function';
210
+
208
211
  function base64Text(bytes: Uint8Array, url: boolean): string {
212
+ // Built a character at a time, a sourcemap's base64 was ropes of garbage:
213
+ // 600 MB of a Vite session's 1.4 GB allocated in its first 20 s in a tab.
214
+ if (nativeBase64) {
215
+ return (bytes as Uint8Array & { toBase64(options: { alphabet: string; omitPadding: boolean }): string })
216
+ .toBase64({ alphabet: url ? 'base64url' : 'base64', omitPadding: url });
217
+ }
209
218
  const alphabet = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/';
210
219
  let out = '';
211
220
  for (let index = 0; index < bytes.length; index += 3) {
@@ -235,7 +244,19 @@ export function base64urlSlice(this: Uint8Array, start?: number, end?: number):
235
244
 
236
245
  /** Node's `ByteLengthUtf8`. */
237
246
  export function byteLengthUtf8(string: string): number {
238
- return encoder.encode(string).length;
247
+ // Counted, not encoded: encoding the whole string to read its length
248
+ // allocated every Buffer.from(string) twice.
249
+ let length = 0;
250
+ for (let index = 0; index < string.length; index += 1) {
251
+ const code = string.charCodeAt(index);
252
+ if (code < 0x80) length += 1;
253
+ else if (code < 0x800) length += 2;
254
+ else if (code >= 0xd800 && code < 0xdc00 && index + 1 < string.length) {
255
+ const next = string.charCodeAt(index + 1);
256
+ if (next >= 0xdc00 && next < 0xe000) { length += 4; index += 1; } else length += 3;
257
+ } else length += 3;
258
+ }
259
+ return length;
239
260
  }
240
261
 
241
262
  /** Node's `Compare`: two buffers whole, as `memcmp` orders them. */
@@ -330,6 +330,7 @@ type HostBytes = InstanceType<typeof HostBuffer>;
330
330
  * encodings the guest Buffer polyfill does not (utf16le, latin1) and views a
331
331
  * caller's ArrayBuffer at its offset instead of copying the whole store.
332
332
  */
333
+ const utf8Encoder = new TextEncoder();
333
334
  function cryptoBytes(value: unknown, encoding?: string, allowArrayBuffer = false): HostBytes {
334
335
  if (typeof value === 'string') {
335
336
  // `buffer` 6.0.3 predates base64url; the alphabet is a superset of base64's.
@@ -337,6 +338,14 @@ function cryptoBytes(value: unknown, encoding?: string, allowArrayBuffer = false
337
338
  if (encoding !== undefined && !HostBuffer.isEncoding(encoding)) {
338
339
  throw new TypeError(`Unknown encoding: ${encoding}`);
339
340
  }
341
+ // UTF-8, the default, by the engine's encoder: `buffer`'s own builds an
342
+ // array of numbers per string, and a Vite session hashing every module it
343
+ // served made hundreds of MB of that garbage in its first seconds in a tab.
344
+ const lower = encoding?.toLowerCase();
345
+ if (lower === undefined || lower === 'utf8' || lower === 'utf-8') {
346
+ const bytes = utf8Encoder.encode(value);
347
+ return HostBuffer.from(bytes.buffer as ArrayBuffer, bytes.byteOffset, bytes.byteLength);
348
+ }
340
349
  return HostBuffer.from(value, encoding);
341
350
  }
342
351
  if (ArrayBuffer.isView(value)) {