@oipage/cli 0.3.0-alpha.5 → 0.3.0
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/.github/FUNDING.yml +11 -11
- package/AUTHORS.txt +6 -6
- package/CHANGELOG +47 -47
- package/LICENSE +20 -20
- package/README.md +132 -132
- package/bin/WebSocket/decodeWsFrame.js +43 -43
- package/bin/WebSocket/encodeWsFrame.js +28 -28
- package/bin/WebSocket/headersToJSON.js +26 -26
- package/bin/WebSocket/index.js +106 -106
- package/bin/data/mineTypes.json +104 -104
- package/bin/disk.js +42 -42
- package/bin/help.js +55 -55
- package/bin/intercept.js +15 -15
- package/bin/network.js +21 -21
- package/bin/notebook/index.js +8 -8
- package/bin/proxy.js +82 -82
- package/bin/run +146 -146
- package/bin/serve.js +318 -318
- package/bin/template/404.html +184 -184
- package/bin/tools/deleteEmptyFolder.js +33 -33
- package/bin/tools/format.js +75 -75
- package/bin/tools/isBinary.js +83 -83
- package/bin/tools/network.js +42 -42
- package/bin/tools/openBrowser.js +34 -34
- package/bin/tools/resolve404.js +98 -98
- package/bin/tools/resolveImport.js +96 -96
- package/bin/website-htmls/components/ui-select-file/index.html +8 -8
- package/bin/website-htmls/components/ui-select-file/index.js +42 -42
- package/bin/website-htmls/components/ui-select-file/index.scss +44 -44
- package/bin/website-htmls/dialogs/imageSize/index.html +54 -54
- package/bin/website-htmls/dialogs/imageSize/index.js +53 -53
- package/bin/website-htmls/dialogs/imageSize/index.scss +140 -140
- package/bin/website-htmls/dialogs/index.js +52 -52
- package/bin/website-htmls/images/addFile.svg +1 -1
- package/bin/website-htmls/images/img-to-pdf.svg +1 -1
- package/bin/website-htmls/index.html +17 -17
- package/bin/website-htmls/main.js +13 -13
- package/bin/website-htmls/pages/App/index.html +6 -6
- package/bin/website-htmls/pages/App/index.js +22 -22
- package/bin/website-htmls/pages/App/index.scss +43 -43
- package/bin/website-htmls/pages/appStore/index.html +7 -7
- package/bin/website-htmls/pages/appStore/index.js +19 -19
- package/bin/website-htmls/pages/appStore/index.scss +54 -54
- package/bin/website-htmls/pages/chart/index.html +8 -8
- package/bin/website-htmls/pages/chart/index.js +72 -72
- package/bin/website-htmls/pages/chart/index.scss +71 -71
- package/bin/website-htmls/pages/home/index.html +19 -19
- package/bin/website-htmls/pages/home/index.js +19 -19
- package/bin/website-htmls/pages/home/index.scss +87 -87
- package/bin/website-htmls/pages/image-editor/index.html +27 -27
- package/bin/website-htmls/pages/image-editor/index.js +106 -106
- package/bin/website-htmls/pages/image-editor/index.scss +69 -69
- package/bin/website-htmls/pages/img-to-pdf/index.html +3 -3
- package/bin/website-htmls/pages/img-to-pdf/index.js +44 -44
- package/bin/website-htmls/pages/img-to-pdf/index.scss +9 -9
- package/bin/website-htmls/pages/recorder-screen/index.html +15 -15
- package/bin/website-htmls/pages/recorder-screen/index.js +227 -227
- package/bin/website-htmls/pages/recorder-screen/index.scss +52 -52
- package/bin/website-htmls/router.config.js +29 -29
- package/bin/website-htmls/styles/common.css +44 -44
- package/bin/website-htmls/styles/normalize.css +94 -94
- package/bin/website-plugins/intercept/chart.js +33 -33
- package/bin/website-plugins/intercept/getPackage.js +17 -17
- package/bin/website-plugins/intercept/head.js +8 -8
- package/bin/website-plugins/intercept/index.js +8 -8
- package/bin/website-plugins/intercept/oipage-vislite-intercept.js +55 -55
- package/bin/website-plugins/intercept/oipage-zipaper-intercept.js +36 -36
- package/bin/website-plugins/loader/index.js +11 -11
- package/bin/website-plugins/loader/oipage-html-loader.js +7 -7
- package/bin/website-plugins/loader/oipage-scss-loader.js +149 -149
- package/docs/config.md +106 -106
- package/package.json +42 -42
- package/src/index.js +29 -29
- package/types/serve.d.ts +83 -83
package/bin/serve.js
CHANGED
|
@@ -1,319 +1,319 @@
|
|
|
1
|
-
const { join, resolve, sep } = require("path");
|
|
2
|
-
const { existsSync, lstatSync, statSync, createReadStream } = require("fs");
|
|
3
|
-
const { createServer } = require('http');
|
|
4
|
-
const { gzipSync, createGzip } = require("zlib");
|
|
5
|
-
const packageValue = require("../package.json");
|
|
6
|
-
const network = require("./tools/network.js");
|
|
7
|
-
const mineTypes = require("./data/mineTypes.json");
|
|
8
|
-
const resolveImportFactory = require("./tools/resolveImport.js");
|
|
9
|
-
const openBrowser = require("./tools/openBrowser.js");
|
|
10
|
-
const { doIntercept } = require("./intercept.js");
|
|
11
|
-
const proxy = require("./proxy.js");
|
|
12
|
-
const resolve404_default = require("./tools/resolve404.js");
|
|
13
|
-
const isBinary = require("./tools/isBinary.js");
|
|
14
|
-
const websiteIntercept = require("./website-plugins/intercept/index.js");
|
|
15
|
-
const websiteLoader = require("./website-plugins/loader/index.js");
|
|
16
|
-
|
|
17
|
-
const WebSocketClass = require("./WebSocket/index.js");
|
|
18
|
-
|
|
19
|
-
// 可压缩的 MIME 类型集合(文本类资源,压缩收益大)
|
|
20
|
-
const compressibleTypes = new Set([
|
|
21
|
-
"text/html", "text/css", "text/plain", "text/xml", "text/mathml",
|
|
22
|
-
"text/vnd.sun.j2me.app-descriptor", "text/vnd.wap.wml", "text/x-component",
|
|
23
|
-
"application/javascript", "application/json", "application/xml",
|
|
24
|
-
"application/xhtml+xml", "application/atom+xml", "application/rss+xml",
|
|
25
|
-
"application/xspf+xml", "application/x-tcl", "application/x-perl",
|
|
26
|
-
"image/svg+xml"
|
|
27
|
-
]);
|
|
28
|
-
|
|
29
|
-
// 需要设置 charset 的 MIME 类型前缀/精确匹配集合
|
|
30
|
-
// 文本类资源需要 charset 告知浏览器字符编码,二进制资源不需要
|
|
31
|
-
const charsetPrefixes = ["text/"];
|
|
32
|
-
const charsetTypes = new Set([
|
|
33
|
-
"application/javascript", "application/json", "application/xml",
|
|
34
|
-
"application/xhtml+xml", "application/atom+xml", "application/rss+xml",
|
|
35
|
-
"application/xspf+xml", "application/x-tcl", "application/x-perl",
|
|
36
|
-
"application/rtf", "application/postscript",
|
|
37
|
-
"application/vnd.google-earth.kml+xml", "application/x-javascript",
|
|
38
|
-
"image/svg+xml"
|
|
39
|
-
]);
|
|
40
|
-
|
|
41
|
-
/**
|
|
42
|
-
* 根据 MIME 类型构建 Content-Type 值
|
|
43
|
-
* 文本类型追加 charset=utf-8,二进制类型不追加
|
|
44
|
-
* @param {string} mimeType MIME 类型
|
|
45
|
-
* @returns {string} 完整的 Content-Type 值
|
|
46
|
-
*/
|
|
47
|
-
function buildContentType(mimeType) {
|
|
48
|
-
if (!mimeType) return "text/plain;charset=utf-8";
|
|
49
|
-
if (charsetPrefixes.some(prefix => mimeType.startsWith(prefix))) {
|
|
50
|
-
return mimeType + ";charset=utf-8";
|
|
51
|
-
}
|
|
52
|
-
if (charsetTypes.has(mimeType)) {
|
|
53
|
-
return mimeType + ";charset=utf-8";
|
|
54
|
-
}
|
|
55
|
-
return mimeType;
|
|
56
|
-
}
|
|
57
|
-
|
|
58
|
-
// 开发服务器
|
|
59
|
-
module.exports = function (config) {
|
|
60
|
-
let startTime = new Date().valueOf();
|
|
61
|
-
|
|
62
|
-
const resolve404 = config.devServer["404"] || resolve404_default;
|
|
63
|
-
const cache = "cache" in config.devServer ? config.devServer.cache : true;
|
|
64
|
-
const website = "website" in config.devServer ? config.devServer.website : true;
|
|
65
|
-
const gzip = "gzip" in config.devServer ? config.devServer.gzip : false;
|
|
66
|
-
const port = +config.devServer.port; // 端口号
|
|
67
|
-
const basePath = resolve((/^\./.test(config.devServer.baseUrl)) ? join(process.cwd(), config.devServer.baseUrl) : config.devServer.baseUrl); // 服务器根路径
|
|
68
|
-
|
|
69
|
-
const name = (config.name || "OIPage") + "-http-server";
|
|
70
|
-
const version = config.version || packageValue.version;
|
|
71
|
-
|
|
72
|
-
const wsHandler = WebSocketClass(port + 1, (config.name || "OIPage") + "-ws-server", version, website);
|
|
73
|
-
|
|
74
|
-
// 全局错误捕获,防止服务器因任何未捕获的错误而停机
|
|
75
|
-
// 2026年8月8日 于南宁
|
|
76
|
-
process.on('uncaughtException', function (err) {
|
|
77
|
-
console.log("<i> \x1b[1m\x1b[31m[" + name + "] uncaughtException: " + err.message + "\x1b[0m " + new Date().toLocaleString());
|
|
78
|
-
console.log(err.stack);
|
|
79
|
-
});
|
|
80
|
-
process.on('unhandledRejection', function (reason) {
|
|
81
|
-
console.log("<i> \x1b[1m\x1b[31m[" + name + "] unhandledRejection: " + (reason && reason.message || reason) + "\x1b[0m " + new Date().toLocaleString());
|
|
82
|
-
});
|
|
83
|
-
|
|
84
|
-
let Server = createServer(function (request, response) {
|
|
85
|
-
let headers = request.headers;
|
|
86
|
-
|
|
87
|
-
const rawIp = (request.socket.remoteAddress || '').replace(/^::ffff:/, "").replace(/^::1/, "127.0.0.1");
|
|
88
|
-
|
|
89
|
-
let url;
|
|
90
|
-
try {
|
|
91
|
-
|
|
92
|
-
// 上游网关二次解码的架构层面风险(%252e 双重编码),导致文件路径被二次解码,导致路径穿越漏洞
|
|
93
|
-
// 后续处理中,对url不要再decodeURIComponent进行解码,防止路径穿越漏洞
|
|
94
|
-
// 2026年8月9日 于南宁
|
|
95
|
-
url = decodeURIComponent(request.url);
|
|
96
|
-
} catch (e) {
|
|
97
|
-
|
|
98
|
-
// 如果 URL 包含无效的百分号编码(如 %ZZ),decodeURIComponent 会抛出 URIError
|
|
99
|
-
// 虽然被 uncaughtException 捕获,但会导致当前请求处理中断且无响应返回,客户端会挂起等待
|
|
100
|
-
// 2026年8月9日 于南宁
|
|
101
|
-
response.writeHead(400, {
|
|
102
|
-
'Content-Type': buildContentType('text/plain'),
|
|
103
|
-
'Access-Control-Allow-Origin': '*',
|
|
104
|
-
'Server': 'Powered by ' + name + '@' + version
|
|
105
|
-
});
|
|
106
|
-
response.end('Bad Request');
|
|
107
|
-
return;
|
|
108
|
-
}
|
|
109
|
-
|
|
110
|
-
let urlArray = url.split("?");
|
|
111
|
-
url = urlArray[0];
|
|
112
|
-
|
|
113
|
-
// 请求的文件路径
|
|
114
|
-
let filePath;
|
|
115
|
-
|
|
116
|
-
let isWebsite = website && /^\/@website\//.test(url);
|
|
117
|
-
if (isWebsite) {
|
|
118
|
-
filePath = join(__dirname, "./website-htmls/", url.replace(/^\/@website\//, ""));
|
|
119
|
-
} else {
|
|
120
|
-
filePath = join(basePath, url == "/" ? "index.html" : url.replace(/^\//, ""));
|
|
121
|
-
}
|
|
122
|
-
|
|
123
|
-
filePath = resolve(filePath);
|
|
124
|
-
|
|
125
|
-
let proxyIntercept = proxy({
|
|
126
|
-
name,
|
|
127
|
-
version
|
|
128
|
-
}, config.devServer.proxy || {});
|
|
129
|
-
delete config.devServer.proxy;
|
|
130
|
-
for (let i = 0; i < proxyIntercept.length; i++) {
|
|
131
|
-
config.devServer.intercept.push(proxyIntercept[i]);
|
|
132
|
-
}
|
|
133
|
-
|
|
134
|
-
// 请求拦截
|
|
135
|
-
if (doIntercept(url.replace(/^\/@website/, "").replace(/^\/@modules\//, ""), isWebsite ? websiteIntercept : config.devServer.intercept, request, response, wsHandler)) {
|
|
136
|
-
console.log("<i> \x1b[1m\x1b[32m[" + name + "] intercept: " + url + '\x1b[0m ' + new Date().toLocaleString() + " " + rawIp);
|
|
137
|
-
} else {
|
|
138
|
-
|
|
139
|
-
// 对应前置都没有找到的,默认去当前命令行找
|
|
140
|
-
// 2026年4月13日 于南京
|
|
141
|
-
if (/^\/@modules\//.test(url)) {
|
|
142
|
-
filePath = join(process.cwd(), url.replace(/^\/@modules\//, "node_modules/"))
|
|
143
|
-
}
|
|
144
|
-
|
|
145
|
-
// 禁止访问沙箱外文件,路径穿越拦截
|
|
146
|
-
// 2026年8月8日 于南宁
|
|
147
|
-
if ((!(filePath.startsWith(basePath + sep) || filePath === basePath) &&
|
|
148
|
-
!(filePath.startsWith(process.cwd() + sep) || filePath === process.cwd())
|
|
149
|
-
) || filePath.includes('\0')) {
|
|
150
|
-
response.writeHead(403, {
|
|
151
|
-
'Content-Type': buildContentType("text/plain"),
|
|
152
|
-
'Access-Control-Allow-Origin': '*',
|
|
153
|
-
'Server': 'Powered by ' + name + '@' + version
|
|
154
|
-
});
|
|
155
|
-
response.write("Path Traversal attack");
|
|
156
|
-
response.end();
|
|
157
|
-
console.log("<i> \x1b[1m\x1b[31m[" + name + "] Path Traversal attack, path: " + url + "\x1b[0m " + new Date().toLocaleString() + " " + rawIp);
|
|
158
|
-
return;
|
|
159
|
-
}
|
|
160
|
-
|
|
161
|
-
// 如果存在且是文件
|
|
162
|
-
if (existsSync(filePath) && !lstatSync(filePath).isDirectory()) {
|
|
163
|
-
|
|
164
|
-
// 判断是否是请求而无需进一步解析
|
|
165
|
-
// 2025年12月5日 于南京
|
|
166
|
-
let isXHR = headers["sec-fetch-dest"] === "empty";
|
|
167
|
-
|
|
168
|
-
let dotName = (filePath.match(/\.([^.]+)$/) || [])[1] || "";
|
|
169
|
-
let fileType = mineTypes[dotName]; // 文件类型
|
|
170
|
-
let fileInfo = statSync(filePath);
|
|
171
|
-
|
|
172
|
-
let fileModifiedTime = new Date(fileInfo.mtime).toGMTString();
|
|
173
|
-
|
|
174
|
-
let responseHeader = {
|
|
175
|
-
'Content-Type': buildContentType(fileType || "text/plain"),
|
|
176
|
-
'Access-Control-Allow-Origin': '*',
|
|
177
|
-
'Server': 'Powered by ' + name + '@' + version,
|
|
178
|
-
'Cache-Control': 'no-cache',
|
|
179
|
-
// 'Content-Length': fileInfo.size, // 会导致拦截修改的文本内容不对
|
|
180
|
-
|
|
181
|
-
// 与if-modified-since配合使用,做缓存验证
|
|
182
|
-
'Last-Modified': fileModifiedTime,
|
|
183
|
-
|
|
184
|
-
// 与if-none-match配合使用,做缓存验证
|
|
185
|
-
// ETag 包含 gzip 标志,确保 gzip 配置变化时缓存正确失效
|
|
186
|
-
'ETag': '"' + fileInfo.mtime.getTime() + (gzip ? '-gz' : '') + '"',
|
|
187
|
-
|
|
188
|
-
// 告知代理/缓存:响应内容会根据 Accept-Encoding 不同而变化
|
|
189
|
-
'Vary': 'Accept-Encoding'
|
|
190
|
-
};
|
|
191
|
-
|
|
192
|
-
if (cache && headers["if-modified-since"]) {
|
|
193
|
-
let ifModifiedSince = new Date(headers["if-modified-since"]).valueOf()
|
|
194
|
-
let lastModified = new Date(fileModifiedTime).valueOf()
|
|
195
|
-
|
|
196
|
-
// 注意这里不能使用<=,否则
|
|
197
|
-
// 1、会出现时区问题
|
|
198
|
-
// 2、作为开发服务器,可能同一个url地址对应的文件不同,被误判为未修改
|
|
199
|
-
// 2025年12月29日 于南京
|
|
200
|
-
// if (lastModified <= ifModifiedSince) {}
|
|
201
|
-
|
|
202
|
-
// 同时检查 ETag(If-None-Match),确保 gzip 配置变化时不会错误返回 304
|
|
203
|
-
// 比如:gzip 从关闭改为开启,响应内容从原始变为压缩,ETag 不同,必须重新传输
|
|
204
|
-
// 2026年08月15日 于南宁
|
|
205
|
-
let etagMatch = !headers["if-none-match"] || headers["if-none-match"] === responseHeader['ETag'];
|
|
206
|
-
|
|
207
|
-
if (lastModified === ifModifiedSince && etagMatch) {
|
|
208
|
-
response.writeHead(304, responseHeader);
|
|
209
|
-
response.end();
|
|
210
|
-
console.log("<i> \x1b[1m\x1b[32m[" + name + "] Cache File: " + url + "\x1b[0m " + new Date().toLocaleString() + " \x1b[33m\x1b[1m304\x1b[0m" + (isXHR ? " \x1b[33m\x1b[1m" + request.method + "\x1b[0m" : "") + " " + rawIp);
|
|
211
|
-
return;
|
|
212
|
-
}
|
|
213
|
-
}
|
|
214
|
-
|
|
215
|
-
let sendType = "";
|
|
216
|
-
let entry = headers.accept !== "*/*";
|
|
217
|
-
|
|
218
|
-
// 判断是否需要 gzip 压缩:配置开启 && 客户端支持 && 文件类型可压缩
|
|
219
|
-
let acceptEncoding = headers["accept-encoding"] || "";
|
|
220
|
-
let shouldGzip = gzip && /\bgzip\b/.test(acceptEncoding) && compressibleTypes.has(fileType);
|
|
221
|
-
|
|
222
|
-
// 如果文件小于1M且不是二进制,认为不大,直接读取
|
|
223
|
-
// 如果是二进制文件,即使小于10M也使用流(避免不必要的加载器处理和import解析)
|
|
224
|
-
if (fileInfo.size < 10 * 1024 * 1024 && !isBinary(filePath)) {
|
|
225
|
-
let { source, resolveImport } = resolveImportFactory(basePath, filePath, entry, isWebsite ? websiteIntercept : config.devServer.intercept, urlArray[1] === "download", isWebsite)
|
|
226
|
-
|
|
227
|
-
// 只处理非下载文件
|
|
228
|
-
// 过大的也不进行处理
|
|
229
|
-
// (对website无效)
|
|
230
|
-
if (urlArray[1] !== "download") {
|
|
231
|
-
let loaders = isWebsite ? websiteLoader : config.module;
|
|
232
|
-
|
|
233
|
-
for (let i = 0; i < loaders.rules.length; i++) {
|
|
234
|
-
if (loaders.rules[i].test.test(filePath)) {
|
|
235
|
-
source = loaders.rules[i].use.call({
|
|
236
|
-
root: basePath, // 服务器根路径
|
|
237
|
-
path: filePath.replace(basePath, ""), // 文件相对路径
|
|
238
|
-
entry, // 是否是浏览器地址栏直接访问
|
|
239
|
-
setFileType(_fileType) { // 设置文件类型
|
|
240
|
-
fileType = _fileType;
|
|
241
|
-
responseHeader['Content-Type'] = buildContentType(_fileType);
|
|
242
|
-
}
|
|
243
|
-
}, source);
|
|
244
|
-
break;
|
|
245
|
-
}
|
|
246
|
-
}
|
|
247
|
-
|
|
248
|
-
}
|
|
249
|
-
|
|
250
|
-
sendType = "Read";
|
|
251
|
-
let body = isXHR ? source : resolveImport(source, fileType !== "application/javascript");
|
|
252
|
-
|
|
253
|
-
// gzip 压缩后发送(需要重新判断 fileType,因为 loader 可能修改了它)
|
|
254
|
-
if (shouldGzip && compressibleTypes.has(fileType)) {
|
|
255
|
-
responseHeader['Content-Encoding'] = 'gzip';
|
|
256
|
-
body = gzipSync(Buffer.from(body));
|
|
257
|
-
}
|
|
258
|
-
|
|
259
|
-
response.writeHead(200, responseHeader);
|
|
260
|
-
response.write(body);
|
|
261
|
-
response.end();
|
|
262
|
-
}
|
|
263
|
-
|
|
264
|
-
// 对于大文件,使用流读取
|
|
265
|
-
else {
|
|
266
|
-
sendType = "Stream";
|
|
267
|
-
|
|
268
|
-
if (shouldGzip) {
|
|
269
|
-
// gzip 流式压缩:文件大小变化,不能设置 Content-Length
|
|
270
|
-
responseHeader['Content-Encoding'] = 'gzip';
|
|
271
|
-
response.writeHead(200, responseHeader);
|
|
272
|
-
createReadStream(filePath).pipe(createGzip()).pipe(response);
|
|
273
|
-
} else {
|
|
274
|
-
responseHeader['Content-Length'] = fileInfo.size
|
|
275
|
-
|
|
276
|
-
response.writeHead(200, responseHeader);
|
|
277
|
-
createReadStream(filePath).pipe(response);
|
|
278
|
-
}
|
|
279
|
-
}
|
|
280
|
-
|
|
281
|
-
console.log("<i> \x1b[1m\x1b[32m[" + name + "] " + sendType + " File: " + url + '\x1b[0m ' + new Date().toLocaleString() + (shouldGzip ? " \x1b[33m\x1b[1mgzip\x1b[0m" : "") + (isXHR ? " \x1b[33m\x1b[1m" + request.method + "\x1b[0m" : "") + " " + rawIp);
|
|
282
|
-
}
|
|
283
|
-
|
|
284
|
-
// 否则就是404
|
|
285
|
-
else {
|
|
286
|
-
response.writeHead(404, {
|
|
287
|
-
'Content-Type': buildContentType("text/html"),
|
|
288
|
-
'Access-Control-Allow-Origin': '*',
|
|
289
|
-
'Server': 'Powered by ' + name + '@' + version
|
|
290
|
-
});
|
|
291
|
-
response.write(resolve404(filePath, url, website, config.name || "OIPage", rawIp));
|
|
292
|
-
response.end();
|
|
293
|
-
}
|
|
294
|
-
}
|
|
295
|
-
|
|
296
|
-
});
|
|
297
|
-
|
|
298
|
-
Server.listen(port);
|
|
299
|
-
|
|
300
|
-
// 获取网络信息
|
|
301
|
-
let networkValue = network();
|
|
302
|
-
|
|
303
|
-
// 打印成功提示
|
|
304
|
-
console.log('<i> \x1b[1m\x1b[32m[' + name + '] Project is running at:\x1b[0m');
|
|
305
|
-
console.log('<i> \x1b[1m\x1b[32m[' + name + '] Loopback: \x1b[36mhttp://localhost:' + port + '/\x1b[0m');
|
|
306
|
-
for (let ipv4Item of networkValue.IPv4) console.log('<i> \x1b[1m\x1b[32m[' + name + '] On Your Network (IPv4):\x1b[36m http://' + ipv4Item.address + ':' + port + '/\x1b[0m');
|
|
307
|
-
for (let ipv6Item of networkValue.IPv6) console.log('<i> \x1b[1m\x1b[32m[' + name + '] On Your Network (IPv6): \x1b[36mhttp://[' + ipv6Item.address + ']:' + port + '/\x1b[0m');
|
|
308
|
-
console.log('<i> \x1b[1m\x1b[32m[' + name + '] Content not from ' + (config.name || "OIPage") + ' is served from \x1b[36m"' + basePath + '" \x1b[0mdirectory');
|
|
309
|
-
|
|
310
|
-
if (typeof config.devServer.open === "string" || config.devServer.open) {
|
|
311
|
-
let url = 'http://localhost:' + port + "/" + (typeof config.devServer.open === "string" ? config.devServer.open : "");
|
|
312
|
-
openBrowser(url);
|
|
313
|
-
console.log('<i> \x1b[1m\x1b[32m[' + name + '] Open: \x1b[36m' + url + '\x1b[0m');
|
|
314
|
-
}
|
|
315
|
-
|
|
316
|
-
if (!cache) console.log('<i> \x1b[1m\x1b[32m[' + name + '] Cancel 304 Cache!\x1b[0m');
|
|
317
|
-
if (gzip) console.log('<i> \x1b[1m\x1b[32m[' + name + '] Gzip Compression: \x1b[36menabled\x1b[0m');
|
|
318
|
-
console.log('\n' + (config.name || "OIPage") + ' ' + version + ' compiled \x1b[1m\x1b[32msuccessfully\x1b[0m in ' + (new Date().valueOf() - startTime) + ' ms\n');
|
|
1
|
+
const { join, resolve, sep } = require("path");
|
|
2
|
+
const { existsSync, lstatSync, statSync, createReadStream } = require("fs");
|
|
3
|
+
const { createServer } = require('http');
|
|
4
|
+
const { gzipSync, createGzip } = require("zlib");
|
|
5
|
+
const packageValue = require("../package.json");
|
|
6
|
+
const network = require("./tools/network.js");
|
|
7
|
+
const mineTypes = require("./data/mineTypes.json");
|
|
8
|
+
const resolveImportFactory = require("./tools/resolveImport.js");
|
|
9
|
+
const openBrowser = require("./tools/openBrowser.js");
|
|
10
|
+
const { doIntercept } = require("./intercept.js");
|
|
11
|
+
const proxy = require("./proxy.js");
|
|
12
|
+
const resolve404_default = require("./tools/resolve404.js");
|
|
13
|
+
const isBinary = require("./tools/isBinary.js");
|
|
14
|
+
const websiteIntercept = require("./website-plugins/intercept/index.js");
|
|
15
|
+
const websiteLoader = require("./website-plugins/loader/index.js");
|
|
16
|
+
|
|
17
|
+
const WebSocketClass = require("./WebSocket/index.js");
|
|
18
|
+
|
|
19
|
+
// 可压缩的 MIME 类型集合(文本类资源,压缩收益大)
|
|
20
|
+
const compressibleTypes = new Set([
|
|
21
|
+
"text/html", "text/css", "text/plain", "text/xml", "text/mathml",
|
|
22
|
+
"text/vnd.sun.j2me.app-descriptor", "text/vnd.wap.wml", "text/x-component",
|
|
23
|
+
"application/javascript", "application/json", "application/xml",
|
|
24
|
+
"application/xhtml+xml", "application/atom+xml", "application/rss+xml",
|
|
25
|
+
"application/xspf+xml", "application/x-tcl", "application/x-perl",
|
|
26
|
+
"image/svg+xml"
|
|
27
|
+
]);
|
|
28
|
+
|
|
29
|
+
// 需要设置 charset 的 MIME 类型前缀/精确匹配集合
|
|
30
|
+
// 文本类资源需要 charset 告知浏览器字符编码,二进制资源不需要
|
|
31
|
+
const charsetPrefixes = ["text/"];
|
|
32
|
+
const charsetTypes = new Set([
|
|
33
|
+
"application/javascript", "application/json", "application/xml",
|
|
34
|
+
"application/xhtml+xml", "application/atom+xml", "application/rss+xml",
|
|
35
|
+
"application/xspf+xml", "application/x-tcl", "application/x-perl",
|
|
36
|
+
"application/rtf", "application/postscript",
|
|
37
|
+
"application/vnd.google-earth.kml+xml", "application/x-javascript",
|
|
38
|
+
"image/svg+xml"
|
|
39
|
+
]);
|
|
40
|
+
|
|
41
|
+
/**
|
|
42
|
+
* 根据 MIME 类型构建 Content-Type 值
|
|
43
|
+
* 文本类型追加 charset=utf-8,二进制类型不追加
|
|
44
|
+
* @param {string} mimeType MIME 类型
|
|
45
|
+
* @returns {string} 完整的 Content-Type 值
|
|
46
|
+
*/
|
|
47
|
+
function buildContentType(mimeType) {
|
|
48
|
+
if (!mimeType) return "text/plain;charset=utf-8";
|
|
49
|
+
if (charsetPrefixes.some(prefix => mimeType.startsWith(prefix))) {
|
|
50
|
+
return mimeType + ";charset=utf-8";
|
|
51
|
+
}
|
|
52
|
+
if (charsetTypes.has(mimeType)) {
|
|
53
|
+
return mimeType + ";charset=utf-8";
|
|
54
|
+
}
|
|
55
|
+
return mimeType;
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
// 开发服务器
|
|
59
|
+
module.exports = function (config) {
|
|
60
|
+
let startTime = new Date().valueOf();
|
|
61
|
+
|
|
62
|
+
const resolve404 = config.devServer["404"] || resolve404_default;
|
|
63
|
+
const cache = "cache" in config.devServer ? config.devServer.cache : true;
|
|
64
|
+
const website = "website" in config.devServer ? config.devServer.website : true;
|
|
65
|
+
const gzip = "gzip" in config.devServer ? config.devServer.gzip : false;
|
|
66
|
+
const port = +config.devServer.port; // 端口号
|
|
67
|
+
const basePath = resolve((/^\./.test(config.devServer.baseUrl)) ? join(process.cwd(), config.devServer.baseUrl) : config.devServer.baseUrl); // 服务器根路径
|
|
68
|
+
|
|
69
|
+
const name = (config.name || "OIPage") + "-http-server";
|
|
70
|
+
const version = config.version || packageValue.version;
|
|
71
|
+
|
|
72
|
+
const wsHandler = WebSocketClass(port + 1, (config.name || "OIPage") + "-ws-server", version, website);
|
|
73
|
+
|
|
74
|
+
// 全局错误捕获,防止服务器因任何未捕获的错误而停机
|
|
75
|
+
// 2026年8月8日 于南宁
|
|
76
|
+
process.on('uncaughtException', function (err) {
|
|
77
|
+
console.log("<i> \x1b[1m\x1b[31m[" + name + "] uncaughtException: " + err.message + "\x1b[0m " + new Date().toLocaleString());
|
|
78
|
+
console.log(err.stack);
|
|
79
|
+
});
|
|
80
|
+
process.on('unhandledRejection', function (reason) {
|
|
81
|
+
console.log("<i> \x1b[1m\x1b[31m[" + name + "] unhandledRejection: " + (reason && reason.message || reason) + "\x1b[0m " + new Date().toLocaleString());
|
|
82
|
+
});
|
|
83
|
+
|
|
84
|
+
let Server = createServer(function (request, response) {
|
|
85
|
+
let headers = request.headers;
|
|
86
|
+
|
|
87
|
+
const rawIp = (request.socket.remoteAddress || '').replace(/^::ffff:/, "").replace(/^::1/, "127.0.0.1");
|
|
88
|
+
|
|
89
|
+
let url;
|
|
90
|
+
try {
|
|
91
|
+
|
|
92
|
+
// 上游网关二次解码的架构层面风险(%252e 双重编码),导致文件路径被二次解码,导致路径穿越漏洞
|
|
93
|
+
// 后续处理中,对url不要再decodeURIComponent进行解码,防止路径穿越漏洞
|
|
94
|
+
// 2026年8月9日 于南宁
|
|
95
|
+
url = decodeURIComponent(request.url);
|
|
96
|
+
} catch (e) {
|
|
97
|
+
|
|
98
|
+
// 如果 URL 包含无效的百分号编码(如 %ZZ),decodeURIComponent 会抛出 URIError
|
|
99
|
+
// 虽然被 uncaughtException 捕获,但会导致当前请求处理中断且无响应返回,客户端会挂起等待
|
|
100
|
+
// 2026年8月9日 于南宁
|
|
101
|
+
response.writeHead(400, {
|
|
102
|
+
'Content-Type': buildContentType('text/plain'),
|
|
103
|
+
'Access-Control-Allow-Origin': '*',
|
|
104
|
+
'Server': 'Powered by ' + name + '@' + version
|
|
105
|
+
});
|
|
106
|
+
response.end('Bad Request');
|
|
107
|
+
return;
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
let urlArray = url.split("?");
|
|
111
|
+
url = urlArray[0];
|
|
112
|
+
|
|
113
|
+
// 请求的文件路径
|
|
114
|
+
let filePath;
|
|
115
|
+
|
|
116
|
+
let isWebsite = website && /^\/@website\//.test(url);
|
|
117
|
+
if (isWebsite) {
|
|
118
|
+
filePath = join(__dirname, "./website-htmls/", url.replace(/^\/@website\//, ""));
|
|
119
|
+
} else {
|
|
120
|
+
filePath = join(basePath, url == "/" ? "index.html" : url.replace(/^\//, ""));
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
filePath = resolve(filePath);
|
|
124
|
+
|
|
125
|
+
let proxyIntercept = proxy({
|
|
126
|
+
name,
|
|
127
|
+
version
|
|
128
|
+
}, config.devServer.proxy || {});
|
|
129
|
+
delete config.devServer.proxy;
|
|
130
|
+
for (let i = 0; i < proxyIntercept.length; i++) {
|
|
131
|
+
config.devServer.intercept.push(proxyIntercept[i]);
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
// 请求拦截
|
|
135
|
+
if (doIntercept(url.replace(/^\/@website/, "").replace(/^\/@modules\//, ""), isWebsite ? websiteIntercept : config.devServer.intercept, request, response, wsHandler)) {
|
|
136
|
+
console.log("<i> \x1b[1m\x1b[32m[" + name + "] intercept: " + url + '\x1b[0m ' + new Date().toLocaleString() + " " + rawIp);
|
|
137
|
+
} else {
|
|
138
|
+
|
|
139
|
+
// 对应前置都没有找到的,默认去当前命令行找
|
|
140
|
+
// 2026年4月13日 于南京
|
|
141
|
+
if (/^\/@modules\//.test(url)) {
|
|
142
|
+
filePath = join(process.cwd(), url.replace(/^\/@modules\//, "node_modules/"))
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
// 禁止访问沙箱外文件,路径穿越拦截
|
|
146
|
+
// 2026年8月8日 于南宁
|
|
147
|
+
if ((!(filePath.startsWith(basePath + sep) || filePath === basePath) &&
|
|
148
|
+
!(filePath.startsWith(process.cwd() + sep) || filePath === process.cwd())
|
|
149
|
+
) || filePath.includes('\0')) {
|
|
150
|
+
response.writeHead(403, {
|
|
151
|
+
'Content-Type': buildContentType("text/plain"),
|
|
152
|
+
'Access-Control-Allow-Origin': '*',
|
|
153
|
+
'Server': 'Powered by ' + name + '@' + version
|
|
154
|
+
});
|
|
155
|
+
response.write("Path Traversal attack");
|
|
156
|
+
response.end();
|
|
157
|
+
console.log("<i> \x1b[1m\x1b[31m[" + name + "] Path Traversal attack, path: " + url + "\x1b[0m " + new Date().toLocaleString() + " " + rawIp);
|
|
158
|
+
return;
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
// 如果存在且是文件
|
|
162
|
+
if (existsSync(filePath) && !lstatSync(filePath).isDirectory()) {
|
|
163
|
+
|
|
164
|
+
// 判断是否是请求而无需进一步解析
|
|
165
|
+
// 2025年12月5日 于南京
|
|
166
|
+
let isXHR = headers["sec-fetch-dest"] === "empty";
|
|
167
|
+
|
|
168
|
+
let dotName = (filePath.match(/\.([^.]+)$/) || [])[1] || "";
|
|
169
|
+
let fileType = mineTypes[dotName]; // 文件类型
|
|
170
|
+
let fileInfo = statSync(filePath);
|
|
171
|
+
|
|
172
|
+
let fileModifiedTime = new Date(fileInfo.mtime).toGMTString();
|
|
173
|
+
|
|
174
|
+
let responseHeader = {
|
|
175
|
+
'Content-Type': buildContentType(fileType || "text/plain"),
|
|
176
|
+
'Access-Control-Allow-Origin': '*',
|
|
177
|
+
'Server': 'Powered by ' + name + '@' + version,
|
|
178
|
+
'Cache-Control': 'no-cache',
|
|
179
|
+
// 'Content-Length': fileInfo.size, // 会导致拦截修改的文本内容不对
|
|
180
|
+
|
|
181
|
+
// 与if-modified-since配合使用,做缓存验证
|
|
182
|
+
'Last-Modified': fileModifiedTime,
|
|
183
|
+
|
|
184
|
+
// 与if-none-match配合使用,做缓存验证
|
|
185
|
+
// ETag 包含 gzip 标志,确保 gzip 配置变化时缓存正确失效
|
|
186
|
+
'ETag': '"' + fileInfo.mtime.getTime() + (gzip ? '-gz' : '') + '"',
|
|
187
|
+
|
|
188
|
+
// 告知代理/缓存:响应内容会根据 Accept-Encoding 不同而变化
|
|
189
|
+
'Vary': 'Accept-Encoding'
|
|
190
|
+
};
|
|
191
|
+
|
|
192
|
+
if (cache && headers["if-modified-since"]) {
|
|
193
|
+
let ifModifiedSince = new Date(headers["if-modified-since"]).valueOf()
|
|
194
|
+
let lastModified = new Date(fileModifiedTime).valueOf()
|
|
195
|
+
|
|
196
|
+
// 注意这里不能使用<=,否则
|
|
197
|
+
// 1、会出现时区问题
|
|
198
|
+
// 2、作为开发服务器,可能同一个url地址对应的文件不同,被误判为未修改
|
|
199
|
+
// 2025年12月29日 于南京
|
|
200
|
+
// if (lastModified <= ifModifiedSince) {}
|
|
201
|
+
|
|
202
|
+
// 同时检查 ETag(If-None-Match),确保 gzip 配置变化时不会错误返回 304
|
|
203
|
+
// 比如:gzip 从关闭改为开启,响应内容从原始变为压缩,ETag 不同,必须重新传输
|
|
204
|
+
// 2026年08月15日 于南宁
|
|
205
|
+
let etagMatch = !headers["if-none-match"] || headers["if-none-match"] === responseHeader['ETag'];
|
|
206
|
+
|
|
207
|
+
if (lastModified === ifModifiedSince && etagMatch) {
|
|
208
|
+
response.writeHead(304, responseHeader);
|
|
209
|
+
response.end();
|
|
210
|
+
console.log("<i> \x1b[1m\x1b[32m[" + name + "] Cache File: " + url + "\x1b[0m " + new Date().toLocaleString() + " \x1b[33m\x1b[1m304\x1b[0m" + (isXHR ? " \x1b[33m\x1b[1m" + request.method + "\x1b[0m" : "") + " " + rawIp);
|
|
211
|
+
return;
|
|
212
|
+
}
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
let sendType = "";
|
|
216
|
+
let entry = headers.accept !== "*/*";
|
|
217
|
+
|
|
218
|
+
// 判断是否需要 gzip 压缩:配置开启 && 客户端支持 && 文件类型可压缩
|
|
219
|
+
let acceptEncoding = headers["accept-encoding"] || "";
|
|
220
|
+
let shouldGzip = gzip && /\bgzip\b/.test(acceptEncoding) && compressibleTypes.has(fileType);
|
|
221
|
+
|
|
222
|
+
// 如果文件小于1M且不是二进制,认为不大,直接读取
|
|
223
|
+
// 如果是二进制文件,即使小于10M也使用流(避免不必要的加载器处理和import解析)
|
|
224
|
+
if (fileInfo.size < 10 * 1024 * 1024 && !isBinary(filePath)) {
|
|
225
|
+
let { source, resolveImport } = resolveImportFactory(basePath, filePath, entry, isWebsite ? websiteIntercept : config.devServer.intercept, urlArray[1] === "download", isWebsite)
|
|
226
|
+
|
|
227
|
+
// 只处理非下载文件
|
|
228
|
+
// 过大的也不进行处理
|
|
229
|
+
// (对website无效)
|
|
230
|
+
if (urlArray[1] !== "download") {
|
|
231
|
+
let loaders = isWebsite ? websiteLoader : config.module;
|
|
232
|
+
|
|
233
|
+
for (let i = 0; i < loaders.rules.length; i++) {
|
|
234
|
+
if (loaders.rules[i].test.test(filePath)) {
|
|
235
|
+
source = loaders.rules[i].use.call({
|
|
236
|
+
root: basePath, // 服务器根路径
|
|
237
|
+
path: filePath.replace(basePath, ""), // 文件相对路径
|
|
238
|
+
entry, // 是否是浏览器地址栏直接访问
|
|
239
|
+
setFileType(_fileType) { // 设置文件类型
|
|
240
|
+
fileType = _fileType;
|
|
241
|
+
responseHeader['Content-Type'] = buildContentType(_fileType);
|
|
242
|
+
}
|
|
243
|
+
}, source);
|
|
244
|
+
break;
|
|
245
|
+
}
|
|
246
|
+
}
|
|
247
|
+
|
|
248
|
+
}
|
|
249
|
+
|
|
250
|
+
sendType = "Read";
|
|
251
|
+
let body = isXHR ? source : resolveImport(source, fileType !== "application/javascript");
|
|
252
|
+
|
|
253
|
+
// gzip 压缩后发送(需要重新判断 fileType,因为 loader 可能修改了它)
|
|
254
|
+
if (shouldGzip && compressibleTypes.has(fileType)) {
|
|
255
|
+
responseHeader['Content-Encoding'] = 'gzip';
|
|
256
|
+
body = gzipSync(Buffer.from(body));
|
|
257
|
+
}
|
|
258
|
+
|
|
259
|
+
response.writeHead(200, responseHeader);
|
|
260
|
+
response.write(body);
|
|
261
|
+
response.end();
|
|
262
|
+
}
|
|
263
|
+
|
|
264
|
+
// 对于大文件,使用流读取
|
|
265
|
+
else {
|
|
266
|
+
sendType = "Stream";
|
|
267
|
+
|
|
268
|
+
if (shouldGzip) {
|
|
269
|
+
// gzip 流式压缩:文件大小变化,不能设置 Content-Length
|
|
270
|
+
responseHeader['Content-Encoding'] = 'gzip';
|
|
271
|
+
response.writeHead(200, responseHeader);
|
|
272
|
+
createReadStream(filePath).pipe(createGzip()).pipe(response);
|
|
273
|
+
} else {
|
|
274
|
+
responseHeader['Content-Length'] = fileInfo.size
|
|
275
|
+
|
|
276
|
+
response.writeHead(200, responseHeader);
|
|
277
|
+
createReadStream(filePath).pipe(response);
|
|
278
|
+
}
|
|
279
|
+
}
|
|
280
|
+
|
|
281
|
+
console.log("<i> \x1b[1m\x1b[32m[" + name + "] " + sendType + " File: " + url + '\x1b[0m ' + new Date().toLocaleString() + (shouldGzip ? " \x1b[33m\x1b[1mgzip\x1b[0m" : "") + (isXHR ? " \x1b[33m\x1b[1m" + request.method + "\x1b[0m" : "") + " " + rawIp);
|
|
282
|
+
}
|
|
283
|
+
|
|
284
|
+
// 否则就是404
|
|
285
|
+
else {
|
|
286
|
+
response.writeHead(404, {
|
|
287
|
+
'Content-Type': buildContentType("text/html"),
|
|
288
|
+
'Access-Control-Allow-Origin': '*',
|
|
289
|
+
'Server': 'Powered by ' + name + '@' + version
|
|
290
|
+
});
|
|
291
|
+
response.write(resolve404(filePath, url, website, config.name || "OIPage", rawIp));
|
|
292
|
+
response.end();
|
|
293
|
+
}
|
|
294
|
+
}
|
|
295
|
+
|
|
296
|
+
});
|
|
297
|
+
|
|
298
|
+
Server.listen(port);
|
|
299
|
+
|
|
300
|
+
// 获取网络信息
|
|
301
|
+
let networkValue = network();
|
|
302
|
+
|
|
303
|
+
// 打印成功提示
|
|
304
|
+
console.log('<i> \x1b[1m\x1b[32m[' + name + '] Project is running at:\x1b[0m');
|
|
305
|
+
console.log('<i> \x1b[1m\x1b[32m[' + name + '] Loopback: \x1b[36mhttp://localhost:' + port + '/\x1b[0m');
|
|
306
|
+
for (let ipv4Item of networkValue.IPv4) console.log('<i> \x1b[1m\x1b[32m[' + name + '] On Your Network (IPv4):\x1b[36m http://' + ipv4Item.address + ':' + port + '/\x1b[0m');
|
|
307
|
+
for (let ipv6Item of networkValue.IPv6) console.log('<i> \x1b[1m\x1b[32m[' + name + '] On Your Network (IPv6): \x1b[36mhttp://[' + ipv6Item.address + ']:' + port + '/\x1b[0m');
|
|
308
|
+
console.log('<i> \x1b[1m\x1b[32m[' + name + '] Content not from ' + (config.name || "OIPage") + ' is served from \x1b[36m"' + basePath + '" \x1b[0mdirectory');
|
|
309
|
+
|
|
310
|
+
if (typeof config.devServer.open === "string" || config.devServer.open) {
|
|
311
|
+
let url = 'http://localhost:' + port + "/" + (typeof config.devServer.open === "string" ? config.devServer.open : "");
|
|
312
|
+
openBrowser(url);
|
|
313
|
+
console.log('<i> \x1b[1m\x1b[32m[' + name + '] Open: \x1b[36m' + url + '\x1b[0m');
|
|
314
|
+
}
|
|
315
|
+
|
|
316
|
+
if (!cache) console.log('<i> \x1b[1m\x1b[32m[' + name + '] Cancel 304 Cache!\x1b[0m');
|
|
317
|
+
if (gzip) console.log('<i> \x1b[1m\x1b[32m[' + name + '] Gzip Compression: \x1b[36menabled\x1b[0m');
|
|
318
|
+
console.log('\n' + (config.name || "OIPage") + ' ' + version + ' compiled \x1b[1m\x1b[32msuccessfully\x1b[0m in ' + (new Date().valueOf() - startTime) + ' ms\n');
|
|
319
319
|
};
|