-
Notifications
You must be signed in to change notification settings - Fork 7
/
neo4j_driver.js
68 lines (60 loc) · 1.54 KB
/
neo4j_driver.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
var http = require('http');
exports.createClient = function(options) {
options = options || {};
var
running = 0,
backlog = [],
host = options.host || 'localhost',
port = options.port || 7474,
limit = options.limit || 10;
function dequeue() {
if (backlog.length && running < limit) {
req.apply(null, backlog.shift());
}
}
function req(method, path, data, callback) {
running += 1;
return http
.request({
host: host,
port: port,
path: '/db/data/' + (path.join ? path.join('/') : path),
headers: {'Content-Type':'application/json'},
method: method
}, function(res){
var buffer = '';
res.on('data', function(chunk){
buffer += chunk;
});
res.on('end', function(){
var output;
if (callback && buffer != '') {
try {
output = JSON.parse(buffer);
} catch (err) {
console.error(err);
}
callback(output, res);
}
running -= 1;
dequeue();
});
})
.on('error', function(){
running -= 1;
backlog.push([method, path, data, callback]);
dequeue();
})
.end(data ? JSON.stringify(data) : undefined);
};
return {
get: function(path, callback) {
backlog.push(['GET', path, null, callback]);
dequeue();
},
post: function(path, data, callback) {
backlog.push(['POST', path, data, callback]);
dequeue();
}
};
}