@nac3/runtime 2.5.1 → 2.6.0

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.
@@ -6,7 +6,7 @@
6
6
  =====================================================================
7
7
 
8
8
  AUTHORITATIVE VERSION CONSTANT: see the global.NAC export at the
9
- bottom of this file. `version: '2.5.1', spec_version: '2.5'`
9
+ bottom of this file. `version: '2.6.0', spec_version: '2.6'`
10
10
  is the normative declaration. The dated changelog sections below are
11
11
  historical notes; if they ever drift from the export, the export
12
12
  wins.
@@ -6930,8 +6930,13 @@
6930
6930
 
6931
6931
  const _v25_plugins = Object.create(null);
6932
6932
 
6933
- /* The 8 backend names from SPEC.md sec 15.4. Any other name in a
6934
- manifest is a hard reject at register time. */
6933
+ /* The 9 backend names from SPEC.md sec 15.4. Any other name in a
6934
+ manifest is a hard reject at register time. `emitter-listen` is
6935
+ the v2.6 addition (RFC V26-01 hook E-3): the only real event
6936
+ source before it was postMessage, so a plugin wrapping a library
6937
+ that emits its own events (map.on('moveend'),
6938
+ video.addEventListener('timeupdate'), pdf.js 'pagechanging')
6939
+ had to fall back to polling or to a hand-written mirror node. */
6935
6940
  const _V25_VALID_BACKENDS = {
6936
6941
  'postmessage': true,
6937
6942
  'postmessage-listen': true,
@@ -6941,8 +6946,96 @@
6941
6946
  'same-origin-window': true,
6942
6947
  'synthetic-mutation': true,
6943
6948
  'synthetic-poll': true,
6949
+ 'emitter-listen': true,
6944
6950
  };
6945
6951
 
6952
+ /* The closed list of NacError codes the plugin contract raises
6953
+ (SPEC.md sec 15.9), plus the namespace a plugin uses for a code
6954
+ of its own (v2.6, RFC V26-01 hook E-6). Before this, `call` lifted
6955
+ ANY string a backend put on err._code, so a plugin-specific code
6956
+ worked by accident of implementation rather than by contract --
6957
+ and a future runtime honouring the closed list could have killed
6958
+ it without breaking anything written down. */
6959
+ const _V25_ERROR_CODES = {
6960
+ 'plugin_not_registered': true,
6961
+ 'verb_not_declared': true,
6962
+ 'state_not_declared': true,
6963
+ 'event_not_declared': true,
6964
+ 'backend_unknown': true,
6965
+ 'backend_target_missing': true,
6966
+ 'cross_origin_blocked': true,
6967
+ 'state_unreadable': true,
6968
+ 'timeout': true,
6969
+ 'invalid': true,
6970
+ };
6971
+ const _V25_PLUGIN_CODE_PREFIX = 'plugin.';
6972
+
6973
+ /* Is `code` a code this contract lets a backend raise? Either one of
6974
+ the ten above, or a plugin-owned code under the reserved prefix.
6975
+ Anything else is `invalid` -- fail closed, and name what was
6976
+ refused on err.offending_code so the author can see it. */
6977
+ function _v25_isDeclaredCode(code) {
6978
+ if (typeof code !== 'string' || !code) return false;
6979
+ if (_V25_ERROR_CODES[code]) return true;
6980
+ return code.indexOf(_V25_PLUGIN_CODE_PREFIX) === 0 &&
6981
+ code.length > _V25_PLUGIN_CODE_PREFIX.length;
6982
+ }
6983
+
6984
+ /* ---------- 15.10 API handle registry (v2.6, hook E-4) ----------
6985
+
6986
+ sdk-call used to resolve its `path` against the global object and
6987
+ nothing else, so an API object that never lands on `window` -- a
6988
+ React ref, a viewer handle held in a closure, a Leaflet map
6989
+ instance -- was unreachable. SPEC 15.1 names exactly that case
6990
+ ("invoke excalidrawAPI.setElements(...) on a React ref") as a
6991
+ motivation, and the backend could not do it.
6992
+
6993
+ The host registers the object once under a name; manifests stay
6994
+ pure JSON and reference it by `handle`. */
6995
+
6996
+ const _v25_api_handles = Object.create(null);
6997
+
6998
+ function registerApiHandle(name, obj) {
6999
+ if (typeof name !== 'string' || !name) {
7000
+ const e = new Error('registerApiHandle: name must be a non-empty string');
7001
+ e.code = 'invalid'; throw e;
7002
+ }
7003
+ if (obj == null) {
7004
+ const e = new Error('registerApiHandle: handle "' + name + '" cannot be null or undefined');
7005
+ e.code = 'invalid'; throw e;
7006
+ }
7007
+ _v25_api_handles[name] = obj;
7008
+ }
7009
+
7010
+ function unregisterApiHandle(name) {
7011
+ if (typeof name !== 'string' || !name) return false;
7012
+ if (!Object.prototype.hasOwnProperty.call(_v25_api_handles, name)) return false;
7013
+ delete _v25_api_handles[name];
7014
+ return true;
7015
+ }
7016
+
7017
+ function apiHandles() {
7018
+ return Object.keys(_v25_api_handles).sort();
7019
+ }
7020
+
7021
+ /* Resolve the root a binding's `path` is walked against: a registered
7022
+ handle when the binding declares one, the global object otherwise.
7023
+ An undeclared handle is backend_target_missing, not a silent fall
7024
+ back to `window` -- the whole point is that the two are different
7025
+ places and a manifest naming the wrong one must say so. */
7026
+ function _v25_bindingRoot(binding) {
7027
+ if (binding.handle == null) return { root: global, label: 'window' };
7028
+ if (typeof binding.handle !== 'string' || !binding.handle) {
7029
+ const e = new Error('`handle` must be a non-empty string');
7030
+ e._code = 'invalid'; throw e;
7031
+ }
7032
+ if (!Object.prototype.hasOwnProperty.call(_v25_api_handles, binding.handle)) {
7033
+ const e = new Error('API handle "' + binding.handle + '" is not registered (NAC.registerApiHandle)');
7034
+ e._code = 'backend_target_missing'; throw e;
7035
+ }
7036
+ return { root: _v25_api_handles[binding.handle], label: 'handle:' + binding.handle };
7037
+ }
7038
+
6946
7039
  /* Counter for the request_id baked into every provenance event. */
