1+ /* global WebSocket,Blob,FileReader */
12const stream = require ( 'stream' )
23const util = require ( 'util' )
3- const timers = require ( 'timers' )
44const http = require ( 'http' )
55
66const debug = util . debuglog ( 'net' )
77
88const proxy = {
9- protocol : ( window . location . protocol == 'https:' ) ? 'wss' : 'ws' ,
9+ protocol : ( window . location . protocol === 'https:' ) ? 'wss' : 'ws' ,
1010 hostname : window . location . hostname ,
1111 port : window . location . port ,
1212 path : '/api/vm/net'
@@ -55,14 +55,14 @@ exports.connect = exports.createConnection = function (/* options, connectListen
5555function toNumber ( x ) { return ( x = Number ( x ) ) >= 0 ? x : false }
5656
5757function isPipeName ( s ) {
58- return util . isString ( s ) && toNumber ( s ) === false
58+ return typeof s === 'string' && toNumber ( s ) === false
5959}
6060
6161// Returns an array [options] or [options, cb]
6262// It is the same as the argument of Socket.prototype.connect().
6363function normalizeConnectArgs ( args ) {
6464 let options = { }
65- if ( util . isObject ( args [ 0 ] ) ) {
65+ if ( typeof args [ 0 ] === 'object' ) {
6666 // connect(options, [cb])
6767 options = args [ 0 ]
6868 } else if ( isPipeName ( args [ 0 ] ) ) {
@@ -71,12 +71,12 @@ function normalizeConnectArgs (args) {
7171 } else {
7272 // connect(port, [host], [cb])
7373 options . port = args [ 0 ]
74- if ( util . isString ( args [ 1 ] ) ) {
74+ if ( typeof args [ 1 ] === 'string' ) {
7575 options . host = args [ 1 ]
7676 }
7777 }
7878 const cb = args [ args . length - 1 ]
79- return util . isFunction ( cb ) ? [ options , cb ] : [ options ]
79+ return typeof cb === 'function' ? [ options , cb ] : [ options ]
8080}
8181exports . _normalizeConnectArgs = normalizeConnectArgs
8282
@@ -86,8 +86,9 @@ function Socket (options) {
8686 this . _connecting = false
8787 this . _host = null
8888
89- if ( util . isNumber ( options ) ) { options = { fd : options } } // Legacy interface.
90- else if ( util . isUndefined ( options ) ) { options = { } }
89+ if ( typeof options === 'number' ) {
90+ options = { fd : options }
91+ } else if ( typeof options === 'undefined' ) { options = { } }
9192
9293 stream . Duplex . call ( this , options )
9394
@@ -98,7 +99,7 @@ function Socket (options) {
9899 this . _writableState . decodeStrings = false
99100
100101 // default to *not* allowing half open sockets
101- this . allowHalfOpen = options && options . allowHalfOpen || false
102+ this . allowHalfOpen = ( options && options . allowHalfOpen ) || false
102103}
103104util . inherits ( Socket , stream . Duplex )
104105
@@ -235,9 +236,9 @@ Socket.prototype._write = function (data, encoding, cb) {
235236 this . _pendingData = null
236237 this . _pendingEncoding = ''
237238
238- if ( encoding == 'binary' && typeof data === 'string' ) { // TODO: maybe apply this for all string inputs?
239+ if ( encoding === 'binary' && typeof data === 'string' ) { // TODO: maybe apply this for all string inputs?
239240 // Setting encoding is very important for binary data - otherwise the data gets modified
240- data = new Buffer ( data , encoding )
241+ data = Buffer . alloc ( data , encoding )
241242 }
242243
243244 // Send the data
@@ -251,14 +252,14 @@ Socket.prototype._write = function (data, encoding, cb) {
251252}
252253
253254Socket . prototype . write = function ( chunk , encoding , cb ) {
254- if ( ! util . isString ( chunk ) && ! util . isBuffer ( chunk ) ) { throw new TypeError ( 'invalid data' ) }
255+ if ( typeof chunk !== 'string' && ! Buffer . isBuffer ( chunk ) ) { throw new TypeError ( 'invalid data' ) }
255256 return stream . Duplex . prototype . write . apply ( this , arguments )
256257}
257258
258259Socket . prototype . connect = function ( options , cb ) {
259260 const self = this
260261
261- if ( ! util . isObject ( options ) ) {
262+ if ( typeof options !== 'object' ) {
262263 // Old API:
263264 // connect(port, [host], [cb])
264265 // connect(path, [cb]);
@@ -374,22 +375,22 @@ Socket.prototype._handleWebsocket = function () {
374375 }
375376
376377 if ( typeof contents === 'string' ) {
377- const buffer = new Buffer ( contents )
378+ const buffer = Buffer . alloc ( contents )
378379 gotBuffer ( buffer )
379380 } else if ( window . Blob && contents instanceof Blob ) {
380381 const fileReader = new FileReader ( )
381382 fileReader . addEventListener ( 'load' , function ( e ) {
382383 const buf = fileReader . result
383384 const arr = new Uint8Array ( buf )
384- gotBuffer ( new Buffer ( arr ) )
385+ gotBuffer ( Buffer . from ( arr ) )
385386 } )
386387 fileReader . readAsArrayBuffer ( contents )
387388 } else {
388389 console . warn ( 'Cannot read TCP stream: unsupported message type' , contents )
389390 }
390391 } )
391392 this . _ws . addEventListener ( 'close' , function ( ) {
392- if ( self . readyState == 'open' ) {
393+ if ( self . readyState === 'open' ) {
393394 // console.log('TCP closed');
394395 self . destroy ( )
395396 }
0 commit comments