Skip to content

Commit

Permalink
buffer: fix atob/btoa no-arg case
Browse files Browse the repository at this point in the history
PR-URL: #41478
Fixes: #41450
Reviewed-By: Tobias Nießen <tniessen@tnie.de>
Reviewed-By: Filip Skokan <panva.ip@gmail.com>
Reviewed-By: Michaël Zasso <targos@protonmail.com>
  • Loading branch information
benjamingr authored and danielleadams committed Mar 14, 2022
1 parent 44b6927 commit 14bb6f9
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 9 deletions.
7 changes: 7 additions & 0 deletions lib/buffer.js
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ const {
ERR_INVALID_ARG_VALUE,
ERR_INVALID_BUFFER_SIZE,
ERR_OUT_OF_RANGE,
ERR_MISSING_ARGS,
ERR_UNKNOWN_ENCODING
},
hideStackFrames
Expand Down Expand Up @@ -1214,6 +1215,9 @@ function btoa(input) {
// The implementation here has not been performance optimized in any way and
// should not be.
// Refs: https://github.com/nodejs/node/pull/38433#issuecomment-828426932
if (arguments.length === 0) {
throw new ERR_MISSING_ARGS('input');
}
input = `${input}`;
for (let n = 0; n < input.length; n++) {
if (input[n].charCodeAt(0) > 0xff)
Expand All @@ -1230,6 +1234,9 @@ function atob(input) {
// The implementation here has not been performance optimized in any way and
// should not be.
// Refs: https://github.com/nodejs/node/pull/38433#issuecomment-828426932
if (arguments.length === 0) {
throw new ERR_MISSING_ARGS('input');
}
input = `${input}`;
for (let n = 0; n < input.length; n++) {
if (!kBase64Digits.includes(input[n]))
Expand Down
9 changes: 0 additions & 9 deletions test/parallel/test-btoa-atob-global.js

This file was deleted.

14 changes: 14 additions & 0 deletions test/parallel/test-btoa-atob.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
'use strict';

require('../common');

const { strictEqual, throws } = require('assert');
const buffer = require('buffer');

// Exported on the global object
strictEqual(globalThis.atob, buffer.atob);
strictEqual(globalThis.btoa, buffer.btoa);

// Throws type error on no argument passed
throws(() => buffer.atob(), /TypeError/);
throws(() => buffer.btoa(), /TypeError/);

0 comments on commit 14bb6f9

Please sign in to comment.