diff --git a/CHANGELOG.yaml b/CHANGELOG.yaml index 04bbb424..d3119d33 100644 --- a/CHANGELOG.yaml +++ b/CHANGELOG.yaml @@ -3,6 +3,7 @@ unreleased: - Add support for configuring module resolver based on environment new features: - Enhanced performance when operating on buffers in Node environment + - Added missing buffer APIs to expose a uniform interface across environments 5.1.1: date: 2024-08-01 diff --git a/lib/vendor/buffer/index.browser.js b/lib/vendor/buffer/index.browser.js index 96f052b5..020d35e8 100644 --- a/lib/vendor/buffer/index.browser.js +++ b/lib/vendor/buffer/index.browser.js @@ -1,9 +1,20 @@ const SpecificBuffer = require('./buffer'); const buffer = require('buffer/'); +// Using 32-bit implementation value from Node +// https://github.com/nodejs/node/blob/main/deps/v8/include/v8-primitive.h#L126 +const K_STRING_MAX_LENGTH = (1 << 28) - 16; + module.exports = { Buffer: SpecificBuffer(buffer.Buffer), SlowBuffer: buffer.SlowBuffer, INSPECT_MAX_BYTES: buffer.INSPECT_MAX_BYTES, - kMaxLength: buffer.kMaxLength + kMaxLength: buffer.kMaxLength, + kStringMaxLength: K_STRING_MAX_LENGTH, + constants: { + MAX_LENGTH: buffer.kMaxLength, + MAX_STRING_LENGTH: K_STRING_MAX_LENGTH + }, + File: File, + Blob: Blob } diff --git a/lib/vendor/buffer/index.js b/lib/vendor/buffer/index.js index eed3ce6e..8b70a536 100644 --- a/lib/vendor/buffer/index.js +++ b/lib/vendor/buffer/index.js @@ -5,5 +5,9 @@ module.exports = { Buffer: SpecificBuffer(globalThis.Buffer), SlowBuffer: buffer.SlowBuffer, INSPECT_MAX_BYTES: buffer.INSPECT_MAX_BYTES, - kMaxLength: buffer.kMaxLength + kMaxLength: buffer.kMaxLength, + kStringMaxLength: buffer.kStringMaxLength, + constants: buffer.constants, + File: buffer.File, + Blob: buffer.Blob, } diff --git a/test/unit/sandbox-libraries/buffer.test.js b/test/unit/sandbox-libraries/buffer.test.js index 07599d47..e62479dc 100644 --- a/test/unit/sandbox-libraries/buffer.test.js +++ b/test/unit/sandbox-libraries/buffer.test.js @@ -243,8 +243,35 @@ describe('sandbox library - buffer', function () { buffer = require('buffer'); assert.strictEqual(typeof buffer.kMaxLength, 'number'); + assert.strictEqual(typeof buffer.kStringMaxLength, 'number'); + assert.strictEqual(typeof buffer.constants.MAX_LENGTH, 'number'); + assert.strictEqual(typeof buffer.constants.MAX_STRING_LENGTH, 'number'); assert.strictEqual(typeof buffer.INSPECT_MAX_BYTES, 'number'); `, done); }); + + it('should expose File class', function (done) { + context.execute(` + const assert = require('assert'), + buffer = require('buffer'); + + const lastModified = Date.now(); + const file = new buffer.File([], 'filename.txt', { type: 'text/plain', lastModified }); + + assert.strictEqual(file.name, 'filename.txt'); + assert.strictEqual(file.lastModified, lastModified); + `, done); + }); + + it('should expose Blob class', function (done) { + context.execute(` + const assert = require('assert'), + buffer = require('buffer'); + + const blob = new buffer.Blob(['hello world'], { type: 'text/plain' }); + + assert.strictEqual(blob.size, 11); + `, done); + }); });