@ixo/supamoto-bot-sdk
Version:
An SDK to easily interact with Supamoto bot db
38 lines (34 loc) • 1.53 kB
JavaScript
;
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.generateUniqueCustomerId = generateUniqueCustomerId;
var _crypto = _interopRequireDefault(require("crypto"));
/**
* Generates a unique Customer ID in the format C21009802
* Uses high-precision timestamp + deterministic hashing for guaranteed uniqueness
*
* Approach:
* 1. Get high-precision timestamp (microseconds via process.hrtime.bigint())
* 2. Create deterministic hash of the precise timestamp
* 3. Convert to 8-character alphanumeric string
*
* This handles concurrent sessions by using microsecond precision (~1,000,000 unique
* values per second) making same-timestamp collisions virtually impossible.
*/
function generateUniqueCustomerId() {
// Get high-precision timestamp in nanoseconds
var hrTime = process.hrtime.bigint();
// Convert to microseconds (divide by 1000) for reasonable precision
// This gives us ~1,000,000 unique values per second
var microseconds = hrTime / BigInt(1000);
// Create deterministic hash of the microsecond timestamp
var timestampStr = microseconds.toString();
var hash = _crypto["default"].createHash('sha256').update(timestampStr).digest('hex');
// Take first 8 characters of hash and convert to uppercase
// This gives us 8 characters from [0-9A-F] set
var hashPrefix = hash.substring(0, 8).toUpperCase();
var customerId = "C".concat(hashPrefix);
return customerId;
}