Skip to content

Commit

Permalink
Add missing buffer APIs
Browse files Browse the repository at this point in the history
  • Loading branch information
appurva21 committed Aug 29, 2024
1 parent 4f4d73e commit 394ba44
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 2 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
13 changes: 12 additions & 1 deletion lib/vendor/buffer/index.browser.js
Original file line number Diff line number Diff line change
@@ -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
}
6 changes: 5 additions & 1 deletion lib/vendor/buffer/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}
27 changes: 27 additions & 0 deletions test/unit/sandbox-libraries/buffer.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
});

0 comments on commit 394ba44

Please sign in to comment.