forked from monteslu/wsrpc
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
157 lines (140 loc) · 3.73 KB
/
index.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
const EventEmitter = require('eventemitter3');
const transports = require('./transports');
function rawr({ transport, timeout = 0, handlers = {}, methods, idGenerator }) {
let callId = 0;
// eslint-disable-next-line no-param-reassign
methods = methods || handlers; // backwards compat
const pendingCalls = {};
const methodHandlers = {};
const notificationEvents = new EventEmitter();
notificationEvents.on = notificationEvents.on.bind(notificationEvents);
transport.on('rpc', (msg) => {
if (msg.id) {
// handle an RPC request
if (msg.params && methodHandlers[msg.method]) {
methodHandlers[msg.method](msg);
return;
}
// handle an RPC result
const promise = pendingCalls[msg.id];
if (promise) {
if (promise.timeoutId) {
clearTimeout(promise.timeoutId);
}
delete pendingCalls[msg.id];
if (msg.error) {
promise.reject(msg.error);
}
return promise.resolve(msg.result);
}
return;
}
// handle a notification
msg.params.unshift(msg.method);
notificationEvents.emit(...msg.params);
});
function addHandler(methodName, handler) {
methodHandlers[methodName] = (msg) => {
Promise.resolve()
.then(() => {
return handler.apply(this, msg.params);
})
.then((result) => {
transport.send({
id: msg.id,
result
});
})
.catch((error) => {
const serializedError = { message: error.message };
if (error.code) {
serializedError.code = error.code;
}
transport.send({
id: msg.id,
error: serializedError
});
});
};
}
Object.keys(methods).forEach((m) => {
addHandler(m, methods[m]);
});
function sendMessage(method, params, config) {
const id = idGenerator ? idGenerator() : ++callId;
const msg = {
jsonrpc: '2.0',
method,
params,
id
};
let timeoutId;
if (config.timeout || timeout) {
timeoutId = setTimeout(() => {
if (pendingCalls[id]) {
const err = new Error('RPC timeout');
err.code = 504;
pendingCalls[id].reject(err);
delete pendingCalls[id];
}
}, config.timeout || timeout);
}
const response = new Promise((resolve, reject) => {
pendingCalls[id] = { resolve, reject, timeoutId };
});
transport.send(msg, config);
return response;
}
const methodsProxy = new Proxy({}, {
get: (target, name) => {
return (...args) => {
return sendMessage(name, args, {});
};
}
});
const configurableMethodsProxy = new Proxy({}, {
get: (target, name) => {
return (...args) => {
let config;
if (args.length) {
const testArg = args.pop();
if (testArg && typeof testArg === 'object' && !Array.isArray(testArg)) {
config = testArg;
}
}
return sendMessage(name, args, config || {});
};
}
});
const notifiers = new Proxy({}, {
get: (target, name) => {
return (...args) => {
const msg = {
jsonrpc: '2.0',
method: name,
params: args
};
transport.send(msg);
};
}
});
const notifications = new Proxy({}, {
get: (target, name) => {
return (callback) => {
notificationEvents.on(name.substring(2), (...args) => {
return callback.apply(callback, args);
});
};
}
});
return {
methods: methodsProxy,
methodsExt: configurableMethodsProxy,
addHandler,
notifications,
notifiers,
transport,
};
}
rawr.transports = transports;
module.exports = rawr;