66 lines
2.4 KiB
JavaScript
66 lines
2.4 KiB
JavaScript
/* serve-test.js —— e2e 用的本地静态服务器(v3.25.15 起脱离网页部署)
|
||
*
|
||
* 历史:本项目早期同时发布网页版(Cloudflare Pages),这个脚本会解析 `_headers` 下发 CSP、
|
||
* 并模拟 Pages Functions(/api/img、/plugins/*)。**现在是桌面客户端项目**,那些部署面已删除,
|
||
* 因此这里只保留「能跑 e2e」的最小职责:把 dist/ 当静态目录伺服。
|
||
*
|
||
* 桌面端的 CSP 由 src-tauri/tauri.conf.json 决定,e2e 不模拟 webview 的 CSP(要验就装真包)。
|
||
* 用法:node scripts/serve-test.js(IWB_LOCAL_PORT 可改端口,默认 8787)
|
||
*/
|
||
'use strict'
|
||
|
||
const http = require('node:http')
|
||
const fs = require('node:fs')
|
||
const path = require('node:path')
|
||
|
||
const root = path.resolve(__dirname, '..')
|
||
const dist = path.join(root, 'dist')
|
||
const port = Number(process.env.IWB_LOCAL_PORT || 8787)
|
||
|
||
const mime = {
|
||
'.html': 'text/html; charset=utf-8',
|
||
'.js': 'text/javascript; charset=utf-8',
|
||
'.css': 'text/css; charset=utf-8',
|
||
'.json': 'application/json; charset=utf-8',
|
||
'.png': 'image/png',
|
||
'.jpg': 'image/jpeg',
|
||
'.jpeg': 'image/jpeg',
|
||
'.webp': 'image/webp',
|
||
'.gif': 'image/gif',
|
||
'.svg': 'image/svg+xml',
|
||
'.ico': 'image/x-icon',
|
||
'.woff2': 'font/woff2',
|
||
'.map': 'application/json; charset=utf-8',
|
||
}
|
||
|
||
const server = http.createServer((req, res) => {
|
||
let pathname = '/'
|
||
try { pathname = decodeURIComponent(new URL(req.url, 'http://127.0.0.1').pathname) } catch (e) { pathname = '/' }
|
||
if (pathname.endsWith('/')) pathname += 'index.html'
|
||
|
||
let file = path.join(dist, pathname)
|
||
// 目录穿越防护:只允许 servicedir 内的文件
|
||
if (!file.startsWith(dist + path.sep)) {
|
||
res.writeHead(403, { 'content-type': 'text/plain; charset=utf-8' })
|
||
res.end('forbidden')
|
||
return
|
||
}
|
||
fs.stat(file, (err, st) => {
|
||
if (err || !st.isFile()) {
|
||
res.writeHead(404, { 'content-type': 'text/plain; charset=utf-8' })
|
||
res.end('not found')
|
||
return
|
||
}
|
||
res.writeHead(200, {
|
||
'content-type': mime[path.extname(file)] || 'application/octet-stream',
|
||
'cache-control': 'no-store',
|
||
})
|
||
fs.createReadStream(file).pipe(res)
|
||
})
|
||
})
|
||
|
||
server.listen(port, () => {
|
||
console.log('[serve-test] serving ' + path.relative(process.cwd(), dist) + ' at http://127.0.0.1:' + port)
|
||
console.log('[serve-test] 纯静态:不再注入 _headers / 不再模拟 Pages Functions(项目已转为桌面客户端)')
|
||
})
|