@faasjs/http 0.0.3-beta.99 → 0.0.4-beta.10

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 CHANGED
@@ -159,7 +159,7 @@ type Response = {
159
159
  declare class HttpError extends Error {
160
160
  readonly statusCode: number;
161
161
  readonly message: string;
162
- constructor({ statusCode, message }: {
162
+ constructor({ statusCode, message, }: {
163
163
  statusCode?: number;
164
164
  message: string;
165
165
  });
package/dist/index.d.ts CHANGED
@@ -159,7 +159,7 @@ type Response = {
159
159
  declare class HttpError extends Error {
160
160
  readonly statusCode: number;
161
161
  readonly message: string;
162
- constructor({ statusCode, message }: {
162
+ constructor({ statusCode, message, }: {
163
163
  statusCode?: number;
164
164
  message: string;
165
165
  });
package/dist/index.js CHANGED
@@ -18,16 +18,19 @@ var Session = class {
18
18
  this.cookie = cookie;
19
19
  if (!(config == null ? void 0 : config.secret))
20
20
  cookie.logger.warn("Session's secret is missing.");
21
- this.config = Object.assign({
22
- key: "key",
23
- secret: crypto.randomBytes(128).toString("hex"),
24
- salt: "salt",
25
- signedSalt: "signedSalt",
26
- keylen: 64,
27
- iterations: 100,
28
- digest: "sha256",
29
- cipherName: "aes-256-cbc"
30
- }, config);
21
+ this.config = Object.assign(
22
+ {
23
+ key: "key",
24
+ secret: crypto.randomBytes(128).toString("hex"),
25
+ salt: "salt",
26
+ signedSalt: "signedSalt",
27
+ keylen: 64,
28
+ iterations: 100,
29
+ digest: "sha256",
30
+ cipherName: "aes-256-cbc"
31
+ },
32
+ config
33
+ );
31
34
  this.secret = crypto.pbkdf2Sync(
32
35
  this.config.secret,
33
36
  this.config.salt,
@@ -58,12 +61,17 @@ var Session = class {
58
61
  text = JSON.stringify(text);
59
62
  const iv = crypto.randomBytes(16);
60
63
  const cipher = crypto.createCipheriv(this.config.cipherName, this.secret, iv);
61
- const encrypted = Buffer.concat([cipher.update(text), cipher.final()]).toString("base64");
62
- const main = Buffer.from([encrypted, iv.toString("base64")].join("--")).toString("base64");
64
+ const encrypted = Buffer.concat([
65
+ cipher.update(text),
66
+ cipher.final()
67
+ ]).toString("base64");
68
+ const main = Buffer.from(
69
+ [encrypted, iv.toString("base64")].join("--")
70
+ ).toString("base64");
63
71
  const hmac = crypto.createHmac(this.config.digest, this.signedSecret);
64
72
  hmac.update(main);
65
73
  const digest = hmac.digest("hex");
66
- return main + "--" + digest;
74
+ return `${main}--${digest}`;
67
75
  }
68
76
  decode(text) {
69
77
  text = decodeURIComponent(text);
@@ -77,7 +85,11 @@ var Session = class {
77
85
  const parts = message.split("--").map(function(part2) {
78
86
  return Buffer.from(part2, "base64");
79
87
  });
80
- const cipher = crypto.createDecipheriv(this.config.cipherName, this.secret, parts[1]);
88
+ const cipher = crypto.createDecipheriv(
89
+ this.config.cipherName,
90
+ this.secret,
91
+ parts[1]
92
+ );
81
93
  const part = Buffer.from(cipher.update(parts[0])).toString("utf8");
82
94
  const final = cipher.final("utf8");
83
95
  const decrypt = [part, final].join("");
@@ -96,20 +108,26 @@ var Session = class {
96
108
  }
97
109
  update() {
98
110
  if (this.changed)
99
- this.cookie.write(this.config.key, this.encode(JSON.stringify(this.content)));
111
+ this.cookie.write(
112
+ this.config.key,
113
+ this.encode(JSON.stringify(this.content))
114
+ );
100
115
  return this;
101
116
  }
102
117
  };
103
118
  var Cookie = class {
104
119
  constructor(config, logger) {
105
120
  this.logger = logger;
106
- this.config = deep_merge.deepMerge({
107
- path: "/",
108
- expires: 31536e3,
109
- secure: true,
110
- httpOnly: true,
111
- session: {}
112
- }, config);
121
+ this.config = deep_merge.deepMerge(
122
+ {
123
+ path: "/",
124
+ expires: 31536e3,
125
+ secure: true,
126
+ httpOnly: true,
127
+ session: {}
128
+ },
129
+ config
130
+ );
113
131
  this.session = new Session(this, this.config.session);
114
132
  this.content = /* @__PURE__ */ Object.create(null);
115
133
  this.setCookie = /* @__PURE__ */ Object.create(null);
@@ -117,12 +135,14 @@ var Cookie = class {
117
135
  invoke(cookie, logger) {
118
136
  this.content = /* @__PURE__ */ Object.create(null);
119
137
  if (cookie)
120
- cookie.split(";").forEach((x) => {
121
- x = x.trim();
122
- const k = /([^=]+)/.exec(x);
138
+ for (const x of cookie.split(";")) {
139
+ const trimX = x.trim();
140
+ const k = /([^=]+)/.exec(trimX);
123
141
  if (k !== null)
124
- this.content[k[0]] = decodeURIComponent(x.replace(`${k[0]}=`, "").replace(/;$/, ""));
125
- });
142
+ this.content[k[0]] = decodeURIComponent(
143
+ trimX.replace(`${k[0]}=`, "").replace(/;$/, "")
144
+ );
145
+ }
126
146
  this.setCookie = /* @__PURE__ */ Object.create(null);
127
147
  this.session.invoke(this.read(this.session.config.key), logger);
128
148
  return this;
@@ -370,6 +390,23 @@ var HttpError = class _HttpError extends Error {
370
390
  }
371
391
  };
372
392
  var Name = "http";
393
+ function deepClone(obj) {
394
+ if (obj === null || typeof obj !== "object")
395
+ return obj;
396
+ if (Array.isArray(obj))
397
+ return JSON.parse(JSON.stringify(obj));
398
+ const clone = {};
399
+ for (const key in obj) {
400
+ if (!obj.hasOwnProperty(key))
401
+ continue;
402
+ if (typeof obj[key] === "function") {
403
+ clone[key] = obj[key];
404
+ continue;
405
+ }
406
+ clone[key] = deepClone(obj[key]);
407
+ }
408
+ return clone;
409
+ }
373
410
  var Http = class {
374
411
  constructor(config) {
375
412
  this.type = Name;
@@ -386,13 +423,18 @@ var Http = class {
386
423
  const logger$1 = new logger.Logger(this.name);
387
424
  logger$1.debug("Generate api gateway's config");
388
425
  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 };
426
+ const config = data.config.plugins ? deep_merge.deepMerge(data.config.plugins[this.name || this.type], {
427
+ config: this.config
428
+ }) : { config: this.config };
390
429
  if (!config.config.path) {
391
- config.config.path = "/" + ((_a = data.name) == null ? void 0 : _a.replace(/_/g, "/").replace(/\/index$/, ""));
430
+ config.config.path = `/${(_a = data.name) == null ? void 0 : _a.replace(/_/g, "/").replace(/\/index$/, "")}`;
392
431
  if (config.config.path === "/index")
393
432
  config.config.path = "/";
394
433
  if (config.config.ignorePathPrefix) {
395
- config.config.path = config.config.path.replace(new RegExp("^" + config.config.ignorePathPrefix), "");
434
+ config.config.path = config.config.path.replace(
435
+ new RegExp(`^${config.config.ignorePathPrefix}`),
436
+ ""
437
+ );
396
438
  if (config.config.path === "")
397
439
  config.config.path = "/";
398
440
  }
@@ -403,7 +445,11 @@ var Http = class {
403
445
  await provider.deploy(this.type, data, config);
404
446
  }
405
447
  async onMount(data, next) {
406
- data.logger.debug("[onMount] merge config");
448
+ var _a, _b;
449
+ const logger$1 = new logger.Logger(((_a = data.logger) == null ? void 0 : _a.label) || this.name);
450
+ if (!logger$1.label.endsWith(this.name))
451
+ logger$1.label = `${logger$1.label}] [${this.name}`;
452
+ logger$1.debug("[onMount] merge config");
407
453
  const prefix = `SECRET_${this.name.toUpperCase()}_`;
408
454
  for (let key in process.env)
409
455
  if (key.startsWith(prefix)) {
@@ -412,63 +458,83 @@ var Http = class {
412
458
  if (key.includes("_")) {
413
459
  let config = this.config;
414
460
  const keys = key.split("_");
415
- keys.slice(0, keys.length - 1).forEach((k) => {
461
+ for (const k of keys.slice(0, keys.length - 1)) {
416
462
  if (!config[k])
417
463
  config[k] = /* @__PURE__ */ Object.create(null);
418
464
  config = config[k];
419
- });
465
+ }
420
466
  config[keys[keys.length - 1]] = value;
421
467
  } else
422
468
  this.config[key] = value;
423
469
  }
424
- if (data.config.plugins && data.config.plugins[this.name || this.type])
425
- this.config = deep_merge.deepMerge(this.config, data.config.plugins[this.name || this.type].config);
426
- data.logger.debug("[onMount] prepare cookie & session");
427
- this.cookie = new Cookie(this.config.cookie || {}, data.logger);
470
+ if (!data.config)
471
+ throw Error(`[${this.name}] Config not found.`);
472
+ if ((_b = data.config.plugins) == null ? void 0 : _b[this.name || this.type])
473
+ this.config = deep_merge.deepMerge(
474
+ this.config,
475
+ data.config.plugins[this.name || this.type].config
476
+ );
477
+ logger$1.debug("[onMount] prepare cookie & session");
478
+ this.cookie = new Cookie(this.config.cookie || {}, logger$1);
428
479
  this.session = this.cookie.session;
429
480
  if (this.validatorOptions) {
430
- data.logger.debug("[onMount] prepare validator");
431
- this.validator = new Validator(this.validatorOptions);
481
+ logger$1.debug("[onMount] prepare validator");
482
+ this.validator = new Validator(
483
+ this.validatorOptions
484
+ );
432
485
  }
433
486
  await next();
434
487
  }
435
488
  async onInvoke(data, next) {
436
- var _a, _b;
489
+ var _a, _b, _c, _d;
490
+ const logger$1 = new logger.Logger(((_a = data.logger) == null ? void 0 : _a.label) || this.name);
491
+ if (!((_b = logger$1.label) == null ? void 0 : _b.endsWith(this.name)))
492
+ logger$1.label = `${logger$1.label}] [${this.name}`;
437
493
  this.headers = data.event.headers || /* @__PURE__ */ Object.create(null);
438
494
  this.body = data.event.body;
439
495
  this.params = data.event.queryString || /* @__PURE__ */ Object.create(null);
440
496
  this.response = { headers: /* @__PURE__ */ Object.create(null) };
441
497
  if (data.event.body) {
442
- if (((_a = this.headers["content-type"]) == null ? void 0 : _a.includes("application/json")) && typeof data.event.body === "string" && data.event.body.length > 1) {
443
- data.logger.debug("[onInvoke] Parse params from json body");
498
+ if (((_c = this.headers["content-type"]) == null ? void 0 : _c.includes("application/json")) && typeof data.event.body === "string" && data.event.body.length > 1) {
499
+ logger$1.debug("[onInvoke] Parse params from json body");
444
500
  try {
445
501
  this.params = Object.keys(this.params).length ? Object.assign(this.params, JSON.parse(data.event.body)) : JSON.parse(data.event.body);
446
502
  } catch (error) {
447
- data.logger.error("[onInvoke] Parse params from json body failed: %s", error.message);
503
+ logger$1.error(
504
+ "[onInvoke] Parse params from json body failed: %s",
505
+ error.message
506
+ );
448
507
  }
449
508
  } else {
450
- data.logger.debug("[onInvoke] Parse params from raw body");
509
+ logger$1.debug("[onInvoke] Parse params from raw body");
451
510
  this.params = data.event.body || /* @__PURE__ */ Object.create(null);
452
511
  }
453
- if (this.params && typeof this.params === "object" && this.params["_"])
454
- delete this.params["_"];
455
- data.event.params = JSON.parse(JSON.stringify(this.params));
456
- data.logger.debug("[onInvoke] Params: %j", this.params);
512
+ if (this.params && typeof this.params === "object" && this.params._)
513
+ delete this.params._;
514
+ data.event.params = deepClone(this.params);
515
+ logger$1.debug("[onInvoke] Params: %j", this.params);
457
516
  }
458
- this.cookie.invoke(this.headers.cookie, data.logger);
517
+ this.cookie.invoke(this.headers.cookie, logger$1);
459
518
  if (this.headers.cookie) {
460
- data.logger.debug("[onInvoke] Cookie: %j", this.cookie.content);
461
- data.logger.debug("[onInvoke] Session: %j", this.session.content);
519
+ logger$1.debug("[onInvoke] Cookie: %j", this.cookie.content);
520
+ logger$1.debug(
521
+ "[onInvoke] Session: %s %j",
522
+ this.session.config.key,
523
+ this.session.content
524
+ );
462
525
  }
463
526
  try {
464
527
  if (this.validator) {
465
- data.logger.debug("[onInvoke] Valid request");
466
- await this.validator.valid({
467
- headers: this.headers,
468
- params: this.params,
469
- cookie: this.cookie,
470
- session: this.session
471
- }, data.logger);
528
+ logger$1.debug("[onInvoke] Valid request");
529
+ await this.validator.valid(
530
+ {
531
+ headers: this.headers,
532
+ params: this.params,
533
+ cookie: this.cookie,
534
+ session: this.session
535
+ },
536
+ logger$1
537
+ );
472
538
  }
473
539
  await next();
474
540
  } catch (error) {
@@ -476,9 +542,11 @@ var Http = class {
476
542
  }
477
543
  this.session.update();
478
544
  if (data.response)
479
- if (data.response instanceof Error || ((_b = data.response.constructor) == null ? void 0 : _b.name) === "Error") {
480
- data.logger.error(data.response);
481
- this.response.body = JSON.stringify({ error: { message: data.response.message } });
545
+ if (data.response instanceof Error || ((_d = data.response.constructor) == null ? void 0 : _d.name) === "Error") {
546
+ logger$1.error(data.response);
547
+ this.response.body = JSON.stringify({
548
+ error: { message: data.response.message }
549
+ });
482
550
  try {
483
551
  this.response.statusCode = data.response.statusCode || 500;
484
552
  } catch (error) {
@@ -490,11 +558,15 @@ var Http = class {
490
558
  this.response.body = JSON.stringify({ data: data.response });
491
559
  if (!this.response.statusCode)
492
560
  this.response.statusCode = this.response.body ? 200 : 201;
493
- this.response.headers = Object.assign({
494
- "Content-Type": "application/json; charset=utf-8",
495
- "Cache-Control": "no-cache, no-store",
496
- "X-FaasJS-Request-Id": data.logger.label
497
- }, this.cookie.headers(), this.response.headers);
561
+ this.response.headers = Object.assign(
562
+ {
563
+ "Content-Type": "application/json; charset=utf-8",
564
+ "Cache-Control": "no-cache, no-store",
565
+ "X-FaasJS-Request-Id": data.logger.label
566
+ },
567
+ this.cookie.headers(),
568
+ this.response.headers
569
+ );
498
570
  data.response = Object.assign({}, data.response, this.response);
499
571
  const originBody = data.response.body;
500
572
  data.response.originBody = originBody;
package/dist/index.mjs CHANGED
@@ -16,16 +16,19 @@ var Session = class {
16
16
  this.cookie = cookie;
17
17
  if (!(config == null ? void 0 : config.secret))
18
18
  cookie.logger.warn("Session's secret is missing.");
19
- this.config = Object.assign({
20
- key: "key",
21
- secret: randomBytes(128).toString("hex"),
22
- salt: "salt",
23
- signedSalt: "signedSalt",
24
- keylen: 64,
25
- iterations: 100,
26
- digest: "sha256",
27
- cipherName: "aes-256-cbc"
28
- }, config);
19
+ this.config = Object.assign(
20
+ {
21
+ key: "key",
22
+ secret: randomBytes(128).toString("hex"),
23
+ salt: "salt",
24
+ signedSalt: "signedSalt",
25
+ keylen: 64,
26
+ iterations: 100,
27
+ digest: "sha256",
28
+ cipherName: "aes-256-cbc"
29
+ },
30
+ config
31
+ );
29
32
  this.secret = pbkdf2Sync(
30
33
  this.config.secret,
31
34
  this.config.salt,
@@ -56,12 +59,17 @@ var Session = class {
56
59
  text = JSON.stringify(text);
57
60
  const iv = randomBytes(16);
58
61
  const cipher = createCipheriv(this.config.cipherName, this.secret, iv);
59
- const encrypted = Buffer.concat([cipher.update(text), cipher.final()]).toString("base64");
60
- const main = Buffer.from([encrypted, iv.toString("base64")].join("--")).toString("base64");
62
+ const encrypted = Buffer.concat([
63
+ cipher.update(text),
64
+ cipher.final()
65
+ ]).toString("base64");
66
+ const main = Buffer.from(
67
+ [encrypted, iv.toString("base64")].join("--")
68
+ ).toString("base64");
61
69
  const hmac = createHmac(this.config.digest, this.signedSecret);
62
70
  hmac.update(main);
63
71
  const digest = hmac.digest("hex");
64
- return main + "--" + digest;
72
+ return `${main}--${digest}`;
65
73
  }
66
74
  decode(text) {
67
75
  text = decodeURIComponent(text);
@@ -75,7 +83,11 @@ var Session = class {
75
83
  const parts = message.split("--").map(function(part2) {
76
84
  return Buffer.from(part2, "base64");
77
85
  });
78
- const cipher = createDecipheriv(this.config.cipherName, this.secret, parts[1]);
86
+ const cipher = createDecipheriv(
87
+ this.config.cipherName,
88
+ this.secret,
89
+ parts[1]
90
+ );
79
91
  const part = Buffer.from(cipher.update(parts[0])).toString("utf8");
80
92
  const final = cipher.final("utf8");
81
93
  const decrypt = [part, final].join("");
@@ -94,20 +106,26 @@ var Session = class {
94
106
  }
95
107
  update() {
96
108
  if (this.changed)
97
- this.cookie.write(this.config.key, this.encode(JSON.stringify(this.content)));
109
+ this.cookie.write(
110
+ this.config.key,
111
+ this.encode(JSON.stringify(this.content))
112
+ );
98
113
  return this;
99
114
  }
100
115
  };
101
116
  var Cookie = class {
102
117
  constructor(config, logger) {
103
118
  this.logger = logger;
104
- this.config = deepMerge({
105
- path: "/",
106
- expires: 31536e3,
107
- secure: true,
108
- httpOnly: true,
109
- session: {}
110
- }, config);
119
+ this.config = deepMerge(
120
+ {
121
+ path: "/",
122
+ expires: 31536e3,
123
+ secure: true,
124
+ httpOnly: true,
125
+ session: {}
126
+ },
127
+ config
128
+ );
111
129
  this.session = new Session(this, this.config.session);
112
130
  this.content = /* @__PURE__ */ Object.create(null);
113
131
  this.setCookie = /* @__PURE__ */ Object.create(null);
@@ -115,12 +133,14 @@ var Cookie = class {
115
133
  invoke(cookie, logger) {
116
134
  this.content = /* @__PURE__ */ Object.create(null);
117
135
  if (cookie)
118
- cookie.split(";").forEach((x) => {
119
- x = x.trim();
120
- const k = /([^=]+)/.exec(x);
136
+ for (const x of cookie.split(";")) {
137
+ const trimX = x.trim();
138
+ const k = /([^=]+)/.exec(trimX);
121
139
  if (k !== null)
122
- this.content[k[0]] = decodeURIComponent(x.replace(`${k[0]}=`, "").replace(/;$/, ""));
123
- });
140
+ this.content[k[0]] = decodeURIComponent(
141
+ trimX.replace(`${k[0]}=`, "").replace(/;$/, "")
142
+ );
143
+ }
124
144
  this.setCookie = /* @__PURE__ */ Object.create(null);
125
145
  this.session.invoke(this.read(this.session.config.key), logger);
126
146
  return this;
@@ -368,6 +388,23 @@ var HttpError = class _HttpError extends Error {
368
388
  }
369
389
  };
370
390
  var Name = "http";
391
+ function deepClone(obj) {
392
+ if (obj === null || typeof obj !== "object")
393
+ return obj;
394
+ if (Array.isArray(obj))
395
+ return JSON.parse(JSON.stringify(obj));
396
+ const clone = {};
397
+ for (const key in obj) {
398
+ if (!obj.hasOwnProperty(key))
399
+ continue;
400
+ if (typeof obj[key] === "function") {
401
+ clone[key] = obj[key];
402
+ continue;
403
+ }
404
+ clone[key] = deepClone(obj[key]);
405
+ }
406
+ return clone;
407
+ }
371
408
  var Http = class {
372
409
  constructor(config) {
373
410
  this.type = Name;
@@ -384,13 +421,18 @@ var Http = class {
384
421
  const logger = new Logger(this.name);
385
422
  logger.debug("Generate api gateway's config");
386
423
  logger.debug("%j", data);
387
- const config = data.config.plugins ? deepMerge(data.config.plugins[this.name || this.type], { config: this.config }) : { config: this.config };
424
+ const config = data.config.plugins ? deepMerge(data.config.plugins[this.name || this.type], {
425
+ config: this.config
426
+ }) : { config: this.config };
388
427
  if (!config.config.path) {
389
- config.config.path = "/" + ((_a = data.name) == null ? void 0 : _a.replace(/_/g, "/").replace(/\/index$/, ""));
428
+ config.config.path = `/${(_a = data.name) == null ? void 0 : _a.replace(/_/g, "/").replace(/\/index$/, "")}`;
390
429
  if (config.config.path === "/index")
391
430
  config.config.path = "/";
392
431
  if (config.config.ignorePathPrefix) {
393
- config.config.path = config.config.path.replace(new RegExp("^" + config.config.ignorePathPrefix), "");
432
+ config.config.path = config.config.path.replace(
433
+ new RegExp(`^${config.config.ignorePathPrefix}`),
434
+ ""
435
+ );
394
436
  if (config.config.path === "")
395
437
  config.config.path = "/";
396
438
  }
@@ -401,7 +443,11 @@ var Http = class {
401
443
  await provider.deploy(this.type, data, config);
402
444
  }
403
445
  async onMount(data, next) {
404
- data.logger.debug("[onMount] merge config");
446
+ var _a, _b;
447
+ const logger = new Logger(((_a = data.logger) == null ? void 0 : _a.label) || this.name);
448
+ if (!logger.label.endsWith(this.name))
449
+ logger.label = `${logger.label}] [${this.name}`;
450
+ logger.debug("[onMount] merge config");
405
451
  const prefix = `SECRET_${this.name.toUpperCase()}_`;
406
452
  for (let key in process.env)
407
453
  if (key.startsWith(prefix)) {
@@ -410,63 +456,83 @@ var Http = class {
410
456
  if (key.includes("_")) {
411
457
  let config = this.config;
412
458
  const keys = key.split("_");
413
- keys.slice(0, keys.length - 1).forEach((k) => {
459
+ for (const k of keys.slice(0, keys.length - 1)) {
414
460
  if (!config[k])
415
461
  config[k] = /* @__PURE__ */ Object.create(null);
416
462
  config = config[k];
417
- });
463
+ }
418
464
  config[keys[keys.length - 1]] = value;
419
465
  } else
420
466
  this.config[key] = value;
421
467
  }
422
- if (data.config.plugins && data.config.plugins[this.name || this.type])
423
- this.config = deepMerge(this.config, data.config.plugins[this.name || this.type].config);
424
- data.logger.debug("[onMount] prepare cookie & session");
425
- this.cookie = new Cookie(this.config.cookie || {}, data.logger);
468
+ if (!data.config)
469
+ throw Error(`[${this.name}] Config not found.`);
470
+ if ((_b = data.config.plugins) == null ? void 0 : _b[this.name || this.type])
471
+ this.config = deepMerge(
472
+ this.config,
473
+ data.config.plugins[this.name || this.type].config
474
+ );
475
+ logger.debug("[onMount] prepare cookie & session");
476
+ this.cookie = new Cookie(this.config.cookie || {}, logger);
426
477
  this.session = this.cookie.session;
427
478
  if (this.validatorOptions) {
428
- data.logger.debug("[onMount] prepare validator");
429
- this.validator = new Validator(this.validatorOptions);
479
+ logger.debug("[onMount] prepare validator");
480
+ this.validator = new Validator(
481
+ this.validatorOptions
482
+ );
430
483
  }
431
484
  await next();
432
485
  }
433
486
  async onInvoke(data, next) {
434
- var _a, _b;
487
+ var _a, _b, _c, _d;
488
+ const logger = new Logger(((_a = data.logger) == null ? void 0 : _a.label) || this.name);
489
+ if (!((_b = logger.label) == null ? void 0 : _b.endsWith(this.name)))
490
+ logger.label = `${logger.label}] [${this.name}`;
435
491
  this.headers = data.event.headers || /* @__PURE__ */ Object.create(null);
436
492
  this.body = data.event.body;
437
493
  this.params = data.event.queryString || /* @__PURE__ */ Object.create(null);
438
494
  this.response = { headers: /* @__PURE__ */ Object.create(null) };
439
495
  if (data.event.body) {
440
- if (((_a = this.headers["content-type"]) == null ? void 0 : _a.includes("application/json")) && typeof data.event.body === "string" && data.event.body.length > 1) {
441
- data.logger.debug("[onInvoke] Parse params from json body");
496
+ if (((_c = this.headers["content-type"]) == null ? void 0 : _c.includes("application/json")) && typeof data.event.body === "string" && data.event.body.length > 1) {
497
+ logger.debug("[onInvoke] Parse params from json body");
442
498
  try {
443
499
  this.params = Object.keys(this.params).length ? Object.assign(this.params, JSON.parse(data.event.body)) : JSON.parse(data.event.body);
444
500
  } catch (error) {
445
- data.logger.error("[onInvoke] Parse params from json body failed: %s", error.message);
501
+ logger.error(
502
+ "[onInvoke] Parse params from json body failed: %s",
503
+ error.message
504
+ );
446
505
  }
447
506
  } else {
448
- data.logger.debug("[onInvoke] Parse params from raw body");
507
+ logger.debug("[onInvoke] Parse params from raw body");
449
508
  this.params = data.event.body || /* @__PURE__ */ Object.create(null);
450
509
  }
451
- if (this.params && typeof this.params === "object" && this.params["_"])
452
- delete this.params["_"];
453
- data.event.params = JSON.parse(JSON.stringify(this.params));
454
- data.logger.debug("[onInvoke] Params: %j", this.params);
510
+ if (this.params && typeof this.params === "object" && this.params._)
511
+ delete this.params._;
512
+ data.event.params = deepClone(this.params);
513
+ logger.debug("[onInvoke] Params: %j", this.params);
455
514
  }
456
- this.cookie.invoke(this.headers.cookie, data.logger);
515
+ this.cookie.invoke(this.headers.cookie, logger);
457
516
  if (this.headers.cookie) {
458
- data.logger.debug("[onInvoke] Cookie: %j", this.cookie.content);
459
- data.logger.debug("[onInvoke] Session: %j", this.session.content);
517
+ logger.debug("[onInvoke] Cookie: %j", this.cookie.content);
518
+ logger.debug(
519
+ "[onInvoke] Session: %s %j",
520
+ this.session.config.key,
521
+ this.session.content
522
+ );
460
523
  }
461
524
  try {
462
525
  if (this.validator) {
463
- data.logger.debug("[onInvoke] Valid request");
464
- await this.validator.valid({
465
- headers: this.headers,
466
- params: this.params,
467
- cookie: this.cookie,
468
- session: this.session
469
- }, data.logger);
526
+ logger.debug("[onInvoke] Valid request");
527
+ await this.validator.valid(
528
+ {
529
+ headers: this.headers,
530
+ params: this.params,
531
+ cookie: this.cookie,
532
+ session: this.session
533
+ },
534
+ logger
535
+ );
470
536
  }
471
537
  await next();
472
538
  } catch (error) {
@@ -474,9 +540,11 @@ var Http = class {
474
540
  }
475
541
  this.session.update();
476
542
  if (data.response)
477
- if (data.response instanceof Error || ((_b = data.response.constructor) == null ? void 0 : _b.name) === "Error") {
478
- data.logger.error(data.response);
479
- this.response.body = JSON.stringify({ error: { message: data.response.message } });
543
+ if (data.response instanceof Error || ((_d = data.response.constructor) == null ? void 0 : _d.name) === "Error") {
544
+ logger.error(data.response);
545
+ this.response.body = JSON.stringify({
546
+ error: { message: data.response.message }
547
+ });
480
548
  try {
481
549
  this.response.statusCode = data.response.statusCode || 500;
482
550
  } catch (error) {
@@ -488,11 +556,15 @@ var Http = class {
488
556
  this.response.body = JSON.stringify({ data: data.response });
489
557
  if (!this.response.statusCode)
490
558
  this.response.statusCode = this.response.body ? 200 : 201;
491
- this.response.headers = Object.assign({
492
- "Content-Type": "application/json; charset=utf-8",
493
- "Cache-Control": "no-cache, no-store",
494
- "X-FaasJS-Request-Id": data.logger.label
495
- }, this.cookie.headers(), this.response.headers);
559
+ this.response.headers = Object.assign(
560
+ {
561
+ "Content-Type": "application/json; charset=utf-8",
562
+ "Cache-Control": "no-cache, no-store",
563
+ "X-FaasJS-Request-Id": data.logger.label
564
+ },
565
+ this.cookie.headers(),
566
+ this.response.headers
567
+ );
496
568
  data.response = Object.assign({}, data.response, this.response);
497
569
  const originBody = data.response.body;
498
570
  data.response.originBody = originBody;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@faasjs/http",
3
- "version": "0.0.3-beta.99",
3
+ "version": "0.0.4-beta.10",
4
4
  "license": "MIT",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",
@@ -21,12 +21,16 @@
21
21
  "files": [
22
22
  "dist"
23
23
  ],
24
- "dependencies": {
25
- "@faasjs/func": "0.0.3-beta.99",
26
- "@faasjs/logger": "0.0.3-beta.99"
24
+ "peerDependencies": {
25
+ "@faasjs/func": "0.0.4-beta.10",
26
+ "@faasjs/logger": "0.0.4-beta.10"
27
+ },
28
+ "devDependencies": {
29
+ "@faasjs/func": "0.0.4-beta.10",
30
+ "@faasjs/logger": "0.0.4-beta.10"
27
31
  },
28
32
  "engines": {
29
- "npm": ">=8.0.0",
30
- "node": ">=16.0.0"
33
+ "npm": ">=9.0.0",
34
+ "node": ">=18.0.0"
31
35
  }
32
36
  }