@foxglove/embed 0.68.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.
@@ -0,0 +1,111 @@
1
+ import type { ParentTransportPortMessage, ViewerTransportPortMessage } from "./commands.ts";
2
+ import type { ParentTransport, ParentTransportFactory } from "./types.ts";
3
+
4
+ function errorMessage(err: unknown): string {
5
+ return err instanceof Error ? err.message : "Unknown parent transport error";
6
+ }
7
+
8
+ function transferableData(data: string | ArrayBuffer | ArrayBufferView<ArrayBuffer>): {
9
+ data: string | ArrayBuffer;
10
+ transfer: Transferable[];
11
+ } {
12
+ if (typeof data === "string") {
13
+ return { data, transfer: [] };
14
+ }
15
+ if (data instanceof ArrayBuffer) {
16
+ return { data, transfer: [data] };
17
+ }
18
+ const buffer = data.buffer.slice(data.byteOffset, data.byteOffset + data.byteLength);
19
+ return { data: buffer, transfer: [buffer] };
20
+ }
21
+
22
+ /** Owns the parent half of one iframe transport channel. */
23
+ export class ParentTransportBridge {
24
+ public readonly port: MessagePort;
25
+ #parentPort: MessagePort;
26
+ #connection?: ParentTransport;
27
+ #factory: ParentTransportFactory;
28
+ #closed = false;
29
+ #connectionGeneration = 0;
30
+
31
+ public constructor(factory: ParentTransportFactory) {
32
+ this.#factory = factory;
33
+ const channel = new MessageChannel();
34
+ this.port = channel.port2;
35
+ channel.port1.onmessage = this.#handleMessage;
36
+ channel.port1.start();
37
+ this.#parentPort = channel.port1;
38
+ }
39
+
40
+ public close(): void {
41
+ if (this.#closed) {
42
+ return;
43
+ }
44
+ this.#closed = true;
45
+ this.#connectionGeneration++;
46
+ this.#connection?.close();
47
+ this.#connection = undefined;
48
+ this.#parentPort.close();
49
+ }
50
+
51
+ #handleMessage = (event: MessageEvent<ParentTransportPortMessage>): void => {
52
+ if (this.#closed) {
53
+ return;
54
+ }
55
+ switch (event.data.type) {
56
+ case "connect":
57
+ this.#connect(event.data.url, event.data.protocols);
58
+ break;
59
+ case "send":
60
+ this.#connection?.send(event.data.data);
61
+ break;
62
+ case "close":
63
+ this.#connectionGeneration++;
64
+ this.#connection?.close();
65
+ this.#connection = undefined;
66
+ this.#parentPort.postMessage({
67
+ type: "close",
68
+ code: 1000,
69
+ reason: "Viewer closed the connection",
70
+ wasClean: true,
71
+ } satisfies ViewerTransportPortMessage);
72
+ break;
73
+ }
74
+ };
75
+
76
+ #connect(url: string, protocols: string[]): void {
77
+ this.#connectionGeneration++;
78
+ const generation = this.#connectionGeneration;
79
+ this.#connection?.close();
80
+ this.#connection = undefined;
81
+
82
+ const postIfCurrent = (message: ViewerTransportPortMessage, transfer: Transferable[] = []) => {
83
+ if (!this.#closed && generation === this.#connectionGeneration) {
84
+ this.#parentPort.postMessage(message, transfer);
85
+ }
86
+ };
87
+
88
+ try {
89
+ this.#connection = this.#factory({
90
+ url,
91
+ protocols,
92
+ onOpen: (protocol) => {
93
+ postIfCurrent({ type: "open", protocol });
94
+ },
95
+ onMessage: (data) => {
96
+ const transferable = transferableData(data);
97
+ postIfCurrent({ type: "message", data: transferable.data }, transferable.transfer);
98
+ },
99
+ onError: (error) => {
100
+ postIfCurrent({ type: "error", message: error.message });
101
+ },
102
+ onClose: (event) => {
103
+ postIfCurrent({ type: "close", ...event });
104
+ },
105
+ });
106
+ } catch (err) {
107
+ postIfCurrent({ type: "error", message: errorMessage(err) });
108
+ postIfCurrent({ type: "close", reason: "Parent transport factory failed" });
109
+ }
110
+ }
111
+ }
package/src/commands.ts CHANGED
@@ -8,11 +8,22 @@ import type {
8
8
  DataSource,
9
9
  FoxgloveCommandId,
10
10
  OpaqueLayoutData,
11
+ ParentLiveSource,
11
12
  SelectLayoutParams,
12
13
  KeybindingBase,
13
14
  Time,
14
15
  } from "./types.ts";
15
16
 
17
+ export const IframeCapability = {
18
+ parentOwnedLiveTransport: "parent-owned-live-transport",
19
+ } as const;
20
+
21
+ export type IframeCapability = (typeof IframeCapability)[keyof typeof IframeCapability];
22
+
23
+ export type IframeDataSource =
24
+ | Exclude<DataSource, ParentLiveSource>
25
+ | (Omit<ParentLiveSource, "transport"> & { transport: "parent" });
26
+
16
27
  /**
17
28
  * Override or disable a built-in viewer shortcut.
18
29
  *
@@ -54,7 +65,17 @@ export type IframeOriginAckCommand = {
54
65
 
55
66
  export type IframeSetDatasourceCommand = {
56
67
  type: "set-data-source";
57
- payload: DataSource;
68
+ payload: IframeDataSource;
69
+ /**
70
+ * Transferable metadata for a parent-owned live source. This stays outside `payload` so the
71
+ * payload remains the data source shape understood by iframe versions that predate this field.
72
+ */
73
+ parentTransport?: IframeParentTransport;
74
+ };
75
+
76
+ export type IframeParentTransport = {
77
+ id: string;
78
+ port: MessagePort;
58
79
  };
