@interopio/search-api 2.5.2 → 3.0.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,475 +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 { ProviderModel } from "../models/provider";
|
|
7
|
-
import { AsyncSequelizer } from "../services/sequelizer";
|
|
8
|
-
import { GlueController } from "./glue";
|
|
9
|
-
import {
|
|
10
|
-
default as CallbackRegistryFactory,
|
|
11
|
-
CallbackRegistry
|
|
12
|
-
} from "callback-registry";
|
|
13
|
-
import { operationDecoder, queryConfigDecoder, searchCancelRequestDecoder } from "../shared/decoders";
|
|
14
|
-
import { ActiveQuery, LegacySearchRequest, ProviderQueryDoneCommand, ProviderQueryErrorCommand, ProviderQueryResultCommand, SearchCancelRequest } from "../shared/types";
|
|
15
|
-
import { STALE_QUERY_TIMEOUT_MS } from "../shared/constants";
|
|
16
|
-
import { CLIENT_TO_PROVIDER_PROTOCOL_OPERATIONS, ProtocolProviderInfoResponse, ProtocolSearchRequest, ProtocolSearchResponse } from "../shared/protocol";
|
|
17
|
-
import { LimitsTracker } from "../services/limits-tracker";
|
|
18
|
-
import { ModelFactory } from "../services/model-factory";
|
|
19
|
-
import { extractErrorMsg } from "../shared/utils";
|
|
20
|
-
|
|
21
|
-
export class ProviderController {
|
|
22
|
-
|
|
23
|
-
private readonly registry: CallbackRegistry = CallbackRegistryFactory();
|
|
24
|
-
private readonly providersModels: { [key in string]: ProviderModel } = {};
|
|
25
|
-
private readonly activeQueries: { [key in string]: ActiveQuery } = {};
|
|
26
|
-
|
|
27
|
-
constructor(
|
|
28
|
-
private readonly logger: IOConnectCore.Logger.API,
|
|
29
|
-
private readonly glueController: GlueController,
|
|
30
|
-
private readonly sequelizer: AsyncSequelizer,
|
|
31
|
-
private readonly limitsTracker: LimitsTracker,
|
|
32
|
-
private readonly modelsFactory: ModelFactory
|
|
33
|
-
) {}
|
|
34
|
-
|
|
35
|
-
public async processRegisterProvider(data: { config: IOConnectSearch.ProviderRegistrationConfig, commandId: string }): Promise<IOConnectSearch.Provider> {
|
|
36
|
-
|
|
37
|
-
const { config, commandId } = data;
|
|
38
|
-
|
|
39
|
-
this.logger.info(`[${commandId}] enqueueing the provider registration process with config: ${JSON.stringify(config)}`);
|
|
40
|
-
|
|
41
|
-
const result = await this.sequelizer.enqueue<IOConnectSearch.Provider>(async () => {
|
|
42
|
-
|
|
43
|
-
const allProvidersInfo = await this.glueController.getAllProvidersInfo();
|
|
44
|
-
|
|
45
|
-
const allProvidersData = allProvidersInfo.flatMap((provInfo) => provInfo.info.providers);
|
|
46
|
-
|
|
47
|
-
if (allProvidersData.some((providerData) => providerData && providerData.name === config.name)) {
|
|
48
|
-
throw new Error(`Cannot register a new provider with name: ${config.name}, because there already is a provider with this name`);
|
|
49
|
-
}
|
|
50
|
-
|
|
51
|
-
await this.glueController.registerMainProviderMethod(this.handleSearchQueryRequest.bind(this));
|
|
52
|
-
|
|
53
|
-
const modelData: IOConnectSearch.ProviderData = {
|
|
54
|
-
id: nanoid(10),
|
|
55
|
-
name: config.name,
|
|
56
|
-
interopId: this.glueController.myInteropId,
|
|
57
|
-
appName: this.glueController.myAppName,
|
|
58
|
-
types: config.types
|
|
59
|
-
};
|
|
60
|
-
|
|
61
|
-
const model = this.modelsFactory.buildProviderModel(modelData, this);
|
|
62
|
-
|
|
63
|
-
this.providersModels[modelData.id] = model;
|
|
64
|
-
|
|
65
|
-
return model.exposeFacade();
|
|
66
|
-
});
|
|
67
|
-
|
|
68
|
-
this.logger.info(`[${commandId}] the provider with name: ${config.name} has been registered.`);
|
|
69
|
-
|
|
70
|
-
return result;
|
|
71
|
-
}
|
|
72
|
-
|
|
73
|
-
public processProviderOnQuery(data: { callback: (query: IOConnectSearch.ProviderQuery) => void, id: string, commandId: string }): IOConnectSearch.UnsubscribeFunction {
|
|
74
|
-
return this.registry.add(`on-search-query-${data.id}`, data.callback);
|
|
75
|
-
}
|
|
76
|
-
|
|
77
|
-
public processProviderOnQueryCancel(data: { callback: (query: { id: string; }) => void, id: string, commandId: string }): IOConnectSearch.UnsubscribeFunction {
|
|
78
|
-
return this.registry.add(`on-cancel-query-${data.id}`, data.callback);
|
|
79
|
-
}
|
|
80
|
-
|
|
81
|
-
public async processProviderUnregister(data: { id: string, commandId: string }): Promise<void> {
|
|
82
|
-
|
|
83
|
-
this.logger.info(`[${data.commandId}] enqueueing the provider un-registration with id: ${data.id}`);
|
|
84
|
-
|
|
85
|
-
await this.sequelizer.enqueue<void>(async () => {
|
|
86
|
-
this.cleanUpProvider(data.id, data.commandId);
|
|
87
|
-
|
|
88
|
-
if (Object.keys(this.providersModels).length) {
|
|
89
|
-
return;
|
|
90
|
-
}
|
|
91
|
-
|
|
92
|
-
await this.glueController.clearMainProviderMethod();
|
|
93
|
-
});
|
|
94
|
-
|
|
95
|
-
this.logger.info(`[${data.commandId}] the provider un-registration with id: ${data.id} completed`);
|
|
96
|
-
|
|
97
|
-
}
|
|
98
|
-
|
|
99
|
-
public async processProviderQueryDone(command: ProviderQueryDoneCommand): Promise<void> {
|
|
100
|
-
|
|
101
|
-
const { commandId, identification } = command;
|
|
102
|
-
|
|
103
|
-
this.activeQueries[identification.queryId]?.publisher.syncSuspendProvider(identification.providerId, commandId);
|
|
104
|
-
|
|
105
|
-
await this.sequelizer.enqueue<void>(async () => {
|
|
106
|
-
|
|
107
|
-
this.logger.trace(`[${commandId}] Processing a query done command with identification: ${JSON.stringify(identification)}`);
|
|
108
|
-
|
|
109
|
-
const activeQuery = this.activeQueries[identification.queryId];
|
|
110
|
-
|
|
111
|
-
if (!activeQuery) {
|
|
112
|
-
this.logger.warn(`[${commandId}] Cannot mark provider: ${identification.providerId} done with query ${identification.queryId}, because there is no active query with this id`);
|
|
113
|
-
return;
|
|
114
|
-
}
|
|
115
|
-
|
|
116
|
-
await this.cleanUpProviderQuery(identification.queryId, identification.providerId, commandId);
|
|
117
|
-
|
|
118
|
-
if (activeQuery.providersAtWork.length) {
|
|
119
|
-
this.logger.trace(`[${commandId}] Query done command completed, but there are more providers still at work.`);
|
|
120
|
-
|
|
121
|
-
return;
|
|
122
|
-
}
|
|
123
|
-
|
|
124
|
-
this.cleanUpQuery(identification.queryId, commandId);
|
|
125
|
-
|
|
126
|
-
this.logger.trace(`[${commandId}] Query is completed, signalling.`);
|
|
127
|
-
});
|
|
128
|
-
}
|
|
129
|
-
|
|
130
|
-
public processProviderQueryError(command: ProviderQueryErrorCommand): Promise<void> {
|
|
131
|
-
const { commandId, identification, error } = command;
|
|
132
|
-
|
|
133
|
-
this.logger.warn(`[${commandId}] Processing an error sent by provider: ${identification.providerId} for query id: ${identification.queryId} -> ${error}`);
|
|
134
|
-
|
|
135
|
-
this.activeQueries[identification.queryId]?.publisher.markProviderError(command);
|
|
136
|
-
|
|
137
|
-
return this.processProviderQueryDone(command);
|
|
138
|
-
}
|
|
139
|
-
|
|
140
|
-
public processProviderQueryResult(command: ProviderQueryResultCommand): void {
|
|
141
|
-
const { commandId, identification } = command;
|
|
142
|
-
|
|
143
|
-
const activeQuery = this.activeQueries[identification.queryId];
|
|
144
|
-
|
|
145
|
-
if (!activeQuery) {
|
|
146
|
-
const errorMessage = `Will not send this result to the client, because there is no active query with id ${identification.queryId}. Most likely this query was cancelled.`;
|
|
147
|
-
this.logger.warn(`[${command}] ${errorMessage}`);
|
|
148
|
-
throw new Error(errorMessage);
|
|
149
|
-
}
|
|
150
|
-
|
|
151
|
-
if (activeQuery.publisher.checkProviderSuspended(identification.providerId)) {
|
|
152
|
-
const errorMessage = `Will not send this result to the client, because there is no info about this provider in the active query with id ${identification.queryId}. Most likely this query was marked as done by this provider already.`;
|
|
153
|
-
this.logger.warn(`[${command}] ${errorMessage}`);
|
|
154
|
-
throw new Error(errorMessage);
|
|
155
|
-
}
|
|
156
|
-
|
|
157
|
-
const requestedTypes = activeQuery.requestedTypes;
|
|
158
|
-
|
|
159
|
-
if (requestedTypes && requestedTypes.every((searchType) => searchType.name !== command.result.type.name)) {
|
|
160
|
-
const errorMessage = `Will not send this result to the client, because this result has a defined type: ${command.result.type.name} which is not in the explicitly requested list of types by the client.`;
|
|
161
|
-
this.logger.warn(`[${command}] ${errorMessage}`);
|
|
162
|
-
throw new Error(errorMessage);
|
|
163
|
-
}
|
|
164
|
-
|
|
165
|
-
const testResult = this.limitsTracker.testResultLimit(command);
|
|
166
|
-
|
|
167
|
-
if (testResult?.maxLimitHit) {
|
|
168
|
-
const errorMessage = `Will not process this result from provider ${command.identification.providerId}, because this provider has reached the max results limit set by the client. This provider cannot send more result, marking it as done.`;
|
|
169
|
-
this.logger.info(errorMessage);
|
|
170
|
-
setTimeout(() => this.processProviderQueryDone(command), 0);
|
|
171
|
-
throw new Error(errorMessage);
|
|
172
|
-
}
|
|
173
|
-
|
|
174
|
-
if (testResult?.maxLimitPerTypeHit) {
|
|
175
|
-
const errorMessage = `Will not process this result from provider ${command.identification.providerId}, because this provider has reached the max results limit per type as set by the client.`;
|
|
176
|
-
this.logger.info(errorMessage);
|
|
177
|
-
throw new Error(errorMessage);
|
|
178
|
-
}
|
|
179
|
-
|
|
180
|
-
this.logger.trace(`[${commandId}] An active query for query ${identification.queryId} was found and the provider is within limits, queueing the result`);
|
|
181
|
-
|
|
182
|
-
this.limitsTracker.update(command);
|
|
183
|
-
|
|
184
|
-
activeQuery.publisher.queueResult(command);
|
|
185
|
-
|
|
186
|
-
this.logger.trace(`[${commandId}] The query result was queued successfully.`);
|
|
187
|
-
|
|
188
|
-
}
|
|
189
|
-
|
|
190
|
-
private async handleSearchQueryRequest(args: any, caller: IOConnectCore.Interop.Instance): Promise<any> {
|
|
191
|
-
const { operation } = args;
|
|
192
|
-
|
|
193
|
-
const validatedOperation = operationDecoder.runWithException(operation);
|
|
194
|
-
|
|
195
|
-
const commandId = nanoid(10);
|
|
196
|
-
|
|
197
|
-
switch (validatedOperation) {
|
|
198
|
-
case CLIENT_TO_PROVIDER_PROTOCOL_OPERATIONS.info:
|
|
199
|
-
return this.handleInfoOperation({ commandId });
|
|
200
|
-
case CLIENT_TO_PROVIDER_PROTOCOL_OPERATIONS.search:
|
|
201
|
-
return this.handleSearchOperation({ args, commandId }, caller);
|
|
202
|
-
case CLIENT_TO_PROVIDER_PROTOCOL_OPERATIONS.cancel:
|
|
203
|
-
return this.handleCancelOperation({ args, commandId });
|
|
204
|
-
default:
|
|
205
|
-
throw new Error(`Unrecognized operation: ${operation}`);
|
|
206
|
-
}
|
|
207
|
-
}
|
|
208
|
-
|
|
209
|
-
private async handleInfoOperation(request: { commandId: string }): Promise<ProtocolProviderInfoResponse> {
|
|
210
|
-
|
|
211
|
-
this.logger.info(`[${request.commandId}] handling an info operation`);
|
|
212
|
-
|
|
213
|
-
const allSupportedTypes: IOConnectSearch.SearchType[] = Object.values(this.providersModels).flatMap((providerModel) => providerModel.myProviderData.types || []);
|
|
214
|
-
|
|
215
|
-
const uniqueSupportedTypes = [...new Set(allSupportedTypes)];
|
|
216
|
-
|
|
217
|
-
const hasWildCardTypeProvider = Object.values(this.providersModels).some((providerModel) => !providerModel.myProviderData.types);
|
|
218
|
-
|
|
219
|
-
if (hasWildCardTypeProvider) {
|
|
220
|
-
uniqueSupportedTypes.push({ name: "*" });
|
|
221
|
-
}
|
|
222
|
-
|
|
223
|
-
const providers: IOConnectSearch.ProviderData[] = Object.values(this.providersModels).map((providerModel) => providerModel.myProviderData);
|
|
224
|
-
|
|
225
|
-
const response: ProtocolProviderInfoResponse = {
|
|
226
|
-
supportedTypes: uniqueSupportedTypes.map((supportedType) => supportedType.name),
|
|
227
|
-
providers: providers,
|
|
228
|
-
apiVersion: "1"
|
|
229
|
-
};
|
|
230
|
-
|
|
231
|
-
this.logger.info(`[${request.commandId}] responding to an info operation with: ${JSON.stringify(response)}`);
|
|
232
|
-
|
|
233
|
-
return response;
|
|
234
|
-
|
|
235
|
-
}
|
|
236
|
-
|
|
237
|
-
private async handleSearchOperation(request: { args: ProtocolSearchRequest | LegacySearchRequest, commandId: string }, caller: IOConnectCore.Interop.Instance): Promise<ProtocolSearchResponse> {
|
|
238
|
-
const commandId = request.commandId;
|
|
239
|
-
|
|
240
|
-
const queryId = this.generateQueryId();
|
|
241
|
-
|
|
242
|
-
this.logger.info(`[${commandId}] Processing search operation with queryId: ${queryId} request details: ${JSON.stringify(request.args)}`);
|
|
243
|
-
|
|
244
|
-
const isLegacyRequest = this.checkRequestLegacy(request.args);
|
|
245
|
-
|
|
246
|
-
const validatedRequest = this.prepareRequest(request.args, isLegacyRequest, commandId);
|
|
247
|
-
|
|
248
|
-
this.logger.info(`[${commandId}] Search operation with queryId: ${queryId} is validated. Creating an active query and enqueueing calling the providers.`);
|
|
249
|
-
|
|
250
|
-
this.activeQueries[queryId] = {
|
|
251
|
-
queryId,
|
|
252
|
-
callerInstanceId: caller.instance as string,
|
|
253
|
-
providersAtWork: [],
|
|
254
|
-
requestedTypes: validatedRequest.types,
|
|
255
|
-
publisher: this.modelsFactory.buildPublisher(caller.instance as string, queryId, isLegacyRequest),
|
|
256
|
-
staleTimer: this.setClearStaleQueryTimer(queryId)
|
|
257
|
-
};
|
|
258
|
-
|
|
259
|
-
if (validatedRequest.providerLimits) {
|
|
260
|
-
this.limitsTracker.enableTracking(validatedRequest.providerLimits, queryId);
|
|
261
|
-
}
|
|
262
|
-
|
|
263
|
-
// needed to allow the search clients to process the query id
|
|
264
|
-
setTimeout(() => {
|
|
265
|
-
this.sequelizer.enqueue<void>(async () => {
|
|
266
|
-
try {
|
|
267
|
-
this.logger.info(`[${commandId}] Calling the providers.`);
|
|
268
|
-
this.callProviders(validatedRequest, queryId, commandId);
|
|
269
|
-
} catch (error) {
|
|
270
|
-
this.logger.error(`[${commandId}] Error calling the providers: ${extractErrorMsg(error)}`);
|
|
271
|
-
}
|
|
272
|
-
});
|
|
273
|
-
}, 0);
|
|
274
|
-
|
|
275
|
-
this.logger.info(`[${commandId}] Search operation with queryID: ${queryId} processed successfully.`);
|
|
276
|
-
|
|
277
|
-
return { id: queryId };
|
|
278
|
-
}
|
|
279
|
-
|
|
280
|
-
private async handleCancelOperation(request: { args: SearchCancelRequest, commandId: string }): Promise<void> {
|
|
281
|
-
|
|
282
|
-
await this.sequelizer.enqueue<void>(async () => {
|
|
283
|
-
const validation = searchCancelRequestDecoder.run(request.args);
|
|
284
|
-
|
|
285
|
-
if (!validation.ok) {
|
|
286
|
-
const errorMsg = `Cannot process a cancel request, because of validation error: ${JSON.stringify(validation.error)}`;
|
|
287
|
-
|
|
288
|
-
this.logger.warn(`[${request.commandId}] ${errorMsg}`);
|
|
289
|
-
throw new Error(errorMsg);
|
|
290
|
-
}
|
|
291
|
-
|
|
292
|
-
const validatedRequest = validation.result;
|
|
293
|
-
|
|
294
|
-
const activeQuery = this.activeQueries[validatedRequest.id];
|
|
295
|
-
|
|
296
|
-
if (!activeQuery) {
|
|
297
|
-
return;
|
|
298
|
-
}
|
|
299
|
-
|
|
300
|
-
clearTimeout(activeQuery.staleTimer);
|
|
301
|
-
|
|
302
|
-
activeQuery.publisher.cancel(request.commandId);
|
|
303
|
-
|
|
304
|
-
delete this.activeQueries[validatedRequest.id];
|
|
305
|
-
|
|
306
|
-
activeQuery.providersAtWork.forEach((provider) => this.registry.execute(`on-cancel-query-${provider.myProviderData.id}`, { id: validatedRequest.id }));
|
|
307
|
-
});
|
|
308
|
-
|
|
309
|
-
}
|
|
310
|
-
|
|
311
|
-
private generateQueryId(): string {
|
|
312
|
-
const queryId = nanoid(10);
|
|
313
|
-
|
|
314
|
-
if (this.activeQueries[queryId]) {
|
|
315
|
-
return this.generateQueryId();
|
|
316
|
-
}
|
|
317
|
-
|
|
318
|
-
return queryId;
|
|
319
|
-
}
|
|
320
|
-
|
|
321
|
-
private translateLegacySearchRequest(legacyRequest: LegacySearchRequest): IOConnectSearch.QueryConfig {
|
|
322
|
-
return {
|
|
323
|
-
search: legacyRequest.search,
|
|
324
|
-
types: legacyRequest.types?.map<IOConnectSearch.SearchType>((searchType) => ({ name: searchType })),
|
|
325
|
-
providerLimits: {
|
|
326
|
-
maxResults: legacyRequest.limit,
|
|
327
|
-
maxResultsPerType: legacyRequest.categoryLimit
|
|
328
|
-
}
|
|
329
|
-
};
|
|
330
|
-
}
|
|
331
|
-
|
|
332
|
-
private checkRequestLegacy(searchRequest: ProtocolSearchRequest | LegacySearchRequest): boolean {
|
|
333
|
-
return typeof (searchRequest as ProtocolSearchRequest).apiVersion === "undefined";
|
|
334
|
-
}
|
|
335
|
-
|
|
336
|
-
private callProviders(validatedRequest: IOConnectSearch.QueryConfig, queryId: string, commandId: string): void {
|
|
337
|
-
let providers: ProviderModel[] = validatedRequest.providers ?
|
|
338
|
-
this.getFilteredProviderModels(validatedRequest.providers)
|
|
339
|
-
: Object.values(this.providersModels);
|
|
340
|
-
|
|
341
|
-
this.logger.trace(`[${commandId}] initial providers filtration yielded: ${JSON.stringify(providers.map((p) => p.myProviderData.name).join(", "))}`);
|
|
342
|
-
|
|
343
|
-
providers = validatedRequest.types ? this.getFilteredProvidersBySearchTypes(providers, validatedRequest.types) : providers;
|
|
344
|
-
|
|
345
|
-
this.logger.trace(`[${commandId}] search type providers filtration yielded: ${JSON.stringify(providers.map((p) => p.myProviderData.name).join(", "))}`);
|
|
346
|
-
|
|
347
|
-
this.activeQueries[queryId].publisher.configureProviders(providers);
|
|
348
|
-
|
|
349
|
-
this.activeQueries[queryId].providersAtWork.push(...providers);
|
|
350
|
-
|
|
351
|
-
providers.forEach((provider) => this.callProvider(provider, validatedRequest, queryId, commandId));
|
|
352
|
-
}
|
|
353
|
-
|
|
354
|
-
private callProvider(provider: ProviderModel, validatedRequest: IOConnectSearch.QueryConfig, queryId: string, commandId: string): void {
|
|
355
|
-
const queryModel = this.modelsFactory.buildProviderQueryModel(validatedRequest, { queryId, providerId: provider.myProviderData.id }, this);
|
|
356
|
-
|
|
357
|
-
const queryFacade = queryModel.exposeFacade();
|
|
358
|
-
|
|
359
|
-
this.logger.info(`[${commandId}] The query facade for provider: ${provider.myProviderData.id} with name ${provider.myProviderData.name} is ready, raising the event for query ID: ${queryId}.`);
|
|
360
|
-
|
|
361
|
-
this.registry.execute(`on-search-query-${provider.myProviderData.id}`, queryFacade);
|
|
362
|
-
}
|
|
363
|
-
|
|
364
|
-
private getFilteredProviderModels(providers: IOConnectSearch.ProviderData[]): ProviderModel[] {
|
|
365
|
-
|
|
366
|
-
const filtered = providers.reduce<ProviderModel[]>((providers, provider) => {
|
|
367
|
-
if (this.providersModels[provider.id]) {
|
|
368
|
-
providers.push(this.providersModels[provider.id]);
|
|
369
|
-
}
|
|
370
|
-
|
|
371
|
-
return providers;
|
|
372
|
-
}, []);
|
|
373
|
-
|
|
374
|
-
return filtered;
|
|
375
|
-
}
|
|
376
|
-
|
|
377
|
-
private getFilteredProvidersBySearchTypes(providers: ProviderModel[], searchTypes: IOConnectSearch.SearchType[]): ProviderModel[] {
|
|
378
|
-
|
|
379
|
-
const filtered = providers.filter((provider) => {
|
|
380
|
-
|
|
381
|
-
// a provider with no registered types should receive all queries
|
|
382
|
-
if (!provider.myProviderData.types || !provider.myProviderData.types.length) {
|
|
383
|
-
return true;
|
|
384
|
-
}
|
|
385
|
-
|
|
386
|
-
return provider.myProviderData.types?.some((providerSearchType) => searchTypes.some((searchType) => searchType.name === providerSearchType.name));
|
|
387
|
-
});
|
|
388
|
-
|
|
389
|
-
return filtered;
|
|
390
|
-
}
|
|
391
|
-
|
|
392
|
-
private setClearStaleQueryTimer(queryId: string): NodeJS.Timeout {
|
|
393
|
-
return setTimeout(() => {
|
|
394
|
-
|
|
395
|
-
const commandId = nanoid(10);
|
|
396
|
-
|
|
397
|
-
this.logger.info(`[${commandId}] Stale query timer is activated for queryId: ${queryId}`);
|
|
398
|
-
|
|
399
|
-
const activeQuery = this.activeQueries[queryId];
|
|
400
|
-
|
|
401
|
-
if (!activeQuery) {
|
|
402
|
-
this.logger.info(`[${commandId}] No active query was found, this was a false activation.`);
|
|
403
|
-
|
|
404
|
-
return;
|
|
405
|
-
}
|
|
406
|
-
|
|
407
|
-
this.logger.info(`[${commandId}] force-marking the query as done`);
|
|
408
|
-
|
|
409
|
-
this.cleanUpQuery(queryId, commandId);
|
|
410
|
-
|
|
411
|
-
this.logger.info(`[${commandId}] the stale query was cleared.`);
|
|
412
|
-
|
|
413
|
-
}, STALE_QUERY_TIMEOUT_MS);
|
|
414
|
-
}
|
|
415
|
-
|
|
416
|
-
private prepareRequest(searchRequest: LegacySearchRequest | IOConnectSearch.QueryConfig, isLegacyRequest: boolean, commandId: string): IOConnectSearch.QueryConfig {
|
|
417
|
-
const parsedRequest = isLegacyRequest ? this.translateLegacySearchRequest(searchRequest as LegacySearchRequest) : searchRequest;
|
|
418
|
-
|
|
419
|
-
const validation = queryConfigDecoder.run(parsedRequest);
|
|
420
|
-
|
|
421
|
-
if (!validation.ok) {
|
|
422
|
-
const errorMsg = `Cannot process a search request, because of validation error: ${JSON.stringify(validation.error)}`;
|
|
423
|
-
|
|
424
|
-
this.logger.warn(`[${commandId}] ${errorMsg}`);
|
|
425
|
-
throw new Error(errorMsg);
|
|
426
|
-
}
|
|
427
|
-
|
|
428
|
-
const validatedRequest = validation.result;
|
|
429
|
-
|
|
430
|
-
return validatedRequest;
|
|
431
|
-
}
|
|
432
|
-
|
|
433
|
-
private cleanUpQuery(queryId: string, commandId: string): void {
|
|
434
|
-
const activeQuery = this.activeQueries[queryId];
|
|
435
|
-
|
|
436
|
-
clearTimeout(activeQuery.staleTimer);
|
|
437
|
-
|
|
438
|
-
activeQuery.publisher.cleanPublisher(commandId);
|
|
439
|
-
|
|
440
|
-
delete this.activeQueries[queryId];
|
|
441
|
-
this.limitsTracker.cleanTracking(queryId);
|
|
442
|
-
}
|
|
443
|
-
|
|
444
|
-
private cleanUpProvider(providerId: string, commandId: string): void {
|
|
445
|
-
this.registry.clearKey(`on-search-query-${providerId}`);
|
|
446
|
-
this.registry.clearKey(`on-cancel-query-${providerId}`);
|
|
447
|
-
|
|
448
|
-
delete this.providersModels[providerId];
|
|
449
|
-
|
|
450
|
-
const queriesWithProvider = Object.values(this.activeQueries).filter((query) => !query.publisher.checkProviderSuspended(providerId));
|
|
451
|
-
|
|
452
|
-
queriesWithProvider.forEach((query) => {
|
|
453
|
-
this.processProviderQueryDone({
|
|
454
|
-
identification: {
|
|
455
|
-
queryId: query.queryId,
|
|
456
|
-
providerId
|
|
457
|
-
},
|
|
458
|
-
commandId
|
|
459
|
-
});
|
|
460
|
-
});
|
|
461
|
-
}
|
|
462
|
-
|
|
463
|
-
private async cleanUpProviderQuery(queryId: string, providerId: string, commandId: string): Promise<void> {
|
|
464
|
-
const activeQuery = this.activeQueries[queryId];
|
|
465
|
-
|
|
466
|
-
if (!activeQuery) {
|
|
467
|
-
this.logger.warn(`[${commandId}] Cannot clean up a provider query ${queryId} for provider ${providerId} because there is no such active query`);
|
|
468
|
-
return;
|
|
469
|
-
}
|
|
470
|
-
|
|
471
|
-
activeQuery.providersAtWork = activeQuery.providersAtWork.filter((provider) => provider.myProviderData.id !== providerId);
|
|
472
|
-
|
|
473
|
-
await activeQuery.publisher.markProviderDone(providerId, commandId);
|
|
474
|
-
}
|
|
475
|
-
}
|
package/src/facade.ts
DELETED
|
@@ -1,69 +0,0 @@
|
|
|
1
|
-
import { IOConnectSearch } from "../search";
|
|
2
|
-
import { MainController } from "./controllers/main";
|
|
3
|
-
import { nonNegativeNumberDecoder, providerRegistrationConfig, queryConfigDecoder } from "./shared/decoders";
|
|
4
|
-
import { nanoid } from "nanoid/non-secure";
|
|
5
|
-
import { version } from "../package.json";
|
|
6
|
-
|
|
7
|
-
export class SearchFacade {
|
|
8
|
-
|
|
9
|
-
constructor(
|
|
10
|
-
private readonly main: MainController
|
|
11
|
-
) {}
|
|
12
|
-
|
|
13
|
-
public exposeApi(): IOConnectSearch.API {
|
|
14
|
-
|
|
15
|
-
const api: IOConnectSearch.API = {
|
|
16
|
-
version,
|
|
17
|
-
setDebounceMS: this.setDebounceMS.bind(this),
|
|
18
|
-
getDebounceMS: this.getDebounceMS.bind(this),
|
|
19
|
-
listProviders: this.providers.bind(this),
|
|
20
|
-
listTypes: this.types.bind(this),
|
|
21
|
-
query: this.query.bind(this),
|
|
22
|
-
registerProvider: this.registerProvider.bind(this)
|
|
23
|
-
};
|
|
24
|
-
|
|
25
|
-
return Object.freeze(api);
|
|
26
|
-
}
|
|
27
|
-
|
|
28
|
-
private setDebounceMS(milliseconds: number): void {
|
|
29
|
-
nonNegativeNumberDecoder.runWithException(milliseconds);
|
|
30
|
-
|
|
31
|
-
const commandId = nanoid(10);
|
|
32
|
-
|
|
33
|
-
return this.main.setDebounceMS({ milliseconds, commandId });
|
|
34
|
-
}
|
|
35
|
-
|
|
36
|
-
private getDebounceMS(): number {
|
|
37
|
-
const commandId = nanoid(10);
|
|
38
|
-
|
|
39
|
-
return this.main.getDebounceMS({ commandId });
|
|
40
|
-
}
|
|
41
|
-
|
|
42
|
-
private async providers(): Promise<IOConnectSearch.ProviderData[]> {
|
|
43
|
-
const commandId = nanoid(10);
|
|
44
|
-
|
|
45
|
-
return this.main.providers({ commandId });
|
|
46
|
-
}
|
|
47
|
-
|
|
48
|
-
private async types(): Promise<IOConnectSearch.SearchType[]> {
|
|
49
|
-
const commandId = nanoid(10);
|
|
50
|
-
|
|
51
|
-
return this.main.types({ commandId });
|
|
52
|
-
}
|
|
53
|
-
|
|
54
|
-
private async query(queryConfig: IOConnectSearch.QueryConfig): Promise<IOConnectSearch.Query> {
|
|
55
|
-
const verifiedConfig = queryConfigDecoder.runWithException(queryConfig);
|
|
56
|
-
|
|
57
|
-
const commandId = nanoid(10);
|
|
58
|
-
|
|
59
|
-
return this.main.query({ queryConfig: verifiedConfig, commandId });
|
|
60
|
-
}
|
|
61
|
-
|
|
62
|
-
private async registerProvider(config: IOConnectSearch.ProviderRegistrationConfig): Promise<IOConnectSearch.Provider> {
|
|
63
|
-
const verifiedConfig = providerRegistrationConfig.runWithException(config);
|
|
64
|
-
|
|
65
|
-
const commandId = nanoid(10);
|
|
66
|
-
|
|
67
|
-
return this.main.registerProvider({ config: verifiedConfig, commandId });
|
|
68
|
-
}
|
|
69
|
-
}
|
package/src/index.ts
DELETED
|
@@ -1,19 +0,0 @@
|
|
|
1
|
-
/* eslint-disable @typescript-eslint/no-explicit-any */
|
|
2
|
-
import { IOConnectSearchFactoryFunction, IOConnectSearch } from "../search.d";
|
|
3
|
-
import { IOConnectCore } from "@interopio/core";
|
|
4
|
-
import { IoC } from "./shared/ioc";
|
|
5
|
-
|
|
6
|
-
const factoryFunction: IOConnectSearchFactoryFunction = async (io: IOConnectCore.API, config?: IOConnectSearch.Config): Promise<void> => {
|
|
7
|
-
|
|
8
|
-
const ioc = new IoC(io, config);
|
|
9
|
-
|
|
10
|
-
(io as any).search = ioc.facade.exposeApi();
|
|
11
|
-
};
|
|
12
|
-
|
|
13
|
-
// attach to window object
|
|
14
|
-
if (typeof window !== "undefined") {
|
|
15
|
-
/* eslint-disable @typescript-eslint/no-explicit-any */
|
|
16
|
-
(window as any).IOSearch = factoryFunction;
|
|
17
|
-
}
|
|
18
|
-
|
|
19
|
-
export default factoryFunction;
|
|
@@ -1,83 +0,0 @@
|
|
|
1
|
-
import { IOConnectCore } from "@interopio/core";
|
|
2
|
-
import { nanoid } from "nanoid/non-secure";
|
|
3
|
-
import { IOConnectSearch } from "../../search";
|
|
4
|
-
import { ClientController } from "../controllers/client";
|
|
5
|
-
|
|
6
|
-
export class ClientQuery {
|
|
7
|
-
|
|
8
|
-
constructor(
|
|
9
|
-
private readonly controller: ClientController,
|
|
10
|
-
private readonly logger: IOConnectCore.Logger.API,
|
|
11
|
-
private readonly masterQueryId: string
|
|
12
|
-
) {}
|
|
13
|
-
|
|
14
|
-
public exposeFacade(): IOConnectSearch.Query {
|
|
15
|
-
const providerQueryFacade: IOConnectSearch.Query = {
|
|
16
|
-
cancel: this.cancel.bind(this),
|
|
17
|
-
onResults: this.onResults.bind(this),
|
|
18
|
-
onCompleted: this.onCompleted.bind(this),
|
|
19
|
-
onError: this.onError.bind(this)
|
|
20
|
-
};
|
|
21
|
-
|
|
22
|
-
return Object.freeze(providerQueryFacade);
|
|
23
|
-
}
|
|
24
|
-
|
|
25
|
-
private async cancel(): Promise<void> {
|
|
26
|
-
const commandId = nanoid(10);
|
|
27
|
-
|
|
28
|
-
this.logger.info(`[${commandId}] received a valid query cancel request, forwarding to the controller.`);
|
|
29
|
-
|
|
30
|
-
await this.controller.cancelQuery(this.masterQueryId, commandId);
|
|
31
|
-
|
|
32
|
-
this.logger.info(`[${commandId}] the cancel request was completed.`);
|
|
33
|
-
}
|
|
34
|
-
|
|
35
|
-
private onResults(callback: (resultBatch: IOConnectSearch.QueryResultBatch) => void): IOConnectSearch.UnsubscribeFunction {
|
|
36
|
-
if (typeof callback !== "function") {
|
|
37
|
-
throw new Error("onQuery requires a callback of type function");
|
|
38
|
-
}
|
|
39
|
-
|
|
40
|
-
const commandId = nanoid(10);
|
|
41
|
-
|
|
42
|
-
this.logger.info(`[${commandId}] received a valid query onResults request, forwarding to the controller.`);
|
|
43
|
-
|
|
44
|
-
const unsubscribe = this.controller.processClientOnResults({ callback, masterQueryId: this.masterQueryId, commandId });
|
|
45
|
-
|
|
46
|
-
this.logger.info(`[${commandId}] the onResults request was completed.`);
|
|
47
|
-
|
|
48
|
-
return unsubscribe;
|
|
49
|
-
}
|
|
50
|
-
|
|
51
|
-
private onCompleted(callback: () => void): IOConnectSearch.UnsubscribeFunction {
|
|
52
|
-
if (typeof callback !== "function") {
|
|
53
|
-
throw new Error("onQuery requires a callback of type function");
|
|
54
|
-
}
|
|
55
|
-
|
|
56
|
-
const commandId = nanoid(10);
|
|
57
|
-
|
|
58
|
-
this.logger.info(`[${commandId}] received a valid query onCompleted request, forwarding to the controller.`);
|
|
59
|
-
|
|
60
|
-
const unsubscribe = this.controller.processClientOnCompleted({ callback, masterQueryId: this.masterQueryId, commandId });
|
|
61
|
-
|
|
62
|
-
this.logger.info(`[${commandId}] the onCompleted request was completed.`);
|
|
63
|
-
|
|
64
|
-
return unsubscribe;
|
|
65
|
-
}
|
|
66
|
-
|
|
67
|
-
private onError(callback: (error: IOConnectSearch.QueryError) => void): IOConnectSearch.UnsubscribeFunction {
|
|
68
|
-
if (typeof callback !== "function") {
|
|
69
|
-
throw new Error("onQuery requires a callback of type function");
|
|
70
|
-
}
|
|
71
|
-
|
|
72
|
-
const commandId = nanoid(10);
|
|
73
|
-
|
|
74
|
-
this.logger.info(`[${commandId}] received a valid query onError request, forwarding to the controller.`);
|
|
75
|
-
|
|
76
|
-
const unsubscribe = this.controller.processClientOnError({ callback, masterQueryId: this.masterQueryId, commandId });
|
|
77
|
-
|
|
78
|
-
this.logger.info(`[${commandId}] the onError request was completed.`);
|
|
79
|
-
|
|
80
|
-
return unsubscribe;
|
|
81
|
-
}
|
|
82
|
-
|
|
83
|
-
}
|