@bleedingdev/modern-js-server-runtime-extensions 3.9.0-ultramodern.10 → 3.9.0-ultramodern.12
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/localisedLoaderPlugin.js +22 -1
- package/dist/cjs/module-federation-css/remote.js +2 -1
- 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/localisedLoaderPlugin.mjs +22 -1
- 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/localisedLoaderPlugin.mjs +22 -1
- package/dist/esm-node/module-federation-css/remote.mjs +2 -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/module-federation-css/remote.d.ts +2 -0
- 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/module-federation-css/remote.d.mts +2 -0
- package/package.json +10 -10
- package/src/backend-federation-security/index.ts +151 -318
- 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/localisedLoaderPlugin.ts +31 -0
- package/src/module-federation-css/remote.ts +3 -1
- package/tests/backendFederationSecurity.test.ts +63 -0
- package/tests/bff-policy/operationContracts.test.ts +57 -0
- package/tests/localisedLoader.test.ts +36 -1
- package/tests/moduleFederationCss.test.ts +101 -0
|
@@ -142,97 +142,85 @@ const sha256Hex = async (bytes, signal)=>{
|
|
|
142
142
|
const digest = new Uint8Array(await readWithAbort(subtle.digest('SHA-256', bytes), signal));
|
|
143
143
|
return Array.from(digest, (byte)=>byte.toString(16).padStart(2, '0')).join('');
|
|
144
144
|
};
|
|
145
|
-
async function
|
|
146
|
-
const
|
|
147
|
-
assertAllowedEntryUrl(requestedUrl, true === options.allowInsecureHttp);
|
|
148
|
-
const maxBytes = options.maxBytes ?? DEFAULT_MAX_RESOURCE_BYTES;
|
|
149
|
-
if (!Number.isSafeInteger(maxBytes) || maxBytes <= 0) throw new BackendFederationRemoteEntryError('invalid_verification', '[Module Federation] Backend federation resource maxBytes must be a positive safe integer.');
|
|
145
|
+
async function acquireBackendFederationResource(requestedUrl, options, policy) {
|
|
146
|
+
const { label, maxBytes, exactByteLength } = policy;
|
|
150
147
|
const timeoutMs = options.timeoutMs ?? DEFAULT_ENTRY_TIMEOUT_MS;
|
|
151
|
-
if (!Number.isSafeInteger(timeoutMs) || timeoutMs < 0) throw new BackendFederationRemoteEntryError('invalid_verification', '[Module Federation]
|
|
148
|
+
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.');
|
|
149
|
+
if (void 0 !== exactByteLength && exactByteLength > maxBytes) throw new BackendFederationRemoteEntryError('entry_too_large', `[Module Federation] ${label} exceeds the ${maxBytes}-byte policy limit.`);
|
|
152
150
|
const controller = new AbortController();
|
|
153
151
|
let timedOut = false;
|
|
154
|
-
|
|
155
|
-
const onCallerAbort = ()=>{
|
|
156
|
-
callerAborted = true;
|
|
157
|
-
controller.abort(options.signal?.reason);
|
|
158
|
-
};
|
|
152
|
+
const onCallerAbort = ()=>controller.abort(options.signal?.reason);
|
|
159
153
|
if (options.signal?.aborted) onCallerAbort();
|
|
160
154
|
else options.signal?.addEventListener('abort', onCallerAbort, {
|
|
161
155
|
once: true
|
|
162
156
|
});
|
|
163
157
|
const timeout = timeoutMs > 0 ? setTimeout(()=>{
|
|
164
158
|
timedOut = true;
|
|
165
|
-
controller.abort(new Error(
|
|
159
|
+
controller.abort(new Error(`${label} timed out after ${timeoutMs}ms`));
|
|
166
160
|
}, timeoutMs) : void 0;
|
|
167
|
-
const fetchResource = options.fetch ?? globalThis.fetch;
|
|
168
161
|
let reader;
|
|
169
|
-
|
|
170
|
-
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.`, {
|
|
171
|
-
cause
|
|
172
|
-
});
|
|
162
|
+
let response;
|
|
173
163
|
try {
|
|
174
|
-
|
|
175
|
-
if (
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
}
|
|
188
|
-
}
|
|
189
|
-
if (!response.ok) throw new BackendFederationRemoteEntryError('fetch_failed', `[Module Federation] Backend federation ${resourceKind} returned HTTP ${response.status}.`);
|
|
190
|
-
if (response.url) {
|
|
191
|
-
const responseUrl = canonicalEntryUrl(response.url);
|
|
192
|
-
if (responseUrl.href !== requestedUrl.href) throw new BackendFederationRemoteEntryError('redirect_mismatch', `[Module Federation] Backend federation ${resourceKind} redirected outside its trusted URL.`, {
|
|
193
|
-
details: {
|
|
194
|
-
expected: redactBackendFederationUrl(requestedUrl.href),
|
|
195
|
-
received: redactBackendFederationUrl(responseUrl.href)
|
|
196
|
-
}
|
|
197
|
-
});
|
|
198
|
-
}
|
|
164
|
+
const fetchResource = options.fetch ?? globalThis.fetch;
|
|
165
|
+
if (controller.signal.aborted) throw controller.signal.reason;
|
|
166
|
+
if ('function' != typeof fetchResource) throw new Error('Loading requires fetch.');
|
|
167
|
+
response = await readWithAbort(fetchResource(requestedUrl.href, {
|
|
168
|
+
cache: 'no-store',
|
|
169
|
+
redirect: 'error',
|
|
170
|
+
signal: controller.signal
|
|
171
|
+
}), controller.signal);
|
|
172
|
+
if (!response.ok) throw new BackendFederationRemoteEntryError('fetch_failed', `[Module Federation] ${label} returned HTTP ${response.status}.`);
|
|
173
|
+
if (response.url && canonicalEntryUrl(response.url).href !== requestedUrl.href) throw new BackendFederationRemoteEntryError('redirect_mismatch', `[Module Federation] ${label} redirected outside its trusted URL.`, {
|
|
174
|
+
details: {
|
|
175
|
+
expected: redactBackendFederationUrl(requestedUrl.href),
|
|
176
|
+
received: redactBackendFederationUrl(response.url)
|
|
177
|
+
}
|
|
178
|
+
});
|
|
199
179
|
const contentLength = parseContentLength(response.headers?.get('content-length') ?? null);
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
throw new BackendFederationRemoteEntryError('entry_too_large', `[Module Federation] Backend federation ${resourceKind} declares ${contentLength} bytes, exceeding its ${maxBytes}-byte policy limit.`);
|
|
204
|
-
}
|
|
180
|
+
const byteLimit = exactByteLength ?? maxBytes;
|
|
181
|
+
if (void 0 !== contentLength && contentLength > maxBytes) throw new BackendFederationRemoteEntryError('entry_too_large', `[Module Federation] ${label} declares ${contentLength} bytes, exceeding its ${maxBytes}-byte policy limit.`);
|
|
182
|
+
const exactBytes = void 0 === exactByteLength ? void 0 : new Uint8Array(exactByteLength);
|
|
205
183
|
const chunks = [];
|
|
206
184
|
let byteLength = 0;
|
|
185
|
+
const append = (chunk)=>{
|
|
186
|
+
if (byteLength + chunk.byteLength > byteLimit) throw new BackendFederationRemoteEntryError('entry_too_large', `[Module Federation] ${label} exceeded its ${byteLimit}-byte limit while streaming.`);
|
|
187
|
+
if (exactBytes) exactBytes.set(chunk, byteLength);
|
|
188
|
+
else chunks.push(chunk);
|
|
189
|
+
byteLength += chunk.byteLength;
|
|
190
|
+
};
|
|
207
191
|
if (response.body) {
|
|
208
192
|
reader = response.body.getReader();
|
|
209
193
|
while(true){
|
|
210
194
|
const result = await readWithAbort(reader.read(), controller.signal);
|
|
211
195
|
if (result.done) break;
|
|
212
|
-
|
|
213
|
-
if (byteLength > maxBytes) throw new BackendFederationRemoteEntryError('entry_too_large', `[Module Federation] Backend federation ${resourceKind} exceeded its ${maxBytes}-byte limit while streaming.`);
|
|
214
|
-
chunks.push(result.value);
|
|
196
|
+
append(result.value);
|
|
215
197
|
}
|
|
216
|
-
} else if (response.text)
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
198
|
+
} else if (void 0 === exactByteLength && response.text) append(new TextEncoder().encode(await readWithAbort(response.text(), controller.signal)));
|
|
199
|
+
if (void 0 !== exactByteLength && byteLength !== exactByteLength) throw new BackendFederationRemoteEntryError('byte_length_mismatch', `[Module Federation] ${label} byte length mismatch.`, {
|
|
200
|
+
details: {
|
|
201
|
+
expected: exactByteLength,
|
|
202
|
+
received: byteLength
|
|
203
|
+
}
|
|
204
|
+
});
|
|
205
|
+
const bytes = exactBytes ?? new Uint8Array(byteLength);
|
|
223
206
|
let offset = 0;
|
|
224
207
|
for (const chunk of chunks){
|
|
225
208
|
bytes.set(chunk, offset);
|
|
226
209
|
offset += chunk.byteLength;
|
|
227
210
|
}
|
|
228
|
-
if (controller.signal.aborted) throw
|
|
211
|
+
if (controller.signal.aborted) throw controller.signal.reason;
|
|
212
|
+
if (policy.verify) await readWithAbort(policy.verify(bytes, controller.signal), controller.signal);
|
|
229
213
|
return bytes;
|
|
230
214
|
} catch (cause) {
|
|
231
|
-
|
|
232
|
-
|
|
215
|
+
const cancelled = controller.signal.aborted;
|
|
216
|
+
controller.abort(cause);
|
|
217
|
+
if (reader) reader.cancel(cause).catch(()=>void 0);
|
|
218
|
+
else if (response?.body) response.body.cancel(cause).catch(()=>void 0);
|
|
219
|
+
if (cancelled) throw new BackendFederationRemoteEntryError(timedOut ? 'timeout' : 'aborted', timedOut ? `[Module Federation] ${label} timed out after ${timeoutMs}ms.` : `[Module Federation] ${label} loading was aborted.`, {
|
|
220
|
+
cause
|
|
221
|
+
});
|
|
233
222
|
if (cause instanceof BackendFederationRemoteEntryError) throw cause;
|
|
234
|
-
|
|
235
|
-
throw new BackendFederationRemoteEntryError('fetch_failed', `[Module Federation] Backend federation ${resourceKind} loading failed.`, {
|
|
223
|
+
throw new BackendFederationRemoteEntryError('fetch_failed', `[Module Federation] ${label} loading failed.`, {
|
|
236
224
|
cause
|
|
237
225
|
});
|
|
238
226
|
} finally{
|
|
@@ -240,6 +228,14 @@ async function loadBoundedBackendFederationResource(resourceUrl, options = {}) {
|
|
|
240
228
|
options.signal?.removeEventListener('abort', onCallerAbort);
|
|
241
229
|
}
|
|
242
230
|
}
|
|
231
|
+
async function loadBoundedBackendFederationResource(resourceUrl, options = {}) {
|
|
232
|
+
const requestedUrl = canonicalEntryUrl(resourceUrl);
|
|
233
|
+
assertAllowedEntryUrl(requestedUrl, true === options.allowInsecureHttp);
|
|
234
|
+
return acquireBackendFederationResource(requestedUrl, options, {
|
|
235
|
+
label: `Backend federation ${options.kind ?? 'manifest'}`,
|
|
236
|
+
maxBytes: options.maxBytes ?? DEFAULT_MAX_RESOURCE_BYTES
|
|
237
|
+
});
|
|
238
|
+
}
|
|
243
239
|
const evaluateBackendFederationCommonJsEntry = (remote, bytes, evaluator)=>{
|
|
244
240
|
let source;
|
|
245
241
|
try {
|
|
@@ -263,99 +259,24 @@ const evaluateBackendFederationCommonJsEntry = (remote, bytes, evaluator)=>{
|
|
|
263
259
|
async function loadVerifiedBackendFederationEntry(options) {
|
|
264
260
|
const verification = resolveTrustedVerification(options.verification, options.expected ?? {});
|
|
265
261
|
const requestedUrl = assertVerification(options.remote, verification, options.expected ?? {}, true === options.allowInsecureHttp);
|
|
266
|
-
const
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
const onCallerAbort = ()=>{
|
|
275
|
-
callerAborted = true;
|
|
276
|
-
controller.abort(options.signal?.reason);
|
|
277
|
-
};
|
|
278
|
-
if (options.signal?.aborted) onCallerAbort();
|
|
279
|
-
else options.signal?.addEventListener('abort', onCallerAbort, {
|
|
280
|
-
once: true
|
|
281
|
-
});
|
|
282
|
-
const timeout = timeoutMs > 0 ? setTimeout(()=>{
|
|
283
|
-
timedOut = true;
|
|
284
|
-
controller.abort(new Error(`Backend remote entry timed out after ${timeoutMs}ms`));
|
|
285
|
-
}, timeoutMs) : void 0;
|
|
286
|
-
const fetchEntry = options.fetch ?? globalThis.fetch;
|
|
287
|
-
let reader;
|
|
288
|
-
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.`, {
|
|
289
|
-
cause
|
|
290
|
-
});
|
|
291
|
-
try {
|
|
292
|
-
if (controller.signal.aborted) throw cancellationError(controller.signal.reason);
|
|
293
|
-
if ('function' != typeof fetchEntry) throw new BackendFederationRemoteEntryError('fetch_failed', '[Module Federation] Backend remote entry loading requires fetch.');
|
|
294
|
-
let response;
|
|
295
|
-
try {
|
|
296
|
-
response = await readWithAbort(fetchEntry(requestedUrl.href, {
|
|
297
|
-
cache: 'no-store',
|
|
298
|
-
redirect: 'error',
|
|
299
|
-
signal: controller.signal
|
|
300
|
-
}), controller.signal);
|
|
301
|
-
} catch (cause) {
|
|
302
|
-
if (controller.signal.aborted) throw cancellationError(cause);
|
|
303
|
-
throw new BackendFederationRemoteEntryError('fetch_failed', `[Module Federation] Failed to fetch backend remote ${options.remote.name}.`, {
|
|
304
|
-
cause
|
|
305
|
-
});
|
|
306
|
-
}
|
|
307
|
-
if (!response.ok) throw new BackendFederationRemoteEntryError('fetch_failed', `[Module Federation] Backend remote ${options.remote.name} returned HTTP ${response.status}.`);
|
|
308
|
-
if (response.url) {
|
|
309
|
-
const responseUrl = canonicalEntryUrl(response.url);
|
|
310
|
-
if (responseUrl.href !== requestedUrl.href) throw new BackendFederationRemoteEntryError('redirect_mismatch', `[Module Federation] Backend remote ${options.remote.name} redirected outside its verified entry URL.`, {
|
|
262
|
+
const bytes = await acquireBackendFederationResource(requestedUrl, options, {
|
|
263
|
+
label: `Backend remote ${options.remote.name}`,
|
|
264
|
+
maxBytes: options.maxBytes ?? DEFAULT_MAX_ENTRY_BYTES,
|
|
265
|
+
exactByteLength: verification.byteLength,
|
|
266
|
+
async verify (bytes, signal) {
|
|
267
|
+
const actualDigest = await sha256Hex(bytes, signal);
|
|
268
|
+
if (signal.aborted) throw signal.reason;
|
|
269
|
+
if (actualDigest !== verification.sha256) throw new BackendFederationRemoteEntryError('integrity_mismatch', `[Module Federation] Backend remote ${options.remote.name} SHA-256 mismatch.`, {
|
|
311
270
|
details: {
|
|
312
|
-
expected:
|
|
313
|
-
received:
|
|
271
|
+
expected: verification.sha256,
|
|
272
|
+
received: actualDigest
|
|
314
273
|
}
|
|
315
274
|
});
|
|
316
275
|
}
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
throw new BackendFederationRemoteEntryError('entry_too_large', `[Module Federation] Backend remote ${options.remote.name} declares ${contentLength} bytes, exceeding its ${maxBytes}-byte policy limit.`);
|
|
323
|
-
}
|
|
324
|
-
reader = response.body?.getReader();
|
|
325
|
-
const bytes = new Uint8Array(verification.byteLength);
|
|
326
|
-
let byteLength = 0;
|
|
327
|
-
if (reader) while(true){
|
|
328
|
-
const result = await readWithAbort(reader.read(), controller.signal);
|
|
329
|
-
if (result.done) break;
|
|
330
|
-
const nextByteLength = byteLength + result.value.byteLength;
|
|
331
|
-
if (nextByteLength > byteLimit) throw new BackendFederationRemoteEntryError('entry_too_large', `[Module Federation] Backend remote ${options.remote.name} exceeded its ${byteLimit}-byte limit while streaming.`);
|
|
332
|
-
bytes.set(result.value, byteLength);
|
|
333
|
-
byteLength = nextByteLength;
|
|
334
|
-
}
|
|
335
|
-
if (byteLength !== verification.byteLength) throw new BackendFederationRemoteEntryError('byte_length_mismatch', `[Module Federation] Backend remote ${options.remote.name} byte length mismatch.`, {
|
|
336
|
-
details: {
|
|
337
|
-
expected: verification.byteLength,
|
|
338
|
-
received: byteLength
|
|
339
|
-
}
|
|
340
|
-
});
|
|
341
|
-
const actualDigest = await sha256Hex(bytes, controller.signal);
|
|
342
|
-
if (controller.signal.aborted) throw cancellationError(controller.signal.reason);
|
|
343
|
-
if (actualDigest !== verification.sha256) throw new BackendFederationRemoteEntryError('integrity_mismatch', `[Module Federation] Backend remote ${options.remote.name} SHA-256 mismatch.`, {
|
|
344
|
-
details: {
|
|
345
|
-
expected: verification.sha256,
|
|
346
|
-
received: actualDigest
|
|
347
|
-
}
|
|
348
|
-
});
|
|
349
|
-
return evaluateBackendFederationCommonJsEntry(options.remote, bytes, options.evaluateCommonJs);
|
|
350
|
-
} catch (cause) {
|
|
351
|
-
if (!controller.signal.aborted) controller.abort(cause);
|
|
352
|
-
await reader?.cancel(cause).catch(()=>void 0);
|
|
353
|
-
if (cause instanceof BackendFederationRemoteEntryError && !('aborted' === cause.code && timedOut)) throw cause;
|
|
354
|
-
if (controller.signal.aborted && (timedOut || callerAborted)) throw cancellationError(cause);
|
|
355
|
-
throw cause;
|
|
356
|
-
} finally{
|
|
357
|
-
if (timeout) clearTimeout(timeout);
|
|
358
|
-
options.signal?.removeEventListener('abort', onCallerAbort);
|
|
359
|
-
}
|
|
276
|
+
});
|
|
277
|
+
if (options.signal?.aborted) throw new BackendFederationRemoteEntryError('aborted', `[Module Federation] Backend remote ${options.remote.name} loading was aborted.`, {
|
|
278
|
+
cause: options.signal.reason
|
|
279
|
+
});
|
|
280
|
+
return evaluateBackendFederationCommonJsEntry(options.remote, bytes, options.evaluateCommonJs);
|
|
360
281
|
}
|
|
361
282
|
export { BackendFederationRemoteEntryError, evaluateBackendFederationCommonJsEntry, loadBoundedBackendFederationResource, loadVerifiedBackendFederationEntry, redactBackendFederationUrl, resolveBackendFederationRemoteEntryVerification };
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
const DEFAULT_OPERATION_VERSION = 1;
|
|
2
|
+
const deriveOperationVersion = (packageVersion)=>{
|
|
3
|
+
if ('string' != typeof packageVersion) return DEFAULT_OPERATION_VERSION;
|
|
4
|
+
const match = packageVersion.trim().match(/^v?(\d+)\./);
|
|
5
|
+
if (!match) return DEFAULT_OPERATION_VERSION;
|
|
6
|
+
const major = Number.parseInt(match[1], 10);
|
|
7
|
+
return Number.isInteger(major) && major >= 0 ? major : DEFAULT_OPERATION_VERSION;
|
|
8
|
+
};
|
|
9
|
+
const stableOperationStringify = (value)=>{
|
|
10
|
+
if (Array.isArray(value)) return `[${value.map((item)=>stableOperationStringify(item)).join(',')}]`;
|
|
11
|
+
if (value && 'object' == typeof value) {
|
|
12
|
+
const entries = Object.entries(value).filter(([, entryValue])=>void 0 !== entryValue).sort(([a], [b])=>a.localeCompare(b)).map(([key, entryValue])=>`${JSON.stringify(key)}:${stableOperationStringify(entryValue)}`);
|
|
13
|
+
return `{${entries.join(',')}}`;
|
|
14
|
+
}
|
|
15
|
+
return JSON.stringify(value) ?? 'null';
|
|
16
|
+
};
|
|
17
|
+
const serializeOperationContract = (operation, requestId)=>stableOperationStringify({
|
|
18
|
+
httpMethod: String(operation.httpMethod || '').toUpperCase(),
|
|
19
|
+
name: operation.name,
|
|
20
|
+
requestId,
|
|
21
|
+
routePath: operation.routePath,
|
|
22
|
+
...operation.schemas ? {
|
|
23
|
+
schemas: operation.schemas
|
|
24
|
+
} : {}
|
|
25
|
+
});
|
|
26
|
+
async function digestOperationContract(operation, requestId) {
|
|
27
|
+
const digest = await globalThis.crypto.subtle.digest('SHA-256', new TextEncoder().encode(serializeOperationContract(operation, requestId)));
|
|
28
|
+
return Array.from(new Uint8Array(digest), (value)=>value.toString(16).padStart(2, '0')).join('');
|
|
29
|
+
}
|
|
30
|
+
export { DEFAULT_OPERATION_VERSION, deriveOperationVersion, digestOperationContract, serializeOperationContract, stableOperationStringify };
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import { existsSync, readFileSync } from "node:fs";
|
|
2
|
+
import node_path from "node:path";
|
|
3
|
+
import { deriveOperationVersion } from "./operationIdentity.mjs";
|
|
4
|
+
function resolveOperationProducer(options) {
|
|
5
|
+
for (const directory of options.directories){
|
|
6
|
+
if (!directory) continue;
|
|
7
|
+
let current = node_path.resolve(directory);
|
|
8
|
+
while(true){
|
|
9
|
+
const filename = node_path.join(current, 'package.json');
|
|
10
|
+
if (existsSync(filename)) {
|
|
11
|
+
options.onDependency?.(filename);
|
|
12
|
+
let metadata = {};
|
|
13
|
+
try {
|
|
14
|
+
metadata = JSON.parse(readFileSync(filename, 'utf8')) ?? {};
|
|
15
|
+
} catch {}
|
|
16
|
+
return {
|
|
17
|
+
requestId: options.requestId?.trim() || 'string' == typeof metadata.name && metadata.name.trim() || 'default',
|
|
18
|
+
operationVersion: deriveOperationVersion(metadata.version)
|
|
19
|
+
};
|
|
20
|
+
}
|
|
21
|
+
const parent = node_path.dirname(current);
|
|
22
|
+
if (parent === current) break;
|
|
23
|
+
current = parent;
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
return {
|
|
27
|
+
requestId: options.requestId?.trim() || 'default',
|
|
28
|
+
operationVersion: deriveOperationVersion()
|
|
29
|
+
};
|
|
30
|
+
}
|
|
31
|
+
export { resolveOperationProducer };
|
|
@@ -1,12 +1,33 @@
|
|
|
1
1
|
import { setLoaderRouteIdResolver } from "@modern-js/plugin-data-loader/runtime";
|
|
2
2
|
import { resolveLocalisedLoaderRouteId } from "@modern-js/runtime-extensions/localised-loader-identity";
|
|
3
|
+
import { applyLocalisedUrlsToRoutes } from "@modern-js/runtime-extensions/localised-urls";
|
|
3
4
|
import { getLoaderCtx } from "@modern-js/server-core";
|
|
5
|
+
function expandCanonicalRoutes(routes) {
|
|
6
|
+
const languages = new Set();
|
|
7
|
+
const localisedUrls = {};
|
|
8
|
+
let physical = false;
|
|
9
|
+
const visit = (nodes)=>{
|
|
10
|
+
for (const route of nodes){
|
|
11
|
+
physical ||= 'modernCanonicalPath' in route;
|
|
12
|
+
const identity = Reflect.get(route, 'modernLocalisedRoute');
|
|
13
|
+
if (identity?.paths) {
|
|
14
|
+
localisedUrls[identity.canonicalPath] = identity.paths;
|
|
15
|
+
for (const language of Object.keys(identity.paths))languages.add(language);
|
|
16
|
+
}
|
|
17
|
+
if ('children' in route && route.children) visit(route.children);
|
|
18
|
+
}
|
|
19
|
+
};
|
|
20
|
+
visit(routes);
|
|
21
|
+
return physical || 0 === languages.size ? routes : applyLocalisedUrlsToRoutes(routes, [
|
|
22
|
+
...languages
|
|
23
|
+
], localisedUrls);
|
|
24
|
+
}
|
|
4
25
|
function injectLocalisedLoaderPlugin() {
|
|
5
26
|
return {
|
|
6
27
|
name: '@modern-js/localised-loader',
|
|
7
28
|
setup (api) {
|
|
8
29
|
const handler = async (context, next)=>{
|
|
9
|
-
setLoaderRouteIdResolver(getLoaderCtx(context), (requestedRouteId, { routes, matchedRouteIds })=>resolveLocalisedLoaderRouteId(routes, requestedRouteId, matchedRouteIds));
|
|
30
|
+
setLoaderRouteIdResolver(getLoaderCtx(context), (requestedRouteId, { routes, matchedRouteIds })=>resolveLocalisedLoaderRouteId(routes, requestedRouteId, matchedRouteIds), expandCanonicalRoutes);
|
|
10
31
|
await next();
|
|
11
32
|
};
|
|
12
33
|
api.onPrepare(()=>{
|
|
@@ -6,7 +6,8 @@ const fetchJsonWithTimeout = async (url, fetcher, timeout)=>{
|
|
|
6
6
|
try {
|
|
7
7
|
const response = await Promise.race([
|
|
8
8
|
fetcher(url, {
|
|
9
|
-
signal: controller.signal
|
|
9
|
+
signal: controller.signal,
|
|
10
|
+
redirect: 'error'
|
|
10
11
|
}),
|
|
11
12
|
new Promise((_, reject)=>{
|
|
12
13
|
timeoutId = setTimeout(()=>{
|