@workerstack/clients 0.0.0-stage → 1.0.0-rc.23

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 (558) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +619 -2
  3. package/bin/honey-clients.js +94 -0
  4. package/dist/cli.d.ts +102 -0
  5. package/dist/cli.js +190 -0
  6. package/dist/emit/cli/channels.d.ts +73 -0
  7. package/dist/emit/cli/channels.js +197 -0
  8. package/dist/emit/cli/commands.d.ts +27 -0
  9. package/dist/emit/cli/commands.js +187 -0
  10. package/dist/emit/cli/discovery.d.ts +35 -0
  11. package/dist/emit/cli/discovery.js +202 -0
  12. package/dist/emit/cli/flags.d.ts +59 -0
  13. package/dist/emit/cli/flags.js +186 -0
  14. package/dist/emit/cli/index.d.ts +1 -0
  15. package/dist/emit/cli/index.js +1 -0
  16. package/dist/emit/cli/install.d.ts +37 -0
  17. package/dist/emit/cli/install.js +88 -0
  18. package/dist/emit/cli/layout.d.ts +23 -0
  19. package/dist/emit/cli/layout.js +118 -0
  20. package/dist/emit/cli/module.d.ts +30 -0
  21. package/dist/emit/cli/module.js +186 -0
  22. package/dist/emit/cli/release.d.ts +40 -0
  23. package/dist/emit/cli/release.js +183 -0
  24. package/dist/emit/cli/skills.d.ts +11 -0
  25. package/dist/emit/cli/skills.js +56 -0
  26. package/dist/emit/csharp/errors.d.ts +7 -0
  27. package/dist/emit/csharp/errors.js +69 -0
  28. package/dist/emit/csharp/index.d.ts +1 -0
  29. package/dist/emit/csharp/index.js +1 -0
  30. package/dist/emit/csharp/json.d.ts +13 -0
  31. package/dist/emit/csharp/json.js +113 -0
  32. package/dist/emit/csharp/realtime.d.ts +10 -0
  33. package/dist/emit/csharp/realtime.js +33 -0
  34. package/dist/emit/csharp/sdk-answer.d.ts +48 -0
  35. package/dist/emit/csharp/sdk-answer.js +85 -0
  36. package/dist/emit/csharp/sdk-body.d.ts +10 -0
  37. package/dist/emit/csharp/sdk-body.js +87 -0
  38. package/dist/emit/csharp/sdk-client.d.ts +32 -0
  39. package/dist/emit/csharp/sdk-client.js +177 -0
  40. package/dist/emit/csharp/sdk-csproj.d.ts +36 -0
  41. package/dist/emit/csharp/sdk-csproj.js +136 -0
  42. package/dist/emit/csharp/sdk-doc.d.ts +12 -0
  43. package/dist/emit/csharp/sdk-doc.js +35 -0
  44. package/dist/emit/csharp/sdk-method.d.ts +26 -0
  45. package/dist/emit/csharp/sdk-method.js +187 -0
  46. package/dist/emit/csharp/sdk-plan.d.ts +38 -0
  47. package/dist/emit/csharp/sdk-plan.js +1 -0
  48. package/dist/emit/csharp/sdk.d.ts +40 -0
  49. package/dist/emit/csharp/sdk.js +105 -0
  50. package/dist/emit/csharp/sse.d.ts +18 -0
  51. package/dist/emit/csharp/sse.js +29 -0
  52. package/dist/emit/csharp/transport.d.ts +11 -0
  53. package/dist/emit/csharp/transport.js +67 -0
  54. package/dist/emit/csharp/types-enum.d.ts +19 -0
  55. package/dist/emit/csharp/types-enum.js +63 -0
  56. package/dist/emit/csharp/types-names.d.ts +33 -0
  57. package/dist/emit/csharp/types-names.js +182 -0
  58. package/dist/emit/csharp/types-record.d.ts +25 -0
  59. package/dist/emit/csharp/types-record.js +65 -0
  60. package/dist/emit/csharp/types-union.d.ts +27 -0
  61. package/dist/emit/csharp/types-union.js +113 -0
  62. package/dist/emit/csharp/types.d.ts +36 -0
  63. package/dist/emit/csharp/types.js +126 -0
  64. package/dist/emit/csharp/ws.d.ts +20 -0
  65. package/dist/emit/csharp/ws.js +14 -0
  66. package/dist/emit/go/errors.d.ts +7 -0
  67. package/dist/emit/go/errors.js +44 -0
  68. package/dist/emit/go/http.d.ts +11 -0
  69. package/dist/emit/go/http.js +50 -0
  70. package/dist/emit/go/index.d.ts +1 -0
  71. package/dist/emit/go/index.js +1 -0
  72. package/dist/emit/go/module-path.d.ts +2 -0
  73. package/dist/emit/go/module-path.js +27 -0
  74. package/dist/emit/go/sdk-answer.d.ts +28 -0
  75. package/dist/emit/go/sdk-answer.js +41 -0
  76. package/dist/emit/go/sdk-body.d.ts +24 -0
  77. package/dist/emit/go/sdk-body.js +76 -0
  78. package/dist/emit/go/sdk-client.d.ts +29 -0
  79. package/dist/emit/go/sdk-client.js +141 -0
  80. package/dist/emit/go/sdk-method.d.ts +68 -0
  81. package/dist/emit/go/sdk-method.js +172 -0
  82. package/dist/emit/go/sdk-mirror.d.ts +23 -0
  83. package/dist/emit/go/sdk-mirror.js +97 -0
  84. package/dist/emit/go/sdk-opts.d.ts +55 -0
  85. package/dist/emit/go/sdk-opts.js +100 -0
  86. package/dist/emit/go/sdk.d.ts +26 -0
  87. package/dist/emit/go/sdk.js +111 -0
  88. package/dist/emit/go/sse.d.ts +16 -0
  89. package/dist/emit/go/sse.js +26 -0
  90. package/dist/emit/go/types-codec.d.ts +15 -0
  91. package/dist/emit/go/types-codec.js +171 -0
  92. package/dist/emit/go/types-names.d.ts +14 -0
  93. package/dist/emit/go/types-names.js +64 -0
  94. package/dist/emit/go/types-struct.d.ts +29 -0
  95. package/dist/emit/go/types-struct.js +65 -0
  96. package/dist/emit/go/types-union.d.ts +17 -0
  97. package/dist/emit/go/types-union.js +84 -0
  98. package/dist/emit/go/types.d.ts +32 -0
  99. package/dist/emit/go/types.js +177 -0
  100. package/dist/emit/go/ws.d.ts +12 -0
  101. package/dist/emit/go/ws.js +70 -0
  102. package/dist/emit/java/gradle.d.ts +57 -0
  103. package/dist/emit/java/gradle.js +221 -0
  104. package/dist/emit/java/index.d.ts +2 -0
  105. package/dist/emit/java/index.js +2 -0
  106. package/dist/emit/java/json.d.ts +13 -0
  107. package/dist/emit/java/json.js +84 -0
  108. package/dist/emit/java/layout-ast.d.ts +146 -0
  109. package/dist/emit/java/layout-ast.js +102 -0
  110. package/dist/emit/java/layout-decl.d.ts +20 -0
  111. package/dist/emit/java/layout-decl.js +155 -0
  112. package/dist/emit/java/layout-doc.d.ts +55 -0
  113. package/dist/emit/java/layout-doc.js +166 -0
  114. package/dist/emit/java/layout-dot.d.ts +18 -0
  115. package/dist/emit/java/layout-dot.js +127 -0
  116. package/dist/emit/java/layout-strings.d.ts +8 -0
  117. package/dist/emit/java/layout-strings.js +76 -0
  118. package/dist/emit/java/layout-types.d.ts +11 -0
  119. package/dist/emit/java/layout-types.js +95 -0
  120. package/dist/emit/java/layout-visitor.d.ts +38 -0
  121. package/dist/emit/java/layout-visitor.js +232 -0
  122. package/dist/emit/java/layout.d.ts +61 -0
  123. package/dist/emit/java/layout.js +121 -0
  124. package/dist/emit/java/models-enum.d.ts +17 -0
  125. package/dist/emit/java/models-enum.js +51 -0
  126. package/dist/emit/java/models-names.d.ts +29 -0
  127. package/dist/emit/java/models-names.js +97 -0
  128. package/dist/emit/java/models-record.d.ts +24 -0
  129. package/dist/emit/java/models-record.js +79 -0
  130. package/dist/emit/java/models-union.d.ts +27 -0
  131. package/dist/emit/java/models-union.js +86 -0
  132. package/dist/emit/java/models.d.ts +28 -0
  133. package/dist/emit/java/models.js +118 -0
  134. package/dist/emit/java/realtime.d.ts +50 -0
  135. package/dist/emit/java/realtime.js +69 -0
  136. package/dist/emit/java/sdk-client.d.ts +66 -0
  137. package/dist/emit/java/sdk-client.js +207 -0
  138. package/dist/emit/java/sdk-decl.d.ts +11 -0
  139. package/dist/emit/java/sdk-decl.js +63 -0
  140. package/dist/emit/java/sdk-errors.d.ts +13 -0
  141. package/dist/emit/java/sdk-errors.js +141 -0
  142. package/dist/emit/java/sdk-headers.d.ts +12 -0
  143. package/dist/emit/java/sdk-headers.js +74 -0
  144. package/dist/emit/java/sdk-mirror.d.ts +14 -0
  145. package/dist/emit/java/sdk-mirror.js +107 -0
  146. package/dist/emit/java/sdk-models.d.ts +16 -0
  147. package/dist/emit/java/sdk-models.js +68 -0
  148. package/dist/emit/java/sdk-op.d.ts +57 -0
  149. package/dist/emit/java/sdk-op.js +216 -0
  150. package/dist/emit/java/sdk-params.d.ts +38 -0
  151. package/dist/emit/java/sdk-params.js +133 -0
  152. package/dist/emit/java/sdk-plan.d.ts +52 -0
  153. package/dist/emit/java/sdk-plan.js +1 -0
  154. package/dist/emit/java/sdk-record.d.ts +16 -0
  155. package/dist/emit/java/sdk-record.js +99 -0
  156. package/dist/emit/java/sdk.d.ts +49 -0
  157. package/dist/emit/java/sdk.js +216 -0
  158. package/dist/emit/java/sse.d.ts +35 -0
  159. package/dist/emit/java/sse.js +57 -0
  160. package/dist/emit/java/ws.d.ts +27 -0
  161. package/dist/emit/java/ws.js +31 -0
  162. package/dist/emit/mcp/annotations.d.ts +15 -0
  163. package/dist/emit/mcp/annotations.js +15 -0
  164. package/dist/emit/mcp/files.d.ts +18 -0
  165. package/dist/emit/mcp/files.js +42 -0
  166. package/dist/emit/mcp/index.d.ts +3 -0
  167. package/dist/emit/mcp/index.js +3 -0
  168. package/dist/emit/mcp/literal.d.ts +2 -0
  169. package/dist/emit/mcp/literal.js +33 -0
  170. package/dist/emit/mcp/mounted.d.ts +21 -0
  171. package/dist/emit/mcp/mounted.js +89 -0
  172. package/dist/emit/mcp/output-schema.d.ts +18 -0
  173. package/dist/emit/mcp/output-schema.js +38 -0
  174. package/dist/emit/mcp/registry.d.ts +23 -0
  175. package/dist/emit/mcp/registry.js +83 -0
  176. package/dist/emit/mcp/runtime.d.ts +10 -0
  177. package/dist/emit/mcp/runtime.js +47 -0
  178. package/dist/emit/mcp/schema.d.ts +19 -0
  179. package/dist/emit/mcp/schema.js +88 -0
  180. package/dist/emit/mcp/server.d.ts +30 -0
  181. package/dist/emit/mcp/server.js +246 -0
  182. package/dist/emit/mcp/tools.d.ts +51 -0
  183. package/dist/emit/mcp/tools.js +153 -0
  184. package/dist/emit/python/errors.d.ts +8 -0
  185. package/dist/emit/python/errors.js +68 -0
  186. package/dist/emit/python/index.d.ts +1 -0
  187. package/dist/emit/python/index.js +1 -0
  188. package/dist/emit/python/realtime.d.ts +10 -0
  189. package/dist/emit/python/realtime.js +33 -0
  190. package/dist/emit/python/sdk-answer.d.ts +49 -0
  191. package/dist/emit/python/sdk-answer.js +89 -0
  192. package/dist/emit/python/sdk-body.d.ts +11 -0
  193. package/dist/emit/python/sdk-body.js +40 -0
  194. package/dist/emit/python/sdk-client.d.ts +36 -0
  195. package/dist/emit/python/sdk-client.js +163 -0
  196. package/dist/emit/python/sdk-doc.d.ts +9 -0
  197. package/dist/emit/python/sdk-doc.js +32 -0
  198. package/dist/emit/python/sdk-imports.d.ts +14 -0
  199. package/dist/emit/python/sdk-imports.js +38 -0
  200. package/dist/emit/python/sdk-method.d.ts +29 -0
  201. package/dist/emit/python/sdk-method.js +183 -0
  202. package/dist/emit/python/sdk-plan.d.ts +33 -0
  203. package/dist/emit/python/sdk-plan.js +1 -0
  204. package/dist/emit/python/sdk-pyproject.d.ts +27 -0
  205. package/dist/emit/python/sdk-pyproject.js +103 -0
  206. package/dist/emit/python/sdk.d.ts +22 -0
  207. package/dist/emit/python/sdk.js +154 -0
  208. package/dist/emit/python/sse.d.ts +12 -0
  209. package/dist/emit/python/sse.js +23 -0
  210. package/dist/emit/python/transport.d.ts +15 -0
  211. package/dist/emit/python/transport.js +60 -0
  212. package/dist/emit/python/types-dict.d.ts +31 -0
  213. package/dist/emit/python/types-dict.js +45 -0
  214. package/dist/emit/python/types-names.d.ts +16 -0
  215. package/dist/emit/python/types-names.js +52 -0
  216. package/dist/emit/python/types-union.d.ts +32 -0
  217. package/dist/emit/python/types-union.js +46 -0
  218. package/dist/emit/python/types.d.ts +36 -0
  219. package/dist/emit/python/types.js +128 -0
  220. package/dist/emit/python/ws.d.ts +14 -0
  221. package/dist/emit/python/ws.js +16 -0
  222. package/dist/emit/rust/errors.d.ts +7 -0
  223. package/dist/emit/rust/errors.js +107 -0
  224. package/dist/emit/rust/http.d.ts +13 -0
  225. package/dist/emit/rust/http.js +67 -0
  226. package/dist/emit/rust/index.d.ts +1 -0
  227. package/dist/emit/rust/index.js +1 -0
  228. package/dist/emit/rust/realtime.d.ts +11 -0
  229. package/dist/emit/rust/realtime.js +29 -0
  230. package/dist/emit/rust/sdk-body.d.ts +19 -0
  231. package/dist/emit/rust/sdk-body.js +53 -0
  232. package/dist/emit/rust/sdk-cargo.d.ts +43 -0
  233. package/dist/emit/rust/sdk-cargo.js +178 -0
  234. package/dist/emit/rust/sdk-client.d.ts +25 -0
  235. package/dist/emit/rust/sdk-client.js +108 -0
  236. package/dist/emit/rust/sdk-method.d.ts +15 -0
  237. package/dist/emit/rust/sdk-method.js +119 -0
  238. package/dist/emit/rust/sdk-opts.d.ts +58 -0
  239. package/dist/emit/rust/sdk-opts.js +117 -0
  240. package/dist/emit/rust/sdk-plan.d.ts +33 -0
  241. package/dist/emit/rust/sdk-plan.js +1 -0
  242. package/dist/emit/rust/sdk.d.ts +23 -0
  243. package/dist/emit/rust/sdk.js +111 -0
  244. package/dist/emit/rust/sse.d.ts +19 -0
  245. package/dist/emit/rust/sse.js +20 -0
  246. package/dist/emit/rust/types-enum.d.ts +13 -0
  247. package/dist/emit/rust/types-enum.js +62 -0
  248. package/dist/emit/rust/types-names.d.ts +19 -0
  249. package/dist/emit/rust/types-names.js +66 -0
  250. package/dist/emit/rust/types-struct.d.ts +21 -0
  251. package/dist/emit/rust/types-struct.js +59 -0
  252. package/dist/emit/rust/types-union.d.ts +24 -0
  253. package/dist/emit/rust/types-union.js +97 -0
  254. package/dist/emit/rust/types.d.ts +34 -0
  255. package/dist/emit/rust/types.js +105 -0
  256. package/dist/emit/rust/ws.d.ts +21 -0
  257. package/dist/emit/rust/ws.js +20 -0
  258. package/dist/emit/shared/escape.d.ts +55 -0
  259. package/dist/emit/shared/escape.js +144 -0
  260. package/dist/emit/shared/format.d.ts +24 -0
  261. package/dist/emit/shared/format.js +23 -0
  262. package/dist/emit/shared/gofmt.d.ts +3 -0
  263. package/dist/emit/shared/gofmt.js +26 -0
  264. package/dist/emit/shared/lang.d.ts +60 -0
  265. package/dist/emit/shared/lang.js +233 -0
  266. package/dist/emit/shared/literals.d.ts +4 -0
  267. package/dist/emit/shared/literals.js +31 -0
  268. package/dist/emit/shared/metadata.d.ts +106 -0
  269. package/dist/emit/shared/metadata.js +130 -0
  270. package/dist/emit/shared/mirror.d.ts +22 -0
  271. package/dist/emit/shared/mirror.js +26 -0
  272. package/dist/emit/shared/ruff.d.ts +3 -0
  273. package/dist/emit/shared/ruff.js +31 -0
  274. package/dist/emit/shared/rustfmt.d.ts +3 -0
  275. package/dist/emit/shared/rustfmt.js +32 -0
  276. package/dist/emit/shared/status-keys.d.ts +7 -0
  277. package/dist/emit/shared/status-keys.js +67 -0
  278. package/dist/emit/shared/type-emitter.d.ts +19 -0
  279. package/dist/emit/shared/type-emitter.js +100 -0
  280. package/dist/emit/ts/app-client.d.ts +4 -0
  281. package/dist/emit/ts/app-client.js +68 -0
  282. package/dist/emit/ts/http.d.ts +20 -0
  283. package/dist/emit/ts/http.js +74 -0
  284. package/dist/emit/ts/package.d.ts +36 -0
  285. package/dist/emit/ts/package.js +126 -0
  286. package/dist/emit/ts/route-tree.d.ts +68 -0
  287. package/dist/emit/ts/route-tree.js +240 -0
  288. package/dist/emit/ts/sdk-types-preamble.d.ts +14 -0
  289. package/dist/emit/ts/sdk-types-preamble.js +123 -0
  290. package/dist/emit/ts/sdk-types.d.ts +13 -0
  291. package/dist/emit/ts/sdk-types.js +213 -0
  292. package/dist/emit/ts/sdk.d.ts +21 -0
  293. package/dist/emit/ts/sdk.js +173 -0
  294. package/dist/emit/ts/service-map.d.ts +40 -0
  295. package/dist/emit/ts/service-map.js +171 -0
  296. package/dist/emit/ts/tanstack-query.d.ts +12 -0
  297. package/dist/emit/ts/tanstack-query.js +156 -0
  298. package/dist/emit/ts/type-gen.d.ts +26 -0
  299. package/dist/emit/ts/type-gen.js +255 -0
  300. package/dist/emit/ts/webhooks.d.ts +19 -0
  301. package/dist/emit/ts/webhooks.js +81 -0
  302. package/dist/extract/chain.d.ts +34 -0
  303. package/dist/extract/chain.js +84 -0
  304. package/dist/extract/extract.d.ts +41 -0
  305. package/dist/extract/extract.js +191 -0
  306. package/dist/extract/serialize.d.ts +34 -0
  307. package/dist/extract/serialize.js +295 -0
  308. package/dist/extract/walk.d.ts +47 -0
  309. package/dist/extract/walk.js +109 -0
  310. package/dist/gen/args.d.ts +28 -0
  311. package/dist/gen/args.js +133 -0
  312. package/dist/gen/formatters.d.ts +62 -0
  313. package/dist/gen/formatters.js +146 -0
  314. package/dist/gen/process.d.ts +13 -0
  315. package/dist/gen/process.js +35 -0
  316. package/dist/gen/shipped.d.ts +17 -0
  317. package/dist/gen/shipped.js +55 -0
  318. package/dist/gen/targets.d.ts +93 -0
  319. package/dist/gen/targets.js +171 -0
  320. package/dist/gen/write.d.ts +33 -0
  321. package/dist/gen/write.js +118 -0
  322. package/dist/index.d.ts +13 -0
  323. package/dist/index.js +15 -0
  324. package/dist/ir/build.d.ts +37 -0
  325. package/dist/ir/build.js +152 -0
  326. package/dist/ir/load.d.ts +11 -0
  327. package/dist/ir/load.js +112 -0
  328. package/dist/ir/model.d.ts +206 -0
  329. package/dist/ir/model.js +5 -0
  330. package/dist/ir/resolve-refs.d.ts +11 -0
  331. package/dist/ir/resolve-refs.js +66 -0
  332. package/dist/ir/resource-tree.d.ts +9 -0
  333. package/dist/ir/resource-tree.js +53 -0
  334. package/dist/ir/route-tree-intern.d.ts +18 -0
  335. package/dist/ir/route-tree-intern.js +174 -0
  336. package/dist/ir/schema.d.ts +11 -0
  337. package/dist/ir/schema.js +192 -0
  338. package/dist/ir/sdk-model.d.ts +86 -0
  339. package/dist/ir/sdk-model.js +204 -0
  340. package/dist/ir/slots.d.ts +42 -0
  341. package/dist/ir/slots.js +245 -0
  342. package/dist/runtime/cli/body.go +176 -0
  343. package/dist/runtime/cli/config.go +243 -0
  344. package/dist/runtime/cli/discovery.go +147 -0
  345. package/dist/runtime/cli/format.go +174 -0
  346. package/dist/runtime/cli/log.go +117 -0
  347. package/dist/runtime/cli/output.go +247 -0
  348. package/dist/runtime/cli/params.go +197 -0
  349. package/dist/runtime/cli/run.go +226 -0
  350. package/dist/runtime/cli/socket.go +60 -0
  351. package/dist/runtime/cli/stream.go +217 -0
  352. package/dist/runtime/cli/target.go +117 -0
  353. package/dist/runtime/csharp/Adapters.cs +183 -0
  354. package/dist/runtime/csharp/Auth.cs +56 -0
  355. package/dist/runtime/csharp/Body.cs +135 -0
  356. package/dist/runtime/csharp/Config.cs +161 -0
  357. package/dist/runtime/csharp/Envelope.cs +149 -0
  358. package/dist/runtime/csharp/Errors.cs +194 -0
  359. package/dist/runtime/csharp/Events.cs +155 -0
  360. package/dist/runtime/csharp/Exchange.cs +247 -0
  361. package/dist/runtime/csharp/Handler.cs +103 -0
  362. package/dist/runtime/csharp/Hooks.cs +128 -0
  363. package/dist/runtime/csharp/Invalidation.cs +102 -0
  364. package/dist/runtime/csharp/Json.cs +144 -0
  365. package/dist/runtime/csharp/Log.cs +48 -0
  366. package/dist/runtime/csharp/Operation.cs +33 -0
  367. package/dist/runtime/csharp/Path.cs +65 -0
  368. package/dist/runtime/csharp/Query.cs +72 -0
  369. package/dist/runtime/csharp/Realtime.cs +118 -0
  370. package/dist/runtime/csharp/RealtimeLoop.cs +236 -0
  371. package/dist/runtime/csharp/Redirect.cs +53 -0
  372. package/dist/runtime/csharp/Result.cs +57 -0
  373. package/dist/runtime/csharp/Sse.cs +122 -0
  374. package/dist/runtime/csharp/Stale.cs +151 -0
  375. package/dist/runtime/csharp/Transport.cs +143 -0
  376. package/dist/runtime/csharp/TransportLongPoll.cs +63 -0
  377. package/dist/runtime/csharp/TransportSse.cs +54 -0
  378. package/dist/runtime/csharp/TransportWs.cs +40 -0
  379. package/dist/runtime/csharp/Wire.cs +109 -0
  380. package/dist/runtime/csharp/Ws.cs +300 -0
  381. package/dist/runtime/csharp/WsDial.cs +127 -0
  382. package/dist/runtime/go/auth.go +127 -0
  383. package/dist/runtime/go/body.go +129 -0
  384. package/dist/runtime/go/config.go +75 -0
  385. package/dist/runtime/go/errors.go +185 -0
  386. package/dist/runtime/go/export.go +37 -0
  387. package/dist/runtime/go/hooks.go +53 -0
  388. package/dist/runtime/go/invalidation.go +112 -0
  389. package/dist/runtime/go/log.go +92 -0
  390. package/dist/runtime/go/nullable.go +64 -0
  391. package/dist/runtime/go/options.go +118 -0
  392. package/dist/runtime/go/path.go +156 -0
  393. package/dist/runtime/go/query.go +147 -0
  394. package/dist/runtime/go/realtime.go +239 -0
  395. package/dist/runtime/go/realtime_loop.go +208 -0
  396. package/dist/runtime/go/redirect.go +124 -0
  397. package/dist/runtime/go/request.go +222 -0
  398. package/dist/runtime/go/result.go +86 -0
  399. package/dist/runtime/go/sse.go +186 -0
  400. package/dist/runtime/go/stale.go +229 -0
  401. package/dist/runtime/go/stream.go +105 -0
  402. package/dist/runtime/go/transport.go +242 -0
  403. package/dist/runtime/go/transport_longpoll.go +121 -0
  404. package/dist/runtime/go/transport_sse.go +175 -0
  405. package/dist/runtime/go/ws.go +205 -0
  406. package/dist/runtime/java/Answer.java +107 -0
  407. package/dist/runtime/java/ApiException.java +110 -0
  408. package/dist/runtime/java/BinaryBody.java +36 -0
  409. package/dist/runtime/java/Body.java +105 -0
  410. package/dist/runtime/java/Call.java +23 -0
  411. package/dist/runtime/java/CallLog.java +51 -0
  412. package/dist/runtime/java/ClientConfig.java +233 -0
  413. package/dist/runtime/java/ConnectionState.java +12 -0
  414. package/dist/runtime/java/Credential.java +58 -0
  415. package/dist/runtime/java/Deadline.java +38 -0
  416. package/dist/runtime/java/Decode.java +181 -0
  417. package/dist/runtime/java/Envelope.java +141 -0
  418. package/dist/runtime/java/ErrorEnvelope.java +23 -0
  419. package/dist/runtime/java/EventStream.java +129 -0
  420. package/dist/runtime/java/Exchange.java +62 -0
  421. package/dist/runtime/java/FilePart.java +5 -0
  422. package/dist/runtime/java/Flight.java +306 -0
  423. package/dist/runtime/java/HoneyException.java +14 -0
  424. package/dist/runtime/java/Hooks.java +47 -0
  425. package/dist/runtime/java/Hops.java +133 -0
  426. package/dist/runtime/java/InvalidationConfig.java +73 -0
  427. package/dist/runtime/java/Json.java +23 -0
  428. package/dist/runtime/java/Keepalive.java +60 -0
  429. package/dist/runtime/java/LogEntry.java +14 -0
  430. package/dist/runtime/java/LongPollAdapter.java +68 -0
  431. package/dist/runtime/java/Operation.java +21 -0
  432. package/dist/runtime/java/Opt.java +121 -0
  433. package/dist/runtime/java/Pump.java +160 -0
  434. package/dist/runtime/java/RawResponse.java +48 -0
  435. package/dist/runtime/java/Realtime.java +152 -0
  436. package/dist/runtime/java/RealtimeLoop.java +371 -0
  437. package/dist/runtime/java/RedirectPolicy.java +13 -0
  438. package/dist/runtime/java/Redirects.java +126 -0
  439. package/dist/runtime/java/RequestContext.java +78 -0
  440. package/dist/runtime/java/RequestHook.java +9 -0
  441. package/dist/runtime/java/RequestOptions.java +91 -0
  442. package/dist/runtime/java/ResponseContext.java +70 -0
  443. package/dist/runtime/java/ResponseHook.java +12 -0
  444. package/dist/runtime/java/Result.java +32 -0
  445. package/dist/runtime/java/ResumableConnection.java +165 -0
  446. package/dist/runtime/java/SdkException.java +11 -0
  447. package/dist/runtime/java/Socket.java +301 -0
  448. package/dist/runtime/java/Sse.java +171 -0
  449. package/dist/runtime/java/SseAdapter.java +61 -0
  450. package/dist/runtime/java/SseEvent.java +10 -0
  451. package/dist/runtime/java/StaleMeta.java +16 -0
  452. package/dist/runtime/java/StaleTracker.java +287 -0
  453. package/dist/runtime/java/Transport.java +16 -0
  454. package/dist/runtime/java/TransportAdapter.java +14 -0
  455. package/dist/runtime/java/TransportConnection.java +15 -0
  456. package/dist/runtime/java/TransportKind.java +12 -0
  457. package/dist/runtime/java/TransportMessage.java +9 -0
  458. package/dist/runtime/java/TransportOptions.java +138 -0
  459. package/dist/runtime/java/TypedWebSocket.java +83 -0
  460. package/dist/runtime/java/UrlPath.java +49 -0
  461. package/dist/runtime/java/UrlQuery.java +99 -0
  462. package/dist/runtime/java/WsAdapter.java +52 -0
  463. package/dist/runtime/java/WsClose.java +47 -0
  464. package/dist/runtime/java/WsDial.java +148 -0
  465. package/dist/runtime/java/WsMessage.java +18 -0
  466. package/dist/runtime/mcp/call.ts +167 -0
  467. package/dist/runtime/mcp/files.ts +183 -0
  468. package/dist/runtime/mcp/guard.ts +176 -0
  469. package/dist/runtime/mcp/http.ts +159 -0
  470. package/dist/runtime/mcp/jsonrpc.ts +50 -0
  471. package/dist/runtime/mcp/listen.ts +76 -0
  472. package/dist/runtime/mcp/mount.ts +123 -0
  473. package/dist/runtime/mcp/protocol.ts +219 -0
  474. package/dist/runtime/mcp/reader.ts +57 -0
  475. package/dist/runtime/mcp/result.ts +85 -0
  476. package/dist/runtime/mcp/stdio.ts +79 -0
  477. package/dist/runtime/mcp/stream.ts +62 -0
  478. package/dist/runtime/python/__init__.py +2 -0
  479. package/dist/runtime/python/auth.py +67 -0
  480. package/dist/runtime/python/body.py +83 -0
  481. package/dist/runtime/python/config.py +139 -0
  482. package/dist/runtime/python/errors.py +213 -0
  483. package/dist/runtime/python/events.py +152 -0
  484. package/dist/runtime/python/exchange.py +298 -0
  485. package/dist/runtime/python/hooks.py +86 -0
  486. package/dist/runtime/python/invalidation.py +57 -0
  487. package/dist/runtime/python/log.py +60 -0
  488. package/dist/runtime/python/operation.py +25 -0
  489. package/dist/runtime/python/path.py +41 -0
  490. package/dist/runtime/python/query.py +48 -0
  491. package/dist/runtime/python/realtime.py +244 -0
  492. package/dist/runtime/python/redirect.py +93 -0
  493. package/dist/runtime/python/result.py +58 -0
  494. package/dist/runtime/python/send_async.py +189 -0
  495. package/dist/runtime/python/send_sync.py +105 -0
  496. package/dist/runtime/python/sse.py +81 -0
  497. package/dist/runtime/python/stale.py +94 -0
  498. package/dist/runtime/python/transport.py +157 -0
  499. package/dist/runtime/python/transport_longpoll.py +60 -0
  500. package/dist/runtime/python/transport_sse.py +60 -0
  501. package/dist/runtime/python/transport_ws.py +57 -0
  502. package/dist/runtime/python/ws.py +276 -0
  503. package/dist/runtime/rust/Cargo.lock +1763 -0
  504. package/dist/runtime/rust/answer.rs +120 -0
  505. package/dist/runtime/rust/auth.rs +80 -0
  506. package/dist/runtime/rust/body.rs +150 -0
  507. package/dist/runtime/rust/call.rs +200 -0
  508. package/dist/runtime/rust/config.rs +254 -0
  509. package/dist/runtime/rust/envelope.rs +84 -0
  510. package/dist/runtime/rust/errors.rs +235 -0
  511. package/dist/runtime/rust/events.rs +177 -0
  512. package/dist/runtime/rust/exchange.rs +287 -0
  513. package/dist/runtime/rust/hooks.rs +63 -0
  514. package/dist/runtime/rust/invalidation.rs +84 -0
  515. package/dist/runtime/rust/log.rs +88 -0
  516. package/dist/runtime/rust/nullable.rs +13 -0
  517. package/dist/runtime/rust/path.rs +83 -0
  518. package/dist/runtime/rust/query.rs +84 -0
  519. package/dist/runtime/rust/realtime.rs +182 -0
  520. package/dist/runtime/rust/realtime_loop.rs +177 -0
  521. package/dist/runtime/rust/redirect.rs +121 -0
  522. package/dist/runtime/rust/request.rs +178 -0
  523. package/dist/runtime/rust/result.rs +101 -0
  524. package/dist/runtime/rust/sse.rs +104 -0
  525. package/dist/runtime/rust/stale.rs +209 -0
  526. package/dist/runtime/rust/transport.rs +221 -0
  527. package/dist/runtime/rust/transport_longpoll.rs +108 -0
  528. package/dist/runtime/rust/transport_sse.rs +91 -0
  529. package/dist/runtime/rust/transport_ws.rs +75 -0
  530. package/dist/runtime/rust/ws.rs +322 -0
  531. package/dist/runtime/ts/app-types.ts +200 -0
  532. package/dist/runtime/ts/app.ts +104 -0
  533. package/dist/runtime/ts/config.ts +124 -0
  534. package/dist/runtime/ts/defaults.ts +48 -0
  535. package/dist/runtime/ts/error.ts +113 -0
  536. package/dist/runtime/ts/hooks.ts +92 -0
  537. package/dist/runtime/ts/http.ts +228 -0
  538. package/dist/runtime/ts/media-type.ts +36 -0
  539. package/dist/runtime/ts/path.ts +152 -0
  540. package/dist/runtime/ts/realtime-connection.ts +296 -0
  541. package/dist/runtime/ts/realtime-transports.ts +149 -0
  542. package/dist/runtime/ts/realtime.ts +293 -0
  543. package/dist/runtime/ts/redirect-policy.ts +17 -0
  544. package/dist/runtime/ts/redirects.ts +74 -0
  545. package/dist/runtime/ts/request.ts +239 -0
  546. package/dist/runtime/ts/response.ts +219 -0
  547. package/dist/runtime/ts/sdk-realtime.ts +10 -0
  548. package/dist/runtime/ts/sdk-sse.ts +55 -0
  549. package/dist/runtime/ts/sdk-ws.ts +21 -0
  550. package/dist/runtime/ts/sdk.ts +242 -0
  551. package/dist/runtime/ts/sse.ts +175 -0
  552. package/dist/runtime/ts/stale.ts +137 -0
  553. package/dist/runtime/ts/stream.ts +87 -0
  554. package/dist/runtime/ts/webhook.ts +116 -0
  555. package/dist/runtime/ts/ws.ts +155 -0
  556. package/dist/runtime/ts-tanstack-query/invalidate.ts +152 -0
  557. package/dist/runtime/ts-tanstack-query/query.ts +261 -0
  558. package/package.json +84 -5
