@modern-js/server-core 2.69.7 → 3.0.0-alpha.1
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/dist/cjs/adapters/node/helper/index.js +52 -39
- package/dist/cjs/adapters/node/helper/loadCache.js +52 -47
- package/dist/cjs/adapters/node/helper/loadConfig.js +69 -85
- package/dist/cjs/adapters/node/helper/loadEnv.js +60 -50
- package/dist/cjs/adapters/node/helper/loadPlugin.js +40 -32
- package/dist/cjs/adapters/node/helper/utils.js +33 -28
- package/dist/cjs/adapters/node/hono.js +87 -99
- package/dist/cjs/adapters/node/index.js +84 -60
- package/dist/cjs/adapters/node/node.js +164 -190
- package/dist/cjs/adapters/node/plugins/index.js +69 -23
- package/dist/cjs/adapters/node/plugins/nodeServer.js +39 -34
- package/dist/cjs/adapters/node/plugins/resource.js +185 -179
- package/dist/cjs/adapters/node/plugins/static.js +152 -164
- package/dist/cjs/constants.js +70 -57
- package/dist/cjs/context.js +37 -28
- package/dist/cjs/helper.js +40 -33
- package/dist/cjs/hono.js +46 -0
- package/dist/cjs/index.js +236 -57
- package/dist/cjs/plugins/compat/hooks.js +71 -83
- package/dist/cjs/plugins/compat/index.js +59 -60
- package/dist/cjs/plugins/default.js +52 -47
- package/dist/cjs/plugins/favicon.js +43 -39
- package/dist/cjs/plugins/index.js +73 -54
- package/dist/cjs/plugins/log.js +82 -87
- package/dist/cjs/plugins/middlewares.js +43 -37
- package/dist/cjs/plugins/monitors.js +178 -187
- package/dist/cjs/plugins/processedBy.js +45 -39
- package/dist/cjs/plugins/render/csrRscRender.js +70 -71
- package/dist/cjs/plugins/render/dataHandler.js +49 -45
- package/dist/cjs/plugins/render/index.js +151 -131
- package/dist/cjs/plugins/render/inject.js +84 -86
- package/dist/cjs/plugins/render/render.js +244 -271
- package/dist/cjs/plugins/render/renderRscHandler.js +68 -69
- package/dist/cjs/plugins/render/serverActionHandler.js +45 -44
- package/dist/cjs/plugins/render/ssrCache.js +173 -191
- package/dist/cjs/plugins/render/ssrRender.js +89 -94
- package/dist/cjs/plugins/render/utils.js +44 -36
- package/dist/cjs/plugins/route.js +60 -58
- package/dist/cjs/serverBase.js +164 -162
- package/dist/cjs/types/config/bff.js +17 -15
- package/dist/cjs/types/config/dev.js +17 -15
- package/dist/cjs/types/config/html.js +17 -15
- package/dist/cjs/types/config/index.js +90 -29
- package/dist/cjs/types/config/output.js +17 -15
- package/dist/cjs/types/config/security.js +17 -15
- package/dist/cjs/types/config/server.js +17 -15
- package/dist/cjs/types/config/share.js +17 -15
- package/dist/cjs/types/config/source.js +17 -15
- package/dist/cjs/types/config/tools.js +17 -15
- package/dist/cjs/types/index.js +76 -25
- package/dist/cjs/types/plugins/base.js +17 -15
- package/dist/cjs/types/plugins/index.js +55 -21
- package/dist/cjs/types/plugins/plugin.js +18 -0
- package/dist/cjs/types/render.js +17 -15
- package/dist/cjs/types/requestHandler.js +17 -15
- package/dist/cjs/types/server.js +17 -15
- package/dist/cjs/utils/entry.js +37 -32
- package/dist/cjs/utils/env.js +46 -40
- package/dist/cjs/utils/error.js +56 -50
- package/dist/cjs/utils/index.js +111 -33
- package/dist/cjs/utils/middlewareCollector.js +58 -49
- package/dist/cjs/utils/publicDir.js +92 -0
- package/dist/cjs/utils/request.js +73 -65
- package/dist/cjs/utils/serverConfig.js +39 -31
- package/dist/cjs/utils/storage.js +58 -68
- package/dist/cjs/utils/transformStream.js +57 -53
- package/dist/cjs/utils/warmup.js +34 -27
- package/dist/esm/adapters/node/helper/index.mjs +6 -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 +2 -0
- package/dist/esm/adapters/node/hono.mjs +53 -0
- package/dist/esm/adapters/node/index.mjs +5 -0
- package/dist/esm/adapters/node/node.mjs +129 -0
- package/dist/esm/adapters/node/plugins/nodeServer.mjs +9 -0
- package/dist/esm/adapters/node/plugins/resource.mjs +137 -0
- package/dist/esm/adapters/node/plugins/static.mjs +118 -0
- package/dist/esm/constants.mjs +28 -0
- package/dist/{esm-node/context.js → esm/context.mjs} +1 -4
- package/dist/esm/helper.mjs +11 -0
- package/dist/esm/hono.mjs +3 -0
- package/dist/esm/index.mjs +13 -0
- package/dist/esm/plugins/compat/hooks.mjs +40 -0
- package/dist/esm/plugins/compat/index.mjs +27 -0
- package/dist/esm/plugins/default.mjs +25 -0
- package/dist/esm/plugins/favicon.mjs +14 -0
- package/dist/{esm-node/plugins/index.js → esm/plugins/index.mjs} +2 -15
- package/dist/esm/plugins/log.mjs +58 -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 +43 -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 +226 -0
- package/dist/esm/plugins/render/renderRscHandler.mjs +41 -0
- package/dist/esm/plugins/render/serverActionHandler.mjs +18 -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/serverBase.mjs +136 -0
- package/dist/esm/types/plugins/index.mjs +1 -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-node/utils/index.js → esm/utils/index.mjs} +1 -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 +30 -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 +14 -0
- package/dist/esm-node/adapters/node/helper/loadConfig.mjs +31 -0
- package/dist/esm-node/adapters/node/helper/loadEnv.mjs +22 -0
- package/dist/esm-node/adapters/node/helper/loadPlugin.mjs +13 -0
- package/dist/esm-node/adapters/node/helper/utils.mjs +2 -0
- package/dist/esm-node/adapters/node/hono.mjs +53 -0
- package/dist/esm-node/adapters/node/index.mjs +5 -0
- package/dist/esm-node/adapters/node/node.mjs +129 -0
- package/dist/esm-node/adapters/node/plugins/index.mjs +3 -0
- package/dist/esm-node/adapters/node/plugins/nodeServer.mjs +9 -0
- package/dist/esm-node/adapters/node/plugins/resource.mjs +137 -0
- package/dist/esm-node/adapters/node/plugins/static.mjs +118 -0
- package/dist/esm-node/constants.mjs +28 -0
- package/dist/esm-node/context.mjs +3 -0
- package/dist/esm-node/helper.mjs +11 -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 +40 -0
- package/dist/esm-node/plugins/compat/index.mjs +27 -0
- package/dist/esm-node/plugins/default.mjs +25 -0
- package/dist/esm-node/plugins/favicon.mjs +14 -0
- package/dist/esm-node/plugins/index.mjs +9 -0
- package/dist/esm-node/plugins/log.mjs +58 -0
- package/dist/esm-node/plugins/middlewares.mjs +15 -0
- package/dist/esm-node/plugins/monitors.mjs +149 -0
- package/dist/esm-node/plugins/processedBy.mjs +16 -0
- package/dist/esm-node/plugins/render/csrRscRender.mjs +43 -0
- package/dist/esm-node/plugins/render/dataHandler.mjs +19 -0
- package/dist/esm-node/plugins/render/index.mjs +84 -0
- package/dist/esm-node/plugins/render/inject.mjs +55 -0
- package/dist/esm-node/plugins/render/render.mjs +226 -0
- package/dist/esm-node/plugins/render/renderRscHandler.mjs +41 -0
- package/dist/esm-node/plugins/render/serverActionHandler.mjs +18 -0
- package/dist/esm-node/plugins/render/ssrCache.mjs +158 -0
- package/dist/esm-node/plugins/render/ssrRender.mjs +62 -0
- package/dist/esm-node/plugins/render/utils.mjs +15 -0
- package/dist/esm-node/plugins/route.mjs +34 -0
- package/dist/esm-node/serverBase.mjs +136 -0
- package/dist/esm-node/types/config/index.mjs +6 -0
- package/dist/esm-node/types/index.mjs +4 -0
- package/dist/esm-node/types/plugins/index.mjs +1 -0
- package/dist/esm-node/utils/entry.mjs +3 -0
- package/dist/esm-node/utils/env.mjs +14 -0
- package/dist/esm-node/utils/error.mjs +53 -0
- package/dist/esm-node/utils/index.mjs +9 -0
- package/dist/esm-node/utils/middlewareCollector.mjs +26 -0
- package/dist/esm-node/utils/publicDir.mjs +33 -0
- package/dist/esm-node/utils/request.mjs +40 -0
- package/dist/esm-node/utils/serverConfig.mjs +9 -0
- package/dist/esm-node/utils/storage.mjs +30 -0
- package/dist/esm-node/utils/transformStream.mjs +28 -0
- package/dist/esm-node/utils/warmup.mjs +6 -0
- package/dist/types/adapters/node/helper/loadConfig.d.ts +1 -1
- package/dist/types/adapters/node/plugins/nodeServer.d.ts +2 -2
- package/dist/types/adapters/node/plugins/resource.d.ts +3 -3
- package/dist/types/adapters/node/plugins/static.d.ts +3 -2
- package/dist/types/hono.d.ts +3 -0
- package/dist/types/index.d.ts +1 -1
- package/dist/types/plugins/compat/hooks.d.ts +1 -1
- package/dist/types/plugins/default.d.ts +2 -2
- package/dist/types/plugins/favicon.d.ts +2 -2
- package/dist/types/plugins/log.d.ts +2 -2
- package/dist/types/plugins/middlewares.d.ts +2 -2
- package/dist/types/plugins/monitors.d.ts +4 -4
- package/dist/types/plugins/processedBy.d.ts +2 -2
- package/dist/types/plugins/render/dataHandler.d.ts +1 -1
- package/dist/types/plugins/render/index.d.ts +2 -2
- package/dist/types/plugins/render/inject.d.ts +2 -2
- package/dist/types/plugins/render/ssrRender.d.ts +3 -5
- package/dist/types/plugins/route.d.ts +2 -2
- package/dist/types/serverBase.d.ts +14 -20
- package/dist/types/types/config/bff.d.ts +0 -2
- package/dist/types/types/config/html.d.ts +0 -1
- package/dist/types/types/config/index.d.ts +0 -6
- package/dist/types/types/config/output.d.ts +2 -8
- package/dist/types/types/config/server.d.ts +1 -1
- package/dist/types/types/plugins/base.d.ts +5 -26
- package/dist/types/types/plugins/index.d.ts +1 -2
- package/dist/types/types/plugins/{new.d.ts → plugin.d.ts} +6 -4
- package/dist/types/types/render.d.ts +2 -5
- package/dist/types/types/requestHandler.d.ts +3 -7
- package/dist/types/types/server.d.ts +2 -1
- package/dist/types/utils/index.d.ts +1 -0
- package/dist/types/utils/publicDir.d.ts +40 -0
- package/dist/types/utils/transformStream.d.ts +1 -1
- package/package.json +25 -29
- package/rslib.config.mts +4 -0
- package/rstest.config.ts +7 -0
- package/dist/cjs/plugins/customServer/base.js +0 -186
- package/dist/cjs/plugins/customServer/context.js +0 -70
- package/dist/cjs/plugins/customServer/index.js +0 -210
- package/dist/cjs/plugins/customServer/routerApi.js +0 -44
- package/dist/cjs/plugins/customServer/template.js +0 -58
- package/dist/cjs/types/plugins/new.js +0 -16
- package/dist/cjs/types/plugins/old.js +0 -16
- package/dist/esm/adapters/node/helper/index.js +0 -13
- package/dist/esm/adapters/node/helper/loadCache.js +0 -44
- package/dist/esm/adapters/node/helper/loadConfig.js +0 -157
- package/dist/esm/adapters/node/helper/loadEnv.js +0 -73
- package/dist/esm/adapters/node/helper/loadPlugin.js +0 -59
- package/dist/esm/adapters/node/helper/utils.js +0 -7
- package/dist/esm/adapters/node/hono.js +0 -148
- package/dist/esm/adapters/node/index.js +0 -23
- package/dist/esm/adapters/node/node.js +0 -357
- package/dist/esm/adapters/node/plugins/index.js +0 -3
- package/dist/esm/adapters/node/plugins/nodeServer.js +0 -18
- package/dist/esm/adapters/node/plugins/resource.js +0 -542
- package/dist/esm/adapters/node/plugins/static.js +0 -245
- package/dist/esm/constants.js +0 -35
- package/dist/esm/context.js +0 -6
- package/dist/esm/helper.js +0 -14
- package/dist/esm/index.js +0 -22
- package/dist/esm/plugins/compat/hooks.js +0 -70
- package/dist/esm/plugins/compat/index.js +0 -45
- package/dist/esm/plugins/customServer/base.js +0 -218
- package/dist/esm/plugins/customServer/context.js +0 -66
- package/dist/esm/plugins/customServer/index.js +0 -369
- package/dist/esm/plugins/customServer/routerApi.js +0 -26
- package/dist/esm/plugins/customServer/template.js +0 -47
- package/dist/esm/plugins/default.js +0 -31
- package/dist/esm/plugins/favicon.js +0 -34
- package/dist/esm/plugins/index.js +0 -22
- package/dist/esm/plugins/log.js +0 -106
- package/dist/esm/plugins/middlewares.js +0 -22
- package/dist/esm/plugins/monitors.js +0 -287
- package/dist/esm/plugins/processedBy.js +0 -42
- package/dist/esm/plugins/render/csrRscRender.js +0 -88
- package/dist/esm/plugins/render/dataHandler.js +0 -48
- package/dist/esm/plugins/render/index.js +0 -221
- package/dist/esm/plugins/render/inject.js +0 -126
- package/dist/esm/plugins/render/render.js +0 -572
- package/dist/esm/plugins/render/renderRscHandler.js +0 -81
- package/dist/esm/plugins/render/serverActionHandler.js +0 -49
- package/dist/esm/plugins/render/ssrCache.js +0 -305
- package/dist/esm/plugins/render/ssrRender.js +0 -124
- package/dist/esm/plugins/render/utils.js +0 -17
- package/dist/esm/plugins/route.js +0 -93
- package/dist/esm/serverBase.js +0 -248
- package/dist/esm/types/config/index.js +0 -6
- package/dist/esm/types/index.js +0 -4
- package/dist/esm/types/plugins/index.js +0 -2
- package/dist/esm/types/requestHandler.js +0 -0
- package/dist/esm/types/server.js +0 -0
- package/dist/esm/utils/entry.js +0 -10
- package/dist/esm/utils/env.js +0 -20
- package/dist/esm/utils/error.js +0 -33
- package/dist/esm/utils/index.js +0 -8
- package/dist/esm/utils/middlewareCollector.js +0 -31
- package/dist/esm/utils/request.js +0 -51
- package/dist/esm/utils/serverConfig.js +0 -13
- package/dist/esm/utils/storage.js +0 -38
- package/dist/esm/utils/transformStream.js +0 -104
- package/dist/esm/utils/warmup.js +0 -9
- package/dist/esm-node/adapters/node/helper/index.js +0 -13
- package/dist/esm-node/adapters/node/helper/loadCache.js +0 -19
- package/dist/esm-node/adapters/node/helper/loadConfig.js +0 -59
- package/dist/esm-node/adapters/node/helper/loadEnv.js +0 -22
- package/dist/esm-node/adapters/node/helper/loadPlugin.js +0 -15
- package/dist/esm-node/adapters/node/helper/utils.js +0 -7
- package/dist/esm-node/adapters/node/hono.js +0 -79
- package/dist/esm-node/adapters/node/index.js +0 -23
- package/dist/esm-node/adapters/node/node.js +0 -169
- package/dist/esm-node/adapters/node/plugins/nodeServer.js +0 -14
- package/dist/esm-node/adapters/node/plugins/resource.js +0 -157
- package/dist/esm-node/adapters/node/plugins/static.js +0 -144
- package/dist/esm-node/constants.js +0 -35
- package/dist/esm-node/helper.js +0 -14
- package/dist/esm-node/index.js +0 -22
- package/dist/esm-node/plugins/compat/hooks.js +0 -66
- package/dist/esm-node/plugins/compat/index.js +0 -40
- package/dist/esm-node/plugins/customServer/base.js +0 -162
- package/dist/esm-node/plugins/customServer/context.js +0 -43
- package/dist/esm-node/plugins/customServer/index.js +0 -185
- package/dist/esm-node/plugins/customServer/routerApi.js +0 -20
- package/dist/esm-node/plugins/customServer/template.js +0 -34
- package/dist/esm-node/plugins/default.js +0 -30
- package/dist/esm-node/plugins/favicon.js +0 -20
- package/dist/esm-node/plugins/log.js +0 -73
- package/dist/esm-node/plugins/middlewares.js +0 -19
- package/dist/esm-node/plugins/monitors.js +0 -174
- package/dist/esm-node/plugins/processedBy.js +0 -20
- package/dist/esm-node/plugins/render/csrRscRender.js +0 -54
- package/dist/esm-node/plugins/render/dataHandler.js +0 -25
- package/dist/esm-node/plugins/render/index.js +0 -112
- package/dist/esm-node/plugins/render/inject.js +0 -69
- package/dist/esm-node/plugins/render/render.js +0 -253
- package/dist/esm-node/plugins/render/renderRscHandler.js +0 -52
- package/dist/esm-node/plugins/render/serverActionHandler.js +0 -27
- package/dist/esm-node/plugins/render/ssrCache.js +0 -190
- package/dist/esm-node/plugins/render/ssrRender.js +0 -77
- package/dist/esm-node/plugins/render/utils.js +0 -17
- package/dist/esm-node/plugins/route.js +0 -42
- package/dist/esm-node/serverBase.js +0 -146
- package/dist/esm-node/types/plugins/index.js +0 -2
- package/dist/esm-node/utils/entry.js +0 -10
- package/dist/esm-node/utils/env.js +0 -20
- package/dist/esm-node/utils/error.js +0 -61
- package/dist/esm-node/utils/middlewareCollector.js +0 -29
- package/dist/esm-node/utils/request.js +0 -50
- package/dist/esm-node/utils/serverConfig.js +0 -11
- package/dist/esm-node/utils/storage.js +0 -40
- package/dist/esm-node/utils/transformStream.js +0 -36
- package/dist/esm-node/utils/warmup.js +0 -9
- package/dist/types/plugins/customServer/base.d.ts +0 -7
- package/dist/types/plugins/customServer/context.d.ts +0 -8
- package/dist/types/plugins/customServer/index.d.ts +0 -11
- package/dist/types/plugins/customServer/routerApi.d.ts +0 -9
- package/dist/types/plugins/customServer/template.d.ts +0 -11
- package/dist/types/types/plugins/old.d.ts +0 -56
- /package/dist/{esm-node/adapters/node/plugins/index.js → esm/adapters/node/plugins/index.mjs} +0 -0
- /package/dist/{esm-node/types/server.js → esm/types/config/bff.mjs} +0 -0
- /package/dist/esm/types/config/{bff.js → dev.mjs} +0 -0
- /package/dist/esm/types/config/{dev.js → html.mjs} +0 -0
- /package/dist/{esm-node/types/config/index.js → esm/types/config/index.mjs} +0 -0
- /package/dist/esm/types/config/{html.js → output.mjs} +0 -0
- /package/dist/esm/types/config/{output.js → security.mjs} +0 -0
- /package/dist/esm/types/config/{security.js → server.mjs} +0 -0
- /package/dist/esm/types/config/{server.js → share.mjs} +0 -0
- /package/dist/esm/types/config/{share.js → source.mjs} +0 -0
- /package/dist/esm/types/config/{source.js → tools.mjs} +0 -0
- /package/dist/{esm-node/types/index.js → esm/types/index.mjs} +0 -0
- /package/dist/esm/types/{config/tools.js → plugins/base.mjs} +0 -0
- /package/dist/esm/types/plugins/{base.js → plugin.mjs} +0 -0
- /package/dist/esm/types/{plugins/new.js → render.mjs} +0 -0
- /package/dist/esm/types/{plugins/old.js → requestHandler.mjs} +0 -0
- /package/dist/esm/types/{render.js → server.mjs} +0 -0
- /package/dist/esm-node/types/config/{bff.js → bff.mjs} +0 -0
- /package/dist/esm-node/types/config/{dev.js → dev.mjs} +0 -0
- /package/dist/esm-node/types/config/{html.js → html.mjs} +0 -0
- /package/dist/esm-node/types/config/{output.js → output.mjs} +0 -0
- /package/dist/esm-node/types/config/{security.js → security.mjs} +0 -0
- /package/dist/esm-node/types/config/{server.js → server.mjs} +0 -0
- /package/dist/esm-node/types/config/{share.js → share.mjs} +0 -0
- /package/dist/esm-node/types/config/{source.js → source.mjs} +0 -0
- /package/dist/esm-node/types/config/{tools.js → tools.mjs} +0 -0
- /package/dist/esm-node/types/plugins/{base.js → base.mjs} +0 -0
- /package/dist/esm-node/types/plugins/{new.js → plugin.mjs} +0 -0
- /package/dist/esm-node/types/{plugins/old.js → render.mjs} +0 -0
- /package/dist/esm-node/types/{render.js → requestHandler.mjs} +0 -0
- /package/dist/esm-node/types/{requestHandler.js → server.mjs} +0 -0
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import { AsyncLocalStorage } from "async_hooks";
|
|
2
|
+
const createStorage = ()=>{
|
|
3
|
+
let storage;
|
|
4
|
+
if (void 0 !== AsyncLocalStorage) storage = new AsyncLocalStorage();
|
|
5
|
+
const run = (context, cb)=>{
|
|
6
|
+
if (!storage) throw new Error(`Unable to use async_hook, please confirm the node version >= 12.17
|
|
7
|
+
`);
|
|
8
|
+
return new Promise((resolve, reject)=>{
|
|
9
|
+
storage.run(context, ()=>{
|
|
10
|
+
try {
|
|
11
|
+
return resolve(cb());
|
|
12
|
+
} catch (error) {
|
|
13
|
+
return reject(error);
|
|
14
|
+
}
|
|
15
|
+
});
|
|
16
|
+
});
|
|
17
|
+
};
|
|
18
|
+
const useHonoContext = ()=>{
|
|
19
|
+
if (!storage) throw new Error(`Unable to use async_hook, please confirm the node version >= 12.17
|
|
20
|
+
`);
|
|
21
|
+
const context = storage.getStore();
|
|
22
|
+
if (!context) throw new Error("Can't call useContext out of server scope");
|
|
23
|
+
return context;
|
|
24
|
+
};
|
|
25
|
+
return {
|
|
26
|
+
run,
|
|
27
|
+
useHonoContext
|
|
28
|
+
};
|
|
29
|
+
};
|
|
30
|
+
export { createStorage };
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
function createTransformStream(fn) {
|
|
2
|
+
const decoder = new TextDecoder();
|
|
3
|
+
const encoder = new TextEncoder();
|
|
4
|
+
return new TransformStream({
|
|
5
|
+
async transform (chunk, controller) {
|
|
6
|
+
const content = decoder.decode(chunk);
|
|
7
|
+
const newContent = fn ? await fn(content) : content;
|
|
8
|
+
controller.enqueue(encoder.encode(newContent));
|
|
9
|
+
}
|
|
10
|
+
});
|
|
11
|
+
}
|
|
12
|
+
function transformResponse(response, transformCb) {
|
|
13
|
+
let readable = null;
|
|
14
|
+
if (response.body) {
|
|
15
|
+
const stream = createTransformStream(async (before)=>{
|
|
16
|
+
if (Array.isArray(transformCb)) return transformCb.reduce(async (before, cb)=>cb(await before), Promise.resolve(before));
|
|
17
|
+
return transformCb(before);
|
|
18
|
+
});
|
|
19
|
+
response.body.pipeThrough(stream);
|
|
20
|
+
readable = stream.readable;
|
|
21
|
+
}
|
|
22
|
+
return new Response(readable, {
|
|
23
|
+
status: response.status,
|
|
24
|
+
headers: response.headers,
|
|
25
|
+
statusText: response.statusText
|
|
26
|
+
});
|
|
27
|
+
}
|
|
28
|
+
export { createTransformStream, transformResponse };
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
import { loadServerEnv } from "./loadEnv.mjs";
|
|
2
|
+
import { loadServerPlugins } from "./loadPlugin.mjs";
|
|
3
|
+
import { loadServerCliConfig, loadServerRuntimeConfig } from "./loadConfig.mjs";
|
|
4
|
+
import { loadCacheConfig } from "./loadCache.mjs";
|
|
5
|
+
import { isResFinalized } from "./utils.mjs";
|
|
6
|
+
export { isResFinalized, loadCacheConfig, loadServerCliConfig, loadServerEnv, loadServerPlugins, loadServerRuntimeConfig };
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import path from "path";
|
|
2
|
+
import { SERVER_DIR, requireExistModule } from "@modern-js/utils";
|
|
3
|
+
const CACHE_FILENAME = 'cache';
|
|
4
|
+
async function loadCacheConfig(pwd) {
|
|
5
|
+
const serverCacheFilepath = path.resolve(pwd, SERVER_DIR, CACHE_FILENAME);
|
|
6
|
+
const mod = await requireExistModule(serverCacheFilepath, {
|
|
7
|
+
interop: false
|
|
8
|
+
});
|
|
9
|
+
if (mod?.cacheOption) return {
|
|
10
|
+
strategy: mod.cacheOption,
|
|
11
|
+
container: mod.customContainer
|
|
12
|
+
};
|
|
13
|
+
}
|
|
14
|
+
export { loadCacheConfig };
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import path_0 from "path";
|
|
2
|
+
import { OUTPUT_CONFIG_FILE, ensureAbsolutePath, fs as utils_fs, lodash, requireExistModule } from "@modern-js/utils";
|
|
3
|
+
import { parse } from "flatted";
|
|
4
|
+
async function loadServerConfigNew(serverConfigPath) {
|
|
5
|
+
const mod = await requireExistModule(serverConfigPath);
|
|
6
|
+
if (mod) return mod;
|
|
7
|
+
}
|
|
8
|
+
async function loadServerRuntimeConfig(serverConfigPath) {
|
|
9
|
+
const newServerConfig = await loadServerConfigNew(serverConfigPath);
|
|
10
|
+
return newServerConfig;
|
|
11
|
+
}
|
|
12
|
+
function loadServerCliConfig(pwd, defaultConfig = {}) {
|
|
13
|
+
const cliConfigPath = ensureAbsolutePath(pwd, path_0.join(defaultConfig.output?.distPath?.root || 'dist', OUTPUT_CONFIG_FILE));
|
|
14
|
+
let cliConfig = {
|
|
15
|
+
output: {},
|
|
16
|
+
source: {},
|
|
17
|
+
tools: {},
|
|
18
|
+
server: {},
|
|
19
|
+
security: {},
|
|
20
|
+
bff: {},
|
|
21
|
+
html: {},
|
|
22
|
+
dev: {}
|
|
23
|
+
};
|
|
24
|
+
try {
|
|
25
|
+
const content = utils_fs.readFileSync(cliConfigPath, 'utf-8');
|
|
26
|
+
cliConfig = parse(content);
|
|
27
|
+
} catch (_) {}
|
|
28
|
+
const mergedCliConfig = lodash.merge(defaultConfig, cliConfig);
|
|
29
|
+
return mergedCliConfig;
|
|
30
|
+
}
|
|
31
|
+
export { loadServerCliConfig, loadServerRuntimeConfig };
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import path from "path";
|
|
2
|
+
import { dotenv, dotenvExpand, fs } from "@modern-js/utils";
|
|
3
|
+
async function loadServerEnv(options) {
|
|
4
|
+
const { pwd } = options;
|
|
5
|
+
const serverEnv = process.env.MODERN_ENV;
|
|
6
|
+
const defaultEnvPath = path.resolve(pwd, ".env");
|
|
7
|
+
const serverEnvPath = path.resolve(pwd, `.env.${serverEnv}`);
|
|
8
|
+
if (await fs.pathExists(defaultEnvPath) && !(await fs.stat(defaultEnvPath)).isDirectory()) {
|
|
9
|
+
const envConfig = dotenv.config({
|
|
10
|
+
path: defaultEnvPath
|
|
11
|
+
});
|
|
12
|
+
dotenvExpand(envConfig);
|
|
13
|
+
}
|
|
14
|
+
if (await fs.pathExists(serverEnvPath) && !(await fs.stat(serverEnvPath)).isDirectory()) {
|
|
15
|
+
const envConfig = dotenv.config({
|
|
16
|
+
path: serverEnvPath
|
|
17
|
+
});
|
|
18
|
+
if (envConfig.parsed) Object.assign(process.env, envConfig.parsed);
|
|
19
|
+
dotenvExpand(envConfig);
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
export { loadServerEnv };
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { compatibleRequire, tryResolve } from "@modern-js/utils";
|
|
2
|
+
async function resolveServerPlugin(plugin, appDirectory) {
|
|
3
|
+
const { name, options } = plugin;
|
|
4
|
+
const pluginPath = tryResolve(name, appDirectory);
|
|
5
|
+
const module = await compatibleRequire(pluginPath);
|
|
6
|
+
const pluginInstance = module(options);
|
|
7
|
+
return pluginInstance;
|
|
8
|
+
}
|
|
9
|
+
async function loadServerPlugins(serverPlugins, appDirectory) {
|
|
10
|
+
const instances = await Promise.all(serverPlugins.map((plugin)=>resolveServerPlugin(plugin, appDirectory)));
|
|
11
|
+
return instances;
|
|
12
|
+
}
|
|
13
|
+
export { loadServerPlugins };
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
import { isResFinalized } from "./helper/index.mjs";
|
|
2
|
+
const httpCallBack2HonoMid = (handler)=>async (context, next)=>{
|
|
3
|
+
const { req, res } = context.env.node;
|
|
4
|
+
const onPipe = ()=>{
|
|
5
|
+
res._modernBodyPiped = true;
|
|
6
|
+
};
|
|
7
|
+
res.once('pipe', onPipe);
|
|
8
|
+
req.__honoRequest = context.req;
|
|
9
|
+
req.__templates = context.get('templates') || {};
|
|
10
|
+
req.__serverManifest = context.get('serverManifest') || {};
|
|
11
|
+
req.__rscServerManifest = context.get('rscServerManifest');
|
|
12
|
+
req.__rscClientManifest = context.get('rscClientManifest');
|
|
13
|
+
req.__rscSSRManifest = context.get('rscSSRManifest');
|
|
14
|
+
try {
|
|
15
|
+
await handler(req, res);
|
|
16
|
+
} finally{
|
|
17
|
+
delete req.__honoRequest;
|
|
18
|
+
delete req.__templates;
|
|
19
|
+
delete req.__serverManifest;
|
|
20
|
+
delete req.__rscServerManifest;
|
|
21
|
+
delete req.__rscClientManifest;
|
|
22
|
+
delete req.__rscSSRManifest;
|
|
23
|
+
res.removeListener('pipe', onPipe);
|
|
24
|
+
}
|
|
25
|
+
if (isResFinalized(res)) {
|
|
26
|
+
context.res;
|
|
27
|
+
context.finalized = true;
|
|
28
|
+
} else await next();
|
|
29
|
+
};
|
|
30
|
+
const noop = ()=>{};
|
|
31
|
+
const connectMid2HonoMid = (handler)=>async (context, next)=>new Promise((resolve, reject)=>{
|
|
32
|
+
const { req, res } = context.env.node;
|
|
33
|
+
if (handler.length < 3) resolve(handler(req, res, noop));
|
|
34
|
+
else handler(req, res, (err)=>{
|
|
35
|
+
if (err) reject(err);
|
|
36
|
+
else resolve(next());
|
|
37
|
+
});
|
|
38
|
+
});
|
|
39
|
+
const connectMockMid2HonoMid = (handler)=>async (context, next)=>new Promise((resolve, reject)=>{
|
|
40
|
+
const { req, res } = context.env.node;
|
|
41
|
+
if (handler.length < 3) {
|
|
42
|
+
res.once('finish', ()=>{
|
|
43
|
+
context.res;
|
|
44
|
+
context.finalized = true;
|
|
45
|
+
resolve();
|
|
46
|
+
});
|
|
47
|
+
handler(req, res, noop);
|
|
48
|
+
} else handler(req, res, (err)=>{
|
|
49
|
+
if (err) reject(err);
|
|
50
|
+
else resolve(next());
|
|
51
|
+
});
|
|
52
|
+
});
|
|
53
|
+
export { connectMid2HonoMid, connectMockMid2HonoMid, httpCallBack2HonoMid };
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
import { connectMid2HonoMid, connectMockMid2HonoMid, httpCallBack2HonoMid } from "./hono.mjs";
|
|
2
|
+
import { createNodeServer, createWebRequest, sendResponse } from "./node.mjs";
|
|
3
|
+
import { getHtmlTemplates, getServerManifest, injectNodeSeverPlugin, injectResourcePlugin, injectRscManifestPlugin, serverStaticPlugin } from "./plugins/index.mjs";
|
|
4
|
+
import { loadCacheConfig, loadServerCliConfig, loadServerEnv, loadServerPlugins, loadServerRuntimeConfig } from "./helper/index.mjs";
|
|
5
|
+
export { connectMid2HonoMid, connectMockMid2HonoMid, createNodeServer, createWebRequest, getHtmlTemplates, getServerManifest, httpCallBack2HonoMid, injectNodeSeverPlugin, injectResourcePlugin, injectRscManifestPlugin, loadCacheConfig, loadServerCliConfig, loadServerEnv, loadServerPlugins, loadServerRuntimeConfig, sendResponse, serverStaticPlugin };
|
|
@@ -0,0 +1,129 @@
|
|
|
1
|
+
import { ServerResponse } from "node:http";
|
|
2
|
+
import { Readable, Writable } from "node:stream";
|
|
3
|
+
import cloneable_readable from "cloneable-readable";
|
|
4
|
+
import { isResFinalized } from "./helper/index.mjs";
|
|
5
|
+
const createWebRequest = (req, res, body)=>{
|
|
6
|
+
const headerRecord = [];
|
|
7
|
+
for (const [key, value] of Object.entries(req.headers))if (!key.startsWith(':')) {
|
|
8
|
+
if (Array.isArray(value)) {
|
|
9
|
+
for (const item of value)if (void 0 !== item) headerRecord.push([
|
|
10
|
+
key,
|
|
11
|
+
item
|
|
12
|
+
]);
|
|
13
|
+
} else if (void 0 !== value) {
|
|
14
|
+
if ('string' == typeof value) headerRecord.push([
|
|
15
|
+
key,
|
|
16
|
+
value
|
|
17
|
+
]);
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
const { method } = req;
|
|
21
|
+
const controller = new AbortController();
|
|
22
|
+
const init = {
|
|
23
|
+
method,
|
|
24
|
+
headers: headerRecord,
|
|
25
|
+
signal: controller.signal
|
|
26
|
+
};
|
|
27
|
+
res.on('close', ()=>controller.abort('res closed'));
|
|
28
|
+
const url = `http://${req.headers.host}${req.url}`;
|
|
29
|
+
const needsRequestBody = body || !('GET' === method || 'HEAD' === method);
|
|
30
|
+
const cloneableReq = needsRequestBody ? cloneable_readable(req) : null;
|
|
31
|
+
if (needsRequestBody) {
|
|
32
|
+
if (body) init.body = body;
|
|
33
|
+
else {
|
|
34
|
+
const stream = cloneableReq.clone();
|
|
35
|
+
init.body = Readable.toWeb(stream);
|
|
36
|
+
}
|
|
37
|
+
init.duplex = 'half';
|
|
38
|
+
}
|
|
39
|
+
const originalRequest = new Request(url, init);
|
|
40
|
+
if (needsRequestBody) {
|
|
41
|
+
const interceptedMethods = [
|
|
42
|
+
'json',
|
|
43
|
+
'text',
|
|
44
|
+
'blob',
|
|
45
|
+
'arrayBuffer',
|
|
46
|
+
'formData'
|
|
47
|
+
];
|
|
48
|
+
return new Proxy(originalRequest, {
|
|
49
|
+
get (target, prop) {
|
|
50
|
+
if (interceptedMethods.includes(prop)) return (...args)=>{
|
|
51
|
+
cloneableReq.resume();
|
|
52
|
+
return target[prop].call(target, ...args);
|
|
53
|
+
};
|
|
54
|
+
const value = target[prop];
|
|
55
|
+
if ('body' === prop) {
|
|
56
|
+
cloneableReq.resume();
|
|
57
|
+
return value;
|
|
58
|
+
}
|
|
59
|
+
if ('function' == typeof value) return (...args)=>value.apply(target, args);
|
|
60
|
+
return value;
|
|
61
|
+
}
|
|
62
|
+
});
|
|
63
|
+
}
|
|
64
|
+
return originalRequest;
|
|
65
|
+
};
|
|
66
|
+
const sendResponse = async (response, res)=>{
|
|
67
|
+
res.statusMessage = response.statusText;
|
|
68
|
+
res.statusCode = response.status;
|
|
69
|
+
const cookies = [];
|
|
70
|
+
for (const [key, value] of response.headers.entries())if ('set-cookie' === key) cookies.push(value);
|
|
71
|
+
else res.setHeader(key, value);
|
|
72
|
+
if (cookies.length > 0) res.setHeader('set-cookie', cookies);
|
|
73
|
+
if (response.headers.get('Content-Type')?.match(/text\/event-stream/i) && res instanceof ServerResponse) res.flushHeaders();
|
|
74
|
+
if (response.body) {
|
|
75
|
+
const writable = Writable.toWeb(res);
|
|
76
|
+
await response.body.pipeTo(writable);
|
|
77
|
+
} else res.end();
|
|
78
|
+
};
|
|
79
|
+
const handleResponseError = (e, res)=>{
|
|
80
|
+
const err = e instanceof Error ? e : new Error('unknown error', {
|
|
81
|
+
cause: e
|
|
82
|
+
});
|
|
83
|
+
if ('ABORT_ERR' === err.code || 'ERR_STREAM_PREMATURE_CLOSE' === err.code) console.info('The user aborted a request.');
|
|
84
|
+
else {
|
|
85
|
+
console.error(e);
|
|
86
|
+
if (!res.headersSent) res.writeHead(500, {
|
|
87
|
+
'Content-Type': 'text/plain'
|
|
88
|
+
});
|
|
89
|
+
res.end(`Error: ${err.message}`);
|
|
90
|
+
res.destroy(err);
|
|
91
|
+
}
|
|
92
|
+
};
|
|
93
|
+
const getRequestListener = (handler)=>async (req, res)=>{
|
|
94
|
+
try {
|
|
95
|
+
const request = createWebRequest(req, res);
|
|
96
|
+
const response = await handler(request, {
|
|
97
|
+
node: {
|
|
98
|
+
req,
|
|
99
|
+
res
|
|
100
|
+
}
|
|
101
|
+
});
|
|
102
|
+
if (!response.res && !isResFinalized(res)) await sendResponse(response, res);
|
|
103
|
+
} catch (error) {
|
|
104
|
+
return handleResponseError(error, res);
|
|
105
|
+
}
|
|
106
|
+
};
|
|
107
|
+
const createNodeServer = async (requestHandler, httpsOptions, http2)=>{
|
|
108
|
+
const requestListener = getRequestListener(requestHandler);
|
|
109
|
+
let nodeServer;
|
|
110
|
+
if (httpsOptions) if (http2) {
|
|
111
|
+
const { createSecureServer } = await import("node:http2");
|
|
112
|
+
nodeServer = createSecureServer({
|
|
113
|
+
allowHTTP1: true,
|
|
114
|
+
maxSessionMemory: 1024,
|
|
115
|
+
...httpsOptions
|
|
116
|
+
}, (req, res)=>requestListener(req, res));
|
|
117
|
+
} else {
|
|
118
|
+
const { createServer } = await import("node:https");
|
|
119
|
+
nodeServer = createServer(httpsOptions, requestListener);
|
|
120
|
+
}
|
|
121
|
+
else {
|
|
122
|
+
const { createServer } = await import("node:http");
|
|
123
|
+
nodeServer = createServer(requestListener);
|
|
124
|
+
}
|
|
125
|
+
nodeServer.getRequestListener = ()=>requestListener;
|
|
126
|
+
nodeServer.getRequestHandler = ()=>requestHandler;
|
|
127
|
+
return nodeServer;
|
|
128
|
+
};
|
|
129
|
+
export { createNodeServer, createWebRequest, sendResponse };
|
|
@@ -0,0 +1,137 @@
|
|
|
1
|
+
import path from "path";
|
|
2
|
+
import { fileReader } from "@modern-js/runtime-utils/fileReader";
|
|
3
|
+
import { LOADABLE_STATS_FILE, MAIN_ENTRY_NAME, NESTED_ROUTE_SPEC_FILE, ROUTE_MANIFEST_FILE, SERVER_BUNDLE_DIRECTORY, compatibleRequire, fs, isProd } from "@modern-js/utils";
|
|
4
|
+
import { uniqueKeyByRoute } from "../../../utils/index.mjs";
|
|
5
|
+
async function getHtmlTemplates(pwd, routes) {
|
|
6
|
+
const htmlRoutes = routes.filter((route)=>route.entryName);
|
|
7
|
+
const htmls = await Promise.all(htmlRoutes.map(async (route)=>{
|
|
8
|
+
let html;
|
|
9
|
+
try {
|
|
10
|
+
const htmlPath = path.join(pwd, route.entryPath);
|
|
11
|
+
html = (await fileReader.readFile(htmlPath, 'utf-8'))?.toString();
|
|
12
|
+
} catch (e) {}
|
|
13
|
+
return [
|
|
14
|
+
uniqueKeyByRoute(route),
|
|
15
|
+
html
|
|
16
|
+
];
|
|
17
|
+
}) || []);
|
|
18
|
+
const templates = Object.fromEntries(htmls);
|
|
19
|
+
return templates;
|
|
20
|
+
}
|
|
21
|
+
function injectTemplates(pwd, routes, htmlTemplatePromise) {
|
|
22
|
+
return async (c, next)=>{
|
|
23
|
+
if (routes && !c.get('templates')) {
|
|
24
|
+
const templates = await (htmlTemplatePromise || getHtmlTemplates(pwd, routes));
|
|
25
|
+
c.set('templates', templates);
|
|
26
|
+
}
|
|
27
|
+
await next();
|
|
28
|
+
};
|
|
29
|
+
}
|
|
30
|
+
const loadBundle = async (filepath, logger)=>{
|
|
31
|
+
if (!await fs.pathExists(filepath)) return;
|
|
32
|
+
try {
|
|
33
|
+
const module = await compatibleRequire(filepath, false);
|
|
34
|
+
return module;
|
|
35
|
+
} catch (e) {
|
|
36
|
+
logger.error(`Load ${filepath} bundle failed, error = %s`, e instanceof Error ? e.stack || e.message : e);
|
|
37
|
+
return;
|
|
38
|
+
}
|
|
39
|
+
};
|
|
40
|
+
async function getServerManifest(pwd, routes, logger) {
|
|
41
|
+
const loaderBundles = {};
|
|
42
|
+
const renderBundles = {};
|
|
43
|
+
await Promise.all(routes.filter((route)=>Boolean(route.bundle)).map(async (route)=>{
|
|
44
|
+
const entryName = route.entryName || MAIN_ENTRY_NAME;
|
|
45
|
+
const renderBundlePath = path.join(pwd, route.bundle || '');
|
|
46
|
+
const loaderBundlePath = path.join(pwd, SERVER_BUNDLE_DIRECTORY, `${entryName}-server-loaders.js`);
|
|
47
|
+
const renderBundle = await loadBundle(renderBundlePath, logger);
|
|
48
|
+
const loaderBundle = await loadBundle(loaderBundlePath, logger);
|
|
49
|
+
renderBundle && (renderBundles[entryName] = renderBundle);
|
|
50
|
+
loaderBundle && (loaderBundles[entryName] = loaderBundle?.loadModules ? await loaderBundle?.loadModules() : loaderBundle);
|
|
51
|
+
}));
|
|
52
|
+
const loadableUri = path.join(pwd, LOADABLE_STATS_FILE);
|
|
53
|
+
const loadableStats = await compatibleRequire(loadableUri).catch((_)=>({}));
|
|
54
|
+
const routesManifestUri = path.join(pwd, ROUTE_MANIFEST_FILE);
|
|
55
|
+
const routeManifest = await compatibleRequire(routesManifestUri).catch((_)=>({}));
|
|
56
|
+
const nestedRoutesJsonPath = path.join(pwd, NESTED_ROUTE_SPEC_FILE);
|
|
57
|
+
const nestedRoutesJson = await compatibleRequire(nestedRoutesJsonPath).catch((_)=>({}));
|
|
58
|
+
return {
|
|
59
|
+
loaderBundles,
|
|
60
|
+
renderBundles,
|
|
61
|
+
loadableStats,
|
|
62
|
+
routeManifest,
|
|
63
|
+
nestedRoutesJson
|
|
64
|
+
};
|
|
65
|
+
}
|
|
66
|
+
function injectServerManifest(pwd, routes, manifestPromise) {
|
|
67
|
+
return async (c, next)=>{
|
|
68
|
+
if (routes && !c.get('serverManifest')) {
|
|
69
|
+
const logger = c.get('logger');
|
|
70
|
+
const serverManifest = await (manifestPromise || getServerManifest(pwd, routes, logger));
|
|
71
|
+
c.set('serverManifest', serverManifest);
|
|
72
|
+
}
|
|
73
|
+
await next();
|
|
74
|
+
};
|
|
75
|
+
}
|
|
76
|
+
async function getRscServerManifest(pwd) {
|
|
77
|
+
const rscServerManifest = await compatibleRequire(path.join(pwd, 'bundles', 'react-server-manifest.json')).catch((_)=>void 0);
|
|
78
|
+
return rscServerManifest;
|
|
79
|
+
}
|
|
80
|
+
async function getClientManifest(pwd) {
|
|
81
|
+
const rscClientManifest = await compatibleRequire(path.join(pwd, 'react-client-manifest.json')).catch((_)=>void 0);
|
|
82
|
+
return rscClientManifest;
|
|
83
|
+
}
|
|
84
|
+
async function getRscSSRManifest(pwd) {
|
|
85
|
+
const rscSSRManifest = await compatibleRequire(path.join(pwd, 'react-ssr-manifest.json')).catch((_)=>void 0);
|
|
86
|
+
return rscSSRManifest;
|
|
87
|
+
}
|
|
88
|
+
const injectRscManifestPlugin = (enableRsc)=>({
|
|
89
|
+
name: '@modern-js/plugin-inject-rsc-manifest',
|
|
90
|
+
setup (api) {
|
|
91
|
+
api.onPrepare(()=>{
|
|
92
|
+
const { middlewares, distDirectory: pwd } = api.getServerContext();
|
|
93
|
+
if (!enableRsc) return;
|
|
94
|
+
middlewares.push({
|
|
95
|
+
name: 'inject-rsc-manifest',
|
|
96
|
+
handler: async (c, next)=>{
|
|
97
|
+
if (!c.get('rscServerManifest')) {
|
|
98
|
+
const rscServerManifest = await getRscServerManifest(pwd);
|
|
99
|
+
c.set('rscServerManifest', rscServerManifest);
|
|
100
|
+
}
|
|
101
|
+
if (!c.get('rscClientManifest')) {
|
|
102
|
+
const rscClientManifest = await getClientManifest(pwd);
|
|
103
|
+
c.set('rscClientManifest', rscClientManifest);
|
|
104
|
+
}
|
|
105
|
+
if (!c.get('rscSSRManifest')) {
|
|
106
|
+
const rscSSRManifest = await getRscSSRManifest(pwd);
|
|
107
|
+
c.set('rscSSRManifest', rscSSRManifest);
|
|
108
|
+
}
|
|
109
|
+
await next();
|
|
110
|
+
}
|
|
111
|
+
});
|
|
112
|
+
});
|
|
113
|
+
}
|
|
114
|
+
});
|
|
115
|
+
const injectResourcePlugin = ()=>({
|
|
116
|
+
name: '@modern-js/plugin-inject-resource',
|
|
117
|
+
setup (api) {
|
|
118
|
+
api.onPrepare(()=>{
|
|
119
|
+
const { middlewares, routes, distDirectory: pwd } = api.getServerContext();
|
|
120
|
+
let htmlTemplatePromise;
|
|
121
|
+
let manifestPromise;
|
|
122
|
+
if (isProd()) {
|
|
123
|
+
manifestPromise = getServerManifest(pwd, routes || [], console);
|
|
124
|
+
htmlTemplatePromise = getHtmlTemplates(pwd, routes || []);
|
|
125
|
+
}
|
|
126
|
+
middlewares.push({
|
|
127
|
+
name: 'inject-server-manifest',
|
|
128
|
+
handler: injectServerManifest(pwd, routes, manifestPromise)
|
|
129
|
+
});
|
|
130
|
+
middlewares.push({
|
|
131
|
+
name: 'inject-html',
|
|
132
|
+
handler: injectTemplates(pwd, routes, htmlTemplatePromise)
|
|
133
|
+
});
|
|
134
|
+
});
|
|
135
|
+
}
|
|
136
|
+
});
|
|
137
|
+
export { getClientManifest, getHtmlTemplates, getRscSSRManifest, getRscServerManifest, getServerManifest, injectResourcePlugin, injectRscManifestPlugin, injectServerManifest, injectTemplates };
|
|
@@ -0,0 +1,118 @@
|
|
|
1
|
+
import path from "path";
|
|
2
|
+
import { fileReader } from "@modern-js/runtime-utils/fileReader";
|
|
3
|
+
import { fs } from "@modern-js/utils";
|
|
4
|
+
import { getMimeType } from "hono/utils/mime";
|
|
5
|
+
import { sortRoutes } from "../../../utils/index.mjs";
|
|
6
|
+
import { getPublicDirPatterns } from "../../../utils/publicDir.mjs";
|
|
7
|
+
const serverStaticPlugin = ()=>({
|
|
8
|
+
name: '@modern-js/plugin-server-static',
|
|
9
|
+
setup (api) {
|
|
10
|
+
api.onPrepare(()=>{
|
|
11
|
+
const { middlewares, distDirectory: pwd, routes } = api.getServerContext();
|
|
12
|
+
const config = api.getServerConfig();
|
|
13
|
+
const serverStaticMiddleware = createStaticMiddleware({
|
|
14
|
+
pwd: pwd,
|
|
15
|
+
routes,
|
|
16
|
+
output: config.output || {},
|
|
17
|
+
html: config.html || {},
|
|
18
|
+
server: config.server || {}
|
|
19
|
+
});
|
|
20
|
+
middlewares.push({
|
|
21
|
+
name: 'server-static',
|
|
22
|
+
handler: serverStaticMiddleware
|
|
23
|
+
});
|
|
24
|
+
});
|
|
25
|
+
}
|
|
26
|
+
});
|
|
27
|
+
function createPublicMiddleware({ pwd, routes }) {
|
|
28
|
+
return async (c, next)=>{
|
|
29
|
+
const route = matchPublicRoute(c.req, routes);
|
|
30
|
+
if (route) {
|
|
31
|
+
const { entryPath } = route;
|
|
32
|
+
const filename = path.join(pwd, entryPath);
|
|
33
|
+
const data = await fileReader.readFile(filename, 'buffer');
|
|
34
|
+
const mimeType = getMimeType(filename);
|
|
35
|
+
if (null !== data) {
|
|
36
|
+
const body = new Uint8Array(data.buffer, data.byteOffset, data.byteLength);
|
|
37
|
+
if (mimeType) c.header('Content-Type', mimeType);
|
|
38
|
+
Object.entries(route.responseHeaders || {}).forEach(([k, v])=>{
|
|
39
|
+
c.header(k, v);
|
|
40
|
+
});
|
|
41
|
+
return c.body(body, 200);
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
return await next();
|
|
45
|
+
};
|
|
46
|
+
}
|
|
47
|
+
function matchPublicRoute(req, routes) {
|
|
48
|
+
for (const route of routes.sort(sortRoutes))if (!route.isSSR && route.entryPath.startsWith('public') && req.path.startsWith(route.urlPath)) return route;
|
|
49
|
+
}
|
|
50
|
+
const extractPathname = (url)=>{
|
|
51
|
+
try {
|
|
52
|
+
if (url.includes('://')) return new URL(url).pathname || '/';
|
|
53
|
+
if (url.startsWith('//')) return new URL(`http:${url}`).pathname || '/';
|
|
54
|
+
return url;
|
|
55
|
+
} catch (e) {
|
|
56
|
+
return url;
|
|
57
|
+
}
|
|
58
|
+
};
|
|
59
|
+
function createStaticMiddleware(options) {
|
|
60
|
+
const { pwd, routes } = options;
|
|
61
|
+
const prefix = options.output.assetPrefix || '/';
|
|
62
|
+
const pathPrefix = extractPathname(prefix);
|
|
63
|
+
const { distPath: { css: cssPath, js: jsPath, media: mediaPath } = {} } = options.output;
|
|
64
|
+
const { favicon } = options.html;
|
|
65
|
+
const { publicDir } = options.server;
|
|
66
|
+
const favicons = prepareFavicons(favicon);
|
|
67
|
+
const staticFiles = [
|
|
68
|
+
cssPath,
|
|
69
|
+
jsPath,
|
|
70
|
+
mediaPath
|
|
71
|
+
].filter((v)=>Boolean(v));
|
|
72
|
+
const publicDirPatterns = getPublicDirPatterns(publicDir);
|
|
73
|
+
const staticReg = [
|
|
74
|
+
'static/',
|
|
75
|
+
'upload/',
|
|
76
|
+
...staticFiles,
|
|
77
|
+
...publicDirPatterns
|
|
78
|
+
];
|
|
79
|
+
const iconReg = [
|
|
80
|
+
'favicon.ico',
|
|
81
|
+
'icon.png',
|
|
82
|
+
...favicons
|
|
83
|
+
];
|
|
84
|
+
const regPrefix = pathPrefix.endsWith('/') ? pathPrefix : `${pathPrefix}/`;
|
|
85
|
+
const staticPathRegExp = new RegExp(`^${regPrefix}(${[
|
|
86
|
+
...staticReg,
|
|
87
|
+
...iconReg
|
|
88
|
+
].join('|')})`);
|
|
89
|
+
return async (c, next)=>{
|
|
90
|
+
const pageRoute = c.get('route');
|
|
91
|
+
const pathname = c.req.path;
|
|
92
|
+
if (pageRoute && '' === path.extname(pathname)) return next();
|
|
93
|
+
const hit = staticPathRegExp.test(pathname);
|
|
94
|
+
if (!hit) return createPublicMiddleware({
|
|
95
|
+
pwd,
|
|
96
|
+
routes: routes || []
|
|
97
|
+
})(c, next);
|
|
98
|
+
{
|
|
99
|
+
const filepath = path.join(pwd, pathname.replace(pathPrefix, ()=>''));
|
|
100
|
+
if (!await fs.pathExists(filepath)) return next();
|
|
101
|
+
const mimeType = getMimeType(filepath);
|
|
102
|
+
if (mimeType) c.header('Content-Type', mimeType);
|
|
103
|
+
const stat = await fs.lstat(filepath);
|
|
104
|
+
const { size } = stat;
|
|
105
|
+
const chunk = await fileReader.readFileFromSystem(filepath, 'buffer');
|
|
106
|
+
c.header('Content-Length', String(size));
|
|
107
|
+
if (null === chunk) return next();
|
|
108
|
+
const body = new Uint8Array(chunk.buffer, chunk.byteOffset, chunk.byteLength);
|
|
109
|
+
return c.body(body, 200);
|
|
110
|
+
}
|
|
111
|
+
};
|
|
112
|
+
}
|
|
113
|
+
const prepareFavicons = (favicon)=>{
|
|
114
|
+
const faviconNames = [];
|
|
115
|
+
if (favicon && 'string' == typeof favicon) faviconNames.push(favicon.substring(favicon.lastIndexOf('/') + 1));
|
|
116
|
+
return faviconNames;
|
|
117
|
+
};
|
|
118
|
+
export { createPublicMiddleware, createStaticMiddleware, serverStaticPlugin };
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
const AGGRED_DIR = {
|
|
2
|
+
mock: 'config/mock',
|
|
3
|
+
server: 'server',
|
|
4
|
+
api: 'api',
|
|
5
|
+
shared: 'shared',
|
|
6
|
+
lambda: 'lambda'
|
|
7
|
+
};
|
|
8
|
+
const REPLACE_REG = {
|
|
9
|
+
before: {
|
|
10
|
+
head: '<head\\b[^>]*>',
|
|
11
|
+
body: '<body\\b[^>]*>'
|
|
12
|
+
},
|
|
13
|
+
after: {
|
|
14
|
+
head: '</head>',
|
|
15
|
+
body: '</body>'
|
|
16
|
+
}
|
|
17
|
+
};
|
|
18
|
+
var constants_ServerTimings = /*#__PURE__*/ function(ServerTimings) {
|
|
19
|
+
ServerTimings["SERVER_HANDLE_REQUEST"] = "server-handle-request";
|
|
20
|
+
ServerTimings["SERVER_MIDDLEWARE"] = "server-middleware";
|
|
21
|
+
ServerTimings["SERVER_HOOK_AFTER_RENDER"] = "server-hook-after-render";
|
|
22
|
+
ServerTimings["SERVER_HOOK_AFTER_MATCH"] = "server-hook-after-match";
|
|
23
|
+
return ServerTimings;
|
|
24
|
+
}({});
|
|
25
|
+
const SERVER_TIMING = 'Server-Timing';
|
|
26
|
+
const X_RENDER_CACHE = 'x-render-cache';
|
|
27
|
+
const X_MODERNJS_RENDER = 'x-modernjs-render';
|
|
28
|
+
export { AGGRED_DIR, REPLACE_REG, SERVER_TIMING, constants_ServerTimings as ServerTimings, X_MODERNJS_RENDER, X_RENDER_CACHE };
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
const LOADER_CONTEXT = 'loaderContext';
|
|
2
|
+
function getLoaderCtx(c) {
|
|
3
|
+
const loaderContext = c.get(LOADER_CONTEXT);
|
|
4
|
+
if (loaderContext) return loaderContext;
|
|
5
|
+
{
|
|
6
|
+
const loaderContext = new Map();
|
|
7
|
+
c.set(LOADER_CONTEXT, loaderContext);
|
|
8
|
+
return loaderContext;
|
|
9
|
+
}
|
|
10
|
+
}
|
|
11
|
+
export { getLoaderCtx };
|