@floegence/flowersec-core 5.11.0 → 6.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (713) hide show
  1. package/README.md +170 -58
  2. package/THIRD_PARTY_NOTICES.md +3070 -5
  3. package/dist/application/rpcJSON.d.ts +15 -0
  4. package/dist/{rpc/validate.js → application/rpcJSON.js} +1 -1
  5. package/dist/browser/clientV4.d.ts +61 -0
  6. package/dist/browser/clientV4.js +179 -0
  7. package/dist/browser/index.d.ts +15 -8
  8. package/dist/browser/index.js +5 -3
  9. package/dist/browser/indexedDBPoolV4.d.ts +101 -0
  10. package/dist/browser/indexedDBPoolV4.js +634 -0
  11. package/dist/browser/liveHTTPSV4.d.ts +35 -0
  12. package/dist/browser/liveHTTPSV4.js +162 -0
  13. package/dist/browser/localBrowserBridge.d.ts +20 -0
  14. package/dist/browser/localBrowserBridge.js +82 -0
  15. package/dist/browser/poolServerAllow.d.ts +21 -0
  16. package/dist/browser/poolServerAllow.js +151 -0
  17. package/dist/browser/webTransportPositionsV4.d.ts +16 -0
  18. package/dist/browser/webTransportPositionsV4.js +75 -0
  19. package/dist/browser/webTransportStreamV4.d.ts +36 -0
  20. package/dist/browser/webTransportStreamV4.js +370 -0
  21. package/dist/browser/webTransportV4.d.ts +126 -0
  22. package/dist/browser/webTransportV4.js +627 -0
  23. package/dist/browser/wssV4.d.ts +69 -0
  24. package/dist/browser/wssV4.js +339 -0
  25. package/dist/cli.d.ts +24 -1
  26. package/dist/cli.js +91 -119
  27. package/dist/defaults.d.ts +1 -23
  28. package/dist/defaults.js +1 -23
  29. package/dist/facade.d.ts +104 -21
  30. package/dist/facade.js +45 -8
  31. package/dist/generated/brotliWorker.d.ts +1 -0
  32. package/dist/generated/brotliWorker.js +3 -0
  33. package/dist/generated/idna151.d.ts +2 -0
  34. package/dist/generated/idna151.js +38 -0
  35. package/dist/generated/normalization151.d.ts +5 -0
  36. package/dist/generated/normalization151.js +878 -0
  37. package/dist/generated/sqliteWorkers.d.ts +2 -0
  38. package/dist/generated/sqliteWorkers.js +3 -0
  39. package/dist/generated/transportV4APIResults.d.ts +21 -1
  40. package/dist/generated/transportV4APIResults.js +42 -1
  41. package/dist/generated/transportV4Registry.d.ts +276 -2
  42. package/dist/generated/transportV4Registry.js +10 -4
  43. package/dist/interop/browserRunner.d.ts +72 -0
  44. package/dist/interop/browserRunner.js +354 -0
  45. package/dist/interop/currentContracts.d.ts +6 -0
  46. package/dist/interop/currentContracts.js +14 -0
  47. package/dist/interop/currentNativeTunnel.d.ts +30 -0
  48. package/dist/interop/currentNativeTunnel.js +149 -0
  49. package/dist/interop/currentParity.d.ts +68 -0
  50. package/dist/interop/currentParity.js +392 -0
  51. package/dist/interop/currentPeer.d.ts +241 -0
  52. package/dist/interop/currentPeer.js +630 -0
  53. package/dist/interop/currentRegisteredTunnelPeer.d.ts +37 -0
  54. package/dist/interop/currentRegisteredTunnelPeer.js +93 -0
  55. package/dist/interop/currentRelayPeer.d.ts +118 -0
  56. package/dist/interop/currentRelayPeer.js +251 -0
  57. package/dist/interop/currentServer.d.ts +44 -0
  58. package/dist/interop/currentServer.js +293 -0
  59. package/dist/interop/currentTLSPin.d.ts +9 -0
  60. package/dist/interop/currentTLSPin.js +44 -0
  61. package/dist/interop/currentTunnelMaterial.d.ts +34 -0
  62. package/dist/interop/currentTunnelMaterial.js +100 -0
  63. package/dist/interop/httpApplicationPeer.d.ts +15 -0
  64. package/dist/interop/httpApplicationPeer.js +167 -0
  65. package/dist/interop/invalidProofPeer.d.ts +8 -0
  66. package/dist/interop/invalidProofPeer.js +106 -0
  67. package/dist/interop/proxyServerPeer.js +8 -56
  68. package/dist/interop/serverParityPeer.d.ts +101 -1
  69. package/dist/interop/serverParityPeer.js +550 -578
  70. package/dist/node/acceptorCurrent.d.ts +55 -0
  71. package/dist/node/acceptorCurrent.js +286 -0
  72. package/dist/node/admissionRecord.d.ts +23 -0
  73. package/dist/node/admissionRecord.js +178 -0
  74. package/dist/node/clientV4.d.ts +59 -0
  75. package/dist/node/clientV4.js +197 -0
  76. package/dist/node/endpointDialer.d.ts +70 -0
  77. package/dist/node/endpointDialer.js +231 -0
  78. package/dist/node/endpointListener.d.ts +48 -0
  79. package/dist/node/endpointListener.js +403 -0
  80. package/dist/node/endpointTransport.d.ts +7 -0
  81. package/dist/node/endpointTransport.js +18 -0
  82. package/dist/node/grantIssuerCurrent.d.ts +64 -0
  83. package/dist/node/grantIssuerCurrent.js +355 -0
  84. package/dist/node/index.d.ts +52 -18
  85. package/dist/node/index.js +24 -7
  86. package/dist/node/liveHTTPSV4.d.ts +26 -0
  87. package/dist/node/liveHTTPSV4.js +283 -0
  88. package/dist/node/liveTunnelAuthorityCurrent.d.ts +153 -0
  89. package/dist/node/liveTunnelAuthorityCurrent.js +590 -0
  90. package/dist/node/nativeDuplexBridgeV4.d.ts +41 -0
  91. package/dist/node/nativeDuplexBridgeV4.js +494 -0
  92. package/dist/node/nativeTransportCurrent.d.ts +196 -0
  93. package/dist/node/nativeTransportCurrent.js +37 -0
  94. package/dist/node/poolServerAllow.d.ts +33 -0
  95. package/dist/node/poolServerAllow.js +277 -0
  96. package/dist/node/proxyCredentials.d.ts +67 -0
  97. package/dist/node/proxyCredentials.js +364 -0
  98. package/dist/node/proxyNetwork.d.ts +18 -0
  99. package/dist/node/proxyNetwork.js +213 -0
  100. package/dist/node/proxyServer.d.ts +30 -4
  101. package/dist/node/proxyServer.js +599 -154
  102. package/dist/node/rawQUICCurrent.d.ts +139 -0
  103. package/dist/node/rawQUICCurrent.js +1177 -0
  104. package/dist/node/registeredLiveTunnelControl.d.ts +74 -0
  105. package/dist/node/registeredLiveTunnelControl.js +865 -0
  106. package/dist/node/registeredPoolTunnelAuthority.d.ts +41 -0
  107. package/dist/node/registeredPoolTunnelAuthority.js +297 -0
  108. package/dist/node/registeredTunnelControl.d.ts +189 -0
  109. package/dist/node/registeredTunnelControl.js +1007 -0
  110. package/dist/node/registeredWinnerContinuation.d.ts +26 -0
  111. package/dist/node/registeredWinnerContinuation.js +83 -0
  112. package/dist/node/relayNativePair.d.ts +62 -0
  113. package/dist/node/relayNativePair.js +737 -0
  114. package/dist/node/relayRecord.d.ts +57 -0
  115. package/dist/node/relayRecord.js +275 -0
  116. package/dist/node/serveRawQUIC.d.ts +58 -0
  117. package/dist/node/serveRawQUIC.js +331 -0
  118. package/dist/node/serveWSS.d.ts +58 -0
  119. package/dist/node/serveWSS.js +309 -0
  120. package/dist/node/sqliteAdmission.d.ts +100 -0
  121. package/dist/node/sqliteAdmission.js +1185 -0
  122. package/dist/node/sqliteExecutionContentV4.d.ts +63 -0
  123. package/dist/node/sqliteExecutionContentV4.js +251 -0
  124. package/dist/node/sqliteExecutionV4.d.ts +114 -0
  125. package/dist/node/sqliteExecutionV4.js +1052 -0
  126. package/dist/node/sqliteFormat.d.ts +20 -0
  127. package/dist/node/sqliteFormat.js +72 -0
  128. package/dist/node/sqliteInspectionV4.d.ts +7 -0
  129. package/dist/node/sqliteInspectionV4.js +47 -0
  130. package/dist/node/sqliteNativeV4.d.ts +3 -0
  131. package/dist/node/sqliteNativeV4.js +16 -0
  132. package/dist/node/sqlitePoolV4.d.ts +41 -0
  133. package/dist/node/sqlitePoolV4.js +282 -0
  134. package/dist/node/sqlitePoolWorkerProgramV4.js +411 -0
  135. package/dist/node/sqlitePoolWorkerV4.d.ts +67 -0
  136. package/dist/node/sqlitePoolWorkerV4.js +206 -0
  137. package/dist/node/sqliteV4.d.ts +82 -0
  138. package/dist/node/sqliteV4.js +166 -0
  139. package/dist/node/sqliteWorkerProgramV4.js +175 -0
  140. package/dist/node/sqliteWorkerV4.d.ts +47 -0
  141. package/dist/node/sqliteWorkerV4.js +142 -0
  142. package/dist/node/streamDuplexV4.d.ts +33 -0
  143. package/dist/node/streamDuplexV4.js +359 -0
  144. package/dist/node/tunnelListenerPreparation.d.ts +37 -0
  145. package/dist/node/tunnelListenerPreparation.js +162 -0
  146. package/dist/node/tunnelRuntime.d.ts +124 -0
  147. package/dist/node/tunnelRuntime.js +1205 -0
  148. package/dist/node/webTransportCurrent.d.ts +24 -0
  149. package/dist/node/webTransportCurrent.js +12 -0
  150. package/dist/node/wssV4.d.ts +96 -0
  151. package/dist/node/wssV4.js +822 -0
  152. package/dist/proxy/admission.d.ts +21 -0
  153. package/dist/proxy/admission.js +126 -0
  154. package/dist/proxy/brotliBody.d.ts +10 -0
  155. package/dist/proxy/brotliBody.js +85 -0
  156. package/dist/proxy/credentialControl.d.ts +30 -0
  157. package/dist/proxy/credentialControl.js +86 -0
  158. package/dist/proxy/currentStream.d.ts +24 -0
  159. package/dist/proxy/currentStream.js +158 -0
  160. package/dist/proxy/fetch.d.ts +3 -2
  161. package/dist/proxy/fetch.js +96 -29
  162. package/dist/proxy/headers.d.ts +8 -0
  163. package/dist/proxy/headers.js +63 -0
  164. package/dist/proxy/index.d.ts +4 -0
  165. package/dist/proxy/index.js +2 -0
  166. package/dist/proxy/integration.d.ts +12 -8
  167. package/dist/proxy/integration.js +23 -16
  168. package/dist/proxy/policy.d.ts +5 -0
  169. package/dist/proxy/policy.js +60 -22
  170. package/dist/proxy/publicationOwner.d.ts +10 -0
  171. package/dist/proxy/publicationOwner.js +130 -0
  172. package/dist/proxy/requestAssociation.d.ts +11 -0
  173. package/dist/proxy/requestAssociation.js +8 -0
  174. package/dist/proxy/runtime.d.ts +3 -1
  175. package/dist/proxy/runtime.js +403 -170
  176. package/dist/proxy/scope.js +2 -2
  177. package/dist/proxy/serviceWorker.d.ts +3 -0
  178. package/dist/proxy/serviceWorker.js +634 -176
  179. package/dist/proxy/serviceWorkerRuntime.js +26 -1
  180. package/dist/proxy/stream.d.ts +16 -3
  181. package/dist/proxy/stream.js +6 -0
  182. package/dist/proxy/surface.d.ts +45 -0
  183. package/dist/proxy/surface.js +384 -0
  184. package/dist/proxy/types.d.ts +49 -6
  185. package/dist/proxy/windowBridge.d.ts +11 -3
  186. package/dist/proxy/windowBridge.js +68 -25
  187. package/dist/proxy/wire.d.ts +11 -0
  188. package/dist/proxy/wire.js +214 -0
  189. package/dist/proxy/wsPatch.js +13 -6
  190. package/dist/public/contract.d.ts +0 -71
  191. package/dist/public/contract.js +0 -8
  192. package/dist/public/streamKind.d.ts +3 -0
  193. package/dist/public/streamKind.js +19 -0
  194. package/dist/public/streamMetadata.d.ts +34 -2
  195. package/dist/public/streamMetadata.js +206 -13
  196. package/dist/v4/admissionOffer.d.ts +8 -0
  197. package/dist/v4/checkpoint.d.ts +23 -0
  198. package/dist/v4/checkpoint.js +16 -0
  199. package/dist/v4/clientServices.d.ts +35 -0
  200. package/dist/v4/clientServices.js +11 -0
  201. package/dist/v4/connectionDiagnostic.d.ts +42 -0
  202. package/dist/v4/connectionDiagnostic.js +45 -0
  203. package/dist/v4/connectionRequirements.d.ts +5 -0
  204. package/dist/v4/connectionRequirements.js +34 -0
  205. package/dist/v4/controller.d.ts +122 -0
  206. package/dist/v4/controller.js +1161 -0
  207. package/dist/v4/controllerNotifications.d.ts +81 -0
  208. package/dist/v4/controllerNotifications.js +610 -0
  209. package/dist/v4/diagnostics.d.ts +44 -0
  210. package/dist/v4/diagnostics.js +9 -0
  211. package/dist/v4/drain.d.ts +39 -0
  212. package/dist/v4/drain.js +80 -0
  213. package/dist/v4/duplexBridge.d.ts +53 -0
  214. package/dist/v4/duplexBridge.js +446 -0
  215. package/dist/v4/entrypoints.d.ts +20 -0
  216. package/dist/v4/entrypoints.js +50 -0
  217. package/dist/v4/eventSource.d.ts +34 -0
  218. package/dist/v4/eventSource.js +28 -0
  219. package/dist/v4/handlerPlan.d.ts +53 -0
  220. package/dist/v4/handlerPlan.js +129 -0
  221. package/dist/v4/liveness.d.ts +20 -0
  222. package/dist/v4/liveness.js +10 -0
  223. package/dist/v4/localReport.d.ts +17 -0
  224. package/dist/v4/localReport.js +10 -0
  225. package/dist/v4/messageDefinition.d.ts +66 -0
  226. package/dist/v4/messageDefinition.js +109 -0
  227. package/dist/v4/messageStream.d.ts +89 -0
  228. package/dist/v4/messageStream.js +1375 -0
  229. package/dist/v4/notificationOperation.d.ts +30 -0
  230. package/dist/v4/notificationOperation.js +31 -0
  231. package/dist/v4/notificationSubscription.d.ts +47 -0
  232. package/dist/v4/notificationSubscription.js +24 -0
  233. package/dist/v4/operationReference.d.ts +28 -0
  234. package/dist/v4/operationReference.js +61 -0
  235. package/dist/v4/operationReferenceCodec.d.ts +20 -0
  236. package/dist/v4/operationReferenceCodec.js +129 -0
  237. package/dist/v4/operationReferenceStore.d.ts +90 -0
  238. package/dist/v4/operationReferenceStore.js +272 -0
  239. package/dist/v4/operationResultRead.d.ts +26 -0
  240. package/dist/v4/operationResultRead.js +24 -0
  241. package/dist/v4/poolControl.d.ts +11 -0
  242. package/dist/v4/poolControl.js +50 -0
  243. package/dist/v4/poolSource.d.ts +93 -0
  244. package/dist/v4/poolSource.js +900 -0
  245. package/dist/v4/public.d.ts +232 -0
  246. package/dist/v4/public.js +632 -0
  247. package/dist/v4/responsePublication.d.ts +48 -0
  248. package/dist/v4/responsePublication.js +227 -0
  249. package/dist/v4/resume.d.ts +17 -0
  250. package/dist/v4/resume.js +15 -0
  251. package/dist/v4/runtime/applicationExecutor.d.ts +190 -0
  252. package/dist/v4/runtime/applicationExecutor.js +759 -0
  253. package/dist/v4/runtime/applicationHeader.d.ts +61 -0
  254. package/dist/v4/runtime/applicationHeader.js +222 -0
  255. package/dist/v4/runtime/canonicalText.d.ts +8 -0
  256. package/dist/v4/runtime/canonicalText.js +142 -0
  257. package/dist/v4/runtime/cbor.d.ts +92 -0
  258. package/dist/v4/runtime/cbor.js +728 -0
  259. package/dist/v4/runtime/cborWriter.d.ts +18 -0
  260. package/dist/v4/runtime/cborWriter.js +59 -0
  261. package/dist/v4/runtime/checkpointToken.d.ts +41 -0
  262. package/dist/v4/runtime/checkpointToken.js +61 -0
  263. package/dist/v4/runtime/clientAdmission.d.ts +40 -0
  264. package/dist/v4/runtime/clientAdmission.js +378 -0
  265. package/dist/v4/runtime/clientSessionSpec.d.ts +45 -0
  266. package/dist/v4/runtime/clientSessionSpec.js +91 -0
  267. package/dist/v4/runtime/clock.d.ts +80 -0
  268. package/dist/v4/runtime/clock.js +284 -0
  269. package/dist/v4/runtime/connectionFacts.d.ts +23 -0
  270. package/dist/v4/runtime/connectionFacts.js +106 -0
  271. package/dist/v4/runtime/contractAcceptance.d.ts +20 -0
  272. package/dist/v4/runtime/contractAcceptance.js +91 -0
  273. package/dist/v4/runtime/contractQuery.d.ts +57 -0
  274. package/dist/v4/runtime/contractQuery.js +265 -0
  275. package/dist/v4/runtime/contractQueryAccess.d.ts +25 -0
  276. package/dist/v4/runtime/contractQueryAccess.js +117 -0
  277. package/dist/v4/runtime/contractQueryAcquisition.d.ts +83 -0
  278. package/dist/v4/runtime/contractQueryAcquisition.js +572 -0
  279. package/dist/v4/runtime/contractQueryClient.d.ts +70 -0
  280. package/dist/v4/runtime/contractQueryClient.js +411 -0
  281. package/dist/v4/runtime/contractQueryCodecs.d.ts +20 -0
  282. package/dist/v4/runtime/contractQueryCodecs.js +71 -0
  283. package/dist/v4/runtime/contractQueryExchange.d.ts +36 -0
  284. package/dist/v4/runtime/contractQueryExchange.js +160 -0
  285. package/dist/v4/runtime/contractQueryService.d.ts +42 -0
  286. package/dist/v4/runtime/contractQueryService.js +572 -0
  287. package/dist/v4/runtime/contractQuerySession.d.ts +38 -0
  288. package/dist/v4/runtime/contractQuerySession.js +158 -0
  289. package/dist/v4/runtime/contractRoutes.d.ts +87 -0
  290. package/dist/v4/runtime/contractRoutes.js +396 -0
  291. package/dist/v4/runtime/contractSnapshotReader.d.ts +59 -0
  292. package/dist/v4/runtime/contractSnapshotReader.js +418 -0
  293. package/dist/v4/runtime/contractSnapshotWriter.d.ts +32 -0
  294. package/dist/v4/runtime/contractSnapshotWriter.js +208 -0
  295. package/dist/v4/runtime/controllerServiceSource.d.ts +23 -0
  296. package/dist/v4/runtime/controllerServiceSource.js +109 -0
  297. package/dist/v4/runtime/controllerUnaryRoute.d.ts +28 -0
  298. package/dist/v4/runtime/credentialNamespace.d.ts +143 -0
  299. package/dist/v4/runtime/credentialNamespace.js +1286 -0
  300. package/dist/v4/runtime/credentialSupport.d.ts +60 -0
  301. package/dist/v4/runtime/credentialSupport.js +294 -0
  302. package/dist/v4/runtime/credentialVerifier.d.ts +404 -0
  303. package/dist/v4/runtime/credentialVerifier.js +1443 -0
  304. package/dist/v4/runtime/cryptoUsage.d.ts +135 -0
  305. package/dist/v4/runtime/cryptoUsage.js +552 -0
  306. package/dist/v4/runtime/deadline.d.ts +31 -0
  307. package/dist/v4/runtime/deadline.js +188 -0
  308. package/dist/v4/runtime/diagnosticCounters.d.ts +15 -0
  309. package/dist/v4/runtime/diagnosticCounters.js +78 -0
  310. package/dist/v4/runtime/diagnosticObservation.d.ts +25 -0
  311. package/dist/v4/runtime/diagnosticObservation.js +67 -0
  312. package/dist/v4/runtime/diagnosticSink.d.ts +16 -0
  313. package/dist/v4/runtime/diagnosticSink.js +320 -0
  314. package/dist/v4/runtime/ed25519.d.ts +1 -0
  315. package/dist/v4/runtime/ed25519.js +34 -0
  316. package/dist/v4/runtime/endpointHop.d.ts +12 -0
  317. package/dist/v4/runtime/endpointHop.js +26 -0
  318. package/dist/v4/runtime/envelope.d.ts +66 -0
  319. package/dist/v4/runtime/envelope.js +275 -0
  320. package/dist/v4/runtime/environment.d.ts +277 -0
  321. package/dist/v4/runtime/environment.js +1911 -0
  322. package/dist/v4/runtime/executionManagementChannel.d.ts +27 -0
  323. package/dist/v4/runtime/executionManagementChannel.js +733 -0
  324. package/dist/v4/runtime/executionManagementCodec.d.ts +84 -0
  325. package/dist/v4/runtime/executionManagementCodec.js +127 -0
  326. package/dist/v4/runtime/executionStorage.d.ts +71 -0
  327. package/dist/v4/runtime/executionStorage.js +10 -0
  328. package/dist/v4/runtime/fixedDiagnosticExecutor.d.ts +34 -0
  329. package/dist/v4/runtime/fixedDiagnosticExecutor.js +149 -0
  330. package/dist/v4/runtime/fixedManagementExecutor.d.ts +21 -0
  331. package/dist/v4/runtime/fixedManagementExecutor.js +153 -0
  332. package/dist/v4/runtime/fixedQueryExecutor.d.ts +46 -0
  333. package/dist/v4/runtime/fixedQueryExecutor.js +311 -0
  334. package/dist/v4/runtime/grantDigest.d.ts +9 -0
  335. package/dist/v4/runtime/grantDigest.js +31 -0
  336. package/dist/v4/runtime/hopAuthentication.d.ts +51 -0
  337. package/dist/v4/runtime/hopAuthentication.js +392 -0
  338. package/dist/v4/runtime/host.d.ts +15 -0
  339. package/dist/v4/runtime/host.js +332 -0
  340. package/dist/v4/runtime/initializerWorkload.d.ts +38 -0
  341. package/dist/v4/runtime/initializerWorkload.js +164 -0
  342. package/dist/v4/runtime/lifecycle.d.ts +5 -0
  343. package/dist/v4/runtime/lifecycle.js +17 -0
  344. package/dist/v4/runtime/liveAuthorization.d.ts +42 -0
  345. package/dist/v4/runtime/liveAuthorization.js +133 -0
  346. package/dist/v4/runtime/liveAuthorizationWire.d.ts +8 -0
  347. package/dist/v4/runtime/liveAuthorizationWire.js +31 -0
  348. package/dist/v4/runtime/liveCarrierPreparation.d.ts +9 -0
  349. package/dist/v4/runtime/liveCarrierPreparation.js +27 -0
  350. package/dist/v4/runtime/liveness.d.ts +37 -0
  351. package/dist/v4/runtime/liveness.js +319 -0
  352. package/dist/v4/runtime/maintenancePositions.d.ts +29 -0
  353. package/dist/v4/runtime/maintenancePositions.js +152 -0
  354. package/dist/v4/runtime/messageCodec.d.ts +20 -0
  355. package/dist/v4/runtime/messageCodec.js +192 -0
  356. package/dist/v4/runtime/namespaceContinuity.d.ts +20 -0
  357. package/dist/v4/runtime/namespaceContinuity.js +176 -0
  358. package/dist/v4/runtime/nativeCandidates.d.ts +18 -0
  359. package/dist/v4/runtime/nativeCandidates.js +109 -0
  360. package/dist/v4/runtime/nativeFailure.d.ts +10 -0
  361. package/dist/v4/runtime/nativeFailure.js +32 -0
  362. package/dist/v4/runtime/nativeFrame.d.ts +47 -0
  363. package/dist/v4/runtime/nativeFrame.js +318 -0
  364. package/dist/v4/runtime/nativeIngress.d.ts +30 -0
  365. package/dist/v4/runtime/nativeIngress.js +177 -0
  366. package/dist/v4/runtime/nativePositions.d.ts +24 -0
  367. package/dist/v4/runtime/nativePositions.js +131 -0
  368. package/dist/v4/runtime/nativeProviderPositions.d.ts +16 -0
  369. package/dist/v4/runtime/nativeProviderPositions.js +75 -0
  370. package/dist/v4/runtime/nativeSend.d.ts +15 -0
  371. package/dist/v4/runtime/nativeSend.js +57 -0
  372. package/dist/v4/runtime/noiseHandshake.d.ts +70 -0
  373. package/dist/v4/runtime/noiseHandshake.js +373 -0
  374. package/dist/v4/runtime/noiseWasmBinary.d.ts +2 -0
  375. package/dist/v4/runtime/noiseWasmBinary.js +3 -0
  376. package/dist/v4/runtime/notifyChannel.d.ts +37 -0
  377. package/dist/v4/runtime/notifyChannel.js +277 -0
  378. package/dist/v4/runtime/notifyDispatch.d.ts +108 -0
  379. package/dist/v4/runtime/notifyDispatch.js +717 -0
  380. package/dist/v4/runtime/notifyExecution.d.ts +18 -0
  381. package/dist/v4/runtime/notifyExecution.js +59 -0
  382. package/dist/v4/runtime/notifyPreparation.d.ts +70 -0
  383. package/dist/v4/runtime/notifyPreparation.js +465 -0
  384. package/dist/v4/runtime/openAdmission.d.ts +125 -0
  385. package/dist/v4/runtime/openAdmission.js +810 -0
  386. package/dist/v4/runtime/peerOpenPreparation.d.ts +18 -0
  387. package/dist/v4/runtime/peerOpenPreparation.js +61 -0
  388. package/dist/v4/runtime/poolJournal.d.ts +18 -0
  389. package/dist/v4/runtime/poolJournal.js +14 -0
  390. package/dist/v4/runtime/poolJournalStorage.d.ts +4 -0
  391. package/dist/v4/runtime/poolJournalStorage.js +172 -0
  392. package/dist/v4/runtime/poolRecord.d.ts +9 -0
  393. package/dist/v4/runtime/poolRecord.js +23 -0
  394. package/dist/v4/runtime/poolRecordStorage.d.ts +10 -0
  395. package/dist/v4/runtime/poolRecordStorage.js +216 -0
  396. package/dist/v4/runtime/poolServerAllow.d.ts +49 -0
  397. package/dist/v4/runtime/poolServerAllow.js +38 -0
  398. package/dist/v4/runtime/poolSpend.d.ts +20 -0
  399. package/dist/v4/runtime/poolSpend.js +4 -0
  400. package/dist/v4/runtime/queryRenewalPosition.d.ts +59 -0
  401. package/dist/v4/runtime/queryRenewalPosition.js +220 -0
  402. package/dist/v4/runtime/random.d.ts +15 -0
  403. package/dist/v4/runtime/random.js +71 -0
  404. package/dist/v4/runtime/rawStreamPreparation.d.ts +40 -0
  405. package/dist/v4/runtime/rawStreamPreparation.js +125 -0
  406. package/dist/v4/runtime/receiveDirection.d.ts +135 -0
  407. package/dist/v4/runtime/receiveDirection.js +957 -0
  408. package/dist/v4/runtime/receiveWorkspace.d.ts +19 -0
  409. package/dist/v4/runtime/receiveWorkspace.js +82 -0
  410. package/dist/v4/runtime/record.d.ts +7 -0
  411. package/dist/v4/runtime/record.js +173 -0
  412. package/dist/v4/runtime/recordCrypto.d.ts +121 -0
  413. package/dist/v4/runtime/recordCrypto.js +633 -0
  414. package/dist/v4/runtime/recordWorkspace.d.ts +17 -0
  415. package/dist/v4/runtime/recordWorkspace.js +69 -0
  416. package/dist/v4/runtime/registeredCarrierPreparation.d.ts +17 -0
  417. package/dist/v4/runtime/registeredCarrierPreparation.js +191 -0
  418. package/dist/v4/runtime/rekey.d.ts +40 -0
  419. package/dist/v4/runtime/rekey.js +362 -0
  420. package/dist/v4/runtime/relayCredentials.d.ts +126 -0
  421. package/dist/v4/runtime/relayCredentials.js +349 -0
  422. package/dist/v4/runtime/replayWindow.d.ts +6 -0
  423. package/dist/v4/runtime/replayWindow.js +59 -0
  424. package/dist/v4/runtime/resources.d.ts +200 -0
  425. package/dist/v4/runtime/resources.js +1236 -0
  426. package/dist/v4/runtime/resumeCodec.d.ts +44 -0
  427. package/dist/v4/runtime/resumeCodec.js +192 -0
  428. package/dist/v4/runtime/resumeStream.d.ts +23 -0
  429. package/dist/v4/runtime/resumeStream.js +23 -0
  430. package/dist/v4/runtime/rpcApplication.d.ts +234 -0
  431. package/dist/v4/runtime/rpcApplication.js +2170 -0
  432. package/dist/v4/runtime/rpcApplicationServices.d.ts +18 -0
  433. package/dist/v4/runtime/rpcApplicationServices.js +62 -0
  434. package/dist/v4/runtime/rpcCallCapacity.d.ts +62 -0
  435. package/dist/v4/runtime/rpcCallCapacity.js +289 -0
  436. package/dist/v4/runtime/rpcChannel.d.ts +58 -0
  437. package/dist/v4/runtime/rpcChannel.js +334 -0
  438. package/dist/v4/runtime/rpcCompletion.d.ts +42 -0
  439. package/dist/v4/runtime/rpcCompletion.js +152 -0
  440. package/dist/v4/runtime/rpcEventSource.d.ts +27 -0
  441. package/dist/v4/runtime/rpcEventSource.js +201 -0
  442. package/dist/v4/runtime/rpcFragment.d.ts +42 -0
  443. package/dist/v4/runtime/rpcFragment.js +227 -0
  444. package/dist/v4/runtime/rpcInput.d.ts +41 -0
  445. package/dist/v4/runtime/rpcInput.js +137 -0
  446. package/dist/v4/runtime/rpcNetwork.d.ts +143 -0
  447. package/dist/v4/runtime/rpcNetwork.js +652 -0
  448. package/dist/v4/runtime/rpcOutputInterest.d.ts +20 -0
  449. package/dist/v4/runtime/rpcOutputInterest.js +106 -0
  450. package/dist/v4/runtime/rpcPayload.d.ts +24 -0
  451. package/dist/v4/runtime/rpcPayload.js +82 -0
  452. package/dist/v4/runtime/rpcPreacceptedStreams.d.ts +46 -0
  453. package/dist/v4/runtime/rpcPreacceptedStreams.js +406 -0
  454. package/dist/v4/runtime/rpcPublisher.d.ts +49 -0
  455. package/dist/v4/runtime/rpcPublisher.js +406 -0
  456. package/dist/v4/runtime/rpcReceiver.d.ts +40 -0
  457. package/dist/v4/runtime/rpcReceiver.js +255 -0
  458. package/dist/v4/runtime/rpcResumeDispatch.d.ts +46 -0
  459. package/dist/v4/runtime/rpcResumeDispatch.js +266 -0
  460. package/dist/v4/runtime/rpcStream.d.ts +14 -0
  461. package/dist/v4/runtime/rpcStream.js +12 -0
  462. package/dist/v4/runtime/rpcStreamMessages.d.ts +53 -0
  463. package/dist/v4/runtime/rpcStreamMessages.js +428 -0
  464. package/dist/v4/runtime/rpcStreamPreparation.d.ts +21 -0
  465. package/dist/v4/runtime/rpcStreamPreparation.js +110 -0
  466. package/dist/v4/runtime/rpcStreamingDispatch.d.ts +26 -0
  467. package/dist/v4/runtime/rpcStreamingDispatch.js +607 -0
  468. package/dist/v4/runtime/rpcStreamingExchange.d.ts +91 -0
  469. package/dist/v4/runtime/rpcStreamingExchange.js +812 -0
  470. package/dist/v4/runtime/rpcUnaryCall.d.ts +16 -0
  471. package/dist/v4/runtime/rpcUnaryCall.js +152 -0
  472. package/dist/v4/runtime/rpcUnaryDispatch.d.ts +34 -0
  473. package/dist/v4/runtime/rpcUnaryDispatch.js +569 -0
  474. package/dist/v4/runtime/rpcUnaryExchange.d.ts +127 -0
  475. package/dist/v4/runtime/rpcUnaryExchange.js +1042 -0
  476. package/dist/v4/runtime/rpcUnaryPreparation.d.ts +102 -0
  477. package/dist/v4/runtime/rpcUnaryPreparation.js +736 -0
  478. package/dist/v4/runtime/rpcUnaryRegistration.d.ts +33 -0
  479. package/dist/v4/runtime/rpcUnaryRegistration.js +113 -0
  480. package/dist/v4/runtime/rpcUnaryResultRecipient.d.ts +21 -0
  481. package/dist/v4/runtime/rpcUnaryResultRecipient.js +57 -0
  482. package/dist/v4/runtime/schema.d.ts +26 -0
  483. package/dist/v4/runtime/schema.js +668 -0
  484. package/dist/v4/runtime/schemaRegistry.d.ts +128 -0
  485. package/dist/v4/runtime/schemaRegistry.js +74 -0
  486. package/dist/v4/runtime/sendBudget.d.ts +10 -0
  487. package/dist/v4/runtime/sendBudget.js +46 -0
  488. package/dist/v4/runtime/serveGroup.d.ts +73 -0
  489. package/dist/v4/runtime/serveGroup.js +589 -0
  490. package/dist/v4/runtime/serverAdmission.d.ts +33 -0
  491. package/dist/v4/runtime/serverAdmission.js +342 -0
  492. package/dist/v4/runtime/serverAdmissionAuthority.d.ts +23 -0
  493. package/dist/v4/runtime/serverAdmissionAuthority.js +5 -0
  494. package/dist/v4/runtime/serverSessionSpec.d.ts +9 -0
  495. package/dist/v4/runtime/serverSessionSpec.js +12 -0
  496. package/dist/v4/runtime/serviceBinding.d.ts +128 -0
  497. package/dist/v4/runtime/serviceBinding.js +1435 -0
  498. package/dist/v4/runtime/serviceBindingConfig.d.ts +72 -0
  499. package/dist/v4/runtime/serviceBindingConfig.js +85 -0
  500. package/dist/v4/runtime/serviceBindingPool.d.ts +58 -0
  501. package/dist/v4/runtime/serviceBindingPool.js +327 -0
  502. package/dist/v4/runtime/serviceContract.d.ts +111 -0
  503. package/dist/v4/runtime/serviceContract.js +588 -0
  504. package/dist/v4/runtime/serviceInputs.d.ts +30 -0
  505. package/dist/v4/runtime/serviceInputs.js +201 -0
  506. package/dist/v4/runtime/serviceOfferPlan.d.ts +81 -0
  507. package/dist/v4/runtime/serviceOfferPlan.js +335 -0
  508. package/dist/v4/runtime/serviceOfferScheduler.d.ts +12 -0
  509. package/dist/v4/runtime/serviceOfferScheduler.js +201 -0
  510. package/dist/v4/runtime/serviceOfferWork.d.ts +25 -0
  511. package/dist/v4/runtime/serviceOfferWork.js +1 -0
  512. package/dist/v4/runtime/session.d.ts +411 -0
  513. package/dist/v4/runtime/session.js +6369 -0
  514. package/dist/v4/runtime/sessionActivity.d.ts +19 -0
  515. package/dist/v4/runtime/sessionActivity.js +97 -0
  516. package/dist/v4/runtime/sessionAdmission.d.ts +93 -0
  517. package/dist/v4/runtime/sessionAdmission.js +498 -0
  518. package/dist/v4/runtime/sessionCleanup.d.ts +47 -0
  519. package/dist/v4/runtime/sessionCleanup.js +268 -0
  520. package/dist/v4/runtime/sessionKeyPreparation.d.ts +19 -0
  521. package/dist/v4/runtime/sessionKeyPreparation.js +50 -0
  522. package/dist/v4/runtime/signedMap.d.ts +68 -0
  523. package/dist/v4/runtime/signedMap.js +425 -0
  524. package/dist/v4/runtime/streamAdapter.d.ts +113 -0
  525. package/dist/v4/runtime/streamAdapter.js +62 -0
  526. package/dist/v4/runtime/streamData.d.ts +11 -0
  527. package/dist/v4/runtime/streamData.js +50 -0
  528. package/dist/v4/runtime/streamOpenPreparation.d.ts +23 -0
  529. package/dist/v4/runtime/streamOpenPreparation.js +77 -0
  530. package/dist/v4/runtime/streamRegistration.d.ts +87 -0
  531. package/dist/v4/runtime/streamRegistration.js +296 -0
  532. package/dist/v4/runtime/taskObservation.d.ts +1 -0
  533. package/dist/v4/runtime/taskObservation.js +48 -0
  534. package/dist/v4/runtime/timeArithmetic.d.ts +29 -0
  535. package/dist/v4/runtime/timeArithmetic.js +107 -0
  536. package/dist/v4/runtime/unreliable.d.ts +76 -0
  537. package/dist/v4/runtime/unreliable.js +473 -0
  538. package/dist/v4/runtime/volatileExecutions.d.ts +128 -0
  539. package/dist/v4/runtime/volatileExecutions.js +792 -0
  540. package/dist/v4/runtime/wireRegistry.d.ts +52 -0
  541. package/dist/v4/runtime/wireRegistry.js +29 -0
  542. package/dist/v4/runtime/writeRequest.d.ts +42 -0
  543. package/dist/v4/runtime/writeRequest.js +237 -0
  544. package/dist/v4/serve.d.ts +107 -0
  545. package/dist/v4/serve.js +80 -0
  546. package/dist/v4/serviceClient.d.ts +92 -0
  547. package/dist/v4/serviceClient.js +323 -0
  548. package/dist/v4/serviceDefinition.d.ts +138 -0
  549. package/dist/v4/serviceDefinition.js +150 -0
  550. package/dist/v4/serviceDependencies.d.ts +66 -0
  551. package/dist/v4/serviceDependencies.js +250 -0
  552. package/dist/v4/serviceFactory.d.ts +31 -0
  553. package/dist/v4/serviceFactory.js +157 -0
  554. package/dist/v4/serviceHandlers.d.ts +55 -0
  555. package/dist/v4/serviceHandlers.js +15 -0
  556. package/dist/v4/serviceMembers.d.ts +4 -0
  557. package/dist/v4/serviceMembers.js +12 -0
  558. package/dist/v4/staticServiceContracts.d.ts +43 -0
  559. package/dist/v4/staticServiceContracts.js +161 -0
  560. package/dist/v4/storageFormat.d.ts +16 -0
  561. package/dist/v4/storageFormat.js +5 -0
  562. package/dist/v4/streamContent.d.ts +30 -0
  563. package/dist/v4/streamContent.js +29 -0
  564. package/dist/v4/streamHandlers.d.ts +61 -0
  565. package/dist/v4/streamHandlers.js +16 -0
  566. package/dist/v4/streamingOperation.d.ts +64 -0
  567. package/dist/v4/streamingOperation.js +141 -0
  568. package/dist/v4/testSupport/cbor.d.ts +124 -0
  569. package/dist/v4/testSupport/cbor.js +230 -0
  570. package/dist/v4/testSupport/credentials.d.ts +55 -0
  571. package/dist/v4/testSupport/credentials.js +111 -0
  572. package/dist/v4/testSupport/unicode.d.ts +19 -0
  573. package/dist/v4/testSupport/unicode.js +112 -0
  574. package/dist/v4/unaryOperation.d.ts +59 -0
  575. package/dist/v4/unaryOperation.js +80 -0
  576. package/dist/v4/unreliable.d.ts +26 -0
  577. package/dist/v4/unreliable.js +10 -0
  578. package/dist/v4/webStreams.d.ts +30 -0
  579. package/dist/v4/webStreams.js +377 -0
  580. package/package.json +15 -12
  581. package/sbom/cyclonedx.json +756 -38
  582. package/sbom/spdx.json +555 -38
  583. package/dist/browser/connectSessionV3.d.ts +0 -31
  584. package/dist/browser/connectSessionV3.js +0 -241
  585. package/dist/browser/directWebSocketProfileV1.d.ts +0 -14
  586. package/dist/browser/directWebSocketProfileV1.js +0 -71
  587. package/dist/browser/httpDirectV1.d.ts +0 -30
  588. package/dist/browser/httpDirectV1.js +0 -52
  589. package/dist/browser/privateLoopbackV1.d.ts +0 -30
  590. package/dist/browser/privateLoopbackV1.js +0 -70
  591. package/dist/framing/index.d.ts +0 -1
  592. package/dist/framing/index.js +0 -1
  593. package/dist/framing/jsonframe.d.ts +0 -14
  594. package/dist/framing/jsonframe.js +0 -42
  595. package/dist/node/acceptor.d.ts +0 -36
  596. package/dist/node/acceptor.js +0 -155
  597. package/dist/node/acceptorV3.d.ts +0 -57
  598. package/dist/node/acceptorV3.js +0 -227
  599. package/dist/node/byteStreamDuplex.d.ts +0 -4
  600. package/dist/node/byteStreamDuplex.js +0 -69
  601. package/dist/node/connectSessionV3.d.ts +0 -24
  602. package/dist/node/connectSessionV3.js +0 -79
  603. package/dist/node/connectionPath.d.ts +0 -28
  604. package/dist/node/connectionPath.js +0 -169
  605. package/dist/node/nativeTransportAddon.d.ts +0 -104
  606. package/dist/node/nativeTransportAddon.js +0 -212
  607. package/dist/node/rawQuicAdapterV3.d.ts +0 -10
  608. package/dist/node/rawQuicAdapterV3.js +0 -76
  609. package/dist/node/rawQuicServerV3.d.ts +0 -24
  610. package/dist/node/rawQuicServerV3.js +0 -34
  611. package/dist/node/runtimeAuthorizationV3.d.ts +0 -29
  612. package/dist/node/runtimeAuthorizationV3.js +0 -126
  613. package/dist/node/tunnelRuntimeV3.d.ts +0 -57
  614. package/dist/node/tunnelRuntimeV3.js +0 -857
  615. package/dist/node/webSocketServerV3.d.ts +0 -27
  616. package/dist/node/webSocketServerV3.js +0 -219
  617. package/dist/node/wsFactory.d.ts +0 -8
  618. package/dist/node/wsFactory.js +0 -84
  619. package/dist/public/streamHandlers.d.ts +0 -20
  620. package/dist/public/streamHandlers.js +0 -127
  621. package/dist/rpc/callError.d.ts +0 -5
  622. package/dist/rpc/callError.js +0 -11
  623. package/dist/rpc/caller.d.ts +0 -8
  624. package/dist/rpc/client.d.ts +0 -23
  625. package/dist/rpc/client.js +0 -171
  626. package/dist/rpc/index.d.ts +0 -6
  627. package/dist/rpc/index.js +0 -6
  628. package/dist/rpc/rpcProxy.d.ts +0 -16
  629. package/dist/rpc/rpcProxy.js +0 -58
  630. package/dist/rpc/server.d.ts +0 -51
  631. package/dist/rpc/server.js +0 -283
  632. package/dist/rpc/typed.d.ts +0 -5
  633. package/dist/rpc/typed.js +0 -9
  634. package/dist/rpc/validate.d.ts +0 -4
  635. package/dist/rpc/wire.d.ts +0 -15
  636. package/dist/v3/admissionError.d.ts +0 -4
  637. package/dist/v3/admissionError.js +0 -8
  638. package/dist/v3/artifact.d.ts +0 -145
  639. package/dist/v3/artifact.js +0 -1176
  640. package/dist/v3/artifactLease.d.ts +0 -17
  641. package/dist/v3/artifactLease.js +0 -109
  642. package/dist/v3/browserRuntime.d.ts +0 -37
  643. package/dist/v3/browserRuntime.js +0 -197
  644. package/dist/v3/browserSessionRuntime.d.ts +0 -2
  645. package/dist/v3/browserSessionRuntime.js +0 -21
  646. package/dist/v3/capability.d.ts +0 -32
  647. package/dist/v3/capability.js +0 -345
  648. package/dist/v3/carrier.d.ts +0 -84
  649. package/dist/v3/carrier.js +0 -411
  650. package/dist/v3/connectionController.d.ts +0 -100
  651. package/dist/v3/connectionController.js +0 -861
  652. package/dist/v3/contract.d.ts +0 -65
  653. package/dist/v3/contract.js +0 -3
  654. package/dist/v3/controller.d.ts +0 -45
  655. package/dist/v3/controller.js +0 -316
  656. package/dist/v3/handshake.d.ts +0 -81
  657. package/dist/v3/handshake.js +0 -471
  658. package/dist/v3/jcs.d.ts +0 -6
  659. package/dist/v3/jcs.js +0 -69
  660. package/dist/v3/jsonPreflight.d.ts +0 -1
  661. package/dist/v3/jsonPreflight.js +0 -251
  662. package/dist/v3/nodeRuntime.d.ts +0 -43
  663. package/dist/v3/nodeRuntime.js +0 -373
  664. package/dist/v3/nodeSessionRuntime.d.ts +0 -2
  665. package/dist/v3/nodeSessionRuntime.js +0 -13
  666. package/dist/v3/protocol.d.ts +0 -99
  667. package/dist/v3/protocol.js +0 -674
  668. package/dist/v3/publicApi.d.ts +0 -12
  669. package/dist/v3/publicApi.js +0 -46
  670. package/dist/v3/publicSession.js +0 -281
  671. package/dist/v3/retryDisposition.d.ts +0 -16
  672. package/dist/v3/retryDisposition.js +0 -23
  673. package/dist/v3/runtimeAdapters.d.ts +0 -9
  674. package/dist/v3/runtimeAdapters.js +0 -109
  675. package/dist/v3/security.d.ts +0 -36
  676. package/dist/v3/security.js +0 -113
  677. package/dist/v3/serverAdmission.d.ts +0 -38
  678. package/dist/v3/serverAdmission.js +0 -223
  679. package/dist/v3/session.d.ts +0 -322
  680. package/dist/v3/session.js +0 -2302
  681. package/dist/v3/sessionConnector.d.ts +0 -42
  682. package/dist/v3/sessionConnector.js +0 -513
  683. package/dist/v3/streamLifetimeLedger.d.ts +0 -31
  684. package/dist/v3/streamLifetimeLedger.js +0 -131
  685. package/dist/v3/streamMetadata.js +0 -7
  686. package/dist/v3/transportConstants.d.ts +0 -56
  687. package/dist/v3/transportConstants.js +0 -59
  688. package/dist/v3/unreliableMessage.d.ts +0 -44
  689. package/dist/v3/unreliableMessage.js +0 -287
  690. package/dist/v3/webSocketCarrier.d.ts +0 -31
  691. package/dist/v3/webSocketCarrier.js +0 -370
  692. package/dist/v3/webTransportCarrier.d.ts +0 -27
  693. package/dist/v3/webTransportCarrier.js +0 -552
  694. package/dist/ws-client/binaryTransport.d.ts +0 -64
  695. package/dist/ws-client/binaryTransport.js +0 -393
  696. package/dist/yamux/byteReader.d.ts +0 -13
  697. package/dist/yamux/byteReader.js +0 -81
  698. package/dist/yamux/constants.d.ts +0 -10
  699. package/dist/yamux/constants.js +0 -14
  700. package/dist/yamux/errors.d.ts +0 -15
  701. package/dist/yamux/errors.js +0 -34
  702. package/dist/yamux/header.d.ts +0 -17
  703. package/dist/yamux/header.js +0 -26
  704. package/dist/yamux/index.d.ts +0 -6
  705. package/dist/yamux/index.js +0 -6
  706. package/dist/yamux/session.d.ts +0 -81
  707. package/dist/yamux/session.js +0 -401
  708. package/dist/yamux/stream.d.ts +0 -42
  709. package/dist/yamux/stream.js +0 -296
  710. /package/dist/{rpc/caller.js → node/sqlitePoolWorkerProgramV4.d.ts} +0 -0
  711. /package/dist/{rpc/wire.js → node/sqliteWorkerProgramV4.d.ts} +0 -0
  712. /package/dist/{v3/publicSession.d.ts → v4/admissionOffer.js} +0 -0
  713. /package/dist/{v3/streamMetadata.d.ts → v4/runtime/controllerUnaryRoute.js} +0 -0
