objectid.ts 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  1. import { BSONValue } from './bson_value';
  2. import { BSONError } from './error';
  3. import { type InspectFn, defaultInspect } from './parser/utils';
  4. import { BSONDataView, ByteUtils } from './utils/byte_utils';
  5. // Regular expression that checks for hex value
  6. const checkForHexRegExp = new RegExp('^[0-9a-fA-F]{24}$');
  7. // Unique sequence for the current process (initialized on first use)
  8. let PROCESS_UNIQUE: Uint8Array | null = null;
  9. /** @public */
  10. export interface ObjectIdLike {
  11. id: string | Uint8Array;
  12. __id?: string;
  13. toHexString(): string;
  14. }
  15. /** @public */
  16. export interface ObjectIdExtended {
  17. $oid: string;
  18. }
  19. const kId = Symbol('id');
  20. /**
  21. * A class representation of the BSON ObjectId type.
  22. * @public
  23. * @category BSONType
  24. */
  25. export class ObjectId extends BSONValue {
  26. get _bsontype(): 'ObjectId' {
  27. return 'ObjectId';
  28. }
  29. /** @internal */
  30. private static index = Math.floor(Math.random() * 0xffffff);
  31. static cacheHexString: boolean;
  32. /** ObjectId Bytes @internal */
  33. private [kId]!: Uint8Array;
  34. /** ObjectId hexString cache @internal */
  35. private __id?: string;
  36. /**
  37. * Create an ObjectId type
  38. *
  39. * @param inputId - Can be a 24 character hex string, 12 byte binary Buffer, or a number.
  40. */
  41. constructor(inputId?: string | number | ObjectId | ObjectIdLike | Uint8Array) {
  42. super();
  43. // workingId is set based on type of input and whether valid id exists for the input
  44. let workingId;
  45. if (typeof inputId === 'object' && inputId && 'id' in inputId) {
  46. if (typeof inputId.id !== 'string' && !ArrayBuffer.isView(inputId.id)) {
  47. throw new BSONError('Argument passed in must have an id that is of type string or Buffer');
  48. }
  49. if ('toHexString' in inputId && typeof inputId.toHexString === 'function') {
  50. workingId = ByteUtils.fromHex(inputId.toHexString());
  51. } else {
  52. workingId = inputId.id;
  53. }
  54. } else {
  55. workingId = inputId;
  56. }
  57. // the following cases use workingId to construct an ObjectId
  58. if (workingId == null || typeof workingId === 'number') {
  59. // The most common use case (blank id, new objectId instance)
  60. // Generate a new id
  61. this[kId] = ObjectId.generate(typeof workingId === 'number' ? workingId : undefined);
  62. } else if (ArrayBuffer.isView(workingId) && workingId.byteLength === 12) {
  63. // If intstanceof matches we can escape calling ensure buffer in Node.js environments
  64. this[kId] = ByteUtils.toLocalBufferType(workingId);
  65. } else if (typeof workingId === 'string') {
  66. if (workingId.length === 24 && checkForHexRegExp.test(workingId)) {
  67. this[kId] = ByteUtils.fromHex(workingId);
  68. } else {
  69. throw new BSONError(
  70. 'input must be a 24 character hex string, 12 byte Uint8Array, or an integer'
  71. );
  72. }
  73. } else {
  74. throw new BSONError('Argument passed in does not match the accepted types');
  75. }
  76. // If we are caching the hex string
  77. if (ObjectId.cacheHexString) {
  78. this.__id = ByteUtils.toHex(this.id);
  79. }
  80. }
  81. /**
  82. * The ObjectId bytes
  83. * @readonly
  84. */
  85. get id(): Uint8Array {
  86. return this[kId];
  87. }
  88. set id(value: Uint8Array) {
  89. this[kId] = value;
  90. if (ObjectId.cacheHexString) {
  91. this.__id = ByteUtils.toHex(value);
  92. }
  93. }
  94. /** Returns the ObjectId id as a 24 lowercase character hex string representation */
  95. toHexString(): string {
  96. if (ObjectId.cacheHexString && this.__id) {
  97. return this.__id;
  98. }
  99. const hexString = ByteUtils.toHex(this.id);
  100. if (ObjectId.cacheHexString && !this.__id) {
  101. this.__id = hexString;
  102. }
  103. return hexString;
  104. }
  105. /**
  106. * Update the ObjectId index
  107. * @internal
  108. */
  109. private static getInc(): number {
  110. return (ObjectId.index = (ObjectId.index + 1) % 0xffffff);
  111. }
  112. /**
  113. * Generate a 12 byte id buffer used in ObjectId's
  114. *
  115. * @param time - pass in a second based timestamp.
  116. */
  117. static generate(time?: number): Uint8Array {
  118. if ('number' !== typeof time) {
  119. time = Math.floor(Date.now() / 1000);
  120. }
  121. const inc = ObjectId.getInc();
  122. const buffer = ByteUtils.allocate(12);
  123. // 4-byte timestamp
  124. BSONDataView.fromUint8Array(buffer).setUint32(0, time, false);
  125. // set PROCESS_UNIQUE if yet not initialized
  126. if (PROCESS_UNIQUE === null) {
  127. PROCESS_UNIQUE = ByteUtils.randomBytes(5);
  128. }
  129. // 5-byte process unique
  130. buffer[4] = PROCESS_UNIQUE[0];
  131. buffer[5] = PROCESS_UNIQUE[1];
  132. buffer[6] = PROCESS_UNIQUE[2];
  133. buffer[7] = PROCESS_UNIQUE[3];
  134. buffer[8] = PROCESS_UNIQUE[4];
  135. // 3-byte counter
  136. buffer[11] = inc & 0xff;
  137. buffer[10] = (inc >> 8) & 0xff;
  138. buffer[9] = (inc >> 16) & 0xff;
  139. return buffer;
  140. }
  141. /**
  142. * Converts the id into a 24 character hex string for printing, unless encoding is provided.
  143. * @param encoding - hex or base64
  144. */
  145. toString(encoding?: 'hex' | 'base64'): string {
  146. // Is the id a buffer then use the buffer toString method to return the format
  147. if (encoding === 'base64') return ByteUtils.toBase64(this.id);
  148. if (encoding === 'hex') return this.toHexString();
  149. return this.toHexString();
  150. }
  151. /** Converts to its JSON the 24 character hex string representation. */
  152. toJSON(): string {
  153. return this.toHexString();
  154. }
  155. /** @internal */
  156. private static is(variable: unknown): variable is ObjectId {
  157. return (
  158. variable != null &&
  159. typeof variable === 'object' &&
  160. '_bsontype' in variable &&
  161. variable._bsontype === 'ObjectId'
  162. );
  163. }
  164. /**
  165. * Compares the equality of this ObjectId with `otherID`.
  166. *
  167. * @param otherId - ObjectId instance to compare against.
  168. */
  169. equals(otherId: string | ObjectId | ObjectIdLike | undefined | null): boolean {
  170. if (otherId === undefined || otherId === null) {
  171. return false;
  172. }
  173. if (ObjectId.is(otherId)) {
  174. return this[kId][11] === otherId[kId][11] && ByteUtils.equals(this[kId], otherId[kId]);
  175. }
  176. if (typeof otherId === 'string') {
  177. return otherId.toLowerCase() === this.toHexString();
  178. }
  179. if (typeof otherId === 'object' && typeof otherId.toHexString === 'function') {
  180. const otherIdString = otherId.toHexString();
  181. const thisIdString = this.toHexString();
  182. return typeof otherIdString === 'string' && otherIdString.toLowerCase() === thisIdString;
  183. }
  184. return false;
  185. }
  186. /** Returns the generation date (accurate up to the second) that this ID was generated. */
  187. getTimestamp(): Date {
  188. const timestamp = new Date();
  189. const time = BSONDataView.fromUint8Array(this.id).getUint32(0, false);
  190. timestamp.setTime(Math.floor(time) * 1000);
  191. return timestamp;
  192. }
  193. /** @internal */
  194. static createPk(): ObjectId {
  195. return new ObjectId();
  196. }
  197. /**
  198. * Creates an ObjectId from a second based number, with the rest of the ObjectId zeroed out. Used for comparisons or sorting the ObjectId.
  199. *
  200. * @param time - an integer number representing a number of seconds.
  201. */
  202. static createFromTime(time: number): ObjectId {
  203. const buffer = ByteUtils.fromNumberArray([0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]);
  204. // Encode time into first 4 bytes
  205. BSONDataView.fromUint8Array(buffer).setUint32(0, time, false);
  206. // Return the new objectId
  207. return new ObjectId(buffer);
  208. }
  209. /**
  210. * Creates an ObjectId from a hex string representation of an ObjectId.
  211. *
  212. * @param hexString - create a ObjectId from a passed in 24 character hexstring.
  213. */
  214. static createFromHexString(hexString: string): ObjectId {
  215. if (hexString?.length !== 24) {
  216. throw new BSONError('hex string must be 24 characters');
  217. }
  218. return new ObjectId(ByteUtils.fromHex(hexString));
  219. }
  220. /** Creates an ObjectId instance from a base64 string */
  221. static createFromBase64(base64: string): ObjectId {
  222. if (base64?.length !== 16) {
  223. throw new BSONError('base64 string must be 16 characters');
  224. }
  225. return new ObjectId(ByteUtils.fromBase64(base64));
  226. }
  227. /**
  228. * Checks if a value can be used to create a valid bson ObjectId
  229. * @param id - any JS value
  230. */
  231. static isValid(id: string | number | ObjectId | ObjectIdLike | Uint8Array): boolean {
  232. if (id == null) return false;
  233. try {
  234. new ObjectId(id);
  235. return true;
  236. } catch {
  237. return false;
  238. }
  239. }
  240. /** @internal */
  241. toExtendedJSON(): ObjectIdExtended {
  242. if (this.toHexString) return { $oid: this.toHexString() };
  243. return { $oid: this.toString('hex') };
  244. }
  245. /** @internal */
  246. static fromExtendedJSON(doc: ObjectIdExtended): ObjectId {
  247. return new ObjectId(doc.$oid);
  248. }
  249. /**
  250. * Converts to a string representation of this Id.
  251. *
  252. * @returns return the 24 character hex string representation.
  253. */
  254. inspect(depth?: number, options?: unknown, inspect?: InspectFn): string {
  255. inspect ??= defaultInspect;
  256. return `new ObjectId(${inspect(this.toHexString(), options)})`;
  257. }
  258. }