@plitzi/sdk-server 0.37.2 → 0.37.4

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 (359) hide show
  1. package/CHANGELOG.md +1170 -0
  2. package/README.md +100 -7
  3. package/dist/actions.d.ts +6 -2
  4. package/dist/actions.js +5 -2
  5. package/dist/adapters/cloudAdapters.js +4 -3
  6. package/dist/adapters/jsonAdapters.js +28 -6
  7. package/dist/adapters/jsonAdapters.test.d.ts +1 -0
  8. package/dist/auth.d.ts +7 -4
  9. package/dist/auth.js +8 -6
  10. package/dist/core/auth/api.d.ts +29 -1
  11. package/dist/core/auth/api.js +47 -13
  12. package/dist/core/auth/createAuth.d.ts +4 -0
  13. package/dist/core/auth/createAuth.js +3 -3
  14. package/dist/core/auth/credentials.d.ts +3 -0
  15. package/dist/core/auth/credentials.js +1 -1
  16. package/dist/core/auth/csrf.d.ts +25 -1
  17. package/dist/core/auth/csrf.js +29 -4
  18. package/dist/core/auth/renewal.d.ts +26 -0
  19. package/dist/core/auth/renewal.js +24 -0
  20. package/dist/core/auth/routes.js +8 -6
  21. package/dist/core/auth/session.d.ts +45 -0
  22. package/dist/core/auth/session.js +47 -1
  23. package/dist/core/auth/sessionActivity.d.ts +11 -0
  24. package/dist/core/auth/sessionActivity.js +14 -0
  25. package/dist/core/auth/throttle.d.ts +15 -0
  26. package/dist/core/auth/throttle.js +29 -1
  27. package/dist/core/auth/totp.d.ts +9 -1
  28. package/dist/core/auth/totp.js +11 -6
  29. package/dist/core/createServer.actions.test.d.ts +1 -0
  30. package/dist/core/createServer.d.ts +14 -1
  31. package/dist/core/createServer.functions.test.d.ts +1 -0
  32. package/dist/core/createServer.js +22 -12
  33. package/dist/core/fontStore.js +1 -1
  34. package/dist/core/handlers/authRouteHandlers.d.ts +1 -1
  35. package/dist/core/handlers/authRouteHandlers.js +57 -21
  36. package/dist/core/handlers/csrfMiddleware.d.ts +23 -0
  37. package/dist/core/handlers/csrfMiddleware.js +31 -7
  38. package/dist/core/handlers/csrfMiddleware.test.d.ts +1 -0
  39. package/dist/core/handlers/oauthRouteHandlers.js +5 -3
  40. package/dist/core/handlers/socialRouteHandlers.d.ts +1 -1
  41. package/dist/core/handlers/socialRouteHandlers.js +2 -1
  42. package/dist/core/http/dispatcher.d.ts +9 -1
  43. package/dist/core/http/dispatcher.js +38 -11
  44. package/dist/core/http/navigation.d.ts +5 -2
  45. package/dist/core/http/navigation.js +7 -4
  46. package/dist/core/http/socketResponse.d.ts +18 -0
  47. package/dist/core/http/socketResponse.js +59 -0
  48. package/dist/core/http/sse.d.ts +26 -0
  49. package/dist/core/http/sse.js +78 -0
  50. package/dist/core/http/stages/authRoutes.js +3 -1
  51. package/dist/core/http/stages/exchangeRoute.test.d.ts +1 -0
  52. package/dist/core/http/stages/middlewares.d.ts +1 -1
  53. package/dist/core/http/stages/middlewares.js +16 -10
  54. package/dist/core/http/stages/middlewares.test.d.ts +1 -0
  55. package/dist/core/http/stages/oauth.js +2 -2
  56. package/dist/core/http/stages/sessionRenewal.d.ts +16 -0
  57. package/dist/core/http/stages/sessionRenewal.js +81 -0
  58. package/dist/core/http/stages/sessionRenewal.test.d.ts +1 -0
  59. package/dist/core/http/stages/signInRoute.d.ts +2 -0
  60. package/dist/core/http/stages/signInRoute.js +139 -0
  61. package/dist/core/http/stages/signInRoute.test.d.ts +1 -0
  62. package/dist/core/http/types.d.ts +9 -0
  63. package/dist/core/http/webExchange.d.ts +18 -0
  64. package/dist/core/http/webExchange.js +53 -0
  65. package/dist/core/oauth/authorize.js +37 -13
  66. package/dist/core/oauth/consentPage.js +11 -0
  67. package/dist/core/oauth/issueContext.d.ts +14 -0
  68. package/dist/core/oauth/issueContext.js +32 -0
  69. package/dist/core/oauth/records.d.ts +7 -0
  70. package/dist/core/oauth/register.d.ts +2 -0
  71. package/dist/core/oauth/register.js +32 -11
  72. package/dist/core/oauth/register.test.d.ts +1 -0
  73. package/dist/core/oauth/token.js +21 -6
  74. package/dist/core/oauth/token.test.d.ts +1 -0
  75. package/dist/core/requestParser.d.ts +4 -0
  76. package/dist/core/requestParser.js +14 -7
  77. package/dist/core/sdkAssets.js +1 -1
  78. package/dist/core/server/baseServer.d.ts +10 -0
  79. package/dist/core/server/baseServer.js +42 -11
  80. package/dist/core/server/closeOnSignals.js +4 -3
  81. package/dist/core/server/drain.d.ts +18 -0
  82. package/dist/core/server/drain.js +62 -0
  83. package/dist/core/server/drain.test.d.ts +1 -0
  84. package/dist/core/server/fleet/channel.d.ts +103 -0
  85. package/dist/core/server/fleet/channel.js +191 -0
  86. package/dist/core/server/fleet/channel.test.d.ts +1 -0
  87. package/dist/core/server/fleet/fixtures/fleetServer.d.ts +1 -0
  88. package/dist/core/server/fleet/fleet.integration.test.d.ts +1 -0
  89. package/dist/core/server/fleet/link.d.ts +23 -0
  90. package/dist/core/server/fleet/link.js +70 -0
  91. package/dist/core/server/fleet/relay.d.ts +11 -0
  92. package/dist/core/server/fleet/relay.js +104 -0
  93. package/dist/core/server/fleet/relay.test.d.ts +1 -0
  94. package/dist/core/server/fleet/role.d.ts +46 -0
  95. package/dist/core/server/fleet/role.js +59 -0
  96. package/dist/core/server/fleet/role.test.d.ts +1 -0
  97. package/dist/core/server/fleet/stores.d.ts +7 -0
  98. package/dist/core/server/fleet/stores.js +19 -0
  99. package/dist/core/server/fleet/supervisor.d.ts +26 -0
  100. package/dist/core/server/fleet/supervisor.js +108 -0
  101. package/dist/core/server/fleet/supervisor.test.d.ts +1 -0
  102. package/dist/core/server/pageServer.js +34 -5
  103. package/dist/core/services/action.js +10 -2
  104. package/dist/core/services/functionRoutes.d.ts +9 -0
  105. package/dist/core/services/functionRoutes.js +47 -0
  106. package/dist/core/services/realtime.d.ts +8 -0
  107. package/dist/core/services/realtime.js +57 -0
  108. package/dist/core/services/registry.js +6 -2
  109. package/dist/core/staticFiles.js +12 -4
  110. package/dist/core/staticFiles.test.d.ts +1 -0
  111. package/dist/core/transports.d.ts +13 -3
  112. package/dist/core/transports.js +14 -5
  113. package/dist/functions-api.d.ts +281 -0
  114. package/dist/functions.d.ts +19 -0
  115. package/dist/functions.js +5 -0
  116. package/dist/functionsRunner.d.ts +15 -0
  117. package/dist/functionsRunner.js +6 -0
  118. package/dist/handlers.d.ts +2 -2
  119. package/dist/handlers.js +2 -2
  120. package/dist/helpers/atomicFile.d.ts +10 -0
  121. package/dist/helpers/atomicFile.js +33 -0
  122. package/dist/helpers/atomicFile.test.d.ts +1 -0
  123. package/dist/helpers/buildResponseHelpers.js +35 -11
  124. package/dist/helpers/buildServerInfo.js +4 -2
  125. package/dist/helpers/buildServerInfo.test.d.ts +1 -0
  126. package/dist/helpers/cache/CompressedFileCache.d.ts +18 -0
  127. package/dist/helpers/cache/CompressedFileCache.js +44 -0
  128. package/dist/helpers/cache/CompressedFileCache.test.d.ts +1 -0
  129. package/dist/helpers/cache/cacheManager.d.ts +1 -1
  130. package/dist/helpers/cache/cacheManager.js +2 -1
  131. package/dist/helpers/cache/cacheManager.test.d.ts +1 -0
  132. package/dist/helpers/cache/index.d.ts +1 -1
  133. package/dist/helpers/cache/keys.d.ts +15 -1
  134. package/dist/helpers/cache/keys.js +47 -5
  135. package/dist/helpers/cache/serverCaches.d.ts +7 -1
  136. package/dist/helpers/compress.d.ts +3 -1
  137. package/dist/helpers/compress.js +11 -3
  138. package/dist/helpers/hydrationPayload.d.ts +11 -0
  139. package/dist/helpers/hydrationPayload.js +51 -0
  140. package/dist/helpers/hydrationPayload.test.d.ts +1 -0
  141. package/dist/helpers/metrics.d.ts +1 -1
  142. package/dist/helpers/metrics.js +3 -2
  143. package/dist/helpers/outboundGuard.d.ts +14 -6
  144. package/dist/helpers/outboundGuard.js +132 -8
  145. package/dist/helpers/paintedCookie.d.ts +8 -0
  146. package/dist/helpers/paintedCookie.js +13 -0
  147. package/dist/helpers/reportReactBuild.d.ts +9 -0
  148. package/dist/helpers/reportReactBuild.js +18 -0
  149. package/dist/helpers/reportReactBuild.test.d.ts +1 -0
  150. package/dist/helpers/serverLog.d.ts +25 -1
  151. package/dist/helpers/serverLog.js +68 -3
  152. package/dist/helpers/serverLog.test.d.ts +1 -0
  153. package/dist/helpers/wsFrame.d.ts +3 -0
  154. package/dist/helpers/wsFrame.js +5 -0
  155. package/dist/index.d.ts +6 -1
  156. package/dist/index.js +5 -2
  157. package/dist/kernel.d.ts +2 -1
  158. package/dist/kernel.js +3 -2
  159. package/dist/modules/actions/connectorWrite.js +2 -1
  160. package/dist/modules/actions/index.d.ts +29 -10
  161. package/dist/modules/actions/index.js +43 -6
  162. package/dist/modules/actions/jobs/failureStreak.d.ts +17 -0
  163. package/dist/modules/actions/jobs/failureStreak.js +47 -0
  164. package/dist/modules/actions/jobs/failureStreak.test.d.ts +1 -0
  165. package/dist/modules/actions/jobs/index.d.ts +2 -1
  166. package/dist/modules/actions/jobs/index.js +5 -3
  167. package/dist/modules/actions/jobs/memoryQueue.d.ts +3 -0
  168. package/dist/modules/actions/jobs/memoryQueue.js +17 -1
  169. package/dist/modules/actions/jobs/scheduler.d.ts +2 -0
  170. package/dist/modules/actions/jobs/scheduler.js +12 -7
  171. package/dist/modules/actions/jobs/worker.d.ts +3 -1
  172. package/dist/modules/actions/jobs/worker.js +9 -4
  173. package/dist/modules/actions/moduleFor.js +5 -2
  174. package/dist/modules/actions/runtime/errors.d.ts +12 -0
  175. package/dist/modules/actions/runtime/errors.js +16 -1
  176. package/dist/modules/actions/runtime/kvChange.d.ts +27 -0
  177. package/dist/modules/actions/runtime/kvChange.js +23 -0
  178. package/dist/modules/actions/runtime/kvList.d.ts +51 -0
  179. package/dist/modules/actions/runtime/kvList.js +52 -0
  180. package/dist/modules/actions/runtime/kvStore.d.ts +8 -0
  181. package/dist/modules/actions/runtime/kvStore.js +46 -20
  182. package/dist/modules/actions/runtime/memoryKv.d.ts +3 -0
  183. package/dist/modules/actions/runtime/memoryKv.js +19 -1
  184. package/dist/modules/actions/runtime/namespaceKv.d.ts +4 -6
  185. package/dist/modules/actions/runtime/namespaceKv.js +15 -3
  186. package/dist/modules/actions/runtime/namespaceKv.test.d.ts +1 -0
  187. package/dist/modules/actions/runtime/precheck.js +4 -8
  188. package/dist/modules/actions/runtime/rateLimit.d.ts +19 -0
  189. package/dist/modules/actions/runtime/rateLimit.js +19 -0
  190. package/dist/modules/actions/runtime/redisKv.d.ts +27 -0
  191. package/dist/modules/actions/runtime/redisKv.integration.test.d.ts +1 -0
  192. package/dist/modules/actions/runtime/redisKv.js +46 -0
  193. package/dist/modules/actions/runtime/report.js +2 -1
  194. package/dist/modules/actions/runtime/runAction.d.ts +10 -2
  195. package/dist/modules/actions/runtime/runAction.js +52 -116
  196. package/dist/modules/actions/runtime/runLogger.d.ts +5 -0
  197. package/dist/modules/actions/runtime/runLogger.js +8 -2
  198. package/dist/modules/actions/runtime/signing.d.ts +20 -0
  199. package/dist/modules/actions/runtime/signing.js +25 -0
  200. package/dist/modules/actions/runtime/signing.test.d.ts +1 -0
  201. package/dist/modules/actions/runtime/taskContext.d.ts +33 -0
  202. package/dist/modules/actions/runtime/taskContext.js +124 -0
  203. package/dist/modules/actions/tasks/builtins.js +5 -1
  204. package/dist/modules/actions/tasks/flow.js +81 -10
  205. package/dist/modules/actions/tasks/helpers.js +2 -2
  206. package/dist/modules/actions/tasks/http.js +2 -3
  207. package/dist/modules/actions/tasks/kv.js +39 -0
  208. package/dist/modules/actions/tasks/list.d.ts +2 -0
  209. package/dist/modules/actions/tasks/list.js +145 -0
  210. package/dist/modules/actions/tasks/realtime.d.ts +2 -0
  211. package/dist/modules/actions/tasks/realtime.js +104 -0
  212. package/dist/modules/actions/tasks/registry.d.ts +10 -1
  213. package/dist/modules/actions/tasks/registry.js +17 -6
  214. package/dist/modules/actions/transport/callHandler.js +5 -2
  215. package/dist/modules/actions/transport/catalogHandler.d.ts +5 -2
  216. package/dist/modules/actions/transport/catalogHandler.js +3 -2
  217. package/dist/modules/actions/transport/stream.d.ts +4 -5
  218. package/dist/modules/actions/transport/stream.js +12 -76
  219. package/dist/modules/actions/transport/webhookHandler.js +4 -3
  220. package/dist/modules/actions/types.d.ts +86 -3
  221. package/dist/modules/connectors/engine.js +3 -5
  222. package/dist/modules/functions/build.d.ts +22 -0
  223. package/dist/modules/functions/build.js +122 -0
  224. package/dist/modules/functions/build.test.d.ts +1 -0
  225. package/dist/modules/functions/capabilities.d.ts +22 -0
  226. package/dist/modules/functions/capabilities.js +228 -0
  227. package/dist/modules/functions/config.d.ts +34 -0
  228. package/dist/modules/functions/config.js +24 -0
  229. package/dist/modules/functions/context.d.ts +17 -0
  230. package/dist/modules/functions/context.js +88 -0
  231. package/dist/modules/functions/contract.d.ts +121 -0
  232. package/dist/modules/functions/contract.js +9 -0
  233. package/dist/modules/functions/driver.d.ts +36 -0
  234. package/dist/modules/functions/driver.js +150 -0
  235. package/dist/modules/functions/load.d.ts +15 -0
  236. package/dist/modules/functions/load.js +45 -0
  237. package/dist/modules/functions/load.test.d.ts +1 -0
  238. package/dist/modules/functions/local.d.ts +29 -0
  239. package/dist/modules/functions/local.js +61 -0
  240. package/dist/modules/functions/local.test.d.ts +1 -0
  241. package/dist/modules/functions/manifest.d.ts +24 -0
  242. package/dist/modules/functions/manifest.js +163 -0
  243. package/dist/modules/functions/manifest.test.d.ts +1 -0
  244. package/dist/modules/functions/native.d.ts +8 -0
  245. package/dist/modules/functions/native.js +21 -0
  246. package/dist/modules/functions/native.test.d.ts +1 -0
  247. package/dist/modules/functions/protocol.d.ts +216 -0
  248. package/dist/modules/functions/protocol.js +18 -0
  249. package/dist/modules/functions/routes.d.ts +36 -0
  250. package/dist/modules/functions/routes.js +181 -0
  251. package/dist/modules/functions/routes.test.d.ts +1 -0
  252. package/dist/modules/functions/runner/messages.d.ts +5 -0
  253. package/dist/modules/functions/runner/messages.js +188 -0
  254. package/dist/modules/functions/runner/remote.d.ts +19 -0
  255. package/dist/modules/functions/runner/remote.js +116 -0
  256. package/dist/modules/functions/runner/remote.test.d.ts +1 -0
  257. package/dist/modules/functions/runner/service.d.ts +32 -0
  258. package/dist/modules/functions/runner/service.js +209 -0
  259. package/dist/modules/functions/sandbox/guest.d.ts +45 -0
  260. package/dist/modules/functions/sandbox/guest.js +660 -0
  261. package/dist/modules/functions/sandbox/isolate.d.ts +37 -0
  262. package/dist/modules/functions/sandbox/isolate.js +347 -0
  263. package/dist/modules/functions/sandbox/isolate.test.d.ts +1 -0
  264. package/dist/modules/functions/sandbox/prelude.d.ts +2 -0
  265. package/dist/modules/functions/sandbox/prelude.js +49 -0
  266. package/dist/modules/functions/space.d.ts +40 -0
  267. package/dist/modules/functions/space.js +162 -0
  268. package/dist/modules/functions/testing/tasksOf.d.ts +4 -0
  269. package/dist/modules/functions/tryEntry.d.ts +9 -0
  270. package/dist/modules/functions/tryEntry.js +49 -0
  271. package/dist/modules/mongo/kv.js +31 -0
  272. package/dist/modules/mysql/accounts.js +19 -9
  273. package/dist/modules/mysql/index.d.ts +2 -1
  274. package/dist/modules/mysql/jobs/index.d.ts +1 -1
  275. package/dist/modules/mysql/jobs/kv.js +30 -0
  276. package/dist/modules/mysql/jobs/schema.d.ts +9 -0
  277. package/dist/modules/mysql/jobs/schema.js +21 -4
  278. package/dist/modules/mysql/schema.js +25 -0
  279. package/dist/modules/mysql/sessions.d.ts +1 -11
  280. package/dist/modules/mysql/sessions.js +6 -1
  281. package/dist/modules/realtime/declarations.d.ts +11 -0
  282. package/dist/modules/realtime/declarations.js +26 -0
  283. package/dist/modules/realtime/grants.d.ts +23 -0
  284. package/dist/modules/realtime/grants.js +61 -0
  285. package/dist/modules/realtime/handlers.d.ts +74 -0
  286. package/dist/modules/realtime/handlers.js +224 -0
  287. package/dist/modules/realtime/hub.d.ts +62 -0
  288. package/dist/modules/realtime/hub.js +141 -0
  289. package/dist/modules/realtime/index.d.ts +51 -0
  290. package/dist/modules/realtime/index.js +62 -0
  291. package/dist/modules/realtime/memoryPubSub.d.ts +10 -0
  292. package/dist/modules/realtime/memoryPubSub.js +43 -0
  293. package/dist/modules/realtime/realtime.test.d.ts +1 -0
  294. package/dist/modules/realtime/redisPubSub.d.ts +24 -0
  295. package/dist/modules/realtime/redisPubSub.js +36 -0
  296. package/dist/modules/realtime/redisPubSub.test.d.ts +1 -0
  297. package/dist/modules/realtime/socket.d.ts +25 -0
  298. package/dist/modules/realtime/socket.js +122 -0
  299. package/dist/modules/realtime/socket.test.d.ts +1 -0
  300. package/dist/modules/rsc/handler.d.ts +16 -0
  301. package/dist/modules/rsc/handler.js +17 -10
  302. package/dist/modules/rsc/handler.test.d.ts +1 -0
  303. package/dist/modules/rsc/resolveRscData.js +2 -1
  304. package/dist/modules/runtime/bundle.d.ts +24 -0
  305. package/dist/modules/runtime/bundle.js +102 -0
  306. package/dist/modules/runtime/contract.d.ts +37 -0
  307. package/dist/modules/runtime/contract.js +21 -0
  308. package/dist/modules/runtime/entry.test.d.ts +1 -0
  309. package/dist/modules/runtime/host.d.ts +38 -0
  310. package/dist/modules/runtime/host.js +68 -0
  311. package/dist/modules/runtime/inside.d.ts +15 -0
  312. package/dist/modules/runtime/inside.js +47 -0
  313. package/dist/modules/runtime/inside.test.d.ts +1 -0
  314. package/dist/modules/runtime/runner.d.ts +11 -0
  315. package/dist/modules/runtime/runner.js +38 -0
  316. package/dist/modules/runtime/runtime.test.d.ts +1 -0
  317. package/dist/modules/runtime/stages.d.ts +50 -0
  318. package/dist/modules/runtime/stages.js +117 -0
  319. package/dist/modules/ssr/Component.d.ts +3 -1
  320. package/dist/modules/ssr/Component.js +2 -1
  321. package/dist/modules/ssr/handler.js +10 -2
  322. package/dist/modules/ssr/loadPluginComponents.js +2 -1
  323. package/dist/modules/ssr/prepareRender.js +39 -10
  324. package/dist/modules/ssr/preview.d.ts +3 -0
  325. package/dist/modules/ssr/preview.js +17 -7
  326. package/dist/modules/ssr/redirect.test.d.ts +1 -0
  327. package/dist/modules/ssr/registerExternalPlugins.d.ts +6 -1
  328. package/dist/modules/ssr/registerExternalPlugins.js +19 -16
  329. package/dist/modules/ssr/registerExternalPlugins.test.d.ts +1 -0
  330. package/dist/modules/ssr/reportMissingPlugins.d.ts +9 -0
  331. package/dist/modules/ssr/reportMissingPlugins.js +24 -0
  332. package/dist/modules/ssr/reportMissingPlugins.test.d.ts +1 -0
  333. package/dist/modules/ssr/streamBody.d.ts +2 -2
  334. package/dist/modules/ssr/streamBody.js +4 -1
  335. package/dist/modules/ssr/template.js +1 -1
  336. package/dist/modules/ssr/views/template.ejs +8 -5
  337. package/dist/mysql.d.ts +1 -1
  338. package/dist/mysql.js +2 -2
  339. package/dist/oauth.js +2 -2
  340. package/dist/plugins/compile.js +9 -4
  341. package/dist/plugins/copy.js +3 -3
  342. package/dist/plugins/manager.d.ts +35 -3
  343. package/dist/plugins/manager.js +109 -38
  344. package/dist/runtime.d.ts +15 -0
  345. package/dist/runtime.js +7 -0
  346. package/package.json +39 -8
  347. package/skills/plitzi-authoring/SKILL.md +98 -14
  348. package/skills/plitzi-authoring/reference/accessibility.md +111 -0
  349. package/skills/plitzi-authoring/reference/authoring-errors.md +70 -0
  350. package/skills/plitzi-authoring/reference/data-and-visibility.md +84 -18
  351. package/skills/plitzi-authoring/reference/elements-and-styles.md +61 -7
  352. package/skills/plitzi-authoring/reference/flows.md +108 -17
  353. package/skills/plitzi-authoring/reference/lists.md +74 -0
  354. package/skills/plitzi-authoring/reference/performance.md +67 -0
  355. package/skills/plitzi-authoring/reference/plugins.md +167 -0
  356. package/skills/plitzi-authoring/reference/review-checklist.md +19 -2
  357. package/skills/plitzi-authoring/reference/templates.md +67 -27
  358. package/skills/plitzi-authoring/reference/testing.md +72 -5
  359. package/skills/plitzi-authoring/reference/validation.md +73 -0
