space-data-module-sdk 0.8.4 → 0.8.6

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 (354) hide show
  1. package/README.md +117 -20
  2. package/bin/space-data-module.js +263 -0
  3. package/docs/.nojekyll +1 -0
  4. package/docs/AGENTS.md +34 -0
  5. package/docs/architecture.svg +141 -0
  6. package/docs/browser-wasmedge-isomorphic.md +179 -0
  7. package/docs/flatsql-streaming-standard.md +341 -0
  8. package/docs/gpu-module-abi.md +193 -0
  9. package/docs/index.html +125 -0
  10. package/docs/isomorphic-pthreads.md +188 -0
  11. package/docs/isomorphic-sdn-runtime-plan.md +167 -0
  12. package/docs/language-runtime-matrix.md +82 -0
  13. package/docs/manifest-structure.svg +114 -0
  14. package/docs/module-bundle-runtime-plan.md +19 -0
  15. package/docs/module-lifecycle.svg +70 -0
  16. package/docs/module-publication-standard.md +540 -0
  17. package/docs/node-red-default-node-parity.md +88 -0
  18. package/docs/protocol-installation.md +196 -0
  19. package/docs/secrets-capability.md +152 -0
  20. package/docs/styles.css +288 -0
  21. package/docs/testing-harness.md +408 -0
  22. package/docs/tri-runtime-parity.md +132 -0
  23. package/docs/wallet-callback.html +2 -0
  24. package/package.json +25 -6
  25. package/schemas/HttpRequestAbi.fbs +41 -0
  26. package/schemas/HttpResponseAbi.fbs +36 -0
  27. package/schemas/PluginManifest.fbs +2 -1
  28. package/src/AGENTS.md +1 -1
  29. package/src/app/codec.js +548 -0
  30. package/src/app/index.js +6 -0
  31. package/src/browser.js +5 -0
  32. package/src/bundle/constants.js +1 -0
  33. package/src/bundle/index.js +15 -0
  34. package/src/bundle/sigdomain.js +179 -0
  35. package/src/bundle/signing.js +767 -0
  36. package/src/bundle/wasm.js +3 -2
  37. package/src/capabilities.js +3 -0
  38. package/src/compat/index.js +7 -0
  39. package/src/compat/sdnLegacy.js +449 -0
  40. package/src/compiler/compileModule.js +556 -55
  41. package/src/compiler/flatcSupport.js +41 -27
  42. package/src/compiler/index.d.ts +2 -0
  43. package/src/compiler/index.js +18 -0
  44. package/src/compiler/invokeGlue.js +1212 -183
  45. package/src/compiler/pthreadArtifactGuard.js +910 -0
  46. package/src/compiler/sdnShimGenerator.js +323 -0
  47. package/src/compiler/wasiThreadsToolchain.js +150 -0
  48. package/src/compliance/index.js +11 -1
  49. package/src/compliance/legacyWildcardPorts.js +228 -0
  50. package/src/compliance/pluginCompliance.js +1000 -96
  51. package/src/field-stream/index.d.ts +31 -0
  52. package/src/field-stream/index.js +119 -0
  53. package/src/flow/dependencyStreamBridge.js +234 -0
  54. package/src/flow/flatsqlLinkShim.js +304 -0
  55. package/src/flow/flowCodec.js +107 -0
  56. package/src/flow/flowCompiler.js +2900 -0
  57. package/src/flow/flowRuntimeHost.js +877 -0
  58. package/src/flow/index.d.ts +117 -0
  59. package/src/flow/index.js +31 -0
  60. package/src/flow/isomorphicFlowHost.js +384 -0
  61. package/src/flow/jsFlowRuntime.js +275 -0
  62. package/src/flow/normalize.js +294 -0
  63. package/src/flow/runtime-src/flow_runtime.cpp +1699 -0
  64. package/src/flow/vendor/sdn-flow/MethodRegistry.js +400 -0
  65. package/src/flow/vendor/sdn-flow/constants.js +39 -0
  66. package/src/flow/vendor/sdn-flow/index.js +29 -0
  67. package/src/flow/vendor/sdn-flow/normalize.js +506 -0
  68. package/src/flow/vendor/sdn-flow/wasmCompatibility.js +189 -0
  69. package/src/generated/http/cpp/HttpRequestAbi_generated.h +285 -0
  70. package/src/generated/http/cpp/HttpResponseAbi_generated.h +192 -0
  71. package/src/generated/http/sdn/http/http-header.js +53 -0
  72. package/src/generated/http/sdn/http/http-header.ts +67 -0
  73. package/src/generated/http/sdn/http/http-request.js +132 -0
  74. package/src/generated/http/sdn/http/http-request.ts +179 -0
  75. package/src/generated/http/sdn/http/http-response.js +144 -0
  76. package/src/generated/http/sdn/http/http-response.ts +168 -0
  77. package/src/generated/http/sdn/http.js +11 -0
  78. package/src/generated/http/sdn/http.ts +4 -0
  79. package/src/generated/orbpro/flow/backpressure-policy.d.ts +12 -0
  80. package/src/generated/orbpro/flow/backpressure-policy.d.ts.map +1 -0
  81. package/src/generated/orbpro/flow/backpressure-policy.js +17 -0
  82. package/src/generated/orbpro/flow/backpressure-policy.js.map +1 -0
  83. package/src/generated/orbpro/flow/backpressure-policy.ts +15 -0
  84. package/src/generated/orbpro/flow/flow-bundle-manifest.d.ts +142 -0
  85. package/src/generated/orbpro/flow/flow-bundle-manifest.d.ts.map +1 -0
  86. package/src/generated/orbpro/flow/flow-bundle-manifest.js +304 -0
  87. package/src/generated/orbpro/flow/flow-bundle-manifest.js.map +1 -0
  88. package/src/generated/orbpro/flow/flow-bundle-manifest.ts +399 -0
  89. package/src/generated/orbpro/flow/flow-edge.d.ts +113 -0
  90. package/src/generated/orbpro/flow/flow-edge.d.ts.map +1 -0
  91. package/src/generated/orbpro/flow/flow-edge.js +221 -0
  92. package/src/generated/orbpro/flow/flow-edge.js.map +1 -0
  93. package/src/generated/orbpro/flow/flow-edge.ts +292 -0
  94. package/src/generated/orbpro/flow/flow-node.d.ts +80 -0
  95. package/src/generated/orbpro/flow/flow-node.d.ts.map +1 -0
  96. package/src/generated/orbpro/flow/flow-node.js +158 -0
  97. package/src/generated/orbpro/flow/flow-node.js.map +1 -0
  98. package/src/generated/orbpro/flow/flow-node.ts +202 -0
  99. package/src/generated/orbpro/flow/flow-program.d.ts +164 -0
  100. package/src/generated/orbpro/flow/flow-program.d.ts.map +1 -0
  101. package/src/generated/orbpro/flow/flow-program.js +350 -0
  102. package/src/generated/orbpro/flow/flow-program.js.map +1 -0
  103. package/src/generated/orbpro/flow/flow-program.ts +477 -0
  104. package/src/generated/orbpro/flow/flow-trigger.d.ts +108 -0
  105. package/src/generated/orbpro/flow/flow-trigger.d.ts.map +1 -0
  106. package/src/generated/orbpro/flow/flow-trigger.js +207 -0
  107. package/src/generated/orbpro/flow/flow-trigger.js.map +1 -0
  108. package/src/generated/orbpro/flow/flow-trigger.ts +276 -0
  109. package/src/generated/orbpro/flow/node-kind.d.ts +13 -0
  110. package/src/generated/orbpro/flow/node-kind.d.ts.map +1 -0
  111. package/src/generated/orbpro/flow/node-kind.js +16 -0
  112. package/src/generated/orbpro/flow/node-kind.js.map +1 -0
  113. package/src/generated/orbpro/{invoke.ts → flow/node-kind.ts} +12 -2
  114. package/src/generated/orbpro/flow/plugin-version-binding.d.ts +52 -0
  115. package/src/generated/orbpro/flow/plugin-version-binding.d.ts.map +1 -0
  116. package/src/generated/orbpro/flow/plugin-version-binding.js +85 -0
  117. package/src/generated/orbpro/flow/plugin-version-binding.js.map +1 -0
  118. package/src/generated/orbpro/flow/plugin-version-binding.ts +121 -0
  119. package/src/generated/orbpro/flow/schema-hash-binding.d.ts +70 -0
  120. package/src/generated/orbpro/flow/schema-hash-binding.d.ts.map +1 -0
  121. package/src/generated/orbpro/flow/schema-hash-binding.js +143 -0
  122. package/src/generated/orbpro/flow/schema-hash-binding.js.map +1 -0
  123. package/src/generated/orbpro/flow/schema-hash-binding.ts +189 -0
  124. package/src/generated/orbpro/flow/trigger-binding.d.ts +71 -0
  125. package/src/generated/orbpro/flow/trigger-binding.d.ts.map +1 -0
  126. package/src/generated/orbpro/flow/trigger-binding.js +142 -0
  127. package/src/generated/orbpro/flow/trigger-binding.js.map +1 -0
  128. package/src/generated/orbpro/flow/trigger-binding.ts +181 -0
  129. package/src/generated/orbpro/flow/trigger-kind.d.ts +14 -0
  130. package/src/generated/orbpro/flow/trigger-kind.d.ts.map +1 -0
  131. package/src/generated/orbpro/flow/trigger-kind.js +17 -0
  132. package/src/generated/orbpro/flow/trigger-kind.js.map +1 -0
  133. package/src/generated/orbpro/flow/trigger-kind.ts +17 -0
  134. package/src/generated/orbpro/flow.d.ts +21 -0
  135. package/src/generated/orbpro/flow.d.ts.map +1 -0
  136. package/src/generated/orbpro/flow.js +23 -0
  137. package/src/generated/orbpro/flow.js.map +1 -0
  138. package/src/generated/orbpro/flow.ts +24 -0
  139. package/src/generated/orbpro/manifest/capability-kind.d.ts +2 -1
  140. package/src/generated/orbpro/manifest/capability-kind.js +1 -0
  141. package/src/generated/orbpro/manifest/capability-kind.ts +1 -0
  142. package/src/generated/orbpro/plugin/stream-invoke-request.d.ts +93 -0
  143. package/src/generated/orbpro/plugin/stream-invoke-request.js +160 -0
  144. package/src/generated/orbpro/plugin/stream-invoke-request.ts +211 -0
  145. package/src/generated/orbpro/plugin/stream-invoke-response.d.ts +99 -0
  146. package/src/generated/orbpro/plugin/stream-invoke-response.js +175 -0
  147. package/src/generated/orbpro/plugin/stream-invoke-response.ts +226 -0
  148. package/src/generated/orbpro/plugin.js +8 -0
  149. package/src/generated/orbpro/stream.d.ts +12 -0
  150. package/src/generated/orbpro/stream.js +14 -0
  151. package/src/generated/orbpro/stream.ts +15 -0
  152. package/src/generated/spacedatastandards/plg/EntryFunction.d.ts +52 -0
  153. package/src/generated/spacedatastandards/plg/EntryFunction.d.ts.map +1 -0
  154. package/src/generated/spacedatastandards/plg/EntryFunction.js +106 -0
  155. package/src/generated/spacedatastandards/plg/EntryFunction.ts +162 -0
  156. package/src/generated/spacedatastandards/plg/FlatBufferTypeRef.d.ts +90 -0
  157. package/src/generated/spacedatastandards/plg/FlatBufferTypeRef.d.ts.map +1 -0
  158. package/src/generated/spacedatastandards/plg/FlatBufferTypeRef.js +189 -0
  159. package/src/generated/spacedatastandards/plg/FlatBufferTypeRef.ts +270 -0
  160. package/src/generated/spacedatastandards/plg/PLG.d.ts +492 -0
  161. package/src/generated/spacedatastandards/plg/PLG.d.ts.map +1 -0
  162. package/src/generated/spacedatastandards/plg/PLG.js +1139 -0
  163. package/src/generated/spacedatastandards/plg/PLG.ts +1600 -0
  164. package/src/generated/spacedatastandards/plg/PLGAcceptedTypeSet.d.ts +63 -0
  165. package/src/generated/spacedatastandards/plg/PLGAcceptedTypeSet.d.ts.map +1 -0
  166. package/src/generated/spacedatastandards/plg/PLGAcceptedTypeSet.js +138 -0
  167. package/src/generated/spacedatastandards/plg/PLGAcceptedTypeSet.ts +189 -0
  168. package/src/generated/spacedatastandards/plg/PLGBuildArtifact.d.ts +41 -0
  169. package/src/generated/spacedatastandards/plg/PLGBuildArtifact.d.ts.map +1 -0
  170. package/src/generated/spacedatastandards/plg/PLGBuildArtifact.js +104 -0
  171. package/src/generated/spacedatastandards/plg/PLGBuildArtifact.ts +151 -0
  172. package/src/generated/spacedatastandards/plg/PLGFlowEdge.d.ts +68 -0
  173. package/src/generated/spacedatastandards/plg/PLGFlowEdge.d.ts.map +1 -0
  174. package/src/generated/spacedatastandards/plg/PLGFlowEdge.js +124 -0
  175. package/src/generated/spacedatastandards/plg/PLGFlowEdge.ts +184 -0
  176. package/src/generated/spacedatastandards/plg/PLGFlowEdgeContract.d.ts +58 -0
  177. package/src/generated/spacedatastandards/plg/PLGFlowEdgeContract.d.ts.map +1 -0
  178. package/src/generated/spacedatastandards/plg/PLGFlowEdgeContract.js +126 -0
  179. package/src/generated/spacedatastandards/plg/PLGFlowEdgeContract.ts +161 -0
  180. package/src/generated/spacedatastandards/plg/PLGFlowNode.d.ts +82 -0
  181. package/src/generated/spacedatastandards/plg/PLGFlowNode.d.ts.map +1 -0
  182. package/src/generated/spacedatastandards/plg/PLGFlowNode.js +164 -0
  183. package/src/generated/spacedatastandards/plg/PLGFlowNode.ts +244 -0
  184. package/src/generated/spacedatastandards/plg/PLGFlowTrigger.d.ts +55 -0
  185. package/src/generated/spacedatastandards/plg/PLGFlowTrigger.d.ts.map +1 -0
  186. package/src/generated/spacedatastandards/plg/PLGFlowTrigger.js +105 -0
  187. package/src/generated/spacedatastandards/plg/PLGFlowTrigger.ts +162 -0
  188. package/src/generated/spacedatastandards/plg/PLGFlowTriggerBinding.d.ts +42 -0
  189. package/src/generated/spacedatastandards/plg/PLGFlowTriggerBinding.d.ts.map +1 -0
  190. package/src/generated/spacedatastandards/plg/PLGFlowTriggerBinding.js +83 -0
  191. package/src/generated/spacedatastandards/plg/PLGFlowTriggerBinding.ts +127 -0
  192. package/src/generated/spacedatastandards/plg/PLGHostCapability.d.ts +36 -0
  193. package/src/generated/spacedatastandards/plg/PLGHostCapability.d.ts.map +1 -0
  194. package/src/generated/spacedatastandards/plg/PLGHostCapability.js +90 -0
  195. package/src/generated/spacedatastandards/plg/PLGHostCapability.ts +127 -0
  196. package/src/generated/spacedatastandards/plg/PLGMethodManifest.d.ts +53 -0
  197. package/src/generated/spacedatastandards/plg/PLGMethodManifest.d.ts.map +1 -0
  198. package/src/generated/spacedatastandards/plg/PLGMethodManifest.js +153 -0
  199. package/src/generated/spacedatastandards/plg/PLGMethodManifest.ts +210 -0
  200. package/src/generated/spacedatastandards/plg/PLGPortManifest.d.ts +70 -0
  201. package/src/generated/spacedatastandards/plg/PLGPortManifest.d.ts.map +1 -0
  202. package/src/generated/spacedatastandards/plg/PLGPortManifest.js +149 -0
  203. package/src/generated/spacedatastandards/plg/PLGPortManifest.ts +212 -0
  204. package/src/generated/spacedatastandards/plg/PLGProtocolSpec.d.ts +73 -0
  205. package/src/generated/spacedatastandards/plg/PLGProtocolSpec.d.ts.map +1 -0
  206. package/src/generated/spacedatastandards/plg/PLGProtocolSpec.js +199 -0
  207. package/src/generated/spacedatastandards/plg/PLGProtocolSpec.ts +292 -0
  208. package/src/generated/spacedatastandards/plg/PLGTimerSpec.d.ts +40 -0
  209. package/src/generated/spacedatastandards/plg/PLGTimerSpec.d.ts.map +1 -0
  210. package/src/generated/spacedatastandards/plg/PLGTimerSpec.js +103 -0
  211. package/src/generated/spacedatastandards/plg/PLGTimerSpec.ts +148 -0
  212. package/src/generated/spacedatastandards/plg/PluginCapability.d.ts +41 -0
  213. package/src/generated/spacedatastandards/plg/PluginCapability.d.ts.map +1 -0
  214. package/src/generated/spacedatastandards/plg/PluginCapability.js +82 -0
  215. package/src/generated/spacedatastandards/plg/{plugin-capability.ts → PluginCapability.ts} +55 -18
  216. package/src/generated/spacedatastandards/plg/PluginDependency.d.ts +42 -0
  217. package/src/generated/spacedatastandards/plg/PluginDependency.d.ts.map +1 -0
  218. package/src/generated/spacedatastandards/plg/PluginDependency.js +80 -0
  219. package/src/generated/spacedatastandards/plg/PluginDependency.ts +124 -0
  220. package/src/generated/spacedatastandards/plg/TAB.d.ts +85 -0
  221. package/src/generated/spacedatastandards/plg/TAB.d.ts.map +1 -0
  222. package/src/generated/spacedatastandards/plg/TAB.js +173 -0
  223. package/src/generated/spacedatastandards/plg/TAB.ts +228 -0
  224. package/src/generated/spacedatastandards/plg/bufferMutability.d.ts +18 -0
  225. package/src/generated/spacedatastandards/plg/bufferMutability.d.ts.map +1 -0
  226. package/src/generated/spacedatastandards/plg/bufferMutability.js +20 -0
  227. package/src/generated/spacedatastandards/plg/bufferMutability.ts +23 -0
  228. package/src/generated/spacedatastandards/plg/bufferOwnership.d.ts +18 -0
  229. package/src/generated/spacedatastandards/plg/bufferOwnership.d.ts.map +1 -0
  230. package/src/generated/spacedatastandards/plg/bufferOwnership.js +20 -0
  231. package/src/generated/spacedatastandards/plg/bufferOwnership.ts +23 -0
  232. package/src/generated/spacedatastandards/plg/drainBehavior.d.ts +20 -0
  233. package/src/generated/spacedatastandards/plg/drainBehavior.d.ts.map +1 -0
  234. package/src/generated/spacedatastandards/plg/drainBehavior.js +22 -0
  235. package/src/generated/spacedatastandards/plg/drainBehavior.ts +25 -0
  236. package/src/generated/spacedatastandards/plg/flowEdgeRoutePolicy.d.ts +10 -0
  237. package/src/generated/spacedatastandards/plg/flowEdgeRoutePolicy.d.ts.map +1 -0
  238. package/src/generated/spacedatastandards/plg/flowEdgeRoutePolicy.js +12 -0
  239. package/src/generated/spacedatastandards/plg/flowEdgeRoutePolicy.ts +13 -0
  240. package/src/generated/spacedatastandards/plg/hostCapabilityKind.d.ts +51 -0
  241. package/src/generated/spacedatastandards/plg/hostCapabilityKind.d.ts.map +1 -0
  242. package/src/generated/spacedatastandards/plg/hostCapabilityKind.js +53 -0
  243. package/src/generated/spacedatastandards/plg/hostCapabilityKind.ts +55 -0
  244. package/src/generated/spacedatastandards/plg/invokeSurfaceKind.d.ts +15 -0
  245. package/src/generated/spacedatastandards/plg/invokeSurfaceKind.d.ts.map +1 -0
  246. package/src/generated/spacedatastandards/plg/invokeSurfaceKind.js +17 -0
  247. package/src/generated/spacedatastandards/plg/invokeSurfaceKind.ts +19 -0
  248. package/src/generated/spacedatastandards/plg/main.d.ts +29 -0
  249. package/src/generated/spacedatastandards/plg/main.d.ts.map +1 -0
  250. package/src/generated/spacedatastandards/plg/main.js +30 -0
  251. package/src/generated/spacedatastandards/plg/main.ts +32 -0
  252. package/src/generated/spacedatastandards/plg/payloadWireFormat.d.ts +17 -0
  253. package/src/generated/spacedatastandards/plg/payloadWireFormat.d.ts.map +1 -0
  254. package/src/generated/spacedatastandards/plg/payloadWireFormat.js +19 -0
  255. package/src/generated/spacedatastandards/plg/payloadWireFormat.ts +21 -0
  256. package/src/generated/spacedatastandards/plg/pluginCategory.d.ts +82 -0
  257. package/src/generated/spacedatastandards/plg/pluginCategory.d.ts.map +1 -0
  258. package/src/generated/spacedatastandards/plg/pluginCategory.js +84 -0
  259. package/src/generated/spacedatastandards/plg/pluginCategory.ts +103 -0
  260. package/src/generated/spacedatastandards/plg/publicationState.d.ts +18 -0
  261. package/src/generated/spacedatastandards/plg/publicationState.d.ts.map +1 -0
  262. package/src/generated/spacedatastandards/plg/publicationState.js +20 -0
  263. package/src/generated/spacedatastandards/plg/purchaseTier.d.ts +18 -0
  264. package/src/generated/spacedatastandards/plg/purchaseTier.d.ts.map +1 -0
  265. package/src/generated/spacedatastandards/plg/purchaseTier.js +20 -0
  266. package/src/host/AGENTS.md +2 -2
  267. package/src/host/abi.js +53 -66
  268. package/src/host/browserHost.js +355 -8
  269. package/src/host/cpp/keyslotClient.hpp +231 -0
  270. package/src/host/cpp/secretsClient.hpp +583 -0
  271. package/src/host/hostcallWire.js +183 -0
  272. package/src/host/index.js +2 -0
  273. package/src/host/isomorphicLoader.js +44 -7
  274. package/src/host/nodeHost.js +227 -8
  275. package/src/host/sabHostcallChannel.js +206 -0
  276. package/src/host/timerDriver.js +203 -0
  277. package/src/host/wasiShim.js +41 -1
  278. package/src/host/wasiThreadBrowserWorker.mjs +48 -0
  279. package/src/host/wasiThreadHost.js +393 -0
  280. package/src/host/wasiThreadWorker.mjs +32 -0
  281. package/src/host/wasiThreadWorkerRuntime.js +93 -0
  282. package/src/http/index.js +339 -0
  283. package/src/index.d.ts +725 -11
  284. package/src/index.js +4 -0
  285. package/src/invoke/codec.js +1202 -128
  286. package/src/invoke/index.js +5 -1
  287. package/src/licensing/index.d.ts +2 -0
  288. package/src/licensing/index.js +2 -0
  289. package/src/licensing/records.js +287 -8
  290. package/src/manifest/codec.js +62 -14
  291. package/src/manifest/index.js +2 -0
  292. package/src/manifest/legacyToPlg.js +164 -25
  293. package/src/manifest/normalize.js +1 -0
  294. package/src/manifest/plgCodec.js +1303 -62
  295. package/src/manifest/typeRefs.js +162 -24
  296. package/src/runtime/compiledRuntimeAbi.js +47 -0
  297. package/src/runtime/compiledRuntimeAbi.json +33 -0
  298. package/src/runtime/index.d.ts +32 -0
  299. package/src/runtime/index.js +1 -0
  300. package/src/runtime-host/index.js +3 -0
  301. package/src/runtime-host/methodRegistry.js +55 -0
  302. package/src/standards/index.js +200 -56
  303. package/src/standards/sharedCatalog.js +16 -0
  304. package/src/testing/browserModuleHarness.js +604 -31
  305. package/src/testing/docker/wasmedge-parity.Dockerfile +39 -0
  306. package/src/testing/index.d.ts +151 -0
  307. package/src/testing/index.js +21 -0
  308. package/src/testing/native/wasmedge_emscripten_pthread_runner.c +25 -1
  309. package/src/testing/parityBrowserRunner.js +138 -0
  310. package/src/testing/parityHarness.js +616 -0
  311. package/src/testing/parityLanes.js +537 -0
  312. package/src/testing/processInvoke.js +29 -21
  313. package/src/testing/streamInvokeCodec.js +10 -175
  314. package/src/testing/wasmedgePin.json +6 -0
  315. package/src/testing/workerModuleHarness.js +242 -0
  316. package/src/testing/workerModuleHarnessWorker.js +176 -0
  317. package/src/transport/index.js +3 -0
  318. package/src/transport/pki.js +352 -14
  319. package/src/transport/records.js +16 -7
  320. package/src/utils/ecdsaDer.js +106 -0
  321. package/src/utils/wasmCrypto.js +0 -43
  322. package/templates/gpu-module/README.md +48 -0
  323. package/templates/gpu-module/include/space_data_gpu_abi.h +122 -0
  324. package/templates/gpu-module/manifest.json +84 -0
  325. package/schemas/PluginInvokeRequest.fbs +0 -18
  326. package/schemas/PluginInvokeResponse.fbs +0 -30
  327. package/schemas/spacedatastandards/PLG.fbs +0 -191
  328. package/src/generated/orbpro/invoke/plugin-invoke-request.d.ts +0 -51
  329. package/src/generated/orbpro/invoke/plugin-invoke-request.d.ts.map +0 -1
  330. package/src/generated/orbpro/invoke/plugin-invoke-request.js +0 -131
  331. package/src/generated/orbpro/invoke/plugin-invoke-request.js.map +0 -1
  332. package/src/generated/orbpro/invoke/plugin-invoke-request.ts +0 -173
  333. package/src/generated/orbpro/invoke/plugin-invoke-response.d.ts +0 -76
  334. package/src/generated/orbpro/invoke/plugin-invoke-response.d.ts.map +0 -1
  335. package/src/generated/orbpro/invoke/plugin-invoke-response.js +0 -184
  336. package/src/generated/orbpro/invoke/plugin-invoke-response.js.map +0 -1
  337. package/src/generated/orbpro/invoke/plugin-invoke-response.ts +0 -243
  338. package/src/generated/orbpro/invoke.d.ts +0 -3
  339. package/src/generated/orbpro/invoke.js +0 -5
  340. package/src/generated/spacedatastandards/plg/entry-function.js +0 -86
  341. package/src/generated/spacedatastandards/plg/entry-function.ts +0 -119
  342. package/src/generated/spacedatastandards/plg/index.js +0 -16
  343. package/src/generated/spacedatastandards/plg/index.ts +0 -11
  344. package/src/generated/spacedatastandards/plg/plg.js +0 -657
  345. package/src/generated/spacedatastandards/plg/plg.ts +0 -924
  346. package/src/generated/spacedatastandards/plg/plugin-capability.js +0 -66
  347. package/src/generated/spacedatastandards/plg/plugin-category.js +0 -15
  348. package/src/generated/spacedatastandards/plg/plugin-category.ts +0 -53
  349. package/src/generated/spacedatastandards/plg/plugin-dependency.js +0 -63
  350. package/src/generated/spacedatastandards/plg/plugin-dependency.ts +0 -86
  351. package/src/generated/spacedatastandards/plg/publication-state.js +0 -9
  352. package/src/generated/spacedatastandards/plg/purchase-tier.js +0 -9
  353. /package/src/generated/spacedatastandards/plg/{publication-state.ts → publicationState.ts} +0 -0
  354. /package/src/generated/spacedatastandards/plg/{purchase-tier.ts → purchaseTier.ts} +0 -0
