From e91e8917b937e0a991a185751e75e8992773c793 Mon Sep 17 00:00:00 2001 From: Antonio Barcelos Date: Wed, 13 Jan 2021 17:59:31 +0100 Subject: [PATCH 1/4] Remove circular dependencies --- src/internal/packstream-v2.js | 9 +- src/internal/temporal-factory.js | 137 +++++++++++++++++++++++ src/internal/temporal-util.js | 148 ++++--------------------- test/internal/temporal-factory.test.js | 92 +++++++++++++++ test/internal/temporal-util.test.js | 40 ------- 5 files changed, 255 insertions(+), 171 deletions(-) create mode 100644 src/internal/temporal-factory.js create mode 100644 test/internal/temporal-factory.test.js diff --git a/src/internal/packstream-v2.js b/src/internal/packstream-v2.js index 05ab8f33a..cc0b852d1 100644 --- a/src/internal/packstream-v2.js +++ b/src/internal/packstream-v2.js @@ -33,12 +33,15 @@ import { import { int, isInt } from '../integer' import { dateToEpochDay, + localDateTimeToEpochSecond, + localTimeToNanoOfDay +} from './temporal-util' + +import { epochDayToDate, epochSecondAndNanoToLocalDateTime, - localDateTimeToEpochSecond, - localTimeToNanoOfDay, nanoOfDayToLocalTime -} from './temporal-util' +} from './temporal-factory' const POINT_2D = 0x58 const POINT_2D_STRUCT_SIZE = 3 diff --git a/src/internal/temporal-factory.js b/src/internal/temporal-factory.js new file mode 100644 index 000000000..154f36da3 --- /dev/null +++ b/src/internal/temporal-factory.js @@ -0,0 +1,137 @@ +/** + * Copyright (c) 2002-2020 "Neo4j," + * Neo4j Sweden AB [http://neo4j.com] + * + * This file is part of Neo4j. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +import { Date, LocalDateTime, LocalTime } from '../temporal-types' +import { int } from '../integer' +import { + DAYS_0000_TO_1970, + DAYS_PER_400_YEAR_CYCLE, + NANOS_PER_HOUR, + NANOS_PER_MINUTE, + NANOS_PER_SECOND, + SECONDS_PER_DAY, + floorDiv, + floorMod +} from './temporal-util' + +/** + * Converts given epoch day to a local date. + * @param {Integer|number|string} epochDay the epoch day to convert. + * @return {Date} the date representing the epoch day in years, months and days. + */ +export function epochDayToDate (epochDay) { + epochDay = int(epochDay) + + let zeroDay = epochDay.add(DAYS_0000_TO_1970).subtract(60) + let adjust = int(0) + if (zeroDay.lessThan(0)) { + const adjustCycles = zeroDay + .add(1) + .div(DAYS_PER_400_YEAR_CYCLE) + .subtract(1) + adjust = adjustCycles.multiply(400) + zeroDay = zeroDay.add(adjustCycles.multiply(-DAYS_PER_400_YEAR_CYCLE)) + } + let year = zeroDay + .multiply(400) + .add(591) + .div(DAYS_PER_400_YEAR_CYCLE) + let dayOfYearEst = zeroDay.subtract( + year + .multiply(365) + .add(year.div(4)) + .subtract(year.div(100)) + .add(year.div(400)) + ) + if (dayOfYearEst.lessThan(0)) { + year = year.subtract(1) + dayOfYearEst = zeroDay.subtract( + year + .multiply(365) + .add(year.div(4)) + .subtract(year.div(100)) + .add(year.div(400)) + ) + } + year = year.add(adjust) + const marchDayOfYear = dayOfYearEst + + const marchMonth = marchDayOfYear + .multiply(5) + .add(2) + .div(153) + const month = marchMonth + .add(2) + .modulo(12) + .add(1) + const day = marchDayOfYear + .subtract( + marchMonth + .multiply(306) + .add(5) + .div(10) + ) + .add(1) + year = year.add(marchMonth.div(10)) + + return new Date(year, month, day) +} + +/** + * Converts nanoseconds of the day into local time. + * @param {Integer|number|string} nanoOfDay the nanoseconds of the day to convert. + * @return {LocalTime} the local time representing given nanoseconds of the day. + */ +export function nanoOfDayToLocalTime (nanoOfDay) { + nanoOfDay = int(nanoOfDay) + + const hour = nanoOfDay.div(NANOS_PER_HOUR) + nanoOfDay = nanoOfDay.subtract(hour.multiply(NANOS_PER_HOUR)) + + const minute = nanoOfDay.div(NANOS_PER_MINUTE) + nanoOfDay = nanoOfDay.subtract(minute.multiply(NANOS_PER_MINUTE)) + + const second = nanoOfDay.div(NANOS_PER_SECOND) + const nanosecond = nanoOfDay.subtract(second.multiply(NANOS_PER_SECOND)) + + return new LocalTime(hour, minute, second, nanosecond) +} + +/** + * Converts given epoch second and nanosecond adjustment into a local date time object. + * @param {Integer|number|string} epochSecond the epoch second to use. + * @param {Integer|number|string} nano the nanosecond to use. + * @return {LocalDateTime} the local date time representing given epoch second and nano. + */ +export function epochSecondAndNanoToLocalDateTime (epochSecond, nano) { + const epochDay = floorDiv(epochSecond, SECONDS_PER_DAY) + const secondsOfDay = floorMod(epochSecond, SECONDS_PER_DAY) + const nanoOfDay = secondsOfDay.multiply(NANOS_PER_SECOND).add(nano) + + const localDate = epochDayToDate(epochDay) + const localTime = nanoOfDayToLocalTime(nanoOfDay) + return new LocalDateTime( + localDate.year, + localDate.month, + localDate.day, + localTime.hour, + localTime.minute, + localTime.second, + localTime.nanosecond + ) +} diff --git a/src/internal/temporal-util.js b/src/internal/temporal-util.js index a80c0be40..b33ed8f74 100644 --- a/src/internal/temporal-util.js +++ b/src/internal/temporal-util.js @@ -18,7 +18,6 @@ */ import { int, isInt } from '../integer' -import { Date, LocalDateTime, LocalTime } from '../temporal-types' import { assertNumberOrInteger } from './util' import { newError } from '../error' @@ -57,24 +56,24 @@ class ValueRange { } } -const YEAR_RANGE = new ValueRange(-999999999, 999999999) -const MONTH_OF_YEAR_RANGE = new ValueRange(1, 12) -const DAY_OF_MONTH_RANGE = new ValueRange(1, 31) -const HOUR_OF_DAY_RANGE = new ValueRange(0, 23) -const MINUTE_OF_HOUR_RANGE = new ValueRange(0, 59) -const SECOND_OF_MINUTE_RANGE = new ValueRange(0, 59) -const NANOSECOND_OF_SECOND_RANGE = new ValueRange(0, 999999999) - -const MINUTES_PER_HOUR = 60 -const SECONDS_PER_MINUTE = 60 -const SECONDS_PER_HOUR = SECONDS_PER_MINUTE * MINUTES_PER_HOUR -const NANOS_PER_SECOND = 1000000000 -const NANOS_PER_MILLISECOND = 1000000 -const NANOS_PER_MINUTE = NANOS_PER_SECOND * SECONDS_PER_MINUTE -const NANOS_PER_HOUR = NANOS_PER_MINUTE * MINUTES_PER_HOUR -const DAYS_0000_TO_1970 = 719528 -const DAYS_PER_400_YEAR_CYCLE = 146097 -const SECONDS_PER_DAY = 86400 +export const YEAR_RANGE = new ValueRange(-999999999, 999999999) +export const MONTH_OF_YEAR_RANGE = new ValueRange(1, 12) +export const DAY_OF_MONTH_RANGE = new ValueRange(1, 31) +export const HOUR_OF_DAY_RANGE = new ValueRange(0, 23) +export const MINUTE_OF_HOUR_RANGE = new ValueRange(0, 59) +export const SECOND_OF_MINUTE_RANGE = new ValueRange(0, 59) +export const NANOSECOND_OF_SECOND_RANGE = new ValueRange(0, 999999999) + +export const MINUTES_PER_HOUR = 60 +export const SECONDS_PER_MINUTE = 60 +export const SECONDS_PER_HOUR = SECONDS_PER_MINUTE * MINUTES_PER_HOUR +export const NANOS_PER_SECOND = 1000000000 +export const NANOS_PER_MILLISECOND = 1000000 +export const NANOS_PER_MINUTE = NANOS_PER_SECOND * SECONDS_PER_MINUTE +export const NANOS_PER_HOUR = NANOS_PER_MINUTE * MINUTES_PER_HOUR +export const DAYS_0000_TO_1970 = 719528 +export const DAYS_PER_400_YEAR_CYCLE = 146097 +export const SECONDS_PER_DAY = 86400 export function normalizeSecondsForDuration (seconds, nanoseconds) { return int(seconds).add(floorDiv(nanoseconds, NANOS_PER_SECOND)) @@ -104,26 +103,6 @@ export function localTimeToNanoOfDay (hour, minute, second, nanosecond) { return totalNanos.add(nanosecond) } -/** - * Converts nanoseconds of the day into local time. - * @param {Integer|number|string} nanoOfDay the nanoseconds of the day to convert. - * @return {LocalTime} the local time representing given nanoseconds of the day. - */ -export function nanoOfDayToLocalTime (nanoOfDay) { - nanoOfDay = int(nanoOfDay) - - const hour = nanoOfDay.div(NANOS_PER_HOUR) - nanoOfDay = nanoOfDay.subtract(hour.multiply(NANOS_PER_HOUR)) - - const minute = nanoOfDay.div(NANOS_PER_MINUTE) - nanoOfDay = nanoOfDay.subtract(minute.multiply(NANOS_PER_MINUTE)) - - const second = nanoOfDay.div(NANOS_PER_SECOND) - const nanosecond = nanoOfDay.subtract(second.multiply(NANOS_PER_SECOND)) - - return new LocalTime(hour, minute, second, nanosecond) -} - /** * Converts given local date time into a single integer representing this same time in epoch seconds UTC. * @param {Integer|number|string} year the year of the local date-time to convert. @@ -149,30 +128,6 @@ export function localDateTimeToEpochSecond ( return epochDay.multiply(SECONDS_PER_DAY).add(localTimeSeconds) } -/** - * Converts given epoch second and nanosecond adjustment into a local date time object. - * @param {Integer|number|string} epochSecond the epoch second to use. - * @param {Integer|number|string} nano the nanosecond to use. - * @return {LocalDateTime} the local date time representing given epoch second and nano. - */ -export function epochSecondAndNanoToLocalDateTime (epochSecond, nano) { - const epochDay = floorDiv(epochSecond, SECONDS_PER_DAY) - const secondsOfDay = floorMod(epochSecond, SECONDS_PER_DAY) - const nanoOfDay = secondsOfDay.multiply(NANOS_PER_SECOND).add(nano) - - const localDate = epochDayToDate(epochDay) - const localTime = nanoOfDayToLocalTime(nanoOfDay) - return new LocalDateTime( - localDate.year, - localDate.month, - localDate.day, - localTime.hour, - localTime.minute, - localTime.second, - localTime.nanosecond - ) -} - /** * Converts given local date into a single integer representing it's epoch day. * @param {Integer|number|string} year the year of the local date to convert. @@ -220,69 +175,6 @@ export function dateToEpochDay (year, month, day) { return epochDay.subtract(DAYS_0000_TO_1970) } -/** - * Converts given epoch day to a local date. - * @param {Integer|number|string} epochDay the epoch day to convert. - * @return {Date} the date representing the epoch day in years, months and days. - */ -export function epochDayToDate (epochDay) { - epochDay = int(epochDay) - - let zeroDay = epochDay.add(DAYS_0000_TO_1970).subtract(60) - let adjust = int(0) - if (zeroDay.lessThan(0)) { - const adjustCycles = zeroDay - .add(1) - .div(DAYS_PER_400_YEAR_CYCLE) - .subtract(1) - adjust = adjustCycles.multiply(400) - zeroDay = zeroDay.add(adjustCycles.multiply(-DAYS_PER_400_YEAR_CYCLE)) - } - let year = zeroDay - .multiply(400) - .add(591) - .div(DAYS_PER_400_YEAR_CYCLE) - let dayOfYearEst = zeroDay.subtract( - year - .multiply(365) - .add(year.div(4)) - .subtract(year.div(100)) - .add(year.div(400)) - ) - if (dayOfYearEst.lessThan(0)) { - year = year.subtract(1) - dayOfYearEst = zeroDay.subtract( - year - .multiply(365) - .add(year.div(4)) - .subtract(year.div(100)) - .add(year.div(400)) - ) - } - year = year.add(adjust) - const marchDayOfYear = dayOfYearEst - - const marchMonth = marchDayOfYear - .multiply(5) - .add(2) - .div(153) - const month = marchMonth - .add(2) - .modulo(12) - .add(1) - const day = marchDayOfYear - .subtract( - marchMonth - .multiply(306) - .add(5) - .div(10) - ) - .add(1) - year = year.add(marchMonth.div(10)) - - return new Date(year, month, day) -} - /** * Format given duration to an ISO 8601 string. * @param {Integer|number|string} months the number of months. @@ -529,7 +421,7 @@ function isLeapYear (year) { * @param {Integer|number|string} y the divisor. * @return {Integer} the result. */ -function floorDiv (x, y) { +export function floorDiv (x, y) { x = int(x) y = int(y) @@ -545,7 +437,7 @@ function floorDiv (x, y) { * @param {Integer|number|string} y the divisor. * @return {Integer} the result. */ -function floorMod (x, y) { +export function floorMod (x, y) { x = int(x) y = int(y) diff --git a/test/internal/temporal-factory.test.js b/test/internal/temporal-factory.test.js new file mode 100644 index 000000000..c1090436d --- /dev/null +++ b/test/internal/temporal-factory.test.js @@ -0,0 +1,92 @@ +/** + * Copyright (c) 2002-2020 "Neo4j," + * Neo4j Sweden AB [http://neo4j.com] + * + * This file is part of Neo4j. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import { int } from '../../src/integer' +import * as factory from '../../src/internal/temporal-factory' +import { types } from '../../src' + +describe('#unit temporal-factory', () => { + it('should convert epoch day to cypher date', () => { + expect(factory.epochDayToDate(-719528)).toEqual(date(0, 1, 1)) + expect(factory.epochDayToDate(-135153)).toEqual(date(1599, 12, 19)) + expect(factory.epochDayToDate(7905)).toEqual(date(1991, 8, 24)) + expect(factory.epochDayToDate(int(48210))).toEqual(date(2101, 12, 30)) + expect(factory.epochDayToDate(int(-4310226))).toEqual(date(-9831, 1, 1)) + }) + + it('should convert nanosecond of the day to cypher local time', () => { + expect(factory.nanoOfDayToLocalTime(68079000012399)).toEqual( + localTime(18, 54, 39, 12399) + ) + expect(factory.nanoOfDayToLocalTime(0)).toEqual(localTime(0, 0, 0, 0)) + expect(factory.nanoOfDayToLocalTime(1)).toEqual(localTime(0, 0, 0, 1)) + expect(factory.nanoOfDayToLocalTime(int(86399999999999))).toEqual( + localTime(23, 59, 59, 999999999) + ) + expect(factory.nanoOfDayToLocalTime(int(46277000808080))).toEqual( + localTime(12, 51, 17, 808080) + ) + }) + + it('should convert epoch second with nano to cypher local date-time', () => { + expect(factory.epochSecondAndNanoToLocalDateTime(653165977, 999)).toEqual( + localDateTime(1990, 9, 12, 18, 59, 37, 999) + ) + expect( + factory.epochSecondAndNanoToLocalDateTime(-62703676801, 12345) + ).toEqual(localDateTime(-18, 12, 31, 23, 59, 59, 12345)) + expect(factory.epochSecondAndNanoToLocalDateTime(2678400, int(1))).toEqual( + localDateTime(1970, 2, 1, 0, 0, 0, 1) + ) + expect( + factory.epochSecondAndNanoToLocalDateTime( + int(3065493882737), + int(1794673) + ) + ).toEqual(localDateTime(99111, 8, 21, 6, 32, 17, 1794673)) + expect( + factory.epochSecondAndNanoToLocalDateTime(int(-37428234001), 999999111) + ).toEqual(localDateTime(783, 12, 12, 20, 19, 59, 999999111)) + }) +}) + +function date (year, month, day) { + return new types.Date(int(year), int(month), int(day)) +} + +function localTime (hour, minute, second, nanosecond) { + return new types.LocalTime( + int(hour), + int(minute), + int(second), + int(nanosecond) + ) +} + +function localDateTime (year, month, day, hour, minute, second, nanosecond) { + return new types.LocalDateTime( + int(year), + int(month), + int(day), + int(hour), + int(minute), + int(second), + int(nanosecond) + ) +} diff --git a/test/internal/temporal-util.test.js b/test/internal/temporal-util.test.js index 1855fe0f5..cbcc181a3 100644 --- a/test/internal/temporal-util.test.js +++ b/test/internal/temporal-util.test.js @@ -165,14 +165,6 @@ describe('#unit temporal-util', () => { ) }) - it('should convert epoch day to cypher date', () => { - expect(util.epochDayToDate(-719528)).toEqual(date(0, 1, 1)) - expect(util.epochDayToDate(-135153)).toEqual(date(1599, 12, 19)) - expect(util.epochDayToDate(7905)).toEqual(date(1991, 8, 24)) - expect(util.epochDayToDate(int(48210))).toEqual(date(2101, 12, 30)) - expect(util.epochDayToDate(int(-4310226))).toEqual(date(-9831, 1, 1)) - }) - it('should convert cypher date to epoch day', () => { expect(util.dateToEpochDay(-13, 12, 31)).toEqual(int(-723912)) expect(util.dateToEpochDay(9, 9, 9)).toEqual(int(-715989)) @@ -181,24 +173,6 @@ describe('#unit temporal-util', () => { expect(util.dateToEpochDay(19999, 9, 28)).toEqual(int(6585227)) }) - it('should convert epoch second with nano to cypher local date-time', () => { - expect(util.epochSecondAndNanoToLocalDateTime(653165977, 999)).toEqual( - localDateTime(1990, 9, 12, 18, 59, 37, 999) - ) - expect(util.epochSecondAndNanoToLocalDateTime(-62703676801, 12345)).toEqual( - localDateTime(-18, 12, 31, 23, 59, 59, 12345) - ) - expect(util.epochSecondAndNanoToLocalDateTime(2678400, int(1))).toEqual( - localDateTime(1970, 2, 1, 0, 0, 0, 1) - ) - expect( - util.epochSecondAndNanoToLocalDateTime(int(3065493882737), int(1794673)) - ).toEqual(localDateTime(99111, 8, 21, 6, 32, 17, 1794673)) - expect( - util.epochSecondAndNanoToLocalDateTime(int(-37428234001), 999999111) - ).toEqual(localDateTime(783, 12, 12, 20, 19, 59, 999999111)) - }) - it('should convert cypher local date-time to epoch second', () => { expect( util.localDateTimeToEpochSecond(1990, 9, 12, 18, 59, 37, 999) @@ -217,20 +191,6 @@ describe('#unit temporal-util', () => { ).toEqual(int(-37428234001)) }) - it('should convert nanosecond of the day to cypher local time', () => { - expect(util.nanoOfDayToLocalTime(68079000012399)).toEqual( - localTime(18, 54, 39, 12399) - ) - expect(util.nanoOfDayToLocalTime(0)).toEqual(localTime(0, 0, 0, 0)) - expect(util.nanoOfDayToLocalTime(1)).toEqual(localTime(0, 0, 0, 1)) - expect(util.nanoOfDayToLocalTime(int(86399999999999))).toEqual( - localTime(23, 59, 59, 999999999) - ) - expect(util.nanoOfDayToLocalTime(int(46277000808080))).toEqual( - localTime(12, 51, 17, 808080) - ) - }) - it('should convert cypher local time to nanosecond of the day', () => { expect(util.localTimeToNanoOfDay(18, 54, 39, 12399)).toEqual( int(68079000012399) From 0e00d8912674554249a6701cce0a7d099047c500 Mon Sep 17 00:00:00 2001 From: Antonio Barcelos Date: Wed, 13 Jan 2021 19:01:56 +0100 Subject: [PATCH 2/4] Remove circular dependency between buf/combined-buf.js, node/index.js and node/node-utf8.js --- src/internal/node/node-utf8.js | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/src/internal/node/node-utf8.js b/src/internal/node/node-utf8.js index ba0485259..a036e2182 100644 --- a/src/internal/node/node-utf8.js +++ b/src/internal/node/node-utf8.js @@ -17,7 +17,6 @@ * limitations under the License. */ -import CombinedBuffer from '../buf/combined-buf' import NodeBuffer from './node-buf' import { newError } from '../../error' import node from 'buffer' @@ -30,9 +29,9 @@ function encode (str) { } function decode (buffer, length) { - if (buffer instanceof NodeBuffer) { + if (Object.prototype.hasOwnProperty.call(buffer, '_buffer')) { return decodeNodeBuffer(buffer, length) - } else if (buffer instanceof CombinedBuffer) { + } else if (Object.prototype.hasOwnProperty.call(buffer, '_buffers')) { return decodeCombinedBuffer(buffer, length) } else { throw newError(`Don't know how to decode strings from '${buffer}'`) From 21191a3f2717ec66e5119760e339d350da564ed9 Mon Sep 17 00:00:00 2001 From: Antonio Barcelos Date: Wed, 13 Jan 2021 19:07:21 +0100 Subject: [PATCH 3/4] remove circular dependency between driver.js and internal/connection-verifier.js --- src/internal/connectivity-verifier.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/internal/connectivity-verifier.js b/src/internal/connectivity-verifier.js index 911ebdaad..e2049618e 100644 --- a/src/internal/connectivity-verifier.js +++ b/src/internal/connectivity-verifier.js @@ -18,7 +18,7 @@ */ import ConnectionHolder from './connection-holder' -import { READ } from '../driver' +import { ACCESS_MODE_READ } from './constants' import { ResultStreamObserver } from './bolt' /** @@ -49,7 +49,7 @@ export default class ConnectivityVerifier { */ function acquireAndReleaseDummyConnection (connectionProvider, database) { const connectionHolder = new ConnectionHolder({ - mode: READ, + mode: ACCESS_MODE_READ, database, connectionProvider }) From a6c9fe0821b8a9bd7ca051716b40ebe860979d6e Mon Sep 17 00:00:00 2001 From: Antonio Barcelos Date: Wed, 13 Jan 2021 19:43:07 +0100 Subject: [PATCH 4/4] remove circular dependency between driver and session --- src/driver.js | 13 +------------ src/session.js | 14 ++++++++++++-- 2 files changed, 13 insertions(+), 14 deletions(-) diff --git a/src/driver.js b/src/driver.js index 360703319..6b81ae561 100644 --- a/src/driver.js +++ b/src/driver.js @@ -275,17 +275,6 @@ class Driver { }) } - /** - * @protected - */ - static _validateSessionMode (rawMode) { - const mode = rawMode || WRITE - if (mode !== ACCESS_MODE_READ && mode !== ACCESS_MODE_WRITE) { - throw newError('Illegal session mode ' + mode) - } - return mode - } - /** * @private */ @@ -296,7 +285,7 @@ class Driver { reactive, fetchSize }) { - const sessionMode = Driver._validateSessionMode(defaultAccessMode) + const sessionMode = Session._validateSessionMode(defaultAccessMode) const connectionProvider = this._getOrCreateConnectionProvider() const bookmark = bookmarkOrBookmarks ? new Bookmark(bookmarkOrBookmarks) diff --git a/src/session.js b/src/session.js index 42ecdb01b..447a928be 100644 --- a/src/session.js +++ b/src/session.js @@ -22,7 +22,6 @@ import Transaction from './transaction' import { newError } from './error' import { validateQueryAndParameters } from './internal/util' import ConnectionHolder from './internal/connection-holder' -import Driver from './driver' import { ACCESS_MODE_READ, ACCESS_MODE_WRITE } from './internal/constants' import TransactionExecutor from './internal/transaction-executor' import Bookmark from './internal/bookmark' @@ -205,7 +204,7 @@ class Session { ) } - const mode = Driver._validateSessionMode(accessMode) + const mode = Session._validateSessionMode(accessMode) const connectionHolder = this._connectionHolderWithMode(mode) connectionHolder.initializeConnection() this._hasTx = true @@ -323,6 +322,17 @@ class Session { _onCompleteCallback (meta) { this._updateBookmark(new Bookmark(meta.bookmark)) } + + /** + * @protected + */ + static _validateSessionMode (rawMode) { + const mode = rawMode || ACCESS_MODE_WRITE + if (mode !== ACCESS_MODE_READ && mode !== ACCESS_MODE_WRITE) { + throw newError('Illegal session mode ' + mode) + } + return mode + } } function _createTransactionExecutor (config) {