@bleedingdev/modern-js-server-core 3.9.0-ultramodern.4 → 3.9.0-ultramodern.5
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/index.js +4 -0
- package/dist/cjs/adapters/node/plugins/static.js +107 -29
- package/dist/cjs/index.js +10 -34
- package/dist/cjs/plugins/compat/index.js +1 -0
- package/dist/cjs/serverBase.js +58 -17
- package/dist/cjs/utils/error.js +0 -10
- package/dist/esm/adapters/node/index.mjs +1 -0
- package/dist/esm/adapters/node/plugins/static.mjs +107 -32
- package/dist/esm/index.mjs +1 -1
- package/dist/esm/plugins/compat/index.mjs +1 -0
- package/dist/esm/serverBase.mjs +58 -17
- package/dist/esm/utils/error.mjs +0 -1
- package/dist/esm-node/adapters/node/index.mjs +1 -0
- package/dist/esm-node/adapters/node/plugins/static.mjs +107 -32
- package/dist/esm-node/index.mjs +1 -1
- package/dist/esm-node/plugins/compat/index.mjs +1 -0
- package/dist/esm-node/serverBase.mjs +58 -17
- package/dist/esm-node/utils/error.mjs +0 -1
- package/dist/types/adapters/node/index.d.ts +2 -0
- package/dist/types/adapters/node/plugins/static.d.ts +42 -4
- package/dist/types/index.d.ts +1 -2
- package/dist/types/serverBase.d.ts +6 -0
- package/dist/types/types/config/bff.d.ts +4 -15
- package/dist/types/types/config/server.d.ts +1 -3
- package/dist/types/types/plugins/plugin.d.ts +15 -1
- package/dist/types/utils/error.d.ts +0 -2
- package/package.json +5 -6
- package/dist/cjs/adapters/node/plugins/staticModuleFederation.js +0 -173
- package/dist/cjs/adapters/node/plugins/staticPrecompressed.js +0 -152
- package/dist/cjs/adapters/node/plugins/staticServing.js +0 -206
- package/dist/cjs/types/config/bffRuntime.js +0 -18
- package/dist/cjs/types/config/serverTelemetry.js +0 -18
- package/dist/esm/adapters/node/plugins/staticModuleFederation.mjs +0 -104
- package/dist/esm/adapters/node/plugins/staticPrecompressed.mjs +0 -111
- package/dist/esm/adapters/node/plugins/staticServing.mjs +0 -152
- package/dist/esm/types/config/bffRuntime.mjs +0 -0
- package/dist/esm/types/config/serverTelemetry.mjs +0 -0
- package/dist/esm-node/adapters/node/plugins/staticModuleFederation.mjs +0 -105
- package/dist/esm-node/adapters/node/plugins/staticPrecompressed.mjs +0 -112
- package/dist/esm-node/adapters/node/plugins/staticServing.mjs +0 -153
- package/dist/esm-node/types/config/bffRuntime.mjs +0 -1
- package/dist/esm-node/types/config/serverTelemetry.mjs +0 -1
- package/dist/types/adapters/node/plugins/staticModuleFederation.d.ts +0 -13
- package/dist/types/adapters/node/plugins/staticPrecompressed.d.ts +0 -13
- package/dist/types/adapters/node/plugins/staticServing.d.ts +0 -25
- package/dist/types/types/config/bffRuntime.d.ts +0 -116
- package/dist/types/types/config/serverTelemetry.d.ts +0 -319
|
@@ -1,206 +0,0 @@
|
|
|
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 MODULE_FEDERATION_ASSET_REFRESH_INTERVAL_MS = 1000;
|
|
52
|
-
const getStaticMimeType = (filename)=>(0, mime_namespaceObject.getMimeType)(filename) ?? ('.cjs' === external_path_default().extname(filename).toLowerCase() ? "text/javascript; charset=UTF-8" : void 0);
|
|
53
|
-
const servePreCompressedPublicRouteAsset = async (c, pwd, route)=>{
|
|
54
|
-
const { entryPath } = route;
|
|
55
|
-
const originFilename = external_path_default().join(pwd, entryPath);
|
|
56
|
-
const preCompressedAsset = await (0, external_staticPrecompressed_js_namespaceObject.resolvePreCompressedAsset)(c, originFilename);
|
|
57
|
-
if (!preCompressedAsset.acceptable) {
|
|
58
|
-
(0, external_staticPrecompressed_js_namespaceObject.applyPreCompressedAssetHeaders)(c, preCompressedAsset);
|
|
59
|
-
return c.body(null, 406);
|
|
60
|
-
}
|
|
61
|
-
const filename = preCompressedAsset.selected?.filepath ?? originFilename;
|
|
62
|
-
const data = await fileReader_namespaceObject.fileReader.readFile(filename, 'buffer');
|
|
63
|
-
const mimeType = getStaticMimeType(originFilename);
|
|
64
|
-
if (null === data) return null;
|
|
65
|
-
const body = new Uint8Array(data.buffer, data.byteOffset, data.byteLength);
|
|
66
|
-
if (mimeType) c.header('Content-Type', mimeType);
|
|
67
|
-
Object.entries(route.responseHeaders || {}).forEach(([k, v])=>{
|
|
68
|
-
c.header(k, v);
|
|
69
|
-
});
|
|
70
|
-
(0, external_staticPrecompressed_js_namespaceObject.applyPreCompressedAssetHeaders)(c, preCompressedAsset);
|
|
71
|
-
c.header('Content-Length', String(data.byteLength));
|
|
72
|
-
return c.body(body, 200);
|
|
73
|
-
};
|
|
74
|
-
const isPathInside = (target, root)=>{
|
|
75
|
-
const relative = external_path_default().relative(external_path_default().resolve(root), external_path_default().resolve(target));
|
|
76
|
-
return '' === relative || !relative.startsWith(`..${external_path_default().sep}`) && '..' !== relative && !external_path_default().isAbsolute(relative);
|
|
77
|
-
};
|
|
78
|
-
const resolvePublicDirectoryAsset = async (pwd, pathname)=>{
|
|
79
|
-
let decodedPathname;
|
|
80
|
-
try {
|
|
81
|
-
decodedPathname = decodeURIComponent(pathname).replace(/\\/gu, '/');
|
|
82
|
-
} catch {
|
|
83
|
-
return null;
|
|
84
|
-
}
|
|
85
|
-
if (decodedPathname.includes('\0') || decodedPathname.split('/').includes('..')) return null;
|
|
86
|
-
const publicDirectory = external_path_default().join(pwd, 'public');
|
|
87
|
-
const filepath = external_path_default().resolve(publicDirectory, decodedPathname.replace(/^\/+/u, ''));
|
|
88
|
-
if (!isPathInside(filepath, publicDirectory)) return null;
|
|
89
|
-
try {
|
|
90
|
-
const [realPublicDirectory, realFilepath, stat] = await Promise.all([
|
|
91
|
-
utils_namespaceObject.fs.realpath(publicDirectory),
|
|
92
|
-
utils_namespaceObject.fs.realpath(filepath),
|
|
93
|
-
utils_namespaceObject.fs.stat(filepath)
|
|
94
|
-
]);
|
|
95
|
-
if (!stat.isFile() || !isPathInside(realFilepath, realPublicDirectory)) return null;
|
|
96
|
-
return realFilepath;
|
|
97
|
-
} catch {
|
|
98
|
-
return null;
|
|
99
|
-
}
|
|
100
|
-
};
|
|
101
|
-
const servePublicDirectoryAsset = async (c, pwd)=>{
|
|
102
|
-
const method = c.req.raw.method.toUpperCase();
|
|
103
|
-
if ('GET' !== method && 'HEAD' !== method) return null;
|
|
104
|
-
const originFilename = await resolvePublicDirectoryAsset(pwd, c.req.path);
|
|
105
|
-
if (null === originFilename) return null;
|
|
106
|
-
const preCompressedAsset = await (0, external_staticPrecompressed_js_namespaceObject.resolvePreCompressedAsset)(c, originFilename);
|
|
107
|
-
if (!preCompressedAsset.acceptable) {
|
|
108
|
-
(0, external_staticPrecompressed_js_namespaceObject.applyPreCompressedAssetHeaders)(c, preCompressedAsset);
|
|
109
|
-
return c.body(null, 406);
|
|
110
|
-
}
|
|
111
|
-
const selectedFilename = preCompressedAsset.selected?.filepath ?? originFilename;
|
|
112
|
-
const publicDirectory = await utils_namespaceObject.fs.realpath(external_path_default().join(pwd, 'public'));
|
|
113
|
-
let realSelectedFilename;
|
|
114
|
-
try {
|
|
115
|
-
realSelectedFilename = await utils_namespaceObject.fs.realpath(selectedFilename);
|
|
116
|
-
} catch {
|
|
117
|
-
return null;
|
|
118
|
-
}
|
|
119
|
-
if (!isPathInside(realSelectedFilename, publicDirectory)) return null;
|
|
120
|
-
const data = await fileReader_namespaceObject.fileReader.readFileFromSystem(realSelectedFilename, 'buffer');
|
|
121
|
-
if (null === data) return null;
|
|
122
|
-
const mimeType = getStaticMimeType(originFilename);
|
|
123
|
-
if (mimeType) c.header('Content-Type', mimeType);
|
|
124
|
-
(0, external_staticPrecompressed_js_namespaceObject.applyPreCompressedAssetHeaders)(c, preCompressedAsset);
|
|
125
|
-
c.header('Content-Length', String(data.byteLength));
|
|
126
|
-
if ('HEAD' === method) return c.body(null, 200);
|
|
127
|
-
const body = new Uint8Array(data.buffer, data.byteOffset, data.byteLength);
|
|
128
|
-
return c.body(body, 200);
|
|
129
|
-
};
|
|
130
|
-
const createModuleFederationStaticServing = ({ pwd, pathPrefix })=>{
|
|
131
|
-
let moduleFederationAssets = null;
|
|
132
|
-
let moduleFederationAssetsExpiresAt = 0;
|
|
133
|
-
let moduleFederationAssetsRefresh = null;
|
|
134
|
-
const getModuleFederationAssets = async ()=>{
|
|
135
|
-
if (moduleFederationAssets && Date.now() < moduleFederationAssetsExpiresAt) return moduleFederationAssets;
|
|
136
|
-
if (!moduleFederationAssetsRefresh) moduleFederationAssetsRefresh = (0, external_staticModuleFederation_js_namespaceObject.getModuleFederationAssetList)(pwd).then((assets)=>{
|
|
137
|
-
moduleFederationAssets = assets;
|
|
138
|
-
moduleFederationAssetsExpiresAt = Date.now() + MODULE_FEDERATION_ASSET_REFRESH_INTERVAL_MS;
|
|
139
|
-
return assets;
|
|
140
|
-
}).finally(()=>{
|
|
141
|
-
moduleFederationAssetsRefresh = null;
|
|
142
|
-
});
|
|
143
|
-
return moduleFederationAssetsRefresh;
|
|
144
|
-
};
|
|
145
|
-
const resolveRequest = async (pathname)=>{
|
|
146
|
-
const requestPath = (0, external_staticModuleFederation_js_namespaceObject.getModuleFederationRequestPath)(pathname, pathPrefix);
|
|
147
|
-
if (requestPath.includes('..')) return null;
|
|
148
|
-
const moduleFederationAssetMeta = await getModuleFederationAssets();
|
|
149
|
-
return {
|
|
150
|
-
requestPath,
|
|
151
|
-
isModuleFederationAsset: moduleFederationAssetMeta.assets.has(requestPath),
|
|
152
|
-
isModuleFederationRemoteEntry: moduleFederationAssetMeta.remoteEntries.has(requestPath)
|
|
153
|
-
};
|
|
154
|
-
};
|
|
155
|
-
const serveFile = async (c, filepath, moduleFederationAsset = false, moduleFederationRemoteEntry = false, requestPath = '')=>{
|
|
156
|
-
if (moduleFederationAsset) (0, external_staticModuleFederation_js_namespaceObject.applyModuleFederationAssetHeaders)(c);
|
|
157
|
-
const mimeType = getStaticMimeType(filepath);
|
|
158
|
-
if (mimeType) c.header('Content-Type', mimeType);
|
|
159
|
-
const shouldPatchManifest = moduleFederationAsset && (0, external_staticModuleFederation_js_namespaceObject.isModuleFederationManifestRequest)(requestPath) && !(0, external_staticModuleFederation_js_namespaceObject.isBackendModuleFederationManifestRequest)(requestPath);
|
|
160
|
-
const shouldPatchRemoteEntry = moduleFederationRemoteEntry;
|
|
161
|
-
const canUsePreCompressed = !shouldPatchManifest && !shouldPatchRemoteEntry;
|
|
162
|
-
const preCompressedAsset = canUsePreCompressed ? await (0, external_staticPrecompressed_js_namespaceObject.resolvePreCompressedAsset)(c, filepath) : {
|
|
163
|
-
selected: null,
|
|
164
|
-
hasVariant: false,
|
|
165
|
-
acceptable: true
|
|
166
|
-
};
|
|
167
|
-
if (!preCompressedAsset.acceptable) {
|
|
168
|
-
(0, external_staticPrecompressed_js_namespaceObject.applyPreCompressedAssetHeaders)(c, preCompressedAsset);
|
|
169
|
-
return c.body(null, 406);
|
|
170
|
-
}
|
|
171
|
-
const targetFilepath = preCompressedAsset.selected?.filepath ?? filepath;
|
|
172
|
-
const chunk = await fileReader_namespaceObject.fileReader.readFileFromSystem(targetFilepath, 'buffer');
|
|
173
|
-
if (null === chunk) return null;
|
|
174
|
-
const responseChunk = shouldPatchManifest ? (0, external_staticModuleFederation_js_namespaceObject.patchModuleFederationManifestPublicPath)(c, chunk, pathPrefix) : shouldPatchRemoteEntry ? (0, external_staticModuleFederation_js_namespaceObject.patchModuleFederationRemoteEntryPublicPath)(c, chunk, pathPrefix) : chunk;
|
|
175
|
-
(0, external_staticPrecompressed_js_namespaceObject.applyPreCompressedAssetHeaders)(c, preCompressedAsset);
|
|
176
|
-
c.header('Content-Length', String(responseChunk.byteLength));
|
|
177
|
-
const body = new Uint8Array(responseChunk.buffer, responseChunk.byteOffset, responseChunk.byteLength);
|
|
178
|
-
return c.body(body, 200);
|
|
179
|
-
};
|
|
180
|
-
const serveByPath = async (c, filepath, request, moduleFederationAsset = false, moduleFederationRemoteEntry = false)=>{
|
|
181
|
-
if (!isPathInside(filepath, pwd)) return null;
|
|
182
|
-
if (!await utils_namespaceObject.fs.pathExists(filepath)) return null;
|
|
183
|
-
return serveFile(c, filepath, moduleFederationAsset, moduleFederationRemoteEntry, request.requestPath);
|
|
184
|
-
};
|
|
185
|
-
const serveStaticHit = (c, request)=>serveByPath(c, external_path_default().join(pwd, request.requestPath), request, request.isModuleFederationAsset, request.isModuleFederationRemoteEntry);
|
|
186
|
-
const serveModuleFederationAsset = (c, request)=>{
|
|
187
|
-
if (!request.isModuleFederationAsset) return null;
|
|
188
|
-
return serveByPath(c, external_path_default().join(pwd, request.requestPath), request, true, request.isModuleFederationRemoteEntry);
|
|
189
|
-
};
|
|
190
|
-
return {
|
|
191
|
-
resolveRequest,
|
|
192
|
-
serveStaticHit,
|
|
193
|
-
serveModuleFederationAsset
|
|
194
|
-
};
|
|
195
|
-
};
|
|
196
|
-
exports.createModuleFederationStaticServing = __webpack_exports__.createModuleFederationStaticServing;
|
|
197
|
-
exports.servePreCompressedPublicRouteAsset = __webpack_exports__.servePreCompressedPublicRouteAsset;
|
|
198
|
-
exports.servePublicDirectoryAsset = __webpack_exports__.servePublicDirectoryAsset;
|
|
199
|
-
for(var __rspack_i in __webpack_exports__)if (-1 === [
|
|
200
|
-
"createModuleFederationStaticServing",
|
|
201
|
-
"servePreCompressedPublicRouteAsset",
|
|
202
|
-
"servePublicDirectoryAsset"
|
|
203
|
-
].indexOf(__rspack_i)) exports[__rspack_i] = __webpack_exports__[__rspack_i];
|
|
204
|
-
Object.defineProperty(exports, '__esModule', {
|
|
205
|
-
value: true
|
|
206
|
-
});
|
|
@@ -1,18 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
var __webpack_require__ = {};
|
|
3
|
-
(()=>{
|
|
4
|
-
__webpack_require__.r = (exports1)=>{
|
|
5
|
-
if ("u" > typeof Symbol && Symbol.toStringTag) Object.defineProperty(exports1, Symbol.toStringTag, {
|
|
6
|
-
value: 'Module'
|
|
7
|
-
});
|
|
8
|
-
Object.defineProperty(exports1, '__esModule', {
|
|
9
|
-
value: true
|
|
10
|
-
});
|
|
11
|
-
};
|
|
12
|
-
})();
|
|
13
|
-
var __webpack_exports__ = {};
|
|
14
|
-
__webpack_require__.r(__webpack_exports__);
|
|
15
|
-
for(var __rspack_i in __webpack_exports__)exports[__rspack_i] = __webpack_exports__[__rspack_i];
|
|
16
|
-
Object.defineProperty(exports, '__esModule', {
|
|
17
|
-
value: true
|
|
18
|
-
});
|
|
@@ -1,18 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
var __webpack_require__ = {};
|
|
3
|
-
(()=>{
|
|
4
|
-
__webpack_require__.r = (exports1)=>{
|
|
5
|
-
if ("u" > typeof Symbol && Symbol.toStringTag) Object.defineProperty(exports1, Symbol.toStringTag, {
|
|
6
|
-
value: 'Module'
|
|
7
|
-
});
|
|
8
|
-
Object.defineProperty(exports1, '__esModule', {
|
|
9
|
-
value: true
|
|
10
|
-
});
|
|
11
|
-
};
|
|
12
|
-
})();
|
|
13
|
-
var __webpack_exports__ = {};
|
|
14
|
-
__webpack_require__.r(__webpack_exports__);
|
|
15
|
-
for(var __rspack_i in __webpack_exports__)exports[__rspack_i] = __webpack_exports__[__rspack_i];
|
|
16
|
-
Object.defineProperty(exports, '__esModule', {
|
|
17
|
-
value: true
|
|
18
|
-
});
|
|
@@ -1,104 +0,0 @@
|
|
|
1
|
-
import { fileReader } from "@modern-js/runtime-utils/fileReader";
|
|
2
|
-
import { fs } from "@modern-js/utils";
|
|
3
|
-
import path from "path";
|
|
4
|
-
const MODULE_FEDERATION_MANIFEST_FILE = 'mf-manifest.json';
|
|
5
|
-
const BACKEND_MODULE_FEDERATION_MANIFEST_FILE = 'backend-mf-manifest.json';
|
|
6
|
-
const MODULE_FEDERATION_MANIFEST_FILES = [
|
|
7
|
-
MODULE_FEDERATION_MANIFEST_FILE,
|
|
8
|
-
BACKEND_MODULE_FEDERATION_MANIFEST_FILE
|
|
9
|
-
];
|
|
10
|
-
const MODULE_FEDERATION_OPTIONAL_FILES = [
|
|
11
|
-
'mf-stats.json'
|
|
12
|
-
];
|
|
13
|
-
const trimLeadingSlash = (value)=>value.replace(/^\/+/, '');
|
|
14
|
-
const getModuleFederationRequestPath = (pathname, pathPrefix)=>{
|
|
15
|
-
const normalizedPrefix = `/${trimLeadingSlash(pathPrefix)}`.replace(/\/+$/u, '');
|
|
16
|
-
const requestPath = normalizedPrefix && (pathname === normalizedPrefix || pathname.startsWith(`${normalizedPrefix}/`)) ? pathname.slice(normalizedPrefix.length) : pathname;
|
|
17
|
-
return trimLeadingSlash(requestPath);
|
|
18
|
-
};
|
|
19
|
-
const isModuleFederationManifestRequest = (requestPath)=>MODULE_FEDERATION_MANIFEST_FILES.includes(requestPath);
|
|
20
|
-
const isBackendModuleFederationManifestRequest = (requestPath)=>requestPath === BACKEND_MODULE_FEDERATION_MANIFEST_FILE;
|
|
21
|
-
const applyModuleFederationAssetHeaders = (c)=>{
|
|
22
|
-
c.header('Access-Control-Allow-Origin', '*');
|
|
23
|
-
c.header('Access-Control-Allow-Headers', '*');
|
|
24
|
-
c.header('Access-Control-Allow-Methods', 'GET,HEAD,OPTIONS');
|
|
25
|
-
};
|
|
26
|
-
const joinModuleFederationAssetPath = (assetPath, assetName)=>{
|
|
27
|
-
if (!assetName) return '';
|
|
28
|
-
return trimLeadingSlash(path.posix.join(assetPath || '', assetName));
|
|
29
|
-
};
|
|
30
|
-
const appendModuleFederationAsset = (set, assetPath)=>{
|
|
31
|
-
if (assetPath) set.add(trimLeadingSlash(assetPath));
|
|
32
|
-
};
|
|
33
|
-
const appendModuleFederationAssets = (set, assets)=>{
|
|
34
|
-
assets?.js?.sync?.forEach((asset)=>appendModuleFederationAsset(set, asset));
|
|
35
|
-
assets?.js?.async?.forEach((asset)=>appendModuleFederationAsset(set, asset));
|
|
36
|
-
assets?.css?.sync?.forEach((asset)=>appendModuleFederationAsset(set, asset));
|
|
37
|
-
assets?.css?.async?.forEach((asset)=>appendModuleFederationAsset(set, asset));
|
|
38
|
-
};
|
|
39
|
-
const hasAbsoluteProtocol = (value)=>/^https?:\/\//i.test(value) || value.startsWith('//');
|
|
40
|
-
const ensureLeadingSlash = (value)=>{
|
|
41
|
-
if ('' === value) return '/';
|
|
42
|
-
return value.startsWith('/') ? value : `/${value}`;
|
|
43
|
-
};
|
|
44
|
-
const ensureTrailingSlash = (value)=>value.endsWith('/') ? value : `${value}/`;
|
|
45
|
-
const patchModuleFederationManifestPublicPath = (c, manifestBuffer, pathPrefix)=>{
|
|
46
|
-
try {
|
|
47
|
-
const manifest = JSON.parse(manifestBuffer.toString('utf-8'));
|
|
48
|
-
const publicPath = manifest.metaData?.publicPath;
|
|
49
|
-
if (!publicPath || hasAbsoluteProtocol(publicPath)) return manifestBuffer;
|
|
50
|
-
const requestURL = new URL(c.req.url);
|
|
51
|
-
const prefixPath = ensureTrailingSlash(ensureLeadingSlash(pathPrefix || '/'));
|
|
52
|
-
manifest.metaData = {
|
|
53
|
-
...manifest.metaData,
|
|
54
|
-
publicPath: `${requestURL.origin}${prefixPath}`
|
|
55
|
-
};
|
|
56
|
-
return Buffer.from(JSON.stringify(manifest), 'utf-8');
|
|
57
|
-
} catch {
|
|
58
|
-
return manifestBuffer;
|
|
59
|
-
}
|
|
60
|
-
};
|
|
61
|
-
const patchModuleFederationRemoteEntryPublicPath = (c, remoteEntryBuffer, pathPrefix)=>{
|
|
62
|
-
const requestURL = new URL(c.req.url);
|
|
63
|
-
const prefixPath = ensureTrailingSlash(ensureLeadingSlash(pathPrefix || '/'));
|
|
64
|
-
const publicPath = `${requestURL.origin}${prefixPath}`;
|
|
65
|
-
const source = remoteEntryBuffer.toString('utf-8');
|
|
66
|
-
const patched = source.replace(/__webpack_require__\.p\s*=\s*(['"`])[^'"`]*\1;/, `__webpack_require__.p = ${JSON.stringify(publicPath)};`).replace(/__rspack_require__\.p\s*=\s*(['"`])[^'"`]*\1;/, `__rspack_require__.p = ${JSON.stringify(publicPath)};`);
|
|
67
|
-
if (patched === source) return remoteEntryBuffer;
|
|
68
|
-
return Buffer.from(patched, 'utf-8');
|
|
69
|
-
};
|
|
70
|
-
const getModuleFederationAssetList = async (pwd)=>{
|
|
71
|
-
const assets = new Set();
|
|
72
|
-
const remoteEntries = new Set();
|
|
73
|
-
let manifestFound = false;
|
|
74
|
-
for (const manifestFile of MODULE_FEDERATION_MANIFEST_FILES){
|
|
75
|
-
const manifestPath = path.join(pwd, manifestFile);
|
|
76
|
-
if (!await fs.pathExists(manifestPath)) continue;
|
|
77
|
-
manifestFound = true;
|
|
78
|
-
assets.add(manifestFile);
|
|
79
|
-
const manifestBuffer = await fileReader.readFileFromSystem(manifestPath, 'buffer');
|
|
80
|
-
if (null !== manifestBuffer) try {
|
|
81
|
-
const manifest = JSON.parse(manifestBuffer.toString('utf-8'));
|
|
82
|
-
const remoteEntry = joinModuleFederationAssetPath(manifest.metaData?.remoteEntry?.path, manifest.metaData?.remoteEntry?.name);
|
|
83
|
-
const dtsZip = joinModuleFederationAssetPath(manifest.metaData?.types?.path, manifest.metaData?.types?.zip);
|
|
84
|
-
const dtsApi = joinModuleFederationAssetPath(manifest.metaData?.types?.path, manifest.metaData?.types?.api);
|
|
85
|
-
if (remoteEntry) {
|
|
86
|
-
assets.add(remoteEntry);
|
|
87
|
-
remoteEntries.add(remoteEntry);
|
|
88
|
-
}
|
|
89
|
-
appendModuleFederationAsset(assets, dtsZip);
|
|
90
|
-
appendModuleFederationAsset(assets, dtsApi);
|
|
91
|
-
manifest.shared?.forEach((item)=>appendModuleFederationAssets(assets, item.assets));
|
|
92
|
-
manifest.remotes?.forEach((item)=>appendModuleFederationAssets(assets, item.assets));
|
|
93
|
-
manifest.exposes?.forEach((item)=>appendModuleFederationAssets(assets, item.assets));
|
|
94
|
-
} catch {}
|
|
95
|
-
}
|
|
96
|
-
if (manifestFound) {
|
|
97
|
-
for (const filename of MODULE_FEDERATION_OPTIONAL_FILES)if (await fs.pathExists(path.join(pwd, filename))) assets.add(filename);
|
|
98
|
-
}
|
|
99
|
-
return {
|
|
100
|
-
assets,
|
|
101
|
-
remoteEntries
|
|
102
|
-
};
|
|
103
|
-
};
|
|
104
|
-
export { MODULE_FEDERATION_MANIFEST_FILE, applyModuleFederationAssetHeaders, getModuleFederationAssetList, getModuleFederationRequestPath, isBackendModuleFederationManifestRequest, isModuleFederationManifestRequest, patchModuleFederationManifestPublicPath, patchModuleFederationRemoteEntryPublicPath };
|
|
@@ -1,111 +0,0 @@
|
|
|
1
|
-
import { fs } from "@modern-js/utils";
|
|
2
|
-
const PRE_COMPRESSED_ASSET_EXTENSIONS = {
|
|
3
|
-
br: '.br',
|
|
4
|
-
gzip: '.gz'
|
|
5
|
-
};
|
|
6
|
-
const PRE_COMPRESSED_SUPPORTED_ENCODINGS = [
|
|
7
|
-
'br',
|
|
8
|
-
'gzip'
|
|
9
|
-
];
|
|
10
|
-
const QUALITY_VALUE_PATTERN = /^(?:0(?:\.\d{0,3})?|1(?:\.0{0,3})?)$/u;
|
|
11
|
-
const parseAcceptEncoding = (value)=>value.split(',').map((item)=>item.trim()).filter(Boolean).map((item)=>{
|
|
12
|
-
const [rawName, ...params] = item.split(';');
|
|
13
|
-
const name = rawName.trim().toLowerCase();
|
|
14
|
-
let q = 1;
|
|
15
|
-
let qualitySeen = false;
|
|
16
|
-
for (const param of params){
|
|
17
|
-
const [key, rawValue] = param.split('=').map((v)=>v.trim());
|
|
18
|
-
if ('q' === key.toLowerCase()) {
|
|
19
|
-
if (qualitySeen || null == rawValue || !QUALITY_VALUE_PATTERN.test(rawValue)) {
|
|
20
|
-
q = 0;
|
|
21
|
-
break;
|
|
22
|
-
}
|
|
23
|
-
qualitySeen = true;
|
|
24
|
-
q = Number(rawValue);
|
|
25
|
-
}
|
|
26
|
-
}
|
|
27
|
-
return {
|
|
28
|
-
name,
|
|
29
|
-
q
|
|
30
|
-
};
|
|
31
|
-
});
|
|
32
|
-
const getAcceptedRepresentations = (value)=>{
|
|
33
|
-
if (!value) return [
|
|
34
|
-
'identity'
|
|
35
|
-
];
|
|
36
|
-
const parsed = parseAcceptEncoding(value);
|
|
37
|
-
const qualityByEncoding = new Map();
|
|
38
|
-
let wildcardQuality;
|
|
39
|
-
for (const { name, q } of parsed){
|
|
40
|
-
if ('*' === name) {
|
|
41
|
-
wildcardQuality = q;
|
|
42
|
-
continue;
|
|
43
|
-
}
|
|
44
|
-
qualityByEncoding.set(name, q);
|
|
45
|
-
}
|
|
46
|
-
const getQuality = (encoding)=>{
|
|
47
|
-
const explicit = qualityByEncoding.get(encoding);
|
|
48
|
-
if (void 0 !== explicit) return explicit;
|
|
49
|
-
return wildcardQuality ?? 0;
|
|
50
|
-
};
|
|
51
|
-
const identityQuality = qualityByEncoding.get('identity') ?? (0 === wildcardQuality ? 0 : 1);
|
|
52
|
-
return [
|
|
53
|
-
...PRE_COMPRESSED_SUPPORTED_ENCODINGS.map((encoding)=>({
|
|
54
|
-
encoding,
|
|
55
|
-
quality: getQuality(encoding)
|
|
56
|
-
})),
|
|
57
|
-
{
|
|
58
|
-
encoding: 'identity',
|
|
59
|
-
quality: identityQuality
|
|
60
|
-
}
|
|
61
|
-
].filter((item)=>item.quality > 0).sort((a, b)=>b.quality - a.quality).map((item)=>item.encoding);
|
|
62
|
-
};
|
|
63
|
-
const appendVaryHeader = (c, value)=>{
|
|
64
|
-
const current = c.res.headers.get('Vary');
|
|
65
|
-
if (!current) return void c.header('Vary', value);
|
|
66
|
-
const values = current.split(',').map((item)=>item.trim().toLowerCase()).filter(Boolean);
|
|
67
|
-
if (!values.includes(value.toLowerCase())) c.header('Vary', `${current}, ${value}`);
|
|
68
|
-
};
|
|
69
|
-
const resolvePreCompressedAsset = async (c, filepath)=>{
|
|
70
|
-
const brPath = `${filepath}${PRE_COMPRESSED_ASSET_EXTENSIONS.br}`;
|
|
71
|
-
const gzipPath = `${filepath}${PRE_COMPRESSED_ASSET_EXTENSIONS.gzip}`;
|
|
72
|
-
const [hasBr, hasGzip] = await Promise.all([
|
|
73
|
-
fs.pathExists(brPath),
|
|
74
|
-
fs.pathExists(gzipPath)
|
|
75
|
-
]);
|
|
76
|
-
const hasVariant = hasBr || hasGzip;
|
|
77
|
-
const acceptedRepresentations = getAcceptedRepresentations(c.req.header('accept-encoding'));
|
|
78
|
-
for (const encoding of acceptedRepresentations){
|
|
79
|
-
if ('identity' === encoding) return {
|
|
80
|
-
selected: null,
|
|
81
|
-
hasVariant,
|
|
82
|
-
acceptable: true
|
|
83
|
-
};
|
|
84
|
-
if ('br' === encoding && hasBr) return {
|
|
85
|
-
selected: {
|
|
86
|
-
filepath: brPath,
|
|
87
|
-
encoding
|
|
88
|
-
},
|
|
89
|
-
hasVariant: true,
|
|
90
|
-
acceptable: true
|
|
91
|
-
};
|
|
92
|
-
if ('gzip' === encoding && hasGzip) return {
|
|
93
|
-
selected: {
|
|
94
|
-
filepath: gzipPath,
|
|
95
|
-
encoding
|
|
96
|
-
},
|
|
97
|
-
hasVariant: true,
|
|
98
|
-
acceptable: true
|
|
99
|
-
};
|
|
100
|
-
}
|
|
101
|
-
return {
|
|
102
|
-
selected: null,
|
|
103
|
-
hasVariant,
|
|
104
|
-
acceptable: false
|
|
105
|
-
};
|
|
106
|
-
};
|
|
107
|
-
const applyPreCompressedAssetHeaders = (c, preCompressedAsset)=>{
|
|
108
|
-
if (preCompressedAsset.hasVariant || !preCompressedAsset.acceptable) appendVaryHeader(c, 'Accept-Encoding');
|
|
109
|
-
if (preCompressedAsset.selected) c.header('Content-Encoding', preCompressedAsset.selected.encoding);
|
|
110
|
-
};
|
|
111
|
-
export { applyPreCompressedAssetHeaders, resolvePreCompressedAsset };
|
|
@@ -1,152 +0,0 @@
|
|
|
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, isBackendModuleFederationManifestRequest, isModuleFederationManifestRequest, patchModuleFederationManifestPublicPath, patchModuleFederationRemoteEntryPublicPath } from "./staticModuleFederation.mjs";
|
|
6
|
-
import { applyPreCompressedAssetHeaders, resolvePreCompressedAsset } from "./staticPrecompressed.mjs";
|
|
7
|
-
const MODULE_FEDERATION_ASSET_REFRESH_INTERVAL_MS = 1000;
|
|
8
|
-
const getStaticMimeType = (filename)=>getMimeType(filename) ?? ('.cjs' === path.extname(filename).toLowerCase() ? "text/javascript; charset=UTF-8" : void 0);
|
|
9
|
-
const servePreCompressedPublicRouteAsset = async (c, pwd, route)=>{
|
|
10
|
-
const { entryPath } = route;
|
|
11
|
-
const originFilename = path.join(pwd, entryPath);
|
|
12
|
-
const preCompressedAsset = await resolvePreCompressedAsset(c, originFilename);
|
|
13
|
-
if (!preCompressedAsset.acceptable) {
|
|
14
|
-
applyPreCompressedAssetHeaders(c, preCompressedAsset);
|
|
15
|
-
return c.body(null, 406);
|
|
16
|
-
}
|
|
17
|
-
const filename = preCompressedAsset.selected?.filepath ?? originFilename;
|
|
18
|
-
const data = await fileReader.readFile(filename, 'buffer');
|
|
19
|
-
const mimeType = getStaticMimeType(originFilename);
|
|
20
|
-
if (null === data) return null;
|
|
21
|
-
const body = new Uint8Array(data.buffer, data.byteOffset, data.byteLength);
|
|
22
|
-
if (mimeType) c.header('Content-Type', mimeType);
|
|
23
|
-
Object.entries(route.responseHeaders || {}).forEach(([k, v])=>{
|
|
24
|
-
c.header(k, v);
|
|
25
|
-
});
|
|
26
|
-
applyPreCompressedAssetHeaders(c, preCompressedAsset);
|
|
27
|
-
c.header('Content-Length', String(data.byteLength));
|
|
28
|
-
return c.body(body, 200);
|
|
29
|
-
};
|
|
30
|
-
const isPathInside = (target, root)=>{
|
|
31
|
-
const relative = path.relative(path.resolve(root), path.resolve(target));
|
|
32
|
-
return '' === relative || !relative.startsWith(`..${path.sep}`) && '..' !== relative && !path.isAbsolute(relative);
|
|
33
|
-
};
|
|
34
|
-
const resolvePublicDirectoryAsset = async (pwd, pathname)=>{
|
|
35
|
-
let decodedPathname;
|
|
36
|
-
try {
|
|
37
|
-
decodedPathname = decodeURIComponent(pathname).replace(/\\/gu, '/');
|
|
38
|
-
} catch {
|
|
39
|
-
return null;
|
|
40
|
-
}
|
|
41
|
-
if (decodedPathname.includes('\0') || decodedPathname.split('/').includes('..')) return null;
|
|
42
|
-
const publicDirectory = path.join(pwd, 'public');
|
|
43
|
-
const filepath = path.resolve(publicDirectory, decodedPathname.replace(/^\/+/u, ''));
|
|
44
|
-
if (!isPathInside(filepath, publicDirectory)) return null;
|
|
45
|
-
try {
|
|
46
|
-
const [realPublicDirectory, realFilepath, stat] = await Promise.all([
|
|
47
|
-
fs.realpath(publicDirectory),
|
|
48
|
-
fs.realpath(filepath),
|
|
49
|
-
fs.stat(filepath)
|
|
50
|
-
]);
|
|
51
|
-
if (!stat.isFile() || !isPathInside(realFilepath, realPublicDirectory)) return null;
|
|
52
|
-
return realFilepath;
|
|
53
|
-
} catch {
|
|
54
|
-
return null;
|
|
55
|
-
}
|
|
56
|
-
};
|
|
57
|
-
const servePublicDirectoryAsset = async (c, pwd)=>{
|
|
58
|
-
const method = c.req.raw.method.toUpperCase();
|
|
59
|
-
if ('GET' !== method && 'HEAD' !== method) return null;
|
|
60
|
-
const originFilename = await resolvePublicDirectoryAsset(pwd, c.req.path);
|
|
61
|
-
if (null === originFilename) return null;
|
|
62
|
-
const preCompressedAsset = await resolvePreCompressedAsset(c, originFilename);
|
|
63
|
-
if (!preCompressedAsset.acceptable) {
|
|
64
|
-
applyPreCompressedAssetHeaders(c, preCompressedAsset);
|
|
65
|
-
return c.body(null, 406);
|
|
66
|
-
}
|
|
67
|
-
const selectedFilename = preCompressedAsset.selected?.filepath ?? originFilename;
|
|
68
|
-
const publicDirectory = await fs.realpath(path.join(pwd, 'public'));
|
|
69
|
-
let realSelectedFilename;
|
|
70
|
-
try {
|
|
71
|
-
realSelectedFilename = await fs.realpath(selectedFilename);
|
|
72
|
-
} catch {
|
|
73
|
-
return null;
|
|
74
|
-
}
|
|
75
|
-
if (!isPathInside(realSelectedFilename, publicDirectory)) return null;
|
|
76
|
-
const data = await fileReader.readFileFromSystem(realSelectedFilename, 'buffer');
|
|
77
|
-
if (null === data) return null;
|
|
78
|
-
const mimeType = getStaticMimeType(originFilename);
|
|
79
|
-
if (mimeType) c.header('Content-Type', mimeType);
|
|
80
|
-
applyPreCompressedAssetHeaders(c, preCompressedAsset);
|
|
81
|
-
c.header('Content-Length', String(data.byteLength));
|
|
82
|
-
if ('HEAD' === method) return c.body(null, 200);
|
|
83
|
-
const body = new Uint8Array(data.buffer, data.byteOffset, data.byteLength);
|
|
84
|
-
return c.body(body, 200);
|
|
85
|
-
};
|
|
86
|
-
const createModuleFederationStaticServing = ({ pwd, pathPrefix })=>{
|
|
87
|
-
let moduleFederationAssets = null;
|
|
88
|
-
let moduleFederationAssetsExpiresAt = 0;
|
|
89
|
-
let moduleFederationAssetsRefresh = null;
|
|
90
|
-
const getModuleFederationAssets = async ()=>{
|
|
91
|
-
if (moduleFederationAssets && Date.now() < moduleFederationAssetsExpiresAt) return moduleFederationAssets;
|
|
92
|
-
if (!moduleFederationAssetsRefresh) moduleFederationAssetsRefresh = getModuleFederationAssetList(pwd).then((assets)=>{
|
|
93
|
-
moduleFederationAssets = assets;
|
|
94
|
-
moduleFederationAssetsExpiresAt = Date.now() + MODULE_FEDERATION_ASSET_REFRESH_INTERVAL_MS;
|
|
95
|
-
return assets;
|
|
96
|
-
}).finally(()=>{
|
|
97
|
-
moduleFederationAssetsRefresh = null;
|
|
98
|
-
});
|
|
99
|
-
return moduleFederationAssetsRefresh;
|
|
100
|
-
};
|
|
101
|
-
const resolveRequest = async (pathname)=>{
|
|
102
|
-
const requestPath = getModuleFederationRequestPath(pathname, pathPrefix);
|
|
103
|
-
if (requestPath.includes('..')) return null;
|
|
104
|
-
const moduleFederationAssetMeta = await getModuleFederationAssets();
|
|
105
|
-
return {
|
|
106
|
-
requestPath,
|
|
107
|
-
isModuleFederationAsset: moduleFederationAssetMeta.assets.has(requestPath),
|
|
108
|
-
isModuleFederationRemoteEntry: moduleFederationAssetMeta.remoteEntries.has(requestPath)
|
|
109
|
-
};
|
|
110
|
-
};
|
|
111
|
-
const serveFile = async (c, filepath, moduleFederationAsset = false, moduleFederationRemoteEntry = false, requestPath = '')=>{
|
|
112
|
-
if (moduleFederationAsset) applyModuleFederationAssetHeaders(c);
|
|
113
|
-
const mimeType = getStaticMimeType(filepath);
|
|
114
|
-
if (mimeType) c.header('Content-Type', mimeType);
|
|
115
|
-
const shouldPatchManifest = moduleFederationAsset && isModuleFederationManifestRequest(requestPath) && !isBackendModuleFederationManifestRequest(requestPath);
|
|
116
|
-
const shouldPatchRemoteEntry = moduleFederationRemoteEntry;
|
|
117
|
-
const canUsePreCompressed = !shouldPatchManifest && !shouldPatchRemoteEntry;
|
|
118
|
-
const preCompressedAsset = canUsePreCompressed ? await resolvePreCompressedAsset(c, filepath) : {
|
|
119
|
-
selected: null,
|
|
120
|
-
hasVariant: false,
|
|
121
|
-
acceptable: true
|
|
122
|
-
};
|
|
123
|
-
if (!preCompressedAsset.acceptable) {
|
|
124
|
-
applyPreCompressedAssetHeaders(c, preCompressedAsset);
|
|
125
|
-
return c.body(null, 406);
|
|
126
|
-
}
|
|
127
|
-
const targetFilepath = preCompressedAsset.selected?.filepath ?? filepath;
|
|
128
|
-
const chunk = await fileReader.readFileFromSystem(targetFilepath, 'buffer');
|
|
129
|
-
if (null === chunk) return null;
|
|
130
|
-
const responseChunk = shouldPatchManifest ? patchModuleFederationManifestPublicPath(c, chunk, pathPrefix) : shouldPatchRemoteEntry ? patchModuleFederationRemoteEntryPublicPath(c, chunk, pathPrefix) : chunk;
|
|
131
|
-
applyPreCompressedAssetHeaders(c, preCompressedAsset);
|
|
132
|
-
c.header('Content-Length', String(responseChunk.byteLength));
|
|
133
|
-
const body = new Uint8Array(responseChunk.buffer, responseChunk.byteOffset, responseChunk.byteLength);
|
|
134
|
-
return c.body(body, 200);
|
|
135
|
-
};
|
|
136
|
-
const serveByPath = async (c, filepath, request, moduleFederationAsset = false, moduleFederationRemoteEntry = false)=>{
|
|
137
|
-
if (!isPathInside(filepath, pwd)) return null;
|
|
138
|
-
if (!await fs.pathExists(filepath)) return null;
|
|
139
|
-
return serveFile(c, filepath, moduleFederationAsset, moduleFederationRemoteEntry, request.requestPath);
|
|
140
|
-
};
|
|
141
|
-
const serveStaticHit = (c, request)=>serveByPath(c, path.join(pwd, request.requestPath), request, request.isModuleFederationAsset, request.isModuleFederationRemoteEntry);
|
|
142
|
-
const serveModuleFederationAsset = (c, request)=>{
|
|
143
|
-
if (!request.isModuleFederationAsset) return null;
|
|
144
|
-
return serveByPath(c, path.join(pwd, request.requestPath), request, true, request.isModuleFederationRemoteEntry);
|
|
145
|
-
};
|
|
146
|
-
return {
|
|
147
|
-
resolveRequest,
|
|
148
|
-
serveStaticHit,
|
|
149
|
-
serveModuleFederationAsset
|
|
150
|
-
};
|
|
151
|
-
};
|
|
152
|
-
export { createModuleFederationStaticServing, servePreCompressedPublicRouteAsset, servePublicDirectoryAsset };
|
|
File without changes
|
|
File without changes
|