@bleedingdev/modern-js-server-runtime-extensions 3.9.0-ultramodern.2 → 3.9.0-ultramodern.20
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
|
@@ -0,0 +1,193 @@
|
|
|
1
|
+
import type { Middleware } from '@modern-js/server-core';
|
|
2
|
+
import { fs } from '@modern-js/utils';
|
|
3
|
+
|
|
4
|
+
type SupportedEncoding = 'br' | 'gzip';
|
|
5
|
+
|
|
6
|
+
type ResolvePreCompressedAssetResult = {
|
|
7
|
+
selected: {
|
|
8
|
+
filepath: string;
|
|
9
|
+
encoding: SupportedEncoding;
|
|
10
|
+
} | null;
|
|
11
|
+
hasVariant: boolean;
|
|
12
|
+
acceptable: boolean;
|
|
13
|
+
};
|
|
14
|
+
|
|
15
|
+
const PRE_COMPRESSED_ASSET_EXTENSIONS: Record<SupportedEncoding, string> = {
|
|
16
|
+
br: '.br',
|
|
17
|
+
gzip: '.gz',
|
|
18
|
+
};
|
|
19
|
+
|
|
20
|
+
const PRE_COMPRESSED_SUPPORTED_ENCODINGS: SupportedEncoding[] = ['br', 'gzip'];
|
|
21
|
+
|
|
22
|
+
const QUALITY_VALUE_PATTERN = /^(?:0(?:\.\d{0,3})?|1(?:\.0{0,3})?)$/u;
|
|
23
|
+
|
|
24
|
+
const parseAcceptEncoding = (value: string) =>
|
|
25
|
+
value
|
|
26
|
+
.split(',')
|
|
27
|
+
.map(item => item.trim())
|
|
28
|
+
.filter(Boolean)
|
|
29
|
+
.map(item => {
|
|
30
|
+
const [rawName, ...params] = item.split(';');
|
|
31
|
+
const name = rawName.trim().toLowerCase();
|
|
32
|
+
let q = 1;
|
|
33
|
+
let qualitySeen = false;
|
|
34
|
+
|
|
35
|
+
for (const param of params) {
|
|
36
|
+
const [key, rawValue] = param.split('=').map(v => v.trim());
|
|
37
|
+
if (key.toLowerCase() !== 'q') {
|
|
38
|
+
continue;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
if (
|
|
42
|
+
qualitySeen ||
|
|
43
|
+
rawValue == null ||
|
|
44
|
+
!QUALITY_VALUE_PATTERN.test(rawValue)
|
|
45
|
+
) {
|
|
46
|
+
q = 0;
|
|
47
|
+
break;
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
qualitySeen = true;
|
|
51
|
+
q = Number(rawValue);
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
return {
|
|
55
|
+
name,
|
|
56
|
+
q,
|
|
57
|
+
};
|
|
58
|
+
});
|
|
59
|
+
|
|
60
|
+
const getAcceptedRepresentations = (
|
|
61
|
+
value: string | null | undefined,
|
|
62
|
+
): Array<SupportedEncoding | 'identity'> => {
|
|
63
|
+
if (!value) {
|
|
64
|
+
return ['identity'];
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
const parsed = parseAcceptEncoding(value);
|
|
68
|
+
const qualityByEncoding = new Map<string, number>();
|
|
69
|
+
let wildcardQuality: number | undefined;
|
|
70
|
+
|
|
71
|
+
for (const { name, q } of parsed) {
|
|
72
|
+
if (name === '*') {
|
|
73
|
+
wildcardQuality = q;
|
|
74
|
+
continue;
|
|
75
|
+
}
|
|
76
|
+
qualityByEncoding.set(name, q);
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
const getQuality = (encoding: SupportedEncoding) => {
|
|
80
|
+
const explicit = qualityByEncoding.get(encoding);
|
|
81
|
+
if (explicit !== undefined) {
|
|
82
|
+
return explicit;
|
|
83
|
+
}
|
|
84
|
+
return wildcardQuality ?? 0;
|
|
85
|
+
};
|
|
86
|
+
|
|
87
|
+
const identityQuality =
|
|
88
|
+
qualityByEncoding.get('identity') ?? wildcardQuality ?? 1;
|
|
89
|
+
|
|
90
|
+
return [
|
|
91
|
+
...PRE_COMPRESSED_SUPPORTED_ENCODINGS.map(encoding => ({
|
|
92
|
+
encoding,
|
|
93
|
+
quality: getQuality(encoding),
|
|
94
|
+
})),
|
|
95
|
+
{
|
|
96
|
+
encoding: 'identity' as const,
|
|
97
|
+
quality: identityQuality,
|
|
98
|
+
},
|
|
99
|
+
]
|
|
100
|
+
.filter(item => item.quality > 0)
|
|
101
|
+
.sort((a, b) => b.quality - a.quality)
|
|
102
|
+
.map(item => item.encoding);
|
|
103
|
+
};
|
|
104
|
+
|
|
105
|
+
const appendVaryHeader = (
|
|
106
|
+
c: Parameters<Middleware>[0],
|
|
107
|
+
value: string,
|
|
108
|
+
): void => {
|
|
109
|
+
const current = c.res.headers.get('Vary');
|
|
110
|
+
|
|
111
|
+
if (!current) {
|
|
112
|
+
c.header('Vary', value);
|
|
113
|
+
return;
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
const values = current
|
|
117
|
+
.split(',')
|
|
118
|
+
.map(item => item.trim().toLowerCase())
|
|
119
|
+
.filter(Boolean);
|
|
120
|
+
|
|
121
|
+
if (!values.includes(value.toLowerCase())) {
|
|
122
|
+
c.header('Vary', `${current}, ${value}`);
|
|
123
|
+
}
|
|
124
|
+
};
|
|
125
|
+
|
|
126
|
+
export const resolvePreCompressedAsset = async (
|
|
127
|
+
c: Parameters<Middleware>[0],
|
|
128
|
+
filepath: string,
|
|
129
|
+
): Promise<ResolvePreCompressedAssetResult> => {
|
|
130
|
+
const brPath = `${filepath}${PRE_COMPRESSED_ASSET_EXTENSIONS.br}`;
|
|
131
|
+
const gzipPath = `${filepath}${PRE_COMPRESSED_ASSET_EXTENSIONS.gzip}`;
|
|
132
|
+
|
|
133
|
+
const [hasBr, hasGzip] = await Promise.all([
|
|
134
|
+
fs.pathExists(brPath),
|
|
135
|
+
fs.pathExists(gzipPath),
|
|
136
|
+
]);
|
|
137
|
+
|
|
138
|
+
const hasVariant = hasBr || hasGzip;
|
|
139
|
+
const acceptedRepresentations = getAcceptedRepresentations(
|
|
140
|
+
c.req.header('accept-encoding'),
|
|
141
|
+
);
|
|
142
|
+
|
|
143
|
+
for (const encoding of acceptedRepresentations) {
|
|
144
|
+
if (encoding === 'identity') {
|
|
145
|
+
return {
|
|
146
|
+
selected: null,
|
|
147
|
+
hasVariant,
|
|
148
|
+
acceptable: true,
|
|
149
|
+
};
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
if (encoding === 'br' && hasBr) {
|
|
153
|
+
return {
|
|
154
|
+
selected: {
|
|
155
|
+
filepath: brPath,
|
|
156
|
+
encoding,
|
|
157
|
+
},
|
|
158
|
+
hasVariant: true,
|
|
159
|
+
acceptable: true,
|
|
160
|
+
};
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
if (encoding === 'gzip' && hasGzip) {
|
|
164
|
+
return {
|
|
165
|
+
selected: {
|
|
166
|
+
filepath: gzipPath,
|
|
167
|
+
encoding,
|
|
168
|
+
},
|
|
169
|
+
hasVariant: true,
|
|
170
|
+
acceptable: true,
|
|
171
|
+
};
|
|
172
|
+
}
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
return {
|
|
176
|
+
selected: null,
|
|
177
|
+
hasVariant,
|
|
178
|
+
acceptable: false,
|
|
179
|
+
};
|
|
180
|
+
};
|
|
181
|
+
|
|
182
|
+
export const applyPreCompressedAssetHeaders = (
|
|
183
|
+
c: Parameters<Middleware>[0],
|
|
184
|
+
preCompressedAsset: ResolvePreCompressedAssetResult,
|
|
185
|
+
) => {
|
|
186
|
+
if (preCompressedAsset.hasVariant || !preCompressedAsset.acceptable) {
|
|
187
|
+
appendVaryHeader(c, 'Accept-Encoding');
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
if (preCompressedAsset.selected) {
|
|
191
|
+
c.header('Content-Encoding', preCompressedAsset.selected.encoding);
|
|
192
|
+
}
|
|
193
|
+
};
|
|
@@ -153,7 +153,7 @@ export function createVictoriaMetricsTelemetryExporter(
|
|
|
153
153
|
};
|
|
154
154
|
}
|
|
155
155
|
|
|
156
|
-
export function
|
|
156
|
+
export function warnOnMetricsOtlpEndpoint(endpoint: string | undefined) {
|
|
157
157
|
if (!endpoint || !endpoint.includes('/v1/metrics')) {
|
|
158
158
|
return;
|
|
159
159
|
}
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import type { ServerTelemetryUserConfig } from '@modern-js/server-
|
|
1
|
+
import type { ServerTelemetryUserConfig } from '@modern-js/runtime-extensions/server-config';
|
|
2
2
|
import {
|
|
3
3
|
type ContractGateSnapshotStore,
|
|
4
4
|
DEFAULT_CONTRACT_GATE_SNAPSHOT_PATH,
|
|
@@ -26,7 +26,7 @@ import {
|
|
|
26
26
|
type SetupTelemetryHealthMonitoringOptions = {
|
|
27
27
|
registry: TelemetryRegistry;
|
|
28
28
|
appDirectory: string;
|
|
29
|
-
|
|
29
|
+
healthConfig: ServerTelemetryUserConfig['health'] | undefined;
|
|
30
30
|
};
|
|
31
31
|
|
|
32
32
|
type SetupTelemetryHealthMonitoringResult = {
|
|
@@ -59,28 +59,26 @@ function emitHealthTransitionMetric(
|
|
|
59
59
|
export const setupTelemetryHealthMonitoring = ({
|
|
60
60
|
registry,
|
|
61
61
|
appDirectory,
|
|
62
|
-
|
|
62
|
+
healthConfig,
|
|
63
63
|
}: SetupTelemetryHealthMonitoringOptions): SetupTelemetryHealthMonitoringResult => {
|
|
64
|
-
if (!
|
|
64
|
+
if (!healthConfig?.enabled) {
|
|
65
65
|
return {};
|
|
66
66
|
}
|
|
67
67
|
|
|
68
|
-
const contractGates =
|
|
68
|
+
const contractGates = healthConfig.contractGates as
|
|
69
69
|
| Record<string, boolean | { passed: boolean; reason?: string }>
|
|
70
70
|
| undefined;
|
|
71
71
|
|
|
72
72
|
const healthMonitor = new TelemetryHealthMonitor({
|
|
73
73
|
registry,
|
|
74
|
-
evaluationIntervalMs:
|
|
74
|
+
evaluationIntervalMs: healthConfig.evaluationIntervalMs,
|
|
75
75
|
minConsecutiveHealthyEvaluations:
|
|
76
|
-
|
|
77
|
-
// `rollbackConsecutiveFailures` is the legacy public config key. It now
|
|
78
|
-
// controls health-state hysteresis and never triggers a deployment action.
|
|
76
|
+
healthConfig.minConsecutiveHealthyEvaluations,
|
|
79
77
|
minConsecutiveFailedEvaluations:
|
|
80
|
-
|
|
81
|
-
maxQueueUtilization:
|
|
82
|
-
maxTotalDropped:
|
|
83
|
-
maxUnhealthyExporters:
|
|
78
|
+
healthConfig.minConsecutiveFailedEvaluations,
|
|
79
|
+
maxQueueUtilization: healthConfig.maxQueueUtilization,
|
|
80
|
+
maxTotalDropped: healthConfig.maxTotalDropped,
|
|
81
|
+
maxUnhealthyExporters: healthConfig.maxUnhealthyExporters,
|
|
84
82
|
requiredContractGates: Object.keys(contractGates || {}),
|
|
85
83
|
onTransition: evaluation => {
|
|
86
84
|
emitHealthTransitionMetric(registry, evaluation);
|
|
@@ -94,22 +92,21 @@ export const setupTelemetryHealthMonitoring = ({
|
|
|
94
92
|
let gateSnapshotStorePromise: Promise<ContractGateSnapshotStore> | undefined;
|
|
95
93
|
let runtimeFallbackSignalConfig: RuntimeFallbackSignalConfig | undefined;
|
|
96
94
|
|
|
97
|
-
// `autopilot` is retained only as the legacy config key for snapshot input.
|
|
98
95
|
const snapshotObservationEnabled =
|
|
99
|
-
|
|
96
|
+
healthConfig.snapshotObservation?.enabled ?? true;
|
|
100
97
|
if (snapshotObservationEnabled) {
|
|
101
98
|
const gateSnapshotPath = resolveContractGateSnapshotPath(
|
|
102
99
|
appDirectory,
|
|
103
|
-
|
|
100
|
+
healthConfig.snapshotObservation?.gateSnapshotPath,
|
|
104
101
|
);
|
|
105
102
|
gateSnapshotStorePromise = resolveContractGateSnapshotStore({
|
|
106
103
|
appDirectory,
|
|
107
104
|
gateSnapshotPath: gateSnapshotPath || DEFAULT_CONTRACT_GATE_SNAPSHOT_PATH,
|
|
108
|
-
stateStore:
|
|
105
|
+
stateStore: healthConfig.snapshotObservation?.stateStore,
|
|
109
106
|
});
|
|
110
107
|
|
|
111
108
|
const runtimeSignalConfig =
|
|
112
|
-
|
|
109
|
+
healthConfig.snapshotObservation?.runtimeFallbackSignal;
|
|
113
110
|
if (runtimeSignalConfig?.enabled === true) {
|
|
114
111
|
runtimeFallbackSignalConfig = {
|
|
115
112
|
endpoint: resolveRuntimeFallbackSignalEndpoint(
|
|
@@ -141,11 +138,11 @@ export const setupTelemetryHealthMonitoring = ({
|
|
|
141
138
|
}
|
|
142
139
|
|
|
143
140
|
// Detailed runtime status stays authenticated even when signal ingestion is
|
|
144
|
-
// disabled.
|
|
141
|
+
// disabled.
|
|
145
142
|
const runtimeStatusAuthConfig =
|
|
146
143
|
runtimeFallbackSignalConfig?.auth ??
|
|
147
144
|
normalizeRuntimeFallbackSignalAuthConfig(
|
|
148
|
-
|
|
145
|
+
healthConfig.snapshotObservation?.runtimeFallbackSignal?.auth,
|
|
149
146
|
);
|
|
150
147
|
|
|
151
148
|
return {
|
|
@@ -1,4 +1,5 @@
|
|
|
1
|
-
import type { ServerTelemetryUserConfig } from '@modern-js/server-
|
|
1
|
+
import type { ServerTelemetryUserConfig } from '@modern-js/runtime-extensions/server-config';
|
|
2
|
+
import { logger } from '@modern-js/utils';
|
|
2
3
|
import {
|
|
3
4
|
type ContractGateSnapshotStore,
|
|
4
5
|
resolveContractGateSnapshotPath,
|
|
@@ -7,13 +8,13 @@ import { ContractGateSnapshotObserver } from '../contractGateSnapshotObserver';
|
|
|
7
8
|
import {
|
|
8
9
|
createOtlpTelemetryExporter,
|
|
9
10
|
createVictoriaMetricsTelemetryExporter,
|
|
10
|
-
maybeWarnLegacyOtlpEndpoint,
|
|
11
11
|
TelemetryHealthMonitor,
|
|
12
12
|
TelemetryRegistry,
|
|
13
|
+
warnOnMetricsOtlpEndpoint,
|
|
13
14
|
} from '../telemetryCore';
|
|
14
15
|
|
|
15
16
|
type TelemetryLifecycleApi = {
|
|
16
|
-
|
|
17
|
+
onDispose: (disposer: () => Promise<void>) => () => void;
|
|
17
18
|
onPrepare: (prepare: () => Promise<void>) => void;
|
|
18
19
|
};
|
|
19
20
|
|
|
@@ -21,7 +22,7 @@ type RegisterTelemetryLifecycleOptions = {
|
|
|
21
22
|
api: TelemetryLifecycleApi;
|
|
22
23
|
registry: TelemetryRegistry;
|
|
23
24
|
telemetryConfig: ServerTelemetryUserConfig;
|
|
24
|
-
|
|
25
|
+
healthConfig: ServerTelemetryUserConfig['health'] | undefined;
|
|
25
26
|
healthMonitor?: TelemetryHealthMonitor;
|
|
26
27
|
gateSnapshotStorePromise?: Promise<ContractGateSnapshotStore>;
|
|
27
28
|
appDirectory: string;
|
|
@@ -41,7 +42,7 @@ const ensureTelemetryBeforeExitHook = () => {
|
|
|
41
42
|
telemetryBeforeExitHookInstalled = true;
|
|
42
43
|
process.on('beforeExit', () => {
|
|
43
44
|
for (const close of [...activeTelemetryLaneClosers]) {
|
|
44
|
-
void close();
|
|
45
|
+
void close().catch((error: unknown) => logger.error(error));
|
|
45
46
|
}
|
|
46
47
|
});
|
|
47
48
|
};
|
|
@@ -50,51 +51,38 @@ export const registerTelemetryLifecycle = ({
|
|
|
50
51
|
api,
|
|
51
52
|
registry,
|
|
52
53
|
telemetryConfig,
|
|
53
|
-
|
|
54
|
+
healthConfig,
|
|
54
55
|
healthMonitor,
|
|
55
56
|
gateSnapshotStorePromise,
|
|
56
57
|
appDirectory,
|
|
57
58
|
}: RegisterTelemetryLifecycleOptions) => {
|
|
58
59
|
let contractGateSnapshotObserver: ContractGateSnapshotObserver | undefined;
|
|
59
|
-
let
|
|
60
|
-
const closeTelemetryLane =
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
await registry.shutdown();
|
|
60
|
+
let closePromise: Promise<void> | undefined;
|
|
61
|
+
const closeTelemetryLane = () => {
|
|
62
|
+
closePromise ??= Promise.resolve().then(async () => {
|
|
63
|
+
activeTelemetryLaneClosers.delete(closeTelemetryLane);
|
|
64
|
+
contractGateSnapshotObserver?.stop();
|
|
65
|
+
healthMonitor?.stop();
|
|
66
|
+
await registry.shutdown();
|
|
67
|
+
});
|
|
68
|
+
return closePromise;
|
|
69
69
|
};
|
|
70
70
|
|
|
71
|
+
// Registration precedes exporter startup and any later plugin failure.
|
|
72
|
+
// Native prod/dev shutdown and runtime retirement share this disposal path.
|
|
73
|
+
api.onDispose(closeTelemetryLane);
|
|
74
|
+
activeTelemetryLaneClosers.add(closeTelemetryLane);
|
|
75
|
+
ensureTelemetryBeforeExitHook();
|
|
76
|
+
|
|
71
77
|
let prepared = false;
|
|
72
78
|
api.onPrepare(async () => {
|
|
73
|
-
if (prepared) {
|
|
79
|
+
if (prepared || closePromise) {
|
|
74
80
|
return;
|
|
75
81
|
}
|
|
76
82
|
prepared = true;
|
|
77
83
|
|
|
78
|
-
// Shutdown path for the telemetry lane: flush pending envelopes and
|
|
79
|
-
// stop health/snapshot pollers when the node server closes (this also
|
|
80
|
-
// covers dev-server restarts, which close the previous node server
|
|
81
|
-
// before assembling a new one), with process beforeExit as the
|
|
82
|
-
// final-flush floor when no node server handle exists.
|
|
83
|
-
const { nodeServer } = api.getServerContext() as {
|
|
84
|
-
nodeServer?: {
|
|
85
|
-
once?: (event: string, listener: () => void) => unknown;
|
|
86
|
-
};
|
|
87
|
-
};
|
|
88
|
-
if (nodeServer && typeof nodeServer.once === 'function') {
|
|
89
|
-
nodeServer.once('close', () => {
|
|
90
|
-
void closeTelemetryLane();
|
|
91
|
-
});
|
|
92
|
-
}
|
|
93
|
-
activeTelemetryLaneClosers.add(closeTelemetryLane);
|
|
94
|
-
ensureTelemetryBeforeExitHook();
|
|
95
|
-
|
|
96
84
|
if (telemetryConfig.exporters?.otlp?.enabled) {
|
|
97
|
-
|
|
85
|
+
warnOnMetricsOtlpEndpoint(telemetryConfig.exporters.otlp.endpoint);
|
|
98
86
|
await registry.register(
|
|
99
87
|
createOtlpTelemetryExporter(telemetryConfig.exporters.otlp),
|
|
100
88
|
);
|
|
@@ -123,11 +111,11 @@ export const registerTelemetryLifecycle = ({
|
|
|
123
111
|
monitor: healthMonitor,
|
|
124
112
|
gateSnapshotPath: resolveContractGateSnapshotPath(
|
|
125
113
|
appDirectory,
|
|
126
|
-
|
|
114
|
+
healthConfig?.snapshotObservation?.gateSnapshotPath,
|
|
127
115
|
),
|
|
128
116
|
gateSnapshotStore,
|
|
129
|
-
pollIntervalMs:
|
|
130
|
-
gateStaleAfterMs:
|
|
117
|
+
pollIntervalMs: healthConfig?.snapshotObservation?.pollIntervalMs,
|
|
118
|
+
gateStaleAfterMs: healthConfig?.snapshotObservation?.gateStaleAfterMs,
|
|
131
119
|
});
|
|
132
120
|
}
|
|
133
121
|
if (contractGateSnapshotObserver) {
|
package/src/telemetry/plugin.ts
CHANGED
|
@@ -1,10 +1,10 @@
|
|
|
1
|
-
import { parseTraceparent } from '@modern-js/
|
|
1
|
+
import { parseTraceparent } from '@modern-js/runtime-extensions/request-context';
|
|
2
|
+
import type { ServerTelemetryUserConfig } from '@modern-js/runtime-extensions/server-config';
|
|
2
3
|
import type {
|
|
3
4
|
Context,
|
|
4
5
|
Next,
|
|
5
6
|
ServerEnv,
|
|
6
7
|
ServerPlugin,
|
|
7
|
-
ServerTelemetryUserConfig,
|
|
8
8
|
} from '@modern-js/server-core';
|
|
9
9
|
import type { CoreMonitor } from '@modern-js/types';
|
|
10
10
|
import { logger } from '@modern-js/utils';
|
|
@@ -98,6 +98,8 @@ export const resolveTelemetrySloOptions = (
|
|
|
98
98
|
|
|
99
99
|
export const injectTelemetryPlugin = (): ServerPlugin => ({
|
|
100
100
|
name: '@modern-js/inject-telemetry',
|
|
101
|
+
pre: ['@modern-js/plugin-inject-route'],
|
|
102
|
+
post: ['@modern-js/plugin-inject-config-middleware'],
|
|
101
103
|
setup(api) {
|
|
102
104
|
const serverConfig = api.getServerConfig();
|
|
103
105
|
const telemetryConfig = serverConfig?.server?.telemetry;
|
|
@@ -139,7 +141,7 @@ export const injectTelemetryPlugin = (): ServerPlugin => ({
|
|
|
139
141
|
} = setupTelemetryHealthMonitoring({
|
|
140
142
|
registry,
|
|
141
143
|
appDirectory,
|
|
142
|
-
|
|
144
|
+
healthConfig: telemetryConfig.health,
|
|
143
145
|
});
|
|
144
146
|
|
|
145
147
|
if (runtimeFallbackSignalConfig) {
|
|
@@ -188,7 +190,7 @@ export const injectTelemetryPlugin = (): ServerPlugin => ({
|
|
|
188
190
|
api,
|
|
189
191
|
registry,
|
|
190
192
|
telemetryConfig,
|
|
191
|
-
|
|
193
|
+
healthConfig: telemetryConfig.health,
|
|
192
194
|
healthMonitor,
|
|
193
195
|
gateSnapshotStorePromise,
|
|
194
196
|
appDirectory,
|
|
@@ -111,6 +111,7 @@ export const createRuntimeStatusMiddleware = ({
|
|
|
111
111
|
runtimeStatusAuthConfig,
|
|
112
112
|
);
|
|
113
113
|
|
|
114
|
+
const healthSnapshot = healthMonitor?.getStatusSnapshot();
|
|
114
115
|
return c.json({
|
|
115
116
|
ok: true,
|
|
116
117
|
timestamp: Date.now(),
|
|
@@ -118,10 +119,10 @@ export const createRuntimeStatusMiddleware = ({
|
|
|
118
119
|
queueStats: registry.getQueueStats(),
|
|
119
120
|
exporterHealth: registry.getExporterHealth(),
|
|
120
121
|
},
|
|
121
|
-
health:
|
|
122
|
+
health: healthSnapshot
|
|
122
123
|
? {
|
|
123
124
|
enabled: true,
|
|
124
|
-
...
|
|
125
|
+
...healthSnapshot,
|
|
125
126
|
}
|
|
126
127
|
: { enabled: false },
|
|
127
128
|
runtimeFallbackSignal: runtimeFallbackSignalConfig
|
package/src/telemetryCore.ts
CHANGED
|
@@ -7,9 +7,9 @@ export {
|
|
|
7
7
|
export {
|
|
8
8
|
createOtlpTelemetryExporter,
|
|
9
9
|
createVictoriaMetricsTelemetryExporter,
|
|
10
|
-
maybeWarnLegacyOtlpEndpoint,
|
|
11
10
|
type OtlpExporterOptions,
|
|
12
11
|
type VictoriaMetricsExporterOptions,
|
|
12
|
+
warnOnMetricsOtlpEndpoint,
|
|
13
13
|
} from './telemetry/exporters';
|
|
14
14
|
export {
|
|
15
15
|
type TelemetryContractGateStatus,
|
|
@@ -414,3 +414,66 @@ describe('verified backend federation entry loading', () => {
|
|
|
414
414
|
);
|
|
415
415
|
});
|
|
416
416
|
});
|
|
417
|
+
|
|
418
|
+
describe('shared acquisition deadline', () => {
|
|
419
|
+
test.each([
|
|
420
|
+
'resource',
|
|
421
|
+
'entry',
|
|
422
|
+
] as const)('%s settles despite noncooperative stream cancellation', async kind => {
|
|
423
|
+
let cancelled = false;
|
|
424
|
+
const fetch = async () =>
|
|
425
|
+
new Response(
|
|
426
|
+
new ReadableStream<Uint8Array>({
|
|
427
|
+
cancel() {
|
|
428
|
+
cancelled = true;
|
|
429
|
+
return new Promise(() => {});
|
|
430
|
+
},
|
|
431
|
+
}),
|
|
432
|
+
);
|
|
433
|
+
const policy = { fetch, timeoutMs: 10 };
|
|
434
|
+
const operation =
|
|
435
|
+
kind === 'resource'
|
|
436
|
+
? loadBoundedBackendFederationResource(entryUrl, policy)
|
|
437
|
+
: loadVerifiedNodeEntry({
|
|
438
|
+
...policy,
|
|
439
|
+
remote: { entry: entryUrl, name: remoteName },
|
|
440
|
+
verification: verification(),
|
|
441
|
+
});
|
|
442
|
+
await expect(operation).rejects.toMatchObject({ code: 'timeout' });
|
|
443
|
+
expect(cancelled).toBe(true);
|
|
444
|
+
});
|
|
445
|
+
|
|
446
|
+
test('the entry deadline includes SHA-256 and never evaluates after expiration', async () => {
|
|
447
|
+
const evaluateCommonJs = rs.fn();
|
|
448
|
+
const digest = rs
|
|
449
|
+
.spyOn(globalThis.crypto.subtle, 'digest')
|
|
450
|
+
.mockImplementation(() => new Promise(() => {}));
|
|
451
|
+
try {
|
|
452
|
+
await expect(
|
|
453
|
+
loadVerifiedBackendFederationEntry({
|
|
454
|
+
fetch: async () => new Response(source),
|
|
455
|
+
timeoutMs: 10,
|
|
456
|
+
remote: { entry: entryUrl, name: remoteName },
|
|
457
|
+
verification: verification(),
|
|
458
|
+
evaluateCommonJs,
|
|
459
|
+
}),
|
|
460
|
+
).rejects.toMatchObject({ code: 'timeout' });
|
|
461
|
+
expect(evaluateCommonJs).not.toHaveBeenCalled();
|
|
462
|
+
} finally {
|
|
463
|
+
digest.mockRestore();
|
|
464
|
+
}
|
|
465
|
+
});
|
|
466
|
+
|
|
467
|
+
test('zero disables the timer while retaining exact-byte verification', async () => {
|
|
468
|
+
const entry = await loadVerifiedNodeEntry({
|
|
469
|
+
fetch: async () => {
|
|
470
|
+
await new Promise(resolve => setTimeout(resolve, 20));
|
|
471
|
+
return new Response(source);
|
|
472
|
+
},
|
|
473
|
+
timeoutMs: 0,
|
|
474
|
+
remote: { entry: entryUrl, name: remoteName },
|
|
475
|
+
verification: verification(),
|
|
476
|
+
});
|
|
477
|
+
expect(typeof entry.get).toBe('function');
|
|
478
|
+
});
|
|
479
|
+
});
|