@pikku/core 0.8.1 → 0.9.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 (146) hide show
  1. package/CHANGELOG.md +13 -1
  2. package/dist/function/functions.types.d.ts +9 -9
  3. package/dist/handle-error.d.ts +1 -1
  4. package/dist/index.d.ts +11 -10
  5. package/dist/index.js +11 -10
  6. package/dist/pikku-state.d.ts +9 -9
  7. package/dist/services/user-session-service.d.ts +1 -1
  8. package/dist/types/core.types.d.ts +5 -5
  9. package/dist/types/core.types.js +9 -9
  10. package/dist/wirings/channel/channel-handler.d.ts +3 -0
  11. package/dist/{events → wirings}/channel/channel-handler.js +3 -3
  12. package/dist/wirings/channel/channel-runner.d.ts +14 -0
  13. package/dist/{events → wirings}/channel/channel-runner.js +13 -13
  14. package/dist/{events → wirings}/channel/channel.types.d.ts +7 -7
  15. package/dist/{events → wirings}/channel/eventhub-service.d.ts +4 -4
  16. package/dist/wirings/channel/eventhub-store.d.ts +5 -0
  17. package/dist/wirings/channel/local/local-channel-runner.d.ts +3 -0
  18. package/dist/{events → wirings}/channel/local/local-eventhub-service.d.ts +4 -4
  19. package/dist/{events → wirings}/channel/local/local-eventhub-service.js +1 -1
  20. package/dist/{events → wirings}/channel/serverless/serverless-channel-runner.d.ts +2 -2
  21. package/dist/{events → wirings}/http/http-runner.d.ts +12 -12
  22. package/dist/{events → wirings}/http/http-runner.js +15 -15
  23. package/dist/{events → wirings}/http/http.types.d.ts +32 -27
  24. package/dist/{events → wirings}/http/index.d.ts +1 -1
  25. package/dist/{events → wirings}/http/index.js +1 -1
  26. package/dist/{events → wirings}/mcp/mcp-endpoint-registry.d.ts +0 -1
  27. package/dist/{events → wirings}/mcp/mcp-endpoint-registry.js +0 -10
  28. package/dist/{events → wirings}/mcp/mcp-runner.d.ts +4 -4
  29. package/dist/{events → wirings}/mcp/mcp-runner.js +4 -4
  30. package/dist/{events → wirings}/mcp/mcp.types.d.ts +7 -7
  31. package/dist/{events → wirings}/queue/index.d.ts +1 -1
  32. package/dist/{events → wirings}/queue/index.js +1 -1
  33. package/dist/{events → wirings}/queue/queue-runner.d.ts +2 -2
  34. package/dist/{events → wirings}/queue/queue-runner.js +1 -1
  35. package/dist/{events → wirings}/queue/queue.types.d.ts +3 -3
  36. package/dist/wirings/scheduler/index.d.ts +3 -0
  37. package/dist/wirings/scheduler/index.js +3 -0
  38. package/dist/{events → wirings}/scheduler/scheduler-runner.d.ts +2 -2
  39. package/dist/{events → wirings}/scheduler/scheduler-runner.js +1 -1
  40. package/dist/{events → wirings}/scheduler/scheduler.types.d.ts +3 -3
  41. package/lcov.info +3072 -2982
  42. package/package.json +9 -9
  43. package/src/function/functions.types.ts +13 -13
  44. package/src/handle-error.ts +1 -1
  45. package/src/index.ts +14 -13
  46. package/src/pikku-state.ts +11 -11
  47. package/src/services/user-session-service.ts +1 -1
  48. package/src/types/core.types.ts +5 -5
  49. package/src/{events → wirings}/channel/channel-handler.ts +5 -5
  50. package/src/{events → wirings}/channel/channel-runner.ts +18 -18
  51. package/src/{events → wirings}/channel/channel.types.ts +16 -16
  52. package/src/{events → wirings}/channel/eventhub-service.ts +12 -8
  53. package/src/wirings/channel/eventhub-store.ts +13 -0
  54. package/src/{events → wirings}/channel/local/local-channel-runner.test.ts +2 -2
  55. package/src/{events → wirings}/channel/local/local-channel-runner.ts +2 -2
  56. package/src/{events → wirings}/channel/local/local-eventhub-service.ts +16 -10
  57. package/src/{events → wirings}/channel/serverless/serverless-channel-runner.ts +2 -2
  58. package/src/{events → wirings}/http/http-runner.test.ts +4 -4
  59. package/src/{events → wirings}/http/http-runner.ts +32 -31
  60. package/src/{events → wirings}/http/http.types.ts +40 -35
  61. package/src/{events → wirings}/http/index.ts +1 -1
  62. package/src/wirings/mcp/mcp-endpoint-registry.test.js +338 -0
  63. package/src/wirings/mcp/mcp-endpoint-registry.test.js.map +1 -0
  64. package/src/{events → wirings}/mcp/mcp-endpoint-registry.ts +0 -10
  65. package/src/{events → wirings}/mcp/mcp-runner.ts +10 -10
  66. package/src/{events → wirings}/mcp/mcp.types.ts +9 -7
  67. package/src/{events → wirings}/queue/index.ts +1 -1
  68. package/src/{events → wirings}/queue/queue-runner.ts +5 -5
  69. package/src/{events → wirings}/queue/queue.types.ts +3 -3
  70. package/src/{events → wirings}/scheduler/index.ts +1 -1
  71. package/src/{events → wirings}/scheduler/scheduler-runner.ts +4 -4
  72. package/src/{events → wirings}/scheduler/scheduler.types.ts +3 -3
  73. package/tsconfig.tsbuildinfo +1 -1
  74. package/dist/events/channel/channel-handler.d.ts +0 -3
  75. package/dist/events/channel/channel-runner.d.ts +0 -14
  76. package/dist/events/channel/eventhub-store.d.ts +0 -5
  77. package/dist/events/channel/local/local-channel-runner.d.ts +0 -3
  78. package/dist/events/scheduler/index.d.ts +0 -3
  79. package/dist/events/scheduler/index.js +0 -3
  80. package/src/events/channel/eventhub-store.ts +0 -8
  81. /package/dist/{events → wirings}/channel/channel-store.d.ts +0 -0
  82. /package/dist/{events → wirings}/channel/channel-store.js +0 -0
  83. /package/dist/{events → wirings}/channel/channel.types.js +0 -0
  84. /package/dist/{events → wirings}/channel/eventhub-service.js +0 -0
  85. /package/dist/{events → wirings}/channel/eventhub-store.js +0 -0
  86. /package/dist/{events → wirings}/channel/index.d.ts +0 -0
  87. /package/dist/{events → wirings}/channel/index.js +0 -0
  88. /package/dist/{events → wirings}/channel/local/index.d.ts +0 -0
  89. /package/dist/{events → wirings}/channel/local/index.js +0 -0
  90. /package/dist/{events → wirings}/channel/local/local-channel-handler.d.ts +0 -0
  91. /package/dist/{events → wirings}/channel/local/local-channel-handler.js +0 -0
  92. /package/dist/{events → wirings}/channel/local/local-channel-runner.js +0 -0
  93. /package/dist/{events → wirings}/channel/log-channels.d.ts +0 -0
  94. /package/dist/{events → wirings}/channel/log-channels.js +0 -0
  95. /package/dist/{events → wirings}/channel/pikku-abstract-channel-handler.d.ts +0 -0
  96. /package/dist/{events → wirings}/channel/pikku-abstract-channel-handler.js +0 -0
  97. /package/dist/{events → wirings}/channel/serverless/index.d.ts +0 -0
  98. /package/dist/{events → wirings}/channel/serverless/index.js +0 -0
  99. /package/dist/{events → wirings}/channel/serverless/serverless-channel-runner.js +0 -0
  100. /package/dist/{events → wirings}/http/http.types.js +0 -0
  101. /package/dist/{events → wirings}/http/log-http-routes.d.ts +0 -0
  102. /package/dist/{events → wirings}/http/log-http-routes.js +0 -0
  103. /package/dist/{events → wirings}/http/pikku-fetch-http-request.d.ts +0 -0
  104. /package/dist/{events → wirings}/http/pikku-fetch-http-request.js +0 -0
  105. /package/dist/{events → wirings}/http/pikku-fetch-http-response.d.ts +0 -0
  106. /package/dist/{events → wirings}/http/pikku-fetch-http-response.js +0 -0
  107. /package/dist/{events → wirings}/mcp/index.d.ts +0 -0
  108. /package/dist/{events → wirings}/mcp/index.js +0 -0
  109. /package/dist/{events → wirings}/mcp/mcp.types.js +0 -0
  110. /package/dist/{events → wirings}/queue/queue.types.js +0 -0
  111. /package/dist/{events → wirings}/queue/register-queue-helper.d.ts +0 -0
  112. /package/dist/{events → wirings}/queue/register-queue-helper.js +0 -0
  113. /package/dist/{events → wirings}/queue/validate-worker-config.d.ts +0 -0
  114. /package/dist/{events → wirings}/queue/validate-worker-config.js +0 -0
  115. /package/dist/{events → wirings}/rpc/index.d.ts +0 -0
  116. /package/dist/{events → wirings}/rpc/index.js +0 -0
  117. /package/dist/{events → wirings}/rpc/rpc-runner.d.ts +0 -0
  118. /package/dist/{events → wirings}/rpc/rpc-runner.js +0 -0
  119. /package/dist/{events → wirings}/rpc/rpc-types.d.ts +0 -0
  120. /package/dist/{events → wirings}/rpc/rpc-types.js +0 -0
  121. /package/dist/{events → wirings}/scheduler/log-schedulers.d.ts +0 -0
  122. /package/dist/{events → wirings}/scheduler/log-schedulers.js +0 -0
  123. /package/dist/{events → wirings}/scheduler/scheduler.types.js +0 -0
  124. /package/src/{events → wirings}/channel/channel-store.ts +0 -0
  125. /package/src/{events → wirings}/channel/index.ts +0 -0
  126. /package/src/{events → wirings}/channel/local/index.ts +0 -0
  127. /package/src/{events → wirings}/channel/local/local-channel-handler.ts +0 -0
  128. /package/src/{events → wirings}/channel/local/local-eventhub-service.test.ts +0 -0
  129. /package/src/{events → wirings}/channel/log-channels.ts +0 -0
  130. /package/src/{events → wirings}/channel/pikku-abstract-channel-handler.test.ts +0 -0
  131. /package/src/{events → wirings}/channel/pikku-abstract-channel-handler.ts +0 -0
  132. /package/src/{events → wirings}/channel/serverless/index.ts +0 -0
  133. /package/src/{events → wirings}/http/log-http-routes.ts +0 -0
  134. /package/src/{events → wirings}/http/pikku-fetch-http-request.test.ts +0 -0
  135. /package/src/{events → wirings}/http/pikku-fetch-http-request.ts +0 -0
  136. /package/src/{events → wirings}/http/pikku-fetch-http-response.test.ts +0 -0
  137. /package/src/{events → wirings}/http/pikku-fetch-http-response.ts +0 -0
  138. /package/src/{events → wirings}/mcp/index.ts +0 -0
  139. /package/src/{events → wirings}/mcp/mcp-endpoint-registry.test.d.ts +0 -0
  140. /package/src/{events → wirings}/mcp/mcp-endpoint-registry.test.ts +0 -0
  141. /package/src/{events → wirings}/queue/register-queue-helper.ts +0 -0
  142. /package/src/{events → wirings}/queue/validate-worker-config.ts +0 -0
  143. /package/src/{events → wirings}/rpc/index.ts +0 -0
  144. /package/src/{events → wirings}/rpc/rpc-runner.ts +0 -0
  145. /package/src/{events → wirings}/rpc/rpc-types.ts +0 -0
  146. /package/src/{events → wirings}/scheduler/log-schedulers.ts +0 -0
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@pikku/core",
3
- "version": "0.8.1",
3
+ "version": "0.9.0",
4
4
  "author": "yasser.fadl@gmail.com",
