@faasjs/func 0.0.2-beta.99 → 0.0.3-beta.2
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 +331 -4
- package/dist/index.d.ts +156 -0
- package/dist/index.js +225 -0
- package/dist/index.mjs +198 -0
- package/package.json +22 -19
- package/lib/index.d.ts +0 -130
- package/lib/index.es.js +0 -212
- package/lib/index.js +0 -220
- package/lib/plugins/run_handler/index.d.ts +0 -6
package/dist/index.mjs
ADDED
|
@@ -0,0 +1,198 @@
|
|
|
1
|
+
// src/index.ts
|
|
2
|
+
import { Logger } from "@faasjs/logger";
|
|
3
|
+
|
|
4
|
+
// src/plugins/run_handler/index.ts
|
|
5
|
+
var RunHandler = class {
|
|
6
|
+
constructor() {
|
|
7
|
+
this.type = "handler";
|
|
8
|
+
this.name = "handler";
|
|
9
|
+
}
|
|
10
|
+
async onInvoke(data, next) {
|
|
11
|
+
if (data.handler)
|
|
12
|
+
if (!data.runHandler) {
|
|
13
|
+
try {
|
|
14
|
+
data.response = await new Promise(function(resolve, reject) {
|
|
15
|
+
data.callback = function(error, result) {
|
|
16
|
+
if (error)
|
|
17
|
+
reject(error);
|
|
18
|
+
else
|
|
19
|
+
resolve(result);
|
|
20
|
+
};
|
|
21
|
+
Promise.resolve(data.handler(data)).then(resolve).catch(reject);
|
|
22
|
+
});
|
|
23
|
+
} catch (error) {
|
|
24
|
+
data.logger.error(error);
|
|
25
|
+
data.response = error;
|
|
26
|
+
}
|
|
27
|
+
data.runHandler = true;
|
|
28
|
+
} else
|
|
29
|
+
data.logger.warn("[RunHandler] handler has been run");
|
|
30
|
+
await next();
|
|
31
|
+
}
|
|
32
|
+
};
|
|
33
|
+
|
|
34
|
+
// src/index.ts
|
|
35
|
+
import { randomBytes } from "crypto";
|
|
36
|
+
var Func = class {
|
|
37
|
+
constructor(config) {
|
|
38
|
+
this.handler = config.handler;
|
|
39
|
+
this.plugins = config.plugins || [];
|
|
40
|
+
this.plugins.push(new RunHandler());
|
|
41
|
+
this.config = {
|
|
42
|
+
providers: /* @__PURE__ */ Object.create(null),
|
|
43
|
+
plugins: /* @__PURE__ */ Object.create(null)
|
|
44
|
+
};
|
|
45
|
+
this.mounted = false;
|
|
46
|
+
this.cachedFunctions = /* @__PURE__ */ Object.create(null);
|
|
47
|
+
try {
|
|
48
|
+
this.filename = new Error().stack.split("\n").find((s) => /[^/]\.func\.ts/.test(s)).match(/\((.*\.func\.ts).*\)/)[1];
|
|
49
|
+
} catch (error) {
|
|
50
|
+
new Logger("Func").warn(error.message);
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
compose(key) {
|
|
54
|
+
let list = [];
|
|
55
|
+
if (this.cachedFunctions[key])
|
|
56
|
+
list = this.cachedFunctions[key];
|
|
57
|
+
else {
|
|
58
|
+
for (const plugin of this.plugins) {
|
|
59
|
+
const handler = plugin[key];
|
|
60
|
+
if (typeof handler === "function")
|
|
61
|
+
list.push({
|
|
62
|
+
key: plugin.name,
|
|
63
|
+
handler: handler.bind(plugin)
|
|
64
|
+
});
|
|
65
|
+
}
|
|
66
|
+
this.cachedFunctions[key] = list;
|
|
67
|
+
}
|
|
68
|
+
return async function(data, next) {
|
|
69
|
+
let index = -1;
|
|
70
|
+
const logger = (data == null ? void 0 : data.logger) || new Logger();
|
|
71
|
+
const dispatch = async function(i) {
|
|
72
|
+
if (i <= index)
|
|
73
|
+
return Promise.reject(Error("next() called multiple times"));
|
|
74
|
+
index = i;
|
|
75
|
+
let fn = list[i];
|
|
76
|
+
if (i === list.length)
|
|
77
|
+
fn = next;
|
|
78
|
+
if (!fn)
|
|
79
|
+
return Promise.resolve();
|
|
80
|
+
if (typeof fn.key === "undefined")
|
|
81
|
+
fn.key = `UnNamedPlugin#${i}`;
|
|
82
|
+
logger.debug("[%s][%s] begin", fn.key, key);
|
|
83
|
+
logger.time(fn.key);
|
|
84
|
+
try {
|
|
85
|
+
const res = await Promise.resolve(fn.handler(data, dispatch.bind(null, i + 1)));
|
|
86
|
+
logger.timeEnd(fn.key, "[%s][%s] end", fn.key, key);
|
|
87
|
+
return res;
|
|
88
|
+
} catch (err) {
|
|
89
|
+
logger.timeEnd(fn.key, "[%s][%s] failed", fn.key, key);
|
|
90
|
+
logger.error(err);
|
|
91
|
+
return Promise.reject(err);
|
|
92
|
+
}
|
|
93
|
+
};
|
|
94
|
+
return await dispatch(0);
|
|
95
|
+
};
|
|
96
|
+
}
|
|
97
|
+
deploy(data) {
|
|
98
|
+
if (!data.logger)
|
|
99
|
+
data.logger = new Logger("Func");
|
|
100
|
+
data.logger.debug("onDeploy");
|
|
101
|
+
data.logger.debug("Plugins: " + this.plugins.map((p) => `${p.type}#${p.name}`).join(","));
|
|
102
|
+
return this.compose("onDeploy")(data);
|
|
103
|
+
}
|
|
104
|
+
async mount(data) {
|
|
105
|
+
if (!data.logger)
|
|
106
|
+
data.logger = new Logger("Func");
|
|
107
|
+
data.logger.debug("onMount");
|
|
108
|
+
if (this.mounted) {
|
|
109
|
+
data.logger.warn("mount() has been called, skipped.");
|
|
110
|
+
return;
|
|
111
|
+
}
|
|
112
|
+
if (!data.config)
|
|
113
|
+
data.config = this.config;
|
|
114
|
+
try {
|
|
115
|
+
data.logger.time("mount");
|
|
116
|
+
data.logger.debug("Plugins: " + this.plugins.map((p) => `${p.type}#${p.name}`).join(","));
|
|
117
|
+
await this.compose("onMount")(data);
|
|
118
|
+
this.mounted = true;
|
|
119
|
+
} finally {
|
|
120
|
+
data.logger.timeEnd("mount", "mounted");
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
async invoke(data) {
|
|
124
|
+
if (!this.mounted)
|
|
125
|
+
await this.mount({
|
|
126
|
+
event: data.event,
|
|
127
|
+
context: data.context,
|
|
128
|
+
config: data.config,
|
|
129
|
+
logger: data.logger
|
|
130
|
+
});
|
|
131
|
+
try {
|
|
132
|
+
await this.compose("onInvoke")(data);
|
|
133
|
+
} catch (error) {
|
|
134
|
+
data.logger.error(error);
|
|
135
|
+
data.response = error;
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
export() {
|
|
139
|
+
const handler = async (event, context, callback) => {
|
|
140
|
+
if (typeof context === "undefined")
|
|
141
|
+
context = {};
|
|
142
|
+
if (!context.request_id)
|
|
143
|
+
context.request_id = randomBytes(16).toString("hex");
|
|
144
|
+
if (!context.request_at)
|
|
145
|
+
context.request_at = Math.round(new Date().getTime() / 1e3);
|
|
146
|
+
context.callbackWaitsForEmptyEventLoop = false;
|
|
147
|
+
const logger = new Logger(context.request_id);
|
|
148
|
+
logger.debug("event: %j", event);
|
|
149
|
+
logger.debug("context: %j", context);
|
|
150
|
+
const data = {
|
|
151
|
+
event,
|
|
152
|
+
context,
|
|
153
|
+
callback,
|
|
154
|
+
response: void 0,
|
|
155
|
+
handler: this.handler,
|
|
156
|
+
logger,
|
|
157
|
+
config: this.config
|
|
158
|
+
};
|
|
159
|
+
await this.invoke(data);
|
|
160
|
+
if (Object.prototype.toString.call(data.response) === "[object Error]")
|
|
161
|
+
throw data.response;
|
|
162
|
+
return data.response;
|
|
163
|
+
};
|
|
164
|
+
handler.bind(this);
|
|
165
|
+
return { handler };
|
|
166
|
+
}
|
|
167
|
+
};
|
|
168
|
+
var plugins = [];
|
|
169
|
+
function usePlugin(plugin) {
|
|
170
|
+
if (!plugins.find((p) => p.name === plugin.name))
|
|
171
|
+
plugins.push(plugin);
|
|
172
|
+
if (!plugin.mount)
|
|
173
|
+
plugin.mount = async function({ config }) {
|
|
174
|
+
if (plugin.onMount)
|
|
175
|
+
await plugin.onMount({
|
|
176
|
+
config,
|
|
177
|
+
event: {},
|
|
178
|
+
context: {},
|
|
179
|
+
logger: new Logger(plugin.name)
|
|
180
|
+
}, async () => Promise.resolve());
|
|
181
|
+
};
|
|
182
|
+
return plugin;
|
|
183
|
+
}
|
|
184
|
+
function useFunc(handler) {
|
|
185
|
+
plugins = [];
|
|
186
|
+
const invokeHandler = handler();
|
|
187
|
+
const func = new Func({
|
|
188
|
+
plugins,
|
|
189
|
+
handler: invokeHandler
|
|
190
|
+
});
|
|
191
|
+
plugins = [];
|
|
192
|
+
return func;
|
|
193
|
+
}
|
|
194
|
+
export {
|
|
195
|
+
Func,
|
|
196
|
+
useFunc,
|
|
197
|
+
usePlugin
|
|
198
|
+
};
|
package/package.json
CHANGED
|
@@ -1,29 +1,32 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@faasjs/func",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.3-beta.2",
|
|
4
4
|
"license": "MIT",
|
|
5
|
-
"main": "
|
|
6
|
-
"types": "
|
|
7
|
-
"
|
|
5
|
+
"main": "dist/index.js",
|
|
6
|
+
"types": "dist/index.d.ts",
|
|
7
|
+
"homepage": "https://faasjs.com/docs/func",
|
|
8
|
+
"repository": {
|
|
9
|
+
"type": "git",
|
|
10
|
+
"url": "git+https://github.com/faasjs/faasjs.git",
|
|
11
|
+
"directory": "packages/func"
|
|
12
|
+
},
|
|
13
|
+
"bugs": {
|
|
14
|
+
"url": "https://github.com/faasjs/faasjs/issues"
|
|
15
|
+
},
|
|
16
|
+
"funding": "https://github.com/sponsors/faasjs",
|
|
8
17
|
"scripts": {
|
|
9
|
-
"
|
|
10
|
-
"
|
|
11
|
-
"ci": "yarn lint && jest --silent"
|
|
18
|
+
"build": "tsup-node src/index.ts --format esm,cjs",
|
|
19
|
+
"build:types": "tsup-node src/index.ts --dts-only"
|
|
12
20
|
},
|
|
13
21
|
"files": [
|
|
14
|
-
"
|
|
22
|
+
"dist"
|
|
15
23
|
],
|
|
16
24
|
"dependencies": {
|
|
17
|
-
"@faasjs/deep_merge": "^0.0.
|
|
18
|
-
"@faasjs/logger": "^0.0.
|
|
19
|
-
},
|
|
20
|
-
"devDependencies": {
|
|
21
|
-
"@types/debug": "*",
|
|
22
|
-
"@types/jest": "*",
|
|
23
|
-
"@types/node": "*",
|
|
24
|
-
"rollup": "*",
|
|
25
|
-
"rollup-plugin-typescript2": "*",
|
|
26
|
-
"typescript": "*"
|
|
25
|
+
"@faasjs/deep_merge": "^0.0.3-beta.2",
|
|
26
|
+
"@faasjs/logger": "^0.0.3-beta.2"
|
|
27
27
|
},
|
|
28
|
-
"
|
|
28
|
+
"engines": {
|
|
29
|
+
"npm": ">=8.0.0",
|
|
30
|
+
"node": ">=16.0.0"
|
|
31
|
+
}
|
|
29
32
|
}
|
package/lib/index.d.ts
DELETED
|
@@ -1,130 +0,0 @@
|
|
|
1
|
-
import Logger from '@faasjs/logger';
|
|
2
|
-
export declare type Handler<TEvent = any, TContext = any, RESULT = any> = (data: InvokeData<TEvent, TContext>) => RESULT;
|
|
3
|
-
export declare type Next = () => Promise<void>;
|
|
4
|
-
export declare type ExportedHandler<TEvent = any, TContext = any, RESULT = any> = (event: TEvent, context?: TContext, callback?: (...args: any) => any) => Promise<RESULT>;
|
|
5
|
-
export interface Plugin {
|
|
6
|
-
[key: string]: any;
|
|
7
|
-
readonly type: string;
|
|
8
|
-
readonly name: string;
|
|
9
|
-
onDeploy?: (data: DeployData, next: Next) => void;
|
|
10
|
-
onMount?: (data: MountData, next: Next) => void;
|
|
11
|
-
onInvoke?: (data: InvokeData, next: Next) => void;
|
|
12
|
-
}
|
|
13
|
-
export interface Config {
|
|
14
|
-
[key: string]: any;
|
|
15
|
-
providers: {
|
|
16
|
-
[key: string]: {
|
|
17
|
-
type: string;
|
|
18
|
-
config: {
|
|
19
|
-
[key: string]: any;
|
|
20
|
-
};
|
|
21
|
-
};
|
|
22
|
-
};
|
|
23
|
-
plugins: {
|
|
24
|
-
[key: string]: {
|
|
25
|
-
[key: string]: any;
|
|
26
|
-
provider?: string;
|
|
27
|
-
type: string;
|
|
28
|
-
config?: {
|
|
29
|
-
[key: string]: any;
|
|
30
|
-
};
|
|
31
|
-
};
|
|
32
|
-
};
|
|
33
|
-
}
|
|
34
|
-
export interface DeployData {
|
|
35
|
-
[key: string]: any;
|
|
36
|
-
root: string;
|
|
37
|
-
filename: string;
|
|
38
|
-
env?: string;
|
|
39
|
-
name?: string;
|
|
40
|
-
config?: Config;
|
|
41
|
-
version?: string;
|
|
42
|
-
dependencies?: {
|
|
43
|
-
[name: string]: string;
|
|
44
|
-
};
|
|
45
|
-
plugins?: {
|
|
46
|
-
[name: string]: {
|
|
47
|
-
[key: string]: any;
|
|
48
|
-
name?: string;
|
|
49
|
-
type: string;
|
|
50
|
-
provider?: string;
|
|
51
|
-
config: {
|
|
52
|
-
[key: string]: any;
|
|
53
|
-
};
|
|
54
|
-
plugin: Plugin;
|
|
55
|
-
};
|
|
56
|
-
};
|
|
57
|
-
logger?: Logger;
|
|
58
|
-
}
|
|
59
|
-
export interface MountData {
|
|
60
|
-
[key: string]: any;
|
|
61
|
-
config: Config;
|
|
62
|
-
event: any;
|
|
63
|
-
context: any;
|
|
64
|
-
}
|
|
65
|
-
export interface InvokeData<TEvent = any, TContext = any, RESULT = any> {
|
|
66
|
-
[key: string]: any;
|
|
67
|
-
event: TEvent;
|
|
68
|
-
context: TContext;
|
|
69
|
-
callback: any;
|
|
70
|
-
response: any;
|
|
71
|
-
logger: Logger;
|
|
72
|
-
handler: Handler<TEvent, TContext, RESULT>;
|
|
73
|
-
config: Config;
|
|
74
|
-
}
|
|
75
|
-
export declare type LifeCycleKey = 'onDeploy' | 'onMount' | 'onInvoke';
|
|
76
|
-
export interface FuncConfig<TEvent = any, TContext = any, RESULT = any> {
|
|
77
|
-
plugins?: Plugin[];
|
|
78
|
-
handler?: Handler<TEvent, TContext, RESULT>;
|
|
79
|
-
}
|
|
80
|
-
export declare class Func<TEvent = any, TContext = any, RESULT = any> {
|
|
81
|
-
[key: string]: any;
|
|
82
|
-
plugins: Plugin[];
|
|
83
|
-
handler?: Handler<TEvent, TContext, RESULT>;
|
|
84
|
-
logger: Logger;
|
|
85
|
-
config: Config;
|
|
86
|
-
mounted: boolean;
|
|
87
|
-
private cachedFunctions;
|
|
88
|
-
/**
|
|
89
|
-
* 新建流程
|
|
90
|
-
* @param config {object} 配置项
|
|
91
|
-
* @param config.plugins {Plugin[]} 插件
|
|
92
|
-
* @param config.handler {Handler} 业务函数
|
|
93
|
-
*/
|
|
94
|
-
constructor(config: FuncConfig<TEvent, TContext>);
|
|
95
|
-
compose(key: LifeCycleKey): (data: any, next?: () => void) => any;
|
|
96
|
-
/**
|
|
97
|
-
* 发布云资源
|
|
98
|
-
* @param data {object} 代码包信息
|
|
99
|
-
* @param data.root {string} 项目根目录
|
|
100
|
-
* @param data.filename {string} 包括完整路径的流程文件名
|
|
101
|
-
* @param data.env {string} 环境
|
|
102
|
-
*/
|
|
103
|
-
deploy(data: DeployData): any;
|
|
104
|
-
/**
|
|
105
|
-
* 启动云实例
|
|
106
|
-
*/
|
|
107
|
-
mount(data: {
|
|
108
|
-
event: TEvent;
|
|
109
|
-
context: TContext;
|
|
110
|
-
config?: Config;
|
|
111
|
-
}): Promise<void>;
|
|
112
|
-
/**
|
|
113
|
-
* 执行云函数
|
|
114
|
-
* @param data {object} 执行信息
|
|
115
|
-
*/
|
|
116
|
-
invoke(data: InvokeData<TEvent, TContext, RESULT>): Promise<void>;
|
|
117
|
-
/**
|
|
118
|
-
* 创建触发函数
|
|
119
|
-
*/
|
|
120
|
-
export(): {
|
|
121
|
-
handler: ExportedHandler<TEvent, TContext, RESULT>;
|
|
122
|
-
};
|
|
123
|
-
}
|
|
124
|
-
export interface UseifyPlugin {
|
|
125
|
-
mount?({ config: Config }: {
|
|
126
|
-
config: any;
|
|
127
|
-
}): Promise<void>;
|
|
128
|
-
}
|
|
129
|
-
export declare function usePlugin<T extends Plugin>(plugin: T & UseifyPlugin): T & UseifyPlugin;
|
|
130
|
-
export declare function useFunc<TEvent = any, TContext = any, RESULT = any>(handler: () => Handler<TEvent, TContext, RESULT>): Func<TEvent, TContext, RESULT>;
|
package/lib/index.es.js
DELETED
|
@@ -1,212 +0,0 @@
|
|
|
1
|
-
import Logger from '@faasjs/logger';
|
|
2
|
-
|
|
3
|
-
class RunHandler {
|
|
4
|
-
constructor() {
|
|
5
|
-
this.type = 'handler';
|
|
6
|
-
this.name = 'handler';
|
|
7
|
-
}
|
|
8
|
-
async onInvoke(data, next) {
|
|
9
|
-
if (data.handler)
|
|
10
|
-
if (!data.runHandler) {
|
|
11
|
-
try {
|
|
12
|
-
data.response = await new Promise(function (resolve, reject) {
|
|
13
|
-
data.callback = function (error, result) {
|
|
14
|
-
if (error)
|
|
15
|
-
reject(error);
|
|
16
|
-
else
|
|
17
|
-
resolve(result);
|
|
18
|
-
};
|
|
19
|
-
Promise.resolve(data.handler(data)).then(function (result) {
|
|
20
|
-
resolve(result);
|
|
21
|
-
}).catch(function (error) {
|
|
22
|
-
reject(error);
|
|
23
|
-
});
|
|
24
|
-
});
|
|
25
|
-
}
|
|
26
|
-
catch (error) {
|
|
27
|
-
data.logger.error(error);
|
|
28
|
-
data.response = error;
|
|
29
|
-
}
|
|
30
|
-
data.runHandler = true;
|
|
31
|
-
}
|
|
32
|
-
else
|
|
33
|
-
data.logger.warn('[RunHandler] handler has been run');
|
|
34
|
-
await next();
|
|
35
|
-
}
|
|
36
|
-
}
|
|
37
|
-
|
|
38
|
-
/* eslint-disable @typescript-eslint/no-unsafe-call,@typescript-eslint/no-unsafe-return,@typescript-eslint/no-unsafe-member-access,@typescript-eslint/no-unsafe-assignment */
|
|
39
|
-
class Func {
|
|
40
|
-
/**
|
|
41
|
-
* 新建流程
|
|
42
|
-
* @param config {object} 配置项
|
|
43
|
-
* @param config.plugins {Plugin[]} 插件
|
|
44
|
-
* @param config.handler {Handler} 业务函数
|
|
45
|
-
*/
|
|
46
|
-
constructor(config) {
|
|
47
|
-
this.logger = new Logger('Func');
|
|
48
|
-
this.handler = config.handler;
|
|
49
|
-
this.plugins = config.plugins || [];
|
|
50
|
-
this.plugins.push(new RunHandler());
|
|
51
|
-
this.config = {
|
|
52
|
-
providers: Object.create(null),
|
|
53
|
-
plugins: Object.create(null)
|
|
54
|
-
};
|
|
55
|
-
this.mounted = false;
|
|
56
|
-
this.cachedFunctions = Object.create(null);
|
|
57
|
-
}
|
|
58
|
-
compose(key) {
|
|
59
|
-
const logger = new Logger(key);
|
|
60
|
-
let list = [];
|
|
61
|
-
if (this.cachedFunctions[key])
|
|
62
|
-
list = this.cachedFunctions[key];
|
|
63
|
-
else {
|
|
64
|
-
for (const plugin of this.plugins)
|
|
65
|
-
if (typeof plugin[key] === 'function')
|
|
66
|
-
list.push({
|
|
67
|
-
key: plugin.name,
|
|
68
|
-
handler: plugin[key].bind(plugin)
|
|
69
|
-
});
|
|
70
|
-
this.cachedFunctions[key] = list;
|
|
71
|
-
}
|
|
72
|
-
return function (data, next) {
|
|
73
|
-
let index = -1;
|
|
74
|
-
const dispatch = async function (i) {
|
|
75
|
-
if (i <= index)
|
|
76
|
-
return Promise.reject(Error('next() called multiple times'));
|
|
77
|
-
index = i;
|
|
78
|
-
let fn = list[i];
|
|
79
|
-
if (i === list.length)
|
|
80
|
-
fn = next;
|
|
81
|
-
if (!fn)
|
|
82
|
-
return Promise.resolve();
|
|
83
|
-
if (fn.key) {
|
|
84
|
-
logger.debug(`[${fn.key}] begin`);
|
|
85
|
-
logger.time(fn.key);
|
|
86
|
-
}
|
|
87
|
-
try {
|
|
88
|
-
const res = await Promise.resolve(fn.handler(data, dispatch.bind(null, i + 1)));
|
|
89
|
-
if (fn.key)
|
|
90
|
-
logger.timeEnd(fn.key, `[${fn.key}] end`);
|
|
91
|
-
return res;
|
|
92
|
-
}
|
|
93
|
-
catch (err) {
|
|
94
|
-
if (fn.key)
|
|
95
|
-
logger.timeEnd(fn.key, `[${fn.key}] failed`);
|
|
96
|
-
console.error(err);
|
|
97
|
-
return Promise.reject(err);
|
|
98
|
-
}
|
|
99
|
-
};
|
|
100
|
-
return dispatch(0);
|
|
101
|
-
};
|
|
102
|
-
}
|
|
103
|
-
/**
|
|
104
|
-
* 发布云资源
|
|
105
|
-
* @param data {object} 代码包信息
|
|
106
|
-
* @param data.root {string} 项目根目录
|
|
107
|
-
* @param data.filename {string} 包括完整路径的流程文件名
|
|
108
|
-
* @param data.env {string} 环境
|
|
109
|
-
*/
|
|
110
|
-
deploy(data) {
|
|
111
|
-
this.logger.debug('onDeploy');
|
|
112
|
-
return this.compose('onDeploy')(data);
|
|
113
|
-
}
|
|
114
|
-
/**
|
|
115
|
-
* 启动云实例
|
|
116
|
-
*/
|
|
117
|
-
async mount(data) {
|
|
118
|
-
this.logger.debug('onMount');
|
|
119
|
-
if (this.mounted) {
|
|
120
|
-
this.logger.warn('mount() has been called, skipped.');
|
|
121
|
-
return;
|
|
122
|
-
}
|
|
123
|
-
data.config = this.config;
|
|
124
|
-
try {
|
|
125
|
-
this.logger.time('mount');
|
|
126
|
-
await this.compose('onMount')(data);
|
|
127
|
-
this.mounted = true;
|
|
128
|
-
}
|
|
129
|
-
finally {
|
|
130
|
-
this.logger.timeEnd('mount', 'mounted');
|
|
131
|
-
}
|
|
132
|
-
}
|
|
133
|
-
/**
|
|
134
|
-
* 执行云函数
|
|
135
|
-
* @param data {object} 执行信息
|
|
136
|
-
*/
|
|
137
|
-
async invoke(data) {
|
|
138
|
-
// 实例未启动时执行启动函数
|
|
139
|
-
if (!this.mounted)
|
|
140
|
-
await this.mount({
|
|
141
|
-
event: data.event,
|
|
142
|
-
context: data.context,
|
|
143
|
-
});
|
|
144
|
-
try {
|
|
145
|
-
await this.compose('onInvoke')(data);
|
|
146
|
-
}
|
|
147
|
-
catch (error) {
|
|
148
|
-
// 执行异常时回传异常
|
|
149
|
-
this.logger.error(error);
|
|
150
|
-
data.response = error;
|
|
151
|
-
}
|
|
152
|
-
}
|
|
153
|
-
/**
|
|
154
|
-
* 创建触发函数
|
|
155
|
-
*/
|
|
156
|
-
export() {
|
|
157
|
-
return {
|
|
158
|
-
handler: async (event, context, callback) => {
|
|
159
|
-
const logger = new Logger();
|
|
160
|
-
logger.debug('event: %O', event);
|
|
161
|
-
logger.debug('context: %O', context);
|
|
162
|
-
if (typeof context === 'undefined')
|
|
163
|
-
context = {};
|
|
164
|
-
if (!context.request_id)
|
|
165
|
-
context.request_id = new Date().getTime().toString();
|
|
166
|
-
if (!context.request_at)
|
|
167
|
-
context.request_at = Math.round(new Date().getTime() / 1000);
|
|
168
|
-
context.callbackWaitsForEmptyEventLoop = false;
|
|
169
|
-
const data = {
|
|
170
|
-
event,
|
|
171
|
-
context,
|
|
172
|
-
callback,
|
|
173
|
-
response: undefined,
|
|
174
|
-
handler: this.handler,
|
|
175
|
-
logger,
|
|
176
|
-
config: this.config
|
|
177
|
-
};
|
|
178
|
-
await this.invoke(data);
|
|
179
|
-
if (Object.prototype.toString.call(data.response) === '[object Error]')
|
|
180
|
-
throw data.response;
|
|
181
|
-
return data.response;
|
|
182
|
-
}
|
|
183
|
-
};
|
|
184
|
-
}
|
|
185
|
-
}
|
|
186
|
-
let plugins = [];
|
|
187
|
-
function usePlugin(plugin) {
|
|
188
|
-
if (!plugins.find(p => p.name === plugin.name))
|
|
189
|
-
plugins.push(plugin);
|
|
190
|
-
if (!plugin.mount)
|
|
191
|
-
plugin.mount = async function ({ config }) {
|
|
192
|
-
if (plugin.onMount)
|
|
193
|
-
await plugin.onMount({
|
|
194
|
-
config,
|
|
195
|
-
event: {},
|
|
196
|
-
context: {}
|
|
197
|
-
}, async () => Promise.resolve());
|
|
198
|
-
};
|
|
199
|
-
return plugin;
|
|
200
|
-
}
|
|
201
|
-
function useFunc(handler) {
|
|
202
|
-
plugins = [];
|
|
203
|
-
const invokeHanlder = handler();
|
|
204
|
-
const func = new Func({
|
|
205
|
-
plugins,
|
|
206
|
-
handler: invokeHanlder
|
|
207
|
-
});
|
|
208
|
-
plugins = [];
|
|
209
|
-
return func;
|
|
210
|
-
}
|
|
211
|
-
|
|
212
|
-
export { Func, useFunc, usePlugin };
|