Skip to content

Commit 917668c

Browse files
W-A-Jamesnbbeekenbaileympearson
authored
feat(NODE-4877): Add support for useBigInt64 (#3519)
Co-authored-by: Neal Beeken <[email protected]> Co-authored-by: Bailey Pearson <[email protected]>
1 parent 10146a4 commit 917668c

File tree

22 files changed

+670
-378
lines changed

22 files changed

+670
-378
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,3 +87,4 @@ etc/docs/build
8787
!docs/**/*.png
8888
!docs/**/*.css
8989
!docs/**/*.js
90+
.nvmrc

package-lock.json

Lines changed: 7 additions & 7 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
"email": "[email protected]"
2626
},
2727
"dependencies": {
28-
"bson": "^5.0.0",
28+
"bson": "^5.0.1",
2929
"mongodb-connection-string-url": "^2.6.0",
3030
"socks": "^2.7.1"
3131
},

src/bson.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,6 @@ export interface BSONSerializeOptions
3636
| 'allowObjectSmallerThanBufferSize'
3737
| 'index'
3838
| 'validation'
39-
| 'useBigInt64'
4039
> {
4140
/**
4241
* Enabling the raw option will return a [Node.js Buffer](https://nodejs.org/api/buffer.html)
@@ -67,6 +66,7 @@ export interface BSONSerializeOptions
6766
export function pluckBSONSerializeOptions(options: BSONSerializeOptions): BSONSerializeOptions {
6867
const {
6968
fieldsAsRaw,
69+
useBigInt64,
7070
promoteValues,
7171
promoteBuffers,
7272
promoteLongs,
@@ -78,6 +78,7 @@ export function pluckBSONSerializeOptions(options: BSONSerializeOptions): BSONSe
7878
} = options;
7979
return {
8080
fieldsAsRaw,
81+
useBigInt64,
8182
promoteValues,
8283
promoteBuffers,
8384
promoteLongs,
@@ -102,6 +103,7 @@ export function resolveBSONOptions(
102103
const parentOptions = parent?.bsonOptions;
103104
return {
104105
raw: options?.raw ?? parentOptions?.raw ?? false,
106+
useBigInt64: options?.useBigInt64 ?? parentOptions?.useBigInt64 ?? false,
105107
promoteLongs: options?.promoteLongs ?? parentOptions?.promoteLongs ?? true,
106108
promoteValues: options?.promoteValues ?? parentOptions?.promoteValues ?? true,
107109
promoteBuffers: options?.promoteBuffers ?? parentOptions?.promoteBuffers ?? false,

src/cmap/auth/mongodb_aws.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ const AWS_RELATIVE_URI = 'http://169.254.170.2';
2121
const AWS_EC2_URI = 'http://169.254.169.254';
2222
const AWS_EC2_PATH = '/latest/meta-data/iam/security-credentials';
2323
const bsonOptions: BSONSerializeOptions = {
24+
useBigInt64: false,
2425
promoteLongs: true,
2526
promoteValues: true,
2627
promoteBuffers: false,

src/cmap/commands.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -304,6 +304,7 @@ export class Response {
304304
queryFailure?: boolean;
305305
shardConfigStale?: boolean;
306306
awaitCapable?: boolean;
307+
useBigInt64: boolean;
307308
promoteLongs: boolean;
308309
promoteValues: boolean;
309310
promoteBuffers: boolean;
@@ -320,6 +321,7 @@ export class Response {
320321
this.raw = message;
321322
this.data = msgBody;
322323
this.opts = opts ?? {
324+
useBigInt64: false,
323325
promoteLongs: true,
324326
promoteValues: true,
325327
promoteBuffers: false,
@@ -334,6 +336,7 @@ export class Response {
334336
this.fromCompressed = msgHeader.fromCompressed;
335337

336338
// Flag values
339+
this.useBigInt64 = typeof this.opts.useBigInt64 === 'boolean' ? this.opts.useBigInt64 : false;
337340
this.promoteLongs = typeof this.opts.promoteLongs === 'boolean' ? this.opts.promoteLongs : true;
338341
this.promoteValues =
339342
typeof this.opts.promoteValues === 'boolean' ? this.opts.promoteValues : true;
@@ -354,6 +357,7 @@ export class Response {
354357
// Allow the return of raw documents instead of parsing
355358
const raw = options.raw || false;
356359
const documentsReturnedIn = options.documentsReturnedIn || null;
360+
const useBigInt64 = options.useBigInt64 ?? this.opts.useBigInt64;
357361
const promoteLongs = options.promoteLongs ?? this.opts.promoteLongs;
358362
const promoteValues = options.promoteValues ?? this.opts.promoteValues;
359363
const promoteBuffers = options.promoteBuffers ?? this.opts.promoteBuffers;
@@ -362,6 +366,7 @@ export class Response {
362366

363367
// Set up the options
364368
const _options: BSONSerializeOptions = {
369+
useBigInt64,
365370
promoteLongs,
366371
promoteValues,
367372
promoteBuffers,
@@ -590,6 +595,7 @@ export class BinMsg {
590595
checksumPresent: boolean;
591596
moreToCome: boolean;
592597
exhaustAllowed: boolean;
598+
useBigInt64: boolean;
593599
promoteLongs: boolean;
594600
promoteValues: boolean;
595601
promoteBuffers: boolean;
@@ -607,6 +613,7 @@ export class BinMsg {
607613
this.raw = message;
608614
this.data = msgBody;
609615
this.opts = opts ?? {
616+
useBigInt64: false,
610617
promoteLongs: true,
611618
promoteValues: true,
612619
promoteBuffers: false,
@@ -625,6 +632,7 @@ export class BinMsg {
625632
this.checksumPresent = (this.responseFlags & OPTS_CHECKSUM_PRESENT) !== 0;
626633
this.moreToCome = (this.responseFlags & OPTS_MORE_TO_COME) !== 0;
627634
this.exhaustAllowed = (this.responseFlags & OPTS_EXHAUST_ALLOWED) !== 0;
635+
this.useBigInt64 = typeof this.opts.useBigInt64 === 'boolean' ? this.opts.useBigInt64 : false;
628636
this.promoteLongs = typeof this.opts.promoteLongs === 'boolean' ? this.opts.promoteLongs : true;
629637
this.promoteValues =
630638
typeof this.opts.promoteValues === 'boolean' ? this.opts.promoteValues : true;
@@ -648,6 +656,7 @@ export class BinMsg {
648656
// Allow the return of raw documents instead of parsing
649657
const raw = options.raw || false;
650658
const documentsReturnedIn = options.documentsReturnedIn || null;
659+
const useBigInt64 = options.useBigInt64 ?? this.opts.useBigInt64;
651660
const promoteLongs = options.promoteLongs ?? this.opts.promoteLongs;
652661
const promoteValues = options.promoteValues ?? this.opts.promoteValues;
653662
const promoteBuffers = options.promoteBuffers ?? this.opts.promoteBuffers;
@@ -656,6 +665,7 @@ export class BinMsg {
656665

657666
// Set up the options
658667
const bsonOptions: BSONSerializeOptions = {
668+
useBigInt64,
659669
promoteLongs,
660670
promoteValues,
661671
promoteBuffers,

src/cmap/connection.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -661,6 +661,7 @@ function write(
661661
command: !!options.command,
662662

663663
// for BSON parsing
664+
useBigInt64: typeof options.useBigInt64 === 'boolean' ? options.useBigInt64 : false,
664665
promoteLongs: typeof options.promoteLongs === 'boolean' ? options.promoteLongs : true,
665666
promoteValues: typeof options.promoteValues === 'boolean' ? options.promoteValues : true,
666667
promoteBuffers: typeof options.promoteBuffers === 'boolean' ? options.promoteBuffers : false,

src/connection_string.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -255,6 +255,15 @@ export function parseOptions(
255255
mongoClient = undefined;
256256
}
257257

258+
// validate BSONOptions
259+
if (options.useBigInt64 && typeof options.promoteLongs === 'boolean' && !options.promoteLongs) {
260+
throw new MongoAPIError('Must request either bigint or Long for int64 deserialization');
261+
}
262+
263+
if (options.useBigInt64 && typeof options.promoteValues === 'boolean' && !options.promoteValues) {
264+
throw new MongoAPIError('Must request either bigint or Long for int64 deserialization');
265+
}
266+
258267
const url = new ConnectionString(uri);
259268
const { hosts, isSRV } = url;
260269

@@ -955,6 +964,9 @@ export const OPTIONS = {
955964
promoteValues: {
956965
type: 'boolean'
957966
},
967+
useBigInt64: {
968+
type: 'boolean'
969+
},
958970
proxyHost: {
959971
type: 'string'
960972
},

src/cursor/abstract_cursor.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -642,6 +642,8 @@ export abstract class AbstractCursor<
642642
this[kId] =
643643
typeof response.cursor.id === 'number'
644644
? Long.fromNumber(response.cursor.id)
645+
: typeof response.cursor.id === 'bigint'
646+
? Long.fromBigInt(response.cursor.id)
645647
: response.cursor.id;
646648

647649
if (response.cursor.ns) {
@@ -741,6 +743,8 @@ export function next<T>(
741743
const cursorId =
742744
typeof response.cursor.id === 'number'
743745
? Long.fromNumber(response.cursor.id)
746+
: typeof response.cursor.id === 'bigint'
747+
? Long.fromBigInt(response.cursor.id)
744748
: response.cursor.id;
745749

746750
cursor[kDocuments].pushMany(response.cursor.nextBatch);

src/db.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ const DB_OPTIONS_ALLOW_LIST = [
5757
'readConcern',
5858
'retryMiliSeconds',
5959
'numberOfRetries',
60+
'useBigInt64',
6061
'promoteBuffers',
6162
'promoteLongs',
6263
'bsonRegExp',

0 commit comments

Comments
 (0)