@k13engineering/yajrpc 0.0.2 → 0.0.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.
@@ -1,4 +1,12 @@
1
- /*import type { TJsonRpcError, TJsonRpcMessage, TJsonRpcNotification, TJsonRpcOptionalId, TJsonRpcParameters, TJsonRpcRequest, TJsonRpcResponse } from "./types.ts";*/
1
+ /*import type {
2
+ TJsonRpcError,
3
+ TJsonRpcMessage,
4
+ TJsonRpcNotification,
5
+ TJsonRpcOptionalId,
6
+ TJsonRpcParameters,
7
+ TJsonRpcRequest,
8
+ TJsonRpcResponse
9
+ } from "./types.ts";*/
2
10
 
3
11
  /*type TCoerceParamsResult = {
4
12
  error: Error;
@@ -0,0 +1,37 @@
1
+ import type { TNotifyMethod, TRequestMethod } from "../types.js";
2
+ import type { TRpcNotificationsDefinition } from "./notification.js";
3
+ import type { TRpcRequestsDefinition } from "./requests.js";
4
+ declare const createCombined: <TSideARequests extends TRpcRequestsDefinition<any>, TSideANotifications extends TRpcNotificationsDefinition<any>, TSideBRequests extends TRpcRequestsDefinition<any>, TSideBNotifications extends TRpcNotificationsDefinition<any>>({ sideADefinitions, sideBDefinitions }: {
5
+ sideADefinitions: {
6
+ requests: TSideARequests;
7
+ notifications: TSideANotifications;
8
+ };
9
+ sideBDefinitions: {
10
+ requests: TSideBRequests;
11
+ notifications: TSideBNotifications;
12
+ };
13
+ }) => {
14
+ sideA: ({ incomingRequests, incomingNotifications, outgoingRequest, outgoingNotify }: {
15
+ incomingRequests: Parameters<TSideARequests["createServer"]>[0];
16
+ incomingNotifications: Parameters<TSideANotifications["createServer"]>[0];
17
+ outgoingRequest: TRequestMethod;
18
+ outgoingNotify: TNotifyMethod;
19
+ }) => {
20
+ outgoingRequests: ReturnType<TSideBRequests["createClient"]>;
21
+ outgoingNotifications: ReturnType<TSideBNotifications["createClient"]>;
22
+ handleIncomingRequest: any;
23
+ handleIncomingNotification: any;
24
+ };
25
+ sideB: ({ incomingRequests, incomingNotifications, outgoingRequest, outgoingNotify }: {
26
+ incomingRequests: Parameters<TSideBRequests["createServer"]>[0];
27
+ incomingNotifications: Parameters<TSideBNotifications["createServer"]>[0];
28
+ outgoingRequest: TRequestMethod;
29
+ outgoingNotify: TNotifyMethod;
30
+ }) => {
31
+ outgoingRequests: ReturnType<TSideARequests["createClient"]>;
32
+ outgoingNotifications: ReturnType<TSideANotifications["createClient"]>;
33
+ handleIncomingRequest: any;
34
+ handleIncomingNotification: any;
35
+ };
36
+ };
37
+ export { createCombined };
@@ -0,0 +1,102 @@
1
+ /*import type {
2
+ TNotifyMethod,
3
+ TRequestMethod
4
+ } from "../types.ts";*/
5
+ /*import type { TRpcNotificationsDefinition } from "./notification.ts";*/
6
+ /*import type { TRpcRequestsDefinition } from "./requests.ts";*/
7
+
8
+ const createCombined = /*<
9
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
10
+ TSideARequests extends TRpcRequestsDefinition<any>,
11
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
12
+ TSideANotifications extends TRpcNotificationsDefinition<any>,
13
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
14
+ TSideBRequests extends TRpcRequestsDefinition<any>,
15
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
16
+ TSideBNotifications extends TRpcNotificationsDefinition<any>>*/({
17
+ sideADefinitions,
18
+ sideBDefinitions
19
+ }/*: {
20
+ sideADefinitions: {
21
+ requests: TSideARequests;
22
+ notifications: TSideANotifications;
23
+ },
24
+ sideBDefinitions: {
25
+ requests: TSideBRequests;
26
+ notifications: TSideBNotifications;
27
+ }
28
+ }*/) => {
29
+
30
+ const sideA = ({
31
+ incomingRequests,
32
+ incomingNotifications,
33
+
34
+ outgoingRequest,
35
+ outgoingNotify
36
+ }/*: {
37
+ incomingRequests: Parameters<TSideARequests["createServer"]>[0];
38
+ incomingNotifications: Parameters<TSideANotifications["createServer"]>[0];
39
+
40
+ outgoingRequest: TRequestMethod;
41
+ outgoingNotify: TNotifyMethod;
42
+ }*/) => {
43
+ const { handleRequest } = sideADefinitions.requests.createServer(incomingRequests);
44
+ const { handleNotification } = sideADefinitions.notifications.createServer(incomingNotifications);
45
+
46
+ const outgoingRequests = sideBDefinitions.requests.createClient({
47
+ request: outgoingRequest
48
+ });
49
+
50
+ const outgoingNotifications = sideBDefinitions.notifications.createClient({
51
+ notify: outgoingNotify
52
+ });
53
+
54
+ return {
55
+ outgoingRequests: outgoingRequests /*as ReturnType<TSideBRequests["createClient"]>*/,
56
+ outgoingNotifications: outgoingNotifications /*as ReturnType<TSideBNotifications["createClient"]>*/,
57
+
58
+ handleIncomingRequest: handleRequest,
59
+ handleIncomingNotification: handleNotification
60
+ };
61
+ };
62
+
63
+ const sideB = ({
64
+ incomingRequests,
65
+ incomingNotifications,
66
+
67
+ outgoingRequest,
68
+ outgoingNotify
69
+ }/*: {
70
+ incomingRequests: Parameters<TSideBRequests["createServer"]>[0];
71
+ incomingNotifications: Parameters<TSideBNotifications["createServer"]>[0];
72
+
73
+ outgoingRequest: TRequestMethod
74
+ outgoingNotify: TNotifyMethod;
75
+ }*/) => {
76
+ const { handleRequest } = sideBDefinitions.requests.createServer(incomingRequests);
77
+ const { handleNotification } = sideBDefinitions.notifications.createServer(incomingNotifications);
78
+
79
+ const outgoingRequests = sideADefinitions.requests.createClient({
80
+ request: outgoingRequest
81
+ });
82
+
83
+ const outgoingNotifications = sideADefinitions.notifications.createClient({
84
+ notify: outgoingNotify
85
+ });
86
+
87
+ return {
88
+ outgoingRequests: outgoingRequests /*as ReturnType<TSideARequests["createClient"]>*/,
89
+ outgoingNotifications: outgoingNotifications /*as ReturnType<TSideANotifications["createClient"]>*/,
90
+
91
+ handleIncomingRequest: handleRequest,
92
+ handleIncomingNotification: handleNotification
93
+ };
94
+ };
95
+
96
+ return {
97
+ sideA,
98
+ sideB
99
+ };
100
+ };
101
+
102
+ export { createCombined };
@@ -0,0 +1,32 @@
1
+ import { type TObjectParser } from "./parser.js";
2
+ import type { TJsonRpcParameters, TNotificationHandler, TNotifyMethod } from "../types.js";
3
+ type RpcNotificationDefinition<TParams = unknown> = {
4
+ paramsParser: TObjectParser<TParams>;
5
+ };
6
+ type RpcNotificationsDefinitionMap = Record<string, RpcNotificationDefinition<any>>;
7
+ type ExtractParserType<T> = T extends TObjectParser<infer U> ? U : never;
8
+ type ServerHandlers<TMap extends RpcNotificationsDefinitionMap> = {
9
+ [K in keyof TMap]: (params: ExtractParserType<TMap[K]["paramsParser"]>) => void;
10
+ };
11
+ type ClientMethods<TMap extends RpcNotificationsDefinitionMap> = {
12
+ [K in keyof TMap]: (params: ExtractParserType<TMap[K]["paramsParser"]>) => void;
13
+ };
14
+ declare const createRpcNotificationsDefinition: <const TMap extends RpcNotificationsDefinitionMap>(definitions: TMap) => {
15
+ createServer: ({ notifications, handleUnknownNotification, handleParametersParseError }: {
16
+ notifications: ServerHandlers<TMap>;
17
+ handleUnknownNotification: TNotificationHandler;
18
+ handleParametersParseError: (args: {
19
+ method: string;
20
+ params: TJsonRpcParameters | undefined;
21
+ parseError: Error;
22
+ }) => void;
23
+ }) => {
24
+ handleNotification: TNotificationHandler;
25
+ };
26
+ createClient: ({ notify }: {
27
+ notify: TNotifyMethod;
28
+ }) => ClientMethods<TMap>;
29
+ };
30
+ type TRpcNotificationsDefinition<TMap extends RpcNotificationsDefinitionMap> = ReturnType<typeof createRpcNotificationsDefinition<TMap>>;
31
+ export { createRpcNotificationsDefinition };
32
+ export type { TRpcNotificationsDefinition };
@@ -0,0 +1,95 @@
1
+ import { /*type TObjectParser */} from "./parser.js";
2
+ /*import type {
3
+ TJsonRpcParameters,
4
+ TNotificationHandler,
5
+ TNotifyMethod
6
+ } from "../types.ts";*/
7
+
8
+ /*type RpcNotificationDefinition<TParams = unknown> = {
9
+ paramsParser: TObjectParser<TParams>;
10
+ };*/
11
+
12
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
13
+ /*type RpcNotificationsDefinitionMap = Record<string, RpcNotificationDefinition<any>>;*/
14
+
15
+ /*type ExtractParserType<T> = T extends TObjectParser<infer U> ? U : never;*/
16
+
17
+ /*type ServerHandlers<TMap extends RpcNotificationsDefinitionMap> = {
18
+ [K in keyof TMap]: (params: ExtractParserType<TMap[K]["paramsParser"]>) => void;
19
+ };*/
20
+
21
+ /*type ClientMethods<TMap extends RpcNotificationsDefinitionMap> = {
22
+ [K in keyof TMap]: (params: ExtractParserType<TMap[K]["paramsParser"]>) => void;
23
+ };*/
24
+
25
+ const createRpcNotificationsDefinition = /*<const TMap extends RpcNotificationsDefinitionMap>*/(
26
+ definitions/*: TMap*/
27
+ ) => {
28
+ const createServer = ({
29
+ notifications,
30
+ handleUnknownNotification,
31
+ handleParametersParseError
32
+ }/*: {
33
+ notifications: ServerHandlers<TMap>;
34
+ handleUnknownNotification: TNotificationHandler;
35
+ handleParametersParseError: (args: {
36
+ method: string;
37
+ params: TJsonRpcParameters | undefined;
38
+ parseError: Error;
39
+ }) => void;
40
+ }*/) => {
41
+ const handleNotification/*: TNotificationHandler*/ = ({ method, params }) => {
42
+ const definition = definitions[method];
43
+ if (!definition) {
44
+ handleUnknownNotification({ method, params });
45
+ return;
46
+ }
47
+
48
+ const parseResult = definition.paramsParser.parse({ raw: params });
49
+ if (parseResult.error) {
50
+ handleParametersParseError({
51
+ method,
52
+ params,
53
+ parseError: parseResult.error
54
+ });
55
+ return;
56
+ }
57
+
58
+ const handler = notifications[method /*as keyof TMap*/];
59
+ handler(parseResult.value /*as ExtractParserType<TMap[keyof TMap]["paramsParser"]>*/);
60
+ };
61
+
62
+ return {
63
+ handleNotification
64
+ };
65
+ };
66
+
67
+ const createClient = ({ notify }/*: { notify: TNotifyMethod }*/) => {
68
+ const client = {} /*as ClientMethods<TMap>*/;
69
+
70
+ for (const method of Object.keys(definitions) /*as Array<keyof TMap & string>*/) {
71
+ const definition = definitions[method];
72
+ (client /*as Record<string, unknown>*/)[method] = (params/*: unknown*/) => {
73
+ const formattedParams = definition.paramsParser.format({ value: params });
74
+ notify({ method, params: formattedParams /*as Record<string, unknown>*/ });
75
+ };
76
+ }
77
+
78
+ return client;
79
+ };
80
+
81
+ return {
82
+ createServer,
83
+ createClient
84
+ };
85
+ };
86
+
87
+ /*type TRpcNotificationsDefinition<TMap extends RpcNotificationsDefinitionMap> = ReturnType<
88
+ typeof createRpcNotificationsDefinition<TMap>
89
+ >;*/
90
+
91
+ export { createRpcNotificationsDefinition };
92
+
93
+ /*export type {
94
+ TRpcNotificationsDefinition
95
+ };*/
@@ -0,0 +1,16 @@
1
+ type TObjectParseResult<T> = {
2
+ error: Error;
3
+ value: undefined;
4
+ } | {
5
+ error: undefined;
6
+ value: T;
7
+ };
8
+ type TObjectParser<T> = {
9
+ parse: ({ raw }: {
10
+ raw: unknown;
11
+ }) => TObjectParseResult<T>;
12
+ format: ({ value }: {
13
+ value: T;
14
+ }) => unknown;
15
+ };
16
+ export type { TObjectParser, TObjectParseResult };
@@ -0,0 +1,17 @@
1
+ /*type TObjectParseResult<T> = {
2
+ error: Error;
3
+ value: undefined;
4
+ } | {
5
+ error: undefined;
6
+ value: T;
7
+ };*/
8
+
9
+ /*type TObjectParser<T> = {
10
+ parse: ({ raw }: { raw: unknown }) => TObjectParseResult<T>;
11
+ format: ({ value }: { value: T }) => unknown;
12
+ };*/
13
+
14
+ /*export type {
15
+ TObjectParser,
16
+ TObjectParseResult
17
+ };*/
@@ -0,0 +1,39 @@
1
+ import { type TObjectParser } from "./parser.js";
2
+ import type { TJsonRpcParameters, TRequestHandler, TRequestHandlerResponse, TRequestMethod } from "../types.js";
3
+ type RpcRequestDefinition<TParams = unknown, TResult = unknown> = {
4
+ paramsParser: TObjectParser<TParams>;
5
+ resultParser: TObjectParser<TResult>;
6
+ };
7
+ type RpcRequestsDefinitionMap = Record<string, RpcRequestDefinition<any, any>>;
8
+ type ExtractParserType<T> = T extends TObjectParser<infer U> ? U : never;
9
+ type ServerHandlers<TMap extends RpcRequestsDefinitionMap> = {
10
+ [K in keyof TMap]: (params: ExtractParserType<TMap[K]["paramsParser"]>) => Promise<ExtractParserType<TMap[K]["resultParser"]>>;
11
+ };
12
+ type ClientMethods<TMap extends RpcRequestsDefinitionMap> = {
13
+ [K in keyof TMap]: (params: ExtractParserType<TMap[K]["paramsParser"]>) => Promise<{
14
+ error: Error;
15
+ result: undefined;
16
+ } | {
17
+ error: undefined;
18
+ result: ExtractParserType<TMap[K]["resultParser"]>;
19
+ }>;
20
+ };
21
+ declare const createRpcRequestsDefinition: <const TMap extends RpcRequestsDefinitionMap>(definitions: TMap) => {
22
+ createServer: ({ requests, handleUnknownRequest, handleParametersParseError }: {
23
+ requests: ServerHandlers<TMap>;
24
+ handleUnknownRequest: TRequestHandler;
25
+ handleParametersParseError: (args: {
26
+ method: string;
27
+ params: TJsonRpcParameters | undefined;
28
+ parseError: Error;
29
+ }) => Promise<TRequestHandlerResponse>;
30
+ }) => {
31
+ handleRequest: TRequestHandler;
32
+ };
33
+ createClient: ({ request }: {
34
+ request: TRequestMethod;
35
+ }) => ClientMethods<TMap>;
36
+ };
37
+ type TRpcRequestsDefinition<TMap extends RpcRequestsDefinitionMap> = ReturnType<typeof createRpcRequestsDefinition<TMap>>;
38
+ export { createRpcRequestsDefinition };
39
+ export type { TRpcRequestsDefinition };
@@ -0,0 +1,134 @@
1
+ import { /*type TObjectParser */} from "./parser.js";
2
+ /*import type {
3
+ TJsonRpcParameters,
4
+ TRequestHandler,
5
+ TRequestHandlerResponse,
6
+ TRequestMethod,
7
+ TRequestResponseValue
8
+ } from "../types.ts";*/
9
+
10
+ /*type RpcRequestDefinition<TParams = unknown, TResult = unknown> = {
11
+ paramsParser: TObjectParser<TParams>;
12
+ resultParser: TObjectParser<TResult>;
13
+ };*/
14
+
15
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
16
+ /*type RpcRequestsDefinitionMap = Record<string, RpcRequestDefinition<any, any>>;*/
17
+
18
+ /*type ExtractParserType<T> = T extends TObjectParser<infer U> ? U : never;*/
19
+
20
+ /*type ServerHandlers<TMap extends RpcRequestsDefinitionMap> = {
21
+ [K in keyof TMap]: (
22
+ params: ExtractParserType<TMap[K]["paramsParser"]>
23
+ ) => Promise<ExtractParserType<TMap[K]["resultParser"]>>;
24
+ };*/
25
+
26
+ /*type ClientMethods<TMap extends RpcRequestsDefinitionMap> = {
27
+ [K in keyof TMap]: (
28
+ params: ExtractParserType<TMap[K]["paramsParser"]>
29
+ ) => Promise<
30
+ | { error: Error; result: undefined }
31
+ | { error: undefined; result: ExtractParserType<TMap[K]["resultParser"]> }
32
+ >;
33
+ };*/
34
+
35
+ const createRpcRequestsDefinition = /*<const TMap extends RpcRequestsDefinitionMap>*/(
36
+ definitions/*: TMap*/
37
+ ) => {
38
+ const createServer = ({
39
+ requests,
40
+ handleUnknownRequest,
41
+ handleParametersParseError
42
+ }/*: {
43
+ requests: ServerHandlers<TMap>;
44
+ handleUnknownRequest: TRequestHandler;
45
+ handleParametersParseError: (args: {
46
+ method: string;
47
+ params: TJsonRpcParameters | undefined;
48
+ parseError: Error;
49
+ }) => Promise<TRequestHandlerResponse>;
50
+ }*/) => {
51
+ const handleRequest/*: TRequestHandler*/ = async ({ method, params }) => {
52
+ const definition = definitions[method];
53
+ if (!definition) {
54
+ return await handleUnknownRequest({ method, params });
55
+ }
56
+
57
+ const parseResult = definition.paramsParser.parse({ raw: params });
58
+ if (parseResult.error) {
59
+ return await handleParametersParseError({
60
+ method,
61
+ params,
62
+ parseError: parseResult.error
63
+ });
64
+ }
65
+
66
+ const handler = requests[method /*as keyof TMap*/];
67
+ const result = await handler(
68
+ parseResult.value /*as ExtractParserType<TMap[keyof TMap]["paramsParser"]>*/
69
+ );
70
+ const formattedResult = definition.resultParser.format({ value: result });
71
+
72
+ return {
73
+ result: formattedResult /*as TRequestResponseValue*/,
74
+ error: undefined
75
+ };
76
+ };
77
+
78
+ return {
79
+ handleRequest
80
+ };
81
+ };
82
+
83
+ const createClient = ({ request }/*: { request: TRequestMethod }*/) => {
84
+ const client = {} /*as ClientMethods<TMap>*/;
85
+
86
+ for (const method of Object.keys(definitions) /*as Array<keyof TMap & string>*/) {
87
+ const definition = definitions[method];
88
+ (client /*as Record<string, unknown>*/)[method] = async (params/*: unknown*/) => {
89
+ const formattedParams = definition.paramsParser.format({ value: params });
90
+ const { error: requestError, response } = await request({
91
+ method,
92
+ params: formattedParams /*as Record<string, unknown>*/
93
+ });
94
+ if (requestError !== undefined) {
95
+ return {
96
+ error: Error("failed to execute request", { cause: requestError }),
97
+ result: undefined
98
+ };
99
+ }
100
+ const { error: parseError, value } = definition.resultParser.parse({
101
+ raw: response.result
102
+ });
103
+ if (parseError !== undefined) {
104
+ return {
105
+ error: Error("failed to parse result", { cause: parseError }),
106
+ result: undefined
107
+ };
108
+ }
109
+
110
+ return {
111
+ error: undefined,
112
+ result: value
113
+ };
114
+ };
115
+ }
116
+
117
+ return client;
118
+ };
119
+
120
+ return {
121
+ createServer,
122
+ createClient
123
+ };
124
+ };
125
+
126
+ /*type TRpcRequestsDefinition<TMap extends RpcRequestsDefinitionMap> = ReturnType<
127
+ typeof createRpcRequestsDefinition<TMap>
128
+ >;*/
129
+
130
+ export { createRpcRequestsDefinition };
131
+
132
+ /*export type {
133
+ TRpcRequestsDefinition
134
+ };*/
@@ -1,47 +1,8 @@
1
- import type { TJsonRpcError, TJsonRpcMessage, TJsonRpcParameters, TRequestResponse, TRequestResponseValue, TRequestResult } from "./types.js";
2
- type TRequestHandlerResponse = {
3
- result: TRequestResponseValue;
4
- error: undefined;
5
- } | {
6
- result: undefined;
7
- error: TJsonRpcError;
8
- } | {
9
- result: undefined;
10
- error: undefined;
11
- };
12
- type TRequestHandler = (args: {
13
- method: string;
14
- params: TJsonRpcParameters | undefined;
15
- }) => Promise<TRequestHandlerResponse>;
16
- type TNotificationHandler = (args: {
17
- method: string;
18
- params: TJsonRpcParameters | undefined;
19
- }) => void;
20
- type TNotifyMethod = (args: {
21
- method: string;
22
- params: TJsonRpcParameters;
23
- }) => void;
24
- type TRequestMethod = (args: {
25
- method: string;
26
- params: TJsonRpcParameters;
27
- timeoutMs?: number;
28
- }) => Promise<TRequestResult>;
29
- declare const createJrpc: ({ sendMessage, handleRequest, handleNotification }: {
30
- sendMessage: (args: {
31
- message: TJsonRpcMessage;
32
- }) => void;
33
- handleRequest: TRequestHandler;
34
- handleNotification: TNotificationHandler;
35
- }) => {
36
- receivedMessage: ({ message }: {
37
- message: unknown;
38
- }) => {
39
- error: Error | undefined;
40
- };
41
- request: TRequestMethod;
42
- notify: TNotifyMethod;
43
- close: () => void;
44
- };
45
- type TJrpc = ReturnType<typeof createJrpc>;
46
- export { createJrpc };
47
- export type { TJsonRpcMessage, TRequestResponse, TRequestResult, TJrpc, TRequestHandler, TNotificationHandler, TRequestMethod, TNotifyMethod };
1
+ import { createJrpc } from "./jrpc.js";
2
+ import { createWebSocketJrpc } from "./websocket.js";
3
+ import { createRpcRequestsDefinition } from "./definitions/requests.js";
4
+ import { createRpcNotificationsDefinition } from "./definitions/notification.js";
5
+ import { createCombined } from "./definitions/combined.js";
6
+ import type { TObjectParser } from "./definitions/parser.js";
7
+ export { createJrpc, createWebSocketJrpc, createRpcRequestsDefinition, createRpcNotificationsDefinition, createCombined };
8
+ export type { TObjectParser };