5
5
  "license": "MIT",
6
6
  "module": "dist/index.js",
@@ -20,14 +20,14 @@
20
20
  ".": "./dist/index.js",
21
21
  "./middleware": "./dist/middleware/index.js",
22
22
  "./function": "./dist/function/index.js",
23
- "./channel": "./dist/events/channel/index.js",
24
- "./channel/local": "./dist/events/channel/local/index.js",
25
- "./channel/serverless": "./dist/events/channel/serverless/index.js",
26
- "./http": "./dist/events/http/index.js",
27
- "./queue": "./dist/events/queue/index.js",
28
- "./scheduler": "./dist/events/scheduler/index.js",
29
- "./rpc": "./dist/events/rpc/index.js",
30
- "./mcp": "./dist/events/mcp/index.js",
23
+ "./channel": "./dist/wirings/channel/index.js",
24
+ "./channel/local": "./dist/wirings/channel/local/index.js",
25
+ "./channel/serverless": "./dist/wirings/channel/serverless/index.js",
26
+ "./http": "./dist/wirings/http/index.js",
27
+ "./queue": "./dist/wirings/queue/index.js",
28
+ "./scheduler": "./dist/wirings/scheduler/index.js",
29
+ "./rpc": "./dist/wirings/rpc/index.js",
30
+ "./mcp": "./dist/wirings/mcp/index.js",
31
31
  "./errors": "./dist/errors/index.js",
