From 7ca8fc5caa3480f2290a8c92d281819cc57116a6 Mon Sep 17 00:00:00 2001 From: Patrick McAndrew Date: Sun, 8 Oct 2023 13:12:18 +0100 Subject: [PATCH] add missing Buffer constants (#312) --- index.js | 16 ++++++++++++++++ test/node/test-buffer.js | 21 ++++++++++++++------- 2 files changed, 30 insertions(+), 7 deletions(-) diff --git a/index.js b/index.js index cc6371f..4b441fa 100644 --- a/index.js +++ b/index.js @@ -22,6 +22,22 @@ exports.INSPECT_MAX_BYTES = 50 const K_MAX_LENGTH = 0x7fffffff exports.kMaxLength = K_MAX_LENGTH +/** + * Not used internally, but exported to maintain api compatability + * Uses 32-bit implementation value from Node defined in String:kMaxLength + * + * @see https://github.com/nodejs/node/blob/main/deps/v8/include/v8-primitive.h#L126 + * @see https://github.com/nodejs/node/blob/main/src/node_buffer.cc#L1298 + * @see https://github.com/nodejs/node/blob/main/lib/buffer.js#L142 + */ +const K_STRING_MAX_LENGTH = (1 << 28) - 16 +exports.kStringMaxLength = K_STRING_MAX_LENGTH + +exports.constants = { + MAX_LENGTH: K_MAX_LENGTH, + MAX_STRING_LENGTH: K_STRING_MAX_LENGTH +} + /** * If `Buffer.TYPED_ARRAY_SUPPORT`: * === true Use Uint8Array implementation (fastest) diff --git a/test/node/test-buffer.js b/test/node/test-buffer.js index 3a8c452..7fe62bf 100644 --- a/test/node/test-buffer.js +++ b/test/node/test-buffer.js @@ -4,6 +4,7 @@ var Buffer = require('../../').Buffer; var common = { skip: function () {} }; var assert = require('assert'); +const buffer = require('../../'); var Buffer = require('../../').Buffer; var SlowBuffer = require('../../').SlowBuffer; @@ -442,10 +443,10 @@ for (var i = 0; i < Buffer.byteLength(utf8String); i++) { { // Bug regression test var testValue = '\u00F6\u65E5\u672C\u8A9E'; // ö日本語 - var buffer = new Buffer(32); - var size = buffer.write(testValue, 0, 'utf8'); -// console.log('bytes written to buffer: ' + size); - var slice = buffer.toString('utf8', 0, size); + var testBuffer = new Buffer(32); + var size = testBuffer.write(testValue, 0, 'utf8'); +// console.log('bytes written to testBuffer: ' + size); + var slice = testBuffer.toString('utf8', 0, size); assert.equal(slice, testValue); } @@ -1050,12 +1051,12 @@ Buffer(Buffer(0), 0, 0); // GH-5110 { - var buffer = new Buffer('test'); - var string = JSON.stringify(buffer); + var testBuffer = new Buffer('test'); + var string = JSON.stringify(testBuffer); assert.strictEqual(string, '{"type":"Buffer","data":[116,101,115,116]}'); - assert.deepStrictEqual(buffer, JSON.parse(string, function(key, value) { + assert.deepStrictEqual(testBuffer, JSON.parse(string, function(key, value) { return value && value.type === 'Buffer' ? new Buffer(value.data) : value; @@ -1498,3 +1499,9 @@ assert.throws(() => Buffer(-100), assert.throws(() => Buffer(-1), '"size" argument must not be negative'); +// Verify constants +assert.equal(0x7fffffff, buffer.kMaxLength) +assert.equal(buffer.kMaxLength, buffer.constants.MAX_LENGTH) + +assert.equal((1 << 28) - 16, buffer.kStringMaxLength) +assert.equal(buffer.kStringMaxLength, buffer.constants.MAX_STRING_LENGTH) -- 2.34.1