@bleedingdev/modern-js-server-runtime-extensions 3.9.0-ultramodern.2 → 3.9.0-ultramodern.20
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +8 -6
- package/dist/cjs/backend-federation-security/index.js +73 -152
- package/dist/cjs/bff-policy/crossProjectPolicy.js +299 -0
- package/dist/cjs/bff-policy/index.js +69 -0
- package/dist/cjs/bff-policy/node.js +85 -0
- package/dist/cjs/bff-policy/operationContracts.js +197 -0
- package/dist/cjs/bff-policy/operationIdentity.js +81 -0
- package/dist/cjs/bff-policy/producer.js +79 -0
- package/dist/cjs/bff-policy/resolveCrossProjectPolicy.js +65 -0
- package/dist/cjs/contract-gate-snapshot-store/http-store.js +1 -8
- package/dist/cjs/errorResponsePlugin.js +57 -0
- package/dist/cjs/localisedLoader.js +65 -0
- package/dist/cjs/localisedLoaderPlugin.js +62 -0
- package/dist/cjs/mfCache.js +6 -0
- package/dist/cjs/module-federation-css/plugin.js +6 -0
- package/dist/cjs/module-federation-css/remote.js +2 -1
- package/dist/cjs/serverConfig.js +18 -0
- package/dist/cjs/serverPlugin.js +74 -0
- package/dist/cjs/static-serving/plugin.js +57 -0
- package/dist/cjs/static-serving/responders.js +158 -0
- package/dist/cjs/static-serving/staticModuleFederation.js +173 -0
- package/dist/cjs/static-serving/staticPrecompressed.js +152 -0
- package/dist/cjs/telemetry/exporters.js +4 -4
- package/dist/cjs/telemetry/healthSetup.js +14 -14
- package/dist/cjs/telemetry/lifecycle.js +20 -21
- package/dist/cjs/telemetry/plugin.js +10 -4
- package/dist/cjs/telemetry/runtimeEndpoints.js +3 -2
- package/dist/cjs/telemetryCore.js +5 -5
- package/dist/esm/backend-federation-security/index.mjs +73 -152
- package/dist/esm/bff-policy/crossProjectPolicy.mjs +249 -0
- package/dist/esm/bff-policy/index.mjs +2 -0
- package/dist/esm/bff-policy/operationIdentity.mjs +30 -0
- package/dist/esm/bff-policy/producer.mjs +31 -0
- package/dist/esm/contract-gate-snapshot-store/http-store.mjs +1 -8
- package/dist/esm/errorResponsePlugin.mjs +19 -0
- package/dist/esm/localisedLoader.mjs +24 -0
- package/dist/esm/localisedLoaderPlugin.mjs +24 -0
- package/dist/esm/mfCache.mjs +6 -0
- package/dist/esm/module-federation-css/plugin.mjs +6 -0
- package/dist/esm/module-federation-css/remote.mjs +2 -1
- package/dist/esm/serverConfig.mjs +0 -0
- package/dist/esm/serverPlugin.mjs +34 -0
- package/dist/esm/telemetry/exporters.mjs +2 -2
- package/dist/esm/telemetry/healthSetup.mjs +14 -14
- package/dist/esm/telemetry/lifecycle.mjs +21 -22
- package/dist/esm/telemetry/plugin.mjs +9 -3
- package/dist/esm/telemetry/runtimeEndpoints.mjs +3 -2
- package/dist/esm/telemetryCore.mjs +1 -1
- package/dist/esm-node/backend-federation-security/index.mjs +73 -152
- package/dist/esm-node/bff-policy/crossProjectPolicy.mjs +250 -0
- package/dist/esm-node/bff-policy/index.mjs +3 -0
- package/dist/esm-node/bff-policy/node.mjs +4 -0
- package/dist/esm-node/bff-policy/operationContracts.mjs +107 -0
- package/dist/esm-node/bff-policy/operationIdentity.mjs +31 -0
- package/dist/esm-node/bff-policy/producer.mjs +32 -0
- package/dist/esm-node/bff-policy/resolveCrossProjectPolicy.mjs +28 -0
- package/dist/esm-node/contract-gate-snapshot-store/http-store.mjs +1 -8
- package/dist/esm-node/errorResponsePlugin.mjs +20 -0
- package/dist/esm-node/localisedLoader.mjs +25 -0
- package/dist/esm-node/localisedLoaderPlugin.mjs +25 -0
- package/dist/esm-node/mfCache.mjs +6 -0
- package/dist/esm-node/module-federation-css/plugin.mjs +6 -0
- package/dist/esm-node/module-federation-css/remote.mjs +2 -1
- package/dist/esm-node/serverConfig.mjs +1 -0
- package/dist/esm-node/serverPlugin.mjs +35 -0
- package/dist/esm-node/static-serving/plugin.mjs +18 -0
- package/dist/esm-node/static-serving/responders.mjs +111 -0
- package/dist/esm-node/static-serving/staticModuleFederation.mjs +105 -0
- package/dist/esm-node/static-serving/staticPrecompressed.mjs +112 -0
- package/dist/esm-node/telemetry/exporters.mjs +2 -2
- package/dist/esm-node/telemetry/healthSetup.mjs +14 -14
- package/dist/esm-node/telemetry/lifecycle.mjs +21 -22
- package/dist/esm-node/telemetry/plugin.mjs +9 -3
- package/dist/esm-node/telemetry/runtimeEndpoints.mjs +3 -2
- package/dist/esm-node/telemetryCore.mjs +1 -1
- package/dist/types/backend-federation-security/node.d.ts +1 -1
- package/dist/types/bff-policy/crossProjectPolicy.d.ts +101 -0
- package/dist/types/bff-policy/index.d.ts +2 -0
- package/dist/types/bff-policy/node.d.ts +3 -0
- package/dist/types/bff-policy/operationContracts.d.ts +46 -0
- package/dist/types/bff-policy/operationIdentity.d.ts +34 -0
- package/dist/types/bff-policy/producer.d.ts +10 -0
- package/dist/types/bff-policy/resolveCrossProjectPolicy.d.ts +39 -0
- package/dist/types/errorResponsePlugin.d.ts +3 -0
- package/dist/types/index.d.ts +1 -0
- package/dist/types/localisedLoader.d.ts +15 -0
- package/dist/types/localisedLoaderPlugin.d.ts +2 -0
- package/dist/types/mfCache.d.ts +2 -3
- package/dist/types/module-federation-css/plugin.d.ts +2 -3
- package/dist/types/module-federation-css/remote.d.ts +2 -0
- package/dist/types/serverConfig.d.ts +17 -0
- package/dist/types/serverPlugin.d.ts +5 -0
- package/dist/types/static-serving/plugin.d.ts +3 -0
- package/dist/types/static-serving/responders.d.ts +2 -0
- package/dist/types/static-serving/staticModuleFederation.d.ts +13 -0
- package/dist/types/static-serving/staticPrecompressed.d.ts +13 -0
- package/dist/types/telemetry/exporters.d.ts +1 -1
- package/dist/types/telemetry/healthSetup.d.ts +3 -3
- package/dist/types/telemetry/lifecycle.d.ts +4 -4
- package/dist/types/telemetry/plugin.d.ts +2 -1
- package/dist/types/telemetryCore.d.ts +1 -1
- package/dist/types-esm/backend-federation-security/index.d.mts +68 -0
- package/dist/types-esm/backend-federation-security/node.d.mts +3 -0
- package/dist/types-esm/bff-policy/crossProjectPolicy.d.mts +101 -0
- package/dist/types-esm/bff-policy/index.d.mts +2 -0
- package/dist/types-esm/bff-policy/node.d.mts +3 -0
- package/dist/types-esm/bff-policy/operationContracts.d.mts +46 -0
- package/dist/types-esm/bff-policy/operationIdentity.d.mts +34 -0
- package/dist/types-esm/bff-policy/producer.d.mts +10 -0
- package/dist/types-esm/bff-policy/resolveCrossProjectPolicy.d.mts +39 -0
- package/dist/types-esm/contract-gate-snapshot-normalization.d.mts +15 -0
- package/dist/types-esm/contract-gate-snapshot-store/file-store.d.mts +3 -0
- package/dist/types-esm/contract-gate-snapshot-store/http-store.d.mts +5 -0
- package/dist/types-esm/contract-gate-snapshot-store/index.d.mts +5 -0
- package/dist/types-esm/contract-gate-snapshot-store/resolve.d.mts +9 -0
- package/dist/types-esm/contract-gate-snapshot-store/types.d.mts +47 -0
- package/dist/types-esm/contractGateSnapshotObserver.d.mts +33 -0
- package/dist/types-esm/env.d.mts +40 -0
- package/dist/types-esm/errorResponsePlugin.d.mts +3 -0
- package/dist/types-esm/index.d.mts +8 -0
- package/dist/types-esm/localisedLoader.d.mts +15 -0
- package/dist/types-esm/localisedLoaderPlugin.d.mts +2 -0
- package/dist/types-esm/mfCache.d.mts +26 -0
- package/dist/types-esm/module-federation-css/collector.d.mts +25 -0
- package/dist/types-esm/module-federation-css/index.d.mts +8 -0
- package/dist/types-esm/module-federation-css/manifest.d.mts +28 -0
- package/dist/types-esm/module-federation-css/plugin.d.mts +22 -0
- package/dist/types-esm/module-federation-css/remote.d.mts +22 -0
- package/dist/types-esm/runtime-fallback-signal/index.d.mts +4 -0
- package/dist/types-esm/runtime-fallback-signal/model.d.mts +70 -0
- package/dist/types-esm/runtime-fallback-signal/resolver.d.mts +29 -0
- package/dist/types-esm/runtime-fallback-signal/store.d.mts +4 -0
- package/dist/types-esm/runtimeFallbackSignal.d.mts +1 -0
- package/dist/types-esm/runtimeLifecycle.d.mts +10 -0
- package/dist/types-esm/serverConfig.d.mts +17 -0
- package/dist/types-esm/serverPlugin.d.mts +5 -0
- package/dist/types-esm/static-serving/plugin.d.mts +3 -0
- package/dist/types-esm/static-serving/responders.d.mts +2 -0
- package/dist/types-esm/static-serving/staticModuleFederation.d.mts +13 -0
- package/dist/types-esm/static-serving/staticPrecompressed.d.mts +13 -0
- package/dist/types-esm/telemetry/envelope.d.mts +55 -0
- package/dist/types-esm/telemetry/exporters.d.mts +12 -0
- package/dist/types-esm/telemetry/healthEvaluation.d.mts +20 -0
- package/dist/types-esm/telemetry/healthMonitor.d.mts +88 -0
- package/dist/types-esm/telemetry/healthSetup.d.mts +17 -0
- package/dist/types-esm/telemetry/lifecycle.d.mts +18 -0
- package/dist/types-esm/telemetry/metrics.d.mts +3 -0
- package/dist/types-esm/telemetry/plugin.d.mts +13 -0
- package/dist/types-esm/telemetry/registry.d.mts +69 -0
- package/dist/types-esm/telemetry/registryTypes.d.mts +50 -0
- package/dist/types-esm/telemetry/runtimeEndpoints.d.mts +35 -0
- package/dist/types-esm/telemetry.d.mts +1 -0
- package/dist/types-esm/telemetryCore.d.mts +6 -0
- package/package.json +183 -29
- package/rslib.config.mts +36 -2
- package/rstest.config.mts +11 -0
- package/src/backend-federation-security/index.ts +151 -318
- package/src/backend-federation-security/node.ts +24 -22
- package/src/bff-policy/crossProjectPolicy.ts +754 -0
- package/src/bff-policy/index.ts +2 -0
- package/src/bff-policy/node.ts +3 -0
- package/src/bff-policy/operationContracts.ts +225 -0
- package/src/bff-policy/operationIdentity.ts +90 -0
- package/src/bff-policy/producer.ts +40 -0
- package/src/bff-policy/resolveCrossProjectPolicy.ts +88 -0
- package/src/contract-gate-snapshot-store/http-store.ts +1 -11
- package/src/contract-gate-snapshot-store/resolve.ts +0 -1
- package/src/errorResponsePlugin.ts +21 -0
- package/src/index.ts +4 -0
- package/src/localisedLoader.ts +50 -0
- package/src/localisedLoaderPlugin.ts +33 -0
- package/src/mfCache.ts +4 -3
- package/src/module-federation-css/plugin.ts +4 -3
- package/src/module-federation-css/remote.ts +3 -1
- package/src/serverConfig.ts +32 -0
- package/src/serverPlugin.ts +38 -0
- package/src/static-serving/plugin.ts +16 -0
- package/src/static-serving/responders.ts +208 -0
- package/src/static-serving/staticModuleFederation.ts +255 -0
- package/src/static-serving/staticPrecompressed.ts +193 -0
- package/src/telemetry/exporters.ts +1 -1
- package/src/telemetry/healthSetup.ts +17 -20
- package/src/telemetry/lifecycle.ts +27 -39
- package/src/telemetry/plugin.ts +6 -4
- package/src/telemetry/runtimeEndpoints.ts +3 -2
- package/src/telemetryCore.ts +1 -1
- package/tests/backendFederationSecurity.test.ts +63 -0
- package/tests/bff-policy/crossProjectPolicy.matrix.test.ts +450 -0
- package/tests/bff-policy/crossProjectPolicy.test.ts +197 -0
- package/tests/bff-policy/operationContracts.test.ts +278 -0
- package/tests/bff-policy/optionalZodPeer.test.ts +30 -0
- package/tests/bff-policy/resolveCrossProjectPolicy.test.ts +76 -0
- package/tests/bff-policy/zodEvaluatorProbe.test.ts +49 -0
- package/tests/contractGateSnapshotObserver.test.ts +0 -58
- package/tests/contractGateSnapshotStore.test.ts +0 -51
- package/tests/env.test.ts +45 -64
- package/tests/localisedLoader.test.ts +155 -0
- package/tests/mfCache.test.ts +1 -88
- package/tests/moduleFederationCss.test.ts +98 -41
- package/tests/nativeErrorResponse.test.ts +118 -0
- package/tests/nativeServerAssembly.test.ts +385 -0
- package/tests/runtimeLifecycle.test.ts +69 -17
- package/tests/static-serving/ordering.test.ts +214 -0
- package/tests/static-serving/static.test.ts +411 -0
- package/tests/telemetry.test.ts +35 -198
- package/tests/telemetryHealthMonitor.test.ts +0 -4
- package/tests/telemetryHealthObservation.test.ts +302 -0
- package/tests/telemetryLifecycle.test.ts +7 -303
- package/tests/telemetryRegistryQueueMatrix.test.ts +14 -111
- package/tests/registration.test.ts +0 -112
- package/tests/telemetryAutopilot.test.ts +0 -1134
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import type {} from '@modern-js/plugin/server';
|
|
2
|
+
import type {
|
|
3
|
+
BffRuntimeUserConfig,
|
|
4
|
+
BffRuntimeFramework as CanonicalBffRuntimeFramework,
|
|
5
|
+
ServerTelemetryConfigExtension,
|
|
6
|
+
} from '@modern-js/runtime-extensions/server-config';
|
|
7
|
+
import type { BffUserConfig, ServerUserConfig } from '@modern-js/server-core';
|
|
8
|
+
|
|
9
|
+
type BffConfigExtension = Omit<BffRuntimeUserConfig, 'runtimeFramework'>;
|
|
10
|
+
|
|
11
|
+
type ForkBffRuntimeRegistry = Record<
|
|
12
|
+
Exclude<CanonicalBffRuntimeFramework, 'hono'>,
|
|
13
|
+
true
|
|
14
|
+
>;
|
|
15
|
+
|
|
16
|
+
declare module '@modern-js/plugin/server' {
|
|
17
|
+
interface BffRuntimeRegistry extends ForkBffRuntimeRegistry {}
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
declare module '@modern-js/server-core' {
|
|
21
|
+
interface BffUserConfig extends BffConfigExtension {}
|
|
22
|
+
interface ServerUserConfig extends ServerTelemetryConfigExtension {}
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
export type UltramodernBffUserConfig = Omit<
|
|
26
|
+
BffUserConfig,
|
|
27
|
+
keyof BffRuntimeUserConfig
|
|
28
|
+
> &
|
|
29
|
+
BffRuntimeUserConfig;
|
|
30
|
+
|
|
31
|
+
export type UltramodernServerUserConfig = ServerUserConfig &
|
|
32
|
+
ServerTelemetryConfigExtension;
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
import './serverConfig';
|
|
2
|
+
import type { ServerPlugin } from '@modern-js/server-core';
|
|
3
|
+
import { injectErrorResponsePlugin } from './errorResponsePlugin';
|
|
4
|
+
import { injectLocalisedLoaderPlugin } from './localisedLoaderPlugin';
|
|
5
|
+
import { injectMfAssetCacheHeadersPlugin } from './mfCache';
|
|
6
|
+
import { injectModuleFederationCssPlugin } from './module-federation-css/plugin';
|
|
7
|
+
import { disposeServerRuntime } from './runtimeLifecycle';
|
|
8
|
+
import { injectStaticServingPlugin } from './static-serving/plugin';
|
|
9
|
+
import { injectTelemetryPlugin } from './telemetry/plugin';
|
|
10
|
+
|
|
11
|
+
/** Compose fork server policies through the native ServerPlugin extension. */
|
|
12
|
+
export const ultramodernServerPlugin = (): ServerPlugin => ({
|
|
13
|
+
name: '@modern-js/ultramodern-server',
|
|
14
|
+
usePlugins: [
|
|
15
|
+
injectLocalisedLoaderPlugin(),
|
|
16
|
+
injectErrorResponsePlugin(),
|
|
17
|
+
injectTelemetryPlugin(),
|
|
18
|
+
injectModuleFederationCssPlugin(),
|
|
19
|
+
injectMfAssetCacheHeadersPlugin(),
|
|
20
|
+
injectStaticServingPlugin(),
|
|
21
|
+
{
|
|
22
|
+
name: '@modern-js/server-runtime-lifecycle',
|
|
23
|
+
// Native disposal reverses registration: Effect resources close before
|
|
24
|
+
// the telemetry lane flushes their final events.
|
|
25
|
+
pre: ['@modern-js/inject-telemetry'],
|
|
26
|
+
setup(api) {
|
|
27
|
+
api.onDispose(() => {
|
|
28
|
+
const { serverBase } = api.getServerContext();
|
|
29
|
+
if (serverBase) {
|
|
30
|
+
return disposeServerRuntime(serverBase);
|
|
31
|
+
}
|
|
32
|
+
});
|
|
33
|
+
},
|
|
34
|
+
},
|
|
35
|
+
],
|
|
36
|
+
});
|
|
37
|
+
|
|
38
|
+
export default ultramodernServerPlugin;
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import type { ServerPlugin } from '@modern-js/server-core';
|
|
2
|
+
import { createStaticExtensionResponders } from './responders';
|
|
3
|
+
|
|
4
|
+
export function injectStaticServingPlugin(): ServerPlugin {
|
|
5
|
+
return {
|
|
6
|
+
name: '@modern-js/server-static-extensions',
|
|
7
|
+
post: ['@modern-js/plugin-server-static'],
|
|
8
|
+
setup(api) {
|
|
9
|
+
api.updateServerContext({
|
|
10
|
+
staticAssetResponders: createStaticExtensionResponders(),
|
|
11
|
+
});
|
|
12
|
+
},
|
|
13
|
+
};
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
export default injectStaticServingPlugin;
|
|
@@ -0,0 +1,208 @@
|
|
|
1
|
+
import path from 'node:path';
|
|
2
|
+
import type { Middleware } from '@modern-js/server-core';
|
|
3
|
+
import {
|
|
4
|
+
type ServerStaticPluginOptions,
|
|
5
|
+
type ServeStaticAsset,
|
|
6
|
+
type StaticAsset,
|
|
7
|
+
serveStaticAsset,
|
|
8
|
+
} from '@modern-js/server-core/node';
|
|
9
|
+
import {
|
|
10
|
+
applyModuleFederationAssetHeaders,
|
|
11
|
+
getModuleFederationAssetList,
|
|
12
|
+
getModuleFederationRequestPath,
|
|
13
|
+
isBackendModuleFederationManifestRequest,
|
|
14
|
+
isModuleFederationManifestRequest,
|
|
15
|
+
type ModuleFederationServeAssets,
|
|
16
|
+
patchModuleFederationManifestPublicPath,
|
|
17
|
+
patchModuleFederationRemoteEntryPublicPath,
|
|
18
|
+
} from './staticModuleFederation';
|
|
19
|
+
import {
|
|
20
|
+
applyPreCompressedAssetHeaders,
|
|
21
|
+
resolvePreCompressedAsset,
|
|
22
|
+
} from './staticPrecompressed';
|
|
23
|
+
|
|
24
|
+
type Context = Parameters<Middleware>[0];
|
|
25
|
+
|
|
26
|
+
async function respondPrecompressedAsset(
|
|
27
|
+
context: Context,
|
|
28
|
+
asset: StaticAsset,
|
|
29
|
+
serve: ServeStaticAsset,
|
|
30
|
+
) {
|
|
31
|
+
const selected = await resolvePreCompressedAsset(context, asset.filename);
|
|
32
|
+
const response = selected.acceptable
|
|
33
|
+
? await serve({
|
|
34
|
+
filename: selected.selected?.filepath,
|
|
35
|
+
contentLength: true,
|
|
36
|
+
})
|
|
37
|
+
: new Response(null, { status: 406 });
|
|
38
|
+
if (response === null) {
|
|
39
|
+
return null;
|
|
40
|
+
}
|
|
41
|
+
context.res = response;
|
|
42
|
+
applyPreCompressedAssetHeaders(context, selected);
|
|
43
|
+
if (path.extname(asset.filename).toLowerCase() === '.cjs') {
|
|
44
|
+
context.res.headers.set('Content-Type', 'text/javascript; charset=UTF-8');
|
|
45
|
+
}
|
|
46
|
+
return context.res;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
// Native core owns readers, containment and response conversion.
|
|
50
|
+
// This extension owns federation and compressed/generated asset selection.
|
|
51
|
+
export function createStaticExtensionResponders(): ServerStaticPluginOptions {
|
|
52
|
+
const caches = new Map<
|
|
53
|
+
string,
|
|
54
|
+
{
|
|
55
|
+
assets: ModuleFederationServeAssets | null;
|
|
56
|
+
expires: number;
|
|
57
|
+
pending: Promise<ModuleFederationServeAssets> | null;
|
|
58
|
+
}
|
|
59
|
+
>();
|
|
60
|
+
const getAssets = (root: string) => {
|
|
61
|
+
let cache = caches.get(root);
|
|
62
|
+
if (!cache) {
|
|
63
|
+
cache = { assets: null, expires: 0, pending: null };
|
|
64
|
+
caches.set(root, cache);
|
|
65
|
+
}
|
|
66
|
+
if (cache.assets && Date.now() < cache.expires) {
|
|
67
|
+
return Promise.resolve(cache.assets);
|
|
68
|
+
}
|
|
69
|
+
if (cache.pending) {
|
|
70
|
+
return cache.pending;
|
|
71
|
+
}
|
|
72
|
+
const current = cache;
|
|
73
|
+
const pending = getModuleFederationAssetList(root)
|
|
74
|
+
.then(assets => {
|
|
75
|
+
current.assets = assets;
|
|
76
|
+
current.expires = Date.now() + 1_000;
|
|
77
|
+
return assets;
|
|
78
|
+
})
|
|
79
|
+
.finally(() => {
|
|
80
|
+
current.pending = null;
|
|
81
|
+
});
|
|
82
|
+
current.pending = pending;
|
|
83
|
+
return pending;
|
|
84
|
+
};
|
|
85
|
+
|
|
86
|
+
async function serveFederated(
|
|
87
|
+
context: Context,
|
|
88
|
+
asset: StaticAsset,
|
|
89
|
+
serve: ServeStaticAsset,
|
|
90
|
+
requestPath: string,
|
|
91
|
+
pathPrefix: string,
|
|
92
|
+
remoteEntry: boolean,
|
|
93
|
+
) {
|
|
94
|
+
applyModuleFederationAssetHeaders(context);
|
|
95
|
+
const patchManifest =
|
|
96
|
+
isModuleFederationManifestRequest(requestPath) &&
|
|
97
|
+
!isBackendModuleFederationManifestRequest(requestPath);
|
|
98
|
+
if (!patchManifest && !remoteEntry) {
|
|
99
|
+
return respondPrecompressedAsset(context, asset, serve);
|
|
100
|
+
}
|
|
101
|
+
const response = await serve({ contentLength: true });
|
|
102
|
+
if (!response) {
|
|
103
|
+
return null;
|
|
104
|
+
}
|
|
105
|
+
const bytes = Buffer.from(await response.arrayBuffer());
|
|
106
|
+
const patched = patchManifest
|
|
107
|
+
? patchModuleFederationManifestPublicPath(context, bytes, pathPrefix)
|
|
108
|
+
: patchModuleFederationRemoteEntryPublicPath(context, bytes, pathPrefix);
|
|
109
|
+
response.headers.set('Content-Length', String(patched.byteLength));
|
|
110
|
+
if (path.extname(asset.filename).toLowerCase() === '.cjs') {
|
|
111
|
+
response.headers.set('Content-Type', 'text/javascript; charset=UTF-8');
|
|
112
|
+
}
|
|
113
|
+
return new Response(Uint8Array.from(patched), {
|
|
114
|
+
status: response.status,
|
|
115
|
+
headers: response.headers,
|
|
116
|
+
});
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
async function serveGenerated(context: Context, root: string) {
|
|
120
|
+
if (!['GET', 'HEAD'].includes(context.req.raw.method.toUpperCase())) {
|
|
121
|
+
return null;
|
|
122
|
+
}
|
|
123
|
+
let pathname: string;
|
|
124
|
+
try {
|
|
125
|
+
pathname = decodeURIComponent(context.req.path).replace(/\\/gu, '/');
|
|
126
|
+
} catch {
|
|
127
|
+
return null;
|
|
128
|
+
}
|
|
129
|
+
if (pathname.includes('\0') || pathname.split('/').includes('..')) {
|
|
130
|
+
return null;
|
|
131
|
+
}
|
|
132
|
+
const publicRoot = path.join(root, 'public');
|
|
133
|
+
const response = await serveStaticAsset(
|
|
134
|
+
context,
|
|
135
|
+
{
|
|
136
|
+
filename: path.resolve(publicRoot, pathname.replace(/^\/+/, '')),
|
|
137
|
+
kind: 'static',
|
|
138
|
+
root: publicRoot,
|
|
139
|
+
realpath: true,
|
|
140
|
+
},
|
|
141
|
+
(asset, serve) => respondPrecompressedAsset(context, asset, serve),
|
|
142
|
+
);
|
|
143
|
+
if (response && context.req.raw.method.toUpperCase() === 'HEAD') {
|
|
144
|
+
return new Response(null, {
|
|
145
|
+
status: response.status,
|
|
146
|
+
headers: response.headers,
|
|
147
|
+
});
|
|
148
|
+
}
|
|
149
|
+
return response;
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
return {
|
|
153
|
+
async respondAsset(context, asset, serve, { root, pathPrefix }) {
|
|
154
|
+
const requestPath = getModuleFederationRequestPath(
|
|
155
|
+
context.req.path,
|
|
156
|
+
pathPrefix,
|
|
157
|
+
);
|
|
158
|
+
if (requestPath.includes('..')) {
|
|
159
|
+
return null;
|
|
160
|
+
}
|
|
161
|
+
const assets = await getAssets(root);
|
|
162
|
+
if (asset.kind === 'static' && assets.assets.has(requestPath)) {
|
|
163
|
+
return serveFederated(
|
|
164
|
+
context,
|
|
165
|
+
asset,
|
|
166
|
+
serve,
|
|
167
|
+
requestPath,
|
|
168
|
+
pathPrefix,
|
|
169
|
+
assets.remoteEntries.has(requestPath),
|
|
170
|
+
);
|
|
171
|
+
}
|
|
172
|
+
return respondPrecompressedAsset(context, asset, serve);
|
|
173
|
+
},
|
|
174
|
+
async respondPublicFallback(context, respondPublic, { root, pathPrefix }) {
|
|
175
|
+
const requestPath = getModuleFederationRequestPath(
|
|
176
|
+
context.req.path,
|
|
177
|
+
pathPrefix,
|
|
178
|
+
);
|
|
179
|
+
if (requestPath.includes('..')) {
|
|
180
|
+
return null;
|
|
181
|
+
}
|
|
182
|
+
const assets = await getAssets(root);
|
|
183
|
+
if (assets.assets.has(requestPath)) {
|
|
184
|
+
const response = await serveStaticAsset(
|
|
185
|
+
context,
|
|
186
|
+
{
|
|
187
|
+
filename: path.join(root, requestPath),
|
|
188
|
+
kind: 'static',
|
|
189
|
+
root,
|
|
190
|
+
},
|
|
191
|
+
(asset, serve) =>
|
|
192
|
+
serveFederated(
|
|
193
|
+
context,
|
|
194
|
+
asset,
|
|
195
|
+
serve,
|
|
196
|
+
requestPath,
|
|
197
|
+
pathPrefix,
|
|
198
|
+
assets.remoteEntries.has(requestPath),
|
|
199
|
+
),
|
|
200
|
+
);
|
|
201
|
+
if (response !== null) {
|
|
202
|
+
return response;
|
|
203
|
+
}
|
|
204
|
+
}
|
|
205
|
+
return (await respondPublic()) ?? serveGenerated(context, root);
|
|
206
|
+
},
|
|
207
|
+
};
|
|
208
|
+
}
|
|
@@ -0,0 +1,255 @@
|
|
|
1
|
+
import { fileReader } from '@modern-js/runtime-utils/fileReader';
|
|
2
|
+
import type { Middleware } from '@modern-js/server-core';
|
|
3
|
+
import { fs } from '@modern-js/utils';
|
|
4
|
+
import path from 'path';
|
|
5
|
+
|
|
6
|
+
export const MODULE_FEDERATION_MANIFEST_FILE = 'mf-manifest.json';
|
|
7
|
+
const BACKEND_MODULE_FEDERATION_MANIFEST_FILE = 'backend-mf-manifest.json';
|
|
8
|
+
|
|
9
|
+
const MODULE_FEDERATION_MANIFEST_FILES = [
|
|
10
|
+
MODULE_FEDERATION_MANIFEST_FILE,
|
|
11
|
+
BACKEND_MODULE_FEDERATION_MANIFEST_FILE,
|
|
12
|
+
];
|
|
13
|
+
const MODULE_FEDERATION_OPTIONAL_FILES = ['mf-stats.json'];
|
|
14
|
+
|
|
15
|
+
type ModuleFederationManifest = {
|
|
16
|
+
metaData?: {
|
|
17
|
+
remoteEntry?: {
|
|
18
|
+
path?: string;
|
|
19
|
+
name?: string;
|
|
20
|
+
};
|
|
21
|
+
publicPath?: string;
|
|
22
|
+
types?: {
|
|
23
|
+
path?: string;
|
|
24
|
+
zip?: string;
|
|
25
|
+
api?: string;
|
|
26
|
+
};
|
|
27
|
+
};
|
|
28
|
+
shared?: Array<{
|
|
29
|
+
assets?: ModuleFederationAssets;
|
|
30
|
+
}>;
|
|
31
|
+
remotes?: Array<{
|
|
32
|
+
assets?: ModuleFederationAssets;
|
|
33
|
+
}>;
|
|
34
|
+
exposes?: Array<{
|
|
35
|
+
assets?: ModuleFederationAssets;
|
|
36
|
+
}>;
|
|
37
|
+
};
|
|
38
|
+
|
|
39
|
+
type ModuleFederationAssets = {
|
|
40
|
+
js?: {
|
|
41
|
+
sync?: string[];
|
|
42
|
+
async?: string[];
|
|
43
|
+
};
|
|
44
|
+
css?: {
|
|
45
|
+
sync?: string[];
|
|
46
|
+
async?: string[];
|
|
47
|
+
};
|
|
48
|
+
};
|
|
49
|
+
|
|
50
|
+
export type ModuleFederationServeAssets = {
|
|
51
|
+
assets: Set<string>;
|
|
52
|
+
remoteEntries: Set<string>;
|
|
53
|
+
};
|
|
54
|
+
|
|
55
|
+
const trimLeadingSlash = (value: string) => value.replace(/^\/+/, '');
|
|
56
|
+
|
|
57
|
+
export const getModuleFederationRequestPath = (
|
|
58
|
+
pathname: string,
|
|
59
|
+
pathPrefix: string,
|
|
60
|
+
) => {
|
|
61
|
+
const normalizedPrefix = `/${trimLeadingSlash(pathPrefix)}`.replace(
|
|
62
|
+
/\/+$/u,
|
|
63
|
+
'',
|
|
64
|
+
);
|
|
65
|
+
const requestPath =
|
|
66
|
+
normalizedPrefix &&
|
|
67
|
+
(pathname === normalizedPrefix ||
|
|
68
|
+
pathname.startsWith(`${normalizedPrefix}/`))
|
|
69
|
+
? pathname.slice(normalizedPrefix.length)
|
|
70
|
+
: pathname;
|
|
71
|
+
|
|
72
|
+
return trimLeadingSlash(requestPath);
|
|
73
|
+
};
|
|
74
|
+
|
|
75
|
+
export const isModuleFederationManifestRequest = (requestPath: string) =>
|
|
76
|
+
MODULE_FEDERATION_MANIFEST_FILES.includes(requestPath);
|
|
77
|
+
|
|
78
|
+
export const isBackendModuleFederationManifestRequest = (requestPath: string) =>
|
|
79
|
+
requestPath === BACKEND_MODULE_FEDERATION_MANIFEST_FILE;
|
|
80
|
+
|
|
81
|
+
export const applyModuleFederationAssetHeaders = (
|
|
82
|
+
c: Parameters<Middleware>[0],
|
|
83
|
+
) => {
|
|
84
|
+
c.header('Access-Control-Allow-Origin', '*');
|
|
85
|
+
c.header('Access-Control-Allow-Headers', '*');
|
|
86
|
+
c.header('Access-Control-Allow-Methods', 'GET,HEAD,OPTIONS');
|
|
87
|
+
};
|
|
88
|
+
|
|
89
|
+
const joinModuleFederationAssetPath = (
|
|
90
|
+
assetPath?: string,
|
|
91
|
+
assetName?: string,
|
|
92
|
+
) => {
|
|
93
|
+
if (!assetName) {
|
|
94
|
+
return '';
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
return trimLeadingSlash(path.posix.join(assetPath || '', assetName));
|
|
98
|
+
};
|
|
99
|
+
|
|
100
|
+
const appendModuleFederationAsset = (set: Set<string>, assetPath?: string) => {
|
|
101
|
+
if (assetPath) {
|
|
102
|
+
set.add(trimLeadingSlash(assetPath));
|
|
103
|
+
}
|
|
104
|
+
};
|
|
105
|
+
|
|
106
|
+
const appendModuleFederationAssets = (
|
|
107
|
+
set: Set<string>,
|
|
108
|
+
assets?: ModuleFederationAssets,
|
|
109
|
+
) => {
|
|
110
|
+
assets?.js?.sync?.forEach(asset => appendModuleFederationAsset(set, asset));
|
|
111
|
+
assets?.js?.async?.forEach(asset => appendModuleFederationAsset(set, asset));
|
|
112
|
+
assets?.css?.sync?.forEach(asset => appendModuleFederationAsset(set, asset));
|
|
113
|
+
assets?.css?.async?.forEach(asset => appendModuleFederationAsset(set, asset));
|
|
114
|
+
};
|
|
115
|
+
|
|
116
|
+
const hasAbsoluteProtocol = (value: string) =>
|
|
117
|
+
/^https?:\/\//i.test(value) || value.startsWith('//');
|
|
118
|
+
|
|
119
|
+
const ensureLeadingSlash = (value: string) => {
|
|
120
|
+
if (value === '') {
|
|
121
|
+
return '/';
|
|
122
|
+
}
|
|
123
|
+
return value.startsWith('/') ? value : `/${value}`;
|
|
124
|
+
};
|
|
125
|
+
|
|
126
|
+
const ensureTrailingSlash = (value: string) =>
|
|
127
|
+
value.endsWith('/') ? value : `${value}/`;
|
|
128
|
+
|
|
129
|
+
export const patchModuleFederationManifestPublicPath = (
|
|
130
|
+
c: Parameters<Middleware>[0],
|
|
131
|
+
manifestBuffer: Buffer,
|
|
132
|
+
pathPrefix: string,
|
|
133
|
+
) => {
|
|
134
|
+
try {
|
|
135
|
+
const manifest = JSON.parse(
|
|
136
|
+
manifestBuffer.toString('utf-8'),
|
|
137
|
+
) as ModuleFederationManifest;
|
|
138
|
+
const publicPath = manifest.metaData?.publicPath;
|
|
139
|
+
|
|
140
|
+
if (!publicPath || hasAbsoluteProtocol(publicPath)) {
|
|
141
|
+
return manifestBuffer;
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
const requestURL = new URL(c.req.url);
|
|
145
|
+
const prefixPath = ensureTrailingSlash(
|
|
146
|
+
ensureLeadingSlash(pathPrefix || '/'),
|
|
147
|
+
);
|
|
148
|
+
manifest.metaData = {
|
|
149
|
+
...manifest.metaData,
|
|
150
|
+
publicPath: `${requestURL.origin}${prefixPath}`,
|
|
151
|
+
};
|
|
152
|
+
|
|
153
|
+
return Buffer.from(JSON.stringify(manifest), 'utf-8');
|
|
154
|
+
} catch {
|
|
155
|
+
return manifestBuffer;
|
|
156
|
+
}
|
|
157
|
+
};
|
|
158
|
+
|
|
159
|
+
export const patchModuleFederationRemoteEntryPublicPath = (
|
|
160
|
+
c: Parameters<Middleware>[0],
|
|
161
|
+
remoteEntryBuffer: Buffer,
|
|
162
|
+
pathPrefix: string,
|
|
163
|
+
) => {
|
|
164
|
+
const requestURL = new URL(c.req.url);
|
|
165
|
+
const prefixPath = ensureTrailingSlash(ensureLeadingSlash(pathPrefix || '/'));
|
|
166
|
+
const publicPath = `${requestURL.origin}${prefixPath}`;
|
|
167
|
+
const source = remoteEntryBuffer.toString('utf-8');
|
|
168
|
+
const patched = source
|
|
169
|
+
.replace(
|
|
170
|
+
/__webpack_require__\.p\s*=\s*(['"`])[^'"`]*\1;/,
|
|
171
|
+
`__webpack_require__.p = ${JSON.stringify(publicPath)};`,
|
|
172
|
+
)
|
|
173
|
+
.replace(
|
|
174
|
+
/__rspack_require__\.p\s*=\s*(['"`])[^'"`]*\1;/,
|
|
175
|
+
`__rspack_require__.p = ${JSON.stringify(publicPath)};`,
|
|
176
|
+
);
|
|
177
|
+
|
|
178
|
+
if (patched === source) {
|
|
179
|
+
return remoteEntryBuffer;
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
return Buffer.from(patched, 'utf-8');
|
|
183
|
+
};
|
|
184
|
+
|
|
185
|
+
export const getModuleFederationAssetList = async (
|
|
186
|
+
pwd: string,
|
|
187
|
+
): Promise<ModuleFederationServeAssets> => {
|
|
188
|
+
const assets = new Set<string>();
|
|
189
|
+
const remoteEntries = new Set<string>();
|
|
190
|
+
let manifestFound = false;
|
|
191
|
+
|
|
192
|
+
for (const manifestFile of MODULE_FEDERATION_MANIFEST_FILES) {
|
|
193
|
+
const manifestPath = path.join(pwd, manifestFile);
|
|
194
|
+
if (!(await fs.pathExists(manifestPath))) {
|
|
195
|
+
continue;
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
manifestFound = true;
|
|
199
|
+
assets.add(manifestFile);
|
|
200
|
+
const manifestBuffer = await fileReader.readFileFromSystem(
|
|
201
|
+
manifestPath,
|
|
202
|
+
'buffer',
|
|
203
|
+
);
|
|
204
|
+
if (manifestBuffer === null) {
|
|
205
|
+
continue;
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
try {
|
|
209
|
+
const manifest = JSON.parse(
|
|
210
|
+
manifestBuffer.toString('utf-8'),
|
|
211
|
+
) as ModuleFederationManifest;
|
|
212
|
+
const remoteEntry = joinModuleFederationAssetPath(
|
|
213
|
+
manifest.metaData?.remoteEntry?.path,
|
|
214
|
+
manifest.metaData?.remoteEntry?.name,
|
|
215
|
+
);
|
|
216
|
+
const dtsZip = joinModuleFederationAssetPath(
|
|
217
|
+
manifest.metaData?.types?.path,
|
|
218
|
+
manifest.metaData?.types?.zip,
|
|
219
|
+
);
|
|
220
|
+
const dtsApi = joinModuleFederationAssetPath(
|
|
221
|
+
manifest.metaData?.types?.path,
|
|
222
|
+
manifest.metaData?.types?.api,
|
|
223
|
+
);
|
|
224
|
+
|
|
225
|
+
if (remoteEntry) {
|
|
226
|
+
assets.add(remoteEntry);
|
|
227
|
+
remoteEntries.add(remoteEntry);
|
|
228
|
+
}
|
|
229
|
+
appendModuleFederationAsset(assets, dtsZip);
|
|
230
|
+
appendModuleFederationAsset(assets, dtsApi);
|
|
231
|
+
manifest.shared?.forEach(item =>
|
|
232
|
+
appendModuleFederationAssets(assets, item.assets),
|
|
233
|
+
);
|
|
234
|
+
manifest.remotes?.forEach(item =>
|
|
235
|
+
appendModuleFederationAssets(assets, item.assets),
|
|
236
|
+
);
|
|
237
|
+
manifest.exposes?.forEach(item =>
|
|
238
|
+
appendModuleFederationAssets(assets, item.assets),
|
|
239
|
+
);
|
|
240
|
+
} catch {}
|
|
241
|
+
}
|
|
242
|
+
|
|
243
|
+
if (manifestFound) {
|
|
244
|
+
for (const filename of MODULE_FEDERATION_OPTIONAL_FILES) {
|
|
245
|
+
if (await fs.pathExists(path.join(pwd, filename))) {
|
|
246
|
+
assets.add(filename);
|
|
247
|
+
}
|
|
248
|
+
}
|
|
249
|
+
}
|
|
250
|
+
|
|
251
|
+
return {
|
|
252
|
+
assets,
|
|
253
|
+
remoteEntries,
|
|
254
|
+
};
|
|
255
|
+
};
|