@pikku/core 0.11.1 → 0.12.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 (363) hide show
  1. package/CHANGELOG.md +55 -0
  2. package/dist/crypto-utils.d.ts +2 -0
  3. package/dist/crypto-utils.js +78 -0
  4. package/dist/errors/error-handler.js +3 -3
  5. package/dist/errors/errors.d.ts +8 -0
  6. package/dist/errors/errors.js +16 -0
  7. package/dist/errors/index.d.ts +0 -1
  8. package/dist/errors/index.js +0 -1
  9. package/dist/function/function-runner.d.ts +10 -3
  10. package/dist/function/function-runner.js +136 -31
  11. package/dist/function/functions.types.d.ts +35 -12
  12. package/dist/function/functions.types.js +16 -6
  13. package/dist/function/index.d.ts +2 -2
  14. package/dist/function/index.js +1 -2
  15. package/dist/handle-error.js +1 -10
  16. package/dist/index.d.ts +30 -24
  17. package/dist/index.js +19 -27
  18. package/dist/middleware/auth-apikey.d.ts +1 -1
  19. package/dist/middleware/auth-apikey.js +3 -3
  20. package/dist/middleware/auth-bearer.d.ts +1 -1
  21. package/dist/middleware/auth-bearer.js +3 -6
  22. package/dist/middleware/auth-cookie.d.ts +1 -1
  23. package/dist/middleware/auth-cookie.js +6 -8
  24. package/dist/middleware/cors.d.ts +46 -0
  25. package/dist/middleware/cors.js +73 -0
  26. package/dist/middleware/index.d.ts +5 -3
  27. package/dist/middleware/index.js +5 -3
  28. package/dist/middleware/remote-auth.d.ts +5 -0
  29. package/dist/middleware/remote-auth.js +55 -0
  30. package/dist/middleware/timeout.d.ts +1 -1
  31. package/dist/middleware-runner.d.ts +3 -14
  32. package/dist/middleware-runner.js +14 -16
  33. package/dist/permissions.d.ts +3 -86
  34. package/dist/permissions.js +20 -80
  35. package/dist/pikku-state.d.ts +33 -104
  36. package/dist/pikku-state.js +150 -66
  37. package/dist/schema.d.ts +8 -6
  38. package/dist/schema.js +31 -18
  39. package/dist/services/ai-agent-runner-service.d.ts +24 -0
  40. package/dist/services/ai-agent-runner-service.js +1 -0
  41. package/dist/services/ai-run-state-service.d.ts +13 -0
  42. package/dist/services/ai-run-state-service.js +1 -0
  43. package/dist/services/ai-storage-service.d.ts +18 -0
  44. package/dist/services/ai-storage-service.js +1 -0
  45. package/dist/services/deployment-service.d.ts +19 -0
  46. package/dist/services/deployment-service.js +1 -0
  47. package/dist/services/gopass-secrets.d.ts +47 -0
  48. package/dist/services/gopass-secrets.js +94 -0
  49. package/dist/services/in-memory-trigger-service.d.ts +23 -0
  50. package/dist/services/in-memory-trigger-service.js +50 -0
  51. package/dist/services/in-memory-workflow-service.d.ts +59 -0
  52. package/dist/services/in-memory-workflow-service.js +287 -0
  53. package/dist/services/index.d.ts +25 -11
  54. package/dist/services/index.js +10 -13
  55. package/dist/services/local-secrets.d.ts +23 -0
  56. package/dist/services/local-secrets.js +45 -0
  57. package/dist/services/local-variables.d.ts +5 -0
  58. package/dist/services/local-variables.js +18 -0
  59. package/dist/services/scheduler-service.d.ts +21 -1
  60. package/dist/services/scheduler-service.js +14 -0
  61. package/dist/services/schema-service.d.ts +6 -0
  62. package/dist/services/scoped-secret-service.d.ts +16 -0
  63. package/dist/services/scoped-secret-service.js +35 -0
  64. package/dist/services/secret-service.d.ts +25 -4
  65. package/dist/services/trigger-service.d.ts +23 -0
  66. package/dist/services/trigger-service.js +1 -0
  67. package/dist/services/typed-secret-service.d.ts +30 -0
  68. package/dist/services/typed-secret-service.js +40 -0
  69. package/dist/services/typed-variables-service.d.ts +27 -0
  70. package/dist/services/typed-variables-service.js +46 -0
  71. package/dist/services/user-session-service.d.ts +13 -0
  72. package/dist/services/user-session-service.js +17 -0
  73. package/dist/services/variables-service.d.ts +5 -0
  74. package/dist/services/workflow-service.d.ts +43 -0
  75. package/dist/services/workflow-service.js +1 -0
  76. package/dist/types/core.types.d.ts +54 -10
  77. package/dist/types/core.types.js +7 -0
  78. package/dist/types/state.types.d.ts +127 -0
  79. package/dist/types/state.types.js +1 -0
  80. package/dist/utils.d.ts +10 -1
  81. package/dist/utils.js +60 -3
  82. package/dist/version.d.ts +6 -0
  83. package/dist/version.js +19 -0
  84. package/dist/wirings/ai-agent/ai-agent-memory.d.ts +29 -0
  85. package/dist/wirings/ai-agent/ai-agent-memory.js +199 -0
  86. package/dist/wirings/ai-agent/ai-agent-model-config.d.ts +9 -0
  87. package/dist/wirings/ai-agent/ai-agent-model-config.js +36 -0
  88. package/dist/wirings/ai-agent/ai-agent-prepare.d.ts +60 -0
  89. package/dist/wirings/ai-agent/ai-agent-prepare.js +312 -0
  90. package/dist/wirings/ai-agent/ai-agent-registry.d.ts +15 -0
  91. package/dist/wirings/ai-agent/ai-agent-registry.js +47 -0
  92. package/dist/wirings/ai-agent/ai-agent-runner.d.ts +3 -0
  93. package/dist/wirings/ai-agent/ai-agent-runner.js +96 -0
  94. package/dist/wirings/ai-agent/ai-agent-stream.d.ts +11 -0
  95. package/dist/wirings/ai-agent/ai-agent-stream.js +503 -0
  96. package/dist/wirings/ai-agent/ai-agent.types.d.ts +254 -0
  97. package/dist/wirings/ai-agent/ai-agent.types.js +1 -0
  98. package/dist/wirings/ai-agent/index.d.ts +5 -0
  99. package/dist/wirings/ai-agent/index.js +4 -0
  100. package/dist/wirings/channel/channel-common.d.ts +7 -4
  101. package/dist/wirings/channel/channel-common.js +15 -8
  102. package/dist/wirings/channel/channel-handler.js +13 -4
  103. package/dist/wirings/channel/channel-middleware-runner.d.ts +9 -0
  104. package/dist/wirings/channel/channel-middleware-runner.js +73 -0
  105. package/dist/wirings/channel/channel-runner.js +12 -12
  106. package/dist/wirings/channel/channel.types.d.ts +5 -1
  107. package/dist/wirings/channel/define-channel-routes.d.ts +21 -0
  108. package/dist/wirings/channel/define-channel-routes.js +23 -0
  109. package/dist/wirings/channel/index.d.ts +10 -8
  110. package/dist/wirings/channel/index.js +7 -8
  111. package/dist/wirings/channel/local/index.d.ts +3 -3
  112. package/dist/wirings/channel/local/index.js +3 -3
  113. package/dist/wirings/channel/local/local-channel-runner.js +18 -6
  114. package/dist/wirings/channel/log-channels.js +1 -1
  115. package/dist/wirings/channel/serverless/index.d.ts +1 -1
  116. package/dist/wirings/channel/serverless/index.js +1 -1
  117. package/dist/wirings/channel/serverless/serverless-channel-runner.js +17 -6
  118. package/dist/wirings/cli/channel/cli-channel-runner.js +3 -2
  119. package/dist/wirings/cli/channel/index.d.ts +1 -1
  120. package/dist/wirings/cli/channel/index.js +1 -1
  121. package/dist/wirings/cli/cli-runner.d.ts +1 -1
  122. package/dist/wirings/cli/cli-runner.js +22 -18
  123. package/dist/wirings/cli/cli.types.d.ts +7 -6
  124. package/dist/wirings/cli/define-cli-commands.d.ts +19 -0
  125. package/dist/wirings/cli/define-cli-commands.js +20 -0
  126. package/dist/wirings/cli/index.d.ts +2 -3
  127. package/dist/wirings/cli/index.js +1 -3
  128. package/dist/wirings/http/http-routes.d.ts +69 -0
  129. package/dist/wirings/http/http-routes.js +139 -0
  130. package/dist/wirings/http/http-runner.d.ts +2 -2
  131. package/dist/wirings/http/http-runner.js +62 -27
  132. package/dist/wirings/http/http.types.d.ts +77 -24
  133. package/dist/wirings/http/index.d.ts +5 -4
  134. package/dist/wirings/http/index.js +4 -3
  135. package/dist/wirings/http/log-http-routes.js +1 -1
  136. package/dist/wirings/http/routers/path-to-regex.js +2 -2
  137. package/dist/wirings/mcp/index.d.ts +4 -3
  138. package/dist/wirings/mcp/index.js +3 -3
  139. package/dist/wirings/mcp/mcp-runner.d.ts +1 -3
  140. package/dist/wirings/mcp/mcp-runner.js +42 -56
  141. package/dist/wirings/mcp/mcp.types.d.ts +3 -3
  142. package/dist/wirings/node/index.d.ts +2 -0
  143. package/dist/wirings/node/index.js +1 -0
  144. package/dist/wirings/node/node.types.d.ts +20 -0
  145. package/dist/wirings/node/node.types.js +1 -0
  146. package/dist/wirings/oauth2/index.d.ts +3 -0
  147. package/dist/wirings/oauth2/index.js +2 -0
  148. package/dist/wirings/oauth2/oauth2-client.d.ts +47 -0
  149. package/dist/wirings/oauth2/oauth2-client.js +267 -0
  150. package/dist/wirings/oauth2/oauth2.types.d.ts +65 -0
  151. package/dist/wirings/oauth2/oauth2.types.js +4 -0
  152. package/dist/wirings/oauth2/wire-oauth2-credential.d.ts +20 -0
  153. package/dist/wirings/oauth2/wire-oauth2-credential.js +21 -0
  154. package/dist/wirings/queue/index.d.ts +2 -2
  155. package/dist/wirings/queue/index.js +1 -1
  156. package/dist/wirings/queue/queue-runner.d.ts +7 -0
  157. package/dist/wirings/queue/queue-runner.js +23 -12
  158. package/dist/wirings/queue/queue.types.d.ts +8 -2
  159. package/dist/wirings/rpc/index.d.ts +1 -1
  160. package/dist/wirings/rpc/index.js +1 -1
  161. package/dist/wirings/rpc/rpc-runner.d.ts +40 -13
  162. package/dist/wirings/rpc/rpc-runner.js +191 -25
  163. package/dist/wirings/rpc/rpc-types.d.ts +15 -4
  164. package/dist/wirings/scheduler/index.d.ts +3 -3
  165. package/dist/wirings/scheduler/index.js +2 -3
  166. package/dist/wirings/scheduler/log-schedulers.js +1 -1
  167. package/dist/wirings/scheduler/scheduler-runner.d.ts +5 -0
  168. package/dist/wirings/scheduler/scheduler-runner.js +28 -10
  169. package/dist/wirings/secret/index.d.ts +3 -0
  170. package/dist/wirings/secret/index.js +2 -0
  171. package/dist/wirings/secret/secret.types.d.ts +27 -0
  172. package/dist/wirings/secret/secret.types.js +1 -0
  173. package/dist/wirings/secret/validate-secret-definitions.d.ts +6 -0
  174. package/dist/wirings/secret/validate-secret-definitions.js +55 -0
  175. package/dist/wirings/trigger/index.d.ts +3 -0
  176. package/dist/wirings/trigger/index.js +2 -0
  177. package/dist/wirings/trigger/pikku-trigger-service.d.ts +26 -0
  178. package/dist/wirings/trigger/pikku-trigger-service.js +75 -0
  179. package/dist/wirings/trigger/trigger-runner.d.ts +42 -0
  180. package/dist/wirings/trigger/trigger-runner.js +85 -0
  181. package/dist/wirings/trigger/trigger.types.d.ts +130 -0
  182. package/dist/wirings/trigger/trigger.types.js +40 -0
  183. package/dist/wirings/variable/index.d.ts +3 -0
  184. package/dist/wirings/variable/index.js +2 -0
  185. package/dist/wirings/variable/validate-variable-definitions.d.ts +6 -0
  186. package/dist/wirings/variable/validate-variable-definitions.js +45 -0
  187. package/dist/wirings/variable/variable.types.d.ts +18 -0
  188. package/dist/wirings/variable/variable.types.js +1 -0
  189. package/dist/wirings/workflow/dsl/index.d.ts +5 -0
  190. package/dist/wirings/workflow/dsl/index.js +4 -0
  191. package/dist/wirings/workflow/dsl/workflow-dsl.types.d.ts +292 -0
  192. package/dist/wirings/workflow/dsl/workflow-dsl.types.js +5 -0
  193. package/dist/wirings/workflow/dsl/workflow-runner.d.ts +5 -0
  194. package/dist/wirings/workflow/dsl/workflow-runner.js +23 -0
  195. package/dist/wirings/workflow/graph/graph-node.d.ts +117 -0
  196. package/dist/wirings/workflow/graph/graph-node.js +58 -0
  197. package/dist/wirings/workflow/graph/graph-runner.d.ts +9 -0
  198. package/dist/wirings/workflow/graph/graph-runner.js +540 -0
  199. package/dist/wirings/workflow/graph/index.d.ts +4 -0
  200. package/dist/wirings/workflow/graph/index.js +3 -0
  201. package/dist/wirings/workflow/graph/template.d.ts +15 -0
  202. package/dist/wirings/workflow/graph/template.js +27 -0
  203. package/dist/wirings/workflow/graph/wire-workflow-graph.d.ts +15 -0
  204. package/dist/wirings/workflow/graph/wire-workflow-graph.js +8 -0
  205. package/dist/wirings/workflow/graph/workflow-graph.types.d.ts +82 -0
  206. package/dist/wirings/workflow/graph/workflow-graph.types.js +15 -0
  207. package/dist/wirings/workflow/index.d.ts +7 -12
  208. package/dist/wirings/workflow/index.js +8 -11
  209. package/dist/wirings/workflow/pikku-workflow-service.d.ts +114 -10
  210. package/dist/wirings/workflow/pikku-workflow-service.js +386 -115
  211. package/dist/wirings/workflow/workflow-helpers.d.ts +36 -0
  212. package/dist/wirings/workflow/workflow-helpers.js +38 -0
  213. package/dist/wirings/workflow/workflow-utils.d.ts +1 -0
  214. package/dist/wirings/workflow/workflow-utils.js +1 -0
  215. package/dist/wirings/workflow/workflow.types.d.ts +83 -214
  216. package/package.json +13 -6
  217. package/run-tests.sh +4 -1
  218. package/src/crypto-utils.ts +99 -0
  219. package/src/errors/error-handler.ts +3 -3
  220. package/src/errors/errors.ts +17 -0
  221. package/src/errors/index.ts +0 -1
  222. package/src/factory-functions.test.ts +42 -2
  223. package/src/function/function-runner.test.ts +5 -12
  224. package/src/function/function-runner.ts +195 -40
  225. package/src/function/functions.types.ts +66 -14
  226. package/src/function/index.ts +5 -2
  227. package/src/handle-error.ts +2 -10
  228. package/src/index.ts +106 -27
  229. package/src/middleware/auth-apikey.test.ts +15 -12
  230. package/src/middleware/auth-apikey.ts +3 -3
  231. package/src/middleware/auth-bearer.test.ts +17 -14
  232. package/src/middleware/auth-bearer.ts +25 -27
  233. package/src/middleware/auth-cookie.test.ts +16 -13
  234. package/src/middleware/auth-cookie.ts +6 -8
  235. package/src/middleware/cors.ts +96 -0
  236. package/src/middleware/index.ts +5 -3
  237. package/src/middleware/remote-auth.ts +68 -0
  238. package/src/middleware-runner.test.ts +122 -0
  239. package/src/middleware-runner.ts +24 -17
  240. package/src/permissions.test.ts +102 -86
  241. package/src/permissions.ts +32 -93
  242. package/src/pikku-state.ts +168 -201
  243. package/src/run-tests-script.test.ts +49 -0
  244. package/src/schema.ts +50 -18
  245. package/src/services/ai-agent-runner-service.ts +29 -0
  246. package/src/services/ai-run-state-service.ts +20 -0
  247. package/src/services/ai-storage-service.ts +39 -0
  248. package/src/services/deployment-service.ts +22 -0
  249. package/src/services/gopass-secrets.ts +95 -0
  250. package/src/services/in-memory-trigger-service.ts +68 -0
  251. package/src/services/in-memory-workflow-service.ts +395 -0
  252. package/src/services/index.ts +49 -14
  253. package/src/services/local-secrets.ts +51 -0
  254. package/src/services/local-variables.ts +22 -0
  255. package/src/services/scheduler-service.ts +29 -1
  256. package/src/services/schema-service.ts +7 -0
  257. package/src/services/scoped-secret-service.ts +41 -0
  258. package/src/services/secret-service.ts +25 -4
  259. package/src/services/trigger-service.ts +38 -0
  260. package/src/services/typed-secret-service.ts +70 -0
  261. package/src/services/typed-variables-service.ts +81 -0
  262. package/src/services/user-session-service.ts +23 -0
  263. package/src/services/variables-service.ts +5 -0
  264. package/src/services/workflow-service.ts +94 -0
  265. package/src/types/core.types.ts +82 -8
  266. package/src/types/state.types.ts +169 -0
  267. package/src/utils.ts +74 -5
  268. package/src/version.test.ts +80 -0
  269. package/src/version.ts +25 -0
  270. package/src/wirings/ai-agent/ai-agent-memory.ts +300 -0
  271. package/src/wirings/ai-agent/ai-agent-model-config.ts +43 -0
  272. package/src/wirings/ai-agent/ai-agent-prepare.ts +470 -0
  273. package/src/wirings/ai-agent/ai-agent-registry.test.ts +37 -0
  274. package/src/wirings/ai-agent/ai-agent-registry.ts +82 -0
  275. package/src/wirings/ai-agent/ai-agent-runner.test.ts +75 -0
  276. package/src/wirings/ai-agent/ai-agent-runner.ts +137 -0
  277. package/src/wirings/ai-agent/ai-agent-stream.test.ts +246 -0
  278. package/src/wirings/ai-agent/ai-agent-stream.ts +774 -0
  279. package/src/wirings/ai-agent/ai-agent.types.ts +273 -0
  280. package/src/wirings/ai-agent/index.ts +28 -0
  281. package/src/wirings/channel/channel-common.ts +32 -19
  282. package/src/wirings/channel/channel-handler.ts +18 -5
  283. package/src/wirings/channel/channel-middleware-runner.ts +119 -0
  284. package/src/wirings/channel/channel-runner.ts +14 -15
  285. package/src/wirings/channel/channel.types.ts +17 -1
  286. package/src/wirings/channel/define-channel-routes.ts +25 -0
  287. package/src/wirings/channel/index.ts +23 -8
  288. package/src/wirings/channel/local/index.ts +3 -3
  289. package/src/wirings/channel/local/local-channel-runner.test.ts +47 -2
  290. package/src/wirings/channel/local/local-channel-runner.ts +22 -6
  291. package/src/wirings/channel/log-channels.ts +1 -1
  292. package/src/wirings/channel/serverless/index.ts +5 -1
  293. package/src/wirings/channel/serverless/serverless-channel-runner.ts +22 -6
  294. package/src/wirings/cli/channel/cli-channel-runner.ts +4 -3
  295. package/src/wirings/cli/channel/index.ts +1 -1
  296. package/src/wirings/cli/cli-runner.test.ts +28 -28
  297. package/src/wirings/cli/cli-runner.ts +27 -19
  298. package/src/wirings/cli/cli.types.ts +12 -11
  299. package/src/wirings/cli/command-parser.test.ts +8 -8
  300. package/src/wirings/cli/define-cli-commands.ts +24 -0
  301. package/src/wirings/cli/index.ts +10 -4
  302. package/src/wirings/http/http-routes.test.ts +322 -0
  303. package/src/wirings/http/http-routes.ts +198 -0
  304. package/src/wirings/http/http-runner.test.ts +5 -5
  305. package/src/wirings/http/http-runner.ts +80 -29
  306. package/src/wirings/http/http.types.ts +121 -23
  307. package/src/wirings/http/index.ts +16 -4
  308. package/src/wirings/http/log-http-routes.ts +1 -1
  309. package/src/wirings/http/routers/path-to-regex.test.ts +30 -19
  310. package/src/wirings/http/routers/path-to-regex.ts +2 -2
  311. package/src/wirings/mcp/index.ts +27 -3
  312. package/src/wirings/mcp/mcp-endpoint-registry.test.ts +3 -3
  313. package/src/wirings/mcp/mcp-runner.ts +56 -67
  314. package/src/wirings/mcp/mcp.types.ts +3 -3
  315. package/src/wirings/node/index.ts +2 -0
  316. package/src/wirings/node/node.types.ts +23 -0
  317. package/src/wirings/oauth2/index.ts +3 -0
  318. package/src/wirings/oauth2/oauth2-client.test.ts +929 -0
  319. package/src/wirings/oauth2/oauth2-client.ts +335 -0
  320. package/src/wirings/oauth2/oauth2.types.ts +69 -0
  321. package/src/wirings/oauth2/wire-oauth2-credential.ts +23 -0
  322. package/src/wirings/queue/index.ts +14 -2
  323. package/src/wirings/queue/queue-runner.test.ts +115 -73
  324. package/src/wirings/queue/queue-runner.ts +39 -13
  325. package/src/wirings/queue/queue.types.ts +12 -2
  326. package/src/wirings/rpc/index.ts +1 -1
  327. package/src/wirings/rpc/rpc-runner.ts +270 -34
  328. package/src/wirings/rpc/rpc-types.ts +21 -4
  329. package/src/wirings/scheduler/index.ts +7 -4
  330. package/src/wirings/scheduler/log-schedulers.ts +1 -1
  331. package/src/wirings/scheduler/scheduler-runner.test.ts +97 -53
  332. package/src/wirings/scheduler/scheduler-runner.ts +50 -10
  333. package/src/wirings/secret/index.ts +9 -0
  334. package/src/wirings/secret/secret.types.ts +32 -0
  335. package/src/wirings/secret/validate-secret-definitions.ts +82 -0
  336. package/src/wirings/trigger/index.ts +10 -0
  337. package/src/wirings/trigger/pikku-trigger-service.ts +134 -0
  338. package/src/wirings/trigger/trigger-runner.ts +138 -0
  339. package/src/wirings/trigger/trigger.types.ts +178 -0
  340. package/src/wirings/variable/index.ts +8 -0
  341. package/src/wirings/variable/validate-variable-definitions.ts +69 -0
  342. package/src/wirings/variable/variable.types.ts +22 -0
  343. package/src/wirings/workflow/dsl/index.ts +31 -0
  344. package/src/wirings/workflow/dsl/workflow-dsl.types.ts +320 -0
  345. package/src/wirings/workflow/dsl/workflow-runner.ts +28 -0
  346. package/src/wirings/workflow/graph/graph-node.ts +222 -0
  347. package/src/wirings/workflow/graph/graph-runner.test.ts +495 -0
  348. package/src/wirings/workflow/graph/graph-runner.ts +816 -0
  349. package/src/wirings/workflow/graph/index.ts +14 -0
  350. package/src/wirings/workflow/graph/template.ts +42 -0
  351. package/src/wirings/workflow/graph/wire-workflow-graph.ts +29 -0
  352. package/src/wirings/workflow/graph/workflow-graph.types.ts +104 -0
  353. package/src/wirings/workflow/index.ts +64 -26
  354. package/src/wirings/workflow/pikku-workflow-service.test.ts +212 -0
  355. package/src/wirings/workflow/pikku-workflow-service.ts +571 -136
  356. package/src/wirings/workflow/workflow-helpers.ts +79 -0
  357. package/src/wirings/workflow/workflow-utils.ts +0 -0
  358. package/src/wirings/workflow/workflow.types.ts +123 -279
  359. package/tsconfig.tsbuildinfo +1 -1
  360. package/dist/wirings/workflow/workflow-runner.d.ts +0 -34
  361. package/dist/wirings/workflow/workflow-runner.js +0 -65
  362. package/src/function/function-runner.ts.bak +0 -188
  363. package/src/wirings/workflow/workflow-runner.ts +0 -72
