@volter/tabnode 0.5.37 → 0.5.39

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)) {
@@ -78627,7 +78650,27 @@ function __substrateFollowAwaits(code2) {
78627
78650
  for (const [at3, text] of leading) insert(at3, text);
78628
78651
  };
78629
78652
  visit(ast, false, void 0);
78630
- return edits.length ? applyReplacements(code2, edits) : code2;
78653
+ const followed = edits.length ? applyReplacements(code2, edits) : code2;
78654
+ return __substrateTopLevelAwait(ast) ? `${followed}
78655
+ ${__substrateTopLevelAwaitMarker}` : followed;
78656
+ }
78657
+ const __substrateTopLevelAwaitMarker = "/*__substrate_top_level_await__*/";
78658
+ function __substrateTopLevelAwait(ast) {
78659
+ const isFunction = (node) => node.type === "FunctionDeclaration" || node.type === "FunctionExpression" || node.type === "ArrowFunctionExpression";
78660
+ const stack = [ast];
78661
+ while (stack.length) {
78662
+ const node = stack.pop();
78663
+ if (node.type === "AwaitExpression" || node.type === "ForOfStatement" && node.await) return true;
78664
+ for (const key of Object.keys(node)) {
78665
+ if (key === "type" || key === "start" || key === "end" || key === "loc" || key === "range") continue;
78666
+ const child = node[key];
78667
+ if (!child || typeof child !== "object") continue;
78668
+ for (const item of Array.isArray(child) ? child : [child]) {
78669
+ if (item && typeof item.type === "string" && !isFunction(item)) stack.push(item);
78670
+ }
78671
+ }
78672
+ }
78673
+ return false;
78631
78674
  }
78632
78675
  function __substrateUnfollowAwaits(source) {
78633
78676
  if (!source.includes("__substrate")) return source;
@@ -79772,10 +79815,10 @@ function createRequire(vfs2, fsShim, process2, currentDir, moduleCache, options,
79772
79815
  const wrappedCode = `(function($exports, $require, $module, $filename, $dirname, $process, $console, $importMeta, $dynamicImport, __substrateGuestGlobal, __substrateGuestConstructor) { var exports = $exports; var require = $require; var module = $module; var __filename = $filename; var __dirname = $dirname; var process = $process; var console = $console; var import_meta = $importMeta; var __dynamicImport = $dynamicImport; var document = void 0, window = void 0, location = void 0, self = void 0; var globalThis = __substrateGuestGlobal($process); var global = globalThis; var Buffer = globalThis.Buffer; var queueMicrotask = globalThis.queueMicrotask, atob = globalThis.atob, btoa = globalThis.btoa, structuredClone = globalThis.structuredClone, setTimeout = globalThis.setTimeout, clearTimeout = globalThis.clearTimeout, setInterval = globalThis.setInterval, clearInterval = globalThis.clearInterval; globalThis.process = $process; global.process = $process; with ({ __proto__: null, get navigator() { return globalThis.navigator; }, set navigator(value) { globalThis.navigator = value; }, get Navigator() { return globalThis.Navigator; }, set Navigator(value) { globalThis.Navigator = value; }, get Promise() { return globalThis.Promise; }, set Promise(value) { globalThis.Promise = value; }, get fetch() { return globalThis.fetch; }, set fetch(value) { globalThis.fetch = value; } }) { return (function() {${code2}
79773
79816
  }).call(${strictBody ? "void 0" : "$module.exports"}); }
79774
79817
  })${__substrateSourceURL(resolvedPath)}`;
79775
- let bodyKind = code2.includes(__substrateAwaitMarker) ? "generator" : "sync";
79818
+ let bodyKind = code2.includes(__substrateTopLevelAwaitMarker) ? "async" : code2.includes(__substrateAwaitMarker) ? "generator" : "sync";
79776
79819
  let fn2;
79777
79820
  try {
79778
- fn2 = __substrateSloppyEval(bodyKind === "generator" ? __substrateGeneratorBody(wrappedCode) : wrappedCode);
79821
+ fn2 = __substrateSloppyEval(bodyKind === "generator" ? __substrateGeneratorBody(wrappedCode) : bodyKind === "async" ? __substrateAsyncBody(wrappedCode) : wrappedCode);
79779
79822
  } catch (evalError) {
79780
79823
  const msg2 = evalError instanceof Error ? evalError.message : String(evalError);
79781
79824
  if (!(evalError instanceof SyntaxError)) throw evalError;
@@ -80133,10 +80176,10 @@ class Runtime {
80133
80176
  }).call(${strictBody ? "void 0" : "$module.exports"}); }
80134
80177
  })${__substrateSourceURL(filename)}`;
80135
80178
  const dynamicImport = createDynamicImport(require2, this.process, filename.startsWith("data:") ? filename : `file://${filename}`);