6947
7040
  let _v25_req_counter = 0;
6948
7041
  function _v25_nextRequestId(prefix) {
@@ -7027,29 +7120,52 @@
7027
7120
  if (!path || typeof path !== 'string') {
7028
7121
  return Promise.reject(new Error('sdk-call requires `path` string'));
7029
7122
  }
7030
- const resolved = _v25_resolvePath(global, path);
7123
+ let base;
7124
+ try { base = _v25_bindingRoot(binding); }
7125
+ catch (e) { return Promise.reject(e); }
7126
+ const resolved = _v25_resolvePath(base.root, path);
7031
7127
  if (mode === 'read') {
7032
7128
  if (resolved.parent == null) {
7033
- return Promise.reject(new Error('sdk-call read: path not resolvable: ' + path));
7129
+ return Promise.reject(new Error('sdk-call read: path not resolvable: ' + base.label + '.' + path));
7034
7130
  }
7035
7131
  if (resolved.value === undefined) {
7036
- return Promise.reject(new Error('sdk-call read: path resolves to undefined: ' + path));
7132
+ return Promise.reject(new Error('sdk-call read: path resolves to undefined: ' + base.label + '.' + path));
7037
7133
  }
7038
7134
  return Promise.resolve(resolved.value);
7039
7135
  }
7040
7136
  if (mode === 'assign') {
7041
7137
  if (resolved.parent == null) {
7042
- return Promise.reject(new Error('sdk-call assign: path parent not resolvable: ' + path));
7138
+ return Promise.reject(new Error('sdk-call assign: path parent not resolvable: ' + base.label + '.' + path));
7043
7139
  }
7044
7140
  resolved.parent[resolved.key] = args;
7045
7141
  return Promise.resolve({ ok: true });
7046
7142
  }
7047
7143
  if (mode === 'invoke') {
7048
7144
  if (resolved.parent == null || typeof resolved.value !== 'function') {
7049
- return Promise.reject(new Error('sdk-call invoke: path is not a function: ' + path));
7145
+ return Promise.reject(new Error('sdk-call invoke: path is not a function: ' + base.label + '.' + path));
7146
+ }
7147
+ /* v2.6 (hook E-4): `args_order` spells out the POSITIONAL
7148
+ argument list, so a library signature like
7149
+ map.setView(center, zoom) is expressible. Without it the
7150
+ backend passes the args object as a single argument, which is
7151
+ what every published manifest does and keeps doing. */
7152
+ let list = null;
7153
+ if (binding.args_order != null) {
7154
+ if (!Array.isArray(binding.args_order)) {
7155
+ const e = new Error('sdk-call: `args_order` must be an array of arg names');
7156
+ e._code = 'invalid'; return Promise.reject(e);
7157
+ }
7158
+ const bag = (args && typeof args === 'object') ? args : {};
7159
+ list = [];
7160
+ for (let i = 0; i < binding.args_order.length; i++) {
7161
+ const k = binding.args_order[i];
7162
+ list.push(Object.prototype.hasOwnProperty.call(bag, k) ? bag[k] : undefined);
7163
+ }
7050
7164
  }
7051
7165
  try {
7052
- const ret = resolved.value.call(resolved.parent, args);
7166
+ const ret = list
7167
+ ? resolved.value.apply(resolved.parent, list)
7168
+ : resolved.value.call(resolved.parent, args);
7053
7169
  return Promise.resolve(ret);
7054
7170
  } catch (e) {
7055
7171
  return Promise.reject(e);
@@ -7292,6 +7408,7 @@
7292
7408
  whole subscription if any observed path's primary binding is
7293
7409
  not dom-mirror -- spec contract, no silent degradation. */
7294
7410
  const elements = [];
7411
+ const unresolved = [];
7295
7412
  const manifest = _v25_plugins[plugin];
7296
7413
  for (let i = 0; i < paths.length; i++) {
7297
7414
  const stateChain = manifest.state[paths[i]];
@@ -7307,9 +7424,47 @@
7307
7424
  }
7308
7425
  const el = document.querySelector(first.selector);
7309
7426
  if (el) elements.push(el);
7427
+ else unresolved.push(paths[i] + ' (' + first.selector + ')');
7428
+ }
7429
+ /* v2.6 (hook E-5). Until 2.5.2 an unresolved selector was dropped
7430
+ here with `if (el) elements.push(el)`, so a subscription whose
7431
+ mirror nodes were all absent observed NOTHING: subscribe()
7432
+ returned a valid handle and the callback never fired. Success
7433
+ and silence -- the silent damage this protocol exists to remove,
7434
+ inside the protocol.
7435
+
7436
+ Two ways out, and the manifest chooses:
7437
+ - no `root`: fail closed. An absent mirror node is
7438
+ backend_target_missing at subscribe time, named selector by
7439
+ selector.
7440
+ - `root` declared: observe that container with subtree:true
7441
+ and re-read on every mutation. A node that comes and goes
7442
+ (the row of a virtualised grid) is then observed by the
7443
+ container that outlives it, so the subscription survives
7444
+ the remount. The root itself must resolve -- otherwise we
7445
+ would be back to observing nothing. */
7446
+ let rootEl = null;
7447
+ if (binding.root != null) {
7448
+ if (typeof binding.root !== 'string' || !binding.root) {
7449
+ const e = new Error('synthetic-mutation: `root` must be a non-empty CSS selector');
7450
+ e._code = 'invalid'; throw e;
7451
+ }
7452
+ rootEl = document.querySelector(binding.root);
7453
+ if (!rootEl) {
7454
+ const e = new Error('synthetic-mutation: `root` selector matched no element: ' + binding.root +
7455
+ ' -- an observer with no root would never fire');
7456
+ e._code = 'backend_target_missing'; throw e;
7457
+ }
7458
+ }
7459
+ if (!rootEl && unresolved.length) {
7460
+ const e = new Error('synthetic-mutation: no element for observed state ' + unresolved.join(', ') +
7461
+ ' -- a subscription with nothing to observe would never fire; declare `root` to observe a' +
7462
+ ' container that outlives the node and re-resolve on mutation');
7463
+ e._code = 'backend_target_missing'; throw e;
7310
7464
  }
7311
7465
  let cached = null;
7312
7466
  let pending = null;
7467
+ let primed = false; /* has this subscription ever held a readable snapshot? */
7313
7468
  function snapshot() {
7314
7469
  const out = {};
7315
7470
  let allOk = true;
@@ -7333,29 +7488,48 @@
7333
7488
  }
7334
7489
  return false;
7335
7490
  }
7491
+ function emit(snap) {
7492
+ try { cb(snap); }
7493
+ finally {
7494
+ _v25_emitProvenance('event', plugin, event_path, 'synthetic-mutation',
7495
+ _v25_nextRequestId('pe'), true, null, 0);
7496
+ }
7497
+ }
7336
7498
  function tick() {
7337
7499
  pending = null;
7338
7500
  snapshot().then(function (snap) {
7339
7501
  if (snap == null) return;
7340
- if (cached == null) { cached = snap; return; }
7502
+ if (cached == null) {
7503
+ cached = snap;
7504
+ /* v2.6 (hook E-5): the subscription started with NOTHING
7505
+ readable -- the observed node was not painted when
7506
+ subscribe() was called, which under `root` is a legal and
7507
+ expected state. The move from unreadable to readable IS
7508
+ the change the caller is waiting for; adopting it as a
7509
+ silent baseline is how a row that remounts goes unnoticed.
7510
+ When the first snapshot DID resolve, this branch never
7511
+ runs and the baseline behaviour of 2.5 is untouched. */
7512
+ if (!primed) { primed = true; emit(snap); }
7513
+ return;
7514
+ }
7341
7515
  if (diffsFromCached(snap)) {
7342
7516
  cached = snap;
7343
- try { cb(snap); }
7344
- finally {
7345
- _v25_emitProvenance('event', plugin, event_path, 'synthetic-mutation',
7346
- _v25_nextRequestId('pe'), true, null, 0);
7347
- }
7517
+ emit(snap);
7348
7518
  }
7349
7519
  });
7350
7520
  }
7351
- /* Prime cache from first snapshot. */
7352
- snapshot().then(function (first) { if (first != null) cached = first; });
7521
+ /* Prime cache from first snapshot. A tick that got there first
7522
+ keeps its snapshot: the fresher read wins. */
7523
+ snapshot().then(function (first) {
7524
+ if (first != null && cached == null) { cached = first; primed = true; }
7525
+ });
7353
7526
  const observer = new MutationObserver(function () {
7354
7527
  if (pending != null) return; /* already throttled */
7355
7528
  pending = setTimeout(tick, throttle);
7356
7529
  });
7357
- for (let i = 0; i < elements.length; i++) {
7358
- observer.observe(elements[i], {
7530
+ const watched = rootEl ? [rootEl] : elements;
7531
+ for (let i = 0; i < watched.length; i++) {
7532
+ observer.observe(watched[i], {
7359
7533
  attributes: true, childList: true, characterData: true, subtree: true,
7360
7534
  });
7361
7535
  }
@@ -7365,6 +7539,117 @@
7365
7539
  };
7366
7540
  }
7367
7541
 
7542
+ /* emitter-listen (event, v2.6 -- hook E-3): subscribe to a real
7543
+ event source that is NOT postMessage. The emitter is the global
7544
+ object, a dotted path into it, or a registered API handle; the
7545
+ subscribe / unsubscribe method pair is detected from the three
7546
+ shapes libraries actually ship (on/off, addEventListener/
7547
+ removeEventListener, addListener/removeListener) or declared
7548
+ outright.
7549
+
7550
+ Fail closed in both directions: no subscribe method is
7551
+ backend_target_missing, and NO REMOVAL METHOD is too -- a
7552
+ subscription that cannot be torn down is a leak dressed as a
7553
+ success, and unsubscribe() is part of the published contract.
7554
+
7555
+ Payload: `extract` plucks a field out of the event object the
7556
+ library hands us. `observe` instead re-reads declared state paths
7557
+ and fires the callback with that snapshot -- the useful shape for
7558
+ a continuous surface, where the event only says "it moved" and
7559
+ the state says where it moved to. */
7560
+ const _V25_LISTEN_PAIRS = {
7561
+ 'on': 'off',
7562
+ 'addEventListener': 'removeEventListener',
7563
+ 'addListener': 'removeListener',
7564
+ };
7565
+
7566
+ function _v25_subscribe_emitter_listen(plugin, event_path, binding, cb) {
7567
+ const eventName = binding.event;
7568
+ if (!eventName || typeof eventName !== 'string') {
7569
+ const e = new Error('emitter-listen requires `event` (the library event name)');
7570
+ e._code = 'invalid'; throw e;
7571
+ }
7572
+ const base = _v25_bindingRoot(binding); /* throws with _code */
7573
+ let target = base.root;
7574
+ if (binding.path != null) {
7575
+ if (typeof binding.path !== 'string' || !binding.path) {
7576
+ const e = new Error('emitter-listen: `path` must be a non-empty dotted path');
7577
+ e._code = 'invalid'; throw e;
7578
+ }
7579
+ target = _v25_resolvePath(base.root, binding.path).value;
7580
+ }
7581
+ if (target == null) {
7582
+ const e = new Error('emitter-listen: emitter not resolvable at ' + base.label +
7583
+ (binding.path ? '.' + binding.path : ''));
7584
+ e._code = 'backend_target_missing'; throw e;
7585
+ }
7586
+ let onName = binding.method;
7587
+ if (onName != null) {
7588
+ if (typeof onName !== 'string' || typeof target[onName] !== 'function') {
7589
+ const e = new Error('emitter-listen: declared `method` "' + onName + '" is not a function on the emitter');
7590
+ e._code = 'backend_target_missing'; throw e;
7591
+ }
7592
+ } else {
7593
+ onName = null;
7594
+ for (const cand in _V25_LISTEN_PAIRS) {
7595
+ if (typeof target[cand] === 'function') { onName = cand; break; }
7596
+ }
7597
+ if (!onName) {
7598
+ const e = new Error('emitter-listen: emitter exposes none of on / addEventListener / addListener');
7599
+ e._code = 'backend_target_missing'; throw e;
7600
+ }
7601
+ }
7602
+ const offName = binding.off_method || _V25_LISTEN_PAIRS[onName] || null;
7603
+ if (!offName || typeof target[offName] !== 'function') {
7604
+ const e = new Error('emitter-listen: emitter has no removal method (' +
7605
+ (offName || 'none derivable from "' + onName + '"') +
7606
+ ') -- a subscription that cannot be torn down is refused');
7607
+ e._code = 'backend_target_missing'; throw e;
7608
+ }
7609
+ const observed = Array.isArray(binding.observe) ? binding.observe.slice() : null;
7610
+ if (observed) {
7611
+ for (let i = 0; i < observed.length; i++) {
7612
+ if (!_v25_plugins[plugin].state[observed[i]]) {
7613
+ const e = new Error('emitter-listen: observed state "' + observed[i] + '" not declared');
7614
+ e._code = 'invalid'; throw e;
7615
+ }
7616
+ }
7617
+ }
7618
+ function fire(value, ok, errMsg) {
7619
+ try { if (ok) cb(value); }
7620
+ finally {
7621
+ _v25_emitProvenance('event', plugin, event_path, 'emitter-listen',
7622
+ _v25_nextRequestId('pe'), !!ok, errMsg || null, 0);
7623
+ }
7624
+ }
7625
+ function handler(payload) {
7626
+ if (observed) {
7627
+ const out = {};
7628
+ let allOk = true;
7629
+ Promise.all(observed.map(function (p) {
7630
+ return read(plugin, p).then(function (v) { out[p] = v; },
7631
+ function () { allOk = false; });
7632
+ })).then(function () {
7633
+ if (allOk) fire(out, true, null);
7634
+ else fire(null, false, 'observed state unreadable at event time');
7635
+ });
7636
+ return;
7637
+ }
7638
+ let value;
7639
+ try {
7640
+ value = binding.extract ? _v25_extract(payload, binding.extract, false) : payload;
7641
+ } catch (e) {
7642
+ fire(null, false, e.message);
7643
+ return;
7644
+ }
7645
+ fire(value, true, null);
7646
+ }
7647
+ target[onName](eventName, handler);
7648
+ return function teardown() {
7649
+ try { target[offName](eventName, handler); } catch (_) { /* idempotent */ }
7650
+ };
7651
+ }
7652
+
7368
7653
  /* postmessage-listen (event): subscribe to incoming `message` events
7369
7654
  on the host window, filter by source iframe + MessageId, run the
7370
7655
  extractor, fire the user callback. Returns a teardown closure. */
@@ -7416,10 +7701,90 @@
7416
7701
  'postmessage-listen': _v25_subscribe_postmessage_listen,
7417
7702
  'synthetic-poll': _v25_subscribe_synthetic_poll,
7418
7703
  'synthetic-mutation': _v25_subscribe_synthetic_mutation,
7704
+ 'emitter-listen': _v25_subscribe_emitter_listen,
7419
7705
  };
7420
7706
 
7421
7707
  /* ---------- 15.2 / 15.3 public surface ---------- */
7422
7708
 
7709
+ /* ---------- 15.11 self-description (v2.6, hook E-2) ----------
7710
+
7711
+ A manifest used to declare only WHERE a verb routes, never what it
7712
+ is called or what it takes. The DOM half of the protocol has had
7713
+ that since v1 (a label, a hint, `fields[]` with a type); the
7714
+ plugin half had nothing, so on a continuous surface -- where the
7715
+ actionable thing IS the argument: a coordinate and a zoom, an
7716
+ instant, a page, a pose -- nothing declared the shape, the unit or
7717
+ the range of what travels in `args`, and a voice runner could not
7718
+ even name the verb out loud.
7719
+
7720
+ All of it is OPTIONAL: every manifest published against 2.5 stays
7721
+ valid. What is NOT optional is being well-formed when declared --
7722
+ a label that is not a string, or a param of a type the vocabulary
7723
+ does not have, is `invalid` at register time rather than a
7724
+ surprise at the moment an agent reads it. */
7725
+
7726
+ const _V25_PARAM_TYPES = {
7727
+ 'string': true, 'number': true, 'boolean': true, 'object': true, 'array': true,
7728
+ };
7729
+
7730
+ function _v25_validateText(value, where, field) {
7731
+ if (value == null) return;
7732
+ if (typeof value !== 'string' || !value.trim()) {
7733
+ const e = new Error(where + ': `' + field + '` must be a non-empty string when declared');
7734
+ e.code = 'invalid'; throw e;
7735
+ }
7736
+ }
7737
+
7738
+ function _v25_validateParams(params, where) {
7739
+ if (params == null) return;
7740
+ if (!Array.isArray(params)) {
7741
+ const e = new Error(where + ': `params` must be an array of parameter declarations');
7742
+ e.code = 'invalid'; throw e;
7743
+ }
7744
+ const seen = Object.create(null);
7745
+ for (let i = 0; i < params.length; i++) {
7746
+ const p = params[i];
7747
+ const at = where + ' params[' + i + ']';
7748
+ if (!p || typeof p !== 'object' || Array.isArray(p)) {
7749
+ const e = new Error(at + ': must be an object');
7750
+ e.code = 'invalid'; throw e;
7751
+ }
7752
+ if (typeof p.name !== 'string' || !p.name.trim()) {
7753
+ const e = new Error(at + ': `name` is required and must be a non-empty string');
7754
+ e.code = 'invalid'; throw e;
7755
+ }
7756
+ if (seen[p.name]) {
7757
+ const e = new Error(at + ': duplicate parameter name "' + p.name + '"');
7758
+ e.code = 'invalid'; throw e;
7759
+ }
7760
+ seen[p.name] = true;
7761
+ if (typeof p.type !== 'string' || !_V25_PARAM_TYPES[p.type]) {
7762
+ const e = new Error(at + ': `type` must be one of string, number, boolean, object, array (got ' +
7763
+ JSON.stringify(p.type) + ')');
7764
+ e.code = 'invalid'; throw e;
7765
+ }
7766
+ if (p.required != null && typeof p.required !== 'boolean') {
7767
+ const e = new Error(at + ': `required` must be a boolean when declared');
7768
+ e.code = 'invalid'; throw e;
7769
+ }
7770
+ _v25_validateText(p.label, at, 'label');
7771
+ _v25_validateText(p.hint, at, 'hint');
7772
+ _v25_validateText(p.unit, at, 'unit');
7773
+ if (p.min != null && typeof p.min !== 'number') {
7774
+ const e = new Error(at + ': `min` must be a number when declared');
7775
+ e.code = 'invalid'; throw e;
7776
+ }
7777
+ if (p.max != null && typeof p.max !== 'number') {
7778
+ const e = new Error(at + ': `max` must be a number when declared');
7779
+ e.code = 'invalid'; throw e;
7780
+ }
7781
+ if (p.enum != null && (!Array.isArray(p.enum) || p.enum.length === 0)) {
7782
+ const e = new Error(at + ': `enum` must be a non-empty array when declared');
7783
+ e.code = 'invalid'; throw e;
7784
+ }
7785
+ }
7786
+ }
7787
+
7423
7788
  function _v25_validateManifest(slug, manifest) {
7424
7789
  if (typeof slug !== 'string' || !slug) {
7425
7790
  const e = new Error('registerPlugin: slug must be non-empty string');
@@ -7432,6 +7797,8 @@
7432
7797
  const verbs = manifest.verbs || {};
7433
7798
  const states = manifest.state || {};
7434
7799
  const events = manifest.events || {};
7800
+ _v25_validateText(manifest.label, 'manifest "' + slug + '"', 'label');
7801
+ _v25_validateText(manifest.hint, 'manifest "' + slug + '"', 'hint');
7435
7802
  /* Verbs: single binding each, backend in vocabulary. */
7436
7803
  for (const vp in verbs) {
7437
7804
  const b = verbs[vp];
@@ -7443,6 +7810,9 @@
7443
7810
  const e = new Error('verb "' + vp + '" declares unknown backend: ' + b.backend);
7444
7811
  e.code = 'backend_unknown'; throw e;
7445
7812
  }
7813
+ _v25_validateText(b.label, 'verb "' + vp + '"', 'label');
7814
+ _v25_validateText(b.hint, 'verb "' + vp + '"', 'hint');
7815
+ _v25_validateParams(b.params, 'verb "' + vp + '"');
7446
7816
  }
7447
7817
  /* State: array of bindings each. */
7448
7818
  for (const sp in states) {
@@ -7461,6 +7831,9 @@
7461
7831
  const e = new Error('state "' + sp + '" binding[' + i + '] unknown backend: ' + b.backend);
7462
7832
  e.code = 'backend_unknown'; throw e;
7463
7833
  }
7834
+ _v25_validateText(b.label, 'state "' + sp + '" binding[' + i + ']', 'label');
7835
+ _v25_validateText(b.hint, 'state "' + sp + '" binding[' + i + ']', 'hint');
7836
+ _v25_validateText(b.unit, 'state "' + sp + '" binding[' + i + ']', 'unit');
7464
7837
  }
7465
7838
  }
