@faasjs/http 0.0.3-beta.107 → 0.0.3-beta.108

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);
@@ -121,7 +139,9 @@ var Cookie = class {
121
139
  x = x.trim();
122
140
  const k = /([^=]+)/.exec(x);
123
141
  if (k !== null)
124
- this.content[k[0]] = decodeURIComponent(x.replace(`${k[0]}=`, "").replace(/;$/, ""));
142
+ this.content[k[0]] = decodeURIComponent(
143
+ x.replace(`${k[0]}=`, "").replace(/;$/, "")
144
+ );
125
145
  });
126
146
  this.setCookie = /* @__PURE__ */ Object.create(null);
127
147
  this.session.invoke(this.read(this.session.config.key), logger);
@@ -403,13 +423,18 @@ var Http = class {
403
423
  const logger$1 = new logger.Logger(this.name);
404
424
  logger$1.debug("Generate api gateway's config");
405
425
  logger$1.debug("%j", data);
406
- 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 };
407
429
  if (!config.config.path) {
408
- 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$/, "")}`;
409
431
  if (config.config.path === "/index")
410
432
  config.config.path = "/";
411
433
  if (config.config.ignorePathPrefix) {
412
- 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
+ );
413
438
  if (config.config.path === "")
414
439
  config.config.path = "/";
415
440
  }
@@ -420,6 +445,7 @@ var Http = class {
420
445
  await provider.deploy(this.type, data, config);
421
446
  }
422
447
  async onMount(data, next) {
448
+ var _a;
423
449
  data.logger.debug("[onMount] merge config");
424
450
  const prefix = `SECRET_${this.name.toUpperCase()}_`;
425
451
  for (let key in process.env)
@@ -438,14 +464,19 @@ var Http = class {
438
464
  } else
439
465
  this.config[key] = value;
440
466
  }
441
- if (data.config.plugins && data.config.plugins[this.name || this.type])
442
- this.config = deep_merge.deepMerge(this.config, data.config.plugins[this.name || this.type].config);
467
+ if ((_a = data.config.plugins) == null ? void 0 : _a[this.name || this.type])
468
+ this.config = deep_merge.deepMerge(
469
+ this.config,
470
+ data.config.plugins[this.name || this.type].config
471
+ );
443
472
  data.logger.debug("[onMount] prepare cookie & session");
444
473
  this.cookie = new Cookie(this.config.cookie || {}, data.logger);
445
474
  this.session = this.cookie.session;
446
475
  if (this.validatorOptions) {
447
476
  data.logger.debug("[onMount] prepare validator");
448
- this.validator = new Validator(this.validatorOptions);
477
+ this.validator = new Validator(
478
+ this.validatorOptions
479
+ );
449
480
  }
450
481
  await next();
451
482
  }
@@ -461,14 +492,17 @@ var Http = class {
461
492
  try {
462
493
  this.params = Object.keys(this.params).length ? Object.assign(this.params, JSON.parse(data.event.body)) : JSON.parse(data.event.body);
463
494
  } catch (error) {
464
- data.logger.error("[onInvoke] Parse params from json body failed: %s", error.message);
495
+ data.logger.error(
496
+ "[onInvoke] Parse params from json body failed: %s",
497
+ error.message
498
+ );
465
499
  }
466
500
  } else {
467
501
  data.logger.debug("[onInvoke] Parse params from raw body");
468
502
  this.params = data.event.body || /* @__PURE__ */ Object.create(null);
469
503
  }
470
- if (this.params && typeof this.params === "object" && this.params["_"])
471
- delete this.params["_"];
504
+ if (this.params && typeof this.params === "object" && this.params._)
505
+ delete this.params._;
472
506
  data.event.params = deepClone(this.params);
473
507
  data.logger.debug("[onInvoke] Params: %j", this.params);
474
508
  }
@@ -480,12 +514,15 @@ var Http = class {
480
514
  try {
481
515
  if (this.validator) {
482
516
  data.logger.debug("[onInvoke] Valid request");
483
- await this.validator.valid({
484
- headers: this.headers,
485
- params: this.params,
486
- cookie: this.cookie,
487
- session: this.session
488
- }, data.logger);
517
+ await this.validator.valid(
518
+ {
519
+ headers: this.headers,
520
+ params: this.params,
521
+ cookie: this.cookie,
522
+ session: this.session
523
+ },
524
+ data.logger
525
+ );
489
526
  }
490
527
  await next();
491
528
  } catch (error) {
@@ -495,7 +532,9 @@ var Http = class {
495
532
  if (data.response)
496
533
  if (data.response instanceof Error || ((_b = data.response.constructor) == null ? void 0 : _b.name) === "Error") {
497
534
  data.logger.error(data.response);
498
- this.response.body = JSON.stringify({ error: { message: data.response.message } });
535
+ this.response.body = JSON.stringify({
536
+ error: { message: data.response.message }
537
+ });
499
538
  try {
500
539
  this.response.statusCode = data.response.statusCode || 500;
501
540
  } catch (error) {
@@ -507,11 +546,15 @@ var Http = class {
507
546
  this.response.body = JSON.stringify({ data: data.response });
508
547
  if (!this.response.statusCode)
509
548
  this.response.statusCode = this.response.body ? 200 : 201;
510
- this.response.headers = Object.assign({
511
- "Content-Type": "application/json; charset=utf-8",
512
- "Cache-Control": "no-cache, no-store",
513
- "X-FaasJS-Request-Id": data.logger.label
514
- }, this.cookie.headers(), this.response.headers);
549
+ this.response.headers = Object.assign(
550
+ {
551
+ "Content-Type": "application/json; charset=utf-8",
552
+ "Cache-Control": "no-cache, no-store",
553
+ "X-FaasJS-Request-Id": data.logger.label
554
+ },
555
+ this.cookie.headers(),
556
+ this.response.headers
557
+ );
515
558
  data.response = Object.assign({}, data.response, this.response);
516
559
  const originBody = data.response.body;
517
560
  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);
@@ -119,7 +137,9 @@ var Cookie = class {
119
137
  x = x.trim();
120
138
  const k = /([^=]+)/.exec(x);
121
139
  if (k !== null)
122
- this.content[k[0]] = decodeURIComponent(x.replace(`${k[0]}=`, "").replace(/;$/, ""));
140
+ this.content[k[0]] = decodeURIComponent(
141
+ x.replace(`${k[0]}=`, "").replace(/;$/, "")
142
+ );
123
143
  });
124
144
  this.setCookie = /* @__PURE__ */ Object.create(null);
125
145
  this.session.invoke(this.read(this.session.config.key), logger);
@@ -401,13 +421,18 @@ var Http = class {
401
421
  const logger = new Logger(this.name);
402
422
  logger.debug("Generate api gateway's config");
403
423
  logger.debug("%j", data);
404
- 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 };
405
427
  if (!config.config.path) {
406
- 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$/, "")}`;
407
429
  if (config.config.path === "/index")
