1 import http from 'node:http';
2 import httpProxy from 'http-proxy';
3 import sirv from 'sirv';
4 import fse from 'fs-extra';
6 const testing_port = 9999;
7 const use_proxy = !!process.env.API_ON_SAME_PORT;
8 const static_port = use_proxy ? 9998 : 9999;
9 const reverse_only = !!process.env.REVERSE_ONLY;
11 export default async function globalSetup() {
12 const workdir = 'dist_for_testing';
14 fse.mkdirpSync(workdir);
15 fse.copySync('dist', workdir);
17 const api_endpoint = use_proxy
19 : 'https:/nominatim.openstreetmap.org/';
21 fse.outputFileSync(workdir + '/theme/config.theme.js', `
22 Nominatim_Config.Nominatim_API_Endpoint = '${api_endpoint}';
23 Nominatim_Config.Reverse_Only = ${reverse_only};
26 const handler = sirv(workdir);
27 const server = http.createServer(handler).listen(static_port);
28 await new Promise(resolve => server.on('listening', resolve));
30 `static server serving ${workdir} directory running on port ${static_port}`
33 globalThis.__staticServer = server;
36 const proxy = httpProxy.createProxy({
38 followRedirects: true,
40 globalThis.__proxy = proxy;
41 console.log('proxy started');
43 const proxy_server = http.createServer((req, res) => {
44 const api_url_match = !req.url.match(
45 /(.html|.css|.js|.ico|.png|.svg)(\?|$)/
47 const target = api_url_match
48 ? 'https://nominatim.openstreetmap.org'
49 : 'http://localhost:' + static_port;
50 return proxy.web(req, res, { target });
51 }).listen(testing_port);
53 globalThis.__proxyServer = proxy_server;
54 console.log(`proxy server started on port ${testing_port}`);