@pikku/core 0.7.7 → 0.8.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 +18 -1
- package/dist/errors/error-handler.d.ts +2 -0
- package/dist/errors/errors.js +2 -0
- package/dist/{channel → events/channel}/channel-handler.d.ts +1 -1
- package/dist/{channel → events/channel}/channel-handler.js +2 -2
- package/dist/{channel → events/channel}/channel-runner.d.ts +1 -1
- package/dist/{channel → events/channel}/channel-runner.js +8 -8
- package/dist/{channel → events/channel}/channel-store.d.ts +1 -1
- package/dist/{channel → events/channel}/channel.types.d.ts +5 -9
- package/dist/{channel → events/channel}/local/local-channel-runner.js +10 -10
- package/dist/{channel → events/channel}/log-channels.d.ts +1 -1
- package/dist/{channel → events/channel}/log-channels.js +1 -1
- package/dist/{channel → events/channel}/serverless/serverless-channel-runner.js +11 -11
- package/dist/{http → events/http}/http-runner.d.ts +2 -2
- package/dist/{http → events/http}/http-runner.js +30 -19
- package/dist/{http → events/http}/http.types.d.ts +4 -3
- package/dist/{http → events/http}/index.d.ts +0 -1
- package/dist/{http → events/http}/index.js +0 -1
- package/dist/{http → events/http}/log-http-routes.d.ts +1 -1
- package/dist/{http → events/http}/log-http-routes.js +1 -1
- package/dist/{http → events/http}/pikku-fetch-http-request.js +1 -1
- package/dist/events/mcp/index.d.ts +2 -0
- package/dist/events/mcp/index.js +2 -0
- package/dist/events/mcp/mcp-runner.d.ts +30 -0
- package/dist/events/mcp/mcp-runner.js +192 -0
- package/dist/events/mcp/mcp.types.d.ts +117 -0
- package/dist/events/queue/index.d.ts +6 -0
- package/dist/events/queue/index.js +6 -0
- package/dist/events/queue/queue-runner.d.ts +24 -0
- package/dist/events/queue/queue-runner.js +83 -0
- package/dist/events/queue/queue.types.d.ts +154 -0
- package/dist/events/queue/register-queue-helper.d.ts +17 -0
- package/dist/events/queue/register-queue-helper.js +34 -0
- package/dist/events/queue/validate-worker-config.d.ts +41 -0
- package/dist/events/queue/validate-worker-config.js +50 -0
- package/dist/events/rpc/index.d.ts +2 -0
- package/dist/events/rpc/rpc-runner.d.ts +28 -0
- package/dist/{rpc → events/rpc}/rpc-runner.js +2 -2
- package/dist/events/scheduler/index.d.ts +3 -0
- package/dist/events/scheduler/index.js +3 -0
- package/dist/{scheduler → events/scheduler}/log-schedulers.d.ts +1 -1
- package/dist/{scheduler → events/scheduler}/log-schedulers.js +1 -1
- package/dist/{scheduler → events/scheduler}/scheduler-runner.d.ts +4 -3
- package/dist/{scheduler → events/scheduler}/scheduler-runner.js +7 -4
- package/dist/{scheduler → events/scheduler}/scheduler.types.d.ts +3 -4
- package/dist/function/function-runner.js +8 -0
- package/dist/function/functions.types.d.ts +3 -2
- package/dist/handle-error.d.ts +2 -2
- package/dist/handle-error.js +1 -1
- package/dist/index.d.ts +10 -6
- package/dist/index.js +10 -6
- package/dist/middleware-runner.d.ts +4 -4
- package/dist/middleware-runner.js +4 -2
- package/dist/pikku-state.d.ts +18 -4
- package/dist/pikku-state.js +12 -0
- package/dist/services/user-session-service.d.ts +1 -1
- package/dist/types/core.types.d.ts +22 -4
- package/dist/types/core.types.js +9 -1
- package/lcov.info +3557 -3476
- package/package.json +8 -7
- package/src/errors/error-handler.ts +2 -1
- package/src/errors/errors.ts +2 -0
- package/src/{channel → events/channel}/channel-handler.ts +3 -3
- package/src/{channel → events/channel}/channel-runner.ts +9 -9
- package/src/{channel → events/channel}/channel-store.ts +1 -1
- package/src/{channel → events/channel}/channel.types.ts +5 -9
- package/src/{channel → events/channel}/local/local-channel-runner.test.ts +1 -1
- package/src/{channel → events/channel}/local/local-channel-runner.ts +11 -11
- package/src/{channel → events/channel}/local/local-eventhub-service.test.ts +1 -1
- package/src/{channel → events/channel}/log-channels.ts +2 -2
- package/src/{channel → events/channel}/serverless/serverless-channel-runner.ts +12 -12
- package/src/{http → events/http}/http-runner.test.ts +4 -4
- package/src/{http → events/http}/http-runner.ts +39 -20
- package/src/{http → events/http}/http.types.ts +4 -3
- package/src/{http → events/http}/index.ts +0 -1
- package/src/{http → events/http}/log-http-routes.ts +2 -2
- package/src/{http → events/http}/pikku-fetch-http-request.ts +1 -1
- package/src/events/mcp/index.ts +2 -0
- package/src/events/mcp/mcp-runner.ts +321 -0
- package/src/events/mcp/mcp.types.ts +150 -0
- package/src/events/queue/index.ts +18 -0
- package/src/events/queue/queue-runner.ts +125 -0
- package/src/events/queue/queue.types.ts +176 -0
- package/src/events/queue/register-queue-helper.ts +62 -0
- package/src/events/queue/validate-worker-config.ts +113 -0
- package/src/events/rpc/index.ts +2 -0
- package/src/{rpc → events/rpc}/rpc-runner.ts +3 -3
- package/src/events/scheduler/index.ts +9 -0
- package/src/{scheduler → events/scheduler}/log-schedulers.ts +2 -2
- package/src/{scheduler → events/scheduler}/scheduler-runner.ts +11 -10
- package/src/{scheduler → events/scheduler}/scheduler.types.ts +2 -4
- package/src/function/function-runner.ts +19 -1
- package/src/function/functions.types.ts +3 -1
- package/src/handle-error.ts +2 -2
- package/src/index.ts +23 -6
- package/src/middleware-runner.ts +14 -7
- package/src/pikku-state.ts +40 -4
- package/src/services/user-session-service.ts +1 -1
- package/src/types/core.types.ts +31 -4
- package/tsconfig.json +3 -2
- package/tsconfig.tsbuildinfo +1 -1
- package/dist/rpc/index.d.ts +0 -2
- package/dist/rpc/rpc-runner.d.ts +0 -27
- package/dist/scheduler/index.d.ts +0 -3
- package/dist/scheduler/index.js +0 -3
- package/src/http/incomingmessage-to-request-convertor.ts +0 -0
- package/src/rpc/index.ts +0 -2
- package/src/scheduler/index.ts +0 -5
- /package/dist/{channel → events/channel}/channel-store.js +0 -0
- /package/dist/{channel → events/channel}/channel.types.js +0 -0
- /package/dist/{channel → events/channel}/eventhub-service.d.ts +0 -0
- /package/dist/{channel → events/channel}/eventhub-service.js +0 -0
- /package/dist/{channel → events/channel}/eventhub-store.d.ts +0 -0
- /package/dist/{channel → events/channel}/eventhub-store.js +0 -0
- /package/dist/{channel → events/channel}/index.d.ts +0 -0
- /package/dist/{channel → events/channel}/index.js +0 -0
- /package/dist/{channel → events/channel}/local/index.d.ts +0 -0
- /package/dist/{channel → events/channel}/local/index.js +0 -0
- /package/dist/{channel → events/channel}/local/local-channel-handler.d.ts +0 -0
- /package/dist/{channel → events/channel}/local/local-channel-handler.js +0 -0
- /package/dist/{channel → events/channel}/local/local-channel-runner.d.ts +0 -0
- /package/dist/{channel → events/channel}/local/local-eventhub-service.d.ts +0 -0
- /package/dist/{channel → events/channel}/local/local-eventhub-service.js +0 -0
- /package/dist/{channel → events/channel}/pikku-abstract-channel-handler.d.ts +0 -0
- /package/dist/{channel → events/channel}/pikku-abstract-channel-handler.js +0 -0
- /package/dist/{channel → events/channel}/serverless/index.d.ts +0 -0
- /package/dist/{channel → events/channel}/serverless/index.js +0 -0
- /package/dist/{channel → events/channel}/serverless/serverless-channel-runner.d.ts +0 -0
- /package/dist/{http → events/http}/http.types.js +0 -0
- /package/dist/{http → events/http}/pikku-fetch-http-request.d.ts +0 -0
- /package/dist/{http → events/http}/pikku-fetch-http-response.d.ts +0 -0
- /package/dist/{http → events/http}/pikku-fetch-http-response.js +0 -0
- /package/dist/{http/incomingmessage-to-request-convertor.d.ts → events/mcp/mcp.types.js} +0 -0
- /package/dist/{http/incomingmessage-to-request-convertor.js → events/queue/queue.types.js} +0 -0
- /package/dist/{rpc → events/rpc}/index.js +0 -0
- /package/dist/{rpc → events/rpc}/rpc-types.d.ts +0 -0
- /package/dist/{rpc → events/rpc}/rpc-types.js +0 -0
- /package/dist/{scheduler → events/scheduler}/scheduler.types.js +0 -0
- /package/src/{channel → events/channel}/eventhub-service.ts +0 -0
- /package/src/{channel → events/channel}/eventhub-store.ts +0 -0
- /package/src/{channel → events/channel}/index.ts +0 -0
- /package/src/{channel → events/channel}/local/index.ts +0 -0
- /package/src/{channel → events/channel}/local/local-channel-handler.ts +0 -0
- /package/src/{channel → events/channel}/local/local-eventhub-service.ts +0 -0
- /package/src/{channel → events/channel}/pikku-abstract-channel-handler.test.ts +0 -0
- /package/src/{channel → events/channel}/pikku-abstract-channel-handler.ts +0 -0
- /package/src/{channel → events/channel}/serverless/index.ts +0 -0
- /package/src/{http → events/http}/pikku-fetch-http-request.test.ts +0 -0
- /package/src/{http → events/http}/pikku-fetch-http-response.test.ts +0 -0
- /package/src/{http → events/http}/pikku-fetch-http-response.ts +0 -0
- /package/src/{rpc → events/rpc}/rpc-types.ts +0 -0
package/CHANGELOG.md
CHANGED
|
@@ -1,4 +1,21 @@
|
|
|
1
|
-
## 0.
|
|
1
|
+
## 0.8.0
|
|
2
|
+
|
|
3
|
+
### Major Features
|
|
4
|
+
|
|
5
|
+
- **Model Context Protocol (MCP) Support**: Added MCP implementation with resources, tools, and prompts
|
|
6
|
+
- **Queue System**: Added queue support with runners and workers
|
|
7
|
+
- **RPC (Remote Procedure Calls)**: Added typed RPC calls inside functions with local and remote support
|
|
8
|
+
- **Middleware Runner**: Added middleware runner to functions for enhanced request processing
|
|
9
|
+
- **Multiple Bootstrap Files**: Added support for different transport bootstrap files
|
|
10
|
+
- **Middleware Runner**: Run middleware on any function
|
|
11
|
+
|
|
12
|
+
## 0.7.8
|
|
13
|
+
|
|
14
|
+
### Patch Changes
|
|
15
|
+
|
|
16
|
+
- 8b4f52e: fix: nextjs compatability with 0.7
|
|
17
|
+
- 8b4f52e: refactor: moving schemas in channels to functions
|
|
18
|
+
- 1d70184: feat: adding multiple bootstrap files for different transports
|
|
2
19
|
|
|
3
20
|
## 0.7.7
|
|
4
21
|
|
|
@@ -15,6 +15,7 @@ export declare class PikkuError extends Error {
|
|
|
15
15
|
export interface ErrorDetails {
|
|
16
16
|
status: number;
|
|
17
17
|
message: string;
|
|
18
|
+
mcpCode?: number;
|
|
18
19
|
}
|
|
19
20
|
/**
|
|
20
21
|
* Adds an error to the API errors map.
|
|
@@ -35,4 +36,5 @@ export declare const addErrors: (errors: Array<[error: any, details: ErrorDetail
|
|
|
35
36
|
export declare const getErrorResponse: (error: Error) => {
|
|
36
37
|
status: number;
|
|
37
38
|
message: string;
|
|
39
|
+
mcpCode?: number;
|
|
38
40
|
} | undefined;
|
package/dist/errors/errors.js
CHANGED
|
@@ -10,6 +10,7 @@ export class BadRequestError extends PikkuError {
|
|
|
10
10
|
}
|
|
11
11
|
addError(BadRequestError, {
|
|
12
12
|
status: 400,
|
|
13
|
+
mcpCode: -32600,
|
|
13
14
|
message: 'The server cannot or will not process the request due to client error (e.g., malformed request syntax).',
|
|
14
15
|
});
|
|
15
16
|
/**
|
|
@@ -81,6 +82,7 @@ export class NotFoundError extends PikkuError {
|
|
|
81
82
|
*/
|
|
82
83
|
addError(NotFoundError, {
|
|
83
84
|
status: 404,
|
|
85
|
+
mcpCode: -32601,
|
|
84
86
|
message: 'The server cannot find the requested resource.',
|
|
85
87
|
});
|
|
86
88
|
/**
|
|
@@ -1,3 +1,3 @@
|
|
|
1
|
-
import { CoreServices, CoreUserSession } from '
|
|
1
|
+
import { CoreServices, CoreUserSession } from '../../types/core.types.js';
|
|
2
2
|
import { CoreAPIChannel, PikkuChannelHandler } from './channel.types.js';
|
|
3
3
|
export declare const processMessageHandlers: (services: CoreServices, session: CoreUserSession | undefined, channelConfig: CoreAPIChannel<any, any>, channelHandler: PikkuChannelHandler) => (rawData: any) => Promise<unknown>;
|
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import { pikkuState } from '
|
|
2
|
-
import { runPikkuFunc } from '
|
|
1
|
+
import { pikkuState } from '../../pikku-state.js';
|
|
2
|
+
import { runPikkuFunc } from '../../function/function-runner.js';
|
|
3
3
|
const getRouteMeta = (channelName, routingProperty, routerValue) => {
|
|
4
4
|
const channelMeta = pikkuState('channel', 'meta')[channelName];
|
|
5
5
|
if (!channelMeta) {
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { UserSessionService } from '
|
|
1
|
+
import { UserSessionService } from '../../services/user-session-service.js';
|
|
2
2
|
import { ChannelMeta, CoreAPIChannel, RunChannelOptions, RunChannelParams } from './channel.types.js';
|
|
3
3
|
/**
|
|
4
4
|
* Adds a channel and registers all functions referenced in it using the
|
|
@@ -1,7 +1,7 @@
|
|
|
1
|
-
import { NotFoundError } from '
|
|
2
|
-
import { addFunction } from '
|
|
3
|
-
import { pikkuState } from '
|
|
4
|
-
import { coerceTopLevelDataFromSchema, validateSchema } from '
|
|
1
|
+
import { NotFoundError } from '../../errors/errors.js';
|
|
2
|
+
import { addFunction } from '../../function/function-runner.js';
|
|
3
|
+
import { pikkuState } from '../../pikku-state.js';
|
|
4
|
+
import { coerceTopLevelDataFromSchema, validateSchema } from '../../schema.js';
|
|
5
5
|
import { match } from 'path-to-regexp';
|
|
6
6
|
/**
|
|
7
7
|
* Adds a channel and registers all functions referenced in it using the
|
|
@@ -16,14 +16,14 @@ export const addChannel = (channel) => {
|
|
|
16
16
|
}
|
|
17
17
|
pikkuState('channel', 'channels').set(channel.name, channel);
|
|
18
18
|
// Register onConnect function if provided
|
|
19
|
-
if (channel.onConnect && channelMeta.
|
|
20
|
-
addFunction(channelMeta.
|
|
19
|
+
if (channel.onConnect && channelMeta.connect) {
|
|
20
|
+
addFunction(channelMeta.connect.pikkuFuncName, {
|
|
21
21
|
func: channel.onConnect,
|
|
22
22
|
});
|
|
23
23
|
}
|
|
24
24
|
// Register onDisconnect function if provided
|
|
25
|
-
if (channel.onDisconnect && channelMeta.
|
|
26
|
-
addFunction(channelMeta.
|
|
25
|
+
if (channel.onDisconnect && channelMeta.disconnect) {
|
|
26
|
+
addFunction(channelMeta.disconnect.pikkuFuncName, {
|
|
27
27
|
func: channel.onDisconnect,
|
|
28
28
|
});
|
|
29
29
|
}
|
|
@@ -1,7 +1,7 @@
|
|
|
1
|
-
import { PikkuError } from '
|
|
1
|
+
import { PikkuError } from '../../errors/error-handler.js';
|
|
2
2
|
import { HTTPFunctionMetaInputTypes, PikkuHTTPRequest, PikkuHTTPResponse } from '../http/http.types.js';
|
|
3
|
-
import { APIDocs, CoreSingletonServices, CreateSessionServices, PikkuMiddleware } from '
|
|
4
|
-
import { CoreAPIFunction, CoreAPIFunctionSessionless, CoreAPIPermission } from '
|
|
3
|
+
import { APIDocs, CoreSingletonServices, CreateSessionServices, PikkuMiddleware } from '../../types/core.types.js';
|
|
4
|
+
import { CoreAPIFunction, CoreAPIFunctionSessionless, CoreAPIPermission } from '../../function/functions.types.js';
|
|
5
5
|
export type RunChannelOptions = Partial<{
|
|
6
6
|
skipUserSession: boolean;
|
|
7
7
|
respondWith404: boolean;
|
|
@@ -18,8 +18,6 @@ export type RunChannelParams<ChannelData> = {
|
|
|
18
18
|
};
|
|
19
19
|
export interface ChannelMessageMeta {
|
|
20
20
|
pikkuFuncName: string;
|
|
21
|
-
inputs: string[] | null;
|
|
22
|
-
outputs: string[] | null;
|
|
23
21
|
docs?: APIDocs;
|
|
24
22
|
}
|
|
25
23
|
export interface ChannelMeta {
|
|
@@ -29,10 +27,8 @@ export interface ChannelMeta {
|
|
|
29
27
|
query?: string[];
|
|
30
28
|
input: string | null;
|
|
31
29
|
inputTypes?: HTTPFunctionMetaInputTypes;
|
|
32
|
-
connect:
|
|
33
|
-
|
|
34
|
-
disconnect: boolean;
|
|
35
|
-
disconnectPikkuFuncName: string | null;
|
|
30
|
+
connect: ChannelMessageMeta | null;
|
|
31
|
+
disconnect: ChannelMessageMeta | null;
|
|
36
32
|
message: ChannelMessageMeta | null;
|
|
37
33
|
messageRoutes: Record<string, Record<string, ChannelMessageMeta>>;
|
|
38
34
|
docs?: APIDocs;
|
|
@@ -1,12 +1,12 @@
|
|
|
1
1
|
import { openChannel } from '../channel-runner.js';
|
|
2
2
|
import { createHTTPInteraction } from '../../http/http-runner.js';
|
|
3
|
-
import { closeSessionServices } from '
|
|
3
|
+
import { closeSessionServices } from '../../../utils.js';
|
|
4
4
|
import { processMessageHandlers } from '../channel-handler.js';
|
|
5
5
|
import { PikkuLocalChannelHandler } from './local-channel-handler.js';
|
|
6
|
-
import {
|
|
7
|
-
import { runMiddleware } from '
|
|
8
|
-
import { PikkuUserSessionService } from '
|
|
9
|
-
import { runPikkuFuncDirectly } from '
|
|
6
|
+
import { handleHTTPError } from '../../../handle-error.js';
|
|
7
|
+
import { runMiddleware } from '../../../middleware-runner.js';
|
|
8
|
+
import { PikkuUserSessionService } from '../../../services/user-session-service.js';
|
|
9
|
+
import { runPikkuFuncDirectly } from '../../../function/function-runner.js';
|
|
10
10
|
import { rpcService } from '../../rpc/rpc-runner.js';
|
|
11
11
|
export const runLocalChannel = async ({ singletonServices, channelId, request, response, route, createSessionServices, skipUserSession = false, respondWith404 = true, coerceDataFromSchema = true, logWarningsForStatusCodes = [], bubbleErrors = false, }) => {
|
|
12
12
|
let sessionServices;
|
|
@@ -43,13 +43,13 @@ export const runLocalChannel = async ({ singletonServices, channelId, request, r
|
|
|
43
43
|
userSession: userSession,
|
|
44
44
|
});
|
|
45
45
|
channelHandler.registerOnOpen(() => {
|
|
46
|
-
if (channelConfig.onConnect && meta.
|
|
47
|
-
runPikkuFuncDirectly(meta.
|
|
46
|
+
if (channelConfig.onConnect && meta.connect) {
|
|
47
|
+
runPikkuFuncDirectly(meta.connect.pikkuFuncName, { ...allServices, channel }, openingData);
|
|
48
48
|
}
|
|
49
49
|
});
|
|
50
50
|
channelHandler.registerOnClose(async () => {
|
|
51
|
-
if (channelConfig.onDisconnect && meta.
|
|
52
|
-
runPikkuFuncDirectly(meta.
|
|
51
|
+
if (channelConfig.onDisconnect && meta.disconnect) {
|
|
52
|
+
runPikkuFuncDirectly(meta.disconnect.pikkuFuncName, { ...allServices, channel }, openingData);
|
|
53
53
|
}
|
|
54
54
|
if (sessionServices) {
|
|
55
55
|
await closeSessionServices(singletonServices.logger, sessionServices);
|
|
@@ -58,7 +58,7 @@ export const runLocalChannel = async ({ singletonServices, channelId, request, r
|
|
|
58
58
|
channelHandler.registerOnMessage(processMessageHandlers(allServices, session, channelConfig, channelHandler));
|
|
59
59
|
}
|
|
60
60
|
catch (e) {
|
|
61
|
-
|
|
61
|
+
handleHTTPError(e, http, channelId, singletonServices.logger, logWarningsForStatusCodes, respondWith404, bubbleErrors);
|
|
62
62
|
}
|
|
63
63
|
finally {
|
|
64
64
|
if (sessionServices) {
|
|
@@ -1,13 +1,13 @@
|
|
|
1
|
-
import { closeSessionServices } from '
|
|
1
|
+
import { closeSessionServices } from '../../../utils.js';
|
|
2
2
|
import { processMessageHandlers } from '../channel-handler.js';
|
|
3
3
|
import { openChannel } from '../channel-runner.js';
|
|
4
4
|
import { createHTTPInteraction } from '../../http/http-runner.js';
|
|
5
|
-
import {
|
|
6
|
-
import { PikkuUserSessionService } from '
|
|
7
|
-
import { runMiddleware } from '
|
|
8
|
-
import { pikkuState } from '
|
|
5
|
+
import { handleHTTPError } from '../../../handle-error.js';
|
|
6
|
+
import { PikkuUserSessionService } from '../../../services/user-session-service.js';
|
|
7
|
+
import { runMiddleware } from '../../../middleware-runner.js';
|
|
8
|
+
import { pikkuState } from '../../../pikku-state.js';
|
|
9
9
|
import { PikkuFetchHTTPRequest } from '../../http/pikku-fetch-http-request.js';
|
|
10
|
-
import { runPikkuFuncDirectly } from '
|
|
10
|
+
import { runPikkuFuncDirectly } from '../../../function/function-runner.js';
|
|
11
11
|
const getVariablesForChannel = ({ channelId, channelName, channelHandlerFactory, openingData, }) => {
|
|
12
12
|
const channels = pikkuState('channel', 'channels');
|
|
13
13
|
const channelConfig = channels[channelName];
|
|
@@ -59,13 +59,13 @@ export const runChannelConnect = async ({ singletonServices, channelId, channelO
|
|
|
59
59
|
if (createSessionServices) {
|
|
60
60
|
sessionServices = await createSessionServices(singletonServices, { http }, await userSession.get());
|
|
61
61
|
}
|
|
62
|
-
if (channelConfig.onConnect && meta.
|
|
63
|
-
await runPikkuFuncDirectly(meta.
|
|
62
|
+
if (channelConfig.onConnect && meta.connect) {
|
|
63
|
+
await runPikkuFuncDirectly(meta.connect.pikkuFuncName, { ...singletonServices, ...sessionServices, channel }, openingData);
|
|
64
64
|
}
|
|
65
65
|
http?.response?.status(101);
|
|
66
66
|
}
|
|
67
67
|
catch (e) {
|
|
68
|
-
|
|
68
|
+
handleHTTPError(e, http, channelId, singletonServices.logger, logWarningsForStatusCodes, respondWith404, bubbleErrors);
|
|
69
69
|
}
|
|
70
70
|
finally {
|
|
71
71
|
if (sessionServices) {
|
|
@@ -89,8 +89,8 @@ export const runChannelDisconnect = async ({ singletonServices, ...params }) =>
|
|
|
89
89
|
if (!sessionServices && params.createSessionServices) {
|
|
90
90
|
sessionServices = await params.createSessionServices(singletonServices, {}, session);
|
|
91
91
|
}
|
|
92
|
-
if (channelConfig.onDisconnect && meta.
|
|
93
|
-
await runPikkuFuncDirectly(meta.
|
|
92
|
+
if (channelConfig.onDisconnect && meta.disconnect) {
|
|
93
|
+
await runPikkuFuncDirectly(meta.disconnect.pikkuFuncName, { ...singletonServices, ...sessionServices, channel }, undefined);
|
|
94
94
|
}
|
|
95
95
|
await params.channelStore.removeChannels([channel.channelId]);
|
|
96
96
|
if (sessionServices) {
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { CoreHTTPFunctionRoute, RunRouteOptions, RunRouteParams, PikkuHTTP, PikkuHTTPRequest, PikkuHTTPResponse } from './http.types.js';
|
|
2
|
-
import { PikkuMiddleware } from '
|
|
2
|
+
import { PikkuMiddleware } from '../../types/core.types.js';
|
|
3
3
|
import { PikkuFetchHTTPResponse } from './pikku-fetch-http-response.js';
|
|
4
4
|
/**
|
|
5
5
|
* Registers middleware either globally or for a specific route.
|
|
@@ -87,4 +87,4 @@ export declare const pikkuFetch: <In, Out>(request: Request | PikkuHTTPRequest,
|
|
|
87
87
|
* @param {RunRouteOptions & RunRouteParams} options - Options such as singleton services, session handling, and error configuration.
|
|
88
88
|
* @returns {Promise<Out | void>} The output from the route handler or void if an error occurred.
|
|
89
89
|
*/
|
|
90
|
-
export declare const fetchData: <In, Out>(request: Request | PikkuHTTPRequest, response: PikkuHTTPResponse, { singletonServices, createSessionServices, skipUserSession, respondWith404, logWarningsForStatusCodes, coerceDataFromSchema, bubbleErrors, generateRequestId, }: RunRouteOptions & RunRouteParams) => Promise<Out | void>;
|
|
90
|
+
export declare const fetchData: <In, Out>(request: Request | PikkuHTTPRequest, response: PikkuHTTPResponse, { singletonServices, createSessionServices, skipUserSession, respondWith404, logWarningsForStatusCodes, coerceDataFromSchema, bubbleErrors, generateRequestId, ignoreMiddleware, }: RunRouteOptions & RunRouteParams) => Promise<Out | void>;
|
|
@@ -1,13 +1,13 @@
|
|
|
1
1
|
import { match } from 'path-to-regexp';
|
|
2
|
-
import { MissingSessionError, NotFoundError } from '
|
|
3
|
-
import { closeSessionServices, createWeakUID, isSerializable, } from '
|
|
4
|
-
import { PikkuUserSessionService, } from '
|
|
5
|
-
import { runMiddleware } from '
|
|
6
|
-
import {
|
|
7
|
-
import { pikkuState } from '
|
|
2
|
+
import { MissingSessionError, NotFoundError } from '../../errors/errors.js';
|
|
3
|
+
import { closeSessionServices, createWeakUID, isSerializable, } from '../../utils.js';
|
|
4
|
+
import { PikkuUserSessionService, } from '../../services/user-session-service.js';
|
|
5
|
+
import { runMiddleware } from '../../middleware-runner.js';
|
|
6
|
+
import { handleHTTPError } from '../../handle-error.js';
|
|
7
|
+
import { pikkuState } from '../../pikku-state.js';
|
|
8
8
|
import { PikkuFetchHTTPResponse } from './pikku-fetch-http-response.js';
|
|
9
9
|
import { PikkuFetchHTTPRequest } from './pikku-fetch-http-request.js';
|
|
10
|
-
import { addFunction, runPikkuFunc } from '
|
|
10
|
+
import { addFunction, runPikkuFunc } from '../../function/function-runner.js';
|
|
11
11
|
import { rpcService } from '../rpc/rpc-runner.js';
|
|
12
12
|
/**
|
|
13
13
|
* Registers middleware either globally or for a specific route.
|
|
@@ -21,14 +21,23 @@ import { rpcService } from '../rpc/rpc-runner.js';
|
|
|
21
21
|
* @param {APIMiddleware[]} [middleware] - The middleware array to apply when a route pattern is specified.
|
|
22
22
|
*/
|
|
23
23
|
export const addMiddleware = (routeOrMiddleware, middleware) => {
|
|
24
|
+
const middlewareStore = pikkuState('http', 'middleware');
|
|
24
25
|
if (typeof routeOrMiddleware === 'string' && middleware) {
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
26
|
+
const route = routeOrMiddleware;
|
|
27
|
+
// Remove existing entry for this route if it exists
|
|
28
|
+
const existingIndex = middlewareStore.findIndex((item) => item.route === route);
|
|
29
|
+
if (existingIndex !== -1) {
|
|
30
|
+
middlewareStore.splice(existingIndex, 1);
|
|
31
|
+
}
|
|
32
|
+
middlewareStore.push({ route, middleware });
|
|
29
33
|
}
|
|
30
34
|
else {
|
|
31
|
-
|
|
35
|
+
// Handle global middleware
|
|
36
|
+
const existingIndex = middlewareStore.findIndex((item) => item.route === '*');
|
|
37
|
+
if (existingIndex !== -1) {
|
|
38
|
+
middlewareStore.splice(existingIndex, 1);
|
|
39
|
+
}
|
|
40
|
+
middlewareStore.push({
|
|
32
41
|
route: '*',
|
|
33
42
|
middleware: routeOrMiddleware,
|
|
34
43
|
});
|
|
@@ -57,8 +66,6 @@ export const addHTTPRoute = (httpRoute) => {
|
|
|
57
66
|
}
|
|
58
67
|
addFunction(routeMeta.pikkuFuncName, {
|
|
59
68
|
func: httpRoute.func,
|
|
60
|
-
auth: httpRoute.auth,
|
|
61
|
-
permissions: httpRoute.permissions,
|
|
62
69
|
});
|
|
63
70
|
const routes = pikkuState('http', 'routes');
|
|
64
71
|
if (!routes.has(httpRoute.method)) {
|
|
@@ -217,7 +224,7 @@ const executeRouteWithMiddleware = async (services, matchedRoute, http, options)
|
|
|
217
224
|
channel,
|
|
218
225
|
});
|
|
219
226
|
};
|
|
220
|
-
|
|
227
|
+
result = await runPikkuFunc(meta.pikkuFuncName, {
|
|
221
228
|
getAllServices,
|
|
222
229
|
session,
|
|
223
230
|
data,
|
|
@@ -234,7 +241,6 @@ const executeRouteWithMiddleware = async (services, matchedRoute, http, options)
|
|
|
234
241
|
http?.response?.status(200);
|
|
235
242
|
// TODO: Evaluate if the response stream should be explicitly ended.
|
|
236
243
|
// http?.response?.end()
|
|
237
|
-
return result;
|
|
238
244
|
};
|
|
239
245
|
// Execute middleware, then run the main logic
|
|
240
246
|
await runMiddleware({ ...singletonServices, userSession }, { http }, middleware, runMain);
|
|
@@ -295,7 +301,7 @@ export const pikkuFetch = async (request, params) => {
|
|
|
295
301
|
* @param {RunRouteOptions & RunRouteParams} options - Options such as singleton services, session handling, and error configuration.
|
|
296
302
|
* @returns {Promise<Out | void>} The output from the route handler or void if an error occurred.
|
|
297
303
|
*/
|
|
298
|
-
export const fetchData = async (request, response, { singletonServices, createSessionServices, skipUserSession = false, respondWith404 = true, logWarningsForStatusCodes = [], coerceDataFromSchema = true, bubbleErrors = false, generateRequestId, }) => {
|
|
304
|
+
export const fetchData = async (request, response, { singletonServices, createSessionServices, skipUserSession = false, respondWith404 = true, logWarningsForStatusCodes = [], coerceDataFromSchema = true, bubbleErrors = false, generateRequestId, ignoreMiddleware = false, }) => {
|
|
299
305
|
const requestId = request.getHeader?.('x-request-id') ||
|
|
300
306
|
generateRequestId?.() ||
|
|
301
307
|
createWeakUID();
|
|
@@ -326,12 +332,17 @@ export const fetchData = async (request, response, { singletonServices, createSe
|
|
|
326
332
|
createSessionServices,
|
|
327
333
|
skipUserSession,
|
|
328
334
|
requestId,
|
|
329
|
-
},
|
|
335
|
+
}, ignoreMiddleware
|
|
336
|
+
? {
|
|
337
|
+
...matchedRoute,
|
|
338
|
+
middleware: [],
|
|
339
|
+
}
|
|
340
|
+
: matchedRoute, http, { coerceDataFromSchema }));
|
|
330
341
|
return result;
|
|
331
342
|
}
|
|
332
343
|
catch (e) {
|
|
333
344
|
// Handle errors and, depending on configuration, bubble them up or respond with an error
|
|
334
|
-
|
|
345
|
+
handleHTTPError(e, http, requestId, singletonServices.logger, logWarningsForStatusCodes, respondWith404, bubbleErrors);
|
|
335
346
|
}
|
|
336
347
|
finally {
|
|
337
348
|
// Clean up any session-specific services created during processing
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import type { SerializeOptions } from 'cookie';
|
|
2
|
-
import type { PikkuError } from '
|
|
3
|
-
import type { APIDocs, CoreServices, CoreSingletonServices, CoreUserSession, CreateSessionServices, PikkuMiddleware } from '
|
|
4
|
-
import type { CoreAPIFunction, CoreAPIFunctionSessionless, CoreAPIPermission, CorePermissionGroup } from '
|
|
2
|
+
import type { PikkuError } from '../../errors/error-handler.js';
|
|
3
|
+
import type { APIDocs, CoreServices, CoreSingletonServices, CoreUserSession, CreateSessionServices, PikkuMiddleware } from '../../types/core.types.js';
|
|
4
|
+
import type { CoreAPIFunction, CoreAPIFunctionSessionless, CoreAPIPermission, CorePermissionGroup } from '../../function/functions.types.js';
|
|
5
5
|
type ExtractRouteParams<S extends string> = S extends `${string}:${infer Param}/${infer Rest}` ? Param | ExtractRouteParams<`/${Rest}`> : S extends `${string}:${infer Param}` ? Param : never;
|
|
6
6
|
export type AssertRouteParams<In, Route extends string> = ExtractRouteParams<Route> extends keyof In ? unknown : ['Error: Route parameters', ExtractRouteParams<Route>, 'not in', keyof In];
|
|
7
7
|
export type RunRouteOptions = Partial<{
|
|
@@ -11,6 +11,7 @@ export type RunRouteOptions = Partial<{
|
|
|
11
11
|
coerceDataFromSchema: boolean;
|
|
12
12
|
bubbleErrors: boolean;
|
|
13
13
|
generateRequestId: () => string;
|
|
14
|
+
ignoreMiddleware: boolean;
|
|
14
15
|
}>;
|
|
15
16
|
export type RunRouteParams = {
|
|
16
17
|
singletonServices: CoreSingletonServices;
|
|
@@ -1,6 +1,5 @@
|
|
|
1
1
|
export * from './pikku-fetch-http-request.js';
|
|
2
2
|
export * from './pikku-fetch-http-response.js';
|
|
3
|
-
export * from './incomingmessage-to-request-convertor.js';
|
|
4
3
|
export * from './log-http-routes.js';
|
|
5
4
|
export { fetch, fetchData, addHTTPRoute } from './http-runner.js';
|
|
6
5
|
export type * from './http.types.js';
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { parse as parseQuery } from 'picoquery';
|
|
2
2
|
import { parse as parseCookie } from 'cookie';
|
|
3
|
-
import { UnprocessableContentError } from '
|
|
3
|
+
import { UnprocessableContentError } from '../../errors/errors.js';
|
|
4
4
|
/**
|
|
5
5
|
* Abstract class representing a pikku request.
|
|
6
6
|
* @template In - The type of the request body.
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import type { CoreServices, CoreSingletonServices, CoreUserSession, CreateSessionServices } from '../../types/core.types.js';
|
|
2
|
+
import type { CoreMCPResource, CoreMCPTool, CoreMCPPrompt, JsonRpcRequest, JsonRpcResponse, JsonRpcErrorResponse, PikkuMCP } from './mcp.types.js';
|
|
3
|
+
import type { CoreAPIFunctionSessionless } from '../../function/functions.types.js';
|
|
4
|
+
export declare class MCPError extends Error {
|
|
5
|
+
readonly error: JsonRpcErrorResponse;
|
|
6
|
+
constructor(error: JsonRpcErrorResponse);
|
|
7
|
+
}
|
|
8
|
+
export type RunMCPEndpointParams<Tools extends string = any> = {
|
|
9
|
+
session?: CoreUserSession;
|
|
10
|
+
singletonServices: CoreSingletonServices;
|
|
11
|
+
mcp?: PikkuMCP<Tools>;
|
|
12
|
+
createSessionServices?: CreateSessionServices<CoreSingletonServices, CoreServices<CoreSingletonServices>, CoreUserSession>;
|
|
13
|
+
};
|
|
14
|
+
export type JsonRpcError = {
|
|
15
|
+
code: number;
|
|
16
|
+
message: string;
|
|
17
|
+
data?: any;
|
|
18
|
+
};
|
|
19
|
+
export declare const addMCPResource: <APIFunction extends CoreAPIFunctionSessionless<any, any>>(mcpResource: CoreMCPResource<APIFunction>) => void;
|
|
20
|
+
export declare const addMCPTool: <APIFunction extends CoreAPIFunctionSessionless<any, any>>(mcpTool: CoreMCPTool<APIFunction>) => void;
|
|
21
|
+
export declare const addMCPPrompt: <APIFunction extends CoreAPIFunctionSessionless<any, any>>(mcpPrompt: CoreMCPPrompt<APIFunction>) => void;
|
|
22
|
+
export declare function runMCPResource(request: JsonRpcRequest, params: RunMCPEndpointParams, uri: string): Promise<JsonRpcResponse>;
|
|
23
|
+
export declare function runMCPTool(request: JsonRpcRequest, params: RunMCPEndpointParams, name: string): Promise<JsonRpcResponse>;
|
|
24
|
+
export declare function runMCPPrompt(request: JsonRpcRequest, params: RunMCPEndpointParams, name: string): Promise<JsonRpcResponse>;
|
|
25
|
+
export declare const getMCPTools: () => Map<string, CoreMCPTool>;
|
|
26
|
+
export declare const getMCPResources: () => Map<string, CoreMCPResource>;
|
|
27
|
+
export declare const getMCPResourcesMeta: () => import("./mcp.types.js").MCPResourceMeta;
|
|
28
|
+
export declare const getMCPToolsMeta: () => import("./mcp.types.js").MCPToolMeta;
|
|
29
|
+
export declare const getMCPPrompts: () => Map<string, CoreMCPPrompt>;
|
|
30
|
+
export declare const getMCPPromptsMeta: () => import("./mcp.types.js").MCPPromptMeta;
|