-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathtxt.js
71 lines (71 loc) · 1.61 KB
/
txt.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
const axios = require('axios')
class Txt {
constructor(o) {
if (!o) {
console.log("txt config required")
process.exit(1)
}
if (!o.url) {
console.log("txt url required")
process.exit(1)
}
this.url = o.url
this.channel = (o.channel ? o.channel : "txt")
this.headers = o.headers
this.queue = new Set()
}
async last () {
try {
let res = await axios.get(this.url + "/_/_/json?at=-null&limit=1")
console.log("#last = ", res.data)
if (res.data.result.length > 0) {
return res.data.result[0].i
} else {
return null;
}
} catch (e) {
console.log("error : last()", e)
process.exit(1)
}
}
async status () {
try {
let res = await axios.get(this.url + "/status")
if (res.data) {
return res.data
} else {
return null;
}
} catch (e) {
console.log("error : status()", e)
process.exit(1)
}
}
async start (fn) {
for (let ev of this.queue) {
let o = ev.data
let f = fn[ev.type]
if (!f) {
f = (x) => {
return {}
}
}
let result = await f(o)
if (result) {
let channel = (result.channel ? result.channel : this.channel)
if (result.channel) delete result.channel
let res = await axios.post(this.url + "/api", {
channel: channel,
set: { [o.tx.h]: result }
}, { headers: this.headers })
} else {
// ignored item
}
this.queue.delete(ev)
}
setTimeout(() => {
this.start(fn)
}, 1000)
};
};
module.exports = Txt