From f7909aac2909e904c7e8eed71b17a93af21a7ab9 Mon Sep 17 00:00:00 2001 From: McToel Date: Thu, 13 Feb 2025 15:25:33 +0100 Subject: [PATCH 1/9] Removed Proxy and local address code --- lib/request.js | 117 ++++++++++++++++++++++++------------------------- package.json | 1 - 2 files changed, 57 insertions(+), 61 deletions(-) diff --git a/lib/request.js b/lib/request.js index 349f86ba..1f0a5db7 100644 --- a/lib/request.js +++ b/lib/request.js @@ -1,62 +1,62 @@ -import ProxyAgent from 'https-proxy-agent'; -import {isIP} from 'net'; -import {Agent as HttpsAgent} from 'https'; -import roundRobin from '@derhuerst/round-robin-scheduler'; +// import ProxyAgent from 'https-proxy-agent'; +// import {isIP} from 'net'; +// import {Agent as HttpsAgent} from 'https'; +// import roundRobin from '@derhuerst/round-robin-scheduler'; import {randomBytes} from 'crypto'; import {stringify} from 'qs'; import {Request, fetch} from 'cross-fetch'; import {parse as parseContentType} from 'content-type'; import {HafasError} from './errors.js'; -const proxyAddress = process.env.HTTPS_PROXY || process.env.HTTP_PROXY || null; -const localAddresses = process.env.LOCAL_ADDRESS || null; +// const proxyAddress = process.env.HTTPS_PROXY || process.env.HTTP_PROXY || null; +// const localAddresses = process.env.LOCAL_ADDRESS || null; -if (proxyAddress && localAddresses) { - console.error('Both env vars HTTPS_PROXY/HTTP_PROXY and LOCAL_ADDRESS are not supported.'); - process.exit(1); -} +// if (proxyAddress && localAddresses) { +// console.error('Both env vars HTTPS_PROXY/HTTP_PROXY and LOCAL_ADDRESS are not supported.'); +// process.exit(1); +// } -const plainAgent = new HttpsAgent({ - keepAlive: true, -}); -let getAgent = () => plainAgent; +// const plainAgent = new HttpsAgent({ +// keepAlive: true, +// }); +// let getAgent = () => plainAgent; -if (proxyAddress) { - const agent = new ProxyAgent(proxyAddress, { - keepAlive: true, - keepAliveMsecs: 10 * 1000, // 10s - }); - getAgent = () => agent; -} else if (localAddresses) { - const agents = process.env.LOCAL_ADDRESS.split(',') - .map((addr) => { - const family = isIP(addr); - if (family === 0) { - throw new Error('invalid local address:' + addr); - } - return new HttpsAgent({ - localAddress: addr, family, - keepAlive: true, - }); - }); - const pool = roundRobin(agents); - getAgent = () => pool.get(); -} +// if (proxyAddress) { +// const agent = new ProxyAgent(proxyAddress, { +// keepAlive: true, +// keepAliveMsecs: 10 * 1000, // 10s +// }); +// getAgent = () => agent; +// } else if (localAddresses) { +// const agents = process.env.LOCAL_ADDRESS.split(',') +// .map((addr) => { +// const family = isIP(addr); +// if (family === 0) { +// throw new Error('invalid local address:' + addr); +// } +// return new HttpsAgent({ +// localAddress: addr, family, +// keepAlive: true, +// }); +// }); +// const pool = roundRobin(agents); +// getAgent = () => pool.get(); +// } -const id = randomBytes(3) - .toString('hex'); -const randomizeUserAgent = (userAgent) => { - let ua = userAgent; - for ( - let i = Math.round(5 + Math.random() * 5); - i < ua.length; - i += Math.round(5 + Math.random() * 5) - ) { - ua = ua.slice(0, i) + id + ua.slice(i); - i += id.length; - } - return ua; -}; +// const id = randomBytes(3) +// .toString('hex'); +// const randomizeUserAgent = (userAgent) => { +// let ua = userAgent; +// for ( +// let i = Math.round(5 + Math.random() * 5); +// i < ua.length; +// i += Math.round(5 + Math.random() * 5) +// ) { +// ua = ua.slice(0, i) + id + ua.slice(i); +// i += id.length; +// } +// return ua; +// }; const checkIfResponseIsOk = (_) => { const { @@ -101,8 +101,8 @@ const request = async (ctx, userAgent, reqData) => { delete reqData.endpoint; const rawReqBody = profile.transformReqBody(ctx, reqData.body); - const req = profile.transformReq(ctx, { - agent: getAgent(), + const reqOptions = profile.transformReq(ctx, { + keepalive: true, method: reqData.method, // todo: CORS? referrer policy? body: JSON.stringify(rawReqBody), @@ -111,10 +111,7 @@ const request = async (ctx, userAgent, reqData) => { 'Accept-Encoding': 'gzip, br, deflate', 'Accept': 'application/json', 'Accept-Language': opt.language || profile.defaultLanguage || 'en', - 'user-agent': profile.randomizeUserAgent - ? randomizeUserAgent(userAgent) - : userAgent, - 'connection': 'keep-alive', // prevent excessive re-connecting + 'user-agent': userAgent, ...reqData.headers, }, redirect: 'follow', @@ -122,15 +119,15 @@ const request = async (ctx, userAgent, reqData) => { }); let url = endpoint + (reqData.path || ''); - if (req.query) { - url += '?' + stringify(req.query, {arrayFormat: 'brackets', encodeValuesOnly: true}); + if (reqOptions.query) { + url += '?' + stringify(reqOptions.query, {arrayFormat: 'brackets', encodeValuesOnly: true}); } const reqId = randomBytes(3) .toString('hex'); - const fetchReq = new Request(url, req); + const fetchReq = new Request(url, reqOptions); profile.logRequest(ctx, fetchReq, reqId); - const res = await fetch(url, req); + const res = await fetch(url, reqOptions); const errProps = { // todo [breaking]: assign as non-enumerable property @@ -150,7 +147,7 @@ const request = async (ctx, userAgent, reqData) => { let cType = res.headers.get('content-type'); if (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); } } diff --git a/package.json b/package.json index e92832f5..63a61a43 100644 --- a/package.json +++ b/package.json @@ -56,7 +56,6 @@ "node": ">=16" }, "dependencies": { - "@derhuerst/round-robin-scheduler": "^1.0.4", "content-type": "^1.0.4", "cross-fetch": "^4.0.0", "db-hafas-stations": "^1.0.0", From 46e21f8d7530357477a9606280c5da113171276f Mon Sep 17 00:00:00 2001 From: McToel Date: Thu, 13 Feb 2025 16:01:29 +0100 Subject: [PATCH 2/9] replaced node crypto with web crypto --- lib/request.js | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/lib/request.js b/lib/request.js index 1f0a5db7..120ee380 100644 --- a/lib/request.js +++ b/lib/request.js @@ -2,7 +2,6 @@ // import {isIP} from 'net'; // import {Agent as HttpsAgent} from 'https'; // import roundRobin from '@derhuerst/round-robin-scheduler'; -import {randomBytes} from 'crypto'; import {stringify} from 'qs'; import {Request, fetch} from 'cross-fetch'; import {parse as parseContentType} from 'content-type'; @@ -58,6 +57,14 @@ import {HafasError} from './errors.js'; // return ua; // }; +const randomBytesHex = (nBytes = 8) => { + const array = new Uint8Array(nBytes); + crypto.getRandomValues(array); + return Array.from(array) + .map((byte) => byte.toString(16).padStart(2, '0')) + .join(''); + }; + const checkIfResponseIsOk = (_) => { const { body, @@ -122,8 +129,7 @@ const request = async (ctx, userAgent, reqData) => { if (reqOptions.query) { url += '?' + stringify(reqOptions.query, {arrayFormat: 'brackets', encodeValuesOnly: true}); } - const reqId = randomBytes(3) - .toString('hex'); + const reqId = randomBytesHex(3); const fetchReq = new Request(url, reqOptions); profile.logRequest(ctx, fetchReq, reqId); From e425d9b01296d913372be4cb091b4f7b5a43cbdd Mon Sep 17 00:00:00 2001 From: McToel Date: Fri, 14 Feb 2025 00:40:00 +0100 Subject: [PATCH 3/9] Replaced require with static imports --- p/db/index.js | 6 ++---- p/dbnav/index.js | 5 +---- p/dbregioguide/index.js | 4 +--- p/dbweb/index.js | 5 +---- test/dbnav-departures.js | 4 +--- test/dbnav-refresh-journey.js | 4 +--- test/dbnav-stop.js | 4 +--- test/dbnav-trip.js | 5 +---- test/dbregioguide-trip.js | 5 +---- test/dbris-arrivals.js | 5 +---- test/dbweb-departures.js | 5 +---- test/dbweb-journey.js | 5 +---- test/dbweb-refresh-journey.js | 5 +---- test/dbweb-trip.js | 5 +---- test/lib/request.js | 3 --- 15 files changed, 15 insertions(+), 55 deletions(-) diff --git a/p/db/index.js b/p/db/index.js index af5cdb5d..3878fb97 100644 --- a/p/db/index.js +++ b/p/db/index.js @@ -1,7 +1,5 @@ -import {createRequire} from 'module'; -const require = createRequire(import.meta.url); -const dbnavBase = require('../dbnav/base.json'); -const dbregioguideBase = require('../dbregioguide/base.json'); +import dbnavBase from '../dbnav/base.json' with { type: "json" }; +import dbregioguideBase from '../dbregioguide/base.json' with { type: "json" }; import {products} from '../../lib/products.js'; // journeys() diff --git a/p/dbnav/index.js b/p/dbnav/index.js index a093c1ce..bf21297f 100644 --- a/p/dbnav/index.js +++ b/p/dbnav/index.js @@ -1,7 +1,4 @@ -import {createRequire} from 'module'; -const require = createRequire(import.meta.url); - -const baseProfile = require('./base.json'); +import baseProfile from './base.json' with { type: "json" }; import {products} from '../../lib/products.js'; import {formatJourneysReq, formatRefreshJourneyReq} from './journeys-req.js'; import {formatTripReq} from './trip-req.js'; diff --git a/p/dbregioguide/index.js b/p/dbregioguide/index.js index 4a67c738..7a8f5b83 100644 --- a/p/dbregioguide/index.js +++ b/p/dbregioguide/index.js @@ -1,6 +1,4 @@ -import {createRequire} from 'module'; -const require = createRequire(import.meta.url); -const baseProfile = require('./base.json'); +import baseProfile from './base.json' with { type: "json" }; import {products} from '../../lib/products.js'; import {formatTripReq} from './trip-req.js'; diff --git a/p/dbweb/index.js b/p/dbweb/index.js index 32be5a53..9752ba57 100644 --- a/p/dbweb/index.js +++ b/p/dbweb/index.js @@ -1,7 +1,4 @@ -import {createRequire} from 'module'; -const require = createRequire(import.meta.url); - -const baseProfile = require('./base.json'); +import baseProfile from './base.json' with { type: "json" }; import {products} from '../../lib/products.js'; import {formatJourneysReq, formatRefreshJourneyReq} from './journeys-req.js'; import {formatLocationFilter} from './location-filter.js'; diff --git a/test/dbnav-departures.js b/test/dbnav-departures.js index 4fde5139..42a7d844 100644 --- a/test/dbnav-departures.js +++ b/test/dbnav-departures.js @@ -1,13 +1,11 @@ // todo: use import assertions once they're supported by Node.js & ESLint // https://github.com/tc39/proposal-import-assertions -import {createRequire} from 'module'; -const require = createRequire(import.meta.url); import tap from 'tap'; import {createClient} from '../index.js'; import {profile as rawProfile} from '../p/dbnav/index.js'; -const res = require('./fixtures/dbnav-departures.json'); +import res from './fixtures/dbnav-departures.json' with { type: "json" }; import {dbnavDepartures as expected} from './fixtures/dbnav-departures.js'; const client = createClient(rawProfile, 'public-transport/hafas-client:test', {enrichStations: false}); diff --git a/test/dbnav-refresh-journey.js b/test/dbnav-refresh-journey.js index 1b35ceed..1e97a5e2 100644 --- a/test/dbnav-refresh-journey.js +++ b/test/dbnav-refresh-journey.js @@ -1,13 +1,11 @@ // todo: use import assertions once they're supported by Node.js & ESLint // https://github.com/tc39/proposal-import-assertions -import {createRequire} from 'module'; -const require = createRequire(import.meta.url); import tap from 'tap'; import {createClient} from '../index.js'; import {profile as rawProfile} from '../p/dbnav/index.js'; -const res = require('./fixtures/dbnav-refresh-journey.json'); +import res from './fixtures/dbnav-refresh-journey.json' with { type: "json" }; import {dbNavJourney as expected} from './fixtures/dbnav-refresh-journey.js'; const client = createClient(rawProfile, 'public-transport/hafas-client:test', {enrichStations: false}); diff --git a/test/dbnav-stop.js b/test/dbnav-stop.js index 0e167b5a..247d3da8 100644 --- a/test/dbnav-stop.js +++ b/test/dbnav-stop.js @@ -1,13 +1,11 @@ // todo: use import assertions once they're supported by Node.js & ESLint // https://github.com/tc39/proposal-import-assertions -import {createRequire} from 'module'; -const require = createRequire(import.meta.url); import tap from 'tap'; import {createClient} from '../index.js'; import {profile as rawProfile} from '../p/dbnav/index.js'; -const res = require('./fixtures/dbnav-stop.json'); +import res from './fixtures/dbnav-stop.json' with { type: "json" }; import {dbnavDepartures as expected} from './fixtures/dbnav-stop.js'; const client = createClient(rawProfile, 'public-transport/hafas-client:test', {enrichStations: false}); diff --git a/test/dbnav-trip.js b/test/dbnav-trip.js index 62ef9684..12c1770b 100644 --- a/test/dbnav-trip.js +++ b/test/dbnav-trip.js @@ -1,13 +1,10 @@ // todo: use import assertions once they're supported by Node.js & ESLint // https://github.com/tc39/proposal-import-assertions -import {createRequire} from 'module'; -const require = createRequire(import.meta.url); - import tap from 'tap'; import {createClient} from '../index.js'; import {profile as rawProfile} from '../p/dbnav/index.js'; -const res = require('./fixtures/dbnav-trip.json'); +import res from './fixtures/dbnav-trip.json' with { type: "json" }; import {dbTrip as expected} from './fixtures/dbnav-trip.js'; const client = createClient(rawProfile, 'public-transport/hafas-client:test', {enrichStations: false}); diff --git a/test/dbregioguide-trip.js b/test/dbregioguide-trip.js index 8bb38b6a..86ba5120 100644 --- a/test/dbregioguide-trip.js +++ b/test/dbregioguide-trip.js @@ -1,13 +1,10 @@ // todo: use import assertions once they're supported by Node.js & ESLint // https://github.com/tc39/proposal-import-assertions -import {createRequire} from 'module'; -const require = createRequire(import.meta.url); - import tap from 'tap'; import {createClient} from '../index.js'; import {profile as rawProfile} from '../p/dbregioguide/index.js'; -const res = require('./fixtures/dbregioguide-trip.json'); +import res from './fixtures/dbregioguide-trip.json' with { type: "json" }; import {dbTrip as expected} from './fixtures/dbregioguide-trip.js'; const client = createClient(rawProfile, 'public-transport/hafas-client:test', {enrichStations: false}); diff --git a/test/dbris-arrivals.js b/test/dbris-arrivals.js index 3ddc9140..bd0bba3c 100644 --- a/test/dbris-arrivals.js +++ b/test/dbris-arrivals.js @@ -1,13 +1,10 @@ // todo: use import assertions once they're supported by Node.js & ESLint // https://github.com/tc39/proposal-import-assertions -import {createRequire} from 'module'; -const require = createRequire(import.meta.url); - import tap from 'tap'; import {createClient} from '../index.js'; import {profile as rawProfile} from '../p/dbweb/index.js'; -const res = require('./fixtures/dbris-arrivals.json'); +import res from './fixtures/dbris-arrivals.json' with { type: "json" }; import {dbArrivals as expected} from './fixtures/dbris-arrivals.js'; const client = createClient(rawProfile, 'public-transport/hafas-client:test', {enrichStations: false}); diff --git a/test/dbweb-departures.js b/test/dbweb-departures.js index 3b4a1227..69538643 100644 --- a/test/dbweb-departures.js +++ b/test/dbweb-departures.js @@ -1,13 +1,10 @@ // todo: use import assertions once they're supported by Node.js & ESLint // https://github.com/tc39/proposal-import-assertions -import {createRequire} from 'module'; -const require = createRequire(import.meta.url); - import tap from 'tap'; import {createClient} from '../index.js'; import {profile as rawProfile} from '../p/dbweb/index.js'; -const res = require('./fixtures/dbweb-departures.json'); +import res from './fixtures/dbweb-departures.json' with { type: "json" }; import {dbwebDepartures as expected} from './fixtures/dbweb-departures.js'; const client = createClient(rawProfile, 'public-transport/hafas-client:test', {enrichStations: true}); diff --git a/test/dbweb-journey.js b/test/dbweb-journey.js index f777c829..1f3c4099 100644 --- a/test/dbweb-journey.js +++ b/test/dbweb-journey.js @@ -1,13 +1,10 @@ // todo: use import assertions once they're supported by Node.js & ESLint // https://github.com/tc39/proposal-import-assertions -import {createRequire} from 'module'; -const require = createRequire(import.meta.url); - import tap from 'tap'; import {createClient} from '../index.js'; import {profile as rawProfile} from '../p/dbweb/index.js'; -const res = require('./fixtures/dbweb-journey.json'); +import res from './fixtures/dbweb-journey.json' with { type: "json" }; import {dbwebJourney as expected} from './fixtures/dbweb-journey.js'; const client = createClient(rawProfile, 'public-transport/hafas-client:test', {enrichStations: false}); diff --git a/test/dbweb-refresh-journey.js b/test/dbweb-refresh-journey.js index fe97f2b7..64940f73 100644 --- a/test/dbweb-refresh-journey.js +++ b/test/dbweb-refresh-journey.js @@ -1,13 +1,10 @@ // todo: use import assertions once they're supported by Node.js & ESLint // https://github.com/tc39/proposal-import-assertions -import {createRequire} from 'module'; -const require = createRequire(import.meta.url); - import tap from 'tap'; import {createClient} from '../index.js'; import {profile as rawProfile} from '../p/dbweb/index.js'; -const res = require('./fixtures/dbweb-refresh-journey.json'); +import res from './fixtures/dbweb-refresh-journey.json' with { type: "json" }; import {dbJourney as expected} from './fixtures/dbweb-refresh-journey.js'; const client = createClient(rawProfile, 'public-transport/hafas-client:test', {enrichStations: false}); diff --git a/test/dbweb-trip.js b/test/dbweb-trip.js index 64e8cab3..8236c7fd 100644 --- a/test/dbweb-trip.js +++ b/test/dbweb-trip.js @@ -1,13 +1,10 @@ // todo: use import assertions once they're supported by Node.js & ESLint // https://github.com/tc39/proposal-import-assertions -import {createRequire} from 'module'; -const require = createRequire(import.meta.url); - import tap from 'tap'; import {createClient} from '../index.js'; import {profile as rawProfile} from '../p/dbweb/index.js'; -const res = require('./fixtures/dbweb-trip.json'); +import res from './fixtures/dbweb-trip.json' with { type: "json" }; import {dbwebTrip as expected} from './fixtures/dbweb-trip.js'; const client = createClient(rawProfile, 'public-transport/hafas-client:test', {enrichStations: false}); diff --git a/test/lib/request.js b/test/lib/request.js index 1cd532ad..998ce215 100644 --- a/test/lib/request.js +++ b/test/lib/request.js @@ -1,8 +1,5 @@ // todo: use import assertions once they're supported by Node.js & ESLint // https://github.com/tc39/proposal-import-assertions -import {createRequire} from 'module'; -const require = createRequire(import.meta.url); - import tap from 'tap'; import { checkIfResponseIsOk as checkIfResIsOk, From a9eacd20b5b58f008d20af1c39e3817fa2cfa6b7 Mon Sep 17 00:00:00 2001 From: McToel Date: Fri, 14 Feb 2025 00:43:22 +0100 Subject: [PATCH 4/9] removed commented out imports --- lib/request.js | 4 ---- 1 file changed, 4 deletions(-) diff --git a/lib/request.js b/lib/request.js index 120ee380..e9a02b73 100644 --- a/lib/request.js +++ b/lib/request.js @@ -1,7 +1,3 @@ -// import ProxyAgent from 'https-proxy-agent'; -// import {isIP} from 'net'; -// import {Agent as HttpsAgent} from 'https'; -// import roundRobin from '@derhuerst/round-robin-scheduler'; import {stringify} from 'qs'; import {Request, fetch} from 'cross-fetch'; import {parse as parseContentType} from 'content-type'; From 47e1285600331bc9a9a6fec03ec1b246bff475ae Mon Sep 17 00:00:00 2001 From: McToel Date: Sun, 16 Feb 2025 02:08:07 +0100 Subject: [PATCH 5/9] import db-hafas-stations on demand --- index.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/index.js b/index.js index ee787c2e..fd63c644 100644 --- a/index.js +++ b/index.js @@ -1,6 +1,5 @@ import isObj from 'lodash/isObject.js'; import distance from 'gps-distance'; -import readStations from 'db-hafas-stations'; import {defaultProfile} from './lib/default-profile.js'; import {validateProfile} from './lib/validate-profile.js'; @@ -27,7 +26,8 @@ const validateLocation = (loc, name = 'location') => { } }; -const loadEnrichedStationData = (profile) => new Promise((resolve, reject) => { +const loadEnrichedStationData = (profile) => new Promise(async (resolve, reject) => { + const { default: readStations} = await import('db-hafas-stations'); const items = {}; readStations.full() .on('data', (station) => { From 53c20ab0e8bbffa05baca4889694af2889b55d9b Mon Sep 17 00:00:00 2001 From: McToel Date: Sun, 16 Feb 2025 16:39:00 +0100 Subject: [PATCH 6/9] trying to handle undefined envs --- lib/default-profile.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/default-profile.js b/lib/default-profile.js index 5c2e3e49..a8cf3b6c 100644 --- a/lib/default-profile.js +++ b/lib/default-profile.js @@ -37,7 +37,7 @@ import {formatTravellers} from '../format/travellers.js'; import {formatLoyaltyCard} from '../format/loyalty-cards.js'; import {formatTransfers} from '../format/transfers.js'; -const DEBUG = (/(^|,)hafas-client(,|$)/).test(process.env.DEBUG || ''); +const DEBUG = (/(^|,)hafas-client(,|$)/).test(process?.env?.DEBUG ?? ''); const logRequest = DEBUG ? (_, req, reqId) => console.error(String(req.body)) : () => { }; From c908705167e216b9642c5a7bbf6d01a216b60e76 Mon Sep 17 00:00:00 2001 From: McToel Date: Sun, 16 Feb 2025 16:50:06 +0100 Subject: [PATCH 7/9] Less optimistic variable handling --- lib/default-profile.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/default-profile.js b/lib/default-profile.js index a8cf3b6c..d4049806 100644 --- a/lib/default-profile.js +++ b/lib/default-profile.js @@ -37,7 +37,7 @@ import {formatTravellers} from '../format/travellers.js'; import {formatLoyaltyCard} from '../format/loyalty-cards.js'; import {formatTransfers} from '../format/transfers.js'; -const DEBUG = (/(^|,)hafas-client(,|$)/).test(process?.env?.DEBUG ?? ''); +const DEBUG = (/(^|,)hafas-client(,|$)/).test((typeof process !== 'undefined') ? (process.env.DEBUG || '') : ''); const logRequest = DEBUG ? (_, req, reqId) => console.error(String(req.body)) : () => { }; From f943e50cbda13955b7ba62e18a5eb3544a127a65 Mon Sep 17 00:00:00 2001 From: McToel Date: Mon, 17 Feb 2025 15:46:01 +0100 Subject: [PATCH 8/9] cleanup --- lib/request.js | 50 -------------------------------------------------- 1 file changed, 50 deletions(-) diff --git a/lib/request.js b/lib/request.js index e9a02b73..bb5441f2 100644 --- a/lib/request.js +++ b/lib/request.js @@ -3,56 +3,6 @@ import {Request, fetch} from 'cross-fetch'; import {parse as parseContentType} from 'content-type'; import {HafasError} from './errors.js'; -// const proxyAddress = process.env.HTTPS_PROXY || process.env.HTTP_PROXY || null; -// const localAddresses = process.env.LOCAL_ADDRESS || null; - -// if (proxyAddress && localAddresses) { -// console.error('Both env vars HTTPS_PROXY/HTTP_PROXY and LOCAL_ADDRESS are not supported.'); -// process.exit(1); -// } - -// const plainAgent = new HttpsAgent({ -// keepAlive: true, -// }); -// let getAgent = () => plainAgent; - -// if (proxyAddress) { -// const agent = new ProxyAgent(proxyAddress, { -// keepAlive: true, -// keepAliveMsecs: 10 * 1000, // 10s -// }); -// getAgent = () => agent; -// } else if (localAddresses) { -// const agents = process.env.LOCAL_ADDRESS.split(',') -// .map((addr) => { -// const family = isIP(addr); -// if (family === 0) { -// throw new Error('invalid local address:' + addr); -// } -// return new HttpsAgent({ -// localAddress: addr, family, -// keepAlive: true, -// }); -// }); -// const pool = roundRobin(agents); -// getAgent = () => pool.get(); -// } - -// const id = randomBytes(3) -// .toString('hex'); -// const randomizeUserAgent = (userAgent) => { -// let ua = userAgent; -// for ( -// let i = Math.round(5 + Math.random() * 5); -// i < ua.length; -// i += Math.round(5 + Math.random() * 5) -// ) { -// ua = ua.slice(0, i) + id + ua.slice(i); -// i += id.length; -// } -// return ua; -// }; - const randomBytesHex = (nBytes = 8) => { const array = new Uint8Array(nBytes); crypto.getRandomValues(array); From 932a4aa0dbf1e3d52799ce1d90b76c4157646332 Mon Sep 17 00:00:00 2001 From: McToel Date: Mon, 17 Feb 2025 16:05:15 +0100 Subject: [PATCH 9/9] Small browser docs addition --- readme.md | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/readme.md b/readme.md index a50443bb..fc56cd87 100644 --- a/readme.md +++ b/readme.md @@ -76,6 +76,12 @@ There are [community-maintained TypeScript typings available as `@types/hafas-cl > [!IMPORTANT] > Depending on your use case, it is very important that you employ caching, either with a simple [HTTP proxy cache](https://github.com/traines-source/time-space-train-planner/blob/master/deployments/nginx-cache.conf) in front of the REST API or by using [cached-hafas-client](https://github.com/public-transport/cached-hafas-client) (where, of course, you can just drop in a `db-vendo-client` instead of a `hafas-client` instance). Also see [db-rest](https://github.com/derhuerst/db-rest), which does this and some more plumbing. +## Browser usage + +`db-vendo-client` is mostly browser compatible, however none of the endpoints enables CORS, so it is impossible to use `db-vendo-client` in normal browser environments. It was tested with vite + capacitorjs and should also work with cordova or react native and similar projects. + +**Limitations:** Does not work with `enrichStations` option enabled. + ## Related Projects - [hafas-client](https://github.com/public-transport/hafas-client/) – including further related projects