Skip to content

Commit 9cb1c31

Browse files
committed
binary messages not working
1 parent c1c91a5 commit 9cb1c31

File tree

2 files changed

+72
-11
lines changed

2 files changed

+72
-11
lines changed

packages/compass-web/polyfills/net/index.ts

Lines changed: 45 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -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+
1015
class 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

115154
export { isIPv4, isIPv6 } from 'is-ip';

packages/compass-web/scripts/ws-proxy.js

Lines changed: 27 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,19 @@ function createWebSocketProxy(port = 1337, logger = console) {
2929
socket.write(data, 'binary');
3030
} else {
3131
// First message before socket is created is with connection info
32-
const { tls: useSecureConnection, ...connectOptions } = JSON.parse(
33-
data.toString()
34-
);
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);
41+
const { tls: useSecureConnection, ...connectOptions } =
42+
JSON.parse(jsonStr);
43+
44+
// const { tls: useSecureConnection, ...connectOptions } = JSON.parse(data.toString);
3545
logger.log(
3646
'setting up new%s connection to %s:%s',
3747
useSecureConnection ? ' secure' : '',
@@ -61,10 +71,22 @@ function createWebSocketProxy(port = 1337, logger = console) {
6171
connectOptions.port
6272
);
6373
socket.setTimeout(0);
64-
ws.send(JSON.stringify({ preMessageOk: 1 }));
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);
81+
ws.send(encoded);
6582
});
6683
socket.on('data', async (data) => {
67-
ws.send(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);
6890
});
6991
}
7092
});

0 commit comments

Comments
 (0)