@faasjs/http 3.1.0 → 3.1.2
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 +1 -1
- package/dist/index.d.ts +1 -1
- package/dist/index.js +19 -26
- package/dist/index.mjs +19 -26
- package/package.json +5 -5
package/dist/index.d.mts
CHANGED
|
@@ -187,7 +187,7 @@ declare class HttpError extends Error {
|
|
|
187
187
|
});
|
|
188
188
|
}
|
|
189
189
|
declare class Http<TParams extends Record<string, any> = any, TCookie extends Record<string, string> = any, TSession extends Record<string, string> = any> implements Plugin {
|
|
190
|
-
readonly type
|
|
190
|
+
readonly type = "http";
|
|
191
191
|
readonly name: string;
|
|
192
192
|
headers: {
|
|
193
193
|
[key: string]: string;
|
package/dist/index.d.ts
CHANGED
|
@@ -187,7 +187,7 @@ declare class HttpError extends Error {
|
|
|
187
187
|
});
|
|
188
188
|
}
|
|
189
189
|
declare class Http<TParams extends Record<string, any> = any, TCookie extends Record<string, string> = any, TSession extends Record<string, string> = any> implements Plugin {
|
|
190
|
-
readonly type
|
|
190
|
+
readonly type = "http";
|
|
191
191
|
readonly name: string;
|
|
192
192
|
headers: {
|
|
193
193
|
[key: string]: string;
|
package/dist/index.js
CHANGED
|
@@ -2,7 +2,6 @@
|
|
|
2
2
|
|
|
3
3
|
var func = require('@faasjs/func');
|
|
4
4
|
var deep_merge = require('@faasjs/deep_merge');
|
|
5
|
-
var logger = require('@faasjs/logger');
|
|
6
5
|
var crypto = require('crypto');
|
|
7
6
|
var zlib = require('zlib');
|
|
8
7
|
|
|
@@ -375,7 +374,7 @@ var HttpError = class _HttpError extends Error {
|
|
|
375
374
|
this.message = message;
|
|
376
375
|
}
|
|
377
376
|
};
|
|
378
|
-
var Name = "
|
|
377
|
+
var Name = "Http";
|
|
379
378
|
function deepClone(obj) {
|
|
380
379
|
if (obj === null || typeof obj !== "object") return obj;
|
|
381
380
|
if (Array.isArray(obj)) return JSON.parse(JSON.stringify(obj));
|
|
@@ -391,7 +390,7 @@ function deepClone(obj) {
|
|
|
391
390
|
return clone;
|
|
392
391
|
}
|
|
393
392
|
var Http = class {
|
|
394
|
-
type =
|
|
393
|
+
type = "http";
|
|
395
394
|
name = Name;
|
|
396
395
|
headers;
|
|
397
396
|
body;
|
|
@@ -411,10 +410,7 @@ var Http = class {
|
|
|
411
410
|
}
|
|
412
411
|
}
|
|
413
412
|
async onMount(data, next) {
|
|
414
|
-
|
|
415
|
-
if (!logger$1.label.endsWith(this.name))
|
|
416
|
-
logger$1.label = `${logger$1.label}] [${this.name}`;
|
|
417
|
-
logger$1.debug("[onMount] merge config");
|
|
413
|
+
data.logger.debug("merge config");
|
|
418
414
|
const prefix = `SECRET_${this.name.toUpperCase()}_`;
|
|
419
415
|
for (let key in process.env)
|
|
420
416
|
if (key.startsWith(prefix)) {
|
|
@@ -436,11 +432,11 @@ var Http = class {
|
|
|
436
432
|
this.config,
|
|
437
433
|
data.config.plugins[this.name || this.type].config
|
|
438
434
|
);
|
|
439
|
-
logger
|
|
440
|
-
this.cookie = new Cookie(this.config.cookie || {}, logger
|
|
435
|
+
data.logger.debug("prepare cookie & session");
|
|
436
|
+
this.cookie = new Cookie(this.config.cookie || {}, data.logger);
|
|
441
437
|
this.session = this.cookie.session;
|
|
442
438
|
if (this.validatorOptions) {
|
|
443
|
-
logger
|
|
439
|
+
data.logger.debug("prepare validator");
|
|
444
440
|
this.validator = new Validator(
|
|
445
441
|
this.validatorOptions
|
|
446
442
|
);
|
|
@@ -448,45 +444,42 @@ var Http = class {
|
|
|
448
444
|
await next();
|
|
449
445
|
}
|
|
450
446
|
async onInvoke(data, next) {
|
|
451
|
-
const logger$1 = new logger.Logger(data.logger?.label || this.name);
|
|
452
|
-
if (!logger$1.label?.endsWith(this.name))
|
|
453
|
-
logger$1.label = `${logger$1.label}] [${this.name}`;
|
|
454
447
|
this.headers = data.event.headers || /* @__PURE__ */ Object.create(null);
|
|
455
448
|
this.body = data.event.body;
|
|
456
449
|
this.params = data.event.queryString || /* @__PURE__ */ Object.create(null);
|
|
457
450
|
this.response = { headers: /* @__PURE__ */ Object.create(null) };
|
|
458
451
|
if (data.event.body) {
|
|
459
452
|
if (this.headers["content-type"]?.includes("application/json") && typeof data.event.body === "string" && data.event.body.length > 1) {
|
|
460
|
-
logger
|
|
453
|
+
data.logger.debug("Parse params from json body");
|
|
461
454
|
try {
|
|
462
455
|
this.params = Object.keys(this.params).length ? Object.assign(this.params, JSON.parse(data.event.body)) : JSON.parse(data.event.body);
|
|
463
456
|
} catch (error) {
|
|
464
|
-
logger
|
|
465
|
-
"
|
|
457
|
+
data.logger.error(
|
|
458
|
+
"Parse params from json body failed: %s",
|
|
466
459
|
error.message
|
|
467
460
|
);
|
|
468
461
|
}
|
|
469
462
|
} else {
|
|
470
|
-
logger
|
|
463
|
+
data.logger.debug("Parse params from raw body");
|
|
471
464
|
this.params = data.event.body || /* @__PURE__ */ Object.create(null);
|
|
472
465
|
}
|
|
473
466
|
if (this.params && typeof this.params === "object" && this.params._)
|
|
474
467
|
delete this.params._;
|
|
475
468
|
data.event.params = deepClone(this.params);
|
|
476
|
-
logger
|
|
469
|
+
data.logger.debug("Params: %j", this.params);
|
|
477
470
|
}
|
|
478
|
-
this.cookie.invoke(this.headers.cookie, logger
|
|
471
|
+
this.cookie.invoke(this.headers.cookie, data.logger);
|
|
479
472
|
if (this.headers.cookie) {
|
|
480
|
-
logger
|
|
481
|
-
logger
|
|
482
|
-
"
|
|
473
|
+
data.logger.debug("Cookie: %j", this.cookie.content);
|
|
474
|
+
data.logger.debug(
|
|
475
|
+
"Session: %s %j",
|
|
483
476
|
this.session.config.key,
|
|
484
477
|
this.session.content
|
|
485
478
|
);
|
|
486
479
|
}
|
|
487
480
|
try {
|
|
488
481
|
if (this.validator) {
|
|
489
|
-
logger
|
|
482
|
+
data.logger.debug("Valid request");
|
|
490
483
|
await this.validator.valid(
|
|
491
484
|
{
|
|
492
485
|
headers: this.headers,
|
|
@@ -494,7 +487,7 @@ var Http = class {
|
|
|
494
487
|
cookie: this.cookie,
|
|
495
488
|
session: this.session
|
|
496
489
|
},
|
|
497
|
-
logger
|
|
490
|
+
data.logger
|
|
498
491
|
);
|
|
499
492
|
}
|
|
500
493
|
await next();
|
|
@@ -504,7 +497,7 @@ var Http = class {
|
|
|
504
497
|
this.session.update();
|
|
505
498
|
if (data.response)
|
|
506
499
|
if (data.response instanceof Error || data.response.constructor?.name === "Error") {
|
|
507
|
-
logger
|
|
500
|
+
data.logger.error(data.response);
|
|
508
501
|
this.response.body = JSON.stringify({
|
|
509
502
|
error: { message: data.response.message }
|
|
510
503
|
});
|
|
@@ -523,7 +516,7 @@ var Http = class {
|
|
|
523
516
|
{
|
|
524
517
|
"content-type": "application/json; charset=utf-8",
|
|
525
518
|
"cache-control": "no-cache, no-store",
|
|
526
|
-
"x-faasjs-request-id": data.
|
|
519
|
+
"x-faasjs-request-id": data.context.request_id
|
|
527
520
|
},
|
|
528
521
|
this.cookie.headers(),
|
|
529
522
|
this.response.headers
|
package/dist/index.mjs
CHANGED
|
@@ -1,6 +1,5 @@
|
|
|
1
1
|
import { usePlugin } from '@faasjs/func';
|
|
2
2
|
import { deepMerge } from '@faasjs/deep_merge';
|
|
3
|
-
import { Logger } from '@faasjs/logger';
|
|
4
3
|
import { randomBytes, pbkdf2Sync, createCipheriv, createHmac, createDecipheriv } from 'node:crypto';
|
|
5
4
|
import { brotliCompressSync, gzipSync, deflateSync } from 'node:zlib';
|
|
6
5
|
|
|
@@ -373,7 +372,7 @@ var HttpError = class _HttpError extends Error {
|
|
|
373
372
|
this.message = message;
|
|
374
373
|
}
|
|
375
374
|
};
|
|
376
|
-
var Name = "
|
|
375
|
+
var Name = "Http";
|
|
377
376
|
function deepClone(obj) {
|
|
378
377
|
if (obj === null || typeof obj !== "object") return obj;
|
|
379
378
|
if (Array.isArray(obj)) return JSON.parse(JSON.stringify(obj));
|
|
@@ -389,7 +388,7 @@ function deepClone(obj) {
|
|
|
389
388
|
return clone;
|
|
390
389
|
}
|
|
391
390
|
var Http = class {
|
|
392
|
-
type =
|
|
391
|
+
type = "http";
|
|
393
392
|
name = Name;
|
|
394
393
|
headers;
|
|
395
394
|
body;
|
|
@@ -409,10 +408,7 @@ var Http = class {
|
|
|
409
408
|
}
|
|
410
409
|
}
|
|
411
410
|
async onMount(data, next) {
|
|
412
|
-
|
|
413
|
-
if (!logger.label.endsWith(this.name))
|
|
414
|
-
logger.label = `${logger.label}] [${this.name}`;
|
|
415
|
-
logger.debug("[onMount] merge config");
|
|
411
|
+
data.logger.debug("merge config");
|
|
416
412
|
const prefix = `SECRET_${this.name.toUpperCase()}_`;
|
|
417
413
|
for (let key in process.env)
|
|
418
414
|
if (key.startsWith(prefix)) {
|
|
@@ -434,11 +430,11 @@ var Http = class {
|
|
|
434
430
|
this.config,
|
|
435
431
|
data.config.plugins[this.name || this.type].config
|
|
436
432
|
);
|
|
437
|
-
logger.debug("
|
|
438
|
-
this.cookie = new Cookie(this.config.cookie || {}, logger);
|
|
433
|
+
data.logger.debug("prepare cookie & session");
|
|
434
|
+
this.cookie = new Cookie(this.config.cookie || {}, data.logger);
|
|
439
435
|
this.session = this.cookie.session;
|
|
440
436
|
if (this.validatorOptions) {
|
|
441
|
-
logger.debug("
|
|
437
|
+
data.logger.debug("prepare validator");
|
|
442
438
|
this.validator = new Validator(
|
|
443
439
|
this.validatorOptions
|
|
444
440
|
);
|
|
@@ -446,45 +442,42 @@ var Http = class {
|
|
|
446
442
|
await next();
|
|
447
443
|
}
|
|
448
444
|
async onInvoke(data, next) {
|
|
449
|
-
const logger = new Logger(data.logger?.label || this.name);
|
|
450
|
-
if (!logger.label?.endsWith(this.name))
|
|
451
|
-
logger.label = `${logger.label}] [${this.name}`;
|
|
452
445
|
this.headers = data.event.headers || /* @__PURE__ */ Object.create(null);
|
|
453
446
|
this.body = data.event.body;
|
|
454
447
|
this.params = data.event.queryString || /* @__PURE__ */ Object.create(null);
|
|
455
448
|
this.response = { headers: /* @__PURE__ */ Object.create(null) };
|
|
456
449
|
if (data.event.body) {
|
|
457
450
|
if (this.headers["content-type"]?.includes("application/json") && typeof data.event.body === "string" && data.event.body.length > 1) {
|
|
458
|
-
logger.debug("
|
|
451
|
+
data.logger.debug("Parse params from json body");
|
|
459
452
|
try {
|
|
460
453
|
this.params = Object.keys(this.params).length ? Object.assign(this.params, JSON.parse(data.event.body)) : JSON.parse(data.event.body);
|
|
461
454
|
} catch (error) {
|
|
462
|
-
logger.error(
|
|
463
|
-
"
|
|
455
|
+
data.logger.error(
|
|
456
|
+
"Parse params from json body failed: %s",
|
|
464
457
|
error.message
|
|
465
458
|
);
|
|
466
459
|
}
|
|
467
460
|
} else {
|
|
468
|
-
logger.debug("
|
|
461
|
+
data.logger.debug("Parse params from raw body");
|
|
469
462
|
this.params = data.event.body || /* @__PURE__ */ Object.create(null);
|
|
470
463
|
}
|
|
471
464
|
if (this.params && typeof this.params === "object" && this.params._)
|
|
472
465
|
delete this.params._;
|
|
473
466
|
data.event.params = deepClone(this.params);
|
|
474
|
-
logger.debug("
|
|
467
|
+
data.logger.debug("Params: %j", this.params);
|
|
475
468
|
}
|
|
476
|
-
this.cookie.invoke(this.headers.cookie, logger);
|
|
469
|
+
this.cookie.invoke(this.headers.cookie, data.logger);
|
|
477
470
|
if (this.headers.cookie) {
|
|
478
|
-
logger.debug("
|
|
479
|
-
logger.debug(
|
|
480
|
-
"
|
|
471
|
+
data.logger.debug("Cookie: %j", this.cookie.content);
|
|
472
|
+
data.logger.debug(
|
|
473
|
+
"Session: %s %j",
|
|
481
474
|
this.session.config.key,
|
|
482
475
|
this.session.content
|
|
483
476
|
);
|
|
484
477
|
}
|
|
485
478
|
try {
|
|
486
479
|
if (this.validator) {
|
|
487
|
-
logger.debug("
|
|
480
|
+
data.logger.debug("Valid request");
|
|
488
481
|
await this.validator.valid(
|
|
489
482
|
{
|
|
490
483
|
headers: this.headers,
|
|
@@ -492,7 +485,7 @@ var Http = class {
|
|
|
492
485
|
cookie: this.cookie,
|
|
493
486
|
session: this.session
|
|
494
487
|
},
|
|
495
|
-
logger
|
|
488
|
+
data.logger
|
|
496
489
|
);
|
|
497
490
|
}
|
|
498
491
|
await next();
|
|
@@ -502,7 +495,7 @@ var Http = class {
|
|
|
502
495
|
this.session.update();
|
|
503
496
|
if (data.response)
|
|
504
497
|
if (data.response instanceof Error || data.response.constructor?.name === "Error") {
|
|
505
|
-
logger.error(data.response);
|
|
498
|
+
data.logger.error(data.response);
|
|
506
499
|
this.response.body = JSON.stringify({
|
|
507
500
|
error: { message: data.response.message }
|
|
508
501
|
});
|
|
@@ -521,7 +514,7 @@ var Http = class {
|
|
|
521
514
|
{
|
|
522
515
|
"content-type": "application/json; charset=utf-8",
|
|
523
516
|
"cache-control": "no-cache, no-store",
|
|
524
|
-
"x-faasjs-request-id": data.
|
|
517
|
+
"x-faasjs-request-id": data.context.request_id
|
|
525
518
|
},
|
|
526
519
|
this.cookie.headers(),
|
|
527
520
|
this.response.headers
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@faasjs/http",
|
|
3
|
-
"version": "3.1.
|
|
3
|
+
"version": "3.1.2",
|
|
4
4
|
"license": "MIT",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"module": "dist/index.mjs",
|
|
@@ -34,12 +34,12 @@
|
|
|
34
34
|
"dist"
|
|
35
35
|
],
|
|
36
36
|
"peerDependencies": {
|
|
37
|
-
"@faasjs/func": "3.1.
|
|
38
|
-
"@faasjs/logger": "3.1.
|
|
37
|
+
"@faasjs/func": "3.1.2",
|
|
38
|
+
"@faasjs/logger": "3.1.2"
|
|
39
39
|
},
|
|
40
40
|
"devDependencies": {
|
|
41
|
-
"@faasjs/func": "3.1.
|
|
42
|
-
"@faasjs/logger": "3.1.
|
|
41
|
+
"@faasjs/func": "3.1.2",
|
|
42
|
+
"@faasjs/logger": "3.1.2"
|
|
43
43
|
},
|
|
44
44
|
"engines": {
|
|
45
45
|
"node": ">=22.0.0",
|