@faasjs/func 0.0.3-beta.62 → 0.0.3-beta.64

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.js CHANGED
@@ -25,7 +25,146 @@ __export(src_exports, {
25
25
  usePlugin: () => usePlugin
26
26
  });
27
27
  module.exports = __toCommonJS(src_exports);
28
- var import_logger = require("@faasjs/logger");
28
+
29
+ // ../logger/src/index.ts
30
+ var import_util = require("util");
31
+ var LevelColor = ((LevelColor2) => {
32
+ LevelColor2[LevelColor2["debug"] = 90 /* GRAY */] = "debug";
33
+ LevelColor2[LevelColor2["info"] = 32 /* GREEN */] = "info";
34
+ LevelColor2[LevelColor2["warn"] = 33 /* ORANGE */] = "warn";
35
+ LevelColor2[LevelColor2["error"] = 31 /* RED */] = "error";
36
+ return LevelColor2;
37
+ })(LevelColor || {});
38
+ var LevelPriority = {
39
+ debug: 0,
40
+ info: 1,
41
+ warn: 2,
42
+ error: 3
43
+ };
44
+ var Logger = class {
45
+ /**
46
+ * @param label {string} Prefix label
47
+ */
48
+ constructor(label) {
49
+ this.colorfyOutput = true;
50
+ if (label)
51
+ this.label = label;
52
+ this.silent = !process.env.FaasLog && process.env.npm_config_argv && JSON.parse(process.env.npm_config_argv).original.includes("--silent");
53
+ if (["remote", "mono"].includes(process.env.FaasMode))
54
+ this.colorfyOutput = false;
55
+ this.level = process.env.FaasLog ? LevelPriority[process.env.FaasLog.toLowerCase()] : 0;
56
+ this.cachedTimers = {};
57
+ this.size = 1e3;
58
+ this.stdout = console.log;
59
+ this.stderr = console.error;
60
+ }
61
+ /**
62
+ * @param message {string} message
63
+ * @param args {...any=} arguments
64
+ */
65
+ debug(message, ...args) {
66
+ this.log("debug", message, ...args);
67
+ return this;
68
+ }
69
+ /**
70
+ * @param message {string} message
71
+ * @param args {...any=} arguments
72
+ */
73
+ info(message, ...args) {
74
+ this.log("info", message, ...args);
75
+ return this;
76
+ }
77
+ /**
78
+ * @param message {string} message
79
+ * @param args {...any=} arguments
80
+ */
81
+ warn(message, ...args) {
82
+ this.log("warn", message, ...args);
83
+ return this;
84
+ }
85
+ /**
86
+ * @param message {any} message or Error object
87
+ * @param args {...any=} arguments
88
+ */
89
+ error(message, ...args) {
90
+ let stack = false;
91
+ [message].concat(Array.from(args)).forEach((e) => {
92
+ if (e.stack) {
93
+ stack = true;
94
+ this.log("error", e.stack);
95
+ }
96
+ });
97
+ if (!stack)
98
+ this.log("error", message, ...args);
99
+ return this;
100
+ }
101
+ /**
102
+ * @param key {string} timer's label
103
+ * @param level [string=debug] 日志级别,支持 debug、info、warn、error
104
+ */
105
+ time(key, level = "debug") {
106
+ this.cachedTimers[key] = {
107
+ level,
108
+ time: (/* @__PURE__ */ new Date()).getTime()
109
+ };
110
+ return this;
111
+ }
112
+ /**
113
+ * @param key {string} timer's label
114
+ * @param message {string} message
115
+ * @param args {...any=} arguments
116
+ */
117
+ timeEnd(key, message, ...args) {
118
+ if (this.cachedTimers[key]) {
119
+ const timer = this.cachedTimers[key];
120
+ message = message + " +%ims";
121
+ args.push((/* @__PURE__ */ new Date()).getTime() - timer.time);
122
+ this[timer.level](message, ...args);
123
+ delete this.cachedTimers[key];
124
+ } else {
125
+ this.warn("timeEnd not found key %s", key);
126
+ this.debug(message);
127
+ }
128
+ return this;
129
+ }
130
+ /**
131
+ * @param message {string} message
132
+ * @param args {...any=} arguments
133
+ */
134
+ raw(message, ...args) {
135
+ if (this.silent)
136
+ return this;
137
+ this.stdout((0, import_util.format)(message, ...args));
138
+ return this;
139
+ }
140
+ /**
141
+ * @param color {number} color code
142
+ * @param message {string} message
143
+ */
144
+ colorfy(color, message) {
145
+ return `\x1B[0${color}m${message}\x1B[39m`;
146
+ }
147
+ log(level, message, ...args) {
148
+ if (this.silent)
149
+ return this;
150
+ if (LevelPriority[level] < this.level)
151
+ return this;
152
+ let output = level.toUpperCase() + " " + (this.label ? `[${this.label}] ` : "") + (0, import_util.format)(message, ...args);
153
+ if (this.colorfyOutput && level !== "error")
154
+ output = this.colorfy(LevelColor[level], output);
155
+ else if (!this.colorfyOutput)
156
+ output = output.replace(/\n/g, "");
157
+ if (!output)
158
+ return this;
159
+ if (output.length > this.size && !["error", "warn"].includes(level))
160
+ output = output.slice(0, this.size - 100) + "..." + output.slice(output.length - 100);
161
+ if (level === "error")
162
+ this.stderr(output);
163
+ else
164
+ this.stdout(output);
165
+ return this;
166
+ }
167
+ };
29
168
 
