uuid.js 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.UUID = void 0;
  4. var buffer_1 = require("buffer");
  5. var ensure_buffer_1 = require("./ensure_buffer");
  6. var binary_1 = require("./binary");
  7. var uuid_utils_1 = require("./uuid_utils");
  8. var utils_1 = require("./parser/utils");
  9. var error_1 = require("./error");
  10. var BYTE_LENGTH = 16;
  11. var kId = Symbol('id');
  12. /**
  13. * A class representation of the BSON UUID type.
  14. * @public
  15. */
  16. var UUID = /** @class */ (function () {
  17. /**
  18. * Create an UUID type
  19. *
  20. * @param input - Can be a 32 or 36 character hex string (dashes excluded/included) or a 16 byte binary Buffer.
  21. */
  22. function UUID(input) {
  23. if (typeof input === 'undefined') {
  24. // The most common use case (blank id, new UUID() instance)
  25. this.id = UUID.generate();
  26. }
  27. else if (input instanceof UUID) {
  28. this[kId] = buffer_1.Buffer.from(input.id);
  29. this.__id = input.__id;
  30. }
  31. else if (ArrayBuffer.isView(input) && input.byteLength === BYTE_LENGTH) {
  32. this.id = ensure_buffer_1.ensureBuffer(input);
  33. }
  34. else if (typeof input === 'string') {
  35. this.id = uuid_utils_1.uuidHexStringToBuffer(input);
  36. }
  37. else {
  38. throw new error_1.BSONTypeError('Argument passed in UUID constructor must be a UUID, a 16 byte Buffer or a 32/36 character hex string (dashes excluded/included, format: xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx).');
  39. }
  40. }
  41. Object.defineProperty(UUID.prototype, "id", {
  42. /**
  43. * The UUID bytes
  44. * @readonly
  45. */
  46. get: function () {
  47. return this[kId];
  48. },
  49. set: function (value) {
  50. this[kId] = value;
  51. if (UUID.cacheHexString) {
  52. this.__id = uuid_utils_1.bufferToUuidHexString(value);
  53. }
  54. },
  55. enumerable: false,
  56. configurable: true
  57. });
  58. /**
  59. * Generate a 16 byte uuid v4 buffer used in UUIDs
  60. */
  61. /**
  62. * Returns the UUID id as a 32 or 36 character hex string representation, excluding/including dashes (defaults to 36 character dash separated)
  63. * @param includeDashes - should the string exclude dash-separators.
  64. * */
  65. UUID.prototype.toHexString = function (includeDashes) {
  66. if (includeDashes === void 0) { includeDashes = true; }
  67. if (UUID.cacheHexString && this.__id) {
  68. return this.__id;
  69. }
  70. var uuidHexString = uuid_utils_1.bufferToUuidHexString(this.id, includeDashes);
  71. if (UUID.cacheHexString) {
  72. this.__id = uuidHexString;
  73. }
  74. return uuidHexString;
  75. };
  76. /**
  77. * Converts the id into a 36 character (dashes included) hex string, unless a encoding is specified.
  78. */
  79. UUID.prototype.toString = function (encoding) {
  80. return encoding ? this.id.toString(encoding) : this.toHexString();
  81. };
  82. /**
  83. * Converts the id into its JSON string representation.
  84. * A 36 character (dashes included) hex string in the format: xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx
  85. */
  86. UUID.prototype.toJSON = function () {
  87. return this.toHexString();
  88. };
  89. /**
  90. * Compares the equality of this UUID with `otherID`.
  91. *
  92. * @param otherId - UUID instance to compare against.
  93. */
  94. UUID.prototype.equals = function (otherId) {
  95. if (!otherId) {
  96. return false;
  97. }
  98. if (otherId instanceof UUID) {
  99. return otherId.id.equals(this.id);
  100. }
  101. try {
  102. return new UUID(otherId).id.equals(this.id);
  103. }
  104. catch (_a) {
  105. return false;
  106. }
  107. };
  108. /**
  109. * Creates a Binary instance from the current UUID.
  110. */
  111. UUID.prototype.toBinary = function () {
  112. return new binary_1.Binary(this.id, binary_1.Binary.SUBTYPE_UUID);
  113. };
  114. /**
  115. * Generates a populated buffer containing a v4 uuid
  116. */
  117. UUID.generate = function () {
  118. var bytes = utils_1.randomBytes(BYTE_LENGTH);
  119. // Per 4.4, set bits for version and `clock_seq_hi_and_reserved`
  120. // Kindly borrowed from https://github.com/uuidjs/uuid/blob/master/src/v4.js
  121. bytes[6] = (bytes[6] & 0x0f) | 0x40;
  122. bytes[8] = (bytes[8] & 0x3f) | 0x80;
  123. return buffer_1.Buffer.from(bytes);
  124. };
  125. /**
  126. * Checks if a value is a valid bson UUID
  127. * @param input - UUID, string or Buffer to validate.
  128. */
  129. UUID.isValid = function (input) {
  130. if (!input) {
  131. return false;
  132. }
  133. if (input instanceof UUID) {
  134. return true;
  135. }
  136. if (typeof input === 'string') {
  137. return uuid_utils_1.uuidValidateString(input);
  138. }
  139. if (utils_1.isUint8Array(input)) {
  140. // check for length & uuid version (https://tools.ietf.org/html/rfc4122#section-4.1.3)
  141. if (input.length !== BYTE_LENGTH) {
  142. return false;
  143. }
  144. try {
  145. // get this byte as hex: xxxxxxxx-xxxx-XXxx-xxxx-xxxxxxxxxxxx
  146. // check first part as uuid version: xxxxxxxx-xxxx-Xxxx-xxxx-xxxxxxxxxxxx
  147. return parseInt(input[6].toString(16)[0], 10) === binary_1.Binary.SUBTYPE_UUID;
  148. }
  149. catch (_a) {
  150. return false;
  151. }
  152. }
  153. return false;
  154. };
  155. /**
  156. * Creates an UUID from a hex string representation of an UUID.
  157. * @param hexString - 32 or 36 character hex string (dashes excluded/included).
  158. */
  159. UUID.createFromHexString = function (hexString) {
  160. var buffer = uuid_utils_1.uuidHexStringToBuffer(hexString);
  161. return new UUID(buffer);
  162. };
  163. /**
  164. * Converts to a string representation of this Id.
  165. *
  166. * @returns return the 36 character hex string representation.
  167. * @internal
  168. */
  169. UUID.prototype[Symbol.for('nodejs.util.inspect.custom')] = function () {
  170. return this.inspect();
  171. };
  172. UUID.prototype.inspect = function () {
  173. return "new UUID(\"" + this.toHexString() + "\")";
  174. };
  175. return UUID;
  176. }());
  177. exports.UUID = UUID;
  178. Object.defineProperty(UUID.prototype, '_bsontype', { value: 'UUID' });
  179. //# sourceMappingURL=uuid.js.map