[go: nahoru, domu]

Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Websock API cleanup #1782

Merged
merged 18 commits into from
Jun 30, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Remove Websock implicit read length
Callers should be properly aware of how much data they need, as they
need to call rQwait() first to ensure the data is present.
  • Loading branch information
CendioOssman committed Jun 4, 2023
commit e01dd27be49940e3a49e6cc90bd6ae7d0d1d7abd
4 changes: 0 additions & 4 deletions core/websock.js
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,6 @@ export default class Websock {
}

rQshiftStr(len) {
if (typeof(len) === 'undefined') { len = this._rQlen - this._rQi; }
let str = "";
// Handle large arrays in steps to avoid long strings on the stack
for (let i = 0; i < len; i += 4096) {
Expand All @@ -136,20 +135,17 @@ export default class Websock {
}

rQshiftBytes(len) {
if (typeof(len) === 'undefined') { len = this._rQlen - this._rQi; }
this._rQi += len;
return new Uint8Array(this._rQ.buffer, this._rQi - len, len);
}

rQshiftTo(target, len) {
if (len === undefined) { len = this._rQlen - this._rQi; }
// TODO: make this just use set with views when using a ArrayBuffer to store the rQ
target.set(new Uint8Array(this._rQ.buffer, this._rQi, len));
this._rQi += len;
}

rQpeekBytes(len) {
if (typeof(len) === 'undefined') { len = this._rQlen - this._rQi; }
return new Uint8Array(this._rQ.buffer, this._rQi, len);
}

Expand Down
18 changes: 1 addition & 17 deletions tests/test.websock.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,11 +67,6 @@ describe('Websock', function () {
expect(sock._rQlen - sock._rQi).to.equal(befLen - 3);
});

it('should shift the entire rest of the queue off if no length is given', function () {
sock.rQshiftStr();
expect(sock._rQlen - sock._rQi).to.equal(0);
});

it('should be able to handle very large strings', function () {
const BIG_LEN = 500000;
const RQ_BIG = new Uint8Array(BIG_LEN);
Expand All @@ -90,7 +85,7 @@ describe('Websock', function () {
sock._rQ.set(RQ_BIG);
sock._rQlen = RQ_BIG.length;

const shifted = sock.rQshiftStr();
const shifted = sock.rQshiftStr(BIG_LEN);

expect(shifted).to.be.equal(expected);
expect(sock._rQlen - sock._rQi).to.equal(0);
Expand All @@ -106,11 +101,6 @@ describe('Websock', function () {
expect(shifted).to.array.equal(new Uint8Array(RQ_TEMPLATE.buffer, befRQi, 3));
expect(sock._rQlen - sock._rQi).to.equal(befLen - 3);
});

it('should shift the entire rest of the queue off if no length is given', function () {
sock.rQshiftBytes();
expect(sock._rQlen - sock._rQi).to.equal(0);
});
});

describe('rQpeekBytes', function () {
Expand All @@ -130,12 +120,6 @@ describe('Websock', function () {
expect(sl).to.array.equal(new Uint8Array(RQ_TEMPLATE.buffer, 0, 2));
});

it('should use the rest of the receive queue if no end is given', function () {
const sl = sock.rQpeekBytes();
expect(sl).to.have.length(RQ_TEMPLATE.length);
expect(sl).to.array.equal(RQ_TEMPLATE);
});

it('should take the current rQi in to account', function () {
sock._rQi = 1;
expect(sock.rQpeekBytes(2)).to.array.equal(new Uint8Array(RQ_TEMPLATE.buffer, 1, 2));
Expand Down