@@ -0,0 +1,108 @@
1
+ import { serverLog } from "../../../helpers/serverLog.js";
2
+ import { fleetWorkerEnv } from "./role.js";
3
+ import { startHub } from "./link.js";
4
+ import { FLEET_STORES } from "./stores.js";
5
+ import cluster from "node:cluster";
6
+ //#region src/core/server/fleet/supervisor.ts
7
+ var RESTART_WINDOW_MS = 6e4;
8
+ var RESTART_BASE_MS = 500;
9
+ var RESTART_MAX_MS = 3e4;
10
+ /**
11
+ * How long the replacement for a dead worker waits, given how many have died in the last minute. As many deaths as
12
+ * there are workers — each of them crashing once — are replaced at once; past that it doubles from half a second, up
13
+ * to thirty.
14
+ */
15
+ var restartDelay = (deathsInWindow, count) => deathsInWindow <= count ? 0 : Math.min(RESTART_MAX_MS, RESTART_BASE_MS * 2 ** (deathsInWindow - count - 1));
16
+ /** One fleet per process: a second server in the same primary shares the workers the first one started. */
17
+ var shared;
18
+ var exitOf = (code, signal) => signal ? `signal ${signal}` : `code ${code}`;
19
+ /**
20
+ * Starts `count` workers from this primary, each running the process's own entry point — so each builds and binds
21
+ * its own server on the shared port, and the connections are spread between them.
22
+ *
23
+ * A worker that dies is replaced, crash or kill alike, and the replacement takes over whatever the dead one carried
24
+ * (the jobs). Past one death per worker a minute the replacements wait, longer each time (`restartDelay`), so a
25
+ * request that crashes whichever worker takes it cannot turn the primary into a fork loop.
26
+ *
27
+ * Until the fleet has served at all, a death is not retried: a worker that dies before any has listened would die
28
+ * the same way again — a port in use, a broken build — so the fleet stops and the process exits non-zero for
29
+ * whatever supervises it. Once it has served, a replacement that fails to start is retried like any other death:
30
+ * the workers still serving keep serving through a passing failure (a database that is briefly down).
31
+ *
32
+ * `stop` asks every worker to finish with SIGTERM (what `closeOnSignals` answers) and waits for all of them. A
33
+ * primary that dies without stopping them takes them with it: a worker exits when its channel to the primary closes.
34
+ */
35
+ var startFleet = (count, label, exit = (code) => process.exit(code)) => {
36
+ if (shared) {
37
+ if (shared.count !== count) throw new Error(`[${label}] this process already runs ${shared.count} workers; every server in it must ask for the same number`);
38
+ shared.servers += 1;
39
+ const state = shared;
40
+ return { stop: () => leave(state) };
41
+ }
42
+ const workers = /* @__PURE__ */ new Set();
43
+ const restarts = /* @__PURE__ */ new Set();
44
+ /** When workers died, over the last minute: what decides how long the next replacement waits. */
45
+ let deaths = [];
46
+ let served = false;
47
+ let stopping = false;
48
+ const stopAll = () => {
49
+ restarts.forEach((timer) => clearTimeout(timer));
50
+ restarts.clear();
51
+ return Promise.all([...workers].map((worker) => new Promise((resolve) => {
52
+ if (worker.isDead()) {
53
+ resolve();
54
+ return;
55
+ }
56
+ worker.once("exit", () => resolve());
57
+ worker.process.kill("SIGTERM");
58
+ }))).then(() => void 0);
59
+ };
60
+ const fork = (runsJobs) => {
61
+ const worker = cluster.fork(fleetWorkerEnv(runsJobs));
62
+ workers.add(worker);
63
+ worker.once("listening", () => {
64
+ served = true;
65
+ });
66
+ worker.once("exit", (code, signal) => {
67
+ workers.delete(worker);
68
+ if (stopping) return;
69
+ if (!served) {
70
+ serverLog.error(label, `a worker stopped before it could serve (${exitOf(code, signal)}); stopping the rest`);
71
+ stopping = true;
72
+ stopAll().then(() => exit(1));
73
+ return;
74
+ }
75
+ const now = Date.now();
76
+ deaths = [...deaths.filter((at) => now - at < RESTART_WINDOW_MS), now];
77
+ const delayMs = restartDelay(deaths.length, count);
78
+ serverLog.error(label, `worker ${worker.process.pid ?? "?"} stopped (${exitOf(code, signal)}); starting another` + (delayMs > 0 ? ` in ${delayMs}ms — ${deaths.length} have stopped in the last minute` : ""));
79
+ const timer = setTimeout(() => {
80
+ restarts.delete(timer);
81
+ if (!stopping) fork(runsJobs);
82
+ }, delayMs);
83
+ restarts.add(timer);
84
+ });
85
+ };
86
+ cluster.setupPrimary({ serialization: "advanced" });
87
+ startHub(FLEET_STORES);
88
+ for (let index = 0; index < count; index += 1) fork(index === 0);
89
+ const state = {
90
+ count,
91
+ servers: 1,
92
+ stop: async () => {
93
+ stopping = true;
94
+ await stopAll();
95
+ }
96
+ };
97
+ shared = state;
98
+ return { stop: () => leave(state) };
99
+ };
100
+ /** The workers stop when the last server of this process that started them closes. */
101
+ var leave = async (state) => {
102
+ state.servers -= 1;
103
+ if (state.servers > 0) return;
104
+ if (shared === state) shared = void 0;
105
+ await state.stop();
106
+ };
107
+ //#endregion
108
+ export { restartDelay, startFleet };
@@ -0,0 +1 @@
1
+ export {};
@@ -1,11 +1,16 @@
1
+ import { configureServerLog, defaultLogLevel, isLogged, logLevelOf } from "../../helpers/serverLog.js";
2
+ import { runsFleetJobs } from "./fleet/role.js";
3
+ import { fleetStore } from "./fleet/link.js";
4
+ import { realtimeModuleFor } from "../../modules/realtime/index.js";
1
5
  import { actionsModuleFor } from "../../modules/actions/moduleFor.js";
