Skip to content

Commit b4d2ab4

Browse files
Merge da7efb8 into b9796f5
2 parents b9796f5 + da7efb8 commit b4d2ab4

File tree

10 files changed

+183
-9
lines changed

10 files changed

+183
-9
lines changed

packages/firebase/src/index.rn.ts

+3
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,10 @@ import { name, version } from '../package.json';
2020

2121
import '../auth';
2222
import '../database';
23+
// TODO(b/158625454): Storage doesn't actually work by default in RN (it uses
24+
// `atob`). We should provide a RN build that works out of the box.
2325
import '../storage';
26+
import '../firestore';
2427

2528
firebase.registerVersion(name, version, 'rn');
2629

packages/firestore/index.node.memory.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ export function registerFirestore(instance: FirebaseNamespace): void {
3535
instance,
3636
(app, auth) => new Firestore(app, auth, new MemoryComponentProvider())
3737
);
38-
instance.registerVersion(name, version);
38+
instance.registerVersion(name, version, 'node');
3939
}
4040

4141
registerFirestore(firebase);

packages/firestore/index.rn.memory.ts

+42
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
/**
2+
* @license
3+
* Copyright 2020 Google LLC
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 firebase from '@firebase/app';
19+
import { FirebaseNamespace } from '@firebase/app-types';
20+
21+
import { Firestore } from './src/api/database';
22+
import { MemoryComponentProvider } from './src/core/component_provider';
23+
import { configureForFirebase } from './src/platform/config';
24+
25+
import './register-module';
26+
import './src/platform_rn/rn_init';
27+
28+
import { name, version } from './package.json';
29+
30+
/**
31+
* Registers the memory-only Firestore build for ReactNative with the components
32+
* framework.
33+
*/
34+
export function registerFirestore(instance: FirebaseNamespace): void {
35+
configureForFirebase(
36+
instance,
37+
(app, auth) => new Firestore(app, auth, new MemoryComponentProvider())
38+
);
39+
instance.registerVersion(name, version, 'rn');
40+
}
41+
42+
registerFirestore(firebase);

packages/firestore/index.rn.ts

+41
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
/**
2+
* @license
3+
* Copyright 2020 Google LLC
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+
import firebase from '@firebase/app';
18+
import { FirebaseNamespace } from '@firebase/app-types';
19+
20+
import { Firestore } from './src/api/database';
21+
import { IndexedDbComponentProvider } from './src/core/component_provider';
22+
import { configureForFirebase } from './src/platform/config';
23+
24+
import './register-module';
25+
import './src/platform_rn/rn_init';
26+
27+
import { name, version } from './package.json';
28+
29+
/**
30+
* Registers the main Firestore ReactNative build with the components framework.
31+
* Persistence can be enabled via `firebase.firestore().enablePersistence()`.
32+
*/
33+
export function registerFirestore(instance: FirebaseNamespace): void {
34+
configureForFirebase(
35+
instance,
36+
(app, auth) => new Firestore(app, auth, new IndexedDbComponentProvider())
37+
);
38+
instance.registerVersion(name, version, 'rn');
39+
}
40+
41+
registerFirestore(firebase);

packages/firestore/memory/package.json

+1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
"description": "A memory-only build of the Cloud Firestore JS SDK.",
44
"main": "../dist/index.memory.node.cjs.js",
55
"main-esm2017": "../dist/index.memory.node.esm2017.js",
6+
"react-native": "../dist/index.memory.rn.esm2017.js",
67
"browser": "../dist/index.memory.cjs.js",
78
"module": "../dist/index.memory.esm.js",
89
"esm2017": "../dist/index.memory.esm2017.js",

packages/firestore/package.json

+1
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141
},
4242
"main": "dist/index.node.cjs.js",
4343
"main-esm2017": "dist/index.node.esm2017.js",
44+
"react-native": "dist/index.rn.esm2017.js",
4445
"browser": "dist/index.cjs.js",
4546
"module": "dist/index.esm.js",
4647
"esm2017": "dist/index.esm2017.js",

