UNPKG

azure-kusto-data

Version:
87 lines 4.11 kB
// Copyright (c) Microsoft Corporation. // Licensed under the MIT License. import { isNodeLike } from "@azure/core-util"; import { userInfo } from "os"; import { SDK_VERSION } from "./version.js"; // This regex allows all printable ascii, except spaces and chars we use in the format const ReplaceRegex = /[^\w.\-()]/g; const None = "[none]"; export class ClientDetails { constructor(applicationNameForTracing, userNameForTracing) { this.applicationNameForTracing = applicationNameForTracing; this.userNameForTracing = userNameForTracing; if (this.applicationNameForTracing === null) { this.applicationNameForTracing = ClientDetails.defaultApplication(); } if (this.userNameForTracing === null) { this.userNameForTracing = ClientDetails.defaultUser(); } this.versionForTracing = ClientDetails.defaultVersion(); } static defaultApplication() { var _a, _b; if (isNodeLike) { return ((_a = process === null || process === void 0 ? void 0 : process.env) === null || _a === void 0 ? void 0 : _a.npm_package_name) || (process === null || process === void 0 ? void 0 : process.argv[1]) || None; } else { return ((_b = window === null || window === void 0 ? void 0 : window.location) === null || _b === void 0 ? void 0 : _b.href) || None; } } static defaultUser() { var _a, _b, _c, _d, _e; if (isNodeLike) { let username; try { username = userInfo().username; } catch (err) { /* Ignore possible errors like "uv_os_get_passwd returned ENOMEM" that may occur in some environments. */ // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access if (((_a = err.info) === null || _a === void 0 ? void 0 : _a.code) !== "ENOMEM") { throw err; } } return username || (((_b = process.env) === null || _b === void 0 ? void 0 : _b.USERDOMAIN) ? `${(_c = process.env) === null || _c === void 0 ? void 0 : _c.USERDOMAIN}\\${(_d = process.env) === null || _d === void 0 ? void 0 : _d.USERNAME}` : (_e = process === null || process === void 0 ? void 0 : process.env) === null || _e === void 0 ? void 0 : _e.USERNAME) || None; } else { return None; } } static defaultVersion() { var _a; return this.formatHeader([ ["Kusto.JavaScript.Client", SDK_VERSION], ["Runtime." + (isNodeLike ? "Node" : "Browser"), (isNodeLike ? process.version : (_a = window === null || window === void 0 ? void 0 : window.navigator) === null || _a === void 0 ? void 0 : _a.userAgent) || None], ]); } static escapeHeader(header, wrapInBrackets = true) { const clean = header.substring(0, 100).replace(ReplaceRegex, "_"); return wrapInBrackets ? `{${clean}}` : clean; } static formatHeader(args) { return args .filter(([key, val]) => key && val) .map(([key, val]) => `${key}:${this.escapeHeader(val)}`) .join("|"); } static setConnectorDetails(name, version, app_name = null, app_version = null, send_user = false, override_user = null, additional_fields = null) { const params = [["Kusto." + this.escapeHeader(name, false), version]]; app_name = app_name || this.defaultApplication(); app_version = app_version || None; params.push(["App." + this.escapeHeader(app_name), app_version]); params.push(...(additional_fields || [])); let user = None; if (send_user) { user = override_user || this.defaultUser(); } return new ClientDetails(this.formatHeader(params), user); } getHeaders() { return { "x-ms-client-version": this.versionForTracing, "x-ms-app": this.applicationNameForTracing, "x-ms-user": this.userNameForTracing, }; } } //# sourceMappingURL=clientDetails.js.map