Skip to content

Remove dependency on global atob/btoa functions #2677

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

Closed
wants to merge 3 commits into from
Closed
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
1 change: 1 addition & 0 deletions packages/firestore/externs.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
"packages/component/dist/src/component_container.d.ts",
"packages/logger/dist/src/logger.d.ts",
"packages/webchannel-wrapper/src/index.d.ts",
"packages/util/dist/src/crypt.d.ts",
"packages/util/dist/src/environment.d.ts",
"packages/firestore/src/protos/firestore_proto_api.d.ts",
"packages/firestore/dist/lib/src/util/error.d.ts",
Expand Down
13 changes: 0 additions & 13 deletions packages/firestore/src/api/blob.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,16 +39,6 @@ function assertUint8ArrayAvailable(): void {
}
}

/** Helper function to assert Base64 functions are available at runtime. */
function assertBase64Available(): void {
if (!PlatformSupport.getPlatform().base64Available) {
throw new FirestoreError(
Code.UNIMPLEMENTED,
'Blobs are unavailable in Firestore in this environment.'
);
}
}

/**
* Immutable class holding a blob (binary data).
* This class is directly exposed in the public API.
Expand All @@ -64,14 +54,12 @@ export class Blob {
private _binaryString: string;

private constructor(binaryString: string) {
assertBase64Available();
this._binaryString = binaryString;
}

static fromBase64String(base64: string): Blob {
validateExactNumberOfArgs('Blob.fromBase64String', arguments, 1);
validateArgType('Blob.fromBase64String', 'string', 1, base64);
assertBase64Available();
try {
const binaryString = PlatformSupport.getPlatform().atob(base64);
return new Blob(binaryString);
Expand All @@ -95,7 +83,6 @@ export class Blob {

toBase64(): string {
validateExactNumberOfArgs('Blob.toBase64', arguments, 0);
assertBase64Available();
return PlatformSupport.getPlatform().btoa(this._binaryString);
}

Expand Down
3 changes: 0 additions & 3 deletions packages/firestore/src/platform/platform.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,6 @@ export interface Platform {

/** The Platform's 'document' implementation or null if not available. */
readonly document: Document | null;

/** True if and only if the Base64 conversion functions are available. */
readonly base64Available: boolean;
}

/**
Expand Down
11 changes: 3 additions & 8 deletions packages/firestore/src/platform_browser/browser_platform.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,9 @@ import { ConnectivityMonitor } from './../remote/connectivity_monitor';
import { NoopConnectivityMonitor } from '../remote/connectivity_monitor_noop';
import { BrowserConnectivityMonitor } from './browser_connectivity_monitor';
import { WebChannelConnection } from './webchannel_connection';
import { base64 } from '@firebase/util';

export class BrowserPlatform implements Platform {
readonly base64Available: boolean;

constructor() {
this.base64Available = typeof atob !== 'undefined';
}

get document(): Document | null {
return typeof document !== 'undefined' ? document : null;
}
Expand Down Expand Up @@ -61,10 +56,10 @@ export class BrowserPlatform implements Platform {
}

atob(encoded: string): string {
return atob(encoded);
return base64.decodeString(encoded, /*webSafe=*/ false);
}

btoa(raw: string): string {
return btoa(raw);
return base64.encodeString(raw, /*webSafe=*/ false);
}
}
2 changes: 0 additions & 2 deletions packages/firestore/src/platform_node/node_platform.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,6 @@ import { GrpcConnection } from './grpc_connection';
import { loadProtos } from './load_protos';

export class NodePlatform implements Platform {
readonly base64Available = true;

readonly document = null;

get window(): Window | null {
Expand Down
4 changes: 0 additions & 4 deletions packages/firestore/test/util/test_platform.ts
Original file line number Diff line number Diff line change
Expand Up @@ -229,10 +229,6 @@ export class TestPlatform implements Platform {
return this.mockWindow as any; //eslint-disable-line @typescript-eslint/no-explicit-any
}

get base64Available(): boolean {
return this.basePlatform.base64Available;
}

raiseVisibilityEvent(visibility: VisibilityState): void {
if (this.mockDocument) {
this.mockDocument.raiseVisibilityEvent(visibility);
Expand Down