@kehto/paja 0.24.0 → 0.24.2

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/README.md CHANGED
@@ -69,7 +69,9 @@ usable; collapsing never reloads the running app or discards unsaved drafts.
69
69
  The console shows supported interfaces with per-domain injection toggles,
70
70
  runtime ACL controls, signer controls, and a filterable message log with visible
71
71
  error details. It starts expanded and collapses to the left with the chevron
72
- button in the top bar; the same button brings the whole panel back. Collapsing
72
+ button in the top bar; the same button brings the whole panel back. The toolbar
73
+ stays onscreen after restoring a saved collapsed preference, including when no
74
+ runtime tab is open. Collapsing
73
75
  is presentation-only — the target iframe keeps its identity and the loaded
74
76
  napplet keeps running — and Paja remembers the choice per browser origin. In
75
77
  runtime-pointer mode, ACL controls always display and mutate
@@ -10504,7 +10504,7 @@ function createUploadService(options) {
10504
10504
  const uploadInfo = options.uploadInfo ?? DEFAULT_UPLOAD_INFO;
10505
10505
  const generateId2 = options.generateId ?? (() => crypto.randomUUID());
10506
10506
  const now2 = options.now ?? (() => Date.now());
10507
- const entries = /* @__PURE__ */ new Map();
10507
+ const windows = /* @__PURE__ */ new Map();
10508
10508
  function normalizeUploadInfo(info) {
10509
10509
  return {
10510
10510
  rails: Array.isArray(info.rails) ? info.rails.filter((rail) => typeof rail?.rail === "string").map((rail) => ({
@@ -10525,7 +10525,7 @@ function createUploadService(options) {
10525
10525
  send({ type: "upload.info.result", id, error: toErrorMessage2(err2) });
10526
10526
  });
10527
10527
  }
10528
- function handleUpload(windowId, msg, send) {
10528
+ function handleUpload(windowId, msg, send, entries) {
10529
10529
  const m = msg;
10530
10530
  const id = m.id ?? "";
10531
10531
  const request = m.request;
@@ -10534,29 +10534,31 @@ function createUploadService(options) {
10534
10534
  return;
10535
10535
  }
10536
10536
  const uploadId = generateId2();
10537
- const key = `${windowId}:${uploadId}`;
10538
- entries.set(key, { uploadId });
10537
+ const entry = { uploadId };
10538
+ entries.set(uploadId, entry);
10539
+ const isActive = () => windows.get(windowId) === entries && entries.get(uploadId) === entry;
10539
10540
  const ctx = {
10540
10541
  uploadId,
10541
10542
  windowId,
10542
10543
  onStatus: (status) => {
10544
+ if (!isActive()) return;
10543
10545
  const stamped = { ...status, uploadId, updatedAt: status.updatedAt || now2() };
10544
- const entry = entries.get(key);
10545
- if (entry) entry.status = stamped;
10546
+ entry.status = stamped;
10546
10547
  send({ type: "upload.status.changed", status: stamped });
10547
10548
  }
10548
10549
  };
10549
10550
  void uploader.upload(request, ctx).then((result2) => {
10551
+ if (!isActive()) return;
10550
10552
  const stamped = { ...result2, uploadId };
10551
- const entry = entries.get(key);
10552
- if (entry) entry.status = { ...stamped, updatedAt: now2() };
10553
+ entry.status = { ...stamped, updatedAt: now2() };
10553
10554
  send({ type: "upload.upload.result", id, result: stamped });
10554
10555
  }).catch((err2) => {
10555
- entries.delete(key);
10556
+ if (!isActive()) return;
10557
+ entries.delete(uploadId);
10556
10558
  send({ type: "upload.upload.result", id, error: toErrorMessage2(err2) });
10557
10559
  });
10558
10560
  }
10559
- function handleStatus(windowId, msg, send) {
10561
+ function handleStatus(msg, send, entries) {
10560
10562
  const m = msg;
10561
10563
  const id = m.id ?? "";
10562
10564
  const uploadId = m.uploadId;
@@ -10564,7 +10566,7 @@ function createUploadService(options) {
10564
10566
  send({ type: "upload.status.result", id, error: "invalid uploadId" });
10565
10567
  return;
10566
10568
  }
10567
- const tracked = entries.get(`${windowId}:${uploadId}`)?.status;
10569
+ const tracked = entries.get(uploadId)?.status;
10568
10570
  if (tracked) {
10569
10571
  send({ type: "upload.status.result", id, status: tracked });
10570
10572
  return;
@@ -10581,31 +10583,52 @@ function createUploadService(options) {
10581
10583
  }
10582
10584
  send({ type: "upload.status.result", id, error: "unknown upload" });
10583
10585
  }
10586
+ function cancelUploads(entries) {
10587
+ if (!entries) return;
10588
+ for (const entry of entries.values()) {
10589
+ try {
10590
+ uploader.cancel?.(entry.uploadId);
10591
+ } catch {
10592
+ }
10593
+ }
10594
+ }
10595
+ function destroyWindow(windowId) {
10596
+ const entries = windows.get(windowId);
10597
+ windows.delete(windowId);
10598
+ cancelUploads(entries);
10599
+ }
10584
10600
  return {
10585
10601
  descriptor: UPLOAD_DESCRIPTOR,
10586
10602
  handleMessage(windowId, message2, send) {
10603
+ if (!["upload.info", "upload.upload", "upload.status"].includes(message2.type)) return;
10604
+ let entries = windows.get(windowId);
10605
+ if (!entries) {
10606
+ entries = /* @__PURE__ */ new Map();
10607
+ windows.set(windowId, entries);
10608
+ }
10609
+ const lifetime = entries;
10610
+ const sendIfActive = (response) => {
10611
+ if (windows.get(windowId) === lifetime) send(response);
10612
+ };
10587
10613
  switch (message2.type) {
10588
10614
  case "upload.info":
10589
- handleInfo2(windowId, message2, send);
10615
+ handleInfo2(windowId, message2, sendIfActive);
10590
10616
  return;
10591
10617
  case "upload.upload":
10592
- handleUpload(windowId, message2, send);
10618
+ handleUpload(windowId, message2, sendIfActive, entries);
10593
10619
  return;
10594
10620
  case "upload.status":
10595
- handleStatus(windowId, message2, send);
10621
+ handleStatus(message2, sendIfActive, entries);
10596
10622
  return;
10597
10623
  default:
10598
10624
  return;
10599
10625
  }
10600
10626
  },
10601
- onWindowDestroyed(windowId) {
10602
- const prefix = `${windowId}:`;
10603
- for (const [key, entry] of entries) {
10604
- if (key.startsWith(prefix)) {
10605
- uploader.cancel?.(entry.uploadId);
10606
- entries.delete(key);
10607
- }
10608
- }
10627
+ onWindowDestroyed: destroyWindow,
10628
+ onUnregistered() {
10629
+ const pending = [...windows.values()];
10630
+ windows.clear();
10631
+ for (const entries of pending) cancelUploads(entries);
10609
10632
  }
10610
10633
  };
10611
10634
  }
@@ -22770,25 +22793,32 @@ function createNipEncryptionMethods(state, prefix) {
22770
22793
  decrypt: async (pubkey2, ciphertext) => sendRequestWithId(state, crypto.randomUUID(), `${prefix}_decrypt`, [pubkey2, ciphertext])
22771
22794
  };
22772
22795
  }
22796
+ var HEX_PUBKEY2 = /^[0-9a-f]{64}$/i;
22797
+ function readPubkey(value) {
22798
+ if (typeof value === "string") {
22799
+ const normalized = value.trim().toLowerCase();
22800
+ return HEX_PUBKEY2.test(normalized) ? normalized : null;
22801
+ }
22802
+ if (value && typeof value === "object" && "pubkey" in value) {
22803
+ return readPubkey(value.pubkey);
22804
+ }
22805
+ return null;
22806
+ }
22807
+ async function requestPublicKey(state) {
22808
+ const result2 = await sendRequestWithId(state, crypto.randomUUID(), "get_public_key", []);
22809
+ const pubkey2 = readPubkey(result2);
22810
+ if (!pubkey2) throw new Error("NIP-46 signer returned an invalid public key");
22811
+ state.authorizedPubkey = pubkey2;
22812
+ return pubkey2;
22813
+ }
22773
22814
  async function connectNip46State(state, secret) {
22774
22815
  await openWebSocket(state);
22775
- const result2 = await sendRequestWithId(state, crypto.randomUUID(), "connect", [state.bunkerPubkey, secret ?? ""]);
22776
- if (typeof result2 === "string") {
22777
- state.authorizedPubkey = result2;
22778
- } else if (result2 && typeof result2 === "object" && "pubkey" in result2) {
22779
- state.authorizedPubkey = result2.pubkey;
22780
- } else {
22781
- const pkResult = await sendRequestWithId(state, crypto.randomUUID(), "get_public_key", []);
22782
- state.authorizedPubkey = pkResult;
22783
- }
22784
- return state.authorizedPubkey;
22816
+ await sendRequestWithId(state, crypto.randomUUID(), "connect", [state.bunkerPubkey, secret ?? ""]);
22817
+ return requestPublicKey(state);
22785
22818
  }
22786
22819
  function createNip46Signer(state) {
22787
22820
  return {
22788
- getPublicKey: async () => {
22789
- if (state.authorizedPubkey) return state.authorizedPubkey;
22790
- return sendRequestWithId(state, crypto.randomUUID(), "get_public_key", []);
22791
- },
22821
+ getPublicKey: async () => state.authorizedPubkey ?? requestPublicKey(state),
22792
22822
  signEvent: async (event) => {
22793
22823
  const result2 = await sendRequestWithId(state, crypto.randomUUID(), "sign_event", [JSON.stringify(event)]);
22794
22824
  if (typeof result2 === "string") {