@@ -1,19 +1,173 @@
1
1
  import * as flatbuffers from "../vendor/flatbuffers/flatbuffers.js";
2
2
 
3
- import {
4
- PluginInvokeRequest,
5
- PluginInvokeRequestT,
6
- } from "../generated/orbpro/invoke/plugin-invoke-request.js";
7
- import {
8
- PluginInvokeResponse,
9
- PluginInvokeResponseT,
10
- } from "../generated/orbpro/invoke/plugin-invoke-response.js";
11
3
  import { InvokeSurface } from "../generated/orbpro/manifest/invoke-surface.js";
12
4
  import { BufferMutability } from "../generated/orbpro/stream/buffer-mutability.js";
13
5
  import { BufferOwnership } from "../generated/orbpro/stream/buffer-ownership.js";
14
- import { FlatBufferTypeRefT } from "../generated/orbpro/stream/flat-buffer-type-ref.js";
15
- import { TypedArenaBufferT } from "../generated/orbpro/stream/typed-arena-buffer.js";
6
+ import {
7
+ isPayloadSchemaHashValid,
8
+ normalizePayloadSchemaHash,
9
+ } from "../manifest/typeRefs.js";
16
10
  import { toUint8Array } from "../runtime/bufferLike.js";
11
+ import { PIV } from "spacedatastandards.org/lib/js/PIV/PIV.js";
12
+ import { PIVRequest } from "spacedatastandards.org/lib/js/PIV/PIVRequest.js";
13
+ import { PIVResponse } from "spacedatastandards.org/lib/js/PIV/PIVResponse.js";
14
+ import { TABT as SdsTABT } from "spacedatastandards.org/lib/js/PIV/TAB.js";
15
+ import { FlatBufferTypeRefT as SdsFlatBufferTypeRefT } from "spacedatastandards.org/lib/js/PIV/FlatBufferTypeRef.js";
16
+ import { bufferMutability as SdsBufferMutability } from "spacedatastandards.org/lib/js/PIV/bufferMutability.js";
17
+ import { bufferOwnership as SdsBufferOwnership } from "spacedatastandards.org/lib/js/PIV/bufferOwnership.js";
18
+ import { payloadWireFormat as SdsPayloadWireFormat } from "spacedatastandards.org/lib/js/PIV/payloadWireFormat.js";
19
+ import { pivStatus as SdsPivStatus } from "spacedatastandards.org/lib/js/PIV/pivStatus.js";
20
+
21
+ const UINT16_MAX = 0xffff;
22
+ const UINT32_MAX = 0xffffffff;
23
+ const arenaLeaseStates = new WeakMap();
24
+ const arenaTransferStates = new WeakMap();
25
+ const arenaForwardingReceipts = new WeakMap();
26
+
27
+ function firstDefined(...values) {
28
+ return values.find((value) => value !== undefined && value !== null);
29
+ }
30
+
31
+ function readBoundedInteger(value, description, options = {}) {
32
+ const {
33
+ defaultValue,
34
+ minimum = 0,
35
+ maximum = Number.MAX_SAFE_INTEGER,
36
+ } = options;
37
+ if (value === undefined || value === null || value === "") {
38
+ if (defaultValue !== undefined) {
39
+ return defaultValue;
40
+ }
41
+ throw new TypeError(`${description} is required.`);
42
+ }
43
+ let normalized;
44
+ if (typeof value === "bigint") {
45
+ if (value < BigInt(minimum) || value > BigInt(maximum)) {
46
+ throw new RangeError(
47
+ `${description} must be an integer between ${minimum} and ${maximum}.`,
48
+ );
49
+ }
50
+ normalized = Number(value);
51
+ } else {
52
+ normalized = typeof value === "number" ? value : Number(value);
53
+ }
54
+ if (
55
+ !Number.isSafeInteger(normalized) ||
56
+ normalized < minimum ||
57
+ normalized > maximum
58
+ ) {
59
+ throw new RangeError(
60
+ `${description} must be an integer between ${minimum} and ${maximum}.`,
61
+ );
62
+ }
63
+ return normalized;
64
+ }
65
+
66
+ function isPositivePowerOfTwo(value) {
67
+ return value > 0 && Number.isInteger(Math.log2(value));
68
+ }
69
+
70
+ function readAlignment(value, description, options = {}) {
71
+ const alignment = readBoundedInteger(value, description, {
72
+ defaultValue: options.defaultValue,
73
+ minimum: 0,
74
+ maximum: options.maximum ?? UINT32_MAX,
75
+ });
76
+ if (!isPositivePowerOfTwo(alignment)) {
77
+ throw new RangeError(`${description} must be a positive power of two.`);
78
+ }
79
+ return alignment;
80
+ }
81
+
82
+ function assertUint32FrameRange(offset, size, description) {
83
+ if (offset > UINT32_MAX - size) {
84
+ throw new RangeError(
85
+ `${description} offset + size exceeds the uint32 address range.`,
86
+ );
87
+ }
88
+ }
89
+
90
+ function assertViewWithinArena(view, arena, description) {
91
+ if (
92
+ view.buffer !== arena.buffer ||
93
+ view.byteOffset < arena.byteOffset ||
94
+ view.byteOffset + view.byteLength > arena.byteOffset + arena.byteLength
95
+ ) {
96
+ throw new Error(`${description} is outside its arena lease.`);
97
+ }
98
+ }
99
+
100
+ function requireArenaLeaseState(lease) {
101
+ const state = arenaLeaseStates.get(lease);
102
+ if (!state) {
103
+ throw new TypeError("SDS PIV/TAB arena lease is invalid.");
104
+ }
105
+ return state;
106
+ }
107
+
108
+ function assertLiveArenaLease(lease, generation, payload, description) {
109
+ const state = requireArenaLeaseState(lease);
110
+ if (state.closed) {
111
+ throw new Error(`${description} arena lease is closed.`);
112
+ }
113
+ if (generation !== state.generation) {
114
+ throw new Error(
115
+ `${description} has stale arena lease generation ${String(generation)}; active generation is ${state.generation}.`,
116
+ );
117
+ }
118
+ if (payload) {
119
+ assertViewWithinArena(payload, state.arena, description);
120
+ }
121
+ return state;
122
+ }
123
+
124
+ function assertFrameArenaLease(frame, description, options = {}) {
125
+ const lease = frame?.arenaLease;
126
+ if (!lease) {
127
+ if (options.required === true) {
128
+ throw new Error(`${description} requires a live arena lease.`);
129
+ }
130
+ return null;
131
+ }
132
+ const payload = toUint8Array(frame.payload);
133
+ const generation = firstDefined(frame.generation, frame.arenaGeneration);
134
+ return assertLiveArenaLease(lease, generation, payload, description);
135
+ }
136
+
137
+ function assertRetainedAliasSafe(frame, description) {
138
+ const leaseState = assertFrameArenaLease(frame, description);
139
+ if (!leaseState) {
140
+ return null;
141
+ }
142
+ const ownership = normalizeFrameOwnership(frame.ownership);
143
+ const mutability = normalizeFrameMutability(frame.mutability);
144
+ if (
145
+ ownership === BufferOwnership.PRODUCER_OWNED ||
146
+ mutability !== BufferMutability.IMMUTABLE
147
+ ) {
148
+ const payload = toUint8Array(frame.payload);
149
+ const generation = firstDefined(frame.generation, frame.arenaGeneration);
150
+ const receipt = arenaForwardingReceipts.get(frame);
151
+ if (
152
+ receipt &&
153
+ ownership === BufferOwnership.SHARED &&
154
+ receipt.lease === frame.arenaLease &&
155
+ receipt.generation === generation &&
156
+ receipt.payloadBuffer === payload?.buffer &&
157
+ receipt.payloadByteOffset === payload?.byteOffset &&
158
+ receipt.payloadByteLength === payload?.byteLength &&
159
+ receipt.frameId === encodeSdsFrameId(frame) &&
160
+ receipt.ownership === ownership &&
161
+ receipt.mutability === mutability
162
+ ) {
163
+ return leaseState;
164
+ }
165
+ throw new Error(
166
+ `${description} producer-owned or mutable cross-invocation alias requires explicit compatible transfer forwarding.`,
167
+ );
168
+ }
169
+ return leaseState;
170
+ }
17
171
 