2
6
  import { createHttpServer } from "./baseServer.js";
3
7
  import { DEFAULT_TTL_MS } from "../../helpers/cache/defaults.js";
4
8
  import { buildCacheManager } from "../../helpers/cache/cacheManager.js";
5
9
  import { createServerCaches, destroyServerCaches } from "../../helpers/cache/serverCaches.js";
6
10
  import normalizePlugins, { normalizePluginSource } from "../../helpers/normalizePlugins.js";
11
+ import { reportReactBuild } from "../../helpers/reportReactBuild.js";
7
12
  import { invalidatePluginComponentCache } from "../../modules/ssr/loadPluginComponents.js";
8
- import { createMemoryDraftStore } from "../../modules/ssr/preview.js";
13
+ import { DRAFT_STORE_METHODS, createMemoryDraftStore } from "../../modules/ssr/preview.js";
9
14
  import { compileTemplate } from "../../modules/ssr/template.js";
10
15
  import { PluginManager } from "../../plugins/manager.js";
11
16
  import { makeHandler } from "../http/dispatcher.js";
@@ -16,7 +21,18 @@ import { buildPagePipeline } from "../services/registry.js";
16
21
  * enables — so this unit never second-guesses a factory's promise. */
17
22
  var createPageServer = (config, services, extensions) => {
18
23
  const { cacheTtlMs: htmlTtlMs = DEFAULT_TTL_MS.html } = config;
19
- if (config.preview?.enabled && !config.draftStore) config.draftStore = createMemoryDraftStore();
24
+ const logLevel = config.logLevel ?? defaultLogLevel(config.devMode);
25
+ const { logger } = config;
26
+ config.logLevel = logLevel;
27
+ if (logger) config.logger = (event) => {
28
+ if (isLogged(logLevel, logLevelOf(event))) logger(event);
29
+ };
30
+ configureServerLog({
31
+ level: logLevel,
32
+ logger
33
+ });
34
+ reportReactBuild(config.devMode);
35
+ if (config.preview?.enabled && !config.draftStore) config.draftStore = fleetStore("ssr.drafts", DRAFT_STORE_METHODS) ?? createMemoryDraftStore();
20
36
  const caches = createServerCaches(htmlTtlMs, config.rsc?.cacheTtlMs ?? DEFAULT_TTL_MS.rsc);
21
37
  const cache = caches.html ? buildCacheManager(caches.html) : null;
22
38
  const renderFn = config.templateFn ?? compileTemplate();
@@ -32,6 +48,7 @@ var createPageServer = (config, services, extensions) => {
32
48
  }
33
49
  };
