@pikku/core 0.6.26 → 0.7.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.
- package/CHANGELOG.md +17 -1
- package/dist/channel/channel-handler.js +16 -38
- package/dist/channel/channel-runner.d.ts +7 -8
- package/dist/channel/channel-runner.js +58 -7
- package/dist/channel/channel.types.d.ts +14 -27
- package/dist/channel/local/local-channel-runner.js +9 -4
- package/dist/channel/log-channels.js +4 -4
- package/dist/channel/serverless/serverless-channel-runner.js +17 -6
- package/dist/function/function-runner.d.ts +12 -0
- package/dist/function/function-runner.js +38 -0
- package/dist/function/functions.types.d.ts +37 -0
- package/dist/function/functions.types.js +1 -0
- package/dist/function/index.d.ts +2 -0
- package/dist/function/index.js +2 -0
- package/dist/handle-error.d.ts +1 -1
- package/dist/http/http-route-runner.d.ts +3 -3
- package/dist/http/http-route-runner.js +60 -35
- package/dist/http/http-routes.types.d.ts +38 -7
- package/dist/http/http-runner.d.ts +90 -0
- package/dist/http/http-runner.js +336 -0
- package/dist/http/http.types.d.ts +177 -0
- package/dist/http/http.types.js +1 -0
- package/dist/http/index.d.ts +2 -2
- package/dist/http/index.js +1 -1
- package/dist/http/log-http-routes.js +7 -5
- package/dist/http/pikku-fetch-http-request.d.ts +1 -1
- package/dist/http/pikku-fetch-http-response.d.ts +4 -2
- package/dist/http/pikku-fetch-http-response.js +64 -12
- package/dist/http/pikku-http-request.d.ts +56 -0
- package/dist/http/pikku-http-request.js +95 -0
- package/dist/http/pikku-http-response.d.ts +13 -0
- package/dist/http/pikku-http-response.js +60 -0
- package/dist/index.d.ts +3 -2
- package/dist/index.js +3 -2
- package/dist/middleware/auth-apikey.js +5 -4
- package/dist/middleware/auth-bearer.js +5 -4
- package/dist/middleware/auth-cookie.js +10 -9
- package/dist/parse-relative-time-offset.d.ts +12 -0
- package/dist/parse-relative-time-offset.js +20 -0
- package/dist/permissions.d.ts +2 -2
- package/dist/pikku-func.d.ts +12 -0
- package/dist/pikku-func.js +39 -0
- package/dist/pikku-function.d.ts +1 -0
- package/dist/pikku-function.js +1 -0
- package/dist/pikku-state.d.ts +10 -5
- package/dist/pikku-state.js +7 -3
- package/dist/scheduler/scheduler-runner.d.ts +1 -1
- package/dist/scheduler/scheduler-runner.js +24 -6
- package/dist/scheduler/scheduler.types.d.ts +3 -2
- package/dist/services/content-service.d.ts +34 -36
- package/dist/services/index.d.ts +1 -0
- package/dist/services/index.js +1 -0
- package/dist/services/local-content.d.ts +4 -2
- package/dist/services/local-content.js +19 -9
- package/dist/types/core.types.d.ts +15 -1
- package/dist/types/functions.types.d.ts +11 -6
- package/dist/utils.d.ts +2 -0
- package/dist/utils.js +15 -0
- package/package.json +2 -1
- package/lcov.info +0 -4310
- package/src/channel/channel-handler.ts +0 -227
- package/src/channel/channel-runner.ts +0 -98
- package/src/channel/channel-store.ts +0 -29
- package/src/channel/channel.types.ts +0 -171
- package/src/channel/eventhub-service.ts +0 -30
- package/src/channel/eventhub-store.ts +0 -8
- package/src/channel/index.ts +0 -8
- package/src/channel/local/index.ts +0 -3
- package/src/channel/local/local-channel-handler.ts +0 -50
- package/src/channel/local/local-channel-runner.test.ts +0 -158
- package/src/channel/local/local-channel-runner.ts +0 -126
- package/src/channel/local/local-eventhub-service.test.ts +0 -95
- package/src/channel/local/local-eventhub-service.ts +0 -95
- package/src/channel/log-channels.ts +0 -20
- package/src/channel/pikku-abstract-channel-handler.test.ts +0 -52
- package/src/channel/pikku-abstract-channel-handler.ts +0 -38
- package/src/channel/serverless/index.ts +0 -1
- package/src/channel/serverless/serverless-channel-runner.ts +0 -209
- package/src/errors/error-handler.ts +0 -61
- package/src/errors/error.test.ts +0 -54
- package/src/errors/errors.ts +0 -339
- package/src/errors/index.ts +0 -2
- package/src/handle-error.ts +0 -73
- package/src/http/http-route-runner.test.ts +0 -117
- package/src/http/http-route-runner.ts +0 -460
- package/src/http/http-routes.types.ts +0 -211
- package/src/http/incomingmessage-to-request-convertor.ts +0 -0
- package/src/http/index.ts +0 -8
- package/src/http/log-http-routes.ts +0 -20
- package/src/http/pikku-fetch-http-request.test.ts +0 -237
- package/src/http/pikku-fetch-http-request.ts +0 -167
- package/src/http/pikku-fetch-http-response.test.ts +0 -82
- package/src/http/pikku-fetch-http-response.ts +0 -84
- package/src/index.ts +0 -19
- package/src/middleware/auth-apikey.ts +0 -65
- package/src/middleware/auth-bearer.ts +0 -65
- package/src/middleware/auth-cookie.ts +0 -98
- package/src/middleware/index.ts +0 -3
- package/src/middleware/timeout.ts +0 -13
- package/src/middleware-runner.ts +0 -43
- package/src/permissions.test.ts +0 -58
- package/src/permissions.ts +0 -45
- package/src/pikku-request.ts +0 -23
- package/src/pikku-response.ts +0 -5
- package/src/pikku-state.ts +0 -71
- package/src/scheduler/index.ts +0 -5
- package/src/scheduler/log-schedulers.ts +0 -20
- package/src/scheduler/scheduler-runner.ts +0 -87
- package/src/scheduler/scheduler.types.ts +0 -29
- package/src/schema.test.ts +0 -57
- package/src/schema.ts +0 -99
- package/src/services/content-service.ts +0 -69
- package/src/services/index.ts +0 -17
- package/src/services/jwt-service.ts +0 -30
- package/src/services/local-content.ts +0 -91
- package/src/services/local-secrets.ts +0 -43
- package/src/services/local-variables.ts +0 -17
- package/src/services/logger-console.ts +0 -97
- package/src/services/logger.ts +0 -57
- package/src/services/schema-service.ts +0 -26
- package/src/services/secret-service.ts +0 -17
- package/src/services/user-session-service.ts +0 -53
- package/src/services/variables-service.ts +0 -6
- package/src/time-utils.test.ts +0 -56
- package/src/time-utils.ts +0 -32
- package/src/types/core.types.ts +0 -159
- package/src/types/functions.types.ts +0 -63
- package/src/utils.ts +0 -21
- package/tsconfig.json +0 -13
- package/tsconfig.tsbuildinfo +0 -1
|
@@ -1,227 +0,0 @@
|
|
|
1
|
-
import {
|
|
2
|
-
CoreSingletonServices,
|
|
3
|
-
CoreServices,
|
|
4
|
-
JSONValue,
|
|
5
|
-
CoreUserSession,
|
|
6
|
-
} from '../types/core.types.js'
|
|
7
|
-
import {
|
|
8
|
-
ChannelMessageMeta,
|
|
9
|
-
CoreAPIChannel,
|
|
10
|
-
PikkuChannelHandler,
|
|
11
|
-
} from './channel.types.js'
|
|
12
|
-
import { verifyPermissions } from '../permissions.js'
|
|
13
|
-
import { pikkuState } from '../pikku-state.js'
|
|
14
|
-
|
|
15
|
-
const getRouteMeta = (
|
|
16
|
-
channelName: string,
|
|
17
|
-
routingProperty?: string,
|
|
18
|
-
routerValue?: string
|
|
19
|
-
): ChannelMessageMeta => {
|
|
20
|
-
const channelsMeta = pikkuState('channel', 'meta')
|
|
21
|
-
const channelMeta = channelsMeta.find(
|
|
22
|
-
(channelMeta) => channelMeta.name === channelName
|
|
23
|
-
)
|
|
24
|
-
if (!channelMeta) {
|
|
25
|
-
throw new Error(`Channel ${channelName} not found`)
|
|
26
|
-
}
|
|
27
|
-
if (!routingProperty) {
|
|
28
|
-
if (!channelMeta.message) {
|
|
29
|
-
throw new Error(`Channel ${channelName} has no default message route`)
|
|
30
|
-
}
|
|
31
|
-
return channelMeta.message
|
|
32
|
-
}
|
|
33
|
-
if (!routerValue) {
|
|
34
|
-
throw new Error(
|
|
35
|
-
`Channel ${channelName} requires a router value for ${routingProperty}`
|
|
36
|
-
)
|
|
37
|
-
}
|
|
38
|
-
const route = channelMeta.messageRoutes[routingProperty]?.[routerValue]
|
|
39
|
-
if (!route) {
|
|
40
|
-
throw new Error(
|
|
41
|
-
`Channel ${channelName} has no route for ${routingProperty}:${routerValue}`
|
|
42
|
-
)
|
|
43
|
-
}
|
|
44
|
-
return route
|
|
45
|
-
}
|
|
46
|
-
|
|
47
|
-
const validateSchema = (
|
|
48
|
-
logger: CoreSingletonServices['logger'],
|
|
49
|
-
data: JSONValue,
|
|
50
|
-
channelRoute: ChannelMessageMeta
|
|
51
|
-
) => {
|
|
52
|
-
const schemaNames = channelRoute.inputs
|
|
53
|
-
if (schemaNames) {
|
|
54
|
-
// TODO
|
|
55
|
-
// loadSchema(schemaNames, logger)
|
|
56
|
-
// validateJson(schemaNames, data)
|
|
57
|
-
}
|
|
58
|
-
}
|
|
59
|
-
|
|
60
|
-
const validateAuth = (
|
|
61
|
-
requiresSession: boolean,
|
|
62
|
-
session: CoreUserSession | undefined,
|
|
63
|
-
onMessage: any
|
|
64
|
-
) => {
|
|
65
|
-
const auth =
|
|
66
|
-
typeof onMessage === 'function'
|
|
67
|
-
? requiresSession
|
|
68
|
-
: onMessage.auth === undefined
|
|
69
|
-
? requiresSession
|
|
70
|
-
: onMessage.auth
|
|
71
|
-
|
|
72
|
-
if (auth && !session) {
|
|
73
|
-
return false
|
|
74
|
-
}
|
|
75
|
-
return true
|
|
76
|
-
}
|
|
77
|
-
|
|
78
|
-
const validatePermissions = async (
|
|
79
|
-
services: CoreServices,
|
|
80
|
-
session: CoreUserSession | undefined,
|
|
81
|
-
onMessage: any,
|
|
82
|
-
data: unknown
|
|
83
|
-
) => {
|
|
84
|
-
const permissions =
|
|
85
|
-
typeof onMessage === 'function' ? {} : onMessage.permissions
|
|
86
|
-
return await verifyPermissions(permissions, services, data, session)
|
|
87
|
-
}
|
|
88
|
-
|
|
89
|
-
const runFunction = async (
|
|
90
|
-
services: CoreServices,
|
|
91
|
-
channelHandler: PikkuChannelHandler,
|
|
92
|
-
channelMessageMeta: ChannelMessageMeta,
|
|
93
|
-
session: CoreUserSession | undefined,
|
|
94
|
-
onMessage: any,
|
|
95
|
-
data: unknown
|
|
96
|
-
) => {
|
|
97
|
-
const func: any = typeof onMessage === 'function' ? onMessage : onMessage.func
|
|
98
|
-
if (channelMessageMeta.type?.toLowerCase().includes('function')) {
|
|
99
|
-
return await func(
|
|
100
|
-
{
|
|
101
|
-
...services,
|
|
102
|
-
channel: channelHandler.getChannel(),
|
|
103
|
-
},
|
|
104
|
-
data,
|
|
105
|
-
session
|
|
106
|
-
)
|
|
107
|
-
} else {
|
|
108
|
-
return await func(services, channelHandler.getChannel(), data)
|
|
109
|
-
}
|
|
110
|
-
}
|
|
111
|
-
|
|
112
|
-
export const processMessageHandlers = (
|
|
113
|
-
services: CoreServices,
|
|
114
|
-
session: CoreUserSession | undefined,
|
|
115
|
-
channelConfig: CoreAPIChannel<any, any>,
|
|
116
|
-
channelHandler: PikkuChannelHandler
|
|
117
|
-
) => {
|
|
118
|
-
const logger = services.logger
|
|
119
|
-
const requiresSession = channelConfig.auth !== false
|
|
120
|
-
|
|
121
|
-
const processMessage = async (
|
|
122
|
-
data: JSONValue,
|
|
123
|
-
onMessage: any,
|
|
124
|
-
session: CoreUserSession | undefined,
|
|
125
|
-
routingProperty?: string,
|
|
126
|
-
routerValue?: string
|
|
127
|
-
): Promise<unknown> => {
|
|
128
|
-
if (!validateAuth(requiresSession, channelHandler, onMessage)) {
|
|
129
|
-
const routeMessage = routingProperty
|
|
130
|
-
? `route '${routingProperty}:${routerValue}'`
|
|
131
|
-
: 'the default message route'
|
|
132
|
-
logger.error(
|
|
133
|
-
`Channel ${channelConfig.name} with id ${channelHandler.getChannel().channelId} requires a session for ${routeMessage}`
|
|
134
|
-
)
|
|
135
|
-
// TODO: Send error message back breaks typescript, but should be implemented somehow
|
|
136
|
-
channelHandler.getChannel().send(`Unauthorized for ${routeMessage}`)
|
|
137
|
-
return
|
|
138
|
-
}
|
|
139
|
-
|
|
140
|
-
const routeMeta = getRouteMeta(
|
|
141
|
-
channelConfig.name,
|
|
142
|
-
routingProperty,
|
|
143
|
-
routerValue
|
|
144
|
-
)
|
|
145
|
-
|
|
146
|
-
if (routeMeta) {
|
|
147
|
-
await validateSchema(services.logger, data, routeMeta)
|
|
148
|
-
}
|
|
149
|
-
|
|
150
|
-
const hasPermission = await validatePermissions(
|
|
151
|
-
services,
|
|
152
|
-
channelHandler,
|
|
153
|
-
onMessage,
|
|
154
|
-
data
|
|
155
|
-
)
|
|
156
|
-
if (!hasPermission) {
|
|
157
|
-
logger.error(
|
|
158
|
-
`Channel ${channelConfig.name} requires permissions for ${routingProperty || 'default message route'}`
|
|
159
|
-
)
|
|
160
|
-
}
|
|
161
|
-
|
|
162
|
-
return await runFunction(
|
|
163
|
-
services,
|
|
164
|
-
channelHandler,
|
|
165
|
-
routeMeta,
|
|
166
|
-
session,
|
|
167
|
-
onMessage,
|
|
168
|
-
data
|
|
169
|
-
)
|
|
170
|
-
}
|
|
171
|
-
|
|
172
|
-
const onMessage = async (rawData): Promise<unknown> => {
|
|
173
|
-
let result: unknown
|
|
174
|
-
let processed = false
|
|
175
|
-
|
|
176
|
-
// Route-specific handling
|
|
177
|
-
if (typeof rawData === 'string' && channelConfig.onMessageRoute) {
|
|
178
|
-
try {
|
|
179
|
-
const messageData = JSON.parse(rawData)
|
|
180
|
-
const entries = Object.entries(channelConfig.onMessageRoute)
|
|
181
|
-
for (const [routingProperty, routes] of entries) {
|
|
182
|
-
const routerValue = messageData[routingProperty]
|
|
183
|
-
if (routerValue && routes[routerValue]) {
|
|
184
|
-
processed = true
|
|
185
|
-
result = await processMessage(
|
|
186
|
-
messageData,
|
|
187
|
-
routes[routerValue],
|
|
188
|
-
session,
|
|
189
|
-
routingProperty,
|
|
190
|
-
routerValue
|
|
191
|
-
)
|
|
192
|
-
break
|
|
193
|
-
}
|
|
194
|
-
}
|
|
195
|
-
|
|
196
|
-
// Default handler if no routes matched but json data was parsed
|
|
197
|
-
if (!processed && channelConfig.onMessage) {
|
|
198
|
-
processed = true
|
|
199
|
-
result = await processMessage(
|
|
200
|
-
messageData,
|
|
201
|
-
channelConfig.onMessage,
|
|
202
|
-
session
|
|
203
|
-
)
|
|
204
|
-
}
|
|
205
|
-
} catch (error) {
|
|
206
|
-
// Most likely a json error.. ignore
|
|
207
|
-
}
|
|
208
|
-
}
|
|
209
|
-
|
|
210
|
-
// Default handler if no routes matched and json data wasn't parsed
|
|
211
|
-
if (!processed && channelConfig.onMessage) {
|
|
212
|
-
processed = true
|
|
213
|
-
result = await processMessage(rawData, channelConfig.onMessage, session)
|
|
214
|
-
}
|
|
215
|
-
|
|
216
|
-
if (!processed) {
|
|
217
|
-
logger.error(
|
|
218
|
-
`No handler found for message in channel ${channelConfig.name} for ${rawData}`
|
|
219
|
-
)
|
|
220
|
-
logger.error(`Channel ${channelConfig}`)
|
|
221
|
-
}
|
|
222
|
-
|
|
223
|
-
return result
|
|
224
|
-
}
|
|
225
|
-
|
|
226
|
-
return onMessage
|
|
227
|
-
}
|
|
@@ -1,98 +0,0 @@
|
|
|
1
|
-
import { NotFoundError } from '../errors/errors.js'
|
|
2
|
-
import { pikkuState } from '../pikku-state.js'
|
|
3
|
-
import { coerceTopLevelDataFromSchema, validateSchema } from '../schema.js'
|
|
4
|
-
import { UserSessionService } from '../services/user-session-service.js'
|
|
5
|
-
import {
|
|
6
|
-
CoreAPIChannel,
|
|
7
|
-
RunChannelOptions,
|
|
8
|
-
RunChannelParams,
|
|
9
|
-
} from './channel.types.js'
|
|
10
|
-
import { match } from 'path-to-regexp'
|
|
11
|
-
|
|
12
|
-
export const addChannel = <
|
|
13
|
-
In,
|
|
14
|
-
Channel extends string,
|
|
15
|
-
ChannelFunction,
|
|
16
|
-
ChannelFunctionSessionless,
|
|
17
|
-
APIPermission,
|
|
18
|
-
>(
|
|
19
|
-
channel: CoreAPIChannel<
|
|
20
|
-
In,
|
|
21
|
-
Channel,
|
|
22
|
-
ChannelFunction,
|
|
23
|
-
ChannelFunctionSessionless,
|
|
24
|
-
APIPermission
|
|
25
|
-
>
|
|
26
|
-
) => {
|
|
27
|
-
pikkuState('channel', 'channels').push(channel as any)
|
|
28
|
-
}
|
|
29
|
-
|
|
30
|
-
export const getMatchingChannelConfig = (request: string) => {
|
|
31
|
-
const channels = pikkuState('channel', 'channels')
|
|
32
|
-
const channelsMeta = pikkuState('channel', 'meta')
|
|
33
|
-
for (const channelConfig of channels) {
|
|
34
|
-
const cleanedRoute = channelConfig.route.replace(/^\/\//, '/')
|
|
35
|
-
const cleanedRequest = request.replace(/^\/\//, '/')
|
|
36
|
-
const matchFunc = match(cleanedRoute, {
|
|
37
|
-
decode: decodeURIComponent,
|
|
38
|
-
})
|
|
39
|
-
const matchedPath = matchFunc(cleanedRequest)
|
|
40
|
-
if (matchedPath) {
|
|
41
|
-
const schemaName = channelsMeta.find(
|
|
42
|
-
(channelMeta) => channelMeta.route === channelConfig.route
|
|
43
|
-
)?.input
|
|
44
|
-
return {
|
|
45
|
-
matchedPath,
|
|
46
|
-
params: matchedPath.params,
|
|
47
|
-
channelConfig,
|
|
48
|
-
schemaName,
|
|
49
|
-
}
|
|
50
|
-
}
|
|
51
|
-
}
|
|
52
|
-
|
|
53
|
-
return null
|
|
54
|
-
}
|
|
55
|
-
|
|
56
|
-
export const openChannel = async ({
|
|
57
|
-
route,
|
|
58
|
-
singletonServices,
|
|
59
|
-
coerceDataFromSchema = true,
|
|
60
|
-
request,
|
|
61
|
-
}: Pick<CoreAPIChannel<unknown, string>, 'route'> &
|
|
62
|
-
RunChannelParams<unknown> & {
|
|
63
|
-
userSession: UserSessionService<any>
|
|
64
|
-
} & RunChannelOptions): Promise<{
|
|
65
|
-
openingData: unknown
|
|
66
|
-
channelConfig: CoreAPIChannel<unknown, any>
|
|
67
|
-
}> => {
|
|
68
|
-
const matchingChannel = getMatchingChannelConfig(route)
|
|
69
|
-
if (!matchingChannel) {
|
|
70
|
-
singletonServices.logger.info(`Channel not found: ${route}`)
|
|
71
|
-
throw new NotFoundError(`Channel not found: ${route}`)
|
|
72
|
-
}
|
|
73
|
-
|
|
74
|
-
const { params, channelConfig, schemaName } = matchingChannel
|
|
75
|
-
|
|
76
|
-
const requiresSession = channelConfig.auth !== false
|
|
77
|
-
request?.setParams(params)
|
|
78
|
-
|
|
79
|
-
singletonServices.logger.info(
|
|
80
|
-
`Matched channel: ${channelConfig.name} | route: ${channelConfig.route} | auth: ${requiresSession.toString()}`
|
|
81
|
-
)
|
|
82
|
-
|
|
83
|
-
let openingData: any | undefined
|
|
84
|
-
if (request) {
|
|
85
|
-
openingData = await request.data()
|
|
86
|
-
if (coerceDataFromSchema && schemaName) {
|
|
87
|
-
coerceTopLevelDataFromSchema(schemaName, openingData)
|
|
88
|
-
}
|
|
89
|
-
await validateSchema(
|
|
90
|
-
singletonServices.logger,
|
|
91
|
-
singletonServices.schema,
|
|
92
|
-
schemaName,
|
|
93
|
-
openingData
|
|
94
|
-
)
|
|
95
|
-
}
|
|
96
|
-
|
|
97
|
-
return { openingData, channelConfig }
|
|
98
|
-
}
|
|
@@ -1,29 +0,0 @@
|
|
|
1
|
-
import { CoreUserSession } from '../types/core.types.js'
|
|
2
|
-
|
|
3
|
-
export type Channel<ChannelType = unknown, OpeningData = unknown> = {
|
|
4
|
-
channelId: string
|
|
5
|
-
channelName: string
|
|
6
|
-
channelObject?: ChannelType
|
|
7
|
-
openingData?: OpeningData
|
|
8
|
-
}
|
|
9
|
-
|
|
10
|
-
export abstract class ChannelStore<
|
|
11
|
-
ChannelType = unknown,
|
|
12
|
-
OpeningData = unknown,
|
|
13
|
-
UserSession extends CoreUserSession = CoreUserSession,
|
|
14
|
-
TypedChannel = Channel<ChannelType, OpeningData>,
|
|
15
|
-
> {
|
|
16
|
-
public abstract addChannel(
|
|
17
|
-
channel: Channel<ChannelType, OpeningData>
|
|
18
|
-
): Promise<void> | void
|
|
19
|
-
public abstract removeChannels(channelId: string[]): Promise<void> | void
|
|
20
|
-
public abstract setUserSession(
|
|
21
|
-
channelId: string,
|
|
22
|
-
userSession: UserSession | null
|
|
23
|
-
): Promise<void> | void
|
|
24
|
-
public abstract getChannelAndSession(
|
|
25
|
-
channelId: string
|
|
26
|
-
):
|
|
27
|
-
| Promise<TypedChannel & { session: UserSession }>
|
|
28
|
-
| (TypedChannel & { session: UserSession })
|
|
29
|
-
}
|
|
@@ -1,171 +0,0 @@
|
|
|
1
|
-
import { PikkuError } from '../errors/error-handler.js'
|
|
2
|
-
import {
|
|
3
|
-
HTTPFunctionMetaInputTypes,
|
|
4
|
-
PikkuHTTPRequest,
|
|
5
|
-
PikkuHTTPResponse,
|
|
6
|
-
} from '../http/http-routes.types.js'
|
|
7
|
-
import {
|
|
8
|
-
APIDocs,
|
|
9
|
-
CoreServices,
|
|
10
|
-
CoreSingletonServices,
|
|
11
|
-
CreateSessionServices,
|
|
12
|
-
PikkuMiddleware,
|
|
13
|
-
} from '../types/core.types.js'
|
|
14
|
-
import { CoreAPIFunction, CoreAPIPermission } from '../types/functions.types.js'
|
|
15
|
-
|
|
16
|
-
export type RunChannelOptions = Partial<{
|
|
17
|
-
skipUserSession: boolean
|
|
18
|
-
respondWith404: boolean
|
|
19
|
-
coerceDataFromSchema: boolean
|
|
20
|
-
logWarningsForStatusCodes: number[]
|
|
21
|
-
bubbleErrors: boolean
|
|
22
|
-
}>
|
|
23
|
-
|
|
24
|
-
export type RunChannelParams<ChannelData> = {
|
|
25
|
-
channelId: string
|
|
26
|
-
singletonServices: CoreSingletonServices
|
|
27
|
-
request?: PikkuHTTPRequest<ChannelData>
|
|
28
|
-
response?: PikkuHTTPResponse
|
|
29
|
-
createSessionServices?: CreateSessionServices
|
|
30
|
-
}
|
|
31
|
-
|
|
32
|
-
export interface ChannelMessageMeta {
|
|
33
|
-
inputs: string[] | null
|
|
34
|
-
outputs: string[] | null
|
|
35
|
-
docs?: APIDocs
|
|
36
|
-
type: null | string
|
|
37
|
-
}
|
|
38
|
-
|
|
39
|
-
export interface ChannelMeta {
|
|
40
|
-
name: string
|
|
41
|
-
route: string
|
|
42
|
-
params?: string[]
|
|
43
|
-
query?: string[]
|
|
44
|
-
input: string | null
|
|
45
|
-
inputTypes?: HTTPFunctionMetaInputTypes
|
|
46
|
-
connect: boolean
|
|
47
|
-
disconnect: boolean
|
|
48
|
-
message: ChannelMessageMeta | null
|
|
49
|
-
messageRoutes: Record<string, Record<string, ChannelMessageMeta>>
|
|
50
|
-
docs?: APIDocs
|
|
51
|
-
tags?: string[]
|
|
52
|
-
}
|
|
53
|
-
|
|
54
|
-
export type ChannelsMeta = ChannelMeta[]
|
|
55
|
-
|
|
56
|
-
export type CoreChannelConnection<
|
|
57
|
-
ChannelData,
|
|
58
|
-
Out = unknown,
|
|
59
|
-
Services extends CoreServices = CoreServices,
|
|
60
|
-
> = (
|
|
61
|
-
services: Services,
|
|
62
|
-
channel: PikkuChannel<ChannelData, Out>
|
|
63
|
-
) => Promise<void>
|
|
64
|
-
|
|
65
|
-
export type CoreChannelDisconnection<
|
|
66
|
-
ChannelData,
|
|
67
|
-
Services extends CoreServices = CoreServices,
|
|
68
|
-
> = (
|
|
69
|
-
services: Services,
|
|
70
|
-
channel: PikkuChannel<ChannelData, never>
|
|
71
|
-
) => Promise<void>
|
|
72
|
-
|
|
73
|
-
/**
|
|
74
|
-
* Represents a core channel function that performs an operation using core services and a user session.
|
|
75
|
-
*
|
|
76
|
-
* @template In - The input type.
|
|
77
|
-
* @template Services - The services type, defaults to `CoreServices`.
|
|
78
|
-
* @template Session - The session type, defaults to `CoreUserSession`.
|
|
79
|
-
*/
|
|
80
|
-
export type CoreChannelMessage<
|
|
81
|
-
In,
|
|
82
|
-
Out,
|
|
83
|
-
ChannelData,
|
|
84
|
-
Services extends CoreServices = CoreServices,
|
|
85
|
-
> = (
|
|
86
|
-
services: Services,
|
|
87
|
-
channel: PikkuChannel<ChannelData, Out>,
|
|
88
|
-
data: In
|
|
89
|
-
) => Promise<void | Out>
|
|
90
|
-
|
|
91
|
-
export type CoreAPIChannelMessage<
|
|
92
|
-
ChannelFunctionMessage = CoreChannelMessage<unknown, unknown, unknown>,
|
|
93
|
-
> = {
|
|
94
|
-
func: ChannelFunctionMessage
|
|
95
|
-
route: string
|
|
96
|
-
}
|
|
97
|
-
|
|
98
|
-
export type CoreAPIChannel<
|
|
99
|
-
ChannelData,
|
|
100
|
-
Channel extends string,
|
|
101
|
-
ChannelFunctionConnection = CoreChannelConnection<ChannelData>,
|
|
102
|
-
ChannelFunctionDisconnection = CoreChannelDisconnection<ChannelData>,
|
|
103
|
-
ChannelFunctionDefaultMessage =
|
|
104
|
-
| CoreChannelMessage<unknown, unknown, unknown>
|
|
105
|
-
| CoreAPIFunction<unknown, unknown>,
|
|
106
|
-
ChannelFunctionMessageRoute =
|
|
107
|
-
| CoreChannelMessage<unknown, unknown, unknown>
|
|
108
|
-
| CoreAPIFunction<unknown, unknown>,
|
|
109
|
-
APIPermission = CoreAPIPermission<ChannelData>,
|
|
110
|
-
> = {
|
|
111
|
-
name: string
|
|
112
|
-
route: Channel
|
|
113
|
-
onConnect?: ChannelFunctionConnection
|
|
114
|
-
onDisconnect?: ChannelFunctionDisconnection
|
|
115
|
-
onMessage?:
|
|
116
|
-
| {
|
|
117
|
-
func: ChannelFunctionDefaultMessage
|
|
118
|
-
permissions?: Record<string, APIPermission[] | APIPermission>
|
|
119
|
-
auth?: boolean
|
|
120
|
-
}
|
|
121
|
-
| ChannelFunctionDefaultMessage
|
|
122
|
-
onMessageRoute?: Record<
|
|
123
|
-
string,
|
|
124
|
-
Record<
|
|
125
|
-
string,
|
|
126
|
-
| ChannelFunctionMessageRoute
|
|
127
|
-
| {
|
|
128
|
-
func: ChannelFunctionMessageRoute
|
|
129
|
-
permissions?: Record<string, APIPermission[] | APIPermission>
|
|
130
|
-
auth?: boolean
|
|
131
|
-
}
|
|
132
|
-
>
|
|
133
|
-
>
|
|
134
|
-
middleware?: PikkuMiddleware[]
|
|
135
|
-
permissions?: Record<string, APIPermission[] | APIPermission>
|
|
136
|
-
auth?: boolean
|
|
137
|
-
docs?: Partial<{
|
|
138
|
-
description: string
|
|
139
|
-
response: string
|
|
140
|
-
errors: Array<typeof PikkuError>
|
|
141
|
-
tags: string[]
|
|
142
|
-
}>
|
|
143
|
-
tags?: string[]
|
|
144
|
-
}
|
|
145
|
-
|
|
146
|
-
export type CoreAPIChannels = CoreAPIChannel<any, string>[]
|
|
147
|
-
|
|
148
|
-
export interface PikkuChannel<OpeningData, Out> {
|
|
149
|
-
// The channel identifier
|
|
150
|
-
channelId: string
|
|
151
|
-
// The data the channel was created with. This could be query parameters
|
|
152
|
-
// or parameters in the url.
|
|
153
|
-
openingData: OpeningData
|
|
154
|
-
// The data to send. This will fail is the stream has been closed.
|
|
155
|
-
send: (data: Out, isBinary?: boolean) => Promise<void> | void
|
|
156
|
-
// This will close the channel.
|
|
157
|
-
close: () => Promise<void> | void
|
|
158
|
-
// The current state of the channel
|
|
159
|
-
state: 'initial' | 'open' | 'closed'
|
|
160
|
-
}
|
|
161
|
-
|
|
162
|
-
export interface PikkuChannelHandler<OpeningData = unknown, Out = unknown> {
|
|
163
|
-
send(message: Out, isBinary?: boolean): Promise<void> | void
|
|
164
|
-
getChannel(): PikkuChannel<OpeningData, Out>
|
|
165
|
-
}
|
|
166
|
-
|
|
167
|
-
export type PikkuChannelHandlerFactory<OpeningData = unknown, Out = unknown> = (
|
|
168
|
-
channelId: string,
|
|
169
|
-
channelName: string,
|
|
170
|
-
openingData: OpeningData
|
|
171
|
-
) => PikkuChannelHandler<OpeningData, Out>
|
|
@@ -1,30 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Interface defining the methods of a EventHub Service.
|
|
3
|
-
*/
|
|
4
|
-
export interface EventHubService<Out = unknown> {
|
|
5
|
-
/**
|
|
6
|
-
* Subscribes a connection to a specific topic.
|
|
7
|
-
* @param topic - The topic to subscribe to.
|
|
8
|
-
* @param channelId - The unique ID of the connection to subscribe.
|
|
9
|
-
*/
|
|
10
|
-
subscribe(topic: string, channelId: string): Promise<void> | void
|
|
11
|
-
|
|
12
|
-
/**
|
|
13
|
-
* Unsubscribes a connection from a specific topic.
|
|
14
|
-
* @param topic - The topic to unsubscribe from.
|
|
15
|
-
* @param channelId - The unique ID of the connection to unsubscribe.
|
|
16
|
-
*/
|
|
17
|
-
unsubscribe(topic: string, channelId: string): Promise<void> | void
|
|
18
|
-
|
|
19
|
-
/**
|
|
20
|
-
* Sends data to all connections subscribed to a topic.
|
|
21
|
-
* @param topic - The topic to send data to.
|
|
22
|
-
* @param data - The data to send to the subscribers.
|
|
23
|
-
*/
|
|
24
|
-
publish(
|
|
25
|
-
topic: string,
|
|
26
|
-
channelId: string | null,
|
|
27
|
-
data: Out,
|
|
28
|
-
isBinary?: boolean
|
|
29
|
-
): Promise<void> | void
|
|
30
|
-
}
|
|
@@ -1,8 +0,0 @@
|
|
|
1
|
-
export abstract class EventHubStore {
|
|
2
|
-
public abstract getChannelIdsForTopic(topic: string): Promise<string[]>
|
|
3
|
-
public abstract subscribe(topic: string, channelId: string): Promise<boolean>
|
|
4
|
-
public abstract unsubscribe(
|
|
5
|
-
topic: string,
|
|
6
|
-
channelId: string
|
|
7
|
-
): Promise<boolean>
|
|
8
|
-
}
|
package/src/channel/index.ts
DELETED
|
@@ -1,8 +0,0 @@
|
|
|
1
|
-
export * from './channel-runner.js'
|
|
2
|
-
export * from './channel.types.js'
|
|
3
|
-
export * from './log-channels.js'
|
|
4
|
-
export * from './pikku-abstract-channel-handler.js'
|
|
5
|
-
export * from './eventhub-service.js'
|
|
6
|
-
export * from './eventhub-service.js'
|
|
7
|
-
export * from './channel-store.js'
|
|
8
|
-
export * from './eventhub-store.js'
|
|
@@ -1,50 +0,0 @@
|
|
|
1
|
-
import { PikkuAbstractChannelHandler } from '../pikku-abstract-channel-handler.js'
|
|
2
|
-
|
|
3
|
-
export class PikkuLocalChannelHandler<
|
|
4
|
-
OpeningData = unknown,
|
|
5
|
-
Out = unknown,
|
|
6
|
-
> extends PikkuAbstractChannelHandler<OpeningData, Out> {
|
|
7
|
-
private onMessageCallback?: (message: unknown) => void
|
|
8
|
-
private openCallBack?: () => void
|
|
9
|
-
private closeCallback?: () => void
|
|
10
|
-
private sendCallback?: (message: Out, isBinary?: boolean) => void
|
|
11
|
-
|
|
12
|
-
public registerOnOpen(callback: () => void): void {
|
|
13
|
-
this.openCallBack = callback
|
|
14
|
-
}
|
|
15
|
-
|
|
16
|
-
public open() {
|
|
17
|
-
this.getChannel().state = 'open'
|
|
18
|
-
if (this.openCallBack) {
|
|
19
|
-
this.openCallBack()
|
|
20
|
-
}
|
|
21
|
-
}
|
|
22
|
-
|
|
23
|
-
public registerOnMessage(callback: (data: any) => Promise<unknown>): void {
|
|
24
|
-
this.onMessageCallback = callback
|
|
25
|
-
}
|
|
26
|
-
|
|
27
|
-
public async message(data: unknown): Promise<unknown> {
|
|
28
|
-
return this.onMessageCallback?.(data)
|
|
29
|
-
}
|
|
30
|
-
|
|
31
|
-
public registerOnClose(callback: () => void): void {
|
|
32
|
-
this.closeCallback = callback
|
|
33
|
-
}
|
|
34
|
-
|
|
35
|
-
public close() {
|
|
36
|
-
super.close()
|
|
37
|
-
this.closeCallback?.()
|
|
38
|
-
}
|
|
39
|
-
|
|
40
|
-
public registerOnSend(send: (message: Out) => void) {
|
|
41
|
-
this.sendCallback = send
|
|
42
|
-
}
|
|
43
|
-
|
|
44
|
-
public send(message: Out, isBinary?: boolean): void {
|
|
45
|
-
if (!this.sendCallback) {
|
|
46
|
-
throw new Error('No send callback registered')
|
|
47
|
-
}
|
|
48
|
-
return this.sendCallback?.(message, isBinary)
|
|
49
|
-
}
|
|
50
|
-
}
|