forked from GoogleCloudPlatform/functions-framework-nodejs
-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathplugin.ts
226 lines (200 loc) · 5.93 KB
/
plugin.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
/* eslint-disable @typescript-eslint/no-unused-vars */
import * as Debug from 'debug';
import {get, invoke, isEmpty, omit, transform, trim} from 'lodash';
import {OpenFunctionRuntime} from './runtime';
const debug = Debug('ofn:plugin');
/**
* Defining an abstract class to represent Plugin.
* @public
**/
export class Plugin {
/**
* Name of the plugin.
*/
readonly name: string;
/**
* Version of the plugin.
*/
readonly version: string;
/**
* Constructor of the OpenFunction plugin.
*/
constructor(name: string, version = 'unknown') {
if (!trim(name)) {
throw new Error('Plugin name must be specified.');
}
this.name = name;
this.version = version;
}
/**
* Get the value of a property on the plugin.
* @param prop - The property to get.
* @returns The value of the property.
*/
get(prop: string) {
return get(this, prop);
}
/**
* This function is called before the user function is executed.
* @param ctx - Object that contains information about the function that is being executed.
* @param plugins - The collection of loaded pre and post hook plugins.
*/
async execPreHook(
ctx: OpenFunctionRuntime | null,
plugins: Record<string, Plugin>
) {
console.warn(
`Plugin "${this.name}" has not implemented pre hook function.`
);
}
/**
* This function is called after the user function is executed.
* @param ctx - Object that contains information about the function that is being executed.
* @param plugins - The collection of loaded pre and post hook plugins.
*/
async execPostHook(
ctx: OpenFunctionRuntime | null,
plugins: Record<string, Plugin>
) {
console.warn(
`Plugin "${this.name}" has not implemented post hook function.`
);
}
}
/**
* PluginMap type definition.
*/
export type PluginMap = Record<string, Plugin> & {_seq?: string[]};
enum PluginStoreType {
BUILTIN = 1,
CUSTOM,
}
/**
* Initializes a store object for PluginStore singleton class
* with the keys of the PluginStoreType enum and the values of an empty object.
**/
const stores = transform(
PluginStoreType,
(r, k, v) => {
r[v] = {_seq: []} as {} as PluginMap;
},
<Record<string, PluginMap>>{}
);
/**
* PluginStore is a class that manages a collection of plugins.
**/
export class PluginStore {
/**
* Type of the plugin store.
*/
static Type = PluginStoreType;
/**
* Singleton helper method to create PluginStore instance.
* @param type - PluginStoreType - The type of plugin store you want to create.
* @returns A new instance of the PluginStore class.
*/
static Instance(type = PluginStore.Type.CUSTOM): PluginStore {
return new PluginStore(type);
}
/**
* Internal store type.
*/
#type = PluginStoreType.CUSTOM;
/**
* Internal store object.
*/
readonly #store: PluginMap | null = null;
/**
* Private constructor of PluginStore.
* @param type - PluginStoreType - The type of store you want to use.
*/
private constructor(type: PluginStoreType) {
if (!this.#store) {
this.#type = type;
this.#store = stores[type];
}
}
/**
* Adds a plugin to the store.
* @param plugin - Plugin - The plugin to register.
*/
register(plugin: Plugin) {
this.#store![plugin.name] = plugin;
this.#store!._seq?.push(plugin.name);
}
/**
* Removes a plugin from the store.
* @param plugin - Plugin - The plugin to register.
*/
unregister(plugin: Plugin) {
delete this.#store![plugin.name];
omit(this.#store!._seq, plugin.name);
}
/**
* Return the plugin with the given name from the store.
* @param name - The name of the plugin.
* @returns The plugin object.
*/
get(name: string): Plugin {
return this.#store![name];
}
/**
* Getter that tells whether the store is custom type.
* @returns `true` if the `#type` is `PluginStoreType.CUSTOM`.
*/
get #isCustomStore(): boolean {
return this.#type === PluginStoreType.CUSTOM;
}
/**
* It invokes the `execPreHook` function of each plugin in the order specified by the `seq` array
* @param ctx - The context object that is passed to the plugin.
* @param [seq] - The sequence of plugins to be executed. If not specified, all plugins will be executed.
*/
async execPreHooks(ctx: OpenFunctionRuntime | null, seq?: string[]) {
await this.#invokePluginBySeq(
ctx,
'execPreHook',
seq || (this.#isCustomStore && get(ctx, 'prePlugins')) || []
);
}
/**
* It invokes the `execPostHook` function of each plugin in the order specified by the `seq` array
* @param ctx - The context object that is passed to the plugin.
* @param [seq] - The sequence of plugins to be executed. If not specified, all plugins will be executed.
*/
async execPostHooks(ctx: OpenFunctionRuntime | null, seq?: string[]) {
await this.#invokePluginBySeq(
ctx,
'execPostHook',
seq || (this.#isCustomStore && get(ctx, 'postPlugins')) || []
);
}
/**
* It invokes a method on each plugin in the sequence.
* @param ctx - OpenFunctionRuntime context object.
* @param method - The method to invoke on the plugin.
* @param [seq] - The sequence of plugins to invoke. If not provided, the default sequence will be used.
*/
async #invokePluginBySeq(
ctx: OpenFunctionRuntime | null,
method: keyof Plugin,
seq: string[]
) {
const pluginNames = !isEmpty(seq) ? seq : this.#store!._seq ?? [];
const plugins = this.#store!;
for (const pluginName of pluginNames) {
const plugin = plugins[pluginName];
debug('Executing "%s" of plugin "%s"', method, pluginName);
// Try to invoke the plugin method and catch exceptions
try {
await invoke(plugin, method, ctx, plugins);
} catch (ex) {
const err = <Error>ex;
console.error(
`Failed to invoke "${method}" of plugin "${pluginName}"` +
`\nDetailed stack trace: ${err.stack}`
);
}
}
}
}