binhend 2.1.2 → 2.1.4
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/package.json +1 -1
- package/src/alias.js +64 -0
- package/src/binh.js +3 -0
- package/src/cors.js +1 -1
- package/src/web/component.method.js +1 -0
- package/src/web/index.js +3 -0
package/package.json
CHANGED
package/src/alias.js
ADDED
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
|
|
2
|
+
const path = require('path');
|
|
3
|
+
const DefaultModule = require('module');
|
|
4
|
+
|
|
5
|
+
// Protect against improperly customized module constructors by other frameworks/packages
|
|
6
|
+
const Module = module.constructor.length > 1 ? module.constructor : DefaultModule;
|
|
7
|
+
|
|
8
|
+
/** Step 1: Load and parse jsconfig.json */
|
|
9
|
+
const jsconfigPath = path.resolve('jsconfig.json');
|
|
10
|
+
|
|
11
|
+
var jsconfig;
|
|
12
|
+
|
|
13
|
+
try {
|
|
14
|
+
jsconfig = require(jsconfigPath);
|
|
15
|
+
}
|
|
16
|
+
catch (error) {
|
|
17
|
+
return console.log(`[BINEND] Not load 'jsconfig.json' for alias paths.`);
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
const baseUrl = jsconfig?.compilerOptions?.baseUrl || '.';
|
|
21
|
+
const paths = jsconfig?.compilerOptions?.paths || {};
|
|
22
|
+
|
|
23
|
+
/** Step 2: Create an alias map with all possible paths */
|
|
24
|
+
const aliasMap = {};
|
|
25
|
+
|
|
26
|
+
for (const alias in paths) {
|
|
27
|
+
const aliasPath = alias.replace('/*', ''); // Strip the '/*' for matching
|
|
28
|
+
const originalPaths = paths[alias]; // Get the array of paths
|
|
29
|
+
|
|
30
|
+
aliasMap[aliasPath] = originalPaths.map((originalPath) => {
|
|
31
|
+
// Resolve each path to an absolute path
|
|
32
|
+
return path.resolve(baseUrl, originalPath.replace('/*', ''));
|
|
33
|
+
});
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
/** Step 3: Override Module._resolveFilename */
|
|
37
|
+
const originalResolveFilename = Module._resolveFilename;
|
|
38
|
+
|
|
39
|
+
Module._resolveFilename = function (request, parent, isMain, options) {
|
|
40
|
+
// console.log(request, parent, isMain, options);
|
|
41
|
+
for (const alias in aliasMap) {
|
|
42
|
+
if (!request.startsWith(alias)) continue;
|
|
43
|
+
// Loop through the array of resolved paths for this alias
|
|
44
|
+
for (const resolvedPath of aliasMap[alias]) {
|
|
45
|
+
// Construct the new request path
|
|
46
|
+
var newRequest = request.replace(alias, resolvedPath);
|
|
47
|
+
|
|
48
|
+
if (parent.filename.startsWith(binh.webModulePath)) {
|
|
49
|
+
newRequest = newRequest.replace(binh.webSourcePath, binh.webModulePath);
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
// Attempt to resolve the new request
|
|
53
|
+
try {
|
|
54
|
+
return originalResolveFilename.call(this, newRequest, parent, isMain, options);
|
|
55
|
+
} catch (err) {
|
|
56
|
+
// If it fails, continue to the next path
|
|
57
|
+
continue;
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
// Fallback to the original resolution if no alias matches
|
|
63
|
+
return originalResolveFilename.call(this, request, parent, isMain, options);
|
|
64
|
+
};
|
package/src/binh.js
CHANGED
package/src/cors.js
CHANGED
|
@@ -38,7 +38,7 @@ module.exports = (options) => {
|
|
|
38
38
|
res.header('Access-Control-Allow-Origin', allowedOrigin);
|
|
39
39
|
|
|
40
40
|
if (!allowedOrigin) {
|
|
41
|
-
console.error(`Blocked by CORS: ${origin}`);
|
|
41
|
+
console.error(`[BINHEND] Blocked by CORS: ${origin}`);
|
|
42
42
|
return res.sendStatus(403); // Forbidden
|
|
43
43
|
}
|
|
44
44
|
|
package/src/web/index.js
CHANGED