From 237d678cdb1249bcf8d7361ff780e2c3e3d46d89 Mon Sep 17 00:00:00 2001 From: Chocobozzz Date: Fri, 19 Jan 2024 10:17:42 +0100 Subject: [PATCH] Fix compatibility with node 20.11 Node removed buffer_list object and uses a raw array now to store readable buffers: https://github.com/nodejs/node/pull/50341 --- src/client.js | 13 ++++++++++--- src/connection.js | 13 ++++++++++--- src/interfaces.js | 15 +++++++++++---- 3 files changed, 31 insertions(+), 10 deletions(-) diff --git a/src/client.js b/src/client.js index 7a26f23..fa2c79b 100644 --- a/src/client.js +++ b/src/client.js @@ -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); @@ -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")); diff --git a/src/connection.js b/src/connection.js index 038a4ce..0ab2da2 100644 --- a/src/connection.js +++ b/src/connection.js @@ -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); @@ -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(); diff --git a/src/interfaces.js b/src/interfaces.js index d6eee53..ae08030 100644 --- a/src/interfaces.js +++ b/src/interfaces.js @@ -154,12 +154,19 @@ 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) { @@ -167,7 +174,7 @@ module.exports = { } num += byte; } - } while((head = head.next)); + } while(currentBuffer = (currentBuffer.next || readable.buffer[++currentBufferIndex])); return false; }, _drain() {