mirror of
https://github.com/public-transport/db-vendo-client.git
synced 2025-06-19 10:42:33 +03:00
Merge 932a4aa0db
into 6d1d0c626f
This commit is contained in:
commit
1f49a52184
20 changed files with 40 additions and 126 deletions
4
index.js
4
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) => {
|
||||
|
|
|
@ -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))
|
||||
: () => { };
|
||||
|
|
|
@ -1,61 +1,14 @@
|
|||
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;
|
||||
|
||||
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);
|
||||
return Array.from(array)
|
||||
.map((byte) => byte.toString(16).padStart(2, '0'))
|
||||
.join('');
|
||||
};
|
||||
|
||||
const checkIfResponseIsOk = (_) => {
|
||||
|
@ -101,8 +54,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 +64,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 +72,14 @@ 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 reqId = randomBytesHex(3);
|
||||
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 +99,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);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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()
|
||||
|
|
|
@ -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';
|
||||
|
|
|
@ -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';
|
||||
|
||||
|
|
|
@ -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';
|
||||
|
|
|
@ -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",
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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});
|
||||
|
|
|
@ -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});
|
||||
|
|
|
@ -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});
|
||||
|
|
|
@ -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});
|
||||
|
|
|
@ -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});
|
||||
|
|
|
@ -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});
|
||||
|
|
|
@ -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: false});
|
||||
|
|
|
@ -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});
|
||||
|
|
|
@ -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});
|
||||
|
|
|
@ -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});
|
||||
|
|
|
@ -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,
|
||||
|
|
Loading…
Add table
Reference in a new issue