sdkloom 0.0.0-stage → 0.1.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 (383) hide show
  1. package/LICENSE +202 -0
  2. package/README.md +239 -2
  3. package/kits/allowed-sources.json +24 -0
  4. package/kits/csharp/kit.json +6 -0
  5. package/kits/csharp/src/Runtime/Client.cs +295 -0
  6. package/kits/csharp/src/Runtime/Context.cs +216 -0
  7. package/kits/csharp/src/Runtime/Errors.cs +121 -0
  8. package/kits/csharp/src/Runtime/Http.cs +218 -0
  9. package/kits/csharp/src/Runtime/Json.cs +650 -0
  10. package/kits/csharp/src/Runtime/Lang.cs +212 -0
  11. package/kits/csharp/src/Runtime/Models.cs +206 -0
  12. package/kits/csharp/src/Runtime/Multipart.cs +108 -0
  13. package/kits/csharp/src/Runtime/Pagination.cs +122 -0
  14. package/kits/csharp/src/Runtime/Sse.cs +197 -0
  15. package/kits/csharp/src/Runtime/Testing.cs +375 -0
  16. package/kits/csharp/src/Runtime/Transport.cs +212 -0
  17. package/kits/csharp/src/Runtime/Wait.cs +57 -0
  18. package/kits/csharp/src/Runtime/Webhooks.cs +148 -0
  19. package/kits/csharp/tests/ClientTests.cs +128 -0
  20. package/kits/csharp/tests/HttpTests.cs +44 -0
  21. package/kits/csharp/tests/JsonTests.cs +132 -0
  22. package/kits/csharp/tests/LangTests.cs +57 -0
  23. package/kits/csharp/tests/MultipartTests.cs +31 -0
  24. package/kits/csharp/tests/PaginationTests.cs +93 -0
  25. package/kits/csharp/tests/SseTests.cs +122 -0
  26. package/kits/csharp/tests/TestRunner.cs +93 -0
  27. package/kits/csharp/tests/TestingTests.cs +80 -0
  28. package/kits/csharp/tests/TransportTests.cs +145 -0
  29. package/kits/csharp/tests/WaitTests.cs +39 -0
  30. package/kits/csharp/tests/WebhooksTests.cs +46 -0
  31. package/kits/csharp/versions.json +12 -0
  32. package/kits/go/kit.json +6 -0
  33. package/kits/go/src/sdkruntime/client.go +322 -0
  34. package/kits/go/src/sdkruntime/context.go +207 -0
  35. package/kits/go/src/sdkruntime/errors.go +74 -0
  36. package/kits/go/src/sdkruntime/http.go +261 -0
  37. package/kits/go/src/sdkruntime/json.go +527 -0
  38. package/kits/go/src/sdkruntime/lang.go +462 -0
  39. package/kits/go/src/sdkruntime/multipart.go +75 -0
  40. package/kits/go/src/sdkruntime/pagination.go +150 -0
  41. package/kits/go/src/sdkruntime/sse.go +267 -0
  42. package/kits/go/src/sdkruntime/testing.go +343 -0
  43. package/kits/go/src/sdkruntime/transport.go +168 -0
  44. package/kits/go/src/sdkruntime/wait.go +49 -0
  45. package/kits/go/src/sdkruntime/webhooks.go +129 -0
  46. package/kits/go/tests/client_test.go +185 -0
  47. package/kits/go/tests/http_test.go +78 -0
  48. package/kits/go/tests/json_test.go +127 -0
  49. package/kits/go/tests/lang_test.go +98 -0
  50. package/kits/go/tests/multipart_test.go +29 -0
  51. package/kits/go/tests/pagination_test.go +69 -0
  52. package/kits/go/tests/sse_test.go +129 -0
  53. package/kits/go/tests/testing_test.go +121 -0
  54. package/kits/go/tests/transport_test.go +133 -0
  55. package/kits/go/tests/wait_test.go +46 -0
  56. package/kits/go/tests/webhooks_test.go +62 -0
  57. package/kits/go/versions.json +12 -0
  58. package/kits/java/kit.json +6 -0
  59. package/kits/java/src/dev/sdkloom/runtime/ApiException.java +63 -0
  60. package/kits/java/src/dev/sdkloom/runtime/ArgumentException.java +24 -0
  61. package/kits/java/src/dev/sdkloom/runtime/Assert.java +72 -0
  62. package/kits/java/src/dev/sdkloom/runtime/ClientRuntime.java +220 -0
  63. package/kits/java/src/dev/sdkloom/runtime/Clock.java +39 -0
  64. package/kits/java/src/dev/sdkloom/runtime/Context.java +79 -0
  65. package/kits/java/src/dev/sdkloom/runtime/Decode.java +200 -0
  66. package/kits/java/src/dev/sdkloom/runtime/DecodeException.java +26 -0
  67. package/kits/java/src/dev/sdkloom/runtime/Decoder.java +16 -0
  68. package/kits/java/src/dev/sdkloom/runtime/Emitter.java +56 -0
  69. package/kits/java/src/dev/sdkloom/runtime/ErrorFactory.java +18 -0
  70. package/kits/java/src/dev/sdkloom/runtime/EventListener.java +20 -0
  71. package/kits/java/src/dev/sdkloom/runtime/EventStream.java +232 -0
  72. package/kits/java/src/dev/sdkloom/runtime/FakeClock.java +37 -0
  73. package/kits/java/src/dev/sdkloom/runtime/Headers.java +42 -0
  74. package/kits/java/src/dev/sdkloom/runtime/HttpRequest.java +88 -0
  75. package/kits/java/src/dev/sdkloom/runtime/HttpResponse.java +69 -0
  76. package/kits/java/src/dev/sdkloom/runtime/JdkTransport.java +97 -0
  77. package/kits/java/src/dev/sdkloom/runtime/Js.java +230 -0
  78. package/kits/java/src/dev/sdkloom/runtime/Json.java +281 -0
  79. package/kits/java/src/dev/sdkloom/runtime/JsonValue.java +15 -0
  80. package/kits/java/src/dev/sdkloom/runtime/Links.java +62 -0
  81. package/kits/java/src/dev/sdkloom/runtime/Middleware.java +16 -0
  82. package/kits/java/src/dev/sdkloom/runtime/MiddlewareEntry.java +43 -0
  83. package/kits/java/src/dev/sdkloom/runtime/MockRoute.java +111 -0
  84. package/kits/java/src/dev/sdkloom/runtime/MockTransport.java +62 -0
  85. package/kits/java/src/dev/sdkloom/runtime/MultipartForm.java +111 -0
  86. package/kits/java/src/dev/sdkloom/runtime/Next.java +16 -0
  87. package/kits/java/src/dev/sdkloom/runtime/OperationInfo.java +35 -0
  88. package/kits/java/src/dev/sdkloom/runtime/OperationMock.java +105 -0
  89. package/kits/java/src/dev/sdkloom/runtime/OperationSpec.java +125 -0
  90. package/kits/java/src/dev/sdkloom/runtime/Pages.java +55 -0
  91. package/kits/java/src/dev/sdkloom/runtime/Pipeline.java +29 -0
  92. package/kits/java/src/dev/sdkloom/runtime/RawResult.java +31 -0
  93. package/kits/java/src/dev/sdkloom/runtime/RecordedEvent.java +36 -0
  94. package/kits/java/src/dev/sdkloom/runtime/SdkInfo.java +38 -0
  95. package/kits/java/src/dev/sdkloom/runtime/SendOptions.java +34 -0
  96. package/kits/java/src/dev/sdkloom/runtime/ServerSentEvent.java +58 -0
  97. package/kits/java/src/dev/sdkloom/runtime/State.java +58 -0
  98. package/kits/java/src/dev/sdkloom/runtime/Test.java +21 -0
  99. package/kits/java/src/dev/sdkloom/runtime/TestRunner.java +63 -0
  100. package/kits/java/src/dev/sdkloom/runtime/Transport.java +16 -0
  101. package/kits/java/src/dev/sdkloom/runtime/TransportException.java +30 -0
  102. package/kits/java/src/dev/sdkloom/runtime/UrlConnectionTransport.java +195 -0
  103. package/kits/java/src/dev/sdkloom/runtime/Urls.java +116 -0
  104. package/kits/java/src/dev/sdkloom/runtime/Waits.java +53 -0
  105. package/kits/java/src/dev/sdkloom/runtime/WebhookSignatureException.java +24 -0
  106. package/kits/java/src/dev/sdkloom/runtime/Webhooks.java +146 -0
  107. package/kits/java/tests/dev/sdkloom/runtime/ClientTest.java +266 -0
  108. package/kits/java/tests/dev/sdkloom/runtime/EventStreamTest.java +131 -0
  109. package/kits/java/tests/dev/sdkloom/runtime/HttpTest.java +95 -0
  110. package/kits/java/tests/dev/sdkloom/runtime/JdkTransportTest.java +12 -0
  111. package/kits/java/tests/dev/sdkloom/runtime/JsTest.java +102 -0
  112. package/kits/java/tests/dev/sdkloom/runtime/JsonTest.java +87 -0
  113. package/kits/java/tests/dev/sdkloom/runtime/LinksTest.java +22 -0
  114. package/kits/java/tests/dev/sdkloom/runtime/MultipartFormTest.java +30 -0
  115. package/kits/java/tests/dev/sdkloom/runtime/PagesTest.java +75 -0
  116. package/kits/java/tests/dev/sdkloom/runtime/TestData.java +53 -0
  117. package/kits/java/tests/dev/sdkloom/runtime/TestingTest.java +156 -0
  118. package/kits/java/tests/dev/sdkloom/runtime/TransportContract.java +201 -0
  119. package/kits/java/tests/dev/sdkloom/runtime/UrlConnectionTransportTest.java +49 -0
  120. package/kits/java/tests/dev/sdkloom/runtime/WaitsTest.java +43 -0
  121. package/kits/java/tests/dev/sdkloom/runtime/WebhooksTest.java +45 -0
  122. package/kits/java/versions.json +41 -0
  123. package/kits/python/kit.json +6 -0
  124. package/kits/python/src/sdkloom_runtime/__init__.py +1 -0
  125. package/kits/python/src/sdkloom_runtime/client.py +232 -0
  126. package/kits/python/src/sdkloom_runtime/compat.py +195 -0
  127. package/kits/python/src/sdkloom_runtime/context.py +134 -0
  128. package/kits/python/src/sdkloom_runtime/errors.py +65 -0
  129. package/kits/python/src/sdkloom_runtime/http.py +192 -0
  130. package/kits/python/src/sdkloom_runtime/lang.py +207 -0
  131. package/kits/python/src/sdkloom_runtime/models.py +221 -0
  132. package/kits/python/src/sdkloom_runtime/multipart.py +69 -0
  133. package/kits/python/src/sdkloom_runtime/pagination.py +61 -0
  134. package/kits/python/src/sdkloom_runtime/pipeline.py +39 -0
  135. package/kits/python/src/sdkloom_runtime/sse.py +121 -0
  136. package/kits/python/src/sdkloom_runtime/testing.py +224 -0
  137. package/kits/python/src/sdkloom_runtime/transport.py +120 -0
  138. package/kits/python/src/sdkloom_runtime/wait.py +44 -0
  139. package/kits/python/src/sdkloom_runtime/webhooks.py +84 -0
  140. package/kits/python/tests/support.py +28 -0
  141. package/kits/python/tests/test_client.py +275 -0
  142. package/kits/python/tests/test_compat.py +87 -0
  143. package/kits/python/tests/test_http.py +94 -0
  144. package/kits/python/tests/test_lang.py +104 -0
  145. package/kits/python/tests/test_models.py +110 -0
  146. package/kits/python/tests/test_multipart.py +37 -0
  147. package/kits/python/tests/test_pagination.py +70 -0
  148. package/kits/python/tests/test_sse.py +61 -0
  149. package/kits/python/tests/test_testing.py +130 -0
  150. package/kits/python/tests/test_transport.py +124 -0
  151. package/kits/python/tests/test_wait.py +46 -0
  152. package/kits/python/tests/test_webhooks.py +57 -0
  153. package/kits/python/versions.json +33 -0
  154. package/kits/rust/kit.json +6 -0
  155. package/kits/rust/src/runtime/client.rs +212 -0
  156. package/kits/rust/src/runtime/context.rs +182 -0
  157. package/kits/rust/src/runtime/crypto.rs +84 -0
  158. package/kits/rust/src/runtime/errors.rs +147 -0
  159. package/kits/rust/src/runtime/http.rs +248 -0
  160. package/kits/rust/src/runtime/json.rs +424 -0
  161. package/kits/rust/src/runtime/lang.rs +296 -0
  162. package/kits/rust/src/runtime/mod.rs +30 -0
  163. package/kits/rust/src/runtime/models.rs +266 -0
  164. package/kits/rust/src/runtime/multipart.rs +81 -0
  165. package/kits/rust/src/runtime/pagination.rs +117 -0
  166. package/kits/rust/src/runtime/sse.rs +195 -0
  167. package/kits/rust/src/runtime/testing.rs +308 -0
  168. package/kits/rust/src/runtime/transport.rs +324 -0
  169. package/kits/rust/src/runtime/wait.rs +29 -0
  170. package/kits/rust/src/runtime/webhooks.rs +142 -0
  171. package/kits/rust/tests/client.rs +152 -0
  172. package/kits/rust/tests/http.rs +44 -0
  173. package/kits/rust/tests/json.rs +82 -0
  174. package/kits/rust/tests/lang.rs +74 -0
  175. package/kits/rust/tests/multipart.rs +21 -0
  176. package/kits/rust/tests/pagination.rs +61 -0
  177. package/kits/rust/tests/sse.rs +77 -0
  178. package/kits/rust/tests/testing.rs +90 -0
  179. package/kits/rust/tests/transport.rs +218 -0
  180. package/kits/rust/tests/wait.rs +32 -0
  181. package/kits/rust/tests/webhooks.rs +73 -0
  182. package/kits/rust/versions.json +6 -0
  183. package/kits/typescript/kit.json +6 -0
  184. package/kits/typescript/src/client.ts +223 -0
  185. package/kits/typescript/src/context.ts +108 -0
  186. package/kits/typescript/src/errors.ts +63 -0
  187. package/kits/typescript/src/helpers.ts +49 -0
  188. package/kits/typescript/src/http.ts +192 -0
  189. package/kits/typescript/src/models.ts +139 -0
  190. package/kits/typescript/src/multipart.ts +66 -0
  191. package/kits/typescript/src/pagination.ts +54 -0
  192. package/kits/typescript/src/pipeline.ts +25 -0
  193. package/kits/typescript/src/sse.ts +102 -0
  194. package/kits/typescript/src/testing.ts +217 -0
  195. package/kits/typescript/src/transport.ts +101 -0
  196. package/kits/typescript/src/wait.ts +36 -0
  197. package/kits/typescript/src/webhooks.ts +168 -0
  198. package/kits/typescript/tests/client.test.ts +269 -0
  199. package/kits/typescript/tests/helpers.test.ts +27 -0
  200. package/kits/typescript/tests/http.test.ts +80 -0
  201. package/kits/typescript/tests/models.test.ts +71 -0
  202. package/kits/typescript/tests/multipart.test.ts +28 -0
  203. package/kits/typescript/tests/pagination.test.ts +76 -0
  204. package/kits/typescript/tests/sse.test.ts +82 -0
  205. package/kits/typescript/tests/testing.test.ts +103 -0
  206. package/kits/typescript/tests/transport.test.ts +127 -0
  207. package/kits/typescript/tests/wait.test.ts +31 -0
  208. package/kits/typescript/tests/webhooks.test.ts +53 -0
  209. package/kits/typescript/versions.json +15 -0
  210. package/package.json +37 -4
  211. package/packages/cli/recipes/apiKeyHeader.ts +16 -0
  212. package/packages/cli/recipes/apiVersionHeader.ts +16 -0
  213. package/packages/cli/recipes/basic.ts +13 -0
  214. package/packages/cli/recipes/bearer.ts +10 -0
  215. package/packages/cli/recipes/hmacSign.ts +23 -0
  216. package/packages/cli/recipes/idempotencyKey.ts +19 -0
  217. package/packages/cli/recipes/oauthClientCredentials.ts +50 -0
  218. package/packages/cli/recipes/retry.ts +42 -0
  219. package/packages/cli/recipes/timing.ts +12 -0
  220. package/packages/cli/recipes/userAgent.ts +8 -0
  221. package/packages/cli/src/build.js +93 -0
  222. package/packages/cli/src/commands/build.js +31 -0
  223. package/packages/cli/src/commands/conformance-check.js +50 -0
  224. package/packages/cli/src/commands/contract.js +181 -0
  225. package/packages/cli/src/commands/diff.js +113 -0
  226. package/packages/cli/src/commands/doctor.js +43 -0
  227. package/packages/cli/src/commands/import-openapi.js +42 -0
  228. package/packages/cli/src/commands/init.js +27 -0
  229. package/packages/cli/src/commands/kit-check.js +145 -0
  230. package/packages/cli/src/commands/publish.js +81 -0
  231. package/packages/cli/src/commands/recipe.js +49 -0
  232. package/packages/cli/src/commands/record.js +181 -0
  233. package/packages/cli/src/commands/serve.js +156 -0
  234. package/packages/cli/src/commands/spec-check.js +57 -0
  235. package/packages/cli/src/commands/test.js +177 -0
  236. package/packages/cli/src/commands/variants.js +32 -0
  237. package/packages/cli/src/commands/verify.js +57 -0
  238. package/packages/cli/src/commands/version.js +63 -0
  239. package/packages/cli/src/containers.js +82 -0
  240. package/packages/cli/src/coverage.js +72 -0
  241. package/packages/cli/src/git.js +25 -0
  242. package/packages/cli/src/io.js +50 -0
  243. package/packages/cli/src/java/ScenarioHarness.java +297 -0
  244. package/packages/cli/src/java/SchemaRoundTrip.java +63 -0
  245. package/packages/cli/src/java.js +70 -0
  246. package/packages/cli/src/js/harness.mjs +120 -0
  247. package/packages/cli/src/kits.js +103 -0
  248. package/packages/cli/src/languages/csharp.js +274 -0
  249. package/packages/cli/src/languages/go.js +185 -0
  250. package/packages/cli/src/languages/index.js +18 -0
  251. package/packages/cli/src/languages/java.js +187 -0
  252. package/packages/cli/src/languages/python.js +120 -0
  253. package/packages/cli/src/languages/rust.js +189 -0
  254. package/packages/cli/src/languages/types.js +93 -0
  255. package/packages/cli/src/languages/typescript.js +112 -0
  256. package/packages/cli/src/main.js +13 -0
  257. package/packages/cli/src/packaging.js +39 -0
  258. package/packages/cli/src/portability.js +45 -0
  259. package/packages/cli/src/profiles.js +55 -0
  260. package/packages/cli/src/project.js +56 -0
  261. package/packages/cli/src/python/scenario_harness.py +162 -0
  262. package/packages/cli/src/python/schema_roundtrip.py +42 -0
  263. package/packages/cli/src/registries.js +214 -0
  264. package/packages/cli/src/resolve-core.js +39 -0
  265. package/packages/cli/src/run.js +133 -0
  266. package/packages/cli/src/runtimes.js +61 -0
  267. package/packages/cli/src/scenarios-java.js +82 -0
  268. package/packages/cli/src/scenarios-javascript.js +54 -0
  269. package/packages/cli/src/scenarios-python.js +90 -0
  270. package/packages/cli/src/scenarios.js +183 -0
  271. package/packages/cli/src/schema.js +84 -0
  272. package/packages/cli/src/templates.js +85 -0
  273. package/packages/cli/src/toolchains.js +68 -0
  274. package/packages/cli/src/transpiled-tests.js +92 -0
  275. package/packages/codegen/src/canonical-json.js +31 -0
  276. package/packages/codegen/src/capabilities.js +50 -0
  277. package/packages/codegen/src/code-writer.js +54 -0
  278. package/packages/codegen/src/file-tree.js +25 -0
  279. package/packages/codegen/src/hash.js +6 -0
  280. package/packages/codegen/src/index.js +16 -0
  281. package/packages/codegen/src/lock.js +106 -0
  282. package/packages/conformance/src/case.js +143 -0
  283. package/packages/conformance/src/checks.js +40 -0
  284. package/packages/conformance/src/index.js +3 -0
  285. package/packages/conformance/src/matrix.js +48 -0
  286. package/packages/conformance/src/suite.js +48 -0
  287. package/packages/differ/src/diff.js +500 -0
  288. package/packages/differ/src/index.js +3 -0
  289. package/packages/differ/src/notes.js +112 -0
  290. package/packages/differ/src/support.js +34 -0
  291. package/packages/gen-csharp/src/emit-client.js +397 -0
  292. package/packages/gen-csharp/src/emit-harness.js +333 -0
  293. package/packages/gen-csharp/src/emit-mock.js +111 -0
  294. package/packages/gen-csharp/src/emit-models.js +119 -0
  295. package/packages/gen-csharp/src/emit-webhooks.js +57 -0
  296. package/packages/gen-csharp/src/generate.js +245 -0
  297. package/packages/gen-csharp/src/index.js +5 -0
  298. package/packages/gen-csharp/src/names.js +284 -0
  299. package/packages/gen-go/src/emit-client.js +405 -0
  300. package/packages/gen-go/src/emit-harness.js +294 -0
  301. package/packages/gen-go/src/emit-mock.js +112 -0
  302. package/packages/gen-go/src/emit-models.js +146 -0
  303. package/packages/gen-go/src/emit-webhooks.js +58 -0
  304. package/packages/gen-go/src/generate.js +205 -0
  305. package/packages/gen-go/src/index.js +5 -0
  306. package/packages/gen-go/src/names.js +197 -0
  307. package/packages/gen-java/src/emit-client.js +441 -0
  308. package/packages/gen-java/src/emit-models.js +342 -0
  309. package/packages/gen-java/src/emit-testing.js +123 -0
  310. package/packages/gen-java/src/emit-webhooks.js +70 -0
  311. package/packages/gen-java/src/generate.js +244 -0
  312. package/packages/gen-java/src/index.js +4 -0
  313. package/packages/gen-java/src/names.js +197 -0
  314. package/packages/gen-python/src/emit-client.js +272 -0
  315. package/packages/gen-python/src/emit-models.js +139 -0
  316. package/packages/gen-python/src/emit-testing.js +93 -0
  317. package/packages/gen-python/src/emit-webhooks.js +48 -0
  318. package/packages/gen-python/src/generate.js +237 -0
  319. package/packages/gen-python/src/index.js +2 -0
  320. package/packages/gen-python/src/names.js +161 -0
  321. package/packages/gen-python/src/style.js +67 -0
  322. package/packages/gen-rust/src/emit-client.js +379 -0
  323. package/packages/gen-rust/src/emit-harness.js +278 -0
  324. package/packages/gen-rust/src/emit-mock.js +121 -0
  325. package/packages/gen-rust/src/emit-models.js +117 -0
  326. package/packages/gen-rust/src/emit-webhooks.js +58 -0
  327. package/packages/gen-rust/src/generate.js +243 -0
  328. package/packages/gen-rust/src/index.js +5 -0
  329. package/packages/gen-rust/src/names.js +223 -0
  330. package/packages/gen-typescript/src/emit-client.js +434 -0
  331. package/packages/gen-typescript/src/emit-models.js +93 -0
  332. package/packages/gen-typescript/src/emit-package.js +90 -0
  333. package/packages/gen-typescript/src/emit-testing.js +138 -0
  334. package/packages/gen-typescript/src/emit-webhooks.js +52 -0
  335. package/packages/gen-typescript/src/generate.js +70 -0
  336. package/packages/gen-typescript/src/index.js +3 -0
  337. package/packages/gen-typescript/src/naming.js +64 -0
  338. package/packages/gen-typescript/src/runtime.js +81 -0
  339. package/packages/gen-typescript/src/types.js +136 -0
  340. package/packages/ir/src/api-surface.js +22 -0
  341. package/packages/ir/src/checks.js +52 -0
  342. package/packages/ir/src/index.js +32 -0
  343. package/packages/ir/src/ir.js +214 -0
  344. package/packages/ir/src/pages.js +18 -0
  345. package/packages/ir/src/validate-ir.js +462 -0
  346. package/packages/ir/src/validate-types.js +140 -0
  347. package/packages/kit/src/block-parser.js +196 -0
  348. package/packages/kit/src/index.js +15 -0
  349. package/packages/kit/src/kit-check.js +203 -0
  350. package/packages/kit/src/sources.js +53 -0
  351. package/packages/kit/src/version-policy.js +133 -0
  352. package/packages/kit/src/version-range.js +132 -0
  353. package/packages/openapi/src/import.js +292 -0
  354. package/packages/openapi/src/index.js +1 -0
  355. package/packages/openapi/src/names.js +50 -0
  356. package/packages/openapi/src/schemas.js +264 -0
  357. package/packages/packager/src/archive.js +106 -0
  358. package/packages/packager/src/crate.js +39 -0
  359. package/packages/packager/src/gomodule.js +11 -0
  360. package/packages/packager/src/index.js +9 -0
  361. package/packages/packager/src/maven.js +85 -0
  362. package/packages/packager/src/metadata.js +34 -0
  363. package/packages/packager/src/npm.js +71 -0
  364. package/packages/packager/src/nuget.js +78 -0
  365. package/packages/packager/src/sbom.js +56 -0
  366. package/packages/packager/src/wheel.js +62 -0
  367. package/packages/spec/src/index.js +11 -0
  368. package/packages/spec/src/requirements.js +139 -0
  369. package/packages/transpile/src/ast.js +51 -0
  370. package/packages/transpile/src/check.js +669 -0
  371. package/packages/transpile/src/emit-csharp.js +469 -0
  372. package/packages/transpile/src/emit-go.js +529 -0
  373. package/packages/transpile/src/emit-java.js +360 -0
  374. package/packages/transpile/src/emit-python.js +320 -0
  375. package/packages/transpile/src/emit-rust.js +499 -0
  376. package/packages/transpile/src/errors.js +15 -0
  377. package/packages/transpile/src/index.js +13 -0
  378. package/packages/transpile/src/lexer.js +161 -0
  379. package/packages/transpile/src/parser.js +543 -0
  380. package/packages/transpile/src/tests/ast.js +46 -0
  381. package/packages/transpile/src/tests/parse.js +376 -0
  382. package/packages/transpile/src/tests/translate.js +849 -0
  383. package/packages/transpile/src/transpile.js +57 -0
