Skip to content

Commit 7cdf0c8

Browse files
committed
Exposed onComplete method from Driver.
Provides quick feedback to client that credentials are correct.
1 parent b0277d2 commit 7cdf0c8

File tree

9 files changed

+6021
-5092
lines changed

9 files changed

+6021
-5092
lines changed

lib/browser/neo4j-web.js

Lines changed: 2814 additions & 2506 deletions
Large diffs are not rendered by default.

lib/browser/neo4j-web.min.js

Lines changed: 14 additions & 14 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: 2994 additions & 2517 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

lib/v1/driver.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -213,6 +213,13 @@ var _ConnectionStreamObserver = (function (_StreamObserver) {
213213
this._hasFailed = true;
214214
}
215215
}
216+
}, {
217+
key: 'onCompleted',
218+
value: function onCompleted(message) {
219+
if (this._driver.onCompleted) {
220+
this._driver.onCompleted(message);
221+
}
222+
}
216223
}]);
217224

218225
return _ConnectionStreamObserver;

lib/v1/internal/ch-node.js

Lines changed: 50 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -62,16 +62,35 @@ function userHome() {
6262
return process.env[process.platform == 'win32' ? 'USERPROFILE' : 'HOME'];
6363
}
6464

65+
function mkFullPath(pathToCreate) {
66+
try {
67+
_fs2['default'].mkdirSync(pathToCreate);
68+
} catch (e) {
69+
if (e.code === 'ENOENT') {
70+
// Create parent dir
71+
mkFullPath(_path2['default'].dirname(pathToCreate));
72+
// And now try again
73+
mkFullPath(pathToCreate);
74+
return;
75+
}
76+
if (e.code === 'EEXIST') {
77+
return;
78+
}
79+
throw e;
80+
}
81+
}
82+
6583
function loadFingerprint(serverId, knownHostsPath, cb) {
66-
if (!_fs2['default'].existsSync(knownHostsPath)) {
67-
cb(null);
68-
return;
84+
try {
85+
_fs2['default'].accessSync(knownHostsPath);
86+
} catch (e) {
87+
return cb(null);
6988
}
7089
var found = false;
7190
require('readline').createInterface({
7291
input: _fs2['default'].createReadStream(knownHostsPath)
7392
}).on('line', function (line) {
74-
if (line.startsWith(serverId)) {
93+
if (!found && line.startsWith(serverId)) {
7594
found = true;
7695
cb(line.split(" ")[1]);
7796
}
@@ -82,11 +101,31 @@ function loadFingerprint(serverId, knownHostsPath, cb) {
82101
});
83102
}
84103

85-
function storeFingerprint(serverId, knownHostsPath, fingerprint) {
104+
var _lockFingerprintFromAppending = {};
105+
function storeFingerprint(serverId, knownHostsPath, fingerprint, cb) {
106+
// we check if the serverId has been appended
107+
if (!!_lockFingerprintFromAppending[serverId]) {
108+
// if it has, we ignore it
109+
return cb(null);
110+
}
111+
112+
// we make the line as appended
113+
// ( 1 is more efficient to store than true because true is an oddball )
114+
_lockFingerprintFromAppending[serverId] = 1;
115+
116+
// If file doesn't exist, create full path to it
117+
try {
118+
_fs2['default'].accessSync(knownHostsPath);
119+
} catch (_) {
120+
mkFullPath(_path2['default'].dirname(knownHostsPath));
121+
}
122+
86123
_fs2['default'].appendFile(knownHostsPath, serverId + " " + fingerprint + _os.EOL, "utf8", function (err) {
124+
delete _lockFingerprintFromAppending[serverId];
87125
if (err) {
88126
console.log(err);
89127
}
128+
return cb(err);
90129
});
91130
}
92131

@@ -140,8 +179,12 @@ var TrustStrategy = {
140179
if (knownFingerprint === serverFingerprint) {
141180
onSuccess();
142181
} else if (knownFingerprint == null) {
143-
storeFingerprint(serverId, knownHostsPath, serverFingerprint);
144-
onSuccess();
182+
storeFingerprint(serverId, knownHostsPath, serverFingerprint, function (err) {
183+
if (err) {
184+
return onFailure(err);
185+
}
186+
return onSuccess();
187+
});
145188
} else {
146189
onFailure((0, _error.newError)("Database encryption certificate has changed, and no longer " + "matches the certificate stored for " + serverId + " in `" + knownHostsPath + "`. As a security precaution, this driver will not automatically trust the new " + "certificate, because doing so would allow an attacker to pretend to be the Neo4j " + "instance we want to connect to. The certificate provided by the server looks like: " + serverCert + ". If you trust that this certificate is valid, simply remove the line " + "starting with " + serverId + " in `" + knownHostsPath + "`, and the driver will " + "update the file with the new certificate. You can configure which file the driver " + "should use to store this information by setting `knownHosts` to another path in " + "your driver configuration - and you can disable encryption there as well using " + "`encrypted:false`."));
147190
}

lib/v1/internal/connector.js

Lines changed: 41 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -193,6 +193,7 @@ var _mappers = {
193193
*/
194194

195195
var Connection = (function () {
196+
196197
/**
197198
* @constructor
198199
* @param channel - channel with a 'write' function and a 'onmessage'
@@ -370,42 +371,61 @@ var Connection = (function () {
370371
}, {
371372
key: "initialize",
372373
value: function initialize(clientName, token, observer) {
374+
var _this2 = this;
375+
373376
this._queueObserver(observer);
374-
this._packer.packStruct(INIT, [clientName, token]);
377+
this._packer.packStruct(INIT, [this._packable(clientName), this._packable(token)], function (err) {
378+
return _this2._handleFatalError(err);
379+
});
375380
this._chunker.messageBoundary();
381+
this.sync();
376382
}
377383

378384
/** Queue a RUN-message to be sent to the database */
379385
}, {
380386
key: "run",
381387
value: function run(statement, params, observer) {
388+
var _this3 = this;
389+
382390
this._queueObserver(observer);
383-
this._packer.packStruct(RUN, [statement, params]);
391+
this._packer.packStruct(RUN, [this._packable(statement), this._packable(params)], function (err) {
392+
return _this3._handleFatalError(err);
393+
});
384394
this._chunker.messageBoundary();
385395
}
386396

387397
/** Queue a PULL_ALL-message to be sent to the database */
388398
}, {
389399
key: "pullAll",
390400
value: function pullAll(observer) {
401+
var _this4 = this;
402+
391403
this._queueObserver(observer);
392-
this._packer.packStruct(PULL_ALL);
404+
this._packer.packStruct(PULL_ALL, [], function (err) {
405+
return _this4._handleFatalError(err);
406+
});
393407
this._chunker.messageBoundary();
394408
}
395409

396410
/** Queue a DISCARD_ALL-message to be sent to the database */
397411
}, {
398412
key: "discardAll",
399413
value: function discardAll(observer) {
414+
var _this5 = this;
415+
400416
this._queueObserver(observer);
401-
this._packer.packStruct(DISCARD_ALL);
417+
this._packer.packStruct(DISCARD_ALL, [], function (err) {
418+
return _this5._handleFatalError(err);
419+
});
402420
this._chunker.messageBoundary();
403421
}
404422

405423
/** Queue a RESET-message to be sent to the database */
406424
}, {
407425
key: "reset",
408426
value: function reset(observer) {
427+
var _this6 = this;
428+
409429
this._isHandlingFailure = true;
410430
var self = this;
411431
var wrappedObs = {
@@ -419,16 +439,22 @@ var Connection = (function () {
419439
}
420440
};
421441
this._queueObserver(wrappedObs);
422-
this._packer.packStruct(RESET);
442+
this._packer.packStruct(RESET, [], function (err) {
443+
return _this6._handleFatalError(err);
444+
});
423445
this._chunker.messageBoundary();
424446
}
425447

426448
/** Queue a ACK_FAILURE-message to be sent to the database */
427449
}, {
428450
key: "_ackFailure",
429451
value: function _ackFailure(observer) {
452+
var _this7 = this;
453+
430454
this._queueObserver(observer);
431-
this._packer.packStruct(ACK_FAILURE);
455+
this._packer.packStruct(ACK_FAILURE, [], function (err) {
456+
return _this7._handleFatalError(err);
457+
});
432458
this._chunker.messageBoundary();
433459
}
434460
}, {
@@ -477,6 +503,15 @@ var Connection = (function () {
477503
value: function close(cb) {
478504
this._ch.close(cb);
479505
}
506+
}, {
507+
key: "_packable",
508+
value: function _packable(value) {
509+
var _this8 = this;
510+
511+
return this._packer.packable(value, function (err) {
512+
return _this8._handleFatalError(err);
513+
});
514+
}
480515
}]);
481516

482517
return Connection;

0 commit comments

Comments
 (0)