@faasjs/http 0.0.2-beta.394 → 0.0.2-beta.397
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/README.md +3 -3
- package/dist/index.js +74 -33
- package/dist/index.mjs +73 -35
- package/package.json +3 -3
package/README.md
CHANGED
|
@@ -20,7 +20,7 @@ FaasJS's http plugin.
|
|
|
20
20
|
- [Session](classes/Session.md)
|
|
21
21
|
- [Validator](classes/Validator.md)
|
|
22
22
|
|
|
23
|
-
### Type
|
|
23
|
+
### Type Aliases
|
|
24
24
|
|
|
25
25
|
- [CookieOptions](#cookieoptions)
|
|
26
26
|
- [HttpConfig](#httpconfig)
|
|
@@ -38,7 +38,7 @@ FaasJS's http plugin.
|
|
|
38
38
|
|
|
39
39
|
- [useHttp](#usehttp)
|
|
40
40
|
|
|
41
|
-
## Type
|
|
41
|
+
## Type Aliases
|
|
42
42
|
|
|
43
43
|
### CookieOptions
|
|
44
44
|
|
|
@@ -165,9 +165,9 @@ ___
|
|
|
165
165
|
|
|
166
166
|
| Name | Type |
|
|
167
167
|
| :------ | :------ |
|
|
168
|
+
| `onError?` | (`type`: `string`, `key`: `string` \| `string`[], `value?`: `any`) => { `message`: `any` ; `statusCode?`: `number` } \| `void` |
|
|
168
169
|
| `rules` | { [k in keyof Content]?: ValidatorRuleOptions } |
|
|
169
170
|
| `whitelist?` | ``"error"`` \| ``"ignore"`` |
|
|
170
|
-
| `onError?` | (`type`: `string`, `key`: `string` \| `string`[], `value?`: `any`) => `void` \| { `message`: `any` ; `statusCode?`: `number` } |
|
|
171
171
|
|
|
172
172
|
___
|
|
173
173
|
|
package/dist/index.js
CHANGED
|
@@ -1,24 +1,8 @@
|
|
|
1
|
+
"use strict";
|
|
1
2
|
var __defProp = Object.defineProperty;
|
|
2
|
-
var __defProps = Object.defineProperties;
|
|
3
3
|
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
-
var __getOwnPropDescs = Object.getOwnPropertyDescriptors;
|
|
5
4
|
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
6
|
-
var __getOwnPropSymbols = Object.getOwnPropertySymbols;
|
|
7
5
|
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
8
|
-
var __propIsEnum = Object.prototype.propertyIsEnumerable;
|
|
9
|
-
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
|
|
10
|
-
var __spreadValues = (a, b) => {
|
|
11
|
-
for (var prop in b || (b = {}))
|
|
12
|
-
if (__hasOwnProp.call(b, prop))
|
|
13
|
-
__defNormalProp(a, prop, b[prop]);
|
|
14
|
-
if (__getOwnPropSymbols)
|
|
15
|
-
for (var prop of __getOwnPropSymbols(b)) {
|
|
16
|
-
if (__propIsEnum.call(b, prop))
|
|
17
|
-
__defNormalProp(a, prop, b[prop]);
|
|
18
|
-
}
|
|
19
|
-
return a;
|
|
20
|
-
};
|
|
21
|
-
var __spreadProps = (a, b) => __defProps(a, __getOwnPropDescs(b));
|
|
22
6
|
var __export = (target, all) => {
|
|
23
7
|
for (var name in all)
|
|
24
8
|
__defProp(target, name, { get: all[name], enumerable: true });
|
|
@@ -66,9 +50,10 @@ function deepMerge(...sources) {
|
|
|
66
50
|
val = deepMerge(acc[key], value);
|
|
67
51
|
else
|
|
68
52
|
val = value;
|
|
69
|
-
acc =
|
|
53
|
+
acc = {
|
|
54
|
+
...acc,
|
|
70
55
|
[key]: val
|
|
71
|
-
}
|
|
56
|
+
};
|
|
72
57
|
}
|
|
73
58
|
return acc;
|
|
74
59
|
}
|
|
@@ -91,8 +76,20 @@ var Session = class {
|
|
|
91
76
|
digest: "sha256",
|
|
92
77
|
cipherName: "aes-256-cbc"
|
|
93
78
|
}, config);
|
|
94
|
-
this.secret = (0, import_crypto.pbkdf2Sync)(
|
|
95
|
-
|
|
79
|
+
this.secret = (0, import_crypto.pbkdf2Sync)(
|
|
80
|
+
this.config.secret,
|
|
81
|
+
this.config.salt,
|
|
82
|
+
this.config.iterations,
|
|
83
|
+
this.config.keylen / 2,
|
|
84
|
+
this.config.digest
|
|
85
|
+
);
|
|
86
|
+
this.signedSecret = (0, import_crypto.pbkdf2Sync)(
|
|
87
|
+
this.config.secret,
|
|
88
|
+
this.config.signedSalt,
|
|
89
|
+
this.config.iterations,
|
|
90
|
+
this.config.keylen,
|
|
91
|
+
this.config.digest
|
|
92
|
+
);
|
|
96
93
|
this.content = /* @__PURE__ */ Object.create(null);
|
|
97
94
|
}
|
|
98
95
|
invoke(cookie) {
|
|
@@ -241,13 +238,23 @@ var Validator = class {
|
|
|
241
238
|
this.logger.debug("Valid Cookie");
|
|
242
239
|
if (request.cookie == null)
|
|
243
240
|
throw Error("Not found Cookie");
|
|
244
|
-
this.validContent(
|
|
241
|
+
this.validContent(
|
|
242
|
+
"cookie",
|
|
243
|
+
request.cookie.content,
|
|
244
|
+
"",
|
|
245
|
+
this.cookieConfig
|
|
246
|
+
);
|
|
245
247
|
}
|
|
246
248
|
if (this.sessionConfig && request.session) {
|
|
247
249
|
this.logger.debug("Valid Session");
|
|
248
250
|
if (request.session == null)
|
|
249
251
|
throw Error("Not found Session");
|
|
250
|
-
this.validContent(
|
|
252
|
+
this.validContent(
|
|
253
|
+
"session",
|
|
254
|
+
request.session.content,
|
|
255
|
+
"",
|
|
256
|
+
this.sessionConfig
|
|
257
|
+
);
|
|
251
258
|
}
|
|
252
259
|
}
|
|
253
260
|
validContent(type, params, baseKey, config) {
|
|
@@ -258,7 +265,9 @@ var Validator = class {
|
|
|
258
265
|
if (diff.length > 0) {
|
|
259
266
|
if (config.whitelist === "error") {
|
|
260
267
|
const diffKeys = diff.map((k) => `${baseKey}${k}`);
|
|
261
|
-
const error = Error(
|
|
268
|
+
const error = Error(
|
|
269
|
+
`[${type}] Not permitted keys: ${diffKeys.join(", ")}`
|
|
270
|
+
);
|
|
262
271
|
if (config.onError) {
|
|
263
272
|
const res = config.onError(`${type}.whitelist`, baseKey, diffKeys);
|
|
264
273
|
if (res)
|
|
@@ -287,7 +296,11 @@ var Validator = class {
|
|
|
287
296
|
if (typeof value === "undefined" || value === null) {
|
|
288
297
|
const error = Error(`[${type}] ${baseKey}${key} is required.`);
|
|
289
298
|
if (config.onError) {
|
|
290
|
-
const res = config.onError(
|
|
299
|
+
const res = config.onError(
|
|
300
|
+
`${type}.rule.required`,
|
|
301
|
+
`${baseKey}${key}`,
|
|
302
|
+
value
|
|
303
|
+
);
|
|
291
304
|
if (res)
|
|
292
305
|
throw new HttpError(res);
|
|
293
306
|
}
|
|
@@ -312,9 +325,15 @@ var Validator = class {
|
|
|
312
325
|
break;
|
|
313
326
|
}
|
|
314
327
|
if (!typed) {
|
|
315
|
-
const error = Error(
|
|
328
|
+
const error = Error(
|
|
329
|
+
`[${type}] ${baseKey}${key} must be a ${rule.type}.`
|
|
330
|
+
);
|
|
316
331
|
if (config.onError) {
|
|
317
|
-
const res = config.onError(
|
|
332
|
+
const res = config.onError(
|
|
333
|
+
`${type}.rule.type`,
|
|
334
|
+
`${baseKey}${key}`,
|
|
335
|
+
value
|
|
336
|
+
);
|
|
318
337
|
if (res)
|
|
319
338
|
throw new HttpError(res);
|
|
320
339
|
}
|
|
@@ -322,18 +341,30 @@ var Validator = class {
|
|
|
322
341
|
}
|
|
323
342
|
}
|
|
324
343
|
if (rule.regexp && (rule.type === "string" || !rule.type) && !rule.regexp.test(value)) {
|
|
325
|
-
const error = Error(
|
|
344
|
+
const error = Error(
|
|
345
|
+
`[${type}] ${baseKey}${key} must match ${rule.regexp}.`
|
|
346
|
+
);
|
|
326
347
|
if (config.onError) {
|
|
327
|
-
const res = config.onError(
|
|
348
|
+
const res = config.onError(
|
|
349
|
+
`${type}.rule.regexp`,
|
|
350
|
+
`${baseKey}${key}`,
|
|
351
|
+
value
|
|
352
|
+
);
|
|
328
353
|
if (res)
|
|
329
354
|
throw new HttpError(res);
|
|
330
355
|
}
|
|
331
356
|
throw error;
|
|
332
357
|
}
|
|
333
358
|
if (rule.in && !rule.in.includes(value)) {
|
|
334
|
-
const error = Error(
|
|
359
|
+
const error = Error(
|
|
360
|
+
`[${type}] ${baseKey}${key} must be in ${rule.in.join(", ")}.`
|
|
361
|
+
);
|
|
335
362
|
if (config.onError) {
|
|
336
|
-
const res = config.onError(
|
|
363
|
+
const res = config.onError(
|
|
364
|
+
`${type}.rule.in`,
|
|
365
|
+
`${baseKey}${key}`,
|
|
366
|
+
value
|
|
367
|
+
);
|
|
337
368
|
if (res)
|
|
338
369
|
throw new HttpError(res);
|
|
339
370
|
}
|
|
@@ -344,9 +375,19 @@ var Validator = class {
|
|
|
344
375
|
this.logger.warn("Cookie not support nest rule.");
|
|
345
376
|
else if (Array.isArray(value))
|
|
346
377
|
for (const val of value)
|
|
347
|
-
this.validContent(
|
|
378
|
+
this.validContent(
|
|
379
|
+
type,
|
|
380
|
+
val,
|
|
381
|
+
baseKey ? `${baseKey}.${key}.` : `${key}.`,
|
|
382
|
+
rule.config
|
|
383
|
+
);
|
|
348
384
|
else if (typeof value === "object")
|
|
349
|
-
this.validContent(
|
|
385
|
+
this.validContent(
|
|
386
|
+
type,
|
|
387
|
+
value,
|
|
388
|
+
baseKey ? `${baseKey}.${key}.` : `${key}.`,
|
|
389
|
+
rule.config
|
|
390
|
+
);
|
|
350
391
|
}
|
|
351
392
|
}
|
|
352
393
|
}
|
package/dist/index.mjs
CHANGED
|
@@ -1,22 +1,3 @@
|
|
|
1
|
-
var __defProp = Object.defineProperty;
|
|
2
|
-
var __defProps = Object.defineProperties;
|
|
3
|
-
var __getOwnPropDescs = Object.getOwnPropertyDescriptors;
|
|
4
|
-
var __getOwnPropSymbols = Object.getOwnPropertySymbols;
|
|
5
|
-
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
-
var __propIsEnum = Object.prototype.propertyIsEnumerable;
|
|
7
|
-
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
|
|
8
|
-
var __spreadValues = (a, b) => {
|
|
9
|
-
for (var prop in b || (b = {}))
|
|
10
|
-
if (__hasOwnProp.call(b, prop))
|
|
11
|
-
__defNormalProp(a, prop, b[prop]);
|
|
12
|
-
if (__getOwnPropSymbols)
|
|
13
|
-
for (var prop of __getOwnPropSymbols(b)) {
|
|
14
|
-
if (__propIsEnum.call(b, prop))
|
|
15
|
-
__defNormalProp(a, prop, b[prop]);
|
|
16
|
-
}
|
|
17
|
-
return a;
|
|
18
|
-
};
|
|
19
|
-
var __spreadProps = (a, b) => __defProps(a, __getOwnPropDescs(b));
|
|
20
1
|
var __require = /* @__PURE__ */ ((x) => typeof require !== "undefined" ? require : typeof Proxy !== "undefined" ? new Proxy(x, {
|
|
21
2
|
get: (a, b) => (typeof require !== "undefined" ? require : a)[b]
|
|
22
3
|
}) : x)(function(x) {
|
|
@@ -49,9 +30,10 @@ function deepMerge(...sources) {
|
|
|
49
30
|
val = deepMerge(acc[key], value);
|
|
50
31
|
else
|
|
51
32
|
val = value;
|
|
52
|
-
acc =
|
|
33
|
+
acc = {
|
|
34
|
+
...acc,
|
|
53
35
|
[key]: val
|
|
54
|
-
}
|
|
36
|
+
};
|
|
55
37
|
}
|
|
56
38
|
return acc;
|
|
57
39
|
}
|
|
@@ -80,8 +62,20 @@ var Session = class {
|
|
|
80
62
|
digest: "sha256",
|
|
81
63
|
cipherName: "aes-256-cbc"
|
|
82
64
|
}, config);
|
|
83
|
-
this.secret = pbkdf2Sync(
|
|
84
|
-
|
|
65
|
+
this.secret = pbkdf2Sync(
|
|
66
|
+
this.config.secret,
|
|
67
|
+
this.config.salt,
|
|
68
|
+
this.config.iterations,
|
|
69
|
+
this.config.keylen / 2,
|
|
70
|
+
this.config.digest
|
|
71
|
+
);
|
|
72
|
+
this.signedSecret = pbkdf2Sync(
|
|
73
|
+
this.config.secret,
|
|
74
|
+
this.config.signedSalt,
|
|
75
|
+
this.config.iterations,
|
|
76
|
+
this.config.keylen,
|
|
77
|
+
this.config.digest
|
|
78
|
+
);
|
|
85
79
|
this.content = /* @__PURE__ */ Object.create(null);
|
|
86
80
|
}
|
|
87
81
|
invoke(cookie) {
|
|
@@ -230,13 +224,23 @@ var Validator = class {
|
|
|
230
224
|
this.logger.debug("Valid Cookie");
|
|
231
225
|
if (request.cookie == null)
|
|
232
226
|
throw Error("Not found Cookie");
|
|
233
|
-
this.validContent(
|
|
227
|
+
this.validContent(
|
|
228
|
+
"cookie",
|
|
229
|
+
request.cookie.content,
|
|
230
|
+
"",
|
|
231
|
+
this.cookieConfig
|
|
232
|
+
);
|
|
234
233
|
}
|
|
235
234
|
if (this.sessionConfig && request.session) {
|
|
236
235
|
this.logger.debug("Valid Session");
|
|
237
236
|
if (request.session == null)
|
|
238
237
|
throw Error("Not found Session");
|
|
239
|
-
this.validContent(
|
|
238
|
+
this.validContent(
|
|
239
|
+
"session",
|
|
240
|
+
request.session.content,
|
|
241
|
+
"",
|
|
242
|
+
this.sessionConfig
|
|
243
|
+
);
|
|
240
244
|
}
|
|
241
245
|
}
|
|
242
246
|
validContent(type, params, baseKey, config) {
|
|
@@ -247,7 +251,9 @@ var Validator = class {
|
|
|
247
251
|
if (diff.length > 0) {
|
|
248
252
|
if (config.whitelist === "error") {
|
|
249
253
|
const diffKeys = diff.map((k) => `${baseKey}${k}`);
|
|
250
|
-
const error = Error(
|
|
254
|
+
const error = Error(
|
|
255
|
+
`[${type}] Not permitted keys: ${diffKeys.join(", ")}`
|
|
256
|
+
);
|
|
251
257
|
if (config.onError) {
|
|
252
258
|
const res = config.onError(`${type}.whitelist`, baseKey, diffKeys);
|
|
253
259
|
if (res)
|
|
@@ -276,7 +282,11 @@ var Validator = class {
|
|
|
276
282
|
if (typeof value === "undefined" || value === null) {
|
|
277
283
|
const error = Error(`[${type}] ${baseKey}${key} is required.`);
|
|
278
284
|
if (config.onError) {
|
|
279
|
-
const res = config.onError(
|
|
285
|
+
const res = config.onError(
|
|
286
|
+
`${type}.rule.required`,
|
|
287
|
+
`${baseKey}${key}`,
|
|
288
|
+
value
|
|
289
|
+
);
|
|
280
290
|
if (res)
|
|
281
291
|
throw new HttpError(res);
|
|
282
292
|
}
|
|
@@ -301,9 +311,15 @@ var Validator = class {
|
|
|
301
311
|
break;
|
|
302
312
|
}
|
|
303
313
|
if (!typed) {
|
|
304
|
-
const error = Error(
|
|
314
|
+
const error = Error(
|
|
315
|
+
`[${type}] ${baseKey}${key} must be a ${rule.type}.`
|
|
316
|
+
);
|
|
305
317
|
if (config.onError) {
|
|
306
|
-
const res = config.onError(
|
|
318
|
+
const res = config.onError(
|
|
319
|
+
`${type}.rule.type`,
|
|
320
|
+
`${baseKey}${key}`,
|
|
321
|
+
value
|
|
322
|
+
);
|
|
307
323
|
if (res)
|
|
308
324
|
throw new HttpError(res);
|
|
309
325
|
}
|
|
@@ -311,18 +327,30 @@ var Validator = class {
|
|
|
311
327
|
}
|
|
312
328
|
}
|
|
313
329
|
if (rule.regexp && (rule.type === "string" || !rule.type) && !rule.regexp.test(value)) {
|
|
314
|
-
const error = Error(
|
|
330
|
+
const error = Error(
|
|
331
|
+
`[${type}] ${baseKey}${key} must match ${rule.regexp}.`
|
|
332
|
+
);
|
|
315
333
|
if (config.onError) {
|
|
316
|
-
const res = config.onError(
|
|
334
|
+
const res = config.onError(
|
|
335
|
+
`${type}.rule.regexp`,
|
|
336
|
+
`${baseKey}${key}`,
|
|
337
|
+
value
|
|
338
|
+
);
|
|
317
339
|
if (res)
|
|
318
340
|
throw new HttpError(res);
|
|
319
341
|
}
|
|
320
342
|
throw error;
|
|
321
343
|
}
|
|
322
344
|
if (rule.in && !rule.in.includes(value)) {
|
|
323
|
-
const error = Error(
|
|
345
|
+
const error = Error(
|
|
346
|
+
`[${type}] ${baseKey}${key} must be in ${rule.in.join(", ")}.`
|
|
347
|
+
);
|
|
324
348
|
if (config.onError) {
|
|
325
|
-
const res = config.onError(
|
|
349
|
+
const res = config.onError(
|
|
350
|
+
`${type}.rule.in`,
|
|
351
|
+
`${baseKey}${key}`,
|
|
352
|
+
value
|
|
353
|
+
);
|
|
326
354
|
if (res)
|
|
327
355
|
throw new HttpError(res);
|
|
328
356
|
}
|
|
@@ -333,9 +361,19 @@ var Validator = class {
|
|
|
333
361
|
this.logger.warn("Cookie not support nest rule.");
|
|
334
362
|
else if (Array.isArray(value))
|
|
335
363
|
for (const val of value)
|
|
336
|
-
this.validContent(
|
|
364
|
+
this.validContent(
|
|
365
|
+
type,
|
|
366
|
+
val,
|
|
367
|
+
baseKey ? `${baseKey}.${key}.` : `${key}.`,
|
|
368
|
+
rule.config
|
|
369
|
+
);
|
|
337
370
|
else if (typeof value === "object")
|
|
338
|
-
this.validContent(
|
|
371
|
+
this.validContent(
|
|
372
|
+
type,
|
|
373
|
+
value,
|
|
374
|
+
baseKey ? `${baseKey}.${key}.` : `${key}.`,
|
|
375
|
+
rule.config
|
|
376
|
+
);
|
|
339
377
|
}
|
|
340
378
|
}
|
|
341
379
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@faasjs/http",
|
|
3
|
-
"version": "0.0.2-beta.
|
|
3
|
+
"version": "0.0.2-beta.397",
|
|
4
4
|
"license": "MIT",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"types": "dist/index.d.ts",
|
|
@@ -22,8 +22,8 @@
|
|
|
22
22
|
"dist"
|
|
23
23
|
],
|
|
24
24
|
"peerDependencies": {
|
|
25
|
-
"@faasjs/func": "^0.0.2-beta.
|
|
26
|
-
"@faasjs/logger": "^0.0.2-beta.
|
|
25
|
+
"@faasjs/func": "^0.0.2-beta.397",
|
|
26
|
+
"@faasjs/logger": "^0.0.2-beta.397"
|
|
27
27
|
},
|
|
28
28
|
"devDependencies": {
|
|
29
29
|
"tsup": "*",
|