sandboxedjs 0.1.43 → 0.1.45

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.
package/dist/index.cjs CHANGED
@@ -28811,9 +28811,21 @@ async function createPreview(box, options = {}) {
28811
28811
  });
28812
28812
  if (!activated) return null;
28813
28813
  }
28814
- const channel = new MessageChannel();
28815
- serveContainerOn(channel.port1, box);
28816
- worker.postMessage({ type: "sandboxedjs:connect" }, [channel.port2]);
28814
+ let channel = null;
28815
+ const connect = () => {
28816
+ channel?.port1.close();
28817
+ channel = new MessageChannel();
28818
+ serveContainerOn(channel.port1, box);
28819
+ const target = registration.active ?? worker;
28820
+ target.postMessage({ type: "sandboxedjs:connect" }, [channel.port2]);
28821
+ };
28822
+ const onWorkerMessage = (event) => {
28823
+ const type = event.data?.type;
28824
+ if (type === "sandboxedjs:host-needed") connect();
28825
+ if (type === "sandboxedjs:stale") options.onStale?.();
28826
+ };
28827
+ navigator.serviceWorker.addEventListener("message", onWorkerMessage);
28828
+ connect();
28817
28829
  const base2 = registration.scope.replace(/\/$/, "");
28818
28830
  const urlFor = (port) => `${base2}/__sbx__/${port}/`;
28819
28831
  return {
@@ -28826,7 +28838,9 @@ async function createPreview(box, options = {}) {
28826
28838
  return `${urlFor(target.port).replace(/\/$/, "")}${path}`;
28827
28839
  },
28828
28840
  dispose: async () => {
28829
- channel.port1.close();
28841
+ navigator.serviceWorker.removeEventListener("message", onWorkerMessage);
28842
+ channel?.port1.close();
28843
+ channel = null;
28830
28844
  await registration.unregister();
28831
28845
  }
28832
28846
  };