Skip to content

Commit

Permalink
fix: Improve btoa detection
Browse files Browse the repository at this point in the history
  • Loading branch information
CookieCookson committed Apr 15, 2024
1 parent af9ac0b commit 511eda1
Show file tree
Hide file tree
Showing 2 changed files with 2 additions and 3 deletions.
2 changes: 1 addition & 1 deletion src/helpers/toBasicAuth/toBasicAuth.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
export function toBasicAuth(username: string, password: string): string {
const credentials = `${username}:${password}`;
if (typeof window !== "undefined" && window.btoa) {
if (typeof btoa === "function") {
return `Basic ${btoa(credentials)}`;
} else if (typeof Buffer !== "undefined") {
return `Basic ${Buffer.from(credentials, "binary").toString("base64")}`;
Expand Down
3 changes: 1 addition & 2 deletions src/helpers/toBasicAuth/toBasicAuth.unit.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,7 @@ test("converts username and password into Basic Auth header", () => {
});

test("throws error if environment not supported", () => {
if (typeof window !== "undefined") window.btoa = undefined;
// eslint-disable-next-line no-global-assign
if (typeof btoa === "function") btoa = undefined;
if (Buffer) Buffer = undefined;
expect(() => toBasicAuth("", "")).toThrowError();
});

0 comments on commit 511eda1

Please sign in to comment.