@interopio/browser-platform 3.0.1
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/browser.platform.d.ts +455 -0
- package/changelog.md +69 -0
- package/dist/browser.platform.es.js +46074 -0
- package/dist/browser.platform.es.js.map +1 -0
- package/dist/browser.platform.umd.js +46082 -0
- package/dist/browser.platform.umd.js.map +1 -0
- package/license.md +21 -0
- package/package.json +78 -0
- package/readme.md +9 -0
- package/rollup.config.js +45 -0
- package/src/common/constants.ts +46 -0
- package/src/common/defaultConfig.ts +56 -0
- package/src/common/gateway.d.ts +32 -0
- package/src/common/types.ts +222 -0
- package/src/connection/gateway.ts +132 -0
- package/src/connection/no-gateway.ts +1 -0
- package/src/connection/portsBridge.ts +441 -0
- package/src/connection/preferred.ts +247 -0
- package/src/controllers/domains.ts +156 -0
- package/src/controllers/glue.ts +604 -0
- package/src/controllers/interception.ts +53 -0
- package/src/controllers/license.ts +50 -0
- package/src/controllers/main.ts +256 -0
- package/src/controllers/plugins.ts +121 -0
- package/src/controllers/serviceWorker.ts +193 -0
- package/src/controllers/state.ts +84 -0
- package/src/controllers/system.ts +99 -0
- package/src/controllers/transactions.ts +80 -0
- package/src/factory.ts +40 -0
- package/src/fallbacks/browser.ts +47 -0
- package/src/fallbacks/dekstop.ts +21 -0
- package/src/index.ts +18 -0
- package/src/libs/applications/appStore/directory.ts +235 -0
- package/src/libs/applications/appStore/remoteWatcher.ts +112 -0
- package/src/libs/applications/controller.ts +469 -0
- package/src/libs/applications/decoders.ts +98 -0
- package/src/libs/applications/defaults.ts +6 -0
- package/src/libs/applications/types.ts +98 -0
- package/src/libs/channels/controller.ts +109 -0
- package/src/libs/channels/decoders.ts +19 -0
- package/src/libs/extension/controller.ts +120 -0
- package/src/libs/extension/decoders.ts +18 -0
- package/src/libs/extension/types.ts +11 -0
- package/src/libs/intents/constants.ts +9 -0
- package/src/libs/intents/controller.ts +500 -0
- package/src/libs/intents/decoders.ts +96 -0
- package/src/libs/intents/resolverHelper.ts +264 -0
- package/src/libs/intents/types.ts +119 -0
- package/src/libs/layouts/constants.ts +3 -0
- package/src/libs/layouts/controller.ts +476 -0
- package/src/libs/layouts/decoders.ts +132 -0
- package/src/libs/layouts/global/builder.ts +400 -0
- package/src/libs/layouts/global/resetter.ts +116 -0
- package/src/libs/layouts/global/restorer.ts +217 -0
- package/src/libs/layouts/global/validator.ts +169 -0
- package/src/libs/layouts/idbStore.ts +63 -0
- package/src/libs/layouts/types.ts +133 -0
- package/src/libs/manager/controller.ts +175 -0
- package/src/libs/manager/decoders.ts +6 -0
- package/src/libs/manager/defaults.ts +5 -0
- package/src/libs/manager/identity.ts +92 -0
- package/src/libs/manager/session.ts +195 -0
- package/src/libs/manager/types.ts +51 -0
- package/src/libs/notifications/controller.ts +447 -0
- package/src/libs/notifications/decoders.ts +166 -0
- package/src/libs/notifications/types.ts +49 -0
- package/src/libs/notifications/utils.ts +7 -0
- package/src/libs/search/controller.ts +232 -0
- package/src/libs/search/decoders.ts +6 -0
- package/src/libs/search/repositories/applications.ts +62 -0
- package/src/libs/search/repositories/layouts.ts +48 -0
- package/src/libs/search/repositories/workspaces.ts +47 -0
- package/src/libs/search/types.ts +9 -0
- package/src/libs/themes/constants.ts +9 -0
- package/src/libs/themes/controller.ts +153 -0
- package/src/libs/themes/decoders.ts +28 -0
- package/src/libs/themes/types.ts +28 -0
- package/src/libs/windows/controller.ts +498 -0
- package/src/libs/windows/decoders.ts +64 -0
- package/src/libs/windows/types.ts +59 -0
- package/src/libs/workspaces/controller.ts +922 -0
- package/src/libs/workspaces/decoders.ts +868 -0
- package/src/libs/workspaces/defaultConfig.ts +11 -0
- package/src/libs/workspaces/frames.ts +239 -0
- package/src/libs/workspaces/hibernationWatcher.ts +231 -0
- package/src/libs/workspaces/types.ts +506 -0
- package/src/platform.ts +156 -0
- package/src/shared/decoders.ts +479 -0
- package/src/shared/fetchTimeout.ts +25 -0
- package/src/shared/ioc.ts +580 -0
- package/src/shared/logger.ts +19 -0
- package/src/shared/promisePlus.ts +62 -0
- package/src/shared/sequelizer.ts +48 -0
- package/src/shared/utils.ts +86 -0
- package/src/storage/local.ts +111 -0
- package/src/storage/session.ts +436 -0
- package/tsconfig.json +16 -0
|
@@ -0,0 +1,604 @@
|
|
|
1
|
+
/* eslint-disable @typescript-eslint/no-explicit-any */
|
|
2
|
+
import IOConnect, { IOConnectCore } from "@interopio/core";
|
|
3
|
+
import { IOConnectWorkspaces } from "@interopio/workspaces-api";
|
|
4
|
+
import IOBrowser, { IOConnectBrowser, IOConnectBrowserFactoryFunction } from "@interopio/browser";
|
|
5
|
+
import { ApplicationConfig, RefreshResponseData } from "@glue42/server-api";
|
|
6
|
+
import { GlueClientControlName, GlueWebPlatformControlName, GlueWebPlatformStreamName, GlueWebPlatformWorkspacesStreamName, GlueWorkspaceFrameClientControlName, GlueWorkspacesEventsReceiverName } from "../common/constants";
|
|
7
|
+
import { BridgeOperation, GetOrCreateFrameConfig, InternalPlatformConfig, LibDomains, SessionSystemSettings } from "../common/types";
|
|
8
|
+
import { PortsBridge } from "../connection/portsBridge";
|
|
9
|
+
import { generate } from "shortid";
|
|
10
|
+
import { SessionStorageController } from "../storage/session";
|
|
11
|
+
import logger from "../shared/logger";
|
|
12
|
+
import { PromisePlus } from "../shared/promisePlus";
|
|
13
|
+
import { waitFor } from "../shared/utils";
|
|
14
|
+
import { UnsubscribeFunction } from "callback-registry";
|
|
15
|
+
import { FrameSessionData } from "../libs/workspaces/types";
|
|
16
|
+
import { IOConnectBrowserPlatform } from "../../browser.platform";
|
|
17
|
+
import { version } from "../../package.json";
|
|
18
|
+
import { nanoid } from "nanoid";
|
|
19
|
+
import { IOConnectSearch } from "@interopio/search-api";
|
|
20
|
+
|
|
21
|
+
export class GlueController {
|
|
22
|
+
private _config!: InternalPlatformConfig;
|
|
23
|
+
private _systemGlue!: IOConnectCore.API;
|
|
24
|
+
private _contextsTrackingGlue: IOConnectCore.API | undefined;
|
|
25
|
+
private _clientGlue!: IOConnectBrowser.API;
|
|
26
|
+
private _systemStream: IOConnectCore.Interop.Stream | undefined;
|
|
27
|
+
private _workspacesStream: IOConnectCore.Interop.Stream | undefined;
|
|
28
|
+
private _platformClientWindowId!: string;
|
|
29
|
+
private _systemSettings!: SessionSystemSettings;
|
|
30
|
+
|
|
31
|
+
constructor(
|
|
32
|
+
private readonly portsBridge: PortsBridge,
|
|
33
|
+
private readonly sessionStorage: SessionStorageController
|
|
34
|
+
) {}
|
|
35
|
+
|
|
36
|
+
private get logger(): IOConnectBrowser.Logger.API | undefined {
|
|
37
|
+
return logger.get("glue.controller");
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
private get workspaces(): IOConnectWorkspaces.API {
|
|
41
|
+
if (!this._clientGlue.workspaces) {
|
|
42
|
+
throw new Error("Cannot access the Workspaces API");
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
return this._clientGlue.workspaces;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
public get isWorkspacesEnabled(): boolean {
|
|
49
|
+
return !!this._clientGlue.workspaces;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
public get platformVersion(): string {
|
|
53
|
+
return version;
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
public get clientGlue(): IOConnectBrowser.API {
|
|
57
|
+
return this._clientGlue;
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
public get contextsTrackingGlue(): IOConnectCore.API | undefined {
|
|
61
|
+
return this._contextsTrackingGlue;
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
public get systemGlue(): IOConnectCore.API {
|
|
65
|
+
return this._systemGlue;
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
public get platformWindowId(): string {
|
|
69
|
+
return this._platformClientWindowId.slice();
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
public async start(config: InternalPlatformConfig): Promise<void> {
|
|
73
|
+
this._config = config;
|
|
74
|
+
|
|
75
|
+
const systemSettings = this.sessionStorage.getSystemSettings();
|
|
76
|
+
|
|
77
|
+
if (!systemSettings) {
|
|
78
|
+
throw new Error("Cannot initiate the glue controller, because the system settings are not defined");
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
this._systemSettings = systemSettings;
|
|
82
|
+
|
|
83
|
+
this._systemGlue = await this.initSystemGlue(config.browser);
|
|
84
|
+
|
|
85
|
+
logger.setLogger(this._systemGlue.logger);
|
|
86
|
+
|
|
87
|
+
this._contextsTrackingGlue = await this.setUpCtxTracking(config);
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
public async initClientGlue(config?: IOConnectBrowser.Config, factory?: IOConnectBrowserFactoryFunction, isWorkspaceFrame?: boolean, platformApi?: IOConnectBrowserPlatform.API): Promise<IOConnectBrowser.API> {
|
|
91
|
+
const port = await this.portsBridge.createInternalClient();
|
|
92
|
+
|
|
93
|
+
this.registerClientWindow(isWorkspaceFrame);
|
|
94
|
+
|
|
95
|
+
const webConfig = {
|
|
96
|
+
application: "Platform",
|
|
97
|
+
gateway: { webPlatform: { port, windowId: this.platformWindowId } }
|
|
98
|
+
} as IOConnectBrowser.Config;
|
|
99
|
+
|
|
100
|
+
const c = Object.assign({}, config, webConfig);
|
|
101
|
+
|
|
102
|
+
this._clientGlue = factory ? await factory(c) : await IOBrowser(c) as any;
|
|
103
|
+
|
|
104
|
+
this._clientGlue.webPlatform = platformApi;
|
|
105
|
+
|
|
106
|
+
return this._clientGlue;
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
public async createPlatformSystemMethod(handler: (args: IOConnectBrowserPlatform.ControlMessage, caller: IOConnectBrowser.Interop.Instance, success: (args?: IOConnectBrowserPlatform.ControlMessage) => void, error: (error?: string | object) => void) => void): Promise<void> {
|
|
110
|
+
await this.createMethodAsync(GlueWebPlatformControlName, handler);
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
public async createPlatformSystemStream(): Promise<void> {
|
|
114
|
+
this._systemStream = await this.createStream(GlueWebPlatformStreamName);
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
public async createSystemStream(name: string): Promise<IOConnectBrowser.Interop.Stream> {
|
|
118
|
+
return this.createStream(name);
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
public async createWorkspacesStream(): Promise<void> {
|
|
122
|
+
this._workspacesStream = await this.createStream(GlueWebPlatformWorkspacesStreamName);
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
public async createWorkspacesEventsReceiver(callback: (data: any) => void): Promise<void> {
|
|
126
|
+
await this._systemGlue.interop.register(GlueWorkspacesEventsReceiverName, (args) => callback(args));
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
public pushSystemMessage(domain: LibDomains, operation: string, data: any): void {
|
|
130
|
+
if (!this._systemStream) {
|
|
131
|
+
throw new Error(`Cannot push data to domain: ${domain}, because the system stream is not created`);
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
this._systemStream.push({ domain, operation, data });
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
public pushWorkspacesMessage(data: any): void {
|
|
138
|
+
if (!this._workspacesStream) {
|
|
139
|
+
throw new Error("Cannot push data to domain: workspaces, because the workspaces stream is not created");
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
this._workspacesStream.push({ data });
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
public async callFrame<OutBound extends object, InBound>(operationDefinition: BridgeOperation, operationArguments: OutBound, windowId: string): Promise<InBound> {
|
|
146
|
+
const messageData = { operation: operationDefinition.name, operationArguments };
|
|
147
|
+
|
|
148
|
+
const baseErrorMessage = `Internal Platform->Frame Communication Error. Attempted calling workspace frame: ${windowId} for operation ${operationDefinition.name} `;
|
|
149
|
+
|
|
150
|
+
if (operationDefinition.dataDecoder) {
|
|
151
|
+
const decodeResult = operationDefinition.dataDecoder.run(messageData.operationArguments);
|
|
152
|
+
|
|
153
|
+
if (!decodeResult.ok) {
|
|
154
|
+
throw new Error(`${baseErrorMessage} OutBound validation failed: ${JSON.stringify(decodeResult.error)}`);
|
|
155
|
+
}
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
const methodName = GlueWorkspaceFrameClientControlName;
|
|
159
|
+
|
|
160
|
+
const result = await this.transmitMessage<InBound>(methodName, messageData, baseErrorMessage, { windowId }, { methodResponseTimeoutMs: 30000, waitTimeoutMs: 30000 });
|
|
161
|
+
|
|
162
|
+
if (operationDefinition.resultDecoder) {
|
|
163
|
+
const decodeResult = operationDefinition.resultDecoder.run(result);
|
|
164
|
+
|
|
165
|
+
if (!decodeResult.ok) {
|
|
166
|
+
throw new Error(`${baseErrorMessage} Result validation failed: ${JSON.stringify(decodeResult.error)}`);
|
|
167
|
+
}
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
return result;
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
public isValidWindowId(id?: string): boolean {
|
|
174
|
+
return !!(id && this.clientGlue.windows.findById(id));
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
public async sendShutDownSignals(): Promise<void> {
|
|
178
|
+
|
|
179
|
+
const allNonMeWindows = this.clientGlue.windows.list().filter((webWindow) => webWindow.id !== this.platformWindowId);
|
|
180
|
+
|
|
181
|
+
await Promise.all(allNonMeWindows.map((webWindow) => webWindow.close()));
|
|
182
|
+
|
|
183
|
+
const messageData = {
|
|
184
|
+
domain: "system",
|
|
185
|
+
operation: "platformShutdown"
|
|
186
|
+
};
|
|
187
|
+
|
|
188
|
+
const baseErrorMessage = `Internal Platform-> ${messageData.domain} Domain Communication Error. Attempted sending shutdown signal to all clients.`;
|
|
189
|
+
|
|
190
|
+
const targets: IOConnectCore.AGM.InstanceTarget = this.clientGlue.interop.servers()
|
|
191
|
+
.filter((server) => allNonMeWindows.every((webWindow) => webWindow.id !== server.windowId))
|
|
192
|
+
.map((server) => ({ instance: server.instance }));
|
|
193
|
+
|
|
194
|
+
try {
|
|
195
|
+
await this.transmitMessage(GlueClientControlName, messageData, baseErrorMessage, targets, { methodResponseTimeoutMs: 30000, waitTimeoutMs: 30000 });
|
|
196
|
+
} catch (error) {
|
|
197
|
+
console.warn("Failed to send shutdown signal to all clients", error);
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
|
|
203
|
+
public shutdown(): void {
|
|
204
|
+
this.systemGlue.connection.logout();
|
|
205
|
+
this.contextsTrackingGlue?.connection.logout();
|
|
206
|
+
this.clientGlue.connection.logout();
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
public async callWindow<OutBound extends object, InBound>(domain: LibDomains, operationDefinition: BridgeOperation, data: OutBound, target: IOConnectCore.Interop.Instance): Promise<InBound> {
|
|
210
|
+
|
|
211
|
+
const operation = operationDefinition.name;
|
|
212
|
+
|
|
213
|
+
const messageData = { domain, operation, data };
|
|
214
|
+
|
|
215
|
+
const baseErrorMessage = `Internal Platform-> ${domain} Domain Communication Error. Attempted calling client window: ${JSON.stringify(target)} for operation ${operation}. `;
|
|
216
|
+
|
|
217
|
+
if (operationDefinition.dataDecoder) {
|
|
218
|
+
const decodeResult = operationDefinition.dataDecoder.run(messageData.data);
|
|
219
|
+
|
|
220
|
+
if (!decodeResult.ok) {
|
|
221
|
+
throw new Error(`${baseErrorMessage} OutBound validation failed: ${JSON.stringify(decodeResult.error)}`);
|
|
222
|
+
}
|
|
223
|
+
}
|
|
224
|
+
|
|
225
|
+
const result = await this.transmitMessage<InBound>(GlueClientControlName, messageData, baseErrorMessage, target, { methodResponseTimeoutMs: 30000, waitTimeoutMs: 30000 });
|
|
226
|
+
|
|
227
|
+
if (operationDefinition.resultDecoder) {
|
|
228
|
+
const decodeResult = operationDefinition.resultDecoder.run(result);
|
|
229
|
+
|
|
230
|
+
if (!decodeResult.ok) {
|
|
231
|
+
throw new Error(`${baseErrorMessage} Result validation failed when calling window: ${JSON.stringify(target)} for operation ${operation}: ${JSON.stringify(decodeResult.error)}`);
|
|
232
|
+
}
|
|
233
|
+
}
|
|
234
|
+
|
|
235
|
+
return result;
|
|
236
|
+
}
|
|
237
|
+
|
|
238
|
+
public setStartContext(windowId: string, context: any, type: "workspace" | "instance" | "window"): Promise<void> {
|
|
239
|
+
return PromisePlus((resolve, reject) => {
|
|
240
|
+
let unsub: () => void;
|
|
241
|
+
|
|
242
|
+
const ready = waitFor(2, () => {
|
|
243
|
+
resolve();
|
|
244
|
+
unsub();
|
|
245
|
+
});
|
|
246
|
+
const key = `___${type}___${windowId}`;
|
|
247
|
+
|
|
248
|
+
const waitContextDestroy = this._clientGlue.contexts.all().some((ctx) => ctx === key) ?
|
|
249
|
+
this.waitContextDestroy(key) :
|
|
250
|
+
Promise.resolve();
|
|
251
|
+
|
|
252
|
+
waitContextDestroy
|
|
253
|
+
.then(() => this._clientGlue.contexts.subscribe(key, ready))
|
|
254
|
+
.then((un) => {
|
|
255
|
+
unsub = un;
|
|
256
|
+
return this._systemGlue.contexts.set(key, context);
|
|
257
|
+
})
|
|
258
|
+
.then(ready)
|
|
259
|
+
.catch(reject);
|
|
260
|
+
}, 10000, `Timed out waiting to set the ${type} context for: ${windowId}`);
|
|
261
|
+
}
|
|
262
|
+
|
|
263
|
+
public waitContextDestroy(contextName: string): Promise<void> {
|
|
264
|
+
return new Promise((resolve, reject) => {
|
|
265
|
+
|
|
266
|
+
let contextChecks = 0;
|
|
267
|
+
|
|
268
|
+
const interval = setInterval(() => {
|
|
269
|
+
const contextExists = this._clientGlue.contexts.all().some((ctx) => ctx === contextName);
|
|
270
|
+
++contextChecks;
|
|
271
|
+
|
|
272
|
+
if (!contextExists) {
|
|
273
|
+
clearInterval(interval);
|
|
274
|
+
resolve();
|
|
275
|
+
return;
|
|
276
|
+
}
|
|
277
|
+
|
|
278
|
+
if (contextChecks === 50) {
|
|
279
|
+
clearInterval(interval);
|
|
280
|
+
reject(`Timed out waiting for context: ${contextName} to disappear`);
|
|
281
|
+
}
|
|
282
|
+
|
|
283
|
+
}, 100);
|
|
284
|
+
|
|
285
|
+
});
|
|
286
|
+
}
|
|
287
|
+
|
|
288
|
+
public async clearContext(windowId: string, type: "workspace" | "instance" | "window"): Promise<void> {
|
|
289
|
+
const key = `___${type}___${windowId}`;
|
|
290
|
+
|
|
291
|
+
const keyExist = this._systemGlue.contexts.all().some((context) => context === key);
|
|
292
|
+
|
|
293
|
+
if (keyExist) {
|
|
294
|
+
await this._systemGlue.contexts.destroy(key);
|
|
295
|
+
}
|
|
296
|
+
|
|
297
|
+
}
|
|
298
|
+
|
|
299
|
+
public async preserveAllWorkspaceWindowsContext(workspaceId: string): Promise<void> {
|
|
300
|
+
const allWorkspaceClients = this.sessionStorage.pickWorkspaceClients((client) => client.workspaceId === workspaceId);
|
|
301
|
+
|
|
302
|
+
for (const workspaceClient of allWorkspaceClients) {
|
|
303
|
+
const clientCtx = await this._systemGlue.contexts.get(`___window___${workspaceClient.windowId}`);
|
|
304
|
+
|
|
305
|
+
if (!clientCtx || (typeof clientCtx === "object" && !Object.keys(clientCtx).length)) {
|
|
306
|
+
continue;
|
|
307
|
+
}
|
|
308
|
+
|
|
309
|
+
await this._systemGlue.contexts.set(`___window-hibernation___${workspaceClient.windowId}`, clientCtx);
|
|
310
|
+
}
|
|
311
|
+
}
|
|
312
|
+
|
|
313
|
+
public async pullHibernatedContext(windowId: string): Promise<any> {
|
|
314
|
+
const key = `___window-hibernation___${windowId}`;
|
|
315
|
+
|
|
316
|
+
const keyExist = this._systemGlue.contexts.all().some((context) => context === key);
|
|
317
|
+
|
|
318
|
+
if (!keyExist) {
|
|
319
|
+
return;
|
|
320
|
+
}
|
|
321
|
+
const ctx = await this._systemGlue.contexts.get(key);
|
|
322
|
+
|
|
323
|
+
await this._systemGlue.contexts.destroy(key);
|
|
324
|
+
|
|
325
|
+
return ctx;
|
|
326
|
+
}
|
|
327
|
+
|
|
328
|
+
public getServers(): IOConnectBrowser.Interop.Instance[] {
|
|
329
|
+
return this._clientGlue.interop.servers();
|
|
330
|
+
}
|
|
331
|
+
|
|
332
|
+
public subscribeForServerAdded(callback: (server: IOConnectBrowser.Interop.Instance) => void): UnsubscribeFunction {
|
|
333
|
+
return this._clientGlue.interop.serverAdded(callback);
|
|
334
|
+
}
|
|
335
|
+
|
|
336
|
+
public subscribeForMethodAdded(callback: (method: IOConnectBrowser.Interop.Method) => void): UnsubscribeFunction {
|
|
337
|
+
return this._clientGlue.interop.methodAdded(callback);
|
|
338
|
+
}
|
|
339
|
+
|
|
340
|
+
public invokeMethod<T>(method: string | IOConnectBrowser.Interop.MethodDefinition, argumentObj?: object, target?: IOConnectBrowser.Interop.InstanceTarget, options?: IOConnectBrowser.Interop.InvokeOptions, success?: IOConnectBrowser.Interop.InvokeSuccessHandler<T>, error?: IOConnectBrowser.Interop.InvokeErrorHandler): Promise<IOConnectBrowser.Interop.InvocationResult<T>> {
|
|
341
|
+
return this._clientGlue.interop.invoke(method, argumentObj, target, options, success, error);
|
|
342
|
+
}
|
|
343
|
+
|
|
344
|
+
public setContext(name: string, data: any): Promise<void> {
|
|
345
|
+
return this._systemGlue.contexts.set(name, data);
|
|
346
|
+
}
|
|
347
|
+
|
|
348
|
+
public switchTransport(config: IOConnectCore.Connection.TransportSwitchSettings, target: "client" | "system" | "contextsTrack"): Promise<{ success: boolean }> {
|
|
349
|
+
|
|
350
|
+
if (target === "contextsTrack") {
|
|
351
|
+
return this._contextsTrackingGlue ?
|
|
352
|
+
this._contextsTrackingGlue.connection.switchTransport(config) :
|
|
353
|
+
Promise.resolve({ success: true });
|
|
354
|
+
}
|
|
355
|
+
|
|
356
|
+
const glueToSwitch = target === "system" ? this._systemGlue : this._clientGlue;
|
|
357
|
+
|
|
358
|
+
return glueToSwitch.connection.switchTransport(config);
|
|
359
|
+
}
|
|
360
|
+
|
|
361
|
+
public onDisconnected(callback: () => void): UnsubscribeFunction {
|
|
362
|
+
return this._systemGlue.connection.disconnected(callback);
|
|
363
|
+
}
|
|
364
|
+
|
|
365
|
+
public getSystemGlueTransportName(): string {
|
|
366
|
+
return (this._systemGlue as any).connection.transport.name();
|
|
367
|
+
}
|
|
368
|
+
|
|
369
|
+
public async importLayout(layout: IOConnectBrowser.Layouts.Layout): Promise<void> {
|
|
370
|
+
await this._clientGlue.layouts.import([layout], "merge");
|
|
371
|
+
}
|
|
372
|
+
|
|
373
|
+
public async getLayout(name: string): Promise<IOConnectBrowser.Layouts.Layout | undefined> {
|
|
374
|
+
const foundLayout = await this._clientGlue.layouts.get(name, "Global");
|
|
375
|
+
|
|
376
|
+
return foundLayout;
|
|
377
|
+
}
|
|
378
|
+
|
|
379
|
+
public async openWindow(config: { name: string, url: string, layoutComponentId: string, bounds?: IOConnectBrowser.Windows.Bounds, context?: any }): Promise<void> {
|
|
380
|
+
|
|
381
|
+
const existingWindow = this._clientGlue.windows.list().find((webWindow) => webWindow.name === config.name);
|
|
382
|
+
|
|
383
|
+
if (existingWindow) {
|
|
384
|
+
config.name = `${config.name}-${nanoid(7)}`;
|
|
385
|
+
}
|
|
386
|
+
|
|
387
|
+
const openConfig = {
|
|
388
|
+
context: config.context,
|
|
389
|
+
top: config.bounds?.top,
|
|
390
|
+
left: config.bounds?.left,
|
|
391
|
+
width: config.bounds?.width,
|
|
392
|
+
height: config.bounds?.height,
|
|
393
|
+
layoutComponentId: config.layoutComponentId
|
|
394
|
+
} as IOConnectBrowser.Windows.Settings;
|
|
395
|
+
|
|
396
|
+
await this._clientGlue.windows.open(config.name, config.url, openConfig);
|
|
397
|
+
}
|
|
398
|
+
|
|
399
|
+
public async startApp(config: { name: string, layoutComponentId: string, bounds?: IOConnectBrowser.Windows.Bounds, context?: any }): Promise<void> {
|
|
400
|
+
const startOptions = {
|
|
401
|
+
waitForAGMReady: false,
|
|
402
|
+
top: config.bounds?.top,
|
|
403
|
+
left: config.bounds?.left,
|
|
404
|
+
width: config.bounds?.width,
|
|
405
|
+
height: config.bounds?.height,
|
|
406
|
+
layoutComponentId: config.layoutComponentId
|
|
407
|
+
} as IOConnectBrowser.AppManager.ApplicationStartOptions;
|
|
408
|
+
|
|
409
|
+
await this._clientGlue.appManager.application(config.name).start(config.context, startOptions);
|
|
410
|
+
}
|
|
411
|
+
|
|
412
|
+
public async getOrCreateWorkspaceFrame({ bounds, layoutComponentId, frameId }: GetOrCreateFrameConfig): Promise<IOConnectWorkspaces.Frame> {
|
|
413
|
+
const targetFrame = frameId ?
|
|
414
|
+
await this.workspaces.getFrame((frame) => frame.id === frameId) :
|
|
415
|
+
await this.workspaces.createEmptyFrame({
|
|
416
|
+
frameConfig: {
|
|
417
|
+
bounds: bounds ? bounds : undefined
|
|
418
|
+
},
|
|
419
|
+
layoutComponentId: layoutComponentId ? layoutComponentId : undefined
|
|
420
|
+
});
|
|
421
|
+
|
|
422
|
+
return targetFrame;
|
|
423
|
+
}
|
|
424
|
+
|
|
425
|
+
public getAllWindowNames(): string[] {
|
|
426
|
+
return this._clientGlue.windows.list().map((webWindow) => webWindow.name);
|
|
427
|
+
}
|
|
428
|
+
|
|
429
|
+
public getAllOpenedIds(): string[] {
|
|
430
|
+
return this._clientGlue.windows.list().map((webWindow) => webWindow.id);
|
|
431
|
+
}
|
|
432
|
+
|
|
433
|
+
public getAllOtherNonPlatformWindows(excludeId?: string): IOConnectBrowser.Windows.WebWindow[] {
|
|
434
|
+
return this._clientGlue.windows.list().filter((webWindow) => webWindow.name !== "Platform" && webWindow.id !== excludeId);
|
|
435
|
+
}
|
|
436
|
+
|
|
437
|
+
public async getAllOpenedFrameIds(): Promise<string[]> {
|
|
438
|
+
const allFrames = await this.workspaces.getAllFrames();
|
|
439
|
+
|
|
440
|
+
return allFrames.map((frame) => frame.id);
|
|
441
|
+
}
|
|
442
|
+
|
|
443
|
+
public getAllApplicationNames(): string[] {
|
|
444
|
+
return this._clientGlue.appManager.applications().map((app) => app.name);
|
|
445
|
+
}
|
|
446
|
+
|
|
447
|
+
public getAllApplications(): IOConnectBrowser.AppManager.Application[] {
|
|
448
|
+
return this._clientGlue.appManager.applications();
|
|
449
|
+
}
|
|
450
|
+
|
|
451
|
+
public getAllLayoutsSummaries(): Promise<IOConnectBrowser.Layouts.LayoutSummary[]> {
|
|
452
|
+
return this._clientGlue.layouts.getAll("Global");
|
|
453
|
+
}
|
|
454
|
+
|
|
455
|
+
public getAllWorkspacesSummaries(): Promise<IOConnectBrowser.Layouts.LayoutSummary[]> {
|
|
456
|
+
return this._clientGlue.layouts.getAll("Workspace");
|
|
457
|
+
}
|
|
458
|
+
|
|
459
|
+
public async getWorkspaceWindowById(targetId: string): Promise<IOConnectWorkspaces.WorkspaceWindow | undefined> {
|
|
460
|
+
return this._clientGlue.workspaces?.getWindow((workspaceWindow) => workspaceWindow.id === targetId);
|
|
461
|
+
}
|
|
462
|
+
|
|
463
|
+
public getWindowById(targetId: string): IOConnectBrowser.Windows.WebWindow | undefined {
|
|
464
|
+
return this._clientGlue.windows.list().find((webWindow) => webWindow.id === targetId);
|
|
465
|
+
}
|
|
466
|
+
|
|
467
|
+
public async getAllWorkspacesFrames(): Promise<IOConnectWorkspaces.Frame[]> {
|
|
468
|
+
return await this.workspaces.getAllFrames();
|
|
469
|
+
}
|
|
470
|
+
|
|
471
|
+
public async getWorkspacesByFrameId(frameId: string): Promise<IOConnectWorkspaces.Workspace[]> {
|
|
472
|
+
return await this.workspaces.getAllWorkspaces((workspace) => workspace.frameId === frameId);
|
|
473
|
+
}
|
|
474
|
+
|
|
475
|
+
public registerProvider(config: IOConnectSearch.ProviderRegistrationConfig): Promise<IOConnectSearch.Provider> {
|
|
476
|
+
if (!this._clientGlue.search) {
|
|
477
|
+
throw new Error("Cannot start the search provider for Glue42 Core Plus, because the Search API is missing");
|
|
478
|
+
}
|
|
479
|
+
|
|
480
|
+
return this._clientGlue.search.registerProvider(config);
|
|
481
|
+
}
|
|
482
|
+
|
|
483
|
+
public async processServerApplicationsData(applications: RefreshResponseData<ApplicationConfig>): Promise<void> {
|
|
484
|
+
|
|
485
|
+
if (!applications || !applications.data) {
|
|
486
|
+
return;
|
|
487
|
+
}
|
|
488
|
+
|
|
489
|
+
const definitions = applications.data as IOConnectBrowser.AppManager.Definition[];
|
|
490
|
+
|
|
491
|
+
const importResult = await this._clientGlue.appManager.inMemory.import(definitions, "merge");
|
|
492
|
+
|
|
493
|
+
if (importResult.errors && importResult.errors.length) {
|
|
494
|
+
importResult.errors.forEach((error) => {
|
|
495
|
+
this.logger?.warn(`App: ${error.app} was not imported, because of error: ${error.error}`);
|
|
496
|
+
});
|
|
497
|
+
}
|
|
498
|
+
}
|
|
499
|
+
|
|
500
|
+
private async initSystemGlue(config?: IOConnectBrowser.Config): Promise<IOConnectCore.API> {
|
|
501
|
+
|
|
502
|
+
const port = await this.portsBridge.createInternalClient();
|
|
503
|
+
|
|
504
|
+
const logLevel = config?.systemLogger?.level ?? "warn";
|
|
505
|
+
|
|
506
|
+
return await IOConnect({
|
|
507
|
+
application: "Platform-System",
|
|
508
|
+
gateway: { webPlatform: { port } },
|
|
509
|
+
logger: logLevel,
|
|
510
|
+
identity: {
|
|
511
|
+
instance: this._systemSettings.systemInstanceId
|
|
512
|
+
}
|
|
513
|
+
});
|
|
514
|
+
}
|
|
515
|
+
|
|
516
|
+
private async setUpCtxTracking(config: InternalPlatformConfig): Promise<IOConnectCore.API | undefined> {
|
|
517
|
+
if (this._config.connection.preferred) {
|
|
518
|
+
return await this.initContextsTrackingGlue({
|
|
519
|
+
reAnnounceKnownContexts: true,
|
|
520
|
+
trackAllContexts: true
|
|
521
|
+
}, config);
|
|
522
|
+
}
|
|
523
|
+
}
|
|
524
|
+
|
|
525
|
+
private async initContextsTrackingGlue(contextsSettings: IOConnectCore.ContextsConfig, config: InternalPlatformConfig): Promise<IOConnectCore.API> {
|
|
526
|
+
|
|
527
|
+
const port = await this.portsBridge.createInternalClient();
|
|
528
|
+
|
|
529
|
+
return await IOConnect({
|
|
530
|
+
application: "Platform-Contexts-Track",
|
|
531
|
+
gateway: { webPlatform: { port } },
|
|
532
|
+
logger: config?.browser?.systemLogger?.level ?? "warn",
|
|
533
|
+
contexts: contextsSettings,
|
|
534
|
+
identity: {
|
|
535
|
+
instance: this._systemSettings.ctxTrackInstanceId
|
|
536
|
+
}
|
|
537
|
+
});
|
|
538
|
+
}
|
|
539
|
+
|
|
540
|
+
private registerClientWindow(isWorkspaceFrame?: boolean): void {
|
|
541
|
+
if (isWorkspaceFrame) {
|
|
542
|
+
const platformFrame = this.sessionStorage.getPlatformFrame();
|
|
543
|
+
|
|
544
|
+
this._platformClientWindowId = platformFrame ? platformFrame.windowId :
|
|
545
|
+
window.name ? window.name : `g42-${generate()}`;
|
|
546
|
+
|
|
547
|
+
if (!platformFrame) {
|
|
548
|
+
const platformFrameData: FrameSessionData = { windowId: this.platformWindowId, active: true, isPlatform: true };
|
|
549
|
+
this.sessionStorage.saveFrameData(platformFrameData);
|
|
550
|
+
}
|
|
551
|
+
|
|
552
|
+
window.name = this.platformWindowId;
|
|
553
|
+
|
|
554
|
+
return;
|
|
555
|
+
}
|
|
556
|
+
|
|
557
|
+
const platformWindowData = this.sessionStorage.getWindowDataByName("Platform");
|
|
558
|
+
|
|
559
|
+
this._platformClientWindowId = platformWindowData ? platformWindowData.windowId :
|
|
560
|
+
window.name ? window.name : `g42-${generate()}`;
|
|
561
|
+
|
|
562
|
+
if (!platformWindowData) {
|
|
563
|
+
this.sessionStorage.saveWindowData({ name: "Platform", windowId: this.platformWindowId });
|
|
564
|
+
}
|
|
565
|
+
|
|
566
|
+
window.name = this.platformWindowId;
|
|
567
|
+
}
|
|
568
|
+
|
|
569
|
+
private async createMethodAsync(name: string, handler: (args: any, caller: IOConnectBrowser.Interop.Instance, success: (args?: any) => void, error: (error?: string | object) => void) => void): Promise<void> {
|
|
570
|
+
await this._systemGlue.interop.registerAsync(name, handler);
|
|
571
|
+
}
|
|
572
|
+
|
|
573
|
+
private async createStream(name: string): Promise<IOConnectBrowser.Interop.Stream> {
|
|
574
|
+
return this._systemGlue.interop.createStream(name);
|
|
575
|
+
}
|
|
576
|
+
|
|
577
|
+
private async transmitMessage<T>(methodName: string, messageData: any, baseErrorMessage: string, target?: IOConnectCore.AGM.InstanceTarget, options?: IOConnectCore.Interop.InvokeOptions): Promise<T> {
|
|
578
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
579
|
+
let invocationResult: IOConnectCore.Interop.InvocationResult<T>;
|
|
580
|
+
|
|
581
|
+
try {
|
|
582
|
+
invocationResult = await this._systemGlue.interop.invoke<T>(methodName, messageData, target, options);
|
|
583
|
+
|
|
584
|
+
if (!invocationResult) {
|
|
585
|
+
throw new Error(`${baseErrorMessage} Received unsupported result from the client - empty result`);
|
|
586
|
+
}
|
|
587
|
+
|
|
588
|
+
if (!Array.isArray(invocationResult.all_return_values) || invocationResult.all_return_values.length === 0) {
|
|
589
|
+
throw new Error(`${baseErrorMessage} Received unsupported result from the client - empty values collection`);
|
|
590
|
+
}
|
|
591
|
+
} catch (error: any) {
|
|
592
|
+
if (error && error.all_errors && error.all_errors.length) {
|
|
593
|
+
// IMPORTANT: Do NOT change the `Inner message:` string, because it is used by other programs to extract the inner message of a communication error
|
|
594
|
+
const invocationErrorMessage = error.all_errors[0].message;
|
|
595
|
+
throw new Error(`${baseErrorMessage} -> Inner message: ${invocationErrorMessage}`);
|
|
596
|
+
|
|
597
|
+
}
|
|
598
|
+
// IMPORTANT: Do NOT change the `Inner message:` string, because it is used by other programs to extract the inner message of a communication error
|
|
599
|
+
throw new Error(`${baseErrorMessage} -> Inner message: ${error.message}`);
|
|
600
|
+
}
|
|
601
|
+
|
|
602
|
+
return invocationResult.all_return_values[0].returned;
|
|
603
|
+
}
|
|
604
|
+
}
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
import { IOConnectBrowserPlatform } from "../../browser.platform";
|
|
2
|
+
import { InterceptorEntry, InterceptorInquiry, InterceptorInquiryResult } from "../common/types";
|
|
3
|
+
import { interceptorRegistrationRequestDecoder, nonEmptyStringDecoder } from "../shared/decoders";
|
|
4
|
+
|
|
5
|
+
export class InterceptionController {
|
|
6
|
+
private interceptions: Array<InterceptorEntry> = [];
|
|
7
|
+
|
|
8
|
+
public shutdown(): void {
|
|
9
|
+
this.interceptions = [];
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
public async registerInterceptor(request: IOConnectBrowserPlatform.Plugins.InterceptorRegistrationRequest, registrantName: string): Promise<void> {
|
|
13
|
+
|
|
14
|
+
interceptorRegistrationRequestDecoder.runWithException(request);
|
|
15
|
+
nonEmptyStringDecoder.runWithException(registrantName);
|
|
16
|
+
|
|
17
|
+
const collisions = request.interceptions.reduce<Array<{ domain: string, operation: string }>>((collisions, interception) => {
|
|
18
|
+
const foundCollision = this.interceptions.some((registeredInterception) => registeredInterception.domain === interception.domain && registeredInterception.operation === interception.operation);
|
|
19
|
+
|
|
20
|
+
if (foundCollision) {
|
|
21
|
+
collisions.push({ domain: interception.domain, operation: interception.operation });
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
return collisions;
|
|
25
|
+
}, []);
|
|
26
|
+
|
|
27
|
+
if (collisions.length) {
|
|
28
|
+
const collisionsAsString = collisions.map((collision) => `${collision.domain} - ${collision.operation}`).join(", ");
|
|
29
|
+
|
|
30
|
+
throw new Error(`Interception registration is rejected, because the following collisions where found: ${collisionsAsString}`);
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
request.interceptions.forEach((interception) => {
|
|
34
|
+
this.interceptions.push({
|
|
35
|
+
domain: interception.domain,
|
|
36
|
+
operation: interception.operation,
|
|
37
|
+
callInterceptor: request.callInterceptor,
|
|
38
|
+
registrantName
|
|
39
|
+
});
|
|
40
|
+
});
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
public getOperationInterceptor(inquiry: InterceptorInquiry): InterceptorInquiryResult | undefined {
|
|
44
|
+
const foundEntry = this.interceptions.find((registeredInterception) => registeredInterception.domain === inquiry.domain && registeredInterception.operation === inquiry.operation);
|
|
45
|
+
|
|
46
|
+
if (foundEntry) {
|
|
47
|
+
return {
|
|
48
|
+
name: foundEntry.registrantName,
|
|
49
|
+
intercept: foundEntry.callInterceptor
|
|
50
|
+
};
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
}
|