@air-jam/sdk 0.1.4 → 0.9.1

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