forked from GoogleCloudPlatform/functions-framework-nodejs
-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathcontext.ts
214 lines (206 loc) · 4.38 KB
/
context.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
/**
* The OpenFunction's serving context.
* @public
*/
export interface OpenFunctionContext {
/**
* The name of the context.
*/
name: string;
/**
* The version of the context.
*/
version: string;
/**
* The target runtime of the context.
*/
runtime:
| `${RuntimeType}`
| `${Capitalize<RuntimeType>}`
| `${Uppercase<RuntimeType>}`;
/**
* Optional port string of the server.
*/
port?: string;
/**
* Optional input binding object.
*/
inputs?: Record<string, OpenFunctionComponent>;
/**
* Optional output binding object.
*/
outputs?: Record<string, OpenFunctionComponent>;
/**
* Optional state store config;
*/
states?: Record<string, OpenFunctionComponent>;
/**
* Optional plugins to be executed before user function.
*/
prePlugins?: string[];
/**
* Optional plugins to be executed after user function.
*/
postPlugins?: string[];
/**
* Optional trace plugin config.
*/
pluginsTracing?: TraceConfig;
}
/**
* The component interface of the context.
* @public
*/
export interface OpenFunctionComponent {
/**
* The name of the component.
*/
componentName: string;
/**
* The type of the component.
*/
componentType: `${ComponentType}.${string}`;
/**
* The uri of the component.
*/
uri?: string;
/**
* Optional operation of the component.
*/
operation?: string;
/**
* Optional metadata as hash map for the component.
*/
metadata?: Record<string, string>;
}
/**
* Defining runtime type enumeration.
* @public
*/
export enum RuntimeType {
/**
* The Knative type.
*/
Knative = 'knative',
/**
* The async type.
*/
Async = 'async',
}
/**
* Defining component type enumeration.
* @public
*/
export enum ComponentType {
/**
* The binding type.
*/
Binding = 'bindings',
/**
* The pubsub type.
*/
PubSub = 'pubsub',
/**
* The state type
*/
State = 'state',
}
/**
* Provides a set of methods to help determine types used in FunctionContext.
* @public
*/
export class ContextUtils {
/**
* Returns true if the runtime is Knative.
* @param context - The OpenFunctionContext object.
* @returns A boolean value.
*/
static IsKnativeRuntime(context: OpenFunctionContext): boolean {
return context?.runtime?.toLowerCase() === RuntimeType.Knative;
}
/**
* Returns true if the runtime is Async.
* @param context - The OpenFunctionContext object.
* @returns A boolean value.
*/
static IsAsyncRuntime(context: OpenFunctionContext): boolean {
return context?.runtime?.toLowerCase() === RuntimeType.Async;
}
/**
* Checks if the component is a binding component.
* @param component - The component to check.
* @returns A boolean value.
*/
static IsBindingComponent(component: OpenFunctionComponent): boolean {
return component?.componentType.split('.')[0] === ComponentType.Binding;
}
/**
* Checks if the component is a pubsub component.
* @param component - The component to check.
* @returns A boolean value.
*/
static IsPubSubComponent(component: OpenFunctionComponent): boolean {
return component?.componentType.split('.')[0] === ComponentType.PubSub;
}
/**
* Checks if the component is a state component.
* @param component - The component to check.
* @returns A boolean value.
*/
static IsStateComponent(component: OpenFunctionComponent): boolean {
return component?.componentType.split('.')[0] === ComponentType.State;
}
}
/**
* The config of the trace plugin.
* @public
*/
export interface TraceConfig {
/**
* Switch of the tracer
*/
enabled: boolean;
/**
* Provider of the tracer
*/
provider: TraceProvider;
/**
* Optional tags of the tracer
*/
tags?: Record<string, string> & {func?: string};
/**
* Optional baggage of the tracer
*/
baggage?: Record<string, string>;
}
/**
* The trace provider object.
* @public
*/
export interface TraceProvider {
/**
* The name of the provider.
*/
name:
| `${TraceProviderType}`
| `${Capitalize<TraceProviderType>}`
| `${Uppercase<TraceProviderType>}`;
/**
* The address of the OAP server.
*/
oapServer: string;
}
/**
* Defining trace provider type enumeration.
* @public
*/
export enum TraceProviderType {
/**
* The SkyWalking type.
*/
SkyWalking = 'skywalking',
/**
* The OpenTelemetry type.
*/
OpenTelemetry = 'opentelemetry',
}