@cybermp/rpc-browser 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,118 @@
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 RpcBrowserContext extends definitions.RpcContext {
8
+ }
9
+
10
+ class RpcBrowserDatabus extends rpcCore.RpcDatabus {
11
+ constructor(options) {
12
+ super(options);
13
+ this.setupListeners();
14
+ }
15
+ setupListeners() {
16
+ mp.events.on(this.RPC_INVOKE_EVENT, (packet) => {
17
+ this.onPacketReceived?.(this.deserialize(packet), null);
18
+ });
19
+ mp.events.on(this.RPC_RESPONSE_EVENT, (packet) => {
20
+ this.onPacketReceived?.(this.deserialize(packet), null);
21
+ });
22
+ }
23
+ send(packet) {
24
+ const eventNamesMap = {
25
+ [definitions.RpcPacketType.CALL]: this.RPC_INVOKE_EVENT,
26
+ [definitions.RpcPacketType.TRIGGER]: this.RPC_INVOKE_EVENT,
27
+ [definitions.RpcPacketType.RESPONSE]: this.RPC_RESPONSE_EVENT
28
+ };
29
+ const eventName = eventNamesMap[packet.type];
30
+ if (!eventName) {
31
+ return;
32
+ }
33
+ mp.events.emit(eventName, this.serialize(packet));
34
+ }
35
+ }
36
+
37
+ class RpcBrowser extends rpcCore.RpcBase {
38
+ databus;
39
+ constructor(options = {}) {
40
+ super(options);
41
+ this.databus = new RpcBrowserDatabus(this.options);
42
+ this.init();
43
+ }
44
+ async internalCall(packet, options) {
45
+ return new Promise((resolve, reject) => {
46
+ this.pendingStore.addPending({
47
+ resolve,
48
+ reject,
49
+ packet,
50
+ ...options
51
+ });
52
+ this.databus.send(packet);
53
+ });
54
+ }
55
+ internalTrigger(packet) {
56
+ this.databus.send(packet);
57
+ }
58
+ createContext(packet) {
59
+ return new RpcBrowserContext({
60
+ data: packet.data,
61
+ meta: packet.meta,
62
+ packet
63
+ });
64
+ }
65
+ forward() {
66
+ console.error("Forwarding is not supported in Browser environment");
67
+ }
68
+ callServer(method, ...args) {
69
+ const { data, meta, options = {} } = this.parseCallArgs(args);
70
+ const packet = new definitions.RpcPacket(
71
+ {
72
+ type: definitions.RpcPacketType.CALL,
73
+ method,
74
+ data,
75
+ meta
76
+ },
77
+ { target: definitions.MpEnv.SERVER }
78
+ );
79
+ return this.call(packet, options);
80
+ }
81
+ callClient(method, ...args) {
82
+ const { data, meta, options = {} } = this.parseCallArgs(args);
83
+ const packet = new definitions.RpcPacket(
84
+ {
85
+ type: definitions.RpcPacketType.CALL,
86
+ method,
87
+ data,
88
+ meta
89
+ },
90
+ { target: definitions.MpEnv.CLIENT }
91
+ );
92
+ return this.call(packet, options);
93
+ }
94
+ }
95
+
96
+ exports.RpcOptions = rpcCore.RpcOptions;
97
+ exports.RpcBrowser = RpcBrowser;
98
+ exports.RpcBrowserContext = RpcBrowserContext;
99
+ Object.prototype.hasOwnProperty.call(definitions, '__proto__') &&
100
+ !Object.prototype.hasOwnProperty.call(exports, '__proto__') &&
101
+ Object.defineProperty(exports, '__proto__', {
102
+ enumerable: true,
103
+ value: definitions['__proto__']
104
+ });
105
+
106
+ Object.keys(definitions).forEach(function (k) {
107
+ if (k !== 'default' && !Object.prototype.hasOwnProperty.call(exports, k)) exports[k] = definitions[k];
108
+ });
109
+ Object.prototype.hasOwnProperty.call(utils, '__proto__') &&
110
+ !Object.prototype.hasOwnProperty.call(exports, '__proto__') &&
111
+ Object.defineProperty(exports, '__proto__', {
112
+ enumerable: true,
113
+ value: utils['__proto__']
114
+ });
115
+
116
+ Object.keys(utils).forEach(function (k) {
117
+ if (k !== 'default' && !Object.prototype.hasOwnProperty.call(exports, k)) exports[k] = utils[k];
118
+ });
@@ -0,0 +1,29 @@
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 RpcBrowserContext<D = any, M extends Record<string, any> = Record<string, any>> extends RpcContext<D, M> {
8
+ }
9
+
10
+ declare class RpcBrowserDatabus extends RpcDatabus {
11
+ constructor(options: RpcOptions);
12
+ private setupListeners;
13
+ send(packet: RpcPacket): void;
14
+ }
15
+
16
+ declare class RpcBrowser extends RpcBase<RpcBrowserContext> {
17
+ protected databus: RpcBrowserDatabus;
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): RpcBrowserContext;
22
+ protected forward(): 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
+ callClient<R = any>(method: string, options?: CallConfig): Promise<R>;
26
+ callClient<R = any>(method: string, data?: any, meta?: Record<string, any>, options?: CallOptions): Promise<R>;
27
+ }
28
+
29
+ export { RpcBrowser, RpcBrowserContext };
@@ -0,0 +1,29 @@
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 RpcBrowserContext<D = any, M extends Record<string, any> = Record<string, any>> extends RpcContext<D, M> {
8
+ }
9
+
10
+ declare class RpcBrowserDatabus extends RpcDatabus {
11
+ constructor(options: RpcOptions);
12
+ private setupListeners;
13
+ send(packet: RpcPacket): void;
14
+ }
15
+
16
+ declare class RpcBrowser extends RpcBase<RpcBrowserContext> {
17
+ protected databus: RpcBrowserDatabus;
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): RpcBrowserContext;
22
+ protected forward(): 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
+ callClient<R = any>(method: string, options?: CallConfig): Promise<R>;
26
+ callClient<R = any>(method: string, data?: any, meta?: Record<string, any>, options?: CallOptions): Promise<R>;
27
+ }
28
+
29
+ export { RpcBrowser, RpcBrowserContext };
@@ -0,0 +1,29 @@
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 RpcBrowserContext<D = any, M extends Record<string, any> = Record<string, any>> extends RpcContext<D, M> {
8
+ }
9
+
10
+ declare class RpcBrowserDatabus extends RpcDatabus {
11
+ constructor(options: RpcOptions);
12
+ private setupListeners;
13
+ send(packet: RpcPacket): void;
14
+ }
15
+
16
+ declare class RpcBrowser extends RpcBase<RpcBrowserContext> {
17
+ protected databus: RpcBrowserDatabus;
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): RpcBrowserContext;
22
+ protected forward(): 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
+ callClient<R = any>(method: string, options?: CallConfig): Promise<R>;
26
+ callClient<R = any>(method: string, data?: any, meta?: Record<string, any>, options?: CallOptions): Promise<R>;
27
+ }
28
+
29
+ export { RpcBrowser, RpcBrowserContext };
package/dist/index.mjs ADDED
@@ -0,0 +1,96 @@
1
+ import { RpcDatabus, RpcBase } from '@cybermp/rpc-core';
2
+ export { RpcOptions } from '@cybermp/rpc-core';
3
+ import { RpcContext, RpcPacketType, RpcPacket, MpEnv } from '@cybermp/rpc-core/definitions';
4
+ export * from '@cybermp/rpc-core/definitions';
5
+ export * from '@cybermp/rpc-core/utils';
6
+
7
+ class RpcBrowserContext extends RpcContext {
8
+ }
9
+
10
+ class RpcBrowserDatabus extends RpcDatabus {
11
+ constructor(options) {
12
+ super(options);
13
+ this.setupListeners();
14
+ }
15
+ setupListeners() {
16
+ mp.events.on(this.RPC_INVOKE_EVENT, (packet) => {
17
+ this.onPacketReceived?.(this.deserialize(packet), null);
18
+ });
19
+ mp.events.on(this.RPC_RESPONSE_EVENT, (packet) => {
20
+ this.onPacketReceived?.(this.deserialize(packet), null);
21
+ });
22
+ }
23
+ send(packet) {
24
+ const eventNamesMap = {
25
+ [RpcPacketType.CALL]: this.RPC_INVOKE_EVENT,
26
+ [RpcPacketType.TRIGGER]: this.RPC_INVOKE_EVENT,
27
+ [RpcPacketType.RESPONSE]: this.RPC_RESPONSE_EVENT
28
+ };
29
+ const eventName = eventNamesMap[packet.type];
30
+ if (!eventName) {
31
+ return;
32
+ }
33
+ mp.events.emit(eventName, this.serialize(packet));
34
+ }
35
+ }
36
+
37
+ class RpcBrowser extends RpcBase {
38
+ databus;
39
+ constructor(options = {}) {
40
+ super(options);
41
+ this.databus = new RpcBrowserDatabus(this.options);
42
+ this.init();
43
+ }
44
+ async internalCall(packet, options) {
45
+ return new Promise((resolve, reject) => {
46
+ this.pendingStore.addPending({
47
+ resolve,
48
+ reject,
49
+ packet,
50
+ ...options
51
+ });
52
+ this.databus.send(packet);
53
+ });
54
+ }
55
+ internalTrigger(packet) {
56
+ this.databus.send(packet);
57
+ }
58
+ createContext(packet) {
59
+ return new RpcBrowserContext({
60
+ data: packet.data,
61
+ meta: packet.meta,
62
+ packet
63
+ });
64
+ }
65
+ forward() {
66
+ console.error("Forwarding is not supported in Browser environment");
67
+ }
68
+ callServer(method, ...args) {
69
+ const { data, meta, options = {} } = this.parseCallArgs(args);
70
+ const packet = new RpcPacket(
71
+ {
72
+ type: RpcPacketType.CALL,
73
+ method,
74
+ data,
75
+ meta
76
+ },
77
+ { target: MpEnv.SERVER }
78
+ );
79
+ return this.call(packet, options);
80
+ }
81
+ callClient(method, ...args) {
82
+ const { data, meta, options = {} } = this.parseCallArgs(args);
83
+ const packet = new RpcPacket(
84
+ {
85
+ type: RpcPacketType.CALL,
86
+ method,
87
+ data,
88
+ meta
89
+ },
90
+ { target: MpEnv.CLIENT }
91
+ );
92
+ return this.call(packet, options);
93
+ }
94
+ }
95
+
96
+ export { RpcBrowser, RpcBrowserContext };
package/package.json ADDED
@@ -0,0 +1,33 @@
1
+ {
2
+ "name": "@cybermp/rpc-browser",
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/browser-types": "^1.0.0"
27
+ },
28
+ "scripts": {
29
+ "dev": "pnpm run build --watch",
30
+ "build": "unbuild",
31
+ "type:check": "tsc -b"
32
+ }
33
+ }