Skip to content

Commit

Permalink
Updated base64 error to be more descriptive (#6746)
Browse files Browse the repository at this point in the history
  • Loading branch information
maneesht committed Nov 9, 2022
1 parent bec38d9 commit 8876b78
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 0 deletions.
5 changes: 5 additions & 0 deletions .changeset/long-shirts-explode.md
@@ -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
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
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
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
@@ -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;
});
});

0 comments on commit 8876b78

Please sign in to comment.