@faasjs/func 0.0.3-beta.7 → 0.0.3-beta.70
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/README.md +8 -8
- package/dist/index.d.ts +5 -5
- package/dist/index.js +235 -6
- package/dist/index.mjs +261 -12
- package/package.json +4 -5
package/README.md
CHANGED
|
@@ -261,13 +261,13 @@ ___
|
|
|
261
261
|
|
|
262
262
|
### UseifyPlugin
|
|
263
263
|
|
|
264
|
-
Ƭ **UseifyPlugin
|
|
264
|
+
Ƭ **UseifyPlugin**<`T`\>: `T` & { `mount?`: (`data?`: { `config?`: [`Config`](#config) }) => `Promise`<`T`\> }
|
|
265
265
|
|
|
266
|
-
#### Type
|
|
266
|
+
#### Type parameters
|
|
267
267
|
|
|
268
|
-
| Name |
|
|
269
|
-
| :------ |
|
|
270
|
-
| `
|
|
268
|
+
| Name |
|
|
269
|
+
| :------ |
|
|
270
|
+
| `T` |
|
|
271
271
|
|
|
272
272
|
## Functions
|
|
273
273
|
|
|
@@ -317,7 +317,7 @@ ___
|
|
|
317
317
|
|
|
318
318
|
### usePlugin
|
|
319
319
|
|
|
320
|
-
▸ **usePlugin**<`T`\>(`plugin`):
|
|
320
|
+
▸ **usePlugin**<`T`\>(`plugin`): [`UseifyPlugin`](#useifyplugin)<`T`\>
|
|
321
321
|
|
|
322
322
|
#### Type parameters
|
|
323
323
|
|
|
@@ -329,8 +329,8 @@ ___
|
|
|
329
329
|
|
|
330
330
|
| Name | Type |
|
|
331
331
|
| :------ | :------ |
|
|
332
|
-
| `plugin` |
|
|
332
|
+
| `plugin` | [`UseifyPlugin`](#useifyplugin)<`T`\> |
|
|
333
333
|
|
|
334
334
|
#### Returns
|
|
335
335
|
|
|
336
|
-
|
|
336
|
+
[`UseifyPlugin`](#useifyplugin)<`T`\>
|
package/dist/index.d.ts
CHANGED
|
@@ -124,12 +124,12 @@ declare class Func<TEvent = any, TContext = any, TResult = any> {
|
|
|
124
124
|
handler: ExportedHandler<TEvent, TContext, TResult>;
|
|
125
125
|
};
|
|
126
126
|
}
|
|
127
|
-
type UseifyPlugin = {
|
|
128
|
-
mount?: (data
|
|
129
|
-
config
|
|
130
|
-
}) => Promise<
|
|
127
|
+
type UseifyPlugin<T> = T & {
|
|
128
|
+
mount?: (data?: {
|
|
129
|
+
config?: Config;
|
|
130
|
+
}) => Promise<T>;
|
|
131
131
|
};
|
|
132
|
-
declare function usePlugin<T extends Plugin>(plugin: T
|
|
132
|
+
declare function usePlugin<T extends Plugin>(plugin: UseifyPlugin<T>): UseifyPlugin<T>;
|
|
133
133
|
/**
|
|
134
134
|
* ```ts
|
|
135
135
|
* // pure function
|
package/dist/index.js
CHANGED
|
@@ -1,8 +1,13 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
+
var __create = Object.create;
|
|
2
3
|
var __defProp = Object.defineProperty;
|
|
3
4
|
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
5
|
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
6
|
+
var __getProtoOf = Object.getPrototypeOf;
|
|
5
7
|
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
|
+
};
|
|
6
11
|
var __export = (target, all) => {
|
|
7
12
|
for (var name in all)
|
|
8
13
|
__defProp(target, name, { get: all[name], enumerable: true });
|
|
@@ -15,8 +20,208 @@ var __copyProps = (to, from, except, desc) => {
|
|
|
15
20
|
}
|
|
16
21
|
return to;
|
|
17
22
|
};
|
|
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
|
+
));
|
|
18
31
|
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
32
|
|
|
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
|
+
|
|
20
225
|
// src/index.ts
|
|
21
226
|
var src_exports = {};
|
|
22
227
|
__export(src_exports, {
|
|
@@ -25,7 +230,7 @@ __export(src_exports, {
|
|
|
25
230
|
usePlugin: () => usePlugin
|
|
26
231
|
});
|
|
27
232
|
module.exports = __toCommonJS(src_exports);
|
|
28
|
-
var import_logger =
|
|
233
|
+
var import_logger = __toESM(require_dist());
|
|
29
234
|
|
|
30
235
|
// src/plugins/run_handler/index.ts
|
|
31
236
|
var RunHandler = class {
|
|
@@ -60,6 +265,12 @@ var RunHandler = class {
|
|
|
60
265
|
// src/index.ts
|
|
61
266
|
var import_crypto = require("crypto");
|
|
62
267
|
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
|
+
*/
|
|
63
274
|
constructor(config) {
|
|
64
275
|
this.handler = config.handler;
|
|
65
276
|
this.plugins = config.plugins || [];
|
|
@@ -120,6 +331,13 @@ var Func = class {
|
|
|
120
331
|
return await dispatch(0);
|
|
121
332
|
};
|
|
122
333
|
}
|
|
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
|
+
*/
|
|
123
341
|
deploy(data) {
|
|
124
342
|
if (!data.logger)
|
|
125
343
|
data.logger = new import_logger.Logger("Func");
|
|
@@ -127,6 +345,9 @@ var Func = class {
|
|
|
127
345
|
data.logger.debug("Plugins: " + this.plugins.map((p) => `${p.type}#${p.name}`).join(","));
|
|
128
346
|
return this.compose("onDeploy")(data);
|
|
129
347
|
}
|
|
348
|
+
/**
|
|
349
|
+
* First time mount the function
|
|
350
|
+
*/
|
|
130
351
|
async mount(data) {
|
|
131
352
|
if (!data.logger)
|
|
132
353
|
data.logger = new import_logger.Logger("Func");
|
|
@@ -146,6 +367,10 @@ var Func = class {
|
|
|
146
367
|
data.logger.timeEnd("mount", "mounted");
|
|
147
368
|
}
|
|
148
369
|
}
|
|
370
|
+
/**
|
|
371
|
+
* Invoke the function
|
|
372
|
+
* @param data {object} data
|
|
373
|
+
*/
|
|
149
374
|
async invoke(data) {
|
|
150
375
|
if (!this.mounted)
|
|
151
376
|
await this.mount({
|
|
@@ -161,6 +386,9 @@ var Func = class {
|
|
|
161
386
|
data.response = error;
|
|
162
387
|
}
|
|
163
388
|
}
|
|
389
|
+
/**
|
|
390
|
+
* Export the function
|
|
391
|
+
*/
|
|
164
392
|
export() {
|
|
165
393
|
const handler = async (event, context, callback) => {
|
|
166
394
|
if (typeof context === "undefined")
|
|
@@ -168,7 +396,7 @@ var Func = class {
|
|
|
168
396
|
if (!context.request_id)
|
|
169
397
|
context.request_id = (0, import_crypto.randomBytes)(16).toString("hex");
|
|
170
398
|
if (!context.request_at)
|
|
171
|
-
context.request_at = Math.round(new Date().getTime() / 1e3);
|
|
399
|
+
context.request_at = Math.round((/* @__PURE__ */ new Date()).getTime() / 1e3);
|
|
172
400
|
context.callbackWaitsForEmptyEventLoop = false;
|
|
173
401
|
const logger = new import_logger.Logger(context.request_id);
|
|
174
402
|
logger.debug("event: %j", event);
|
|
@@ -196,14 +424,15 @@ function usePlugin(plugin) {
|
|
|
196
424
|
if (!plugins.find((p) => p.name === plugin.name))
|
|
197
425
|
plugins.push(plugin);
|
|
198
426
|
if (!plugin.mount)
|
|
199
|
-
plugin.mount = async function(
|
|
427
|
+
plugin.mount = async function(data) {
|
|
200
428
|
if (plugin.onMount)
|
|
201
429
|
await plugin.onMount({
|
|
202
|
-
config,
|
|
203
|
-
event:
|
|
204
|
-
context:
|
|
430
|
+
config: (data == null ? void 0 : data.config) || /* @__PURE__ */ Object.create(null),
|
|
431
|
+
event: /* @__PURE__ */ Object.create(null),
|
|
432
|
+
context: /* @__PURE__ */ Object.create(null),
|
|
205
433
|
logger: new import_logger.Logger(plugin.name)
|
|
206
434
|
}, async () => Promise.resolve());
|
|
435
|
+
return plugin;
|
|
207
436
|
};
|
|
208
437
|
return plugin;
|
|
209
438
|
}
|
package/dist/index.mjs
CHANGED
|
@@ -1,5 +1,230 @@
|
|
|
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
|
+
|
|
1
226
|
// src/index.ts
|
|
2
|
-
|
|
227
|
+
var import_logger = __toESM(require_dist());
|
|
3
228
|
|
|
4
229
|
// src/plugins/run_handler/index.ts
|
|
5
230
|
var RunHandler = class {
|
|
@@ -34,6 +259,12 @@ var RunHandler = class {
|
|
|
34
259
|
// src/index.ts
|
|
35
260
|
import { randomBytes } from "crypto";
|
|
36
261
|
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
|
+
*/
|
|
37
268
|
constructor(config) {
|
|
38
269
|
this.handler = config.handler;
|
|
39
270
|
this.plugins = config.plugins || [];
|
|
@@ -47,7 +278,7 @@ var Func = class {
|
|
|
47
278
|
try {
|
|
48
279
|
this.filename = new Error().stack.split("\n").find((s) => /[^/]\.func\.ts/.test(s)).match(/\((.*\.func\.ts).*\)/)[1];
|
|
49
280
|
} catch (error) {
|
|
50
|
-
new Logger("Func").warn(error.message);
|
|
281
|
+
new import_logger.Logger("Func").warn(error.message);
|
|
51
282
|
}
|
|
52
283
|
}
|
|
53
284
|
compose(key) {
|
|
@@ -67,7 +298,7 @@ var Func = class {
|
|
|
67
298
|
}
|
|
68
299
|
return async function(data, next) {
|
|
69
300
|
let index = -1;
|
|
70
|
-
const logger = (data == null ? void 0 : data.logger) || new Logger();
|
|
301
|
+
const logger = (data == null ? void 0 : data.logger) || new import_logger.Logger();
|
|
71
302
|
const dispatch = async function(i) {
|
|
72
303
|
if (i <= index)
|
|
73
304
|
return Promise.reject(Error("next() called multiple times"));
|
|
@@ -94,16 +325,26 @@ var Func = class {
|
|
|
94
325
|
return await dispatch(0);
|
|
95
326
|
};
|
|
96
327
|
}
|
|
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
|
+
*/
|
|
97
335
|
deploy(data) {
|
|
98
336
|
if (!data.logger)
|
|
99
|
-
data.logger = new Logger("Func");
|
|
337
|
+
data.logger = new import_logger.Logger("Func");
|
|
100
338
|
data.logger.debug("onDeploy");
|
|
101
339
|
data.logger.debug("Plugins: " + this.plugins.map((p) => `${p.type}#${p.name}`).join(","));
|
|
102
340
|
return this.compose("onDeploy")(data);
|
|
103
341
|
}
|
|
342
|
+
/**
|
|
343
|
+
* First time mount the function
|
|
344
|
+
*/
|
|
104
345
|
async mount(data) {
|
|
105
346
|
if (!data.logger)
|
|
106
|
-
data.logger = new Logger("Func");
|
|
347
|
+
data.logger = new import_logger.Logger("Func");
|
|
107
348
|
data.logger.debug("onMount");
|
|
108
349
|
if (this.mounted) {
|
|
109
350
|
data.logger.warn("mount() has been called, skipped.");
|
|
@@ -120,6 +361,10 @@ var Func = class {
|
|
|
120
361
|
data.logger.timeEnd("mount", "mounted");
|
|
121
362
|
}
|
|
122
363
|
}
|
|
364
|
+
/**
|
|
365
|
+
* Invoke the function
|
|
366
|
+
* @param data {object} data
|
|
367
|
+
*/
|
|
123
368
|
async invoke(data) {
|
|
124
369
|
if (!this.mounted)
|
|
125
370
|
await this.mount({
|
|
@@ -135,6 +380,9 @@ var Func = class {
|
|
|
135
380
|
data.response = error;
|
|
136
381
|
}
|
|
137
382
|
}
|
|
383
|
+
/**
|
|
384
|
+
* Export the function
|
|
385
|
+
*/
|
|
138
386
|
export() {
|
|
139
387
|
const handler = async (event, context, callback) => {
|
|
140
388
|
if (typeof context === "undefined")
|
|
@@ -142,9 +390,9 @@ var Func = class {
|
|
|
142
390
|
if (!context.request_id)
|
|
143
391
|
context.request_id = randomBytes(16).toString("hex");
|
|
144
392
|
if (!context.request_at)
|
|
145
|
-
context.request_at = Math.round(new Date().getTime() / 1e3);
|
|
393
|
+
context.request_at = Math.round((/* @__PURE__ */ new Date()).getTime() / 1e3);
|
|
146
394
|
context.callbackWaitsForEmptyEventLoop = false;
|
|
147
|
-
const logger = new Logger(context.request_id);
|
|
395
|
+
const logger = new import_logger.Logger(context.request_id);
|
|
148
396
|
logger.debug("event: %j", event);
|
|
149
397
|
logger.debug("context: %j", context);
|
|
150
398
|
const data = {
|
|
@@ -170,14 +418,15 @@ function usePlugin(plugin) {
|
|
|
170
418
|
if (!plugins.find((p) => p.name === plugin.name))
|
|
171
419
|
plugins.push(plugin);
|
|
172
420
|
if (!plugin.mount)
|
|
173
|
-
plugin.mount = async function(
|
|
421
|
+
plugin.mount = async function(data) {
|
|
174
422
|
if (plugin.onMount)
|
|
175
423
|
await plugin.onMount({
|
|
176
|
-
config,
|
|
177
|
-
event:
|
|
178
|
-
context:
|
|
179
|
-
logger: new Logger(plugin.name)
|
|
424
|
+
config: (data == null ? void 0 : data.config) || /* @__PURE__ */ Object.create(null),
|
|
425
|
+
event: /* @__PURE__ */ Object.create(null),
|
|
426
|
+
context: /* @__PURE__ */ Object.create(null),
|
|
427
|
+
logger: new import_logger.Logger(plugin.name)
|
|
180
428
|
}, async () => Promise.resolve());
|
|
429
|
+
return plugin;
|
|
181
430
|
};
|
|
182
431
|
return plugin;
|
|
183
432
|
}
|
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.70",
|
|
4
4
|
"license": "MIT",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"types": "dist/index.d.ts",
|
|
@@ -15,15 +15,14 @@
|
|
|
15
15
|
},
|
|
16
16
|
"funding": "https://github.com/sponsors/faasjs",
|
|
17
17
|
"scripts": {
|
|
18
|
-
"build": "tsup-node src/index.ts --format esm,cjs"
|
|
19
|
-
"build:types": "tsup-node src/index.ts --dts-only"
|
|
18
|
+
"build": "tsup-node src/index.ts --format esm,cjs --dts --clean"
|
|
20
19
|
},
|
|
21
20
|
"files": [
|
|
22
21
|
"dist"
|
|
23
22
|
],
|
|
24
23
|
"dependencies": {
|
|
25
|
-
"@faasjs/deep_merge": "^0.0.3-beta.
|
|
26
|
-
"@faasjs/logger": "^0.0.3-beta.
|
|
24
|
+
"@faasjs/deep_merge": "^0.0.3-beta.70",
|
|
25
|
+
"@faasjs/logger": "^0.0.3-beta.70"
|
|
27
26
|
},
|
|
28
27
|
"engines": {
|
|
29
28
|
"npm": ">=8.0.0",
|