@camstack/system 1.2.131 → 1.2.133

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.
@@ -1,7 +1,7 @@
1
- import { ServiceBroker } from 'moleculer';
2
1
  import { TRPCLink } from '@trpc/client';
3
2
  import { AnyRouter } from '@trpc/server';
4
- import { LocalChildRegistry, CapCallInput } from '../transport/index.js';
3
+ import { ServiceBroker } from 'moleculer';
4
+ import { CapCallInput, LocalChildRegistry } from '../transport/index.js';
5
5
  /**
6
6
  * Resolver for in-process capability providers.
7
7
  *
@@ -51,11 +51,39 @@ export type CapUsageObserver = (obs: CapUsageObservation) => void;
51
51
  export interface CapUsageObserverOptions {
52
52
  /** The addon whose `ctx.api` produced this link chain. */
53
53
  readonly callerAddonId: string;
54
- /** Resolve which addon hosts the provider for `capName`. Return `null` to skip recording. */
54
+ /**
55
+ * Resolve which addon hosts the provider for `capName`. `null` means "not in
56
+ * the LOCAL registry" — which is every cross-process provider, and no longer
57
+ * means "do not record": see {@link UNRESOLVED_PROVIDER_ADDON_ID}.
58
+ */
55
59
  readonly providerAddonIdForCap: (capName: string) => string | null;
56
60
  /** Sink for observations. MUST NOT throw — wrapper catches anyway. */
57
61
  readonly observer: CapUsageObserver;
58
62
  }
63
+ /**
64
+ * Stands in for a provider the LOCAL registry cannot name.
65
+ *
66
+ * `providerAddonIdForCap` only searches the local registry, so it returns
67
+ * `null` for every cross-process provider — which is every forked runner,
68
+ * `sqlite-settings` included. That `null` used to drop the whole observation,
69
+ * caller and all. The result: `nodes.getCapUsageGraph`, an admin API whose
70
+ * entire job is to show caller → provider → cap traffic, returned an EMPTY
71
+ * ARRAY on the live hub while the engine logged 39 717 calls a minute to a
72
+ * single collection. An empty graph reads as "nothing is calling anything".
73
+ *
74
+ * The caller is always known — it is whoever made the call — and "who is
75
+ * hammering this cap" is the question the graph exists to answer. So the
76
+ * observation is recorded and the unknown half stays VISIBLY unknown, which is
77
+ * a fact an operator can act on, rather than absent, which is a fact nobody can
78
+ * see. Naming the provider properly needs the cluster-wide cap list; until then
79
+ * this placeholder is the honest value.
80
+ */
81
+ export declare const UNRESOLVED_PROVIDER_ADDON_ID = "(unresolved: cross-process)";
82
+ /**
83
+ * Record one cap call, if an observer is configured. Extracted so the rule —
84
+ * an unresolved provider never erases the caller — has ONE place and a test.
85
+ */
86
+ export declare function recordCapUsage(observerOpts: CapUsageObserverOptions, capName: string, methodName: string): void;
59
87
  /**
60
88
  * Non-terminating tRPC link that intercepts calls when the target
61
89
  * capability has a provider registered in the local process.
@@ -6089,8 +6089,21 @@ function createHubCapForwardService(onUnownedCall) {
6089
6089
  } } }
6090
6090
  };
6091
6091
  }
6092
- //#endregion
6093
- //#region src/kernel/moleculer/trpc-links.ts
6092
+ /**
6093
+ * Record one cap call, if an observer is configured. Extracted so the rule —
6094
+ * an unresolved provider never erases the caller — has ONE place and a test.
6095
+ */
6096
+ function recordCapUsage(observerOpts, capName, methodName) {
6097
+ try {
6098
+ observerOpts.observer({
6099
+ callerAddonId: observerOpts.callerAddonId,
6100
+ providerAddonId: observerOpts.providerAddonIdForCap(capName) ?? "(unresolved: cross-process)",
6101
+ capName,
6102
+ methodName,
6103
+ atMs: Date.now()
6104
+ });
6105
+ } catch {}
6106
+ }
6094
6107
  /** Convert camelCase → kebab-case. */
