-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathindex.js
343 lines (282 loc) · 9.44 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
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
'use strict';
// Request body defination
// {
// articles: {
// method: 'muse.articles.index',
// params: 'any',
// dependencies: [
// 'create'
// ]
// },
// create: {
// method: 'muse.articles.index',
// params: 'any',
// dependencies: []
// }
// }
const _ = require('lodash');
const assert = require('assert');
const mm = require('micromatch');
const semver = require('semver');
// Constant
const DEFAULT_GATEWAY_METHOD_NAME = '__gateway__';
const DEFAULT_GATEWAY_ROUTE_PATH = 'gateway';
const DEFAULT_GATEWAY_HTTP_METHOD = 'post';
const MINIMAL_VERSION_REQUIRED = '0.8.25';
// Create custom error with statusCode
function createError(statusCode, message) {
let err = new Error(message);
err.statusCode = err.status = statusCode == null ? 500 : statusCode;
err.toJSON = function() {
return {
statusCode: err.statusCode,
status: err.status,
message: err.message
};
};
return err;
}
// Cast val to array
function castToArray(val) {
val = val || [];
val = Array.isArray(val) ? val : (val == null ? [] : [val]);
return val;
}
// allowedAPIs: Whitelist api list, support unix glob pattern
// forbiddenAPIs: Blacklist api list, support unix glob pattern
// max: Maximum requests for once
/**
* Baiji Gateway Plugin
*
* @param {Application} app baiji Application instance
* @param {Object} options
* @param {Array} options.allowedAPIs Whitelist api list, support unix glob pattern
* @param {Array} options.forbiddenAPIs Blacklist api list, support unix glob pattern
* @param {Number} options.max Maximum requests for once
* @param {String} options.name Custom gateway method name
* @param {String} options.path Custom gateway route path
* @param {String} options.verb Custom gateway http method
*
* @public
*/
module.exports = function baijiGatewayPlugin(app, options) {
// Check baiji version
assert(
semver.satisfies(app.constructor.VERSION, `>= ${MINIMAL_VERSION_REQUIRED}`),
`baiji-gateway plugin require baiji version larger than ${MINIMAL_VERSION_REQUIRED}`
);
options = Object.assign({}, options);
options.max = options.max || 50;
options.allowedAPIs = castToArray(options.allowedAPIs);
options.forbiddenAPIs = castToArray(options.forbiddenAPIs);
options.name = options.name || DEFAULT_GATEWAY_METHOD_NAME;
options.path = options.path || DEFAULT_GATEWAY_ROUTE_PATH;
options.verb = options.verb || DEFAULT_GATEWAY_HTTP_METHOD;
if (options.onError) {
assert(
typeof options.onError === 'function',
'`options.onError` must be a valid function'
);
}
// Cache all methods
let ALL_METHODS = [];
// Filter all methods according to `allowedAPIs` and `forbiddenAPIs` option
function filterAllowedMethods(app) {
let allMethods = {};
let allMethodNames = [];
app.composedMethods().map(method => {
// Ignore gateway method
if (method.name === options.name) return;
let methodName = method.fullName();
allMethodNames.push(methodName);
allMethods[methodName] = method;
});
// Filter all methods
let filteredMethods = _.difference(allMethodNames, mm(allMethodNames, options.forbiddenAPIs));
filteredMethods = mm(filteredMethods, options.allowedAPIs);
return _.pick(allMethods, filteredMethods);
}
// Invoke method by name
function invokeApiByName(name, ctx, args) {
let method = ALL_METHODS[name];
// Create mock context to imitate actual context
let mockCtx = ctx.adapter.Context.create(
ctx.request,
ctx.response,
method,
_.assign({}, ctx.options, { argsBuilt: true })
).markAsMock().setArgs(args || {}, true);
return new Promise(function(resolve, reject) {
mockCtx.on('error', function(res) {
reject(res.error);
});
mockCtx.on('finish', function(res) {
resolve(res.result);
});
// Invoke method with mocked context
method.invoke(mockCtx);
});
}
// Execute apis by specific orders
// [promise0, [promise1, promise2], promise3, promise4]
function execStack(stack) {
stack = castToArray(stack);
let next = stack.shift();
let promise = Promise.resolve();
if (next) {
if (Array.isArray(next)) {
promise = Promise.all(next.map(fn => fn()));
} else {
promise = next();
}
return promise.then(() => {
return execStack(stack);
});
} else {
return promise;
}
}
// Execute all api requests according to definitions
function executeApis(schema, ctx) {
schema = schema || {};
let result = {};
let stack = _(schema)
// Convert all api dependencies to array
.map(function(api, name) {
api = api || {};
api.dependencies = castToArray(api.dependencies);
return { name, api };
})
// Compare api priority by dependencies
.tap(function(res) {
return res.sort(function(a, b) {
let a_dep = a.api.dependencies;
let b_dep = b.api.dependencies;
if (b_dep.indexOf(a.name) > -1) return -1;
if (a_dep.indexOf(b.name) > -1) return 1;
if (a_dep.length === 0 && b_dep.length !== 0) return -1;
if (b_dep.length === 0 && a_dep.length !== 0) return 1;
if (a_dep.length < b_dep.length) return -1;
if (a_dep.length > b_dep.length) return 1;
if (a.name > b.name) return -1;
if (a.name < b.name) return 1;
return 0;
});
})
// Group apis by dependencies
.tap(function(res) {
let group = [];
let order = [];
res.map(item => {
let key = item.api.dependencies.map(String).join('.');
if (order.indexOf(key) === -1) order.push(key);
let index = order.indexOf(key);
if (!group[index]) group[index] = [];
group[index].push(item);
});
return group;
})
// Convert to executeable stack
.map(function(item) {
item = castToArray(item);
return item.map(i => {
return function apiInvoker() {
return invokeApiByName(i.api.method, ctx, i.api.params)
.then(res => {
result[i.name] = res;
}).catch(err => {
result[i.name] = err;
});
};
});
}).value();
return execStack(stack).then(() => result);
}
// Validate schema and return proper error
function validateSchema(schema) {
schema = schema || {};
let forbiddenAPIs = options.forbiddenAPIs;
let allowedAPIs = options.allowedAPIs;
let forbidden = false;
let hasError;
let apis = [];
_.map(schema, (api, name) => {
// Check if api not contains a valid method
if (!api.method) hasError = true;
// Check if api depends on itself
if (
api.dependencies &&
api.dependencies.length &&
~api.dependencies.indexOf(name)
) hasError = true;
if (!~apis.indexOf(api.method)) apis.push(api.method);
});
let apisCount = apis.length;
// Check whether the apis count is within tolerable range
if (apisCount === 0 || hasError) return createError(422, 'Invalid Request Body');
if (apisCount > options.max) return createError(406, 'Max Requests Exceeded');
// Check whether the apis are within whitelist or without blacklist
// Blacklist has heigher priority
if (forbiddenAPIs.length && mm(apis, forbiddenAPIs).length) forbidden = true;
if (allowedAPIs.length && mm(apis, allowedAPIs).length !== apis.length) forbidden = true;
if (forbidden) return createError(403, 'Forbidden Request');
}
// Build special notes for swagger
function buildNotes() {
let title = 'Baiji gateway plugin method';
let maxLength = 0;
_.map(ALL_METHODS, (method, name) => {
let length = name.length;
if (maxLength < length) maxLength = length;
});
let notes = _.map(ALL_METHODS, (method, name) => {
let paddedName = _.padEnd(`${name}`, maxLength, ' ');
return `${paddedName} => ${method.description}`;
}).join('\n');
return `## ${title}\n\n### Support Methods:\n\`\`\`\n${notes} \n\`\`\``;
}
// Force obj1 key order by obj2
function forceOrder(obj1, obj2) {
let obj = {};
_.map(obj2, function(val, key) {
obj[key] = obj1[String(key)];
});
return obj;
}
// Refresh Gateway methods after mounted
app.on('mount', function() {
ALL_METHODS = filterAllowedMethods(this);
// Define Gateway main method
this.define(options.name, {
description: 'Baiji gateway plugin method',
notes: buildNotes(),
route: { path: options.path, verb: options.verb }
}, function(ctx, next) {
let schema = ctx.body || {};
let error = validateSchema(schema);
if (error) {
if (options.onError) {
return options.onError(error, ctx, next);
} else {
ctx.status(error.status);
return ctx.done(error, next);
}
} else {
return executeApis(schema, ctx).then(res => {
// Force result has the same order with request schema
res = forceOrder(res, schema);
// Make sure result has same type of schema
let data = Array.isArray(schema) ? _.values(res) : res;
ctx.done(data, next);
}).catch(err => {
if (err && options.onError) {
return options.onError(err, ctx, next);
}
ctx.status(err.status);
return ctx.done(err, next);
});
}
});
});
return null;
};