7466
7839
  /* Events: single binding each. */
@@ -7474,6 +7847,16 @@
7474
7847
  const e = new Error('event "' + ep + '" unknown backend: ' + b.backend);
7475
7848
  e.code = 'backend_unknown'; throw e;
7476
7849
  }
7850
+ _v25_validateText(b.label, 'event "' + ep + '"', 'label');
7851
+ _v25_validateText(b.hint, 'event "' + ep + '"', 'hint');
7852
+ /* emitter-listen carries the one field the runtime cannot guess
7853
+ (v2.6): which event of the library to listen for. Caught here
7854
+ instead of at subscribe time so a manifest that can never work
7855
+ is refused when it is registered. */
7856
+ if (b.backend === 'emitter-listen' && (typeof b.event !== 'string' || !b.event)) {
7857
+ const e = new Error('event "' + ep + '": emitter-listen requires `event` (the library event name)');
7858
+ e.code = 'invalid'; throw e;
7859
+ }
7477
7860
  }
7478
7861
  }
7479
7862
 
@@ -7482,12 +7865,121 @@
7482
7865
  /* Re-register replaces -- idempotent for live-reload scenarios. */
7483
7866
  _v25_plugins[slug] = {
7484
7867
  slug: slug,
7868
+ label: manifest.label != null ? manifest.label : null,
7869
+ hint: manifest.hint != null ? manifest.hint : null,
7485
7870
  verbs: manifest.verbs || {},
7486
7871
  state: manifest.state || {},
7487
7872
  events: manifest.events || {},
7488
7873
  };
