@bleedingdev/modern-js-server-runtime-extensions 3.5.0-ultramodern.3 → 3.5.0-ultramodern.31

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.
Files changed (207) hide show
  1. package/dist/cjs/contract-gate-snapshot-store/file-store.js +102 -0
  2. package/dist/cjs/contract-gate-snapshot-store/http-store.js +142 -0
  3. package/dist/cjs/contract-gate-snapshot-store/index.js +58 -0
  4. package/dist/cjs/contract-gate-snapshot-store/resolve.js +122 -0
  5. package/dist/cjs/contract-gate-snapshot-store/types.js +44 -0
  6. package/dist/cjs/contractGateAutopilot.js +27 -6
  7. package/dist/cjs/index.js +13 -13
  8. package/dist/cjs/module-federation-css/collector.js +79 -0
  9. package/dist/cjs/module-federation-css/index.js +55 -0
  10. package/dist/cjs/module-federation-css/manifest.js +144 -0
  11. package/dist/cjs/module-federation-css/plugin.js +71 -0
  12. package/dist/cjs/module-federation-css/remote.js +133 -0
  13. package/dist/cjs/runtime-fallback-signal/auth.js +87 -0
  14. package/dist/cjs/runtime-fallback-signal/constants.js +72 -0
  15. package/dist/cjs/runtime-fallback-signal/contract-gate.js +64 -0
  16. package/dist/cjs/runtime-fallback-signal/endpoint.js +50 -0
  17. package/dist/cjs/runtime-fallback-signal/errors.js +44 -0
  18. package/dist/cjs/runtime-fallback-signal/index.js +99 -0
  19. package/dist/cjs/runtime-fallback-signal/payload.js +72 -0
  20. package/dist/cjs/runtime-fallback-signal/source.js +66 -0
  21. package/dist/cjs/runtime-fallback-signal/state.js +57 -0
  22. package/dist/cjs/runtime-fallback-signal/status.js +47 -0
  23. package/dist/cjs/runtime-fallback-signal/trust-policy.js +108 -0
  24. package/dist/cjs/runtime-fallback-signal/types.js +18 -0
  25. package/dist/cjs/runtimeFallbackSignal.js +32 -281
  26. package/dist/cjs/telemetry/canary.js +180 -0
  27. package/dist/cjs/telemetry/canaryEvaluation.js +79 -0
  28. package/dist/cjs/telemetry/canarySetup.js +107 -0
  29. package/dist/cjs/telemetry/envelope.js +148 -0
  30. package/dist/cjs/telemetry/exporters.js +148 -0
  31. package/dist/cjs/telemetry/lifecycle.js +98 -0
  32. package/dist/cjs/telemetry/metrics.js +115 -0
  33. package/dist/cjs/telemetry/plugin.js +185 -0
  34. package/dist/cjs/telemetry/registry.js +348 -0
  35. package/dist/cjs/telemetry/registryTypes.js +46 -0
  36. package/dist/cjs/telemetry/runtimeEndpoints.js +135 -0
  37. package/dist/cjs/telemetry.js +32 -343
  38. package/dist/cjs/telemetryCore.js +14 -767
  39. package/dist/esm/contract-gate-snapshot-store/file-store.mjs +51 -0
  40. package/dist/esm/contract-gate-snapshot-store/http-store.mjs +92 -0
  41. package/dist/esm/contract-gate-snapshot-store/index.mjs +4 -0
  42. package/dist/esm/contract-gate-snapshot-store/resolve.mjs +68 -0
  43. package/dist/esm/contract-gate-snapshot-store/types.mjs +3 -0
  44. package/dist/esm/contractGateAutopilot.mjs +26 -5
  45. package/dist/esm/index.mjs +2 -2
  46. package/dist/esm/module-federation-css/collector.mjs +38 -0
  47. package/dist/esm/module-federation-css/index.mjs +4 -0
  48. package/dist/esm/module-federation-css/manifest.mjs +69 -0
  49. package/dist/esm/module-federation-css/plugin.mjs +33 -0
  50. package/dist/esm/module-federation-css/remote.mjs +92 -0
  51. package/dist/esm/runtime-fallback-signal/auth.mjs +40 -0
  52. package/dist/esm/runtime-fallback-signal/constants.mjs +10 -0
  53. package/dist/esm/runtime-fallback-signal/contract-gate.mjs +26 -0
  54. package/dist/esm/runtime-fallback-signal/endpoint.mjs +12 -0
  55. package/dist/esm/runtime-fallback-signal/errors.mjs +6 -0
  56. package/dist/esm/runtime-fallback-signal/index.mjs +9 -0
  57. package/dist/esm/runtime-fallback-signal/payload.mjs +31 -0
  58. package/dist/esm/runtime-fallback-signal/source.mjs +22 -0
  59. package/dist/esm/runtime-fallback-signal/state.mjs +16 -0
  60. package/dist/esm/runtime-fallback-signal/status.mjs +9 -0
  61. package/dist/esm/runtime-fallback-signal/trust-policy.mjs +67 -0
  62. package/dist/esm/runtime-fallback-signal/types.mjs +0 -0
  63. package/dist/esm/runtimeFallbackSignal.mjs +1 -222
  64. package/dist/esm/telemetry/canary.mjs +142 -0
  65. package/dist/esm/telemetry/canaryEvaluation.mjs +41 -0
  66. package/dist/esm/telemetry/canarySetup.mjs +69 -0
  67. package/dist/esm/telemetry/envelope.mjs +101 -0
  68. package/dist/esm/telemetry/exporters.mjs +104 -0
  69. package/dist/esm/telemetry/lifecycle.mjs +60 -0
  70. package/dist/esm/telemetry/metrics.mjs +77 -0
  71. package/dist/esm/telemetry/plugin.mjs +87 -0
  72. package/dist/esm/telemetry/registry.mjs +310 -0
  73. package/dist/esm/telemetry/registryTypes.mjs +8 -0
  74. package/dist/esm/telemetry/runtimeEndpoints.mjs +94 -0
  75. package/dist/esm/telemetry.mjs +1 -275
  76. package/dist/esm/telemetryCore.mjs +6 -759
  77. package/dist/esm-node/contract-gate-snapshot-store/file-store.mjs +52 -0
  78. package/dist/esm-node/contract-gate-snapshot-store/http-store.mjs +93 -0
  79. package/dist/esm-node/contract-gate-snapshot-store/index.mjs +5 -0
  80. package/dist/esm-node/contract-gate-snapshot-store/resolve.mjs +70 -0
  81. package/dist/esm-node/contract-gate-snapshot-store/types.mjs +4 -0
  82. package/dist/esm-node/contractGateAutopilot.mjs +26 -5
  83. package/dist/esm-node/index.mjs +2 -2
  84. package/dist/esm-node/module-federation-css/collector.mjs +39 -0
  85. package/dist/esm-node/module-federation-css/index.mjs +5 -0
  86. package/dist/esm-node/module-federation-css/manifest.mjs +70 -0
  87. package/dist/esm-node/module-federation-css/plugin.mjs +34 -0
  88. package/dist/esm-node/module-federation-css/remote.mjs +93 -0
  89. package/dist/esm-node/runtime-fallback-signal/auth.mjs +41 -0
  90. package/dist/esm-node/runtime-fallback-signal/constants.mjs +11 -0
  91. package/dist/esm-node/runtime-fallback-signal/contract-gate.mjs +27 -0
  92. package/dist/esm-node/runtime-fallback-signal/endpoint.mjs +13 -0
  93. package/dist/esm-node/runtime-fallback-signal/errors.mjs +7 -0
  94. package/dist/esm-node/runtime-fallback-signal/index.mjs +10 -0
  95. package/dist/esm-node/runtime-fallback-signal/payload.mjs +32 -0
  96. package/dist/esm-node/runtime-fallback-signal/source.mjs +23 -0
  97. package/dist/esm-node/runtime-fallback-signal/state.mjs +17 -0
  98. package/dist/esm-node/runtime-fallback-signal/status.mjs +10 -0
  99. package/dist/esm-node/runtime-fallback-signal/trust-policy.mjs +68 -0
  100. package/dist/esm-node/runtime-fallback-signal/types.mjs +1 -0
  101. package/dist/esm-node/runtimeFallbackSignal.mjs +1 -222
  102. package/dist/esm-node/telemetry/canary.mjs +143 -0
  103. package/dist/esm-node/telemetry/canaryEvaluation.mjs +42 -0
  104. package/dist/esm-node/telemetry/canarySetup.mjs +70 -0
  105. package/dist/esm-node/telemetry/envelope.mjs +102 -0
  106. package/dist/esm-node/telemetry/exporters.mjs +105 -0
  107. package/dist/esm-node/telemetry/lifecycle.mjs +61 -0
  108. package/dist/esm-node/telemetry/metrics.mjs +78 -0
  109. package/dist/esm-node/telemetry/plugin.mjs +88 -0
  110. package/dist/esm-node/telemetry/registry.mjs +311 -0
  111. package/dist/esm-node/telemetry/registryTypes.mjs +9 -0
  112. package/dist/esm-node/telemetry/runtimeEndpoints.mjs +95 -0
  113. package/dist/esm-node/telemetry.mjs +1 -275
  114. package/dist/esm-node/telemetryCore.mjs +6 -759
  115. package/dist/types/contract-gate-snapshot-store/file-store.d.ts +3 -0
  116. package/dist/types/contract-gate-snapshot-store/http-store.d.ts +11 -0
  117. package/dist/types/contract-gate-snapshot-store/index.d.ts +5 -0
  118. package/dist/types/contract-gate-snapshot-store/resolve.d.ts +9 -0
  119. package/dist/types/{contractGateSnapshotStore.d.ts → contract-gate-snapshot-store/types.d.ts} +1 -11
  120. package/dist/types/contractGateAutopilot.d.ts +4 -2
  121. package/dist/types/index.d.ts +2 -2
  122. package/dist/types/module-federation-css/collector.d.ts +25 -0
  123. package/dist/types/module-federation-css/index.d.ts +8 -0
  124. package/dist/types/module-federation-css/manifest.d.ts +33 -0
  125. package/dist/types/module-federation-css/plugin.d.ts +23 -0
  126. package/dist/types/module-federation-css/remote.d.ts +20 -0
  127. package/dist/types/runtime-fallback-signal/auth.d.ts +18 -0
  128. package/dist/types/runtime-fallback-signal/constants.d.ts +9 -0
  129. package/dist/types/runtime-fallback-signal/contract-gate.d.ts +2 -0
  130. package/dist/types/runtime-fallback-signal/endpoint.d.ts +1 -0
  131. package/dist/types/runtime-fallback-signal/errors.d.ts +2 -0
  132. package/dist/types/runtime-fallback-signal/index.d.ts +10 -0
  133. package/dist/types/runtime-fallback-signal/payload.d.ts +6 -0
  134. package/dist/types/runtime-fallback-signal/source.d.ts +3 -0
  135. package/dist/types/runtime-fallback-signal/state.d.ts +3 -0
  136. package/dist/types/runtime-fallback-signal/status.d.ts +2 -0
  137. package/dist/types/runtime-fallback-signal/trust-policy.d.ts +13 -0
  138. package/dist/types/runtime-fallback-signal/types.d.ts +50 -0
  139. package/dist/types/runtimeFallbackSignal.d.ts +1 -94
  140. package/dist/types/telemetry/canary.d.ts +85 -0
  141. package/dist/types/telemetry/canaryEvaluation.d.ts +19 -0
  142. package/dist/types/telemetry/canarySetup.d.ts +17 -0
  143. package/dist/types/telemetry/envelope.d.ts +45 -0
  144. package/dist/types/telemetry/exporters.d.ts +12 -0
  145. package/dist/types/telemetry/lifecycle.d.ts +18 -0
  146. package/dist/types/telemetry/metrics.d.ts +3 -0
  147. package/dist/types/telemetry/plugin.d.ts +12 -0
  148. package/dist/types/telemetry/registry.d.ts +72 -0
  149. package/dist/types/telemetry/registryTypes.d.ts +50 -0
  150. package/dist/types/telemetry/runtimeEndpoints.d.ts +35 -0
  151. package/dist/types/telemetry.d.ts +1 -12
  152. package/dist/types/telemetryCore.d.ts +6 -257
  153. package/package.json +7 -7
  154. package/src/contract-gate-snapshot-store/file-store.ts +85 -0
  155. package/src/contract-gate-snapshot-store/http-store.ts +159 -0
  156. package/src/contract-gate-snapshot-store/index.ts +20 -0
  157. package/src/contract-gate-snapshot-store/resolve.ts +175 -0
  158. package/src/contract-gate-snapshot-store/types.ts +64 -0
  159. package/src/contractGateAutopilot.ts +45 -9
  160. package/src/index.ts +6 -6
  161. package/src/module-federation-css/collector.ts +82 -0
  162. package/src/module-federation-css/index.ts +14 -0
  163. package/src/module-federation-css/manifest.ts +166 -0
  164. package/src/module-federation-css/plugin.ts +86 -0
  165. package/src/module-federation-css/remote.ts +165 -0
  166. package/src/runtime-fallback-signal/auth.ts +117 -0
  167. package/src/runtime-fallback-signal/constants.ts +19 -0
  168. package/src/runtime-fallback-signal/contract-gate.ts +43 -0
  169. package/src/runtime-fallback-signal/endpoint.ts +22 -0
  170. package/src/runtime-fallback-signal/errors.ts +10 -0
  171. package/src/runtime-fallback-signal/index.ts +36 -0
  172. package/src/runtime-fallback-signal/payload.ts +79 -0
  173. package/src/runtime-fallback-signal/source.ts +49 -0
  174. package/src/runtime-fallback-signal/state.ts +34 -0
  175. package/src/runtime-fallback-signal/status.ts +22 -0
  176. package/src/runtime-fallback-signal/trust-policy.ts +179 -0
  177. package/src/runtime-fallback-signal/types.ts +66 -0
  178. package/src/runtimeFallbackSignal.ts +1 -584
  179. package/src/telemetry/canary.ts +291 -0
  180. package/src/telemetry/canaryEvaluation.ts +83 -0
  181. package/src/telemetry/canarySetup.ts +160 -0
  182. package/src/telemetry/envelope.ts +189 -0
  183. package/src/telemetry/exporters.ts +165 -0
  184. package/src/telemetry/lifecycle.ts +138 -0
  185. package/src/telemetry/metrics.ts +141 -0
  186. package/src/telemetry/plugin.ts +197 -0
  187. package/src/telemetry/registry.ts +467 -0
  188. package/src/telemetry/registryTypes.ts +63 -0
  189. package/src/telemetry/runtimeEndpoints.ts +172 -0
  190. package/src/telemetry.ts +1 -554
  191. package/src/telemetryCore.ts +34 -1332
  192. package/tests/contractGateAutopilot.test.ts +205 -0
  193. package/tests/contractGateSnapshotStore.test.ts +54 -1
  194. package/tests/helpers.ts +1 -0
  195. package/tests/moduleFederationCss.test.ts +1 -1
  196. package/tests/registration.test.ts +1 -1
  197. package/tests/telemetry.test.ts +40 -0
  198. package/tests/telemetryLifecycle.test.ts +253 -2
  199. package/dist/cjs/contractGateSnapshotStore.js +0 -253
  200. package/dist/cjs/moduleFederationCss.js +0 -285
  201. package/dist/esm/contractGateSnapshotStore.mjs +0 -190
  202. package/dist/esm/moduleFederationCss.mjs +0 -225
  203. package/dist/esm-node/contractGateSnapshotStore.mjs +0 -192
  204. package/dist/esm-node/moduleFederationCss.mjs +0 -226
  205. package/dist/types/moduleFederationCss.d.ts +0 -87
  206. package/src/contractGateSnapshotStore.ts +0 -420
  207. package/src/moduleFederationCss.ts +0 -473
