@@ -26,22 +26,12 @@ function createWebSocketProxy(port = 1337, logger = console) {
2626 } ) ;
2727 ws . on ( 'message' , async ( data ) => {
2828 if ( socket ) {
29- socket . write ( data , 'binary' ) ;
29+ socket . write ( decodeMessageWithTypeByte ( data ) , 'binary' ) ;
3030 } else {
3131 // First message before socket is created is with connection info
32- const dataView = new Uint8Array ( data ) ;
33- const messageByte = dataView [ 0 ] ;
34- if ( messageByte !== 0x01 ) {
35- logger . log ( 'first message should have type byte,(%s)' , evt , err ) ;
36- ws . close ( evt === 'close' ? 1001 : 1011 ) ;
37- }
38- const jsonBytes = dataView . subarray ( 1 ) ;
39- const jsonStr = new TextDecoder ( 'utf-8' ) . decode ( jsonBytes ) ;
40- console . log ( 'pre message received on backend: ' , jsonStr ) ;
4132 const { tls : useSecureConnection , ...connectOptions } =
42- JSON . parse ( jsonStr ) ;
33+ decodeMessageWithTypeByte ( data ) ;
4334
44- // const { tls: useSecureConnection, ...connectOptions } = JSON.parse(data.toString);
4535 logger . log (
4636 'setting up new%s connection to %s:%s' ,
4737 useSecureConnection ? ' secure' : '' ,
@@ -71,22 +61,13 @@ function createWebSocketProxy(port = 1337, logger = console) {
7161 connectOptions . port
7262 ) ;
7363 socket . setTimeout ( 0 ) ;
74- const utf8Encoder = new TextEncoder ( ) ;
75- const message = JSON . stringify ( { preMessageOk : 1 } ) ;
76- const utf8Array = utf8Encoder . encode ( message ) ;
77-
78- const encoded = new Uint8Array ( utf8Array . length + 1 ) ;
79- encoded [ 0 ] = 0x01 ;
80- encoded . set ( utf8Array , 1 ) ;
64+ const encoded = encodeStringMessageWithTypeByte (
65+ JSON . stringify ( { preMessageOk : 1 } )
66+ ) ;
8167 ws . send ( encoded ) ;
8268 } ) ;
8369 socket . on ( 'data' , async ( data ) => {
84- const encoded = new Uint8Array ( data . length + 1 ) ;
85- encoded [ 0 ] = 0x02 ;
86- encoded . set ( data , 1 ) ;
87- // ws.send(encoded);
88- logger . log ( encoded ) ;
89- ws . send ( encoded ) ;
70+ ws . send ( encodeBinaryMessageWithTypeByte ( data ) ) ;
9071 } ) ;
9172 }
9273 } ) ;
@@ -95,4 +76,35 @@ function createWebSocketProxy(port = 1337, logger = console) {
9576 return wsServer ;
9677}
9778
79+ function encodeStringMessageWithTypeByte ( message ) {
80+ const utf8Encoder = new TextEncoder ( ) ;
81+ const utf8Array = utf8Encoder . encode ( message ) ;
82+ return encodeMessageWithTypeByte ( utf8Array , 0x01 ) ;
83+ }
84+
85+ function encodeBinaryMessageWithTypeByte ( message ) {
86+ return encodeMessageWithTypeByte ( message , 0x02 ) ;
87+ }
88+
89+ function encodeMessageWithTypeByte ( message , type ) {
90+ const encoded = new Uint8Array ( message . length + 1 ) ;
91+ encoded [ 0 ] = type ;
92+ encoded . set ( message , 1 ) ;
93+ return encoded ;
94+ }
95+
96+ function decodeMessageWithTypeByte ( message ) {
97+ const typeByte = message [ 0 ] ;
98+ if ( typeByte === 0x01 ) {
99+ const jsonBytes = message . subarray ( 1 ) ;
100+ const textDecoder = new TextDecoder ( 'utf-8' ) ;
101+ const jsonStr = textDecoder . decode ( jsonBytes ) ;
102+ return JSON . parse ( jsonStr ) ;
103+ } else if ( typeByte === 0x02 ) {
104+ return message . subarray ( 1 ) ;
105+ } else {
106+ console . error ( 'message does not have valid type byte "%s":' , message ) ;
107+ }
108+ }
109+
98110module . exports = { createWebSocketProxy } ;
0 commit comments