@foxglove/embed 0.67.0 → 0.69.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +3 -0
- package/dist/FoxgloveViewer.d.ts +3 -1
- package/dist/FoxgloveViewer.d.ts.map +1 -1
- package/dist/FoxgloveViewer.js +74 -15
- package/dist/FoxgloveViewer.js.map +1 -1
- package/dist/ParentTransportBridge.d.ts +9 -0
- package/dist/ParentTransportBridge.d.ts.map +1 -0
- package/dist/ParentTransportBridge.js +99 -0
- package/dist/ParentTransportBridge.js.map +1 -0
- package/dist/commands.d.ts +46 -3
- package/dist/commands.d.ts.map +1 -1
- package/dist/commands.js +3 -0
- package/dist/commands.js.map +1 -1
- package/dist/types.d.ts +51 -1
- package/dist/types.d.ts.map +1 -1
- package/package.json +1 -1
- package/src/FoxgloveViewer.test.ts +215 -3
- package/src/FoxgloveViewer.ts +127 -41
- package/src/ParentTransportBridge.test.ts +91 -0
- package/src/ParentTransportBridge.ts +111 -0
- package/src/commands.ts +36 -3
- package/src/types.ts +59 -1
|
@@ -1,6 +1,11 @@
|
|
|
1
1
|
/** @jest-environment jsdom */
|
|
2
|
+
import {
|
|
3
|
+
MessageChannel as NodeMessageChannel,
|
|
4
|
+
MessagePort as NodeMessagePort,
|
|
5
|
+
} from "node:worker_threads";
|
|
6
|
+
|
|
2
7
|
import { FoxgloveViewer } from "./FoxgloveViewer.ts";
|
|
3
|
-
import type
|
|
8
|
+
import { IframeCapability, type IframeCommand } from "./commands.ts";
|
|
4
9
|
|
|
5
10
|
function capturePostedMessages(iframe: HTMLIFrameElement): IframeCommand[] {
|
|
6
11
|
const messages: IframeCommand[] = [];
|
|
@@ -20,15 +25,29 @@ function getIframe(parent: HTMLElement): HTMLIFrameElement {
|
|
|
20
25
|
return iframe;
|
|
21
26
|
}
|
|
22
27
|
|
|
23
|
-
function simulateHandshakeRequest(
|
|
28
|
+
function simulateHandshakeRequest(
|
|
29
|
+
_viewer: FoxgloveViewer,
|
|
30
|
+
iframe: HTMLIFrameElement,
|
|
31
|
+
capabilities: IframeCapability[] = [IframeCapability.parentOwnedLiveTransport],
|
|
32
|
+
) {
|
|
24
33
|
const event = new MessageEvent("message", {
|
|
25
|
-
data: { type: "foxglove-handshake-request" },
|
|
34
|
+
data: { type: "foxglove-handshake-request", capabilities },
|
|
26
35
|
source: iframe.contentWindow,
|
|
27
36
|
origin: "https://embed.foxglove.dev",
|
|
28
37
|
});
|
|
29
38
|
window.dispatchEvent(event);
|
|
30
39
|
}
|
|
31
40
|
|
|
41
|
+
function simulateLegacyHandshakeRequest(iframe: HTMLIFrameElement) {
|
|
42
|
+
window.dispatchEvent(
|
|
43
|
+
new MessageEvent("message", {
|
|
44
|
+
data: { type: "foxglove-handshake-request" },
|
|
45
|
+
source: iframe.contentWindow,
|
|
46
|
+
origin: "https://embed.foxglove.dev",
|
|
47
|
+
}),
|
|
48
|
+
);
|
|
49
|
+
}
|
|
50
|
+
|
|
32
51
|
describe("FoxgloveViewer.seekPlayback", () => {
|
|
33
52
|
let parent: HTMLDivElement;
|
|
34
53
|
|
|
@@ -88,6 +107,199 @@ describe("FoxgloveViewer.seekPlayback", () => {
|
|
|
88
107
|
});
|
|
89
108
|
});
|
|
90
109
|
|
|
110
|
+
describe("FoxgloveViewer parent transport", () => {
|
|
111
|
+
beforeAll(() => {
|
|
112
|
+
Object.assign(globalThis, {
|
|
113
|
+
MessageChannel: NodeMessageChannel,
|
|
114
|
+
MessagePort: NodeMessagePort,
|
|
115
|
+
});
|
|
116
|
+
});
|
|
117
|
+
|
|
118
|
+
it("exposes pending capabilities until the iframe advertises availability", () => {
|
|
119
|
+
const parent = document.createElement("div");
|
|
120
|
+
document.body.appendChild(parent);
|
|
121
|
+
const viewer = new FoxgloveViewer({ parent, orgSlug: undefined });
|
|
122
|
+
const iframe = getIframe(parent);
|
|
123
|
+
const onCapabilities = jest.fn();
|
|
124
|
+
viewer.addEventListener("capabilities", onCapabilities);
|
|
125
|
+
|
|
126
|
+
expect(viewer.getCapabilities()).toEqual({ parentOwnedLiveTransport: "pending" });
|
|
127
|
+
|
|
128
|
+
simulateHandshakeRequest(viewer, iframe);
|
|
129
|
+
|
|
130
|
+
expect(viewer.getCapabilities()).toEqual({ parentOwnedLiveTransport: "available" });
|
|
131
|
+
expect(onCapabilities).toHaveBeenCalledWith(
|
|
132
|
+
expect.objectContaining({
|
|
133
|
+
detail: { parentOwnedLiveTransport: "available" },
|
|
134
|
+
}),
|
|
135
|
+
);
|
|
136
|
+
|
|
137
|
+
viewer.destroy();
|
|
138
|
+
parent.remove();
|
|
139
|
+
});
|
|
140
|
+
|
|
141
|
+
it("treats a legacy iframe without capability advertisement as unavailable", () => {
|
|
142
|
+
const parent = document.createElement("div");
|
|
143
|
+
document.body.appendChild(parent);
|
|
144
|
+
const viewer = new FoxgloveViewer({ parent, orgSlug: undefined });
|
|
145
|
+
const iframe = getIframe(parent);
|
|
146
|
+
const messages = capturePostedMessages(iframe);
|
|
147
|
+
|
|
148
|
+
simulateLegacyHandshakeRequest(iframe);
|
|
149
|
+
|
|
150
|
+
expect(viewer.getCapabilities()).toEqual({ parentOwnedLiveTransport: "unavailable" });
|
|
151
|
+
|
|
152
|
+
messages.length = 0;
|
|
153
|
+
viewer.setDataSource({
|
|
154
|
+
type: "live",
|
|
155
|
+
protocol: "foxglove-websocket",
|
|
156
|
+
url: "wss://example.com",
|
|
157
|
+
});
|
|
158
|
+
expect(messages).toContainEqual({
|
|
159
|
+
type: "set-data-source",
|
|
160
|
+
payload: {
|
|
161
|
+
type: "live",
|
|
162
|
+
protocol: "foxglove-websocket",
|
|
163
|
+
url: "wss://example.com",
|
|
164
|
+
},
|
|
165
|
+
parentTransport: undefined,
|
|
166
|
+
});
|
|
167
|
+
|
|
168
|
+
viewer.destroy();
|
|
169
|
+
parent.remove();
|
|
170
|
+
});
|
|
171
|
+
|
|
172
|
+
it("extends the initial data source handshake with a private message port", () => {
|
|
173
|
+
const parent = document.createElement("div");
|
|
174
|
+
document.body.appendChild(parent);
|
|
175
|
+
const viewer = new FoxgloveViewer({
|
|
176
|
+
parent,
|
|
177
|
+
orgSlug: undefined,
|
|
178
|
+
initialDataSource: {
|
|
179
|
+
type: "live",
|
|
180
|
+
protocol: "foxglove-websocket",
|
|
181
|
+
url: "virtual://robot",
|
|
182
|
+
transport: () => ({ send: jest.fn(), close: jest.fn() }),
|
|
183
|
+
},
|
|
184
|
+
});
|
|
185
|
+
const iframe = getIframe(parent);
|
|
186
|
+
const messages = capturePostedMessages(iframe);
|
|
187
|
+
|
|
188
|
+
simulateHandshakeRequest(viewer, iframe);
|
|
189
|
+
|
|
190
|
+
const handshake = messages.find((message) => message.type === "handshake-ack");
|
|
191
|
+
expect(handshake).toMatchObject({
|
|
192
|
+
type: "handshake-ack",
|
|
193
|
+
payload: {
|
|
194
|
+
initialDataSource: {
|
|
195
|
+
type: "live",
|
|
196
|
+
protocol: "foxglove-websocket",
|
|
197
|
+
url: "virtual://robot",
|
|
198
|
+
transport: "parent",
|
|
199
|
+
},
|
|
200
|
+
initialParentTransport: { id: expect.any(String), port: expect.any(MessagePort) },
|
|
201
|
+
},
|
|
202
|
+
});
|
|
203
|
+
|
|
204
|
+
viewer.destroy();
|
|
205
|
+
parent.remove();
|
|
206
|
+
});
|
|
207
|
+
|
|
208
|
+
it("does not send the parent transport factory into the iframe", () => {
|
|
209
|
+
const parent = document.createElement("div");
|
|
210
|
+
document.body.appendChild(parent);
|
|
211
|
+
const viewer = new FoxgloveViewer({ parent, orgSlug: undefined });
|
|
212
|
+
const iframe = getIframe(parent);
|
|
213
|
+
const messages = capturePostedMessages(iframe);
|
|
214
|
+
const transport = () => ({ send: jest.fn(), close: jest.fn() });
|
|
215
|
+
|
|
216
|
+
simulateHandshakeRequest(viewer, iframe);
|
|
217
|
+
messages.length = 0;
|
|
218
|
+
viewer.setDataSource({
|
|
219
|
+
type: "live",
|
|
220
|
+
protocol: "foxglove-websocket",
|
|
221
|
+
url: "virtual://robot",
|
|
222
|
+
transport,
|
|
223
|
+
});
|
|
224
|
+
|
|
225
|
+
const command = messages.find((message) => message.type === "set-data-source");
|
|
226
|
+
expect(command).toMatchObject({
|
|
227
|
+
payload: { transport: "parent" },
|
|
228
|
+
parentTransport: { id: expect.any(String), port: expect.any(MessagePort) },
|
|
229
|
+
});
|
|
230
|
+
|
|
231
|
+
viewer.destroy();
|
|
232
|
+
parent.remove();
|
|
233
|
+
});
|
|
234
|
+
|
|
235
|
+
it("reports an error instead of sending a parent transport to an unsupported iframe", () => {
|
|
236
|
+
const parent = document.createElement("div");
|
|
237
|
+
document.body.appendChild(parent);
|
|
238
|
+
const viewer = new FoxgloveViewer({
|
|
239
|
+
parent,
|
|
240
|
+
orgSlug: undefined,
|
|
241
|
+
initialDataSource: {
|
|
242
|
+
type: "live",
|
|
243
|
+
protocol: "foxglove-websocket",
|
|
244
|
+
url: "virtual://robot",
|
|
245
|
+
transport: () => ({ send: jest.fn(), close: jest.fn() }),
|
|
246
|
+
},
|
|
247
|
+
});
|
|
248
|
+
const iframe = getIframe(parent);
|
|
249
|
+
const messages = capturePostedMessages(iframe);
|
|
250
|
+
const onError = jest.fn();
|
|
251
|
+
viewer.addEventListener("error", onError);
|
|
252
|
+
|
|
253
|
+
simulateHandshakeRequest(viewer, iframe, []);
|
|
254
|
+
|
|
255
|
+
const handshake = messages.find((message) => message.type === "handshake-ack");
|
|
256
|
+
expect(handshake).toMatchObject({
|
|
257
|
+
type: "handshake-ack",
|
|
258
|
+
payload: {
|
|
259
|
+
initialDataSource: undefined,
|
|
260
|
+
initialParentTransport: undefined,
|
|
261
|
+
},
|
|
262
|
+
});
|
|
263
|
+
expect(onError).toHaveBeenCalledWith(
|
|
264
|
+
expect.objectContaining({
|
|
265
|
+
detail: "[FoxgloveViewer] Parent-owned live transport is not supported by this iframe.",
|
|
266
|
+
}),
|
|
267
|
+
);
|
|
268
|
+
|
|
269
|
+
viewer.destroy();
|
|
270
|
+
parent.remove();
|
|
271
|
+
});
|
|
272
|
+
|
|
273
|
+
it("does not send a parent-owned live source to an unsupported ready iframe", () => {
|
|
274
|
+
const parent = document.createElement("div");
|
|
275
|
+
document.body.appendChild(parent);
|
|
276
|
+
const viewer = new FoxgloveViewer({ parent, orgSlug: undefined });
|
|
277
|
+
const iframe = getIframe(parent);
|
|
278
|
+
const messages = capturePostedMessages(iframe);
|
|
279
|
+
const onError = jest.fn();
|
|
280
|
+
viewer.addEventListener("error", onError);
|
|
281
|
+
|
|
282
|
+
simulateHandshakeRequest(viewer, iframe, []);
|
|
283
|
+
messages.length = 0;
|
|
284
|
+
viewer.setDataSource({
|
|
285
|
+
type: "live",
|
|
286
|
+
protocol: "foxglove-websocket",
|
|
287
|
+
url: "virtual://robot",
|
|
288
|
+
transport: () => ({ send: jest.fn(), close: jest.fn() }),
|
|
289
|
+
});
|
|
290
|
+
|
|
291
|
+
expect(messages).toHaveLength(0);
|
|
292
|
+
expect(onError).toHaveBeenCalledWith(
|
|
293
|
+
expect.objectContaining({
|
|
294
|
+
detail: "[FoxgloveViewer] Parent-owned live transport is not supported by this iframe.",
|
|
295
|
+
}),
|
|
296
|
+
);
|
|
297
|
+
|
|
298
|
+
viewer.destroy();
|
|
299
|
+
parent.remove();
|
|
300
|
+
});
|
|
301
|
+
});
|
|
302
|
+
|
|
91
303
|
describe("fromSec (via seekPlayback)", () => {
|
|
92
304
|
let parent: HTMLDivElement;
|
|
93
305
|
|
package/src/FoxgloveViewer.ts
CHANGED
|
@@ -1,29 +1,34 @@
|
|
|
1
1
|
import type { EmbeddedViewer } from "@foxglove/common";
|
|
2
2
|
|
|
3
|
-
import
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
3
|
+
import { ParentTransportBridge } from "./ParentTransportBridge.ts";
|
|
4
|
+
import {
|
|
5
|
+
IframeCapability,
|
|
6
|
+
type IframeCommand,
|
|
7
|
+
type IframeEvent,
|
|
8
|
+
type IframeGetLayoutCommand,
|
|
9
|
+
type IframeHandshakeAckCommand,
|
|
10
|
+
type IframeInstallExtensionCommand,
|
|
11
|
+
type IframeOriginAckCommand,
|
|
12
|
+
type IframePausePlaybackCommand,
|
|
13
|
+
type IframeParentTransport,
|
|
14
|
+
type IframePlayUntilCommand,
|
|
15
|
+
type IframeSeekPlaybackCommand,
|
|
16
|
+
type IframeSelectLayoutCommand,
|
|
17
|
+
type IframeSetDatasourceCommand,
|
|
18
|
+
type IframeSetKeybindingConfigCommand,
|
|
19
|
+
type IframeSetLayoutCommand,
|
|
20
|
+
type IframeStartPlaybackCommand,
|
|
21
|
+
type SerializedKeybindingConfig,
|
|
19
22
|
} from "./commands.ts";
|
|
20
23
|
import type {
|
|
21
24
|
DataSource,
|
|
25
|
+
FoxgloveViewerCapabilities,
|
|
22
26
|
FoxgloveViewerEventMap,
|
|
23
27
|
FoxgloveViewerEventType,
|
|
24
28
|
Keybinding,
|
|
25
29
|
KeybindingBase,
|
|
26
30
|
OpaqueLayoutData,
|
|
31
|
+
ParentLiveSource,
|
|
27
32
|
SelectLayoutParams,
|
|
28
33
|
Time,
|
|
29
34
|
} from "./types.ts";
|
|
@@ -52,6 +57,10 @@ const DEFAULT_SERVER = "https://embed.foxglove.dev/";
|
|
|
52
57
|
|
|
53
58
|
const DEFAULT_LAYOUT_STORAGE_KEY = "foxglove.default-layout";
|
|
54
59
|
|
|
60
|
+
const PENDING_CAPABILITIES: FoxgloveViewerCapabilities = Object.freeze({
|
|
61
|
+
parentOwnedLiveTransport: "pending",
|
|
62
|
+
});
|
|
63
|
+
|
|
55
64
|
type PromiseWithResolvers<T> = {
|
|
56
65
|
promise: Promise<T>;
|
|
57
66
|
resolve: (value: T) => void;
|
|
@@ -156,7 +165,7 @@ export class FoxgloveViewer extends TypedEventTarget {
|
|
|
156
165
|
#isReadyPromise: PromiseWithResolvers<void>;
|
|
157
166
|
/** Latest commands that have been queued to be executed when the frame is ready. */
|
|
158
167
|
#latestCommands: {
|
|
159
|
-
dataSource:
|
|
168
|
+
dataSource: DataSource | undefined;
|
|
160
169
|
selectLayout: IframeSelectLayoutCommand | undefined;
|
|
161
170
|
extension: Array<IframeInstallExtensionCommand>;
|
|
162
171
|
keybindings: IframeSetKeybindingConfigCommand | undefined;
|
|
@@ -175,6 +184,8 @@ export class FoxgloveViewer extends TypedEventTarget {
|
|
|
175
184
|
#customKeybindingHandlers = new Map<string, () => void>();
|
|
176
185
|
/** Whether the frame has been destroyed. */
|
|
177
186
|
#isDestroyed = false;
|
|
187
|
+
#capabilities = PENDING_CAPABILITIES;
|
|
188
|
+
#parentTransportBridge?: ParentTransportBridge;
|
|
178
189
|
|
|
179
190
|
public constructor(options: FoxgloveViewerOptions) {
|
|
180
191
|
super();
|
|
@@ -248,10 +259,10 @@ export class FoxgloveViewer extends TypedEventTarget {
|
|
|
248
259
|
|
|
249
260
|
/** Set the data source of the Foxglove app. */
|
|
250
261
|
public setDataSource(dataSource: DataSource): void {
|
|
251
|
-
this.#
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
}
|
|
262
|
+
this.#latestCommands.dataSource = dataSource;
|
|
263
|
+
if (this.#isReady) {
|
|
264
|
+
this.#postDataSource(dataSource);
|
|
265
|
+
}
|
|
255
266
|
}
|
|
256
267
|
|
|
257
268
|
/**
|
|
@@ -483,9 +494,16 @@ export class FoxgloveViewer extends TypedEventTarget {
|
|
|
483
494
|
return this.#isReady;
|
|
484
495
|
}
|
|
485
496
|
|
|
497
|
+
/** Features the current iframe makes available to this embedding host. */
|
|
498
|
+
public getCapabilities(): FoxgloveViewerCapabilities {
|
|
499
|
+
return this.#capabilities;
|
|
500
|
+
}
|
|
501
|
+
|
|
486
502
|
/** Destroy the frame. This will remove the iframe from the DOM and stop listening for messages. */
|
|
487
503
|
public destroy(): void {
|
|
488
504
|
this.#isDestroyed = true;
|
|
505
|
+
this.#parentTransportBridge?.close();
|
|
506
|
+
this.#parentTransportBridge = undefined;
|
|
489
507
|
this.#iframe.remove();
|
|
490
508
|
window.removeEventListener("message", this.#handleMessage);
|
|
491
509
|
}
|
|
@@ -556,6 +574,7 @@ export class FoxgloveViewer extends TypedEventTarget {
|
|
|
556
574
|
IframeCommand,
|
|
557
575
|
| IframeHandshakeAckCommand
|
|
558
576
|
| IframeOriginAckCommand
|
|
577
|
+
| IframeSetDatasourceCommand
|
|
559
578
|
| IframeSetLayoutCommand
|
|
560
579
|
| IframeGetLayoutCommand
|
|
561
580
|
| IframeSeekPlaybackCommand
|
|
@@ -570,9 +589,6 @@ export class FoxgloveViewer extends TypedEventTarget {
|
|
|
570
589
|
case "install-extension":
|
|
571
590
|
this.#latestCommands.extension.push(command);
|
|
572
591
|
break;
|
|
573
|
-
case "set-data-source":
|
|
574
|
-
this.#latestCommands.dataSource = command;
|
|
575
|
-
break;
|
|
576
592
|
case "select-layout":
|
|
577
593
|
this.#latestCommands.selectLayout = command;
|
|
578
594
|
break;
|
|
@@ -587,7 +603,7 @@ export class FoxgloveViewer extends TypedEventTarget {
|
|
|
587
603
|
}
|
|
588
604
|
|
|
589
605
|
/** Post a command message to the Foxglove app. */
|
|
590
|
-
#postCommand(message: IframeCommand): void {
|
|
606
|
+
#postCommand(message: IframeCommand, transfer: Transferable[] = []): void {
|
|
591
607
|
if (this.#isDestroyed) {
|
|
592
608
|
console.warn("[FoxgloveViewer] Unable to post command. Frame has been destroyed.");
|
|
593
609
|
return;
|
|
@@ -595,7 +611,47 @@ export class FoxgloveViewer extends TypedEventTarget {
|
|
|
595
611
|
|
|
596
612
|
assert(this.#iframe.contentWindow, "Invariant: iframe should be loaded.");
|
|
597
613
|
|
|
598
|
-
this.#iframe.contentWindow.postMessage(message, this.#server.origin);
|
|
614
|
+
this.#iframe.contentWindow.postMessage(message, this.#server.origin, transfer);
|
|
615
|
+
}
|
|
616
|
+
|
|
617
|
+
#postDataSource(dataSource: DataSource): void {
|
|
618
|
+
const prepared = this.#replaceParentTransport(dataSource);
|
|
619
|
+
if (prepared.dataSource == undefined) {
|
|
620
|
+
return;
|
|
621
|
+
}
|
|
622
|
+
this.#postCommand(
|
|
623
|
+
{
|
|
624
|
+
type: "set-data-source",
|
|
625
|
+
payload: prepared.dataSource,
|
|
626
|
+
parentTransport: prepared.parentTransport,
|
|
627
|
+
},
|
|
628
|
+
prepared.parentTransport ? [prepared.parentTransport.port] : [],
|
|
629
|
+
);
|
|
630
|
+
}
|
|
631
|
+
|
|
632
|
+
#replaceParentTransport(dataSource: DataSource): {
|
|
633
|
+
dataSource?: IframeSetDatasourceCommand["payload"];
|
|
634
|
+
parentTransport?: IframeParentTransport;
|
|
635
|
+
} {
|
|
636
|
+
this.#parentTransportBridge?.close();
|
|
637
|
+
this.#parentTransportBridge = undefined;
|
|
638
|
+
|
|
639
|
+
if (!isParentLiveSource(dataSource)) {
|
|
640
|
+
return { dataSource };
|
|
641
|
+
}
|
|
642
|
+
if (this.#capabilities.parentOwnedLiveTransport !== "available") {
|
|
643
|
+
this.#dispatchError(
|
|
644
|
+
"[FoxgloveViewer] Parent-owned live transport is not supported by this iframe.",
|
|
645
|
+
);
|
|
646
|
+
return {};
|
|
647
|
+
}
|
|
648
|
+
const { transport, ...source } = dataSource;
|
|
649
|
+
const bridge = new ParentTransportBridge(transport);
|
|
650
|
+
this.#parentTransportBridge = bridge;
|
|
651
|
+
return {
|
|
652
|
+
dataSource: { ...source, transport: "parent" },
|
|
653
|
+
parentTransport: { id: crypto.randomUUID(), port: bridge.port },
|
|
654
|
+
};
|
|
599
655
|
}
|
|
600
656
|
|
|
601
657
|
/** Handle a message from the Foxglove app. */
|
|
@@ -616,33 +672,51 @@ export class FoxgloveViewer extends TypedEventTarget {
|
|
|
616
672
|
this.#postCommand({ type: "origin-ack" });
|
|
617
673
|
break;
|
|
618
674
|
|
|
619
|
-
case "foxglove-handshake-request":
|
|
675
|
+
case "foxglove-handshake-request": {
|
|
620
676
|
this.#isReady = true;
|
|
621
677
|
this.#isReadyPromise.resolve();
|
|
678
|
+
this.#capabilities = Object.freeze({
|
|
679
|
+
parentOwnedLiveTransport:
|
|
680
|
+
message.data.capabilities?.includes(IframeCapability.parentOwnedLiveTransport) === true
|
|
681
|
+
? "available"
|
|
682
|
+
: "unavailable",
|
|
683
|
+
});
|
|
622
684
|
|
|
623
|
-
this.#
|
|
624
|
-
|
|
625
|
-
|
|
626
|
-
|
|
627
|
-
|
|
628
|
-
|
|
629
|
-
|
|
630
|
-
|
|
685
|
+
const initialDataSource = this.#latestCommands.dataSource;
|
|
686
|
+
const prepared = initialDataSource
|
|
687
|
+
? this.#replaceParentTransport(initialDataSource)
|
|
688
|
+
: undefined;
|
|
689
|
+
this.#postCommand(
|
|
690
|
+
{
|
|
691
|
+
type: "handshake-ack",
|
|
692
|
+
payload: {
|
|
693
|
+
orgSlug: this.#orgSlug,
|
|
694
|
+
initialDataSource: prepared?.dataSource,
|
|
695
|
+
initialParentTransport: prepared?.parentTransport,
|
|
696
|
+
initialLayoutParams: this.#latestCommands.selectLayout?.payload,
|
|
697
|
+
initialExtensions: this.#latestCommands.extension.flatMap(
|
|
698
|
+
(command) => command.payload,
|
|
699
|
+
),
|
|
700
|
+
initialKeybindingsConfig: this.#latestCommands.keybindings?.payload,
|
|
701
|
+
},
|
|
631
702
|
},
|
|
632
|
-
|
|
703
|
+
prepared?.parentTransport ? [prepared.parentTransport.port] : [],
|
|
704
|
+
);
|
|
705
|
+
this.dispatchEvent(
|
|
706
|
+
new CustomEvent("capabilities" satisfies FoxgloveViewerEventType, {
|
|
707
|
+
detail: this.#capabilities,
|
|
708
|
+
}),
|
|
709
|
+
);
|
|
633
710
|
|
|
634
711
|
break;
|
|
712
|
+
}
|
|
635
713
|
|
|
636
714
|
case "foxglove-handshake-complete":
|
|
637
715
|
this.dispatchEvent(new Event("ready" satisfies FoxgloveViewerEventType));
|
|
638
716
|
break;
|
|
639
717
|
|
|
640
718
|
case "foxglove-error":
|
|
641
|
-
this.
|
|
642
|
-
new CustomEvent("error" satisfies FoxgloveViewerEventType, {
|
|
643
|
-
detail: message.data.payload,
|
|
644
|
-
}),
|
|
645
|
-
);
|
|
719
|
+
this.#dispatchError(message.data.payload);
|
|
646
720
|
break;
|
|
647
721
|
|
|
648
722
|
case "foxglove-layout-data":
|
|
@@ -665,6 +739,14 @@ export class FoxgloveViewer extends TypedEventTarget {
|
|
|
665
739
|
break;
|
|
666
740
|
}
|
|
667
741
|
};
|
|
742
|
+
|
|
743
|
+
#dispatchError(message: string): void {
|
|
744
|
+
this.dispatchEvent(
|
|
745
|
+
new CustomEvent("error" satisfies FoxgloveViewerEventType, {
|
|
746
|
+
detail: message,
|
|
747
|
+
}),
|
|
748
|
+
);
|
|
749
|
+
}
|
|
668
750
|
}
|
|
669
751
|
|
|
670
752
|
/**
|
|
@@ -678,6 +760,10 @@ function fromSec(value: number): Time {
|
|
|
678
760
|
return { sec, nsec };
|
|
679
761
|
}
|
|
680
762
|
|
|
763
|
+
function isParentLiveSource(dataSource: DataSource): dataSource is ParentLiveSource {
|
|
764
|
+
return dataSource.type === "live" && typeof dataSource.transport === "function";
|
|
765
|
+
}
|
|
766
|
+
|
|
681
767
|
function assert(
|
|
682
768
|
condition: unknown,
|
|
683
769
|
message: string = "no additional info provided",
|
|
@@ -0,0 +1,91 @@
|
|
|
1
|
+
import { ParentTransportBridge } from "./ParentTransportBridge.ts";
|
|
2
|
+
import type { ParentTransportFactoryArgs } from "./types.ts";
|
|
3
|
+
|
|
4
|
+
async function nextMessage<T>(port: MessagePort): Promise<T> {
|
|
5
|
+
return await new Promise((resolve) => {
|
|
6
|
+
port.addEventListener(
|
|
7
|
+
"message",
|
|
8
|
+
(event: MessageEvent<T>) => {
|
|
9
|
+
resolve(event.data);
|
|
10
|
+
},
|
|
11
|
+
{ once: true },
|
|
12
|
+
);
|
|
13
|
+
port.start();
|
|
14
|
+
});
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
describe("ParentTransportBridge", () => {
|
|
18
|
+
it("carries text and binary Foxglove frames in both directions", async () => {
|
|
19
|
+
const send = jest.fn();
|
|
20
|
+
const close = jest.fn();
|
|
21
|
+
let resolveFactoryArgs!: (args: ParentTransportFactoryArgs) => void;
|
|
22
|
+
const factoryArgsPromise = new Promise<ParentTransportFactoryArgs>((resolve) => {
|
|
23
|
+
resolveFactoryArgs = resolve;
|
|
24
|
+
});
|
|
25
|
+
const bridge = new ParentTransportBridge((args) => {
|
|
26
|
+
resolveFactoryArgs(args);
|
|
27
|
+
return { send, close };
|
|
28
|
+
});
|
|
29
|
+
|
|
30
|
+
bridge.port.postMessage({
|
|
31
|
+
type: "connect",
|
|
32
|
+
url: "virtual://robot",
|
|
33
|
+
protocols: ["foxglove.websocket.v1"],
|
|
34
|
+
});
|
|
35
|
+
const factoryArgs = await factoryArgsPromise;
|
|
36
|
+
|
|
37
|
+
expect(factoryArgs).toMatchObject({
|
|
38
|
+
url: "virtual://robot",
|
|
39
|
+
protocols: ["foxglove.websocket.v1"],
|
|
40
|
+
});
|
|
41
|
+
|
|
42
|
+
const openMessage = nextMessage(bridge.port);
|
|
43
|
+
factoryArgs.onOpen("foxglove.websocket.v1");
|
|
44
|
+
await expect(openMessage).resolves.toEqual({
|
|
45
|
+
type: "open",
|
|
46
|
+
protocol: "foxglove.websocket.v1",
|
|
47
|
+
});
|
|
48
|
+
|
|
49
|
+
bridge.port.postMessage({ type: "send", data: "subscribe" });
|
|
50
|
+
await new Promise((resolve) => setTimeout(resolve, 0));
|
|
51
|
+
expect(send).toHaveBeenCalledWith("subscribe");
|
|
52
|
+
|
|
53
|
+
const binaryMessage = nextMessage<{ type: string; data: ArrayBuffer }>(bridge.port);
|
|
54
|
+
factoryArgs.onMessage(new Uint8Array([1, 2, 3]));
|
|
55
|
+
const received = await binaryMessage;
|
|
56
|
+
expect(received.type).toBe("message");
|
|
57
|
+
expect([...new Uint8Array(received.data)]).toEqual([1, 2, 3]);
|
|
58
|
+
|
|
59
|
+
const closeMessage = nextMessage(bridge.port);
|
|
60
|
+
bridge.port.postMessage({ type: "close" });
|
|
61
|
+
await expect(closeMessage).resolves.toMatchObject({ type: "close", code: 1000 });
|
|
62
|
+
expect(close).toHaveBeenCalledTimes(1);
|
|
63
|
+
|
|
64
|
+
bridge.close();
|
|
65
|
+
expect(close).toHaveBeenCalledTimes(1);
|
|
66
|
+
});
|
|
67
|
+
|
|
68
|
+
it("creates a fresh parent connection when the viewer reconnects", async () => {
|
|
69
|
+
const connections: { close: jest.Mock }[] = [];
|
|
70
|
+
let resolveSecondConnection!: () => void;
|
|
71
|
+
const secondConnection = new Promise<void>((resolve) => {
|
|
72
|
+
resolveSecondConnection = resolve;
|
|
73
|
+
});
|
|
74
|
+
const bridge = new ParentTransportBridge(() => {
|
|
75
|
+
const connection = { send: jest.fn(), close: jest.fn() };
|
|
76
|
+
connections.push(connection);
|
|
77
|
+
if (connections.length === 2) {
|
|
78
|
+
resolveSecondConnection();
|
|
79
|
+
}
|
|
80
|
+
return connection;
|
|
81
|
+
});
|
|
82
|
+
|
|
83
|
+
bridge.port.postMessage({ type: "connect", url: "virtual://robot", protocols: [] });
|
|
84
|
+
bridge.port.postMessage({ type: "connect", url: "virtual://robot", protocols: [] });
|
|
85
|
+
await secondConnection;
|
|
86
|
+
|
|
87
|
+
expect(connections).toHaveLength(2);
|
|
88
|
+
expect(connections[0]!.close).toHaveBeenCalledTimes(1);
|
|
89
|
+
bridge.close();
|
|
90
|
+
});
|
|
91
|
+
});
|