@@ -14,29 +14,30 @@ class Server extends EventEmitter {
14
14
* @param {Object } options
15
15
* @api public
16
16
*/
17
- constructor ( opts ) {
17
+ constructor ( opts = { } ) {
18
18
super ( ) ;
19
19
20
20
this . clients = { } ;
21
21
this . clientsCount = 0 ;
22
22
23
- opts = opts || { } ;
24
-
25
- this . wsEngine = opts . wsEngine || process . env . EIO_WS_ENGINE || "ws" ;
26
- this . pingTimeout = opts . pingTimeout || 5000 ;
27
- this . pingInterval = opts . pingInterval || 25000 ;
28
- this . upgradeTimeout = opts . upgradeTimeout || 10000 ;
29
- this . maxHttpBufferSize = opts . maxHttpBufferSize || 10e7 ;
30
- this . transports = opts . transports || Object . keys ( transports ) ;
31
- this . allowUpgrades = false !== opts . allowUpgrades ;
32
- this . allowRequest = opts . allowRequest ;
33
- this . perMessageDeflate =
34
- false !== opts . perMessageDeflate ? opts . perMessageDeflate || true : false ;
35
- this . httpCompression =
36
- false !== opts . httpCompression ? opts . httpCompression || { } : false ;
37
- this . initialPacket = opts . initialPacket ;
38
-
39
- this . opts = Object . assign ( { } , opts ) ;
23
+ this . opts = Object . assign (
24
+ {
25
+ wsEngine : process . env . EIO_WS_ENGINE || "ws" ,
26
+ pingTimeout : 5000 ,
27
+ pingInterval : 25000 ,
28
+ upgradeTimeout : 10000 ,
29
+ maxHttpBufferSize : 10e7 ,
30
+ transports : Object . keys ( transports ) ,
31
+ allowUpgrades : true ,
32
+ perMessageDeflate : {
33
+ threshold : 1024
34
+ } ,
35
+ httpCompression : {
36
+ threshold : 1024
37
+ }
38
+ } ,
39
+ opts
40
+ ) ;
40
41
41
42
if ( opts . cookie ) {
42
43
this . opts . cookie = Object . assign (
@@ -50,15 +51,6 @@ class Server extends EventEmitter {
50
51
) ;
51
52
}
52
53
53
- // initialize compression options
54
- [ "perMessageDeflate" , "httpCompression" ] . forEach ( type => {
55
- let compression = this [ type ] ;
56
- if ( true === compression ) this [ type ] = compression = { } ;
57
- if ( compression && null == compression . threshold ) {
58
- compression . threshold = 1024 ;
59
- }
60
- } ) ;
61
-
62
54
this . init ( ) ;
63
55
}
64
56
@@ -68,12 +60,12 @@ class Server extends EventEmitter {
68
60
* @api private
69
61
*/
70
62
init ( ) {
71
- if ( ! ~ this . transports . indexOf ( "websocket" ) ) return ;
63
+ if ( ! ~ this . opts . transports . indexOf ( "websocket" ) ) return ;
72
64
73
65
if ( this . ws ) this . ws . close ( ) ;
74
66
75
67
let wsModule ;
76
- switch ( this . wsEngine ) {
68
+ switch ( this . opts . wsEngine ) {
77
69
case "uws" :
78
70
wsModule = require ( "uws" ) ;
79
71
break ;
@@ -86,8 +78,8 @@ class Server extends EventEmitter {
86
78
this . ws = new wsModule . Server ( {
87
79
noServer : true ,
88
80
clientTracking : false ,
89
- perMessageDeflate : this . perMessageDeflate ,
90
- maxPayload : this . maxHttpBufferSize
81
+ perMessageDeflate : this . opts . perMessageDeflate ,
82
+ maxPayload : this . opts . maxHttpBufferSize
91
83
} ) ;
92
84
}
93
85
@@ -98,7 +90,7 @@ class Server extends EventEmitter {
98
90
* @api public
99
91
*/
100
92
upgrades ( transport ) {
101
- if ( ! this . allowUpgrades ) return [ ] ;
93
+ if ( ! this . opts . allowUpgrades ) return [ ] ;
102
94
return transports [ transport ] . upgradesTo || [ ] ;
103
95
}
104
96
@@ -112,7 +104,7 @@ class Server extends EventEmitter {
112
104
verify ( req , upgrade , fn ) {
113
105
// transport check
114
106
const transport = req . _query . transport ;
115
- if ( ! ~ this . transports . indexOf ( transport ) ) {
107
+ if ( ! ~ this . opts . transports . indexOf ( transport ) ) {
116
108
debug ( 'unknown transport "%s"' , transport ) ;
117
109
return fn ( Server . errors . UNKNOWN_TRANSPORT , false ) ;
118
110
}
@@ -140,8 +132,8 @@ class Server extends EventEmitter {
140
132
// handshake is GET only
141
133
if ( "GET" !== req . method )
142
134
return fn ( Server . errors . BAD_HANDSHAKE_METHOD , false ) ;
143
- if ( ! this . allowRequest ) return fn ( null , true ) ;
144
- return this . allowRequest ( req , fn ) ;
135
+ if ( ! this . opts . allowRequest ) return fn ( null , true ) ;
136
+ return this . opts . allowRequest ( req , fn ) ;
145
137
}
146
138
147
139
fn ( null , true ) ;
@@ -240,10 +232,10 @@ class Server extends EventEmitter {
240
232
try {
241
233
var transport = new transports [ transportName ] ( req ) ;
242
234
if ( "polling" === transportName ) {
243
- transport . maxHttpBufferSize = this . maxHttpBufferSize ;
244
- transport . httpCompression = this . httpCompression ;
235
+ transport . maxHttpBufferSize = this . opts . maxHttpBufferSize ;
236
+ transport . httpCompression = this . opts . httpCompression ;
245
237
} else if ( "websocket" === transportName ) {
246
- transport . perMessageDeflate = this . perMessageDeflate ;
238
+ transport . perMessageDeflate = this . opts . perMessageDeflate ;
247
239
}
248
240
249
241
if ( req . _query && req . _query . b64 ) {
@@ -424,7 +416,7 @@ class Server extends EventEmitter {
424
416
}
425
417
} ) ;
426
418
427
- if ( ~ self . transports . indexOf ( "websocket" ) ) {
419
+ if ( ~ self . opts . transports . indexOf ( "websocket" ) ) {
428
420
server . on ( "upgrade" , function ( req , socket , head ) {
429
421
if ( check ( req ) ) {
430
422
self . handleUpgrade ( req , socket , head ) ;
0 commit comments