mirror of
https://github.com/public-transport/db-vendo-client.git
synced 2025-02-22 22:59:35 +02:00
add profile.randomizeUserAgent flag 📝
This commit is contained in:
parent
e0cdd55908
commit
492fdeb2ef
4 changed files with 29 additions and 2 deletions
|
@ -60,6 +60,30 @@ const retryingDbProfile = withRetrying(dbProfile, {
|
|||
const client = createClient(retryingDbProfile, 'my-awesome-program')
|
||||
```
|
||||
|
||||
## User agent randomization
|
||||
|
||||
By default, `hafas-client` randomizes the client name that you pass into `createClient`, and sends it as [`User-Agent`](https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/User-Agent) in a randomized form:
|
||||
|
||||
```js
|
||||
const createClient('hafas-client')
|
||||
// …
|
||||
const client = createClient(someProfile, 'my-awesome-program')
|
||||
|
||||
await client.journeys(/* … */)
|
||||
// User-Agent: my-awee70429some-pre70429ogram
|
||||
await client.journeys(/* … */)
|
||||
// User-Agent: my-awesom9bb8e2e-prog9bb8e2ram
|
||||
```
|
||||
|
||||
You can turn this off by setting `profile.randomizeUserAgent` to `false`:
|
||||
|
||||
```js
|
||||
const client = createClient({
|
||||
...someProfile,
|
||||
randomizeUserAgent: false,
|
||||
}, 'my-awesome-program')
|
||||
```
|
||||
|
||||
## Logging requests
|
||||
|
||||
You can use `profile.logRequest` and `profile.logResponse` to process the raw [Fetch](https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API) [`Request`](https://developer.mozilla.org/en-US/docs/Web/API/Request) and [`Response`](https://developer.mozilla.org/en-US/docs/Web/API/Response), respectively.
|
||||
|
|
|
@ -65,6 +65,7 @@ const defaultProfile = {
|
|||
salt: null,
|
||||
addChecksum: false,
|
||||
addMicMac: false,
|
||||
randomizeUserAgent: true,
|
||||
logRequest,
|
||||
logResponse,
|
||||
|
||||
|
|
|
@ -66,7 +66,6 @@ const randomizeUserAgent = (userAgent) => {
|
|||
|
||||
const md5 = input => createHash('md5').update(input).digest()
|
||||
|
||||
// todo [breaking]: remove userAgent parameter
|
||||
const request = (ctx, userAgent, reqData) => {
|
||||
const {profile, opt} = ctx
|
||||
|
||||
|
@ -92,7 +91,9 @@ const request = (ctx, userAgent, reqData) => {
|
|||
'Content-Type': 'application/json',
|
||||
'Accept-Encoding': 'gzip, br, deflate',
|
||||
'Accept': 'application/json',
|
||||
'user-agent': randomizeUserAgent(userAgent),
|
||||
'user-agent': profile.randomizeUserAgent
|
||||
? randomizeUserAgent(userAgent)
|
||||
: userAgent,
|
||||
'connection': 'keep-alive', // prevent excessive re-connecting
|
||||
},
|
||||
redirect: 'follow',
|
||||
|
|
|
@ -43,6 +43,7 @@ if (process.env.VCR_MODE && !process.env.VCR_OFF) {
|
|||
const replayer = require('replayer')
|
||||
replayer.configure({
|
||||
headerWhitelist: [
|
||||
// excludes User-Agent & Connection
|
||||
'Content-Type', 'Accept-Encoding', 'Accept',
|
||||
],
|
||||
includeHeaderValues: true,
|
||||
|
|
Loading…
Add table
Reference in a new issue