2323'use strict' ;
2424
2525const EventEmitter = require ( 'events' ) ;
26- const WebSocket = require ( 'ws' ) ;
26+ // const WebSocket = require('ws');
2727const util = require ( 'util' ) ;
2828const utils = require ( '../utils' ) ;
2929const serializer = require ( '../structure/io/graph-serializer' ) ;
@@ -117,55 +117,49 @@ class Connection extends EventEmitter {
117117
118118 this . emit ( 'log' , `ws open` ) ;
119119
120- this . _ws = new WebSocket ( this . url , {
121- headers : this . options . headers ,
122- ca : this . options . ca ,
123- cert : this . options . cert ,
124- pfx : this . options . pfx ,
125- rejectUnauthorized : this . options . rejectUnauthorized
126- } ) ;
120+ this . _ws = new WebSocket ( this . url ) ;
127121
128- this . _ws . on ( 'message' , ( data ) => this . _handleMessage ( data ) ) ;
129- this . _ws . on ( 'error' , ( err ) => this . _handleError ( err ) ) ;
130- this . _ws . on ( 'close' , ( code , message ) => this . _handleClose ( code , message ) ) ;
122+ this . _ws . onmessage = ( data => this . _handleMessage ( data ) ) ;
123+ this . _ws . onerror = ( err => this . _handleError ( err ) ) ;
124+ this . _ws . onclose = ( code , message ) => this . _handleClose ( code , message ) ;
131125
132- this . _ws . on ( ' pong' , ( ) => {
126+ this . _ws . pong = ( ( ) => {
133127 this . emit ( 'log' , 'ws pong received' ) ;
134- if ( this . _pongTimeout ) {
135- clearTimeout ( this . _pongTimeout ) ;
136- this . _pongTimeout = null ;
137- }
138- } ) ;
139- this . _ws . on ( ' ping' , ( ) => {
128+ if ( this . _pongTimeout ) {
129+ clearTimeout ( this . _pongTimeout ) ;
130+ this . _pongTimeout = null ;
131+ }
132+ } ) ;
133+ this . _ws . ping = ( ( ) => {
140134 this . emit ( 'log' , 'ws ping received' ) ;
141- this . _ws . pong ( ) ;
142- } ) ;
135+ this . _ws . pong ( ) ;
136+ } ) ;
143137
144138 return this . _openPromise = new Promise ( ( resolve , reject ) => {
145- this . _ws . on ( 'open' , ( ) => {
146- this . isOpen = true ;
147- if ( this . _pingEnabled ) {
148- this . _pingHeartbeat ( ) ;
149- }
150- resolve ( ) ;
151- } ) ;
152- } ) ;
139+ this . _ws . onopen = ( ( ) => {
140+ this . isOpen = true ;
141+ if ( this . _pingEnabled ) {
142+ this . _pingHeartbeat ( ) ;
143+ }
144+ resolve ( ) ;
145+ } ) ;
146+ } ) ;
153147 }
154148
155149 /** @override */
156150 submit ( bytecode , op , args , requestId , processor ) {
157151 return this . open ( ) . then ( ( ) => new Promise ( ( resolve , reject ) => {
158152 if ( requestId === null || requestId === undefined ) {
159- requestId = utils . getUuid ( ) ;
160- this . _responseHandlers [ requestId ] = {
161- callback : ( err , result ) => err ? reject ( err ) : resolve ( result ) ,
153+ requestId = utils . getUuid ( ) ;
154+ this . _responseHandlers [ requestId ] = {
155+ callback : ( err , result ) => err ? reject ( err ) : resolve ( result ) ,
162156 result : null
163- } ;
164- }
157+ } ;
158+ }
165159
166- const message = Buffer . from ( this . _header + JSON . stringify ( this . _getRequest ( requestId , bytecode , op , args , processor ) ) ) ;
167- this . _ws . send ( message ) ;
168- } ) ) ;
160+ const message = Buffer . from ( this . _header + JSON . stringify ( this . _getRequest ( requestId , bytecode , op , args , processor ) ) ) ;
161+ this . _ws . send ( message ) ;
162+ } ) ) ;
169163 }
170164
171165 _getRequest ( id , bytecode , op , args , processor ) {
@@ -194,20 +188,20 @@ class Connection extends EventEmitter {
194188
195189 this . _pingInterval = setInterval ( ( ) => {
196190 if ( this . isOpen === false ) {
197- // in case of if not open..
198- if ( this . _pingInterval ) {
199- clearInterval ( this . _pingInterval ) ;
200- this . _pingInterval = null ;
201- }
191+ // in case of if not open..
192+ if ( this . _pingInterval ) {
193+ clearInterval ( this . _pingInterval ) ;
194+ this . _pingInterval = null ;
202195 }
196+ }
203197
204- this . _pongTimeout = setTimeout ( ( ) => {
205- this . _ws . terminate ( ) ;
206- } , this . _pongTimeoutDelay ) ;
198+ this . _pongTimeout = setTimeout ( ( ) => {
199+ this . _ws . terminate ( ) ;
200+ } , this . _pongTimeoutDelay ) ;
207201
208- this . _ws . ping ( ) ;
202+ this . _ws . ping ( ) ;
209203
210- } , this . _pingIntervalDelay ) ;
204+ } , this . _pingIntervalDelay ) ;
211205 }
212206
213207 _handleError ( err ) {
@@ -232,15 +226,15 @@ class Connection extends EventEmitter {
232226 // We invoke any of the pending handlers with an error
233227 Object . keys ( this . _responseHandlers ) . forEach ( requestId => {
234228 const handler = this . _responseHandlers [ requestId ] ;
235- this . _clearHandler ( requestId ) ;
236- if ( response . status !== undefined && response . status . message ) {
237- return handler . callback (
229+ this . _clearHandler ( requestId ) ;
230+ if ( response . status !== undefined && response . status . message ) {
231+ return handler . callback (
238232 new Error ( util . format (
239- 'Server error (no request information): %s (%d)' , response . status . message , response . status . code ) ) ) ;
240- } else {
241- return handler . callback ( new Error ( util . format ( 'Server error (no request information): %j' , response ) ) ) ;
242- }
243- } ) ;
233+ 'Server error (no request information): %s (%d)' , response . status . message , response . status . code ) ) ) ;
234+ } else {
235+ return handler . callback ( new Error ( util . format ( 'Server error (no request information): %j' , response ) ) ) ;
236+ }
237+ } ) ;
244238 return ;
245239 }
246240
@@ -255,14 +249,14 @@ class Connection extends EventEmitter {
255249 if ( response . status . code === responseStatusCode . authenticationChallenge && this . _authenticator ) {
256250 this . _authenticator . evaluateChallenge ( response . result . data ) . then ( res => {
257251 return this . submit ( null , 'authentication' , res , response . requestId ) ;
258- } ) . catch ( handler . callback ) ;
252+ } ) . catch ( handler . callback ) ;
259253
260254 return ;
261255 }
262256 else if ( response . status . code >= 400 ) {
263257 // callback in error
264258 return handler . callback (
265- new Error ( util . format ( 'Server error: %s (%d)' , response . status . message , response . status . code ) ) ) ;
259+ new Error ( util . format ( 'Server error: %s (%d)' , response . status . message , response . status . code ) ) ) ;
266260 }
267261 switch ( response . status . code ) {
268262 case responseStatusCode . noContent :
@@ -327,10 +321,10 @@ class Connection extends EventEmitter {
327321 // in another map for types like EnumValue. Could be a nicer way to do this but for now it's solving the
328322 // problem with script submission of non JSON native types
329323 if ( protocolLevel && key === 'bindings' )
330- newObj [ key ] = this . _adaptArgs ( args [ key ] , false ) ;
331- else
332- newObj [ key ] = this . _writer . adaptObject ( args [ key ] ) ;
333- } ) ;
324+ newObj [ key ] = this . _adaptArgs ( args [ key ] , false ) ;
325+ else
326+ newObj [ key ] = this . _writer . adaptObject ( args [ key ] ) ;
327+ } ) ;
334328
335329 return newObj ;
336330 }
@@ -349,8 +343,8 @@ class Connection extends EventEmitter {
349343 if ( ! this . _closePromise ) {
350344 this . _closePromise = new Promise ( resolve => {
351345 this . _closeCallback = resolve ;
352- this . _ws . close ( ) ;
353- } ) ;
346+ this . _ws . close ( ) ;
347+ } ) ;
354348 }
355349 return this . _closePromise ;
356350 }
0 commit comments