Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Exposed missing buffer globals for compatibility #1035

Open
wants to merge 2 commits into
base: feature/fix-buffer
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ unreleased:
fixed bugs:
- GH-1036 Fixed `uncaughtException` event listener not being removed
- GH-1034 Fixed an issue where sandbox crashes for large response body
- GH-1035 Added missing buffer APIs to expose a uniform interface across environments
chores:
- GH-1033 Update ci.yml to run coverage on latest node version
- GH-1032 Add support for configuring module resolver based on environment
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);
});
});
Loading