@pikku/core 0.11.0 → 0.11.2
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 +35 -1
- package/dist/errors/error-handler.js +3 -3
- package/dist/errors/errors.d.ts +1 -1
- package/dist/errors/errors.js +3 -3
- package/dist/function/function-runner.d.ts +8 -8
- package/dist/function/function-runner.js +88 -23
- package/dist/function/functions.types.d.ts +104 -33
- package/dist/function/functions.types.js +50 -7
- package/dist/handle-error.d.ts +1 -1
- package/dist/handle-error.js +1 -1
- package/dist/index.d.ts +6 -2
- package/dist/index.js +5 -2
- package/dist/middleware/auth-apikey.d.ts +1 -1
- package/dist/middleware/auth-apikey.js +3 -3
- package/dist/middleware/auth-bearer.d.ts +1 -1
- package/dist/middleware/auth-bearer.js +3 -3
- package/dist/middleware/auth-cookie.d.ts +1 -1
- package/dist/middleware/auth-cookie.js +5 -5
- package/dist/middleware/timeout.d.ts +1 -1
- package/dist/middleware/timeout.js +3 -2
- package/dist/middleware-runner.d.ts +6 -20
- package/dist/middleware-runner.js +19 -20
- package/dist/permissions.d.ts +6 -89
- package/dist/permissions.js +24 -81
- package/dist/pikku-state.d.ts +32 -104
- package/dist/pikku-state.js +140 -66
- package/dist/schema.d.ts +8 -6
- package/dist/schema.js +25 -13
- package/dist/services/user-session-service.d.ts +7 -3
- package/dist/services/user-session-service.js +8 -1
- package/dist/services/workflow-service.d.ts +38 -0
- package/dist/services/workflow-service.js +1 -0
- package/dist/types/core.types.d.ts +40 -41
- package/dist/types/core.types.js +4 -15
- package/dist/types/state.types.d.ts +135 -0
- package/dist/types/state.types.js +1 -0
- package/dist/utils.d.ts +9 -1
- package/dist/utils.js +48 -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 +7 -10
- package/dist/wirings/channel/channel-runner.d.ts +2 -2
- package/dist/wirings/channel/channel-runner.js +5 -5
- 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/log-channels.js +1 -1
- package/dist/wirings/channel/serverless/serverless-channel-runner.d.ts +1 -1
- package/dist/wirings/channel/serverless/serverless-channel-runner.js +43 -48
- package/dist/wirings/cli/channel/cli-channel-runner.js +3 -2
- package/dist/wirings/cli/cli-runner.d.ts +5 -5
- package/dist/wirings/cli/cli-runner.js +20 -38
- package/dist/wirings/cli/cli.types.d.ts +16 -11
- package/dist/wirings/forge-node/forge-node.types.d.ts +120 -0
- package/dist/wirings/forge-node/forge-node.types.js +38 -0
- package/dist/wirings/forge-node/index.d.ts +1 -0
- package/dist/wirings/forge-node/index.js +1 -0
- package/dist/wirings/http/http-runner.d.ts +8 -8
- package/dist/wirings/http/http-runner.js +46 -51
- package/dist/wirings/http/http.types.d.ts +19 -11
- package/dist/wirings/http/log-http-routes.js +1 -1
- package/dist/wirings/http/routers/path-to-regex.js +2 -2
- package/dist/wirings/mcp/mcp-runner.d.ts +2 -2
- package/dist/wirings/mcp/mcp-runner.js +33 -40
- 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 +13 -23
- package/dist/wirings/queue/queue.types.d.ts +4 -11
- package/dist/wirings/rpc/index.d.ts +1 -1
- package/dist/wirings/rpc/index.js +1 -1
- package/dist/wirings/rpc/rpc-runner.d.ts +17 -16
- package/dist/wirings/rpc/rpc-runner.js +98 -61
- package/dist/wirings/rpc/rpc-types.d.ts +7 -0
- package/dist/wirings/scheduler/log-schedulers.js +1 -1
- package/dist/wirings/scheduler/scheduler-runner.d.ts +3 -3
- package/dist/wirings/scheduler/scheduler-runner.js +18 -29
- package/dist/wirings/scheduler/scheduler.types.d.ts +3 -8
- package/dist/wirings/trigger/index.d.ts +2 -0
- package/dist/wirings/trigger/index.js +2 -0
- package/dist/wirings/trigger/trigger-runner.d.ts +29 -0
- package/dist/wirings/trigger/trigger-runner.js +57 -0
- package/dist/wirings/trigger/trigger.types.d.ts +42 -0
- package/dist/wirings/trigger/trigger.types.js +1 -0
- package/dist/wirings/workflow/dsl/index.d.ts +5 -0
- package/dist/wirings/workflow/dsl/index.js +4 -0
- package/dist/wirings/workflow/dsl/workflow-dsl.types.d.ts +286 -0
- package/dist/wirings/workflow/dsl/workflow-dsl.types.js +5 -0
- package/dist/wirings/workflow/dsl/workflow-runner.d.ts +5 -0
- package/dist/wirings/workflow/dsl/workflow-runner.js +22 -0
- package/dist/wirings/workflow/graph/graph-node.d.ts +122 -0
- package/dist/wirings/workflow/graph/graph-node.js +58 -0
- package/dist/wirings/workflow/graph/graph-runner.d.ts +35 -0
- package/dist/wirings/workflow/graph/graph-runner.js +452 -0
- package/dist/wirings/workflow/graph/index.d.ts +3 -0
- package/dist/wirings/workflow/graph/index.js +3 -0
- package/dist/wirings/workflow/graph/workflow-graph.types.d.ts +95 -0
- package/dist/wirings/workflow/graph/workflow-graph.types.js +15 -0
- package/dist/wirings/workflow/index.d.ts +7 -3
- package/dist/wirings/workflow/index.js +6 -3
- package/dist/wirings/workflow/pikku-workflow-service.d.ts +108 -10
- package/dist/wirings/workflow/pikku-workflow-service.js +274 -165
- package/dist/wirings/workflow/wire-workflow.d.ts +42 -0
- package/dist/wirings/workflow/wire-workflow.js +53 -0
- package/dist/wirings/workflow/workflow-utils.d.ts +23 -0
- package/dist/wirings/workflow/workflow-utils.js +66 -0
- package/dist/wirings/workflow/workflow.types.d.ts +135 -128
- package/package.json +11 -2
- package/src/errors/error-handler.ts +3 -3
- package/src/errors/errors.ts +3 -3
- package/src/factory-functions.test.ts +9 -36
- package/src/function/function-runner.test.ts +58 -69
- package/src/function/function-runner.ts +130 -37
- package/src/function/functions.types.ts +193 -54
- package/src/handle-error.ts +1 -1
- package/src/index.ts +10 -5
- 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 +29 -25
- package/src/permissions.test.ts +68 -130
- package/src/permissions.ts +41 -100
- package/src/pikku-state.ts +156 -201
- package/src/schema.ts +42 -13
- package/src/services/user-session-service.ts +14 -4
- package/src/services/workflow-service.ts +74 -0
- package/src/types/core.types.ts +79 -58
- package/src/types/state.types.ts +195 -0
- package/src/utils.ts +59 -4
- package/src/wirings/channel/channel-common.ts +4 -7
- package/src/wirings/channel/channel-handler.ts +17 -23
- package/src/wirings/channel/channel-runner.ts +7 -7
- package/src/wirings/channel/channel.types.ts +14 -11
- package/src/wirings/channel/local/local-channel-runner.test.ts +8 -4
- package/src/wirings/channel/local/local-channel-runner.ts +23 -38
- package/src/wirings/channel/log-channels.ts +1 -1
- package/src/wirings/channel/serverless/serverless-channel-runner.ts +53 -79
- package/src/wirings/cli/channel/cli-channel-runner.ts +4 -3
- package/src/wirings/cli/cli-runner.test.ts +26 -26
- package/src/wirings/cli/cli-runner.ts +34 -71
- package/src/wirings/cli/cli.types.ts +23 -16
- package/src/wirings/forge-node/forge-node.types.ts +135 -0
- package/src/wirings/forge-node/index.ts +1 -0
- package/src/wirings/http/http-runner.test.ts +10 -10
- package/src/wirings/http/http-runner.ts +56 -66
- package/src/wirings/http/http.types.ts +19 -14
- package/src/wirings/http/log-http-routes.ts +1 -1
- package/src/wirings/http/routers/path-to-regex.test.ts +30 -19
- package/src/wirings/http/routers/path-to-regex.ts +2 -2
- package/src/wirings/mcp/mcp-runner.ts +44 -69
- 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 +16 -38
- package/src/wirings/queue/queue.types.ts +4 -11
- package/src/wirings/rpc/index.ts +1 -1
- package/src/wirings/rpc/rpc-runner.ts +129 -77
- package/src/wirings/rpc/rpc-types.ts +8 -0
- package/src/wirings/scheduler/log-schedulers.ts +1 -1
- package/src/wirings/scheduler/scheduler-runner.test.ts +627 -0
- package/src/wirings/scheduler/scheduler-runner.ts +29 -55
- package/src/wirings/scheduler/scheduler.types.ts +3 -9
- package/src/wirings/trigger/index.ts +2 -0
- package/src/wirings/trigger/trigger-runner.ts +96 -0
- package/src/wirings/trigger/trigger.types.ts +56 -0
- package/src/wirings/workflow/dsl/index.ts +30 -0
- package/src/wirings/workflow/dsl/workflow-dsl.types.ts +312 -0
- package/src/wirings/workflow/dsl/workflow-runner.ts +27 -0
- package/src/wirings/workflow/graph/graph-node.ts +227 -0
- package/src/wirings/workflow/graph/graph-runner.ts +694 -0
- package/src/wirings/workflow/graph/index.ts +3 -0
- package/src/wirings/workflow/graph/workflow-graph.types.ts +126 -0
- package/src/wirings/workflow/index.ts +60 -12
- package/src/wirings/workflow/pikku-workflow-service.ts +401 -195
- package/src/wirings/workflow/wire-workflow.ts +94 -0
- package/src/wirings/workflow/workflow-utils.ts +120 -0
- package/src/wirings/workflow/workflow.types.ts +167 -188
- package/tsconfig.tsbuildinfo +1 -1
- package/dist/wirings/workflow/workflow-runner.d.ts +0 -35
- package/dist/wirings/workflow/workflow-runner.js +0 -68
- package/src/wirings/workflow/workflow-runner.ts +0 -85
|
@@ -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,14 +95,18 @@ 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()
|
|
104
|
+
httpRouter.reset()
|
|
102
105
|
})
|
|
103
106
|
|
|
104
107
|
afterEach(() => {
|
|
105
108
|
resetPikkuState()
|
|
109
|
+
httpRouter.reset()
|
|
106
110
|
})
|
|
107
111
|
|
|
108
112
|
test('runChannel should return undefined and 404 if no matching channel is found', async () => {
|
|
@@ -113,7 +117,7 @@ test('runChannel should return undefined and 404 if no matching channel is found
|
|
|
113
117
|
channelId: 'test-channel-id',
|
|
114
118
|
request: new PikkuMockRequest('/non-existent-channel', 'get'),
|
|
115
119
|
response: mockResponse,
|
|
116
|
-
|
|
120
|
+
createWireServices: mockCreateWireServices,
|
|
117
121
|
})
|
|
118
122
|
|
|
119
123
|
assert.equal(
|
|
@@ -128,7 +132,7 @@ test('runChannel should return undefined and 404 if no matching channel is found
|
|
|
128
132
|
test('runChannel should return a channel handler if channel matches and no auth required', async () => {
|
|
129
133
|
resetPikkuState()
|
|
130
134
|
|
|
131
|
-
pikkuState('channel', 'meta', {
|
|
135
|
+
pikkuState(null, 'channel', 'meta', {
|
|
132
136
|
test: {
|
|
133
137
|
name: 'test',
|
|
134
138
|
route: '/test-channel',
|
|
@@ -149,7 +153,7 @@ test('runChannel should return a channel handler if channel matches and no auth
|
|
|
149
153
|
request: new PikkuMockRequest('/test-channel', 'get'),
|
|
150
154
|
response: new PikkuMockResponse(),
|
|
151
155
|
route: '/test-channel',
|
|
152
|
-
|
|
156
|
+
createWireServices: mockCreateWireServices,
|
|
153
157
|
})
|
|
154
158
|
|
|
155
159
|
assert.ok(result, 'Should return a PikkuChannelHandler instance')
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { openChannel } from '../channel-runner.js'
|
|
2
|
-
import {
|
|
3
|
-
import {
|
|
2
|
+
import { createHTTPWire } from '../../http/http-runner.js'
|
|
3
|
+
import { closeWireServices } from '../../../utils.js'
|
|
4
4
|
import { processMessageHandlers } from '../channel-handler.js'
|
|
5
5
|
import {
|
|
6
6
|
CoreChannel,
|
|
@@ -8,11 +8,10 @@ import {
|
|
|
8
8
|
RunChannelParams,
|
|
9
9
|
} from '../channel.types.js'
|
|
10
10
|
import { PikkuLocalChannelHandler } from './local-channel-handler.js'
|
|
11
|
-
import {
|
|
11
|
+
import { PikkuWire, WireServices } from '../../../types/core.types.js'
|
|
12
12
|
import { handleHTTPError } from '../../../handle-error.js'
|
|
13
|
-
import {
|
|
13
|
+
import { PikkuSessionService } from '../../../services/user-session-service.js'
|
|
14
14
|
import { PikkuHTTP } from '../../http/http.types.js'
|
|
15
|
-
import { rpcService } from '../../rpc/rpc-runner.js'
|
|
16
15
|
import { runChannelLifecycleWithMiddleware } from '../channel-common.js'
|
|
17
16
|
|
|
18
17
|
export const runLocalChannel = async ({
|
|
@@ -21,7 +20,7 @@ export const runLocalChannel = async ({
|
|
|
21
20
|
request,
|
|
22
21
|
response,
|
|
23
22
|
route,
|
|
24
|
-
|
|
23
|
+
createWireServices,
|
|
25
24
|
skipUserSession = false,
|
|
26
25
|
respondWith404 = true,
|
|
27
26
|
coerceDataFromSchema = true,
|
|
@@ -30,14 +29,14 @@ export const runLocalChannel = async ({
|
|
|
30
29
|
}: Partial<Pick<CoreChannel<unknown, any>, 'route'>> &
|
|
31
30
|
RunChannelOptions &
|
|
32
31
|
RunChannelParams<unknown>): Promise<PikkuLocalChannelHandler | void> => {
|
|
33
|
-
let
|
|
32
|
+
let wireServices: WireServices<typeof singletonServices> | undefined
|
|
34
33
|
|
|
35
34
|
let channelHandler: PikkuLocalChannelHandler | undefined
|
|
36
|
-
const userSession = new
|
|
35
|
+
const userSession = new PikkuSessionService()
|
|
37
36
|
|
|
38
37
|
let http: PikkuHTTP | undefined
|
|
39
38
|
if (request) {
|
|
40
|
-
http =
|
|
39
|
+
http = createHTTPWire(request, response)
|
|
41
40
|
route = http?.request?.path()
|
|
42
41
|
}
|
|
43
42
|
|
|
@@ -45,7 +44,7 @@ export const runLocalChannel = async ({
|
|
|
45
44
|
try {
|
|
46
45
|
;({ openingData, channelConfig, meta } = await openChannel({
|
|
47
46
|
channelId,
|
|
48
|
-
|
|
47
|
+
createWireServices,
|
|
49
48
|
respondWith404,
|
|
50
49
|
request,
|
|
51
50
|
response,
|
|
@@ -76,28 +75,13 @@ export const runLocalChannel = async ({
|
|
|
76
75
|
openingData
|
|
77
76
|
)
|
|
78
77
|
const channel = channelHandler.getChannel()
|
|
79
|
-
const
|
|
80
|
-
if (createSessionServices) {
|
|
81
|
-
sessionServices = await createSessionServices(
|
|
82
|
-
singletonServices,
|
|
83
|
-
{ http, channel },
|
|
84
|
-
session
|
|
85
|
-
)
|
|
86
|
-
}
|
|
78
|
+
const wire: PikkuWire = { channel, session: userSession }
|
|
87
79
|
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
...singletonServices,
|
|
92
|
-
...sessionServices,
|
|
93
|
-
channel,
|
|
94
|
-
userSession,
|
|
95
|
-
},
|
|
96
|
-
interaction,
|
|
97
|
-
requiresAuth
|
|
98
|
-
)
|
|
80
|
+
if (createWireServices) {
|
|
81
|
+
wireServices = await createWireServices(singletonServices, wire)
|
|
82
|
+
}
|
|
99
83
|
|
|
100
|
-
const
|
|
84
|
+
const services = { ...singletonServices, ...wireServices }
|
|
101
85
|
|
|
102
86
|
channelHandler.registerOnOpen(async () => {
|
|
103
87
|
if (channelConfig.onConnect && meta.connect) {
|
|
@@ -107,7 +91,7 @@ export const runLocalChannel = async ({
|
|
|
107
91
|
meta: meta.connect,
|
|
108
92
|
lifecycleConfig: channelConfig.onConnect,
|
|
109
93
|
lifecycleType: 'connect',
|
|
110
|
-
services
|
|
94
|
+
services,
|
|
111
95
|
channel,
|
|
112
96
|
data: openingData,
|
|
113
97
|
})
|
|
@@ -129,7 +113,7 @@ export const runLocalChannel = async ({
|
|
|
129
113
|
meta: meta.disconnect,
|
|
130
114
|
lifecycleConfig: channelConfig.onDisconnect,
|
|
131
115
|
lifecycleType: 'disconnect',
|
|
132
|
-
services
|
|
116
|
+
services,
|
|
133
117
|
channel,
|
|
134
118
|
})
|
|
135
119
|
} catch (e) {
|
|
@@ -138,15 +122,16 @@ export const runLocalChannel = async ({
|
|
|
138
122
|
}
|
|
139
123
|
}
|
|
140
124
|
|
|
141
|
-
if (
|
|
142
|
-
await
|
|
125
|
+
if (wireServices) {
|
|
126
|
+
await closeWireServices(singletonServices.logger, wireServices)
|
|
143
127
|
}
|
|
144
128
|
})
|
|
145
129
|
|
|
146
130
|
const onMessage = processMessageHandlers(
|
|
147
|
-
|
|
131
|
+
services,
|
|
148
132
|
channelConfig as any,
|
|
149
|
-
channelHandler
|
|
133
|
+
channelHandler,
|
|
134
|
+
userSession
|
|
150
135
|
)
|
|
151
136
|
channelHandler.registerOnMessage(async (data) => {
|
|
152
137
|
try {
|
|
@@ -168,8 +153,8 @@ export const runLocalChannel = async ({
|
|
|
168
153
|
bubbleErrors
|
|
169
154
|
)
|
|
170
155
|
} finally {
|
|
171
|
-
if (
|
|
172
|
-
await
|
|
156
|
+
if (wireServices) {
|
|
157
|
+
await closeWireServices(singletonServices.logger, wireServices)
|
|
173
158
|
}
|
|
174
159
|
}
|
|
175
160
|
}
|
|
@@ -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,5 +1,5 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
1
|
+
import { PikkuWire, WireServices } from '../../../types/core.types.js'
|
|
2
|
+
import { closeWireServices } from '../../../utils.js'
|
|
3
3
|
import { processMessageHandlers } from '../channel-handler.js'
|
|
4
4
|
import { openChannel } from '../channel-runner.js'
|
|
5
5
|
import type {
|
|
@@ -8,15 +8,14 @@ import type {
|
|
|
8
8
|
RunChannelParams,
|
|
9
9
|
PikkuChannelHandlerFactory,
|
|
10
10
|
} from '../channel.types.js'
|
|
11
|
-
import {
|
|
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 {
|
|
14
|
+
import { PikkuSessionService } from '../../../services/user-session-service.js'
|
|
15
15
|
import { pikkuState } from '../../../pikku-state.js'
|
|
16
16
|
import { PikkuFetchHTTPRequest } from '../../http/pikku-fetch-http-request.js'
|
|
17
17
|
import { PikkuHTTP } from '../../http/http.types.js'
|
|
18
18
|
import { runChannelLifecycleWithMiddleware } from '../channel-common.js'
|
|
19
|
-
import { rpcService } from '../../rpc/rpc-runner.js'
|
|
20
19
|
|
|
21
20
|
export interface RunServerlessChannelParams<ChannelData>
|
|
22
21
|
extends RunChannelParams<ChannelData> {
|
|
@@ -36,9 +35,9 @@ const getVariablesForChannel = ({
|
|
|
36
35
|
channelHandlerFactory: PikkuChannelHandlerFactory
|
|
37
36
|
openingData?: unknown
|
|
38
37
|
}) => {
|
|
39
|
-
const channels = pikkuState('channel', 'channels')
|
|
38
|
+
const channels = pikkuState(null, 'channel', 'channels')
|
|
40
39
|
const channelConfig = channels.get(channelName)
|
|
41
|
-
const channelsMeta = pikkuState('channel', 'meta')
|
|
40
|
+
const channelsMeta = pikkuState(null, 'channel', 'meta')
|
|
42
41
|
const meta = channelsMeta[channelName]
|
|
43
42
|
if (!channelConfig) {
|
|
44
43
|
throw new Error(`Channel not found: ${channelName}`)
|
|
@@ -66,7 +65,7 @@ export const runChannelConnect = async ({
|
|
|
66
65
|
request,
|
|
67
66
|
response,
|
|
68
67
|
route,
|
|
69
|
-
|
|
68
|
+
createWireServices,
|
|
70
69
|
channelStore,
|
|
71
70
|
channelHandlerFactory,
|
|
72
71
|
coerceDataFromSchema = true,
|
|
@@ -76,18 +75,18 @@ export const runChannelConnect = async ({
|
|
|
76
75
|
}: Pick<CoreChannel<unknown, any>, 'route'> &
|
|
77
76
|
RunChannelOptions &
|
|
78
77
|
RunServerlessChannelParams<unknown>) => {
|
|
79
|
-
let
|
|
78
|
+
let wireServices: WireServices | undefined
|
|
80
79
|
|
|
81
80
|
let http: PikkuHTTP | undefined
|
|
82
81
|
if (request instanceof Request) {
|
|
83
|
-
http =
|
|
82
|
+
http = createHTTPWire(new PikkuFetchHTTPRequest(request), response)
|
|
84
83
|
}
|
|
85
84
|
|
|
86
|
-
const userSession = new
|
|
85
|
+
const userSession = new PikkuSessionService(channelStore, channelId)
|
|
87
86
|
|
|
88
87
|
const { channelConfig, openingData, meta } = await openChannel({
|
|
89
88
|
channelId,
|
|
90
|
-
|
|
89
|
+
createWireServices,
|
|
91
90
|
request,
|
|
92
91
|
route,
|
|
93
92
|
singletonServices,
|
|
@@ -107,26 +106,17 @@ export const runChannelConnect = async ({
|
|
|
107
106
|
channelHandlerFactory,
|
|
108
107
|
channelName: channelConfig.name,
|
|
109
108
|
})
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
)
|
|
109
|
+
|
|
110
|
+
const wire: PikkuWire = { channel, session: userSession }
|
|
111
|
+
|
|
112
|
+
if (createWireServices) {
|
|
113
|
+
wireServices = await createWireServices(singletonServices, wire)
|
|
116
114
|
}
|
|
117
115
|
|
|
118
|
-
const
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
...singletonServices,
|
|
123
|
-
...sessionServices,
|
|
124
|
-
channel,
|
|
125
|
-
userSession,
|
|
126
|
-
},
|
|
127
|
-
interaction,
|
|
128
|
-
requiresAuth
|
|
129
|
-
)
|
|
116
|
+
const services = {
|
|
117
|
+
...singletonServices,
|
|
118
|
+
...wireServices,
|
|
119
|
+
}
|
|
130
120
|
|
|
131
121
|
if (channelConfig.onConnect && meta.connect) {
|
|
132
122
|
await runChannelLifecycleWithMiddleware({
|
|
@@ -134,7 +124,7 @@ export const runChannelConnect = async ({
|
|
|
134
124
|
meta: meta.connect,
|
|
135
125
|
lifecycleConfig: channelConfig.onConnect,
|
|
136
126
|
lifecycleType: 'connect',
|
|
137
|
-
services
|
|
127
|
+
services,
|
|
138
128
|
channel,
|
|
139
129
|
data: openingData,
|
|
140
130
|
})
|
|
@@ -151,8 +141,8 @@ export const runChannelConnect = async ({
|
|
|
151
141
|
bubbleErrors
|
|
152
142
|
)
|
|
153
143
|
} finally {
|
|
154
|
-
if (
|
|
155
|
-
await
|
|
144
|
+
if (wireServices) {
|
|
145
|
+
await closeWireServices(singletonServices.logger, wireServices)
|
|
156
146
|
}
|
|
157
147
|
}
|
|
158
148
|
}
|
|
@@ -161,7 +151,7 @@ export const runChannelDisconnect = async ({
|
|
|
161
151
|
singletonServices,
|
|
162
152
|
...params
|
|
163
153
|
}: RunServerlessChannelParams<unknown>): Promise<void> => {
|
|
164
|
-
let
|
|
154
|
+
let wireServices: WireServices | undefined
|
|
165
155
|
|
|
166
156
|
// Try to get channel from store. In serverless environments (especially with
|
|
167
157
|
// serverless-offline or worker threads), disconnect can be called multiple times
|
|
@@ -185,30 +175,21 @@ export const runChannelDisconnect = async ({
|
|
|
185
175
|
openingData,
|
|
186
176
|
channelName,
|
|
187
177
|
})
|
|
188
|
-
const userSession = new
|
|
178
|
+
const userSession = new PikkuSessionService(
|
|
189
179
|
params.channelStore,
|
|
190
180
|
params.channelId
|
|
191
181
|
)
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
)
|
|
182
|
+
userSession.setInitial(session)
|
|
183
|
+
const wire: PikkuWire = { channel, session: userSession }
|
|
184
|
+
|
|
185
|
+
if (!wireServices && params.createWireServices) {
|
|
186
|
+
wireServices = await params.createWireServices(singletonServices, wire)
|
|
198
187
|
}
|
|
199
188
|
|
|
200
|
-
const
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
...singletonServices,
|
|
205
|
-
...sessionServices,
|
|
206
|
-
channel,
|
|
207
|
-
userSession,
|
|
208
|
-
},
|
|
209
|
-
interaction,
|
|
210
|
-
requiresAuth
|
|
211
|
-
)
|
|
189
|
+
const services = {
|
|
190
|
+
...singletonServices,
|
|
191
|
+
...wireServices,
|
|
192
|
+
}
|
|
212
193
|
|
|
213
194
|
if (channelConfig.onDisconnect && meta.disconnect) {
|
|
214
195
|
try {
|
|
@@ -217,7 +198,7 @@ export const runChannelDisconnect = async ({
|
|
|
217
198
|
meta: meta.disconnect,
|
|
218
199
|
lifecycleConfig: channelConfig.onDisconnect,
|
|
219
200
|
lifecycleType: 'disconnect',
|
|
220
|
-
services
|
|
201
|
+
services,
|
|
221
202
|
channel,
|
|
222
203
|
})
|
|
223
204
|
} catch (e: any) {
|
|
@@ -227,8 +208,8 @@ export const runChannelDisconnect = async ({
|
|
|
227
208
|
}
|
|
228
209
|
}
|
|
229
210
|
await params.channelStore.removeChannels([channel.channelId])
|
|
230
|
-
if (
|
|
231
|
-
await
|
|
211
|
+
if (wireServices) {
|
|
212
|
+
await closeWireServices(singletonServices.logger, wireServices)
|
|
232
213
|
}
|
|
233
214
|
}
|
|
234
215
|
|
|
@@ -236,7 +217,7 @@ export const runChannelMessage = async (
|
|
|
236
217
|
{ singletonServices, ...params }: RunServerlessChannelParams<unknown>,
|
|
237
218
|
data: unknown
|
|
238
219
|
): Promise<unknown> => {
|
|
239
|
-
let
|
|
220
|
+
let wireServices: WireServices | undefined
|
|
240
221
|
const { openingData, channelName, session } =
|
|
241
222
|
await params.channelStore.getChannelAndSession(params.channelId)
|
|
242
223
|
|
|
@@ -245,36 +226,29 @@ export const runChannelMessage = async (
|
|
|
245
226
|
openingData,
|
|
246
227
|
channelName,
|
|
247
228
|
})
|
|
248
|
-
const userSession = new
|
|
229
|
+
const userSession = new PikkuSessionService(
|
|
249
230
|
params.channelStore,
|
|
250
231
|
params.channelId
|
|
251
232
|
)
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
)
|
|
233
|
+
userSession.setInitial(session)
|
|
234
|
+
const wire: PikkuWire = { channel, session: userSession }
|
|
235
|
+
|
|
236
|
+
if (params.createWireServices) {
|
|
237
|
+
wireServices = await params.createWireServices(singletonServices, wire)
|
|
258
238
|
}
|
|
259
239
|
|
|
260
|
-
const
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
...singletonServices,
|
|
265
|
-
...sessionServices,
|
|
266
|
-
channel,
|
|
267
|
-
userSession,
|
|
268
|
-
},
|
|
269
|
-
interaction
|
|
270
|
-
)
|
|
240
|
+
const services = {
|
|
241
|
+
...singletonServices,
|
|
242
|
+
...wireServices,
|
|
243
|
+
}
|
|
271
244
|
|
|
272
245
|
let response: unknown
|
|
273
246
|
try {
|
|
274
247
|
const onMessage = processMessageHandlers(
|
|
275
|
-
|
|
248
|
+
services,
|
|
276
249
|
channelConfig,
|
|
277
|
-
channelHandler
|
|
250
|
+
channelHandler,
|
|
251
|
+
userSession
|
|
278
252
|
)
|
|
279
253
|
response = await onMessage(data)
|
|
280
254
|
} catch (e: any) {
|
|
@@ -283,8 +257,8 @@ export const runChannelMessage = async (
|
|
|
283
257
|
)
|
|
284
258
|
return { error: e.message || 'Unknown error' }
|
|
285
259
|
} finally {
|
|
286
|
-
if (
|
|
287
|
-
await
|
|
260
|
+
if (wireServices) {
|
|
261
|
+
await closeWireServices(singletonServices.logger, wireServices)
|
|
288
262
|
}
|
|
289
263
|
}
|
|
290
264
|
return response
|
|
@@ -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
|
|
31
|
-
if (!
|
|
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) {
|