Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
jajbshjahavahh
GitHub Repository: jajbshjahavahh/Gojo-Satoru
Path: blob/master/lib/cloudDBAdapter.js
2591 views
1
//═══════════════════════════════════════════════════════//
2
//If you want to recode, reupload
3
//or copy the codes/script,
4
//pls give credit
5
//no credit? i will take action immediately
6
//© 2022 Xeon Bot Inc. Cheems Bot MD
7
//Thank you to Lord Buddha, Family and Myself
8
//════════════════════════════//
9
const got = require('got')
10
11
const stringify = obj => JSON.stringify(obj, null, 2)
12
const parse = str => JSON.parse(str, (_, v) => {
13
if (
14
v !== null &&
15
typeof v === 'object' &&
16
'type' in v &&
17
v.type === 'Buffer' &&
18
'data' in v &&
19
Array.isArray(v.data)) {
20
return Buffer.from(v.data)
21
}
22
return v
23
})
24
class CloudDBAdapter {
25
constructor(url, {
26
serialize = stringify,
27
deserialize = parse,
28
fetchOptions = {}
29
} = {}) {
30
this.url = url
31
this.serialize = serialize
32
this.deserialize = deserialize
33
this.fetchOptions = fetchOptions
34
}
35
36
async read() {
37
try {
38
let res = await got(this.url, {
39
method: 'GET',
40
headers: {
41
'Accept': 'application/json;q=0.9,text/plain'
42
},
43
...this.fetchOptions
44
})
45
if (res.statusCode !== 200) throw res.statusMessage
46
return this.deserialize(res.body)
47
} catch (e) {
48
return null
49
}
50
}
51
52
async write(obj) {
53
let res = await got(this.url, {
54
method: 'POST',
55
headers: {
56
'Content-Type': 'application/json'
57
},
58
...this.fetchOptions,
59
body: this.serialize(obj)
60
})
61
if (res.statusCode !== 200) throw res.statusMessage
62
return res.body
63
}
64
}
65
66
module.exports = CloudDBAdapter
67
68