18
172
  function toByteBuffer(data) {
19
173
  if (data instanceof flatbuffers.ByteBuffer) {
@@ -28,30 +182,6 @@ function toByteBuffer(data) {
28
182
  );
29
183
  }
30
184
 
31
- function normalizeSchemaHash(value) {
32
- if (!value) {
33
- return [];
34
- }
35
- if (value instanceof Uint8Array) {
36
- return Array.from(value);
37
- }
38
- if (ArrayBuffer.isView(value)) {
39
- return Array.from(new Uint8Array(value.buffer, value.byteOffset, value.byteLength));
40
- }
41
- if (Array.isArray(value)) {
42
- return value.map((byte) => Number(byte) & 0xff);
43
- }
44
- const normalized = String(value).trim().replace(/^0x/i, "");
45
- if (!normalized || normalized.length % 2 !== 0) {
46
- return [];
47
- }
48
- const bytes = [];
49
- for (let index = 0; index < normalized.length; index += 2) {
50
- bytes.push(parseInt(normalized.slice(index, index + 2), 16));
51
- }
52
- return bytes;
53
- }
54
-
55
185
  function normalizeUnsignedInteger(value, fallback = 0) {
56
186
  const normalized = Number(value ?? fallback);
57
187
  if (!Number.isFinite(normalized)) {
@@ -78,34 +208,266 @@ function normalizeBigInt(value, fallback = BigInt(0)) {
78
208
  }
79
209
 
80
210
  function normalizePayloadWireFormat(value) {
81
- if (value === 1 || value === "aligned-binary") {
211
+ const normalized = typeof value === "string" ? value.trim().toLowerCase() : "";
212
+ if (
213
+ value === 1 ||
214
+ normalized === "aligned-binary" ||
215
+ normalized === "aligned_binary" ||
216
+ normalized === "alignedbinary"
217
+ ) {
82
218
  return "aligned-binary";
83
219
  }
84
- return "flatbuffer";
220
+ if (
221
+ value === undefined ||
222
+ value === null ||
223
+ value === 0 ||
224
+ normalized === "" ||
225
+ normalized === "flatbuffer"
226
+ ) {
227
+ return "flatbuffer";
228
+ }
229
+ throw new TypeError(`Unsupported SDS PIV/TAB wire format: ${String(value)}.`);
85
230
  }
86
231
 
87
- function toFlatBufferTypeRefT(value = {}, payloadLength = 0) {
88
- if (value instanceof FlatBufferTypeRefT) {
89
- return value;
232
+ function toSdsWireFormat(value) {
233
+ return normalizePayloadWireFormat(value) === "aligned-binary"
234
+ ? SdsPayloadWireFormat.ALIGNED_BINARY
235
+ : SdsPayloadWireFormat.FLATBUFFER;
236
+ }
237
+
238
+ function fromSdsWireFormat(value) {
239
+ if (value === SdsPayloadWireFormat.ALIGNED_BINARY) {
240
+ return "aligned-binary";
90
241
  }
91
- const wireFormat = normalizePayloadWireFormat(value.wireFormat);
92
- const requiredAlignment = normalizeUnsignedInteger(value.requiredAlignment);
93
- const fixedStringLength = normalizeUnsignedInteger(value.fixedStringLength);
94
- const byteLength =
95
- wireFormat === "aligned-binary"
96
- ? normalizeUnsignedInteger(value.byteLength, payloadLength)
97
- : normalizeUnsignedInteger(value.byteLength);
98
- return new FlatBufferTypeRefT(
99
- value.schemaName ?? null,
100
- value.fileIdentifier ?? null,
101
- normalizeSchemaHash(value.schemaHash),
102
- value.acceptsAnyFlatbuffer === true,
103
- wireFormat,
104
- value.rootTypeName ?? null,
105
- fixedStringLength,
106
- byteLength,
107
- requiredAlignment,
242
+ if (value === SdsPayloadWireFormat.FLATBUFFER) {
243
+ return "flatbuffer";
244
+ }
245
+ throw new TypeError(`Unsupported SDS PIV/TAB wire format value: ${value}.`);
246
+ }
247
+
248
+ function toSdsMutability(value) {
249
+ if (
250
+ value === undefined ||
251
+ value === null ||
252
+ value === BufferMutability.IMMUTABLE ||
253
+ value === "immutable" ||
254
+ value === "IMMUTABLE"
255
+ ) {
256
+ return SdsBufferMutability.IMMUTABLE;
257
+ }
258
+ if (
259
+ value === BufferMutability.APPEND_ONLY ||
260
+ value === "append-only" ||
261
+ value === "APPEND_ONLY"
262
+ ) {
263
+ return SdsBufferMutability.APPEND_ONLY;
264
+ }
265
+ if (
266
+ value === BufferMutability.MUTABLE ||
267
+ value === "mutable" ||
268
+ value === "single-writer-mutable" ||
269
+ value === "SINGLE_WRITER_MUTABLE"
270
+ ) {
271
+ return SdsBufferMutability.SINGLE_WRITER_MUTABLE;
272
+ }
273
+ throw new TypeError(`Unsupported SDS PIV/TAB mutability: ${String(value)}.`);
274
+ }
275
+
276
+ function fromSdsMutability(value) {
277
+ if (value === SdsBufferMutability.APPEND_ONLY) {
278
+ return BufferMutability.APPEND_ONLY;
279
+ }
280
+ if (value === SdsBufferMutability.SINGLE_WRITER_MUTABLE) {
281
+ return BufferMutability.MUTABLE;
282
+ }
283
+ if (value === SdsBufferMutability.IMMUTABLE) {
284
+ return BufferMutability.IMMUTABLE;
285
+ }
286
+ throw new TypeError(`Unsupported SDS PIV/TAB mutability value: ${value}.`);
287
+ }
288
+
289
+ function toSdsOwnership(value) {
290
+ if (
291
+ value === undefined ||
292
+ value === null ||
293
+ value === BufferOwnership.BORROWED ||
294
+ value === BufferOwnership.HOST_OWNED ||
295
+ value === "borrowed" ||
296
+ value === "BORROWED" ||
297
+ value === "host-owned" ||
298
+ value === "host_owned" ||
299
+ value === "HOST_OWNED"
300
+ ) {
301
+ return SdsBufferOwnership.HOST_OWNED;
302
+ }
303
+ if (
304
+ value === BufferOwnership.PRODUCER_OWNED ||
305
+ value === "producer-owned" ||
306
+ value === "producer_owned" ||
307
+ value === "PRODUCER_OWNED" ||
308
+ value === "plugin-owned" ||
309
+ value === "plugin_owned" ||
310
+ value === "PLUGIN_OWNED"
311
+ ) {
312
+ return SdsBufferOwnership.PLUGIN_OWNED;
313
+ }
314
+ if (
315
+ value === BufferOwnership.SHARED ||
316
+ value === "shared" ||
317
+ value === "SHARED" ||
318
+ value === "transferred" ||
319
+ value === "TRANSFERRED"
320
+ ) {
321
+ return SdsBufferOwnership.TRANSFERRED;
322
+ }
323
+ throw new TypeError(`Unsupported SDS PIV/TAB ownership: ${String(value)}.`);
324
+ }
325
+
326
+ function fromSdsOwnership(value) {
327
+ if (value === SdsBufferOwnership.PLUGIN_OWNED) {
328
+ return BufferOwnership.PRODUCER_OWNED;
329
+ }
330
+ if (value === SdsBufferOwnership.TRANSFERRED) {
331
+ return BufferOwnership.SHARED;
332
+ }
333
+ if (value === SdsBufferOwnership.HOST_OWNED) {
334
+ return BufferOwnership.HOST_OWNED;
335
+ }
336
+ throw new TypeError(`Unsupported SDS PIV/TAB ownership value: ${value}.`);
337
+ }
338
+
339
+ function normalizeFrameMutability(value) {
340
+ return fromSdsMutability(toSdsMutability(value));
341
+ }
342
+
343
+ function normalizeFrameOwnership(value) {
344
+ return fromSdsOwnership(toSdsOwnership(value));
345
+ }
346
+
347
+ function toSdsFlatBufferTypeRefT(value = {}, wireFormat = undefined) {
348
+ const schemaHashInput = firstDefined(value.schemaHash, value.SCHEMA_HASH);
349
+ if (!isPayloadSchemaHashValid(schemaHashInput)) {
350
+ throw new TypeError("SDS PIV/TAB schemaHash must contain valid bytes or hex.");
351
+ }
352
+ const normalizedWireFormat = normalizePayloadWireFormat(
353
+ firstDefined(value.wireFormat, value.WIRE_FORMAT, wireFormat),
354
+ );
355
+ return new SdsFlatBufferTypeRefT(
356
+ value.schemaName ?? value.SCHEMA_NAME ?? null,
357
+ value.fileIdentifier ?? value.FILE_IDENTIFIER ?? null,
358
+ value.schemaVersion ?? value.SCHEMA_VERSION ?? null,
359
+ value.rootTypeName ?? value.rootType ?? value.ROOT_TYPE ?? null,
360
+ normalizePayloadSchemaHash(schemaHashInput) ?? [],
361
+ Boolean(value.acceptsAnyFlatbuffer ?? value.ACCEPTS_ANY_FLATBUFFER ?? false),
362
+ toSdsWireFormat(normalizedWireFormat),
363
+ readBoundedInteger(
364
+ firstDefined(value.fixedStringLength, value.FIXED_STRING_LENGTH),
365
+ "SDS PIV/TAB fixedStringLength",
366
+ { defaultValue: 0, maximum: UINT16_MAX },
367
+ ),
368
+ readBoundedInteger(
369
+ firstDefined(value.byteLength, value.BYTE_LENGTH),
370
+ "SDS PIV/TAB byteLength",
371
+ { defaultValue: 0, maximum: UINT32_MAX },
372
+ ),
373
+ readBoundedInteger(
374
+ firstDefined(value.requiredAlignment, value.REQUIRED_ALIGNMENT),
375
+ "SDS PIV/TAB requiredAlignment",
376
+ { defaultValue: 0, maximum: UINT16_MAX },
377
+ ),
378
+ );
379
+ }
380
+
381
+ function normalizeFrameTypeRef(frame = {}) {
382
+ const typeRefInput = frame.typeRef ?? frame.allowedType ?? {};
383
+ const frameWireFormatInput = firstDefined(frame.wireFormat, frame.WIRE_FORMAT);
384
+ const typeWireFormatInput = firstDefined(
385
+ typeRefInput.wireFormat,
386
+ typeRefInput.WIRE_FORMAT,
387
+ );
388
+ const wireFormat = normalizePayloadWireFormat(
389
+ firstDefined(typeWireFormatInput, frameWireFormatInput),
390
+ );
391
+ if (
392
+ typeWireFormatInput !== undefined &&
393
+ frameWireFormatInput !== undefined &&
394
+ normalizePayloadWireFormat(typeWireFormatInput) !==
395
+ normalizePayloadWireFormat(frameWireFormatInput)
396
+ ) {
397
+ throw new Error("SDS PIV/TAB frame and typeRef wire formats disagree.");
398
+ }
399
+ const typeRef = toSdsFlatBufferTypeRefT(typeRefInput, wireFormat);
400
+ if (wireFormat === "aligned-binary") {
401
+ typeRef.REQUIRED_ALIGNMENT = readAlignment(
402
+ typeRef.REQUIRED_ALIGNMENT,
403
+ "SDS PIV/TAB aligned type requiredAlignment",
404
+ { maximum: UINT16_MAX },
405
+ );
406
+ typeRef.BYTE_LENGTH = readBoundedInteger(
407
+ typeRef.BYTE_LENGTH,
408
+ "SDS PIV/TAB aligned type byteLength",
409
+ { minimum: 1, maximum: UINT32_MAX },
410
+ );
411
+ } else if (
412
+ typeRef.REQUIRED_ALIGNMENT > 0 &&
413
+ !isPositivePowerOfTwo(typeRef.REQUIRED_ALIGNMENT)
414
+ ) {
415
+ throw new RangeError(
416
+ "SDS PIV/TAB type requiredAlignment must be a positive power of two when declared.",
417
+ );
418
+ }
419
+ return { typeRef, wireFormat };
420
+ }
421
+
422
+ function resolveFrameAlignment(frame, requiredAlignment) {
423
+ const declared = firstDefined(frame?.alignment, frame?.ALIGNMENT);
424
+ const alignment = readAlignment(
425
+ declared,
426
+ "SDS PIV/TAB frame alignment",
427
+ {
428
+ defaultValue: Math.max(INVOKE_ARENA_ALIGNMENT, requiredAlignment || 1),
429
+ },
108
430
  );
431
+ if (alignment < requiredAlignment) {
432
+ throw new RangeError(
433
+ `SDS PIV/TAB frame alignment ${alignment} is below required alignment ${requiredAlignment}.`,
434
+ );
435
+ }
436
+ return alignment;
437
+ }
438
+
439
+ function assertAlignedFrameSize(wireFormat, typeRef, size) {
440
+ if (wireFormat !== "aligned-binary") {
441
+ return;
442
+ }
443
+ if (size !== typeRef.BYTE_LENGTH) {
444
+ throw new RangeError(
445
+ `SDS PIV/TAB aligned payload size ${size} does not match declared byteLength ${typeRef.BYTE_LENGTH}.`,
446
+ );
447
+ }
448
+ }
449
+
450
+ function encodeSdsFrameId(frame = {}) {
451
+ if (frame.frameId !== undefined && frame.frameId !== null) {
452
+ return normalizeBigInt(frame.frameId);
453
+ }
454
+ if (
455
+ frame.sequence !== undefined ||
456
+ frame.endOfStream !== undefined ||
457
+ frame.endOfStream === true
458
+ ) {
459
+ const sequence = normalizeBigInt(frame.sequence);
460
+ return (sequence << BigInt(1)) | (frame.endOfStream === true ? BigInt(1) : BigInt(0));
461
+ }
462
+ return normalizeBigInt(frame.traceId);
463
+ }
464
+
465
+ function decodeSdsFrameId(frameId) {
466
+ const normalized = normalizeBigInt(frameId);
467
+ return {
468
+ sequence: normalized >> BigInt(1),
469
+ endOfStream: (normalized & BigInt(1)) === BigInt(1),
470
+ };
109
471
  }
110
472
 
111
473
  function alignOffset(offset, alignment) {
@@ -116,145 +478,857 @@ function alignOffset(offset, alignment) {
116
478
  return remainder === 0 ? offset : offset + alignment - remainder;
117
479
  }
118
480
 
119
- function normalizeArenaFrame(frame = {}, offset) {
120
- const payload = toUint8Array(frame.payload ?? new Uint8Array()) ?? new Uint8Array();
121
- const typeRef = toFlatBufferTypeRefT(frame.typeRef ?? frame.allowedType ?? {}, payload.length);
122
- const alignment = Math.max(
123
- 1,
124
- normalizeUnsignedInteger(
125
- frame.alignment,
126
- typeRef.requiredAlignment > 0 ? typeRef.requiredAlignment : 8,
127
- ),
481
+ function arenaFrameOffset(frame) {
482
+ return readBoundedInteger(
483
+ firstDefined(frame?.offset, frame?.OFFSET),
484
+ "SDS PIV/TAB frame offset",
485
+ { defaultValue: 0, maximum: UINT32_MAX },
128
486
  );
487
+ }
488
+
489
+ function arenaFrameSize(frame) {
490
+ return readBoundedInteger(
491
+ firstDefined(frame?.size, frame?.SIZE),
492
+ "SDS PIV/TAB frame size",
493
+ { defaultValue: 0, maximum: UINT32_MAX },
494
+ );
495
+ }
496
+
497
+ function arenaFrameAlignment(frame) {
498
+ return readAlignment(
499
+ firstDefined(frame?.alignment, frame?.ALIGNMENT),
500
+ "SDS PIV/TAB frame alignment",
501
+ { defaultValue: INVOKE_ARENA_ALIGNMENT },
502
+ );
503
+ }
504
+
505
+ function normalizeSdsArenaFrame(frame = {}, offset) {
506
+ const payload = toUint8Array(frame.payload ?? new Uint8Array()) ?? new Uint8Array();
507
+ assertRetainedAliasSafe(frame, "SDS PIV/TAB frame forwarding");
508
+ const { typeRef, wireFormat } = normalizeFrameTypeRef(frame);
509
+ const requiredAlignment = typeRef.REQUIRED_ALIGNMENT;
510
+ const alignment = resolveFrameAlignment(frame, requiredAlignment);
511
+ assertAlignedFrameSize(wireFormat, typeRef, payload.length);
129
512
  const alignedOffset = alignOffset(offset, alignment);
513
+ assertUint32FrameRange(alignedOffset, payload.length, "SDS PIV/TAB frame");
130
514
  return {
131
515
  payload,
132
516
  padding: alignedOffset - offset,
133
- buffer: new TypedArenaBufferT(
134
- typeRef,
135
- frame.portId ?? null,
136
- alignment,
517
+ buffer: new SdsTABT(
137
518
  alignedOffset,
138
519
  payload.length,
139
- frame.ownership ?? BufferOwnership.BORROWED,
140
- normalizeUnsignedInteger(frame.generation),
141
- frame.mutability ?? BufferMutability.IMMUTABLE,
142
- normalizeBigInt(frame.traceId),
143
- normalizeUnsignedInteger(frame.streamId),
144
- normalizeBigInt(frame.sequence),
145
- frame.endOfStream === true,
520
+ alignment,
521
+ toSdsWireFormat(wireFormat),
522
+ typeRef,
523
+ toSdsMutability(frame.mutability),
524
+ toSdsOwnership(frame.ownership),
525
+ encodeSdsFrameId(frame),
526
+ frame.portId ?? null,
146
527
  ),
147
528
  };
148
529
  }
149
530
 
150
- function packArenaFrames(frames = []) {
531
+ /**
532
+ * Alignment guaranteed for the payload arena base of every encoded PIV request
533
+ * or response, measured as an absolute byte address.
534
+ */
535
+ export const INVOKE_ARENA_ALIGNMENT = 8;
536
+
537
+ /**
538
+ * Create an application-blind lifetime token for an arena view. Decoded PIV
539
+ * envelopes create one automatically; hosts can also create one explicitly
540
+ * for external/shared arenas. Advancing or closing the lease invalidates every
541
+ * frame that captured an older generation.
542
+ */
543
+ export function createInvokeArenaLease(arenaInput, options = {}) {
544
+ const arena = toUint8Array(arenaInput);
545
+ if (!arena) {
546
+ throw new TypeError(
547
+ "createInvokeArenaLease requires Uint8Array-compatible arena bytes.",
548
+ );
549
+ }
550
+ const state = {
551
+ arena,
552
+ generation: readBoundedInteger(
553
+ options.generation,
554
+ "SDS PIV/TAB arena lease generation",
555
+ { defaultValue: 1, maximum: UINT32_MAX },
556
+ ),
557
+ closed: false,
558
+ };
559
+ const lease = {
560
+ get arena() {
561
+ return state.arena;
562
+ },
563
+ get generation() {
564
+ return state.generation;
565
+ },
566
+ get closed() {
567
+ return state.closed;
568
+ },
569
+ advance(nextArenaInput = undefined) {
570
+ if (state.closed) {
571
+ throw new Error("SDS PIV/TAB arena lease is closed.");
572
+ }
573
+ if (state.generation >= UINT32_MAX) {
574
+ throw new RangeError(
575
+ "SDS PIV/TAB arena lease generation exceeds the uint32 range.",
576
+ );
577
+ }
578
+ if (nextArenaInput !== undefined) {
579
+ const nextArena = toUint8Array(nextArenaInput);
580
+ if (!nextArena) {
581
+ throw new TypeError(
582
+ "SDS PIV/TAB arena lease advance requires Uint8Array-compatible bytes.",
583
+ );
584
+ }
585
+ state.arena = nextArena;
586
+ }
587
+ state.generation += 1;
588
+ return state.generation;
589
+ },
590
+ close() {
591
+ state.closed = true;
592
+ },
593
+ createTransferToken(frame, transferOptions = {}) {
594
+ const frameState = assertFrameArenaLease(
595
+ frame,
596
+ "SDS PIV/TAB arena transfer",
597
+ { required: true },
598
+ );
599
+ if (frameState !== state || frame.arenaLease !== lease) {
600
+ throw new Error(
601
+ "SDS PIV/TAB arena transfer frame belongs to a different lease.",
602
+ );
603
+ }
604
+ const ownership = normalizeFrameOwnership(
605
+ transferOptions.ownership ?? "transferred",
606
+ );
607
+ if (ownership !== BufferOwnership.SHARED) {
608
+ throw new Error(
609
+ "SDS PIV/TAB arena transfer ownership must be transferred/shared.",
610
+ );
611
+ }
612
+ const mutability = normalizeFrameMutability(
613
+ transferOptions.mutability ?? frame.mutability,
614
+ );
615
+ const payload = toUint8Array(frame.payload);
616
+ const token = Object.freeze({});
617
+ arenaTransferStates.set(token, {
618
+ lease,
619
+ generation: state.generation,
620
+ payloadBuffer: payload.buffer,
621
+ payloadByteOffset: payload.byteOffset,
622
+ payloadByteLength: payload.byteLength,
623
+ frameId: encodeSdsFrameId(frame),
624
+ ownership,
625
+ mutability,
626
+ consumed: false,
627
+ });
628
+ return token;
629
+ },
630
+ };
631
+ arenaLeaseStates.set(lease, state);
632
+ return Object.freeze(lease);
633
+ }
634
+
635
+ /**
636
+ * Create a ubyte vector whose data is aligned to `alignment` bytes relative to
637
+ * the finished buffer. Generated `createPayloadArenaVector` aligns to one byte.
638
+ */
639
+ function createAlignedByteVector(builder, bytes, alignment) {
640
+ builder.startVector(1, bytes.length, alignment);
641
+ builder.bb.setPosition((builder.space -= bytes.length));
642
+ builder.bb.bytes().set(bytes, builder.space);
643
+ return builder.endVector();
644
+ }
645
+
646
+ function arenaBackedBuilder(arenaInput, description) {
647
+ const arena = toUint8Array(arenaInput);
648
+ if (!arena || arena.byteLength <= 0) {
649
+ throw new TypeError(`${description} requires a non-empty Uint8Array arena.`);
650
+ }
651
+ const builder = new flatbuffers.Builder(1);
652
+ builder.bb = new flatbuffers.ByteBuffer(arena);
653
+ builder.space = arena.byteLength;
654
+ builder.clear();
655
+ builder.bb = new flatbuffers.ByteBuffer(arena);
656
+ builder.space = arena.byteLength;
657
+ return builder;
658
+ }
659
+
660
+ function withFixedBuilderArena(callback, description) {
661
+ const previousGrow = flatbuffers.Builder.growByteBuffer;
662
+ try {
663
+ flatbuffers.Builder.growByteBuffer = () => {
664
+ throw new Error(`${description} arena is too small.`);
665
+ };
666
+ return callback();
667
+ } finally {
668
+ flatbuffers.Builder.growByteBuffer = previousGrow;
669
+ }
670
+ }
671
+
672
+ function describeInvokeBufferKind(kind) {
673
+ return kind === "request" ? "PIV request" : "PIV response";
674
+ }
675
+
676
+ export function assertAlignedInvokeBuffer(
677
+ bytes,
678
+ arenaArray,
679
+ kind,
680
+ arenaAlignment = INVOKE_ARENA_ALIGNMENT,
681
+ ) {
682
+ if (bytes.byteOffset % INVOKE_ARENA_ALIGNMENT !== 0) {
683
+ throw new Error(
684
+ `${describeInvokeBufferKind(kind)} buffer base is not ${INVOKE_ARENA_ALIGNMENT}-byte aligned (byteOffset ${bytes.byteOffset}).`,
685
+ );
686
+ }
687
+ if (
688
+ arenaArray &&
689
+ arenaArray.length > 0 &&
690
+ arenaArray.byteOffset % arenaAlignment !== 0
691
+ ) {
692
+ throw new Error(
693
+ `${describeInvokeBufferKind(kind)} payload arena base is not ${arenaAlignment}-byte aligned (byteOffset ${arenaArray.byteOffset}).`,
694
+ );
695
+ }
696
+ }
697
+
698
+ function packArenaFrames(frames = [], normalizeFrame = normalizeSdsArenaFrame) {
151
699
  const packedFrames = [];
152
700
  const normalizedFrames = [];
153
701
  let offset = 0;
702
+ let arenaAlignment = INVOKE_ARENA_ALIGNMENT;
154
703
  for (const frame of frames) {
155
- const normalized = normalizeArenaFrame(frame, offset);
156
- offset = normalized.buffer.offset + normalized.buffer.size;
704
+ const normalized = normalizeFrame(frame, offset);
705
+ offset = arenaFrameOffset(normalized.buffer) + arenaFrameSize(normalized.buffer);
706
+ arenaAlignment = Math.max(arenaAlignment, arenaFrameAlignment(normalized.buffer));
157
707
  packedFrames.push(normalized.buffer);
158
708
  normalizedFrames.push(normalized);
159
709
  }
160
710
 
161
711
  const arena = new Uint8Array(offset);
162
712
  for (const normalized of normalizedFrames) {
163
- arena.set(normalized.payload, normalized.buffer.offset);
713
+ arena.set(normalized.payload, arenaFrameOffset(normalized.buffer));
164
714
  }
165
715
  return {
166
716
  frames: packedFrames,
167
717
  arena,
718
+ arenaAlignment,
719
+ };
720
+ }
721
+
722
+ function hasExplicitFrameOffset(frame = {}) {
723
+ return frame.offset !== undefined || frame.OFFSET !== undefined;
724
+ }
725
+
726
+ function normalizeExternalArenaFrame(frame = {}, externalArena) {
727
+ const payload = toUint8Array(frame.payload);
728
+ const leaseState = assertRetainedAliasSafe(
729
+ frame,
730
+ "SDS PIV/TAB external frame forwarding",
731
+ );
732
+ if (leaseState && externalArena) {
733
+ assertViewWithinArena(
734
+ externalArena,
735
+ leaseState.arena,
736
+ "SDS PIV/TAB external arena",
737
+ );
738
+ }
739
+ const { typeRef, wireFormat } = normalizeFrameTypeRef(frame);
740
+ const alignment = resolveFrameAlignment(frame, typeRef.REQUIRED_ALIGNMENT);
741
+ let offset = readBoundedInteger(
742
+ firstDefined(frame.offset, frame.OFFSET),
743
+ "SDS PIV/TAB external arena frame offset",
744
+ { defaultValue: 0, maximum: UINT32_MAX },
745
+ );
746
+ if (
747
+ !hasExplicitFrameOffset(frame) &&
748
+ payload &&
749
+ externalArena &&
750
+ payload.buffer === externalArena.buffer
751
+ ) {
752
+ offset = payload.byteOffset - externalArena.byteOffset;
753
+ }
754
+ const size = readBoundedInteger(
755
+ firstDefined(frame.size, frame.SIZE, frame.byteLength, payload?.byteLength),
756
+ "SDS PIV/TAB external arena frame size",
757
+ { defaultValue: 0, maximum: UINT32_MAX },
758
+ );
759
+ assertUint32FrameRange(offset, size, "SDS PIV/TAB external arena frame");
760
+ assertAlignedFrameSize(wireFormat, typeRef, size);
761
+ if (size > 0 && externalArena) {
762
+ if (offset > externalArena.length || size > externalArena.length - offset) {
763
+ throw new Error("SDS PIV external arena frame range exceeds externalArena.");
764
+ }
765
+ const absoluteOffset = externalArena.byteOffset + offset;
766
+ if (alignment > 1 && absoluteOffset % alignment !== 0) {
767
+ throw new Error(
768
+ `SDS PIV external arena frame "${frame.portId ?? ""}" is misaligned: absolute offset ${absoluteOffset} violates alignment ${alignment}.`,
769
+ );
770
+ }
771
+ }
772
+ return new SdsTABT(
773
+ offset,
774
+ size,
775
+ alignment,
776
+ toSdsWireFormat(wireFormat),
777
+ typeRef,
778
+ toSdsMutability(frame.mutability),
779
+ toSdsOwnership(frame.ownership),
780
+ encodeSdsFrameId(frame),
781
+ frame.portId ?? null,
782
+ );
783
+ }
784
+
785
+ function packExternalArenaFrames(frames = [], externalArenaInput) {
786
+ const externalArena = toUint8Array(externalArenaInput);
787
+ if (!externalArena) {
788
+ throw new TypeError(
789
+ "SDS PIV external arena encoding requires externalArena bytes.",
790
+ );
791
+ }
792
+ let arenaAlignment = INVOKE_ARENA_ALIGNMENT;
793
+ const packedFrames = frames.map((frame) => {
794
+ const packedFrame = normalizeExternalArenaFrame(frame, externalArena);
795
+ arenaAlignment = Math.max(arenaAlignment, arenaFrameAlignment(packedFrame));
796
+ return packedFrame;
797
+ });
798
+ return {
799
+ frames: packedFrames,
800
+ arena: new Uint8Array(),
801
+ arenaAlignment,
802
+ externalArena,
168
803
  };
169
804
  }
170
805
 
171
- function materializeArenaFrames(frames = [], arenaBytes) {
806
+ function decodeSdsTypeRef(typeRef) {
807
+ if (!typeRef) {
808
+ return null;
809
+ }
810
+ const schemaHash = typeRef.schemaHashArray?.() ?? new Uint8Array();
811
+ return {
812
+ schemaName: typeRef.SCHEMA_NAME() ?? null,
813
+ fileIdentifier: typeRef.FILE_IDENTIFIER() ?? null,
814
+ schemaVersion: typeRef.SCHEMA_VERSION() ?? null,
815
+ schemaHash:
816
+ schemaHash.byteLength > 0 ? new Uint8Array(schemaHash) : undefined,
817
+ acceptsAnyFlatbuffer: typeRef.ACCEPTS_ANY_FLATBUFFER?.() === true,
818
+ wireFormat: fromSdsWireFormat(typeRef.WIRE_FORMAT()),
819
+ rootTypeName: typeRef.ROOT_TYPE() ?? null,
820
+ fixedStringLength: readBoundedInteger(
821
+ typeRef.FIXED_STRING_LENGTH(),
822
+ "SDS PIV/TAB decoded fixedStringLength",
823
+ { defaultValue: 0, maximum: UINT16_MAX },
824
+ ),
825
+ byteLength: readBoundedInteger(
826
+ typeRef.BYTE_LENGTH(),
827
+ "SDS PIV/TAB decoded byteLength",
828
+ { defaultValue: 0, maximum: UINT32_MAX },
829
+ ),
830
+ requiredAlignment: readBoundedInteger(
831
+ typeRef.REQUIRED_ALIGNMENT(),
832
+ "SDS PIV/TAB decoded requiredAlignment",
833
+ { defaultValue: 0, maximum: UINT16_MAX },
834
+ ),
835
+ };
836
+ }
837
+
838
+ function validateDecodedFrameContract(frame) {
839
+ const typeRef = frame.typeRef;
840
+ if (typeRef && typeRef.wireFormat !== frame.wireFormat) {
841
+ throw new Error("SDS PIV/TAB frame and typeRef wire formats disagree.");
842
+ }
843
+ const requiredAlignment = typeRef?.requiredAlignment ?? 0;
844
+ if (requiredAlignment > 0 && !isPositivePowerOfTwo(requiredAlignment)) {
845
+ throw new RangeError(
846
+ "SDS PIV/TAB decoded requiredAlignment must be a positive power of two.",
847
+ );
848
+ }
849
+ if (frame.alignment < requiredAlignment) {
850
+ throw new RangeError(
851
+ `SDS PIV/TAB frame alignment ${frame.alignment} is below required alignment ${requiredAlignment}.`,
852
+ );
853
+ }
854
+ if (frame.wireFormat === "aligned-binary") {
855
+ if (!typeRef) {
856
+ throw new Error("SDS PIV/TAB aligned frame requires a typeRef.");
857
+ }
858
+ if (!isPositivePowerOfTwo(requiredAlignment)) {
859
+ throw new RangeError(
860
+ "SDS PIV/TAB aligned type requiredAlignment must be a positive power of two.",
861
+ );
862
+ }
863
+ // byteLength 0/absent = VARIABLE-LENGTH aligned stream: the frame is placed
864
+ // on requiredAlignment (checked above) and carries no stride to match. A
865
+ // declared stride is still enforced exactly. Mirrors OutputTypeMatches in
866
+ // src/compiler/invokeGlue.js — the JS host and the in-wasm glue must admit
867
+ // exactly the same frames or the same artifact behaves differently in the
868
+ // browser and under WasmEdge.
869
+ const declaredByteLength = Number(typeRef.byteLength ?? 0);
870
+ if (declaredByteLength > 0 && frame.size !== declaredByteLength) {
871
+ throw new RangeError(
872
+ `SDS PIV/TAB aligned payload size ${frame.size} does not match declared byteLength ${declaredByteLength}.`,
873
+ );
874
+ }
875
+ }
876
+ }
877
+
878
+ function decodeSdsTabFrame(frame) {
879
+ if (!frame) {
880
+ return null;
881
+ }
882
+ const offset = normalizeUnsignedInteger(frame.OFFSET());
883
+ const size = normalizeUnsignedInteger(frame.SIZE());
884
+ const alignment = arenaFrameAlignment({ ALIGNMENT: frame.ALIGNMENT() });
885
+ const wireFormat = fromSdsWireFormat(frame.WIRE_FORMAT());
886
+ const frameId = normalizeBigInt(frame.FRAME_ID());
887
+ const streamFrame = decodeSdsFrameId(frameId);
888
+ const decoded = {
889
+ typeRef: decodeSdsTypeRef(frame.TYPE_REF()),
890
+ portId: frame.PORT_ID() ?? null,
891
+ alignment,
892
+ offset,
893
+ size,
894
+ ownership: fromSdsOwnership(frame.OWNERSHIP()),
895
+ generation: 0,
896
+ mutability: fromSdsMutability(frame.MUTABILITY()),
897
+ frameId,
898
+ traceId: frameId,
899
+ streamId: 0,
900
+ sequence: streamFrame.sequence,
901
+ endOfStream: streamFrame.endOfStream,
902
+ wireFormat,
903
+ };
904
+ validateDecodedFrameContract(decoded);
905
+ return decoded;
906
+ }
907
+
908
+ function decodeSdsArenaFrames(length, accessor) {
909
+ const frames = [];
910
+ for (let index = 0; index < length; index++) {
911
+ const frame = decodeSdsTabFrame(accessor(index));
912
+ if (frame) {
913
+ frames.push(frame);
914
+ }
915
+ }
916
+ return frames;
917
+ }
918
+
919
+ function resolveDecodedArenaLease(arenaBytes, options = {}) {
920
+ const externalArena = toUint8Array(options.externalArena);
921
+ const sourceArena = arenaBytes.length > 0
922
+ ? arenaBytes
923
+ : externalArena ?? arenaBytes;
924
+ if (!options.arenaLease) {
925
+ return createInvokeArenaLease(sourceArena);
926
+ }
927
+ const state = requireArenaLeaseState(options.arenaLease);
928
+ if (state.closed) {
929
+ throw new Error("SDS PIV/TAB decoded arena lease is closed.");
930
+ }
931
+ assertViewWithinArena(
932
+ sourceArena,
933
+ state.arena,
934
+ "SDS PIV/TAB decoded arena",
935
+ );
936
+ return options.arenaLease;
937
+ }
938
+
939
+ function materializeSdsArenaFrames(frames = [], arenaBytes, options = {}) {
940
+ const externalArena = toUint8Array(options.externalArena);
941
+ const arenaLease = options.arenaLease;
942
+ const arenaGeneration = arenaLease
943
+ ? requireArenaLeaseState(arenaLease).generation
944
+ : undefined;
172
945
  return frames.map((frame) => {
173
- const offset = normalizeUnsignedInteger(frame.offset);
174
- const size = normalizeUnsignedInteger(frame.size);
175
- const payload = new Uint8Array(arenaBytes.slice(offset, offset + size));
946
+ const offset = arenaFrameOffset(frame);
947
+ const size = arenaFrameSize(frame);
948
+ const sourceArena = arenaBytes.length > 0 ? arenaBytes : externalArena;
949
+ const sourceDescription =
950
+ arenaBytes.length > 0 ? "PAYLOAD_ARENA" : "external arena";
951
+ assertUint32FrameRange(offset, size, "SDS PIV/TAB decoded frame");
952
+ if (size > 0 && !sourceArena) {
953
+ throw new Error(
954
+ "SDS PIV external arena bytes are required when PAYLOAD_ARENA is empty.",
955
+ );
956
+ }
957
+ if (
958
+ sourceArena &&
959
+ (offset > sourceArena.length || size > sourceArena.length - offset)
960
+ ) {
961
+ throw new Error(`SDS PIV TAB payload range exceeds ${sourceDescription}.`);
962
+ }
963
+ const alignment = arenaFrameAlignment(frame);
964
+ if (sourceArena && alignment > 1 && size > 0) {
965
+ const absoluteOffset = sourceArena.byteOffset + offset;
966
+ if (absoluteOffset % alignment !== 0) {
967
+ throw new Error(
968
+ `SDS PIV TAB "${frame.portId ?? ""}" payload is misaligned: absolute offset ${absoluteOffset} violates alignment ${alignment}.`,
969
+ );
970
+ }
971
+ }
972
+ const payload = sourceArena
973
+ ? sourceArena.subarray(offset, offset + size)
974
+ : new Uint8Array();
176
975
  return {
177
976
  ...frame,
178
977
  payload,
179
978
  typeRef: frame.typeRef ?? null,
979
+ arenaLease,
980
+ arenaGeneration,
981
+ generation: arenaGeneration,
180
982
  };
181
983
  });
182
984
  }
183
985
 
184
- function encodeRoot(builderFactory, finish, value) {
185
- const builder = new flatbuffers.Builder(1024);
186
- finish(builder, builderFactory(value).pack(builder));
187
- return builder.asUint8Array();
986
+ function packFrameOffsets(builder, frames) {
987
+ return frames.map((frame) => frame.pack(builder));
188
988
  }
189
989
 
190
- export function encodePluginInvokeRequest(request = {}) {
191
- const { frames, arena } = packArenaFrames(
192
- Array.isArray(request.inputs) ? request.inputs : request.inputFrames ?? [],
990
+ function encodePluginInvokeRequestWithBuilder(builder, request = {}) {
991
+ const inputFrames = Array.isArray(request.inputs)
992
+ ? request.inputs
993
+ : request.inputFrames ?? [];
994
+ const { frames, arena, arenaAlignment } =
995
+ request.externalArena !== undefined
996
+ ? packExternalArenaFrames(inputFrames, request.externalArena)
997
+ : packArenaFrames(inputFrames);
998
+ const methodIdOffset =
999
+ request.methodId !== null && request.methodId !== undefined
1000
+ ? builder.createString(String(request.methodId))
1001
+ : 0;
1002
+ const framesVector = PIVRequest.createInputsVector(
1003
+ builder,
1004
+ packFrameOffsets(builder, frames),
1005
+ );
1006
+ const arenaVector = createAlignedByteVector(builder, arena, arenaAlignment);
1007
+ PIVRequest.startPIVRequest(builder);
1008
+ PIVRequest.addMethodId(builder, methodIdOffset);
1009
+ PIVRequest.addInputs(builder, framesVector);
1010
+ PIVRequest.addPayloadArena(builder, arenaVector);
1011
+ PIVRequest.addTraceId(builder, normalizeBigInt(request.traceId));
1012
+ PIVRequest.addOutputStreamCap(
1013
+ builder,
1014
+ normalizeUnsignedInteger(request.outputStreamCap),
1015
+ );
1016
+ const requestOffset = PIVRequest.endPIVRequest(builder);
1017
+ PIV.startPIV(builder);
1018
+ PIV.addRequest(builder, requestOffset);
1019
+ PIV.finishPIVBuffer(builder, PIV.endPIV(builder));
1020
+ const bytes = builder.asUint8Array();
1021
+ const root = PIV.getRootAsPIV(new flatbuffers.ByteBuffer(bytes));
1022
+ assertAlignedInvokeBuffer(
1023
+ bytes,
1024
+ root.REQUEST()?.payloadArenaArray(),
1025
+ "request",
1026
+ arenaAlignment,
193
1027
  );
194
- return encodeRoot(
195
- (value) =>
196
- new PluginInvokeRequestT(value.methodId ?? null, frames, Array.from(arena)),
197
- PluginInvokeRequest.finishPluginInvokeRequestBuffer,
1028
+ return bytes;
1029
+ }
1030
+
1031
+ export function encodePluginInvokeRequest(request = {}) {
1032
+ return encodePluginInvokeRequestWithBuilder(
1033
+ new flatbuffers.Builder(1024),
198
1034
  request,
199
1035
  );
200
1036
  }
201
1037
 
202
- export function decodePluginInvokeRequest(data) {
1038
+ export function writePluginInvokeRequestToArena(request = {}, arenaInput) {
1039
+ const builder = arenaBackedBuilder(
1040
+ arenaInput,
1041
+ "SDS PIV direct invoke request",
1042
+ );
1043
+ const bytes = withFixedBuilderArena(
1044
+ () => encodePluginInvokeRequestWithBuilder(builder, request),
1045
+ "SDS PIV direct invoke request",
1046
+ );
1047
+ const arena = toUint8Array(arenaInput);
1048
+ if (bytes.buffer !== arena.buffer) {
1049
+ throw new Error(
1050
+ "SDS PIV direct invoke request was not authored in the supplied arena.",
1051
+ );
1052
+ }
1053
+ return bytes;
1054
+ }
1055
+
1056
+ export function decodePluginInvokeRequest(data, options = {}) {
203
1057
  const bb = toByteBuffer(data);
204
- if (!PluginInvokeRequest.bufferHasIdentifier(bb)) {
205
- throw new Error("Plugin invoke request buffer identifier mismatch.");
1058
+ if (!PIV.bufferHasIdentifier(bb)) {
1059
+ throw new Error("SDS PIV invoke request buffer identifier mismatch.");
206
1060
  }
207
- const unpacked = PluginInvokeRequest.getRootAsPluginInvokeRequest(bb).unpack();
208
- const arena = Uint8Array.from(unpacked.payloadArena ?? []);
209
- const inputs = materializeArenaFrames(unpacked.inputFrames ?? [], arena);
1061
+ const root = PIV.getRootAsPIV(bb);
1062
+ const request = root.REQUEST();
1063
+ if (!request) {
1064
+ throw new Error("SDS PIV invoke envelope does not contain a request.");
1065
+ }
1066
+ const arena = request.payloadArenaArray() ?? new Uint8Array();
1067
+ const inputFrames = decodeSdsArenaFrames(request.inputsLength(), (index) =>
1068
+ request.INPUTS(index),
1069
+ );
1070
+ const arenaLease = resolveDecodedArenaLease(arena, options);
1071
+ const inputs = materializeSdsArenaFrames(inputFrames, arena, {
1072
+ ...options,
1073
+ arenaLease,
1074
+ });
210
1075
  return {
211
- methodId: unpacked.methodId ?? null,
1076
+ methodId: request.METHOD_ID() ?? null,
212
1077
  inputFrames: inputs,
213
1078
  inputs,
214
1079
  payloadArena: arena,
1080
+ arenaLease,
1081
+ traceId: request.TRACE_ID() ?? BigInt(0),
1082
+ outputStreamCap: request.OUTPUT_STREAM_CAP() ?? 0,
1083
+ envelope: "PIV",
215
1084
  };
216
1085
  }
217
1086
 
1087
+ function resolvePivStatus(response = {}) {
1088
+ if (response.status !== undefined) {
1089
+ return response.status;
1090
+ }
1091
+ if (response.yielded === true) {
1092
+ return SdsPivStatus.YIELDED;
1093
+ }
1094
+ if (Number(response.statusCode ?? 0) !== 0 || response.errorCode) {
1095
+ return SdsPivStatus.FAILED;
1096
+ }
1097
+ return SdsPivStatus.OK;
1098
+ }
1099
+
218
1100
  export function encodePluginInvokeResponse(response = {}) {
219
- const { frames, arena } = packArenaFrames(
220
- Array.isArray(response.outputs) ? response.outputs : response.outputFrames ?? [],
1101
+ const outputFrames = Array.isArray(response.outputs)
1102
+ ? response.outputs
1103
+ : response.outputFrames ?? [];
1104
+ const { frames, arena, arenaAlignment } =
1105
+ response.externalArena !== undefined
1106
+ ? packExternalArenaFrames(outputFrames, response.externalArena)
1107
+ : packArenaFrames(outputFrames);
1108
+ const builder = new flatbuffers.Builder(1024);
1109
+ const errorCodeOffset =
1110
+ response.errorCode !== null && response.errorCode !== undefined
1111
+ ? builder.createString(String(response.errorCode))
1112
+ : 0;
1113
+ const errorMessageOffset =
1114
+ response.errorMessage !== null && response.errorMessage !== undefined
1115
+ ? builder.createString(String(response.errorMessage))
1116
+ : 0;
1117
+ const framesVector = PIVResponse.createOutputsVector(
1118
+ builder,
1119
+ packFrameOffsets(builder, frames),
1120
+ );
1121
+ const arenaVector = createAlignedByteVector(builder, arena, arenaAlignment);
1122
+ PIVResponse.startPIVResponse(builder);
1123
+ PIVResponse.addStatusCode(builder, Number(response.statusCode ?? 0));
1124
+ PIVResponse.addStatus(builder, resolvePivStatus(response));
1125
+ PIVResponse.addYielded(builder, response.yielded === true);
1126
+ PIVResponse.addBacklogRemaining(
1127
+ builder,
1128
+ normalizeUnsignedInteger(response.backlogRemaining),
221
1129
  );
222
- return encodeRoot(
223
- (value) =>
224
- new PluginInvokeResponseT(
225
- Number(value.statusCode ?? 0),
226
- value.yielded === true,
227
- normalizeUnsignedInteger(value.backlogRemaining),
228
- frames,
229
- Array.from(arena),
230
- value.errorCode ?? null,
231
- value.errorMessage ?? null,
232
- ),
233
- PluginInvokeResponse.finishPluginInvokeResponseBuffer,
234
- response,
1130
+ PIVResponse.addOutputs(builder, framesVector);
1131
+ PIVResponse.addPayloadArena(builder, arenaVector);
1132
+ PIVResponse.addErrorCode(builder, errorCodeOffset);
1133
+ PIVResponse.addErrorMessage(builder, errorMessageOffset);
1134
+ PIVResponse.addTraceId(builder, normalizeBigInt(response.traceId));
1135
+ const responseOffset = PIVResponse.endPIVResponse(builder);
1136
+ PIV.startPIV(builder);
1137
+ PIV.addResponse(builder, responseOffset);
1138
+ PIV.finishPIVBuffer(builder, PIV.endPIV(builder));
1139
+ const bytes = builder.asUint8Array();
1140
+ const root = PIV.getRootAsPIV(new flatbuffers.ByteBuffer(bytes));
1141
+ assertAlignedInvokeBuffer(
1142
+ bytes,
1143
+ root.RESPONSE()?.payloadArenaArray(),
1144
+ "response",
1145
+ arenaAlignment,
235
1146
  );
1147
+ return bytes;
236
1148
  }
237
1149
 
238
- export function decodePluginInvokeResponse(data) {
1150
+ export function decodePluginInvokeResponse(data, options = {}) {
239
1151
  const bb = toByteBuffer(data);
240
- if (!PluginInvokeResponse.bufferHasIdentifier(bb)) {
241
- throw new Error("Plugin invoke response buffer identifier mismatch.");
1152
+ if (!PIV.bufferHasIdentifier(bb)) {
1153
+ throw new Error("SDS PIV invoke response buffer identifier mismatch.");
1154
+ }
1155
+ const root = PIV.getRootAsPIV(bb);
1156
+ const response = root.RESPONSE();
1157
+ if (!response) {
1158
+ throw new Error("SDS PIV invoke envelope does not contain a response.");
242
1159
  }
243
- const unpacked = PluginInvokeResponse.getRootAsPluginInvokeResponse(bb).unpack();
244
- const arena = Uint8Array.from(unpacked.payloadArena ?? []);
245
- const outputs = materializeArenaFrames(unpacked.outputFrames ?? [], arena);
1160
+ const arena = response.payloadArenaArray() ?? new Uint8Array();
1161
+ const outputFrames = decodeSdsArenaFrames(response.outputsLength(), (index) =>
1162
+ response.OUTPUTS(index),
1163
+ );
1164
+ const arenaLease = resolveDecodedArenaLease(arena, options);
1165
+ const outputs = materializeSdsArenaFrames(outputFrames, arena, {
1166
+ ...options,
1167
+ arenaLease,
1168
+ });
246
1169
  return {
247
- statusCode: unpacked.statusCode ?? 0,
248
- yielded: unpacked.yielded === true,
249
- backlogRemaining: unpacked.backlogRemaining ?? 0,
1170
+ statusCode: response.STATUS_CODE() ?? 0,
1171
+ status: response.STATUS() ?? SdsPivStatus.OK,
1172
+ yielded: response.YIELDED() === true,
1173
+ backlogRemaining: response.BACKLOG_REMAINING() ?? 0,
250
1174
  outputFrames: outputs,
251
1175
  outputs,
252
1176
  payloadArena: arena,
253
- errorCode: unpacked.errorCode ?? null,
254
- errorMessage: unpacked.errorMessage ?? null,
1177
+ arenaLease,
1178
+ errorCode: response.ERROR_CODE() ?? null,
1179
+ errorMessage: response.ERROR_MESSAGE() ?? null,
1180
+ traceId: response.TRACE_ID() ?? BigInt(0),
1181
+ envelope: "PIV",
255
1182
  };
256
1183
  }
257
1184
 
1185
+ /**
1186
+ * Forward a decoded output frame of one module invocation directly as an input
1187
+ * frame for the next invocation. The returned descriptor references the same
1188
+ * payload bytes, so no payload decode or re-serialization happens on the hop.
1189
+ */
1190
+ function consumeArenaTransferToken(token, outputFrame, payload) {
1191
+ const transfer = arenaTransferStates.get(token);
1192
+ if (!transfer) {
1193
+ throw new TypeError("SDS PIV/TAB arena transfer token is invalid.");
1194
+ }
1195
+ if (transfer.consumed) {
1196
+ throw new Error("SDS PIV/TAB arena transfer token was already consumed.");
1197
+ }
1198
+ if (
1199
+ transfer.lease !== outputFrame.arenaLease ||
1200
+ transfer.generation !==
1201
+ firstDefined(outputFrame.generation, outputFrame.arenaGeneration) ||
1202
+ transfer.payloadBuffer !== payload.buffer ||
1203
+ transfer.payloadByteOffset !== payload.byteOffset ||
1204
+ transfer.payloadByteLength !== payload.byteLength ||
1205
+ transfer.frameId !== encodeSdsFrameId(outputFrame)
1206
+ ) {
1207
+ throw new Error(
1208
+ "SDS PIV/TAB arena transfer token is incompatible with this frame.",
1209
+ );
1210
+ }
1211
+ assertLiveArenaLease(
1212
+ transfer.lease,
1213
+ transfer.generation,
1214
+ payload,
1215
+ "SDS PIV/TAB arena transfer",
1216
+ );
1217
+ transfer.consumed = true;
1218
+ return transfer;
1219
+ }
1220
+
1221
+ export function forwardOutputFrameAsInput(outputFrame, overrides = {}) {
1222
+ if (!outputFrame || typeof outputFrame !== "object") {
1223
+ throw new TypeError(
1224
+ "forwardOutputFrameAsInput requires a decoded output frame.",
1225
+ );
1226
+ }
1227
+ const payload = toUint8Array(outputFrame.payload);
1228
+ if (!payload) {
1229
+ throw new TypeError(
1230
+ "forwardOutputFrameAsInput requires an output frame with payload bytes.",
1231
+ );
1232
+ }
1233
+ const portId = overrides.portId ?? outputFrame.portId ?? null;
1234
+ if (!portId) {
1235
+ throw new TypeError(
1236
+ "forwardOutputFrameAsInput requires a portId (from the frame or overrides).",
1237
+ );
1238
+ }
1239
+ const typeRef = overrides.typeRef ?? outputFrame.typeRef ?? null;
1240
+ const wireFormat = normalizePayloadWireFormat(
1241
+ firstDefined(typeRef?.wireFormat, outputFrame.wireFormat),
1242
+ );
1243
+ const frameId = encodeSdsFrameId(outputFrame);
1244
+ if (overrides.copyCanonical === true) {
1245
+ if (wireFormat !== "flatbuffer") {
1246
+ throw new Error(
1247
+ "SDS PIV/TAB canonical copy forwarding cannot copy an aligned-binary frame without schema transcoding.",
1248
+ );
1249
+ }
1250
+ return {
1251
+ portId,
1252
+ payload: new Uint8Array(payload),
1253
+ typeRef,
1254
+ alignment: overrides.alignment ?? INVOKE_ARENA_ALIGNMENT,
1255
+ ownership: BufferOwnership.HOST_OWNED,
1256
+ mutability: BufferMutability.IMMUTABLE,
1257
+ frameId,
1258
+ traceId: overrides.traceId ?? outputFrame.traceId ?? frameId,
1259
+ streamId: overrides.streamId ?? outputFrame.streamId,
1260
+ sequence: overrides.sequence ?? outputFrame.sequence,
1261
+ endOfStream:
1262
+ overrides.endOfStream ?? (outputFrame.endOfStream === true || undefined),
1263
+ wireFormat,
1264
+ };
1265
+ }
1266
+
1267
+ assertFrameArenaLease(outputFrame, "SDS PIV/TAB zero-copy forwarding", {
1268
+ required: true,
1269
+ });
1270
+ const sourceOwnership = normalizeFrameOwnership(outputFrame.ownership);
1271
+ const sourceMutability = normalizeFrameMutability(outputFrame.mutability);
1272
+ const requestedOwnership = normalizeFrameOwnership(
1273
+ overrides.ownership ?? sourceOwnership,
1274
+ );
1275
+ const requestedMutability = normalizeFrameMutability(
1276
+ overrides.mutability ?? sourceMutability,
1277
+ );
1278
+ const requiresTransfer =
1279
+ sourceOwnership === BufferOwnership.PRODUCER_OWNED ||
1280
+ sourceMutability !== BufferMutability.IMMUTABLE ||
1281
+ requestedOwnership === BufferOwnership.PRODUCER_OWNED ||
1282
+ requestedMutability !== BufferMutability.IMMUTABLE ||
1283
+ requestedOwnership !== sourceOwnership ||
1284
+ requestedMutability !== sourceMutability;
1285
+ let ownership = sourceOwnership;
1286
+ let mutability = sourceMutability;
1287
+ let consumedTransfer = null;
1288
+ if (requiresTransfer && !overrides.arenaTransfer) {
1289
+ throw new Error(
1290
+ "SDS PIV/TAB producer-owned or mutable cross-invocation alias requires an explicit arena transfer token.",
1291
+ );
1292
+ }
1293
+ if (overrides.arenaTransfer) {
1294
+ consumedTransfer = consumeArenaTransferToken(
1295
+ overrides.arenaTransfer,
1296
+ outputFrame,
1297
+ payload,
1298
+ );
1299
+ ownership = consumedTransfer.ownership;
1300
+ mutability = consumedTransfer.mutability;
1301
+ }
1302
+ const forwarded = {
1303
+ portId,
1304
+ payload,
1305
+ typeRef,
1306
+ alignment:
1307
+ overrides.alignment ??
1308
+ (outputFrame.alignment > 0 ? outputFrame.alignment : undefined),
1309
+ ownership,
1310
+ mutability,
1311
+ arenaLease: outputFrame.arenaLease,
1312
+ arenaGeneration: outputFrame.arenaGeneration ?? outputFrame.generation,
1313
+ generation: outputFrame.generation,
1314
+ frameId,
1315
+ traceId: overrides.traceId ?? outputFrame.traceId,
1316
+ streamId: overrides.streamId ?? outputFrame.streamId,
1317
+ sequence: overrides.sequence ?? outputFrame.sequence,
1318
+ endOfStream:
1319
+ overrides.endOfStream ?? (outputFrame.endOfStream === true || undefined),
1320
+ wireFormat,
1321
+ };
1322
+ if (consumedTransfer) {
1323
+ arenaForwardingReceipts.set(forwarded, {
1324
+ ...consumedTransfer,
1325
+ ownership,
1326
+ mutability,
1327
+ });
1328
+ }
1329
+ return forwarded;
1330
+ }
1331
+
258
1332
  export function normalizeInvokeSurfaceName(value) {
259
1333
  if (value === InvokeSurface.COMMAND || value === "command") {
260
1334
  return "command";