kepler-companion
Advanced tools
Comparing version 1.1.2 to 1.1.3
export declare type KeplerCompanionConfiguration = { | ||
host?: string; | ||
port?: number; | ||
ssl?: boolean; | ||
telemetryPath?: string; | ||
enabled?: boolean; | ||
host: string; | ||
port: number; | ||
ssl: boolean; | ||
enabled: boolean; | ||
timeout: number; | ||
}; | ||
@@ -17,10 +17,13 @@ export declare type TelemetryOpts = { | ||
export default class KeplerCompanion { | ||
private sdk; | ||
config: KeplerCompanionConfiguration; | ||
constructor(config?: {}); | ||
constructor(config?: Partial<KeplerCompanionConfiguration>); | ||
get mode(): 'node' | 'browser'; | ||
private getUserId; | ||
turnOff(): void; | ||
add(opts: TelemetryOpts): Promise<unknown> | void; | ||
private _add; | ||
/** | ||
* Add telemetry. | ||
* Never returns a rejected promise | ||
*/ | ||
add(opts: TelemetryOpts): Promise<void>; | ||
private sendHttpRequest; | ||
} |
@@ -7,3 +7,2 @@ "use strict"; | ||
const getmac_1 = __importDefault(require("getmac")); | ||
const kuzzle_sdk_1 = require("kuzzle-sdk"); | ||
const crypto = typeof window === 'undefined' ? require('crypto') : window.crypto; | ||
@@ -30,5 +29,5 @@ /** | ||
enabled: true, | ||
timeout: 500, | ||
}; | ||
this.config = { ...this.config, ...config }; | ||
this.sdk = new kuzzle_sdk_1.Kuzzle(new kuzzle_sdk_1.Http(this.config.host, { port: this.config.port, ssl: this.config.ssl })); | ||
} | ||
@@ -57,3 +56,7 @@ get mode() { | ||
} | ||
add(opts) { | ||
/** | ||
* Add telemetry. | ||
* Never returns a rejected promise | ||
*/ | ||
async add(opts) { | ||
if (!this.config.enabled) { | ||
@@ -66,24 +69,57 @@ return; | ||
} | ||
this._add(opts).catch(() => { }); | ||
} | ||
async _add(opts) { | ||
const user = this.getUserId(opts.product); | ||
try { | ||
await this.sdk.connect(); | ||
await this.sdk.query({ | ||
controller: 'telemetry', | ||
action: 'register', | ||
a: opts.action, | ||
p: opts.product, | ||
v: opts.version, | ||
u: user, | ||
body: opts.tags || {}, | ||
await this.sendHttpRequest({ | ||
method: 'POST', | ||
path: '/_/telemetry/register', | ||
params: { | ||
a: opts.action, | ||
p: opts.product, | ||
v: opts.version, | ||
u: user, | ||
}, | ||
body: { | ||
t: opts.tags, | ||
}, | ||
}); | ||
} | ||
finally { | ||
this.sdk.disconnect(); | ||
catch (error) { | ||
// Do nothing | ||
} | ||
} | ||
sendHttpRequest({ method, path, params, body }) { | ||
const queryArgs = new URLSearchParams(params); | ||
const url = `http${this.config.ssl ? 's' : ''}://${this.config.host}:${this.config.port}${path}?${queryArgs}`; | ||
if (typeof XMLHttpRequest === 'undefined') { | ||
// NodeJS implementation, using http.request: | ||
// eslint-disable-next-line @typescript-eslint/no-var-requires | ||
const httpClient = require('min-req-promise'); | ||
return httpClient.request(url, method, { | ||
body: JSON.stringify(body), | ||
timeout: this.config.timeout | ||
}); | ||
} | ||
// Browser implementation, using XMLHttpRequest: | ||
return new Promise((resolve, reject) => { | ||
const xhr = new XMLHttpRequest(); | ||
xhr.timeout = this.config.timeout; | ||
xhr.onreadystatechange = () => { | ||
if (xhr.readyState === 4 && xhr.status === 0) { | ||
reject(new Error('Cannot connect to host. Is the host online?')); | ||
} | ||
}; | ||
xhr.open(method, url); | ||
xhr.onload = () => { | ||
try { | ||
resolve(xhr.responseText); | ||
} | ||
catch (err) { | ||
reject(err); | ||
} | ||
}; | ||
xhr.send(JSON.stringify(body)); | ||
}); | ||
} | ||
} | ||
exports.default = KeplerCompanion; | ||
//# sourceMappingURL=KeplerCompanion.js.map |
{ | ||
"name": "kepler-companion", | ||
"version": "1.1.2", | ||
"version": "1.1.3", | ||
"description": "Kepler analytics toolkit for client side", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
6612
157