@grabbit-labs/dynafetch 0.2.2 → 0.2.3

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.
Binary file
Binary file
Binary file
Binary file
Binary file
package/dist/index.js CHANGED
@@ -814,6 +814,34 @@ function shouldSkipDynamicScriptUrl(url, pageUrl, policy) {
814
814
  }
815
815
 
816
816
  // ../../src/phantom/execute.ts
817
+ var esbuildModulePromise = null;
818
+ var esbuildRefCount = 0;
819
+ async function acquireEsbuildModule() {
820
+ if (!esbuildModulePromise) {
821
+ esbuildModulePromise = import("esbuild");
822
+ }
823
+ esbuildRefCount++;
824
+ try {
825
+ return await esbuildModulePromise;
826
+ } catch (error2) {
827
+ esbuildRefCount = Math.max(0, esbuildRefCount - 1);
828
+ if (esbuildRefCount === 0) esbuildModulePromise = null;
829
+ throw error2;
830
+ }
831
+ }
832
+ function releaseEsbuildModule(esbuildMod) {
833
+ if (!esbuildMod) return;
834
+ esbuildRefCount = Math.max(0, esbuildRefCount - 1);
835
+ if (esbuildRefCount > 0) return;
836
+ const stopFn = esbuildMod?.stop || esbuildMod?.default?.stop;
837
+ if (typeof stopFn === "function") {
838
+ try {
839
+ stopFn.call(esbuildMod?.default ?? esbuildMod);
840
+ } catch {
841
+ }
842
+ }
843
+ esbuildModulePromise = null;
844
+ }
817
845
  var Executor = class {
818
846
  constructor(harvestData, options = null) {
819
847
  this.logs = [];
@@ -845,6 +873,9 @@ var Executor = class {
845
873
  this.moduleInFlight = /* @__PURE__ */ new Map();
846
874
  // entryUrl -> promise
847
875
  this.windowClosed = false;
876
+ this.esbuildModule = null;
877
+ this.originalGlobalMessageChannel = void 0;
878
+ this.originalGlobalMessagePort = void 0;
848
879
  // Simple telemetry counters (useful for debugging).
849
880
  this.telemetry_stubbed = 0;
850
881
  this.telemetry_proxy = 0;
@@ -936,6 +967,16 @@ var Executor = class {
936
967
  if (!warning) return;
937
968
  this.warnings.add(warning);
938
969
  }
970
+ unrefNewMessagePorts(initialHandles) {
971
+ for (const handle of process._getActiveHandles()) {
972
+ if (initialHandles.has(handle)) continue;
973
+ if (handle?.constructor?.name !== "MessagePort") continue;
974
+ try {
975
+ handle.unref?.();
976
+ } catch {
977
+ }
978
+ }
979
+ }
939
980
  applyDefaults(quiescence, moduleWaitMsOverride) {
940
981
  const hardMaxCap = this.clampMs(Number(process.env.PHANTOM_QUIESCENCE_MAX_CAP_MS ?? 8e3), 500, 6e4);
941
982
  const minWaitMs = this.clampMs(quiescence?.minWaitMs ?? 75, 0, 1e4);
@@ -1213,7 +1254,8 @@ var Executor = class {
1213
1254
  if (!this.windowClosed) window.eval(cached);
1214
1255
  return;
1215
1256
  }
1216
- const esbuildMod = await import("esbuild");
1257
+ const esbuildMod = this.esbuildModule ?? await acquireEsbuildModule();
1258
+ this.esbuildModule = esbuildMod;
1217
1259
  const buildFn = esbuildMod?.build || esbuildMod?.default?.build;
1218
1260
  if (typeof buildFn !== "function") {
1219
1261
  throw new Error("esbuild.build not available (esbuild import failed)");
@@ -1439,8 +1481,27 @@ var Executor = class {
1439
1481
  }
1440
1482
  {
1441
1483
  const _g = globalThis;
1442
- if (!window.MessageChannel && _g.MessageChannel) window.MessageChannel = _g.MessageChannel;
1443
- if (!window.MessagePort && _g.MessagePort) window.MessagePort = _g.MessagePort;
1484
+ if (_g.MessageChannel) {
1485
+ if (this.originalGlobalMessageChannel === void 0) {
1486
+ this.originalGlobalMessageChannel = _g.MessageChannel;
1487
+ }
1488
+ const NativeMessageChannel = _g.MessageChannel;
1489
+ const UnrefMessageChannel = class MessageChannel extends NativeMessageChannel {
1490
+ constructor() {
1491
+ super();
1492
+ this.port1?.unref?.();
1493
+ this.port2?.unref?.();
1494
+ }
1495
+ };
1496
+ window.MessageChannel = UnrefMessageChannel;
1497
+ _g.MessageChannel = UnrefMessageChannel;
1498
+ }
1499
+ if (_g.MessagePort) {
1500
+ if (this.originalGlobalMessagePort === void 0) {
1501
+ this.originalGlobalMessagePort = _g.MessagePort;
1502
+ }
1503
+ window.MessagePort = _g.MessagePort;
1504
+ }
1444
1505
  }
1445
1506
  if (!window.requestIdleCallback) {
1446
1507
  window.requestIdleCallback = (cb) => window.setTimeout(() => cb({
@@ -1664,6 +1725,7 @@ var Executor = class {
1664
1725
  async execute() {
1665
1726
  const onNodeUncaught = (err) => this.recordExecutionError(err, "uncaughtException");
1666
1727
  const onNodeUnhandled = (reason) => this.recordExecutionError(reason, "unhandledRejection");
1728
+ const initialActiveHandles = new Set(process._getActiveHandles());
1667
1729
  process.on("uncaughtException", onNodeUncaught);
1668
1730
  process.on("unhandledRejection", onNodeUnhandled);
1669
1731
  try {
@@ -2091,8 +2153,20 @@ var Executor = class {
2091
2153
  };
2092
2154
  const shutdownGraceMs = this.clampMs(Number(process.env.PHANTOM_SHUTDOWN_GRACE_MS ?? 50), 10, 5e3);
2093
2155
  await new Promise((r) => setTimeout(r, shutdownGraceMs));
2156
+ this.unrefNewMessagePorts(initialActiveHandles);
2094
2157
  return result;
2095
2158
  } finally {
2159
+ const g = globalThis;
2160
+ if (this.originalGlobalMessageChannel !== void 0) {
2161
+ g.MessageChannel = this.originalGlobalMessageChannel;
2162
+ this.originalGlobalMessageChannel = void 0;
2163
+ }
2164
+ if (this.originalGlobalMessagePort !== void 0) {
2165
+ g.MessagePort = this.originalGlobalMessagePort;
2166
+ this.originalGlobalMessagePort = void 0;
2167
+ }
2168
+ releaseEsbuildModule(this.esbuildModule);
2169
+ this.esbuildModule = null;
2096
2170
  process.off("uncaughtException", onNodeUncaught);
2097
2171
  process.off("unhandledRejection", onNodeUnhandled);
2098
2172
  }