@bleedingdev/modern-js-server-core 3.5.0-ultramodern.5 → 3.5.0-ultramodern.75

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.
@@ -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 external_staticModuleFederation_js_namespaceObject = require("./staticModuleFederation.js");
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,23 +70,11 @@ function createPublicMiddleware({ pwd, routes }) {
74
70
  return async (c, next)=>{
75
71
  const route = matchPublicRoute(c.req, routes);
76
72
  if (route) {
77
- const { entryPath } = route;
78
- const originFilename = external_path_default().join(pwd, entryPath);
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
  }
76
+ const generatedPublicAsset = await (0, external_staticServing_js_namespaceObject.servePublicDirectoryAsset)(c, pwd);
77
+ if (null !== generatedPublicAsset) return generatedPublicAsset;
94
78
  return await next();
95
79
  };
96
80
  }
@@ -140,54 +124,24 @@ function createStaticMiddleware(options) {
140
124
  pwd,
141
125
  routes: routes || []
142
126
  });
143
- let moduleFederationAssetsPromise = null;
144
- const getModuleFederationAssets = async ()=>{
145
- if (!moduleFederationAssetsPromise) moduleFederationAssetsPromise = (0, external_staticModuleFederation_js_namespaceObject.getModuleFederationAssetList)(pwd);
146
- return moduleFederationAssetsPromise;
147
- };
148
- const serveFile = async (c, filepath, moduleFederationAsset = false, moduleFederationRemoteEntry = false, requestPath = '')=>{
149
- if (moduleFederationAsset) (0, external_staticModuleFederation_js_namespaceObject.applyModuleFederationAssetHeaders)(c);
150
- const mimeType = (0, mime_namespaceObject.getMimeType)(filepath);
151
- if (mimeType) c.header('Content-Type', mimeType);
152
- const shouldPatchManifest = moduleFederationAsset && (0, external_staticModuleFederation_js_namespaceObject.isModuleFederationManifestRequest)(requestPath);
153
- const shouldPatchRemoteEntry = moduleFederationRemoteEntry;
154
- const canUsePreCompressed = !shouldPatchManifest && !shouldPatchRemoteEntry;
155
- const preCompressedAsset = canUsePreCompressed ? await (0, external_staticPrecompressed_js_namespaceObject.resolvePreCompressedAsset)(c, filepath) : {
156
- selected: null,
157
- hasVariant: false
158
- };
159
- const targetFilepath = preCompressedAsset.selected?.filepath ?? filepath;
160
- const chunk = await fileReader_namespaceObject.fileReader.readFileFromSystem(targetFilepath, 'buffer');
161
- if (null === chunk) return null;
162
- const responseChunk = shouldPatchManifest ? (0, external_staticModuleFederation_js_namespaceObject.patchModuleFederationManifestPublicPath)(c, chunk, pathPrefix) : shouldPatchRemoteEntry ? (0, external_staticModuleFederation_js_namespaceObject.patchModuleFederationRemoteEntryPublicPath)(c, chunk, pathPrefix) : chunk;
163
- (0, external_staticPrecompressed_js_namespaceObject.applyPreCompressedAssetHeaders)(c, preCompressedAsset);
164
- c.header('Content-Length', String(responseChunk.byteLength));
165
- const body = new Uint8Array(responseChunk.buffer, responseChunk.byteOffset, responseChunk.byteLength);
166
- return c.body(body, 200);
167
- };
127
+ const moduleFederationStaticServing = (0, external_staticServing_js_namespaceObject.createModuleFederationStaticServing)({
128
+ pwd,
129
+ pathPrefix
130
+ });
168
131
  return async (c, next)=>{
169
132
  const pageRoute = c.get('route');
170
133
  const pathname = c.req.path;
171
134
  if (pageRoute && '' === external_path_default().extname(pathname)) return next();
172
135
  const hit = staticPathRegExp.test(pathname);
173
- const requestPath = (0, external_staticModuleFederation_js_namespaceObject.getModuleFederationRequestPath)(pathname, pathPrefix);
174
- if (requestPath.includes('..')) return next();
175
- const moduleFederationAssetMeta = await getModuleFederationAssets();
176
- const isModuleFederationAsset = moduleFederationAssetMeta.assets.has(requestPath);
177
- const isModuleFederationRemoteEntry = moduleFederationAssetMeta.remoteEntry === requestPath;
178
- const serveByPath = async (filepath, moduleFederationAsset = false, moduleFederationRemoteEntry = false)=>{
179
- if (!await utils_namespaceObject.fs.pathExists(filepath)) return null;
180
- return serveFile(c, filepath, moduleFederationAsset, moduleFederationRemoteEntry, requestPath);
181
- };
136
+ const staticServingRequest = await moduleFederationStaticServing.resolveRequest(pathname);
137
+ if (null === staticServingRequest) return next();
182
138
  if (hit) {
183
- const response = await serveByPath(external_path_default().join(pwd, requestPath), isModuleFederationAsset, isModuleFederationRemoteEntry);
139
+ const response = await moduleFederationStaticServing.serveStaticHit(c, staticServingRequest);
184
140
  if (null !== response) return response;
185
141
  return next();
186
142
  }
187
- if (isModuleFederationAsset) {
188
- const response = await serveByPath(external_path_default().join(pwd, requestPath), true, isModuleFederationRemoteEntry);
189
- if (null !== response) return response;
190
- }
143
+ const moduleFederationResponse = await moduleFederationStaticServing.serveModuleFederationAsset(c, staticServingRequest);
144
+ if (null !== moduleFederationResponse) return moduleFederationResponse;
191
145
  return publicMiddleware(c, next);
192
146
  };