@@ -0,0 +1,25 @@
1
+ /**
2
+ * Type-safe helper for defining channel message routes that can be composed.
3
+ * Returns the routes record as-is (identity function) for use with `wireChannel`.
4
+ *
5
+ * @example
6
+ * ```typescript
7
+ * export const chatRoutes = defineChannelRoutes({
8
+ * greet: pikkuChannelFunc({ func: greetFunc }),
9
+ * farewell: pikkuChannelFunc({ func: farewellFunc }),
10
+ * })
11
+ *
12
+ * wireChannel({
13
+ * name: 'chat',
14
+ * route: '/ws/chat',
15
+ * onMessageWiring: {
16
+ * command: chatRoutes,
17
+ * }
18
+ * })
19
+ * ```
20
+ */
21
+ export function defineChannelRoutes<T extends Record<string, any>>(
22
+ routes: T
23
+ ): T {
24
+ return routes
25
+ }
@@ -1,8 +1,23 @@
1
- export * from './channel-runner.js'
2
- export * from './channel.types.js'
3
- export * from './log-channels.js'
4
- export * from './pikku-abstract-channel-handler.js'
5
- export * from './eventhub-service.js'
6
- export * from './eventhub-service.js'
7
- export * from './channel-store.js'
8
- export * from './eventhub-store.js'
1
+ export { wireChannel, openChannel } from './channel-runner.js'
2
+ export {
3
+ addChannelMiddleware,
4
+ combineChannelMiddleware,
5
+ wrapChannelWithMiddleware,
6
+ } from './channel-middleware-runner.js'
7
+ export { logChannels } from './log-channels.js'
8
+ export { PikkuAbstractChannelHandler } from './pikku-abstract-channel-handler.js'
9
+ export type { EventHubService } from './eventhub-service.js'
10
+ export { ChannelStore } from './channel-store.js'
11
+ export type { Channel } from './channel-store.js'
12
+ export { EventHubStore } from './eventhub-store.js'
13
+ export type {
14
+ ChannelsMeta,
15
+ CoreChannel,
16
+ CorePikkuChannelMiddleware,
17
+ CorePikkuChannelMiddlewareFactory,
18
+ ChannelMessageMeta,
19
+ ChannelMeta,
20
+ PikkuChannel,
21
+ PikkuChannelHandlerFactory,
22
+ } from './channel.types.js'
23
+ export { defineChannelRoutes } from './define-channel-routes.js'
@@ -1,3 +1,3 @@
1
- export * from './local-channel-handler.js'
2
- export * from './local-channel-runner.js'
3
- export * from './local-eventhub-service.js'
1
+ export { PikkuLocalChannelHandler } from './local-channel-handler.js'
2
+ export { runLocalChannel } from './local-channel-runner.js'
3
+ export { LocalEventHubService } from './local-eventhub-service.js'
@@ -101,10 +101,12 @@ const mockCreateWireServices = async () => ({
101
101
 
102
102
  beforeEach(() => {
103
103
  resetPikkuState()
104
+ httpRouter.reset()
104
105
  })
105
106
 
106
107
  afterEach(() => {
107
108
  resetPikkuState()
109
+ httpRouter.reset()
108
110
  })
109
111
 
110
112
  test('runChannel should return undefined and 404 if no matching channel is found', async () => {
@@ -130,7 +132,7 @@ test('runChannel should return undefined and 404 if no matching channel is found
130
132
  test('runChannel should return a channel handler if channel matches and no auth required', async () => {
131
133
  resetPikkuState()
132
134
 
133
- pikkuState('channel', 'meta', {
135
+ pikkuState(null, 'channel', 'meta', {
134
136
  test: {
135
137
  name: 'test',
136
138
  route: '/test-channel',
@@ -158,6 +160,49 @@ test('runChannel should return a channel handler if channel matches and no auth
158
160
 
159
161
  // Simulate opening the channel
160
162
  result.open()
163
+ })
164
+
165
+ test('runChannel should close wire services once when channel closes', async () => {
166
+ resetPikkuState()
167
+ let closeCount = 0
168
+
169
+ pikkuState(null, 'channel', 'meta', {
170
+ test: {
171
+ name: 'test',
172
+ route: '/test-channel',
173
+ },
174
+ } as any)
175
+ wireChannel({
176
+ name: 'test',
177
+ route: '/test-channel',
178
+ auth: false,
179
+ })
180
+
181
+ httpRouter.initialize()
182
+
183
+ const result = await runLocalChannel({
184
+ singletonServices: mockSingletonServices,
185
+ channelId: 'test-channel-id',
186
+ request: new PikkuMockRequest('/test-channel', 'get'),
187
+ response: new PikkuMockResponse(),
188
+ route: '/test-channel',
189
+ createWireServices: async () => ({
190
+ tracked: {
191
+ close: async () => {
192
+ closeCount += 1
193
+ },
194
+ },
195
+ }),
196
+ })
197
+
198
+ assert.ok(result)
199
+ assert.equal(closeCount, 0)
200
+
201
+ result.close()
202
+ await new Promise((resolve) => setTimeout(resolve, 0))
203
+ assert.equal(closeCount, 1)
161
204
 
162
- // TODO: Test that the opened channel works
205
+ result.close()
206
+ await new Promise((resolve) => setTimeout(resolve, 0))
207
+ assert.equal(closeCount, 1)
163
208
  })
@@ -10,7 +10,10 @@ import {
10
10
  import { PikkuLocalChannelHandler } from './local-channel-handler.js'
11
11
  import { PikkuWire, WireServices } from '../../../types/core.types.js'
12
12
  import { handleHTTPError } from '../../../handle-error.js'
13
- import { PikkuSessionService } from '../../../services/user-session-service.js'
13
+ import {
14
+ PikkuSessionService,
15
+ createMiddlewareSessionWireProps,
16
+ } from '../../../services/user-session-service.js'
14
17
  import { PikkuHTTP } from '../../http/http.types.js'
15
18
  import { runChannelLifecycleWithMiddleware } from '../channel-common.js'
16
19
 
@@ -30,6 +33,7 @@ export const runLocalChannel = async ({
30
33
  RunChannelOptions &
31
34
  RunChannelParams<unknown>): Promise<PikkuLocalChannelHandler | void> => {
32
35
  let wireServices: WireServices<typeof singletonServices> | undefined
36
+ let closedWireServices = false
33
37
 
34
38
  let channelHandler: PikkuLocalChannelHandler | undefined
35
39
  const userSession = new PikkuSessionService()
@@ -75,12 +79,23 @@ export const runLocalChannel = async ({
75
79
  openingData
76
80
  )
77
81
  const channel = channelHandler.getChannel()
78
- const wire: PikkuWire = { channel, session: userSession }
82
+ const wire: PikkuWire = {
83
+ channel,
84
+ ...createMiddlewareSessionWireProps(userSession),
85
+ }
79
86
 
80
87
  if (createWireServices) {
81
88
  wireServices = await createWireServices(singletonServices, wire)
82
89
  }
83
90
 
91
+ const closeServices = async () => {
92
+ if (!wireServices || closedWireServices) {
93
+ return
94
+ }
95
+ closedWireServices = true
96
+ await closeWireServices(singletonServices.logger, wireServices)
97
+ }
98
+
84
99
  const services = { ...singletonServices, ...wireServices }
85
100
 
86
101
  channelHandler.registerOnOpen(async () => {
@@ -94,6 +109,7 @@ export const runLocalChannel = async ({
94
109
  services,
95
110
  channel,
96
111
  data: openingData,
112
+ channelMiddlewareMeta: meta.channelMiddleware,
97
113
  })
98
114
  if (result !== undefined) {
99
115
  await channel.send(result)
@@ -115,6 +131,7 @@ export const runLocalChannel = async ({
115
131
  lifecycleType: 'disconnect',
116
132
  services,
117
133
  channel,
134
+ channelMiddlewareMeta: meta.channelMiddleware,
118
135
  })
119
136
  } catch (e) {
120
137
  singletonServices.logger.error(`Error handling onDisconnect: ${e}`)
@@ -122,9 +139,7 @@ export const runLocalChannel = async ({
122
139
  }
123
140
  }
124
141
 
125
- if (wireServices) {
126
- await closeWireServices(singletonServices.logger, wireServices)
127
- }
142
+ await closeServices()
128
143
  })
129
144
 
130
145
  const onMessage = processMessageHandlers(
@@ -153,7 +168,8 @@ export const runLocalChannel = async ({
153
168
  bubbleErrors
154
169
  )
155
170
  } finally {
156
- if (wireServices) {
171
+ if (!channelHandler && wireServices && !closedWireServices) {
172
+ closedWireServices = true
157
173
  await closeWireServices(singletonServices.logger, wireServices)
158
174
  }
159
175
  }
@@ -6,7 +6,7 @@ import { Logger } from '../../services/index.js'
6
6
  * @param logger - A logger for logging information.
7
7
  */
8
8
  export const logChannels = (logger: Logger) => {
9
- const channels = pikkuState('channel', 'channels')
9
+ const channels = pikkuState(null, 'channel', 'channels')
10
10
  if (channels.size === 0) {
11
11
  logger.info('No channels added')
12
12
  return
@@ -1 +1,5 @@
1
- export * from './serverless-channel-runner.js'
1
+ export {
2
+ runChannelConnect,
3
+ runChannelDisconnect,
4
+ runChannelMessage,
5
+ } from './serverless-channel-runner.js'
@@ -11,7 +11,10 @@ import type {
11
11
  import { createHTTPWire } from '../../http/http-runner.js'
12
12
  import { ChannelStore } from '../channel-store.js'
13
13
  import { handleHTTPError } from '../../../handle-error.js'
14
- import { PikkuSessionService } from '../../../services/user-session-service.js'
14
+ import {
15
+ PikkuSessionService,
16
+ createMiddlewareSessionWireProps,
17
+ } from '../../../services/user-session-service.js'
15
18
  import { pikkuState } from '../../../pikku-state.js'
16
19
  import { PikkuFetchHTTPRequest } from '../../http/pikku-fetch-http-request.js'
17
20
  import { PikkuHTTP } from '../../http/http.types.js'
@@ -35,9 +38,9 @@ const getVariablesForChannel = ({
35
38
  channelHandlerFactory: PikkuChannelHandlerFactory
36
39
  openingData?: unknown
37
40
  }) => {
38
- const channels = pikkuState('channel', 'channels')
41
+ const channels = pikkuState(null, 'channel', 'channels')
39
42
  const channelConfig = channels.get(channelName)
40
- const channelsMeta = pikkuState('channel', 'meta')
43
+ const channelsMeta = pikkuState(null, 'channel', 'meta')
41
44
  const meta = channelsMeta[channelName]
42
45
  if (!channelConfig) {
43
46
  throw new Error(`Channel not found: ${channelName}`)
@@ -107,7 +110,10 @@ export const runChannelConnect = async ({
107
110
  channelName: channelConfig.name,
108
111
  })
109
112
 
110
- const wire: PikkuWire = { channel, session: userSession }
113
+ const wire: PikkuWire = {
114
+ channel,
115
+ ...createMiddlewareSessionWireProps(userSession),
116
+ }
111
117
 
112
118
  if (createWireServices) {
113
119
  wireServices = await createWireServices(singletonServices, wire)
@@ -127,6 +133,7 @@ export const runChannelConnect = async ({
127
133
  services,
128
134
  channel,
129
135
  data: openingData,
136
+ channelMiddlewareMeta: meta.channelMiddleware,
130
137
  })
131
138
  }
132
139
  http?.response?.status(101)
@@ -175,12 +182,16 @@ export const runChannelDisconnect = async ({
175
182
  openingData,
176
183
  channelName,
177
184
  })
185
+
178
186
  const userSession = new PikkuSessionService(
179
187
  params.channelStore,
180
188
  params.channelId
181
189
  )
182
190
  userSession.setInitial(session)
183
- const wire: PikkuWire = { channel, session: userSession }
191
+ const wire: PikkuWire = {
192
+ channel,
193
+ ...createMiddlewareSessionWireProps(userSession),
194
+ }
184
195
 
185
196
  if (!wireServices && params.createWireServices) {
186
197
  wireServices = await params.createWireServices(singletonServices, wire)
@@ -200,6 +211,7 @@ export const runChannelDisconnect = async ({
200
211
  lifecycleType: 'disconnect',
201
212
  services,
202
213
  channel,
214
+ channelMiddlewareMeta: meta.channelMiddleware,
203
215
  })
204
216
  } catch (e: any) {
205
217
  singletonServices.logger.error(
@@ -226,12 +238,16 @@ export const runChannelMessage = async (
226
238
  openingData,
227
239
  channelName,
228
240
  })
241
+
229
242
  const userSession = new PikkuSessionService(
230
243
  params.channelStore,
231
244
  params.channelId
232
245
  )
233
246
  userSession.setInitial(session)
234
- const wire: PikkuWire = { channel, session: userSession }
247
+ const wire: PikkuWire = {
248
+ channel,
249
+ ...createMiddlewareSessionWireProps(userSession),
250
+ }
235
251
 
236
252
  if (params.createWireServices) {
237
253
  wireServices = await params.createWireServices(singletonServices, wire)
@@ -1,5 +1,5 @@
1
1
  import { pikkuState } from '../../../pikku-state.js'
2
- import { CorePikkuCLIRender, CLIMeta } from '../cli.types.js'
2
+ import { CorePikkuCLIRender, type CLIMeta } from '../cli.types.js'
3
3
  import { generateCommandHelp, parseCLIArguments } from '../command-parser.js'
4
4
 
5
5
  /**
@@ -27,12 +27,13 @@ export async function executeCLIViaChannel({
27
27
  defaultRenderer?: CorePikkuCLIRender<any>
28
28
  }): Promise<void> {
29
29
  // Get CLI metadata from state
30
- const allCLIMeta = pikkuState('cli', 'meta') as unknown as CLIMeta | undefined
31
- if (!allCLIMeta) {
30
+ const cliMeta = pikkuState(null, 'cli', 'meta')
31
+ if (!cliMeta || !('programs' in cliMeta)) {
32
32
  throw new Error(
33
33
  '[PKU342] CLI metadata not found. No CLI wirings were registered. See https://pikku.dev/docs/pikku-cli/errors/pku342 for more information.'
34
34
  )
35
35
  }
36
+ const allCLIMeta = cliMeta as CLIMeta
36
37
  const programMeta = allCLIMeta.programs[programName]
37
38
 
38
39
  if (!programMeta) {
@@ -1 +1 @@
1
- export * from './cli-channel-runner.js'
1
+ export { executeCLIViaChannel } from './cli-channel-runner.js'
@@ -30,7 +30,7 @@ describe('CLI Runner', () => {
30
30
 
31
31
  describe('runCLICommand', () => {
32
32
  test('should throw NotFoundError when program not found', async () => {
33
- pikkuState('cli', 'meta', { programs: {}, renderers: {} })
33
+ pikkuState(null, 'cli', 'meta', { programs: {}, renderers: {} })
34
34
 
35
35
  await assert.rejects(
36
36
  async () =>
@@ -45,7 +45,7 @@ describe('CLI Runner', () => {
45
45
  })
46
46
 
47
47
  test('should throw NotFoundError when command not found', async () => {
48
- pikkuState('cli', 'meta', {
48
+ pikkuState(null, 'cli', 'meta', {
49
49
  programs: {
50
50
  'test-cli': {
51
51
  program: 'test-cli',
@@ -56,7 +56,7 @@ describe('CLI Runner', () => {
56
56
  renderers: {},
57
57
  })
58
58
 
59
- pikkuState('cli', 'programs', {
59
+ pikkuState(null, 'cli', 'programs', {
60
60
  'test-cli': {
61
61
  defaultRenderer: undefined,
62
62
  middleware: [],
@@ -84,14 +84,14 @@ describe('CLI Runner', () => {
84
84
  }
85
85
 
86
86
  // Setup metadata
87
- pikkuState('cli', 'meta', {
87
+ pikkuState(null, 'cli', 'meta', {
88
88
  programs: {
89
89
  'test-cli': {
90
90
  program: 'test-cli',
91
91
  commands: {
92
92
  greet: {
93
93
  command: 'greet <name>',
94
- pikkuFuncName: 'greetFunc',
94
+ pikkuFuncId: 'greetFunc',
95
95
  positionals: [{ name: 'name', required: true }],
96
96
  options: {},
97
97
  },
@@ -102,7 +102,7 @@ describe('CLI Runner', () => {
102
102
  renderers: {},
103
103
  })
104
104
 
105
- pikkuState('cli', 'programs', {
105
+ pikkuState(null, 'cli', 'programs', {
106
106
  'test-cli': {
107
107
  defaultRenderer: undefined,
108
108
  middleware: [],
@@ -110,9 +110,9 @@ describe('CLI Runner', () => {
110
110
  },
111
111
  })
112
112
 
113
- pikkuState('function', 'meta', {
113
+ pikkuState(null, 'function', 'meta', {
114
114
  greetFunc: {
115
- pikkuFuncName: 'greetFunc',
115
+ pikkuFuncId: 'greetFunc',
116
116
  inputSchemaName: null,
117
117
  outputSchemaName: null,
118
118
  },
@@ -148,14 +148,14 @@ describe('CLI Runner', () => {
148
148
  return { success: true }
149
149
  }
150
150
 
151
- pikkuState('cli', 'meta', {
151
+ pikkuState(null, 'cli', 'meta', {
152
152
  programs: {
153
153
  'test-cli': {
154
154
  program: 'test-cli',
155
155
  commands: {
156
156
  test: {
157
157
  command: 'test',
158
- pikkuFuncName: 'testFunc',
158
+ pikkuFuncId: 'testFunc',
159
159
  positionals: [],
160
160
  options: {},
161
161
  },
@@ -166,7 +166,7 @@ describe('CLI Runner', () => {
166
166
  renderers: {},
167
167
  })
168
168
 
169
- pikkuState('cli', 'programs', {
169
+ pikkuState(null, 'cli', 'programs', {
170
170
  'test-cli': {
171
171
  defaultRenderer: undefined,
172
172
  middleware: [testMiddleware],
@@ -174,9 +174,9 @@ describe('CLI Runner', () => {
174
174
  },
175
175
  })
176
176
 
177
- pikkuState('function', 'meta', {
177
+ pikkuState(null, 'function', 'meta', {
178
178
  testFunc: {
179
- pikkuFuncName: 'testFunc',
179
+ pikkuFuncId: 'testFunc',
180
180
  inputSchemaName: null,
181
181
  outputSchemaName: null,
182
182
  },
@@ -208,14 +208,14 @@ describe('CLI Runner', () => {
208
208
 
209
209
  const testFunc = async () => ({ success: true })
210
210
 
211
- pikkuState('cli', 'meta', {
211
+ pikkuState(null, 'cli', 'meta', {
212
212
  programs: {
213
213
  'test-cli': {
214
214
  program: 'test-cli',
215
215
  commands: {
216
216
  greet: {
217
217
  command: 'greet <name>',
218
- pikkuFuncName: 'greetFunc',
218
+ pikkuFuncId: 'greetFunc',
219
219
  positionals: [{ name: 'name', required: true }],
220
220
  options: {},
221
221
  },
@@ -226,7 +226,7 @@ describe('CLI Runner', () => {
226
226
  renderers: {},
227
227
  })
228
228
 
229
- pikkuState('cli', 'programs', {
229
+ pikkuState(null, 'cli', 'programs', {
230
230
  'test-cli': {
231
231
  defaultRenderer: undefined,
232
232
  middleware: [testMiddleware],
@@ -234,9 +234,9 @@ describe('CLI Runner', () => {
234
234
  },
235
235
  })
236
236
 
237
- pikkuState('function', 'meta', {
237
+ pikkuState(null, 'function', 'meta', {
238
238
  greetFunc: {
239
- pikkuFuncName: 'greetFunc',
239
+ pikkuFuncId: 'greetFunc',
240
240
  inputSchemaName: null,
241
241
  outputSchemaName: null,
242
242
  },
@@ -260,14 +260,14 @@ describe('CLI Runner', () => {
260
260
  test('should throw error when auth required but no session', async () => {
261
261
  const testFunc = async () => ({ success: true })
262
262
 
263
- pikkuState('cli', 'meta', {
263
+ pikkuState(null, 'cli', 'meta', {
264
264
  programs: {
265
265
  'test-cli': {
266
266
  program: 'test-cli',
267
267
  commands: {
268
268
  secure: {
269
269
  command: 'secure',
270
- pikkuFuncName: 'secureFunc',
270
+ pikkuFuncId: 'secureFunc',
271
271
  positionals: [],
272
272
  options: {},
273
273
  },
@@ -278,9 +278,9 @@ describe('CLI Runner', () => {
278
278
  renderers: {},
279
279
  })
280
280
 
281
- pikkuState('function', 'meta', {
281
+ pikkuState(null, 'function', 'meta', {
282
282
  secureFunc: {
283
- pikkuFuncName: 'secureFunc',
283
+ pikkuFuncId: 'secureFunc',
284
284
  inputSchemaName: null,
285
285
  outputSchemaName: null,
286
286
  },
@@ -306,14 +306,14 @@ describe('CLI Runner', () => {
306
306
  test('should register CLI program and commands', () => {
307
307
  const greetFunc = async () => 'Hello'
308
308
 
309
- pikkuState('cli', 'meta', {
309
+ pikkuState(null, 'cli', 'meta', {
310
310
  programs: {
311
311
  'my-cli': {
312
312
  program: 'my-cli',
313
313
  commands: {
314
314
  greet: {
315
315
  command: 'greet <name>',
316
- pikkuFuncName: 'greetFunc',
316
+ pikkuFuncId: 'greetFunc',
317
317
  positionals: [{ name: 'name', required: true }],
318
318
  options: {
319
319
  loud: {
@@ -337,7 +337,7 @@ describe('CLI Runner', () => {
337
337
  },
338
338
  })
339
339
 
340
- const programs = pikkuState('cli', 'programs')
340
+ const programs = pikkuState(null, 'cli', 'programs')
341
341
  assert.ok(programs['my-cli'])
342
342
  assert.strictEqual(programs['my-cli'].middleware.length, 0)
343
343
  })
@@ -347,7 +347,7 @@ describe('CLI Runner', () => {
347
347
  await next()
348
348
  }
349
349
 
350
- pikkuState('cli', 'meta', {
350
+ pikkuState(null, 'cli', 'meta', {
351
351
  programs: {
352
352
  'my-cli': {
353
353
  program: 'my-cli',
@@ -364,13 +364,13 @@ describe('CLI Runner', () => {
364
364
  middleware: [middleware],
365
365
  })
366
366
 
367
- const programs = pikkuState('cli', 'programs')
367
+ const programs = pikkuState(null, 'cli', 'programs')
368
368
  assert.strictEqual(programs['my-cli'].middleware.length, 1)
369
369
  assert.strictEqual(programs['my-cli'].middleware[0], middleware)
370
370
  })
371
371
 
372
372
  test('should throw error when CLI metadata not found', () => {
373
- pikkuState('cli', 'meta', { programs: {}, renderers: {} })
373
+ pikkuState(null, 'cli', 'meta', { programs: {}, renderers: {} })
374
374
 
375
375
  assert.throws(() => {
376
376
  wireCLI({