-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathws.js
131 lines (105 loc) · 2.95 KB
/
ws.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
const ETH = require('./index')
module.exports = class WS extends ETH {
constructor (socket) {
super(new RPC(socket))
}
}
class RPC {
constructor (socket) {
this.id = 0
this.inflight = new Map()
this.subscriptions = new Map()
this.socket = socket
const self = this
on(this.socket, 'message', onmessage)
on(this.socket, 'close', onclose)
on(this.socket, 'open', onopen)
this.destroyed = false
this.ending = null
this.endingResolve = null
this.queued = []
this.opened = false
function onopen () {
self.opened = true
for (const s of self.queued) self.socket.send(s)
}
function onclose () {
self.destroyed = true
self.socket = null
for (const [resolve, reject] of self.inflight.values()) {
reject(new Error('WebSocket destroyed'))
}
if (self.endingResolve) self.endingResolve()
}
function onmessage (message) {
let obj
try {
obj = JSON.parse(message)
} catch (_) {
return false
}
if (obj.method === 'parity_subscription') {
const cb = self.subscriptions.get(obj.params.subscription)
if (cb != null) {
if (obj.params.error) {
const err = new Error(obj.params.error.message)
err.code = obj.params.error.code
cb(err)
return true
}
cb(null, obj.params.result)
return true
}
}
const p = self.inflight.get(obj.id)
if (!p) return false
self.inflight.delete(obj.id)
if (self.inflight.size === 0) {
if (self.ending) self.socket.close()
}
if (obj.error) {
const err = new Error(obj.error.message)
err.code = obj.error.code
p[1](err)
return true
}
p[0](obj.result)
return true
}
}
request (method, params) {
if (!this.socket) return Promise.reject(new Error('Socket destroyed'))
const id = '' + ++this.id
const obj = { jsonrpc: '2.0', id, method, params }
return new Promise((resolve, reject) => {
this.inflight.set(id, [resolve, reject])
const s = JSON.stringify(obj)
if (!this.opened) this.queued.push(s)
else this.socket.send(s)
})
}
async subscribe (method, params, cb) {
if (cb == null) return this.subscribe(method, [], params)
const id = await this.request('parity_subscribe', [method, params])
this.subscriptions.set(id, cb)
return async () => {
await this.request('parity_unsubscribe', [id])
this.subscriptions.delete(id)
}
}
end () {
this.ending = new Promise(resolve => {
if (!this.socket) return resolve()
this.endingResolve = resolve
if (this.inflight.size === 0) this.socket.close()
})
return this.ending
}
destroy () {
if (this.socket) this.socket.close()
}
}
function on (e, name, fn) {
if (e.on) e.on(name, fn)
else if (e.addEventListener) e.addEventListener(name, fn)
}