Skip to content

Commit 799b99d

Browse files
committed
Merge remote-tracking branch 'origin/master' into fz/storage-list
2 parents 7b62273 + eebbc9a commit 799b99d

File tree

13 files changed

+192
-32
lines changed

13 files changed

+192
-32
lines changed

packages/firebase/package.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,14 +38,16 @@
3838
"react-native": "dist/index.rn.cjs.js",
3939
"dependencies": {
4040
"@firebase/app": "0.4.1",
41+
"@firebase/app-types": "0.4.0",
4142
"@firebase/auth": "0.11.2",
4243
"@firebase/database": "0.4.1",
4344
"@firebase/firestore": "1.3.1",
4445
"@firebase/functions": "0.4.7",
4546
"@firebase/messaging": "0.3.20",
4647
"@firebase/polyfill": "0.3.14",
4748
"@firebase/storage": "0.2.16",
48-
"@firebase/performance": "0.2.2"
49+
"@firebase/performance": "0.2.2",
50+
"@firebase/util": "0.2.15"
4951
},
5052
"devDependencies": {
5153
"git-rev-sync": "1.12.0",

packages/firestore/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,8 @@
3131
"@firebase/firestore-types": "1.3.0",
3232
"@firebase/logger": "0.1.14",
3333
"@firebase/webchannel-wrapper": "0.2.20",
34+
"@grpc/grpc-js": "0.4.0",
3435
"@grpc/proto-loader": "^0.5.0",
35-
"grpc": "1.20.3",
3636
"tslib": "1.9.3"
3737
},
3838
"peerDependencies": {

packages/firestore/src/platform/platform.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import { ProtoByteString } from '../core/types';
2020
import { Connection } from '../remote/connection';
2121
import { JsonProtoSerializer } from '../remote/serializer';
2222
import { fail } from '../util/assert';
23+
import { ConnectivityMonitor } from './../remote/connectivity_monitor';
2324

2425
/**
2526
* Provides a common interface to load anything platform dependent, e.g.
@@ -31,6 +32,7 @@ import { fail } from '../util/assert';
3132
// use in our client.
3233
export interface Platform {
3334
loadConnection(databaseInfo: DatabaseInfo): Promise<Connection>;
35+
newConnectivityMonitor(): ConnectivityMonitor;
3436
newSerializer(databaseId: DatabaseId): JsonProtoSerializer;
3537

3638
/** Formats an object as a JSON string, suitable for logging. */
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
/**
2+
* @license
3+
* Copyright 2019 Google Inc.
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
import { debug } from '../util/log';
19+
import {
20+
ConnectivityMonitor,
21+
ConnectivityMonitorCallback,
22+
NetworkStatus
23+
} from './../remote/connectivity_monitor';
24+
25+
const LOG_TAG = 'ConnectivityMonitor';
26+
27+
/**
28+
* Browser implementation of ConnectivityMonitor.
29+
*/
30+
export class BrowserConnectivityMonitor implements ConnectivityMonitor {
31+
private readonly networkAvailableListener = () => this.onNetworkAvailable();
32+
private readonly networkUnavailableListener = () =>
33+
this.onNetworkUnavailable();
34+
private callbacks: ConnectivityMonitorCallback[] = [];
35+
36+
constructor() {
37+
this.configureNetworkMonitoring();
38+
}
39+
40+
addCallback(callback: (status: NetworkStatus) => void): void {
41+
this.callbacks.push(callback);
42+
}
43+
44+
shutdown(): void {
45+
window.removeEventListener('online', this.networkAvailableListener);
46+
window.removeEventListener('offline', this.networkUnavailableListener);
47+
}
48+
49+
private configureNetworkMonitoring(): void {
50+
window.addEventListener('online', this.networkAvailableListener);
51+
window.addEventListener('offline', this.networkUnavailableListener);
52+
}
53+
54+
private onNetworkAvailable(): void {
55+
debug(LOG_TAG, 'Network connectivity changed: AVAILABLE');
56+
for (const callback of this.callbacks) {
57+
callback(NetworkStatus.AVAILABLE);
58+
}
59+
}
60+
61+
private onNetworkUnavailable(): void {
62+
debug(LOG_TAG, 'Network connectivity changed: UNAVAILABLE');
63+
for (const callback of this.callbacks) {
64+
callback(NetworkStatus.UNAVAILABLE);
65+
}
66+
}
67+
}

packages/firestore/src/platform_browser/browser_platform.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,9 @@ import { DatabaseId, DatabaseInfo } from '../core/database_info';
1919
import { Platform } from '../platform/platform';
2020
import { Connection } from '../remote/connection';
2121
import { JsonProtoSerializer } from '../remote/serializer';
22+
import { ConnectivityMonitor } from './../remote/connectivity_monitor';
2223

24+
import { BrowserConnectivityMonitor } from './browser_connectivity_monitor';
2325
import { WebChannelConnection } from './webchannel_connection';
2426

2527
export class BrowserPlatform implements Platform {
@@ -43,6 +45,10 @@ export class BrowserPlatform implements Platform {
4345
return Promise.resolve(new WebChannelConnection(databaseInfo));
4446
}
4547

48+
newConnectivityMonitor(): ConnectivityMonitor {
49+
return new BrowserConnectivityMonitor();
50+
}
51+
4652
newSerializer(databaseId: DatabaseId): JsonProtoSerializer {
4753
return new JsonProtoSerializer(databaseId, { useProto3Json: true });
4854
}

packages/firestore/src/platform_node/grpc_connection.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,12 @@
1515
* limitations under the License.
1616
*/
1717

18-
import * as grpc from 'grpc';
18+
import * as grpc from '@grpc/grpc-js';
1919

2020
import firebase from '@firebase/app';
2121
const SDK_VERSION = firebase.SDK_VERSION;
2222

23-
const grpcVersion = require('grpc/package.json').version;
23+
const grpcVersion = require('@grpc/grpc-js/package.json').version;
2424

2525
import { Token } from '../api/credentials';
2626
import { DatabaseInfo } from '../core/database_info';

packages/firestore/src/platform_node/load_protos.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@
1515
* limitations under the License.
1616
*/
1717

18+
import * as grpc from '@grpc/grpc-js';
1819
import * as protoLoader from '@grpc/proto-loader';
19-
import * as grpc from 'grpc';
2020
import * as path from 'path';
2121
import * as ProtobufJS from 'protobufjs';
2222

packages/firestore/src/platform_node/node_platform.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ import { Platform } from '../platform/platform';
2222
import { Connection } from '../remote/connection';
2323
import { JsonProtoSerializer } from '../remote/serializer';
2424
import { Code, FirestoreError } from '../util/error';
25+
import { ConnectivityMonitor } from './../remote/connectivity_monitor';
26+
import { NoopConnectivityMonitor } from './../remote/connectivity_monitor_noop';
2527

2628
import { GrpcConnection } from './grpc_connection';
2729
import { loadProtos } from './load_protos';
@@ -46,6 +48,10 @@ export class NodePlatform implements Platform {
4648
return Promise.resolve(new GrpcConnection(protos, databaseInfo));
4749
}
4850

51+
newConnectivityMonitor(): ConnectivityMonitor {
52+
return new NoopConnectivityMonitor();
53+
}
54+
4955
newSerializer(partitionId: DatabaseId): JsonProtoSerializer {
5056
return new JsonProtoSerializer(partitionId, { useProto3Json: false });
5157
}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
/**
2+
* @license
3+
* Copyright 2019 Google Inc.
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
/**
19+
* The set of network states is deliberately simplified -- we only care about
20+
* states such that transition between them should break currently
21+
* established connections.
22+
*/
23+
export const enum NetworkStatus {
24+
AVAILABLE,
25+
UNAVAILABLE
26+
}
27+
28+
export type ConnectivityMonitorCallback = (status: NetworkStatus) => void;
29+
30+
/**
31+
* A base class for monitoring changes in network connectivity; it is expected
32+
* that each platform will have its own system-dependent implementation.
33+
*/
34+
export interface ConnectivityMonitor {
35+
/**
36+
* Adds a callback to be called when connectivity changes.
37+
*
38+
* Callbacks are not made on the initial state of connectivity, since this
39+
* monitor is primarily used for resetting backoff in the remote store when
40+
* connectivity changes. As such, the initial connectivity state is
41+
* irrelevant here.
42+
*/
43+
addCallback(callback: ConnectivityMonitorCallback): void;
44+
45+
/**
46+
* Stops monitoring connectivity. After this call completes, no further
47+
* callbacks will be triggered. After shutdown() is called, no further calls
48+
* are allowed on this instance.
49+
*/
50+
shutdown(): void;
51+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/**
2+
* @license
3+
* Copyright 2019 Google Inc.
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
import { ConnectivityMonitor, NetworkStatus } from './connectivity_monitor';
19+
20+
export class NoopConnectivityMonitor implements ConnectivityMonitor {
21+
addCallback(callback: (status: NetworkStatus) => void): void {
22+
// No-op.
23+
}
24+
25+
shutdown(): void {
26+
// No-op.
27+
}
28+
}

packages/firestore/test/util/test_platform.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ import { Platform } from '../../src/platform/platform';
2121
import { Connection } from '../../src/remote/connection';
2222
import { JsonProtoSerializer } from '../../src/remote/serializer';
2323
import { assert, fail } from '../../src/util/assert';
24+
import { ConnectivityMonitor } from './../../src/remote/connectivity_monitor';
25+
import { NoopConnectivityMonitor } from './../../src/remote/connectivity_monitor_noop';
2426

2527
/**
2628
* `Window` fake that implements the event and storage API that is used by
@@ -245,6 +247,10 @@ export class TestPlatform implements Platform {
245247
return this.basePlatform.loadConnection(databaseInfo);
246248
}
247249

250+
newConnectivityMonitor(): ConnectivityMonitor {
251+
return new NoopConnectivityMonitor();
252+
}
253+
248254
newSerializer(databaseId: DatabaseId): JsonProtoSerializer {
249255
return this.basePlatform.newSerializer(databaseId);
250256
}

packages/testing/src/api/index.ts

Lines changed: 12 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -20,19 +20,21 @@ import * as request from 'request';
2020
import { base64 } from '@firebase/util';
2121
import { setLogLevel, LogLevel } from '@firebase/logger';
2222
import * as grpc from 'grpc';
23+
import * as protoLoader from '@grpc/proto-loader';
2324
import { resolve } from 'path';
24-
import * as fs from 'fs';
2525

2626
export { database, firestore } from 'firebase';
2727

28-
const PROTO_ROOT = {
29-
root: resolve(
30-
__dirname,
31-
process.env.FIRESTORE_EMULATOR_PROTO_ROOT || '../protos'
32-
),
33-
file: 'google/firestore/emulator/v1/firestore_emulator.proto'
34-
};
35-
const PROTOS = grpc.load(PROTO_ROOT, /* format = */ 'proto');
28+
const PROTO_ROOT = resolve(
29+
__dirname,
30+
process.env.FIRESTORE_EMULATOR_PROTO_ROOT || '../protos'
31+
);
32+
const PROTO_FILE = resolve(
33+
PROTO_ROOT,
34+
'google/firestore/emulator/v1/firestore_emulator.proto'
35+
);
36+
const PKG_DEF = protoLoader.loadSync(PROTO_FILE, { includeDirs: [PROTO_ROOT] });
37+
const PROTOS = grpc.loadPackageDefinition(PKG_DEF);
3638
const EMULATOR = PROTOS['google']['firestore']['emulator']['v1'];
3739

3840
/** If this environment variable is set, use it for the database emulator's address. */
@@ -202,13 +204,7 @@ export function loadFirestoreRules(
202204

203205
let client = new EMULATOR.FirestoreEmulator(
204206
FIRESTORE_ADDRESS,
205-
grpc.credentials.createInsecure(),
206-
{
207-
// Cap how much backoff gRPC will perform. This is testing code, so
208-
// efficiency is less important than responsiveness.
209-
'grpc.initial_reconnect_backoff_ms': 100,
210-
'grpc.max_reconnect_backoff_ms': 100
211-
}
207+
grpc.credentials.createInsecure()
212208
);
213209
return new Promise((resolve, reject) => {
214210
client.setSecurityRules(

yarn.lock

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -893,6 +893,13 @@
893893
through2 "^2.0.0"
894894
xdg-basedir "^3.0.0"
895895

896+
897+
version "0.4.0"
898+
resolved "https://registry.npmjs.org/@grpc/grpc-js/-/grpc-js-0.4.0.tgz#b20a6abf1346ce7fd696a042f83d10c94e143881"
899+
integrity sha512-UbGDPnstJamJrSiHzCSwSavIX260IfLOZLRJYDqRKJA/jmVZa3hPMWDjhFrcCKDq2MLc/O/nauFED3r4khcZrA==
900+
dependencies:
901+
semver "^6.0.0"
902+
896903
"@grpc/proto-loader@^0.5.0":
897904
version "0.5.0"
898905
resolved "https://registry.npmjs.org/@grpc/proto-loader/-/proto-loader-0.5.0.tgz#6d21930530db6089ed68a90f10a22b76fdc3387d"
@@ -6901,17 +6908,6 @@ [email protected]:
69016908
node-pre-gyp "^0.13.0"
69026909
protobufjs "^5.0.3"
69036910

6904-
6905-
version "1.20.3"
6906-
resolved "https://registry.npmjs.org/grpc/-/grpc-1.20.3.tgz#a74d36718f1e89c4a64f2fb9441199c3c8f78978"
6907-
integrity sha512-GsEsi0NVj6usS/xor8pF/xDbDiwZQR59aZl5NUZ59Sy2bdPQFZ3UePr5wevZjHboirRCIQCKRI1cCgvSWUe2ag==
6908-
dependencies:
6909-
lodash.camelcase "^4.3.0"
6910-
lodash.clone "^4.5.0"
6911-
nan "^2.13.2"
6912-
node-pre-gyp "^0.13.0"
6913-
protobufjs "^5.0.3"
6914-
69156911
gtoken@^1.2.1, gtoken@^1.2.3:
69166912
version "1.2.3"
69176913
resolved "https://registry.npmjs.org/gtoken/-/gtoken-1.2.3.tgz#5509571b8afd4322e124cf66cf68115284c476d8"

0 commit comments

Comments
 (0)