-
Notifications
You must be signed in to change notification settings - Fork 135
/
Copy pathOpenHandler.ts
322 lines (290 loc) · 9.59 KB
/
OpenHandler.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
import { MessageHandler } from '../BasicFDC3Server';
import { AppRegistration, InstanceID, ServerContext, State } from '../ServerContext';
import { Directory, DirectoryApp } from '../directory/DirectoryInterface';
import { ContextElement } from '@kite9/fdc3-context';
import { OpenError, ResolveError, AppIdentifier, AppMetadata } from '@kite9/fdc3-standard';
import { BrowserTypes } from '@kite9/fdc3-schema';
import { errorResponse, successResponse } from './support';
type BroadcastEvent = BrowserTypes.BroadcastEvent;
type AddContextListenerRequest = BrowserTypes.AddContextListenerRequest;
type FindInstancesRequest = BrowserTypes.FindInstancesRequest;
type GetAppMetadataRequest = BrowserTypes.GetAppMetadataRequest;
type OpenRequest = BrowserTypes.OpenRequest;
type WebConnectionProtocol4ValidateAppIdentity = BrowserTypes.WebConnectionProtocol4ValidateAppIdentity;
type WebConnectionProtocol5ValidateAppIdentityFailedResponse =
BrowserTypes.WebConnectionProtocol5ValidateAppIdentityFailedResponse;
type WebConnectionProtocol5ValidateAppIdentitySuccessResponse =
BrowserTypes.WebConnectionProtocol5ValidateAppIdentitySuccessResponse;
enum AppState {
Opening,
DeliveringContext,
Done,
}
class PendingApp {
private readonly sc: ServerContext<AppRegistration>;
private readonly msg: OpenRequest;
readonly context: ContextElement | undefined;
readonly source: AppMetadata;
state: AppState = AppState.Opening;
private openedApp: AppIdentifier | undefined = undefined;
constructor(
sc: ServerContext<AppRegistration>,
msg: OpenRequest,
context: ContextElement | undefined,
source: AppIdentifier,
timeoutMs: number
) {
this.context = context;
this.source = source;
this.sc = sc;
this.msg = msg;
setTimeout(() => {
if (this.state != AppState.Done) {
this.onError();
}
}, timeoutMs);
}
private onSuccess() {
this.sc.setAppState(this.openedApp?.instanceId!!, State.Connected);
successResponse(
this.sc,
this.msg,
this.source,
{
appIdentifier: {
appId: this.openedApp!!.appId,
instanceId: this.openedApp!!.instanceId,
},
},
'openResponse'
);
}
private onError() {
errorResponse(this.sc, this.msg, this.source, OpenError.AppTimeout, 'openResponse');
}
setOpened(openedApp: AppIdentifier) {
this.openedApp = openedApp;
if (this.context) {
this.state = AppState.DeliveringContext;
} else {
this.setDone();
}
}
setDone() {
this.state = AppState.Done;
this.onSuccess();
}
}
export class OpenHandler implements MessageHandler {
private readonly directory: Directory;
readonly pending: Map<InstanceID, PendingApp> = new Map();
readonly timeoutMs: number;
constructor(d: Directory, timeoutMs: number) {
this.directory = d;
this.timeoutMs = timeoutMs;
}
shutdown(): void {}
async accept(msg: any, sc: ServerContext<AppRegistration>, uuid: InstanceID): Promise<void> {
switch (msg.type as string) {
case 'addContextListenerRequest':
return this.handleAddContextListener(msg as AddContextListenerRequest, sc, uuid);
case 'WCP4ValidateAppIdentity':
return this.handleValidate(msg as WebConnectionProtocol4ValidateAppIdentity, sc, uuid);
}
const from = sc.getInstanceDetails(uuid);
try {
if (from) {
switch (msg.type as string) {
case 'openRequest':
return this.open(msg as OpenRequest, sc, from);
case 'findInstancesRequest':
return this.findInstances(msg as FindInstancesRequest, sc, from);
case 'getAppMetadataRequest':
return this.getAppMetadata(msg as GetAppMetadataRequest, sc, from);
}
}
} catch (e: any) {
const responseType = msg.type.replace(new RegExp('Request$'), 'Response');
errorResponse(sc, msg, from!!, e.message ?? e, responseType);
}
}
/**
* This deals with sending pending context to listeners of newly-opened apps.
*/
handleAddContextListener(
arg0: AddContextListenerRequest,
sc: ServerContext<AppRegistration>,
from: InstanceID
): void {
const pendingOpen = this.pending.get(from);
if (pendingOpen) {
const channelId = arg0.payload.channelId!!;
const contextType = arg0.payload.contextType;
if (pendingOpen.context && pendingOpen.state == AppState.DeliveringContext) {
if (contextType == pendingOpen.context.type || contextType == undefined) {
// ok, we can deliver to this listener
const message: BroadcastEvent = {
meta: {
eventUuid: sc.createUUID(),
timestamp: new Date(),
},
type: 'broadcastEvent',
payload: {
channelId,
context: pendingOpen.context,
originatingApp: {
appId: pendingOpen.source.appId,
instanceId: pendingOpen.source.instanceId,
},
},
};
pendingOpen.setDone();
this.pending.delete(from);
sc.post(message, arg0.meta.source?.instanceId!!);
}
}
}
}
filterPublicDetails(appD: DirectoryApp, appID: AppIdentifier): AppMetadata {
return {
appId: appD.appId,
name: appD.name,
version: appD.version,
title: appD.title,
tooltip: appD.tooltip,
description: appD.description,
icons: appD.icons,
screenshots: appD.screenshots,
instanceId: appID.instanceId,
};
}
getAppMetadata(arg0: GetAppMetadataRequest, sc: ServerContext<AppRegistration>, from: AppIdentifier): void {
const appID = arg0.payload.app;
const details = this.directory.retrieveAppsById(appID.appId);
if (details.length > 0) {
successResponse(
sc,
arg0,
from,
{
appMetadata: this.filterPublicDetails(details[0], appID),
},
'getAppMetadataResponse'
);
} else {
errorResponse(sc, arg0, from, ResolveError.TargetAppUnavailable, 'getAppMetadataResponse');
}
}
async findInstances(
arg0: FindInstancesRequest,
sc: ServerContext<AppRegistration>,
from: AppIdentifier
): Promise<void> {
const appId = arg0.payload.app.appId;
const openApps = await sc.getConnectedApps();
const matching = openApps
.filter(a => a.appId == appId)
.map(a => {
return {
appId: a.appId,
instanceId: a.instanceId,
};
});
successResponse(
sc,
arg0,
from,
{
appIdentifiers: matching,
},
'findInstancesResponse'
);
}
async open(arg0: OpenRequest, sc: ServerContext<AppRegistration>, from: AppIdentifier): Promise<void> {
const source = arg0.payload.app;
const context = arg0.payload.context;
try {
const uuid = await sc.open(source.appId);
this.pending.set(uuid, new PendingApp(sc, arg0, context, from, this.timeoutMs));
} catch (e: any) {
errorResponse(sc, arg0, from, e.message, 'openResponse');
}
}
async handleValidate(
arg0: WebConnectionProtocol4ValidateAppIdentity,
sc: ServerContext<AppRegistration>,
from: InstanceID
): Promise<void> {
const _this = this;
const responseMeta = {
connectionAttemptUuid: arg0.meta.connectionAttemptUuid,
timestamp: new Date(),
};
function returnError() {
const message: WebConnectionProtocol5ValidateAppIdentityFailedResponse = {
meta: responseMeta,
type: 'WCP5ValidateAppIdentityFailedResponse',
payload: {
message: 'App Instance not found',
},
};
sc.post(message, from);
}
function returnSuccess(appIdentity: AppIdentifier) {
if (!appIdentity.instanceId) {
throw new Error("App does not include an instanceId, can't ");
}
const aopMetadata = _this.filterPublicDetails(
_this.directory.retrieveAppsById(appIdentity.appId)[0],
appIdentity
);
const message: WebConnectionProtocol5ValidateAppIdentitySuccessResponse = {
meta: responseMeta,
type: 'WCP5ValidateAppIdentityResponse',
payload: {
appId: appIdentity.appId,
instanceId: appIdentity.instanceId,
instanceUuid: from,
implementationMetadata: {
provider: sc.provider(),
providerVersion: sc.providerVersion(),
fdc3Version: sc.fdc3Version(),
optionalFeatures: {
DesktopAgentBridging: false,
OriginatingAppMetadata: true,
UserChannelMembershipAPIs: true,
},
appMetadata: aopMetadata,
},
},
};
sc.post(message, appIdentity.instanceId!!);
}
if (arg0.payload.instanceUuid) {
// existing app reconnecting
const appIdentity = sc.getInstanceDetails(arg0.payload.instanceUuid);
if (appIdentity) {
// in this case, the app is reconnecting, so let's just re-assign the
// identity
sc.setInstanceDetails(from, appIdentity);
sc.setAppState(from, State.Connected);
return returnSuccess(appIdentity);
}
}
// we need to assign an identity to this app
const appIdentity = sc.getInstanceDetails(from);
if (appIdentity) {
sc.setAppState(appIdentity.instanceId, State.Connected);
returnSuccess(appIdentity);
// make sure if the opener is listening for this app to open gets informed
const pendingOpen = this.pending.get(from);
if (pendingOpen) {
if (pendingOpen.state == AppState.Opening) {
pendingOpen.setOpened(appIdentity);
}
}
} else {
returnError();
}
}
}