@interopio/search-api 2.5.2 → 3.1.0-beta.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/changelog.md +2 -0
- package/dist/search-api.es.js +3 -2
- package/dist/search-api.es.js.map +1 -1
- package/dist/search-api.umd.js +3 -2
- package/dist/search-api.umd.js.map +1 -1
- package/package.json +4 -5
- package/src/controllers/client.ts +0 -352
- package/src/controllers/glue.ts +0 -154
- package/src/controllers/main.ts +0 -85
- package/src/controllers/provider.ts +0 -475
- package/src/facade.ts +0 -69
- package/src/index.ts +0 -19
- package/src/models/client-query.ts +0 -83
- package/src/models/provider-query.ts +0 -84
- package/src/models/provider.ts +0 -91
- package/src/services/limits-tracker.ts +0 -75
- package/src/services/model-factory.ts +0 -41
- package/src/services/publisher.ts +0 -178
- package/src/services/sequelizer.ts +0 -48
- package/src/shared/constants.ts +0 -11
- package/src/shared/decoders.ts +0 -123
- package/src/shared/ioc.ts +0 -118
- package/src/shared/protocol.ts +0 -71
- package/src/shared/types.ts +0 -103
- package/src/shared/utils.ts +0 -9
|
@@ -1,352 +0,0 @@
|
|
|
1
|
-
/* eslint-disable @typescript-eslint/no-explicit-any */
|
|
2
|
-
/* eslint-disable indent */
|
|
3
|
-
import { IOConnectCore } from "@interopio/core";
|
|
4
|
-
import { nanoid } from "nanoid/non-secure";
|
|
5
|
-
import { IOConnectSearch } from "../../search";
|
|
6
|
-
import { ActiveClientQuery, InteropServerProvider, LegacySearchResultItem } from "../shared/types";
|
|
7
|
-
import { GlueController } from "./glue";
|
|
8
|
-
import {
|
|
9
|
-
default as CallbackRegistryFactory,
|
|
10
|
-
CallbackRegistry
|
|
11
|
-
} from "callback-registry";
|
|
12
|
-
import { ProtocolProviderError, ProtocolSearchCompleted, ProtocolSearchResultsBatch, SEARCH_QUERY_STATUSES } from "../shared/protocol";
|
|
13
|
-
import { queryStatusDecoder, protocolProviderErrorDecoder, protocolSearchCompletedDecoder, protocolSearchResultsBatchDecoder } from "../shared/decoders";
|
|
14
|
-
import { ModelFactory } from "../services/model-factory";
|
|
15
|
-
|
|
16
|
-
export class ClientController {
|
|
17
|
-
|
|
18
|
-
private readonly registry: CallbackRegistry = CallbackRegistryFactory();
|
|
19
|
-
private readonly activeQueryLookup: { [key in string]: ActiveClientQuery } = {};
|
|
20
|
-
private readonly queryIdToMasterIdLookup: { [key in string]: string } = {};
|
|
21
|
-
private pendingDebounce: ({ resolve: (value: IOConnectSearch.Query) => void, reject: (reason?: any) => void })[] = [];
|
|
22
|
-
private debounceTimer?: NodeJS.Timer;
|
|
23
|
-
private debounceMS = 0;
|
|
24
|
-
|
|
25
|
-
constructor(
|
|
26
|
-
private readonly logger: IOConnectCore.Logger.API,
|
|
27
|
-
private readonly glueController: GlueController,
|
|
28
|
-
private readonly modelFactory: ModelFactory
|
|
29
|
-
) {}
|
|
30
|
-
|
|
31
|
-
public setDebounceMS(data: { milliseconds: number, commandId: string }): void {
|
|
32
|
-
|
|
33
|
-
this.logger.info(`[${data.commandId}] Setting the debounceMS to: ${data.milliseconds}`);
|
|
34
|
-
|
|
35
|
-
this.debounceMS = data.milliseconds;
|
|
36
|
-
|
|
37
|
-
this.logger.info(`[${data.commandId}] debounceMS set to: ${data.milliseconds}`);
|
|
38
|
-
}
|
|
39
|
-
|
|
40
|
-
public getDebounceMS(data: { commandId: string }): number {
|
|
41
|
-
|
|
42
|
-
this.logger.info(`[${data.commandId}] Getting the debounceMS`);
|
|
43
|
-
|
|
44
|
-
return this.debounceMS;
|
|
45
|
-
|
|
46
|
-
}
|
|
47
|
-
|
|
48
|
-
public async query(data: { queryConfig: IOConnectSearch.QueryConfig, commandId: string }, skipDebounce?: boolean): Promise<IOConnectSearch.Query> {
|
|
49
|
-
|
|
50
|
-
if (this.debounceMS && !skipDebounce) {
|
|
51
|
-
return this.debounceQuery(data);
|
|
52
|
-
}
|
|
53
|
-
|
|
54
|
-
await this.glueController.registerMainClientMethod(this.handleProviderCall.bind(this));
|
|
55
|
-
|
|
56
|
-
const { queryConfig, commandId } = data;
|
|
57
|
-
|
|
58
|
-
this.logger.info(`[${commandId}] Initiating a query request`);
|
|
59
|
-
|
|
60
|
-
let allProvidersInfo = await this.glueController.getAllProvidersInfo();
|
|
61
|
-
|
|
62
|
-
this.logger.trace(`[${commandId}] Got all available providers: ${JSON.stringify(allProvidersInfo)}`);
|
|
63
|
-
|
|
64
|
-
if (queryConfig.providers) {
|
|
65
|
-
this.logger.info(`[${commandId}] Filtering providers by explicitly allowed providers.`);
|
|
66
|
-
allProvidersInfo = this.filterProvidersByAllowList(allProvidersInfo, queryConfig.providers);
|
|
67
|
-
}
|
|
68
|
-
|
|
69
|
-
if (queryConfig.types) {
|
|
70
|
-
this.logger.info(`[${commandId}] Filtering providers by explicitly allowed types.`);
|
|
71
|
-
allProvidersInfo = this.filterProvidersByAllowedTypes(allProvidersInfo, queryConfig.types);
|
|
72
|
-
}
|
|
73
|
-
|
|
74
|
-
if (!allProvidersInfo.length) {
|
|
75
|
-
this.logger.warn(`[${commandId}] There are no providers that can handle the query for ${data.queryConfig.search}`);
|
|
76
|
-
}
|
|
77
|
-
|
|
78
|
-
this.logger.info(`[${commandId}] Sending query request to providers: ${JSON.stringify(allProvidersInfo)}`);
|
|
79
|
-
const allQueryResponses = await this.glueController.sendQueryRequest(queryConfig, allProvidersInfo);
|
|
80
|
-
|
|
81
|
-
this.logger.info(`[${commandId}] Received responses from the providers: ${JSON.stringify(allQueryResponses)}`);
|
|
82
|
-
const masterQueryId = this.generateMasterQueryId();
|
|
83
|
-
|
|
84
|
-
const queryModel = this.modelFactory.buildClientQueryModel(masterQueryId, this);
|
|
85
|
-
|
|
86
|
-
this.logger.info(`[${commandId}] The query is in progress with master id: ${masterQueryId}`);
|
|
87
|
-
|
|
88
|
-
this.activeQueryLookup[masterQueryId] = {
|
|
89
|
-
servers: allQueryResponses,
|
|
90
|
-
model: queryModel
|
|
91
|
-
};
|
|
92
|
-
|
|
93
|
-
allQueryResponses.forEach((response) => {
|
|
94
|
-
this.queryIdToMasterIdLookup[response.queryId] = masterQueryId;
|
|
95
|
-
});
|
|
96
|
-
|
|
97
|
-
if (!allQueryResponses.length) {
|
|
98
|
-
// there are no providers that will handle this request
|
|
99
|
-
setTimeout(() => {
|
|
100
|
-
this.registry.execute(`on-query-completed-${masterQueryId}`);
|
|
101
|
-
|
|
102
|
-
this.cleanUpQuery(masterQueryId);
|
|
103
|
-
}, 0);
|
|
104
|
-
}
|
|
105
|
-
|
|
106
|
-
return queryModel.exposeFacade();
|
|
107
|
-
}
|
|
108
|
-
|
|
109
|
-
public async cancelQuery(masterQueryId: string, commandId: string): Promise<void> {
|
|
110
|
-
|
|
111
|
-
const activeQuery = this.activeQueryLookup[masterQueryId];
|
|
112
|
-
|
|
113
|
-
if (!activeQuery) {
|
|
114
|
-
throw new Error(`[${commandId}] Cannot cancel query: ${masterQueryId}, because this query does not exist`);
|
|
115
|
-
}
|
|
116
|
-
|
|
117
|
-
const interopIds = activeQuery.servers;
|
|
118
|
-
|
|
119
|
-
this.logger.info(`[${commandId}] Sending cancel query requests`);
|
|
120
|
-
|
|
121
|
-
await Promise.all(interopIds.map((serverId) => {
|
|
122
|
-
this.logger.trace(`[${commandId}] Sending cancel query request to ${serverId.interopId} with queryId: ${serverId.queryId}`);
|
|
123
|
-
|
|
124
|
-
return this.glueController.sendQueryCancelRequest({ id: serverId.queryId }, { instance: serverId.interopId });
|
|
125
|
-
}));
|
|
126
|
-
|
|
127
|
-
this.logger.info(`[${commandId}] The query was cancelled`);
|
|
128
|
-
}
|
|
129
|
-
|
|
130
|
-
public processClientOnResults(data: { callback: (resultBatch: IOConnectSearch.QueryResultBatch) => void, masterQueryId: string, commandId: string }): IOConnectSearch.UnsubscribeFunction {
|
|
131
|
-
return this.registry.add(`on-query-results-${data.masterQueryId}`, data.callback);
|
|
132
|
-
}
|
|
133
|
-
|
|
134
|
-
public processClientOnCompleted(data: { callback: () => void, masterQueryId: string, commandId: string }): IOConnectSearch.UnsubscribeFunction {
|
|
135
|
-
return this.registry.add(`on-query-completed-${data.masterQueryId}`, data.callback);
|
|
136
|
-
}
|
|
137
|
-
|
|
138
|
-
public processClientOnError(data: { callback: (error: IOConnectSearch.QueryError) => void, masterQueryId: string, commandId: string }): IOConnectSearch.UnsubscribeFunction {
|
|
139
|
-
return this.registry.add(`on-query-error-${data.masterQueryId}`, data.callback);
|
|
140
|
-
}
|
|
141
|
-
|
|
142
|
-
private async handleProviderCall(args: any): Promise<any> {
|
|
143
|
-
const { status } = args;
|
|
144
|
-
|
|
145
|
-
const validatedOperation = queryStatusDecoder.runWithException(status);
|
|
146
|
-
|
|
147
|
-
const commandId = nanoid(10);
|
|
148
|
-
|
|
149
|
-
switch (validatedOperation) {
|
|
150
|
-
case SEARCH_QUERY_STATUSES.done:
|
|
151
|
-
return this.handleQueryCompleted({ completedConfig: args, commandId });
|
|
152
|
-
case SEARCH_QUERY_STATUSES.inProgress:
|
|
153
|
-
return this.handleQueryResults({ resultsBatch: args, commandId });
|
|
154
|
-
case SEARCH_QUERY_STATUSES.error:
|
|
155
|
-
return this.handleQueryError({ error: args, commandId });
|
|
156
|
-
default:
|
|
157
|
-
throw new Error(`Unrecognized status: ${status}`);
|
|
158
|
-
}
|
|
159
|
-
}
|
|
160
|
-
|
|
161
|
-
private handleQueryResults(data: { resultsBatch: ProtocolSearchResultsBatch, commandId: string }): void {
|
|
162
|
-
const { resultsBatch, commandId } = data;
|
|
163
|
-
|
|
164
|
-
this.logger.trace(`[${commandId}] Processing a results batch from provider: ${resultsBatch.provider?.name} with id: ${resultsBatch.provider?.id}`);
|
|
165
|
-
|
|
166
|
-
const verifiedResultsBatch = protocolSearchResultsBatchDecoder.runWithException(resultsBatch);
|
|
167
|
-
|
|
168
|
-
const masterQueryId = this.queryIdToMasterIdLookup[verifiedResultsBatch.queryId];
|
|
169
|
-
|
|
170
|
-
if (!masterQueryId) {
|
|
171
|
-
this.logger.warn(`[${commandId}] Received results for an unknown query. Provider ${JSON.stringify(verifiedResultsBatch.provider)}, items: ${JSON.stringify(verifiedResultsBatch.items)}`);
|
|
172
|
-
return;
|
|
173
|
-
}
|
|
174
|
-
|
|
175
|
-
this.logger.trace(`[${commandId}] The results batch is validated, forwarding to the callbacks`);
|
|
176
|
-
|
|
177
|
-
const translatedResults: IOConnectSearch.QueryResult[] = this.checkTransformLegacyResults(verifiedResultsBatch.items);
|
|
178
|
-
|
|
179
|
-
const results: IOConnectSearch.QueryResultBatch = {
|
|
180
|
-
provider: verifiedResultsBatch.provider,
|
|
181
|
-
results: translatedResults
|
|
182
|
-
};
|
|
183
|
-
|
|
184
|
-
this.registry.execute(`on-query-results-${masterQueryId}`, results);
|
|
185
|
-
}
|
|
186
|
-
|
|
187
|
-
private handleQueryCompleted(data: { completedConfig: ProtocolSearchCompleted, commandId: string }): void {
|
|
188
|
-
const { completedConfig, commandId } = data;
|
|
189
|
-
|
|
190
|
-
this.logger.trace(`[${commandId}] Processing a query completed message from query id: ${completedConfig.queryId}`);
|
|
191
|
-
|
|
192
|
-
const verifiedCompleteConfig = protocolSearchCompletedDecoder.runWithException(completedConfig);
|
|
193
|
-
|
|
194
|
-
const masterQueryId = this.queryIdToMasterIdLookup[verifiedCompleteConfig.queryId];
|
|
195
|
-
|
|
196
|
-
if (!masterQueryId) {
|
|
197
|
-
this.logger.warn(`[${commandId}] Received completed message for an unknown query. Provider query id: ${JSON.stringify(verifiedCompleteConfig.queryId)}`);
|
|
198
|
-
return;
|
|
199
|
-
}
|
|
200
|
-
|
|
201
|
-
if (verifiedCompleteConfig.items.length) {
|
|
202
|
-
const translatedResults: IOConnectSearch.QueryResult[] = this.checkTransformLegacyResults(verifiedCompleteConfig.items);
|
|
203
|
-
|
|
204
|
-
const results: IOConnectSearch.QueryResultBatch = {
|
|
205
|
-
results: translatedResults
|
|
206
|
-
};
|
|
207
|
-
|
|
208
|
-
this.registry.execute(`on-query-results-${masterQueryId}`, results);
|
|
209
|
-
}
|
|
210
|
-
|
|
211
|
-
delete this.queryIdToMasterIdLookup[verifiedCompleteConfig.queryId];
|
|
212
|
-
|
|
213
|
-
const activeQuery = this.activeQueryLookup[masterQueryId];
|
|
214
|
-
|
|
215
|
-
activeQuery.servers = activeQuery.servers.filter((server) => server.queryId !== verifiedCompleteConfig.queryId);
|
|
216
|
-
|
|
217
|
-
if (activeQuery.servers.length) {
|
|
218
|
-
this.logger.trace(`[${commandId}] Waiting for more providers to complete`);
|
|
219
|
-
|
|
220
|
-
return;
|
|
221
|
-
}
|
|
222
|
-
|
|
223
|
-
this.logger.trace(`[${commandId}] All providers are done, marking this query as completed`);
|
|
224
|
-
|
|
225
|
-
this.registry.execute(`on-query-completed-${masterQueryId}`);
|
|
226
|
-
|
|
227
|
-
this.cleanUpQuery(masterQueryId);
|
|
228
|
-
}
|
|
229
|
-
|
|
230
|
-
private handleQueryError(data: { error: ProtocolProviderError, commandId: string }): void {
|
|
231
|
-
const { error, commandId } = data;
|
|
232
|
-
|
|
233
|
-
this.logger.trace(`[${commandId}] Processing an error message from query: ${error.queryId}`);
|
|
234
|
-
|
|
235
|
-
const validatedError = protocolProviderErrorDecoder.runWithException(error);
|
|
236
|
-
|
|
237
|
-
const masterQueryId = this.queryIdToMasterIdLookup[validatedError.queryId];
|
|
238
|
-
|
|
239
|
-
if (!masterQueryId) {
|
|
240
|
-
this.logger.warn(`[${commandId}] Received error message for an unknown query. Provider query id: ${JSON.stringify(validatedError.queryId)} and message: ${JSON.stringify(validatedError.errorMessage)}`);
|
|
241
|
-
return;
|
|
242
|
-
}
|
|
243
|
-
|
|
244
|
-
const queryError: IOConnectSearch.QueryError = {
|
|
245
|
-
error: validatedError.errorMessage,
|
|
246
|
-
provider: validatedError.provider
|
|
247
|
-
};
|
|
248
|
-
|
|
249
|
-
this.registry.execute(`on-query-error-${masterQueryId}`, queryError);
|
|
250
|
-
}
|
|
251
|
-
|
|
252
|
-
private filterProvidersByAllowList(servers: InteropServerProvider[], allowed: IOConnectSearch.ProviderData[]): InteropServerProvider[] {
|
|
253
|
-
|
|
254
|
-
const allowedLookup = allowed.reduce<{ [key in string]: boolean }>((lookup, allowedEntry) => {
|
|
255
|
-
lookup[allowedEntry.id] = true;
|
|
256
|
-
|
|
257
|
-
return lookup;
|
|
258
|
-
}, {});
|
|
259
|
-
|
|
260
|
-
return servers.filter((server) => {
|
|
261
|
-
const serverProviders = server.info.providers;
|
|
262
|
-
|
|
263
|
-
return serverProviders.some((provider) => allowedLookup[provider.id]);
|
|
264
|
-
});
|
|
265
|
-
}
|
|
266
|
-
|
|
267
|
-
private filterProvidersByAllowedTypes(servers: InteropServerProvider[], allowed: IOConnectSearch.SearchType[]): InteropServerProvider[] {
|
|
268
|
-
|
|
269
|
-
const allowedLookup = allowed.reduce<{ [key in string]: boolean }>((lookup, allowedEntry) => {
|
|
270
|
-
lookup[allowedEntry.name] = true;
|
|
271
|
-
|
|
272
|
-
return lookup;
|
|
273
|
-
}, {});
|
|
274
|
-
|
|
275
|
-
return servers.filter((server) => {
|
|
276
|
-
const allTypes = server.info.supportedTypes;
|
|
277
|
-
|
|
278
|
-
// this is an interop provider which might have multiple providers, some have defined types, some not
|
|
279
|
-
if (allTypes.some((searchType) => searchType === "*")) {
|
|
280
|
-
return true;
|
|
281
|
-
}
|
|
282
|
-
|
|
283
|
-
// allows providers with no defined type to receive all queries
|
|
284
|
-
if (!allTypes || !allTypes.length) {
|
|
285
|
-
return true;
|
|
286
|
-
}
|
|
287
|
-
|
|
288
|
-
return allTypes.some((supportedType) => allowedLookup[supportedType]);
|
|
289
|
-
});
|
|
290
|
-
}
|
|
291
|
-
|
|
292
|
-
private generateMasterQueryId(): string {
|
|
293
|
-
const queryId = nanoid(10);
|
|
294
|
-
|
|
295
|
-
if (this.activeQueryLookup[queryId]) {
|
|
296
|
-
return this.generateMasterQueryId();
|
|
297
|
-
}
|
|
298
|
-
|
|
299
|
-
return queryId;
|
|
300
|
-
}
|
|
301
|
-
|
|
302
|
-
private cleanUpQuery(masterQueryId: string): void {
|
|
303
|
-
this.registry.clearKey(`on-query-results-${masterQueryId}`);
|
|
304
|
-
this.registry.clearKey(`on-query-completed-${masterQueryId}`);
|
|
305
|
-
this.registry.clearKey(`on-query-error-${masterQueryId}`);
|
|
306
|
-
|
|
307
|
-
delete this.activeQueryLookup[masterQueryId];
|
|
308
|
-
}
|
|
309
|
-
|
|
310
|
-
private debounceQuery(data: { queryConfig: IOConnectSearch.QueryConfig, commandId: string }): Promise<IOConnectSearch.Query> {
|
|
311
|
-
return new Promise<IOConnectSearch.Query>((res, rej) => {
|
|
312
|
-
clearTimeout(this.debounceTimer);
|
|
313
|
-
|
|
314
|
-
this.debounceTimer = setTimeout(() => {
|
|
315
|
-
const currentPending = [...this.pendingDebounce];
|
|
316
|
-
|
|
317
|
-
this.pendingDebounce = [];
|
|
318
|
-
|
|
319
|
-
this.query(data, true)
|
|
320
|
-
.then((query) => currentPending.forEach(({ resolve }) => resolve(query)))
|
|
321
|
-
.catch((error) => currentPending.forEach(({ reject }) => reject(error)));
|
|
322
|
-
|
|
323
|
-
}, this.debounceMS);
|
|
324
|
-
|
|
325
|
-
this.pendingDebounce.push({ resolve: res, reject: rej });
|
|
326
|
-
});
|
|
327
|
-
}
|
|
328
|
-
|
|
329
|
-
private checkTransformLegacyResults(items: (IOConnectSearch.QueryResult | LegacySearchResultItem)[]): IOConnectSearch.QueryResult[] {
|
|
330
|
-
|
|
331
|
-
if (!items.length) {
|
|
332
|
-
return [];
|
|
333
|
-
}
|
|
334
|
-
|
|
335
|
-
const sampleItem = items[0];
|
|
336
|
-
|
|
337
|
-
if (!sampleItem || typeof sampleItem.type === "object") {
|
|
338
|
-
return items as IOConnectSearch.QueryResult[];
|
|
339
|
-
}
|
|
340
|
-
|
|
341
|
-
return (items as LegacySearchResultItem[]).map<IOConnectSearch.QueryResult>((item) => {
|
|
342
|
-
return {
|
|
343
|
-
type: { name: item.type, displayName: item.category },
|
|
344
|
-
id: item.id,
|
|
345
|
-
displayName: item.displayName,
|
|
346
|
-
description: item.description,
|
|
347
|
-
iconURL: item.iconURL,
|
|
348
|
-
action: item.action
|
|
349
|
-
};
|
|
350
|
-
});
|
|
351
|
-
}
|
|
352
|
-
}
|
package/src/controllers/glue.ts
DELETED
|
@@ -1,154 +0,0 @@
|
|
|
1
|
-
/* eslint-disable @typescript-eslint/no-explicit-any */
|
|
2
|
-
import { IOConnectCore } from "@interopio/core";
|
|
3
|
-
import { IOConnectSearch } from "../../search";
|
|
4
|
-
import { MAIN_CLIENT_METHOD_NAME, MAIN_PROVIDER_METHOD_NAME } from "../shared/constants";
|
|
5
|
-
import { CLIENT_TO_PROVIDER_PROTOCOL_OPERATIONS, ProtocolProviderError, ProtocolProviderInfoRequest, ProtocolProviderInfoResponse, ProtocolQueryCancelRequest, ProtocolSearchCompleted, ProtocolSearchRequest, ProtocolSearchResponse, ProtocolSearchResultsBatch, SEARCH_QUERY_STATUSES } from "../shared/protocol";
|
|
6
|
-
import { InteropServerProvider, InteropServerQueryIdentification, PublisherQueryResultBatch, SearchCancelRequest } from "../shared/types";
|
|
7
|
-
|
|
8
|
-
export class GlueController {
|
|
9
|
-
constructor(private readonly glue: IOConnectCore.API) {}
|
|
10
|
-
|
|
11
|
-
public get myAppName(): string | undefined {
|
|
12
|
-
return this.glue.interop.instance.applicationName;
|
|
13
|
-
}
|
|
14
|
-
|
|
15
|
-
public get myInteropId(): string {
|
|
16
|
-
return this.glue.interop.instance.instance as string;
|
|
17
|
-
}
|
|
18
|
-
|
|
19
|
-
public async registerMainProviderMethod(handler: (args: any, caller: IOConnectCore.Interop.Instance) => Promise<any>): Promise<void> {
|
|
20
|
-
const mainMethodStatus = this.checkMyMethodExists(MAIN_PROVIDER_METHOD_NAME);
|
|
21
|
-
|
|
22
|
-
if (mainMethodStatus.exists) {
|
|
23
|
-
return;
|
|
24
|
-
}
|
|
25
|
-
|
|
26
|
-
await this.glue.interop.register(MAIN_PROVIDER_METHOD_NAME, handler);
|
|
27
|
-
}
|
|
28
|
-
|
|
29
|
-
public async registerMainClientMethod(handler: (args: any, caller: IOConnectCore.Interop.Instance) => Promise<any>): Promise<void> {
|
|
30
|
-
const mainMethodStatus = this.checkMyMethodExists(MAIN_CLIENT_METHOD_NAME);
|
|
31
|
-
|
|
32
|
-
if (mainMethodStatus.exists) {
|
|
33
|
-
return;
|
|
34
|
-
}
|
|
35
|
-
|
|
36
|
-
await this.glue.interop.register(MAIN_CLIENT_METHOD_NAME, handler);
|
|
37
|
-
}
|
|
38
|
-
|
|
39
|
-
public async clearMainProviderMethod(): Promise<void> {
|
|
40
|
-
// unregister is async, there is a bug in the typings
|
|
41
|
-
await this.glue.interop.unregister(MAIN_PROVIDER_METHOD_NAME);
|
|
42
|
-
}
|
|
43
|
-
|
|
44
|
-
public async sendClientResultsBatch(batch: PublisherQueryResultBatch, clientInstanceId: string, queryId: string): Promise<void> {
|
|
45
|
-
const interopArguments: ProtocolSearchResultsBatch = {
|
|
46
|
-
items: batch.results,
|
|
47
|
-
provider: batch.provider,
|
|
48
|
-
queryId,
|
|
49
|
-
status: SEARCH_QUERY_STATUSES.inProgress
|
|
50
|
-
};
|
|
51
|
-
|
|
52
|
-
await this.glue.interop.invoke<void>(MAIN_CLIENT_METHOD_NAME, interopArguments, { instance: clientInstanceId });
|
|
53
|
-
}
|
|
54
|
-
|
|
55
|
-
public async sendClientQueueCompleted(clientInstanceId: string, queryId: string): Promise<void> {
|
|
56
|
-
const interopArguments: ProtocolSearchCompleted = {
|
|
57
|
-
items: [],
|
|
58
|
-
queryId,
|
|
59
|
-
status: SEARCH_QUERY_STATUSES.done
|
|
60
|
-
};
|
|
61
|
-
|
|
62
|
-
await this.glue.interop.invoke<void>(MAIN_CLIENT_METHOD_NAME, interopArguments, { instance: clientInstanceId });
|
|
63
|
-
}
|
|
64
|
-
|
|
65
|
-
public async sendClientErrorMessage(error: string, clientInstanceId: string, queryId: string, provider: IOConnectSearch.ProviderData): Promise<void> {
|
|
66
|
-
const interopArguments: ProtocolProviderError = {
|
|
67
|
-
items: [],
|
|
68
|
-
provider,
|
|
69
|
-
errorMessage: error,
|
|
70
|
-
queryId,
|
|
71
|
-
status: SEARCH_QUERY_STATUSES.error
|
|
72
|
-
};
|
|
73
|
-
|
|
74
|
-
await this.glue.interop.invoke<void>(MAIN_CLIENT_METHOD_NAME, interopArguments, { instance: clientInstanceId });
|
|
75
|
-
}
|
|
76
|
-
|
|
77
|
-
public async sendQueryRequest(queryConfig: IOConnectSearch.QueryConfig, instances: InteropServerProvider[]): Promise<InteropServerQueryIdentification[]> {
|
|
78
|
-
|
|
79
|
-
if (!instances.length) {
|
|
80
|
-
return [];
|
|
81
|
-
}
|
|
82
|
-
|
|
83
|
-
const target = instances.map((inst) => ({ instance: inst.interopId }));
|
|
84
|
-
|
|
85
|
-
const invokeArgs: ProtocolSearchRequest = {
|
|
86
|
-
operation: CLIENT_TO_PROVIDER_PROTOCOL_OPERATIONS.search,
|
|
87
|
-
apiVersion: "1",
|
|
88
|
-
...queryConfig
|
|
89
|
-
};
|
|
90
|
-
|
|
91
|
-
const response = await this.glue.interop.invoke<ProtocolSearchResponse>(MAIN_PROVIDER_METHOD_NAME, invokeArgs, target);
|
|
92
|
-
|
|
93
|
-
const allReturned = response.all_return_values || [];
|
|
94
|
-
|
|
95
|
-
return allReturned.map<InteropServerQueryIdentification>((returnValue) => {
|
|
96
|
-
return {
|
|
97
|
-
interopId: returnValue.executed_by?.instance as string,
|
|
98
|
-
queryId: returnValue.returned.id
|
|
99
|
-
};
|
|
100
|
-
});
|
|
101
|
-
}
|
|
102
|
-
|
|
103
|
-
public async sendQueryCancelRequest(request: SearchCancelRequest, instance: { instance: string }): Promise<void> {
|
|
104
|
-
const args: ProtocolQueryCancelRequest = {
|
|
105
|
-
operation: CLIENT_TO_PROVIDER_PROTOCOL_OPERATIONS.cancel,
|
|
106
|
-
id: request.id
|
|
107
|
-
};
|
|
108
|
-
|
|
109
|
-
await this.glue.interop.invoke(MAIN_PROVIDER_METHOD_NAME, args, instance);
|
|
110
|
-
}
|
|
111
|
-
|
|
112
|
-
public async getAllProvidersInfo(): Promise<InteropServerProvider[]> {
|
|
113
|
-
|
|
114
|
-
if (this.glue.interop.methods().every((method) => method.name !== MAIN_PROVIDER_METHOD_NAME)) {
|
|
115
|
-
return [];
|
|
116
|
-
}
|
|
117
|
-
|
|
118
|
-
const args: ProtocolProviderInfoRequest = {
|
|
119
|
-
operation: CLIENT_TO_PROVIDER_PROTOCOL_OPERATIONS.info
|
|
120
|
-
};
|
|
121
|
-
|
|
122
|
-
const response = await this.glue.interop.invoke<ProtocolProviderInfoResponse>(MAIN_PROVIDER_METHOD_NAME, args, "all");
|
|
123
|
-
|
|
124
|
-
const allReturned = response.all_return_values || [];
|
|
125
|
-
|
|
126
|
-
return allReturned.map<InteropServerProvider>((returnValue) => {
|
|
127
|
-
|
|
128
|
-
const isLegacy = typeof returnValue.returned.apiVersion === "undefined";
|
|
129
|
-
|
|
130
|
-
const info: ProtocolProviderInfoResponse = isLegacy ? {
|
|
131
|
-
supportedTypes: returnValue.returned.supportedTypes,
|
|
132
|
-
apiVersion: returnValue.returned.apiVersion,
|
|
133
|
-
providers: [{
|
|
134
|
-
interopId: returnValue.executed_by?.instance as string,
|
|
135
|
-
id: returnValue.executed_by?.instance as string,
|
|
136
|
-
name: returnValue.executed_by?.instance as string,
|
|
137
|
-
appName: response.executed_by?.application,
|
|
138
|
-
types: returnValue.returned.supportedTypes.map((t) => ({ name: t }))
|
|
139
|
-
}]
|
|
140
|
-
} : returnValue.returned;
|
|
141
|
-
|
|
142
|
-
return {
|
|
143
|
-
interopId: returnValue.executed_by?.instance as string,
|
|
144
|
-
info
|
|
145
|
-
};
|
|
146
|
-
});
|
|
147
|
-
}
|
|
148
|
-
|
|
149
|
-
private checkMyMethodExists(methodName: string): { exists: boolean } {
|
|
150
|
-
const myMethods = this.glue.interop.methodsForInstance({ instance: this.glue.interop.instance.instance });
|
|
151
|
-
|
|
152
|
-
return { exists: myMethods.some((method) => method.name === methodName) };
|
|
153
|
-
}
|
|
154
|
-
}
|
package/src/controllers/main.ts
DELETED
|
@@ -1,85 +0,0 @@
|
|
|
1
|
-
import { IOConnectCore } from "@interopio/core";
|
|
2
|
-
import { IOConnectSearch } from "../../search";
|
|
3
|
-
import { ClientController } from "./client";
|
|
4
|
-
import { GlueController } from "./glue";
|
|
5
|
-
import { ProviderController } from "./provider";
|
|
6
|
-
|
|
7
|
-
export class MainController {
|
|
8
|
-
|
|
9
|
-
constructor(
|
|
10
|
-
private readonly logger: IOConnectCore.Logger.API,
|
|
11
|
-
private readonly glueController: GlueController,
|
|
12
|
-
private readonly clientController: ClientController,
|
|
13
|
-
private readonly providerController: ProviderController
|
|
14
|
-
) {}
|
|
15
|
-
|
|
16
|
-
public setDebounceMS(data: { milliseconds: number, commandId: string }): void {
|
|
17
|
-
this.logger.info(`[${data.commandId}] Starting setDebounceMS operation with duration ${data.milliseconds}`);
|
|
18
|
-
|
|
19
|
-
this.clientController.setDebounceMS(data);
|
|
20
|
-
|
|
21
|
-
this.logger.info(`[${data.commandId}] Operation setDebounceMS with duration ${data.milliseconds} completed`);
|
|
22
|
-
}
|
|
23
|
-
|
|
24
|
-
public getDebounceMS(data: { commandId: string }): number {
|
|
25
|
-
this.logger.info(`[${data.commandId}] Starting getDebounceMS operation.`);
|
|
26
|
-
|
|
27
|
-
return this.clientController.getDebounceMS(data);
|
|
28
|
-
}
|
|
29
|
-
|
|
30
|
-
public async query(data: { queryConfig: IOConnectSearch.QueryConfig, commandId: string }): Promise<IOConnectSearch.Query> {
|
|
31
|
-
this.logger.info(`[${data.commandId}] Starting query operation with config ${JSON.stringify(data.queryConfig)}`);
|
|
32
|
-
|
|
33
|
-
if (Array.isArray(data.queryConfig.providers) && !data.queryConfig.providers.length) {
|
|
34
|
-
throw new Error("Cannot sent a query with a defined empty array of providers, because this is an impossible query for complete.");
|
|
35
|
-
}
|
|
36
|
-
|
|
37
|
-
if (Array.isArray(data.queryConfig.types) && !data.queryConfig.types.length) {
|
|
38
|
-
throw new Error("Cannot sent a query with a defined empty array of types, because this is an impossible query for complete.");
|
|
39
|
-
}
|
|
40
|
-
|
|
41
|
-
const query = await this.clientController.query(data);
|
|
42
|
-
|
|
43
|
-
this.logger.info(`[${data.commandId}] Operation query with config ${JSON.stringify(data.queryConfig)} completed.`);
|
|
44
|
-
|
|
45
|
-
return query;
|
|
46
|
-
}
|
|
47
|
-
|
|
48
|
-
public async registerProvider(data: { config: IOConnectSearch.ProviderRegistrationConfig, commandId: string }): Promise<IOConnectSearch.Provider> {
|
|
49
|
-
this.logger.info(`[${data.commandId}] Starting registerProvider operation with config ${JSON.stringify(data.config)}`);
|
|
50
|
-
|
|
51
|
-
const provider = await this.providerController.processRegisterProvider(data);
|
|
52
|
-
|
|
53
|
-
this.logger.info(`[${data.commandId}] Operation registerProvider with config ${JSON.stringify(data.config)} completed.`);
|
|
54
|
-
|
|
55
|
-
return provider;
|
|
56
|
-
}
|
|
57
|
-
|
|
58
|
-
public async providers(data: { commandId: string }): Promise<IOConnectSearch.ProviderData[]> {
|
|
59
|
-
this.logger.info(`[${data.commandId}] Starting providers operation.`);
|
|
60
|
-
|
|
61
|
-
const allProvidersInfo = await this.glueController.getAllProvidersInfo();
|
|
62
|
-
|
|
63
|
-
const allProvidersData = allProvidersInfo.flatMap((provInfo) => provInfo.info.providers);
|
|
64
|
-
|
|
65
|
-
this.logger.info(`[${data.commandId}] Operation providers completed.`);
|
|
66
|
-
|
|
67
|
-
return allProvidersData;
|
|
68
|
-
}
|
|
69
|
-
|
|
70
|
-
public async types(data: { commandId: string }): Promise<IOConnectSearch.SearchType[]> {
|
|
71
|
-
this.logger.info(`[${data.commandId}] Starting types operation.`);
|
|
72
|
-
|
|
73
|
-
const allProvidersInfo = await this.glueController.getAllProvidersInfo();
|
|
74
|
-
|
|
75
|
-
const allProvidersData = allProvidersInfo.flatMap((provInfo) => provInfo.info.providers);
|
|
76
|
-
|
|
77
|
-
const allSupportedTypes = allProvidersData.filter((provData) => !!provData.types).flatMap((provData) => provData.types as IOConnectSearch.SearchType[]);
|
|
78
|
-
|
|
79
|
-
const uniqueSupportedTypes = [...new Set(allSupportedTypes)];
|
|
80
|
-
|
|
81
|
-
this.logger.info(`[${data.commandId}] Operation types completed.`);
|
|
82
|
-
|
|
83
|
-
return uniqueSupportedTypes;
|
|
84
|
-
}
|
|
85
|
-
}
|