@@ -0,0 +1,1375 @@
1
+ import { streamMetadataFromDocument } from "../public/streamMetadata.js";
2
+ import { CBORDecoder, cborDecoderCharge, byteLength, byteSlice } from "./runtime/cbor.js";
3
+ import { ResourceError, ResourceVector } from "./runtime/resources.js";
4
+ import { timerChunk } from "./runtime/deadline.js";
5
+ import { acquireMessageStreamAdapter } from "./runtime/streamAdapter.js";
6
+ import { MessageSegments, messageInputBytes, checkApplicationMessageOutput } from "./runtime/messageCodec.js";
7
+ import { messageDefinition, sameMessageDigest, emptyMessageMetadata } from "./messageDefinition.js";
8
+ import { applicationHasPermit } from "./streamHandlers.js";
9
+ import { applicationHasCompletionAncestor, applicationDependsOn } from "./runtime/applicationExecutor.js";
10
+ import { DiagnosticActivity } from "./runtime/diagnosticObservation.js";
11
+ export class V4MessageStreamError extends Error {
12
+ code;
13
+ progress;
14
+ application_input_delivered;
15
+ codec;
16
+ cause;
17
+ constructor(code, progress, application_input_delivered = false, codec) {
18
+ super(code);
19
+ this.code = code;
20
+ this.progress = progress;
21
+ this.application_input_delivered = application_input_delivered;
22
+ this.codec = codec;
23
+ this.name = "V4MessageStreamError";
24
+ this.cause = code === "result_mode_conflict" ? "decoder_started" : undefined;
25
+ }
26
+ }
27
+ const complete = Object.freeze({ status: "complete", core_cleanup: "complete", pending_callbacks: 0n });
28
+ const pending = Object.freeze({ status: "pending", core_cleanup: "pending", pending_callbacks: 0n });
29
+ const empty = new Uint8Array(), token = Symbol("original typed message owner"), copy = Uint8Array.prototype.set;
30
+ const NativePromise = Promise, enqueue = queueMicrotask, defineProperty = Object.defineProperty, freeze = Object.freeze;
31
+ const nativeDecode = TextDecoder.prototype.decode, utf8 = new TextDecoder("utf-8", { fatal: true, ignoreBOM: true });
32
+ const sendCap = 2 * 1024 * 1024 + 32;
33
+ function result(value) { defineProperty(value, "then", { value: undefined }); return freeze(value); }
34
+ function duration(value, fallback, max = 90000n) {
35
+ const n = value ?? fallback;
36
+ if (typeof n !== "bigint" || n < 1n || n > max)
37
+ throw new Error("configuration_capacity");
38
+ return n;
39
+ }
40
+ export function captureMessageOptions(options = {}) {
41
+ return Object.freeze({ assemblyTimeoutMS: duration(options.assemblyTimeoutMS, 30000n), sendTimeoutMS: duration(options.sendTimeoutMS, 30000n), cleanupTimeoutMS: duration(options.cleanupTimeoutMS, 5000n, 30000n) });
42
+ }
43
+ export function messageAdapterProfile(options, prepaid) {
44
+ return Object.freeze({ kind: "message", readBytes: 4096, inputBackingBytes: 1, inputEntries: 1,
45
+ gracefulFinishMS: Number(options.sendTimeoutMS), cleanupMS: Number(options.cleanupTimeoutMS), ...(prepaid === undefined ? {} : { prepaid }) });
46
+ }
47
+ export function messageAdapterCharge(runtimeBytes) {
48
+ return new ResourceVector([25728n + runtimeBytes, 0n, 0n, 5n, 6n, 6n, 4n, 0n, 0n, 1n, 0n]);
49
+ }
50
+ /** One duplex owner, bounded FIFO and persistent message cursor. Complete
51
+ * payloads are allocated only after validating the original four-byte prefix. */
52
+ export class V4TypedMessageStream {
53
+ #owner;
54
+ #inbound;
55
+ #outbound;
56
+ #metadata = emptyMessageMetadata();
57
+ #definition;
58
+ #prepared;
59
+ #options;
60
+ #sends = [];
61
+ #actualSends = new Set();
62
+ #sendBytes = 0;
63
+ #publishing = false;
64
+ #publisher;
65
+ #sealed = false;
66
+ #closed = false;
67
+ #ioEnded = false;
68
+ #ioDetached = false;
69
+ #cleaning = false;
70
+ #failure;
71
+ #closeResult;
72
+ #direction = "c2s";
73
+ #closeTail;
74
+ #closeRunning = false;
75
+ #fin;
76
+ #finResolve;
77
+ #finReject;
78
+ #finDeadline;
79
+ #finTimer;
80
+ #finStarted = false;
81
+ #finResult;
82
+ #finish;
83
+ #readWait;
84
+ #readOwner;
85
+ #readRunning = false;
86
+ #handoff = false;
87
+ #readAdmissionBlocked = false;
88
+ #readScheduled = false;
89
+ #readAbort;
90
+ #prefix = new Uint8Array(4);
91
+ #prefixBytes = 0;
92
+ #length;
93
+ #body = empty;
94
+ #filled = 0;
95
+ #bodyReference;
96
+ #typedReference;
97
+ #typedPrepaid = false;
98
+ #completion;
99
+ #value;
100
+ #decoded = false;
101
+ #decoding = false;
102
+ #applicationInputDelivered = false;
103
+ #decodeContext;
104
+ #decodeAbort;
105
+ #decodeFailed = false;
106
+ #eof = false;
107
+ #assembly;
108
+ #assemblyTimer;
109
+ #resourceObserver;
110
+ #dependencyDeadline;
111
+ #dependencyTimer;
112
+ #cleanupDeadline;
113
+ #cleanupTimer;
114
+ #cleanupIncomplete = false;
115
+ #waits = 0;
116
+ #cleanupWaits = 0;
117
+ #observers = new Set();
118
+ constructor(capability, definition, options) {
119
+ if (capability !== token)
120
+ throw new Error("owner_unavailable");
121
+ messageDefinition(definition);
122
+ this.#definition = definition;
123
+ this.#options = captureMessageOptions(options);
124
+ Object.defineProperty(this, "then", { value: undefined });
125
+ }
126
+ get metadata() { return this.#metadata; }
127
+ /** Internal pre-acceptance preparation has no byte I/O or delivery rights. */
128
+ prepare(capability, context) {
129
+ if (capability !== token || this.#prepared !== undefined || this.#closed)
130
+ throw new Error("owner_unavailable");
131
+ const def = messageDefinition(this.#definition);
132
+ if (context.kind !== this.#definition.kind)
133
+ throw new V4MessageStreamError("definition_mismatch");
134
+ const config = { bytes: 4096, nodes: 272, textBytes: 128, arrayItems: 1, runtimeBytes: context.runtimeBytes };
135
+ const reference = context.reserve("v4_message_metadata", cborDecoderCharge(config));
136
+ let decoder;
137
+ try {
138
+ decoder = new CBORDecoder(config, reference);
139
+ const document = decoder.decodeMap(context.metadata, "TypedMessageMetadata");
140
+ let application;
141
+ try {
142
+ let digest = empty;
143
+ application = empty;
144
+ for (let k = document.firstChild(document.field(0, 2)); k >= 0;) {
145
+ const v = document.nextSibling(k), bytes = new Uint8Array(document.size(v));
146
+ document.copyPayload(v, bytes);
147
+ if (document.text(k) === "definition")
148
+ digest = bytes;
149
+ else
150
+ application = bytes;
151
+ k = document.nextSibling(v);
152
+ }
153
+ if (!sameMessageDigest(digest, def.digest))
154
+ throw new V4MessageStreamError("definition_mismatch");
155
+ }
156
+ finally {
157
+ document.release();
158
+ }
159
+ if (application.length !== 0) {
160
+ const doc = decoder.decodeMap(application, "StreamMetadata");
161
+ try {
162
+ this.#metadata = streamMetadataFromDocument(doc);
163
+ }
164
+ finally {
165
+ doc.release();
166
+ application.fill(0);
167
+ }
168
+ }
169
+ this.#inbound = def.directions[context.localOpener ? 1 : 0];
170
+ this.#outbound = def.directions[context.localOpener ? 0 : 1];
171
+ this.#prepared = Object.freeze({ localOpener: context.localOpener, kind: context.kind });
172
+ }
173
+ finally {
174
+ decoder?.close();
175
+ reference.release();
176
+ }
177
+ }
178
+ /** Internal one-time transfer after the same OPEN reaches accepted. */
179
+ bind(capability, owner) {
180
+ if (capability !== token || this.#owner !== undefined || this.#closed)
181
+ throw new Error("owner_unavailable");
182
+ if (this.#prepared === undefined)
183
+ this.prepare(token, owner);
184
+ if (this.#prepared.localOpener !== owner.localOpener || this.#prepared.kind !== owner.kind)
185
+ throw new V4MessageStreamError("definition_mismatch");
186
+ owner.check();
187
+ this.#owner = owner;
188
+ this.#direction = owner.direction;
189
+ owner.cleanupWith(() => this.#cleanup());
190
+ owner.invalidateWith(() => { if (this.#applicationInputDelivered)
191
+ this.#endIO();
192
+ else
193
+ this.#fail("closed"); });
194
+ owner.ioEndedWith(() => this.#endIO());
195
+ }
196
+ #check() {
197
+ if (this.#closed || this.#owner === undefined)
198
+ throw new V4MessageStreamError(this.#failure ?? "closed");
199
+ this.#owner.check();
200
+ return this.#owner;
201
+ }
202
+ #checkRead() {
203
+ if (!this.#applicationInputDelivered)
204
+ return this.#check();
205
+ if (this.#closed || this.#owner === undefined)
206
+ throw new V4MessageStreamError(this.#failure ?? "closed", undefined, true);
207
+ // The same application completion owns already-disclosed input. Revocation
208
+ // still prevents all future I/O, but cannot recall that original input.
209
+ return this.#owner;
210
+ }
211
+ #endIO() {
212
+ if (this.#closed || this.#ioEnded)
213
+ return;
214
+ // A complete empty frame has no body read to provide another wake.
215
+ if (this.#length === undefined && this.#prefixBytes === 4 && this.#prefix.every(byte => byte === 0)) {
216
+ const owner = this.#owner;
217
+ try {
218
+ this.#bodyReference = owner.reserve("v4_message_body", new ResourceVector([owner.runtimeBytes + 256n, 0n, 0n, 1n, 0n, 0n, 0n, 0n, 0n, 0n, 0n]));
219
+ this.#length = 0;
220
+ }
221
+ catch {
222
+ this.#fail("resource_exhausted");
223
+ return;
224
+ }
225
+ }
226
+ // Only a completely authenticated/assembled body can outlive transport.
227
+ if (this.#length === undefined || this.#filled !== this.#length || this.#bodyReference === undefined) {
228
+ this.#fail("closed");
229
+ return;
230
+ }
231
+ this.#ioEnded = true;
232
+ this.#sealed = true;
233
+ this.#stopAssembly();
234
+ this.#resourceObserver?.();
235
+ this.#resourceObserver = undefined;
236
+ for (const entry of this.#actualSends) {
237
+ entry.revoked = true;
238
+ entry.reason = "closed";
239
+ entry.abort.abort();
240
+ entry.request?.cancel();
241
+ this.#settleSend(entry, "closed");
242
+ if (!entry.running)
243
+ this.#releaseSend(entry);
244
+ }
245
+ this.#sends.length = 0;
246
+ if (this.#finTimer !== undefined)
247
+ clearTimeout(this.#finTimer);
248
+ this.#finTimer = undefined;
249
+ this.#finReject?.(new V4MessageStreamError("closed"));
250
+ this.#finReject = this.#finResolve = undefined;
251
+ if (!this.#readRunning)
252
+ this.#owner?.releaseReader();
253
+ this.#cleanup();
254
+ }
255
+ #snapshot(entry) {
256
+ const accepted = entry.accepted + (entry.request?.progress().accepted_bytes ?? 0n);
257
+ return result({ submission: accepted === 0n ? "not_submitted" : "submitted", stream_bytes_accepted_at_return: accepted,
258
+ publication_pending: accepted > 0n && !entry.done, cleanup_status: entry.done ? complete : pending });
259
+ }
260
+ #settleSend(entry, failure) {
261
+ if (entry.delivered)
262
+ return;
263
+ entry.delivered = true;
264
+ entry.signal?.removeEventListener("abort", entry.canceled);
265
+ if (failure === undefined) {
266
+ entry.resolve(this.#snapshot(entry));
267
+ }
268
+ else {
269
+ entry.diagnosticFailure = failure;
270
+ const error = new V4MessageStreamError(failure, this.#snapshot(entry));
271
+ entry.reject(error);
272
+ }
273
+ this.#finishSendDiagnostic(entry);
274
+ }
275
+ #finishSendDiagnostic(entry) {
276
+ if (!entry.done || !entry.delivered || entry.diagnosticFinished)
277
+ return;
278
+ entry.diagnosticFinished = true;
279
+ if (entry.diagnosticFailure !== undefined)
280
+ entry.diagnostic.failure(new V4MessageStreamError(entry.diagnosticFailure));
281
+ else {
282
+ entry.diagnostic.event({ state: "ready", code: "ok" });
283
+ entry.diagnostic.close();
284
+ }
285
+ }
286
+ send(value, options = {}) {
287
+ const diagnostic = new DiagnosticActivity(this.#owner?.diagnostics, "application");
288
+ let resolve, reject;
289
+ const promise = new NativePromise((yes, no) => { resolve = yes; reject = no; });
290
+ let reference, admitted;
291
+ try {
292
+ const owner = this.#check();
293
+ if (this.#sealed)
294
+ throw new V4MessageStreamError("closed");
295
+ if (options.signal?.aborted)
296
+ throw new V4MessageStreamError("canceled");
297
+ if (options.admission !== undefined && options.admission !== "queued" && options.admission !== "try_now")
298
+ throw new V4MessageStreamError("encode_failed");
299
+ if ((options.admission === "try_now" || applicationHasPermit(options.context)) && (this.#publishing || this.#sends.length !== 0))
300
+ throw new V4MessageStreamError("would_block");
301
+ if (this.#actualSends.size === 8 || this.#sendBytes + 4 > sendCap)
302
+ throw new V4MessageStreamError("resource_exhausted");
303
+ const maximum = this.#outbound.maximum, codec = this.#outbound.application;
304
+ const bytes = messageInputBytes(value, this.#outbound.implementation, maximum), outputBytes = codec === undefined ? 0 : 2 * maximum;
305
+ if (outputBytes + 4 > sendCap - this.#sendBytes)
306
+ throw new V4MessageStreamError("resource_exhausted");
307
+ const timeout = duration(options.timeoutMS, this.#options.sendTimeoutMS, this.#options.sendTimeoutMS), deadline = owner.deadline(timeout);
308
+ reference = owner.reserveSend("v4_message_send", new ResourceVector([BigInt(bytes + outputBytes) + (codec?.applicationBytes ?? 0n) + owner.runtimeBytes + 512n, 0n, 0n, 1n, 1n, 1n, 1n, 0n, 0n, 0n, 0n]));
309
+ const entry = { diagnostic, diagnosticFailure: undefined, diagnosticFinished: false, value, reference, deadline, signal: options.signal, canceled: () => this.#cancelSend(entry), resolve, reject, timer: undefined,
310
+ segments: undefined, request: undefined, capacity: outputBytes + 4, accepted: 0n, prefix: new Uint8Array(4), running: false, done: false, delivered: false, revoked: false, reason: undefined,
311
+ abort: new AbortController(), context: options.context, permit: undefined, payload: undefined, preparing: false, applicationContext: undefined };
312
+ this.#sendBytes += entry.capacity;
313
+ this.#actualSends.add(entry);
314
+ this.#sends.push(entry);
315
+ reference = undefined;
316
+ admitted = entry;
317
+ const guard = () => { this.#check(); entry.deadline.check(); if (entry.signal?.aborted || entry.revoked)
318
+ throw new V4MessageStreamError("canceled"); };
319
+ if (applicationHasPermit(options.context)) {
320
+ let used = false;
321
+ if (codec?.execution !== "async") {
322
+ entry.running = entry.preparing = true;
323
+ try {
324
+ const synchronous = owner.application.synchronous(options.context, guard, () => this.#encodeSend(entry, owner, guard, options.context));
325
+ used = synchronous.used;
326
+ }
327
+ finally {
328
+ entry.running = entry.preparing = false;
329
+ }
330
+ }
331
+ if (!used)
332
+ entry.permit = owner.application.tryOrdinary("short", options.context);
333
+ }
334
+ else if (options.admission === "try_now")
335
+ entry.permit = owner.application.tryOrdinary("short");
336
+ const expire = () => {
337
+ try {
338
+ entry.deadline.check();
339
+ entry.timer = setTimeout(expire, timerChunk(entry.deadline.remainingMS()));
340
+ }
341
+ catch {
342
+ entry.timer = undefined;
343
+ this.#cancelSend(entry, "deadline_exceeded");
344
+ }
345
+ };
346
+ entry.signal?.addEventListener("abort", entry.canceled, { once: true });
347
+ expire();
348
+ enqueue(() => { if (entry.signal?.aborted)
349
+ this.#cancelSend(entry); this.#publish(); });
350
+ }
351
+ catch (error) {
352
+ reference?.release();
353
+ const code = error instanceof V4MessageStreamError ? error.code : error instanceof ResourceError ? "resource_exhausted" :
354
+ error instanceof Error && (error.message === "dependency_unavailable" || error.message === "would_block") ? error.message : "encode_failed";
355
+ if (admitted !== undefined) {
356
+ this.#cancelSend(admitted, code);
357
+ return promise;
358
+ }
359
+ diagnostic.failure(new V4MessageStreamError(code));
360
+ reject(new V4MessageStreamError(code, result({ submission: "not_submitted", stream_bytes_accepted_at_return: 0n, publication_pending: false, cleanup_status: complete })));
361
+ }
362
+ return promise;
363
+ }
364
+ #cancelSend(entry, reason = "canceled") {
365
+ if (entry.done)
366
+ return;
367
+ const submitted = entry.accepted + (entry.request?.progress().accepted_bytes ?? 0n) > 0n;
368
+ if (!submitted) {
369
+ entry.revoked = true;
370
+ entry.reason = reason;
371
+ entry.abort.abort();
372
+ entry.request?.cancel();
373
+ const i = this.#sends.indexOf(entry);
374
+ if (i >= 0)
375
+ this.#sends.splice(i, 1);
376
+ if (entry.preparing && this.#publisher === entry) {
377
+ this.#publisher = undefined;
378
+ this.#publishing = false;
379
+ }
380
+ }
381
+ else if (reason !== "canceled") {
382
+ entry.reason = reason;
383
+ entry.request?.cancel();
384
+ this.#fail(reason);
385
+ }
386
+ this.#settleSend(entry, reason);
387
+ if (!entry.running)
388
+ this.#releaseSend(entry);
389
+ this.#publish();
390
+ }
391
+ #publish() {
392
+ if (this.#publishing || this.#closed || this.#ioEnded)
393
+ return;
394
+ const entry = this.#sends[0];
395
+ if (entry === undefined) {
396
+ this.#startFIN();
397
+ return;
398
+ }
399
+ this.#publishing = true;
400
+ this.#publisher = entry;
401
+ void this.#runSend(entry).finally(() => {
402
+ if (this.#publisher === entry) {
403
+ this.#publisher = undefined;
404
+ this.#publishing = false;
405
+ }
406
+ this.#publish();
407
+ this.#cleanup();
408
+ });
409
+ }
410
+ #encodeSend(entry, owner, guard, context) {
411
+ guard();
412
+ const codec = this.#outbound.application;
413
+ if (codec !== undefined) {
414
+ if (codec.execution !== "sync" || context === undefined)
415
+ throw new V4MessageStreamError("encode_failed");
416
+ entry.applicationContext = context;
417
+ try {
418
+ let output;
419
+ try {
420
+ output = codec.encode(context, entry.value);
421
+ }
422
+ catch {
423
+ throw new V4MessageStreamError("encode_failed");
424
+ }
425
+ this.#sealApplicationOutput(entry, guard, output);
426
+ output = undefined;
427
+ this.#finishApplicationEncoding(entry, owner);
428
+ }
429
+ finally {
430
+ entry.applicationContext = undefined;
431
+ }
432
+ return;
433
+ }
434
+ const segments = entry.segments = new MessageSegments(this.#outbound.maximum, owner.runtimeBytes, (charge, capacity) => {
435
+ guard();
436
+ if (capacity > sendCap - this.#sendBytes)
437
+ throw new ResourceError("resource_exhausted");
438
+ const ref = owner.reserveSend("v4_message_segment", charge);
439
+ this.#sendBytes += capacity;
440
+ entry.capacity += capacity;
441
+ return ref;
442
+ }, guard);
443
+ if (this.#outbound.implementation === "bytes")
444
+ segments.append(entry.value);
445
+ else
446
+ segments.utf8(entry.value);
447
+ entry.payload = segments.finalize();
448
+ entry.value = undefined;
449
+ new DataView(entry.prefix.buffer).setUint32(0, segments.length);
450
+ entry.reference.shrink(new ResourceVector([owner.runtimeBytes + 512n, 0n, 0n, 1n, 1n, 1n, 1n, 0n, 0n, 0n, 0n]));
451
+ }
452
+ #sealApplicationOutput(entry, guard, value) {
453
+ guard();
454
+ let source = checkApplicationMessageOutput(value, this.#outbound.maximum);
455
+ const length = byteLength(source), owned = new Uint8Array(length);
456
+ copy.call(owned, source);
457
+ source = undefined;
458
+ value = undefined;
459
+ entry.value = undefined;
460
+ entry.payload = [owned];
461
+ new DataView(entry.prefix.buffer).setUint32(0, length);
462
+ }
463
+ #finishApplicationEncoding(entry, owner) {
464
+ const length = byteLength(entry.payload[0]);
465
+ this.#sendBytes -= entry.capacity - length - 4;
466
+ entry.capacity = length + 4;
467
+ entry.reference.shrink(new ResourceVector([BigInt(length) + owner.runtimeBytes + 512n, 0n, 0n, 1n, 1n, 1n, 1n, 0n, 0n, 0n, 0n]));
468
+ }
469
+ async #runSend(entry) {
470
+ entry.running = true;
471
+ let failure;
472
+ let applicationTail;
473
+ try {
474
+ const owner = this.#check();
475
+ const guard = () => { this.#check(); entry.deadline.check(); if (entry.revoked)
476
+ throw new V4MessageStreamError(entry.reason ?? "canceled"); };
477
+ guard();
478
+ if (entry.payload === undefined) {
479
+ entry.preparing = true;
480
+ let invocation;
481
+ try {
482
+ entry.permit ??= await owner.application.acquire("short", entry.abort.signal, guard, entry.context);
483
+ guard();
484
+ const codec = this.#outbound.application;
485
+ if (codec !== undefined) {
486
+ if (owner.authentication === undefined)
487
+ throw new V4MessageStreamError("encode_failed");
488
+ invocation = owner.application.context(entry.permit, owner.authentication, entry.abort.signal);
489
+ entry.applicationContext = invocation.context;
490
+ applicationTail = owner.sessionCleanup.startApplicationCallback();
491
+ if (codec.execution === "async") {
492
+ let output;
493
+ try {
494
+ output = await codec.encode(invocation.context, entry.value);
495
+ }
496
+ catch {
497
+ throw new V4MessageStreamError("encode_failed");
498
+ }
499
+ this.#sealApplicationOutput(entry, guard, output);
500
+ output = undefined;
501
+ this.#finishApplicationEncoding(entry, owner);
502
+ }
503
+ else
504
+ this.#encodeSend(entry, owner, guard, invocation.context);
505
+ }
506
+ else
507
+ this.#encodeSend(entry, owner, guard);
508
+ }
509
+ finally {
510
+ invocation?.release();
511
+ entry.applicationContext = undefined;
512
+ entry.permit?.release();
513
+ entry.permit = undefined;
514
+ entry.preparing = false;
515
+ applicationTail?.callbackExited();
516
+ }
517
+ }
518
+ const payload = entry.payload;
519
+ for (const bytes of [entry.prefix, ...payload]) {
520
+ for (let at = 0; at < bytes.length;) {
521
+ guard();
522
+ const count = Math.min(owner.maxWriteBytes, bytes.length - at);
523
+ if (count > sendCap - this.#sendBytes)
524
+ throw new ResourceError("resource_exhausted");
525
+ const remaining = entry.deadline.remainingMS(), request = owner.prepareWrite(byteSlice(bytes, at, at + count), remaining < owner.maxWriteMilliseconds ? remaining : owner.maxWriteMilliseconds);
526
+ entry.request = request;
527
+ this.#sendBytes += count;
528
+ try {
529
+ if (entry.revoked)
530
+ request.cancel();
531
+ request.start();
532
+ const progress = await request.wait();
533
+ await request.waitCleanup();
534
+ entry.accepted += progress.accepted_bytes;
535
+ entry.request = undefined;
536
+ if (progress.accepted_bytes !== BigInt(count) || progress.terminal_reason !== "complete")
537
+ throw new V4MessageStreamError(entry.reason ?? "write_failed");
538
+ at += count;
539
+ }
540
+ finally {
541
+ this.#sendBytes -= count;
542
+ }
543
+ }
544
+ }
545
+ }
546
+ catch (error) {
547
+ failure = entry.reason ?? (error instanceof V4MessageStreamError ? error.code : error instanceof ResourceError ? "resource_exhausted" : "encode_failed");
548
+ if (!this.#ioEnded && entry.accepted + (entry.request?.progress().accepted_bytes ?? 0n) > 0n)
549
+ this.#fail(failure);
550
+ }
551
+ finally {
552
+ if (entry.request !== undefined) {
553
+ entry.request.cancel();
554
+ await entry.request.waitCleanup();
555
+ entry.accepted += entry.request.progress().accepted_bytes;
556
+ entry.request = undefined;
557
+ }
558
+ const i = this.#sends.indexOf(entry);
559
+ if (i >= 0)
560
+ this.#sends.splice(i, 1);
561
+ applicationTail?.finish();
562
+ entry.running = false;
563
+ this.#releaseSend(entry);
564
+ this.#settleSend(entry, failure);
565
+ }
566
+ }
567
+ #releaseSend(entry) {
568
+ if (entry.done)
569
+ return;
570
+ entry.done = true;
571
+ if (entry.timer !== undefined)
572
+ clearTimeout(entry.timer);
573
+ entry.timer = undefined;
574
+ entry.signal?.removeEventListener("abort", entry.canceled);
575
+ entry.value = undefined;
576
+ entry.permit?.release();
577
+ entry.permit = undefined;
578
+ if (entry.segments === undefined)
579
+ for (const bytes of entry.payload ?? [])
580
+ bytes.fill(0);
581
+ entry.segments?.close();
582
+ entry.segments = undefined;
583
+ entry.payload = undefined;
584
+ entry.prefix.fill(0);
585
+ entry.prefix = empty;
586
+ this.#sendBytes -= entry.capacity;
587
+ entry.capacity = 0;
588
+ entry.reference.release();
589
+ this.#actualSends.delete(entry);
590
+ this.#finishSendDiagnostic(entry);
591
+ }
592
+ receive(options) { return this.#receive(false, options); }
593
+ receiveEncoded(options) { return this.#receive(true, options); }
594
+ #receive(encoded, options) {
595
+ const diagnostic = new DiagnosticActivity(this.#owner?.diagnostics, "application");
596
+ let resolve, reject;
597
+ const promise = new NativePromise((yes, no) => { resolve = yes; reject = no; });
598
+ try {
599
+ this.#checkRead();
600
+ if (applicationDependsOn(options?.context, this.#decodeContext))
601
+ throw new V4MessageStreamError("dependency_unavailable", undefined, this.#applicationInputDelivered);
602
+ if (this.#readWait !== undefined)
603
+ throw new V4MessageStreamError("read_in_progress");
604
+ if (this.#ioEnded && this.#length === undefined)
605
+ throw new V4MessageStreamError("closed");
606
+ if (options?.signal?.aborted)
607
+ throw new V4MessageStreamError("canceled");
608
+ applicationHasPermit(options?.context);
609
+ if (encoded && this.#applicationInputDelivered)
610
+ throw new V4MessageStreamError("result_mode_conflict", undefined, true, this.#codecIdentity());
611
+ const wait = { diagnostic, diagnosticOutcome: undefined, diagnosticFinished: false, encoded, signal: options?.signal, context: options?.context, claim: undefined, resolve, reject, canceled: () => {
612
+ if (this.#readWait !== wait || this.#handoff)
613
+ return;
614
+ this.#readWait = undefined;
615
+ wait.signal?.removeEventListener("abort", wait.canceled);
616
+ if (!this.#decoded && !this.#applicationInputDelivered && this.#typedPrepaid && this.#bodyReference !== undefined && this.#length !== undefined) {
617
+ try {
618
+ this.#bodyReference.shrink(new ResourceVector([BigInt(this.#body.buffer.byteLength) + this.#owner.runtimeBytes + 256n, 0n, 0n, 1n, 0n, 0n, 0n, 0n, 0n, 0n, 0n]));
619
+ this.#typedPrepaid = false;
620
+ }
621
+ catch { /* Closed roots retain the original charge until cleanup. */ }
622
+ }
623
+ if (!this.#decoded && !this.#applicationInputDelivered) {
624
+ this.#typedReference?.release();
625
+ this.#typedReference = undefined;
626
+ this.#completion?.close();
627
+ this.#completion = undefined;
628
+ }
629
+ wait.claim?.release();
630
+ wait.claim = undefined;
631
+ if (this.#dependencyTimer !== undefined)
632
+ clearTimeout(this.#dependencyTimer);
633
+ this.#dependencyTimer = undefined;
634
+ const error = new V4MessageStreamError("canceled", undefined, this.#applicationInputDelivered);
635
+ this.#readAbort?.abort();
636
+ wait.diagnosticOutcome = error;
637
+ this.#finishReadDiagnostic(wait);
638
+ wait.reject(error);
639
+ } };
640
+ this.#readWait = wait;
641
+ wait.signal?.addEventListener("abort", wait.canceled, { once: true });
642
+ this.#scheduleRead();
643
+ }
644
+ catch (error) {
645
+ const failure = error instanceof V4MessageStreamError ? error : new V4MessageStreamError(error instanceof Error && error.message === "dependency_unavailable" ? "dependency_unavailable" : "closed");
646
+ diagnostic.failure(failure);
647
+ reject(failure);
648
+ }
649
+ return promise;
650
+ }
651
+ #finishReadDiagnostic(wait) {
652
+ if (wait === this.#readOwner || wait.diagnosticOutcome === undefined || wait.diagnosticFinished)
653
+ return;
654
+ wait.diagnosticFinished = true;
655
+ if (wait.diagnosticOutcome === "ready") {
656
+ wait.diagnostic.event({ state: "ready", code: "ok" });
657
+ wait.diagnostic.close();
658
+ }
659
+ else
660
+ wait.diagnostic.failure(wait.diagnosticOutcome);
661
+ }
662
+ #releaseReadOwner() {
663
+ const original = this.#readOwner;
664
+ this.#readOwner = undefined;
665
+ if (original !== undefined)
666
+ this.#finishReadDiagnostic(original);
667
+ }
668
+ #scheduleRead() {
669
+ if (this.#readScheduled || this.#readRunning || this.#closed || this.#readWait === undefined)
670
+ return;
671
+ this.#readScheduled = true;
672
+ enqueue(() => { this.#readScheduled = false; if (!this.#closed && this.#readWait !== undefined)
673
+ void this.#runRead(); });
674
+ }
675
+ #startAssembly() {
676
+ if (this.#assembly !== undefined)
677
+ return;
678
+ this.#assembly = this.#owner.deadline(this.#options.assemblyTimeoutMS);
679
+ const tick = () => {
680
+ try {
681
+ this.#assembly.check();
682
+ this.#assemblyTimer = setTimeout(tick, timerChunk(this.#assembly.remainingMS()));
683
+ }
684
+ catch {
685
+ this.#assemblyTimer = undefined;
686
+ this.#fail("deadline_exceeded");
687
+ }
688
+ };
689
+ tick();
690
+ }
691
+ #stopAssembly() { if (this.#assemblyTimer !== undefined)
692
+ clearTimeout(this.#assemblyTimer); this.#assemblyTimer = undefined; this.#assembly = undefined; }
693
+ #bodyAdmission(wait) {
694
+ const owner = this.#owner, n = this.#length, typed = !wait.encoded;
695
+ const dependent = applicationHasCompletionAncestor(wait.context);
696
+ let completion;
697
+ try {
698
+ if (dependent) {
699
+ this.#dependencyDeadline ??= owner.deadline(30000n);
700
+ this.#dependencyDeadline.check();
701
+ if (typed && wait.claim?.active !== true)
702
+ wait.claim = owner.application.claimCompletion(wait.context);
703
+ if (this.#dependencyTimer === undefined) {
704
+ const tick = () => {
705
+ try {
706
+ this.#dependencyDeadline.check();
707
+ this.#dependencyTimer = setTimeout(tick, timerChunk(this.#dependencyDeadline.remainingMS()));
708
+ }
709
+ catch {
710
+ this.#dependencyTimer = undefined;
711
+ const current = this.#readWait;
712
+ if (current !== undefined && current.context !== undefined)
713
+ this.#rejectRead(current, "completion_dependency_unavailable");
714
+ }
715
+ };
716
+ tick();
717
+ }
718
+ }
719
+ if (typed && this.#completion === undefined)
720
+ completion = owner.application.reserveCompletion();
721
+ if (this.#bodyReference === undefined) {
722
+ const extra = typed ? (this.#inbound.implementation === "utf8" ? BigInt(n) * 2n : 0n) + (this.#inbound.application?.applicationBytes ?? 0n) + owner.runtimeBytes + 256n : 0n;
723
+ const body = owner.admitBody(n, new ResourceVector([owner.runtimeBytes + 256n + extra, 0n, 0n, typed ? 2n : 1n, typed ? 1n : 0n, typed ? 1n : 0n, 0n, 0n, 0n, 0n, 0n]));
724
+ this.#bodyReference = body.reference;
725
+ this.#typedPrepaid = typed;
726
+ this.#body = body.bytes;
727
+ }
728
+ if (typed && !this.#typedPrepaid && this.#typedReference === undefined)
729
+ this.#typedReference = owner.reserve("v4_message_decode", new ResourceVector([
730
+ (this.#inbound.implementation === "utf8" ? BigInt(n) * 2n : 0n) + (this.#inbound.application?.applicationBytes ?? 0n) + owner.runtimeBytes + 256n, 0n, 0n, 1n, 1n, 1n, 0n, 0n, 0n, 0n, 0n,
731
+ ]));
732
+ if (completion !== undefined) {
733
+ this.#completion = completion;
734
+ completion = undefined;
735
+ }
736
+ const observe = this.#resourceObserver;
737
+ this.#resourceObserver = undefined;
738
+ observe?.();
739
+ this.#readAdmissionBlocked = false;
740
+ owner.pauseCredit(false);
741
+ return true;
742
+ }
743
+ catch (error) {
744
+ completion?.close();
745
+ wait.claim?.release();
746
+ wait.claim = undefined;
747
+ if (dependent) {
748
+ this.#rejectRead(wait, "completion_dependency_unavailable");
749
+ return false;
750
+ }
751
+ if (!(error instanceof ResourceError) || error.code !== "resource_exhausted")
752
+ throw error;
753
+ owner.pauseCredit(true);
754
+ // No short cursor/borrow is held while waiting for a complete vector.
755
+ owner.releaseReader();
756
+ this.#readAdmissionBlocked = true;
757
+ this.#resourceObserver ??= owner.observeResources(() => {
758
+ if (this.#readRunning) {
759
+ this.#readAdmissionBlocked = false;
760
+ return;
761
+ }
762
+ this.#scheduleRead();
763
+ });
764
+ return false;
765
+ }
766
+ }
767
+ async #runRead() {
768
+ if (this.#readRunning)
769
+ return;
770
+ this.#readRunning = true;
771
+ // A failed attempt's own temporary refunds are not new capacity. Subscribe
772
+ // only after the attempt exits; subsequent owner transitions wake it once.
773
+ const observe = this.#resourceObserver;
774
+ this.#resourceObserver = undefined;
775
+ observe?.();
776
+ try {
777
+ while (this.#readWait !== undefined && !this.#closed) {
778
+ this.#releaseReadOwner();
779
+ this.#readOwner = this.#readWait;
780
+ const owner = this.#checkRead();
781
+ if (this.#readWait === undefined || this.#closed)
782
+ break;
783
+ if (this.#eof) {
784
+ this.#deliverRead();
785
+ break;
786
+ }
787
+ if (this.#prefixBytes === 0 && this.#length === undefined) {
788
+ try {
789
+ owner.ensureReadQueue();
790
+ }
791
+ catch (error) {
792
+ if (!(error instanceof ResourceError) || error.code !== "resource_exhausted")
793
+ throw error;
794
+ owner.pauseCredit(true);
795
+ owner.releaseReader();
796
+ this.#readAdmissionBlocked = true;
797
+ this.#resourceObserver ??= owner.observeResources(() => {
798
+ if (this.#readRunning) {
799
+ this.#readAdmissionBlocked = false;
800
+ return;
801
+ }
802
+ this.#scheduleRead();
803
+ });
804
+ break;
805
+ }
806
+ this.#readAdmissionBlocked = false;
807
+ owner.pauseCredit(false);
808
+ }
809
+ if (this.#prefixBytes === 4 && this.#length === undefined) {
810
+ this.#length = new DataView(this.#prefix.buffer).getUint32(0);
811
+ if (this.#length > this.#inbound.maximum) {
812
+ this.#fail("framing_error");
813
+ break;
814
+ }
815
+ }
816
+ if (this.#length !== undefined && !this.#bodyAdmission(this.#readWait))
817
+ break;
818
+ if (this.#length !== undefined && this.#filled === this.#length) {
819
+ this.#stopAssembly();
820
+ owner.releaseReader();
821
+ if (!this.#readWait.encoded && !this.#decoded) {
822
+ const wait = this.#readWait, abort = this.#readAbort = new AbortController();
823
+ let permit;
824
+ const check = () => {
825
+ this.#check();
826
+ if (this.#readWait !== wait || abort.signal.aborted)
827
+ throw new Error("canceled");
828
+ if (wait.context !== undefined) {
829
+ applicationHasPermit(wait.context);
830
+ if (applicationHasCompletionAncestor(wait.context))
831
+ this.#dependencyDeadline?.check();
832
+ }
833
+ };
834
+ try {
835
+ permit = await this.#completion.acquire(abort.signal, check, wait.context, wait.claim);
836
+ wait.claim = undefined;
837
+ check();
838
+ const codec = this.#inbound.application;
839
+ if (codec === undefined) {
840
+ try {
841
+ this.#value = this.#inbound.implementation === "bytes" ? this.#body : nativeDecode.call(utf8, this.#body);
842
+ }
843
+ catch {
844
+ this.#fail("framing_error");
845
+ break;
846
+ }
847
+ }
848
+ else {
849
+ if (owner.authentication === undefined)
850
+ throw new V4MessageStreamError("decode_failed");
851
+ const decodeAbort = this.#decodeAbort = new AbortController();
852
+ const invocation = owner.application.context(permit, owner.authentication, decodeAbort.signal);
853
+ try {
854
+ check();
855
+ this.#decodeContext = invocation.context;
856
+ this.#decoding = true;
857
+ this.#applicationInputDelivered = true;
858
+ this.#readAbort = undefined;
859
+ try {
860
+ if (codec.execution === "sync")
861
+ this.#value = codec.decode(invocation.context, this.#body);
862
+ else {
863
+ const output = codec.decode(invocation.context, this.#body);
864
+ this.#cleanup();
865
+ this.#value = await output;
866
+ }
867
+ }
868
+ catch {
869
+ this.#decodeFailed = true;
870
+ }
871
+ }
872
+ finally {
873
+ invocation.release();
874
+ this.#decodeContext = undefined;
875
+ this.#decodeAbort = undefined;
876
+ this.#decoding = false;
877
+ }
878
+ }
879
+ this.#decoded = true;
880
+ }
881
+ catch {
882
+ if (this.#readWait === wait)
883
+ this.#rejectRead(wait, "completion_dependency_unavailable");
884
+ break;
885
+ }
886
+ finally {
887
+ permit?.release();
888
+ if (this.#readAbort === abort)
889
+ this.#readAbort = undefined;
890
+ }
891
+ }
892
+ this.#deliverRead();
893
+ break;
894
+ }
895
+ const before = owner.readState();
896
+ if (before.stream_status !== "open") {
897
+ if (before.stream_status === "eof" && this.#prefixBytes === 0) {
898
+ this.#eof = true;
899
+ owner.releaseReader();
900
+ continue;
901
+ }
902
+ this.#fail("framing_error");
903
+ break;
904
+ }
905
+ const abort = this.#readAbort = new AbortController();
906
+ await owner.readInto(this.#length === undefined ? 4 - this.#prefixBytes : this.#length - this.#filled, bytes => {
907
+ if (this.#closed || abort.signal.aborted)
908
+ return 0;
909
+ if (this.#prefixBytes === 0)
910
+ this.#startAssembly();
911
+ else
912
+ this.#assembly?.check();
913
+ const n = byteLength(bytes);
914
+ if (this.#length === undefined) {
915
+ copy.call(this.#prefix, bytes, this.#prefixBytes);
916
+ this.#prefixBytes += n;
917
+ if (this.#prefixBytes === 4)
918
+ owner.pauseCredit(true);
919
+ }
920
+ else {
921
+ copy.call(this.#body, bytes, this.#filled);
922
+ this.#filled += n;
923
+ if (this.#filled === this.#length)
924
+ this.#stopAssembly();
925
+ }
926
+ return n;
927
+ }, abort.signal);
928
+ this.#readAbort = undefined;
929
+ const state = owner.readState();
930
+ if (state.stream_status !== "open") {
931
+ if (state.stream_status === "eof" && this.#prefixBytes === 0) {
932
+ this.#eof = true;
933
+ owner.releaseReader();
934
+ }
935
+ else if (!(this.#ioEnded && this.#length !== undefined && this.#filled === this.#length) &&
936
+ (state.stream_status !== "eof" || this.#prefixBytes < 4 || this.#length !== undefined && this.#filled < this.#length)) {
937
+ this.#fail("framing_error");
938
+ break;
939
+ }
940
+ else if (this.#length === undefined && new DataView(this.#prefix.buffer).getUint32(0) !== 0) {
941
+ this.#fail("framing_error");
942
+ break;
943
+ }
944
+ }
945
+ }
946
+ }
947
+ catch {
948
+ if (!this.#readAbort?.signal.aborted && !this.#closed)
949
+ this.#fail("framing_error");
950
+ }
951
+ finally {
952
+ this.#readAbort = undefined;
953
+ this.#readRunning = false;
954
+ if (this.#closed) {
955
+ this.#owner?.releaseReader();
956
+ this.#clearBody();
957
+ }
958
+ else if (this.#ioEnded)
959
+ this.#owner?.releaseReader();
960
+ this.#cleanup();
961
+ this.#releaseReadOwner();
962
+ // A replacement waiter may have arrived while the old native read exited.
963
+ if (!this.#closed && this.#readWait !== undefined && !this.#readAdmissionBlocked)
964
+ this.#scheduleRead();
965
+ }
966
+ }
967
+ #codecIdentity() {
968
+ const inbound = this.#inbound;
969
+ return inbound === undefined ? undefined : result({
970
+ codec_schema_digest: new Uint8Array(inbound.schema), codec_revision: inbound.revision,
971
+ });
972
+ }
973
+ #deliverRead() {
974
+ const wait = this.#readWait;
975
+ if (wait === undefined || wait.signal?.aborted)
976
+ return;
977
+ this.#checkRead();
978
+ if (this.#readWait !== wait || wait.signal?.aborted || this.#closed)
979
+ return;
980
+ const identity = this.#codecIdentity();
981
+ if (!this.#eof && identity === undefined) {
982
+ this.#fail("definition_mismatch");
983
+ return;
984
+ }
985
+ const value = this.#eof ? result({ done: true }) : result({
986
+ done: false, value: wait.encoded ? this.#body : this.#value,
987
+ application_input_delivered: this.#applicationInputDelivered,
988
+ codec_schema_digest: identity.codec_schema_digest, codec_revision: identity.codec_revision,
989
+ });
990
+ wait.signal?.removeEventListener("abort", wait.canceled);
991
+ wait.claim?.release();
992
+ wait.claim = undefined;
993
+ const failed = this.#decodeFailed && !wait.encoded;
994
+ this.#checkRead();
995
+ if (this.#readWait !== wait || wait.signal?.aborted || this.#closed)
996
+ return;
997
+ this.#handoff = true;
998
+ try {
999
+ if (failed) {
1000
+ const error = new V4MessageStreamError("decode_failed", undefined, this.#applicationInputDelivered, identity);
1001
+ wait.diagnosticOutcome = error;
1002
+ wait.reject(error);
1003
+ }
1004
+ else {
1005
+ wait.diagnosticOutcome = "ready";
1006
+ wait.resolve(value);
1007
+ }
1008
+ }
1009
+ finally {
1010
+ // Reentrant host hooks cannot reclaim, zero, or deliver this same body.
1011
+ if (!this.#eof)
1012
+ this.#clearBody(!failed && (wait.encoded || this.#inbound.implementation === "bytes"));
1013
+ this.#readWait = undefined;
1014
+ this.#handoff = false;
1015
+ this.#finishReadDiagnostic(wait);
1016
+ }
1017
+ }
1018
+ #rejectRead(wait, reason) {
1019
+ if (this.#readWait !== wait)
1020
+ return;
1021
+ this.#readWait = undefined;
1022
+ wait.signal?.removeEventListener("abort", wait.canceled);
1023
+ wait.claim?.release();
1024
+ wait.claim = undefined;
1025
+ this.#readAbort?.abort();
1026
+ this.#owner?.releaseReader();
1027
+ if (this.#dependencyTimer !== undefined)
1028
+ clearTimeout(this.#dependencyTimer);
1029
+ this.#dependencyTimer = undefined;
1030
+ const error = new V4MessageStreamError(reason, undefined, this.#applicationInputDelivered, this.#codecIdentity());
1031
+ wait.diagnosticOutcome = error;
1032
+ this.#finishReadDiagnostic(wait);
1033
+ wait.reject(error);
1034
+ }
1035
+ #clearBody(delivered = false) {
1036
+ this.#stopAssembly();
1037
+ if (this.#dependencyTimer !== undefined)
1038
+ clearTimeout(this.#dependencyTimer);
1039
+ this.#dependencyTimer = undefined;
1040
+ this.#dependencyDeadline = undefined;
1041
+ if (!delivered && !this.#applicationInputDelivered)
1042
+ this.#body.fill(0);
1043
+ this.#body = empty;
1044
+ this.#value = undefined;
1045
+ this.#bodyReference?.release();
1046
+ this.#bodyReference = undefined;
1047
+ this.#typedReference?.release();
1048
+ this.#typedReference = undefined;
1049
+ this.#completion?.close();
1050
+ this.#completion = undefined;
1051
+ this.#length = undefined;
1052
+ this.#filled = 0;
1053
+ this.#prefixBytes = 0;
1054
+ this.#prefix.fill(0);
1055
+ this.#decoded = this.#decodeFailed = this.#typedPrepaid = this.#applicationInputDelivered = false;
1056
+ }
1057
+ closeWrite(options) {
1058
+ if (this.#fin === undefined) {
1059
+ try {
1060
+ const owner = this.#check();
1061
+ this.#sealed = true;
1062
+ this.#finDeadline = owner.deadline(this.#options.sendTimeoutMS);
1063
+ this.#fin = new NativePromise((resolve, reject) => { this.#finResolve = resolve; this.#finReject = reject; });
1064
+ void this.#fin.catch(() => undefined);
1065
+ const expire = () => {
1066
+ try {
1067
+ this.#finDeadline.check();
1068
+ this.#finTimer = setTimeout(expire, timerChunk(this.#finDeadline.remainingMS()));
1069
+ }
1070
+ catch {
1071
+ this.#finTimer = undefined;
1072
+ this.#fail("deadline_exceeded");
1073
+ }
1074
+ };
1075
+ expire();
1076
+ this.#publish();
1077
+ }
1078
+ catch {
1079
+ return Promise.reject(new V4MessageStreamError(this.#failure ?? "closed"));
1080
+ }
1081
+ }
1082
+ return this.#wait(this.#fin, options);
1083
+ }
1084
+ #startFIN() {
1085
+ if (this.#fin === undefined || this.#finStarted || this.#closed)
1086
+ return;
1087
+ this.#finStarted = true;
1088
+ void this.#owner.closeWrite().then(value => {
1089
+ if (this.#finTimer !== undefined)
1090
+ clearTimeout(this.#finTimer);
1091
+ this.#finTimer = undefined;
1092
+ this.#finResult = value;
1093
+ this.#finResolve?.(value);
1094
+ this.#finResolve = this.#finReject = undefined;
1095
+ this.#cleanup();
1096
+ }, () => this.#fail("write_failed"));
1097
+ }
1098
+ finish(options) {
1099
+ if (this.#finish === undefined) {
1100
+ this.#finish = this.closeWrite().then(async () => {
1101
+ if (this.#owner === undefined && this.#failure === undefined && this.#finResult !== undefined)
1102
+ return result({ ...this.#finResult, send_drained: true, read_terminal: "eof", cleanup_status: complete });
1103
+ this.#check();
1104
+ const value = await this.#owner.finish();
1105
+ this.#finResult = value;
1106
+ return value;
1107
+ });
1108
+ void this.#finish.catch(() => undefined);
1109
+ }
1110
+ return this.#wait(this.#finish, options);
1111
+ }
1112
+ close(options) {
1113
+ this.#fail("closed");
1114
+ return this.waitCleanup(options).then(cleanup => result({ ...(this.#closeResult ?? { direction: this.#direction, send_drained: false, read_terminal: "abandoned" }), cleanup_status: cleanup }));
1115
+ }
1116
+ #fail(reason) {
1117
+ if (this.#closed)
1118
+ return;
1119
+ this.#closed = true;
1120
+ this.#sealed = true;
1121
+ this.#failure = reason;
1122
+ this.#stopAssembly();
1123
+ this.#resourceObserver?.();
1124
+ this.#resourceObserver = undefined;
1125
+ this.#readAbort?.abort();
1126
+ this.#decodeAbort?.abort();
1127
+ const wait = this.#readWait;
1128
+ if (!this.#handoff) {
1129
+ this.#readWait = undefined;
1130
+ if (wait !== undefined) {
1131
+ wait.signal?.removeEventListener("abort", wait.canceled);
1132
+ wait.claim?.release();
1133
+ wait.claim = undefined;
1134
+ const error = new V4MessageStreamError(reason, undefined, this.#applicationInputDelivered, this.#codecIdentity());
1135
+ wait.diagnosticOutcome = error;
1136
+ this.#finishReadDiagnostic(wait);
1137
+ wait.reject(error);
1138
+ }
1139
+ }
1140
+ for (const entry of this.#actualSends) {
1141
+ entry.revoked = true;
1142
+ entry.reason = reason;
1143
+ entry.abort.abort();
1144
+ entry.request?.cancel();
1145
+ this.#settleSend(entry, reason);
1146
+ if (!entry.running)
1147
+ this.#releaseSend(entry);
1148
+ }
1149
+ this.#sends.length = 0;
1150
+ if (this.#finTimer !== undefined)
1151
+ clearTimeout(this.#finTimer);
1152
+ this.#finTimer = undefined;
1153
+ this.#finReject?.(new V4MessageStreamError(reason));
1154
+ this.#finReject = this.#finResolve = undefined;
1155
+ if (!this.#readRunning) {
1156
+ this.#owner?.releaseReader();
1157
+ this.#clearBody();
1158
+ }
1159
+ if (this.#owner !== undefined) {
1160
+ try {
1161
+ this.#cleanupDeadline = this.#owner.deadline(this.#options.cleanupTimeoutMS);
1162
+ }
1163
+ catch {
1164
+ this.#cleanupIncomplete = true;
1165
+ }
1166
+ this.#closeRunning = true;
1167
+ this.#closeTail = this.#owner.reset().then(value => { this.#closeResult = value; }, () => undefined).finally(() => { this.#closeRunning = false; this.#cleanup(); });
1168
+ const expire = () => {
1169
+ try {
1170
+ this.#cleanupDeadline.check();
1171
+ this.#cleanupTimer = setTimeout(expire, timerChunk(this.#cleanupDeadline.remainingMS()));
1172
+ }
1173
+ catch {
1174
+ this.#cleanupTimer = undefined;
1175
+ this.#cleanupIncomplete = true;
1176
+ this.#notify();
1177
+ }
1178
+ };
1179
+ if (this.#cleanupDeadline !== undefined)
1180
+ expire();
1181
+ }
1182
+ this.#cleanup();
1183
+ }
1184
+ #cleanup() {
1185
+ if (this.#owner === undefined || this.#cleaning)
1186
+ return;
1187
+ this.#cleaning = true;
1188
+ try {
1189
+ this.#collect();
1190
+ }
1191
+ finally {
1192
+ this.#cleaning = false;
1193
+ }
1194
+ }
1195
+ #collect() {
1196
+ if (this.#owner === undefined)
1197
+ return;
1198
+ if (this.#ioEnded && !this.#ioDetached && (!this.#readRunning || this.#decoding) && !this.#closeRunning &&
1199
+ [...this.#actualSends].every(entry => entry.preparing) && this.#owner.cleanupStatus().status === "complete") {
1200
+ this.#ioDetached = true;
1201
+ this.#owner.detachIO();
1202
+ }
1203
+ if (this.#ioDetached && this.#applicationInputDelivered)
1204
+ this.#owner.releaseDelivery();
1205
+ if (this.#ioEnded && this.#length === undefined && !this.#handoff)
1206
+ this.#closed = true;
1207
+ const natural = this.#eof && this.#finResult !== undefined && this.#sends.length === 0;
1208
+ if ((!this.#closed && !natural) || this.#handoff || this.#readRunning || this.#publishing || this.#closeRunning || this.#actualSends.size !== 0 || this.#waits !== 0 || this.#owner.cleanupStatus().status !== "complete") {
1209
+ this.#notify();
1210
+ return;
1211
+ }
1212
+ this.#closed = true;
1213
+ this.#sealed = true;
1214
+ this.#resourceObserver?.();
1215
+ this.#resourceObserver = undefined;
1216
+ if (this.#cleanupTimer !== undefined)
1217
+ clearTimeout(this.#cleanupTimer);
1218
+ this.#cleanupTimer = undefined;
1219
+ const owner = this.#owner;
1220
+ this.#owner = undefined;
1221
+ owner.release();
1222
+ this.#definition = undefined;
1223
+ this.#prepared = undefined;
1224
+ this.#inbound = this.#outbound = undefined;
1225
+ this.#cleanupDeadline = this.#finDeadline = undefined;
1226
+ this.#notify();
1227
+ }
1228
+ #notify() { for (const wake of this.#observers)
1229
+ wake(); }
1230
+ cleanupStatus() {
1231
+ if (this.#owner === undefined)
1232
+ return complete;
1233
+ return result({ status: this.#cleanupIncomplete ? "cleanup_incomplete" : "pending", core_cleanup: this.#owner.cleanupStatus().core_cleanup,
1234
+ pending_callbacks: BigInt(this.#actualSends.size + Number(this.#readRunning) + Number(this.#publishing) + Number(this.#closeRunning)) });
1235
+ }
1236
+ #wait(operation, options) {
1237
+ if (options?.signal?.aborted)
1238
+ return Promise.reject(new V4MessageStreamError("canceled"));
1239
+ if (this.#owner === undefined)
1240
+ return operation;
1241
+ if (this.#waits >= 32)
1242
+ return Promise.reject(new V4MessageStreamError("resource_exhausted"));
1243
+ let reference;
1244
+ try {
1245
+ reference = this.#owner.reserve("v4_message_wait", new ResourceVector([this.#owner.runtimeBytes + 128n, 0n, 0n, 1n, 0n, 1n, 0n, 0n, 0n, 0n, 0n]));
1246
+ }
1247
+ catch {
1248
+ return Promise.reject(new V4MessageStreamError("resource_exhausted"));
1249
+ }
1250
+ this.#waits++;
1251
+ return new NativePromise((resolve, reject) => {
1252
+ let done = false;
1253
+ const finish = () => { if (done)
1254
+ return false; done = true; options?.signal?.removeEventListener("abort", cancel); return true; };
1255
+ const release = () => { this.#waits--; reference.release(); this.#cleanup(); };
1256
+ const cancel = () => { if (finish())
1257
+ reject(new V4MessageStreamError("canceled")); };
1258
+ options?.signal?.addEventListener("abort", cancel, { once: true });
1259
+ // An uncancelable native Promise reaction remains a real retained tail.
1260
+ // Cancel detaches only the public wait; its slot is refunded on reaction exit.
1261
+ void operation.then(value => { try {
1262
+ if (finish())
1263
+ resolve(value);
1264
+ }
1265
+ finally {
1266
+ release();
1267
+ } }, () => {
1268
+ try {
1269
+ if (finish())
1270
+ reject(new V4MessageStreamError(this.#failure ?? "write_failed"));
1271
+ }
1272
+ finally {
1273
+ release();
1274
+ }
1275
+ });
1276
+ });
1277
+ }
1278
+ waitCleanup(options) {
1279
+ try {
1280
+ if (applicationDependsOn(options?.context, this.#decodeContext) || [...this.#actualSends].some(entry => applicationDependsOn(options?.context, entry.applicationContext))) {
1281
+ return Promise.reject(new V4MessageStreamError("dependency_unavailable", undefined, this.#applicationInputDelivered));
1282
+ }
1283
+ }
1284
+ catch {
1285
+ return Promise.reject(new V4MessageStreamError("dependency_unavailable", undefined, this.#applicationInputDelivered));
1286
+ }
1287
+ const initial = this.cleanupStatus();
1288
+ if (initial.status !== "pending")
1289
+ return Promise.resolve(initial);
1290
+ if (options?.signal?.aborted)
1291
+ return Promise.reject(new V4MessageStreamError("canceled"));
1292
+ if (this.#cleanupWaits >= 32)
1293
+ return Promise.reject(new V4MessageStreamError("resource_exhausted"));
1294
+ const owner = this.#owner;
1295
+ let deadline, reference;
1296
+ try {
1297
+ deadline = owner.deadline(this.#options.cleanupTimeoutMS);
1298
+ reference = owner.reserve("v4_message_cleanup_wait", new ResourceVector([owner.runtimeBytes + 256n, 0n, 0n, 1n, 0n, 1n, 1n, 0n, 0n, 0n, 0n]));
1299
+ }
1300
+ catch {
1301
+ return Promise.reject(new V4MessageStreamError("resource_exhausted"));
1302
+ }
1303
+ this.#cleanupWaits++;
1304
+ return new NativePromise((resolve, reject) => {
1305
+ let timer;
1306
+ let done = false;
1307
+ const finish = (canceled = false) => {
1308
+ if (done)
1309
+ return;
1310
+ done = true;
1311
+ if (timer !== undefined)
1312
+ clearTimeout(timer);
1313
+ this.#observers.delete(wake);
1314
+ options?.signal?.removeEventListener("abort", cancel);
1315
+ this.#cleanupWaits--;
1316
+ reference.release();
1317
+ if (canceled)
1318
+ reject(new V4MessageStreamError("canceled"));
1319
+ else
1320
+ resolve(this.cleanupStatus());
1321
+ };
1322
+ const cancel = () => finish(true);
1323
+ const wake = () => { if (this.cleanupStatus().status !== "pending")
1324
+ finish(); };
1325
+ const tick = () => { try {
1326
+ deadline.check();
1327
+ timer = setTimeout(tick, timerChunk(deadline.remainingMS()));
1328
+ }
1329
+ catch {
1330
+ finish();
1331
+ } };
1332
+ this.#observers.add(wake);
1333
+ options?.signal?.addEventListener("abort", cancel, { once: true });
1334
+ if (options?.signal?.aborted)
1335
+ cancel();
1336
+ else {
1337
+ wake();
1338
+ if (!done)
1339
+ tick();
1340
+ }
1341
+ });
1342
+ }
1343
+ }
1344
+ /** Low-level conversion requires an untouched accepted Stream whose OPEN
1345
+ * already contains the exact definition binding. */
1346
+ export function asTypedMessages(stream, definition, options = {}) {
1347
+ messageDefinition(definition);
1348
+ const fixed = captureMessageOptions(options), owner = acquireMessageStreamAdapter(stream, messageAdapterProfile(fixed));
1349
+ try {
1350
+ const candidate = prepareTypedMessages(definition, fixed);
1351
+ candidate.bind(token, owner);
1352
+ return candidate;
1353
+ }
1354
+ catch (error) {
1355
+ owner.rollback();
1356
+ throw error;
1357
+ }
1358
+ }
1359
+ /** Internal candidate construction occurs before OPEN and application delivery. */
1360
+ export function prepareTypedMessages(definition, options) {
1361
+ return new V4TypedMessageStream(token, definition, options);
1362
+ }
1363
+ export function prepareMessageBinding(candidate, context) {
1364
+ candidate.prepare(token, context);
1365
+ }
1366
+ export function bindTypedMessages(candidate, stream, options, prepaid, preaccepted = false) {
1367
+ const owner = acquireMessageStreamAdapter(stream, { ...messageAdapterProfile(captureMessageOptions(options), prepaid), preaccepted });
1368
+ try {
1369
+ candidate.bind(token, owner);
1370
+ }
1371
+ catch (error) {
1372
+ owner.rollback();
1373
+ throw error;
1374
+ }
1375
+ }