buffer: fix atob/btoa no-arg case

PR-URL: https://github.com/nodejs/node/pull/41478
Fixes: https://github.com/nodejs/node/issues/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>
This commit is contained in:
Benjamin Gruenbaum authored and Ruy Adorno committed 2022-02-07 23:29:49 -05:00
1 parent e11a079469
commit 6064b1df5d
3 files changed
+21 -9

No files matched your search

+7
View File
@@ -96,6 +96,7 @@ const {
ERR_INVALID_ARG_VALUE,
ERR_INVALID_BUFFER_SIZE,
ERR_OUT_OF_RANGE,
ERR_MISSING_ARGS,
ERR_UNKNOWN_ENCODING
},
hideStackFrames
@@ -1218,6 +1219,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)
@@ -1234,6 +1238,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]))
-9
View File
@@ -1,9 +0,0 @@
'use strict';
require('../common');
const { strictEqual } = require('assert');
const buffer = require('buffer');
strictEqual(globalThis.atob, buffer.atob);
strictEqual(globalThis.btoa, buffer.btoa);
+14
View File
@@ -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/);