@a16n/web-sim 0.0.0-stage → 0.1.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 (281) hide show
  1. package/README.md +13 -2
  2. package/bin/cli.ts +255 -0
  3. package/bin/web-sim.js +39 -0
  4. package/overrides/@backpackapp-io/react-native-toast/overrides.ts +15 -0
  5. package/overrides/@react-native-firebase/app/overrides.ts +34 -0
  6. package/overrides/@tiktok-for-business/react-native-sdk/NativeTiktokSdk.ts +29 -0
  7. package/overrides/@tiktok-for-business/react-native-sdk/overrides.ts +9 -0
  8. package/overrides/expo-application/ExpoApplication.ts +81 -0
  9. package/overrides/expo-application/overrides.ts +16 -0
  10. package/overrides/expo-background-task/ExpoBackgroundTaskModule.ts +30 -0
  11. package/overrides/expo-background-task/overrides.ts +9 -0
  12. package/overrides/expo-constants/ExponentConstants.ts +66 -0
  13. package/overrides/expo-constants/overrides.ts +9 -0
  14. package/overrides/expo-device/ExpoDevice.ts +104 -0
  15. package/overrides/expo-device/overrides.ts +9 -0
  16. package/overrides/expo-glass-effect/GlassContainer.tsx +9 -0
  17. package/overrides/expo-glass-effect/GlassView.tsx +95 -0
  18. package/overrides/expo-glass-effect/availability.ts +14 -0
  19. package/overrides/expo-glass-effect/overrides.ts +14 -0
  20. package/overrides/expo-haptics/Haptics.ts +67 -0
  21. package/overrides/expo-haptics/overrides.ts +5 -0
  22. package/overrides/expo-mail-composer/ExpoMailComposer.ts +40 -0
  23. package/overrides/expo-mail-composer/overrides.ts +9 -0
  24. package/overrides/expo-mesh-gradient/MeshGradientView.tsx +44 -0
  25. package/overrides/expo-mesh-gradient/overrides.ts +9 -0
  26. package/overrides/expo-modules-core/overrides.ts +12 -0
  27. package/overrides/expo-navigation-bar/ExpoNavigationBar.ts +29 -0
  28. package/overrides/expo-navigation-bar/NavigationBar.ts +43 -0
  29. package/overrides/expo-navigation-bar/overrides.ts +12 -0
  30. package/overrides/expo-router/NativeStackView.tsx +74 -0
  31. package/overrides/expo-router/NativeTabsView.tsx +263 -0
  32. package/overrides/expo-router/SafeAreaProvider.ts +6 -0
  33. package/overrides/expo-router/overrides.ts +23 -0
  34. package/overrides/expo-router/statusBarRequests.ts +51 -0
  35. package/overrides/expo-screen-orientation/ExpoScreenOrientation.ts +173 -0
  36. package/overrides/expo-screen-orientation/overrides.ts +21 -0
  37. package/overrides/expo-sharing/SharingNativeModule.ts +21 -0
  38. package/overrides/expo-sharing/overrides.ts +9 -0
  39. package/overrides/expo-sms/ExpoSMS.ts +24 -0
  40. package/overrides/expo-sms/overrides.ts +9 -0
  41. package/overrides/expo-splash-screen/SplashScreen.ts +25 -0
  42. package/overrides/expo-splash-screen/overrides.ts +5 -0
  43. package/overrides/expo-status-bar/StatusBar.tsx +44 -0
  44. package/overrides/expo-status-bar/overrides.ts +5 -0
  45. package/overrides/expo-store-review/ExpoStoreReview.ts +18 -0
  46. package/overrides/expo-store-review/overrides.ts +9 -0
  47. package/overrides/expo-symbols/SymbolView.tsx +192 -0
  48. package/overrides/expo-symbols/overrides.ts +9 -0
  49. package/overrides/expo-system-ui/ExpoSystemUI.ts +34 -0
  50. package/overrides/expo-system-ui/overrides.ts +9 -0
  51. package/overrides/expo-task-manager/ExpoTaskManager.ts +39 -0
  52. package/overrides/expo-task-manager/overrides.ts +9 -0
  53. package/overrides/expo-tracking-transparency/ExpoTrackingTransparency.ts +30 -0
  54. package/overrides/expo-tracking-transparency/overrides.ts +11 -0
  55. package/overrides/expo-video/ExpoVideo.ts +16 -0
  56. package/overrides/expo-video/overrides.ts +9 -0
  57. package/overrides/lottie-react-native/LottieView.tsx +87 -0
  58. package/overrides/lottie-react-native/overrides.ts +9 -0
  59. package/overrides/react-native-pager-view/PagerViewNativeComponent.tsx +254 -0
  60. package/overrides/react-native-pager-view/overrides.ts +9 -0
  61. package/overrides/react-native-reanimated/overrides.ts +28 -0
  62. package/overrides/react-native-safe-area-context/NativeSafeAreaProvider.tsx +40 -0
  63. package/overrides/react-native-safe-area-context/overrides.ts +12 -0
  64. package/overrides/react-native-share/NativeRNShare.ts +22 -0
  65. package/overrides/react-native-share/overrides.ts +12 -0
  66. package/overrides/react-native-share/platform.ts +14 -0
  67. package/overrides/react-native-turbo-image/TurboImage.tsx +101 -0
  68. package/overrides/react-native-turbo-image/overrides.ts +9 -0
  69. package/overrides/react-native-web/RefreshControl.tsx +57 -0
  70. package/overrides/react-native-web/overrides.ts +157 -0
  71. package/overrides/react-native-web/useElementLayout.ts +152 -0
  72. package/package.json +80 -4
  73. package/plugins/battery/overrides/expo-battery/ExpoBattery.ts +57 -0
  74. package/plugins/battery/overrides/expo-battery/overrides.ts +9 -0
  75. package/plugins/battery/plugin.ts +8 -0
  76. package/plugins/battery/shell.tsx +56 -0
  77. package/plugins/battery/state.ts +11 -0
  78. package/plugins/biometrics/overrides/expo-local-authentication/ExpoLocalAuthentication.ts +90 -0
  79. package/plugins/biometrics/overrides/expo-local-authentication/overrides.ts +11 -0
  80. package/plugins/biometrics/plugin.ts +8 -0
  81. package/plugins/biometrics/shell.tsx +88 -0
  82. package/plugins/biometrics/state.ts +41 -0
  83. package/plugins/brightness/app.ts +5 -0
  84. package/plugins/brightness/overrides/expo-brightness/Brightness.ts +98 -0
  85. package/plugins/brightness/overrides/expo-brightness/overrides.ts +10 -0
  86. package/plugins/brightness/plugin.ts +9 -0
  87. package/plugins/brightness/shell.tsx +55 -0
  88. package/plugins/brightness/state.ts +19 -0
  89. package/plugins/calendar/calendar.ts +299 -0
  90. package/plugins/calendar/overrides/expo-calendar/ExpoCalendar.ts +358 -0
  91. package/plugins/calendar/overrides/expo-calendar/LegacyExpoCalendar.ts +163 -0
  92. package/plugins/calendar/overrides/expo-calendar/overrides.ts +14 -0
  93. package/plugins/calendar/plugin.ts +8 -0
  94. package/plugins/calendar/shell.tsx +123 -0
  95. package/plugins/camera/overrides/expo-camera/ExpoCamera.tsx +269 -0
  96. package/plugins/camera/overrides/expo-camera/ExpoCameraManager.ts +56 -0
  97. package/plugins/camera/overrides/expo-camera/overrides.ts +12 -0
  98. package/plugins/camera/plugin.ts +9 -0
  99. package/plugins/camera/shell.tsx +107 -0
  100. package/plugins/camera/state.ts +61 -0
  101. package/plugins/clipboard/overrides/expo-clipboard/ExpoClipboard.ts +138 -0
  102. package/plugins/clipboard/overrides/expo-clipboard/overrides.ts +9 -0
  103. package/plugins/clipboard/plugin.ts +8 -0
  104. package/plugins/clipboard/shell.tsx +122 -0
  105. package/plugins/clipboard/state.ts +93 -0
  106. package/plugins/clock/app.ts +25 -0
  107. package/plugins/clock/plugin.ts +8 -0
  108. package/plugins/clock/shell.tsx +225 -0
  109. package/plugins/clock/state.ts +12 -0
  110. package/plugins/contacts/contacts.ts +125 -0
  111. package/plugins/contacts/overrides/expo-contacts/ExpoContacts.ts +153 -0
  112. package/plugins/contacts/overrides/expo-contacts/ExpoContactsNext.ts +232 -0
  113. package/plugins/contacts/overrides/expo-contacts/overrides.ts +12 -0
  114. package/plugins/contacts/plugin.ts +8 -0
  115. package/plugins/contacts/shell.tsx +109 -0
  116. package/plugins/home-indicator/app.ts +5 -0
  117. package/plugins/home-indicator/plugin.ts +9 -0
  118. package/plugins/home-indicator/requests.ts +48 -0
  119. package/plugins/home-indicator/shell.tsx +68 -0
  120. package/plugins/home-indicator/state.ts +31 -0
  121. package/plugins/keep-awake/app.ts +5 -0
  122. package/plugins/keep-awake/overrides/expo-keep-awake/ExpoKeepAwake.ts +27 -0
  123. package/plugins/keep-awake/overrides/expo-keep-awake/overrides.ts +9 -0
  124. package/plugins/keep-awake/plugin.ts +9 -0
  125. package/plugins/keep-awake/shell.tsx +80 -0
  126. package/plugins/keep-awake/state.ts +12 -0
  127. package/plugins/keyboard/Keyboard.tsx +354 -0
  128. package/plugins/keyboard/app.ts +90 -0
  129. package/plugins/keyboard/controller.ts +384 -0
  130. package/plugins/keyboard/fields.ts +244 -0
  131. package/plugins/keyboard/icons.tsx +283 -0
  132. package/plugins/keyboard/keyboard.css +362 -0
  133. package/plugins/keyboard/layouts/android.ts +175 -0
  134. package/plugins/keyboard/layouts/index.ts +10 -0
  135. package/plugins/keyboard/layouts/ios.ts +219 -0
  136. package/plugins/keyboard/layouts/types.ts +80 -0
  137. package/plugins/keyboard/overrides/react-native-keyboard-controller/KeyboardController.tsx +470 -0
  138. package/plugins/keyboard/overrides/react-native-keyboard-controller/overrides.ts +9 -0
  139. package/plugins/keyboard/plugin.ts +13 -0
  140. package/plugins/keyboard/shell.tsx +133 -0
  141. package/plugins/keyboard/state.ts +92 -0
  142. package/plugins/keyboard/suggestions.ts +63 -0
  143. package/plugins/keyboard/taps.ts +77 -0
  144. package/plugins/keyboard/typing.ts +112 -0
  145. package/plugins/locale/app.ts +232 -0
  146. package/plugins/locale/locale.ts +143 -0
  147. package/plugins/locale/overrides/expo-localization/ExpoLocalization.ts +41 -0
  148. package/plugins/locale/overrides/expo-localization/overrides.ts +9 -0
  149. package/plugins/locale/plugin.ts +9 -0
  150. package/plugins/locale/shell.tsx +147 -0
  151. package/plugins/locale/state.ts +36 -0
  152. package/plugins/location/geo.ts +56 -0
  153. package/plugins/location/overrides/expo-location/ExpoLocation.ts +235 -0
  154. package/plugins/location/overrides/expo-location/overrides.ts +11 -0
  155. package/plugins/location/places.ts +38 -0
  156. package/plugins/location/plugin.ts +8 -0
  157. package/plugins/location/shell.tsx +188 -0
  158. package/plugins/location/state.ts +51 -0
  159. package/plugins/motion/overrides/expo-sensors/DeviceSensor.ts +169 -0
  160. package/plugins/motion/overrides/expo-sensors/ExponentPedometer.ts +54 -0
  161. package/plugins/motion/overrides/expo-sensors/overrides.ts +14 -0
  162. package/plugins/motion/plugin.ts +8 -0
  163. package/plugins/motion/sensors.ts +116 -0
  164. package/plugins/motion/shell.tsx +136 -0
  165. package/plugins/motion/state.ts +46 -0
  166. package/plugins/network/app.ts +400 -0
  167. package/plugins/network/overrides/expo-network/ExpoNetwork.ts +51 -0
  168. package/plugins/network/overrides/expo-network/overrides.ts +9 -0
  169. package/plugins/network/plugin.ts +8 -0
  170. package/plugins/network/shell.tsx +67 -0
  171. package/plugins/network/state.ts +29 -0
  172. package/plugins/notifications/center.ts +255 -0
  173. package/plugins/notifications/content.ts +36 -0
  174. package/plugins/notifications/overrides/expo-notifications/NotificationsModule.ts +175 -0
  175. package/plugins/notifications/overrides/expo-notifications/channels.ts +108 -0
  176. package/plugins/notifications/overrides/expo-notifications/getDevicePushTokenAsync.ts +19 -0
  177. package/plugins/notifications/overrides/expo-notifications/getExpoPushTokenAsync.ts +16 -0
  178. package/plugins/notifications/overrides/expo-notifications/overrides.ts +36 -0
  179. package/plugins/notifications/plugin.ts +8 -0
  180. package/plugins/notifications/shell.tsx +192 -0
  181. package/plugins/notifications/state.ts +56 -0
  182. package/plugins/notifications/tokens.ts +47 -0
  183. package/plugins/notifications/trigger.ts +107 -0
  184. package/plugins/permissions/plugin.ts +6 -0
  185. package/plugins/permissions/shell.tsx +50 -0
  186. package/plugins/permissions/state.ts +38 -0
  187. package/plugins/photos/appLibrary.ts +114 -0
  188. package/plugins/photos/library.ts +322 -0
  189. package/plugins/photos/overrides/expo-image-picker/ExponentImagePicker.ts +153 -0
  190. package/plugins/photos/overrides/expo-image-picker/overrides.ts +13 -0
  191. package/plugins/photos/overrides/expo-media-library/ExpoMediaLibrary.ts +235 -0
  192. package/plugins/photos/overrides/expo-media-library/NativeMediaLibraryModule.ts +318 -0
  193. package/plugins/photos/overrides/expo-media-library/overrides.ts +19 -0
  194. package/plugins/photos/plugin.ts +8 -0
  195. package/plugins/photos/samples.ts +367 -0
  196. package/plugins/photos/shell.tsx +164 -0
  197. package/plugins/photos/state.ts +88 -0
  198. package/plugins/storage/files.ts +351 -0
  199. package/plugins/storage/overrides/expo-file-system/ExpoFileSystem.ts +770 -0
  200. package/plugins/storage/overrides/expo-file-system/ExponentFileSystem.ts +196 -0
  201. package/plugins/storage/overrides/expo-file-system/overrides.ts +13 -0
  202. package/plugins/storage/overrides/expo-secure-store/ExpoSecureStore.ts +75 -0
  203. package/plugins/storage/overrides/expo-secure-store/overrides.ts +9 -0
  204. package/plugins/storage/plugin.ts +8 -0
  205. package/plugins/storage/reset.ts +136 -0
  206. package/plugins/storage/secure.ts +71 -0
  207. package/plugins/storage/shell.tsx +365 -0
  208. package/plugins/storage/state.ts +13 -0
  209. package/src/client/app/appDocument.ts +129 -0
  210. package/src/client/app/appFocus.ts +97 -0
  211. package/src/client/app/console.ts +118 -0
  212. package/src/client/app/fonts.ts +76 -0
  213. package/src/client/app/main.tsx +99 -0
  214. package/src/client/app/routeContext.ts +45 -0
  215. package/src/client/app/touch.ts +640 -0
  216. package/src/client/entry.ts +20 -0
  217. package/src/client/modules.d.ts +8 -0
  218. package/src/client/react-native/app.ts +57 -0
  219. package/src/client/react-native/deep.ts +13 -0
  220. package/src/client/react-native/keyboard.tsx +192 -0
  221. package/src/client/react-native/library.ts +256 -0
  222. package/src/client/shell/Cookies.tsx +95 -0
  223. package/src/client/shell/LogDrawer.tsx +415 -0
  224. package/src/client/shell/PhotoPicker.tsx +174 -0
  225. package/src/client/shell/PluginOverlays.tsx +42 -0
  226. package/src/client/shell/PluginPanels.tsx +82 -0
  227. package/src/client/shell/ResetApp.tsx +87 -0
  228. package/src/client/shell/Resizer.tsx +99 -0
  229. package/src/client/shell/Shell.tsx +531 -0
  230. package/src/client/shell/SystemBars.tsx +376 -0
  231. package/src/client/shell/SystemCamera.tsx +181 -0
  232. package/src/client/shell/SystemOverlay.tsx +422 -0
  233. package/src/client/shell/SystemSheets.tsx +462 -0
  234. package/src/client/shell/backdrop.ts +229 -0
  235. package/src/client/shell/control.ts +91 -0
  236. package/src/client/shell/events.ts +226 -0
  237. package/src/client/shell/geometry.ts +68 -0
  238. package/src/client/shell/host.ts +492 -0
  239. package/src/client/shell/icons.tsx +34 -0
  240. package/src/client/shell/logFeed.ts +102 -0
  241. package/src/client/shell/main.tsx +22 -0
  242. package/src/client/shell/permissionCopy.ts +169 -0
  243. package/src/client/shell/plugins.ts +266 -0
  244. package/src/client/shell/popout.ts +185 -0
  245. package/src/client/shell/popover.ts +27 -0
  246. package/src/client/shell/shell.css +878 -0
  247. package/src/client/shell/statusTime.ts +31 -0
  248. package/src/client/shell/tooltips.ts +104 -0
  249. package/src/client/virtual.d.ts +23 -0
  250. package/src/devices/catalog.ts +223 -0
  251. package/src/devices/types.ts +83 -0
  252. package/src/node/appInfo.ts +188 -0
  253. package/src/node/appSource.ts +140 -0
  254. package/src/node/config.ts +180 -0
  255. package/src/node/control.ts +144 -0
  256. package/src/node/doctor.ts +199 -0
  257. package/src/node/events/cli.ts +206 -0
  258. package/src/node/events/shared.ts +238 -0
  259. package/src/node/events/store.ts +266 -0
  260. package/src/node/fonts.ts +377 -0
  261. package/src/node/index.ts +5 -0
  262. package/src/node/overrides/define.ts +56 -0
  263. package/src/node/overrides/packages.ts +136 -0
  264. package/src/node/overrides/platformPatch.ts +56 -0
  265. package/src/node/overrides/plugin.ts +169 -0
  266. package/src/node/overrides/registry.ts +258 -0
  267. package/src/node/paths.ts +18 -0
  268. package/src/node/platform.ts +171 -0
  269. package/src/node/plugins.ts +98 -0
  270. package/src/node/proxy.ts +421 -0
  271. package/src/node/server.ts +254 -0
  272. package/src/node/worklets.ts +86 -0
  273. package/src/plugin/index.ts +33 -0
  274. package/src/plugin/types.ts +127 -0
  275. package/src/plugin/ui.tsx +562 -0
  276. package/src/runtime/clock.ts +112 -0
  277. package/src/runtime/draw.ts +196 -0
  278. package/src/runtime/index.ts +519 -0
  279. package/src/runtime/media.ts +379 -0
  280. package/src/runtime/records.ts +76 -0
  281. package/src/runtime/timezone.ts +81 -0
