Skip to content

Don't use process when not available #2277

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 3 commits into from
Oct 22, 2019
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
4 changes: 2 additions & 2 deletions packages/firestore/src/local/indexeddb_mutation_queue.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ import { LocalSerializer } from './local_serializer';
import { MutationQueue } from './mutation_queue';
import { PersistenceTransaction, ReferenceDelegate } from './persistence';
import { PersistencePromise } from './persistence_promise';
import { SimpleDbStore, SimpleDbTransaction } from './simple_db';
import { SimpleDb, SimpleDbStore, SimpleDbTransaction } from './simple_db';

/** A mutation queue for a specific user, backed by IndexedDB. */
export class IndexedDbMutationQueue implements MutationQueue {
Expand Down Expand Up @@ -675,7 +675,7 @@ function convertStreamToken(token: ProtoByteString): string {
if (token instanceof Uint8Array) {
// TODO(b/78771403): Convert tokens to strings during deserialization
assert(
process.env.USE_MOCK_PERSISTENCE === 'YES',
SimpleDb.isMockPersistence(),
'Persisting non-string stream tokens is only supported with mock persistence.'
);
return token.toString();
Expand Down
3 changes: 2 additions & 1 deletion packages/firestore/src/local/local_serializer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ import {
DbUnknownDocument
} from './indexeddb_schema';
import { QueryData, QueryPurpose } from './query_data';
import { SimpleDb } from './simple_db';

/** Serializer for values stored in the LocalStore. */
export class LocalSerializer {
Expand Down Expand Up @@ -251,7 +252,7 @@ export class LocalSerializer {
if (queryData.resumeToken instanceof Uint8Array) {
// TODO(b/78771403): Convert tokens to strings during deserialization
assert(
process.env.USE_MOCK_PERSISTENCE === 'YES',
SimpleDb.isMockPersistence(),
'Persisting non-string stream tokens is only supported with mock persistence .'
);
resumeToken = queryData.resumeToken.toString();
Expand Down
30 changes: 23 additions & 7 deletions packages/firestore/src/local/simple_db.ts
Original file line number Diff line number Diff line change
Expand Up @@ -149,20 +149,25 @@ export class SimpleDb {
if (typeof window === 'undefined' || window.indexedDB == null) {
return false;
}

if (SimpleDb.isMockPersistence()) {
return true;
}

// In some Node environments, `window` is defined, but `window.navigator` is
// not. We don't support IndexedDB persistence in Node if the
// isMockPersistence() check above returns false.
if (window.navigator === undefined) {
return false;
}

// We extensively use indexed array values and compound keys,
// which IE and Edge do not support. However, they still have indexedDB
// defined on the window, so we need to check for them here and make sure
// to return that persistence is not enabled for those browsers.
// For tracking support of this feature, see here:
// https://developer.microsoft.com/en-us/microsoft-edge/platform/status/indexeddbarraysandmultientrysupport/

// If we are running in Node using the IndexedDBShim, `window` is defined,
// but `window.navigator` is not. In this case, we support IndexedDB and
// return `true`.
if (window.navigator === undefined) {
return process.env.USE_MOCK_PERSISTENCE === 'YES';
}

// Check the UA string to find out the browser.
const ua = getUA();

Expand Down Expand Up @@ -197,6 +202,17 @@ export class SimpleDb {
}
}

/**
* Returns true if the backing IndexedDB store is the Node IndexedDBShim
* (see https://github.com/axemclion/IndexedDBShim).
*/
static isMockPersistence(): boolean {
return (
typeof process !== 'undefined' &&
process.env.USE_MOCK_PERSISTENCE === 'YES'
);
}

/** Helper to get a typed SimpleDbStore from a transaction. */
static getStore<KeyType extends IDBValidKey, ValueType extends unknown>(
txn: SimpleDbTransaction,
Expand Down