Remove Base64 WebSocket remnants

There is no encoding/decoding in modern WebSockets, so let's clean up
some of the old crud that no longer serves a purpose.
This commit is contained in:
Pierre Ossman 2023-05-15 13:33:11 +02:00
parent b146de6d69
commit 3fc0cb0cb7
2 changed files with 5 additions and 45 deletions

View File

@ -177,7 +177,7 @@ export default class Websock {
flush() { flush() {
if (this._sQlen > 0 && this.readyState === 'open') { if (this._sQlen > 0 && this.readyState === 'open') {
this._websocket.send(this._encodeMessage()); this._websocket.send(new Uint8Array(this._sQ.buffer, 0, this._sQlen));
this._sQlen = 0; this._sQlen = 0;
} }
} }
@ -268,11 +268,6 @@ export default class Websock {
} }
// private methods // private methods
_encodeMessage() {
// Put in a binary arraybuffer
// according to the spec, you can send ArrayBufferViews with the send method
return new Uint8Array(this._sQ.buffer, 0, this._sQlen);
}
// We want to move all the unread data to the start of the queue, // We want to move all the unread data to the start of the queue,
// e.g. compacting. // e.g. compacting.
@ -312,17 +307,14 @@ export default class Websock {
} }
// push arraybuffer values onto the end of the receive que // push arraybuffer values onto the end of the receive que
_DecodeMessage(data) { _recvMessage(e) {
const u8 = new Uint8Array(data); const u8 = new Uint8Array(e.data);
if (u8.length > this._rQbufferSize - this._rQlen) { if (u8.length > this._rQbufferSize - this._rQlen) {
this._expandCompactRQ(u8.length); this._expandCompactRQ(u8.length);
} }
this._rQ.set(u8, this._rQlen); this._rQ.set(u8, this._rQlen);
this._rQlen += u8.length; this._rQlen += u8.length;
}
_recvMessage(e) {
this._DecodeMessage(e.data);
if (this._rQlen - this._rQi > 0) { if (this._rQlen - this._rQi > 0) {
this._eventHandlers.message(); this._eventHandlers.message();
if (this._rQlen == this._rQi) { if (this._rQlen == this._rQi) {

View File

@ -161,10 +161,9 @@ describe('Websock', function () {
it('should actually send on the websocket', function () { it('should actually send on the websocket', function () {
sock._sQ = new Uint8Array([1, 2, 3]); sock._sQ = new Uint8Array([1, 2, 3]);
sock._sQlen = 3; sock._sQlen = 3;
const encoded = sock._encodeMessage();
sock.flush(); sock.flush();
expect(sock).to.have.sent(encoded); expect(sock).to.have.sent(new Uint8Array([1, 2, 3]));
}); });
it('should not call send if we do not have anything queued up', function () { it('should not call send if we do not have anything queued up', function () {
@ -397,9 +396,8 @@ describe('Websock', function () {
sock._allocateBuffers(); sock._allocateBuffers();
}); });
it('should support adding binary Uint8Array data to the receive queue', function () { it('should support adding data to the receive queue', function () {
const msg = { data: new Uint8Array([1, 2, 3]) }; const msg = { data: new Uint8Array([1, 2, 3]) };
sock._mode = 'binary';
sock._recvMessage(msg); sock._recvMessage(msg);
expect(sock.rQshiftStr(3)).to.equal('\x01\x02\x03'); expect(sock.rQshiftStr(3)).to.equal('\x01\x02\x03');
}); });
@ -426,7 +424,6 @@ describe('Websock', function () {
sock._rQlen = 6; sock._rQlen = 6;
sock._rQi = 6; sock._rQi = 6;
const msg = { data: new Uint8Array([1, 2, 3]).buffer }; const msg = { data: new Uint8Array([1, 2, 3]).buffer };
sock._mode = 'binary';
sock._recvMessage(msg); sock._recvMessage(msg);
expect(sock._rQlen).to.equal(0); expect(sock._rQlen).to.equal(0);
expect(sock._rQi).to.equal(0); expect(sock._rQi).to.equal(0);
@ -438,7 +435,6 @@ describe('Websock', function () {
sock._rQlen = 20; sock._rQlen = 20;
sock._rQi = 10; sock._rQi = 10;
const msg = { data: new Uint8Array([1, 2]).buffer }; const msg = { data: new Uint8Array([1, 2]).buffer };
sock._mode = 'binary';
sock._recvMessage(msg); sock._recvMessage(msg);
expect(sock._rQlen).to.equal(12); expect(sock._rQlen).to.equal(12);
expect(sock._rQi).to.equal(0); expect(sock._rQi).to.equal(0);
@ -450,7 +446,6 @@ describe('Websock', function () {
sock._rQi = 0; sock._rQi = 0;
sock._rQbufferSize = 20; sock._rQbufferSize = 20;
const msg = { data: new Uint8Array(30).buffer }; const msg = { data: new Uint8Array(30).buffer };
sock._mode = 'binary';
sock._recvMessage(msg); sock._recvMessage(msg);
expect(sock._rQlen).to.equal(30); expect(sock._rQlen).to.equal(30);
expect(sock._rQi).to.equal(0); expect(sock._rQi).to.equal(0);
@ -463,37 +458,10 @@ describe('Websock', function () {
sock._rQi = 16; sock._rQi = 16;
sock._rQbufferSize = 20; sock._rQbufferSize = 20;
const msg = { data: new Uint8Array(6).buffer }; const msg = { data: new Uint8Array(6).buffer };
sock._mode = 'binary';
sock._recvMessage(msg); sock._recvMessage(msg);
expect(sock._rQlen).to.equal(6); expect(sock._rQlen).to.equal(6);
expect(sock._rQi).to.equal(0); expect(sock._rQi).to.equal(0);
expect(sock._rQ.length).to.equal(48); expect(sock._rQ.length).to.equal(48);
}); });
}); });
describe('Data encoding', function () {
before(function () { FakeWebSocket.replace(); });
after(function () { FakeWebSocket.restore(); });
describe('as binary data', function () {
let sock;
beforeEach(function () {
sock = new Websock();
sock.open('ws://', 'binary');
sock._websocket._open();
});
it('should only send the send queue up to the send queue length', function () {
sock._sQ = new Uint8Array([1, 2, 3, 4, 5]);
sock._sQlen = 3;
const res = sock._encodeMessage();
expect(res).to.array.equal(new Uint8Array([1, 2, 3]));
});
it('should properly pass the encoded data off to the actual WebSocket', function () {
sock.send([1, 2, 3]);
expect(sock._websocket._getSentData()).to.array.equal(new Uint8Array([1, 2, 3]));
});
});
});
}); });