@air-jam/sdk 0.1.3 → 0.9.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/LICENSE +21 -0
- package/README.md +627 -57
- package/dist/agent-tooling-BSMGBix9.d.cts +123 -0
- package/dist/agent-tooling-BSMGBix9.d.ts +123 -0
- package/dist/agent-tooling.cjs +83 -0
- package/dist/agent-tooling.cjs.map +1 -0
- package/dist/agent-tooling.d.cts +2 -0
- package/dist/agent-tooling.d.ts +2 -0
- package/dist/agent-tooling.js +17 -0
- package/dist/agent-tooling.js.map +1 -0
- package/dist/arcade/bridge/controller.cjs +749 -0
- package/dist/arcade/bridge/controller.cjs.map +1 -0
- package/dist/arcade/bridge/controller.d.cts +85 -0
- package/dist/arcade/bridge/controller.d.ts +85 -0
- package/dist/arcade/bridge/controller.js +33 -0
- package/dist/arcade/bridge/controller.js.map +1 -0
- package/dist/arcade/bridge/host.cjs +421 -0
- package/dist/arcade/bridge/host.cjs.map +1 -0
- package/dist/arcade/bridge/host.d.cts +73 -0
- package/dist/arcade/bridge/host.d.ts +73 -0
- package/dist/arcade/bridge/host.js +25 -0
- package/dist/arcade/bridge/host.js.map +1 -0
- package/dist/arcade/bridge/iframe.cjs +453 -0
- package/dist/arcade/bridge/iframe.cjs.map +1 -0
- package/dist/arcade/bridge/iframe.d.cts +80 -0
- package/dist/arcade/bridge/iframe.d.ts +80 -0
- package/dist/arcade/bridge/iframe.js +28 -0
- package/dist/arcade/bridge/iframe.js.map +1 -0
- package/dist/arcade/bridge.cjs +1067 -0
- package/dist/arcade/bridge.cjs.map +1 -0
- package/dist/arcade/bridge.d.cts +11 -0
- package/dist/arcade/bridge.d.ts +11 -0
- package/dist/arcade/bridge.js +76 -0
- package/dist/arcade/bridge.js.map +1 -0
- package/dist/arcade/host.cjs +488 -0
- package/dist/arcade/host.cjs.map +1 -0
- package/dist/arcade/host.d.cts +19 -0
- package/dist/arcade/host.d.ts +19 -0
- package/dist/arcade/host.js +13 -0
- package/dist/arcade/host.js.map +1 -0
- package/dist/arcade/surface.cjs +113 -0
- package/dist/arcade/surface.cjs.map +1 -0
- package/dist/arcade/surface.d.cts +14 -0
- package/dist/arcade/surface.d.ts +14 -0
- package/dist/arcade/surface.js +27 -0
- package/dist/arcade/surface.js.map +1 -0
- package/dist/arcade/url.cjs +363 -0
- package/dist/arcade/url.cjs.map +1 -0
- package/dist/arcade/url.d.cts +68 -0
- package/dist/arcade/url.d.ts +68 -0
- package/dist/arcade/url.js +28 -0
- package/dist/arcade/url.js.map +1 -0
- package/dist/arcade-runtime-url-BW-MUAOY.d.cts +28 -0
- package/dist/arcade-runtime-url-l8qj01Pg.d.ts +28 -0
- package/dist/arcade-surface-identity-CQxDpn_w.d.cts +16 -0
- package/dist/arcade-surface-identity-CQxDpn_w.d.ts +16 -0
- package/dist/arcade.cjs +1565 -0
- package/dist/arcade.cjs.map +1 -0
- package/dist/arcade.d.cts +18 -0
- package/dist/arcade.d.ts +18 -0
- package/dist/arcade.js +122 -0
- package/dist/arcade.js.map +1 -0
- package/dist/capabilities.cjs +39 -0
- package/dist/capabilities.cjs.map +1 -0
- package/dist/capabilities.d.cts +31 -0
- package/dist/capabilities.d.ts +31 -0
- package/dist/capabilities.js +12 -0
- package/dist/capabilities.js.map +1 -0
- package/dist/chunk-2432OL7L.js +193 -0
- package/dist/chunk-2432OL7L.js.map +1 -0
- package/dist/chunk-2J65FXEP.js +41 -0
- package/dist/chunk-2J65FXEP.js.map +1 -0
- package/dist/chunk-32NYL7XI.js +539 -0
- package/dist/chunk-32NYL7XI.js.map +1 -0
- package/dist/chunk-3USU7GY2.js +54 -0
- package/dist/chunk-3USU7GY2.js.map +1 -0
- package/dist/chunk-BE6Z4SIT.js +65 -0
- package/dist/chunk-BE6Z4SIT.js.map +1 -0
- package/dist/chunk-CQUG24WX.js +249 -0
- package/dist/chunk-CQUG24WX.js.map +1 -0
- package/dist/chunk-D3X4MN4U.js +288 -0
- package/dist/chunk-D3X4MN4U.js.map +1 -0
- package/dist/chunk-EVZJA6RN.js +140 -0
- package/dist/chunk-EVZJA6RN.js.map +1 -0
- package/dist/chunk-HFWRZVZ3.js +1438 -0
- package/dist/chunk-HFWRZVZ3.js.map +1 -0
- package/dist/chunk-IKKDONQL.js +1 -0
- package/dist/chunk-IKKDONQL.js.map +1 -0
- package/dist/chunk-JTF47B56.js +1 -0
- package/dist/chunk-JTF47B56.js.map +1 -0
- package/dist/chunk-LIXPF4P6.js +22 -0
- package/dist/chunk-LIXPF4P6.js.map +1 -0
- package/dist/chunk-MKBASO6X.js +216 -0
- package/dist/chunk-MKBASO6X.js.map +1 -0
- package/dist/chunk-MPDMJIX5.js +1 -0
- package/dist/chunk-MPDMJIX5.js.map +1 -0
- package/dist/chunk-NCL6WJCL.js +91 -0
- package/dist/chunk-NCL6WJCL.js.map +1 -0
- package/dist/chunk-NU5W6VAV.js +126 -0
- package/dist/chunk-NU5W6VAV.js.map +1 -0
- package/dist/chunk-TGRTEWYV.js +195 -0
- package/dist/chunk-TGRTEWYV.js.map +1 -0
- package/dist/chunk-VDRNNGHX.js +184 -0
- package/dist/chunk-VDRNNGHX.js.map +1 -0
- package/dist/chunk-VN5LDWVN.js +32 -0
- package/dist/chunk-VN5LDWVN.js.map +1 -0
- package/dist/chunk-W2VK36RO.js +11 -0
- package/dist/chunk-W2VK36RO.js.map +1 -0
- package/dist/chunk-X24K52DG.js +12 -0
- package/dist/chunk-X24K52DG.js.map +1 -0
- package/dist/chunk-XLUWV45V.js +25 -0
- package/dist/chunk-XLUWV45V.js.map +1 -0
- package/dist/chunk-Y4RAPKKB.js +238 -0
- package/dist/chunk-Y4RAPKKB.js.map +1 -0
- package/dist/chunk-YXHH3MQT.js +230 -0
- package/dist/chunk-YXHH3MQT.js.map +1 -0
- package/dist/chunk-ZLPL2R3R.js +183 -0
- package/dist/chunk-ZLPL2R3R.js.map +1 -0
- package/dist/chunk-ZTQ5SP4H.js +9 -0
- package/dist/chunk-ZTQ5SP4H.js.map +1 -0
- package/dist/controller-Bod8YYr1.d.cts +172 -0
- package/dist/controller-DxVchfiA.d.ts +172 -0
- package/dist/dev-log-events-CVAM29hx.d.cts +123 -0
- package/dist/dev-log-events-CVAM29hx.d.ts +123 -0
- package/dist/handshake-CC_LrK5g.d.cts +31 -0
- package/dist/handshake-CC_LrK5g.d.ts +31 -0
- package/dist/host-8hmq_Sgc.d.cts +109 -0
- package/dist/host-Cw-N6e-5.d.ts +109 -0
- package/dist/index.cjs +8512 -3518
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +676 -1070
- package/dist/index.d.ts +676 -1070
- package/dist/index.js +4892 -3188
- package/dist/index.js.map +1 -1
- package/dist/metadata.cjs +114 -0
- package/dist/metadata.cjs.map +1 -0
- package/dist/metadata.d.cts +95 -0
- package/dist/metadata.d.ts +95 -0
- package/dist/metadata.js +83 -0
- package/dist/metadata.js.map +1 -0
- package/dist/notices-CnGaaS8B.d.ts +38 -0
- package/dist/notices-NyDM5Mtz.d.cts +38 -0
- package/dist/platform-machine.cjs +425 -0
- package/dist/platform-machine.cjs.map +1 -0
- package/dist/platform-machine.d.cts +1183 -0
- package/dist/platform-machine.d.ts +1183 -0
- package/dist/platform-machine.js +309 -0
- package/dist/platform-machine.js.map +1 -0
- package/dist/platform-settings-cuor3i11.d.cts +48 -0
- package/dist/platform-settings-cuor3i11.d.ts +48 -0
- package/dist/prefabs.cjs +36 -0
- package/dist/prefabs.cjs.map +1 -0
- package/dist/prefabs.d.cts +49 -0
- package/dist/prefabs.d.ts +49 -0
- package/dist/prefabs.js +8 -0
- package/dist/prefabs.js.map +1 -0
- package/dist/preview.cjs +2609 -0
- package/dist/preview.cjs.map +1 -0
- package/dist/preview.d.cts +155 -0
- package/dist/preview.d.ts +155 -0
- package/dist/preview.js +1671 -0
- package/dist/preview.js.map +1 -0
- package/dist/protocol.cjs +621 -54
- package/dist/protocol.cjs.map +1 -1
- package/dist/protocol.d.cts +76 -240
- package/dist/protocol.d.ts +76 -240
- package/dist/protocol.js +100 -2
- package/dist/protocol.js.map +1 -1
- package/dist/realtime-client-Cu8yVJFw.d.cts +15 -0
- package/dist/realtime-client-Cu8yVJFw.d.ts +15 -0
- package/dist/release.cjs +143 -0
- package/dist/release.cjs.map +1 -0
- package/dist/release.d.cts +64 -0
- package/dist/release.d.ts +64 -0
- package/dist/release.js +43 -0
- package/dist/release.js.map +1 -0
- package/dist/standard-match-phase-BucA5dYo.d.ts +44 -0
- package/dist/standard-match-phase-U2ra8J-0.d.cts +44 -0
- package/dist/sync-B6QsNrXz.d.cts +125 -0
- package/dist/sync-B6QsNrXz.d.ts +125 -0
- package/dist/ui.cjs +2827 -0
- package/dist/ui.cjs.map +1 -0
- package/dist/ui.d.cts +320 -0
- package/dist/ui.d.ts +320 -0
- package/dist/ui.js +1718 -0
- package/dist/ui.js.map +1 -0
- package/dist/url-policy-C89zxgw9.d.cts +9 -0
- package/dist/url-policy-C89zxgw9.d.ts +9 -0
- package/package.json +154 -13
- package/src/agent/agent-action.ts +304 -0
- package/src/agent/agent-contract.ts +180 -0
- package/src/agent-tooling.ts +20 -0
- package/src/arcade/bridge/controller.ts +15 -0
- package/src/arcade/bridge/host.ts +12 -0
- package/src/arcade/bridge/iframe.ts +61 -0
- package/src/arcade/bridge.ts +3 -0
- package/src/arcade/host.ts +1 -0
- package/src/arcade/surface.ts +4 -0
- package/src/arcade/url.ts +60 -0
- package/src/arcade.ts +4 -0
- package/src/audio/audio-manager.ts +162 -171
- package/src/audio/hooks.tsx +218 -97
- package/src/audio/index.ts +1 -1
- package/src/audio/music.tsx +139 -0
- package/src/capabilities/manifest.ts +71 -0
- package/src/capabilities.ts +12 -0
- package/src/components/connection-status-pill.tsx +55 -0
- package/src/components/controller-player-name-field.tsx +119 -0
- package/src/components/controller-primary-action.tsx +58 -0
- package/src/components/host-mute-button.tsx +49 -0
- package/src/components/join-qr-overlay.tsx +133 -0
- package/src/components/join-url-action-buttons.tsx +95 -0
- package/src/components/join-url-controls.tsx +58 -0
- package/src/components/join-url-field.tsx +52 -0
- package/src/components/lifecycle-action-group.tsx +129 -0
- package/src/components/player-avatar-strip.tsx +65 -0
- package/src/components/player-avatar.tsx +42 -14
- package/src/components/room-qr-code.tsx +125 -0
- package/src/components/runtime-shell-header.tsx +37 -0
- package/src/components/shell-classes.ts +19 -0
- package/src/components/surface-orientation-layout.ts +60 -0
- package/src/components/surface-viewport-layout.ts +268 -0
- package/src/components/surface-viewport.tsx +314 -0
- package/src/components/ui/button.tsx +4 -6
- package/src/components/ui/slider.tsx +1 -1
- package/src/components/volume-controls.tsx +65 -6
- package/src/context/air-jam-context.tsx +209 -141
- package/src/context/index.ts +2 -2
- package/src/context/session-providers.tsx +77 -0
- package/src/context/session-scope.ts +64 -0
- package/src/context/socket-manager.ts +6 -25
- package/src/contracts/v2/bridge.ts +261 -0
- package/src/contracts/v2/handshake.ts +40 -0
- package/src/contracts/v2/index.ts +2 -0
- package/src/dev/browser-log-sink.ts +912 -0
- package/src/diagnostics.ts +128 -0
- package/src/events.ts +7 -34
- package/src/hooks/internal/use-controller-runtime-api.ts +875 -0
- package/src/hooks/internal/use-host-runtime-api.ts +1051 -0
- package/src/hooks/internal/use-runtime-control-contract.ts +15 -0
- package/src/hooks/internal/use-runtime-inspection-contract.ts +16 -0
- package/src/hooks/internal/use-runtime-observability-contract.ts +24 -0
- package/src/hooks/use-air-jam-controller.ts +155 -475
- package/src/hooks/use-air-jam-host.ts +156 -485
- package/src/hooks/use-connection-status.ts +8 -0
- package/src/hooks/use-controller-lifecycle-intents.ts +27 -0
- package/src/hooks/use-controller-lifecycle-permissions.ts +36 -0
- package/src/hooks/use-controller-shell-status.ts +40 -0
- package/src/hooks/use-controller-tick.ts +65 -0
- package/src/hooks/use-controller-toasts.ts +116 -0
- package/src/hooks/use-get-input.ts +8 -5
- package/src/hooks/use-host-arcade-restore.ts +30 -0
- package/src/hooks/use-host-join-controls.ts +152 -0
- package/src/hooks/use-host-tick.ts +171 -0
- package/src/hooks/use-input-writer.ts +83 -0
- package/src/hooks/use-lifecycle-action-group-model.ts +98 -0
- package/src/hooks/use-players.ts +12 -0
- package/src/hooks/use-room.ts +22 -0
- package/src/hooks/use-send-signal.ts +6 -2
- package/src/index.ts +205 -31
- package/src/internal/input-manager.ts +195 -215
- package/src/lifecycle/index.ts +9 -0
- package/src/lifecycle/standard-match-phase.ts +32 -0
- package/src/metadata/manifest.ts +152 -0
- package/src/metadata.ts +12 -0
- package/src/platform-machine.ts +475 -0
- package/src/prefabs/types.ts +82 -0
- package/src/prefabs.ts +12 -0
- package/src/preview/host-workspace.tsx +59 -0
- package/src/preview/identity.ts +66 -0
- package/src/preview/layout.ts +223 -0
- package/src/preview/manager.ts +577 -0
- package/src/preview/messages.ts +26 -0
- package/src/preview/ownership.ts +81 -0
- package/src/preview/url.ts +117 -0
- package/src/preview/window.tsx +490 -0
- package/src/preview/workspace.tsx +678 -0
- package/src/preview.ts +42 -0
- package/src/protocol/arcade-platform-actions.ts +29 -0
- package/src/protocol/controller.ts +187 -0
- package/src/protocol/core.ts +23 -0
- package/src/protocol/dev-log-events.ts +222 -0
- package/src/protocol/errors.ts +21 -0
- package/src/protocol/host-grant.ts +185 -0
- package/src/protocol/host.ts +138 -0
- package/src/protocol/notices.ts +43 -0
- package/src/protocol/signals.ts +38 -0
- package/src/protocol/socket-events.ts +154 -0
- package/src/protocol/sync.ts +121 -0
- package/src/protocol/url-policy.ts +63 -0
- package/src/protocol.ts +12 -386
- package/src/qrcode.d.ts +16 -0
- package/src/release.ts +99 -0
- package/src/runtime/air-jam-config.ts +287 -0
- package/src/runtime/air-jam-error-boundary.tsx +672 -0
- package/src/runtime/arcade-bridge-request-surface.ts +15 -0
- package/src/runtime/arcade-runtime-url.ts +95 -0
- package/src/runtime/arcade-surface-identity.ts +16 -0
- package/src/runtime/contracts/control.ts +68 -0
- package/src/runtime/contracts/inspection.ts +165 -0
- package/src/runtime/contracts/observability.ts +94 -0
- package/src/runtime/controller-bridge.ts +451 -0
- package/src/runtime/controller-identity.ts +132 -0
- package/src/runtime/controller-presentation.ts +24 -0
- package/src/runtime/controller-realtime-client.ts +591 -0
- package/src/runtime/create-air-jam-app.tsx +260 -0
- package/src/runtime/dev-runtime-events.ts +36 -0
- package/src/runtime/embedded-runtime-adapters.ts +68 -0
- package/src/runtime/host-bridge.ts +388 -0
- package/src/runtime/host-realtime-client.ts +550 -0
- package/src/runtime/iframe-bridge.ts +135 -0
- package/src/runtime/realtime-client.ts +20 -0
- package/src/runtime/router-basename.ts +25 -0
- package/src/runtime/runtime-owner-contexts.tsx +4 -0
- package/src/runtime/runtime-session-params.ts +169 -0
- package/src/runtime/sdk-version.ts +6 -0
- package/src/runtime/session-runtimes.tsx +188 -0
- package/src/runtime/validate-arcade-bridge-attach.ts +14 -0
- package/src/runtime-control.ts +10 -0
- package/src/runtime-inspection.ts +14 -0
- package/src/runtime-observability.ts +9 -0
- package/src/settings/platform-settings-bridge.ts +336 -0
- package/src/settings/platform-settings-runtime.tsx +387 -0
- package/src/settings/platform-settings.ts +288 -0
- package/src/state/connection-store.ts +80 -12
- package/src/store/air-jam-store-domain-constants.ts +4 -0
- package/src/store/create-air-jam-store.ts +1094 -227
- package/src/styles.css +4 -38
- package/src/ui.ts +29 -0
- package/src/utils/is-rpc-serializable.ts +56 -0
- package/src/utils/player-avatar-url.ts +48 -0
- package/src/utils/url-builder.ts +7 -0
- package/dist/.tsbuildinfo +0 -1
- package/dist/chunk-B5DURUWT.js +0 -80
- package/dist/chunk-B5DURUWT.js.map +0 -1
- package/src/AirJamClient.ts +0 -224
- package/src/audio/volume-store.ts +0 -124
- package/src/components/controller-shell.tsx +0 -346
- package/src/components/host-shell.tsx +0 -521
- package/src/components/qr-scanner-dialog.tsx +0 -373
- package/src/components/ui/alert.tsx +0 -66
- package/src/components/ui/avatar.tsx +0 -46
- package/src/components/ui/badge.tsx +0 -47
- package/src/components/ui/card.tsx +0 -92
- package/src/components/ui/dialog.tsx +0 -122
- package/src/components/ui/tabs.tsx +0 -64
- package/src/hooks/use-air-jam-shell.ts +0 -379
- package/src/hooks/use-fullscreen.ts +0 -108
|
@@ -0,0 +1,550 @@
|
|
|
1
|
+
import type { AirJamSocket } from "../context/socket-manager";
|
|
2
|
+
import { AIRJAM_DEV_LOG_EVENTS, type RoomCode } from "../protocol";
|
|
3
|
+
import { emitAirJamDevRuntimeEvent } from "./dev-runtime-events";
|
|
4
|
+
import {
|
|
5
|
+
createHostBridgeCloseMessage,
|
|
6
|
+
createHostBridgeEmitMessage,
|
|
7
|
+
createHostBridgeRequestMessage,
|
|
8
|
+
createHostBridgeResponseMessage,
|
|
9
|
+
parseHostBridgeAttachMessage,
|
|
10
|
+
parseHostBridgeCloseMessage,
|
|
11
|
+
parseHostBridgeEventMessage,
|
|
12
|
+
parseHostBridgeResponseMessage,
|
|
13
|
+
type HostBridgeClientEventArgs,
|
|
14
|
+
type HostBridgeClientEventName,
|
|
15
|
+
type HostBridgeServerEventArgs,
|
|
16
|
+
type HostBridgeServerEventName,
|
|
17
|
+
} from "./host-bridge";
|
|
18
|
+
import type {
|
|
19
|
+
AirJamRealtimeClient,
|
|
20
|
+
BridgeListener,
|
|
21
|
+
DirectSocketGetter,
|
|
22
|
+
} from "./realtime-client";
|
|
23
|
+
import { readChildHostRuntimeParams } from "./runtime-session-params";
|
|
24
|
+
import { validateArcadeBridgeAttachEpoch } from "./validate-arcade-bridge-attach";
|
|
25
|
+
|
|
26
|
+
const BRIDGE_HANDSHAKE_TIMEOUT_MS = 2000;
|
|
27
|
+
const BRIDGE_ACK_TIMEOUT_MS = 5_000;
|
|
28
|
+
const HOST_BRIDGE_RUNTIME_KIND = "arcade-host-runtime";
|
|
29
|
+
const BRIDGE_TRANSIENT_CLOSE_REASONS = new Set(["game_unloaded", "replaced"]);
|
|
30
|
+
const BRIDGE_TRANSIENT_FAILURE_REASONS = new Set(["handshake_timeout"]);
|
|
31
|
+
|
|
32
|
+
class DirectHostRealtimeClient implements AirJamRealtimeClient {
|
|
33
|
+
constructor(private readonly socket: AirJamSocket) {}
|
|
34
|
+
|
|
35
|
+
get connected(): boolean {
|
|
36
|
+
return this.socket.connected;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
get id(): string | undefined {
|
|
40
|
+
return this.socket.id;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
connect(): this {
|
|
44
|
+
this.socket.connect();
|
|
45
|
+
return this;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
disconnect(): this {
|
|
49
|
+
this.socket.disconnect();
|
|
50
|
+
return this;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
on(event: string, listener: BridgeListener): this {
|
|
54
|
+
this.socket.on(event as never, listener as never);
|
|
55
|
+
return this;
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
off(event: string, listener?: BridgeListener): this {
|
|
59
|
+
if (!listener) {
|
|
60
|
+
this.socket.off(event as never);
|
|
61
|
+
return this;
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
this.socket.off(event as never, listener as never);
|
|
65
|
+
return this;
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
emit(event: string, ...args: unknown[]): this {
|
|
69
|
+
(
|
|
70
|
+
this.socket.emit as unknown as (
|
|
71
|
+
this: AirJamSocket,
|
|
72
|
+
event: string,
|
|
73
|
+
...args: unknown[]
|
|
74
|
+
) => void
|
|
75
|
+
).call(this.socket, event, ...args);
|
|
76
|
+
return this;
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
emitWithAck<TAck>(event: string, ...args: unknown[]): Promise<TAck> {
|
|
80
|
+
return new Promise<TAck>((resolve, reject) => {
|
|
81
|
+
const timeoutId = setTimeout(() => {
|
|
82
|
+
reject(
|
|
83
|
+
new Error(
|
|
84
|
+
`Timed out waiting for acknowledgement for realtime event "${event}".`,
|
|
85
|
+
),
|
|
86
|
+
);
|
|
87
|
+
}, BRIDGE_ACK_TIMEOUT_MS);
|
|
88
|
+
|
|
89
|
+
(
|
|
90
|
+
this.socket.emit as unknown as (
|
|
91
|
+
this: AirJamSocket,
|
|
92
|
+
event: string,
|
|
93
|
+
...args: unknown[]
|
|
94
|
+
) => void
|
|
95
|
+
).call(this.socket, event, ...args, (ack: TAck) => {
|
|
96
|
+
clearTimeout(timeoutId);
|
|
97
|
+
resolve(ack);
|
|
98
|
+
});
|
|
99
|
+
});
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
class EmbeddedHostBridgeClient implements AirJamRealtimeClient {
|
|
104
|
+
public connected = false;
|
|
105
|
+
public id: string | undefined;
|
|
106
|
+
|
|
107
|
+
private listeners = new Map<string, Set<BridgeListener>>();
|
|
108
|
+
private port: MessagePort | null = null;
|
|
109
|
+
private connectTimeout: ReturnType<typeof setTimeout> | null = null;
|
|
110
|
+
private reconnectTimeout: ReturnType<typeof setTimeout> | null = null;
|
|
111
|
+
private requested = false;
|
|
112
|
+
private lastAttachedArcadeEpoch: number | null = null;
|
|
113
|
+
private allowReconnect = true;
|
|
114
|
+
private pendingAcks = new Map<
|
|
115
|
+
string,
|
|
116
|
+
{
|
|
117
|
+
resolve: (ack: unknown) => void;
|
|
118
|
+
reject: (error: Error) => void;
|
|
119
|
+
timeoutId: ReturnType<typeof setTimeout>;
|
|
120
|
+
}
|
|
121
|
+
>();
|
|
122
|
+
|
|
123
|
+
connect(): this {
|
|
124
|
+
const runtimeParams = readChildHostRuntimeParams();
|
|
125
|
+
if (!runtimeParams || typeof window === "undefined") {
|
|
126
|
+
this.fail("Embedded host bridge runtime params missing.", {
|
|
127
|
+
reason: "runtime_params_missing",
|
|
128
|
+
});
|
|
129
|
+
return this;
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
if (this.connected || this.requested) {
|
|
133
|
+
return this;
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
this.allowReconnect = true;
|
|
137
|
+
this.clearReconnectTimeout();
|
|
138
|
+
this.clearPort(false);
|
|
139
|
+
this.requested = true;
|
|
140
|
+
|
|
141
|
+
const channel = new MessageChannel();
|
|
142
|
+
this.port = channel.port1;
|
|
143
|
+
this.port.onmessage = (event) => {
|
|
144
|
+
this.handlePortMessage(event.data);
|
|
145
|
+
};
|
|
146
|
+
this.port.start?.();
|
|
147
|
+
|
|
148
|
+
this.connectTimeout = setTimeout(() => {
|
|
149
|
+
this.fail("Embedded host bridge handshake timed out.", {
|
|
150
|
+
reason: "handshake_timeout",
|
|
151
|
+
runtimeParams,
|
|
152
|
+
});
|
|
153
|
+
}, BRIDGE_HANDSHAKE_TIMEOUT_MS);
|
|
154
|
+
|
|
155
|
+
window.parent.postMessage(
|
|
156
|
+
createHostBridgeRequestMessage({
|
|
157
|
+
roomId: runtimeParams.room as RoomCode,
|
|
158
|
+
capabilityToken: runtimeParams.capabilityToken,
|
|
159
|
+
arcadeSurface: runtimeParams.arcadeSurface,
|
|
160
|
+
}),
|
|
161
|
+
"*",
|
|
162
|
+
[channel.port2],
|
|
163
|
+
);
|
|
164
|
+
|
|
165
|
+
this.emitRuntimeEvent(
|
|
166
|
+
AIRJAM_DEV_LOG_EVENTS.runtime.embeddedBridgeRequested,
|
|
167
|
+
"Embedded host bridge requested",
|
|
168
|
+
"info",
|
|
169
|
+
runtimeParams,
|
|
170
|
+
{
|
|
171
|
+
capabilityExpiresAt: runtimeParams.capabilityExpiresAt,
|
|
172
|
+
gameId: runtimeParams.arcadeSurface.gameId,
|
|
173
|
+
surfaceKind: runtimeParams.arcadeSurface.kind,
|
|
174
|
+
},
|
|
175
|
+
);
|
|
176
|
+
|
|
177
|
+
return this;
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
disconnect(): this {
|
|
181
|
+
this.allowReconnect = false;
|
|
182
|
+
this.clearReconnectTimeout();
|
|
183
|
+
if (this.port) {
|
|
184
|
+
this.port.postMessage(createHostBridgeCloseMessage("detached"));
|
|
185
|
+
}
|
|
186
|
+
this.clearPort(true, "detached");
|
|
187
|
+
return this;
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
on(event: string, listener: BridgeListener): this {
|
|
191
|
+
const eventKey = String(event);
|
|
192
|
+
const current = this.listeners.get(eventKey) ?? new Set<BridgeListener>();
|
|
193
|
+
current.add(listener);
|
|
194
|
+
this.listeners.set(eventKey, current);
|
|
195
|
+
return this;
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
off(event: string, listener?: BridgeListener): this {
|
|
199
|
+
const eventKey = String(event);
|
|
200
|
+
const current = this.listeners.get(eventKey);
|
|
201
|
+
if (!current) {
|
|
202
|
+
return this;
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
if (!listener) {
|
|
206
|
+
this.listeners.delete(eventKey);
|
|
207
|
+
return this;
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
current.delete(listener);
|
|
211
|
+
if (current.size === 0) {
|
|
212
|
+
this.listeners.delete(eventKey);
|
|
213
|
+
}
|
|
214
|
+
return this;
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
emit(event: string, ...args: unknown[]): this {
|
|
218
|
+
if (!this.port || !this.connected) {
|
|
219
|
+
return this;
|
|
220
|
+
}
|
|
221
|
+
|
|
222
|
+
const bridgeEvent = event as HostBridgeClientEventName;
|
|
223
|
+
this.port.postMessage(
|
|
224
|
+
createHostBridgeEmitMessage(
|
|
225
|
+
bridgeEvent,
|
|
226
|
+
...(args as HostBridgeClientEventArgs[typeof bridgeEvent]),
|
|
227
|
+
),
|
|
228
|
+
);
|
|
229
|
+
return this;
|
|
230
|
+
}
|
|
231
|
+
|
|
232
|
+
emitWithAck<TAck>(event: string, ...args: unknown[]): Promise<TAck> {
|
|
233
|
+
if (!this.port || !this.connected) {
|
|
234
|
+
return Promise.reject(
|
|
235
|
+
new Error(
|
|
236
|
+
`Cannot emit acknowledged realtime event "${event}" because the embedded host bridge is not connected.`,
|
|
237
|
+
),
|
|
238
|
+
);
|
|
239
|
+
}
|
|
240
|
+
|
|
241
|
+
const requestId =
|
|
242
|
+
globalThis.crypto?.randomUUID?.() ??
|
|
243
|
+
`ack-${Date.now().toString(36)}-${Math.random().toString(36).slice(2)}`;
|
|
244
|
+
|
|
245
|
+
return new Promise<TAck>((resolve, reject) => {
|
|
246
|
+
const timeoutId = setTimeout(() => {
|
|
247
|
+
this.pendingAcks.delete(requestId);
|
|
248
|
+
reject(
|
|
249
|
+
new Error(
|
|
250
|
+
`Timed out waiting for acknowledgement for bridge event "${event}".`,
|
|
251
|
+
),
|
|
252
|
+
);
|
|
253
|
+
}, BRIDGE_ACK_TIMEOUT_MS);
|
|
254
|
+
|
|
255
|
+
this.pendingAcks.set(requestId, {
|
|
256
|
+
resolve: (ack) => resolve(ack as TAck),
|
|
257
|
+
reject,
|
|
258
|
+
timeoutId,
|
|
259
|
+
});
|
|
260
|
+
|
|
261
|
+
const bridgeEvent = event as HostBridgeClientEventName;
|
|
262
|
+
this.port?.postMessage(
|
|
263
|
+
createHostBridgeEmitMessage(
|
|
264
|
+
bridgeEvent,
|
|
265
|
+
...(args as HostBridgeClientEventArgs[typeof bridgeEvent]),
|
|
266
|
+
{ requestId },
|
|
267
|
+
),
|
|
268
|
+
);
|
|
269
|
+
});
|
|
270
|
+
}
|
|
271
|
+
|
|
272
|
+
private handlePortMessage(message: unknown): void {
|
|
273
|
+
const responseMessage = parseHostBridgeResponseMessage(message);
|
|
274
|
+
if (responseMessage) {
|
|
275
|
+
const pending = this.pendingAcks.get(responseMessage.payload.requestId);
|
|
276
|
+
if (!pending) {
|
|
277
|
+
return;
|
|
278
|
+
}
|
|
279
|
+
|
|
280
|
+
clearTimeout(pending.timeoutId);
|
|
281
|
+
this.pendingAcks.delete(responseMessage.payload.requestId);
|
|
282
|
+
pending.resolve(responseMessage.payload.ack);
|
|
283
|
+
return;
|
|
284
|
+
}
|
|
285
|
+
|
|
286
|
+
const attachMessage = parseHostBridgeAttachMessage(message);
|
|
287
|
+
if (attachMessage) {
|
|
288
|
+
if (
|
|
289
|
+
attachMessage.payload.handshake.runtimeKind !== "arcade-host-runtime" ||
|
|
290
|
+
attachMessage.payload.handshake.capabilityFlags.hostBridge !== true
|
|
291
|
+
) {
|
|
292
|
+
this.fail("Embedded host bridge handshake rejected.", {
|
|
293
|
+
reason: "handshake_rejected",
|
|
294
|
+
runtimeParams: readChildHostRuntimeParams(),
|
|
295
|
+
});
|
|
296
|
+
return;
|
|
297
|
+
}
|
|
298
|
+
|
|
299
|
+
const snapshot = attachMessage.payload.snapshot;
|
|
300
|
+
const epochResult = validateArcadeBridgeAttachEpoch(
|
|
301
|
+
this.lastAttachedArcadeEpoch,
|
|
302
|
+
snapshot.arcadeSurface,
|
|
303
|
+
);
|
|
304
|
+
if (!epochResult.ok) {
|
|
305
|
+
this.fail(
|
|
306
|
+
"Embedded host bridge attach rejected: stale arcade surface epoch.",
|
|
307
|
+
{
|
|
308
|
+
reason: "stale_arcade_surface_epoch",
|
|
309
|
+
runtimeParams: readChildHostRuntimeParams(),
|
|
310
|
+
data: {
|
|
311
|
+
attachedEpoch: snapshot.arcadeSurface.epoch,
|
|
312
|
+
previousEpoch: this.lastAttachedArcadeEpoch,
|
|
313
|
+
},
|
|
314
|
+
},
|
|
315
|
+
);
|
|
316
|
+
return;
|
|
317
|
+
}
|
|
318
|
+
this.lastAttachedArcadeEpoch = epochResult.nextLast;
|
|
319
|
+
|
|
320
|
+
this.emitRuntimeEvent(
|
|
321
|
+
AIRJAM_DEV_LOG_EVENTS.runtime.embeddedBridgeAttached,
|
|
322
|
+
"Embedded host bridge attached",
|
|
323
|
+
"info",
|
|
324
|
+
readChildHostRuntimeParams(),
|
|
325
|
+
{
|
|
326
|
+
connected: snapshot.connected,
|
|
327
|
+
socketId: snapshot.socketId,
|
|
328
|
+
playerCount: snapshot.players.length,
|
|
329
|
+
},
|
|
330
|
+
);
|
|
331
|
+
|
|
332
|
+
this.id = snapshot.socketId ?? snapshot.roomId;
|
|
333
|
+
this.requested = false;
|
|
334
|
+
this.clearConnectTimeout();
|
|
335
|
+
|
|
336
|
+
if (snapshot.connected && !this.connected) {
|
|
337
|
+
this.connected = true;
|
|
338
|
+
this.notify("connect");
|
|
339
|
+
} else if (!snapshot.connected) {
|
|
340
|
+
this.connected = false;
|
|
341
|
+
}
|
|
342
|
+
|
|
343
|
+
for (const player of snapshot.players) {
|
|
344
|
+
this.notify("server:controllerJoined", player);
|
|
345
|
+
}
|
|
346
|
+
|
|
347
|
+
if (snapshot.state) {
|
|
348
|
+
this.notify("server:state", {
|
|
349
|
+
roomId: snapshot.roomId,
|
|
350
|
+
state: snapshot.state,
|
|
351
|
+
});
|
|
352
|
+
}
|
|
353
|
+
|
|
354
|
+
return;
|
|
355
|
+
}
|
|
356
|
+
|
|
357
|
+
const eventMessage = parseHostBridgeEventMessage(message);
|
|
358
|
+
if (eventMessage) {
|
|
359
|
+
const { event, args, requestId } = eventMessage.payload;
|
|
360
|
+
|
|
361
|
+
if (event === "connect") {
|
|
362
|
+
this.connected = true;
|
|
363
|
+
}
|
|
364
|
+
|
|
365
|
+
if (event === "disconnect") {
|
|
366
|
+
this.connected = false;
|
|
367
|
+
}
|
|
368
|
+
|
|
369
|
+
if (event === "airjam:action_rpc" && requestId && this.port) {
|
|
370
|
+
const [payload] = args as [
|
|
371
|
+
HostBridgeServerEventArgs["airjam:action_rpc"][0],
|
|
372
|
+
];
|
|
373
|
+
this.notify(event, payload, (ack: unknown) => {
|
|
374
|
+
this.port?.postMessage(
|
|
375
|
+
createHostBridgeResponseMessage(requestId, ack),
|
|
376
|
+
);
|
|
377
|
+
});
|
|
378
|
+
return;
|
|
379
|
+
}
|
|
380
|
+
|
|
381
|
+
this.notify(event, ...(args as HostBridgeServerEventArgs[typeof event]));
|
|
382
|
+
return;
|
|
383
|
+
}
|
|
384
|
+
|
|
385
|
+
const closeMessage = parseHostBridgeCloseMessage(message);
|
|
386
|
+
if (closeMessage) {
|
|
387
|
+
this.emitRuntimeEvent(
|
|
388
|
+
AIRJAM_DEV_LOG_EVENTS.runtime.embeddedBridgeClosed,
|
|
389
|
+
"Embedded host bridge closed",
|
|
390
|
+
"info",
|
|
391
|
+
readChildHostRuntimeParams(),
|
|
392
|
+
{ reason: closeMessage.payload.reason },
|
|
393
|
+
);
|
|
394
|
+
this.clearPort(true, closeMessage.payload.reason);
|
|
395
|
+
this.scheduleReconnect(
|
|
396
|
+
closeMessage.payload.reason,
|
|
397
|
+
BRIDGE_TRANSIENT_CLOSE_REASONS,
|
|
398
|
+
);
|
|
399
|
+
}
|
|
400
|
+
}
|
|
401
|
+
|
|
402
|
+
private fail(
|
|
403
|
+
reason: string,
|
|
404
|
+
options?: {
|
|
405
|
+
reason?: string;
|
|
406
|
+
runtimeParams?: ReturnType<typeof readChildHostRuntimeParams>;
|
|
407
|
+
data?: Record<string, unknown>;
|
|
408
|
+
},
|
|
409
|
+
): void {
|
|
410
|
+
this.emitRuntimeEvent(
|
|
411
|
+
AIRJAM_DEV_LOG_EVENTS.runtime.embeddedBridgeRejected,
|
|
412
|
+
reason,
|
|
413
|
+
"warn",
|
|
414
|
+
options?.runtimeParams ?? readChildHostRuntimeParams(),
|
|
415
|
+
{
|
|
416
|
+
reason: options?.reason ?? reason,
|
|
417
|
+
...options?.data,
|
|
418
|
+
},
|
|
419
|
+
);
|
|
420
|
+
this.clearPort(false);
|
|
421
|
+
this.scheduleReconnect(options?.reason, BRIDGE_TRANSIENT_FAILURE_REASONS);
|
|
422
|
+
this.notify("disconnect", reason);
|
|
423
|
+
}
|
|
424
|
+
|
|
425
|
+
private clearPort(notifyDisconnect: boolean, reason?: string): void {
|
|
426
|
+
const shouldNotify = notifyDisconnect && (this.connected || this.requested);
|
|
427
|
+
this.clearConnectTimeout();
|
|
428
|
+
this.requested = false;
|
|
429
|
+
this.connected = false;
|
|
430
|
+
this.id = undefined;
|
|
431
|
+
this.lastAttachedArcadeEpoch = null;
|
|
432
|
+
|
|
433
|
+
if (this.port) {
|
|
434
|
+
this.port.onmessage = null;
|
|
435
|
+
this.port.close();
|
|
436
|
+
this.port = null;
|
|
437
|
+
}
|
|
438
|
+
|
|
439
|
+
for (const pending of this.pendingAcks.values()) {
|
|
440
|
+
clearTimeout(pending.timeoutId);
|
|
441
|
+
pending.reject(
|
|
442
|
+
new Error("Embedded host bridge disconnected before acknowledgement."),
|
|
443
|
+
);
|
|
444
|
+
}
|
|
445
|
+
this.pendingAcks.clear();
|
|
446
|
+
|
|
447
|
+
if (shouldNotify) {
|
|
448
|
+
this.notify("disconnect", reason);
|
|
449
|
+
}
|
|
450
|
+
}
|
|
451
|
+
|
|
452
|
+
private scheduleReconnect(
|
|
453
|
+
reason: string | undefined,
|
|
454
|
+
transientReasons: ReadonlySet<string>,
|
|
455
|
+
): void {
|
|
456
|
+
if (!this.allowReconnect || !transientReasons.has(reason ?? "")) {
|
|
457
|
+
return;
|
|
458
|
+
}
|
|
459
|
+
|
|
460
|
+
this.clearReconnectTimeout();
|
|
461
|
+
this.reconnectTimeout = setTimeout(() => {
|
|
462
|
+
this.reconnectTimeout = null;
|
|
463
|
+
this.connect();
|
|
464
|
+
}, 50);
|
|
465
|
+
}
|
|
466
|
+
|
|
467
|
+
private clearConnectTimeout(): void {
|
|
468
|
+
if (!this.connectTimeout) {
|
|
469
|
+
return;
|
|
470
|
+
}
|
|
471
|
+
|
|
472
|
+
clearTimeout(this.connectTimeout);
|
|
473
|
+
this.connectTimeout = null;
|
|
474
|
+
}
|
|
475
|
+
|
|
476
|
+
private clearReconnectTimeout(): void {
|
|
477
|
+
if (!this.reconnectTimeout) {
|
|
478
|
+
return;
|
|
479
|
+
}
|
|
480
|
+
|
|
481
|
+
clearTimeout(this.reconnectTimeout);
|
|
482
|
+
this.reconnectTimeout = null;
|
|
483
|
+
}
|
|
484
|
+
|
|
485
|
+
private emitRuntimeEvent(
|
|
486
|
+
event: (typeof AIRJAM_DEV_LOG_EVENTS.runtime)[keyof typeof AIRJAM_DEV_LOG_EVENTS.runtime],
|
|
487
|
+
message: string,
|
|
488
|
+
level: "info" | "warn" | "error",
|
|
489
|
+
runtimeParams: ReturnType<typeof readChildHostRuntimeParams>,
|
|
490
|
+
data?: Record<string, unknown>,
|
|
491
|
+
): void {
|
|
492
|
+
emitAirJamDevRuntimeEvent({
|
|
493
|
+
event,
|
|
494
|
+
message,
|
|
495
|
+
level,
|
|
496
|
+
role: "host",
|
|
497
|
+
roomId: runtimeParams?.room,
|
|
498
|
+
runtimeEpoch: runtimeParams?.arcadeSurface.epoch,
|
|
499
|
+
runtimeKind: HOST_BRIDGE_RUNTIME_KIND,
|
|
500
|
+
data,
|
|
501
|
+
});
|
|
502
|
+
}
|
|
503
|
+
|
|
504
|
+
private notify<TEvent extends HostBridgeServerEventName>(
|
|
505
|
+
event: TEvent,
|
|
506
|
+
...args: HostBridgeServerEventArgs[TEvent]
|
|
507
|
+
): void {
|
|
508
|
+
const current = this.listeners.get(event);
|
|
509
|
+
if (!current) {
|
|
510
|
+
return;
|
|
511
|
+
}
|
|
512
|
+
|
|
513
|
+
for (const listener of current) {
|
|
514
|
+
listener(...args);
|
|
515
|
+
}
|
|
516
|
+
}
|
|
517
|
+
}
|
|
518
|
+
|
|
519
|
+
let embeddedHostClient: EmbeddedHostBridgeClient | null = null;
|
|
520
|
+
const directHostClients = new WeakMap<AirJamSocket, DirectHostRealtimeClient>();
|
|
521
|
+
|
|
522
|
+
export const isEmbeddedHostRuntime = (): boolean =>
|
|
523
|
+
readChildHostRuntimeParams() !== null;
|
|
524
|
+
|
|
525
|
+
export const getHostRealtimeClient = (
|
|
526
|
+
getSocket: DirectSocketGetter<"host">,
|
|
527
|
+
): AirJamRealtimeClient => {
|
|
528
|
+
if (!isEmbeddedHostRuntime()) {
|
|
529
|
+
const socket = getSocket("host");
|
|
530
|
+
const existing = directHostClients.get(socket);
|
|
531
|
+
if (existing) {
|
|
532
|
+
return existing;
|
|
533
|
+
}
|
|
534
|
+
|
|
535
|
+
const client = new DirectHostRealtimeClient(socket);
|
|
536
|
+
directHostClients.set(socket, client);
|
|
537
|
+
return client;
|
|
538
|
+
}
|
|
539
|
+
|
|
540
|
+
if (!embeddedHostClient) {
|
|
541
|
+
embeddedHostClient = new EmbeddedHostBridgeClient();
|
|
542
|
+
}
|
|
543
|
+
|
|
544
|
+
return embeddedHostClient;
|
|
545
|
+
};
|
|
546
|
+
|
|
547
|
+
export const resetHostRealtimeClientForTests = (): void => {
|
|
548
|
+
embeddedHostClient?.disconnect();
|
|
549
|
+
embeddedHostClient = null;
|
|
550
|
+
};
|
|
@@ -0,0 +1,135 @@
|
|
|
1
|
+
import { z } from "zod";
|
|
2
|
+
import {
|
|
3
|
+
AIR_JAM_PROTOCOL_V2,
|
|
4
|
+
type RuntimeKind,
|
|
5
|
+
type V2Handshake,
|
|
6
|
+
v2HandshakeSchema,
|
|
7
|
+
} from "../contracts/v2";
|
|
8
|
+
import {
|
|
9
|
+
normalizePlatformSettings,
|
|
10
|
+
type PlatformSettingsSnapshot,
|
|
11
|
+
} from "../settings/platform-settings";
|
|
12
|
+
|
|
13
|
+
export const AIRJAM_BRIDGE_INIT = "AIRJAM_BRIDGE_INIT" as const;
|
|
14
|
+
export const AIRJAM_SETTINGS_SYNC = "AIRJAM_SETTINGS_SYNC" as const;
|
|
15
|
+
export const AIRJAM_SETTINGS_READY = "AIRJAM_SETTINGS_READY" as const;
|
|
16
|
+
|
|
17
|
+
export interface AirJamSettingsSyncPayload {
|
|
18
|
+
settings: PlatformSettingsSnapshot;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
export interface AirJamSettingsSyncMessage {
|
|
22
|
+
type: typeof AIRJAM_SETTINGS_SYNC;
|
|
23
|
+
payload: AirJamSettingsSyncPayload;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
export interface AirJamBridgeInitMessage {
|
|
27
|
+
type: typeof AIRJAM_BRIDGE_INIT;
|
|
28
|
+
payload: {
|
|
29
|
+
handshake: V2Handshake;
|
|
30
|
+
};
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
export interface AirJamSettingsReadyMessage {
|
|
34
|
+
type: typeof AIRJAM_SETTINGS_READY;
|
|
35
|
+
payload: {
|
|
36
|
+
ready: true;
|
|
37
|
+
};
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
const airJamSettingsSyncSchema = z
|
|
41
|
+
.object({
|
|
42
|
+
type: z.literal(AIRJAM_SETTINGS_SYNC),
|
|
43
|
+
payload: z
|
|
44
|
+
.object({
|
|
45
|
+
settings: z
|
|
46
|
+
.object({
|
|
47
|
+
audio: z
|
|
48
|
+
.object({
|
|
49
|
+
masterVolume: z.number(),
|
|
50
|
+
musicVolume: z.number(),
|
|
51
|
+
sfxVolume: z.number(),
|
|
52
|
+
})
|
|
53
|
+
.strict(),
|
|
54
|
+
accessibility: z
|
|
55
|
+
.object({
|
|
56
|
+
reducedMotion: z.boolean(),
|
|
57
|
+
highContrast: z.boolean(),
|
|
58
|
+
})
|
|
59
|
+
.strict(),
|
|
60
|
+
feedback: z
|
|
61
|
+
.object({
|
|
62
|
+
hapticsEnabled: z.boolean(),
|
|
63
|
+
})
|
|
64
|
+
.strict(),
|
|
65
|
+
previewControllers: z
|
|
66
|
+
.object({
|
|
67
|
+
activeOpacity: z.number(),
|
|
68
|
+
})
|
|
69
|
+
.strict(),
|
|
70
|
+
})
|
|
71
|
+
.strict(),
|
|
72
|
+
})
|
|
73
|
+
.strict(),
|
|
74
|
+
})
|
|
75
|
+
.strict();
|
|
76
|
+
|
|
77
|
+
const airJamBridgeInitSchema = z
|
|
78
|
+
.object({
|
|
79
|
+
type: z.literal(AIRJAM_BRIDGE_INIT),
|
|
80
|
+
payload: z
|
|
81
|
+
.object({
|
|
82
|
+
handshake: v2HandshakeSchema,
|
|
83
|
+
})
|
|
84
|
+
.strict(),
|
|
85
|
+
})
|
|
86
|
+
.strict();
|
|
87
|
+
|
|
88
|
+
const airJamSettingsReadySchema = z
|
|
89
|
+
.object({
|
|
90
|
+
type: z.literal(AIRJAM_SETTINGS_READY),
|
|
91
|
+
payload: z
|
|
92
|
+
.object({
|
|
93
|
+
ready: z.literal(true),
|
|
94
|
+
})
|
|
95
|
+
.strict(),
|
|
96
|
+
})
|
|
97
|
+
.strict();
|
|
98
|
+
|
|
99
|
+
export const createBridgeHandshake = ({
|
|
100
|
+
sdkVersion,
|
|
101
|
+
runtimeKind,
|
|
102
|
+
capabilityFlags = {},
|
|
103
|
+
}: {
|
|
104
|
+
sdkVersion: string;
|
|
105
|
+
runtimeKind: RuntimeKind;
|
|
106
|
+
capabilityFlags?: Record<string, boolean>;
|
|
107
|
+
}): V2Handshake =>
|
|
108
|
+
v2HandshakeSchema.parse({
|
|
109
|
+
protocolVersion: AIR_JAM_PROTOCOL_V2,
|
|
110
|
+
sdkVersion,
|
|
111
|
+
runtimeKind,
|
|
112
|
+
capabilityFlags,
|
|
113
|
+
});
|
|
114
|
+
|
|
115
|
+
export const isAirJamSettingsSyncMessage = (
|
|
116
|
+
value: unknown,
|
|
117
|
+
): value is AirJamSettingsSyncMessage => {
|
|
118
|
+
const result = airJamSettingsSyncSchema.safeParse(value);
|
|
119
|
+
return (
|
|
120
|
+
result.success && !!normalizePlatformSettings(result.data.payload.settings)
|
|
121
|
+
);
|
|
122
|
+
};
|
|
123
|
+
|
|
124
|
+
export const parseAirJamBridgeInitMessage = (
|
|
125
|
+
value: unknown,
|
|
126
|
+
): AirJamBridgeInitMessage | null => {
|
|
127
|
+
const result = airJamBridgeInitSchema.safeParse(value);
|
|
128
|
+
return result.success ? result.data : null;
|
|
129
|
+
};
|
|
130
|
+
|
|
131
|
+
export const isAirJamSettingsReadyMessage = (
|
|
132
|
+
value: unknown,
|
|
133
|
+
): value is AirJamSettingsReadyMessage => {
|
|
134
|
+
return airJamSettingsReadySchema.safeParse(value).success;
|
|
135
|
+
};
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import type { AirJamSocket } from "../context/socket-manager";
|
|
2
|
+
|
|
3
|
+
export interface AirJamRealtimeClient {
|
|
4
|
+
readonly connected: boolean;
|
|
5
|
+
readonly id?: string;
|
|
6
|
+
connect(): this;
|
|
7
|
+
disconnect(): this;
|
|
8
|
+
on(event: string, listener: BridgeListener): this;
|
|
9
|
+
off(event: string, listener?: BridgeListener): this;
|
|
10
|
+
emit(event: string, ...args: unknown[]): this;
|
|
11
|
+
emitWithAck<TAck>(event: string, ...args: unknown[]): Promise<TAck>;
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
export type BridgeListener = {
|
|
15
|
+
bivarianceHack(...args: unknown[]): void;
|
|
16
|
+
}["bivarianceHack"];
|
|
17
|
+
|
|
18
|
+
export type DirectSocketGetter<T extends "host" | "controller"> = (
|
|
19
|
+
role: T,
|
|
20
|
+
) => AirJamSocket;
|