From ec5fe78b5ce7e06055ba06fd38acce4aae7f3781 Mon Sep 17 00:00:00 2001 From: Jannis R Date: Sat, 16 Nov 2024 01:29:00 +0100 Subject: [PATCH] with $RANDOM_LOCAL_ADDRESSES_RANGE, use localaddress-agent This allows sending requests with a large number of IPv6 addresses. --- api.js | 21 ++++++++++++++++++++- package.json | 3 +++ 2 files changed, 23 insertions(+), 1 deletion(-) diff --git a/api.js b/api.js index 1170656..66fb83e 100644 --- a/api.js +++ b/api.js @@ -7,7 +7,10 @@ import {dirname, join as pathJoin} from 'node:path' import {fileURLToPath} from 'node:url' import _cliNative from 'cli-native' const {to: parse} = _cliNative -import {createVbbHafas as createHafas} from 'vbb-hafas' +import { + createVbbHafas as createHafas, + defaults as vbbHafasDefaults, +} from 'vbb-hafas' import createHealthCheck from 'hafas-client-health-check' import Redis from 'ioredis' import {createCachedHafasClient as withCache} from 'cached-hafas-client' @@ -28,12 +31,28 @@ const docsRoot = pathJoin(__dirname, 'docs') const berlinFriedrichstr = '900100001' +const customVbbProfile = { + ...vbbHafasDefaults.profile, +} + +// todo: DRY env var check with localaddress-agent/random-from-env.js +// Currently, this is impossible: localaddress-agent is an optional dependencies, so we rely on it to check the env var. +if (process.env.RANDOM_LOCAL_ADDRESSES_RANGE) { + const {randomLocalAddressAgent} = await import('localaddress-agent/random-from-env.js') + + customVbbProfile.transformReq = (_, req) => { + req.agent = randomLocalAddressAgent + return req + } +} + let hafas = createHafas( // seems like `vbb-rest` is being redirected // pkg.name, // seems like these are being blocked // require('crypto').randomBytes(10).toString('hex'), 'App/4.5.1 (iPhone; iOS 15.2; Scale/3.00)', + {profile: customVbbProfile}, ) let healthCheck = createHealthCheck(hafas, berlinFriedrichstr) diff --git a/package.json b/package.json index 700f6bf..70de151 100644 --- a/package.json +++ b/package.json @@ -44,6 +44,9 @@ "vbb-stations": "^8.2.0", "vbb-stations-autocomplete": "^4.4.0" }, + "optionalDependencies": { + "localaddress-agent": "^2.1.1" + }, "scripts": { "build": "REDIS_URL='' ./build/index.js", "start": "node index.js",