@kehto/paja 0.32.0 → 0.32.1

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.
@@ -3736,6 +3736,25 @@ function setGlobalRate(config, napplet, limit) {
3736
3736
  }
3737
3737
  };
3738
3738
  }
3739
+ function setUnfocusedMultiplier(config, multiplier) {
3740
+ if (!Number.isFinite(multiplier) || multiplier <= 0) {
3741
+ throw new RangeError("unfocusedMultiplier must be a finite number above zero");
3742
+ }
3743
+ return { ...config, unfocusedMultiplier: multiplier };
3744
+ }
3745
+ function setBurstGuard(config, guard) {
3746
+ const { windowMs, maxOps, action } = guard;
3747
+ if (!Number.isFinite(windowMs) || windowMs < 0) {
3748
+ throw new RangeError("burstGuard.windowMs must be a finite number of zero or more");
3749
+ }
3750
+ if (!Number.isSafeInteger(maxOps) || maxOps < 0) {
3751
+ throw new RangeError("burstGuard.maxOps must be a non-negative safe integer");
3752
+ }
3753
+ if (!isValidAction(action)) {
3754
+ throw new RangeError("burstGuard.action must be 'flag', 'block' or 'ignore'");
3755
+ }
3756
+ return { ...config, burstGuard: { windowMs, maxOps, action } };
3757
+ }
3739
3758
  function addMatcher(config, matcher) {
3740
3759
  return { ...config, matchers: [...config.matchers, matcher] };
3741
3760
  }
