@kehto/paja 0.16.3 → 0.16.4

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.
@@ -702,7 +702,7 @@ function createNapEnforceGate(config) {
702
702
  function formatDenialReason(capability) {
703
703
  return `denied: ${capability}`;
704
704
  }
705
- function createSessionRegistry(notifier) {
705
+ function createSessionRegistry(notifier, onRegister) {
706
706
  const byWindowId2 = /* @__PURE__ */ new Map();
707
707
  const byPubkey2 = /* @__PURE__ */ new Map();
708
708
  const byWindowIdEntry = /* @__PURE__ */ new Map();
@@ -712,6 +712,7 @@ function createSessionRegistry(notifier) {
712
712
  byWindowId2.set(windowId, entry.pubkey);
713
713
  byPubkey2.set(entry.pubkey, entry);
714
714
  byWindowIdEntry.set(windowId, entry);
715
+ onRegister?.(windowId);
715
716
  },
716
717
  unregister(windowId) {
717
718
  const pubkey2 = byWindowId2.get(windowId);
@@ -931,6 +932,12 @@ function createFirewallState(persistence) {
931
932
  counters = result2.newState;
932
933
  return result2;
933
934
  },
935
+ resetInitBudget(initKey) {
936
+ if (!Object.hasOwn(counters.bursts, initKey)) return;
937
+ const bursts = { ...counters.bursts };
938
+ delete bursts[initKey];
939
+ counters = { ...counters, bursts };
940
+ },
934
941
  setPolicy(napplet, policy) {
935
942
  config = setPolicy(config, napplet, policy);
936
943
  },
@@ -2416,8 +2423,8 @@ function buildObservation(envelope, windowId, senderCap, sessionRegistry2, getFo
2416
2423
  size,
2417
2424
  initElapsedMs,
2418
2425
  // `windowId` comes from the host's source-bound session lookup, never the
2419
- // napplet message envelope. It scopes startup burst accounting to this
2420
- // registered realm while rate limits remain dTag-wide.
2426
+ // napplet message envelope. Trusted register() calls retire this key's
2427
+ // previous startup budget; ordinary messages cannot reset it.
2421
2428
  initKey: windowId,
2422
2429
  focused: focus.focused,
2423
2430
  msSinceFocusGain: focus.msSinceFocusGain,
@@ -2664,7 +2671,10 @@ function createRuntime(hooks) {
2664
2671
  const subscriptions = /* @__PURE__ */ new Map();
2665
2672
  const serviceRegistry = { ...hooks.services };
2666
2673
  const registeredServices = createRegisteredServices(serviceRegistry);
2667
- const sessionRegistry2 = createSessionRegistry(hooks.onPendingUpdate);
2674
+ const firewallState = createFirewallState(hooks.firewallPersistence);
2675
+ const sessionRegistry2 = createSessionRegistry(hooks.onPendingUpdate, (windowId) => {
2676
+ firewallState.resetInitBudget(windowId);
2677
+ });
2668
2678
  let incRuntime = null;
2669
2679
  const aclState = createAclState(hooks.aclPersistence, "permissive", (mutation) => {
2670
2680
  if (mutation.type === "revoke" && mutation.capability !== "relay:read") return;
@@ -2674,7 +2684,6 @@ function createRuntime(hooks) {
2674
2684
  }
2675
2685
  }
2676
2686
  });
2677
- const firewallState = createFirewallState(hooks.firewallPersistence);
2678
2687
  const manifestCache2 = createManifestCache(hooks.manifestPersistence);
2679
2688
  const replayDetector = createReplayDetector(
2680
2689
  hooks.getConfigOverrides ? () => hooks.getConfigOverrides().replayWindowSeconds : void 0
@@ -4033,16 +4042,29 @@ function nappletNamespacePrelude(domains) {
4033
4042
  function request(message, resultType, map, options = {}) {
4034
4043
  const requestId = message.id ?? id();
4035
4044
  const outbound = { ...message, id: requestId };
4045
+ const timeoutMs = options.timeoutMs === void 0 ? requestTimeoutMs : options.timeoutMs;
4046
+ if (options.signal?.aborted) return Promise.reject(new DOMException("Aborted", "AbortError"));
4036
4047
  return new Promise((resolve, reject2) => {
4037
- const off = listen((event) => {
4048
+ let timer;
4049
+ let off;
4050
+ const cleanup = () => {
4051
+ off?.();
4052
+ if (timer !== void 0) clearTimeout(timer);
4053
+ options.signal?.removeEventListener("abort", abort);
4054
+ };
4055
+ const abort = () => {
4056
+ cleanup();
4057
+ if (options.cancelOnAbort) post({ type: "resource.cancel", id: requestId });
4058
+ reject2(new DOMException("Aborted", "AbortError"));
4059
+ };
4060
+ off = listen((event) => {
4038
4061
  if (!isParentMessage(event)) return;
4039
4062
  const incoming = event.data;
4040
4063
  if (typeof incoming !== "object" || incoming === null) return;
4041
4064
  const msg = incoming;
4042
4065
  if (msg.type !== resultType && msg.type !== `${message.type}.error`) return;
4043
4066
  if (msg.id !== requestId) return;
4044
- off();
4045
- clearTimeout(timer);
4067
+ cleanup();
4046
4068
  const error = typeof msg.error === "string" ? msg.error : "";
4047
4069
  if (error && options.rejectOnError !== false) {
4048
4070
  reject2(new Error(error));
@@ -4054,10 +4076,13 @@ function nappletNamespacePrelude(domains) {
4054
4076
  reject2(err2);
4055
4077
  }
4056
4078
  });
4057
- const timer = setTimeout(() => {
4058
- off();
4059
- reject2(new Error(`${message.type} timed out`));
4060
- }, requestTimeoutMs);
4079
+ if (timeoutMs !== null) {
4080
+ timer = setTimeout(() => {
4081
+ cleanup();
4082
+ reject2(new Error(`${message.type} timed out`));
4083
+ }, timeoutMs);
4084
+ }
4085
+ options.signal?.addEventListener("abort", abort, { once: true });
4061
4086
  post(outbound);
4062
4087
  });
4063
4088
  }
@@ -4806,17 +4831,25 @@ function nappletNamespacePrelude(domains) {
4806
4831
  type: "resource.bytes",
4807
4832
  url,
4808
4833
  ...Array.isArray(options?.servers) ? { servers: Array.from(options.servers) } : {}
4809
- }, "resource.bytes.result", (msg) => msg.blob);
4834
+ }, "resource.bytes.result", (msg) => msg.blob, {
4835
+ timeoutMs: null,
4836
+ signal: options?.signal,
4837
+ cancelOnAbort: true
4838
+ });
4810
4839
  return {
4811
4840
  info: () => request({ type: "resource.info" }, "resource.info.result", (msg) => msg.info),
4812
4841
  bytes,
4813
- bytesMany: (requests) => request({
4842
+ bytesMany: (requests, options) => request({
4814
4843
  type: "resource.bytesMany",
4815
4844
  requests: Array.from(requests || [], (entry) => ({
4816
4845
  url: entry.url,
4817
4846
  ...Array.isArray(entry.servers) ? { servers: Array.from(entry.servers) } : {}
4818
4847
  }))
4819
- }, "resource.bytesMany.result", (msg) => Array.isArray(msg.items) ? msg.items : []),
4848
+ }, "resource.bytesMany.result", (msg) => Array.isArray(msg.items) ? msg.items : [], {
4849
+ timeoutMs: null,
4850
+ signal: options?.signal,
4851
+ cancelOnAbort: true
4852
+ }),
4820
4853
  bytesAsObjectURL(url) {
4821
4854
  let objectUrl = "";
4822
4855
  const handle = {
@@ -9572,6 +9605,9 @@ function isValidFilterArray2(value) {
9572
9605
  function isPlainObject2(value) {
9573
9606
  return typeof value === "object" && value !== null && !Array.isArray(value);
9574
9607
  }
9608
+ function actionRegistryKey(windowId, actionId) {
9609
+ return JSON.stringify([windowId, actionId]);
9610
+ }
9575
9611
  var MODIFIER_ALIASES = {
9576
9612
  ctrl: "ctrl",
9577
9613
  control: "ctrl",
@@ -9640,7 +9676,7 @@ function bindingsForWindow(windowId, registry2, windowIndex) {
9640
9676
  if (!actionIds) return [];
9641
9677
  const bindings = [];
9642
9678
  for (const actionId of actionIds) {
9643
- const entry = registry2.get(actionId);
9679
+ const entry = registry2.get(actionRegistryKey(windowId, actionId));
9644
9680
  if (!entry || entry.windowId !== windowId) continue;
9645
9681
  bindings.push({ actionId, key: entry.chordString });
9646
9682
  }
@@ -9653,10 +9689,9 @@ function pushBindings(windowId, registry2, windowIndex, send) {
9653
9689
  };
9654
9690
  send(payload);
9655
9691
  }
9656
- function removeActionFromWindowIndex(actionId, windowIndex) {
9657
- for (const [wid, set] of windowIndex.entries()) {
9658
- if (set.delete(actionId) && set.size === 0) windowIndex.delete(wid);
9659
- }
9692
+ function removeActionFromWindowIndex(windowId, actionId, windowIndex) {
9693
+ const actions = windowIndex.get(windowId);
9694
+ if (actions?.delete(actionId) && actions.size === 0) windowIndex.delete(windowId);
9660
9695
  }
9661
9696
  var KEYS_SERVICE_VERSION = "1.3.0";
9662
9697
  function rememberActionForWindow(windowIndex, windowId, actionId) {
@@ -9664,7 +9699,7 @@ function rememberActionForWindow(windowIndex, windowId, actionId) {
9664
9699
  windowIndex.get(windowId).add(actionId);
9665
9700
  }
9666
9701
  function bindActionEntry(registry2, windowIndex, actionId, windowId, chord, chordString) {
9667
- registry2.set(actionId, { chord, chordString, windowId });
9702
+ registry2.set(actionRegistryKey(windowId, actionId), { actionId, chord, chordString, windowId });
9668
9703
  rememberActionForWindow(windowIndex, windowId, actionId);
9669
9704
  }
9670
9705
  function bindActionAndMarkChanged(registry2, windowIndex, changedWindowIds, actionId, windowId, chord, chordString) {
@@ -9737,6 +9772,7 @@ function createKeysService(options = {}) {
9737
9772
  const bridgeSendHandles = /* @__PURE__ */ new Map();
9738
9773
  const unsubscribeHandles = /* @__PURE__ */ new Map();
9739
9774
  const handleBridgeRegisterAction = (windowId, m, send) => {
9775
+ const registryKey = actionRegistryKey(windowId, m.action.id);
9740
9776
  bridgeSendHandles.set(windowId, send);
9741
9777
  let binding;
9742
9778
  const changedWindowIds = /* @__PURE__ */ new Set();
@@ -9744,8 +9780,8 @@ function createKeysService(options = {}) {
9744
9780
  try {
9745
9781
  const chord = parseChord(m.action.defaultKey);
9746
9782
  const normalizedChord = formatChord(chord);
9747
- const existingEntry = bridgeActionRegistry.get(m.action.id);
9748
- const existing = unsubscribeHandles.get(m.action.id);
9783
+ const existingEntry = bridgeActionRegistry.get(registryKey);
9784
+ const existing = unsubscribeHandles.get(registryKey);
9749
9785
  let nextUnsubscribe;
9750
9786
  if (!isUnavailableBinding(chord)) {
9751
9787
  nextUnsubscribe = bridge.subscribe(normalizedChord, (ev) => {
@@ -9771,13 +9807,13 @@ function createKeysService(options = {}) {
9771
9807
  existing();
9772
9808
  } catch {
9773
9809
  }
9774
- unsubscribeHandles.delete(m.action.id);
9810
+ unsubscribeHandles.delete(registryKey);
9775
9811
  }
9776
9812
  if (existingEntry) changedWindowIds.add(existingEntry.windowId);
9777
- removeActionFromWindowIndex(m.action.id, bridgeWindowActions);
9778
- bridgeActionRegistry.delete(m.action.id);
9813
+ removeActionFromWindowIndex(windowId, m.action.id, bridgeWindowActions);
9814
+ bridgeActionRegistry.delete(registryKey);
9779
9815
  if (nextUnsubscribe) {
9780
- unsubscribeHandles.set(m.action.id, nextUnsubscribe);
9816
+ unsubscribeHandles.set(registryKey, nextUnsubscribe);
9781
9817
  binding = bindActionAndMarkChanged(
9782
9818
  bridgeActionRegistry,
9783
9819
  bridgeWindowActions,
@@ -9807,18 +9843,19 @@ function createKeysService(options = {}) {
9807
9843
  send
9808
9844
  );
9809
9845
  };
9810
- const handleBridgeUnregisterAction = (_windowId, m, send) => {
9846
+ const handleBridgeUnregisterAction = (windowId, m, send) => {
9811
9847
  if (!m.actionId) return;
9812
- const unsubscribe = unsubscribeHandles.get(m.actionId);
9813
- const entry = bridgeActionRegistry.get(m.actionId);
9848
+ const registryKey = actionRegistryKey(windowId, m.actionId);
9849
+ const unsubscribe = unsubscribeHandles.get(registryKey);
9850
+ const entry = bridgeActionRegistry.get(registryKey);
9814
9851
  if (!unsubscribe) return;
9815
9852
  try {
9816
9853
  unsubscribe();
9817
9854
  } catch {
9818
9855
  }
9819
- unsubscribeHandles.delete(m.actionId);
9820
- bridgeActionRegistry.delete(m.actionId);
9821
- removeActionFromWindowIndex(m.actionId, bridgeWindowActions);
9856
+ unsubscribeHandles.delete(registryKey);
9857
+ bridgeActionRegistry.delete(registryKey);
9858
+ removeActionFromWindowIndex(windowId, m.actionId, bridgeWindowActions);
9822
9859
  if (entry) {
9823
9860
  const ownerSend = bridgeSendHandles.get(entry.windowId) ?? send;
9824
9861
  pushBindings(entry.windowId, bridgeActionRegistry, bridgeWindowActions, ownerSend);
@@ -9838,15 +9875,16 @@ function createKeysService(options = {}) {
9838
9875
  const actions = bridgeWindowActions.get(windowId);
9839
9876
  if (!actions) return;
9840
9877
  for (const actionId of actions) {
9841
- const unsubscribe = unsubscribeHandles.get(actionId);
9878
+ const registryKey = actionRegistryKey(windowId, actionId);
9879
+ const unsubscribe = unsubscribeHandles.get(registryKey);
9842
9880
  if (unsubscribe) {
9843
9881
  try {
9844
9882
  unsubscribe();
9845
9883
  } catch {
9846
9884
  }
9847
9885
  }
9848
- unsubscribeHandles.delete(actionId);
9849
- bridgeActionRegistry.delete(actionId);
9886
+ unsubscribeHandles.delete(registryKey);
9887
+ bridgeActionRegistry.delete(registryKey);
9850
9888
  }
9851
9889
  bridgeWindowActions.delete(windowId);
9852
9890
  bridgeSendHandles.delete(windowId);
@@ -9899,13 +9937,13 @@ function createKeysService(options = {}) {
9899
9937
  });
9900
9938
  }
9901
9939
  if (isReserved) return;
9902
- for (const [actionId, entry] of actionRegistry.entries()) {
9940
+ for (const entry of actionRegistry.values()) {
9903
9941
  if (chordMatches(entry.chord, ev)) {
9904
9942
  const send = sendHandles.get(entry.windowId);
9905
9943
  if (send) {
9906
9944
  const payload = {
9907
9945
  type: "keys.action",
9908
- actionId
9946
+ actionId: entry.actionId
9909
9947
  };
9910
9948
  send(payload);
9911
9949
  }
@@ -9914,6 +9952,7 @@ function createKeysService(options = {}) {
9914
9952
  };
9915
9953
  target.addEventListener("keydown", listener);
9916
9954
  const handleDocumentRegisterAction = (windowId, m, send) => {
9955
+ const registryKey = actionRegistryKey(windowId, m.action.id);
9917
9956
  sendHandles.set(windowId, send);
9918
9957
  let binding;
9919
9958
  const changedWindowIds = /* @__PURE__ */ new Set();
@@ -9921,11 +9960,11 @@ function createKeysService(options = {}) {
9921
9960
  try {
9922
9961
  const chord = parseChord(m.action.defaultKey);
9923
9962
  const normalizedChord = formatChord(chord);
9924
- const existing = actionRegistry.get(m.action.id);
9963
+ const existing = actionRegistry.get(registryKey);
9925
9964
  if (existing) {
9926
9965
  changedWindowIds.add(existing.windowId);
9927
- removeActionFromWindowIndex(m.action.id, windowActions);
9928
- actionRegistry.delete(m.action.id);
9966
+ removeActionFromWindowIndex(windowId, m.action.id, windowActions);
9967
+ actionRegistry.delete(registryKey);
9929
9968
  }
9930
9969
  if (!isUnavailableBinding(chord)) {
9931
9970
  binding = bindActionAndMarkChanged(actionRegistry, windowActions, changedWindowIds, m.action.id, windowId, chord, normalizedChord);
@@ -9943,10 +9982,12 @@ function createKeysService(options = {}) {
9943
9982
  sendRegisterActionResult2(send, m, binding);
9944
9983
  pushChangedBindings(changedWindowIds, actionRegistry, windowActions, sendHandles, send);
9945
9984
  };
9946
- const handleDocumentUnregisterAction = (_windowId, m, send) => {
9947
- if (!m.actionId || !actionRegistry.has(m.actionId)) return;
9948
- const entry = actionRegistry.get(m.actionId);
9949
- actionRegistry.delete(m.actionId);
9985
+ const handleDocumentUnregisterAction = (windowId, m, send) => {
9986
+ if (!m.actionId) return;
9987
+ const registryKey = actionRegistryKey(windowId, m.actionId);
9988
+ const entry = actionRegistry.get(registryKey);
9989
+ if (!entry) return;
9990
+ actionRegistry.delete(registryKey);
9950
9991
  const set = windowActions.get(entry.windowId);
9951
9992
  if (set) {
9952
9993
  set.delete(m.actionId);
@@ -9966,7 +10007,7 @@ function createKeysService(options = {}) {
9966
10007
  onWindowDestroyed(windowId) {
9967
10008
  const actions = windowActions.get(windowId);
9968
10009
  if (actions) {
9969
- for (const actionId of actions) actionRegistry.delete(actionId);
10010
+ for (const actionId of actions) actionRegistry.delete(actionRegistryKey(windowId, actionId));
9970
10011
  windowActions.delete(windowId);
9971
10012
  }
9972
10013
  sendHandles.delete(windowId);