@@ -0,0 +1,293 @@
1
+ /**
2
+ * The realtime resume protocol's client side (H8): the frames a server sends, the errors a
3
+ * connection ends with, the transport adapters a consumer plugs in, the keepalive loop and the
4
+ * fallback chain that picks the first transport to answer `ready`.
5
+ */
6
+
7
+ export type ServerFrame =
8
+ | { data: unknown; id?: string | undefined; reconnectToken?: string | undefined; t: "msg" }
9
+ | { t: "pong" }
10
+ | { reason: string; t: "bye" }
11
+ | { reconnectToken: string; t: "ready" }
12
+
13
+ export class RealtimeError extends Error {
14
+ readonly reason: string
15
+
16
+ constructor(message: string, reason: string) {
17
+ super(message)
18
+ this.reason = reason
19
+ this.name = "RealtimeError"
20
+ }
21
+ }
22
+
23
+ export class RealtimeConnectError extends RealtimeError {
24
+ override name = "RealtimeConnectError"
25
+ }
26
+
27
+ export class RealtimeKickedError extends RealtimeError {
28
+ override name = "RealtimeKickedError"
29
+ }
30
+
31
+ export class RealtimeAbortError extends RealtimeError {
32
+ override name = "RealtimeAbortError"
33
+ }
34
+
35
+ export type Transport = "longpoll" | "sse" | "ws"
36
+
37
+ export type ConnectionState = "closed" | "connected" | "connecting" | "draining" | "idle" | "reconnecting"
38
+
39
+ export type TransportOpts = {
40
+ headers?: Record<string, string> | undefined
41
+ lastEventId?: string | undefined
42
+ reconnectToken?: string | undefined
43
+ signal?: AbortSignal | undefined
44
+ }
45
+
46
+ /** One live transport: the adapter calls `onFrame`, `onClose` and `onError`; the client replaces them. */
47
+ export type TransportConnection = {
48
+ close(): void
49
+ onClose: (reason: string) => void
50
+ onError: (err: unknown) => void
51
+ onFrame: (frame: ServerFrame) => void
52
+ send(data: string): Promise<void> | void
53
+ }
54
+
55
+ /** A swallowed callback: a consumer's hook must never break the reconnect loop. */
56
+ export function safely(fn: (() => void) | undefined): void {
57
+ try {
58
+ fn?.()
59
+ } catch {
60
+ /* the consumer's callback threw; the connection carries on */
61
+ }
62
+ }
63
+
64
+ export type TransportAdapter = {
65
+ connect(url: string, opts: TransportOpts): TransportConnection
66
+ }
67
+
68
+ /** The transport the adapter at `index` of a chain stands for: WebSocket, then SSE, then long polling. */
69
+ export function transportAt(index: number): Transport {
70
+ if (index === 0) return "ws"
71
+ return index === 1 ? "sse" : "longpoll"
72
+ }
73
+
74
+ export type KeepaliveOptions = {
75
+ interval?: number
76
+ onDead: () => void
77
+ sendPing: () => void
78
+ timeout?: number
79
+ transport: Transport
80
+ }
81
+
82
+ export type KeepaliveLoop = { onFrame(): void; onPong(): void; start(): void; stop(): void }
83
+
84
+ /**
85
+ * A connection's liveness: over WebSocket a ping every `interval` and dead after `timeout` without
86
+ * a pong; over SSE dead after `timeout` without a frame; long polling needs none.
87
+ */
88
+ class Keepalive implements KeepaliveLoop {
89
+ readonly #opts: KeepaliveOptions
90
+ #ping: ReturnType<typeof setInterval> | null = null
91
+ #dead: ReturnType<typeof setTimeout> | null = null
92
+
93
+ constructor(opts: KeepaliveOptions) {
94
+ this.#opts = opts
95
+ }
96
+
97
+ #armDead(): void {
98
+ if (this.#dead !== null) clearTimeout(this.#dead)
99
+ this.#dead = setTimeout(() => this.#opts.onDead(), this.#opts.timeout ?? 60_000)
100
+ }
101
+
102
+ start(): void {
103
+ this.stop()
104
+ const { transport } = this.#opts
105
+ if (transport === "longpoll") return
106
+ if (transport === "ws") this.#ping = setInterval(() => this.#opts.sendPing(), this.#opts.interval ?? 25_000)
107
+ this.#armDead()
108
+ }
109
+
110
+ stop(): void {
111
+ if (this.#ping !== null) clearInterval(this.#ping)
112
+ if (this.#dead !== null) clearTimeout(this.#dead)
113
+ this.#ping = null
114
+ this.#dead = null
115
+ }
116
+
117
+ onPong(): void {
118
+ if (this.#dead !== null) this.#armDead()
119
+ }
120
+
121
+ onFrame(): void {
122
+ if (this.#opts.transport === "sse" && this.#dead !== null) this.#armDead()
123
+ }
124
+ }
125
+
126
+ export function createKeepaliveLoop(opts: KeepaliveOptions): KeepaliveLoop {
127
+ return new Keepalive(opts)
128
+ }
129
+
130
+ /** The keepalive `set` gives `transport` (WebSocket 20 s unless set, SSE none; 0 none), dead an interval after a ping. */
131
+ export function keepaliveOf(
132
+ set: number | undefined,
133
+ transport: Transport,
134
+ conn: { send(data: string): void },
135
+ onDead: () => void,
136
+ ): KeepaliveLoop | null {
137
+ const interval = set ?? (transport === "ws" ? 20_000 : 0)
138
+ if (interval <= 0) return null
139
+ const sendPing = () => conn.send(JSON.stringify({ t: "ping" }))
140
+ return createKeepaliveLoop({ interval, onDead, sendPing, timeout: 2 * interval, transport })
141
+ }
142
+
143
+ /** Dial `adapter` and resolve once it sends `ready`; reject on an error, a close or `timeout`. */
144
+ export function dialReady(
145
+ adapter: TransportAdapter,
146
+ target: { opts: TransportOpts; url: string },
147
+ timeout: number,
148
+ ): Promise<TransportConnection> {
149
+ return new Promise((resolve, reject) => {
150
+ const conn = adapter.connect(target.url, target.opts)
151
+ const timer = setTimeout(() => {
152
+ conn.close()
153
+ reject(new Error("timeout"))
154
+ }, timeout)
155
+ const onFrame = conn.onFrame
156
+ conn.onFrame = (frame) => {
157
+ if (frame.t !== "ready") return onFrame(frame)
158
+ clearTimeout(timer)
159
+ conn.onFrame = onFrame
160
+ resolve(conn)
161
+ }
162
+ conn.onError = (err) => {
163
+ clearTimeout(timer)
164
+ conn.close()
165
+ reject(err)
166
+ }
167
+ conn.onClose = (reason) => {
168
+ clearTimeout(timer)
169
+ reject(new Error(reason))
170
+ }
171
+ })
172
+ }
173
+
174
+ /** The dial statuses no retry changes, most specific highest; a WebSocket refused without a status ranks lowest. */
175
+ const RANK: Record<number, number> = { 400: 3, 401: 6, 403: 5, 404: 1, 405: 1, 410: 2, 422: 4 }
176
+ const rank = (err: unknown) =>
177
+ RANK[(err as { status?: number } | null)?.status ?? 0] ?? Number((err as RealtimeError)?.reason === "upgrade_refused")
178
+ export const refused = (err: unknown): boolean => rank(err) > 0
179
+ /** Dial 0, 1, … until one opens; else the last transient failure, or the most specific refusal. A 401 refreshes once. */
180
+ export async function walkChain<T>(
181
+ count: number,
182
+ dial: (index: number) => Promise<T>,
183
+ opened: (index: number) => unknown,
184
+ refresh?: () => Promise<boolean>,
185
+ ): Promise<T> {
186
+ let transient: unknown
187
+ let refusal: unknown
188
+ for (let i = 0; i < count; i++) {
189
+ try {
190
+ const conn = await dial(i)
191
+ opened(i)
192
+ return conn
193
+ } catch (err) {
194
+ if (rank(err) === 6 && refresh) {
195
+ const fresh = await refresh()
196
+ refresh = undefined
197
+ if (fresh) {
198
+ ;[i, transient, refusal] = [-1, undefined, undefined]
199
+ continue
200
+ }
201
+ }
202
+ if (!refused(err)) transient = err
203
+ else if (rank(err) > rank(refusal)) refusal = err
204
+ }
205
+ }
206
+ throw transient ?? refusal
207
+ }
208
+
209
+ export type FallbackChain = {
210
+ connect(url: string, transportOpts: TransportOpts): Promise<{ conn: TransportConnection; transport: Transport }>
211
+ readonly provenTransport: Transport | null
212
+ }
213
+
214
+ /** Try each transport in order until one is ready; remember the one that worked and try it first next time. */
215
+ class Chain implements FallbackChain {
216
+ readonly #transports: TransportAdapter[]
217
+ readonly #timeout: number
218
+ #proven: number | null = null
219
+
220
+ constructor(opts: { timeout?: number; transports: TransportAdapter[] }) {
221
+ this.#transports = opts.transports
222
+ this.#timeout = opts.timeout ?? 3000
223
+ }
224
+
225
+ get provenTransport(): Transport | null {
226
+ return this.#proven === null ? null : transportAt(this.#proven)
227
+ }
228
+
229
+ async #dial(url: string, opts: TransportOpts) {
230
+ const proven = this.#proven
231
+ if (proven !== null && proven < this.#transports.length) {
232
+ try {
233
+ const conn = await dialReady(this.#transports[proven] as TransportAdapter, { opts, url }, this.#timeout)
234
+ return { conn, transport: transportAt(proven) }
235
+ } catch {
236
+ /* the proven transport failed: fall through to the whole chain */
237
+ }
238
+ }
239
+ for (let i = 0; i < this.#transports.length; i++) {
240
+ try {
241
+ const conn = await dialReady(this.#transports[i] as TransportAdapter, { opts, url }, this.#timeout)
242
+ this.#proven = i
243
+ return { conn, transport: transportAt(i) }
244
+ } catch {
245
+ /* try the next one */
246
+ }
247
+ }
248
+ throw new RealtimeConnectError("All transports failed", "all_failed")
249
+ }
250
+
251
+ connect(url: string, opts: TransportOpts): Promise<{ conn: TransportConnection; transport: Transport }> {
252
+ const attempt =
253
+ this.#transports.length === 0
254
+ ? Promise.reject(new RealtimeConnectError("No transports configured", "no_transports"))
255
+ : this.#dial(url, opts)
256
+ /* a caller that never awaits the attempt must not see an unhandled rejection */
257
+ attempt.catch(() => {})
258
+ return attempt
259
+ }
260
+ }
261
+
262
+ export function createFallbackChain(opts: { timeout?: number; transports: TransportAdapter[] }): FallbackChain {
263
+ return new Chain(opts)
264
+ }
265
+
266
+ export type ResumableConnectionOpts = {
267
+ fallbackTimeout?: number | undefined
268
+ /** Each dial's headers; a function is read on every dial, so a rotated token reaches the reconnect. */
269
+ headers?: Record<string, string> | (() => Record<string, string> | Promise<Record<string, string>>) | undefined
270
+ /** Ping every this many ms (WebSocket 20 s, SSE none, unless set; 0: none); one unanswered as long reconnects. */
271
+ keepaliveInterval?: number | undefined
272
+ lastEventId?: string | undefined
273
+ /** Reconnects after a drop before the stream ends; 0 retries forever. Default 5. */
274
+ maxReconnectAttempts?: number | undefined
275
+ onReconnected?: (() => void) | undefined
276
+ onReconnecting?: ((attempt: number, transport: Transport) => void) | undefined
277
+ reconnectDelayMs?: number | undefined
278
+ reconnectToken?: string | undefined
279
+ signal?: AbortSignal | undefined
280
+ transports: TransportAdapter[]
281
+ url: string
282
+ }
283
+
284
+ export type ResumableConnection<T = unknown> = {
285
+ close(reason?: string): void
286
+ readonly provenTransport: Transport | null
287
+ /** Resolves once sent; over SSE and long-poll a POST, rejecting with its error. */
288
+ send(data: unknown): Promise<void>
289
+ /** `send`'s raw form: `text` as it is, a text frame or a POSTed text/plain body. */
290
+ sendText(text: string): Promise<void>
291
+ readonly state: ConnectionState
292
+ [Symbol.asyncIterator](): AsyncIterableIterator<T>
293
+ }
@@ -0,0 +1,17 @@
1
+ /**
2
+ * Headers a request may carry to another origin when a `redirect: "follow"` client follows a
3
+ * cross-origin redirect. Everything else is dropped on that hop: credentials, the auth header and
4
+ * every header set from config, per call or by an `onRequest` hook, whatever its name. An
5
+ * allowlist, so a header added later never leaks by default (H18.10).
6
+ */
7
+ export const CROSS_ORIGIN_SAFE_HEADERS: readonly string[] = ["accept", "accept-language", "user-agent"]
8
+
9
+ /** The headers to send on a cross-origin hop: only the allowlisted ones from `headers`. */
10
+ export function crossOriginHeaders(headers: Headers): Headers {
11
+ const out = new Headers()
12
+ for (const name of CROSS_ORIGIN_SAFE_HEADERS) {
13
+ const value = headers.get(name)
14
+ if (value !== null) out.set(name, value)
15
+ }
16
+ return out
17
+ }
@@ -0,0 +1,74 @@
1
+ import type { RedirectPolicy } from "./config.ts"
2
+ import { crossOriginHeaders } from "./redirect-policy.ts"
3
+ import { isReplayable } from "./request.ts"
4
+
5
+ const REDIRECT_STATUSES: readonly number[] = [301, 302, 303, 307, 308]
6
+ const MAX_REDIRECTS = 20
7
+
8
+ /** Browsers hide redirect targets from script (`opaqueredirect`), so the platform must follow. */
9
+ function platformFollowsRedirects(): boolean {
10
+ const g = globalThis as { document?: unknown; WorkerGlobalScope?: unknown }
11
+ return typeof g.document !== "undefined" || typeof g.WorkerGlobalScope !== "undefined"
12
+ }
13
+
14
+ /** The request for the next hop, or `null` when this redirect must be returned unfollowed. */
15
+ function nextHop(response: Response, init: RequestInit, crossOrigin: boolean): RequestInit | null {
16
+ const next: RequestInit = { ...init, headers: new Headers(init.headers) }
17
+ if (response.status === 307 || response.status === 308) {
18
+ /* a body is replayed only to the same origin, and only when it can be sent twice */
19
+ if (init.body != null && (crossOrigin || !isReplayable(init.body))) return null
20
+ } else {
21
+ /* 303, and 301/302 for POST: the browser behavior is GET without a body */
22
+ const method = (init.method ?? "GET").toUpperCase()
23
+ if (response.status === 303 ? method !== "HEAD" : method === "POST") {
24
+ next.method = "GET"
25
+ delete next.body
26
+ ;(next.headers as Headers).delete("content-type")
27
+ }
28
+ }
29
+ if (crossOrigin) next.headers = crossOriginHeaders(next.headers as Headers)
30
+ return next
31
+ }
32
+
33
+ /** Where a redirect leads, when it is one this client follows: http(s), and same-origin unless `follow`. */
34
+ function redirectTarget(response: Response, current: URL, origin: string, policy: RedirectPolicy): URL | null {
35
+ const location = response.headers.get("location")
36
+ if (!REDIRECT_STATUSES.includes(response.status) || location === null) return null
37
+ let target: URL
38
+ try {
39
+ target = new URL(location, current)
40
+ } catch {
41
+ /* an unparseable Location is returned as the answer it is */
42
+ return null
43
+ }
44
+ if (target.protocol !== "http:" && target.protocol !== "https:") return null
45
+ return target.origin !== origin && policy !== "follow" ? null : target
46
+ }
47
+
48
+ /**
49
+ * Send `init` to `url`, following redirects as `policy` says: hop by hop, so a cross-origin hop
50
+ * carries only the safe headers and never a body.
51
+ */
52
+ export async function fetchFollowing(
53
+ fetch: typeof globalThis.fetch,
54
+ url: URL,
55
+ init: RequestInit,
56
+ { origin, policy }: { origin: string; policy: RedirectPolicy },
57
+ ): Promise<Response> {
58
+ if (policy === "manual" || policy === "error" || platformFollowsRedirects()) {
59
+ return fetch(url.toString(), policy === "same-origin" ? init : { ...init, redirect: policy })
60
+ }
61
+ let current = url
62
+ let currentInit: RequestInit = { ...init, redirect: "manual" }
63
+ for (let hop = 0; ; hop++) {
64
+ const response = await fetch(current.toString(), currentInit)
65
+ const target = hop >= MAX_REDIRECTS ? null : redirectTarget(response, current, origin, policy)
66
+ const next = target === null ? null : nextHop(response, currentInit, target.origin !== origin)
67
+ if (target === null || next === null) return response
68
+ await response.body?.cancel().catch(() => {
69
+ /* the redirect's own body is never read */
70
+ })
71
+ current = target
72
+ currentInit = next
73
+ }
74
+ }
@@ -0,0 +1,239 @@
1
+ import type { ClientConfig } from "./config.ts"
2
+ import { ClientError } from "./error.ts"
3
+
4
+ /** What one call sends beyond the path: its parameters, headers, body and timing. */
5
+ export type RequestOptions = {
6
+ /** a base URL for this call alone, in place of `config.baseURL` (C2) */
7
+ baseURL?: string | undefined
8
+ /** raw bytes sent as they are, `application/octet-stream` unless a content type is set */
9
+ body?: ArrayBuffer | Blob | ReadableStream<Uint8Array> | Uint8Array | undefined
10
+ cookies?: Record<string, string> | undefined
11
+ form?: Record<string, unknown> | undefined
12
+ headers?: Record<string, string | undefined> | undefined
13
+ /** the `Idempotency-Key` an idempotent operation sends; a fresh one per call otherwise */
14
+ idempotencyKey?: string | undefined
15
+ json?: unknown
16
+ lastEventId?: string | undefined
17
+ params?: Record<string, string> | undefined
18
+ /** resolve the unread `Response` instead of its parsed body (C2); throw mode still throws on non-2xx */
19
+ raw?: boolean | undefined
20
+ search?: Record<string, unknown> | undefined
21
+ signal?: AbortSignal | undefined
22
+ /** Per-call timeout in milliseconds; overrides `config.timeout`, `0` disables it. */
23
+ timeout?: number | undefined
24
+ }
25
+
26
+ /** How a request is read: by its answer (`auto`), as a value (`rest`) or as an event stream (`stream`). */
27
+ export type RequestKind = "auto" | "rest" | "stream"
28
+
29
+ /** A query or form value as text: a Date by its ISO string; nothing for null, undefined or a symbol. */
30
+ function coerceParam(v: unknown): string | null {
31
+ if (v === undefined || v === null) return null
32
+ if (v instanceof Date) return v.toISOString()
33
+ if (typeof v === "symbol") return null
34
+ return String(v)
35
+ }
36
+
37
+ function coerceFormValue(key: string, v: unknown): string | null {
38
+ if (v !== null && typeof v === "object" && !(v instanceof Date)) {
39
+ throw new TypeError(
40
+ `Form field ${JSON.stringify(key)}: nested objects cannot be form-encoded; pass a string (e.g. JSON.stringify) or use json`,
41
+ )
42
+ }
43
+ return coerceParam(v)
44
+ }
45
+
46
+ /** The query of `query`: an array repeats its key, null and undefined are left out. */
47
+ export function defaultSearchParams(query: Record<string, unknown>): URLSearchParams {
48
+ const params = new URLSearchParams()
49
+ for (const [k, v] of Object.entries(query)) {
50
+ if (v === undefined || v === null) continue
51
+ if (Array.isArray(v)) {
52
+ for (const item of v) {
53
+ const s = coerceParam(item)
54
+ if (s !== null) params.append(k, s)
55
+ }
56
+ } else {
57
+ const s = coerceParam(v)
58
+ if (s !== null) params.set(k, s)
59
+ }
60
+ }
61
+ return params
62
+ }
63
+
64
+ /* RFC 9110's token: what a header name may hold */
65
+ const TOKEN = /^[!#$%&'*+.^_`|~0-9A-Za-z-]+$/
66
+
67
+ /** `value` for the header `name`, else a TypeError naming the header, never the value (Node's Headers quotes it). */
68
+ export function headerValue(name: string, value: string): string {
69
+ if (!TOKEN.test(name)) throw new TypeError(`the ${name} header cannot be sent`)
70
+ for (let i = 0; i < value.length; i++) {
71
+ const cp = value.charCodeAt(i)
72
+ if ((cp < 0x20 && cp !== 0x09) || cp === 0x7f) throw new TypeError(`the ${name} header's value cannot be sent`)
73
+ }
74
+ return value
75
+ }
76
+
77
+ /** Each of `layers` over the last, checked; `cookies` joined on; a fresh `x-request-id` unless one is set. */
78
+ export function headersOf(
79
+ layers: Record<string, string | undefined>[],
80
+ cookies: Record<string, string> = {},
81
+ requestId: boolean,
82
+ ): Headers {
83
+ const headers = new Headers()
84
+ for (const layer of layers)
85
+ for (const [k, v] of Object.entries(layer)) if (v !== undefined) headers.set(k, headerValue(k, v))
86
+ const pairs = Object.entries(cookies)
87
+ .map(([k, v]) => `${encodeURIComponent(k)}=${encodeURIComponent(v)}`)
88
+ .join("; ")
89
+ if (pairs) {
90
+ const existing = headers.get("cookie")
91
+ headers.set("cookie", existing ? `${existing}; ${pairs}` : pairs)
92
+ }
93
+ if (requestId && !headers.has("x-request-id")) headers.set("x-request-id", newClientRequestId())
94
+ return headers
95
+ }
96
+
97
+ /**
98
+ * RFC 4122 §4.4 UUID for `x-request-id`: `crypto.randomUUID` where it exists, else
99
+ * `getRandomValues`, so older runtimes still work without node:crypto.
100
+ */
101
+ export function newClientRequestId(): string {
102
+ const c = globalThis.crypto as Crypto | undefined
103
+ if (c && typeof c.randomUUID === "function") return c.randomUUID()
104
+ if (c && typeof c.getRandomValues === "function") {
105
+ const bytes = c.getRandomValues(new Uint8Array(16))
106
+ bytes[6] = ((bytes[6] as number) & 0x0f) | 0x40
107
+ bytes[8] = ((bytes[8] as number) & 0x3f) | 0x80
108
+ let out = ""
109
+ for (let i = 0; i < 16; i++) {
110
+ if (i === 4 || i === 6 || i === 8 || i === 10) out += "-"
111
+ out += (bytes[i] as number).toString(16).padStart(2, "0")
112
+ }
113
+ return out
114
+ }
115
+ throw new Error("honey: no crypto.randomUUID or crypto.getRandomValues in this runtime")
116
+ }
117
+
118
+ function isFileLike(v: unknown): boolean {
119
+ return (typeof File !== "undefined" && v instanceof File) || (typeof Blob !== "undefined" && v instanceof Blob)
120
+ }
121
+
122
+ function isFileList(v: unknown): v is ArrayLike<File> {
123
+ return typeof FileList !== "undefined" && v instanceof FileList
124
+ }
125
+
126
+ /** A form with a file in it, as multipart: fetch sets the boundary, so no content type is set here. */
127
+ function multipartBody(entries: [string, unknown][]): FormData {
128
+ const fd = new FormData()
129
+ for (const [k, v] of entries) {
130
+ if (v === undefined || v === null) continue
131
+ const items = isFileList(v) ? Array.from(v) : Array.isArray(v) ? v : [v]
132
+ for (const item of items) {
133
+ if (isFileLike(item)) fd.append(k, item as Blob)
134
+ else {
135
+ const s = coerceFormValue(k, item)
136
+ if (s !== null) fd.append(k, s)
137
+ }
138
+ }
139
+ }
140
+ return fd
141
+ }
142
+
143
+ /** The body `opts` declares, setting its content type on `headers`; both encodings repeat a key per array item. */
144
+ export function encodeBody(opts: RequestOptions, headers: Headers): BodyInit | undefined {
145
+ if (opts.body !== undefined) {
146
+ if (!headers.has("content-type")) headers.set("content-type", "application/octet-stream")
147
+ return opts.body as BodyInit
148
+ }
149
+ if (opts.json !== undefined) {
150
+ headers.set("content-type", "application/json")
151
+ try {
152
+ return JSON.stringify(opts.json)
153
+ } catch (e) {
154
+ /* nothing was sent: a status-0 error, a result in safe mode like any other failure */
155
+ const message = `JSON serialization failed: ${e instanceof Error ? e.message : String(e)}`
156
+ throw new ClientError({ body: opts.json, message, response: Response.error(), status: 0 })
157
+ }
158
+ }
159
+ if (opts.form === undefined) return undefined
160
+ const entries = Object.entries(opts.form)
161
+ if (entries.some(([, v]) => isFileLike(v) || isFileList(v) || (Array.isArray(v) && v.some(isFileLike)))) {
162
+ return multipartBody(entries)
163
+ }
164
+ headers.set("content-type", "application/x-www-form-urlencoded")
165
+ const params = new URLSearchParams()
166
+ for (const [k, v] of entries) {
167
+ for (const item of Array.isArray(v) ? v : [v]) {
168
+ const s = coerceFormValue(k, item)
169
+ if (s !== null) params.append(k, s)
170
+ }
171
+ }
172
+ return params.toString()
173
+ }
174
+
175
+ /** A body that can be sent a second time (retry, redirect). Streams are consumed by the first send. */
176
+ export function isReplayable(body: BodyInit | null | undefined): boolean {
177
+ return !(typeof ReadableStream !== "undefined" && body instanceof ReadableStream)
178
+ }
179
+
180
+ function timeoutError(ms: number): Error {
181
+ return typeof DOMException === "function"
182
+ ? new DOMException(`Request timed out after ${ms} ms`, "TimeoutError")
183
+ : Object.assign(new Error(`Request timed out after ${ms} ms`), { name: "TimeoutError" })
184
+ }
185
+
186
+ /** A request's signal: the caller's abort, the client's and the timeout as one, alive until `done()`. */
187
+ export type RequestSignal = {
188
+ /** release the timer and the listeners; call once the body has been read or abandoned */
189
+ done: () => void
190
+ /** stop the timeout but keep the cancellations (the answer turned out to be a stream) */
191
+ exemptFromTimeout: () => void
192
+ signal: AbortSignal | undefined
193
+ }
194
+
195
+ /**
196
+ * The signal for one request, aborting with the first of `sources` (the caller's, the client's
197
+ * dispose) or the timeout: a stream has no timeout; `timeout` 0 or less disables it. Listeners,
198
+ * not `AbortSignal.any`: a combined signal only weakly held can be collected before it fires.
199
+ */
200
+ export function requestSignal(
201
+ sources: readonly (AbortSignal | undefined)[],
202
+ timeout: number | undefined,
203
+ ): RequestSignal {
204
+ const signals = sources.filter((signal): signal is AbortSignal => signal !== undefined)
205
+ const timed = timeout !== undefined && timeout > 0
206
+ if (!timed && signals.length <= 1) return { done: () => {}, exemptFromTimeout: () => {}, signal: signals[0] }
207
+ const ctrl = new AbortController()
208
+ const timer = timed ? setTimeout(() => ctrl.abort(timeoutError(timeout)), timeout) : undefined
209
+ ;(timer as unknown as { unref?: () => void } | undefined)?.unref?.()
210
+ const listeners = signals.map((signal) => {
211
+ const onAbort = () => ctrl.abort(signal.reason)
212
+ if (signal.aborted) ctrl.abort(signal.reason)
213
+ else signal.addEventListener("abort", onAbort, { once: true })
214
+ return () => signal.removeEventListener("abort", onAbort)
215
+ })
216
+ return {
217
+ done: () => {
218
+ clearTimeout(timer)
219
+ for (const remove of listeners) remove()
220
+ },
221
+ exemptFromTimeout: () => clearTimeout(timer),
222
+ signal: ctrl.signal,
223
+ }
224
+ }
225
+
226
+ /** The fetch init for one send: credentials and mode from the config, half-duplex for a stream body. */
227
+ export function requestInit(
228
+ at: { body: BodyInit | undefined; headers: Headers; method: string; signal: AbortSignal | undefined },
229
+ config: Pick<ClientConfig, "credentials" | "mode">,
230
+ ): RequestInit {
231
+ const init: RequestInit = { headers: at.headers, method: at.method }
232
+ if (at.body !== undefined) init.body = at.body
233
+ if (at.signal !== undefined) init.signal = at.signal
234
+ if (config.credentials) init.credentials = config.credentials
235
+ if (config.mode) init.mode = config.mode
236
+ /* a stream body needs half-duplex on Node's fetch */
237
+ if (at.body !== undefined && !isReplayable(at.body)) (init as RequestInit & { duplex?: string }).duplex = "half"
238
+ return init
239
+ }