@belte/belte 0.19.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.
Files changed (326) hide show
  1. package/CHANGELOG.md +313 -0
  2. package/LICENSE +21 -0
  3. package/README.md +559 -0
  4. package/bin/belte.ts +183 -0
  5. package/package.json +110 -0
  6. package/src/App.svelte +31 -0
  7. package/src/appEntry.ts +151 -0
  8. package/src/assets/app.html +14 -0
  9. package/src/belteResolverPlugin.ts +858 -0
  10. package/src/build.ts +147 -0
  11. package/src/buildCli.ts +129 -0
  12. package/src/buildDisconnected.ts +122 -0
  13. package/src/bundleApp.ts +149 -0
  14. package/src/bundleDisconnectedEntry.ts +17 -0
  15. package/src/cliEntry.ts +25 -0
  16. package/src/clientBuildPlugins.ts +41 -0
  17. package/src/clientEntry.ts +7 -0
  18. package/src/compile.ts +64 -0
  19. package/src/controlServerWorker.ts +422 -0
  20. package/src/dedupeSveltePlugin.ts +66 -0
  21. package/src/devEntry.ts +169 -0
  22. package/src/discoveryEntry.ts +81 -0
  23. package/src/lib/browser/applyStreamedResolution.ts +33 -0
  24. package/src/lib/browser/cacheEntryFromSnapshot.ts +48 -0
  25. package/src/lib/browser/flushUnresolvedPlaceholders.ts +16 -0
  26. package/src/lib/browser/installStreamingPlaceholders.ts +32 -0
  27. package/src/lib/browser/openResolveStream.ts +42 -0
  28. package/src/lib/browser/page.svelte.ts +258 -0
  29. package/src/lib/browser/pageStreamController.ts +17 -0
  30. package/src/lib/browser/refetchPlaceholder.ts +12 -0
  31. package/src/lib/browser/remoteProxy.ts +37 -0
  32. package/src/lib/browser/socketChannel.ts +192 -0
  33. package/src/lib/browser/socketProxy.ts +57 -0
  34. package/src/lib/browser/startClient.ts +153 -0
  35. package/src/lib/browser/subscribe.ts +131 -0
  36. package/src/lib/browser/types/Errors.ts +9 -0
  37. package/src/lib/browser/types/Layouts.ts +7 -0
  38. package/src/lib/browser/types/Pages.ts +7 -0
  39. package/src/lib/browser/types/StreamingDeferred.ts +9 -0
  40. package/src/lib/bundle/BundleMenu.ts +11 -0
  41. package/src/lib/bundle/BundleMenuItem.ts +24 -0
  42. package/src/lib/bundle/BundleWindow.ts +36 -0
  43. package/src/lib/bundle/WEBVIEW_BUILD_REVISION.ts +9 -0
  44. package/src/lib/bundle/WEBVIEW_VERSION.ts +7 -0
  45. package/src/lib/bundle/bindConnectedFlag.ts +29 -0
  46. package/src/lib/bundle/bindRequestNavigate.ts +31 -0
  47. package/src/lib/bundle/buildWebviewLib.ts +111 -0
  48. package/src/lib/bundle/disconnected.css +9 -0
  49. package/src/lib/bundle/disconnected.svelte +386 -0
  50. package/src/lib/bundle/ensureWebviewLib.ts +20 -0
  51. package/src/lib/bundle/exitWithParent.ts +28 -0
  52. package/src/lib/bundle/infoPlist.ts +46 -0
  53. package/src/lib/bundle/installDownloads.ts +24 -0
  54. package/src/lib/bundle/installMacMenu.ts +39 -0
  55. package/src/lib/bundle/listenLocalControlServer.ts +19 -0
  56. package/src/lib/bundle/native/belteMenu.mm +422 -0
  57. package/src/lib/bundle/native/webview.h +4557 -0
  58. package/src/lib/bundle/onMenu.ts +41 -0
  59. package/src/lib/bundle/openWebview.ts +104 -0
  60. package/src/lib/bundle/pngToIcns.ts +47 -0
  61. package/src/lib/bundle/probeBelteServer.ts +34 -0
  62. package/src/lib/bundle/resolveServerBinary.ts +12 -0
  63. package/src/lib/bundle/resolveWebviewLib.ts +53 -0
  64. package/src/lib/bundle/serverBinaryFilename.ts +8 -0
  65. package/src/lib/bundle/signMacApp.ts +35 -0
  66. package/src/lib/bundle/spawnEmbeddedServer.ts +65 -0
  67. package/src/lib/bundle/stableLocalPort.ts +19 -0
  68. package/src/lib/bundle/waitForServer.ts +23 -0
  69. package/src/lib/bundle/webviewCachePath.ts +23 -0
  70. package/src/lib/bundle/webviewLibName.ts +11 -0
  71. package/src/lib/cli/connectToServer.ts +23 -0
  72. package/src/lib/cli/createClient.ts +170 -0
  73. package/src/lib/cli/dispatchCommand.ts +71 -0
  74. package/src/lib/cli/loadEnvFromBinaryDir.ts +16 -0
  75. package/src/lib/cli/parseArgvForRpc.ts +97 -0
  76. package/src/lib/cli/printHelp.ts +119 -0
  77. package/src/lib/cli/printSessionHelp.ts +27 -0
  78. package/src/lib/cli/printSessionStatus.ts +21 -0
  79. package/src/lib/cli/printTrimmed.ts +8 -0
  80. package/src/lib/cli/printValue.ts +10 -0
  81. package/src/lib/cli/resolveCliTarget.ts +48 -0
  82. package/src/lib/cli/runCli.ts +139 -0
  83. package/src/lib/cli/runSession.ts +105 -0
  84. package/src/lib/cli/startLocalInstance.ts +14 -0
  85. package/src/lib/cli/tokenizeLine.ts +51 -0
  86. package/src/lib/cli/types/CliManifest.ts +9 -0
  87. package/src/lib/cli/types/CliManifestEntry.ts +17 -0
  88. package/src/lib/cli/types/CliTarget.ts +13 -0
  89. package/src/lib/mcp/annotationsForMethod.ts +29 -0
  90. package/src/lib/mcp/createMcpResourceServer.ts +101 -0
  91. package/src/lib/mcp/createMcpServer.ts +42 -0
  92. package/src/lib/mcp/dispatchMcpRequest.ts +146 -0
  93. package/src/lib/mcp/mcpResourceServerSlot.ts +18 -0
  94. package/src/lib/mcp/mcpSurface.ts +265 -0
  95. package/src/lib/mcp/toolResultFromResponse.ts +66 -0
  96. package/src/lib/mcp/types/JsonRpcRequest.ts +12 -0
  97. package/src/lib/mcp/types/JsonRpcResponse.ts +20 -0
  98. package/src/lib/mcp/types/McpResourceContents.ts +10 -0
  99. package/src/lib/mcp/types/McpResourceDescriptor.ts +6 -0
  100. package/src/lib/mcp/types/McpResourceServer.ts +12 -0
  101. package/src/lib/mcp/types/McpServer.ts +9 -0
  102. package/src/lib/mcp/types/McpServerOptions.ts +16 -0
  103. package/src/lib/server/AppModule.ts +33 -0
  104. package/src/lib/server/DELETE.ts +9 -0
  105. package/src/lib/server/GET.ts +9 -0
  106. package/src/lib/server/HEAD.ts +9 -0
  107. package/src/lib/server/PATCH.ts +9 -0
  108. package/src/lib/server/POST.ts +9 -0
  109. package/src/lib/server/PUT.ts +9 -0
  110. package/src/lib/server/agent.ts +76 -0
  111. package/src/lib/server/appDataDir.ts +15 -0
  112. package/src/lib/server/cli/buildEnvContent.ts +19 -0
  113. package/src/lib/server/cli/createTarGz.ts +76 -0
  114. package/src/lib/server/cli/handleCliDownload.ts +153 -0
  115. package/src/lib/server/cli/handleCliInstall.ts +37 -0
  116. package/src/lib/server/cli/installScript.ts +29 -0
  117. package/src/lib/server/cli/maxSourceMtime.ts +26 -0
  118. package/src/lib/server/cookies.ts +29 -0
  119. package/src/lib/server/env.ts +50 -0
  120. package/src/lib/server/error.ts +70 -0
  121. package/src/lib/server/json.ts +28 -0
  122. package/src/lib/server/jsonl.ts +46 -0
  123. package/src/lib/server/prompts/definePrompt.ts +20 -0
  124. package/src/lib/server/prompts/promptRegistry.ts +9 -0
  125. package/src/lib/server/prompts/registerPrompt.ts +6 -0
  126. package/src/lib/server/prompts/renderPromptTemplate.ts +16 -0
  127. package/src/lib/server/prompts/types/Prompt.ts +13 -0
  128. package/src/lib/server/prompts/types/PromptOptions.ts +12 -0
  129. package/src/lib/server/prompts/types/PromptRegistryEntry.ts +13 -0
  130. package/src/lib/server/prompts/types/PromptRoutes.ts +10 -0
  131. package/src/lib/server/redirect.ts +42 -0
  132. package/src/lib/server/request.ts +18 -0
  133. package/src/lib/server/rpc/defineVerb.ts +133 -0
  134. package/src/lib/server/rpc/dispatchVerbInProcess.ts +46 -0
  135. package/src/lib/server/rpc/findVerbByCommandName.ts +18 -0
  136. package/src/lib/server/rpc/parseArgs.ts +95 -0
  137. package/src/lib/server/rpc/registerVerb.ts +6 -0
  138. package/src/lib/server/rpc/types/RemoteHandler.ts +27 -0
  139. package/src/lib/server/rpc/types/RemoteRoutes.ts +13 -0
  140. package/src/lib/server/rpc/types/TypedResponse.ts +18 -0
  141. package/src/lib/server/rpc/types/VerbHelper.ts +68 -0
  142. package/src/lib/server/rpc/types/VerbRegistryEntry.ts +29 -0
  143. package/src/lib/server/rpc/unprocessed.ts +14 -0
  144. package/src/lib/server/rpc/verbRegistry.ts +11 -0
  145. package/src/lib/server/runtime/DEFAULT_PORT.ts +6 -0
  146. package/src/lib/server/runtime/DEV_REBUILD_MESSAGE.ts +4 -0
  147. package/src/lib/server/runtime/DEV_RELOAD_CLIENT_SCRIPT.ts +29 -0
  148. package/src/lib/server/runtime/acceptsZstd.ts +8 -0
  149. package/src/lib/server/runtime/buildOpenApiSpec.ts +106 -0
  150. package/src/lib/server/runtime/cacheControlForAsset.ts +22 -0
  151. package/src/lib/server/runtime/containsTraversal.ts +37 -0
  152. package/src/lib/server/runtime/createAssetHeaderCache.ts +35 -0
  153. package/src/lib/server/runtime/createPublicAssetServer.ts +63 -0
  154. package/src/lib/server/runtime/createRouteDispatcher.ts +100 -0
  155. package/src/lib/server/runtime/createServer.ts +692 -0
  156. package/src/lib/server/runtime/devReloadResponse.ts +35 -0
  157. package/src/lib/server/runtime/disableIdleTimeoutForStream.ts +27 -0
  158. package/src/lib/server/runtime/envSchemaStore.ts +15 -0
  159. package/src/lib/server/runtime/findOpenPort.ts +35 -0
  160. package/src/lib/server/runtime/getActiveServer.ts +6 -0
  161. package/src/lib/server/runtime/globToPathSet.ts +29 -0
  162. package/src/lib/server/runtime/inProcessServer.ts +20 -0
  163. package/src/lib/server/runtime/internalErrorResponse.ts +25 -0
  164. package/src/lib/server/runtime/isCrossOriginUpgrade.ts +19 -0
  165. package/src/lib/server/runtime/listenOnOpenPort.ts +36 -0
  166. package/src/lib/server/runtime/logExposedSurfaces.ts +162 -0
  167. package/src/lib/server/runtime/mimeForExtension.ts +20 -0
  168. package/src/lib/server/runtime/parseIdleTimeout.ts +10 -0
  169. package/src/lib/server/runtime/parsePort.ts +11 -0
  170. package/src/lib/server/runtime/registryManifests.ts +66 -0
  171. package/src/lib/server/runtime/requestContext.ts +5 -0
  172. package/src/lib/server/runtime/resolveStreamResponse.ts +29 -0
  173. package/src/lib/server/runtime/runWithRequestScope.ts +57 -0
  174. package/src/lib/server/runtime/safeJsonForScript.ts +17 -0
  175. package/src/lib/server/runtime/serializeCacheSnapshot.ts +45 -0
  176. package/src/lib/server/runtime/serverSlot.ts +13 -0
  177. package/src/lib/server/runtime/setActiveServer.ts +6 -0
  178. package/src/lib/server/runtime/snapshotEntryFromCache.ts +81 -0
  179. package/src/lib/server/runtime/streamCacheResolutions.ts +37 -0
  180. package/src/lib/server/runtime/streamFromIterator.ts +86 -0
  181. package/src/lib/server/runtime/streamStash.ts +64 -0
  182. package/src/lib/server/runtime/types/Assets.ts +1 -0
  183. package/src/lib/server/runtime/types/RequestStore.ts +27 -0
  184. package/src/lib/server/runtime/withResponseDefaults.ts +24 -0
  185. package/src/lib/server/server.ts +32 -0
  186. package/src/lib/server/socket.ts +31 -0
  187. package/src/lib/server/sockets/createSocketDispatcher.ts +311 -0
  188. package/src/lib/server/sockets/defineSocket.ts +167 -0
  189. package/src/lib/server/sockets/lookupSocket.ts +6 -0
  190. package/src/lib/server/sockets/recentHistory.ts +11 -0
  191. package/src/lib/server/sockets/registerSocket.ts +6 -0
  192. package/src/lib/server/sockets/socketOperations.ts +35 -0
  193. package/src/lib/server/sockets/socketRegistry.ts +9 -0
  194. package/src/lib/server/sockets/types/Socket.ts +21 -0
  195. package/src/lib/server/sockets/types/SocketClientFrame.ts +18 -0
  196. package/src/lib/server/sockets/types/SocketOperation.ts +22 -0
  197. package/src/lib/server/sockets/types/SocketOptions.ts +22 -0
  198. package/src/lib/server/sockets/types/SocketRegistryEntry.ts +17 -0
  199. package/src/lib/server/sockets/types/SocketRoutes.ts +10 -0
  200. package/src/lib/server/sockets/types/SocketServerFrame.ts +15 -0
  201. package/src/lib/server/sse.ts +53 -0
  202. package/src/lib/shared/BELTE_PACKAGE_NAME.ts +7 -0
  203. package/src/lib/shared/CACHE_CONTROL_VALUES.ts +16 -0
  204. package/src/lib/shared/HttpError.ts +19 -0
  205. package/src/lib/shared/RESOLVE_STREAM_PATH.ts +7 -0
  206. package/src/lib/shared/STREAMING_CONTENT_TYPES.ts +11 -0
  207. package/src/lib/shared/activeCacheStore.ts +20 -0
  208. package/src/lib/shared/appDataDir.ts +34 -0
  209. package/src/lib/shared/belteImportName.ts +44 -0
  210. package/src/lib/shared/browserClientFlags.ts +10 -0
  211. package/src/lib/shared/buildRpcRequest.ts +70 -0
  212. package/src/lib/shared/bundleLayout.ts +36 -0
  213. package/src/lib/shared/bundled.ts +34 -0
  214. package/src/lib/shared/cache.ts +559 -0
  215. package/src/lib/shared/cacheStoreSlot.ts +16 -0
  216. package/src/lib/shared/canonicalJson.ts +63 -0
  217. package/src/lib/shared/carriesBodyArgs.ts +13 -0
  218. package/src/lib/shared/clearLastConnection.ts +7 -0
  219. package/src/lib/shared/commandNameForUrl.ts +17 -0
  220. package/src/lib/shared/createCacheStore.ts +75 -0
  221. package/src/lib/shared/createPushIterator.ts +93 -0
  222. package/src/lib/shared/createRemoteFunction.ts +99 -0
  223. package/src/lib/shared/decodeResponse.ts +47 -0
  224. package/src/lib/shared/detectTarget.ts +27 -0
  225. package/src/lib/shared/exeSuffix.ts +9 -0
  226. package/src/lib/shared/exitOnBuildFailure.ts +17 -0
  227. package/src/lib/shared/extraForwardHeaders.ts +16 -0
  228. package/src/lib/shared/fileStem.ts +9 -0
  229. package/src/lib/shared/findExportCallSite.ts +479 -0
  230. package/src/lib/shared/forwardHeaders.ts +41 -0
  231. package/src/lib/shared/getRemoteMeta.ts +5 -0
  232. package/src/lib/shared/globalCacheStore.ts +15 -0
  233. package/src/lib/shared/globalCacheStoreSlot.ts +14 -0
  234. package/src/lib/shared/importNamesToStrip.ts +13 -0
  235. package/src/lib/shared/invalidateEvent.ts +11 -0
  236. package/src/lib/shared/isCompileTarget.ts +15 -0
  237. package/src/lib/shared/isDebugEnabled.ts +23 -0
  238. package/src/lib/shared/isModuleNotFound.ts +16 -0
  239. package/src/lib/shared/isReadOnlyMethod.ts +14 -0
  240. package/src/lib/shared/isStreamingResponse.ts +11 -0
  241. package/src/lib/shared/jsonSchemaForPromptArguments.ts +29 -0
  242. package/src/lib/shared/jsonSchemaForSchema.ts +32 -0
  243. package/src/lib/shared/jsonlErrorFrame.ts +24 -0
  244. package/src/lib/shared/keyForRemoteCall.ts +29 -0
  245. package/src/lib/shared/lastConnectionPath.ts +7 -0
  246. package/src/lib/shared/loadEnvFile.ts +17 -0
  247. package/src/lib/shared/loadEnvFromDataDir.ts +15 -0
  248. package/src/lib/shared/loadSvelteConfig.ts +18 -0
  249. package/src/lib/shared/log.ts +104 -0
  250. package/src/lib/shared/manifestModule.ts +39 -0
  251. package/src/lib/shared/memoizeByKey.ts +24 -0
  252. package/src/lib/shared/nearestLayoutPrefix.ts +36 -0
  253. package/src/lib/shared/normalizeTarget.ts +10 -0
  254. package/src/lib/shared/pageUrlForFile.ts +14 -0
  255. package/src/lib/shared/parseBoundedEnvInt.ts +20 -0
  256. package/src/lib/shared/parseEnv.ts +30 -0
  257. package/src/lib/shared/parsePromptMarkdown.ts +34 -0
  258. package/src/lib/shared/parseRouteSegments.ts +22 -0
  259. package/src/lib/shared/prepareRpcModule.ts +59 -0
  260. package/src/lib/shared/prepareSocketModule.ts +49 -0
  261. package/src/lib/shared/programNameForPackage.ts +14 -0
  262. package/src/lib/shared/promptNameForFile.ts +10 -0
  263. package/src/lib/shared/queryStringFromArgs.ts +27 -0
  264. package/src/lib/shared/readEnvFile.ts +15 -0
  265. package/src/lib/shared/readLastConnection.ts +18 -0
  266. package/src/lib/shared/readPackageJson.ts +9 -0
  267. package/src/lib/shared/recordRemoteMeta.ts +5 -0
  268. package/src/lib/shared/remoteMetaStore.ts +16 -0
  269. package/src/lib/shared/resolveClientFlags.ts +20 -0
  270. package/src/lib/shared/responseErrorText.ts +9 -0
  271. package/src/lib/shared/rpcUrlForFile.ts +19 -0
  272. package/src/lib/shared/runningAsStandaloneBinary.ts +13 -0
  273. package/src/lib/shared/serializeEnv.ts +18 -0
  274. package/src/lib/shared/setCacheStoreResolver.ts +6 -0
  275. package/src/lib/shared/setGlobalCacheStoreResolver.ts +6 -0
  276. package/src/lib/shared/socketNameForFile.ts +11 -0
  277. package/src/lib/shared/sseErrorFrame.ts +29 -0
  278. package/src/lib/shared/streamResponse.ts +169 -0
  279. package/src/lib/shared/stripImport.ts +27 -0
  280. package/src/lib/shared/subscribableFromResponse.ts +51 -0
  281. package/src/lib/shared/toBunRoutePattern.ts +28 -0
  282. package/src/lib/shared/types/CacheEntry.ts +63 -0
  283. package/src/lib/shared/types/CacheInvalidation.ts +9 -0
  284. package/src/lib/shared/types/CacheOptions.ts +33 -0
  285. package/src/lib/shared/types/CacheSnapshot.ts +16 -0
  286. package/src/lib/shared/types/CacheSnapshotEntry.ts +15 -0
  287. package/src/lib/shared/types/CacheStore.ts +32 -0
  288. package/src/lib/shared/types/ClientFlags.ts +11 -0
  289. package/src/lib/shared/types/CompileTarget.ts +6 -0
  290. package/src/lib/shared/types/HttpVerb.ts +1 -0
  291. package/src/lib/shared/types/LastConnection.ts +9 -0
  292. package/src/lib/shared/types/PromptArgument.ts +12 -0
  293. package/src/lib/shared/types/RawRemoteFunction.ts +13 -0
  294. package/src/lib/shared/types/RemoteFunction.ts +42 -0
  295. package/src/lib/shared/types/StandardSchemaV1.ts +57 -0
  296. package/src/lib/shared/types/StreamedResolution.ts +10 -0
  297. package/src/lib/shared/types/StreamingPlaceholder.ts +13 -0
  298. package/src/lib/shared/types/Subscribable.ts +15 -0
  299. package/src/lib/shared/types/SvelteConfig.ts +5 -0
  300. package/src/lib/shared/withJsonSchema.ts +20 -0
  301. package/src/lib/shared/writeLastConnection.ts +13 -0
  302. package/src/lib/shared/writeRoutesDts.ts +67 -0
  303. package/src/lib/test/clearVerbRegistry.ts +11 -0
  304. package/src/lib/test/createTestClient.ts +78 -0
  305. package/src/preload.ts +20 -0
  306. package/src/scaffold.ts +92 -0
  307. package/src/serverBuildPlugins.ts +25 -0
  308. package/src/serverEntry.ts +94 -0
  309. package/src/sveltePlugin.ts +58 -0
  310. package/src/tailwindStylePreprocessor.ts +62 -0
  311. package/template/bunfig.toml +4 -0
  312. package/template/package.json +19 -0
  313. package/template/src/app.ts +23 -0
  314. package/template/src/browser/app.css +21 -0
  315. package/template/src/browser/app.html +24 -0
  316. package/template/src/browser/pages/about/page.svelte +5 -0
  317. package/template/src/browser/pages/layout.svelte +26 -0
  318. package/template/src/browser/pages/page.svelte +20 -0
  319. package/template/src/bundle/icon.png +0 -0
  320. package/template/src/cli/banner.txt +3 -0
  321. package/template/src/cli/footer.txt +1 -0
  322. package/template/src/server/config.ts +17 -0
  323. package/template/src/server/rpc/getHello.ts +35 -0
  324. package/template/svelte.config.js +12 -0
  325. package/template/tsconfig.json +18 -0
  326. package/tsconfig.app.json +16 -0
