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

fix invalid this errors on node:buffer #2907

Merged
merged 1 commit into from
Oct 11, 2024
Merged
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
4 changes: 2 additions & 2 deletions src/node/buffer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ import {
transcode,
} from 'node-internal:internal_buffer';

const atob = globalThis.atob;
const btoa = globalThis.btoa;
const atob = globalThis.atob.bind(globalThis);
const btoa = globalThis.btoa.bind(globalThis);
const Blob = globalThis.Blob;
const File = globalThis.File;

Expand Down
12 changes: 12 additions & 0 deletions src/workerd/api/node/tests/buffer-nodejs-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -6157,3 +6157,15 @@ export const sliceOffsetLimits = {
strictEqual(Buffer.from('abcd').utf8Slice(1, 0).toString(), '');
},
};

// Ref: https://github.com/unjs/unenv/pull/325
// Without `.bind(globalThis)` the following tests fail.
export const invalidThisTests = {
async test() {
const bufferModule = await import('node:buffer');
strictEqual(bufferModule.btoa('hello'), 'aGVsbG8=');
strictEqual(bufferModule.atob('aGVsbG8='), 'hello');
ok(new bufferModule.File([], 'file'));
ok(new bufferModule.Blob([]));
},
};
6 changes: 4 additions & 2 deletions src/workerd/api/node/tests/node-compat-v2-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -83,9 +83,11 @@ export const nodeJsBufferExports = {
// Expected node:buffer exports should be present
const { atob, btoa, Blob } = await import('node:buffer');
assert.notEqual(atob, undefined);
assert.strictEqual(atob, globalThis.atob);
assert.notEqual(btoa, undefined);
assert.strictEqual(btoa, globalThis.btoa);
// We cannot do strictEqual with atob and globalThis.atob
// since, buffer module exports them with `.bind(globalThis)`
assert.strictEqual(typeof atob, 'function');
assert.strictEqual(typeof btoa, 'function');
anonrig marked this conversation as resolved.
Show resolved Hide resolved
assert.notEqual(Blob, undefined);
assert.strictEqual(Blob, globalThis.Blob);
},
Expand Down