cmvr-wms/mobile-app/metro.config.js

33 lines
2.1 KiB
JavaScript
Raw Normal View History

const { getDefaultConfig } = require('expo/metro-config');
const http = require('node:http');
const https = require('node:https');
const { environment, internal } = require('./config/environment.cjs');
const config = getDefaultConfig(__dirname);
// 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(?:[\\/]|$)/];
// 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; }
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'];
const transport = target.protocol === 'https:' ? https : http;
const upstream = transport.request(target, { method:req.method, headers,timeout:220000 }, response => {
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;