@lynx-js/web-rsbuild-server-middleware-canary 0.17.1-canary-20250924-6411f84c → 0.17.1-canary-20250925-9ab108fc
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/CHANGELOG.md +1 -1
- package/dist/index.js +58 -26
- package/package.json +4 -6
- /package/dist/{node/index.d.ts → index.d.ts} +0 -0
package/CHANGELOG.md
CHANGED
package/dist/index.js
CHANGED
|
@@ -1,50 +1,82 @@
|
|
|
1
|
-
import
|
|
2
|
-
import
|
|
3
|
-
import { fileURLToPath } from
|
|
4
|
-
const
|
|
5
|
-
const
|
|
6
|
-
const packageRoot = (()=>{
|
|
7
|
-
let currentDir =
|
|
8
|
-
while(currentDir.length &&
|
|
9
|
-
|
|
1
|
+
import path from 'node:path';
|
|
2
|
+
import fs from 'node:fs';
|
|
3
|
+
import { fileURLToPath } from 'node:url';
|
|
4
|
+
const __filename = fileURLToPath(import.meta.url);
|
|
5
|
+
const __dirname = path.dirname(__filename);
|
|
6
|
+
const packageRoot = (() => {
|
|
7
|
+
let currentDir = __dirname;
|
|
8
|
+
while (currentDir.length && currentDir !== '/'
|
|
9
|
+
&& fs.existsSync(path.join(currentDir, 'package.json')) === false) {
|
|
10
|
+
currentDir = path.dirname(currentDir);
|
|
11
|
+
}
|
|
12
|
+
if (currentDir.length === 0) {
|
|
13
|
+
throw new Error('Cannot find package root');
|
|
14
|
+
}
|
|
10
15
|
return currentDir;
|
|
11
16
|
})();
|
|
12
|
-
const WEB_CORE_DIST =
|
|
17
|
+
const WEB_CORE_DIST = path.join(packageRoot, 'www');
|
|
13
18
|
const fileCache = new Map();
|
|
14
|
-
function createWebVirtualFilesMiddleware(subPath) {
|
|
15
|
-
if (subPath.endsWith('/'))
|
|
16
|
-
|
|
19
|
+
export function createWebVirtualFilesMiddleware(subPath) {
|
|
20
|
+
if (subPath.endsWith('/')) {
|
|
21
|
+
subPath = subPath.slice(0, -1);
|
|
22
|
+
}
|
|
23
|
+
return (req, res, next) => {
|
|
17
24
|
if (req.url) {
|
|
18
25
|
let url = req.url;
|
|
19
|
-
if (url.startsWith('//'))
|
|
26
|
+
if (url.startsWith('//')) {
|
|
27
|
+
url = url.slice(1);
|
|
28
|
+
}
|
|
20
29
|
if (url.startsWith(subPath)) {
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
if (''
|
|
30
|
+
// get the relative path by removing origin and query
|
|
31
|
+
// http://example.com:port/path/to/web/file.js -> /web/file.js
|
|
32
|
+
if (url.includes('?'))
|
|
33
|
+
url = url.split('?')[0];
|
|
34
|
+
let relativePath = path.posix.relative(subPath, url);
|
|
35
|
+
if (relativePath === '') {
|
|
36
|
+
relativePath = 'index.html';
|
|
37
|
+
}
|
|
24
38
|
try {
|
|
25
|
-
const filePath =
|
|
26
|
-
const extname =
|
|
39
|
+
const filePath = path.join(WEB_CORE_DIST, ...relativePath.split(path.posix.sep));
|
|
40
|
+
const extname = path.extname(filePath);
|
|
27
41
|
let fileContent;
|
|
28
|
-
if (fileCache.has(filePath))
|
|
42
|
+
if (fileCache.has(filePath)) {
|
|
43
|
+
fileContent = fileCache.get(filePath);
|
|
44
|
+
}
|
|
29
45
|
else {
|
|
30
|
-
fileContent =
|
|
31
|
-
|
|
46
|
+
fileContent = extname === '.wasm'
|
|
47
|
+
? fs.readFileSync(filePath)
|
|
48
|
+
: fs.readFileSync(filePath, 'utf-8');
|
|
49
|
+
if (typeof fileContent === 'string') {
|
|
50
|
+
fileContent = fileContent.replaceAll('http://lynx-web-core-mocked.localhost/', subPath + '/');
|
|
51
|
+
}
|
|
32
52
|
fileCache.set(filePath, fileContent);
|
|
33
53
|
}
|
|
34
|
-
const contextType =
|
|
54
|
+
const contextType = extname === '.js'
|
|
55
|
+
? 'application/javascript; charset=utf-8'
|
|
56
|
+
: extname === '.css'
|
|
57
|
+
? 'text/css; charset=utf-8'
|
|
58
|
+
: extname === '.html'
|
|
59
|
+
? 'text/html; charset=utf-8'
|
|
60
|
+
: extname === '.wasm'
|
|
61
|
+
? 'application/wasm'
|
|
62
|
+
: extname === '.json'
|
|
63
|
+
? 'application/json'
|
|
64
|
+
: 'text/plain';
|
|
35
65
|
res.setHeader('Content-Length', Buffer.byteLength(fileContent));
|
|
36
66
|
res.setHeader('Content-Type', contextType);
|
|
67
|
+
// enable cross-origin-isolate to enable SAB
|
|
37
68
|
res.setHeader('Cross-Origin-Opener-Policy', 'same-origin');
|
|
38
69
|
res.setHeader('Cross-Origin-Embedder-Policy', 'require-corp');
|
|
39
70
|
res.statusCode = 200;
|
|
40
71
|
res.end(fileContent);
|
|
41
72
|
return;
|
|
42
|
-
}
|
|
73
|
+
}
|
|
74
|
+
catch {
|
|
75
|
+
// file not found, continue to next middleware
|
|
76
|
+
}
|
|
43
77
|
}
|
|
44
78
|
}
|
|
45
79
|
next();
|
|
46
80
|
};
|
|
47
81
|
}
|
|
48
|
-
export { createWebVirtualFilesMiddleware };
|
|
49
|
-
|
|
50
82
|
//# sourceMappingURL=index.js.map
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@lynx-js/web-rsbuild-server-middleware-canary",
|
|
3
|
-
"version": "0.17.1-canary-
|
|
3
|
+
"version": "0.17.1-canary-20250925-9ab108fc",
|
|
4
4
|
"private": false,
|
|
5
5
|
"description": "a dev server middleware for rsbuild to serve Lynx Web Platform shell project",
|
|
6
6
|
"keywords": [],
|
|
@@ -12,7 +12,7 @@
|
|
|
12
12
|
"license": "Apache-2.0",
|
|
13
13
|
"type": "module",
|
|
14
14
|
"main": "dist/index.js",
|
|
15
|
-
"typings": "dist/
|
|
15
|
+
"typings": "dist/index.d.ts",
|
|
16
16
|
"files": [
|
|
17
17
|
"dist",
|
|
18
18
|
"!dist/**/*.js.map",
|
|
@@ -26,12 +26,10 @@
|
|
|
26
26
|
"@rsbuild/core": "1.5.12",
|
|
27
27
|
"rsbuild-plugin-arethetypeswrong": "0.1.1",
|
|
28
28
|
"rsbuild-plugin-publint": "0.3.3",
|
|
29
|
-
"@lynx-js/web-core": "npm:@lynx-js/web-core-canary@0.17.1-canary-
|
|
29
|
+
"@lynx-js/web-core": "npm:@lynx-js/web-core-canary@0.17.1-canary-20250925-9ab108fc",
|
|
30
30
|
"@lynx-js/web-elements": "npm:@lynx-js/web-elements-canary@0.8.7"
|
|
31
31
|
},
|
|
32
32
|
"scripts": {
|
|
33
|
-
"build": "
|
|
34
|
-
"build:lib": "rslib build",
|
|
35
|
-
"build:web": "rsbuild build"
|
|
33
|
+
"build": "rsbuild build"
|
|
36
34
|
}
|
|
37
35
|
}
|
|
File without changes
|