80136
- let bodyKind = code2.includes(__substrateAwaitMarker) ? "generator" : "sync";
80179
+ let bodyKind = code2.includes(__substrateTopLevelAwaitMarker) ? "async" : code2.includes(__substrateAwaitMarker) ? "generator" : "sync";
80137
80180
  let fn2;
80138
80181
  try {
80139
- fn2 = __substrateSloppyEval(bodyKind === "generator" ? __substrateGeneratorBody(wrappedCode) : wrappedCode);
80182
+ fn2 = __substrateSloppyEval(bodyKind === "generator" ? __substrateGeneratorBody(wrappedCode) : bodyKind === "async" ? __substrateAsyncBody(wrappedCode) : wrappedCode);
80140
80183
  } catch (syntaxError) {
80141
80184
  if (!(syntaxError instanceof SyntaxError)) throw syntaxError;
80142
80185
  bodyKind = "async";
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)) {
@@ -38285,7 +38308,27 @@ function __substrateFollowAwaits(code2) {
38285
38308
  for (const [at2, text] of leading) insert(at2, text);
38286
38309
  };
38287
38310
  visit(ast, false, void 0);
38288
- return edits.length ? applyReplacements(code2, edits) : code2;
38311
+ const followed = edits.length ? applyReplacements(code2, edits) : code2;
38312
+ return __substrateTopLevelAwait(ast) ? `${followed}
38313
+ ${__substrateTopLevelAwaitMarker}` : followed;
38314
+ }
38315
+ const __substrateTopLevelAwaitMarker = "/*__substrate_top_level_await__*/";
38316
+ function __substrateTopLevelAwait(ast) {
38317
+ const isFunction = (node) => node.type === "FunctionDeclaration" || node.type === "FunctionExpression" || node.type === "ArrowFunctionExpression";
38318
+ const stack = [ast];
38319
+ while (stack.length) {
38320
+ const node = stack.pop();
38321
+ if (node.type === "AwaitExpression" || node.type === "ForOfStatement" && node.await) return true;
38322
+ for (const key of Object.keys(node)) {
38323
+ if (key === "type" || key === "start" || key === "end" || key === "loc" || key === "range") continue;
38324
+ const child = node[key];
38325
+ if (!child || typeof child !== "object") continue;
38326
+ for (const item of Array.isArray(child) ? child : [child]) {
38327
+ if (item && typeof item.type === "string" && !isFunction(item)) stack.push(item);
38328
+ }
38329
+ }
38330
+ }
38331
+ return false;
38289
38332
  }
