2017-11-11 22:49:04 +01:00
|
|
|
'use strict'
|
|
|
|
|
2018-09-03 15:31:20 +02:00
|
|
|
const DEBUG = process.env.NODE_DEBUG === 'hafas-client'
|
|
|
|
|
2018-07-26 19:03:28 +02:00
|
|
|
const {join} = require('path')
|
2018-05-21 12:15:40 +02:00
|
|
|
const createHash = require('create-hash')
|
2018-09-03 15:31:20 +02:00
|
|
|
const captureStackTrace = DEBUG ? require('capture-stack-trace') : () => {}
|
2018-01-23 01:30:20 +01:00
|
|
|
const {stringify} = require('query-string')
|
2017-11-11 22:49:04 +01:00
|
|
|
const Promise = require('pinkie-promise')
|
|
|
|
const {fetch} = require('fetch-ponyfill')({Promise})
|
|
|
|
|
2018-07-26 19:03:28 +02:00
|
|
|
let id
|
|
|
|
try {
|
|
|
|
id = require('../id.json')
|
|
|
|
} catch (err) {
|
|
|
|
const p = join(__dirname, '..', 'id.json')
|
|
|
|
console.error(`Failed to load the install-unique ID from ${p}.`)
|
|
|
|
process.exit(1)
|
|
|
|
}
|
|
|
|
|
2018-08-08 18:40:35 +02:00
|
|
|
const randomizeUserAgent = (userAgent) => {
|
|
|
|
const i = Math.round(Math.random() * userAgent.length)
|
|
|
|
return userAgent.slice(0, i) + id + userAgent.slice(i)
|
|
|
|
}
|
|
|
|
|
2018-05-21 12:15:40 +02:00
|
|
|
const md5 = input => createHash('md5').update(input).digest()
|
2018-01-15 00:45:05 +01:00
|
|
|
|
2018-07-19 21:50:20 +02:00
|
|
|
const request = (profile, userAgent, opt, data) => {
|
2018-07-09 12:40:38 +02:00
|
|
|
const body = profile.transformReqBody({
|
2018-09-03 15:29:06 +02:00
|
|
|
lang: opt.language || 'en', // todo: is it `eng` actually?
|
2018-07-09 12:40:38 +02:00
|
|
|
svcReqL: [data]
|
|
|
|
})
|
2018-09-03 15:31:20 +02:00
|
|
|
if (DEBUG) console.error(JSON.stringify(body))
|
|
|
|
|
2017-11-11 22:49:04 +01:00
|
|
|
const req = profile.transformReq({
|
|
|
|
method: 'post',
|
|
|
|
// todo: CORS? referrer policy?
|
|
|
|
body: JSON.stringify(body),
|
|
|
|
headers: {
|
|
|
|
'Content-Type': 'application/json',
|
|
|
|
'Accept-Encoding': 'gzip, deflate',
|
2018-06-07 12:04:21 +02:00
|
|
|
'Accept': 'application/json',
|
2018-08-08 18:40:35 +02:00
|
|
|
'user-agent': randomizeUserAgent(userAgent)
|
2017-11-11 22:49:04 +01:00
|
|
|
},
|
2018-01-15 00:45:05 +01:00
|
|
|
query: {}
|
2017-11-11 22:49:04 +01:00
|
|
|
})
|
|
|
|
|
2018-01-15 00:45:40 +01:00
|
|
|
if (profile.addChecksum || profile.addMicMac) {
|
2018-01-15 00:45:05 +01:00
|
|
|
if (!Buffer.isBuffer(profile.salt)) {
|
|
|
|
throw new Error('profile.salt must be a Buffer.')
|
|
|
|
}
|
2018-01-15 00:45:40 +01:00
|
|
|
if (profile.addChecksum) {
|
|
|
|
const checksum = md5(Buffer.concat([
|
|
|
|
Buffer.from(req.body, 'utf8'),
|
|
|
|
profile.salt
|
|
|
|
]))
|
|
|
|
req.query.checksum = checksum.toString('hex')
|
|
|
|
}
|
|
|
|
if (profile.addMicMac) {
|
|
|
|
const mic = md5(Buffer.from(req.body, 'utf8'))
|
|
|
|
req.query.mic = mic.toString('hex')
|
|
|
|
|
2018-01-19 15:47:41 +01:00
|
|
|
const micAsHex = Buffer.from(mic.toString('hex'), 'utf8')
|
|
|
|
const mac = md5(Buffer.concat([micAsHex, profile.salt]))
|
2018-01-15 00:45:40 +01:00
|
|
|
req.query.mac = mac.toString('hex')
|
|
|
|
}
|
2018-01-15 00:45:05 +01:00
|
|
|
}
|
|
|
|
|
2018-03-02 23:57:29 +01:00
|
|
|
const url = profile.endpoint + '?' + stringify(req.query)
|
2017-11-11 22:49:04 +01:00
|
|
|
|
2018-01-23 01:24:37 +01:00
|
|
|
// Async stack traces are not supported everywhere yet, so we create our own.
|
|
|
|
const err = new Error()
|
|
|
|
err.isHafasError = true
|
|
|
|
err.request = body
|
2018-03-02 23:57:29 +01:00
|
|
|
err.url = url
|
2018-01-23 01:24:37 +01:00
|
|
|
captureStackTrace(err)
|
|
|
|
|
2017-11-11 22:49:04 +01:00
|
|
|
return fetch(url, req)
|
|
|
|
.then((res) => {
|
2018-01-23 01:24:37 +01:00
|
|
|
err.statusCode = res.status
|
2017-11-11 22:49:04 +01:00
|
|
|
if (!res.ok) {
|
2018-01-23 01:24:37 +01:00
|
|
|
err.message = res.statusText
|
|
|
|
throw err
|
2017-11-11 22:49:04 +01:00
|
|
|
}
|
|
|
|
return res.json()
|
|
|
|
})
|
|
|
|
.then((b) => {
|
2018-09-03 15:31:20 +02:00
|
|
|
if (DEBUG) console.error(JSON.stringify(b))
|
|
|
|
|
2018-01-23 01:24:37 +01:00
|
|
|
if (b.err) {
|
|
|
|
err.message = b.err
|
|
|
|
throw err
|
|
|
|
}
|
|
|
|
if (!b.svcResL || !b.svcResL[0]) {
|
|
|
|
err.message = 'invalid response'
|
|
|
|
throw err
|
|
|
|
}
|
2017-11-11 22:49:04 +01:00
|
|
|
if (b.svcResL[0].err !== 'OK') {
|
2018-01-28 14:12:52 +01:00
|
|
|
err.message = b.svcResL[0].errTxt || b.svcResL[0].err
|
2018-01-23 01:24:37 +01:00
|
|
|
throw err
|
2017-11-11 22:49:04 +01:00
|
|
|
}
|
|
|
|
const d = b.svcResL[0].res
|
|
|
|
const c = d.common || {}
|
|
|
|
|
2018-06-28 13:45:56 +02:00
|
|
|
d.hints = []
|
|
|
|
if (opt.remarks && Array.isArray(c.remL)) {
|
2018-06-30 18:04:44 +02:00
|
|
|
const icons = opt.remarks && c.icoL || []
|
|
|
|
d.hints = c.remL.map(hint => profile.parseHint(profile, hint, icons))
|
2017-11-12 01:23:34 +01:00
|
|
|
}
|
2018-06-07 18:46:24 +02:00
|
|
|
d.warnings = []
|
2018-06-28 13:45:56 +02:00
|
|
|
if (opt.remarks && Array.isArray(c.himL)) {
|
2018-06-30 18:04:44 +02:00
|
|
|
const icons = opt.remarks && c.icoL || []
|
|
|
|
d.warnings = c.himL.map(w => profile.parseWarning(profile, w, icons))
|
2017-11-12 01:23:34 +01:00
|
|
|
}
|
|
|
|
if (Array.isArray(c.opL)) {
|
|
|
|
d.operators = c.opL.map(op => profile.parseOperator(profile, op))
|
|
|
|
}
|
2018-01-05 18:46:19 +01:00
|
|
|
if (Array.isArray(c.prodL)) {
|
2018-06-13 19:59:44 +02:00
|
|
|
const parse = profile.parseLine(profile, opt, {
|
2018-06-13 18:59:56 +02:00
|
|
|
operators: d.operators
|
|
|
|
})
|
2018-01-05 18:46:19 +01:00
|
|
|
d.lines = c.prodL.map(parse)
|
|
|
|
}
|
2018-01-26 16:25:13 +01:00
|
|
|
if (Array.isArray(c.locL)) {
|
2018-07-10 23:32:34 +02:00
|
|
|
const data = {lines: d.lines}
|
|
|
|
const parse = loc => profile.parseLocation(profile, opt, data, loc)
|
|
|
|
|
2018-01-26 16:25:13 +01:00
|
|
|
d.locations = c.locL.map(parse)
|
2018-07-10 23:32:34 +02:00
|
|
|
for (let i = 0; i < d.locations.length; i++) {
|
|
|
|
const raw = c.locL[i]
|
|
|
|
const loc = d.locations[i]
|
|
|
|
if ('number' === typeof raw.mMastLocX) {
|
|
|
|
loc.station = Object.assign({}, d.locations[raw.mMastLocX])
|
|
|
|
loc.station.type = 'station'
|
|
|
|
} else if (raw.isMainMast) loc.type = 'station'
|
|
|
|
}
|
2018-01-26 16:25:13 +01:00
|
|
|
}
|
2017-11-11 22:49:04 +01:00
|
|
|
return d
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = request
|