@ricsam/isolate-fetch 0.1.19 → 0.1.21

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.
@@ -72,6 +72,7 @@ var import_isolate_core = require("@ricsam/isolate-core");
72
72
  var import_stream_state = require("./stream-state.cjs");
73
73
  var instanceStateMap = new WeakMap;
74
74
  var passthruBodies = new WeakMap;
75
+ var trackedGlobalHandles = new WeakMap;
75
76
  var nextInstanceId = 1;
76
77
  function getInstanceStateMapForContext(context) {
77
78
  let map = instanceStateMap.get(context);
@@ -89,6 +90,37 @@ function getPassthruBodiesForContext(context) {
89
90
  }
90
91
  return map;
91
92
  }
93
+ function getTrackedGlobalHandlesForContext(context) {
94
+ let handles = trackedGlobalHandles.get(context);
95
+ if (!handles) {
96
+ handles = new Set;
97
+ trackedGlobalHandles.set(context, handles);
98
+ }
99
+ return handles;
100
+ }
101
+ function setTrackedGlobal(context, name, value) {
102
+ if (value instanceof import_isolated_vm.default.Callback || value instanceof import_isolated_vm.default.Reference) {
103
+ getTrackedGlobalHandlesForContext(context).add(value);
104
+ }
105
+ context.global.setSync(name, value);
106
+ }
107
+ function releaseIfSupported(handle) {
108
+ const maybeHandle = handle;
109
+ if (typeof maybeHandle.release === "function") {
110
+ maybeHandle.release();
111
+ }
112
+ }
113
+ function releaseTrackedGlobalHandles(context) {
114
+ const handles = trackedGlobalHandles.get(context);
115
+ if (!handles)
116
+ return;
117
+ for (const handle of handles) {
118
+ try {
119
+ releaseIfSupported(handle);
120
+ } catch {}
121
+ }
122
+ handles.clear();
123
+ }
92
124
  var headersCode = `
93
125
  (function() {
94
126
  class Headers {
@@ -421,24 +453,23 @@ var multipartCode = `
421
453
  })();
422
454
  `;
423
455
  function setupStreamCallbacks(context, streamRegistry) {
424
- const global = context.global;
425
- global.setSync("__Stream_create", new import_isolated_vm.default.Callback(() => {
456
+ setTrackedGlobal(context, "__Stream_create", new import_isolated_vm.default.Callback(() => {
426
457
  return streamRegistry.create();
427
458
  }));
428
- global.setSync("__Stream_push", new import_isolated_vm.default.Callback((streamId, chunkArray) => {
459
+ setTrackedGlobal(context, "__Stream_push", new import_isolated_vm.default.Callback((streamId, chunkArray) => {
429
460
  const chunk = new Uint8Array(chunkArray);
430
461
  return streamRegistry.push(streamId, chunk);
431
462
  }));
432
- global.setSync("__Stream_close", new import_isolated_vm.default.Callback((streamId) => {
463
+ setTrackedGlobal(context, "__Stream_close", new import_isolated_vm.default.Callback((streamId) => {
433
464
  streamRegistry.close(streamId);
434
465
  }));
435
- global.setSync("__Stream_error", new import_isolated_vm.default.Callback((streamId, message) => {
466
+ setTrackedGlobal(context, "__Stream_error", new import_isolated_vm.default.Callback((streamId, message) => {
436
467
  streamRegistry.error(streamId, new Error(message));
437
468
  }));
438
- global.setSync("__Stream_isQueueFull", new import_isolated_vm.default.Callback((streamId) => {
469
+ setTrackedGlobal(context, "__Stream_isQueueFull", new import_isolated_vm.default.Callback((streamId) => {
439
470
  return streamRegistry.isQueueFull(streamId);
440
471
  }));
441
- global.setSync("__Stream_cancel", new import_isolated_vm.default.Callback((streamId) => {
472
+ setTrackedGlobal(context, "__Stream_cancel", new import_isolated_vm.default.Callback((streamId) => {
442
473
  streamRegistry.cancel(streamId);
443
474
  }));
444
475
  const pullRef = new import_isolated_vm.default.Reference(async (streamId) => {
@@ -448,7 +479,7 @@ function setupStreamCallbacks(context, streamRegistry) {
448
479
  }
449
480
  return JSON.stringify({ done: false, value: Array.from(result.value) });
450
481
  });
451
- global.setSync("__Stream_pull_ref", pullRef);
482
+ setTrackedGlobal(context, "__Stream_pull_ref", pullRef);
452
483
  }
453
484
  var hostBackedStreamCode = `
