Always return copy of data from socket
We don't know how long the caller will hang on to this data, so we need to be safe by default and assume it will kept indefinitely. That means we can't return a reference to the internal buffer, as that will get overwritten with future messages. We want to avoid unnecessary copying in performance critical code, though. So allow code to explicitly ask for a shared buffer, assuming they know the data needs to be consumed immediately.
This commit is contained in:
@@ -101,6 +101,12 @@ 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 return a shared array if requested', function () {
|
||||
const befRQi = sock._rQi;
|
||||
const shifted = sock.rQshiftBytes(3, false);
|
||||
expect(shifted).to.array.equal(new Uint8Array(RQ_TEMPLATE.buffer, befRQi, 3));
|
||||
expect(shifted.buffer.byteLength).to.not.equal(shifted.length);
|
||||
});
|
||||
});
|
||||
|
||||
describe('rQpeekBytes', function () {
|
||||
@@ -124,6 +130,12 @@ describe('Websock', function () {
|
||||
sock._rQi = 1;
|
||||
expect(sock.rQpeekBytes(2)).to.array.equal(new Uint8Array(RQ_TEMPLATE.buffer, 1, 2));
|
||||
});
|
||||
|
||||
it('should return a shared array if requested', function () {
|
||||
const sl = sock.rQpeekBytes(2, false);
|
||||
expect(sl).to.array.equal(new Uint8Array(RQ_TEMPLATE.buffer, 0, 2));
|
||||
expect(sl.buffer.byteLength).to.not.equal(sl.length);
|
||||
});
|
||||
});
|
||||
|
||||
describe('rQwait', function () {
|
||||
|
||||
Reference in New Issue
Block a user