generate & send install-unique IDs

This commit is contained in:
Jannis R 2018-07-26 19:03:28 +02:00
parent 8653e43b2b
commit 96c1df5c3a
No known key found for this signature in database
GPG key ID: 0FE83946296A88A5
4 changed files with 21 additions and 2 deletions

2
.gitignore vendored
View file

@ -6,3 +6,5 @@ node_modules
npm-debug.log npm-debug.log
package-lock.json package-lock.json
/id.json

6
lib/generate-install-id.js Executable file
View file

@ -0,0 +1,6 @@
#!/usr/bin/env node
const {randomBytes} = require('crypto')
const id = randomBytes(6).toString('hex')
process.stdout.write(JSON.stringify(id) + '\n')

View file

@ -1,5 +1,6 @@
'use strict' 'use strict'
const {join} = require('path')
const createHash = require('create-hash') const createHash = require('create-hash')
let captureStackTrace = () => {} let captureStackTrace = () => {}
if (process.env.NODE_DEBUG === 'hafas-client') { if (process.env.NODE_DEBUG === 'hafas-client') {
@ -9,6 +10,15 @@ const {stringify} = require('query-string')
const Promise = require('pinkie-promise') const Promise = require('pinkie-promise')
const {fetch} = require('fetch-ponyfill')({Promise}) const {fetch} = require('fetch-ponyfill')({Promise})
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)
}
const md5 = input => createHash('md5').update(input).digest() const md5 = input => createHash('md5').update(input).digest()
const request = (profile, userAgent, opt, data) => { const request = (profile, userAgent, opt, data) => {
@ -24,7 +34,7 @@ const request = (profile, userAgent, opt, data) => {
'Content-Type': 'application/json', 'Content-Type': 'application/json',
'Accept-Encoding': 'gzip, deflate', 'Accept-Encoding': 'gzip, deflate',
'Accept': 'application/json', 'Accept': 'application/json',
'user-agent': userAgent 'user-agent': id + ' ' + userAgent
}, },
query: {} query: {}
}) })

View file

@ -62,6 +62,7 @@
}, },
"scripts": { "scripts": {
"test": "env NODE_ENV=dev NODE_DEBUG=hafas-client node test/index.js", "test": "env NODE_ENV=dev NODE_DEBUG=hafas-client node test/index.js",
"prepublishOnly": "npm test | tap-spec" "prepublishOnly": "npm test | tap-spec",
"install": "lib/generate-install-id.js >id.json"
} }
} }