uuid_utils.js 1.7 KB

1234567891011121314151617181920212223242526272829303132333435
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.bufferToUuidHexString = exports.uuidHexStringToBuffer = exports.uuidValidateString = void 0;
  4. var buffer_1 = require("buffer");
  5. var error_1 = require("./error");
  6. // Validation regex for v4 uuid (validates with or without dashes)
  7. var VALIDATION_REGEX = /^(?:[0-9a-f]{8}-[0-9a-f]{4}-4[0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}|[0-9a-f]{12}4[0-9a-f]{3}[89ab][0-9a-f]{15})$/i;
  8. var uuidValidateString = function (str) {
  9. return typeof str === 'string' && VALIDATION_REGEX.test(str);
  10. };
  11. exports.uuidValidateString = uuidValidateString;
  12. var uuidHexStringToBuffer = function (hexString) {
  13. if (!exports.uuidValidateString(hexString)) {
  14. throw new error_1.BSONTypeError('UUID string representations must be a 32 or 36 character hex string (dashes excluded/included). Format: "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" or "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx".');
  15. }
  16. var sanitizedHexString = hexString.replace(/-/g, '');
  17. return buffer_1.Buffer.from(sanitizedHexString, 'hex');
  18. };
  19. exports.uuidHexStringToBuffer = uuidHexStringToBuffer;
  20. var bufferToUuidHexString = function (buffer, includeDashes) {
  21. if (includeDashes === void 0) { includeDashes = true; }
  22. return includeDashes
  23. ? buffer.toString('hex', 0, 4) +
  24. '-' +
  25. buffer.toString('hex', 4, 6) +
  26. '-' +
  27. buffer.toString('hex', 6, 8) +
  28. '-' +
  29. buffer.toString('hex', 8, 10) +
  30. '-' +
  31. buffer.toString('hex', 10, 16)
  32. : buffer.toString('hex');
  33. };
  34. exports.bufferToUuidHexString = bufferToUuidHexString;
  35. //# sourceMappingURL=uuid_utils.js.map