@@ -0,0 +1,159 @@
1
+ import { normalizeSnapshot } from './resolve';
2
+ import type {
3
+ ContractGateSnapshotHttpStoreOptions,
4
+ ContractGateSnapshotStore,
5
+ ContractGateSnapshotStoreUserConfig,
6
+ } from './types';
7
+ import { CONTRACT_GATE_SNAPSHOT_SCHEMA_VERSION } from './types';
8
+
9
+ const DEFAULT_HTTP_STORE_TIMEOUT_MS = 5_000;
10
+
11
+ export const BUILTIN_HTTP_STATE_STORE_MODULES = new Set([
12
+ 'http',
13
+ // historical aliases kept for config compatibility with the era when these
14
+ // modules lived inside @modern-js/server-core.
15
+ '@modern-js/server-core/http',
16
+ '@modern-js/server-core/contract-gate-http-store',
17
+ '@modern-js/server-runtime-extensions/http',
18
+ '@modern-js/server-runtime-extensions/contract-gate-http-store',
19
+ ]);
20
+
21
+ export const normalizeHttpStoreOptions = (
22
+ options: Record<string, unknown> | undefined,
23
+ ): ContractGateSnapshotHttpStoreOptions => {
24
+ const endpoint =
25
+ typeof options?.endpoint === 'string' ? options.endpoint.trim() : '';
26
+ if (!endpoint) {
27
+ throw new Error(
28
+ '[telemetry.canary.autopilot] HTTP stateStore requires options.endpoint',
29
+ );
30
+ }
31
+
32
+ const readMethod =
33
+ typeof options?.readMethod === 'string' && options.readMethod.trim()
34
+ ? options.readMethod.trim().toUpperCase()
35
+ : 'GET';
36
+ const writeMethod =
37
+ typeof options?.writeMethod === 'string' && options.writeMethod.trim()
38
+ ? options.writeMethod.trim().toUpperCase()
39
+ : 'PUT';
40
+
41
+ const timeoutMsRaw = Number(options?.timeoutMs);
42
+ const timeoutMs =
43
+ Number.isFinite(timeoutMsRaw) && timeoutMsRaw > 0
44
+ ? Math.floor(timeoutMsRaw)
45
+ : DEFAULT_HTTP_STORE_TIMEOUT_MS;
46
+
47
+ const headersRaw = options?.headers;
48
+ const headers: Record<string, string> = {};
49
+ if (
50
+ headersRaw &&
51
+ typeof headersRaw === 'object' &&
52
+ !Array.isArray(headersRaw)
53
+ ) {
54
+ Object.entries(headersRaw).forEach(([key, value]) => {
55
+ if (typeof key === 'string' && key.trim().length > 0 && value != null) {
56
+ headers[key] = String(value);
57
+ }
58
+ });
59
+ }
60
+
61
+ return {
62
+ endpoint,
63
+ readMethod,
64
+ writeMethod,
65
+ headers,
66
+ timeoutMs,
67
+ };
68
+ };
69
+
70
+ export const withTimeoutAbort = (timeoutMs: number) => {
71
+ const controller = new AbortController();
72
+ const timer = setTimeout(() => controller.abort(), timeoutMs);
73
+ return {
74
+ signal: controller.signal,
75
+ clear: () => clearTimeout(timer),
76
+ };
77
+ };
78
+
79
+ export const createHttpContractGateSnapshotStore = (
80
+ options: ContractGateSnapshotHttpStoreOptions,
81
+ ): ContractGateSnapshotStore => {
82
+ const normalized = normalizeHttpStoreOptions(
83
+ options as unknown as Record<string, unknown>,
84
+ );
85
+ const endpoint = normalized.endpoint;
86
+
87
+ return {
88
+ name: `http:${endpoint}`,
89
+ async readSnapshot() {
90
+ const { signal, clear } = withTimeoutAbort(normalized.timeoutMs || 5_000);
91
+ try {
92
+ const response = await fetch(endpoint, {
93
+ method: normalized.readMethod || 'GET',
94
+ headers: {
95
+ accept: 'application/json',
96
+ ...(normalized.headers || {}),
97
+ },
98
+ signal,
99
+ });
100
+
101
+ if (response.status === 404) {
102
+ return undefined;
103
+ }
104
+
105
+ if (!response.ok) {
106
+ throw new Error(
107
+ `HTTP stateStore read failed with status ${String(response.status)}`,
108
+ );
109
+ }
110
+
111
+ const payload = await response.json();
112
+ return normalizeSnapshot(payload);
113
+ } finally {
114
+ clear();
115
+ }
116
+ },
117
+ async writeSnapshot(snapshot) {
118
+ const body = JSON.stringify(
119
+ normalizeSnapshot(snapshot) || {
120
+ schemaVersion: CONTRACT_GATE_SNAPSHOT_SCHEMA_VERSION,
121
+ updatedAt: Date.now(),
122
+ gates: {},
123
+ },
124
+ );
125
+ const { signal, clear } = withTimeoutAbort(normalized.timeoutMs || 5_000);
126
+ try {
127
+ const response = await fetch(endpoint, {
128
+ method: normalized.writeMethod || 'PUT',
129
+ headers: {
130
+ 'content-type': 'application/json',
131
+ ...(normalized.headers || {}),
132
+ },
133
+ body,
134
+ signal,
135
+ });
136
+ if (!response.ok) {
137
+ throw new Error(
138
+ `HTTP stateStore write failed with status ${String(response.status)}`,
139
+ );
140
+ }
141
+ } finally {
142
+ clear();
143
+ }
144
+ },
145
+ };
146
+ };
147
+
148
+ export const tryResolveBuiltinSnapshotStore = (input: {
149
+ stateStore: ContractGateSnapshotStoreUserConfig;
150
+ }): ContractGateSnapshotStore | undefined => {
151
+ const moduleName = input.stateStore.module.trim();
152
+ if (!BUILTIN_HTTP_STATE_STORE_MODULES.has(moduleName)) {
153
+ return undefined;
154
+ }
155
+
156
+ return createHttpContractGateSnapshotStore(
157
+ (input.stateStore.options || {}) as ContractGateSnapshotHttpStoreOptions,
158
+ );
159
+ };
@@ -0,0 +1,20 @@
1
+ export {
2
+ createFileContractGateSnapshotStore,
3
+ resolveContractGateSnapshotPath,
4
+ } from './file-store';
5
+ export { createHttpContractGateSnapshotStore } from './http-store';
6
+ export { resolveContractGateSnapshotStore } from './resolve';
7
+ export type {
8
+ ContractGateSnapshotHttpStoreOptions,
9
+ ContractGateSnapshotStore,
10
+ ContractGateSnapshotStoreFactory,
11
+ ContractGateSnapshotStoreFactoryContext,
12
+ ContractGateSnapshotStoreModule,
13
+ ContractGateSnapshotStoreUserConfig,
14
+ GateSnapshot,
15
+ GateSnapshotGateValue,
16
+ } from './types';
17
+ export {
18
+ CONTRACT_GATE_SNAPSHOT_SCHEMA_VERSION,
19
+ DEFAULT_CONTRACT_GATE_SNAPSHOT_PATH,
20
+ } from './types';
@@ -0,0 +1,175 @@
1
+ import { createRequire } from 'node:module';
2
+
3
+ import path from 'path';
4
+
5
+ import { createFileContractGateSnapshotStore } from './file-store';
6
+
7
+ import { tryResolveBuiltinSnapshotStore } from './http-store';
8
+
9
+ import type {
10
+ ContractGateSnapshotStore,
11
+ ContractGateSnapshotStoreFactory,
12
+ ContractGateSnapshotStoreFactoryContext,
13
+ ContractGateSnapshotStoreModule,
14
+ ContractGateSnapshotStoreUserConfig,
15
+ GateSnapshot,
16
+ GateSnapshotGateValue,
17
+ LoggerLike,
18
+ } from './types';
19
+
20
+ import { CONTRACT_GATE_SNAPSHOT_SCHEMA_VERSION } from './types';
21
+
22
+ export const isRecord = (value: unknown): value is Record<string, unknown> =>
23
+ typeof value === 'object' && value !== null && !Array.isArray(value);
24
+
25
+ export const normalizeSnapshot = (
26
+ snapshot: unknown,
27
+ ): GateSnapshot | undefined => {
28
+ if (!isRecord(snapshot)) {
29
+ return undefined;
30
+ }
31
+
32
+ const schemaVersion =
33
+ typeof snapshot.schemaVersion === 'number'
34
+ ? snapshot.schemaVersion
35
+ : CONTRACT_GATE_SNAPSHOT_SCHEMA_VERSION;
36
+ const updatedAt =
37
+ typeof snapshot.updatedAt === 'number' ? snapshot.updatedAt : Date.now();
38
+ const gates = isRecord(snapshot.gates)
39
+ ? (snapshot.gates as Record<string, GateSnapshotGateValue>)
40
+ : {};
41
+
42
+ return {
43
+ schemaVersion,
44
+ updatedAt,
45
+ gates,
46
+ };
47
+ };
48
+
49
+ const pickStoreFactory = (
50
+ mod: ContractGateSnapshotStoreModule,
51
+ ): ContractGateSnapshotStoreFactory | undefined => {
52
+ if (typeof mod === 'function') {
53
+ return mod as unknown as ContractGateSnapshotStoreFactory;
54
+ }
55
+
56
+ if (typeof mod.createContractGateSnapshotStore === 'function') {
57
+ return mod.createContractGateSnapshotStore;
58
+ }
59
+
60
+ if (typeof mod.default === 'function') {
61
+ return mod.default;
62
+ }
63
+
64
+ if (
65
+ mod.default &&
66
+ typeof mod.default === 'object' &&
67
+ typeof mod.default.createContractGateSnapshotStore === 'function'
68
+ ) {
69
+ return mod.default.createContractGateSnapshotStore;
70
+ }
71
+
72
+ return undefined;
73
+ };
74
+
75
+ const ensureStoreShape = (
76
+ store: ContractGateSnapshotStore,
77
+ modulePath: string,
78
+ ) => {
79
+ if (
80
+ !store ||
81
+ typeof store !== 'object' ||
82
+ typeof store.readSnapshot !== 'function' ||
83
+ typeof store.writeSnapshot !== 'function'
84
+ ) {
85
+ throw new Error(
86
+ `Invalid contract gate snapshot store from "${modulePath}". Expected { readSnapshot(), writeSnapshot() }.`,
87
+ );
88
+ }
89
+ };
90
+
91
+ /**
92
+ * Resolves a user-configured stateStore module specifier from the app, not
93
+ * from this framework package: relative paths resolve against appDirectory,
94
+ * and bare package specifiers resolve through the app's own module graph
95
+ * (`createRequire` anchored at the app package.json). Resolving from the
96
+ * framework package breaks app-installed stores under pnpm's strict
97
+ * node_modules layout. Bare specifiers fall back to framework-local
98
+ * resolution for stores installed alongside the framework.
99
+ */
100
+ const resolveStoreModulePath = (appDirectory: string, modulePath: string) => {
101
+ const normalized = modulePath.trim();
102
+ if (!normalized) {
103
+ throw new Error(
104
+ 'Contract gate snapshot stateStore.module must be non-empty',
105
+ );
106
+ }
107
+
108
+ if (path.isAbsolute(normalized)) {
109
+ return normalized;
110
+ }
111
+
112
+ if (normalized.startsWith('.')) {
113
+ return path.resolve(appDirectory, normalized);
114
+ }
115
+
116
+ const appRequire = createRequire(path.join(appDirectory, 'package.json'));
117
+ try {
118
+ return appRequire.resolve(normalized);
119
+ } catch (_error) {
120
+ // Fall back to this package's own resolution so stores installed next to
121
+ // the framework keep working.
122
+ return normalized;
123
+ }
124
+ };
125
+
126
+ export const resolveContractGateSnapshotStore = async (input: {
127
+ appDirectory: string;
128
+ gateSnapshotPath: string;
129
+ stateStore?: ContractGateSnapshotStoreUserConfig;
130
+ logger?: LoggerLike;
131
+ }): Promise<ContractGateSnapshotStore> => {
132
+ const { appDirectory, gateSnapshotPath, stateStore, logger } = input;
133
+
134
+ if (!stateStore?.module) {
135
+ return createFileContractGateSnapshotStore(gateSnapshotPath);
136
+ }
137
+
138
+ const builtinStore = tryResolveBuiltinSnapshotStore({ stateStore });
139
+ if (builtinStore) {
140
+ logger?.info?.(
141
+ `[telemetry.canary.autopilot] using built-in contract gate snapshot store "${builtinStore.name}"`,
142
+ );
143
+ return builtinStore;
144
+ }
145
+
146
+ const modulePath = resolveStoreModulePath(appDirectory, stateStore.module);
147
+ let mod: ContractGateSnapshotStoreModule;
148
+ try {
149
+ // eslint-disable-next-line import/no-dynamic-require,global-require
150
+ mod = require(modulePath) as ContractGateSnapshotStoreModule;
151
+ } catch (error) {
152
+ throw new Error(
153
+ `[telemetry.canary.autopilot] Failed to load stateStore.module "${stateStore.module}" (${modulePath}): ${error instanceof Error ? error.message : String(error)}`,
154
+ );
155
+ }
156
+
157
+ const factory = pickStoreFactory(mod);
158
+ if (!factory) {
159
+ throw new Error(
160
+ `[telemetry.canary.autopilot] stateStore.module "${stateStore.module}" does not export createContractGateSnapshotStore()`,
161
+ );
162
+ }
163
+
164
+ const store = await factory({
165
+ appDirectory,
166
+ gateSnapshotPath,
167
+ options: stateStore.options,
168
+ logger,
169
+ });
170
+ ensureStoreShape(store, modulePath);
171
+ logger?.info?.(
172
+ `[telemetry.canary.autopilot] using contract gate snapshot store "${store.name || modulePath}"`,
173
+ );
174
+ return store;
175
+ };
@@ -0,0 +1,64 @@
1
+ export const CONTRACT_GATE_SNAPSHOT_SCHEMA_VERSION = 1;
2
+
3
+ export const DEFAULT_CONTRACT_GATE_SNAPSHOT_PATH =
4
+ '.modern/contract-gates.json';
5
+
6
+ export type GateSnapshotGateValue =
7
+ | boolean
8
+ | {
9
+ passed?: boolean;
10
+ reason?: string;
11
+ updatedAt?: number;
12
+ expiresAt?: number;
13
+ [key: string]: unknown;
14
+ };
15
+
16
+ export type GateSnapshot = {
17
+ schemaVersion?: number;
18
+ updatedAt?: number;
19
+ gates?: Record<string, GateSnapshotGateValue>;
20
+ };
21
+
22
+ export type LoggerLike = {
23
+ info?: (message: string) => void;
24
+ warn?: (message: string) => void;
25
+ };
26
+
27
+ export type ContractGateSnapshotStore = {
28
+ name: string;
29
+ readSnapshot: () => Promise<GateSnapshot | undefined>;
30
+ writeSnapshot: (snapshot: GateSnapshot) => Promise<void>;
31
+ };
32
+
33
+ export type ContractGateSnapshotStoreFactoryContext = {
34
+ appDirectory: string;
35
+ gateSnapshotPath: string;
36
+ options?: Record<string, unknown>;
37
+ logger?: LoggerLike;
38
+ };
39
+
40
+ export type ContractGateSnapshotStoreFactory = (
41
+ context: ContractGateSnapshotStoreFactoryContext,
42
+ ) => Promise<ContractGateSnapshotStore> | ContractGateSnapshotStore;
43
+
44
+ export type ContractGateSnapshotStoreModule = {
45
+ createContractGateSnapshotStore?: ContractGateSnapshotStoreFactory;
46
+ default?:
47
+ | ContractGateSnapshotStoreFactory
48
+ | {
49
+ createContractGateSnapshotStore?: ContractGateSnapshotStoreFactory;
50
+ };
51
+ };
52
+
53
+ export type ContractGateSnapshotStoreUserConfig = {
54
+ module: string;
55
+ options?: Record<string, unknown>;
56
+ };
57
+
58
+ export type ContractGateSnapshotHttpStoreOptions = {
59
+ endpoint: string;
60
+ readMethod?: string;
61
+ writeMethod?: string;
62
+ headers?: Record<string, string>;
63
+ timeoutMs?: number;
64
+ };
@@ -3,7 +3,7 @@ import {
3
3
  createFileContractGateSnapshotStore,
4
4
  type GateSnapshot,
5
5
  type GateSnapshotGateValue,
6
- } from './contractGateSnapshotStore';
6
+ } from './contract-gate-snapshot-store';
7
7
  import type { TelemetryCanaryOrchestrator } from './telemetryCore';
