@5minds/processcube_engine_client 5.2.0-feature-9a5b28-m1yxdd4u → 5.2.0
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/dist/Client.d.ts +4 -3
- package/dist/ClientFactory.d.ts +16 -16
- package/dist/Clients/AnonymousSessionHttpClient.d.ts +2 -2
- package/dist/Clients/ApplicationInfoHttpClient.d.ts +5 -4
- package/dist/Clients/BaseClient.d.ts +10 -8
- package/dist/Clients/CorrelationHttpClient.d.ts +4 -3
- package/dist/Clients/CronjobHttpClient.d.ts +5 -4
- package/dist/Clients/DataObjectInstanceHttpClient.d.ts +3 -2
- package/dist/Clients/EventHttpClient.d.ts +13 -12
- package/dist/Clients/ExternalTaskApiHttpClient.d.ts +7 -6
- package/dist/Clients/ExternalTaskHttpClient.d.ts +4 -3
- package/dist/Clients/FlowNodeInstanceHttpClient.d.ts +8 -7
- package/dist/Clients/Lib/SocketIoManager.d.ts +15 -28
- package/dist/Clients/ManualTaskHttpClient.d.ts +7 -6
- package/dist/Clients/NotificationHttpClient.d.ts +52 -51
- package/dist/Clients/ProcessDefinitionHttpClient.d.ts +11 -10
- package/dist/Clients/ProcessDefinitionHttpClientNode.d.ts +2 -2
- package/dist/Clients/ProcessInstanceHttpClient.d.ts +8 -7
- package/dist/Clients/ProcessModelHttpClient.d.ts +11 -10
- package/dist/Clients/UntypedTaskHttpClient.d.ts +7 -6
- package/dist/Clients/UserTaskHttpClient.d.ts +11 -10
- package/dist/ExternalTaskExecution.d.ts +13 -11
- package/dist/ExternalTaskWorker.d.ts +17 -1
- package/dist/IdentityAccessor.d.ts +54 -0
- package/dist/IdentityAccessorClientCredentials.d.ts +14 -0
- package/dist/Retry.d.ts +1 -0
- package/dist/Types/IExternalTaskWorker.d.ts +11 -2
- package/dist/commonjs/Client.js +23 -22
- package/dist/commonjs/Client.js.map +1 -1
- package/dist/commonjs/ClientFactory.js +25 -28
- package/dist/commonjs/ClientFactory.js.map +1 -1
- package/dist/commonjs/Clients/AnonymousSessionHttpClient.js +3 -3
- package/dist/commonjs/Clients/AnonymousSessionHttpClient.js.map +1 -1
- package/dist/commonjs/Clients/ApplicationInfoHttpClient.js +5 -5
- package/dist/commonjs/Clients/ApplicationInfoHttpClient.js.map +1 -1
- package/dist/commonjs/Clients/BaseClient.js +23 -15
- package/dist/commonjs/Clients/BaseClient.js.map +1 -1
- package/dist/commonjs/Clients/CorrelationHttpClient.js +2 -2
- package/dist/commonjs/Clients/CorrelationHttpClient.js.map +1 -1
- package/dist/commonjs/Clients/CronjobHttpClient.js +3 -3
- package/dist/commonjs/Clients/CronjobHttpClient.js.map +1 -1
- package/dist/commonjs/Clients/DataObjectInstanceHttpClient.js +1 -1
- package/dist/commonjs/Clients/DataObjectInstanceHttpClient.js.map +1 -1
- package/dist/commonjs/Clients/EventHttpClient.js +3 -3
- package/dist/commonjs/Clients/EventHttpClient.js.map +1 -1
- package/dist/commonjs/Clients/ExternalTaskApiHttpClient.js +5 -5
- package/dist/commonjs/Clients/ExternalTaskApiHttpClient.js.map +1 -1
- package/dist/commonjs/Clients/ExternalTaskHttpClient.js +7 -5
- package/dist/commonjs/Clients/ExternalTaskHttpClient.js.map +1 -1
- package/dist/commonjs/Clients/FlowNodeInstanceHttpClient.js +6 -6
- package/dist/commonjs/Clients/FlowNodeInstanceHttpClient.js.map +1 -1
- package/dist/commonjs/Clients/Lib/SocketIoManager.js +149 -67
- package/dist/commonjs/Clients/Lib/SocketIoManager.js.map +1 -1
- package/dist/commonjs/Clients/ManualTaskHttpClient.js +2 -2
- package/dist/commonjs/Clients/ManualTaskHttpClient.js.map +1 -1
- package/dist/commonjs/Clients/NotificationHttpClient.js +3 -3
- package/dist/commonjs/Clients/NotificationHttpClient.js.map +1 -1
- package/dist/commonjs/Clients/ProcessDefinitionHttpClient.js +8 -8
- package/dist/commonjs/Clients/ProcessDefinitionHttpClient.js.map +1 -1
- package/dist/commonjs/Clients/ProcessDefinitionHttpClientNode.js +17 -7
- package/dist/commonjs/Clients/ProcessDefinitionHttpClientNode.js.map +1 -1
- package/dist/commonjs/Clients/ProcessInstanceHttpClient.js +7 -7
- package/dist/commonjs/Clients/ProcessInstanceHttpClient.js.map +1 -1
- package/dist/commonjs/Clients/ProcessModelHttpClient.js +9 -9
- package/dist/commonjs/Clients/ProcessModelHttpClient.js.map +1 -1
- package/dist/commonjs/Clients/UntypedTaskHttpClient.js +2 -2
- package/dist/commonjs/Clients/UntypedTaskHttpClient.js.map +1 -1
- package/dist/commonjs/Clients/UserTaskHttpClient.js +4 -4
- package/dist/commonjs/Clients/UserTaskHttpClient.js.map +1 -1
- package/dist/commonjs/ExternalTaskExecution.js +116 -56
- package/dist/commonjs/ExternalTaskExecution.js.map +1 -1
- package/dist/commonjs/ExternalTaskWorker.js +79 -22
- package/dist/commonjs/ExternalTaskWorker.js.map +1 -1
- package/dist/commonjs/IdentityAccessor.js +253 -0
- package/dist/commonjs/IdentityAccessor.js.map +1 -0
- package/dist/commonjs/IdentityAccessorClientCredentials.js +96 -0
- package/dist/commonjs/IdentityAccessorClientCredentials.js.map +1 -0
- package/dist/commonjs/Retry.js +30 -0
- package/dist/commonjs/Retry.js.map +1 -0
- package/dist/commonjs/Types/index.js +17 -7
- package/dist/commonjs/Types/index.js.map +1 -1
- package/dist/commonjs/index.js +5 -1
- package/dist/commonjs/index.js.map +1 -1
- package/dist/index.d.ts +2 -0
- package/package.json +12 -5
- package/tsconfig.tests.json +20 -0
|
@@ -15,24 +15,30 @@ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (
|
|
|
15
15
|
}) : function(o, v) {
|
|
16
16
|
o["default"] = v;
|
|
17
17
|
});
|
|
18
|
-
var __importStar = (this && this.__importStar) || function (
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
};
|
|
18
|
+
var __importStar = (this && this.__importStar) || (function () {
|
|
19
|
+
var ownKeys = function(o) {
|
|
20
|
+
ownKeys = Object.getOwnPropertyNames || function (o) {
|
|
21
|
+
var ar = [];
|
|
22
|
+
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
|
|
23
|
+
return ar;
|
|
24
|
+
};
|
|
25
|
+
return ownKeys(o);
|
|
26
|
+
};
|
|
27
|
+
return function (mod) {
|
|
28
|
+
if (mod && mod.__esModule) return mod;
|
|
29
|
+
var result = {};
|
|
30
|
+
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
|
|
31
|
+
__setModuleDefault(result, mod);
|
|
32
|
+
return result;
|
|
33
|
+
};
|
|
34
|
+
})();
|
|
25
35
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
26
36
|
exports.ExternalTaskWorker = void 0;
|
|
27
37
|
const uuid = __importStar(require("uuid"));
|
|
28
38
|
const processcube_engine_sdk_1 = require("@5minds/processcube_engine_sdk");
|
|
29
39
|
const ExternalTaskApiHttpClient_1 = require("./Clients/ExternalTaskApiHttpClient");
|
|
30
40
|
const ExternalTaskExecution_1 = require("./ExternalTaskExecution");
|
|
31
|
-
const
|
|
32
|
-
const dummyIdentity = {
|
|
33
|
-
token: 'ZHVtbXlfdG9rZW4=',
|
|
34
|
-
userId: 'dummy_token',
|
|
35
|
-
};
|
|
41
|
+
const IdentityAccessor_1 = require("./IdentityAccessor");
|
|
36
42
|
class ExternalTaskWorker {
|
|
37
43
|
_workerId;
|
|
38
44
|
lockDuration;
|
|
@@ -41,28 +47,43 @@ class ExternalTaskWorker {
|
|
|
41
47
|
longpollingTimeout;
|
|
42
48
|
processingFunction;
|
|
43
49
|
payloadFilter;
|
|
44
|
-
|
|
50
|
+
useCustomLogger = false;
|
|
51
|
+
logger;
|
|
52
|
+
identityAccessor;
|
|
45
53
|
_pollingActive = false;
|
|
46
54
|
externalTaskClient;
|
|
47
55
|
customErrorHandler = null;
|
|
56
|
+
onHeartbeatCallback = null;
|
|
48
57
|
abortController;
|
|
49
58
|
abortSignal;
|
|
59
|
+
activelyProcessedExternalTasks = {};
|
|
50
60
|
constructor(engineUrl, topic, processingFunction, config) {
|
|
51
61
|
this._workerId = config?.workerId ?? uuid.v4();
|
|
52
62
|
this.topic = topic;
|
|
53
63
|
this.processingFunction = processingFunction;
|
|
54
|
-
this.
|
|
64
|
+
this.identityAccessor = IdentityAccessor_1.IdentityAccessor.wrap(config?.identity, engineUrl);
|
|
55
65
|
this.maxTasks = config?.maxTasks ?? 10;
|
|
56
66
|
this.longpollingTimeout = config?.longpollingTimeout ?? 10000;
|
|
57
67
|
this.lockDuration = config?.lockDuration ?? 30000;
|
|
58
68
|
this.payloadFilter = config?.payloadFilter;
|
|
59
|
-
this.
|
|
69
|
+
this.useCustomLogger = config?.logger != null;
|
|
70
|
+
this.logger = config?.logger ?? new processcube_engine_sdk_1.Logger('external_task_worker');
|
|
71
|
+
this.externalTaskClient = new ExternalTaskApiHttpClient_1.ExternalTaskApiClient(engineUrl, this.identityAccessor);
|
|
60
72
|
}
|
|
73
|
+
/**
|
|
74
|
+
* @deprecated Use getIdentity instead.
|
|
75
|
+
*/
|
|
61
76
|
get identity() {
|
|
62
|
-
return this.
|
|
77
|
+
return this.identityAccessor.getIdentityStatic();
|
|
63
78
|
}
|
|
79
|
+
async getIdentity() {
|
|
80
|
+
return this.identityAccessor.getIdentity();
|
|
81
|
+
}
|
|
82
|
+
/**
|
|
83
|
+
* @deprecated This is no longer safe to use. Identity should be set via the constructor.
|
|
84
|
+
*/
|
|
64
85
|
set identity(value) {
|
|
65
|
-
this.
|
|
86
|
+
this.identityAccessor = IdentityAccessor_1.IdentityAccessor.wrap(value, this.externalTaskClient.getEngineUrl());
|
|
66
87
|
}
|
|
67
88
|
get workerId() {
|
|
68
89
|
return this._workerId;
|
|
@@ -73,10 +94,23 @@ class ExternalTaskWorker {
|
|
|
73
94
|
onWorkerError(callback) {
|
|
74
95
|
this.customErrorHandler = callback;
|
|
75
96
|
}
|
|
97
|
+
onHeartbeat(callback) {
|
|
98
|
+
this.onHeartbeatCallback = callback;
|
|
99
|
+
}
|
|
100
|
+
getActiveExternalTasks() {
|
|
101
|
+
return Object.keys(this.activelyProcessedExternalTasks);
|
|
102
|
+
}
|
|
103
|
+
isExternalTaskBeingProcessed(externalTaskId) {
|
|
104
|
+
return this.activelyProcessedExternalTasks[externalTaskId] != null;
|
|
105
|
+
}
|
|
76
106
|
start() {
|
|
77
107
|
this._pollingActive = true;
|
|
78
108
|
this.abortController = new AbortController();
|
|
79
109
|
this.abortSignal = this.abortController.signal;
|
|
110
|
+
this.abortSignal.onabort = () => {
|
|
111
|
+
Object.values(this.activelyProcessedExternalTasks).forEach((executor) => executor.abort());
|
|
112
|
+
this.activelyProcessedExternalTasks = {};
|
|
113
|
+
};
|
|
80
114
|
this.processExternalTasks();
|
|
81
115
|
}
|
|
82
116
|
stop() {
|
|
@@ -85,8 +119,16 @@ class ExternalTaskWorker {
|
|
|
85
119
|
this.abortController?.abort();
|
|
86
120
|
}
|
|
87
121
|
}
|
|
122
|
+
abortExternalTaskIfPresent(externalTaskId) {
|
|
123
|
+
if (this.activelyProcessedExternalTasks[externalTaskId] != null) {
|
|
124
|
+
this.activelyProcessedExternalTasks[externalTaskId].abort();
|
|
125
|
+
delete this.activelyProcessedExternalTasks[externalTaskId];
|
|
126
|
+
}
|
|
127
|
+
}
|
|
88
128
|
dispose() {
|
|
129
|
+
this.stop();
|
|
89
130
|
this.externalTaskClient.dispose();
|
|
131
|
+
this.identityAccessor?.dispose();
|
|
90
132
|
}
|
|
91
133
|
async processExternalTasks() {
|
|
92
134
|
let errorCount = 0;
|
|
@@ -96,9 +138,20 @@ class ExternalTaskWorker {
|
|
|
96
138
|
try {
|
|
97
139
|
externalTasks = await this.fetchAndLockExternalTasks();
|
|
98
140
|
errorCount = 0;
|
|
141
|
+
if (typeof this.onHeartbeatCallback === 'function') {
|
|
142
|
+
this.onHeartbeatCallback('fetchAndLock');
|
|
143
|
+
}
|
|
99
144
|
}
|
|
100
145
|
catch (error) {
|
|
101
|
-
|
|
146
|
+
if (this.abortSignal?.aborted) {
|
|
147
|
+
return;
|
|
148
|
+
}
|
|
149
|
+
this.logger.error('Failed to fetch and lock External Tasks.', { err: error });
|
|
150
|
+
if (error.message?.match(/expired/i) && this.identityAccessor.canIdentityBeRefreshed()) {
|
|
151
|
+
this.logger.info('Identity has expired. Refreshing identity to get a new auth token...');
|
|
152
|
+
await this.identityAccessor.forceImmediateIdentityRefresh();
|
|
153
|
+
this.logger.info('Identity refresh completed. Fetch and Lock will continue.');
|
|
154
|
+
}
|
|
102
155
|
errorCount++;
|
|
103
156
|
const timeoutOffset = (Math.floor(Math.random() * 10) + 1) * 30;
|
|
104
157
|
const calculatedTimeout = 2 ** errorCount * 1000 + timeoutOffset;
|
|
@@ -119,25 +172,29 @@ class ExternalTaskWorker {
|
|
|
119
172
|
}
|
|
120
173
|
}
|
|
121
174
|
async fetchAndLockExternalTasks() {
|
|
122
|
-
return this.externalTaskClient.fetchAndLockExternalTasks(this.workerId, this.topic, this.maxTasks, this.longpollingTimeout, this.lockDuration, this.payloadFilter, this.
|
|
175
|
+
return this.externalTaskClient.fetchAndLockExternalTasks(this.workerId, this.topic, this.maxTasks, this.longpollingTimeout, this.lockDuration, this.payloadFilter, this.identityAccessor, this.abortSignal);
|
|
123
176
|
}
|
|
124
177
|
async startExecutingExternalTask(externalTask) {
|
|
125
178
|
try {
|
|
126
179
|
const externalTaskExecution = new ExternalTaskExecution_1.ExternalTaskExecution(externalTask, this.processingFunction, this.externalTaskClient, {
|
|
127
|
-
identity: this.
|
|
180
|
+
identity: this.identityAccessor,
|
|
128
181
|
lockDuration: this.lockDuration,
|
|
129
182
|
longpollingTimeout: this.longpollingTimeout,
|
|
130
183
|
maxTasks: this.maxTasks,
|
|
131
184
|
payloadFilter: this.payloadFilter,
|
|
132
185
|
workerId: this.workerId,
|
|
133
|
-
}, this.topic, this.
|
|
186
|
+
}, this.topic, this.onHeartbeatCallback, this.customErrorHandler, this.useCustomLogger ? this.logger : undefined);
|
|
187
|
+
this.activelyProcessedExternalTasks[externalTask.id] = externalTaskExecution;
|
|
134
188
|
await externalTaskExecution.execute();
|
|
135
189
|
}
|
|
136
190
|
catch (error) {
|
|
137
191
|
if (typeof this.customErrorHandler === 'function') {
|
|
138
192
|
this.customErrorHandler('unprocessableExternalTask', error, externalTask);
|
|
139
193
|
}
|
|
140
|
-
logger.warn(`External Task ${externalTask.id} could not be processed.`, { err: error });
|
|
194
|
+
this.logger.warn(`External Task ${externalTask.id} could not be processed.`, { err: error });
|
|
195
|
+
}
|
|
196
|
+
finally {
|
|
197
|
+
delete this.activelyProcessedExternalTasks[externalTask.id];
|
|
141
198
|
}
|
|
142
199
|
}
|
|
143
200
|
async sleep(milliseconds) {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"ExternalTaskWorker.js","sourceRoot":"","sources":["../../src/ExternalTaskWorker.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"ExternalTaskWorker.js","sourceRoot":"","sources":["../../src/ExternalTaskWorker.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA,2CAA6B;AAE7B,2EAAgF;AAEhF,mFAA4E;AAC5E,mEAAgE;AAChE,yDAAsD;AAQtD,MAAa,kBAAkB;IACZ,SAAS,CAAS;IAClB,YAAY,CAAS;IACrB,KAAK,CAAS;IACd,QAAQ,CAAS;IACjB,kBAAkB,CAAS;IAC3B,kBAAkB,CAAiE;IACnF,aAAa,CAAS;IACtB,eAAe,GAAY,KAAK,CAAC;IACjC,MAAM,CAAU;IAEzB,gBAAgB,CAAmB;IACnC,cAAc,GAAG,KAAK,CAAC;IACvB,kBAAkB,CAAwB;IAC1C,kBAAkB,GAAuB,IAAI,CAAC;IAC9C,mBAAmB,GAAgC,IAAI,CAAC;IACxD,eAAe,CAAkB;IACjC,WAAW,CAAc;IAEzB,8BAA8B,GAAgF,EAAE,CAAC;IA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AAC,CAAC;QAC9D,CAAC;IACH,CAAC;IAEO,KAAK,CAAC,KAAK,CAAC,YAAoB;QACtC,OAAO,IAAI,OAAO,CAAO,CAAC,OAAiB,EAAQ,EAAE;YACnD,UAAU,CAAC,GAAS,EAAE;gBACpB,OAAO,EAAE,CAAC;YACZ,CAAC,EAAE,YAAY,CAAC,CAAC;QACnB,CAAC,CAAC,CAAC;IACL,CAAC;CACF;AA1ND,gDA0NC"}
|
|
@@ -0,0 +1,253 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
14
|
+
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
15
|
+
}) : function(o, v) {
|
|
16
|
+
o["default"] = v;
|
|
17
|
+
});
|
|
18
|
+
var __importStar = (this && this.__importStar) || (function () {
|
|
19
|
+
var ownKeys = function(o) {
|
|
20
|
+
ownKeys = Object.getOwnPropertyNames || function (o) {
|
|
21
|
+
var ar = [];
|
|
22
|
+
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
|
|
23
|
+
return ar;
|
|
24
|
+
};
|
|
25
|
+
return ownKeys(o);
|
|
26
|
+
};
|
|
27
|
+
return function (mod) {
|
|
28
|
+
if (mod && mod.__esModule) return mod;
|
|
29
|
+
var result = {};
|
|
30
|
+
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
|
|
31
|
+
__setModuleDefault(result, mod);
|
|
32
|
+
return result;
|
|
33
|
+
};
|
|
34
|
+
})();
|
|
35
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
36
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
37
|
+
};
|
|
38
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
39
|
+
exports.IdentityAccessor = void 0;
|
|
40
|
+
const async_lock_1 = __importDefault(require("async-lock"));
|
|
41
|
+
const dayjs_1 = __importDefault(require("dayjs"));
|
|
42
|
+
const uuid = __importStar(require("uuid"));
|
|
43
|
+
const processcube_engine_sdk_1 = require("@5minds/processcube_engine_sdk");
|
|
44
|
+
const index_1 = require("./index");
|
|
45
|
+
const dummyIdentity = {
|
|
46
|
+
token: 'ZHVtbXlfdG9rZW4=',
|
|
47
|
+
userId: 'dummy_token',
|
|
48
|
+
};
|
|
49
|
+
class IdentityAccessor {
|
|
50
|
+
static wrap(identity, engineUrl) {
|
|
51
|
+
if (identity instanceof IdentityAccessor) {
|
|
52
|
+
return identity;
|
|
53
|
+
}
|
|
54
|
+
let accessor;
|
|
55
|
+
if (!identity) {
|
|
56
|
+
accessor = new IdentityAccessor();
|
|
57
|
+
}
|
|
58
|
+
else if (typeof identity === 'function') {
|
|
59
|
+
accessor = new IdentityAccessor(identity);
|
|
60
|
+
}
|
|
61
|
+
else if (isClientCredentialsConfig(identity)) {
|
|
62
|
+
if (!identity.engineUrl && engineUrl) {
|
|
63
|
+
identity.engineUrl = engineUrl;
|
|
64
|
+
}
|
|
65
|
+
accessor = new index_1.ClientCredentialsIdentityAccessor(identity);
|
|
66
|
+
}
|
|
67
|
+
else if (!identity.token) {
|
|
68
|
+
const invalidIdentity = new processcube_engine_sdk_1.BadRequestError(`The given identity is invalid. Required property 'token' is missing.`);
|
|
69
|
+
invalidIdentity.additionalInformation = {
|
|
70
|
+
configuredIdentity: identity,
|
|
71
|
+
};
|
|
72
|
+
throw invalidIdentity;
|
|
73
|
+
}
|
|
74
|
+
else {
|
|
75
|
+
accessor = new IdentityAccessor(identity);
|
|
76
|
+
}
|
|
77
|
+
accessor.initialize();
|
|
78
|
+
return accessor;
|
|
79
|
+
}
|
|
80
|
+
identityGetter;
|
|
81
|
+
latestIdentity;
|
|
82
|
+
expirationTimeInMs;
|
|
83
|
+
id;
|
|
84
|
+
identityAvailablePromise;
|
|
85
|
+
identityAvailableResolve;
|
|
86
|
+
identityAvailableReject;
|
|
87
|
+
refreshFailedCritically = false;
|
|
88
|
+
refreshTimeout;
|
|
89
|
+
disposed = false;
|
|
90
|
+
isDynamicRefreshObject = false;
|
|
91
|
+
logger = new processcube_engine_sdk_1.Logger('engine-client:identity-accessor');
|
|
92
|
+
newIdentityCallbacks = [];
|
|
93
|
+
asyncLock = new async_lock_1.default({ maxPending: 100000 });
|
|
94
|
+
constructor(staticOrDynamicIdentity) {
|
|
95
|
+
if (typeof staticOrDynamicIdentity === 'function') {
|
|
96
|
+
this.id = uuid.v4();
|
|
97
|
+
this.identityGetter = staticOrDynamicIdentity;
|
|
98
|
+
this.isDynamicRefreshObject = true;
|
|
99
|
+
}
|
|
100
|
+
else if (staticOrDynamicIdentity) {
|
|
101
|
+
this.id = staticOrDynamicIdentity?.userId ?? uuid.v4();
|
|
102
|
+
this.latestIdentity = staticOrDynamicIdentity;
|
|
103
|
+
this.identityGetter = async () => staticOrDynamicIdentity;
|
|
104
|
+
}
|
|
105
|
+
else {
|
|
106
|
+
this.id = dummyIdentity.userId;
|
|
107
|
+
this.latestIdentity = dummyIdentity;
|
|
108
|
+
this.identityGetter = async () => dummyIdentity;
|
|
109
|
+
}
|
|
110
|
+
this.identityAvailablePromise = new Promise((resolve, reject) => {
|
|
111
|
+
this.identityAvailableResolve = resolve;
|
|
112
|
+
this.identityAvailableReject = reject;
|
|
113
|
+
});
|
|
114
|
+
}
|
|
115
|
+
initialize() {
|
|
116
|
+
this.fetchIdentity().then(() => this.refreshIdentityPeriodically());
|
|
117
|
+
}
|
|
118
|
+
getId() {
|
|
119
|
+
return this.id;
|
|
120
|
+
}
|
|
121
|
+
getIdentitySync() {
|
|
122
|
+
return this.latestIdentity;
|
|
123
|
+
}
|
|
124
|
+
canIdentityBeRefreshed() {
|
|
125
|
+
return this.isDynamicRefreshObject && this.expirationTimeInMs > 0 && !this.refreshFailedCritically && !this.disposed;
|
|
126
|
+
}
|
|
127
|
+
async getIdentity() {
|
|
128
|
+
await this.waitUntilIdentityIsAvailable();
|
|
129
|
+
return this.latestIdentity;
|
|
130
|
+
}
|
|
131
|
+
async forceImmediateIdentityRefresh() {
|
|
132
|
+
if (!this.isDynamicRefreshObject) {
|
|
133
|
+
return;
|
|
134
|
+
}
|
|
135
|
+
clearTimeout(this.refreshTimeout);
|
|
136
|
+
await this.fetchIdentity();
|
|
137
|
+
this.refreshIdentityPeriodically();
|
|
138
|
+
}
|
|
139
|
+
/**
|
|
140
|
+
* This method only waits the first time the identity is retrieved from any remote source to ensure no missing references occur.
|
|
141
|
+
*/
|
|
142
|
+
async waitUntilIdentityIsAvailable() {
|
|
143
|
+
return await this.identityAvailablePromise;
|
|
144
|
+
}
|
|
145
|
+
/**
|
|
146
|
+
* @deprecated This method only exists for downward compatibility purposes, use getIdentity instead.
|
|
147
|
+
*/
|
|
148
|
+
getIdentityStatic() {
|
|
149
|
+
if (!this.latestIdentity) {
|
|
150
|
+
throw new Error('Identity not yet initialized');
|
|
151
|
+
}
|
|
152
|
+
return this.latestIdentity;
|
|
153
|
+
}
|
|
154
|
+
onNewIdentity(callback) {
|
|
155
|
+
this.newIdentityCallbacks.push(callback);
|
|
156
|
+
}
|
|
157
|
+
removeOnNewIdentityListener(callback) {
|
|
158
|
+
this.newIdentityCallbacks = this.newIdentityCallbacks.filter((cb) => cb !== callback);
|
|
159
|
+
}
|
|
160
|
+
/**
|
|
161
|
+
* The IdentityAccesor creates a refresh timer, which needs to be disposed.
|
|
162
|
+
* This method should be called when the IdentityAccessor is not needed anymore.
|
|
163
|
+
* If any Client using this will be disposed, the IdentityAccessor will be disposed as well.
|
|
164
|
+
*/
|
|
165
|
+
dispose() {
|
|
166
|
+
clearTimeout(this.refreshTimeout);
|
|
167
|
+
this.refreshTimeout = undefined;
|
|
168
|
+
this.identityAvailableResolve();
|
|
169
|
+
this.disposed = true;
|
|
170
|
+
}
|
|
171
|
+
getBaseMinRefreshTimeInMs() {
|
|
172
|
+
const fiveMinutes = 5 * 60 * 1000;
|
|
173
|
+
return fiveMinutes;
|
|
174
|
+
}
|
|
175
|
+
refreshIdentityPeriodically() {
|
|
176
|
+
clearTimeout(this.refreshTimeout);
|
|
177
|
+
if (this.canIdentityBeRefreshed()) {
|
|
178
|
+
const timeUntilNextRefresh = Math.max((this.expirationTimeInMs ?? 0) - Date.now() - this.getBaseMinRefreshTimeInMs(), this.getBaseMinRefreshTimeInMs());
|
|
179
|
+
const timeUntilNextRefresh32Bit = Math.min(timeUntilNextRefresh, 2147483647);
|
|
180
|
+
this.logger.debug(`Current Identity will expire at ${(0, dayjs_1.default)()
|
|
181
|
+
.add(this.expirationTimeInMs ?? 0, 'milliseconds')
|
|
182
|
+
.toISOString()}`);
|
|
183
|
+
this.logger.debug(`Next Refresh will be performed at ${(0, dayjs_1.default)().add(timeUntilNextRefresh32Bit, 'milliseconds').toISOString()}`);
|
|
184
|
+
this.refreshTimeout = setTimeout(async () => {
|
|
185
|
+
await this.fetchIdentity();
|
|
186
|
+
this.refreshIdentityPeriodically();
|
|
187
|
+
}, timeUntilNextRefresh32Bit);
|
|
188
|
+
}
|
|
189
|
+
else if (this.refreshFailedCritically) {
|
|
190
|
+
this.logger.error(`Identity refresh failed critically and won't be refreshed anymore.`);
|
|
191
|
+
this.identityAvailableReject(new Error('Identity refresh failed critically and wont be refreshed anymore.'));
|
|
192
|
+
}
|
|
193
|
+
}
|
|
194
|
+
async fetchIdentity() {
|
|
195
|
+
await this.asyncLock.acquire('refreshIdentity', async () => {
|
|
196
|
+
this.refreshFailedCritically = false;
|
|
197
|
+
try {
|
|
198
|
+
this.logger.debug('Fetching new identity');
|
|
199
|
+
this.latestIdentity = await this.identityGetter();
|
|
200
|
+
this.logger.trace('New Identity fetched');
|
|
201
|
+
if (this.latestIdentity == dummyIdentity || this.latestIdentity?.token == dummyIdentity.token) {
|
|
202
|
+
this.expirationTimeInMs = 0;
|
|
203
|
+
this.identityAvailableResolve();
|
|
204
|
+
}
|
|
205
|
+
else if (this.latestIdentity?.token) {
|
|
206
|
+
const decodedToken = this.getDecodedToken(this.latestIdentity.token);
|
|
207
|
+
this.expirationTimeInMs = decodedToken?.exp ? decodedToken.exp * 1000 : 0;
|
|
208
|
+
this.identityAvailableResolve();
|
|
209
|
+
try {
|
|
210
|
+
await Promise.all(this.newIdentityCallbacks.map((cb) => cb(this.latestIdentity)));
|
|
211
|
+
}
|
|
212
|
+
catch (err) {
|
|
213
|
+
this.logger.warn('Error while publishing new identity to listeners', { err });
|
|
214
|
+
}
|
|
215
|
+
}
|
|
216
|
+
else {
|
|
217
|
+
this.identityAvailableResolve();
|
|
218
|
+
}
|
|
219
|
+
}
|
|
220
|
+
catch (err) {
|
|
221
|
+
this.logger.error('Error while refreshing identity', { err });
|
|
222
|
+
this.refreshFailedCritically =
|
|
223
|
+
!err.message ||
|
|
224
|
+
!['ETIMEDOUT', 'ECONNRESET', 'ECONNREFUSED', 'other side closed', 'SocketError', 'EAI_AGAIN', 'EHOSTUNREACH', 'EPIPE', 'ECONNABORTED', 'EHOSTDOWN'].some((errorString) => err.message.includes(errorString));
|
|
225
|
+
}
|
|
226
|
+
});
|
|
227
|
+
}
|
|
228
|
+
getDecodedToken(tokenString) {
|
|
229
|
+
if (!tokenString) {
|
|
230
|
+
return undefined;
|
|
231
|
+
}
|
|
232
|
+
if (tokenString === dummyIdentity.token) {
|
|
233
|
+
return undefined;
|
|
234
|
+
}
|
|
235
|
+
try {
|
|
236
|
+
const splitToken = this.latestIdentity.token.split('.');
|
|
237
|
+
const decodedToken = JSON.parse(Buffer.from(splitToken[1], 'base64').toString());
|
|
238
|
+
return decodedToken;
|
|
239
|
+
}
|
|
240
|
+
catch (error) {
|
|
241
|
+
return undefined;
|
|
242
|
+
}
|
|
243
|
+
}
|
|
244
|
+
}
|
|
245
|
+
exports.IdentityAccessor = IdentityAccessor;
|
|
246
|
+
function isClientCredentialsConfig(identity) {
|
|
247
|
+
return (identity.clientId !== undefined ||
|
|
248
|
+
identity.clientSecret !== undefined ||
|
|
249
|
+
identity.scope !== undefined ||
|
|
250
|
+
identity.authorityUrl !== undefined ||
|
|
251
|
+
identity.engineUrl !== undefined);
|
|
252
|
+
}
|
|
253
|
+
//# sourceMappingURL=IdentityAccessor.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"IdentityAccessor.js","sourceRoot":"","sources":["../../src/IdentityAccessor.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA,4DAAmC;AACnC,kDAA0B;AAC1B,2CAA6B;AAE7B,2EAAmF;AAEnF,mCAA4D;AAa5D,MAAM,aAAa,GAAa;IAC9B,KAAK,EAAE,kBAAkB;IACzB,MAAM,EAAE,aAAa;CACtB,CAAC;AAEF,MAAa,gBAAgB;IAC3B,MAAM,CAAC,IAAI,CAAC,QAAuB,EAAE,SAAkB;QACrD,IAAI,QAAQ,YAAY,gBAAgB,EAAE,CAAC;YACzC,OAAO,QAAQ,CAAC;QAClB,CAAC;QAED,IAAI,QAA0B,CAAC;QAE/B,IAAI,CAAC,QAAQ,EAAE,CAAC;YACd,QAAQ,GAAG,IAAI,gBAAgB,EAAE,CAAC;QACpC,CAAC;aAAM,IAAI,OAAO,QAAQ,KAAK,UAAU,EAAE,CAAC;YAC1C,QAAQ,GAAG,IAAI,gBAAgB,CAAC,QAAQ,CAAC,CAAC;QAC5C,CAAC;aAAM,IAAI,yBAAyB,CAAC,QAAQ,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+
exports.ClientCredentialsIdentityAccessor = void 0;
|
|
4
|
+
const ClientFactory_1 = require("./ClientFactory");
|
|
5
|
+
const Retry_1 = require("./Retry");
|
|
6
|
+
const index_1 = require("./index");
|
|
7
|
+
class ClientCredentialsIdentityAccessor extends index_1.IdentityAccessor {
|
|
8
|
+
config;
|
|
9
|
+
constructor(clientCredentialsConfig) {
|
|
10
|
+
super(async () => {
|
|
11
|
+
const identity = await this.getFreshIdentity(clientCredentialsConfig);
|
|
12
|
+
return identity;
|
|
13
|
+
});
|
|
14
|
+
this.config = clientCredentialsConfig;
|
|
15
|
+
this.id = this.hash(JSON.stringify(clientCredentialsConfig ?? {}));
|
|
16
|
+
}
|
|
17
|
+
async getFreshIdentity(clientCredentialsConfig) {
|
|
18
|
+
const token = await this.getNewToken(clientCredentialsConfig);
|
|
19
|
+
return {
|
|
20
|
+
token,
|
|
21
|
+
userId: clientCredentialsConfig.clientId ?? '',
|
|
22
|
+
};
|
|
23
|
+
}
|
|
24
|
+
getBaseMinRefreshTimeInMs() {
|
|
25
|
+
const fiveMinutes = 5 * 60 * 1000;
|
|
26
|
+
if (this.config.baseMinRefreshTimeInMs) {
|
|
27
|
+
return this.config.baseMinRefreshTimeInMs > 0 ? this.config.baseMinRefreshTimeInMs : fiveMinutes;
|
|
28
|
+
}
|
|
29
|
+
return fiveMinutes;
|
|
30
|
+
}
|
|
31
|
+
async getNewToken(clientCredentialsConfig) {
|
|
32
|
+
this.logger.debug('Fetching new auth token from authority');
|
|
33
|
+
const authorityUrl = await this.getAuthorityUrl(clientCredentialsConfig);
|
|
34
|
+
const openIdClient = await this.getOpenIdClient();
|
|
35
|
+
const wellKnownUrl = new URL(`${authorityUrl}/.well-known/openid-configuration`);
|
|
36
|
+
let tokenSet;
|
|
37
|
+
try {
|
|
38
|
+
const config = await (0, Retry_1.executeWithRetry)(async () => openIdClient.discovery(wellKnownUrl, clientCredentialsConfig.clientId ?? '', clientCredentialsConfig.clientSecret, undefined, {
|
|
39
|
+
execute: [openIdClient.allowInsecureRequests],
|
|
40
|
+
}), `${wellKnownUrl} not available, retrying...`);
|
|
41
|
+
tokenSet = await (0, Retry_1.executeWithRetry)(async () => openIdClient.clientCredentialsGrant(config, { scope: clientCredentialsConfig.scope ?? '' }), `${wellKnownUrl} not available, retrying...`);
|
|
42
|
+
}
|
|
43
|
+
catch (err) {
|
|
44
|
+
this.logger.error('Error while getting new token from authority', { err, authorityUrl, wellKnownUrl });
|
|
45
|
+
throw err;
|
|
46
|
+
}
|
|
47
|
+
if (tokenSet.access_token) {
|
|
48
|
+
this.logger.trace('Successfuily received a new access token from authority');
|
|
49
|
+
return tokenSet.access_token;
|
|
50
|
+
}
|
|
51
|
+
else {
|
|
52
|
+
this.logger.error('No access token found in token set');
|
|
53
|
+
throw new Error('No access token found in token set');
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
async getOpenIdClient() {
|
|
57
|
+
const openIdClient = await import('openid-client');
|
|
58
|
+
return openIdClient;
|
|
59
|
+
}
|
|
60
|
+
async getAuthorityUrl(config) {
|
|
61
|
+
if (config.authorityUrl) {
|
|
62
|
+
return this.ensureEndsWithoutSlash(this.config.authorityUrl);
|
|
63
|
+
}
|
|
64
|
+
if (config.engineUrl) {
|
|
65
|
+
return this.getAuthorityUrlFromEngineUrl(config.engineUrl);
|
|
66
|
+
}
|
|
67
|
+
throw new Error('No authorityUrl or engineUrl provided.');
|
|
68
|
+
}
|
|
69
|
+
async getAuthorityUrlFromEngineUrl(engineUrl) {
|
|
70
|
+
try {
|
|
71
|
+
const applicationInfoClient = ClientFactory_1.ClientFactory.createApplicationInfoClient(engineUrl);
|
|
72
|
+
const authorityAddress = await (0, Retry_1.executeWithRetry)(async () => applicationInfoClient.getAuthorityAddress(), `${engineUrl} not available, retrying...`);
|
|
73
|
+
return this.ensureEndsWithoutSlash(authorityAddress);
|
|
74
|
+
}
|
|
75
|
+
catch (err) {
|
|
76
|
+
this.logger.error('Error while getting authority address from engine', { err, engineUrl });
|
|
77
|
+
throw err;
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
ensureEndsWithoutSlash(url) {
|
|
81
|
+
return url.replace(/(\/)$/, '');
|
|
82
|
+
}
|
|
83
|
+
hash(str) {
|
|
84
|
+
// djb2 constants
|
|
85
|
+
let hash = 5381;
|
|
86
|
+
const product = 33;
|
|
87
|
+
let i = str.length;
|
|
88
|
+
while (i) {
|
|
89
|
+
hash = (hash * product) ^ str.charCodeAt(--i);
|
|
90
|
+
}
|
|
91
|
+
const numericalHash = hash >>> 0;
|
|
92
|
+
return numericalHash + '';
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
exports.ClientCredentialsIdentityAccessor = ClientCredentialsIdentityAccessor;
|
|
96
|
+
//# sourceMappingURL=IdentityAccessorClientCredentials.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"IdentityAccessorClientCredentials.js","sourceRoot":"","sources":["../../src/IdentityAccessorClientCredentials.ts"],"names":[],"mappings":";;;AAEA,mDAAgD;AAChD,mCAA2C;AAC3C,mCAAoE;AAEpE,MAAa,iCAAkC,SAAQ,wBAAgB;IAC7D,MAAM,CAA0B;IAExC,YAAY,uBAAgD;QAC1D,KAAK,CAAC,KAAK,IAAI,EAAE;YACf,MAAM,QAAQ,GAAG,MAAM,IAAI,CAAC,gBAAgB,CAAC,uBAAuB,CAAC,CAAC;YACtE,OAAO,QAAQ,CAAC;QAClB,CAAC,CAAC,CAAC;QAEH,IAAI,CAAC,MAAM,GAAG,uBAAuB,CAAC;QACtC,IAAI,CAAC,EAAE,GAAG,IAAI,CAAC,IAAI,CAAC,IAAI,CAAC,SAAS,CAAC,uBAAuB,IAAI,EAAE,CAAC,CAAC,CAAC;IACrE,CAAC;IAEM,KAAK,CAAC,gBAAgB,CAAC,uBAAgD;QAC5E,MAAM,KAAK,GAAG,MAAM,IAAI,CAAC,WAAW,CAAC,uBAAuB,CAAC,CAAC;QAE9D,OAAO;YACL,KAAK;YACL,MA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SAAS,6BAA6B,CAAC,CAAC;YAE5J,OAAO,IAAI,CAAC,sBAAsB,CAAC,gBAAgB,CAAC,CAAC;QACvD,CAAC;QAAC,OAAO,GAAG,EAAE,CAAC;YACb,IAAI,CAAC,MAAM,CAAC,KAAK,CAAC,mDAAmD,EAAE,EAAE,GAAG,EAAE,SAAS,EAAE,CAAC,CAAC;YAC3F,MAAM,GAAG,CAAC;QACZ,CAAC;IACH,CAAC;IAEO,sBAAsB,CAAC,GAAW;QACxC,OAAO,GAAG,CAAC,OAAO,CAAC,OAAO,EAAE,EAAE,CAAC,CAAC;IAClC,CAAC;IAEO,IAAI,CAAC,GAAW;QACtB,iBAAiB;QACjB,IAAI,IAAI,GAAG,IAAI,CAAC;QAChB,MAAM,OAAO,GAAG,EAAE,CAAC;QAEnB,IAAI,CAAC,GAAG,GAAG,CAAC,MAAM,CAAC;QAEnB,OAAO,CAAC,EAAE,CAAC;YACT,IAAI,GAAG,CAAC,IAAI,GAAG,OAAO,CAAC,GAAG,GAAG,CAAC,UAAU,CAAC,EAAE,CAAC,CAAC,CAAC;QAChD,CAAC;QAED,MAAM,aAAa,GAAG,IAAI,KAAK,CAAC,CAAC;QAEjC,OAAO,aAAa,GAAG,EAAE,CAAC;IAC5B,CAAC;CACF;AArHD,8EAqHC"}
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.executeWithRetry = executeWithRetry;
|
|
4
|
+
const processcube_engine_sdk_1 = require("@5minds/processcube_engine_sdk");
|
|
5
|
+
const logger = new processcube_engine_sdk_1.Logger('engine-client:retry');
|
|
6
|
+
function executeWithRetry(fn, retryMessage, errorsToRetry = ['ETIMEDOUT', 'ECONNRESET', 'ECONNREFUSED', 'other side closed', 'SocketError', 'EAI_AGAIN', 'EHOSTUNREACH', 'EPIPE', 'ECONNABORTED', 'EHOSTDOWN'], maxRetries = 20, retryInterval = 5000) {
|
|
7
|
+
return new Promise((resolve, reject) => {
|
|
8
|
+
const execute = async (retries) => {
|
|
9
|
+
try {
|
|
10
|
+
const result = await fn();
|
|
11
|
+
resolve(result);
|
|
12
|
+
}
|
|
13
|
+
catch (error) {
|
|
14
|
+
const message = error.message || error || '';
|
|
15
|
+
const isRetryable = errorsToRetry.some((retryError) => message.includes(retryError));
|
|
16
|
+
if (retries > 0 && isRetryable) {
|
|
17
|
+
if (retryMessage) {
|
|
18
|
+
logger.debug(retryMessage, { reason: message });
|
|
19
|
+
}
|
|
20
|
+
setTimeout(() => execute(retries - 1), retryInterval);
|
|
21
|
+
}
|
|
22
|
+
else {
|
|
23
|
+
reject(error);
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
};
|
|
27
|
+
execute(maxRetries);
|
|
28
|
+
});
|
|
29
|
+
}
|
|
30
|
+
//# sourceMappingURL=Retry.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"Retry.js","sourceRoot":"","sources":["../../src/Retry.ts"],"names":[],"mappings":";;AAIA,4CA8BC;AAlCD,2EAAwD;AAExD,MAAM,MAAM,GAAG,IAAI,+BAAM,CAAC,qBAAqB,CAAC,CAAC;AAEjD,SAAgB,gBAAgB,CAC9B,EAA0B,EAC1B,YAAqB,EACrB,gBAA+B,CAAC,WAAW,EAAE,YAAY,EAAE,cAAc,EAAE,mBAAmB,EAAE,aAAa,EAAE,WAAW,EAAE,cAAc,EAAE,OAAO,EAAE,cAAc,EAAE,WAAW,CAAC,EACjL,aAAqB,EAAE,EACvB,gBAAwB,IAAI;IAE5B,OAAO,IAAI,OAAO,CAAU,CAAC,OAAO,EAAE,MAAM,EAAE,EAAE;QAC9C,MAAM,OAAO,GAAG,KAAK,EAAE,OAAe,EAAE,EAAE;YACxC,IAAI,CAAC;gBACH,MAAM,MAAM,GAAG,MAAM,EAAE,EAAE,CAAC;gBAC1B,OAAO,CAAC,MAAM,CAAC,CAAC;YAClB,CAAC;YAAC,OAAO,KAAU,EAAE,CAAC;gBACpB,MAAM,OAAO,GAAG,KAAK,CAAC,OAAO,IAAI,KAAK,IAAI,EAAE,CAAC;gBAC7C,MAAM,WAAW,GAAG,aAAa,CAAC,IAAI,CAAC,CAAC,UAAU,EAAE,EAAE,CAAC,OAAO,CAAC,QAAQ,CAAC,UAAU,CAAC,CAAC,CAAC;gBAErF,IAAI,OAAO,GAAG,CAAC,IAAI,WAAW,EAAE,CAAC;oBAC/B,IAAI,YAAY,EAAE,CAAC;wBACjB,MAAM,CAAC,KAAK,CAAC,YAAY,EAAE,EAAE,MAAM,EAAE,OAAO,EAAE,CAAC,CAAC;oBAClD,CAAC;oBAED,UAAU,CAAC,GAAG,EAAE,CAAC,OAAO,CAAC,OAAO,GAAG,CAAC,CAAC,EAAE,aAAa,CAAC,CAAC;gBACxD,CAAC;qBAAM,CAAC;oBACN,MAAM,CAAC,KAAK,CAAC,CAAC;gBAChB,CAAC;YACH,CAAC;QACH,CAAC,CAAC;QAEF,OAAO,CAAC,UAAU,CAAC,CAAC;IACtB,CAAC,CAAC,CAAC;AACL,CAAC"}
|
|
@@ -15,13 +15,23 @@ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (
|
|
|
15
15
|
}) : function(o, v) {
|
|
16
16
|
o["default"] = v;
|
|
17
17
|
});
|
|
18
|
-
var __importStar = (this && this.__importStar) || function (
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
};
|
|
18
|
+
var __importStar = (this && this.__importStar) || (function () {
|
|
19
|
+
var ownKeys = function(o) {
|
|
20
|
+
ownKeys = Object.getOwnPropertyNames || function (o) {
|
|
21
|
+
var ar = [];
|
|
22
|
+
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
|
|
23
|
+
return ar;
|
|
24
|
+
};
|
|
25
|
+
return ownKeys(o);
|
|
26
|
+
};
|
|
27
|
+
return function (mod) {
|
|
28
|
+
if (mod && mod.__esModule) return mod;
|
|
29
|
+
var result = {};
|
|
30
|
+
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
|
|
31
|
+
__setModuleDefault(result, mod);
|
|
32
|
+
return result;
|
|
33
|
+
};
|
|
34
|
+
})();
|
|
25
35
|
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
26
36
|
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
27
37
|
};
|