@trenskow/rpc 0.1.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/LICENSE +9 -0
- package/README.md +57 -0
- package/eslint.config.js +55 -0
- package/index.d.ts +62 -0
- package/index.js +12 -0
- package/lib/index.js +166 -0
- package/package.json +34 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
Copyright 2025 Kristian Trenskow
|
|
2
|
+
|
|
3
|
+
Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
|
|
4
|
+
|
|
5
|
+
1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
|
|
6
|
+
|
|
7
|
+
2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
|
|
8
|
+
|
|
9
|
+
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
package/README.md
ADDED
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
@trenskow/rpc
|
|
2
|
+
----
|
|
3
|
+
|
|
4
|
+
A simple RPC server and client for JavaScript.
|
|
5
|
+
|
|
6
|
+
# Usage
|
|
7
|
+
|
|
8
|
+
Below is an example on how to use.
|
|
9
|
+
|
|
10
|
+
> The transport layer must be provided with a message sender and receiver.
|
|
11
|
+
> In this example we just use an imaginary method called `onDidReceiveMessage(data)` to receive data.
|
|
12
|
+
> Then we use another imaginary method called `sendMessage(data)` to send data.
|
|
13
|
+
|
|
14
|
+
## Server
|
|
15
|
+
|
|
16
|
+
````javascript
|
|
17
|
+
import rpc from '@trenskow/rpc';
|
|
18
|
+
|
|
19
|
+
const server = rpc.serve(
|
|
20
|
+
{
|
|
21
|
+
hello: async (name) => {
|
|
22
|
+
return `Hello, ${name}!.`;
|
|
23
|
+
},
|
|
24
|
+
/* Other methods available. */
|
|
25
|
+
},
|
|
26
|
+
(command, data) => {
|
|
27
|
+
sendMessage({ command, data });
|
|
28
|
+
}
|
|
29
|
+
);
|
|
30
|
+
|
|
31
|
+
onDidReceiveMessage(({ command, data }) => {
|
|
32
|
+
server.onMessage(command, data);
|
|
33
|
+
});
|
|
34
|
+
|
|
35
|
+
````
|
|
36
|
+
|
|
37
|
+
## Client
|
|
38
|
+
|
|
39
|
+
````javascript
|
|
40
|
+
import rpc from '@trenskow/rpc';
|
|
41
|
+
|
|
42
|
+
const client = rpc.connect(
|
|
43
|
+
(command, data) => {
|
|
44
|
+
sendMessage({ command, data })
|
|
45
|
+
}
|
|
46
|
+
);
|
|
47
|
+
|
|
48
|
+
await client.remote.hello('World'); // Returns 'Hello, World!'
|
|
49
|
+
|
|
50
|
+
onDidReceiveMessage({ command, data }) => {
|
|
51
|
+
client.onMessage(command, data);
|
|
52
|
+
});
|
|
53
|
+
````
|
|
54
|
+
|
|
55
|
+
# License
|
|
56
|
+
|
|
57
|
+
See license in LICENSE.
|
package/eslint.config.js
ADDED
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
import globals from 'globals';
|
|
2
|
+
import path from 'node:path';
|
|
3
|
+
import { fileURLToPath } from 'node:url';
|
|
4
|
+
import js from '@eslint/js';
|
|
5
|
+
import { FlatCompat } from '@eslint/eslintrc';
|
|
6
|
+
|
|
7
|
+
const __filename = fileURLToPath(import.meta.url);
|
|
8
|
+
const __dirname = path.dirname(__filename);
|
|
9
|
+
const compat = new FlatCompat({
|
|
10
|
+
baseDirectory: __dirname,
|
|
11
|
+
recommendedConfig: js.configs.recommended,
|
|
12
|
+
allConfig: js.configs.all
|
|
13
|
+
});
|
|
14
|
+
|
|
15
|
+
export default [...compat.extends('eslint:recommended'), {
|
|
16
|
+
languageOptions: {
|
|
17
|
+
globals: {
|
|
18
|
+
...globals.node,
|
|
19
|
+
...globals.mocha,
|
|
20
|
+
},
|
|
21
|
+
|
|
22
|
+
ecmaVersion: 'latest',
|
|
23
|
+
sourceType: 'module',
|
|
24
|
+
},
|
|
25
|
+
|
|
26
|
+
rules: {
|
|
27
|
+
indent: ['error', 'tab', {
|
|
28
|
+
SwitchCase: 1,
|
|
29
|
+
}],
|
|
30
|
+
|
|
31
|
+
'linebreak-style': ['error', 'unix'],
|
|
32
|
+
quotes: ['error', 'single'],
|
|
33
|
+
semi: ['error', 'always'],
|
|
34
|
+
|
|
35
|
+
'no-console': ['error', {
|
|
36
|
+
allow: ['warn', 'error', 'info'],
|
|
37
|
+
}],
|
|
38
|
+
|
|
39
|
+
'no-unused-vars': ['error', {
|
|
40
|
+
argsIgnorePattern: '^_',
|
|
41
|
+
}],
|
|
42
|
+
|
|
43
|
+
'no-empty': ['error', {
|
|
44
|
+
allowEmptyCatch: true,
|
|
45
|
+
}],
|
|
46
|
+
|
|
47
|
+
'no-trailing-spaces': ['error', {
|
|
48
|
+
ignoreComments: true,
|
|
49
|
+
}],
|
|
50
|
+
|
|
51
|
+
'require-atomic-updates': 'off',
|
|
52
|
+
'no-implicit-globals': ['error'],
|
|
53
|
+
'eol-last': ['error', 'always'],
|
|
54
|
+
},
|
|
55
|
+
}];
|
package/index.d.ts
ADDED
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
//
|
|
2
|
+
// index.d.ts
|
|
3
|
+
// 0-bit-games-frontend-vscode
|
|
4
|
+
//
|
|
5
|
+
// Created by Kristian Trenskow on 2025/12/21
|
|
6
|
+
// See license in LICENSE.
|
|
7
|
+
//
|
|
8
|
+
|
|
9
|
+
type MessageSender = (command: string, data: any) => void;
|
|
10
|
+
|
|
11
|
+
export interface RPC<D> {
|
|
12
|
+
|
|
13
|
+
sender: MessageSender;
|
|
14
|
+
|
|
15
|
+
onMessage(
|
|
16
|
+
command: string,
|
|
17
|
+
data: D
|
|
18
|
+
): void;
|
|
19
|
+
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
export class Server<T extends object> extends RPC<any[]> {
|
|
23
|
+
|
|
24
|
+
private readonly local: T;
|
|
25
|
+
|
|
26
|
+
constructor(
|
|
27
|
+
local: T,
|
|
28
|
+
sender?: MessageSender
|
|
29
|
+
)
|
|
30
|
+
|
|
31
|
+
};
|
|
32
|
+
|
|
33
|
+
interface ClientOptions {
|
|
34
|
+
timeout?: number;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
export class Client<T extends object> extends RPC<any> {
|
|
38
|
+
|
|
39
|
+
readonly remote: T;
|
|
40
|
+
|
|
41
|
+
sender: MessageSender;
|
|
42
|
+
|
|
43
|
+
constructor(
|
|
44
|
+
sender?: MessageSender,
|
|
45
|
+
options?: ClientOptions
|
|
46
|
+
)
|
|
47
|
+
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
function serve<T extends object>(
|
|
51
|
+
handler: T,
|
|
52
|
+
sender: MessageSender
|
|
53
|
+
): Server<T>;
|
|
54
|
+
|
|
55
|
+
function connect<T extends object>(
|
|
56
|
+
sender: MessageSender
|
|
57
|
+
): Client<T>;
|
|
58
|
+
|
|
59
|
+
export default {
|
|
60
|
+
serve,
|
|
61
|
+
connect
|
|
62
|
+
};
|
package/index.js
ADDED
package/lib/index.js
ADDED
|
@@ -0,0 +1,166 @@
|
|
|
1
|
+
//
|
|
2
|
+
// index.js
|
|
3
|
+
// 0-bit-games-frontend-vscode
|
|
4
|
+
//
|
|
5
|
+
// Created by Kristian Trenskow on 2025/12/21
|
|
6
|
+
// See license in LICENSE.
|
|
7
|
+
//
|
|
8
|
+
|
|
9
|
+
import { deflate, inflate } from '@trenskow/json-compressor';
|
|
10
|
+
|
|
11
|
+
export class Server {
|
|
12
|
+
|
|
13
|
+
constructor(
|
|
14
|
+
local,
|
|
15
|
+
sender
|
|
16
|
+
) {
|
|
17
|
+
|
|
18
|
+
this.sender = sender;
|
|
19
|
+
|
|
20
|
+
this._local = local;
|
|
21
|
+
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
get local() {
|
|
25
|
+
return this._local;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
onMessage(
|
|
29
|
+
command,
|
|
30
|
+
...data
|
|
31
|
+
) {
|
|
32
|
+
|
|
33
|
+
const parts = command.split(':');
|
|
34
|
+
|
|
35
|
+
if (parts[0] !== 'call') {
|
|
36
|
+
return;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
const identifier = parts[1];
|
|
40
|
+
|
|
41
|
+
const methodName = parts[2];
|
|
42
|
+
|
|
43
|
+
const method = this._local[methodName];
|
|
44
|
+
|
|
45
|
+
if (typeof method !== 'function') {
|
|
46
|
+
console.warn(`RPC: Method ${methodName} is not a function on the handler.`);
|
|
47
|
+
return;
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
Promise.resolve(
|
|
51
|
+
method(...inflate(data))
|
|
52
|
+
).then((result) => {
|
|
53
|
+
this.sender(
|
|
54
|
+
`response:${identifier}`,
|
|
55
|
+
deflate(result)
|
|
56
|
+
);
|
|
57
|
+
}).catch((error) => {
|
|
58
|
+
this.sender(
|
|
59
|
+
`error:${identifier}`,
|
|
60
|
+
deflate(error?.toString() ?? 'Unknown error')
|
|
61
|
+
);
|
|
62
|
+
});
|
|
63
|
+
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
};
|
|
67
|
+
|
|
68
|
+
export class Client {
|
|
69
|
+
|
|
70
|
+
constructor(
|
|
71
|
+
sender,
|
|
72
|
+
options = {}
|
|
73
|
+
) {
|
|
74
|
+
|
|
75
|
+
this.sender = sender;
|
|
76
|
+
|
|
77
|
+
this._responses = new Map();
|
|
78
|
+
this._identifierCounter = 0;
|
|
79
|
+
this._options = options;
|
|
80
|
+
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
get remote() {
|
|
84
|
+
return new Proxy({}, {
|
|
85
|
+
get: (_target, prop) => {
|
|
86
|
+
return (...args) => {
|
|
87
|
+
|
|
88
|
+
const identifier = (this._identifierCounter++).toString();
|
|
89
|
+
|
|
90
|
+
return new Promise((resolve, reject) => {
|
|
91
|
+
|
|
92
|
+
this._responses.set(
|
|
93
|
+
identifier, {
|
|
94
|
+
resolve,
|
|
95
|
+
reject,
|
|
96
|
+
timeout: setTimeout(() => {
|
|
97
|
+
this._responses.delete(identifier);
|
|
98
|
+
reject(new Error(`RPC: Timeout waiting for response for identifier ${identifier}.`));
|
|
99
|
+
}, this._options.timeout ?? 5000)
|
|
100
|
+
});
|
|
101
|
+
|
|
102
|
+
this.sender(
|
|
103
|
+
`call:${identifier}:${String(prop)}`,
|
|
104
|
+
deflate(args)
|
|
105
|
+
);
|
|
106
|
+
|
|
107
|
+
});
|
|
108
|
+
|
|
109
|
+
};
|
|
110
|
+
}
|
|
111
|
+
});
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
onMessage(
|
|
115
|
+
command,
|
|
116
|
+
data
|
|
117
|
+
) {
|
|
118
|
+
|
|
119
|
+
const parts = command.split(':');
|
|
120
|
+
|
|
121
|
+
const type = parts[0];
|
|
122
|
+
const identifier = parts[1];
|
|
123
|
+
|
|
124
|
+
if (type !== 'response' && type !== 'error') {
|
|
125
|
+
return;
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
const responseHandler = this._responses.get(identifier);
|
|
129
|
+
|
|
130
|
+
clearTimeout(responseHandler.timeout);
|
|
131
|
+
|
|
132
|
+
if (!responseHandler) {
|
|
133
|
+
console.warn(`RPC: No response handler found for identifier ${identifier}.`);
|
|
134
|
+
return;
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
this._responses.delete(identifier);
|
|
138
|
+
|
|
139
|
+
data = inflate(data);
|
|
140
|
+
|
|
141
|
+
if (type === 'response') {
|
|
142
|
+
responseHandler.resolve(data);
|
|
143
|
+
} else if (type === 'error') {
|
|
144
|
+
responseHandler.reject(new Error(data));
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
export default {
|
|
152
|
+
serve: function(
|
|
153
|
+
handler,
|
|
154
|
+
sender
|
|
155
|
+
) {
|
|
156
|
+
return new Server(
|
|
157
|
+
handler,
|
|
158
|
+
sender);
|
|
159
|
+
},
|
|
160
|
+
connect: function(
|
|
161
|
+
sender
|
|
162
|
+
) {
|
|
163
|
+
return new Client(
|
|
164
|
+
sender);
|
|
165
|
+
}
|
|
166
|
+
};
|
package/package.json
ADDED
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@trenskow/rpc",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "A simple RPC server and client for JavaScript.",
|
|
5
|
+
"keywords": [
|
|
6
|
+
"rpc",
|
|
7
|
+
"json"
|
|
8
|
+
],
|
|
9
|
+
"homepage": "https://github.com/trenskow/rpc#readme",
|
|
10
|
+
"bugs": {
|
|
11
|
+
"url": "https://github.com/trenskow/rpc/issues"
|
|
12
|
+
},
|
|
13
|
+
"repository": {
|
|
14
|
+
"type": "git",
|
|
15
|
+
"url": "git+https://github.com/trenskow/rpc.git"
|
|
16
|
+
},
|
|
17
|
+
"license": "BSD-2-Clause",
|
|
18
|
+
"author": "Kristian Trenskow <this.is@kristians.email>",
|
|
19
|
+
"type": "module",
|
|
20
|
+
"main": "index.js",
|
|
21
|
+
"types": "index.d.ts",
|
|
22
|
+
"scripts": {
|
|
23
|
+
"test": "echo \"Error: no test specified\" && exit 1"
|
|
24
|
+
},
|
|
25
|
+
"devDependencies": {
|
|
26
|
+
"@eslint/eslintrc": "^3.3.3",
|
|
27
|
+
"@eslint/js": "^9.39.2",
|
|
28
|
+
"eslint": "^9.39.2",
|
|
29
|
+
"globals": "^16.5.0"
|
|
30
|
+
},
|
|
31
|
+
"dependencies": {
|
|
32
|
+
"@trenskow/json-compressor": "^0.1.2"
|
|
33
|
+
}
|
|
34
|
+
}
|