akanjs 3.2.0-rc.9 → 3.2.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.
- package/ARCHITECTURE.md +41 -38
- package/CHANGELOG.md +0 -680
- package/CONTRIBUTING.md +3 -0
- package/base/base.ts +2 -1
- package/client/clientRuntime.ts +1 -0
- package/client/cn.ts +2 -2
- package/client/csrTypes.ts +2 -0
- package/client/frameConfig.ts +1 -0
- package/client/index.ts +249 -26
- package/client/locale.ts +1 -0
- package/client/makePageProto.tsx +12 -85
- package/client/nativeThemeColor.ts +51 -0
- package/client/route/PageDefinition.ts +11 -3
- package/client/route/RouteDefinition.ts +24 -7
- package/client/route/RouteGuards.ts +2 -1
- package/client/route/RouteHead.ts +4 -1
- package/client/route/routeArgs.ts +8 -0
- package/client/router.ts +17 -2
- package/client/rscNavigation.ts +1 -0
- package/client/viewportMeta.ts +86 -6
- package/common/CodeTranscript.ts +1 -1
- package/common/Logger.ts +4 -2
- package/common/PipelinePlanSchema.ts +2 -2
- package/common/clientAddress.ts +14 -0
- package/common/err.ts +122 -0
- package/common/eventStream.ts +71 -19
- package/common/facetBarrel.ts +38 -0
- package/common/index.ts +7 -0
- package/common/isGuardRefusal.ts +14 -0
- package/common/loggedFailure.ts +11 -0
- package/common/mcpExposure.ts +17 -4
- package/common/pipelinePlan.ts +1 -1
- package/common/routeConvention.ts +3 -1
- package/common/types.ts +18 -1
- package/constant/constantRegistry.ts +4 -8
- package/constant/crystalize.ts +1 -2
- package/constant/getDefault.ts +1 -1
- package/constant/immerify.ts +1 -1
- package/constant/index.ts +78 -21
- package/constant/purify.ts +4 -9
- package/constant/serialize.ts +4 -2
- package/constant/types.ts +1 -0
- package/constant/via.ts +1 -2
- package/dictionary/agent.dictionary.ts +12 -0
- package/dictionary/base.dictionary.ts +11 -0
- package/dictionary/locale.ts +1 -1
- package/dictionary/trans.ts +14 -97
- package/document/dataLoader.ts +39 -19
- package/document/databaseRegistry.ts +2 -1
- package/document/documentSchema.ts +1 -0
- package/document/filterMeta.ts +4 -1
- package/document/into.ts +2 -1
- package/document/loaderInfo.ts +2 -1
- package/document/noDocumentError.ts +4 -1
- package/document/queryEvaluator.ts +1 -1
- package/document/types.ts +1 -1
- package/fetch/client/fetchClient.ts +83 -269
- package/fetch/client/fetchClientTypes.ts +49 -0
- package/fetch/client/httpClient.ts +3 -2
- package/fetch/client/remoteError.ts +4 -20
- package/fetch/client/requestScope.ts +41 -0
- package/fetch/client/signalCatalog.ts +131 -0
- package/fetch/client/wsClient.ts +82 -6
- package/fetch/requestStorage.ts +1 -0
- package/package.json +1 -1
- package/server/agentHooks.ts +16 -0
- package/server/akanApp.ts +110 -1272
- package/server/akanOption.ts +30 -3
- package/server/akanServer.ts +179 -674
- package/server/artifact/builderRpc.ts +14 -6
- package/server/artifact/routeSeedIndexStore.ts +4 -2
- package/server/assetHeaders.ts +6 -0
- package/server/console.ts +6 -4
- package/server/decorators.ts +2 -2
- package/server/di/diLifecycle.ts +24 -35
- package/server/di/predefinedAdaptor.ts +24 -1
- package/server/di/resolveHierarchy.ts +19 -6
- package/server/di/stageFailedError.ts +10 -0
- package/server/di/utils.ts +8 -3
- package/server/ed25519Key.ts +31 -15
- package/server/flightRowReader.ts +106 -20
- package/server/gateway/childSupervisor.ts +328 -0
- package/server/gateway/devHostChannel.ts +47 -0
- package/server/gateway/gatewayChildren.ts +161 -0
- package/server/gateway/gatewayConfig.ts +182 -0
- package/server/gateway/gatewayHttpProxy.ts +231 -0
- package/server/gateway/gatewayLogging.ts +111 -0
- package/server/gateway/gatewayRooms.ts +141 -0
- package/server/gateway/gatewayStatus.ts +122 -0
- package/server/gateway/gatewayStatusPage.ts +54 -0
- package/server/gateway/gatewayWebSocketRelay.ts +149 -0
- package/server/gateway/listeningPort.ts +13 -0
- package/server/hmr/devHmrController.ts +10 -1
- package/server/htmlResultCache.ts +234 -0
- package/server/imageOptimizer.ts +4 -1
- package/server/index.ts +281 -17
- package/server/lifecycle/parentChannel.ts +79 -0
- package/server/lifecycle/shutdownManager.ts +6 -3
- package/server/lifecycle/startGuard.ts +90 -0
- package/server/logging/hubFileSink.ts +1 -0
- package/server/logging/logControlSocket.ts +42 -3
- package/server/logging/logHub.ts +0 -0
- package/server/logging/logStdoutWriter.ts +3 -1
- package/server/logging/logStreamRoute.ts +28 -11
- package/server/logging/serverLogging.ts +74 -0
- package/server/machine/machineToken.ts +10 -3
- package/server/mcp/AgentTools.ts +149 -0
- package/server/mcp/AgentToolsAccount.ts +26 -0
- package/server/mcp/McpApps.ts +32 -2
- package/server/mcp/McpDispatcher.ts +44 -30
- package/server/mcp/McpPages.ts +34 -12
- package/server/mcp/McpRateLimiter.ts +14 -0
- package/server/mcp/McpRouter.ts +4 -2
- package/server/mcp/McpUiSession.ts +2 -1
- package/server/mcp/index.ts +1 -0
- package/server/migration/migrationCommand.ts +31 -2
- package/server/migration/migrationRunner.ts +36 -1
- package/server/nativeFile.ts +39 -12
- package/server/nativeSecret.ts +12 -3
- package/server/oauth/OAuthClientIdMetadata.ts +2 -2
- package/server/ops/migrateJobs.ts +2 -2
- package/server/ops/opsCommand.ts +1 -1
- package/server/ops/opsRoute.ts +5 -1
- package/server/ops/opsToken.ts +3 -1
- package/server/remote/RemoteFetch.ts +161 -0
- package/server/remote/RemoteMcp.ts +242 -0
- package/server/remote/RemoteOAuth.ts +270 -0
- package/server/remote/RemoteSource.ts +214 -0
- package/server/remote/index.ts +17 -0
- package/server/remote/remoteError.ts +19 -0
- package/server/resolver/CascadeRunner.ts +20 -8
- package/server/resolver/database.resolver.ts +10 -417
- package/server/resolver/databaseModelInstance.ts +209 -0
- package/server/resolver/invalidSignal.ts +7 -0
- package/server/resolver/livePause.ts +34 -0
- package/server/resolver/modelFacade.ts +136 -0
- package/server/resolver/modelLoaders.ts +84 -0
- package/server/resolver/modelSchema.ts +52 -0
- package/server/resolver/roomAdmission.ts +68 -0
- package/server/resolver/signal.resolver.ts +45 -111
- package/server/resolver/sliceCrud.ts +63 -0
- package/server/resolver/timedQuery.ts +16 -0
- package/server/resolver/websocketOf.ts +2 -1
- package/server/routeElementComposer.tsx +45 -6
- package/server/routeTreeBuilder.ts +23 -9
- package/server/routing/apiRouter.ts +53 -28
- package/server/routing/builtinRoutes.ts +194 -0
- package/server/routing/localRooms.ts +34 -0
- package/server/routing/roomReplay.ts +2 -2
- package/server/routing/serverLimits.ts +4 -1
- package/server/routing/serverListener.ts +194 -0
- package/server/rscClient.tsx +121 -204
- package/server/rscClientCache.ts +165 -0
- package/server/rscClientChunks.ts +126 -8
- package/server/rscClientFetch.ts +3 -1
- package/server/rscClientPatch.ts +78 -15
- package/server/rscDocumentHead.tsx +177 -0
- package/server/rscFlightRenderer.ts +83 -0
- package/server/rscFlightStream.ts +259 -0
- package/server/rscHostReloads.ts +238 -0
- package/server/rscHostRenderStream.ts +297 -0
- package/server/rscHostRequests.ts +158 -0
- package/server/rscHttp.ts +18 -6
- package/server/rscNavigationState.ts +24 -1
- package/server/rscPagePrompts.ts +65 -14
- package/server/rscPrefetchCache.ts +5 -1
- package/server/rscRenderControl.ts +33 -0
- package/server/rscRenderFallbacks.ts +299 -0
- package/server/rscRenderHandler.ts +439 -0
- package/server/rscRenderStats.ts +49 -0
- package/server/rscResultCaches.ts +74 -0
- package/server/rscRouteDocument.tsx +316 -0
- package/server/rscSegmentOutletReference.ts +5 -1
- package/server/rscStaticApp.tsx +73 -0
- package/server/rscWorker.tsx +112 -1341
- package/server/rscWorkerHost.ts +175 -803
- package/server/rscWorkerProcess.ts +155 -0
- package/server/rscWorkerRecycler.ts +128 -0
- package/server/serverConfig.ts +128 -0
- package/server/serverMetrics.ts +52 -0
- package/server/sitemap.ts +1 -0
- package/server/ssrFromRscRenderer.tsx +20 -20
- package/server/ssrStaticApp.tsx +243 -0
- package/server/webArtifact.ts +86 -0
- package/server/webAssetRoutes.ts +84 -0
- package/server/webCsrSurface.ts +172 -0
- package/server/webDocumentRoute.ts +180 -0
- package/server/webRouteContext.ts +232 -0
- package/server/webRouter.ts +129 -1165
- package/server/webRscRoute.ts +155 -0
- package/server/webSiteFiles.ts +139 -0
- package/server/webStaticApp.ts +62 -0
- package/server/wellKnownRoutes.ts +73 -0
- package/service/agent.service.ts +14 -2
- package/service/agentAccounts.ts +19 -0
- package/service/agentLoop/agentLoop.ts +550 -0
- package/service/agentLoop/agentLoopTypes.ts +220 -0
- package/service/agentLoop/deltaStream.ts +40 -0
- package/service/agentLoop/loopAnswer.ts +67 -0
- package/service/agentLoop/loopAsk.ts +44 -0
- package/service/agentLoop/loopCompaction.ts +146 -0
- package/service/agentLoop/loopRemotes.ts +181 -0
- package/service/agentLoop/loopRequest.ts +120 -0
- package/service/agentLoop/loopStored.ts +24 -0
- package/service/agentLoop/loopToolOutput.ts +54 -0
- package/service/agentLoop/loopToolkits.ts +155 -0
- package/service/agentLoop/loopTranscript.ts +138 -0
- package/{signal → service}/agentMeter.ts +2 -1
- package/{signal → service}/agentRelayPolicy.ts +43 -8
- package/service/endpointCacheKeys.ts +14 -2
- package/service/index.ts +6 -0
- package/service/injectInfo.ts +2 -1
- package/service/ipcTypes.ts +8 -2
- package/service/liveRoomLimit.ts +68 -0
- package/service/liveRoomLimitError.ts +16 -0
- package/service/liveSyncHub.ts +24 -2
- package/service/predefinedAdaptor/agentTools.adaptor.ts +34 -0
- package/service/predefinedAdaptor/anthropicLlm.ts +33 -14
- package/service/predefinedAdaptor/compress.adaptor.ts +2 -2
- package/service/predefinedAdaptor/database.adaptor.ts +62 -19
- package/service/predefinedAdaptor/index.ts +1 -0
- package/service/predefinedAdaptor/llm.adaptor.ts +24 -1
- package/service/predefinedAdaptor/openaiDialect.ts +72 -23
- package/service/predefinedAdaptor/openaiLlm.ts +1 -1
- package/service/predefinedAdaptor/role.adaptor.ts +2 -0
- package/service/predefinedAdaptor/solidPubsub.adaptor.ts +35 -2
- package/service/predefinedAdaptor/solidQueue.adaptor.ts +2 -2
- package/service/predefinedAdaptor/solidSqlite.ts +6 -3
- package/service/predefinedAdaptor/sql/DocumentHydrator.ts +95 -0
- package/service/predefinedAdaptor/sql/DocumentRowCodec.ts +192 -0
- package/service/predefinedAdaptor/sql/DocumentWriter.ts +298 -0
- package/service/predefinedAdaptor/sql/QueryScope.ts +70 -0
- package/service/predefinedAdaptor/sql/QueryWriter.ts +200 -0
- package/service/predefinedAdaptor/sql/SqlDocumentStore.ts +127 -1069
- package/service/predefinedAdaptor/sql/SqlTableSchema.ts +357 -0
- package/service/predefinedAdaptor/sql/StoredRows.ts +95 -0
- package/service/predefinedAdaptor/sql/TransactionScope.ts +14 -7
- package/service/predefinedAdaptor/sql/dialect/postgres.ts +1 -1
- package/service/predefinedAdaptor/sql/dialect/sqlite.ts +1 -1
- package/service/predefinedAdaptor/sql/driver/bunSqlite.ts +20 -9
- package/service/predefinedAdaptor/sql/driver/postgres.ts +1 -1
- package/service/predefinedAdaptor/sql/types.ts +28 -4
- package/service/predefinedAdaptor/sseData.ts +88 -0
- package/service/predefinedAdaptor/storage.adaptor.ts +2 -1
- package/service/predefinedAdaptor/websocket.adaptor.ts +74 -44
- package/service/serviceRegistry.ts +1 -0
- package/signal/SignalFailure.ts +35 -0
- package/signal/agent.signal.ts +15 -9
- package/signal/agentTurnStream.ts +23 -9
- package/signal/byIdRead.ts +18 -0
- package/signal/carriedClientIps.ts +1 -0
- package/signal/endpointCache.ts +61 -6
- package/signal/endpointErrorContract.ts +1 -1
- package/signal/endpointRateLimit.ts +84 -3
- package/signal/index.ts +322 -32
- package/signal/mcp/McpDocument.ts +8 -1
- package/signal/mcp/mcpProtocol.ts +6 -1
- package/signal/mcp/pagePrompt.ts +15 -0
- package/signal/middleware.ts +10 -3
- package/signal/openapi/openapi.ts +10 -1
- package/signal/resolveFieldContext.ts +34 -0
- package/signal/serializer/fetch.serializer.ts +22 -15
- package/signal/signalContext.ts +15 -42
- package/signal/slice.ts +22 -19
- package/signal/trace.ts +44 -5
- package/signal/types.ts +46 -8
- package/store/action.ts +32 -1057
- package/store/actionErrorMessage.ts +20 -0
- package/store/actionOption.ts +22 -0
- package/store/baseSt.ts +1 -0
- package/store/formFieldSetters.ts +229 -0
- package/store/index.ts +125 -17
- package/store/modelActionContext.ts +65 -0
- package/store/modelOpenActions.ts +158 -0
- package/store/modelWriteActions.ts +240 -0
- package/store/modelWriteGuard.ts +3 -12
- package/store/openSeq.ts +34 -0
- package/store/sliceListActions.ts +370 -0
- package/store/sliceLiveActions.ts +140 -0
- package/store/stateBuilder.ts +2 -0
- package/store/storeInstance.ts +4 -18
- package/store/storeRegistry.ts +1 -1
- package/test/conformance.ts +25 -5
- package/types/base/base.d.ts +2 -1
- package/types/client/clientRuntime.d.ts +1 -0
- package/types/client/cn.d.ts +2 -2
- package/types/client/csrTypes.d.ts +2 -0
- package/types/client/frameConfig.d.ts +1 -0
- package/types/client/index.d.ts +25 -26
- package/types/client/locale.d.ts +1 -0
- package/types/client/makePageProto.d.ts +5 -13
- package/types/client/nativeThemeColor.d.ts +6 -0
- package/types/client/route/PageDefinition.d.ts +5 -2
- package/types/client/route/RouteDefinition.d.ts +2 -2
- package/types/client/route/routeArgs.d.ts +7 -0
- package/types/client/router.d.ts +3 -1
- package/types/client/rscNavigation.d.ts +1 -0
- package/types/client/viewportMeta.d.ts +5 -2
- package/types/common/CodeTranscript.d.ts +1 -1
- package/types/common/clientAddress.d.ts +5 -0
- package/types/common/err.d.ts +48 -0
- package/types/common/eventStream.d.ts +5 -1
- package/types/common/facetBarrel.d.ts +4 -0
- package/types/common/index.d.ts +6 -3
- package/types/common/isGuardRefusal.d.ts +3 -0
- package/types/common/loggedFailure.d.ts +2 -0
- package/types/common/mcpExposure.d.ts +9 -2
- package/types/common/routeConvention.d.ts +2 -0
- package/types/common/types.d.ts +18 -1
- package/types/constant/constantRegistry.d.ts +3 -1
- package/types/constant/crystalize.d.ts +1 -1
- package/types/constant/getDefault.d.ts +1 -1
- package/types/constant/immerify.d.ts +1 -1
- package/types/constant/index.d.ts +21 -21
- package/types/constant/purify.d.ts +2 -1
- package/types/constant/serialize.d.ts +1 -1
- package/types/constant/types.d.ts +1 -0
- package/types/dictionary/agent.dictionary.d.ts +1 -1
- package/types/dictionary/base.dictionary.d.ts +1 -1
- package/types/dictionary/dictionary.d.ts +9 -9
- package/types/dictionary/locale.d.ts +1 -1
- package/types/dictionary/trans.d.ts +3 -26
- package/types/document/dataLoader.d.ts +5 -0
- package/types/document/databaseRegistry.d.ts +2 -1
- package/types/document/documentSchema.d.ts +1 -0
- package/types/document/filterMeta.d.ts +4 -1
- package/types/document/into.d.ts +1 -1
- package/types/document/loaderInfo.d.ts +2 -1
- package/types/document/noDocumentError.d.ts +8 -1
- package/types/document/queryEvaluator.d.ts +1 -1
- package/types/document/types.d.ts +1 -1
- package/types/fetch/client/fetchClient.d.ts +6 -36
- package/types/fetch/client/fetchClientTypes.d.ts +40 -0
- package/types/fetch/client/httpClient.d.ts +2 -0
- package/types/fetch/client/remoteError.d.ts +0 -4
- package/types/fetch/client/requestScope.d.ts +11 -0
- package/types/fetch/client/signalCatalog.d.ts +25 -0
- package/types/fetch/client/wsClient.d.ts +2 -0
- package/types/fetch/requestStorage.d.ts +1 -0
- package/types/server/agentHooks.d.ts +4 -0
- package/types/server/akanApp.d.ts +2 -20
- package/types/server/akanOption.d.ts +16 -2
- package/types/server/akanServer.d.ts +2 -2
- package/types/server/assetHeaders.d.ts +1 -0
- package/types/server/console.d.ts +4 -3
- package/types/server/di/predefinedAdaptor.d.ts +1 -0
- package/types/server/di/stageFailedError.d.ts +5 -0
- package/types/server/ed25519Key.d.ts +1 -0
- package/types/server/flightRowReader.d.ts +8 -2
- package/types/server/gateway/childSupervisor.d.ts +35 -0
- package/types/server/gateway/devHostChannel.d.ts +14 -0
- package/types/server/gateway/gatewayChildren.d.ts +50 -0
- package/types/server/gateway/gatewayConfig.d.ts +82 -0
- package/types/server/gateway/gatewayHttpProxy.d.ts +22 -0
- package/types/server/gateway/gatewayLogging.d.ts +16 -0
- package/types/server/gateway/gatewayRooms.d.ts +25 -0
- package/types/server/gateway/gatewayStatus.d.ts +63 -0
- package/types/server/gateway/gatewayStatusPage.d.ts +9 -0
- package/types/server/gateway/gatewayWebSocketRelay.d.ts +17 -0
- package/types/server/gateway/listeningPort.d.ts +5 -0
- package/types/server/htmlResultCache.d.ts +51 -0
- package/types/server/index.d.ts +19 -17
- package/types/server/lifecycle/parentChannel.d.ts +13 -0
- package/types/server/lifecycle/startGuard.d.ts +10 -0
- package/types/server/logging/logHub.d.ts +5 -1
- package/types/server/logging/logStreamRoute.d.ts +2 -0
- package/types/server/logging/rotatingLogWriter.d.ts +1 -1
- package/types/server/logging/serverLogging.d.ts +12 -0
- package/types/server/mcp/AgentTools.d.ts +31 -0
- package/types/server/mcp/AgentToolsAccount.d.ts +7 -0
- package/types/server/mcp/McpApps.d.ts +6 -2
- package/types/server/mcp/McpDispatcher.d.ts +10 -1
- package/types/server/mcp/McpPages.d.ts +2 -1
- package/types/server/mcp/McpRateLimiter.d.ts +3 -0
- package/types/server/mcp/index.d.ts +1 -0
- package/types/server/migration/migrationCommand.d.ts +1 -1
- package/types/server/migration/migrationRunner.d.ts +9 -1
- package/types/server/remote/RemoteFetch.d.ts +42 -0
- package/types/server/remote/RemoteMcp.d.ts +52 -0
- package/types/server/remote/RemoteOAuth.d.ts +58 -0
- package/types/server/remote/RemoteSource.d.ts +51 -0
- package/types/server/remote/index.d.ts +5 -0
- package/types/server/remote/remoteError.d.ts +12 -0
- package/types/server/remote.d.ts +1 -0
- package/types/server/resolver/CascadeRunner.d.ts +2 -2
- package/types/server/resolver/database.resolver.d.ts +2 -2
- package/types/server/resolver/databaseModelInstance.d.ts +4 -0
- package/types/server/resolver/invalidSignal.d.ts +2 -0
- package/types/server/resolver/livePause.d.ts +5 -0
- package/types/server/resolver/modelFacade.d.ts +5 -0
- package/types/server/resolver/modelLoaders.d.ts +15 -0
- package/types/server/resolver/modelSchema.d.ts +6 -0
- package/types/server/resolver/roomAdmission.d.ts +7 -0
- package/types/server/resolver/signal.resolver.d.ts +1 -1
- package/types/server/resolver/sliceCrud.d.ts +8 -0
- package/types/server/resolver/timedQuery.d.ts +8 -0
- package/types/server/routeElementComposer.d.ts +21 -5
- package/types/server/routing/builtinRoutes.d.ts +24 -0
- package/types/server/routing/localRooms.d.ts +14 -0
- package/types/server/routing/roomReplay.d.ts +2 -2
- package/types/server/routing/serverListener.d.ts +31 -0
- package/types/server/rscClientCache.d.ts +34 -0
- package/types/server/rscClientChunks.d.ts +3 -1
- package/types/server/rscClientPatch.d.ts +12 -1
- package/types/server/rscDocumentHead.d.ts +33 -0
- package/types/server/rscFlightRenderer.d.ts +14 -0
- package/types/server/rscFlightStream.d.ts +53 -0
- package/types/server/rscHostReloads.d.ts +54 -0
- package/types/server/rscHostRenderStream.d.ts +53 -0
- package/types/server/rscHostRequests.d.ts +29 -0
- package/types/server/rscHttp.d.ts +4 -2
- package/types/server/rscNavigationState.d.ts +5 -1
- package/types/server/rscPagePrompts.d.ts +13 -0
- package/types/server/rscRenderControl.d.ts +12 -0
- package/types/server/rscRenderFallbacks.d.ts +41 -0
- package/types/server/rscRenderHandler.d.ts +29 -0
- package/types/server/rscRenderStats.d.ts +32 -0
- package/types/server/rscResultCaches.d.ts +18 -0
- package/types/server/rscRouteDocument.d.ts +52 -0
- package/types/server/rscStaticApp.d.ts +28 -0
- package/types/server/rscWorker.d.ts +1 -3
- package/types/server/rscWorkerHost.d.ts +10 -70
- package/types/server/rscWorkerProcess.d.ts +31 -0
- package/types/server/rscWorkerRecycler.d.ts +22 -0
- package/types/server/serverConfig.d.ts +30 -0
- package/types/server/serverMetrics.d.ts +11 -0
- package/types/server/sitemap.d.ts +1 -0
- package/types/server/ssrFromRscRenderer.d.ts +2 -2
- package/types/server/ssrStaticApp.d.ts +20 -0
- package/types/server/webArtifact.d.ts +7 -0
- package/types/server/webAssetRoutes.d.ts +11 -0
- package/types/server/webCsrSurface.d.ts +18 -0
- package/types/server/webDocumentRoute.d.ts +19 -0
- package/types/server/webRouteContext.d.ts +45 -0
- package/types/server/webRouter.d.ts +3 -28
- package/types/server/webRscRoute.d.ts +24 -0
- package/types/server/webSiteFiles.d.ts +12 -0
- package/types/server/webStaticApp.d.ts +8 -0
- package/types/server/wellKnownRoutes.d.ts +5 -0
- package/types/service/agent.service.d.ts +3 -0
- package/types/service/agentAccounts.d.ts +8 -0
- package/types/service/agentLoop/agentLoop.d.ts +33 -0
- package/types/service/agentLoop/agentLoopTypes.d.ts +235 -0
- package/types/service/agentLoop/deltaStream.d.ts +3 -0
- package/types/service/agentLoop/loopAnswer.d.ts +13 -0
- package/types/service/agentLoop/loopAsk.d.ts +12 -0
- package/types/service/agentLoop/loopCompaction.d.ts +12 -0
- package/types/service/agentLoop/loopRemotes.d.ts +40 -0
- package/types/service/agentLoop/loopRequest.d.ts +9 -0
- package/types/service/agentLoop/loopStored.d.ts +6 -0
- package/types/service/agentLoop/loopToolOutput.d.ts +8 -0
- package/types/service/agentLoop/loopToolkits.d.ts +17 -0
- package/types/service/agentLoop/loopTranscript.d.ts +13 -0
- package/types/{signal → service}/agentMeter.d.ts +1 -1
- package/types/{signal → service}/agentRelayPolicy.d.ts +18 -3
- package/types/service/index.d.ts +6 -0
- package/types/service/injectInfo.d.ts +2 -1
- package/types/service/ipcTypes.d.ts +10 -0
- package/types/service/liveRoomLimit.d.ts +26 -0
- package/types/service/liveRoomLimitError.d.ts +9 -0
- package/types/service/predefinedAdaptor/agentTools.adaptor.d.ts +31 -0
- package/types/service/predefinedAdaptor/anthropicLlm.d.ts +2 -2
- package/types/service/predefinedAdaptor/database.adaptor.d.ts +9 -7
- package/types/service/predefinedAdaptor/index.d.ts +1 -0
- package/types/service/predefinedAdaptor/llm.adaptor.d.ts +12 -0
- package/types/service/predefinedAdaptor/openaiDialect.d.ts +3 -2
- package/types/service/predefinedAdaptor/role.adaptor.d.ts +2 -0
- package/types/service/predefinedAdaptor/solidPubsub.adaptor.d.ts +3 -0
- package/types/service/predefinedAdaptor/solidSqlite.d.ts +1 -1
- package/types/service/predefinedAdaptor/sql/DocumentHydrator.d.ts +11 -0
- package/types/service/predefinedAdaptor/sql/DocumentRowCodec.d.ts +26 -0
- package/types/service/predefinedAdaptor/sql/DocumentWriter.d.ts +25 -0
- package/types/service/predefinedAdaptor/sql/QueryScope.d.ts +16 -0
- package/types/service/predefinedAdaptor/sql/QueryWriter.d.ts +20 -0
- package/types/service/predefinedAdaptor/sql/SqlDocumentStore.d.ts +5 -28
- package/types/service/predefinedAdaptor/sql/SqlTableSchema.d.ts +22 -0
- package/types/service/predefinedAdaptor/sql/StoredRows.d.ts +27 -0
- package/types/service/predefinedAdaptor/sql/TransactionScope.d.ts +3 -1
- package/types/service/predefinedAdaptor/sql/driver/bunSqlite.d.ts +3 -15
- package/types/service/predefinedAdaptor/sql/driver/postgres.d.ts +0 -8
- package/types/service/predefinedAdaptor/sql/types.d.ts +27 -4
- package/types/service/predefinedAdaptor/sseData.d.ts +13 -0
- package/types/service/predefinedAdaptor/websocket.adaptor.d.ts +7 -3
- package/types/service/serviceRegistry.d.ts +1 -0
- package/types/signal/SignalFailure.d.ts +4 -0
- package/types/signal/agentTurnStream.d.ts +3 -0
- package/types/signal/byIdRead.d.ts +7 -0
- package/types/signal/carriedClientIps.d.ts +4 -0
- package/types/signal/endpointRateLimit.d.ts +12 -0
- package/types/signal/index.d.ts +32 -32
- package/types/signal/mcp/McpDocument.d.ts +2 -0
- package/types/signal/mcp/mcpProtocol.d.ts +5 -0
- package/types/signal/mcp/pagePrompt.d.ts +13 -0
- package/types/signal/resolveFieldContext.d.ts +15 -0
- package/types/signal/serializer/fetch.serializer.d.ts +3 -1
- package/types/signal/signalContext.d.ts +2 -14
- package/types/signal/slice.d.ts +15 -13
- package/types/signal/trace.d.ts +12 -1
- package/types/signal/types.d.ts +38 -7
- package/types/store/action.d.ts +4 -27
- package/types/store/actionErrorMessage.d.ts +1 -0
- package/types/store/actionOption.d.ts +24 -0
- package/types/store/baseSt.d.ts +1 -0
- package/types/store/formFieldSetters.d.ts +80 -0
- package/types/store/index.d.ts +17 -16
- package/types/store/modelActionContext.d.ts +189 -0
- package/types/store/modelOpenActions.d.ts +12 -0
- package/types/store/modelWriteActions.d.ts +16 -0
- package/types/store/modelWriteGuard.d.ts +2 -5
- package/types/store/openSeq.d.ts +17 -0
- package/types/store/sliceListActions.d.ts +295 -0
- package/types/store/sliceLiveActions.d.ts +6 -0
- package/types/store/stateBuilder.d.ts +2 -0
- package/types/test/conformance.d.ts +3 -1
- package/types/ui/Agent/Chat.d.ts +5 -2
- package/types/ui/Agent/ChatHeader.d.ts +13 -0
- package/types/ui/Agent/ChatPendingCards.d.ts +23 -0
- package/types/ui/Agent/ToolRow.d.ts +13 -0
- package/types/ui/Field/Choice.d.ts +2 -1
- package/types/ui/Field/index.d.ts +1 -1
- package/types/ui/Reference/origin.d.ts +1 -0
- package/types/ui/UiOverride/UiOverride.d.ts +2 -0
- package/types/ui/index.d.ts +2 -1
- package/types/ui/overlayLayer.d.ts +8 -2
- package/types/ui/recipe/badgeRecipe.d.ts +3 -3
- package/types/ui/recipe/buttonRecipe.d.ts +3 -3
- package/types/ui/recipe/inputRecipe.d.ts +1 -1
- package/types/vendor/@akanjs/native/packages/cli/src/lib/project.d.ts +4 -0
- package/types/vendor/@akanjs/native/packages/desktop/src/relaunch.d.ts +1 -0
- package/types/vendor/@akanjs/native/plugins/appearance/src/index.d.ts +9 -0
- package/types/vendor/@akanjs/native/plugins/external-view/src/index.d.ts +22 -13
- package/types/vendor/@akanjs/native/plugins/single-instance/src/socket.d.ts +15 -2
- package/types/vendor/use-agentic/AgentClientTools.d.ts +19 -0
- package/types/vendor/use-agentic/Compaction.d.ts +3 -1
- package/types/vendor/use-agentic/SessionQuestion.d.ts +26 -0
- package/types/vendor/use-agentic/Transcript.d.ts +1 -1
- package/types/vendor/use-agentic/failureCode.d.ts +4 -0
- package/types/vendor/use-agentic/index.d.ts +1 -0
- package/types/vendor/use-agentic/types.d.ts +12 -3
- package/types/webkit/CsrGuards.d.ts +3 -0
- package/types/webkit/McpAppHost.d.ts +24 -0
- package/types/webkit/RouteLevelBoundary.d.ts +17 -0
- package/types/webkit/csrRefresh.d.ts +13 -0
- package/types/webkit/index.d.ts +3 -0
- package/types/webkit/needsBrowser.d.ts +5 -0
- package/types/webkit/useCamera.d.ts +1 -1
- package/types/webkit/useCsrKeyboard.d.ts +19 -0
- package/types/webkit/useCsrLocationState.d.ts +21 -0
- package/types/webkit/useCsrNavigation.d.ts +25 -0
- package/types/webkit/useCsrRefs.d.ts +33 -0
- package/types/webkit/useCsrRenderTrace.d.ts +13 -0
- package/types/webkit/useCsrStackLifecycle.d.ts +12 -0
- package/types/webkit/useCsrTransitions.d.ts +2 -0
- package/types/webkit/useGeoLocation.d.ts +1 -1
- package/types/webkit/useHistory.d.ts +2 -3
- package/types/webkit/useResolvedFrame.d.ts +38 -0
- package/types/webkit/useSyncNavigation.d.ts +22 -0
- package/ui/Agent/Bubble.tsx +4 -118
- package/ui/Agent/Chat.tsx +402 -235
- package/ui/Agent/ChatHeader.tsx +59 -0
- package/ui/Agent/ChatPendingCards.tsx +49 -0
- package/ui/Agent/ToolRow.tsx +117 -0
- package/ui/Agent/fetchRunner.ts +8 -1
- package/ui/BottomSheet.tsx +1 -1
- package/ui/Data/Item.tsx +2 -2
- package/ui/Data/ListContainer.tsx +348 -181
- package/ui/Data/useViewModal.ts +10 -4
- package/ui/Dialog/LegacyModal.tsx +1 -1
- package/ui/Dialog/Modal.tsx +1 -1
- package/ui/Field/Choice.tsx +3 -1
- package/ui/Load/StreamBoundary.tsx +1 -6
- package/ui/Load/View.tsx +2 -1
- package/ui/Model/EditModal.tsx +2 -1
- package/ui/Popconfirm.tsx +1 -1
- package/ui/Reference/origin.ts +1 -0
- package/ui/Select.tsx +43 -51
- package/ui/Signal/RestApi.tsx +3 -2
- package/ui/System/Client.tsx +4 -1
- package/ui/UiOverride/UiOverride.tsx +2 -0
- package/ui/index.ts +5 -0
- package/ui/overlayLayer.ts +25 -2
- package/vendor/@akanjs/native/native/android/src/com/akanjs/runtime/AkanNativeActivity.kt +31 -13
- package/vendor/@akanjs/native/native/android/src/com/akanjs/runtime/AkanNativePlugin.kt +4 -0
- package/vendor/@akanjs/native/native/desktop/Cargo.lock +1 -0
- package/vendor/@akanjs/native/native/desktop/Cargo.toml +2 -0
- package/vendor/@akanjs/native/native/desktop/src/external.rs +240 -114
- package/vendor/@akanjs/native/native/desktop/src/json.rs +14 -1
- package/vendor/@akanjs/native/native/desktop/src/lib.rs +12 -4
- package/vendor/@akanjs/native/native/desktop/src/linux/external.rs +696 -0
- package/vendor/@akanjs/native/native/desktop/src/linux/mod.rs +1 -0
- package/vendor/@akanjs/native/native/ios/Sources/AkanNativeViewController.swift +6 -1
- package/vendor/@akanjs/native/packages/cli/src/lib/native-plugins.ts +5 -3
- package/vendor/@akanjs/native/packages/cli/src/lib/project.ts +25 -1
- package/vendor/@akanjs/native/packages/desktop/src/deeplinks.ts +2 -2
- package/vendor/@akanjs/native/packages/desktop/src/host.ts +2 -2
- package/vendor/@akanjs/native/packages/desktop/src/relaunch.ts +8 -3
- package/vendor/@akanjs/native/plugins/app/src/desktop.ts +2 -2
- package/vendor/@akanjs/native/plugins/appearance/android/AppearancePlugin.kt +13 -0
- package/vendor/@akanjs/native/plugins/appearance/ios/AppearancePlugin.swift +27 -0
- package/vendor/@akanjs/native/plugins/appearance/native-plugin.json +2 -2
- package/vendor/@akanjs/native/plugins/appearance/src/index.ts +8 -1
- package/vendor/@akanjs/native/plugins/autostart/src/desktop.ts +2 -1
- package/vendor/@akanjs/native/plugins/external-view/native-plugin.json +4 -1
- package/vendor/@akanjs/native/plugins/external-view/src/index.ts +22 -13
- package/vendor/@akanjs/native/plugins/single-instance/src/socket.ts +113 -24
- package/vendor/use-agentic/AgentClientTools.ts +33 -0
- package/vendor/use-agentic/AgentSession.ts +35 -73
- package/vendor/use-agentic/CHANGELOG.md +0 -43
- package/vendor/use-agentic/Compaction.ts +12 -2
- package/vendor/use-agentic/SessionQuestion.ts +99 -0
- package/vendor/use-agentic/ToolRunner.ts +3 -15
- package/vendor/use-agentic/Transcript.ts +8 -5
- package/vendor/use-agentic/WIRE.md +28 -9
- package/vendor/use-agentic/failureCode.ts +44 -0
- package/vendor/use-agentic/httpRunner.ts +22 -8
- package/vendor/use-agentic/index.ts +1 -0
- package/vendor/use-agentic/types.ts +13 -1
- package/webkit/CsrGuards.ts +11 -2
- package/webkit/McpAppFetchBridge.ts +3 -1
- package/webkit/McpAppHost.ts +122 -0
- package/webkit/RenderLayer.tsx +48 -14
- package/webkit/RouteLevelBoundary.tsx +37 -0
- package/webkit/csrRefresh.ts +33 -0
- package/webkit/index.ts +3 -0
- package/webkit/lazy.tsx +5 -1
- package/webkit/needsBrowser.ts +10 -0
- package/webkit/useCamera.tsx +18 -7
- package/webkit/useCsrKeyboard.ts +155 -0
- package/webkit/useCsrLocationState.ts +58 -0
- package/webkit/useCsrNavigation.ts +302 -0
- package/webkit/useCsrRefs.ts +52 -0
- package/webkit/useCsrRenderTrace.ts +68 -0
- package/webkit/useCsrStackLifecycle.ts +120 -0
- package/webkit/useCsrTransitions.ts +552 -0
- package/webkit/useCsrValues.ts +73 -1235
- package/webkit/useGeoLocation.tsx +1 -3
- package/webkit/useHistory.ts +2 -2
- package/webkit/useLocation.ts +3 -2
- package/webkit/useResolvedFrame.ts +116 -0
- package/webkit/useSyncNavigation.ts +109 -0
- package/vendor/@akanjs/native/CHANGELOG.md +0 -21
package/server/rscWorkerHost.ts
CHANGED
|
@@ -1,10 +1,7 @@
|
|
|
1
|
-
import
|
|
2
|
-
import
|
|
3
|
-
import { fileURLToPath } from "node:url";
|
|
4
|
-
import { type AkanI18nConfig, DEFAULT_AKAN_I18N, Logger, type LogRecord } from "akanjs/common";
|
|
1
|
+
import { AkanError } from "akanjs/base";
|
|
2
|
+
import { Logger, type LogRecord } from "akanjs/common";
|
|
5
3
|
import type { AkanTheme } from "akanjs/fetch";
|
|
6
4
|
import type { AkanMetricsReport } from "akanjs/service";
|
|
7
|
-
import { RestartSchedule } from "../common/restartSchedule";
|
|
8
5
|
import type {
|
|
9
6
|
PagePromptEntry,
|
|
10
7
|
PagePromptRun,
|
|
@@ -13,33 +10,28 @@ import type {
|
|
|
13
10
|
} from "../signal/mcp/pagePrompt";
|
|
14
11
|
import type { ClientManifest } from "./artifact";
|
|
15
12
|
import type { RouteCacheInvalidation, RouteCacheRenderState } from "./cachePolicy";
|
|
16
|
-
import {
|
|
17
|
-
import {
|
|
18
|
-
import
|
|
13
|
+
import { type RscAdoptedBundle, RscHostReloads, type RscWorkerReloadInput } from "./rscHostReloads";
|
|
14
|
+
import type { RscRedirectMethod, RscRedirectStatus, RscRenderResult } from "./rscHostRenderStream";
|
|
15
|
+
import { RscHostRequests } from "./rscHostRequests";
|
|
16
|
+
import type { RscPagePromptDrawn } from "./rscStaticApp";
|
|
17
|
+
import { type RscProcess, RscWorkerProcess, type RscWorkerStatus } from "./rscWorkerProcess";
|
|
18
|
+
import { RscWorkerRecycler } from "./rscWorkerRecycler";
|
|
19
|
+
import type { RscRenderOptions, RscTraceMetadata } from "./ssrTypes";
|
|
19
20
|
import type { BaseBuildArtifact, CssAsset } from "./types";
|
|
20
21
|
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
}
|
|
35
|
-
|
|
36
|
-
interface RscCall {
|
|
37
|
-
resolve: (value: unknown) => void;
|
|
38
|
-
reject: (error: Error) => void;
|
|
39
|
-
}
|
|
40
|
-
|
|
41
|
-
export type RscRedirectMethod = "replace" | "push";
|
|
42
|
-
export type RscRedirectStatus = 303 | 307 | 308;
|
|
22
|
+
export type { RscAdoptedBundle, RscReloadFailure, RscWorkerReloadInput } from "./rscHostReloads";
|
|
23
|
+
export {
|
|
24
|
+
createIdempotentRscRenderCancel,
|
|
25
|
+
createRscHostRenderStream,
|
|
26
|
+
getRscHostMaxPendingChunks,
|
|
27
|
+
isRscHostPendingChunkOverflow,
|
|
28
|
+
nextRscHostPendingChunkCount,
|
|
29
|
+
type RscPending,
|
|
30
|
+
type RscRedirectMethod,
|
|
31
|
+
type RscRedirectStatus,
|
|
32
|
+
type RscRenderResult,
|
|
33
|
+
} from "./rscHostRenderStream";
|
|
34
|
+
export type { RscWorkerStatus } from "./rscWorkerProcess";
|
|
43
35
|
|
|
44
36
|
export interface RscWorkerInvalidateCacheMessage {
|
|
45
37
|
type: "invalidate-cache";
|
|
@@ -48,49 +40,6 @@ export interface RscWorkerInvalidateCacheMessage {
|
|
|
48
40
|
paths?: string[];
|
|
49
41
|
}
|
|
50
42
|
|
|
51
|
-
export type RscRenderResult =
|
|
52
|
-
| {
|
|
53
|
-
type: "stream";
|
|
54
|
-
stream: ReadableStream<Uint8Array>;
|
|
55
|
-
theme?: AkanTheme;
|
|
56
|
-
status?: number;
|
|
57
|
-
trace?: RscTraceMetadata;
|
|
58
|
-
lateControl: Promise<SsrLateControl | null>;
|
|
59
|
-
cacheState: Promise<RouteCacheRenderState>;
|
|
60
|
-
cancel: (reason?: unknown) => void;
|
|
61
|
-
}
|
|
62
|
-
| { type: "redirect"; location: string; method: RscRedirectMethod; status: RscRedirectStatus }
|
|
63
|
-
| { type: "not-found" };
|
|
64
|
-
|
|
65
|
-
export function getRscHostMaxPendingChunks(value = process.env.AKAN_RSC_HOST_MAX_PENDING_CHUNKS): number {
|
|
66
|
-
const parsed = Number.parseInt(value ?? "", 10);
|
|
67
|
-
return Number.isFinite(parsed) && parsed > 0 ? parsed : DEFAULT_RSC_HOST_MAX_PENDING_CHUNKS;
|
|
68
|
-
}
|
|
69
|
-
|
|
70
|
-
export function nextRscHostPendingChunkCount(currentPendingChunks: number, desiredSize: number | null): number {
|
|
71
|
-
return desiredSize !== null && desiredSize <= 0 ? currentPendingChunks + 1 : 0;
|
|
72
|
-
}
|
|
73
|
-
|
|
74
|
-
export function isRscHostPendingChunkOverflow(pendingChunks: number, maxPendingChunks: number): boolean {
|
|
75
|
-
return pendingChunks > maxPendingChunks;
|
|
76
|
-
}
|
|
77
|
-
|
|
78
|
-
function createRscRenderAbortError(reason?: unknown): Error {
|
|
79
|
-
if (reason instanceof Error) return reason;
|
|
80
|
-
const error = new Error(reason === undefined ? "rsc render aborted" : String(reason));
|
|
81
|
-
error.name = "AbortError";
|
|
82
|
-
return error;
|
|
83
|
-
}
|
|
84
|
-
|
|
85
|
-
export function createIdempotentRscRenderCancel(onCancel: (reason?: unknown) => void): (reason?: unknown) => void {
|
|
86
|
-
let cancelled = false;
|
|
87
|
-
return (reason?: unknown) => {
|
|
88
|
-
if (cancelled) return;
|
|
89
|
-
cancelled = true;
|
|
90
|
-
onCancel(reason);
|
|
91
|
-
};
|
|
92
|
-
}
|
|
93
|
-
|
|
94
43
|
export function createRscWorkerInvalidateCacheMessage(
|
|
95
44
|
invalidation?: string | RouteCacheInvalidation,
|
|
96
45
|
): RscWorkerInvalidateCacheMessage {
|
|
@@ -152,171 +101,6 @@ export function projectRscWorkerProcessMetrics(metrics: AkanMetricsReport): Akan
|
|
|
152
101
|
};
|
|
153
102
|
}
|
|
154
103
|
|
|
155
|
-
export function createRscHostRenderStream(input: {
|
|
156
|
-
setPending: (pending: RscPending) => void;
|
|
157
|
-
deletePending: () => void;
|
|
158
|
-
sendRenderOrQueue: () => void;
|
|
159
|
-
cancelRender: (reason?: unknown) => void;
|
|
160
|
-
maxPendingChunks?: number;
|
|
161
|
-
signal?: AbortSignal;
|
|
162
|
-
onPendingChunkOverflow?: () => void;
|
|
163
|
-
}): Promise<RscRenderResult> {
|
|
164
|
-
let settled = false;
|
|
165
|
-
let stream!: ReadableStream<Uint8Array>;
|
|
166
|
-
let theme: AkanTheme | undefined;
|
|
167
|
-
let status: number | undefined;
|
|
168
|
-
let trace: RscTraceMetadata | undefined;
|
|
169
|
-
let resolveLateControl!: (control: SsrLateControl | null) => void;
|
|
170
|
-
let resolveCacheState!: (state: RouteCacheRenderState) => void;
|
|
171
|
-
const lateControl = new Promise<SsrLateControl | null>((resolve) => {
|
|
172
|
-
resolveLateControl = resolve;
|
|
173
|
-
});
|
|
174
|
-
const cacheState = new Promise<RouteCacheRenderState>((resolve) => {
|
|
175
|
-
resolveCacheState = resolve;
|
|
176
|
-
});
|
|
177
|
-
let lateControlSettled = false;
|
|
178
|
-
let cacheStateSettled = false;
|
|
179
|
-
const settleLateControl = (control: SsrLateControl | null) => {
|
|
180
|
-
if (lateControlSettled) return;
|
|
181
|
-
lateControlSettled = true;
|
|
182
|
-
resolveLateControl(control);
|
|
183
|
-
};
|
|
184
|
-
const settleCacheState = (state: RouteCacheRenderState) => {
|
|
185
|
-
if (cacheStateSettled) return;
|
|
186
|
-
cacheStateSettled = true;
|
|
187
|
-
resolveCacheState(state);
|
|
188
|
-
};
|
|
189
|
-
const maxPendingChunks = input.maxPendingChunks ?? getRscHostMaxPendingChunks();
|
|
190
|
-
let pendingChunks = 0;
|
|
191
|
-
let removeAbortListener: (() => void) | undefined;
|
|
192
|
-
const cleanupAbortListener = () => {
|
|
193
|
-
removeAbortListener?.();
|
|
194
|
-
removeAbortListener = undefined;
|
|
195
|
-
};
|
|
196
|
-
const cancelRender = createIdempotentRscRenderCancel((reason) => {
|
|
197
|
-
input.deletePending();
|
|
198
|
-
settleLateControl(null);
|
|
199
|
-
settleCacheState({ cacheable: false, reason: "cancelled" });
|
|
200
|
-
input.cancelRender(reason);
|
|
201
|
-
cleanupAbortListener();
|
|
202
|
-
});
|
|
203
|
-
|
|
204
|
-
return new Promise<RscRenderResult>((resolve, reject) => {
|
|
205
|
-
stream = new ReadableStream<Uint8Array>({
|
|
206
|
-
start: (controller) => {
|
|
207
|
-
const abortRender = () => {
|
|
208
|
-
const error = createRscRenderAbortError(input.signal?.reason);
|
|
209
|
-
cancelRender(error);
|
|
210
|
-
if (!settled) {
|
|
211
|
-
settled = true;
|
|
212
|
-
reject(error);
|
|
213
|
-
return;
|
|
214
|
-
}
|
|
215
|
-
controller.error(error);
|
|
216
|
-
};
|
|
217
|
-
const settleStream = () => {
|
|
218
|
-
if (settled) return;
|
|
219
|
-
settled = true;
|
|
220
|
-
resolve({
|
|
221
|
-
type: "stream",
|
|
222
|
-
stream,
|
|
223
|
-
get theme() {
|
|
224
|
-
return theme;
|
|
225
|
-
},
|
|
226
|
-
status,
|
|
227
|
-
trace,
|
|
228
|
-
lateControl,
|
|
229
|
-
cacheState,
|
|
230
|
-
cancel: cancelRender,
|
|
231
|
-
});
|
|
232
|
-
};
|
|
233
|
-
input.setPending({
|
|
234
|
-
onMeta: (meta) => {
|
|
235
|
-
theme = meta.theme;
|
|
236
|
-
status = meta.status;
|
|
237
|
-
trace = meta.trace;
|
|
238
|
-
settleStream();
|
|
239
|
-
},
|
|
240
|
-
onTheme: (next) => {
|
|
241
|
-
theme = next;
|
|
242
|
-
},
|
|
243
|
-
onChunk: (data) => {
|
|
244
|
-
settleStream();
|
|
245
|
-
pendingChunks = nextRscHostPendingChunkCount(pendingChunks, controller.desiredSize);
|
|
246
|
-
if (isRscHostPendingChunkOverflow(pendingChunks, maxPendingChunks)) {
|
|
247
|
-
const error = new Error(`rsc worker host queue exceeded ${maxPendingChunks} pending chunks`);
|
|
248
|
-
input.onPendingChunkOverflow?.();
|
|
249
|
-
cancelRender(error);
|
|
250
|
-
controller.error(error);
|
|
251
|
-
return;
|
|
252
|
-
}
|
|
253
|
-
controller.enqueue(data);
|
|
254
|
-
},
|
|
255
|
-
onEnd: () => {
|
|
256
|
-
settleLateControl(null);
|
|
257
|
-
settleCacheState({ cacheable: false, reason: "missing-cache-state" });
|
|
258
|
-
cleanupAbortListener();
|
|
259
|
-
settleStream();
|
|
260
|
-
controller.close();
|
|
261
|
-
},
|
|
262
|
-
onError: (msg) => {
|
|
263
|
-
settleLateControl(null);
|
|
264
|
-
settleCacheState({ cacheable: false, reason: "error" });
|
|
265
|
-
cleanupAbortListener();
|
|
266
|
-
if (!settled) {
|
|
267
|
-
settled = true;
|
|
268
|
-
reject(new Error(msg));
|
|
269
|
-
return;
|
|
270
|
-
}
|
|
271
|
-
controller.error(new Error(msg));
|
|
272
|
-
},
|
|
273
|
-
onRedirect: (location, method, status) => {
|
|
274
|
-
settleLateControl(null);
|
|
275
|
-
settleCacheState({ cacheable: false, reason: "redirect" });
|
|
276
|
-
cleanupAbortListener();
|
|
277
|
-
if (!settled) {
|
|
278
|
-
settled = true;
|
|
279
|
-
resolve({ type: "redirect", location, method, status });
|
|
280
|
-
controller.close();
|
|
281
|
-
return;
|
|
282
|
-
}
|
|
283
|
-
controller.error(new Error(`redirect after stream started: ${location}`));
|
|
284
|
-
},
|
|
285
|
-
onLateRedirect: (location, method, status) =>
|
|
286
|
-
settleLateControl({ type: "redirect", location, method, status }),
|
|
287
|
-
onLateNotFound: () => settleLateControl({ type: "not-found" }),
|
|
288
|
-
onCacheState: settleCacheState,
|
|
289
|
-
onNotFound: () => {
|
|
290
|
-
settleLateControl(null);
|
|
291
|
-
settleCacheState({ cacheable: false, reason: "not-found" });
|
|
292
|
-
cleanupAbortListener();
|
|
293
|
-
if (!settled) {
|
|
294
|
-
settled = true;
|
|
295
|
-
resolve({ type: "not-found" });
|
|
296
|
-
controller.close();
|
|
297
|
-
return;
|
|
298
|
-
}
|
|
299
|
-
controller.error(new Error("not-found after stream started"));
|
|
300
|
-
},
|
|
301
|
-
});
|
|
302
|
-
if (input.signal) {
|
|
303
|
-
if (input.signal.aborted) {
|
|
304
|
-
abortRender();
|
|
305
|
-
return;
|
|
306
|
-
}
|
|
307
|
-
input.signal.addEventListener("abort", abortRender, { once: true });
|
|
308
|
-
removeAbortListener = () => input.signal?.removeEventListener("abort", abortRender);
|
|
309
|
-
}
|
|
310
|
-
input.sendRenderOrQueue();
|
|
311
|
-
},
|
|
312
|
-
cancel: cancelRender,
|
|
313
|
-
pull: () => {
|
|
314
|
-
pendingChunks = Math.max(0, pendingChunks - 1);
|
|
315
|
-
},
|
|
316
|
-
});
|
|
317
|
-
});
|
|
318
|
-
}
|
|
319
|
-
|
|
320
104
|
type RscInMsg =
|
|
321
105
|
| { type: "hello" }
|
|
322
106
|
| { type: "ready" }
|
|
@@ -340,6 +124,7 @@ type RscInMsg =
|
|
|
340
124
|
| { type: "log.records"; records: LogRecord[]; dropped?: number }
|
|
341
125
|
| { type: "page-prompts.result"; requestId: string; result: PagePromptEntry[] }
|
|
342
126
|
| { type: "page-prompt.result"; requestId: string; result: PagePromptRun }
|
|
127
|
+
| { type: "page-prompt.draw.result"; requestId: string; result: RscPagePromptDrawn }
|
|
343
128
|
| { type: "page-prompts.visible.result"; requestId: string; result: string[] }
|
|
344
129
|
| { type: "web-manifest.result"; requestId: string; result: unknown }
|
|
345
130
|
| {
|
|
@@ -351,187 +136,95 @@ type RscInMsg =
|
|
|
351
136
|
running?: RscAdoptedBundle;
|
|
352
137
|
};
|
|
353
138
|
|
|
354
|
-
export interface RscWorkerReloadInput {
|
|
355
|
-
clientManifest: ClientManifest;
|
|
356
|
-
cssAssets?: Record<string, CssAsset>;
|
|
357
|
-
buildId: number;
|
|
358
|
-
/** Undefined keeps the latest bundle a reload named (e.g. a client-manifest-only reload after a lazy route build). */
|
|
359
|
-
pagesBundlePath?: string;
|
|
360
|
-
}
|
|
361
|
-
|
|
362
|
-
export interface RscAdoptedBundle {
|
|
363
|
-
pagesBundlePath: string;
|
|
364
|
-
buildId: number;
|
|
365
|
-
}
|
|
366
|
-
|
|
367
|
-
interface RscReloadWaiter {
|
|
368
|
-
resolve: (adopted: RscAdoptedBundle) => void;
|
|
369
|
-
reject: (err: RscReloadFailure) => void;
|
|
370
|
-
}
|
|
371
|
-
|
|
372
|
-
/** A reload the worker did not take: what it runs instead, and whether a later reload failed in this one's place. */
|
|
373
|
-
export interface RscReloadFailure extends Error {
|
|
374
|
-
/** What the worker runs now. */
|
|
375
|
-
adopted: RscAdoptedBundle;
|
|
376
|
-
/** The latest state it was asked to take, which a later reload may have moved past the caller's own. */
|
|
377
|
-
failed: RscAdoptedBundle;
|
|
378
|
-
}
|
|
379
|
-
|
|
380
|
-
export type RscWorkerStatus = "starting" | "ready" | "restarting" | "stopped";
|
|
381
|
-
type RscProcess = Bun.Subprocess;
|
|
382
|
-
|
|
383
139
|
export class RscWorker {
|
|
384
|
-
static readonly #
|
|
385
|
-
static readonly #devMaxRssBytes = 768 * 1024 * 1024;
|
|
140
|
+
static readonly #queuedRespawns = 2;
|
|
386
141
|
readonly ready: Promise<void>;
|
|
387
142
|
#logger = new Logger("RscWorker");
|
|
388
|
-
|
|
389
|
-
#
|
|
390
|
-
readonly #
|
|
391
|
-
readonly #
|
|
392
|
-
#clientManifest: ClientManifest;
|
|
393
|
-
#pagesBundlePath: string;
|
|
394
|
-
#pagesBundleBuildId: number;
|
|
395
|
-
#cssAssets: Record<string, CssAsset>;
|
|
396
|
-
#basePaths: string[];
|
|
397
|
-
#i18n: AkanI18nConfig;
|
|
398
|
-
#resolveReady!: () => void;
|
|
399
|
-
#rejectReady!: (err: Error) => void;
|
|
400
|
-
#readyResolved = false;
|
|
401
|
-
|
|
402
|
-
#pendingReload: {
|
|
403
|
-
waiters: RscReloadWaiter[];
|
|
404
|
-
reloadState: number;
|
|
405
|
-
reloadId: number | null;
|
|
406
|
-
pagesBundlePath: string;
|
|
407
|
-
buildId: number;
|
|
408
|
-
} | null = null;
|
|
409
|
-
#nextReloadId = 1;
|
|
410
|
-
#answeredReloadId = 0;
|
|
143
|
+
readonly #worker: RscWorkerProcess;
|
|
144
|
+
readonly #requests: RscHostRequests;
|
|
145
|
+
readonly #recycler: RscWorkerRecycler;
|
|
146
|
+
readonly #reloads: RscHostReloads;
|
|
411
147
|
|
|
412
148
|
#droppedWhileBooting = false;
|
|
413
|
-
#reloadState = 0;
|
|
414
|
-
#init: RscAdoptedBundle & { reloadState: number; cssAssets: Record<string, CssAsset> } = {
|
|
415
|
-
pagesBundlePath: "",
|
|
416
|
-
buildId: 0,
|
|
417
|
-
reloadState: 0,
|
|
418
|
-
cssAssets: {},
|
|
419
|
-
};
|
|
420
|
-
/** What the worker runs: a respawn falls back to it when the bundle a reload named failed to import. */
|
|
421
|
-
#adopted: RscAdoptedBundle;
|
|
422
|
-
|
|
423
|
-
#status: RscWorkerStatus = "starting";
|
|
424
|
-
#killed = false;
|
|
425
|
-
#queuedSends: { requestId: string; send: () => void }[] = [];
|
|
426
|
-
readonly #restart = new RestartSchedule({ min: 200, max: 30_000 });
|
|
427
|
-
#restartCount = 0;
|
|
428
|
-
#recycleCount = 0;
|
|
429
|
-
#reloadsSinceSpawn = 0;
|
|
430
|
-
#lastRecycleAtMono: number | null = null;
|
|
431
|
-
#lastRecycleReason: string | undefined;
|
|
432
|
-
#lastWorkerMetrics: AkanMetricsReport = {};
|
|
433
149
|
onLogRecords: ((records: LogRecord[], dropped: number) => void) | null = null;
|
|
434
150
|
#logLevel: number | null = null;
|
|
435
|
-
#hostPendingChunkOverflowCount = 0;
|
|
436
|
-
#recycleTimer: ReturnType<typeof setTimeout> | null = null;
|
|
437
|
-
#rollingRecycle: { oldProc: RscProcess; reason: string; reloadsSinceSpawn: number } | null = null;
|
|
438
|
-
#booting: RscProcess | null = null;
|
|
439
|
-
|
|
440
151
|
readonly #failBeforeReady: boolean;
|
|
441
|
-
static readonly #queuedRespawns = 2;
|
|
442
152
|
|
|
443
153
|
constructor(artifact: BaseBuildArtifact, { failBeforeReady = false }: { failBeforeReady?: boolean } = {}) {
|
|
444
154
|
this.#failBeforeReady = failBeforeReady;
|
|
445
|
-
|
|
446
|
-
|
|
447
|
-
|
|
448
|
-
|
|
449
|
-
|
|
450
|
-
|
|
451
|
-
|
|
452
|
-
this.ready = new Promise<void>((resolve, reject) => {
|
|
453
|
-
this.#resolveReady = () => {
|
|
454
|
-
if (this.#readyResolved) return;
|
|
455
|
-
this.#readyResolved = true;
|
|
456
|
-
resolve();
|
|
457
|
-
};
|
|
458
|
-
this.#rejectReady = (err) => {
|
|
459
|
-
if (this.#readyResolved) return;
|
|
460
|
-
this.#readyResolved = true;
|
|
461
|
-
reject(err);
|
|
462
|
-
};
|
|
155
|
+
|
|
156
|
+
const read = RscHostReloads.readArtifact(artifact);
|
|
157
|
+
const logger = this.#logger;
|
|
158
|
+
const worker = new RscWorkerProcess(logger, {
|
|
159
|
+
onMessage: (message, proc) => this.#handleMessage(message as RscInMsg, proc),
|
|
160
|
+
onExit: (proc, code) => this.#handleExit(proc, code),
|
|
161
|
+
logRecords: () => this.onLogRecords,
|
|
463
162
|
});
|
|
464
|
-
|
|
465
|
-
|
|
163
|
+
const requests = new RscHostRequests(worker, logger);
|
|
164
|
+
const recycler = new RscWorkerRecycler(worker, requests, logger);
|
|
165
|
+
this.#worker = worker;
|
|
166
|
+
this.#requests = requests;
|
|
167
|
+
this.#recycler = recycler;
|
|
168
|
+
this.#reloads = new RscHostReloads(read, worker, recycler);
|
|
169
|
+
this.ready = worker.ready;
|
|
466
170
|
}
|
|
467
171
|
|
|
468
172
|
renderWithMeta(req: Request, options: RscRenderOptions = {}): Promise<RscRenderResult> {
|
|
469
|
-
|
|
470
|
-
return createRscHostRenderStream({
|
|
471
|
-
setPending: (pending) => this.#pending.set(requestId, pending),
|
|
472
|
-
deletePending: () => this.#pending.delete(requestId),
|
|
473
|
-
sendRenderOrQueue: () => this.#sendRenderOrQueue(requestId, req, options),
|
|
474
|
-
cancelRender: () => this.#cancelRender(requestId),
|
|
475
|
-
signal: options.signal,
|
|
476
|
-
onPendingChunkOverflow: () => {
|
|
477
|
-
this.#hostPendingChunkOverflowCount += 1;
|
|
478
|
-
},
|
|
479
|
-
});
|
|
173
|
+
return this.#requests.render(req, options);
|
|
480
174
|
}
|
|
481
175
|
|
|
482
176
|
listPagePrompts(): Promise<PagePromptEntry[]> {
|
|
483
|
-
return this.#call<PagePromptEntry[]>((requestId) => ({ type: "page-prompts", requestId }));
|
|
177
|
+
return this.#requests.call<PagePromptEntry[]>((requestId) => ({ type: "page-prompts", requestId }));
|
|
484
178
|
}
|
|
485
179
|
|
|
486
180
|
runPagePrompt(input: PagePromptRunInput): Promise<PagePromptRun> {
|
|
487
|
-
return this.#call<PagePromptRun>((requestId) => ({ type: "page-prompt.run", requestId, input }));
|
|
181
|
+
return this.#requests.call<PagePromptRun>((requestId) => ({ type: "page-prompt.run", requestId, input }));
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
async drawPagePrompt(
|
|
185
|
+
input: PagePromptRunInput,
|
|
186
|
+
clientManifest: ClientManifest | undefined,
|
|
187
|
+
deadlineMs: number,
|
|
188
|
+
): Promise<RscPagePromptDrawn> {
|
|
189
|
+
const deadlineAt = Date.now() + deadlineMs;
|
|
190
|
+
let timer: ReturnType<typeof setTimeout> | undefined;
|
|
191
|
+
const expired = new Promise<never>((_, reject) => {
|
|
192
|
+
timer = setTimeout(
|
|
193
|
+
() =>
|
|
194
|
+
reject(new AkanError("rscWorkerFailed", `static app "${input.name}" passed its ${deadlineMs} ms deadline`)),
|
|
195
|
+
Math.max(0, deadlineMs),
|
|
196
|
+
);
|
|
197
|
+
});
|
|
198
|
+
const drawn = this.#requests.call<RscPagePromptDrawn>((requestId) => ({
|
|
199
|
+
type: "page-prompt.draw",
|
|
200
|
+
requestId,
|
|
201
|
+
input,
|
|
202
|
+
clientManifest,
|
|
203
|
+
deadlineMs: Math.max(0, deadlineAt - Date.now()),
|
|
204
|
+
}));
|
|
205
|
+
try {
|
|
206
|
+
return await Promise.race([drawn, expired]);
|
|
207
|
+
} finally {
|
|
208
|
+
clearTimeout(timer);
|
|
209
|
+
}
|
|
488
210
|
}
|
|
489
211
|
|
|
490
212
|
visiblePagePrompts(input: PagePromptVisibleInput): Promise<string[]> {
|
|
491
|
-
return this.#call<string[]>((requestId) => ({ type: "page-prompts.visible", requestId, input }));
|
|
213
|
+
return this.#requests.call<string[]>((requestId) => ({ type: "page-prompts.visible", requestId, input }));
|
|
492
214
|
}
|
|
493
215
|
|
|
494
216
|
/** The root layout's `.manifest()` for `basePath`, in the browser's keys; `null` when it declares none. */
|
|
495
217
|
webManifest(basePath: string | null): Promise<unknown> {
|
|
496
|
-
return this.#call<unknown>((requestId) => ({ type: "web-manifest", requestId, basePath }));
|
|
497
|
-
}
|
|
498
|
-
|
|
499
|
-
#call<T>(message: (requestId: string) => object): Promise<T> {
|
|
500
|
-
const requestId = crypto.randomUUID();
|
|
501
|
-
return new Promise<T>((resolve, reject) => {
|
|
502
|
-
this.#calls.set(requestId, { resolve: resolve as (value: unknown) => void, reject });
|
|
503
|
-
const send = () => {
|
|
504
|
-
if (!this.#calls.has(requestId)) return;
|
|
505
|
-
try {
|
|
506
|
-
this.#proc.send(message(requestId));
|
|
507
|
-
} catch (err) {
|
|
508
|
-
this.#settleCall(requestId, (call) =>
|
|
509
|
-
call.reject(new Error(`rsc worker send failed: ${err instanceof Error ? err.message : String(err)}`)),
|
|
510
|
-
);
|
|
511
|
-
}
|
|
512
|
-
};
|
|
513
|
-
if (this.#status === "ready") send();
|
|
514
|
-
else if (this.#status === "stopped")
|
|
515
|
-
this.#settleCall(requestId, (call) => call.reject(new Error("rsc worker is stopped")));
|
|
516
|
-
else this.#queuedSends.push({ requestId, send });
|
|
517
|
-
});
|
|
518
|
-
}
|
|
519
|
-
|
|
520
|
-
#settleCall(requestId: string, fn: (call: RscCall) => void): boolean {
|
|
521
|
-
const call = this.#calls.get(requestId);
|
|
522
|
-
if (!call) return false;
|
|
523
|
-
this.#calls.delete(requestId);
|
|
524
|
-
fn(call);
|
|
525
|
-
return true;
|
|
218
|
+
return this.#requests.call<unknown>((requestId) => ({ type: "web-manifest", requestId, basePath }));
|
|
526
219
|
}
|
|
527
220
|
|
|
528
221
|
invalidateRouteResultCache(invalidation?: string | RouteCacheInvalidation): void {
|
|
529
|
-
if (this.#status !== "ready") {
|
|
222
|
+
if (this.#worker.status !== "ready") {
|
|
530
223
|
this.#droppedWhileBooting = true;
|
|
531
224
|
return;
|
|
532
225
|
}
|
|
533
226
|
try {
|
|
534
|
-
this.#proc.send(createRscWorkerInvalidateCacheMessage(invalidation));
|
|
227
|
+
this.#worker.proc.send(createRscWorkerInvalidateCacheMessage(invalidation));
|
|
535
228
|
} catch (error) {
|
|
536
229
|
this.#logger.warn(
|
|
537
230
|
`rsc worker cache invalidate send failed: ${error instanceof Error ? error.message : String(error)}`,
|
|
@@ -540,322 +233,143 @@ export class RscWorker {
|
|
|
540
233
|
}
|
|
541
234
|
|
|
542
235
|
kill(): void {
|
|
543
|
-
this.#killed = true;
|
|
544
|
-
this.#status = "stopped";
|
|
545
|
-
this.#restart.cancel();
|
|
546
|
-
|
|
547
|
-
this.#
|
|
548
|
-
this.#rollingRecycle?.oldProc.kill();
|
|
549
|
-
this.#rollingRecycle = null;
|
|
550
|
-
this.#proc.kill();
|
|
236
|
+
this.#worker.killed = true;
|
|
237
|
+
this.#worker.status = "stopped";
|
|
238
|
+
this.#worker.restart.cancel();
|
|
239
|
+
this.#recycler.stop();
|
|
240
|
+
this.#worker.proc.kill();
|
|
551
241
|
}
|
|
552
242
|
|
|
553
243
|
setLogLevel(minSev: number | null) {
|
|
554
244
|
this.#logLevel = minSev;
|
|
555
|
-
if (this.#status !== "ready") this.#droppedWhileBooting = true;
|
|
245
|
+
if (this.#worker.status !== "ready") this.#droppedWhileBooting = true;
|
|
556
246
|
this.#sendLogLevel();
|
|
557
247
|
}
|
|
558
248
|
|
|
559
249
|
#sendLogLevel() {
|
|
560
|
-
if (this.#status !== "ready") return;
|
|
250
|
+
if (this.#worker.status !== "ready") return;
|
|
561
251
|
try {
|
|
562
|
-
this.#proc.send({ type: "log-level", minSev: this.#logLevel });
|
|
252
|
+
this.#worker.proc.send({ type: "log-level", minSev: this.#logLevel });
|
|
563
253
|
} catch {
|
|
564
254
|
}
|
|
565
255
|
}
|
|
566
256
|
|
|
567
257
|
get status(): RscWorkerStatus {
|
|
568
|
-
return this.#status;
|
|
258
|
+
return this.#worker.status;
|
|
569
259
|
}
|
|
570
260
|
|
|
571
261
|
getMetrics(): AkanMetricsReport {
|
|
572
|
-
return Object.assign(projectRscWorkerProcessMetrics(this.#lastWorkerMetrics), {
|
|
573
|
-
rscWorkerPid: this.#proc.pid,
|
|
574
|
-
rscWorkerStatus: this.#status,
|
|
575
|
-
rscWorkerRestartCount: this.#restartCount,
|
|
576
|
-
rscWorkerRecycleCount: this.#recycleCount,
|
|
577
|
-
rscWorkerLastRecycleReason: this.#lastRecycleReason,
|
|
578
|
-
rscPendingRenderCount: this.#pending.size,
|
|
579
|
-
rscQueuedSendCount: this.#queuedSends.length,
|
|
580
|
-
rscHostPendingChunkOverflowCount: this.#hostPendingChunkOverflowCount,
|
|
262
|
+
return Object.assign(projectRscWorkerProcessMetrics(this.#worker.lastWorkerMetrics), {
|
|
263
|
+
rscWorkerPid: this.#worker.proc.pid,
|
|
264
|
+
rscWorkerStatus: this.#worker.status,
|
|
265
|
+
rscWorkerRestartCount: this.#worker.restartCount,
|
|
266
|
+
rscWorkerRecycleCount: this.#recycler.recycleCount,
|
|
267
|
+
rscWorkerLastRecycleReason: this.#recycler.lastRecycleReason,
|
|
268
|
+
rscPendingRenderCount: this.#requests.pending.size,
|
|
269
|
+
rscQueuedSendCount: this.#requests.queuedSends.length,
|
|
270
|
+
rscHostPendingChunkOverflowCount: this.#requests.hostPendingChunkOverflowCount,
|
|
581
271
|
});
|
|
582
272
|
}
|
|
583
273
|
|
|
584
274
|
restartWhenIdle(reason: string): boolean {
|
|
585
|
-
|
|
586
|
-
return false;
|
|
587
|
-
}
|
|
588
|
-
if (this.#pending.size > 0 || this.#calls.size > 0 || this.#queuedSends.length > 0) {
|
|
589
|
-
if (!this.#recycleTimer) {
|
|
590
|
-
const graceMs = MemoryLimit.parsePositiveIntEnv("AKAN_RSC_WORKER_RECYCLE_GRACE_MS") ?? 5_000;
|
|
591
|
-
this.#recycleTimer = setTimeout(() => {
|
|
592
|
-
this.#recycleTimer = null;
|
|
593
|
-
this.restartWhenIdle(reason);
|
|
594
|
-
}, graceMs);
|
|
595
|
-
}
|
|
596
|
-
return false;
|
|
597
|
-
}
|
|
598
|
-
this.#lastRecycleReason = reason;
|
|
599
|
-
this.#recycleCount += 1;
|
|
600
|
-
this.#lastRecycleAtMono = performance.now();
|
|
601
|
-
this.#logger.info(`[rsc] rolling recycle worker reason=${reason} oldPid=${this.#proc.pid}`);
|
|
602
|
-
this.#status = "restarting";
|
|
603
|
-
this.#rollingRecycle = { oldProc: this.#proc, reason, reloadsSinceSpawn: this.#reloadsSinceSpawn };
|
|
604
|
-
this.#proc = this.#spawn();
|
|
605
|
-
return true;
|
|
275
|
+
return this.#recycler.restartWhenIdle(reason);
|
|
606
276
|
}
|
|
607
277
|
|
|
608
278
|
updateCssAssets(cssAssets: Record<string, CssAsset>): void {
|
|
609
|
-
this.#cssAssets = cssAssets;
|
|
610
|
-
if (this.#status !== "ready") {
|
|
279
|
+
this.#reloads.cssAssets = cssAssets;
|
|
280
|
+
if (this.#worker.status !== "ready") {
|
|
611
281
|
this.#droppedWhileBooting = true;
|
|
612
282
|
return;
|
|
613
283
|
}
|
|
614
284
|
try {
|
|
615
|
-
this.#proc.send({ type: "updateCssAssets", cssAssets });
|
|
285
|
+
this.#worker.proc.send({ type: "updateCssAssets", cssAssets });
|
|
616
286
|
} catch {
|
|
617
287
|
}
|
|
618
288
|
}
|
|
619
289
|
|
|
620
290
|
/** Resolves with the pages bundle the worker runs once it has taken this reload, or a later one. */
|
|
621
291
|
reload(input: RscWorkerReloadInput): Promise<RscAdoptedBundle> {
|
|
622
|
-
|
|
623
|
-
this.#reloadState += 1;
|
|
624
|
-
this.#clientManifest = input.clientManifest;
|
|
625
|
-
this.#cssAssets = input.cssAssets ?? this.#cssAssets;
|
|
626
|
-
this.#pagesBundleBuildId = input.buildId;
|
|
627
|
-
if (input.pagesBundlePath) this.#pagesBundlePath = input.pagesBundlePath;
|
|
628
|
-
return new Promise<RscAdoptedBundle>((resolve, reject) => {
|
|
629
|
-
|
|
630
|
-
this.#pendingReload = {
|
|
631
|
-
waiters: [...(this.#pendingReload?.waiters ?? []), { resolve, reject }],
|
|
632
|
-
reloadState: this.#reloadState,
|
|
633
|
-
reloadId: null,
|
|
634
|
-
pagesBundlePath: this.#pagesBundlePath,
|
|
635
|
-
buildId: this.#pagesBundleBuildId,
|
|
636
|
-
};
|
|
637
|
-
if (this.#status === "ready") this.#sendPendingReload();
|
|
638
|
-
});
|
|
639
|
-
}
|
|
640
|
-
|
|
641
|
-
#sendPendingReload(): void {
|
|
642
|
-
const pending = this.#pendingReload;
|
|
643
|
-
if (!pending) return;
|
|
644
|
-
|
|
645
|
-
const answering = this.#answeredReloadId < this.#nextReloadId - 1;
|
|
646
|
-
if (!answering && this.#shouldRecycleForReloadAccumulation() && this.restartWhenIdle("pages-reload-accumulation"))
|
|
647
|
-
return;
|
|
648
|
-
this.#reloadsSinceSpawn += 1;
|
|
649
|
-
pending.reloadId = this.#nextReloadId++;
|
|
650
|
-
try {
|
|
651
|
-
|
|
652
|
-
this.#proc.send({
|
|
653
|
-
type: "reload",
|
|
654
|
-
clientManifest: this.#clientManifest,
|
|
655
|
-
cssAssets: this.#cssAssets,
|
|
656
|
-
buildId: pending.buildId,
|
|
657
|
-
pagesBundlePath: pending.pagesBundlePath,
|
|
658
|
-
reloadId: pending.reloadId,
|
|
659
|
-
});
|
|
660
|
-
} catch {
|
|
661
|
-
|
|
662
|
-
pending.reloadId = null;
|
|
663
|
-
}
|
|
664
|
-
}
|
|
665
|
-
|
|
666
|
-
#resolveReload(adopted: RscAdoptedBundle): void {
|
|
667
|
-
this.#adopted = adopted;
|
|
668
|
-
const pending = this.#pendingReload;
|
|
669
|
-
if (!pending) return;
|
|
670
|
-
this.#pendingReload = null;
|
|
671
|
-
for (const waiter of pending.waiters) waiter.resolve(adopted);
|
|
672
|
-
}
|
|
673
|
-
|
|
674
|
-
#rejectReload(message: string): void {
|
|
675
|
-
this.#pagesBundlePath = this.#adopted.pagesBundlePath;
|
|
676
|
-
this.#pagesBundleBuildId = this.#adopted.buildId;
|
|
677
|
-
const pending = this.#pendingReload;
|
|
678
|
-
if (!pending) return;
|
|
679
|
-
this.#pendingReload = null;
|
|
680
|
-
const failed = { pagesBundlePath: pending.pagesBundlePath, buildId: pending.buildId };
|
|
681
|
-
for (const waiter of pending.waiters)
|
|
682
|
-
waiter.reject(Object.assign(new Error(message), { adopted: this.#adopted, failed }));
|
|
683
|
-
}
|
|
684
|
-
|
|
685
|
-
#spawn(): RscProcess {
|
|
686
|
-
this.#status = "starting";
|
|
687
|
-
this.#reloadsSinceSpawn = 0;
|
|
688
|
-
const workerPath = this.#resolveWorkerPath();
|
|
689
|
-
let proc!: RscProcess;
|
|
690
|
-
const earlyMessages: RscInMsg[] = [];
|
|
691
|
-
|
|
692
|
-
const piped = Logger.isNdjson;
|
|
693
|
-
proc = Bun.spawn(["bun", "--conditions", "react-server", workerPath], {
|
|
694
|
-
ipc: (message: RscInMsg) => {
|
|
695
|
-
if (!proc) {
|
|
696
|
-
earlyMessages.push(message);
|
|
697
|
-
return;
|
|
698
|
-
}
|
|
699
|
-
this.#handleMessage(message, proc);
|
|
700
|
-
},
|
|
701
|
-
stdio: ["ignore", piped ? "pipe" : "inherit", piped ? "pipe" : "inherit"],
|
|
702
|
-
serialization: "advanced",
|
|
703
|
-
env: { ...process.env },
|
|
704
|
-
});
|
|
705
|
-
if (piped) this.#readOutput(proc);
|
|
706
|
-
if (earlyMessages.length > 0) {
|
|
707
|
-
setTimeout(() => {
|
|
708
|
-
for (const message of earlyMessages.splice(0)) this.#handleMessage(message, proc);
|
|
709
|
-
}, 0);
|
|
710
|
-
}
|
|
711
|
-
proc.exited.then((code) => this.#handleExit(proc, code));
|
|
712
|
-
return proc;
|
|
713
|
-
}
|
|
714
|
-
|
|
715
|
-
#readOutput(proc: RscProcess) {
|
|
716
|
-
const replicaIdx = Number(process.env.AKAN_REPLICA_IDX);
|
|
717
|
-
const record = (type: "stdout" | "stderr", text: string) =>
|
|
718
|
-
this.onLogRecords?.(
|
|
719
|
-
[
|
|
720
|
-
ChildOutputReader.toRecord({
|
|
721
|
-
type,
|
|
722
|
-
text,
|
|
723
|
-
name: "rsc-worker",
|
|
724
|
-
role: "rsc-worker",
|
|
725
|
-
replicaIdx: Number.isInteger(replicaIdx) ? replicaIdx : null,
|
|
726
|
-
pid: proc.pid,
|
|
727
|
-
}),
|
|
728
|
-
],
|
|
729
|
-
0,
|
|
730
|
-
);
|
|
731
|
-
const reader = new ChildOutputReader({
|
|
732
|
-
onLine: (line) => record("stdout", line),
|
|
733
|
-
onBlock: (lines) => record("stderr", lines.join("")),
|
|
734
|
-
});
|
|
735
|
-
void reader.pipe(proc.stdout instanceof ReadableStream ? proc.stdout : null, "stdout");
|
|
736
|
-
void reader.pipe(proc.stderr instanceof ReadableStream ? proc.stderr : null, "stderr");
|
|
737
|
-
}
|
|
738
|
-
|
|
739
|
-
#resolveWorkerPath(): string {
|
|
740
|
-
if (process.env.AKAN_RSC_WORKER_PATH) return path.resolve(process.env.AKAN_RSC_WORKER_PATH);
|
|
741
|
-
|
|
742
|
-
const distWorkerPath = path.join(process.cwd(), "rscWorker.js");
|
|
743
|
-
if (fs.existsSync(distWorkerPath)) return distWorkerPath;
|
|
744
|
-
|
|
745
|
-
try {
|
|
746
|
-
return Bun.resolveSync("akanjs/server/rsc-worker", import.meta.dir);
|
|
747
|
-
} catch {
|
|
748
|
-
return fileURLToPath(new URL("./rscWorker.tsx", import.meta.url));
|
|
749
|
-
}
|
|
292
|
+
return this.#reloads.reload(input);
|
|
750
293
|
}
|
|
751
294
|
|
|
752
295
|
#handleMessage(message: RscInMsg, proc: RscProcess): void {
|
|
753
|
-
if (proc !== this.#proc) return;
|
|
296
|
+
if (proc !== this.#worker.proc) return;
|
|
754
297
|
if (message.type === "cache-state") {
|
|
755
|
-
this.#pending.get(message.requestId)?.onCacheState?.(message.state);
|
|
298
|
+
this.#requests.pending.get(message.requestId)?.onCacheState?.(message.state);
|
|
756
299
|
return;
|
|
757
300
|
}
|
|
758
301
|
switch (message.type) {
|
|
759
302
|
case "hello":
|
|
760
303
|
|
|
761
|
-
this.#booting = proc;
|
|
762
|
-
this.#
|
|
763
|
-
pagesBundlePath: this.#pagesBundlePath,
|
|
764
|
-
buildId: this.#pagesBundleBuildId,
|
|
765
|
-
reloadState: this.#reloadState,
|
|
766
|
-
cssAssets: this.#cssAssets,
|
|
767
|
-
};
|
|
768
|
-
this.#proc.send({
|
|
769
|
-
type: "init",
|
|
770
|
-
clientManifest: this.#clientManifest,
|
|
771
|
-
pagesBundlePath: this.#pagesBundlePath,
|
|
772
|
-
pagesBundleBuildId: this.#pagesBundleBuildId,
|
|
773
|
-
cssAssets: this.#cssAssets,
|
|
774
|
-
basePaths: this.#basePaths,
|
|
775
|
-
i18n: this.#i18n,
|
|
776
|
-
});
|
|
304
|
+
this.#worker.booting = proc;
|
|
305
|
+
this.#reloads.sendInit();
|
|
777
306
|
return;
|
|
778
307
|
case "ready":
|
|
779
|
-
this.#booting = null;
|
|
780
|
-
this.#status = "ready";
|
|
781
|
-
this.#
|
|
308
|
+
this.#worker.booting = null;
|
|
309
|
+
this.#worker.status = "ready";
|
|
310
|
+
this.#reloads.answerSentReloads();
|
|
782
311
|
this.#droppedWhileBooting = false;
|
|
783
|
-
this.#restart.reset();
|
|
784
|
-
this.#resolveReady();
|
|
785
|
-
this.#finishRollingRecycle();
|
|
312
|
+
this.#worker.restart.reset();
|
|
313
|
+
this.#worker.resolveReady();
|
|
314
|
+
this.#recycler.finishRollingRecycle();
|
|
786
315
|
|
|
787
|
-
if (this.#cssAssets !== this.#init.cssAssets) this.updateCssAssets(this.#cssAssets);
|
|
788
|
-
this.#flushQueuedSends();
|
|
316
|
+
if (this.#reloads.cssAssets !== this.#reloads.init.cssAssets) this.updateCssAssets(this.#reloads.cssAssets);
|
|
317
|
+
this.#requests.flushQueuedSends();
|
|
789
318
|
if (this.#logLevel !== null) this.#sendLogLevel();
|
|
790
|
-
|
|
791
|
-
this.#adopted = { pagesBundlePath: this.#init.pagesBundlePath, buildId: this.#init.buildId };
|
|
792
|
-
if (this.#pendingReload && this.#pendingReload.reloadState <= this.#init.reloadState)
|
|
793
|
-
this.#resolveReload(this.#adopted);
|
|
794
|
-
else this.#sendPendingReload();
|
|
319
|
+
this.#reloads.adoptInit();
|
|
795
320
|
return;
|
|
796
|
-
case "reloaded":
|
|
797
|
-
|
|
798
|
-
|
|
799
|
-
if (proc === this.#proc && message.pagesBundlePath) {
|
|
800
|
-
this.#adopted = { pagesBundlePath: message.pagesBundlePath, buildId: message.buildId };
|
|
801
|
-
this.#answeredReloadId = Math.max(this.#answeredReloadId, message.reloadId ?? 0);
|
|
802
|
-
if (!pending) {
|
|
803
|
-
this.#pagesBundlePath = message.pagesBundlePath;
|
|
804
|
-
this.#pagesBundleBuildId = message.buildId;
|
|
805
|
-
}
|
|
806
|
-
}
|
|
807
|
-
if (pending && pending.reloadId !== null && pending.reloadId === message.reloadId)
|
|
808
|
-
this.#resolveReload({ pagesBundlePath: pending.pagesBundlePath, buildId: pending.buildId });
|
|
321
|
+
case "reloaded":
|
|
322
|
+
this.#reloads.onReloaded(message, proc);
|
|
809
323
|
return;
|
|
810
|
-
}
|
|
811
324
|
case "chunk":
|
|
812
|
-
this.#pending.get(message.requestId)?.onChunk(message.data);
|
|
325
|
+
this.#requests.pending.get(message.requestId)?.onChunk(message.data);
|
|
813
326
|
return;
|
|
814
327
|
case "meta":
|
|
815
|
-
this.#pending
|
|
328
|
+
this.#requests.pending
|
|
816
329
|
.get(message.requestId)
|
|
817
330
|
?.onMeta?.({ theme: message.theme, status: message.status, trace: message.trace });
|
|
818
331
|
return;
|
|
819
332
|
case "theme":
|
|
820
|
-
this.#pending.get(message.requestId)?.onTheme?.(message.theme);
|
|
333
|
+
this.#requests.pending.get(message.requestId)?.onTheme?.(message.theme);
|
|
821
334
|
return;
|
|
822
335
|
case "end":
|
|
823
|
-
this.#resolvePending(message.requestId, (p) => p.onEnd());
|
|
336
|
+
this.#requests.resolvePending(message.requestId, (p) => p.onEnd());
|
|
824
337
|
return;
|
|
825
338
|
case "redirect":
|
|
826
|
-
this.#resolvePending(message.requestId, (p) =>
|
|
339
|
+
this.#requests.resolvePending(message.requestId, (p) =>
|
|
827
340
|
p.onRedirect?.(message.location, message.method ?? "replace", message.status ?? 307),
|
|
828
341
|
);
|
|
829
342
|
return;
|
|
830
343
|
case "late-redirect":
|
|
831
|
-
this.#pending
|
|
344
|
+
this.#requests.pending
|
|
832
345
|
.get(message.requestId)
|
|
833
346
|
?.onLateRedirect?.(message.location, message.method ?? "replace", message.status ?? 307);
|
|
834
347
|
return;
|
|
835
348
|
case "late-not-found":
|
|
836
|
-
this.#pending.get(message.requestId)?.onLateNotFound?.();
|
|
349
|
+
this.#requests.pending.get(message.requestId)?.onLateNotFound?.();
|
|
837
350
|
return;
|
|
838
351
|
case "not-found":
|
|
839
|
-
this.#resolvePending(message.requestId, (p) => p.onNotFound?.());
|
|
352
|
+
this.#requests.resolvePending(message.requestId, (p) => p.onNotFound?.());
|
|
840
353
|
return;
|
|
841
354
|
case "metrics":
|
|
842
|
-
this.#lastWorkerMetrics = message.metrics;
|
|
843
|
-
this.#maybeRecycleFromMetrics(message.metrics);
|
|
355
|
+
this.#worker.lastWorkerMetrics = message.metrics;
|
|
356
|
+
this.#recycler.maybeRecycleFromMetrics(message.metrics);
|
|
844
357
|
return;
|
|
845
358
|
case "log.records":
|
|
846
359
|
this.onLogRecords?.(message.records, message.dropped ?? 0);
|
|
847
360
|
return;
|
|
848
361
|
case "page-prompts.result":
|
|
849
362
|
case "page-prompt.result":
|
|
363
|
+
case "page-prompt.draw.result":
|
|
850
364
|
case "page-prompts.visible.result":
|
|
851
365
|
case "web-manifest.result":
|
|
852
|
-
this.#settleCall(message.requestId, (call) => call.resolve(message.result));
|
|
366
|
+
this.#requests.settleCall(message.requestId, (call) => call.resolve(message.result));
|
|
853
367
|
return;
|
|
854
368
|
case "error":
|
|
855
369
|
if (message.requestId === "__init__") {
|
|
856
|
-
if (!this.#readyResolved) {
|
|
857
|
-
this.#rejectReload(String(message.message));
|
|
858
|
-
this.#rejectReady(new
|
|
370
|
+
if (!this.#worker.readyResolved) {
|
|
371
|
+
this.#reloads.rejectReload(String(message.message));
|
|
372
|
+
this.#worker.rejectReady(new AkanError("rscWorkerFailed", String(message.message)));
|
|
859
373
|
} else {
|
|
860
374
|
this.#bootFailed(proc, String(message.message));
|
|
861
375
|
proc.kill();
|
|
@@ -863,230 +377,88 @@ export class RscWorker {
|
|
|
863
377
|
return;
|
|
864
378
|
}
|
|
865
379
|
if (message.requestId === "__reload__") {
|
|
866
|
-
|
|
867
|
-
if (proc === this.#proc && message.running) this.#adopted = message.running;
|
|
868
|
-
const pending = this.#pendingReload;
|
|
869
|
-
if (pending && pending.reloadId !== null && pending.reloadId === message.reloadId)
|
|
870
|
-
this.#rejectReload(String(message.message));
|
|
380
|
+
this.#reloads.onReloadFailed(message, proc);
|
|
871
381
|
return;
|
|
872
382
|
}
|
|
873
|
-
if (
|
|
874
|
-
|
|
383
|
+
if (
|
|
384
|
+
this.#requests.settleCall(message.requestId, (call) =>
|
|
385
|
+
call.reject(new AkanError("rscWorkerFailed", String(message.message))),
|
|
386
|
+
)
|
|
387
|
+
)
|
|
388
|
+
return;
|
|
389
|
+
this.#requests.resolvePending(message.requestId, (p) => p.onError(String(message.message)));
|
|
875
390
|
return;
|
|
876
391
|
}
|
|
877
392
|
}
|
|
878
393
|
|
|
879
|
-
#resolvePending(requestId: string, fn: (p: RscPending) => void): void {
|
|
880
|
-
const p = this.#pending.get(requestId);
|
|
881
|
-
if (!p) return;
|
|
882
|
-
fn(p);
|
|
883
|
-
this.#pending.delete(requestId);
|
|
884
|
-
}
|
|
885
|
-
|
|
886
394
|
/** The page request's headers as the worker sees them, `x-real-ip` being only the address the host resolved. */
|
|
887
395
|
static workerHeaders(req: Request, clientIp: string | null | undefined): Record<string, string> {
|
|
888
|
-
|
|
889
|
-
req.headers.forEach((value, key) => {
|
|
890
|
-
headers[key] = value;
|
|
891
|
-
});
|
|
892
|
-
|
|
893
|
-
delete headers["x-real-ip"];
|
|
894
|
-
if (clientIp) headers["x-real-ip"] = clientIp;
|
|
895
|
-
return headers;
|
|
896
|
-
}
|
|
897
|
-
|
|
898
|
-
#sendRenderOrQueue(requestId: string, req: Request, { clientManifest, clientIp, prefetch }: RscRenderOptions): void {
|
|
899
|
-
const send = () => {
|
|
900
|
-
if (!this.#pending.has(requestId)) return;
|
|
901
|
-
try {
|
|
902
|
-
const headers = RscWorker.workerHeaders(req, clientIp);
|
|
903
|
-
const { url, method } = req;
|
|
904
|
-
this.#proc.send({ type: "render", requestId, url, method, headers, clientManifest, prefetch });
|
|
905
|
-
} catch (err) {
|
|
906
|
-
this.#resolvePending(requestId, (p) =>
|
|
907
|
-
p.onError(`rsc worker send failed: ${err instanceof Error ? err.message : String(err)}`),
|
|
908
|
-
);
|
|
909
|
-
}
|
|
910
|
-
};
|
|
911
|
-
if (this.#status === "ready") send();
|
|
912
|
-
else if (this.#status === "stopped") {
|
|
913
|
-
this.#resolvePending(requestId, (p) => p.onError("rsc worker is stopped"));
|
|
914
|
-
} else {
|
|
915
|
-
this.#queuedSends.push({ requestId, send });
|
|
916
|
-
}
|
|
917
|
-
}
|
|
918
|
-
|
|
919
|
-
#cancelRender(requestId: string): void {
|
|
920
|
-
if (this.#status !== "ready") return;
|
|
921
|
-
try {
|
|
922
|
-
this.#proc.send({ type: "cancel", requestId });
|
|
923
|
-
} catch {
|
|
924
|
-
}
|
|
925
|
-
}
|
|
926
|
-
|
|
927
|
-
#flushQueuedSends(): void {
|
|
928
|
-
const queue = this.#queuedSends;
|
|
929
|
-
this.#queuedSends = [];
|
|
930
|
-
for (const { send } of queue) {
|
|
931
|
-
try {
|
|
932
|
-
send();
|
|
933
|
-
} catch (err) {
|
|
934
|
-
this.#logger.error(`[rsc] queued send failed: ${err instanceof Error ? err.message : String(err)}`);
|
|
935
|
-
}
|
|
936
|
-
}
|
|
937
|
-
}
|
|
938
|
-
|
|
939
|
-
#failRequests(err: Error, which: (requestId: string) => boolean = () => true): void {
|
|
940
|
-
for (const [requestId, pending] of this.#pending) {
|
|
941
|
-
if (!which(requestId)) continue;
|
|
942
|
-
this.#pending.delete(requestId);
|
|
943
|
-
pending.onError(err.message);
|
|
944
|
-
}
|
|
945
|
-
for (const [requestId, call] of this.#calls) {
|
|
946
|
-
if (!which(requestId)) continue;
|
|
947
|
-
this.#calls.delete(requestId);
|
|
948
|
-
call.reject(err);
|
|
949
|
-
}
|
|
950
|
-
this.#queuedSends = this.#queuedSends.filter((entry) => !which(entry.requestId));
|
|
396
|
+
return RscHostRequests.workerHeaders(req, clientIp);
|
|
951
397
|
}
|
|
952
398
|
|
|
953
399
|
#bootFailed(proc: RscProcess, message: string): boolean {
|
|
954
|
-
this.#booting = null;
|
|
400
|
+
this.#worker.booting = null;
|
|
955
401
|
this.#logger.error(`[rsc] worker init error on restart: ${message}`);
|
|
956
|
-
const covered =
|
|
957
|
-
|
|
958
|
-
|
|
959
|
-
|
|
960
|
-
|
|
961
|
-
this.#
|
|
962
|
-
this.#
|
|
963
|
-
this.#reloadsSinceSpawn = rolling.reloadsSinceSpawn;
|
|
402
|
+
const covered = this.#reloads.rejectIfCovered(message);
|
|
403
|
+
const rolling = proc === this.#worker.proc ? this.#recycler.rollingRecycle : null;
|
|
404
|
+
this.#recycler.rollingRecycle = null;
|
|
405
|
+
if (!rolling || !RscWorkerProcess.isAlive(rolling.oldProc)) return false;
|
|
406
|
+
this.#worker.proc = rolling.oldProc;
|
|
407
|
+
this.#worker.status = "ready";
|
|
408
|
+
this.#worker.reloadsSinceSpawn = rolling.reloadsSinceSpawn;
|
|
964
409
|
if (this.#droppedWhileBooting) {
|
|
965
410
|
this.#droppedWhileBooting = false;
|
|
966
|
-
this.updateCssAssets(this.#cssAssets);
|
|
411
|
+
this.updateCssAssets(this.#reloads.cssAssets);
|
|
967
412
|
this.#sendLogLevel();
|
|
968
413
|
this.invalidateRouteResultCache();
|
|
969
414
|
}
|
|
970
|
-
this.#flushQueuedSends();
|
|
971
|
-
if (!covered) this.#sendPendingReload();
|
|
415
|
+
this.#requests.flushQueuedSends();
|
|
416
|
+
if (!covered) this.#reloads.sendPendingReload();
|
|
972
417
|
return true;
|
|
973
418
|
}
|
|
974
419
|
|
|
975
|
-
static #isAlive(proc: RscProcess): boolean {
|
|
976
|
-
return proc.exitCode === null && proc.signalCode === null && !proc.killed;
|
|
977
|
-
}
|
|
978
|
-
|
|
979
|
-
#finishRollingRecycle(): void {
|
|
980
|
-
const recycle = this.#rollingRecycle;
|
|
981
|
-
if (!recycle) return;
|
|
982
|
-
this.#rollingRecycle = null;
|
|
983
|
-
this.#logger.info(
|
|
984
|
-
`[rsc] rolling recycle ready reason=${recycle.reason} oldPid=${recycle.oldProc.pid} newPid=${this.#proc.pid}`,
|
|
985
|
-
);
|
|
986
|
-
recycle.oldProc.kill();
|
|
987
|
-
this.#lastWorkerMetrics = {};
|
|
988
|
-
}
|
|
989
|
-
|
|
990
420
|
#handleExit(proc: RscProcess, code: number | null): void {
|
|
991
421
|
|
|
992
|
-
if (proc === this.#rollingRecycle?.oldProc) {
|
|
993
|
-
this.#rollingRecycle = null;
|
|
422
|
+
if (proc === this.#recycler.rollingRecycle?.oldProc) {
|
|
423
|
+
this.#recycler.rollingRecycle = null;
|
|
994
424
|
return;
|
|
995
425
|
}
|
|
996
426
|
|
|
997
|
-
if (proc !== this.#proc) return;
|
|
427
|
+
if (proc !== this.#worker.proc) return;
|
|
998
428
|
|
|
999
|
-
if (proc === this.#booting && !this.#killed && proc.signalCode === null) {
|
|
429
|
+
if (proc === this.#worker.booting && !this.#worker.killed && proc.signalCode === null) {
|
|
1000
430
|
const message = `rsc worker exited with code ${code} while loading the pages bundle`;
|
|
1001
|
-
if (this.#readyResolved && this.#bootFailed(proc, message)) return;
|
|
1002
|
-
if (!this.#readyResolved) {
|
|
1003
|
-
this.#booting = null;
|
|
1004
|
-
this.#status = "stopped";
|
|
1005
|
-
this.#failRequests(new
|
|
1006
|
-
this.#rejectReload(message);
|
|
1007
|
-
this.#rejectReady(new
|
|
431
|
+
if (this.#worker.readyResolved && this.#bootFailed(proc, message)) return;
|
|
432
|
+
if (!this.#worker.readyResolved) {
|
|
433
|
+
this.#worker.booting = null;
|
|
434
|
+
this.#worker.status = "stopped";
|
|
435
|
+
this.#requests.failRequests(new AkanError("rscWorkerFailed", message));
|
|
436
|
+
this.#reloads.rejectReload(message);
|
|
437
|
+
this.#worker.rejectReady(new AkanError("rscWorkerFailed", message));
|
|
1008
438
|
return;
|
|
1009
439
|
}
|
|
1010
440
|
}
|
|
1011
441
|
|
|
1012
|
-
const err = new
|
|
1013
|
-
|
|
1014
|
-
const queued = new Set(this.#queuedSends.map((entry) => entry.requestId));
|
|
1015
|
-
const keepsQueue = this.#restart.attempts < RscWorker.#queuedRespawns;
|
|
1016
|
-
this.#failRequests(err, (requestId) => !keepsQueue || !queued.has(requestId));
|
|
1017
|
-
|
|
1018
|
-
if (this.#killed) {
|
|
1019
|
-
this.#status = "stopped";
|
|
1020
|
-
this.#failRequests(err);
|
|
1021
|
-
this.#rejectReload(err.message);
|
|
1022
|
-
return;
|
|
1023
|
-
}
|
|
1024
|
-
if (this.#failBeforeReady && !this.#readyResolved) {
|
|
1025
|
-
this.#status = "stopped";
|
|
1026
|
-
this.#failRequests(err);
|
|
1027
|
-
this.#rejectReload(err.message);
|
|
1028
|
-
this.#rejectReady(err);
|
|
1029
|
-
return;
|
|
1030
|
-
}
|
|
1031
|
-
|
|
1032
|
-
this.#scheduleRestart();
|
|
1033
|
-
}
|
|
1034
|
-
|
|
1035
|
-
#scheduleRestart(): void {
|
|
1036
|
-
this.#status = "restarting";
|
|
1037
|
-
const delay = this.#restart.schedule(() => {
|
|
1038
|
-
if (this.#killed) return;
|
|
1039
|
-
try {
|
|
1040
|
-
this.#proc = this.#spawn();
|
|
1041
|
-
} catch (err) {
|
|
1042
|
-
this.#logger.error(`[rsc] failed to spawn worker: ${err instanceof Error ? err.message : String(err)}`);
|
|
1043
|
-
this.#scheduleRestart();
|
|
1044
|
-
}
|
|
1045
|
-
});
|
|
1046
|
-
this.#restartCount += 1;
|
|
1047
|
-
this.#logger.verbose(`[rsc] worker crashed, restarting in ${delay}ms (attempt ${this.#restart.attempts})`);
|
|
1048
|
-
}
|
|
442
|
+
const err = new AkanError("rscWorkerStopped", `rsc worker exited with code ${code}`);
|
|
1049
443
|
|
|
1050
|
-
|
|
1051
|
-
const
|
|
1052
|
-
|
|
1053
|
-
|
|
1054
|
-
if (this.#lastRecycleAtMono === null) return true;
|
|
1055
|
-
const minIntervalMs = MemoryLimit.parsePositiveIntEnv("AKAN_RSC_WORKER_MIN_RECYCLE_INTERVAL_MS") ?? 1_000;
|
|
1056
|
-
return performance.now() - this.#lastRecycleAtMono >= minIntervalMs;
|
|
1057
|
-
}
|
|
444
|
+
const queued = new Set(this.#requests.queuedSends.map((entry) => entry.requestId));
|
|
445
|
+
const keepsQueue = this.#worker.restart.attempts < RscWorker.#queuedRespawns;
|
|
446
|
+
this.#requests.failRequests(err, (requestId) => !keepsQueue || !queued.has(requestId));
|
|
1058
447
|
|
|
1059
|
-
|
|
1060
|
-
|
|
1061
|
-
|
|
1062
|
-
|
|
1063
|
-
this.restartWhenIdle(`rss>${Math.round(maxRssBytes / 1024 / 1024)}MiB`);
|
|
448
|
+
if (this.#worker.killed) {
|
|
449
|
+
this.#worker.status = "stopped";
|
|
450
|
+
this.#requests.failRequests(err);
|
|
451
|
+
this.#reloads.rejectReload(err.message, err.code);
|
|
1064
452
|
return;
|
|
1065
453
|
}
|
|
1066
|
-
|
|
1067
|
-
|
|
1068
|
-
this.
|
|
454
|
+
if (this.#failBeforeReady && !this.#worker.readyResolved) {
|
|
455
|
+
this.#worker.status = "stopped";
|
|
456
|
+
this.#requests.failRequests(err);
|
|
457
|
+
this.#reloads.rejectReload(err.message, err.code);
|
|
458
|
+
this.#worker.rejectReady(err);
|
|
1069
459
|
return;
|
|
1070
460
|
}
|
|
1071
|
-
const maxRouteModules = MemoryLimit.parsePositiveIntEnv("AKAN_RSC_WORKER_MAX_ROUTE_MODULES");
|
|
1072
|
-
if (maxRouteModules && (metrics.rscLoadedRouteModuleCount ?? 0) >= maxRouteModules) {
|
|
1073
|
-
this.restartWhenIdle(`routeModules>${maxRouteModules}`);
|
|
1074
|
-
}
|
|
1075
|
-
}
|
|
1076
|
-
|
|
1077
|
-
static #getRscMaxReloads(): number | null {
|
|
1078
|
-
if (process.env.AKAN_RSC_WORKER_MAX_RELOADS !== undefined)
|
|
1079
|
-
return MemoryLimit.parsePositiveIntEnv("AKAN_RSC_WORKER_MAX_RELOADS");
|
|
1080
|
-
return process.env.NODE_ENV === "production" ? null : RscWorker.#devMaxReloads;
|
|
1081
|
-
}
|
|
1082
461
|
|
|
1083
|
-
|
|
1084
|
-
return MemoryLimit.resolveMaxRssBytes({
|
|
1085
|
-
megabytesEnv: "AKAN_RSC_WORKER_MAX_RSS_MB",
|
|
1086
|
-
bytesEnv: "AKAN_RSC_WORKER_MAX_RSS",
|
|
1087
|
-
limitFraction: 0.55,
|
|
1088
|
-
|
|
1089
|
-
fallbackBytes: process.env.NODE_ENV === "production" ? null : RscWorker.#devMaxRssBytes,
|
|
1090
|
-
});
|
|
462
|
+
this.#worker.scheduleRestart();
|
|
1091
463
|
}
|
|
1092
464
|
}
|