408
430
  config.config.path = "/";
409
431
  if (config.config.ignorePathPrefix) {
410
- 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
+ );
411
436
  if (config.config.path === "")
412
437
  config.config.path = "/";
413
438
  }
@@ -418,6 +443,7 @@ var Http = class {
418
443
  await provider.deploy(this.type, data, config);
419
444
  }
420
445
  async onMount(data, next) {
446
+ var _a;
421
447
  data.logger.debug("[onMount] merge config");
422
448
  const prefix = `SECRET_${this.name.toUpperCase()}_`;
423
449
  for (let key in process.env)
@@ -436,14 +462,19 @@ var Http = class {
436
462
  } else
437
463
  this.config[key] = value;
438
464
  }
439
- if (data.config.plugins && data.config.plugins[this.name || this.type])
440
- this.config = deepMerge(this.config, data.config.plugins[this.name || this.type].config);
465
+ if ((_a = data.config.plugins) == null ? void 0 : _a[this.name || this.type])
466
+ this.config = deepMerge(
467
+ this.config,
468
+ data.config.plugins[this.name || this.type].config
469
+ );
441
470
  data.logger.debug("[onMount] prepare cookie & session");
442
471
  this.cookie = new Cookie(this.config.cookie || {}, data.logger);
443
472
  this.session = this.cookie.session;
