bson.d.ts 62 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729
  1. /**
  2. * A class representation of the BSON Binary type.
  3. * @public
  4. * @category BSONType
  5. */
  6. export declare class Binary extends BSONValue {
  7. get _bsontype(): 'Binary';
  8. /* Excluded from this release type: BSON_BINARY_SUBTYPE_DEFAULT */
  9. /** Initial buffer default size */
  10. static readonly BUFFER_SIZE = 256;
  11. /** Default BSON type */
  12. static readonly SUBTYPE_DEFAULT = 0;
  13. /** Function BSON type */
  14. static readonly SUBTYPE_FUNCTION = 1;
  15. /**
  16. * Legacy default BSON Binary type
  17. * @deprecated BSON Binary subtype 2 is deprecated in the BSON specification
  18. */
  19. static readonly SUBTYPE_BYTE_ARRAY = 2;
  20. /** Deprecated UUID BSON type @deprecated Please use SUBTYPE_UUID */
  21. static readonly SUBTYPE_UUID_OLD = 3;
  22. /** UUID BSON type */
  23. static readonly SUBTYPE_UUID = 4;
  24. /** MD5 BSON type */
  25. static readonly SUBTYPE_MD5 = 5;
  26. /** Encrypted BSON type */
  27. static readonly SUBTYPE_ENCRYPTED = 6;
  28. /** Column BSON type */
  29. static readonly SUBTYPE_COLUMN = 7;
  30. /** Sensitive BSON type */
  31. static readonly SUBTYPE_SENSITIVE = 8;
  32. /** Vector BSON type */
  33. static readonly SUBTYPE_VECTOR = 9;
  34. /** User BSON type */
  35. static readonly SUBTYPE_USER_DEFINED = 128;
  36. /** datatype of a Binary Vector (subtype: 9) */
  37. static readonly VECTOR_TYPE: Readonly<{
  38. readonly Int8: 3;
  39. readonly Float32: 39;
  40. readonly PackedBit: 16;
  41. }>;
  42. /**
  43. * The bytes of the Binary value.
  44. *
  45. * The format of a Binary value in BSON is defined as:
  46. * ```txt
  47. * binary ::= int32 subtype (byte*)
  48. * ```
  49. *
  50. * This `buffer` is the "(byte*)" segment.
  51. *
  52. * Unless the value is subtype 2, then deserialize will read the first 4 bytes as an int32 and set this to the remaining bytes.
  53. *
  54. * ```txt
  55. * binary ::= int32 unsigned_byte(2) int32 (byte*)
  56. * ```
  57. *
  58. * @see https://bsonspec.org/spec.html
  59. */
  60. buffer: Uint8Array;
  61. /**
  62. * The binary subtype.
  63. *
  64. * Current defined values are:
  65. *
  66. * - `unsigned_byte(0)` Generic binary subtype
  67. * - `unsigned_byte(1)` Function
  68. * - `unsigned_byte(2)` Binary (Deprecated)
  69. * - `unsigned_byte(3)` UUID (Deprecated)
  70. * - `unsigned_byte(4)` UUID
  71. * - `unsigned_byte(5)` MD5
  72. * - `unsigned_byte(6)` Encrypted BSON value
  73. * - `unsigned_byte(7)` Compressed BSON column
  74. * - `unsigned_byte(8)` Sensitive
  75. * - `unsigned_byte(9)` Vector
  76. * - `unsigned_byte(128)` - `unsigned_byte(255)` User defined
  77. */
  78. sub_type: number;
  79. /**
  80. * The Binary's `buffer` can be larger than the Binary's content.
  81. * This property is used to determine where the content ends in the buffer.
  82. */
  83. position: number;
  84. /**
  85. * Create a new Binary instance.
  86. * @param buffer - a buffer object containing the binary data.
  87. * @param subType - the option binary type.
  88. */
  89. constructor(buffer?: BinarySequence, subType?: number);
  90. /**
  91. * Updates this binary with byte_value.
  92. *
  93. * @param byteValue - a single byte we wish to write.
  94. */
  95. put(byteValue: string | number | Uint8Array | number[]): void;
  96. /**
  97. * Writes a buffer to the binary.
  98. *
  99. * @param sequence - a string or buffer to be written to the Binary BSON object.
  100. * @param offset - specify the binary of where to write the content.
  101. */
  102. write(sequence: BinarySequence, offset: number): void;
  103. /**
  104. * Returns a view of **length** bytes starting at **position**.
  105. *
  106. * @param position - read from the given position in the Binary.
  107. * @param length - the number of bytes to read.
  108. */
  109. read(position: number, length: number): Uint8Array;
  110. /** returns a view of the binary value as a Uint8Array */
  111. value(): Uint8Array;
  112. /** the length of the binary sequence */
  113. length(): number;
  114. toJSON(): string;
  115. toString(encoding?: 'hex' | 'base64' | 'utf8' | 'utf-8'): string;
  116. /* Excluded from this release type: toExtendedJSON */
  117. toUUID(): UUID;
  118. /** Creates an Binary instance from a hex digit string */
  119. static createFromHexString(hex: string, subType?: number): Binary;
  120. /** Creates an Binary instance from a base64 string */
  121. static createFromBase64(base64: string, subType?: number): Binary;
  122. /* Excluded from this release type: fromExtendedJSON */
  123. inspect(depth?: number, options?: unknown, inspect?: InspectFn): string;
  124. /**
  125. * If this Binary represents a Int8 Vector (`binary.buffer[0] === Binary.VECTOR_TYPE.Int8`),
  126. * returns a copy of the bytes in a new Int8Array.
  127. *
  128. * If the Binary is not a Vector, or the datatype is not Int8, an error is thrown.
  129. */
  130. toInt8Array(): Int8Array;
  131. /**
  132. * If this Binary represents a Float32 Vector (`binary.buffer[0] === Binary.VECTOR_TYPE.Float32`),
  133. * returns a copy of the bytes in a new Float32Array.
  134. *
  135. * If the Binary is not a Vector, or the datatype is not Float32, an error is thrown.
  136. */
  137. toFloat32Array(): Float32Array;
  138. /**
  139. * If this Binary represents packed bit Vector (`binary.buffer[0] === Binary.VECTOR_TYPE.PackedBit`),
  140. * returns a copy of the bytes that are packed bits.
  141. *
  142. * Use `toBits` to get the unpacked bits.
  143. *
  144. * If the Binary is not a Vector, or the datatype is not PackedBit, an error is thrown.
  145. */
  146. toPackedBits(): Uint8Array;
  147. /**
  148. * If this Binary represents a Packed bit Vector (`binary.buffer[0] === Binary.VECTOR_TYPE.PackedBit`),
  149. * returns a copy of the bit unpacked into a new Int8Array.
  150. *
  151. * Use `toPackedBits` to get the bits still in packed form.
  152. *
  153. * If the Binary is not a Vector, or the datatype is not PackedBit, an error is thrown.
  154. */
  155. toBits(): Int8Array;
  156. /**
  157. * Constructs a Binary representing an Int8 Vector.
  158. * @param array - The array to store as a view on the Binary class
  159. */
  160. static fromInt8Array(array: Int8Array): Binary;
  161. /** Constructs a Binary representing an Float32 Vector. */
  162. static fromFloat32Array(array: Float32Array): Binary;
  163. /**
  164. * Constructs a Binary representing a packed bit Vector.
  165. *
  166. * Use `fromBits` to pack an array of 1s and 0s.
  167. */
  168. static fromPackedBits(array: Uint8Array, padding?: number): Binary;
  169. /**
  170. * Constructs a Binary representing an Packed Bit Vector.
  171. * @param array - The array of 1s and 0s to pack into the Binary instance
  172. */
  173. static fromBits(bits: ArrayLike<number>): Binary;
  174. }
  175. /** @public */
  176. export declare interface BinaryExtended {
  177. $binary: {
  178. subType: string;
  179. base64: string;
  180. };
  181. }
  182. /** @public */
  183. export declare interface BinaryExtendedLegacy {
  184. $type: string;
  185. $binary: string;
  186. }
  187. /** @public */
  188. export declare type BinarySequence = Uint8Array | number[];
  189. declare namespace BSON {
  190. export {
  191. setInternalBufferSize,
  192. serialize,
  193. serializeWithBufferAndIndex,
  194. deserialize,
  195. calculateObjectSize,
  196. deserializeStream,
  197. UUIDExtended,
  198. BinaryExtended,
  199. BinaryExtendedLegacy,
  200. BinarySequence,
  201. CodeExtended,
  202. DBRefLike,
  203. Decimal128Extended,
  204. DoubleExtended,
  205. EJSONOptions,
  206. Int32Extended,
  207. LongExtended,
  208. MaxKeyExtended,
  209. MinKeyExtended,
  210. ObjectIdExtended,
  211. ObjectIdLike,
  212. BSONRegExpExtended,
  213. BSONRegExpExtendedLegacy,
  214. BSONSymbolExtended,
  215. LongWithoutOverrides,
  216. TimestampExtended,
  217. TimestampOverrides,
  218. LongWithoutOverridesClass,
  219. SerializeOptions,
  220. DeserializeOptions,
  221. Code,
  222. BSONSymbol,
  223. DBRef,
  224. Binary,
  225. ObjectId,
  226. UUID,
  227. Long,
  228. Timestamp,
  229. Double,
  230. Int32,
  231. MinKey,
  232. MaxKey,
  233. BSONRegExp,
  234. Decimal128,
  235. BSONValue,
  236. bsonType,
  237. BSONTypeTag,
  238. BSONError,
  239. BSONVersionError,
  240. BSONRuntimeError,
  241. BSONOffsetError,
  242. BSONType,
  243. EJSON,
  244. onDemand,
  245. OnDemand,
  246. Document,
  247. CalculateObjectSizeOptions
  248. }
  249. }
  250. export { BSON }
  251. /* Excluded from this release type: BSON_MAJOR_VERSION */
  252. /* Excluded from this release type: BSON_VERSION_SYMBOL */
  253. /**
  254. * @public
  255. * @experimental
  256. */
  257. declare type BSONElement = [
  258. type: number,
  259. nameOffset: number,
  260. nameLength: number,
  261. offset: number,
  262. length: number
  263. ];
  264. /**
  265. * @public
  266. * @category Error
  267. *
  268. * `BSONError` objects are thrown when BSON encounters an error.
  269. *
  270. * This is the parent class for all the other errors thrown by this library.
  271. */
  272. export declare class BSONError extends Error {
  273. /* Excluded from this release type: bsonError */
  274. get name(): string;
  275. constructor(message: string, options?: {
  276. cause?: unknown;
  277. });
  278. /**
  279. * @public
  280. *
  281. * All errors thrown from the BSON library inherit from `BSONError`.
  282. * This method can assist with determining if an error originates from the BSON library
  283. * even if it does not pass an `instanceof` check against this class' constructor.
  284. *
  285. * @param value - any javascript value that needs type checking
  286. */
  287. static isBSONError(value: unknown): value is BSONError;
  288. }
  289. /**
  290. * @public
  291. * @category Error
  292. *
  293. * @experimental
  294. *
  295. * An error generated when BSON bytes are invalid.
  296. * Reports the offset the parser was able to reach before encountering the error.
  297. */
  298. export declare class BSONOffsetError extends BSONError {
  299. get name(): 'BSONOffsetError';
  300. offset: number;
  301. constructor(message: string, offset: number, options?: {
  302. cause?: unknown;
  303. });
  304. }
  305. /**
  306. * A class representation of the BSON RegExp type.
  307. * @public
  308. * @category BSONType
  309. */
  310. export declare class BSONRegExp extends BSONValue {
  311. get _bsontype(): 'BSONRegExp';
  312. pattern: string;
  313. options: string;
  314. /**
  315. * @param pattern - The regular expression pattern to match
  316. * @param options - The regular expression options
  317. */
  318. constructor(pattern: string, options?: string);
  319. static parseOptions(options?: string): string;
  320. /* Excluded from this release type: toExtendedJSON */
  321. /* Excluded from this release type: fromExtendedJSON */
  322. inspect(depth?: number, options?: unknown, inspect?: InspectFn): string;
  323. }
  324. /** @public */
  325. export declare interface BSONRegExpExtended {
  326. $regularExpression: {
  327. pattern: string;
  328. options: string;
  329. };
  330. }
  331. /** @public */
  332. export declare interface BSONRegExpExtendedLegacy {
  333. $regex: string | BSONRegExp;
  334. $options: string;
  335. }
  336. /**
  337. * @public
  338. * @category Error
  339. *
  340. * An error generated when BSON functions encounter an unexpected input
  341. * or reaches an unexpected/invalid internal state
  342. *
  343. */
  344. export declare class BSONRuntimeError extends BSONError {
  345. get name(): 'BSONRuntimeError';
  346. constructor(message: string);
  347. }
  348. /**
  349. * A class representation of the BSON Symbol type.
  350. * @public
  351. * @category BSONType
  352. */
  353. export declare class BSONSymbol extends BSONValue {
  354. get _bsontype(): 'BSONSymbol';
  355. value: string;
  356. /**
  357. * @param value - the string representing the symbol.
  358. */
  359. constructor(value: string);
  360. /** Access the wrapped string value. */
  361. valueOf(): string;
  362. toString(): string;
  363. toJSON(): string;
  364. /* Excluded from this release type: toExtendedJSON */
  365. /* Excluded from this release type: fromExtendedJSON */
  366. inspect(depth?: number, options?: unknown, inspect?: InspectFn): string;
  367. }
  368. /** @public */
  369. export declare interface BSONSymbolExtended {
  370. $symbol: string;
  371. }
  372. /** @public */
  373. export declare const BSONType: Readonly<{
  374. readonly double: 1;
  375. readonly string: 2;
  376. readonly object: 3;
  377. readonly array: 4;
  378. readonly binData: 5;
  379. readonly undefined: 6;
  380. readonly objectId: 7;
  381. readonly bool: 8;
  382. readonly date: 9;
  383. readonly null: 10;
  384. readonly regex: 11;
  385. readonly dbPointer: 12;
  386. readonly javascript: 13;
  387. readonly symbol: 14;
  388. readonly javascriptWithScope: 15;
  389. readonly int: 16;
  390. readonly timestamp: 17;
  391. readonly long: 18;
  392. readonly decimal: 19;
  393. readonly minKey: -1;
  394. readonly maxKey: 127;
  395. }>;
  396. /** @public */
  397. export declare type BSONType = (typeof BSONType)[keyof typeof BSONType];
  398. /** @public */
  399. export declare const bsonType: unique symbol;
  400. /** @public */
  401. export declare type BSONTypeTag = 'BSONRegExp' | 'BSONSymbol' | 'ObjectId' | 'Binary' | 'Decimal128' | 'Double' | 'Int32' | 'Long' | 'MaxKey' | 'MinKey' | 'Timestamp' | 'Code' | 'DBRef';
  402. /** @public */
  403. export declare abstract class BSONValue {
  404. /** @public */
  405. abstract get _bsontype(): BSONTypeTag;
  406. get [bsonType](): this['_bsontype'];
  407. /* Excluded from this release type: [BSON_VERSION_SYMBOL] */
  408. /**
  409. * @public
  410. * Prints a human-readable string of BSON value information
  411. * If invoked manually without node.js.inspect function, this will default to a modified JSON.stringify
  412. */
  413. abstract inspect(depth?: number, options?: unknown, inspect?: InspectFn): string;
  414. /* Excluded from this release type: toExtendedJSON */
  415. }
  416. /**
  417. * @public
  418. * @category Error
  419. */
  420. export declare class BSONVersionError extends BSONError {
  421. get name(): 'BSONVersionError';
  422. constructor();
  423. }
  424. /**
  425. * @public
  426. * @experimental
  427. *
  428. * A collection of functions that help work with data in a Uint8Array.
  429. * ByteUtils is configured at load time to use Node.js or Web based APIs for the internal implementations.
  430. */
  431. declare type ByteUtils = {
  432. /** Transforms the input to an instance of Buffer if running on node, otherwise Uint8Array */
  433. toLocalBufferType: (buffer: Uint8Array | ArrayBufferView | ArrayBuffer) => Uint8Array;
  434. /** Create empty space of size */
  435. allocate: (size: number) => Uint8Array;
  436. /** Create empty space of size, use pooled memory when available */
  437. allocateUnsafe: (size: number) => Uint8Array;
  438. /** Check if two Uint8Arrays are deep equal */
  439. equals: (a: Uint8Array, b: Uint8Array) => boolean;
  440. /** Check if two Uint8Arrays are deep equal */
  441. fromNumberArray: (array: number[]) => Uint8Array;
  442. /** Create a Uint8Array from a base64 string */
  443. fromBase64: (base64: string) => Uint8Array;
  444. /** Create a base64 string from bytes */
  445. toBase64: (buffer: Uint8Array) => string;
  446. /** **Legacy** binary strings are an outdated method of data transfer. Do not add public API support for interpreting this format */
  447. fromISO88591: (codePoints: string) => Uint8Array;
  448. /** **Legacy** binary strings are an outdated method of data transfer. Do not add public API support for interpreting this format */
  449. toISO88591: (buffer: Uint8Array) => string;
  450. /** Create a Uint8Array from a hex string */
  451. fromHex: (hex: string) => Uint8Array;
  452. /** Create a lowercase hex string from bytes */
  453. toHex: (buffer: Uint8Array) => string;
  454. /** Create a string from utf8 code units, fatal=true will throw an error if UTF-8 bytes are invalid, fatal=false will insert replacement characters */
  455. toUTF8: (buffer: Uint8Array, start: number, end: number, fatal: boolean) => string;
  456. /** Get the utf8 code unit count from a string if it were to be transformed to utf8 */
  457. utf8ByteLength: (input: string) => number;
  458. /** Encode UTF8 bytes generated from `source` string into `destination` at byteOffset. Returns the number of bytes encoded. */
  459. encodeUTF8Into: (destination: Uint8Array, source: string, byteOffset: number) => number;
  460. /** Generate a Uint8Array filled with random bytes with byteLength */
  461. randomBytes: (byteLength: number) => Uint8Array;
  462. /** Interprets `buffer` as an array of 32-bit values and swaps the byte order in-place. */
  463. swap32: (buffer: Uint8Array) => Uint8Array;
  464. };
  465. /* Excluded declaration from this release type: ByteUtils */
  466. /**
  467. * Calculate the bson size for a passed in Javascript object.
  468. *
  469. * @param object - the Javascript object to calculate the BSON byte size for
  470. * @returns size of BSON object in bytes
  471. * @public
  472. */
  473. export declare function calculateObjectSize(object: Document, options?: CalculateObjectSizeOptions): number;
  474. /** @public */
  475. export declare type CalculateObjectSizeOptions = Pick<SerializeOptions, 'serializeFunctions' | 'ignoreUndefined'>;
  476. /**
  477. * A class representation of the BSON Code type.
  478. * @public
  479. * @category BSONType
  480. */
  481. export declare class Code extends BSONValue {
  482. get _bsontype(): 'Code';
  483. code: string;
  484. scope: Document | null;
  485. /**
  486. * @param code - a string or function.
  487. * @param scope - an optional scope for the function.
  488. */
  489. constructor(code: string | Function, scope?: Document | null);
  490. toJSON(): {
  491. code: string;
  492. scope?: Document;
  493. };
  494. /* Excluded from this release type: toExtendedJSON */
  495. /* Excluded from this release type: fromExtendedJSON */
  496. inspect(depth?: number, options?: unknown, inspect?: InspectFn): string;
  497. }
  498. /** @public */
  499. export declare interface CodeExtended {
  500. $code: string;
  501. $scope?: Document;
  502. }
  503. /**
  504. * A class representation of the BSON DBRef type.
  505. * @public
  506. * @category BSONType
  507. */
  508. export declare class DBRef extends BSONValue {
  509. get _bsontype(): 'DBRef';
  510. collection: string;
  511. oid: ObjectId;
  512. db?: string;
  513. fields: Document;
  514. /**
  515. * @param collection - the collection name.
  516. * @param oid - the reference ObjectId.
  517. * @param db - optional db name, if omitted the reference is local to the current db.
  518. */
  519. constructor(collection: string, oid: ObjectId, db?: string, fields?: Document);
  520. /* Excluded from this release type: namespace */
  521. /* Excluded from this release type: namespace */
  522. toJSON(): DBRefLike & Document;
  523. /* Excluded from this release type: toExtendedJSON */
  524. /* Excluded from this release type: fromExtendedJSON */
  525. inspect(depth?: number, options?: unknown, inspect?: InspectFn): string;
  526. }
  527. /** @public */
  528. export declare interface DBRefLike {
  529. $ref: string;
  530. $id: ObjectId;
  531. $db?: string;
  532. }
  533. /**
  534. * A class representation of the BSON Decimal128 type.
  535. * @public
  536. * @category BSONType
  537. */
  538. export declare class Decimal128 extends BSONValue {
  539. get _bsontype(): 'Decimal128';
  540. readonly bytes: Uint8Array;
  541. /**
  542. * @param bytes - a buffer containing the raw Decimal128 bytes in little endian order,
  543. * or a string representation as returned by .toString()
  544. */
  545. constructor(bytes: Uint8Array | string);
  546. /**
  547. * Create a Decimal128 instance from a string representation
  548. *
  549. * @param representation - a numeric string representation.
  550. */
  551. static fromString(representation: string): Decimal128;
  552. /**
  553. * Create a Decimal128 instance from a string representation, allowing for rounding to 34
  554. * significant digits
  555. *
  556. * @example Example of a number that will be rounded
  557. * ```ts
  558. * > let d = Decimal128.fromString('37.499999999999999196428571428571375')
  559. * Uncaught:
  560. * BSONError: "37.499999999999999196428571428571375" is not a valid Decimal128 string - inexact rounding
  561. * at invalidErr (/home/wajames/js-bson/lib/bson.cjs:1402:11)
  562. * at Decimal128.fromStringInternal (/home/wajames/js-bson/lib/bson.cjs:1633:25)
  563. * at Decimal128.fromString (/home/wajames/js-bson/lib/bson.cjs:1424:27)
  564. *
  565. * > d = Decimal128.fromStringWithRounding('37.499999999999999196428571428571375')
  566. * new Decimal128("37.49999999999999919642857142857138")
  567. * ```
  568. * @param representation - a numeric string representation.
  569. */
  570. static fromStringWithRounding(representation: string): Decimal128;
  571. private static _fromString;
  572. /** Create a string representation of the raw Decimal128 value */
  573. toString(): string;
  574. toJSON(): Decimal128Extended;
  575. /* Excluded from this release type: toExtendedJSON */
  576. /* Excluded from this release type: fromExtendedJSON */
  577. inspect(depth?: number, options?: unknown, inspect?: InspectFn): string;
  578. }
  579. /** @public */
  580. export declare interface Decimal128Extended {
  581. $numberDecimal: string;
  582. }
  583. /**
  584. * Deserialize data as BSON.
  585. *
  586. * @param buffer - the buffer containing the serialized set of BSON documents.
  587. * @returns returns the deserialized Javascript Object.
  588. * @public
  589. */
  590. export declare function deserialize(buffer: Uint8Array, options?: DeserializeOptions): Document;
  591. /** @public */
  592. export declare interface DeserializeOptions {
  593. /**
  594. * when deserializing a Long return as a BigInt.
  595. * @defaultValue `false`
  596. */
  597. useBigInt64?: boolean;
  598. /**
  599. * when deserializing a Long will fit it into a Number if it's smaller than 53 bits.
  600. * @defaultValue `true`
  601. */
  602. promoteLongs?: boolean;
  603. /**
  604. * when deserializing a Binary will return it as a node.js Buffer instance.
  605. * @defaultValue `false`
  606. */
  607. promoteBuffers?: boolean;
  608. /**
  609. * when deserializing will promote BSON values to their Node.js closest equivalent types.
  610. * @defaultValue `true`
  611. */
  612. promoteValues?: boolean;
  613. /**
  614. * allow to specify if there what fields we wish to return as unserialized raw buffer.
  615. * @defaultValue `null`
  616. */
  617. fieldsAsRaw?: Document;
  618. /**
  619. * return BSON regular expressions as BSONRegExp instances.
  620. * @defaultValue `false`
  621. */
  622. bsonRegExp?: boolean;
  623. /**
  624. * allows the buffer to be larger than the parsed BSON object.
  625. * @defaultValue `false`
  626. */
  627. allowObjectSmallerThanBufferSize?: boolean;
  628. /**
  629. * Offset into buffer to begin reading document from
  630. * @defaultValue `0`
  631. */
  632. index?: number;
  633. raw?: boolean;
  634. /** Allows for opt-out utf-8 validation for all keys or
  635. * specified keys. Must be all true or all false.
  636. *
  637. * @example
  638. * ```js
  639. * // disables validation on all keys
  640. * validation: { utf8: false }
  641. *
  642. * // enables validation only on specified keys a, b, and c
  643. * validation: { utf8: { a: true, b: true, c: true } }
  644. *
  645. * // disables validation only on specified keys a, b
  646. * validation: { utf8: { a: false, b: false } }
  647. * ```
  648. */
  649. validation?: {
  650. utf8: boolean | Record<string, true> | Record<string, false>;
  651. };
  652. }
  653. /**
  654. * Deserialize stream data as BSON documents.
  655. *
  656. * @param data - the buffer containing the serialized set of BSON documents.
  657. * @param startIndex - the start index in the data Buffer where the deserialization is to start.
  658. * @param numberOfDocuments - number of documents to deserialize.
  659. * @param documents - an array where to store the deserialized documents.
  660. * @param docStartIndex - the index in the documents array from where to start inserting documents.
  661. * @param options - additional options used for the deserialization.
  662. * @returns next index in the buffer after deserialization **x** numbers of documents.
  663. * @public
  664. */
  665. export declare function deserializeStream(data: Uint8Array | ArrayBuffer, startIndex: number, numberOfDocuments: number, documents: Document[], docStartIndex: number, options: DeserializeOptions): number;
  666. /** @public */
  667. export declare interface Document {
  668. [key: string]: any;
  669. }
  670. /**
  671. * A class representation of the BSON Double type.
  672. * @public
  673. * @category BSONType
  674. */
  675. export declare class Double extends BSONValue {
  676. get _bsontype(): 'Double';
  677. value: number;
  678. /**
  679. * Create a Double type
  680. *
  681. * @param value - the number we want to represent as a double.
  682. */
  683. constructor(value: number);
  684. /**
  685. * Attempt to create an double type from string.
  686. *
  687. * This method will throw a BSONError on any string input that is not representable as a IEEE-754 64-bit double.
  688. * Notably, this method will also throw on the following string formats:
  689. * - Strings in non-decimal and non-exponential formats (binary, hex, or octal digits)
  690. * - Strings with characters other than numeric, floating point, or leading sign characters (Note: 'Infinity', '-Infinity', and 'NaN' input strings are still allowed)
  691. * - Strings with leading and/or trailing whitespace
  692. *
  693. * Strings with leading zeros, however, are also allowed
  694. *
  695. * @param value - the string we want to represent as a double.
  696. */
  697. static fromString(value: string): Double;
  698. /**
  699. * Access the number value.
  700. *
  701. * @returns returns the wrapped double number.
  702. */
  703. valueOf(): number;
  704. toJSON(): number;
  705. toString(radix?: number): string;
  706. /* Excluded from this release type: toExtendedJSON */
  707. /* Excluded from this release type: fromExtendedJSON */
  708. inspect(depth?: number, options?: unknown, inspect?: InspectFn): string;
  709. }
  710. /** @public */
  711. export declare interface DoubleExtended {
  712. $numberDouble: string;
  713. }
  714. /** @public */
  715. export declare const EJSON: {
  716. parse: typeof parse;
  717. stringify: typeof stringify;
  718. serialize: typeof EJSONserialize;
  719. deserialize: typeof EJSONdeserialize;
  720. };
  721. /**
  722. * Deserializes an Extended JSON object into a plain JavaScript object with native/BSON types
  723. *
  724. * @param ejson - The Extended JSON object to deserialize
  725. * @param options - Optional settings passed to the parse method
  726. */
  727. declare function EJSONdeserialize(ejson: Document, options?: EJSONOptions): any;
  728. /** @public */
  729. export declare type EJSONOptions = {
  730. /**
  731. * Output using the Extended JSON v1 spec
  732. * @defaultValue `false`
  733. */
  734. legacy?: boolean;
  735. /**
  736. * Enable Extended JSON's `relaxed` mode, which attempts to return native JS types where possible, rather than BSON types
  737. * @defaultValue `false` */
  738. relaxed?: boolean;
  739. /**
  740. * Enable native bigint support
  741. * @defaultValue `false`
  742. */
  743. useBigInt64?: boolean;
  744. };
  745. /**
  746. * Serializes an object to an Extended JSON string, and reparse it as a JavaScript object.
  747. *
  748. * @param value - The object to serialize
  749. * @param options - Optional settings passed to the `stringify` function
  750. */
  751. declare function EJSONserialize(value: any, options?: EJSONOptions): Document;
  752. declare type InspectFn = (x: unknown, options?: unknown) => string;
  753. /**
  754. * A class representation of a BSON Int32 type.
  755. * @public
  756. * @category BSONType
  757. */
  758. export declare class Int32 extends BSONValue {
  759. get _bsontype(): 'Int32';
  760. value: number;
  761. /**
  762. * Create an Int32 type
  763. *
  764. * @param value - the number we want to represent as an int32.
  765. */
  766. constructor(value: number | string);
  767. /**
  768. * Attempt to create an Int32 type from string.
  769. *
  770. * This method will throw a BSONError on any string input that is not representable as an Int32.
  771. * Notably, this method will also throw on the following string formats:
  772. * - Strings in non-decimal formats (exponent notation, binary, hex, or octal digits)
  773. * - Strings non-numeric and non-leading sign characters (ex: '2.0', '24,000')
  774. * - Strings with leading and/or trailing whitespace
  775. *
  776. * Strings with leading zeros, however, are allowed.
  777. *
  778. * @param value - the string we want to represent as an int32.
  779. */
  780. static fromString(value: string): Int32;
  781. /**
  782. * Access the number value.
  783. *
  784. * @returns returns the wrapped int32 number.
  785. */
  786. valueOf(): number;
  787. toString(radix?: number): string;
  788. toJSON(): number;
  789. /* Excluded from this release type: toExtendedJSON */
  790. /* Excluded from this release type: fromExtendedJSON */
  791. inspect(depth?: number, options?: unknown, inspect?: InspectFn): string;
  792. }
  793. /** @public */
  794. export declare interface Int32Extended {
  795. $numberInt: string;
  796. }
  797. /**
  798. * A class representing a 64-bit integer
  799. * @public
  800. * @category BSONType
  801. * @remarks
  802. * The internal representation of a long is the two given signed, 32-bit values.
  803. * We use 32-bit pieces because these are the size of integers on which
  804. * Javascript performs bit-operations. For operations like addition and
  805. * multiplication, we split each number into 16 bit pieces, which can easily be
  806. * multiplied within Javascript's floating-point representation without overflow
  807. * or change in sign.
  808. * In the algorithms below, we frequently reduce the negative case to the
  809. * positive case by negating the input(s) and then post-processing the result.
  810. * Note that we must ALWAYS check specially whether those values are MIN_VALUE
  811. * (-2^63) because -MIN_VALUE == MIN_VALUE (since 2^63 cannot be represented as
  812. * a positive number, it overflows back into a negative). Not handling this
  813. * case would often result in infinite recursion.
  814. * Common constant values ZERO, ONE, NEG_ONE, etc. are found as static properties on this class.
  815. */
  816. export declare class Long extends BSONValue {
  817. get _bsontype(): 'Long';
  818. /** An indicator used to reliably determine if an object is a Long or not. */
  819. get __isLong__(): boolean;
  820. /**
  821. * The high 32 bits as a signed value.
  822. */
  823. high: number;
  824. /**
  825. * The low 32 bits as a signed value.
  826. */
  827. low: number;
  828. /**
  829. * Whether unsigned or not.
  830. */
  831. unsigned: boolean;
  832. /**
  833. * Constructs a 64 bit two's-complement integer, given its low and high 32 bit values as *signed* integers.
  834. *
  835. * @param low - The low (signed) 32 bits of the long
  836. * @param high - The high (signed) 32 bits of the long
  837. * @param unsigned - Whether unsigned or not, defaults to signed
  838. */
  839. constructor(low: number, high?: number, unsigned?: boolean);
  840. /**
  841. * Constructs a 64 bit two's-complement integer, given a bigint representation.
  842. *
  843. * @param value - BigInt representation of the long value
  844. * @param unsigned - Whether unsigned or not, defaults to signed
  845. */
  846. constructor(value: bigint, unsigned?: boolean);
  847. /**
  848. * Constructs a 64 bit two's-complement integer, given a string representation.
  849. *
  850. * @param value - String representation of the long value
  851. * @param unsigned - Whether unsigned or not, defaults to signed
  852. */
  853. constructor(value: string, unsigned?: boolean);
  854. static TWO_PWR_24: Long;
  855. /** Maximum unsigned value. */
  856. static MAX_UNSIGNED_VALUE: Long;
  857. /** Signed zero */
  858. static ZERO: Long;
  859. /** Unsigned zero. */
  860. static UZERO: Long;
  861. /** Signed one. */
  862. static ONE: Long;
  863. /** Unsigned one. */
  864. static UONE: Long;
  865. /** Signed negative one. */
  866. static NEG_ONE: Long;
  867. /** Maximum signed value. */
  868. static MAX_VALUE: Long;
  869. /** Minimum signed value. */
  870. static MIN_VALUE: Long;
  871. /**
  872. * Returns a Long representing the 64 bit integer that comes by concatenating the given low and high bits.
  873. * Each is assumed to use 32 bits.
  874. * @param lowBits - The low 32 bits
  875. * @param highBits - The high 32 bits
  876. * @param unsigned - Whether unsigned or not, defaults to signed
  877. * @returns The corresponding Long value
  878. */
  879. static fromBits(lowBits: number, highBits: number, unsigned?: boolean): Long;
  880. /**
  881. * Returns a Long representing the given 32 bit integer value.
  882. * @param value - The 32 bit integer in question
  883. * @param unsigned - Whether unsigned or not, defaults to signed
  884. * @returns The corresponding Long value
  885. */
  886. static fromInt(value: number, unsigned?: boolean): Long;
  887. /**
  888. * Returns a Long representing the given value, provided that it is a finite number. Otherwise, zero is returned.
  889. * @param value - The number in question
  890. * @param unsigned - Whether unsigned or not, defaults to signed
  891. * @returns The corresponding Long value
  892. */
  893. static fromNumber(value: number, unsigned?: boolean): Long;
  894. /**
  895. * Returns a Long representing the given value, provided that it is a finite number. Otherwise, zero is returned.
  896. * @param value - The number in question
  897. * @param unsigned - Whether unsigned or not, defaults to signed
  898. * @returns The corresponding Long value
  899. */
  900. static fromBigInt(value: bigint, unsigned?: boolean): Long;
  901. /* Excluded from this release type: _fromString */
  902. /**
  903. * Returns a signed Long representation of the given string, written using radix 10.
  904. * Will throw an error if the given text is not exactly representable as a Long.
  905. * Throws an error if any of the following conditions are true:
  906. * - the string contains invalid characters for the radix 10
  907. * - the string contains whitespace
  908. * - the value the string represents is too large or too small to be a Long
  909. * Unlike Long.fromString, this method does not coerce '+/-Infinity' and 'NaN' to Long.Zero
  910. * @param str - The textual representation of the Long
  911. * @returns The corresponding Long value
  912. */
  913. static fromStringStrict(str: string): Long;
  914. /**
  915. * Returns a Long representation of the given string, written using the radix 10.
  916. * Will throw an error if the given parameters are not exactly representable as a Long.
  917. * Throws an error if any of the following conditions are true:
  918. * - the string contains invalid characters for the given radix
  919. * - the string contains whitespace
  920. * - the value the string represents is too large or too small to be a Long
  921. * Unlike Long.fromString, this method does not coerce '+/-Infinity' and 'NaN' to Long.Zero
  922. * @param str - The textual representation of the Long
  923. * @param unsigned - Whether unsigned or not, defaults to signed
  924. * @returns The corresponding Long value
  925. */
  926. static fromStringStrict(str: string, unsigned?: boolean): Long;
  927. /**
  928. * Returns a signed Long representation of the given string, written using the specified radix.
  929. * Will throw an error if the given parameters are not exactly representable as a Long.
  930. * Throws an error if any of the following conditions are true:
  931. * - the string contains invalid characters for the given radix
  932. * - the string contains whitespace
  933. * - the value the string represents is too large or too small to be a Long
  934. * Unlike Long.fromString, this method does not coerce '+/-Infinity' and 'NaN' to Long.Zero
  935. * @param str - The textual representation of the Long
  936. * @param radix - The radix in which the text is written (2-36), defaults to 10
  937. * @returns The corresponding Long value
  938. */
  939. static fromStringStrict(str: string, radix?: boolean): Long;
  940. /**
  941. * Returns a Long representation of the given string, written using the specified radix.
  942. * Will throw an error if the given parameters are not exactly representable as a Long.
  943. * Throws an error if any of the following conditions are true:
  944. * - the string contains invalid characters for the given radix
  945. * - the string contains whitespace
  946. * - the value the string represents is too large or too small to be a Long
  947. * Unlike Long.fromString, this method does not coerce '+/-Infinity' and 'NaN' to Long.Zero
  948. * @param str - The textual representation of the Long
  949. * @param unsigned - Whether unsigned or not, defaults to signed
  950. * @param radix - The radix in which the text is written (2-36), defaults to 10
  951. * @returns The corresponding Long value
  952. */
  953. static fromStringStrict(str: string, unsigned?: boolean, radix?: number): Long;
  954. /**
  955. * Returns a signed Long representation of the given string, written using radix 10.
  956. *
  957. * If the input string is empty, this function will throw a BSONError.
  958. *
  959. * If input string does not have valid signed 64-bit Long representation, this method will return a coerced value:
  960. * - inputs that overflow 64-bit signed long will be coerced to Long.MAX_VALUE and Long.MIN_VALUE respectively
  961. * - 'NaN' or '+/-Infinity' are coerced to Long.ZERO
  962. * - other invalid characters sequences have variable behavior
  963. *
  964. * @param str - The textual representation of the Long
  965. * @returns The corresponding Long value
  966. */
  967. static fromString(str: string): Long;
  968. /**
  969. * Returns a signed Long representation of the given string, written using the provided radix.
  970. *
  971. * If the input string is empty or a provided radix is not within (2-36), this function will throw a BSONError.
  972. *
  973. * If input parameters do not have valid signed 64-bit Long representation, this method will return a coerced value:
  974. * - inputs that overflow 64-bit signed long will be coerced to Long.MAX_VALUE and Long.MIN_VALUE respectively
  975. * - if the radix is less than 24, 'NaN' is coerced to Long.ZERO
  976. * - if the radix is less than 35, '+/-Infinity' inputs are coerced to Long.ZERO
  977. * - other invalid characters sequences have variable behavior
  978. * @param str - The textual representation of the Long
  979. * @param radix - The radix in which the text is written (2-36), defaults to 10
  980. * @returns The corresponding Long value
  981. */
  982. static fromString(str: string, radix?: number): Long;
  983. /**
  984. * Returns a Long representation of the given string, written using radix 10.
  985. *
  986. * If the input string is empty, this function will throw a BSONError.
  987. *
  988. * If input parameters do not have a valid 64-bit Long representation, this method will return a coerced value:
  989. * - inputs that overflow 64-bit long will be coerced to max or min (if signed) values
  990. * - if the radix is less than 24, 'NaN' is coerced to Long.ZERO
  991. * - if the radix is less than 35, '+/-Infinity' inputs are coerced to Long.ZERO
  992. * - other invalid characters sequences have variable behavior
  993. * @param str - The textual representation of the Long
  994. * @param unsigned - Whether unsigned or not, defaults to signed
  995. * @returns The corresponding Long value
  996. */
  997. static fromString(str: string, unsigned?: boolean): Long;
  998. /**
  999. * Returns a Long representation of the given string, written using the specified radix.
  1000. *
  1001. * If the input string is empty or a provided radix is not within (2-36), this function will throw a BSONError.
  1002. *
  1003. * If input parameters do not have a valid 64-bit Long representation, this method will return a coerced value:
  1004. * - inputs that overflow 64-bit long will be coerced to max or min (if signed) values
  1005. * - if the radix is less than 24, 'NaN' is coerced to Long.ZERO
  1006. * - if the radix is less than 35, '+/-Infinity' inputs are coerced to Long.ZERO
  1007. * - other invalid characters sequences have variable behavior
  1008. * @param str - The textual representation of the Long
  1009. * @param unsigned - Whether unsigned or not, defaults to signed
  1010. * @param radix - The radix in which the text is written (2-36), defaults to 10
  1011. * @returns The corresponding Long value
  1012. */
  1013. static fromString(str: string, unsigned?: boolean, radix?: number): Long;
  1014. /**
  1015. * Creates a Long from its byte representation.
  1016. * @param bytes - Byte representation
  1017. * @param unsigned - Whether unsigned or not, defaults to signed
  1018. * @param le - Whether little or big endian, defaults to big endian
  1019. * @returns The corresponding Long value
  1020. */
  1021. static fromBytes(bytes: number[], unsigned?: boolean, le?: boolean): Long;
  1022. /**
  1023. * Creates a Long from its little endian byte representation.
  1024. * @param bytes - Little endian byte representation
  1025. * @param unsigned - Whether unsigned or not, defaults to signed
  1026. * @returns The corresponding Long value
  1027. */
  1028. static fromBytesLE(bytes: number[], unsigned?: boolean): Long;
  1029. /**
  1030. * Creates a Long from its big endian byte representation.
  1031. * @param bytes - Big endian byte representation
  1032. * @param unsigned - Whether unsigned or not, defaults to signed
  1033. * @returns The corresponding Long value
  1034. */
  1035. static fromBytesBE(bytes: number[], unsigned?: boolean): Long;
  1036. /**
  1037. * Tests if the specified object is a Long.
  1038. */
  1039. static isLong(value: unknown): value is Long;
  1040. /**
  1041. * Converts the specified value to a Long.
  1042. * @param unsigned - Whether unsigned or not, defaults to signed
  1043. */
  1044. static fromValue(val: number | string | {
  1045. low: number;
  1046. high: number;
  1047. unsigned?: boolean;
  1048. }, unsigned?: boolean): Long;
  1049. /** Returns the sum of this and the specified Long. */
  1050. add(addend: string | number | Long | Timestamp): Long;
  1051. /**
  1052. * Returns the sum of this and the specified Long.
  1053. * @returns Sum
  1054. */
  1055. and(other: string | number | Long | Timestamp): Long;
  1056. /**
  1057. * Compares this Long's value with the specified's.
  1058. * @returns 0 if they are the same, 1 if the this is greater and -1 if the given one is greater
  1059. */
  1060. compare(other: string | number | Long | Timestamp): 0 | 1 | -1;
  1061. /** This is an alias of {@link Long.compare} */
  1062. comp(other: string | number | Long | Timestamp): 0 | 1 | -1;
  1063. /**
  1064. * Returns this Long divided by the specified. The result is signed if this Long is signed or unsigned if this Long is unsigned.
  1065. * @returns Quotient
  1066. */
  1067. divide(divisor: string | number | Long | Timestamp): Long;
  1068. /**This is an alias of {@link Long.divide} */
  1069. div(divisor: string | number | Long | Timestamp): Long;
  1070. /**
  1071. * Tests if this Long's value equals the specified's.
  1072. * @param other - Other value
  1073. */
  1074. equals(other: string | number | Long | Timestamp): boolean;
  1075. /** This is an alias of {@link Long.equals} */
  1076. eq(other: string | number | Long | Timestamp): boolean;
  1077. /** Gets the high 32 bits as a signed integer. */
  1078. getHighBits(): number;
  1079. /** Gets the high 32 bits as an unsigned integer. */
  1080. getHighBitsUnsigned(): number;
  1081. /** Gets the low 32 bits as a signed integer. */
  1082. getLowBits(): number;
  1083. /** Gets the low 32 bits as an unsigned integer. */
  1084. getLowBitsUnsigned(): number;
  1085. /** Gets the number of bits needed to represent the absolute value of this Long. */
  1086. getNumBitsAbs(): number;
  1087. /** Tests if this Long's value is greater than the specified's. */
  1088. greaterThan(other: string | number | Long | Timestamp): boolean;
  1089. /** This is an alias of {@link Long.greaterThan} */
  1090. gt(other: string | number | Long | Timestamp): boolean;
  1091. /** Tests if this Long's value is greater than or equal the specified's. */
  1092. greaterThanOrEqual(other: string | number | Long | Timestamp): boolean;
  1093. /** This is an alias of {@link Long.greaterThanOrEqual} */
  1094. gte(other: string | number | Long | Timestamp): boolean;
  1095. /** This is an alias of {@link Long.greaterThanOrEqual} */
  1096. ge(other: string | number | Long | Timestamp): boolean;
  1097. /** Tests if this Long's value is even. */
  1098. isEven(): boolean;
  1099. /** Tests if this Long's value is negative. */
  1100. isNegative(): boolean;
  1101. /** Tests if this Long's value is odd. */
  1102. isOdd(): boolean;
  1103. /** Tests if this Long's value is positive. */
  1104. isPositive(): boolean;
  1105. /** Tests if this Long's value equals zero. */
  1106. isZero(): boolean;
  1107. /** Tests if this Long's value is less than the specified's. */
  1108. lessThan(other: string | number | Long | Timestamp): boolean;
  1109. /** This is an alias of {@link Long#lessThan}. */
  1110. lt(other: string | number | Long | Timestamp): boolean;
  1111. /** Tests if this Long's value is less than or equal the specified's. */
  1112. lessThanOrEqual(other: string | number | Long | Timestamp): boolean;
  1113. /** This is an alias of {@link Long.lessThanOrEqual} */
  1114. lte(other: string | number | Long | Timestamp): boolean;
  1115. /** Returns this Long modulo the specified. */
  1116. modulo(divisor: string | number | Long | Timestamp): Long;
  1117. /** This is an alias of {@link Long.modulo} */
  1118. mod(divisor: string | number | Long | Timestamp): Long;
  1119. /** This is an alias of {@link Long.modulo} */
  1120. rem(divisor: string | number | Long | Timestamp): Long;
  1121. /**
  1122. * Returns the product of this and the specified Long.
  1123. * @param multiplier - Multiplier
  1124. * @returns Product
  1125. */
  1126. multiply(multiplier: string | number | Long | Timestamp): Long;
  1127. /** This is an alias of {@link Long.multiply} */
  1128. mul(multiplier: string | number | Long | Timestamp): Long;
  1129. /** Returns the Negation of this Long's value. */
  1130. negate(): Long;
  1131. /** This is an alias of {@link Long.negate} */
  1132. neg(): Long;
  1133. /** Returns the bitwise NOT of this Long. */
  1134. not(): Long;
  1135. /** Tests if this Long's value differs from the specified's. */
  1136. notEquals(other: string | number | Long | Timestamp): boolean;
  1137. /** This is an alias of {@link Long.notEquals} */
  1138. neq(other: string | number | Long | Timestamp): boolean;
  1139. /** This is an alias of {@link Long.notEquals} */
  1140. ne(other: string | number | Long | Timestamp): boolean;
  1141. /**
  1142. * Returns the bitwise OR of this Long and the specified.
  1143. */
  1144. or(other: number | string | Long): Long;
  1145. /**
  1146. * Returns this Long with bits shifted to the left by the given amount.
  1147. * @param numBits - Number of bits
  1148. * @returns Shifted Long
  1149. */
  1150. shiftLeft(numBits: number | Long): Long;
  1151. /** This is an alias of {@link Long.shiftLeft} */
  1152. shl(numBits: number | Long): Long;
  1153. /**
  1154. * Returns this Long with bits arithmetically shifted to the right by the given amount.
  1155. * @param numBits - Number of bits
  1156. * @returns Shifted Long
  1157. */
  1158. shiftRight(numBits: number | Long): Long;
  1159. /** This is an alias of {@link Long.shiftRight} */
  1160. shr(numBits: number | Long): Long;
  1161. /**
  1162. * Returns this Long with bits logically shifted to the right by the given amount.
  1163. * @param numBits - Number of bits
  1164. * @returns Shifted Long
  1165. */
  1166. shiftRightUnsigned(numBits: Long | number): Long;
  1167. /** This is an alias of {@link Long.shiftRightUnsigned} */
  1168. shr_u(numBits: number | Long): Long;
  1169. /** This is an alias of {@link Long.shiftRightUnsigned} */
  1170. shru(numBits: number | Long): Long;
  1171. /**
  1172. * Returns the difference of this and the specified Long.
  1173. * @param subtrahend - Subtrahend
  1174. * @returns Difference
  1175. */
  1176. subtract(subtrahend: string | number | Long | Timestamp): Long;
  1177. /** This is an alias of {@link Long.subtract} */
  1178. sub(subtrahend: string | number | Long | Timestamp): Long;
  1179. /** Converts the Long to a 32 bit integer, assuming it is a 32 bit integer. */
  1180. toInt(): number;
  1181. /** Converts the Long to a the nearest floating-point representation of this value (double, 53 bit mantissa). */
  1182. toNumber(): number;
  1183. /** Converts the Long to a BigInt (arbitrary precision). */
  1184. toBigInt(): bigint;
  1185. /**
  1186. * Converts this Long to its byte representation.
  1187. * @param le - Whether little or big endian, defaults to big endian
  1188. * @returns Byte representation
  1189. */
  1190. toBytes(le?: boolean): number[];
  1191. /**
  1192. * Converts this Long to its little endian byte representation.
  1193. * @returns Little endian byte representation
  1194. */
  1195. toBytesLE(): number[];
  1196. /**
  1197. * Converts this Long to its big endian byte representation.
  1198. * @returns Big endian byte representation
  1199. */
  1200. toBytesBE(): number[];
  1201. /**
  1202. * Converts this Long to signed.
  1203. */
  1204. toSigned(): Long;
  1205. /**
  1206. * Converts the Long to a string written in the specified radix.
  1207. * @param radix - Radix (2-36), defaults to 10
  1208. * @throws RangeError If `radix` is out of range
  1209. */
  1210. toString(radix?: number): string;
  1211. /** Converts this Long to unsigned. */
  1212. toUnsigned(): Long;
  1213. /** Returns the bitwise XOR of this Long and the given one. */
  1214. xor(other: Long | number | string): Long;
  1215. /** This is an alias of {@link Long.isZero} */
  1216. eqz(): boolean;
  1217. /** This is an alias of {@link Long.lessThanOrEqual} */
  1218. le(other: string | number | Long | Timestamp): boolean;
  1219. toExtendedJSON(options?: EJSONOptions): number | LongExtended;
  1220. static fromExtendedJSON(doc: {
  1221. $numberLong: string;
  1222. }, options?: EJSONOptions): number | Long | bigint;
  1223. inspect(depth?: number, options?: unknown, inspect?: InspectFn): string;
  1224. }
  1225. /** @public */
  1226. export declare interface LongExtended {
  1227. $numberLong: string;
  1228. }
  1229. /** @public */
  1230. export declare type LongWithoutOverrides = new (low: unknown, high?: number | boolean, unsigned?: boolean) => {
  1231. [P in Exclude<keyof Long, TimestampOverrides>]: Long[P];
  1232. };
  1233. /** @public */
  1234. export declare const LongWithoutOverridesClass: LongWithoutOverrides;
  1235. /**
  1236. * A class representation of the BSON MaxKey type.
  1237. * @public
  1238. * @category BSONType
  1239. */
  1240. export declare class MaxKey extends BSONValue {
  1241. get _bsontype(): 'MaxKey';
  1242. /* Excluded from this release type: toExtendedJSON */
  1243. /* Excluded from this release type: fromExtendedJSON */
  1244. inspect(): string;
  1245. }
  1246. /** @public */
  1247. export declare interface MaxKeyExtended {
  1248. $maxKey: 1;
  1249. }
  1250. /**
  1251. * A class representation of the BSON MinKey type.
  1252. * @public
  1253. * @category BSONType
  1254. */
  1255. export declare class MinKey extends BSONValue {
  1256. get _bsontype(): 'MinKey';
  1257. /* Excluded from this release type: toExtendedJSON */
  1258. /* Excluded from this release type: fromExtendedJSON */
  1259. inspect(): string;
  1260. }
  1261. /** @public */
  1262. export declare interface MinKeyExtended {
  1263. $minKey: 1;
  1264. }
  1265. /**
  1266. * @experimental
  1267. * @public
  1268. *
  1269. * A collection of functions that get or set various numeric types and bit widths from a Uint8Array.
  1270. */
  1271. declare type NumberUtils = {
  1272. /** Is true if the current system is big endian. */
  1273. isBigEndian: boolean;
  1274. /**
  1275. * Parses a signed int32 at offset. Throws a `RangeError` if value is negative.
  1276. */
  1277. getNonnegativeInt32LE: (source: Uint8Array, offset: number) => number;
  1278. getInt32LE: (source: Uint8Array, offset: number) => number;
  1279. getUint32LE: (source: Uint8Array, offset: number) => number;
  1280. getUint32BE: (source: Uint8Array, offset: number) => number;
  1281. getBigInt64LE: (source: Uint8Array, offset: number) => bigint;
  1282. getFloat64LE: (source: Uint8Array, offset: number) => number;
  1283. setInt32BE: (destination: Uint8Array, offset: number, value: number) => 4;
  1284. setInt32LE: (destination: Uint8Array, offset: number, value: number) => 4;
  1285. setBigInt64LE: (destination: Uint8Array, offset: number, value: bigint) => 8;
  1286. setFloat64LE: (destination: Uint8Array, offset: number, value: number) => 8;
  1287. };
  1288. /**
  1289. * Number parsing and serializing utilities.
  1290. *
  1291. * @experimental
  1292. * @public
  1293. */
  1294. declare const NumberUtils: NumberUtils;
  1295. /**
  1296. * A class representation of the BSON ObjectId type.
  1297. * @public
  1298. * @category BSONType
  1299. */
  1300. export declare class ObjectId extends BSONValue {
  1301. get _bsontype(): 'ObjectId';
  1302. /* Excluded from this release type: index */
  1303. static cacheHexString: boolean;
  1304. /* Excluded from this release type: buffer */
  1305. /** To generate a new ObjectId, use ObjectId() with no argument. */
  1306. constructor();
  1307. /**
  1308. * Create ObjectId from a 24 character hex string.
  1309. *
  1310. * @param inputId - A 24 character hex string.
  1311. */
  1312. constructor(inputId: string);
  1313. /**
  1314. * Create ObjectId from the BSON ObjectId type.
  1315. *
  1316. * @param inputId - The BSON ObjectId type.
  1317. */
  1318. constructor(inputId: ObjectId);
  1319. /**
  1320. * Create ObjectId from the object type that has the toHexString method.
  1321. *
  1322. * @param inputId - The ObjectIdLike type.
  1323. */
  1324. constructor(inputId: ObjectIdLike);
  1325. /**
  1326. * Create ObjectId from a 12 byte binary Buffer.
  1327. *
  1328. * @param inputId - A 12 byte binary Buffer.
  1329. */
  1330. constructor(inputId: Uint8Array);
  1331. /**
  1332. * Implementation overload.
  1333. *
  1334. * @param inputId - All input types that are used in the constructor implementation.
  1335. */
  1336. constructor(inputId?: string | ObjectId | ObjectIdLike | Uint8Array);
  1337. /**
  1338. * The ObjectId bytes
  1339. * @readonly
  1340. */
  1341. get id(): Uint8Array;
  1342. set id(value: Uint8Array);
  1343. /* Excluded from this release type: validateHexString */
  1344. /** Returns the ObjectId id as a 24 lowercase character hex string representation */
  1345. toHexString(): string;
  1346. /* Excluded from this release type: getInc */
  1347. /**
  1348. * Generate a 12 byte id buffer used in ObjectId's
  1349. *
  1350. * @param time - pass in a second based timestamp.
  1351. */
  1352. static generate(time?: number): Uint8Array;
  1353. /**
  1354. * Converts the id into a 24 character hex string for printing, unless encoding is provided.
  1355. * @param encoding - hex or base64
  1356. */
  1357. toString(encoding?: 'hex' | 'base64'): string;
  1358. /** Converts to its JSON the 24 character hex string representation. */
  1359. toJSON(): string;
  1360. /* Excluded from this release type: is */
  1361. /**
  1362. * Compares the equality of this ObjectId with `otherID`.
  1363. *
  1364. * @param otherId - ObjectId instance to compare against.
  1365. */
  1366. equals(otherId: string | ObjectId | ObjectIdLike | undefined | null): boolean;
  1367. /** Returns the generation date (accurate up to the second) that this ID was generated. */
  1368. getTimestamp(): Date;
  1369. /* Excluded from this release type: createPk */
  1370. /* Excluded from this release type: serializeInto */
  1371. /**
  1372. * Creates an ObjectId from a second based number, with the rest of the ObjectId zeroed out. Used for comparisons or sorting the ObjectId.
  1373. *
  1374. * @param time - an integer number representing a number of seconds.
  1375. */
  1376. static createFromTime(time: number): ObjectId;
  1377. /**
  1378. * Creates an ObjectId from a hex string representation of an ObjectId.
  1379. *
  1380. * @param hexString - create a ObjectId from a passed in 24 character hexstring.
  1381. */
  1382. static createFromHexString(hexString: string): ObjectId;
  1383. /** Creates an ObjectId instance from a base64 string */
  1384. static createFromBase64(base64: string): ObjectId;
  1385. /**
  1386. * Checks if a value can be used to create a valid bson ObjectId
  1387. * @param id - any JS value
  1388. */
  1389. static isValid(id: string | ObjectId | ObjectIdLike | Uint8Array): boolean;
  1390. /* Excluded from this release type: toExtendedJSON */
  1391. /* Excluded from this release type: fromExtendedJSON */
  1392. /* Excluded from this release type: isCached */
  1393. /**
  1394. * Converts to a string representation of this Id.
  1395. *
  1396. * @returns return the 24 character hex string representation.
  1397. */
  1398. inspect(depth?: number, options?: unknown, inspect?: InspectFn): string;
  1399. }
  1400. /** @public */
  1401. export declare interface ObjectIdExtended {
  1402. $oid: string;
  1403. }
  1404. /** @public */
  1405. export declare interface ObjectIdLike {
  1406. id: string | Uint8Array;
  1407. __id?: string;
  1408. toHexString(): string;
  1409. }
  1410. /**
  1411. * @experimental
  1412. * @public
  1413. *
  1414. * A new set of BSON APIs that are currently experimental and not intended for production use.
  1415. */
  1416. export declare type OnDemand = {
  1417. parseToElements: (this: void, bytes: Uint8Array, startOffset?: number) => Iterable<BSONElement>;
  1418. BSONElement: BSONElement;
  1419. ByteUtils: ByteUtils;
  1420. NumberUtils: NumberUtils;
  1421. };
  1422. /**
  1423. * @experimental
  1424. * @public
  1425. */
  1426. export declare const onDemand: OnDemand;
  1427. /**
  1428. * Parse an Extended JSON string, constructing the JavaScript value or object described by that
  1429. * string.
  1430. *
  1431. * @example
  1432. * ```js
  1433. * const { EJSON } = require('bson');
  1434. * const text = '{ "int32": { "$numberInt": "10" } }';
  1435. *
  1436. * // prints { int32: { [String: '10'] _bsontype: 'Int32', value: '10' } }
  1437. * console.log(EJSON.parse(text, { relaxed: false }));
  1438. *
  1439. * // prints { int32: 10 }
  1440. * console.log(EJSON.parse(text));
  1441. * ```
  1442. */
  1443. declare function parse(text: string, options?: EJSONOptions): any;
  1444. /**
  1445. * Serialize a Javascript object.
  1446. *
  1447. * @param object - the Javascript object to serialize.
  1448. * @returns Buffer object containing the serialized object.
  1449. * @public
  1450. */
  1451. export declare function serialize(object: Document, options?: SerializeOptions): Uint8Array;
  1452. /** @public */
  1453. export declare interface SerializeOptions {
  1454. /**
  1455. * the serializer will check if keys are valid.
  1456. * @defaultValue `false`
  1457. */
  1458. checkKeys?: boolean;
  1459. /**
  1460. * serialize the javascript functions
  1461. * @defaultValue `false`
  1462. */
  1463. serializeFunctions?: boolean;
  1464. /**
  1465. * serialize will not emit undefined fields
  1466. * note that the driver sets this to `false`
  1467. * @defaultValue `true`
  1468. */
  1469. ignoreUndefined?: boolean;
  1470. /* Excluded from this release type: minInternalBufferSize */
  1471. /**
  1472. * the index in the buffer where we wish to start serializing into
  1473. * @defaultValue `0`
  1474. */
  1475. index?: number;
  1476. }
  1477. /**
  1478. * Serialize a Javascript object using a predefined Buffer and index into the buffer,
  1479. * useful when pre-allocating the space for serialization.
  1480. *
  1481. * @param object - the Javascript object to serialize.
  1482. * @param finalBuffer - the Buffer you pre-allocated to store the serialized BSON object.
  1483. * @returns the index pointing to the last written byte in the buffer.
  1484. * @public
  1485. */
  1486. export declare function serializeWithBufferAndIndex(object: Document, finalBuffer: Uint8Array, options?: SerializeOptions): number;
  1487. /**
  1488. * Sets the size of the internal serialization buffer.
  1489. *
  1490. * @param size - The desired size for the internal serialization buffer in bytes
  1491. * @public
  1492. */
  1493. export declare function setInternalBufferSize(size: number): void;
  1494. /**
  1495. * Converts a BSON document to an Extended JSON string, optionally replacing values if a replacer
  1496. * function is specified or optionally including only the specified properties if a replacer array
  1497. * is specified.
  1498. *
  1499. * @param value - The value to convert to extended JSON
  1500. * @param replacer - A function that alters the behavior of the stringification process, or an array of String and Number objects that serve as a whitelist for selecting/filtering the properties of the value object to be included in the JSON string. If this value is null or not provided, all properties of the object are included in the resulting JSON string
  1501. * @param space - A String or Number object that's used to insert white space into the output JSON string for readability purposes.
  1502. * @param options - Optional settings
  1503. *
  1504. * @example
  1505. * ```js
  1506. * const { EJSON } = require('bson');
  1507. * const Int32 = require('mongodb').Int32;
  1508. * const doc = { int32: new Int32(10) };
  1509. *
  1510. * // prints '{"int32":{"$numberInt":"10"}}'
  1511. * console.log(EJSON.stringify(doc, { relaxed: false }));
  1512. *
  1513. * // prints '{"int32":10}'
  1514. * console.log(EJSON.stringify(doc));
  1515. * ```
  1516. */
  1517. declare function stringify(value: any, replacer?: (number | string)[] | ((this: any, key: string, value: any) => any) | EJSONOptions, space?: string | number, options?: EJSONOptions): string;
  1518. /**
  1519. * @public
  1520. * @category BSONType
  1521. *
  1522. * A special type for _internal_ MongoDB use and is **not** associated with the regular Date type.
  1523. */
  1524. export declare class Timestamp extends LongWithoutOverridesClass {
  1525. get _bsontype(): 'Timestamp';
  1526. get [bsonType](): 'Timestamp';
  1527. static readonly MAX_VALUE: Long;
  1528. /**
  1529. * An incrementing ordinal for operations within a given second.
  1530. */
  1531. get i(): number;
  1532. /**
  1533. * A `time_t` value measuring seconds since the Unix epoch
  1534. */
  1535. get t(): number;
  1536. /**
  1537. * @param int - A 64-bit bigint representing the Timestamp.
  1538. */
  1539. constructor(int: bigint);
  1540. /**
  1541. * @param long - A 64-bit Long representing the Timestamp.
  1542. */
  1543. constructor(long: Long);
  1544. /**
  1545. * @param value - A pair of two values indicating timestamp and increment.
  1546. */
  1547. constructor(value: {
  1548. t: number;
  1549. i: number;
  1550. });
  1551. toJSON(): {
  1552. $timestamp: string;
  1553. };
  1554. /** Returns a Timestamp represented by the given (32-bit) integer value. */
  1555. static fromInt(value: number): Timestamp;
  1556. /** Returns a Timestamp representing the given number value, provided that it is a finite number. Otherwise, zero is returned. */
  1557. static fromNumber(value: number): Timestamp;
  1558. /**
  1559. * Returns a Timestamp for the given high and low bits. Each is assumed to use 32 bits.
  1560. *
  1561. * @param lowBits - the low 32-bits.
  1562. * @param highBits - the high 32-bits.
  1563. */
  1564. static fromBits(lowBits: number, highBits: number): Timestamp;
  1565. /**
  1566. * Returns a Timestamp from the given string, optionally using the given radix.
  1567. *
  1568. * @param str - the textual representation of the Timestamp.
  1569. * @param optRadix - the radix in which the text is written.
  1570. */
  1571. static fromString(str: string, optRadix: number): Timestamp;
  1572. /* Excluded from this release type: toExtendedJSON */
  1573. /* Excluded from this release type: fromExtendedJSON */
  1574. inspect(depth?: number, options?: unknown, inspect?: InspectFn): string;
  1575. }
  1576. /** @public */
  1577. export declare interface TimestampExtended {
  1578. $timestamp: {
  1579. t: number;
  1580. i: number;
  1581. };
  1582. }
  1583. /** @public */
  1584. export declare type TimestampOverrides = '_bsontype' | 'toExtendedJSON' | 'fromExtendedJSON' | 'inspect' | typeof bsonType;
  1585. /**
  1586. * A class representation of the BSON UUID type.
  1587. * @public
  1588. */
  1589. export declare class UUID extends Binary {
  1590. /**
  1591. * Create a UUID type
  1592. *
  1593. * When the argument to the constructor is omitted a random v4 UUID will be generated.
  1594. *
  1595. * @param input - Can be a 32 or 36 character hex string (dashes excluded/included) or a 16 byte binary Buffer.
  1596. */
  1597. constructor(input?: string | Uint8Array | UUID);
  1598. /**
  1599. * The UUID bytes
  1600. * @readonly
  1601. */
  1602. get id(): Uint8Array;
  1603. set id(value: Uint8Array);
  1604. /**
  1605. * Returns the UUID id as a 32 or 36 character hex string representation, excluding/including dashes (defaults to 36 character dash separated)
  1606. * @param includeDashes - should the string exclude dash-separators.
  1607. */
  1608. toHexString(includeDashes?: boolean): string;
  1609. /**
  1610. * Converts the id into a 36 character (dashes included) hex string, unless a encoding is specified.
  1611. */
  1612. toString(encoding?: 'hex' | 'base64'): string;
  1613. /**
  1614. * Converts the id into its JSON string representation.
  1615. * A 36 character (dashes included) hex string in the format: xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx
  1616. */
  1617. toJSON(): string;
  1618. /**
  1619. * Compares the equality of this UUID with `otherID`.
  1620. *
  1621. * @param otherId - UUID instance to compare against.
  1622. */
  1623. equals(otherId: string | Uint8Array | UUID): boolean;
  1624. /**
  1625. * Creates a Binary instance from the current UUID.
  1626. */
  1627. toBinary(): Binary;
  1628. /**
  1629. * Generates a populated buffer containing a v4 uuid
  1630. */
  1631. static generate(): Uint8Array;
  1632. /**
  1633. * Checks if a value is a valid bson UUID
  1634. * @param input - UUID, string or Buffer to validate.
  1635. */
  1636. static isValid(input: string | Uint8Array | UUID | Binary): boolean;
  1637. /**
  1638. * Creates an UUID from a hex string representation of an UUID.
  1639. * @param hexString - 32 or 36 character hex string (dashes excluded/included).
  1640. */
  1641. static createFromHexString(hexString: string): UUID;
  1642. /** Creates an UUID from a base64 string representation of an UUID. */
  1643. static createFromBase64(base64: string): UUID;
  1644. /* Excluded from this release type: bytesFromString */
  1645. /* Excluded from this release type: isValidUUIDString */
  1646. /**
  1647. * Converts to a string representation of this Id.
  1648. *
  1649. * @returns return the 36 character hex string representation.
  1650. *
  1651. */
  1652. inspect(depth?: number, options?: unknown, inspect?: InspectFn): string;
  1653. }
  1654. /** @public */
  1655. export declare type UUIDExtended = {
  1656. $uuid: string;
  1657. };
  1658. export { }