Skip to content

Updated base64 error to be more descriptive #6746

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

Merged
merged 10 commits into from
Nov 9, 2022
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
5 changes: 5 additions & 0 deletions .changeset/long-shirts-explode.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@firebase/storage": patch
---

Fixed issue where if btoa wasn't supported in the environment, then the user would get a generic message.
7 changes: 7 additions & 0 deletions packages/storage/src/implementation/error.ts
Original file line number Diff line number Diff line change
Expand Up @@ -261,6 +261,13 @@ export function noDownloadURL(): StorageError {
);
}

export function missingPolyFill(polyFill: string): StorageError {
return new StorageError(
StorageErrorCode.UNSUPPORTED_ENVIRONMENT,
`${polyFill} is missing. Make sure to install the required polyfills. See https://firebase.google.com/docs/web/environments-js-sdk#polyfills for more information.`
);
}

/**
* @internal
*/
Expand Down
3 changes: 3 additions & 0 deletions packages/storage/src/implementation/string.ts
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,9 @@ export function base64Bytes_(format: StringFormat, value: string): Uint8Array {
try {
bytes = decodeBase64(value);
} catch (e) {
if ((e as Error).message.includes('polyfill')) {
throw e;
}
throw invalidFormat(format, 'Invalid character found');
}
const array = new Uint8Array(bytes.length);
Expand Down
5 changes: 5 additions & 0 deletions packages/storage/src/platform/browser/base64.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,13 @@
* limitations under the License.
*/

import { missingPolyFill } from '../../implementation/error';

/** Converts a Base64 encoded string to a binary string. */
export function decodeBase64(encoded: string): string {
if (typeof atob === 'undefined') {
throw missingPolyFill('base-64');
}
return atob(encoded);
}

Expand Down
36 changes: 36 additions & 0 deletions packages/storage/test/browser/string.browser.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/**
* @license
* Copyright 2022 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

import { expect } from 'chai';
import { missingPolyFill } from '../../src/implementation/error';
import { dataFromString, StringFormat } from '../../src/implementation/string';
import { assertThrows } from '../unit/testshared';

describe('String browser tests', () => {
it('should reject if atob is undefined', () => {
const originalAToB = global.atob;
// @ts-ignore
global.atob = undefined;
const str = 'CpYlM1-XsGxTd1n6izHMU_yY3Bw=';

const error = assertThrows(() => {
dataFromString(StringFormat.BASE64URL, str);
}, 'storage/unsupported-environment');
expect(error.message).to.equal(missingPolyFill('base-64').message);
global.atob = originalAToB;
});
});