38290
38333
  function __substrateUnfollowAwaits(source) {
38291
38334
  if (!source.includes("__substrate")) return source;
@@ -39433,10 +39476,10 @@ function createRequire(vfs2, fsShim, process2, currentDir, moduleCache, options,
39433
39476
  const wrappedCode = `(function($exports, $require, $module, $filename, $dirname, $process, $console, $importMeta, $dynamicImport, __substrateGuestGlobal, __substrateGuestConstructor) { var exports = $exports; var require = $require; var module = $module; var __filename = $filename; var __dirname = $dirname; var process = $process; var console = $console; var import_meta = $importMeta; var __dynamicImport = $dynamicImport; var document = void 0, window = void 0, location = void 0, self = void 0; var globalThis = __substrateGuestGlobal($process); var global = globalThis; var Buffer = globalThis.Buffer; var queueMicrotask = globalThis.queueMicrotask, atob = globalThis.atob, btoa = globalThis.btoa, structuredClone = globalThis.structuredClone, setTimeout = globalThis.setTimeout, clearTimeout = globalThis.clearTimeout, setInterval = globalThis.setInterval, clearInterval = globalThis.clearInterval; globalThis.process = $process; global.process = $process; with ({ __proto__: null, get navigator() { return globalThis.navigator; }, set navigator(value) { globalThis.navigator = value; }, get Navigator() { return globalThis.Navigator; }, set Navigator(value) { globalThis.Navigator = value; }, get Promise() { return globalThis.Promise; }, set Promise(value) { globalThis.Promise = value; }, get fetch() { return globalThis.fetch; }, set fetch(value) { globalThis.fetch = value; } }) { return (function() {${code2}
39434
39477
  }).call(${strictBody ? "void 0" : "$module.exports"}); }
39435
39478
  })${__substrateSourceURL(resolvedPath)}`;
39436
- let bodyKind = code2.includes(__substrateAwaitMarker) ? "generator" : "sync";
39479
+ let bodyKind = code2.includes(__substrateTopLevelAwaitMarker) ? "async" : code2.includes(__substrateAwaitMarker) ? "generator" : "sync";
39437
39480
  let fn;
39438
39481
  try {
39439
- fn = __substrateSloppyEval(bodyKind === "generator" ? __substrateGeneratorBody(wrappedCode) : wrappedCode);
39482
+ fn = __substrateSloppyEval(bodyKind === "generator" ? __substrateGeneratorBody(wrappedCode) : bodyKind === "async" ? __substrateAsyncBody(wrappedCode) : wrappedCode);
39440
39483
  } catch (evalError) {
39441
39484
  const msg2 = evalError instanceof Error ? evalError.message : String(evalError);
39442
39485
  if (!(evalError instanceof SyntaxError)) throw evalError;
@@ -39794,10 +39837,10 @@ class Runtime {
39794
39837
  }).call(${strictBody ? "void 0" : "$module.exports"}); }
39795
39838
  })${__substrateSourceURL(filename)}`;
39796
39839
  const dynamicImport = createDynamicImport(require2, this.process, filename.startsWith("data:") ? filename : `file://${filename}`);
39797
- let bodyKind = code2.includes(__substrateAwaitMarker) ? "generator" : "sync";
39840
+ let bodyKind = code2.includes(__substrateTopLevelAwaitMarker) ? "async" : code2.includes(__substrateAwaitMarker) ? "generator" : "sync";
39798
39841
  let fn;
