@pikku/core 0.11.0 → 0.11.1

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 (105) hide show
  1. package/CHANGELOG.md +11 -1
  2. package/dist/errors/errors.d.ts +1 -1
  3. package/dist/errors/errors.js +3 -3
  4. package/dist/function/function-runner.d.ts +6 -7
  5. package/dist/function/function-runner.js +35 -16
  6. package/dist/function/functions.types.d.ts +22 -30
  7. package/dist/function/functions.types.js +9 -6
  8. package/dist/handle-error.d.ts +1 -1
  9. package/dist/handle-error.js +1 -1
  10. package/dist/middleware/auth-apikey.js +3 -3
  11. package/dist/middleware/auth-bearer.js +3 -3
  12. package/dist/middleware/auth-cookie.js +5 -5
  13. package/dist/middleware/timeout.js +3 -2
  14. package/dist/middleware-runner.d.ts +3 -6
  15. package/dist/middleware-runner.js +11 -12
  16. package/dist/permissions.d.ts +6 -6
  17. package/dist/permissions.js +15 -16
  18. package/dist/services/user-session-service.d.ts +7 -3
  19. package/dist/services/user-session-service.js +8 -1
  20. package/dist/types/core.types.d.ts +29 -38
  21. package/dist/types/core.types.js +3 -14
  22. package/dist/utils.d.ts +1 -1
  23. package/dist/utils.js +3 -3
  24. package/dist/wirings/channel/channel-common.js +2 -3
  25. package/dist/wirings/channel/channel-handler.d.ts +3 -2
  26. package/dist/wirings/channel/channel-handler.js +6 -9
  27. package/dist/wirings/channel/channel-runner.d.ts +2 -2
  28. package/dist/wirings/channel/channel.types.d.ts +10 -5
  29. package/dist/wirings/channel/local/local-channel-runner.d.ts +1 -1
  30. package/dist/wirings/channel/local/local-channel-runner.js +19 -26
  31. package/dist/wirings/channel/serverless/serverless-channel-runner.d.ts +1 -1
  32. package/dist/wirings/channel/serverless/serverless-channel-runner.js +41 -46
  33. package/dist/wirings/cli/cli-runner.d.ts +5 -5
  34. package/dist/wirings/cli/cli-runner.js +10 -28
  35. package/dist/wirings/cli/cli.types.d.ts +15 -11
  36. package/dist/wirings/http/http-runner.d.ts +6 -6
  37. package/dist/wirings/http/http-runner.js +25 -39
  38. package/dist/wirings/http/http.types.d.ts +5 -10
  39. package/dist/wirings/mcp/mcp-runner.d.ts +2 -2
  40. package/dist/wirings/mcp/mcp-runner.js +12 -19
  41. package/dist/wirings/mcp/mcp.types.d.ts +6 -0
  42. package/dist/wirings/queue/queue-runner.d.ts +3 -3
  43. package/dist/wirings/queue/queue-runner.js +7 -17
  44. package/dist/wirings/queue/queue.types.d.ts +4 -11
  45. package/dist/wirings/rpc/rpc-runner.d.ts +10 -8
  46. package/dist/wirings/rpc/rpc-runner.js +36 -47
  47. package/dist/wirings/scheduler/scheduler-runner.d.ts +3 -3
  48. package/dist/wirings/scheduler/scheduler-runner.js +13 -24
  49. package/dist/wirings/scheduler/scheduler.types.d.ts +3 -8
  50. package/dist/wirings/workflow/index.d.ts +11 -2
  51. package/dist/wirings/workflow/index.js +10 -2
  52. package/dist/wirings/workflow/pikku-workflow-service.d.ts +12 -4
  53. package/dist/wirings/workflow/pikku-workflow-service.js +61 -79
  54. package/dist/wirings/workflow/workflow-runner.d.ts +3 -4
  55. package/dist/wirings/workflow/workflow-runner.js +11 -14
  56. package/dist/wirings/workflow/workflow.types.d.ts +114 -29
  57. package/package.json +2 -2
  58. package/src/errors/errors.ts +3 -3
  59. package/src/factory-functions.test.ts +9 -36
  60. package/src/function/function-runner.test.ts +57 -68
  61. package/src/function/function-runner.ts +51 -28
  62. package/src/function/function-runner.ts.bak +188 -0
  63. package/src/function/functions.types.ts +68 -51
  64. package/src/handle-error.ts +1 -1
  65. package/src/middleware/auth-apikey.test.ts +360 -0
  66. package/src/middleware/auth-apikey.ts +3 -7
  67. package/src/middleware/auth-bearer.test.ts +447 -0
  68. package/src/middleware/auth-bearer.ts +27 -33
  69. package/src/middleware/auth-cookie.test.ts +525 -0
  70. package/src/middleware/auth-cookie.ts +6 -6
  71. package/src/middleware/timeout.ts +4 -2
  72. package/src/middleware-runner.test.ts +58 -127
  73. package/src/middleware-runner.ts +13 -16
  74. package/src/permissions.test.ts +41 -45
  75. package/src/permissions.ts +21 -21
  76. package/src/services/user-session-service.ts +14 -4
  77. package/src/types/core.types.ts +67 -55
  78. package/src/utils.ts +4 -4
  79. package/src/wirings/channel/channel-common.ts +4 -7
  80. package/src/wirings/channel/channel-handler.ts +16 -22
  81. package/src/wirings/channel/channel-runner.ts +2 -2
  82. package/src/wirings/channel/channel.types.ts +14 -11
  83. package/src/wirings/channel/local/local-channel-runner.test.ts +5 -3
  84. package/src/wirings/channel/local/local-channel-runner.ts +23 -38
  85. package/src/wirings/channel/serverless/serverless-channel-runner.ts +51 -77
  86. package/src/wirings/cli/cli-runner.test.ts +7 -7
  87. package/src/wirings/cli/cli-runner.ts +24 -62
  88. package/src/wirings/cli/cli.types.ts +22 -16
  89. package/src/wirings/http/http-runner.test.ts +8 -8
  90. package/src/wirings/http/http-runner.ts +29 -56
  91. package/src/wirings/http/http.types.ts +5 -14
  92. package/src/wirings/mcp/mcp-runner.ts +23 -48
  93. package/src/wirings/mcp/mcp.types.ts +6 -0
  94. package/src/wirings/queue/queue-runner.test.ts +641 -0
  95. package/src/wirings/queue/queue-runner.ts +10 -32
  96. package/src/wirings/queue/queue.types.ts +4 -11
  97. package/src/wirings/rpc/rpc-runner.ts +41 -55
  98. package/src/wirings/scheduler/scheduler-runner.test.ts +627 -0
  99. package/src/wirings/scheduler/scheduler-runner.ts +24 -50
  100. package/src/wirings/scheduler/scheduler.types.ts +3 -9
  101. package/src/wirings/workflow/index.ts +24 -3
  102. package/src/wirings/workflow/pikku-workflow-service.ts +83 -93
  103. package/src/wirings/workflow/workflow-runner.ts +12 -25
  104. package/src/wirings/workflow/workflow.types.ts +144 -54
  105. package/tsconfig.tsbuildinfo +1 -1
