forked from fountainhead-cash/bitqueryd
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
185 lines (180 loc) · 5.15 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
require('dotenv').config()
const jq = require('fountainhead-bigjq')
const bcode = require('bcode')
const MongoClient = require('mongodb').MongoClient
const traverse = require('traverse')
const dbTypes = ["u", "c"]
const dbMapping = {
u: "unconfirmed",
c: "confirmed"
}
const ops = ["db", "find", "aggregate", "sort", "project", "limit", "skip", "distinct"]
var db, client
var timeout = null
var log_result = true
var validate = function(r) {
if (typeof r.v === 'undefined') {
return { status: "invalid", result: false, errors: ["v missing"] }
}
if (typeof r.q === 'undefined') {
return { status: "invalid", result: false, errors: ['q missing'] }
}
let keys = Object.keys(r.q)
if (keys.length === 0) {
return { status: "invalid", result: false, errors: ['q empty'] }
}
let errors = []
for (let i=0; i<keys.length; i++) {
if (ops.indexOf(keys[i]) < 0) {
errors.push("invalid MongoDB op(supported: find, aggregate, sort, project, limit, distinct)")
return { status: "invalid", result: false, errors: errors }
}
}
return { status: "valid", result: true }
}
var read = async function(r) {
let isvalid = validate(r)
if (!isvalid.result) return isvalid;
let result = {}
// 1. v: version
// 2. q: query
// 3. r: response
if (r.q) {
let query = r.q
let encoding = r.e // legacy for v2 (deprecated with v3)
let resfilter = r.r
if (query.find) {
query.find = bcode.encode(query.find, encoding)
} else if (query.aggregate) {
query.aggregate = bcode.encode(query.aggregate, encoding)
}
let promises = []
let src = (query.db && query.db.length > 0) ? query.db : dbTypes
for (let i=0; i<src.length; i++) {
let key = src[i];
if (src.indexOf(key) >= 0) {
promises.push(lookup({ request: query, encoding: encoding }, key, resfilter))
}
}
try {
let responses = await Promise.all(promises)
responses.forEach(function(response) {
result[response.name] = response.items
})
} catch (e) {
if (result.errors) {
result.errors.push(e.toString())
} else {
result.errors = [e.toString()]
}
}
}
return result
}
var exit = function() {
client.close()
}
var init = function(config) {
return new Promise(function(resolve, reject) {
let url = (config && config.url ? config.url : "mongodb://localhost:27017")
let name = (config && config.name ? config.name : "bitdb")
let sockTimeout = (config && config.timeout) ? config.timeout + 100 : 20100
log_result = (config && typeof config.log_result === 'boolean') ? config.log_result : true;
if (/mongodb:.*/.test(url)) {
MongoClient.connect(url, {
useNewUrlParser: true,
socketTimeoutMS: sockTimeout
}, function(err, _client) {
if (err) console.log(err)
client = _client
if (config && config.timeout) {
timeout = config.timeout
}
db = client.db(name)
resolve({ read: read, exit: exit })
})
} else {
reject("Invalid Node URL")
}
})
}
var lookup = function(r, key, resfilter) {
let collectionName = dbMapping[key]
let collection = db.collection(collectionName)
let query = r.request
return new Promise(async function(resolve, reject) {
let cursor
if (query.find || query.aggregate) {
if (query.find) {
cursor = collection.find(query.find, { allowDiskUse:true })
} else if (query.aggregate) {
cursor = collection.aggregate(query.aggregate, { allowDiskUse:true })
}
if (query.sort) {
cursor = cursor.sort(query.sort)
} else {
cursor = cursor.sort({'blk.i': -1})
}
if (query.project) {
cursor = cursor.project(query.project)
}
if (query.skip) {
cursor = cursor.skip(query.skip)
}
if (query.limit) {
cursor = cursor.limit(query.limit)
} else {
cursor = cursor.limit(100)
}
if (timeout) {
cursor = cursor.maxTimeMS(timeout)
}
cursor.toArray(async function(err, docs) {
if (err) {
reject(err)
} else {
let res = docs;
res = bcode.decode(docs, r.encoding)
// response filter
if (resfilter && resfilter.f && res.length > 0) {
try {
let f = resfilter.f;
let result = await jq.run(f, res, log_result)
resolve({
name: key,
items: result
})
} catch (e) {
reject(e)
}
} else {
resolve({
name: key,
items: res
})
}
}
})
} else if (query.distinct) {
if (query.distinct.field) {
try {
let items = await collection.distinct(query.distinct.field, query.distinct.query, query.distinct.options)
let res = items
res = bcode.decode(docs, r.encoding)
resolve({
name: key,
items: res
})
} catch (e) {
reject(e)
}
}
}
})
}
module.exports = {
init: init,
exit: exit,
read: read,
validate: validate
}