Skip to content

Commit aad4b64

Browse files
committed
Merge branch 'master' into breaking-changes
2 parents 0bdc3dc + 15f0adb commit aad4b64

File tree

4 files changed

+39
-28
lines changed

4 files changed

+39
-28
lines changed

README.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -249,8 +249,8 @@ await cli.connect();
249249
- `options` {Object}
250250
- `protocols` {Array<String>} - Array of subprotocols supported by this server. Can be overridden in an [auth](#serverauthcallback) callback. Defaults to `[]`.
251251
- `callTimeoutMs` {Number} - Milliseconds to wait before unanswered outbound calls are rejected automatically. Defaults to `30000`.
252-
- `pingIntervalMs` {Number} - Milliseconds between WebSocket pings to connected clients. Defaults to `30000`.
253-
- `deferPingsOnActivity` {Boolean} - Should connected clients skip sending pings if activity received? Defaults to `false`.
252+
- `pingIntervalMs` {Number} - Milliseconds between WebSocket pings to connected clients. Used for keep-alive timeouts. Defaults to `30000`.
253+
- `deferPingsOnActivity` {Boolean} - Should connected clients skip sending keep-alive pings if activity received? Defaults to `false`.
254254
- `respondWithDetailedErrors` {Boolean} - Specifies whether to send detailed errors (including stack trace) to remote party upon an error being thrown by a handler. Defaults to `false`.
255255
- `callConcurrency` {Number} - The number of concurrent in-flight outbound calls permitted at any one time. Additional calls are queued. (There is no limit on inbound calls.) Defaults to `1`.
256256
- `strictMode` {Boolean} - Enable strict validation of calls & responses. Defaults to `false`. (See [Strict Validation](#strict-validation) to understand how this works.)
@@ -388,8 +388,8 @@ Returns a `Promise` which resolves when the server has completed closing.
388388
- `headers` {Object} - Additional HTTP headers to send along with the websocket upgrade request. Defaults to `{}`.
389389
- `query` {Object|String} - An optional query string or object to append as the query string of the connection URL. Defaults to `''`.
390390
- `callTimeoutMs` {Number} - Milliseconds to wait before unanswered outbound calls are rejected automatically. Defaults to `60000`.
391-
- `pingIntervalMs` {Number} - Milliseconds between WebSocket pings. Defaults to `30000`.
392-
- `deferPingsOnActivity` {Boolean} - Should the client skip sending pings if activity received? Defaults to `false`.
391+
- `pingIntervalMs` {Number} - Milliseconds between WebSocket pings. Used for keep-alive timeouts. Defaults to `30000`.
392+
- `deferPingsOnActivity` {Boolean} - Should the client skip sending keep-alive pings if activity received? Defaults to `false`.
393393
- `strictMode` {Boolean} - Enable strict validation of calls & responses. Defaults to `false`. (See [Strict Validation](#strict-validation) to understand how this works.)
394394
- `strictModeValidators` {Array<Validator>} - Optional additional validators to be used in conjunction with `strictMode`. (See [Strict Validation](#adding-additional-validation-schemas) to understand how this works.)
395395
- `respondWithDetailedErrors` {Boolean} - Specifies whether to send detailed errors (including stack trace) to remote party upon an error being thrown by a handler. Defaults to `false`.

lib/server.js

Lines changed: 32 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -110,13 +110,21 @@ class RPCServer extends EventEmitter {
110110
}
111111
};
112112

113+
socket.on('error', (err) => {
114+
abortUpgrade(err);
115+
});
116+
113117
try {
114118
if (this._state !== OPEN) {
115119
throw new WebsocketUpgradeError(500, "Server not open");
116120
}
117121

118-
const headers = request.headers;
122+
if (socket.readyState !== 'open') {
123+
throw new WebsocketUpgradeError(400, `Client readyState = '${socket.readyState}'`);
124+
}
119125

126+
const headers = request.headers;
127+
120128
if (headers.upgrade.toLowerCase() !== 'websocket') {
121129
throw new WebsocketUpgradeError(400, "Can only upgrade websocket upgrade requests");
122130
}
@@ -163,25 +171,32 @@ class RPCServer extends EventEmitter {
163171
const accept = (session, protocol) => {
164172
if (resolved) return;
165173
resolved = true;
174+
175+
try {
176+
if (socket.readyState !== 'open') {
177+
throw new WebsocketUpgradeError(400, `Client readyState = '${socket.readyState}'`);
178+
}
166179

167-
if (protocol === undefined) {
168-
// pick first subprotocol (preferred by server) that is also supported by the client
169-
protocol = (this._options.protocols ?? []).find(p => protocols.has(p));
170-
} else if (protocol !== false && !protocols.has(protocol)) {
171-
abortUpgrade(new WebsocketUpgradeError(400, `Client doesn't support expected subprotocol`));
172-
return;
173-
}
180+
if (protocol === undefined) {
181+
// pick first subprotocol (preferred by server) that is also supported by the client
182+
protocol = (this._options.protocols ?? []).find(p => protocols.has(p));
183+
} else if (protocol !== false && !protocols.has(protocol)) {
184+
throw new WebsocketUpgradeError(400, `Client doesn't support expected subprotocol`);
185+
}
174186

175-
// cache auth results for connection creation
176-
this._pendingUpgrades.set(request, {
177-
session: session ?? {},
178-
protocol,
179-
handshake
180-
});
187+
// cache auth results for connection creation
188+
this._pendingUpgrades.set(request, {
189+
session: session ?? {},
190+
protocol,
191+
handshake
192+
});
181193

182-
this._wss.handleUpgrade(request, socket, head, ws => {
183-
this._wss.emit('connection', ws, request);
184-
});
194+
this._wss.handleUpgrade(request, socket, head, ws => {
195+
this._wss.emit('connection', ws, request);
196+
});
197+
} catch (err) {
198+
abortUpgrade(err);
199+
}
185200
};
186201

187202
const reject = (code = 404, message = 'Not found') => {
@@ -198,10 +213,6 @@ class RPCServer extends EventEmitter {
198213
reject(400, `Client connection closed before upgrade complete`);
199214
});
200215

201-
socket.on('error', (err) => {
202-
abortUpgrade(err);
203-
});
204-
205216
if (this.authCallback) {
206217
await this.authCallback(
207218
accept,

package-lock.json

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "ocpp-rpc",
3-
"version": "1.10.1",
3+
"version": "1.10.2",
44
"description": "A client & server implementation of the WAMP-like RPC-over-websocket system defined in the OCPP protcols (e.g. OCPP1.6-J and OCPP2.0.1).",
55
"main": "index.js",
66
"scripts": {

0 commit comments

Comments
 (0)