6095
6108
  function toKebab(name) {
6096
6109
  return name.replace(/[A-Z]/g, (m, i) => (i > 0 ? "-" : "") + m.toLowerCase());
@@ -6140,16 +6153,7 @@ function localProviderLink(resolver, observerOpts) {
6140
6153
  if (!provider || typeof provider !== "object") return next(opWithDefaults);
6141
6154
  const fn = Reflect.get(provider, parsed.method);
6142
6155
  if (typeof fn !== "function") return next(opWithDefaults);
6143
- if (observerOpts) try {
6144
- const providerAddonId = observerOpts.providerAddonIdForCap(parsed.capName);
6145
- if (providerAddonId) observerOpts.observer({
6146
- callerAddonId: observerOpts.callerAddonId,
6147
- providerAddonId,
6148
- capName: parsed.capName,
6149
- methodName: parsed.method,
6150
- atMs: Date.now()
6151
- });
6152
- } catch {}
6156
+ if (observerOpts) recordCapUsage(observerOpts, parsed.capName, parsed.method);
6153
6157
  return observable((observer) => {
6154
6158
  Promise.resolve(fn.call(provider, opWithDefaults.input)).then((data) => {
6155
6159
  observer.next({ result: {
@@ -6410,9 +6414,9 @@ function brokerTransportLink(broker, serviceMap, observerOpts, linkOptions) {
6410
6414
  }
6411
6415
  if (observerOpts) try {
6412
6416
  const providerAddonId = serviceName !== parsed.capName ? serviceName : observerOpts.providerAddonIdForCap(parsed.capName);
6413
- if (providerAddonId) observerOpts.observer({
6417
+ observerOpts.observer({
6414
6418
  callerAddonId: observerOpts.callerAddonId,
6415
- providerAddonId,
6419
+ providerAddonId: providerAddonId ?? "(unresolved: cross-process)",
6416
6420
  capName: parsed.capName,
6417
6421
  methodName: parsed.method,
6418
6422
  atMs: Date.now()
@@ -6093,8 +6093,21 @@ function createHubCapForwardService(onUnownedCall) {
6093
6093
  } } }
6094
6094
  };
6095
6095
  }
6096
- //#endregion
6097
- //#region src/kernel/moleculer/trpc-links.ts
6096
+ /**
6097
+ * Record one cap call, if an observer is configured. Extracted so the rule —
6098
+ * an unresolved provider never erases the caller — has ONE place and a test.
6099
+ */
6100
+ function recordCapUsage(observerOpts, capName, methodName) {
6101
+ try {
6102
+ observerOpts.observer({
6103
+ callerAddonId: observerOpts.callerAddonId,
6104
+ providerAddonId: observerOpts.providerAddonIdForCap(capName) ?? "(unresolved: cross-process)",
6105
+ capName,
6106
+ methodName,
6107
+ atMs: Date.now()
6108
+ });
6109
+ } catch {}
6110
+ }
6098
6111
  /** Convert camelCase → kebab-case. */
6099
6112
  function toKebab(name) {
6100
6113
  return name.replace(/[A-Z]/g, (m, i) => (i > 0 ? "-" : "") + m.toLowerCase());
@@ -6144,16 +6157,7 @@ function localProviderLink(resolver, observerOpts) {
6144
6157
  if (!provider || typeof provider !== "object") return next(opWithDefaults);
6145
6158
  const fn = Reflect.get(provider, parsed.method);
6146
6159
  if (typeof fn !== "function") return next(opWithDefaults);
6147
- if (observerOpts) try {
6148
- const providerAddonId = observerOpts.providerAddonIdForCap(parsed.capName);
6149
- if (providerAddonId) observerOpts.observer({
6150
- callerAddonId: observerOpts.callerAddonId,
6151
- providerAddonId,
6152
- capName: parsed.capName,
6153
- methodName: parsed.method,
6154
- atMs: Date.now()
6155
- });
6156
- } catch {}
6160
+ if (observerOpts) recordCapUsage(observerOpts, parsed.capName, parsed.method);
6157
6161
  return observable((observer) => {
6158
6162
  Promise.resolve(fn.call(provider, opWithDefaults.input)).then((data) => {
6159
6163
  observer.next({ result: {
@@ -6414,9 +6418,9 @@ function brokerTransportLink(broker, serviceMap, observerOpts, linkOptions) {
6414
6418
  }
6415
6419
  if (observerOpts) try {
6416
6420
  const providerAddonId = serviceName !== parsed.capName ? serviceName : observerOpts.providerAddonIdForCap(parsed.capName);
6417
- if (providerAddonId) observerOpts.observer({
6421
+ observerOpts.observer({
6418
6422
  callerAddonId: observerOpts.callerAddonId,
6419
- providerAddonId,
6423
+ providerAddonId: providerAddonId ?? "(unresolved: cross-process)",
6420
6424
  capName: parsed.capName,
6421
6425
  methodName: parsed.method,
6422
6426
  atMs: Date.now()
@@ -8,6 +8,139 @@ import { tmpdir } from "node:os";
8
8
  import { existsSync, mkdirSync, readFileSync, renameSync, writeFileSync } from "node:fs";
9
9
  import { createServer } from "node:http";
10
10
  import { isIP } from "node:net";
11
+ //#region src/http/lan-http-bind.ts
12
+ /**
13
+ * Second HTTP listener that shares the hub's existing request handler.
14
+ *
15
+ * Fastify is one protocol per instance; HTTPS stays on :4443 and this
16
+ * binds a plain `http.Server` that emits `request` AND `upgrade` onto the
17
+ * same handler graph (tRPC HTTP, cookies, data-plane, static, tRPC WS).
18
+ *
19
+ * Forwarding only `request` is not enough: the viewer speaks tRPC exclusively
20
+ * over WebSocket (`wsLink`). `/health` wins the LAN race, then `ws://…/trpc`
21
+ * hangs because nobody handles the upgrade on this socket.
22
+ */
23
+ var DEFAULT_HTTP_PORT = 4480;
24
+ var handlers = null;
25
+ var server = null;
26
+ var pending = null;
27
+ var state = {
28
+ listening: false,
29
+ error: null,
30
+ port: DEFAULT_HTTP_PORT
31
+ };
32
+ var boundRequestedHost = null;
33
+ var DEFAULT_LAN_HTTP_PORT = DEFAULT_HTTP_PORT;
34
+ /** Map "all IPv4" to dual-stack IPv6 (`::`, ipv6Only false) so IPv4 still works. */
35
+ function allFamiliesListenHost(host) {
36
+ if (host === "0.0.0.0" || host === "*" || host === "") return {
37
+ host: "::",
38
+ ipv6Only: false
39
+ };
40
+ return {
41
+ host,
42
+ ipv6Only: false
43
+ };
44
+ }
45
+ function registerLanHttpHandler(next) {
46
+ handlers = next;
47
+ }
48
+ function readLanHttpState() {
49
+ return state;
50
+ }
51
+ async function applyLanHttp(options) {
52
+ pending = options;
53
+ if (!options.enabled) {
54
+ await closeLanHttp();
55
+ state = {
56
+ listening: false,
57
+ error: null,
58
+ port: options.port
59
+ };
60
+ return state;
61
+ }
62
+ if (handlers === null) {
63
+ state = {
64
+ listening: false,
65
+ error: "HTTP listener is not registered yet",
66
+ port: options.port
67
+ };
68
+ return state;
69
+ }
70
+ if (server !== null && state.listening && (options.port === 0 || state.port === options.port) && boundRequestedHost === options.host) return state;
71
+ await closeLanHttp();
72
+ try {
73
+ const bound = await listenHttp(options.port, options.host, handlers);
74
+ server = bound;
75
+ boundRequestedHost = options.host;
76
+ const addr = bound.address();
77
+ state = {
78
+ listening: true,
79
+ error: null,
80
+ port: typeof addr === "object" && addr !== null ? addr.port : options.port
81
+ };
82
+ return state;
83
+ } catch (err) {
84
+ server = null;
85
+ state = {
86
+ listening: false,
87
+ error: err instanceof Error ? err.message : String(err),
88
+ port: options.port
89
+ };
90
+ return state;
91
+ }
92
+ }
93
+ /** After the hub registers the Fastify request handler, bind whatever was pending. */
94
+ async function bindPendingLanHttp() {
95
+ if (pending === null) return applyLanHttp({
96
+ enabled: true,
97
+ port: DEFAULT_HTTP_PORT,
98
+ host: "0.0.0.0"
99
+ });
100
+ return applyLanHttp(pending);
101
+ }
102
+ async function closeLanHttp() {
103
+ const current = server;
104
+ server = null;
105
+ boundRequestedHost = null;
106
+ if (current === null) return;
107
+ await new Promise((resolve) => {
108
+ current.close(() => resolve());
109
+ });
110
+ }
111
+ function listenHttp(port, host, next) {
112
+ const resolved = allFamiliesListenHost(host);
113
+ return listenOn(port, resolved, next).catch((err) => {
114
+ if (resolved.host === "::" && host !== "::") return listenOn(port, {
115
+ host: "0.0.0.0",
116
+ ipv6Only: false
117
+ }, next);
118
+ throw err;
119
+ });
120
+ }
121
+ function listenOn(port, bind, next) {
122
+ return new Promise((resolve, reject) => {
123
+ const created = createServer(next.onRequest);
124
+ created.on("upgrade", next.onUpgrade);
125
+ const onError = (err) => {
126
+ created.off("listening", onListening);
127
+ created.close();
128
+ reject(err);
129
+ };
130
+ const onListening = () => {
131
+ created.off("error", onError);
132
+ resolve(created);
133
+ };
134
+ created.once("error", onError);
135
+ created.once("listening", onListening);
136
+ created.listen({
137
+ port,
138
+ host: bind.host,
139
+ ipv6Only: bind.ipv6Only
140
+ });
141
+ });
142
+ }
143
+ //#endregion
11
144
  //#region src/tls/cert-evaluation.ts
12
145
  /**
13
146
  * The regeneration rule (D227), as a pure function.
@@ -616,137 +749,4 @@ function readTlsAccessStatus(dataDir, restartRequired = false) {
616
749
  }
617
750
  }
618
751
  //#endregion
619
- //#region src/http/lan-http-bind.ts
620
- /**
621
- * Second HTTP listener that shares the hub's existing request handler.
622
- *
623
- * Fastify is one protocol per instance; HTTPS stays on :4443 and this
624
- * binds a plain `http.Server` that emits `request` AND `upgrade` onto the
625
- * same handler graph (tRPC HTTP, cookies, data-plane, static, tRPC WS).
626
- *
627
- * Forwarding only `request` is not enough: the viewer speaks tRPC exclusively
628
- * over WebSocket (`wsLink`). `/health` wins the LAN race, then `ws://…/trpc`
629
- * hangs because nobody handles the upgrade on this socket.
630
- */
631
- var DEFAULT_HTTP_PORT = 4480;
632
- var handlers = null;
633
- var server = null;
634
- var pending = null;
635
- var state = {
636
- listening: false,
637
- error: null,
638
- port: DEFAULT_HTTP_PORT
639
- };
640
- var boundRequestedHost = null;
641
- var DEFAULT_LAN_HTTP_PORT = DEFAULT_HTTP_PORT;
642
- /** Map "all IPv4" to dual-stack IPv6 (`::`, ipv6Only false) so IPv4 still works. */
643
- function allFamiliesListenHost(host) {
644
- if (host === "0.0.0.0" || host === "*" || host === "") return {
645
- host: "::",
646
- ipv6Only: false
647
- };
648
- return {
649
- host,
650
- ipv6Only: false
651
- };
652
- }
653
- function registerLanHttpHandler(next) {
654
- handlers = next;
655
- }
656
- function readLanHttpState() {
657
- return state;
658
- }
659
- async function applyLanHttp(options) {
660
- pending = options;
661
- if (!options.enabled) {
662
- await closeLanHttp();
663
- state = {
664
- listening: false,
665
- error: null,
666
- port: options.port
667
- };
668
- return state;
669
- }
670
- if (handlers === null) {
671
- state = {
672
- listening: false,
673
- error: "HTTP listener is not registered yet",
674
- port: options.port
675
- };
676
- return state;
677
- }
678
- if (server !== null && state.listening && (options.port === 0 || state.port === options.port) && boundRequestedHost === options.host) return state;
679
- await closeLanHttp();
680
- try {
681
- const bound = await listenHttp(options.port, options.host, handlers);
682
- server = bound;
683
- boundRequestedHost = options.host;
684
- const addr = bound.address();
685
- state = {
686
- listening: true,
687
- error: null,
688
- port: typeof addr === "object" && addr !== null ? addr.port : options.port
689
- };
690
- return state;
691
- } catch (err) {
692
- server = null;
693
- state = {
694
- listening: false,
695
- error: err instanceof Error ? err.message : String(err),
696
- port: options.port
697
- };
698
- return state;
699
- }
700
- }
701
- /** After the hub registers the Fastify request handler, bind whatever was pending. */
702
- async function bindPendingLanHttp() {
703
- if (pending === null) return applyLanHttp({
704
- enabled: true,
705
- port: DEFAULT_HTTP_PORT,
706
- host: "0.0.0.0"
707
- });
708
- return applyLanHttp(pending);
709
- }
710
- async function closeLanHttp() {
711
- const current = server;
712
- server = null;
713
- boundRequestedHost = null;
714
- if (current === null) return;
715
- await new Promise((resolve) => {
716
- current.close(() => resolve());
717
- });
718
- }
719
- function listenHttp(port, host, next) {
720
- const resolved = allFamiliesListenHost(host);
721
- return listenOn(port, resolved, next).catch((err) => {
722
- if (resolved.host === "::" && host !== "::") return listenOn(port, {
723
- host: "0.0.0.0",
724
- ipv6Only: false
725
- }, next);
726
- throw err;
727
- });
728
- }
729
- function listenOn(port, bind, next) {
730
- return new Promise((resolve, reject) => {
731
- const created = createServer(next.onRequest);
732
- created.on("upgrade", next.onUpgrade);
733
- const onError = (err) => {
734
- created.off("listening", onListening);
735
- created.close();
736
- reject(err);
737
- };
738
- const onListening = () => {
739
- created.off("error", onError);
740
- resolve(created);
741
- };
742
- created.once("error", onError);
743
- created.once("listening", onListening);
744
- created.listen({
745
- port,
746
- host: bind.host,
747
- ipv6Only: bind.ipv6Only
748
- });
749
- });
750
- }
751
- //#endregion
752
- export { evaluateExistingCert as C, SERVER_AUTH_OID as S, CA_COMMON_NAME as _, closeLanHttp as a, LEAF_RENEWAL_WINDOW_DAYS as b, readExtraSans as c, writeExtraSans as d, writeTlsMode as f, reissueTlsLeaf as g, loadTlsCert as h, bindPendingLanHttp as i, readTlsAccessStatus as l, ensureTlsCert as m, allFamiliesListenHost as n, readLanHttpState as o, validateUploadedTls as p, applyLanHttp as r, registerLanHttpHandler as s, DEFAULT_LAN_HTTP_PORT as t, readTlsMode as u, collectCertIdentity as v, MAX_LEAF_VALIDITY_DAYS as x, CA_VALIDITY_DAYS as y };
752
+ export { registerLanHttpHandler as C, readLanHttpState as S, DEFAULT_LAN_HTTP_PORT as _, writeTlsMode as a, bindPendingLanHttp as b, loadTlsCert as c, collectCertIdentity as d, CA_VALIDITY_DAYS as f, evaluateExistingCert as g, SERVER_AUTH_OID as h, writeExtraSans as i, reissueTlsLeaf as l, MAX_LEAF_VALIDITY_DAYS as m, readTlsAccessStatus as n, validateUploadedTls as o, LEAF_RENEWAL_WINDOW_DAYS as p, readTlsMode as r, ensureTlsCert as s, readExtraSans as t, CA_COMMON_NAME as u, allFamiliesListenHost as v, closeLanHttp as x, applyLanHttp as y };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@camstack/system",
3
- "version": "1.2.131",
3
+ "version": "1.2.133",
4
4
  "description": "Core addon for CamStack — builtins, pipeline, process management, auth, logging, events",
5
5
  "keywords": [
6
6
  "camstack",