Skip to content

Commit

Permalink
Moving creation of providers to the index.js
Browse files Browse the repository at this point in the history
  • Loading branch information
bigmontz committed Mar 8, 2021
1 parent 2ce5354 commit 402f3ea
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 128 deletions.
68 changes: 41 additions & 27 deletions src/driver.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,9 @@

import ConnectionProvider from './internal/connection-provider'
import Bookmark from './internal/bookmark'
import DirectConnectionProvider from './internal/connection-provider-direct'
import ConnectivityVerifier from './internal/connectivity-verifier'
import ConfiguredCustomResolver from './internal/resolver/configured-custom-resolver'

import {
ACCESS_MODE_READ,
ACCESS_MODE_WRITE,
Expand Down Expand Up @@ -74,20 +75,23 @@ class Driver {
* You should not be calling this directly, instead use {@link driver}.
* @constructor
* @protected
* @param {ServerAddress} address
* @param {string} userAgent
* @param {Object} authToken
* @param {Object} meta Metainformation about the driver
* @param {Object} config
* @param {function(id: number, config:Object, log:Logger, hostNameResolver: ConfiguredCustomResolver): ConnectionProvider } createConnectonProvider Creates the connection provider
*/
constructor (address, userAgent, authToken = {}, config = {}) {
constructor (
meta,
config = {},
createConnectonProvider = (id, config, log, hostNameResolver) => {}
) {
sanitizeConfig(config)
validateConfig(config)

this._id = idGenerator++
this._address = address
this._userAgent = userAgent
this._authToken = authToken
this._meta = meta
this._config = config
this._log = Logger.create(config)
this._createConnectionProvider = createConnectonProvider

/**
* Reference to the connection provider. Initialized lazily by {@link _getOrCreateConnectionProvider}.
Expand Down Expand Up @@ -144,7 +148,7 @@ class Driver {
* @returns {boolean}
*/
_supportsRouting () {
return false
return this._meta.routing
}

/**
Expand Down Expand Up @@ -259,24 +263,10 @@ class Driver {
*/
_afterConstruction () {
this._log.info(
`Direct driver ${this._id} created for server address ${this._address}`
`${this._meta.typename} driver ${this._id} created for server address ${this._meta.address}`
)
}

/**
* @protected
*/
_createConnectionProvider (address, userAgent, authToken) {
return new DirectConnectionProvider({
id: this._id,
config: this._config,
log: this._log,
address: address,
userAgent: userAgent,
authToken: authToken
})
}

/**
* @private
*/
Expand Down Expand Up @@ -309,16 +299,31 @@ class Driver {
_getOrCreateConnectionProvider () {
if (!this._connectionProvider) {
this._connectionProvider = this._createConnectionProvider(
this._address,
this._userAgent,
this._authToken
this._id,
this._config,
this._log,
createHostNameResolver(this._config)
)
}

return this._connectionProvider
}
}

/**
* @private
* @returns {Object} the given config.
*/
function validateConfig (config) {
const resolver = config.resolver
if (resolver && typeof resolver !== 'function') {
throw new TypeError(
`Configured resolver should be a function. Got: ${resolver}`
)
}
return config
}

/**
* @private
*/
Expand Down Expand Up @@ -371,6 +376,15 @@ function validateFetchSizeValue (rawValue, defaultWhenAbsent) {
}
}

/**
* @private
* @returns {ConfiguredCustomResolver} new custom resolver that wraps the passed-in resolver function.
* If resolved function is not specified, it defaults to an identity resolver.
*/
function createHostNameResolver (config) {
return new ConfiguredCustomResolver(config.resolver)
}

export { Driver, READ, WRITE }

export default Driver
61 changes: 41 additions & 20 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
* limitations under the License.
*/
import { Driver, READ, WRITE } from './driver'
import RoutingDriver from './routing-driver'
import VERSION from './version'
import urlUtil from './internal/url-util'
import ServerAddress from './internal/server-address'
Expand Down Expand Up @@ -55,6 +54,9 @@ import {
ResultSummary,
Result
} from 'neo4j-driver-core'
import { HostNameResolver } from './internal/node'
import DirectConnectionProvider from './internal/connection-provider-direct'
import RoutingConnectionProvider from './internal/connection-provider-routing'

const {
util: { ENCRYPTION_ON, ENCRYPTION_OFF, assertString, isEmptyObjectOrNull }
Expand Down Expand Up @@ -236,27 +238,46 @@ function driver (url, authToken, config = {}) {

// Use default user agent or user agent specified by user.
config.userAgent = config.userAgent || USER_AGENT
const address = ServerAddress.fromUrl(parsedUrl.hostAndPort)

if (routing) {
return new RoutingDriver(
ServerAddress.fromUrl(parsedUrl.hostAndPort),
parsedUrl.query,
config.userAgent,
authToken,
config
)
} else {
if (!isEmptyObjectOrNull(parsedUrl.query)) {
throw new Error(
`Parameters are not supported with none routed scheme. Given URL: '${url}'`
)
const meta = {
address,
typename: routing ? 'Routing' : 'Direct',
routing
}

return new Driver(meta, config, createConnectionProviderFunction())

function createConnectionProviderFunction () {
if (routing) {
return (id, config, log, hostNameResolver) =>
new RoutingConnectionProvider({
id,
config,
log,
hostNameResolver,
authToken,
address,
userAgent: config.userAgent,
routingContext: parsedUrl.query
})
} else {
if (!isEmptyObjectOrNull(parsedUrl.query)) {
throw new Error(
`Parameters are not supported with none routed scheme. Given URL: '${url}'`
)
}

return (id, config, log) =>
new DirectConnectionProvider({
id,
config,
log,
authToken,
address,
userAgent: config.userAgent
})
}
return new Driver(
ServerAddress.fromUrl(parsedUrl.hostAndPort),
config.userAgent,
authToken,
config
)
}
}

Expand Down
81 changes: 0 additions & 81 deletions src/routing-driver.js

This file was deleted.

0 comments on commit 402f3ea

Please sign in to comment.