7489
7874
  }
7490
7875
 
7876
+ /* ---------- 15.10 discovery (v2.6, hook E-1) ----------
7877
+
7878
+ Until 2.5.2 the registry was readable by exactly three functions
7879
+ -- call, read and subscribe -- and all three require the caller to
7880
+ already know the slug and the path. An agent that did not register
7881
+ the plugin itself had no way to learn it exists: it read the DOM
7882
+ manifest (section 5), found the chrome, and the canvas surface was
7883
+ INVISIBLE. For a plugin that declares no DOM at all there was
7884
+ nothing to find whatsoever.
7885
+
7886
+ plugins() and describePlugin() are the read-only answer. They are
7887
+ SYNCHRONOUS on purpose -- unlike call / read / subscribe, which
7888
+ reach a surface and therefore have to be async, these only read a
7889
+ local object. And they hand back a COPY: a caller cannot reach the
7890
+ live registry through the descriptor and rewrite another plugin's
7891
+ routing. */
7892
+
7893
+ function _v25_copy(value) {
7894
+ if (value == null || typeof value !== 'object') return value;
7895
+ try { return JSON.parse(JSON.stringify(value)); }
7896
+ catch (_) { return undefined; } /* not representable as data */
7897
+ }
7898
+
7899
+ function _v25_describeEntry(entry) {
7900
+ const out = {
7901
+ slug: entry.slug,
7902
+ label: entry.label != null ? entry.label : null,
7903
+ hint: entry.hint != null ? entry.hint : null,
7904
+ verbs: [],
7905
+ state: [],
7906
+ events: [],
7907
+ };
7908
+ const verbPaths = Object.keys(entry.verbs).sort();
7909
+ for (let i = 0; i < verbPaths.length; i++) {
7910
+ const vp = verbPaths[i];
7911
+ const b = entry.verbs[vp] || {};
7912
+ const row = {
7913
+ verb_path: vp,
7914
+ backend: b.backend || null,
7915
+ label: b.label != null ? b.label : null,
7916
+ hint: b.hint != null ? b.hint : null,
7917
+ params: Array.isArray(b.params) ? _v25_copy(b.params) : null,
7918
+ };
7919
+ if (b.handle != null) row.handle = b.handle;
7920
+ out.verbs.push(row);
7921
+ }
7922
+ const statePaths = Object.keys(entry.state).sort();
7923
+ for (let i = 0; i < statePaths.length; i++) {
7924
+ const sp = statePaths[i];
7925
+ const chain = entry.state[sp] || [];
7926
+ /* The label of a state PATH is the first one its chain declares:
7927
+ the chain is an ordered preference of sources for one value,
7928
+ so the value is named once, by whichever source names it. */
7929
+ let label = null; let hint = null; let unit = null;
7930
+ const backends = [];
7931
+ for (let j = 0; j < chain.length; j++) {
7932
+ const b = chain[j] || {};
7933
+ if (label == null && b.label != null) label = b.label;
7934
+ if (hint == null && b.hint != null) hint = b.hint;
7935
+ if (unit == null && b.unit != null) unit = b.unit;
7936
+ backends.push(b.backend || null);
7937
+ }
7938
+ out.state.push({
7939
+ state_path: sp,
7940
+ backends: backends,
7941
+ label: label,
7942
+ hint: hint,
7943
+ unit: unit,
7944
+ });
7945
+ }
7946
+ const eventPaths = Object.keys(entry.events).sort();
7947
+ for (let i = 0; i < eventPaths.length; i++) {
7948
+ const ep = eventPaths[i];
7949
+ const b = entry.events[ep] || {};
7950
+ const row = {
7951
+ event_path: ep,
7952
+ backend: b.backend || null,
7953
+ label: b.label != null ? b.label : null,
7954
+ hint: b.hint != null ? b.hint : null,
7955
+ observe: Array.isArray(b.observe) ? b.observe.slice() : null,
7956
+ };
7957
+ if (b.event != null) row.event = b.event;
7958
+ out.events.push(row);
7959
+ }
7960
+ return out;
7961
+ }
7962
+
7963
+ function plugins() {
7964
+ const slugs = Object.keys(_v25_plugins).sort();
7965
+ const out = [];
7966
+ for (let i = 0; i < slugs.length; i++) {
7967
+ out.push(_v25_describeEntry(_v25_plugins[slugs[i]]));
7968
+ }
7969
+ return out;
7970
+ }
7971
+
7972
+ function describePlugin(slug) {
7973
+ if (typeof slug !== 'string' || !slug) {
7974
+ throw _v25_buildNacError('invalid', 'NAC.describePlugin: slug must be a non-empty string');
7975
+ }
7976
+ if (!_v25_plugins[slug]) {
7977
+ throw _v25_buildNacError('plugin_not_registered',
7978
+ 'NAC.describePlugin: plugin "' + slug + '" not registered');
7979
+ }
7980
+ return _v25_describeEntry(_v25_plugins[slug]);
7981
+ }
7982
+
7491
7983
  /* Provenance emit helper -- the kind is one of 'invoked', 'read',
7492
7984
  'event'. Detail shape per SPEC.md sec 15.7. */
