Skip to content

Commit bb11710

Browse files
committed
Refactor DbgpConnection chunk buffering
Save chunks in an array instead of overwriting the buffer everytime
1 parent 207686a commit bb11710

File tree

1 file changed

+19
-13
lines changed

1 file changed

+19
-13
lines changed

src/dbgp.ts

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -22,15 +22,15 @@ export abstract class DbgpConnection {
2222

2323
private _socket: net.Socket;
2424
private _parsingState: ParsingState;
25-
private _dataLengthBuffer: Buffer;
26-
private _responseBuffer: Buffer;
25+
private _chunksDataLength: number;
26+
private _chunks: Buffer[];
2727
private _dataLength: number;
2828

2929
constructor(socket: net.Socket) {
3030
this._socket = socket;
3131
this._parsingState = ParsingState.DataLength;
32-
this._dataLengthBuffer = new Buffer(0);
33-
this._responseBuffer = new Buffer(0);
32+
this._chunksDataLength = 0;
33+
this._chunks = [];
3434
socket.on('data', (data: Buffer) => this._handleDataChunk(data));
3535
}
3636

@@ -43,8 +43,9 @@ export abstract class DbgpConnection {
4343
if (nullByteIndex !== -1) {
4444
// YES -> we received the data length and are ready to receive the response
4545
this._dataLength = parseInt(iconv.decode(data.slice(0, nullByteIndex), ENCODING));
46-
// reset buffer
47-
this._dataLengthBuffer = new Buffer(0);
46+
// reset buffered chunks
47+
this._chunks = [];
48+
this._chunksDataLength = 0;
4849
// switch to response parsing state
4950
this._parsingState = ParsingState.Response;
5051
// if data contains more info (except the NULL byte)
@@ -54,20 +55,24 @@ export abstract class DbgpConnection {
5455
this._handleDataChunk(rest);
5556
}
5657
} else {
57-
// NO -> this is only part of the data length. We buffer it and wait for the next data event
58-
this._dataLengthBuffer = Buffer.concat([this._dataLengthBuffer, data], this._dataLengthBuffer.length + data.length);
58+
// NO -> this is only part of the data length. We wait for the next data event
59+
this._chunks.push(data);
60+
this._chunksDataLength += data.length;
5961
}
6062
} else if (this._parsingState === ParsingState.Response) {
6163
// does the new data together with the buffered data add up to the data length?
62-
if (this._responseBuffer.length + data.length >= this._dataLength) {
64+
if (this._chunksDataLength + data.length >= this._dataLength) {
6365
// YES -> we received the whole response
64-
const lastResponsePiece = data.slice(0, this._dataLength - this._responseBuffer.length);
6566
// append the last piece of the response
66-
const response = Buffer.concat([this._responseBuffer, lastResponsePiece], this._dataLength);
67+
const lastResponsePiece = data.slice(0, this._dataLength - this._chunksDataLength);
68+
this._chunks.push(lastResponsePiece);
69+
this._chunksDataLength += data.length;
70+
const response = Buffer.concat(this._chunks, this._chunksDataLength);
6771
// call response handler
6872
this.handleResponse(parseResponse(response));
6973
// reset buffer
70-
this._responseBuffer = new Buffer(0);
74+
this._chunks = [];
75+
this._chunksDataLength = 0;
7176
// switch to data length parsing state
7277
this._parsingState = ParsingState.DataLength;
7378
// if data contains more info (except the NULL byte)
@@ -78,7 +83,8 @@ export abstract class DbgpConnection {
7883
}
7984
} else {
8085
// NO -> this is not the whole response yet. We buffer it and wait for the next data event.
81-
this._responseBuffer = Buffer.concat([this._responseBuffer, data], this._responseBuffer.length + data.length);
86+
this._chunks.push(data);
87+
this._chunksDataLength += data.length;
8288
}
8389
}
8490
}

0 commit comments

Comments
 (0)