Skip to content

Commit c15bfbd

Browse files
committed
working
1 parent 9cb1c31 commit c15bfbd

File tree

2 files changed

+40
-30
lines changed

2 files changed

+40
-30
lines changed

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

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,6 @@ class Socket extends Duplex {
5959
if (dataView[0] === 0x01) {
6060
try {
6161
const res = this.decodeMessageWithTypeByte(dataView);
62-
// const res = JSON.parse(data) as { preMessageOk: 1 };
6362
if (res.preMessageOk) {
6463
setTimeout(() => {
6564
this.emit(options.tls ? 'secureConnect' : 'connect');
@@ -82,7 +81,6 @@ class Socket extends Duplex {
8281
// noop
8382
}
8483
_write(chunk: ArrayBufferLike, _encoding: BufferEncoding, cb: () => void) {
85-
// need this to change
8684
this._ws?.send(this.encodeBinaryMessageWithTypeByte(new Uint8Array(chunk)));
8785
setTimeout(() => {
8886
cb();
@@ -143,10 +141,10 @@ class Socket extends Duplex {
143141
const textDecoder = new TextDecoder('utf-8');
144142
const jsonStr = textDecoder.decode(jsonBytes);
145143
return JSON.parse(jsonStr);
146-
}
147-
// this has to change still too
148-
else if (typeByte === MESSAGE_TYPE.BINARY) {
144+
} else if (typeByte === MESSAGE_TYPE.BINARY) {
149145
return message.subarray(1);
146+
} else {
147+
console.error('message does not have valid type byte "%s":', message);
150148
}
151149
}
152150
}

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

Lines changed: 37 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -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+
98110
module.exports = { createWebSocketProxy };

0 commit comments

Comments
 (0)