@bleedingdev/modern-js-server-runtime-extensions 3.9.0-ultramodern.2 → 3.9.0-ultramodern.21
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/README.md +8 -6
- package/dist/cjs/backend-federation-security/index.js +73 -152
- package/dist/cjs/bff-policy/crossProjectPolicy.js +299 -0
- package/dist/cjs/bff-policy/index.js +69 -0
- package/dist/cjs/bff-policy/node.js +85 -0
- package/dist/cjs/bff-policy/operationContracts.js +197 -0
- package/dist/cjs/bff-policy/operationIdentity.js +81 -0
- package/dist/cjs/bff-policy/producer.js +79 -0
- package/dist/cjs/bff-policy/resolveCrossProjectPolicy.js +65 -0
- package/dist/cjs/contract-gate-snapshot-store/http-store.js +1 -8
- package/dist/cjs/errorResponsePlugin.js +57 -0
- package/dist/cjs/localisedLoader.js +65 -0
- package/dist/cjs/localisedLoaderPlugin.js +62 -0
- package/dist/cjs/mfCache.js +6 -0
- package/dist/cjs/module-federation-css/plugin.js +6 -0
- package/dist/cjs/module-federation-css/remote.js +2 -1
- package/dist/cjs/serverConfig.js +18 -0
- package/dist/cjs/serverPlugin.js +74 -0
- package/dist/cjs/static-serving/plugin.js +57 -0
- package/dist/cjs/static-serving/responders.js +158 -0
- package/dist/cjs/static-serving/staticModuleFederation.js +173 -0
- package/dist/cjs/static-serving/staticPrecompressed.js +152 -0
- package/dist/cjs/telemetry/exporters.js +4 -4
- package/dist/cjs/telemetry/healthSetup.js +14 -14
- package/dist/cjs/telemetry/lifecycle.js +20 -21
- package/dist/cjs/telemetry/plugin.js +10 -4
- package/dist/cjs/telemetry/runtimeEndpoints.js +3 -2
- package/dist/cjs/telemetryCore.js +5 -5
- package/dist/esm/backend-federation-security/index.mjs +73 -152
- package/dist/esm/bff-policy/crossProjectPolicy.mjs +249 -0
- package/dist/esm/bff-policy/index.mjs +2 -0
- package/dist/esm/bff-policy/operationIdentity.mjs +30 -0
- package/dist/esm/bff-policy/producer.mjs +31 -0
- package/dist/esm/contract-gate-snapshot-store/http-store.mjs +1 -8
- package/dist/esm/errorResponsePlugin.mjs +19 -0
- package/dist/esm/localisedLoader.mjs +24 -0
- package/dist/esm/localisedLoaderPlugin.mjs +24 -0
- package/dist/esm/mfCache.mjs +6 -0
- package/dist/esm/module-federation-css/plugin.mjs +6 -0
- package/dist/esm/module-federation-css/remote.mjs +2 -1
- package/dist/esm/serverConfig.mjs +0 -0
- package/dist/esm/serverPlugin.mjs +34 -0
- package/dist/esm/telemetry/exporters.mjs +2 -2
- package/dist/esm/telemetry/healthSetup.mjs +14 -14
- package/dist/esm/telemetry/lifecycle.mjs +21 -22
- package/dist/esm/telemetry/plugin.mjs +9 -3
- package/dist/esm/telemetry/runtimeEndpoints.mjs +3 -2
- package/dist/esm/telemetryCore.mjs +1 -1
- package/dist/esm-node/backend-federation-security/index.mjs +73 -152
- package/dist/esm-node/bff-policy/crossProjectPolicy.mjs +250 -0
- package/dist/esm-node/bff-policy/index.mjs +3 -0
- package/dist/esm-node/bff-policy/node.mjs +4 -0
- package/dist/esm-node/bff-policy/operationContracts.mjs +107 -0
- package/dist/esm-node/bff-policy/operationIdentity.mjs +31 -0
- package/dist/esm-node/bff-policy/producer.mjs +32 -0
- package/dist/esm-node/bff-policy/resolveCrossProjectPolicy.mjs +28 -0
- package/dist/esm-node/contract-gate-snapshot-store/http-store.mjs +1 -8
- package/dist/esm-node/errorResponsePlugin.mjs +20 -0
- package/dist/esm-node/localisedLoader.mjs +25 -0
- package/dist/esm-node/localisedLoaderPlugin.mjs +25 -0
- package/dist/esm-node/mfCache.mjs +6 -0
- package/dist/esm-node/module-federation-css/plugin.mjs +6 -0
- package/dist/esm-node/module-federation-css/remote.mjs +2 -1
- package/dist/esm-node/serverConfig.mjs +1 -0
- package/dist/esm-node/serverPlugin.mjs +35 -0
- package/dist/esm-node/static-serving/plugin.mjs +18 -0
- package/dist/esm-node/static-serving/responders.mjs +111 -0
- package/dist/esm-node/static-serving/staticModuleFederation.mjs +105 -0
- package/dist/esm-node/static-serving/staticPrecompressed.mjs +112 -0
- package/dist/esm-node/telemetry/exporters.mjs +2 -2
- package/dist/esm-node/telemetry/healthSetup.mjs +14 -14
- package/dist/esm-node/telemetry/lifecycle.mjs +21 -22
- package/dist/esm-node/telemetry/plugin.mjs +9 -3
- package/dist/esm-node/telemetry/runtimeEndpoints.mjs +3 -2
- package/dist/esm-node/telemetryCore.mjs +1 -1
- package/dist/types/backend-federation-security/node.d.ts +1 -1
- package/dist/types/bff-policy/crossProjectPolicy.d.ts +101 -0
- package/dist/types/bff-policy/index.d.ts +2 -0
- package/dist/types/bff-policy/node.d.ts +3 -0
- package/dist/types/bff-policy/operationContracts.d.ts +46 -0
- package/dist/types/bff-policy/operationIdentity.d.ts +34 -0
- package/dist/types/bff-policy/producer.d.ts +10 -0
- package/dist/types/bff-policy/resolveCrossProjectPolicy.d.ts +39 -0
- package/dist/types/errorResponsePlugin.d.ts +3 -0
- package/dist/types/index.d.ts +1 -0
- package/dist/types/localisedLoader.d.ts +15 -0
- package/dist/types/localisedLoaderPlugin.d.ts +2 -0
- package/dist/types/mfCache.d.ts +2 -3
- package/dist/types/module-federation-css/plugin.d.ts +2 -3
- package/dist/types/module-federation-css/remote.d.ts +2 -0
- package/dist/types/serverConfig.d.ts +17 -0
- package/dist/types/serverPlugin.d.ts +5 -0
- package/dist/types/static-serving/plugin.d.ts +3 -0
- package/dist/types/static-serving/responders.d.ts +2 -0
- package/dist/types/static-serving/staticModuleFederation.d.ts +13 -0
- package/dist/types/static-serving/staticPrecompressed.d.ts +13 -0
- package/dist/types/telemetry/exporters.d.ts +1 -1
- package/dist/types/telemetry/healthSetup.d.ts +3 -3
- package/dist/types/telemetry/lifecycle.d.ts +4 -4
- package/dist/types/telemetry/plugin.d.ts +2 -1
- package/dist/types/telemetryCore.d.ts +1 -1
- package/dist/types-esm/backend-federation-security/index.d.mts +68 -0
- package/dist/types-esm/backend-federation-security/node.d.mts +3 -0
- package/dist/types-esm/bff-policy/crossProjectPolicy.d.mts +101 -0
- package/dist/types-esm/bff-policy/index.d.mts +2 -0
- package/dist/types-esm/bff-policy/node.d.mts +3 -0
- package/dist/types-esm/bff-policy/operationContracts.d.mts +46 -0
- package/dist/types-esm/bff-policy/operationIdentity.d.mts +34 -0
- package/dist/types-esm/bff-policy/producer.d.mts +10 -0
- package/dist/types-esm/bff-policy/resolveCrossProjectPolicy.d.mts +39 -0
- package/dist/types-esm/contract-gate-snapshot-normalization.d.mts +15 -0
- package/dist/types-esm/contract-gate-snapshot-store/file-store.d.mts +3 -0
- package/dist/types-esm/contract-gate-snapshot-store/http-store.d.mts +5 -0
- package/dist/types-esm/contract-gate-snapshot-store/index.d.mts +5 -0
- package/dist/types-esm/contract-gate-snapshot-store/resolve.d.mts +9 -0
- package/dist/types-esm/contract-gate-snapshot-store/types.d.mts +47 -0
- package/dist/types-esm/contractGateSnapshotObserver.d.mts +33 -0
- package/dist/types-esm/env.d.mts +40 -0
- package/dist/types-esm/errorResponsePlugin.d.mts +3 -0
- package/dist/types-esm/index.d.mts +8 -0
- package/dist/types-esm/localisedLoader.d.mts +15 -0
- package/dist/types-esm/localisedLoaderPlugin.d.mts +2 -0
- package/dist/types-esm/mfCache.d.mts +26 -0
- package/dist/types-esm/module-federation-css/collector.d.mts +25 -0
- package/dist/types-esm/module-federation-css/index.d.mts +8 -0
- package/dist/types-esm/module-federation-css/manifest.d.mts +28 -0
- package/dist/types-esm/module-federation-css/plugin.d.mts +22 -0
- package/dist/types-esm/module-federation-css/remote.d.mts +22 -0
- package/dist/types-esm/runtime-fallback-signal/index.d.mts +4 -0
- package/dist/types-esm/runtime-fallback-signal/model.d.mts +70 -0
- package/dist/types-esm/runtime-fallback-signal/resolver.d.mts +29 -0
- package/dist/types-esm/runtime-fallback-signal/store.d.mts +4 -0
- package/dist/types-esm/runtimeFallbackSignal.d.mts +1 -0
- package/dist/types-esm/runtimeLifecycle.d.mts +10 -0
- package/dist/types-esm/serverConfig.d.mts +17 -0
- package/dist/types-esm/serverPlugin.d.mts +5 -0
- package/dist/types-esm/static-serving/plugin.d.mts +3 -0
- package/dist/types-esm/static-serving/responders.d.mts +2 -0
- package/dist/types-esm/static-serving/staticModuleFederation.d.mts +13 -0
- package/dist/types-esm/static-serving/staticPrecompressed.d.mts +13 -0
- package/dist/types-esm/telemetry/envelope.d.mts +55 -0
- package/dist/types-esm/telemetry/exporters.d.mts +12 -0
- package/dist/types-esm/telemetry/healthEvaluation.d.mts +20 -0
- package/dist/types-esm/telemetry/healthMonitor.d.mts +88 -0
- package/dist/types-esm/telemetry/healthSetup.d.mts +17 -0
- package/dist/types-esm/telemetry/lifecycle.d.mts +18 -0
- package/dist/types-esm/telemetry/metrics.d.mts +3 -0
- package/dist/types-esm/telemetry/plugin.d.mts +13 -0
- package/dist/types-esm/telemetry/registry.d.mts +69 -0
- package/dist/types-esm/telemetry/registryTypes.d.mts +50 -0
- package/dist/types-esm/telemetry/runtimeEndpoints.d.mts +35 -0
- package/dist/types-esm/telemetry.d.mts +1 -0
- package/dist/types-esm/telemetryCore.d.mts +6 -0
- package/package.json +183 -29
- package/rslib.config.mts +36 -2
- package/rstest.config.mts +11 -0
- package/src/backend-federation-security/index.ts +151 -318
- package/src/backend-federation-security/node.ts +24 -22
- package/src/bff-policy/crossProjectPolicy.ts +754 -0
- package/src/bff-policy/index.ts +2 -0
- package/src/bff-policy/node.ts +3 -0
- package/src/bff-policy/operationContracts.ts +225 -0
- package/src/bff-policy/operationIdentity.ts +90 -0
- package/src/bff-policy/producer.ts +40 -0
- package/src/bff-policy/resolveCrossProjectPolicy.ts +88 -0
- package/src/contract-gate-snapshot-store/http-store.ts +1 -11
- package/src/contract-gate-snapshot-store/resolve.ts +0 -1
- package/src/errorResponsePlugin.ts +21 -0
- package/src/index.ts +4 -0
- package/src/localisedLoader.ts +50 -0
- package/src/localisedLoaderPlugin.ts +33 -0
- package/src/mfCache.ts +4 -3
- package/src/module-federation-css/plugin.ts +4 -3
- package/src/module-federation-css/remote.ts +3 -1
- package/src/serverConfig.ts +32 -0
- package/src/serverPlugin.ts +38 -0
- package/src/static-serving/plugin.ts +16 -0
- package/src/static-serving/responders.ts +208 -0
- package/src/static-serving/staticModuleFederation.ts +255 -0
- package/src/static-serving/staticPrecompressed.ts +193 -0
- package/src/telemetry/exporters.ts +1 -1
- package/src/telemetry/healthSetup.ts +17 -20
- package/src/telemetry/lifecycle.ts +27 -39
- package/src/telemetry/plugin.ts +6 -4
- package/src/telemetry/runtimeEndpoints.ts +3 -2
- package/src/telemetryCore.ts +1 -1
- package/tests/backendFederationSecurity.test.ts +63 -0
- package/tests/bff-policy/crossProjectPolicy.matrix.test.ts +450 -0
- package/tests/bff-policy/crossProjectPolicy.test.ts +197 -0
- package/tests/bff-policy/operationContracts.test.ts +278 -0
- package/tests/bff-policy/optionalZodPeer.test.ts +30 -0
- package/tests/bff-policy/resolveCrossProjectPolicy.test.ts +76 -0
- package/tests/bff-policy/zodEvaluatorProbe.test.ts +49 -0
- package/tests/contractGateSnapshotObserver.test.ts +0 -58
- package/tests/contractGateSnapshotStore.test.ts +0 -51
- package/tests/env.test.ts +45 -64
- package/tests/localisedLoader.test.ts +155 -0
- package/tests/mfCache.test.ts +1 -88
- package/tests/moduleFederationCss.test.ts +98 -41
- package/tests/nativeErrorResponse.test.ts +118 -0
- package/tests/nativeServerAssembly.test.ts +385 -0
- package/tests/runtimeLifecycle.test.ts +69 -17
- package/tests/static-serving/ordering.test.ts +214 -0
- package/tests/static-serving/static.test.ts +411 -0
- package/tests/telemetry.test.ts +35 -198
- package/tests/telemetryHealthMonitor.test.ts +0 -4
- package/tests/telemetryHealthObservation.test.ts +302 -0
- package/tests/telemetryLifecycle.test.ts +7 -303
- package/tests/telemetryRegistryQueueMatrix.test.ts +14 -111
- package/tests/registration.test.ts +0 -112
- package/tests/telemetryAutopilot.test.ts +0 -1134
|
@@ -1,1134 +0,0 @@
|
|
|
1
|
-
import { createDefaultPlugins, createServerBase } from '@modern-js/server-core';
|
|
2
|
-
import fs from 'fs';
|
|
3
|
-
import os from 'os';
|
|
4
|
-
import path from 'path';
|
|
5
|
-
import { injectTelemetryPlugin } from '../src/telemetry';
|
|
6
|
-
import { getDefaultAppContext, getDefaultConfig } from './helpers';
|
|
7
|
-
|
|
8
|
-
const makeTempDir = () =>
|
|
9
|
-
fs.mkdtempSync(path.join(os.tmpdir(), 'modern-telemetry-autopilot-'));
|
|
10
|
-
|
|
11
|
-
describe('telemetry autopilot runtime signal', () => {
|
|
12
|
-
test('accepts runtime fallback signal and persists gate snapshot', async () => {
|
|
13
|
-
const tempDir = makeTempDir();
|
|
14
|
-
const snapshotPath = path.join(tempDir, '.modern/contract-gates.json');
|
|
15
|
-
|
|
16
|
-
try {
|
|
17
|
-
const config = getDefaultConfig();
|
|
18
|
-
config.server = {
|
|
19
|
-
telemetry: {
|
|
20
|
-
enabled: true,
|
|
21
|
-
canary: {
|
|
22
|
-
enabled: true,
|
|
23
|
-
rollbackConsecutiveFailures: 1,
|
|
24
|
-
autopilot: {
|
|
25
|
-
enabled: true,
|
|
26
|
-
gateSnapshotPath: snapshotPath,
|
|
27
|
-
pollIntervalMs: 20,
|
|
28
|
-
gateStaleAfterMs: 60_000,
|
|
29
|
-
runtimeFallbackSignal: {
|
|
30
|
-
enabled: true,
|
|
31
|
-
endpoint: '/_modern/contract-gates/runtime-fallback',
|
|
32
|
-
gateName: 'runtime-mf-fallback-health',
|
|
33
|
-
failureHoldMs: 5_000,
|
|
34
|
-
maxBodyBytes: 4_096,
|
|
35
|
-
auth: {
|
|
36
|
-
expectedValue: 'accept-signal-token',
|
|
37
|
-
},
|
|
38
|
-
},
|
|
39
|
-
},
|
|
40
|
-
},
|
|
41
|
-
},
|
|
42
|
-
} as any;
|
|
43
|
-
|
|
44
|
-
const server = createServerBase({
|
|
45
|
-
config,
|
|
46
|
-
pwd: tempDir,
|
|
47
|
-
appContext: getDefaultAppContext(),
|
|
48
|
-
});
|
|
49
|
-
|
|
50
|
-
server.addPlugins([
|
|
51
|
-
...createDefaultPlugins({
|
|
52
|
-
logger: false,
|
|
53
|
-
}),
|
|
54
|
-
injectTelemetryPlugin(),
|
|
55
|
-
]);
|
|
56
|
-
|
|
57
|
-
await server.init();
|
|
58
|
-
|
|
59
|
-
const response = await server.request(
|
|
60
|
-
'/_modern/contract-gates/runtime-fallback',
|
|
61
|
-
{
|
|
62
|
-
method: 'POST',
|
|
63
|
-
headers: new Headers({
|
|
64
|
-
'content-type': 'application/json',
|
|
65
|
-
'x-modernjs-runtime-signal-token': 'accept-signal-token',
|
|
66
|
-
}),
|
|
67
|
-
body: JSON.stringify({
|
|
68
|
-
reason: 'remote_load_failed',
|
|
69
|
-
phase: 'load',
|
|
70
|
-
appName: 'dashboard',
|
|
71
|
-
entry: 'https://remote.example.com/remoteEntry.js',
|
|
72
|
-
}),
|
|
73
|
-
},
|
|
74
|
-
{},
|
|
75
|
-
);
|
|
76
|
-
|
|
77
|
-
expect(response.status).toBe(202);
|
|
78
|
-
expect(fs.existsSync(snapshotPath)).toBe(true);
|
|
79
|
-
|
|
80
|
-
const snapshot = JSON.parse(fs.readFileSync(snapshotPath, 'utf8')) as {
|
|
81
|
-
gates?: Record<string, any>;
|
|
82
|
-
};
|
|
83
|
-
const gate = snapshot.gates?.['runtime-mf-fallback-health'];
|
|
84
|
-
expect(gate).toBeTruthy();
|
|
85
|
-
expect(gate.passed).toBe(false);
|
|
86
|
-
expect(String(gate.reason)).toContain(
|
|
87
|
-
'runtime_fallback:remote_load_failed',
|
|
88
|
-
);
|
|
89
|
-
expect(typeof gate.expiresAt).toBe('number');
|
|
90
|
-
expect(gate.expiresAt).toBeGreaterThan(Date.now());
|
|
91
|
-
} finally {
|
|
92
|
-
fs.rmSync(tempDir, { recursive: true, force: true });
|
|
93
|
-
}
|
|
94
|
-
});
|
|
95
|
-
|
|
96
|
-
test('rejects oversized runtime fallback signal payload', async () => {
|
|
97
|
-
const tempDir = makeTempDir();
|
|
98
|
-
const snapshotPath = path.join(tempDir, '.modern/contract-gates.json');
|
|
99
|
-
|
|
100
|
-
try {
|
|
101
|
-
const config = getDefaultConfig();
|
|
102
|
-
config.server = {
|
|
103
|
-
telemetry: {
|
|
104
|
-
enabled: true,
|
|
105
|
-
canary: {
|
|
106
|
-
enabled: true,
|
|
107
|
-
rollbackConsecutiveFailures: 1,
|
|
108
|
-
autopilot: {
|
|
109
|
-
enabled: true,
|
|
110
|
-
gateSnapshotPath: snapshotPath,
|
|
111
|
-
runtimeFallbackSignal: {
|
|
112
|
-
enabled: true,
|
|
113
|
-
endpoint: '/_modern/contract-gates/runtime-fallback',
|
|
114
|
-
maxBodyBytes: 16,
|
|
115
|
-
auth: {
|
|
116
|
-
expectedValue: 'oversize-signal-token',
|
|
117
|
-
},
|
|
118
|
-
},
|
|
119
|
-
},
|
|
120
|
-
},
|
|
121
|
-
},
|
|
122
|
-
} as any;
|
|
123
|
-
|
|
124
|
-
const server = createServerBase({
|
|
125
|
-
config,
|
|
126
|
-
pwd: tempDir,
|
|
127
|
-
appContext: getDefaultAppContext(),
|
|
128
|
-
});
|
|
129
|
-
server.addPlugins([
|
|
130
|
-
...createDefaultPlugins({
|
|
131
|
-
logger: false,
|
|
132
|
-
}),
|
|
133
|
-
injectTelemetryPlugin(),
|
|
134
|
-
]);
|
|
135
|
-
|
|
136
|
-
await server.init();
|
|
137
|
-
|
|
138
|
-
const response = await server.request(
|
|
139
|
-
'/_modern/contract-gates/runtime-fallback',
|
|
140
|
-
{
|
|
141
|
-
method: 'POST',
|
|
142
|
-
headers: new Headers({
|
|
143
|
-
'content-type': 'application/json',
|
|
144
|
-
'x-modernjs-runtime-signal-token': 'oversize-signal-token',
|
|
145
|
-
}),
|
|
146
|
-
body: JSON.stringify({
|
|
147
|
-
reason: 'x'.repeat(2_048),
|
|
148
|
-
}),
|
|
149
|
-
},
|
|
150
|
-
{},
|
|
151
|
-
);
|
|
152
|
-
|
|
153
|
-
expect(response.status).toBe(413);
|
|
154
|
-
const payload = (await response.json()) as {
|
|
155
|
-
ok?: boolean;
|
|
156
|
-
error?: string;
|
|
157
|
-
};
|
|
158
|
-
expect(payload.ok).toBe(false);
|
|
159
|
-
expect(String(payload.error)).toContain('payload too large');
|
|
160
|
-
} finally {
|
|
161
|
-
fs.rmSync(tempDir, { recursive: true, force: true });
|
|
162
|
-
}
|
|
163
|
-
});
|
|
164
|
-
|
|
165
|
-
test('requires runtime fallback auth token when configured', async () => {
|
|
166
|
-
const tempDir = makeTempDir();
|
|
167
|
-
const snapshotPath = path.join(tempDir, '.modern/contract-gates.json');
|
|
168
|
-
|
|
169
|
-
try {
|
|
170
|
-
const config = getDefaultConfig();
|
|
171
|
-
config.server = {
|
|
172
|
-
telemetry: {
|
|
173
|
-
enabled: true,
|
|
174
|
-
canary: {
|
|
175
|
-
enabled: true,
|
|
176
|
-
rollbackConsecutiveFailures: 1,
|
|
177
|
-
autopilot: {
|
|
178
|
-
enabled: true,
|
|
179
|
-
gateSnapshotPath: snapshotPath,
|
|
180
|
-
runtimeFallbackSignal: {
|
|
181
|
-
enabled: true,
|
|
182
|
-
endpoint: '/_modern/contract-gates/runtime-fallback',
|
|
183
|
-
auth: {
|
|
184
|
-
enabled: true,
|
|
185
|
-
headerName: 'x-modernjs-runtime-signal-token',
|
|
186
|
-
expectedValue: 'top-secret-token',
|
|
187
|
-
},
|
|
188
|
-
},
|
|
189
|
-
},
|
|
190
|
-
},
|
|
191
|
-
},
|
|
192
|
-
} as any;
|
|
193
|
-
|
|
194
|
-
const server = createServerBase({
|
|
195
|
-
config,
|
|
196
|
-
pwd: tempDir,
|
|
197
|
-
appContext: getDefaultAppContext(),
|
|
198
|
-
});
|
|
199
|
-
server.addPlugins([
|
|
200
|
-
...createDefaultPlugins({
|
|
201
|
-
logger: false,
|
|
202
|
-
}),
|
|
203
|
-
injectTelemetryPlugin(),
|
|
204
|
-
]);
|
|
205
|
-
|
|
206
|
-
await server.init();
|
|
207
|
-
|
|
208
|
-
const rejected = await server.request(
|
|
209
|
-
'/_modern/contract-gates/runtime-fallback',
|
|
210
|
-
{
|
|
211
|
-
method: 'POST',
|
|
212
|
-
headers: new Headers({
|
|
213
|
-
'content-type': 'application/json',
|
|
214
|
-
}),
|
|
215
|
-
body: JSON.stringify({
|
|
216
|
-
reason: 'remote_load_failed',
|
|
217
|
-
phase: 'load',
|
|
218
|
-
appName: 'crm',
|
|
219
|
-
}),
|
|
220
|
-
},
|
|
221
|
-
{},
|
|
222
|
-
);
|
|
223
|
-
expect(rejected.status).toBe(401);
|
|
224
|
-
expect(fs.existsSync(snapshotPath)).toBe(false);
|
|
225
|
-
|
|
226
|
-
const accepted = await server.request(
|
|
227
|
-
'/_modern/contract-gates/runtime-fallback',
|
|
228
|
-
{
|
|
229
|
-
method: 'POST',
|
|
230
|
-
headers: new Headers({
|
|
231
|
-
'content-type': 'application/json',
|
|
232
|
-
'x-modernjs-runtime-signal-token': 'top-secret-token',
|
|
233
|
-
}),
|
|
234
|
-
body: JSON.stringify({
|
|
235
|
-
reason: 'remote_load_failed',
|
|
236
|
-
phase: 'load',
|
|
237
|
-
appName: 'crm',
|
|
238
|
-
}),
|
|
239
|
-
},
|
|
240
|
-
{},
|
|
241
|
-
);
|
|
242
|
-
expect(accepted.status).toBe(202);
|
|
243
|
-
expect(fs.existsSync(snapshotPath)).toBe(true);
|
|
244
|
-
} finally {
|
|
245
|
-
fs.rmSync(tempDir, { recursive: true, force: true });
|
|
246
|
-
}
|
|
247
|
-
});
|
|
248
|
-
|
|
249
|
-
test('exposes runtime status endpoint and applies auth when runtime signal auth is enabled', async () => {
|
|
250
|
-
const tempDir = makeTempDir();
|
|
251
|
-
const snapshotPath = path.join(tempDir, '.modern/contract-gates.json');
|
|
252
|
-
|
|
253
|
-
try {
|
|
254
|
-
const config = getDefaultConfig();
|
|
255
|
-
config.server = {
|
|
256
|
-
telemetry: {
|
|
257
|
-
enabled: true,
|
|
258
|
-
canary: {
|
|
259
|
-
enabled: true,
|
|
260
|
-
rollbackConsecutiveFailures: 1,
|
|
261
|
-
autopilot: {
|
|
262
|
-
enabled: true,
|
|
263
|
-
gateSnapshotPath: snapshotPath,
|
|
264
|
-
runtimeFallbackSignal: {
|
|
265
|
-
enabled: true,
|
|
266
|
-
endpoint: '/_modern/contract-gates/runtime-fallback',
|
|
267
|
-
auth: {
|
|
268
|
-
enabled: true,
|
|
269
|
-
headerName: 'x-modernjs-runtime-signal-token',
|
|
270
|
-
expectedValue: 'status-token',
|
|
271
|
-
},
|
|
272
|
-
},
|
|
273
|
-
},
|
|
274
|
-
},
|
|
275
|
-
},
|
|
276
|
-
} as any;
|
|
277
|
-
|
|
278
|
-
const server = createServerBase({
|
|
279
|
-
config,
|
|
280
|
-
pwd: tempDir,
|
|
281
|
-
appContext: getDefaultAppContext(),
|
|
282
|
-
});
|
|
283
|
-
server.addPlugins([
|
|
284
|
-
...createDefaultPlugins({
|
|
285
|
-
logger: false,
|
|
286
|
-
}),
|
|
287
|
-
injectTelemetryPlugin(),
|
|
288
|
-
]);
|
|
289
|
-
|
|
290
|
-
await server.init();
|
|
291
|
-
|
|
292
|
-
const unauthorized = await server.request(
|
|
293
|
-
'/_modern/runtime/status',
|
|
294
|
-
{
|
|
295
|
-
method: 'GET',
|
|
296
|
-
headers: new Headers(),
|
|
297
|
-
},
|
|
298
|
-
{},
|
|
299
|
-
);
|
|
300
|
-
expect(unauthorized.status).toBe(401);
|
|
301
|
-
|
|
302
|
-
const authorized = await server.request(
|
|
303
|
-
'/_modern/runtime/status',
|
|
304
|
-
{
|
|
305
|
-
method: 'GET',
|
|
306
|
-
headers: new Headers({
|
|
307
|
-
'x-modernjs-runtime-signal-token': 'status-token',
|
|
308
|
-
}),
|
|
309
|
-
},
|
|
310
|
-
{},
|
|
311
|
-
);
|
|
312
|
-
expect(authorized.status).toBe(200);
|
|
313
|
-
const payload = (await authorized.json()) as {
|
|
314
|
-
ok?: boolean;
|
|
315
|
-
telemetry?: { queueStats?: { capacity?: number } };
|
|
316
|
-
canary?: { enabled?: boolean };
|
|
317
|
-
runtimeFallbackSignal?: {
|
|
318
|
-
enabled?: boolean;
|
|
319
|
-
endpoint?: string;
|
|
320
|
-
auth?: { enabled?: boolean };
|
|
321
|
-
};
|
|
322
|
-
};
|
|
323
|
-
expect(payload.ok).toBe(true);
|
|
324
|
-
expect(payload.telemetry?.queueStats?.capacity).toBeGreaterThan(0);
|
|
325
|
-
expect(payload.canary?.enabled).toBe(true);
|
|
326
|
-
expect(payload.runtimeFallbackSignal?.enabled).toBe(true);
|
|
327
|
-
expect(payload.runtimeFallbackSignal?.endpoint).toBe(
|
|
328
|
-
'/_modern/contract-gates/runtime-fallback',
|
|
329
|
-
);
|
|
330
|
-
expect(payload.runtimeFallbackSignal?.auth?.enabled).toBe(true);
|
|
331
|
-
} finally {
|
|
332
|
-
fs.rmSync(tempDir, { recursive: true, force: true });
|
|
333
|
-
}
|
|
334
|
-
});
|
|
335
|
-
|
|
336
|
-
test('supports pluggable contract gate snapshot stateStore modules', async () => {
|
|
337
|
-
const tempDir = makeTempDir();
|
|
338
|
-
const defaultSnapshotPath = path.join(
|
|
339
|
-
tempDir,
|
|
340
|
-
'.modern/contract-gates.json',
|
|
341
|
-
);
|
|
342
|
-
const customSnapshotPath = path.join(tempDir, '.state-store/gates.json');
|
|
343
|
-
const stateStoreModulePath = path.join(tempDir, 'gate-state-store.js');
|
|
344
|
-
|
|
345
|
-
fs.writeFileSync(
|
|
346
|
-
stateStoreModulePath,
|
|
347
|
-
`'use strict';
|
|
348
|
-
const fs = require('fs');
|
|
349
|
-
const path = require('path');
|
|
350
|
-
exports.createContractGateSnapshotStore = ({ gateSnapshotPath, options }) => {
|
|
351
|
-
const targetPath = options && typeof options.snapshotPath === 'string'
|
|
352
|
-
? options.snapshotPath
|
|
353
|
-
: gateSnapshotPath;
|
|
354
|
-
return {
|
|
355
|
-
name: 'test-state-store',
|
|
356
|
-
async readSnapshot() {
|
|
357
|
-
if (!fs.existsSync(targetPath)) {
|
|
358
|
-
return undefined;
|
|
359
|
-
}
|
|
360
|
-
return JSON.parse(fs.readFileSync(targetPath, 'utf8'));
|
|
361
|
-
},
|
|
362
|
-
async writeSnapshot(snapshot) {
|
|
363
|
-
fs.mkdirSync(path.dirname(targetPath), { recursive: true });
|
|
364
|
-
fs.writeFileSync(targetPath, JSON.stringify(snapshot, null, 2));
|
|
365
|
-
},
|
|
366
|
-
};
|
|
367
|
-
};
|
|
368
|
-
`,
|
|
369
|
-
'utf8',
|
|
370
|
-
);
|
|
371
|
-
|
|
372
|
-
try {
|
|
373
|
-
const config = getDefaultConfig();
|
|
374
|
-
config.server = {
|
|
375
|
-
telemetry: {
|
|
376
|
-
enabled: true,
|
|
377
|
-
canary: {
|
|
378
|
-
enabled: true,
|
|
379
|
-
rollbackConsecutiveFailures: 1,
|
|
380
|
-
autopilot: {
|
|
381
|
-
enabled: true,
|
|
382
|
-
gateSnapshotPath: defaultSnapshotPath,
|
|
383
|
-
stateStore: {
|
|
384
|
-
module: stateStoreModulePath,
|
|
385
|
-
options: {
|
|
386
|
-
snapshotPath: customSnapshotPath,
|
|
387
|
-
},
|
|
388
|
-
},
|
|
389
|
-
runtimeFallbackSignal: {
|
|
390
|
-
enabled: true,
|
|
391
|
-
endpoint: '/_modern/contract-gates/runtime-fallback',
|
|
392
|
-
auth: {
|
|
393
|
-
expectedValue: 'state-store-token',
|
|
394
|
-
},
|
|
395
|
-
},
|
|
396
|
-
},
|
|
397
|
-
},
|
|
398
|
-
},
|
|
399
|
-
} as any;
|
|
400
|
-
|
|
401
|
-
const server = createServerBase({
|
|
402
|
-
config,
|
|
403
|
-
pwd: tempDir,
|
|
404
|
-
appContext: getDefaultAppContext(),
|
|
405
|
-
});
|
|
406
|
-
server.addPlugins([
|
|
407
|
-
...createDefaultPlugins({
|
|
408
|
-
logger: false,
|
|
409
|
-
}),
|
|
410
|
-
injectTelemetryPlugin(),
|
|
411
|
-
]);
|
|
412
|
-
|
|
413
|
-
await server.init();
|
|
414
|
-
|
|
415
|
-
const response = await server.request(
|
|
416
|
-
'/_modern/contract-gates/runtime-fallback',
|
|
417
|
-
{
|
|
418
|
-
method: 'POST',
|
|
419
|
-
headers: new Headers({
|
|
420
|
-
'content-type': 'application/json',
|
|
421
|
-
'x-modernjs-runtime-signal-token': 'state-store-token',
|
|
422
|
-
}),
|
|
423
|
-
body: JSON.stringify({
|
|
424
|
-
reason: 'remote_mount_failed',
|
|
425
|
-
phase: 'mount',
|
|
426
|
-
appName: 'crm',
|
|
427
|
-
}),
|
|
428
|
-
},
|
|
429
|
-
{},
|
|
430
|
-
);
|
|
431
|
-
|
|
432
|
-
expect(response.status).toBe(202);
|
|
433
|
-
expect(fs.existsSync(defaultSnapshotPath)).toBe(false);
|
|
434
|
-
expect(fs.existsSync(customSnapshotPath)).toBe(true);
|
|
435
|
-
|
|
436
|
-
const snapshot = JSON.parse(fs.readFileSync(customSnapshotPath, 'utf8'));
|
|
437
|
-
expect(snapshot.gates?.['runtime-mf-fallback-health']?.passed).toBe(
|
|
438
|
-
false,
|
|
439
|
-
);
|
|
440
|
-
} finally {
|
|
441
|
-
fs.rmSync(tempDir, { recursive: true, force: true });
|
|
442
|
-
}
|
|
443
|
-
});
|
|
444
|
-
|
|
445
|
-
test('autopilot path honors configured contract gate snapshot stateStore', async () => {
|
|
446
|
-
const tempDir = makeTempDir();
|
|
447
|
-
const defaultSnapshotPath = path.join(
|
|
448
|
-
tempDir,
|
|
449
|
-
'.modern/contract-gates.json',
|
|
450
|
-
);
|
|
451
|
-
const customSnapshotPath = path.join(tempDir, '.state-store/gates.json');
|
|
452
|
-
const stateStoreModulePath = path.join(
|
|
453
|
-
tempDir,
|
|
454
|
-
'autopilot-gate-state-store.js',
|
|
455
|
-
);
|
|
456
|
-
const readMarkerPath = path.join(tempDir, '.state-store/read-marker.txt');
|
|
457
|
-
const now = Date.now();
|
|
458
|
-
|
|
459
|
-
fs.mkdirSync(path.dirname(customSnapshotPath), { recursive: true });
|
|
460
|
-
fs.writeFileSync(
|
|
461
|
-
customSnapshotPath,
|
|
462
|
-
JSON.stringify(
|
|
463
|
-
{
|
|
464
|
-
schemaVersion: 1,
|
|
465
|
-
updatedAt: now,
|
|
466
|
-
gates: {
|
|
467
|
-
'state-store-autopilot-gate': {
|
|
468
|
-
passed: false,
|
|
469
|
-
reason: 'from configured stateStore',
|
|
470
|
-
updatedAt: now,
|
|
471
|
-
},
|
|
472
|
-
},
|
|
473
|
-
},
|
|
474
|
-
null,
|
|
475
|
-
2,
|
|
476
|
-
),
|
|
477
|
-
'utf8',
|
|
478
|
-
);
|
|
479
|
-
fs.writeFileSync(
|
|
480
|
-
stateStoreModulePath,
|
|
481
|
-
`'use strict';
|
|
482
|
-
const fs = require('fs');
|
|
483
|
-
const path = require('path');
|
|
484
|
-
exports.createContractGateSnapshotStore = ({ gateSnapshotPath, options }) => {
|
|
485
|
-
const targetPath = options && typeof options.snapshotPath === 'string'
|
|
486
|
-
? options.snapshotPath
|
|
487
|
-
: gateSnapshotPath;
|
|
488
|
-
const markerPath = options && typeof options.markerPath === 'string'
|
|
489
|
-
? options.markerPath
|
|
490
|
-
: undefined;
|
|
491
|
-
return {
|
|
492
|
-
name: 'autopilot-test-state-store',
|
|
493
|
-
async readSnapshot() {
|
|
494
|
-
if (markerPath) {
|
|
495
|
-
fs.mkdirSync(path.dirname(markerPath), { recursive: true });
|
|
496
|
-
fs.appendFileSync(markerPath, targetPath + '\\n');
|
|
497
|
-
}
|
|
498
|
-
if (!fs.existsSync(targetPath)) {
|
|
499
|
-
return undefined;
|
|
500
|
-
}
|
|
501
|
-
return JSON.parse(fs.readFileSync(targetPath, 'utf8'));
|
|
502
|
-
},
|
|
503
|
-
async writeSnapshot(snapshot) {
|
|
504
|
-
fs.mkdirSync(path.dirname(targetPath), { recursive: true });
|
|
505
|
-
fs.writeFileSync(targetPath, JSON.stringify(snapshot, null, 2));
|
|
506
|
-
},
|
|
507
|
-
};
|
|
508
|
-
};
|
|
509
|
-
`,
|
|
510
|
-
'utf8',
|
|
511
|
-
);
|
|
512
|
-
|
|
513
|
-
try {
|
|
514
|
-
const config = getDefaultConfig();
|
|
515
|
-
config.server = {
|
|
516
|
-
telemetry: {
|
|
517
|
-
enabled: true,
|
|
518
|
-
canary: {
|
|
519
|
-
enabled: true,
|
|
520
|
-
rollbackConsecutiveFailures: 1,
|
|
521
|
-
contractGates: {
|
|
522
|
-
'state-store-autopilot-gate': true,
|
|
523
|
-
},
|
|
524
|
-
autopilot: {
|
|
525
|
-
enabled: true,
|
|
526
|
-
gateSnapshotPath: defaultSnapshotPath,
|
|
527
|
-
pollIntervalMs: 20,
|
|
528
|
-
gateStaleAfterMs: 60_000,
|
|
529
|
-
stateStore: {
|
|
530
|
-
module: stateStoreModulePath,
|
|
531
|
-
options: {
|
|
532
|
-
snapshotPath: customSnapshotPath,
|
|
533
|
-
markerPath: readMarkerPath,
|
|
534
|
-
},
|
|
535
|
-
},
|
|
536
|
-
runtimeFallbackSignal: {
|
|
537
|
-
auth: {
|
|
538
|
-
enabled: true,
|
|
539
|
-
expectedValue: 'state-store-status-token',
|
|
540
|
-
},
|
|
541
|
-
},
|
|
542
|
-
},
|
|
543
|
-
},
|
|
544
|
-
},
|
|
545
|
-
} as any;
|
|
546
|
-
|
|
547
|
-
const server = createServerBase({
|
|
548
|
-
config,
|
|
549
|
-
pwd: tempDir,
|
|
550
|
-
appContext: getDefaultAppContext(),
|
|
551
|
-
});
|
|
552
|
-
|
|
553
|
-
server.addPlugins([
|
|
554
|
-
...createDefaultPlugins({
|
|
555
|
-
logger: false,
|
|
556
|
-
}),
|
|
557
|
-
injectTelemetryPlugin(),
|
|
558
|
-
]);
|
|
559
|
-
|
|
560
|
-
await server.init();
|
|
561
|
-
|
|
562
|
-
expect(fs.existsSync(readMarkerPath)).toBe(true);
|
|
563
|
-
expect(fs.readFileSync(readMarkerPath, 'utf8')).toContain(
|
|
564
|
-
customSnapshotPath,
|
|
565
|
-
);
|
|
566
|
-
expect(fs.existsSync(defaultSnapshotPath)).toBe(false);
|
|
567
|
-
|
|
568
|
-
const status = await server.request(
|
|
569
|
-
'/_modern/runtime/status',
|
|
570
|
-
{
|
|
571
|
-
method: 'GET',
|
|
572
|
-
headers: new Headers({
|
|
573
|
-
'x-modernjs-runtime-signal-token': 'state-store-status-token',
|
|
574
|
-
}),
|
|
575
|
-
},
|
|
576
|
-
{},
|
|
577
|
-
);
|
|
578
|
-
expect(status.status).toBe(200);
|
|
579
|
-
const statusBody = await status.json();
|
|
580
|
-
expect(statusBody.canary?.enabled).toBe(true);
|
|
581
|
-
} finally {
|
|
582
|
-
fs.rmSync(tempDir, { recursive: true, force: true });
|
|
583
|
-
}
|
|
584
|
-
});
|
|
585
|
-
|
|
586
|
-
test('enforces runtime fallback trust policy before mutating gate snapshots', async () => {
|
|
587
|
-
const tempDir = makeTempDir();
|
|
588
|
-
const snapshotPath = path.join(tempDir, '.modern/contract-gates.json');
|
|
589
|
-
|
|
590
|
-
try {
|
|
591
|
-
const config = getDefaultConfig();
|
|
592
|
-
config.server = {
|
|
593
|
-
telemetry: {
|
|
594
|
-
enabled: true,
|
|
595
|
-
canary: {
|
|
596
|
-
enabled: true,
|
|
597
|
-
rollbackConsecutiveFailures: 1,
|
|
598
|
-
autopilot: {
|
|
599
|
-
enabled: true,
|
|
600
|
-
gateSnapshotPath: snapshotPath,
|
|
601
|
-
runtimeFallbackSignal: {
|
|
602
|
-
enabled: true,
|
|
603
|
-
endpoint: '/_modern/contract-gates/runtime-fallback',
|
|
604
|
-
auth: {
|
|
605
|
-
expectedValue: 'trust-policy-token',
|
|
606
|
-
},
|
|
607
|
-
trustPolicy: {
|
|
608
|
-
allowedApps: ['crm-shell'],
|
|
609
|
-
allowedEntryOrigins: ['https://erp.example.com'],
|
|
610
|
-
expectedRuntimeDigests: {
|
|
611
|
-
'crm-shell': 'digest-crm-v1',
|
|
612
|
-
},
|
|
613
|
-
enforceRuntimeDigest: true,
|
|
614
|
-
},
|
|
615
|
-
},
|
|
616
|
-
},
|
|
617
|
-
},
|
|
618
|
-
},
|
|
619
|
-
} as any;
|
|
620
|
-
|
|
621
|
-
const server = createServerBase({
|
|
622
|
-
config,
|
|
623
|
-
pwd: tempDir,
|
|
624
|
-
appContext: getDefaultAppContext(),
|
|
625
|
-
});
|
|
626
|
-
server.addPlugins([
|
|
627
|
-
...createDefaultPlugins({
|
|
628
|
-
logger: false,
|
|
629
|
-
}),
|
|
630
|
-
injectTelemetryPlugin(),
|
|
631
|
-
]);
|
|
632
|
-
|
|
633
|
-
await server.init();
|
|
634
|
-
|
|
635
|
-
const untrustedApp = await server.request(
|
|
636
|
-
'/_modern/contract-gates/runtime-fallback',
|
|
637
|
-
{
|
|
638
|
-
method: 'POST',
|
|
639
|
-
headers: new Headers({
|
|
640
|
-
'content-type': 'application/json',
|
|
641
|
-
'x-modernjs-runtime-signal-token': 'trust-policy-token',
|
|
642
|
-
}),
|
|
643
|
-
body: JSON.stringify({
|
|
644
|
-
reason: 'remote_load_failed',
|
|
645
|
-
phase: 'load',
|
|
646
|
-
appName: 'unknown-app',
|
|
647
|
-
entry: 'https://erp.example.com/remoteEntry.js',
|
|
648
|
-
runtimeDigest: 'digest-crm-v1',
|
|
649
|
-
}),
|
|
650
|
-
},
|
|
651
|
-
{},
|
|
652
|
-
);
|
|
653
|
-
expect(untrustedApp.status).toBe(403);
|
|
654
|
-
|
|
655
|
-
const digestMismatch = await server.request(
|
|
656
|
-
'/_modern/contract-gates/runtime-fallback',
|
|
657
|
-
{
|
|
658
|
-
method: 'POST',
|
|
659
|
-
headers: new Headers({
|
|
660
|
-
'content-type': 'application/json',
|
|
661
|
-
'x-modernjs-runtime-signal-token': 'trust-policy-token',
|
|
662
|
-
}),
|
|
663
|
-
body: JSON.stringify({
|
|
664
|
-
reason: 'remote_load_failed',
|
|
665
|
-
phase: 'load',
|
|
666
|
-
appName: 'crm-shell',
|
|
667
|
-
entry: 'https://erp.example.com/remoteEntry.js',
|
|
668
|
-
runtimeDigest: 'digest-wrong',
|
|
669
|
-
}),
|
|
670
|
-
},
|
|
671
|
-
{},
|
|
672
|
-
);
|
|
673
|
-
expect(digestMismatch.status).toBe(403);
|
|
674
|
-
|
|
675
|
-
const trusted = await server.request(
|
|
676
|
-
'/_modern/contract-gates/runtime-fallback',
|
|
677
|
-
{
|
|
678
|
-
method: 'POST',
|
|
679
|
-
headers: new Headers({
|
|
680
|
-
'content-type': 'application/json',
|
|
681
|
-
'x-modernjs-runtime-signal-token': 'trust-policy-token',
|
|
682
|
-
}),
|
|
683
|
-
body: JSON.stringify({
|
|
684
|
-
reason: 'remote_load_failed',
|
|
685
|
-
phase: 'load',
|
|
686
|
-
appName: 'crm-shell',
|
|
687
|
-
entry: 'https://erp.example.com/remoteEntry.js',
|
|
688
|
-
runtimeDigest: 'digest-crm-v1',
|
|
689
|
-
metadata: {
|
|
690
|
-
compatibility: {
|
|
691
|
-
'@tanstack/react-router': '1.170.15',
|
|
692
|
-
},
|
|
693
|
-
remote: 'remote/Widget',
|
|
694
|
-
},
|
|
695
|
-
}),
|
|
696
|
-
},
|
|
697
|
-
{},
|
|
698
|
-
);
|
|
699
|
-
expect(trusted.status).toBe(202);
|
|
700
|
-
|
|
701
|
-
const snapshot = JSON.parse(fs.readFileSync(snapshotPath, 'utf8')) as {
|
|
702
|
-
gates?: Record<string, any>;
|
|
703
|
-
};
|
|
704
|
-
expect(snapshot.gates?.['runtime-mf-fallback-health']?.passed).toBe(
|
|
705
|
-
false,
|
|
706
|
-
);
|
|
707
|
-
expect(
|
|
708
|
-
snapshot.gates?.['runtime-mf-fallback-health']?.metadata?.metadata
|
|
709
|
-
?.compatibility?.['@tanstack/react-router'],
|
|
710
|
-
).toBe('1.170.15');
|
|
711
|
-
expect(
|
|
712
|
-
snapshot.gates?.['runtime-mf-fallback-health']?.metadata?.metadata
|
|
713
|
-
?.remote,
|
|
714
|
-
).toBe('remote/Widget');
|
|
715
|
-
} finally {
|
|
716
|
-
fs.rmSync(tempDir, { recursive: true, force: true });
|
|
717
|
-
}
|
|
718
|
-
});
|
|
719
|
-
|
|
720
|
-
test('deduplicates repeated fallback events and enforces rate limits per source window', async () => {
|
|
721
|
-
const tempDir = makeTempDir();
|
|
722
|
-
const snapshotPath = path.join(tempDir, '.modern/contract-gates.json');
|
|
723
|
-
|
|
724
|
-
try {
|
|
725
|
-
const config = getDefaultConfig();
|
|
726
|
-
config.server = {
|
|
727
|
-
telemetry: {
|
|
728
|
-
enabled: true,
|
|
729
|
-
canary: {
|
|
730
|
-
enabled: true,
|
|
731
|
-
rollbackConsecutiveFailures: 1,
|
|
732
|
-
autopilot: {
|
|
733
|
-
enabled: true,
|
|
734
|
-
gateSnapshotPath: snapshotPath,
|
|
735
|
-
runtimeFallbackSignal: {
|
|
736
|
-
enabled: true,
|
|
737
|
-
endpoint: '/_modern/contract-gates/runtime-fallback',
|
|
738
|
-
auth: {
|
|
739
|
-
expectedValue: 'rate-limit-token',
|
|
740
|
-
},
|
|
741
|
-
trustPolicy: {
|
|
742
|
-
allowedApps: ['crm-shell'],
|
|
743
|
-
dedupeWindowMs: 60_000,
|
|
744
|
-
maxSignalsPerWindow: 1,
|
|
745
|
-
windowMs: 60_000,
|
|
746
|
-
},
|
|
747
|
-
},
|
|
748
|
-
},
|
|
749
|
-
},
|
|
750
|
-
},
|
|
751
|
-
} as any;
|
|
752
|
-
|
|
753
|
-
const server = createServerBase({
|
|
754
|
-
config,
|
|
755
|
-
pwd: tempDir,
|
|
756
|
-
appContext: getDefaultAppContext(),
|
|
757
|
-
});
|
|
758
|
-
server.addPlugins([
|
|
759
|
-
...createDefaultPlugins({
|
|
760
|
-
logger: false,
|
|
761
|
-
}),
|
|
762
|
-
injectTelemetryPlugin(),
|
|
763
|
-
]);
|
|
764
|
-
|
|
765
|
-
await server.init();
|
|
766
|
-
|
|
767
|
-
const payload = {
|
|
768
|
-
reason: 'remote_mount_failed',
|
|
769
|
-
phase: 'mount',
|
|
770
|
-
appName: 'crm-shell',
|
|
771
|
-
entry: 'https://erp.example.com/remoteEntry.js',
|
|
772
|
-
};
|
|
773
|
-
|
|
774
|
-
const first = await server.request(
|
|
775
|
-
'/_modern/contract-gates/runtime-fallback',
|
|
776
|
-
{
|
|
777
|
-
method: 'POST',
|
|
778
|
-
headers: new Headers({
|
|
779
|
-
'content-type': 'application/json',
|
|
780
|
-
'x-modernjs-runtime-signal-token': 'rate-limit-token',
|
|
781
|
-
}),
|
|
782
|
-
body: JSON.stringify(payload),
|
|
783
|
-
},
|
|
784
|
-
{},
|
|
785
|
-
);
|
|
786
|
-
expect(first.status).toBe(202);
|
|
787
|
-
|
|
788
|
-
const duplicate = await server.request(
|
|
789
|
-
'/_modern/contract-gates/runtime-fallback',
|
|
790
|
-
{
|
|
791
|
-
method: 'POST',
|
|
792
|
-
headers: new Headers({
|
|
793
|
-
'content-type': 'application/json',
|
|
794
|
-
'x-modernjs-runtime-signal-token': 'rate-limit-token',
|
|
795
|
-
}),
|
|
796
|
-
body: JSON.stringify(payload),
|
|
797
|
-
},
|
|
798
|
-
{},
|
|
799
|
-
);
|
|
800
|
-
expect(duplicate.status).toBe(202);
|
|
801
|
-
const duplicateBody = (await duplicate.json()) as {
|
|
802
|
-
deduped?: boolean;
|
|
803
|
-
};
|
|
804
|
-
expect(duplicateBody.deduped).toBe(true);
|
|
805
|
-
|
|
806
|
-
const secondUnique = await server.request(
|
|
807
|
-
'/_modern/contract-gates/runtime-fallback',
|
|
808
|
-
{
|
|
809
|
-
method: 'POST',
|
|
810
|
-
headers: new Headers({
|
|
811
|
-
'content-type': 'application/json',
|
|
812
|
-
'x-modernjs-runtime-signal-token': 'rate-limit-token',
|
|
813
|
-
}),
|
|
814
|
-
body: JSON.stringify({
|
|
815
|
-
...payload,
|
|
816
|
-
reason: 'remote_load_failed',
|
|
817
|
-
}),
|
|
818
|
-
},
|
|
819
|
-
{},
|
|
820
|
-
);
|
|
821
|
-
expect(secondUnique.status).toBe(429);
|
|
822
|
-
} finally {
|
|
823
|
-
fs.rmSync(tempDir, { recursive: true, force: true });
|
|
824
|
-
}
|
|
825
|
-
});
|
|
826
|
-
|
|
827
|
-
test('keeps the runtime fallback signal endpoint disabled unless explicitly enabled', async () => {
|
|
828
|
-
const tempDir = makeTempDir();
|
|
829
|
-
const snapshotPath = path.join(tempDir, '.modern/contract-gates.json');
|
|
830
|
-
|
|
831
|
-
try {
|
|
832
|
-
const config = getDefaultConfig();
|
|
833
|
-
config.server = {
|
|
834
|
-
telemetry: {
|
|
835
|
-
enabled: true,
|
|
836
|
-
canary: {
|
|
837
|
-
enabled: true,
|
|
838
|
-
rollbackConsecutiveFailures: 1,
|
|
839
|
-
autopilot: {
|
|
840
|
-
enabled: true,
|
|
841
|
-
gateSnapshotPath: snapshotPath,
|
|
842
|
-
// no runtimeFallbackSignal config: the canary kill switch must
|
|
843
|
-
// stay OFF by default.
|
|
844
|
-
},
|
|
845
|
-
},
|
|
846
|
-
},
|
|
847
|
-
} as any;
|
|
848
|
-
|
|
849
|
-
const server = createServerBase({
|
|
850
|
-
config,
|
|
851
|
-
pwd: tempDir,
|
|
852
|
-
appContext: getDefaultAppContext(),
|
|
853
|
-
});
|
|
854
|
-
server.addPlugins([
|
|
855
|
-
...createDefaultPlugins({
|
|
856
|
-
logger: false,
|
|
857
|
-
}),
|
|
858
|
-
injectTelemetryPlugin(),
|
|
859
|
-
]);
|
|
860
|
-
|
|
861
|
-
await server.init();
|
|
862
|
-
|
|
863
|
-
const response = await server.request(
|
|
864
|
-
'/_modern/contract-gates/runtime-fallback',
|
|
865
|
-
{
|
|
866
|
-
method: 'POST',
|
|
867
|
-
headers: new Headers({
|
|
868
|
-
'content-type': 'application/json',
|
|
869
|
-
}),
|
|
870
|
-
body: JSON.stringify({
|
|
871
|
-
reason: 'remote_load_failed',
|
|
872
|
-
phase: 'load',
|
|
873
|
-
appName: 'dashboard',
|
|
874
|
-
}),
|
|
875
|
-
},
|
|
876
|
-
{},
|
|
877
|
-
);
|
|
878
|
-
|
|
879
|
-
expect(response.status).toBe(404);
|
|
880
|
-
expect(fs.existsSync(snapshotPath)).toBe(false);
|
|
881
|
-
} finally {
|
|
882
|
-
fs.rmSync(tempDir, { recursive: true, force: true });
|
|
883
|
-
}
|
|
884
|
-
});
|
|
885
|
-
|
|
886
|
-
test('refuses to enable the runtime fallback signal endpoint without an auth token', async () => {
|
|
887
|
-
const tempDir = makeTempDir();
|
|
888
|
-
const snapshotPath = path.join(tempDir, '.modern/contract-gates.json');
|
|
889
|
-
|
|
890
|
-
try {
|
|
891
|
-
const config = getDefaultConfig();
|
|
892
|
-
config.server = {
|
|
893
|
-
telemetry: {
|
|
894
|
-
enabled: true,
|
|
895
|
-
canary: {
|
|
896
|
-
enabled: true,
|
|
897
|
-
autopilot: {
|
|
898
|
-
enabled: true,
|
|
899
|
-
gateSnapshotPath: snapshotPath,
|
|
900
|
-
runtimeFallbackSignal: {
|
|
901
|
-
enabled: true,
|
|
902
|
-
// no auth token configured: enabling must fail closed.
|
|
903
|
-
},
|
|
904
|
-
},
|
|
905
|
-
},
|
|
906
|
-
},
|
|
907
|
-
} as any;
|
|
908
|
-
|
|
909
|
-
const server = createServerBase({
|
|
910
|
-
config,
|
|
911
|
-
pwd: tempDir,
|
|
912
|
-
appContext: getDefaultAppContext(),
|
|
913
|
-
});
|
|
914
|
-
server.addPlugins([
|
|
915
|
-
...createDefaultPlugins({
|
|
916
|
-
logger: false,
|
|
917
|
-
}),
|
|
918
|
-
injectTelemetryPlugin(),
|
|
919
|
-
]);
|
|
920
|
-
|
|
921
|
-
await expect(server.init()).rejects.toThrow(
|
|
922
|
-
/requires an auth token|auth\.expectedValue/,
|
|
923
|
-
);
|
|
924
|
-
} finally {
|
|
925
|
-
fs.rmSync(tempDir, { recursive: true, force: true });
|
|
926
|
-
}
|
|
927
|
-
});
|
|
928
|
-
|
|
929
|
-
test('rate limits cannot be reset by rotating payload identities', async () => {
|
|
930
|
-
const tempDir = makeTempDir();
|
|
931
|
-
const snapshotPath = path.join(tempDir, '.modern/contract-gates.json');
|
|
932
|
-
|
|
933
|
-
try {
|
|
934
|
-
const config = getDefaultConfig();
|
|
935
|
-
config.server = {
|
|
936
|
-
telemetry: {
|
|
937
|
-
enabled: true,
|
|
938
|
-
canary: {
|
|
939
|
-
enabled: true,
|
|
940
|
-
rollbackConsecutiveFailures: 1,
|
|
941
|
-
autopilot: {
|
|
942
|
-
enabled: true,
|
|
943
|
-
gateSnapshotPath: snapshotPath,
|
|
944
|
-
runtimeFallbackSignal: {
|
|
945
|
-
enabled: true,
|
|
946
|
-
endpoint: '/_modern/contract-gates/runtime-fallback',
|
|
947
|
-
auth: {
|
|
948
|
-
expectedValue: 'rotation-token',
|
|
949
|
-
},
|
|
950
|
-
trustPolicy: {
|
|
951
|
-
dedupeWindowMs: 0,
|
|
952
|
-
maxSignalsPerWindow: 1,
|
|
953
|
-
windowMs: 60_000,
|
|
954
|
-
},
|
|
955
|
-
},
|
|
956
|
-
},
|
|
957
|
-
},
|
|
958
|
-
},
|
|
959
|
-
} as any;
|
|
960
|
-
|
|
961
|
-
const server = createServerBase({
|
|
962
|
-
config,
|
|
963
|
-
pwd: tempDir,
|
|
964
|
-
appContext: getDefaultAppContext(),
|
|
965
|
-
});
|
|
966
|
-
server.addPlugins([
|
|
967
|
-
...createDefaultPlugins({
|
|
968
|
-
logger: false,
|
|
969
|
-
}),
|
|
970
|
-
injectTelemetryPlugin(),
|
|
971
|
-
]);
|
|
972
|
-
|
|
973
|
-
await server.init();
|
|
974
|
-
|
|
975
|
-
const sendSignal = (appName: string) =>
|
|
976
|
-
server.request(
|
|
977
|
-
'/_modern/contract-gates/runtime-fallback',
|
|
978
|
-
{
|
|
979
|
-
method: 'POST',
|
|
980
|
-
headers: new Headers({
|
|
981
|
-
'content-type': 'application/json',
|
|
982
|
-
'x-modernjs-runtime-signal-token': 'rotation-token',
|
|
983
|
-
}),
|
|
984
|
-
body: JSON.stringify({
|
|
985
|
-
reason: 'remote_load_failed',
|
|
986
|
-
phase: 'load',
|
|
987
|
-
appName,
|
|
988
|
-
entry: `https://${appName}.example.com/remoteEntry.js`,
|
|
989
|
-
}),
|
|
990
|
-
},
|
|
991
|
-
{},
|
|
992
|
-
);
|
|
993
|
-
|
|
994
|
-
const first = await sendSignal('app-a');
|
|
995
|
-
expect(first.status).toBe(202);
|
|
996
|
-
|
|
997
|
-
// A rotated appName/entry pair used to mint a fresh rate-limit bucket;
|
|
998
|
-
// the limiter is now keyed on connection identity instead.
|
|
999
|
-
const rotated = await sendSignal('app-b');
|
|
1000
|
-
expect(rotated.status).toBe(429);
|
|
1001
|
-
} finally {
|
|
1002
|
-
fs.rmSync(tempDir, { recursive: true, force: true });
|
|
1003
|
-
}
|
|
1004
|
-
});
|
|
1005
|
-
|
|
1006
|
-
test('runtime status endpoint stays a bare health probe without configured auth', async () => {
|
|
1007
|
-
const tempDir = makeTempDir();
|
|
1008
|
-
const snapshotPath = path.join(tempDir, '.modern/contract-gates.json');
|
|
1009
|
-
|
|
1010
|
-
try {
|
|
1011
|
-
const config = getDefaultConfig();
|
|
1012
|
-
config.server = {
|
|
1013
|
-
telemetry: {
|
|
1014
|
-
enabled: true,
|
|
1015
|
-
canary: {
|
|
1016
|
-
enabled: true,
|
|
1017
|
-
autopilot: {
|
|
1018
|
-
enabled: true,
|
|
1019
|
-
gateSnapshotPath: snapshotPath,
|
|
1020
|
-
},
|
|
1021
|
-
},
|
|
1022
|
-
},
|
|
1023
|
-
} as any;
|
|
1024
|
-
|
|
1025
|
-
const server = createServerBase({
|
|
1026
|
-
config,
|
|
1027
|
-
pwd: tempDir,
|
|
1028
|
-
appContext: getDefaultAppContext(),
|
|
1029
|
-
});
|
|
1030
|
-
server.addPlugins([
|
|
1031
|
-
...createDefaultPlugins({
|
|
1032
|
-
logger: false,
|
|
1033
|
-
}),
|
|
1034
|
-
injectTelemetryPlugin(),
|
|
1035
|
-
]);
|
|
1036
|
-
|
|
1037
|
-
await server.init();
|
|
1038
|
-
|
|
1039
|
-
const response = await server.request('/_modern/runtime/status', {}, {});
|
|
1040
|
-
expect(response.status).toBe(200);
|
|
1041
|
-
const payload = (await response.json()) as Record<string, unknown>;
|
|
1042
|
-
expect(payload.ok).toBe(true);
|
|
1043
|
-
expect(typeof payload.timestamp).toBe('number');
|
|
1044
|
-
// No telemetry/canary/trust internals are disclosed without auth.
|
|
1045
|
-
expect(payload.telemetry).toBeUndefined();
|
|
1046
|
-
expect(payload.canary).toBeUndefined();
|
|
1047
|
-
expect(payload.runtimeFallbackSignal).toBeUndefined();
|
|
1048
|
-
} finally {
|
|
1049
|
-
fs.rmSync(tempDir, { recursive: true, force: true });
|
|
1050
|
-
}
|
|
1051
|
-
});
|
|
1052
|
-
|
|
1053
|
-
test('runtime status auth works even when the signal endpoint stays disabled', async () => {
|
|
1054
|
-
const tempDir = makeTempDir();
|
|
1055
|
-
const snapshotPath = path.join(tempDir, '.modern/contract-gates.json');
|
|
1056
|
-
|
|
1057
|
-
try {
|
|
1058
|
-
const config = getDefaultConfig();
|
|
1059
|
-
config.server = {
|
|
1060
|
-
telemetry: {
|
|
1061
|
-
enabled: true,
|
|
1062
|
-
canary: {
|
|
1063
|
-
enabled: true,
|
|
1064
|
-
autopilot: {
|
|
1065
|
-
enabled: true,
|
|
1066
|
-
gateSnapshotPath: snapshotPath,
|
|
1067
|
-
runtimeFallbackSignal: {
|
|
1068
|
-
// endpoint stays disabled, but its auth config still guards
|
|
1069
|
-
// the status endpoint detail view.
|
|
1070
|
-
auth: {
|
|
1071
|
-
enabled: true,
|
|
1072
|
-
expectedValue: 'status-only-token',
|
|
1073
|
-
},
|
|
1074
|
-
},
|
|
1075
|
-
},
|
|
1076
|
-
},
|
|
1077
|
-
},
|
|
1078
|
-
} as any;
|
|
1079
|
-
|
|
1080
|
-
const server = createServerBase({
|
|
1081
|
-
config,
|
|
1082
|
-
pwd: tempDir,
|
|
1083
|
-
appContext: getDefaultAppContext(),
|
|
1084
|
-
});
|
|
1085
|
-
server.addPlugins([
|
|
1086
|
-
...createDefaultPlugins({
|
|
1087
|
-
logger: false,
|
|
1088
|
-
}),
|
|
1089
|
-
injectTelemetryPlugin(),
|
|
1090
|
-
]);
|
|
1091
|
-
|
|
1092
|
-
await server.init();
|
|
1093
|
-
|
|
1094
|
-
const signalResponse = await server.request(
|
|
1095
|
-
'/_modern/contract-gates/runtime-fallback',
|
|
1096
|
-
{
|
|
1097
|
-
method: 'POST',
|
|
1098
|
-
headers: new Headers({
|
|
1099
|
-
'content-type': 'application/json',
|
|
1100
|
-
'x-modernjs-runtime-signal-token': 'status-only-token',
|
|
1101
|
-
}),
|
|
1102
|
-
body: JSON.stringify({ appName: 'dashboard' }),
|
|
1103
|
-
},
|
|
1104
|
-
{},
|
|
1105
|
-
);
|
|
1106
|
-
expect(signalResponse.status).toBe(404);
|
|
1107
|
-
|
|
1108
|
-
const unauthorized = await server.request(
|
|
1109
|
-
'/_modern/runtime/status',
|
|
1110
|
-
{},
|
|
1111
|
-
{},
|
|
1112
|
-
);
|
|
1113
|
-
expect(unauthorized.status).toBe(401);
|
|
1114
|
-
|
|
1115
|
-
const authorized = await server.request(
|
|
1116
|
-
'/_modern/runtime/status',
|
|
1117
|
-
{
|
|
1118
|
-
method: 'GET',
|
|
1119
|
-
headers: new Headers({
|
|
1120
|
-
'x-modernjs-runtime-signal-token': 'status-only-token',
|
|
1121
|
-
}),
|
|
1122
|
-
},
|
|
1123
|
-
{},
|
|
1124
|
-
);
|
|
1125
|
-
expect(authorized.status).toBe(200);
|
|
1126
|
-
const payload = (await authorized.json()) as Record<string, any>;
|
|
1127
|
-
expect(payload.ok).toBe(true);
|
|
1128
|
-
expect(payload.canary?.enabled).toBe(true);
|
|
1129
|
-
expect(payload.runtimeFallbackSignal?.enabled).toBe(false);
|
|
1130
|
-
} finally {
|
|
1131
|
-
fs.rmSync(tempDir, { recursive: true, force: true });
|
|
1132
|
-
}
|
|
1133
|
-
});
|
|
1134
|
-
});
|