@@ -0,0 +1,41 @@
1
+ /*
2
+ Subscribes to bundle menu clicks. Each custom menu item declared in the bundle
3
+ window config dispatches a `belte:menu` CustomEvent into the page when clicked.
4
+ Two forms, both returning an unsubscribe so they drop straight into a Svelte
5
+ `$effect`:
6
+
7
+ // catch-all — every emit name flows through one handler
8
+ $effect(() =>
9
+ onMenu((name) => {
10
+ if (name === 'reload') location.reload()
11
+ }),
12
+ )
13
+
14
+ // filtered — handler fires only for the named item
15
+ $effect(() => onMenu('reload', () => location.reload()))
16
+
17
+ Inert during SSR and in a plain browser tab — `$effect` only runs client-side,
18
+ the native menu that fires the event exists only in the bundled desktop app,
19
+ and `window` is guarded so importing the module never assumes a DOM.
20
+ */
21
+ export function onMenu(handler: (name: string) => void): () => void
22
+ export function onMenu(name: string, handler: () => void): () => void
23
+ export function onMenu(
24
+ nameOrHandler: string | ((name: string) => void),
25
+ maybeHandler?: () => void,
26
+ ): () => void {
27
+ if (typeof window === 'undefined') {
28
+ return () => {}
29
+ }
30
+ // String first arg = filter to that emit name; otherwise a catch-all handler.
31
+ const filter = typeof nameOrHandler === 'string' ? nameOrHandler : undefined
32
+ const handler = typeof nameOrHandler === 'string' ? maybeHandler : nameOrHandler
33
+ function listener(event: Event) {
34
+ const name = (event as CustomEvent<{ name: string }>).detail.name
35
+ if (filter === undefined || filter === name) {
36
+ handler?.(name)
37
+ }
38
+ }
39
+ window.addEventListener('belte:menu', listener)
40
+ return () => window.removeEventListener('belte:menu', listener)
41
+ }
@@ -0,0 +1,104 @@
1
+ import { dlopen, FFIType, type Pointer } from 'bun:ffi'
2
+ import type { BundleMenu } from './BundleMenu.ts'
3
+ import { installDownloads } from './installDownloads.ts'
4
+ import { installMacMenu } from './installMacMenu.ts'
5
+ import { resolveWebviewLib } from './resolveWebviewLib.ts'
6
+
7
+ // WEBVIEW_HINT_NONE — the window is freely resizable (the only hint we need).
8
+ const WEBVIEW_HINT_NONE = 0
9
+
10
+ /*
11
+ Encodes a string as a NUL-terminated UTF-8 buffer for the C ABI. bun:ffi
12
+ passes a TypedArray to a `ptr` argument as a raw pointer, and the webview
13
+ C functions expect NUL-terminated `const char *`.
14
+ */
15
+ function cString(value: string): Uint8Array {
16
+ return new TextEncoder().encode(`${value}\0`)
17
+ }
18
+
19
+ /*
20
+ Opens a native OS webview window pointed at `url` and blocks until the
21
+ user closes it. This drives the platform UI run loop (WebKit on macOS,
22
+ WebView2 on Windows, WebKitGTK on Linux) via FFI against the webview C
23
+ library — no Chromium is bundled. Because `webview_run` enters a blocking
24
+ native event loop on the calling thread, the belte server must already be
25
+ running in a separate process; this call owns the main thread until the
26
+ window closes, then destroys the handle and releases the library.
27
+ */
28
+ export async function openWebview({
29
+ url,
30
+ title,
31
+ width = 1024,
32
+ height = 768,
33
+ menu,
34
+ fileMenu,
35
+ onWindow,
36
+ }: {
37
+ url: string
38
+ title: string
39
+ width?: number
40
+ height?: number
41
+ menu?: BundleMenu[]
42
+ // The File menu, inserted before Edit — the launcher's Start/Disconnect.
43
+ fileMenu?: BundleMenu
44
+ /*
45
+ Hands back the window handle once it exists, before the run loop blocks the
46
+ thread. The launcher forwards it to its control-server worker so the worker
47
+ can navigate the window from off-thread (e.g. bounce back to the connect
48
+ screen when the connected server dies).
49
+ */
50
+ onWindow?: (handle: Pointer | null) => void
51
+ }): Promise<void> {
52
+ const libPath = await resolveWebviewLib()
53
+ const { symbols, close } = dlopen(libPath, {
54
+ webview_create: { args: [FFIType.i32, FFIType.ptr], returns: FFIType.ptr },
55
+ webview_set_title: { args: [FFIType.ptr, FFIType.ptr], returns: FFIType.void },
56
+ webview_set_size: {
57
+ args: [FFIType.ptr, FFIType.i32, FFIType.i32, FFIType.i32],
58
+ returns: FFIType.void,
59
+ },
60
+ webview_init: { args: [FFIType.ptr, FFIType.ptr], returns: FFIType.i32 },
61
+ webview_navigate: { args: [FFIType.ptr, FFIType.ptr], returns: FFIType.void },
62
+ webview_run: { args: [FFIType.ptr], returns: FFIType.void },
63
+ webview_destroy: { args: [FFIType.ptr], returns: FFIType.void },
64
+ })
65
+
66
+ /*
67
+ First arg is the webview's `debug` flag: 1 enables the native inspector
68
+ (WKWebView's Web Inspector, WebView2 DevTools, WebKitGTK inspector) so a JS
69
+ error on the loaded page — otherwise silent in a bare bundle window — can be
70
+ read via right-click → Inspect. Gated behind BELTE_INSPECT so release bundles
71
+ ship without it. The second arg is an optional parent handle; null = fresh window.
72
+ */
73
+ const debug = process.env.BELTE_INSPECT ? 1 : 0
74
+ const handle = symbols.webview_create(debug, null)
75
+ symbols.webview_set_title(handle, cString(title))
76
+ symbols.webview_set_size(handle, width, height, WEBVIEW_HINT_NONE)
77
+ /*
78
+ Install the macOS menu bar (no-op off macOS) after the application exists
79
+ but before the run loop starts, so Quit and the Edit shortcuts work — the
80
+ upstream webview omits the menu entirely — plus the bundle's custom menus.
81
+ */
82
+ installMacMenu(libPath, handle, title, menu, fileMenu)
83
+ /*
84
+ Attach the download delegate (no-op off macOS / before 11.3) before the
85
+ first navigation, so `<a download>`, blob/data links, and attachment
86
+ responses save a file instead of being silently dropped by the bare webview.
87
+ */
88
+ installDownloads(libPath, handle)
89
+ onWindow?.(handle)
90
+
91
+ /*
92
+ Stamp the bundle marker before navigation so it runs ahead of page scripts on
93
+ every document the webview loads — local embedded server or a remote one. This
94
+ is what makes browser-side bundled() report "in the webview" regardless of who
95
+ served the page; a plain browser tab (even hitting the embedded localhost
96
+ server) never runs this, so it stays false there.
97
+ */
98
+ symbols.webview_init(handle, cString('window.__BELTE_BUNDLE__=true'))
99
+ symbols.webview_navigate(handle, cString(url))
100
+ // Blocks here, running the native UI loop, until the window is closed.
101
+ symbols.webview_run(handle)
102
+ symbols.webview_destroy(handle)
103
+ close()
104
+ }
@@ -0,0 +1,47 @@
1
+ import { log } from '../shared/log.ts'
2
+
3
+ /*
4
+ The conventional macOS `.iconset` contents — each variant is a square PNG
5
+ at the named pixel size. `iconutil` packs a directory of exactly these
6
+ into a multi-resolution `.icns`. @2x entries are the retina variants.
7
+ */
8
+ const ICONSET_VARIANTS = [
9
+ { name: 'icon_16x16.png', size: 16 },
10
+ { name: 'icon_16x16@2x.png', size: 32 },
11
+ { name: 'icon_32x32.png', size: 32 },
12
+ { name: 'icon_32x32@2x.png', size: 64 },
13
+ { name: 'icon_128x128.png', size: 128 },
14
+ { name: 'icon_128x128@2x.png', size: 256 },
15
+ { name: 'icon_256x256.png', size: 256 },
16
+ { name: 'icon_256x256@2x.png', size: 512 },
17
+ { name: 'icon_512x512.png', size: 512 },
18
+ { name: 'icon_512x512@2x.png', size: 1024 },
19
+ ]
20
+
21
+ /*
22
+ Converts a PNG into a macOS `.icns` using the system `sips` + `iconutil`
23
+ tools, which ship with macOS. `sips` resizes the source into each iconset
24
+ variant; `iconutil` packs the iconset directory into the `.icns`. Returns
25
+ true on success. On any failure (tools missing, unreadable source) it logs
26
+ a warning and returns false so the bundle still completes without an icon
27
+ rather than aborting the whole build.
28
+ */
29
+ export async function pngToIcns(pngPath: string, outPath: string): Promise<boolean> {
30
+ const iconset = `${outPath}.iconset`
31
+ try {
32
+ await Bun.$`mkdir -p ${iconset}`.quiet()
33
+ await Promise.all(
34
+ ICONSET_VARIANTS.map(({ name, size }) =>
35
+ Bun.$`sips -z ${size} ${size} ${pngPath} --out ${`${iconset}/${name}`}`.quiet(),
36
+ ),
37
+ )
38
+ await Bun.$`iconutil -c icns ${iconset} -o ${outPath}`.quiet()
39
+ return true
40
+ } catch (error) {
41
+ log.warn(`could not convert ${pngPath} to .icns — bundling without an icon`)
42
+ log.error(error)
43
+ return false
44
+ } finally {
45
+ await Bun.$`rm -rf ${iconset}`.quiet()
46
+ }
47
+ }
@@ -0,0 +1,34 @@
1
+ // The identity shape a belte server returns from GET /__belte/identity.
2
+ export type BelteIdentity = { name: string; version: string }
3
+
4
+ /*
5
+ Confirms a URL points at a belte server before the launcher navigates the app
6
+ window there, by fetching its unauthenticated identity endpoint. Returns the
7
+ server's identity on success, or undefined when nothing belte answers — a network
8
+ error, the wrong port, or a non-belte page (a bare 403/404, a different app). The
9
+ endpoint bypasses the app's own middleware, so an auth-guarded belte app still
10
+ verifies here even though its pages would later redirect to a login.
11
+ */
12
+ export async function probeBelteServer(target: string): Promise<BelteIdentity | undefined> {
13
+ try {
14
+ const base = target.replace(/\/+$/, '')
15
+ const response = await fetch(`${base}/__belte/identity`, {
16
+ headers: { accept: 'application/json' },
17
+ signal: AbortSignal.timeout(5000),
18
+ })
19
+ if (!response.ok) {
20
+ return undefined
21
+ }
22
+ const body = (await response.json()) as {
23
+ belte?: boolean
24
+ name?: string
25
+ version?: string
26
+ }
27
+ if (body.belte !== true) {
28
+ return undefined
29
+ }
30
+ return { name: body.name ?? 'belte app', version: body.version ?? '0.0.0' }
31
+ } catch {
32
+ return undefined
33
+ }
34
+ }
@@ -0,0 +1,12 @@
1
+ import { dirname, join } from 'node:path'
2
+ import { serverBinaryFilename } from './serverBinaryFilename.ts'
3
+
4
+ /*
5
+ Locates the embedded server binary that ships beside the launcher inside a
6
+ bundle. The launcher's own path is `process.execPath` (the compiled binary
7
+ itself), so the server sits in the same directory — true for both the
8
+ flat-directory layout and a macOS `.app`'s `Contents/MacOS/`.
9
+ */
10
+ export function resolveServerBinary(): string {
11
+ return join(dirname(process.execPath), serverBinaryFilename())
12
+ }
@@ -0,0 +1,53 @@
1
+ import { dirname, join } from 'node:path'
2
+ import { bundleLayout } from '../shared/bundleLayout.ts'
3
+ import { webviewCachePath } from './webviewCachePath.ts'
4
+ import { webviewLibName } from './webviewLibName.ts'
5
+
6
+ /*
7
+ Locates the native webview shared library to load over FFI, without ever
8
+ compiling — this runs in the compiled launcher too, where no toolchain is
9
+ present. Resolution order:
10
+
11
+ 1. BELTE_WEBVIEW_LIB — explicit path, the escape hatch for any layout.
12
+ 2. inside a bundle — beside the launcher binary (flat layout) or in
13
+ `../Frameworks` (macOS `.app`), so a shipped bundle is self-contained.
14
+ 3. belte's own build cache — the library compiled from the vendored
15
+ header by buildWebviewLib (populated at build time via ensureWebviewLib).
16
+
17
+ belte ships the vendored source rather than a prebuilt binary, so the
18
+ toolchain path (`belte bundle`) calls ensureWebviewLib to build-on-miss;
19
+ this resolver only reports what already exists. Throws with
20
+ guidance when nothing resolves rather than letting dlopen fail opaquely.
21
+ */
22
+ export async function resolveWebviewLib(cwd: string = process.cwd()): Promise<string> {
23
+ const fromEnv = process.env.BELTE_WEBVIEW_LIB
24
+ if (fromEnv) {
25
+ return fromEnv
26
+ }
27
+
28
+ const libName = webviewLibName()
29
+
30
+ /*
31
+ Bundle-relative candidates. In dev `process.execPath` is the `bun`
32
+ binary, so these miss and we fall through to the build cache; in a
33
+ shipped bundle the launcher's own directory holds the lib (flat layout)
34
+ or its sibling Frameworks dir (macOS `.app`) — bundleLayout knows which.
35
+ */
36
+ const { binDir, libDir } = bundleLayout(dirname(process.execPath))
37
+ const bundledCandidates = [join(binDir, libName), join(libDir, libName)]
38
+ for (const candidate of bundledCandidates) {
39
+ if (await Bun.file(candidate).exists()) {
40
+ return candidate
41
+ }
42
+ }
43
+
44
+ const cached = webviewCachePath()
45
+ if (await Bun.file(cached).exists()) {
46
+ return cached
47
+ }
48
+
49
+ throw new Error(
50
+ '[belte] no native webview library found. Run `belte bundle` to ' +
51
+ 'build it from the vendored source, or set BELTE_WEBVIEW_LIB to a prebuilt one.',
52
+ )
53
+ }
@@ -0,0 +1,8 @@
1
+ /*
2
+ Filename of the embedded server binary that ships beside the launcher
3
+ inside a bundle. Both the bundler (which writes it) and the launcher
4
+ (which spawns it) derive the name here so they can't drift apart.
5
+ */
6
+ export function serverBinaryFilename(platform: NodeJS.Platform = process.platform): string {
7
+ return platform === 'win32' ? 'server.exe' : 'server'
8
+ }
@@ -0,0 +1,35 @@
1
+ import { log } from '../shared/log.ts'
2
+
3
+ /*
4
+ Ad-hoc code-signs an assembled macOS `.app` so it launches on other Macs.
5
+
6
+ Apple Silicon mandates a valid code signature for every executable. `bun
7
+ build --compile` emits an ad-hoc, linker-signed binary, but assembling the
8
+ `.app` around it (writing Info.plist, dropping in the lib) leaves the bundle
9
+ unsealed — `codesign --verify` then reports the signature as modified, and a
10
+ copy that picks up a quarantine flag (AirDrop, USB, download) gets silently
11
+ killed by Gatekeeper/AMFI: the icon bounces once and nothing opens.
12
+
13
+ Re-signing inside-out fixes that. Nested Mach-O code (the webview dylib, the
14
+ embedded server binary, the launcher) is signed first, then the bundle as a
15
+ whole, which seals Resources and binds Info.plist. The identity is `-`,
16
+ ad-hoc: no certificate, no Developer account, no network — as far as signing
17
+ goes without a paid Developer ID. Recipients copying a quarantined bundle
18
+ still need `xattr -cr <app>` once, but the app no longer fails to launch.
19
+
20
+ Best-effort: if `codesign` is missing or fails, warn and return rather than
21
+ abort the bundle, which is otherwise complete and usable on the build host.
22
+ */
23
+ export async function signMacApp(bundleRoot: string, innerPaths: string[]): Promise<void> {
24
+ try {
25
+ // Inner Mach-O code inside-out, then the bundle, which re-signs the
26
+ // main executable as part of sealing — order matters for nested seals.
27
+ for (const path of innerPaths) {
28
+ await Bun.$`codesign --force --sign - ${path}`.quiet()
29
+ }
30
+ await Bun.$`codesign --force --sign - ${bundleRoot}`.quiet()
31
+ } catch (error) {
32
+ log.warn(`could not code-sign ${bundleRoot} — it may not launch when copied to another Mac`)
33
+ log.error(error)
34
+ }
35
+ }
@@ -0,0 +1,65 @@
1
+ import { dirname, join } from 'node:path'
2
+ import { DEFAULT_PORT } from '../server/runtime/DEFAULT_PORT.ts'
3
+ import { findOpenPort } from '../server/runtime/findOpenPort.ts'
4
+ import { parsePort } from '../server/runtime/parsePort.ts'
5
+ import { appDataDir } from '../shared/appDataDir.ts'
6
+ import { bundleLayout } from '../shared/bundleLayout.ts'
7
+ import { readEnvFile } from '../shared/readEnvFile.ts'
8
+ import { resolveServerBinary } from './resolveServerBinary.ts'
9
+ import { waitForServer } from './waitForServer.ts'
10
+
11
+ /*
12
+ The port the embedded server binds. A `PORT` from the shell, the data-dir `.env`
13
+ (where the config form writes), or the shipped binary-dir `.env` is honored — so
14
+ the server answers at a fixed, known address another machine can reliably connect
15
+ to. With none set, the first open port at/above 3000 is chosen (matching the
16
+ standalone server's default). Precedence matches the server's own env stack:
17
+ shell > data-dir > binary-dir. A configured port is used as-is — if it's taken,
18
+ the bind failure surfaces rather than silently moving.
19
+ */
20
+ async function resolveEmbeddedPort(programName: string): Promise<number> {
21
+ const [dataDirEnv, binaryDirEnv] = await Promise.all([
22
+ readEnvFile(join(appDataDir(programName), '.env')),
23
+ readEnvFile(bundleLayout(dirname(process.execPath)).envPath),
24
+ ])
25
+ return (
26
+ parsePort(process.env.PORT ?? dataDirEnv.PORT ?? binaryDirEnv.PORT) ??
27
+ findOpenPort(DEFAULT_PORT)
28
+ )
29
+ }
30
+
31
+ /*
32
+ Spawns the sibling server binary on a free port and waits for it to answer,
33
+ returning the live URL plus the child so the caller owns its lifetime (reaping on
34
+ disconnect/exit). Readiness is raced against the child's exit so a server that
35
+ crashes on boot (missing config) surfaces immediately instead of stalling out
36
+ waitForServer's full timeout; the loser branch resolves (never rejects) so it
37
+ can't surface as an unhandled rejection once the child is later reaped. Does not
38
+ reap a previous child — the caller owns that.
39
+ */
40
+ export async function spawnEmbeddedServer({
41
+ programName,
42
+ timeoutMs,
43
+ }: {
44
+ programName: string
45
+ timeoutMs?: number
46
+ }): Promise<{ url: string; child: ReturnType<typeof Bun.spawn> }> {
47
+ const port = await resolveEmbeddedPort(programName)
48
+ const url = `http://localhost:${port}`
49
+ const child = Bun.spawn({
50
+ cmd: [resolveServerBinary()],
51
+ // BELTE_PARENT_PID lets the child exit if the parent is force-quit (a clean
52
+ // shutdown reaps it directly). The server resolves its own config from its
53
+ // data-dir/binary-dir .env at boot, so nothing else is injected.
54
+ env: { ...process.env, PORT: String(port), BELTE_PARENT_PID: String(process.pid) },
55
+ stdio: ['inherit', 'inherit', 'inherit'],
56
+ })
57
+ const outcome = await Promise.race([
58
+ waitForServer(url, timeoutMs ? { timeoutMs } : undefined).then(() => undefined),
59
+ child.exited,
60
+ ])
61
+ if (outcome !== undefined) {
62
+ throw new Error(`[belte] embedded server exited (code ${outcome}) before binding`)
63
+ }
64
+ return { url, child }
65
+ }
@@ -0,0 +1,19 @@
1
+ /*
2
+ Derives a deterministic localhost port from the program name so the connect
3
+ screen's origin (and thus its localStorage) stays stable across launches — a
4
+ remembered server URL survives a relaunch only if the page is reloaded from the
5
+ same origin. Hashes the name with FNV-1a (32-bit) and maps it into the
6
+ dynamic/private range (49152–65535). The caller probes availability and falls
7
+ back to a random free port on collision, so determinism is a best effort, not a
8
+ guarantee.
9
+ */
10
+ export function stableLocalPort(programName: string): number {
11
+ // FNV-1a 32-bit: offset basis 2166136261, prime 16777619. Math.imul keeps
12
+ // the multiply in 32-bit space; `>>> 0` reads the result back as unsigned.
13
+ let hash = 0x811c9dc5
14
+ for (const character of programName) {
15
+ hash ^= character.charCodeAt(0)
16
+ hash = Math.imul(hash, 0x01000193)
17
+ }
18
+ return 49152 + ((hash >>> 0) % 16384)
19
+ }
@@ -0,0 +1,23 @@
1
+ /*
2
+ Polls an HTTP URL until it answers (any status) or the deadline passes.
3
+ The spawned server child binds asynchronously, so the launcher can't open
4
+ the webview until a request round-trips. A connection refusal throws and
5
+ is swallowed; once Bun.serve is listening the fetch resolves and we
6
+ return. Throws on timeout so the launcher can report a failed boot rather
7
+ than open a blank window.
8
+ */
9
+ export async function waitForServer(
10
+ url: string,
11
+ { timeoutMs = 10_000, intervalMs = 50 }: { timeoutMs?: number; intervalMs?: number } = {},
12
+ ): Promise<void> {
13
+ const deadline = Bun.nanoseconds() + timeoutMs * 1e6
14
+ while (Bun.nanoseconds() < deadline) {
15
+ try {
16
+ await fetch(url)
17
+ return
18
+ } catch {
19
+ await Bun.sleep(intervalMs)
20
+ }
21
+ }
22
+ throw new Error(`[belte] server did not become ready at ${url} within ${timeoutMs}ms`)
23
+ }
@@ -0,0 +1,23 @@
1
+ import { join } from 'node:path'
2
+ import { WEBVIEW_BUILD_REVISION } from './WEBVIEW_BUILD_REVISION.ts'
3
+ import { WEBVIEW_VERSION } from './WEBVIEW_VERSION.ts'
4
+ import { webviewLibName } from './webviewLibName.ts'
5
+
6
+ /*
7
+ Absolute path where the locally built webview library is cached. belte
8
+ compiles the vendored `native/webview.h` once per host and reuses the
9
+ result; both buildWebviewLib (the writer) and resolveWebviewLib (a reader)
10
+ derive the location here so they never drift.
11
+
12
+ The cache sits next to the vendored source inside the belte package, so it
13
+ is shared across every project on the machine that uses this belte install
14
+ and survives independently of any consumer's `cwd`. Namespacing by
15
+ platform + arch + upstream version keeps a single cache correct across
16
+ architectures and makes a header bump — or a belte native-build bump — select
17
+ a fresh path automatically.
18
+ */
19
+ export function webviewCachePath(): string {
20
+ const nativeDir = new URL('./native', import.meta.url).pathname
21
+ const key = `${process.platform}-${process.arch}-${WEBVIEW_VERSION}-${WEBVIEW_BUILD_REVISION}`
22
+ return join(nativeDir, '.cache', key, webviewLibName())
23
+ }
@@ -0,0 +1,11 @@
1
+ import { suffix } from 'bun:ffi'
2
+
3
+ /*
4
+ Native webview shared-library filename for a platform. `suffix` is Bun's
5
+ host shared-library extension (`dylib`/`so`/`dll`). The bundler copies a
6
+ file under this name and the loader looks for it under the same name, so
7
+ both derive it here.
8
+ */
9
+ export function webviewLibName(platform: NodeJS.Platform = process.platform): string {
10
+ return platform === 'win32' ? `webview.${suffix}` : `libwebview.${suffix}`
11
+ }
@@ -0,0 +1,23 @@
1
+ import { probeBelteServer } from '../bundle/probeBelteServer.ts'
2
+ import { log } from '../shared/log.ts'
3
+ import { writeLastConnection } from '../shared/writeLastConnection.ts'
4
+ import type { CliTarget } from './types/CliTarget.ts'
5
+
6
+ /*
7
+ Connects to a remote belte server: probes its identity endpoint first so we never
8
+ record or talk to a non-belte URL, then persists the intent so the next bare run
9
+ resumes here. Carries the env bearer token (baked or shell) for authed servers.
10
+ Returns the target, or undefined when nothing belte answers.
11
+ */
12
+ export async function connectToServer(
13
+ programName: string,
14
+ url: string,
15
+ ): Promise<CliTarget | undefined> {
16
+ const identity = await probeBelteServer(url)
17
+ if (!identity) {
18
+ log.warn(`no belte server responded at ${url}`)
19
+ return undefined
20
+ }
21
+ await writeLastConnection(programName, { kind: 'url', url })
22
+ return { url, token: process.env.BELTE_APP_TOKEN, name: identity.name }
23
+ }