30
169
  // src/plugins/run_handler/index.ts
31
170
  var RunHandler = class {
@@ -60,6 +199,12 @@ var RunHandler = class {
60
199
  // src/index.ts
61
200
  var import_crypto = require("crypto");
62
201
  var Func = class {
202
+ /**
203
+ * Create a cloud function
204
+ * @param config {object} config
205
+ * @param config.plugins {Plugin[]} plugins list
206
+ * @param config.handler {Handler} business logic
207
+ */
63
208
  constructor(config) {
64
209
  this.handler = config.handler;
65
210
  this.plugins = config.plugins || [];
@@ -73,7 +218,7 @@ var Func = class {
73
218
  try {
74
219
  this.filename = new Error().stack.split("\n").find((s) => /[^/]\.func\.ts/.test(s)).match(/\((.*\.func\.ts).*\)/)[1];
75
220
  } catch (error) {
76
- new import_logger.Logger("Func").warn(error.message);
221
+ new Logger("Func").warn(error.message);
77
222
  }
78
223
  }
79
224
  compose(key) {
@@ -93,7 +238,7 @@ var Func = class {
93
238
  }
94
239
  return async function(data, next) {
95
240
  let index = -1;
96
- const logger = (data == null ? void 0 : data.logger) || new import_logger.Logger();
241
+ const logger = (data == null ? void 0 : data.logger) || new Logger();
97
242
  const dispatch = async function(i) {
98
243
  if (i <= index)
99
244
  return Promise.reject(Error("next() called multiple times"));
@@ -120,16 +265,26 @@ var Func = class {
120
265
  return await dispatch(0);
121
266
  };
122
267
  }
268
+ /**
269
+ * Deploy the function
270
+ * @param data {object} data
271
+ * @param data.root {string} root path
272
+ * @param data.filename {string} filename
273
+ * @param data.env {string} environment
274
+ */
123
275
  deploy(data) {
124
276
  if (!data.logger)
125
- data.logger = new import_logger.Logger("Func");
277
+ data.logger = new Logger("Func");
126
278
  data.logger.debug("onDeploy");
127
279
  data.logger.debug("Plugins: " + this.plugins.map((p) => `${p.type}#${p.name}`).join(","));
128
280
  return this.compose("onDeploy")(data);
129
281
  }
282
+ /**
283
+ * First time mount the function
284
+ */
130
285
  async mount(data) {
131
286
  if (!data.logger)
132
- data.logger = new import_logger.Logger("Func");
287
+ data.logger = new Logger("Func");
133
288
  data.logger.debug("onMount");
134
289
  if (this.mounted) {
135
290
  data.logger.warn("mount() has been called, skipped.");
@@ -146,6 +301,10 @@ var Func = class {
146
301
  data.logger.timeEnd("mount", "mounted");
147
302
  }
148
303
  }
304
+ /**
305
+ * Invoke the function
306
+ * @param data {object} data
307
+ */
149
308
  async invoke(data) {
150
309
  if (!this.mounted)
151
310
  await this.mount({
@@ -161,6 +320,9 @@ var Func = class {
161
320
  data.response = error;
162
321
  }
163
322
  }
323
+ /**
324
+ * Export the function
325
+ */
164
326
  export() {
165
327
  const handler = async (event, context, callback) => {
166
328
  if (typeof context === "undefined")
@@ -168,9 +330,9 @@ var Func = class {
168
330
  if (!context.request_id)
169
331
  context.request_id = (0, import_crypto.randomBytes)(16).toString("hex");
170
332
  if (!context.request_at)
171
- context.request_at = Math.round(new Date().getTime() / 1e3);
333
+ context.request_at = Math.round((/* @__PURE__ */ new Date()).getTime() / 1e3);
172
334
  context.callbackWaitsForEmptyEventLoop = false;
173
- const logger = new import_logger.Logger(context.request_id);
335
+ const logger = new Logger(context.request_id);
174
336
  logger.debug("event: %j", event);
175
337
  logger.debug("context: %j", context);
176
338
  const data = {
@@ -202,7 +364,7 @@ function usePlugin(plugin) {
202
364
  config: (data == null ? void 0 : data.config) || /* @__PURE__ */ Object.create(null),
203
365
  event: /* @__PURE__ */ Object.create(null),
204
366
  context: /* @__PURE__ */ Object.create(null),
205
- logger: new import_logger.Logger(plugin.name)
367
+ logger: new Logger(plugin.name)
206
368
  }, async () => Promise.resolve());
207
369
  return plugin;
208
370
  };
package/dist/index.mjs CHANGED
@@ -1,5 +1,142 @@
1
- // src/index.ts
2
- import { Logger } from "@faasjs/logger";
1
+ // ../logger/src/index.ts
2
+ import { format } from "util";
3
+ var LevelColor = ((LevelColor2) => {
4
+ LevelColor2[LevelColor2["debug"] = 90 /* GRAY */] = "debug";
5
+ LevelColor2[LevelColor2["info"] = 32 /* GREEN */] = "info";
6
+ LevelColor2[LevelColor2["warn"] = 33 /* ORANGE */] = "warn";
7
+ LevelColor2[LevelColor2["error"] = 31 /* RED */] = "error";
8
+ return LevelColor2;
9
+ })(LevelColor || {});
10
+ var LevelPriority = {
11
+ debug: 0,
12
+ info: 1,
13
+ warn: 2,
14
+ error: 3
15
+ };
16
+ var Logger = class {
17
+ /**
18
+ * @param label {string} Prefix label
19
+ */
20
+ constructor(label) {
21
+ this.colorfyOutput = true;
22
+ if (label)
23
+ this.label = label;
24
+ this.silent = !process.env.FaasLog && process.env.npm_config_argv && JSON.parse(process.env.npm_config_argv).original.includes("--silent");
25
+ if (["remote", "mono"].includes(process.env.FaasMode))
26
+ this.colorfyOutput = false;
27
+ this.level = process.env.FaasLog ? LevelPriority[process.env.FaasLog.toLowerCase()] : 0;
28
+ this.cachedTimers = {};
29
+ this.size = 1e3;
30
+ this.stdout = console.log;
31
+ this.stderr = console.error;
32
+ }
33
+ /**
34
+ * @param message {string} message
35
+ * @param args {...any=} arguments
36
+ */
37
+ debug(message, ...args) {
38
+ this.log("debug", message, ...args);
39
+ return this;
40
+ }
41
+ /**
42
+ * @param message {string} message
43
+ * @param args {...any=} arguments
44
+ */
45
+ info(message, ...args) {
46
+ this.log("info", message, ...args);
47
+ return this;
48
+ }
49
+ /**
50
+ * @param message {string} message
51
+ * @param args {...any=} arguments
52
+ */
53
+ warn(message, ...args) {
54
+ this.log("warn", message, ...args);
55
+ return this;
56
+ }
57
+ /**
58
+ * @param message {any} message or Error object
59
+ * @param args {...any=} arguments
60
+ */
61
+ error(message, ...args) {
62
+ let stack = false;
63
+ [message].concat(Array.from(args)).forEach((e) => {
64
+ if (e.stack) {
65
+ stack = true;
66
+ this.log("error", e.stack);
67
+ }
68
+ });
69
+ if (!stack)
70
+ this.log("error", message, ...args);
71
+ return this;
72
+ }
73
+ /**
74
+ * @param key {string} timer's label
75
+ * @param level [string=debug] 日志级别,支持 debug、info、warn、error
76
+ */
77
+ time(key, level = "debug") {
78
+ this.cachedTimers[key] = {
79
+ level,
80
+ time: (/* @__PURE__ */ new Date()).getTime()
81
+ };
82
+ return this;
83
+ }
84
+ /**
85
+ * @param key {string} timer's label
86
+ * @param message {string} message
87
+ * @param args {...any=} arguments
88
+ */
89
+ timeEnd(key, message, ...args) {
90
+ if (this.cachedTimers[key]) {
91
+ const timer = this.cachedTimers[key];
92
+ message = message + " +%ims";
93
+ args.push((/* @__PURE__ */ new Date()).getTime() - timer.time);
94
+ this[timer.level](message, ...args);
95
+ delete this.cachedTimers[key];
96
+ } else {
97
+ this.warn("timeEnd not found key %s", key);
98
+ this.debug(message);
99
+ }
100
+ return this;
101
+ }
102
+ /**
103
+ * @param message {string} message
104
+ * @param args {...any=} arguments
105
+ */
106
+ raw(message, ...args) {
107
+ if (this.silent)
108
+ return this;
109
+ this.stdout(format(message, ...args));
110
+ return this;
111
+ }
112
+ /**
113
+ * @param color {number} color code
114
+ * @param message {string} message
115
+ */
116
+ colorfy(color, message) {
117
+ return `\x1B[0${color}m${message}\x1B[39m`;
118
+ }
119
+ log(level, message, ...args) {
120
+ if (this.silent)
121
+ return this;
122
+ if (LevelPriority[level] < this.level)
123
+ return this;
124
+ let output = level.toUpperCase() + " " + (this.label ? `[${this.label}] ` : "") + format(message, ...args);
125
+ if (this.colorfyOutput && level !== "error")
126
+ output = this.colorfy(LevelColor[level], output);
127
+ else if (!this.colorfyOutput)
128
+ output = output.replace(/\n/g, "");
129
+ if (!output)
130
+ return this;
131
+ if (output.length > this.size && !["error", "warn"].includes(level))
132
+ output = output.slice(0, this.size - 100) + "..." + output.slice(output.length - 100);
133
+ if (level === "error")
134
+ this.stderr(output);
135
+ else
136
+ this.stdout(output);
137
+ return this;
138
+ }
139
+ };
3
140
 
4
141
  // src/plugins/run_handler/index.ts
5
142
  var RunHandler = class {
@@ -34,6 +171,12 @@ var RunHandler = class {
34
171
  // src/index.ts
35
172
  import { randomBytes } from "crypto";
36
173
  var Func = class {
174
+ /**
175
+ * Create a cloud function
176
+ * @param config {object} config
177
+ * @param config.plugins {Plugin[]} plugins list
178
+ * @param config.handler {Handler} business logic
179
+ */
37
180
  constructor(config) {
38
181
  this.handler = config.handler;
39
182
  this.plugins = config.plugins || [];
@@ -94,6 +237,13 @@ var Func = class {
94
237
  return await dispatch(0);
95
238
  };
96
239
  }
240
+ /**
241
+ * Deploy the function
242
+ * @param data {object} data
243
+ * @param data.root {string} root path
244
+ * @param data.filename {string} filename
245
+ * @param data.env {string} environment
246
+ */
97
247
  deploy(data) {
98
248
  if (!data.logger)
99
249
  data.logger = new Logger("Func");
@@ -101,6 +251,9 @@ var Func = class {
101
251
  data.logger.debug("Plugins: " + this.plugins.map((p) => `${p.type}#${p.name}`).join(","));
102
252
  return this.compose("onDeploy")(data);
103
253
  }
254
+ /**
255
+ * First time mount the function
256
+ */
104
257
  async mount(data) {
105
258
  if (!data.logger)
106
259
  data.logger = new Logger("Func");
@@ -120,6 +273,10 @@ var Func = class {
120
273
  data.logger.timeEnd("mount", "mounted");
121
274
  }
122
275
  }
276
+ /**
277
+ * Invoke the function
278
+ * @param data {object} data
279
+ */
123
280
  async invoke(data) {
124
281
  if (!this.mounted)
125
282
  await this.mount({
@@ -135,6 +292,9 @@ var Func = class {
135
292
  data.response = error;
136
293
  }
137
294
  }
295
+ /**
296
+ * Export the function
297
+ */
138
298
  export() {
139
299
  const handler = async (event, context, callback) => {
140
300
  if (typeof context === "undefined")
@@ -142,7 +302,7 @@ var Func = class {
142
302
  if (!context.request_id)
143
303
  context.request_id = randomBytes(16).toString("hex");
144
304
  if (!context.request_at)
145
- context.request_at = Math.round(new Date().getTime() / 1e3);
305
+ context.request_at = Math.round((/* @__PURE__ */ new Date()).getTime() / 1e3);
146
306
  context.callbackWaitsForEmptyEventLoop = false;
147
307
  const logger = new Logger(context.request_id);
148
308
  logger.debug("event: %j", event);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@faasjs/func",
3
- "version": "0.0.3-beta.62",
3
+ "version": "0.0.3-beta.64",
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/deep_merge": "^0.0.3-beta.62",
26
- "@faasjs/logger": "^0.0.3-beta.62"
25
+ "@faasjs/deep_merge": "^0.0.3-beta.64",
26
+ "@faasjs/logger": "^0.0.3-beta.64"
27
27
  },
28
28
  "engines": {
29
29
  "npm": ">=8.0.0",