8
8
 
9
9
  const DEFAULT_POLL_INTERVAL_MS = 15_000;
@@ -39,7 +39,7 @@ export class ContractGateAutopilot {
39
39
  private readonly gateStaleAfterMs: number;
40
40
  private readonly logger?: LoggerLike;
41
41
  private poller?: ReturnType<typeof setInterval>;
42
- private lastSnapshotFingerprint?: string;
42
+ private syncGeneration = 0;
43
43
  private readonly appliedGateFingerprints = new Map<string, string>();
44
44
 
45
45
  constructor(options: ContractGateAutopilotOptions) {
@@ -87,13 +87,20 @@ export class ContractGateAutopilot {
87
87
  }
88
88
 
89
89
  async syncOnce() {
90
+ const syncGeneration = ++this.syncGeneration;
90
91
  const snapshot = await this.loadSnapshot();
92
+ if (syncGeneration !== this.syncGeneration) {
93
+ return 0;
94
+ }
91
95
  if (!snapshot) {
92
96
  return 0;
93
97
  }
94
98
 
99
+ const snapshotGateNames = this.getSnapshotGateNames(snapshot);
95
100
  const gates = this.normalizeSnapshot(snapshot);
96
- let updatedCount = 0;
101
+ let updatedCount = snapshotGateNames
102
+ ? this.clearOmittedGates(snapshotGateNames)
103
+ : 0;
97
104
  for (const gate of gates) {
98
105
  this.orchestrator.addRequiredContractGate(gate.name);
99
106
  const fingerprint = `${gate.passed ? '1' : '0'}:${gate.reason || ''}`;
@@ -112,6 +119,41 @@ export class ContractGateAutopilot {
112
119
  return updatedCount;
113
120
  }
114
121
 
122
+ private getSnapshotGateNames(snapshot: GateSnapshot) {
123
+ const gates = snapshot.gates;
124
+ if (!gates || typeof gates !== 'object') {
125
+ return undefined;
126
+ }
127
+
128
+ const names = new Set<string>();
129
+ for (const name of Object.keys(gates)) {
130
+ const normalizedName = name.trim();
131
+ if (normalizedName) {
132
+ names.add(normalizedName);
133
+ }
134
+ }
135
+
136
+ return names;
137
+ }
138
+
139
+ private clearOmittedGates(snapshotGateNames: ReadonlySet<string>) {
140
+ let updatedCount = 0;
141
+ for (const gateName of Array.from(this.appliedGateFingerprints.keys())) {
142
+ if (snapshotGateNames.has(gateName)) {
143
+ continue;
144
+ }
145
+
146
+ this.orchestrator.setContractGate(gateName, true);
147
+ this.appliedGateFingerprints.delete(gateName);
148
+ updatedCount += 1;
149
+ this.logger?.info?.(
150
+ `[telemetry.canary.autopilot] gate=${gateName} passed=true reason=omitted`,
151
+ );
152
+ }
153
+
154
+ return updatedCount;
155
+ }
156
+
115
157
  private async loadSnapshot() {
116
158
  try {
117
159
  const snapshot = await this.gateSnapshotStore.readSnapshot();
@@ -119,12 +161,6 @@ export class ContractGateAutopilot {
119
161
  return undefined;
120
162
  }
121
163
 
122
- const fingerprint = JSON.stringify(snapshot);
123
- if (fingerprint === this.lastSnapshotFingerprint) {
124
- return undefined;
125
- }
126
- this.lastSnapshotFingerprint = fingerprint;
127
-
128
164
  return snapshot;
129
165
  } catch (error) {
130
166
  const source =
package/src/index.ts CHANGED
@@ -1,7 +1,3 @@
1
- export {
2
- ContractGateAutopilot,
3
- type ContractGateAutopilotOptions,
4
- } from './contractGateAutopilot';
5
1
  export {
6
2
  CONTRACT_GATE_SNAPSHOT_SCHEMA_VERSION,
7
3
  type ContractGateSnapshotHttpStoreOptions,
@@ -17,7 +13,11 @@ export {
17
13
  type GateSnapshotGateValue,
18
14
  resolveContractGateSnapshotPath,
19
15
  resolveContractGateSnapshotStore,
20
- } from './contractGateSnapshotStore';
16
+ } from './contract-gate-snapshot-store';
17
+ export {
18
+ ContractGateAutopilot,
19
+ type ContractGateAutopilotOptions,
20
+ } from './contractGateAutopilot';
21
21
  export {
22
22
  DEFAULT_ENVIRONMENT_NAME,
23
23
  parseServerRuntimeExtensionsEnv,
@@ -41,7 +41,7 @@ export {
41
41
  type ModuleFederationCssPluginOptions,
42
42
  type ModuleFederationManifest,
43
43
  type RemoteModuleFederationCssCollection,
44
- } from './moduleFederationCss';
44
+ } from './module-federation-css';
45
45
  export {
46
46
  createOtlpTelemetryExporter,
47
47
  createRuntimeFallbackSignalRuntimeState,
@@ -0,0 +1,82 @@
1
+ import type { Monitors } from '@modern-js/types';
2
+
3
+ import type { CollectDirectRemoteModuleFederationCssOptions } from './remote';
4
+
5
+ import { collectDirectRemoteModuleFederationCssWithMeta } from './remote';
6
+
7
+ export type ModuleFederationCssCollectorOptions =
8
+ CollectDirectRemoteModuleFederationCssOptions & {
9
+ /**
10
+ * How long a successful remote CSS collection may be served from cache.
11
+ * `0` disables caching (beyond in-flight request coalescing).
12
+ */
13
+ ttlMs?: number;
14
+ /** Clock override for tests. */
15
+ now?: () => number;
16
+ };
17
+
18
+ /**
19
+ * TTL cache around remote MF CSS collection. Remote manifests mutate
20
+ * independently of the host (that is the point of module federation), so the
21
+ * collection must expire: a boot-time-forever cache serves stale or broken
22
+ * CSS links after any remote redeploy.
23
+ *
24
+ * Error handling: a collection where any remote fetch failed is never cached.
25
+ * The last known-good asset list is served instead (stale-on-error) and the
26
+ * next request retries the collection.
27
+ */
28
+ export const createModuleFederationCssCollector = (
29
+ pwd: string,
30
+ options: ModuleFederationCssCollectorOptions = {},
31
+ ) => {
32
+ const { ttlMs = 0, now = Date.now, ...collectOptions } = options;
33
+ const normalizedTtlMs = Math.max(0, ttlMs);
34
+
35
+ let cached: { assets: string[]; expiresAt: number } | undefined;
36
+ let lastGoodAssets: string[] | undefined;
37
+ let inflight: Promise<string[]> | undefined;
38
+
39
+ const refresh = (monitors?: Monitors) => {
40
+ const promise = collectDirectRemoteModuleFederationCssWithMeta(pwd, {
41
+ ...collectOptions,
42
+ monitors: monitors ?? collectOptions.monitors,
43
+ })
44
+ .then(result => {
45
+ if (!result.errored) {
46
+ lastGoodAssets = result.assets;
47
+ cached = {
48
+ assets: result.assets,
49
+ expiresAt: now() + normalizedTtlMs,
50
+ };
51
+ return result.assets;
52
+ }
53
+
54
+ // Error path: invalidate so the next request retries, and serve the
55
+ // last known-good list when one exists.
56
+ cached = undefined;
57
+ return lastGoodAssets ?? result.assets;
58
+ })
59
+ .finally(() => {
60
+ if (inflight === promise) {
61
+ inflight = undefined;
62
+ }
63
+ });
64
+
65
+ inflight = promise;
66
+ return promise;
67
+ };
68
+
69
+ return {
70
+ collect(monitors?: Monitors): Promise<string[]> {
71
+ if (cached && now() < cached.expiresAt) {
72
+ return Promise.resolve(cached.assets);
73
+ }
74
+ if (inflight) {
75
+ return inflight;
76
+ }
77
+ return refresh(monitors);
78
+ },
79
+ };
80
+ };
81
+
82
+ export const DEFAULT_REMOTE_CSS_CACHE_TTL_MS = 30_000;
@@ -0,0 +1,14 @@
1
+ export type { ModuleFederationCssCollectorOptions } from './collector';
2
+ export { createModuleFederationCssCollector } from './collector';
3
+ export type { ModuleFederationManifest } from './manifest';
4
+ export { collectModuleFederationManifestCss } from './manifest';
5
+ export type { ModuleFederationCssPluginOptions } from './plugin';
6
+ export { injectModuleFederationCssPlugin } from './plugin';
7
+ export type {
8
+ CollectDirectRemoteModuleFederationCssOptions,
9
+ RemoteModuleFederationCssCollection,
10
+ } from './remote';
11
+ export {
12
+ collectDirectRemoteModuleFederationCss,
13
+ collectDirectRemoteModuleFederationCssWithMeta,
14
+ } from './remote';