-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathipc.js
139 lines (114 loc) · 3.2 KB
/
ipc.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
const net = require('net')
const ETH = require('./index')
module.exports = class IPC 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 = typeof socket === 'string' ? net.connect(socket) : socket
this.socket.unref()
let buf = ''
const self = this
this.socket.setEncoding('utf-8')
this.socket.on('data', ondata)
this.socket.on('error', this.socket.destroy)
this.socket.on('close', onclose)
this.destroyed = false
this.ending = null
this.endingResolve = null
function onclose () {
self.destroyed = true
self.socket = null
for (const [resolve, reject] of self.inflight.values()) {
reject(new Error('Socket 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.active()) {
self.socket.unref()
if (self.ending) self.socket.end()
}
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
}
function ondata (data) {
buf += data
while (true) {
const n = buf.indexOf('\n')
if (n === -1) return
if (!onmessage(buf.slice(0, n).trim())) {
self.socket.destroy()
return
}
buf = buf.slice(n + 1)
}
}
}
active () {
return this.inflight.size > 0 || this.subscriptions.size > 0
}
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])
if (this.active()) this.socket.ref()
this.socket.write(JSON.stringify(obj) + '\n')
})
}
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.active()) this.socket.destroy()
})
return this.ending
}
destroy () {
if (this.socket) this.socket.destroy()
}
}