Spaces:
Sleeping
Sleeping
File size: 13,320 Bytes
4c452d5 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 | import { bsonType } from './bson_value';
import { BSONError } from './error';
import type { Int32 } from './int_32';
import { Long } from './long';
import { type InspectFn, defaultInspect } from './parser/utils';
/**
* @public
* @deprecated This type is no longer used internally and will be removed in a future version.
*/
export type TimestampOverrides =
| '_bsontype'
| 'toExtendedJSON'
| 'fromExtendedJSON'
| 'inspect'
| typeof bsonType;
/**
* @public
*
* Inherited `Long` members surfaced on `Timestamp`.
* When `Long` gains a new member: add it here if it should pass through, or
* add a `@deprecated declare` line on the `Timestamp` class if it should be
* surfaced as deprecated. Anything left unclassified is silently dropped from
* the public type — preventing accidental behavioral bleed-through.
*/
type TimestampKept =
| 'toBigInt'
| 'toString'
| 'compare'
| 'equals'
| 'eq'
| 'notEquals'
| 'neq'
| 'ne'
| 'lessThan'
| 'lt'
| 'lessThanOrEqual'
| 'lte'
| 'le'
| 'greaterThan'
| 'gt'
| 'greaterThanOrEqual'
| 'gte'
| 'ge';
/** @public */
export type LongWithoutOverrides = new (
low: unknown,
high?: number | boolean,
unsigned?: boolean
) => {
[P in TimestampKept]: Long[P];
};
/** @public */
export const LongWithoutOverridesClass: LongWithoutOverrides =
Long as unknown as LongWithoutOverrides;
/** @public */
export interface TimestampExtended {
$timestamp: {
t: number;
i: number;
};
}
/**
* @public
* @category BSONType
*
* A special type for _internal_ MongoDB use and is **not** associated with the regular Date type.
*/
export class Timestamp extends LongWithoutOverridesClass {
get _bsontype(): 'Timestamp' {
return 'Timestamp';
}
get [bsonType](): 'Timestamp' {
return 'Timestamp';
}
/**
* @deprecated Use `Long.MAX_UNSIGNED_VALUE` instead.
*/
static readonly MAX_VALUE = Long.MAX_UNSIGNED_VALUE;
/**
* An incrementing ordinal for operations within a given second.
*/
get i(): number {
return this.low >>> 0;
}
/**
* A `time_t` value measuring seconds since the Unix epoch
*/
get t(): number {
return this.high >>> 0;
}
/**
* @param int - A 64-bit bigint representing the Timestamp.
*/
constructor(int: bigint);
/**
* @param long - A 64-bit Long representing the Timestamp.
*/
constructor(long: Long);
/**
* @param value - A pair of two values indicating timestamp and increment.
*/
constructor(value: { t: number; i: number });
constructor(low?: bigint | Long | { t: number | Int32; i: number | Int32 }) {
if (low == null) {
super(0, 0, true);
} else if (typeof low === 'bigint') {
super(low, true);
} else if (Long.isLong(low)) {
super(low.low, low.high, true);
} else if (typeof low === 'object' && 't' in low && 'i' in low) {
if (typeof low.t !== 'number' && (typeof low.t !== 'object' || low.t._bsontype !== 'Int32')) {
throw new BSONError('Timestamp constructed from { t, i } must provide t as a number');
}
if (typeof low.i !== 'number' && (typeof low.i !== 'object' || low.i._bsontype !== 'Int32')) {
throw new BSONError('Timestamp constructed from { t, i } must provide i as a number');
}
const t = Number(low.t);
const i = Number(low.i);
if (t < 0 || Number.isNaN(t)) {
throw new BSONError('Timestamp constructed from { t, i } must provide a positive t');
}
if (i < 0 || Number.isNaN(i)) {
throw new BSONError('Timestamp constructed from { t, i } must provide a positive i');
}
if (t > 0xffff_ffff) {
throw new BSONError(
'Timestamp constructed from { t, i } must provide t equal or less than uint32 max'
);
}
if (i > 0xffff_ffff) {
throw new BSONError(
'Timestamp constructed from { t, i } must provide i equal or less than uint32 max'
);
}
super(i, t, true);
} else {
throw new BSONError(
'A Timestamp can only be constructed with: bigint, Long, or { t: number; i: number }'
);
}
}
toJSON(): { $timestamp: string } {
return {
// eslint-disable-next-line @typescript-eslint/no-base-to-string
$timestamp: this.toString()
};
}
/**
* Returns a Timestamp represented by the given (32-bit) integer value.
* @deprecated Stores `value` in the low 32 bits (increment), leaving t = 0. Use `new Timestamp({ t, i })` or `Timestamp.fromBits(lowBits, highBits)` for explicit construction.
*/
static fromInt(value: number): Timestamp {
return new Timestamp(Long.fromInt(value, true));
}
/**
* Returns a Timestamp representing the given number value, provided that it is a finite number. Otherwise, zero is returned.
* @deprecated Splits `value` across (t, i) as a uint64; the result rarely matches user intent. Use `new Timestamp({ t, i })` or `Timestamp.fromBits(lowBits, highBits)` for explicit construction.
*/
static fromNumber(value: number): Timestamp {
return new Timestamp(Long.fromNumber(value, true));
}
/**
* Returns a Timestamp for the given high and low bits. Each is assumed to use 32 bits.
*
* @param lowBits - the low 32-bits.
* @param highBits - the high 32-bits.
*/
static fromBits(lowBits: number, highBits: number): Timestamp {
return new Timestamp({ i: lowBits, t: highBits });
}
/**
* Returns a Timestamp from the given string, optionally using the given radix.
*
* @param str - the textual representation of the Timestamp.
* @param optRadix - the radix in which the text is written.
*/
static fromString(str: string, optRadix: number): Timestamp {
return new Timestamp(Long.fromString(str, true, optRadix));
}
/** @internal */
toExtendedJSON(): TimestampExtended {
return { $timestamp: { t: this.t, i: this.i } };
}
/** @internal */
static fromExtendedJSON(doc: TimestampExtended): Timestamp {
// The Long check is necessary because extended JSON has different behavior given the size of the input number
const i = Long.isLong(doc.$timestamp.i)
? doc.$timestamp.i.getLowBitsUnsigned() // Need to fetch the least significant 32 bits
: doc.$timestamp.i;
const t = Long.isLong(doc.$timestamp.t)
? doc.$timestamp.t.getLowBitsUnsigned() // Need to fetch the least significant 32 bits
: doc.$timestamp.t;
return new Timestamp({ t, i });
}
inspect(depth?: number, options?: unknown, inspect?: InspectFn): string {
inspect ??= defaultInspect;
const t = inspect(this.t, options);
const i = inspect(this.i, options);
return `new Timestamp({ t: ${t}, i: ${i} })`;
}
// ********************
// **** DEPRECATED ****
// ********************
// Declare used to avoid runtime effects for field declaration.
// See: https://www.typescriptlang.org/docs/handbook/2/classes.html#type-only-field-declarations
// Arithmetic
/** @deprecated Not applicable to Timestamp; Timestamp is not intended to be used as a Long. */
declare add: Long['add'];
/** @deprecated Not applicable to Timestamp; Timestamp is not intended to be used as a Long. */
declare subtract: Long['subtract'];
/** @deprecated Not applicable to Timestamp; Timestamp is not intended to be used as a Long. */
declare sub: Long['sub'];
/** @deprecated Not applicable to Timestamp; Timestamp is not intended to be used as a Long. */
declare multiply: Long['multiply'];
/** @deprecated Not applicable to Timestamp; Timestamp is not intended to be used as a Long. */
declare mul: Long['mul'];
/** @deprecated Not applicable to Timestamp; Timestamp is not intended to be used as a Long. */
declare divide: Long['divide'];
/** @deprecated Not applicable to Timestamp; Timestamp is not intended to be used as a Long. */
declare div: Long['div'];
/** @deprecated Not applicable to Timestamp; Timestamp is not intended to be used as a Long. */
declare modulo: Long['modulo'];
/** @deprecated Not applicable to Timestamp; Timestamp is not intended to be used as a Long. */
declare mod: Long['mod'];
/** @deprecated Not applicable to Timestamp; Timestamp is not intended to be used as a Long. */
declare rem: Long['rem'];
/** @deprecated Not applicable to Timestamp as the underlying Long is always unsigned. */
declare negate: Long['negate'];
/** @deprecated Not applicable to Timestamp as the underlying Long is always unsigned. */
declare neg: Long['neg'];
// Bitwise / shifts
/** @deprecated Not applicable to Timestamp; Timestamp is not intended to be used as a Long. */
declare and: Long['and'];
/** @deprecated Not applicable to Timestamp; Timestamp is not intended to be used as a Long. */
declare or: Long['or'];
/** @deprecated Not applicable to Timestamp; Timestamp is not intended to be used as a Long. */
declare xor: Long['xor'];
/** @deprecated Not applicable to Timestamp; Timestamp is not intended to be used as a Long. */
declare not: Long['not'];
/** @deprecated Not applicable to Timestamp; Timestamp is not intended to be used as a Long. */
declare shiftLeft: Long['shiftLeft'];
/** @deprecated Not applicable to Timestamp; Timestamp is not intended to be used as a Long. */
declare shl: Long['shl'];
/** @deprecated Not applicable to Timestamp; Timestamp is not intended to be used as a Long. */
declare shiftRight: Long['shiftRight'];
/** @deprecated Not applicable to Timestamp; Timestamp is not intended to be used as a Long. */
declare shr: Long['shr'];
/** @deprecated Not applicable to Timestamp; Timestamp is not intended to be used as a Long. */
declare shiftRightUnsigned: Long['shiftRightUnsigned'];
/** @deprecated Not applicable to Timestamp; Timestamp is not intended to be used as a Long. */
declare shr_u: Long['shr_u'];
/** @deprecated Not applicable to Timestamp; Timestamp is not intended to be used as a Long. */
declare shru: Long['shru'];
// Signing
/** @deprecated Not applicable to Timestamp as the underlying Long is always unsigned. */
declare toSigned: Long['toSigned'];
/** @deprecated Not applicable to Timestamp as the underlying Long is always unsigned (this is a no-op). */
declare toUnsigned: Long['toUnsigned'];
/** @deprecated Not applicable to Timestamp as the underlying Long is always unsigned (is always false). */
declare isNegative: Long['isNegative'];
/** @deprecated Not applicable to Timestamp as the underlying Long is always unsigned (is always true). */
declare isPositive: Long['isPositive'];
/** @deprecated Not applicable to Timestamp as the underlying Long is always unsigned (is always true). */
declare unsigned: Long['unsigned'];
// Conversion
/** @deprecated Not applicable to Timestamp; use `.t` for seconds since the Unix epoch, or `.i` for the increment ordinal. */
declare toInt: Long['toInt'];
/** @deprecated Not applicable to Timestamp; use `.t` for seconds since the Unix epoch, or `.i` for the increment ordinal. */
declare toNumber: Long['toNumber'];
/** @deprecated Not applicable to Timestamp; use `.t` for seconds since the Unix epoch, or `.i` for the increment ordinal. */
declare toBytes: Long['toBytes'];
/** @deprecated Not applicable to Timestamp; use `.t` for seconds since the Unix epoch, or `.i` for the increment ordinal. */
declare toBytesLE: Long['toBytesLE'];
/** @deprecated Not applicable to Timestamp; use `.t` for seconds since the Unix epoch, or `.i` for the increment ordinal. */
declare toBytesBE: Long['toBytesBE'];
// Other
/** @deprecated Incompatible with Timestamp: returns a signed integer, but Timestamp is always unsigned. Use `.t` for seconds since the Unix epoch. */
declare getHighBits: Long['getHighBits'];
/** @deprecated Not applicable to Timestamp; use `.t` for seconds since the Unix epoch. */
declare getHighBitsUnsigned: Long['getHighBitsUnsigned'];
/** @deprecated Incompatible with Timestamp: returns a signed integer, but Timestamp is always unsigned. Use `.i` for the increment ordinal. */
declare getLowBits: Long['getLowBits'];
/** @deprecated Not applicable to Timestamp; use `.i` for the increment ordinal. */
declare getLowBitsUnsigned: Long['getLowBitsUnsigned'];
/** @deprecated Not applicable to Timestamp; use `.i` instead. */
declare low: Long['low'];
/** @deprecated Not applicable to Timestamp; use `.t` instead. */
declare high: Long['high'];
/** @deprecated Use .compare() for general comparison, or .equals(), .lessThan(), .greaterThan() for specific cases. */
declare comp: Long['comp'];
/** @deprecated Compare `.t` and `.i` against `0` explicitly. */
declare isZero: Long['isZero'];
/** @deprecated Compare `.t` and `.i` against `0` explicitly. */
declare eqz: Long['eqz'];
/** @deprecated Not applicable to Timestamp. Use the bsonType symbol or _bsontype === 'Timestamp' to identify a Timestamp. */
declare __isLong__: Long['__isLong__'];
/** @deprecated Not applicable to Timestamp. */
declare getNumBitsAbs: Long['getNumBitsAbs'];
/** @deprecated Not applicable to Timestamp; tests parity of the increment only. */
declare isEven: Long['isEven'];
/** @deprecated Not applicable to Timestamp; tests parity of the increment only. */
declare isOdd: Long['isOdd'];
}
|