@faasjs/http 0.0.3-beta.36 → 0.0.3-beta.38

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.
Files changed (3) hide show
  1. package/dist/index.js +135 -15
  2. package/dist/index.mjs +126 -10
  3. package/package.json +3 -3
package/dist/index.js CHANGED
@@ -29,7 +29,130 @@ __export(src_exports, {
29
29
  useHttp: () => useHttp
30
30
  });
31
31
  module.exports = __toCommonJS(src_exports);
32
- var import_func = require("@faasjs/func");
32
+
33
+ // ../logger/src/index.ts
34
+ var import_util = require("util");
35
+ var LevelColor = ((LevelColor2) => {
36
+ LevelColor2[LevelColor2["debug"] = 90 /* GRAY */] = "debug";
37
+ LevelColor2[LevelColor2["info"] = 32 /* GREEN */] = "info";
38
+ LevelColor2[LevelColor2["warn"] = 33 /* ORANGE */] = "warn";
39
+ LevelColor2[LevelColor2["error"] = 31 /* RED */] = "error";
40
+ return LevelColor2;
41
+ })(LevelColor || {});
42
+ var LevelPriority = {
43
+ debug: 0,
44
+ info: 1,
45
+ warn: 2,
46
+ error: 3
47
+ };
48
+ var Logger = class {
49
+ constructor(label) {
50
+ this.colorfyOutput = true;
51
+ if (label)
52
+ this.label = label;
53
+ this.silent = !process.env.FaasLog && process.env.npm_config_argv && JSON.parse(process.env.npm_config_argv).original.includes("--silent");
54
+ if (["remote", "mono"].includes(process.env.FaasMode))
55
+ this.colorfyOutput = false;
56
+ this.level = process.env.FaasLog ? LevelPriority[process.env.FaasLog.toLowerCase()] : 0;
57
+ this.cachedTimers = {};
58
+ this.size = 1e3;
59
+ this.stdout = console.log;
60
+ this.stderr = console.error;
61
+ }
62
+ debug(message, ...args) {
63
+ this.log("debug", message, ...args);
64
+ return this;
65
+ }
66
+ info(message, ...args) {
67
+ this.log("info", message, ...args);
68
+ return this;
69
+ }
70
+ warn(message, ...args) {
71
+ this.log("warn", message, ...args);
72
+ return this;
73
+ }
74
+ error(message, ...args) {
75
+ let stack = false;
76
+ [message].concat(Array.from(args)).forEach((e) => {
77
+ if (e.stack) {
78
+ stack = true;
79
+ this.log("error", e.stack);
80
+ }
81
+ });
82
+ if (!stack)
83
+ this.log("error", message, ...args);
84
+ return this;
85
+ }
86
+ time(key, level = "debug") {
87
+ this.cachedTimers[key] = {
88
+ level,
89
+ time: new Date().getTime()
90
+ };
91
+ return this;
92
+ }
93
+ timeEnd(key, message, ...args) {
94
+ if (this.cachedTimers[key]) {
95
+ const timer = this.cachedTimers[key];
96
+ message = message + " +%ims";
97
+ args.push(new Date().getTime() - timer.time);
98
+ this[timer.level](message, ...args);
99
+ delete this.cachedTimers[key];
100
+ } else {
101
+ this.warn("timeEnd not found key %s", key);
102
+ this.debug(message);
103
+ }
104
+ return this;
105
+ }
106
+ raw(message, ...args) {
107
+ if (this.silent)
108
+ return this;
109
+ this.stdout((0, import_util.format)(message, ...args));
110
+ return this;
111
+ }
112
+ colorfy(color, message) {
113
+ return `\x1B[0${color}m${message}\x1B[39m`;
114
+ }
115
+ log(level, message, ...args) {
116
+ if (this.silent)
117
+ return this;
118
+ if (LevelPriority[level] < this.level)
119
+ return this;
120
+ let output = level.toUpperCase() + " " + (this.label ? `[${this.label}] ` : "") + (0, import_util.format)(message, ...args);
121
+ if (this.colorfyOutput && level !== "error")
122
+ output = this.colorfy(LevelColor[level], output);
123
+ else if (!this.colorfyOutput)
124
+ output = output.replace(/\n/g, "");
125
+ if (!output)
126
+ return this;
127
+ if (output.length > this.size && !["error", "warn"].includes(level))
128
+ output = output.slice(0, this.size - 100) + "..." + output.slice(output.length - 100);
129
+ if (level === "error")
130
+ this.stderr(output);
131
+ else
132
+ this.stdout(output);
133
+ return this;
134
+ }
135
+ };
136
+
137
+ // ../func/src/index.ts
138
+ var import_crypto = require("crypto");
139
+ var plugins = [];
140
+ function usePlugin(plugin) {
141
+ if (!plugins.find((p) => p.name === plugin.name))
142
+ plugins.push(plugin);
143
+ if (!plugin.mount)
144
+ plugin.mount = async function(data) {
145
+ if (plugin.onMount)
146
+ await plugin.onMount({
147
+ config: (data == null ? void 0 : data.config) || /* @__PURE__ */ Object.create(null),
148
+ event: /* @__PURE__ */ Object.create(null),
149
+ context: /* @__PURE__ */ Object.create(null),
150
+ logger: new Logger(plugin.name)
151
+ }, async () => Promise.resolve());
152
+ return plugin;
153
+ };
154
+ return plugin;
155
+ }
33
156
 
