@@ -80,13 +80,10 @@ class Packer {
80
80
/**
81
81
* @constructor
82
82
* @param {Chunker } channel the chunker backed by a network channel.
83
- * @param {boolean } disableLosslessIntegers if this packer should convert all received integers to native JS numbers
84
- * (including native {@link Number} type or our own {@link Integer}) as native {@link Number}.
85
83
*/
86
- constructor ( channel , disableLosslessIntegers = false ) {
84
+ constructor ( channel ) {
87
85
this . _ch = channel ;
88
86
this . _byteArraysSupported = true ;
89
- this . _disableLosslessIntegers = disableLosslessIntegers ;
90
87
}
91
88
92
89
/**
@@ -107,7 +104,7 @@ class Packer {
107
104
} else if ( typeof ( x ) == "string" ) {
108
105
return ( ) => this . packString ( x , onError ) ;
109
106
} else if ( isInt ( x ) ) {
110
- return this . _packableInteger ( x , onError ) ;
107
+ return ( ) => this . packInteger ( x ) ;
111
108
} else if ( x instanceof Int8Array ) {
112
109
return ( ) => this . packBytes ( x , onError ) ;
113
110
} else if ( x instanceof Array ) {
@@ -150,28 +147,6 @@ class Packer {
150
147
}
151
148
}
152
149
153
- /**
154
- * Creates a packable function out of the provided {@link Integer} value.
155
- * @param {Integer } x the value to pack.
156
- * @param {function } onError the callback for the case when value cannot be packed.
157
- * @return {function }
158
- * @private
159
- */
160
- _packableInteger ( x , onError ) {
161
- if ( this . _disableLosslessIntegers ) {
162
- // pack Integer objects only when native numbers are not used, fail otherwise
163
- // Integer can't represent special values like Number.NEGATIVE_INFINITY
164
- // and should not be used at all when native numbers are enabled
165
- if ( onError ) {
166
- onError ( newError ( `Cannot pack Integer value ${ x } (${ JSON . stringify ( x ) } ) when native numbers are enabled. ` +
167
- `Please use native Number instead or disable native number support on the driver level.` ) ) ;
168
- }
169
- return ( ) => undefined ;
170
- } else {
171
- return ( ) => this . packInteger ( x ) ;
172
- }
173
- }
174
-
175
150
/**
176
151
* Packs a struct
177
152
* @param signature the signature of the struct
@@ -209,6 +184,7 @@ class Packer {
209
184
this . _ch . writeInt32 ( low ) ;
210
185
}
211
186
}
187
+
212
188
packFloat ( x ) {
213
189
this . _ch . writeUInt8 ( FLOAT_64 ) ;
214
190
this . _ch . writeFloat64 ( x ) ;
@@ -343,8 +319,7 @@ class Unpacker {
343
319
344
320
/**
345
321
* @constructor
346
- * @param {boolean } disableLosslessIntegers if this unpacker should convert all received integers to native JS numbers
347
- * (including native {@link Number} type or our own {@link Integer}) as native {@link Number}.
322
+ * @param {boolean } disableLosslessIntegers if this unpacker should convert all received integers to native JS numbers.
348
323
*/
349
324
constructor ( disableLosslessIntegers = false ) {
350
325
// Higher level layers can specify how to map structs to higher-level objects.
@@ -368,9 +343,12 @@ class Unpacker {
368
343
return boolean ;
369
344
}
370
345
371
- const number = this . _unpackNumber ( marker , buffer ) ;
372
- if ( number !== null ) {
373
- return number ;
346
+ const numberOrInteger = this . _unpackNumberOrInteger ( marker , buffer ) ;
347
+ if ( numberOrInteger !== null ) {
348
+ if ( this . _disableLosslessIntegers && isInt ( numberOrInteger ) ) {
349
+ return numberOrInteger . toNumberOrInfinity ( ) ;
350
+ }
351
+ return numberOrInteger ;
374
352
}
375
353
376
354
const string = this . _unpackString ( marker , markerHigh , markerLow , buffer ) ;
@@ -411,7 +389,7 @@ class Unpacker {
411
389
}
412
390
}
413
391
414
- _unpackNumber ( marker , buffer ) {
392
+ _unpackNumberOrInteger ( marker , buffer ) {
415
393
if ( marker == FLOAT_64 ) {
416
394
return buffer . readFloat64 ( ) ;
417
395
} else if ( marker >= 0 && marker < 128 ) {
@@ -428,8 +406,7 @@ class Unpacker {
428
406
} else if ( marker == INT_64 ) {
429
407
const high = buffer . readInt32 ( ) ;
430
408
const low = buffer . readInt32 ( ) ;
431
- const integer = new Integer ( low , high ) ;
432
- return this . _disableLosslessIntegers ? integer . toNumberOrInfinity ( ) : integer ;
409
+ return new Integer ( low , high ) ;
433
410
} else {
434
411
return null ;
435
412
}
0 commit comments