1
1
'use strict' ;
2
2
3
3
const EventEmitter = require ( 'events' ) ;
4
- const crypto = require ( 'crypto' ) ;
5
4
const https = require ( 'https' ) ;
6
5
const http = require ( 'http' ) ;
7
6
const net = require ( 'net' ) ;
8
7
const tls = require ( 'tls' ) ;
9
- const url = require ( 'url' ) ;
8
+ const { randomBytes, createHash } = require ( 'crypto' ) ;
9
+ const { URL } = require ( 'url' ) ;
10
10
11
11
const PerMessageDeflate = require ( './permessage-deflate' ) ;
12
12
const EventTarget = require ( './event-target' ) ;
@@ -480,10 +480,7 @@ function initAsClient(websocket, address, protocols, options) {
480
480
parsedUrl = address ;
481
481
websocket . url = address . href ;
482
482
} else {
483
- //
484
- // The WHATWG URL constructor is not available on Node.js < 6.13.0
485
- //
486
- parsedUrl = url . URL ? new url . URL ( address ) : url . parse ( address ) ;
483
+ parsedUrl = new URL ( address ) ;
487
484
websocket . url = address ;
488
485
}
489
486
@@ -496,7 +493,7 @@ function initAsClient(websocket, address, protocols, options) {
496
493
const isSecure =
497
494
parsedUrl . protocol === 'wss:' || parsedUrl . protocol === 'https:' ;
498
495
const defaultPort = isSecure ? 443 : 80 ;
499
- const key = crypto . randomBytes ( 16 ) . toString ( 'base64' ) ;
496
+ const key = randomBytes ( 16 ) . toString ( 'base64' ) ;
500
497
const get = isSecure ? https . get : http . get ;
501
498
const path = parsedUrl . search
502
499
? `${ parsedUrl . pathname || '/' } ${ parsedUrl . search } `
@@ -588,9 +585,7 @@ function initAsClient(websocket, address, protocols, options) {
588
585
589
586
req . abort ( ) ;
590
587
591
- const addr = url . URL
592
- ? new url . URL ( location , address )
593
- : url . resolve ( address , location ) ;
588
+ const addr = new URL ( location , address ) ;
594
589
595
590
initAsClient ( websocket , addr , protocols , options ) ;
596
591
} else if ( ! websocket . emit ( 'unexpected-response' , req , res ) ) {
@@ -613,8 +608,7 @@ function initAsClient(websocket, address, protocols, options) {
613
608
614
609
req = websocket . _req = null ;
615
610
616
- const digest = crypto
617
- . createHash ( 'sha1' )
611
+ const digest = createHash ( 'sha1' )
618
612
. update ( key + GUID )
619
613
. digest ( 'base64' ) ;
620
614
@@ -676,13 +670,7 @@ function initAsClient(websocket, address, protocols, options) {
676
670
* @private
677
671
*/
678
672
function netConnect ( options ) {
679
- //
680
- // Override `options.path` only if `options` is a copy of the original options
681
- // object. This is always true on Node.js >= 8 but not on Node.js 6 where
682
- // `options.socketPath` might be `undefined` even if the `socketPath` option
683
- // was originally set.
684
- //
685
- if ( options . protocolVersion ) options . path = options . socketPath ;
673
+ options . path = options . socketPath ;
686
674
return net . connect ( options ) ;
687
675
}
688
676
1 commit comments
julien-f commentedon Jun 3, 2019
It would be nice to add a
engines
field in thepackage.json
to better document this 🙂