diff --git a/.gitignore b/.gitignore index e4ed3d2b..21bd24e5 100644 --- a/.gitignore +++ b/.gitignore @@ -6,3 +6,5 @@ node_modules npm-debug.log package-lock.json + +/id.json diff --git a/lib/generate-install-id.js b/lib/generate-install-id.js new file mode 100755 index 00000000..14235608 --- /dev/null +++ b/lib/generate-install-id.js @@ -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') diff --git a/lib/request.js b/lib/request.js index 00396906..2567f082 100644 --- a/lib/request.js +++ b/lib/request.js @@ -1,5 +1,6 @@ 'use strict' +const {join} = require('path') const createHash = require('create-hash') let captureStackTrace = () => {} if (process.env.NODE_DEBUG === 'hafas-client') { @@ -9,6 +10,15 @@ const {stringify} = require('query-string') const Promise = require('pinkie-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 request = (profile, userAgent, opt, data) => { @@ -24,7 +34,7 @@ const request = (profile, userAgent, opt, data) => { 'Content-Type': 'application/json', 'Accept-Encoding': 'gzip, deflate', 'Accept': 'application/json', - 'user-agent': userAgent + 'user-agent': id + ' ' + userAgent }, query: {} }) diff --git a/package.json b/package.json index 49e0c460..83ec9439 100644 --- a/package.json +++ b/package.json @@ -62,6 +62,7 @@ }, "scripts": { "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" } }