32
32
  "./services": "./dist/services/index.js",
33
33
  "./schema": "./dist/schema.js",
@@ -1,4 +1,4 @@
1
- import { PikkuChannel } from '../events/channel/channel.types.js'
1
+ import { PikkuChannel } from '../wirings/channel/channel.types.js'
2
2
  import type {
3
3
  CoreServices,
4
4
  CoreSingletonServices,
@@ -14,7 +14,7 @@ import type {
14
14
  * @template Services - The services type, defaults to `CoreServices`.
15
15
  * @template Session - The session type, defaults to `CoreUserSession`.
16
16
  */
17
- export type CoreAPIFunction<
17
+ export type CorePikkuFunction<
18
18
  In,
19
19
  Out,
20
20
  ChannelData extends unknown | null = null,
@@ -41,7 +41,7 @@ export type CoreAPIFunction<
41
41
  * @template Services - The services type, defaults to `CoreServices`.
42
42
  * @template Session - The session type, defaults to `CoreUserSession`.
43
43
  */
44
- export type CoreAPIFunctionSessionless<
44
+ export type CorePikkuFunctionSessionless<
45
45
  In,
46
46
  Out,
47
47
  ChannelData extends unknown | null = null,
@@ -61,30 +61,30 @@ export type CoreAPIFunctionSessionless<
61
61
  ) => ChannelData extends null ? Promise<Out> : Promise<Out> | Promise<void>
62
62
 
63
63
  /**
64
- * Represents a function that checks permissions for a given API operation.
64
+ * Represents a function that checks permissions for a given operation.
65
65
  *
66
66
  * @template In - The input type.
67
67
  * @template Services - The services type, defaults to `CoreServices`.
68
68
  * @template Session - The session type, defaults to `CoreUserSession`.
69
69
  */
70
- export type CoreAPIPermission<
70
+ export type CorePikkuPermission<
71
71
  In = any,
72
72
  Services extends CoreSingletonServices = CoreServices,
73
73
  Session extends CoreUserSession = CoreUserSession,
74
74
  > = (services: Services, data: In, session?: Session) => Promise<boolean>
75
75
 
76
- export type CorePermissionGroup<APIPermission = CoreAPIPermission<any>> =
77
- | Record<string, APIPermission | APIPermission[]>
76
+ export type CorePermissionGroup<PikkuPermission = CorePikkuPermission<any>> =
77
+ | Record<string, PikkuPermission | PikkuPermission[]>
78
78
  | undefined
79
79
 
80
80
  export type CorePikkuFunctionConfig<
81
- APIFunction extends
82
- | CoreAPIFunction<any, any>
83
- | CoreAPIFunctionSessionless<any, any>,
84
- APIPermission extends CoreAPIPermission<any> = CoreAPIPermission<any>,
81
+ PikkuFunction extends
82
+ | CorePikkuFunction<any, any>
83
+ | CorePikkuFunctionSessionless<any, any>,
84
+ PikkuPermission extends CorePikkuPermission<any> = CorePikkuPermission<any>,
85
85
  > = {
86
- func: APIFunction
86
+ func: PikkuFunction
87
87
  auth?: boolean
88
- permissions?: CorePermissionGroup<APIPermission>
88
+ permissions?: CorePermissionGroup<PikkuPermission>
89
89
  middleware?: PikkuFunctionMiddleware[]
90
90
  }
@@ -1,7 +1,7 @@
1
1
  import { getErrorResponse } from './errors/error-handler.js'
2
2
  import { NotFoundError } from './errors/errors.js'
3
3
  import { Logger } from './services/logger.js'
4
- import { PikkuHTTP } from './events/http/http.types.js'
4
+ import { PikkuHTTP } from './wirings/http/http.types.js'
5
5
 
6
6
  /**
7
7
  * Handle errors that occur during route processing
package/src/index.ts CHANGED
@@ -10,25 +10,26 @@ export * from './services/schema-service.js'
10
10
  export * from './services/jwt-service.js'
11
11
  export * from './services/secret-service.js'
12
12
  export * from './services/user-session-service.js'
13
- export * from './events/http/index.js'
14
- export * from './events/channel/index.js'
15
- export * from './events/scheduler/index.js'
16
- export * from './events/rpc/index.js'
17
- export * from './events/queue/index.js'
18
- export * from './events/mcp/index.js'
13
+ export * from './wirings/http/index.js'
14
+ export * from './wirings/channel/index.js'
15
+ export * from './wirings/scheduler/index.js'
16
+ export * from './wirings/rpc/index.js'
17
+ export * from './wirings/queue/index.js'
18
+ export * from './wirings/mcp/index.js'
19
19
  export * from './errors/index.js'
20
20
  export * from './middleware/index.js'
21
21
  export * from './time-utils.js'
22
22
  export * from './utils.js'
23
23
  export { pikkuState } from './pikku-state.js'
24
24
  export { runMiddleware } from './middleware-runner.js'
25
- export { addHTTPRoute, addMiddleware } from './events/http/http-runner.js'
26
- export { addChannel } from './events/channel/channel-runner.js'
27
- export { addScheduledTask } from './events/scheduler/scheduler-runner.js'
25
+ export { wireHTTP, addMiddleware } from './wirings/http/http-runner.js'
26
+ export { wireChannel } from './wirings/channel/channel-runner.js'
27
+ export { wireScheduler } from './wirings/scheduler/scheduler-runner.js'
28
+ export { wireQueueWorker } from './wirings/queue/queue-runner.js'
28
29
  export {
29
- addMCPResource,
30
- addMCPTool,
31
- addMCPPrompt,
30
+ wireMCPResource,
31
+ wireMCPTool,
32
+ wireMCPPrompt,
32
33
  runMCPResource,
33
34
  runMCPTool,
34
35
  runMCPPrompt,
@@ -38,4 +39,4 @@ export {
38
39
  getMCPResourcesMeta,
39
40
  getMCPToolsMeta,
40
41
  getMCPPromptsMeta,
41
- } from './events/mcp/mcp-runner.js'
42
+ } from './wirings/mcp/mcp-runner.js'
@@ -1,21 +1,21 @@
1
- import { ChannelsMeta, CoreAPIChannel } from './events/channel/channel.types.js'
1
+ import { ChannelsMeta, CoreChannel } from './wirings/channel/channel.types.js'
2
2
  import {
3
- CoreHTTPFunctionRoute,
3
+ CoreHTTPFunctionWiring,
4
4
  HTTPMethod,
5
- HTTPRoutesMeta,
6
- } from './events/http/http.types.js'
5
+ HTTPWiringsMeta,
6
+ } from './wirings/http/http.types.js'
7
7
  import { FunctionsMeta, PikkuMiddleware } from './types/core.types.js'
8
8
  import {
9
9
  CoreScheduledTask,
10
10
  ScheduledTasksMeta,
11
- } from './events/scheduler/scheduler.types.js'
11
+ } from './wirings/scheduler/scheduler.types.js'
12
12
  import { ErrorDetails, PikkuError } from './errors/error-handler.js'
13
13
  import { CorePikkuFunctionConfig } from './function/functions.types.js'
14
- import { RPCMeta } from './events/rpc/rpc-types.js'
14
+ import { RPCMeta } from './wirings/rpc/rpc-types.js'
15
15
  import {
16
16
  queueWorkersMeta,
17
17
  CoreQueueWorker,
18
- } from './events/queue/queue.types.js'
18
+ } from './wirings/queue/queue.types.js'
19
19
  import {
20
20
  CoreMCPResource,
21
21
  CoreMCPTool,
@@ -23,7 +23,7 @@ import {
23
23
  MCPResourceMeta,
24
24
  MCPToolMeta,
25
25
  MCPPromptMeta,
26
- } from './events/mcp/mcp.types.js'
26
+ } from './wirings/mcp/mcp.types.js'
27
27
 
28
28
  interface PikkuState {
29
29
  function: {
@@ -42,11 +42,11 @@ interface PikkuState {
42
42
  }
43
43
  http: {
44
44
  middleware: Array<{ route: string; middleware: PikkuMiddleware[] }>
45
- routes: Map<HTTPMethod, Map<string, CoreHTTPFunctionRoute<any, any, any>>>
46
- meta: HTTPRoutesMeta
45
+ routes: Map<HTTPMethod, Map<string, CoreHTTPFunctionWiring<any, any, any>>>
46
+ meta: HTTPWiringsMeta
47
47
  }
48
48
  channel: {
49
- channels: Map<string, CoreAPIChannel<any, any>>
49
+ channels: Map<string, CoreChannel<any, any>>
50
50
  meta: ChannelsMeta
51
51
  }
52
52
  scheduler: {
@@ -1,4 +1,4 @@
1
- import { ChannelStore } from '../events/channel/channel-store.js'
1
+ import { ChannelStore } from '../wirings/channel/channel-store.js'
2
2
  import { CoreUserSession } from '../types/core.types.js'
3
3
 
4
4
  export interface UserSessionService<UserSession extends CoreUserSession> {
@@ -1,13 +1,13 @@
1
1
  import type { Logger, LogLevel } from '../services/logger.js'
2
2
  import { VariablesService } from '../services/variables-service.js'
3
3
  import { SchemaService } from '../services/schema-service.js'
4
- import { PikkuHTTP } from '../events/http/http.types.js'
4
+ import { PikkuHTTP } from '../wirings/http/http.types.js'
5
5
  import { UserSessionService } from '../services/user-session-service.js'
6
- import { PikkuChannel } from '../events/channel/channel.types.js'
7
- import { PikkuRPC } from '../events/rpc/rpc-types.js'
8
- import { PikkuMCP } from '../events/mcp/mcp.types.js'
6
+ import { PikkuChannel } from '../wirings/channel/channel.types.js'
7
+ import { PikkuRPC } from '../wirings/rpc/rpc-types.js'
8
+ import { PikkuMCP } from '../wirings/mcp/mcp.types.js'
9
9
 
10
- export enum PikkuEventTypes {
10
+ export enum PikkuWiringTypes {
11
11
  http = 'http',
12
12
  scheduler = 'scheduler',
13
13
  channel = 'channel',
@@ -5,7 +5,7 @@ import {
5
5
  } from '../../types/core.types.js'
6
6
  import {
7
7
  ChannelMessageMeta,
8
- CoreAPIChannel,
8
+ CoreChannel,
9
9
  PikkuChannelHandler,
10
10
  } from './channel.types.js'
11
11
  import { pikkuState } from '../../pikku-state.js'
@@ -31,7 +31,7 @@ const getRouteMeta = (
31
31
  `Channel ${channelName} requires a router value for ${routingProperty}`
32
32
  )
33
33
  }
34
- const route = channelMeta.messageRoutes[routingProperty]?.[routerValue]
34
+ const route = channelMeta.messageWirings[routingProperty]?.[routerValue]
35
35
  if (!route) {
36
36
  throw new Error(
37
37
  `Channel ${channelName} has no route for ${routingProperty}:${routerValue}`
@@ -61,7 +61,7 @@ const validateAuth = (
61
61
  export const processMessageHandlers = (
62
62
  services: CoreServices,
63
63
  session: CoreUserSession | undefined,
64
- channelConfig: CoreAPIChannel<any, any>,
64
+ channelConfig: CoreChannel<any, any>,
65
65
  channelHandler: PikkuChannelHandler
66
66
  ) => {
67
67
  const logger = services.logger
@@ -115,10 +115,10 @@ export const processMessageHandlers = (
115
115
  let processed = false
116
116
 
117
117
  // Route-specific handling
118
- if (typeof rawData === 'string' && channelConfig.onMessageRoute) {
118
+ if (typeof rawData === 'string' && channelConfig.onMessageWiring) {
119
119
  try {
120
120
  const messageData = JSON.parse(rawData)
121
- const entries = Object.entries(channelConfig.onMessageRoute)
121
+ const entries = Object.entries(channelConfig.onMessageWiring)
122
122
  for (const [routingProperty, routes] of entries) {
123
123
  const routerValue = messageData[routingProperty]
124
124
  if (routerValue && routes[routerValue]) {
@@ -5,7 +5,7 @@ import { coerceTopLevelDataFromSchema, validateSchema } from '../../schema.js'
5
5
  import { UserSessionService } from '../../services/user-session-service.js'
6
6
  import {
7
7
  ChannelMeta,
8
- CoreAPIChannel,
8
+ CoreChannel,
9
9
  RunChannelOptions,
10
10
  RunChannelParams,
11
11
  } from './channel.types.js'
@@ -15,14 +15,14 @@ import { match } from 'path-to-regexp'
15
15
  * Adds a channel and registers all functions referenced in it using the
16
16
  * function names already stored in the channel metadata
17
17
  */
18
- export const addChannel = <
18
+ export const wireChannel = <
19
19
  In,
20
20
  Channel extends string,
21
21
  ChannelFunction,
22
22
  ChannelFunctionSessionless,
23
- APIPermission,
23
+ PikkuPermission,
24
24
  >(
25
- channel: CoreAPIChannel<In, Channel, ChannelFunction, APIPermission>
25
+ channel: CoreChannel<In, Channel, ChannelFunction, PikkuPermission>
26
26
  ) => {
27
27
  // Get the channel metadata
28
28
  const channelsMeta = pikkuState('channel', 'meta')
@@ -58,20 +58,20 @@ export const addChannel = <
58
58
  })
