Skip to content

Commit d5eacb6

Browse files
Don't use process when not available (#2277)
1 parent 3264492 commit d5eacb6

File tree

3 files changed

+27
-10
lines changed

3 files changed

+27
-10
lines changed

packages/firestore/src/local/indexeddb_mutation_queue.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ import { LocalSerializer } from './local_serializer';
4747
import { MutationQueue } from './mutation_queue';
4848
import { PersistenceTransaction, ReferenceDelegate } from './persistence';
4949
import { PersistencePromise } from './persistence_promise';
50-
import { SimpleDbStore, SimpleDbTransaction } from './simple_db';
50+
import { SimpleDb, SimpleDbStore, SimpleDbTransaction } from './simple_db';
5151

5252
/** A mutation queue for a specific user, backed by IndexedDB. */
5353
export class IndexedDbMutationQueue implements MutationQueue {
@@ -675,7 +675,7 @@ function convertStreamToken(token: ProtoByteString): string {
675675
if (token instanceof Uint8Array) {
676676
// TODO(b/78771403): Convert tokens to strings during deserialization
677677
assert(
678-
process.env.USE_MOCK_PERSISTENCE === 'YES',
678+
SimpleDb.isMockPersistence(),
679679
'Persisting non-string stream tokens is only supported with mock persistence.'
680680
);
681681
return token.toString();

packages/firestore/src/local/local_serializer.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ import {
4343
DbUnknownDocument
4444
} from './indexeddb_schema';
4545
import { QueryData, QueryPurpose } from './query_data';
46+
import { SimpleDb } from './simple_db';
4647

4748
/** Serializer for values stored in the LocalStore. */
4849
export class LocalSerializer {
@@ -251,7 +252,7 @@ export class LocalSerializer {
251252
if (queryData.resumeToken instanceof Uint8Array) {
252253
// TODO(b/78771403): Convert tokens to strings during deserialization
253254
assert(
254-
process.env.USE_MOCK_PERSISTENCE === 'YES',
255+
SimpleDb.isMockPersistence(),
255256
'Persisting non-string stream tokens is only supported with mock persistence .'
256257
);
257258
resumeToken = queryData.resumeToken.toString();

packages/firestore/src/local/simple_db.ts

Lines changed: 23 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -149,20 +149,25 @@ export class SimpleDb {
149149
if (typeof window === 'undefined' || window.indexedDB == null) {
150150
return false;
151151
}
152+
153+
if (SimpleDb.isMockPersistence()) {
154+
return true;
155+
}
156+
157+
// In some Node environments, `window` is defined, but `window.navigator` is
158+
// not. We don't support IndexedDB persistence in Node if the
159+
// isMockPersistence() check above returns false.
160+
if (window.navigator === undefined) {
161+
return false;
162+
}
163+
152164
// We extensively use indexed array values and compound keys,
153165
// which IE and Edge do not support. However, they still have indexedDB
154166
// defined on the window, so we need to check for them here and make sure
155167
// to return that persistence is not enabled for those browsers.
156168
// For tracking support of this feature, see here:
157169
// https://developer.microsoft.com/en-us/microsoft-edge/platform/status/indexeddbarraysandmultientrysupport/
158170

159-
// If we are running in Node using the IndexedDBShim, `window` is defined,
160-
// but `window.navigator` is not. In this case, we support IndexedDB and
161-
// return `true`.
162-
if (window.navigator === undefined) {
163-
return process.env.USE_MOCK_PERSISTENCE === 'YES';
164-
}
165-
166171
// Check the UA string to find out the browser.
167172
const ua = getUA();
168173

@@ -197,6 +202,17 @@ export class SimpleDb {
197202
}
198203
}
199204

205+
/**
206+
* Returns true if the backing IndexedDB store is the Node IndexedDBShim
207+
* (see https://github.com/axemclion/IndexedDBShim).
208+
*/
209+
static isMockPersistence(): boolean {
210+
return (
211+
typeof process !== 'undefined' &&
212+
process.env.USE_MOCK_PERSISTENCE === 'YES'
213+
);
214+
}
215+
200216
/** Helper to get a typed SimpleDbStore from a transaction. */
201217
static getStore<KeyType extends IDBValidKey, ValueType extends unknown>(
202218
txn: SimpleDbTransaction,

0 commit comments

Comments
 (0)