@faasjs/http 0.0.2-beta.3 → 0.0.2-beta.319

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/lib/index.es.js DELETED
@@ -1,518 +0,0 @@
1
- import deepMerge from '@faasjs/deep_merge';
2
- import Logger from '@faasjs/logger';
3
- import { randomBytes, pbkdf2Sync, createCipheriv, createHmac, createDecipheriv } from 'crypto';
4
-
5
- class Session {
6
- constructor(cookie, config) {
7
- this.cookie = cookie;
8
- this.config = Object.assign({
9
- key: 'key',
10
- secret: randomBytes(128).toString('hex'),
11
- salt: 'salt',
12
- signedSalt: 'signedSalt',
13
- keylen: 64,
14
- iterations: 100,
15
- digest: 'sha256',
16
- cipherName: 'aes-256-cbc'
17
- }, config);
18
- this.secret = pbkdf2Sync(this.config.secret, this.config.salt, this.config.iterations, this.config.keylen / 2, this.config.digest);
19
- this.signedSecret = pbkdf2Sync(this.config.secret, this.config.signedSalt, this.config.iterations, this.config.keylen, this.config.digest);
20
- this.content = Object.create(null);
21
- }
22
- invoke(cookie) {
23
- try {
24
- this.content = cookie ? this.decode(cookie) : Object.create(null);
25
- }
26
- catch (error) {
27
- console.error(error);
28
- this.content = Object.create(null);
29
- }
30
- this.changed = false;
31
- }
32
- encode(text) {
33
- if (typeof text !== 'string') {
34
- text = JSON.stringify(text);
35
- }
36
- const iv = randomBytes(16);
37
- const cipher = createCipheriv(this.config.cipherName, this.secret, iv);
38
- const encrypted = Buffer.concat([cipher.update(text), cipher.final()]).toString('base64');
39
- const main = Buffer.from([encrypted, iv.toString('base64')].join('--')).toString('base64');
40
- const hmac = createHmac(this.config.digest, this.signedSecret);
41
- hmac.update(main);
42
- const digest = hmac.digest('hex');
43
- return main + '--' + digest;
44
- }
45
- decode(text) {
46
- text = decodeURIComponent(text);
47
- const signedParts = text.split('--');
48
- const hmac = createHmac(this.config.digest, this.signedSecret);
49
- hmac.update(signedParts[0]);
50
- const digest = hmac.digest('hex');
51
- if (signedParts[1] !== digest) {
52
- throw Error('Not valid');
53
- }
54
- const message = Buffer.from(signedParts[0], 'base64').toString();
55
- const parts = message.split('--').map(function (part) {
56
- return Buffer.from(part, 'base64');
57
- });
58
- const cipher = createDecipheriv(this.config.cipherName, this.secret, parts[1]);
59
- const part = Buffer.from(cipher.update(parts[0])).toString('utf8');
60
- const final = cipher.final('utf8');
61
- const decryptor = [part, final].join('');
62
- return JSON.parse(decryptor);
63
- }
64
- read(key) {
65
- return this.content[key];
66
- }
67
- write(key, value) {
68
- if (value === null || typeof value === 'undefined') {
69
- delete this.content[key];
70
- }
71
- else {
72
- this.content[key] = value;
73
- }
74
- this.changed = true;
75
- return this;
76
- }
77
- update() {
78
- if (this.changed) {
79
- this.cookie.write(this.config.key, this.encode(JSON.stringify(this.content)));
80
- }
81
- return this;
82
- }
83
- }
84
-
85
- class Cookie {
86
- constructor(config) {
87
- this.config = deepMerge({
88
- path: '/',
89
- expires: 31536000,
90
- secure: true,
91
- httpOnly: true,
92
- session: {}
93
- }, config);
94
- this.session = new Session(this, this.config.session);
95
- this.content = Object.create(null);
96
- this.setCookie = Object.create(null);
97
- }
98
- invoke(cookie) {
99
- this.content = Object.create(null);
100
- // 解析 cookie
101
- if (cookie) {
102
- cookie.split(';').map((x) => {
103
- x = x.trim();
104
- const k = x.match(/([^=]+)/);
105
- if (k !== null) {
106
- this.content[k[0]] = decodeURIComponent(x.replace(`${k[0]}=`, '').replace(/;$/, ''));
107
- }
108
- });
109
- }
110
- this.setCookie = Object.create(null);
111
- // 预读取 session
112
- this.session.invoke(this.read(this.session.config.key));
113
- return this;
114
- }
115
- read(key) {
116
- return this.content[key];
117
- }
118
- write(key, value, opts) {
119
- opts = Object.assign(this.config, opts || {});
120
- let cookie;
121
- if (value === null || typeof value === 'undefined') {
122
- opts.expires = 'Thu, 01 Jan 1970 00:00:01 GMT';
123
- cookie = `${key}=;`;
124
- delete this.content[key];
125
- }
126
- else {
127
- cookie = `${key}=${encodeURIComponent(value)};`;
128
- this.content[key] = value;
129
- }
130
- if (typeof opts.expires === 'number') {
131
- cookie += `max-age=${opts.expires};`;
132
- }
133
- else if (typeof opts.expires === 'string') {
134
- cookie += `expires=${opts.expires};`;
135
- }
136
- cookie += `path=${opts.path || '/'};`;
137
- if (opts.domain) {
138
- cookie += `domain=${opts.domain};`;
139
- }
140
- if (opts.secure) {
141
- cookie += 'Secure;';
142
- }
143
- if (opts.httpOnly) {
144
- cookie += 'HttpOnly;';
145
- }
146
- if (opts.sameSite) {
147
- cookie += `SameSite=${opts.sameSite};`;
148
- }
149
- this.setCookie[key] = cookie;
150
- return this;
151
- }
152
- headers() {
153
- if (!Object.keys(this.setCookie).length) {
154
- return {};
155
- }
156
- else {
157
- return {
158
- 'Set-Cookie': Object.values(this.setCookie)
159
- };
160
- }
161
- }
162
- }
163
-
164
- class ValidError extends Error {
165
- constructor({ statusCode, headers, message }) {
166
- super(message);
167
- this.statusCode = statusCode || 500;
168
- this.headers = headers || Object.create(null);
169
- }
170
- }
171
- class Validator {
172
- constructor(config) {
173
- this.paramsConfig = config.params;
174
- this.cookieConfig = config.cookie;
175
- this.sessionConfig = config.session;
176
- this.logger = new Logger('Http.Validator');
177
- }
178
- valid({ params, cookie, session }) {
179
- this.request = {
180
- params,
181
- cookie,
182
- session
183
- };
184
- this.logger.debug('Begin');
185
- if (this.paramsConfig) {
186
- this.logger.debug('Valid params');
187
- this.validContent('params', params, '', this.paramsConfig);
188
- }
189
- if (this.cookieConfig) {
190
- this.logger.debug('Valid cookie');
191
- if (!cookie) {
192
- throw Error('Not found Cookie');
193
- }
194
- this.validContent('cookie', cookie.content, '', this.cookieConfig);
195
- }
196
- if (this.sessionConfig) {
197
- this.logger.debug('Valid Session');
198
- if (!session) {
199
- throw Error('Not found Session');
200
- }
201
- this.validContent('session', session.content, '', this.sessionConfig);
202
- }
203
- }
204
- validContent(type, params, baseKey, config) {
205
- if (config.whitelist) {
206
- const paramsKeys = Object.keys(params);
207
- const rulesKeys = Object.keys(config.rules);
208
- const diff = paramsKeys.filter(k => !rulesKeys.includes(k));
209
- if (diff.length) {
210
- if (config.whitelist === 'error') {
211
- const diffKeys = diff.map(k => `${baseKey}${k}`);
212
- const error = Error(`[${type}] Unpermitted keys: ${diffKeys.join(', ')}`);
213
- if (config.onError) {
214
- const res = config.onError(`${type}.whitelist`, baseKey, diffKeys);
215
- if (res) {
216
- throw new ValidError(res);
217
- }
218
- }
219
- throw error;
220
- }
221
- else if (config.whitelist === 'ignore') {
222
- for (const key of diff) {
223
- delete params[key];
224
- }
225
- }
226
- }
227
- }
228
- for (const key in config.rules) {
229
- const rule = config.rules[key];
230
- let value = params[key];
231
- // default
232
- if (rule.default) {
233
- if (type === 'cookie' || type === 'session') {
234
- this.logger.warn('Cookie and Session not support default rule.');
235
- }
236
- else if (typeof value === 'undefined' && rule.default) {
237
- value = typeof rule.default === 'function' ? rule.default(this.request) : rule.default;
238
- params[key] = value;
239
- }
240
- }
241
- // required
242
- if (rule.required === true) {
243
- if (typeof value === 'undefined' || value === null) {
244
- const error = Error(`[${type}] ${baseKey}${key} is required.`);
245
- if (config.onError) {
246
- const res = config.onError(`${type}.rule.required`, `${baseKey}${key}`, value);
247
- if (res) {
248
- throw new ValidError(res);
249
- }
250
- }
251
- throw error;
252
- }
253
- }
254
- if (typeof value !== 'undefined' && value !== null) {
255
- // type
256
- if (rule.type) {
257
- if (type === 'cookie') {
258
- this.logger.warn('Cookie not support type rule');
259
- }
260
- else {
261
- let typed = true;
262
- switch (rule.type) {
263
- case 'array':
264
- typed = Array.isArray(value);
265
- break;
266
- case 'object':
267
- typed = Object.prototype.toString.call(value) === '[object Object]';
268
- break;
269
- default:
270
- typed = typeof value === rule.type;
271
- break;
272
- }
273
- if (!typed) {
274
- const error = Error(`[${type}] ${baseKey}${key} must be a ${rule.type}.`);
275
- if (config.onError) {
276
- const res = config.onError(`${type}.rule.type`, `${baseKey}${key}`, value);
277
- if (res) {
278
- throw new ValidError(res);
279
- }
280
- }
281
- throw error;
282
- }
283
- }
284
- }
285
- // in
286
- if (rule.in && !rule.in.includes(value)) {
287
- const error = Error(`[${type}] ${baseKey}${key} must be in ${rule.in.join(', ')}.`);
288
- if (config.onError) {
289
- const res = config.onError(`${type}.rule.in`, `${baseKey}${key}`, value);
290
- if (res) {
291
- throw new ValidError(res);
292
- }
293
- }
294
- throw error;
295
- }
296
- // nest config
297
- if (rule.config) {
298
- if (type === 'cookie') {
299
- this.logger.warn('Cookie not support nest rule.');
300
- }
301
- else {
302
- if (Array.isArray(value)) {
303
- // array
304
- for (const val of value) {
305
- this.validContent(type, val, (baseKey ? `${baseKey}.${key}.` : `${key}.`), rule.config);
306
- }
307
- }
308
- else if (typeof value === 'object') {
309
- // object
310
- this.validContent(type, value, (baseKey ? `${baseKey}.${key}.` : `${key}.`), rule.config);
311
- }
312
- }
313
- }
314
- }
315
- }
316
- }
317
- }
318
-
319
- const ContentType = {
320
- plain: 'text/plain',
321
- html: 'text/html',
322
- xml: 'application/xml',
323
- csv: 'text/csv',
324
- css: 'text/css',
325
- javascript: 'application/javascript',
326
- json: 'application/json',
327
- jsonp: 'application/javascript'
328
- };
329
- class Http {
330
- /**
331
- * 创建 Http 插件实例
332
- * @param config {object} 配置项
333
- * @param config.name {string} 配置名
334
- * @param config.config {object} 网关配置
335
- * @param config.validator {object} 入参校验配置
336
- * @param config.validator.params {object} params 校验配置
337
- * @param config.validator.params.whitelist {string} 白名单配置
338
- * @param config.validator.params.onError {function} 自定义报错
339
- * @param config.validator.params.rules {object} 参数校验规则
340
- * @param config.validator.cookie {object} cookie 校验配置
341
- * @param config.validator.cookie.whitelist {string} 白名单配置
342
- * @param config.validator.cookie.onError {function} 自定义报错
343
- * @param config.validator.cookie.rules {object} 参数校验规则
344
- * @param config.validator.session {object} session 校验配置
345
- * @param config.validator.session.whitelist {string} 白名单配置
346
- * @param config.validator.session.onError {function} 自定义报错
347
- * @param config.validator.session.rules {object} 参数校验规则
348
- */
349
- constructor(config = Object.create(null)) {
350
- this.logger = new Logger('Http');
351
- this.type = 'http';
352
- this.name = config.name;
353
- this.config = config.config || Object.create(null);
354
- if (config.validator) {
355
- this.validatorOptions = config.validator;
356
- }
357
- this.headers = Object.create(null);
358
- this.cookie = new Cookie(this.config.cookie || {});
359
- this.session = this.cookie.session;
360
- }
361
- async onDeploy(data, next) {
362
- await next();
363
- this.logger.debug('[Http] 组装网关配置');
364
- this.logger.debug('%o', data);
365
- const config = deepMerge(data.config.plugins[this.name || this.type], { config: this.config });
366
- // 根据文件及文件夹名生成路径
367
- config.config.path = '=/' + data.name.replace(/_/g, '/').replace(/\/index$/, '');
368
- this.logger.debug('[Http] 组装完成 %o', config);
369
- // 引用服务商部署插件
370
- // eslint-disable-next-line security/detect-non-literal-require, @typescript-eslint/no-var-requires
371
- const Provider = require(config.provider.type);
372
- const provider = new Provider(config.provider.config);
373
- // 部署网关
374
- await provider.deploy(this.type, data, config);
375
- }
376
- async onMount(data, next) {
377
- this.logger.debug('[onMount] merge config');
378
- if (data.config.plugins[this.name || this.type]) {
379
- this.config = deepMerge(this.config, data.config.plugins[this.name || this.type].config);
380
- }
381
- this.logger.debug('[onMount] prepare cookie & session');
382
- this.cookie = new Cookie(this.config.cookie || {});
383
- this.session = this.cookie.session;
384
- if (this.validatorOptions) {
385
- this.logger.debug('[onMount] prepare validator');
386
- this.validator = new Validator(this.validatorOptions);
387
- }
388
- await next();
389
- }
390
- async onInvoke(data, next) {
391
- this.logger.debug('[onInvoke] Parse & valid');
392
- this.logger.time('http');
393
- this.headers = data.event.headers || Object.create(null);
394
- this.params = Object.create(null);
395
- this.response = {
396
- headers: Object.create(null)
397
- };
398
- if (data.event.body) {
399
- if (data.event.headers && data.event.headers['content-type'] && data.event.headers['content-type'].includes('application/json')) {
400
- this.logger.debug('[onInvoke] Parse params from json body');
401
- this.params = JSON.parse(data.event.body);
402
- }
403
- else {
404
- this.logger.debug('[onInvoke] Parse params from raw body');
405
- this.params = data.event.body;
406
- }
407
- }
408
- else if (data.event.queryString) {
409
- this.logger.debug('[onInvoke] Parse params from queryString');
410
- this.params = data.event.queryString;
411
- }
412
- this.logger.debug('[onInvoke] Parse cookie');
413
- this.cookie.invoke(this.headers['cookie']);
414
- this.logger.debug('[onInvoke] Cookie: %o', this.cookie.content);
415
- this.logger.debug('[onInvoke] Session: %o', this.session.content);
416
- if (this.validator) {
417
- this.logger.debug('[onInvoke] Valid request');
418
- try {
419
- this.validator.valid({
420
- params: this.params,
421
- cookie: this.cookie,
422
- session: this.session
423
- });
424
- }
425
- catch (error) {
426
- this.logger.error(error);
427
- data.response = {
428
- statusCode: error.statusCode || 500,
429
- headers: Object.assign({
430
- 'Content-Type': 'application/json; charset=utf-8',
431
- 'X-Request-Id': data.context.request_id
432
- }, error.headers || {}),
433
- body: JSON.stringify({
434
- error: {
435
- message: error.message
436
- }
437
- })
438
- };
439
- return;
440
- }
441
- }
442
- this.logger.timeEnd('http', '[onInvoke] Parse & valid done');
443
- await next();
444
- this.logger.debug('[onInvoke] Generate response');
445
- this.logger.time('http');
446
- // update seesion
447
- this.session.update();
448
- // 处理 body
449
- if (data.response) {
450
- if (data.response instanceof Error || (data.response.constructor && data.response.constructor.name === 'Error')) {
451
- // 当结果是错误类型时
452
- this.logger.error(data.response);
453
- this.response.body = JSON.stringify({ error: { message: data.response.message } });
454
- this.response.statusCode = 500;
455
- }
456
- else if (Object.prototype.toString.call(data.response) === '[object Object]' && data.response.statusCode && data.response.headers) {
457
- // 当返回结果是响应结构体时
458
- this.response = data.response;
459
- }
460
- else {
461
- this.response.body = JSON.stringify({ data: data.response });
462
- }
463
- }
464
- // 处理 statusCode
465
- if (!this.response.statusCode) {
466
- this.response.statusCode = this.response.body ? 200 : 201;
467
- }
468
- // 处理 headers
469
- this.response.headers = Object.assign({
470
- 'Content-Type': 'application/json; charset=utf-8',
471
- 'X-Request-Id': new Date().getTime().toString()
472
- }, this.cookie.headers(), this.response.headers);
473
- /* eslint-disable-next-line require-atomic-updates */
474
- data.response = this.response;
475
- this.logger.timeEnd('http', '[onInvoke] done');
476
- }
477
- /**
478
- * 设置 header
479
- * @param key {string} key
480
- * @param value {*} value
481
- */
482
- setHeader(key, value) {
483
- this.response.headers[key] = value;
484
- return this;
485
- }
486
- /**
487
- * 设置 Content-Type
488
- * @param type {string} 类型
489
- * @param charset {string} 编码
490
- */
491
- setContentType(type, charset = 'utf-8') {
492
- if (ContentType[type]) {
493
- this.setHeader('Content-Type', `${ContentType[type]}; charset=${charset}`);
494
- }
495
- else {
496
- this.setHeader('Content-Type', `${type}; charset=${charset}`);
497
- }
498
- return this;
499
- }
500
- /**
501
- * 设置状态码
502
- * @param code {number} 状态码
503
- */
504
- setStatusCode(code) {
505
- this.response.statusCode = code;
506
- return this;
507
- }
508
- /**
509
- * 设置 body
510
- * @param body {*} 内容
511
- */
512
- setBody(body) {
513
- this.response.body = body;
514
- return this;
515
- }
516
- }
517
-
518
- export { ContentType, Cookie, Http, Session, Validator };