59
80
 
60
81
  export type IframeSetLayoutCommand = {
@@ -105,7 +126,8 @@ export type IframeHandshakeAckCommand = {
105
126
  type: "handshake-ack";
106
127
  payload: {
107
128
  orgSlug?: string;
108
- initialDataSource?: DataSource;
129
+ initialDataSource?: IframeDataSource;
130
+ initialParentTransport?: IframeParentTransport;
109
131
  initialLayout?: OpaqueLayoutData;
110
132
  initialLayoutParams?: SelectLayoutParams;
111
133
  initialExtensions: Array<string | File>;
@@ -129,8 +151,19 @@ export type IframeCommand =
129
151
 
130
152
  export type IframeEvent =
131
153
  | { type: "foxglove-origin-request" }
132
- | { type: "foxglove-handshake-request" }
154
+ | { type: "foxglove-handshake-request"; capabilities?: IframeCapability[] }
133
155
  | { type: "foxglove-handshake-complete" }
134
156
  | { type: "foxglove-error"; payload: string }
135
157
  | { type: "foxglove-layout-data"; payload: OpaqueLayoutData }
136
158
  | { type: "foxglove-keybinding-triggered"; payload: { keybindingId: string } };
159
+
160
+ export type ParentTransportPortMessage =
161
+ | { type: "connect"; url: string; protocols: string[] }
162
+ | { type: "send"; data: string | ArrayBuffer }
163
+ | { type: "close" };
164
+
165
+ export type ViewerTransportPortMessage =
166
+ | { type: "open"; protocol?: string }
167
+ | { type: "message"; data: string | ArrayBuffer }
168
+ | { type: "error"; message: string }
169
+ | { type: "close"; code?: number; reason?: string; wasClean?: boolean };
package/src/types.ts CHANGED
@@ -66,12 +66,61 @@ export type DeviceSource =
66
66
 
67
67
  export type LiveSourceProtocol = "foxglove-websocket" | "rosbridge-websocket";
68
68
 
69
- export type LiveSource = {
69
+ type IframeLiveSource = {
70
70
  type: "live";
71
71
  protocol: LiveSourceProtocol;
72
72
  url: string;
73
+ /** Opens the connection inside the embedded Foxglove frame. This is the default. */
74
+ transport?: "iframe";
73
75
  };
74
76
 
77
+ /**
78
+ * A Foxglove WebSocket Protocol source whose connection is owned by the embedding parent page.
79
+ * Frames are tunneled to the viewer over a dedicated `MessageChannel`.
80
+ */
81
+ export type ParentLiveSource = {
82
+ type: "live";
83
+ protocol: "foxglove-websocket";
84
+ url: string;
85
+ /** Creates each parent-owned connection attempt for this source. */
86
+ transport: ParentTransportFactory;
87
+ };
88
+
89
+ export type LiveSource = IframeLiveSource | ParentLiveSource;
90
+
91
+ export type ParentTransportCloseEvent = {
92
+ code?: number;
93
+ reason?: string;
94
+ wasClean?: boolean;
95
+ };
96
+
97
+ export type ParentTransportFactoryArgs = {
98
+ /** The URL supplied on the parent live data source. It may be a label for virtual transports. */
99
+ url: string;
100
+ /** Foxglove WebSocket subprotocols supported by the embedded viewer. */
101
+ protocols: readonly string[];
102
+ /** Signal that the transport is ready to exchange Foxglove protocol frames. */
103
+ onOpen: (protocol?: string) => void;
104
+ /** Deliver a Foxglove WebSocket Protocol text or binary frame to the viewer. */
105
+ onMessage: (data: string | ArrayBuffer | ArrayBufferView<ArrayBuffer>) => void;
106
+ /** Report a transport error to the viewer. */
107
+ onError: (error: Error) => void;
108
+ /** Signal that the current connection closed. */
109
+ onClose: (event?: ParentTransportCloseEvent) => void;
110
+ };
111
+
112
+ /** A parent-owned connection that receives Foxglove client frames from the embedded viewer. */
113
+ export type ParentTransport = {
114
+ send: (data: string | BufferSource) => void;
115
+ close: () => void;
116
+ };
117
+
118
+ /**
119
+ * Creates one parent-owned connection attempt. This may wrap a real WebSocket, translate another
120
+ * protocol, or implement an entirely virtual Foxglove protocol server.
121
+ */
122
+ export type ParentTransportFactory = (args: ParentTransportFactoryArgs) => ParentTransport;
123
+
75
124
  export type RemoteFileSource = {
76
125
  type: "remote-file";
77
126
  urls: string[];
@@ -97,9 +146,18 @@ export type DataSource =
97
146
  | RemoteFileSource
98
147
  | RemoteAccessSource;
99
148
 
149
+ export type FoxgloveViewerCapabilityStatus = "pending" | "available" | "unavailable";
150
+
151
+ /** Features the iframe currently makes available to its embedding host. */
152
+ export type FoxgloveViewerCapabilities = Readonly<{
153
+ /** Whether the iframe can accept a parent-owned Foxglove WebSocket live transport. */
154
+ parentOwnedLiveTransport: FoxgloveViewerCapabilityStatus;
155
+ }>;
156
+
100
157
  export type FoxgloveViewerEventMap = {
101
158
  ready: Event;
102
159
  error: CustomEvent<string>;
160
+ capabilities: CustomEvent<FoxgloveViewerCapabilities>;
103
161
  };
104
162
 
105
163
  /**