1- 'use strict' ;
2-
3- const BSON = require ( '../register-bson' ) ;
1+ import * as BSON from '../register-bson' ;
42const EJSON = BSON . EJSON ;
5- const vm = require ( ' vm') ;
3+ import * as vm from 'node: vm';
64
75// BSON types
86const Binary = BSON . Binary ;
@@ -30,6 +28,7 @@ function getOldBSON() {
3028 try {
3129 // do a dynamic resolve to avoid exception when running browser tests
3230 const file = require . resolve ( 'bson' ) ;
31+ // eslint-disable-next-line @typescript-eslint/no-var-requires
3332 const oldModule = require ( file ) . BSON ;
3433 const funcs = new oldModule . BSON ( ) ;
3534 oldModule . serialize = funcs . serialize ;
@@ -49,7 +48,7 @@ describe('Extended JSON', function () {
4948
5049 before ( function ( ) {
5150 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 ;
5352 const date = new Date ( ) ;
5453 date . setTime ( 1488372056737 ) ;
5554 doc = {
@@ -80,15 +79,15 @@ describe('Extended JSON', function () {
8079
8180 it ( 'should correctly extend an existing mongodb module' , function ( ) {
8281 // TODO(NODE-4377): doubleNumberIntFit should be a double not a $numberLong
83- var json =
82+ const json =
8483 '{"_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"}}' ;
8584
8685 expect ( json ) . to . equal ( EJSON . stringify ( doc , null , 0 , { relaxed : false } ) ) ;
8786 } ) ;
8887
8988 it ( 'should correctly deserialize using the default relaxed mode' , function ( ) {
9089 // 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 ) ) ;
9291
9392 // Validate the values
9493 expect ( 300 ) . to . equal ( doc1 . int32Number ) ;
@@ -109,23 +108,23 @@ describe('Extended JSON', function () {
109108
110109 it ( 'should correctly serialize, and deserialize using built-in BSON' , function ( ) {
111110 // Create a doc
112- var doc1 = {
111+ const doc1 = {
113112 int32 : new Int32 ( 10 )
114113 } ;
115114
116115 // Serialize the document
117- var text = EJSON . stringify ( doc1 , null , 0 , { relaxed : false } ) ;
116+ const text = EJSON . stringify ( doc1 , null , 0 , { relaxed : false } ) ;
118117 expect ( text ) . to . equal ( '{"int32":{"$numberInt":"10"}}' ) ;
119118
120119 // Deserialize the json in strict and non strict mode
121- var doc2 = EJSON . parse ( text , { relaxed : false } ) ;
120+ let doc2 = EJSON . parse ( text , { relaxed : false } ) ;
122121 expect ( doc2 . int32 . _bsontype ) . to . equal ( 'Int32' ) ;
123122 doc2 = EJSON . parse ( text ) ;
124123 expect ( doc2 . int32 ) . to . equal ( 10 ) ;
125124 } ) ;
126125
127126 it ( 'should correctly serialize bson types when they are values' , function ( ) {
128- var serialized = EJSON . stringify ( new ObjectId ( '591801a468f9e7024b6235ea' ) , { relaxed : false } ) ;
127+ let serialized = EJSON . stringify ( new ObjectId ( '591801a468f9e7024b6235ea' ) , { relaxed : false } ) ;
129128 expect ( serialized ) . to . equal ( '{"$oid":"591801a468f9e7024b6235ea"}' ) ;
130129 serialized = EJSON . stringify ( new ObjectID ( '591801a468f9e7024b6235ea' ) , { relaxed : false } ) ;
131130 expect ( serialized ) . to . equal ( '{"$oid":"591801a468f9e7024b6235ea"}' ) ;
@@ -183,8 +182,8 @@ describe('Extended JSON', function () {
183182 expect ( EJSON . parse ( 'null' ) ) . to . be . null ;
184183 expect ( EJSON . parse ( '[null]' ) [ 0 ] ) . to . be . null ;
185184
186- var input = '{"result":[{"_id":{"$oid":"591801a468f9e7024b623939"},"emptyField":null}]}' ;
187- var parsed = EJSON . parse ( input ) ;
185+ const input = '{"result":[{"_id":{"$oid":"591801a468f9e7024b623939"},"emptyField":null}]}' ;
186+ const parsed = EJSON . parse ( input ) ;
188187
189188 expect ( parsed ) . to . deep . equal ( {
190189 result : [ { _id : new ObjectId ( '591801a468f9e7024b623939' ) , emptyField : null } ]
@@ -334,14 +333,14 @@ describe('Extended JSON', function () {
334333 it ( 'should work for function-valued and array-valued replacer parameters' , function ( ) {
335334 const doc = { a : new Int32 ( 10 ) , b : new Int32 ( 10 ) } ;
336335
337- var replacerArray = [ 'a' , '$numberInt' ] ;
338- var serialized = EJSON . stringify ( doc , replacerArray , 0 , { relaxed : false } ) ;
336+ const replacerArray = [ 'a' , '$numberInt' ] ;
337+ let serialized = EJSON . stringify ( doc , replacerArray , 0 , { relaxed : false } ) ;
339338 expect ( serialized ) . to . equal ( '{"a":{"$numberInt":"10"}}' ) ;
340339
341340 serialized = EJSON . stringify ( doc , replacerArray ) ;
342341 expect ( serialized ) . to . equal ( '{"a":10}' ) ;
343342
344- var replacerFunc = function ( key , value ) {
343+ const replacerFunc = function ( key , value ) {
345344 return key === 'b' ? undefined : value ;
346345 } ;
347346 serialized = EJSON . stringify ( doc , replacerFunc , 0 , { relaxed : false } ) ;
@@ -352,11 +351,13 @@ describe('Extended JSON', function () {
352351 } ) ;
353352
354353 if ( ! usingOldBSON ) {
355- it . skip ( 'skipping 4.x/1.x interop tests' , ( ) => { } ) ;
354+ it . skip ( 'skipping 4.x/1.x interop tests' , ( ) => {
355+ // ignore
356+ } ) ;
356357 } else {
357358 it ( 'should interoperate 4.x with 1.x versions of this library' , function ( ) {
358359 const buffer = Buffer . alloc ( 64 ) ;
359- for ( var i = 0 ; i < buffer . length ; i ++ ) {
360+ for ( let i = 0 ; i < buffer . length ; i ++ ) {
360361 buffer [ i ] = i ;
361362 }
362363 const [ oldBsonObject , newBsonObject ] = [ OldBSON , BSON ] . map ( bsonModule => {
@@ -454,7 +455,9 @@ describe('Extended JSON', function () {
454455 // by mongodb-core, then remove this test case and uncomment the MinKey checks in the test case above
455456 it ( 'should interop with MinKey 1.x and 4.x, except the case that #310 breaks' , function ( ) {
456457 if ( ! usingOldBSON ) {
457- it . skip ( 'interop tests' , ( ) => { } ) ;
458+ it . skip ( 'interop tests' , ( ) => {
459+ // ignore
460+ } ) ;
458461 return ;
459462 }
460463
@@ -516,7 +519,7 @@ describe('Extended JSON', function () {
516519 const serialized = EJSON . stringify ( original ) ;
517520 expect ( serialized ) . to . equal ( '{"__proto__":{"a":42}}' ) ;
518521 const deserialized = EJSON . parse ( serialized ) ;
519- expect ( deserialized ) . to . have . deep . ownPropertyDescriptor ( '__proto__' , {
522+ expect ( deserialized ) . to . have . ownPropertyDescriptor ( '__proto__' , {
520523 configurable : true ,
521524 enumerable : true ,
522525 writable : true ,
@@ -527,7 +530,8 @@ describe('Extended JSON', function () {
527530
528531 context ( 'circular references' , ( ) => {
529532 it ( 'should throw a helpful error message for input with circular references' , function ( ) {
530- const obj = {
533+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
534+ const obj : any = {
531535 some : {
532536 property : {
533537 array : [ ]
@@ -542,7 +546,8 @@ Converting circular structure to EJSON:
542546 } ) ;
543547
544548 it ( 'should throw a helpful error message for input with circular references, one-level nested' , function ( ) {
545- const obj = { } ;
549+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
550+ const obj : any = { } ;
546551 obj . obj = obj ;
547552 expect ( ( ) => EJSON . serialize ( obj ) ) . to . throw ( `\
548553Converting circular structure to EJSON:
@@ -551,7 +556,8 @@ Converting circular structure to EJSON:
551556 } ) ;
552557
553558 it ( 'should throw a helpful error message for input with circular references, one-level nested inside base object' , function ( ) {
554- const obj = { } ;
559+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
560+ const obj : any = { } ;
555561 obj . obj = obj ;
556562 expect ( ( ) => EJSON . serialize ( { foo : obj } ) ) . to . throw ( `\
557563Converting circular structure to EJSON:
@@ -560,7 +566,8 @@ Converting circular structure to EJSON:
560566 } ) ;
561567
562568 it ( 'should throw a helpful error message for input with circular references, pointing back to base object' , function ( ) {
563- const obj = { foo : { } } ;
569+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
570+ const obj : any = { foo : { } } ;
564571 obj . foo . obj = obj ;
565572 expect ( ( ) => EJSON . serialize ( obj ) ) . to . throw ( `\
566573Converting circular structure to EJSON:
@@ -785,4 +792,13 @@ Converting circular structure to EJSON:
785792 expect ( parsedUUID ) . to . deep . equal ( expectedResult ) ;
786793 } ) ;
787794 } ) ;
795+
796+ it ( 'should only enumerate own property keys from input objects' , ( ) => {
797+ const input = { a : 1 } ;
798+ Object . setPrototypeOf ( input , { b : 2 } ) ;
799+ const string = EJSON . stringify ( input ) ;
800+ expect ( string ) . to . not . include ( `"b":` ) ;
801+ const result = JSON . parse ( string ) ;
802+ expect ( result ) . to . deep . equal ( { a : 1 } ) ;
803+ } ) ;
788804} ) ;
0 commit comments