@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
|
@@ -0,0 +1,90 @@
|
|
|
1
|
+
export type OperationContractEntry = {
|
|
2
|
+
name: string;
|
|
3
|
+
httpMethod: string;
|
|
4
|
+
routePath: string;
|
|
5
|
+
};
|
|
6
|
+
|
|
7
|
+
export type OperationContractDefinition = {
|
|
8
|
+
requestId: string;
|
|
9
|
+
operationVersion: number;
|
|
10
|
+
schemaHash: string;
|
|
11
|
+
method: string;
|
|
12
|
+
routePath: string;
|
|
13
|
+
operationId: string;
|
|
14
|
+
handlerName: string;
|
|
15
|
+
filename?: string;
|
|
16
|
+
};
|
|
17
|
+
|
|
18
|
+
export type OperationContractMap = Record<string, OperationContractDefinition>;
|
|
19
|
+
|
|
20
|
+
export const DEFAULT_OPERATION_VERSION = 1;
|
|
21
|
+
|
|
22
|
+
/**
|
|
23
|
+
* Derives the operation version from a producer package version: the semver
|
|
24
|
+
* major is the contract version, so consumers regenerated against an older
|
|
25
|
+
* producer major fail the `operation_version_mismatch` gate instead of
|
|
26
|
+
* silently calling an incompatible API.
|
|
27
|
+
*
|
|
28
|
+
* Falls back to {@link DEFAULT_OPERATION_VERSION} when no parseable version
|
|
29
|
+
* is available.
|
|
30
|
+
*/
|
|
31
|
+
export const deriveOperationVersion = (packageVersion?: unknown): number => {
|
|
32
|
+
if (typeof packageVersion !== 'string') {
|
|
33
|
+
return DEFAULT_OPERATION_VERSION;
|
|
34
|
+
}
|
|
35
|
+
const match = packageVersion.trim().match(/^v?(\d+)\./);
|
|
36
|
+
if (!match) {
|
|
37
|
+
return DEFAULT_OPERATION_VERSION;
|
|
38
|
+
}
|
|
39
|
+
const major = Number.parseInt(match[1]!, 10);
|
|
40
|
+
return Number.isInteger(major) && major >= 0
|
|
41
|
+
? major
|
|
42
|
+
: DEFAULT_OPERATION_VERSION;
|
|
43
|
+
};
|
|
44
|
+
|
|
45
|
+
/** JSON.stringify with recursively sorted object keys for stable hashing. */
|
|
46
|
+
export const stableOperationStringify = (value: unknown): string => {
|
|
47
|
+
if (Array.isArray(value)) {
|
|
48
|
+
return `[${value.map(item => stableOperationStringify(item)).join(',')}]`;
|
|
49
|
+
}
|
|
50
|
+
if (value && typeof value === 'object') {
|
|
51
|
+
const entries = Object.entries(value as Record<string, unknown>)
|
|
52
|
+
.filter(([, entryValue]) => typeof entryValue !== 'undefined')
|
|
53
|
+
.sort(([a], [b]) => a.localeCompare(b))
|
|
54
|
+
.map(
|
|
55
|
+
([key, entryValue]) =>
|
|
56
|
+
`${JSON.stringify(key)}:${stableOperationStringify(entryValue)}`,
|
|
57
|
+
);
|
|
58
|
+
return `{${entries.join(',')}}`;
|
|
59
|
+
}
|
|
60
|
+
return JSON.stringify(value) ?? 'null';
|
|
61
|
+
};
|
|
62
|
+
|
|
63
|
+
export type OperationContractHashInput = OperationContractEntry & {
|
|
64
|
+
schemas?: Record<string, unknown>;
|
|
65
|
+
};
|
|
66
|
+
|
|
67
|
+
export const serializeOperationContract = (
|
|
68
|
+
operation: OperationContractHashInput,
|
|
69
|
+
requestId: string,
|
|
70
|
+
): string =>
|
|
71
|
+
stableOperationStringify({
|
|
72
|
+
httpMethod: String(operation.httpMethod || '').toUpperCase(),
|
|
73
|
+
name: operation.name,
|
|
74
|
+
requestId,
|
|
75
|
+
routePath: operation.routePath,
|
|
76
|
+
...(operation.schemas ? { schemas: operation.schemas } : {}),
|
|
77
|
+
});
|
|
78
|
+
|
|
79
|
+
export async function digestOperationContract(
|
|
80
|
+
operation: OperationContractHashInput,
|
|
81
|
+
requestId: string,
|
|
82
|
+
): Promise<string> {
|
|
83
|
+
const digest = await globalThis.crypto.subtle.digest(
|
|
84
|
+
'SHA-256',
|
|
85
|
+
new TextEncoder().encode(serializeOperationContract(operation, requestId)),
|
|
86
|
+
);
|
|
87
|
+
return Array.from(new Uint8Array(digest), value =>
|
|
88
|
+
value.toString(16).padStart(2, '0'),
|
|
89
|
+
).join('');
|
|
90
|
+
}
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
import { existsSync, readFileSync } from 'node:fs';
|
|
2
|
+
import path from 'node:path';
|
|
3
|
+
import { deriveOperationVersion } from './operationIdentity';
|
|
4
|
+
|
|
5
|
+
/** Resolve the owning package once. Never borrow an ancestor's version when
|
|
6
|
+
* the producer has an absent or invalid version of its own. */
|
|
7
|
+
export function resolveOperationProducer(options: {
|
|
8
|
+
directories: Array<string | undefined>;
|
|
9
|
+
requestId?: string;
|
|
10
|
+
onDependency?: (filename: string) => void;
|
|
11
|
+
}) {
|
|
12
|
+
for (const directory of options.directories) {
|
|
13
|
+
if (!directory) continue;
|
|
14
|
+
let current = path.resolve(directory);
|
|
15
|
+
while (true) {
|
|
16
|
+
const filename = path.join(current, 'package.json');
|
|
17
|
+
if (existsSync(filename)) {
|
|
18
|
+
options.onDependency?.(filename);
|
|
19
|
+
let metadata: { name?: unknown; version?: unknown } = {};
|
|
20
|
+
try {
|
|
21
|
+
metadata = JSON.parse(readFileSync(filename, 'utf8')) ?? {};
|
|
22
|
+
} catch {}
|
|
23
|
+
return {
|
|
24
|
+
requestId:
|
|
25
|
+
options.requestId?.trim() ||
|
|
26
|
+
(typeof metadata.name === 'string' && metadata.name.trim()) ||
|
|
27
|
+
'default',
|
|
28
|
+
operationVersion: deriveOperationVersion(metadata.version),
|
|
29
|
+
};
|
|
30
|
+
}
|
|
31
|
+
const parent = path.dirname(current);
|
|
32
|
+
if (parent === current) break;
|
|
33
|
+
current = parent;
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
return {
|
|
37
|
+
requestId: options.requestId?.trim() || 'default',
|
|
38
|
+
operationVersion: deriveOperationVersion(),
|
|
39
|
+
};
|
|
40
|
+
}
|
|
@@ -1,11 +1,41 @@
|
|
|
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 {
|
|
4
5
|
getLoaderCtx,
|
|
5
6
|
type MiddlewareHandler,
|
|
7
|
+
type ServerLoaderBundle,
|
|
6
8
|
type ServerPlugin,
|
|
7
9
|
} from '@modern-js/server-core';
|
|
8
10
|
|
|
11
|
+
function expandCanonicalRoutes(routes: ServerLoaderBundle['routes']) {
|
|
12
|
+
const languages = new Set<string>();
|
|
13
|
+
const localisedUrls: Record<string, Record<string, string>> = {};
|
|
14
|
+
let physical = false;
|
|
15
|
+
const visit = (nodes: ServerLoaderBundle['routes']) => {
|
|
16
|
+
for (const route of nodes) {
|
|
17
|
+
physical ||= 'modernCanonicalPath' in route;
|
|
18
|
+
const identity = Reflect.get(route, 'modernLocalisedRoute') as
|
|
19
|
+
| { canonicalPath: string; paths?: Record<string, string> }
|
|
20
|
+
| undefined;
|
|
21
|
+
if (identity?.paths) {
|
|
22
|
+
localisedUrls[identity.canonicalPath] = identity.paths;
|
|
23
|
+
for (const language of Object.keys(identity.paths))
|
|
24
|
+
languages.add(language);
|
|
25
|
+
}
|
|
26
|
+
if ('children' in route && route.children) visit(route.children);
|
|
27
|
+
}
|
|
28
|
+
};
|
|
29
|
+
visit(routes);
|
|
30
|
+
return physical || languages.size === 0
|
|
31
|
+
? routes
|
|
32
|
+
: (applyLocalisedUrlsToRoutes(
|
|
33
|
+
routes,
|
|
34
|
+
[...languages],
|
|
35
|
+
localisedUrls,
|
|
36
|
+
) as typeof routes);
|
|
37
|
+
}
|
|
38
|
+
|
|
9
39
|
export function injectLocalisedLoaderPlugin(): ServerPlugin {
|
|
10
40
|
return {
|
|
11
41
|
name: '@modern-js/localised-loader',
|
|
@@ -19,6 +49,7 @@ export function injectLocalisedLoaderPlugin(): ServerPlugin {
|
|
|
19
49
|
requestedRouteId,
|
|
20
50
|
matchedRouteIds,
|
|
21
51
|
),
|
|
52
|
+
expandCanonicalRoutes,
|
|
22
53
|
);
|
|
23
54
|
await next();
|
|
24
55
|
};
|
|
@@ -15,6 +15,8 @@ type FetchLike = (
|
|
|
15
15
|
input: string,
|
|
16
16
|
init?: {
|
|
17
17
|
signal?: AbortSignal;
|
|
18
|
+
/** Trusted fetchers must honor this: reject instead of following a redirect. */
|
|
19
|
+
redirect?: 'error';
|
|
18
20
|
},
|
|
19
21
|
) => Promise<Response>;
|
|
20
22
|
|
|
@@ -34,7 +36,7 @@ const fetchJsonWithTimeout = async (
|
|
|
34
36
|
|
|
35
37
|
try {
|
|
36
38
|
const response = await Promise.race([
|
|
37
|
-
fetcher(url, { signal: controller.signal }),
|
|
39
|
+
fetcher(url, { signal: controller.signal, redirect: 'error' }),
|
|
38
40
|
new Promise<Response>((_, reject) => {
|
|
39
41
|
timeoutId = setTimeout(() => {
|
|
40
42
|
controller.abort();
|
|
@@ -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
|
+
});
|
|
@@ -1,4 +1,7 @@
|
|
|
1
1
|
import 'reflect-metadata';
|
|
2
|
+
import fs from 'node:fs';
|
|
3
|
+
import os from 'node:os';
|
|
4
|
+
import path from 'node:path';
|
|
2
5
|
import { z } from 'zod';
|
|
3
6
|
import {
|
|
4
7
|
buildOperationContractMap,
|
|
@@ -8,6 +11,8 @@ import {
|
|
|
8
11
|
deriveOperationVersion,
|
|
9
12
|
type OperationContractSource,
|
|
10
13
|
} from '../../src/bff-policy/operationContracts';
|
|
14
|
+
import { digestOperationContract } from '../../src/bff-policy/operationIdentity';
|
|
15
|
+
import { resolveOperationProducer } from '../../src/bff-policy/producer';
|
|
11
16
|
|
|
12
17
|
const createSchemaHandler = (schema: z.ZodType) => {
|
|
13
18
|
const handler = () => ({ ok: true });
|
|
@@ -16,6 +21,58 @@ const createSchemaHandler = (schema: z.ZodType) => {
|
|
|
16
21
|
};
|
|
17
22
|
|
|
18
23
|
describe('operation contract utilities', () => {
|
|
24
|
+
test('browser and Node hash the same canonical operation including nested schemas', async () => {
|
|
25
|
+
const operation = {
|
|
26
|
+
name: 'café',
|
|
27
|
+
httpMethod: 'post',
|
|
28
|
+
routePath: '/api/:id',
|
|
29
|
+
schemas: {
|
|
30
|
+
QUERY: {
|
|
31
|
+
properties: { z: { type: 'number' }, a: { enum: ['x', 'y'] } },
|
|
32
|
+
},
|
|
33
|
+
},
|
|
34
|
+
};
|
|
35
|
+
expect(await digestOperationContract(operation, 'producer')).toBe(
|
|
36
|
+
createOperationContractHash(operation, 'producer'),
|
|
37
|
+
);
|
|
38
|
+
});
|
|
39
|
+
|
|
40
|
+
test('resolves the owning producer beyond former depth limits and never borrows its parent version', () => {
|
|
41
|
+
const root = fs.mkdtempSync(path.join(os.tmpdir(), 'bff-producer-'));
|
|
42
|
+
const producer = path.join(root, 'producer');
|
|
43
|
+
const nested = path.join(
|
|
44
|
+
producer,
|
|
45
|
+
...Array.from({ length: 35 }, () => 'nested'),
|
|
46
|
+
);
|
|
47
|
+
fs.mkdirSync(nested, { recursive: true });
|
|
48
|
+
fs.writeFileSync(
|
|
49
|
+
path.join(root, 'package.json'),
|
|
50
|
+
JSON.stringify({ version: '99.0.0' }),
|
|
51
|
+
);
|
|
52
|
+
fs.writeFileSync(
|
|
53
|
+
path.join(producer, 'package.json'),
|
|
54
|
+
JSON.stringify({ name: 'producer', version: '3.1.0' }),
|
|
55
|
+
);
|
|
56
|
+
try {
|
|
57
|
+
const dependencies: string[] = [];
|
|
58
|
+
expect(
|
|
59
|
+
resolveOperationProducer({
|
|
60
|
+
directories: [nested, root],
|
|
61
|
+
onDependency: file => dependencies.push(file),
|
|
62
|
+
}),
|
|
63
|
+
).toEqual({ requestId: 'producer', operationVersion: 3 });
|
|
64
|
+
expect(dependencies).toEqual([path.join(producer, 'package.json')]);
|
|
65
|
+
fs.writeFileSync(path.join(producer, 'package.json'), '{broken');
|
|
66
|
+
expect(
|
|
67
|
+
resolveOperationProducer({
|
|
68
|
+
directories: [nested, root],
|
|
69
|
+
requestId: ' explicit ',
|
|
70
|
+
}),
|
|
71
|
+
).toEqual({ requestId: 'explicit', operationVersion: 1 });
|
|
72
|
+
} finally {
|
|
73
|
+
fs.rmSync(root, { recursive: true, force: true });
|
|
74
|
+
}
|
|
75
|
+
});
|
|
19
76
|
test('creates deterministic operation entries and aggregate hashes', () => {
|
|
20
77
|
const handlers = [
|
|
21
78
|
{
|
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { handleRequest } from '@modern-js/plugin-data-loader/runtime';
|
|
2
|
+
import { applyLocalisedUrlsToRoutes } from '@modern-js/runtime-extensions/localised-urls';
|
|
2
3
|
import {
|
|
3
4
|
createServerBase,
|
|
4
5
|
getLoaderCtx,
|
|
@@ -40,10 +41,12 @@ const routes = [
|
|
|
40
41
|
})),
|
|
41
42
|
];
|
|
42
43
|
|
|
44
|
+
let activeRoutes = routes;
|
|
45
|
+
|
|
43
46
|
const render: MiddlewareHandler = async context =>
|
|
44
47
|
(await handleRequest({
|
|
45
48
|
request: context.req.raw,
|
|
46
|
-
routes,
|
|
49
|
+
routes: activeRoutes,
|
|
47
50
|
serverRoutes: [
|
|
48
51
|
{
|
|
49
52
|
urlPath: '/base',
|
|
@@ -118,3 +121,35 @@ test('retains canonical routes and rejects loader IDs from another route', async
|
|
|
118
121
|
expect((await load('/base/cs/zdroje/item', 'search'))?.status).toBe(403);
|
|
119
122
|
expect((await load('/base/cs/hledat', 'missing'))?.status).toBe(403);
|
|
120
123
|
});
|
|
124
|
+
|
|
125
|
+
test('projects canonical routes for loader matching without rewriting the request URL', async () => {
|
|
126
|
+
const previous = activeRoutes;
|
|
127
|
+
activeRoutes = applyLocalisedUrlsToRoutes(
|
|
128
|
+
[
|
|
129
|
+
{
|
|
130
|
+
type: 'nested',
|
|
131
|
+
id: 'search',
|
|
132
|
+
path: ':lang/search',
|
|
133
|
+
loader: ({ params, request }) =>
|
|
134
|
+
Response.json({
|
|
135
|
+
language: params.lang,
|
|
136
|
+
url: request.url,
|
|
137
|
+
}),
|
|
138
|
+
},
|
|
139
|
+
] satisfies NestedRoute[],
|
|
140
|
+
['en', 'cs'],
|
|
141
|
+
{ '/search': { en: '/search', cs: '/hledat' } },
|
|
142
|
+
'canonical',
|
|
143
|
+
) as typeof routes;
|
|
144
|
+
try {
|
|
145
|
+
const response = await load('/base/cs/hledat?q=tractor', 'search');
|
|
146
|
+
expect(response?.status).toBe(200);
|
|
147
|
+
expect(await response?.json()).toEqual({
|
|
148
|
+
language: 'cs',
|
|
149
|
+
url: 'https://example.test/base/cs/hledat?q=tractor&__loader=search',
|
|
150
|
+
});
|
|
151
|
+
expect((await load('/base/cs/hledat', 'missing'))?.status).toBe(403);
|
|
152
|
+
} finally {
|
|
153
|
+
activeRoutes = previous;
|
|
154
|
+
}
|
|
155
|
+
});
|
|
@@ -1,4 +1,6 @@
|
|
|
1
1
|
import { mkdtemp, rm, writeFile } from 'node:fs/promises';
|
|
2
|
+
import type { Server } from 'node:http';
|
|
3
|
+
import { createServer } from 'node:http';
|
|
2
4
|
import os from 'node:os';
|
|
3
5
|
import path from 'node:path';
|
|
4
6
|
import {
|
|
@@ -7,6 +9,19 @@ import {
|
|
|
7
9
|
createModuleFederationCssCollector,
|
|
8
10
|
} from '../src/module-federation-css';
|
|
9
11
|
|
|
12
|
+
const listen = async (server: Server) => {
|
|
13
|
+
await new Promise<void>((resolve, reject) => {
|
|
14
|
+
server.once('error', reject);
|
|
15
|
+
server.listen(0, '127.0.0.1', () => resolve());
|
|
16
|
+
});
|
|
17
|
+
const address = server.address();
|
|
18
|
+
const port = typeof address === 'object' && address ? address.port : 0;
|
|
19
|
+
return `http://127.0.0.1:${String(port)}`;
|
|
20
|
+
};
|
|
21
|
+
|
|
22
|
+
const closeServer = (server: Server) =>
|
|
23
|
+
new Promise<void>(resolve => server.close(() => resolve()));
|
|
24
|
+
|
|
10
25
|
const tempDirs: string[] = [];
|
|
11
26
|
|
|
12
27
|
const createTempDir = async () => {
|
|
@@ -210,6 +225,92 @@ describe('module federation css collection', () => {
|
|
|
210
225
|
});
|
|
211
226
|
});
|
|
212
227
|
|
|
228
|
+
describe('module federation css native fetch redirect policy', () => {
|
|
229
|
+
it('never contacts a redirect destination and marks the remote errored', async () => {
|
|
230
|
+
const servers: Server[] = [];
|
|
231
|
+
try {
|
|
232
|
+
let destinationHits = 0;
|
|
233
|
+
const destinationServer = createServer((_req, res) => {
|
|
234
|
+
destinationHits += 1;
|
|
235
|
+
res.statusCode = 200;
|
|
236
|
+
res.setHeader('content-type', 'application/json');
|
|
237
|
+
res.end(JSON.stringify({ exposes: [] }));
|
|
238
|
+
});
|
|
239
|
+
servers.push(destinationServer);
|
|
240
|
+
const destinationOrigin = await listen(destinationServer);
|
|
241
|
+
|
|
242
|
+
const redirectServer = createServer((_req, res) => {
|
|
243
|
+
res.statusCode = 302;
|
|
244
|
+
res.setHeader('location', `${destinationOrigin}/mf-manifest.json`);
|
|
245
|
+
res.end();
|
|
246
|
+
});
|
|
247
|
+
servers.push(redirectServer);
|
|
248
|
+
const redirectOrigin = await listen(redirectServer);
|
|
249
|
+
|
|
250
|
+
const pwd = await createTempDir();
|
|
251
|
+
await writeFile(
|
|
252
|
+
path.join(pwd, 'mf-manifest.json'),
|
|
253
|
+
JSON.stringify({
|
|
254
|
+
remotes: [{ entry: `${redirectOrigin}/mf-manifest.json` }],
|
|
255
|
+
}),
|
|
256
|
+
);
|
|
257
|
+
|
|
258
|
+
const warn = rs.fn();
|
|
259
|
+
const result = await collectDirectRemoteModuleFederationCssWithMeta(pwd, {
|
|
260
|
+
monitors: { warn } as any,
|
|
261
|
+
});
|
|
262
|
+
|
|
263
|
+
expect(result.assets).toEqual([]);
|
|
264
|
+
expect(result.errored).toBe(true);
|
|
265
|
+
expect(destinationHits).toBe(0);
|
|
266
|
+
expect(warn).toHaveBeenCalledTimes(1);
|
|
267
|
+
} finally {
|
|
268
|
+
await Promise.all(servers.map(closeServer));
|
|
269
|
+
}
|
|
270
|
+
});
|
|
271
|
+
|
|
272
|
+
it('still collects CSS from a direct loopback manifest (safe direct control)', async () => {
|
|
273
|
+
const servers: Server[] = [];
|
|
274
|
+
try {
|
|
275
|
+
const directServer = createServer((_req, res) => {
|
|
276
|
+
res.statusCode = 200;
|
|
277
|
+
res.setHeader('content-type', 'application/json');
|
|
278
|
+
res.end(
|
|
279
|
+
JSON.stringify({
|
|
280
|
+
metaData: { publicPath: 'https://cdn.example.com/direct/' },
|
|
281
|
+
exposes: [
|
|
282
|
+
{
|
|
283
|
+
assets: {
|
|
284
|
+
css: { sync: ['static/css/direct.css'] },
|
|
285
|
+
},
|
|
286
|
+
},
|
|
287
|
+
],
|
|
288
|
+
}),
|
|
289
|
+
);
|
|
290
|
+
});
|
|
291
|
+
servers.push(directServer);
|
|
292
|
+
const directOrigin = await listen(directServer);
|
|
293
|
+
|
|
294
|
+
const pwd = await createTempDir();
|
|
295
|
+
await writeFile(
|
|
296
|
+
path.join(pwd, 'mf-manifest.json'),
|
|
297
|
+
JSON.stringify({
|
|
298
|
+
remotes: [{ entry: `${directOrigin}/mf-manifest.json` }],
|
|
299
|
+
}),
|
|
300
|
+
);
|
|
301
|
+
|
|
302
|
+
const result = await collectDirectRemoteModuleFederationCssWithMeta(pwd);
|
|
303
|
+
|
|
304
|
+
expect(result.assets).toEqual([
|
|
305
|
+
'https://cdn.example.com/direct/static/css/direct.css',
|
|
306
|
+
]);
|
|
307
|
+
expect(result.errored).toBe(false);
|
|
308
|
+
} finally {
|
|
309
|
+
await Promise.all(servers.map(closeServer));
|
|
310
|
+
}
|
|
311
|
+
});
|
|
312
|
+
});
|
|
313
|
+
|
|
213
314
|
describe('module federation css collector cache', () => {
|
|
214
315
|
const writeHostManifest = async () => {
|
|
215
316
|
const pwd = await createTempDir();
|