@faasjs/http 0.0.3-beta.99 → 0.0.4-beta.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 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);
@@ -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,6 +445,7 @@ var Http = class {
403
445
  await provider.deploy(this.type, data, config);
404
446
  }
405
447
  async onMount(data, next) {
448
+ var _a;
406
449
  data.logger.debug("[onMount] merge config");
407
450
  const prefix = `SECRET_${this.name.toUpperCase()}_`;
408
451
  for (let key in process.env)
@@ -421,14 +464,19 @@ var Http = class {
421
464
  } else
422
465
  this.config[key] = value;
423
466
  }
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);
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
+ );
426
472
  data.logger.debug("[onMount] prepare cookie & session");
427
473
  this.cookie = new Cookie(this.config.cookie || {}, data.logger);
428
474
  this.session = this.cookie.session;
429
475
  if (this.validatorOptions) {
430
476
  data.logger.debug("[onMount] prepare validator");
431
- this.validator = new Validator(this.validatorOptions);
477
+ this.validator = new Validator(
478
+ this.validatorOptions
479
+ );
432
480
  }
433
481
  await next();
434
482
  }
@@ -444,15 +492,18 @@ var Http = class {
444
492
  try {
445
493
  this.params = Object.keys(this.params).length ? Object.assign(this.params, JSON.parse(data.event.body)) : JSON.parse(data.event.body);
446
494
  } catch (error) {
447
- 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
+ );
448
499
  }
449
500
  } else {
450
501
  data.logger.debug("[onInvoke] Parse params from raw body");
451
502
  this.params = data.event.body || /* @__PURE__ */ Object.create(null);
452
503
  }
453
- if (this.params && typeof this.params === "object" && this.params["_"])
454
- delete this.params["_"];
455
- data.event.params = JSON.parse(JSON.stringify(this.params));
504
+ if (this.params && typeof this.params === "object" && this.params._)
505
+ delete this.params._;
506
+ data.event.params = deepClone(this.params);
456
507
  data.logger.debug("[onInvoke] Params: %j", this.params);
457
508
  }
458
509
  this.cookie.invoke(this.headers.cookie, data.logger);
@@ -463,12 +514,15 @@ var Http = class {
463
514
  try {
464
515
  if (this.validator) {
465
516
  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);
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
+ );
472
526
  }
473
527
  await next();
474
528
  } catch (error) {
@@ -478,7 +532,9 @@ var Http = class {
478
532
  if (data.response)
479
533
  if (data.response instanceof Error || ((_b = data.response.constructor) == null ? void 0 : _b.name) === "Error") {
480
534
  data.logger.error(data.response);
481
- this.response.body = JSON.stringify({ error: { message: data.response.message } });
535
+ this.response.body = JSON.stringify({
536
+ error: { message: data.response.message }
537
+ });
482
538
  try {
483
539
  this.response.statusCode = data.response.statusCode || 500;
484
540
  } catch (error) {
@@ -490,11 +546,15 @@ var Http = class {
490
546
  this.response.body = JSON.stringify({ data: data.response });
491
547
  if (!this.response.statusCode)
492
548
  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);
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
+ );
498
558
  data.response = Object.assign({}, data.response, this.response);
499
559
  const originBody = data.response.body;
500
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);
@@ -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,6 +443,7 @@ var Http = class {
401
443
  await provider.deploy(this.type, data, config);
402
444
  }
403
445
  async onMount(data, next) {
446
+ var _a;
404
447
  data.logger.debug("[onMount] merge config");
405
448
  const prefix = `SECRET_${this.name.toUpperCase()}_`;
406
449
  for (let key in process.env)
@@ -419,14 +462,19 @@ var Http = class {
419
462
  } else
420
463
  this.config[key] = value;
421
464
  }
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);
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
+ );
424
470
  data.logger.debug("[onMount] prepare cookie & session");
425
471
  this.cookie = new Cookie(this.config.cookie || {}, data.logger);
426
472
  this.session = this.cookie.session;
427
473
  if (this.validatorOptions) {
428
474
  data.logger.debug("[onMount] prepare validator");
429
- this.validator = new Validator(this.validatorOptions);
475
+ this.validator = new Validator(
476
+ this.validatorOptions
477
+ );
430
478
  }
431
479
  await next();
432
480
  }
@@ -442,15 +490,18 @@ var Http = class {
442
490
  try {
443
491
  this.params = Object.keys(this.params).length ? Object.assign(this.params, JSON.parse(data.event.body)) : JSON.parse(data.event.body);
444
492
  } catch (error) {
445
- 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
+ );
446
497
  }
447
498
  } else {
448
499
  data.logger.debug("[onInvoke] Parse params from raw body");
449
500
  this.params = data.event.body || /* @__PURE__ */ Object.create(null);
450
501
  }
451
- if (this.params && typeof this.params === "object" && this.params["_"])
452
- delete this.params["_"];
453
- data.event.params = JSON.parse(JSON.stringify(this.params));
502
+ if (this.params && typeof this.params === "object" && this.params._)
503
+ delete this.params._;
504
+ data.event.params = deepClone(this.params);
454
505
  data.logger.debug("[onInvoke] Params: %j", this.params);
455
506
  }
456
507
  this.cookie.invoke(this.headers.cookie, data.logger);
@@ -461,12 +512,15 @@ var Http = class {
461
512
  try {
462
513
  if (this.validator) {
463
514
  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);
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
+ );
470
524
  }
471
525
  await next();
472
526
  } catch (error) {
@@ -476,7 +530,9 @@ var Http = class {
476
530
  if (data.response)
477
531
  if (data.response instanceof Error || ((_b = data.response.constructor) == null ? void 0 : _b.name) === "Error") {
478
532
  data.logger.error(data.response);
479
- this.response.body = JSON.stringify({ error: { message: data.response.message } });
533
+ this.response.body = JSON.stringify({
534
+ error: { message: data.response.message }
535
+ });
480
536
  try {
481
537
  this.response.statusCode = data.response.statusCode || 500;
482
538
  } catch (error) {
@@ -488,11 +544,15 @@ var Http = class {
488
544
  this.response.body = JSON.stringify({ data: data.response });
489
545
  if (!this.response.statusCode)
490
546
  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);
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
+ );
496
556
  data.response = Object.assign({}, data.response, this.response);
497
557
  const originBody = data.response.body;
498
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.99",
3
+ "version": "0.0.4-beta.2",
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.2",
26
+ "@faasjs/logger": "0.0.4-beta.2"
27
+ },
28
+ "devDependencies": {
29
+ "@faasjs/func": "0.0.4-beta.2",
30
+ "@faasjs/logger": "0.0.4-beta.2"
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
  }