Skip to content
This repository was archived by the owner on Nov 2, 2020. It is now read-only.

Commit 80b0cf1

Browse files
committed
Downgrade multiple listeners error to warning, add secureProtocol back
1 parent 4ec2442 commit 80b0cf1

11 files changed

+21
-24
lines changed

README.md

+3-2
Original file line numberDiff line numberDiff line change
@@ -253,16 +253,17 @@ server.on('upgrade', (request, socket, head) => {
253253
**For more information check typings (`*.d.ts`) files in [dist](https://github.com/ClusterWS/cWS/blob/master/dist) folder**
254254

255255
### Secure WebSocket
256-
You can use `wss://` with `cws` by providing `https` server to `cws`:
256+
You can use `wss://` with `cws` by providing `https` server to `cws` and setting `secureProtocol` on https options:
257257

258258
```js
259259
const { readFileSync } = require('fs');
260260
const { createServer } = require('https');
261-
const { WebSocket } = require('@clusterws/cws');
261+
const { WebSocket, secureProtocol } = require('@clusterws/cws');
262262

263263
const options = {
264264
key: readFileSync(/** path to key */),
265265
cert: readFileSync(/** path to certificate */),
266+
secureProtocol
266267
// ...other Node HTTPS options
267268
};
268269

dist/client.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -52,14 +52,14 @@ class WebSocket {
5252
}
5353
on(event, listener) {
5454
if (this.registeredEvents[event] === undefined) {
55-
console.log(`Attempt to set unsupported event listener '${event}'`);
55+
console.warn(`cWS does not support '${event}' event`);
5656
return;
5757
}
5858
if (typeof listener !== 'function') {
5959
throw new Error(`Listener for '${event}' event must be a function`);
6060
}
6161
if (this.registeredEvents[event] !== shared_1.noop) {
62-
throw new Error(`Can not set listener for '${event}' event twice`);
62+
console.warn(`cWS does not support multiple listeners for the same event. Old listener for '${event}' event will be overwritten`);
6363
}
6464
this.registeredEvents[event] = listener;
6565
}

dist/index.d.ts

+1
Original file line numberDiff line numberDiff line change
@@ -27,3 +27,4 @@ export declare type ServerConfigs = {
2727
};
2828
export { WebSocket } from './client';
2929
export { WebSocketServer } from './server';
30+
export declare const secureProtocol: string;

dist/index.js

+1
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,4 @@ var client_1 = require("./client");
44
exports.WebSocket = client_1.WebSocket;
55
var server_1 = require("./server");
66
exports.WebSocketServer = server_1.WebSocketServer;
7+
exports.secureProtocol = 'TLSv1_2_method';

dist/server.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -76,14 +76,14 @@ class WebSocketServer {
7676
}
7777
on(event, listener) {
7878
if (this.registeredEvents[event] === undefined) {
79-
console.log(`Attempt to set unsupported event listener '${event}'`);
79+
console.warn(`cWS does not support '${event}' event`);
8080
return;
8181
}
8282
if (typeof listener !== 'function') {
8383
throw new Error(`Listener for '${event}' event must be a function`);
8484
}
8585
if (this.registeredEvents[event] !== shared_1.noop) {
86-
throw new Error(`Can not set listener for '${event}' event twice`);
86+
console.warn(`cWS does not support multiple listeners for the same event. Old listener for '${event}' event will be overwritten`);
8787
}
8888
this.registeredEvents[event] = listener;
8989
}

examples/ssl.js

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/// Simple example of using ssl with cws (you main need to adjust imports and key, cert files based on your env)
2-
const { WebSocket } = require('../dist');
32
const { readFileSync } = require('fs');
43
const { createServer } = require('https');
4+
const { WebSocket, secureProtocol } = require('../dist');
55

66
const htmlFile = `
77
<html>
@@ -21,7 +21,8 @@ const htmlFile = `
2121

2222
const options = {
2323
key: readFileSync('./tests/certs/key.pem'),
24-
cert: readFileSync('./tests/certs/certificate.pem')
24+
cert: readFileSync('./tests/certs/certificate.pem'),
25+
secureProtocol
2526
};
2627

2728
const server = createServer(options, (req, res) => {

lib/client.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ export class WebSocket {
7070
public on(event: 'close', listener: (code?: number, reason?: string) => void): void;
7171
public on(event: string, listener: (...args: any[]) => void): void {
7272
if (this.registeredEvents[event] === undefined) {
73-
console.log(`Attempt to set unsupported event listener '${event}'`);
73+
console.warn(`cWS does not support '${event}' event`);
7474
return;
7575
}
7676

@@ -79,7 +79,7 @@ export class WebSocket {
7979
}
8080

8181
if (this.registeredEvents[event] !== noop) {
82-
throw new Error(`Can not set listener for '${event}' event twice`);
82+
console.warn(`cWS does not support multiple listeners for the same event. Old listener for '${event}' event will be overwritten`);
8383
}
8484

8585
this.registeredEvents[event] = listener;

lib/index.ts

+2
Original file line numberDiff line numberDiff line change
@@ -29,3 +29,5 @@ export type ServerConfigs = {
2929

3030
export { WebSocket } from './client';
3131
export { WebSocketServer } from './server';
32+
33+
export const secureProtocol: string = 'TLSv1_2_method';

lib/server.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ export class WebSocketServer {
108108
public on(event: 'connection', listener: (socket: WebSocket) => void): void;
109109
public on(event: string, listener: (...args: any[]) => void): void {
110110
if (this.registeredEvents[event] === undefined) {
111-
console.log(`Attempt to set unsupported event listener '${event}'`);
111+
console.warn(`cWS does not support '${event}' event`);
112112
return;
113113
}
114114

@@ -117,7 +117,7 @@ export class WebSocketServer {
117117
}
118118

119119
if (this.registeredEvents[event] !== noop) {
120-
throw new Error(`Can not set listener for '${event}' event twice`);
120+
console.warn(`cWS does not support multiple listeners for the same event. Old listener for '${event}' event will be overwritten`);
121121
}
122122

123123
this.registeredEvents[event] = listener;

tests/server-client.test.ts

-10
Original file line numberDiff line numberDiff line change
@@ -206,11 +206,6 @@ describe('Server & Client', () => {
206206
});
207207

208208
it('Correctly validate listener', (done: any) => {
209-
expect(() => {
210-
this.wsServer.on('connection', (connection: WebSocket) => { /** */ });
211-
this.wsServer.on('connection', (connection: WebSocket) => { /** */ });
212-
}).to.throw(`Can not set listener for 'connection' event twice`);
213-
214209
expect(() => {
215210
this.wsServer.on('connection', '');
216211
}).to.throw(`Listener for 'connection' event must be a function`);
@@ -229,11 +224,6 @@ describe('Server & Client', () => {
229224
socket.on('open', '');
230225
}).to.throw(`Listener for 'open' event must be a function`);
231226

232-
expect(() => {
233-
const socket: any = new WebSocket('ws://localhost:3000');
234-
socket.on('open', () => { /** */ });
235-
socket.on('open', () => { /** */ });
236-
}).to.throw(`Can not set listener for 'open' event twice`);
237227

238228
expect(() => {
239229
let socket: any = new WebSocket('ws://localhost:3000');

tests/ssl.test.ts

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
11
import { expect } from 'chai';
22
import { readFileSync } from 'fs';
33
import { createServer } from 'https';
4-
import { WebSocket } from '../dist';
4+
import { WebSocket, secureProtocol } from '../dist';
55

66
describe('SSL Server & Client', (): void => {
77
it('Should accept connection correctly and send/receive message', (done: any): void => {
88
const sendMessage: string = 'Hello world';
99
const options: any = {
1010
key: readFileSync('./tests/certs/key.pem'),
11-
cert: readFileSync('./tests/certs/certificate.pem')
11+
cert: readFileSync('./tests/certs/certificate.pem'),
12+
secureProtocol
1213
};
1314

1415
const server: any = createServer(options);

0 commit comments

Comments
 (0)