Skip to content

Commit c3fb512

Browse files
committed
Renamed 'useNativeNumbers' to 'disableLosslessIntegers'
New name seems more descriptive and emphasizes that numbers returned from driver with this setting set to `true` might result in loss of precision.
1 parent 4cf5e71 commit c3fb512

File tree

6 files changed

+27
-35
lines changed

6 files changed

+27
-35
lines changed

src/v1/index.js

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -158,16 +158,17 @@ const USER_AGENT = "neo4j-javascript/" + VERSION;
158158
* // on the operating system level. Default value is 5000, which is 5 seconds.
159159
* connectionTimeout: 5000, // 5 seconds
160160
*
161-
* // Make this driver always return and accept native JavaScript number for integer values, instead of the
161+
* // Make this driver always return native JavaScript numbers for integer values, instead of the
162162
* // dedicated {@link Integer} class. Values that do not fit in native number bit range will be represented as
163-
* // <code>Number.NEGATIVE_INFINITY</code> or <code>Number.POSITIVE_INFINITY</code>. Driver will fail to encode
164-
* // {@link Integer} values passed as query parameters when this setting is set to <code>true</code>.
165-
* // <b>Warning:</b> It is not safe to enable this setting when JavaScript applications are not the only ones
163+
* // <code>Number.NEGATIVE_INFINITY</code> or <code>Number.POSITIVE_INFINITY</code>.
164+
* // <b>Warning:</b> It is not always safe to enable this setting when JavaScript applications are not the only ones
166165
* // interacting with the database. Stored numbers might in such case be not representable by native
167-
* // {@link Number} type and thus driver will return lossy values. For example, this might happen when data was
166+
* // {@link Number} type and thus driver will return lossy values. This might also happen when data was
168167
* // initially imported using neo4j import tool and contained numbers larger than
169168
* // <code>Number.MAX_SAFE_INTEGER</code>. Driver will then return positive infinity, which is lossy.
170-
* useNativeNumbers: false,
169+
* // Default value for this option is <code>false</code> because native JavaScript numbers might result
170+
* // in loss of precision in the general case.
171+
* disableLosslessIntegers: false,
171172
* }
172173
*
173174
* @param {string} url The URL for the Neo4j database, for instance "bolt://localhost"

