@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/lib/index.js DELETED
@@ -1,220 +0,0 @@
1
- 'use strict';
2
-
3
- Object.defineProperty(exports, '__esModule', { value: true });
4
-
5
- function _interopDefault (ex) { return (ex && (typeof ex === 'object') && 'default' in ex) ? ex['default'] : ex; }
6
-
7
- var Logger = _interopDefault(require('@faasjs/logger'));
8
-
9
- class RunHandler {
10
- constructor() {
11
- this.type = 'handler';
12
- this.name = 'handler';
13
- }
14
- async onInvoke(data, next) {
15
- if (data.handler)
16
- if (!data.runHandler) {
17
- try {
18
- data.response = await new Promise(function (resolve, reject) {
19
- data.callback = function (error, result) {
20
- if (error)
21
- reject(error);
22
- else
23
- resolve(result);
24
- };
25
- Promise.resolve(data.handler(data)).then(function (result) {
26
- resolve(result);
27
- }).catch(function (error) {
28
- reject(error);
29
- });
30
- });
31
- }
32
- catch (error) {
33
- data.logger.error(error);
34
- data.response = error;
35
- }
36
- data.runHandler = true;
37
- }
38
- else
39
- data.logger.warn('[RunHandler] handler has been run');
40
- await next();
41
- }
42
- }
43
-
44
- /* eslint-disable @typescript-eslint/no-unsafe-call,@typescript-eslint/no-unsafe-return,@typescript-eslint/no-unsafe-member-access,@typescript-eslint/no-unsafe-assignment */
45
- class Func {
46
- /**
47
- * 新建流程
48
- * @param config {object} 配置项
49
- * @param config.plugins {Plugin[]} 插件
50
- * @param config.handler {Handler} 业务函数
51
- */
52
- constructor(config) {
53
- this.logger = new Logger('Func');
54
- this.handler = config.handler;
55
- this.plugins = config.plugins || [];
56
- this.plugins.push(new RunHandler());
57
- this.config = {
58
- providers: Object.create(null),
59
- plugins: Object.create(null)
60
- };
61
- this.mounted = false;
62
- this.cachedFunctions = Object.create(null);
63
- }
64
- compose(key) {
65
- const logger = new Logger(key);
66
- let list = [];
67
- if (this.cachedFunctions[key])
68
- list = this.cachedFunctions[key];
69
- else {
70
- for (const plugin of this.plugins)
71
- if (typeof plugin[key] === 'function')
72
- list.push({
73
- key: plugin.name,
74
- handler: plugin[key].bind(plugin)
75
- });
76
- this.cachedFunctions[key] = list;
77
- }
78
- return function (data, next) {
79
- let index = -1;
80
- const dispatch = async function (i) {
81
- if (i <= index)
82
- return Promise.reject(Error('next() called multiple times'));
83
- index = i;
84
- let fn = list[i];
85
- if (i === list.length)
86
- fn = next;
87
- if (!fn)
88
- return Promise.resolve();
89
- if (fn.key) {
90
- logger.debug(`[${fn.key}] begin`);
91
- logger.time(fn.key);
92
- }
93
- try {
94
- const res = await Promise.resolve(fn.handler(data, dispatch.bind(null, i + 1)));
95
- if (fn.key)
96
- logger.timeEnd(fn.key, `[${fn.key}] end`);
97
- return res;
98
- }
99
- catch (err) {
100
- if (fn.key)
101
- logger.timeEnd(fn.key, `[${fn.key}] failed`);
102
- console.error(err);
103
- return Promise.reject(err);
104
- }
105
- };
106
- return dispatch(0);
107
- };
108
- }
109
- /**
110
- * 发布云资源
111
- * @param data {object} 代码包信息
112
- * @param data.root {string} 项目根目录
113
- * @param data.filename {string} 包括完整路径的流程文件名
114
- * @param data.env {string} 环境
115
- */
116
- deploy(data) {
117
- this.logger.debug('onDeploy');
118
- return this.compose('onDeploy')(data);
119
- }
120
- /**
121
- * 启动云实例
122
- */
123
- async mount(data) {
124
- this.logger.debug('onMount');
125
- if (this.mounted) {
126
- this.logger.warn('mount() has been called, skipped.');
127
- return;
128
- }
129
- data.config = this.config;
130
- try {
131
- this.logger.time('mount');
132
- await this.compose('onMount')(data);
133
- this.mounted = true;
134
- }
135
- finally {
136
- this.logger.timeEnd('mount', 'mounted');
137
- }
138
- }
139
- /**
140
- * 执行云函数
141
- * @param data {object} 执行信息
142
- */
143
- async invoke(data) {
144
- // 实例未启动时执行启动函数
145
- if (!this.mounted)
146
- await this.mount({
147
- event: data.event,
148
- context: data.context,
149
- });
150
- try {
151
- await this.compose('onInvoke')(data);
152
- }
153
- catch (error) {
154
- // 执行异常时回传异常
155
- this.logger.error(error);
156
- data.response = error;
157
- }
158
- }
159
- /**
160
- * 创建触发函数
161
- */
162
- export() {
163
- return {
164
- handler: async (event, context, callback) => {
165
- const logger = new Logger();
166
- logger.debug('event: %O', event);
167
- logger.debug('context: %O', context);
168
- if (typeof context === 'undefined')
169
- context = {};
170
- if (!context.request_id)
171
- context.request_id = new Date().getTime().toString();
172
- if (!context.request_at)
173
- context.request_at = Math.round(new Date().getTime() / 1000);
174
- context.callbackWaitsForEmptyEventLoop = false;
175
- const data = {
176
- event,
177
- context,
178
- callback,
179
- response: undefined,
180
- handler: this.handler,
181
- logger,
182
- config: this.config
183
- };
184
- await this.invoke(data);
185
- if (Object.prototype.toString.call(data.response) === '[object Error]')
186
- throw data.response;
187
- return data.response;
188
- }
189
- };
190
- }
191
- }
192
- let plugins = [];
193
- function usePlugin(plugin) {
194
- if (!plugins.find(p => p.name === plugin.name))
195
- plugins.push(plugin);
196
- if (!plugin.mount)
197
- plugin.mount = async function ({ config }) {
198
- if (plugin.onMount)
199
- await plugin.onMount({
200
- config,
201
- event: {},
202
- context: {}
203
- }, async () => Promise.resolve());
204
- };
205
- return plugin;
206
- }
207
- function useFunc(handler) {
208
- plugins = [];
209
- const invokeHanlder = handler();
210
- const func = new Func({
211
- plugins,
212
- handler: invokeHanlder
213
- });
214
- plugins = [];
215
- return func;
216
- }
217
-
218
- exports.Func = Func;
219
- exports.useFunc = useFunc;
220
- exports.usePlugin = usePlugin;
@@ -1,6 +0,0 @@
1
- import { InvokeData } from '../../index';
2
- export default class RunHandler {
3
- readonly type: string;
4
- readonly name: string;
5
- onInvoke(data: InvokeData, next: () => Promise<void>): Promise<void>;
6
- }