34
50
  const actions = actionsModuleFor(config);
51
+ const realtime = realtimeModuleFor(config);
35
52
  const stages = buildPagePipeline(services, extensions);
36
53
  const makeHandlerForPort = (port) => {
37
54
  const buildContext = (raw, rawRes, req, res) => ({
@@ -44,15 +61,27 @@ var createPageServer = (config, services, extensions) => {
44
61
  renderFn,
45
62
  caches,
46
63
  pluginManager,
47
- actions
64
+ actions,
65
+ realtime
66
+ });
67
+ return makeHandler("SSR", buildContext, stages, {
68
+ compression: config.compression,
69
+ ...realtime ? { upgrades: (path) => path === realtime.path } : {}
48
70
  });
49
- return makeHandler("SSR", buildContext, stages, config.compression);
50
71
  };
51
72
  return createHttpServer(config, makeHandlerForPort, {
52
73
  label: "SSR",
53
74
  cache,
54
75
  plugins,
55
- onListen: () => actions?.jobs?.start(),
76
+ onListen: () => {
77
+ if (runsFleetJobs()) actions?.jobs?.start();
78
+ },
79
+ beforeFork: () => pluginManager.prepareAll(),
80
+ forgetPlugins: (name, version) => {
81
+ pluginManager.forget(name, version);
82
+ invalidatePluginComponentCache();
83
+ },
84
+ onClosing: () => realtime?.close(),
56
85
  onDestroy: async () => {
57
86
  await actions?.jobs?.stop();
58
87
  destroyServerCaches(caches);
@@ -47,9 +47,17 @@ var actionStage = async (ctx) => {
47
47
  ctx.res.send("");
48
48
  return true;
49
49
  }
50
- handleActionCatalog({
50
+ const { spaceId, environment = "main", revision = 0 } = req.ctx.spaceDeployment ?? {};
51
+ await handleActionCatalog({
51
52
  res: ctx.res,
52
- module: actions
53
+ module: actions,
54
+ ...typeof spaceId === "number" ? {
55
+ spaceId,
56
+ at: {
57
+ environment,
58
+ revision
59
+ }
60
+ } : {}
53
61
  });
54
62
  return true;
55
63
  }
@@ -0,0 +1,9 @@
1
+ import { SSRContext, Stage } from '../http/types';
2
+ /**
3
+ * The space's functions answering HTTP under `/api/`: a request there that a declared route answers is run
4
+ * by it — the deployment's own routes first, then the space's, in the sandbox — and anything else goes on to the page
5
+ * server, which answers as it would have. `/api` is never a page: its slug is refused, so the path is the functions'.
6
+ *
7
+ * After the auth chain, so `ctx.user` is who the session says; the session itself never reaches the code.
8
+ */
9
+ export declare const functionRoutesStage: Stage<SSRContext>;
@@ -0,0 +1,47 @@
1
+ import { clientIp, readRawBytes, requestOrigin } from "../requestParser.js";
2
+ import { FUNCTION_ROUTES_PREFIX } from "@plitzi/sdk-shared/actions";
3
+ //#region src/core/services/functionRoutes.ts
4
+ /**
5
+ * The space's functions answering HTTP under `/api/`: a request there that a declared route answers is run
6
+ * by it — the deployment's own routes first, then the space's, in the sandbox — and anything else goes on to the page
7
+ * server, which answers as it would have. `/api` is never a page: its slug is refused, so the path is the functions'.
8
+ *
9
+ * After the auth chain, so `ctx.user` is who the session says; the session itself never reaches the code.
10
+ */
11
+ var functionRoutesStage = async (ctx) => {
12
+ const { req, actions } = ctx;
13
+ const { spaceId, environment = "main", revision = 0 } = req.ctx.spaceDeployment ?? {};
14
+ if (!actions || typeof spaceId !== "number" || !req.path.startsWith(`${FUNCTION_ROUTES_PREFIX}/`)) return false;
15
+ const route = await actions.routeFor({
16
+ spaceId,
17
+ environment,
18
+ at: {
19
+ environment,
20
+ revision
21
+ },
22
+ ...req.ctx.user ? { user: req.ctx.user } : {},
23
+ callerId: req.ctx.user ? `user:${String(req.ctx.user.id)}` : `ip:${clientIp(ctx.raw, req)}`,
24
+ signal: ctx.signal
25
+ }, req.method, req.path.slice(FUNCTION_ROUTES_PREFIX.length));
26
+ if (!route) return false;
27
+ ctx.operation = "function:route";
28
+ const headers = new Headers();
29
+ Object.entries(req.headers).forEach(([name, value]) => {
30
+ if (!name.startsWith(":") && value !== void 0) headers.set(name, Array.isArray(value) ? value.join(", ") : value);
31
+ });
32
+ const hasBody = req.method !== "GET" && req.method !== "HEAD";
33
+ const response = await route.handle(new Request(`${requestOrigin(req) || `${req.protocol}://${req.hostname}`}${req.url}`, {
34
+ method: req.method,
35
+ headers,
36
+ ...hasBody ? { body: new Uint8Array(await readRawBytes(ctx.raw)) } : {}
37
+ }));
38
+ ctx.res.setStatus(response.status);
39
+ response.headers.forEach((value, name) => {
40
+ ctx.res.setHeader(name, value);
41
+ });
42
+ if (!response.headers.has("cache-control")) ctx.res.setHeader("Cache-Control", "no-store");
43
+ ctx.res.send(Buffer.from(await response.arrayBuffer()));
44
+ return true;
45
+ };
46
+ //#endregion
47
+ export { functionRoutesStage };
@@ -0,0 +1,8 @@
1
+ import { SSRContext, Stage } from '../http/types';
2
+ /**
3
+ * `/_realtime`: a page subscribing (GET, a stream that stays open) and publishing (POST) — or asking to switch the
4
+ * same GET to a WebSocket, which does both.
5
+ *
6
+ * After the auth middleware, like the action endpoint: who may open a channel depends on who is asking.
7
+ */
8
+ export declare const realtimeStage: Stage<SSRContext>;
@@ -0,0 +1,57 @@
1
+ import { handleRealtimePublish, handleRealtimeSubscribe } from "../../modules/realtime/handlers.js";
2
+ import { readRawBody } from "../requestParser.js";
3
+ import { handleRealtimeSocket } from "../../modules/realtime/socket.js";
4
+ //#region src/core/services/realtime.ts
5
+ /**
6
+ * `/_realtime`: a page subscribing (GET, a stream that stays open) and publishing (POST) — or asking to switch the
7
+ * same GET to a WebSocket, which does both.
8
+ *
9
+ * After the auth middleware, like the action endpoint: who may open a channel depends on who is asking.
10
+ */
11
+ var realtimeStage = async (ctx) => {
12
+ const { realtime, req } = ctx;
13
+ if (!realtime || req.path !== realtime.path) return false;
14
+ if (ctx.upgrade) {
15
+ ctx.operation = "realtime:socket";
16
+ await handleRealtimeSocket({
17
+ req,
18
+ res: ctx.res,
19
+ raw: ctx.raw,
20
+ upgrade: ctx.upgrade,
21
+ hub: realtime.hub,
22
+ resolveChannels: realtime.resolveChannels,
23
+ grants: realtime.grants,
24
+ allowedOrigins: realtime.allowedOrigins
25
+ });
26
+ return true;
27
+ }
28
+ if (req.method === "GET") {
29
+ ctx.operation = "realtime:subscribe";
30
+ await handleRealtimeSubscribe({
31
+ req,
32
+ res: ctx.res,
33
+ raw: ctx.rawRes,
34
+ signal: ctx.signal,
35
+ hub: realtime.hub,
36
+ resolveChannels: realtime.resolveChannels,
37
+ grants: realtime.grants
38
+ });
39
+ return true;
40
+ }
41
+ if (req.method === "POST") {
42
+ ctx.operation = "realtime:publish";
43
+ req.body = await readRawBody(ctx.raw);
44
+ await handleRealtimePublish({
45
+ req,
46
+ res: ctx.res,
47
+ hub: realtime.hub
48
+ });
49
+ return true;
50
+ }
51
+ ctx.res.setStatus(405);
52
+ ctx.res.setHeader("Allow", "GET, POST");
53
+ ctx.res.send("");
54
+ return true;
55
+ };
56
+ //#endregion
57
+ export { realtimeStage };
@@ -1,10 +1,12 @@
1
1
  import { actionStage } from "./action.js";
2
+ import { functionRoutesStage } from "./functionRoutes.js";
3
+ import { realtimeStage } from "./realtime.js";
2
4
  import { rscStage } from "./rsc.js";
3
5
  import { notFoundStage, ssrStage } from "./ssr.js";
4
6
  import { authRoutesStages } from "../http/stages/authRoutes.js";
5
7
  import { fontAssetsStage } from "../http/stages/fontAssets.js";
6
8
  import { healthStage } from "../http/stages/health.js";
7
- import { middlewaresStage } from "../http/stages/middlewares.js";
9
+ import { createMiddlewaresStage } from "../http/stages/middlewares.js";
8
10
  import { pluginAssetsStage } from "../http/stages/pluginAssets.js";
9
11
  import { configStaticStage, publicDirStage, sdkAssetsStage, wellKnownStage } from "../http/stages/static.js";
10
12
  //#region src/core/services/registry.ts
@@ -20,10 +22,12 @@ var buildPagePipeline = (services, extensions = {}) => {
20
22
  sdkAssetsStage
21
23
  ];
22
24
  stages.push(...extensions.preAuth ?? []);
23
- stages.push(middlewaresStage);
25
+ stages.push(createMiddlewaresStage());
24
26
  if (services.rsc) stages.push(rscStage);
25
27
  stages.push(actionStage);
28
+ stages.push(realtimeStage);
26
29
  stages.push(...extensions.data ?? []);
30
+ stages.push(functionRoutesStage);
27
31
  stages.push(services.ssr ? ssrStage : notFoundStage);
28
32
  return stages;
29
33
  };
@@ -1,7 +1,10 @@
1
1
  import { getCacheControl, getMimeType } from "./mimeTypes.js";
2
- import fs from "node:fs";
2
+ import { CompressedFileCache } from "../helpers/cache/CompressedFileCache.js";
3
3
  import path from "node:path";
4
+ import fs from "node:fs";
4
5
  //#region src/core/staticFiles.ts
6
+ var compressedFiles = new CompressedFileCache(16777216);
7
+ var isText = (mimeType) => /^text\/|javascript|json|xml|svg/.test(mimeType);
5
8
  var buildEtag = (stat) => `"${stat.mtimeMs.toString(36)}-${stat.size.toString(36)}"`;
6
9
  var serveFile = (req, res, filePath, stat) => {
7
10
  const etag = buildEtag(stat);
@@ -14,9 +17,14 @@ var serveFile = (req, res, filePath, stat) => {
14
17
  res.end();
15
18
  return true;
16
19
  }
17
- const content = fs.readFileSync(filePath);
18
- res.setHeader("Content-Type", getMimeType(filePath));
19
- res.send(content);
20
+ const mimeType = getMimeType(filePath);
21
+ res.setHeader("Content-Type", mimeType);
22
+ if (isText(mimeType)) {
23
+ res.send(() => fs.readFileSync(filePath, "utf-8"), { compressed: compressedFiles.storeFor(filePath, etag) });
24
+ compressedFiles.trim();
25
+ return true;
26
+ }
27
+ res.send(fs.readFileSync(filePath));
20
28
  return true;
21
29
  };
22
30
  var serveStatic = (req, res, rootDir) => {
@@ -0,0 +1 @@
1
+ export {};
@@ -1,6 +1,9 @@
1
+ import { default as http, IncomingMessage } from 'node:http';
2
+ import { default as http2 } from 'node:http2';
3
+ import { default as https } from 'node:https';
1
4
  import { RawResponse } from '../helpers/buildResponseHelpers';
2
5
  import { SSRServerConfig } from '@plitzi/sdk-shared';
3
- import { IncomingMessage } from 'node:http';
6
+ import { Duplex } from 'node:stream';
4
7
  export type CloseableServer = {
5
8
  close: (cb?: (err?: Error) => void) => unknown;
6
9
  listen: (port: number, host: string, cb: () => void) => unknown;
@@ -8,7 +11,14 @@ export type CloseableServer = {
8
11
  * is how a taken port used to read as a crash in `node:net` rather than as this server not starting. */
9
12
  on: (event: 'error', listener: (error: NodeJS.ErrnoException) => void) => unknown;
10
13
  };
11
- export type Handler = (req: IncomingMessage, res: RawResponse) => void;
14
+ /** The server a port is served by: Node's own, whichever protocol — what a shutdown drains (`server/drain.ts`). */
15
+ export type PrimaryServer = http.Server | https.Server | http2.Http2SecureServer;
16
+ /** A request asking to switch protocols: node hands over the socket, and the bytes it read past the headers. */
17
+ export type UpgradeHandler = (req: IncomingMessage, socket: Duplex, head: Buffer) => void;
18
+ /** What a server answers requests with — and, when it takes any, upgrades (`upgrade`). */
19
+ export type Handler = ((req: IncomingMessage, res: RawResponse) => void) & {
20
+ upgrade?: UpgradeHandler;
21
+ };
12
22
  export declare const tlsOptions: (config: SSRServerConfig, label?: string) => {
13
23
  key: string | Buffer<ArrayBufferLike>;
14
24
  cert: string | Buffer<ArrayBufferLike>;
@@ -16,6 +26,6 @@ export declare const tlsOptions: (config: SSRServerConfig, label?: string) => {
16
26
  };
17
27
  export declare const protoLabel: (version: number, hasTls: boolean) => string;
18
28
  export declare const buildTransport: (config: SSRServerConfig, handler: Handler, port: number, label?: string) => {
19
- primary: CloseableServer;
29
+ primary: PrimaryServer;
20
30
  h3?: CloseableServer;
21
31
  };
@@ -1,3 +1,4 @@
1
+ import { serverLog } from "../helpers/serverLog.js";
1
2
  import http from "node:http";
2
3
  import http2 from "node:http2";
3
4
  import https from "node:https";
@@ -16,6 +17,14 @@ var protoLabel = (version, hasTls) => {
16
17
  if (version >= 2) return hasTls ? "HTTP/2 (TLS)" : "HTTP/1.1 - TLS Missing";
17
18
  return hasTls ? "HTTPS/1.1" : "HTTP/1.1";
18
19
  };
20
+ /**
21
+ * Upgrades reach HTTP/1.1 servers only. Over HTTP/2 a browser has no way to ask for one — node does not speak the
22
+ * extended CONNECT that WebSockets over h2 need — so a client there stays on what plain requests can do.
23
+ */
24
+ var withUpgrades = (server, handler) => {
25
+ if (handler.upgrade) server.on("upgrade", handler.upgrade);
26
+ return server;
27
+ };
19
28
  var buildTransport = (config, handler, port, label = "Server") => {
20
29
  const version = config.httpVersion ?? 2;
21
30
  let primary;
@@ -29,10 +38,10 @@ var buildTransport = (config, handler, port, label = "Server") => {
29
38
  try {
30
39
  h3 = (await import("node:http3")).createServer(tlsOptions(config, label), handler);
31
40
  h3.listen(port, "0.0.0.0", () => {
32
- console.log(`[${label}] HTTP/3 (QUIC) listening on port ${port}`);
41
+ serverLog.info(label, `HTTP/3 (QUIC) listening on port ${port}`);
33
42
  });
34
43
  } catch {
35
- console.warn(`[${label}] HTTP/3 unavailable — start Node.js with --experimental-quic (requires Node ≥ 23). Falling back to HTTP/2.`);
44
+ serverLog.warn(label, "HTTP/3 unavailable — start Node.js with --experimental-quic (requires Node ≥ 23). Falling back to HTTP/2.");
36
45
  }
37
46
  })();
38
47
  } else if (version >= 2) {
@@ -40,9 +49,9 @@ var buildTransport = (config, handler, port, label = "Server") => {
40
49
  ...tlsOptions(config, label),
41
50
  allowHTTP1: true
42
51
  }, handler);
43
- else primary = http.createServer(handler);
44
- } else if (config.tls) primary = https.createServer(tlsOptions(config, label), handler);
45
- else primary = http.createServer(handler);
52
+ else primary = withUpgrades(http.createServer(handler), handler);
53
+ } else if (config.tls) primary = withUpgrades(https.createServer(tlsOptions(config, label), handler), handler);
54
+ else primary = withUpgrades(http.createServer(handler), handler);
46
55
  return {
47
56
  primary,
48
57
  h3