Skip to content

Commit dd00124

Browse files
committed
JS driver v1.2.0: Checking in transpiled files for bower
1 parent f82e012 commit dd00124

File tree

9 files changed

+2147
-526
lines changed

9 files changed

+2147
-526
lines changed

lib/browser/neo4j-web.js

Lines changed: 268 additions & 53 deletions
Large diffs are not rendered by default.

lib/browser/neo4j-web.min.js

Lines changed: 12 additions & 11 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

lib/browser/neo4j-web.test.js

Lines changed: 1624 additions & 432 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

lib/v1/internal/connection-providers.js

Lines changed: 76 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,12 @@ var _rediscovery = require('./rediscovery');
4949

5050
var _rediscovery2 = _interopRequireDefault(_rediscovery);
5151

52+
var _features = require('./features');
53+
54+
var _features2 = _interopRequireDefault(_features);
55+
56+
var _hostNameResolvers = require('./host-name-resolvers');
57+
5258
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
5359

5460
/**
@@ -78,7 +84,7 @@ var ConnectionProvider = function () {
7884
(0, _createClass3.default)(ConnectionProvider, [{
7985
key: 'acquireConnection',
8086
value: function acquireConnection(mode) {
81-
throw new Error('Abstract method');
87+
throw new Error('Abstract function');
8288
}
8389
}, {
8490
key: '_withAdditionalOnErrorCallback',
@@ -128,10 +134,12 @@ var LoadBalancer = exports.LoadBalancer = function (_ConnectionProvider2) {
128134

129135
var _this2 = (0, _possibleConstructorReturn3.default)(this, (LoadBalancer.__proto__ || (0, _getPrototypeOf2.default)(LoadBalancer)).call(this));
130136

131-
_this2._routingTable = new _routingTable2.default(new _roundRobinArray2.default([address]));
137+
_this2._seedRouter = address;
138+
_this2._routingTable = new _routingTable2.default(new _roundRobinArray2.default([_this2._seedRouter]));
132139
_this2._rediscovery = new _rediscovery2.default();
133140
_this2._connectionPool = connectionPool;
134141
_this2._driverOnErrorCallback = driverOnErrorCallback;
142+
_this2._hostNameResolver = LoadBalancer._createHostNameResolver();
135143
return _this2;
136144
}
137145

@@ -188,7 +196,50 @@ var LoadBalancer = exports.LoadBalancer = function (_ConnectionProvider2) {
188196

189197
var knownRouters = currentRoutingTable.routers.toArray();
190198

191-
var refreshedTablePromise = knownRouters.reduce(function (refreshedTablePromise, currentRouter, currentIndex) {
199+
return this._fetchNewRoutingTable(knownRouters, currentRoutingTable).then(function (newRoutingTable) {
200+
if (LoadBalancer._isValidRoutingTable(newRoutingTable)) {
201+
// one of the known routers returned a valid routing table - use it
202+
return newRoutingTable;
203+
}
204+
205+
if (!newRoutingTable) {
206+
// returned routing table was undefined, this means a connection error happened and the last known
207+
// router did not return a valid routing table, so we need to forget it
208+
var lastRouterIndex = knownRouters.length - 1;
209+
LoadBalancer._forgetRouter(currentRoutingTable, knownRouters, lastRouterIndex);
210+
}
211+
212+
// none of the known routers returned a valid routing table - try to use seed router address for rediscovery
213+
return _this4._fetchNewRoutingTableUsingSeedRouterAddress(knownRouters, _this4._seedRouter);
214+
}).then(function (newRoutingTable) {
215+
if (LoadBalancer._isValidRoutingTable(newRoutingTable)) {
216+
_this4._updateRoutingTable(newRoutingTable);
217+
return newRoutingTable;
218+
}
219+
220+
// none of the existing routers returned valid routing table, throw exception
221+
throw (0, _error.newError)('Could not perform discovery. No routing servers available.', _error.SERVICE_UNAVAILABLE);
222+
});
223+
}
224+
}, {
225+
key: '_fetchNewRoutingTableUsingSeedRouterAddress',
226+
value: function _fetchNewRoutingTableUsingSeedRouterAddress(knownRouters, seedRouter) {
227+
var _this5 = this;
228+
229+
return this._hostNameResolver.resolve(seedRouter).then(function (resolvedRouterAddresses) {
230+
// filter out all addresses that we've already tried
231+
var newAddresses = resolvedRouterAddresses.filter(function (address) {
232+
return knownRouters.indexOf(address) < 0;
233+
});
234+
return _this5._fetchNewRoutingTable(newAddresses, null);
235+
});
236+
}
237+
}, {
238+
key: '_fetchNewRoutingTable',
239+
value: function _fetchNewRoutingTable(routerAddresses, routingTable) {
240+
var _this6 = this;
241+
242+
return routerAddresses.reduce(function (refreshedTablePromise, currentRouter, currentIndex) {
192243
return refreshedTablePromise.then(function (newRoutingTable) {
193244
if (newRoutingTable) {
194245
if (!newRoutingTable.writers.isEmpty()) {
@@ -199,28 +250,14 @@ var LoadBalancer = exports.LoadBalancer = function (_ConnectionProvider2) {
199250
// returned routing table was undefined, this means a connection error happened and we need to forget the
200251
// previous router and try the next one
201252
var previousRouterIndex = currentIndex - 1;
202-
_this4._forgetRouter(currentRoutingTable, knownRouters, previousRouterIndex);
253+
LoadBalancer._forgetRouter(routingTable, routerAddresses, previousRouterIndex);
203254
}
204255

205256
// try next router
206-
var session = _this4._createSessionForRediscovery(currentRouter);
207-
return _this4._rediscovery.lookupRoutingTableOnRouter(session, currentRouter);
257+
var session = _this6._createSessionForRediscovery(currentRouter);
258+
return _this6._rediscovery.lookupRoutingTableOnRouter(session, currentRouter);
208259
});
209260
}, _promise2.default.resolve(null));
210-
211-
return refreshedTablePromise.then(function (newRoutingTable) {
212-
if (newRoutingTable && !newRoutingTable.writers.isEmpty()) {
213-
_this4._updateRoutingTable(newRoutingTable);
214-
return newRoutingTable;
215-
}
216-
217-
// forget the last known router because it did not return a valid routing table
218-
var lastRouterIndex = knownRouters.length - 1;
219-
_this4._forgetRouter(currentRoutingTable, knownRouters, lastRouterIndex);
220-
221-
// none of the existing routers returned valid routing table, throw exception
222-
throw (0, _error.newError)('Could not perform discovery. No routing servers available.', _error.SERVICE_UNAVAILABLE);
223-
});
224261
}
225262
}, {
226263
key: '_createSessionForRediscovery',
@@ -233,27 +270,40 @@ var LoadBalancer = exports.LoadBalancer = function (_ConnectionProvider2) {
233270
}, {
234271
key: '_updateRoutingTable',
235272
value: function _updateRoutingTable(newRoutingTable) {
236-
var _this5 = this;
273+
var _this7 = this;
237274

238275
var currentRoutingTable = this._routingTable;
239276

240277
// close old connections to servers not present in the new routing table
241278
var staleServers = currentRoutingTable.serversDiff(newRoutingTable);
242279
staleServers.forEach(function (server) {
243-
return _this5._connectionPool.purge(server);
280+
return _this7._connectionPool.purge(server);
244281
});
245282

246283
// make this driver instance aware of the new table
247284
this._routingTable = newRoutingTable;
248285
}
286+
}], [{
287+
key: '_isValidRoutingTable',
288+
value: function _isValidRoutingTable(routingTable) {
289+
return routingTable && !routingTable.writers.isEmpty();
290+
}
249291
}, {
250292
key: '_forgetRouter',
251293
value: function _forgetRouter(routingTable, routersArray, routerIndex) {
252294
var address = routersArray[routerIndex];
253-
if (address) {
295+
if (routingTable && address) {
254296
routingTable.forgetRouter(address);
255297
}
256298
}
299+
}, {
300+
key: '_createHostNameResolver',
301+
value: function _createHostNameResolver() {
302+
if ((0, _features2.default)('dns_lookup')) {
303+
return new _hostNameResolvers.DnsHostNameResolver();
304+
}
305+
return new _hostNameResolvers.DummyHostNameResolver();
306+
}
257307
}]);
258308
return LoadBalancer;
259309
}(ConnectionProvider);
@@ -264,10 +314,10 @@ var SingleConnectionProvider = exports.SingleConnectionProvider = function (_Con
264314
function SingleConnectionProvider(connectionPromise) {
265315
(0, _classCallCheck3.default)(this, SingleConnectionProvider);
266316

267-
var _this6 = (0, _possibleConstructorReturn3.default)(this, (SingleConnectionProvider.__proto__ || (0, _getPrototypeOf2.default)(SingleConnectionProvider)).call(this));
317+
var _this8 = (0, _possibleConstructorReturn3.default)(this, (SingleConnectionProvider.__proto__ || (0, _getPrototypeOf2.default)(SingleConnectionProvider)).call(this));
268318

269-
_this6._connectionPromise = connectionPromise;
270-
return _this6;
319+
_this8._connectionPromise = connectionPromise;
320+
return _this8;
271321
}
272322

273323
(0, _createClass3.default)(SingleConnectionProvider, [{

lib/v1/internal/connector.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
Object.defineProperty(exports, "__esModule", {
44
value: true
55
});
6-
exports.Connection = exports.parseUrl = exports.parseScheme = exports.connect = undefined;
6+
exports.Connection = exports.parsePort = exports.parseHost = exports.parseUrl = exports.parseScheme = exports.connect = undefined;
77

88
var _classCallCheck2 = require('babel-runtime/helpers/classCallCheck');
99

@@ -613,4 +613,6 @@ function connect(url) {
613613
exports.connect = connect;
614614
exports.parseScheme = parseScheme;
615615
exports.parseUrl = parseUrl;
616+
exports.parseHost = parseHost;
617+
exports.parsePort = parsePort;
616618
exports.Connection = Connection;

lib/v1/internal/features.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,17 @@ var FEATURES = {
4545
} catch (e) {
4646
return false;
4747
}
48+
},
49+
dns_lookup: function dns_lookup() {
50+
try {
51+
var lookupFunction = require('dns').lookup;
52+
if (lookupFunction && typeof lookupFunction === 'function') {
53+
return true;
54+
}
55+
return false;
56+
} catch (e) {
57+
return false;
58+
}
4859
}
4960
};
5061

Lines changed: 131 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,131 @@
1+
'use strict';
2+
3+
Object.defineProperty(exports, "__esModule", {
4+
value: true
5+
});
6+
exports.DnsHostNameResolver = exports.DummyHostNameResolver = undefined;
7+
8+
var _promise = require('babel-runtime/core-js/promise');
9+
10+
var _promise2 = _interopRequireDefault(_promise);
11+
12+
var _getPrototypeOf = require('babel-runtime/core-js/object/get-prototype-of');
13+
14+
var _getPrototypeOf2 = _interopRequireDefault(_getPrototypeOf);
15+
16+
var _possibleConstructorReturn2 = require('babel-runtime/helpers/possibleConstructorReturn');
17+
18+
var _possibleConstructorReturn3 = _interopRequireDefault(_possibleConstructorReturn2);
19+
20+
var _inherits2 = require('babel-runtime/helpers/inherits');
21+
22+
var _inherits3 = _interopRequireDefault(_inherits2);
23+
24+
var _classCallCheck2 = require('babel-runtime/helpers/classCallCheck');
25+
26+
var _classCallCheck3 = _interopRequireDefault(_classCallCheck2);
27+
28+
var _createClass2 = require('babel-runtime/helpers/createClass');
29+
30+
var _createClass3 = _interopRequireDefault(_createClass2);
31+
32+
var _connector = require('./connector');
33+
34+
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
35+
36+
var HostNameResolver = function () {
37+
function HostNameResolver() {
38+
(0, _classCallCheck3.default)(this, HostNameResolver);
39+
}
40+
41+
(0, _createClass3.default)(HostNameResolver, [{
42+
key: 'resolve',
43+
value: function resolve() {
44+
throw new Error('Abstract function');
45+
}
46+
}]);
47+
return HostNameResolver;
48+
}(); /**
49+
* Copyright (c) 2002-2017 "Neo Technology,","
50+
* Network Engine for Objects in Lund AB [http://neotechnology.com]
51+
*
52+
* This file is part of Neo4j.
53+
*
54+
* Licensed under the Apache License, Version 2.0 (the "License");
55+
* you may not use this file except in compliance with the License.
56+
* You may obtain a copy of the License at
57+
*
58+
* http://www.apache.org/licenses/LICENSE-2.0
59+
*
60+
* Unless required by applicable law or agreed to in writing, software
61+
* distributed under the License is distributed on an "AS IS" BASIS,
62+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
63+
* See the License for the specific language governing permissions and
64+
* limitations under the License.
65+
*/
66+
67+
var DummyHostNameResolver = exports.DummyHostNameResolver = function (_HostNameResolver) {
68+
(0, _inherits3.default)(DummyHostNameResolver, _HostNameResolver);
69+
70+
function DummyHostNameResolver() {
71+
(0, _classCallCheck3.default)(this, DummyHostNameResolver);
72+
return (0, _possibleConstructorReturn3.default)(this, (DummyHostNameResolver.__proto__ || (0, _getPrototypeOf2.default)(DummyHostNameResolver)).apply(this, arguments));
73+
}
74+
75+
(0, _createClass3.default)(DummyHostNameResolver, [{
76+
key: 'resolve',
77+
value: function resolve(seedRouter) {
78+
return resolveToItself(seedRouter);
79+
}
80+
}]);
81+
return DummyHostNameResolver;
82+
}(HostNameResolver);
83+
84+
var DnsHostNameResolver = exports.DnsHostNameResolver = function (_HostNameResolver2) {
85+
(0, _inherits3.default)(DnsHostNameResolver, _HostNameResolver2);
86+
87+
function DnsHostNameResolver() {
88+
(0, _classCallCheck3.default)(this, DnsHostNameResolver);
89+
90+
var _this2 = (0, _possibleConstructorReturn3.default)(this, (DnsHostNameResolver.__proto__ || (0, _getPrototypeOf2.default)(DnsHostNameResolver)).call(this));
91+
92+
_this2._dns = require('dns');
93+
return _this2;
94+
}
95+
96+
(0, _createClass3.default)(DnsHostNameResolver, [{
97+
key: 'resolve',
98+
value: function resolve(seedRouter) {
99+
var _this3 = this;
100+
101+
var seedRouterHost = (0, _connector.parseHost)(seedRouter);
102+
var seedRouterPort = (0, _connector.parsePort)(seedRouter);
103+
104+
return new _promise2.default(function (resolve) {
105+
_this3._dns.lookup(seedRouterHost, { all: true }, function (error, addresses) {
106+
if (error) {
107+
resolve(resolveToItself(seedRouter));
108+
} else {
109+
var addressesWithPorts = addresses.map(function (address) {
110+
return addressWithPort(address, seedRouterPort);
111+
});
112+
resolve(addressesWithPorts);
113+
}
114+
});
115+
});
116+
}
117+
}]);
118+
return DnsHostNameResolver;
119+
}(HostNameResolver);
120+
121+
function resolveToItself(address) {
122+
return _promise2.default.resolve([address]);
123+
}
124+
125+
function addressWithPort(addressObject, port) {
126+
var address = addressObject.address;
127+
if (port) {
128+
return address + ':' + port;
129+
}
130+
return address;
131+
}

