@bleedingdev/modern-js-server-runtime-extensions 3.11.3-ultramodern.1 → 3.11.3-ultramodern.11
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 +10 -0
- package/dist/cjs/backend-federation-security/index.js +7 -8
- package/dist/cjs/backend-federation-security/node.js +1 -1
- package/dist/cjs/bff-policy/operationIdentity.js +2 -3
- package/dist/cjs/env.js +5 -1
- package/dist/cjs/mfCache.js +6 -7
- package/dist/cjs/module-federation-css/plugin.js +7 -3
- package/dist/cjs/render-resources/channels.js +55 -0
- package/dist/cjs/render-resources/index.js +47 -0
- package/dist/cjs/render-resources/plugin.js +82 -0
- package/dist/cjs/runtime-fallback-signal/model.js +9 -10
- package/dist/cjs/serverPlugin.js +2 -0
- package/dist/cjs/static-serving/immutableAssetStat.js +64 -0
- package/dist/cjs/static-serving/plugin.js +2 -1
- package/dist/cjs/static-serving/responders.js +13 -8
- package/dist/cjs/static-serving/staticPrecompressed.js +6 -3
- package/dist/cjs/telemetry/envelope.js +9 -9
- package/dist/cjs/telemetry/exporters.js +4 -4
- package/dist/cjs/telemetry/registryTypes.js +2 -2
- package/dist/esm/env.mjs +5 -1
- package/dist/esm/module-federation-css/plugin.mjs +7 -3
- package/dist/esm/serverPlugin.mjs +2 -0
- package/dist/esm-node/env.mjs +5 -1
- package/dist/esm-node/module-federation-css/plugin.mjs +7 -3
- package/dist/esm-node/render-resources/channels.mjs +5 -0
- package/dist/esm-node/render-resources/index.mjs +3 -0
- package/dist/esm-node/render-resources/plugin.mjs +43 -0
- package/dist/esm-node/serverPlugin.mjs +2 -0
- package/dist/esm-node/static-serving/immutableAssetStat.mjs +24 -0
- package/dist/esm-node/static-serving/plugin.mjs +2 -1
- package/dist/esm-node/static-serving/responders.mjs +13 -8
- package/dist/esm-node/static-serving/staticPrecompressed.mjs +6 -3
- package/dist/types/env.d.ts +15 -0
- package/dist/types/module-federation-css/plugin.d.ts +3 -2
- package/dist/types/render-resources/channels.d.ts +27 -0
- package/dist/types/render-resources/index.d.ts +2 -0
- package/dist/types/render-resources/plugin.d.ts +3 -0
- package/dist/types/static-serving/immutableAssetStat.d.ts +8 -0
- package/dist/types/static-serving/responders.d.ts +7 -1
- package/dist/types/static-serving/staticPrecompressed.d.ts +1 -1
- package/dist/types-esm/env.d.mts +15 -0
- package/dist/types-esm/module-federation-css/plugin.d.mts +3 -2
- package/dist/types-esm/render-resources/channels.d.mts +27 -0
- package/dist/types-esm/render-resources/index.d.mts +2 -0
- package/dist/types-esm/render-resources/plugin.d.mts +3 -0
- package/dist/types-esm/static-serving/immutableAssetStat.d.mts +8 -0
- package/dist/types-esm/static-serving/responders.d.mts +7 -1
- package/dist/types-esm/static-serving/staticPrecompressed.d.mts +1 -1
- package/package.json +31 -14
- package/src/env.ts +25 -0
- package/src/module-federation-css/plugin.ts +21 -5
- package/src/render-resources/channels.ts +37 -0
- package/src/render-resources/index.ts +7 -0
- package/src/render-resources/plugin.ts +56 -0
- package/src/serverPlugin.ts +2 -0
- package/src/static-serving/immutableAssetStat.ts +41 -0
- package/src/static-serving/plugin.ts +4 -1
- package/src/static-serving/responders.ts +30 -6
- package/src/static-serving/staticPrecompressed.ts +5 -4
|
@@ -29,8 +29,9 @@ export type ModuleFederationCssPluginOptions = {
|
|
|
29
29
|
* being pinned at boot: remote manifests change on every remote redeploy, and
|
|
30
30
|
* fetch failures must not pin an empty/partial list for the process lifetime.
|
|
31
31
|
*
|
|
32
|
-
* Existing plugin ordering places this after the native resource plugin
|
|
33
|
-
*
|
|
32
|
+
* Existing plugin ordering places this after the native resource plugin.
|
|
33
|
+
* Lazy loading keeps enrichment in the page-only resource prefix, before
|
|
34
|
+
* configured render middleware.
|
|
34
35
|
*/
|
|
35
36
|
export const injectModuleFederationCssPlugin = (
|
|
36
37
|
options: ModuleFederationCssPluginOptions = {},
|
|
@@ -41,7 +42,12 @@ export const injectModuleFederationCssPlugin = (
|
|
|
41
42
|
|
|
42
43
|
setup(api) {
|
|
43
44
|
api.onPrepare(() => {
|
|
44
|
-
const {
|
|
45
|
+
const {
|
|
46
|
+
middlewares,
|
|
47
|
+
renderMiddlewares,
|
|
48
|
+
renderResources,
|
|
49
|
+
distDirectory: pwd,
|
|
50
|
+
} = api.getServerContext();
|
|
45
51
|
|
|
46
52
|
if (!pwd) {
|
|
47
53
|
return;
|
|
@@ -78,10 +84,20 @@ export const injectModuleFederationCssPlugin = (
|
|
|
78
84
|
await next();
|
|
79
85
|
};
|
|
80
86
|
|
|
81
|
-
|
|
87
|
+
const middleware = {
|
|
82
88
|
name: 'inject-module-federation-css',
|
|
83
89
|
handler,
|
|
84
|
-
}
|
|
90
|
+
};
|
|
91
|
+
if (isProd() && renderResources?.loading === 'lazy') {
|
|
92
|
+
// Native resource injection runs first and prepends this template
|
|
93
|
+
// handler. Enrich immediately after it, before application consumers.
|
|
94
|
+
const templates = renderMiddlewares.findIndex(
|
|
95
|
+
middleware => middleware.name === 'inject-html',
|
|
96
|
+
);
|
|
97
|
+
renderMiddlewares.splice(templates + 1, 0, middleware);
|
|
98
|
+
} else {
|
|
99
|
+
middlewares.push(middleware);
|
|
100
|
+
}
|
|
85
101
|
});
|
|
86
102
|
},
|
|
87
103
|
});
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
import diagnosticsChannel from 'node:diagnostics_channel';
|
|
2
|
+
import type { ServerContext } from '@modern-js/server-core';
|
|
3
|
+
|
|
4
|
+
/** One server bundle import, published after it settles. */
|
|
5
|
+
export interface RenderBundleLoadEvent {
|
|
6
|
+
/** The serving application's output directory, when configured. */
|
|
7
|
+
readonly distDirectory: ServerContext['distDirectory'];
|
|
8
|
+
readonly filepath: string;
|
|
9
|
+
readonly durationMs: number;
|
|
10
|
+
readonly outcome: 'loaded' | 'failed';
|
|
11
|
+
/** The request path that triggered the import, or `<startup>` for warmup. */
|
|
12
|
+
readonly triggerPath: string;
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
/** One request through the server middlewares, traced from start to settle. */
|
|
16
|
+
export interface ServerRequestTrace {
|
|
17
|
+
/** The serving application's output directory, when configured. */
|
|
18
|
+
readonly distDirectory: ServerContext['distDirectory'];
|
|
19
|
+
/** The request path, without its query. */
|
|
20
|
+
readonly path: string;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* Process-wide, so every server in a process publishes here regardless of
|
|
25
|
+
* which module format loaded this package. Node reports a subscriber's
|
|
26
|
+
* exception as an uncaught exception.
|
|
27
|
+
*/
|
|
28
|
+
export const renderBundleLoadChannel =
|
|
29
|
+
diagnosticsChannel.channel<RenderBundleLoadEvent>(
|
|
30
|
+
'modern-js:server:render-bundle-load',
|
|
31
|
+
);
|
|
32
|
+
|
|
33
|
+
/** Publishes `start` when a request enters and `asyncEnd` when it settles. */
|
|
34
|
+
export const serverRequestChannel =
|
|
35
|
+
diagnosticsChannel.tracingChannel<ServerRequestTrace>(
|
|
36
|
+
'modern-js:server:request',
|
|
37
|
+
);
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
import { performance } from 'node:perf_hooks';
|
|
2
|
+
import type {
|
|
3
|
+
Middleware,
|
|
4
|
+
ServerEnv,
|
|
5
|
+
ServerPlugin,
|
|
6
|
+
} from '@modern-js/server-core';
|
|
7
|
+
import { parseServerRuntimeExtensionsEnv } from '../env';
|
|
8
|
+
import {
|
|
9
|
+
type RenderBundleLoadEvent,
|
|
10
|
+
renderBundleLoadChannel,
|
|
11
|
+
type ServerRequestTrace,
|
|
12
|
+
serverRequestChannel,
|
|
13
|
+
} from './channels';
|
|
14
|
+
|
|
15
|
+
export function injectRenderResourcesPlugin(): ServerPlugin {
|
|
16
|
+
return {
|
|
17
|
+
name: '@modern-js/server-render-resources',
|
|
18
|
+
setup(api) {
|
|
19
|
+
const distDirectory = api.getServerContext().distDirectory;
|
|
20
|
+
api.updateServerContext({
|
|
21
|
+
renderResources: {
|
|
22
|
+
loading: parseServerRuntimeExtensionsEnv().renderResources,
|
|
23
|
+
onBundleLoad({ filepath, requestPath }) {
|
|
24
|
+
if (!renderBundleLoadChannel.hasSubscribers) {
|
|
25
|
+
return undefined;
|
|
26
|
+
}
|
|
27
|
+
const started = performance.now();
|
|
28
|
+
return outcome => {
|
|
29
|
+
renderBundleLoadChannel.publish({
|
|
30
|
+
distDirectory,
|
|
31
|
+
filepath,
|
|
32
|
+
durationMs: performance.now() - started,
|
|
33
|
+
outcome,
|
|
34
|
+
triggerPath: requestPath ?? '<startup>',
|
|
35
|
+
} satisfies RenderBundleLoadEvent);
|
|
36
|
+
};
|
|
37
|
+
},
|
|
38
|
+
},
|
|
39
|
+
});
|
|
40
|
+
api.onPrepare(() => {
|
|
41
|
+
// Node returns next() directly while nothing subscribes.
|
|
42
|
+
const handler: Middleware<ServerEnv> = (c, next) =>
|
|
43
|
+
serverRequestChannel.tracePromise(next, {
|
|
44
|
+
distDirectory,
|
|
45
|
+
path: c.req.path,
|
|
46
|
+
} satisfies ServerRequestTrace);
|
|
47
|
+
api.getServerContext().middlewares.unshift({
|
|
48
|
+
name: 'trace-server-request',
|
|
49
|
+
handler,
|
|
50
|
+
});
|
|
51
|
+
});
|
|
52
|
+
},
|
|
53
|
+
};
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
export default injectRenderResourcesPlugin;
|
package/src/serverPlugin.ts
CHANGED
|
@@ -4,6 +4,7 @@ import { injectErrorResponsePlugin } from './errorResponsePlugin';
|
|
|
4
4
|
import { injectLocalisedLoaderPlugin } from './localisedLoaderPlugin';
|
|
5
5
|
import { injectMfAssetCacheHeadersPlugin } from './mfCache';
|
|
6
6
|
import { injectModuleFederationCssPlugin } from './module-federation-css/plugin';
|
|
7
|
+
import { injectRenderResourcesPlugin } from './render-resources/plugin';
|
|
7
8
|
import { disposeServerRuntime } from './runtimeLifecycle';
|
|
8
9
|
import { injectStaticServingPlugin } from './static-serving/plugin';
|
|
9
10
|
import { injectTelemetryPlugin } from './telemetry/plugin';
|
|
@@ -18,6 +19,7 @@ export const ultramodernServerPlugin = (): ServerPlugin => ({
|
|
|
18
19
|
injectModuleFederationCssPlugin(),
|
|
19
20
|
injectMfAssetCacheHeadersPlugin(),
|
|
20
21
|
injectStaticServingPlugin(),
|
|
22
|
+
injectRenderResourcesPlugin(),
|
|
21
23
|
{
|
|
22
24
|
name: '@modern-js/server-runtime-lifecycle',
|
|
23
25
|
// Native disposal reverses registration: Effect resources close before
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
import type { StaticAssetStat } from '@modern-js/server-core/node';
|
|
2
|
+
import { fs } from '@modern-js/utils';
|
|
3
|
+
|
|
4
|
+
export const IMMUTABLE_ASSET_STAT_LIMIT = 8192;
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* File metadata for immutable releases. Only found regular files are kept, so
|
|
8
|
+
* a missing path is looked up again by the next request. The oldest entry is
|
|
9
|
+
* dropped once the limit is reached.
|
|
10
|
+
*/
|
|
11
|
+
export function createImmutableAssetStat(): StaticAssetStat {
|
|
12
|
+
const stats = new Map<string, ReturnType<StaticAssetStat>>();
|
|
13
|
+
const forget = (filename: string, pending: ReturnType<StaticAssetStat>) => {
|
|
14
|
+
if (stats.get(filename) === pending) {
|
|
15
|
+
stats.delete(filename);
|
|
16
|
+
}
|
|
17
|
+
};
|
|
18
|
+
return filename => {
|
|
19
|
+
const cached = stats.get(filename);
|
|
20
|
+
if (cached) {
|
|
21
|
+
return cached;
|
|
22
|
+
}
|
|
23
|
+
const pending: ReturnType<StaticAssetStat> = fs.stat(filename).then(
|
|
24
|
+
stat => {
|
|
25
|
+
if (!stat.isFile()) {
|
|
26
|
+
forget(filename, pending);
|
|
27
|
+
}
|
|
28
|
+
return stat;
|
|
29
|
+
},
|
|
30
|
+
() => {
|
|
31
|
+
forget(filename, pending);
|
|
32
|
+
return null;
|
|
33
|
+
},
|
|
34
|
+
);
|
|
35
|
+
if (stats.size >= IMMUTABLE_ASSET_STAT_LIMIT) {
|
|
36
|
+
stats.delete(stats.keys().next().value!);
|
|
37
|
+
}
|
|
38
|
+
stats.set(filename, pending);
|
|
39
|
+
return pending;
|
|
40
|
+
};
|
|
41
|
+
}
|
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import type { ServerPlugin } from '@modern-js/server-core';
|
|
2
|
+
import { parseServerRuntimeExtensionsEnv } from '../env';
|
|
2
3
|
import { createStaticExtensionResponders } from './responders';
|
|
3
4
|
|
|
4
5
|
export function injectStaticServingPlugin(): ServerPlugin {
|
|
@@ -7,7 +8,9 @@ export function injectStaticServingPlugin(): ServerPlugin {
|
|
|
7
8
|
post: ['@modern-js/plugin-server-static'],
|
|
8
9
|
setup(api) {
|
|
9
10
|
api.updateServerContext({
|
|
10
|
-
staticAssetResponders: createStaticExtensionResponders(
|
|
11
|
+
staticAssetResponders: createStaticExtensionResponders(
|
|
12
|
+
parseServerRuntimeExtensionsEnv(),
|
|
13
|
+
),
|
|
11
14
|
});
|
|
12
15
|
},
|
|
13
16
|
};
|
|
@@ -6,6 +6,7 @@ import {
|
|
|
6
6
|
type StaticAsset,
|
|
7
7
|
serveStaticAsset,
|
|
8
8
|
} from '@modern-js/server-core/node';
|
|
9
|
+
import { createImmutableAssetStat } from './immutableAssetStat';
|
|
9
10
|
import {
|
|
10
11
|
applyModuleFederationAssetHeaders,
|
|
11
12
|
getModuleFederationAssetList,
|
|
@@ -23,12 +24,24 @@ import {
|
|
|
23
24
|
|
|
24
25
|
type Context = Parameters<Middleware>[0];
|
|
25
26
|
|
|
27
|
+
export type StaticExtensionPolicy = {
|
|
28
|
+
/** Release files never change while serving: keep found files and the federation inventory. */
|
|
29
|
+
immutableAssets: boolean;
|
|
30
|
+
/** Probe and serve `.br`/`.gz` variants. */
|
|
31
|
+
precompressedAssets: boolean;
|
|
32
|
+
};
|
|
33
|
+
|
|
26
34
|
async function respondPrecompressedAsset(
|
|
27
35
|
context: Context,
|
|
28
36
|
asset: StaticAsset,
|
|
29
37
|
serve: ServeStaticAsset,
|
|
38
|
+
precompressedAssets: boolean,
|
|
30
39
|
) {
|
|
31
|
-
const selected = await resolvePreCompressedAsset(
|
|
40
|
+
const selected = await resolvePreCompressedAsset(
|
|
41
|
+
context,
|
|
42
|
+
asset.filename,
|
|
43
|
+
precompressedAssets,
|
|
44
|
+
);
|
|
32
45
|
const response = selected.acceptable
|
|
33
46
|
? await serve({
|
|
34
47
|
filename: selected.selected?.filepath,
|
|
@@ -48,7 +61,16 @@ async function respondPrecompressedAsset(
|
|
|
48
61
|
|
|
49
62
|
// Native core owns readers, containment and response conversion.
|
|
50
63
|
// This extension owns federation and compressed/generated asset selection.
|
|
51
|
-
export function createStaticExtensionResponders(
|
|
64
|
+
export function createStaticExtensionResponders({
|
|
65
|
+
immutableAssets,
|
|
66
|
+
precompressedAssets,
|
|
67
|
+
}: StaticExtensionPolicy): ServerStaticPluginOptions {
|
|
68
|
+
const stat = immutableAssets ? createImmutableAssetStat() : undefined;
|
|
69
|
+
const respondPrecompressed = (
|
|
70
|
+
context: Context,
|
|
71
|
+
asset: StaticAsset,
|
|
72
|
+
serve: ServeStaticAsset,
|
|
73
|
+
) => respondPrecompressedAsset(context, asset, serve, precompressedAssets);
|
|
52
74
|
const caches = new Map<
|
|
53
75
|
string,
|
|
54
76
|
{
|
|
@@ -73,7 +95,7 @@ export function createStaticExtensionResponders(): ServerStaticPluginOptions {
|
|
|
73
95
|
const pending = getModuleFederationAssetList(root)
|
|
74
96
|
.then(assets => {
|
|
75
97
|
current.assets = assets;
|
|
76
|
-
current.expires = Date.now() + 1_000;
|
|
98
|
+
current.expires = immutableAssets ? Infinity : Date.now() + 1_000;
|
|
77
99
|
return assets;
|
|
78
100
|
})
|
|
79
101
|
.finally(() => {
|
|
@@ -96,7 +118,7 @@ export function createStaticExtensionResponders(): ServerStaticPluginOptions {
|
|
|
96
118
|
isModuleFederationManifestRequest(requestPath) &&
|
|
97
119
|
!isBackendModuleFederationManifestRequest(requestPath);
|
|
98
120
|
if (!patchManifest && !remoteEntry) {
|
|
99
|
-
return
|
|
121
|
+
return respondPrecompressed(context, asset, serve);
|
|
100
122
|
}
|
|
101
123
|
const response = await serve({ contentLength: true, revalidate: false });
|
|
102
124
|
if (!response) {
|
|
@@ -141,7 +163,7 @@ export function createStaticExtensionResponders(): ServerStaticPluginOptions {
|
|
|
141
163
|
root: publicRoot,
|
|
142
164
|
realpath: true,
|
|
143
165
|
},
|
|
144
|
-
(asset, serve) =>
|
|
166
|
+
(asset, serve) => respondPrecompressed(context, asset, serve),
|
|
145
167
|
);
|
|
146
168
|
if (response && context.req.raw.method.toUpperCase() === 'HEAD') {
|
|
147
169
|
return new Response(null, {
|
|
@@ -153,6 +175,7 @@ export function createStaticExtensionResponders(): ServerStaticPluginOptions {
|
|
|
153
175
|
}
|
|
154
176
|
|
|
155
177
|
return {
|
|
178
|
+
stat,
|
|
156
179
|
async respondAsset(context, asset, serve, { root, pathPrefix }) {
|
|
157
180
|
const requestPath = getModuleFederationRequestPath(
|
|
158
181
|
context.req.path,
|
|
@@ -172,7 +195,7 @@ export function createStaticExtensionResponders(): ServerStaticPluginOptions {
|
|
|
172
195
|
assets.remoteEntries.has(requestPath),
|
|
173
196
|
);
|
|
174
197
|
}
|
|
175
|
-
return
|
|
198
|
+
return respondPrecompressed(context, asset, serve);
|
|
176
199
|
},
|
|
177
200
|
async respondPublicFallback(context, respondPublic, { root, pathPrefix }) {
|
|
178
201
|
const requestPath = getModuleFederationRequestPath(
|
|
@@ -190,6 +213,7 @@ export function createStaticExtensionResponders(): ServerStaticPluginOptions {
|
|
|
190
213
|
filename: path.join(root, requestPath),
|
|
191
214
|
kind: 'static',
|
|
192
215
|
root,
|
|
216
|
+
stat,
|
|
193
217
|
},
|
|
194
218
|
(asset, serve) =>
|
|
195
219
|
serveFederated(
|
|
@@ -126,14 +126,15 @@ const appendVaryHeader = (
|
|
|
126
126
|
export const resolvePreCompressedAsset = async (
|
|
127
127
|
c: Parameters<Middleware>[0],
|
|
128
128
|
filepath: string,
|
|
129
|
+
probeVariants: boolean,
|
|
129
130
|
): Promise<ResolvePreCompressedAssetResult> => {
|
|
130
131
|
const brPath = `${filepath}${PRE_COMPRESSED_ASSET_EXTENSIONS.br}`;
|
|
131
132
|
const gzipPath = `${filepath}${PRE_COMPRESSED_ASSET_EXTENSIONS.gzip}`;
|
|
132
133
|
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
fs.pathExists(gzipPath)
|
|
136
|
-
|
|
134
|
+
// Without variants only identity negotiation remains, including its 406.
|
|
135
|
+
const [hasBr, hasGzip] = probeVariants
|
|
136
|
+
? await Promise.all([fs.pathExists(brPath), fs.pathExists(gzipPath)])
|
|
137
|
+
: [false, false];
|
|
137
138
|
|
|
138
139
|
const hasVariant = hasBr || hasGzip;
|
|
139
140
|
const acceptedRepresentations = getAcceptedRepresentations(
|