-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathindex.js
198 lines (194 loc) · 5.1 KB
/
index.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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
/**
*
* # Prerequisite: bitd must be running.
*
* # Usage:
* const bitdb = require('bitdb')
* bitdb.read({ ... }, function(err, response) {
* res.json(response)
* })
*
* # See examples/app.js for an actual example
*
*/
const iconv = require('iconv-lite');
const traverse = require('traverse')
const MongoClient = require('mongodb').MongoClient;
const Bitdb = {
config: {
url: 'mongodb://localhost:27017',
dbName: 'bitdb'
},
confirmed: function() {
if (Bitdb._db) {
return Bitdb._db.collection('confirmed');
} else {
throw new Error("Must initialize DB first");
}
},
unconfirmed: function() {
if (Bitdb._db) {
return Bitdb._db.collection('unconfirmed');
} else {
throw new Error("Must initialize DB first");
}
},
exit: function() {
Bitdb.mongo.close()
},
init: function(options, callback) {
// Initialize DB
let url = ( (options && options.url) ? options.url : Bitdb.config.url );
let dbName = ( (options && options.name) ? options.name : Bitdb.config.dbName );
MongoClient.connect(url, {useNewUrlParser: true}, function(err, client) {
if (err) console.log(err)
Bitdb._db = client.db(dbName);
Bitdb.mongo = client;
callback(Bitdb._db)
})
},
encoding: function(subtree, encoding_schema) {
traverse(subtree).forEach(function(x) {
if (this.isLeaf) {
let encoding = "utf8";
let token = x;
if (/^0x/i.test(x)) {
encoding = 'hex';
token = x.substring(2)
}
let newVal = token;
// if the key is a special directive like '$in', traverse up the tree
// until we find a normal key that starts with b or s
let node = this;
if (/^([0-9]+|\$).*/.test(node.key)) {
while(!node.isRoot) {
node = node.parent;
if (/^b[0-9]+/.test(node.key)) {
break;
}
}
}
if (encoding_schema && encoding_schema[node.key]) {
encoding = encoding_schema[node.key];
}
if (/^b[0-9]+/.test(node.key)) {
newVal = iconv.encode(token, encoding).toString("base64");
}
this.update(newVal)
}
})
},
/**
*
* bitdb.read({ ... }).then(function(result) {
* // do something with the result
* })
*/
read: function(r, cb) {
/**
* r := {
* "request": {
* "find": {
* "b0": "0x6d02"
* },
* "sort": {
* "b1": 1
* },
* "limit": 50
* },
* "response": {
* "b0": "hex",
* "b1": "utf8",
* "b2": "hex"
* }
* }
**/
if (!Bitdb._db) {
Bitdb.init(null, function() {
Bitdb.read(r, cb)
})
} else {
if (r.request) {
let query = r.request;
Bitdb.encoding(query.find, query.encoding)
Bitdb.encoding(query.aggregate, query.encoding)
// query
return Promise.all([
Bitdb.lookup(r, Bitdb.confirmed()),
Bitdb.lookup(r, Bitdb.unconfirmed())
])
.then(function(results) {
cb(null, {
status: "success",
confirmed: results[0],
unconfirmed: results[1]
})
})
.catch(function(err) {
cb(null, {
status: "error",
error: err
})
})
} else {
cb(null, {
status: "error",
error: "The request needs a query object"
})
}
}
},
lookup: function(r, collection) {
let query = r.request;
return new Promise(function(resolve, reject) {
let cursor;
if (query.find || query.aggregate) {
if (query.find) {
cursor = collection.find(query.find)
} else if (query.aggregate) {
cursor = collection.aggregate(query.aggregate)
}
if (query.sort) {
cursor = cursor.sort(query.sort)
} else {
cursor = cursor.sort({block_index: -1})
}
if (query.project) {
cursor = cursor.project(query.project)
}
if (query.limit) {
cursor = cursor.limit(query.limit)
} else {
cursor = cursor.limit(100)
}
cursor.toArray(function(err, docs) {
// transform each key in the response object
let transformed = docs.map(function(doc) {
let o = doc;
if (r.response && r.response.encoding) {
for (let key in r.response.encoding) {
let encoding = r.response.encoding[key];
if (doc[key]) {
o[key] = iconv.encode(doc[key], "base64").toString(encoding)
}
}
}
return o;
})
resolve(transformed)
})
} else if (query.distinct) {
if (query.distinct.field) {
collection.distinct(query.distinct.field, query.distinct.query, query.distinct.options).then(function(items) {
resolve(items)
})
}
}
})
}
}
module.exports = {
init: Bitdb.init,
exit: Bitdb.exit,
read: Bitdb.read,
}