7493
7985
  function _v25_emitProvenance(kind, plugin, path, backend, request_id, ok, error, duration_ms, extra) {
@@ -7556,7 +8048,19 @@
7556
8048
  const msg = (err && err.message) || String(err);
7557
8049
  _v25_emitProvenance('invoked', plugin, verb_path, binding.backend, request_id, false, msg, Date.now() - t0);
7558
8050
  if (err && err._code) {
7559
- throw _v25_buildNacError(err._code, msg);
8051
+ /* v2.6 (hook E-6): the code has to be one the contract
8052
+ declares -- one of the ten of 15.9, or a plugin-owned code
8053
+ under the `plugin.` prefix. Anything else is `invalid` and
8054
+ names what it refused, instead of inventing a code the
8055
+ published list does not have. */
8056
+ if (_v25_isDeclaredCode(err._code)) {
8057
+ throw _v25_buildNacError(err._code, msg);
8058
+ }
8059
+ throw _v25_buildNacError('invalid',
8060
+ 'NAC.call: backend raised the undeclared error code "' + err._code +
8061
+ '" (SPEC 15.9: one of the ten contract codes, or a plugin code under the "' +
8062
+ _V25_PLUGIN_CODE_PREFIX + '" prefix): ' + msg,
8063
+ { offending_code: err._code });
7560
8064
  }
7561
8065
  throw err;
7562
8066
  });
