|
19 | 19 |
|
20 | 20 | import ConnectionProvider from './internal/connection-provider' |
21 | 21 | import Bookmark from './internal/bookmark' |
22 | | -import DirectConnectionProvider from './internal/connection-provider-direct' |
23 | 22 | import ConnectivityVerifier from './internal/connectivity-verifier' |
| 23 | +import ConfiguredCustomResolver from './internal/resolver/configured-custom-resolver' |
| 24 | + |
24 | 25 | import { |
25 | 26 | ACCESS_MODE_READ, |
26 | 27 | ACCESS_MODE_WRITE, |
@@ -74,20 +75,23 @@ class Driver { |
74 | 75 | * You should not be calling this directly, instead use {@link driver}. |
75 | 76 | * @constructor |
76 | 77 | * @protected |
77 | | - * @param {ServerAddress} address |
78 | | - * @param {string} userAgent |
79 | | - * @param {Object} authToken |
| 78 | + * @param {Object} meta Metainformation about the driver |
80 | 79 | * @param {Object} config |
| 80 | + * @param {function(id: number, config:Object, log:Logger, hostNameResolver: ConfiguredCustomResolver): ConnectionProvider } createConnectonProvider Creates the connection provider |
81 | 81 | */ |
82 | | - constructor (address, userAgent, authToken = {}, config = {}) { |
| 82 | + constructor ( |
| 83 | + meta, |
| 84 | + config = {}, |
| 85 | + createConnectonProvider = (id, config, log, hostNameResolver) => {} |
| 86 | + ) { |
83 | 87 | sanitizeConfig(config) |
| 88 | + validateConfig(config) |
84 | 89 |
|
85 | 90 | this._id = idGenerator++ |
86 | | - this._address = address |
87 | | - this._userAgent = userAgent |
88 | | - this._authToken = authToken |
| 91 | + this._meta = meta |
89 | 92 | this._config = config |
90 | 93 | this._log = Logger.create(config) |
| 94 | + this._createConnectionProvider = createConnectonProvider |
91 | 95 |
|
92 | 96 | /** |
93 | 97 | * Reference to the connection provider. Initialized lazily by {@link _getOrCreateConnectionProvider}. |
@@ -144,7 +148,7 @@ class Driver { |
144 | 148 | * @returns {boolean} |
145 | 149 | */ |
146 | 150 | _supportsRouting () { |
147 | | - return false |
| 151 | + return this._meta.routing |
148 | 152 | } |
149 | 153 |
|
150 | 154 | /** |
@@ -259,24 +263,10 @@ class Driver { |
259 | 263 | */ |
260 | 264 | _afterConstruction () { |
261 | 265 | this._log.info( |
262 | | - `Direct driver ${this._id} created for server address ${this._address}` |
| 266 | + `${this._meta.typename} driver ${this._id} created for server address ${this._meta.address}` |
263 | 267 | ) |
264 | 268 | } |
265 | 269 |
|
266 | | - /** |
267 | | - * @protected |
268 | | - */ |
269 | | - _createConnectionProvider (address, userAgent, authToken) { |
270 | | - return new DirectConnectionProvider({ |
271 | | - id: this._id, |
272 | | - config: this._config, |
273 | | - log: this._log, |
274 | | - address: address, |
275 | | - userAgent: userAgent, |
276 | | - authToken: authToken |
277 | | - }) |
278 | | - } |
279 | | - |
280 | 270 | /** |
281 | 271 | * @private |
282 | 272 | */ |
@@ -309,16 +299,31 @@ class Driver { |
309 | 299 | _getOrCreateConnectionProvider () { |
310 | 300 | if (!this._connectionProvider) { |
311 | 301 | this._connectionProvider = this._createConnectionProvider( |
312 | | - this._address, |
313 | | - this._userAgent, |
314 | | - this._authToken |
| 302 | + this._id, |
| 303 | + this._config, |
| 304 | + this._log, |
| 305 | + createHostNameResolver(this._config) |
315 | 306 | ) |
316 | 307 | } |
317 | 308 |
|
318 | 309 | return this._connectionProvider |
319 | 310 | } |
320 | 311 | } |
321 | 312 |
|
| 313 | +/** |
| 314 | + * @private |
| 315 | + * @returns {Object} the given config. |
| 316 | + */ |
| 317 | +function validateConfig (config) { |
| 318 | + const resolver = config.resolver |
| 319 | + if (resolver && typeof resolver !== 'function') { |
| 320 | + throw new TypeError( |
| 321 | + `Configured resolver should be a function. Got: ${resolver}` |
| 322 | + ) |
| 323 | + } |
| 324 | + return config |
| 325 | +} |
| 326 | + |
322 | 327 | /** |
323 | 328 | * @private |
324 | 329 | */ |
@@ -371,6 +376,15 @@ function validateFetchSizeValue (rawValue, defaultWhenAbsent) { |
371 | 376 | } |
372 | 377 | } |
373 | 378 |
|
| 379 | +/** |
| 380 | + * @private |
| 381 | + * @returns {ConfiguredCustomResolver} new custom resolver that wraps the passed-in resolver function. |
| 382 | + * If resolved function is not specified, it defaults to an identity resolver. |
| 383 | + */ |
| 384 | +function createHostNameResolver (config) { |
| 385 | + return new ConfiguredCustomResolver(config.resolver) |
| 386 | +} |
| 387 | + |
374 | 388 | export { Driver, READ, WRITE } |
375 | 389 |
|
376 | 390 | export default Driver |
0 commit comments