Files
vibemake/scripts/model-assets-proxy.mjs
Jerry 1c837766cd feat: integrate robot assembly and simulation with resilient loading
Add backend-driven assembly and simulation experiences, MAKE branding, mobile rendering budgets and desktop-only CAD/PCB routes. Improve logo loading progress, document image recovery and URDF request handling; include dev packaging and regression coverage.
2026-10-30 18:38:39 +08:00

44 lines
2.2 KiB
JavaScript

import https from 'node:https';
import { modelAssetTarget } from '../js/model-assets.js';
const REQUEST_HEADERS = ['accept-encoding', 'range', 'if-range', 'if-none-match', 'if-modified-since'];
const RESPONSE_HEADERS = ['content-type', 'content-length', 'content-encoding', 'content-range', 'accept-ranges', 'cache-control', 'etag', 'last-modified', 'vary'];
export function proxyModelAsset(req, res) {
if (req.method !== 'GET' && req.method !== 'HEAD') {
res.writeHead(405, { Allow: 'GET, HEAD' }).end();
return;
}
const target = modelAssetTarget(req.url);
if (!target) {
res.writeHead(404, { 'Content-Type': 'text/plain; charset=utf-8' }).end('Unknown model asset');
return;
}
// 只转发读取 / 缓存相关请求头,绝不把网站的 Cookie、Authorization 带给 OSS。
const headers = Object.fromEntries(REQUEST_HEADERS.filter((key) => req.headers[key]).map((key) => [key, req.headers[key]]));
const upstream = https.request(target, { method: req.method, headers }, (up) => {
// 不跟随或透传重定向,保证资源始终来自允许的 bucket。
if (up.statusCode >= 300 && up.statusCode < 400 && up.statusCode !== 304) {
up.resume();
res.writeHead(502, { 'Cache-Control': 'no-store' }).end('Unexpected asset redirect');
return;
}
const out = Object.fromEntries(RESPONSE_HEADERS.filter((key) => up.headers[key] !== undefined).map((key) => [key, up.headers[key]]));
out['X-Content-Type-Options'] = 'nosniff';
out['Content-Security-Policy'] = "default-src 'none'; sandbox";
out['Cache-Control'] = up.statusCode < 400 ? (out['cache-control'] || 'public, max-age=86400') : 'no-store';
delete out['cache-control'];
res.writeHead(up.statusCode, out);
up.on('error', () => res.destroy());
up.pipe(res);
res.on('close', () => { if (!res.writableFinished) up.destroy(); });
});
upstream.setTimeout(60_000, () => upstream.destroy(new Error('Model asset timeout')));
upstream.on('error', () => {
if (res.headersSent) { res.destroy(); return; }
res.writeHead(502, { 'Content-Type': 'text/plain; charset=utf-8', 'Cache-Control': 'no-store' }).end('Model asset unavailable');
});
res.on('close', () => { if (!res.writableFinished) upstream.destroy(); });
upstream.end();
}