@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.
- package/dist/cjs/contract-gate-snapshot-store/file-store.js +102 -0
- package/dist/cjs/contract-gate-snapshot-store/http-store.js +142 -0
- package/dist/cjs/contract-gate-snapshot-store/index.js +58 -0
- package/dist/cjs/contract-gate-snapshot-store/resolve.js +122 -0
- package/dist/cjs/contract-gate-snapshot-store/types.js +44 -0
- package/dist/cjs/contractGateAutopilot.js +27 -6
- package/dist/cjs/index.js +13 -13
- package/dist/cjs/module-federation-css/collector.js +79 -0
- package/dist/cjs/module-federation-css/index.js +55 -0
- package/dist/cjs/module-federation-css/manifest.js +144 -0
- package/dist/cjs/module-federation-css/plugin.js +71 -0
- package/dist/cjs/module-federation-css/remote.js +133 -0
- package/dist/cjs/runtime-fallback-signal/auth.js +87 -0
- package/dist/cjs/runtime-fallback-signal/constants.js +72 -0
- package/dist/cjs/runtime-fallback-signal/contract-gate.js +64 -0
- package/dist/cjs/runtime-fallback-signal/endpoint.js +50 -0
- package/dist/cjs/runtime-fallback-signal/errors.js +44 -0
- package/dist/cjs/runtime-fallback-signal/index.js +99 -0
- package/dist/cjs/runtime-fallback-signal/payload.js +72 -0
- package/dist/cjs/runtime-fallback-signal/source.js +66 -0
- package/dist/cjs/runtime-fallback-signal/state.js +57 -0
- package/dist/cjs/runtime-fallback-signal/status.js +47 -0
- package/dist/cjs/runtime-fallback-signal/trust-policy.js +108 -0
- package/dist/cjs/runtime-fallback-signal/types.js +18 -0
- package/dist/cjs/runtimeFallbackSignal.js +32 -281
- package/dist/cjs/telemetry/canary.js +180 -0
- package/dist/cjs/telemetry/canaryEvaluation.js +79 -0
- package/dist/cjs/telemetry/canarySetup.js +107 -0
- package/dist/cjs/telemetry/envelope.js +148 -0
- package/dist/cjs/telemetry/exporters.js +148 -0
- package/dist/cjs/telemetry/lifecycle.js +98 -0
- package/dist/cjs/telemetry/metrics.js +115 -0
- package/dist/cjs/telemetry/plugin.js +185 -0
- package/dist/cjs/telemetry/registry.js +348 -0
- package/dist/cjs/telemetry/registryTypes.js +46 -0
- package/dist/cjs/telemetry/runtimeEndpoints.js +135 -0
- package/dist/cjs/telemetry.js +32 -343
- package/dist/cjs/telemetryCore.js +14 -767
- package/dist/esm/contract-gate-snapshot-store/file-store.mjs +51 -0
- package/dist/esm/contract-gate-snapshot-store/http-store.mjs +92 -0
- package/dist/esm/contract-gate-snapshot-store/index.mjs +4 -0
- package/dist/esm/contract-gate-snapshot-store/resolve.mjs +68 -0
- package/dist/esm/contract-gate-snapshot-store/types.mjs +3 -0
- package/dist/esm/contractGateAutopilot.mjs +26 -5
- package/dist/esm/index.mjs +2 -2
- package/dist/esm/module-federation-css/collector.mjs +38 -0
- package/dist/esm/module-federation-css/index.mjs +4 -0
- package/dist/esm/module-federation-css/manifest.mjs +69 -0
- package/dist/esm/module-federation-css/plugin.mjs +33 -0
- package/dist/esm/module-federation-css/remote.mjs +92 -0
- package/dist/esm/runtime-fallback-signal/auth.mjs +40 -0
- package/dist/esm/runtime-fallback-signal/constants.mjs +10 -0
- package/dist/esm/runtime-fallback-signal/contract-gate.mjs +26 -0
- package/dist/esm/runtime-fallback-signal/endpoint.mjs +12 -0
- package/dist/esm/runtime-fallback-signal/errors.mjs +6 -0
- package/dist/esm/runtime-fallback-signal/index.mjs +9 -0
- package/dist/esm/runtime-fallback-signal/payload.mjs +31 -0
- package/dist/esm/runtime-fallback-signal/source.mjs +22 -0
- package/dist/esm/runtime-fallback-signal/state.mjs +16 -0
- package/dist/esm/runtime-fallback-signal/status.mjs +9 -0
- package/dist/esm/runtime-fallback-signal/trust-policy.mjs +67 -0
- package/dist/esm/runtime-fallback-signal/types.mjs +0 -0
- package/dist/esm/runtimeFallbackSignal.mjs +1 -222
- package/dist/esm/telemetry/canary.mjs +142 -0
- package/dist/esm/telemetry/canaryEvaluation.mjs +41 -0
- package/dist/esm/telemetry/canarySetup.mjs +69 -0
- package/dist/esm/telemetry/envelope.mjs +101 -0
- package/dist/esm/telemetry/exporters.mjs +104 -0
- package/dist/esm/telemetry/lifecycle.mjs +60 -0
- package/dist/esm/telemetry/metrics.mjs +77 -0
- package/dist/esm/telemetry/plugin.mjs +87 -0
- package/dist/esm/telemetry/registry.mjs +310 -0
- package/dist/esm/telemetry/registryTypes.mjs +8 -0
- package/dist/esm/telemetry/runtimeEndpoints.mjs +94 -0
- package/dist/esm/telemetry.mjs +1 -275
- package/dist/esm/telemetryCore.mjs +6 -759
- package/dist/esm-node/contract-gate-snapshot-store/file-store.mjs +52 -0
- package/dist/esm-node/contract-gate-snapshot-store/http-store.mjs +93 -0
- package/dist/esm-node/contract-gate-snapshot-store/index.mjs +5 -0
- package/dist/esm-node/contract-gate-snapshot-store/resolve.mjs +70 -0
- package/dist/esm-node/contract-gate-snapshot-store/types.mjs +4 -0
- package/dist/esm-node/contractGateAutopilot.mjs +26 -5
- package/dist/esm-node/index.mjs +2 -2
- package/dist/esm-node/module-federation-css/collector.mjs +39 -0
- package/dist/esm-node/module-federation-css/index.mjs +5 -0
- package/dist/esm-node/module-federation-css/manifest.mjs +70 -0
- package/dist/esm-node/module-federation-css/plugin.mjs +34 -0
- package/dist/esm-node/module-federation-css/remote.mjs +93 -0
- package/dist/esm-node/runtime-fallback-signal/auth.mjs +41 -0
- package/dist/esm-node/runtime-fallback-signal/constants.mjs +11 -0
- package/dist/esm-node/runtime-fallback-signal/contract-gate.mjs +27 -0
- package/dist/esm-node/runtime-fallback-signal/endpoint.mjs +13 -0
- package/dist/esm-node/runtime-fallback-signal/errors.mjs +7 -0
- package/dist/esm-node/runtime-fallback-signal/index.mjs +10 -0
- package/dist/esm-node/runtime-fallback-signal/payload.mjs +32 -0
- package/dist/esm-node/runtime-fallback-signal/source.mjs +23 -0
- package/dist/esm-node/runtime-fallback-signal/state.mjs +17 -0
- package/dist/esm-node/runtime-fallback-signal/status.mjs +10 -0
- package/dist/esm-node/runtime-fallback-signal/trust-policy.mjs +68 -0
- package/dist/esm-node/runtime-fallback-signal/types.mjs +1 -0
- package/dist/esm-node/runtimeFallbackSignal.mjs +1 -222
- package/dist/esm-node/telemetry/canary.mjs +143 -0
- package/dist/esm-node/telemetry/canaryEvaluation.mjs +42 -0
- package/dist/esm-node/telemetry/canarySetup.mjs +70 -0
- package/dist/esm-node/telemetry/envelope.mjs +102 -0
- package/dist/esm-node/telemetry/exporters.mjs +105 -0
- package/dist/esm-node/telemetry/lifecycle.mjs +61 -0
- package/dist/esm-node/telemetry/metrics.mjs +78 -0
- package/dist/esm-node/telemetry/plugin.mjs +88 -0
- package/dist/esm-node/telemetry/registry.mjs +311 -0
- package/dist/esm-node/telemetry/registryTypes.mjs +9 -0
- package/dist/esm-node/telemetry/runtimeEndpoints.mjs +95 -0
- package/dist/esm-node/telemetry.mjs +1 -275
- package/dist/esm-node/telemetryCore.mjs +6 -759
- package/dist/types/contract-gate-snapshot-store/file-store.d.ts +3 -0
- package/dist/types/contract-gate-snapshot-store/http-store.d.ts +11 -0
- package/dist/types/contract-gate-snapshot-store/index.d.ts +5 -0
- package/dist/types/contract-gate-snapshot-store/resolve.d.ts +9 -0
- package/dist/types/{contractGateSnapshotStore.d.ts → contract-gate-snapshot-store/types.d.ts} +1 -11
- package/dist/types/contractGateAutopilot.d.ts +4 -2
- package/dist/types/index.d.ts +2 -2
- package/dist/types/module-federation-css/collector.d.ts +25 -0
- package/dist/types/module-federation-css/index.d.ts +8 -0
- package/dist/types/module-federation-css/manifest.d.ts +33 -0
- package/dist/types/module-federation-css/plugin.d.ts +23 -0
- package/dist/types/module-federation-css/remote.d.ts +20 -0
- package/dist/types/runtime-fallback-signal/auth.d.ts +18 -0
- package/dist/types/runtime-fallback-signal/constants.d.ts +9 -0
- package/dist/types/runtime-fallback-signal/contract-gate.d.ts +2 -0
- package/dist/types/runtime-fallback-signal/endpoint.d.ts +1 -0
- package/dist/types/runtime-fallback-signal/errors.d.ts +2 -0
- package/dist/types/runtime-fallback-signal/index.d.ts +10 -0
- package/dist/types/runtime-fallback-signal/payload.d.ts +6 -0
- package/dist/types/runtime-fallback-signal/source.d.ts +3 -0
- package/dist/types/runtime-fallback-signal/state.d.ts +3 -0
- package/dist/types/runtime-fallback-signal/status.d.ts +2 -0
- package/dist/types/runtime-fallback-signal/trust-policy.d.ts +13 -0
- package/dist/types/runtime-fallback-signal/types.d.ts +50 -0
- package/dist/types/runtimeFallbackSignal.d.ts +1 -94
- package/dist/types/telemetry/canary.d.ts +85 -0
- package/dist/types/telemetry/canaryEvaluation.d.ts +19 -0
- package/dist/types/telemetry/canarySetup.d.ts +17 -0
- package/dist/types/telemetry/envelope.d.ts +45 -0
- package/dist/types/telemetry/exporters.d.ts +12 -0
- package/dist/types/telemetry/lifecycle.d.ts +18 -0
- package/dist/types/telemetry/metrics.d.ts +3 -0
- package/dist/types/telemetry/plugin.d.ts +12 -0
- package/dist/types/telemetry/registry.d.ts +72 -0
- package/dist/types/telemetry/registryTypes.d.ts +50 -0
- package/dist/types/telemetry/runtimeEndpoints.d.ts +35 -0
- package/dist/types/telemetry.d.ts +1 -12
- package/dist/types/telemetryCore.d.ts +6 -257
- package/package.json +7 -7
- package/src/contract-gate-snapshot-store/file-store.ts +85 -0
- package/src/contract-gate-snapshot-store/http-store.ts +159 -0
- package/src/contract-gate-snapshot-store/index.ts +20 -0
- package/src/contract-gate-snapshot-store/resolve.ts +175 -0
- package/src/contract-gate-snapshot-store/types.ts +64 -0
- package/src/contractGateAutopilot.ts +45 -9
- package/src/index.ts +6 -6
- package/src/module-federation-css/collector.ts +82 -0
- package/src/module-federation-css/index.ts +14 -0
- package/src/module-federation-css/manifest.ts +166 -0
- package/src/module-federation-css/plugin.ts +86 -0
- package/src/module-federation-css/remote.ts +165 -0
- package/src/runtime-fallback-signal/auth.ts +117 -0
- package/src/runtime-fallback-signal/constants.ts +19 -0
- package/src/runtime-fallback-signal/contract-gate.ts +43 -0
- package/src/runtime-fallback-signal/endpoint.ts +22 -0
- package/src/runtime-fallback-signal/errors.ts +10 -0
- package/src/runtime-fallback-signal/index.ts +36 -0
- package/src/runtime-fallback-signal/payload.ts +79 -0
- package/src/runtime-fallback-signal/source.ts +49 -0
- package/src/runtime-fallback-signal/state.ts +34 -0
- package/src/runtime-fallback-signal/status.ts +22 -0
- package/src/runtime-fallback-signal/trust-policy.ts +179 -0
- package/src/runtime-fallback-signal/types.ts +66 -0
- package/src/runtimeFallbackSignal.ts +1 -584
- package/src/telemetry/canary.ts +291 -0
- package/src/telemetry/canaryEvaluation.ts +83 -0
- package/src/telemetry/canarySetup.ts +160 -0
- package/src/telemetry/envelope.ts +189 -0
- package/src/telemetry/exporters.ts +165 -0
- package/src/telemetry/lifecycle.ts +138 -0
- package/src/telemetry/metrics.ts +141 -0
- package/src/telemetry/plugin.ts +197 -0
- package/src/telemetry/registry.ts +467 -0
- package/src/telemetry/registryTypes.ts +63 -0
- package/src/telemetry/runtimeEndpoints.ts +172 -0
- package/src/telemetry.ts +1 -554
- package/src/telemetryCore.ts +34 -1332
- package/tests/contractGateAutopilot.test.ts +205 -0
- package/tests/contractGateSnapshotStore.test.ts +54 -1
- package/tests/helpers.ts +1 -0
- package/tests/moduleFederationCss.test.ts +1 -1
- package/tests/registration.test.ts +1 -1
- package/tests/telemetry.test.ts +40 -0
- package/tests/telemetryLifecycle.test.ts +253 -2
- package/dist/cjs/contractGateSnapshotStore.js +0 -253
- package/dist/cjs/moduleFederationCss.js +0 -285
- package/dist/esm/contractGateSnapshotStore.mjs +0 -190
- package/dist/esm/moduleFederationCss.mjs +0 -225
- package/dist/esm-node/contractGateSnapshotStore.mjs +0 -192
- package/dist/esm-node/moduleFederationCss.mjs +0 -226
- package/dist/types/moduleFederationCss.d.ts +0 -87
- package/src/contractGateSnapshotStore.ts +0 -420
- package/src/moduleFederationCss.ts +0 -473
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
import { fs } from "@modern-js/utils";
|
|
2
|
+
import { promises } from "fs";
|
|
3
|
+
import path from "path";
|
|
4
|
+
import { parseServerRuntimeExtensionsEnv } from "../env.mjs";
|
|
5
|
+
import { normalizeSnapshot } from "./resolve.mjs";
|
|
6
|
+
import { CONTRACT_GATE_SNAPSHOT_SCHEMA_VERSION, DEFAULT_CONTRACT_GATE_SNAPSHOT_PATH } from "./types.mjs";
|
|
7
|
+
const resolveContractGateSnapshotPath = (appDirectory, configuredPath)=>{
|
|
8
|
+
const rawPath = configuredPath || parseServerRuntimeExtensionsEnv().contractGatesFile || DEFAULT_CONTRACT_GATE_SNAPSHOT_PATH;
|
|
9
|
+
if (path.isAbsolute(rawPath)) return rawPath;
|
|
10
|
+
return path.resolve(appDirectory, rawPath);
|
|
11
|
+
};
|
|
12
|
+
const writeSnapshotFileAtomically = async (resolvedPath, contents)=>{
|
|
13
|
+
const directory = path.dirname(resolvedPath);
|
|
14
|
+
await promises.mkdir(directory, {
|
|
15
|
+
recursive: true
|
|
16
|
+
});
|
|
17
|
+
const temporaryPath = path.join(directory, `.${path.basename(resolvedPath)}.${process.pid}.${Date.now()}.${Math.random().toString(36).slice(2)}.tmp`);
|
|
18
|
+
try {
|
|
19
|
+
await promises.writeFile(temporaryPath, contents);
|
|
20
|
+
await promises.rename(temporaryPath, resolvedPath);
|
|
21
|
+
} catch (error) {
|
|
22
|
+
await promises.rm(temporaryPath, {
|
|
23
|
+
force: true
|
|
24
|
+
}).catch(()=>{});
|
|
25
|
+
throw error;
|
|
26
|
+
}
|
|
27
|
+
};
|
|
28
|
+
const createFileContractGateSnapshotStore = (gateSnapshotPath)=>{
|
|
29
|
+
const resolvedPath = path.resolve(gateSnapshotPath);
|
|
30
|
+
return {
|
|
31
|
+
name: `file:${resolvedPath}`,
|
|
32
|
+
async readSnapshot () {
|
|
33
|
+
if (!await fs.pathExists(resolvedPath)) return;
|
|
34
|
+
try {
|
|
35
|
+
const raw = await promises.readFile(resolvedPath, 'utf8');
|
|
36
|
+
return normalizeSnapshot(JSON.parse(raw));
|
|
37
|
+
} catch (_error) {
|
|
38
|
+
return;
|
|
39
|
+
}
|
|
40
|
+
},
|
|
41
|
+
async writeSnapshot (snapshot) {
|
|
42
|
+
const normalized = normalizeSnapshot(snapshot) || {
|
|
43
|
+
schemaVersion: CONTRACT_GATE_SNAPSHOT_SCHEMA_VERSION,
|
|
44
|
+
updatedAt: Date.now(),
|
|
45
|
+
gates: {}
|
|
46
|
+
};
|
|
47
|
+
await writeSnapshotFileAtomically(resolvedPath, `${JSON.stringify(normalized, null, 2)}\n`);
|
|
48
|
+
}
|
|
49
|
+
};
|
|
50
|
+
};
|
|
51
|
+
export { createFileContractGateSnapshotStore, resolveContractGateSnapshotPath };
|
|
@@ -0,0 +1,92 @@
|
|
|
1
|
+
import { normalizeSnapshot } from "./resolve.mjs";
|
|
2
|
+
import { CONTRACT_GATE_SNAPSHOT_SCHEMA_VERSION } from "./types.mjs";
|
|
3
|
+
const DEFAULT_HTTP_STORE_TIMEOUT_MS = 5000;
|
|
4
|
+
const BUILTIN_HTTP_STATE_STORE_MODULES = new Set([
|
|
5
|
+
'http',
|
|
6
|
+
'@modern-js/server-core/http',
|
|
7
|
+
'@modern-js/server-core/contract-gate-http-store',
|
|
8
|
+
'@modern-js/server-runtime-extensions/http',
|
|
9
|
+
'@modern-js/server-runtime-extensions/contract-gate-http-store'
|
|
10
|
+
]);
|
|
11
|
+
const normalizeHttpStoreOptions = (options)=>{
|
|
12
|
+
const endpoint = 'string' == typeof options?.endpoint ? options.endpoint.trim() : '';
|
|
13
|
+
if (!endpoint) throw new Error('[telemetry.canary.autopilot] HTTP stateStore requires options.endpoint');
|
|
14
|
+
const readMethod = 'string' == typeof options?.readMethod && options.readMethod.trim() ? options.readMethod.trim().toUpperCase() : 'GET';
|
|
15
|
+
const writeMethod = 'string' == typeof options?.writeMethod && options.writeMethod.trim() ? options.writeMethod.trim().toUpperCase() : 'PUT';
|
|
16
|
+
const timeoutMsRaw = Number(options?.timeoutMs);
|
|
17
|
+
const timeoutMs = Number.isFinite(timeoutMsRaw) && timeoutMsRaw > 0 ? Math.floor(timeoutMsRaw) : DEFAULT_HTTP_STORE_TIMEOUT_MS;
|
|
18
|
+
const headersRaw = options?.headers;
|
|
19
|
+
const headers = {};
|
|
20
|
+
if (headersRaw && 'object' == typeof headersRaw && !Array.isArray(headersRaw)) Object.entries(headersRaw).forEach(([key, value])=>{
|
|
21
|
+
if ('string' == typeof key && key.trim().length > 0 && null != value) headers[key] = String(value);
|
|
22
|
+
});
|
|
23
|
+
return {
|
|
24
|
+
endpoint,
|
|
25
|
+
readMethod,
|
|
26
|
+
writeMethod,
|
|
27
|
+
headers,
|
|
28
|
+
timeoutMs
|
|
29
|
+
};
|
|
30
|
+
};
|
|
31
|
+
const withTimeoutAbort = (timeoutMs)=>{
|
|
32
|
+
const controller = new AbortController();
|
|
33
|
+
const timer = setTimeout(()=>controller.abort(), timeoutMs);
|
|
34
|
+
return {
|
|
35
|
+
signal: controller.signal,
|
|
36
|
+
clear: ()=>clearTimeout(timer)
|
|
37
|
+
};
|
|
38
|
+
};
|
|
39
|
+
const createHttpContractGateSnapshotStore = (options)=>{
|
|
40
|
+
const normalized = normalizeHttpStoreOptions(options);
|
|
41
|
+
const endpoint = normalized.endpoint;
|
|
42
|
+
return {
|
|
43
|
+
name: `http:${endpoint}`,
|
|
44
|
+
async readSnapshot () {
|
|
45
|
+
const { signal, clear } = withTimeoutAbort(normalized.timeoutMs || 5000);
|
|
46
|
+
try {
|
|
47
|
+
const response = await fetch(endpoint, {
|
|
48
|
+
method: normalized.readMethod || 'GET',
|
|
49
|
+
headers: {
|
|
50
|
+
accept: 'application/json',
|
|
51
|
+
...normalized.headers || {}
|
|
52
|
+
},
|
|
53
|
+
signal
|
|
54
|
+
});
|
|
55
|
+
if (404 === response.status) return;
|
|
56
|
+
if (!response.ok) throw new Error(`HTTP stateStore read failed with status ${String(response.status)}`);
|
|
57
|
+
const payload = await response.json();
|
|
58
|
+
return normalizeSnapshot(payload);
|
|
59
|
+
} finally{
|
|
60
|
+
clear();
|
|
61
|
+
}
|
|
62
|
+
},
|
|
63
|
+
async writeSnapshot (snapshot) {
|
|
64
|
+
const body = JSON.stringify(normalizeSnapshot(snapshot) || {
|
|
65
|
+
schemaVersion: CONTRACT_GATE_SNAPSHOT_SCHEMA_VERSION,
|
|
66
|
+
updatedAt: Date.now(),
|
|
67
|
+
gates: {}
|
|
68
|
+
});
|
|
69
|
+
const { signal, clear } = withTimeoutAbort(normalized.timeoutMs || 5000);
|
|
70
|
+
try {
|
|
71
|
+
const response = await fetch(endpoint, {
|
|
72
|
+
method: normalized.writeMethod || 'PUT',
|
|
73
|
+
headers: {
|
|
74
|
+
'content-type': 'application/json',
|
|
75
|
+
...normalized.headers || {}
|
|
76
|
+
},
|
|
77
|
+
body,
|
|
78
|
+
signal
|
|
79
|
+
});
|
|
80
|
+
if (!response.ok) throw new Error(`HTTP stateStore write failed with status ${String(response.status)}`);
|
|
81
|
+
} finally{
|
|
82
|
+
clear();
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
};
|
|
86
|
+
};
|
|
87
|
+
const tryResolveBuiltinSnapshotStore = (input)=>{
|
|
88
|
+
const moduleName = input.stateStore.module.trim();
|
|
89
|
+
if (!BUILTIN_HTTP_STATE_STORE_MODULES.has(moduleName)) return;
|
|
90
|
+
return createHttpContractGateSnapshotStore(input.stateStore.options || {});
|
|
91
|
+
};
|
|
92
|
+
export { BUILTIN_HTTP_STATE_STORE_MODULES, createHttpContractGateSnapshotStore, normalizeHttpStoreOptions, tryResolveBuiltinSnapshotStore, withTimeoutAbort };
|
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
export { createFileContractGateSnapshotStore, resolveContractGateSnapshotPath } from "./file-store.mjs";
|
|
2
|
+
export { createHttpContractGateSnapshotStore } from "./http-store.mjs";
|
|
3
|
+
export { resolveContractGateSnapshotStore } from "./resolve.mjs";
|
|
4
|
+
export { CONTRACT_GATE_SNAPSHOT_SCHEMA_VERSION, DEFAULT_CONTRACT_GATE_SNAPSHOT_PATH } from "./types.mjs";
|
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
import { createRequire } from "node:module";
|
|
2
|
+
import path from "path";
|
|
3
|
+
import { createFileContractGateSnapshotStore } from "./file-store.mjs";
|
|
4
|
+
import { tryResolveBuiltinSnapshotStore } from "./http-store.mjs";
|
|
5
|
+
import { CONTRACT_GATE_SNAPSHOT_SCHEMA_VERSION } from "./types.mjs";
|
|
6
|
+
const isRecord = (value)=>'object' == typeof value && null !== value && !Array.isArray(value);
|
|
7
|
+
const normalizeSnapshot = (snapshot)=>{
|
|
8
|
+
if (!isRecord(snapshot)) return;
|
|
9
|
+
const schemaVersion = 'number' == typeof snapshot.schemaVersion ? snapshot.schemaVersion : CONTRACT_GATE_SNAPSHOT_SCHEMA_VERSION;
|
|
10
|
+
const updatedAt = 'number' == typeof snapshot.updatedAt ? snapshot.updatedAt : Date.now();
|
|
11
|
+
const gates = isRecord(snapshot.gates) ? snapshot.gates : {};
|
|
12
|
+
return {
|
|
13
|
+
schemaVersion,
|
|
14
|
+
updatedAt,
|
|
15
|
+
gates
|
|
16
|
+
};
|
|
17
|
+
};
|
|
18
|
+
const pickStoreFactory = (mod)=>{
|
|
19
|
+
if ('function' == typeof mod) return mod;
|
|
20
|
+
if ('function' == typeof mod.createContractGateSnapshotStore) return mod.createContractGateSnapshotStore;
|
|
21
|
+
if ('function' == typeof mod.default) return mod.default;
|
|
22
|
+
if (mod.default && 'object' == typeof mod.default && 'function' == typeof mod.default.createContractGateSnapshotStore) return mod.default.createContractGateSnapshotStore;
|
|
23
|
+
};
|
|
24
|
+
const ensureStoreShape = (store, modulePath)=>{
|
|
25
|
+
if (!store || 'object' != typeof store || 'function' != typeof store.readSnapshot || 'function' != typeof store.writeSnapshot) throw new Error(`Invalid contract gate snapshot store from "${modulePath}". Expected { readSnapshot(), writeSnapshot() }.`);
|
|
26
|
+
};
|
|
27
|
+
const resolveStoreModulePath = (appDirectory, modulePath)=>{
|
|
28
|
+
const normalized = modulePath.trim();
|
|
29
|
+
if (!normalized) throw new Error('Contract gate snapshot stateStore.module must be non-empty');
|
|
30
|
+
if (path.isAbsolute(normalized)) return normalized;
|
|
31
|
+
if (normalized.startsWith('.')) return path.resolve(appDirectory, normalized);
|
|
32
|
+
const appRequire = createRequire(path.join(appDirectory, 'package.json'));
|
|
33
|
+
try {
|
|
34
|
+
return appRequire.resolve(normalized);
|
|
35
|
+
} catch (_error) {
|
|
36
|
+
return normalized;
|
|
37
|
+
}
|
|
38
|
+
};
|
|
39
|
+
const resolveContractGateSnapshotStore = async (input)=>{
|
|
40
|
+
const { appDirectory, gateSnapshotPath, stateStore, logger } = input;
|
|
41
|
+
if (!stateStore?.module) return createFileContractGateSnapshotStore(gateSnapshotPath);
|
|
42
|
+
const builtinStore = tryResolveBuiltinSnapshotStore({
|
|
43
|
+
stateStore
|
|
44
|
+
});
|
|
45
|
+
if (builtinStore) {
|
|
46
|
+
logger?.info?.(`[telemetry.canary.autopilot] using built-in contract gate snapshot store "${builtinStore.name}"`);
|
|
47
|
+
return builtinStore;
|
|
48
|
+
}
|
|
49
|
+
const modulePath = resolveStoreModulePath(appDirectory, stateStore.module);
|
|
50
|
+
let mod;
|
|
51
|
+
try {
|
|
52
|
+
mod = require(modulePath);
|
|
53
|
+
} catch (error) {
|
|
54
|
+
throw new Error(`[telemetry.canary.autopilot] Failed to load stateStore.module "${stateStore.module}" (${modulePath}): ${error instanceof Error ? error.message : String(error)}`);
|
|
55
|
+
}
|
|
56
|
+
const factory = pickStoreFactory(mod);
|
|
57
|
+
if (!factory) throw new Error(`[telemetry.canary.autopilot] stateStore.module "${stateStore.module}" does not export createContractGateSnapshotStore()`);
|
|
58
|
+
const store = await factory({
|
|
59
|
+
appDirectory,
|
|
60
|
+
gateSnapshotPath,
|
|
61
|
+
options: stateStore.options,
|
|
62
|
+
logger
|
|
63
|
+
});
|
|
64
|
+
ensureStoreShape(store, modulePath);
|
|
65
|
+
logger?.info?.(`[telemetry.canary.autopilot] using contract gate snapshot store "${store.name || modulePath}"`);
|
|
66
|
+
return store;
|
|
67
|
+
};
|
|
68
|
+
export { isRecord, normalizeSnapshot, resolveContractGateSnapshotStore };
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { createFileContractGateSnapshotStore } from "./
|
|
1
|
+
import { createFileContractGateSnapshotStore } from "./contract-gate-snapshot-store/index.mjs";
|
|
2
2
|
const DEFAULT_POLL_INTERVAL_MS = 15000;
|
|
3
3
|
const DEFAULT_GATE_STALE_AFTER_MS = 600000;
|
|
4
4
|
class ContractGateAutopilot {
|
|
@@ -17,10 +17,13 @@ class ContractGateAutopilot {
|
|
|
17
17
|
}
|
|
18
18
|
}
|
|
19
19
|
async syncOnce() {
|
|
20
|
+
const syncGeneration = ++this.syncGeneration;
|
|
20
21
|
const snapshot = await this.loadSnapshot();
|
|
22
|
+
if (syncGeneration !== this.syncGeneration) return 0;
|
|
21
23
|
if (!snapshot) return 0;
|
|
24
|
+
const snapshotGateNames = this.getSnapshotGateNames(snapshot);
|
|
22
25
|
const gates = this.normalizeSnapshot(snapshot);
|
|
23
|
-
let updatedCount = 0;
|
|
26
|
+
let updatedCount = snapshotGateNames ? this.clearOmittedGates(snapshotGateNames) : 0;
|
|
24
27
|
for (const gate of gates){
|
|
25
28
|
this.orchestrator.addRequiredContractGate(gate.name);
|
|
26
29
|
const fingerprint = `${gate.passed ? '1' : '0'}:${gate.reason || ''}`;
|
|
@@ -33,13 +36,30 @@ class ContractGateAutopilot {
|
|
|
33
36
|
}
|
|
34
37
|
return updatedCount;
|
|
35
38
|
}
|
|
39
|
+
getSnapshotGateNames(snapshot) {
|
|
40
|
+
const gates = snapshot.gates;
|
|
41
|
+
if (!gates || 'object' != typeof gates) return;
|
|
42
|
+
const names = new Set();
|
|
43
|
+
for (const name of Object.keys(gates)){
|
|
44
|
+
const normalizedName = name.trim();
|
|
45
|
+
if (normalizedName) names.add(normalizedName);
|
|
46
|
+
}
|
|
47
|
+
return names;
|
|
48
|
+
}
|
|
49
|
+
clearOmittedGates(snapshotGateNames) {
|
|
50
|
+
let updatedCount = 0;
|
|
51
|
+
for (const gateName of Array.from(this.appliedGateFingerprints.keys()))if (!snapshotGateNames.has(gateName)) {
|
|
52
|
+
this.orchestrator.setContractGate(gateName, true);
|
|
53
|
+
this.appliedGateFingerprints.delete(gateName);
|
|
54
|
+
updatedCount += 1;
|
|
55
|
+
this.logger?.info?.(`[telemetry.canary.autopilot] gate=${gateName} passed=true reason=omitted`);
|
|
56
|
+
}
|
|
57
|
+
return updatedCount;
|
|
58
|
+
}
|
|
36
59
|
async loadSnapshot() {
|
|
37
60
|
try {
|
|
38
61
|
const snapshot = await this.gateSnapshotStore.readSnapshot();
|
|
39
62
|
if (!snapshot) return;
|
|
40
|
-
const fingerprint = JSON.stringify(snapshot);
|
|
41
|
-
if (fingerprint === this.lastSnapshotFingerprint) return;
|
|
42
|
-
this.lastSnapshotFingerprint = fingerprint;
|
|
43
63
|
return snapshot;
|
|
44
64
|
} catch (error) {
|
|
45
65
|
const source = this.gateSnapshotPath || this.gateSnapshotStore.name || 'stateStore';
|
|
@@ -111,6 +131,7 @@ class ContractGateAutopilot {
|
|
|
111
131
|
if ('number' == typeof value && Number.isFinite(value) && value > 0) return value;
|
|
112
132
|
}
|
|
113
133
|
constructor(options){
|
|
134
|
+
this.syncGeneration = 0;
|
|
114
135
|
this.appliedGateFingerprints = new Map();
|
|
115
136
|
this.orchestrator = options.orchestrator;
|
|
116
137
|
if (!options.gateSnapshotStore && !options.gateSnapshotPath) throw new Error('ContractGateAutopilot requires gateSnapshotPath or gateSnapshotStore');
|
package/dist/esm/index.mjs
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
|
+
export { CONTRACT_GATE_SNAPSHOT_SCHEMA_VERSION, DEFAULT_CONTRACT_GATE_SNAPSHOT_PATH, createFileContractGateSnapshotStore, createHttpContractGateSnapshotStore, resolveContractGateSnapshotPath, resolveContractGateSnapshotStore } from "./contract-gate-snapshot-store/index.mjs";
|
|
1
2
|
export { ContractGateAutopilot } from "./contractGateAutopilot.mjs";
|
|
2
|
-
export { CONTRACT_GATE_SNAPSHOT_SCHEMA_VERSION, DEFAULT_CONTRACT_GATE_SNAPSHOT_PATH, createFileContractGateSnapshotStore, createHttpContractGateSnapshotStore, resolveContractGateSnapshotPath, resolveContractGateSnapshotStore } from "./contractGateSnapshotStore.mjs";
|
|
3
3
|
export { DEFAULT_ENVIRONMENT_NAME, parseServerRuntimeExtensionsEnv } from "./env.mjs";
|
|
4
4
|
export { getRequestPathname, injectMfAssetCacheHeadersPlugin, isMfManifestAsset, isMfRemoteEntryAsset, resolveMfAssetCacheHeaders } from "./mfCache.mjs";
|
|
5
|
-
export { collectDirectRemoteModuleFederationCss, collectDirectRemoteModuleFederationCssWithMeta, collectModuleFederationManifestCss, createModuleFederationCssCollector, injectModuleFederationCssPlugin } from "./
|
|
5
|
+
export { collectDirectRemoteModuleFederationCss, collectDirectRemoteModuleFederationCssWithMeta, collectModuleFederationManifestCss, createModuleFederationCssCollector, injectModuleFederationCssPlugin } from "./module-federation-css/index.mjs";
|
|
6
6
|
export { DEFAULT_RUNTIME_FALLBACK_SIGNAL_ENDPOINT, DEFAULT_RUNTIME_STATUS_ENDPOINT, TelemetryCanaryOrchestrator, TelemetryRegistry, TelemetryStartupHealthError, createOtlpTelemetryExporter, createRuntimeFallbackSignalRuntimeState, createRuntimeSignalError, createTelemetryAwareMetrics, createVictoriaMetricsTelemetryExporter, enforceRuntimeFallbackSignalAuthToken, enforceRuntimeFallbackSignalTrustPolicy, getRuntimeSignalErrorStatusCode, hasEnabledTelemetryExporters, injectTelemetryPlugin, normalizeRequiredRuntimeFallbackSignalAuthConfig, normalizeRuntimeFallbackSignalAuthConfig, normalizeRuntimeFallbackTrustPolicy, parseRuntimeFallbackSignalPayloadFromRawBody, resolveRuntimeFallbackSignalEndpoint, resolveTelemetrySloOptions } from "./telemetry.mjs";
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
import { collectDirectRemoteModuleFederationCssWithMeta } from "./remote.mjs";
|
|
2
|
+
const createModuleFederationCssCollector = (pwd, options = {})=>{
|
|
3
|
+
const { ttlMs = 0, now = Date.now, ...collectOptions } = options;
|
|
4
|
+
const normalizedTtlMs = Math.max(0, ttlMs);
|
|
5
|
+
let cached;
|
|
6
|
+
let lastGoodAssets;
|
|
7
|
+
let inflight;
|
|
8
|
+
const refresh = (monitors)=>{
|
|
9
|
+
const promise = collectDirectRemoteModuleFederationCssWithMeta(pwd, {
|
|
10
|
+
...collectOptions,
|
|
11
|
+
monitors: monitors ?? collectOptions.monitors
|
|
12
|
+
}).then((result)=>{
|
|
13
|
+
if (!result.errored) {
|
|
14
|
+
lastGoodAssets = result.assets;
|
|
15
|
+
cached = {
|
|
16
|
+
assets: result.assets,
|
|
17
|
+
expiresAt: now() + normalizedTtlMs
|
|
18
|
+
};
|
|
19
|
+
return result.assets;
|
|
20
|
+
}
|
|
21
|
+
cached = void 0;
|
|
22
|
+
return lastGoodAssets ?? result.assets;
|
|
23
|
+
}).finally(()=>{
|
|
24
|
+
if (inflight === promise) inflight = void 0;
|
|
25
|
+
});
|
|
26
|
+
inflight = promise;
|
|
27
|
+
return promise;
|
|
28
|
+
};
|
|
29
|
+
return {
|
|
30
|
+
collect (monitors) {
|
|
31
|
+
if (cached && now() < cached.expiresAt) return Promise.resolve(cached.assets);
|
|
32
|
+
if (inflight) return inflight;
|
|
33
|
+
return refresh(monitors);
|
|
34
|
+
}
|
|
35
|
+
};
|
|
36
|
+
};
|
|
37
|
+
const DEFAULT_REMOTE_CSS_CACHE_TTL_MS = 30000;
|
|
38
|
+
export { DEFAULT_REMOTE_CSS_CACHE_TTL_MS, createModuleFederationCssCollector };
|
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
export { createModuleFederationCssCollector } from "./collector.mjs";
|
|
2
|
+
export { collectModuleFederationManifestCss } from "./manifest.mjs";
|
|
3
|
+
export { injectModuleFederationCssPlugin } from "./plugin.mjs";
|
|
4
|
+
export { collectDirectRemoteModuleFederationCss, collectDirectRemoteModuleFederationCssWithMeta } from "./remote.mjs";
|
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
import { fileReader } from "@modern-js/runtime-utils/fileReader";
|
|
2
|
+
import { fs } from "@modern-js/utils";
|
|
3
|
+
import path from "path";
|
|
4
|
+
const MODULE_FEDERATION_MANIFEST_FILE = 'mf-manifest.json';
|
|
5
|
+
const warn = (monitors, message, ...args)=>{
|
|
6
|
+
if (monitors) return void monitors.warn(message, ...args);
|
|
7
|
+
console.warn(message, ...args);
|
|
8
|
+
};
|
|
9
|
+
const ensureTrailingSlash = (value)=>value.endsWith('/') ? value : `${value}/`;
|
|
10
|
+
const tryResolveUrl = (value, base)=>{
|
|
11
|
+
try {
|
|
12
|
+
return new URL(value, base).toString();
|
|
13
|
+
} catch {
|
|
14
|
+
return;
|
|
15
|
+
}
|
|
16
|
+
};
|
|
17
|
+
const normalizeRemoteEntry = (entry)=>{
|
|
18
|
+
const value = entry.trim();
|
|
19
|
+
if (!value) return;
|
|
20
|
+
const atIndex = value.lastIndexOf('@');
|
|
21
|
+
return atIndex >= 0 ? value.slice(atIndex + 1) : value;
|
|
22
|
+
};
|
|
23
|
+
const getCssAssets = (assets)=>[
|
|
24
|
+
...assets?.css?.sync || [],
|
|
25
|
+
...assets?.css?.async || []
|
|
26
|
+
];
|
|
27
|
+
const getManifestFallbackBase = (manifestUrl)=>{
|
|
28
|
+
try {
|
|
29
|
+
return new URL('.', manifestUrl).toString();
|
|
30
|
+
} catch {
|
|
31
|
+
return ensureTrailingSlash(manifestUrl);
|
|
32
|
+
}
|
|
33
|
+
};
|
|
34
|
+
const getManifestPublicPathBase = (publicPath, manifestUrl)=>{
|
|
35
|
+
if (!publicPath || 'auto' === publicPath) return getManifestFallbackBase(manifestUrl);
|
|
36
|
+
const base = tryResolveUrl(ensureTrailingSlash(publicPath), manifestUrl);
|
|
37
|
+
return base || getManifestFallbackBase(manifestUrl);
|
|
38
|
+
};
|
|
39
|
+
const appendResolvedCssAssets = (result, seen, assets, base)=>{
|
|
40
|
+
for (const asset of assets){
|
|
41
|
+
if (!asset) continue;
|
|
42
|
+
const resolved = tryResolveUrl(asset, base);
|
|
43
|
+
if (resolved && !seen.has(resolved)) {
|
|
44
|
+
seen.add(resolved);
|
|
45
|
+
result.push(resolved);
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
};
|
|
49
|
+
const collectModuleFederationManifestCss = (manifest, manifestUrl)=>{
|
|
50
|
+
const base = getManifestPublicPathBase(manifest.metaData?.publicPath, manifestUrl);
|
|
51
|
+
const result = [];
|
|
52
|
+
const seen = new Set();
|
|
53
|
+
for (const item of manifest.shared || [])appendResolvedCssAssets(result, seen, getCssAssets(item.assets), base);
|
|
54
|
+
for (const item of manifest.exposes || [])appendResolvedCssAssets(result, seen, getCssAssets(item.assets), base);
|
|
55
|
+
return result;
|
|
56
|
+
};
|
|
57
|
+
const getHostManifest = async (pwd, monitors)=>{
|
|
58
|
+
const manifestPath = path.join(pwd, MODULE_FEDERATION_MANIFEST_FILE);
|
|
59
|
+
if (!await fs.pathExists(manifestPath)) return;
|
|
60
|
+
const manifestBuffer = await fileReader.readFileFromSystem(manifestPath, 'buffer');
|
|
61
|
+
if (null === manifestBuffer) return;
|
|
62
|
+
try {
|
|
63
|
+
return JSON.parse(manifestBuffer.toString('utf-8'));
|
|
64
|
+
} catch (error) {
|
|
65
|
+
warn(monitors, 'Parse module federation manifest failed, error = %s', error instanceof Error ? error.message : error);
|
|
66
|
+
return;
|
|
67
|
+
}
|
|
68
|
+
};
|
|
69
|
+
export { appendResolvedCssAssets, collectModuleFederationManifestCss, ensureTrailingSlash, getCssAssets, getHostManifest, getManifestFallbackBase, getManifestPublicPathBase, normalizeRemoteEntry, tryResolveUrl, warn };
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
import { isProd } from "@modern-js/utils";
|
|
2
|
+
import { DEFAULT_REMOTE_CSS_CACHE_TTL_MS, createModuleFederationCssCollector } from "./collector.mjs";
|
|
3
|
+
const injectModuleFederationCssPlugin = (options = {})=>({
|
|
4
|
+
name: '@modern-js/inject-module-federation-css',
|
|
5
|
+
setup (api) {
|
|
6
|
+
api.onPrepare(()=>{
|
|
7
|
+
const { middlewares, distDirectory: pwd } = api.getServerContext();
|
|
8
|
+
if (!pwd) return;
|
|
9
|
+
const ttlMs = Math.max(0, options.remoteCssCacheTtlMs ?? (isProd() ? DEFAULT_REMOTE_CSS_CACHE_TTL_MS : 0));
|
|
10
|
+
const collector = createModuleFederationCssCollector(pwd, {
|
|
11
|
+
ttlMs
|
|
12
|
+
});
|
|
13
|
+
if (isProd()) collector.collect().catch(()=>{});
|
|
14
|
+
const handler = async (c, next)=>{
|
|
15
|
+
const serverManifest = c.get('serverManifest');
|
|
16
|
+
if (serverManifest && !serverManifest.moduleFederationCssAssets) {
|
|
17
|
+
const monitors = c.get('monitors');
|
|
18
|
+
const moduleFederationCssAssets = await collector.collect(monitors);
|
|
19
|
+
c.set('serverManifest', {
|
|
20
|
+
...serverManifest,
|
|
21
|
+
moduleFederationCssAssets
|
|
22
|
+
});
|
|
23
|
+
}
|
|
24
|
+
await next();
|
|
25
|
+
};
|
|
26
|
+
middlewares.push({
|
|
27
|
+
name: 'inject-module-federation-css',
|
|
28
|
+
handler
|
|
29
|
+
});
|
|
30
|
+
});
|
|
31
|
+
}
|
|
32
|
+
});
|
|
33
|
+
export { injectModuleFederationCssPlugin };
|
|
@@ -0,0 +1,92 @@
|
|
|
1
|
+
import { collectModuleFederationManifestCss, getHostManifest, normalizeRemoteEntry, warn } from "./manifest.mjs";
|
|
2
|
+
const DEFAULT_REMOTE_MANIFEST_TIMEOUT = 1500;
|
|
3
|
+
const fetchJsonWithTimeout = async (url, fetcher, timeout)=>{
|
|
4
|
+
const controller = new AbortController();
|
|
5
|
+
let timeoutId;
|
|
6
|
+
try {
|
|
7
|
+
const response = await Promise.race([
|
|
8
|
+
fetcher(url, {
|
|
9
|
+
signal: controller.signal
|
|
10
|
+
}),
|
|
11
|
+
new Promise((_, reject)=>{
|
|
12
|
+
timeoutId = setTimeout(()=>{
|
|
13
|
+
controller.abort();
|
|
14
|
+
reject(new Error(`Request timed out after ${timeout}ms`));
|
|
15
|
+
}, timeout);
|
|
16
|
+
})
|
|
17
|
+
]);
|
|
18
|
+
if (!response.ok) throw new Error(`Unexpected status ${response.status}`);
|
|
19
|
+
return await response.json();
|
|
20
|
+
} finally{
|
|
21
|
+
if (timeoutId) clearTimeout(timeoutId);
|
|
22
|
+
}
|
|
23
|
+
};
|
|
24
|
+
const collectDirectRemoteModuleFederationCssWithMeta = async (pwd, options = {})=>{
|
|
25
|
+
const hostManifest = await getHostManifest(pwd, options.monitors);
|
|
26
|
+
if (!hostManifest) return {
|
|
27
|
+
assets: [],
|
|
28
|
+
errored: false
|
|
29
|
+
};
|
|
30
|
+
const fetcher = options.fetcher || globalThis.fetch?.bind(globalThis);
|
|
31
|
+
if (!fetcher) {
|
|
32
|
+
warn(options.monitors, 'Skip module federation remote CSS collection because fetch is unavailable.');
|
|
33
|
+
return {
|
|
34
|
+
assets: [],
|
|
35
|
+
errored: false
|
|
36
|
+
};
|
|
37
|
+
}
|
|
38
|
+
const timeout = options.timeout ?? DEFAULT_REMOTE_MANIFEST_TIMEOUT;
|
|
39
|
+
const remoteResults = await Promise.all((hostManifest.remotes || []).map(async (remote)=>{
|
|
40
|
+
if (!remote.entry) return {
|
|
41
|
+
assets: [],
|
|
42
|
+
errored: false
|
|
43
|
+
};
|
|
44
|
+
const remoteEntry = normalizeRemoteEntry(remote.entry);
|
|
45
|
+
if (!remoteEntry) return {
|
|
46
|
+
assets: [],
|
|
47
|
+
errored: false
|
|
48
|
+
};
|
|
49
|
+
let remoteManifestUrl;
|
|
50
|
+
try {
|
|
51
|
+
remoteManifestUrl = new URL(remoteEntry).toString();
|
|
52
|
+
} catch {
|
|
53
|
+
warn(options.monitors, 'Skip module federation remote CSS collection for non-absolute manifest URL %s', remoteEntry);
|
|
54
|
+
return {
|
|
55
|
+
assets: [],
|
|
56
|
+
errored: false
|
|
57
|
+
};
|
|
58
|
+
}
|
|
59
|
+
try {
|
|
60
|
+
const remoteManifest = await fetchJsonWithTimeout(remoteManifestUrl, fetcher, timeout);
|
|
61
|
+
return {
|
|
62
|
+
assets: collectModuleFederationManifestCss(remoteManifest, remoteManifestUrl),
|
|
63
|
+
errored: false
|
|
64
|
+
};
|
|
65
|
+
} catch (error) {
|
|
66
|
+
warn(options.monitors, 'Load module federation remote manifest %s failed, error = %s', remoteManifestUrl, error instanceof Error ? error.message : error);
|
|
67
|
+
return {
|
|
68
|
+
assets: [],
|
|
69
|
+
errored: true
|
|
70
|
+
};
|
|
71
|
+
}
|
|
72
|
+
}));
|
|
73
|
+
const cssAssets = [];
|
|
74
|
+
const seen = new Set();
|
|
75
|
+
let errored = false;
|
|
76
|
+
for (const result of remoteResults){
|
|
77
|
+
errored = errored || result.errored;
|
|
78
|
+
for (const asset of result.assets)if (!seen.has(asset)) {
|
|
79
|
+
seen.add(asset);
|
|
80
|
+
cssAssets.push(asset);
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
return {
|
|
84
|
+
assets: cssAssets,
|
|
85
|
+
errored
|
|
86
|
+
};
|
|
87
|
+
};
|
|
88
|
+
const collectDirectRemoteModuleFederationCss = async (pwd, options = {})=>{
|
|
89
|
+
const { assets } = await collectDirectRemoteModuleFederationCssWithMeta(pwd, options);
|
|
90
|
+
return assets;
|
|
91
|
+
};
|
|
92
|
+
export { collectDirectRemoteModuleFederationCss, collectDirectRemoteModuleFederationCssWithMeta };
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
import { createHash, timingSafeEqual } from "node:crypto";
|
|
2
|
+
import { DEFAULT_RUNTIME_FALLBACK_AUTH_HEADER } from "./constants.mjs";
|
|
3
|
+
import { createRuntimeSignalError } from "./errors.mjs";
|
|
4
|
+
function normalizeRuntimeFallbackSignalAuthConfig(configured) {
|
|
5
|
+
const headerName = 'string' == typeof configured?.headerName && configured.headerName.trim() ? configured.headerName.trim().toLowerCase() : DEFAULT_RUNTIME_FALLBACK_AUTH_HEADER;
|
|
6
|
+
const expectedFromEnv = 'string' == typeof configured?.expectedValueEnv && configured.expectedValueEnv.trim().length > 0 ? process.env[configured.expectedValueEnv.trim()] : void 0;
|
|
7
|
+
const expectedFromConfig = 'string' == typeof configured?.expectedValue && configured.expectedValue.trim().length > 0 ? configured.expectedValue.trim() : void 0;
|
|
8
|
+
const expectedValue = expectedFromConfig || expectedFromEnv;
|
|
9
|
+
const enabled = configured?.enabled === true;
|
|
10
|
+
if (enabled && !expectedValue) throw new Error('[telemetry.canary.autopilot.runtimeFallbackSignal] auth.enabled is true but no expected token is configured');
|
|
11
|
+
return {
|
|
12
|
+
enabled,
|
|
13
|
+
headerName,
|
|
14
|
+
expectedValue
|
|
15
|
+
};
|
|
16
|
+
}
|
|
17
|
+
function normalizeRequiredRuntimeFallbackSignalAuthConfig(configured) {
|
|
18
|
+
if (configured?.enabled === false) throw new Error('[telemetry.canary.autopilot.runtimeFallbackSignal] the endpoint cannot be enabled with auth disabled; configure auth.expectedValue or auth.expectedValueEnv');
|
|
19
|
+
try {
|
|
20
|
+
return normalizeRuntimeFallbackSignalAuthConfig({
|
|
21
|
+
...configured,
|
|
22
|
+
enabled: true
|
|
23
|
+
});
|
|
24
|
+
} catch (_error) {
|
|
25
|
+
throw new Error('[telemetry.canary.autopilot.runtimeFallbackSignal] enabling the endpoint requires an auth token; configure auth.expectedValue or auth.expectedValueEnv');
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
function safeTokenEquals(candidate, expected) {
|
|
29
|
+
const candidateDigest = createHash('sha256').update(candidate).digest();
|
|
30
|
+
const expectedDigest = createHash('sha256').update(expected).digest();
|
|
31
|
+
return timingSafeEqual(candidateDigest, expectedDigest);
|
|
32
|
+
}
|
|
33
|
+
function enforceRuntimeFallbackSignalAuthToken(token, authConfig) {
|
|
34
|
+
if (!authConfig.enabled) return;
|
|
35
|
+
if (!token || !authConfig.expectedValue || !safeTokenEquals(token, authConfig.expectedValue)) throw createRuntimeSignalError('runtime fallback signal auth failed', 'UNAUTHORIZED');
|
|
36
|
+
}
|
|
37
|
+
function enforceRuntimeFallbackSignalAuth(c, runtimeSignalConfig) {
|
|
38
|
+
enforceRuntimeFallbackSignalAuthToken(c.req.header(runtimeSignalConfig.auth.headerName), runtimeSignalConfig.auth);
|
|
39
|
+
}
|
|
40
|
+
export { enforceRuntimeFallbackSignalAuth, enforceRuntimeFallbackSignalAuthToken, normalizeRequiredRuntimeFallbackSignalAuthConfig, normalizeRuntimeFallbackSignalAuthConfig };
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
const DEFAULT_RUNTIME_FALLBACK_SIGNAL_ENDPOINT = '/_modern/contract-gates/runtime-fallback';
|
|
2
|
+
const DEFAULT_RUNTIME_STATUS_ENDPOINT = '/_modern/runtime/status';
|
|
3
|
+
const DEFAULT_RUNTIME_FALLBACK_GATE_NAME = 'runtime-mf-fallback-health';
|
|
4
|
+
const DEFAULT_RUNTIME_FALLBACK_FAILURE_HOLD_MS = 300000;
|
|
5
|
+
const DEFAULT_RUNTIME_FALLBACK_MAX_BODY_BYTES = 16384;
|
|
6
|
+
const DEFAULT_RUNTIME_FALLBACK_AUTH_HEADER = 'x-modernjs-runtime-signal-token';
|
|
7
|
+
const DEFAULT_RUNTIME_FALLBACK_TRUST_MAX_SIGNALS_PER_WINDOW = 30;
|
|
8
|
+
const DEFAULT_RUNTIME_FALLBACK_TRUST_WINDOW_MS = 60000;
|
|
9
|
+
const DEFAULT_RUNTIME_FALLBACK_TRUST_DEDUPE_WINDOW_MS = 10000;
|
|
10
|
+
export { DEFAULT_RUNTIME_FALLBACK_AUTH_HEADER, DEFAULT_RUNTIME_FALLBACK_FAILURE_HOLD_MS, DEFAULT_RUNTIME_FALLBACK_GATE_NAME, DEFAULT_RUNTIME_FALLBACK_MAX_BODY_BYTES, DEFAULT_RUNTIME_FALLBACK_SIGNAL_ENDPOINT, DEFAULT_RUNTIME_FALLBACK_TRUST_DEDUPE_WINDOW_MS, DEFAULT_RUNTIME_FALLBACK_TRUST_MAX_SIGNALS_PER_WINDOW, DEFAULT_RUNTIME_FALLBACK_TRUST_WINDOW_MS, DEFAULT_RUNTIME_STATUS_ENDPOINT };
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import { CONTRACT_GATE_SNAPSHOT_SCHEMA_VERSION } from "../contract-gate-snapshot-store/index.mjs";
|
|
2
|
+
async function persistRuntimeFallbackContractGate(payload, runtimeSignalConfig) {
|
|
3
|
+
const now = Date.now();
|
|
4
|
+
const gateSnapshotStore = await runtimeSignalConfig.gateSnapshotStore;
|
|
5
|
+
const snapshot = await gateSnapshotStore.readSnapshot() || {};
|
|
6
|
+
const existingGates = snapshot.gates && 'object' == typeof snapshot.gates ? snapshot.gates : {};
|
|
7
|
+
const reason = 'string' == typeof payload.reason ? payload.reason : 'runtime_fallback';
|
|
8
|
+
const phase = 'string' == typeof payload.phase ? payload.phase : 'unknown';
|
|
9
|
+
const appName = 'string' == typeof payload.appName ? payload.appName : 'unknown';
|
|
10
|
+
const entry = 'string' == typeof payload.entry ? payload.entry : void 0;
|
|
11
|
+
snapshot.schemaVersion = 'number' == typeof snapshot.schemaVersion ? snapshot.schemaVersion : CONTRACT_GATE_SNAPSHOT_SCHEMA_VERSION;
|
|
12
|
+
snapshot.updatedAt = now;
|
|
13
|
+
snapshot.gates = {
|
|
14
|
+
...existingGates,
|
|
15
|
+
[runtimeSignalConfig.gateName]: {
|
|
16
|
+
passed: false,
|
|
17
|
+
reason: `runtime_fallback:${reason} phase=${phase} app=${appName}${entry ? ` entry=${entry}` : ''}`,
|
|
18
|
+
updatedAt: now,
|
|
19
|
+
expiresAt: now + runtimeSignalConfig.failureHoldMs,
|
|
20
|
+
source: 'runtime-mf-fallback-signal',
|
|
21
|
+
metadata: payload
|
|
22
|
+
}
|
|
23
|
+
};
|
|
24
|
+
await gateSnapshotStore.writeSnapshot(snapshot);
|
|
25
|
+
}
|
|
26
|
+
export { persistRuntimeFallbackContractGate };
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { DEFAULT_RUNTIME_FALLBACK_SIGNAL_ENDPOINT } from "./constants.mjs";
|
|
2
|
+
function resolveRuntimeFallbackSignalEndpoint(configuredEndpoint) {
|
|
3
|
+
const rawEndpoint = configuredEndpoint?.trim();
|
|
4
|
+
if (!rawEndpoint) return DEFAULT_RUNTIME_FALLBACK_SIGNAL_ENDPOINT;
|
|
5
|
+
if (rawEndpoint.startsWith('/')) return rawEndpoint;
|
|
6
|
+
try {
|
|
7
|
+
return new URL(rawEndpoint).pathname || DEFAULT_RUNTIME_FALLBACK_SIGNAL_ENDPOINT;
|
|
8
|
+
} catch (_error) {
|
|
9
|
+
return `/${rawEndpoint.replace(/^\/+/, '')}`;
|
|
10
|
+
}
|
|
11
|
+
}
|
|
12
|
+
export { resolveRuntimeFallbackSignalEndpoint };
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
export { enforceRuntimeFallbackSignalAuth, enforceRuntimeFallbackSignalAuthToken, normalizeRequiredRuntimeFallbackSignalAuthConfig, normalizeRuntimeFallbackSignalAuthConfig } from "./auth.mjs";
|
|
2
|
+
export { DEFAULT_RUNTIME_FALLBACK_FAILURE_HOLD_MS, DEFAULT_RUNTIME_FALLBACK_GATE_NAME, DEFAULT_RUNTIME_FALLBACK_MAX_BODY_BYTES, DEFAULT_RUNTIME_FALLBACK_SIGNAL_ENDPOINT, DEFAULT_RUNTIME_STATUS_ENDPOINT } from "./constants.mjs";
|
|
3
|
+
export { persistRuntimeFallbackContractGate } from "./contract-gate.mjs";
|
|
4
|
+
export { resolveRuntimeFallbackSignalEndpoint } from "./endpoint.mjs";
|
|
5
|
+
export { createRuntimeSignalError } from "./errors.mjs";
|
|
6
|
+
export { parseRuntimeFallbackSignalPayload, parseRuntimeFallbackSignalPayloadFromRawBody } from "./payload.mjs";
|
|
7
|
+
export { createRuntimeFallbackSignalRuntimeState } from "./state.mjs";
|
|
8
|
+
export { getRuntimeSignalErrorStatusCode } from "./status.mjs";
|
|
9
|
+
export { enforceRuntimeFallbackSignalTrustPolicy, normalizeRuntimeFallbackTrustPolicy } from "./trust-policy.mjs";
|