@trenskow/rpc 0.1.3 → 0.1.5

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/lib/index.js CHANGED
@@ -89,15 +89,21 @@ export class Client {
89
89
 
90
90
  return new Promise((resolve, reject) => {
91
91
 
92
+ const response = {
93
+ resolve,
94
+ reject
95
+ };
96
+
97
+ if (typeof this._options?.timeout === 'number') {
98
+ response.timeout = setTimeout(() => {
99
+ this._responses.delete(identifier);
100
+ reject(new Error('RPC: Timeout waiting for response.'));
101
+ }, this._options.timeout ?? 5000);
102
+ }
103
+
92
104
  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
- });
105
+ identifier,
106
+ response);
101
107
 
102
108
  this.sender(
103
109
  `call:${identifier}:${String(prop)}`,
@@ -127,13 +133,14 @@ export class Client {
127
133
 
128
134
  const responseHandler = this._responses.get(identifier);
129
135
 
130
- clearTimeout(responseHandler.timeout);
131
-
132
136
  if (!responseHandler) {
133
- console.warn(`RPC: No response handler found for identifier ${identifier}.`);
134
137
  return;
135
138
  }
136
139
 
140
+ if (responseHandler?.timeout) {
141
+ clearTimeout(responseHandler.timeout);
142
+ }
143
+
137
144
  this._responses.delete(identifier);
138
145
 
139
146
  data = inflate(data);
@@ -158,9 +165,11 @@ export default {
158
165
  sender);
159
166
  },
160
167
  connect: function(
161
- sender
168
+ sender,
169
+ options
162
170
  ) {
163
171
  return new Client(
164
- sender);
172
+ sender,
173
+ options);
165
174
  }
166
175
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@trenskow/rpc",
3
- "version": "0.1.3",
3
+ "version": "0.1.5",
4
4
  "description": "A simple RPC server and client for JavaScript.",
5
5
  "keywords": [
6
6
  "rpc",
@@ -25,13 +25,13 @@
25
25
  "devDependencies": {
26
26
  "@eslint/eslintrc": "^3.3.3",
27
27
  "@eslint/js": "^9.39.2",
28
- "chai": "^6.2.1",
28
+ "chai": "^6.2.2",
29
29
  "chai-as-promised": "^8.0.2",
30
30
  "eslint": "^9.39.2",
31
31
  "globals": "^16.5.0",
32
32
  "mocha": "^11.7.5"
33
33
  },
34
34
  "dependencies": {
35
- "@trenskow/json-compressor": "^0.1.2"
35
+ "@trenskow/json-compressor": "^0.1.3"
36
36
  }
37
37
  }
package/test/index.js CHANGED
@@ -46,6 +46,16 @@ clientTransport.addTransport(serverTransport);
46
46
  const server = rpc.serve({
47
47
  async add(a, b) {
48
48
  return a + b;
49
+ },
50
+ async throwError() {
51
+ throw new Error('This is a test error.');
52
+ },
53
+ timeout() {
54
+ return new Promise((resolve) => {
55
+ setTimeout(() => {
56
+ resolve('done');
57
+ }, 300);
58
+ });
49
59
  }
50
60
  }, (command, data) => {
51
61
  serverTransport.send(command, data);
@@ -53,6 +63,8 @@ const server = rpc.serve({
53
63
 
54
64
  const client = rpc.connect((command, data) => {
55
65
  clientTransport.send(command, data);
66
+ }, {
67
+ timeout: 200
56
68
  });
57
69
 
58
70
  serverTransport.on('message', ({ command, data }) => {
@@ -71,6 +83,14 @@ describe('RPC', () => {
71
83
  return expect(client.remote.add(2, 3)).to.eventually.equal(5);
72
84
  });
73
85
 
86
+ it ('should handle errors from the server', () => {
87
+ return expect(client.remote.throwError()).to.be.rejectedWith('This is a test error.');
88
+ });
89
+
90
+ it ('should handle timeouts', () => {
91
+ return expect(client.remote.timeout()).to.be.rejectedWith('RPC: Timeout waiting for response.');
92
+ });
93
+
74
94
  });
75
95
 
76
96
  });