@@ -0,0 +1,91 @@
1
+ import type { ControlCommand, ControlReply } from '../../node/control.ts'
2
+ import type { Appearance, Orientation } from '../../devices/types.ts'
3
+ import type { Origin } from './events.ts'
4
+ import type { HostStore } from './host.ts'
5
+
6
+ /**
7
+ * Answers the CLI's commands, which the dev server relays over its websocket.
8
+ */
9
+ export function listenForControl(host: HostStore): void {
10
+ const hot = import.meta.hot
11
+ if (hot === undefined) {
12
+ return
13
+ }
14
+ // The server sends each command to one tab, the one used last, so a command runs once however many tabs are open.
15
+ const announce = () => hot.send('web-sim:shell', { tab: host.events.tab })
16
+ announce()
17
+ hot.on('vite:ws:connect', announce)
18
+ window.addEventListener('focus', announce)
19
+ document.addEventListener('visibilitychange', () => document.visibilityState === 'visible' && announce())
20
+ // Events still waiting for their batch go before the page does.
21
+ window.addEventListener('pagehide', host.events.flush)
22
+
23
+ hot.on('web-sim:control', async ({ id, command, cause }: { id: number; command: ControlCommand; cause?: string }) => {
24
+ let reply: ControlReply
25
+ try {
26
+ reply = { id, ok: true, result: await perform(host, command, { actor: 'cli', cause }) }
27
+ } catch (error) {
28
+ reply = { id, ok: false, error: error instanceof Error ? error.message : String(error) }
29
+ }
30
+ hot.send('web-sim:control-reply', reply)
31
+ })
32
+ }
33
+
34
+ function simState(host: HostStore) {
35
+ const { device, orientation, appearance } = host.getState()
36
+ return { device: device.id, platform: device.platform, orientation, appearance, width: device.screen.width, height: device.screen.height }
37
+ }
38
+
39
+ async function perform(host: HostStore, command: ControlCommand, origin: Origin): Promise<unknown> {
40
+ const { plugins } = host
41
+ switch (command.type) {
42
+ case 'plugins':
43
+ return plugins.plugins.map((plugin) => ({ id: plugin.id, title: plugin.title, actions: Object.keys(plugin.actions ?? {}), state: plugins.get(plugin.id) }))
44
+ case 'state':
45
+ if (command.plugin === 'sim') {
46
+ return simState(host)
47
+ }
48
+ if (command.plugin !== undefined) {
49
+ const state = plugins.get(command.plugin)
50
+ if (state === undefined) {
51
+ throw new Error(`No plugin '${command.plugin}'. Loaded: sim, ${plugins.plugins.map((plugin) => plugin.id).join(', ')}`)
52
+ }
53
+ return state
54
+ }
55
+ return { sim: simState(host), ...Object.fromEntries(plugins.plugins.map((plugin) => [plugin.id, plugins.get(plugin.id)])) }
56
+ case 'set':
57
+ if (command.plugin === 'sim') {
58
+ const { device, orientation, appearance, width, height } = command.patch as {
59
+ device?: string
60
+ orientation?: Orientation
61
+ appearance?: Appearance
62
+ width?: number
63
+ height?: number
64
+ }
65
+ if (device !== undefined) {
66
+ if (!host.devices.some((candidate) => candidate.id === device)) {
67
+ throw new Error(`No device '${device}'. Devices: ${host.devices.map((candidate) => candidate.id).join(', ')}`)
68
+ }
69
+ host.setDevice(device, origin)
70
+ }
71
+ if (orientation !== undefined) {
72
+ host.setOrientation(orientation, origin)
73
+ }
74
+ if (appearance !== undefined) {
75
+ host.setAppearance(appearance, origin)
76
+ }
77
+ if (width !== undefined || height !== undefined) {
78
+ const { screen, resizable } = host.getState().device
79
+ if (resizable === undefined) {
80
+ throw new Error(`${host.getState().device.id} has a fixed size. Resizable devices: ${host.devices.filter((candidate) => candidate.resizable).map((candidate) => candidate.id).join(', ')}`)
81
+ }
82
+ host.setScreenSize({ width: width ?? screen.width, height: height ?? screen.height }, origin)
83
+ }
84
+ return simState(host)
85
+ }
86
+ plugins.set(command.plugin, command.patch, origin)
87
+ return plugins.get(command.plugin)
88
+ case 'run':
89
+ return await plugins.run(command.plugin, command.action, command.args, origin)
90
+ }
91
+ }
@@ -0,0 +1,226 @@
1
+ import type { Actor, EventInput, SimEvent } from '../../node/events/shared.ts'
2
+
3
+ /** Who caused a change, and the command or action it is part of. Passed down from each entry point. */
4
+ export type Origin = { actor: Actor; cause?: string }
5
+
6
+ export const APP: Origin = { actor: 'app' }
7
+ export const USER: Origin = { actor: 'user' }
8
+ export const SYSTEM: Origin = { actor: 'system' }
9
+
10
+ /** An event as the shell knows it, before the dev server gives it an id and a global run. */
11
+ export type ShellEvent = Omit<SimEvent, 'id'> & { seq: number }
12
+
13
+ export type LogInput = EventInput & {
14
+ /** Strings longer than this are replaced by a marker such as `<string 34 KB>`. Default 1 KB. */
15
+ maxString?: number
16
+ }
17
+
18
+ const RING_SIZE = 5000
19
+ const FLUSH_MS = 100
20
+ const TAB_KEY = 'web-sim:tab'
21
+ const RUN_KEY = 'web-sim:tab-run'
22
+
23
+ /**
24
+ * The shell's half of the event log: stamps each event with its time, run and tab, keeps the newest few thousand for
25
+ * the shell's own UI, and sends them to the dev server in batches over Vite's websocket. The server keeps the log
26
+ * that the CLI reads.
27
+ */
28
+ export class EventLog {
29
+ readonly tab: string
30
+ /** This tab's run, bumped at each app launch or reload. The server maps it to a global run number. */
31
+ run = 0
32
+ private seq = 0
33
+ private spans = 0
34
+ private ring: ShellEvent[] = []
35
+ private pending: ShellEvent[] = []
36
+ private timer: ReturnType<typeof setTimeout> | undefined
37
+ private listeners = new Set<(event: ShellEvent) => void>()
38
+
39
+ constructor() {
40
+ this.tab = tabId()
41
+ this.run = storedRun()
42
+ }
43
+
44
+ log = (input: LogInput): ShellEvent => {
45
+ const event: ShellEvent = {
46
+ seq: ++this.seq,
47
+ time: Date.now(),
48
+ run: this.run,
49
+ tab: this.tab,
50
+ kind: input.kind,
51
+ level: input.level ?? 'info',
52
+ actor: input.actor ?? 'app',
53
+ summary: input.summary,
54
+ }
55
+ if (input.data !== undefined) {
56
+ event.data = toJson(input.data, input.maxString ?? 1024)
57
+ }
58
+ if (input.span !== undefined) {
59
+ event.span = input.span
60
+ }
61
+ if (input.cause !== undefined) {
62
+ event.cause = input.cause
63
+ }
64
+ this.ring.push(event)
65
+ if (this.ring.length > RING_SIZE) {
66
+ this.ring.splice(0, this.ring.length - RING_SIZE)
67
+ }
68
+ this.pending.push(event)
69
+ this.timer ??= setTimeout(this.flush, FLUSH_MS)
70
+ this.listeners.forEach((listener) => listener(event))
71
+ return event
72
+ }
73
+
74
+ /** A span id unique across tabs and reloads, such as `act-k3f9-12`. */
75
+ span(prefix: string): string {
76
+ return `${prefix}-${this.tab}-${++this.spans}`
77
+ }
78
+
79
+ /** A new run. The count is kept across reloads of the tab, so a reloaded shell's runs never repeat its last ones. */
80
+ startRun(): void {
81
+ this.run += 1
82
+ try {
83
+ sessionStorage.setItem(RUN_KEY, String(this.run))
84
+ } catch {
85
+ // Storage blocked: runs may repeat after a reload.
86
+ }
87
+ }
88
+
89
+ recent(): readonly ShellEvent[] {
90
+ return this.ring
91
+ }
92
+
93
+ subscribe(listener: (event: ShellEvent) => void): () => void {
94
+ this.listeners.add(listener)
95
+ return () => this.listeners.delete(listener)
96
+ }
97
+
98
+ /** Sends what is waiting now, as the page unloads. */
99
+ flush = (): void => {
100
+ clearTimeout(this.timer)
101
+ this.timer = undefined
102
+ if (this.pending.length === 0) {
103
+ return
104
+ }
105
+ const events = this.pending
106
+ this.pending = []
107
+ // Vite's client holds messages while its socket reconnects.
108
+ import.meta.hot?.send('web-sim:events', { tab: this.tab, events })
109
+ }
110
+ }
111
+
112
+ /** This tab's id: kept across reloads of the tab, new for each tab. */
113
+ function tabId(): string {
114
+ try {
115
+ const stored = sessionStorage.getItem(TAB_KEY)
116
+ if (stored !== null) {
117
+ return stored
118
+ }
119
+ const id = Math.random().toString(36).slice(2, 6)
120
+ sessionStorage.setItem(TAB_KEY, id)
121
+ return id
122
+ } catch {
123
+ return Math.random().toString(36).slice(2, 6)
124
+ }
125
+ }
126
+
127
+ function storedRun(): number {
128
+ try {
129
+ return Number(sessionStorage.getItem(RUN_KEY) ?? 0) || 0
130
+ } catch {
131
+ return 0
132
+ }
133
+ }
134
+
135
+ function kb(length: number): string {
136
+ return length >= 1024 ? `${Math.round(length / 1024)} KB` : `${length} chars`
137
+ }
138
+
139
+ /**
140
+ * A copy of any value as plain JSON, safe to send and store: strings over `maxString` become a marker, long arrays and
141
+ * objects are cut with a note of how many were left out, cycles and deep nesting stop, and what JSON has no form
142
+ * for (functions, errors, DOM nodes, maps) gets a readable stand-in. Works on values from the app's window too, so it
143
+ * checks types by their tag rather than `instanceof`.
144
+ */
145
+ export function toJson(value: unknown, maxString = 1024): unknown {
146
+ const seen = new WeakSet<object>()
147
+ const walk = (current: unknown, depth: number): unknown => {
148
+ switch (typeof current) {
149
+ case 'string':
150
+ return current.length > maxString ? `<string ${kb(current.length)}>` : current
151
+ case 'number':
152
+ return Number.isFinite(current) ? current : String(current)
153
+ case 'boolean':
154
+ return current
155
+ case 'undefined':
156
+ return '<undefined>'
157
+ case 'bigint':
158
+ return `${current}n`
159
+ case 'symbol':
160
+ return current.toString()
161
+ case 'function':
162
+ return `<function ${current.name || 'anonymous'}>`
163
+ }
164
+ if (current === null) {
165
+ return null
166
+ }
167
+ const object = current as object
168
+ if (seen.has(object)) {
169
+ return '<circular>'
170
+ }
171
+ const tag = Object.prototype.toString.call(object).slice(8, -1)
172
+ if (tag === 'Error' || (typeof (object as Error).stack === 'string' && typeof (object as Error).message === 'string')) {
173
+ const error = object as Error
174
+ return { name: error.name, message: error.message, stack: error.stack }
175
+ }
176
+ if (tag === 'Date') {
177
+ return (object as Date).toISOString()
178
+ }
179
+ if (tag === 'RegExp') {
180
+ return String(object)
181
+ }
182
+ if (typeof (object as Node).nodeType === 'number' && typeof (object as Node).nodeName === 'string') {
183
+ return `<${(object as Node).nodeName.toLowerCase()}>`
184
+ }
185
+ if (depth >= 6) {
186
+ return Array.isArray(object) ? `<array ${object.length}>` : `<${tag}>`
187
+ }
188
+ seen.add(object)
189
+ try {
190
+ if (tag === 'Map') {
191
+ return walk(Object.fromEntries([...(object as Map<unknown, unknown>)].map(([key, item]) => [String(key), item])), depth)
192
+ }
193
+ if (tag === 'Set') {
194
+ return walk([...(object as Set<unknown>)], depth)
195
+ }
196
+ if (Array.isArray(object)) {
197
+ const items = object.slice(0, 100).map((item) => walk(item, depth + 1))
198
+ return object.length > 100 ? [...items, `<${object.length - 100} more>`] : items
199
+ }
200
+ if (ArrayBuffer.isView(object) || tag === 'ArrayBuffer' || tag === 'Blob' || tag === 'File') {
201
+ const size = (object as { byteLength?: number; size?: number }).byteLength ?? (object as { size?: number }).size ?? 0
202
+ return `<${tag} ${size} bytes>`
203
+ }
204
+ const entries = Object.entries(object)
205
+ const result: Record<string, unknown> = {}
206
+ for (const [key, item] of entries.slice(0, 100)) {
207
+ result[key] = walk(item, depth + 1)
208
+ }
209
+ if (entries.length > 100) {
210
+ result['…'] = `<${entries.length - 100} more keys>`
211
+ }
212
+ return result
213
+ } catch {
214
+ return `<${tag}>`
215
+ } finally {
216
+ seen.delete(object)
217
+ }
218
+ }
219
+ return walk(value, 0)
220
+ }
221
+
222
+ /** A short, readable form of a value for a summary line: strings as they are, the rest as compact JSON, cut at `max`. */
223
+ export function brief(value: unknown, max = 60): string {
224
+ const text = typeof value === 'string' ? value : JSON.stringify(toJson(value, max)) ?? String(value)
225
+ return text.length > max ? `${text.slice(0, max - 1)}…` : text
226
+ }
@@ -0,0 +1,68 @@
1
+ import type { Device, Orientation } from '../../devices/types.ts'
2
+
3
+ export type Rect = { x: number; y: number; width: number; height: number }
4
+
5
+ export type Side = 'left' | 'right' | 'top' | 'bottom'
6
+
7
+ export type FrameButton = { side: Side; offset: number; length: number }
8
+
9
+ /**
10
+ * Where everything sits in the drawn device, in points from the frame's top left. Landscape is the device turned
11
+ * a quarter to the left, as iOS and Android turn it: the top of the screen, with its cutout, goes to the left.
12
+ */
13
+ export type FrameLayout = {
14
+ frame: { width: number; height: number; radius: number }
15
+ screen: Rect & { radius: number }
16
+ buttons: FrameButton[]
17
+ /** The home button's centre and size, on devices with one. */
18
+ homeButton: { x: number; y: number; size: number } | null
19
+ }
20
+
21
+ /** Side buttons in portrait: offsets and lengths along the side, from the top, as fractions of the frame's height. */
22
+ const BUTTONS: Record<Device['platform'], { side: 'left' | 'right'; offset: number; length: number }[]> = {
23
+ ios: [
24
+ { side: 'left', offset: 0.165, length: 0.035 },
25
+ { side: 'left', offset: 0.235, length: 0.07 },
26
+ { side: 'left', offset: 0.32, length: 0.07 },
27
+ { side: 'right', offset: 0.26, length: 0.11 },
28
+ ],
29
+ android: [
30
+ { side: 'right', offset: 0.2, length: 0.07 },
31
+ { side: 'right', offset: 0.31, length: 0.13 },
32
+ ],
33
+ }
34
+
35
+ export function screenSize(device: Device, orientation: Orientation): { width: number; height: number } {
36
+ const { width, height } = device.screen
37
+ return orientation === 'portrait' ? { width, height } : { width: height, height: width }
38
+ }
39
+
40
+ export function frameLayout(device: Device, orientation: Orientation): FrameLayout {
41
+ const { bezel, chin = 0 } = device.frame
42
+ const portraitWidth = device.screen.width + bezel * 2
43
+ const portraitHeight = device.screen.height + bezel * 2 + chin * 2
44
+ const radius = device.cornerRadius > 0 ? device.cornerRadius + bezel : 56
45
+ const portraitButtons = BUTTONS[device.platform].map((button) => ({
46
+ side: button.side as Side,
47
+ offset: button.offset * portraitHeight,
48
+ length: button.length * portraitHeight,
49
+ }))
50
+ const homeButtonSize = chin > 0 ? Math.min(chin * 0.62, 64) : 0
51
+
52
+ if (orientation === 'portrait') {
53
+ return {
54
+ frame: { width: portraitWidth, height: portraitHeight, radius },
55
+ screen: { x: bezel, y: bezel + chin, ...device.screen, radius: device.cornerRadius },
56
+ buttons: portraitButtons,
57
+ homeButton: chin > 0 ? { x: portraitWidth / 2, y: portraitHeight - chin / 2 - bezel / 2, size: homeButtonSize } : null,
58
+ }
59
+ }
60
+ // Turned left: the portrait left side becomes the bottom, the right side the top, and a point that was `y` from
61
+ // the top is now `y` from the left.
62
+ return {
63
+ frame: { width: portraitHeight, height: portraitWidth, radius },
64
+ screen: { x: bezel + chin, y: bezel, width: device.screen.height, height: device.screen.width, radius: device.cornerRadius },
65
+ buttons: portraitButtons.map((button) => ({ ...button, side: button.side === 'left' ? 'bottom' : 'top' })),
66
+ homeButton: chin > 0 ? { x: portraitHeight - chin / 2 - bezel / 2, y: portraitWidth / 2, size: homeButtonSize } : null,
67
+ }
68
+ }