@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
|
@@ -5,67 +5,17 @@ import 'reflect-metadata';
|
|
|
5
5
|
// Structural handler protocol; native operators retain ownership of metadata.
|
|
6
6
|
type ApiHandler = (...args: any) => any | Promise<any>;
|
|
7
7
|
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
routePath: string;
|
|
20
|
-
operationId: string;
|
|
21
|
-
handlerName: string;
|
|
22
|
-
filename?: string;
|
|
23
|
-
};
|
|
24
|
-
|
|
25
|
-
export type OperationContractMap = Record<string, OperationContractDefinition>;
|
|
26
|
-
|
|
27
|
-
export const DEFAULT_OPERATION_VERSION = 1;
|
|
28
|
-
|
|
29
|
-
/**
|
|
30
|
-
* Derives the operation version from a producer package version: the semver
|
|
31
|
-
* major is the contract version, so consumers regenerated against an older
|
|
32
|
-
* producer major fail the `operation_version_mismatch` gate instead of
|
|
33
|
-
* silently calling an incompatible API.
|
|
34
|
-
*
|
|
35
|
-
* Falls back to {@link DEFAULT_OPERATION_VERSION} when no parseable version
|
|
36
|
-
* is available.
|
|
37
|
-
*/
|
|
38
|
-
export const deriveOperationVersion = (packageVersion?: unknown): number => {
|
|
39
|
-
if (typeof packageVersion !== 'string') {
|
|
40
|
-
return DEFAULT_OPERATION_VERSION;
|
|
41
|
-
}
|
|
42
|
-
const match = packageVersion.trim().match(/^v?(\d+)\./);
|
|
43
|
-
if (!match) {
|
|
44
|
-
return DEFAULT_OPERATION_VERSION;
|
|
45
|
-
}
|
|
46
|
-
const major = Number.parseInt(match[1]!, 10);
|
|
47
|
-
return Number.isInteger(major) && major >= 0
|
|
48
|
-
? major
|
|
49
|
-
: DEFAULT_OPERATION_VERSION;
|
|
50
|
-
};
|
|
51
|
-
|
|
52
|
-
/** JSON.stringify with recursively sorted object keys for stable hashing. */
|
|
53
|
-
const stableStringify = (value: unknown): string => {
|
|
54
|
-
if (Array.isArray(value)) {
|
|
55
|
-
return `[${value.map(item => stableStringify(item)).join(',')}]`;
|
|
56
|
-
}
|
|
57
|
-
if (value && typeof value === 'object') {
|
|
58
|
-
const entries = Object.entries(value as Record<string, unknown>)
|
|
59
|
-
.filter(([, entryValue]) => typeof entryValue !== 'undefined')
|
|
60
|
-
.sort(([a], [b]) => a.localeCompare(b))
|
|
61
|
-
.map(
|
|
62
|
-
([key, entryValue]) =>
|
|
63
|
-
`${JSON.stringify(key)}:${stableStringify(entryValue)}`,
|
|
64
|
-
);
|
|
65
|
-
return `{${entries.join(',')}}`;
|
|
66
|
-
}
|
|
67
|
-
return JSON.stringify(value) ?? 'null';
|
|
68
|
-
};
|
|
8
|
+
import {
|
|
9
|
+
DEFAULT_OPERATION_VERSION,
|
|
10
|
+
type OperationContractDefinition,
|
|
11
|
+
type OperationContractEntry,
|
|
12
|
+
type OperationContractHashInput,
|
|
13
|
+
type OperationContractMap,
|
|
14
|
+
serializeOperationContract,
|
|
15
|
+
stableOperationStringify,
|
|
16
|
+
} from './operationIdentity';
|
|
17
|
+
|
|
18
|
+
export * from './operationIdentity';
|
|
69
19
|
|
|
70
20
|
const sha256 = (text: string) =>
|
|
71
21
|
createHash('sha256').update(text).digest('hex');
|
|
@@ -156,33 +106,10 @@ export const serializeOperationSchemas = (
|
|
|
156
106
|
return Object.keys(serialized).length > 0 ? serialized : undefined;
|
|
157
107
|
};
|
|
158
108
|
|
|
159
|
-
export type OperationContractHashInput = OperationContractEntry & {
|
|
160
|
-
/** Serialized schema documents; omit for schema-less operations. */
|
|
161
|
-
schemas?: Record<string, unknown> | undefined;
|
|
162
|
-
};
|
|
163
|
-
|
|
164
|
-
/**
|
|
165
|
-
* Per-operation contract hash. The hash covers the operation identity
|
|
166
|
-
* (name, method, route, producer requestId) plus the serialized input
|
|
167
|
-
* schemas, so:
|
|
168
|
-
*
|
|
169
|
-
* - changing an input schema changes the hash of exactly that operation;
|
|
170
|
-
* - reordering routes or adding unrelated operations never rotates the hash
|
|
171
|
-
* of other operations (each operation is hashed independently).
|
|
172
|
-
*/
|
|
173
109
|
export const createOperationContractHash = (
|
|
174
110
|
operation: OperationContractHashInput,
|
|
175
111
|
requestId: string,
|
|
176
|
-
): string =>
|
|
177
|
-
sha256(
|
|
178
|
-
stableStringify({
|
|
179
|
-
httpMethod: String(operation.httpMethod || '').toUpperCase(),
|
|
180
|
-
name: operation.name,
|
|
181
|
-
requestId,
|
|
182
|
-
routePath: operation.routePath,
|
|
183
|
-
...(operation.schemas ? { schemas: operation.schemas } : {}),
|
|
184
|
-
}),
|
|
185
|
-
);
|
|
112
|
+
): string => sha256(serializeOperationContract(operation, requestId));
|
|
186
113
|
|
|
187
114
|
export const createOperationEntries = (
|
|
188
115
|
handlers: Array<{
|
|
@@ -213,7 +140,7 @@ export const createOperationSchemaHash = (
|
|
|
213
140
|
requestId: string,
|
|
214
141
|
): string =>
|
|
215
142
|
sha256(
|
|
216
|
-
|
|
143
|
+
stableOperationStringify({
|
|
217
144
|
operations: [...operationEntries]
|
|
218
145
|
.map(item => ({
|
|
219
146
|
hash:
|
|
@@ -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
|
+
}
|
|
@@ -9,7 +9,6 @@ import { tryResolveBuiltinSnapshotStore } from './http-store';
|
|
|
9
9
|
import type {
|
|
10
10
|
ContractGateSnapshotStore,
|
|
11
11
|
ContractGateSnapshotStoreFactory,
|
|
12
|
-
ContractGateSnapshotStoreFactoryContext,
|
|
13
12
|
ContractGateSnapshotStoreModule,
|
|
14
13
|
ContractGateSnapshotStoreUserConfig,
|
|
15
14
|
GateSnapshot,
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
import type { LoaderRouteIdResolver } from '@modern-js/plugin-data-loader/runtime';
|
|
2
|
+
import { resolveLocalisedLoaderRouteId } from '@modern-js/runtime-extensions/localised-loader-identity';
|
|
3
|
+
import { applyLocalisedUrlsToRoutes } from '@modern-js/runtime-extensions/localised-urls';
|
|
4
|
+
import type { ServerLoaderBundle } from '@modern-js/server-core';
|
|
5
|
+
|
|
6
|
+
type LoaderRoutes = ServerLoaderBundle['routes'];
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* Project canonical-only localized routes so loader matching sees every
|
|
10
|
+
* localized URL. Routes already emitted as physical localized aliases are
|
|
11
|
+
* returned unchanged.
|
|
12
|
+
*
|
|
13
|
+
* Runtime-neutral: shared by the Node server plugin and the Cloudflare worker
|
|
14
|
+
* route data handler.
|
|
15
|
+
*/
|
|
16
|
+
export function expandLocalisedLoaderRoutes(
|
|
17
|
+
routes: LoaderRoutes,
|
|
18
|
+
): LoaderRoutes {
|
|
19
|
+
const languages = new Set<string>();
|
|
20
|
+
const localisedUrls: Record<string, Record<string, string>> = {};
|
|
21
|
+
let physical = false;
|
|
22
|
+
const visit = (nodes: LoaderRoutes) => {
|
|
23
|
+
for (const route of nodes) {
|
|
24
|
+
physical ||= 'modernCanonicalPath' in route;
|
|
25
|
+
const identity = Reflect.get(route, 'modernLocalisedRoute') as
|
|
26
|
+
| { canonicalPath: string; paths?: Record<string, string> }
|
|
27
|
+
| undefined;
|
|
28
|
+
if (identity?.paths) {
|
|
29
|
+
localisedUrls[identity.canonicalPath] = identity.paths;
|
|
30
|
+
for (const language of Object.keys(identity.paths))
|
|
31
|
+
languages.add(language);
|
|
32
|
+
}
|
|
33
|
+
if ('children' in route && route.children) visit(route.children);
|
|
34
|
+
}
|
|
35
|
+
};
|
|
36
|
+
visit(routes);
|
|
37
|
+
return physical || languages.size === 0
|
|
38
|
+
? routes
|
|
39
|
+
: (applyLocalisedUrlsToRoutes(
|
|
40
|
+
routes,
|
|
41
|
+
[...languages],
|
|
42
|
+
localisedUrls,
|
|
43
|
+
) as LoaderRoutes);
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
/** Resolve a canonical loader route ID to the localized alias matched by the URL. */
|
|
47
|
+
export const resolveLocalisedLoaderRoute: LoaderRouteIdResolver = (
|
|
48
|
+
requestedRouteId,
|
|
49
|
+
{ routes, matchedRouteIds },
|
|
50
|
+
) => resolveLocalisedLoaderRouteId(routes, requestedRouteId, matchedRouteIds);
|
|
@@ -1,10 +1,13 @@
|
|
|
1
1
|
import { setLoaderRouteIdResolver } from '@modern-js/plugin-data-loader/runtime';
|
|
2
|
-
import { resolveLocalisedLoaderRouteId } from '@modern-js/runtime-extensions/localised-loader-identity';
|
|
3
2
|
import {
|
|
4
3
|
getLoaderCtx,
|
|
5
4
|
type MiddlewareHandler,
|
|
6
5
|
type ServerPlugin,
|
|
7
6
|
} from '@modern-js/server-core';
|
|
7
|
+
import {
|
|
8
|
+
expandLocalisedLoaderRoutes,
|
|
9
|
+
resolveLocalisedLoaderRoute,
|
|
10
|
+
} from './localisedLoader';
|
|
8
11
|
|
|
9
12
|
export function injectLocalisedLoaderPlugin(): ServerPlugin {
|
|
10
13
|
return {
|
|
@@ -13,12 +16,8 @@ export function injectLocalisedLoaderPlugin(): ServerPlugin {
|
|
|
13
16
|
const handler: MiddlewareHandler = async (context, next) => {
|
|
14
17
|
setLoaderRouteIdResolver(
|
|
15
18
|
getLoaderCtx(context),
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
routes,
|
|
19
|
-
requestedRouteId,
|
|
20
|
-
matchedRouteIds,
|
|
21
|
-
),
|
|
19
|
+
resolveLocalisedLoaderRoute,
|
|
20
|
+
expandLocalisedLoaderRoutes,
|
|
22
21
|
);
|
|
23
22
|
await next();
|
|
24
23
|
};
|
|
@@ -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();
|
package/src/serverConfig.ts
CHANGED
|
@@ -2,6 +2,7 @@ import type {} from '@modern-js/plugin/server';
|
|
|
2
2
|
import type {
|
|
3
3
|
BffRuntimeUserConfig,
|
|
4
4
|
BffRuntimeFramework as CanonicalBffRuntimeFramework,
|
|
5
|
+
ServerSourceConfigExtension,
|
|
5
6
|
ServerTelemetryConfigExtension,
|
|
6
7
|
} from '@modern-js/runtime-extensions/server-config';
|
|
7
8
|
import type { BffUserConfig, ServerUserConfig } from '@modern-js/server-core';
|
|
@@ -20,6 +21,7 @@ declare module '@modern-js/plugin/server' {
|
|
|
20
21
|
declare module '@modern-js/server-core' {
|
|
21
22
|
interface BffUserConfig extends BffConfigExtension {}
|
|
22
23
|
interface ServerUserConfig extends ServerTelemetryConfigExtension {}
|
|
24
|
+
interface SourceUserConfig extends ServerSourceConfigExtension {}
|
|
23
25
|
}
|
|
24
26
|
|
|
25
27
|
export type UltramodernBffUserConfig = Omit<
|
|
@@ -19,6 +19,11 @@ type ModuleFederationManifest = {
|
|
|
19
19
|
name?: string;
|
|
20
20
|
};
|
|
21
21
|
publicPath?: string;
|
|
22
|
+
ssrRemoteEntry?: {
|
|
23
|
+
path?: string;
|
|
24
|
+
name?: string;
|
|
25
|
+
};
|
|
26
|
+
ssrPublicPath?: string;
|
|
22
27
|
types?: {
|
|
23
28
|
path?: string;
|
|
24
29
|
zip?: string;
|
|
@@ -106,11 +111,111 @@ const appendModuleFederationAsset = (set: Set<string>, assetPath?: string) => {
|
|
|
106
111
|
const appendModuleFederationAssets = (
|
|
107
112
|
set: Set<string>,
|
|
108
113
|
assets?: ModuleFederationAssets,
|
|
114
|
+
directory = '',
|
|
115
|
+
) => {
|
|
116
|
+
for (const asset of [
|
|
117
|
+
...(assets?.js?.sync ?? []),
|
|
118
|
+
...(assets?.js?.async ?? []),
|
|
119
|
+
...(assets?.css?.sync ?? []),
|
|
120
|
+
...(assets?.css?.async ?? []),
|
|
121
|
+
]) {
|
|
122
|
+
appendModuleFederationAsset(
|
|
123
|
+
set,
|
|
124
|
+
joinModuleFederationAssetPath(directory, asset),
|
|
125
|
+
);
|
|
126
|
+
}
|
|
127
|
+
};
|
|
128
|
+
|
|
129
|
+
const appendModuleFederationManifestAssets = (
|
|
130
|
+
set: Set<string>,
|
|
131
|
+
manifest: ModuleFederationManifest,
|
|
132
|
+
directory?: string,
|
|
133
|
+
) => {
|
|
134
|
+
for (const item of [
|
|
135
|
+
...(manifest.shared ?? []),
|
|
136
|
+
...(manifest.remotes ?? []),
|
|
137
|
+
...(manifest.exposes ?? []),
|
|
138
|
+
]) {
|
|
139
|
+
appendModuleFederationAssets(set, item.assets, directory);
|
|
140
|
+
}
|
|
141
|
+
};
|
|
142
|
+
|
|
143
|
+
/**
|
|
144
|
+
* The output directory of the Node container a server-rendering host loads,
|
|
145
|
+
* which the browser manifest publishes as its SSR snapshot: `ssrRemoteEntry`
|
|
146
|
+
* resolved against `ssrPublicPath`. A container published elsewhere has no
|
|
147
|
+
* files here.
|
|
148
|
+
*/
|
|
149
|
+
const getServerContainerDirectory = (
|
|
150
|
+
metaData: ModuleFederationManifest['metaData'],
|
|
151
|
+
) => {
|
|
152
|
+
const { publicPath, ssrPublicPath } = metaData ?? {};
|
|
153
|
+
if (
|
|
154
|
+
typeof publicPath !== 'string' ||
|
|
155
|
+
typeof ssrPublicPath !== 'string' ||
|
|
156
|
+
!ssrPublicPath.startsWith(publicPath)
|
|
157
|
+
) {
|
|
158
|
+
return undefined;
|
|
159
|
+
}
|
|
160
|
+
const directory = ssrPublicPath
|
|
161
|
+
.slice(publicPath.length)
|
|
162
|
+
.replace(/^\/+|\/+$/gu, '');
|
|
163
|
+
return directory &&
|
|
164
|
+
directory
|
|
165
|
+
.split('/')
|
|
166
|
+
.every(segment => segment && segment !== '.' && segment !== '..')
|
|
167
|
+
? directory
|
|
168
|
+
: undefined;
|
|
169
|
+
};
|
|
170
|
+
|
|
171
|
+
const readModuleFederationManifest = async (manifestPath: string) => {
|
|
172
|
+
const manifestBuffer = await fileReader.readFileFromSystem(
|
|
173
|
+
manifestPath,
|
|
174
|
+
'buffer',
|
|
175
|
+
);
|
|
176
|
+
if (manifestBuffer === null) {
|
|
177
|
+
return undefined;
|
|
178
|
+
}
|
|
179
|
+
try {
|
|
180
|
+
return JSON.parse(
|
|
181
|
+
manifestBuffer.toString('utf-8'),
|
|
182
|
+
) as ModuleFederationManifest;
|
|
183
|
+
} catch {
|
|
184
|
+
return undefined;
|
|
185
|
+
}
|
|
186
|
+
};
|
|
187
|
+
|
|
188
|
+
/**
|
|
189
|
+
* Serve the Node container's entry and the chunks its own manifest names, as
|
|
190
|
+
* native development does. The application's server bundles share that
|
|
191
|
+
* directory and stay private.
|
|
192
|
+
*/
|
|
193
|
+
const appendServerContainerAssets = async (
|
|
194
|
+
pwd: string,
|
|
195
|
+
set: Set<string>,
|
|
196
|
+
metaData: ModuleFederationManifest['metaData'],
|
|
109
197
|
) => {
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
198
|
+
const directory = getServerContainerDirectory(metaData);
|
|
199
|
+
const remoteEntry = joinModuleFederationAssetPath(
|
|
200
|
+
metaData?.ssrRemoteEntry?.path,
|
|
201
|
+
metaData?.ssrRemoteEntry?.name,
|
|
202
|
+
);
|
|
203
|
+
if (!directory || !remoteEntry) {
|
|
204
|
+
return;
|
|
205
|
+
}
|
|
206
|
+
set.add(joinModuleFederationAssetPath(directory, remoteEntry));
|
|
207
|
+
const manifestPath = path.join(
|
|
208
|
+
pwd,
|
|
209
|
+
directory,
|
|
210
|
+
MODULE_FEDERATION_MANIFEST_FILE,
|
|
211
|
+
);
|
|
212
|
+
if (!(await fs.pathExists(manifestPath))) {
|
|
213
|
+
return;
|
|
214
|
+
}
|
|
215
|
+
const manifest = await readModuleFederationManifest(manifestPath);
|
|
216
|
+
if (manifest) {
|
|
217
|
+
appendModuleFederationManifestAssets(set, manifest, directory);
|
|
218
|
+
}
|
|
114
219
|
};
|
|
115
220
|
|
|
116
221
|
const hasAbsoluteProtocol = (value: string) =>
|
|
@@ -145,9 +250,20 @@ export const patchModuleFederationManifestPublicPath = (
|
|
|
145
250
|
const prefixPath = ensureTrailingSlash(
|
|
146
251
|
ensureLeadingSlash(pathPrefix || '/'),
|
|
147
252
|
);
|
|
253
|
+
const servedPublicPath = `${requestURL.origin}${prefixPath}`;
|
|
254
|
+
// A native remote publishes its Node container under the browser public
|
|
255
|
+
// path; a server-rendering host needs that path absolute too.
|
|
256
|
+
const { ssrPublicPath } = manifest.metaData ?? {};
|
|
148
257
|
manifest.metaData = {
|
|
149
258
|
...manifest.metaData,
|
|
150
|
-
publicPath:
|
|
259
|
+
publicPath: servedPublicPath,
|
|
260
|
+
...(typeof ssrPublicPath === 'string' &&
|
|
261
|
+
!hasAbsoluteProtocol(ssrPublicPath) &&
|
|
262
|
+
ssrPublicPath.startsWith(publicPath)
|
|
263
|
+
? {
|
|
264
|
+
ssrPublicPath: `${servedPublicPath}${ssrPublicPath.slice(publicPath.length).replace(/^\/+/u, '')}`,
|
|
265
|
+
}
|
|
266
|
+
: {}),
|
|
151
267
|
};
|
|
152
268
|
|
|
153
269
|
return Buffer.from(JSON.stringify(manifest), 'utf-8');
|
|
@@ -197,18 +313,12 @@ export const getModuleFederationAssetList = async (
|
|
|
197
313
|
|
|
198
314
|
manifestFound = true;
|
|
199
315
|
assets.add(manifestFile);
|
|
200
|
-
const
|
|
201
|
-
|
|
202
|
-
'buffer',
|
|
203
|
-
);
|
|
204
|
-
if (manifestBuffer === null) {
|
|
316
|
+
const manifest = await readModuleFederationManifest(manifestPath);
|
|
317
|
+
if (!manifest) {
|
|
205
318
|
continue;
|
|
206
319
|
}
|
|
207
320
|
|
|
208
321
|
try {
|
|
209
|
-
const manifest = JSON.parse(
|
|
210
|
-
manifestBuffer.toString('utf-8'),
|
|
211
|
-
) as ModuleFederationManifest;
|
|
212
322
|
const remoteEntry = joinModuleFederationAssetPath(
|
|
213
323
|
manifest.metaData?.remoteEntry?.path,
|
|
214
324
|
manifest.metaData?.remoteEntry?.name,
|
|
@@ -228,15 +338,10 @@ export const getModuleFederationAssetList = async (
|
|
|
228
338
|
}
|
|
229
339
|
appendModuleFederationAsset(assets, dtsZip);
|
|
230
340
|
appendModuleFederationAsset(assets, dtsApi);
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
appendModuleFederationAssets(assets, item.assets),
|
|
236
|
-
);
|
|
237
|
-
manifest.exposes?.forEach(item =>
|
|
238
|
-
appendModuleFederationAssets(assets, item.assets),
|
|
239
|
-
);
|
|
341
|
+
appendModuleFederationManifestAssets(assets, manifest);
|
|
342
|
+
if (manifestFile === MODULE_FEDERATION_MANIFEST_FILE) {
|
|
343
|
+
await appendServerContainerAssets(pwd, assets, manifest.metaData);
|
|
344
|
+
}
|
|
240
345
|
} catch {}
|
|
241
346
|
}
|
|
242
347
|
|
package/rslib.config.mts
DELETED
|
@@ -1,38 +0,0 @@
|
|
|
1
|
-
import {
|
|
2
|
-
RSLIB_CODE_ENTRY_GLOB,
|
|
3
|
-
rslibConfig,
|
|
4
|
-
ts7DtsConfig,
|
|
5
|
-
} from '@modern-js/rslib';
|
|
6
|
-
import { defineConfig } from '@rslib/core';
|
|
7
|
-
|
|
8
|
-
export default defineConfig({
|
|
9
|
-
...rslibConfig,
|
|
10
|
-
lib: rslibConfig.lib.map(lib =>
|
|
11
|
-
lib.id === 'esm-web'
|
|
12
|
-
? {
|
|
13
|
-
...lib,
|
|
14
|
-
source: {
|
|
15
|
-
...lib.source,
|
|
16
|
-
entry: {
|
|
17
|
-
index: [
|
|
18
|
-
RSLIB_CODE_ENTRY_GLOB,
|
|
19
|
-
'!./src/static-serving/**',
|
|
20
|
-
'!./src/bff-policy/node.ts',
|
|
21
|
-
'!./src/bff-policy/operationContracts.ts',
|
|
22
|
-
'!./src/bff-policy/resolveCrossProjectPolicy.ts',
|
|
23
|
-
],
|
|
24
|
-
},
|
|
25
|
-
},
|
|
26
|
-
}
|
|
27
|
-
: lib.id === 'esm-node'
|
|
28
|
-
? {
|
|
29
|
-
...lib,
|
|
30
|
-
dts: {
|
|
31
|
-
...ts7DtsConfig,
|
|
32
|
-
autoExtension: true,
|
|
33
|
-
distPath: './dist/types-esm',
|
|
34
|
-
},
|
|
35
|
-
}
|
|
36
|
-
: lib,
|
|
37
|
-
),
|
|
38
|
-
});
|
package/rstest.config.mts
DELETED
|
@@ -1,18 +0,0 @@
|
|
|
1
|
-
import { withTestPreset } from '@scripts/rstest-config';
|
|
2
|
-
|
|
3
|
-
export default withTestPreset({
|
|
4
|
-
root: __dirname,
|
|
5
|
-
testEnvironment: 'node',
|
|
6
|
-
globals: true,
|
|
7
|
-
tools: {
|
|
8
|
-
swc: {
|
|
9
|
-
jsc: {
|
|
10
|
-
transform: {
|
|
11
|
-
react: {
|
|
12
|
-
runtime: 'automatic',
|
|
13
|
-
},
|
|
14
|
-
},
|
|
15
|
-
},
|
|
16
|
-
},
|
|
17
|
-
},
|
|
18
|
-
});
|