mirror of
				https://github.com/public-transport/db-vendo-client.git
				synced 2025-10-31 08:06:33 +02:00 
			
		
		
		
	Removed Proxy and local address code
This commit is contained in:
		
							parent
							
								
									9314e59053
								
							
						
					
					
						commit
						f7909aac29
					
				
					 2 changed files with 57 additions and 61 deletions
				
			
		
							
								
								
									
										117
									
								
								lib/request.js
									
										
									
									
									
								
							
							
						
						
									
										117
									
								
								lib/request.js
									
										
									
									
									
								
							|  | @ -1,62 +1,62 @@ | ||||||
| import ProxyAgent from 'https-proxy-agent'; | // import ProxyAgent from 'https-proxy-agent';
 | ||||||
| import {isIP} from 'net'; | // import {isIP} from 'net';
 | ||||||
| import {Agent as HttpsAgent} from 'https'; | // import {Agent as HttpsAgent} from 'https';
 | ||||||
| import roundRobin from '@derhuerst/round-robin-scheduler'; | // import roundRobin from '@derhuerst/round-robin-scheduler';
 | ||||||
| import {randomBytes} from 'crypto'; | import {randomBytes} from 'crypto'; | ||||||
| import {stringify} from 'qs'; | import {stringify} from 'qs'; | ||||||
| import {Request, fetch} from 'cross-fetch'; | import {Request, fetch} from 'cross-fetch'; | ||||||
| import {parse as parseContentType} from 'content-type'; | import {parse as parseContentType} from 'content-type'; | ||||||
| import {HafasError} from './errors.js'; | import {HafasError} from './errors.js'; | ||||||
| 
 | 
 | ||||||
| const proxyAddress = process.env.HTTPS_PROXY || process.env.HTTP_PROXY || null; | // const proxyAddress = process.env.HTTPS_PROXY || process.env.HTTP_PROXY || null;
 | ||||||
| const localAddresses = process.env.LOCAL_ADDRESS || null; | // const localAddresses = process.env.LOCAL_ADDRESS || null;
 | ||||||
| 
 | 
 | ||||||
| if (proxyAddress && localAddresses) { | // if (proxyAddress && localAddresses) {
 | ||||||
| 	console.error('Both env vars HTTPS_PROXY/HTTP_PROXY and LOCAL_ADDRESS are not supported.'); | // 	console.error('Both env vars HTTPS_PROXY/HTTP_PROXY and LOCAL_ADDRESS are not supported.');
 | ||||||
| 	process.exit(1); | // 	process.exit(1);
 | ||||||
| } | // }
 | ||||||
| 
 | 
 | ||||||
| const plainAgent = new HttpsAgent({ | // const plainAgent = new HttpsAgent({
 | ||||||
| 	keepAlive: true, | // 	keepAlive: true,
 | ||||||
| }); | // });
 | ||||||
| let getAgent = () => plainAgent; | // let getAgent = () => plainAgent;
 | ||||||
| 
 | 
 | ||||||
| if (proxyAddress) { | // if (proxyAddress) {
 | ||||||
| 	const agent = new ProxyAgent(proxyAddress, { | // 	const agent = new ProxyAgent(proxyAddress, {
 | ||||||
| 		keepAlive: true, | // 		keepAlive: true,
 | ||||||
| 		keepAliveMsecs: 10 * 1000, // 10s
 | // 		keepAliveMsecs: 10 * 1000, // 10s
 | ||||||
| 	}); | // 	});
 | ||||||
| 	getAgent = () => agent; | // 	getAgent = () => agent;
 | ||||||
| } else if (localAddresses) { | // } else if (localAddresses) {
 | ||||||
| 	const agents = process.env.LOCAL_ADDRESS.split(',') | // 	const agents = process.env.LOCAL_ADDRESS.split(',')
 | ||||||
| 		.map((addr) => { | // 		.map((addr) => {
 | ||||||
| 			const family = isIP(addr); | // 			const family = isIP(addr);
 | ||||||
| 			if (family === 0) { | // 			if (family === 0) {
 | ||||||
| 				throw new Error('invalid local address:' + addr); | // 				throw new Error('invalid local address:' + addr);
 | ||||||
| 			} | // 			}
 | ||||||
| 			return new HttpsAgent({ | // 			return new HttpsAgent({
 | ||||||
| 				localAddress: addr, family, | // 				localAddress: addr, family,
 | ||||||
| 				keepAlive: true, | // 				keepAlive: true,
 | ||||||
| 			}); | // 			});
 | ||||||
| 		}); | // 		});
 | ||||||
| 	const pool = roundRobin(agents); | // 	const pool = roundRobin(agents);
 | ||||||
| 	getAgent = () => pool.get(); | // 	getAgent = () => pool.get();
 | ||||||
| } | // }
 | ||||||
| 
 | 
 | ||||||
| const id = randomBytes(3) | // const id = randomBytes(3)
 | ||||||
| 	.toString('hex'); | // 	.toString('hex');
 | ||||||
| const randomizeUserAgent = (userAgent) => { | // const randomizeUserAgent = (userAgent) => {
 | ||||||
| 	let ua = userAgent; | // 	let ua = userAgent;
 | ||||||
| 	for ( | // 	for (
 | ||||||
| 		let i = Math.round(5 + Math.random() * 5); | // 		let i = Math.round(5 + Math.random() * 5);
 | ||||||
| 		i < ua.length; | // 		i < ua.length;
 | ||||||
| 		i += Math.round(5 + Math.random() * 5) | // 		i += Math.round(5 + Math.random() * 5)
 | ||||||
| 	) { | // 	) {
 | ||||||
| 		ua = ua.slice(0, i) + id + ua.slice(i); | // 		ua = ua.slice(0, i) + id + ua.slice(i);
 | ||||||
| 		i += id.length; | // 		i += id.length;
 | ||||||
| 	} | // 	}
 | ||||||
| 	return ua; | // 	return ua;
 | ||||||
| }; | // };
 | ||||||
