@@ -7,6 +7,11 @@ import { Duplex } from 'stream';
77 * used when running compass-web in a local sandbox, mms has their own
88 * implementation
99 */
10+ enum MESSAGE_TYPE {
11+ JSON = 0x01 ,
12+ BINARY = 0x02 ,
13+ }
14+
1015class Socket extends Duplex {
1116 private _ws : WebSocket | null = null ;
1217 constructor ( ) {
@@ -36,7 +41,7 @@ class Socket extends Duplex {
3641 ok : 1 ,
3742 } ) ;
3843 setTimeout ( ( ) => {
39- this . _ws ?. send ( connectMsg ) ;
44+ this . _ws ?. send ( this . encodeStringMessageWithTypeByte ( connectMsg ) ) ;
4045 } ) ;
4146 } ,
4247 { once : true }
@@ -49,10 +54,12 @@ class Socket extends Duplex {
4954 } ) ;
5055 this . _ws . addEventListener (
5156 'message' ,
52- ( { data } : MessageEvent < string | ArrayBuffer > ) => {
53- if ( typeof data === 'string' ) {
57+ ( { data } : MessageEvent < ArrayBuffer > ) => {
58+ const dataView = new Uint8Array ( data ) ;
59+ if ( dataView [ 0 ] === 0x01 ) {
5460 try {
55- const res = JSON . parse ( data ) as { preMessageOk : 1 } ;
61+ const res = this . decodeMessageWithTypeByte ( dataView ) ;
62+ // const res = JSON.parse(data) as { preMessageOk: 1 };
5663 if ( res . preMessageOk ) {
5764 setTimeout ( ( ) => {
5865 this . emit ( options . tls ? 'secureConnect' : 'connect' ) ;
@@ -64,7 +71,7 @@ class Socket extends Duplex {
6471 }
6572 } else {
6673 setTimeout ( ( ) => {
67- this . emit ( 'data' , Buffer . from ( data ) ) ;
74+ this . emit ( 'data' , this . decodeMessageWithTypeByte ( dataView ) ) ;
6875 } ) ;
6976 }
7077 }
@@ -75,7 +82,8 @@ class Socket extends Duplex {
7582 // noop
7683 }
7784 _write ( chunk : ArrayBufferLike , _encoding : BufferEncoding , cb : ( ) => void ) {
78- this . _ws ?. send ( chunk ) ;
85+ // need this to change
86+ this . _ws ?. send ( this . encodeBinaryMessageWithTypeByte ( new Uint8Array ( chunk ) ) ) ;
7987 setTimeout ( ( ) => {
8088 cb ( ) ;
8189 } ) ;
@@ -110,6 +118,37 @@ class Socket extends Duplex {
110118 setNoDelay ( ) {
111119 return this ;
112120 }
121+
122+ encodeStringMessageWithTypeByte ( message : string ) {
123+ const utf8Encoder = new TextEncoder ( ) ;
124+ const utf8Array = utf8Encoder . encode ( message ) ;
125+ return this . encodeMessageWithTypeByte ( utf8Array , MESSAGE_TYPE . JSON ) ;
126+ }
127+
128+ encodeBinaryMessageWithTypeByte ( message : Uint8Array ) {
129+ return this . encodeMessageWithTypeByte ( message , MESSAGE_TYPE . BINARY ) ;
130+ }
131+
132+ encodeMessageWithTypeByte ( message : Uint8Array , type : MESSAGE_TYPE ) {
133+ const encoded = new Uint8Array ( message . length + 1 ) ;
134+ encoded [ 0 ] = type ;
135+ encoded . set ( message , 1 ) ;
136+ return encoded ;
137+ }
138+
139+ decodeMessageWithTypeByte ( message : Uint8Array ) {
140+ const typeByte = message [ 0 ] ;
141+ if ( typeByte === MESSAGE_TYPE . JSON ) {
142+ const jsonBytes = message . subarray ( 1 ) ;
143+ const textDecoder = new TextDecoder ( 'utf-8' ) ;
144+ const jsonStr = textDecoder . decode ( jsonBytes ) ;
145+ return JSON . parse ( jsonStr ) ;
146+ }
147+ // this has to change still too
148+ else if ( typeByte === MESSAGE_TYPE . BINARY ) {
149+ return message . subarray ( 1 ) ;
150+ }
151+ }
113152}
114153
115154export { isIPv4 , isIPv6 } from 'is-ip' ;
0 commit comments