@cybermp/rpc-router 0.1.0-rc.1
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/LICENSE +21 -0
- package/dist/client/index.cjs +76 -0
- package/dist/client/index.d.cts +34 -0
- package/dist/client/index.d.mts +34 -0
- package/dist/client/index.d.ts +34 -0
- package/dist/client/index.mjs +74 -0
- package/dist/server/index.cjs +295 -0
- package/dist/server/index.d.cts +78 -0
- package/dist/server/index.d.mts +78 -0
- package/dist/server/index.d.ts +78 -0
- package/dist/server/index.mjs +285 -0
- package/dist/shared/rpc-router.Cu7hweRg.d.cts +157 -0
- package/dist/shared/rpc-router.Cu7hweRg.d.mts +157 -0
- package/dist/shared/rpc-router.Cu7hweRg.d.ts +157 -0
- package/package.json +36 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 CyberMP-RPC
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const definitions = require('@cybermp/rpc-core/definitions');
|
|
4
|
+
const utils = require('@cybermp/rpc-core/utils');
|
|
5
|
+
|
|
6
|
+
const parseClientRest = (...args) => {
|
|
7
|
+
let player;
|
|
8
|
+
let data;
|
|
9
|
+
let meta;
|
|
10
|
+
let options;
|
|
11
|
+
if (utils.CURRENT_ENVIRONMENT === definitions.MpEnv.SERVER) {
|
|
12
|
+
[player, data, meta, options] = args;
|
|
13
|
+
} else {
|
|
14
|
+
[data, meta, options] = args;
|
|
15
|
+
}
|
|
16
|
+
return { player, data, meta, options };
|
|
17
|
+
};
|
|
18
|
+
const createRouterClient = (options) => {
|
|
19
|
+
const path = options.path ?? [];
|
|
20
|
+
const { rpc, target: targetEnv } = options;
|
|
21
|
+
const triggerClient = (...args) => {
|
|
22
|
+
const { data = {}, meta = {}, player } = parseClientRest(...args);
|
|
23
|
+
rpc.trigger(
|
|
24
|
+
new definitions.RpcPacket(
|
|
25
|
+
{
|
|
26
|
+
type: definitions.RpcPacketType.TRIGGER,
|
|
27
|
+
method: path.join("."),
|
|
28
|
+
data,
|
|
29
|
+
meta: { ...meta, router: true }
|
|
30
|
+
},
|
|
31
|
+
{ target: targetEnv }
|
|
32
|
+
),
|
|
33
|
+
player
|
|
34
|
+
);
|
|
35
|
+
};
|
|
36
|
+
const callClient = (...args) => {
|
|
37
|
+
const {
|
|
38
|
+
data = {},
|
|
39
|
+
meta = {},
|
|
40
|
+
options: callOptions = {},
|
|
41
|
+
player
|
|
42
|
+
} = parseClientRest(...args);
|
|
43
|
+
return rpc.call(
|
|
44
|
+
new definitions.RpcPacket(
|
|
45
|
+
{
|
|
46
|
+
type: definitions.RpcPacketType.CALL,
|
|
47
|
+
method: path.join("."),
|
|
48
|
+
data,
|
|
49
|
+
meta: { ...meta, router: true }
|
|
50
|
+
},
|
|
51
|
+
{ target: targetEnv }
|
|
52
|
+
),
|
|
53
|
+
callOptions,
|
|
54
|
+
player
|
|
55
|
+
);
|
|
56
|
+
};
|
|
57
|
+
return new Proxy(
|
|
58
|
+
{
|
|
59
|
+
trigger: triggerClient,
|
|
60
|
+
call: callClient
|
|
61
|
+
},
|
|
62
|
+
{
|
|
63
|
+
get(target, key) {
|
|
64
|
+
if (key === "call" || key === "trigger") {
|
|
65
|
+
return Reflect.get(target, key);
|
|
66
|
+
}
|
|
67
|
+
return createRouterClient({
|
|
68
|
+
...options,
|
|
69
|
+
path: [...path, key]
|
|
70
|
+
});
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
);
|
|
74
|
+
};
|
|
75
|
+
|
|
76
|
+
exports.createRouterClient = createRouterClient;
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
import { CallOptions, RpcBase } from '@cybermp/rpc-core';
|
|
2
|
+
import { MpEnv, RpcApplyType } from '@cybermp/rpc-core/definitions';
|
|
3
|
+
import { k as RouterOrProcedure, n as ContractProcedure, I as InferSchemaInput, b as InferSchemaOutput, R as Router } from '../shared/rpc-router.Cu7hweRg.cjs';
|
|
4
|
+
|
|
5
|
+
type ClientReturn<TOutput, TMethod extends RpcApplyType> = TMethod extends RpcApplyType.ON ? void : Promise<TOutput>;
|
|
6
|
+
type ClientRest<TInput, TMethod extends RpcApplyType, TEnv extends MpEnv, TPlayer = number> = [
|
|
7
|
+
...(TEnv extends MpEnv.SERVER ? [player: TPlayer] : []),
|
|
8
|
+
...[
|
|
9
|
+
...(undefined extends TInput ? [data?: TInput] : [data: TInput]),
|
|
10
|
+
meta?: Record<string, any>,
|
|
11
|
+
...(TMethod extends RpcApplyType.ON ? [] : [options?: CallOptions])
|
|
12
|
+
]
|
|
13
|
+
];
|
|
14
|
+
type Client<TInput, TOutput, TMethod extends RpcApplyType, TEnv extends MpEnv, TPlayer = number> = TMethod extends RpcApplyType.ON ? {
|
|
15
|
+
trigger: (...args: ClientRest<TInput, TMethod, TEnv, TPlayer>) => ClientReturn<TOutput, TMethod>;
|
|
16
|
+
} : {
|
|
17
|
+
call: (...args: ClientRest<TInput, TMethod, TEnv, TPlayer>) => ClientReturn<TOutput, TMethod>;
|
|
18
|
+
};
|
|
19
|
+
type NestedClient = Client<any, any, RpcApplyType, MpEnv, any> | {
|
|
20
|
+
[k: string]: NestedClient;
|
|
21
|
+
};
|
|
22
|
+
type RouterClient<TRouter extends RouterOrProcedure, TEnv extends MpEnv, TPlayer = number> = TRouter extends ContractProcedure<infer UInput, infer UOutput, infer UMethod> ? Client<InferSchemaInput<UInput>, InferSchemaOutput<UOutput>, UMethod, TEnv, TPlayer> : {
|
|
23
|
+
[K in keyof TRouter]: TRouter[K] extends RouterOrProcedure ? RouterClient<TRouter[K], TEnv, TPlayer> : never;
|
|
24
|
+
};
|
|
25
|
+
|
|
26
|
+
type RouterClientOptions<TTargetEnv extends MpEnv> = {
|
|
27
|
+
target: TTargetEnv;
|
|
28
|
+
rpc: RpcBase<any>;
|
|
29
|
+
path?: readonly string[];
|
|
30
|
+
};
|
|
31
|
+
declare const createRouterClient: <R extends Router, TSourceEnv extends MpEnv, TTargetEnv extends MpEnv, TPlayer = number>(options: RouterClientOptions<TTargetEnv>) => RouterClient<R, TSourceEnv, TPlayer>;
|
|
32
|
+
|
|
33
|
+
export { createRouterClient };
|
|
34
|
+
export type { Client, ClientRest, NestedClient, RouterClient };
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
import { CallOptions, RpcBase } from '@cybermp/rpc-core';
|
|
2
|
+
import { MpEnv, RpcApplyType } from '@cybermp/rpc-core/definitions';
|
|
3
|
+
import { k as RouterOrProcedure, n as ContractProcedure, I as InferSchemaInput, b as InferSchemaOutput, R as Router } from '../shared/rpc-router.Cu7hweRg.mjs';
|
|
4
|
+
|
|
5
|
+
type ClientReturn<TOutput, TMethod extends RpcApplyType> = TMethod extends RpcApplyType.ON ? void : Promise<TOutput>;
|
|
6
|
+
type ClientRest<TInput, TMethod extends RpcApplyType, TEnv extends MpEnv, TPlayer = number> = [
|
|
7
|
+
...(TEnv extends MpEnv.SERVER ? [player: TPlayer] : []),
|
|
8
|
+
...[
|
|
9
|
+
...(undefined extends TInput ? [data?: TInput] : [data: TInput]),
|
|
10
|
+
meta?: Record<string, any>,
|
|
11
|
+
...(TMethod extends RpcApplyType.ON ? [] : [options?: CallOptions])
|
|
12
|
+
]
|
|
13
|
+
];
|
|
14
|
+
type Client<TInput, TOutput, TMethod extends RpcApplyType, TEnv extends MpEnv, TPlayer = number> = TMethod extends RpcApplyType.ON ? {
|
|
15
|
+
trigger: (...args: ClientRest<TInput, TMethod, TEnv, TPlayer>) => ClientReturn<TOutput, TMethod>;
|
|
16
|
+
} : {
|
|
17
|
+
call: (...args: ClientRest<TInput, TMethod, TEnv, TPlayer>) => ClientReturn<TOutput, TMethod>;
|
|
18
|
+
};
|
|
19
|
+
type NestedClient = Client<any, any, RpcApplyType, MpEnv, any> | {
|
|
20
|
+
[k: string]: NestedClient;
|
|
21
|
+
};
|
|
22
|
+
type RouterClient<TRouter extends RouterOrProcedure, TEnv extends MpEnv, TPlayer = number> = TRouter extends ContractProcedure<infer UInput, infer UOutput, infer UMethod> ? Client<InferSchemaInput<UInput>, InferSchemaOutput<UOutput>, UMethod, TEnv, TPlayer> : {
|
|
23
|
+
[K in keyof TRouter]: TRouter[K] extends RouterOrProcedure ? RouterClient<TRouter[K], TEnv, TPlayer> : never;
|
|
24
|
+
};
|
|
25
|
+
|
|
26
|
+
type RouterClientOptions<TTargetEnv extends MpEnv> = {
|
|
27
|
+
target: TTargetEnv;
|
|
28
|
+
rpc: RpcBase<any>;
|
|
29
|
+
path?: readonly string[];
|
|
30
|
+
};
|
|
31
|
+
declare const createRouterClient: <R extends Router, TSourceEnv extends MpEnv, TTargetEnv extends MpEnv, TPlayer = number>(options: RouterClientOptions<TTargetEnv>) => RouterClient<R, TSourceEnv, TPlayer>;
|
|
32
|
+
|
|
33
|
+
export { createRouterClient };
|
|
34
|
+
export type { Client, ClientRest, NestedClient, RouterClient };
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
import { CallOptions, RpcBase } from '@cybermp/rpc-core';
|
|
2
|
+
import { MpEnv, RpcApplyType } from '@cybermp/rpc-core/definitions';
|
|
3
|
+
import { k as RouterOrProcedure, n as ContractProcedure, I as InferSchemaInput, b as InferSchemaOutput, R as Router } from '../shared/rpc-router.Cu7hweRg.js';
|
|
4
|
+
|
|
5
|
+
type ClientReturn<TOutput, TMethod extends RpcApplyType> = TMethod extends RpcApplyType.ON ? void : Promise<TOutput>;
|
|
6
|
+
type ClientRest<TInput, TMethod extends RpcApplyType, TEnv extends MpEnv, TPlayer = number> = [
|
|
7
|
+
...(TEnv extends MpEnv.SERVER ? [player: TPlayer] : []),
|
|
8
|
+
...[
|
|
9
|
+
...(undefined extends TInput ? [data?: TInput] : [data: TInput]),
|
|
10
|
+
meta?: Record<string, any>,
|
|
11
|
+
...(TMethod extends RpcApplyType.ON ? [] : [options?: CallOptions])
|
|
12
|
+
]
|
|
13
|
+
];
|
|
14
|
+
type Client<TInput, TOutput, TMethod extends RpcApplyType, TEnv extends MpEnv, TPlayer = number> = TMethod extends RpcApplyType.ON ? {
|
|
15
|
+
trigger: (...args: ClientRest<TInput, TMethod, TEnv, TPlayer>) => ClientReturn<TOutput, TMethod>;
|
|
16
|
+
} : {
|
|
17
|
+
call: (...args: ClientRest<TInput, TMethod, TEnv, TPlayer>) => ClientReturn<TOutput, TMethod>;
|
|
18
|
+
};
|
|
19
|
+
type NestedClient = Client<any, any, RpcApplyType, MpEnv, any> | {
|
|
20
|
+
[k: string]: NestedClient;
|
|
21
|
+
};
|
|
22
|
+
type RouterClient<TRouter extends RouterOrProcedure, TEnv extends MpEnv, TPlayer = number> = TRouter extends ContractProcedure<infer UInput, infer UOutput, infer UMethod> ? Client<InferSchemaInput<UInput>, InferSchemaOutput<UOutput>, UMethod, TEnv, TPlayer> : {
|
|
23
|
+
[K in keyof TRouter]: TRouter[K] extends RouterOrProcedure ? RouterClient<TRouter[K], TEnv, TPlayer> : never;
|
|
24
|
+
};
|
|
25
|
+
|
|
26
|
+
type RouterClientOptions<TTargetEnv extends MpEnv> = {
|
|
27
|
+
target: TTargetEnv;
|
|
28
|
+
rpc: RpcBase<any>;
|
|
29
|
+
path?: readonly string[];
|
|
30
|
+
};
|
|
31
|
+
declare const createRouterClient: <R extends Router, TSourceEnv extends MpEnv, TTargetEnv extends MpEnv, TPlayer = number>(options: RouterClientOptions<TTargetEnv>) => RouterClient<R, TSourceEnv, TPlayer>;
|
|
32
|
+
|
|
33
|
+
export { createRouterClient };
|
|
34
|
+
export type { Client, ClientRest, NestedClient, RouterClient };
|
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
import { RpcPacket, RpcPacketType, MpEnv } from '@cybermp/rpc-core/definitions';
|
|
2
|
+
import { CURRENT_ENVIRONMENT } from '@cybermp/rpc-core/utils';
|
|
3
|
+
|
|
4
|
+
const parseClientRest = (...args) => {
|
|
5
|
+
let player;
|
|
6
|
+
let data;
|
|
7
|
+
let meta;
|
|
8
|
+
let options;
|
|
9
|
+
if (CURRENT_ENVIRONMENT === MpEnv.SERVER) {
|
|
10
|
+
[player, data, meta, options] = args;
|
|
11
|
+
} else {
|
|
12
|
+
[data, meta, options] = args;
|
|
13
|
+
}
|
|
14
|
+
return { player, data, meta, options };
|
|
15
|
+
};
|
|
16
|
+
const createRouterClient = (options) => {
|
|
17
|
+
const path = options.path ?? [];
|
|
18
|
+
const { rpc, target: targetEnv } = options;
|
|
19
|
+
const triggerClient = (...args) => {
|
|
20
|
+
const { data = {}, meta = {}, player } = parseClientRest(...args);
|
|
21
|
+
rpc.trigger(
|
|
22
|
+
new RpcPacket(
|
|
23
|
+
{
|
|
24
|
+
type: RpcPacketType.TRIGGER,
|
|
25
|
+
method: path.join("."),
|
|
26
|
+
data,
|
|
27
|
+
meta: { ...meta, router: true }
|
|
28
|
+
},
|
|
29
|
+
{ target: targetEnv }
|
|
30
|
+
),
|
|
31
|
+
player
|
|
32
|
+
);
|
|
33
|
+
};
|
|
34
|
+
const callClient = (...args) => {
|
|
35
|
+
const {
|
|
36
|
+
data = {},
|
|
37
|
+
meta = {},
|
|
38
|
+
options: callOptions = {},
|
|
39
|
+
player
|
|
40
|
+
} = parseClientRest(...args);
|
|
41
|
+
return rpc.call(
|
|
42
|
+
new RpcPacket(
|
|
43
|
+
{
|
|
44
|
+
type: RpcPacketType.CALL,
|
|
45
|
+
method: path.join("."),
|
|
46
|
+
data,
|
|
47
|
+
meta: { ...meta, router: true }
|
|
48
|
+
},
|
|
49
|
+
{ target: targetEnv }
|
|
50
|
+
),
|
|
51
|
+
callOptions,
|
|
52
|
+
player
|
|
53
|
+
);
|
|
54
|
+
};
|
|
55
|
+
return new Proxy(
|
|
56
|
+
{
|
|
57
|
+
trigger: triggerClient,
|
|
58
|
+
call: callClient
|
|
59
|
+
},
|
|
60
|
+
{
|
|
61
|
+
get(target, key) {
|
|
62
|
+
if (key === "call" || key === "trigger") {
|
|
63
|
+
return Reflect.get(target, key);
|
|
64
|
+
}
|
|
65
|
+
return createRouterClient({
|
|
66
|
+
...options,
|
|
67
|
+
path: [...path, key]
|
|
68
|
+
});
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
);
|
|
72
|
+
};
|
|
73
|
+
|
|
74
|
+
export { createRouterClient };
|
|
@@ -0,0 +1,295 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const definitions = require('@cybermp/rpc-core/definitions');
|
|
4
|
+
const radash = require('radash');
|
|
5
|
+
|
|
6
|
+
class ProcedureBuilder {
|
|
7
|
+
"~crpc";
|
|
8
|
+
constructor(def) {
|
|
9
|
+
this["~crpc"] = def;
|
|
10
|
+
}
|
|
11
|
+
context() {
|
|
12
|
+
return new ProcedureBuilder(
|
|
13
|
+
this["~crpc"]
|
|
14
|
+
);
|
|
15
|
+
}
|
|
16
|
+
input(schema) {
|
|
17
|
+
return new ProcedureBuilder({
|
|
18
|
+
...this["~crpc"],
|
|
19
|
+
inputSchema: schema
|
|
20
|
+
});
|
|
21
|
+
}
|
|
22
|
+
output(schema) {
|
|
23
|
+
return new ProcedureBuilder({
|
|
24
|
+
...this["~crpc"],
|
|
25
|
+
outputSchema: schema
|
|
26
|
+
});
|
|
27
|
+
}
|
|
28
|
+
method(method) {
|
|
29
|
+
return new ProcedureBuilder({
|
|
30
|
+
...this["~crpc"],
|
|
31
|
+
method
|
|
32
|
+
});
|
|
33
|
+
}
|
|
34
|
+
handler(...handler) {
|
|
35
|
+
return new Procedure({
|
|
36
|
+
...this["~crpc"],
|
|
37
|
+
handler
|
|
38
|
+
});
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
const procedure = new ProcedureBuilder({ method: definitions.RpcApplyType.ON });
|
|
42
|
+
|
|
43
|
+
class Procedure {
|
|
44
|
+
"~crpc";
|
|
45
|
+
constructor(def) {
|
|
46
|
+
this["~crpc"] = def;
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
function isProcedure(item) {
|
|
50
|
+
if (item instanceof Procedure) {
|
|
51
|
+
return true;
|
|
52
|
+
}
|
|
53
|
+
return (typeof item === "object" || typeof item === "function") && item !== null && "~crpc" in item && typeof item["~crpc"] === "object" && item["~crpc"] !== null && "handler" in item["~crpc"];
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
class ContractBuilder {
|
|
57
|
+
"~crpc";
|
|
58
|
+
constructor(def) {
|
|
59
|
+
this["~crpc"] = def;
|
|
60
|
+
}
|
|
61
|
+
input(schema) {
|
|
62
|
+
return new ContractBuilder({
|
|
63
|
+
...this["~crpc"],
|
|
64
|
+
inputSchema: schema
|
|
65
|
+
});
|
|
66
|
+
}
|
|
67
|
+
output(schema) {
|
|
68
|
+
return new ContractBuilder({
|
|
69
|
+
...this["~crpc"],
|
|
70
|
+
outputSchema: schema
|
|
71
|
+
});
|
|
72
|
+
}
|
|
73
|
+
method(method) {
|
|
74
|
+
return new ContractBuilder({
|
|
75
|
+
...this["~crpc"],
|
|
76
|
+
method
|
|
77
|
+
});
|
|
78
|
+
}
|
|
79
|
+
build() {
|
|
80
|
+
return new Contract(this["~crpc"]);
|
|
81
|
+
}
|
|
82
|
+
implement(...handler) {
|
|
83
|
+
return new Procedure({
|
|
84
|
+
...this["~crpc"],
|
|
85
|
+
handler
|
|
86
|
+
});
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
const contract = new ContractBuilder({ method: definitions.RpcApplyType.ON });
|
|
90
|
+
|
|
91
|
+
class Contract {
|
|
92
|
+
"~crpc";
|
|
93
|
+
constructor(def) {
|
|
94
|
+
this["~crpc"] = def;
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
function isContract(item) {
|
|
98
|
+
if (item instanceof Contract) {
|
|
99
|
+
return true;
|
|
100
|
+
}
|
|
101
|
+
return (typeof item === "object" || typeof item === "function") && item !== null && "~crpc" in item && typeof item["~crpc"] === "object" && item["~crpc"] !== null && !("handler" in item["~crpc"]);
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
class RpcRouter {
|
|
105
|
+
constructor(rpc, options) {
|
|
106
|
+
this.rpc = rpc;
|
|
107
|
+
this.options = options;
|
|
108
|
+
this.registerGlobalMiddleware();
|
|
109
|
+
}
|
|
110
|
+
contract = new ContractBuilder({
|
|
111
|
+
method: definitions.RpcApplyType.ON
|
|
112
|
+
});
|
|
113
|
+
procedure = new ProcedureBuilder({
|
|
114
|
+
method: definitions.RpcApplyType.ON
|
|
115
|
+
});
|
|
116
|
+
store = {};
|
|
117
|
+
contractProcedureMap = /* @__PURE__ */ new WeakMap();
|
|
118
|
+
async validateSchema(schema, value) {
|
|
119
|
+
const result = await schema["~standard"].validate(value);
|
|
120
|
+
if (result.issues) {
|
|
121
|
+
throw definitions.RpcError.invalidData({
|
|
122
|
+
message: `Validation failed at ${result.issues.map((i) => i.message).join(", ")}`,
|
|
123
|
+
data: {
|
|
124
|
+
issues: result.issues
|
|
125
|
+
}
|
|
126
|
+
});
|
|
127
|
+
}
|
|
128
|
+
return result.value;
|
|
129
|
+
}
|
|
130
|
+
shouldValidate(key) {
|
|
131
|
+
const v = this.options?.validation;
|
|
132
|
+
if (typeof v === "boolean" && v) {
|
|
133
|
+
return true;
|
|
134
|
+
}
|
|
135
|
+
if (typeof v === "object") {
|
|
136
|
+
return !!v?.[key];
|
|
137
|
+
}
|
|
138
|
+
return false;
|
|
139
|
+
}
|
|
140
|
+
registerGlobalMiddleware() {
|
|
141
|
+
this.rpc.use(async (ctx, next) => {
|
|
142
|
+
if (!ctx.meta.router) {
|
|
143
|
+
return next?.();
|
|
144
|
+
}
|
|
145
|
+
const procedureContext = ctx;
|
|
146
|
+
const procedure = this.store[ctx.packet.method];
|
|
147
|
+
if (!procedure || !isProcedure(procedure)) {
|
|
148
|
+
return next?.();
|
|
149
|
+
}
|
|
150
|
+
procedureContext.procedure = procedure;
|
|
151
|
+
const def = procedure["~crpc"];
|
|
152
|
+
if (def.inputSchema && this.shouldValidate("input")) {
|
|
153
|
+
procedureContext.data = await this.validateSchema(
|
|
154
|
+
def.inputSchema,
|
|
155
|
+
procedureContext.data
|
|
156
|
+
);
|
|
157
|
+
}
|
|
158
|
+
if (procedureContext.packet.type === definitions.RpcPacketType.TRIGGER) {
|
|
159
|
+
return next?.();
|
|
160
|
+
}
|
|
161
|
+
const result = await next?.();
|
|
162
|
+
if (def.outputSchema && this.shouldValidate("output")) {
|
|
163
|
+
const validatedResult = await this.validateSchema(
|
|
164
|
+
def.outputSchema,
|
|
165
|
+
result
|
|
166
|
+
);
|
|
167
|
+
return validatedResult;
|
|
168
|
+
}
|
|
169
|
+
return result;
|
|
170
|
+
});
|
|
171
|
+
}
|
|
172
|
+
apply(router) {
|
|
173
|
+
for (const [name, procedure] of Object.entries(this.store)) {
|
|
174
|
+
delete this.store[name];
|
|
175
|
+
if (isContract(procedure)) {
|
|
176
|
+
continue;
|
|
177
|
+
}
|
|
178
|
+
this.removeProcedure(name, procedure);
|
|
179
|
+
}
|
|
180
|
+
const flatRouter = radash.crush(router);
|
|
181
|
+
for (const [name, procedure] of Object.entries(flatRouter)) {
|
|
182
|
+
this.store[name] = procedure;
|
|
183
|
+
if (isContract(procedure)) {
|
|
184
|
+
continue;
|
|
185
|
+
}
|
|
186
|
+
this.applyProcedure(name, procedure);
|
|
187
|
+
}
|
|
188
|
+
}
|
|
189
|
+
findNameByProcedureOrContract(c) {
|
|
190
|
+
for (const [key, value] of Object.entries(this.store)) {
|
|
191
|
+
if (value === c) {
|
|
192
|
+
return key;
|
|
193
|
+
}
|
|
194
|
+
}
|
|
195
|
+
return null;
|
|
196
|
+
}
|
|
197
|
+
implement(contractOrTree, ...handlerOrTree) {
|
|
198
|
+
const [zeroHandler] = handlerOrTree;
|
|
199
|
+
if (isContract(contractOrTree) && typeof zeroHandler !== "object") {
|
|
200
|
+
this.implementContract(contractOrTree, ...handlerOrTree);
|
|
201
|
+
return;
|
|
202
|
+
}
|
|
203
|
+
this.implementContractTree(contractOrTree, zeroHandler);
|
|
204
|
+
}
|
|
205
|
+
implementContract(contract, ...handler) {
|
|
206
|
+
if (!isContract(contract)) {
|
|
207
|
+
return;
|
|
208
|
+
}
|
|
209
|
+
const name = this.findNameByProcedureOrContract(contract);
|
|
210
|
+
if (!name) {
|
|
211
|
+
return;
|
|
212
|
+
}
|
|
213
|
+
const proc = new Procedure({
|
|
214
|
+
...contract["~crpc"],
|
|
215
|
+
handler
|
|
216
|
+
});
|
|
217
|
+
this.contractProcedureMap.set(contract, proc);
|
|
218
|
+
this.store[name] = proc;
|
|
219
|
+
this.applyProcedure(name, proc);
|
|
220
|
+
}
|
|
221
|
+
implementContractTree(contractTree, handlerTree) {
|
|
222
|
+
for (const [key, contract] of Object.entries(contractTree)) {
|
|
223
|
+
const handler = handlerTree[key];
|
|
224
|
+
if (!handler) {
|
|
225
|
+
continue;
|
|
226
|
+
}
|
|
227
|
+
if (isContract(contract) && (Array.isArray(handler) || typeof handler === "function")) {
|
|
228
|
+
this.implementContract(
|
|
229
|
+
contract,
|
|
230
|
+
...Array.isArray(handler) ? handler : [handler]
|
|
231
|
+
);
|
|
232
|
+
} else if (typeof contract === "object" && typeof handler === "object") {
|
|
233
|
+
this.implementContractTree(
|
|
234
|
+
contract,
|
|
235
|
+
handler
|
|
236
|
+
);
|
|
237
|
+
}
|
|
238
|
+
}
|
|
239
|
+
}
|
|
240
|
+
unimplement(contract) {
|
|
241
|
+
if (isContract(contract)) {
|
|
242
|
+
this.unimplementContract(contract);
|
|
243
|
+
return;
|
|
244
|
+
}
|
|
245
|
+
this.unimplementContractTree(contract);
|
|
246
|
+
}
|
|
247
|
+
unimplementContract(contract) {
|
|
248
|
+
const proc = this.contractProcedureMap.get(contract);
|
|
249
|
+
if (!proc) {
|
|
250
|
+
return;
|
|
251
|
+
}
|
|
252
|
+
const name = this.findNameByProcedureOrContract(proc);
|
|
253
|
+
if (!name) {
|
|
254
|
+
return;
|
|
255
|
+
}
|
|
256
|
+
this.store[name] = contract;
|
|
257
|
+
this.contractProcedureMap.delete(contract);
|
|
258
|
+
this.removeProcedure(name, proc);
|
|
259
|
+
}
|
|
260
|
+
unimplementContractTree(contractTree) {
|
|
261
|
+
for (const contract of Object.values(contractTree)) {
|
|
262
|
+
if (isContract(contract)) {
|
|
263
|
+
this.unimplementContract(contract);
|
|
264
|
+
} else {
|
|
265
|
+
this.unimplementContractTree(contract);
|
|
266
|
+
}
|
|
267
|
+
}
|
|
268
|
+
}
|
|
269
|
+
removeProcedure(name, procedure) {
|
|
270
|
+
const def = procedure["~crpc"];
|
|
271
|
+
if (def.method === definitions.RpcApplyType.REGISTER) {
|
|
272
|
+
this.rpc.unregister(name);
|
|
273
|
+
} else if (def.method === definitions.RpcApplyType.ON) {
|
|
274
|
+
this.rpc.offAll(name);
|
|
275
|
+
}
|
|
276
|
+
}
|
|
277
|
+
applyProcedure(name, procedure) {
|
|
278
|
+
const def = procedure["~crpc"];
|
|
279
|
+
if (def.method === definitions.RpcApplyType.REGISTER) {
|
|
280
|
+
this.rpc.register(name, ...def.handler);
|
|
281
|
+
} else if (def.method === definitions.RpcApplyType.ON) {
|
|
282
|
+
this.rpc.on(name, ...def.handler);
|
|
283
|
+
}
|
|
284
|
+
}
|
|
285
|
+
}
|
|
286
|
+
|
|
287
|
+
exports.Contract = Contract;
|
|
288
|
+
exports.ContractBuilder = ContractBuilder;
|
|
289
|
+
exports.Procedure = Procedure;
|
|
290
|
+
exports.ProcedureBuilder = ProcedureBuilder;
|
|
291
|
+
exports.RpcRouter = RpcRouter;
|
|
292
|
+
exports.contract = contract;
|
|
293
|
+
exports.isContract = isContract;
|
|
294
|
+
exports.isProcedure = isProcedure;
|
|
295
|
+
exports.procedure = procedure;
|
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
import { A as AnySchema, P as ProcedureDef, S as Schema, I as InferSchemaInput, a as ProcedureHandler, b as InferSchemaOutput, c as Procedure, C as ContractDef, d as Contract, e as AnyContract, R as Router, f as ContractRouter } from '../shared/rpc-router.Cu7hweRg.cjs';
|
|
2
|
+
export { h as AnyProcedure, n as ContractProcedure, l as InferRouterInputs, m as InferRouterOutputs, g as ProcedureContext, k as RouterOrProcedure, i as isContract, j as isProcedure } from '../shared/rpc-router.Cu7hweRg.cjs';
|
|
3
|
+
import { RpcBase } from '@cybermp/rpc-core';
|
|
4
|
+
import { RpcContext, RpcApplyType } from '@cybermp/rpc-core/definitions';
|
|
5
|
+
|
|
6
|
+
type OverrideRpcContext<T extends RpcContext, D = any, M extends Record<string, any> = Record<string, any>> = Omit<T, 'data' | 'meta'> & {
|
|
7
|
+
data: D;
|
|
8
|
+
meta: M;
|
|
9
|
+
};
|
|
10
|
+
|
|
11
|
+
interface ProcedureBuilderDef<TCurrentContext extends RpcContext, TInputSchema extends AnySchema, TOutputSchema extends AnySchema, TMethod extends RpcApplyType> extends Omit<ProcedureDef<TCurrentContext, TInputSchema, TOutputSchema, TMethod>, 'handler'> {
|
|
12
|
+
}
|
|
13
|
+
declare class ProcedureBuilder<TCurrentContext extends RpcContext, TInputSchema extends AnySchema, TOutputSchema extends AnySchema, TMethod extends RpcApplyType> {
|
|
14
|
+
'~crpc': ProcedureBuilderDef<TCurrentContext, TInputSchema, TOutputSchema, TMethod>;
|
|
15
|
+
constructor(def: ProcedureBuilderDef<TCurrentContext, TInputSchema, TOutputSchema, TMethod>);
|
|
16
|
+
context<UContext extends TCurrentContext>(): ProcedureBuilder<UContext, TInputSchema, TOutputSchema, TMethod>;
|
|
17
|
+
input<USchema extends AnySchema>(schema: USchema): ProcedureBuilder<TCurrentContext, USchema, TOutputSchema, TMethod>;
|
|
18
|
+
output<USchema extends AnySchema>(schema: USchema): ProcedureBuilder<TCurrentContext, TInputSchema, USchema, TMethod>;
|
|
19
|
+
method<UMethod extends RpcApplyType>(method: UMethod): ProcedureBuilder<TCurrentContext, TInputSchema, UMethod extends RpcApplyType.ON ? Schema<void> : TOutputSchema, UMethod>;
|
|
20
|
+
handler<UFuncOutput extends InferSchemaInput<TOutputSchema>>(...handler: ProcedureHandler<OverrideRpcContext<TCurrentContext, InferSchemaOutput<TInputSchema>>, UFuncOutput>[]): Procedure<TCurrentContext, TInputSchema, TOutputSchema extends {
|
|
21
|
+
initial?: true;
|
|
22
|
+
} ? Schema<UFuncOutput> : TOutputSchema, TMethod>;
|
|
23
|
+
}
|
|
24
|
+
declare const procedure: ProcedureBuilder<RpcContext<any, Record<string, any>>, Schema<unknown, unknown>, Schema<void>, RpcApplyType.ON>;
|
|
25
|
+
|
|
26
|
+
interface ContractBuilderDef<TInputSchema extends AnySchema, TOutputSchema extends AnySchema, TMethod extends RpcApplyType> extends ContractDef<TInputSchema, TOutputSchema, TMethod> {
|
|
27
|
+
}
|
|
28
|
+
declare class ContractBuilder<TCurrentContext extends RpcContext, TInputSchema extends AnySchema, TOutputSchema extends AnySchema, TMethod extends RpcApplyType> {
|
|
29
|
+
'~crpc': ContractBuilderDef<TInputSchema, TOutputSchema, TMethod>;
|
|
30
|
+
constructor(def: ContractBuilderDef<TInputSchema, TOutputSchema, TMethod>);
|
|
31
|
+
input<USchema extends AnySchema>(schema: USchema): ContractBuilder<TCurrentContext, USchema, TOutputSchema, TMethod>;
|
|
32
|
+
output<USchema extends AnySchema>(schema: USchema): ContractBuilder<TCurrentContext, TInputSchema, USchema, TMethod>;
|
|
33
|
+
method<UMethod extends RpcApplyType>(method: UMethod): ContractBuilder<TCurrentContext, TInputSchema, UMethod extends RpcApplyType.ON ? Schema<void> : TOutputSchema, UMethod>;
|
|
34
|
+
build(): Contract<TInputSchema, TOutputSchema, TMethod>;
|
|
35
|
+
implement<UFuncOutput extends InferSchemaInput<TOutputSchema>>(...handler: ProcedureHandler<OverrideRpcContext<TCurrentContext, InferSchemaOutput<TInputSchema>>, UFuncOutput>[]): Procedure<TCurrentContext, TInputSchema, TOutputSchema extends {
|
|
36
|
+
initial?: true;
|
|
37
|
+
} ? Schema<UFuncOutput> : TOutputSchema, TMethod>;
|
|
38
|
+
}
|
|
39
|
+
declare const contract: ContractBuilder<RpcContext<any, Record<string, any>>, Schema<unknown, unknown>, Schema<void> & {
|
|
40
|
+
initial?: true;
|
|
41
|
+
}, RpcApplyType.ON>;
|
|
42
|
+
|
|
43
|
+
type RpcRouterOptions = {
|
|
44
|
+
validation?: boolean | {
|
|
45
|
+
input?: boolean;
|
|
46
|
+
output?: boolean;
|
|
47
|
+
};
|
|
48
|
+
};
|
|
49
|
+
type InferImplementHandler<TContext extends RpcContext, TContract> = TContract extends AnyContract ? ProcedureHandler<OverrideRpcContext<TContext, InferSchemaOutput<TContract['~crpc']['inputSchema']>>, InferSchemaOutput<TContract['~crpc']['outputSchema']>> : TContract extends Record<string, any> ? {
|
|
50
|
+
[K in keyof TContract]: InferImplementHandler<TContext, TContract[K]>[] | InferImplementHandler<TContext, TContract[K]>;
|
|
51
|
+
} : never;
|
|
52
|
+
declare class RpcRouter<Context extends RpcContext> {
|
|
53
|
+
private readonly rpc;
|
|
54
|
+
private readonly options?;
|
|
55
|
+
readonly contract: ContractBuilder<Context, any, Schema<void>, RpcApplyType.ON>;
|
|
56
|
+
readonly procedure: ProcedureBuilder<Context, any, Schema<void>, RpcApplyType.ON>;
|
|
57
|
+
private store;
|
|
58
|
+
private contractProcedureMap;
|
|
59
|
+
constructor(rpc: RpcBase<any>, options?: RpcRouterOptions | undefined);
|
|
60
|
+
private validateSchema;
|
|
61
|
+
private shouldValidate;
|
|
62
|
+
private registerGlobalMiddleware;
|
|
63
|
+
apply(router: Router): void;
|
|
64
|
+
private findNameByProcedureOrContract;
|
|
65
|
+
implement<T extends AnyContract>(contract: T, ...handler: InferImplementHandler<Context, T>[]): void;
|
|
66
|
+
implement<T extends ContractRouter>(contractTree: T, handlerTree: InferImplementHandler<Context, T>): void;
|
|
67
|
+
private implementContract;
|
|
68
|
+
private implementContractTree;
|
|
69
|
+
unimplement(contract: AnyContract): void;
|
|
70
|
+
unimplement(contractTree: ContractRouter): void;
|
|
71
|
+
private unimplementContract;
|
|
72
|
+
private unimplementContractTree;
|
|
73
|
+
private removeProcedure;
|
|
74
|
+
private applyProcedure;
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
export { AnyContract, Contract, ContractBuilder, ContractDef, ContractRouter, Procedure, ProcedureBuilder, ProcedureDef, ProcedureHandler, Router, RpcRouter, contract, procedure };
|
|
78
|
+
export type { ContractBuilderDef, InferImplementHandler, ProcedureBuilderDef, RpcRouterOptions };
|