@faasjs/http 2.3.1 → 2.5.0
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.js +38 -76
- package/dist/index.mjs +38 -76
- package/package.json +5 -5
package/dist/index.js
CHANGED
|
@@ -9,15 +9,13 @@ var zlib = require('zlib');
|
|
|
9
9
|
var __require = /* @__PURE__ */ ((x) => typeof require !== "undefined" ? require : typeof Proxy !== "undefined" ? new Proxy(x, {
|
|
10
10
|
get: (a, b) => (typeof require !== "undefined" ? require : a)[b]
|
|
11
11
|
}) : x)(function(x) {
|
|
12
|
-
if (typeof require !== "undefined")
|
|
13
|
-
return require.apply(this, arguments);
|
|
12
|
+
if (typeof require !== "undefined") return require.apply(this, arguments);
|
|
14
13
|
throw Error('Dynamic require of "' + x + '" is not supported');
|
|
15
14
|
});
|
|
16
15
|
var Session = class {
|
|
17
16
|
constructor(cookie, config) {
|
|
18
17
|
this.cookie = cookie;
|
|
19
|
-
if (!(config == null ? void 0 : config.secret))
|
|
20
|
-
cookie.logger.warn("Session's secret is missing.");
|
|
18
|
+
if (!(config == null ? void 0 : config.secret)) cookie.logger.warn("Session's secret is missing.");
|
|
21
19
|
this.config = Object.assign(
|
|
22
20
|
{
|
|
23
21
|
key: "key",
|
|
@@ -57,8 +55,7 @@ var Session = class {
|
|
|
57
55
|
this.changed = false;
|
|
58
56
|
}
|
|
59
57
|
encode(text) {
|
|
60
|
-
if (typeof text !== "string")
|
|
61
|
-
text = JSON.stringify(text);
|
|
58
|
+
if (typeof text !== "string") text = JSON.stringify(text);
|
|
62
59
|
const iv = crypto.randomBytes(16);
|
|
63
60
|
const cipher = crypto.createCipheriv(this.config.cipherName, this.secret, iv);
|
|
64
61
|
const encrypted = Buffer.concat([
|
|
@@ -79,8 +76,7 @@ var Session = class {
|
|
|
79
76
|
const hmac = crypto.createHmac(this.config.digest, this.signedSecret);
|
|
80
77
|
hmac.update(signedParts[0]);
|
|
81
78
|
const digest = hmac.digest("hex");
|
|
82
|
-
if (signedParts[1] !== digest)
|
|
83
|
-
throw Error("Session Not valid");
|
|
79
|
+
if (signedParts[1] !== digest) throw Error("Session Not valid");
|
|
84
80
|
const message = Buffer.from(signedParts[0], "base64").toString();
|
|
85
81
|
const parts = message.split("--").map((part2) => Buffer.from(part2, "base64"));
|
|
86
82
|
const cipher = crypto.createDecipheriv(
|
|
@@ -97,10 +93,8 @@ var Session = class {
|
|
|
97
93
|
return this.content[key];
|
|
98
94
|
}
|
|
99
95
|
write(key, value) {
|
|
100
|
-
if (value === null || typeof value === "undefined")
|
|
101
|
-
|
|
102
|
-
else
|
|
103
|
-
this.content[key] = value;
|
|
96
|
+
if (value === null || typeof value === "undefined") delete this.content[key];
|
|
97
|
+
else this.content[key] = value;
|
|
104
98
|
this.changed = true;
|
|
105
99
|
return this;
|
|
106
100
|
}
|
|
@@ -159,25 +153,19 @@ var Cookie = class {
|
|
|
159
153
|
cookie = `${key}=${encodeURIComponent(value)};`;
|
|
160
154
|
this.content[key] = value;
|
|
161
155
|
}
|
|
162
|
-
if (typeof opts.expires === "number")
|
|
163
|
-
cookie += `max-age=${opts.expires};`;
|
|
156
|
+
if (typeof opts.expires === "number") cookie += `max-age=${opts.expires};`;
|
|
164
157
|
else if (typeof opts.expires === "string")
|
|
165
158
|
cookie += `expires=${opts.expires};`;
|
|
166
159
|
cookie += `path=${opts.path || "/"};`;
|
|
167
|
-
if (opts.domain)
|
|
168
|
-
|
|
169
|
-
if (opts.
|
|
170
|
-
|
|
171
|
-
if (opts.httpOnly)
|
|
172
|
-
cookie += "HttpOnly;";
|
|
173
|
-
if (opts.sameSite)
|
|
174
|
-
cookie += `SameSite=${opts.sameSite};`;
|
|
160
|
+
if (opts.domain) cookie += `domain=${opts.domain};`;
|
|
161
|
+
if (opts.secure) cookie += "Secure;";
|
|
162
|
+
if (opts.httpOnly) cookie += "HttpOnly;";
|
|
163
|
+
if (opts.sameSite) cookie += `SameSite=${opts.sameSite};`;
|
|
175
164
|
this.setCookie[key] = cookie;
|
|
176
165
|
return this;
|
|
177
166
|
}
|
|
178
167
|
headers() {
|
|
179
|
-
if (Object.keys(this.setCookie).length === 0)
|
|
180
|
-
return {};
|
|
168
|
+
if (Object.keys(this.setCookie).length === 0) return {};
|
|
181
169
|
return { "Set-Cookie": Object.values(this.setCookie) };
|
|
182
170
|
}
|
|
183
171
|
};
|
|
@@ -193,8 +181,7 @@ var Validator = class {
|
|
|
193
181
|
async valid(request, logger) {
|
|
194
182
|
if (this.before) {
|
|
195
183
|
const result = await this.before(request);
|
|
196
|
-
if (result)
|
|
197
|
-
throw new HttpError(result);
|
|
184
|
+
if (result) throw new HttpError(result);
|
|
198
185
|
}
|
|
199
186
|
this.request = request;
|
|
200
187
|
if (this.paramsConfig && request.params) {
|
|
@@ -203,8 +190,7 @@ var Validator = class {
|
|
|
203
190
|
}
|
|
204
191
|
if (this.cookieConfig && request.cookie) {
|
|
205
192
|
logger.debug("Valid Cookie");
|
|
206
|
-
if (request.cookie == null)
|
|
207
|
-
throw Error("Not found Cookie");
|
|
193
|
+
if (request.cookie == null) throw Error("Not found Cookie");
|
|
208
194
|
this.validContent(
|
|
209
195
|
"cookie",
|
|
210
196
|
request.cookie.content,
|
|
@@ -215,8 +201,7 @@ var Validator = class {
|
|
|
215
201
|
}
|
|
216
202
|
if (this.sessionConfig && request.session) {
|
|
217
203
|
logger.debug("Valid Session");
|
|
218
|
-
if (request.session == null)
|
|
219
|
-
throw Error("Not found Session");
|
|
204
|
+
if (request.session == null) throw Error("Not found Session");
|
|
220
205
|
this.validContent(
|
|
221
206
|
"session",
|
|
222
207
|
request.session.content,
|
|
@@ -239,20 +224,17 @@ var Validator = class {
|
|
|
239
224
|
);
|
|
240
225
|
if (config.onError) {
|
|
241
226
|
const res = config.onError(`${type}.whitelist`, baseKey, diffKeys);
|
|
242
|
-
if (res)
|
|
243
|
-
throw new HttpError(res);
|
|
227
|
+
if (res) throw new HttpError(res);
|
|
244
228
|
}
|
|
245
229
|
throw error;
|
|
246
230
|
}
|
|
247
231
|
if (config.whitelist === "ignore")
|
|
248
|
-
for (const key of diff)
|
|
249
|
-
delete params[key];
|
|
232
|
+
for (const key of diff) delete params[key];
|
|
250
233
|
}
|
|
251
234
|
}
|
|
252
235
|
for (const key in config.rules) {
|
|
253
236
|
const rule = config.rules[key];
|
|
254
|
-
if (!rule)
|
|
255
|
-
continue;
|
|
237
|
+
if (!rule) continue;
|
|
256
238
|
let value = params[key];
|
|
257
239
|
if (rule.default) {
|
|
258
240
|
if (type === "cookie" || type === "session")
|
|
@@ -271,16 +253,14 @@ var Validator = class {
|
|
|
271
253
|
`${baseKey}${key}`,
|
|
272
254
|
value
|
|
273
255
|
);
|
|
274
|
-
if (res)
|
|
275
|
-
throw new HttpError(res);
|
|
256
|
+
if (res) throw new HttpError(res);
|
|
276
257
|
}
|
|
277
258
|
throw error;
|
|
278
259
|
}
|
|
279
260
|
}
|
|
280
261
|
if (typeof value !== "undefined" && value !== null) {
|
|
281
262
|
if (rule.type)
|
|
282
|
-
if (type === "cookie")
|
|
283
|
-
logger.warn("Cookie not support type rule");
|
|
263
|
+
if (type === "cookie") logger.warn("Cookie not support type rule");
|
|
284
264
|
else {
|
|
285
265
|
let typed = true;
|
|
286
266
|
switch (rule.type) {
|
|
@@ -304,8 +284,7 @@ var Validator = class {
|
|
|
304
284
|
`${baseKey}${key}`,
|
|
305
285
|
value
|
|
306
286
|
);
|
|
307
|
-
if (res)
|
|
308
|
-
throw new HttpError(res);
|
|
287
|
+
if (res) throw new HttpError(res);
|
|
309
288
|
}
|
|
310
289
|
throw error;
|
|
311
290
|
}
|
|
@@ -320,8 +299,7 @@ var Validator = class {
|
|
|
320
299
|
`${baseKey}${key}`,
|
|
321
300
|
value
|
|
322
301
|
);
|
|
323
|
-
if (res)
|
|
324
|
-
throw new HttpError(res);
|
|
302
|
+
if (res) throw new HttpError(res);
|
|
325
303
|
}
|
|
326
304
|
throw error;
|
|
327
305
|
}
|
|
@@ -335,14 +313,12 @@ var Validator = class {
|
|
|
335
313
|
`${baseKey}${key}`,
|
|
336
314
|
value
|
|
337
315
|
);
|
|
338
|
-
if (res)
|
|
339
|
-
throw new HttpError(res);
|
|
316
|
+
if (res) throw new HttpError(res);
|
|
340
317
|
}
|
|
341
318
|
throw error;
|
|
342
319
|
}
|
|
343
320
|
if (rule.config) {
|
|
344
|
-
if (type === "cookie")
|
|
345
|
-
logger.warn("Cookie not support nest rule.");
|
|
321
|
+
if (type === "cookie") logger.warn("Cookie not support nest rule.");
|
|
346
322
|
else if (Array.isArray(value))
|
|
347
323
|
for (const val of value)
|
|
348
324
|
this.validContent(
|
|
@@ -381,22 +357,18 @@ var HttpError = class _HttpError extends Error {
|
|
|
381
357
|
message
|
|
382
358
|
}) {
|
|
383
359
|
super(message);
|
|
384
|
-
if (Error.captureStackTrace)
|
|
385
|
-
Error.captureStackTrace(this, _HttpError);
|
|
360
|
+
if (Error.captureStackTrace) Error.captureStackTrace(this, _HttpError);
|
|
386
361
|
this.statusCode = statusCode || 500;
|
|
387
362
|
this.message = message;
|
|
388
363
|
}
|
|
389
364
|
};
|
|
390
365
|
var Name = "http";
|
|
391
366
|
function deepClone(obj) {
|
|
392
|
-
if (obj === null || typeof obj !== "object")
|
|
393
|
-
|
|
394
|
-
if (Array.isArray(obj))
|
|
395
|
-
return JSON.parse(JSON.stringify(obj));
|
|
367
|
+
if (obj === null || typeof obj !== "object") return obj;
|
|
368
|
+
if (Array.isArray(obj)) return JSON.parse(JSON.stringify(obj));
|
|
396
369
|
const clone = {};
|
|
397
370
|
for (const key in obj) {
|
|
398
|
-
if (!obj.hasOwnProperty(key))
|
|
399
|
-
continue;
|
|
371
|
+
if (!obj.hasOwnProperty(key)) continue;
|
|
400
372
|
if (typeof obj[key] === "function") {
|
|
401
373
|
clone[key] = obj[key];
|
|
402
374
|
continue;
|
|
@@ -411,8 +383,7 @@ var Http = class {
|
|
|
411
383
|
this.name = Name;
|
|
412
384
|
this.name = (config == null ? void 0 : config.name) || this.type;
|
|
413
385
|
this.config = (config == null ? void 0 : config.config) || /* @__PURE__ */ Object.create(null);
|
|
414
|
-
if (config == null ? void 0 : config.validator)
|
|
415
|
-
this.validatorOptions = config.validator;
|
|
386
|
+
if (config == null ? void 0 : config.validator) this.validatorOptions = config.validator;
|
|
416
387
|
}
|
|
417
388
|
async onDeploy(data, next) {
|
|
418
389
|
var _a;
|
|
@@ -426,15 +397,13 @@ var Http = class {
|
|
|
426
397
|
}) : { config: this.config };
|
|
427
398
|
if (!config.config.path) {
|
|
428
399
|
config.config.path = `/${(_a = data.name) == null ? void 0 : _a.replace(/_/g, "/").replace(/\/index$/, "")}`;
|
|
429
|
-
if (config.config.path === "/index")
|
|
430
|
-
config.config.path = "/";
|
|
400
|
+
if (config.config.path === "/index") config.config.path = "/";
|
|
431
401
|
if (config.config.ignorePathPrefix) {
|
|
432
402
|
config.config.path = config.config.path.replace(
|
|
433
403
|
new RegExp(`^${config.config.ignorePathPrefix}`),
|
|
434
404
|
""
|
|
435
405
|
);
|
|
436
|
-
if (config.config.path === "")
|
|
437
|
-
config.config.path = "/";
|
|
406
|
+
if (config.config.path === "") config.config.path = "/";
|
|
438
407
|
}
|
|
439
408
|
}
|
|
440
409
|
logger$1.debug("Api gateway's config: %j", config);
|
|
@@ -457,16 +426,13 @@ var Http = class {
|
|
|
457
426
|
let config = this.config;
|
|
458
427
|
const keys = key.split("_");
|
|
459
428
|
for (const k of keys.slice(0, keys.length - 1)) {
|
|
460
|
-
if (!config[k])
|
|
461
|
-
config[k] = /* @__PURE__ */ Object.create(null);
|
|
429
|
+
if (!config[k]) config[k] = /* @__PURE__ */ Object.create(null);
|
|
462
430
|
config = config[k];
|
|
463
431
|
}
|
|
464
432
|
config[keys[keys.length - 1]] = value;
|
|
465
|
-
} else
|
|
466
|
-
this.config[key] = value;
|
|
433
|
+
} else this.config[key] = value;
|
|
467
434
|
}
|
|
468
|
-
if (!data.config)
|
|
469
|
-
throw Error(`[${this.name}] Config not found.`);
|
|
435
|
+
if (!data.config) throw Error(`[${this.name}] Config not found.`);
|
|
470
436
|
if ((_b = data.config.plugins) == null ? void 0 : _b[this.name || this.type])
|
|
471
437
|
this.config = deep_merge.deepMerge(
|
|
472
438
|
this.config,
|
|
@@ -553,8 +519,7 @@ var Http = class {
|
|
|
553
519
|
}
|
|
554
520
|
} else if (Object.prototype.toString.call(data.response) === "[object Object]" && data.response.statusCode && data.response.headers)
|
|
555
521
|
this.response = data.response;
|
|
556
|
-
else
|
|
557
|
-
this.response.body = JSON.stringify({ data: data.response });
|
|
522
|
+
else this.response.body = JSON.stringify({ data: data.response });
|
|
558
523
|
if (!this.response.statusCode)
|
|
559
524
|
this.response.statusCode = this.response.body ? 200 : 201;
|
|
560
525
|
this.response.headers = Object.assign(
|
|
@@ -574,8 +539,7 @@ var Http = class {
|
|
|
574
539
|
if (!data.response.body || data.response.isBase64Encoded || typeof data.response.body !== "string" || data.response.body.length < 1024)
|
|
575
540
|
return;
|
|
576
541
|
const acceptEncoding = this.headers["accept-encoding"] || this.headers["Accept-Encoding"];
|
|
577
|
-
if (!acceptEncoding || !/(br|gzip|deflate)/.test(acceptEncoding))
|
|
578
|
-
return;
|
|
542
|
+
if (!acceptEncoding || !/(br|gzip|deflate)/.test(acceptEncoding)) return;
|
|
579
543
|
try {
|
|
580
544
|
if (acceptEncoding.includes("br")) {
|
|
581
545
|
data.response.headers["Content-Encoding"] = "br";
|
|
@@ -586,8 +550,7 @@ var Http = class {
|
|
|
586
550
|
} else if (acceptEncoding.includes("deflate")) {
|
|
587
551
|
data.response.headers["Content-Encoding"] = "deflate";
|
|
588
552
|
data.response.body = zlib.deflateSync(originBody).toString("base64");
|
|
589
|
-
} else
|
|
590
|
-
throw Error("No matched compression.");
|
|
553
|
+
} else throw Error("No matched compression.");
|
|
591
554
|
data.response.isBase64Encoded = true;
|
|
592
555
|
} catch (error) {
|
|
593
556
|
console.error(error);
|
|
@@ -612,8 +575,7 @@ var Http = class {
|
|
|
612
575
|
setContentType(type, charset = "utf-8") {
|
|
613
576
|
if (ContentType[type])
|
|
614
577
|
this.setHeader("Content-Type", `${ContentType[type]}; charset=${charset}`);
|
|
615
|
-
else
|
|
616
|
-
this.setHeader("Content-Type", `${type}; charset=${charset}`);
|
|
578
|
+
else this.setHeader("Content-Type", `${type}; charset=${charset}`);
|
|
617
579
|
return this;
|
|
618
580
|
}
|
|
619
581
|
/**
|
package/dist/index.mjs
CHANGED
|
@@ -7,15 +7,13 @@ import { brotliCompressSync, gzipSync, deflateSync } from 'node:zlib';
|
|
|
7
7
|
var __require = /* @__PURE__ */ ((x) => typeof require !== "undefined" ? require : typeof Proxy !== "undefined" ? new Proxy(x, {
|
|
8
8
|
get: (a, b) => (typeof require !== "undefined" ? require : a)[b]
|
|
9
9
|
}) : x)(function(x) {
|
|
10
|
-
if (typeof require !== "undefined")
|
|
11
|
-
return require.apply(this, arguments);
|
|
10
|
+
if (typeof require !== "undefined") return require.apply(this, arguments);
|
|
12
11
|
throw Error('Dynamic require of "' + x + '" is not supported');
|
|
13
12
|
});
|
|
14
13
|
var Session = class {
|
|
15
14
|
constructor(cookie, config) {
|
|
16
15
|
this.cookie = cookie;
|
|
17
|
-
if (!(config == null ? void 0 : config.secret))
|
|
18
|
-
cookie.logger.warn("Session's secret is missing.");
|
|
16
|
+
if (!(config == null ? void 0 : config.secret)) cookie.logger.warn("Session's secret is missing.");
|
|
19
17
|
this.config = Object.assign(
|
|
20
18
|
{
|
|
21
19
|
key: "key",
|
|
@@ -55,8 +53,7 @@ var Session = class {
|
|
|
55
53
|
this.changed = false;
|
|
56
54
|
}
|
|
57
55
|
encode(text) {
|
|
58
|
-
if (typeof text !== "string")
|
|
59
|
-
text = JSON.stringify(text);
|
|
56
|
+
if (typeof text !== "string") text = JSON.stringify(text);
|
|
60
57
|
const iv = randomBytes(16);
|
|
61
58
|
const cipher = createCipheriv(this.config.cipherName, this.secret, iv);
|
|
62
59
|
const encrypted = Buffer.concat([
|
|
@@ -77,8 +74,7 @@ var Session = class {
|
|
|
77
74
|
const hmac = createHmac(this.config.digest, this.signedSecret);
|
|
78
75
|
hmac.update(signedParts[0]);
|
|
79
76
|
const digest = hmac.digest("hex");
|
|
80
|
-
if (signedParts[1] !== digest)
|
|
81
|
-
throw Error("Session Not valid");
|
|
77
|
+
if (signedParts[1] !== digest) throw Error("Session Not valid");
|
|
82
78
|
const message = Buffer.from(signedParts[0], "base64").toString();
|
|
83
79
|
const parts = message.split("--").map((part2) => Buffer.from(part2, "base64"));
|
|
84
80
|
const cipher = createDecipheriv(
|
|
@@ -95,10 +91,8 @@ var Session = class {
|
|
|
95
91
|
return this.content[key];
|
|
96
92
|
}
|
|
97
93
|
write(key, value) {
|
|
98
|
-
if (value === null || typeof value === "undefined")
|
|
99
|
-
|
|
100
|
-
else
|
|
101
|
-
this.content[key] = value;
|
|
94
|
+
if (value === null || typeof value === "undefined") delete this.content[key];
|
|
95
|
+
else this.content[key] = value;
|
|
102
96
|
this.changed = true;
|
|
103
97
|
return this;
|
|
104
98
|
}
|
|
@@ -157,25 +151,19 @@ var Cookie = class {
|
|
|
157
151
|
cookie = `${key}=${encodeURIComponent(value)};`;
|
|
158
152
|
this.content[key] = value;
|
|
159
153
|
}
|
|
160
|
-
if (typeof opts.expires === "number")
|
|
161
|
-
cookie += `max-age=${opts.expires};`;
|
|
154
|
+
if (typeof opts.expires === "number") cookie += `max-age=${opts.expires};`;
|
|
162
155
|
else if (typeof opts.expires === "string")
|
|
163
156
|
cookie += `expires=${opts.expires};`;
|
|
164
157
|
cookie += `path=${opts.path || "/"};`;
|
|
165
|
-
if (opts.domain)
|
|
166
|
-
|
|
167
|
-
if (opts.
|
|
168
|
-
|
|
169
|
-
if (opts.httpOnly)
|
|
170
|
-
cookie += "HttpOnly;";
|
|
171
|
-
if (opts.sameSite)
|
|
172
|
-
cookie += `SameSite=${opts.sameSite};`;
|
|
158
|
+
if (opts.domain) cookie += `domain=${opts.domain};`;
|
|
159
|
+
if (opts.secure) cookie += "Secure;";
|
|
160
|
+
if (opts.httpOnly) cookie += "HttpOnly;";
|
|
161
|
+
if (opts.sameSite) cookie += `SameSite=${opts.sameSite};`;
|
|
173
162
|
this.setCookie[key] = cookie;
|
|
174
163
|
return this;
|
|
175
164
|
}
|
|
176
165
|
headers() {
|
|
177
|
-
if (Object.keys(this.setCookie).length === 0)
|
|
178
|
-
return {};
|
|
166
|
+
if (Object.keys(this.setCookie).length === 0) return {};
|
|
179
167
|
return { "Set-Cookie": Object.values(this.setCookie) };
|
|
180
168
|
}
|
|
181
169
|
};
|
|
@@ -191,8 +179,7 @@ var Validator = class {
|
|
|
191
179
|
async valid(request, logger) {
|
|
192
180
|
if (this.before) {
|
|
193
181
|
const result = await this.before(request);
|
|
194
|
-
if (result)
|
|
195
|
-
throw new HttpError(result);
|
|
182
|
+
if (result) throw new HttpError(result);
|
|
196
183
|
}
|
|
197
184
|
this.request = request;
|
|
198
185
|
if (this.paramsConfig && request.params) {
|
|
@@ -201,8 +188,7 @@ var Validator = class {
|
|
|
201
188
|
}
|
|
202
189
|
if (this.cookieConfig && request.cookie) {
|
|
203
190
|
logger.debug("Valid Cookie");
|
|
204
|
-
if (request.cookie == null)
|
|
205
|
-
throw Error("Not found Cookie");
|
|
191
|
+
if (request.cookie == null) throw Error("Not found Cookie");
|
|
206
192
|
this.validContent(
|
|
207
193
|
"cookie",
|
|
208
194
|
request.cookie.content,
|
|
@@ -213,8 +199,7 @@ var Validator = class {
|
|
|
213
199
|
}
|
|
214
200
|
if (this.sessionConfig && request.session) {
|
|
215
201
|
logger.debug("Valid Session");
|
|
216
|
-
if (request.session == null)
|
|
217
|
-
throw Error("Not found Session");
|
|
202
|
+
if (request.session == null) throw Error("Not found Session");
|
|
218
203
|
this.validContent(
|
|
219
204
|
"session",
|
|
220
205
|
request.session.content,
|
|
@@ -237,20 +222,17 @@ var Validator = class {
|
|
|
237
222
|
);
|
|
238
223
|
if (config.onError) {
|
|
239
224
|
const res = config.onError(`${type}.whitelist`, baseKey, diffKeys);
|
|
240
|
-
if (res)
|
|
241
|
-
throw new HttpError(res);
|
|
225
|
+
if (res) throw new HttpError(res);
|
|
242
226
|
}
|
|
243
227
|
throw error;
|
|
244
228
|
}
|
|
245
229
|
if (config.whitelist === "ignore")
|
|
246
|
-
for (const key of diff)
|
|
247
|
-
delete params[key];
|
|
230
|
+
for (const key of diff) delete params[key];
|
|
248
231
|
}
|
|
249
232
|
}
|
|
250
233
|
for (const key in config.rules) {
|
|
251
234
|
const rule = config.rules[key];
|
|
252
|
-
if (!rule)
|
|
253
|
-
continue;
|
|
235
|
+
if (!rule) continue;
|
|
254
236
|
let value = params[key];
|
|
255
237
|
if (rule.default) {
|
|
256
238
|
if (type === "cookie" || type === "session")
|
|
@@ -269,16 +251,14 @@ var Validator = class {
|
|
|
269
251
|
`${baseKey}${key}`,
|
|
270
252
|
value
|
|
271
253
|
);
|
|
272
|
-
if (res)
|
|
273
|
-
throw new HttpError(res);
|
|
254
|
+
if (res) throw new HttpError(res);
|
|
274
255
|
}
|
|
275
256
|
throw error;
|
|
276
257
|
}
|
|
277
258
|
}
|
|
278
259
|
if (typeof value !== "undefined" && value !== null) {
|
|
279
260
|
if (rule.type)
|
|
280
|
-
if (type === "cookie")
|
|
281
|
-
logger.warn("Cookie not support type rule");
|
|
261
|
+
if (type === "cookie") logger.warn("Cookie not support type rule");
|
|
282
262
|
else {
|
|
283
263
|
let typed = true;
|
|
284
264
|
switch (rule.type) {
|
|
@@ -302,8 +282,7 @@ var Validator = class {
|
|
|
302
282
|
`${baseKey}${key}`,
|
|
303
283
|
value
|
|
304
284
|
);
|
|
305
|
-
if (res)
|
|
306
|
-
throw new HttpError(res);
|
|
285
|
+
if (res) throw new HttpError(res);
|
|
307
286
|
}
|
|
308
287
|
throw error;
|
|
309
288
|
}
|
|
@@ -318,8 +297,7 @@ var Validator = class {
|
|
|
318
297
|
`${baseKey}${key}`,
|
|
319
298
|
value
|
|
320
299
|
);
|
|
321
|
-
if (res)
|
|
322
|
-
throw new HttpError(res);
|
|
300
|
+
if (res) throw new HttpError(res);
|
|
323
301
|
}
|
|
324
302
|
throw error;
|
|
325
303
|
}
|
|
@@ -333,14 +311,12 @@ var Validator = class {
|
|
|
333
311
|
`${baseKey}${key}`,
|
|
334
312
|
value
|
|
335
313
|
);
|
|
336
|
-
if (res)
|
|
337
|
-
throw new HttpError(res);
|
|
314
|
+
if (res) throw new HttpError(res);
|
|
338
315
|
}
|
|
339
316
|
throw error;
|
|
340
317
|
}
|
|
341
318
|
if (rule.config) {
|
|
342
|
-
if (type === "cookie")
|
|
343
|
-
logger.warn("Cookie not support nest rule.");
|
|
319
|
+
if (type === "cookie") logger.warn("Cookie not support nest rule.");
|
|
344
320
|
else if (Array.isArray(value))
|
|
345
321
|
for (const val of value)
|
|
346
322
|
this.validContent(
|
|
@@ -379,22 +355,18 @@ var HttpError = class _HttpError extends Error {
|
|
|
379
355
|
message
|
|
380
356
|
}) {
|
|
381
357
|
super(message);
|
|
382
|
-
if (Error.captureStackTrace)
|
|
383
|
-
Error.captureStackTrace(this, _HttpError);
|
|
358
|
+
if (Error.captureStackTrace) Error.captureStackTrace(this, _HttpError);
|
|
384
359
|
this.statusCode = statusCode || 500;
|
|
385
360
|
this.message = message;
|
|
386
361
|
}
|
|
387
362
|
};
|
|
388
363
|
var Name = "http";
|
|
389
364
|
function deepClone(obj) {
|
|
390
|
-
if (obj === null || typeof obj !== "object")
|
|
391
|
-
|
|
392
|
-
if (Array.isArray(obj))
|
|
393
|
-
return JSON.parse(JSON.stringify(obj));
|
|
365
|
+
if (obj === null || typeof obj !== "object") return obj;
|
|
366
|
+
if (Array.isArray(obj)) return JSON.parse(JSON.stringify(obj));
|
|
394
367
|
const clone = {};
|
|
395
368
|
for (const key in obj) {
|
|
396
|
-
if (!obj.hasOwnProperty(key))
|
|
397
|
-
continue;
|
|
369
|
+
if (!obj.hasOwnProperty(key)) continue;
|
|
398
370
|
if (typeof obj[key] === "function") {
|
|
399
371
|
clone[key] = obj[key];
|
|
400
372
|
continue;
|
|
@@ -409,8 +381,7 @@ var Http = class {
|
|
|
409
381
|
this.name = Name;
|
|
410
382
|
this.name = (config == null ? void 0 : config.name) || this.type;
|
|
411
383
|
this.config = (config == null ? void 0 : config.config) || /* @__PURE__ */ Object.create(null);
|
|
412
|
-
if (config == null ? void 0 : config.validator)
|
|
413
|
-
this.validatorOptions = config.validator;
|
|
384
|
+
if (config == null ? void 0 : config.validator) this.validatorOptions = config.validator;
|
|
414
385
|
}
|
|
415
386
|
async onDeploy(data, next) {
|
|
416
387
|
var _a;
|
|
@@ -424,15 +395,13 @@ var Http = class {
|
|
|
424
395
|
}) : { config: this.config };
|
|
425
396
|
if (!config.config.path) {
|
|
426
397
|
config.config.path = `/${(_a = data.name) == null ? void 0 : _a.replace(/_/g, "/").replace(/\/index$/, "")}`;
|
|
427
|
-
if (config.config.path === "/index")
|
|
428
|
-
config.config.path = "/";
|
|
398
|
+
if (config.config.path === "/index") config.config.path = "/";
|
|
429
399
|
if (config.config.ignorePathPrefix) {
|
|
430
400
|
config.config.path = config.config.path.replace(
|
|
431
401
|
new RegExp(`^${config.config.ignorePathPrefix}`),
|
|
432
402
|
""
|
|
433
403
|
);
|
|
434
|
-
if (config.config.path === "")
|
|
435
|
-
config.config.path = "/";
|
|
404
|
+
if (config.config.path === "") config.config.path = "/";
|
|
436
405
|
}
|
|
437
406
|
}
|
|
438
407
|
logger.debug("Api gateway's config: %j", config);
|
|
@@ -455,16 +424,13 @@ var Http = class {
|
|
|
455
424
|
let config = this.config;
|
|
456
425
|
const keys = key.split("_");
|
|
457
426
|
for (const k of keys.slice(0, keys.length - 1)) {
|
|
458
|
-
if (!config[k])
|
|
459
|
-
config[k] = /* @__PURE__ */ Object.create(null);
|
|
427
|
+
if (!config[k]) config[k] = /* @__PURE__ */ Object.create(null);
|
|
460
428
|
config = config[k];
|
|
461
429
|
}
|
|
462
430
|
config[keys[keys.length - 1]] = value;
|
|
463
|
-
} else
|
|
464
|
-
this.config[key] = value;
|
|
431
|
+
} else this.config[key] = value;
|
|
465
432
|
}
|
|
466
|
-
if (!data.config)
|
|
467
|
-
throw Error(`[${this.name}] Config not found.`);
|
|
433
|
+
if (!data.config) throw Error(`[${this.name}] Config not found.`);
|
|
468
434
|
if ((_b = data.config.plugins) == null ? void 0 : _b[this.name || this.type])
|
|
469
435
|
this.config = deepMerge(
|
|
470
436
|
this.config,
|
|
@@ -551,8 +517,7 @@ var Http = class {
|
|
|
551
517
|
}
|
|
552
518
|
} else if (Object.prototype.toString.call(data.response) === "[object Object]" && data.response.statusCode && data.response.headers)
|
|
553
519
|
this.response = data.response;
|
|
554
|
-
else
|
|
555
|
-
this.response.body = JSON.stringify({ data: data.response });
|
|
520
|
+
else this.response.body = JSON.stringify({ data: data.response });
|
|
556
521
|
if (!this.response.statusCode)
|
|
557
522
|
this.response.statusCode = this.response.body ? 200 : 201;
|
|
558
523
|
this.response.headers = Object.assign(
|
|
@@ -572,8 +537,7 @@ var Http = class {
|
|
|
572
537
|
if (!data.response.body || data.response.isBase64Encoded || typeof data.response.body !== "string" || data.response.body.length < 1024)
|
|
573
538
|
return;
|
|
574
539
|
const acceptEncoding = this.headers["accept-encoding"] || this.headers["Accept-Encoding"];
|
|
575
|
-
if (!acceptEncoding || !/(br|gzip|deflate)/.test(acceptEncoding))
|
|
576
|
-
return;
|
|
540
|
+
if (!acceptEncoding || !/(br|gzip|deflate)/.test(acceptEncoding)) return;
|
|
577
541
|
try {
|
|
578
542
|
if (acceptEncoding.includes("br")) {
|
|
579
543
|
data.response.headers["Content-Encoding"] = "br";
|
|
@@ -584,8 +548,7 @@ var Http = class {
|
|
|
584
548
|
} else if (acceptEncoding.includes("deflate")) {
|
|
585
549
|
data.response.headers["Content-Encoding"] = "deflate";
|
|
586
550
|
data.response.body = deflateSync(originBody).toString("base64");
|
|
587
|
-
} else
|
|
588
|
-
throw Error("No matched compression.");
|
|
551
|
+
} else throw Error("No matched compression.");
|
|
589
552
|
data.response.isBase64Encoded = true;
|
|
590
553
|
} catch (error) {
|
|
591
554
|
console.error(error);
|
|
@@ -610,8 +573,7 @@ var Http = class {
|
|
|
610
573
|
setContentType(type, charset = "utf-8") {
|
|
611
574
|
if (ContentType[type])
|
|
612
575
|
this.setHeader("Content-Type", `${ContentType[type]}; charset=${charset}`);
|
|
613
|
-
else
|
|
614
|
-
this.setHeader("Content-Type", `${type}; charset=${charset}`);
|
|
576
|
+
else this.setHeader("Content-Type", `${type}; charset=${charset}`);
|
|
615
577
|
return this;
|
|
616
578
|
}
|
|
617
579
|
/**
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@faasjs/http",
|
|
3
|
-
"version": "2.
|
|
3
|
+
"version": "2.5.0",
|
|
4
4
|
"license": "MIT",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"types": "dist/index.d.ts",
|
|
@@ -22,12 +22,12 @@
|
|
|
22
22
|
"dist"
|
|
23
23
|
],
|
|
24
24
|
"peerDependencies": {
|
|
25
|
-
"@faasjs/func": "2.
|
|
26
|
-
"@faasjs/logger": "2.
|
|
25
|
+
"@faasjs/func": "2.5.0",
|
|
26
|
+
"@faasjs/logger": "2.5.0"
|
|
27
27
|
},
|
|
28
28
|
"devDependencies": {
|
|
29
|
-
"@faasjs/func": "2.
|
|
30
|
-
"@faasjs/logger": "2.
|
|
29
|
+
"@faasjs/func": "2.5.0",
|
|
30
|
+
"@faasjs/logger": "2.5.0"
|
|
31
31
|
},
|
|
32
32
|
"engines": {
|
|
33
33
|
"npm": ">=9.0.0",
|