jsonrpc-client-websocket 1.5.1 → 1.5.3
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/README.md +50 -21
- package/lib/jsonrpc-websocket.d.ts +1 -1
- package/lib/jsonrpc-websocket.js +3 -3
- package/package.json +17 -16
package/README.md
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
[](https://github.com/fabriciobastian/jsonrpc-client-websocket/actions/workflows/build.yml) [](https://choosealicense.com/licenses/mit/) [](https://www.npmjs.com/package/jsonrpc-client-websocket)
|
|
2
2
|
|
|
3
3
|

|
|
4
4
|
|
|
@@ -7,14 +7,14 @@ A simple JSON RPC 2.0 client over websockets
|
|
|
7
7
|
## Open connection
|
|
8
8
|
|
|
9
9
|
```typescript
|
|
10
|
-
const websocketUrl =
|
|
10
|
+
const websocketUrl = 'ws://mywebsocketurl:port';
|
|
11
11
|
const requestTimeoutMs = 2000;
|
|
12
|
-
const websocket = new JsonRpcWebsocket(
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
(error: JsonRpcError) => { /* handle error */ });
|
|
12
|
+
const websocket = new JsonRpcWebsocket(websocketUrl, requestTimeoutMs, (error: JsonRpcError) => {
|
|
13
|
+
/* handle error */
|
|
14
|
+
});
|
|
16
15
|
await websocket.open();
|
|
17
16
|
```
|
|
17
|
+
|
|
18
18
|
Requests that do not receive a response within the specified timeout will fail with a REQUEST_TIMEOUT code.
|
|
19
19
|
The callback (optional) is used for eventual errors, such as receiving a response that does not match any request id and
|
|
20
20
|
connection errors. Furthermore, all errors that are sent to an eventual caller are also reported on the callback, e.g.
|
|
@@ -33,25 +33,25 @@ Considering that the server has a method `sum(a: int, b: int)`
|
|
|
33
33
|
### with positional parameters
|
|
34
34
|
|
|
35
35
|
```typescript
|
|
36
|
-
websocket.call('sum', [1,2])
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
36
|
+
websocket.call('sum', [1, 2])
|
|
37
|
+
.then((response) => {
|
|
38
|
+
// handle response
|
|
39
|
+
})
|
|
40
|
+
.catch((error) => {
|
|
41
|
+
// handle error
|
|
42
|
+
});
|
|
43
43
|
```
|
|
44
44
|
|
|
45
45
|
### with named parameters
|
|
46
46
|
|
|
47
47
|
```typescript
|
|
48
|
-
websocket.call('sum', {b: 1, a: 2})
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
48
|
+
websocket.call('sum', { b: 1, a: 2 })
|
|
49
|
+
.then((response) => {
|
|
50
|
+
// handle response
|
|
51
|
+
})
|
|
52
|
+
.catch((error) => {
|
|
53
|
+
// handle error
|
|
54
|
+
});
|
|
55
55
|
```
|
|
56
56
|
|
|
57
57
|
## Send notification
|
|
@@ -66,12 +66,41 @@ websocket.notify('log', ['a log message']);
|
|
|
66
66
|
|
|
67
67
|
```typescript
|
|
68
68
|
websocket.on('sum', (a: number, b: number) => {
|
|
69
|
-
|
|
69
|
+
return a + b;
|
|
70
70
|
});
|
|
71
71
|
```
|
|
72
|
+
|
|
72
73
|
The defined RPC methods can also be called with both positional and named parameters.
|
|
73
74
|
|
|
75
|
+
### Minification/Obfuscation
|
|
76
|
+
|
|
77
|
+
When using code minification/obfuscation the parameter names may change, which can
|
|
78
|
+
cause calls using named parameters to fail. To mitigate this you can do one or a
|
|
79
|
+
combination of the following:
|
|
80
|
+
- do not minify/obfuscate the code where `on` is used, so the function registration does not get modified
|
|
81
|
+
- use positional parameters
|
|
82
|
+
- register functions as following:
|
|
83
|
+
|
|
84
|
+
```typescript
|
|
85
|
+
websocket.on('sum', (params: { a: number; b: number }) => {
|
|
86
|
+
return params.a + params.b;
|
|
87
|
+
});
|
|
88
|
+
```
|
|
89
|
+
|
|
90
|
+
and then call the function using positional parameters using the object:
|
|
91
|
+
|
|
92
|
+
```typescript
|
|
93
|
+
websocket.call('sum', [{ a: 1, b: 2 }])
|
|
94
|
+
.then((response) => {
|
|
95
|
+
// handle response
|
|
96
|
+
})
|
|
97
|
+
.catch((error) => {
|
|
98
|
+
// handle error
|
|
99
|
+
});
|
|
100
|
+
```
|
|
101
|
+
|
|
74
102
|
## Remove RPC method
|
|
103
|
+
|
|
75
104
|
```typescript
|
|
76
105
|
websocket.off('sum');
|
|
77
106
|
```
|
|
@@ -5,7 +5,7 @@ export declare enum WebsocketReadyStates {
|
|
|
5
5
|
CLOSING = 2,
|
|
6
6
|
CLOSED = 3
|
|
7
7
|
}
|
|
8
|
-
export
|
|
8
|
+
export type ErrorCallback = (error: JsonRpcError) => void;
|
|
9
9
|
export declare class JsonRpcWebsocket {
|
|
10
10
|
private url;
|
|
11
11
|
private requestTimeoutMs;
|
package/lib/jsonrpc-websocket.js
CHANGED
|
@@ -9,6 +9,9 @@ export var WebsocketReadyStates;
|
|
|
9
9
|
WebsocketReadyStates[WebsocketReadyStates["CLOSED"] = 3] = "CLOSED";
|
|
10
10
|
})(WebsocketReadyStates || (WebsocketReadyStates = {}));
|
|
11
11
|
export class JsonRpcWebsocket {
|
|
12
|
+
get state() {
|
|
13
|
+
return this.websocket ? this.websocket.readyState : WebsocketReadyStates.CLOSED;
|
|
14
|
+
}
|
|
12
15
|
constructor(url, requestTimeoutMs, onError) {
|
|
13
16
|
this.url = url;
|
|
14
17
|
this.requestTimeoutMs = requestTimeoutMs;
|
|
@@ -18,9 +21,6 @@ export class JsonRpcWebsocket {
|
|
|
18
21
|
this.pendingRequests = {};
|
|
19
22
|
this.rpcMethods = {};
|
|
20
23
|
}
|
|
21
|
-
get state() {
|
|
22
|
-
return this.websocket ? this.websocket.readyState : WebsocketReadyStates.CLOSED;
|
|
23
|
-
}
|
|
24
24
|
async open() {
|
|
25
25
|
if (this.websocket) {
|
|
26
26
|
await this.close();
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "jsonrpc-client-websocket",
|
|
3
|
-
"version": "1.5.
|
|
3
|
+
"version": "1.5.3",
|
|
4
4
|
"description": "A simple JSON RPC 2.0 websocket client",
|
|
5
5
|
"main": "lib/index.js",
|
|
6
6
|
"types": "lib/index.d.ts",
|
|
@@ -9,7 +9,7 @@
|
|
|
9
9
|
],
|
|
10
10
|
"scripts": {
|
|
11
11
|
"test": "jest",
|
|
12
|
-
"coverage": "jest --config
|
|
12
|
+
"coverage": "jest --config jest.config.ts --coverage",
|
|
13
13
|
"build": "tsc",
|
|
14
14
|
"format": "prettier --write \"src/**/*.ts\"",
|
|
15
15
|
"lint": "tsc --noEmit && eslint . --ext .js,.jsx,.ts,.tsx --fix",
|
|
@@ -38,20 +38,21 @@
|
|
|
38
38
|
"author": "Fabricio Bastian",
|
|
39
39
|
"license": "MIT",
|
|
40
40
|
"devDependencies": {
|
|
41
|
-
"@types/jest": "^
|
|
42
|
-
"@typescript-eslint/eslint-plugin": "^5.
|
|
43
|
-
"@typescript-eslint/parser": "^5.
|
|
44
|
-
"eslint": "^8.
|
|
45
|
-
"eslint-config-prettier": "^8.
|
|
46
|
-
"eslint-plugin-jest": "^
|
|
47
|
-
"jest": "^
|
|
48
|
-
"jest-
|
|
49
|
-
"mock
|
|
50
|
-
"
|
|
51
|
-
"
|
|
52
|
-
"
|
|
53
|
-
"ts-
|
|
54
|
-
"
|
|
41
|
+
"@types/jest": "^29.5.1",
|
|
42
|
+
"@typescript-eslint/eslint-plugin": "^5.59.2",
|
|
43
|
+
"@typescript-eslint/parser": "^5.59.2",
|
|
44
|
+
"eslint": "^8.39.0",
|
|
45
|
+
"eslint-config-prettier": "^8.8.0",
|
|
46
|
+
"eslint-plugin-jest": "^27.2.1",
|
|
47
|
+
"jest": "^29.5.0",
|
|
48
|
+
"jest-environment-jsdom": "^29.5.0",
|
|
49
|
+
"jest-websocket-mock": "^2.4.0",
|
|
50
|
+
"mock-socket": "^9.2.1",
|
|
51
|
+
"npm-check-updates": "^16.10.9",
|
|
52
|
+
"prettier": "^2.8.8",
|
|
53
|
+
"ts-jest": "^29.1.0",
|
|
54
|
+
"ts-node": "^10.9.1",
|
|
55
|
+
"typescript": "^5.0.4"
|
|
55
56
|
},
|
|
56
57
|
"dependencies": {
|
|
57
58
|
"get-parameter-names": "^0.3.0"
|