@hahnpro/flow-sdk 4.14.4
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/LICENSE +21 -0
- package/README.md +3 -0
- package/dist/FlowApplication.d.ts +35 -0
- package/dist/FlowApplication.js +270 -0
- package/dist/FlowElement.d.ts +50 -0
- package/dist/FlowElement.js +143 -0
- package/dist/FlowEvent.d.ts +19 -0
- package/dist/FlowEvent.js +71 -0
- package/dist/FlowLogger.d.ts +23 -0
- package/dist/FlowLogger.js +57 -0
- package/dist/FlowModule.d.ts +7 -0
- package/dist/FlowModule.js +14 -0
- package/dist/RpcClient.d.ts +11 -0
- package/dist/RpcClient.js +70 -0
- package/dist/TestModule.d.ts +2 -0
- package/dist/TestModule.js +27 -0
- package/dist/amqp.d.ts +33 -0
- package/dist/amqp.js +12 -0
- package/dist/api/Queue.d.ts +15 -0
- package/dist/api/Queue.js +27 -0
- package/dist/api/api.d.ts +62 -0
- package/dist/api/api.js +52 -0
- package/dist/api/asset.interface.d.ts +54 -0
- package/dist/api/asset.interface.js +2 -0
- package/dist/api/asset.service.d.ts +10 -0
- package/dist/api/asset.service.js +21 -0
- package/dist/api/assettypes.service.d.ts +6 -0
- package/dist/api/assettypes.service.js +10 -0
- package/dist/api/content.interface.d.ts +35 -0
- package/dist/api/content.interface.js +12 -0
- package/dist/api/content.service.d.ts +17 -0
- package/dist/api/content.service.js +39 -0
- package/dist/api/data.interface.d.ts +29 -0
- package/dist/api/data.interface.js +2 -0
- package/dist/api/data.service.d.ts +15 -0
- package/dist/api/data.service.js +49 -0
- package/dist/api/endpoint.interface.d.ts +22 -0
- package/dist/api/endpoint.interface.js +2 -0
- package/dist/api/endpoint.service.d.ts +8 -0
- package/dist/api/endpoint.service.js +17 -0
- package/dist/api/events.interface.d.ts +16 -0
- package/dist/api/events.interface.js +2 -0
- package/dist/api/events.service.d.ts +7 -0
- package/dist/api/events.service.js +13 -0
- package/dist/api/http.service.d.ts +26 -0
- package/dist/api/http.service.js +74 -0
- package/dist/api/index.d.ts +12 -0
- package/dist/api/index.js +16 -0
- package/dist/api/mock/api.mock.d.ts +158 -0
- package/dist/api/mock/api.mock.js +111 -0
- package/dist/api/mock/asset.mock.service.d.ts +12 -0
- package/dist/api/mock/asset.mock.service.js +21 -0
- package/dist/api/mock/assetTypes.mock.service.d.ts +6 -0
- package/dist/api/mock/assetTypes.mock.service.js +11 -0
- package/dist/api/mock/content.mock.service.d.ts +18 -0
- package/dist/api/mock/content.mock.service.js +64 -0
- package/dist/api/mock/data.mock.service.d.ts +13 -0
- package/dist/api/mock/data.mock.service.js +52 -0
- package/dist/api/mock/endpoint.mock.service.d.ts +15 -0
- package/dist/api/mock/endpoint.mock.service.js +24 -0
- package/dist/api/mock/events.mock.service.d.ts +7 -0
- package/dist/api/mock/events.mock.service.js +14 -0
- package/dist/api/mock/index.d.ts +9 -0
- package/dist/api/mock/index.js +12 -0
- package/dist/api/mock/secret.mock.service.d.ts +6 -0
- package/dist/api/mock/secret.mock.service.js +11 -0
- package/dist/api/mock/task.mock.service.d.ts +7 -0
- package/dist/api/mock/task.mock.service.js +15 -0
- package/dist/api/mock/timeseries.mock.service.d.ts +19 -0
- package/dist/api/mock/timeseries.mock.service.js +69 -0
- package/dist/api/mock/user.mock.service.d.ts +7 -0
- package/dist/api/mock/user.mock.service.js +14 -0
- package/dist/api/proxy.service.d.ts +11 -0
- package/dist/api/proxy.service.js +17 -0
- package/dist/api/secret.interface.d.ts +7 -0
- package/dist/api/secret.interface.js +2 -0
- package/dist/api/secret.service.d.ts +6 -0
- package/dist/api/secret.service.js +10 -0
- package/dist/api/sidriveiq.interface.d.ts +104 -0
- package/dist/api/sidriveiq.interface.js +2 -0
- package/dist/api/sidriveiq.service.d.ts +97 -0
- package/dist/api/sidriveiq.service.js +97 -0
- package/dist/api/task.interface.d.ts +22 -0
- package/dist/api/task.interface.js +2 -0
- package/dist/api/task.service.d.ts +7 -0
- package/dist/api/task.service.js +13 -0
- package/dist/api/timeseries.interface.d.ts +39 -0
- package/dist/api/timeseries.interface.js +2 -0
- package/dist/api/timeseries.service.d.ts +17 -0
- package/dist/api/timeseries.service.js +38 -0
- package/dist/api/user.service.d.ts +6 -0
- package/dist/api/user.service.js +21 -0
- package/dist/extra-validators.d.ts +1 -0
- package/dist/extra-validators.js +52 -0
- package/dist/flow.interface.d.ts +41 -0
- package/dist/flow.interface.js +2 -0
- package/dist/index.d.ts +11 -0
- package/dist/index.js +16 -0
- package/dist/rpc_server.py +115 -0
- package/dist/unit-decorators.d.ts +39 -0
- package/dist/unit-decorators.js +157 -0
- package/dist/unit-utils.d.ts +8 -0
- package/dist/unit-utils.js +143 -0
- package/dist/units.d.ts +31 -0
- package/dist/units.js +570 -0
- package/dist/utils.d.ts +8 -0
- package/dist/utils.js +106 -0
- package/package.json +64 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2020 Hahn PRO
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
import 'reflect-metadata';
|
|
2
|
+
import type { CloudEvent } from 'cloudevents';
|
|
3
|
+
import { PartialObserver } from 'rxjs';
|
|
4
|
+
import { AmqpConnection, Nack } from './amqp';
|
|
5
|
+
import { API } from './api';
|
|
6
|
+
import type { ClassType, Flow, FlowElementContext } from './flow.interface';
|
|
7
|
+
import type { FlowEvent } from './FlowEvent';
|
|
8
|
+
import { Logger } from './FlowLogger';
|
|
9
|
+
import { RpcClient } from './RpcClient';
|
|
10
|
+
export declare class FlowApplication {
|
|
11
|
+
private amqpConnection?;
|
|
12
|
+
api: API;
|
|
13
|
+
private context;
|
|
14
|
+
private declarations;
|
|
15
|
+
private elements;
|
|
16
|
+
private logger;
|
|
17
|
+
private properties;
|
|
18
|
+
private _rpcClient;
|
|
19
|
+
private outputStreamMap;
|
|
20
|
+
constructor(modules: ClassType<any>[], flow: Flow, logger?: Logger, amqpConnection?: AmqpConnection, skipApi?: boolean);
|
|
21
|
+
subscribe: (streamId: string, observer: PartialObserver<FlowEvent>) => import("rxjs").Subscription;
|
|
22
|
+
emit: (event: FlowEvent) => void;
|
|
23
|
+
emitPartial: (completeEvent: FlowEvent, partialEvent: FlowEvent) => void;
|
|
24
|
+
getProperties(): Record<string, any>;
|
|
25
|
+
onMessage: (event: CloudEvent) => Promise<Nack | undefined>;
|
|
26
|
+
publishEvent: (event: FlowEvent) => Promise<void>;
|
|
27
|
+
get rpcClient(): RpcClient;
|
|
28
|
+
destroy(exitCode?: number): Promise<void>;
|
|
29
|
+
private getOutputStream;
|
|
30
|
+
private truncate;
|
|
31
|
+
}
|
|
32
|
+
export interface Context extends FlowElementContext {
|
|
33
|
+
app?: FlowApplication;
|
|
34
|
+
logger?: Logger;
|
|
35
|
+
}
|
|
@@ -0,0 +1,270 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.FlowApplication = void 0;
|
|
4
|
+
const tslib_1 = require("tslib");
|
|
5
|
+
require("reflect-metadata");
|
|
6
|
+
const object_sizeof_1 = (0, tslib_1.__importDefault)(require("object-sizeof"));
|
|
7
|
+
const rxjs_1 = require("rxjs");
|
|
8
|
+
const operators_1 = require("rxjs/operators");
|
|
9
|
+
const util_1 = require("util");
|
|
10
|
+
const uuid_1 = require("uuid");
|
|
11
|
+
const amqp_1 = require("./amqp");
|
|
12
|
+
const api_1 = require("./api");
|
|
13
|
+
const FlowLogger_1 = require("./FlowLogger");
|
|
14
|
+
const RpcClient_1 = require("./RpcClient");
|
|
15
|
+
const MAX_EVENT_SIZE_BYTES = 512 * 1024;
|
|
16
|
+
class FlowApplication {
|
|
17
|
+
constructor(modules, flow, logger, amqpConnection, skipApi) {
|
|
18
|
+
var _a, _b, _c;
|
|
19
|
+
this.amqpConnection = amqpConnection;
|
|
20
|
+
this.declarations = {};
|
|
21
|
+
this.elements = {};
|
|
22
|
+
this.outputStreamMap = {};
|
|
23
|
+
this.subscribe = (streamId, observer) => this.getOutputStream(streamId).subscribe(observer);
|
|
24
|
+
this.emit = (event) => {
|
|
25
|
+
if (event) {
|
|
26
|
+
try {
|
|
27
|
+
this.publishEvent(event);
|
|
28
|
+
this.getOutputStream(event.getStreamId()).next(event);
|
|
29
|
+
}
|
|
30
|
+
catch (err) {
|
|
31
|
+
this.logger.error(err);
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
};
|
|
35
|
+
this.emitPartial = (completeEvent, partialEvent) => {
|
|
36
|
+
try {
|
|
37
|
+
if (completeEvent) {
|
|
38
|
+
this.getOutputStream(completeEvent.getStreamId()).next(completeEvent);
|
|
39
|
+
}
|
|
40
|
+
if (partialEvent) {
|
|
41
|
+
this.publishEvent(partialEvent);
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
catch (err) {
|
|
45
|
+
this.logger.error(err);
|
|
46
|
+
}
|
|
47
|
+
};
|
|
48
|
+
this.onMessage = async (event) => {
|
|
49
|
+
var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l;
|
|
50
|
+
if (event.type === 'com.flowstudio.deployment.update') {
|
|
51
|
+
try {
|
|
52
|
+
const flow = event.data;
|
|
53
|
+
if (!flow) {
|
|
54
|
+
return new amqp_1.Nack(false);
|
|
55
|
+
}
|
|
56
|
+
let context = {};
|
|
57
|
+
if (flow.context) {
|
|
58
|
+
this.context = Object.assign(Object.assign({}, this.context), flow.context);
|
|
59
|
+
context = this.context;
|
|
60
|
+
}
|
|
61
|
+
if (flow.properties) {
|
|
62
|
+
this.properties = flow.properties;
|
|
63
|
+
for (const element of Object.values(this.elements)) {
|
|
64
|
+
(_a = element.onFlowPropertiesChanged) === null || _a === void 0 ? void 0 : _a.call(element, flow.properties);
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
if (Object.keys(context).length > 0) {
|
|
68
|
+
for (const element of flow.elements || []) {
|
|
69
|
+
context = Object.assign(Object.assign({}, context), { name: element.name });
|
|
70
|
+
(_c = (_b = this.elements) === null || _b === void 0 ? void 0 : _b[element.id]) === null || _c === void 0 ? void 0 : _c.onContextChanged(context);
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
for (const element of flow.elements || []) {
|
|
74
|
+
(_e = (_d = this.elements) === null || _d === void 0 ? void 0 : _d[element.id]) === null || _e === void 0 ? void 0 : _e.onPropertiesChanged(element.properties);
|
|
75
|
+
}
|
|
76
|
+
const statusEvent = {
|
|
77
|
+
eventId: (0, uuid_1.v4)(),
|
|
78
|
+
eventTime: new Date().toISOString(),
|
|
79
|
+
eventType: 'com.hahnpro.event.health',
|
|
80
|
+
contentType: 'application/json',
|
|
81
|
+
data: { deploymentId: this.context.deploymentId, status: 'updated' },
|
|
82
|
+
};
|
|
83
|
+
(_f = this.amqpConnection) === null || _f === void 0 ? void 0 : _f.publish('deployment', 'health', statusEvent).catch((err) => this.logger.error(err));
|
|
84
|
+
}
|
|
85
|
+
catch (err) {
|
|
86
|
+
this.logger.error(err);
|
|
87
|
+
const statusEvent = {
|
|
88
|
+
eventId: (0, uuid_1.v4)(),
|
|
89
|
+
eventTime: new Date().toISOString(),
|
|
90
|
+
eventType: 'com.hahnpro.event.health',
|
|
91
|
+
contentType: 'application/json',
|
|
92
|
+
data: { deploymentId: this.context.deploymentId, status: 'updating failed' },
|
|
93
|
+
};
|
|
94
|
+
(_g = this.amqpConnection) === null || _g === void 0 ? void 0 : _g.publish('deployment', 'health', statusEvent).catch((err) => this.logger.error(err));
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
else if (event.type === 'com.flowstudio.deployment.message') {
|
|
98
|
+
const data = event.data;
|
|
99
|
+
const elementId = data === null || data === void 0 ? void 0 : data.elementId;
|
|
100
|
+
if (elementId) {
|
|
101
|
+
(_k = (_j = (_h = this.elements) === null || _h === void 0 ? void 0 : _h[elementId]) === null || _j === void 0 ? void 0 : _j.onMessage) === null || _k === void 0 ? void 0 : _k.call(_j, data);
|
|
102
|
+
}
|
|
103
|
+
else {
|
|
104
|
+
for (const element of Object.values(this.elements)) {
|
|
105
|
+
(_l = element === null || element === void 0 ? void 0 : element.onMessage) === null || _l === void 0 ? void 0 : _l.call(element, data);
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
else if (event.type === 'com.flowstudio.deployment.destroy') {
|
|
110
|
+
this.destroy();
|
|
111
|
+
}
|
|
112
|
+
else {
|
|
113
|
+
return new amqp_1.Nack(false);
|
|
114
|
+
}
|
|
115
|
+
return undefined;
|
|
116
|
+
};
|
|
117
|
+
this.publishEvent = (event) => {
|
|
118
|
+
if (!this.amqpConnection) {
|
|
119
|
+
return;
|
|
120
|
+
}
|
|
121
|
+
try {
|
|
122
|
+
const message = event.format();
|
|
123
|
+
if ((0, object_sizeof_1.default)(message) > MAX_EVENT_SIZE_BYTES) {
|
|
124
|
+
message.data = this.truncate(message.data);
|
|
125
|
+
}
|
|
126
|
+
return this.amqpConnection.publish('flowlogs', '', message);
|
|
127
|
+
}
|
|
128
|
+
catch (err) {
|
|
129
|
+
this.logger.error(err);
|
|
130
|
+
}
|
|
131
|
+
};
|
|
132
|
+
this.logger = new FlowLogger_1.FlowLogger(Object.assign({ id: 'none', functionFqn: 'FlowApplication' }, flow === null || flow === void 0 ? void 0 : flow.context), logger || undefined, this.publishEvent);
|
|
133
|
+
process.on('uncaughtException', (err) => {
|
|
134
|
+
this.logger.error('Uncaught exception!');
|
|
135
|
+
this.logger.error(err);
|
|
136
|
+
this.destroy(1);
|
|
137
|
+
});
|
|
138
|
+
process.on('unhandledRejection', (reason) => {
|
|
139
|
+
this.logger.error('Unhandled promise rejection!');
|
|
140
|
+
this.logger.error(reason);
|
|
141
|
+
});
|
|
142
|
+
process.on('SIGTERM', () => {
|
|
143
|
+
this.logger.log('Flow Deployment is terminating');
|
|
144
|
+
this.destroy(0);
|
|
145
|
+
});
|
|
146
|
+
try {
|
|
147
|
+
if (skipApi !== true) {
|
|
148
|
+
this.api = new api_1.API();
|
|
149
|
+
}
|
|
150
|
+
}
|
|
151
|
+
catch (err) {
|
|
152
|
+
this.logger.error((err === null || err === void 0 ? void 0 : err.message) || err);
|
|
153
|
+
}
|
|
154
|
+
try {
|
|
155
|
+
this.context = Object.assign({}, flow.context);
|
|
156
|
+
this.properties = flow.properties || {};
|
|
157
|
+
for (const module of modules) {
|
|
158
|
+
const moduleName = Reflect.getMetadata('module:name', module);
|
|
159
|
+
const moduleDeclarations = Reflect.getMetadata('module:declarations', module);
|
|
160
|
+
if (!moduleName || !moduleDeclarations || !Array.isArray(moduleDeclarations)) {
|
|
161
|
+
throw new Error(`FlowModule (${module.name}) metadata is missing or invalid`);
|
|
162
|
+
}
|
|
163
|
+
for (const declaration of moduleDeclarations) {
|
|
164
|
+
const functionFqn = Reflect.getMetadata('element:functionFqn', declaration);
|
|
165
|
+
if (!functionFqn) {
|
|
166
|
+
throw new Error(`FlowFunction (${declaration.name}) metadata is missing or invalid`);
|
|
167
|
+
}
|
|
168
|
+
this.declarations[`${moduleName}.${functionFqn}`] = declaration;
|
|
169
|
+
}
|
|
170
|
+
}
|
|
171
|
+
for (const element of flow.elements) {
|
|
172
|
+
const { id, name, properties, module, functionFqn } = element;
|
|
173
|
+
try {
|
|
174
|
+
const context = Object.assign(Object.assign({}, this.context), { id, name, logger, app: this });
|
|
175
|
+
this.elements[id] = new this.declarations[`${module}.${functionFqn}`](context, properties);
|
|
176
|
+
}
|
|
177
|
+
catch (err) {
|
|
178
|
+
throw new Error(`Could not create FlowElement for ${module}.${functionFqn}`);
|
|
179
|
+
}
|
|
180
|
+
}
|
|
181
|
+
for (const connection of flow.connections) {
|
|
182
|
+
const { source, target, sourceStream = 'default', targetStream = 'default' } = connection;
|
|
183
|
+
if (!source || !target) {
|
|
184
|
+
continue;
|
|
185
|
+
}
|
|
186
|
+
const streamId = `${source}.${sourceStream}`;
|
|
187
|
+
const element = this.elements[target];
|
|
188
|
+
if (!element || !element.constructor) {
|
|
189
|
+
throw new Error(target + ' has not been initialized');
|
|
190
|
+
}
|
|
191
|
+
const streamHandler = Reflect.getMetadata(`stream:${targetStream}`, element.constructor);
|
|
192
|
+
if (!streamHandler || !element[streamHandler]) {
|
|
193
|
+
throw new Error(`${target} does not implement a handler for ${targetStream}`);
|
|
194
|
+
}
|
|
195
|
+
const streamOptions = Reflect.getMetadata(`stream:options:${targetStream}`, element.constructor) || {};
|
|
196
|
+
const concurrent = streamOptions.concurrent || 1;
|
|
197
|
+
const outputStream = this.getOutputStream(streamId);
|
|
198
|
+
outputStream
|
|
199
|
+
.pipe((0, operators_1.mergeMap)((event) => element[streamHandler](event), concurrent), (0, operators_1.catchError)((err, observable) => {
|
|
200
|
+
element.handleApiError(err);
|
|
201
|
+
return observable;
|
|
202
|
+
}))
|
|
203
|
+
.subscribe();
|
|
204
|
+
}
|
|
205
|
+
(_c = (_b = (_a = this.amqpConnection) === null || _a === void 0 ? void 0 : _a.managedChannel.assertExchange('deployment', 'direct', { durable: true })) === null || _b === void 0 ? void 0 : _b.then(() => this.amqpConnection.managedChannel.assertExchange('flowlogs', 'fanout', { durable: true }))) === null || _c === void 0 ? void 0 : _c.then(() => this.amqpConnection.createSubscriber((msg) => this.onMessage(msg), {
|
|
206
|
+
exchange: 'deployment',
|
|
207
|
+
routingKey: this.context.deploymentId,
|
|
208
|
+
queueOptions: { durable: false, exclusive: true },
|
|
209
|
+
})).catch((error) => {
|
|
210
|
+
this.logger.error('could not assert Exchange!\nError:\n' + error.toString());
|
|
211
|
+
});
|
|
212
|
+
this.logger.log('Flow Deployment is running');
|
|
213
|
+
}
|
|
214
|
+
catch (err) {
|
|
215
|
+
this.logger.error(err);
|
|
216
|
+
this.destroy(1);
|
|
217
|
+
}
|
|
218
|
+
}
|
|
219
|
+
getProperties() {
|
|
220
|
+
return this.properties;
|
|
221
|
+
}
|
|
222
|
+
get rpcClient() {
|
|
223
|
+
var _a;
|
|
224
|
+
if (!((_a = this.amqpConnection) === null || _a === void 0 ? void 0 : _a.managedConnection)) {
|
|
225
|
+
throw new Error('No AMQP connection available');
|
|
226
|
+
}
|
|
227
|
+
if (!this._rpcClient) {
|
|
228
|
+
this._rpcClient = new RpcClient_1.RpcClient(this.amqpConnection.managedConnection);
|
|
229
|
+
}
|
|
230
|
+
return this._rpcClient;
|
|
231
|
+
}
|
|
232
|
+
async destroy(exitCode = 0) {
|
|
233
|
+
var _a, _b, _c, _d;
|
|
234
|
+
try {
|
|
235
|
+
try {
|
|
236
|
+
for (const element of Object.values(this.elements)) {
|
|
237
|
+
(_a = element === null || element === void 0 ? void 0 : element.onDestroy) === null || _a === void 0 ? void 0 : _a.call(element);
|
|
238
|
+
}
|
|
239
|
+
await ((_b = this._rpcClient) === null || _b === void 0 ? void 0 : _b.close());
|
|
240
|
+
}
|
|
241
|
+
catch (err) {
|
|
242
|
+
this.logger.error(err);
|
|
243
|
+
}
|
|
244
|
+
await new Promise((res) => setTimeout(res, 250));
|
|
245
|
+
await ((_d = (_c = this.amqpConnection) === null || _c === void 0 ? void 0 : _c.managedConnection) === null || _d === void 0 ? void 0 : _d.close());
|
|
246
|
+
}
|
|
247
|
+
catch (err) {
|
|
248
|
+
console.error(err);
|
|
249
|
+
}
|
|
250
|
+
finally {
|
|
251
|
+
process.exit(exitCode);
|
|
252
|
+
}
|
|
253
|
+
}
|
|
254
|
+
getOutputStream(id) {
|
|
255
|
+
const stream = this.outputStreamMap[id];
|
|
256
|
+
if (!stream) {
|
|
257
|
+
this.outputStreamMap[id] = new rxjs_1.Subject();
|
|
258
|
+
return this.outputStreamMap[id];
|
|
259
|
+
}
|
|
260
|
+
return stream;
|
|
261
|
+
}
|
|
262
|
+
truncate(msg) {
|
|
263
|
+
let truncated = (0, util_1.inspect)(msg, { depth: 4, maxStringLength: 1000 });
|
|
264
|
+
if (truncated.startsWith("'") && truncated.endsWith("'")) {
|
|
265
|
+
truncated = truncated.substring(1, truncated.length - 1);
|
|
266
|
+
}
|
|
267
|
+
return truncated;
|
|
268
|
+
}
|
|
269
|
+
}
|
|
270
|
+
exports.FlowApplication = FlowApplication;
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
import { PythonShell } from 'python-shell';
|
|
2
|
+
import { API } from './api';
|
|
3
|
+
import { ClassType, DeploymentMessage, FlowContext, FlowElementContext } from './flow.interface';
|
|
4
|
+
import { Context } from './FlowApplication';
|
|
5
|
+
import { FlowEvent } from './FlowEvent';
|
|
6
|
+
import { FlowLogger } from './FlowLogger';
|
|
7
|
+
export declare abstract class FlowElement<T = any> {
|
|
8
|
+
private readonly propertiesClassType?;
|
|
9
|
+
private readonly whitelist;
|
|
10
|
+
readonly functionFqn: string;
|
|
11
|
+
protected readonly api?: API;
|
|
12
|
+
protected readonly logger: FlowLogger;
|
|
13
|
+
protected metadata: FlowElementContext;
|
|
14
|
+
protected properties: T;
|
|
15
|
+
private readonly app?;
|
|
16
|
+
private readonly rpcRoutingKey;
|
|
17
|
+
private stopPropagateStream;
|
|
18
|
+
constructor({ app, logger, ...metadata }: Context, properties?: unknown, propertiesClassType?: ClassType<T>, whitelist?: boolean);
|
|
19
|
+
get flowProperties(): Record<string, any>;
|
|
20
|
+
onDestroy?: () => void;
|
|
21
|
+
onMessage?: (message: DeploymentMessage) => void;
|
|
22
|
+
onFlowPropertiesChanged?: (properties: Record<string, any>) => void;
|
|
23
|
+
onContextChanged: (context: Partial<FlowContext>) => void;
|
|
24
|
+
onPropertiesChanged: (properties: T) => void;
|
|
25
|
+
protected setProperties: (properties: T) => void;
|
|
26
|
+
handleApiError: (error: any) => void;
|
|
27
|
+
/**
|
|
28
|
+
* @deprecated since version 4.8.0, will be removed in 5.0.0, use emitEvent(...) instead
|
|
29
|
+
*/
|
|
30
|
+
protected emitOutput(data?: any, outputId?: string, time?: Date): FlowEvent;
|
|
31
|
+
protected emitEvent(data: any, inputEvent: FlowEvent, outputId?: string, time?: Date): FlowEvent;
|
|
32
|
+
protected validateProperties<P>(classType: ClassType<P>, properties?: any, whitelist?: boolean): P;
|
|
33
|
+
protected validateEventData<E>(classType: ClassType<E>, event: FlowEvent, whitelist?: boolean): E;
|
|
34
|
+
protected interpolate: (value: any, ...templateVariables: any) => any;
|
|
35
|
+
protected callRpcFunction(functionName: string, ...args: any[]): Promise<unknown>;
|
|
36
|
+
protected runPyRpcScript(scriptPath: string, ...args: (string | boolean | number)[]): PythonShell;
|
|
37
|
+
}
|
|
38
|
+
export declare function InputStream(id?: string, options?: {
|
|
39
|
+
concurrent?: number;
|
|
40
|
+
stopPropagation?: boolean;
|
|
41
|
+
}): MethodDecorator;
|
|
42
|
+
export declare function FlowFunction(fqn: string): ClassDecorator;
|
|
43
|
+
export declare abstract class FlowResource<T = any> extends FlowElement<T> {
|
|
44
|
+
}
|
|
45
|
+
export declare abstract class FlowTask<T = any> extends FlowElement<T> {
|
|
46
|
+
}
|
|
47
|
+
export declare abstract class FlowTrigger<T = any> extends FlowElement<T> {
|
|
48
|
+
}
|
|
49
|
+
export declare abstract class FlowDashboard<T = any> extends FlowResource<T> {
|
|
50
|
+
}
|
|
@@ -0,0 +1,143 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.FlowDashboard = exports.FlowTrigger = exports.FlowTask = exports.FlowResource = exports.FlowFunction = exports.InputStream = exports.FlowElement = void 0;
|
|
4
|
+
const tslib_1 = require("tslib");
|
|
5
|
+
const class_transformer_1 = require("class-transformer");
|
|
6
|
+
const class_validator_1 = require("class-validator");
|
|
7
|
+
const python_shell_1 = require("python-shell");
|
|
8
|
+
const FlowEvent_1 = require("./FlowEvent");
|
|
9
|
+
const FlowLogger_1 = require("./FlowLogger");
|
|
10
|
+
const utils_1 = require("./utils");
|
|
11
|
+
class FlowElement {
|
|
12
|
+
constructor(_a, properties, propertiesClassType, whitelist = false) {
|
|
13
|
+
var _b, _c;
|
|
14
|
+
var { app, logger } = _a, metadata = (0, tslib_1.__rest)(_a, ["app", "logger"]);
|
|
15
|
+
this.propertiesClassType = propertiesClassType;
|
|
16
|
+
this.whitelist = whitelist;
|
|
17
|
+
this.stopPropagateStream = new Map();
|
|
18
|
+
this.onContextChanged = (context) => {
|
|
19
|
+
this.metadata = Object.assign(Object.assign({}, this.metadata), context);
|
|
20
|
+
};
|
|
21
|
+
this.onPropertiesChanged = (properties) => {
|
|
22
|
+
this.setProperties(properties);
|
|
23
|
+
};
|
|
24
|
+
this.setProperties = (properties) => {
|
|
25
|
+
if (this.propertiesClassType) {
|
|
26
|
+
this.properties = this.validateProperties(this.propertiesClassType, properties, this.whitelist);
|
|
27
|
+
}
|
|
28
|
+
else {
|
|
29
|
+
this.properties = properties;
|
|
30
|
+
}
|
|
31
|
+
};
|
|
32
|
+
this.handleApiError = (error) => (0, utils_1.handleApiError)(error, this.logger);
|
|
33
|
+
this.interpolate = (value, ...templateVariables) => (0, utils_1.fillTemplate)(value, ...templateVariables);
|
|
34
|
+
this.app = app;
|
|
35
|
+
this.api = (_b = this.app) === null || _b === void 0 ? void 0 : _b.api;
|
|
36
|
+
this.metadata = Object.assign(Object.assign({}, metadata), { functionFqn: this.functionFqn });
|
|
37
|
+
this.logger = new FlowLogger_1.FlowLogger(this.metadata, logger || undefined, (_c = this.app) === null || _c === void 0 ? void 0 : _c.publishEvent);
|
|
38
|
+
this.rpcRoutingKey = (this.metadata.flowId || '') + (this.metadata.deploymentId || '') + this.metadata.id;
|
|
39
|
+
if (properties) {
|
|
40
|
+
this.setProperties(properties);
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
get flowProperties() {
|
|
44
|
+
var _a, _b;
|
|
45
|
+
return ((_b = (_a = this.app) === null || _a === void 0 ? void 0 : _a.getProperties) === null || _b === void 0 ? void 0 : _b.call(_a)) || {};
|
|
46
|
+
}
|
|
47
|
+
emitOutput(data = {}, outputId = 'default', time = new Date()) {
|
|
48
|
+
return this.emitEvent(data, null, outputId, time);
|
|
49
|
+
}
|
|
50
|
+
emitEvent(data, inputEvent, outputId = 'default', time = new Date()) {
|
|
51
|
+
var _a, _b, _c;
|
|
52
|
+
const partialEvent = new FlowEvent_1.FlowEvent(this.metadata, data, outputId, time);
|
|
53
|
+
const completeEvent = new FlowEvent_1.FlowEvent(this.metadata, Object.assign(Object.assign({}, ((inputEvent === null || inputEvent === void 0 ? void 0 : inputEvent.getData()) || {})), data), outputId, time);
|
|
54
|
+
const streamID = ((_a = inputEvent === null || inputEvent === void 0 ? void 0 : inputEvent.getMetadata()) === null || _a === void 0 ? void 0 : _a.inputStreamId) || '';
|
|
55
|
+
if ((this.stopPropagateStream.has(streamID) && this.stopPropagateStream.get(streamID)) || !this.stopPropagateStream.has(streamID)) {
|
|
56
|
+
(_b = this.app) === null || _b === void 0 ? void 0 : _b.emit(partialEvent);
|
|
57
|
+
return partialEvent;
|
|
58
|
+
}
|
|
59
|
+
else {
|
|
60
|
+
(_c = this.app) === null || _c === void 0 ? void 0 : _c.emitPartial(completeEvent, partialEvent);
|
|
61
|
+
return completeEvent;
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
validateProperties(classType, properties = {}, whitelist = false) {
|
|
65
|
+
const props = (0, class_transformer_1.plainToClass)(classType, properties);
|
|
66
|
+
const errors = (0, class_validator_1.validateSync)(props, { whitelist });
|
|
67
|
+
if (errors && Array.isArray(errors) && errors.length > 0) {
|
|
68
|
+
for (const e of errors) {
|
|
69
|
+
this.logger.error(`Validation for property "${e.property}" failed:\n${JSON.stringify(e.constraints || {})}\nvalue: ${e.value}`);
|
|
70
|
+
}
|
|
71
|
+
throw new Error('Properties Validation failed');
|
|
72
|
+
}
|
|
73
|
+
else {
|
|
74
|
+
return props;
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
validateEventData(classType, event, whitelist = false) {
|
|
78
|
+
return this.validateProperties(classType, event.getData(), whitelist);
|
|
79
|
+
}
|
|
80
|
+
async callRpcFunction(functionName, ...args) {
|
|
81
|
+
var _a, _b;
|
|
82
|
+
try {
|
|
83
|
+
return (_b = (_a = this.app) === null || _a === void 0 ? void 0 : _a.rpcClient) === null || _b === void 0 ? void 0 : _b.callFunction(this.rpcRoutingKey, functionName, ...args);
|
|
84
|
+
}
|
|
85
|
+
catch (err) {
|
|
86
|
+
this.logger.error(err);
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
runPyRpcScript(scriptPath, ...args) {
|
|
90
|
+
const options = {
|
|
91
|
+
mode: 'text',
|
|
92
|
+
pythonOptions: ['-u'],
|
|
93
|
+
args: [__dirname, this.rpcRoutingKey, ...args.map((v) => v.toString())],
|
|
94
|
+
};
|
|
95
|
+
return python_shell_1.PythonShell.run(scriptPath, options, (err, outputs) => {
|
|
96
|
+
if (err) {
|
|
97
|
+
this.logger.error(err);
|
|
98
|
+
}
|
|
99
|
+
this.logger.debug(outputs);
|
|
100
|
+
});
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
exports.FlowElement = FlowElement;
|
|
104
|
+
function InputStream(id = 'default', options) {
|
|
105
|
+
return (target, propertyKey, propertyDescriptor) => {
|
|
106
|
+
Reflect.defineMetadata(`stream:${id}`, propertyKey, target.constructor);
|
|
107
|
+
if (options) {
|
|
108
|
+
Reflect.defineMetadata(`stream:options:${id}`, options, target.constructor);
|
|
109
|
+
}
|
|
110
|
+
const method = propertyDescriptor.value;
|
|
111
|
+
propertyDescriptor.value = function (event) {
|
|
112
|
+
var _a;
|
|
113
|
+
if (!this.stopPropagateStream.has(id)) {
|
|
114
|
+
this.stopPropagateStream.set(id, (_a = options === null || options === void 0 ? void 0 : options.stopPropagation) !== null && _a !== void 0 ? _a : false);
|
|
115
|
+
}
|
|
116
|
+
return method.call(this, new FlowEvent_1.FlowEvent(Object.assign(Object.assign({}, event.getMetadata()), { inputStreamId: id }), event.getData(), event.getType(), new Date(event.getTime())));
|
|
117
|
+
};
|
|
118
|
+
};
|
|
119
|
+
}
|
|
120
|
+
exports.InputStream = InputStream;
|
|
121
|
+
function FlowFunction(fqn) {
|
|
122
|
+
const fqnRegExp = new RegExp('^([a-zA-Z][a-zA-Z0-9]*[.-])*[a-zA-Z][a-zA-Z0-9]*$');
|
|
123
|
+
if (!fqnRegExp.test(fqn)) {
|
|
124
|
+
throw new Error(`Flow Function FQN (${fqn}) is not valid`);
|
|
125
|
+
}
|
|
126
|
+
return (target) => {
|
|
127
|
+
Reflect.defineMetadata('element:functionFqn', fqn, target);
|
|
128
|
+
target.prototype.functionFqn = fqn;
|
|
129
|
+
};
|
|
130
|
+
}
|
|
131
|
+
exports.FlowFunction = FlowFunction;
|
|
132
|
+
class FlowResource extends FlowElement {
|
|
133
|
+
}
|
|
134
|
+
exports.FlowResource = FlowResource;
|
|
135
|
+
class FlowTask extends FlowElement {
|
|
136
|
+
}
|
|
137
|
+
exports.FlowTask = FlowTask;
|
|
138
|
+
class FlowTrigger extends FlowElement {
|
|
139
|
+
}
|
|
140
|
+
exports.FlowTrigger = FlowTrigger;
|
|
141
|
+
class FlowDashboard extends FlowResource {
|
|
142
|
+
}
|
|
143
|
+
exports.FlowDashboard = FlowDashboard;
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import { FlowElementContext } from './flow.interface';
|
|
2
|
+
export declare class FlowEvent {
|
|
3
|
+
private event;
|
|
4
|
+
private metadata;
|
|
5
|
+
constructor(metadata: FlowElementContext, data: any, outputId?: string, time?: Date, dataType?: string);
|
|
6
|
+
format: () => any;
|
|
7
|
+
getData: () => any;
|
|
8
|
+
getDataContentType: () => string;
|
|
9
|
+
getDataschema: () => string;
|
|
10
|
+
getId: () => string;
|
|
11
|
+
getMetadata: () => any;
|
|
12
|
+
getSource: () => string;
|
|
13
|
+
getStreamId: () => string;
|
|
14
|
+
getSubject: () => string;
|
|
15
|
+
getTime: () => Date;
|
|
16
|
+
getType: () => string;
|
|
17
|
+
toJSON: () => any;
|
|
18
|
+
toString: () => string;
|
|
19
|
+
}
|
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.FlowEvent = void 0;
|
|
4
|
+
const cloudevents_1 = require("cloudevents");
|
|
5
|
+
const lodash_1 = require("lodash");
|
|
6
|
+
class FlowEvent {
|
|
7
|
+
constructor(metadata, data, outputId = 'default', time = new Date(), dataType) {
|
|
8
|
+
this.format = () => {
|
|
9
|
+
const event = this.event.toJSON();
|
|
10
|
+
if (event.datacontenttype === 'application/json') {
|
|
11
|
+
try {
|
|
12
|
+
event.data = JSON.parse(event.data);
|
|
13
|
+
}
|
|
14
|
+
catch (err) {
|
|
15
|
+
}
|
|
16
|
+
}
|
|
17
|
+
return event;
|
|
18
|
+
};
|
|
19
|
+
this.getData = () => (0, lodash_1.cloneDeep)(this.event.data || {});
|
|
20
|
+
this.getDataContentType = () => this.event.datacontenttype;
|
|
21
|
+
this.getDataschema = () => this.event.dataschema;
|
|
22
|
+
this.getId = () => this.event.id;
|
|
23
|
+
this.getMetadata = () => this.metadata;
|
|
24
|
+
this.getSource = () => this.event.source;
|
|
25
|
+
this.getStreamId = () => `${this.metadata.elementId}.${this.event.type}`;
|
|
26
|
+
this.getSubject = () => this.event.subject;
|
|
27
|
+
this.getTime = () => new Date(this.event.time);
|
|
28
|
+
this.getType = () => this.event.type;
|
|
29
|
+
this.toJSON = () => this.event.toJSON();
|
|
30
|
+
this.toString = () => this.event.toString();
|
|
31
|
+
const { id: elementId, deploymentId, flowId, functionFqn, inputStreamId } = metadata;
|
|
32
|
+
if (data instanceof Error) {
|
|
33
|
+
const error = { message: data.message, stack: data.stack };
|
|
34
|
+
data = error;
|
|
35
|
+
}
|
|
36
|
+
else {
|
|
37
|
+
data = (0, lodash_1.cloneDeep)(data);
|
|
38
|
+
}
|
|
39
|
+
if (data == null) {
|
|
40
|
+
data = {};
|
|
41
|
+
}
|
|
42
|
+
if (dataType == null) {
|
|
43
|
+
if (typeof data === 'string') {
|
|
44
|
+
try {
|
|
45
|
+
JSON.parse(data);
|
|
46
|
+
dataType = 'application/json';
|
|
47
|
+
}
|
|
48
|
+
catch (err) {
|
|
49
|
+
dataType = 'text/plain';
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
else if (typeof data === 'object') {
|
|
53
|
+
dataType = 'application/json';
|
|
54
|
+
}
|
|
55
|
+
else {
|
|
56
|
+
data = String(data);
|
|
57
|
+
dataType = 'text/plain';
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
this.metadata = { deploymentId, elementId, flowId, functionFqn, inputStreamId };
|
|
61
|
+
this.event = new cloudevents_1.CloudEvent({
|
|
62
|
+
source: `flows/${flowId}/deployments/${deploymentId}/elements/${elementId}`,
|
|
63
|
+
type: outputId,
|
|
64
|
+
subject: functionFqn,
|
|
65
|
+
datacontenttype: dataType,
|
|
66
|
+
data,
|
|
67
|
+
time: time.toISOString(),
|
|
68
|
+
});
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
exports.FlowEvent = FlowEvent;
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import { FlowElementContext } from './flow.interface';
|
|
2
|
+
import { FlowEvent } from './FlowEvent';
|
|
3
|
+
export interface Logger {
|
|
4
|
+
debug(message: any, metadata?: any): void;
|
|
5
|
+
error(message: any, metadata?: any): void;
|
|
6
|
+
log(message: any, metadata?: any): void;
|
|
7
|
+
warn(message: any, metadata?: any): void;
|
|
8
|
+
verbose(message: any, metadata?: any): void;
|
|
9
|
+
}
|
|
10
|
+
export declare const defaultLogger: Logger;
|
|
11
|
+
export declare class FlowLogger implements Logger {
|
|
12
|
+
private readonly metadata;
|
|
13
|
+
private readonly logger;
|
|
14
|
+
private readonly publishEvent?;
|
|
15
|
+
private static getStackTrace;
|
|
16
|
+
constructor(metadata: FlowElementContext, logger?: Logger, publishEvent?: (event: FlowEvent) => Promise<void>);
|
|
17
|
+
debug: (message: any) => void;
|
|
18
|
+
error: (message: any) => void;
|
|
19
|
+
log: (message: any) => void;
|
|
20
|
+
warn: (message: any) => void;
|
|
21
|
+
verbose: (message: any) => void;
|
|
22
|
+
private publish;
|
|
23
|
+
}
|