From 5d9d738152e60855388e22e9d1b88b5ca21d4c37 Mon Sep 17 00:00:00 2001 From: Jannis R Date: Thu, 19 Jul 2018 21:50:20 +0200 Subject: [PATCH] mandatory User-Agent param :boom: --- index.js | 20 ++++++++++++-------- lib/request.js | 4 ++-- throttle.js | 4 ++-- 3 files changed, 16 insertions(+), 12 deletions(-) diff --git a/index.js b/index.js index 62aa7b6a..671290a5 100644 --- a/index.js +++ b/index.js @@ -12,7 +12,7 @@ const _request = require('./lib/request') const isNonEmptyString = str => 'string' === typeof str && str.length > 0 -const createClient = (profile, request = _request) => { +const createClient = (profile, userAgent, request = _request) => { profile = Object.assign({}, defaultProfile, profile) if (!profile.parseProducts) { profile.parseProducts = createParseBitmask(profile) @@ -22,6 +22,10 @@ const createClient = (profile, request = _request) => { } validateProfile(profile) + if ('string' !== typeof userAgent) { + throw new Error('userAgent must be a string'); + } + const _stationBoard = (station, type, parser, opt = {}) => { if (isObj(station)) station = profile.formatStation(station.id) else if ('string' === typeof station) station = profile.formatStation(station) @@ -45,7 +49,7 @@ const createClient = (profile, request = _request) => { const products = profile.formatProductsFilter(opt.products || {}) const dir = opt.direction ? profile.formatStation(opt.direction) : null - return request(profile, opt, { + return request(profile, userAgent, opt, { meth: 'StationBoard', req: { type, @@ -181,7 +185,7 @@ const createClient = (profile, request = _request) => { } if (profile.journeysNumF) query.numF = opt.results - return request(profile, opt, { + return request(profile, userAgent, opt, { cfg: {polyEnc: 'GPA'}, meth: 'TripSearch', req: profile.transformJourneysQuery(query, opt) @@ -234,7 +238,7 @@ const createClient = (profile, request = _request) => { }, opt) const f = profile.formatLocationFilter(opt.stations, opt.addresses, opt.poi) - return request(profile, opt, { + return request(profile, userAgent, opt, { cfg: {polyEnc: 'GPA'}, meth: 'LocMatch', req: {input: { @@ -261,7 +265,7 @@ const createClient = (profile, request = _request) => { opt = Object.assign({ stationLines: false // parse & expose lines of the station? }, opt) - return request(profile, opt, { + return request(profile, userAgent, opt, { meth: 'LocDetails', req: { locL: [station] @@ -295,7 +299,7 @@ const createClient = (profile, request = _request) => { stationLines: false // parse & expose lines of the station? }, opt) - return request(profile, opt, { + return request(profile, userAgent, opt, { cfg: {polyEnc: 'GPA'}, meth: 'LocGeoPos', req: { @@ -334,7 +338,7 @@ const createClient = (profile, request = _request) => { opt.when = new Date(opt.when || Date.now()) if (Number.isNaN(+opt.when)) throw new Error('opt.when is invalid') - return request(profile, opt, { + return request(profile, userAgent, opt, { cfg: {polyEnc: 'GPA'}, meth: 'JourneyDetails', req: { @@ -384,7 +388,7 @@ const createClient = (profile, request = _request) => { if (Number.isNaN(+opt.when)) throw new Error('opt.when is invalid') const durationPerStep = opt.duration / Math.max(opt.frames, 1) * 1000 - return request(profile, opt, { + return request(profile, userAgent, opt, { meth: 'JourneyGeoPos', req: { maxJny: opt.results, diff --git a/lib/request.js b/lib/request.js index 709fa483..00396906 100644 --- a/lib/request.js +++ b/lib/request.js @@ -11,7 +11,7 @@ const {fetch} = require('fetch-ponyfill')({Promise}) const md5 = input => createHash('md5').update(input).digest() -const request = (profile, opt, data) => { +const request = (profile, userAgent, opt, data) => { const body = profile.transformReqBody({ lang: opt.language || 'en', svcReqL: [data] @@ -24,7 +24,7 @@ const request = (profile, opt, data) => { 'Content-Type': 'application/json', 'Accept-Encoding': 'gzip, deflate', 'Accept': 'application/json', - 'user-agent': 'https://github.com/public-transport/hafas-client' + 'user-agent': userAgent }, query: {} }) diff --git a/throttle.js b/throttle.js index 99fb3cc0..995e855e 100644 --- a/throttle.js +++ b/throttle.js @@ -5,9 +5,9 @@ const throttle = require('p-throttle') const request = require('./lib/request') const createClient = require('.') -const createThrottledClient = (profile, limit = 5, interval = 1000) => { +const createThrottledClient = (profile, userAgent, limit = 5, interval = 1000) => { const throttledRequest = throttle(request, limit, interval) - return createClient(profile, throttledRequest) + return createClient(profile, userAgent, throttledRequest) } module.exports = createThrottledClient