Skip to content

Commit

Permalink
[minor] Remove length threshold
Browse files Browse the repository at this point in the history
Do not use a length threshold to determine whether or not to copy data.
  • Loading branch information
lpinca committed Mar 15, 2019
1 parent 6c22584 commit a40e29f
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 50 deletions.
16 changes: 5 additions & 11 deletions lib/sender.js
Expand Up @@ -43,7 +43,7 @@ class Sender {
* @public
*/
static frame(data, options) {
const merge = data.length < 1024 || (options.mask && options.readOnly);
const merge = options.mask && options.readOnly;
var offset = options.mask ? 6 : 2;
var payloadLength = data.length;

Expand All @@ -60,26 +60,20 @@ class Sender {
target[0] = options.fin ? options.opcode | 0x80 : options.opcode;
if (options.rsv1) target[0] |= 0x40;

target[1] = payloadLength;

if (payloadLength === 126) {
target.writeUInt16BE(data.length, 2);
} else if (payloadLength === 127) {
target.writeUInt32BE(0, 2);
target.writeUInt32BE(data.length, 6);
}

if (!options.mask) {
target[1] = payloadLength;
if (merge) {
data.copy(target, offset);
return [target];
}

return [target, data];
}
if (!options.mask) return [target, data];

const mask = randomBytes(4);

target[1] = payloadLength | 0x80;
target[1] |= 0x80;
target[offset - 4] = mask[0];
target[offset - 3] = mask[1];
target[offset - 2] = mask[2];
Expand Down
102 changes: 63 additions & 39 deletions test/sender.test.js
Expand Up @@ -13,7 +13,9 @@ class MockSocket {
if (write) this.write = write;
}

cork() {}
write() {}
uncork() {}
}

describe('Sender', function() {
Expand Down Expand Up @@ -87,17 +89,20 @@ describe('Sender', function() {
});

it('compresses all frames in a fragmented message', function(done) {
const fragments = [];
const chunks = [];
const perMessageDeflate = new PerMessageDeflate({ threshold: 3 });
const mockSocket = new MockSocket({
write: (data) => {
fragments.push(data);
if (fragments.length !== 2) return;
write: (chunk) => {
chunks.push(chunk);
if (chunks.length !== 4) return;

assert.strictEqual(chunks[0].length, 2);
assert.strictEqual(chunks[0][0] & 0x40, 0x40);
assert.strictEqual(chunks[1].length, 9);

assert.strictEqual(fragments[0][0] & 0x40, 0x40);
assert.strictEqual(fragments[0].length, 11);
assert.strictEqual(fragments[1][0] & 0x40, 0x00);
assert.strictEqual(fragments[1].length, 6);
assert.strictEqual(chunks[2].length, 2);
assert.strictEqual(chunks[2][0] & 0x40, 0x00);
assert.strictEqual(chunks[3].length, 4);
done();
}
});
Expand All @@ -112,17 +117,20 @@ describe('Sender', function() {
});

it('compresses no frames in a fragmented message', function(done) {
const fragments = [];
const chunks = [];
const perMessageDeflate = new PerMessageDeflate({ threshold: 3 });
const mockSocket = new MockSocket({
write: (data) => {
fragments.push(data);
if (fragments.length !== 2) return;
write: (chunk) => {
chunks.push(chunk);
if (chunks.length !== 4) return;

assert.strictEqual(fragments[0][0] & 0x40, 0x00);
assert.strictEqual(fragments[0].length, 4);
assert.strictEqual(fragments[1][0] & 0x40, 0x00);
assert.strictEqual(fragments[1].length, 5);
assert.strictEqual(chunks[0].length, 2);
assert.strictEqual(chunks[0][0] & 0x40, 0x00);
assert.strictEqual(chunks[1].length, 2);

assert.strictEqual(chunks[2].length, 2);
assert.strictEqual(chunks[2][0] & 0x40, 0x00);
assert.strictEqual(chunks[3].length, 3);
done();
}
});
Expand All @@ -137,17 +145,20 @@ describe('Sender', function() {
});

it('compresses empty buffer as first fragment', function(done) {
const fragments = [];
const chunks = [];
const perMessageDeflate = new PerMessageDeflate({ threshold: 0 });
const mockSocket = new MockSocket({
write: (data) => {
fragments.push(data);
if (fragments.length !== 2) return;
write: (chunk) => {
chunks.push(chunk);
if (chunks.length !== 4) return;

assert.strictEqual(fragments[0][0] & 0x40, 0x40);
assert.strictEqual(fragments[0].length, 3);
assert.strictEqual(fragments[1][0] & 0x40, 0x00);
assert.strictEqual(fragments[1].length, 8);
assert.strictEqual(chunks[0].length, 2);
assert.strictEqual(chunks[0][0] & 0x40, 0x40);
assert.strictEqual(chunks[1].length, 1);

assert.strictEqual(chunks[2].length, 2);
assert.strictEqual(chunks[2][0] & 0x40, 0x00);
assert.strictEqual(chunks[3].length, 6);
done();
}
});
Expand All @@ -162,17 +173,20 @@ describe('Sender', function() {
});

it('compresses empty buffer as last fragment', function(done) {
const fragments = [];
const chunks = [];
const perMessageDeflate = new PerMessageDeflate({ threshold: 0 });
const mockSocket = new MockSocket({
write: (data) => {
fragments.push(data);
if (fragments.length !== 2) return;
write: (chunk) => {
chunks.push(chunk);
if (chunks.length !== 4) return;

assert.strictEqual(chunks[0].length, 2);
assert.strictEqual(chunks[0][0] & 0x40, 0x40);
assert.strictEqual(chunks[1].length, 10);

assert.strictEqual(fragments[0][0] & 0x40, 0x40);
assert.strictEqual(fragments[0].length, 12);
assert.strictEqual(fragments[1][0] & 0x40, 0x00);
assert.strictEqual(fragments[1].length, 3);
assert.strictEqual(chunks[2].length, 2);
assert.strictEqual(chunks[2][0] & 0x40, 0x00);
assert.strictEqual(chunks[3].length, 1);
done();
}
});
Expand All @@ -193,10 +207,15 @@ describe('Sender', function() {
let count = 0;
const mockSocket = new MockSocket({
write: (data) => {
if (++count === 1) return;
if (++count < 3) return;

assert.ok(data.equals(Buffer.from([0x89, 0x02, 0x68, 0x69])));
if (count === 4) done();
if (count % 2) {
assert.ok(data.equals(Buffer.from([0x89, 0x02])));
} else {
assert.ok(data.equals(Buffer.from([0x68, 0x69])));
}

if (count === 8) done();
}
});
const sender = new Sender(mockSocket, {
Expand All @@ -220,10 +239,15 @@ describe('Sender', function() {
let count = 0;
const mockSocket = new MockSocket({
write: (data) => {
if (++count === 1) return;
if (++count < 3) return;

if (count % 2) {
assert.ok(data.equals(Buffer.from([0x8a, 0x02])));
} else {
assert.ok(data.equals(Buffer.from([0x68, 0x69])));
}

assert.ok(data.equals(Buffer.from([0x8a, 0x02, 0x68, 0x69])));
if (count === 4) done();
if (count === 8) done();
}
});
const sender = new Sender(mockSocket, {
Expand Down Expand Up @@ -263,7 +287,7 @@ describe('Sender', function() {
sender.send('baz', { compress: true, fin: true });

sender.close(1000, undefined, false, () => {
assert.strictEqual(count, 4);
assert.strictEqual(count, 8);
done();
});
});
Expand Down

0 comments on commit a40e29f

Please sign in to comment.