@@ -4199,7 +4218,7 @@ function bitsToCapabilities(bits) {
4199
4218
  function toIdentity(pubkey2, dTag, hash) {
4200
4219
  return { pubkey: pubkey2, dTag, hash };
4201
4220
  }
4202
- function createAclState(persistence, defaultPolicy = "permissive", onMutation) {
4221
+ function createAclState(persistence, defaultPolicy = "permissive", onMutation, pinDefaultPolicy = false) {
4203
4222
  let state = createState(defaultPolicy);
4204
4223
  function ensureRuntimeDefaultEntry(id) {
4205
4224
  if (state.defaultPolicy !== "permissive") return;
@@ -4288,7 +4307,8 @@ function createAclState(persistence, defaultPolicy = "permissive", onMutation) {
4288
4307
  try {
4289
4308
  const raw = persistence.load();
4290
4309
  if (!raw) return;
4291
- state = deserialize(raw);
4310
+ const loaded = deserialize(raw);
4311
+ state = pinDefaultPolicy ? { ...loaded, defaultPolicy } : loaded;
4292
4312
  } catch {
4293
4313
  state = createState(defaultPolicy);
4294
4314
  }
@@ -4328,6 +4348,12 @@ function createFirewallState(persistence) {
4328
4348
  setGlobalRate(napplet, limit) {
4329
4349
  config = setGlobalRate(config, napplet, limit);
4330
4350
  },
4351
+ setUnfocusedMultiplier(multiplier) {
4352
+ config = setUnfocusedMultiplier(config, multiplier);
4353
+ },
4354
+ setBurstGuard(guard) {
4355
+ config = setBurstGuard(config, guard);
4356
+ },
4331
4357
  addMatcher(matcher) {
4332
4358
  config = addMatcher(config, matcher);
4333
4359
  },
@@ -6158,7 +6184,7 @@ function createRuntime(hooks) {
6158
6184
  extensions?.destroyWindow(windowId);
6159
6185
  });
6160
6186
  let incRuntime = null;
6161
- const aclState = createAclState(hooks.aclPersistence, "permissive", (mutation) => {
6187
+ const aclState = createAclState(hooks.aclPersistence, hooks.aclDefaultPolicy ?? "permissive", (mutation) => {
6162
6188
  for (const entry of sessionRegistry2.getAllEntries()) {
6163
6189
  if (entry.dTag === mutation.dTag && entry.aggregateHash === mutation.aggregateHash) {
6164
6190
  for (const service of Object.values(serviceRegistry)) {
@@ -6170,7 +6196,7 @@ function createRuntime(hooks) {
6170
6196
  }
6171
6197
  }
6172
6198
  }
6173
- });
6199
+ }, hooks.aclDefaultPolicy !== void 0);
6174
6200
  const manifestCache2 = createManifestCache(hooks.manifestPersistence);
6175
6201
  const replayDetector = createReplayDetector(
6176
6202
  hooks.getConfigOverrides ? () => hooks.getConfigOverrides().replayWindowSeconds : void 0
@@ -16962,9 +16988,13 @@ function adaptHooks(shellHooks, deps) {
16962
16988
  shellSecretPersistence: createShellSecretPersistence(),
16963
16989
  guidPersistence: createGuidPersistence(),
16964
16990
  onAclCheck: shellHooks.onAclCheck,
16991
+ firewallPersistence: shellHooks.firewallPersistence,
16992
+ onFirewallEvent: shellHooks.onFirewallEvent,
16993
+ getFocusContext: shellHooks.getFocusContext,
16965
16994
  onHashMismatch: shellHooks.onHashMismatch,
16966
16995
  services: shellHooks.services,
16967
- getConfigOverrides: shellHooks.getConfigOverrides
16996
+ getConfigOverrides: shellHooks.getConfigOverrides,
16997
+ aclDefaultPolicy: shellHooks.aclDefaultPolicy
16968
16998
  };
16969
16999
  }
16970
17000
  var registry = /* @__PURE__ */ new Map();
@@ -17910,6 +17940,7 @@ function hasNappletFramePolicy(doc) {
17910
17940
  return [...doc.querySelectorAll('meta[http-equiv="Content-Security-Policy" i]')].some((meta) => meta.content.split(";").some((directive) => directive.trim() === NAPPLET_FRAME_POLICY));
17911
17941
  }
17912
17942
  var NAPPLET_HELLO = "napplet.hello";
17943
+ var MAX_DEFERRED_MESSAGES = 1024;
17913
17944
  function reportUnrouted(hooks, event, reason) {
17914
17945
  if (!hooks.onUnroutedMessage) return;
17915
17946
  const data = event.data;
@@ -17925,8 +17956,18 @@ var REVOCATION_DETAIL = {
17925
17956
  "invalid-hello": "napplet.hello did not carry exactly one MessagePort",
17926
17957
  "second-load": "The frame loaded a second document (navigation or reload)",
17927
17958
  "port-closed": "The bound NIP-5D port closed or failed",
17959
+ "deferred-overflow": `More than ${MAX_DEFERRED_MESSAGES} deferred messages were waiting for this frame`,
17928
17960
  host: "The host revoked the napplet binding"
17929
17961
  };
17962
+ function decideDispatch(hooks, windowId, envelope) {
17963
+ if (!hooks.filterNappletMessage) return "dispatch";
17964
+ try {
17965
+ const decision = hooks.filterNappletMessage(windowId, envelope);
17966
+ return decision === "dispatch" || decision === "defer" ? decision : "drop";
17967
+ } catch {
17968
+ return "drop";
17969
+ }
17970
+ }
17930
17971
  function isHello(data) {
17931
17972
  return typeof data === "object" && data !== null && data.type === NAPPLET_HELLO;
17932
17973
  }
@@ -17939,7 +17980,29 @@ function warnWithoutFramePolicy(frame) {
17939
17980
  `[kehto] The host page has no "${NAPPLET_FRAME_POLICY}" CSP. A napplet that navigates its frame before its first load is invisible to NIP-5D's replacement evidence; add renderNappletFramePolicyMeta() to the host page <head>.`
17940
17981
  );
17941
17982
  }
17942
- function createNip5dTransport({ hooks, runtime, state: shellReadyState }) {
17983
+ function observeFrameLoads(frame, onSecondLoad) {
17984
+ warnWithoutFramePolicy(frame);
17985
+ let loads = 0;
17986
+ const onLoad = () => {
17987
+ loads += 1;
17988
+ if (loads > 1) onSecondLoad();
17989
+ };
17990
+ frame.addEventListener("load", onLoad);
17991
+ return () => frame.removeEventListener("load", onLoad);
17992
+ }
17993
+ function sessionContext({ hooks, runtime, state }, windowId, binding) {
17994
+ return {
17995
+ hooks,
17996
+ origin: binding.origin,
17997
+ runtime,
17998
+ state,
17999
+ sourceRegistrationId: binding.registrationId,
18000
+ sourceWindow: binding.sourceWindow,
18001
+ windowId
18002
+ };
18003
+ }
18004
+ function createNip5dTransport(options) {
18005
+ const { hooks, runtime, state: shellReadyState } = options;
17943
18006
  const bindings = /* @__PURE__ */ new Map();
17944
18007
  function currentBinding(windowId) {
17945
18008
  const binding = bindings.get(windowId);
@@ -17973,22 +18036,36 @@ function createNip5dTransport({ hooks, runtime, state: shellReadyState }) {
17973
18036
  hooks.onNappletMessage?.(windowId, envelope);
17974
18037
  } catch {
17975
18038
  }
17976
- const context = {
17977
- hooks,
17978
- origin: binding.origin,
17979
- runtime,
17980
- state: shellReadyState,
17981
- sourceRegistrationId: binding.registrationId,
17982
- sourceWindow: binding.sourceWindow,
17983
- windowId
17984
- };
18039
+ const context = sessionContext(options, windowId, binding);
17985
18040
  if (!registerSourceSession(context)) return;
17986
18041
  if (envelope.type === "shell.ready") {
17987
18042
  handleShellReady(context);
17988
18043
  return;
17989
18044
  }
18045
+ const decision = decideDispatch(hooks, windowId, envelope);
18046
+ if (decision === "drop" || currentBinding(windowId) !== binding) return;
18047
+ if (decision === "defer" || binding.deferred.length > 0) {
18048
+ if (binding.deferred.length >= MAX_DEFERRED_MESSAGES) {
18049
+ revokeNapplet(windowId, "deferred-overflow");
18050
+ return;
18051
+ }
18052
+ binding.deferred.push(envelope);
18053
+ return;
18054
+ }
17990
18055
  runtime.handleMessage(windowId, envelope);
17991
18056
  }
18057
+ function releaseDeferred(windowId) {
18058
+ const binding = currentBinding(windowId);
18059
+ if (!binding) return 0;
18060
+ let released = 0;
18061
+ while (binding.deferred.length > 0 && currentBinding(windowId) === binding) {
18062
+ const envelope = binding.deferred.shift();
18063
+ if (!registerSourceSession(sessionContext(options, windowId, binding))) continue;
18064
+ runtime.handleMessage(windowId, envelope);
18065
+ released += 1;
18066
+ }
18067
+ return released;
18068
+ }
17992
18069
  function bindPort(windowId, sourceWindow, origin, port) {
17993
18070
  if (!originRegistry.bindPort(windowId, port)) {
17994
18071
  port.close();
@@ -17999,7 +18076,8 @@ function createNip5dTransport({ hooks, runtime, state: shellReadyState }) {
17999
18076
  port,
18000
18077
  sourceWindow,
18001
18078
  origin,
18002
- registrationId: originRegistry.getRegistrationId(sourceWindow) ?? 0
18079
+ registrationId: originRegistry.getRegistrationId(sourceWindow) ?? 0,
18080
+ deferred: []
18003
18081
  };
18004
18082
  bindings.set(windowId, binding);
18005
18083
  const isCurrent = () => currentBinding(windowId) === binding;
@@ -18041,14 +18119,7 @@ function createNip5dTransport({ hooks, runtime, state: shellReadyState }) {
18041
18119
  bindPort(windowId, sourceWindow, event.origin, ports[0]);
18042
18120
  }
18043
18121
  function observeFrame(frame, windowId) {
18044
- warnWithoutFramePolicy(frame);
18045
- let loads = 0;
18046
- const onLoad = () => {
18047
- loads += 1;
18048
- if (loads > 1) revokeNapplet(windowId, "second-load");
18049
- };
18050
- frame.addEventListener("load", onLoad);
18051
- return () => frame.removeEventListener("load", onLoad);
18122
+ return observeFrameLoads(frame, () => revokeNapplet(windowId, "second-load"));
18052
18123
  }
18053
18124
  function destroy() {
18054
18125
  for (const [windowId, binding] of bindings) {
@@ -18057,7 +18128,7 @@ function createNip5dTransport({ hooks, runtime, state: shellReadyState }) {
18057
18128
  }
18058
18129
  bindings.clear();
18059
18130
  }
18060
- return { handleMessage, revokeNapplet, observeFrame, destroy };
18131
+ return { handleMessage, revokeNapplet, releaseDeferred, observeFrame, destroy };
18061
18132
  }
18062
18133
  function createShellBridge(hooks) {
18063
18134
  const shellReadyState = createShellReadyState();
@@ -18104,6 +18175,7 @@ function createShellBridge(hooks) {
18104
18175
  },
18105
18176
  observeFrame: transport.observeFrame,
18106
18177
  revokeNapplet: transport.revokeNapplet,
18178
+ releaseDeferred: transport.releaseDeferred,
18107
18179
  get runtime() {
18108
18180
  return runtime;
18109
18181
  }