packages/firestore/rollup.config.es2017.js

+26-1
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,31 @@ const browserBuilds = [
100100
}
101101
];
102102

103+
const reactNativeBuilds = [
104+
// Persistence build
105+
{
106+
input: 'index.rn.ts',
107+
output: {
108+
file: pkg['react-native'],
109+
format: 'es',
110+
sourcemap: true
111+
},
112+
plugins: browserBuildPlugins,
113+
external: resolveBrowserExterns
114+
},
115+
// Memory-only build
116+
{
117+
input: 'index.rn.memory.ts',
118+
output: {
119+
file: path.resolve('./memory', memoryPkg['react-native']),
120+
format: 'es',
121+
sourcemap: true
122+
},
123+
plugins: browserBuildPlugins,
124+
external: resolveBrowserExterns
125+
}
126+
];
127+
103128
// MARK: Node builds
104129

105130
const nodeBuildPlugins = [
@@ -145,4 +170,4 @@ const nodeBuilds = [
145170
}
146171
];
147172

148-
export default [...browserBuilds, ...nodeBuilds];
173+
export default [...browserBuilds, ...reactNativeBuilds, ...nodeBuilds];

packages/firestore/src/platform_browser/browser_platform.ts

+4-7
Original file line numberDiff line numberDiff line change
@@ -19,21 +19,18 @@ 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';
22+
import { ConnectivityMonitor } from '../remote/connectivity_monitor';
2323

2424
import { NoopConnectivityMonitor } from '../remote/connectivity_monitor_noop';
2525
import { BrowserConnectivityMonitor } from './browser_connectivity_monitor';
2626
import { WebChannelConnection } from './webchannel_connection';
2727
import { debugAssert } from '../util/assert';
2828

2929
// Implements the Platform API for browsers and some browser-like environments
30-
// (including ReactNative).
30+
// (including ReactNative, which has its own ReactNativePlatform that extends
31+
// from this class).
3132
export class BrowserPlatform implements Platform {
32-
readonly base64Available: boolean;
33-
34-
constructor() {
35-
this.base64Available = typeof atob !== 'undefined';
36-
}
33+
readonly base64Available = typeof atob !== 'undefined';
3734

3835
get document(): Document | null {
3936
// `document` is not always available, e.g. in ReactNative and WebWorkers.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/**
2+
* @license
3+
* Copyright 2020 Google LLC
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 { PlatformSupport } from '../platform/platform';
19+
import { ReactNativePlatform } from './rn_platform';
20+
21+
/**
22+
* This code needs to run before Firestore is used. This can be achieved in
23+
* several ways:
24+
* 1) Through the JSCompiler compiling this code and then (automatically)
25+
* executing it before exporting the Firestore symbols.
26+
* 2) Through importing this module first in a Firestore main module
27+
*/
28+
PlatformSupport.setPlatform(new ReactNativePlatform());
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/**
2+
* @license
3+
* Copyright 2020 Google LLC
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 { BrowserPlatform } from '../platform_browser/browser_platform';
19+
import { base64 } from '@firebase/util';
20+
21+
// Implements the Platform API for ReactNative.
22+
export class ReactNativePlatform extends BrowserPlatform {
23+
readonly base64Available = true;
24+
25+
atob(encoded: string): string {
26+
// WebSafe uses a different URL-encoding safe alphabet that doesn't match
27+
// the encoding used on the backend.
28+
return base64.decodeString(encoded, /* webSafe =*/ false);
29+
}
30+
31+
btoa(raw: string): string {
32+
// WebSafe uses a different URL-encoding safe alphabet that doesn't match
33+
// the encoding used on the backend.
34+
return base64.encodeString(raw, /* webSafe =*/ false);
35+
}
36+
}

0 commit comments

Comments
 (0)