@o3r/third-party 15.3.0-prerelease.16 → 15.3.0-prerelease.17
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.
|
@@ -1,109 +1,52 @@
|
|
|
1
|
-
import { BehaviorSubject, distinctUntilChanged,
|
|
2
|
-
import { filter, map, share, timeout } from
|
|
3
|
-
import { v4 } from
|
|
4
|
-
|
|
5
|
-
/**
|
|
6
|
-
* Default options that will represent the interface
|
|
7
|
-
*/
|
|
1
|
+
import { BehaviorSubject, distinctUntilChanged, firstValueFrom, fromEvent } from "rxjs";
|
|
2
|
+
import { filter, map, share, timeout } from "rxjs/operators";
|
|
3
|
+
import { v4 } from "uuid";
|
|
8
4
|
const defaultOptions = {
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
5
|
+
bridgeName: "abTestBridge",
|
|
6
|
+
readyEventName: "ab-test-ready",
|
|
7
|
+
logger: console
|
|
8
|
+
};
|
|
9
|
+
var AbTestBridge = class {
|
|
10
|
+
constructor(isExperimentEqual, options) {
|
|
11
|
+
this.isExperimentEqual = isExperimentEqual;
|
|
12
|
+
this.experimentSubject$ = new BehaviorSubject([]);
|
|
13
|
+
this.experiments$ = this.experimentSubject$.pipe(distinctUntilChanged((experimentsA, experimentsB) => experimentsB.length === experimentsA.length && experimentsA.every((eA) => experimentsB.find((eB) => isExperimentEqual(eA, eB)))));
|
|
14
|
+
this.options = {
|
|
15
|
+
...defaultOptions,
|
|
16
|
+
...options
|
|
17
|
+
};
|
|
18
|
+
if (window[this.options.bridgeName]) this.log(`An instance of ${this.options.bridgeName} already exists. This AbTestBridge instance will be ignored`);
|
|
19
|
+
else window[this.options.bridgeName] = {
|
|
20
|
+
start: this.start.bind(this),
|
|
21
|
+
stop: this.stop.bind(this)
|
|
22
|
+
};
|
|
23
|
+
document.dispatchEvent(new CustomEvent(this.options.readyEventName));
|
|
24
|
+
}
|
|
25
|
+
log(...args) {
|
|
26
|
+
(this.options.logger.debug || this.options.logger.log)("A/B Test", ...args);
|
|
27
|
+
}
|
|
28
|
+
start(experiments) {
|
|
29
|
+
this.log("Start experiment", experiments);
|
|
30
|
+
const currentProfile = this.experimentSubject$.getValue();
|
|
31
|
+
this.experimentSubject$.next([...currentProfile, ...(Array.isArray(experiments) ? experiments : [experiments]).filter((exp) => !currentProfile.some((expB) => this.isExperimentEqual(exp, expB)))]);
|
|
32
|
+
}
|
|
33
|
+
stop(experiments) {
|
|
34
|
+
this.log("Stop experiment", experiments);
|
|
35
|
+
const currentExperiments = this.experimentSubject$.getValue();
|
|
36
|
+
if (experiments) this.experimentSubject$.next(currentExperiments.filter((expB) => !(Array.isArray(experiments) ? experiments : [experiments]).some((expA) => this.isExperimentEqual(expB, expA))));
|
|
37
|
+
else this.experimentSubject$.next([]);
|
|
38
|
+
}
|
|
12
39
|
};
|
|
13
|
-
/**
|
|
14
|
-
* Bridge between the application and a third party A/B testing provider.
|
|
15
|
-
* Exposes a start and stop methods to allow the external script to set the list of experiments to run over the
|
|
16
|
-
* application.
|
|
17
|
-
*
|
|
18
|
-
* Share the resulting list of experiments with the rest of the application via an observable.
|
|
19
|
-
*/
|
|
20
|
-
class AbTestBridge {
|
|
21
|
-
/**
|
|
22
|
-
* AbTestBridge constructor
|
|
23
|
-
* @param isExperimentEqual check two different experiments match to identify an experiment to start or to stop
|
|
24
|
-
* @param options configure the communication with the A/B testing third party provider
|
|
25
|
-
*/
|
|
26
|
-
constructor(isExperimentEqual, options) {
|
|
27
|
-
this.isExperimentEqual = isExperimentEqual;
|
|
28
|
-
/**
|
|
29
|
-
* Behaviour subject to control the experiments via the exposed interface
|
|
30
|
-
*/
|
|
31
|
-
this.experimentSubject$ = new BehaviorSubject([]);
|
|
32
|
-
this.experiments$ = this.experimentSubject$.pipe(distinctUntilChanged((experimentsA, experimentsB) => experimentsB.length === experimentsA.length && experimentsA.every((eA) => experimentsB.find((eB) => isExperimentEqual(eA, eB)))));
|
|
33
|
-
this.options = {
|
|
34
|
-
...defaultOptions,
|
|
35
|
-
...options
|
|
36
|
-
};
|
|
37
|
-
if (window[this.options.bridgeName]) {
|
|
38
|
-
this.log(`An instance of ${this.options.bridgeName} already exists. This AbTestBridge instance will be ignored`);
|
|
39
|
-
}
|
|
40
|
-
else {
|
|
41
|
-
window[this.options.bridgeName] = { start: this.start.bind(this), stop: this.stop.bind(this) };
|
|
42
|
-
}
|
|
43
|
-
document.dispatchEvent(new CustomEvent(this.options.readyEventName));
|
|
44
|
-
}
|
|
45
|
-
/**
|
|
46
|
-
* Use configured logger to log AB testing related information
|
|
47
|
-
* @param args
|
|
48
|
-
*/
|
|
49
|
-
log(...args) {
|
|
50
|
-
(this.options.logger.debug || this.options.logger.log)('A/B Test', ...args);
|
|
51
|
-
}
|
|
52
|
-
/**
|
|
53
|
-
* @inheritDoc
|
|
54
|
-
*/
|
|
55
|
-
start(experiments) {
|
|
56
|
-
this.log('Start experiment', experiments);
|
|
57
|
-
const currentProfile = this.experimentSubject$.getValue();
|
|
58
|
-
this.experimentSubject$.next([
|
|
59
|
-
...currentProfile,
|
|
60
|
-
...(Array.isArray(experiments)
|
|
61
|
-
? experiments
|
|
62
|
-
: [experiments]).filter((exp) => !currentProfile.some((expB) => this.isExperimentEqual(exp, expB)))
|
|
63
|
-
]);
|
|
64
|
-
}
|
|
65
|
-
/**
|
|
66
|
-
* @inheritDoc
|
|
67
|
-
*/
|
|
68
|
-
stop(experiments) {
|
|
69
|
-
this.log('Stop experiment', experiments);
|
|
70
|
-
const currentExperiments = this.experimentSubject$.getValue();
|
|
71
|
-
if (experiments) {
|
|
72
|
-
// Stop the mentioned experiment
|
|
73
|
-
this.experimentSubject$.next(currentExperiments.filter((expB) => !(Array.isArray(experiments) ? experiments : [experiments]).some((expA) => this.isExperimentEqual(expB, expA))));
|
|
74
|
-
}
|
|
75
|
-
else {
|
|
76
|
-
// Stop all the experiment
|
|
77
|
-
this.experimentSubject$.next([]);
|
|
78
|
-
}
|
|
79
|
-
}
|
|
80
|
-
}
|
|
81
|
-
|
|
82
|
-
/**
|
|
83
|
-
* Default options for an IFrameBridge
|
|
84
|
-
*/
|
|
85
40
|
const IFRAME_BRIDGE_DEFAULT_OPTIONS = {
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
41
|
+
handshakeTries: 10,
|
|
42
|
+
handshakeTimeout: 200,
|
|
43
|
+
messageWithResponseTimeout: 1e3
|
|
89
44
|
};
|
|
90
|
-
/**
|
|
91
|
-
* Verifies if a message respects the format expected by an IFrameBridge
|
|
92
|
-
* @param message
|
|
93
|
-
*/
|
|
94
45
|
function isSupportedMessage(message) {
|
|
95
|
-
|
|
96
|
-
&& !!message.action
|
|
97
|
-
&& !!message.version
|
|
98
|
-
&& !!message.channelId;
|
|
46
|
+
return typeof message === "object" && !!message.action && !!message.version && !!message.channelId;
|
|
99
47
|
}
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
* @param scriptUrl script to be executed inside the iframe
|
|
103
|
-
* @param additionalHeader custom html headers stringified
|
|
104
|
-
*/
|
|
105
|
-
function generateIFrameContent(scriptUrl, additionalHeader = '') {
|
|
106
|
-
return `<html>
|
|
48
|
+
function generateIFrameContent(scriptUrl, additionalHeader = "") {
|
|
49
|
+
return `<html>
|
|
107
50
|
<head>
|
|
108
51
|
<script>
|
|
109
52
|
class Bridge {
|
|
@@ -174,80 +117,60 @@ function generateIFrameContent(scriptUrl, additionalHeader = '') {
|
|
|
174
117
|
}
|
|
175
118
|
|
|
176
119
|
const BRIDGE = new Bridge();
|
|
177
|
-
|
|
178
|
-
<script src='${scriptUrl}'
|
|
120
|
+
<\/script>
|
|
121
|
+
<script src='${scriptUrl}'><\/script>
|
|
179
122
|
${additionalHeader}
|
|
180
123
|
</head>
|
|
181
124
|
<body></body>
|
|
182
125
|
</html>`;
|
|
183
126
|
}
|
|
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
|
-
* @param messageId message identifier
|
|
232
|
-
*/
|
|
233
|
-
async sendMessage(message, messageId) {
|
|
234
|
-
await this.handshakePromise;
|
|
235
|
-
this._sendMessage(message, messageId);
|
|
236
|
-
}
|
|
237
|
-
/**
|
|
238
|
-
* Method to send a message to the script run in the iframe and wait for an answer
|
|
239
|
-
* @param message
|
|
240
|
-
* @param timeoutMilliseconds
|
|
241
|
-
*/
|
|
242
|
-
async sendMessageAndWaitForResponse(message, timeoutMilliseconds = this.options.messageWithResponseTimeout) {
|
|
243
|
-
await this.handshakePromise;
|
|
244
|
-
return this._sendMessageAndWaitForResponse(message, timeoutMilliseconds);
|
|
245
|
-
}
|
|
246
|
-
}
|
|
247
|
-
|
|
248
|
-
/**
|
|
249
|
-
* Generated bundle index. Do not edit.
|
|
250
|
-
*/
|
|
251
|
-
|
|
127
|
+
var IframeBridge = class {
|
|
128
|
+
constructor(parent, child, options = {}) {
|
|
129
|
+
this.child = child;
|
|
130
|
+
this.options = {
|
|
131
|
+
...IFRAME_BRIDGE_DEFAULT_OPTIONS,
|
|
132
|
+
...options
|
|
133
|
+
};
|
|
134
|
+
this.channelId = v4();
|
|
135
|
+
this.internalMessages$ = fromEvent(parent, "message").pipe(filter((event) => {
|
|
136
|
+
const messageEvent = event;
|
|
137
|
+
return isSupportedMessage(messageEvent.data) && messageEvent.data.channelId === this.channelId;
|
|
138
|
+
}), map((event) => event.data), share());
|
|
139
|
+
this.messages$ = this.internalMessages$.pipe(filter((message) => !message.id));
|
|
140
|
+
this.handshakePromise = this.handshake();
|
|
141
|
+
}
|
|
142
|
+
async handshake() {
|
|
143
|
+
for (let i = 0; i < this.options.handshakeTries; i++) try {
|
|
144
|
+
await this._sendMessageAndWaitForResponse({
|
|
145
|
+
action: "HANDSHAKE_PARENT",
|
|
146
|
+
version: "1.0"
|
|
147
|
+
}, this.options.handshakeTimeout);
|
|
148
|
+
return;
|
|
149
|
+
} catch {}
|
|
150
|
+
throw new Error("Handshake failed.");
|
|
151
|
+
}
|
|
152
|
+
_sendMessage(message, messageId) {
|
|
153
|
+
if (this.child.contentWindow) this.child.contentWindow.postMessage({
|
|
154
|
+
...message,
|
|
155
|
+
channelId: this.channelId,
|
|
156
|
+
id: messageId
|
|
157
|
+
}, "*");
|
|
158
|
+
}
|
|
159
|
+
_sendMessageAndWaitForResponse(message, timeoutMilliseconds = this.options.messageWithResponseTimeout) {
|
|
160
|
+
const id = v4();
|
|
161
|
+
const promise = firstValueFrom(this.internalMessages$.pipe(filter((response) => response.id === id), timeout(timeoutMilliseconds)));
|
|
162
|
+
this._sendMessage(message, id);
|
|
163
|
+
return promise;
|
|
164
|
+
}
|
|
165
|
+
async sendMessage(message, messageId) {
|
|
166
|
+
await this.handshakePromise;
|
|
167
|
+
this._sendMessage(message, messageId);
|
|
168
|
+
}
|
|
169
|
+
async sendMessageAndWaitForResponse(message, timeoutMilliseconds = this.options.messageWithResponseTimeout) {
|
|
170
|
+
await this.handshakePromise;
|
|
171
|
+
return this._sendMessageAndWaitForResponse(message, timeoutMilliseconds);
|
|
172
|
+
}
|
|
173
|
+
};
|
|
252
174
|
export { AbTestBridge, IFRAME_BRIDGE_DEFAULT_OPTIONS, IframeBridge, generateIFrameContent, isSupportedMessage };
|
|
253
|
-
|
|
175
|
+
|
|
176
|
+
//# sourceMappingURL=o3r-third-party.mjs.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"o3r-third-party.mjs","sources":["../../src/bridge/ab-testing/ab-testing-bridge.ts","../../src/bridge/iframe/helpers.ts","../../src/bridge/iframe/bridge.ts","../../src/o3r-third-party.ts"],"sourcesContent":["import type {\n Logger,\n} from '@o3r/core';\nimport {\n BehaviorSubject,\n distinctUntilChanged,\n Observable,\n} from 'rxjs';\n\n/**\n * Shared interface with the A/B testing provider\n */\nexport interface AbTestBridgeInterface<T> {\n /**\n * Start an AB testing experiment\n */\n start(experiments: T | T[]): void;\n /**\n * Stop an AB testing experiment\n */\n stop(experiments?: T | T[]): void;\n}\n\n/**\n * Configure the A/B testing script interfaces with the application\n */\nexport interface AbTestBridgeConfig {\n /**\n * Reference to communicate with the bridge from the window\n * @default 'abTestBridge'\n */\n bridgeName: string;\n /**\n * Debug logger\n * @default console\n */\n logger: Logger;\n /**\n * Event a third party can subscribe to before starting the communication with the bridge\n * @default 'ab-test-ready'\n */\n readyEventName: string;\n}\n\n/**\n * Default options that will represent the interface\n */\nconst defaultOptions = {\n bridgeName: 'abTestBridge',\n readyEventName: 'ab-test-ready',\n logger: console\n} as const satisfies AbTestBridgeConfig;\n\n/**\n * Bridge between the application and a third party A/B testing provider.\n * Exposes a start and stop methods to allow the external script to set the list of experiments to run over the\n * application.\n *\n * Share the resulting list of experiments with the rest of the application via an observable.\n */\nexport class AbTestBridge<T> implements AbTestBridgeInterface<T> {\n /**\n * Behaviour subject to control the experiments via the exposed interface\n */\n private readonly experimentSubject$: BehaviorSubject<T[]> = new BehaviorSubject<T[]>([]);\n /**\n * Options to configure the communication between the AB Testing bridge and third parties\n */\n private readonly options: AbTestBridgeConfig;\n /**\n * Observable with the list of AB testing experiments currently applied\n */\n public experiments$: Observable<T[]>;\n\n /**\n * AbTestBridge constructor\n * @param isExperimentEqual check two different experiments match to identify an experiment to start or to stop\n * @param options configure the communication with the A/B testing third party provider\n */\n constructor(private readonly isExperimentEqual: (value1?: T, value2?: T) => boolean, options?: Partial<AbTestBridgeConfig>) {\n this.experiments$ = this.experimentSubject$.pipe(\n distinctUntilChanged((experimentsA: T[], experimentsB: T[]) =>\n experimentsB.length === experimentsA.length && experimentsA.every((eA) => experimentsB.find((eB) => isExperimentEqual(eA, eB))\n ))\n );\n this.options = {\n ...defaultOptions,\n ...options\n };\n if ((window as any)[this.options.bridgeName]) {\n this.log(`An instance of ${this.options.bridgeName} already exists. This AbTestBridge instance will be ignored`);\n } else {\n (window as any)[this.options.bridgeName] = { start: this.start.bind(this), stop: this.stop.bind(this) };\n }\n document.dispatchEvent(new CustomEvent(this.options.readyEventName));\n }\n\n /**\n * Use configured logger to log AB testing related information\n * @param args\n */\n private log(...args: any[]) {\n (this.options.logger.debug || this.options.logger.log)('A/B Test', ...args);\n }\n\n /**\n * @inheritDoc\n */\n public start(experiments: T | T[]) {\n this.log('Start experiment', experiments);\n const currentProfile = this.experimentSubject$.getValue();\n this.experimentSubject$.next([\n ...currentProfile,\n ...(Array.isArray(experiments)\n ? experiments\n : [experiments]).filter((exp) => !currentProfile.some((expB: T) => this.isExperimentEqual(exp, expB))\n )\n ]);\n }\n\n /**\n * @inheritDoc\n */\n public stop(experiments?: T | T[]) {\n this.log('Stop experiment', experiments);\n const currentExperiments = this.experimentSubject$.getValue();\n if (experiments) {\n // Stop the mentioned experiment\n this.experimentSubject$.next(currentExperiments.filter((expB: T) => !(Array.isArray(experiments) ? experiments : [experiments]).some((expA) => this.isExperimentEqual(expB, expA)))\n );\n } else {\n // Stop all the experiment\n this.experimentSubject$.next([]);\n }\n }\n}\n","import {\n IFrameBridgeOptions,\n InternalIframeMessage,\n} from './contracts';\n\n/**\n * Default options for an IFrameBridge\n */\nexport const IFRAME_BRIDGE_DEFAULT_OPTIONS: Readonly<IFrameBridgeOptions> = {\n handshakeTries: 10,\n handshakeTimeout: 200,\n messageWithResponseTimeout: 1000\n} as const;\n\n/**\n * Verifies if a message respects the format expected by an IFrameBridge\n * @param message\n */\nexport function isSupportedMessage(message: any): message is InternalIframeMessage {\n return typeof message === 'object'\n && !!message.action\n && !!message.version\n && !!message.channelId;\n}\n\n/**\n * Generates the html content of an iframe\n * @param scriptUrl script to be executed inside the iframe\n * @param additionalHeader custom html headers stringified\n */\nexport function generateIFrameContent(scriptUrl: string, additionalHeader = '') {\n return `<html>\n <head>\n <script>\n class Bridge {\n handshakeDone = false;\n\n queuedMessages = [];\n\n channelId;\n\n messagesBuffer = [];\n\n listener;\n\n constructor() {\n if (window.parent) {\n window.addEventListener('message', (event) => {\n const message = event.data;\n if (this.isValidMessage(message)) {\n if (message.action === 'HANDSHAKE_PARENT') {\n this.channelId = message.channelId;\n this.sendMessage({action: 'HANDSHAKE_CHILD', version: '1.0', id: message.id});\n this.handshakeDone = true;\n this.queuedMessages.forEach((queuedMessage) => this.sendMessage(queuedMessage));\n this.queuedMessages = [];\n } else if (this.channelId && this.channelId === message.channelId) {\n // actual message\n if (this.listener) {\n this.listener(message);\n } else {\n this.messagesBuffer.push(message);\n }\n }\n }\n });\n } else {\n throw new Error('Error in child frame bridge: can\\\\'t access parent window.');\n }\n }\n\n register(handlerFunction, replayMissedMessages) {\n if (!this.listener) {\n this.listener = handlerFunction;\n if (replayMissedMessages) {\n this.messagesBuffer.forEach((message) => handlerFunction(message));\n }\n this.messagesBuffer = [];\n }\n }\n\n isValidMessage(message) {\n return !!message.action && !!message.version && !!message.channelId;\n }\n\n sendMessage(message) {\n if(this.handshakeDone) {\n window.parent.postMessage({...message, channelId: this.channelId}, '*');\n } else {\n this.queuedMessages.push(message);\n }\n }\n\n uuid() {\n return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function(c) {\n var r = Math.random() * 16 | 0, v = c == 'x' ? r : (r & 0x3 | 0x8);\n return v.toString(16);\n });\n }\n }\n\n const BRIDGE = new Bridge();\n </script>\n <script src='${scriptUrl}'></script>\n ${additionalHeader}\n </head>\n <body></body>\n</html>`;\n}\n","import {\n firstValueFrom,\n fromEvent,\n Observable,\n} from 'rxjs';\nimport {\n filter,\n map,\n share,\n timeout,\n} from 'rxjs/operators';\nimport {\n v4,\n} from 'uuid';\nimport {\n IFrameBridgeOptions,\n IframeMessage,\n InternalIframeMessage,\n} from './contracts';\nimport {\n IFRAME_BRIDGE_DEFAULT_OPTIONS,\n isSupportedMessage,\n} from './helpers';\n\n/**\n * Bridge that exposes an easy abstraction layer to communicate between a Host and an IFrame using the\n * postMessage API.\n */\nexport class IframeBridge {\n /** ID used to ensure that the Bridge only processes messages meant for this instance, since postMessage is global to the window. */\n private readonly channelId: string;\n\n /** Observable that emits all the messages received from the IFrame. */\n private readonly internalMessages$: Observable<InternalIframeMessage>;\n\n /** Promise that will resolve once the handshake has been completed, undefined if it's already done. */\n private readonly handshakePromise?: Promise<void>;\n\n /** Options to configure the behaviour of the Bridge. */\n private readonly options: IFrameBridgeOptions;\n\n /**\n * Observable that emits all the messages received from the IFrame and that are\n * not a direct response to a request.\n */\n public readonly messages$: Observable<InternalIframeMessage>;\n\n constructor(parent: Window, private readonly child: HTMLIFrameElement, options: Partial<IFrameBridgeOptions> = {}) {\n this.options = { ...IFRAME_BRIDGE_DEFAULT_OPTIONS, ...options };\n this.channelId = v4();\n this.internalMessages$ = fromEvent(parent, 'message').pipe(\n filter((event): event is MessageEvent<InternalIframeMessage> => {\n const messageEvent = event as MessageEvent<InternalIframeMessage>;\n return isSupportedMessage(messageEvent.data) && messageEvent.data.channelId === this.channelId;\n }),\n map((event) => event.data),\n share()\n );\n this.messages$ = this.internalMessages$.pipe(\n // Here we remove all the messages having an \"ID\" because they are bound to their corresponding request and\n // are returned directly by the function sendMessageAndWaitForResponse\n filter((message) => !message.id)\n );\n\n this.handshakePromise = this.handshake();\n }\n\n private async handshake() {\n for (let i = 0; i < this.options.handshakeTries; i++) {\n try {\n await this._sendMessageAndWaitForResponse({\n action: 'HANDSHAKE_PARENT',\n version: '1.0'\n }, this.options.handshakeTimeout);\n return;\n } catch {}\n }\n throw new Error('Handshake failed.');\n }\n\n private _sendMessage(message: IframeMessage, messageId?: string) {\n if (this.child.contentWindow) {\n this.child.contentWindow.postMessage({ ...message, channelId: this.channelId, id: messageId }, '*');\n }\n }\n\n private _sendMessageAndWaitForResponse(message: IframeMessage, timeoutMilliseconds: number = this.options.messageWithResponseTimeout) {\n const id = v4();\n const promise = firstValueFrom(\n this.internalMessages$.pipe(\n filter((response) => response.id === id),\n timeout(timeoutMilliseconds)\n )\n );\n void this._sendMessage(message, id);\n return promise;\n }\n\n /**\n * Method to send a message to the script run in the iframe\n * @param message message object\n * @param messageId message identifier\n */\n public async sendMessage(message: IframeMessage, messageId?: string) {\n await this.handshakePromise;\n this._sendMessage(message, messageId);\n }\n\n /**\n * Method to send a message to the script run in the iframe and wait for an answer\n * @param message\n * @param timeoutMilliseconds\n */\n public async sendMessageAndWaitForResponse(message: IframeMessage, timeoutMilliseconds: number = this.options.messageWithResponseTimeout) {\n await this.handshakePromise;\n return this._sendMessageAndWaitForResponse(message, timeoutMilliseconds);\n }\n}\n","/**\n * Generated bundle index. Do not edit.\n */\n\nexport * from './index';\n"],"names":[],"mappings":";;;;AA4CA;;AAEG;AACH,MAAM,cAAc,GAAG;AACrB,IAAA,UAAU,EAAE,cAAc;AAC1B,IAAA,cAAc,EAAE,eAAe;AAC/B,IAAA,MAAM,EAAE;CAC6B;AAEvC;;;;;;AAMG;MACU,YAAY,CAAA;AAcvB;;;;AAIG;IACH,WAAA,CAA6B,iBAAsD,EAAE,OAAqC,EAAA;QAA7F,IAAA,CAAA,iBAAiB,GAAjB,iBAAiB;AAlB9C;;AAEG;AACc,QAAA,IAAA,CAAA,kBAAkB,GAAyB,IAAI,eAAe,CAAM,EAAE,CAAC;QAgBtF,IAAI,CAAC,YAAY,GAAG,IAAI,CAAC,kBAAkB,CAAC,IAAI,CAC9C,oBAAoB,CAAC,CAAC,YAAiB,EAAE,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mBAAmB,CAAC;IAC1E;AACD;;ACrHD;;AAEG;;"}
|
|
1
|
+
{"version":3,"file":"o3r-third-party.mjs","names":[],"sources":["../../src/bridge/ab-testing/ab-testing-bridge.ts","../../src/bridge/iframe/helpers.ts","../../src/bridge/iframe/bridge.ts"],"sourcesContent":["import type {\n Logger,\n} from '@o3r/core';\nimport {\n BehaviorSubject,\n distinctUntilChanged,\n Observable,\n} from 'rxjs';\n\n/**\n * Shared interface with the A/B testing provider\n */\nexport interface AbTestBridgeInterface<T> {\n /**\n * Start an AB testing experiment\n */\n start(experiments: T | T[]): void;\n /**\n * Stop an AB testing experiment\n */\n stop(experiments?: T | T[]): void;\n}\n\n/**\n * Configure the A/B testing script interfaces with the application\n */\nexport interface AbTestBridgeConfig {\n /**\n * Reference to communicate with the bridge from the window\n * @default 'abTestBridge'\n */\n bridgeName: string;\n /**\n * Debug logger\n * @default console\n */\n logger: Logger;\n /**\n * Event a third party can subscribe to before starting the communication with the bridge\n * @default 'ab-test-ready'\n */\n readyEventName: string;\n}\n\n/**\n * Default options that will represent the interface\n */\nconst defaultOptions = {\n bridgeName: 'abTestBridge',\n readyEventName: 'ab-test-ready',\n logger: console\n} as const satisfies AbTestBridgeConfig;\n\n/**\n * Bridge between the application and a third party A/B testing provider.\n * Exposes a start and stop methods to allow the external script to set the list of experiments to run over the\n * application.\n *\n * Share the resulting list of experiments with the rest of the application via an observable.\n */\nexport class AbTestBridge<T> implements AbTestBridgeInterface<T> {\n /**\n * Behaviour subject to control the experiments via the exposed interface\n */\n private readonly experimentSubject$: BehaviorSubject<T[]> = new BehaviorSubject<T[]>([]);\n /**\n * Options to configure the communication between the AB Testing bridge and third parties\n */\n private readonly options: AbTestBridgeConfig;\n /**\n * Observable with the list of AB testing experiments currently applied\n */\n public experiments$: Observable<T[]>;\n\n /**\n * AbTestBridge constructor\n * @param isExperimentEqual check two different experiments match to identify an experiment to start or to stop\n * @param options configure the communication with the A/B testing third party provider\n */\n constructor(private readonly isExperimentEqual: (value1?: T, value2?: T) => boolean, options?: Partial<AbTestBridgeConfig>) {\n this.experiments$ = this.experimentSubject$.pipe(\n distinctUntilChanged((experimentsA: T[], experimentsB: T[]) =>\n experimentsB.length === experimentsA.length && experimentsA.every((eA) => experimentsB.find((eB) => isExperimentEqual(eA, eB))\n ))\n );\n this.options = {\n ...defaultOptions,\n ...options\n };\n if ((window as any)[this.options.bridgeName]) {\n this.log(`An instance of ${this.options.bridgeName} already exists. This AbTestBridge instance will be ignored`);\n } else {\n (window as any)[this.options.bridgeName] = { start: this.start.bind(this), stop: this.stop.bind(this) };\n }\n document.dispatchEvent(new CustomEvent(this.options.readyEventName));\n }\n\n /**\n * Use configured logger to log AB testing related information\n * @param args\n */\n private log(...args: any[]) {\n (this.options.logger.debug || this.options.logger.log)('A/B Test', ...args);\n }\n\n /**\n * @inheritDoc\n */\n public start(experiments: T | T[]) {\n this.log('Start experiment', experiments);\n const currentProfile = this.experimentSubject$.getValue();\n this.experimentSubject$.next([\n ...currentProfile,\n ...(Array.isArray(experiments)\n ? experiments\n : [experiments]).filter((exp) => !currentProfile.some((expB: T) => this.isExperimentEqual(exp, expB))\n )\n ]);\n }\n\n /**\n * @inheritDoc\n */\n public stop(experiments?: T | T[]) {\n this.log('Stop experiment', experiments);\n const currentExperiments = this.experimentSubject$.getValue();\n if (experiments) {\n // Stop the mentioned experiment\n this.experimentSubject$.next(currentExperiments.filter((expB: T) => !(Array.isArray(experiments) ? experiments : [experiments]).some((expA) => this.isExperimentEqual(expB, expA)))\n );\n } else {\n // Stop all the experiment\n this.experimentSubject$.next([]);\n }\n }\n}\n","import {\n IFrameBridgeOptions,\n InternalIframeMessage,\n} from './contracts';\n\n/**\n * Default options for an IFrameBridge\n */\nexport const IFRAME_BRIDGE_DEFAULT_OPTIONS: Readonly<IFrameBridgeOptions> = {\n handshakeTries: 10,\n handshakeTimeout: 200,\n messageWithResponseTimeout: 1000\n} as const;\n\n/**\n * Verifies if a message respects the format expected by an IFrameBridge\n * @param message\n */\nexport function isSupportedMessage(message: any): message is InternalIframeMessage {\n return typeof message === 'object'\n && !!message.action\n && !!message.version\n && !!message.channelId;\n}\n\n/**\n * Generates the html content of an iframe\n * @param scriptUrl script to be executed inside the iframe\n * @param additionalHeader custom html headers stringified\n */\nexport function generateIFrameContent(scriptUrl: string, additionalHeader = '') {\n return `<html>\n <head>\n <script>\n class Bridge {\n handshakeDone = false;\n\n queuedMessages = [];\n\n channelId;\n\n messagesBuffer = [];\n\n listener;\n\n constructor() {\n if (window.parent) {\n window.addEventListener('message', (event) => {\n const message = event.data;\n if (this.isValidMessage(message)) {\n if (message.action === 'HANDSHAKE_PARENT') {\n this.channelId = message.channelId;\n this.sendMessage({action: 'HANDSHAKE_CHILD', version: '1.0', id: message.id});\n this.handshakeDone = true;\n this.queuedMessages.forEach((queuedMessage) => this.sendMessage(queuedMessage));\n this.queuedMessages = [];\n } else if (this.channelId && this.channelId === message.channelId) {\n // actual message\n if (this.listener) {\n this.listener(message);\n } else {\n this.messagesBuffer.push(message);\n }\n }\n }\n });\n } else {\n throw new Error('Error in child frame bridge: can\\\\'t access parent window.');\n }\n }\n\n register(handlerFunction, replayMissedMessages) {\n if (!this.listener) {\n this.listener = handlerFunction;\n if (replayMissedMessages) {\n this.messagesBuffer.forEach((message) => handlerFunction(message));\n }\n this.messagesBuffer = [];\n }\n }\n\n isValidMessage(message) {\n return !!message.action && !!message.version && !!message.channelId;\n }\n\n sendMessage(message) {\n if(this.handshakeDone) {\n window.parent.postMessage({...message, channelId: this.channelId}, '*');\n } else {\n this.queuedMessages.push(message);\n }\n }\n\n uuid() {\n return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function(c) {\n var r = Math.random() * 16 | 0, v = c == 'x' ? r : (r & 0x3 | 0x8);\n return v.toString(16);\n });\n }\n }\n\n const BRIDGE = new Bridge();\n </script>\n <script src='${scriptUrl}'></script>\n ${additionalHeader}\n </head>\n <body></body>\n</html>`;\n}\n","import {\n firstValueFrom,\n fromEvent,\n Observable,\n} from 'rxjs';\nimport {\n filter,\n map,\n share,\n timeout,\n} from 'rxjs/operators';\nimport {\n v4,\n} from 'uuid';\nimport {\n IFrameBridgeOptions,\n IframeMessage,\n InternalIframeMessage,\n} from './contracts';\nimport {\n IFRAME_BRIDGE_DEFAULT_OPTIONS,\n isSupportedMessage,\n} from './helpers';\n\n/**\n * Bridge that exposes an easy abstraction layer to communicate between a Host and an IFrame using the\n * postMessage API.\n */\nexport class IframeBridge {\n /** ID used to ensure that the Bridge only processes messages meant for this instance, since postMessage is global to the window. */\n private readonly channelId: string;\n\n /** Observable that emits all the messages received from the IFrame. */\n private readonly internalMessages$: Observable<InternalIframeMessage>;\n\n /** Promise that will resolve once the handshake has been completed, undefined if it's already done. */\n private readonly handshakePromise?: Promise<void>;\n\n /** Options to configure the behaviour of the Bridge. */\n private readonly options: IFrameBridgeOptions;\n\n /**\n * Observable that emits all the messages received from the IFrame and that are\n * not a direct response to a request.\n */\n public readonly messages$: Observable<InternalIframeMessage>;\n\n constructor(parent: Window, private readonly child: HTMLIFrameElement, options: Partial<IFrameBridgeOptions> = {}) {\n this.options = { ...IFRAME_BRIDGE_DEFAULT_OPTIONS, ...options };\n this.channelId = v4();\n this.internalMessages$ = fromEvent(parent, 'message').pipe(\n filter((event): event is MessageEvent<InternalIframeMessage> => {\n const messageEvent = event as MessageEvent<InternalIframeMessage>;\n return isSupportedMessage(messageEvent.data) && messageEvent.data.channelId === this.channelId;\n }),\n map((event) => event.data),\n share()\n );\n this.messages$ = this.internalMessages$.pipe(\n // Here we remove all the messages having an \"ID\" because they are bound to their corresponding request and\n // are returned directly by the function sendMessageAndWaitForResponse\n filter((message) => !message.id)\n );\n\n this.handshakePromise = this.handshake();\n }\n\n private async handshake() {\n for (let i = 0; i < this.options.handshakeTries; i++) {\n try {\n await this._sendMessageAndWaitForResponse({\n action: 'HANDSHAKE_PARENT',\n version: '1.0'\n }, this.options.handshakeTimeout);\n return;\n } catch {}\n }\n throw new Error('Handshake failed.');\n }\n\n private _sendMessage(message: IframeMessage, messageId?: string) {\n if (this.child.contentWindow) {\n this.child.contentWindow.postMessage({ ...message, channelId: this.channelId, id: messageId }, '*');\n }\n }\n\n private _sendMessageAndWaitForResponse(message: IframeMessage, timeoutMilliseconds: number = this.options.messageWithResponseTimeout) {\n const id = v4();\n const promise = firstValueFrom(\n this.internalMessages$.pipe(\n filter((response) => response.id === id),\n timeout(timeoutMilliseconds)\n )\n );\n void this._sendMessage(message, id);\n return promise;\n }\n\n /**\n * Method to send a message to the script run in the iframe\n * @param message message object\n * @param messageId message identifier\n */\n public async sendMessage(message: IframeMessage, messageId?: string) {\n await this.handshakePromise;\n this._sendMessage(message, messageId);\n }\n\n /**\n * Method to send a message to the script run in the iframe and wait for an answer\n * @param message\n * @param timeoutMilliseconds\n */\n public async sendMessageAndWaitForResponse(message: IframeMessage, timeoutMilliseconds: number = this.options.messageWithResponseTimeout) {\n await this.handshakePromise;\n return this._sendMessageAndWaitForResponse(message, timeoutMilliseconds);\n }\n}\n"],"mappings":";;;AA+CA,MAAM,iBAAiB;CACrB,YAAY;CACZ,gBAAgB;CAChB,QAAQ;AAC6B;AASvC,IAAa,eAAb,MAAyB;CAmBvB,YAA6B,mBAAwD,SAAqC;EAA7F,KAAA,oBAAA;EAfZ,KAAA,qBAA2C,IAAI,gBAAqB,CAAA,CAAE;EAgBrF,KAAK,eAAe,KAAK,mBAAmB,KAC1C,sBAAsB,cAAmB,iBACvC,aAAa,WAAW,aAAa,UAAU,aAAa,OAAO,OAAO,aAAa,MAAM,OAAO,kBAAkB,IAAI,EAAE,CAAC,CAAC,CAC7H,CAAC;EAEN,KAAK,UAAU;GACb,GAAG;GACH,GAAG;EACJ;EACD,IAAK,OAAe,KAAK,QAAQ,aAC/B,KAAK,IAAI,kBAAkB,KAAK,QAAQ,WAAU,4DAA6D;OAE9G,OAAe,KAAK,QAAQ,cAAc;GAAE,OAAO,KAAK,MAAM,KAAK,IAA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sBAA8B,KAAK,QAAQ,4BAA0B;EACtI,MAAM,KAAK;EACX,OAAO,KAAK,+BAA+B,SAAS,mBAAmB;CACzE;AACD"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@o3r/third-party",
|
|
3
|
-
"version": "15.3.0-prerelease.
|
|
3
|
+
"version": "15.3.0-prerelease.17",
|
|
4
4
|
"publishConfig": {
|
|
5
5
|
"access": "public"
|
|
6
6
|
},
|
|
@@ -12,14 +12,14 @@
|
|
|
12
12
|
"otter-module"
|
|
13
13
|
],
|
|
14
14
|
"dependencies": {
|
|
15
|
-
"@o3r/schematics": "~15.3.0-prerelease.
|
|
15
|
+
"@o3r/schematics": "~15.3.0-prerelease.17",
|
|
16
16
|
"tslib": "^2.6.2",
|
|
17
17
|
"uuid": "~14.0.0"
|
|
18
18
|
},
|
|
19
19
|
"peerDependencies": {
|
|
20
20
|
"@angular-devkit/schematics": "^22.0.0",
|
|
21
|
-
"@o3r/core": "~15.3.0-prerelease.
|
|
22
|
-
"@o3r/schematics": "~15.3.0-prerelease.
|
|
21
|
+
"@o3r/core": "~15.3.0-prerelease.17",
|
|
22
|
+
"@o3r/schematics": "~15.3.0-prerelease.17",
|
|
23
23
|
"@schematics/angular": "^22.0.0",
|
|
24
24
|
"rxjs": "^7.8.1",
|
|
25
25
|
"type-fest": "^5.3.1",
|
|
@@ -1,38 +1,37 @@
|
|
|
1
|
-
import { Logger } from
|
|
2
|
-
import { Observable } from
|
|
3
|
-
|
|
1
|
+
import { Logger } from "@o3r/core";
|
|
2
|
+
import { Observable } from "rxjs";
|
|
4
3
|
/**
|
|
5
4
|
* Shared interface with the A/B testing provider
|
|
6
5
|
*/
|
|
7
|
-
interface AbTestBridgeInterface<T> {
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
6
|
+
export interface AbTestBridgeInterface<T> {
|
|
7
|
+
/**
|
|
8
|
+
* Start an AB testing experiment
|
|
9
|
+
*/
|
|
10
|
+
start(experiments: T | T[]): void;
|
|
11
|
+
/**
|
|
12
|
+
* Stop an AB testing experiment
|
|
13
|
+
*/
|
|
14
|
+
stop(experiments?: T | T[]): void;
|
|
16
15
|
}
|
|
17
16
|
/**
|
|
18
17
|
* Configure the A/B testing script interfaces with the application
|
|
19
18
|
*/
|
|
20
|
-
interface AbTestBridgeConfig {
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
19
|
+
export interface AbTestBridgeConfig {
|
|
20
|
+
/**
|
|
21
|
+
* Reference to communicate with the bridge from the window
|
|
22
|
+
* @default 'abTestBridge'
|
|
23
|
+
*/
|
|
24
|
+
bridgeName: string;
|
|
25
|
+
/**
|
|
26
|
+
* Debug logger
|
|
27
|
+
* @default console
|
|
28
|
+
*/
|
|
29
|
+
logger: Logger;
|
|
30
|
+
/**
|
|
31
|
+
* Event a third party can subscribe to before starting the communication with the bridge
|
|
32
|
+
* @default 'ab-test-ready'
|
|
33
|
+
*/
|
|
34
|
+
readyEventName: string;
|
|
36
35
|
}
|
|
37
36
|
/**
|
|
38
37
|
* Bridge between the application and a third party A/B testing provider.
|
|
@@ -41,142 +40,136 @@ interface AbTestBridgeConfig {
|
|
|
41
40
|
*
|
|
42
41
|
* Share the resulting list of experiments with the rest of the application via an observable.
|
|
43
42
|
*/
|
|
44
|
-
declare class AbTestBridge<T> implements AbTestBridgeInterface<T> {
|
|
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
|
-
|
|
43
|
+
export declare class AbTestBridge<T> implements AbTestBridgeInterface<T> {
|
|
44
|
+
private readonly isExperimentEqual;
|
|
45
|
+
/**
|
|
46
|
+
* Behaviour subject to control the experiments via the exposed interface
|
|
47
|
+
*/
|
|
48
|
+
private readonly experimentSubject$;
|
|
49
|
+
/**
|
|
50
|
+
* Options to configure the communication between the AB Testing bridge and third parties
|
|
51
|
+
*/
|
|
52
|
+
private readonly options;
|
|
53
|
+
/**
|
|
54
|
+
* Observable with the list of AB testing experiments currently applied
|
|
55
|
+
*/
|
|
56
|
+
experiments$: Observable<T[]>;
|
|
57
|
+
/**
|
|
58
|
+
* AbTestBridge constructor
|
|
59
|
+
* @param isExperimentEqual check two different experiments match to identify an experiment to start or to stop
|
|
60
|
+
* @param options configure the communication with the A/B testing third party provider
|
|
61
|
+
*/
|
|
62
|
+
constructor(isExperimentEqual: (value1?: T, value2?: T) => boolean, options?: Partial<AbTestBridgeConfig>);
|
|
63
|
+
/**
|
|
64
|
+
* Use configured logger to log AB testing related information
|
|
65
|
+
* @param args
|
|
66
|
+
*/
|
|
67
|
+
private log;
|
|
68
|
+
/**
|
|
69
|
+
* @inheritDoc
|
|
70
|
+
*/
|
|
71
|
+
start(experiments: T | T[]): void;
|
|
72
|
+
/**
|
|
73
|
+
* @inheritDoc
|
|
74
|
+
*/
|
|
75
|
+
stop(experiments?: T | T[]): void;
|
|
77
76
|
}
|
|
78
|
-
|
|
79
77
|
/**
|
|
80
78
|
* Represents messages exchanged between host and iFrame.
|
|
81
79
|
*/
|
|
82
|
-
interface IframeMessage {
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
80
|
+
export interface IframeMessage {
|
|
81
|
+
/**
|
|
82
|
+
* String used to identify the type of action to perform.
|
|
83
|
+
*/
|
|
84
|
+
action: string;
|
|
85
|
+
/**
|
|
86
|
+
* The version of the action, to allow for backward and forward compatibility.
|
|
87
|
+
*/
|
|
88
|
+
version: string;
|
|
89
|
+
/**
|
|
90
|
+
* Payload of the message.
|
|
91
|
+
*/
|
|
92
|
+
data?: unknown;
|
|
95
93
|
}
|
|
96
|
-
interface InternalIframeMessage extends IframeMessage {
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
94
|
+
export interface InternalIframeMessage extends IframeMessage {
|
|
95
|
+
/**
|
|
96
|
+
* The ID associated to the channel Host <-> iFrame
|
|
97
|
+
*/
|
|
98
|
+
channelId: string;
|
|
99
|
+
/**
|
|
100
|
+
* ID used to handle messages that expect a response from the other party.
|
|
101
|
+
* No used for unidirectional messages.
|
|
102
|
+
*/
|
|
103
|
+
id?: string;
|
|
106
104
|
}
|
|
107
105
|
/**
|
|
108
106
|
* Options that can be passed to configure an IFrameBridge
|
|
109
107
|
*/
|
|
110
|
-
interface IFrameBridgeOptions {
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
108
|
+
export interface IFrameBridgeOptions {
|
|
109
|
+
/**
|
|
110
|
+
* Number of times the Host will try to handshake with the iFrame before failing.
|
|
111
|
+
*/
|
|
112
|
+
handshakeTries: number;
|
|
113
|
+
/**
|
|
114
|
+
* For a given handshake try, how long will the host wait for an answer before considering
|
|
115
|
+
* that the try failed.
|
|
116
|
+
*/
|
|
117
|
+
handshakeTimeout: number;
|
|
118
|
+
/**
|
|
119
|
+
* When sending a message that expects a response, how long should the Bridge wait before
|
|
120
|
+
* considering there will be answer.
|
|
121
|
+
*/
|
|
122
|
+
messageWithResponseTimeout: number;
|
|
125
123
|
}
|
|
126
|
-
|
|
127
124
|
/**
|
|
128
125
|
* Bridge that exposes an easy abstraction layer to communicate between a Host and an IFrame using the
|
|
129
126
|
* postMessage API.
|
|
130
127
|
*/
|
|
131
|
-
declare class IframeBridge {
|
|
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
|
-
|
|
128
|
+
export declare class IframeBridge {
|
|
129
|
+
private readonly child;
|
|
130
|
+
/** ID used to ensure that the Bridge only processes messages meant for this instance, since postMessage is global to the window. */
|
|
131
|
+
private readonly channelId;
|
|
132
|
+
/** Observable that emits all the messages received from the IFrame. */
|
|
133
|
+
private readonly internalMessages$;
|
|
134
|
+
/** Promise that will resolve once the handshake has been completed, undefined if it's already done. */
|
|
135
|
+
private readonly handshakePromise?;
|
|
136
|
+
/** Options to configure the behaviour of the Bridge. */
|
|
137
|
+
private readonly options;
|
|
138
|
+
/**
|
|
139
|
+
* Observable that emits all the messages received from the IFrame and that are
|
|
140
|
+
* not a direct response to a request.
|
|
141
|
+
*/
|
|
142
|
+
readonly messages$: Observable<InternalIframeMessage>;
|
|
143
|
+
constructor(parent: Window, child: HTMLIFrameElement, options?: Partial<IFrameBridgeOptions>);
|
|
144
|
+
private handshake;
|
|
145
|
+
private _sendMessage;
|
|
146
|
+
private _sendMessageAndWaitForResponse;
|
|
147
|
+
/**
|
|
148
|
+
* Method to send a message to the script run in the iframe
|
|
149
|
+
* @param message message object
|
|
150
|
+
* @param messageId message identifier
|
|
151
|
+
*/
|
|
152
|
+
sendMessage(message: IframeMessage, messageId?: string): Promise<void>;
|
|
153
|
+
/**
|
|
154
|
+
* Method to send a message to the script run in the iframe and wait for an answer
|
|
155
|
+
* @param message
|
|
156
|
+
* @param timeoutMilliseconds
|
|
157
|
+
*/
|
|
158
|
+
sendMessageAndWaitForResponse(message: IframeMessage, timeoutMilliseconds?: number): Promise<InternalIframeMessage>;
|
|
162
159
|
}
|
|
163
|
-
|
|
164
160
|
/**
|
|
165
161
|
* Default options for an IFrameBridge
|
|
166
162
|
*/
|
|
167
|
-
declare const IFRAME_BRIDGE_DEFAULT_OPTIONS: Readonly<IFrameBridgeOptions>;
|
|
163
|
+
export declare const IFRAME_BRIDGE_DEFAULT_OPTIONS: Readonly<IFrameBridgeOptions>;
|
|
168
164
|
/**
|
|
169
165
|
* Verifies if a message respects the format expected by an IFrameBridge
|
|
170
166
|
* @param message
|
|
171
167
|
*/
|
|
172
|
-
declare function isSupportedMessage(message: any): message is InternalIframeMessage;
|
|
168
|
+
export declare function isSupportedMessage(message: any): message is InternalIframeMessage;
|
|
173
169
|
/**
|
|
174
170
|
* Generates the html content of an iframe
|
|
175
171
|
* @param scriptUrl script to be executed inside the iframe
|
|
176
172
|
* @param additionalHeader custom html headers stringified
|
|
177
173
|
*/
|
|
178
|
-
declare function generateIFrameContent(scriptUrl: string, additionalHeader?: string): string;
|
|
179
|
-
|
|
180
|
-
export { AbTestBridge, IFRAME_BRIDGE_DEFAULT_OPTIONS, IframeBridge, generateIFrameContent, isSupportedMessage };
|
|
181
|
-
export type { AbTestBridgeConfig, AbTestBridgeInterface, IFrameBridgeOptions, IframeMessage, InternalIframeMessage };
|
|
182
|
-
//# sourceMappingURL=o3r-third-party.d.ts.map
|
|
174
|
+
export declare function generateIFrameContent(scriptUrl: string, additionalHeader?: string): string;
|
|
175
|
+
//# sourceMappingURL=o3r-third-party.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"o3r-third-party.d.ts","sources":["../../src/bridge/ab-testing/ab-testing-bridge.ts","../../src/bridge/iframe/contracts.ts","../../src/bridge/iframe/bridge.ts","../../src/bridge/iframe/helpers.ts"],"mappings":"
|
|
1
|
+
{"version":3,"file":"o3r-third-party.d.ts","names":[],"sources":["../../src/bridge/ab-testing/ab-testing-bridge.ts","../../src/bridge/iframe/contracts.ts","../../src/bridge/iframe/bridge.ts","../../src/bridge/iframe/helpers.ts"],"mappings":";;;;;iBAYiB,sBAAsB;;;;EAIrC,MAAM,aAAa,IAAI;;;;EAIvB,KAAK,cAAc,IAAI;;;;;iBAMR;;;;;EAKf;;;;;EAKA,QAAQ;;;;;EAKR;;;;;;;;;qBAmBW,aAAa,cAAc,sBAAsB;mBAmB/B;;;;mBAfZ;;;;mBAIA;;;;EAIV,cAAc,WAAW;;;;;;EAOH,YAAA,oBAAoB,SAAS,GAAG,SAAS,eAAe,UAAU,QAAQ;;;;;UAsB/F;;;;EAOD,MAAM,aAAa,IAAI;;;;EAevB,KAAK,cAAc,IAAI;;;;;iBCxHf;;;;EAIf;;;;EAKA;;;;EAKA;;iBAGe,8BAA8B;;;;EAI7C;;;;;EAMA;;;;;iBAMe;;;;EAIf;;;;;EAMA;;;;;EAMA;;;;;;qBCxBW;mBAmBkC;;mBAjB5B;;mBAGA;;mBAGA;;mBAGA;;;;;WAMD,WAAW,WAAW;EAE1B,YAAA,QAAQ,QAAyB,OAAO,mBAAmB,UAAS,QAAQ;UAoB1E;UAaN;UAMA;;;;;;EAiBK,YAAY,SAAS,eAAe,qBAAkB;;;;;;EAUtD,8BAA8B,SAAS,eAAe,+BAAqE,QAAA;;;;;qBCzG7H,+BAA+B,SAAS;;;;;wBAUrC,mBAAmB,eAAe,WAAW;;;;;;wBAY7C,sBAAsB,mBAAmB"}
|