@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 CHANGED
@@ -1,6 +1,6 @@
1
1
  # @lynx-js/web-rsbuild-server-middleware
2
2
 
3
- ## 0.17.1-canary-20250924152242-6411f84c7ea9a2af9fa3744a0f9ed0075b679f6a
3
+ ## 0.17.1-canary-20250925040534-9ab108fc8b1323f8369eacad4b561725713f8ee1
4
4
 
5
5
  ### Patch Changes
6
6
 
package/dist/index.js CHANGED
@@ -1,50 +1,82 @@
1
- import node_path from "node:path";
2
- import node_fs from "node:fs";
3
- import { fileURLToPath } from "node:url";
4
- const node_filename = fileURLToPath(import.meta.url);
5
- const node_dirname = node_path.dirname(node_filename);
6
- const packageRoot = (()=>{
7
- let currentDir = node_dirname;
8
- while(currentDir.length && '/' !== currentDir && false === node_fs.existsSync(node_path.join(currentDir, 'package.json')))currentDir = node_path.dirname(currentDir);
9
- if (0 === currentDir.length) throw new Error('Cannot find package root');
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 = node_path.join(packageRoot, 'www');
17
+ const WEB_CORE_DIST = path.join(packageRoot, 'www');
13
18
  const fileCache = new Map();
14
- function createWebVirtualFilesMiddleware(subPath) {
15
- if (subPath.endsWith('/')) subPath = subPath.slice(0, -1);
16
- return (req, res, next)=>{
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('//')) url = url.slice(1);
26
+ if (url.startsWith('//')) {
27
+ url = url.slice(1);
28
+ }
20
29
  if (url.startsWith(subPath)) {
21
- if (url.includes('?')) url = url.split('?')[0];
22
- let relativePath = node_path.posix.relative(subPath, url);
23
- if ('' === relativePath) relativePath = 'index.html';
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 = node_path.join(WEB_CORE_DIST, ...relativePath.split(node_path.posix.sep));
26
- const extname = node_path.extname(filePath);
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)) fileContent = fileCache.get(filePath);
42
+ if (fileCache.has(filePath)) {
43
+ fileContent = fileCache.get(filePath);
44
+ }
29
45
  else {
30
- fileContent = '.wasm' === extname ? node_fs.readFileSync(filePath) : node_fs.readFileSync(filePath, 'utf-8');
31
- if ('string' == typeof fileContent) fileContent = fileContent.replaceAll('http://lynx-web-core-mocked.localhost/', subPath + '/');
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 = '.js' === extname ? "application/javascript; charset=utf-8" : '.css' === extname ? 'text/css; charset=utf-8' : '.html' === extname ? 'text/html; charset=utf-8' : '.wasm' === extname ? 'application/wasm' : '.json' === extname ? 'application/json' : 'text/plain';
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
- } catch {}
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-20250924-6411f84c",
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/node/index.d.ts",
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-20250924-6411f84c",
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": "npm run build:web && npm run build:lib",
34
- "build:lib": "rslib build",
35
- "build:web": "rsbuild build"
33
+ "build": "rsbuild build"
36
34
  }
37
35
  }
File without changes