@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
package/bin/belte.ts ADDED
@@ -0,0 +1,183 @@
1
+ #!/usr/bin/env bun
2
+ import { build } from '../src/build.ts'
3
+ import { buildCli } from '../src/buildCli.ts'
4
+ import { bundleApp } from '../src/bundleApp.ts'
5
+ import { compile } from '../src/compile.ts'
6
+ import { normalizeTarget } from '../src/lib/shared/normalizeTarget.ts'
7
+ import { scaffold } from '../src/scaffold.ts'
8
+
9
+ const PRELOAD = new URL('../src/preload.ts', import.meta.url).pathname
10
+ const SERVER_ENTRY = new URL('../src/serverEntry.ts', import.meta.url).pathname
11
+ const DEV_ENTRY = new URL('../src/devEntry.ts', import.meta.url).pathname
12
+ const cwd = process.cwd()
13
+ const [, , command, ...rest] = process.argv
14
+
15
+ // Reads `--name=value` or `--name value` from the trailing argv tail.
16
+ function parseFlag(name: string): string | undefined {
17
+ const prefix = `--${name}=`
18
+ const match = rest.find((arg) => arg.startsWith(prefix))
19
+ if (match) {
20
+ return match.slice(prefix.length)
21
+ }
22
+ const index = rest.indexOf(`--${name}`)
23
+ if (index !== -1 && index + 1 < rest.length) {
24
+ return rest[index + 1]
25
+ }
26
+ return undefined
27
+ }
28
+
29
+ /*
30
+ Runs a long-lived child (server, job, script) and owns its shutdown. Ctrl+C
31
+ delivers SIGINT to the whole foreground process group, so without a parent
32
+ handler the parent's default action kills it instantly — abandoning the
33
+ `await child.exited` and orphaning the child, which (for a server) can then
34
+ linger holding the port. Forwarding the signal and awaiting the child's exit
35
+ (with a SIGKILL watchdog for a wedged child) guarantees the child is reaped
36
+ before the parent leaves. Mirrors the child's exit code so callers and CI see
37
+ the real result.
38
+ */
39
+ async function runChild(cmd: string[]): Promise<never> {
40
+ const child = Bun.spawn({ cmd, cwd, stdio: ['inherit', 'inherit', 'inherit'] })
41
+ const forward = (signal: NodeJS.Signals) => {
42
+ child.kill(signal)
43
+ setTimeout(() => child.kill('SIGKILL'), 3000).unref()
44
+ }
45
+ process.on('SIGINT', () => forward('SIGINT'))
46
+ process.on('SIGTERM', () => forward('SIGTERM'))
47
+ process.exit(await child.exited)
48
+ }
49
+
50
+ /*
51
+ Runs the dev orchestrator (devEntry) — not `bun --watch`. The orchestrator owns
52
+ the loop: it builds the client, spawns the server as a child on a fixed dev
53
+ port, watches src/ recursively, and on any change rebuilds + restarts the child.
54
+ The server mounts a live-reload channel under dev, so the browser reloads itself
55
+ when the restarted server comes back. runChild forwards Ctrl+C so the
56
+ orchestrator (and its server child) shut down cleanly.
57
+ */
58
+ async function dev(): Promise<void> {
59
+ await runChild(['bun', '--preload', PRELOAD, DEV_ENTRY])
60
+ }
61
+
62
+ // Performs a single client build with no server attached (for CI / static deploys).
63
+ async function buildOnce(): Promise<void> {
64
+ await build({ cwd })
65
+ }
66
+
67
+ // Starts the production server against an already-built dist directory.
68
+ async function start(): Promise<void> {
69
+ await runChild(['bun', '--preload', PRELOAD, SERVER_ENTRY])
70
+ }
71
+
72
+ /*
73
+ Runs an arbitrary script under the belte preload — same runtime as the server,
74
+ so jobs/scripts get .svelte compilation, belte/* + $server/$shared resolution,
75
+ and the .css no-op loader for free. Everything after `run` is forwarded
76
+ verbatim: the first token is the script, the rest are its argv (bun stops
77
+ parsing its own flags at the script path).
78
+ */
79
+ async function runCmd(): Promise<void> {
80
+ if (rest.length === 0) {
81
+ console.error('usage: belte run <file> [args...]')
82
+ process.exit(1)
83
+ }
84
+ await runChild(['bun', '--preload', PRELOAD, ...rest])
85
+ }
86
+
87
+ // Parses the --target and --out flags and produces a standalone executable.
88
+ async function compileCmd(): Promise<void> {
89
+ const targetFlag = parseFlag('target')
90
+ const outFlag = parseFlag('out')
91
+ await compile({
92
+ cwd,
93
+ target: targetFlag ? normalizeTarget(targetFlag) : undefined,
94
+ outfile: outFlag,
95
+ })
96
+ }
97
+
98
+ // Builds the standalone CLI binary — a thin remote client (manifest baked in)
99
+ // that ships the compiled server beside it, so it can talk to a remote server
100
+ // or spawn a local instance (`<name> start`). Discovery walks the rpc registry
101
+ // to bake the manifest in. `--platforms a,b,c` cross-compiles per target into
102
+ // dist/cli-thin/<platform>/ (cli + server) — the layout the /__belte/cli
103
+ // download endpoint streams. For just the server, use `belte compile`.
104
+ async function cliCmd(): Promise<void> {
105
+ const targetFlag = parseFlag('target')
106
+ const outFlag = parseFlag('out')
107
+ const platformsFlag = parseFlag('platforms')
108
+ const platforms = platformsFlag
109
+ ? platformsFlag.split(',').map((value) => normalizeTarget(value.trim()))
110
+ : undefined
111
+ await buildCli({
112
+ cwd,
113
+ target: targetFlag ? normalizeTarget(targetFlag) : undefined,
114
+ outfile: outFlag,
115
+ platforms,
116
+ })
117
+ }
118
+
119
+ // Assembles a movable, self-contained app bundle for the host platform —
120
+ // the server binary, the launcher, and the webview lib together (a .app on
121
+ // macOS, a flat directory elsewhere). Unsigned; for distribution to other
122
+ // users the bundle still needs platform signing/notarization.
123
+ async function bundleCmd(): Promise<void> {
124
+ await bundleApp({ cwd })
125
+ }
126
+
127
+ // Scaffolds the bundled template into a new project directory.
128
+ async function scaffoldCmd(): Promise<void> {
129
+ const name = rest.find((arg) => !arg.startsWith('--'))
130
+ if (!name) {
131
+ console.error('usage: bunx belte scaffold <project-name>')
132
+ process.exit(1)
133
+ }
134
+ await scaffold({ cwd, name })
135
+ }
136
+
137
+ // Prints the CLI synopsis to stderr and exits non-zero. Marked `never` because the process is gone.
138
+ function usage(): never {
139
+ console.error(
140
+ 'usage:\n' +
141
+ ' bunx belte scaffold <project-name> scaffold a new belte project\n' +
142
+ ' belte dev build + run with hot reload\n' +
143
+ ' belte build build the client into dist/_app/\n' +
144
+ ' belte start run the production server against dist/\n' +
145
+ ' belte run <file> [args...] run a script under the belte preload\n' +
146
+ ' (jobs, one-off scripts — same runtime as\n' +
147
+ ' the server). For tests, add\n' +
148
+ ' preload = ["@belte/belte/preload"] under\n' +
149
+ ' [test] in bunfig.toml and use `bun test`\n' +
150
+ ' belte compile [--target=<bun-...>] [--out=<path>]\n' +
151
+ ' build a standalone server executable\n' +
152
+ ' belte cli [--target=<bun-...>] [--out=<path>] [--platforms=<a,b,c>]\n' +
153
+ ' build the cli binary — a thin remote client that\n' +
154
+ ' ships the server beside it (connect to a remote\n' +
155
+ ' server or `start` a local instance; --platforms\n' +
156
+ ' cross-compiles per platform)\n' +
157
+ ' belte bundle build a movable, self-contained app\n' +
158
+ ' bundle for this platform (unsigned). Boots\n' +
159
+ ' into a connect screen — start the embedded\n' +
160
+ ' server or connect to a remote one',
161
+ )
162
+ process.exit(1)
163
+ }
164
+
165
+ if (command === 'scaffold') {
166
+ await scaffoldCmd()
167
+ } else if (command === 'dev') {
168
+ await dev()
169
+ } else if (command === 'build') {
170
+ await buildOnce()
171
+ } else if (command === 'start') {
172
+ await start()
173
+ } else if (command === 'run') {
174
+ await runCmd()
175
+ } else if (command === 'compile') {
176
+ await compileCmd()
177
+ } else if (command === 'cli') {
178
+ await cliCmd()
179
+ } else if (command === 'bundle') {
180
+ await bundleCmd()
181
+ } else {
182
+ usage()
183
+ }
package/package.json ADDED
@@ -0,0 +1,110 @@
1
+ {
2
+ "name": "@belte/belte",
3
+ "version": "0.19.0",
4
+ "type": "module",
5
+ "description": "Isomorphic multimodal HTTP framework built for humans and machines in a single Bun runtime",
6
+ "license": "MIT",
7
+ "author": "Brian Cray",
8
+ "homepage": "https://github.com/briancray/belte#readme",
9
+ "repository": {
10
+ "type": "git",
11
+ "url": "git+https://github.com/briancray/belte.git",
12
+ "directory": "packages/belte"
13
+ },
14
+ "bugs": {
15
+ "url": "https://github.com/briancray/belte/issues"
16
+ },
17
+ "keywords": [
18
+ "bun",
19
+ "svelte",
20
+ "ssr",
21
+ "spa",
22
+ "framework",
23
+ "isomorphic",
24
+ "rpc",
25
+ "mcp",
26
+ "cli"
27
+ ],
28
+ "engines": {
29
+ "bun": ">=1.3.0"
30
+ },
31
+ "publishConfig": {
32
+ "access": "public",
33
+ "provenance": true
34
+ },
35
+ "exports": {
36
+ "./server/GET": "./src/lib/server/GET.ts",
37
+ "./server/POST": "./src/lib/server/POST.ts",
38
+ "./server/PUT": "./src/lib/server/PUT.ts",
39
+ "./server/PATCH": "./src/lib/server/PATCH.ts",
40
+ "./server/DELETE": "./src/lib/server/DELETE.ts",
41
+ "./server/HEAD": "./src/lib/server/HEAD.ts",
42
+ "./server/socket": "./src/lib/server/socket.ts",
43
+ "./server/json": "./src/lib/server/json.ts",
44
+ "./server/jsonl": "./src/lib/server/jsonl.ts",
45
+ "./server/sse": "./src/lib/server/sse.ts",
46
+ "./server/error": "./src/lib/server/error.ts",
47
+ "./server/redirect": "./src/lib/server/redirect.ts",
48
+ "./server/request": "./src/lib/server/request.ts",
49
+ "./server/cookies": "./src/lib/server/cookies.ts",
50
+ "./server/env": "./src/lib/server/env.ts",
51
+ "./server/appDataDir": "./src/lib/server/appDataDir.ts",
52
+ "./server/server": "./src/lib/server/server.ts",
53
+ "./server/AppModule": "./src/lib/server/AppModule.ts",
54
+ "./server/agent": "./src/lib/server/agent.ts",
55
+ "./server/rpc/defineVerb": "./src/lib/server/rpc/defineVerb.ts",
56
+ "./server/sockets/defineSocket": "./src/lib/server/sockets/defineSocket.ts",
57
+ "./server/prompts/definePrompt": "./src/lib/server/prompts/definePrompt.ts",
58
+ "./browser/page": "./src/lib/browser/page.svelte.ts",
59
+ "./browser/navigate": "./src/lib/browser/page.svelte.ts",
60
+ "./browser/subscribe": "./src/lib/browser/subscribe.ts",
61
+ "./shared/bundled": "./src/lib/shared/bundled.ts",
62
+ "./shared/cache": "./src/lib/shared/cache.ts",
63
+ "./shared/HttpError": "./src/lib/shared/HttpError.ts",
64
+ "./shared/withJsonSchema": "./src/lib/shared/withJsonSchema.ts",
65
+ "./shared/log": "./src/lib/shared/log.ts",
66
+ "./mcp/createMcpServer": "./src/lib/mcp/createMcpServer.ts",
67
+ "./bundle/BundleWindow": "./src/lib/bundle/BundleWindow.ts",
68
+ "./bundle/BundleMenu": "./src/lib/bundle/BundleMenu.ts",
69
+ "./bundle/BundleMenuItem": "./src/lib/bundle/BundleMenuItem.ts",
70
+ "./bundle/onMenu": "./src/lib/bundle/onMenu.ts",
71
+ "./test/createTestClient": "./src/lib/test/createTestClient.ts",
72
+ "./test/clearVerbRegistry": "./src/lib/test/clearVerbRegistry.ts",
73
+ "./tsconfig": "./tsconfig.app.json",
74
+ "./build": "./src/build.ts",
75
+ "./compile": "./src/compile.ts",
76
+ "./preload": "./src/preload.ts",
77
+ "./svelte-plugin": "./src/sveltePlugin.ts",
78
+ "./resolver-plugin": "./src/belteResolverPlugin.ts",
79
+ "./browser/remoteProxy": "./src/lib/browser/remoteProxy.ts",
80
+ "./browser/socketProxy": "./src/lib/browser/socketProxy.ts",
81
+ "./server/prompts/renderPromptTemplate": "./src/lib/server/prompts/renderPromptTemplate.ts"
82
+ },
83
+ "bin": {
84
+ "belte": "./bin/belte.ts"
85
+ },
86
+ "files": [
87
+ "src",
88
+ "bin",
89
+ "template",
90
+ "tsconfig.app.json",
91
+ "CHANGELOG.md"
92
+ ],
93
+ "peerDependencies": {
94
+ "svelte": "^5.0.0",
95
+ "bun-plugin-tailwind": "*",
96
+ "tailwindcss": "^4.0.0"
97
+ },
98
+ "peerDependenciesMeta": {
99
+ "bun-plugin-tailwind": {
100
+ "optional": true
101
+ },
102
+ "tailwindcss": {
103
+ "optional": true
104
+ }
105
+ },
106
+ "devDependencies": {
107
+ "bun-plugin-tailwind": "^0.1.2",
108
+ "tailwindcss": "^4.0.0"
109
+ }
110
+ }
package/src/App.svelte ADDED
@@ -0,0 +1,31 @@
1
+ <script lang="ts">
2
+ import type { Component } from 'svelte'
3
+ import type { Page as PageState } from './lib/browser/page.svelte.ts'
4
+
5
+ let {
6
+ state,
7
+ }: {
8
+ state: {
9
+ page: PageState
10
+ render: { Layout: Component | undefined; Page: Component | undefined }
11
+ }
12
+ } = $props()
13
+
14
+ let Layout = $derived(state.render.Layout)
15
+ let PageView = $derived(state.render.Page)
16
+ let params = $derived(state.page.params)
17
+ </script>
18
+
19
+ {#if Layout}
20
+ <Layout>
21
+ {#if PageView}
22
+ {#key PageView}
23
+ <PageView {...params} />
24
+ {/key}
25
+ {/if}
26
+ </Layout>
27
+ {:else if PageView}
28
+ {#key PageView}
29
+ <PageView {...params} />
30
+ {/key}
31
+ {/if}
@@ -0,0 +1,151 @@
1
+ // @ts-expect-error virtual module resolved by belteResolverPlugin
2
+ import { disconnectedHtml } from './_virtual/bundle-disconnected.ts'
3
+ // @ts-expect-error virtual module resolved by belteResolverPlugin
4
+ import bundleWindow from './_virtual/bundle-window.ts'
5
+ // @ts-expect-error virtual module resolved by belteResolverPlugin
6
+ import programName from './_virtual/cli-name.ts'
7
+ import type { BundleMenu } from './lib/bundle/BundleMenu.ts'
8
+ import type { BundleWindow } from './lib/bundle/BundleWindow.ts'
9
+ import { openWebview } from './lib/bundle/openWebview.ts'
10
+ import { envSchemaStore } from './lib/server/runtime/envSchemaStore.ts'
11
+ import { jsonSchemaForSchema } from './lib/shared/jsonSchemaForSchema.ts'
12
+ import { log } from './lib/shared/log.ts'
13
+
14
+ /*
15
+ Compiled bundle launcher entry — the executable a bundle runs. Instead of a blank
16
+ window it boots into a connect screen, letting the user either connect to a remote
17
+ server by URL or start the embedded server binary that ships beside this launcher.
18
+
19
+ The connect screen is served by a tiny control server, but that server can't live
20
+ on this main thread: `openWebview` calls `webview_run`, a native UI loop that
21
+ blocks the thread (and its JS event loop) until the window closes, so an
22
+ in-process `Bun.serve` would never answer. The control server runs in a Worker
23
+ instead (see controlServerWorker.ts) and hands back the origin to point the window
24
+ at; it also owns the embedded-server child and the native menu flag, since neither
25
+ can be driven from a main thread frozen inside webview_run.
26
+
27
+ Bun doesn't apply this build's plugins to the worker entry, so the worker can't
28
+ import belte's virtual modules; this entry imports them (the connect-screen HTML,
29
+ the app title) and passes them in the worker's `init` message.
30
+
31
+ The window owns the main thread; on close we tell the worker to reap its child.
32
+ */
33
+ const window = bundleWindow as BundleWindow
34
+ const title = window.title ?? programName
35
+
36
+ /*
37
+ Derive the config form's JSON Schema here (the worker can't import the virtuals
38
+ that carry the schema) and pass the plain object through init — the validator
39
+ itself isn't serializable, but its JSON Schema is. Undefined when the app
40
+ declares no config, so the connect screen never gates Start.
41
+
42
+ Default source is src/server/config.ts's env schema, so one declaration drives
43
+ both boot validation and the setup form. Imported with skipValidation set — we
44
+ want the schema's shape, not a boot check the launcher has no business running.
45
+ `window.config` replaces it when set, for a bundle form that should differ from
46
+ the env schema.
47
+ */
48
+ envSchemaStore.skipValidation = true
49
+ // @ts-expect-error virtual module resolved by belteResolverPlugin
50
+ await import('./_virtual/config.ts')
51
+ const formSchema = window.config ?? envSchemaStore.schema
52
+ const configSchema = formSchema ? jsonSchemaForSchema(formSchema) : undefined
53
+
54
+ /*
55
+ Spawn the control server worker. `__BELTE_WORKER_ENTRY__` is the worker's absolute
56
+ path, injected by bundleApp via Bun's `define` so the specifier is a static literal
57
+ at build time — that's what makes `bun build --compile` embed the worker module
58
+ into the standalone binary. A relative specifier resolves against the build's cwd
59
+ (the consumer project), not this file, so it isn't found; a
60
+ `new URL(..., import.meta.url)` specifier isn't embedded at all.
61
+ */
62
+ declare const __BELTE_WORKER_ENTRY__: string
63
+ const worker = new Worker(__BELTE_WORKER_ENTRY__)
64
+
65
+ worker.addEventListener('error', (event: ErrorEvent) => {
66
+ log.error(`control server worker failed: ${event.message}`)
67
+ })
68
+
69
+ // Hand the worker the plugin-resolved data it can't import itself, then start it.
70
+ worker.postMessage({
71
+ type: 'init',
72
+ init: { disconnectedHtml: disconnectedHtml as string, title, programName, configSchema },
73
+ })
74
+
75
+ /*
76
+ The worker, once bound, resolves where the window should open and posts both the
77
+ control-server `origin` (for the File-menu action URLs) and the `target` to point
78
+ the window at now: the live server when it could resume the last connection, else
79
+ the connect screen. Resolving before this means a successful resume opens straight
80
+ at the app — no connect-screen flash — at the cost of a brief window-less moment
81
+ while it boots/probes (the OS shows the launching dock icon meanwhile).
82
+ */
83
+ const { origin, target } = await new Promise<{ origin: string; target: string }>((resolve) => {
84
+ worker.addEventListener('message', (event: MessageEvent) => {
85
+ const data = event.data as { type: string; origin?: string; target?: string }
86
+ if (data.type === 'ready' && data.origin && data.target) {
87
+ resolve({ origin: data.origin, target: data.target })
88
+ }
89
+ })
90
+ })
91
+
92
+ /*
93
+ The built-in File menu (Start / Disconnect), placed before Edit. Each item is a
94
+ `navigate` repointing the window at a control-server URL the connect screen
95
+ interprets; the internal `role` drives the native validateMenuItem: gating (Start
96
+ when disconnected, Disconnect when connected). There's no Connect item — Disconnect
97
+ already returns to the connect screen, whose form is the place to point at another
98
+ server. Roled items are launcher-only, so they carry an extra field the public
99
+ BundleMenuItem type doesn't advertise — modelled here with a local type and bridged
100
+ to BundleMenu when handed to openWebview (which only serialises the menu to JSON).
101
+ */
102
+ type FileMenuItem =
103
+ | { separator: true }
104
+ | {
105
+ label: string
106
+ shortcut?: string
107
+ navigate: string
108
+ role: 'start' | 'disconnect'
109
+ }
110
+ const fileMenu: { label: string; items: FileMenuItem[] } = {
111
+ label: 'File',
112
+ items: [
113
+ { label: 'Start Server', navigate: `${origin}/?action=start`, role: 'start' },
114
+ { separator: true },
115
+ { label: 'Disconnect', navigate: `${origin}/?action=disconnect`, role: 'disconnect' },
116
+ ],
117
+ }
118
+
119
+ log.info(`opening ${title} window at ${target}`)
120
+ await openWebview({
121
+ url: target,
122
+ title,
123
+ width: window.width,
124
+ height: window.height,
125
+ menu: window.menu,
126
+ fileMenu: fileMenu as unknown as BundleMenu,
127
+ // Forward the window handle so the worker can bounce it back to the connect
128
+ // screen if the connected server stops responding.
129
+ onWindow: (handle) => {
130
+ if (handle) {
131
+ worker.postMessage({ type: 'window', handle: Number(handle) })
132
+ }
133
+ },
134
+ })
135
+
136
+ /*
137
+ Window closed — have the worker reap the embedded server child and stop its
138
+ control server before we exit, since both live on the worker thread. Bounded by a
139
+ timeout so a wedged worker can't keep the launcher alive.
140
+ */
141
+ await new Promise<void>((resolve) => {
142
+ worker.addEventListener('message', (event: MessageEvent) => {
143
+ if ((event.data as { type?: string }).type === 'shutdownDone') {
144
+ resolve()
145
+ }
146
+ })
147
+ worker.postMessage({ type: 'shutdown' })
148
+ setTimeout(resolve, 1000)
149
+ })
150
+ worker.terminate()
151
+ process.exit(0)
@@ -0,0 +1,14 @@
1
+ <!doctype html>
2
+ <html lang="en">
3
+ <head>
4
+ <meta charset="UTF-8" />
5
+ <meta name="viewport" content="width=device-width, initial-scale=1.0" />
6
+ <link rel="stylesheet" href="/_app/client.css" />
7
+ <!--ssr:head-->
8
+ </head>
9
+ <body>
10
+ <div id="app"><!--ssr:body--></div>
11
+ <!--ssr:state-->
12
+ <script type="module" src="/_app/client.js"></script>
13
+ </body>
14
+ </html>