-
Notifications
You must be signed in to change notification settings - Fork 135
/
Copy pathDesktopAgentDetails.ts
92 lines (85 loc) · 3.38 KB
/
DesktopAgentDetails.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
import { DesktopAgentDetails, DESKTOP_AGENT_SESSION_STORAGE_KEY_PREFIX } from '@finos/fdc3-standard';
import { Logger } from '../util/Logger';
import { createUUID } from '../util/Uuid';
/**
* Note that we also key by the window name as well, in case multiple iframes are using the same session storage.
*/
export function sessionKey(): string {
//If the window or frame is not named, create and apply a unique name to it
if (!globalThis.window.name) {
globalThis.window.name = createUUID();
}
const windowName = globalThis.window.name;
const keyName = DESKTOP_AGENT_SESSION_STORAGE_KEY_PREFIX + '-' + windowName;
return keyName;
}
/** Used to persist data on the connection, which can later be used to ensure
* reconnection to the same Desktop Agent and to request the same instanceId.
*/
export function storeDesktopAgentDetails(details: DesktopAgentDetails) {
Logger.debug(`DesktopAgentDetails: Storing Desktop Agent details:`, details);
//check if there are existing details in storage to update
let detailsToStore = retrieveAllDesktopAgentDetails();
if (!detailsToStore) {
detailsToStore = {};
}
detailsToStore[details.identityUrl] = details;
globalThis.sessionStorage.setItem(sessionKey(), JSON.stringify(detailsToStore));
}
/** Retrieves persisted data about previous connections. Used to ensure reconnection
* to the same agent and to request the same instanceId.
*/
export function retrieveAllDesktopAgentDetails(): Record<string, DesktopAgentDetails> | null {
const detailsStr = globalThis.sessionStorage.getItem(sessionKey());
if (detailsStr) {
try {
const theData: Record<string, DesktopAgentDetails> = JSON.parse(detailsStr) as Record<
string,
DesktopAgentDetails
>;
if (typeof theData !== 'object' || Array.isArray(theData)) {
throw new Error('Stored DesktopAgentDetails is not in the expected format!');
}
return theData;
} catch (e) {
Logger.warn(
// eslint-disable-next-line @typescript-eslint/no-explicit-any
`DesktopAgentDetails: FDC3 connection data couldn't be parsed\nstorage key: ${sessionKey()}\nvalue: ${detailsStr}\nmessage: ${(e as any).message ?? e}`
);
return null;
}
} else {
return null;
}
}
/** Retrieves persisted data about previous connections for this specific app
* (identified by the identityUrl). Used to ensure reconnection to the same
* agent and to request the same instanceId.
*/
export function retrieveDesktopAgentDetails(identityUrl: string): DesktopAgentDetails | null {
const allDetails = retrieveAllDesktopAgentDetails();
Logger.debug(`DesktopAgentDetails: retrieveDesktopAgentDetails:`, allDetails);
if (allDetails) {
const theData: DesktopAgentDetails = allDetails[identityUrl];
if (theData) {
//check we got the minimum properties
if (
typeof theData.agentType === 'string' &&
theData.agentType && //TODO: check this is one of the enum values
typeof theData.appId === 'string' &&
theData.appId &&
typeof theData.instanceId === 'string' &&
theData.instanceId
) {
return theData;
} else {
//ignore it and post a warning
Logger.warn(
`DesktopAgentDetails: Stored details do not meet minimum requirements and will be ignored:\n${JSON.stringify(theData, null, 2)}`
);
return null;
}
}
}
return null;
}