193
147
  }
@@ -41,22 +41,28 @@ __webpack_require__.d(__webpack_exports__, {
41
41
  applyModuleFederationAssetHeaders: ()=>applyModuleFederationAssetHeaders,
42
42
  getModuleFederationAssetList: ()=>getModuleFederationAssetList,
43
43
  getModuleFederationRequestPath: ()=>getModuleFederationRequestPath,
44
+ isBackendModuleFederationManifestRequest: ()=>isBackendModuleFederationManifestRequest,
44
45
  isModuleFederationManifestRequest: ()=>isModuleFederationManifestRequest,
45
46
  patchModuleFederationManifestPublicPath: ()=>patchModuleFederationManifestPublicPath,
46
- patchModuleFederationRemoteEntryPublicPath: ()=>patchModuleFederationRemoteEntryPublicPath,
47
- trimLeadingSlash: ()=>trimLeadingSlash
47
+ patchModuleFederationRemoteEntryPublicPath: ()=>patchModuleFederationRemoteEntryPublicPath
48
48
  });
49
49
  const fileReader_namespaceObject = require("@modern-js/runtime-utils/fileReader");
50
50
  const utils_namespaceObject = require("@modern-js/utils");
51
51
  const external_path_namespaceObject = require("path");
52
52
  var external_path_default = /*#__PURE__*/ __webpack_require__.n(external_path_namespaceObject);
53
53
  const MODULE_FEDERATION_MANIFEST_FILE = 'mf-manifest.json';
54
+ const BACKEND_MODULE_FEDERATION_MANIFEST_FILE = 'backend-mf-manifest.json';
55
+ const MODULE_FEDERATION_MANIFEST_FILES = [
56
+ MODULE_FEDERATION_MANIFEST_FILE,
57
+ BACKEND_MODULE_FEDERATION_MANIFEST_FILE
58
+ ];
54
59
  const MODULE_FEDERATION_OPTIONAL_FILES = [
55
60
  'mf-stats.json'
56
61
  ];
57
62
  const trimLeadingSlash = (value)=>value.replace(/^\/+/, '');