454
485
  (function() {
@@ -531,8 +562,7 @@ var hostBackedStreamCode = `
531
562
  })();
532
563
  `;
533
564
  function setupResponse(context, stateMap, streamRegistry) {
534
- const global = context.global;
535
- global.setSync("__Response_construct", new import_isolated_vm.default.Callback((bodyBytes, status, statusText, headers) => {
565
+ setTrackedGlobal(context, "__Response_construct", new import_isolated_vm.default.Callback((bodyBytes, status, statusText, headers) => {
536
566
  const instanceId = nextInstanceId++;
537
567
  const body = bodyBytes ? new Uint8Array(bodyBytes) : null;
538
568
  const state = {
@@ -549,7 +579,7 @@ function setupResponse(context, stateMap, streamRegistry) {
549
579
  stateMap.set(instanceId, state);
550
580
  return instanceId;
551
581
  }));
552
- global.setSync("__Response_constructStreaming", new import_isolated_vm.default.Callback((streamId, status, statusText, headers) => {
582
+ setTrackedGlobal(context, "__Response_constructStreaming", new import_isolated_vm.default.Callback((streamId, status, statusText, headers) => {
553
583
  const instanceId = nextInstanceId++;
554
584
  const state = {
555
585
  status,
@@ -565,7 +595,7 @@ function setupResponse(context, stateMap, streamRegistry) {
565
595
  stateMap.set(instanceId, state);
566
596
  return instanceId;
567
597
  }));
568
- global.setSync("__Response_constructFromFetch", new import_isolated_vm.default.Callback((bodyBytes, status, statusText, headers, url, redirected) => {
598
+ setTrackedGlobal(context, "__Response_constructFromFetch", new import_isolated_vm.default.Callback((bodyBytes, status, statusText, headers, url, redirected) => {
569
599
  const instanceId = nextInstanceId++;
570
600
  const body = bodyBytes ? new Uint8Array(bodyBytes) : null;
571
601
  const state = {
@@ -582,45 +612,45 @@ function setupResponse(context, stateMap, streamRegistry) {
582
612
  stateMap.set(instanceId, state);
583
613
  return instanceId;
584
614
  }));
585
- global.setSync("__Response_get_status", new import_isolated_vm.default.Callback((instanceId) => {
615
+ setTrackedGlobal(context, "__Response_get_status", new import_isolated_vm.default.Callback((instanceId) => {
586
616
  const state = stateMap.get(instanceId);
587
617
  return state?.status ?? 200;
588
618
  }));
589
- global.setSync("__Response_get_statusText", new import_isolated_vm.default.Callback((instanceId) => {
619
+ setTrackedGlobal(context, "__Response_get_statusText", new import_isolated_vm.default.Callback((instanceId) => {
590
620
  const state = stateMap.get(instanceId);
591
621
  return state?.statusText ?? "";
592
622
  }));
593
- global.setSync("__Response_get_headers", new import_isolated_vm.default.Callback((instanceId) => {
623
+ setTrackedGlobal(context, "__Response_get_headers", new import_isolated_vm.default.Callback((instanceId) => {
594
624
  const state = stateMap.get(instanceId);
595
625
  return state?.headers ?? [];
596
626
  }));
597
- global.setSync("__Response_get_bodyUsed", new import_isolated_vm.default.Callback((instanceId) => {
627
+ setTrackedGlobal(context, "__Response_get_bodyUsed", new import_isolated_vm.default.Callback((instanceId) => {
598
628
  const state = stateMap.get(instanceId);
599
629
  return state?.bodyUsed ?? false;
600
630
  }));
601
- global.setSync("__Response_get_url", new import_isolated_vm.default.Callback((instanceId) => {
631
+ setTrackedGlobal(context, "__Response_get_url", new import_isolated_vm.default.Callback((instanceId) => {
602
632
  const state = stateMap.get(instanceId);
603
633
  return state?.url ?? "";
604
634
  }));
605
- global.setSync("__Response_get_redirected", new import_isolated_vm.default.Callback((instanceId) => {
635
+ setTrackedGlobal(context, "__Response_get_redirected", new import_isolated_vm.default.Callback((instanceId) => {
606
636
  const state = stateMap.get(instanceId);
607
637
  return state?.redirected ?? false;
608
638
  }));
609
- global.setSync("__Response_get_type", new import_isolated_vm.default.Callback((instanceId) => {
639
+ setTrackedGlobal(context, "__Response_get_type", new import_isolated_vm.default.Callback((instanceId) => {
610
640
  const state = stateMap.get(instanceId);
611
641
  return state?.type ?? "default";
612
642
  }));
613
- global.setSync("__Response_get_nullBody", new import_isolated_vm.default.Callback((instanceId) => {
643
+ setTrackedGlobal(context, "__Response_get_nullBody", new import_isolated_vm.default.Callback((instanceId) => {
614
644
  const state = stateMap.get(instanceId);
615
645
  return state?.nullBody ?? false;
616
646
  }));
617
- global.setSync("__Response_setType", new import_isolated_vm.default.Callback((instanceId, type) => {
647
+ setTrackedGlobal(context, "__Response_setType", new import_isolated_vm.default.Callback((instanceId, type) => {
618
648
  const state = stateMap.get(instanceId);
619
649
  if (state) {
620
650
  state.type = type;
621
651
  }
622
652
  }));
623
- global.setSync("__Response_markBodyUsed", new import_isolated_vm.default.Callback((instanceId) => {
653
+ setTrackedGlobal(context, "__Response_markBodyUsed", new import_isolated_vm.default.Callback((instanceId) => {
624
654
  const state = stateMap.get(instanceId);
625
655
  if (state) {
626
656
  if (state.bodyUsed) {
@@ -629,20 +659,20 @@ function setupResponse(context, stateMap, streamRegistry) {
629
659
  state.bodyUsed = true;
630
660
  }
631
661
  }));
632
- global.setSync("__Response_text", new import_isolated_vm.default.Callback((instanceId) => {
662
+ setTrackedGlobal(context, "__Response_text", new import_isolated_vm.default.Callback((instanceId) => {
633
663
  const state = stateMap.get(instanceId);
634
664
  if (!state || !state.body)
635
665
  return "";
636
666
  return new TextDecoder().decode(state.body);
637
667
  }));
638
- global.setSync("__Response_arrayBuffer", new import_isolated_vm.default.Callback((instanceId) => {
668
+ setTrackedGlobal(context, "__Response_arrayBuffer", new import_isolated_vm.default.Callback((instanceId) => {
639
669
  const state = stateMap.get(instanceId);
640
670
  if (!state || !state.body) {
641
671
  return new import_isolated_vm.default.ExternalCopy(new ArrayBuffer(0)).copyInto();
642
672
  }
643
673
  return new import_isolated_vm.default.ExternalCopy(state.body.buffer.slice(state.body.byteOffset, state.body.byteOffset + state.body.byteLength)).copyInto();
644
674
  }));
645
- global.setSync("__Response_clone", new import_isolated_vm.default.Callback((instanceId) => {
675
+ setTrackedGlobal(context, "__Response_clone", new import_isolated_vm.default.Callback((instanceId) => {
646
676
  const state = stateMap.get(instanceId);
647
677
  if (!state) {
648
678
  throw new Error("[TypeError]Cannot clone invalid Response");
@@ -688,7 +718,7 @@ function setupResponse(context, stateMap, streamRegistry) {
688
718
  stateMap.set(newId, newState);
689
719
  return newId;
690
720
  }));
691
- global.setSync("__Response_getStreamId", new import_isolated_vm.default.Callback((instanceId) => {
721
+ setTrackedGlobal(context, "__Response_getStreamId", new import_isolated_vm.default.Callback((instanceId) => {
692
722
  const state = stateMap.get(instanceId);
693
723
  return state?.streamId ?? null;
694
724
  }));
@@ -1073,8 +1103,7 @@ function setupResponse(context, stateMap, streamRegistry) {
1073
1103
  context.evalSync(responseCode);
1074
1104
  }
1075
1105
  function setupRequest(context, stateMap) {
1076
- const global = context.global;
1077
- global.setSync("__Request_construct", new import_isolated_vm.default.Callback((url, method, headers, bodyBytes, mode, credentials, cache, redirect, referrer, integrity) => {
1106
+ setTrackedGlobal(context, "__Request_construct", new import_isolated_vm.default.Callback((url, method, headers, bodyBytes, mode, credentials, cache, redirect, referrer, integrity) => {
1078
1107
  const instanceId = nextInstanceId++;
1079
1108
  const body = bodyBytes ? new Uint8Array(bodyBytes) : null;
1080
1109
  const state = {
@@ -1095,47 +1124,47 @@ function setupRequest(context, stateMap) {
1095
1124
  stateMap.set(instanceId, state);
1096
1125
  return instanceId;
1097
1126
  }));
1098
- global.setSync("__Request_get_method", new import_isolated_vm.default.Callback((instanceId) => {
1127
+ setTrackedGlobal(context, "__Request_get_method", new import_isolated_vm.default.Callback((instanceId) => {
1099
1128
  const state = stateMap.get(instanceId);
1100
1129
  return state?.method ?? "GET";
1101
1130
  }));
1102
- global.setSync("__Request_get_url", new import_isolated_vm.default.Callback((instanceId) => {
1131
+ setTrackedGlobal(context, "__Request_get_url", new import_isolated_vm.default.Callback((instanceId) => {
1103
1132
  const state = stateMap.get(instanceId);
1104
1133
  return state?.url ?? "";
1105
1134
  }));
1106
- global.setSync("__Request_get_headers", new import_isolated_vm.default.Callback((instanceId) => {
1135
+ setTrackedGlobal(context, "__Request_get_headers", new import_isolated_vm.default.Callback((instanceId) => {
1107
1136
  const state = stateMap.get(instanceId);
1108
1137
  return state?.headers ?? [];
1109
1138
  }));
1110
- global.setSync("__Request_get_bodyUsed", new import_isolated_vm.default.Callback((instanceId) => {
1139
+ setTrackedGlobal(context, "__Request_get_bodyUsed", new import_isolated_vm.default.Callback((instanceId) => {
1111
1140
  const state = stateMap.get(instanceId);
1112
1141
  return state?.bodyUsed ?? false;
1113
1142
  }));
1114
- global.setSync("__Request_get_mode", new import_isolated_vm.default.Callback((instanceId) => {
1143
+ setTrackedGlobal(context, "__Request_get_mode", new import_isolated_vm.default.Callback((instanceId) => {
1115
1144
  const state = stateMap.get(instanceId);
1116
1145
  return state?.mode ?? "cors";
1117
1146
  }));
1118
- global.setSync("__Request_get_credentials", new import_isolated_vm.default.Callback((instanceId) => {
1147
+ setTrackedGlobal(context, "__Request_get_credentials", new import_isolated_vm.default.Callback((instanceId) => {
1119
1148
  const state = stateMap.get(instanceId);
1120
1149
  return state?.credentials ?? "same-origin";
1121
1150
  }));
1122
- global.setSync("__Request_get_cache", new import_isolated_vm.default.Callback((instanceId) => {
1151
+ setTrackedGlobal(context, "__Request_get_cache", new import_isolated_vm.default.Callback((instanceId) => {
1123
1152
  const state = stateMap.get(instanceId);
1124
1153
  return state?.cache ?? "default";
1125
1154
  }));
1126
- global.setSync("__Request_get_redirect", new import_isolated_vm.default.Callback((instanceId) => {
1155
+ setTrackedGlobal(context, "__Request_get_redirect", new import_isolated_vm.default.Callback((instanceId) => {
1127
1156
  const state = stateMap.get(instanceId);
1128
1157
  return state?.redirect ?? "follow";
1129
1158
  }));
1130
- global.setSync("__Request_get_referrer", new import_isolated_vm.default.Callback((instanceId) => {
1159
+ setTrackedGlobal(context, "__Request_get_referrer", new import_isolated_vm.default.Callback((instanceId) => {
1131
1160
  const state = stateMap.get(instanceId);
1132
1161
  return state?.referrer ?? "about:client";
1133
1162
  }));
1134
- global.setSync("__Request_get_integrity", new import_isolated_vm.default.Callback((instanceId) => {
1163
+ setTrackedGlobal(context, "__Request_get_integrity", new import_isolated_vm.default.Callback((instanceId) => {
1135
1164
  const state = stateMap.get(instanceId);
1136
1165
  return state?.integrity ?? "";
1137
1166
  }));
1138
- global.setSync("__Request_markBodyUsed", new import_isolated_vm.default.Callback((instanceId) => {
1167
+ setTrackedGlobal(context, "__Request_markBodyUsed", new import_isolated_vm.default.Callback((instanceId) => {
1139
1168
  const state = stateMap.get(instanceId);
1140
1169
  if (state) {
1141
1170
  if (state.bodyUsed) {
@@ -1144,26 +1173,26 @@ function setupRequest(context, stateMap) {
1144
1173
  state.bodyUsed = true;
1145
1174
  }
1146
1175
  }));
1147
- global.setSync("__Request_text", new import_isolated_vm.default.Callback((instanceId) => {
1176
+ setTrackedGlobal(context, "__Request_text", new import_isolated_vm.default.Callback((instanceId) => {
1148
1177
  const state = stateMap.get(instanceId);
1149
1178
  if (!state || !state.body)
1150
1179
  return "";
1151
1180
  return new TextDecoder().decode(state.body);
1152
1181
  }));
1153
- global.setSync("__Request_arrayBuffer", new import_isolated_vm.default.Callback((instanceId) => {
1182
+ setTrackedGlobal(context, "__Request_arrayBuffer", new import_isolated_vm.default.Callback((instanceId) => {
1154
1183
  const state = stateMap.get(instanceId);
1155
1184
  if (!state || !state.body) {
1156
1185
  return new import_isolated_vm.default.ExternalCopy(new ArrayBuffer(0)).copyInto();
1157
1186
  }
1158
1187
  return new import_isolated_vm.default.ExternalCopy(state.body.buffer.slice(state.body.byteOffset, state.body.byteOffset + state.body.byteLength)).copyInto();
1159
1188
  }));
1160
- global.setSync("__Request_getBodyBytes", new import_isolated_vm.default.Callback((instanceId) => {
1189
+ setTrackedGlobal(context, "__Request_getBodyBytes", new import_isolated_vm.default.Callback((instanceId) => {
1161
1190
  const state = stateMap.get(instanceId);
1162
1191
  if (!state || !state.body)
1163
1192
  return null;
1164
1193
  return Array.from(state.body);
1165
1194
  }));
1166
- global.setSync("__Request_clone", new import_isolated_vm.default.Callback((instanceId) => {
1195
+ setTrackedGlobal(context, "__Request_clone", new import_isolated_vm.default.Callback((instanceId) => {
1167
1196
  const state = stateMap.get(instanceId);
1168
1197
  if (!state) {
1169
1198
  throw new Error("[TypeError]Cannot clone invalid Request");
@@ -1177,11 +1206,11 @@ function setupRequest(context, stateMap) {
1177
1206
  stateMap.set(newId, newState);
1178
1207
  return newId;
1179
1208
  }));
1180
- global.setSync("__Request_getStreamId", new import_isolated_vm.default.Callback((instanceId) => {
1209
+ setTrackedGlobal(context, "__Request_getStreamId", new import_isolated_vm.default.Callback((instanceId) => {
1181
1210
  const state = stateMap.get(instanceId);
1182
1211
  return state?.streamId ?? null;
1183
1212
  }));
1184
- global.setSync("__Request_get_signalAborted", new import_isolated_vm.default.Callback((instanceId) => {
1213
+ setTrackedGlobal(context, "__Request_get_signalAborted", new import_isolated_vm.default.Callback((instanceId) => {
1185
1214
  const state = stateMap.get(instanceId);
1186
1215
  return state?.signalAborted ?? false;
1187
1216
  }));
@@ -1527,9 +1556,8 @@ function setupRequest(context, stateMap) {
1527
1556
  }
1528
1557
  var FETCH_STREAM_THRESHOLD = 64 * 1024;
1529
1558
  function setupFetchFunction(context, stateMap, streamRegistry, options) {
1530
- const global = context.global;
1531
1559
  const fetchAbortControllers = new Map;
1532
- global.setSync("__fetch_abort", new import_isolated_vm.default.Callback((fetchId) => {
1560
+ setTrackedGlobal(context, "__fetch_abort", new import_isolated_vm.default.Callback((fetchId) => {
1533
1561
  const controller = fetchAbortControllers.get(fetchId);
1534
1562
  if (controller) {
1535
1563
  setImmediate(() => controller.abort());
@@ -1624,7 +1652,7 @@ function setupFetchFunction(context, stateMap, streamRegistry, options) {
1624
1652
  fetchAbortControllers.delete(fetchId);
1625
1653
  }
1626
1654
  });
1627
- global.setSync("__fetch_ref", fetchRef);
1655
+ setTrackedGlobal(context, "__fetch_ref", fetchRef);
1628
1656
  const fetchCode = `
1629
1657
  (function() {
1630
1658
  function __decodeError(err) {
@@ -1709,12 +1737,11 @@ function setupFetchFunction(context, stateMap, streamRegistry, options) {
1709
1737
  context.evalSync(fetchCode);
1710
1738
  }
1711
1739
  function setupServer(context, serveState) {
1712
- const global = context.global;
1713
1740
  context.evalSync(`
1714
1741
  globalThis.__upgradeRegistry__ = new Map();
1715
1742
  globalThis.__upgradeIdCounter__ = 0;
1716
1743
  `);
1717
- global.setSync("__setPendingUpgrade__", new import_isolated_vm.default.Callback((connectionId) => {
1744
+ setTrackedGlobal(context, "__setPendingUpgrade__", new import_isolated_vm.default.Callback((connectionId) => {
1718
1745
  serveState.pendingUpgrade = { requested: true, connectionId };
1719
1746
  }));
1720
1747
  context.evalSync(`
@@ -1733,13 +1760,12 @@ function setupServer(context, serveState) {
1733
1760
  `);
1734
1761
  }
1735
1762
  function setupServerWebSocket(context, wsCommandCallbacks) {
1736
- const global = context.global;
1737
- global.setSync("__ServerWebSocket_send", new import_isolated_vm.default.Callback((connectionId, data) => {
1763
+ setTrackedGlobal(context, "__ServerWebSocket_send", new import_isolated_vm.default.Callback((connectionId, data) => {
1738
1764
  const cmd = { type: "message", connectionId, data };
1739
1765
  for (const cb of wsCommandCallbacks)
1740
1766
  cb(cmd);
1741
1767
  }));
1742
- global.setSync("__ServerWebSocket_close", new import_isolated_vm.default.Callback((connectionId, code, reason) => {
1768
+ setTrackedGlobal(context, "__ServerWebSocket_close", new import_isolated_vm.default.Callback((connectionId, code, reason) => {
1743
1769
  const cmd = { type: "close", connectionId, code, reason };
1744
1770
  for (const cb of wsCommandCallbacks)
1745
1771
  cb(cmd);
@@ -1792,8 +1818,7 @@ function setupServerWebSocket(context, wsCommandCallbacks) {
1792
1818
  `);
1793
1819
  }
1794
1820
  function setupClientWebSocket(context, clientWsCommandCallbacks) {
1795
- const global = context.global;
1796
- global.setSync("__WebSocket_connect", new import_isolated_vm.default.Callback((socketId, url, protocols) => {
1821
+ setTrackedGlobal(context, "__WebSocket_connect", new import_isolated_vm.default.Callback((socketId, url, protocols) => {
1797
1822
  const cmd = {
1798
1823
  type: "connect",
1799
1824
  socketId,
@@ -1803,12 +1828,12 @@ function setupClientWebSocket(context, clientWsCommandCallbacks) {
1803
1828
  for (const cb of clientWsCommandCallbacks)
1804
1829
  cb(cmd);
1805
1830
  }));
1806
- global.setSync("__WebSocket_send", new import_isolated_vm.default.Callback((socketId, data) => {
1831
+ setTrackedGlobal(context, "__WebSocket_send", new import_isolated_vm.default.Callback((socketId, data) => {
1807
1832
  const cmd = { type: "send", socketId, data };
1808
1833
  for (const cb of clientWsCommandCallbacks)
1809
1834
  cb(cmd);
1810
1835
  }));
1811
- global.setSync("__WebSocket_close", new import_isolated_vm.default.Callback((socketId, code, reason) => {
1836
+ setTrackedGlobal(context, "__WebSocket_close", new import_isolated_vm.default.Callback((socketId, code, reason) => {
1812
1837
  const cmd = { type: "close", socketId, code, reason };
1813
1838
  for (const cb of clientWsCommandCallbacks)
1814
1839
  cb(cmd);
@@ -2194,7 +2219,7 @@ async function setupFetch(context, options) {
2194
2219
  setupServe(context);
2195
2220
  setupClientWebSocket(context, clientWsCommandCallbacks);
2196
2221
  const eventCallbacks = new Set;
2197
- context.global.setSync("__emit", new import_isolated_vm.default.Callback((eventName, payloadJson) => {
2222
+ setTrackedGlobal(context, "__emit", new import_isolated_vm.default.Callback((eventName, payloadJson) => {
2198
2223
  const payload = JSON.parse(payloadJson);
2199
2224
  for (const cb of eventCallbacks)
2200
2225
  cb(eventName, payload);
@@ -2234,6 +2259,7 @@ async function setupFetch(context, options) {
2234
2259
  context.evalSync(`globalThis.__upgradeRegistry__.clear()`);
2235
2260
  serveState.activeConnections.clear();
2236
2261
  serveState.pendingUpgrade = null;
2262
+ releaseTrackedGlobalHandles(context);
2237
2263
  try {
2238
2264
  requestAbortSignalRef.release();
2239
2265
  } catch {}
@@ -2323,7 +2349,8 @@ async function setupFetch(context, options) {
2323
2349
  passthruMap.delete(responseState.streamId);
2324
2350
  const responseHeaders2 = new Headers(responseState.headers);
2325
2351
  const status2 = responseState.status === 101 ? 200 : responseState.status;
2326
- const response2 = new Response(passthruBody, {
2352
+ const hasNullBodyStatus2 = responseState.status === 101 || responseState.status === 103 || responseState.status === 204 || responseState.status === 205 || responseState.status === 304;
2353
+ const response2 = new Response(hasNullBodyStatus2 ? null : passthruBody, {
2327
2354
  status: status2,
2328
2355
  statusText: responseState.statusText,
2329
2356
  headers: responseHeaders2
@@ -2373,7 +2400,12 @@ async function setupFetch(context, options) {
2373
2400
  });
2374
2401
  const responseHeaders2 = new Headers(responseState.headers);
2375
2402
  const status2 = responseState.status === 101 ? 200 : responseState.status;
2376
- const response2 = new Response(pumpedStream, {
2403
+ const hasNullBodyStatus2 = responseState.status === 101 || responseState.status === 103 || responseState.status === 204 || responseState.status === 205 || responseState.status === 304;
2404
+ if (hasNullBodyStatus2) {
2405
+ streamRegistry.cancel(responseStreamId);
2406
+ streamRegistry.delete(responseStreamId);
2407
+ }
2408
+ const response2 = new Response(hasNullBodyStatus2 ? null : pumpedStream, {
2377
2409
  status: status2,
2378
2410
  statusText: responseState.statusText,
2379
2411
  headers: responseHeaders2
@@ -2382,7 +2414,8 @@ async function setupFetch(context, options) {
2382
2414
  return response2;
2383
2415
  }
2384
2416
  const responseHeaders = new Headers(responseState.headers);
2385
- const responseBody = responseState.body;
2417
+ const hasNullBodyStatus = responseState.status === 101 || responseState.status === 103 || responseState.status === 204 || responseState.status === 205 || responseState.status === 304;
2418
+ const responseBody = hasNullBodyStatus ? null : responseState.body;
2386
2419
  const status = responseState.status === 101 ? 200 : responseState.status;
2387
2420
  const response = new Response(responseBody, {
2388
2421
  status,
@@ -2577,4 +2610,4 @@ async function setupFetch(context, options) {
2577
2610
  };
2578
2611
  }
2579
2612
 
2580
- //# debugId=9791957F393D36A464756E2164756E21
2613
+ //# debugId=515350DA36E4FB2764756E2164756E21