Skip to content

Commit

Permalink
JS driver v1.2.0: Checking in transpiled files for bower
Browse files Browse the repository at this point in the history
  • Loading branch information
lutovich committed Mar 23, 2017
1 parent f82e012 commit dd00124
Show file tree
Hide file tree
Showing 9 changed files with 2,147 additions and 526 deletions.
321 changes: 268 additions & 53 deletions lib/browser/neo4j-web.js

Large diffs are not rendered by default.

23 changes: 12 additions & 11 deletions lib/browser/neo4j-web.min.js

Large diffs are not rendered by default.

2,056 changes: 1,624 additions & 432 deletions lib/browser/neo4j-web.test.js

Large diffs are not rendered by default.

102 changes: 76 additions & 26 deletions lib/v1/internal/connection-providers.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,12 @@ var _rediscovery = require('./rediscovery');

var _rediscovery2 = _interopRequireDefault(_rediscovery);

var _features = require('./features');

var _features2 = _interopRequireDefault(_features);

var _hostNameResolvers = require('./host-name-resolvers');

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }

/**
Expand Down Expand Up @@ -78,7 +84,7 @@ var ConnectionProvider = function () {
(0, _createClass3.default)(ConnectionProvider, [{
key: 'acquireConnection',
value: function acquireConnection(mode) {
throw new Error('Abstract method');
throw new Error('Abstract function');
}
}, {
key: '_withAdditionalOnErrorCallback',
Expand Down Expand Up @@ -128,10 +134,12 @@ var LoadBalancer = exports.LoadBalancer = function (_ConnectionProvider2) {

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

_this2._routingTable = new _routingTable2.default(new _roundRobinArray2.default([address]));
_this2._seedRouter = address;
_this2._routingTable = new _routingTable2.default(new _roundRobinArray2.default([_this2._seedRouter]));
_this2._rediscovery = new _rediscovery2.default();
_this2._connectionPool = connectionPool;
_this2._driverOnErrorCallback = driverOnErrorCallback;
_this2._hostNameResolver = LoadBalancer._createHostNameResolver();
return _this2;
}

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

var knownRouters = currentRoutingTable.routers.toArray();

var refreshedTablePromise = knownRouters.reduce(function (refreshedTablePromise, currentRouter, currentIndex) {
return this._fetchNewRoutingTable(knownRouters, currentRoutingTable).then(function (newRoutingTable) {
if (LoadBalancer._isValidRoutingTable(newRoutingTable)) {
// one of the known routers returned a valid routing table - use it
return newRoutingTable;
}

if (!newRoutingTable) {
// returned routing table was undefined, this means a connection error happened and the last known
// router did not return a valid routing table, so we need to forget it
var lastRouterIndex = knownRouters.length - 1;
LoadBalancer._forgetRouter(currentRoutingTable, knownRouters, lastRouterIndex);
}

// none of the known routers returned a valid routing table - try to use seed router address for rediscovery
return _this4._fetchNewRoutingTableUsingSeedRouterAddress(knownRouters, _this4._seedRouter);
}).then(function (newRoutingTable) {
if (LoadBalancer._isValidRoutingTable(newRoutingTable)) {
_this4._updateRoutingTable(newRoutingTable);
return newRoutingTable;
}

// none of the existing routers returned valid routing table, throw exception
throw (0, _error.newError)('Could not perform discovery. No routing servers available.', _error.SERVICE_UNAVAILABLE);
});
}
}, {
key: '_fetchNewRoutingTableUsingSeedRouterAddress',
value: function _fetchNewRoutingTableUsingSeedRouterAddress(knownRouters, seedRouter) {
var _this5 = this;

return this._hostNameResolver.resolve(seedRouter).then(function (resolvedRouterAddresses) {
// filter out all addresses that we've already tried
var newAddresses = resolvedRouterAddresses.filter(function (address) {
return knownRouters.indexOf(address) < 0;
});
return _this5._fetchNewRoutingTable(newAddresses, null);
});
}
}, {
key: '_fetchNewRoutingTable',
value: function _fetchNewRoutingTable(routerAddresses, routingTable) {
var _this6 = this;

return routerAddresses.reduce(function (refreshedTablePromise, currentRouter, currentIndex) {
return refreshedTablePromise.then(function (newRoutingTable) {
if (newRoutingTable) {
if (!newRoutingTable.writers.isEmpty()) {
Expand All @@ -199,28 +250,14 @@ var LoadBalancer = exports.LoadBalancer = function (_ConnectionProvider2) {
// returned routing table was undefined, this means a connection error happened and we need to forget the
// previous router and try the next one
var previousRouterIndex = currentIndex - 1;
_this4._forgetRouter(currentRoutingTable, knownRouters, previousRouterIndex);
LoadBalancer._forgetRouter(routingTable, routerAddresses, previousRouterIndex);
}

// try next router
var session = _this4._createSessionForRediscovery(currentRouter);
return _this4._rediscovery.lookupRoutingTableOnRouter(session, currentRouter);
var session = _this6._createSessionForRediscovery(currentRouter);
return _this6._rediscovery.lookupRoutingTableOnRouter(session, currentRouter);
});
}, _promise2.default.resolve(null));

return refreshedTablePromise.then(function (newRoutingTable) {
if (newRoutingTable && !newRoutingTable.writers.isEmpty()) {
_this4._updateRoutingTable(newRoutingTable);
return newRoutingTable;
}

// forget the last known router because it did not return a valid routing table
var lastRouterIndex = knownRouters.length - 1;
_this4._forgetRouter(currentRoutingTable, knownRouters, lastRouterIndex);

// none of the existing routers returned valid routing table, throw exception
throw (0, _error.newError)('Could not perform discovery. No routing servers available.', _error.SERVICE_UNAVAILABLE);
});
}
}, {
key: '_createSessionForRediscovery',
Expand All @@ -233,27 +270,40 @@ var LoadBalancer = exports.LoadBalancer = function (_ConnectionProvider2) {
}, {
key: '_updateRoutingTable',
value: function _updateRoutingTable(newRoutingTable) {
var _this5 = this;
var _this7 = this;

var currentRoutingTable = this._routingTable;

// close old connections to servers not present in the new routing table
var staleServers = currentRoutingTable.serversDiff(newRoutingTable);
staleServers.forEach(function (server) {
return _this5._connectionPool.purge(server);
return _this7._connectionPool.purge(server);
});

// make this driver instance aware of the new table
this._routingTable = newRoutingTable;
}
}], [{
key: '_isValidRoutingTable',
value: function _isValidRoutingTable(routingTable) {
return routingTable && !routingTable.writers.isEmpty();
}
}, {
key: '_forgetRouter',
value: function _forgetRouter(routingTable, routersArray, routerIndex) {
var address = routersArray[routerIndex];
if (address) {
if (routingTable && address) {
routingTable.forgetRouter(address);
}
}
}, {
key: '_createHostNameResolver',
value: function _createHostNameResolver() {
if ((0, _features2.default)('dns_lookup')) {
return new _hostNameResolvers.DnsHostNameResolver();
}
return new _hostNameResolvers.DummyHostNameResolver();
}
}]);
return LoadBalancer;
}(ConnectionProvider);
Expand All @@ -264,10 +314,10 @@ var SingleConnectionProvider = exports.SingleConnectionProvider = function (_Con
function SingleConnectionProvider(connectionPromise) {
(0, _classCallCheck3.default)(this, SingleConnectionProvider);

var _this6 = (0, _possibleConstructorReturn3.default)(this, (SingleConnectionProvider.__proto__ || (0, _getPrototypeOf2.default)(SingleConnectionProvider)).call(this));
var _this8 = (0, _possibleConstructorReturn3.default)(this, (SingleConnectionProvider.__proto__ || (0, _getPrototypeOf2.default)(SingleConnectionProvider)).call(this));

_this6._connectionPromise = connectionPromise;
return _this6;
_this8._connectionPromise = connectionPromise;
return _this8;
}

(0, _createClass3.default)(SingleConnectionProvider, [{
Expand Down
4 changes: 3 additions & 1 deletion lib/v1/internal/connector.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.Connection = exports.parseUrl = exports.parseScheme = exports.connect = undefined;
exports.Connection = exports.parsePort = exports.parseHost = exports.parseUrl = exports.parseScheme = exports.connect = undefined;

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

Expand Down Expand Up @@ -613,4 +613,6 @@ function connect(url) {
exports.connect = connect;
exports.parseScheme = parseScheme;
exports.parseUrl = parseUrl;
exports.parseHost = parseHost;
exports.parsePort = parsePort;
exports.Connection = Connection;
11 changes: 11 additions & 0 deletions lib/v1/internal/features.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,17 @@ var FEATURES = {
} catch (e) {
return false;
}
},
dns_lookup: function dns_lookup() {
try {
var lookupFunction = require('dns').lookup;
if (lookupFunction && typeof lookupFunction === 'function') {
return true;
}
return false;
} catch (e) {
return false;
}
}
};

Expand Down
131 changes: 131 additions & 0 deletions lib/v1/internal/host-name-resolvers.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
'use strict';

Object.defineProperty(exports, "__esModule", {
value: true
});
exports.DnsHostNameResolver = exports.DummyHostNameResolver = undefined;

var _promise = require('babel-runtime/core-js/promise');

var _promise2 = _interopRequireDefault(_promise);

var _getPrototypeOf = require('babel-runtime/core-js/object/get-prototype-of');

var _getPrototypeOf2 = _interopRequireDefault(_getPrototypeOf);

var _possibleConstructorReturn2 = require('babel-runtime/helpers/possibleConstructorReturn');

var _possibleConstructorReturn3 = _interopRequireDefault(_possibleConstructorReturn2);

var _inherits2 = require('babel-runtime/helpers/inherits');

var _inherits3 = _interopRequireDefault(_inherits2);

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

var _classCallCheck3 = _interopRequireDefault(_classCallCheck2);

var _createClass2 = require('babel-runtime/helpers/createClass');

var _createClass3 = _interopRequireDefault(_createClass2);

var _connector = require('./connector');

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }

var HostNameResolver = function () {
function HostNameResolver() {
(0, _classCallCheck3.default)(this, HostNameResolver);
}

(0, _createClass3.default)(HostNameResolver, [{
key: 'resolve',
value: function resolve() {
throw new Error('Abstract function');
}
}]);
return HostNameResolver;
}(); /**
* Copyright (c) 2002-2017 "Neo Technology,","
* Network Engine for Objects in Lund AB [http://neotechnology.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.
*/

