@frockbot/applet-sdk 0.3.30 → 0.3.32
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/package.json +1 -1
- package/src/client/index.ts +11 -0
- package/src/client/transport.ts +13 -5
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@frockbot/applet-sdk",
|
|
3
|
-
"version": "0.3.
|
|
3
|
+
"version": "0.3.32",
|
|
4
4
|
"private": false,
|
|
5
5
|
"type": "module",
|
|
6
6
|
"description": "Authoring SDK for FrockBot Applets: schema-first Durable Object server, TanStack DB client, component kit, linter, and the `applet` CLI.",
|
package/src/client/index.ts
CHANGED
|
@@ -123,6 +123,9 @@ function decodeHostInit(data: unknown): AppletHostInitV1 | undefined {
|
|
|
123
123
|
socketUrl: value.socketUrl,
|
|
124
124
|
token: value.token,
|
|
125
125
|
generationId: value.generationId,
|
|
126
|
+
...(value.tokenTransport === "subprotocol-v1"
|
|
127
|
+
? { tokenTransport: "subprotocol-v1" as const }
|
|
128
|
+
: {}),
|
|
126
129
|
},
|
|
127
130
|
};
|
|
128
131
|
}
|
|
@@ -178,6 +181,14 @@ export function createApplet<TServer extends { tables: TablesShape }>(
|
|
|
178
181
|
lastInit = init.applet;
|
|
179
182
|
transport.connect(init.applet);
|
|
180
183
|
});
|
|
184
|
+
window.parent.postMessage(
|
|
185
|
+
{
|
|
186
|
+
schemaVersion: 1,
|
|
187
|
+
type: "applet/ready",
|
|
188
|
+
tokenTransport: "subprotocol-v1",
|
|
189
|
+
},
|
|
190
|
+
"*",
|
|
191
|
+
);
|
|
181
192
|
// Leave with a close frame rather than a dropped connection: the server
|
|
182
193
|
// then sees a 1000, not a 1006 it has to discover on its next write. A
|
|
183
194
|
// page restored from the back/forward cache reconnects with the same
|
package/src/client/transport.ts
CHANGED
|
@@ -22,6 +22,7 @@ export interface AppletInitV1 {
|
|
|
22
22
|
/** Short-lived viewer token; appended as `?token=`. */
|
|
23
23
|
token: string;
|
|
24
24
|
generationId: string;
|
|
25
|
+
tokenTransport?: "subprotocol-v1";
|
|
25
26
|
}
|
|
26
27
|
|
|
27
28
|
export type AppletStatus =
|
|
@@ -43,7 +44,10 @@ export interface AppletSocket {
|
|
|
43
44
|
onerror: ((event: unknown) => void) | null;
|
|
44
45
|
}
|
|
45
46
|
|
|
46
|
-
export type AppletSocketFactory = (
|
|
47
|
+
export type AppletSocketFactory = (
|
|
48
|
+
url: string,
|
|
49
|
+
protocols?: string[],
|
|
50
|
+
) => AppletSocket;
|
|
47
51
|
|
|
48
52
|
/** What a TanStack DB collection hands the transport for one table. */
|
|
49
53
|
export interface AppletTableSink {
|
|
@@ -69,8 +73,8 @@ export interface AppletTransportOptions {
|
|
|
69
73
|
|
|
70
74
|
class RejectedMutation extends Error {}
|
|
71
75
|
|
|
72
|
-
function defaultSocketFactory(url: string): AppletSocket {
|
|
73
|
-
return new WebSocket(url) as unknown as AppletSocket;
|
|
76
|
+
function defaultSocketFactory(url: string, protocols?: string[]): AppletSocket {
|
|
77
|
+
return new WebSocket(url, protocols) as unknown as AppletSocket;
|
|
74
78
|
}
|
|
75
79
|
|
|
76
80
|
export class AppletTransport {
|
|
@@ -185,8 +189,12 @@ export class AppletTransport {
|
|
|
185
189
|
status: this.#attempt === 0 ? "connecting" : "reconnecting",
|
|
186
190
|
});
|
|
187
191
|
const url = new URL(this.#init.socketUrl);
|
|
188
|
-
|
|
189
|
-
|
|
192
|
+
const protocols =
|
|
193
|
+
this.#init.tokenTransport === "subprotocol-v1"
|
|
194
|
+
? ["frockbot.applet.v1", `frockbot.viewer.${this.#init.token}`]
|
|
195
|
+
: undefined;
|
|
196
|
+
if (!protocols) url.searchParams.set("token", this.#init.token);
|
|
197
|
+
const socket = this.#options.socketFactory(url.toString(), protocols);
|
|
190
198
|
this.#socket = socket;
|
|
191
199
|
socket.onopen = () => {
|
|
192
200
|
if (this.#currentSocketId === id) this.#handshake();
|