Skip to content

Commit

Permalink
Merge pull request #27 from stas-ut21/develop
Browse files Browse the repository at this point in the history
4.2.2
  • Loading branch information
stas-ut21 committed May 5, 2021
2 parents 5c6308d + 5dd59b7 commit ca9570e
Show file tree
Hide file tree
Showing 10 changed files with 4,345 additions and 1,222 deletions.
3 changes: 1 addition & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
language: node_js
sudo: false
node_js:
- "15"
- "14"
- "13"
- "12"
- "11"
- "10"
- "9"
- "8"
after_success:
- "npm install coveralls@3 && nyc report --reporter=text-lcov | coveralls"
12 changes: 12 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,15 @@
**May 5, 2021 —** [**Docs**](https://github.com/stas-ut21/ws-await/blob/v4.2.2/README.md)

- Add Typescript definitions!
- Upgrade ws to 7.4.5
- Upgrade eslint to 7.25.0
- Upgrade mocha to 8.3.2
- Default `generateAwaitId` option now use `crypto` module
- Change `package-lock.json` to 2 version
- Fix some JSDOC bugs in `websocket.js`
- Update `.travis.yml` add NodeJS 15
- Update `.travis.yml`: drop NodeJS 8,9

**February 15, 2021 —** [**Docs**](https://github.com/stas-ut21/ws-await/blob/v4.2.1/README.md)

- Upgrade ws to 7.4.3
Expand Down
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ specific message.
[here](https://github.com/websockets/ws/blob/master/README.md). This module only adds new methods and properties
and does not affect the old ones(except Send method - it became asynchronous).

> This module has Typescript definitions!
## Table of Contents

* [Installing](#installing)
Expand Down
2 changes: 1 addition & 1 deletion doc/wsAwait.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ only adds new methods and properties and does not affect the old ones(except `se
This class represents a WebSocket server. It extends the `WebSocket` class. The following describes the add-ons!
Full support for the old api!

### new WebSocket(address[, protocols][, options])
### new WebSocketAwait(address[, protocols][, options])

- `options` {Object}
- `awaitTimeout` {Number} The timeout waiting for a response.
Expand Down
353 changes: 353 additions & 0 deletions index.d.ts

Large diffs are not rendered by default.

6 changes: 3 additions & 3 deletions lib/default-options.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
'use strict';

const crypto = require('crypto');

/**
* Default options
*/
Expand Down Expand Up @@ -31,9 +33,7 @@ module.exports = {
*
* @return {String} is random string
*/
generateAwaitId: () => `_${Math.random()
.toString(36)
.substr(2, 10)}`,
generateAwaitId: () => `_${crypto.randomBytes(16).toString('hex')}`,
/**
* The function to installation identification parameter to sending messages
*
Expand Down
32 changes: 17 additions & 15 deletions lib/websocket.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ class WebSocketAwait extends WebSocket {
* @param {Function} options.attachAwaitId The function to installation identification parameter to sending messages
* @param {Function} options.extractAwaitId The function to extract identification parameter from incoming messages
* @param {Function|Null} options.deleteAwaitId The function to delete identification parameter from incoming messages
* @throws {WebSocketAwaitValidationError} if validation error
* @throws {WebSocketAwait.WebSocketAwaitValidationError} if validation error
*/
static validateOptions(options) {
const schema = {
Expand All @@ -87,7 +87,7 @@ class WebSocketAwait extends WebSocket {
}

if (schemaArray.indexOf(typeof options[option])) {
throw new WebSocketAwaitValidationError(
throw new WebSocketAwait.WebSocketAwaitValidationError(
`The "${option}" argument must be of type ${schema[option]}. ` +
`Received type ${typeof options[option]}`,
);
Expand Down Expand Up @@ -129,28 +129,24 @@ class WebSocketAwait extends WebSocket {
const awaitId = this._options.extractAwaitId(parsedData);

if (awaitId) {
if (!this._options.leaveAwaitId) {
this._options.deleteAwaitId(parsedData);
}

if (this._checkAwaitId(awaitId)) {
const {resolve} = this._getAwaitId(awaitId);

this._deleteAwaitId(awaitId);

if (!this._options.leaveAwaitId) {
delete parsedData.awaitId;
}

resolve(parsedData);
} else {
if (!this._options.leaveAwaitId) {
this._options.deleteAwaitId(parsedData);
}

this.emit('messageAwait', parsedData, awaitId);
}
} else {
this.emit('message', parsedData);
}
} catch (error) {
this.emit('error', new WebSocketAwaitProcessedError(
this.emit('error', new WebSocketAwait.WebSocketAwaitProcessedError(
'The message received but not processed: ' +
`${error.message}, ${data}`,
));
Expand All @@ -161,7 +157,7 @@ class WebSocketAwait extends WebSocket {
for (const item of this._awaitList) {
const {reject} = this._getAwaitId(item[0]);

reject(new WebSocketAwaitConnectionCloseError(
reject(new WebSocketAwait.WebSocketAwaitConnectionCloseError(
'The message is not sent or not response is received: Connection close',
));
this._deleteAwaitId(item[0]);
Expand Down Expand Up @@ -205,7 +201,7 @@ class WebSocketAwait extends WebSocket {
_setAwaitId(awaitId, resolve, reject) {
const timeout = setTimeout(() => {
this._deleteAwaitId(awaitId);
reject(new WebSocketAwaitTimeoutAwaitError(
reject(new WebSocketAwait.WebSocketAwaitTimeoutAwaitError(
'No response is received: Response timeout expired',
));
}, this._options.awaitTimeout);
Expand Down Expand Up @@ -281,7 +277,7 @@ class WebSocketAwait extends WebSocket {
this._deleteAwaitId(awaitId);
}

reject(new WebSocketAwaitSendError(`The message is not sent: ${error.message}`));
reject(new WebSocketAwait.WebSocketAwaitSendError(`The message is not sent: ${error.message}`));
}
});
}
Expand All @@ -306,10 +302,16 @@ class WebSocketAwait extends WebSocket {
await this.send(this._options.attachAwaitId(data, awaitId), options);
resolve(awaitId);
} catch (error) {
reject(new WebSocketAwaitSendError(`The message is not sent: ${error.message}`));
reject(new WebSocketAwait.WebSocketAwaitSendError(`The message is not sent: ${error.message}`));
}
});
}
}

WebSocketAwait.WebSocketAwaitValidationError = WebSocketAwaitValidationError;
WebSocketAwait.WebSocketAwaitConnectionCloseError = WebSocketAwaitConnectionCloseError;
WebSocketAwait.WebSocketAwaitTimeoutAwaitError = WebSocketAwaitTimeoutAwaitError;
WebSocketAwait.WebSocketAwaitSendError = WebSocketAwaitSendError;
WebSocketAwait.WebSocketAwaitProcessedError = WebSocketAwaitProcessedError;

module.exports = WebSocketAwait;
Loading

0 comments on commit ca9570e

Please sign in to comment.