var DummyHostNameResolver = exports.DummyHostNameResolver = function (_HostNameResolver) {
(0, _inherits3.default)(DummyHostNameResolver, _HostNameResolver);

function DummyHostNameResolver() {
(0, _classCallCheck3.default)(this, DummyHostNameResolver);
return (0, _possibleConstructorReturn3.default)(this, (DummyHostNameResolver.__proto__ || (0, _getPrototypeOf2.default)(DummyHostNameResolver)).apply(this, arguments));
}

(0, _createClass3.default)(DummyHostNameResolver, [{
key: 'resolve',
value: function resolve(seedRouter) {
return resolveToItself(seedRouter);
}
}]);
return DummyHostNameResolver;
}(HostNameResolver);

var DnsHostNameResolver = exports.DnsHostNameResolver = function (_HostNameResolver2) {
(0, _inherits3.default)(DnsHostNameResolver, _HostNameResolver2);

function DnsHostNameResolver() {
(0, _classCallCheck3.default)(this, DnsHostNameResolver);

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

_this2._dns = require('dns');
return _this2;
}

(0, _createClass3.default)(DnsHostNameResolver, [{
key: 'resolve',
value: function resolve(seedRouter) {
var _this3 = this;

var seedRouterHost = (0, _connector.parseHost)(seedRouter);
var seedRouterPort = (0, _connector.parsePort)(seedRouter);

return new _promise2.default(function (resolve) {
_this3._dns.lookup(seedRouterHost, { all: true }, function (error, addresses) {
if (error) {
resolve(resolveToItself(seedRouter));
} else {
var addressesWithPorts = addresses.map(function (address) {
return addressWithPort(address, seedRouterPort);
});
resolve(addressesWithPorts);
}
});
});
}
}]);
return DnsHostNameResolver;
}(HostNameResolver);

