@faasjs/http 0.0.3-beta.86 → 0.0.3-beta.88
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/dist/index.d.mts +210 -0
- package/dist/index.d.ts +4 -3
- package/dist/index.js +70 -108
- package/dist/index.mjs +38 -73
- package/package.json +4 -4
package/dist/index.d.mts
ADDED
|
@@ -0,0 +1,210 @@
|
|
|
1
|
+
import { Plugin, DeployData, Next, MountData, InvokeData, UseifyPlugin } from '@faasjs/func';
|
|
2
|
+
import { Logger } from '@faasjs/logger';
|
|
3
|
+
|
|
4
|
+
type SessionOptions = {
|
|
5
|
+
key: string;
|
|
6
|
+
secret: string;
|
|
7
|
+
salt?: string;
|
|
8
|
+
signedSalt?: string;
|
|
9
|
+
keylen?: number;
|
|
10
|
+
iterations?: number;
|
|
11
|
+
digest?: string;
|
|
12
|
+
cipherName?: string;
|
|
13
|
+
};
|
|
14
|
+
type SessionContent = string | number | {
|
|
15
|
+
[key: string]: any;
|
|
16
|
+
} | null | undefined;
|
|
17
|
+
declare class Session<S extends Record<string, string> = any, C extends Record<string, string> = any> {
|
|
18
|
+
content: Record<string, string | number>;
|
|
19
|
+
readonly config: {
|
|
20
|
+
key: string;
|
|
21
|
+
secret: string;
|
|
22
|
+
salt: string;
|
|
23
|
+
signedSalt: string;
|
|
24
|
+
keylen: number;
|
|
25
|
+
iterations: number;
|
|
26
|
+
digest: string;
|
|
27
|
+
cipherName: string;
|
|
28
|
+
};
|
|
29
|
+
private readonly secret;
|
|
30
|
+
private readonly signedSecret;
|
|
31
|
+
private readonly cookie;
|
|
32
|
+
private changed?;
|
|
33
|
+
constructor(cookie: Cookie<C, S>, config: SessionOptions);
|
|
34
|
+
invoke(cookie?: string, logger?: Logger): void;
|
|
35
|
+
encode(text: SessionContent): string;
|
|
36
|
+
decode<TData = any>(text: string): TData | SessionContent;
|
|
37
|
+
read(key: string): string | number;
|
|
38
|
+
write(key: string, value?: string | number | null): Session<S, C>;
|
|
39
|
+
update(): Session<S, C>;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
type CookieOptions = {
|
|
43
|
+
domain?: string;
|
|
44
|
+
path?: string;
|
|
45
|
+
expires?: number;
|
|
46
|
+
secure?: boolean;
|
|
47
|
+
httpOnly?: boolean;
|
|
48
|
+
sameSite?: 'Strict' | 'Lax' | 'None';
|
|
49
|
+
session?: SessionOptions;
|
|
50
|
+
[key: string]: any;
|
|
51
|
+
};
|
|
52
|
+
declare class Cookie<C extends Record<string, string> = any, S extends Record<string, string> = any> {
|
|
53
|
+
session: Session<S, C>;
|
|
54
|
+
content: Record<string, string>;
|
|
55
|
+
readonly config: {
|
|
56
|
+
domain?: string;
|
|
57
|
+
path: string;
|
|
58
|
+
expires: number;
|
|
59
|
+
secure: boolean;
|
|
60
|
+
httpOnly: boolean;
|
|
61
|
+
sameSite?: 'Strict' | 'Lax' | 'None';
|
|
62
|
+
session: SessionOptions;
|
|
63
|
+
};
|
|
64
|
+
logger: Logger;
|
|
65
|
+
private setCookie;
|
|
66
|
+
constructor(config: CookieOptions, logger?: Logger);
|
|
67
|
+
invoke(cookie: string | undefined, logger: Logger): Cookie<C, S>;
|
|
68
|
+
read(key: string): any;
|
|
69
|
+
write(key: string, value: string, opts?: {
|
|
70
|
+
domain?: string;
|
|
71
|
+
path?: string;
|
|
72
|
+
expires?: number | string;
|
|
73
|
+
secure?: boolean;
|
|
74
|
+
httpOnly?: boolean;
|
|
75
|
+
sameSite?: 'Strict' | 'Lax' | 'None';
|
|
76
|
+
}): Cookie<C, S>;
|
|
77
|
+
headers(): {
|
|
78
|
+
'Set-Cookie'?: string[];
|
|
79
|
+
};
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
type ValidatorRuleOptionsType = 'string' | 'number' | 'boolean' | 'object' | 'array';
|
|
83
|
+
type ValidatorRuleOptions = {
|
|
84
|
+
type?: ValidatorRuleOptionsType;
|
|
85
|
+
required?: boolean;
|
|
86
|
+
in?: any[];
|
|
87
|
+
default?: any;
|
|
88
|
+
config?: Partial<ValidatorOptions>;
|
|
89
|
+
regexp?: RegExp;
|
|
90
|
+
};
|
|
91
|
+
type ValidatorOptions<Content = Record<string, any>> = {
|
|
92
|
+
whitelist?: 'error' | 'ignore';
|
|
93
|
+
rules: {
|
|
94
|
+
[k in keyof Content]?: ValidatorRuleOptions;
|
|
95
|
+
};
|
|
96
|
+
onError?: (type: string, key: string | string[], value?: any) => {
|
|
97
|
+
statusCode?: number;
|
|
98
|
+
message: any;
|
|
99
|
+
} | void;
|
|
100
|
+
};
|
|
101
|
+
type Request<TParams extends Record<string, any> = any, TCookie extends Record<string, string> = any, TSession extends Record<string, string> = any> = {
|
|
102
|
+
headers: {
|
|
103
|
+
[key: string]: string;
|
|
104
|
+
};
|
|
105
|
+
params?: TParams;
|
|
106
|
+
cookie?: Cookie<TCookie, TSession>;
|
|
107
|
+
session?: Session<TSession, TCookie>;
|
|
108
|
+
};
|
|
109
|
+
type BeforeOption<TParams extends Record<string, any> = any, TCookie extends Record<string, string> = any, TSession extends Record<string, string> = any> = (request: Request<TParams, TCookie, TSession>) => Promise<void | {
|
|
110
|
+
statusCode?: number;
|
|
111
|
+
message: string;
|
|
112
|
+
}>;
|
|
113
|
+
type ValidatorConfig<TParams extends Record<string, any> = any, TCookie extends Record<string, string> = any, TSession extends Record<string, string> = any> = {
|
|
114
|
+
params?: ValidatorOptions<TParams>;
|
|
115
|
+
cookie?: ValidatorOptions<TCookie>;
|
|
116
|
+
session?: ValidatorOptions<TSession>;
|
|
117
|
+
before?: BeforeOption;
|
|
118
|
+
};
|
|
119
|
+
declare class Validator<TParams extends Record<string, any> = any, TCookie extends Record<string, string> = any, TSession extends Record<string, string> = any> {
|
|
120
|
+
before?: BeforeOption<TParams, TCookie, TSession>;
|
|
121
|
+
paramsConfig?: ValidatorOptions<TParams>;
|
|
122
|
+
cookieConfig?: ValidatorOptions<TCookie>;
|
|
123
|
+
sessionConfig?: ValidatorOptions<TSession>;
|
|
124
|
+
private request;
|
|
125
|
+
constructor(config: ValidatorConfig<TParams, TCookie, TSession>);
|
|
126
|
+
valid(request: Request<TParams, TCookie, TSession>, logger: Logger): Promise<void>;
|
|
127
|
+
validContent(type: string, params: {
|
|
128
|
+
[key: string]: any;
|
|
129
|
+
}, baseKey: string, config: ValidatorOptions, logger: Logger): void;
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
declare const ContentType: {
|
|
133
|
+
[key: string]: string;
|
|
134
|
+
};
|
|
135
|
+
type HttpConfig<TParams extends Record<string, any> = any, TCookie extends Record<string, string> = any, TSession extends Record<string, string> = any> = {
|
|
136
|
+
[key: string]: any;
|
|
137
|
+
name?: string;
|
|
138
|
+
config?: {
|
|
139
|
+
[key: string]: any;
|
|
140
|
+
/** POST as default */
|
|
141
|
+
method?: 'BEGIN' | 'GET' | 'POST' | 'DELETE' | 'HEAD' | 'PUT' | 'OPTIONS' | 'TRACE' | 'PATCH' | 'ANY';
|
|
142
|
+
timeout?: number;
|
|
143
|
+
/** file relative path as default */
|
|
144
|
+
path?: string;
|
|
145
|
+
ignorePathPrefix?: string;
|
|
146
|
+
functionName?: string;
|
|
147
|
+
cookie?: CookieOptions;
|
|
148
|
+
};
|
|
149
|
+
validator?: ValidatorConfig<TParams, TCookie, TSession>;
|
|
150
|
+
};
|
|
151
|
+
type Response = {
|
|
152
|
+
statusCode?: number;
|
|
153
|
+
headers?: {
|
|
154
|
+
[key: string]: string;
|
|
155
|
+
};
|
|
156
|
+
body?: string;
|
|
157
|
+
message?: string;
|
|
158
|
+
};
|
|
159
|
+
declare class HttpError extends Error {
|
|
160
|
+
readonly statusCode: number;
|
|
161
|
+
readonly message: string;
|
|
162
|
+
constructor({ statusCode, message }: {
|
|
163
|
+
statusCode?: number;
|
|
164
|
+
message: string;
|
|
165
|
+
});
|
|
166
|
+
}
|
|
167
|
+
declare class Http<TParams extends Record<string, any> = any, TCookie extends Record<string, string> = any, TSession extends Record<string, string> = any> implements Plugin {
|
|
168
|
+
readonly type: string;
|
|
169
|
+
readonly name: string;
|
|
170
|
+
headers: {
|
|
171
|
+
[key: string]: string;
|
|
172
|
+
};
|
|
173
|
+
body: any;
|
|
174
|
+
params: TParams;
|
|
175
|
+
cookie: Cookie<TCookie, TSession>;
|
|
176
|
+
session: Session<TSession, TCookie>;
|
|
177
|
+
config: HttpConfig<TParams, TCookie, TSession>;
|
|
178
|
+
private readonly validatorOptions?;
|
|
179
|
+
private response?;
|
|
180
|
+
private validator?;
|
|
181
|
+
constructor(config?: HttpConfig<TParams, TCookie, TSession>);
|
|
182
|
+
onDeploy(data: DeployData, next: Next): Promise<void>;
|
|
183
|
+
onMount(data: MountData, next: Next): Promise<void>;
|
|
184
|
+
onInvoke(data: InvokeData, next: Next): Promise<void>;
|
|
185
|
+
/**
|
|
186
|
+
* set header
|
|
187
|
+
* @param key {string} key
|
|
188
|
+
* @param value {*} value
|
|
189
|
+
*/
|
|
190
|
+
setHeader(key: string, value: string): Http<TParams, TCookie, TSession>;
|
|
191
|
+
/**
|
|
192
|
+
* set Content-Type
|
|
193
|
+
* @param type {string} 类型
|
|
194
|
+
* @param charset {string} 编码
|
|
195
|
+
*/
|
|
196
|
+
setContentType(type: string, charset?: string): Http<TParams, TCookie, TSession>;
|
|
197
|
+
/**
|
|
198
|
+
* set status code
|
|
199
|
+
* @param code {number} 状态码
|
|
200
|
+
*/
|
|
201
|
+
setStatusCode(code: number): Http<TParams, TCookie, TSession>;
|
|
202
|
+
/**
|
|
203
|
+
* set body
|
|
204
|
+
* @param body {*} 内容
|
|
205
|
+
*/
|
|
206
|
+
setBody(body: string): Http<TParams, TCookie, TSession>;
|
|
207
|
+
}
|
|
208
|
+
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>>;
|
|
209
|
+
|
|
210
|
+
export { ContentType, Cookie, CookieOptions, Http, HttpConfig, HttpError, Response, Session, SessionOptions, Validator, ValidatorConfig, ValidatorOptions, ValidatorRuleOptions, useHttp };
|
package/dist/index.d.ts
CHANGED
|
@@ -31,7 +31,7 @@ declare class Session<S extends Record<string, string> = any, C extends Record<s
|
|
|
31
31
|
private readonly cookie;
|
|
32
32
|
private changed?;
|
|
33
33
|
constructor(cookie: Cookie<C, S>, config: SessionOptions);
|
|
34
|
-
invoke(cookie?: string): void;
|
|
34
|
+
invoke(cookie?: string, logger?: Logger): void;
|
|
35
35
|
encode(text: SessionContent): string;
|
|
36
36
|
decode<TData = any>(text: string): TData | SessionContent;
|
|
37
37
|
read(key: string): string | number;
|
|
@@ -61,9 +61,10 @@ declare class Cookie<C extends Record<string, string> = any, S extends Record<st
|
|
|
61
61
|
sameSite?: 'Strict' | 'Lax' | 'None';
|
|
62
62
|
session: SessionOptions;
|
|
63
63
|
};
|
|
64
|
+
logger: Logger;
|
|
64
65
|
private setCookie;
|
|
65
|
-
constructor(config: CookieOptions);
|
|
66
|
-
invoke(cookie: string | undefined): Cookie<C, S>;
|
|
66
|
+
constructor(config: CookieOptions, logger?: Logger);
|
|
67
|
+
invoke(cookie: string | undefined, logger: Logger): Cookie<C, S>;
|
|
67
68
|
read(key: string): any;
|
|
68
69
|
write(key: string, value: string, opts?: {
|
|
69
70
|
domain?: string;
|
package/dist/index.js
CHANGED
|
@@ -1,76 +1,26 @@
|
|
|
1
|
-
|
|
2
|
-
var __defProp = Object.defineProperty;
|
|
3
|
-
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
-
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
-
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
-
var __export = (target, all) => {
|
|
7
|
-
for (var name in all)
|
|
8
|
-
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
-
};
|
|
10
|
-
var __copyProps = (to, from, except, desc) => {
|
|
11
|
-
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
-
for (let key of __getOwnPropNames(from))
|
|
13
|
-
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
-
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
-
}
|
|
16
|
-
return to;
|
|
17
|
-
};
|
|
18
|
-
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
-
|
|
20
|
-
// src/index.ts
|
|
21
|
-
var src_exports = {};
|
|
22
|
-
__export(src_exports, {
|
|
23
|
-
ContentType: () => ContentType,
|
|
24
|
-
Cookie: () => Cookie,
|
|
25
|
-
Http: () => Http,
|
|
26
|
-
HttpError: () => HttpError,
|
|
27
|
-
Session: () => Session,
|
|
28
|
-
Validator: () => Validator,
|
|
29
|
-
useHttp: () => useHttp
|
|
30
|
-
});
|
|
31
|
-
module.exports = __toCommonJS(src_exports);
|
|
32
|
-
var import_func = require("@faasjs/func");
|
|
33
|
-
|
|
34
|
-
// ../deep_merge/src/index.ts
|
|
35
|
-
var shouldMerge = function(item) {
|
|
36
|
-
const type = Object.prototype.toString.call(item);
|
|
37
|
-
return type === "[object Object]" || type === "[object Array]";
|
|
38
|
-
};
|
|
39
|
-
function deepMerge(...sources) {
|
|
40
|
-
let acc = /* @__PURE__ */ Object.create(null);
|
|
41
|
-
for (const source of sources)
|
|
42
|
-
if (source instanceof Array) {
|
|
43
|
-
if (!(acc instanceof Array))
|
|
44
|
-
acc = [];
|
|
45
|
-
acc = [...new Set(source.concat(...acc))];
|
|
46
|
-
} else if (shouldMerge(source))
|
|
47
|
-
for (const [key, value] of Object.entries(source)) {
|
|
48
|
-
let val;
|
|
49
|
-
if (shouldMerge(value))
|
|
50
|
-
val = deepMerge(acc[key], value);
|
|
51
|
-
else
|
|
52
|
-
val = value;
|
|
53
|
-
acc = {
|
|
54
|
-
...acc,
|
|
55
|
-
[key]: val
|
|
56
|
-
};
|
|
57
|
-
}
|
|
58
|
-
return acc;
|
|
59
|
-
}
|
|
1
|
+
'use strict';
|
|
60
2
|
|
|
61
|
-
|
|
62
|
-
var
|
|
3
|
+
var func = require('@faasjs/func');
|
|
4
|
+
var deep_merge = require('@faasjs/deep_merge');
|
|
5
|
+
var logger = require('@faasjs/logger');
|
|
6
|
+
var crypto = require('crypto');
|
|
7
|
+
var zlib = require('zlib');
|
|
63
8
|
|
|
64
|
-
|
|
65
|
-
|
|
9
|
+
var __require = /* @__PURE__ */ ((x) => typeof require !== "undefined" ? require : typeof Proxy !== "undefined" ? new Proxy(x, {
|
|
10
|
+
get: (a, b) => (typeof require !== "undefined" ? require : a)[b]
|
|
11
|
+
}) : x)(function(x) {
|
|
12
|
+
if (typeof require !== "undefined")
|
|
13
|
+
return require.apply(this, arguments);
|
|
14
|
+
throw Error('Dynamic require of "' + x + '" is not supported');
|
|
15
|
+
});
|
|
66
16
|
var Session = class {
|
|
67
17
|
constructor(cookie, config) {
|
|
68
18
|
this.cookie = cookie;
|
|
69
19
|
if (!(config == null ? void 0 : config.secret))
|
|
70
|
-
|
|
20
|
+
cookie.logger.warn("Session's secret is missing.");
|
|
71
21
|
this.config = Object.assign({
|
|
72
22
|
key: "key",
|
|
73
|
-
secret:
|
|
23
|
+
secret: crypto.randomBytes(128).toString("hex"),
|
|
74
24
|
salt: "salt",
|
|
75
25
|
signedSalt: "signedSalt",
|
|
76
26
|
keylen: 64,
|
|
@@ -78,14 +28,14 @@ var Session = class {
|
|
|
78
28
|
digest: "sha256",
|
|
79
29
|
cipherName: "aes-256-cbc"
|
|
80
30
|
}, config);
|
|
81
|
-
this.secret =
|
|
31
|
+
this.secret = crypto.pbkdf2Sync(
|
|
82
32
|
this.config.secret,
|
|
83
33
|
this.config.salt,
|
|
84
34
|
this.config.iterations,
|
|
85
35
|
this.config.keylen / 2,
|
|
86
36
|
this.config.digest
|
|
87
37
|
);
|
|
88
|
-
this.signedSecret =
|
|
38
|
+
this.signedSecret = crypto.pbkdf2Sync(
|
|
89
39
|
this.config.secret,
|
|
90
40
|
this.config.signedSalt,
|
|
91
41
|
this.config.iterations,
|
|
@@ -94,11 +44,11 @@ var Session = class {
|
|
|
94
44
|
);
|
|
95
45
|
this.content = /* @__PURE__ */ Object.create(null);
|
|
96
46
|
}
|
|
97
|
-
invoke(cookie) {
|
|
47
|
+
invoke(cookie, logger) {
|
|
98
48
|
try {
|
|
99
49
|
this.content = cookie ? this.decode(cookie) : /* @__PURE__ */ Object.create(null);
|
|
100
50
|
} catch (error) {
|
|
101
|
-
|
|
51
|
+
logger == null ? void 0 : logger.error(error);
|
|
102
52
|
this.content = /* @__PURE__ */ Object.create(null);
|
|
103
53
|
}
|
|
104
54
|
this.changed = false;
|
|
@@ -106,11 +56,11 @@ var Session = class {
|
|
|
106
56
|
encode(text) {
|
|
107
57
|
if (typeof text !== "string")
|
|
108
58
|
text = JSON.stringify(text);
|
|
109
|
-
const iv =
|
|
110
|
-
const cipher =
|
|
59
|
+
const iv = crypto.randomBytes(16);
|
|
60
|
+
const cipher = crypto.createCipheriv(this.config.cipherName, this.secret, iv);
|
|
111
61
|
const encrypted = Buffer.concat([cipher.update(text), cipher.final()]).toString("base64");
|
|
112
62
|
const main = Buffer.from([encrypted, iv.toString("base64")].join("--")).toString("base64");
|
|
113
|
-
const hmac =
|
|
63
|
+
const hmac = crypto.createHmac(this.config.digest, this.signedSecret);
|
|
114
64
|
hmac.update(main);
|
|
115
65
|
const digest = hmac.digest("hex");
|
|
116
66
|
return main + "--" + digest;
|
|
@@ -118,7 +68,7 @@ var Session = class {
|
|
|
118
68
|
decode(text) {
|
|
119
69
|
text = decodeURIComponent(text);
|
|
120
70
|
const signedParts = text.split("--");
|
|
121
|
-
const hmac =
|
|
71
|
+
const hmac = crypto.createHmac(this.config.digest, this.signedSecret);
|
|
122
72
|
hmac.update(signedParts[0]);
|
|
123
73
|
const digest = hmac.digest("hex");
|
|
124
74
|
if (signedParts[1] !== digest)
|
|
@@ -127,7 +77,7 @@ var Session = class {
|
|
|
127
77
|
const parts = message.split("--").map(function(part2) {
|
|
128
78
|
return Buffer.from(part2, "base64");
|
|
129
79
|
});
|
|
130
|
-
const cipher =
|
|
80
|
+
const cipher = crypto.createDecipheriv(this.config.cipherName, this.secret, parts[1]);
|
|
131
81
|
const part = Buffer.from(cipher.update(parts[0])).toString("utf8");
|
|
132
82
|
const final = cipher.final("utf8");
|
|
133
83
|
const decrypt = [part, final].join("");
|
|
@@ -150,11 +100,10 @@ var Session = class {
|
|
|
150
100
|
return this;
|
|
151
101
|
}
|
|
152
102
|
};
|
|
153
|
-
|
|
154
|
-
// src/cookie.ts
|
|
155
103
|
var Cookie = class {
|
|
156
|
-
constructor(config) {
|
|
157
|
-
this.
|
|
104
|
+
constructor(config, logger) {
|
|
105
|
+
this.logger = logger;
|
|
106
|
+
this.config = deep_merge.deepMerge({
|
|
158
107
|
path: "/",
|
|
159
108
|
expires: 31536e3,
|
|
160
109
|
secure: true,
|
|
@@ -165,7 +114,7 @@ var Cookie = class {
|
|
|
165
114
|
this.content = /* @__PURE__ */ Object.create(null);
|
|
166
115
|
this.setCookie = /* @__PURE__ */ Object.create(null);
|
|
167
116
|
}
|
|
168
|
-
invoke(cookie) {
|
|
117
|
+
invoke(cookie, logger) {
|
|
169
118
|
this.content = /* @__PURE__ */ Object.create(null);
|
|
170
119
|
if (cookie)
|
|
171
120
|
cookie.split(";").forEach((x) => {
|
|
@@ -175,7 +124,7 @@ var Cookie = class {
|
|
|
175
124
|
this.content[k[0]] = decodeURIComponent(x.replace(`${k[0]}=`, "").replace(/;$/, ""));
|
|
176
125
|
});
|
|
177
126
|
this.setCookie = /* @__PURE__ */ Object.create(null);
|
|
178
|
-
this.session.invoke(this.read(this.session.config.key));
|
|
127
|
+
this.session.invoke(this.read(this.session.config.key), logger);
|
|
179
128
|
return this;
|
|
180
129
|
}
|
|
181
130
|
read(key) {
|
|
@@ -398,9 +347,6 @@ var Validator = class {
|
|
|
398
347
|
}
|
|
399
348
|
}
|
|
400
349
|
};
|
|
401
|
-
|
|
402
|
-
// src/index.ts
|
|
403
|
-
var import_zlib = require("zlib");
|
|
404
350
|
var ContentType = {
|
|
405
351
|
plain: "text/plain",
|
|
406
352
|
html: "text/html",
|
|
@@ -411,14 +357,14 @@ var ContentType = {
|
|
|
411
357
|
json: "application/json",
|
|
412
358
|
jsonp: "application/javascript"
|
|
413
359
|
};
|
|
414
|
-
var HttpError = class extends Error {
|
|
360
|
+
var HttpError = class _HttpError extends Error {
|
|
415
361
|
constructor({
|
|
416
362
|
statusCode,
|
|
417
363
|
message
|
|
418
364
|
}) {
|
|
419
365
|
super(message);
|
|
420
366
|
if (Error.captureStackTrace)
|
|
421
|
-
Error.captureStackTrace(this,
|
|
367
|
+
Error.captureStackTrace(this, _HttpError);
|
|
422
368
|
this.statusCode = statusCode || 500;
|
|
423
369
|
this.message = message;
|
|
424
370
|
}
|
|
@@ -437,10 +383,10 @@ var Http = class {
|
|
|
437
383
|
var _a;
|
|
438
384
|
data.dependencies["@faasjs/http"] = "*";
|
|
439
385
|
await next();
|
|
440
|
-
const logger = new
|
|
441
|
-
logger.debug("Generate api gateway's config");
|
|
442
|
-
logger.debug("%j", data);
|
|
443
|
-
const config = data.config.plugins ? deepMerge(data.config.plugins[this.name || this.type], { config: this.config }) : { config: this.config };
|
|
386
|
+
const logger$1 = new logger.Logger(this.name);
|
|
387
|
+
logger$1.debug("Generate api gateway's config");
|
|
388
|
+
logger$1.debug("%j", data);
|
|
389
|
+
const config = data.config.plugins ? deep_merge.deepMerge(data.config.plugins[this.name || this.type], { config: this.config }) : { config: this.config };
|
|
444
390
|
if (!config.config.path) {
|
|
445
391
|
config.config.path = "/" + ((_a = data.name) == null ? void 0 : _a.replace(/_/g, "/").replace(/\/index$/, ""));
|
|
446
392
|
if (config.config.path === "/index")
|
|
@@ -451,8 +397,8 @@ var Http = class {
|
|
|
451
397
|
config.config.path = "/";
|
|
452
398
|
}
|
|
453
399
|
}
|
|
454
|
-
logger.debug("Api gateway's config: %j", config);
|
|
455
|
-
const Provider =
|
|
400
|
+
logger$1.debug("Api gateway's config: %j", config);
|
|
401
|
+
const Provider = __require(config.provider.type).Provider;
|
|
456
402
|
const provider = new Provider(config.provider.config);
|
|
457
403
|
await provider.deploy(this.type, data, config);
|
|
458
404
|
}
|
|
@@ -476,9 +422,9 @@ var Http = class {
|
|
|
476
422
|
this.config[key] = value;
|
|
477
423
|
}
|
|
478
424
|
if (data.config.plugins && data.config.plugins[this.name || this.type])
|
|
479
|
-
this.config = deepMerge(this.config, data.config.plugins[this.name || this.type].config);
|
|
425
|
+
this.config = deep_merge.deepMerge(this.config, data.config.plugins[this.name || this.type].config);
|
|
480
426
|
data.logger.debug("[onMount] prepare cookie & session");
|
|
481
|
-
this.cookie = new Cookie(this.config.cookie || {});
|
|
427
|
+
this.cookie = new Cookie(this.config.cookie || {}, data.logger);
|
|
482
428
|
this.session = this.cookie.session;
|
|
483
429
|
if (this.validatorOptions) {
|
|
484
430
|
data.logger.debug("[onMount] prepare validator");
|
|
@@ -504,7 +450,7 @@ var Http = class {
|
|
|
504
450
|
delete this.params["_"];
|
|
505
451
|
data.logger.debug("[onInvoke] Params: %j", this.params);
|
|
506
452
|
}
|
|
507
|
-
this.cookie.invoke(this.headers.cookie);
|
|
453
|
+
this.cookie.invoke(this.headers.cookie, data.logger);
|
|
508
454
|
if (this.headers.cookie) {
|
|
509
455
|
data.logger.debug("[onInvoke] Cookie: %j", this.cookie.content);
|
|
510
456
|
data.logger.debug("[onInvoke] Session: %j", this.session.content);
|
|
@@ -556,13 +502,13 @@ var Http = class {
|
|
|
556
502
|
try {
|
|
557
503
|
if (acceptEncoding.includes("br")) {
|
|
558
504
|
data.response.headers["Content-Encoding"] = "br";
|
|
559
|
-
data.response.body =
|
|
505
|
+
data.response.body = zlib.brotliCompressSync(originBody).toString("base64");
|
|
560
506
|
} else if (acceptEncoding.includes("gzip")) {
|
|
561
507
|
data.response.headers["Content-Encoding"] = "gzip";
|
|
562
|
-
data.response.body =
|
|
508
|
+
data.response.body = zlib.gzipSync(originBody).toString("base64");
|
|
563
509
|
} else if (acceptEncoding.includes("deflate")) {
|
|
564
510
|
data.response.headers["Content-Encoding"] = "deflate";
|
|
565
|
-
data.response.body =
|
|
511
|
+
data.response.body = zlib.deflateSync(originBody).toString("base64");
|
|
566
512
|
} else
|
|
567
513
|
throw Error("No matched compression.");
|
|
568
514
|
data.response.isBase64Encoded = true;
|
|
@@ -572,10 +518,20 @@ var Http = class {
|
|
|
572
518
|
delete data.response.headers["Content-Encoding"];
|
|
573
519
|
}
|
|
574
520
|
}
|
|
521
|
+
/**
|
|
522
|
+
* set header
|
|
523
|
+
* @param key {string} key
|
|
524
|
+
* @param value {*} value
|
|
525
|
+
*/
|
|
575
526
|
setHeader(key, value) {
|
|
576
527
|
this.response.headers[key] = value;
|
|
577
528
|
return this;
|
|
578
529
|
}
|
|
530
|
+
/**
|
|
531
|
+
* set Content-Type
|
|
532
|
+
* @param type {string} 类型
|
|
533
|
+
* @param charset {string} 编码
|
|
534
|
+
*/
|
|
579
535
|
setContentType(type, charset = "utf-8") {
|
|
580
536
|
if (ContentType[type])
|
|
581
537
|
this.setHeader("Content-Type", `${ContentType[type]}; charset=${charset}`);
|
|
@@ -583,25 +539,31 @@ var Http = class {
|
|
|
583
539
|
this.setHeader("Content-Type", `${type}; charset=${charset}`);
|
|
584
540
|
return this;
|
|
585
541
|
}
|
|
542
|
+
/**
|
|
543
|
+
* set status code
|
|
544
|
+
* @param code {number} 状态码
|
|
545
|
+
*/
|
|
586
546
|
setStatusCode(code) {
|
|
587
547
|
this.response.statusCode = code;
|
|
588
548
|
return this;
|
|
589
549
|
}
|
|
550
|
+
/**
|
|
551
|
+
* set body
|
|
552
|
+
* @param body {*} 内容
|
|
553
|
+
*/
|
|
590
554
|
setBody(body) {
|
|
591
555
|
this.response.body = body;
|
|
592
556
|
return this;
|
|
593
557
|
}
|
|
594
558
|
};
|
|
595
559
|
function useHttp(config) {
|
|
596
|
-
return
|
|
560
|
+
return func.usePlugin(new Http(config));
|
|
597
561
|
}
|
|
598
|
-
|
|
599
|
-
|
|
600
|
-
|
|
601
|
-
|
|
602
|
-
|
|
603
|
-
|
|
604
|
-
|
|
605
|
-
|
|
606
|
-
useHttp
|
|
607
|
-
});
|
|
562
|
+
|
|
563
|
+
exports.ContentType = ContentType;
|
|
564
|
+
exports.Cookie = Cookie;
|
|
565
|
+
exports.Http = Http;
|
|
566
|
+
exports.HttpError = HttpError;
|
|
567
|
+
exports.Session = Session;
|
|
568
|
+
exports.Validator = Validator;
|
|
569
|
+
exports.useHttp = useHttp;
|
package/dist/index.mjs
CHANGED
|
@@ -1,59 +1,21 @@
|
|
|
1
|
+
import { usePlugin } from '@faasjs/func';
|
|
2
|
+
import { deepMerge } from '@faasjs/deep_merge';
|
|
3
|
+
import { Logger } from '@faasjs/logger';
|
|
4
|
+
import { randomBytes, pbkdf2Sync, createCipheriv, createHmac, createDecipheriv } from 'crypto';
|
|
5
|
+
import { brotliCompressSync, gzipSync, deflateSync } from 'zlib';
|
|
6
|
+
|
|
1
7
|
var __require = /* @__PURE__ */ ((x) => typeof require !== "undefined" ? require : typeof Proxy !== "undefined" ? new Proxy(x, {
|
|
2
8
|
get: (a, b) => (typeof require !== "undefined" ? require : a)[b]
|
|
3
9
|
}) : x)(function(x) {
|
|
4
10
|
if (typeof require !== "undefined")
|
|
5
11
|
return require.apply(this, arguments);
|
|
6
|
-
throw
|
|
12
|
+
throw Error('Dynamic require of "' + x + '" is not supported');
|
|
7
13
|
});
|
|
8
|
-
|
|
9
|
-
// src/index.ts
|
|
10
|
-
import {
|
|
11
|
-
usePlugin
|
|
12
|
-
} from "@faasjs/func";
|
|
13
|
-
|
|
14
|
-
// ../deep_merge/src/index.ts
|
|
15
|
-
var shouldMerge = function(item) {
|
|
16
|
-
const type = Object.prototype.toString.call(item);
|
|
17
|
-
return type === "[object Object]" || type === "[object Array]";
|
|
18
|
-
};
|
|
19
|
-
function deepMerge(...sources) {
|
|
20
|
-
let acc = /* @__PURE__ */ Object.create(null);
|
|
21
|
-
for (const source of sources)
|
|
22
|
-
if (source instanceof Array) {
|
|
23
|
-
if (!(acc instanceof Array))
|
|
24
|
-
acc = [];
|
|
25
|
-
acc = [...new Set(source.concat(...acc))];
|
|
26
|
-
} else if (shouldMerge(source))
|
|
27
|
-
for (const [key, value] of Object.entries(source)) {
|
|
28
|
-
let val;
|
|
29
|
-
if (shouldMerge(value))
|
|
30
|
-
val = deepMerge(acc[key], value);
|
|
31
|
-
else
|
|
32
|
-
val = value;
|
|
33
|
-
acc = {
|
|
34
|
-
...acc,
|
|
35
|
-
[key]: val
|
|
36
|
-
};
|
|
37
|
-
}
|
|
38
|
-
return acc;
|
|
39
|
-
}
|
|
40
|
-
|
|
41
|
-
// src/index.ts
|
|
42
|
-
import { Logger } from "@faasjs/logger";
|
|
43
|
-
|
|
44
|
-
// src/session.ts
|
|
45
|
-
import {
|
|
46
|
-
randomBytes,
|
|
47
|
-
pbkdf2Sync,
|
|
48
|
-
createCipheriv,
|
|
49
|
-
createHmac,
|
|
50
|
-
createDecipheriv
|
|
51
|
-
} from "crypto";
|
|
52
14
|
var Session = class {
|
|
53
15
|
constructor(cookie, config) {
|
|
54
16
|
this.cookie = cookie;
|
|
55
17
|
if (!(config == null ? void 0 : config.secret))
|
|
56
|
-
|
|
18
|
+
cookie.logger.warn("Session's secret is missing.");
|
|
57
19
|
this.config = Object.assign({
|
|
58
20
|
key: "key",
|
|
59
21
|
secret: randomBytes(128).toString("hex"),
|
|
@@ -80,11 +42,11 @@ var Session = class {
|
|
|
80
42
|
);
|
|
81
43
|
this.content = /* @__PURE__ */ Object.create(null);
|
|
82
44
|
}
|
|
83
|
-
invoke(cookie) {
|
|
45
|
+
invoke(cookie, logger) {
|
|
84
46
|
try {
|
|
85
47
|
this.content = cookie ? this.decode(cookie) : /* @__PURE__ */ Object.create(null);
|
|
86
48
|
} catch (error) {
|
|
87
|
-
|
|
49
|
+
logger == null ? void 0 : logger.error(error);
|
|
88
50
|
this.content = /* @__PURE__ */ Object.create(null);
|
|
89
51
|
}
|
|
90
52
|
this.changed = false;
|
|
@@ -136,10 +98,9 @@ var Session = class {
|
|
|
136
98
|
return this;
|
|
137
99
|
}
|
|
138
100
|
};
|
|
139
|
-
|
|
140
|
-
// src/cookie.ts
|
|
141
101
|
var Cookie = class {
|
|
142
|
-
constructor(config) {
|
|
102
|
+
constructor(config, logger) {
|
|
103
|
+
this.logger = logger;
|
|
143
104
|
this.config = deepMerge({
|
|
144
105
|
path: "/",
|
|
145
106
|
expires: 31536e3,
|
|
@@ -151,7 +112,7 @@ var Cookie = class {
|
|
|
151
112
|
this.content = /* @__PURE__ */ Object.create(null);
|
|
152
113
|
this.setCookie = /* @__PURE__ */ Object.create(null);
|
|
153
114
|
}
|
|
154
|
-
invoke(cookie) {
|
|
115
|
+
invoke(cookie, logger) {
|
|
155
116
|
this.content = /* @__PURE__ */ Object.create(null);
|
|
156
117
|
if (cookie)
|
|
157
118
|
cookie.split(";").forEach((x) => {
|
|
@@ -161,7 +122,7 @@ var Cookie = class {
|
|
|
161
122
|
this.content[k[0]] = decodeURIComponent(x.replace(`${k[0]}=`, "").replace(/;$/, ""));
|
|
162
123
|
});
|
|
163
124
|
this.setCookie = /* @__PURE__ */ Object.create(null);
|
|
164
|
-
this.session.invoke(this.read(this.session.config.key));
|
|
125
|
+
this.session.invoke(this.read(this.session.config.key), logger);
|
|
165
126
|
return this;
|
|
166
127
|
}
|
|
167
128
|
read(key) {
|
|
@@ -384,13 +345,6 @@ var Validator = class {
|
|
|
384
345
|
}
|
|
385
346
|
}
|
|
386
347
|
};
|
|
387
|
-
|
|
388
|
-
// src/index.ts
|
|
389
|
-
import {
|
|
390
|
-
gzipSync,
|
|
391
|
-
deflateSync,
|
|
392
|
-
brotliCompressSync
|
|
393
|
-
} from "zlib";
|
|
394
348
|
var ContentType = {
|
|
395
349
|
plain: "text/plain",
|
|
396
350
|
html: "text/html",
|
|
@@ -401,14 +355,14 @@ var ContentType = {
|
|
|
401
355
|
json: "application/json",
|
|
402
356
|
jsonp: "application/javascript"
|
|
403
357
|
};
|
|
404
|
-
var HttpError = class extends Error {
|
|
358
|
+
var HttpError = class _HttpError extends Error {
|
|
405
359
|
constructor({
|
|
406
360
|
statusCode,
|
|
407
361
|
message
|
|
408
362
|
}) {
|
|
409
363
|
super(message);
|
|
410
364
|
if (Error.captureStackTrace)
|
|
411
|
-
Error.captureStackTrace(this,
|
|
365
|
+
Error.captureStackTrace(this, _HttpError);
|
|
412
366
|
this.statusCode = statusCode || 500;
|
|
413
367
|
this.message = message;
|
|
414
368
|
}
|
|
@@ -468,7 +422,7 @@ var Http = class {
|
|
|
468
422
|
if (data.config.plugins && data.config.plugins[this.name || this.type])
|
|
469
423
|
this.config = deepMerge(this.config, data.config.plugins[this.name || this.type].config);
|
|
470
424
|
data.logger.debug("[onMount] prepare cookie & session");
|
|
471
|
-
this.cookie = new Cookie(this.config.cookie || {});
|
|
425
|
+
this.cookie = new Cookie(this.config.cookie || {}, data.logger);
|
|
472
426
|
this.session = this.cookie.session;
|
|
473
427
|
if (this.validatorOptions) {
|
|
474
428
|
data.logger.debug("[onMount] prepare validator");
|
|
@@ -494,7 +448,7 @@ var Http = class {
|
|
|
494
448
|
delete this.params["_"];
|
|
495
449
|
data.logger.debug("[onInvoke] Params: %j", this.params);
|
|
496
450
|
}
|
|
497
|
-
this.cookie.invoke(this.headers.cookie);
|
|
451
|
+
this.cookie.invoke(this.headers.cookie, data.logger);
|
|
498
452
|
if (this.headers.cookie) {
|
|
499
453
|
data.logger.debug("[onInvoke] Cookie: %j", this.cookie.content);
|
|
500
454
|
data.logger.debug("[onInvoke] Session: %j", this.session.content);
|
|
@@ -562,10 +516,20 @@ var Http = class {
|
|
|
562
516
|
delete data.response.headers["Content-Encoding"];
|
|
563
517
|
}
|
|
564
518
|
}
|
|
519
|
+
/**
|
|
520
|
+
* set header
|
|
521
|
+
* @param key {string} key
|
|
522
|
+
* @param value {*} value
|
|
523
|
+
*/
|
|
565
524
|
setHeader(key, value) {
|
|
566
525
|
this.response.headers[key] = value;
|
|
567
526
|
return this;
|
|
568
527
|
}
|
|
528
|
+
/**
|
|
529
|
+
* set Content-Type
|
|
530
|
+
* @param type {string} 类型
|
|
531
|
+
* @param charset {string} 编码
|
|
532
|
+
*/
|
|
569
533
|
setContentType(type, charset = "utf-8") {
|
|
570
534
|
if (ContentType[type])
|
|
571
535
|
this.setHeader("Content-Type", `${ContentType[type]}; charset=${charset}`);
|
|
@@ -573,10 +537,18 @@ var Http = class {
|
|
|
573
537
|
this.setHeader("Content-Type", `${type}; charset=${charset}`);
|
|
574
538
|
return this;
|
|
575
539
|
}
|
|
540
|
+
/**
|
|
541
|
+
* set status code
|
|
542
|
+
* @param code {number} 状态码
|
|
543
|
+
*/
|
|
576
544
|
setStatusCode(code) {
|
|
577
545
|
this.response.statusCode = code;
|
|
578
546
|
return this;
|
|
579
547
|
}
|
|
548
|
+
/**
|
|
549
|
+
* set body
|
|
550
|
+
* @param body {*} 内容
|
|
551
|
+
*/
|
|
580
552
|
setBody(body) {
|
|
581
553
|
this.response.body = body;
|
|
582
554
|
return this;
|
|
@@ -585,12 +557,5 @@ var Http = class {
|
|
|
585
557
|
function useHttp(config) {
|
|
586
558
|
return usePlugin(new Http(config));
|
|
587
559
|
}
|
|
588
|
-
|
|
589
|
-
|
|
590
|
-
Cookie,
|
|
591
|
-
Http,
|
|
592
|
-
HttpError,
|
|
593
|
-
Session,
|
|
594
|
-
Validator,
|
|
595
|
-
useHttp
|
|
596
|
-
};
|
|
560
|
+
|
|
561
|
+
export { ContentType, Cookie, Http, HttpError, Session, Validator, useHttp };
|
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.88",
|
|
4
4
|
"license": "MIT",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"types": "dist/index.d.ts",
|
|
@@ -16,14 +16,14 @@
|
|
|
16
16
|
},
|
|
17
17
|
"funding": "https://github.com/sponsors/faasjs",
|
|
18
18
|
"scripts": {
|
|
19
|
-
"build": "tsup-node src/index.ts --
|
|
19
|
+
"build": "tsup-node src/index.ts --config ../../tsup.config.json"
|
|
20
20
|
},
|
|
21
21
|
"files": [
|
|
22
22
|
"dist"
|
|
23
23
|
],
|
|
24
24
|
"dependencies": {
|
|
25
|
-
"@faasjs/func": "^0.0.3-beta.
|
|
26
|
-
"@faasjs/logger": "^0.0.3-beta.
|
|
25
|
+
"@faasjs/func": "^0.0.3-beta.88",
|
|
26
|
+
"@faasjs/logger": "^0.0.3-beta.88"
|
|
27
27
|
},
|
|
28
28
|
"engines": {
|
|
29
29
|
"npm": ">=8.0.0",
|