@faasjs/http 0.0.3-beta.8 → 0.0.3-beta.81
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 +2 -2
- package/dist/index.d.ts +1 -1
- package/dist/index.js +21 -5
- package/dist/index.mjs +21 -5
- package/package.json +4 -5
package/README.md
CHANGED
|
@@ -200,7 +200,7 @@ ___
|
|
|
200
200
|
|
|
201
201
|
### useHttp
|
|
202
202
|
|
|
203
|
-
▸ **useHttp**<`TParams`, `TCookie`, `TSession`\>(`config?`): [`Http`](classes/Http.md)<`TParams`, `TCookie`, `TSession
|
|
203
|
+
▸ **useHttp**<`TParams`, `TCookie`, `TSession`\>(`config?`): `UseifyPlugin`<[`Http`](classes/Http.md)<`TParams`, `TCookie`, `TSession`\>\>
|
|
204
204
|
|
|
205
205
|
#### Type parameters
|
|
206
206
|
|
|
@@ -218,4 +218,4 @@ ___
|
|
|
218
218
|
|
|
219
219
|
#### Returns
|
|
220
220
|
|
|
221
|
-
[`Http`](classes/Http.md)<`TParams`, `TCookie`, `TSession
|
|
221
|
+
`UseifyPlugin`<[`Http`](classes/Http.md)<`TParams`, `TCookie`, `TSession`\>\>
|
package/dist/index.d.ts
CHANGED
|
@@ -204,6 +204,6 @@ declare class Http<TParams extends Record<string, any> = any, TCookie extends Re
|
|
|
204
204
|
*/
|
|
205
205
|
setBody(body: string): Http<TParams, TCookie, TSession>;
|
|
206
206
|
}
|
|
207
|
-
declare function useHttp<TParams extends Record<string, any> = any, TCookie extends Record<string, string> = any, TSession extends Record<string, string> = any>(config?: HttpConfig<TParams, TCookie, TSession>): Http<TParams, TCookie, TSession
|
|
207
|
+
declare function useHttp<TParams extends Record<string, any> = any, TCookie extends Record<string, string> = any, TSession extends Record<string, string> = any>(config?: HttpConfig<TParams, TCookie, TSession>): UseifyPlugin<Http<TParams, TCookie, TSession>>;
|
|
208
208
|
|
|
209
209
|
export { ContentType, Cookie, CookieOptions, Http, HttpConfig, HttpError, Response, Session, SessionOptions, Validator, ValidatorConfig, ValidatorOptions, ValidatorRuleOptions, useHttp };
|
package/dist/index.js
CHANGED
|
@@ -66,6 +66,8 @@ var import_crypto = require("crypto");
|
|
|
66
66
|
var Session = class {
|
|
67
67
|
constructor(cookie, config) {
|
|
68
68
|
this.cookie = cookie;
|
|
69
|
+
if (!(config == null ? void 0 : config.secret))
|
|
70
|
+
console.warn("Session's secret is missing.");
|
|
69
71
|
this.config = Object.assign({
|
|
70
72
|
key: "key",
|
|
71
73
|
secret: (0, import_crypto.randomBytes)(128).toString("hex"),
|
|
@@ -120,7 +122,7 @@ var Session = class {
|
|
|
120
122
|
hmac.update(signedParts[0]);
|
|
121
123
|
const digest = hmac.digest("hex");
|
|
122
124
|
if (signedParts[1] !== digest)
|
|
123
|
-
throw Error("Not valid");
|
|
125
|
+
throw Error("Session Not valid");
|
|
124
126
|
const message = Buffer.from(signedParts[0], "base64").toString();
|
|
125
127
|
const parts = message.split("--").map(function(part2) {
|
|
126
128
|
return Buffer.from(part2, "base64");
|
|
@@ -430,9 +432,6 @@ var Http = class {
|
|
|
430
432
|
this.config = (config == null ? void 0 : config.config) || /* @__PURE__ */ Object.create(null);
|
|
431
433
|
if (config == null ? void 0 : config.validator)
|
|
432
434
|
this.validatorOptions = config.validator;
|
|
433
|
-
this.headers = /* @__PURE__ */ Object.create(null);
|
|
434
|
-
this.cookie = new Cookie(this.config.cookie || {});
|
|
435
|
-
this.session = this.cookie.session;
|
|
436
435
|
}
|
|
437
436
|
async onDeploy(data, next) {
|
|
438
437
|
var _a;
|
|
@@ -459,6 +458,23 @@ var Http = class {
|
|
|
459
458
|
}
|
|
460
459
|
async onMount(data, next) {
|
|
461
460
|
data.logger.debug("[onMount] merge config");
|
|
461
|
+
const prefix = `SECRET_${this.name.toUpperCase()}_`;
|
|
462
|
+
for (let key in process.env)
|
|
463
|
+
if (key.startsWith(prefix)) {
|
|
464
|
+
const value = process.env[key];
|
|
465
|
+
key = key.replace(prefix, "").toLowerCase();
|
|
466
|
+
if (key.includes("_")) {
|
|
467
|
+
let config = this.config;
|
|
468
|
+
const keys = key.split("_");
|
|
469
|
+
keys.slice(0, keys.length - 1).forEach((k) => {
|
|
470
|
+
if (!config[k])
|
|
471
|
+
config[k] = /* @__PURE__ */ Object.create(null);
|
|
472
|
+
config = config[k];
|
|
473
|
+
});
|
|
474
|
+
config[keys[keys.length - 1]] = value;
|
|
475
|
+
} else
|
|
476
|
+
this.config[key] = value;
|
|
477
|
+
}
|
|
462
478
|
if (data.config.plugins && data.config.plugins[this.name || this.type])
|
|
463
479
|
this.config = deepMerge(this.config, data.config.plugins[this.name || this.type].config);
|
|
464
480
|
data.logger.debug("[onMount] prepare cookie & session");
|
|
@@ -479,7 +495,7 @@ var Http = class {
|
|
|
479
495
|
if (data.event.body) {
|
|
480
496
|
if ((_a = this.headers["content-type"]) == null ? void 0 : _a.includes("application/json")) {
|
|
481
497
|
data.logger.debug("[onInvoke] Parse params from json body");
|
|
482
|
-
this.params = Object.assign(this.params, JSON.parse(data.event.body));
|
|
498
|
+
this.params = Object.keys(this.params).length ? Object.assign(this.params, JSON.parse(data.event.body)) : JSON.parse(data.event.body);
|
|
483
499
|
} else {
|
|
484
500
|
data.logger.debug("[onInvoke] Parse params from raw body");
|
|
485
501
|
this.params = data.event.body || /* @__PURE__ */ Object.create(null);
|
package/dist/index.mjs
CHANGED
|
@@ -52,6 +52,8 @@ import {
|
|
|
52
52
|
var Session = class {
|
|
53
53
|
constructor(cookie, config) {
|
|
54
54
|
this.cookie = cookie;
|
|
55
|
+
if (!(config == null ? void 0 : config.secret))
|
|
56
|
+
console.warn("Session's secret is missing.");
|
|
55
57
|
this.config = Object.assign({
|
|
56
58
|
key: "key",
|
|
57
59
|
secret: randomBytes(128).toString("hex"),
|
|
@@ -106,7 +108,7 @@ var Session = class {
|
|
|
106
108
|
hmac.update(signedParts[0]);
|
|
107
109
|
const digest = hmac.digest("hex");
|
|
108
110
|
if (signedParts[1] !== digest)
|
|
109
|
-
throw Error("Not valid");
|
|
111
|
+
throw Error("Session Not valid");
|
|
110
112
|
const message = Buffer.from(signedParts[0], "base64").toString();
|
|
111
113
|
const parts = message.split("--").map(function(part2) {
|
|
112
114
|
return Buffer.from(part2, "base64");
|
|
@@ -420,9 +422,6 @@ var Http = class {
|
|
|
420
422
|
this.config = (config == null ? void 0 : config.config) || /* @__PURE__ */ Object.create(null);
|
|
421
423
|
if (config == null ? void 0 : config.validator)
|
|
422
424
|
this.validatorOptions = config.validator;
|
|
423
|
-
this.headers = /* @__PURE__ */ Object.create(null);
|
|
424
|
-
this.cookie = new Cookie(this.config.cookie || {});
|
|
425
|
-
this.session = this.cookie.session;
|
|
426
425
|
}
|
|
427
426
|
async onDeploy(data, next) {
|
|
428
427
|
var _a;
|
|
@@ -449,6 +448,23 @@ var Http = class {
|
|
|
449
448
|
}
|
|
450
449
|
async onMount(data, next) {
|
|
451
450
|
data.logger.debug("[onMount] merge config");
|
|
451
|
+
const prefix = `SECRET_${this.name.toUpperCase()}_`;
|
|
452
|
+
for (let key in process.env)
|
|
453
|
+
if (key.startsWith(prefix)) {
|
|
454
|
+
const value = process.env[key];
|
|
455
|
+
key = key.replace(prefix, "").toLowerCase();
|
|
456
|
+
if (key.includes("_")) {
|
|
457
|
+
let config = this.config;
|
|
458
|
+
const keys = key.split("_");
|
|
459
|
+
keys.slice(0, keys.length - 1).forEach((k) => {
|
|
460
|
+
if (!config[k])
|
|
461
|
+
config[k] = /* @__PURE__ */ Object.create(null);
|
|
462
|
+
config = config[k];
|
|
463
|
+
});
|
|
464
|
+
config[keys[keys.length - 1]] = value;
|
|
465
|
+
} else
|
|
466
|
+
this.config[key] = value;
|
|
467
|
+
}
|
|
452
468
|
if (data.config.plugins && data.config.plugins[this.name || this.type])
|
|
453
469
|
this.config = deepMerge(this.config, data.config.plugins[this.name || this.type].config);
|
|
454
470
|
data.logger.debug("[onMount] prepare cookie & session");
|
|
@@ -469,7 +485,7 @@ var Http = class {
|
|
|
469
485
|
if (data.event.body) {
|
|
470
486
|
if ((_a = this.headers["content-type"]) == null ? void 0 : _a.includes("application/json")) {
|
|
471
487
|
data.logger.debug("[onInvoke] Parse params from json body");
|
|
472
|
-
this.params = Object.assign(this.params, JSON.parse(data.event.body));
|
|
488
|
+
this.params = Object.keys(this.params).length ? Object.assign(this.params, JSON.parse(data.event.body)) : JSON.parse(data.event.body);
|
|
473
489
|
} else {
|
|
474
490
|
data.logger.debug("[onInvoke] Parse params from raw body");
|
|
475
491
|
this.params = data.event.body || /* @__PURE__ */ Object.create(null);
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@faasjs/http",
|
|
3
|
-
"version": "0.0.3-beta.
|
|
3
|
+
"version": "0.0.3-beta.81",
|
|
4
4
|
"license": "MIT",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"types": "dist/index.d.ts",
|
|
@@ -16,15 +16,14 @@
|
|
|
16
16
|
},
|
|
17
17
|
"funding": "https://github.com/sponsors/faasjs",
|
|
18
18
|
"scripts": {
|
|
19
|
-
"build": "tsup-node src/index.ts --format esm,cjs"
|
|
20
|
-
"build:types": "tsup-node src/index.ts --dts-only"
|
|
19
|
+
"build": "tsup-node src/index.ts --format esm,cjs --dts --clean"
|
|
21
20
|
},
|
|
22
21
|
"files": [
|
|
23
22
|
"dist"
|
|
24
23
|
],
|
|
25
24
|
"dependencies": {
|
|
26
|
-
"@faasjs/func": "^0.0.3-beta.
|
|
27
|
-
"@faasjs/logger": "^0.0.3-beta.
|
|
25
|
+
"@faasjs/func": "^0.0.3-beta.81",
|
|
26
|
+
"@faasjs/logger": "^0.0.3-beta.81"
|
|
28
27
|
},
|
|
29
28
|
"engines": {
|
|
30
29
|
"npm": ">=8.0.0",
|