UNPKG

@bugsnag/cuid

Version:

Collision-resistant ids optimized for horizontal scaling and performance. For node and browsers.

44 lines (36 loc) 1.26 kB
'use strict'; const pad = require('./pad.js'); const os = require('os'); const _interopDefaultLegacy = e => e && typeof e === 'object' && 'default' in e ? e : { default: e }; const os__default = /*#__PURE__*/_interopDefaultLegacy(os); function getHostname () { try { return os__default.default.hostname(); } catch (e) { /** * This is most likely Windows 7 which is known to cause os.hostname() to break * @see https://github.com/nodejs/node/issues/41297 * @see https://github.com/libuv/libuv/issues/3260 * * Fallback to take hostname from environment variables * @see https://docs.microsoft.com/en-us/windows-server/administration/windows-commands/hostname#notes */ // eslint-disable-next-line no-underscore-dangle return process.env._CLUSTER_NETWORK_NAME_ || process.env.COMPUTERNAME || 'hostname'; } } var padding = 2, pid = pad(process.pid.toString(36), padding), hostname = getHostname(), length = hostname.length, hostId = pad(hostname .split('') .reduce(function (prev, char) { return +prev + char.charCodeAt(0); }, +length + 36) .toString(36), padding); function fingerprint () { return pid + hostId; } module.exports = fingerprint;