-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
225 lines (207 loc) · 6.51 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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
const debug = require('debug')('koa-json-rpc');
const compose = require('koa-compose');
const Jsonrpc = require('./lib/jsonrpc');
const handler = async (router, context, next, requestObject, responseKey) => {
debug('start handle: %o', requestObject);
if (!Jsonrpc.requestIsValid(requestObject)) {
const result = Jsonrpc.handleInvalidRequest(requestObject);
debug('result for %o: %o', requestObject, result);
return context.state.jsonRpcResponses[responseKey] = result;
}
const jsonrpc = new Jsonrpc({request: requestObject});
const _ctx = Object.create(context, {
body: {
configurable: false,
get: function () {
return context.state.jsonRpcResponses[responseKey];
},
set: function (val) {
if (Jsonrpc.responseIsValid(val)) {
context.state.jsonRpcResponses[responseKey] = val;
} else {
jsonrpc.result = val;
context.state.jsonRpcResponses[responseKey] = jsonrpc.response;
}
}
}
});
_ctx.jsonrpc = jsonrpc;
let {proxy, revoke} = Proxy.revocable(_ctx, {
set (target, prop, val, receiver) {
if ('body' === prop) {
target[prop] = val;
} else {
context[prop] = val;
}
return true;
}
});
const ctx = proxy;
if (router._handlers[requestObject.method]) {
await Promise.resolve(router._handlers[requestObject.method](ctx, next))
.catch(async err => { // err.message err.name err.code err.status err.stack...
if (router.onerror) {
try {
debug(requestObject.method + ': trying handle error');
await router.onerror(err, ctx);
} catch (err) {
debug(requestObject.method + ': onerror is down: %o', err);
ctx.jsonrpc.serverError(null, err);
ctx.body = ctx.jsonrpc.response;
}
} else {
ctx.jsonrpc.serverError(null, ('status' in err) ? err : err.toString());
ctx.body = ctx.jsonrpc.response;
}
});
if (Jsonrpc.isNotification(requestObject)) {
ctx.body = null;
}
revoke();
} else { // TODO: does this case is possible ?
context.state.jsonRpcResponses[responseKey] = {
jsonrpc: '2.0',
id: ctx.jsonrpc.id || null,
error: {
code: -32601,
message: 'Method not found',
}
};
revoke();
}
};
module.exports = class Router {
constructor (props = {}) {
debug('creating new json-rpc');
debug('props: %o', props);
this._handlers = {};
this.onerror = props.onerror;
this.parallel = Boolean(props.parallel) || true;
this.bodyParser = props.bodyParser;
}
static get parseError () {
return Jsonrpc.parseError;
}
method (method, ...middlewares) {
debug('register method: %s', method);
if (!Jsonrpc.methodIsValid(method)) {
throw new Error('"method" must be string containing the name of the method to be invoked and cannot starts with "rpc."');
}
this._handlers[method] = compose(middlewares);
return this;
}
get methods () {
return Object.keys(this._handlers);
}
hasAllHandlersForRequest (reqBody) {
debug('checking for handlers for a %0', reqBody);
if (Array.isArray(reqBody)) {
return reqBody.every(req => {
if (!isObject(req)) return true;
if (isObject(req) && ('method' in req) && this._handlers[req.method]) return true;
return false;
});
}
if (isObject(reqBody)) {
if (!('method' in reqBody)) return true;
if (this._handlers[reqBody.method]) return true;
if (!Jsonrpc.methodIsValid(reqBody.method)) return true;
return false;
}
}
get middleware () {
const middleware = async (ctx, next) => {
if (!ctx.request.body) {
return ctx.body = Jsonrpc.parseError;
}
const parallel = this.parallel;
debug('request: %o', ctx.request.body);
let isInitialMiddleware = false;
if (!ctx.state.jsonRpcResponses) {
isInitialMiddleware = true;
ctx.state.jsonRpcResponses = Array.isArray(ctx.request.body) ? {...ctx.request.body.map(v => undefined)} : {0: undefined};
}
debug('is initial middleware: ', isInitialMiddleware);
if (Array.isArray(ctx.request.body)) {
ctx.type = 'json';
if (!ctx.request.body.length) {
ctx.state.jsonRpcResponses['0'] = Jsonrpc.invalidRequest;
} else {
if (parallel) {
let currentRouterHandlers = [];
if (this.hasAllHandlersForRequest(ctx.request.body)) {
currentRouterHandlers = ctx.request.body.map((rpcReq, i) => handler(this, ctx, next, rpcReq, i));
} else {
currentRouterHandlers = ctx.request.body.reduce((acc, rpcReq, i, body) => {
if (!isObject(rpcReq)) {
acc.push(handler(this, ctx, next, rpcReq, i));
return acc;
}
if (Boolean(this._handlers[rpcReq.method]) || !('method' in rpcReq)) {
acc.push(handler(this, ctx, next, rpcReq, i));
return acc;
}
return acc;
}, []).concat(parallel ? next() : []);
}
await Promise.all(currentRouterHandlers);
} else {
for (let i = 0, l = ctx.request.body.length; i < l; i++) {
const rpcReq = ctx.request.body[i];
if (!Jsonrpc.requestIsValid(rpcReq) || this.hasAllHandlersForRequest(rpcReq)) {
await handler(this, ctx, next, rpcReq, i)
}
}
if (Object.values(ctx.state.jsonRpcResponses).filter(v => v === undefined).length) {
await next();
}
}
}
} else {
if (this.hasAllHandlersForRequest(ctx.request.body)) {
await handler(this, ctx, next, ctx.request.body, 0);
} else {
await next();
}
}
if (isInitialMiddleware) {
ctx.status = 200;
let finalResult = isBatch(ctx.request.body) ? [] : ctx.state.jsonRpcResponses[0];
if (isBatch(ctx.request.body)) { // batch
for (let i in ctx.state.jsonRpcResponses) {
const res = ctx.state.jsonRpcResponses[i];
if (res === undefined) {
let response = Jsonrpc.methodNotFound;
response.id = (Array.isArray(ctx.request.body) ? ctx.request.body[Number(i)].id : ctx.request.body.id) || null;
finalResult.push(response);
continue;
}
if (res !== null) {
finalResult.push(res);
}
}
if (!finalResult.length) finalResult = null;
} else if (undefined === finalResult) {
let response = Jsonrpc.methodNotFound;
response.id = ctx.request.body.id || null;
finalResult = response;
}
if (null !== finalResult) {
debug('response: %o', finalResult);
ctx.body = finalResult;
}
delete ctx.state.jsonRpcResponses;
}
};
if (this.bodyParser) {
return compose([this.bodyParser, middleware]);
}
return middleware;
}
};
function isObject (obj) {
return '[object Object]' === Object.prototype.toString.call(obj);
}
function isBatch (req) {
return Array.isArray(req) && req.length;
}