@faasjs/func 0.0.3-beta.37 → 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.
- package/dist/index.js +110 -7
- package/dist/index.mjs +103 -2
- package/package.json +3 -3
package/dist/index.js
CHANGED
|
@@ -25,7 +25,110 @@ __export(src_exports, {
|
|
|
25
25
|
usePlugin: () => usePlugin
|
|
26
26
|
});
|
|
27
27
|
module.exports = __toCommonJS(src_exports);
|
|
28
|
-
|
|
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
|
+
constructor(label) {
|
|
46
|
+
this.colorfyOutput = true;
|
|
47
|
+
if (label)
|
|
48
|
+
this.label = label;
|
|
49
|
+
this.silent = !process.env.FaasLog && process.env.npm_config_argv && JSON.parse(process.env.npm_config_argv).original.includes("--silent");
|
|
50
|
+
if (["remote", "mono"].includes(process.env.FaasMode))
|
|
51
|
+
this.colorfyOutput = false;
|
|
52
|
+
this.level = process.env.FaasLog ? LevelPriority[process.env.FaasLog.toLowerCase()] : 0;
|
|
53
|
+
this.cachedTimers = {};
|
|
54
|
+
this.size = 1e3;
|
|
55
|
+
this.stdout = console.log;
|
|
56
|
+
this.stderr = console.error;
|
|
57
|
+
}
|
|
58
|
+
debug(message, ...args) {
|
|
59
|
+
this.log("debug", message, ...args);
|
|
60
|
+
return this;
|
|
61
|
+
}
|
|
62
|
+
info(message, ...args) {
|
|
63
|
+
this.log("info", message, ...args);
|
|
64
|
+
return this;
|
|
65
|
+
}
|
|
66
|
+
warn(message, ...args) {
|
|
67
|
+
this.log("warn", message, ...args);
|
|
68
|
+
return this;
|
|
69
|
+
}
|
|
70
|
+
error(message, ...args) {
|
|
71
|
+
let stack = false;
|
|
72
|
+
[message].concat(Array.from(args)).forEach((e) => {
|
|
73
|
+
if (e.stack) {
|
|
74
|
+
stack = true;
|
|
75
|
+
this.log("error", e.stack);
|
|
76
|
+
}
|
|
77
|
+
});
|
|
78
|
+
if (!stack)
|
|
79
|
+
this.log("error", message, ...args);
|
|
80
|
+
return this;
|
|
81
|
+
}
|
|
82
|
+
time(key, level = "debug") {
|
|
83
|
+
this.cachedTimers[key] = {
|
|
84
|
+
level,
|
|
85
|
+
time: new Date().getTime()
|
|
86
|
+
};
|
|
87
|
+
return this;
|
|
88
|
+
}
|
|
89
|
+
timeEnd(key, message, ...args) {
|
|
90
|
+
if (this.cachedTimers[key]) {
|
|
91
|
+
const timer = this.cachedTimers[key];
|
|
92
|
+
message = message + " +%ims";
|
|
93
|
+
args.push(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
|
+
raw(message, ...args) {
|
|
103
|
+
if (this.silent)
|
|
104
|
+
return this;
|
|
105
|
+
this.stdout((0, import_util.format)(message, ...args));
|
|
106
|
+
return this;
|
|
107
|
+
}
|
|
108
|
+
colorfy(color, message) {
|
|
109
|
+
return `\x1B[0${color}m${message}\x1B[39m`;
|
|
110
|
+
}
|
|
111
|
+
log(level, message, ...args) {
|
|
112
|
+
if (this.silent)
|
|
113
|
+
return this;
|
|
114
|
+
if (LevelPriority[level] < this.level)
|
|
115
|
+
return this;
|
|
116
|
+
let output = level.toUpperCase() + " " + (this.label ? `[${this.label}] ` : "") + (0, import_util.format)(message, ...args);
|
|
117
|
+
if (this.colorfyOutput && level !== "error")
|
|
118
|
+
output = this.colorfy(LevelColor[level], output);
|
|
119
|
+
else if (!this.colorfyOutput)
|
|
120
|
+
output = output.replace(/\n/g, "");
|
|
121
|
+
if (!output)
|
|
122
|
+
return this;
|
|
123
|
+
if (output.length > this.size && !["error", "warn"].includes(level))
|
|
124
|
+
output = output.slice(0, this.size - 100) + "..." + output.slice(output.length - 100);
|
|
125
|
+
if (level === "error")
|
|
126
|
+
this.stderr(output);
|
|
127
|
+
else
|
|
128
|
+
this.stdout(output);
|
|
129
|
+
return this;
|
|
130
|
+
}
|
|
131
|
+
};
|
|
29
132
|
|
|
30
133
|
// src/plugins/run_handler/index.ts
|
|
31
134
|
var RunHandler = class {
|
|
@@ -73,7 +176,7 @@ var Func = class {
|
|
|
73
176
|
try {
|
|
74
177
|
this.filename = new Error().stack.split("\n").find((s) => /[^/]\.func\.ts/.test(s)).match(/\((.*\.func\.ts).*\)/)[1];
|
|
75
178
|
} catch (error) {
|
|
76
|
-
new
|
|
179
|
+
new Logger("Func").warn(error.message);
|
|
77
180
|
}
|
|
78
181
|
}
|
|
79
182
|
compose(key) {
|
|
@@ -93,7 +196,7 @@ var Func = class {
|
|
|
93
196
|
}
|
|
94
197
|
return async function(data, next) {
|
|
95
198
|
let index = -1;
|
|
96
|
-
const logger = (data == null ? void 0 : data.logger) || new
|
|
199
|
+
const logger = (data == null ? void 0 : data.logger) || new Logger();
|
|
97
200
|
const dispatch = async function(i) {
|
|
98
201
|
if (i <= index)
|
|
99
202
|
return Promise.reject(Error("next() called multiple times"));
|
|
@@ -122,14 +225,14 @@ var Func = class {
|
|
|
122
225
|
}
|
|
123
226
|
deploy(data) {
|
|
124
227
|
if (!data.logger)
|
|
125
|
-
data.logger = new
|
|
228
|
+
data.logger = new Logger("Func");
|
|
126
229
|
data.logger.debug("onDeploy");
|
|
127
230
|
data.logger.debug("Plugins: " + this.plugins.map((p) => `${p.type}#${p.name}`).join(","));
|
|
128
231
|
return this.compose("onDeploy")(data);
|
|
129
232
|
}
|
|
130
233
|
async mount(data) {
|
|
131
234
|
if (!data.logger)
|
|
132
|
-
data.logger = new
|
|
235
|
+
data.logger = new Logger("Func");
|
|
133
236
|
data.logger.debug("onMount");
|
|
134
237
|
if (this.mounted) {
|
|
135
238
|
data.logger.warn("mount() has been called, skipped.");
|
|
@@ -170,7 +273,7 @@ var Func = class {
|
|
|
170
273
|
if (!context.request_at)
|
|
171
274
|
context.request_at = Math.round(new Date().getTime() / 1e3);
|
|
172
275
|
context.callbackWaitsForEmptyEventLoop = false;
|
|
173
|
-
const logger = new
|
|
276
|
+
const logger = new Logger(context.request_id);
|
|
174
277
|
logger.debug("event: %j", event);
|
|
175
278
|
logger.debug("context: %j", context);
|
|
176
279
|
const data = {
|
|
@@ -202,7 +305,7 @@ function usePlugin(plugin) {
|
|
|
202
305
|
config: (data == null ? void 0 : data.config) || /* @__PURE__ */ Object.create(null),
|
|
203
306
|
event: /* @__PURE__ */ Object.create(null),
|
|
204
307
|
context: /* @__PURE__ */ Object.create(null),
|
|
205
|
-
logger: new
|
|
308
|
+
logger: new Logger(plugin.name)
|
|
206
309
|
}, async () => Promise.resolve());
|
|
207
310
|
return plugin;
|
|
208
311
|
};
|
package/dist/index.mjs
CHANGED
|
@@ -1,5 +1,106 @@
|
|
|
1
|
-
// src/index.ts
|
|
2
|
-
import {
|
|
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
|
+
constructor(label) {
|
|
18
|
+
this.colorfyOutput = true;
|
|
19
|
+
if (label)
|
|
20
|
+
this.label = label;
|
|
21
|
+
this.silent = !process.env.FaasLog && process.env.npm_config_argv && JSON.parse(process.env.npm_config_argv).original.includes("--silent");
|
|
22
|
+
if (["remote", "mono"].includes(process.env.FaasMode))
|
|
23
|
+
this.colorfyOutput = false;
|
|
24
|
+
this.level = process.env.FaasLog ? LevelPriority[process.env.FaasLog.toLowerCase()] : 0;
|
|
25
|
+
this.cachedTimers = {};
|
|
26
|
+
this.size = 1e3;
|
|
27
|
+
this.stdout = console.log;
|
|
28
|
+
this.stderr = console.error;
|
|
29
|
+
}
|
|
30
|
+
debug(message, ...args) {
|
|
31
|
+
this.log("debug", message, ...args);
|
|
32
|
+
return this;
|
|
33
|
+
}
|
|
34
|
+
info(message, ...args) {
|
|
35
|
+
this.log("info", message, ...args);
|
|
36
|
+
return this;
|
|
37
|
+
}
|
|
38
|
+
warn(message, ...args) {
|
|
39
|
+
this.log("warn", message, ...args);
|
|
40
|
+
return this;
|
|
41
|
+
}
|
|
42
|
+
error(message, ...args) {
|
|
43
|
+
let stack = false;
|
|
44
|
+
[message].concat(Array.from(args)).forEach((e) => {
|
|
45
|
+
if (e.stack) {
|
|
46
|
+
stack = true;
|
|
47
|
+
this.log("error", e.stack);
|
|
48
|
+
}
|
|
49
|
+
});
|
|
50
|
+
if (!stack)
|
|
51
|
+
this.log("error", message, ...args);
|
|
52
|
+
return this;
|
|
53
|
+
}
|
|
54
|
+
time(key, level = "debug") {
|
|
55
|
+
this.cachedTimers[key] = {
|
|
56
|
+
level,
|
|
57
|
+
time: new Date().getTime()
|
|
58
|
+
};
|
|
59
|
+
return this;
|
|
60
|
+
}
|
|
61
|
+
timeEnd(key, message, ...args) {
|
|
62
|
+
if (this.cachedTimers[key]) {
|
|
63
|
+
const timer = this.cachedTimers[key];
|
|
64
|
+
message = message + " +%ims";
|
|
65
|
+
args.push(new Date().getTime() - timer.time);
|
|
66
|
+
this[timer.level](message, ...args);
|
|
67
|
+
delete this.cachedTimers[key];
|
|
68
|
+
} else {
|
|
69
|
+
this.warn("timeEnd not found key %s", key);
|
|
70
|
+
this.debug(message);
|
|
71
|
+
}
|
|
72
|
+
return this;
|
|
73
|
+
}
|
|
74
|
+
raw(message, ...args) {
|
|
75
|
+
if (this.silent)
|
|
76
|
+
return this;
|
|
77
|
+
this.stdout(format(message, ...args));
|
|
78
|
+
return this;
|
|
79
|
+
}
|
|
80
|
+
colorfy(color, message) {
|
|
81
|
+
return `\x1B[0${color}m${message}\x1B[39m`;
|
|
82
|
+
}
|
|
83
|
+
log(level, message, ...args) {
|
|
84
|
+
if (this.silent)
|
|
85
|
+
return this;
|
|
86
|
+
if (LevelPriority[level] < this.level)
|
|
87
|
+
return this;
|
|
88
|
+
let output = level.toUpperCase() + " " + (this.label ? `[${this.label}] ` : "") + format(message, ...args);
|
|
89
|
+
if (this.colorfyOutput && level !== "error")
|
|
90
|
+
output = this.colorfy(LevelColor[level], output);
|
|
91
|
+
else if (!this.colorfyOutput)
|
|
92
|
+
output = output.replace(/\n/g, "");
|
|
93
|
+
if (!output)
|
|
94
|
+
return this;
|
|
95
|
+
if (output.length > this.size && !["error", "warn"].includes(level))
|
|
96
|
+
output = output.slice(0, this.size - 100) + "..." + output.slice(output.length - 100);
|
|
97
|
+
if (level === "error")
|
|
98
|
+
this.stderr(output);
|
|
99
|
+
else
|
|
100
|
+
this.stdout(output);
|
|
101
|
+
return this;
|
|
102
|
+
}
|
|
103
|
+
};
|
|
3
104
|
|
|
4
105
|
// src/plugins/run_handler/index.ts
|
|
5
106
|
var RunHandler = class {
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@faasjs/func",
|
|
3
|
-
"version": "0.0.3-beta.
|
|
3
|
+
"version": "0.0.3-beta.38",
|
|
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.
|
|
26
|
-
"@faasjs/logger": "^0.0.3-beta.
|
|
25
|
+
"@faasjs/deep_merge": "^0.0.3-beta.38",
|
|
26
|
+
"@faasjs/logger": "^0.0.3-beta.38"
|
|
27
27
|
},
|
|
28
28
|
"engines": {
|
|
29
29
|
"npm": ">=8.0.0",
|