while (repeaterID.length < 250) {
repeaterID += "\0";
}
- this._sock.send_string(repeaterID);
+ this._sock.sendString(repeaterID);
return true;
}
const cversion = "00" + parseInt(this._rfbVersion, 10) +
".00" + ((this._rfbVersion * 10) % 10);
- this._sock.send_string("RFB " + cversion + "\n");
+ this._sock.sendString("RFB " + cversion + "\n");
Log.Debug('Sent ProtocolVersion: ' + cversion);
this._rfbInitState = 'Security';
String.fromCharCode(this._rfbCredentials.target.length) +
this._rfbCredentials.username +
this._rfbCredentials.target;
- this._sock.send_string(xvpAuthStr);
+ this._sock.sendString(xvpAuthStr);
this._rfbAuthScheme = 2;
return this._negotiateAuthentication();
}
// XXX we assume lengths are <= 255 (should not be an issue in the real world)
this._sock.send([0, 0, 0, user.length]);
this._sock.send([0, 0, 0, pass.length]);
- this._sock.send_string(user);
- this._sock.send_string(pass);
+ this._sock.sendString(user);
+ this._sock.sendString(pass);
this._rfbInitState = "SecurityResult";
return true;
this._sock.send([0, 0, 0, this._rfbCredentials.username.length]);
this._sock.send([0, 0, 0, this._rfbCredentials.password.length]);
- this._sock.send_string(this._rfbCredentials.username);
- this._sock.send_string(this._rfbCredentials.password);
+ this._sock.sendString(this._rfbCredentials.username);
+ this._sock.sendString(this._rfbCredentials.password);
this._rfbInitState = "SecurityResult";
return true;
}
flush() {
if (this._sQlen > 0 && this._websocket.readyState === WebSocket.OPEN) {
- this._websocket.send(this._encode_message());
+ this._websocket.send(this._encodeMessage());
this._sQlen = 0;
}
}
this.flush();
}
- send_string(str) {
+ sendString(str) {
this.send(str.split('').map(chr => chr.charCodeAt(0)));
}
this._eventHandlers[evt] = handler;
}
- _allocate_buffers() {
+ _allocateBuffers() {
this._rQ = new Uint8Array(this._rQbufferSize);
this._sQ = new Uint8Array(this._sQbufferSize);
}
init() {
- this._allocate_buffers();
+ this._allocateBuffers();
this._rQi = 0;
this._websocket = null;
}
this._websocket = new WebSocket(uri, protocols);
this._websocket.binaryType = 'arraybuffer';
- this._websocket.onmessage = this._recv_message.bind(this);
+ this._websocket.onmessage = this._recvMessage.bind(this);
this._websocket.onopen = () => {
Log.Debug('>> WebSock.onopen');
if (this._websocket.protocol) {
}
// private methods
- _encode_message() {
+ _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);
// The function also expands the receive que if needed, and for
// performance reasons we combine these two actions to avoid
// unneccessary copying.
- _expand_compact_rQ(min_fit) {
+ _expandCompactRQ(minFit) {
// if we're using less than 1/8th of the buffer even with the incoming bytes, compact in place
// instead of resizing
- const required_buffer_size = (this._rQlen - this._rQi + min_fit) * 8;
- const resizeNeeded = this._rQbufferSize < required_buffer_size;
+ const requiredBufferSize = (this._rQlen - this._rQi + minFit) * 8;
+ const resizeNeeded = this._rQbufferSize < requiredBufferSize;
if (resizeNeeded) {
// Make sure we always *at least* double the buffer size, and have at least space for 8x
// the current amount of data
- this._rQbufferSize = Math.max(this._rQbufferSize * 2, required_buffer_size);
+ this._rQbufferSize = Math.max(this._rQbufferSize * 2, requiredBufferSize);
}
// we don't want to grow unboundedly
if (this._rQbufferSize > MAX_RQ_GROW_SIZE) {
this._rQbufferSize = MAX_RQ_GROW_SIZE;
- if (this._rQbufferSize - this.rQlen < min_fit) {
+ if (this._rQbufferSize - this.rQlen < minFit) {
throw new Error("Receive Queue buffer exceeded " + MAX_RQ_GROW_SIZE + " bytes, and the new message could not fit");
}
}
if (resizeNeeded) {
- const old_rQbuffer = this._rQ.buffer;
+ const oldRQbuffer = this._rQ.buffer;
this._rQ = new Uint8Array(this._rQbufferSize);
- this._rQ.set(new Uint8Array(old_rQbuffer, this._rQi, this._rQlen - this._rQi));
+ this._rQ.set(new Uint8Array(oldRQbuffer, this._rQi, this._rQlen - this._rQi));
} else {
if (ENABLE_COPYWITHIN) {
this._rQ.copyWithin(0, this._rQi, this._rQlen);
}
// push arraybuffer values onto the end of the receive que
- _decode_message(data) {
+ _DecodeMessage(data) {
const u8 = new Uint8Array(data);
if (u8.length > this._rQbufferSize - this._rQlen) {
- this._expand_compact_rQ(u8.length);
+ this._expandCompactRQ(u8.length);
}
this._rQ.set(u8, this._rQlen);
this._rQlen += u8.length;
}
- _recv_message(e) {
- this._decode_message(e.data);
+ _recvMessage(e) {
+ this._DecodeMessage(e.data);
if (this.rQlen > 0) {
this._eventHandlers.message();
if (this._rQlen == this._rQi) {
beforeEach(function () {
sock = new Websock();
// skip init
- sock._allocate_buffers();
+ sock._allocateBuffers();
sock._rQ.set(RQ_TEMPLATE);
sock._rQlen = RQ_TEMPLATE.length;
});
describe('rQpeek8', function () {
it('should peek at the next byte without poping it off the queue', function () {
- const bef_len = sock.rQlen;
+ const befLen = sock.rQlen;
const peek = sock.rQpeek8();
expect(sock.rQpeek8()).to.equal(peek);
- expect(sock.rQlen).to.equal(bef_len);
+ expect(sock.rQlen).to.equal(befLen);
});
});
describe('rQshift8()', function () {
it('should pop a single byte from the receive queue', function () {
const peek = sock.rQpeek8();
- const bef_len = sock.rQlen;
+ const befLen = sock.rQlen;
expect(sock.rQshift8()).to.equal(peek);
- expect(sock.rQlen).to.equal(bef_len - 1);
+ expect(sock.rQlen).to.equal(befLen - 1);
});
});
describe('rQshift16()', function () {
it('should pop two bytes from the receive queue and return a single number', function () {
- const bef_len = sock.rQlen;
+ const befLen = sock.rQlen;
const expected = (RQ_TEMPLATE[0] << 8) + RQ_TEMPLATE[1];
expect(sock.rQshift16()).to.equal(expected);
- expect(sock.rQlen).to.equal(bef_len - 2);
+ expect(sock.rQlen).to.equal(befLen - 2);
});
});
describe('rQshift32()', function () {
it('should pop four bytes from the receive queue and return a single number', function () {
- const bef_len = sock.rQlen;
+ const befLen = sock.rQlen;
const expected = (RQ_TEMPLATE[0] << 24) +
(RQ_TEMPLATE[1] << 16) +
(RQ_TEMPLATE[2] << 8) +
RQ_TEMPLATE[3];
expect(sock.rQshift32()).to.equal(expected);
- expect(sock.rQlen).to.equal(bef_len - 4);
+ expect(sock.rQlen).to.equal(befLen - 4);
});
});
describe('rQshiftStr', function () {
it('should shift the given number of bytes off of the receive queue and return a string', function () {
- const bef_len = sock.rQlen;
- const bef_rQi = sock.rQi;
+ const befLen = sock.rQlen;
+ const befRQi = sock.rQi;
const shifted = sock.rQshiftStr(3);
expect(shifted).to.be.a('string');
- expect(shifted).to.equal(String.fromCharCode.apply(null, Array.prototype.slice.call(new Uint8Array(RQ_TEMPLATE.buffer, bef_rQi, 3))));
- expect(sock.rQlen).to.equal(bef_len - 3);
+ expect(shifted).to.equal(String.fromCharCode.apply(null, Array.prototype.slice.call(new Uint8Array(RQ_TEMPLATE.buffer, befRQi, 3))));
+ expect(sock.rQlen).to.equal(befLen - 3);
});
it('should shift the entire rest of the queue off if no length is given', function () {
describe('rQshiftBytes', function () {
it('should shift the given number of bytes of the receive queue and return an array', function () {
- const bef_len = sock.rQlen;
- const bef_rQi = sock.rQi;
+ const befLen = sock.rQlen;
+ const befRQi = sock.rQi;
const shifted = sock.rQshiftBytes(3);
expect(shifted).to.be.an.instanceof(Uint8Array);
- expect(shifted).to.array.equal(new Uint8Array(RQ_TEMPLATE.buffer, bef_rQi, 3));
- expect(sock.rQlen).to.equal(bef_len - 3);
+ expect(shifted).to.array.equal(new Uint8Array(RQ_TEMPLATE.buffer, befRQi, 3));
+ expect(sock.rQlen).to.equal(befLen - 3);
});
it('should shift the entire rest of the queue off if no length is given', function () {
});
it('should not modify the receive queue', function () {
- const bef_len = sock.rQlen;
+ const befLen = sock.rQlen;
sock.rQslice(0, 2);
- expect(sock.rQlen).to.equal(bef_len);
+ expect(sock.rQlen).to.equal(befLen);
});
it('should return an array containing the given slice of the receive queue', function () {
sock._websocket.readyState = WebSocket.OPEN;
sock._sQ = new Uint8Array([1, 2, 3]);
sock._sQlen = 3;
- const encoded = sock._encode_message();
+ const encoded = sock._encodeMessage();
sock.flush();
expect(sock._websocket.send).to.have.been.calledOnce;
});
});
- describe('send_string', function () {
+ describe('sendString', function () {
beforeEach(function () {
sock.send = sinon.spy();
});
it('should call send after converting the string to an array', function () {
- sock.send_string("\x01\x02\x03");
+ sock.sendString("\x01\x02\x03");
expect(sock.send).to.have.been.calledWith([1, 2, 3]);
});
});
});
describe('lifecycle methods', function () {
- let old_WS;
+ let oldWS;
before(function () {
- old_WS = WebSocket;
+ oldWS = WebSocket;
});
let sock;
sock = new Websock();
// eslint-disable-next-line no-global-assign
WebSocket = sinon.spy();
- WebSocket.OPEN = old_WS.OPEN;
- WebSocket.CONNECTING = old_WS.CONNECTING;
- WebSocket.CLOSING = old_WS.CLOSING;
- WebSocket.CLOSED = old_WS.CLOSED;
+ WebSocket.OPEN = oldWS.OPEN;
+ WebSocket.CONNECTING = oldWS.CONNECTING;
+ WebSocket.CLOSING = oldWS.CLOSING;
+ WebSocket.CLOSED = oldWS.CLOSED;
WebSocket.prototype.binaryType = 'arraybuffer';
});
expect(sock._websocket.close).not.to.have.been.called;
});
- it('should reset onmessage to not call _recv_message', function () {
- sinon.spy(sock, '_recv_message');
+ it('should reset onmessage to not call _recvMessage', function () {
+ sinon.spy(sock, '_recvMessage');
sock.close();
sock._websocket.onmessage(null);
try {
- expect(sock._recv_message).not.to.have.been.called;
+ expect(sock._recvMessage).not.to.have.been.called;
} finally {
- sock._recv_message.restore();
+ sock._recvMessage.restore();
}
});
});
describe('event handlers', function () {
beforeEach(function () {
- sock._recv_message = sinon.spy();
+ sock._recvMessage = sinon.spy();
sock.on('open', sinon.spy());
sock.on('close', sinon.spy());
sock.on('error', sinon.spy());
sock.open('ws://');
});
- it('should call _recv_message on a message', function () {
+ it('should call _recvMessage on a message', function () {
sock._websocket.onmessage(null);
- expect(sock._recv_message).to.have.been.calledOnce;
+ expect(sock._recvMessage).to.have.been.calledOnce;
});
it('should call the open event handler on opening', function () {
after(function () {
// eslint-disable-next-line no-global-assign
- WebSocket = old_WS;
+ WebSocket = oldWS;
});
});
let sock;
beforeEach(function () {
sock = new Websock();
- sock._allocate_buffers();
+ sock._allocateBuffers();
});
it('should support adding binary Uint8Array data to the receive queue', function () {
const msg = { data: new Uint8Array([1, 2, 3]) };
sock._mode = 'binary';
- sock._recv_message(msg);
+ sock._recvMessage(msg);
expect(sock.rQshiftStr(3)).to.equal('\x01\x02\x03');
});
sock._eventHandlers.message = sinon.spy();
const msg = { data: new Uint8Array([1, 2, 3]).buffer };
sock._mode = 'binary';
- sock._recv_message(msg);
+ sock._recvMessage(msg);
expect(sock._eventHandlers.message).to.have.been.calledOnce;
});
sock._eventHandlers.message = sinon.spy();
const msg = { data: new Uint8Array([]).buffer };
sock._mode = 'binary';
- sock._recv_message(msg);
+ sock._recvMessage(msg);
expect(sock._eventHandlers.message).not.to.have.been.called;
});
sock.rQi = 6;
const msg = { data: new Uint8Array([1, 2, 3]).buffer };
sock._mode = 'binary';
- sock._recv_message(msg);
+ sock._recvMessage(msg);
expect(sock._rQlen).to.equal(0);
expect(sock.rQi).to.equal(0);
});
sock.rQi = 10;
const msg = { data: new Uint8Array([1, 2]).buffer };
sock._mode = 'binary';
- sock._recv_message(msg);
+ sock._recvMessage(msg);
expect(sock._rQlen).to.equal(12);
expect(sock.rQi).to.equal(0);
});
sock._rQbufferSize = 20;
const msg = { data: new Uint8Array(30).buffer };
sock._mode = 'binary';
- sock._recv_message(msg);
+ sock._recvMessage(msg);
expect(sock._rQlen).to.equal(30);
expect(sock.rQi).to.equal(0);
expect(sock._rQ.length).to.equal(240); // keep the invariant that rQbufferSize / 8 >= rQlen
sock._rQbufferSize = 20;
const msg = { data: new Uint8Array(6).buffer };
sock._mode = 'binary';
- sock._recv_message(msg);
+ sock._recvMessage(msg);
expect(sock._rQlen).to.equal(6);
expect(sock.rQi).to.equal(0);
expect(sock._rQ.length).to.equal(48);
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._encode_message();
+ const res = sock._encodeMessage();
expect(res).to.array.equal(new Uint8Array([1, 2, 3]));
});