2026-10-18 08:49:30 +08:00
|
|
|
const { getDefaultConfig } = require('expo/metro-config');
|
|
|
|
|
const http = require('node:http');
|
|
|
|
|
const https = require('node:https');
|
2026-10-20 10:04:17 +08:00
|
|
|
const { environment, internal } = require('./config/environment.cjs');
|
2026-10-18 08:49:30 +08:00
|
|
|
const config = getDefaultConfig(__dirname);
|
2026-10-20 10:04:17 +08:00
|
|
|
// Never crawl or bundle signing credentials, including access-restricted folders.
|
|
|
|
|
const previousBlocks=config.resolver.blockList;
|
|
|
|
|
config.resolver.blockList=[...(Array.isArray(previousBlocks)?previousBlocks:previousBlocks?[previousBlocks]:[]),/[\\/]keys(?:[\\/]|$)/,/[\\/]ota-data(?:[\\/]|$)/];
|
2026-10-18 08:49:30 +08:00
|
|
|
// Web is a developer preview only. A same-origin proxy avoids preview-browser
|
|
|
|
|
// private-network/CORS restrictions; installed Android calls the API directly.
|
|
|
|
|
config.server = { ...config.server, enhanceMiddleware: middleware => (req, res, next) => {
|
|
|
|
|
if (!req.url.startsWith('/native-api/')) return middleware(req, res, next);
|
|
|
|
|
const env = environment();
|
|
|
|
|
const suffix = req.url.slice('/native-api'.length);
|
|
|
|
|
if (suffix.includes('://') || suffix.includes('..')) { res.writeHead(400); res.end(); return; }
|
2026-10-20 10:04:17 +08:00
|
|
|
let apiUrl=env.apiUrl;
|
|
|
|
|
const selected=req.headers['x-cmvr-dev-server'];
|
|
|
|
|
if(selected){
|
|
|
|
|
try{if(env.variant!=='dev'||typeof selected!=='string'||selected.length>240)throw Error('invalid target');apiUrl=internal(selected,true);}
|
|
|
|
|
catch{res.writeHead(400,{'Content-Type':'application/json'});res.end(JSON.stringify({code:400,msg:'请填写有效的开发内网服务地址。'}));return;}
|
|
|
|
|
}
|
|
|
|
|
const target = new URL(apiUrl + suffix);
|
|
|
|
|
const headers={...req.headers,host:target.host};delete headers['x-cmvr-dev-server'];
|
2026-10-18 08:49:30 +08:00
|
|
|
const transport = target.protocol === 'https:' ? https : http;
|
2026-10-20 10:04:17 +08:00
|
|
|
const upstream = transport.request(target, { method:req.method, headers,timeout:220000 }, response => {
|
2026-10-18 08:49:30 +08:00
|
|
|
res.writeHead(response.statusCode, response.headers); response.pipe(res);
|
|
|
|
|
});
|
|
|
|
|
upstream.on('timeout',()=>upstream.destroy());
|
|
|
|
|
upstream.on('error',()=>{if(!res.headersSent)res.writeHead(502,{'Content-Type':'application/json'});res.end(JSON.stringify({code:502,msg:'开发预览暂未连接后端。'}));});
|
|
|
|
|
req.pipe(upstream);
|
|
|
|
|
}};
|
|
|
|
|
module.exports = config;
|