@@ -7653,7 +8157,12 @@
7653
8157
  let teardown;
7654
8158
  try { teardown = subscriber(plugin, event_path, binding, cb); }
7655
8159
  catch (e) {
7656
- if (e && e._code) throw _v25_buildNacError(e._code, e.message);
8160
+ if (e && e._code) {
8161
+ if (_v25_isDeclaredCode(e._code)) throw _v25_buildNacError(e._code, e.message);
8162
+ throw _v25_buildNacError('invalid',
8163
+ 'NAC.subscribe: backend raised the undeclared error code "' + e._code +
8164
+ '" (SPEC 15.9): ' + e.message, { offending_code: e._code });
8165
+ }
7657
8166
  throw e;
7658
8167
  }
7659
8168
  _v25_sub_counter += 1;
@@ -7921,8 +8430,8 @@
7921
8430
 
7922
8431
  global.NAC = {
7923
8432
  __nac_v1_installed: true,
7924
- version: '2.5.1',
7925
- spec_version: '2.5',
8433
+ version: '2.6.0',
8434
+ spec_version: '2.6',
7926
8435
  /* v2.2.0 (V22-01) -- strict validation toggle. When true,
7927
8436
  NAC.register throws on findings (manifest_role_unknown,
7928
8437
  tab_id_manifest_role_drift, manifest_dom_role_mismatch).
@@ -8172,6 +8681,17 @@
8172
8681
  read: read,
8173
8682
  subscribe: subscribe,
8174
8683
  unsubscribe: unsubscribe,
8684
+ /* v2.6 -- the six hooks of RFC V26-01 (SPEC.md sec 15.10 / 15.11).
8685
+ plugins() + describePlugin() make the registry discoverable, so
8686
+ a plugin living in another repository under another licence can
8687
+ be FOUND by an agent that did not register it; registerApiHandle
8688
+ lets a manifest route to an API object that never lands on
8689
+ window (a React ref, a library instance held in a closure). */
8690
+ plugins: plugins,
8691
+ describePlugin: describePlugin,
8692
+ registerApiHandle: registerApiHandle,
8693
+ unregisterApiHandle: unregisterApiHandle,
8694
+ apiHandles: apiHandles,
8175
8695
  /* v2.5 Block C -- assistive labels (SPEC.md sec 17 / RFC V25-03).
8176
8696
  Canonical human-readable label resolution for screen readers,
8177
8697
  TTS, voice flows, audit logs. Priority order: aria-label >