function resolveToItself(address) {
return _promise2.default.resolve([address]);
}

function addressWithPort(addressObject, port) {
var address = addressObject.address;
if (port) {
return address + ':' + port;
}
return address;
}
6 changes: 4 additions & 2 deletions lib/v1/internal/stream-observer.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ var StreamObserver = function () {
this._error = null;
this._hasFailed = false;
this._errorTransformer = errorTransformer;
this._observer = null;
this._conn = null;
}

/**
Expand Down Expand Up @@ -139,8 +141,8 @@ var StreamObserver = function () {
return;
}
if (this._queuedRecords.length > 0) {
for (var i = 0; i < _queuedRecords.length; i++) {
observer.onNext(_queuedRecords[i]);
for (var i = 0; i < this._queuedRecords.length; i++) {
observer.onNext(this._queuedRecords[i]);
}
}
if (this._tail) {
Expand Down
19 changes: 18 additions & 1 deletion lib/v1/internal/transaction-executor.js
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,24 @@ var TransactionExecutor = function () {
}], [{
key: '_canRetryOn',
value: function _canRetryOn(error) {
return error && error.code && (error.code === _error.SERVICE_UNAVAILABLE || error.code === _error.SESSION_EXPIRED || error.code.indexOf('TransientError') >= 0);
return error && error.code && (error.code === _error.SERVICE_UNAVAILABLE || error.code === _error.SESSION_EXPIRED || this._isTransientError(error));
}
}, {
key: '_isTransientError',
value: function _isTransientError(error) {
// Retries should not happen when transaction was explicitly terminated by the user.
// Termination of transaction might result in two different error codes depending on where it was
// terminated. These are really client errors but classification on the server is not entirely correct and
// they are classified as transient.

var code = error.code;
if (code.indexOf('TransientError') >= 0) {
if (code === 'Neo.TransientError.Transaction.Terminated' || code === 'Neo.TransientError.Transaction.LockClientStopped') {
return false;
}
return true;
}
return false;
}
}]);
return TransactionExecutor;
Expand Down

0 comments on commit dd00124

Please sign in to comment.