@bleedingdev/modern-js-server-runtime-extensions 3.9.0-ultramodern.3 → 3.9.0-ultramodern.5
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/bff-policy/crossProjectPolicy.js +309 -0
- package/dist/cjs/bff-policy/index.js +62 -0
- package/dist/cjs/bff-policy/node.js +69 -0
- package/dist/cjs/bff-policy/operationContracts.js +182 -0
- package/dist/cjs/bff-policy/resolveCrossProjectPolicy.js +65 -0
- package/dist/cjs/errorResponsePlugin.js +57 -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/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/canaryCompatibility.js +73 -0
- package/dist/cjs/telemetry/healthSetup.js +4 -0
- package/dist/cjs/telemetry/lifecycle.js +15 -16
- package/dist/cjs/telemetry/plugin.js +10 -3
- package/dist/cjs/telemetry/runtimeEndpoints.js +10 -3
- package/dist/esm/bff-policy/crossProjectPolicy.mjs +259 -0
- package/dist/esm/bff-policy/index.mjs +1 -0
- package/dist/esm/errorResponsePlugin.mjs +19 -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/serverConfig.mjs +0 -0
- package/dist/esm/serverPlugin.mjs +34 -0
- package/dist/esm/telemetry/canaryCompatibility.mjs +35 -0
- package/dist/esm/telemetry/healthSetup.mjs +4 -0
- package/dist/esm/telemetry/lifecycle.mjs +15 -16
- package/dist/esm/telemetry/plugin.mjs +9 -2
- package/dist/esm/telemetry/runtimeEndpoints.mjs +10 -3
- package/dist/esm-node/bff-policy/crossProjectPolicy.mjs +260 -0
- package/dist/esm-node/bff-policy/index.mjs +2 -0
- package/dist/esm-node/bff-policy/node.mjs +3 -0
- package/dist/esm-node/bff-policy/operationContracts.mjs +129 -0
- package/dist/esm-node/bff-policy/resolveCrossProjectPolicy.mjs +28 -0
- package/dist/esm-node/errorResponsePlugin.mjs +20 -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/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/canaryCompatibility.mjs +36 -0
- package/dist/esm-node/telemetry/healthSetup.mjs +4 -0
- package/dist/esm-node/telemetry/lifecycle.mjs +15 -16
- package/dist/esm-node/telemetry/plugin.mjs +9 -2
- package/dist/esm-node/telemetry/runtimeEndpoints.mjs +10 -3
- package/dist/types/bff-policy/crossProjectPolicy.d.ts +110 -0
- package/dist/types/bff-policy/index.d.ts +1 -0
- package/dist/types/bff-policy/node.d.ts +2 -0
- package/dist/types/bff-policy/operationContracts.d.ts +85 -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/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/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/canaryCompatibility.d.ts +19 -0
- package/dist/types/telemetry/healthSetup.d.ts +3 -1
- package/dist/types/telemetry/lifecycle.d.ts +2 -2
- package/dist/types/telemetry/plugin.d.ts +2 -1
- package/dist/types/telemetry/runtimeEndpoints.d.ts +3 -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 +110 -0
- package/dist/types-esm/bff-policy/index.d.mts +1 -0
- package/dist/types-esm/bff-policy/node.d.mts +2 -0
- package/dist/types-esm/bff-policy/operationContracts.d.mts +85 -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/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 +20 -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/canaryCompatibility.d.mts +19 -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 +19 -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 +37 -0
- package/dist/types-esm/telemetry.d.mts +1 -0
- package/dist/types-esm/telemetryCore.d.mts +6 -0
- package/package.json +158 -26
- package/rslib.config.mts +36 -2
- package/rstest.config.mts +11 -0
- package/src/bff-policy/crossProjectPolicy.ts +781 -0
- package/src/bff-policy/index.ts +1 -0
- package/src/bff-policy/node.ts +2 -0
- package/src/bff-policy/operationContracts.ts +298 -0
- package/src/bff-policy/resolveCrossProjectPolicy.ts +88 -0
- package/src/errorResponsePlugin.ts +21 -0
- package/src/index.ts +4 -0
- package/src/localisedLoaderPlugin.ts +34 -0
- package/src/mfCache.ts +4 -3
- package/src/module-federation-css/plugin.ts +4 -3
- 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/canaryCompatibility.ts +55 -0
- package/src/telemetry/healthSetup.ts +9 -1
- package/src/telemetry/lifecycle.ts +20 -32
- package/src/telemetry/plugin.ts +6 -2
- package/src/telemetry/runtimeEndpoints.ts +13 -2
- package/tests/bff-policy/crossProjectPolicy.matrix.test.ts +487 -0
- package/tests/bff-policy/crossProjectPolicy.test.ts +1004 -0
- package/tests/bff-policy/operationContracts.test.ts +221 -0
- package/tests/bff-policy/optionalZodPeer.test.ts +30 -0
- package/tests/bff-policy/resolveCrossProjectPolicy.test.ts +134 -0
- package/tests/contractGateSnapshotObserver.test.ts +0 -58
- package/tests/env.test.ts +45 -64
- package/tests/localisedLoader.test.ts +120 -0
- package/tests/mfCache.test.ts +1 -88
- package/tests/moduleFederationCss.test.ts +4 -48
- package/tests/nativeErrorResponse.test.ts +199 -0
- package/tests/nativeServerAssembly.test.ts +402 -0
- package/tests/registration.test.ts +1 -86
- package/tests/runtimeLifecycle.test.ts +69 -17
- package/tests/static-serving/ordering.test.ts +214 -0
- package/tests/static-serving/provider.test.ts +83 -0
- package/tests/static-serving/static.test.ts +400 -0
- package/tests/telemetry.test.ts +35 -101
- package/tests/telemetryAutopilot.test.ts +5 -146
- package/tests/telemetryCanaryCompatibility.test.ts +152 -0
- package/tests/telemetryHealthMonitor.test.ts +0 -2
- package/tests/telemetryLifecycle.test.ts +11 -253
- package/tests/telemetryRegistryQueueMatrix.test.ts +0 -1
|
@@ -0,0 +1,221 @@
|
|
|
1
|
+
import 'reflect-metadata';
|
|
2
|
+
import { z } from 'zod';
|
|
3
|
+
import {
|
|
4
|
+
buildOperationContractMap,
|
|
5
|
+
createOperationContractHash,
|
|
6
|
+
createOperationEntries,
|
|
7
|
+
DEFAULT_OPERATION_VERSION,
|
|
8
|
+
deriveOperationVersion,
|
|
9
|
+
type OperationContractSource,
|
|
10
|
+
} from '../../src/bff-policy/operationContracts';
|
|
11
|
+
|
|
12
|
+
const createSchemaHandler = (schema: z.ZodType) => {
|
|
13
|
+
const handler = () => ({ ok: true });
|
|
14
|
+
Reflect.defineMetadata('DATA', schema, handler);
|
|
15
|
+
return handler;
|
|
16
|
+
};
|
|
17
|
+
|
|
18
|
+
describe('operation contract utilities', () => {
|
|
19
|
+
test('creates deterministic operation entries and aggregate hashes', () => {
|
|
20
|
+
const handlers = [
|
|
21
|
+
{
|
|
22
|
+
name: 'beta',
|
|
23
|
+
httpMethod: 'post',
|
|
24
|
+
routePath: '/api/beta',
|
|
25
|
+
filename: '/api/module-a.ts',
|
|
26
|
+
},
|
|
27
|
+
{
|
|
28
|
+
name: 'alpha',
|
|
29
|
+
httpMethod: 'get',
|
|
30
|
+
routePath: '/api/alpha',
|
|
31
|
+
filename: '/api/module-a.ts',
|
|
32
|
+
},
|
|
33
|
+
] satisfies OperationContractSource[];
|
|
34
|
+
|
|
35
|
+
const entries = createOperationEntries(handlers);
|
|
36
|
+
expect(entries.map(item => item.name)).toEqual(['alpha', 'beta']);
|
|
37
|
+
});
|
|
38
|
+
|
|
39
|
+
test('builds per-operation contracts keyed by route and operation id', () => {
|
|
40
|
+
const handlers = [
|
|
41
|
+
{
|
|
42
|
+
name: 'getCustomer',
|
|
43
|
+
httpMethod: 'get',
|
|
44
|
+
routePath: '/api/customer',
|
|
45
|
+
filename: '/api/crm.ts',
|
|
46
|
+
},
|
|
47
|
+
{
|
|
48
|
+
name: 'listDeals',
|
|
49
|
+
httpMethod: 'get',
|
|
50
|
+
routePath: '/api/deals',
|
|
51
|
+
filename: '/api/crm.ts',
|
|
52
|
+
},
|
|
53
|
+
{
|
|
54
|
+
name: 'createInvoice',
|
|
55
|
+
httpMethod: 'post',
|
|
56
|
+
routePath: '/api/invoice',
|
|
57
|
+
filename: '/api/billing.ts',
|
|
58
|
+
},
|
|
59
|
+
] satisfies OperationContractSource[];
|
|
60
|
+
|
|
61
|
+
const contracts = buildOperationContractMap({
|
|
62
|
+
handlers,
|
|
63
|
+
requestId: 'erp-producer',
|
|
64
|
+
});
|
|
65
|
+
|
|
66
|
+
expect(contracts['GET:/api/customer']?.requestId).toBe('erp-producer');
|
|
67
|
+
expect(contracts['GET:/api/customer']?.operationVersion).toBe(1);
|
|
68
|
+
expect(contracts['operation:erp-producer:getCustomer']?.method).toBe('GET');
|
|
69
|
+
expect(contracts['POST:/api/invoice']?.schemaHash).toBeTruthy();
|
|
70
|
+
expect(contracts['GET:/api/customer']?.schemaHash).not.toBe(
|
|
71
|
+
contracts['POST:/api/invoice']?.schemaHash,
|
|
72
|
+
);
|
|
73
|
+
});
|
|
74
|
+
|
|
75
|
+
test('schema hash changes when an operation input schema changes', () => {
|
|
76
|
+
const baseRoute = {
|
|
77
|
+
name: 'createCustomer',
|
|
78
|
+
httpMethod: 'POST',
|
|
79
|
+
routePath: '/api/customer',
|
|
80
|
+
filename: '/api/crm.ts',
|
|
81
|
+
};
|
|
82
|
+
|
|
83
|
+
const contractsV1 = buildOperationContractMap({
|
|
84
|
+
handlers: [
|
|
85
|
+
{
|
|
86
|
+
...baseRoute,
|
|
87
|
+
handler: createSchemaHandler(z.object({ name: z.string() })),
|
|
88
|
+
},
|
|
89
|
+
],
|
|
90
|
+
requestId: 'crm',
|
|
91
|
+
});
|
|
92
|
+
const contractsV2 = buildOperationContractMap({
|
|
93
|
+
handlers: [
|
|
94
|
+
{
|
|
95
|
+
...baseRoute,
|
|
96
|
+
handler: createSchemaHandler(
|
|
97
|
+
z.object({ name: z.string(), email: z.string() }),
|
|
98
|
+
),
|
|
99
|
+
},
|
|
100
|
+
],
|
|
101
|
+
requestId: 'crm',
|
|
102
|
+
});
|
|
103
|
+
const contractsV1Again = buildOperationContractMap({
|
|
104
|
+
handlers: [
|
|
105
|
+
{
|
|
106
|
+
...baseRoute,
|
|
107
|
+
handler: createSchemaHandler(z.object({ name: z.string() })),
|
|
108
|
+
},
|
|
109
|
+
],
|
|
110
|
+
requestId: 'crm',
|
|
111
|
+
});
|
|
112
|
+
|
|
113
|
+
// changing the zod schema (the actual API contract) rotates the hash...
|
|
114
|
+
expect(contractsV1['POST:/api/customer']!.schemaHash).not.toBe(
|
|
115
|
+
contractsV2['POST:/api/customer']!.schemaHash,
|
|
116
|
+
);
|
|
117
|
+
// ...and the same schema produces the same hash.
|
|
118
|
+
expect(contractsV1['POST:/api/customer']!.schemaHash).toBe(
|
|
119
|
+
contractsV1Again['POST:/api/customer']!.schemaHash,
|
|
120
|
+
);
|
|
121
|
+
});
|
|
122
|
+
|
|
123
|
+
test('per-operation hashes are stable across route reordering', () => {
|
|
124
|
+
const handlerA = {
|
|
125
|
+
name: 'alpha',
|
|
126
|
+
httpMethod: 'GET',
|
|
127
|
+
routePath: '/api/alpha',
|
|
128
|
+
filename: '/api/module.ts',
|
|
129
|
+
handler: createSchemaHandler(z.object({ q: z.string() })),
|
|
130
|
+
};
|
|
131
|
+
const handlerB = {
|
|
132
|
+
name: 'beta',
|
|
133
|
+
httpMethod: 'POST',
|
|
134
|
+
routePath: '/api/beta',
|
|
135
|
+
filename: '/api/module.ts',
|
|
136
|
+
};
|
|
137
|
+
|
|
138
|
+
const ordered = buildOperationContractMap({
|
|
139
|
+
handlers: [handlerA, handlerB],
|
|
140
|
+
requestId: 'crm',
|
|
141
|
+
});
|
|
142
|
+
const reversed = buildOperationContractMap({
|
|
143
|
+
handlers: [handlerB, handlerA],
|
|
144
|
+
requestId: 'crm',
|
|
145
|
+
});
|
|
146
|
+
|
|
147
|
+
expect(ordered['GET:/api/alpha']!.schemaHash).toBe(
|
|
148
|
+
reversed['GET:/api/alpha']!.schemaHash,
|
|
149
|
+
);
|
|
150
|
+
expect(ordered['POST:/api/beta']!.schemaHash).toBe(
|
|
151
|
+
reversed['POST:/api/beta']!.schemaHash,
|
|
152
|
+
);
|
|
153
|
+
});
|
|
154
|
+
|
|
155
|
+
test('adding an unrelated operation does not rotate sibling hashes', () => {
|
|
156
|
+
const existing = {
|
|
157
|
+
name: 'getCustomer',
|
|
158
|
+
httpMethod: 'GET',
|
|
159
|
+
routePath: '/api/customer',
|
|
160
|
+
filename: '/api/crm.ts',
|
|
161
|
+
};
|
|
162
|
+
const before = buildOperationContractMap({
|
|
163
|
+
handlers: [existing],
|
|
164
|
+
requestId: 'crm',
|
|
165
|
+
});
|
|
166
|
+
const after = buildOperationContractMap({
|
|
167
|
+
handlers: [
|
|
168
|
+
existing,
|
|
169
|
+
{
|
|
170
|
+
name: 'deleteCustomer',
|
|
171
|
+
httpMethod: 'DELETE',
|
|
172
|
+
routePath: '/api/customer/:id',
|
|
173
|
+
filename: '/api/crm.ts',
|
|
174
|
+
},
|
|
175
|
+
],
|
|
176
|
+
requestId: 'crm',
|
|
177
|
+
});
|
|
178
|
+
|
|
179
|
+
// regression guard for the old per-module grouping, where adding any
|
|
180
|
+
// endpoint to a lambda file rotated every sibling operation hash.
|
|
181
|
+
expect(after['GET:/api/customer']!.schemaHash).toBe(
|
|
182
|
+
before['GET:/api/customer']!.schemaHash,
|
|
183
|
+
);
|
|
184
|
+
});
|
|
185
|
+
|
|
186
|
+
test('hashes are scoped by requestId', () => {
|
|
187
|
+
const handler = {
|
|
188
|
+
name: 'getCustomer',
|
|
189
|
+
httpMethod: 'GET',
|
|
190
|
+
routePath: '/api/customer',
|
|
191
|
+
};
|
|
192
|
+
const a = createOperationContractHash(handler, 'producer-a');
|
|
193
|
+
const b = createOperationContractHash(handler, 'producer-b');
|
|
194
|
+
expect(a).not.toBe(b);
|
|
195
|
+
});
|
|
196
|
+
|
|
197
|
+
test('buildOperationContractMap propagates the operation version', () => {
|
|
198
|
+
const contracts = buildOperationContractMap({
|
|
199
|
+
handlers: [
|
|
200
|
+
{
|
|
201
|
+
name: 'getCustomer',
|
|
202
|
+
httpMethod: 'GET',
|
|
203
|
+
routePath: '/api/customer',
|
|
204
|
+
},
|
|
205
|
+
],
|
|
206
|
+
requestId: 'crm',
|
|
207
|
+
operationVersion: 4,
|
|
208
|
+
});
|
|
209
|
+
expect(contracts['GET:/api/customer']!.operationVersion).toBe(4);
|
|
210
|
+
});
|
|
211
|
+
|
|
212
|
+
test('deriveOperationVersion derives the semver major', () => {
|
|
213
|
+
expect(deriveOperationVersion('2.3.1')).toBe(2);
|
|
214
|
+
expect(deriveOperationVersion('v3.0.0')).toBe(3);
|
|
215
|
+
expect(deriveOperationVersion('0.4.2')).toBe(0);
|
|
216
|
+
expect(deriveOperationVersion('not-a-version')).toBe(
|
|
217
|
+
DEFAULT_OPERATION_VERSION,
|
|
218
|
+
);
|
|
219
|
+
expect(deriveOperationVersion(undefined)).toBe(DEFAULT_OPERATION_VERSION);
|
|
220
|
+
});
|
|
221
|
+
});
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import path from 'node:path';
|
|
2
|
+
import { build } from 'esbuild';
|
|
3
|
+
|
|
4
|
+
describe('zod stays an optional peer', () => {
|
|
5
|
+
test.each([
|
|
6
|
+
'src/bff-policy/index.ts',
|
|
7
|
+
'src/bff-policy/node.ts',
|
|
8
|
+
])('%s bundles without an eager zod dependency', async entry => {
|
|
9
|
+
await expect(
|
|
10
|
+
build({
|
|
11
|
+
bundle: true,
|
|
12
|
+
entryPoints: [path.resolve(__dirname, '../..', entry)],
|
|
13
|
+
format: 'esm',
|
|
14
|
+
packages: 'external',
|
|
15
|
+
platform: 'node',
|
|
16
|
+
plugins: [
|
|
17
|
+
{
|
|
18
|
+
name: 'reject-eager-zod',
|
|
19
|
+
setup(buildApi) {
|
|
20
|
+
buildApi.onResolve({ filter: /^zod$/ }, () => {
|
|
21
|
+
throw new Error('optional zod peer entered the eager graph');
|
|
22
|
+
});
|
|
23
|
+
},
|
|
24
|
+
},
|
|
25
|
+
],
|
|
26
|
+
write: false,
|
|
27
|
+
}),
|
|
28
|
+
).resolves.toBeDefined();
|
|
29
|
+
});
|
|
30
|
+
});
|
|
@@ -0,0 +1,134 @@
|
|
|
1
|
+
import {
|
|
2
|
+
buildOperationContractMap,
|
|
3
|
+
type OperationContractSource,
|
|
4
|
+
} from '../../src/bff-policy/operationContracts';
|
|
5
|
+
import { resolveCrossProjectPolicy } from '../../src/bff-policy/resolveCrossProjectPolicy';
|
|
6
|
+
|
|
7
|
+
const createHandlers = (): Array<
|
|
8
|
+
OperationContractSource & { routeName: string }
|
|
9
|
+
> => [
|
|
10
|
+
{
|
|
11
|
+
name: 'getHello',
|
|
12
|
+
httpMethod: 'GET',
|
|
13
|
+
routeName: '/hello',
|
|
14
|
+
routePath: '/hello',
|
|
15
|
+
filename: 'api/hello.ts',
|
|
16
|
+
handler: () => 'hello',
|
|
17
|
+
},
|
|
18
|
+
];
|
|
19
|
+
|
|
20
|
+
describe('resolveCrossProjectPolicy', () => {
|
|
21
|
+
test('returns undefined without policy config or cross-project marker', () => {
|
|
22
|
+
expect(
|
|
23
|
+
resolveCrossProjectPolicy({
|
|
24
|
+
handlers: createHandlers(),
|
|
25
|
+
requestId: 'crm',
|
|
26
|
+
}),
|
|
27
|
+
).toBeUndefined();
|
|
28
|
+
});
|
|
29
|
+
|
|
30
|
+
test('enables policy with strict defaults for cross-project servers', () => {
|
|
31
|
+
const resolved = resolveCrossProjectPolicy({
|
|
32
|
+
handlers: createHandlers(),
|
|
33
|
+
isCrossProjectServer: true,
|
|
34
|
+
});
|
|
35
|
+
|
|
36
|
+
expect(resolved).toMatchObject({
|
|
37
|
+
enabled: true,
|
|
38
|
+
requireEnvelope: true,
|
|
39
|
+
requireOperationContext: true,
|
|
40
|
+
requireOperationContextDetails: true,
|
|
41
|
+
requireOperationSchemaHash: true,
|
|
42
|
+
requireOperationVersion: true,
|
|
43
|
+
allowUnknownOperations: false,
|
|
44
|
+
});
|
|
45
|
+
});
|
|
46
|
+
|
|
47
|
+
test('keeps explicit user switches over derived defaults', () => {
|
|
48
|
+
const resolved = resolveCrossProjectPolicy({
|
|
49
|
+
crossProjectPolicy: {
|
|
50
|
+
enabled: false,
|
|
51
|
+
requireEnvelope: false,
|
|
52
|
+
allowUnknownOperations: true,
|
|
53
|
+
denyStatus: 451,
|
|
54
|
+
},
|
|
55
|
+
handlers: createHandlers(),
|
|
56
|
+
isCrossProjectServer: true,
|
|
57
|
+
});
|
|
58
|
+
|
|
59
|
+
expect(resolved).toMatchObject({
|
|
60
|
+
enabled: false,
|
|
61
|
+
requireEnvelope: false,
|
|
62
|
+
allowUnknownOperations: true,
|
|
63
|
+
denyStatus: 451,
|
|
64
|
+
});
|
|
65
|
+
});
|
|
66
|
+
|
|
67
|
+
test('derives operation contracts from handlers and requestId', () => {
|
|
68
|
+
const handlers = createHandlers();
|
|
69
|
+
const resolved = resolveCrossProjectPolicy({
|
|
70
|
+
crossProjectPolicy: { enabled: true },
|
|
71
|
+
handlers,
|
|
72
|
+
requestId: 'crm',
|
|
73
|
+
});
|
|
74
|
+
|
|
75
|
+
expect(resolved?.expectedOperationContracts).toEqual(
|
|
76
|
+
buildOperationContractMap({ handlers, requestId: 'crm' }),
|
|
77
|
+
);
|
|
78
|
+
});
|
|
79
|
+
|
|
80
|
+
test('falls back to the default requestId when blank', () => {
|
|
81
|
+
const handlers = createHandlers();
|
|
82
|
+
const resolved = resolveCrossProjectPolicy({
|
|
83
|
+
crossProjectPolicy: { enabled: true },
|
|
84
|
+
handlers,
|
|
85
|
+
requestId: ' ',
|
|
86
|
+
});
|
|
87
|
+
|
|
88
|
+
expect(resolved?.expectedOperationContracts).toEqual(
|
|
89
|
+
buildOperationContractMap({ handlers, requestId: 'default' }),
|
|
90
|
+
);
|
|
91
|
+
});
|
|
92
|
+
|
|
93
|
+
test('generated contracts override user-provided entries for the same key', () => {
|
|
94
|
+
const handlers = createHandlers();
|
|
95
|
+
const generated = buildOperationContractMap({
|
|
96
|
+
handlers,
|
|
97
|
+
requestId: 'crm',
|
|
98
|
+
});
|
|
99
|
+
const resolved = resolveCrossProjectPolicy({
|
|
100
|
+
crossProjectPolicy: {
|
|
101
|
+
enabled: true,
|
|
102
|
+
expectedOperationContracts: {
|
|
103
|
+
'GET:/hello': { schemaHash: 'user-pinned', operationVersion: 9 },
|
|
104
|
+
'POST:/extra': { schemaHash: 'kept', operationVersion: 2 },
|
|
105
|
+
},
|
|
106
|
+
},
|
|
107
|
+
handlers,
|
|
108
|
+
requestId: 'crm',
|
|
109
|
+
});
|
|
110
|
+
|
|
111
|
+
expect(resolved?.expectedOperationContracts['GET:/hello']).toEqual(
|
|
112
|
+
generated['GET:/hello'],
|
|
113
|
+
);
|
|
114
|
+
expect(resolved?.expectedOperationContracts['POST:/extra']).toEqual({
|
|
115
|
+
schemaHash: 'kept',
|
|
116
|
+
operationVersion: 2,
|
|
117
|
+
});
|
|
118
|
+
});
|
|
119
|
+
});
|
|
120
|
+
|
|
121
|
+
describe('resolveCrossProjectPolicy operation version', () => {
|
|
122
|
+
test('derived operation version flows into generated contracts', () => {
|
|
123
|
+
const resolved = resolveCrossProjectPolicy({
|
|
124
|
+
handlers: createHandlers(),
|
|
125
|
+
isCrossProjectServer: true,
|
|
126
|
+
requestId: 'crm',
|
|
127
|
+
operationVersion: 5,
|
|
128
|
+
});
|
|
129
|
+
|
|
130
|
+
expect(resolved?.expectedOperationContracts['GET:/hello']).toMatchObject({
|
|
131
|
+
operationVersion: 5,
|
|
132
|
+
});
|
|
133
|
+
});
|
|
134
|
+
});
|
|
@@ -89,64 +89,6 @@ describe('contract gate observer', () => {
|
|
|
89
89
|
}
|
|
90
90
|
});
|
|
91
91
|
|
|
92
|
-
test('fails stale gate snapshots without manual intervention', async () => {
|
|
93
|
-
const dir = makeTempDir();
|
|
94
|
-
const snapshotPath = path.join(dir, 'contract-gates.json');
|
|
95
|
-
|
|
96
|
-
const registry = new TelemetryRegistry({
|
|
97
|
-
service: 'svc',
|
|
98
|
-
module: 'server',
|
|
99
|
-
environment: 'test',
|
|
100
|
-
flushIntervalMs: 60_000,
|
|
101
|
-
});
|
|
102
|
-
const monitor = new TelemetryHealthMonitor({
|
|
103
|
-
registry,
|
|
104
|
-
minConsecutiveFailedEvaluations: 1,
|
|
105
|
-
});
|
|
106
|
-
|
|
107
|
-
const staleUpdatedAt = Date.now() - 10_000;
|
|
108
|
-
fs.writeFileSync(
|
|
109
|
-
snapshotPath,
|
|
110
|
-
JSON.stringify(
|
|
111
|
-
{
|
|
112
|
-
schemaVersion: 1,
|
|
113
|
-
updatedAt: staleUpdatedAt,
|
|
114
|
-
gates: {
|
|
115
|
-
'module-onboarding-certification-gates': {
|
|
116
|
-
passed: true,
|
|
117
|
-
updatedAt: staleUpdatedAt,
|
|
118
|
-
},
|
|
119
|
-
},
|
|
120
|
-
},
|
|
121
|
-
null,
|
|
122
|
-
2,
|
|
123
|
-
),
|
|
124
|
-
);
|
|
125
|
-
|
|
126
|
-
const observer = new ContractGateSnapshotObserver({
|
|
127
|
-
monitor,
|
|
128
|
-
gateSnapshotPath: snapshotPath,
|
|
129
|
-
gateStaleAfterMs: 1_000,
|
|
130
|
-
});
|
|
131
|
-
|
|
132
|
-
try {
|
|
133
|
-
await observer.syncOnce();
|
|
134
|
-
const decision = monitor.evaluate();
|
|
135
|
-
expect(decision.state).toBe('unhealthy');
|
|
136
|
-
expect(
|
|
137
|
-
decision.failures.some(
|
|
138
|
-
item =>
|
|
139
|
-
item.reason === 'contract_gate_failed' &&
|
|
140
|
-
item.gate === 'module-onboarding-certification-gates',
|
|
141
|
-
),
|
|
142
|
-
).toBe(true);
|
|
143
|
-
} finally {
|
|
144
|
-
observer.stop();
|
|
145
|
-
await registry.shutdown();
|
|
146
|
-
fs.rmSync(dir, { recursive: true, force: true });
|
|
147
|
-
}
|
|
148
|
-
});
|
|
149
|
-
|
|
150
92
|
test('marks unchanged gate snapshots stale after the stale window elapses', async () => {
|
|
151
93
|
const dir = makeTempDir();
|
|
152
94
|
const snapshotPath = path.join(dir, 'contract-gates.json');
|
package/tests/env.test.ts
CHANGED
|
@@ -4,70 +4,51 @@ import {
|
|
|
4
4
|
} from '../src/env';
|
|
5
5
|
|
|
6
6
|
describe('parseServerRuntimeExtensionsEnv', () => {
|
|
7
|
-
test(
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
expect(parsed.modernEnv).toBe('staging');
|
|
26
|
-
expect(parsed.nodeEnv).toBe('production');
|
|
27
|
-
expect(parsed.environmentName).toBe('staging');
|
|
28
|
-
expect(parsed.contractGatesFile).toBe('/var/run/gates.json');
|
|
29
|
-
});
|
|
30
|
-
|
|
31
|
-
test('falls back from MODERN_ENV to NODE_ENV to the default', () => {
|
|
32
|
-
expect(
|
|
33
|
-
parseServerRuntimeExtensionsEnv({ NODE_ENV: 'production' })
|
|
34
|
-
.environmentName,
|
|
35
|
-
).toBe('production');
|
|
36
|
-
expect(
|
|
37
|
-
parseServerRuntimeExtensionsEnv({
|
|
38
|
-
MODERN_ENV: 'preview',
|
|
7
|
+
test.each([
|
|
8
|
+
{
|
|
9
|
+
name: 'uses the documented default when values are blank',
|
|
10
|
+
env: {
|
|
11
|
+
MODERN_ENV: ' ',
|
|
12
|
+
NODE_ENV: '',
|
|
13
|
+
MODERN_CONTRACT_GATES_FILE: ' ',
|
|
14
|
+
},
|
|
15
|
+
expectedEnvironment: DEFAULT_ENVIRONMENT_NAME,
|
|
16
|
+
expectedModernEnv: undefined,
|
|
17
|
+
expectedNodeEnv: undefined,
|
|
18
|
+
expectedContractGatesFile: undefined,
|
|
19
|
+
},
|
|
20
|
+
{
|
|
21
|
+
name: 'trims configured values and prefers MODERN_ENV',
|
|
22
|
+
env: {
|
|
23
|
+
MODERN_ENV: ' staging ',
|
|
39
24
|
NODE_ENV: 'production',
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
} else {
|
|
69
|
-
process.env.MODERN_CONTRACT_GATES_FILE = previous;
|
|
70
|
-
}
|
|
71
|
-
}
|
|
25
|
+
MODERN_CONTRACT_GATES_FILE: ' /var/run/gates.json ',
|
|
26
|
+
},
|
|
27
|
+
expectedEnvironment: 'staging',
|
|
28
|
+
expectedModernEnv: 'staging',
|
|
29
|
+
expectedNodeEnv: 'production',
|
|
30
|
+
expectedContractGatesFile: '/var/run/gates.json',
|
|
31
|
+
},
|
|
32
|
+
{
|
|
33
|
+
name: 'falls back to NODE_ENV when MODERN_ENV is absent',
|
|
34
|
+
env: { NODE_ENV: 'production' },
|
|
35
|
+
expectedEnvironment: 'production',
|
|
36
|
+
expectedModernEnv: undefined,
|
|
37
|
+
expectedNodeEnv: 'production',
|
|
38
|
+
expectedContractGatesFile: undefined,
|
|
39
|
+
},
|
|
40
|
+
])('$name', ({
|
|
41
|
+
env,
|
|
42
|
+
expectedEnvironment,
|
|
43
|
+
expectedModernEnv,
|
|
44
|
+
expectedNodeEnv,
|
|
45
|
+
expectedContractGatesFile,
|
|
46
|
+
}) => {
|
|
47
|
+
const parsed = parseServerRuntimeExtensionsEnv(env);
|
|
48
|
+
|
|
49
|
+
expect(parsed.environmentName).toBe(expectedEnvironment);
|
|
50
|
+
expect(parsed.modernEnv).toBe(expectedModernEnv);
|
|
51
|
+
expect(parsed.nodeEnv).toBe(expectedNodeEnv);
|
|
52
|
+
expect(parsed.contractGatesFile).toBe(expectedContractGatesFile);
|
|
72
53
|
});
|
|
73
54
|
});
|
|
@@ -0,0 +1,120 @@
|
|
|
1
|
+
import { handleRequest } from '@modern-js/plugin-data-loader/runtime';
|
|
2
|
+
import {
|
|
3
|
+
createServerBase,
|
|
4
|
+
getLoaderCtx,
|
|
5
|
+
type MiddlewareHandler,
|
|
6
|
+
type ServerPlugin,
|
|
7
|
+
} from '@modern-js/server-core';
|
|
8
|
+
import type { NestedRoute } from '@modern-js/types';
|
|
9
|
+
import { injectLocalisedLoaderPlugin } from '../src/localisedLoaderPlugin';
|
|
10
|
+
import { getDefaultAppContext, getDefaultConfig } from './helpers';
|
|
11
|
+
|
|
12
|
+
const sourceRoutes: NestedRoute[] = [
|
|
13
|
+
{
|
|
14
|
+
type: 'nested',
|
|
15
|
+
id: 'search',
|
|
16
|
+
path: ':lang/search',
|
|
17
|
+
loader: ({ params, request }) => ({
|
|
18
|
+
language: params.lang,
|
|
19
|
+
query: new URL(request.url).searchParams.get('q'),
|
|
20
|
+
}),
|
|
21
|
+
},
|
|
22
|
+
{
|
|
23
|
+
type: 'nested',
|
|
24
|
+
id: 'resource',
|
|
25
|
+
path: ':lang/resources/:id',
|
|
26
|
+
loader: ({ params }) => Response.json(params),
|
|
27
|
+
action: ({ params }) => Response.json({ action: params.id }),
|
|
28
|
+
},
|
|
29
|
+
];
|
|
30
|
+
// Shape emitted by the localized route generator, retaining each source ID.
|
|
31
|
+
const routes = [
|
|
32
|
+
...sourceRoutes,
|
|
33
|
+
...sourceRoutes.map(route => ({
|
|
34
|
+
...route,
|
|
35
|
+
id: `${route.id}-cs`,
|
|
36
|
+
path: route.path
|
|
37
|
+
?.replace('search', 'hledat')
|
|
38
|
+
.replace('resources', 'zdroje'),
|
|
39
|
+
modernLocalisedRoute: { id: route.id },
|
|
40
|
+
})),
|
|
41
|
+
];
|
|
42
|
+
|
|
43
|
+
const render: MiddlewareHandler = async context =>
|
|
44
|
+
(await handleRequest({
|
|
45
|
+
request: context.req.raw,
|
|
46
|
+
routes,
|
|
47
|
+
serverRoutes: [
|
|
48
|
+
{
|
|
49
|
+
urlPath: '/base',
|
|
50
|
+
entryName: 'main',
|
|
51
|
+
entryPath: 'index.html',
|
|
52
|
+
isSSR: true,
|
|
53
|
+
},
|
|
54
|
+
],
|
|
55
|
+
context: {
|
|
56
|
+
reporter: {},
|
|
57
|
+
loaderContext: getLoaderCtx(context),
|
|
58
|
+
monitors: { timing() {} },
|
|
59
|
+
} as Parameters<typeof handleRequest>[0]['context'],
|
|
60
|
+
})) ?? context.notFound();
|
|
61
|
+
|
|
62
|
+
const fixtureRenderPlugin: ServerPlugin = {
|
|
63
|
+
name: 'fixture-loader-render',
|
|
64
|
+
setup(api) {
|
|
65
|
+
api.onPrepare(() => {
|
|
66
|
+
api.getServerContext().middlewares.push({
|
|
67
|
+
name: 'render',
|
|
68
|
+
handler: render,
|
|
69
|
+
});
|
|
70
|
+
});
|
|
71
|
+
},
|
|
72
|
+
};
|
|
73
|
+
|
|
74
|
+
let server: ReturnType<typeof createServerBase>;
|
|
75
|
+
|
|
76
|
+
beforeAll(async () => {
|
|
77
|
+
server = createServerBase({
|
|
78
|
+
config: getDefaultConfig(),
|
|
79
|
+
pwd: process.cwd(),
|
|
80
|
+
appContext: getDefaultAppContext(),
|
|
81
|
+
});
|
|
82
|
+
// Register after the render plugin to exercise native middleware ordering.
|
|
83
|
+
server.addPlugins([fixtureRenderPlugin, injectLocalisedLoaderPlugin()]);
|
|
84
|
+
await server.init();
|
|
85
|
+
});
|
|
86
|
+
|
|
87
|
+
afterAll(async () => {
|
|
88
|
+
await server.dispose();
|
|
89
|
+
});
|
|
90
|
+
|
|
91
|
+
async function load(path: string, routeId: string, method = 'GET') {
|
|
92
|
+
const url = new URL(path, 'https://example.test');
|
|
93
|
+
url.searchParams.set('__loader', routeId);
|
|
94
|
+
return server.request(url.toString(), { method });
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
test('serves a canonical loader ID at its localized URL with basepath and deferred data', async () => {
|
|
98
|
+
const response = await load('/base/cs/hledat?q=tractor', 'search');
|
|
99
|
+
expect(response?.status).toBe(200);
|
|
100
|
+
expect(response?.headers.get('content-type')).toContain(
|
|
101
|
+
'text/modernjs-deferred',
|
|
102
|
+
);
|
|
103
|
+
expect(await response?.text()).toContain('"language":"cs","query":"tractor"');
|
|
104
|
+
});
|
|
105
|
+
|
|
106
|
+
test('preserves encoded parameters for localized loaders and actions', async () => {
|
|
107
|
+
const response = await load('/base/cs/zdroje/a%2Fb', 'resource');
|
|
108
|
+
expect(response?.status).toBe(200);
|
|
109
|
+
expect(await response?.json()).toEqual({ lang: 'cs', id: 'a/b' });
|
|
110
|
+
const action = await load('/base/cs/zdroje/a%2Fb', 'resource', 'POST');
|
|
111
|
+
expect(action?.status).toBe(200);
|
|
112
|
+
expect(await action?.json()).toEqual({ action: 'a/b' });
|
|
113
|
+
});
|
|
114
|
+
|
|
115
|
+
test('retains canonical routes and rejects loader IDs from another route', async () => {
|
|
116
|
+
expect((await load('/base/en/resources/item', 'resource'))?.status).toBe(200);
|
|
117
|
+
expect((await load('/base/cs/hledat', 'resource'))?.status).toBe(403);
|
|
118
|
+
expect((await load('/base/cs/zdroje/item', 'search'))?.status).toBe(403);
|
|
119
|
+
expect((await load('/base/cs/hledat', 'missing'))?.status).toBe(403);
|
|
120
|
+
});
|