@faasjs/func 0.0.3-beta.71 → 0.0.3-beta.73
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 +2 -230
- package/dist/index.mjs +8 -256
- package/package.json +3 -3
package/dist/index.js
CHANGED
|
@@ -1,13 +1,8 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
var __create = Object.create;
|
|
3
2
|
var __defProp = Object.defineProperty;
|
|
4
3
|
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
5
4
|
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
6
|
-
var __getProtoOf = Object.getPrototypeOf;
|
|
7
5
|
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
8
|
-
var __commonJS = (cb, mod) => function __require() {
|
|
9
|
-
return mod || (0, cb[__getOwnPropNames(cb)[0]])((mod = { exports: {} }).exports, mod), mod.exports;
|
|
10
|
-
};
|
|
11
6
|
var __export = (target, all) => {
|
|
12
7
|
for (var name in all)
|
|
13
8
|
__defProp(target, name, { get: all[name], enumerable: true });
|
|
@@ -20,208 +15,8 @@ var __copyProps = (to, from, except, desc) => {
|
|
|
20
15
|
}
|
|
21
16
|
return to;
|
|
22
17
|
};
|
|
23
|
-
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
|
24
|
-
// If the importer is in node compatibility mode or this is not an ESM
|
|
25
|
-
// file that has been converted to a CommonJS file using a Babel-
|
|
26
|
-
// compatible transform (i.e. "__esModule" has not been set), then set
|
|
27
|
-
// "default" to the CommonJS "module.exports" for node compatibility.
|
|
28
|
-
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
|
|
29
|
-
mod
|
|
30
|
-
));
|
|
31
18
|
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
32
19
|
|
|
33
|
-
// ../logger/dist/index.js
|
|
34
|
-
var require_dist = __commonJS({
|
|
35
|
-
"../logger/dist/index.js"(exports, module2) {
|
|
36
|
-
"use strict";
|
|
37
|
-
var __defProp2 = Object.defineProperty;
|
|
38
|
-
var __getOwnPropDesc2 = Object.getOwnPropertyDescriptor;
|
|
39
|
-
var __getOwnPropNames2 = Object.getOwnPropertyNames;
|
|
40
|
-
var __hasOwnProp2 = Object.prototype.hasOwnProperty;
|
|
41
|
-
var __export2 = (target, all) => {
|
|
42
|
-
for (var name in all)
|
|
43
|
-
__defProp2(target, name, { get: all[name], enumerable: true });
|
|
44
|
-
};
|
|
45
|
-
var __copyProps2 = (to, from, except, desc) => {
|
|
46
|
-
if (from && typeof from === "object" || typeof from === "function") {
|
|
47
|
-
for (let key of __getOwnPropNames2(from))
|
|
48
|
-
if (!__hasOwnProp2.call(to, key) && key !== except)
|
|
49
|
-
__defProp2(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc2(from, key)) || desc.enumerable });
|
|
50
|
-
}
|
|
51
|
-
return to;
|
|
52
|
-
};
|
|
53
|
-
var __toCommonJS2 = (mod) => __copyProps2(__defProp2({}, "__esModule", { value: true }), mod);
|
|
54
|
-
var src_exports2 = {};
|
|
55
|
-
__export2(src_exports2, {
|
|
56
|
-
Color: () => Color,
|
|
57
|
-
Logger: () => Logger2
|
|
58
|
-
});
|
|
59
|
-
module2.exports = __toCommonJS2(src_exports2);
|
|
60
|
-
var import_util = require("util");
|
|
61
|
-
var Color = /* @__PURE__ */ ((Color2) => {
|
|
62
|
-
Color2[Color2["DEFAULT"] = 39] = "DEFAULT";
|
|
63
|
-
Color2[Color2["BLACK"] = 30] = "BLACK";
|
|
64
|
-
Color2[Color2["RED"] = 31] = "RED";
|
|
65
|
-
Color2[Color2["GREEN"] = 32] = "GREEN";
|
|
66
|
-
Color2[Color2["ORANGE"] = 33] = "ORANGE";
|
|
67
|
-
Color2[Color2["BLUE"] = 34] = "BLUE";
|
|
68
|
-
Color2[Color2["MAGENTA"] = 35] = "MAGENTA";
|
|
69
|
-
Color2[Color2["CYAN"] = 36] = "CYAN";
|
|
70
|
-
Color2[Color2["GRAY"] = 90] = "GRAY";
|
|
71
|
-
return Color2;
|
|
72
|
-
})(Color || {});
|
|
73
|
-
var LevelColor = ((LevelColor2) => {
|
|
74
|
-
LevelColor2[
|
|
75
|
-
LevelColor2["debug"] = 90
|
|
76
|
-
/* GRAY */
|
|
77
|
-
] = "debug";
|
|
78
|
-
LevelColor2[
|
|
79
|
-
LevelColor2["info"] = 32
|
|
80
|
-
/* GREEN */
|
|
81
|
-
] = "info";
|
|
82
|
-
LevelColor2[
|
|
83
|
-
LevelColor2["warn"] = 33
|
|
84
|
-
/* ORANGE */
|
|
85
|
-
] = "warn";
|
|
86
|
-
LevelColor2[
|
|
87
|
-
LevelColor2["error"] = 31
|
|
88
|
-
/* RED */
|
|
89
|
-
] = "error";
|
|
90
|
-
return LevelColor2;
|
|
91
|
-
})(LevelColor || {});
|
|
92
|
-
var LevelPriority = {
|
|
93
|
-
debug: 0,
|
|
94
|
-
info: 1,
|
|
95
|
-
warn: 2,
|
|
96
|
-
error: 3
|
|
97
|
-
};
|
|
98
|
-
var Logger2 = class {
|
|
99
|
-
/**
|
|
100
|
-
* @param label {string} Prefix label
|
|
101
|
-
*/
|
|
102
|
-
constructor(label) {
|
|
103
|
-
this.colorfyOutput = true;
|
|
104
|
-
if (label)
|
|
105
|
-
this.label = label;
|
|
106
|
-
this.silent = !process.env.FaasLog && process.env.npm_config_argv && JSON.parse(process.env.npm_config_argv).original.includes("--silent");
|
|
107
|
-
if (["remote", "mono"].includes(process.env.FaasMode))
|
|
108
|
-
this.colorfyOutput = false;
|
|
109
|
-
this.level = process.env.FaasLog ? LevelPriority[process.env.FaasLog.toLowerCase()] : 0;
|
|
110
|
-
this.cachedTimers = {};
|
|
111
|
-
this.size = 1e3;
|
|
112
|
-
this.stdout = console.log;
|
|
113
|
-
this.stderr = console.error;
|
|
114
|
-
}
|
|
115
|
-
/**
|
|
116
|
-
* @param message {string} message
|
|
117
|
-
* @param args {...any=} arguments
|
|
118
|
-
*/
|
|
119
|
-
debug(message, ...args) {
|
|
120
|
-
this.log("debug", message, ...args);
|
|
121
|
-
return this;
|
|
122
|
-
}
|
|
123
|
-
/**
|
|
124
|
-
* @param message {string} message
|
|
125
|
-
* @param args {...any=} arguments
|
|
126
|
-
*/
|
|
127
|
-
info(message, ...args) {
|
|
128
|
-
this.log("info", message, ...args);
|
|
129
|
-
return this;
|
|
130
|
-
}
|
|
131
|
-
/**
|
|
132
|
-
* @param message {string} message
|
|
133
|
-
* @param args {...any=} arguments
|
|
134
|
-
*/
|
|
135
|
-
warn(message, ...args) {
|
|
136
|
-
this.log("warn", message, ...args);
|
|
137
|
-
return this;
|
|
138
|
-
}
|
|
139
|
-
/**
|
|
140
|
-
* @param message {any} message or Error object
|
|
141
|
-
* @param args {...any=} arguments
|
|
142
|
-
*/
|
|
143
|
-
error(message, ...args) {
|
|
144
|
-
let stack = false;
|
|
145
|
-
[message].concat(Array.from(args)).forEach((e) => {
|
|
146
|
-
if (e.stack) {
|
|
147
|
-
stack = true;
|
|
148
|
-
this.log("error", e.stack);
|
|
149
|
-
}
|
|
150
|
-
});
|
|
151
|
-
if (!stack)
|
|
152
|
-
this.log("error", message, ...args);
|
|
153
|
-
return this;
|
|
154
|
-
}
|
|
155
|
-
/**
|
|
156
|
-
* @param key {string} timer's label
|
|
157
|
-
* @param level [string=debug] 日志级别,支持 debug、info、warn、error
|
|
158
|
-
*/
|
|
159
|
-
time(key, level = "debug") {
|
|
160
|
-
this.cachedTimers[key] = {
|
|
161
|
-
level,
|
|
162
|
-
time: (/* @__PURE__ */ new Date()).getTime()
|
|
163
|
-
};
|
|
164
|
-
return this;
|
|
165
|
-
}
|
|
166
|
-
/**
|
|
167
|
-
* @param key {string} timer's label
|
|
168
|
-
* @param message {string} message
|
|
169
|
-
* @param args {...any=} arguments
|
|
170
|
-
*/
|
|
171
|
-
timeEnd(key, message, ...args) {
|
|
172
|
-
if (this.cachedTimers[key]) {
|
|
173
|
-
const timer = this.cachedTimers[key];
|
|
174
|
-
message = message + " +%ims";
|
|
175
|
-
args.push((/* @__PURE__ */ new Date()).getTime() - timer.time);
|
|
176
|
-
this[timer.level](message, ...args);
|
|
177
|
-
delete this.cachedTimers[key];
|
|
178
|
-
} else {
|
|
179
|
-
this.warn("timeEnd not found key %s", key);
|
|
180
|
-
this.debug(message);
|
|
181
|
-
}
|
|
182
|
-
return this;
|
|
183
|
-
}
|
|
184
|
-
/**
|
|
185
|
-
* @param message {string} message
|
|
186
|
-
* @param args {...any=} arguments
|
|
187
|
-
*/
|
|
188
|
-
raw(message, ...args) {
|
|
189
|
-
if (this.silent)
|
|
190
|
-
return this;
|
|
191
|
-
this.stdout((0, import_util.format)(message, ...args));
|
|
192
|
-
return this;
|
|
193
|
-
}
|
|
194
|
-
/**
|
|
195
|
-
* @param color {number} color code
|
|
196
|
-
* @param message {string} message
|
|
197
|
-
*/
|
|
198
|
-
colorfy(color, message) {
|
|
199
|
-
return `\x1B[0${color}m${message}\x1B[39m`;
|
|
200
|
-
}
|
|
201
|
-
log(level, message, ...args) {
|
|
202
|
-
if (this.silent)
|
|
203
|
-
return this;
|
|
204
|
-
if (LevelPriority[level] < this.level)
|
|
205
|
-
return this;
|
|
206
|
-
let output = level.toUpperCase() + " " + (this.label ? `[${this.label}] ` : "") + (0, import_util.format)(message, ...args);
|
|
207
|
-
if (this.colorfyOutput && level !== "error")
|
|
208
|
-
output = this.colorfy(LevelColor[level], output);
|
|
209
|
-
else if (!this.colorfyOutput)
|
|
210
|
-
output = output.replace(/\n/g, "");
|
|
211
|
-
if (!output)
|
|
212
|
-
return this;
|
|
213
|
-
if (output.length > this.size && !["error", "warn"].includes(level))
|
|
214
|
-
output = output.slice(0, this.size - 100) + "..." + output.slice(output.length - 100);
|
|
215
|
-
if (level === "error")
|
|
216
|
-
this.stderr(output);
|
|
217
|
-
else
|
|
218
|
-
this.stdout(output);
|
|
219
|
-
return this;
|
|
220
|
-
}
|
|
221
|
-
};
|
|
222
|
-
}
|
|
223
|
-
});
|
|
224
|
-
|
|
225
20
|
// src/index.ts
|
|
226
21
|
var src_exports = {};
|
|
227
22
|
__export(src_exports, {
|
|
@@ -230,7 +25,7 @@ __export(src_exports, {
|
|
|
230
25
|
usePlugin: () => usePlugin
|
|
231
26
|
});
|
|
232
27
|
module.exports = __toCommonJS(src_exports);
|
|
233
|
-
var import_logger =
|
|
28
|
+
var import_logger = require("@faasjs/logger");
|
|
234
29
|
|
|
235
30
|
// src/plugins/run_handler/index.ts
|
|
236
31
|
var RunHandler = class {
|
|
@@ -265,12 +60,6 @@ var RunHandler = class {
|
|
|
265
60
|
// src/index.ts
|
|
266
61
|
var import_crypto = require("crypto");
|
|
267
62
|
var Func = class {
|
|
268
|
-
/**
|
|
269
|
-
* Create a cloud function
|
|
270
|
-
* @param config {object} config
|
|
271
|
-
* @param config.plugins {Plugin[]} plugins list
|
|
272
|
-
* @param config.handler {Handler} business logic
|
|
273
|
-
*/
|
|
274
63
|
constructor(config) {
|
|
275
64
|
this.handler = config.handler;
|
|
276
65
|
this.plugins = config.plugins || [];
|
|
@@ -331,13 +120,6 @@ var Func = class {
|
|
|
331
120
|
return await dispatch(0);
|
|
332
121
|
};
|
|
333
122
|
}
|
|
334
|
-
/**
|
|
335
|
-
* Deploy the function
|
|
336
|
-
* @param data {object} data
|
|
337
|
-
* @param data.root {string} root path
|
|
338
|
-
* @param data.filename {string} filename
|
|
339
|
-
* @param data.env {string} environment
|
|
340
|
-
*/
|
|
341
123
|
deploy(data) {
|
|
342
124
|
if (!data.logger)
|
|
343
125
|
data.logger = new import_logger.Logger("Func");
|
|
@@ -345,9 +127,6 @@ var Func = class {
|
|
|
345
127
|
data.logger.debug("Plugins: " + this.plugins.map((p) => `${p.type}#${p.name}`).join(","));
|
|
346
128
|
return this.compose("onDeploy")(data);
|
|
347
129
|
}
|
|
348
|
-
/**
|
|
349
|
-
* First time mount the function
|
|
350
|
-
*/
|
|
351
130
|
async mount(data) {
|
|
352
131
|
if (!data.logger)
|
|
353
132
|
data.logger = new import_logger.Logger("Func");
|
|
@@ -367,10 +146,6 @@ var Func = class {
|
|
|
367
146
|
data.logger.timeEnd("mount", "mounted");
|
|
368
147
|
}
|
|
369
148
|
}
|
|
370
|
-
/**
|
|
371
|
-
* Invoke the function
|
|
372
|
-
* @param data {object} data
|
|
373
|
-
*/
|
|
374
149
|
async invoke(data) {
|
|
375
150
|
if (!this.mounted)
|
|
376
151
|
await this.mount({
|
|
@@ -386,9 +161,6 @@ var Func = class {
|
|
|
386
161
|
data.response = error;
|
|
387
162
|
}
|
|
388
163
|
}
|
|
389
|
-
/**
|
|
390
|
-
* Export the function
|
|
391
|
-
*/
|
|
392
164
|
export() {
|
|
393
165
|
const handler = async (event, context, callback) => {
|
|
394
166
|
if (typeof context === "undefined")
|
|
@@ -396,7 +168,7 @@ var Func = class {
|
|
|
396
168
|
if (!context.request_id)
|
|
397
169
|
context.request_id = (0, import_crypto.randomBytes)(16).toString("hex");
|
|
398
170
|
if (!context.request_at)
|
|
399
|
-
context.request_at = Math.round(
|
|
171
|
+
context.request_at = Math.round(new Date().getTime() / 1e3);
|
|
400
172
|
context.callbackWaitsForEmptyEventLoop = false;
|
|
401
173
|
const logger = new import_logger.Logger(context.request_id);
|
|
402
174
|
logger.debug("event: %j", event);
|
package/dist/index.mjs
CHANGED
|
@@ -1,230 +1,5 @@
|
|
|
1
|
-
var __create = Object.create;
|
|
2
|
-
var __defProp = Object.defineProperty;
|
|
3
|
-
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
-
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
-
var __getProtoOf = Object.getPrototypeOf;
|
|
6
|
-
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
7
|
-
var __require = /* @__PURE__ */ ((x) => typeof require !== "undefined" ? require : typeof Proxy !== "undefined" ? new Proxy(x, {
|
|
8
|
-
get: (a, b) => (typeof require !== "undefined" ? require : a)[b]
|
|
9
|
-
}) : x)(function(x) {
|
|
10
|
-
if (typeof require !== "undefined")
|
|
11
|
-
return require.apply(this, arguments);
|
|
12
|
-
throw new Error('Dynamic require of "' + x + '" is not supported');
|
|
13
|
-
});
|
|
14
|
-
var __commonJS = (cb, mod) => function __require2() {
|
|
15
|
-
return mod || (0, cb[__getOwnPropNames(cb)[0]])((mod = { exports: {} }).exports, mod), mod.exports;
|
|
16
|
-
};
|
|
17
|
-
var __copyProps = (to, from, except, desc) => {
|
|
18
|
-
if (from && typeof from === "object" || typeof from === "function") {
|
|
19
|
-
for (let key of __getOwnPropNames(from))
|
|
20
|
-
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
21
|
-
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
22
|
-
}
|
|
23
|
-
return to;
|
|
24
|
-
};
|
|
25
|
-
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
|
26
|
-
// If the importer is in node compatibility mode or this is not an ESM
|
|
27
|
-
// file that has been converted to a CommonJS file using a Babel-
|
|
28
|
-
// compatible transform (i.e. "__esModule" has not been set), then set
|
|
29
|
-
// "default" to the CommonJS "module.exports" for node compatibility.
|
|
30
|
-
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
|
|
31
|
-
mod
|
|
32
|
-
));
|
|
33
|
-
|
|
34
|
-
// ../logger/dist/index.js
|
|
35
|
-
var require_dist = __commonJS({
|
|
36
|
-
"../logger/dist/index.js"(exports, module) {
|
|
37
|
-
"use strict";
|
|
38
|
-
var __defProp2 = Object.defineProperty;
|
|
39
|
-
var __getOwnPropDesc2 = Object.getOwnPropertyDescriptor;
|
|
40
|
-
var __getOwnPropNames2 = Object.getOwnPropertyNames;
|
|
41
|
-
var __hasOwnProp2 = Object.prototype.hasOwnProperty;
|
|
42
|
-
var __export = (target, all) => {
|
|
43
|
-
for (var name in all)
|
|
44
|
-
__defProp2(target, name, { get: all[name], enumerable: true });
|
|
45
|
-
};
|
|
46
|
-
var __copyProps2 = (to, from, except, desc) => {
|
|
47
|
-
if (from && typeof from === "object" || typeof from === "function") {
|
|
48
|
-
for (let key of __getOwnPropNames2(from))
|
|
49
|
-
if (!__hasOwnProp2.call(to, key) && key !== except)
|
|
50
|
-
__defProp2(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc2(from, key)) || desc.enumerable });
|
|
51
|
-
}
|
|
52
|
-
return to;
|
|
53
|
-
};
|
|
54
|
-
var __toCommonJS = (mod) => __copyProps2(__defProp2({}, "__esModule", { value: true }), mod);
|
|
55
|
-
var src_exports = {};
|
|
56
|
-
__export(src_exports, {
|
|
57
|
-
Color: () => Color,
|
|
58
|
-
Logger: () => Logger2
|
|
59
|
-
});
|
|
60
|
-
module.exports = __toCommonJS(src_exports);
|
|
61
|
-
var import_util = __require("util");
|
|
62
|
-
var Color = /* @__PURE__ */ ((Color2) => {
|
|
63
|
-
Color2[Color2["DEFAULT"] = 39] = "DEFAULT";
|
|
64
|
-
Color2[Color2["BLACK"] = 30] = "BLACK";
|
|
65
|
-
Color2[Color2["RED"] = 31] = "RED";
|
|
66
|
-
Color2[Color2["GREEN"] = 32] = "GREEN";
|
|
67
|
-
Color2[Color2["ORANGE"] = 33] = "ORANGE";
|
|
68
|
-
Color2[Color2["BLUE"] = 34] = "BLUE";
|
|
69
|
-
Color2[Color2["MAGENTA"] = 35] = "MAGENTA";
|
|
70
|
-
Color2[Color2["CYAN"] = 36] = "CYAN";
|
|
71
|
-
Color2[Color2["GRAY"] = 90] = "GRAY";
|
|
72
|
-
return Color2;
|
|
73
|
-
})(Color || {});
|
|
74
|
-
var LevelColor = ((LevelColor2) => {
|
|
75
|
-
LevelColor2[
|
|
76
|
-
LevelColor2["debug"] = 90
|
|
77
|
-
/* GRAY */
|
|
78
|
-
] = "debug";
|
|
79
|
-
LevelColor2[
|
|
80
|
-
LevelColor2["info"] = 32
|
|
81
|
-
/* GREEN */
|
|
82
|
-
] = "info";
|
|
83
|
-
LevelColor2[
|
|
84
|
-
LevelColor2["warn"] = 33
|
|
85
|
-
/* ORANGE */
|
|
86
|
-
] = "warn";
|
|
87
|
-
LevelColor2[
|
|
88
|
-
LevelColor2["error"] = 31
|
|
89
|
-
/* RED */
|
|
90
|
-
] = "error";
|
|
91
|
-
return LevelColor2;
|
|
92
|
-
})(LevelColor || {});
|
|
93
|
-
var LevelPriority = {
|
|
94
|
-
debug: 0,
|
|
95
|
-
info: 1,
|
|
96
|
-
warn: 2,
|
|
97
|
-
error: 3
|
|
98
|
-
};
|
|
99
|
-
var Logger2 = class {
|
|
100
|
-
/**
|
|
101
|
-
* @param label {string} Prefix label
|
|
102
|
-
*/
|
|
103
|
-
constructor(label) {
|
|
104
|
-
this.colorfyOutput = true;
|
|
105
|
-
if (label)
|
|
106
|
-
this.label = label;
|
|
107
|
-
this.silent = !process.env.FaasLog && process.env.npm_config_argv && JSON.parse(process.env.npm_config_argv).original.includes("--silent");
|
|
108
|
-
if (["remote", "mono"].includes(process.env.FaasMode))
|
|
109
|
-
this.colorfyOutput = false;
|
|
110
|
-
this.level = process.env.FaasLog ? LevelPriority[process.env.FaasLog.toLowerCase()] : 0;
|
|
111
|
-
this.cachedTimers = {};
|
|
112
|
-
this.size = 1e3;
|
|
113
|
-
this.stdout = console.log;
|
|
114
|
-
this.stderr = console.error;
|
|
115
|
-
}
|
|
116
|
-
/**
|
|
117
|
-
* @param message {string} message
|
|
118
|
-
* @param args {...any=} arguments
|
|
119
|
-
*/
|
|
120
|
-
debug(message, ...args) {
|
|
121
|
-
this.log("debug", message, ...args);
|
|
122
|
-
return this;
|
|
123
|
-
}
|
|
124
|
-
/**
|
|
125
|
-
* @param message {string} message
|
|
126
|
-
* @param args {...any=} arguments
|
|
127
|
-
*/
|
|
128
|
-
info(message, ...args) {
|
|
129
|
-
this.log("info", message, ...args);
|
|
130
|
-
return this;
|
|
131
|
-
}
|
|
132
|
-
/**
|
|
133
|
-
* @param message {string} message
|
|
134
|
-
* @param args {...any=} arguments
|
|
135
|
-
*/
|
|
136
|
-
warn(message, ...args) {
|
|
137
|
-
this.log("warn", message, ...args);
|
|
138
|
-
return this;
|
|
139
|
-
}
|
|
140
|
-
/**
|
|
141
|
-
* @param message {any} message or Error object
|
|
142
|
-
* @param args {...any=} arguments
|
|
143
|
-
*/
|
|
144
|
-
error(message, ...args) {
|
|
145
|
-
let stack = false;
|
|
146
|
-
[message].concat(Array.from(args)).forEach((e) => {
|
|
147
|
-
if (e.stack) {
|
|
148
|
-
stack = true;
|
|
149
|
-
this.log("error", e.stack);
|
|
150
|
-
}
|
|
151
|
-
});
|
|
152
|
-
if (!stack)
|
|
153
|
-
this.log("error", message, ...args);
|
|
154
|
-
return this;
|
|
155
|
-
}
|
|
156
|
-
/**
|
|
157
|
-
* @param key {string} timer's label
|
|
158
|
-
* @param level [string=debug] 日志级别,支持 debug、info、warn、error
|
|
159
|
-
*/
|
|
160
|
-
time(key, level = "debug") {
|
|
161
|
-
this.cachedTimers[key] = {
|
|
162
|
-
level,
|
|
163
|
-
time: (/* @__PURE__ */ new Date()).getTime()
|
|
164
|
-
};
|
|
165
|
-
return this;
|
|
166
|
-
}
|
|
167
|
-
/**
|
|
168
|
-
* @param key {string} timer's label
|
|
169
|
-
* @param message {string} message
|
|
170
|
-
* @param args {...any=} arguments
|
|
171
|
-
*/
|
|
172
|
-
timeEnd(key, message, ...args) {
|
|
173
|
-
if (this.cachedTimers[key]) {
|
|
174
|
-
const timer = this.cachedTimers[key];
|
|
175
|
-
message = message + " +%ims";
|
|
176
|
-
args.push((/* @__PURE__ */ new Date()).getTime() - timer.time);
|
|
177
|
-
this[timer.level](message, ...args);
|
|
178
|
-
delete this.cachedTimers[key];
|
|
179
|
-
} else {
|
|
180
|
-
this.warn("timeEnd not found key %s", key);
|
|
181
|
-
this.debug(message);
|
|
182
|
-
}
|
|
183
|
-
return this;
|
|
184
|
-
}
|
|
185
|
-
/**
|
|
186
|
-
* @param message {string} message
|
|
187
|
-
* @param args {...any=} arguments
|
|
188
|
-
*/
|
|
189
|
-
raw(message, ...args) {
|
|
190
|
-
if (this.silent)
|
|
191
|
-
return this;
|
|
192
|
-
this.stdout((0, import_util.format)(message, ...args));
|
|
193
|
-
return this;
|
|
194
|
-
}
|
|
195
|
-
/**
|
|
196
|
-
* @param color {number} color code
|
|
197
|
-
* @param message {string} message
|
|
198
|
-
*/
|
|
199
|
-
colorfy(color, message) {
|
|
200
|
-
return `\x1B[0${color}m${message}\x1B[39m`;
|
|
201
|
-
}
|
|
202
|
-
log(level, message, ...args) {
|
|
203
|
-
if (this.silent)
|
|
204
|
-
return this;
|
|
205
|
-
if (LevelPriority[level] < this.level)
|
|
206
|
-
return this;
|
|
207
|
-
let output = level.toUpperCase() + " " + (this.label ? `[${this.label}] ` : "") + (0, import_util.format)(message, ...args);
|
|
208
|
-
if (this.colorfyOutput && level !== "error")
|
|
209
|
-
output = this.colorfy(LevelColor[level], output);
|
|
210
|
-
else if (!this.colorfyOutput)
|
|
211
|
-
output = output.replace(/\n/g, "");
|
|
212
|
-
if (!output)
|
|
213
|
-
return this;
|
|
214
|
-
if (output.length > this.size && !["error", "warn"].includes(level))
|
|
215
|
-
output = output.slice(0, this.size - 100) + "..." + output.slice(output.length - 100);
|
|
216
|
-
if (level === "error")
|
|
217
|
-
this.stderr(output);
|
|
218
|
-
else
|
|
219
|
-
this.stdout(output);
|
|
220
|
-
return this;
|
|
221
|
-
}
|
|
222
|
-
};
|
|
223
|
-
}
|
|
224
|
-
});
|
|
225
|
-
|
|
226
1
|
// src/index.ts
|
|
227
|
-
|
|
2
|
+
import { Logger } from "@faasjs/logger";
|
|
228
3
|
|
|
229
4
|
// src/plugins/run_handler/index.ts
|
|
230
5
|
var RunHandler = class {
|
|
@@ -259,12 +34,6 @@ var RunHandler = class {
|
|
|
259
34
|
// src/index.ts
|
|
260
35
|
import { randomBytes } from "crypto";
|
|
261
36
|
var Func = class {
|
|
262
|
-
/**
|
|
263
|
-
* Create a cloud function
|
|
264
|
-
* @param config {object} config
|
|
265
|
-
* @param config.plugins {Plugin[]} plugins list
|
|
266
|
-
* @param config.handler {Handler} business logic
|
|
267
|
-
*/
|
|
268
37
|
constructor(config) {
|
|
269
38
|
this.handler = config.handler;
|
|
270
39
|
this.plugins = config.plugins || [];
|
|
@@ -278,7 +47,7 @@ var Func = class {
|
|
|
278
47
|
try {
|
|
279
48
|
this.filename = new Error().stack.split("\n").find((s) => /[^/]\.func\.ts/.test(s)).match(/\((.*\.func\.ts).*\)/)[1];
|
|
280
49
|
} catch (error) {
|
|
281
|
-
new
|
|
50
|
+
new Logger("Func").warn(error.message);
|
|
282
51
|
}
|
|
283
52
|
}
|
|
284
53
|
compose(key) {
|
|
@@ -298,7 +67,7 @@ var Func = class {
|
|
|
298
67
|
}
|
|
299
68
|
return async function(data, next) {
|
|
300
69
|
let index = -1;
|
|
301
|
-
const logger = (data == null ? void 0 : data.logger) || new
|
|
70
|
+
const logger = (data == null ? void 0 : data.logger) || new Logger();
|
|
302
71
|
const dispatch = async function(i) {
|
|
303
72
|
if (i <= index)
|
|
304
73
|
return Promise.reject(Error("next() called multiple times"));
|
|
@@ -325,26 +94,16 @@ var Func = class {
|
|
|
325
94
|
return await dispatch(0);
|
|
326
95
|
};
|
|
327
96
|
}
|
|
328
|
-
/**
|
|
329
|
-
* Deploy the function
|
|
330
|
-
* @param data {object} data
|
|
331
|
-
* @param data.root {string} root path
|
|
332
|
-
* @param data.filename {string} filename
|
|
333
|
-
* @param data.env {string} environment
|
|
334
|
-
*/
|
|
335
97
|
deploy(data) {
|
|
336
98
|
if (!data.logger)
|
|
337
|
-
data.logger = new
|
|
99
|
+
data.logger = new Logger("Func");
|
|
338
100
|
data.logger.debug("onDeploy");
|
|
339
101
|
data.logger.debug("Plugins: " + this.plugins.map((p) => `${p.type}#${p.name}`).join(","));
|
|
340
102
|
return this.compose("onDeploy")(data);
|
|
341
103
|
}
|
|
342
|
-
/**
|
|
343
|
-
* First time mount the function
|
|
344
|
-
*/
|
|
345
104
|
async mount(data) {
|
|
346
105
|
if (!data.logger)
|
|
347
|
-
data.logger = new
|
|
106
|
+
data.logger = new Logger("Func");
|
|
348
107
|
data.logger.debug("onMount");
|
|
349
108
|
if (this.mounted) {
|
|
350
109
|
data.logger.warn("mount() has been called, skipped.");
|
|
@@ -361,10 +120,6 @@ var Func = class {
|
|
|
361
120
|
data.logger.timeEnd("mount", "mounted");
|
|
362
121
|
}
|
|
363
122
|
}
|
|
364
|
-
/**
|
|
365
|
-
* Invoke the function
|
|
366
|
-
* @param data {object} data
|
|
367
|
-
*/
|
|
368
123
|
async invoke(data) {
|
|
369
124
|
if (!this.mounted)
|
|
370
125
|
await this.mount({
|
|
@@ -380,9 +135,6 @@ var Func = class {
|
|
|
380
135
|
data.response = error;
|
|
381
136
|
}
|
|
382
137
|
}
|
|
383
|
-
/**
|
|
384
|
-
* Export the function
|
|
385
|
-
*/
|
|
386
138
|
export() {
|
|
387
139
|
const handler = async (event, context, callback) => {
|
|
388
140
|
if (typeof context === "undefined")
|
|
@@ -390,9 +142,9 @@ var Func = class {
|
|
|
390
142
|
if (!context.request_id)
|
|
391
143
|
context.request_id = randomBytes(16).toString("hex");
|
|
392
144
|
if (!context.request_at)
|
|
393
|
-
context.request_at = Math.round(
|
|
145
|
+
context.request_at = Math.round(new Date().getTime() / 1e3);
|
|
394
146
|
context.callbackWaitsForEmptyEventLoop = false;
|
|
395
|
-
const logger = new
|
|
147
|
+
const logger = new Logger(context.request_id);
|
|
396
148
|
logger.debug("event: %j", event);
|
|
397
149
|
logger.debug("context: %j", context);
|
|
398
150
|
const data = {
|
|
@@ -424,7 +176,7 @@ function usePlugin(plugin) {
|
|
|
424
176
|
config: (data == null ? void 0 : data.config) || /* @__PURE__ */ Object.create(null),
|
|
425
177
|
event: /* @__PURE__ */ Object.create(null),
|
|
426
178
|
context: /* @__PURE__ */ Object.create(null),
|
|
427
|
-
logger: new
|
|
179
|
+
logger: new Logger(plugin.name)
|
|
428
180
|
}, async () => Promise.resolve());
|
|
429
181
|
return plugin;
|
|
430
182
|
};
|
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.73",
|
|
4
4
|
"license": "MIT",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"types": "dist/index.d.ts",
|
|
@@ -21,8 +21,8 @@
|
|
|
21
21
|
"dist"
|
|
22
22
|
],
|
|
23
23
|
"dependencies": {
|
|
24
|
-
"@faasjs/deep_merge": "^0.0.3-beta.
|
|
25
|
-
"@faasjs/logger": "^0.0.3-beta.
|
|
24
|
+
"@faasjs/deep_merge": "^0.0.3-beta.73",
|
|
25
|
+
"@faasjs/logger": "^0.0.3-beta.73"
|
|
26
26
|
},
|
|
27
27
|
"engines": {
|
|
28
28
|
"npm": ">=8.0.0",
|