lib/v1/internal/stream-observer.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,8 @@ var StreamObserver = function () {
4646
this._error = null;
4747
this._hasFailed = false;
4848
this._errorTransformer = errorTransformer;
49+
this._observer = null;
50+
this._conn = null;
4951
}
5052

5153
/**
@@ -139,8 +141,8 @@ var StreamObserver = function () {
139141
return;
140142
}
141143
if (this._queuedRecords.length > 0) {
142-
for (var i = 0; i < _queuedRecords.length; i++) {
143-
observer.onNext(_queuedRecords[i]);
144+
for (var i = 0; i < this._queuedRecords.length; i++) {
145+
observer.onNext(this._queuedRecords[i]);
144146
}
145147
}
146148
if (this._tail) {

lib/v1/internal/transaction-executor.js

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,24 @@ var TransactionExecutor = function () {
170170
}], [{
171171
key: '_canRetryOn',
172172
value: function _canRetryOn(error) {
173-
return error && error.code && (error.code === _error.SERVICE_UNAVAILABLE || error.code === _error.SESSION_EXPIRED || error.code.indexOf('TransientError') >= 0);
173+
return error && error.code && (error.code === _error.SERVICE_UNAVAILABLE || error.code === _error.SESSION_EXPIRED || this._isTransientError(error));
174+
}
175+
}, {
176+
key: '_isTransientError',
177+
value: function _isTransientError(error) {
178+
// Retries should not happen when transaction was explicitly terminated by the user.
179+
// Termination of transaction might result in two different error codes depending on where it was
180+
// terminated. These are really client errors but classification on the server is not entirely correct and
181+
// they are classified as transient.
182+
183+
var code = error.code;
184+
if (code.indexOf('TransientError') >= 0) {
185+
if (code === 'Neo.TransientError.Transaction.Terminated' || code === 'Neo.TransientError.Transaction.LockClientStopped') {
186+
return false;
187+
}
188+
return true;
189+
}
190+
return false;
174191
}
175192
}]);
176193
return TransactionExecutor;

0 commit comments

Comments
 (0)