timestamp.ts 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. import { Long } from './long';
  2. import { isObjectLike } from './parser/utils';
  3. /** @public */
  4. export type TimestampOverrides = '_bsontype' | 'toExtendedJSON' | 'fromExtendedJSON' | 'inspect';
  5. /** @public */
  6. export type LongWithoutOverrides = new (low: unknown, high?: number, unsigned?: boolean) => {
  7. [P in Exclude<keyof Long, TimestampOverrides>]: Long[P];
  8. };
  9. /** @public */
  10. export const LongWithoutOverridesClass: LongWithoutOverrides =
  11. Long as unknown as LongWithoutOverrides;
  12. /** @public */
  13. export interface TimestampExtended {
  14. $timestamp: {
  15. t: number;
  16. i: number;
  17. };
  18. }
  19. /**
  20. * @public
  21. * @category BSONType
  22. * */
  23. export class Timestamp extends LongWithoutOverridesClass {
  24. _bsontype!: 'Timestamp';
  25. static readonly MAX_VALUE = Long.MAX_UNSIGNED_VALUE;
  26. /**
  27. * @param low - A 64-bit Long representing the Timestamp.
  28. */
  29. constructor(long: Long);
  30. /**
  31. * @param value - A pair of two values indicating timestamp and increment.
  32. */
  33. constructor(value: { t: number; i: number });
  34. /**
  35. * @param low - the low (signed) 32 bits of the Timestamp.
  36. * @param high - the high (signed) 32 bits of the Timestamp.
  37. * @deprecated Please use `Timestamp({ t: high, i: low })` or `Timestamp(Long(low, high))` instead.
  38. */
  39. constructor(low: number, high: number);
  40. constructor(low: number | Long | { t: number; i: number }, high?: number) {
  41. // eslint-disable-next-line @typescript-eslint/ban-ts-comment
  42. // @ts-expect-error
  43. if (!(this instanceof Timestamp)) return new Timestamp(low, high);
  44. if (Long.isLong(low)) {
  45. super(low.low, low.high, true);
  46. } else if (isObjectLike(low) && typeof low.t !== 'undefined' && typeof low.i !== 'undefined') {
  47. super(low.i, low.t, true);
  48. } else {
  49. super(low, high, true);
  50. }
  51. Object.defineProperty(this, '_bsontype', {
  52. value: 'Timestamp',
  53. writable: false,
  54. configurable: false,
  55. enumerable: false
  56. });
  57. }
  58. toJSON(): { $timestamp: string } {
  59. return {
  60. $timestamp: this.toString()
  61. };
  62. }
  63. /** Returns a Timestamp represented by the given (32-bit) integer value. */
  64. static fromInt(value: number): Timestamp {
  65. return new Timestamp(Long.fromInt(value, true));
  66. }
  67. /** Returns a Timestamp representing the given number value, provided that it is a finite number. Otherwise, zero is returned. */
  68. static fromNumber(value: number): Timestamp {
  69. return new Timestamp(Long.fromNumber(value, true));
  70. }
  71. /**
  72. * Returns a Timestamp for the given high and low bits. Each is assumed to use 32 bits.
  73. *
  74. * @param lowBits - the low 32-bits.
  75. * @param highBits - the high 32-bits.
  76. */
  77. static fromBits(lowBits: number, highBits: number): Timestamp {
  78. return new Timestamp(lowBits, highBits);
  79. }
  80. /**
  81. * Returns a Timestamp from the given string, optionally using the given radix.
  82. *
  83. * @param str - the textual representation of the Timestamp.
  84. * @param optRadix - the radix in which the text is written.
  85. */
  86. static fromString(str: string, optRadix: number): Timestamp {
  87. return new Timestamp(Long.fromString(str, true, optRadix));
  88. }
  89. /** @internal */
  90. toExtendedJSON(): TimestampExtended {
  91. return { $timestamp: { t: this.high >>> 0, i: this.low >>> 0 } };
  92. }
  93. /** @internal */
  94. static fromExtendedJSON(doc: TimestampExtended): Timestamp {
  95. return new Timestamp(doc.$timestamp);
  96. }
  97. /** @internal */
  98. [Symbol.for('nodejs.util.inspect.custom')](): string {
  99. return this.inspect();
  100. }
  101. inspect(): string {
  102. return `new Timestamp({ t: ${this.getHighBits()}, i: ${this.getLowBits()} })`;
  103. }
  104. }