-
Notifications
You must be signed in to change notification settings - Fork 0
/
tinyrpc.ts
203 lines (178 loc) · 4.54 KB
/
tinyrpc.ts
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
import beson from "beson";
import fp from "fastify-plugin";
const DEFAULT_SIZE_LIMIT = 10 * 1024 * 1024;
interface InvokeSession { request:import('fastify').FastifyRequest; response:import('fastify').FastifyReply; }
interface CallHandler {(this:InvokeSession, ...args:any[]):Promise<any>|any};
interface CallMap {
[call:string]:CallHandler|{
handler:CallHandler;
args_checker?:{(this:InvokeSession, ...args:any[]):true|string[]};
};
}
interface TinyRPCOptions {
handlers?:CallMap;
size_limit?:number;
};
const plugin:import('fastify').FastifyPluginAsync<TinyRPCOptions> = async(fastify, options)=>{
const HandlerMap = (options.handlers||{}) as CallMap;
fastify.addContentTypeParser('application/beson', {parseAs:'buffer', bodyLimit:options.size_limit||DEFAULT_SIZE_LIMIT}, (request, body, done)=>{
const payload = beson.Deserialize(body as Buffer);
if ( payload === undefined ) {
throw Error("Given payload is not formatted in BESON!");
}
done(payload);
});
type PayloadType = {
rpc?:'1.0';
id?:string;
call:string;
args:Array<any>
};
fastify.post<{Body:PayloadType}>('/', async(req, res)=>{
const payload = req.body;
if ( Object(payload) !== payload ) {
return res.status(400).send({
rpc:'1.0',
error: {
code: 'rpc#invalid-payload-format',
message: 'Request body must be an object'
}
});
}
const {rpc, id, call, args} = req.body;
if ( rpc !== undefined ) {
if ( typeof rpc !== "string" ) {
return res.status(400).send({
rpc:'1.0',
error: {
code: 'rpc#invalid-payload-format',
message: 'Field `rpc` is not a valid version string!'
}
});
}
if ( rpc !== "1.0" ) {
return res.status(400).send({
rpc:'1.0',
error: {
code: 'rpc#invalid-payload-format',
message: 'Field `rpc` only support "1.0" version!'
}
});
}
}
if ( id !== undefined ) {
if ( typeof id !== "string" && typeof id !== "number" ) {
return res.status(400).send({
rpc:'1.0',
error: {
code: 'rpc#invalid-payload-format',
message: 'Field `id` only accepts strings and integers!'
}
});
}
if ( typeof id === "number" && !Number.isInteger(id) ) {
return res.status(400).send({
rpc:'1.0',
error: {
code: 'rpc#invalid-payload-format',
message: 'Field `id` must be an integer!'
}
});
}
}
if ( typeof call !== "string" ) {
return res.status(400).send({
rpc:'1.0',
error: {
code: 'rpc#invalid-payload-format',
message: 'Field `call` must be an string!'
}
});
}
if ( !Array.isArray(args) ) {
return res.status(400).send({
rpc:'1.0',
error: {
code: 'rpc#invalid-payload-format',
message: 'Field `args` must be an array!'
}
});
}
const rpc_info = HandlerMap[call];
if ( rpc_info === undefined || Object(rpc_info) !== rpc_info || (typeof rpc_info !== "function" && typeof rpc_info.handler !== "function") ) {
return res.send({
rpc:'1.0',
id,
error: {
code: 'rpc#call-not-found',
message:'Request call is not defined!',
data: {call, args}
}
});
}
const request_session:InvokeSession = {request:req, response:res};
let handler:CallHandler;
if ( typeof rpc_info === "function" ) {
handler = rpc_info;
}
else {
handler = rpc_info.handler;
if ( rpc_info.args_checker ) {
try {
const result = rpc_info.args_checker.call(request_session, ...args);
if ( result !== true ) {
return res.send({
rpc:'1.0',
id,
error: {
code: 'rpc#invalid-call-args',
message: "Some of the arguments are invalid!",
data: result
}
});
}
}
catch(e:any) {
const err:Error&{detail?:any} = e;
return res.send({
rpc:'1.0',
id,
error: {
code: 'rpc#invalid-call-args',
message: err.message,
data: err.detail||undefined
}
});
}
}
}
const result = await Promise.resolve().then(()=>handler.call(request_session, ...args)).catch((e:any)=>e);
if ( result instanceof Error ) {
const err:Error&{code?:string;detail?:any} = result;
return res.send({
rpc:'1.0',
id,
error: {
code: err.code||'rpc#call-exec-error',
message: err.message,
data: err.detail||undefined
}
});
}
return res.send({
rpc:'1.0',
id,
result
});
});
fastify.setErrorHandler(async(error, req, res)=>{
return res.status(400).send({
rpc:'1.0',
error: {
code: 'rpc#invalid-payload-format',
message: error.message
}
});
});
};
export = fp(plugin);