58
63
  const getModuleFederationRequestPath = (pathname, pathPrefix)=>trimLeadingSlash(pathname.replace(pathPrefix, ()=>''));
59
- const isModuleFederationManifestRequest = (requestPath)=>requestPath === MODULE_FEDERATION_MANIFEST_FILE;
64
+ const isModuleFederationManifestRequest = (requestPath)=>MODULE_FEDERATION_MANIFEST_FILES.includes(requestPath);
65
+ const isBackendModuleFederationManifestRequest = (requestPath)=>requestPath === BACKEND_MODULE_FEDERATION_MANIFEST_FILE;
60
66
  const applyModuleFederationAssetHeaders = (c)=>{
61
67
  c.header('Access-Control-Allow-Origin', '*');
62
68
  c.header('Access-Control-Allow-Headers', '*');
@@ -67,8 +73,7 @@ const joinModuleFederationAssetPath = (assetPath, assetName)=>{
67
73
  return trimLeadingSlash(external_path_default().posix.join(assetPath || '', assetName));
68
74
  };
69
75
  const appendModuleFederationAsset = (set, assetPath)=>{
70
- if (!assetPath) return;
71
- set.add(trimLeadingSlash(assetPath));
76
+ if (assetPath) set.add(trimLeadingSlash(assetPath));
72
77
  };
73
78
  const appendModuleFederationAssets = (set, assets)=>{
74
79
  assets?.js?.sync?.forEach((asset)=>appendModuleFederationAsset(set, asset));
@@ -109,56 +114,55 @@ const patchModuleFederationRemoteEntryPublicPath = (c, remoteEntryBuffer, pathPr
109
114
  };
110
115
  const getModuleFederationAssetList = async (pwd)=>{
111
116
  const assets = new Set();
112
- const manifestPath = external_path_default().join(pwd, MODULE_FEDERATION_MANIFEST_FILE);
113
- if (!await utils_namespaceObject.fs.pathExists(manifestPath)) return {
114
- assets,
115
- remoteEntry: null
116
- };
117
- assets.add(MODULE_FEDERATION_MANIFEST_FILE);
118
- const manifestBuffer = await fileReader_namespaceObject.fileReader.readFileFromSystem(manifestPath, 'buffer');
119
- if (null === manifestBuffer) return {
120
- assets,
121
- remoteEntry: null
122
- };
123
- for (const filename of MODULE_FEDERATION_OPTIONAL_FILES)if (await utils_namespaceObject.fs.pathExists(external_path_default().join(pwd, filename))) assets.add(filename);
124
- let remoteEntryFile = null;
125
- try {
126
- const manifest = JSON.parse(manifestBuffer.toString('utf-8'));
127
- const remoteEntry = joinModuleFederationAssetPath(manifest.metaData?.remoteEntry?.path, manifest.metaData?.remoteEntry?.name);
128
- const dtsZip = joinModuleFederationAssetPath(manifest.metaData?.types?.path, manifest.metaData?.types?.zip);
129
- const dtsApi = joinModuleFederationAssetPath(manifest.metaData?.types?.path, manifest.metaData?.types?.api);
130
- if (remoteEntry) {
131
- assets.add(remoteEntry);
132
- remoteEntryFile = remoteEntry;
133
- }
134
- if (dtsZip) assets.add(dtsZip);
135
- if (dtsApi) assets.add(dtsApi);
136
- manifest.shared?.forEach((item)=>appendModuleFederationAssets(assets, item.assets));
137
- manifest.remotes?.forEach((item)=>appendModuleFederationAssets(assets, item.assets));
138
- manifest.exposes?.forEach((item)=>appendModuleFederationAssets(assets, item.assets));
139
- } catch {}
117
+ const remoteEntries = new Set();
118
+ let manifestFound = false;
119
+ for (const manifestFile of MODULE_FEDERATION_MANIFEST_FILES){
120
+ const manifestPath = external_path_default().join(pwd, manifestFile);
121
+ if (!await utils_namespaceObject.fs.pathExists(manifestPath)) continue;
122
+ manifestFound = true;
123
+ assets.add(manifestFile);
124
+ const manifestBuffer = await fileReader_namespaceObject.fileReader.readFileFromSystem(manifestPath, 'buffer');
125
+ if (null !== manifestBuffer) try {
126
+ const manifest = JSON.parse(manifestBuffer.toString('utf-8'));
127
+ const remoteEntry = joinModuleFederationAssetPath(manifest.metaData?.remoteEntry?.path, manifest.metaData?.remoteEntry?.name);
128
+ const dtsZip = joinModuleFederationAssetPath(manifest.metaData?.types?.path, manifest.metaData?.types?.zip);
129
+ const dtsApi = joinModuleFederationAssetPath(manifest.metaData?.types?.path, manifest.metaData?.types?.api);
130
+ if (remoteEntry) {
131
+ assets.add(remoteEntry);
132
+ remoteEntries.add(remoteEntry);
133
+ }
134
+ appendModuleFederationAsset(assets, dtsZip);
135
+ appendModuleFederationAsset(assets, dtsApi);
136
+ manifest.shared?.forEach((item)=>appendModuleFederationAssets(assets, item.assets));
137
+ manifest.remotes?.forEach((item)=>appendModuleFederationAssets(assets, item.assets));
138
+ manifest.exposes?.forEach((item)=>appendModuleFederationAssets(assets, item.assets));
139
+ } catch {}
140
+ }
141
+ if (manifestFound) {
142
+ for (const filename of MODULE_FEDERATION_OPTIONAL_FILES)if (await utils_namespaceObject.fs.pathExists(external_path_default().join(pwd, filename))) assets.add(filename);
143
+ }
140
144
  return {
141
145
  assets,
142
- remoteEntry: remoteEntryFile
146
+ remoteEntries
143
147
  };
144
148
  };
145
149
  exports.MODULE_FEDERATION_MANIFEST_FILE = __webpack_exports__.MODULE_FEDERATION_MANIFEST_FILE;
146
150
  exports.applyModuleFederationAssetHeaders = __webpack_exports__.applyModuleFederationAssetHeaders;
147
151
  exports.getModuleFederationAssetList = __webpack_exports__.getModuleFederationAssetList;
148
152
  exports.getModuleFederationRequestPath = __webpack_exports__.getModuleFederationRequestPath;
153
+ exports.isBackendModuleFederationManifestRequest = __webpack_exports__.isBackendModuleFederationManifestRequest;
149
154
  exports.isModuleFederationManifestRequest = __webpack_exports__.isModuleFederationManifestRequest;
150
155
  exports.patchModuleFederationManifestPublicPath = __webpack_exports__.patchModuleFederationManifestPublicPath;
151
156
  exports.patchModuleFederationRemoteEntryPublicPath = __webpack_exports__.patchModuleFederationRemoteEntryPublicPath;
152
- exports.trimLeadingSlash = __webpack_exports__.trimLeadingSlash;
153
157
  for(var __rspack_i in __webpack_exports__)if (-1 === [
154
158
  "MODULE_FEDERATION_MANIFEST_FILE",
155
159
  "applyModuleFederationAssetHeaders",
156
160
  "getModuleFederationAssetList",
157
161
  "getModuleFederationRequestPath",
162
+ "isBackendModuleFederationManifestRequest",
158
163
  "isModuleFederationManifestRequest",
159
164
  "patchModuleFederationManifestPublicPath",
160
- "patchModuleFederationRemoteEntryPublicPath",
161
- "trimLeadingSlash"
165
+ "patchModuleFederationRemoteEntryPublicPath"
162
166
  ].indexOf(__rspack_i)) exports[__rspack_i] = __webpack_exports__[__rspack_i];
163
167
  Object.defineProperty(exports, '__esModule', {
164
168
  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,183 @@
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
+ servePublicDirectoryAsset: ()=>servePublicDirectoryAsset
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
+ const external_path_namespaceObject = require("path");
48
+ var external_path_default = /*#__PURE__*/ __webpack_require__.n(external_path_namespaceObject);
49
+ const external_staticModuleFederation_js_namespaceObject = require("./staticModuleFederation.js");
50
+ const external_staticPrecompressed_js_namespaceObject = require("./staticPrecompressed.js");
51
+ const getStaticMimeType = (filename)=>(0, mime_namespaceObject.getMimeType)(filename) ?? ('.cjs' === external_path_default().extname(filename).toLowerCase() ? "text/javascript; charset=UTF-8" : void 0);
52
+ const servePreCompressedPublicRouteAsset = async (c, pwd, route)=>{
53
+ const { entryPath } = route;
54
+ const originFilename = external_path_default().join(pwd, entryPath);
55
+ const preCompressedAsset = await (0, external_staticPrecompressed_js_namespaceObject.resolvePreCompressedAsset)(c, originFilename);
56
+ const filename = preCompressedAsset.selected?.filepath ?? originFilename;
57
+ const data = await fileReader_namespaceObject.fileReader.readFile(filename, 'buffer');
58
+ const mimeType = getStaticMimeType(originFilename);
59
+ if (null === data) return null;
60
+ const body = new Uint8Array(data.buffer, data.byteOffset, data.byteLength);
61
+ if (mimeType) c.header('Content-Type', mimeType);
62
+ Object.entries(route.responseHeaders || {}).forEach(([k, v])=>{
63
+ c.header(k, v);
64
+ });
65
+ (0, external_staticPrecompressed_js_namespaceObject.applyPreCompressedAssetHeaders)(c, preCompressedAsset);
66
+ c.header('Content-Length', String(data.byteLength));
67
+ return c.body(body, 200);
68
+ };
69
+ const isPathInside = (target, root)=>{
70
+ const relative = external_path_default().relative(external_path_default().resolve(root), external_path_default().resolve(target));
71
+ return '' === relative || !relative.startsWith(`..${external_path_default().sep}`) && '..' !== relative && !external_path_default().isAbsolute(relative);
72
+ };
73
+ const resolvePublicDirectoryAsset = async (pwd, pathname)=>{
74
+ let decodedPathname;
75
+ try {
76
+ decodedPathname = decodeURIComponent(pathname).replace(/\\/gu, '/');
77
+ } catch {
78
+ return null;
79
+ }
80
+ if (decodedPathname.includes('\0') || decodedPathname.split('/').includes('..')) return null;
81
+ const publicDirectory = external_path_default().join(pwd, 'public');
82
+ const filepath = external_path_default().resolve(publicDirectory, decodedPathname.replace(/^\/+/u, ''));
83
+ if (!isPathInside(filepath, publicDirectory)) return null;
84
+ try {
85
+ const [realPublicDirectory, realFilepath, stat] = await Promise.all([
86
+ utils_namespaceObject.fs.realpath(publicDirectory),
87
+ utils_namespaceObject.fs.realpath(filepath),
88
+ utils_namespaceObject.fs.stat(filepath)
89
+ ]);
90
+ if (!stat.isFile() || !isPathInside(realFilepath, realPublicDirectory)) return null;
91
+ return realFilepath;
92
+ } catch {
93
+ return null;
94
+ }
95
+ };
96
+ const servePublicDirectoryAsset = async (c, pwd)=>{
97
+ const method = c.req.raw.method.toUpperCase();
98
+ if ('GET' !== method && 'HEAD' !== method) return null;
99
+ const originFilename = await resolvePublicDirectoryAsset(pwd, c.req.path);
100
+ if (null === originFilename) return null;
101
+ const preCompressedAsset = await (0, external_staticPrecompressed_js_namespaceObject.resolvePreCompressedAsset)(c, originFilename);
102
+ const selectedFilename = preCompressedAsset.selected?.filepath ?? originFilename;
103
+ const publicDirectory = await utils_namespaceObject.fs.realpath(external_path_default().join(pwd, 'public'));
104
+ let realSelectedFilename;
105
+ try {
106
+ realSelectedFilename = await utils_namespaceObject.fs.realpath(selectedFilename);
107
+ } catch {
108
+ return null;
109
+ }
110
+ if (!isPathInside(realSelectedFilename, publicDirectory)) return null;
111
+ const data = await fileReader_namespaceObject.fileReader.readFileFromSystem(realSelectedFilename, 'buffer');
112
+ if (null === data) return null;
113
+ const mimeType = getStaticMimeType(originFilename);
114
+ if (mimeType) c.header('Content-Type', mimeType);
115
+ (0, external_staticPrecompressed_js_namespaceObject.applyPreCompressedAssetHeaders)(c, preCompressedAsset);
116
+ c.header('Content-Length', String(data.byteLength));
117
+ if ('HEAD' === method) return c.body(null, 200);
118
+ const body = new Uint8Array(data.buffer, data.byteOffset, data.byteLength);
119
+ return c.body(body, 200);
120
+ };
121
+ const createModuleFederationStaticServing = ({ pwd, pathPrefix })=>{
122
+ let moduleFederationAssetsPromise = null;
123
+ const getModuleFederationAssets = async ()=>{
124
+ if (!moduleFederationAssetsPromise) moduleFederationAssetsPromise = (0, external_staticModuleFederation_js_namespaceObject.getModuleFederationAssetList)(pwd);
125
+ return moduleFederationAssetsPromise;
126
+ };
127
+ const resolveRequest = async (pathname)=>{
128
+ const requestPath = (0, external_staticModuleFederation_js_namespaceObject.getModuleFederationRequestPath)(pathname, pathPrefix);
129
+ if (requestPath.includes('..')) return null;
130
+ const moduleFederationAssetMeta = await getModuleFederationAssets();
131
+ return {
132
+ requestPath,
133
+ isModuleFederationAsset: moduleFederationAssetMeta.assets.has(requestPath),
134
+ isModuleFederationRemoteEntry: moduleFederationAssetMeta.remoteEntries.has(requestPath)
135
+ };
136
+ };
137
+ const serveFile = async (c, filepath, moduleFederationAsset = false, moduleFederationRemoteEntry = false, requestPath = '')=>{
138
+ if (moduleFederationAsset) (0, external_staticModuleFederation_js_namespaceObject.applyModuleFederationAssetHeaders)(c);
139
+ const mimeType = getStaticMimeType(filepath);
140
+ if (mimeType) c.header('Content-Type', mimeType);
141
+ const shouldPatchManifest = moduleFederationAsset && (0, external_staticModuleFederation_js_namespaceObject.isModuleFederationManifestRequest)(requestPath) && !(0, external_staticModuleFederation_js_namespaceObject.isBackendModuleFederationManifestRequest)(requestPath);
142
+ const shouldPatchRemoteEntry = moduleFederationRemoteEntry;
143
+ const canUsePreCompressed = !shouldPatchManifest && !shouldPatchRemoteEntry;
144
+ const preCompressedAsset = canUsePreCompressed ? await (0, external_staticPrecompressed_js_namespaceObject.resolvePreCompressedAsset)(c, filepath) : {
145
+ selected: null,
146
+ hasVariant: false
147
+ };
148
+ const targetFilepath = preCompressedAsset.selected?.filepath ?? filepath;
149
+ const chunk = await fileReader_namespaceObject.fileReader.readFileFromSystem(targetFilepath, 'buffer');
150
+ if (null === chunk) return null;
151
+ const responseChunk = shouldPatchManifest ? (0, external_staticModuleFederation_js_namespaceObject.patchModuleFederationManifestPublicPath)(c, chunk, pathPrefix) : shouldPatchRemoteEntry ? (0, external_staticModuleFederation_js_namespaceObject.patchModuleFederationRemoteEntryPublicPath)(c, chunk, pathPrefix) : chunk;
152
+ (0, external_staticPrecompressed_js_namespaceObject.applyPreCompressedAssetHeaders)(c, preCompressedAsset);
153
+ c.header('Content-Length', String(responseChunk.byteLength));
154
+ const body = new Uint8Array(responseChunk.buffer, responseChunk.byteOffset, responseChunk.byteLength);
155
+ return c.body(body, 200);
156
+ };
157
+ const serveByPath = async (c, filepath, request, moduleFederationAsset = false, moduleFederationRemoteEntry = false)=>{
158
+ if (!isPathInside(filepath, pwd)) return null;
159
+ if (!await utils_namespaceObject.fs.pathExists(filepath)) return null;
160
+ return serveFile(c, filepath, moduleFederationAsset, moduleFederationRemoteEntry, request.requestPath);
161
+ };
162
+ const serveStaticHit = (c, request)=>serveByPath(c, external_path_default().join(pwd, request.requestPath), request, request.isModuleFederationAsset, request.isModuleFederationRemoteEntry);
163
+ const serveModuleFederationAsset = (c, request)=>{
164
+ if (!request.isModuleFederationAsset) return null;
165
+ return serveByPath(c, external_path_default().join(pwd, request.requestPath), request, true, request.isModuleFederationRemoteEntry);
166
+ };
167
+ return {
168
+ resolveRequest,
169
+ serveStaticHit,
170
+ serveModuleFederationAsset
171
+ };
172
+ };
173
+ exports.createModuleFederationStaticServing = __webpack_exports__.createModuleFederationStaticServing;
174
+ exports.servePreCompressedPublicRouteAsset = __webpack_exports__.servePreCompressedPublicRouteAsset;
175
+ exports.servePublicDirectoryAsset = __webpack_exports__.servePublicDirectoryAsset;
176
+ for(var __rspack_i in __webpack_exports__)if (-1 === [
177
+ "createModuleFederationStaticServing",
178
+ "servePreCompressedPublicRouteAsset",
179
+ "servePublicDirectoryAsset"
180
+ ].indexOf(__rspack_i)) exports[__rspack_i] = __webpack_exports__[__rspack_i];
181
+ Object.defineProperty(exports, '__esModule', {
182
+ value: true
183
+ });
@@ -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 { applyModuleFederationAssetHeaders, getModuleFederationAssetList, getModuleFederationRequestPath, isModuleFederationManifestRequest, patchModuleFederationManifestPublicPath, patchModuleFederationRemoteEntryPublicPath } from "./staticModuleFederation.mjs";
8
- import { applyPreCompressedAssetHeaders, resolvePreCompressedAsset } from "./staticPrecompressed.mjs";
4
+ import { createModuleFederationStaticServing, servePreCompressedPublicRouteAsset, servePublicDirectoryAsset } from "./staticServing.mjs";
9
5
  const serverStaticPlugin = ()=>({
10
6
  name: '@modern-js/plugin-server-static',
11
7
  setup (api) {
@@ -30,23 +26,11 @@ function createPublicMiddleware({ pwd, routes }) {
30
26
  return async (c, next)=>{
31
27
  const route = matchPublicRoute(c.req, routes);
32
28
  if (route) {
33
- const { entryPath } = route;
34
- const originFilename = path.join(pwd, entryPath);
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
  }
32
+ const generatedPublicAsset = await servePublicDirectoryAsset(c, pwd);
33
+ if (null !== generatedPublicAsset) return generatedPublicAsset;
50
34
  return await next();
51
35
  };
52
36
  }
@@ -96,54 +80,24 @@ function createStaticMiddleware(options) {
96
80
  pwd,
97
81
  routes: routes || []
98
82
  });
99
- let moduleFederationAssetsPromise = null;
100
- const getModuleFederationAssets = async ()=>{
101
- if (!moduleFederationAssetsPromise) moduleFederationAssetsPromise = getModuleFederationAssetList(pwd);
102
- return moduleFederationAssetsPromise;
103
- };
104
- const serveFile = async (c, filepath, moduleFederationAsset = false, moduleFederationRemoteEntry = false, requestPath = '')=>{
105
- if (moduleFederationAsset) applyModuleFederationAssetHeaders(c);
106
- const mimeType = getMimeType(filepath);
107
- if (mimeType) c.header('Content-Type', mimeType);
108
- const shouldPatchManifest = moduleFederationAsset && isModuleFederationManifestRequest(requestPath);
109
- const shouldPatchRemoteEntry = moduleFederationRemoteEntry;
110
- const canUsePreCompressed = !shouldPatchManifest && !shouldPatchRemoteEntry;
111
- const preCompressedAsset = canUsePreCompressed ? await resolvePreCompressedAsset(c, filepath) : {
112
- selected: null,
113
- hasVariant: false
114
- };
115
- const targetFilepath = preCompressedAsset.selected?.filepath ?? filepath;
116
- const chunk = await fileReader.readFileFromSystem(targetFilepath, 'buffer');
117
- if (null === chunk) return null;
118
- const responseChunk = shouldPatchManifest ? patchModuleFederationManifestPublicPath(c, chunk, pathPrefix) : shouldPatchRemoteEntry ? patchModuleFederationRemoteEntryPublicPath(c, chunk, pathPrefix) : chunk;
119
- applyPreCompressedAssetHeaders(c, preCompressedAsset);
120
- c.header('Content-Length', String(responseChunk.byteLength));
121
- const body = new Uint8Array(responseChunk.buffer, responseChunk.byteOffset, responseChunk.byteLength);
122
- return c.body(body, 200);
123
- };
83
+ const moduleFederationStaticServing = createModuleFederationStaticServing({
84
+ pwd,
85
+ pathPrefix
86
+ });
124
87
  return async (c, next)=>{
125
88
  const pageRoute = c.get('route');
126
89
  const pathname = c.req.path;
127
90
  if (pageRoute && '' === path.extname(pathname)) return next();
128
91
  const hit = staticPathRegExp.test(pathname);
129
- const requestPath = getModuleFederationRequestPath(pathname, pathPrefix);
130
- if (requestPath.includes('..')) return next();
131
- const moduleFederationAssetMeta = await getModuleFederationAssets();
132
- const isModuleFederationAsset = moduleFederationAssetMeta.assets.has(requestPath);
133
- const isModuleFederationRemoteEntry = moduleFederationAssetMeta.remoteEntry === requestPath;
134
- const serveByPath = async (filepath, moduleFederationAsset = false, moduleFederationRemoteEntry = false)=>{
135
- if (!await fs.pathExists(filepath)) return null;
136
- return serveFile(c, filepath, moduleFederationAsset, moduleFederationRemoteEntry, requestPath);
137
- };
92
+ const staticServingRequest = await moduleFederationStaticServing.resolveRequest(pathname);
93
+ if (null === staticServingRequest) return next();
138
94
  if (hit) {
139
- const response = await serveByPath(path.join(pwd, requestPath), isModuleFederationAsset, isModuleFederationRemoteEntry);
95
+ const response = await moduleFederationStaticServing.serveStaticHit(c, staticServingRequest);
140
96
  if (null !== response) return response;
141
97
  return next();
142
98
  }
143
- if (isModuleFederationAsset) {
144
- const response = await serveByPath(path.join(pwd, requestPath), true, isModuleFederationRemoteEntry);
145
- if (null !== response) return response;
146
- }
99
+ const moduleFederationResponse = await moduleFederationStaticServing.serveModuleFederationAsset(c, staticServingRequest);
100
+ if (null !== moduleFederationResponse) return moduleFederationResponse;
147
101
  return publicMiddleware(c, next);
148
102
  };
149
103
  }