1
- 'use strict' ;
2
-
3
- const BSON = require ( '../register-bson' ) ;
1
+ import * as BSON from '../register-bson' ;
4
2
const EJSON = BSON . EJSON ;
5
- const vm = require ( ' vm') ;
3
+ import * as vm from 'node: vm';
6
4
7
5
// BSON types
8
6
const Binary = BSON . Binary ;
@@ -30,6 +28,7 @@ function getOldBSON() {
30
28
try {
31
29
// do a dynamic resolve to avoid exception when running browser tests
32
30
const file = require . resolve ( 'bson' ) ;
31
+ // eslint-disable-next-line @typescript-eslint/no-var-requires
33
32
const oldModule = require ( file ) . BSON ;
34
33
const funcs = new oldModule . BSON ( ) ;
35
34
oldModule . serialize = funcs . serialize ;
@@ -49,7 +48,7 @@ describe('Extended JSON', function () {
49
48
50
49
before ( function ( ) {
51
50
const buffer = Buffer . alloc ( 64 ) ;
52
- for ( var i = 0 ; i < buffer . length ; i ++ ) buffer [ i ] = i ;
51
+ for ( let i = 0 ; i < buffer . length ; i ++ ) buffer [ i ] = i ;
53
52
const date = new Date ( ) ;
54
53
date . setTime ( 1488372056737 ) ;
55
54
doc = {
@@ -80,15 +79,15 @@ describe('Extended JSON', function () {
80
79
81
80
it ( 'should correctly extend an existing mongodb module' , function ( ) {
82
81
// Serialize the document
83
- var json =
82
+ const json =
84
83
'{"_id":{"$numberInt":"100"},"gh":{"$numberInt":"1"},"binary":{"$binary":{"base64":"AAECAwQFBgcICQoLDA0ODxAREhMUFRYXGBkaGxwdHh8gISIjJCUmJygpKissLS4vMDEyMzQ1Njc4OTo7PD0+Pw==","subType":"00"}},"date":{"$date":{"$numberLong":"1488372056737"}},"code":{"$code":"function() {}","$scope":{"a":{"$numberInt":"1"}}},"dbRef":{"$ref":"tests","$id":{"$numberInt":"1"},"$db":"test"},"decimal":{"$numberDecimal":"100"},"double":{"$numberDouble":"10.1"},"int32":{"$numberInt":"10"},"long":{"$numberLong":"200"},"maxKey":{"$maxKey":1},"minKey":{"$minKey":1},"objectId":{"$oid":"111111111111111111111111"},"objectID":{"$oid":"111111111111111111111111"},"oldObjectID":{"$oid":"111111111111111111111111"},"regexp":{"$regularExpression":{"pattern":"hello world","options":"i"}},"symbol":{"$symbol":"symbol"},"timestamp":{"$timestamp":{"t":0,"i":1000}},"int32Number":{"$numberInt":"300"},"doubleNumber":{"$numberDouble":"200.2"},"longNumberIntFit":{"$numberLong":"7036874417766400"},"doubleNumberIntFit":{"$numberLong":"19007199250000000"}}' ;
85
84
86
85
expect ( json ) . to . equal ( EJSON . stringify ( doc , null , 0 , { relaxed : false } ) ) ;
87
86
} ) ;
88
87
89
88
it ( 'should correctly deserialize using the default relaxed mode' , function ( ) {
90
89
// Deserialize the document using non strict mode
91
- var doc1 = EJSON . parse ( EJSON . stringify ( doc , null , 0 ) ) ;
90
+ let doc1 = EJSON . parse ( EJSON . stringify ( doc , null , 0 ) ) ;
92
91
93
92
// Validate the values
94
93
expect ( 300 ) . to . equal ( doc1 . int32Number ) ;
@@ -108,23 +107,23 @@ describe('Extended JSON', function () {
108
107
109
108
it ( 'should correctly serialize, and deserialize using built-in BSON' , function ( ) {
110
109
// Create a doc
111
- var doc1 = {
110
+ const doc1 = {
112
111
int32 : new Int32 ( 10 )
113
112
} ;
114
113
115
114
// Serialize the document
116
- var text = EJSON . stringify ( doc1 , null , 0 , { relaxed : false } ) ;
115
+ const text = EJSON . stringify ( doc1 , null , 0 , { relaxed : false } ) ;
117
116
expect ( text ) . to . equal ( '{"int32":{"$numberInt":"10"}}' ) ;
118
117
119
118
// Deserialize the json in strict and non strict mode
120
- var doc2 = EJSON . parse ( text , { relaxed : false } ) ;
119
+ let doc2 = EJSON . parse ( text , { relaxed : false } ) ;
121
120
expect ( doc2 . int32 . _bsontype ) . to . equal ( 'Int32' ) ;
122
121
doc2 = EJSON . parse ( text ) ;
123
122
expect ( doc2 . int32 ) . to . equal ( 10 ) ;
124
123
} ) ;
125
124
126
125
it ( 'should correctly serialize bson types when they are values' , function ( ) {
127
- var serialized = EJSON . stringify ( new ObjectId ( '591801a468f9e7024b6235ea' ) , { relaxed : false } ) ;
126
+ let serialized = EJSON . stringify ( new ObjectId ( '591801a468f9e7024b6235ea' ) , { relaxed : false } ) ;
128
127
expect ( serialized ) . to . equal ( '{"$oid":"591801a468f9e7024b6235ea"}' ) ;
129
128
serialized = EJSON . stringify ( new ObjectID ( '591801a468f9e7024b6235ea' ) , { relaxed : false } ) ;
130
129
expect ( serialized ) . to . equal ( '{"$oid":"591801a468f9e7024b6235ea"}' ) ;
@@ -182,8 +181,8 @@ describe('Extended JSON', function () {
182
181
expect ( EJSON . parse ( 'null' ) ) . to . be . null ;
183
182
expect ( EJSON . parse ( '[null]' ) [ 0 ] ) . to . be . null ;
184
183
185
- var input = '{"result":[{"_id":{"$oid":"591801a468f9e7024b623939"},"emptyField":null}]}' ;
186
- var parsed = EJSON . parse ( input ) ;
184
+ const input = '{"result":[{"_id":{"$oid":"591801a468f9e7024b623939"},"emptyField":null}]}' ;
185
+ const parsed = EJSON . parse ( input ) ;
187
186
188
187
expect ( parsed ) . to . deep . equal ( {
189
188
result : [ { _id : new ObjectId ( '591801a468f9e7024b623939' ) , emptyField : null } ]
@@ -333,14 +332,14 @@ describe('Extended JSON', function () {
333
332
it ( 'should work for function-valued and array-valued replacer parameters' , function ( ) {
334
333
const doc = { a : new Int32 ( 10 ) , b : new Int32 ( 10 ) } ;
335
334
336
- var replacerArray = [ 'a' , '$numberInt' ] ;
337
- var serialized = EJSON . stringify ( doc , replacerArray , 0 , { relaxed : false } ) ;
335
+ const replacerArray = [ 'a' , '$numberInt' ] ;
336
+ let serialized = EJSON . stringify ( doc , replacerArray , 0 , { relaxed : false } ) ;
338
337
expect ( serialized ) . to . equal ( '{"a":{"$numberInt":"10"}}' ) ;
339
338
340
339
serialized = EJSON . stringify ( doc , replacerArray ) ;
341
340
expect ( serialized ) . to . equal ( '{"a":10}' ) ;
342
341
343
- var replacerFunc = function ( key , value ) {
342
+ const replacerFunc = function ( key , value ) {
344
343
return key === 'b' ? undefined : value ;
345
344
} ;
346
345
serialized = EJSON . stringify ( doc , replacerFunc , 0 , { relaxed : false } ) ;
@@ -351,11 +350,13 @@ describe('Extended JSON', function () {
351
350
} ) ;
352
351
353
352
if ( ! usingOldBSON ) {
354
- it . skip ( 'skipping 4.x/1.x interop tests' , ( ) => { } ) ;
353
+ it . skip ( 'skipping 4.x/1.x interop tests' , ( ) => {
354
+ // ignore
355
+ } ) ;
355
356
} else {
356
357
it ( 'should interoperate 4.x with 1.x versions of this library' , function ( ) {
357
358
const buffer = Buffer . alloc ( 64 ) ;
358
- for ( var i = 0 ; i < buffer . length ; i ++ ) {
359
+ for ( let i = 0 ; i < buffer . length ; i ++ ) {
359
360
buffer [ i ] = i ;
360
361
}
361
362
const [ oldBsonObject , newBsonObject ] = [ OldBSON , BSON ] . map ( bsonModule => {
@@ -453,7 +454,9 @@ describe('Extended JSON', function () {
453
454
// by mongodb-core, then remove this test case and uncomment the MinKey checks in the test case above
454
455
it ( 'should interop with MinKey 1.x and 4.x, except the case that #310 breaks' , function ( ) {
455
456
if ( ! usingOldBSON ) {
456
- it . skip ( 'interop tests' , ( ) => { } ) ;
457
+ it . skip ( 'interop tests' , ( ) => {
458
+ // ignore
459
+ } ) ;
457
460
return ;
458
461
}
459
462
@@ -515,7 +518,7 @@ describe('Extended JSON', function () {
515
518
const serialized = EJSON . stringify ( original ) ;
516
519
expect ( serialized ) . to . equal ( '{"__proto__":{"a":42}}' ) ;
517
520
const deserialized = EJSON . parse ( serialized ) ;
518
- expect ( deserialized ) . to . have . deep . ownPropertyDescriptor ( '__proto__' , {
521
+ expect ( deserialized ) . to . have . ownPropertyDescriptor ( '__proto__' , {
519
522
configurable : true ,
520
523
enumerable : true ,
521
524
writable : true ,
@@ -526,7 +529,8 @@ describe('Extended JSON', function () {
526
529
527
530
context ( 'circular references' , ( ) => {
528
531
it ( 'should throw a helpful error message for input with circular references' , function ( ) {
529
- const obj = {
532
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
533
+ const obj : any = {
530
534
some : {
531
535
property : {
532
536
array : [ ]
@@ -541,7 +545,8 @@ Converting circular structure to EJSON:
541
545
} ) ;
542
546
543
547
it ( 'should throw a helpful error message for input with circular references, one-level nested' , function ( ) {
544
- const obj = { } ;
548
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
549
+ const obj : any = { } ;
545
550
obj . obj = obj ;
546
551
expect ( ( ) => EJSON . serialize ( obj ) ) . to . throw ( `\
547
552
Converting circular structure to EJSON:
@@ -550,7 +555,8 @@ Converting circular structure to EJSON:
550
555
} ) ;
551
556
552
557
it ( 'should throw a helpful error message for input with circular references, one-level nested inside base object' , function ( ) {
553
- const obj = { } ;
558
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
559
+ const obj : any = { } ;
554
560
obj . obj = obj ;
555
561
expect ( ( ) => EJSON . serialize ( { foo : obj } ) ) . to . throw ( `\
556
562
Converting circular structure to EJSON:
@@ -559,7 +565,8 @@ Converting circular structure to EJSON:
559
565
} ) ;
560
566
561
567
it ( 'should throw a helpful error message for input with circular references, pointing back to base object' , function ( ) {
562
- const obj = { foo : { } } ;
568
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
569
+ const obj : any = { foo : { } } ;
563
570
obj . foo . obj = obj ;
564
571
expect ( ( ) => EJSON . serialize ( obj ) ) . to . throw ( `\
565
572
Converting circular structure to EJSON:
@@ -784,4 +791,12 @@ Converting circular structure to EJSON:
784
791
expect ( parsedUUID ) . to . deep . equal ( expectedResult ) ;
785
792
} ) ;
786
793
} ) ;
794
+
795
+ it ( 'should only enumerate own property keys from input objects' , ( ) => {
796
+ const input = { a : 1 } ;
797
+ Object . setPrototypeOf ( input , { b : 2 } ) ;
798
+ const string = EJSON . stringify ( input ) ;
799
+ expect ( string ) . to . include ( `"a":` ) ;
800
+ expect ( string ) . to . not . include ( `"b":` ) ;
801
+ } ) ;
787
802
} ) ;
0 commit comments