@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/README.md
CHANGED
|
@@ -1,9 +1,336 @@
|
|
|
1
1
|
# @faasjs/func
|
|
2
2
|
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
[](https://github.com/faasjs/faasjs/blob/master/packages/faasjs/func/LICENSE)
|
|
3
|
+
[](https://github.com/faasjs/faasjs/blob/main/packages/faasjs/func/LICENSE)
|
|
6
4
|
[](https://www.npmjs.com/package/@faasjs/func)
|
|
7
5
|
[](https://www.npmjs.com/package/@faasjs/func)
|
|
8
6
|
|
|
9
|
-
|
|
7
|
+
FaasJS's function module.
|
|
8
|
+
|
|
9
|
+
## Install
|
|
10
|
+
|
|
11
|
+
npm install @faasjs/func
|
|
12
|
+
|
|
13
|
+
## Modules
|
|
14
|
+
|
|
15
|
+
### Classes
|
|
16
|
+
|
|
17
|
+
- [Func](classes/Func.md)
|
|
18
|
+
|
|
19
|
+
### Type Aliases
|
|
20
|
+
|
|
21
|
+
- [Config](#config)
|
|
22
|
+
- [DeployData](#deploydata)
|
|
23
|
+
- [ExportedHandler](#exportedhandler)
|
|
24
|
+
- [FuncConfig](#funcconfig)
|
|
25
|
+
- [Handler](#handler)
|
|
26
|
+
- [InvokeData](#invokedata)
|
|
27
|
+
- [LifeCycleKey](#lifecyclekey)
|
|
28
|
+
- [MountData](#mountdata)
|
|
29
|
+
- [Next](#next)
|
|
30
|
+
- [Plugin](#plugin)
|
|
31
|
+
- [ProviderConfig](#providerconfig)
|
|
32
|
+
- [UseifyPlugin](#useifyplugin)
|
|
33
|
+
|
|
34
|
+
### Functions
|
|
35
|
+
|
|
36
|
+
- [useFunc](#usefunc)
|
|
37
|
+
- [usePlugin](#useplugin)
|
|
38
|
+
|
|
39
|
+
## Type Aliases
|
|
40
|
+
|
|
41
|
+
### Config
|
|
42
|
+
|
|
43
|
+
Ƭ **Config**: `Object`
|
|
44
|
+
|
|
45
|
+
#### Index signature
|
|
46
|
+
|
|
47
|
+
▪ [key: `string`]: `any`
|
|
48
|
+
|
|
49
|
+
#### Type declaration
|
|
50
|
+
|
|
51
|
+
| Name | Type |
|
|
52
|
+
| :------ | :------ |
|
|
53
|
+
| `plugins?` | { `[key: string]`: { `[key: string]`: `any`; `config?`: { `[key: string]`: `any`; } ; `provider?`: `string` \| [`ProviderConfig`](#providerconfig) ; `type`: `string` }; } |
|
|
54
|
+
| `providers?` | { `[key: string]`: [`ProviderConfig`](#providerconfig); } |
|
|
55
|
+
|
|
56
|
+
___
|
|
57
|
+
|
|
58
|
+
### DeployData
|
|
59
|
+
|
|
60
|
+
Ƭ **DeployData**: `Object`
|
|
61
|
+
|
|
62
|
+
#### Index signature
|
|
63
|
+
|
|
64
|
+
▪ [key: `string`]: `any`
|
|
65
|
+
|
|
66
|
+
#### Type declaration
|
|
67
|
+
|
|
68
|
+
| Name | Type |
|
|
69
|
+
| :------ | :------ |
|
|
70
|
+
| `config?` | [`Config`](#config) |
|
|
71
|
+
| `dependencies` | { `[name: string]`: `string`; } |
|
|
72
|
+
| `env?` | `string` |
|
|
73
|
+
| `filename` | `string` |
|
|
74
|
+
| `logger?` | `Logger` |
|
|
75
|
+
| `name?` | `string` |
|
|
76
|
+
| `plugins?` | { `[name: string]`: { `[key: string]`: `any`; `config`: { `[key: string]`: `any`; } ; `name?`: `string` ; `plugin`: [`Plugin`](#plugin) ; `provider?`: `string` ; `type`: `string` }; } |
|
|
77
|
+
| `root` | `string` |
|
|
78
|
+
| `version?` | `string` |
|
|
79
|
+
|
|
80
|
+
___
|
|
81
|
+
|
|
82
|
+
### ExportedHandler
|
|
83
|
+
|
|
84
|
+
Ƭ **ExportedHandler**<`TEvent`, `TContext`, `TResult`\>: (`event`: `TEvent`, `context?`: `TContext`, `callback?`: (...`args`: `any`) => `any`) => `Promise`<`TResult`\>
|
|
85
|
+
|
|
86
|
+
#### Type parameters
|
|
87
|
+
|
|
88
|
+
| Name | Type |
|
|
89
|
+
| :------ | :------ |
|
|
90
|
+
| `TEvent` | `any` |
|
|
91
|
+
| `TContext` | `any` |
|
|
92
|
+
| `TResult` | `any` |
|
|
93
|
+
|
|
94
|
+
#### Type declaration
|
|
95
|
+
|
|
96
|
+
▸ (`event`, `context?`, `callback?`): `Promise`<`TResult`\>
|
|
97
|
+
|
|
98
|
+
##### Parameters
|
|
99
|
+
|
|
100
|
+
| Name | Type |
|
|
101
|
+
| :------ | :------ |
|
|
102
|
+
| `event` | `TEvent` |
|
|
103
|
+
| `context?` | `TContext` |
|
|
104
|
+
| `callback?` | (...`args`: `any`) => `any` |
|
|
105
|
+
|
|
106
|
+
##### Returns
|
|
107
|
+
|
|
108
|
+
`Promise`<`TResult`\>
|
|
109
|
+
|
|
110
|
+
___
|
|
111
|
+
|
|
112
|
+
### FuncConfig
|
|
113
|
+
|
|
114
|
+
Ƭ **FuncConfig**<`TEvent`, `TContext`, `TResult`\>: `Object`
|
|
115
|
+
|
|
116
|
+
#### Type parameters
|
|
117
|
+
|
|
118
|
+
| Name | Type |
|
|
119
|
+
| :------ | :------ |
|
|
120
|
+
| `TEvent` | `any` |
|
|
121
|
+
| `TContext` | `any` |
|
|
122
|
+
| `TResult` | `any` |
|
|
123
|
+
|
|
124
|
+
#### Type declaration
|
|
125
|
+
|
|
126
|
+
| Name | Type |
|
|
127
|
+
| :------ | :------ |
|
|
128
|
+
| `handler?` | [`Handler`](#handler)<`TEvent`, `TContext`, `TResult`\> |
|
|
129
|
+
| `plugins?` | [`Plugin`](#plugin)[] |
|
|
130
|
+
|
|
131
|
+
___
|
|
132
|
+
|
|
133
|
+
### Handler
|
|
134
|
+
|
|
135
|
+
Ƭ **Handler**<`TEvent`, `TContext`, `TResult`\>: (`data`: [`InvokeData`](#invokedata)<`TEvent`, `TContext`\>) => `Promise`<`TResult`\>
|
|
136
|
+
|
|
137
|
+
#### Type parameters
|
|
138
|
+
|
|
139
|
+
| Name | Type |
|
|
140
|
+
| :------ | :------ |
|
|
141
|
+
| `TEvent` | `any` |
|
|
142
|
+
| `TContext` | `any` |
|
|
143
|
+
| `TResult` | `any` |
|
|
144
|
+
|
|
145
|
+
#### Type declaration
|
|
146
|
+
|
|
147
|
+
▸ (`data`): `Promise`<`TResult`\>
|
|
148
|
+
|
|
149
|
+
##### Parameters
|
|
150
|
+
|
|
151
|
+
| Name | Type |
|
|
152
|
+
| :------ | :------ |
|
|
153
|
+
| `data` | [`InvokeData`](#invokedata)<`TEvent`, `TContext`\> |
|
|
154
|
+
|
|
155
|
+
##### Returns
|
|
156
|
+
|
|
157
|
+
`Promise`<`TResult`\>
|
|
158
|
+
|
|
159
|
+
___
|
|
160
|
+
|
|
161
|
+
### InvokeData
|
|
162
|
+
|
|
163
|
+
Ƭ **InvokeData**<`TEvent`, `TContext`, `TResult`\>: `Object`
|
|
164
|
+
|
|
165
|
+
#### Type parameters
|
|
166
|
+
|
|
167
|
+
| Name | Type |
|
|
168
|
+
| :------ | :------ |
|
|
169
|
+
| `TEvent` | `any` |
|
|
170
|
+
| `TContext` | `any` |
|
|
171
|
+
| `TResult` | `any` |
|
|
172
|
+
|
|
173
|
+
#### Index signature
|
|
174
|
+
|
|
175
|
+
▪ [key: `string`]: `any`
|
|
176
|
+
|
|
177
|
+
#### Type declaration
|
|
178
|
+
|
|
179
|
+
| Name | Type |
|
|
180
|
+
| :------ | :------ |
|
|
181
|
+
| `callback` | `any` |
|
|
182
|
+
| `config` | [`Config`](#config) |
|
|
183
|
+
| `context` | `TContext` |
|
|
184
|
+
| `event` | `TEvent` |
|
|
185
|
+
| `handler?` | [`Handler`](#handler)<`TEvent`, `TContext`, `TResult`\> |
|
|
186
|
+
| `logger` | `Logger` |
|
|
187
|
+
| `response` | `any` |
|
|
188
|
+
|
|
189
|
+
___
|
|
190
|
+
|
|
191
|
+
### LifeCycleKey
|
|
192
|
+
|
|
193
|
+
Ƭ **LifeCycleKey**: ``"onDeploy"`` \| ``"onMount"`` \| ``"onInvoke"``
|
|
194
|
+
|
|
195
|
+
___
|
|
196
|
+
|
|
197
|
+
### MountData
|
|
198
|
+
|
|
199
|
+
Ƭ **MountData**: `Object`
|
|
200
|
+
|
|
201
|
+
#### Index signature
|
|
202
|
+
|
|
203
|
+
▪ [key: `string`]: `any`
|
|
204
|
+
|
|
205
|
+
#### Type declaration
|
|
206
|
+
|
|
207
|
+
| Name | Type |
|
|
208
|
+
| :------ | :------ |
|
|
209
|
+
| `config` | [`Config`](#config) |
|
|
210
|
+
| `context` | `any` |
|
|
211
|
+
| `event` | `any` |
|
|
212
|
+
|
|
213
|
+
___
|
|
214
|
+
|
|
215
|
+
### Next
|
|
216
|
+
|
|
217
|
+
Ƭ **Next**: () => `Promise`<`void`\>
|
|
218
|
+
|
|
219
|
+
#### Type declaration
|
|
220
|
+
|
|
221
|
+
▸ (): `Promise`<`void`\>
|
|
222
|
+
|
|
223
|
+
##### Returns
|
|
224
|
+
|
|
225
|
+
`Promise`<`void`\>
|
|
226
|
+
|
|
227
|
+
___
|
|
228
|
+
|
|
229
|
+
### Plugin
|
|
230
|
+
|
|
231
|
+
Ƭ **Plugin**: `Object`
|
|
232
|
+
|
|
233
|
+
#### Index signature
|
|
234
|
+
|
|
235
|
+
▪ [key: `string`]: `any`
|
|
236
|
+
|
|
237
|
+
#### Type declaration
|
|
238
|
+
|
|
239
|
+
| Name | Type |
|
|
240
|
+
| :------ | :------ |
|
|
241
|
+
| `name` | `string` |
|
|
242
|
+
| `onDeploy?` | (`data`: [`DeployData`](#deploydata), `next`: [`Next`](#next)) => `void` \| `Promise`<`void`\> |
|
|
243
|
+
| `onInvoke?` | (`data`: [`InvokeData`](#invokedata), `next`: [`Next`](#next)) => `void` \| `Promise`<`void`\> |
|
|
244
|
+
| `onMount?` | (`data`: [`MountData`](#mountdata), `next`: [`Next`](#next)) => `void` \| `Promise`<`void`\> |
|
|
245
|
+
| `type` | `string` |
|
|
246
|
+
|
|
247
|
+
___
|
|
248
|
+
|
|
249
|
+
### ProviderConfig
|
|
250
|
+
|
|
251
|
+
Ƭ **ProviderConfig**: `Object`
|
|
252
|
+
|
|
253
|
+
#### Type declaration
|
|
254
|
+
|
|
255
|
+
| Name | Type |
|
|
256
|
+
| :------ | :------ |
|
|
257
|
+
| `config` | { `[key: string]`: `any`; } |
|
|
258
|
+
| `type` | `string` |
|
|
259
|
+
|
|
260
|
+
___
|
|
261
|
+
|
|
262
|
+
### UseifyPlugin
|
|
263
|
+
|
|
264
|
+
Ƭ **UseifyPlugin**: `Object`
|
|
265
|
+
|
|
266
|
+
#### Type declaration
|
|
267
|
+
|
|
268
|
+
| Name | Type |
|
|
269
|
+
| :------ | :------ |
|
|
270
|
+
| `mount?` | (`data`: { `config`: [`Config`](#config) }) => `Promise`<`void`\> |
|
|
271
|
+
|
|
272
|
+
## Functions
|
|
273
|
+
|
|
274
|
+
### useFunc
|
|
275
|
+
|
|
276
|
+
▸ **useFunc**<`TEvent`, `TContext`, `TResult`\>(`handler`): [`Func`](classes/Func.md)<`TEvent`, `TContext`, `TResult`\>
|
|
277
|
+
|
|
278
|
+
```ts
|
|
279
|
+
// pure function
|
|
280
|
+
export default useFunc(() => {
|
|
281
|
+
return () => {
|
|
282
|
+
return 'Hello World'
|
|
283
|
+
}
|
|
284
|
+
})
|
|
285
|
+
|
|
286
|
+
// with http
|
|
287
|
+
import { useHttp } from '@faasjs/http'
|
|
288
|
+
|
|
289
|
+
export default useFunc(() => {
|
|
290
|
+
const http = useHttp<{ name: string }>()
|
|
291
|
+
|
|
292
|
+
return () => {
|
|
293
|
+
return `Hello ${http.params.name}`
|
|
294
|
+
}
|
|
295
|
+
})
|
|
296
|
+
```
|
|
297
|
+
|
|
298
|
+
#### Type parameters
|
|
299
|
+
|
|
300
|
+
| Name | Type |
|
|
301
|
+
| :------ | :------ |
|
|
302
|
+
| `TEvent` | `any` |
|
|
303
|
+
| `TContext` | `any` |
|
|
304
|
+
| `TResult` | `any` |
|
|
305
|
+
|
|
306
|
+
#### Parameters
|
|
307
|
+
|
|
308
|
+
| Name | Type |
|
|
309
|
+
| :------ | :------ |
|
|
310
|
+
| `handler` | () => [`Handler`](#handler)<`TEvent`, `TContext`, `TResult`\> |
|
|
311
|
+
|
|
312
|
+
#### Returns
|
|
313
|
+
|
|
314
|
+
[`Func`](classes/Func.md)<`TEvent`, `TContext`, `TResult`\>
|
|
315
|
+
|
|
316
|
+
___
|
|
317
|
+
|
|
318
|
+
### usePlugin
|
|
319
|
+
|
|
320
|
+
▸ **usePlugin**<`T`\>(`plugin`): `T` & [`UseifyPlugin`](#useifyplugin)
|
|
321
|
+
|
|
322
|
+
#### Type parameters
|
|
323
|
+
|
|
324
|
+
| Name | Type |
|
|
325
|
+
| :------ | :------ |
|
|
326
|
+
| `T` | extends [`Plugin`](#plugin) |
|
|
327
|
+
|
|
328
|
+
#### Parameters
|
|
329
|
+
|
|
330
|
+
| Name | Type |
|
|
331
|
+
| :------ | :------ |
|
|
332
|
+
| `plugin` | `T` & [`UseifyPlugin`](#useifyplugin) |
|
|
333
|
+
|
|
334
|
+
#### Returns
|
|
335
|
+
|
|
336
|
+
`T` & [`UseifyPlugin`](#useifyplugin)
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,156 @@
|
|
|
1
|
+
import { Logger } from '@faasjs/logger';
|
|
2
|
+
|
|
3
|
+
type Handler<TEvent = any, TContext = any, TResult = any> = (data: InvokeData<TEvent, TContext>) => Promise<TResult>;
|
|
4
|
+
type Next = () => Promise<void>;
|
|
5
|
+
type ExportedHandler<TEvent = any, TContext = any, TResult = any> = (event: TEvent, context?: TContext, callback?: (...args: any) => any) => Promise<TResult>;
|
|
6
|
+
type Plugin = {
|
|
7
|
+
[key: string]: any;
|
|
8
|
+
readonly type: string;
|
|
9
|
+
readonly name: string;
|
|
10
|
+
onDeploy?: (data: DeployData, next: Next) => void | Promise<void>;
|
|
11
|
+
onMount?: (data: MountData, next: Next) => void | Promise<void>;
|
|
12
|
+
onInvoke?: (data: InvokeData, next: Next) => void | Promise<void>;
|
|
13
|
+
};
|
|
14
|
+
type ProviderConfig = {
|
|
15
|
+
type: string;
|
|
16
|
+
config: {
|
|
17
|
+
[key: string]: any;
|
|
18
|
+
};
|
|
19
|
+
};
|
|
20
|
+
type Config = {
|
|
21
|
+
[key: string]: any;
|
|
22
|
+
providers?: {
|
|
23
|
+
[key: string]: ProviderConfig;
|
|
24
|
+
};
|
|
25
|
+
plugins?: {
|
|
26
|
+
[key: string]: {
|
|
27
|
+
[key: string]: any;
|
|
28
|
+
provider?: string | ProviderConfig;
|
|
29
|
+
type: string;
|
|
30
|
+
config?: {
|
|
31
|
+
[key: string]: any;
|
|
32
|
+
};
|
|
33
|
+
};
|
|
34
|
+
};
|
|
35
|
+
};
|
|
36
|
+
type DeployData = {
|
|
37
|
+
[key: string]: any;
|
|
38
|
+
root: string;
|
|
39
|
+
filename: string;
|
|
40
|
+
env?: string;
|
|
41
|
+
name?: string;
|
|
42
|
+
config?: Config;
|
|
43
|
+
version?: string;
|
|
44
|
+
dependencies: {
|
|
45
|
+
[name: string]: string;
|
|
46
|
+
};
|
|
47
|
+
plugins?: {
|
|
48
|
+
[name: string]: {
|
|
49
|
+
[key: string]: any;
|
|
50
|
+
name?: string;
|
|
51
|
+
type: string;
|
|
52
|
+
provider?: string;
|
|
53
|
+
config: {
|
|
54
|
+
[key: string]: any;
|
|
55
|
+
};
|
|
56
|
+
plugin: Plugin;
|
|
57
|
+
};
|
|
58
|
+
};
|
|
59
|
+
logger?: Logger;
|
|
60
|
+
};
|
|
61
|
+
type MountData = {
|
|
62
|
+
[key: string]: any;
|
|
63
|
+
config: Config;
|
|
64
|
+
event: any;
|
|
65
|
+
context: any;
|
|
66
|
+
};
|
|
67
|
+
type InvokeData<TEvent = any, TContext = any, TResult = any> = {
|
|
68
|
+
[key: string]: any;
|
|
69
|
+
event: TEvent;
|
|
70
|
+
context: TContext;
|
|
71
|
+
callback: any;
|
|
72
|
+
response: any;
|
|
73
|
+
logger: Logger;
|
|
74
|
+
handler?: Handler<TEvent, TContext, TResult>;
|
|
75
|
+
config: Config;
|
|
76
|
+
};
|
|
77
|
+
type LifeCycleKey = 'onDeploy' | 'onMount' | 'onInvoke';
|
|
78
|
+
type FuncConfig<TEvent = any, TContext = any, TResult = any> = {
|
|
79
|
+
plugins?: Plugin[];
|
|
80
|
+
handler?: Handler<TEvent, TContext, TResult>;
|
|
81
|
+
};
|
|
82
|
+
declare class Func<TEvent = any, TContext = any, TResult = any> {
|
|
83
|
+
[key: string]: any;
|
|
84
|
+
plugins: Plugin[];
|
|
85
|
+
handler?: Handler<TEvent, TContext, TResult>;
|
|
86
|
+
config: Config;
|
|
87
|
+
mounted: boolean;
|
|
88
|
+
filename?: string;
|
|
89
|
+
private cachedFunctions;
|
|
90
|
+
/**
|
|
91
|
+
* Create a cloud function
|
|
92
|
+
* @param config {object} config
|
|
93
|
+
* @param config.plugins {Plugin[]} plugins list
|
|
94
|
+
* @param config.handler {Handler} business logic
|
|
95
|
+
*/
|
|
96
|
+
constructor(config: FuncConfig<TEvent, TContext>);
|
|
97
|
+
private compose;
|
|
98
|
+
/**
|
|
99
|
+
* Deploy the function
|
|
100
|
+
* @param data {object} data
|
|
101
|
+
* @param data.root {string} root path
|
|
102
|
+
* @param data.filename {string} filename
|
|
103
|
+
* @param data.env {string} environment
|
|
104
|
+
*/
|
|
105
|
+
deploy(data: DeployData): any;
|
|
106
|
+
/**
|
|
107
|
+
* First time mount the function
|
|
108
|
+
*/
|
|
109
|
+
mount(data: {
|
|
110
|
+
event: TEvent;
|
|
111
|
+
context: TContext;
|
|
112
|
+
config?: Config;
|
|
113
|
+
logger?: Logger;
|
|
114
|
+
}): Promise<void>;
|
|
115
|
+
/**
|
|
116
|
+
* Invoke the function
|
|
117
|
+
* @param data {object} data
|
|
118
|
+
*/
|
|
119
|
+
invoke(data: InvokeData<TEvent, TContext, TResult>): Promise<void>;
|
|
120
|
+
/**
|
|
121
|
+
* Export the function
|
|
122
|
+
*/
|
|
123
|
+
export(): {
|
|
124
|
+
handler: ExportedHandler<TEvent, TContext, TResult>;
|
|
125
|
+
};
|
|
126
|
+
}
|
|
127
|
+
type UseifyPlugin = {
|
|
128
|
+
mount?: (data: {
|
|
129
|
+
config: Config;
|
|
130
|
+
}) => Promise<void>;
|
|
131
|
+
};
|
|
132
|
+
declare function usePlugin<T extends Plugin>(plugin: T & UseifyPlugin): T & UseifyPlugin;
|
|
133
|
+
/**
|
|
134
|
+
* ```ts
|
|
135
|
+
* // pure function
|
|
136
|
+
* export default useFunc(() => {
|
|
137
|
+
* return () => {
|
|
138
|
+
* return 'Hello World'
|
|
139
|
+
* }
|
|
140
|
+
* })
|
|
141
|
+
*
|
|
142
|
+
* // with http
|
|
143
|
+
* import { useHttp } from '@faasjs/http'
|
|
144
|
+
*
|
|
145
|
+
* export default useFunc(() => {
|
|
146
|
+
* const http = useHttp<{ name: string }>()
|
|
147
|
+
*
|
|
148
|
+
* return () => {
|
|
149
|
+
* return `Hello ${http.params.name}`
|
|
150
|
+
* }
|
|
151
|
+
* })
|
|
152
|
+
* ```
|
|
153
|
+
*/
|
|
154
|
+
declare function useFunc<TEvent = any, TContext = any, TResult = any>(handler: () => Handler<TEvent, TContext, TResult>): Func<TEvent, TContext, TResult>;
|
|
155
|
+
|
|
156
|
+
export { Config, DeployData, ExportedHandler, Func, FuncConfig, Handler, InvokeData, LifeCycleKey, MountData, Next, Plugin, ProviderConfig, UseifyPlugin, useFunc, usePlugin };
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,225 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __export = (target, all) => {
|
|
7
|
+
for (var name in all)
|
|
8
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
+
};
|
|
10
|
+
var __copyProps = (to, from, except, desc) => {
|
|
11
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
+
for (let key of __getOwnPropNames(from))
|
|
13
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
+
}
|
|
16
|
+
return to;
|
|
17
|
+
};
|
|
18
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
+
|
|
20
|
+
// src/index.ts
|
|
21
|
+
var src_exports = {};
|
|
22
|
+
__export(src_exports, {
|
|
23
|
+
Func: () => Func,
|
|
24
|
+
useFunc: () => useFunc,
|
|
25
|
+
usePlugin: () => usePlugin
|
|
26
|
+
});
|
|
27
|
+
module.exports = __toCommonJS(src_exports);
|
|
28
|
+
var import_logger = require("@faasjs/logger");
|
|
29
|
+
|
|
30
|
+
// src/plugins/run_handler/index.ts
|
|
31
|
+
var RunHandler = class {
|
|
32
|
+
constructor() {
|
|
33
|
+
this.type = "handler";
|
|
34
|
+
this.name = "handler";
|
|
35
|
+
}
|
|
36
|
+
async onInvoke(data, next) {
|
|
37
|
+
if (data.handler)
|
|
38
|
+
if (!data.runHandler) {
|
|
39
|
+
try {
|
|
40
|
+
data.response = await new Promise(function(resolve, reject) {
|
|
41
|
+
data.callback = function(error, result) {
|
|
42
|
+
if (error)
|
|
43
|
+
reject(error);
|
|
44
|
+
else
|
|
45
|
+
resolve(result);
|
|
46
|
+
};
|
|
47
|
+
Promise.resolve(data.handler(data)).then(resolve).catch(reject);
|
|
48
|
+
});
|
|
49
|
+
} catch (error) {
|
|
50
|
+
data.logger.error(error);
|
|
51
|
+
data.response = error;
|
|
52
|
+
}
|
|
53
|
+
data.runHandler = true;
|
|
54
|
+
} else
|
|
55
|
+
data.logger.warn("[RunHandler] handler has been run");
|
|
56
|
+
await next();
|
|
57
|
+
}
|
|
58
|
+
};
|
|
59
|
+
|
|
60
|
+
// src/index.ts
|
|
61
|
+
var import_crypto = require("crypto");
|
|
62
|
+
var Func = class {
|
|
63
|
+
constructor(config) {
|
|
64
|
+
this.handler = config.handler;
|
|
65
|
+
this.plugins = config.plugins || [];
|
|
66
|
+
this.plugins.push(new RunHandler());
|
|
67
|
+
this.config = {
|
|
68
|
+
providers: /* @__PURE__ */ Object.create(null),
|
|
69
|
+
plugins: /* @__PURE__ */ Object.create(null)
|
|
70
|
+
};
|
|
71
|
+
this.mounted = false;
|
|
72
|
+
this.cachedFunctions = /* @__PURE__ */ Object.create(null);
|
|
73
|
+
try {
|
|
74
|
+
this.filename = new Error().stack.split("\n").find((s) => /[^/]\.func\.ts/.test(s)).match(/\((.*\.func\.ts).*\)/)[1];
|
|
75
|
+
} catch (error) {
|
|
76
|
+
new import_logger.Logger("Func").warn(error.message);
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
compose(key) {
|
|
80
|
+
let list = [];
|
|
81
|
+
if (this.cachedFunctions[key])
|
|
82
|
+
list = this.cachedFunctions[key];
|
|
83
|
+
else {
|
|
84
|
+
for (const plugin of this.plugins) {
|
|
85
|
+
const handler = plugin[key];
|
|
86
|
+
if (typeof handler === "function")
|
|
87
|
+
list.push({
|
|
88
|
+
key: plugin.name,
|
|
89
|
+
handler: handler.bind(plugin)
|
|
90
|
+
});
|
|
91
|
+
}
|
|
92
|
+
this.cachedFunctions[key] = list;
|
|
93
|
+
}
|
|
94
|
+
return async function(data, next) {
|
|
95
|
+
let index = -1;
|
|
96
|
+
const logger = (data == null ? void 0 : data.logger) || new import_logger.Logger();
|
|
97
|
+
const dispatch = async function(i) {
|
|
98
|
+
if (i <= index)
|
|
99
|
+
return Promise.reject(Error("next() called multiple times"));
|
|
100
|
+
index = i;
|
|
101
|
+
let fn = list[i];
|
|
102
|
+
if (i === list.length)
|
|
103
|
+
fn = next;
|
|
104
|
+
if (!fn)
|
|
105
|
+
return Promise.resolve();
|
|
106
|
+
if (typeof fn.key === "undefined")
|
|
107
|
+
fn.key = `UnNamedPlugin#${i}`;
|
|
108
|
+
logger.debug("[%s][%s] begin", fn.key, key);
|
|
109
|
+
logger.time(fn.key);
|
|
110
|
+
try {
|
|
111
|
+
const res = await Promise.resolve(fn.handler(data, dispatch.bind(null, i + 1)));
|
|
112
|
+
logger.timeEnd(fn.key, "[%s][%s] end", fn.key, key);
|
|
113
|
+
return res;
|
|
114
|
+
} catch (err) {
|
|
115
|
+
logger.timeEnd(fn.key, "[%s][%s] failed", fn.key, key);
|
|
116
|
+
logger.error(err);
|
|
117
|
+
return Promise.reject(err);
|
|
118
|
+
}
|
|
119
|
+
};
|
|
120
|
+
return await dispatch(0);
|
|
121
|
+
};
|
|
122
|
+
}
|
|
123
|
+
deploy(data) {
|
|
124
|
+
if (!data.logger)
|
|
125
|
+
data.logger = new import_logger.Logger("Func");
|
|
126
|
+
data.logger.debug("onDeploy");
|
|
127
|
+
data.logger.debug("Plugins: " + this.plugins.map((p) => `${p.type}#${p.name}`).join(","));
|
|
128
|
+
return this.compose("onDeploy")(data);
|
|
129
|
+
}
|
|
130
|
+
async mount(data) {
|
|
131
|
+
if (!data.logger)
|
|
132
|
+
data.logger = new import_logger.Logger("Func");
|
|
133
|
+
data.logger.debug("onMount");
|
|
134
|
+
if (this.mounted) {
|
|
135
|
+
data.logger.warn("mount() has been called, skipped.");
|
|
136
|
+
return;
|
|
137
|
+
}
|
|
138
|
+
if (!data.config)
|
|
139
|
+
data.config = this.config;
|
|
140
|
+
try {
|
|
141
|
+
data.logger.time("mount");
|
|
142
|
+
data.logger.debug("Plugins: " + this.plugins.map((p) => `${p.type}#${p.name}`).join(","));
|
|
143
|
+
await this.compose("onMount")(data);
|
|
144
|
+
this.mounted = true;
|
|
145
|
+
} finally {
|
|
146
|
+
data.logger.timeEnd("mount", "mounted");
|
|
147
|
+
}
|
|
148
|
+
}
|
|
149
|
+
async invoke(data) {
|
|
150
|
+
if (!this.mounted)
|
|
151
|
+
await this.mount({
|
|
152
|
+
event: data.event,
|
|
153
|
+
context: data.context,
|
|
154
|
+
config: data.config,
|
|
155
|
+
logger: data.logger
|
|
156
|
+
});
|
|
157
|
+
try {
|
|
158
|
+
await this.compose("onInvoke")(data);
|
|
159
|
+
} catch (error) {
|
|
160
|
+
data.logger.error(error);
|
|
161
|
+
data.response = error;
|
|
162
|
+
}
|
|
163
|
+
}
|
|
164
|
+
export() {
|
|
165
|
+
const handler = async (event, context, callback) => {
|
|
166
|
+
if (typeof context === "undefined")
|
|
167
|
+
context = {};
|
|
168
|
+
if (!context.request_id)
|
|
169
|
+
context.request_id = (0, import_crypto.randomBytes)(16).toString("hex");
|
|
170
|
+
if (!context.request_at)
|
|
171
|
+
context.request_at = Math.round(new Date().getTime() / 1e3);
|
|
172
|
+
context.callbackWaitsForEmptyEventLoop = false;
|
|
173
|
+
const logger = new import_logger.Logger(context.request_id);
|
|
174
|
+
logger.debug("event: %j", event);
|
|
175
|
+
logger.debug("context: %j", context);
|
|
176
|
+
const data = {
|
|
177
|
+
event,
|
|
178
|
+
context,
|
|
179
|
+
callback,
|
|
180
|
+
response: void 0,
|
|
181
|
+
handler: this.handler,
|
|
182
|
+
logger,
|
|
183
|
+
config: this.config
|
|
184
|
+
};
|
|
185
|
+
await this.invoke(data);
|
|
186
|
+
if (Object.prototype.toString.call(data.response) === "[object Error]")
|
|
187
|
+
throw data.response;
|
|
188
|
+
return data.response;
|
|
189
|
+
};
|
|
190
|
+
handler.bind(this);
|
|
191
|
+
return { handler };
|
|
192
|
+
}
|
|
193
|
+
};
|
|
194
|
+
var plugins = [];
|
|
195
|
+
function usePlugin(plugin) {
|
|
196
|
+
if (!plugins.find((p) => p.name === plugin.name))
|
|
197
|
+
plugins.push(plugin);
|
|
198
|
+
if (!plugin.mount)
|
|
199
|
+
plugin.mount = async function({ config }) {
|
|
200
|
+
if (plugin.onMount)
|
|
201
|
+
await plugin.onMount({
|
|
202
|
+
config,
|
|
203
|
+
event: {},
|
|
204
|
+
context: {},
|
|
205
|
+
logger: new import_logger.Logger(plugin.name)
|
|
206
|
+
}, async () => Promise.resolve());
|
|
207
|
+
};
|
|
208
|
+
return plugin;
|
|
209
|
+
}
|
|
210
|
+
function useFunc(handler) {
|
|
211
|
+
plugins = [];
|
|
212
|
+
const invokeHandler = handler();
|
|
213
|
+
const func = new Func({
|
|
214
|
+
plugins,
|
|
215
|
+
handler: invokeHandler
|
|
216
|
+
});
|
|
217
|
+
plugins = [];
|
|
218
|
+
return func;
|
|
219
|
+
}
|
|
220
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
221
|
+
0 && (module.exports = {
|
|
222
|
+
Func,
|
|
223
|
+
useFunc,
|
|
224
|
+
usePlugin
|
|
225
|
+
});
|