@pikku/core 0.9.2 → 0.9.4
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 +14 -0
- package/dist/function/function-runner.d.ts +4 -3
- package/dist/function/function-runner.js +20 -7
- package/dist/function/functions.types.d.ts +3 -2
- package/dist/index.d.ts +2 -1
- package/dist/index.js +2 -1
- package/dist/middleware-runner.d.ts +62 -0
- package/dist/middleware-runner.js +91 -2
- package/dist/permissions.d.ts +40 -0
- package/dist/permissions.js +62 -0
- package/dist/pikku-state.d.ts +3 -2
- package/dist/pikku-state.js +2 -0
- package/dist/types/core.types.d.ts +23 -11
- package/dist/wirings/channel/channel-handler.js +1 -0
- package/dist/wirings/channel/local/local-channel-runner.js +23 -15
- package/dist/wirings/channel/serverless/serverless-channel-runner.js +2 -2
- package/dist/wirings/http/http-runner.js +14 -6
- package/dist/wirings/http/http.types.d.ts +1 -0
- package/dist/wirings/mcp/mcp-runner.js +25 -23
- package/dist/wirings/mcp/mcp.types.d.ts +7 -3
- package/dist/wirings/queue/index.d.ts +1 -1
- package/dist/wirings/queue/index.js +1 -1
- package/dist/wirings/queue/queue-runner.d.ts +15 -1
- package/dist/wirings/queue/queue-runner.js +65 -19
- package/dist/wirings/queue/queue.types.d.ts +19 -2
- package/dist/wirings/rpc/rpc-runner.d.ts +5 -2
- package/dist/wirings/rpc/rpc-runner.js +15 -3
- package/dist/wirings/rpc/rpc-types.d.ts +1 -1
- package/dist/wirings/scheduler/scheduler-runner.js +50 -27
- package/dist/wirings/scheduler/scheduler.types.d.ts +17 -2
- package/package.json +1 -1
- package/src/function/function-runner.ts +34 -7
- package/src/function/functions.types.ts +3 -1
- package/src/index.ts +6 -1
- package/src/middleware-runner.ts +110 -2
- package/src/permissions.ts +71 -0
- package/src/pikku-state.ts +5 -2
- package/src/types/core.types.ts +30 -16
- package/src/wirings/channel/channel-handler.ts +1 -0
- package/src/wirings/channel/local/local-channel-runner.ts +33 -16
- package/src/wirings/channel/serverless/serverless-channel-runner.ts +5 -2
- package/src/wirings/http/http-runner.ts +14 -6
- package/src/wirings/http/http.types.ts +1 -0
- package/src/wirings/mcp/mcp-runner.ts +35 -26
- package/src/wirings/mcp/mcp.types.ts +7 -0
- package/src/wirings/queue/index.ts +2 -0
- package/src/wirings/queue/queue-runner.ts +83 -22
- package/src/wirings/queue/queue.types.ts +20 -1
- package/src/wirings/rpc/rpc-runner.ts +16 -4
- package/src/wirings/rpc/rpc-types.ts +1 -1
- package/src/wirings/scheduler/scheduler-runner.ts +74 -35
- package/src/wirings/scheduler/scheduler.types.ts +22 -1
- package/tsconfig.tsbuildinfo +1 -1
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,19 @@
|
|
|
1
1
|
## 0.9.0
|
|
2
2
|
|
|
3
|
+
## 0.9.4
|
|
4
|
+
|
|
5
|
+
### Patch Changes
|
|
6
|
+
|
|
7
|
+
- c18800d: feat: adding queue and scheduledTask to interactions
|
|
8
|
+
|
|
9
|
+
## 0.9.3
|
|
10
|
+
|
|
11
|
+
### Patch Changes
|
|
12
|
+
|
|
13
|
+
- 9691aba: fix: add-functions should support both functions only and objects
|
|
14
|
+
- 2ab0278: refactor: no longer import ALL functions, only the ones used by rpcs
|
|
15
|
+
- 81005ba: feat: creating a smaller meta file for functions to reduce size
|
|
16
|
+
|
|
3
17
|
## 0.9.2
|
|
4
18
|
|
|
5
19
|
### Patch Changes
|
|
@@ -1,12 +1,13 @@
|
|
|
1
1
|
import { CoreServices, CoreUserSession, CorePikkuMiddleware } from '../types/core.types.js';
|
|
2
|
-
import { CorePermissionGroup, CorePikkuFunctionConfig } from './functions.types.js';
|
|
3
|
-
export declare const addFunction: (funcName: string, funcConfig: CorePikkuFunctionConfig<any, any>) => void;
|
|
2
|
+
import { CorePermissionGroup, CorePikkuFunction, CorePikkuFunctionConfig, CorePikkuFunctionSessionless } from './functions.types.js';
|
|
3
|
+
export declare const addFunction: (funcName: string, funcConfig: CorePikkuFunctionConfig<any, any> | CorePikkuFunctionSessionless<any, any> | CorePikkuFunction<any, any>) => void;
|
|
4
4
|
export declare const runPikkuFuncDirectly: <In, Out>(funcName: string, allServices: CoreServices, data: In, session?: CoreUserSession) => Promise<Out>;
|
|
5
|
-
export declare const runPikkuFunc: <In = any, Out = any>(funcName: string, { getAllServices, data, session, permissions: transportPermissions, middleware: transportMiddleware, coerceDataFromSchema, }: {
|
|
5
|
+
export declare const runPikkuFunc: <In = any, Out = any>(funcName: string, { getAllServices, data, session, permissions: transportPermissions, middleware: transportMiddleware, coerceDataFromSchema, tags, }: {
|
|
6
6
|
getAllServices: () => Promise<CoreServices> | CoreServices;
|
|
7
7
|
data: In;
|
|
8
8
|
session?: CoreUserSession;
|
|
9
9
|
permissions?: CorePermissionGroup;
|
|
10
10
|
middleware?: CorePikkuMiddleware[];
|
|
11
11
|
coerceDataFromSchema?: boolean;
|
|
12
|
+
tags?: string[];
|
|
12
13
|
}) => Promise<Out>;
|
|
@@ -1,9 +1,12 @@
|
|
|
1
1
|
import { ForbiddenError } from '../errors/errors.js';
|
|
2
2
|
import { runMiddleware } from '../middleware-runner.js';
|
|
3
|
-
import { verifyPermissions } from '../permissions.js';
|
|
3
|
+
import { verifyPermissions, getPermissionsForTags } from '../permissions.js';
|
|
4
4
|
import { pikkuState } from '../pikku-state.js';
|
|
5
5
|
import { coerceTopLevelDataFromSchema, validateSchema } from '../schema.js';
|
|
6
6
|
export const addFunction = (funcName, funcConfig) => {
|
|
7
|
+
if (funcConfig instanceof Function) {
|
|
8
|
+
funcConfig = { func: funcConfig };
|
|
9
|
+
}
|
|
7
10
|
pikkuState('function', 'functions').set(funcName, funcConfig);
|
|
8
11
|
};
|
|
9
12
|
export const runPikkuFuncDirectly = async (funcName, allServices, data, session) => {
|
|
@@ -13,7 +16,7 @@ export const runPikkuFuncDirectly = async (funcName, allServices, data, session)
|
|
|
13
16
|
}
|
|
14
17
|
return (await funcConfig.func(allServices, data, session));
|
|
15
18
|
};
|
|
16
|
-
export const runPikkuFunc = async (funcName, { getAllServices, data, session, permissions: transportPermissions, middleware: transportMiddleware, coerceDataFromSchema, }) => {
|
|
19
|
+
export const runPikkuFunc = async (funcName, { getAllServices, data, session, permissions: transportPermissions, middleware: transportMiddleware, coerceDataFromSchema, tags = [], }) => {
|
|
17
20
|
const funcConfig = pikkuState('function', 'functions').get(funcName);
|
|
18
21
|
if (!funcConfig) {
|
|
19
22
|
throw new Error(`Function not found: ${funcName}`);
|
|
@@ -26,17 +29,27 @@ export const runPikkuFunc = async (funcName, { getAllServices, data, session, pe
|
|
|
26
29
|
throw new ForbiddenError(`Function ${funcName} requires authentication even though transport does not`);
|
|
27
30
|
}
|
|
28
31
|
const allServices = await getAllServices();
|
|
29
|
-
const
|
|
30
|
-
if (
|
|
32
|
+
const inputSchemaName = funcMeta.inputSchemaName;
|
|
33
|
+
if (inputSchemaName) {
|
|
31
34
|
// Validate request data against the defined schema, if any
|
|
32
|
-
await validateSchema(allServices.logger, allServices.schema,
|
|
35
|
+
await validateSchema(allServices.logger, allServices.schema, inputSchemaName, data);
|
|
33
36
|
// Coerce (top level) query string parameters or date objects if specified by the schema
|
|
34
37
|
if (coerceDataFromSchema) {
|
|
35
|
-
coerceTopLevelDataFromSchema(
|
|
38
|
+
coerceTopLevelDataFromSchema(inputSchemaName, data);
|
|
36
39
|
}
|
|
37
40
|
}
|
|
38
41
|
let permissioned = true;
|
|
39
|
-
|
|
42
|
+
// Check tagged permissions first - ALL must pass
|
|
43
|
+
const taggedPermissions = getPermissionsForTags([
|
|
44
|
+
...tags,
|
|
45
|
+
...(funcConfig.tags || []),
|
|
46
|
+
]);
|
|
47
|
+
if (taggedPermissions.length > 0) {
|
|
48
|
+
const taggedResults = await Promise.all(taggedPermissions.map((permission) => permission(allServices, data, session)));
|
|
49
|
+
permissioned = taggedResults.every((result) => result);
|
|
50
|
+
}
|
|
51
|
+
// Only check function permissions if tagged permissions passed
|
|
52
|
+
if (permissioned && funcConfig.permissions) {
|
|
40
53
|
permissioned = await verifyPermissions(funcConfig.permissions, allServices, data, session);
|
|
41
54
|
}
|
|
42
55
|
if (!permissioned && transportPermissions) {
|
|
@@ -35,12 +35,13 @@ export type CorePikkuFunctionSessionless<In, Out, ChannelData extends unknown |
|
|
|
35
35
|
*/
|
|
36
36
|
export type CorePikkuPermission<In = any, Services extends CoreSingletonServices = CoreServices, Session extends CoreUserSession = CoreUserSession> = (services: Services, data: In, session?: Session) => Promise<boolean>;
|
|
37
37
|
export type CorePermissionGroup<PikkuPermission = CorePikkuPermission<any>> = Record<string, PikkuPermission | PikkuPermission[]> | undefined;
|
|
38
|
-
export type CorePikkuFunctionConfig<PikkuFunction extends CorePikkuFunction<any, any, any, any, any> | CorePikkuFunctionSessionless<any, any, any, any, any>, PikkuPermission extends CorePikkuPermission<any, any, any> = CorePikkuPermission<any>> = {
|
|
38
|
+
export type CorePikkuFunctionConfig<PikkuFunction extends CorePikkuFunction<any, any, any, any, any> | CorePikkuFunctionSessionless<any, any, any, any, any>, PikkuPermission extends CorePikkuPermission<any, any, any> = CorePikkuPermission<any>, PikkuMiddleware extends CorePikkuMiddleware<any> = CorePikkuMiddleware<any>> = {
|
|
39
39
|
name?: string;
|
|
40
40
|
expose?: boolean;
|
|
41
41
|
func: PikkuFunction;
|
|
42
42
|
auth?: boolean;
|
|
43
43
|
permissions?: CorePermissionGroup<PikkuPermission>;
|
|
44
|
-
middleware?:
|
|
44
|
+
middleware?: PikkuMiddleware[];
|
|
45
|
+
tags?: string[];
|
|
45
46
|
docs?: PikkuDocs;
|
|
46
47
|
};
|
package/dist/index.d.ts
CHANGED
|
@@ -21,7 +21,8 @@ export * from './middleware/index.js';
|
|
|
21
21
|
export * from './time-utils.js';
|
|
22
22
|
export * from './utils.js';
|
|
23
23
|
export { pikkuState } from './pikku-state.js';
|
|
24
|
-
export { runMiddleware } from './middleware-runner.js';
|
|
24
|
+
export { runMiddleware, addMiddleware, addMiddlewareForTags, } from './middleware-runner.js';
|
|
25
|
+
export { addPermission, getPermissionsForTags } from './permissions.js';
|
|
25
26
|
export { wireHTTP, addHTTPMiddleware } from './wirings/http/http-runner.js';
|
|
26
27
|
export { wireChannel } from './wirings/channel/channel-runner.js';
|
|
27
28
|
export { wireScheduler } from './wirings/scheduler/scheduler-runner.js';
|
package/dist/index.js
CHANGED
|
@@ -21,7 +21,8 @@ export * from './middleware/index.js';
|
|
|
21
21
|
export * from './time-utils.js';
|
|
22
22
|
export * from './utils.js';
|
|
23
23
|
export { pikkuState } from './pikku-state.js';
|
|
24
|
-
export { runMiddleware } from './middleware-runner.js';
|
|
24
|
+
export { runMiddleware, addMiddleware, addMiddlewareForTags, } from './middleware-runner.js';
|
|
25
|
+
export { addPermission, getPermissionsForTags } from './permissions.js';
|
|
25
26
|
export { wireHTTP, addHTTPMiddleware } from './wirings/http/http-runner.js';
|
|
26
27
|
export { wireChannel } from './wirings/channel/channel-runner.js';
|
|
27
28
|
export { wireScheduler } from './wirings/scheduler/scheduler-runner.js';
|
|
@@ -20,3 +20,65 @@ import { CoreSingletonServices, PikkuInteraction, CorePikkuMiddleware } from './
|
|
|
20
20
|
export declare const runMiddleware: <Middleware extends CorePikkuMiddleware>(services: CoreSingletonServices & {
|
|
21
21
|
userSession?: UserSessionService<any>;
|
|
22
22
|
}, interaction: PikkuInteraction, middlewares: Middleware[], main?: () => Promise<unknown>) => Promise<unknown>;
|
|
23
|
+
/**
|
|
24
|
+
* Adds global middleware for a specific tag.
|
|
25
|
+
*
|
|
26
|
+
* This function allows you to register middleware that will be applied to
|
|
27
|
+
* any wiring (HTTP, Channel, Queue, Scheduler, MCP) that includes the matching tag.
|
|
28
|
+
*
|
|
29
|
+
* @template PikkuMiddleware The middleware type.
|
|
30
|
+
* @param {string} tag - The tag that the middleware should apply to.
|
|
31
|
+
* @param {PikkuMiddleware[]} middleware - The middleware array to apply for the specified tag.
|
|
32
|
+
*
|
|
33
|
+
* @throws {Error} If middleware for the tag already exists.
|
|
34
|
+
*
|
|
35
|
+
* @example
|
|
36
|
+
* ```typescript
|
|
37
|
+
* // Add admin middleware for admin endpoints
|
|
38
|
+
* addMiddleware('admin', [adminMiddleware])
|
|
39
|
+
*
|
|
40
|
+
* // Add authentication middleware for auth endpoints
|
|
41
|
+
* addMiddleware('auth', [authMiddleware])
|
|
42
|
+
*
|
|
43
|
+
* // Add logging middleware for all API endpoints
|
|
44
|
+
* addMiddleware('api', [loggingMiddleware])
|
|
45
|
+
* ```
|
|
46
|
+
*/
|
|
47
|
+
export declare const addMiddleware: <PikkuMiddleware extends CorePikkuMiddleware>(tag: string, middleware: PikkuMiddleware[]) => void;
|
|
48
|
+
/**
|
|
49
|
+
* Retrieves middleware for a given set of tags.
|
|
50
|
+
*
|
|
51
|
+
* This function looks up all middleware registered for any of the provided tags
|
|
52
|
+
* and returns them as a flattened array.
|
|
53
|
+
*
|
|
54
|
+
* @param {string[]} tags - Array of tags to look up middleware for.
|
|
55
|
+
* @returns {CorePikkuMiddleware[]} Array of middleware functions that apply to the given tags.
|
|
56
|
+
*
|
|
57
|
+
* @example
|
|
58
|
+
* ```typescript
|
|
59
|
+
* // Get all middleware for tags 'api' and 'auth'
|
|
60
|
+
* const middleware = getMiddlewareForTags(['api', 'auth'])
|
|
61
|
+
* ```
|
|
62
|
+
*/
|
|
63
|
+
export declare const getMiddlewareForTags: (tags?: string[]) => CorePikkuMiddleware[];
|
|
64
|
+
/**
|
|
65
|
+
* Combines tag-based middleware with wiring-specific middleware.
|
|
66
|
+
*
|
|
67
|
+
* This helper function gets middleware for tags and combines it with any
|
|
68
|
+
* wiring-specific middleware, avoiding the need for manual spreading.
|
|
69
|
+
*
|
|
70
|
+
* @param {CorePikkuMiddleware[] | undefined} wiringMiddleware - Wiring-specific middleware.
|
|
71
|
+
* @param {string[] | undefined} tags - Array of tags to look up middleware for.
|
|
72
|
+
* @returns {CorePikkuMiddleware[]} Combined array of tag-based and wiring-specific middleware.
|
|
73
|
+
*
|
|
74
|
+
* @example
|
|
75
|
+
* ```typescript
|
|
76
|
+
* // Instead of:
|
|
77
|
+
* const taggedMiddleware = getMiddlewareForTags(tags)
|
|
78
|
+
* const combined = [...taggedMiddleware, ...(middleware || [])]
|
|
79
|
+
*
|
|
80
|
+
* // Use:
|
|
81
|
+
* const combined = addMiddlewareForTags(middleware, tags)
|
|
82
|
+
* ```
|
|
83
|
+
*/
|
|
84
|
+
export declare const addMiddlewareForTags: (wiringMiddleware?: CorePikkuMiddleware[], tags?: string[]) => CorePikkuMiddleware[];
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { pikkuState } from './pikku-state.js';
|
|
1
2
|
/**
|
|
2
3
|
* Runs a chain of middleware functions in sequence before executing the main function.
|
|
3
4
|
*
|
|
@@ -16,10 +17,12 @@
|
|
|
16
17
|
* );
|
|
17
18
|
*/
|
|
18
19
|
export const runMiddleware = async (services, interaction, middlewares, main) => {
|
|
20
|
+
// Deduplicate middleware using Set to avoid running the same middleware multiple times
|
|
21
|
+
const uniqueMiddleware = Array.from(new Set(middlewares));
|
|
19
22
|
let result;
|
|
20
23
|
const dispatch = async (index) => {
|
|
21
|
-
if (
|
|
22
|
-
return await
|
|
24
|
+
if (uniqueMiddleware && index < uniqueMiddleware.length) {
|
|
25
|
+
return await uniqueMiddleware[index](services, interaction, () => dispatch(index + 1));
|
|
23
26
|
}
|
|
24
27
|
else if (main) {
|
|
25
28
|
result = await main();
|
|
@@ -28,3 +31,89 @@ export const runMiddleware = async (services, interaction, middlewares, main) =>
|
|
|
28
31
|
await dispatch(0);
|
|
29
32
|
return result;
|
|
30
33
|
};
|
|
34
|
+
/**
|
|
35
|
+
* Adds global middleware for a specific tag.
|
|
36
|
+
*
|
|
37
|
+
* This function allows you to register middleware that will be applied to
|
|
38
|
+
* any wiring (HTTP, Channel, Queue, Scheduler, MCP) that includes the matching tag.
|
|
39
|
+
*
|
|
40
|
+
* @template PikkuMiddleware The middleware type.
|
|
41
|
+
* @param {string} tag - The tag that the middleware should apply to.
|
|
42
|
+
* @param {PikkuMiddleware[]} middleware - The middleware array to apply for the specified tag.
|
|
43
|
+
*
|
|
44
|
+
* @throws {Error} If middleware for the tag already exists.
|
|
45
|
+
*
|
|
46
|
+
* @example
|
|
47
|
+
* ```typescript
|
|
48
|
+
* // Add admin middleware for admin endpoints
|
|
49
|
+
* addMiddleware('admin', [adminMiddleware])
|
|
50
|
+
*
|
|
51
|
+
* // Add authentication middleware for auth endpoints
|
|
52
|
+
* addMiddleware('auth', [authMiddleware])
|
|
53
|
+
*
|
|
54
|
+
* // Add logging middleware for all API endpoints
|
|
55
|
+
* addMiddleware('api', [loggingMiddleware])
|
|
56
|
+
* ```
|
|
57
|
+
*/
|
|
58
|
+
export const addMiddleware = (tag, middleware) => {
|
|
59
|
+
const middlewareStore = pikkuState('misc', 'middleware');
|
|
60
|
+
// Check if tag already exists
|
|
61
|
+
if (middlewareStore[tag]) {
|
|
62
|
+
throw new Error(`Middleware for tag '${tag}' already exists. Use a different tag or remove the existing middleware first.`);
|
|
63
|
+
}
|
|
64
|
+
middlewareStore[tag] = middleware;
|
|
65
|
+
};
|
|
66
|
+
/**
|
|
67
|
+
* Retrieves middleware for a given set of tags.
|
|
68
|
+
*
|
|
69
|
+
* This function looks up all middleware registered for any of the provided tags
|
|
70
|
+
* and returns them as a flattened array.
|
|
71
|
+
*
|
|
72
|
+
* @param {string[]} tags - Array of tags to look up middleware for.
|
|
73
|
+
* @returns {CorePikkuMiddleware[]} Array of middleware functions that apply to the given tags.
|
|
74
|
+
*
|
|
75
|
+
* @example
|
|
76
|
+
* ```typescript
|
|
77
|
+
* // Get all middleware for tags 'api' and 'auth'
|
|
78
|
+
* const middleware = getMiddlewareForTags(['api', 'auth'])
|
|
79
|
+
* ```
|
|
80
|
+
*/
|
|
81
|
+
export const getMiddlewareForTags = (tags) => {
|
|
82
|
+
if (!tags || tags.length === 0) {
|
|
83
|
+
return [];
|
|
84
|
+
}
|
|
85
|
+
const middlewareStore = pikkuState('misc', 'middleware');
|
|
86
|
+
const applicableMiddleware = [];
|
|
87
|
+
// Collect middleware for all matching tags
|
|
88
|
+
for (const tag of tags) {
|
|
89
|
+
const tagMiddleware = middlewareStore[tag];
|
|
90
|
+
if (tagMiddleware) {
|
|
91
|
+
applicableMiddleware.push(...tagMiddleware);
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
return applicableMiddleware;
|
|
95
|
+
};
|
|
96
|
+
/**
|
|
97
|
+
* Combines tag-based middleware with wiring-specific middleware.
|
|
98
|
+
*
|
|
99
|
+
* This helper function gets middleware for tags and combines it with any
|
|
100
|
+
* wiring-specific middleware, avoiding the need for manual spreading.
|
|
101
|
+
*
|
|
102
|
+
* @param {CorePikkuMiddleware[] | undefined} wiringMiddleware - Wiring-specific middleware.
|
|
103
|
+
* @param {string[] | undefined} tags - Array of tags to look up middleware for.
|
|
104
|
+
* @returns {CorePikkuMiddleware[]} Combined array of tag-based and wiring-specific middleware.
|
|
105
|
+
*
|
|
106
|
+
* @example
|
|
107
|
+
* ```typescript
|
|
108
|
+
* // Instead of:
|
|
109
|
+
* const taggedMiddleware = getMiddlewareForTags(tags)
|
|
110
|
+
* const combined = [...taggedMiddleware, ...(middleware || [])]
|
|
111
|
+
*
|
|
112
|
+
* // Use:
|
|
113
|
+
* const combined = addMiddlewareForTags(middleware, tags)
|
|
114
|
+
* ```
|
|
115
|
+
*/
|
|
116
|
+
export const addMiddlewareForTags = (wiringMiddleware, tags) => {
|
|
117
|
+
const taggedMiddleware = getMiddlewareForTags(tags);
|
|
118
|
+
return [...taggedMiddleware, ...(wiringMiddleware || [])];
|
|
119
|
+
};
|
package/dist/permissions.d.ts
CHANGED
|
@@ -8,3 +8,43 @@ import { CorePermissionGroup } from './function/functions.types.js';
|
|
|
8
8
|
* @returns A promise that resolves to void.
|
|
9
9
|
*/
|
|
10
10
|
export declare const verifyPermissions: (permissions: CorePermissionGroup, services: CoreServices, data: any, session?: CoreUserSession) => Promise<boolean>;
|
|
11
|
+
/**
|
|
12
|
+
* Adds global permissions for a specific tag.
|
|
13
|
+
*
|
|
14
|
+
* This function allows you to register permissions that will be applied to
|
|
15
|
+
* any wiring (HTTP, Channel, Queue, Scheduler, MCP) that includes the matching tag.
|
|
16
|
+
*
|
|
17
|
+
* @param {string} tag - The tag that the permissions should apply to.
|
|
18
|
+
* @param {any[]} permissions - The permissions array to apply for the specified tag.
|
|
19
|
+
*
|
|
20
|
+
* @throws {Error} If permissions for the tag already exist.
|
|
21
|
+
*
|
|
22
|
+
* @example
|
|
23
|
+
* ```typescript
|
|
24
|
+
* // Add admin permissions for admin endpoints
|
|
25
|
+
* addPermission('admin', [adminPermission])
|
|
26
|
+
*
|
|
27
|
+
* // Add authentication permissions for auth endpoints
|
|
28
|
+
* addPermission('auth', [authPermission])
|
|
29
|
+
*
|
|
30
|
+
* // Add read permissions for all API endpoints
|
|
31
|
+
* addPermission('api', [readPermission])
|
|
32
|
+
* ```
|
|
33
|
+
*/
|
|
34
|
+
export declare const addPermission: (tag: string, permissions: any[]) => void;
|
|
35
|
+
/**
|
|
36
|
+
* Retrieves permissions for a given set of tags.
|
|
37
|
+
*
|
|
38
|
+
* This function looks up all permissions registered for any of the provided tags
|
|
39
|
+
* and returns them as a flattened array.
|
|
40
|
+
*
|
|
41
|
+
* @param {string[]} tags - Array of tags to look up permissions for.
|
|
42
|
+
* @returns {any[]} Array of permission functions that apply to the given tags.
|
|
43
|
+
*
|
|
44
|
+
* @example
|
|
45
|
+
* ```typescript
|
|
46
|
+
* // Get all permissions for tags 'api' and 'auth'
|
|
47
|
+
* const permissions = getPermissionsForTags(['api', 'auth'])
|
|
48
|
+
* ```
|
|
49
|
+
*/
|
|
50
|
+
export declare const getPermissionsForTags: (tags?: string[]) => any[];
|
package/dist/permissions.js
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { pikkuState } from './pikku-state.js';
|
|
1
2
|
/**
|
|
2
3
|
* 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.
|
|
3
4
|
* @param services - The core services required for permission validation.
|
|
@@ -30,3 +31,64 @@ export const verifyPermissions = async (permissions, services, data, session) =>
|
|
|
30
31
|
}
|
|
31
32
|
return false;
|
|
32
33
|
};
|
|
34
|
+
/**
|
|
35
|
+
* Adds global permissions for a specific tag.
|
|
36
|
+
*
|
|
37
|
+
* This function allows you to register permissions that will be applied to
|
|
38
|
+
* any wiring (HTTP, Channel, Queue, Scheduler, MCP) that includes the matching tag.
|
|
39
|
+
*
|
|
40
|
+
* @param {string} tag - The tag that the permissions should apply to.
|
|
41
|
+
* @param {any[]} permissions - The permissions array to apply for the specified tag.
|
|
42
|
+
*
|
|
43
|
+
* @throws {Error} If permissions for the tag already exist.
|
|
44
|
+
*
|
|
45
|
+
* @example
|
|
46
|
+
* ```typescript
|
|
47
|
+
* // Add admin permissions for admin endpoints
|
|
48
|
+
* addPermission('admin', [adminPermission])
|
|
49
|
+
*
|
|
50
|
+
* // Add authentication permissions for auth endpoints
|
|
51
|
+
* addPermission('auth', [authPermission])
|
|
52
|
+
*
|
|
53
|
+
* // Add read permissions for all API endpoints
|
|
54
|
+
* addPermission('api', [readPermission])
|
|
55
|
+
* ```
|
|
56
|
+
*/
|
|
57
|
+
export const addPermission = (tag, permissions) => {
|
|
58
|
+
const permissionsStore = pikkuState('misc', 'permissions');
|
|
59
|
+
// Check if tag already exists
|
|
60
|
+
if (permissionsStore[tag]) {
|
|
61
|
+
throw new Error(`Permissions for tag '${tag}' already exist. Use a different tag or remove the existing permissions first.`);
|
|
62
|
+
}
|
|
63
|
+
permissionsStore[tag] = permissions;
|
|
64
|
+
};
|
|
65
|
+
/**
|
|
66
|
+
* Retrieves permissions for a given set of tags.
|
|
67
|
+
*
|
|
68
|
+
* This function looks up all permissions registered for any of the provided tags
|
|
69
|
+
* and returns them as a flattened array.
|
|
70
|
+
*
|
|
71
|
+
* @param {string[]} tags - Array of tags to look up permissions for.
|
|
72
|
+
* @returns {any[]} Array of permission functions that apply to the given tags.
|
|
73
|
+
*
|
|
74
|
+
* @example
|
|
75
|
+
* ```typescript
|
|
76
|
+
* // Get all permissions for tags 'api' and 'auth'
|
|
77
|
+
* const permissions = getPermissionsForTags(['api', 'auth'])
|
|
78
|
+
* ```
|
|
79
|
+
*/
|
|
80
|
+
export const getPermissionsForTags = (tags) => {
|
|
81
|
+
if (!tags || tags.length === 0) {
|
|
82
|
+
return [];
|
|
83
|
+
}
|
|
84
|
+
const permissionsStore = pikkuState('misc', 'permissions');
|
|
85
|
+
const applicablePermissions = [];
|
|
86
|
+
// Collect permissions for all matching tags
|
|
87
|
+
for (const tag of new Set(tags)) {
|
|
88
|
+
const tagPermissions = permissionsStore[tag];
|
|
89
|
+
if (tagPermissions) {
|
|
90
|
+
applicablePermissions.push(...tagPermissions);
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
return applicablePermissions;
|
|
94
|
+
};
|
package/dist/pikku-state.d.ts
CHANGED
|
@@ -4,7 +4,6 @@ import { FunctionsMeta, CorePikkuMiddleware } from './types/core.types.js';
|
|
|
4
4
|
import { CoreScheduledTask, ScheduledTasksMeta } from './wirings/scheduler/scheduler.types.js';
|
|
5
5
|
import { ErrorDetails, PikkuError } from './errors/error-handler.js';
|
|
6
6
|
import { CorePikkuFunctionConfig } from './function/functions.types.js';
|
|
7
|
-
import { RPCMeta } from './wirings/rpc/rpc-types.js';
|
|
8
7
|
import { queueWorkersMeta, CoreQueueWorker } from './wirings/queue/queue.types.js';
|
|
9
8
|
import { CoreMCPResource, CoreMCPTool, CoreMCPPrompt, MCPResourceMeta, MCPToolMeta, MCPPromptMeta } from './wirings/mcp/mcp.types.js';
|
|
10
9
|
interface PikkuState {
|
|
@@ -13,7 +12,7 @@ interface PikkuState {
|
|
|
13
12
|
functions: Map<string, CorePikkuFunctionConfig<any, any>>;
|
|
14
13
|
};
|
|
15
14
|
rpc: {
|
|
16
|
-
meta: Record<string,
|
|
15
|
+
meta: Record<string, string>;
|
|
17
16
|
files: Map<string, {
|
|
18
17
|
exportedName: string;
|
|
19
18
|
path: string;
|
|
@@ -50,6 +49,8 @@ interface PikkuState {
|
|
|
50
49
|
misc: {
|
|
51
50
|
errors: Map<PikkuError, ErrorDetails>;
|
|
52
51
|
schemas: Map<string, any>;
|
|
52
|
+
middleware: Record<string, CorePikkuMiddleware[]>;
|
|
53
|
+
permissions: Record<string, any[]>;
|
|
53
54
|
};
|
|
54
55
|
}
|
|
55
56
|
export declare const resetPikkuState: () => void;
|
package/dist/pikku-state.js
CHANGED
|
@@ -36,6 +36,8 @@ export const resetPikkuState = () => {
|
|
|
36
36
|
misc: {
|
|
37
37
|
errors: globalThis.pikkuState?.misc?.errors || new Map(),
|
|
38
38
|
schemas: globalThis.pikkuState?.misc?.schema || new Map(),
|
|
39
|
+
middleware: globalThis.pikkuState?.misc?.middleware || {},
|
|
40
|
+
permissions: globalThis.pikkuState?.misc?.permissions || {},
|
|
39
41
|
},
|
|
40
42
|
};
|
|
41
43
|
};
|
|
@@ -6,6 +6,8 @@ import { UserSessionService } from '../services/user-session-service.js';
|
|
|
6
6
|
import { PikkuChannel } from '../wirings/channel/channel.types.js';
|
|
7
7
|
import { PikkuRPC } from '../wirings/rpc/rpc-types.js';
|
|
8
8
|
import { PikkuMCP } from '../wirings/mcp/mcp.types.js';
|
|
9
|
+
import { PikkuScheduledTask } from '../wirings/scheduler/scheduler.types.js';
|
|
10
|
+
import { PikkuQueue } from '../wirings/queue/queue.types.js';
|
|
9
11
|
export declare enum PikkuWiringTypes {
|
|
10
12
|
http = "http",
|
|
11
13
|
scheduler = "scheduler",
|
|
@@ -18,17 +20,23 @@ export interface FunctionServicesMeta {
|
|
|
18
20
|
optimized: boolean;
|
|
19
21
|
services: string[];
|
|
20
22
|
}
|
|
21
|
-
export type
|
|
23
|
+
export type FunctionRuntimeMeta = {
|
|
22
24
|
pikkuFuncName: string;
|
|
23
|
-
|
|
25
|
+
inputSchemaName: string | null;
|
|
26
|
+
outputSchemaName: string | null;
|
|
27
|
+
expose?: boolean;
|
|
28
|
+
};
|
|
29
|
+
export type FunctionMeta = FunctionRuntimeMeta & Partial<{
|
|
30
|
+
name: string;
|
|
24
31
|
services: FunctionServicesMeta;
|
|
25
|
-
schemaName: string | null;
|
|
26
32
|
inputs: string[] | null;
|
|
27
33
|
outputs: string[] | null;
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
34
|
+
tags: string[];
|
|
35
|
+
docs: PikkuDocs;
|
|
36
|
+
isDirectFunction: boolean;
|
|
31
37
|
}>;
|
|
38
|
+
export type FunctionsRuntimeMeta = Record<string, FunctionRuntimeMeta>;
|
|
39
|
+
export type FunctionsMeta = Record<string, FunctionMeta>;
|
|
32
40
|
export type MakeRequired<T, K extends keyof T> = Omit<T, K> & Required<Pick<T, K>>;
|
|
33
41
|
/**
|
|
34
42
|
* Represents a JSON primitive type which can be a string, number, boolean, null, or undefined.
|
|
@@ -84,11 +92,15 @@ export interface CoreSingletonServices<Config extends CoreConfig = CoreConfig> {
|
|
|
84
92
|
/**
|
|
85
93
|
* Represents different forms of interaction within Pikku and the outside world.
|
|
86
94
|
*/
|
|
87
|
-
export
|
|
88
|
-
http
|
|
89
|
-
mcp
|
|
90
|
-
rpc
|
|
91
|
-
|
|
95
|
+
export type PikkuInteraction = Partial<{
|
|
96
|
+
http: PikkuHTTP;
|
|
97
|
+
mcp: PikkuMCP;
|
|
98
|
+
rpc: PikkuRPC;
|
|
99
|
+
channel: PikkuChannel<unknown, unknown>;
|
|
100
|
+
scheduledTask: PikkuScheduledTask;
|
|
101
|
+
queue: PikkuQueue;
|
|
102
|
+
s: any;
|
|
103
|
+
}>;
|
|
92
104
|
/**
|
|
93
105
|
* A function that can wrap an interaction and be called before or after
|
|
94
106
|
*/
|
|
@@ -4,7 +4,7 @@ import { closeSessionServices } from '../../../utils.js';
|
|
|
4
4
|
import { processMessageHandlers } from '../channel-handler.js';
|
|
5
5
|
import { PikkuLocalChannelHandler } from './local-channel-handler.js';
|
|
6
6
|
import { handleHTTPError } from '../../../handle-error.js';
|
|
7
|
-
import { runMiddleware } from '../../../middleware-runner.js';
|
|
7
|
+
import { addMiddlewareForTags, runMiddleware, } from '../../../middleware-runner.js';
|
|
8
8
|
import { PikkuUserSessionService } from '../../../services/user-session-service.js';
|
|
9
9
|
import { runPikkuFuncDirectly } from '../../../function/function-runner.js';
|
|
10
10
|
import { rpcService } from '../../rpc/rpc-runner.js';
|
|
@@ -17,25 +17,33 @@ export const runLocalChannel = async ({ singletonServices, channelId, request, r
|
|
|
17
17
|
http = createHTTPInteraction(request, response);
|
|
18
18
|
route = http?.request?.path();
|
|
19
19
|
}
|
|
20
|
+
let openingData, channelConfig, meta;
|
|
21
|
+
try {
|
|
22
|
+
;
|
|
23
|
+
({ openingData, channelConfig, meta } = await openChannel({
|
|
24
|
+
channelId,
|
|
25
|
+
createSessionServices,
|
|
26
|
+
respondWith404,
|
|
27
|
+
request,
|
|
28
|
+
response,
|
|
29
|
+
route,
|
|
30
|
+
singletonServices,
|
|
31
|
+
skipUserSession,
|
|
32
|
+
coerceDataFromSchema,
|
|
33
|
+
userSession,
|
|
34
|
+
}));
|
|
35
|
+
}
|
|
36
|
+
catch (e) {
|
|
37
|
+
handleHTTPError(e, http, channelId, singletonServices.logger, logWarningsForStatusCodes, respondWith404, bubbleErrors);
|
|
38
|
+
return;
|
|
39
|
+
}
|
|
20
40
|
const main = async () => {
|
|
21
41
|
try {
|
|
22
|
-
const { openingData, channelConfig, meta } = await openChannel({
|
|
23
|
-
channelId,
|
|
24
|
-
createSessionServices,
|
|
25
|
-
respondWith404,
|
|
26
|
-
request,
|
|
27
|
-
response,
|
|
28
|
-
route,
|
|
29
|
-
singletonServices,
|
|
30
|
-
skipUserSession,
|
|
31
|
-
coerceDataFromSchema,
|
|
32
|
-
userSession,
|
|
33
|
-
});
|
|
34
42
|
channelHandler = new PikkuLocalChannelHandler(channelId, channelConfig.name, openingData);
|
|
35
43
|
const channel = channelHandler.getChannel();
|
|
36
44
|
const session = await userSession.get();
|
|
37
45
|
if (createSessionServices) {
|
|
38
|
-
sessionServices = await createSessionServices(singletonServices, { http }, session);
|
|
46
|
+
sessionServices = await createSessionServices(singletonServices, { http, channel }, session);
|
|
39
47
|
}
|
|
40
48
|
const allServices = rpcService.injectRPCService({
|
|
41
49
|
...singletonServices,
|
|
@@ -69,6 +77,6 @@ export const runLocalChannel = async ({ singletonServices, channelId, request, r
|
|
|
69
77
|
await runMiddleware({
|
|
70
78
|
...singletonServices,
|
|
71
79
|
userSession,
|
|
72
|
-
}, { http },
|
|
80
|
+
}, { http }, addMiddlewareForTags(channelConfig.middleware, channelConfig.tags), main);
|
|
73
81
|
return channelHandler;
|
|
74
82
|
};
|
|
@@ -4,7 +4,7 @@ import { openChannel } from '../channel-runner.js';
|
|
|
4
4
|
import { createHTTPInteraction } from '../../http/http-runner.js';
|
|
5
5
|
import { handleHTTPError } from '../../../handle-error.js';
|
|
6
6
|
import { PikkuUserSessionService } from '../../../services/user-session-service.js';
|
|
7
|
-
import { runMiddleware } from '../../../middleware-runner.js';
|
|
7
|
+
import { addMiddlewareForTags, runMiddleware, } from '../../../middleware-runner.js';
|
|
8
8
|
import { pikkuState } from '../../../pikku-state.js';
|
|
9
9
|
import { PikkuFetchHTTPRequest } from '../../http/pikku-fetch-http-request.js';
|
|
10
10
|
import { runPikkuFuncDirectly } from '../../../function/function-runner.js';
|
|
@@ -76,7 +76,7 @@ export const runChannelConnect = async ({ singletonServices, channelId, channelO
|
|
|
76
76
|
await runMiddleware({
|
|
77
77
|
...singletonServices,
|
|
78
78
|
userSession,
|
|
79
|
-
}, { http }, channelConfig.middleware
|
|
79
|
+
}, { http }, addMiddlewareForTags(channelConfig.middleware, channelConfig.tags), main);
|
|
80
80
|
};
|
|
81
81
|
export const runChannelDisconnect = async ({ singletonServices, ...params }) => {
|
|
82
82
|
let sessionServices;
|