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

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,146 +25,7 @@ __export(src_exports, {
25
25
  usePlugin: () => usePlugin
26
26
  });
27
27
  module.exports = __toCommonJS(src_exports);
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
- };
28
+ var import_logger = require("@faasjs/logger");
168
29
 
169
30
  // src/plugins/run_handler/index.ts
170
31
  var RunHandler = class {
@@ -199,12 +60,6 @@ var RunHandler = class {
199
60
  // src/index.ts
200
61
  var import_crypto = require("crypto");
201
62
  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
- */
208
63
  constructor(config) {
209
64
  this.handler = config.handler;
210
65
  this.plugins = config.plugins || [];
@@ -218,7 +73,7 @@ var Func = class {
218
73
  try {
219
74
  this.filename = new Error().stack.split("\n").find((s) => /[^/]\.func\.ts/.test(s)).match(/\((.*\.func\.ts).*\)/)[1];
220
75
  } catch (error) {
221
- new Logger("Func").warn(error.message);
76
+ new import_logger.Logger("Func").warn(error.message);
222
77
  }
223
78
  }
224
79
  compose(key) {
@@ -238,7 +93,7 @@ var Func = class {
238
93
  }
239
94
  return async function(data, next) {
240
95
  let index = -1;
241
- const logger = (data == null ? void 0 : data.logger) || new Logger();
96
+ const logger = (data == null ? void 0 : data.logger) || new import_logger.Logger();
242
97
  const dispatch = async function(i) {
243
98
  if (i <= index)
244
99
  return Promise.reject(Error("next() called multiple times"));
@@ -265,26 +120,16 @@ var Func = class {
265
120
  return await dispatch(0);
266
121
  };
267
122
  }
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
- */
275
123
  deploy(data) {
276
124
  if (!data.logger)
277
- data.logger = new Logger("Func");
125
+ data.logger = new import_logger.Logger("Func");
278
126
  data.logger.debug("onDeploy");
279
127
  data.logger.debug("Plugins: " + this.plugins.map((p) => `${p.type}#${p.name}`).join(","));
280
128
  return this.compose("onDeploy")(data);
281
129
  }
282
- /**
283
- * First time mount the function
284
- */
285
130
  async mount(data) {
286
131
  if (!data.logger)
287
- data.logger = new Logger("Func");
132
+ data.logger = new import_logger.Logger("Func");
288
133
  data.logger.debug("onMount");
289
134
  if (this.mounted) {
290
135
  data.logger.warn("mount() has been called, skipped.");
@@ -301,10 +146,6 @@ var Func = class {
301
146
  data.logger.timeEnd("mount", "mounted");
302
147
  }
303
148
  }
304
- /**
305
- * Invoke the function
306
- * @param data {object} data
307
- */
308
149
  async invoke(data) {
309
150
  if (!this.mounted)
310
151
  await this.mount({
@@ -320,9 +161,6 @@ var Func = class {
320
161
  data.response = error;
321
162
  }
322
163
  }
323
- /**
324
- * Export the function
325
- */
326
164
  export() {
327
165
  const handler = async (event, context, callback) => {
328
166
  if (typeof context === "undefined")
@@ -330,9 +168,9 @@ var Func = class {
330
168
  if (!context.request_id)
331
169
  context.request_id = (0, import_crypto.randomBytes)(16).toString("hex");
332
170
  if (!context.request_at)
333
- context.request_at = Math.round((/* @__PURE__ */ new Date()).getTime() / 1e3);
171
+ context.request_at = Math.round(new Date().getTime() / 1e3);
334
172
  context.callbackWaitsForEmptyEventLoop = false;
335
- const logger = new Logger(context.request_id);
173
+ const logger = new import_logger.Logger(context.request_id);
336
174
  logger.debug("event: %j", event);
337
175
  logger.debug("context: %j", context);
338
176
  const data = {
@@ -364,7 +202,7 @@ function usePlugin(plugin) {
364
202
  config: (data == null ? void 0 : data.config) || /* @__PURE__ */ Object.create(null),
365
203
  event: /* @__PURE__ */ Object.create(null),
366
204
  context: /* @__PURE__ */ Object.create(null),
367
- logger: new Logger(plugin.name)
205
+ logger: new import_logger.Logger(plugin.name)
368
206
  }, async () => Promise.resolve());
369
207
  return plugin;
370
208
  };
package/dist/index.mjs CHANGED
@@ -1,142 +1,5 @@
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
- };
1
+ // src/index.ts
2
+ import { Logger } from "@faasjs/logger";
140
3
 
141
4
  // src/plugins/run_handler/index.ts
142
5
  var RunHandler = class {
@@ -171,12 +34,6 @@ var RunHandler = class {
171
34
  // src/index.ts
172
35
  import { randomBytes } from "crypto";
173
36
  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
- */
180
37
  constructor(config) {
181
38
  this.handler = config.handler;
182
39
  this.plugins = config.plugins || [];
@@ -237,13 +94,6 @@ var Func = class {
237
94
  return await dispatch(0);
238
95
  };
239
96
  }
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
- */
247
97
  deploy(data) {
248
98
  if (!data.logger)
249
99
  data.logger = new Logger("Func");
@@ -251,9 +101,6 @@ var Func = class {
251
101
  data.logger.debug("Plugins: " + this.plugins.map((p) => `${p.type}#${p.name}`).join(","));
252
102
  return this.compose("onDeploy")(data);
253
103
  }
254
- /**
255
- * First time mount the function
256
- */
257
104
  async mount(data) {
258
105
  if (!data.logger)
259
106
  data.logger = new Logger("Func");
@@ -273,10 +120,6 @@ var Func = class {
273
120
  data.logger.timeEnd("mount", "mounted");
274
121
  }
275
122
  }
276
- /**
277
- * Invoke the function
278
- * @param data {object} data
279
- */
280
123
  async invoke(data) {
281
124
  if (!this.mounted)
282
125
  await this.mount({
@@ -292,9 +135,6 @@ var Func = class {
292
135
  data.response = error;
293
136
  }
294
137
  }
295
- /**
296
- * Export the function
297
- */
298
138
  export() {
299
139
  const handler = async (event, context, callback) => {
300
140
  if (typeof context === "undefined")
@@ -302,7 +142,7 @@ var Func = class {
302
142
  if (!context.request_id)
303
143
  context.request_id = randomBytes(16).toString("hex");
304
144
  if (!context.request_at)
305
- context.request_at = Math.round((/* @__PURE__ */ new Date()).getTime() / 1e3);
145
+ context.request_at = Math.round(new Date().getTime() / 1e3);
306
146
  context.callbackWaitsForEmptyEventLoop = false;
307
147
  const logger = new Logger(context.request_id);
308
148
  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.64",
3
+ "version": "0.0.3-beta.65",
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.64",
26
- "@faasjs/logger": "^0.0.3-beta.64"
25
+ "@faasjs/deep_merge": "^0.0.3-beta.65",
26
+ "@faasjs/logger": "^0.0.3-beta.65"
27
27
  },
28
28
  "engines": {
29
29
  "npm": ">=8.0.0",