@@ -0,0 +1,669 @@
1
+ // Type-checks a middleware against the subset rules and records what every expression is, for the emitters.
2
+
3
+
4
+
5
+ import { TranspileError } from "./errors.ts";
6
+
7
+
8
+
9
+
10
+
11
+
12
+
13
+
14
+
15
+ /** What a call or member read means; emitters translate these instead of re-deriving them. */
16
+
17
+
18
+
19
+
20
+
21
+
22
+
23
+
24
+
25
+
26
+
27
+
28
+
29
+
30
+
31
+
32
+
33
+
34
+
35
+
36
+
37
+
38
+
39
+
40
+
41
+ const t = (base , nullable = false) => ({ base, nullable });
42
+
43
+ /** Argument positions that take a string; a JSON value passed there is converted like String(value) in every target. */
44
+ export const STRING_PARAMS = {
45
+ "req.header": [0], "req.withHeader": [0, 1], "req.withoutHeader": [0], "req.withUrl": [0], "res.header": [0], "ctx.request": [0, 1],
46
+ "ctx.state.get": [0], "ctx.state.set": [0], "ctx.state.delete": [0], "ctx.emit": [0], Number: [0], btoa: [0],
47
+ "req.withBody": [0], encodeURIComponent: [0], hmacSha256Hex: [0, 1], hmacSha256Base64: [0, 1], "JSON.parse": [0],
48
+ };
49
+ const ASYNC = new Set(["next", "ctx.send", "ctx.clock.sleep", "res.text", "res.json"]);
50
+ const DESCRIBE = {
51
+ int: "a number", float: "a number", string: "a string", boolean: "a boolean", request: "a request", response: "a response", json: "a JSON value",
52
+ map: "an object", list: "an array", null: "undefined", void: "nothing", error: "an error", next: "next", ctx: "ctx", input: "input",
53
+ };
54
+
55
+ const isNumber = (type ) => type.base === "int" || type.base === "float";
56
+
57
+ /** The inputs of a checked logic call, in the called function's order, with the expression given for each. */
58
+ export function logicArgs(checked , expr ) {
59
+ const logic = checked.logicCalls.get(expr);
60
+ const arg = expr.args[0];
61
+ if (logic === undefined || arg?.kind !== "object") throw new Error(`internal: unchecked logic call at line ${expr.line}`);
62
+ return logic.input.map((field) => {
63
+ const value = arg.entries.find((entry) => entry.key === field.name)?.value;
64
+ if (value === undefined) throw new Error(`internal: missing input ${field.name} at line ${expr.line}`);
65
+ return { field, type: inputType(field), value };
66
+ });
67
+ }
68
+
69
+ /** The type a logic input has as a parameter. */
70
+ export function inputType(field ) {
71
+ return configType(field.type, field.optional, field.nullable);
72
+ }
73
+
74
+ function configType(node , optional , nullable ) {
75
+ const absent = optional || nullable;
76
+ if (node.kind === "enum") return t("string", absent);
77
+ if (node.kind !== "primitive") return t("json", absent);
78
+ switch (node.name) {
79
+ case "int":
80
+ return t("int", absent);
81
+ case "number":
82
+ return t("float", absent);
83
+ case "bool":
84
+ return t("boolean", absent);
85
+ default:
86
+ return t("string", absent);
87
+ }
88
+ }
89
+
90
+
91
+
92
+
93
+
94
+
95
+ class Checker {
96
+ types = new Map ();
97
+ builtins = new Map ();
98
+ configFields = new Map ();
99
+ declarations = new Map ();
100
+ scopes = [];
101
+ /** Every name declared so far, in any scope: C# forbids reusing a name a closed block declared. */
102
+ declared = new Set ();
103
+ /** Inside a try block a handed-back promise escapes the catch in JavaScript but not in Python or Java. */
104
+ tryDepth = 0;
105
+ fn ;
106
+ config ;
107
+ returns ;
108
+ functions ;
109
+ /** Logic functions listed at or after this one: calling them could recurse. */
110
+ later ;
111
+ logicCalls = new Map ();
112
+
113
+ constructor(fn , config , functions , returns , later = []) {
114
+ this.fn = fn;
115
+ this.config = config;
116
+ this.functions = functions;
117
+ this.later = later;
118
+ this.returns = returns;
119
+ }
120
+
121
+ fail(line , message , rule ) {
122
+ throw new TranspileError(this.fn.name, line, message, rule);
123
+ }
124
+
125
+ run() {
126
+ const [req, next, ctx] = this.fn.params;
127
+ this.scopes = this.fn.logic
128
+ ? [new Map([[req, { type: t("input"), constant: true }]])]
129
+ : [new Map([[req, { type: t("request"), constant: true }], [next, { type: t("next"), constant: true }], [ctx, { type: t("ctx"), constant: true }]])];
130
+ this.block(this.fn.body);
131
+ if (!this.terminates(this.fn.body)) this.fail(1, "every path must end with return or throw", "SL-T003");
132
+ const checked = { fn: this.fn, types: this.types, builtins: this.builtins, configFields: this.configFields, declarations: this.declarations, logicCalls: this.logicCalls };
133
+ return this.returns === undefined ? checked : { ...checked, logic: { input: this.config, returns: this.returns } };
134
+ }
135
+
136
+ // ---- scopes
137
+
138
+ lookup(name ) {
139
+ for (let index = this.scopes.length - 1; index >= 0; index -= 1) {
140
+ const found = this.scopes[index]?.get(name);
141
+ if (found !== undefined) return found;
142
+ }
143
+ return undefined;
144
+ }
145
+
146
+ define(line , name , type , constant ) {
147
+ if (this.lookup(name) !== undefined || this.declared.has(name)) this.fail(line, `${name} is already declared in this middleware; use another name`, "SL-T004");
148
+ // Inputs become the parameters of the generated function, which Java, Go and C# do not let a local shadow.
149
+ if (this.fn.logic && this.config.some((field) => field.name === name)) this.fail(line, `${name} is an input of this logic function; read it as ${this.fn.params[0]}.${name} or use another name`, "SL-L004");
150
+ this.declared.add(name);
151
+ this.scopes[this.scopes.length - 1]?.set(name, { type, constant });
152
+ }
153
+
154
+ scoped(statements , define ) {
155
+ this.scopes.push(new Map());
156
+ define?.();
157
+ this.block(statements);
158
+ this.scopes.pop();
159
+ }
160
+
161
+ // ---- statements
162
+
163
+ terminates(statements ) {
164
+ const last = statements[statements.length - 1];
165
+ if (last === undefined) return false;
166
+ if (last.kind === "return" || last.kind === "throw") return true;
167
+ if (last.kind === "if") return last.otherwise !== undefined && this.terminates(last.then) && this.terminates(last.otherwise);
168
+ if (last.kind === "try") return this.terminates(last.body) && this.terminates(last.handler);
169
+ return false;
170
+ }
171
+
172
+ block(statements ) {
173
+ statements.forEach((statement) => this.statement(statement));
174
+ }
175
+
176
+ condition(expr ) {
177
+ const type = this.expr(expr);
178
+ if (type.base !== "boolean" || type.nullable) this.fail(expr.line, `conditions must be booleans; this is ${DESCRIBE[type.base]}`, "SL-T005");
179
+ }
180
+
181
+ statement(statement ) {
182
+ switch (statement.kind) {
183
+ case "let": {
184
+ const type = this.expr(statement.init);
185
+ if (type.base === "null") this.fail(statement.line, "null and undefined are not initializers on their own", "SL-T004");
186
+ if (type.base === "void") this.fail(statement.line, "this call returns nothing", "SL-T004");
187
+ this.declarations.set(statement, type);
188
+ this.define(statement.line, statement.name, type, statement.constant);
189
+ return;
190
+ }
191
+ case "assign":
192
+ return this.assign(statement);
193
+ case "if":
194
+ this.condition(statement.test);
195
+ this.scoped(statement.then);
196
+ if (statement.otherwise !== undefined) this.scoped(statement.otherwise);
197
+ return;
198
+ case "for":
199
+ this.scopes.push(new Map());
200
+ if (statement.init !== undefined) this.statement(statement.init);
201
+ this.condition(statement.test);
202
+ this.scoped(statement.body);
203
+ if (statement.update !== undefined) this.statement(statement.update);
204
+ this.scopes.pop();
205
+ return;
206
+ case "forOf": {
207
+ const iterable = this.expr(statement.iterable);
208
+ if (iterable.base !== "list" && iterable.base !== "json") this.fail(statement.line, `for … of needs an array; this is ${DESCRIBE[iterable.base]}`, "SL-T002");
209
+ this.declarations.set(statement, t("json", true));
210
+ this.scoped(statement.body, () => this.define(statement.line, statement.name, t("json", true), true));
211
+ return;
212
+ }
213
+ case "while":
214
+ this.condition(statement.test);
215
+ this.scoped(statement.body);
216
+ return;
217
+ case "break":
218
+ return;
219
+ case "return": {
220
+ // `return next(req)` hands back the promise, which is the same as awaiting it.
221
+ const value = statement.value;
222
+ const handedBack = value.kind === "call" && ["next", "ctx.send"].includes(this.peekBuiltin(value));
223
+ if (handedBack && this.tryDepth > 0) this.fail(statement.line, "inside try, write `return await …` so the catch sees its errors in every language", "SL-T012");
224
+ if (this.returns !== undefined) {
225
+ const result = this.expr(value);
226
+ const expected = this.returns;
227
+ const fits = !result.nullable && (result.base === expected.base || (result.base === "int" && expected.base === "float"));
228
+ const describe = (type ) => (type.base === "int" ? "an integer" : type.base === "float" ? "a number that may have a fraction" : DESCRIBE[type.base]);
229
+ const hint = expected.base === "int" && result.base === "float" ? " (Math.floor or Math.ceil makes an integer)" : "";
230
+ if (!fits) this.fail(statement.line, `this logic function returns ${describe(expected)}; this is ${result.nullable ? "possibly undefined" : describe(result)}${hint}`, "SL-L002");
231
+ return;
232
+ }
233
+ const type = handedBack ? this.callType(value) : this.expr(statement.value);
234
+ if (type.base !== "response") this.fail(statement.line, `a middleware returns a response; this is ${DESCRIBE[type.base]}`, "SL-T003");
235
+ return;
236
+ }
237
+ case "throw": {
238
+ const type = this.expr(statement.message);
239
+ if (type.base !== "string") this.fail(statement.line, "the error message must be a string", "SL-T002");
240
+ return;
241
+ }
242
+ case "try":
243
+ this.tryDepth += 1;
244
+ this.scoped(statement.body);
245
+ this.tryDepth -= 1;
246
+ this.declarations.set(statement, t("error"));
247
+ this.scoped(statement.handler, () => this.define(statement.line, statement.param, t("error"), true));
248
+ return;
249
+ case "expression": {
250
+ const value = statement.value;
251
+ const call = value.kind === "await" ? value.value : value;
252
+ if (call.kind !== "call") this.fail(statement.line, "this expression has no effect", "SL-T002");
253
+ this.expr(value);
254
+ return;
255
+ }
256
+ }
257
+ }
258
+
259
+ assign(statement ) {
260
+ const variable = this.lookup(statement.name);
261
+ if (variable === undefined) this.fail(statement.line, `${statement.name} is not declared`, "SL-T010");
262
+ if (variable.constant) this.fail(statement.line, `${statement.name} is a constant`, "SL-T004");
263
+ const value = this.expr(statement.value);
264
+ const target = variable.type;
265
+ if (statement.op !== "=") {
266
+ const numeric = isNumber(target) && isNumber(value) && !target.nullable && !value.nullable && (target.base === "float" || value.base === "int");
267
+ const text = statement.op === "+=" && target.base === "string" && value.base === "string" && !value.nullable;
268
+ if (statement.op === "+=" && target.base === "string" && !text) {
269
+ this.fail(statement.line, `${statement.name} += needs a string; write ${statement.name} += String(…)`, "SL-T009");
270
+ }
271
+ if (!numeric && !text) this.fail(statement.line, `${statement.op} needs ${DESCRIBE[target.base]} on both sides`, "SL-T004");
272
+ return;
273
+ }
274
+ const same = value.base === target.base || (target.base === "float" && value.base === "int") || target.base === "json" || (value.base === "null" && target.nullable);
275
+ if (!same || (value.nullable && !target.nullable)) {
276
+ this.fail(statement.line, `${statement.name} is ${DESCRIBE[target.base]}; it cannot hold ${value.nullable && same ? "a value that may be undefined" : DESCRIBE[value.base]}`, "SL-T004");
277
+ }
278
+ }
279
+
280
+ // ---- expressions
281
+
282
+ expr(expr ) {
283
+ const type = this.infer(expr);
284
+ this.types.set(expr, type);
285
+ return type;
286
+ }
287
+
288
+ /** Absent values print as "undefined" in JavaScript and "null" elsewhere, so they must be resolved first. */
289
+ notAbsent(expr , type ) {
290
+ if (type.nullable && type.base !== "json" && type.base !== "null") this.fail(expr.line, `${describeExpr(expr)} may be undefined, which prints differently in each language; use ?? first`, "SL-T009");
291
+ }
292
+
293
+ stringified(expr ) {
294
+ const type = this.expr(expr);
295
+ this.notAbsent(expr, type);
296
+ return type;
297
+ }
298
+
299
+ numeric(expr , what ) {
300
+ const type = this.expr(expr);
301
+ if (!isNumber(type)) this.fail(expr.line, `${what} needs numbers; this is ${DESCRIBE[type.base]}`, "SL-T007");
302
+ if (type.nullable) this.fail(expr.line, `${describeExpr(expr)} may be undefined; use ?? first`, "SL-T007");
303
+ return type;
304
+ }
305
+
306
+ infer(expr ) {
307
+ switch (expr.kind) {
308
+ case "number":
309
+ return t(expr.integer ? "int" : "float");
310
+ case "string":
311
+ return t("string");
312
+ case "template":
313
+ expr.parts.forEach((part) => {
314
+ if (typeof part !== "string") this.stringified(part);
315
+ });
316
+ return t("string");
317
+ case "boolean":
318
+ return t("boolean");
319
+ case "null":
320
+ return t("null", true);
321
+ case "name": {
322
+ const variable = this.lookup(expr.name);
323
+ if (variable === undefined) this.fail(expr.line, `${expr.name} is not available`, "SL-T010");
324
+ if (variable.type.base === "input") this.fail(expr.line, `${expr.name} is not a value; read one input as ${expr.name}.<name>`, "SL-L001");
325
+ return variable.type;
326
+ }
327
+ case "member":
328
+ return this.member(expr);
329
+ case "call":
330
+ return this.call(expr, false);
331
+ case "await": {
332
+ if (expr.value.kind !== "call") this.fail(expr.line, "await is only used on next(…), ctx.send(…), ctx.clock.sleep(…), text() and json()", "SL-T012");
333
+ return this.call(expr.value, true);
334
+ }
335
+ case "unary":
336
+ if (expr.op === "!") {
337
+ this.condition(expr.value);
338
+ return t("boolean");
339
+ }
340
+ return t(this.numeric(expr.value, "unary -").base);
341
+ case "binary":
342
+ return this.binary(expr);
343
+ case "logical":
344
+ return this.logical(expr);
345
+ case "conditional": {
346
+ this.condition(expr.test);
347
+ return join(this.expr(expr.then), this.expr(expr.otherwise));
348
+ }
349
+ case "object":
350
+ expr.entries.forEach((entry) => this.expr(entry.value));
351
+ return t("map");
352
+ case "array":
353
+ expr.items.forEach((item) => this.expr(item));
354
+ return t("list");
355
+ }
356
+ }
357
+
358
+ binary(expr ) {
359
+ const { op } = expr;
360
+ if (op === "===" || op === "!==") {
361
+ const left = this.expr(expr.left);
362
+ const right = this.expr(expr.right);
363
+ const comparable = (a , b ) =>
364
+ a.base === b.base || (isNumber(a) && isNumber(b)) || a.base === "null" || b.base === "null" || a.base === "json" || b.base === "json";
365
+ if (!comparable(left, right)) this.fail(expr.line, `compares ${DESCRIBE[left.base].replace(/^an? /, "a ")} with ${DESCRIBE[right.base]}; the result is always the same`, "SL-T006");
366
+ return t("boolean");
367
+ }
368
+ if (op === "+") {
369
+ const left = this.expr(expr.left);
370
+ const right = this.expr(expr.right);
371
+ if (left.base === "string" || right.base === "string") {
372
+ this.notAbsent(expr.left, left);
373
+ this.notAbsent(expr.right, right);
374
+ return t("string");
375
+ }
376
+ this.numeric(expr.left, "+");
377
+ this.numeric(expr.right, "+");
378
+ return t(left.base === "int" && right.base === "int" ? "int" : "float");
379
+ }
380
+ const left = this.numeric(expr.left, op);
381
+ const right = this.numeric(expr.right, op);
382
+ if (op === "<" || op === "<=" || op === ">" || op === ">=") return t("boolean");
383
+ if (op === "/" || op === "**") return t("float");
384
+ return t(left.base === "int" && right.base === "int" ? "int" : "float");
385
+ }
386
+
387
+ logical(expr ) {
388
+ if (expr.op === "??") {
389
+ const pure = expr.left.kind === "call" && ["req.header", "res.header", "ctx.state.get"].includes(this.peekBuiltin(expr.left));
390
+ if (expr.left.kind !== "name" && expr.left.kind !== "member" && !pure) {
391
+ this.fail(expr.line, "the left side of ?? must be a variable, a property read or a header or state read", "SL-T008");
392
+ }
393
+ const left = this.expr(expr.left);
394
+ const right = this.expr(expr.right);
395
+ return join({ ...left, nullable: false }, right);
396
+ }
397
+ this.condition(expr.left);
398
+ this.condition(expr.right);
399
+ return t("boolean");
400
+ }
401
+
402
+ member(expr ) {
403
+ const path = memberPath(expr);
404
+ if (this.fn.logic && path?.[0] === this.fn.params[0] && this.lookup(this.fn.params[0])?.type.base === "input") {
405
+ const field = path.length === 2 ? this.config.find((candidate) => candidate.name === path[1]) : undefined;
406
+ if (field === undefined) {
407
+ const known = this.config.map((candidate) => candidate.name).join(", ") || "(none)";
408
+ this.fail(expr.line, `${path.join(".")} is not an input; this logic function's input is: ${known}`, "SL-L001");
409
+ }
410
+ this.builtins.set(expr, "input");
411
+ this.configFields.set(expr, field);
412
+ return configType(field.type, field.optional, field.nullable);
413
+ }
414
+ const [ctxName] = [this.fn.params[2]];
415
+ if (path?.[0] === ctxName && this.lookup(ctxName)?.type.base === "ctx") {
416
+ const rest = path.slice(1).join(".");
417
+ if (path[1] === "config" && path.length === 3) {
418
+ const field = this.config.find((candidate) => candidate.name === path[2]);
419
+ if (field === undefined) {
420
+ const known = this.config.map((candidate) => candidate.name).join(", ") || "(none)";
421
+ this.fail(expr.line, `ctx.config has no field ${path[2] ?? ""}; this middleware's config is: ${known}`, "SL-T011");
422
+ }
423
+ this.builtins.set(expr, "ctx.config");
424
+ this.configFields.set(expr, field);
425
+ return configType(field.type, field.optional, field.nullable);
426
+ }
427
+ const reads = {
428
+ "operation.name": "ctx.operation.name",
429
+ "operation.method": "ctx.operation.method",
430
+ "operation.path": "ctx.operation.path",
431
+ "sdk.name": "ctx.sdk.name",
432
+ "sdk.version": "ctx.sdk.version",
433
+ "sdk.language": "ctx.sdk.language",
434
+ };
435
+ const builtin = reads[rest];
436
+ if (builtin !== undefined) {
437
+ this.builtins.set(expr, builtin);
438
+ return t("string");
439
+ }
440
+ this.fail(expr.line, `ctx has no member ${rest}`, "SL-T011");
441
+ }
442
+ if (path?.[0] === "Math") this.fail(expr.line, `Math.${path[1] ?? ""} must be called`, "SL-T010");
443
+ const object = this.expr(expr.object);
444
+ switch (object.base) {
445
+ case "request":
446
+ if (expr.name === "method" || expr.name === "url") {
447
+ this.builtins.set(expr, expr.name === "method" ? "req.method" : "req.url");
448
+ return t("string");
449
+ }
450
+ return this.fail(expr.line, `a request has no member ${expr.name}`, "SL-T011");
451
+ case "response":
452
+ if (expr.name === "status") {
453
+ this.builtins.set(expr, "res.status");
454
+ return t("int");
455
+ }
456
+ return this.fail(expr.line, `a response has no member ${expr.name}`, "SL-T011");
457
+ case "json":
458
+ case "map":
459
+ this.builtins.set(expr, "json.get");
460
+ return t("json", true);
461
+ case "error":
462
+ if (expr.name === "message") {
463
+ this.builtins.set(expr, "error.message");
464
+ return t("string");
465
+ }
466
+ return this.fail(expr.line, `an error has no member ${expr.name}; use error.message`, "SL-T011");
467
+ default:
468
+ return this.fail(expr.line, `${DESCRIBE[object.base]} has no member ${expr.name}`, "SL-T011");
469
+ }
470
+ }
471
+
472
+ peekBuiltin(expr ) {
473
+ const callee = expr.callee;
474
+ const project = callee.kind === "name" && this.lookup(callee.name) === undefined && (this.functions.some((candidate) => candidate.name === callee.name) || this.later.includes(callee.name));
475
+ if (project) return "logic";
476
+ return this.resolveCall(expr).id;
477
+ }
478
+
479
+ /** Checks a call returned without await as if it were awaited. */
480
+ callType(expr ) {
481
+ const type = this.call(expr, true);
482
+ this.types.set(expr, type);
483
+ return type;
484
+ }
485
+
486
+ /** A call of one of the project's logic functions: `feeCents({ amountCents: 100, country })`, every input once. */
487
+ logicCall(expr , logic , awaited ) {
488
+ if (awaited) this.fail(expr.line, "await is only used on next(…), ctx.send(…), ctx.clock.sleep(…), text() and json()", "SL-T012");
489
+ const arg = expr.args[0];
490
+ if (expr.args.length !== 1 || arg?.kind !== "object") this.fail(expr.line, `${logic.name} takes one object with its inputs: ${logic.name}({ ${logic.input.map((field) => field.name).join(", ")} })`, "SL-L006");
491
+ const given = new Map(arg.entries.map((entry) => [entry.key, entry.value]));
492
+ if (given.size !== arg.entries.length) this.fail(arg.line, `an input of ${logic.name} is given twice`, "SL-L006");
493
+ for (const key of given.keys()) {
494
+ if (!logic.input.some((field) => field.name === key)) this.fail(arg.line, `${logic.name} has no input ${key}; its inputs are ${logic.input.map((field) => field.name).join(", ")}`, "SL-L006");
495
+ }
496
+ // Arguments are evaluated in the order written in TypeScript and in declared order elsewhere: they must agree.
497
+ const written = arg.entries.map((entry) => entry.key).join(", ");
498
+ const declared = logic.input.map((field) => field.name).join(", ");
499
+ if (given.size === logic.input.length && written !== declared) this.fail(arg.line, `write the inputs of ${logic.name} in declared order: ${declared}`, "SL-L006");
500
+ for (const field of logic.input) {
501
+ const value = given.get(field.name);
502
+ if (value === undefined) this.fail(arg.line, `${logic.name} needs its input ${field.name}`, "SL-L006");
503
+ const expected = inputType(field);
504
+ const actual = this.expr(value);
505
+ const fits = !actual.nullable && (actual.base === expected.base || (actual.base === "int" && expected.base === "float"));
506
+ if (!fits) this.fail(value.line, `input ${field.name} of ${logic.name} is ${DESCRIBE[expected.base]}; this is ${actual.nullable ? "possibly undefined" : DESCRIBE[actual.base]}`, "SL-L006");
507
+ }
508
+ this.builtins.set(expr, "logic");
509
+ this.logicCalls.set(expr, logic);
510
+ return inputType({ name: logic.name, type: logic.returns, optional: false, nullable: false });
511
+ }
512
+
513
+ call(expr , awaited ) {
514
+ const callee = expr.callee;
515
+ const logic = callee.kind === "name" && this.lookup(callee.name) === undefined ? this.functions.find((candidate) => candidate.name === callee.name) : undefined;
516
+ if (logic !== undefined) return this.logicCall(expr, logic, awaited);
517
+ if (callee.kind === "name" && this.lookup(callee.name) === undefined && this.later.includes(callee.name)) {
518
+ this.fail(expr.line, `a logic function calls only those listed before it in logic, so nothing recurses; list ${callee.name} before ${this.fn.name}`, "SL-L007");
519
+ }
520
+ const builtin = this.resolveCall(expr);
521
+ this.builtins.set(expr, builtin.id);
522
+ const label = builtin.label;
523
+ if (ASYNC.has(builtin.id) && !awaited) this.fail(expr.line, `${label}(…) must be awaited`, "SL-T012");
524
+ if (!ASYNC.has(builtin.id) && awaited) this.fail(expr.line, "await is only used on next(…), ctx.send(…), ctx.clock.sleep(…), text() and json()", "SL-T012");
525
+ if (builtin.arity !== undefined && !builtin.arity.includes(expr.args.length)) this.fail(expr.line, `${label} takes ${builtin.arity.join(" or ")} argument(s)`, "SL-T011");
526
+ const args = expr.args.map((arg) => this.expr(arg));
527
+ builtin.args?.forEach((expected, index) => {
528
+ const actual = args[index];
529
+ const arg = expr.args[index];
530
+ if (actual === undefined || arg === undefined || expected === "any") return;
531
+ if (expected === "number") {
532
+ this.numeric(arg, label);
533
+ return;
534
+ }
535
+ if (actual.base !== expected && actual.base !== "json") this.fail(arg.line, `${label} needs ${DESCRIBE[expected]} here; this is ${DESCRIBE[actual.base]}`, "SL-T011");
536
+ });
537
+ if (builtin.id === "ctx.state.set" && expr.args[2] !== undefined) {
538
+ const options = expr.args[2];
539
+ if (options.kind !== "object" || options.entries.length !== 1 || options.entries[0]?.key !== "ttlMs") this.fail(options.line, "the third argument of ctx.state.set is { ttlMs }", "SL-T013");
540
+ this.numeric(options.entries[0].value, "ttlMs");
541
+ }
542
+ if (builtin.id === "Math.min" || builtin.id === "Math.max") {
543
+ if (expr.args.length < 2) this.fail(expr.line, `${label} takes at least two numbers`, "SL-T011");
544
+ const types = expr.args.map((arg) => this.numeric(arg, label));
545
+ return t(types.every((type) => type.base === "int") ? "int" : "float");
546
+ }
547
+ if (builtin.id === "Math.abs") return t(this.numeric(expr.args[0] ?? expr, label).base);
548
+ if (builtin.id === "String" && expr.args[0] !== undefined) this.notAbsent(expr.args[0], args[0] ?? t("json"));
549
+ return builtin.returns;
550
+ }
551
+
552
+ resolveCall(expr ) {
553
+ const callee = expr.callee;
554
+ const [, next, ctx] = this.fn.params;
555
+ if (callee.kind === "name") {
556
+ if (callee.name === next) return { id: "next", label: "next", returns: t("response"), arity: [1], args: ["request"] };
557
+ if (callee.name === "String") return { id: "String", label: "String", returns: t("string"), arity: [1], args: ["any"] };
558
+ if (callee.name === "Number") return { id: "Number", label: "Number", returns: t("float"), arity: [1], args: ["string"] };
559
+ if (callee.name === "btoa") return { id: "btoa", label: "btoa", returns: t("string"), arity: [1], args: ["string"] };
560
+ if (callee.name === "encodeURIComponent") return { id: "encodeURIComponent", label: "encodeURIComponent", returns: t("string"), arity: [1], args: ["string"] };
561
+ // Helpers from @sdkloom/core: a random UUID (version 4) and HMAC-SHA256 of UTF-8 text with a UTF-8 key.
562
+ if (callee.name === "randomUUID") return { id: "randomUUID", label: "randomUUID", returns: t("string"), arity: [0], args: [] };
563
+ if (callee.name === "hmacSha256Hex" || callee.name === "hmacSha256Base64") return { id: callee.name, label: callee.name, returns: t("string"), arity: [2], args: ["string", "string"] };
564
+ if (this.lookup(callee.name) === undefined) this.fail(expr.line, `${callee.name} is not available`, "SL-T010");
565
+ return this.fail(expr.line, `${callee.name} cannot be called`, "SL-T011");
566
+ }
567
+ if (callee.kind !== "member") return this.fail(expr.line, "this cannot be called", "SL-T011");
568
+ const path = memberPath(callee);
569
+ // JSON text (a header, a state value, a request body) as a JSON value, read like the result of json().
570
+ if (path?.[0] === "JSON") {
571
+ if (path[1] !== "parse" || path.length !== 2) this.fail(expr.line, `JSON.${path.slice(1).join(".")} is not available; use JSON.parse`, "SL-T010");
572
+ return { id: "JSON.parse", label: "JSON.parse", returns: t("json", true), arity: [1], args: ["any"] };
573
+ }
574
+ if (path?.[0] === "Math") {
575
+ const math = {
576
+ min: { id: "Math.min", returns: t("float") },
577
+ max: { id: "Math.max", returns: t("float") },
578
+ floor: { id: "Math.floor", returns: t("int"), arity: [1], args: ["number"] },
579
+ ceil: { id: "Math.ceil", returns: t("int"), arity: [1], args: ["number"] },
580
+ abs: { id: "Math.abs", returns: t("float"), arity: [1], args: ["number"] },
581
+ random: { id: "Math.random", returns: t("float"), arity: [0] },
582
+ };
583
+ const found = math[path[1] ?? ""];
584
+ if (found === undefined || path.length !== 2) this.fail(expr.line, `Math.${path.slice(1).join(".")} is not available; use Math.min, max, floor, ceil, abs or random`, "SL-T010");
585
+ return { ...found, label: `Math.${path[1] ?? ""}` };
586
+ }
587
+ if (path?.[0] === ctx && this.lookup(ctx)?.type.base === "ctx") {
588
+ const rest = path.slice(1).join(".");
589
+ const label = `ctx.${rest}`;
590
+ const ctxCalls = {
591
+ "clock.now": { id: "ctx.clock.now", returns: t("int"), arity: [0], args: [] },
592
+ "clock.sleep": { id: "ctx.clock.sleep", returns: t("void"), arity: [1], args: ["number"] },
593
+ "state.get": { id: "ctx.state.get", returns: t("json", true), arity: [1], args: ["string"] },
594
+ "state.set": { id: "ctx.state.set", returns: t("void"), arity: [2, 3], args: ["string", "any"] },
595
+ "state.delete": { id: "ctx.state.delete", returns: t("void"), arity: [1], args: ["string"] },
596
+ emit: { id: "ctx.emit", returns: t("void"), arity: [2], args: ["string", "map"] },
597
+ request: { id: "ctx.request", returns: t("request"), arity: [2], args: ["string", "string"] },
598
+ send: { id: "ctx.send", returns: t("response"), arity: [1], args: ["request"] },
599
+ };
600
+ const found = ctxCalls[rest];
601
+ if (found === undefined) this.fail(expr.line, `ctx has no method ${rest}`, "SL-T011");
602
+ return { ...found, label };
603
+ }
604
+ if (path?.[0] === "Date" || path?.[0] === "JSON" || path?.[0] === "console" || path?.[0] === "Object" || path?.[0] === "Promise") {
605
+ this.fail(expr.line, `${path[0]} is not available`, "SL-T010");
606
+ }
607
+ const object = this.expr(callee.object);
608
+ const name = callee.name;
609
+ if (object.base === "request") {
610
+ const calls = {
611
+ header: { id: "req.header", returns: t("string", true), arity: [1], args: ["string"] },
612
+ withHeader: { id: "req.withHeader", returns: t("request"), arity: [2], args: ["string", "string"] },
613
+ withoutHeader: { id: "req.withoutHeader", returns: t("request"), arity: [1], args: ["string"] },
614
+ withUrl: { id: "req.withUrl", returns: t("request"), arity: [1], args: ["string"] },
615
+ withBody: { id: "req.withBody", returns: t("request"), arity: [1], args: ["string"] },
616
+ text: { id: "req.text", returns: t("string"), arity: [0], args: [] },
617
+ };
618
+ const found = calls[name];
619
+ if (found === undefined) this.fail(expr.line, `a request has no member ${name}`, "SL-T011");
620
+ return { ...found, label: `request.${name}` };
621
+ }
622
+ if (object.base === "response") {
623
+ const calls = {
624
+ header: { id: "res.header", returns: t("string", true), arity: [1], args: ["string"] },
625
+ text: { id: "res.text", returns: t("string"), arity: [0], args: [] },
626
+ json: { id: "res.json", returns: t("json", true), arity: [0], args: [] },
627
+ };
628
+ const found = calls[name];
629
+ if (found === undefined) this.fail(expr.line, `a response has no member ${name}`, "SL-T011");
630
+ return { ...found, label: `response.${name}` };
631
+ }
632
+ return this.fail(expr.line, `${DESCRIBE[object.base]} has no method ${name}`, "SL-T011");
633
+ }
634
+ }
635
+
636
+ function join(a , b ) {
637
+ if (a.base === "null") return { ...b, nullable: true };
638
+ if (b.base === "null") return { ...a, nullable: true };
639
+ const nullable = a.nullable || b.nullable;
640
+ if (a.base === b.base) return { base: a.base, nullable };
641
+ if (isNumber(a) && isNumber(b)) return { base: "float", nullable };
642
+ return { base: "json", nullable };
643
+ }
644
+
645
+ /** ctx.clock.now → ["ctx", "clock", "now"]; undefined when the chain does not start with a name. */
646
+ export function memberPath(expr ) {
647
+ if (expr.kind === "name") return [expr.name];
648
+ if (expr.kind !== "member") return undefined;
649
+ const object = memberPath(expr.object);
650
+ return object === undefined ? undefined : [...object, expr.name];
651
+ }
652
+
653
+ function describeExpr(expr ) {
654
+ return memberPath(expr)?.join(".") ?? "this value";
655
+ }
656
+
657
+ /** functions: the project's logic functions, which the middleware may call. */
658
+ export function checkMiddleware(fn , config , functions = []) {
659
+ return new Checker(fn, config, functions).run();
660
+ }
661
+
662
+ /** Checks a logic function: its input fields are read as input.<name>, and every return gives the declared type. */
663
+ export function checkLogic(fn , input , returns , functions = []) {
664
+ // Only functions listed before this one can be called: the call graph has no cycles.
665
+ const index = functions.findIndex((candidate) => candidate.name === fn.name);
666
+ const callable = index < 0 ? functions : functions.slice(0, index);
667
+ const later = index < 0 ? [] : functions.slice(index).map((candidate) => candidate.name);
668
+ return new Checker(fn, input, callable, configType(returns, false, false), later).run();
669
+ }