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 Sep 24, 2024
1 parent 9251063 commit 6ac7540
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 3 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:
- GH-1032 Add support for configuring module resolver based on environment
new features:
- GH-1032 Enhanced performance when operating on buffers in Node environment
- GH-1035 Added missing buffer APIs to expose a uniform interface across environments

5.1.1:
date: 2024-08-01
Expand Down
14 changes: 13 additions & 1 deletion lib/vendor/buffer/buffer.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,21 @@
const NOT_IMPLEMENTED = function () {
throw new Error('Not implemented');
};

function getBufferModule (buffer) {
return {
Buffer: buffer.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,
isAscii: NOT_IMPLEMENTED,
isUtf8: NOT_IMPLEMENTED,
resolveObjectURL: NOT_IMPLEMENTED,
transcode: NOT_IMPLEMENTED
}
}

Expand Down
15 changes: 13 additions & 2 deletions lib/vendor/buffer/index.browser.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,18 @@ const getBufferModule = require('./buffer');
const SpecificBuffer = require('./specific-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 = getBufferModule({
...buffer,
Buffer: SpecificBuffer(buffer.Buffer)
})
Buffer: SpecificBuffer(buffer.Buffer),
kStringMaxLength: K_STRING_MAX_LENGTH,
constants: {
MAX_LENGTH: buffer.kMaxLength,
MAX_STRING_LENGTH: K_STRING_MAX_LENGTH
},
File: File,
Blob: Blob
});
8 changes: 8 additions & 0 deletions lib/vendor/buffer/specific-buffer.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
const NOT_IMPLEMENTED = function () {
throw new Error('Not implemented');
};

function SpecificBuffer (_Buffer) {
const Buffer = function () {
if (typeof arguments[0] === 'number') {
Expand Down Expand Up @@ -51,6 +55,10 @@ function SpecificBuffer (_Buffer) {
return _Buffer.byteLength(...arguments);
};

Buffer.copyBytesFrom = NOT_IMPLEMENTED;

Buffer.of = NOT_IMPLEMENTED;

return Buffer;
}

Expand Down
23 changes: 23 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,31 @@ 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 6ac7540

Please sign in to comment.