41 lines
3.4 KiB
JavaScript
41 lines
3.4 KiB
JavaScript
// A loopback static/fixture server. No proxy, database driver, transport client or environment credentials.
|
|
import http from 'node:http'
|
|
import { readFile } from 'node:fs/promises'
|
|
import { fileURLToPath } from 'node:url'
|
|
import path from 'node:path'
|
|
import { resolveFixture } from './fixtures.mjs'
|
|
const dist = fileURLToPath(new URL('../node_modules/.zyt-ui-preview/dist/', import.meta.url))
|
|
const port = Number(process.argv[2] ?? 5178), host = '127.0.0.1'
|
|
const origin = `http://${host}:${port}`
|
|
const csp = "default-src 'none'; script-src 'self'; style-src 'self' 'unsafe-inline'; connect-src 'self'; img-src 'self' data: blob:; font-src 'self' data:; media-src 'none'; frame-src 'none'; worker-src 'none'; form-action 'none'; object-src 'none'; base-uri 'none'; frame-ancestors 'none'"
|
|
const types = {'.js':'application/javascript','.css':'text/css','.svg':'image/svg+xml','.woff2':'font/woff2','.woff':'font/woff','.png':'image/png','.jpg':'image/jpeg','.webp':'image/webp','.gif':'image/gif','.ttf':'font/ttf'}
|
|
const routes = new Set(['/','/admin','/admin/','/admin/workbench','/admin/order'])
|
|
async function serve(req,res) {
|
|
res.setHeader('Content-Security-Policy',csp); res.setHeader('Cache-Control','no-store'); res.setHeader('X-Content-Type-Options','nosniff'); res.setHeader('Referrer-Policy','no-referrer')
|
|
const send=(status,type,body)=>{ res.writeHead(status,{'Content-Type':type}); res.end(req.method==='HEAD' ? undefined : body) }
|
|
const json=(status,obj)=>send(status,'application/json; charset=utf-8',JSON.stringify(obj))
|
|
const deny=(status)=>json(status,{code:0,show:1,msg:'本地只读预览:此操作未开放',data:null})
|
|
if(req.headers.host !== `${host}:${port}` || (req.headers.origin && req.headers.origin !== origin) || req.headers['sec-fetch-site']==='cross-site') return deny(403)
|
|
if(!req.url?.startsWith('/') || req.url.startsWith('//')) return deny(400)
|
|
const {pathname,searchParams}=new URL(req.url,origin)
|
|
if(pathname.startsWith('/adminapi/')) {
|
|
let params=Object.fromEntries(searchParams)
|
|
if(pathname==='/adminapi/order.order/detail') {
|
|
if(req.method!=='POST') return deny(405)
|
|
if(!req.headers['content-type']?.startsWith('application/json')) return deny(415)
|
|
let raw=''; for await(const chunk of req){ raw+=chunk; if(raw.length>4096) return deny(413) }
|
|
try { params=JSON.parse(raw); if(!params || typeof params!=='object' || Array.isArray(params)) return deny(400) } catch { return deny(400) }
|
|
} else if(req.method!=='GET') return deny(405)
|
|
const result=resolveFixture(pathname,params); return result ? json(200,result) : deny(403)
|
|
}
|
|
if(!['GET','HEAD'].includes(req.method)) return deny(405)
|
|
if(routes.has(pathname)) return send(200,'text/html; charset=utf-8',await readFile(path.join(dist,'local-preview/index.html')))
|
|
const asset=pathname.match(/^\/admin\/assets\/([a-zA-Z0-9_.-]+)$/)
|
|
if(asset && types[path.extname(asset[1])]) {
|
|
try { return send(200,types[path.extname(asset[1])],await readFile(path.join(dist,'assets',asset[1]))) } catch { return deny(404) }
|
|
}
|
|
return deny(404)
|
|
}
|
|
await readFile(path.join(dist,'local-preview/index.html'))
|
|
http.createServer((req,res)=>serve(req,res).catch(()=>{if(!res.headersSent)res.writeHead(500); res.end()})).listen(port,host,()=>console.log(`本地只读预览:${origin}/admin/workbench(合成数据,无转发)`))
|