@bleedingdev/modern-js-server-core 3.5.0-ultramodern.36 → 3.5.0-ultramodern.38
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/plugins/static.js +12 -65
- package/dist/cjs/adapters/node/plugins/staticModuleFederation.js +2 -8
- package/dist/cjs/adapters/node/plugins/staticPrecompressed.js +0 -3
- package/dist/cjs/adapters/node/plugins/staticServing.js +131 -0
- package/dist/esm/adapters/node/plugins/static.mjs +12 -65
- package/dist/esm/adapters/node/plugins/staticModuleFederation.mjs +1 -1
- package/dist/esm/adapters/node/plugins/staticPrecompressed.mjs +1 -1
- package/dist/esm/adapters/node/plugins/staticServing.mjs +80 -0
- package/dist/esm/plugins/render/render.mjs +4 -4
- package/dist/esm-node/adapters/node/plugins/static.mjs +12 -65
- package/dist/esm-node/adapters/node/plugins/staticModuleFederation.mjs +1 -1
- package/dist/esm-node/adapters/node/plugins/staticPrecompressed.mjs +1 -1
- package/dist/esm-node/adapters/node/plugins/staticServing.mjs +81 -0
- package/dist/esm-node/plugins/render/render.mjs +2 -2
- package/dist/types/adapters/node/plugins/staticModuleFederation.d.ts +0 -2
- package/dist/types/adapters/node/plugins/staticPrecompressed.d.ts +1 -2
- package/dist/types/adapters/node/plugins/staticServing.d.ts +19 -0
- package/package.json +11 -11
|
@@ -41,15 +41,11 @@ __webpack_require__.d(__webpack_exports__, {
|
|
|
41
41
|
createStaticMiddleware: ()=>createStaticMiddleware,
|
|
42
42
|
serverStaticPlugin: ()=>serverStaticPlugin
|
|
43
43
|
});
|
|
44
|
-
const fileReader_namespaceObject = require("@modern-js/runtime-utils/fileReader");
|
|
45
|
-
const utils_namespaceObject = require("@modern-js/utils");
|
|
46
|
-
const mime_namespaceObject = require("hono/utils/mime");
|
|
47
44
|
const external_path_namespaceObject = require("path");
|
|
48
45
|
var external_path_default = /*#__PURE__*/ __webpack_require__.n(external_path_namespaceObject);
|
|
49
46
|
const index_js_namespaceObject = require("../../../utils/index.js");
|
|
50
47
|
const publicDir_js_namespaceObject = require("../../../utils/publicDir.js");
|
|
51
|
-
const
|
|
52
|
-
const external_staticPrecompressed_js_namespaceObject = require("./staticPrecompressed.js");
|
|
48
|
+
const external_staticServing_js_namespaceObject = require("./staticServing.js");
|
|
53
49
|
const serverStaticPlugin = ()=>({
|
|
54
50
|
name: '@modern-js/plugin-server-static',
|
|
55
51
|
setup (api) {
|
|
@@ -74,22 +70,8 @@ function createPublicMiddleware({ pwd, routes }) {
|
|
|
74
70
|
return async (c, next)=>{
|
|
75
71
|
const route = matchPublicRoute(c.req, routes);
|
|
76
72
|
if (route) {
|
|
77
|
-
const
|
|
78
|
-
|
|
79
|
-
const preCompressedAsset = await (0, external_staticPrecompressed_js_namespaceObject.resolvePreCompressedAsset)(c, originFilename);
|
|
80
|
-
const filename = preCompressedAsset.selected?.filepath ?? originFilename;
|
|
81
|
-
const data = await fileReader_namespaceObject.fileReader.readFile(filename, 'buffer');
|
|
82
|
-
const mimeType = (0, mime_namespaceObject.getMimeType)(originFilename);
|
|
83
|
-
if (null !== data) {
|
|
84
|
-
const body = new Uint8Array(data.buffer, data.byteOffset, data.byteLength);
|
|
85
|
-
if (mimeType) c.header('Content-Type', mimeType);
|
|
86
|
-
Object.entries(route.responseHeaders || {}).forEach(([k, v])=>{
|
|
87
|
-
c.header(k, v);
|
|
88
|
-
});
|
|
89
|
-
(0, external_staticPrecompressed_js_namespaceObject.applyPreCompressedAssetHeaders)(c, preCompressedAsset);
|
|
90
|
-
c.header('Content-Length', String(data.byteLength));
|
|
91
|
-
return c.body(body, 200);
|
|
92
|
-
}
|
|
73
|
+
const response = await (0, external_staticServing_js_namespaceObject.servePreCompressedPublicRouteAsset)(c, pwd, route);
|
|
74
|
+
if (null !== response) return response;
|
|
93
75
|
}
|
|
94
76
|
return await next();
|
|
95
77
|
};
|
|
@@ -97,10 +79,6 @@ function createPublicMiddleware({ pwd, routes }) {
|
|
|
97
79
|
function matchPublicRoute(req, routes) {
|
|
98
80
|
for (const route of routes.sort(index_js_namespaceObject.sortRoutes))if (!route.isSSR && route.entryPath.startsWith('public') && req.path.startsWith(route.urlPath)) return route;
|
|
99
81
|
}
|
|
100
|
-
const isPathInside = (target, root)=>{
|
|
101
|
-
const relative = external_path_default().relative(external_path_default().resolve(root), external_path_default().resolve(target));
|
|
102
|
-
return '' === relative || !relative.startsWith(`..${external_path_default().sep}`) && '..' !== relative && !external_path_default().isAbsolute(relative);
|
|
103
|
-
};
|
|
104
82
|
const extractPathname = (url)=>{
|
|
105
83
|
try {
|
|
106
84
|
if (url.includes('://')) return new URL(url).pathname || '/';
|
|
@@ -144,55 +122,24 @@ function createStaticMiddleware(options) {
|
|
|
144
122
|
pwd,
|
|
145
123
|
routes: routes || []
|
|
146
124
|
});
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
};
|
|
152
|
-
const serveFile = async (c, filepath, moduleFederationAsset = false, moduleFederationRemoteEntry = false, requestPath = '')=>{
|
|
153
|
-
if (moduleFederationAsset) (0, external_staticModuleFederation_js_namespaceObject.applyModuleFederationAssetHeaders)(c);
|
|
154
|
-
const mimeType = (0, mime_namespaceObject.getMimeType)(filepath);
|
|
155
|
-
if (mimeType) c.header('Content-Type', mimeType);
|
|
156
|
-
const shouldPatchManifest = moduleFederationAsset && (0, external_staticModuleFederation_js_namespaceObject.isModuleFederationManifestRequest)(requestPath);
|
|
157
|
-
const shouldPatchRemoteEntry = moduleFederationRemoteEntry;
|
|
158
|
-
const canUsePreCompressed = !shouldPatchManifest && !shouldPatchRemoteEntry;
|
|
159
|
-
const preCompressedAsset = canUsePreCompressed ? await (0, external_staticPrecompressed_js_namespaceObject.resolvePreCompressedAsset)(c, filepath) : {
|
|
160
|
-
selected: null,
|
|
161
|
-
hasVariant: false
|
|
162
|
-
};
|
|
163
|
-
const targetFilepath = preCompressedAsset.selected?.filepath ?? filepath;
|
|
164
|
-
const chunk = await fileReader_namespaceObject.fileReader.readFileFromSystem(targetFilepath, 'buffer');
|
|
165
|
-
if (null === chunk) return null;
|
|
166
|
-
const responseChunk = shouldPatchManifest ? (0, external_staticModuleFederation_js_namespaceObject.patchModuleFederationManifestPublicPath)(c, chunk, pathPrefix) : shouldPatchRemoteEntry ? (0, external_staticModuleFederation_js_namespaceObject.patchModuleFederationRemoteEntryPublicPath)(c, chunk, pathPrefix) : chunk;
|
|
167
|
-
(0, external_staticPrecompressed_js_namespaceObject.applyPreCompressedAssetHeaders)(c, preCompressedAsset);
|
|
168
|
-
c.header('Content-Length', String(responseChunk.byteLength));
|
|
169
|
-
const body = new Uint8Array(responseChunk.buffer, responseChunk.byteOffset, responseChunk.byteLength);
|
|
170
|
-
return c.body(body, 200);
|
|
171
|
-
};
|
|
125
|
+
const moduleFederationStaticServing = (0, external_staticServing_js_namespaceObject.createModuleFederationStaticServing)({
|
|
126
|
+
pwd,
|
|
127
|
+
pathPrefix
|
|
128
|
+
});
|
|
172
129
|
return async (c, next)=>{
|
|
173
130
|
const pageRoute = c.get('route');
|
|
174
131
|
const pathname = c.req.path;
|
|
175
132
|
if (pageRoute && '' === external_path_default().extname(pathname)) return next();
|
|
176
133
|
const hit = staticPathRegExp.test(pathname);
|
|
177
|
-
const
|
|
178
|
-
if (
|
|
179
|
-
const moduleFederationAssetMeta = await getModuleFederationAssets();
|
|
180
|
-
const isModuleFederationAsset = moduleFederationAssetMeta.assets.has(requestPath);
|
|
181
|
-
const isModuleFederationRemoteEntry = moduleFederationAssetMeta.remoteEntries.has(requestPath);
|
|
182
|
-
const serveByPath = async (filepath, moduleFederationAsset = false, moduleFederationRemoteEntry = false)=>{
|
|
183
|
-
if (!isPathInside(filepath, pwd)) return null;
|
|
184
|
-
if (!await utils_namespaceObject.fs.pathExists(filepath)) return null;
|
|
185
|
-
return serveFile(c, filepath, moduleFederationAsset, moduleFederationRemoteEntry, requestPath);
|
|
186
|
-
};
|
|
134
|
+
const staticServingRequest = await moduleFederationStaticServing.resolveRequest(pathname);
|
|
135
|
+
if (null === staticServingRequest) return next();
|
|
187
136
|
if (hit) {
|
|
188
|
-
const response = await
|
|
137
|
+
const response = await moduleFederationStaticServing.serveStaticHit(c, staticServingRequest);
|
|
189
138
|
if (null !== response) return response;
|
|
190
139
|
return next();
|
|
191
140
|
}
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
if (null !== response) return response;
|
|
195
|
-
}
|
|
141
|
+
const moduleFederationResponse = await moduleFederationStaticServing.serveModuleFederationAsset(c, staticServingRequest);
|
|
142
|
+
if (null !== moduleFederationResponse) return moduleFederationResponse;
|
|
196
143
|
return publicMiddleware(c, next);
|
|
197
144
|
};
|
|
198
145
|
}
|
|
@@ -37,15 +37,13 @@ var __webpack_require__ = {};
|
|
|
37
37
|
var __webpack_exports__ = {};
|
|
38
38
|
__webpack_require__.r(__webpack_exports__);
|
|
39
39
|
__webpack_require__.d(__webpack_exports__, {
|
|
40
|
-
BACKEND_MODULE_FEDERATION_MANIFEST_FILE: ()=>BACKEND_MODULE_FEDERATION_MANIFEST_FILE,
|
|
41
40
|
MODULE_FEDERATION_MANIFEST_FILE: ()=>MODULE_FEDERATION_MANIFEST_FILE,
|
|
42
41
|
applyModuleFederationAssetHeaders: ()=>applyModuleFederationAssetHeaders,
|
|
43
42
|
getModuleFederationAssetList: ()=>getModuleFederationAssetList,
|
|
44
43
|
getModuleFederationRequestPath: ()=>getModuleFederationRequestPath,
|
|
45
44
|
isModuleFederationManifestRequest: ()=>isModuleFederationManifestRequest,
|
|
46
45
|
patchModuleFederationManifestPublicPath: ()=>patchModuleFederationManifestPublicPath,
|
|
47
|
-
patchModuleFederationRemoteEntryPublicPath: ()=>patchModuleFederationRemoteEntryPublicPath
|
|
48
|
-
trimLeadingSlash: ()=>trimLeadingSlash
|
|
46
|
+
patchModuleFederationRemoteEntryPublicPath: ()=>patchModuleFederationRemoteEntryPublicPath
|
|
49
47
|
});
|
|
50
48
|
const fileReader_namespaceObject = require("@modern-js/runtime-utils/fileReader");
|
|
51
49
|
const utils_namespaceObject = require("@modern-js/utils");
|
|
@@ -146,7 +144,6 @@ const getModuleFederationAssetList = async (pwd)=>{
|
|
|
146
144
|
remoteEntries
|
|
147
145
|
};
|
|
148
146
|
};
|
|
149
|
-
exports.BACKEND_MODULE_FEDERATION_MANIFEST_FILE = __webpack_exports__.BACKEND_MODULE_FEDERATION_MANIFEST_FILE;
|
|
150
147
|
exports.MODULE_FEDERATION_MANIFEST_FILE = __webpack_exports__.MODULE_FEDERATION_MANIFEST_FILE;
|
|
151
148
|
exports.applyModuleFederationAssetHeaders = __webpack_exports__.applyModuleFederationAssetHeaders;
|
|
152
149
|
exports.getModuleFederationAssetList = __webpack_exports__.getModuleFederationAssetList;
|
|
@@ -154,17 +151,14 @@ exports.getModuleFederationRequestPath = __webpack_exports__.getModuleFederation
|
|
|
154
151
|
exports.isModuleFederationManifestRequest = __webpack_exports__.isModuleFederationManifestRequest;
|
|
155
152
|
exports.patchModuleFederationManifestPublicPath = __webpack_exports__.patchModuleFederationManifestPublicPath;
|
|
156
153
|
exports.patchModuleFederationRemoteEntryPublicPath = __webpack_exports__.patchModuleFederationRemoteEntryPublicPath;
|
|
157
|
-
exports.trimLeadingSlash = __webpack_exports__.trimLeadingSlash;
|
|
158
154
|
for(var __rspack_i in __webpack_exports__)if (-1 === [
|
|
159
|
-
"BACKEND_MODULE_FEDERATION_MANIFEST_FILE",
|
|
160
155
|
"MODULE_FEDERATION_MANIFEST_FILE",
|
|
161
156
|
"applyModuleFederationAssetHeaders",
|
|
162
157
|
"getModuleFederationAssetList",
|
|
163
158
|
"getModuleFederationRequestPath",
|
|
164
159
|
"isModuleFederationManifestRequest",
|
|
165
160
|
"patchModuleFederationManifestPublicPath",
|
|
166
|
-
"patchModuleFederationRemoteEntryPublicPath"
|
|
167
|
-
"trimLeadingSlash"
|
|
161
|
+
"patchModuleFederationRemoteEntryPublicPath"
|
|
168
162
|
].indexOf(__rspack_i)) exports[__rspack_i] = __webpack_exports__[__rspack_i];
|
|
169
163
|
Object.defineProperty(exports, '__esModule', {
|
|
170
164
|
value: true
|
|
@@ -28,7 +28,6 @@ var __webpack_require__ = {};
|
|
|
28
28
|
var __webpack_exports__ = {};
|
|
29
29
|
__webpack_require__.r(__webpack_exports__);
|
|
30
30
|
__webpack_require__.d(__webpack_exports__, {
|
|
31
|
-
appendVaryHeader: ()=>appendVaryHeader,
|
|
32
31
|
applyPreCompressedAssetHeaders: ()=>applyPreCompressedAssetHeaders,
|
|
33
32
|
resolvePreCompressedAsset: ()=>resolvePreCompressedAsset
|
|
34
33
|
});
|
|
@@ -122,11 +121,9 @@ const applyPreCompressedAssetHeaders = (c, preCompressedAsset)=>{
|
|
|
122
121
|
if (preCompressedAsset.hasVariant) appendVaryHeader(c, 'Accept-Encoding');
|
|
123
122
|
if (preCompressedAsset.selected) c.header('Content-Encoding', preCompressedAsset.selected.encoding);
|
|
124
123
|
};
|
|
125
|
-
exports.appendVaryHeader = __webpack_exports__.appendVaryHeader;
|
|
126
124
|
exports.applyPreCompressedAssetHeaders = __webpack_exports__.applyPreCompressedAssetHeaders;
|
|
127
125
|
exports.resolvePreCompressedAsset = __webpack_exports__.resolvePreCompressedAsset;
|
|
128
126
|
for(var __rspack_i in __webpack_exports__)if (-1 === [
|
|
129
|
-
"appendVaryHeader",
|
|
130
127
|
"applyPreCompressedAssetHeaders",
|
|
131
128
|
"resolvePreCompressedAsset"
|
|
132
129
|
].indexOf(__rspack_i)) exports[__rspack_i] = __webpack_exports__[__rspack_i];
|
|
@@ -0,0 +1,131 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __webpack_require__ = {};
|
|
3
|
+
(()=>{
|
|
4
|
+
__webpack_require__.n = (module)=>{
|
|
5
|
+
var getter = module && module.__esModule ? ()=>module['default'] : ()=>module;
|
|
6
|
+
__webpack_require__.d(getter, {
|
|
7
|
+
a: getter
|
|
8
|
+
});
|
|
9
|
+
return getter;
|
|
10
|
+
};
|
|
11
|
+
})();
|
|
12
|
+
(()=>{
|
|
13
|
+
__webpack_require__.d = (exports1, getters, values)=>{
|
|
14
|
+
var define = (defs, kind)=>{
|
|
15
|
+
for(var key in defs)if (__webpack_require__.o(defs, key) && !__webpack_require__.o(exports1, key)) Object.defineProperty(exports1, key, {
|
|
16
|
+
enumerable: true,
|
|
17
|
+
[kind]: defs[key]
|
|
18
|
+
});
|
|
19
|
+
};
|
|
20
|
+
define(getters, "get");
|
|
21
|
+
define(values, "value");
|
|
22
|
+
};
|
|
23
|
+
})();
|
|
24
|
+
(()=>{
|
|
25
|
+
__webpack_require__.o = (obj, prop)=>Object.prototype.hasOwnProperty.call(obj, prop);
|
|
26
|
+
})();
|
|
27
|
+
(()=>{
|
|
28
|
+
__webpack_require__.r = (exports1)=>{
|
|
29
|
+
if ("u" > typeof Symbol && Symbol.toStringTag) Object.defineProperty(exports1, Symbol.toStringTag, {
|
|
30
|
+
value: 'Module'
|
|
31
|
+
});
|
|
32
|
+
Object.defineProperty(exports1, '__esModule', {
|
|
33
|
+
value: true
|
|
34
|
+
});
|
|
35
|
+
};
|
|
36
|
+
})();
|
|
37
|
+
var __webpack_exports__ = {};
|
|
38
|
+
__webpack_require__.r(__webpack_exports__);
|
|
39
|
+
__webpack_require__.d(__webpack_exports__, {
|
|
40
|
+
createModuleFederationStaticServing: ()=>createModuleFederationStaticServing,
|
|
41
|
+
servePreCompressedPublicRouteAsset: ()=>servePreCompressedPublicRouteAsset
|
|
42
|
+
});
|
|
43
|
+
const fileReader_namespaceObject = require("@modern-js/runtime-utils/fileReader");
|
|
44
|
+
const utils_namespaceObject = require("@modern-js/utils");
|
|
45
|
+
const mime_namespaceObject = require("hono/utils/mime");
|
|
46
|
+
const external_path_namespaceObject = require("path");
|
|
47
|
+
var external_path_default = /*#__PURE__*/ __webpack_require__.n(external_path_namespaceObject);
|
|
48
|
+
const external_staticModuleFederation_js_namespaceObject = require("./staticModuleFederation.js");
|
|
49
|
+
const external_staticPrecompressed_js_namespaceObject = require("./staticPrecompressed.js");
|
|
50
|
+
const servePreCompressedPublicRouteAsset = async (c, pwd, route)=>{
|
|
51
|
+
const { entryPath } = route;
|
|
52
|
+
const originFilename = external_path_default().join(pwd, entryPath);
|
|
53
|
+
const preCompressedAsset = await (0, external_staticPrecompressed_js_namespaceObject.resolvePreCompressedAsset)(c, originFilename);
|
|
54
|
+
const filename = preCompressedAsset.selected?.filepath ?? originFilename;
|
|
55
|
+
const data = await fileReader_namespaceObject.fileReader.readFile(filename, 'buffer');
|
|
56
|
+
const mimeType = (0, mime_namespaceObject.getMimeType)(originFilename);
|
|
57
|
+
if (null === data) return null;
|
|
58
|
+
const body = new Uint8Array(data.buffer, data.byteOffset, data.byteLength);
|
|
59
|
+
if (mimeType) c.header('Content-Type', mimeType);
|
|
60
|
+
Object.entries(route.responseHeaders || {}).forEach(([k, v])=>{
|
|
61
|
+
c.header(k, v);
|
|
62
|
+
});
|
|
63
|
+
(0, external_staticPrecompressed_js_namespaceObject.applyPreCompressedAssetHeaders)(c, preCompressedAsset);
|
|
64
|
+
c.header('Content-Length', String(data.byteLength));
|
|
65
|
+
return c.body(body, 200);
|
|
66
|
+
};
|
|
67
|
+
const isPathInside = (target, root)=>{
|
|
68
|
+
const relative = external_path_default().relative(external_path_default().resolve(root), external_path_default().resolve(target));
|
|
69
|
+
return '' === relative || !relative.startsWith(`..${external_path_default().sep}`) && '..' !== relative && !external_path_default().isAbsolute(relative);
|
|
70
|
+
};
|
|
71
|
+
const createModuleFederationStaticServing = ({ pwd, pathPrefix })=>{
|
|
72
|
+
let moduleFederationAssetsPromise = null;
|
|
73
|
+
const getModuleFederationAssets = async ()=>{
|
|
74
|
+
if (!moduleFederationAssetsPromise) moduleFederationAssetsPromise = (0, external_staticModuleFederation_js_namespaceObject.getModuleFederationAssetList)(pwd);
|
|
75
|
+
return moduleFederationAssetsPromise;
|
|
76
|
+
};
|
|
77
|
+
const resolveRequest = async (pathname)=>{
|
|
78
|
+
const requestPath = (0, external_staticModuleFederation_js_namespaceObject.getModuleFederationRequestPath)(pathname, pathPrefix);
|
|
79
|
+
if (requestPath.includes('..')) return null;
|
|
80
|
+
const moduleFederationAssetMeta = await getModuleFederationAssets();
|
|
81
|
+
return {
|
|
82
|
+
requestPath,
|
|
83
|
+
isModuleFederationAsset: moduleFederationAssetMeta.assets.has(requestPath),
|
|
84
|
+
isModuleFederationRemoteEntry: moduleFederationAssetMeta.remoteEntries.has(requestPath)
|
|
85
|
+
};
|
|
86
|
+
};
|
|
87
|
+
const serveFile = async (c, filepath, moduleFederationAsset = false, moduleFederationRemoteEntry = false, requestPath = '')=>{
|
|
88
|
+
if (moduleFederationAsset) (0, external_staticModuleFederation_js_namespaceObject.applyModuleFederationAssetHeaders)(c);
|
|
89
|
+
const mimeType = (0, mime_namespaceObject.getMimeType)(filepath);
|
|
90
|
+
if (mimeType) c.header('Content-Type', mimeType);
|
|
91
|
+
const shouldPatchManifest = moduleFederationAsset && (0, external_staticModuleFederation_js_namespaceObject.isModuleFederationManifestRequest)(requestPath);
|
|
92
|
+
const shouldPatchRemoteEntry = moduleFederationRemoteEntry;
|
|
93
|
+
const canUsePreCompressed = !shouldPatchManifest && !shouldPatchRemoteEntry;
|
|
94
|
+
const preCompressedAsset = canUsePreCompressed ? await (0, external_staticPrecompressed_js_namespaceObject.resolvePreCompressedAsset)(c, filepath) : {
|
|
95
|
+
selected: null,
|
|
96
|
+
hasVariant: false
|
|
97
|
+
};
|
|
98
|
+
const targetFilepath = preCompressedAsset.selected?.filepath ?? filepath;
|
|
99
|
+
const chunk = await fileReader_namespaceObject.fileReader.readFileFromSystem(targetFilepath, 'buffer');
|
|
100
|
+
if (null === chunk) return null;
|
|
101
|
+
const responseChunk = shouldPatchManifest ? (0, external_staticModuleFederation_js_namespaceObject.patchModuleFederationManifestPublicPath)(c, chunk, pathPrefix) : shouldPatchRemoteEntry ? (0, external_staticModuleFederation_js_namespaceObject.patchModuleFederationRemoteEntryPublicPath)(c, chunk, pathPrefix) : chunk;
|
|
102
|
+
(0, external_staticPrecompressed_js_namespaceObject.applyPreCompressedAssetHeaders)(c, preCompressedAsset);
|
|
103
|
+
c.header('Content-Length', String(responseChunk.byteLength));
|
|
104
|
+
const body = new Uint8Array(responseChunk.buffer, responseChunk.byteOffset, responseChunk.byteLength);
|
|
105
|
+
return c.body(body, 200);
|
|
106
|
+
};
|
|
107
|
+
const serveByPath = async (c, filepath, request, moduleFederationAsset = false, moduleFederationRemoteEntry = false)=>{
|
|
108
|
+
if (!isPathInside(filepath, pwd)) return null;
|
|
109
|
+
if (!await utils_namespaceObject.fs.pathExists(filepath)) return null;
|
|
110
|
+
return serveFile(c, filepath, moduleFederationAsset, moduleFederationRemoteEntry, request.requestPath);
|
|
111
|
+
};
|
|
112
|
+
const serveStaticHit = (c, request)=>serveByPath(c, external_path_default().join(pwd, request.requestPath), request, request.isModuleFederationAsset, request.isModuleFederationRemoteEntry);
|
|
113
|
+
const serveModuleFederationAsset = (c, request)=>{
|
|
114
|
+
if (!request.isModuleFederationAsset) return null;
|
|
115
|
+
return serveByPath(c, external_path_default().join(pwd, request.requestPath), request, true, request.isModuleFederationRemoteEntry);
|
|
116
|
+
};
|
|
117
|
+
return {
|
|
118
|
+
resolveRequest,
|
|
119
|
+
serveStaticHit,
|
|
120
|
+
serveModuleFederationAsset
|
|
121
|
+
};
|
|
122
|
+
};
|
|
123
|
+
exports.createModuleFederationStaticServing = __webpack_exports__.createModuleFederationStaticServing;
|
|
124
|
+
exports.servePreCompressedPublicRouteAsset = __webpack_exports__.servePreCompressedPublicRouteAsset;
|
|
125
|
+
for(var __rspack_i in __webpack_exports__)if (-1 === [
|
|
126
|
+
"createModuleFederationStaticServing",
|
|
127
|
+
"servePreCompressedPublicRouteAsset"
|
|
128
|
+
].indexOf(__rspack_i)) exports[__rspack_i] = __webpack_exports__[__rspack_i];
|
|
129
|
+
Object.defineProperty(exports, '__esModule', {
|
|
130
|
+
value: true
|
|
131
|
+
});
|
|
@@ -1,11 +1,7 @@
|
|
|
1
|
-
import { fileReader } from "@modern-js/runtime-utils/fileReader";
|
|
2
|
-
import { fs } from "@modern-js/utils";
|
|
3
|
-
import { getMimeType } from "hono/utils/mime";
|
|
4
1
|
import path from "path";
|
|
5
2
|
import { sortRoutes } from "../../../utils/index.mjs";
|
|
6
3
|
import { getPublicDirPatterns } from "../../../utils/publicDir.mjs";
|
|
7
|
-
import {
|
|
8
|
-
import { applyPreCompressedAssetHeaders, resolvePreCompressedAsset } from "./staticPrecompressed.mjs";
|
|
4
|
+
import { createModuleFederationStaticServing, servePreCompressedPublicRouteAsset } from "./staticServing.mjs";
|
|
9
5
|
const serverStaticPlugin = ()=>({
|
|
10
6
|
name: '@modern-js/plugin-server-static',
|
|
11
7
|
setup (api) {
|
|
@@ -30,22 +26,8 @@ function createPublicMiddleware({ pwd, routes }) {
|
|
|
30
26
|
return async (c, next)=>{
|
|
31
27
|
const route = matchPublicRoute(c.req, routes);
|
|
32
28
|
if (route) {
|
|
33
|
-
const
|
|
34
|
-
|
|
35
|
-
const preCompressedAsset = await resolvePreCompressedAsset(c, originFilename);
|
|
36
|
-
const filename = preCompressedAsset.selected?.filepath ?? originFilename;
|
|
37
|
-
const data = await fileReader.readFile(filename, 'buffer');
|
|
38
|
-
const mimeType = getMimeType(originFilename);
|
|
39
|
-
if (null !== data) {
|
|
40
|
-
const body = new Uint8Array(data.buffer, data.byteOffset, data.byteLength);
|
|
41
|
-
if (mimeType) c.header('Content-Type', mimeType);
|
|
42
|
-
Object.entries(route.responseHeaders || {}).forEach(([k, v])=>{
|
|
43
|
-
c.header(k, v);
|
|
44
|
-
});
|
|
45
|
-
applyPreCompressedAssetHeaders(c, preCompressedAsset);
|
|
46
|
-
c.header('Content-Length', String(data.byteLength));
|
|
47
|
-
return c.body(body, 200);
|
|
48
|
-
}
|
|
29
|
+
const response = await servePreCompressedPublicRouteAsset(c, pwd, route);
|
|
30
|
+
if (null !== response) return response;
|
|
49
31
|
}
|
|
50
32
|
return await next();
|
|
51
33
|
};
|
|
@@ -53,10 +35,6 @@ function createPublicMiddleware({ pwd, routes }) {
|
|
|
53
35
|
function matchPublicRoute(req, routes) {
|
|
54
36
|
for (const route of routes.sort(sortRoutes))if (!route.isSSR && route.entryPath.startsWith('public') && req.path.startsWith(route.urlPath)) return route;
|
|
55
37
|
}
|
|
56
|
-
const isPathInside = (target, root)=>{
|
|
57
|
-
const relative = path.relative(path.resolve(root), path.resolve(target));
|
|
58
|
-
return '' === relative || !relative.startsWith(`..${path.sep}`) && '..' !== relative && !path.isAbsolute(relative);
|
|
59
|
-
};
|
|
60
38
|
const extractPathname = (url)=>{
|
|
61
39
|
try {
|
|
62
40
|
if (url.includes('://')) return new URL(url).pathname || '/';
|
|
@@ -100,55 +78,24 @@ function createStaticMiddleware(options) {
|
|
|
100
78
|
pwd,
|
|
101
79
|
routes: routes || []
|
|
102
80
|
});
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
};
|
|
108
|
-
const serveFile = async (c, filepath, moduleFederationAsset = false, moduleFederationRemoteEntry = false, requestPath = '')=>{
|
|
109
|
-
if (moduleFederationAsset) applyModuleFederationAssetHeaders(c);
|
|
110
|
-
const mimeType = getMimeType(filepath);
|
|
111
|
-
if (mimeType) c.header('Content-Type', mimeType);
|
|
112
|
-
const shouldPatchManifest = moduleFederationAsset && isModuleFederationManifestRequest(requestPath);
|
|
113
|
-
const shouldPatchRemoteEntry = moduleFederationRemoteEntry;
|
|
114
|
-
const canUsePreCompressed = !shouldPatchManifest && !shouldPatchRemoteEntry;
|
|
115
|
-
const preCompressedAsset = canUsePreCompressed ? await resolvePreCompressedAsset(c, filepath) : {
|
|
116
|
-
selected: null,
|
|
117
|
-
hasVariant: false
|
|
118
|
-
};
|
|
119
|
-
const targetFilepath = preCompressedAsset.selected?.filepath ?? filepath;
|
|
120
|
-
const chunk = await fileReader.readFileFromSystem(targetFilepath, 'buffer');
|
|
121
|
-
if (null === chunk) return null;
|
|
122
|
-
const responseChunk = shouldPatchManifest ? patchModuleFederationManifestPublicPath(c, chunk, pathPrefix) : shouldPatchRemoteEntry ? patchModuleFederationRemoteEntryPublicPath(c, chunk, pathPrefix) : chunk;
|
|
123
|
-
applyPreCompressedAssetHeaders(c, preCompressedAsset);
|
|
124
|
-
c.header('Content-Length', String(responseChunk.byteLength));
|
|
125
|
-
const body = new Uint8Array(responseChunk.buffer, responseChunk.byteOffset, responseChunk.byteLength);
|
|
126
|
-
return c.body(body, 200);
|
|
127
|
-
};
|
|
81
|
+
const moduleFederationStaticServing = createModuleFederationStaticServing({
|
|
82
|
+
pwd,
|
|
83
|
+
pathPrefix
|
|
84
|
+
});
|
|
128
85
|
return async (c, next)=>{
|
|
129
86
|
const pageRoute = c.get('route');
|
|
130
87
|
const pathname = c.req.path;
|
|
131
88
|
if (pageRoute && '' === path.extname(pathname)) return next();
|
|
132
89
|
const hit = staticPathRegExp.test(pathname);
|
|
133
|
-
const
|
|
134
|
-
if (
|
|
135
|
-
const moduleFederationAssetMeta = await getModuleFederationAssets();
|
|
136
|
-
const isModuleFederationAsset = moduleFederationAssetMeta.assets.has(requestPath);
|
|
137
|
-
const isModuleFederationRemoteEntry = moduleFederationAssetMeta.remoteEntries.has(requestPath);
|
|
138
|
-
const serveByPath = async (filepath, moduleFederationAsset = false, moduleFederationRemoteEntry = false)=>{
|
|
139
|
-
if (!isPathInside(filepath, pwd)) return null;
|
|
140
|
-
if (!await fs.pathExists(filepath)) return null;
|
|
141
|
-
return serveFile(c, filepath, moduleFederationAsset, moduleFederationRemoteEntry, requestPath);
|
|
142
|
-
};
|
|
90
|
+
const staticServingRequest = await moduleFederationStaticServing.resolveRequest(pathname);
|
|
91
|
+
if (null === staticServingRequest) return next();
|
|
143
92
|
if (hit) {
|
|
144
|
-
const response = await
|
|
93
|
+
const response = await moduleFederationStaticServing.serveStaticHit(c, staticServingRequest);
|
|
145
94
|
if (null !== response) return response;
|
|
146
95
|
return next();
|
|
147
96
|
}
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
if (null !== response) return response;
|
|
151
|
-
}
|
|
97
|
+
const moduleFederationResponse = await moduleFederationStaticServing.serveModuleFederationAsset(c, staticServingRequest);
|
|
98
|
+
if (null !== moduleFederationResponse) return moduleFederationResponse;
|
|
152
99
|
return publicMiddleware(c, next);
|
|
153
100
|
};
|
|
154
101
|
}
|
|
@@ -96,4 +96,4 @@ const getModuleFederationAssetList = async (pwd)=>{
|
|
|
96
96
|
remoteEntries
|
|
97
97
|
};
|
|
98
98
|
};
|
|
99
|
-
export {
|
|
99
|
+
export { MODULE_FEDERATION_MANIFEST_FILE, applyModuleFederationAssetHeaders, getModuleFederationAssetList, getModuleFederationRequestPath, isModuleFederationManifestRequest, patchModuleFederationManifestPublicPath, patchModuleFederationRemoteEntryPublicPath };
|
|
@@ -88,4 +88,4 @@ const applyPreCompressedAssetHeaders = (c, preCompressedAsset)=>{
|
|
|
88
88
|
if (preCompressedAsset.hasVariant) appendVaryHeader(c, 'Accept-Encoding');
|
|
89
89
|
if (preCompressedAsset.selected) c.header('Content-Encoding', preCompressedAsset.selected.encoding);
|
|
90
90
|
};
|
|
91
|
-
export {
|
|
91
|
+
export { applyPreCompressedAssetHeaders, resolvePreCompressedAsset };
|
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
import { fileReader } from "@modern-js/runtime-utils/fileReader";
|
|
2
|
+
import { fs } from "@modern-js/utils";
|
|
3
|
+
import { getMimeType } from "hono/utils/mime";
|
|
4
|
+
import path from "path";
|
|
5
|
+
import { applyModuleFederationAssetHeaders, getModuleFederationAssetList, getModuleFederationRequestPath, isModuleFederationManifestRequest, patchModuleFederationManifestPublicPath, patchModuleFederationRemoteEntryPublicPath } from "./staticModuleFederation.mjs";
|
|
6
|
+
import { applyPreCompressedAssetHeaders, resolvePreCompressedAsset } from "./staticPrecompressed.mjs";
|
|
7
|
+
const servePreCompressedPublicRouteAsset = async (c, pwd, route)=>{
|
|
8
|
+
const { entryPath } = route;
|
|
9
|
+
const originFilename = path.join(pwd, entryPath);
|
|
10
|
+
const preCompressedAsset = await resolvePreCompressedAsset(c, originFilename);
|
|
11
|
+
const filename = preCompressedAsset.selected?.filepath ?? originFilename;
|
|
12
|
+
const data = await fileReader.readFile(filename, 'buffer');
|
|
13
|
+
const mimeType = getMimeType(originFilename);
|
|
14
|
+
if (null === data) return null;
|
|
15
|
+
const body = new Uint8Array(data.buffer, data.byteOffset, data.byteLength);
|
|
16
|
+
if (mimeType) c.header('Content-Type', mimeType);
|
|
17
|
+
Object.entries(route.responseHeaders || {}).forEach(([k, v])=>{
|
|
18
|
+
c.header(k, v);
|
|
19
|
+
});
|
|
20
|
+
applyPreCompressedAssetHeaders(c, preCompressedAsset);
|
|
21
|
+
c.header('Content-Length', String(data.byteLength));
|
|
22
|
+
return c.body(body, 200);
|
|
23
|
+
};
|
|
24
|
+
const isPathInside = (target, root)=>{
|
|
25
|
+
const relative = path.relative(path.resolve(root), path.resolve(target));
|
|
26
|
+
return '' === relative || !relative.startsWith(`..${path.sep}`) && '..' !== relative && !path.isAbsolute(relative);
|
|
27
|
+
};
|
|
28
|
+
const createModuleFederationStaticServing = ({ pwd, pathPrefix })=>{
|
|
29
|
+
let moduleFederationAssetsPromise = null;
|
|
30
|
+
const getModuleFederationAssets = async ()=>{
|
|
31
|
+
if (!moduleFederationAssetsPromise) moduleFederationAssetsPromise = getModuleFederationAssetList(pwd);
|
|
32
|
+
return moduleFederationAssetsPromise;
|
|
33
|
+
};
|
|
34
|
+
const resolveRequest = async (pathname)=>{
|
|
35
|
+
const requestPath = getModuleFederationRequestPath(pathname, pathPrefix);
|
|
36
|
+
if (requestPath.includes('..')) return null;
|
|
37
|
+
const moduleFederationAssetMeta = await getModuleFederationAssets();
|
|
38
|
+
return {
|
|
39
|
+
requestPath,
|
|
40
|
+
isModuleFederationAsset: moduleFederationAssetMeta.assets.has(requestPath),
|
|
41
|
+
isModuleFederationRemoteEntry: moduleFederationAssetMeta.remoteEntries.has(requestPath)
|
|
42
|
+
};
|
|
43
|
+
};
|
|
44
|
+
const serveFile = async (c, filepath, moduleFederationAsset = false, moduleFederationRemoteEntry = false, requestPath = '')=>{
|
|
45
|
+
if (moduleFederationAsset) applyModuleFederationAssetHeaders(c);
|
|
46
|
+
const mimeType = getMimeType(filepath);
|
|
47
|
+
if (mimeType) c.header('Content-Type', mimeType);
|
|
48
|
+
const shouldPatchManifest = moduleFederationAsset && isModuleFederationManifestRequest(requestPath);
|
|
49
|
+
const shouldPatchRemoteEntry = moduleFederationRemoteEntry;
|
|
50
|
+
const canUsePreCompressed = !shouldPatchManifest && !shouldPatchRemoteEntry;
|
|
51
|
+
const preCompressedAsset = canUsePreCompressed ? await resolvePreCompressedAsset(c, filepath) : {
|
|
52
|
+
selected: null,
|
|
53
|
+
hasVariant: false
|
|
54
|
+
};
|
|
55
|
+
const targetFilepath = preCompressedAsset.selected?.filepath ?? filepath;
|
|
56
|
+
const chunk = await fileReader.readFileFromSystem(targetFilepath, 'buffer');
|
|
57
|
+
if (null === chunk) return null;
|
|
58
|
+
const responseChunk = shouldPatchManifest ? patchModuleFederationManifestPublicPath(c, chunk, pathPrefix) : shouldPatchRemoteEntry ? patchModuleFederationRemoteEntryPublicPath(c, chunk, pathPrefix) : chunk;
|
|
59
|
+
applyPreCompressedAssetHeaders(c, preCompressedAsset);
|
|
60
|
+
c.header('Content-Length', String(responseChunk.byteLength));
|
|
61
|
+
const body = new Uint8Array(responseChunk.buffer, responseChunk.byteOffset, responseChunk.byteLength);
|
|
62
|
+
return c.body(body, 200);
|
|
63
|
+
};
|
|
64
|
+
const serveByPath = async (c, filepath, request, moduleFederationAsset = false, moduleFederationRemoteEntry = false)=>{
|
|
65
|
+
if (!isPathInside(filepath, pwd)) return null;
|
|
66
|
+
if (!await fs.pathExists(filepath)) return null;
|
|
67
|
+
return serveFile(c, filepath, moduleFederationAsset, moduleFederationRemoteEntry, request.requestPath);
|
|
68
|
+
};
|
|
69
|
+
const serveStaticHit = (c, request)=>serveByPath(c, path.join(pwd, request.requestPath), request, request.isModuleFederationAsset, request.isModuleFederationRemoteEntry);
|
|
70
|
+
const serveModuleFederationAsset = (c, request)=>{
|
|
71
|
+
if (!request.isModuleFederationAsset) return null;
|
|
72
|
+
return serveByPath(c, path.join(pwd, request.requestPath), request, true, request.isModuleFederationRemoteEntry);
|
|
73
|
+
};
|
|
74
|
+
return {
|
|
75
|
+
resolveRequest,
|
|
76
|
+
serveStaticHit,
|
|
77
|
+
serveModuleFederationAsset
|
|
78
|
+
};
|
|
79
|
+
};
|
|
80
|
+
export { createModuleFederationStaticServing, servePreCompressedPublicRouteAsset };
|
|
@@ -8,10 +8,10 @@ import { renderRscHandler } from "./renderRscHandler.mjs";
|
|
|
8
8
|
import { serverActionHandler } from "./serverActionHandler.mjs";
|
|
9
9
|
import { ssrRender } from "./ssrRender.mjs";
|
|
10
10
|
import { __webpack_require__ } from "../../rslib-runtime.mjs";
|
|
11
|
-
import * as
|
|
11
|
+
import * as __rspack_external__modern_js_runtime_utils_router_4aa9f9b0 from "@modern-js/runtime-utils/router";
|
|
12
12
|
__webpack_require__.add({
|
|
13
|
-
|
|
14
|
-
module.exports =
|
|
13
|
+
5327 (module) {
|
|
14
|
+
module.exports = __rspack_external__modern_js_runtime_utils_router_4aa9f9b0;
|
|
15
15
|
}
|
|
16
16
|
});
|
|
17
17
|
const DYNAMIC_ROUTE_REG = /\/:./;
|
|
@@ -163,7 +163,7 @@ async function renderHandler(request, options, mode, fallbackWrapper, framework)
|
|
|
163
163
|
const { nestedRoutesJson } = serverManifest;
|
|
164
164
|
const routes = nestedRoutesJson?.[options.routeInfo.entryName];
|
|
165
165
|
if (routes) {
|
|
166
|
-
const { matchRoutes } = __webpack_require__(
|
|
166
|
+
const { matchRoutes } = __webpack_require__(5327);
|
|
167
167
|
const url = new URL(request.url);
|
|
168
168
|
const matchedRoutes = matchRoutes(routes, url.pathname, options.routeInfo.urlPath);
|
|
169
169
|
if (matchedRoutes) {
|
|
@@ -1,12 +1,8 @@
|
|
|
1
1
|
import "node:module";
|
|
2
|
-
import { fileReader } from "@modern-js/runtime-utils/fileReader";
|
|
3
|
-
import { fs } from "@modern-js/utils";
|
|
4
|
-
import { getMimeType } from "hono/utils/mime";
|
|
5
2
|
import path from "path";
|
|
6
3
|
import { sortRoutes } from "../../../utils/index.mjs";
|
|
7
4
|
import { getPublicDirPatterns } from "../../../utils/publicDir.mjs";
|
|
8
|
-
import {
|
|
9
|
-
import { applyPreCompressedAssetHeaders, resolvePreCompressedAsset } from "./staticPrecompressed.mjs";
|
|
5
|
+
import { createModuleFederationStaticServing, servePreCompressedPublicRouteAsset } from "./staticServing.mjs";
|
|
10
6
|
const serverStaticPlugin = ()=>({
|
|
11
7
|
name: '@modern-js/plugin-server-static',
|
|
12
8
|
setup (api) {
|
|
@@ -31,22 +27,8 @@ function createPublicMiddleware({ pwd, routes }) {
|
|
|
31
27
|
return async (c, next)=>{
|
|
32
28
|
const route = matchPublicRoute(c.req, routes);
|
|
33
29
|
if (route) {
|
|
34
|
-
const
|
|
35
|
-
|
|
36
|
-
const preCompressedAsset = await resolvePreCompressedAsset(c, originFilename);
|
|
37
|
-
const filename = preCompressedAsset.selected?.filepath ?? originFilename;
|
|
38
|
-
const data = await fileReader.readFile(filename, 'buffer');
|
|
39
|
-
const mimeType = getMimeType(originFilename);
|
|
40
|
-
if (null !== data) {
|
|
41
|
-
const body = new Uint8Array(data.buffer, data.byteOffset, data.byteLength);
|
|
42
|
-
if (mimeType) c.header('Content-Type', mimeType);
|
|
43
|
-
Object.entries(route.responseHeaders || {}).forEach(([k, v])=>{
|
|
44
|
-
c.header(k, v);
|
|
45
|
-
});
|
|
46
|
-
applyPreCompressedAssetHeaders(c, preCompressedAsset);
|
|
47
|
-
c.header('Content-Length', String(data.byteLength));
|
|
48
|
-
return c.body(body, 200);
|
|
49
|
-
}
|
|
30
|
+
const response = await servePreCompressedPublicRouteAsset(c, pwd, route);
|
|
31
|
+
if (null !== response) return response;
|
|
50
32
|
}
|
|
51
33
|
return await next();
|
|
52
34
|
};
|
|
@@ -54,10 +36,6 @@ function createPublicMiddleware({ pwd, routes }) {
|
|
|
54
36
|
function matchPublicRoute(req, routes) {
|
|
55
37
|
for (const route of routes.sort(sortRoutes))if (!route.isSSR && route.entryPath.startsWith('public') && req.path.startsWith(route.urlPath)) return route;
|
|
56
38
|
}
|
|
57
|
-
const isPathInside = (target, root)=>{
|
|
58
|
-
const relative = path.relative(path.resolve(root), path.resolve(target));
|
|
59
|
-
return '' === relative || !relative.startsWith(`..${path.sep}`) && '..' !== relative && !path.isAbsolute(relative);
|
|
60
|
-
};
|
|
61
39
|
const extractPathname = (url)=>{
|
|
62
40
|
try {
|
|
63
41
|
if (url.includes('://')) return new URL(url).pathname || '/';
|
|
@@ -101,55 +79,24 @@ function createStaticMiddleware(options) {
|
|
|
101
79
|
pwd,
|
|
102
80
|
routes: routes || []
|
|
103
81
|
});
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
};
|
|
109
|
-
const serveFile = async (c, filepath, moduleFederationAsset = false, moduleFederationRemoteEntry = false, requestPath = '')=>{
|
|
110
|
-
if (moduleFederationAsset) applyModuleFederationAssetHeaders(c);
|
|
111
|
-
const mimeType = getMimeType(filepath);
|
|
112
|
-
if (mimeType) c.header('Content-Type', mimeType);
|
|
113
|
-
const shouldPatchManifest = moduleFederationAsset && isModuleFederationManifestRequest(requestPath);
|
|
114
|
-
const shouldPatchRemoteEntry = moduleFederationRemoteEntry;
|
|
115
|
-
const canUsePreCompressed = !shouldPatchManifest && !shouldPatchRemoteEntry;
|
|
116
|
-
const preCompressedAsset = canUsePreCompressed ? await resolvePreCompressedAsset(c, filepath) : {
|
|
117
|
-
selected: null,
|
|
118
|
-
hasVariant: false
|
|
119
|
-
};
|
|
120
|
-
const targetFilepath = preCompressedAsset.selected?.filepath ?? filepath;
|
|
121
|
-
const chunk = await fileReader.readFileFromSystem(targetFilepath, 'buffer');
|
|
122
|
-
if (null === chunk) return null;
|
|
123
|
-
const responseChunk = shouldPatchManifest ? patchModuleFederationManifestPublicPath(c, chunk, pathPrefix) : shouldPatchRemoteEntry ? patchModuleFederationRemoteEntryPublicPath(c, chunk, pathPrefix) : chunk;
|
|
124
|
-
applyPreCompressedAssetHeaders(c, preCompressedAsset);
|
|
125
|
-
c.header('Content-Length', String(responseChunk.byteLength));
|
|
126
|
-
const body = new Uint8Array(responseChunk.buffer, responseChunk.byteOffset, responseChunk.byteLength);
|
|
127
|
-
return c.body(body, 200);
|
|
128
|
-
};
|
|
82
|
+
const moduleFederationStaticServing = createModuleFederationStaticServing({
|
|
83
|
+
pwd,
|
|
84
|
+
pathPrefix
|
|
85
|
+
});
|
|
129
86
|
return async (c, next)=>{
|
|
130
87
|
const pageRoute = c.get('route');
|
|
131
88
|
const pathname = c.req.path;
|
|
132
89
|
if (pageRoute && '' === path.extname(pathname)) return next();
|
|
133
90
|
const hit = staticPathRegExp.test(pathname);
|
|
134
|
-
const
|
|
135
|
-
if (
|
|
136
|
-
const moduleFederationAssetMeta = await getModuleFederationAssets();
|
|
137
|
-
const isModuleFederationAsset = moduleFederationAssetMeta.assets.has(requestPath);
|
|
138
|
-
const isModuleFederationRemoteEntry = moduleFederationAssetMeta.remoteEntries.has(requestPath);
|
|
139
|
-
const serveByPath = async (filepath, moduleFederationAsset = false, moduleFederationRemoteEntry = false)=>{
|
|
140
|
-
if (!isPathInside(filepath, pwd)) return null;
|
|
141
|
-
if (!await fs.pathExists(filepath)) return null;
|
|
142
|
-
return serveFile(c, filepath, moduleFederationAsset, moduleFederationRemoteEntry, requestPath);
|
|
143
|
-
};
|
|
91
|
+
const staticServingRequest = await moduleFederationStaticServing.resolveRequest(pathname);
|
|
92
|
+
if (null === staticServingRequest) return next();
|
|
144
93
|
if (hit) {
|
|
145
|
-
const response = await
|
|
94
|
+
const response = await moduleFederationStaticServing.serveStaticHit(c, staticServingRequest);
|
|
146
95
|
if (null !== response) return response;
|
|
147
96
|
return next();
|
|
148
97
|
}
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
if (null !== response) return response;
|
|
152
|
-
}
|
|
98
|
+
const moduleFederationResponse = await moduleFederationStaticServing.serveModuleFederationAsset(c, staticServingRequest);
|
|
99
|
+
if (null !== moduleFederationResponse) return moduleFederationResponse;
|
|
153
100
|
return publicMiddleware(c, next);
|
|
154
101
|
};
|
|
155
102
|
}
|
|
@@ -97,4 +97,4 @@ const getModuleFederationAssetList = async (pwd)=>{
|
|
|
97
97
|
remoteEntries
|
|
98
98
|
};
|
|
99
99
|
};
|
|
100
|
-
export {
|
|
100
|
+
export { MODULE_FEDERATION_MANIFEST_FILE, applyModuleFederationAssetHeaders, getModuleFederationAssetList, getModuleFederationRequestPath, isModuleFederationManifestRequest, patchModuleFederationManifestPublicPath, patchModuleFederationRemoteEntryPublicPath };
|
|
@@ -89,4 +89,4 @@ const applyPreCompressedAssetHeaders = (c, preCompressedAsset)=>{
|
|
|
89
89
|
if (preCompressedAsset.hasVariant) appendVaryHeader(c, 'Accept-Encoding');
|
|
90
90
|
if (preCompressedAsset.selected) c.header('Content-Encoding', preCompressedAsset.selected.encoding);
|
|
91
91
|
};
|
|
92
|
-
export {
|
|
92
|
+
export { applyPreCompressedAssetHeaders, resolvePreCompressedAsset };
|
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
import "node:module";
|
|
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 path from "path";
|
|
6
|
+
import { applyModuleFederationAssetHeaders, getModuleFederationAssetList, getModuleFederationRequestPath, isModuleFederationManifestRequest, patchModuleFederationManifestPublicPath, patchModuleFederationRemoteEntryPublicPath } from "./staticModuleFederation.mjs";
|
|
7
|
+
import { applyPreCompressedAssetHeaders, resolvePreCompressedAsset } from "./staticPrecompressed.mjs";
|
|
8
|
+
const servePreCompressedPublicRouteAsset = async (c, pwd, route)=>{
|
|
9
|
+
const { entryPath } = route;
|
|
10
|
+
const originFilename = path.join(pwd, entryPath);
|
|
11
|
+
const preCompressedAsset = await resolvePreCompressedAsset(c, originFilename);
|
|
12
|
+
const filename = preCompressedAsset.selected?.filepath ?? originFilename;
|
|
13
|
+
const data = await fileReader.readFile(filename, 'buffer');
|
|
14
|
+
const mimeType = getMimeType(originFilename);
|
|
15
|
+
if (null === data) return null;
|
|
16
|
+
const body = new Uint8Array(data.buffer, data.byteOffset, data.byteLength);
|
|
17
|
+
if (mimeType) c.header('Content-Type', mimeType);
|
|
18
|
+
Object.entries(route.responseHeaders || {}).forEach(([k, v])=>{
|
|
19
|
+
c.header(k, v);
|
|
20
|
+
});
|
|
21
|
+
applyPreCompressedAssetHeaders(c, preCompressedAsset);
|
|
22
|
+
c.header('Content-Length', String(data.byteLength));
|
|
23
|
+
return c.body(body, 200);
|
|
24
|
+
};
|
|
25
|
+
const isPathInside = (target, root)=>{
|
|
26
|
+
const relative = path.relative(path.resolve(root), path.resolve(target));
|
|
27
|
+
return '' === relative || !relative.startsWith(`..${path.sep}`) && '..' !== relative && !path.isAbsolute(relative);
|
|
28
|
+
};
|
|
29
|
+
const createModuleFederationStaticServing = ({ pwd, pathPrefix })=>{
|
|
30
|
+
let moduleFederationAssetsPromise = null;
|
|
31
|
+
const getModuleFederationAssets = async ()=>{
|
|
32
|
+
if (!moduleFederationAssetsPromise) moduleFederationAssetsPromise = getModuleFederationAssetList(pwd);
|
|
33
|
+
return moduleFederationAssetsPromise;
|
|
34
|
+
};
|
|
35
|
+
const resolveRequest = async (pathname)=>{
|
|
36
|
+
const requestPath = getModuleFederationRequestPath(pathname, pathPrefix);
|
|
37
|
+
if (requestPath.includes('..')) return null;
|
|
38
|
+
const moduleFederationAssetMeta = await getModuleFederationAssets();
|
|
39
|
+
return {
|
|
40
|
+
requestPath,
|
|
41
|
+
isModuleFederationAsset: moduleFederationAssetMeta.assets.has(requestPath),
|
|
42
|
+
isModuleFederationRemoteEntry: moduleFederationAssetMeta.remoteEntries.has(requestPath)
|
|
43
|
+
};
|
|
44
|
+
};
|
|
45
|
+
const serveFile = async (c, filepath, moduleFederationAsset = false, moduleFederationRemoteEntry = false, requestPath = '')=>{
|
|
46
|
+
if (moduleFederationAsset) applyModuleFederationAssetHeaders(c);
|
|
47
|
+
const mimeType = getMimeType(filepath);
|
|
48
|
+
if (mimeType) c.header('Content-Type', mimeType);
|
|
49
|
+
const shouldPatchManifest = moduleFederationAsset && isModuleFederationManifestRequest(requestPath);
|
|
50
|
+
const shouldPatchRemoteEntry = moduleFederationRemoteEntry;
|
|
51
|
+
const canUsePreCompressed = !shouldPatchManifest && !shouldPatchRemoteEntry;
|
|
52
|
+
const preCompressedAsset = canUsePreCompressed ? await resolvePreCompressedAsset(c, filepath) : {
|
|
53
|
+
selected: null,
|
|
54
|
+
hasVariant: false
|
|
55
|
+
};
|
|
56
|
+
const targetFilepath = preCompressedAsset.selected?.filepath ?? filepath;
|
|
57
|
+
const chunk = await fileReader.readFileFromSystem(targetFilepath, 'buffer');
|
|
58
|
+
if (null === chunk) return null;
|
|
59
|
+
const responseChunk = shouldPatchManifest ? patchModuleFederationManifestPublicPath(c, chunk, pathPrefix) : shouldPatchRemoteEntry ? patchModuleFederationRemoteEntryPublicPath(c, chunk, pathPrefix) : chunk;
|
|
60
|
+
applyPreCompressedAssetHeaders(c, preCompressedAsset);
|
|
61
|
+
c.header('Content-Length', String(responseChunk.byteLength));
|
|
62
|
+
const body = new Uint8Array(responseChunk.buffer, responseChunk.byteOffset, responseChunk.byteLength);
|
|
63
|
+
return c.body(body, 200);
|
|
64
|
+
};
|
|
65
|
+
const serveByPath = async (c, filepath, request, moduleFederationAsset = false, moduleFederationRemoteEntry = false)=>{
|
|
66
|
+
if (!isPathInside(filepath, pwd)) return null;
|
|
67
|
+
if (!await fs.pathExists(filepath)) return null;
|
|
68
|
+
return serveFile(c, filepath, moduleFederationAsset, moduleFederationRemoteEntry, request.requestPath);
|
|
69
|
+
};
|
|
70
|
+
const serveStaticHit = (c, request)=>serveByPath(c, path.join(pwd, request.requestPath), request, request.isModuleFederationAsset, request.isModuleFederationRemoteEntry);
|
|
71
|
+
const serveModuleFederationAsset = (c, request)=>{
|
|
72
|
+
if (!request.isModuleFederationAsset) return null;
|
|
73
|
+
return serveByPath(c, path.join(pwd, request.requestPath), request, true, request.isModuleFederationRemoteEntry);
|
|
74
|
+
};
|
|
75
|
+
return {
|
|
76
|
+
resolveRequest,
|
|
77
|
+
serveStaticHit,
|
|
78
|
+
serveModuleFederationAsset
|
|
79
|
+
};
|
|
80
|
+
};
|
|
81
|
+
export { createModuleFederationStaticServing, servePreCompressedPublicRouteAsset };
|
|
@@ -9,10 +9,10 @@ import { renderRscHandler } from "./renderRscHandler.mjs";
|
|
|
9
9
|
import { serverActionHandler } from "./serverActionHandler.mjs";
|
|
10
10
|
import { ssrRender } from "./ssrRender.mjs";
|
|
11
11
|
import { __webpack_require__ } from "../../rslib-runtime.mjs";
|
|
12
|
-
import * as
|
|
12
|
+
import * as __rspack_external__modern_js_runtime_utils_router_4aa9f9b0 from "@modern-js/runtime-utils/router";
|
|
13
13
|
__webpack_require__.add({
|
|
14
14
|
"@modern-js/runtime-utils/router" (module) {
|
|
15
|
-
module.exports =
|
|
15
|
+
module.exports = __rspack_external__modern_js_runtime_utils_router_4aa9f9b0;
|
|
16
16
|
}
|
|
17
17
|
});
|
|
18
18
|
const DYNAMIC_ROUTE_REG = /\/:./;
|
|
@@ -1,11 +1,9 @@
|
|
|
1
1
|
import type { Middleware } from '../../../types';
|
|
2
2
|
export declare const MODULE_FEDERATION_MANIFEST_FILE = "mf-manifest.json";
|
|
3
|
-
export declare const BACKEND_MODULE_FEDERATION_MANIFEST_FILE = "backend-mf-manifest.json";
|
|
4
3
|
export type ModuleFederationServeAssets = {
|
|
5
4
|
assets: Set<string>;
|
|
6
5
|
remoteEntries: Set<string>;
|
|
7
6
|
};
|
|
8
|
-
export declare const trimLeadingSlash: (value: string) => string;
|
|
9
7
|
export declare const getModuleFederationRequestPath: (pathname: string, pathPrefix: string) => string;
|
|
10
8
|
export declare const isModuleFederationManifestRequest: (requestPath: string) => boolean;
|
|
11
9
|
export declare const applyModuleFederationAssetHeaders: (c: Parameters<Middleware>[0]) => void;
|
|
@@ -1,13 +1,12 @@
|
|
|
1
1
|
import type { Middleware } from '../../../types';
|
|
2
2
|
type SupportedEncoding = 'br' | 'gzip';
|
|
3
|
-
|
|
3
|
+
type ResolvePreCompressedAssetResult = {
|
|
4
4
|
selected: {
|
|
5
5
|
filepath: string;
|
|
6
6
|
encoding: SupportedEncoding;
|
|
7
7
|
} | null;
|
|
8
8
|
hasVariant: boolean;
|
|
9
9
|
};
|
|
10
|
-
export declare const appendVaryHeader: (c: Parameters<Middleware>[0], value: string) => void;
|
|
11
10
|
export declare const resolvePreCompressedAsset: (c: Parameters<Middleware>[0], filepath: string) => Promise<ResolvePreCompressedAssetResult>;
|
|
12
11
|
export declare const applyPreCompressedAssetHeaders: (c: Parameters<Middleware>[0], preCompressedAsset: ResolvePreCompressedAssetResult) => void;
|
|
13
12
|
export {};
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import type { ServerRoute } from '@modern-js/types';
|
|
2
|
+
import type { Middleware } from '../../../types';
|
|
3
|
+
type MiddlewareContext = Parameters<Middleware>[0];
|
|
4
|
+
type StaticServingRequest = {
|
|
5
|
+
requestPath: string;
|
|
6
|
+
isModuleFederationAsset: boolean;
|
|
7
|
+
isModuleFederationRemoteEntry: boolean;
|
|
8
|
+
};
|
|
9
|
+
type StaticServingOptions = {
|
|
10
|
+
pwd: string;
|
|
11
|
+
pathPrefix: string;
|
|
12
|
+
};
|
|
13
|
+
export declare const servePreCompressedPublicRouteAsset: (c: MiddlewareContext, pwd: string, route: ServerRoute) => Promise<(Response & import("hono").TypedResponse<Uint8Array<ArrayBuffer>, 200, "body">) | null>;
|
|
14
|
+
export declare const createModuleFederationStaticServing: ({ pwd, pathPrefix, }: StaticServingOptions) => {
|
|
15
|
+
resolveRequest: (pathname: string) => Promise<StaticServingRequest | null>;
|
|
16
|
+
serveStaticHit: (c: MiddlewareContext, request: StaticServingRequest) => Promise<(Response & import("hono").TypedResponse<Uint8Array<ArrayBuffer>, 200, "body">) | null>;
|
|
17
|
+
serveModuleFederationAsset: (c: MiddlewareContext, request: StaticServingRequest) => Promise<(Response & import("hono").TypedResponse<Uint8Array<ArrayBuffer>, 200, "body">) | null> | null;
|
|
18
|
+
};
|
|
19
|
+
export {};
|
package/package.json
CHANGED
|
@@ -17,7 +17,7 @@
|
|
|
17
17
|
"modern",
|
|
18
18
|
"modern.js"
|
|
19
19
|
],
|
|
20
|
-
"version": "3.5.0-ultramodern.
|
|
20
|
+
"version": "3.5.0-ultramodern.38",
|
|
21
21
|
"types": "./dist/types/index.d.ts",
|
|
22
22
|
"main": "./dist/cjs/index.js",
|
|
23
23
|
"exports": {
|
|
@@ -74,21 +74,21 @@
|
|
|
74
74
|
"@web-std/stream": "^1.0.3",
|
|
75
75
|
"cloneable-readable": "^3.0.0",
|
|
76
76
|
"flatted": "^3.4.2",
|
|
77
|
-
"hono": "^4.12.
|
|
77
|
+
"hono": "^4.12.28",
|
|
78
78
|
"ts-deepmerge": "8.0.0",
|
|
79
|
-
"@modern-js/
|
|
80
|
-
"@modern-js/
|
|
81
|
-
"@modern-js/utils": "npm:@bleedingdev/modern-js-utils@3.5.0-ultramodern.
|
|
79
|
+
"@modern-js/plugin": "npm:@bleedingdev/modern-js-plugin@3.5.0-ultramodern.38",
|
|
80
|
+
"@modern-js/runtime-utils": "npm:@bleedingdev/modern-js-runtime-utils@3.5.0-ultramodern.38",
|
|
81
|
+
"@modern-js/utils": "npm:@bleedingdev/modern-js-utils@3.5.0-ultramodern.38"
|
|
82
82
|
},
|
|
83
83
|
"devDependencies": {
|
|
84
|
-
"@rslib/core": "0.23.
|
|
84
|
+
"@rslib/core": "0.23.2",
|
|
85
85
|
"@types/cloneable-readable": "^2.0.3",
|
|
86
86
|
"@types/merge-deep": "^3.0.3",
|
|
87
|
-
"@types/node": "^26.
|
|
88
|
-
"@typescript/native-preview": "7.0.0-dev.
|
|
89
|
-
"http-proxy-middleware": "^4.
|
|
90
|
-
"@
|
|
91
|
-
"@
|
|
87
|
+
"@types/node": "^26.1.1",
|
|
88
|
+
"@typescript/native-preview": "7.0.0-dev.20260707.2",
|
|
89
|
+
"http-proxy-middleware": "^4.2.0",
|
|
90
|
+
"@scripts/rstest-config": "2.66.0",
|
|
91
|
+
"@modern-js/types": "npm:@bleedingdev/modern-js-types@3.5.0-ultramodern.38"
|
|
92
92
|
},
|
|
93
93
|
"sideEffects": false,
|
|
94
94
|
"publishConfig": {
|