444
473
  if (this.validatorOptions) {
445
474
  data.logger.debug("[onMount] prepare validator");
446
- this.validator = new Validator(this.validatorOptions);
475
+ this.validator = new Validator(
476
+ this.validatorOptions
477
+ );
447
478
  }
448
479
  await next();
449
480
  }
@@ -459,14 +490,17 @@ var Http = class {
459
490
  try {
460
491
  this.params = Object.keys(this.params).length ? Object.assign(this.params, JSON.parse(data.event.body)) : JSON.parse(data.event.body);
461
492
  } catch (error) {
462
- data.logger.error("[onInvoke] Parse params from json body failed: %s", error.message);
493
+ data.logger.error(
494
+ "[onInvoke] Parse params from json body failed: %s",
495
+ error.message
496
+ );
463
497
  }
464
498
  } else {
465
499
  data.logger.debug("[onInvoke] Parse params from raw body");
466
500
  this.params = data.event.body || /* @__PURE__ */ Object.create(null);
467
501
  }
468
- if (this.params && typeof this.params === "object" && this.params["_"])
469
- delete this.params["_"];
502
+ if (this.params && typeof this.params === "object" && this.params._)
503
+ delete this.params._;
470
504
  data.event.params = deepClone(this.params);
471
505
  data.logger.debug("[onInvoke] Params: %j", this.params);
472
506
  }
@@ -478,12 +512,15 @@ var Http = class {
478
512
  try {
479
513
  if (this.validator) {
480
514
  data.logger.debug("[onInvoke] Valid request");
481
- await this.validator.valid({
482
- headers: this.headers,
483
- params: this.params,
484
- cookie: this.cookie,
485
- session: this.session
486
- }, data.logger);
515
+ await this.validator.valid(
516
+ {
517
+ headers: this.headers,
518
+ params: this.params,
519
+ cookie: this.cookie,
520
+ session: this.session
521
+ },
522
+ data.logger
523
+ );
487
524
  }
488
525
  await next();
489
526
  } catch (error) {
@@ -493,7 +530,9 @@ var Http = class {
493
530
  if (data.response)
494
531
  if (data.response instanceof Error || ((_b = data.response.constructor) == null ? void 0 : _b.name) === "Error") {
495
532
  data.logger.error(data.response);
496
- this.response.body = JSON.stringify({ error: { message: data.response.message } });
533
+ this.response.body = JSON.stringify({
534
+ error: { message: data.response.message }
535
+ });
497
536
  try {
498
537
  this.response.statusCode = data.response.statusCode || 500;
499
538
  } catch (error) {
@@ -505,11 +544,15 @@ var Http = class {
505
544
  this.response.body = JSON.stringify({ data: data.response });
506
545
  if (!this.response.statusCode)
507
546
  this.response.statusCode = this.response.body ? 200 : 201;
508
- this.response.headers = Object.assign({
509
- "Content-Type": "application/json; charset=utf-8",
510
- "Cache-Control": "no-cache, no-store",
511
- "X-FaasJS-Request-Id": data.logger.label
512
- }, this.cookie.headers(), this.response.headers);
547
+ this.response.headers = Object.assign(
548
+ {
549
+ "Content-Type": "application/json; charset=utf-8",
550
+ "Cache-Control": "no-cache, no-store",
551
+ "X-FaasJS-Request-Id": data.logger.label
552
+ },
553
+ this.cookie.headers(),
554
+ this.response.headers
555
+ );
513
556
  data.response = Object.assign({}, data.response, this.response);
514
557
  const originBody = data.response.body;
515
558
  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.107",
3
+ "version": "0.0.3-beta.108",
4
4
  "license": "MIT",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",
@@ -22,8 +22,8 @@
22
22
  "dist"
23
23
  ],
24
24
  "dependencies": {
25
- "@faasjs/func": "0.0.3-beta.107",
26
- "@faasjs/logger": "0.0.3-beta.107"
25
+ "@faasjs/func": "0.0.3-beta.108",
26
+ "@faasjs/logger": "0.0.3-beta.108"
27
27
  },
28
28
  "engines": {
29
29
  "npm": ">=8.0.0",