@@ -1,4 +1,4 @@
1
- import type { CoreServices, PikkuInteraction } from '../../types/core.types.js'
1
+ import type { CoreServices, PikkuWire } from '../../types/core.types.js'
2
2
  import type { CoreQueueWorker, QueueJob, PikkuQueue } from './queue.types.js'
3
3
  import type {
4
4
  CorePikkuFunctionConfig,
@@ -7,11 +7,7 @@ import type {
7
7
  import { getErrorResponse, PikkuError } from '../../errors/error-handler.js'
8
8
  import { pikkuState } from '../../pikku-state.js'
9
9
  import { addFunction, runPikkuFunc } from '../../function/function-runner.js'
10
- import {
11
- CreateSessionServices,
12
- PikkuWiringTypes,
13
- } from '../../types/core.types.js'
14
- import { rpcService } from '../rpc/rpc-runner.js'
10
+ import { CreateWireServices } from '../../types/core.types.js'
15
11
 
16
12
  /**
17
13
  * Error class for queue processor not found
@@ -71,7 +67,6 @@ export const wireQueueWorker = <
71
67
  permissions: queueWorker.func.permissions,
72
68
  middleware: queueWorker.func.middleware as any,
73
69
  tags: queueWorker.func.tags,
74
- docs: queueWorker.func.docs as any,
75
70
  })
76
71
 
77
72
  // Store processor definition in state - runtime adapters will pick this up
@@ -105,12 +100,12 @@ export async function removeQueueWorker(name: string): Promise<void> {
105
100
  */
106
101
  export async function runQueueJob({
107
102
  singletonServices,
108
- createSessionServices,
103
+ createWireServices,
109
104
  job,
110
105
  updateProgress,
111
106
  }: {
112
107
  singletonServices: CoreServices
113
- createSessionServices?: CreateSessionServices
108
+ createWireServices?: CreateWireServices
114
109
  job: QueueJob
115
110
  updateProgress?: (progress: number | string | object) => Promise<void>
116
111
  }): Promise<void> {
@@ -129,7 +124,7 @@ export async function runQueueJob({
129
124
  throw new Error(`Queue worker registration not found for: ${job.queueName}`)
130
125
  }
131
126
 
132
- // Create the queue interaction object
127
+ // Create the queue wire object
133
128
  const queue: PikkuQueue = {
134
129
  queueName: job.queueName,
135
130
  jobId: job.id,
@@ -150,41 +145,24 @@ export async function runQueueJob({
150
145
  try {
151
146
  logger.info(`Processing job ${job.id} in queue ${job.queueName}`)
152
147
 
153
- const interaction: PikkuInteraction = { queue }
154
- // Use provided singleton services
155
- const getAllServices = async () => {
156
- const sessionServices = await createSessionServices?.(
157
- singletonServices,
158
- { queue },
159
- undefined
160
- )
161
-
162
- const services = rpcService.injectRPCService(
163
- {
164
- ...singletonServices,
165
- ...sessionServices,
166
- },
167
- interaction,
168
- false
169
- )
170
-
171
- return services
148
+ const wire: PikkuWire = {
149
+ queue,
172
150
  }
173
151
 
174
152
  // Execute the pikku function with the job data
175
153
  const result = await runPikkuFunc(
176
- PikkuWiringTypes.queue,
154
+ 'queue',
177
155
  job.queueName,
178
156
  processorMeta.pikkuFuncName,
179
157
  {
180
158
  singletonServices,
181
- getAllServices,
159
+ createWireServices,
182
160
  auth: false,
183
161
  data: () => job.data,
184
162
  inheritedMiddleware: processorMeta.middleware,
185
163
  wireMiddleware: queueWorker.middleware,
186
164
  tags: queueWorker.tags,
187
- interaction,
165
+ wire,
188
166
  }
189
167
  )
190
168
 
@@ -1,4 +1,4 @@
1
- import { PikkuDocs, MiddlewareMetadata } from '../../types/core.types.js'
1
+ import { CommonWireMeta } from '../../types/core.types.js'
2
2
  import { CorePikkuFunctionConfig } from '../../function/functions.types.js'
3
3
  import { QueueConfigMapping } from './validate-worker-config.js'
4
4
 
@@ -147,15 +147,9 @@ export interface QueueWorkers {
147
147
  */
148
148
  export type QueueWorkersMeta = Record<
149
149
  string,
150
- {
151
- pikkuFuncName: string
152
- schemaName?: string
150
+ CommonWireMeta & {
153
151
  queueName: string
154
- session?: undefined
155
- docs?: PikkuDocs
156
- tags?: string[]
157
152
  config?: PikkuWorkerConfig
158
- middleware?: MiddlewareMetadata[] // Pre-resolved middleware chain (tag + explicit)
159
153
  }
160
154
  >
161
155
 
@@ -172,14 +166,13 @@ export type CoreQueueWorker<
172
166
  queueName: string
173
167
  func: PikkuFunctionConfig
174
168
  config?: PikkuWorkerConfig
175
- docs?: PikkuDocs
176
- session?: undefined
169
+ errors?: string[]
177
170
  tags?: string[]
178
171
  middleware?: PikkuFunctionConfig['middleware']
179
172
  }
180
173
 
181
174
  /**
182
- * Represents a queue interaction object for middleware
175
+ * Represents a queue wire object for middleware
183
176
  * Provides information and actions for the current queue job execution
184
177
  */
185
178
  export interface PikkuQueue {
@@ -1,8 +1,4 @@
1
- import {
2
- CoreServices,
3
- PikkuInteraction,
4
- PikkuWiringTypes,
5
- } from '../../types/core.types.js'
1
+ import { CoreServices, PikkuWire } from '../../types/core.types.js'
6
2
  import { runPikkuFunc } from '../../function/function-runner.js'
7
3
  import { pikkuState } from '../../pikku-state.js'
8
4
  import { ForbiddenError } from '../../errors/errors.js'
@@ -26,7 +22,7 @@ const getPikkuFunctionName = (rpcName: string): string => {
26
22
  export class ContextAwareRPCService {
27
23
  constructor(
28
24
  private services: CoreServices,
29
- private interaction: PikkuInteraction,
25
+ private wire: PikkuWire,
30
26
  private options: {
31
27
  coerceDataFromSchema?: boolean
32
28
  requiresAuth?: boolean
@@ -48,66 +44,60 @@ export class ContextAwareRPCService {
48
44
  funcName: string,
49
45
  data: In
50
46
  ): Promise<Out> {
51
- const rpcDepth = this.services.rpc?.depth || 0
47
+ const rpcDepth = this.wire.rpc?.depth || 0
48
+ const updatedWire: PikkuWire = {
49
+ ...this.wire,
50
+ rpc: this.wire.rpc
51
+ ? {
52
+ ...this.wire.rpc,
53
+ depth: rpcDepth + 1,
54
+ global: false,
55
+ }
56
+ : undefined,
57
+ }
52
58
  return runPikkuFunc<In, Out>(
53
- PikkuWiringTypes.rpc,
59
+ 'rpc',
54
60
  funcName,
55
61
  getPikkuFunctionName(funcName),
56
62
  {
57
63
  auth: this.options.requiresAuth,
64
+ // TODO: this is a hack since services have already been created
65
+ // but is valid since we don't want to keep creating new wire services
58
66
  singletonServices: this.services,
59
- getAllServices: () => {
60
- this.services.rpc = this.services.rpc
61
- ? ({
62
- ...this.services.rpc,
63
- depth: rpcDepth + 1,
64
- global: false,
65
- } as any)
66
- : undefined
67
- return this.services
68
- },
69
67
  data: () => data,
70
- userSession: this.services.userSession,
71
68
  coerceDataFromSchema: this.options.coerceDataFromSchema,
72
- interaction: this.interaction,
69
+ wire: updatedWire,
73
70
  }
74
71
  )
75
72
  }
76
73
 
77
- public async rpcWithInteraction<In = any, Out = any>(
78
- funcName: string,
74
+ public async rpcWithWire<In = any, Out = any>(
75
+ rpcName: string,
79
76
  data: In,
80
- interaction: PikkuInteraction
77
+ wire: PikkuWire
81
78
  ): Promise<Out> {
82
- const rpcDepth = this.services.rpc?.depth || 0
83
- const mergedInteraction = {
84
- ...this.interaction,
85
- ...interaction,
79
+ const rpcDepth = this.wire.rpc?.depth || 0
80
+ const mergedWire: PikkuWire = {
81
+ ...this.wire,
82
+ ...wire,
83
+ rpc: this.wire.rpc
84
+ ? {
85
+ ...this.wire.rpc,
86
+ depth: rpcDepth + 1,
87
+ global: false,
88
+ }
89
+ : undefined,
86
90
  }
87
91
  return runPikkuFunc<In, Out>(
88
- PikkuWiringTypes.rpc,
89
- funcName,
90
- getPikkuFunctionName(funcName),
92
+ 'rpc',
93
+ rpcName,
94
+ getPikkuFunctionName(rpcName),
91
95
  {
92
96
  auth: this.options.requiresAuth,
93
97
  singletonServices: this.services,
94
- getAllServices: () => {
95
- this.services.rpc = this.services.rpc
96
- ? ({
97
- ...this.services.rpc,
98
- depth: rpcDepth + 1,
99
- global: false,
100
- } as any)
101
- : undefined
102
- return {
103
- ...this.services,
104
- ...mergedInteraction,
105
- }
106
- },
107
98
  data: () => data,
108
- userSession: this.services.userSession,
109
99
  coerceDataFromSchema: this.options.coerceDataFromSchema,
110
- interaction: mergedInteraction,
100
+ wire: mergedWire,
111
101
  }
112
102
  )
113
103
  }
@@ -140,28 +130,24 @@ export class PikkuRPCService<
140
130
  }
141
131
 
142
132
  // Convenience function for initializing
143
- injectRPCService(
133
+ getContextRPCService(
144
134
  services: Services,
145
- interaction: PikkuInteraction,
135
+ wire: PikkuWire,
146
136
  requiresAuth?: boolean | undefined,
147
137
  depth: number = 0
148
- ): Services & { rpc: TypedRPC } {
149
- const serviceCopy = {
150
- ...services,
151
- }
152
- const serviceRPC = new ContextAwareRPCService(serviceCopy, interaction, {
138
+ ): TypedRPC {
139
+ const serviceRPC = new ContextAwareRPCService(services, wire, {
153
140
  coerceDataFromSchema: this.config?.coerceDataFromSchema,
154
141
  requiresAuth,
155
142
  })
156
- serviceCopy.rpc = {
143
+ return {
157
144
  depth,
158
145
  global: false,
159
146
  invoke: serviceRPC.rpc.bind(serviceRPC),
160
147
  invokeExposed: serviceRPC.rpc.bind(serviceRPC),
161
148
  startWorkflow: serviceRPC.startWorkflow.bind(serviceRPC),
162
- rpcWithInteraction: serviceRPC.rpcWithInteraction.bind(serviceRPC),
149
+ rpcWithWire: serviceRPC.rpcWithWire.bind(serviceRPC),
163
150
  } as any
164
- return serviceCopy as Services & { rpc: TypedRPC }
165
151
  }
166
152
  }
167
153