sandboxedjs 0.1.43 → 0.1.44

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.js CHANGED
@@ -28794,9 +28794,19 @@ async function createPreview(box, options = {}) {
28794
28794
  });
28795
28795
  if (!activated) return null;
28796
28796
  }
28797
- const channel = new MessageChannel();
28798
- serveContainerOn(channel.port1, box);
28799
- worker.postMessage({ type: "sandboxedjs:connect" }, [channel.port2]);
28797
+ let channel = null;
28798
+ const connect = () => {
28799
+ channel?.port1.close();
28800
+ channel = new MessageChannel();
28801
+ serveContainerOn(channel.port1, box);
28802
+ const target = registration.active ?? worker;
28803
+ target.postMessage({ type: "sandboxedjs:connect" }, [channel.port2]);
28804
+ };
28805
+ const onWorkerMessage = (event) => {
28806
+ if (event.data?.type === "sandboxedjs:host-needed") connect();
28807
+ };
28808
+ navigator.serviceWorker.addEventListener("message", onWorkerMessage);
28809
+ connect();
28800
28810
  const base2 = registration.scope.replace(/\/$/, "");
28801
28811
  const urlFor = (port) => `${base2}/__sbx__/${port}/`;
28802
28812
  return {
@@ -28809,7 +28819,9 @@ async function createPreview(box, options = {}) {
28809
28819
  return `${urlFor(target.port).replace(/\/$/, "")}${path}`;
28810
28820
  },
28811
28821
  dispose: async () => {
28812
- channel.port1.close();
28822
+ navigator.serviceWorker.removeEventListener("message", onWorkerMessage);
28823
+ channel?.port1.close();
28824
+ channel = null;
28813
28825
  await registration.unregister();
28814
28826
  }
28815
28827
  };