59
59
  }
60
60
 
61
- // Register functions in onMessageRoute
62
- if (channel.onMessageRoute && channelMeta.messageRoutes) {
63
- // Iterate through each channel in onMessageRoute
64
- Object.entries(channel.onMessageRoute).forEach(([channelKey, routes]) => {
65
- const channelRoutes = channelMeta.messageRoutes[channelKey]
66
- if (!channelRoutes) return
61
+ // Register functions in onMessageWiring
62
+ if (channel.onMessageWiring && channelMeta.messageWirings) {
63
+ // Iterate through each channel in onMessageWiring
64
+ Object.entries(channel.onMessageWiring).forEach(([channelKey, wirings]) => {
65
+ const channelWirings = channelMeta.messageWirings[channelKey]
66
+ if (!channelWirings) return
67
67
 
68
- // Iterate through each route in the channel
69
- Object.entries(routes).forEach(([routeKey, handler]) => {
70
- const routeMeta = channelRoutes[routeKey]
71
- if (!routeMeta) return
68
+ // Iterate through each wiring in the channel
69
+ Object.entries(wirings).forEach(([wiringKey, handler]) => {
70
+ const wiringMeta = channelWirings[wiringKey]
71
+ if (!wiringMeta) return
72
72
 
73
73
  // Extract the function from the handler
74
- const routeFunc =
74
+ const wiringFunc =
75
75
  typeof handler === 'function'
76
76
  ? { func: handler }
77
77
  : {
@@ -81,7 +81,7 @@ export const addChannel = <
81
81
  }
82
82
 
83
83
  // Register the function using the pikku name from metadata
84
- addFunction(routeMeta.pikkuFuncName, routeFunc)
84
+ addFunction(wiringMeta.pikkuFuncName, wiringFunc)
85
85
  })
86
86
  })
87
87
  }
@@ -122,12 +122,12 @@ export const openChannel = async ({
122
122
  singletonServices,
123
123
  coerceDataFromSchema = true,
124
124
  request,
125
- }: Pick<CoreAPIChannel<unknown, string>, 'route'> &
125
+ }: Pick<CoreChannel<unknown, string>, 'route'> &
126
126
  RunChannelParams<unknown> & {
127
127
  userSession: UserSessionService<any>
128
128
  } & RunChannelOptions): Promise<{
129
129
  openingData: unknown
130
- channelConfig: CoreAPIChannel<unknown, any>
130
+ channelConfig: CoreChannel<unknown, any>
131
131
  meta: ChannelMeta
132
132
  }> => {
133
133
  const matchingChannel = getMatchingChannelConfig(route)
@@ -11,9 +11,9 @@ import {
11
11
  PikkuMiddleware,
12
12
  } from '../../types/core.types.js'
13
13
  import {
14
- CoreAPIFunction,
15
- CoreAPIFunctionSessionless,
16
- CoreAPIPermission,
14
+ CorePikkuFunction,
15
+ CorePikkuFunctionSessionless,
16
+ CorePikkuPermission,
17
17
  } from '../../function/functions.types.js'
18
18
 
19
19
  export type RunChannelOptions = Partial<{
@@ -47,26 +47,26 @@ export interface ChannelMeta {
47
47
  connect: ChannelMessageMeta | null
48
48
  disconnect: ChannelMessageMeta | null
49
49
  message: ChannelMessageMeta | null
50
- messageRoutes: Record<string, Record<string, ChannelMessageMeta>>
50
+ messageWirings: Record<string, Record<string, ChannelMessageMeta>>
51
51
  docs?: APIDocs
52
52
  tags?: string[]
53
53
  }
54
54
 
55
55
  export type ChannelsMeta = Record<string, ChannelMeta>
56
56
 
57
- export type CoreAPIChannel<
57
+ export type CoreChannel<
58
58
  ChannelData,
59
59
  Channel extends string,
60
60
  ChannelConnect =
61
- | CoreAPIFunction<void, unknown, ChannelData>
62
- | CoreAPIFunctionSessionless<void, unknown, ChannelData>,
61
+ | CorePikkuFunction<void, unknown, ChannelData>
62
+ | CorePikkuFunctionSessionless<void, unknown, ChannelData>,
63
63
  ChannelDisconnect =
64
- | CoreAPIFunction<void, void, ChannelData>
65
- | CoreAPIFunctionSessionless<void, void, ChannelData>,
64
+ | CorePikkuFunction<void, void, ChannelData>
65
+ | CorePikkuFunctionSessionless<void, void, ChannelData>,
66
66
  ChannelFunctionMessage =
67
- | CoreAPIFunction<unknown, unknown, ChannelData>
68
- | CoreAPIFunctionSessionless<unknown, unknown, ChannelData>,
69
- APIPermission = CoreAPIPermission<ChannelData>,
67
+ | CorePikkuFunction<unknown, unknown, ChannelData>
68
+ | CorePikkuFunctionSessionless<unknown, unknown, ChannelData>,
69
+ PikkuPermission = CorePikkuPermission<ChannelData>,
70
70
  > = {
71
71
  name: string
72
72
  route: Channel
@@ -75,24 +75,24 @@ export type CoreAPIChannel<
75
75
  onMessage?:
76
76
  | {
77
77
  func: ChannelFunctionMessage
78
- permissions?: Record<string, APIPermission[] | APIPermission>
78
+ permissions?: Record<string, PikkuPermission[] | PikkuPermission>
79
79
  auth?: boolean
80
80
  }
81
81
  | ChannelFunctionMessage
82
- onMessageRoute?: Record<
82
+ onMessageWiring?: Record<
83
83
  string,
84
84
  Record<
85
85
  string,
86
86
  | ChannelFunctionMessage
87
87
  | {
88
88
  func: ChannelFunctionMessage
89
- permissions?: Record<string, APIPermission[] | APIPermission>
89
+ permissions?: Record<string, PikkuPermission[] | PikkuPermission>
90
90
  auth?: boolean
91
91
  }
92
92
  >
93
93
  >
94
94
  middleware?: PikkuMiddleware[]
95
- permissions?: Record<string, APIPermission[] | APIPermission>
95
+ permissions?: Record<string, PikkuPermission[] | PikkuPermission>
96
96
  auth?: boolean
97
97
  docs?: Partial<{
98
98
  description: string
@@ -1,30 +1,34 @@
1
1
  /**
2
2
  * Interface defining the methods of a EventHub Service.
3
3
  */
4
- export interface EventHubService<Out = unknown> {
4
+ export interface EventHubService<Topics extends Record<string, any>> {
5
5
  /**
6
6
  * Subscribes a connection to a specific topic.
7
7
  * @param topic - The topic to subscribe to.
8
8
  * @param channelId - The unique ID of the connection to subscribe.
9
9
  */
10
- subscribe(topic: string, channelId: string): Promise<void> | void
11
-
10
+ subscribe<T extends keyof Topics>(
11
+ topic: T,
12
+ channelId: string
13
+ ): Promise<void> | void
12
14
  /**
13
15
  * Unsubscribes a connection from a specific topic.
14
16
  * @param topic - The topic to unsubscribe from.
15
17
  * @param channelId - The unique ID of the connection to unsubscribe.
16
18
  */
17
- unsubscribe(topic: string, channelId: string): Promise<void> | void
18
-
19
+ unsubscribe<T extends keyof Topics>(
20
+ topic: T,
21
+ channelId: string
22
+ ): Promise<void> | void
19
23
  /**
20
24
  * Sends data to all connections subscribed to a topic.
21
25
  * @param topic - The topic to send data to.
22
26
  * @param data - The data to send to the subscribers.
23
27
  */
24
- publish(
25
- topic: string,
28
+ publish<T extends keyof Topics>(
29
+ topic: T,
26
30
  channelId: string | null,
27
- data: Out,
31
+ data: Topics[T],
28
32
  isBinary?: boolean
29
33
  ): Promise<void> | void
30
34
  }
@@ -0,0 +1,13 @@
1
+ export abstract class EventHubStore<
2
+ EventTopics extends Record<string, any> = {},
3
+ > {
4
+ public abstract getChannelIdsForTopic(topic: string): Promise<string[]>
5
+ public abstract subscribe<T extends keyof EventTopics>(
6
+ topic: T,
7
+ channelId: string
8
+ ): Promise<boolean>
9
+ public abstract unsubscribe<T extends keyof EventTopics>(
10
+ topic: T,
11
+ channelId: string
12
+ ): Promise<boolean>
13
+ }
@@ -2,7 +2,7 @@ import { test, beforeEach, afterEach } from 'node:test'
2
2
  import * as assert from 'node:assert/strict'
3
3
  import { runLocalChannel } from './local-channel-runner.js'
4
4
  import { pikkuState, resetPikkuState } from '../../../pikku-state.js'
5
- import { addChannel } from '../channel-runner.js'
5
+ import { wireChannel } from '../channel-runner.js'
6
6
  import {
7
7
  HTTPMethod,
8
8
  PikkuHTTPRequest,
@@ -133,7 +133,7 @@ test('runChannel should return a channel handler if channel matches and no auth
133
133
  route: '/test-channel',
134
134
  },
135
135
  } as any)
136
- addChannel({
136
+ wireChannel({
137
137
  name: 'test',
138
138
  route: '/test-channel',
139
139
  auth: false,
@@ -3,7 +3,7 @@ import { createHTTPInteraction } from '../../http/http-runner.js'
3
3
  import { closeSessionServices } from '../../../utils.js'
4
4
  import { processMessageHandlers } from '../channel-handler.js'
5
5
  import {
6
- CoreAPIChannel,
6
+ CoreChannel,
7
7
  RunChannelOptions,
8
8
  RunChannelParams,
9
9
  } from '../channel.types.js'
@@ -28,7 +28,7 @@ export const runLocalChannel = async ({
28
28
  coerceDataFromSchema = true,
29
29
  logWarningsForStatusCodes = [],
30
30
  bubbleErrors = false,
31
- }: Partial<Pick<CoreAPIChannel<unknown, any>, 'route'>> &
31
+ }: Partial<Pick<CoreChannel<unknown, any>, 'route'>> &
32
32
  RunChannelOptions &
33
33
  RunChannelParams<unknown>): Promise<PikkuLocalChannelHandler | void> => {
34
34
  let sessionServices: SessionServices<typeof singletonServices> | undefined
@@ -5,7 +5,7 @@ import { EventHubService } from '../eventhub-service.js'
5
5
  * Implementation of the SubscriptionService interface.
6
6
  * Manages subscriptions and publishes messages to subscribers.
7
7
  */
8
- export class LocalEventHubService<Data = unknown>
8
+ export class LocalEventHubService<Data extends Record<string, any> = {}>
9
9
  implements EventHubService<Data>
10
10
  {
11
11
  private channels = new Map<string, PikkuChannelHandler>()
@@ -13,7 +13,7 @@ export class LocalEventHubService<Data = unknown>
13
13
  /**
14
14
  * A map storing topics and their associated connection IDs.
15
15
  */
16
- private subscriptions: Map<string, Set<string>> = new Map()
16
+ private subscriptions: Map<keyof Data, Set<string>> = new Map()
17
17
 
18
18
  /**
19
19
  * Subscribes a connection to a specific topic.
@@ -21,7 +21,10 @@ export class LocalEventHubService<Data = unknown>
21
21
  * @param topic - The topic to subscribe to.
22
22
  * @param channelId - The unique ID of the connection to subscribe.
23
23
  */
24
- public subscribe(topic: string, channelId: string): void {
24
+ public subscribe<T extends keyof Data>(
25
+ topic: T,
26
+ channelId: string
27
+ ): void | Promise<void> {
25
28
  if (!this.subscriptions.has(topic)) {
26
29
  this.subscriptions.set(topic, new Set())
27
30
  }
@@ -34,12 +37,15 @@ export class LocalEventHubService<Data = unknown>
34
37
  * @param topic - The topic to unsubscribe from.
35
38
  * @param channelId - The unique ID of the connection to unsubscribe.
36
39
  */
37
- public unsubscribe(topic: string, channelId: string): void {
40
+ public unsubscribe<T extends keyof Data>(
41
+ topic: T,
42
+ channelId: string
43
+ ): void | Promise<void> {
38
44
  const topicSubscriptions = this.subscriptions.get(topic)
39
45
  if (topicSubscriptions) {
40
46
  topicSubscriptions.delete(channelId)
41
47
  if (topicSubscriptions.size === 0) {
42
- this.subscriptions.delete(topic) // Cleanup empty subscriptions
48
+ this.subscriptions.delete(topic)
43
49
  }
44
50
  }
45
51
  }
@@ -49,12 +55,12 @@ export class LocalEventHubService<Data = unknown>
49
55
  * @param topic - The topic to send data to.
50
56
  * @param data - The data to send to the subscribers.
51
57
  */
52
- public publish(
53
- topic: string,
54
- channelId: string,
55
- data: Data,
58
+ public publish<T extends keyof Data>(
59
+ topic: T,
60
+ channelId: string | null,
61
+ data: Data[T],
56
62
  isBinary?: boolean
57
- ): void {
63
+ ): void | Promise<void> {
58
64
  const subscribedChannelIds = this.subscriptions.get(topic)
59
65
  if (!subscribedChannelIds) {
60
66
  return
@@ -3,7 +3,7 @@ import { closeSessionServices } from '../../../utils.js'
3
3
  import { processMessageHandlers } from '../channel-handler.js'
4
4
  import { openChannel } from '../channel-runner.js'
5
5
  import type {
6
- CoreAPIChannel,
6
+ CoreChannel,
7
7
  RunChannelOptions,
8
8
  RunChannelParams,
9
9
  PikkuChannelHandlerFactory,
@@ -73,7 +73,7 @@ export const runChannelConnect = async ({
73
73
  logWarningsForStatusCodes = [],
74
74
  respondWith404 = true,
75
75
  bubbleErrors = false,
76
- }: Pick<CoreAPIChannel<unknown, any>, 'route'> &
76
+ }: Pick<CoreChannel<unknown, any>, 'route'> &
77
77
  RunChannelOptions &
78
78
  RunServerlessChannelParams<unknown>) => {
79
79
  let sessionServices: SessionServices | undefined
@@ -2,7 +2,7 @@ import { test, describe, beforeEach, afterEach } from 'node:test'
2
2
  import * as assert from 'assert'
3
3
  import { NotFoundError } from '../../errors/errors.js'
4
4
  import { JSONValue, PikkuMiddleware } from '../../types/core.types.js'
5
- import { fetch, addHTTPRoute } from './http-runner.js'
5
+ import { fetch, wireHTTP } from './http-runner.js'
6
6
  import { pikkuState, resetPikkuState } from '../../pikku-state.js'
7
7
  import {
8
8
  PikkuMockRequest,
@@ -78,7 +78,7 @@ describe('fetch', () => {
78
78
  const routeFunc = async () => ({ success: true })
79
79
  setHTTPFunctionMap(routeFunc)
80
80
 
81
- addHTTPRoute({
81
+ wireHTTP({
82
82
  route: 'test',
83
83
  method: 'get',
84
84
  func: routeFunc,
@@ -98,7 +98,7 @@ describe('fetch', () => {
98
98
  const routeFunc = async () => ({ success: true })
99
99
  setHTTPFunctionMap(routeFunc)
100
100
 
101
- addHTTPRoute({
101
+ wireHTTP({
102
102
  route: 'test',
103
103
  method: 'get',
104
104
  func: routeFunc,
@@ -120,7 +120,7 @@ describe('fetch', () => {
120
120
  throw error
121
121
  }
122
122
  setHTTPFunctionMap(routeFunc)
123
- addHTTPRoute({
123
+ wireHTTP({
124
124
  route: 'test',
125
125
  method: 'get',
126
126
  func: routeFunc,