src/v1/internal/connector.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -164,10 +164,10 @@ class Connection {
164164
* @constructor
165165
* @param {NodeChannel|WebSocketChannel} channel - channel with a 'write' function and a 'onmessage' callback property.
166166
* @param {string} url - the hostname and port to connect to.
167-
* @param {boolean} useNativeNumbers if this connection should treat/convert all received numbers
167+
* @param {boolean} disableLosslessIntegers if this connection should convert all received integers to native JS numbers
168168
* (including native {@link Number} type or our own {@link Integer}) as native {@link Number}.
169169
*/
170-
constructor(channel, url, useNativeNumbers = false) {
170+
constructor(channel, url, disableLosslessIntegers = false) {
171171
/**
172172
* An ordered queue of observers, each exchange response (zero or more
173173
* RECORD messages followed by a SUCCESS message) we recieve will be routed
@@ -181,8 +181,8 @@ class Connection {
181181
this._ch = channel;
182182
this._dechunker = new Dechunker();
183183
this._chunker = new Chunker( channel );
184-
this._packer = new Packer(this._chunker, useNativeNumbers);
185-
this._unpacker = new Unpacker(useNativeNumbers);
184+
this._packer = new Packer(this._chunker, disableLosslessIntegers);
185+
this._unpacker = new Unpacker(disableLosslessIntegers);
186186

187187
this._isHandlingFailure = false;
188188
this._currentFailure = null;
@@ -589,7 +589,7 @@ function connect(url, config = {}, connectionErrorCode = null) {
589589
const Ch = config.channel || Channel;
590590
const parsedUrl = urlUtil.parseBoltUrl(url);
591591
const channelConfig = new ChannelConfig(parsedUrl, config, connectionErrorCode);
592-
return new Connection(new Ch(channelConfig), parsedUrl.hostAndPort, config.useNativeNumbers);
592+
return new Connection(new Ch(channelConfig), parsedUrl.hostAndPort, config.disableLosslessIntegers);
593593
}
594594

595595
export {

src/v1/internal/packstream.js

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -80,13 +80,13 @@ class Packer {
8080
/**
8181
* @constructor
8282
* @param {Chunker} channel the chunker backed by a network channel.
83-
* @param {boolean} useNativeNumbers if this packer should treat/convert all received numbers
83+
* @param {boolean} disableLosslessIntegers if this packer should convert all received integers to native JS numbers
8484
* (including native {@link Number} type or our own {@link Integer}) as native {@link Number}.
8585
*/
86-
constructor(channel, useNativeNumbers = false) {
86+
constructor(channel, disableLosslessIntegers = false) {
8787
this._ch = channel;
8888
this._byteArraysSupported = true;
89-
this._useNativeNumbers = useNativeNumbers;
89+
this._disableLosslessIntegers = disableLosslessIntegers;
9090
}
9191

9292
/**
@@ -158,7 +158,7 @@ class Packer {
158158
* @private
159159
*/
160160
_packableInteger(x, onError) {
161-
if (this._useNativeNumbers) {
161+
if (this._disableLosslessIntegers) {
162162
// pack Integer objects only when native numbers are not used, fail otherwise
163163
// Integer can't represent special values like Number.NEGATIVE_INFINITY
164164
// and should not be used at all when native numbers are enabled
@@ -343,15 +343,15 @@ class Unpacker {
343343

344344
/**
345345
* @constructor
346-
* @param {boolean} useNativeNumbers if this unpacker should treat/convert all received numbers
346+
* @param {boolean} disableLosslessIntegers if this unpacker should convert all received integers to native JS numbers
347347
* (including native {@link Number} type or our own {@link Integer}) as native {@link Number}.
348348
*/
349-
constructor(useNativeNumbers = false) {
349+
constructor(disableLosslessIntegers = false) {
350350
// Higher level layers can specify how to map structs to higher-level objects.
351351
// If we receive a struct that has a signature that does not have a mapper,
352352
// we simply return a Structure object.
353353
this.structMappers = {};
354-
this._useNativeNumbers = useNativeNumbers;
354+
this._disableLosslessIntegers = disableLosslessIntegers;
355355
}
356356

357357
unpack(buffer) {
@@ -429,7 +429,7 @@ class Unpacker {
429429
const high = buffer.readInt32();
430430
const low = buffer.readInt32();
431431
const integer = new Integer(low, high);
432-
return this._useNativeNumbers ? integer.toNumberOrInfinity() : integer;
432+
return this._disableLosslessIntegers ? integer.toNumberOrInfinity() : integer;
433433
} else {
434434
return null;
435435
}

test/types/v1/driver.test.ts

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -17,16 +17,7 @@
1717
* limitations under the License.
1818
*/
1919

20-
import Driver, {
21-
AuthToken,
22-
Config,
23-
EncryptionLevel,
24-
LoadBalancingStrategy,
25-
READ,
26-
SessionMode,
27-
TrustStrategy,
28-
WRITE
29-
} from "../../../types/v1/driver";
20+
import Driver, {AuthToken, Config, EncryptionLevel, LoadBalancingStrategy, READ, SessionMode, TrustStrategy, WRITE} from "../../../types/v1/driver";
3021
import {Parameters} from "../../../types/v1/statement-runner";
3122
import Session from "../../../types/v1/session";
3223
import {Neo4jError} from "../../../types/v1/error";
@@ -61,7 +52,7 @@ const loadBalancingStrategy1: undefined | LoadBalancingStrategy = config.loadBal
6152
const loadBalancingStrategy2: undefined | string = config.loadBalancingStrategy;
6253
const maxConnectionLifetime: undefined | number = config.maxConnectionLifetime;
6354
const connectionTimeout: undefined | number = config.connectionTimeout;
64-
const useNativeNumbers: undefined | boolean = config.useNativeNumbers;
55+
const disableLosslessIntegers: undefined | boolean = config.disableLosslessIntegers;
6556

6657
const sessionMode: SessionMode = dummy;
6758
const sessionModeStr: string = sessionMode;

test/v1/driver.test.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -338,14 +338,14 @@ describe('driver', () => {
338338

339339
nativeNumbers.forEach(number => {
340340

341-
it(`should return native number ${number} when useNativeNumbers=true`, done => {
341+
it(`should return native number ${number} when disableLosslessIntegers=true`, done => {
342342
testNativeNumberInReturnedRecord(number, done);
343343
});
344344

345345
});
346346

347-
it('should fail to pack Integer when useNativeNumbers=true', done => {
348-
driver = neo4j.driver('bolt://localhost', sharedNeo4j.authToken, {useNativeNumbers: true});
347+
it('should fail to pack Integer when disableLosslessIntegers=true', done => {
348+
driver = neo4j.driver('bolt://localhost', sharedNeo4j.authToken, {disableLosslessIntegers: true});
349349
const session = driver.session();
350350

351351
session.run('RETURN $number', {number: neo4j.int(42)})
@@ -379,7 +379,7 @@ describe('driver', () => {
379379
}
380380

381381
function testNativeNumberInReturnedRecord(number, done) {
382-
driver = neo4j.driver('bolt://localhost', sharedNeo4j.authToken, {useNativeNumbers: true});
382+
driver = neo4j.driver('bolt://localhost', sharedNeo4j.authToken, {disableLosslessIntegers: true});
383383

384384
const session = driver.session();
385385
session.run('RETURN $number AS n0, $number AS n1', {number: number}).then(result => {

types/v1/driver.d.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ declare interface Config {
5454
loadBalancingStrategy?: LoadBalancingStrategy;
5555
maxConnectionLifetime?: number;
5656
connectionTimeout?: number;
57-
useNativeNumbers?: boolean;
57+
disableLosslessIntegers?: boolean;
5858
}
5959

6060
declare type SessionMode = "READ" | "WRITE";

0 commit comments

Comments
 (0)