Skip to content

Commit

Permalink
fix invalid this errors on node:buffer
Browse files Browse the repository at this point in the history
  • Loading branch information
anonrig committed Oct 11, 2024
1 parent d0d4af1 commit 19f52a7
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 4 deletions.
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');
assert.notEqual(Blob, undefined);
assert.strictEqual(Blob, globalThis.Blob);
},
Expand Down

0 comments on commit 19f52a7

Please sign in to comment.