@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.
- package/CHANGELOG.md +11 -1
- package/dist/errors/errors.d.ts +1 -1
- package/dist/errors/errors.js +3 -3
- package/dist/function/function-runner.d.ts +6 -7
- package/dist/function/function-runner.js +35 -16
- package/dist/function/functions.types.d.ts +22 -30
- package/dist/function/functions.types.js +9 -6
- package/dist/handle-error.d.ts +1 -1
- package/dist/handle-error.js +1 -1
- package/dist/middleware/auth-apikey.js +3 -3
- package/dist/middleware/auth-bearer.js +3 -3
- package/dist/middleware/auth-cookie.js +5 -5
- package/dist/middleware/timeout.js +3 -2
- package/dist/middleware-runner.d.ts +3 -6
- package/dist/middleware-runner.js +11 -12
- package/dist/permissions.d.ts +6 -6
- package/dist/permissions.js +15 -16
- package/dist/services/user-session-service.d.ts +7 -3
- package/dist/services/user-session-service.js +8 -1
- package/dist/types/core.types.d.ts +29 -38
- package/dist/types/core.types.js +3 -14
- package/dist/utils.d.ts +1 -1
- package/dist/utils.js +3 -3
- package/dist/wirings/channel/channel-common.js +2 -3
- package/dist/wirings/channel/channel-handler.d.ts +3 -2
- package/dist/wirings/channel/channel-handler.js +6 -9
- package/dist/wirings/channel/channel-runner.d.ts +2 -2
- package/dist/wirings/channel/channel.types.d.ts +10 -5
- package/dist/wirings/channel/local/local-channel-runner.d.ts +1 -1
- package/dist/wirings/channel/local/local-channel-runner.js +19 -26
- package/dist/wirings/channel/serverless/serverless-channel-runner.d.ts +1 -1
- package/dist/wirings/channel/serverless/serverless-channel-runner.js +41 -46
- package/dist/wirings/cli/cli-runner.d.ts +5 -5
- package/dist/wirings/cli/cli-runner.js +10 -28
- package/dist/wirings/cli/cli.types.d.ts +15 -11
- package/dist/wirings/http/http-runner.d.ts +6 -6
- package/dist/wirings/http/http-runner.js +25 -39
- package/dist/wirings/http/http.types.d.ts +5 -10
- package/dist/wirings/mcp/mcp-runner.d.ts +2 -2
- package/dist/wirings/mcp/mcp-runner.js +12 -19
- package/dist/wirings/mcp/mcp.types.d.ts +6 -0
- package/dist/wirings/queue/queue-runner.d.ts +3 -3
- package/dist/wirings/queue/queue-runner.js +7 -17
- package/dist/wirings/queue/queue.types.d.ts +4 -11
- package/dist/wirings/rpc/rpc-runner.d.ts +10 -8
- package/dist/wirings/rpc/rpc-runner.js +36 -47
- package/dist/wirings/scheduler/scheduler-runner.d.ts +3 -3
- package/dist/wirings/scheduler/scheduler-runner.js +13 -24
- package/dist/wirings/scheduler/scheduler.types.d.ts +3 -8
- package/dist/wirings/workflow/index.d.ts +11 -2
- package/dist/wirings/workflow/index.js +10 -2
- package/dist/wirings/workflow/pikku-workflow-service.d.ts +12 -4
- package/dist/wirings/workflow/pikku-workflow-service.js +61 -79
- package/dist/wirings/workflow/workflow-runner.d.ts +3 -4
- package/dist/wirings/workflow/workflow-runner.js +11 -14
- package/dist/wirings/workflow/workflow.types.d.ts +114 -29
- package/package.json +2 -2
- package/src/errors/errors.ts +3 -3
- package/src/factory-functions.test.ts +9 -36
- package/src/function/function-runner.test.ts +57 -68
- package/src/function/function-runner.ts +51 -28
- package/src/function/function-runner.ts.bak +188 -0
- package/src/function/functions.types.ts +68 -51
- package/src/handle-error.ts +1 -1
- package/src/middleware/auth-apikey.test.ts +360 -0
- package/src/middleware/auth-apikey.ts +3 -7
- package/src/middleware/auth-bearer.test.ts +447 -0
- package/src/middleware/auth-bearer.ts +27 -33
- package/src/middleware/auth-cookie.test.ts +525 -0
- package/src/middleware/auth-cookie.ts +6 -6
- package/src/middleware/timeout.ts +4 -2
- package/src/middleware-runner.test.ts +58 -127
- package/src/middleware-runner.ts +13 -16
- package/src/permissions.test.ts +41 -45
- package/src/permissions.ts +21 -21
- package/src/services/user-session-service.ts +14 -4
- package/src/types/core.types.ts +67 -55
- package/src/utils.ts +4 -4
- package/src/wirings/channel/channel-common.ts +4 -7
- package/src/wirings/channel/channel-handler.ts +16 -22
- package/src/wirings/channel/channel-runner.ts +2 -2
- package/src/wirings/channel/channel.types.ts +14 -11
- package/src/wirings/channel/local/local-channel-runner.test.ts +5 -3
- package/src/wirings/channel/local/local-channel-runner.ts +23 -38
- package/src/wirings/channel/serverless/serverless-channel-runner.ts +51 -77
- package/src/wirings/cli/cli-runner.test.ts +7 -7
- package/src/wirings/cli/cli-runner.ts +24 -62
- package/src/wirings/cli/cli.types.ts +22 -16
- package/src/wirings/http/http-runner.test.ts +8 -8
- package/src/wirings/http/http-runner.ts +29 -56
- package/src/wirings/http/http.types.ts +5 -14
- package/src/wirings/mcp/mcp-runner.ts +23 -48
- package/src/wirings/mcp/mcp.types.ts +6 -0
- package/src/wirings/queue/queue-runner.test.ts +641 -0
- package/src/wirings/queue/queue-runner.ts +10 -32
- package/src/wirings/queue/queue.types.ts +4 -11
- package/src/wirings/rpc/rpc-runner.ts +41 -55
- package/src/wirings/scheduler/scheduler-runner.test.ts +627 -0
- package/src/wirings/scheduler/scheduler-runner.ts +24 -50
- package/src/wirings/scheduler/scheduler.types.ts +3 -9
- package/src/wirings/workflow/index.ts +24 -3
- package/src/wirings/workflow/pikku-workflow-service.ts +83 -93
- package/src/wirings/workflow/workflow-runner.ts +12 -25
- package/src/wirings/workflow/workflow.types.ts +144 -54
- package/tsconfig.tsbuildinfo +1 -1
package/src/permissions.ts
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
import {
|
|
2
2
|
CoreServices,
|
|
3
|
-
CoreUserSession,
|
|
4
3
|
PikkuWiringTypes,
|
|
5
4
|
PermissionMetadata,
|
|
5
|
+
PikkuWire,
|
|
6
6
|
} from './types/core.types.js'
|
|
7
7
|
import {
|
|
8
8
|
CorePermissionGroup,
|
|
@@ -16,14 +16,14 @@ import { freezeDedupe } from './utils.js'
|
|
|
16
16
|
* This function validates permissions by iterating over permission groups and executing the corresponding permission functions. If all functions in at least one group return true, the permission is considered valid.
|
|
17
17
|
* @param services - The core services required for permission validation.
|
|
18
18
|
* @param data - The data to be used in the permission validation functions.
|
|
19
|
-
* @param
|
|
19
|
+
* @param wire - The wire object containing request/response context and session.
|
|
20
20
|
* @returns A promise that resolves to void.
|
|
21
21
|
*/
|
|
22
|
-
export const verifyPermissions = async (
|
|
22
|
+
export const verifyPermissions = async <Out = any>(
|
|
23
23
|
permissions: CorePermissionGroup,
|
|
24
24
|
services: CoreServices,
|
|
25
25
|
data: any,
|
|
26
|
-
|
|
26
|
+
wire: PikkuWire<any, never, any, never, never, never>
|
|
27
27
|
): Promise<boolean> => {
|
|
28
28
|
if (!permissions) {
|
|
29
29
|
return true
|
|
@@ -38,13 +38,13 @@ export const verifyPermissions = async (
|
|
|
38
38
|
for (const funcs of permissionGroups) {
|
|
39
39
|
if (funcs instanceof Array) {
|
|
40
40
|
const permissioned = await Promise.all(
|
|
41
|
-
funcs.map((func) => func(services, data,
|
|
41
|
+
funcs.map((func) => func(services, data, wire))
|
|
42
42
|
)
|
|
43
43
|
if (permissioned.every((result) => result)) {
|
|
44
44
|
valid = true
|
|
45
45
|
}
|
|
46
46
|
} else {
|
|
47
|
-
valid = await funcs(services, data,
|
|
47
|
+
valid = await funcs(services, data, wire as any)
|
|
48
48
|
}
|
|
49
49
|
if (valid) {
|
|
50
50
|
return true
|
|
@@ -190,14 +190,14 @@ const combinedPermissionsCache: Record<
|
|
|
190
190
|
PikkuWiringTypes,
|
|
191
191
|
Record<string, readonly (CorePermissionGroup | CorePikkuPermission)[]>
|
|
192
192
|
> = {
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
193
|
+
http: {},
|
|
194
|
+
rpc: {},
|
|
195
|
+
channel: {},
|
|
196
|
+
queue: {},
|
|
197
|
+
scheduler: {},
|
|
198
|
+
mcp: {},
|
|
199
|
+
cli: {},
|
|
200
|
+
workflow: {},
|
|
201
201
|
}
|
|
202
202
|
|
|
203
203
|
/**
|
|
@@ -335,17 +335,17 @@ export const runPermissions = async (
|
|
|
335
335
|
wirePermissions,
|
|
336
336
|
funcInheritedPermissions,
|
|
337
337
|
funcPermissions,
|
|
338
|
-
|
|
338
|
+
services,
|
|
339
|
+
wire,
|
|
339
340
|
data,
|
|
340
|
-
session,
|
|
341
341
|
}: {
|
|
342
342
|
wireInheritedPermissions?: PermissionMetadata[]
|
|
343
343
|
wirePermissions?: CorePermissionGroup | CorePikkuPermission[]
|
|
344
344
|
funcInheritedPermissions?: PermissionMetadata[]
|
|
345
345
|
funcPermissions?: CorePermissionGroup | CorePikkuPermission[]
|
|
346
|
-
|
|
346
|
+
services: CoreServices
|
|
347
|
+
wire: PikkuWire<any, never, any, never, never, never>
|
|
347
348
|
data: any
|
|
348
|
-
session?: CoreUserSession
|
|
349
349
|
}
|
|
350
350
|
) => {
|
|
351
351
|
// Combine all permissions: wireInheritedPermissions → wirePermissions → funcInheritedPermissions → funcPermissions
|
|
@@ -362,9 +362,9 @@ export const runPermissions = async (
|
|
|
362
362
|
for (const permission of allPermissions) {
|
|
363
363
|
const result = await verifyPermissions(
|
|
364
364
|
typeof permission === 'function' ? { permission } : permission,
|
|
365
|
-
|
|
365
|
+
services,
|
|
366
366
|
data,
|
|
367
|
-
|
|
367
|
+
wire
|
|
368
368
|
)
|
|
369
369
|
if (result) {
|
|
370
370
|
permissioned = true
|
|
@@ -372,7 +372,7 @@ export const runPermissions = async (
|
|
|
372
372
|
}
|
|
373
373
|
}
|
|
374
374
|
if (!permissioned) {
|
|
375
|
-
|
|
375
|
+
services.logger.debug('Permission denied - combined permissions')
|
|
376
376
|
throw new ForbiddenError('Permission denied')
|
|
377
377
|
}
|
|
378
378
|
}
|
|
@@ -1,18 +1,21 @@
|
|
|
1
1
|
import { ChannelStore } from '../wirings/channel/channel-store.js'
|
|
2
2
|
import { CoreUserSession } from '../types/core.types.js'
|
|
3
3
|
|
|
4
|
-
export interface
|
|
4
|
+
export interface SessionService<UserSession extends CoreUserSession> {
|
|
5
5
|
sessionChanged: boolean
|
|
6
|
+
initial: UserSession | undefined
|
|
6
7
|
setInitial(session: UserSession): void
|
|
8
|
+
freezeInitial(): UserSession | undefined
|
|
7
9
|
set(session: UserSession): Promise<void> | void
|
|
8
10
|
clear(): Promise<void> | void
|
|
9
|
-
get(): Promise<UserSession
|
|
11
|
+
get(): Promise<UserSession> | UserSession | undefined
|
|
10
12
|
}
|
|
11
13
|
|
|
12
|
-
export class
|
|
13
|
-
implements
|
|
14
|
+
export class PikkuSessionService<UserSession extends CoreUserSession>
|
|
15
|
+
implements SessionService<UserSession>
|
|
14
16
|
{
|
|
15
17
|
public sessionChanged = false
|
|
18
|
+
public initial: UserSession | undefined
|
|
16
19
|
private session: UserSession | undefined
|
|
17
20
|
constructor(
|
|
18
21
|
private channelStore?: ChannelStore<unknown, unknown, UserSession>,
|
|
@@ -27,6 +30,13 @@ export class PikkuUserSessionService<UserSession extends CoreUserSession>
|
|
|
27
30
|
this.session = session
|
|
28
31
|
}
|
|
29
32
|
|
|
33
|
+
public freezeInitial() {
|
|
34
|
+
if (this.initial === undefined) {
|
|
35
|
+
this.initial = this.session
|
|
36
|
+
}
|
|
37
|
+
return this.initial
|
|
38
|
+
}
|
|
39
|
+
|
|
30
40
|
public set(session: UserSession) {
|
|
31
41
|
this.sessionChanged = true
|
|
32
42
|
this.session = session
|
package/src/types/core.types.ts
CHANGED
|
@@ -3,7 +3,7 @@ import { VariablesService } from '../services/variables-service.js'
|
|
|
3
3
|
import { SchemaService } from '../services/schema-service.js'
|
|
4
4
|
import { JWTService } from '../services/jwt-service.js'
|
|
5
5
|
import { PikkuHTTP } from '../wirings/http/http.types.js'
|
|
6
|
-
import {
|
|
6
|
+
import { SessionService } from '../services/user-session-service.js'
|
|
7
7
|
import { PikkuChannel } from '../wirings/channel/channel.types.js'
|
|
8
8
|
import { PikkuRPC } from '../wirings/rpc/rpc-types.js'
|
|
9
9
|
import { PikkuMCP } from '../wirings/mcp/mcp.types.js'
|
|
@@ -11,23 +11,22 @@ import { PikkuScheduledTask } from '../wirings/scheduler/scheduler.types.js'
|
|
|
11
11
|
import { PikkuQueue, QueueService } from '../wirings/queue/queue.types.js'
|
|
12
12
|
import { PikkuCLI } from '../wirings/cli/cli.types.js'
|
|
13
13
|
import {
|
|
14
|
-
|
|
14
|
+
PikkuWorkflowWire,
|
|
15
15
|
WorkflowService,
|
|
16
16
|
WorkflowServiceConfig,
|
|
17
|
-
|
|
17
|
+
WorkflowStepWire,
|
|
18
18
|
} from '../wirings/workflow/workflow.types.js'
|
|
19
19
|
import { SchedulerService } from '../services/scheduler-service.js'
|
|
20
20
|
|
|
21
|
-
export
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
}
|
|
21
|
+
export type PikkuWiringTypes =
|
|
22
|
+
| 'http'
|
|
23
|
+
| 'scheduler'
|
|
24
|
+
| 'channel'
|
|
25
|
+
| 'rpc'
|
|
26
|
+
| 'queue'
|
|
27
|
+
| 'mcp'
|
|
28
|
+
| 'cli'
|
|
29
|
+
| 'workflow'
|
|
31
30
|
|
|
32
31
|
export interface FunctionServicesMeta {
|
|
33
32
|
optimized: boolean
|
|
@@ -85,17 +84,18 @@ export type FunctionRuntimeMeta = {
|
|
|
85
84
|
}
|
|
86
85
|
|
|
87
86
|
export type FunctionMeta = FunctionRuntimeMeta &
|
|
88
|
-
Partial<
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
87
|
+
Partial<
|
|
88
|
+
{
|
|
89
|
+
name: string
|
|
90
|
+
services: FunctionServicesMeta
|
|
91
|
+
usedWires: string[]
|
|
92
|
+
inputs: string[] | null
|
|
93
|
+
outputs: string[] | null
|
|
94
|
+
middleware: MiddlewareMetadata[]
|
|
95
|
+
permissions: PermissionMetadata[]
|
|
96
|
+
isDirectFunction: boolean
|
|
97
|
+
} & CommonWireMeta
|
|
98
|
+
>
|
|
99
99
|
|
|
100
100
|
export type FunctionsRuntimeMeta = Record<string, FunctionRuntimeMeta>
|
|
101
101
|
export type FunctionsMeta = Record<string, FunctionMeta>
|
|
@@ -178,31 +178,44 @@ export interface CoreSingletonServices<Config extends CoreConfig = CoreConfig> {
|
|
|
178
178
|
}
|
|
179
179
|
|
|
180
180
|
/**
|
|
181
|
-
* Represents different forms of
|
|
181
|
+
* Represents different forms of wire within Pikku and the outside world.
|
|
182
182
|
*/
|
|
183
|
-
export type
|
|
183
|
+
export type PikkuWire<
|
|
184
|
+
In = unknown,
|
|
185
|
+
Out = unknown,
|
|
186
|
+
HasInitialSession extends boolean = false,
|
|
187
|
+
UserSession extends CoreUserSession = CoreUserSession,
|
|
188
|
+
TypedRPC extends PikkuRPC = PikkuRPC,
|
|
189
|
+
IsChannel extends true | null = null,
|
|
190
|
+
MCPTools extends string | never = never,
|
|
191
|
+
TypedWorkflow extends PikkuWorkflowWire | never = PikkuWorkflowWire,
|
|
192
|
+
> = Partial<{
|
|
184
193
|
http: PikkuHTTP<In>
|
|
185
|
-
mcp: PikkuMCP
|
|
186
|
-
rpc:
|
|
187
|
-
channel:
|
|
194
|
+
mcp: PikkuMCP<MCPTools>
|
|
195
|
+
rpc: TypedRPC
|
|
196
|
+
channel: [IsChannel] extends [null]
|
|
197
|
+
? PikkuChannel<unknown, Out>
|
|
198
|
+
: PikkuChannel<unknown, Out> | undefined
|
|
188
199
|
scheduledTask: PikkuScheduledTask
|
|
189
200
|
queue: PikkuQueue
|
|
190
201
|
cli: PikkuCLI
|
|
191
|
-
workflow:
|
|
192
|
-
workflowStep:
|
|
202
|
+
workflow: TypedWorkflow
|
|
203
|
+
workflowStep: WorkflowStepWire
|
|
204
|
+
initialSession: HasInitialSession extends true
|
|
205
|
+
? UserSession
|
|
206
|
+
: UserSession | undefined
|
|
207
|
+
session: SessionService<UserSession>
|
|
193
208
|
}>
|
|
194
209
|
|
|
195
210
|
/**
|
|
196
|
-
* A function that can wrap an
|
|
211
|
+
* A function that can wrap an wire and be called before or after
|
|
197
212
|
*/
|
|
198
213
|
export type CorePikkuMiddleware<
|
|
199
214
|
SingletonServices extends CoreSingletonServices = CoreSingletonServices,
|
|
200
215
|
UserSession extends CoreUserSession = CoreUserSession,
|
|
201
216
|
> = (
|
|
202
|
-
services: SingletonServices
|
|
203
|
-
|
|
204
|
-
},
|
|
205
|
-
interactions: PikkuInteraction,
|
|
217
|
+
services: SingletonServices,
|
|
218
|
+
wires: PikkuWire<unknown, unknown, false, UserSession>,
|
|
206
219
|
next: () => Promise<void>
|
|
207
220
|
) => Promise<void>
|
|
208
221
|
|
|
@@ -254,7 +267,7 @@ export type CorePikkuMiddlewareGroup<
|
|
|
254
267
|
* ```typescript
|
|
255
268
|
* // Direct function syntax
|
|
256
269
|
* export const logMiddleware = pikkuMiddleware(
|
|
257
|
-
* async ({ logger },
|
|
270
|
+
* async ({ logger }, next) => {
|
|
258
271
|
* logger.info('Request started')
|
|
259
272
|
* await next()
|
|
260
273
|
* }
|
|
@@ -264,7 +277,7 @@ export type CorePikkuMiddlewareGroup<
|
|
|
264
277
|
* export const logMiddleware = pikkuMiddleware({
|
|
265
278
|
* name: 'Request Logger',
|
|
266
279
|
* description: 'Logs request information',
|
|
267
|
-
* func: async ({ logger },
|
|
280
|
+
* func: async ({ logger }, next) => {
|
|
268
281
|
* logger.info('Request started')
|
|
269
282
|
* await next()
|
|
270
283
|
* }
|
|
@@ -292,7 +305,7 @@ export const pikkuMiddleware = <
|
|
|
292
305
|
* message,
|
|
293
306
|
* level = 'info'
|
|
294
307
|
* }) => {
|
|
295
|
-
* return pikkuMiddleware(async ({ logger },
|
|
308
|
+
* return pikkuMiddleware(async ({ logger }, next) => {
|
|
296
309
|
* logger[level](message)
|
|
297
310
|
* await next()
|
|
298
311
|
* })
|
|
@@ -306,20 +319,15 @@ export const pikkuMiddlewareFactory = <In = any>(
|
|
|
306
319
|
}
|
|
307
320
|
|
|
308
321
|
/**
|
|
309
|
-
* Represents the core services used by Pikku, including singleton services
|
|
322
|
+
* Represents the core services used by Pikku, including singleton services.
|
|
310
323
|
*/
|
|
311
|
-
export type CoreServices<
|
|
312
|
-
SingletonServices
|
|
313
|
-
UserSession extends CoreUserSession = CoreUserSession,
|
|
314
|
-
> = SingletonServices &
|
|
315
|
-
PikkuInteraction & {
|
|
316
|
-
userSession?: UserSessionService<UserSession>
|
|
317
|
-
}
|
|
324
|
+
export type CoreServices<SingletonServices = CoreSingletonServices> =
|
|
325
|
+
SingletonServices
|
|
318
326
|
|
|
319
|
-
export type
|
|
327
|
+
export type WireServices<
|
|
320
328
|
SingletonServices extends CoreSingletonServices = CoreSingletonServices,
|
|
321
329
|
Services = CoreServices<SingletonServices>,
|
|
322
|
-
> = Omit<Services, keyof SingletonServices |
|
|
330
|
+
> = Omit<Services, keyof SingletonServices | 'session'>
|
|
323
331
|
|
|
324
332
|
/**
|
|
325
333
|
* Defines a function type for creating singleton services from the given configuration.
|
|
@@ -335,16 +343,15 @@ export type CreateSingletonServices<
|
|
|
335
343
|
/**
|
|
336
344
|
* Defines a function type for creating session-specific services.
|
|
337
345
|
*/
|
|
338
|
-
export type
|
|
346
|
+
export type CreateWireServices<
|
|
339
347
|
SingletonServices extends CoreSingletonServices = CoreSingletonServices,
|
|
340
348
|
Services extends
|
|
341
349
|
CoreServices<SingletonServices> = CoreServices<SingletonServices>,
|
|
342
350
|
UserSession extends CoreUserSession = CoreUserSession,
|
|
343
351
|
> = (
|
|
344
352
|
services: SingletonServices,
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
) => Promise<SessionServices<Services, SingletonServices>>
|
|
353
|
+
wire: PikkuWire<unknown, unknown, false, UserSession>
|
|
354
|
+
) => Promise<WireServices<Services, SingletonServices>>
|
|
348
355
|
|
|
349
356
|
/**
|
|
350
357
|
* Defines a function type for creating config.
|
|
@@ -357,11 +364,16 @@ export type CreateConfig<
|
|
|
357
364
|
/**
|
|
358
365
|
* Represents the documentation for a route, including summary, description, tags, and errors.
|
|
359
366
|
*/
|
|
360
|
-
export type
|
|
367
|
+
export type CommonWireMeta = {
|
|
368
|
+
pikkuFuncName: string
|
|
369
|
+
|
|
370
|
+
tags?: string[]
|
|
361
371
|
summary?: string
|
|
362
372
|
description?: string
|
|
363
|
-
tags?: string[]
|
|
364
373
|
errors?: string[]
|
|
374
|
+
|
|
375
|
+
middleware?: MiddlewareMetadata[]
|
|
376
|
+
permissions?: PermissionMetadata[]
|
|
365
377
|
}
|
|
366
378
|
|
|
367
379
|
/**
|
package/src/utils.ts
CHANGED
|
@@ -1,14 +1,14 @@
|
|
|
1
1
|
import { Logger } from './services/logger.js'
|
|
2
2
|
|
|
3
|
-
// TODO:
|
|
3
|
+
// TODO: WireServices probably needs it's own type
|
|
4
4
|
// but is an issue for the future and will be tackled
|
|
5
5
|
// with dependency injection
|
|
6
|
-
export const
|
|
6
|
+
export const closeWireServices = async (
|
|
7
7
|
logger: Logger,
|
|
8
|
-
|
|
8
|
+
wireServices: Record<string, any>
|
|
9
9
|
) => {
|
|
10
10
|
await Promise.all(
|
|
11
|
-
Object.values(
|
|
11
|
+
Object.values(wireServices).map(async (service) => {
|
|
12
12
|
if (service?.close) {
|
|
13
13
|
try {
|
|
14
14
|
await service.close()
|
|
@@ -1,8 +1,4 @@
|
|
|
1
|
-
import {
|
|
2
|
-
CoreServices,
|
|
3
|
-
PikkuWiringTypes,
|
|
4
|
-
CorePikkuMiddleware,
|
|
5
|
-
} from '../../types/core.types.js'
|
|
1
|
+
import { CoreServices, CorePikkuMiddleware } from '../../types/core.types.js'
|
|
6
2
|
import { CoreChannel, ChannelMessageMeta } from './channel.types.js'
|
|
7
3
|
import { combineMiddleware, runMiddleware } from '../../middleware-runner.js'
|
|
8
4
|
import { runPikkuFuncDirectly } from '../../function/function-runner.js'
|
|
@@ -49,7 +45,7 @@ export const runChannelLifecycleWithMiddleware = async ({
|
|
|
49
45
|
|
|
50
46
|
// Use combineMiddleware to properly resolve metadata + inline middleware
|
|
51
47
|
const allMiddleware = combineMiddleware(
|
|
52
|
-
|
|
48
|
+
'channel',
|
|
53
49
|
`${channelConfig.name}:${lifecycleType}`,
|
|
54
50
|
{
|
|
55
51
|
wireInheritedMiddleware: meta.middleware,
|
|
@@ -61,7 +57,8 @@ export const runChannelLifecycleWithMiddleware = async ({
|
|
|
61
57
|
const runLifecycle = async () => {
|
|
62
58
|
return await runPikkuFuncDirectly(
|
|
63
59
|
meta.pikkuFuncName,
|
|
64
|
-
|
|
60
|
+
services,
|
|
61
|
+
{ channel },
|
|
65
62
|
data
|
|
66
63
|
)
|
|
67
64
|
}
|
|
@@ -2,7 +2,6 @@ import {
|
|
|
2
2
|
CoreServices,
|
|
3
3
|
JSONValue,
|
|
4
4
|
CoreUserSession,
|
|
5
|
-
PikkuWiringTypes,
|
|
6
5
|
} from '../../types/core.types.js'
|
|
7
6
|
import {
|
|
8
7
|
ChannelMessageMeta,
|
|
@@ -11,6 +10,7 @@ import {
|
|
|
11
10
|
} from './channel.types.js'
|
|
12
11
|
import { pikkuState } from '../../pikku-state.js'
|
|
13
12
|
import { runPikkuFunc } from '../../function/function-runner.js'
|
|
13
|
+
import { SessionService } from '../../services/user-session-service.js'
|
|
14
14
|
|
|
15
15
|
const getRouteMeta = (
|
|
16
16
|
channelName: string,
|
|
@@ -62,7 +62,8 @@ const validateAuth = (
|
|
|
62
62
|
export const processMessageHandlers = (
|
|
63
63
|
services: CoreServices,
|
|
64
64
|
channelConfig: CoreChannel<any, any>,
|
|
65
|
-
channelHandler: PikkuChannelHandler
|
|
65
|
+
channelHandler: PikkuChannelHandler,
|
|
66
|
+
userSession?: SessionService<CoreUserSession>
|
|
66
67
|
) => {
|
|
67
68
|
const logger = services.logger
|
|
68
69
|
const requiresSession = channelConfig.auth !== false
|
|
@@ -121,26 +122,19 @@ export const processMessageHandlers = (
|
|
|
121
122
|
? `${channelConfig.name}:${routingProperty}:${routerValue}`
|
|
122
123
|
: `${channelConfig.name}:default`
|
|
123
124
|
|
|
124
|
-
return await runPikkuFunc(
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
wireMiddleware,
|
|
138
|
-
inheritedPermissions,
|
|
139
|
-
wirePermissions,
|
|
140
|
-
tags: channelConfig.tags,
|
|
141
|
-
interaction: { channel: channelHandler.getChannel() },
|
|
142
|
-
}
|
|
143
|
-
)
|
|
125
|
+
return await runPikkuFunc('channel', cacheKey, pikkuFuncName, {
|
|
126
|
+
singletonServices: services,
|
|
127
|
+
data: () => data,
|
|
128
|
+
inheritedMiddleware,
|
|
129
|
+
wireMiddleware,
|
|
130
|
+
inheritedPermissions,
|
|
131
|
+
wirePermissions,
|
|
132
|
+
tags: channelConfig.tags,
|
|
133
|
+
wire: {
|
|
134
|
+
channel: channelHandler.getChannel(),
|
|
135
|
+
session: userSession,
|
|
136
|
+
},
|
|
137
|
+
})
|
|
144
138
|
}
|
|
145
139
|
|
|
146
140
|
return async (rawData): Promise<unknown> => {
|
|
@@ -3,7 +3,7 @@ import { addFunction } from '../../function/function-runner.js'
|
|
|
3
3
|
import { CorePikkuPermission } from '../../function/functions.types.js'
|
|
4
4
|
import { pikkuState } from '../../pikku-state.js'
|
|
5
5
|
import { coerceTopLevelDataFromSchema, validateSchema } from '../../schema.js'
|
|
6
|
-
import {
|
|
6
|
+
import { SessionService } from '../../services/user-session-service.js'
|
|
7
7
|
import { CorePikkuMiddleware } from '../../types/core.types.js'
|
|
8
8
|
import { httpRouter } from '../http/routers/http-router.js'
|
|
9
9
|
import {
|
|
@@ -128,7 +128,7 @@ export const openChannel = async ({
|
|
|
128
128
|
request,
|
|
129
129
|
}: Pick<CoreChannel<unknown, string>, 'route'> &
|
|
130
130
|
RunChannelParams<unknown> & {
|
|
131
|
-
userSession:
|
|
131
|
+
userSession: SessionService<any>
|
|
132
132
|
} & RunChannelOptions): Promise<{
|
|
133
133
|
openingData: unknown
|
|
134
134
|
channelConfig: CoreChannel<unknown, any>
|
|
@@ -5,9 +5,8 @@ import {
|
|
|
5
5
|
PikkuHTTPResponse,
|
|
6
6
|
} from '../http/http.types.js'
|
|
7
7
|
import {
|
|
8
|
-
PikkuDocs,
|
|
9
8
|
CoreSingletonServices,
|
|
10
|
-
|
|
9
|
+
CreateWireServices,
|
|
11
10
|
CorePikkuMiddleware,
|
|
12
11
|
MiddlewareMetadata,
|
|
13
12
|
PermissionMetadata,
|
|
@@ -33,12 +32,15 @@ export type RunChannelParams<ChannelData> = {
|
|
|
33
32
|
singletonServices: CoreSingletonServices
|
|
34
33
|
request?: PikkuHTTPRequest<ChannelData>
|
|
35
34
|
response?: PikkuHTTPResponse
|
|
36
|
-
|
|
35
|
+
createWireServices?: CreateWireServices
|
|
37
36
|
}
|
|
38
37
|
|
|
39
38
|
export interface ChannelMessageMeta {
|
|
40
39
|
pikkuFuncName: string
|
|
41
|
-
|
|
40
|
+
summary?: string
|
|
41
|
+
description?: string
|
|
42
|
+
errors?: string[]
|
|
43
|
+
tags?: string[]
|
|
42
44
|
middleware?: MiddlewareMetadata[]
|
|
43
45
|
permissions?: PermissionMetadata[]
|
|
44
46
|
}
|
|
@@ -54,7 +56,9 @@ export interface ChannelMeta {
|
|
|
54
56
|
disconnect: ChannelMessageMeta | null
|
|
55
57
|
message: ChannelMessageMeta | null
|
|
56
58
|
messageWirings: Record<string, Record<string, ChannelMessageMeta>>
|
|
57
|
-
|
|
59
|
+
summary?: string
|
|
60
|
+
description?: string
|
|
61
|
+
errors?: string[]
|
|
58
62
|
tags?: string[]
|
|
59
63
|
middleware?: MiddlewareMetadata[] // Pre-resolved middleware chain (tag + explicit)
|
|
60
64
|
permissions?: PermissionMetadata[] // Pre-resolved permission chain (tag + explicit)
|
|
@@ -66,20 +70,19 @@ export type CoreChannel<
|
|
|
66
70
|
ChannelData,
|
|
67
71
|
Channel extends string,
|
|
68
72
|
ChannelConnect = CorePikkuFunctionConfig<
|
|
69
|
-
| CorePikkuFunction<void, unknown
|
|
70
|
-
| CorePikkuFunctionSessionless<void, unknown
|
|
73
|
+
| CorePikkuFunction<void, unknown>
|
|
74
|
+
| CorePikkuFunctionSessionless<void, unknown>,
|
|
71
75
|
CorePikkuPermission<void>,
|
|
72
76
|
CorePikkuMiddleware
|
|
73
77
|
>,
|
|
74
78
|
ChannelDisconnect = CorePikkuFunctionConfig<
|
|
75
|
-
|
|
76
|
-
| CorePikkuFunctionSessionless<void, void, ChannelData>,
|
|
79
|
+
CorePikkuFunction<void, void> | CorePikkuFunctionSessionless<void, void>,
|
|
77
80
|
CorePikkuPermission<void>,
|
|
78
81
|
CorePikkuMiddleware
|
|
79
82
|
>,
|
|
80
83
|
ChannelFunctionMessage = CorePikkuFunctionConfig<
|
|
81
|
-
| CorePikkuFunction<unknown, unknown
|
|
82
|
-
| CorePikkuFunctionSessionless<unknown, unknown
|
|
84
|
+
| CorePikkuFunction<unknown, unknown>
|
|
85
|
+
| CorePikkuFunctionSessionless<unknown, unknown>,
|
|
83
86
|
CorePikkuPermission<unknown>,
|
|
84
87
|
CorePikkuMiddleware
|
|
85
88
|
>,
|
|
@@ -95,7 +95,9 @@ export class PikkuMockResponse implements PikkuHTTPResponse {
|
|
|
95
95
|
}
|
|
96
96
|
}
|
|
97
97
|
|
|
98
|
-
const
|
|
98
|
+
const mockCreateWireServices = async () => ({
|
|
99
|
+
wireServiceMock: true,
|
|
100
|
+
})
|
|
99
101
|
|
|
100
102
|
beforeEach(() => {
|
|
101
103
|
resetPikkuState()
|
|
@@ -113,7 +115,7 @@ test('runChannel should return undefined and 404 if no matching channel is found
|
|
|
113
115
|
channelId: 'test-channel-id',
|
|
114
116
|
request: new PikkuMockRequest('/non-existent-channel', 'get'),
|
|
115
117
|
response: mockResponse,
|
|
116
|
-
|
|
118
|
+
createWireServices: mockCreateWireServices,
|
|
117
119
|
})
|
|
118
120
|
|
|
119
121
|
assert.equal(
|
|
@@ -149,7 +151,7 @@ test('runChannel should return a channel handler if channel matches and no auth
|
|
|
149
151
|
request: new PikkuMockRequest('/test-channel', 'get'),
|
|
150
152
|
response: new PikkuMockResponse(),
|
|
151
153
|
route: '/test-channel',
|
|
152
|
-
|
|
154
|
+
createWireServices: mockCreateWireServices,
|
|
153
155
|
})
|
|
154
156
|
|
|
155
157
|
assert.ok(result, 'Should return a PikkuChannelHandler instance')
|