| 
 | 
 | ||||||
| const checkIfResponseIsOk = (_) => { | const checkIfResponseIsOk = (_) => { | ||||||
| 	const { | 	const { | ||||||
|  | @ -101,8 +101,8 @@ const request = async (ctx, userAgent, reqData) => { | ||||||
| 	delete reqData.endpoint; | 	delete reqData.endpoint; | ||||||
| 	const rawReqBody = profile.transformReqBody(ctx, reqData.body); | 	const rawReqBody = profile.transformReqBody(ctx, reqData.body); | ||||||
| 
 | 
 | ||||||
| 	const req = profile.transformReq(ctx, { | 	const reqOptions = profile.transformReq(ctx, { | ||||||
| 		agent: getAgent(), | 		keepalive: true, | ||||||
| 		method: reqData.method, | 		method: reqData.method, | ||||||
| 		// todo: CORS? referrer policy?
 | 		// todo: CORS? referrer policy?
 | ||||||
| 		body: JSON.stringify(rawReqBody), | 		body: JSON.stringify(rawReqBody), | ||||||
|  | @ -111,10 +111,7 @@ const request = async (ctx, userAgent, reqData) => { | ||||||
| 			'Accept-Encoding': 'gzip, br, deflate', | 			'Accept-Encoding': 'gzip, br, deflate', | ||||||
| 			'Accept': 'application/json', | 			'Accept': 'application/json', | ||||||
| 			'Accept-Language': opt.language || profile.defaultLanguage || 'en', | 			'Accept-Language': opt.language || profile.defaultLanguage || 'en', | ||||||
| 			'user-agent': profile.randomizeUserAgent | 			'user-agent': userAgent, | ||||||
| 				? randomizeUserAgent(userAgent) |  | ||||||
| 				: userAgent, |  | ||||||
| 			'connection': 'keep-alive', // prevent excessive re-connecting
 |  | ||||||
| 			...reqData.headers, | 			...reqData.headers, | ||||||
| 		}, | 		}, | ||||||
| 		redirect: 'follow', | 		redirect: 'follow', | ||||||
|  | @ -122,15 +119,15 @@ const request = async (ctx, userAgent, reqData) => { | ||||||
| 	}); | 	}); | ||||||
| 
 | 
 | ||||||
| 	let url = endpoint + (reqData.path || ''); | 	let url = endpoint + (reqData.path || ''); | ||||||
| 	if (req.query) { | 	if (reqOptions.query) { | ||||||
| 		url += '?' + stringify(req.query, {arrayFormat: 'brackets', encodeValuesOnly: true}); | 		url += '?' + stringify(reqOptions.query, {arrayFormat: 'brackets', encodeValuesOnly: true}); | ||||||
| 	} | 	} | ||||||
| 	const reqId = randomBytes(3) | 	const reqId = randomBytes(3) | ||||||
| 		.toString('hex'); | 		.toString('hex'); | ||||||
| 	const fetchReq = new Request(url, req); | 	const fetchReq = new Request(url, reqOptions); | ||||||
| 	profile.logRequest(ctx, fetchReq, reqId); | 	profile.logRequest(ctx, fetchReq, reqId); | ||||||
| 
 | 
 | ||||||
| 	const res = await fetch(url, req); | 	const res = await fetch(url, reqOptions); | ||||||
| 
 | 
 | ||||||
| 	const errProps = { | 	const errProps = { | ||||||
| 		// todo [breaking]: assign as non-enumerable property
 | 		// todo [breaking]: assign as non-enumerable property
 | ||||||
|  | @ -150,7 +147,7 @@ const request = async (ctx, userAgent, reqData) => { | ||||||
| 	let cType = res.headers.get('content-type'); | 	let cType = res.headers.get('content-type'); | ||||||
| 	if (cType) { | 	if (cType) { | ||||||
| 		const {type} = parseContentType(cType); | 		const {type} = parseContentType(cType); | ||||||
| 		if (type !== req.headers['Accept']) { | 		if (type !== reqOptions.headers['Accept']) { | ||||||
| 			throw new HafasError('invalid/unsupported response content-type: ' + cType, null, errProps); | 			throw new HafasError('invalid/unsupported response content-type: ' + cType, null, errProps); | ||||||
| 		} | 		} | ||||||
| 	} | 	} | ||||||
|  |  | ||||||
|  | @ -56,7 +56,6 @@ | ||||||
| 		"node": ">=16" | 		"node": ">=16" | ||||||
| 	}, | 	}, | ||||||
| 	"dependencies": { | 	"dependencies": { | ||||||
| 		"@derhuerst/round-robin-scheduler": "^1.0.4", |  | ||||||
| 		"content-type": "^1.0.4", | 		"content-type": "^1.0.4", | ||||||
| 		"cross-fetch": "^4.0.0", | 		"cross-fetch": "^4.0.0", | ||||||
| 		"db-hafas-stations": "^1.0.0", | 		"db-hafas-stations": "^1.0.0", | ||||||
|  |  | ||||||
		Loading…
	
	Add table
		
		Reference in a new issue