34
157
  // ../deep_merge/src/index.ts
35
158
  var shouldMerge = function(item) {
@@ -58,11 +181,8 @@ function deepMerge(...sources) {
58
181
  return acc;
59
182
  }
60
183
 
61
- // src/index.ts
62
- var import_logger = require("@faasjs/logger");
63
-
64
184
  // src/session.ts
65
- var import_crypto = require("crypto");
185
+ var import_crypto2 = require("crypto");
66
186
  var Session = class {
67
187
  constructor(cookie, config) {
68
188
  this.cookie = cookie;
@@ -70,7 +190,7 @@ var Session = class {
70
190
  console.warn("Session's secret is missing.");
71
191
  this.config = Object.assign({
72
192
  key: "key",
73
- secret: (0, import_crypto.randomBytes)(128).toString("hex"),
193
+ secret: (0, import_crypto2.randomBytes)(128).toString("hex"),
74
194
  salt: "salt",
75
195
  signedSalt: "signedSalt",
76
196
  keylen: 64,
@@ -78,14 +198,14 @@ var Session = class {
78
198
  digest: "sha256",
79
199
  cipherName: "aes-256-cbc"
80
200
  }, config);
81
- this.secret = (0, import_crypto.pbkdf2Sync)(
201
+ this.secret = (0, import_crypto2.pbkdf2Sync)(
82
202
  this.config.secret,
83
203
  this.config.salt,
84
204
  this.config.iterations,
85
205
  this.config.keylen / 2,
86
206
  this.config.digest
87
207
  );
88
- this.signedSecret = (0, import_crypto.pbkdf2Sync)(
208
+ this.signedSecret = (0, import_crypto2.pbkdf2Sync)(
89
209
  this.config.secret,
90
210
  this.config.signedSalt,
91
211
  this.config.iterations,
@@ -106,11 +226,11 @@ var Session = class {
106
226
  encode(text) {
107
227
  if (typeof text !== "string")
108
228
  text = JSON.stringify(text);
109
- const iv = (0, import_crypto.randomBytes)(16);
110
- const cipher = (0, import_crypto.createCipheriv)(this.config.cipherName, this.secret, iv);
229
+ const iv = (0, import_crypto2.randomBytes)(16);
230
+ const cipher = (0, import_crypto2.createCipheriv)(this.config.cipherName, this.secret, iv);
111
231
  const encrypted = Buffer.concat([cipher.update(text), cipher.final()]).toString("base64");
112
232
  const main = Buffer.from([encrypted, iv.toString("base64")].join("--")).toString("base64");
113
- const hmac = (0, import_crypto.createHmac)(this.config.digest, this.signedSecret);
233
+ const hmac = (0, import_crypto2.createHmac)(this.config.digest, this.signedSecret);
114
234
  hmac.update(main);
115
235
  const digest = hmac.digest("hex");
116
236
  return main + "--" + digest;
@@ -118,7 +238,7 @@ var Session = class {
118
238
  decode(text) {
119
239
  text = decodeURIComponent(text);
120
240
  const signedParts = text.split("--");
121
- const hmac = (0, import_crypto.createHmac)(this.config.digest, this.signedSecret);
241
+ const hmac = (0, import_crypto2.createHmac)(this.config.digest, this.signedSecret);
122
242
  hmac.update(signedParts[0]);
123
243
  const digest = hmac.digest("hex");
124
244
  if (signedParts[1] !== digest)
@@ -127,7 +247,7 @@ var Session = class {
127
247
  const parts = message.split("--").map(function(part2) {
128
248
  return Buffer.from(part2, "base64");
129
249
  });
130
- const cipher = (0, import_crypto.createDecipheriv)(this.config.cipherName, this.secret, parts[1]);
250
+ const cipher = (0, import_crypto2.createDecipheriv)(this.config.cipherName, this.secret, parts[1]);
131
251
  const part = Buffer.from(cipher.update(parts[0])).toString("utf8");
132
252
  const final = cipher.final("utf8");
133
253
  const decrypt = [part, final].join("");
@@ -437,7 +557,7 @@ var Http = class {
437
557
  var _a;
438
558
  data.dependencies["@faasjs/http"] = "*";
439
559
  await next();
440
- const logger = new import_logger.Logger(this.name);
560
+ const logger = new Logger(this.name);
441
561
  logger.debug("Generate api gateway's config");
442
562
  logger.debug("%j", data);
443
563
  const config = data.config.plugins ? deepMerge(data.config.plugins[this.name || this.type], { config: this.config }) : { config: this.config };
@@ -591,7 +711,7 @@ var Http = class {
591
711
  }
592
712
  };
593
713
  function useHttp(config) {
594
- return (0, import_func.usePlugin)(new Http(config));
714
+ return usePlugin(new Http(config));
595
715
  }
596
716
  // Annotate the CommonJS export names for ESM import in node:
597
717
  0 && (module.exports = {
package/dist/index.mjs CHANGED
@@ -6,10 +6,129 @@ var __require = /* @__PURE__ */ ((x) => typeof require !== "undefined" ? require
6
6
  throw new Error('Dynamic require of "' + x + '" is not supported');
7
7
  });
8
8
 
9
- // src/index.ts
10
- import {
11
- usePlugin
12
- } from "@faasjs/func";
9
+ // ../logger/src/index.ts
10
+ import { format } from "util";
11
+ var LevelColor = ((LevelColor2) => {
12
+ LevelColor2[LevelColor2["debug"] = 90 /* GRAY */] = "debug";
13
+ LevelColor2[LevelColor2["info"] = 32 /* GREEN */] = "info";
14
+ LevelColor2[LevelColor2["warn"] = 33 /* ORANGE */] = "warn";
15
+ LevelColor2[LevelColor2["error"] = 31 /* RED */] = "error";
16
+ return LevelColor2;
17
+ })(LevelColor || {});
18
+ var LevelPriority = {
19
+ debug: 0,
20
+ info: 1,
21
+ warn: 2,
22
+ error: 3
23
+ };
24
+ var Logger = class {
25
+ constructor(label) {
26
+ this.colorfyOutput = true;
27
+ if (label)
28
+ this.label = label;
29
+ this.silent = !process.env.FaasLog && process.env.npm_config_argv && JSON.parse(process.env.npm_config_argv).original.includes("--silent");
30
+ if (["remote", "mono"].includes(process.env.FaasMode))
31
+ this.colorfyOutput = false;
32
+ this.level = process.env.FaasLog ? LevelPriority[process.env.FaasLog.toLowerCase()] : 0;
33
+ this.cachedTimers = {};
34
+ this.size = 1e3;
35
+ this.stdout = console.log;
36
+ this.stderr = console.error;
37
+ }
38
+ debug(message, ...args) {
39
+ this.log("debug", message, ...args);
40
+ return this;
41
+ }
42
+ info(message, ...args) {
43
+ this.log("info", message, ...args);
44
+ return this;
45
+ }
46
+ warn(message, ...args) {
47
+ this.log("warn", message, ...args);
48
+ return this;
49
+ }
50
+ error(message, ...args) {
51
+ let stack = false;
52
+ [message].concat(Array.from(args)).forEach((e) => {
53
+ if (e.stack) {
54
+ stack = true;
55
+ this.log("error", e.stack);
56
+ }
57
+ });
58
+ if (!stack)
59
+ this.log("error", message, ...args);
60
+ return this;
61
+ }
62
+ time(key, level = "debug") {
63
+ this.cachedTimers[key] = {
64
+ level,
65
+ time: new Date().getTime()
66
+ };
67
+ return this;
68
+ }
69
+ timeEnd(key, message, ...args) {
70
+ if (this.cachedTimers[key]) {
71
+ const timer = this.cachedTimers[key];
72
+ message = message + " +%ims";
73
+ args.push(new Date().getTime() - timer.time);
74
+ this[timer.level](message, ...args);
75
+ delete this.cachedTimers[key];
76
+ } else {
77
+ this.warn("timeEnd not found key %s", key);
78
+ this.debug(message);
79
+ }
80
+ return this;
81
+ }
82
+ raw(message, ...args) {
83
+ if (this.silent)
84
+ return this;
85
+ this.stdout(format(message, ...args));
86
+ return this;
87
+ }
88
+ colorfy(color, message) {
89
+ return `\x1B[0${color}m${message}\x1B[39m`;
90
+ }
91
+ log(level, message, ...args) {
92
+ if (this.silent)
93
+ return this;
94
+ if (LevelPriority[level] < this.level)
95
+ return this;
96
+ let output = level.toUpperCase() + " " + (this.label ? `[${this.label}] ` : "") + format(message, ...args);
97
+ if (this.colorfyOutput && level !== "error")
98
+ output = this.colorfy(LevelColor[level], output);
99
+ else if (!this.colorfyOutput)
100
+ output = output.replace(/\n/g, "");
101
+ if (!output)
102
+ return this;
103
+ if (output.length > this.size && !["error", "warn"].includes(level))
104
+ output = output.slice(0, this.size - 100) + "..." + output.slice(output.length - 100);
105
+ if (level === "error")
106
+ this.stderr(output);
107
+ else
108
+ this.stdout(output);
109
+ return this;
110
+ }
111
+ };
112
+
113
+ // ../func/src/index.ts
114
+ import { randomBytes } from "crypto";
115
+ var plugins = [];
116
+ function usePlugin(plugin) {
117
+ if (!plugins.find((p) => p.name === plugin.name))
118
+ plugins.push(plugin);
119
+ if (!plugin.mount)
120
+ plugin.mount = async function(data) {
121
+ if (plugin.onMount)
122
+ await plugin.onMount({
123
+ config: (data == null ? void 0 : data.config) || /* @__PURE__ */ Object.create(null),
124
+ event: /* @__PURE__ */ Object.create(null),
125
+ context: /* @__PURE__ */ Object.create(null),
126
+ logger: new Logger(plugin.name)
127
+ }, async () => Promise.resolve());
128
+ return plugin;
129
+ };
130
+ return plugin;
131
+ }
13
132
 
14
133
  // ../deep_merge/src/index.ts
15
134
  var shouldMerge = function(item) {
@@ -38,12 +157,9 @@ function deepMerge(...sources) {
38
157
  return acc;
39
158
  }
40
159
 
41
- // src/index.ts
42
- import { Logger } from "@faasjs/logger";
43
-
44
160
  // src/session.ts
45
161
  import {
46
- randomBytes,
162
+ randomBytes as randomBytes2,
47
163
  pbkdf2Sync,
48
164
  createCipheriv,
49
165
  createHmac,
@@ -56,7 +172,7 @@ var Session = class {
56
172
  console.warn("Session's secret is missing.");
57
173
  this.config = Object.assign({
58
174
  key: "key",
59
- secret: randomBytes(128).toString("hex"),
175
+ secret: randomBytes2(128).toString("hex"),
60
176
  salt: "salt",
61
177
  signedSalt: "signedSalt",
62
178
  keylen: 64,
@@ -92,7 +208,7 @@ var Session = class {
92
208
  encode(text) {
93
209
  if (typeof text !== "string")
94
210
  text = JSON.stringify(text);
95
- const iv = randomBytes(16);
211
+ const iv = randomBytes2(16);
96
212
  const cipher = createCipheriv(this.config.cipherName, this.secret, iv);
97
213
  const encrypted = Buffer.concat([cipher.update(text), cipher.final()]).toString("base64");
98
214
  const main = Buffer.from([encrypted, iv.toString("base64")].join("--")).toString("base64");
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@faasjs/http",
3
- "version": "0.0.3-beta.36",
3
+ "version": "0.0.3-beta.38",
4
4
  "license": "MIT",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",
@@ -23,8 +23,8 @@
23
23
  "dist"
24
24
  ],
25
25
  "dependencies": {
26
- "@faasjs/func": "^0.0.3-beta.36",
27
- "@faasjs/logger": "^0.0.3-beta.36"
26
+ "@faasjs/func": "^0.0.3-beta.38",
27
+ "@faasjs/logger": "^0.0.3-beta.38"
28
28
  },
29
29
  "engines": {
30
30
  "npm": ">=8.0.0",