@@ -17,6 +17,22 @@ const hasCode = (filterCode) => ({ status: { code } }) => code === filterCode;
17
17
18
18
const isErrorMessage = ( { status : { code } } ) => [ 200 , 204 , 206 ] . indexOf ( code ) === - 1 ;
19
19
20
+ const serializeToBinary = ( message , accept ) => {
21
+ let serializedMessage = accept + JSON . stringify ( message ) ;
22
+ serializedMessage = unescape ( encodeURIComponent ( serializedMessage ) ) ;
23
+
24
+ // Let's start packing the message into binary
25
+ // mimeLength(1) + mimeType Length + serializedMessage Length
26
+ let binaryMessage = new Uint8Array ( 1 + serializedMessage . length ) ;
27
+ binaryMessage [ 0 ] = accept . length ;
28
+
29
+ for ( let i = 0 ; i < serializedMessage . length ; i ++ ) {
30
+ binaryMessage [ i + 1 ] = serializedMessage . charCodeAt ( i ) ;
31
+ }
32
+
33
+ return binaryMessage ;
34
+ }
35
+
20
36
class GremlinClient extends EventEmitter {
21
37
constructor ( port = 8182 , host = 'localhost' , options = { } ) {
22
38
super ( ) ;
@@ -47,38 +63,37 @@ class GremlinClient extends EventEmitter {
47
63
48
64
this . commands = { } ;
49
65
50
- const connection = this . createConnection ( {
51
- port,
52
- host,
53
- path : this . options . path
54
- } ) ;
55
66
56
67
this . commands$ = new Rx . Subject ( ) ;
57
68
this . commands$ . subscribe ( ( command ) => {
58
69
const { message : { requestId } } = command ;
59
70
this . commands [ requestId ] = command
60
71
} ) ;
61
72
62
- this . registerConnection ( connection ) ;
63
- }
73
+ const connection = this . createConnection ( {
74
+ port,
75
+ host,
76
+ path : this . options . path
77
+ } ) ;
64
78
65
- createConnection ( { port, host, path } ) {
66
- return new WebSocketGremlinConnection ( { port, host, path } ) ;
67
- }
79
+ const connections$ = Rx . Observable . create ( ( observer ) => observer . next ( connection ) ) ;
68
80
69
- registerConnection ( connection ) {
70
- this . connection = connection ;
81
+ const open$ = connections$
82
+ . flatMap ( ( connection ) => Rx . Observable . fromEvent ( connection , 'open' ) ) ;
71
83
72
- const open$ = Rx . Observable . fromEvent ( connection , 'open' ) ;
73
- const error$ = Rx . Observable . fromEvent ( connection , 'error' ) ;
74
- const incomingMessages$ = Rx . Observable . fromEvent ( connection , 'message' )
84
+ const error$ = connections$
85
+ . flatMap ( ( connection ) => Rx . Observable . fromEvent ( connection , 'error' ) ) ;
86
+
87
+ const incomingMessages$ = connections$
88
+ . flatMap ( ( connection ) => Rx . Observable . fromEvent ( connection , 'message' ) )
75
89
. map ( ( { data } ) => {
76
90
const buffer = new Buffer ( data , 'binary' ) ;
77
91
const rawMessage = JSON . parse ( buffer . toString ( 'utf-8' ) ) ;
78
92
79
93
return rawMessage ;
80
94
} ) ;
81
- const close$ = Rx . Observable . fromEvent ( connection , 'close' ) ;
95
+ const close$ = connections$
96
+ . flatMap ( ( connection ) => Rx . Observable . fromEvent ( connection , 'close' ) ) ;
82
97
83
98
const canSend$ = Rx . Observable . merge (
84
99
open$ . map ( true ) ,
@@ -95,11 +110,18 @@ class GremlinClient extends EventEmitter {
95
110
close$ . subscribe ( ( event ) => this . handleDisconnection ( event ) ) ;
96
111
97
112
const outgoingMessages$ = this . commands$
98
- . map ( ( { message } ) => message )
99
- . pausableBuffered ( canSend$ ) ;
113
+ . map ( ( { message } ) => serializeToBinary ( message , this . options . accept ) )
114
+ . pausableBuffered ( canSend$ )
115
+ . combineLatest ( connections$ ) ;
100
116
101
117
outgoingMessages$
102
- . subscribe ( ( message ) => this . sendMessage ( message ) ) ;
118
+ . subscribe ( ( [ binaryMessage , connection ] ) =>
119
+ connection . sendMessage ( binaryMessage )
120
+ ) ;
121
+ }
122
+
123
+ createConnection ( { port, host, path } ) {
124
+ return new WebSocketGremlinConnection ( { port, host, path } ) ;
103
125
}
104
126
105
127
handleError ( err ) {
@@ -177,22 +199,6 @@ class GremlinClient extends EventEmitter {
177
199
return message ;
178
200
} ;
179
201
180
- sendMessage ( message ) {
181
- let serializedMessage = this . options . accept + JSON . stringify ( message ) ;
182
- serializedMessage = unescape ( encodeURIComponent ( serializedMessage ) ) ;
183
-
184
- // Let's start packing the message into binary
185
- // mimeLength(1) + mimeType Length + serializedMessage Length
186
- let binaryMessage = new Uint8Array ( 1 + serializedMessage . length ) ;
187
- binaryMessage [ 0 ] = this . options . accept . length ;
188
-
189
- for ( let i = 0 ; i < serializedMessage . length ; i ++ ) {
190
- binaryMessage [ i + 1 ] = serializedMessage . charCodeAt ( i ) ;
191
- }
192
-
193
- this . connection . sendMessage ( binaryMessage ) ;
194
- } ;
195
-
196
202
/**
197
203
* Asynchronously send a script to Gremlin Server for execution and fire
198
204
* the provided callback when all results have been fetched.
0 commit comments