@bleedingdev/modern-js-server-core 3.2.0-ultramodern.0
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/LICENSE +21 -0
- package/README.md +26 -0
- package/dist/cjs/adapters/node/helper/index.js +55 -0
- package/dist/cjs/adapters/node/helper/loadCache.js +58 -0
- package/dist/cjs/adapters/node/helper/loadConfig.js +78 -0
- package/dist/cjs/adapters/node/helper/loadEnv.js +66 -0
- package/dist/cjs/adapters/node/helper/loadPlugin.js +47 -0
- package/dist/cjs/adapters/node/helper/utils.js +39 -0
- package/dist/cjs/adapters/node/hono.js +95 -0
- package/dist/cjs/adapters/node/index.js +87 -0
- package/dist/cjs/adapters/node/node.js +180 -0
- package/dist/cjs/adapters/node/plugins/index.js +72 -0
- package/dist/cjs/adapters/node/plugins/nodeServer.js +43 -0
- package/dist/cjs/adapters/node/plugins/resource.js +206 -0
- package/dist/cjs/adapters/node/plugins/static.js +379 -0
- package/dist/cjs/constants.js +77 -0
- package/dist/cjs/context.js +41 -0
- package/dist/cjs/helper.js +45 -0
- package/dist/cjs/hono.js +46 -0
- package/dist/cjs/index.js +247 -0
- package/dist/cjs/plugins/compat/hooks.js +80 -0
- package/dist/cjs/plugins/compat/index.js +64 -0
- package/dist/cjs/plugins/contractGateAutopilot.js +158 -0
- package/dist/cjs/plugins/contractGateSnapshotStore.js +239 -0
- package/dist/cjs/plugins/default.js +61 -0
- package/dist/cjs/plugins/favicon.js +48 -0
- package/dist/cjs/plugins/index.js +157 -0
- package/dist/cjs/plugins/log.js +92 -0
- package/dist/cjs/plugins/mfCache.js +78 -0
- package/dist/cjs/plugins/middlewares.js +49 -0
- package/dist/cjs/plugins/monitors.js +192 -0
- package/dist/cjs/plugins/processedBy.js +50 -0
- package/dist/cjs/plugins/render/csrRscRender.js +74 -0
- package/dist/cjs/plugins/render/dataHandler.js +53 -0
- package/dist/cjs/plugins/render/index.js +158 -0
- package/dist/cjs/plugins/render/inject.js +92 -0
- package/dist/cjs/plugins/render/render.js +273 -0
- package/dist/cjs/plugins/render/renderRscHandler.js +72 -0
- package/dist/cjs/plugins/render/serverActionHandler.js +47 -0
- package/dist/cjs/plugins/render/ssrCache.js +198 -0
- package/dist/cjs/plugins/render/ssrRender.js +96 -0
- package/dist/cjs/plugins/render/utils.js +49 -0
- package/dist/cjs/plugins/route.js +68 -0
- package/dist/cjs/plugins/telemetry.js +1283 -0
- package/dist/cjs/serverBase.js +176 -0
- package/dist/cjs/types/config/bff.js +18 -0
- package/dist/cjs/types/config/dev.js +18 -0
- package/dist/cjs/types/config/html.js +18 -0
- package/dist/cjs/types/config/index.js +93 -0
- package/dist/cjs/types/config/output.js +18 -0
- package/dist/cjs/types/config/security.js +18 -0
- package/dist/cjs/types/config/server.js +18 -0
- package/dist/cjs/types/config/share.js +18 -0
- package/dist/cjs/types/config/source.js +18 -0
- package/dist/cjs/types/config/tools.js +18 -0
- package/dist/cjs/types/index.js +79 -0
- package/dist/cjs/types/plugins/base.js +18 -0
- package/dist/cjs/types/plugins/index.js +58 -0
- package/dist/cjs/types/plugins/plugin.js +18 -0
- package/dist/cjs/types/render.js +18 -0
- package/dist/cjs/types/requestHandler.js +18 -0
- package/dist/cjs/types/server.js +18 -0
- package/dist/cjs/utils/entry.js +40 -0
- package/dist/cjs/utils/env.js +51 -0
- package/dist/cjs/utils/error.js +93 -0
- package/dist/cjs/utils/index.js +114 -0
- package/dist/cjs/utils/middlewareCollector.js +63 -0
- package/dist/cjs/utils/publicDir.js +92 -0
- package/dist/cjs/utils/request.js +86 -0
- package/dist/cjs/utils/serverConfig.js +43 -0
- package/dist/cjs/utils/storage.js +69 -0
- package/dist/cjs/utils/transformStream.js +65 -0
- package/dist/cjs/utils/warmup.js +40 -0
- package/dist/esm/adapters/node/helper/index.mjs +5 -0
- package/dist/esm/adapters/node/helper/loadCache.mjs +14 -0
- package/dist/esm/adapters/node/helper/loadConfig.mjs +31 -0
- package/dist/esm/adapters/node/helper/loadEnv.mjs +22 -0
- package/dist/esm/adapters/node/helper/loadPlugin.mjs +13 -0
- package/dist/esm/adapters/node/helper/utils.mjs +5 -0
- package/dist/esm/adapters/node/hono.mjs +55 -0
- package/dist/esm/adapters/node/index.mjs +4 -0
- package/dist/esm/adapters/node/node.mjs +130 -0
- package/dist/esm/adapters/node/plugins/index.mjs +3 -0
- package/dist/esm/adapters/node/plugins/nodeServer.mjs +9 -0
- package/dist/esm/adapters/node/plugins/resource.mjs +138 -0
- package/dist/esm/adapters/node/plugins/static.mjs +329 -0
- package/dist/esm/constants.mjs +28 -0
- package/dist/esm/context.mjs +4 -0
- package/dist/esm/helper.mjs +11 -0
- package/dist/esm/hono.mjs +2 -0
- package/dist/esm/index.mjs +12 -0
- package/dist/esm/plugins/compat/hooks.mjs +40 -0
- package/dist/esm/plugins/compat/index.mjs +27 -0
- package/dist/esm/plugins/contractGateAutopilot.mjs +124 -0
- package/dist/esm/plugins/contractGateSnapshotStore.mjs +180 -0
- package/dist/esm/plugins/default.mjs +27 -0
- package/dist/esm/plugins/favicon.mjs +14 -0
- package/dist/esm/plugins/index.mjs +11 -0
- package/dist/esm/plugins/log.mjs +58 -0
- package/dist/esm/plugins/mfCache.mjs +35 -0
- package/dist/esm/plugins/middlewares.mjs +15 -0
- package/dist/esm/plugins/monitors.mjs +149 -0
- package/dist/esm/plugins/processedBy.mjs +16 -0
- package/dist/esm/plugins/render/csrRscRender.mjs +40 -0
- package/dist/esm/plugins/render/dataHandler.mjs +19 -0
- package/dist/esm/plugins/render/index.mjs +84 -0
- package/dist/esm/plugins/render/inject.mjs +55 -0
- package/dist/esm/plugins/render/render.mjs +230 -0
- package/dist/esm/plugins/render/renderRscHandler.mjs +38 -0
- package/dist/esm/plugins/render/serverActionHandler.mjs +13 -0
- package/dist/esm/plugins/render/ssrCache.mjs +158 -0
- package/dist/esm/plugins/render/ssrRender.mjs +62 -0
- package/dist/esm/plugins/render/utils.mjs +15 -0
- package/dist/esm/plugins/route.mjs +34 -0
- package/dist/esm/plugins/telemetry.mjs +1195 -0
- package/dist/esm/rslib-runtime.mjs +18 -0
- package/dist/esm/serverBase.mjs +139 -0
- package/dist/esm/types/config/bff.mjs +0 -0
- package/dist/esm/types/config/dev.mjs +0 -0
- package/dist/esm/types/config/html.mjs +0 -0
- package/dist/esm/types/config/index.mjs +6 -0
- package/dist/esm/types/config/output.mjs +0 -0
- package/dist/esm/types/config/security.mjs +0 -0
- package/dist/esm/types/config/server.mjs +0 -0
- package/dist/esm/types/config/share.mjs +0 -0
- package/dist/esm/types/config/source.mjs +0 -0
- package/dist/esm/types/config/tools.mjs +0 -0
- package/dist/esm/types/index.mjs +4 -0
- package/dist/esm/types/plugins/base.mjs +0 -0
- package/dist/esm/types/plugins/index.mjs +1 -0
- package/dist/esm/types/plugins/plugin.mjs +0 -0
- package/dist/esm/types/render.mjs +0 -0
- package/dist/esm/types/requestHandler.mjs +0 -0
- package/dist/esm/types/server.mjs +0 -0
- package/dist/esm/utils/entry.mjs +3 -0
- package/dist/esm/utils/env.mjs +14 -0
- package/dist/esm/utils/error.mjs +53 -0
- package/dist/esm/utils/index.mjs +9 -0
- package/dist/esm/utils/middlewareCollector.mjs +26 -0
- package/dist/esm/utils/publicDir.mjs +33 -0
- package/dist/esm/utils/request.mjs +40 -0
- package/dist/esm/utils/serverConfig.mjs +9 -0
- package/dist/esm/utils/storage.mjs +35 -0
- package/dist/esm/utils/transformStream.mjs +28 -0
- package/dist/esm/utils/warmup.mjs +6 -0
- package/dist/esm-node/adapters/node/helper/index.mjs +6 -0
- package/dist/esm-node/adapters/node/helper/loadCache.mjs +15 -0
- package/dist/esm-node/adapters/node/helper/loadConfig.mjs +32 -0
- package/dist/esm-node/adapters/node/helper/loadEnv.mjs +23 -0
- package/dist/esm-node/adapters/node/helper/loadPlugin.mjs +14 -0
- package/dist/esm-node/adapters/node/helper/utils.mjs +6 -0
- package/dist/esm-node/adapters/node/hono.mjs +56 -0
- package/dist/esm-node/adapters/node/index.mjs +5 -0
- package/dist/esm-node/adapters/node/node.mjs +131 -0
- package/dist/esm-node/adapters/node/plugins/index.mjs +4 -0
- package/dist/esm-node/adapters/node/plugins/nodeServer.mjs +10 -0
- package/dist/esm-node/adapters/node/plugins/resource.mjs +139 -0
- package/dist/esm-node/adapters/node/plugins/static.mjs +330 -0
- package/dist/esm-node/constants.mjs +29 -0
- package/dist/esm-node/context.mjs +5 -0
- package/dist/esm-node/helper.mjs +12 -0
- package/dist/esm-node/hono.mjs +3 -0
- package/dist/esm-node/index.mjs +13 -0
- package/dist/esm-node/plugins/compat/hooks.mjs +41 -0
- package/dist/esm-node/plugins/compat/index.mjs +28 -0
- package/dist/esm-node/plugins/contractGateAutopilot.mjs +125 -0
- package/dist/esm-node/plugins/contractGateSnapshotStore.mjs +182 -0
- package/dist/esm-node/plugins/default.mjs +28 -0
- package/dist/esm-node/plugins/favicon.mjs +15 -0
- package/dist/esm-node/plugins/index.mjs +12 -0
- package/dist/esm-node/plugins/log.mjs +59 -0
- package/dist/esm-node/plugins/mfCache.mjs +36 -0
- package/dist/esm-node/plugins/middlewares.mjs +16 -0
- package/dist/esm-node/plugins/monitors.mjs +150 -0
- package/dist/esm-node/plugins/processedBy.mjs +17 -0
- package/dist/esm-node/plugins/render/csrRscRender.mjs +41 -0
- package/dist/esm-node/plugins/render/dataHandler.mjs +20 -0
- package/dist/esm-node/plugins/render/index.mjs +85 -0
- package/dist/esm-node/plugins/render/inject.mjs +56 -0
- package/dist/esm-node/plugins/render/render.mjs +231 -0
- package/dist/esm-node/plugins/render/renderRscHandler.mjs +39 -0
- package/dist/esm-node/plugins/render/serverActionHandler.mjs +14 -0
- package/dist/esm-node/plugins/render/ssrCache.mjs +159 -0
- package/dist/esm-node/plugins/render/ssrRender.mjs +63 -0
- package/dist/esm-node/plugins/render/utils.mjs +16 -0
- package/dist/esm-node/plugins/route.mjs +35 -0
- package/dist/esm-node/plugins/telemetry.mjs +1196 -0
- package/dist/esm-node/rslib-runtime.mjs +19 -0
- package/dist/esm-node/serverBase.mjs +140 -0
- package/dist/esm-node/types/config/bff.mjs +1 -0
- package/dist/esm-node/types/config/dev.mjs +1 -0
- package/dist/esm-node/types/config/html.mjs +1 -0
- package/dist/esm-node/types/config/index.mjs +7 -0
- package/dist/esm-node/types/config/output.mjs +1 -0
- package/dist/esm-node/types/config/security.mjs +1 -0
- package/dist/esm-node/types/config/server.mjs +1 -0
- package/dist/esm-node/types/config/share.mjs +1 -0
- package/dist/esm-node/types/config/source.mjs +1 -0
- package/dist/esm-node/types/config/tools.mjs +1 -0
- package/dist/esm-node/types/index.mjs +5 -0
- package/dist/esm-node/types/plugins/base.mjs +1 -0
- package/dist/esm-node/types/plugins/index.mjs +2 -0
- package/dist/esm-node/types/plugins/plugin.mjs +1 -0
- package/dist/esm-node/types/render.mjs +1 -0
- package/dist/esm-node/types/requestHandler.mjs +1 -0
- package/dist/esm-node/types/server.mjs +1 -0
- package/dist/esm-node/utils/entry.mjs +4 -0
- package/dist/esm-node/utils/env.mjs +15 -0
- package/dist/esm-node/utils/error.mjs +54 -0
- package/dist/esm-node/utils/index.mjs +10 -0
- package/dist/esm-node/utils/middlewareCollector.mjs +27 -0
- package/dist/esm-node/utils/publicDir.mjs +34 -0
- package/dist/esm-node/utils/request.mjs +41 -0
- package/dist/esm-node/utils/serverConfig.mjs +10 -0
- package/dist/esm-node/utils/storage.mjs +36 -0
- package/dist/esm-node/utils/transformStream.mjs +29 -0
- package/dist/esm-node/utils/warmup.mjs +7 -0
- package/dist/types/adapters/node/helper/index.d.ts +6 -0
- package/dist/types/adapters/node/helper/loadCache.d.ts +2 -0
- package/dist/types/adapters/node/helper/loadConfig.d.ts +3 -0
- package/dist/types/adapters/node/helper/loadEnv.d.ts +3 -0
- package/dist/types/adapters/node/helper/loadPlugin.d.ts +3 -0
- package/dist/types/adapters/node/helper/utils.d.ts +21 -0
- package/dist/types/adapters/node/hono.d.ts +19 -0
- package/dist/types/adapters/node/index.d.ts +5 -0
- package/dist/types/adapters/node/node.d.ts +17 -0
- package/dist/types/adapters/node/plugins/index.d.ts +3 -0
- package/dist/types/adapters/node/plugins/nodeServer.d.ts +6 -0
- package/dist/types/adapters/node/plugins/resource.d.ts +11 -0
- package/dist/types/adapters/node/plugins/static.d.ts +25 -0
- package/dist/types/constants.d.ts +26 -0
- package/dist/types/context.d.ts +3 -0
- package/dist/types/helper.d.ts +10 -0
- package/dist/types/hono.d.ts +3 -0
- package/dist/types/index.d.ts +14 -0
- package/dist/types/plugins/compat/hooks.d.ts +8 -0
- package/dist/types/plugins/compat/index.d.ts +3 -0
- package/dist/types/plugins/contractGateAutopilot.d.ts +35 -0
- package/dist/types/plugins/contractGateSnapshotStore.d.ts +57 -0
- package/dist/types/plugins/default.d.ts +7 -0
- package/dist/types/plugins/favicon.d.ts +2 -0
- package/dist/types/plugins/index.d.ts +11 -0
- package/dist/types/plugins/log.d.ts +2 -0
- package/dist/types/plugins/mfCache.d.ts +12 -0
- package/dist/types/plugins/middlewares.d.ts +2 -0
- package/dist/types/plugins/monitors.d.ts +6 -0
- package/dist/types/plugins/processedBy.d.ts +2 -0
- package/dist/types/plugins/render/csrRscRender.d.ts +2 -0
- package/dist/types/plugins/render/dataHandler.d.ts +5 -0
- package/dist/types/plugins/render/index.d.ts +3 -0
- package/dist/types/plugins/render/inject.d.ts +7 -0
- package/dist/types/plugins/render/render.d.ts +16 -0
- package/dist/types/plugins/render/renderRscHandler.d.ts +2 -0
- package/dist/types/plugins/render/serverActionHandler.d.ts +2 -0
- package/dist/types/plugins/render/ssrCache.d.ts +18 -0
- package/dist/types/plugins/render/ssrRender.d.ts +26 -0
- package/dist/types/plugins/render/utils.d.ts +3 -0
- package/dist/types/plugins/route.d.ts +2 -0
- package/dist/types/plugins/telemetry.d.ts +309 -0
- package/dist/types/serverBase.d.ts +38 -0
- package/dist/types/types/config/bff.d.ts +142 -0
- package/dist/types/types/config/dev.d.ts +4 -0
- package/dist/types/types/config/html.d.ts +15 -0
- package/dist/types/types/config/index.d.ts +35 -0
- package/dist/types/types/config/output.d.ts +20 -0
- package/dist/types/types/config/security.d.ts +4 -0
- package/dist/types/types/config/server.d.ts +402 -0
- package/dist/types/types/config/share.d.ts +3 -0
- package/dist/types/types/config/source.d.ts +7 -0
- package/dist/types/types/config/tools.d.ts +2 -0
- package/dist/types/types/index.d.ts +4 -0
- package/dist/types/types/plugins/base.d.ts +57 -0
- package/dist/types/types/plugins/index.d.ts +2 -0
- package/dist/types/types/plugins/plugin.d.ts +31 -0
- package/dist/types/types/render.d.ts +24 -0
- package/dist/types/types/requestHandler.d.ts +48 -0
- package/dist/types/types/server.d.ts +67 -0
- package/dist/types/utils/entry.d.ts +3 -0
- package/dist/types/utils/env.d.ts +2 -0
- package/dist/types/utils/error.d.ts +8 -0
- package/dist/types/utils/index.d.ts +9 -0
- package/dist/types/utils/middlewareCollector.d.ts +12 -0
- package/dist/types/utils/publicDir.d.ts +40 -0
- package/dist/types/utils/request.d.ts +17 -0
- package/dist/types/utils/serverConfig.d.ts +8 -0
- package/dist/types/utils/storage.d.ts +5 -0
- package/dist/types/utils/transformStream.d.ts +5 -0
- package/dist/types/utils/warmup.d.ts +1 -0
- package/package.json +103 -0
- package/rslib.config.mts +4 -0
- package/rstest.config.mts +7 -0
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import "node:module";
|
|
2
|
+
export * from "./plugins/index.mjs";
|
|
3
|
+
export * from "./types/config/index.mjs";
|
|
4
|
+
export * from "./types/plugins/index.mjs";
|
|
5
|
+
export * from "./types/render.mjs";
|
|
6
|
+
export * from "./types/requestHandler.mjs";
|
|
7
|
+
export { Hono } from "hono";
|
|
8
|
+
export { AGGRED_DIR } from "./constants.mjs";
|
|
9
|
+
export { run, useHonoContext } from "./context.mjs";
|
|
10
|
+
export { getLoaderCtx } from "./helper.mjs";
|
|
11
|
+
export { createServerBase } from "./serverBase.mjs";
|
|
12
|
+
export { ErrorDigest, createErrorHtml, onError } from "./utils/index.mjs";
|
|
13
|
+
export { getPublicDirConfig, getPublicDirPatterns, getPublicDirRoutePrefixes, normalizePublicDir, normalizePublicDirPath, resolvePublicDirPaths } from "./utils/publicDir.mjs";
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
import "node:module";
|
|
2
|
+
function getHookRunners(context) {
|
|
3
|
+
const { hooks } = context;
|
|
4
|
+
return {
|
|
5
|
+
config: (params)=>hooks.modifyConfig.call(params),
|
|
6
|
+
prepare: ()=>hooks.onPrepare.call(),
|
|
7
|
+
reset: (params)=>hooks.onReset.call(params),
|
|
8
|
+
fallback: (input)=>hooks.fallback.call(input),
|
|
9
|
+
prepareWebServer: (input)=>hooks.prepareWebServer.call(input),
|
|
10
|
+
prepareApiServer: (input)=>hooks.prepareApiServer.call(input),
|
|
11
|
+
afterMatch: (ctx)=>hooks.afterMatch.call(ctx),
|
|
12
|
+
afterRender: (ctx)=>hooks.afterRender.call(ctx),
|
|
13
|
+
afterStreamingRender: (ctx)=>hooks.afterStreamingRender.call(ctx)
|
|
14
|
+
};
|
|
15
|
+
}
|
|
16
|
+
function transformHookRunner(hookRunnerName) {
|
|
17
|
+
switch(hookRunnerName){
|
|
18
|
+
case 'config':
|
|
19
|
+
return 'modifyConfig';
|
|
20
|
+
case 'prepare':
|
|
21
|
+
return 'onPrepare';
|
|
22
|
+
case 'reset':
|
|
23
|
+
return 'onReset';
|
|
24
|
+
default:
|
|
25
|
+
return hookRunnerName;
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
function handleSetupResult(setupResult, api) {
|
|
29
|
+
if (!setupResult) return;
|
|
30
|
+
Object.keys(setupResult).forEach((key)=>{
|
|
31
|
+
const fn = setupResult[key];
|
|
32
|
+
if ('function' == typeof fn) {
|
|
33
|
+
const newAPI = transformHookRunner(key);
|
|
34
|
+
if (api[newAPI]) api[newAPI]((...params)=>{
|
|
35
|
+
const res = fn(...params);
|
|
36
|
+
return res;
|
|
37
|
+
});
|
|
38
|
+
}
|
|
39
|
+
});
|
|
40
|
+
}
|
|
41
|
+
export { getHookRunners, handleSetupResult, transformHookRunner };
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import "node:module";
|
|
2
|
+
import { createAsyncHook, createAsyncPipelineHook } from "@modern-js/plugin";
|
|
3
|
+
import { getHookRunners, handleSetupResult } from "./hooks.mjs";
|
|
4
|
+
const compatPlugin = ()=>({
|
|
5
|
+
name: '@modern-js/server-compat',
|
|
6
|
+
registryHooks: {
|
|
7
|
+
fallback: createAsyncHook(),
|
|
8
|
+
prepareWebServer: createAsyncPipelineHook(),
|
|
9
|
+
prepareApiServer: createAsyncPipelineHook(),
|
|
10
|
+
afterMatch: createAsyncPipelineHook(),
|
|
11
|
+
afterRender: createAsyncPipelineHook(),
|
|
12
|
+
afterStreamingRender: createAsyncPipelineHook()
|
|
13
|
+
},
|
|
14
|
+
_registryApi: (getServerContext, updateServerContext)=>{
|
|
15
|
+
const getInternalContext = ()=>getServerContext()._internalContext;
|
|
16
|
+
return {
|
|
17
|
+
useConfigContext: ()=>getInternalContext().config,
|
|
18
|
+
useAppContext: ()=>{
|
|
19
|
+
const { _internalContext, ...serverContext } = getServerContext();
|
|
20
|
+
return serverContext;
|
|
21
|
+
},
|
|
22
|
+
setAppContext: (context)=>updateServerContext(context),
|
|
23
|
+
useHookRunners: ()=>getHookRunners(getInternalContext())
|
|
24
|
+
};
|
|
25
|
+
},
|
|
26
|
+
setup: ()=>{}
|
|
27
|
+
});
|
|
28
|
+
export { compatPlugin, handleSetupResult };
|
|
@@ -0,0 +1,125 @@
|
|
|
1
|
+
import "node:module";
|
|
2
|
+
import { createFileContractGateSnapshotStore } from "./contractGateSnapshotStore.mjs";
|
|
3
|
+
const DEFAULT_POLL_INTERVAL_MS = 15000;
|
|
4
|
+
const DEFAULT_GATE_STALE_AFTER_MS = 600000;
|
|
5
|
+
class ContractGateAutopilot {
|
|
6
|
+
async start() {
|
|
7
|
+
await this.syncOnce();
|
|
8
|
+
if (this.poller) return;
|
|
9
|
+
this.poller = setInterval(()=>{
|
|
10
|
+
this.syncOnce();
|
|
11
|
+
}, this.pollIntervalMs);
|
|
12
|
+
if ('function' == typeof this.poller.unref) this.poller.unref();
|
|
13
|
+
}
|
|
14
|
+
stop() {
|
|
15
|
+
if (this.poller) {
|
|
16
|
+
clearInterval(this.poller);
|
|
17
|
+
this.poller = void 0;
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
async syncOnce() {
|
|
21
|
+
const snapshot = await this.loadSnapshot();
|
|
22
|
+
if (!snapshot) return 0;
|
|
23
|
+
const gates = this.normalizeSnapshot(snapshot);
|
|
24
|
+
let updatedCount = 0;
|
|
25
|
+
for (const gate of gates){
|
|
26
|
+
this.orchestrator.addRequiredContractGate(gate.name);
|
|
27
|
+
const fingerprint = `${gate.passed ? '1' : '0'}:${gate.reason || ''}`;
|
|
28
|
+
if (this.appliedGateFingerprints.get(gate.name) !== fingerprint) {
|
|
29
|
+
this.orchestrator.setContractGate(gate.name, gate.passed, gate.reason);
|
|
30
|
+
this.appliedGateFingerprints.set(gate.name, fingerprint);
|
|
31
|
+
updatedCount += 1;
|
|
32
|
+
this.logger?.info?.(`[telemetry.canary.autopilot] gate=${gate.name} passed=${String(gate.passed)} reason=${gate.reason || 'none'}`);
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
return updatedCount;
|
|
36
|
+
}
|
|
37
|
+
async loadSnapshot() {
|
|
38
|
+
try {
|
|
39
|
+
const snapshot = await this.gateSnapshotStore.readSnapshot();
|
|
40
|
+
if (!snapshot) return;
|
|
41
|
+
const fingerprint = JSON.stringify(snapshot);
|
|
42
|
+
if (fingerprint === this.lastSnapshotFingerprint) return;
|
|
43
|
+
this.lastSnapshotFingerprint = fingerprint;
|
|
44
|
+
return snapshot;
|
|
45
|
+
} catch (error) {
|
|
46
|
+
const source = this.gateSnapshotPath || this.gateSnapshotStore.name || 'stateStore';
|
|
47
|
+
this.logger?.warn?.(`[telemetry.canary.autopilot] failed to load gate snapshot ${source}: ${error instanceof Error ? error.message : String(error)}`);
|
|
48
|
+
return;
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
normalizeSnapshot(snapshot) {
|
|
52
|
+
const now = Date.now();
|
|
53
|
+
const output = [];
|
|
54
|
+
const gates = snapshot.gates;
|
|
55
|
+
if (!gates || 'object' != typeof gates) return output;
|
|
56
|
+
for (const [name, value] of Object.entries(gates)){
|
|
57
|
+
const normalizedName = name.trim();
|
|
58
|
+
if (!normalizedName) continue;
|
|
59
|
+
const gate = this.normalizeGateValue(value, snapshot.updatedAt, now);
|
|
60
|
+
if (!gate) continue;
|
|
61
|
+
if ('number' == typeof gate.expiresAt && Number.isFinite(gate.expiresAt) && gate.expiresAt > 0 && now >= gate.expiresAt) {
|
|
62
|
+
output.push({
|
|
63
|
+
name: normalizedName,
|
|
64
|
+
passed: true,
|
|
65
|
+
reason: void 0,
|
|
66
|
+
updatedAt: gate.updatedAt,
|
|
67
|
+
expiresAt: gate.expiresAt
|
|
68
|
+
});
|
|
69
|
+
continue;
|
|
70
|
+
}
|
|
71
|
+
const isStale = this.gateStaleAfterMs > 0 && now - gate.updatedAt > this.gateStaleAfterMs;
|
|
72
|
+
if (isStale) {
|
|
73
|
+
output.push({
|
|
74
|
+
name: normalizedName,
|
|
75
|
+
passed: false,
|
|
76
|
+
reason: gate.reason || 'Gate snapshot is stale',
|
|
77
|
+
updatedAt: gate.updatedAt
|
|
78
|
+
});
|
|
79
|
+
continue;
|
|
80
|
+
}
|
|
81
|
+
output.push({
|
|
82
|
+
name: normalizedName,
|
|
83
|
+
passed: gate.passed,
|
|
84
|
+
reason: gate.reason,
|
|
85
|
+
updatedAt: gate.updatedAt
|
|
86
|
+
});
|
|
87
|
+
}
|
|
88
|
+
return output;
|
|
89
|
+
}
|
|
90
|
+
normalizeGateValue(value, snapshotUpdatedAt, now) {
|
|
91
|
+
if ('boolean' == typeof value) return {
|
|
92
|
+
passed: value,
|
|
93
|
+
updatedAt: this.normalizeUpdatedAt(snapshotUpdatedAt, now)
|
|
94
|
+
};
|
|
95
|
+
if (!value || 'object' != typeof value) return;
|
|
96
|
+
const hasPassed = 'boolean' == typeof value.passed;
|
|
97
|
+
const passed = true === value.passed;
|
|
98
|
+
let reason = 'string' == typeof value.reason && value.reason.trim().length > 0 ? value.reason : void 0;
|
|
99
|
+
if (!hasPassed) reason = reason || 'Gate snapshot record is missing "passed" boolean';
|
|
100
|
+
return {
|
|
101
|
+
passed,
|
|
102
|
+
reason,
|
|
103
|
+
updatedAt: this.normalizeUpdatedAt(value.updatedAt ?? snapshotUpdatedAt, now),
|
|
104
|
+
expiresAt: this.normalizeExpiresAt(value.expiresAt)
|
|
105
|
+
};
|
|
106
|
+
}
|
|
107
|
+
normalizeUpdatedAt(value, fallback) {
|
|
108
|
+
if ('number' == typeof value && Number.isFinite(value) && value > 0) return value;
|
|
109
|
+
return fallback;
|
|
110
|
+
}
|
|
111
|
+
normalizeExpiresAt(value) {
|
|
112
|
+
if ('number' == typeof value && Number.isFinite(value) && value > 0) return value;
|
|
113
|
+
}
|
|
114
|
+
constructor(options){
|
|
115
|
+
this.appliedGateFingerprints = new Map();
|
|
116
|
+
this.orchestrator = options.orchestrator;
|
|
117
|
+
if (!options.gateSnapshotStore && !options.gateSnapshotPath) throw new Error('ContractGateAutopilot requires gateSnapshotPath or gateSnapshotStore');
|
|
118
|
+
this.gateSnapshotPath = options.gateSnapshotPath;
|
|
119
|
+
this.gateSnapshotStore = options.gateSnapshotStore || createFileContractGateSnapshotStore(options.gateSnapshotPath);
|
|
120
|
+
this.pollIntervalMs = Math.max(250, options.pollIntervalMs ?? DEFAULT_POLL_INTERVAL_MS);
|
|
121
|
+
this.gateStaleAfterMs = Math.max(0, options.gateStaleAfterMs ?? DEFAULT_GATE_STALE_AFTER_MS);
|
|
122
|
+
this.logger = options.logger;
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
export { ContractGateAutopilot };
|
|
@@ -0,0 +1,182 @@
|
|
|
1
|
+
import __rslib_shim_module__ from "node:module";
|
|
2
|
+
const require = /*#__PURE__*/ __rslib_shim_module__.createRequire(/*#__PURE__*/ (()=>import.meta.url)());
|
|
3
|
+
import { fs } from "@modern-js/utils";
|
|
4
|
+
import { promises } from "fs";
|
|
5
|
+
import path from "path";
|
|
6
|
+
const CONTRACT_GATE_SNAPSHOT_SCHEMA_VERSION = 1;
|
|
7
|
+
const DEFAULT_CONTRACT_GATE_SNAPSHOT_PATH = '.modern/contract-gates.json';
|
|
8
|
+
const DEFAULT_HTTP_STORE_TIMEOUT_MS = 5000;
|
|
9
|
+
const BUILTIN_HTTP_STATE_STORE_MODULES = new Set([
|
|
10
|
+
'http',
|
|
11
|
+
'@modern-js/server-core/http',
|
|
12
|
+
'@modern-js/server-core/contract-gate-http-store'
|
|
13
|
+
]);
|
|
14
|
+
const isRecord = (value)=>'object' == typeof value && null !== value && !Array.isArray(value);
|
|
15
|
+
const normalizeSnapshot = (snapshot)=>{
|
|
16
|
+
if (!isRecord(snapshot)) return;
|
|
17
|
+
const schemaVersion = 'number' == typeof snapshot.schemaVersion ? snapshot.schemaVersion : CONTRACT_GATE_SNAPSHOT_SCHEMA_VERSION;
|
|
18
|
+
const updatedAt = 'number' == typeof snapshot.updatedAt ? snapshot.updatedAt : Date.now();
|
|
19
|
+
const gates = isRecord(snapshot.gates) ? snapshot.gates : {};
|
|
20
|
+
return {
|
|
21
|
+
schemaVersion,
|
|
22
|
+
updatedAt,
|
|
23
|
+
gates
|
|
24
|
+
};
|
|
25
|
+
};
|
|
26
|
+
const normalizeHttpStoreOptions = (options)=>{
|
|
27
|
+
const endpoint = 'string' == typeof options?.endpoint ? options.endpoint.trim() : '';
|
|
28
|
+
if (!endpoint) throw new Error('[telemetry.canary.autopilot] HTTP stateStore requires options.endpoint');
|
|
29
|
+
const readMethod = 'string' == typeof options?.readMethod && options.readMethod.trim() ? options.readMethod.trim().toUpperCase() : 'GET';
|
|
30
|
+
const writeMethod = 'string' == typeof options?.writeMethod && options.writeMethod.trim() ? options.writeMethod.trim().toUpperCase() : 'PUT';
|
|
31
|
+
const timeoutMsRaw = Number(options?.timeoutMs);
|
|
32
|
+
const timeoutMs = Number.isFinite(timeoutMsRaw) && timeoutMsRaw > 0 ? Math.floor(timeoutMsRaw) : DEFAULT_HTTP_STORE_TIMEOUT_MS;
|
|
33
|
+
const headersRaw = options?.headers;
|
|
34
|
+
const headers = {};
|
|
35
|
+
if (headersRaw && 'object' == typeof headersRaw && !Array.isArray(headersRaw)) Object.entries(headersRaw).forEach(([key, value])=>{
|
|
36
|
+
if ('string' == typeof key && key.trim().length > 0 && null != value) headers[key] = String(value);
|
|
37
|
+
});
|
|
38
|
+
return {
|
|
39
|
+
endpoint,
|
|
40
|
+
readMethod,
|
|
41
|
+
writeMethod,
|
|
42
|
+
headers,
|
|
43
|
+
timeoutMs
|
|
44
|
+
};
|
|
45
|
+
};
|
|
46
|
+
const withTimeoutAbort = (timeoutMs)=>{
|
|
47
|
+
const controller = new AbortController();
|
|
48
|
+
const timer = setTimeout(()=>controller.abort(), timeoutMs);
|
|
49
|
+
return {
|
|
50
|
+
signal: controller.signal,
|
|
51
|
+
clear: ()=>clearTimeout(timer)
|
|
52
|
+
};
|
|
53
|
+
};
|
|
54
|
+
const createHttpContractGateSnapshotStore = (options)=>{
|
|
55
|
+
const normalized = normalizeHttpStoreOptions(options);
|
|
56
|
+
const endpoint = normalized.endpoint;
|
|
57
|
+
return {
|
|
58
|
+
name: `http:${endpoint}`,
|
|
59
|
+
async readSnapshot () {
|
|
60
|
+
const { signal, clear } = withTimeoutAbort(normalized.timeoutMs || 5000);
|
|
61
|
+
try {
|
|
62
|
+
const response = await fetch(endpoint, {
|
|
63
|
+
method: normalized.readMethod || 'GET',
|
|
64
|
+
headers: {
|
|
65
|
+
accept: 'application/json',
|
|
66
|
+
...normalized.headers || {}
|
|
67
|
+
},
|
|
68
|
+
signal
|
|
69
|
+
});
|
|
70
|
+
if (404 === response.status) return;
|
|
71
|
+
if (!response.ok) throw new Error(`HTTP stateStore read failed with status ${String(response.status)}`);
|
|
72
|
+
const payload = await response.json();
|
|
73
|
+
return normalizeSnapshot(payload);
|
|
74
|
+
} finally{
|
|
75
|
+
clear();
|
|
76
|
+
}
|
|
77
|
+
},
|
|
78
|
+
async writeSnapshot (snapshot) {
|
|
79
|
+
const body = JSON.stringify(normalizeSnapshot(snapshot) || {
|
|
80
|
+
schemaVersion: CONTRACT_GATE_SNAPSHOT_SCHEMA_VERSION,
|
|
81
|
+
updatedAt: Date.now(),
|
|
82
|
+
gates: {}
|
|
83
|
+
});
|
|
84
|
+
const { signal, clear } = withTimeoutAbort(normalized.timeoutMs || 5000);
|
|
85
|
+
try {
|
|
86
|
+
const response = await fetch(endpoint, {
|
|
87
|
+
method: normalized.writeMethod || 'PUT',
|
|
88
|
+
headers: {
|
|
89
|
+
'content-type': 'application/json',
|
|
90
|
+
...normalized.headers || {}
|
|
91
|
+
},
|
|
92
|
+
body,
|
|
93
|
+
signal
|
|
94
|
+
});
|
|
95
|
+
if (!response.ok) throw new Error(`HTTP stateStore write failed with status ${String(response.status)}`);
|
|
96
|
+
} finally{
|
|
97
|
+
clear();
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
};
|
|
101
|
+
};
|
|
102
|
+
const tryResolveBuiltinSnapshotStore = (input)=>{
|
|
103
|
+
const moduleName = input.stateStore.module.trim();
|
|
104
|
+
if (!BUILTIN_HTTP_STATE_STORE_MODULES.has(moduleName)) return;
|
|
105
|
+
return createHttpContractGateSnapshotStore(input.stateStore.options || {});
|
|
106
|
+
};
|
|
107
|
+
const pickStoreFactory = (mod)=>{
|
|
108
|
+
if ('function' == typeof mod) return mod;
|
|
109
|
+
if ('function' == typeof mod.createContractGateSnapshotStore) return mod.createContractGateSnapshotStore;
|
|
110
|
+
if ('function' == typeof mod.default) return mod.default;
|
|
111
|
+
if (mod.default && 'object' == typeof mod.default && 'function' == typeof mod.default.createContractGateSnapshotStore) return mod.default.createContractGateSnapshotStore;
|
|
112
|
+
};
|
|
113
|
+
const ensureStoreShape = (store, modulePath)=>{
|
|
114
|
+
if (!store || 'object' != typeof store || 'function' != typeof store.readSnapshot || 'function' != typeof store.writeSnapshot) throw new Error(`Invalid contract gate snapshot store from "${modulePath}". Expected { readSnapshot(), writeSnapshot() }.`);
|
|
115
|
+
};
|
|
116
|
+
const resolveStoreModulePath = (appDirectory, modulePath)=>{
|
|
117
|
+
const normalized = modulePath.trim();
|
|
118
|
+
if (!normalized) throw new Error('Contract gate snapshot stateStore.module must be non-empty');
|
|
119
|
+
if (path.isAbsolute(normalized)) return normalized;
|
|
120
|
+
return normalized.startsWith('.') ? path.resolve(appDirectory, normalized) : normalized;
|
|
121
|
+
};
|
|
122
|
+
const resolveContractGateSnapshotPath = (appDirectory, configuredPath)=>{
|
|
123
|
+
const rawPath = configuredPath || process.env.MODERN_CONTRACT_GATES_FILE || DEFAULT_CONTRACT_GATE_SNAPSHOT_PATH;
|
|
124
|
+
if (path.isAbsolute(rawPath)) return rawPath;
|
|
125
|
+
return path.resolve(appDirectory, rawPath);
|
|
126
|
+
};
|
|
127
|
+
const createFileContractGateSnapshotStore = (gateSnapshotPath)=>{
|
|
128
|
+
const resolvedPath = path.resolve(gateSnapshotPath);
|
|
129
|
+
return {
|
|
130
|
+
name: `file:${resolvedPath}`,
|
|
131
|
+
async readSnapshot () {
|
|
132
|
+
if (!await fs.pathExists(resolvedPath)) return;
|
|
133
|
+
try {
|
|
134
|
+
const raw = await promises.readFile(resolvedPath, 'utf8');
|
|
135
|
+
return normalizeSnapshot(JSON.parse(raw));
|
|
136
|
+
} catch (_error) {
|
|
137
|
+
return;
|
|
138
|
+
}
|
|
139
|
+
},
|
|
140
|
+
async writeSnapshot (snapshot) {
|
|
141
|
+
const normalized = normalizeSnapshot(snapshot) || {
|
|
142
|
+
schemaVersion: CONTRACT_GATE_SNAPSHOT_SCHEMA_VERSION,
|
|
143
|
+
updatedAt: Date.now(),
|
|
144
|
+
gates: {}
|
|
145
|
+
};
|
|
146
|
+
await promises.mkdir(path.dirname(resolvedPath), {
|
|
147
|
+
recursive: true
|
|
148
|
+
});
|
|
149
|
+
await promises.writeFile(resolvedPath, `${JSON.stringify(normalized, null, 2)}\n`);
|
|
150
|
+
}
|
|
151
|
+
};
|
|
152
|
+
};
|
|
153
|
+
const resolveContractGateSnapshotStore = async (input)=>{
|
|
154
|
+
const { appDirectory, gateSnapshotPath, stateStore, logger } = input;
|
|
155
|
+
if (!stateStore?.module) return createFileContractGateSnapshotStore(gateSnapshotPath);
|
|
156
|
+
const builtinStore = tryResolveBuiltinSnapshotStore({
|
|
157
|
+
stateStore
|
|
158
|
+
});
|
|
159
|
+
if (builtinStore) {
|
|
160
|
+
logger?.info?.(`[telemetry.canary.autopilot] using built-in contract gate snapshot store "${builtinStore.name}"`);
|
|
161
|
+
return builtinStore;
|
|
162
|
+
}
|
|
163
|
+
const modulePath = resolveStoreModulePath(appDirectory, stateStore.module);
|
|
164
|
+
let mod;
|
|
165
|
+
try {
|
|
166
|
+
mod = require(modulePath);
|
|
167
|
+
} catch (error) {
|
|
168
|
+
throw new Error(`[telemetry.canary.autopilot] Failed to load stateStore.module "${stateStore.module}" (${modulePath}): ${error instanceof Error ? error.message : String(error)}`);
|
|
169
|
+
}
|
|
170
|
+
const factory = pickStoreFactory(mod);
|
|
171
|
+
if (!factory) throw new Error(`[telemetry.canary.autopilot] stateStore.module "${stateStore.module}" does not export createContractGateSnapshotStore()`);
|
|
172
|
+
const store = await factory({
|
|
173
|
+
appDirectory,
|
|
174
|
+
gateSnapshotPath,
|
|
175
|
+
options: stateStore.options,
|
|
176
|
+
logger
|
|
177
|
+
});
|
|
178
|
+
ensureStoreShape(store, modulePath);
|
|
179
|
+
logger?.info?.(`[telemetry.canary.autopilot] using contract gate snapshot store "${store.name || modulePath}"`);
|
|
180
|
+
return store;
|
|
181
|
+
};
|
|
182
|
+
export { CONTRACT_GATE_SNAPSHOT_SCHEMA_VERSION, DEFAULT_CONTRACT_GATE_SNAPSHOT_PATH, createFileContractGateSnapshotStore, createHttpContractGateSnapshotStore, resolveContractGateSnapshotPath, resolveContractGateSnapshotStore };
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import "node:module";
|
|
2
|
+
import { compatPlugin } from "./compat/index.mjs";
|
|
3
|
+
import { logPlugin } from "./log.mjs";
|
|
4
|
+
import { initMonitorsPlugin, injectServerTiming, injectloggerPlugin } from "./monitors.mjs";
|
|
5
|
+
import { processedByPlugin } from "./processedBy.mjs";
|
|
6
|
+
import { injectRenderHandlerPlugin } from "./render/index.mjs";
|
|
7
|
+
import { injectRoutePlugin } from "./route.mjs";
|
|
8
|
+
import { injectTelemetryPlugin } from "./telemetry.mjs";
|
|
9
|
+
function createSilenceLogger() {
|
|
10
|
+
return new Proxy({}, {
|
|
11
|
+
get: ()=>()=>{}
|
|
12
|
+
});
|
|
13
|
+
}
|
|
14
|
+
function createDefaultPlugins(options = {}) {
|
|
15
|
+
const plugins = [
|
|
16
|
+
compatPlugin(),
|
|
17
|
+
logPlugin(),
|
|
18
|
+
initMonitorsPlugin(),
|
|
19
|
+
injectTelemetryPlugin(),
|
|
20
|
+
injectRenderHandlerPlugin(options),
|
|
21
|
+
injectloggerPlugin(options.logger ? options.logger : createSilenceLogger()),
|
|
22
|
+
injectServerTiming(),
|
|
23
|
+
processedByPlugin(),
|
|
24
|
+
injectRoutePlugin()
|
|
25
|
+
];
|
|
26
|
+
return plugins;
|
|
27
|
+
}
|
|
28
|
+
export { createDefaultPlugins };
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import "node:module";
|
|
2
|
+
const faviconPlugin = ()=>({
|
|
3
|
+
name: '@modern-js/plugin-favicon',
|
|
4
|
+
setup (api) {
|
|
5
|
+
api.onPrepare(()=>{
|
|
6
|
+
const { middlewares } = api.getServerContext();
|
|
7
|
+
middlewares.push({
|
|
8
|
+
name: 'favicon-fallback',
|
|
9
|
+
path: '/favicon.ico',
|
|
10
|
+
handler: async (c, _next)=>c.body(null, 204)
|
|
11
|
+
});
|
|
12
|
+
});
|
|
13
|
+
}
|
|
14
|
+
});
|
|
15
|
+
export { faviconPlugin };
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import "node:module";
|
|
2
|
+
export { compatPlugin, handleSetupResult } from "./compat/index.mjs";
|
|
3
|
+
export { ContractGateAutopilot } from "./contractGateAutopilot.mjs";
|
|
4
|
+
export { CONTRACT_GATE_SNAPSHOT_SCHEMA_VERSION, DEFAULT_CONTRACT_GATE_SNAPSHOT_PATH, createFileContractGateSnapshotStore, createHttpContractGateSnapshotStore, resolveContractGateSnapshotPath, resolveContractGateSnapshotStore } from "./contractGateSnapshotStore.mjs";
|
|
5
|
+
export { createDefaultPlugins } from "./default.mjs";
|
|
6
|
+
export { faviconPlugin } from "./favicon.mjs";
|
|
7
|
+
export { logPlugin } from "./log.mjs";
|
|
8
|
+
export { injectConfigMiddlewarePlugin } from "./middlewares.mjs";
|
|
9
|
+
export { injectServerTiming, injectloggerPlugin } from "./monitors.mjs";
|
|
10
|
+
export { processedByPlugin } from "./processedBy.mjs";
|
|
11
|
+
export { getRenderHandler, injectRenderHandlerPlugin, renderPlugin } from "./render/index.mjs";
|
|
12
|
+
export { DEFAULT_RUNTIME_FALLBACK_SIGNAL_ENDPOINT, DEFAULT_RUNTIME_STATUS_ENDPOINT, TelemetryCanaryOrchestrator, TelemetryRegistry, TelemetryStartupHealthError, createOtlpTelemetryExporter, createRuntimeFallbackSignalRuntimeState, createRuntimeSignalError, createTelemetryAwareMetrics, createVictoriaMetricsTelemetryExporter, enforceRuntimeFallbackSignalAuthToken, enforceRuntimeFallbackSignalTrustPolicy, getRuntimeSignalErrorStatusCode, hasEnabledTelemetryExporters, injectTelemetryPlugin, normalizeRuntimeFallbackSignalAuthConfig, normalizeRuntimeFallbackTrustPolicy, parseRuntimeFallbackSignalPayloadFromRawBody, resolveRuntimeFallbackSignalEndpoint } from "./telemetry.mjs";
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
import "node:module";
|
|
2
|
+
import { getPathname } from "../utils/index.mjs";
|
|
3
|
+
const humanize = (times)=>{
|
|
4
|
+
const [delimiter, separator] = [
|
|
5
|
+
',',
|
|
6
|
+
'.'
|
|
7
|
+
];
|
|
8
|
+
const orderTimes = times.map((v)=>v.replace(/(\d)(?=(\d\d\d)+(?!\d))/g, `$1${delimiter}`));
|
|
9
|
+
return orderTimes.join(separator);
|
|
10
|
+
};
|
|
11
|
+
const time = (start)=>{
|
|
12
|
+
const delta = Date.now() - start;
|
|
13
|
+
return humanize([
|
|
14
|
+
delta < 1000 ? `${delta}ms` : `${Math.round(delta / 1000)}s`
|
|
15
|
+
]);
|
|
16
|
+
};
|
|
17
|
+
const colorStatus = (status)=>{
|
|
18
|
+
const out = {
|
|
19
|
+
7: `\x1b[35m${status}\x1b[0m`,
|
|
20
|
+
5: `\x1b[31m${status}\x1b[0m`,
|
|
21
|
+
4: `\x1b[33m${status}\x1b[0m`,
|
|
22
|
+
3: `\x1b[36m${status}\x1b[0m`,
|
|
23
|
+
2: `\x1b[32m${status}\x1b[0m`,
|
|
24
|
+
1: `\x1b[32m${status}\x1b[0m`,
|
|
25
|
+
0: `\x1b[33m${status}\x1b[0m`
|
|
26
|
+
};
|
|
27
|
+
const calculateStatus = Math.floor(status / 100);
|
|
28
|
+
return out[calculateStatus];
|
|
29
|
+
};
|
|
30
|
+
function log(fn, prefix, method, path, status = 0, elapsed) {
|
|
31
|
+
const out = "<--" === prefix ? ` ${prefix} ${method} ${path}` : ` ${prefix} ${method} ${path} ${colorStatus(status)} ${elapsed}`;
|
|
32
|
+
fn(out);
|
|
33
|
+
}
|
|
34
|
+
function logHandler() {
|
|
35
|
+
return async function(c, next) {
|
|
36
|
+
const { method } = c.req;
|
|
37
|
+
const monitors = c.get('monitors');
|
|
38
|
+
if (!monitors) return void await next();
|
|
39
|
+
const path = getPathname(c.req.raw);
|
|
40
|
+
const logFn = monitors.debug;
|
|
41
|
+
log(logFn, "<--", method, path);
|
|
42
|
+
const start = Date.now();
|
|
43
|
+
await next();
|
|
44
|
+
log(logFn, "-->", method, path, c?.env?.node?.res?.statusCode ?? c.res.status, time(start));
|
|
45
|
+
};
|
|
46
|
+
}
|
|
47
|
+
const logPlugin = ()=>({
|
|
48
|
+
name: '@modern-js/plugin-log',
|
|
49
|
+
setup (api) {
|
|
50
|
+
api.onPrepare(()=>{
|
|
51
|
+
const { middlewares } = api.getServerContext();
|
|
52
|
+
middlewares.push({
|
|
53
|
+
name: 'print_log',
|
|
54
|
+
handler: logHandler()
|
|
55
|
+
});
|
|
56
|
+
});
|
|
57
|
+
}
|
|
58
|
+
});
|
|
59
|
+
export { logPlugin };
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
import "node:module";
|
|
2
|
+
const REMOTE_ENTRY_REGEXP = /(^|\/)remoteEntry(?:\.[a-zA-Z0-9_-]+)?\.js$/;
|
|
3
|
+
function firstQueryValue(value) {
|
|
4
|
+
if (Array.isArray(value)) return value.find((item)=>'string' == typeof item && item.length > 0);
|
|
5
|
+
if ('string' == typeof value && value.length > 0) return value;
|
|
6
|
+
}
|
|
7
|
+
function getRequestPathname(url) {
|
|
8
|
+
try {
|
|
9
|
+
return new URL(url, 'http://modernjs.local').pathname;
|
|
10
|
+
} catch (_error) {
|
|
11
|
+
return url.split('?')[0] || '/';
|
|
12
|
+
}
|
|
13
|
+
}
|
|
14
|
+
function isMfManifestAsset(pathname) {
|
|
15
|
+
return pathname.endsWith('/mf-manifest.json') || pathname.endsWith('/mf-stats.json');
|
|
16
|
+
}
|
|
17
|
+
function isMfRemoteEntryAsset(pathname) {
|
|
18
|
+
return REMOTE_ENTRY_REGEXP.test(pathname);
|
|
19
|
+
}
|
|
20
|
+
function hasRemoteVersionPin(query = {}) {
|
|
21
|
+
return Boolean(firstQueryValue(query.mfv) || firstQueryValue(query.v) || firstQueryValue(query.version));
|
|
22
|
+
}
|
|
23
|
+
function resolveMfAssetCacheHeaders(url, query = {}) {
|
|
24
|
+
const pathname = getRequestPathname(url);
|
|
25
|
+
if (isMfManifestAsset(pathname)) return {
|
|
26
|
+
'cache-control': 'no-cache, no-store, must-revalidate',
|
|
27
|
+
pragma: 'no-cache',
|
|
28
|
+
expires: '0'
|
|
29
|
+
};
|
|
30
|
+
if (isMfRemoteEntryAsset(pathname)) return hasRemoteVersionPin(query) ? {
|
|
31
|
+
'cache-control': 'public, max-age=31536000, immutable'
|
|
32
|
+
} : {
|
|
33
|
+
'cache-control': 'public, max-age=0, must-revalidate'
|
|
34
|
+
};
|
|
35
|
+
}
|
|
36
|
+
export { getRequestPathname, isMfManifestAsset, isMfRemoteEntryAsset, resolveMfAssetCacheHeaders };
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import "node:module";
|
|
2
|
+
const injectConfigMiddlewarePlugin = (configMiddlewares = [], configRenderMiddlewares = [])=>({
|
|
3
|
+
name: '@modern-js/plugin-inject-config-middleware',
|
|
4
|
+
setup (api) {
|
|
5
|
+
api.onPrepare(()=>{
|
|
6
|
+
const { middlewares, renderMiddlewares } = api.getServerContext();
|
|
7
|
+
configMiddlewares.forEach((m)=>{
|
|
8
|
+
middlewares.push(m);
|
|
9
|
+
});
|
|
10
|
+
configRenderMiddlewares.forEach((m)=>{
|
|
11
|
+
renderMiddlewares.push(m);
|
|
12
|
+
});
|
|
13
|
+
});
|
|
14
|
+
}
|
|
15
|
+
});
|
|
16
|
+
export { injectConfigMiddlewarePlugin };
|