Skip to content

Commit

Permalink
Fix compatibility with node 20.11
Browse files Browse the repository at this point in the history
Node removed buffer_list object and uses a raw array now to store
readable buffers: nodejs/node#50341
  • Loading branch information
Chocobozzz committed Jan 19, 2024
1 parent 2f4405b commit 237d678
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 10 deletions.
13 changes: 10 additions & 3 deletions src/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -131,10 +131,17 @@ class Client extends Emitter {
if(!test) { return; }
if(test.toString() === "HTTP/1.1 101 Switching Protocols") {
const CRLF = Buffer.from("\r\n\r\n");
let head = socket._readableState.buffer.head;
let buff = Buffer.allocUnsafe(0);

// Compatibility between node < 20.11 and node >= 20.11
const readable = socket._readableState
let currentBufferIndex = readable.bufferIndex
let currentBuffer = readable.buffer.head || readable.buffer[currentBufferIndex];

do {
buff = Buffer.concat([buff, head.data]);
const data = currentBuffer.data || currentBuffer

buff = Buffer.concat([buff, data]);
const index = buff.indexOf(CRLF);
if(index > -1) {
const headers = socket.read(index + 4);
Expand All @@ -143,7 +150,7 @@ class Client extends Emitter {
this._init();
return;
}
} while((head = head.next));
} while(currentBuffer = (currentBuffer.next || readable.buffer[++currentBufferIndex]));
} else {
const str = test.toString();
this._error = str.slice(0, str.indexOf("\r\n"));
Expand Down
13 changes: 10 additions & 3 deletions src/connection.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,17 @@ class Connection {
const string = test.toString();
if(string === "GET") {
const CRLF = Buffer.from("\r\n\r\n");
let head = socket._readableState.buffer.head;
let buff = Buffer.allocUnsafe(0);

// Compatibility between node < 20.11 and node >= 20.11
const readable = socket._readableState
let currentBufferIndex = readable.bufferIndex
let currentBuffer = readable.buffer.head || readable.buffer[currentBufferIndex];

do {
buff = Buffer.concat([buff, head.data]);
const data = currentBuffer.data || currentBuffer

buff = Buffer.concat([buff, data]);
const index = buff.indexOf(CRLF);
if(index > -1) {
const headers = socket.read(index + 4);
Expand All @@ -47,7 +54,7 @@ class Connection {
socket.end("HTTP/1.1 418 I'm a Teapot");
}
}
} while((head = head.next));
} while(currentBuffer = (currentBuffer.next || readable.buffer[++currentBufferIndex]));
} else if(string === "IPC") {
socket._events[ConnectionEvents.DATA] = this._read.bind(this);
this._read();
Expand Down
15 changes: 11 additions & 4 deletions src/interfaces.js
Original file line number Diff line number Diff line change
Expand Up @@ -154,20 +154,27 @@ module.exports = {
return tag.reverse();
},
_untag() {
let head = this.connection._readableState.buffer.head;
let num = 0;
let size = 0;

// Compatibility between node < 20.11 and node >= 20.11
const readable = this.connection._readableState
let currentBufferIndex = readable.bufferIndex
let currentBuffer = readable.buffer.head || readable.buffer[currentBufferIndex];

do {
for(let i = 0; i < head.data.length; i++) {
const byte = head.data[i];
const data = currentBuffer.data || currentBuffer

for(let i = 0; i < data.length; i++) {
const byte = data[i];
num *= 128;
size++;
if(byte > 127) {
return [size, num + (byte & 127)];
}
num += byte;
}
} while((head = head.next));
} while(currentBuffer = (currentBuffer.next || readable.buffer[++currentBufferIndex]));
return false;
},
_drain() {
Expand Down

0 comments on commit 237d678

Please sign in to comment.