@cybermp/rpc-client 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 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.
package/dist/index.cjs ADDED
@@ -0,0 +1,157 @@
1
+ 'use strict';
2
+
3
+ const rpcCore = require('@cybermp/rpc-core');
4
+ const definitions = require('@cybermp/rpc-core/definitions');
5
+ const utils = require('@cybermp/rpc-core/utils');
6
+
7
+ class RpcClientContext extends definitions.RpcContext {
8
+ }
9
+
10
+ class RpcClientDatabus extends rpcCore.RpcDatabus {
11
+ constructor(options) {
12
+ super(options);
13
+ this.setupListeners();
14
+ }
15
+ setupListeners() {
16
+ mp.events.onCef(this.RPC_INVOKE_EVENT, (packet) => {
17
+ this.onPacketReceived?.(this.deserialize(packet), null);
18
+ });
19
+ mp.events.onCef(this.RPC_RESPONSE_EVENT, (packet) => {
20
+ this.onPacketReceived?.(this.deserialize(packet), null);
21
+ });
22
+ mp.events.onServer(this.RPC_INVOKE_EVENT, (packet) => {
23
+ this.onPacketReceived?.(this.deserialize(packet), null);
24
+ });
25
+ mp.events.onServer(this.RPC_RESPONSE_EVENT, (packet) => {
26
+ this.onPacketReceived?.(this.deserialize(packet), null);
27
+ });
28
+ }
29
+ send(packet) {
30
+ const eventNamesMap = {
31
+ [definitions.RpcPacketType.CALL]: this.RPC_INVOKE_EVENT,
32
+ [definitions.RpcPacketType.TRIGGER]: this.RPC_INVOKE_EVENT,
33
+ [definitions.RpcPacketType.RESPONSE]: this.RPC_RESPONSE_EVENT
34
+ };
35
+ const eventName = eventNamesMap[packet.type];
36
+ if (!eventName) {
37
+ return;
38
+ }
39
+ if (packet.env.target === definitions.MpEnv.SERVER) {
40
+ mp.events.emitServer(eventName, this.serialize(packet));
41
+ } else if (packet.env.target === definitions.MpEnv.BROWSER) {
42
+ mp.events.emitCef(eventName, this.serialize(packet));
43
+ }
44
+ }
45
+ }
46
+
47
+ class RpcClient extends rpcCore.RpcBase {
48
+ databus;
49
+ constructor(options = {}) {
50
+ super(options);
51
+ this.databus = new RpcClientDatabus(this.options);
52
+ this.init();
53
+ }
54
+ async internalCall(packet, options) {
55
+ return new Promise((resolve, reject) => {
56
+ this.pendingStore.addPending({
57
+ resolve,
58
+ reject,
59
+ packet,
60
+ ...options
61
+ });
62
+ this.databus.send(packet);
63
+ });
64
+ }
65
+ internalTrigger(packet) {
66
+ this.databus.send(packet);
67
+ }
68
+ createContext(packet) {
69
+ return new RpcClientContext({
70
+ data: packet.data,
71
+ meta: packet.meta,
72
+ packet
73
+ });
74
+ }
75
+ forward(packet) {
76
+ const fromServerToBrowser = packet.env.source === definitions.MpEnv.SERVER && packet.env.target === definitions.MpEnv.BROWSER;
77
+ const fromBrowserToServer = packet.env.source === definitions.MpEnv.BROWSER && packet.env.target === definitions.MpEnv.SERVER;
78
+ if (!fromServerToBrowser && !fromBrowserToServer) {
79
+ return;
80
+ }
81
+ this.databus.send(packet);
82
+ }
83
+ callServer(method, ...args) {
84
+ const { data, meta, options = {} } = this.parseCallArgs(args);
85
+ const packet = new definitions.RpcPacket(
86
+ {
87
+ type: definitions.RpcPacketType.CALL,
88
+ method,
89
+ data,
90
+ meta
91
+ },
92
+ { target: definitions.MpEnv.SERVER }
93
+ );
94
+ return this.call(packet, options);
95
+ }
96
+ triggerServer(method, data, meta) {
97
+ const packet = new definitions.RpcPacket(
98
+ {
99
+ type: definitions.RpcPacketType.TRIGGER,
100
+ method,
101
+ data,
102
+ meta
103
+ },
104
+ { target: definitions.MpEnv.SERVER }
105
+ );
106
+ this.trigger(packet);
107
+ }
108
+ callBrowser(method, ...args) {
109
+ const { data, meta, options = {} } = this.parseCallArgs(args);
110
+ const packet = new definitions.RpcPacket(
111
+ {
112
+ type: definitions.RpcPacketType.CALL,
113
+ method,
114
+ data,
115
+ meta
116
+ },
117
+ { target: definitions.MpEnv.BROWSER }
118
+ );
119
+ return this.call(packet, options);
120
+ }
121
+ triggerBrowser(method, data, meta) {
122
+ const packet = new definitions.RpcPacket(
123
+ {
124
+ type: definitions.RpcPacketType.TRIGGER,
125
+ method,
126
+ data,
127
+ meta
128
+ },
129
+ { target: definitions.MpEnv.BROWSER }
130
+ );
131
+ this.trigger(packet);
132
+ }
133
+ }
134
+
135
+ exports.RpcOptions = rpcCore.RpcOptions;
136
+ exports.RpcClient = RpcClient;
137
+ exports.RpcClientContext = RpcClientContext;
138
+ Object.prototype.hasOwnProperty.call(definitions, '__proto__') &&
139
+ !Object.prototype.hasOwnProperty.call(exports, '__proto__') &&
140
+ Object.defineProperty(exports, '__proto__', {
141
+ enumerable: true,
142
+ value: definitions['__proto__']
143
+ });
144
+
145
+ Object.keys(definitions).forEach(function (k) {
146
+ if (k !== 'default' && !Object.prototype.hasOwnProperty.call(exports, k)) exports[k] = definitions[k];
147
+ });
148
+ Object.prototype.hasOwnProperty.call(utils, '__proto__') &&
149
+ !Object.prototype.hasOwnProperty.call(exports, '__proto__') &&
150
+ Object.defineProperty(exports, '__proto__', {
151
+ enumerable: true,
152
+ value: utils['__proto__']
153
+ });
154
+
155
+ Object.keys(utils).forEach(function (k) {
156
+ if (k !== 'default' && !Object.prototype.hasOwnProperty.call(exports, k)) exports[k] = utils[k];
157
+ });
@@ -0,0 +1,31 @@
1
+ import { RpcDatabus, RpcOptions, RpcBase, CallOptions, CallConfig } from '@cybermp/rpc-core';
2
+ export { RpcOptions } from '@cybermp/rpc-core';
3
+ import { RpcContext, RpcPacket } from '@cybermp/rpc-core/definitions';
4
+ export * from '@cybermp/rpc-core/definitions';
5
+ export * from '@cybermp/rpc-core/utils';
6
+
7
+ declare class RpcClientContext<D = any, M extends Record<string, any> = Record<string, any>> extends RpcContext<D, M> {
8
+ }
9
+
10
+ declare class RpcClientDatabus extends RpcDatabus {
11
+ constructor(options: RpcOptions);
12
+ private setupListeners;
13
+ send(packet: RpcPacket): void;
14
+ }
15
+
16
+ declare class RpcClient extends RpcBase<RpcClientContext> {
17
+ protected databus: RpcClientDatabus;
18
+ constructor(options?: Partial<RpcOptions>);
19
+ protected internalCall<R>(packet: RpcPacket, options: CallOptions): Promise<R>;
20
+ protected internalTrigger(packet: RpcPacket): void;
21
+ protected createContext(packet: RpcPacket): RpcClientContext;
22
+ protected forward(packet: RpcPacket): void;
23
+ callServer<R = any>(method: string, options?: CallConfig): Promise<R>;
24
+ callServer<R = any>(method: string, data?: any, meta?: Record<string, any>, options?: CallOptions): Promise<R>;
25
+ triggerServer(method: string, data?: any, meta?: Record<string, any>): void;
26
+ callBrowser<R = any>(method: string, options?: CallConfig): Promise<R>;
27
+ callBrowser<R = any>(method: string, data?: any, meta?: Record<string, any>, options?: CallOptions): Promise<R>;
28
+ triggerBrowser(method: string, data?: any, meta?: Record<string, any>): void;
29
+ }
30
+
31
+ export { RpcClient, RpcClientContext };
@@ -0,0 +1,31 @@
1
+ import { RpcDatabus, RpcOptions, RpcBase, CallOptions, CallConfig } from '@cybermp/rpc-core';
2
+ export { RpcOptions } from '@cybermp/rpc-core';
3
+ import { RpcContext, RpcPacket } from '@cybermp/rpc-core/definitions';
4
+ export * from '@cybermp/rpc-core/definitions';
5
+ export * from '@cybermp/rpc-core/utils';
6
+
7
+ declare class RpcClientContext<D = any, M extends Record<string, any> = Record<string, any>> extends RpcContext<D, M> {
8
+ }
9
+
10
+ declare class RpcClientDatabus extends RpcDatabus {
11
+ constructor(options: RpcOptions);
12
+ private setupListeners;
13
+ send(packet: RpcPacket): void;
14
+ }
15
+
16
+ declare class RpcClient extends RpcBase<RpcClientContext> {
17
+ protected databus: RpcClientDatabus;
18
+ constructor(options?: Partial<RpcOptions>);
19
+ protected internalCall<R>(packet: RpcPacket, options: CallOptions): Promise<R>;
20
+ protected internalTrigger(packet: RpcPacket): void;
21
+ protected createContext(packet: RpcPacket): RpcClientContext;
22
+ protected forward(packet: RpcPacket): void;
23
+ callServer<R = any>(method: string, options?: CallConfig): Promise<R>;
24
+ callServer<R = any>(method: string, data?: any, meta?: Record<string, any>, options?: CallOptions): Promise<R>;
25
+ triggerServer(method: string, data?: any, meta?: Record<string, any>): void;
26
+ callBrowser<R = any>(method: string, options?: CallConfig): Promise<R>;
27
+ callBrowser<R = any>(method: string, data?: any, meta?: Record<string, any>, options?: CallOptions): Promise<R>;
28
+ triggerBrowser(method: string, data?: any, meta?: Record<string, any>): void;
29
+ }
30
+
31
+ export { RpcClient, RpcClientContext };
@@ -0,0 +1,31 @@
1
+ import { RpcDatabus, RpcOptions, RpcBase, CallOptions, CallConfig } from '@cybermp/rpc-core';
2
+ export { RpcOptions } from '@cybermp/rpc-core';
3
+ import { RpcContext, RpcPacket } from '@cybermp/rpc-core/definitions';
4
+ export * from '@cybermp/rpc-core/definitions';
5
+ export * from '@cybermp/rpc-core/utils';
6
+
7
+ declare class RpcClientContext<D = any, M extends Record<string, any> = Record<string, any>> extends RpcContext<D, M> {
8
+ }
9
+
10
+ declare class RpcClientDatabus extends RpcDatabus {
11
+ constructor(options: RpcOptions);
12
+ private setupListeners;
13
+ send(packet: RpcPacket): void;
14
+ }
15
+
16
+ declare class RpcClient extends RpcBase<RpcClientContext> {
17
+ protected databus: RpcClientDatabus;
18
+ constructor(options?: Partial<RpcOptions>);
19
+ protected internalCall<R>(packet: RpcPacket, options: CallOptions): Promise<R>;
20
+ protected internalTrigger(packet: RpcPacket): void;
21
+ protected createContext(packet: RpcPacket): RpcClientContext;
22
+ protected forward(packet: RpcPacket): void;
23
+ callServer<R = any>(method: string, options?: CallConfig): Promise<R>;
24
+ callServer<R = any>(method: string, data?: any, meta?: Record<string, any>, options?: CallOptions): Promise<R>;
25
+ triggerServer(method: string, data?: any, meta?: Record<string, any>): void;
26
+ callBrowser<R = any>(method: string, options?: CallConfig): Promise<R>;
27
+ callBrowser<R = any>(method: string, data?: any, meta?: Record<string, any>, options?: CallOptions): Promise<R>;
28
+ triggerBrowser(method: string, data?: any, meta?: Record<string, any>): void;
29
+ }
30
+
31
+ export { RpcClient, RpcClientContext };
package/dist/index.mjs ADDED
@@ -0,0 +1,135 @@
1
+ import { RpcDatabus, RpcBase } from '@cybermp/rpc-core';
2
+ export { RpcOptions } from '@cybermp/rpc-core';
3
+ import { RpcContext, RpcPacketType, MpEnv, RpcPacket } from '@cybermp/rpc-core/definitions';
4
+ export * from '@cybermp/rpc-core/definitions';
5
+ export * from '@cybermp/rpc-core/utils';
6
+
7
+ class RpcClientContext extends RpcContext {
8
+ }
9
+
10
+ class RpcClientDatabus extends RpcDatabus {
11
+ constructor(options) {
12
+ super(options);
13
+ this.setupListeners();
14
+ }
15
+ setupListeners() {
16
+ mp.events.onCef(this.RPC_INVOKE_EVENT, (packet) => {
17
+ this.onPacketReceived?.(this.deserialize(packet), null);
18
+ });
19
+ mp.events.onCef(this.RPC_RESPONSE_EVENT, (packet) => {
20
+ this.onPacketReceived?.(this.deserialize(packet), null);
21
+ });
22
+ mp.events.onServer(this.RPC_INVOKE_EVENT, (packet) => {
23
+ this.onPacketReceived?.(this.deserialize(packet), null);
24
+ });
25
+ mp.events.onServer(this.RPC_RESPONSE_EVENT, (packet) => {
26
+ this.onPacketReceived?.(this.deserialize(packet), null);
27
+ });
28
+ }
29
+ send(packet) {
30
+ const eventNamesMap = {
31
+ [RpcPacketType.CALL]: this.RPC_INVOKE_EVENT,
32
+ [RpcPacketType.TRIGGER]: this.RPC_INVOKE_EVENT,
33
+ [RpcPacketType.RESPONSE]: this.RPC_RESPONSE_EVENT
34
+ };
35
+ const eventName = eventNamesMap[packet.type];
36
+ if (!eventName) {
37
+ return;
38
+ }
39
+ if (packet.env.target === MpEnv.SERVER) {
40
+ mp.events.emitServer(eventName, this.serialize(packet));
41
+ } else if (packet.env.target === MpEnv.BROWSER) {
42
+ mp.events.emitCef(eventName, this.serialize(packet));
43
+ }
44
+ }
45
+ }
46
+
47
+ class RpcClient extends RpcBase {
48
+ databus;
49
+ constructor(options = {}) {
50
+ super(options);
51
+ this.databus = new RpcClientDatabus(this.options);
52
+ this.init();
53
+ }
54
+ async internalCall(packet, options) {
55
+ return new Promise((resolve, reject) => {
56
+ this.pendingStore.addPending({
57
+ resolve,
58
+ reject,
59
+ packet,
60
+ ...options
61
+ });
62
+ this.databus.send(packet);
63
+ });
64
+ }
65
+ internalTrigger(packet) {
66
+ this.databus.send(packet);
67
+ }
68
+ createContext(packet) {
69
+ return new RpcClientContext({
70
+ data: packet.data,
71
+ meta: packet.meta,
72
+ packet
73
+ });
74
+ }
75
+ forward(packet) {
76
+ const fromServerToBrowser = packet.env.source === MpEnv.SERVER && packet.env.target === MpEnv.BROWSER;
77
+ const fromBrowserToServer = packet.env.source === MpEnv.BROWSER && packet.env.target === MpEnv.SERVER;
78
+ if (!fromServerToBrowser && !fromBrowserToServer) {
79
+ return;
80
+ }
81
+ this.databus.send(packet);
82
+ }
83
+ callServer(method, ...args) {
84
+ const { data, meta, options = {} } = this.parseCallArgs(args);
85
+ const packet = new RpcPacket(
86
+ {
87
+ type: RpcPacketType.CALL,
88
+ method,
89
+ data,
90
+ meta
91
+ },
92
+ { target: MpEnv.SERVER }
93
+ );
94
+ return this.call(packet, options);
95
+ }
96
+ triggerServer(method, data, meta) {
97
+ const packet = new RpcPacket(
98
+ {
99
+ type: RpcPacketType.TRIGGER,
100
+ method,
101
+ data,
102
+ meta
103
+ },
104
+ { target: MpEnv.SERVER }
105
+ );
106
+ this.trigger(packet);
107
+ }
108
+ callBrowser(method, ...args) {
109
+ const { data, meta, options = {} } = this.parseCallArgs(args);
110
+ const packet = new RpcPacket(
111
+ {
112
+ type: RpcPacketType.CALL,
113
+ method,
114
+ data,
115
+ meta
116
+ },
117
+ { target: MpEnv.BROWSER }
118
+ );
119
+ return this.call(packet, options);
120
+ }
121
+ triggerBrowser(method, data, meta) {
122
+ const packet = new RpcPacket(
123
+ {
124
+ type: RpcPacketType.TRIGGER,
125
+ method,
126
+ data,
127
+ meta
128
+ },
129
+ { target: MpEnv.BROWSER }
130
+ );
131
+ this.trigger(packet);
132
+ }
133
+ }
134
+
135
+ export { RpcClient, RpcClientContext };
package/package.json ADDED
@@ -0,0 +1,33 @@
1
+ {
2
+ "name": "@cybermp/rpc-client",
3
+ "version": "0.1.0-rc.1",
4
+ "keywords": [],
5
+ "license": "MIT",
6
+ "files": [
7
+ "dist"
8
+ ],
9
+ "exports": {
10
+ ".": {
11
+ "types": "./dist/index.d.ts",
12
+ "require": "./dist/index.js",
13
+ "import": "./dist/index.mjs"
14
+ }
15
+ },
16
+ "dependencies": {
17
+ "@cybermp/rpc-core": "0.1.0-rc.1"
18
+ },
19
+ "unbuild": {
20
+ "failOnWarn": false
21
+ },
22
+ "devDependencies": {
23
+ "@types/node": "^24.2.1"
24
+ },
25
+ "peerDependencies": {
26
+ "@cybermp/client-types": "^1.6.11"
27
+ },
28
+ "scripts": {
29
+ "dev": "pnpm run build --watch",
30
+ "build": "unbuild",
31
+ "type:check": "tsc -b"
32
+ }
33
+ }