db-vendo-client/lib/request.js

102 lines
2.6 KiB
JavaScript
Raw Normal View History

2017-11-11 22:49:04 +01:00
'use strict'
const createHash = require('create-hash')
2018-01-23 01:24:37 +01:00
let captureStackTrace = () => {}
if (process.env.NODE_DEBUG === 'hafas-client') {
2018-01-23 01:24:37 +01:00
captureStackTrace = 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})
const md5 = input => createHash('md5').update(input).digest()
2017-11-11 22:49:04 +01:00
const request = (profile, data) => {
const body = profile.transformReqBody({lang: 'en', svcReqL: [data]})
const req = profile.transformReq({
method: 'post',
// todo: CORS? referrer policy?
body: JSON.stringify(body),
headers: {
'Content-Type': 'application/json',
'Accept-Encoding': 'gzip, deflate',
2018-03-19 22:34:08 +01:00
'user-agent': 'https://github.com/public-transport/hafas-client'
2017-11-11 22:49:04 +01:00
},
query: {}
2017-11-11 22:49:04 +01:00
})
if (profile.addChecksum || profile.addMicMac) {
if (!Buffer.isBuffer(profile.salt)) {
throw new Error('profile.salt must be a Buffer.')
}
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')
const micAsHex = Buffer.from(mic.toString('hex'), 'utf8')
const mac = md5(Buffer.concat([micAsHex, profile.salt]))
req.query.mac = mac.toString('hex')
}
}
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-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 || {}
2017-11-12 01:23:34 +01:00
if (Array.isArray(c.remL)) {
d.remarks = c.remL.map(rem => profile.parseRemark(profile, rem))
}
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)) {
const parse = profile.parseLine(profile, d.operators)
d.lines = c.prodL.map(parse)
}
2018-01-26 16:25:13 +01:00
if (Array.isArray(c.locL)) {
const parse = loc => profile.parseLocation(profile, loc, d.lines)
d.locations = c.locL.map(parse)
}
2017-11-11 22:49:04 +01:00
return d
})
}
module.exports = request