Skip to content

Commit e3ad74e

Browse files
committed
fix: RangeError [ERR_BUFFER_OUT_OF_BOUNDS] "length" is outside of buffer bounds since nodejs 22.7
call buf.utf8Write() conditionally, it will be called in buf.write() ref: #2025
1 parent 19e1fef commit e3ad74e

File tree

2 files changed

+15
-3
lines changed

2 files changed

+15
-3
lines changed

.github/workflows/test.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ jobs:
2323
runs-on: ubuntu-latest
2424
strategy:
2525
matrix:
26-
node_version: ["12", "14", "16", "18"]
26+
node_version: ["12", "14", "16", "18", "20", "22"]
2727
steps:
2828
- uses: actions/checkout@v1
2929
- uses: actions/setup-node@v1

src/writer_buffer.js

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,10 +55,11 @@ BufferWriter.prototype.bytes = function write_bytes_buffer(value) {
5555
};
5656

5757
function writeStringBuffer(val, buf, pos) {
58+
const targetVersion = '22.7.0';
5859
if (val.length < 40) // plain js is faster for short strings (probably due to redundant assertions)
5960
util.utf8.write(val, buf, pos);
60-
else if (buf.utf8Write)
61-
buf.utf8Write(val, pos);
61+
else if (buf.utf8Write && compareVersions(process.version.slice(1), targetVersion) < 0)
62+
buf.utf8Write(val, pos); // node less than 22.7.0
6263
else
6364
buf.write(val, pos);
6465
}
@@ -83,3 +84,14 @@ BufferWriter.prototype.string = function write_string_buffer(value) {
8384
*/
8485

8586
BufferWriter._configure();
87+
88+
function compareVersions(version1, version2) {
89+
const v1Parts = version1.split('.').map(Number);
90+
const v2Parts = version2.split('.').map(Number);
91+
92+
for (let i = 0; i < Math.max(v1Parts.length, v2Parts.length); i+=1) {
93+
if ((v1Parts[i] ?? 0) < (v2Parts[i] ?? 0)) { return -1 }
94+
if ((v1Parts[i] ?? 0) > (v2Parts[i] ?? 0)) { return 1 }
95+
}
96+
return 0;
97+
}

0 commit comments

Comments
 (0)