Skip to content

Commit

Permalink
with $RANDOM_LOCAL_ADDRESSES_RANGE, use localaddress-agent
Browse files Browse the repository at this point in the history
This allows sending requests with a large number of IPv6 addresses.
  • Loading branch information
derhuerst committed Nov 16, 2024
1 parent 2d0752a commit ec5fe78
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 1 deletion.
21 changes: 20 additions & 1 deletion api.js
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand All @@ -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)

Expand Down
3 changes: 3 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down

0 comments on commit ec5fe78

Please sign in to comment.