39799
39842
  try {
39800
- fn = __substrateSloppyEval(bodyKind === "generator" ? __substrateGeneratorBody(wrappedCode) : wrappedCode);
39843
+ fn = __substrateSloppyEval(bodyKind === "generator" ? __substrateGeneratorBody(wrappedCode) : bodyKind === "async" ? __substrateAsyncBody(wrappedCode) : wrappedCode);
39801
39844
  } catch (syntaxError) {
39802
39845
  if (!(syntaxError instanceof SyntaxError)) throw syntaxError;
39803
39846
  bodyKind = "async";
@@ -40291,7 +40334,7 @@ class WorkerRuntime {
40291
40334
  this.worker = new Worker(
40292
40335
  new URL(
40293
40336
  /* @vite-ignore */
40294
- "/assets/runtime-worker-BloPQBYA.js",
40337
+ "/assets/runtime-worker-D73ktngT.js",
40295
40338
  import.meta.url
40296
40339
  ),
40297
40340
  { 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.39",
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. */
package/src/runtime.ts CHANGED
@@ -509,7 +509,34 @@ function __substrateFollowAwaits(code: string): string {
509
509
  for (const [at, text] of leading) insert(at, text);
510
510
  };
511
511
  visit(ast, false, undefined);
512
- return edits.length ? applyReplacements(code, edits) : code;
512
+ const followed = edits.length ? applyReplacements(code, edits) : code;
513
+ return __substrateTopLevelAwait(ast) ? `${followed}\n${__substrateTopLevelAwaitMarker}` : followed;
514
+ }
515
+
516
+ /**
517
+ * Whether a module body awaits outside every function of its own. Its
518
+ * import markers make it a generator, and in a generator `await (x)` is a
519
+ * call of an identifier named `await`: chrome-devtools-mcp's bundle
520
+ * (`const HOST_RUNTIME = await (async () => ...)()`) compiled, ran, and
521
+ * threw "await is not defined". Such a body runs as an async function.
522
+ */
523
+ const __substrateTopLevelAwaitMarker = '/*__substrate_top_level_await__*/';
524
+ function __substrateTopLevelAwait(ast: any): boolean {
525
+ const isFunction = (node: any): boolean => node.type === 'FunctionDeclaration' || node.type === 'FunctionExpression' || node.type === 'ArrowFunctionExpression';
526
+ const stack: any[] = [ast];
527
+ while (stack.length) {
528
+ const node = stack.pop();
529
+ if (node.type === 'AwaitExpression' || (node.type === 'ForOfStatement' && node.await)) return true;
530
+ for (const key of Object.keys(node)) {
531
+ if (key === 'type' || key === 'start' || key === 'end' || key === 'loc' || key === 'range') continue;
532
+ const child = node[key];
533
+ if (!child || typeof child !== 'object') continue;
534
+ for (const item of Array.isArray(child) ? child : [child]) {
535
+ if (item && typeof item.type === 'string' && !isFunction(item)) stack.push(item);
536
+ }
537
+ }
538
+ }
539
+ return false;
513
540
  }
514
541
 
515
542
  /**
@@ -2100,10 +2127,10 @@ function createRequire(
2100
2127
  // still settling; a body that must wait, on a top-level `await` of its
2101
2128
  // own, runs as an async function. Either way its settling is what its
2102
2129
  // importers await.
2103
- let bodyKind: 'sync' | 'generator' | 'async' = code.includes(__substrateAwaitMarker) ? 'generator' : 'sync';
2130
+ let bodyKind: 'sync' | 'generator' | 'async' = code.includes(__substrateTopLevelAwaitMarker) ? 'async' : code.includes(__substrateAwaitMarker) ? 'generator' : 'sync';
2104
2131
  let fn;
2105
2132
  try {
2106
- fn = __substrateSloppyEval(bodyKind === 'generator' ? __substrateGeneratorBody(wrappedCode) : wrappedCode);
2133
+ fn = __substrateSloppyEval(bodyKind === 'generator' ? __substrateGeneratorBody(wrappedCode) : bodyKind === 'async' ? __substrateAsyncBody(wrappedCode) : wrappedCode);
2107
2134
  } catch (evalError) {
2108
2135
  const msg = evalError instanceof Error ? evalError.message : String(evalError);
2109
2136
  // A module with top-level await runs. Node runs a `.js` file of a package
@@ -2648,10 +2675,10 @@ export class Runtime {
2648
2675
  // a module lowered to a function body made V8 refuse vue3-ssr's server at
2649
2676
  // its first top-level `await`. A body V8 refuses for that reason runs as
2650
2677
  // an async function body instead.
2651
- let bodyKind: 'sync' | 'generator' | 'async' = code.includes(__substrateAwaitMarker) ? 'generator' : 'sync';
2678
+ let bodyKind: 'sync' | 'generator' | 'async' = code.includes(__substrateTopLevelAwaitMarker) ? 'async' : code.includes(__substrateAwaitMarker) ? 'generator' : 'sync';
2652
2679
  let fn;
2653
2680
  try {
2654
- fn = __substrateSloppyEval(bodyKind === 'generator' ? __substrateGeneratorBody(wrappedCode) : wrappedCode);
2681
+ fn = __substrateSloppyEval(bodyKind === 'generator' ? __substrateGeneratorBody(wrappedCode) : bodyKind === 'async' ? __substrateAsyncBody(wrappedCode) : wrappedCode);
2655
2682
  } catch (syntaxError) {
2656
2683
  if (!(syntaxError instanceof SyntaxError)) throw syntaxError;
2657
2684
  bodyKind = 'async';
@@ -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)) {