-
Notifications
You must be signed in to change notification settings - Fork 1
/
bitdb.js
258 lines (241 loc) · 5.64 KB
/
bitdb.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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
const genesisUrl = 'https://genesis.bitdb.network/q/1FnauZ9aUH2Bex6JzdcV4eNX7oLSSEbxtN/'
const genesisKey = ['159bcdKY4spcahzfTZhBbFBXrTWpoh4rd3']
const babelUrl = 'https://babel.bitdb.network/q/1DHDifPvtPgKFPZMRSxmVHhiPvFmxZwbfh/'
const dataUrl = 'https://data.bitdb.network/q/1KuUr2pSJDao97XM8Jsq8zwLS6W1WtFfLg/'
const dataKey = ['1Px8CKdrfJUw7eVrTmVYjYtmtDoxjD6tGt']
const axios = require('axios')
const D_ = '19iG3WTYSsbyos3uJ733yK4zEioi1FesNU'
const B_ = '19HxigV4QyBv3tHpQVcUEQyq1pzZVdoAut'
// lb2 or b2 = data
// s3 = contentType, s4 = encoding, s5 = filename
const BCat_ = '15DHFxWZJT58f9nhyGnsRBqrgwK4W6h4Up'
// s2 = info, s3 = mime, s4 = encoding, s5 = filename, s6 = flag, h7... = chunks
const BCatPart_ = '1ChDHzdd1H4wSjgGMHyndZm6qxEDGjqpJL'
// lb2 or b2 = data
const APPS = {
[B_]: 'B',
[BCat_]: 'BCAT',
[BCatPart_]: 'BCATpart',
[D_]: 'D'
}
async function autobitdb(query) {
try {
return await offsetbitdb(query, 1)
} catch(e) {
if (e.code != 'NoResults') { throw e }
return await bitdb(query, true)
}
}
async function offsetbitdb(query, offset, nothrow = false) {
let fix = []
for (let f0 of ['find', 'project', 'sort']) {
let a = query.q[f0]
if (!a) { continue }
let n = {}
for (let f in a) {
if (f.match('\\d$')) {
let v = a[f]
let f2 = f.slice(0,-1) + (parseInt(f.slice(-1))+offset)
n[f2] = v
} else {
n[f] = a[f]
}
}
let f9 = f0
fix.push(()=>{query.q[f9] = a})
query.q[f0] = n
}
if (query.r && query.r.f) {
let a = query.r.f
fix.push(()=>{query.r.f = a})
query.r.f = a.replace(/[^\[]\d([^0-9a-z]|$)/g, d => d[0] + (parseInt(d[1]) + offset) + d.slice(2))
}
try {
return await bitdb(query, nothrow)
} catch(e) {
for (let f of fix) { f() }
throw e
}
return ret
}
async function bitdb(query, nothrow = false) {
console.log('Warning: bitdb has been deprecated. Likely you\'ll need to hunt down the fix for an issue to get any results.')
let url, key
//process.stderr.write(JSON.stringify(query) + '\n')
url = babelUrl
key = genesisKey
if (query.q.find.c) {
url = dataUrl
key = dataKey
}
let b64 = Buffer.from(JSON.stringify(query)).toString('base64')
url = url + b64
let config = { headers: { key: key }, transformResponse:x=>x}
let res
while (true) {
try {
res = (await axios.get(url, config)).data
break
} catch(e) {
if (e.code == 'EAI_AGAIN') {
process.stderr.write('... network interruption ...\n')
await new Promise(resolve => setTimeout(resolve, 1000))
continue
}
throw e
}
}
//process.stderr.write(res + '\n')
try {
let json = JSON.parse(res)
if (json.u && json.c && json.u.concat && json.c.concat && json.u.length > 0) {
return json.u.concat(json.c)
} else if (json.u && (json.u.length === undefined || json.u.length !== 0)) {
return json.u
} else if (json.c && (json.c.length === undefined || json.c.length !== 0)) {
return json.c
} else if (!nothrow) {
let e = new Error("no results for " + JSON.stringify(query))
e.code = 'NoResults'
throw e
} else {
return []
}
} catch(e) {
if (e.name === 'SyntaxError') {
throw new Error('Invalid server response: ' + res);
}
throw e
}
}
function inaddr(addr, limit, skip, reverse = true)
{
return { 'v': 3, 'q': { 'find': {
'in.e.a': addr,
}, 'project': {
'tx.h': 1,
'blk.i': 1,
}, 'sort': {
'blk.i': reverse ? -1 : 1,
}, 'limit': limit, 'skip': skip }, 'r': {
'f': '[.[] | .tx.h]'
} }
}
// reverse = true means newest-first
function d(addr, limit, skip, key = undefined, reverse = true)
{
return { 'v': 3, 'q': { 'find': {
'out.s1': D_,
'in.e.a': addr,
'out.s2': key
}, 'project': {
'tx.h': 1,
'blk': 1,
'out.$': 1,
}, 'sort': {
'blk.i': reverse ? -1 : 1,
'out.s5': reverse ? -1 : 1
}, 'limit': limit, 'skip': skip }, 'r': {
'f': '[.[] | { transaction: .tx.h, block: .blk, alias: .out[0].s2, pointer: .out[0].s3, type: .out[0].s4 , seq: .out[0].s5 }]'
} }
}
function app(txid)
{
return { 'v': 3, 'q': { 'find': {
'tx.h': txid,
'out.s0': {'$exists': false},
'out.s1': {'$exists': true}
}, 'project': {
'out.s1': 1
} }, 'r': {
'f': '.[] | .out[0].s1'
} }
}
function b(txid)
{
return { 'v': 3, 'q': { 'find': {
'out.s1': B_,
'tx.h': txid,
}, 'limit': 1, 'project': {
'blk': 1,
'in.e.a': 1,
'out.$': 1
} }, 'r': {
'f': '.[] | { block: .blk, sender: .in[0].e.a, data: (.out[0].b2 // .out[0].lb2), mime: .out[0].s3, encoding: .out[0].s4, filename: .out[0].s5 }'
} }
}
function bcat(txid)
{
return { 'v': 3, 'q': { 'find': {
'out.s1': BCat_,
'tx.h': txid,
}, 'project': {
'blk': 1,
'in.e.a': 1,
'out.$': 1
} }, 'r': {
'f': '.[] | { block: .blk, sender: .in[0].e.a , info: .out[0].s2, mime: .out[0].s3, encoding: .out[0].s4 , filename: .out[0].s5, flag: .out[0].h6, data: .out[0] }'
} }
}
function c(hash)
{
return { 'v': 3, 'q': { 'find': {
'c': hash
}, 'project': {
'tx': 1
} }, 'r': {
'f': '.[] | .tx.h'
} }
}
function tx(id)
{
return { 'v': 3, 'q': { 'find': {
'tx.h': id,
} }, 'r': {
'f': '.[]'
} }
}
function bcatpart(txid)
{
return { 'v': 3, 'q': { 'find': {
'out.s1': BCatPart_,
'tx.h': txid,
}, 'project': {
'out.$': 1,
} }, 'r': {
'f': '.[] | (.out[0].b2 // .out[0].lb2)'
} }
}
function parseapp(res)
{
res = res.toString()
return (res in APPS) ? APPS[res] : res
}
function parsebcat(res)
{
let r = res
let d = r.data
r.data = []
let i = 7; let chunkid;
if (d.s2 == BCat_) { ++ i; }
while (chunkid = d['h'+i]) {
r.data.push(chunkid)
++ i
}
return r
}
module.exports = {
bitdb: bitdb,
offsetbitdb: offsetbitdb,
autobitdb: autobitdb,
tx: tx,
inaddr: inaddr,
app: app,
b: b,
bcat: bcat,
bcatpart: bcatpart,
c: c,
d: d,
parseapp: parseapp,
parsebcat: parsebcat
}