@bleedingdev/modern-js-server-runtime-extensions 3.9.0-ultramodern.9 → 3.9.3-ultramodern.2
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 +3 -1
- package/dist/cjs/backend-federation-security/index.js +73 -152
- package/dist/cjs/bff-policy/index.js +7 -0
- package/dist/cjs/bff-policy/node.js +20 -4
- package/dist/cjs/bff-policy/operationContracts.js +150 -135
- package/dist/cjs/bff-policy/operationIdentity.js +81 -0
- package/dist/cjs/bff-policy/producer.js +79 -0
- package/dist/cjs/localisedLoader.js +65 -0
- package/dist/cjs/localisedLoaderPlugin.js +2 -2
- package/dist/cjs/module-federation-css/remote.js +2 -1
- package/dist/cjs/static-serving/staticModuleFederation.js +49 -12
- package/dist/esm/backend-federation-security/index.mjs +73 -152
- package/dist/esm/bff-policy/index.mjs +1 -0
- package/dist/esm/bff-policy/operationIdentity.mjs +30 -0
- package/dist/esm/bff-policy/producer.mjs +31 -0
- package/dist/esm/localisedLoader.mjs +24 -0
- package/dist/esm/localisedLoaderPlugin.mjs +2 -2
- package/dist/esm/module-federation-css/remote.mjs +2 -1
- package/dist/esm-node/backend-federation-security/index.mjs +73 -152
- package/dist/esm-node/bff-policy/index.mjs +1 -0
- package/dist/esm-node/bff-policy/node.mjs +1 -0
- package/dist/esm-node/bff-policy/operationContracts.mjs +5 -27
- package/dist/esm-node/bff-policy/operationIdentity.mjs +31 -0
- package/dist/esm-node/bff-policy/producer.mjs +32 -0
- package/dist/esm-node/localisedLoader.mjs +25 -0
- package/dist/esm-node/localisedLoaderPlugin.mjs +2 -2
- package/dist/esm-node/module-federation-css/remote.mjs +2 -1
- package/dist/esm-node/static-serving/staticModuleFederation.mjs +49 -12
- package/dist/types/backend-federation-security/node.d.ts +1 -1
- package/dist/types/bff-policy/index.d.ts +1 -0
- package/dist/types/bff-policy/node.d.ts +1 -0
- package/dist/types/bff-policy/operationContracts.d.ts +2 -41
- package/dist/types/bff-policy/operationIdentity.d.ts +34 -0
- package/dist/types/bff-policy/producer.d.ts +10 -0
- package/dist/types/localisedLoader.d.ts +15 -0
- package/dist/types/module-federation-css/remote.d.ts +2 -0
- package/dist/types/serverConfig.d.ts +3 -1
- package/dist/types-esm/backend-federation-security/node.d.mts +1 -1
- package/dist/types-esm/bff-policy/index.d.mts +1 -0
- package/dist/types-esm/bff-policy/node.d.mts +1 -0
- package/dist/types-esm/bff-policy/operationContracts.d.mts +2 -41
- 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/localisedLoader.d.mts +15 -0
- package/dist/types-esm/module-federation-css/remote.d.mts +2 -0
- package/dist/types-esm/serverConfig.d.mts +3 -1
- package/package.json +47 -19
- package/src/backend-federation-security/index.ts +151 -318
- package/src/backend-federation-security/node.ts +24 -22
- package/src/bff-policy/index.ts +1 -0
- package/src/bff-policy/node.ts +1 -0
- package/src/bff-policy/operationContracts.ts +13 -86
- package/src/bff-policy/operationIdentity.ts +90 -0
- package/src/bff-policy/producer.ts +40 -0
- package/src/contract-gate-snapshot-store/resolve.ts +0 -1
- package/src/localisedLoader.ts +50 -0
- package/src/localisedLoaderPlugin.ts +6 -7
- package/src/module-federation-css/remote.ts +3 -1
- package/src/serverConfig.ts +2 -0
- package/src/static-serving/staticModuleFederation.ts +127 -22
- package/rslib.config.mts +0 -38
- package/rstest.config.mts +0 -18
- package/tests/backendFederationSecurity.test.ts +0 -416
- package/tests/bff-policy/crossProjectPolicy.matrix.test.ts +0 -450
- package/tests/bff-policy/crossProjectPolicy.test.ts +0 -197
- package/tests/bff-policy/operationContracts.test.ts +0 -221
- package/tests/bff-policy/optionalZodPeer.test.ts +0 -30
- package/tests/bff-policy/resolveCrossProjectPolicy.test.ts +0 -76
- package/tests/contractGateSnapshotObserver.test.ts +0 -347
- package/tests/contractGateSnapshotStore.test.ts +0 -225
- package/tests/env.test.ts +0 -54
- package/tests/helpers.ts +0 -20
- package/tests/localisedLoader.test.ts +0 -120
- package/tests/mfCache.test.ts +0 -63
- package/tests/moduleFederationCss.test.ts +0 -348
- package/tests/nativeErrorResponse.test.ts +0 -118
- package/tests/nativeServerAssembly.test.ts +0 -385
- package/tests/runtimeLifecycle.test.ts +0 -159
- package/tests/static-serving/ordering.test.ts +0 -214
- package/tests/static-serving/static.test.ts +0 -411
- package/tests/telemetry.test.ts +0 -248
- package/tests/telemetryHealthMonitor.test.ts +0 -183
- package/tests/telemetryHealthObservation.test.ts +0 -302
- package/tests/telemetryLifecycle.test.ts +0 -123
- package/tests/telemetryRegistryQueueMatrix.test.ts +0 -130
- package/tests/telemetryTraceparent.test.ts +0 -167
- package/tests/tsconfig.json +0 -11
- package/tsconfig.json +0 -10
|
@@ -143,97 +143,85 @@ const sha256Hex = async (bytes, signal)=>{
|
|
|
143
143
|
const digest = new Uint8Array(await readWithAbort(subtle.digest('SHA-256', bytes), signal));
|
|
144
144
|
return Array.from(digest, (byte)=>byte.toString(16).padStart(2, '0')).join('');
|
|
145
145
|
};
|
|
146
|
-
async function
|
|
147
|
-
const
|
|
148
|
-
assertAllowedEntryUrl(requestedUrl, true === options.allowInsecureHttp);
|
|
149
|
-
const maxBytes = options.maxBytes ?? DEFAULT_MAX_RESOURCE_BYTES;
|
|
150
|
-
if (!Number.isSafeInteger(maxBytes) || maxBytes <= 0) throw new BackendFederationRemoteEntryError('invalid_verification', '[Module Federation] Backend federation resource maxBytes must be a positive safe integer.');
|
|
146
|
+
async function acquireBackendFederationResource(requestedUrl, options, policy) {
|
|
147
|
+
const { label, maxBytes, exactByteLength } = policy;
|
|
151
148
|
const timeoutMs = options.timeoutMs ?? DEFAULT_ENTRY_TIMEOUT_MS;
|
|
152
|
-
if (!Number.isSafeInteger(timeoutMs) || timeoutMs < 0) throw new BackendFederationRemoteEntryError('invalid_verification', '[Module Federation]
|
|
149
|
+
if (!Number.isSafeInteger(maxBytes) || maxBytes <= 0 || !Number.isSafeInteger(timeoutMs) || timeoutMs < 0) throw new BackendFederationRemoteEntryError('invalid_verification', '[Module Federation] Resource maxBytes must be a positive safe integer and timeoutMs a non-negative safe integer.');
|
|
150
|
+
if (void 0 !== exactByteLength && exactByteLength > maxBytes) throw new BackendFederationRemoteEntryError('entry_too_large', `[Module Federation] ${label} exceeds the ${maxBytes}-byte policy limit.`);
|
|
153
151
|
const controller = new AbortController();
|
|
154
152
|
let timedOut = false;
|
|
155
|
-
|
|
156
|
-
const onCallerAbort = ()=>{
|
|
157
|
-
callerAborted = true;
|
|
158
|
-
controller.abort(options.signal?.reason);
|
|
159
|
-
};
|
|
153
|
+
const onCallerAbort = ()=>controller.abort(options.signal?.reason);
|
|
160
154
|
if (options.signal?.aborted) onCallerAbort();
|
|
161
155
|
else options.signal?.addEventListener('abort', onCallerAbort, {
|
|
162
156
|
once: true
|
|
163
157
|
});
|
|
164
158
|
const timeout = timeoutMs > 0 ? setTimeout(()=>{
|
|
165
159
|
timedOut = true;
|
|
166
|
-
controller.abort(new Error(
|
|
160
|
+
controller.abort(new Error(`${label} timed out after ${timeoutMs}ms`));
|
|
167
161
|
}, timeoutMs) : void 0;
|
|
168
|
-
const fetchResource = options.fetch ?? globalThis.fetch;
|
|
169
162
|
let reader;
|
|
170
|
-
|
|
171
|
-
const cancellationError = (cause)=>new BackendFederationRemoteEntryError(timedOut ? 'timeout' : 'aborted', timedOut ? `[Module Federation] Backend federation ${resourceKind} timed out after ${timeoutMs}ms.` : `[Module Federation] Backend federation ${resourceKind} loading was aborted.`, {
|
|
172
|
-
cause
|
|
173
|
-
});
|
|
163
|
+
let response;
|
|
174
164
|
try {
|
|
175
|
-
|
|
176
|
-
if (
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
}
|
|
189
|
-
}
|
|
190
|
-
if (!response.ok) throw new BackendFederationRemoteEntryError('fetch_failed', `[Module Federation] Backend federation ${resourceKind} returned HTTP ${response.status}.`);
|
|
191
|
-
if (response.url) {
|
|
192
|
-
const responseUrl = canonicalEntryUrl(response.url);
|
|
193
|
-
if (responseUrl.href !== requestedUrl.href) throw new BackendFederationRemoteEntryError('redirect_mismatch', `[Module Federation] Backend federation ${resourceKind} redirected outside its trusted URL.`, {
|
|
194
|
-
details: {
|
|
195
|
-
expected: redactBackendFederationUrl(requestedUrl.href),
|
|
196
|
-
received: redactBackendFederationUrl(responseUrl.href)
|
|
197
|
-
}
|
|
198
|
-
});
|
|
199
|
-
}
|
|
165
|
+
const fetchResource = options.fetch ?? globalThis.fetch;
|
|
166
|
+
if (controller.signal.aborted) throw controller.signal.reason;
|
|
167
|
+
if ('function' != typeof fetchResource) throw new Error('Loading requires fetch.');
|
|
168
|
+
response = await readWithAbort(fetchResource(requestedUrl.href, {
|
|
169
|
+
cache: 'no-store',
|
|
170
|
+
redirect: 'error',
|
|
171
|
+
signal: controller.signal
|
|
172
|
+
}), controller.signal);
|
|
173
|
+
if (!response.ok) throw new BackendFederationRemoteEntryError('fetch_failed', `[Module Federation] ${label} returned HTTP ${response.status}.`);
|
|
174
|
+
if (response.url && canonicalEntryUrl(response.url).href !== requestedUrl.href) throw new BackendFederationRemoteEntryError('redirect_mismatch', `[Module Federation] ${label} redirected outside its trusted URL.`, {
|
|
175
|
+
details: {
|
|
176
|
+
expected: redactBackendFederationUrl(requestedUrl.href),
|
|
177
|
+
received: redactBackendFederationUrl(response.url)
|
|
178
|
+
}
|
|
179
|
+
});
|
|
200
180
|
const contentLength = parseContentLength(response.headers?.get('content-length') ?? null);
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
throw new BackendFederationRemoteEntryError('entry_too_large', `[Module Federation] Backend federation ${resourceKind} declares ${contentLength} bytes, exceeding its ${maxBytes}-byte policy limit.`);
|
|
205
|
-
}
|
|
181
|
+
const byteLimit = exactByteLength ?? maxBytes;
|
|
182
|
+
if (void 0 !== contentLength && contentLength > maxBytes) throw new BackendFederationRemoteEntryError('entry_too_large', `[Module Federation] ${label} declares ${contentLength} bytes, exceeding its ${maxBytes}-byte policy limit.`);
|
|
183
|
+
const exactBytes = void 0 === exactByteLength ? void 0 : new Uint8Array(exactByteLength);
|
|
206
184
|
const chunks = [];
|
|
207
185
|
let byteLength = 0;
|
|
186
|
+
const append = (chunk)=>{
|
|
187
|
+
if (byteLength + chunk.byteLength > byteLimit) throw new BackendFederationRemoteEntryError('entry_too_large', `[Module Federation] ${label} exceeded its ${byteLimit}-byte limit while streaming.`);
|
|
188
|
+
if (exactBytes) exactBytes.set(chunk, byteLength);
|
|
189
|
+
else chunks.push(chunk);
|
|
190
|
+
byteLength += chunk.byteLength;
|
|
191
|
+
};
|
|
208
192
|
if (response.body) {
|
|
209
193
|
reader = response.body.getReader();
|
|
210
194
|
while(true){
|
|
211
195
|
const result = await readWithAbort(reader.read(), controller.signal);
|
|
212
196
|
if (result.done) break;
|
|
213
|
-
|
|
214
|
-
if (byteLength > maxBytes) throw new BackendFederationRemoteEntryError('entry_too_large', `[Module Federation] Backend federation ${resourceKind} exceeded its ${maxBytes}-byte limit while streaming.`);
|
|
215
|
-
chunks.push(result.value);
|
|
197
|
+
append(result.value);
|
|
216
198
|
}
|
|
217
|
-
} else if (response.text)
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
199
|
+
} else if (void 0 === exactByteLength && response.text) append(new TextEncoder().encode(await readWithAbort(response.text(), controller.signal)));
|
|
200
|
+
if (void 0 !== exactByteLength && byteLength !== exactByteLength) throw new BackendFederationRemoteEntryError('byte_length_mismatch', `[Module Federation] ${label} byte length mismatch.`, {
|
|
201
|
+
details: {
|
|
202
|
+
expected: exactByteLength,
|
|
203
|
+
received: byteLength
|
|
204
|
+
}
|
|
205
|
+
});
|
|
206
|
+
const bytes = exactBytes ?? new Uint8Array(byteLength);
|
|
224
207
|
let offset = 0;
|
|
225
208
|
for (const chunk of chunks){
|
|
226
209
|
bytes.set(chunk, offset);
|
|
227
210
|
offset += chunk.byteLength;
|
|
228
211
|
}
|
|
229
|
-
if (controller.signal.aborted) throw
|
|
212
|
+
if (controller.signal.aborted) throw controller.signal.reason;
|
|
213
|
+
if (policy.verify) await readWithAbort(policy.verify(bytes, controller.signal), controller.signal);
|
|
230
214
|
return bytes;
|
|
231
215
|
} catch (cause) {
|
|
232
|
-
|
|
233
|
-
|
|
216
|
+
const cancelled = controller.signal.aborted;
|
|
217
|
+
controller.abort(cause);
|
|
218
|
+
if (reader) reader.cancel(cause).catch(()=>void 0);
|
|
219
|
+
else if (response?.body) response.body.cancel(cause).catch(()=>void 0);
|
|
220
|
+
if (cancelled) throw new BackendFederationRemoteEntryError(timedOut ? 'timeout' : 'aborted', timedOut ? `[Module Federation] ${label} timed out after ${timeoutMs}ms.` : `[Module Federation] ${label} loading was aborted.`, {
|
|
221
|
+
cause
|
|
222
|
+
});
|
|
234
223
|
if (cause instanceof BackendFederationRemoteEntryError) throw cause;
|
|
235
|
-
|
|
236
|
-
throw new BackendFederationRemoteEntryError('fetch_failed', `[Module Federation] Backend federation ${resourceKind} loading failed.`, {
|
|
224
|
+
throw new BackendFederationRemoteEntryError('fetch_failed', `[Module Federation] ${label} loading failed.`, {
|
|
237
225
|
cause
|
|
238
226
|
});
|
|
239
227
|
} finally{
|
|
@@ -241,6 +229,14 @@ async function loadBoundedBackendFederationResource(resourceUrl, options = {}) {
|
|
|
241
229
|
options.signal?.removeEventListener('abort', onCallerAbort);
|
|
242
230
|
}
|
|
243
231
|
}
|
|
232
|
+
async function loadBoundedBackendFederationResource(resourceUrl, options = {}) {
|
|
233
|
+
const requestedUrl = canonicalEntryUrl(resourceUrl);
|
|
234
|
+
assertAllowedEntryUrl(requestedUrl, true === options.allowInsecureHttp);
|
|
235
|
+
return acquireBackendFederationResource(requestedUrl, options, {
|
|
236
|
+
label: `Backend federation ${options.kind ?? 'manifest'}`,
|
|
237
|
+
maxBytes: options.maxBytes ?? DEFAULT_MAX_RESOURCE_BYTES
|
|
238
|
+
});
|
|
239
|
+
}
|
|
244
240
|
const evaluateBackendFederationCommonJsEntry = (remote, bytes, evaluator)=>{
|
|
245
241
|
let source;
|
|
246
242
|
try {
|
|
@@ -264,99 +260,24 @@ const evaluateBackendFederationCommonJsEntry = (remote, bytes, evaluator)=>{
|
|
|
264
260
|
async function loadVerifiedBackendFederationEntry(options) {
|
|
265
261
|
const verification = resolveTrustedVerification(options.verification, options.expected ?? {});
|
|
266
262
|
const requestedUrl = assertVerification(options.remote, verification, options.expected ?? {}, true === options.allowInsecureHttp);
|
|
267
|
-
const
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
const onCallerAbort = ()=>{
|
|
276
|
-
callerAborted = true;
|
|
277
|
-
controller.abort(options.signal?.reason);
|
|
278
|
-
};
|
|
279
|
-
if (options.signal?.aborted) onCallerAbort();
|
|
280
|
-
else options.signal?.addEventListener('abort', onCallerAbort, {
|
|
281
|
-
once: true
|
|
282
|
-
});
|
|
283
|
-
const timeout = timeoutMs > 0 ? setTimeout(()=>{
|
|
284
|
-
timedOut = true;
|
|
285
|
-
controller.abort(new Error(`Backend remote entry timed out after ${timeoutMs}ms`));
|
|
286
|
-
}, timeoutMs) : void 0;
|
|
287
|
-
const fetchEntry = options.fetch ?? globalThis.fetch;
|
|
288
|
-
let reader;
|
|
289
|
-
const cancellationError = (cause)=>new BackendFederationRemoteEntryError(timedOut ? 'timeout' : 'aborted', timedOut ? `[Module Federation] Backend remote ${options.remote.name} timed out after ${timeoutMs}ms.` : `[Module Federation] Backend remote ${options.remote.name} loading was aborted.`, {
|
|
290
|
-
cause
|
|
291
|
-
});
|
|
292
|
-
try {
|
|
293
|
-
if (controller.signal.aborted) throw cancellationError(controller.signal.reason);
|
|
294
|
-
if ('function' != typeof fetchEntry) throw new BackendFederationRemoteEntryError('fetch_failed', '[Module Federation] Backend remote entry loading requires fetch.');
|
|
295
|
-
let response;
|
|
296
|
-
try {
|
|
297
|
-
response = await readWithAbort(fetchEntry(requestedUrl.href, {
|
|
298
|
-
cache: 'no-store',
|
|
299
|
-
redirect: 'error',
|
|
300
|
-
signal: controller.signal
|
|
301
|
-
}), controller.signal);
|
|
302
|
-
} catch (cause) {
|
|
303
|
-
if (controller.signal.aborted) throw cancellationError(cause);
|
|
304
|
-
throw new BackendFederationRemoteEntryError('fetch_failed', `[Module Federation] Failed to fetch backend remote ${options.remote.name}.`, {
|
|
305
|
-
cause
|
|
306
|
-
});
|
|
307
|
-
}
|
|
308
|
-
if (!response.ok) throw new BackendFederationRemoteEntryError('fetch_failed', `[Module Federation] Backend remote ${options.remote.name} returned HTTP ${response.status}.`);
|
|
309
|
-
if (response.url) {
|
|
310
|
-
const responseUrl = canonicalEntryUrl(response.url);
|
|
311
|
-
if (responseUrl.href !== requestedUrl.href) throw new BackendFederationRemoteEntryError('redirect_mismatch', `[Module Federation] Backend remote ${options.remote.name} redirected outside its verified entry URL.`, {
|
|
263
|
+
const bytes = await acquireBackendFederationResource(requestedUrl, options, {
|
|
264
|
+
label: `Backend remote ${options.remote.name}`,
|
|
265
|
+
maxBytes: options.maxBytes ?? DEFAULT_MAX_ENTRY_BYTES,
|
|
266
|
+
exactByteLength: verification.byteLength,
|
|
267
|
+
async verify (bytes, signal) {
|
|
268
|
+
const actualDigest = await sha256Hex(bytes, signal);
|
|
269
|
+
if (signal.aborted) throw signal.reason;
|
|
270
|
+
if (actualDigest !== verification.sha256) throw new BackendFederationRemoteEntryError('integrity_mismatch', `[Module Federation] Backend remote ${options.remote.name} SHA-256 mismatch.`, {
|
|
312
271
|
details: {
|
|
313
|
-
expected:
|
|
314
|
-
received:
|
|
272
|
+
expected: verification.sha256,
|
|
273
|
+
received: actualDigest
|
|
315
274
|
}
|
|
316
275
|
});
|
|
317
276
|
}
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
throw new BackendFederationRemoteEntryError('entry_too_large', `[Module Federation] Backend remote ${options.remote.name} declares ${contentLength} bytes, exceeding its ${maxBytes}-byte policy limit.`);
|
|
324
|
-
}
|
|
325
|
-
reader = response.body?.getReader();
|
|
326
|
-
const bytes = new Uint8Array(verification.byteLength);
|
|
327
|
-
let byteLength = 0;
|
|
328
|
-
if (reader) while(true){
|
|
329
|
-
const result = await readWithAbort(reader.read(), controller.signal);
|
|
330
|
-
if (result.done) break;
|
|
331
|
-
const nextByteLength = byteLength + result.value.byteLength;
|
|
332
|
-
if (nextByteLength > byteLimit) throw new BackendFederationRemoteEntryError('entry_too_large', `[Module Federation] Backend remote ${options.remote.name} exceeded its ${byteLimit}-byte limit while streaming.`);
|
|
333
|
-
bytes.set(result.value, byteLength);
|
|
334
|
-
byteLength = nextByteLength;
|
|
335
|
-
}
|
|
336
|
-
if (byteLength !== verification.byteLength) throw new BackendFederationRemoteEntryError('byte_length_mismatch', `[Module Federation] Backend remote ${options.remote.name} byte length mismatch.`, {
|
|
337
|
-
details: {
|
|
338
|
-
expected: verification.byteLength,
|
|
339
|
-
received: byteLength
|
|
340
|
-
}
|
|
341
|
-
});
|
|
342
|
-
const actualDigest = await sha256Hex(bytes, controller.signal);
|
|
343
|
-
if (controller.signal.aborted) throw cancellationError(controller.signal.reason);
|
|
344
|
-
if (actualDigest !== verification.sha256) throw new BackendFederationRemoteEntryError('integrity_mismatch', `[Module Federation] Backend remote ${options.remote.name} SHA-256 mismatch.`, {
|
|
345
|
-
details: {
|
|
346
|
-
expected: verification.sha256,
|
|
347
|
-
received: actualDigest
|
|
348
|
-
}
|
|
349
|
-
});
|
|
350
|
-
return evaluateBackendFederationCommonJsEntry(options.remote, bytes, options.evaluateCommonJs);
|
|
351
|
-
} catch (cause) {
|
|
352
|
-
if (!controller.signal.aborted) controller.abort(cause);
|
|
353
|
-
await reader?.cancel(cause).catch(()=>void 0);
|
|
354
|
-
if (cause instanceof BackendFederationRemoteEntryError && !('aborted' === cause.code && timedOut)) throw cause;
|
|
355
|
-
if (controller.signal.aborted && (timedOut || callerAborted)) throw cancellationError(cause);
|
|
356
|
-
throw cause;
|
|
357
|
-
} finally{
|
|
358
|
-
if (timeout) clearTimeout(timeout);
|
|
359
|
-
options.signal?.removeEventListener('abort', onCallerAbort);
|
|
360
|
-
}
|
|
277
|
+
});
|
|
278
|
+
if (options.signal?.aborted) throw new BackendFederationRemoteEntryError('aborted', `[Module Federation] Backend remote ${options.remote.name} loading was aborted.`, {
|
|
279
|
+
cause: options.signal.reason
|
|
280
|
+
});
|
|
281
|
+
return evaluateBackendFederationCommonJsEntry(options.remote, bytes, options.evaluateCommonJs);
|
|
361
282
|
}
|
|
362
283
|
export { BackendFederationRemoteEntryError, evaluateBackendFederationCommonJsEntry, loadBoundedBackendFederationResource, loadVerifiedBackendFederationEntry, redactBackendFederationUrl, resolveBackendFederationRemoteEntryVerification };
|
|
@@ -2,24 +2,10 @@ import "node:module";
|
|
|
2
2
|
import { createRequire } from "node:module";
|
|
3
3
|
import { createHash } from "crypto";
|
|
4
4
|
import "reflect-metadata";
|
|
5
|
+
import { DEFAULT_OPERATION_VERSION, serializeOperationContract, stableOperationStringify } from "./operationIdentity.mjs";
|
|
5
6
|
import { fileURLToPath as __rspack_fileURLToPath } from "node:url";
|
|
6
7
|
var __rspack_import_meta_filename__ = __rspack_fileURLToPath(import.meta.url);
|
|
7
|
-
|
|
8
|
-
const deriveOperationVersion = (packageVersion)=>{
|
|
9
|
-
if ('string' != typeof packageVersion) return DEFAULT_OPERATION_VERSION;
|
|
10
|
-
const match = packageVersion.trim().match(/^v?(\d+)\./);
|
|
11
|
-
if (!match) return DEFAULT_OPERATION_VERSION;
|
|
12
|
-
const major = Number.parseInt(match[1], 10);
|
|
13
|
-
return Number.isInteger(major) && major >= 0 ? major : DEFAULT_OPERATION_VERSION;
|
|
14
|
-
};
|
|
15
|
-
const stableStringify = (value)=>{
|
|
16
|
-
if (Array.isArray(value)) return `[${value.map((item)=>stableStringify(item)).join(',')}]`;
|
|
17
|
-
if (value && 'object' == typeof value) {
|
|
18
|
-
const entries = Object.entries(value).filter(([, entryValue])=>void 0 !== entryValue).sort(([a], [b])=>a.localeCompare(b)).map(([key, entryValue])=>`${JSON.stringify(key)}:${stableStringify(entryValue)}`);
|
|
19
|
-
return `{${entries.join(',')}}`;
|
|
20
|
-
}
|
|
21
|
-
return JSON.stringify(value) ?? 'null';
|
|
22
|
-
};
|
|
8
|
+
export * from "./operationIdentity.mjs";
|
|
23
9
|
const sha256 = (text)=>createHash('sha256').update(text).digest('hex');
|
|
24
10
|
let cachedZodToJSONSchema;
|
|
25
11
|
const ZOD_SPECIFIER = "zod";
|
|
@@ -68,15 +54,7 @@ const serializeOperationSchemas = (handler)=>{
|
|
|
68
54
|
}
|
|
69
55
|
return Object.keys(serialized).length > 0 ? serialized : void 0;
|
|
70
56
|
};
|
|
71
|
-
const createOperationContractHash = (operation, requestId)=>sha256(
|
|
72
|
-
httpMethod: String(operation.httpMethod || '').toUpperCase(),
|
|
73
|
-
name: operation.name,
|
|
74
|
-
requestId,
|
|
75
|
-
routePath: operation.routePath,
|
|
76
|
-
...operation.schemas ? {
|
|
77
|
-
schemas: operation.schemas
|
|
78
|
-
} : {}
|
|
79
|
-
}));
|
|
57
|
+
const createOperationContractHash = (operation, requestId)=>sha256(serializeOperationContract(operation, requestId));
|
|
80
58
|
const createOperationEntries = (handlers)=>handlers.map((item)=>({
|
|
81
59
|
name: item.name,
|
|
82
60
|
httpMethod: String(item.httpMethod || '').toUpperCase(),
|
|
@@ -86,7 +64,7 @@ const createOperationEntries = (handlers)=>handlers.map((item)=>({
|
|
|
86
64
|
const keyB = `${b.routePath}:${b.httpMethod}:${b.name}`;
|
|
87
65
|
return keyA.localeCompare(keyB);
|
|
88
66
|
});
|
|
89
|
-
const createOperationSchemaHash = (operationEntries, requestId)=>sha256(
|
|
67
|
+
const createOperationSchemaHash = (operationEntries, requestId)=>sha256(stableOperationStringify({
|
|
90
68
|
operations: [
|
|
91
69
|
...operationEntries
|
|
92
70
|
].map((item)=>({
|
|
@@ -126,4 +104,4 @@ const buildOperationContractMap = ({ handlers, requestId, operationVersion })=>{
|
|
|
126
104
|
});
|
|
127
105
|
return contracts;
|
|
128
106
|
};
|
|
129
|
-
export {
|
|
107
|
+
export { buildOperationContractMap, createOperationContractHash, createOperationEntries, createOperationSchemaHash, serializeOperationSchemas };
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import "node:module";
|
|
2
|
+
const DEFAULT_OPERATION_VERSION = 1;
|
|
3
|
+
const deriveOperationVersion = (packageVersion)=>{
|
|
4
|
+
if ('string' != typeof packageVersion) return DEFAULT_OPERATION_VERSION;
|
|
5
|
+
const match = packageVersion.trim().match(/^v?(\d+)\./);
|
|
6
|
+
if (!match) return DEFAULT_OPERATION_VERSION;
|
|
7
|
+
const major = Number.parseInt(match[1], 10);
|
|
8
|
+
return Number.isInteger(major) && major >= 0 ? major : DEFAULT_OPERATION_VERSION;
|
|
9
|
+
};
|
|
10
|
+
const stableOperationStringify = (value)=>{
|
|
11
|
+
if (Array.isArray(value)) return `[${value.map((item)=>stableOperationStringify(item)).join(',')}]`;
|
|
12
|
+
if (value && 'object' == typeof value) {
|
|
13
|
+
const entries = Object.entries(value).filter(([, entryValue])=>void 0 !== entryValue).sort(([a], [b])=>a.localeCompare(b)).map(([key, entryValue])=>`${JSON.stringify(key)}:${stableOperationStringify(entryValue)}`);
|
|
14
|
+
return `{${entries.join(',')}}`;
|
|
15
|
+
}
|
|
16
|
+
return JSON.stringify(value) ?? 'null';
|
|
17
|
+
};
|
|
18
|
+
const serializeOperationContract = (operation, requestId)=>stableOperationStringify({
|
|
19
|
+
httpMethod: String(operation.httpMethod || '').toUpperCase(),
|
|
20
|
+
name: operation.name,
|
|
21
|
+
requestId,
|
|
22
|
+
routePath: operation.routePath,
|
|
23
|
+
...operation.schemas ? {
|
|
24
|
+
schemas: operation.schemas
|
|
25
|
+
} : {}
|
|
26
|
+
});
|
|
27
|
+
async function digestOperationContract(operation, requestId) {
|
|
28
|
+
const digest = await globalThis.crypto.subtle.digest('SHA-256', new TextEncoder().encode(serializeOperationContract(operation, requestId)));
|
|
29
|
+
return Array.from(new Uint8Array(digest), (value)=>value.toString(16).padStart(2, '0')).join('');
|
|
30
|
+
}
|
|
31
|
+
export { DEFAULT_OPERATION_VERSION, deriveOperationVersion, digestOperationContract, serializeOperationContract, stableOperationStringify };
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import "node:module";
|
|
2
|
+
import { existsSync, readFileSync } from "node:fs";
|
|
3
|
+
import node_path from "node:path";
|
|
4
|
+
import { deriveOperationVersion } from "./operationIdentity.mjs";
|
|
5
|
+
function resolveOperationProducer(options) {
|
|
6
|
+
for (const directory of options.directories){
|
|
7
|
+
if (!directory) continue;
|
|
8
|
+
let current = node_path.resolve(directory);
|
|
9
|
+
while(true){
|
|
10
|
+
const filename = node_path.join(current, 'package.json');
|
|
11
|
+
if (existsSync(filename)) {
|
|
12
|
+
options.onDependency?.(filename);
|
|
13
|
+
let metadata = {};
|
|
14
|
+
try {
|
|
15
|
+
metadata = JSON.parse(readFileSync(filename, 'utf8')) ?? {};
|
|
16
|
+
} catch {}
|
|
17
|
+
return {
|
|
18
|
+
requestId: options.requestId?.trim() || 'string' == typeof metadata.name && metadata.name.trim() || 'default',
|
|
19
|
+
operationVersion: deriveOperationVersion(metadata.version)
|
|
20
|
+
};
|
|
21
|
+
}
|
|
22
|
+
const parent = node_path.dirname(current);
|
|
23
|
+
if (parent === current) break;
|
|
24
|
+
current = parent;
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
return {
|
|
28
|
+
requestId: options.requestId?.trim() || 'default',
|
|
29
|
+
operationVersion: deriveOperationVersion()
|
|
30
|
+
};
|
|
31
|
+
}
|
|
32
|
+
export { resolveOperationProducer };
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import "node:module";
|
|
2
|
+
import { resolveLocalisedLoaderRouteId } from "@modern-js/runtime-extensions/localised-loader-identity";
|
|
3
|
+
import { applyLocalisedUrlsToRoutes } from "@modern-js/runtime-extensions/localised-urls";
|
|
4
|
+
function expandLocalisedLoaderRoutes(routes) {
|
|
5
|
+
const languages = new Set();
|
|
6
|
+
const localisedUrls = {};
|
|
7
|
+
let physical = false;
|
|
8
|
+
const visit = (nodes)=>{
|
|
9
|
+
for (const route of nodes){
|
|
10
|
+
physical ||= 'modernCanonicalPath' in route;
|
|
11
|
+
const identity = Reflect.get(route, 'modernLocalisedRoute');
|
|
12
|
+
if (identity?.paths) {
|
|
13
|
+
localisedUrls[identity.canonicalPath] = identity.paths;
|
|
14
|
+
for (const language of Object.keys(identity.paths))languages.add(language);
|
|
15
|
+
}
|
|
16
|
+
if ('children' in route && route.children) visit(route.children);
|
|
17
|
+
}
|
|
18
|
+
};
|
|
19
|
+
visit(routes);
|
|
20
|
+
return physical || 0 === languages.size ? routes : applyLocalisedUrlsToRoutes(routes, [
|
|
21
|
+
...languages
|
|
22
|
+
], localisedUrls);
|
|
23
|
+
}
|
|
24
|
+
const resolveLocalisedLoaderRoute = (requestedRouteId, { routes, matchedRouteIds })=>resolveLocalisedLoaderRouteId(routes, requestedRouteId, matchedRouteIds);
|
|
25
|
+
export { expandLocalisedLoaderRoutes, resolveLocalisedLoaderRoute };
|
|
@@ -1,13 +1,13 @@
|
|
|
1
1
|
import "node:module";
|
|
2
2
|
import { setLoaderRouteIdResolver } from "@modern-js/plugin-data-loader/runtime";
|
|
3
|
-
import { resolveLocalisedLoaderRouteId } from "@modern-js/runtime-extensions/localised-loader-identity";
|
|
4
3
|
import { getLoaderCtx } from "@modern-js/server-core";
|
|
4
|
+
import { expandLocalisedLoaderRoutes, resolveLocalisedLoaderRoute } from "./localisedLoader.mjs";
|
|
5
5
|
function injectLocalisedLoaderPlugin() {
|
|
6
6
|
return {
|
|
7
7
|
name: '@modern-js/localised-loader',
|
|
8
8
|
setup (api) {
|
|
9
9
|
const handler = async (context, next)=>{
|
|
10
|
-
setLoaderRouteIdResolver(getLoaderCtx(context),
|
|
10
|
+
setLoaderRouteIdResolver(getLoaderCtx(context), resolveLocalisedLoaderRoute, expandLocalisedLoaderRoutes);
|
|
11
11
|
await next();
|
|
12
12
|
};
|
|
13
13
|
api.onPrepare(()=>{
|
|
@@ -7,7 +7,8 @@ const fetchJsonWithTimeout = async (url, fetcher, timeout)=>{
|
|
|
7
7
|
try {
|
|
8
8
|
const response = await Promise.race([
|
|
9
9
|
fetcher(url, {
|
|
10
|
-
signal: controller.signal
|
|
10
|
+
signal: controller.signal,
|
|
11
|
+
redirect: 'error'
|
|
11
12
|
}),
|
|
12
13
|
new Promise((_, reject)=>{
|
|
13
14
|
timeoutId = setTimeout(()=>{
|
|
@@ -31,11 +31,45 @@ const joinModuleFederationAssetPath = (assetPath, assetName)=>{
|
|
|
31
31
|
const appendModuleFederationAsset = (set, assetPath)=>{
|
|
32
32
|
if (assetPath) set.add(trimLeadingSlash(assetPath));
|
|
33
33
|
};
|
|
34
|
-
const appendModuleFederationAssets = (set, assets)=>{
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
34
|
+
const appendModuleFederationAssets = (set, assets, directory = '')=>{
|
|
35
|
+
for (const asset of [
|
|
36
|
+
...assets?.js?.sync ?? [],
|
|
37
|
+
...assets?.js?.async ?? [],
|
|
38
|
+
...assets?.css?.sync ?? [],
|
|
39
|
+
...assets?.css?.async ?? []
|
|
40
|
+
])appendModuleFederationAsset(set, joinModuleFederationAssetPath(directory, asset));
|
|
41
|
+
};
|
|
42
|
+
const appendModuleFederationManifestAssets = (set, manifest, directory)=>{
|
|
43
|
+
for (const item of [
|
|
44
|
+
...manifest.shared ?? [],
|
|
45
|
+
...manifest.remotes ?? [],
|
|
46
|
+
...manifest.exposes ?? []
|
|
47
|
+
])appendModuleFederationAssets(set, item.assets, directory);
|
|
48
|
+
};
|
|
49
|
+
const getServerContainerDirectory = (metaData)=>{
|
|
50
|
+
const { publicPath, ssrPublicPath } = metaData ?? {};
|
|
51
|
+
if ('string' != typeof publicPath || 'string' != typeof ssrPublicPath || !ssrPublicPath.startsWith(publicPath)) return;
|
|
52
|
+
const directory = ssrPublicPath.slice(publicPath.length).replace(/^\/+|\/+$/gu, '');
|
|
53
|
+
return directory && directory.split('/').every((segment)=>segment && '.' !== segment && '..' !== segment) ? directory : void 0;
|
|
54
|
+
};
|
|
55
|
+
const readModuleFederationManifest = async (manifestPath)=>{
|
|
56
|
+
const manifestBuffer = await fileReader.readFileFromSystem(manifestPath, 'buffer');
|
|
57
|
+
if (null === manifestBuffer) return;
|
|
58
|
+
try {
|
|
59
|
+
return JSON.parse(manifestBuffer.toString('utf-8'));
|
|
60
|
+
} catch {
|
|
61
|
+
return;
|
|
62
|
+
}
|
|
63
|
+
};
|
|
64
|
+
const appendServerContainerAssets = async (pwd, set, metaData)=>{
|
|
65
|
+
const directory = getServerContainerDirectory(metaData);
|
|
66
|
+
const remoteEntry = joinModuleFederationAssetPath(metaData?.ssrRemoteEntry?.path, metaData?.ssrRemoteEntry?.name);
|
|
67
|
+
if (!directory || !remoteEntry) return;
|
|
68
|
+
set.add(joinModuleFederationAssetPath(directory, remoteEntry));
|
|
69
|
+
const manifestPath = path.join(pwd, directory, MODULE_FEDERATION_MANIFEST_FILE);
|
|
70
|
+
if (!await fs.pathExists(manifestPath)) return;
|
|
71
|
+
const manifest = await readModuleFederationManifest(manifestPath);
|
|
72
|
+
if (manifest) appendModuleFederationManifestAssets(set, manifest, directory);
|
|
39
73
|
};
|
|
40
74
|
const hasAbsoluteProtocol = (value)=>/^https?:\/\//i.test(value) || value.startsWith('//');
|
|
41
75
|
const ensureLeadingSlash = (value)=>{
|
|
@@ -50,9 +84,14 @@ const patchModuleFederationManifestPublicPath = (c, manifestBuffer, pathPrefix)=
|
|
|
50
84
|
if (!publicPath || hasAbsoluteProtocol(publicPath)) return manifestBuffer;
|
|
51
85
|
const requestURL = new URL(c.req.url);
|
|
52
86
|
const prefixPath = ensureTrailingSlash(ensureLeadingSlash(pathPrefix || '/'));
|
|
87
|
+
const servedPublicPath = `${requestURL.origin}${prefixPath}`;
|
|
88
|
+
const { ssrPublicPath } = manifest.metaData ?? {};
|
|
53
89
|
manifest.metaData = {
|
|
54
90
|
...manifest.metaData,
|
|
55
|
-
publicPath:
|
|
91
|
+
publicPath: servedPublicPath,
|
|
92
|
+
...'string' == typeof ssrPublicPath && !hasAbsoluteProtocol(ssrPublicPath) && ssrPublicPath.startsWith(publicPath) ? {
|
|
93
|
+
ssrPublicPath: `${servedPublicPath}${ssrPublicPath.slice(publicPath.length).replace(/^\/+/u, '')}`
|
|
94
|
+
} : {}
|
|
56
95
|
};
|
|
57
96
|
return Buffer.from(JSON.stringify(manifest), 'utf-8');
|
|
58
97
|
} catch {
|
|
@@ -77,9 +116,8 @@ const getModuleFederationAssetList = async (pwd)=>{
|
|
|
77
116
|
if (!await fs.pathExists(manifestPath)) continue;
|
|
78
117
|
manifestFound = true;
|
|
79
118
|
assets.add(manifestFile);
|
|
80
|
-
const
|
|
81
|
-
if (
|
|
82
|
-
const manifest = JSON.parse(manifestBuffer.toString('utf-8'));
|
|
119
|
+
const manifest = await readModuleFederationManifest(manifestPath);
|
|
120
|
+
if (manifest) try {
|
|
83
121
|
const remoteEntry = joinModuleFederationAssetPath(manifest.metaData?.remoteEntry?.path, manifest.metaData?.remoteEntry?.name);
|
|
84
122
|
const dtsZip = joinModuleFederationAssetPath(manifest.metaData?.types?.path, manifest.metaData?.types?.zip);
|
|
85
123
|
const dtsApi = joinModuleFederationAssetPath(manifest.metaData?.types?.path, manifest.metaData?.types?.api);
|
|
@@ -89,9 +127,8 @@ const getModuleFederationAssetList = async (pwd)=>{
|
|
|
89
127
|
}
|
|
90
128
|
appendModuleFederationAsset(assets, dtsZip);
|
|
91
129
|
appendModuleFederationAsset(assets, dtsApi);
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
manifest.exposes?.forEach((item)=>appendModuleFederationAssets(assets, item.assets));
|
|
130
|
+
appendModuleFederationManifestAssets(assets, manifest);
|
|
131
|
+
if (manifestFile === MODULE_FEDERATION_MANIFEST_FILE) await appendServerContainerAssets(pwd, assets, manifest.metaData);
|
|
95
132
|
} catch {}
|
|
96
133
|
}
|
|
97
134
|
if (manifestFound) {
|
|
@@ -1,3 +1,3 @@
|
|
|
1
1
|
import { type BackendFederationCommonJsEvaluator, type BackendFederationEntryIntegrity } from './index.js';
|
|
2
2
|
export declare function createBackendFederationEntryIntegrity(value: string | Uint8Array): BackendFederationEntryIntegrity;
|
|
3
|
-
export declare const evaluateNodeBackendFederationCommonJs: BackendFederationCommonJsEvaluator
|
|
3
|
+
export declare const evaluateNodeBackendFederationCommonJs: (source: Parameters<BackendFederationCommonJsEvaluator>[0], { remote }: Parameters<BackendFederationCommonJsEvaluator>[1]) => Record<string, unknown>;
|
|
@@ -1,32 +1,7 @@
|
|
|
1
1
|
import 'reflect-metadata';
|
|
2
2
|
type ApiHandler = (...args: any) => any | Promise<any>;
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
httpMethod: string;
|
|
6
|
-
routePath: string;
|
|
7
|
-
};
|
|
8
|
-
export type OperationContractDefinition = {
|
|
9
|
-
requestId: string;
|
|
10
|
-
operationVersion: number;
|
|
11
|
-
schemaHash: string;
|
|
12
|
-
method: string;
|
|
13
|
-
routePath: string;
|
|
14
|
-
operationId: string;
|
|
15
|
-
handlerName: string;
|
|
16
|
-
filename?: string;
|
|
17
|
-
};
|
|
18
|
-
export type OperationContractMap = Record<string, OperationContractDefinition>;
|
|
19
|
-
export declare const DEFAULT_OPERATION_VERSION = 1;
|
|
20
|
-
/**
|
|
21
|
-
* Derives the operation version from a producer package version: the semver
|
|
22
|
-
* major is the contract version, so consumers regenerated against an older
|
|
23
|
-
* producer major fail the `operation_version_mismatch` gate instead of
|
|
24
|
-
* silently calling an incompatible API.
|
|
25
|
-
*
|
|
26
|
-
* Falls back to {@link DEFAULT_OPERATION_VERSION} when no parseable version
|
|
27
|
-
* is available.
|
|
28
|
-
*/
|
|
29
|
-
export declare const deriveOperationVersion: (packageVersion?: unknown) => number;
|
|
3
|
+
import { type OperationContractEntry, type OperationContractHashInput, type OperationContractMap } from './operationIdentity.js';
|
|
4
|
+
export * from './operationIdentity.js';
|
|
30
5
|
/**
|
|
31
6
|
* Serializes the zod input schemas attached to an operator-decorated handler
|
|
32
7
|
* (`Data`/`Query`/`Params`/`Headers`/`Upload` metadata) into JSON-schema
|
|
@@ -34,19 +9,6 @@ export declare const deriveOperationVersion: (packageVersion?: unknown) => numbe
|
|
|
34
9
|
* (plain handlers, farrow schema-mode handlers).
|
|
35
10
|
*/
|
|
36
11
|
export declare const serializeOperationSchemas: (handler: ApiHandler | undefined) => Record<string, unknown> | undefined;
|
|
37
|
-
export type OperationContractHashInput = OperationContractEntry & {
|
|
38
|
-
/** Serialized schema documents; omit for schema-less operations. */
|
|
39
|
-
schemas?: Record<string, unknown> | undefined;
|
|
40
|
-
};
|
|
41
|
-
/**
|
|
42
|
-
* Per-operation contract hash. The hash covers the operation identity
|
|
43
|
-
* (name, method, route, producer requestId) plus the serialized input
|
|
44
|
-
* schemas, so:
|
|
45
|
-
*
|
|
46
|
-
* - changing an input schema changes the hash of exactly that operation;
|
|
47
|
-
* - reordering routes or adding unrelated operations never rotates the hash
|
|
48
|
-
* of other operations (each operation is hashed independently).
|
|
49
|
-
*/
|
|
50
12
|
export declare const createOperationContractHash: (operation: OperationContractHashInput, requestId: string) => string;
|
|
51
13
|
export declare const createOperationEntries: (handlers: Array<{
|
|
52
14
|
name: string;
|
|
@@ -82,4 +44,3 @@ export declare const buildOperationContractMap: ({ handlers, requestId, operatio
|
|
|
82
44
|
*/
|
|
83
45
|
operationVersion?: number;
|
|
84
46
|
}) => OperationContractMap;
|
|
85
|
-
export {};
|