Skip to content

Commit 0fa52a5

Browse files
Use fetch() in v9 Firestore
1 parent cc2132c commit 0fa52a5

File tree

12 files changed

+69
-68
lines changed

12 files changed

+69
-68
lines changed

packages/firestore/exp/register.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ import { name, version } from '../package.json';
2626
import { setSDKVersion } from '../src/core/version';
2727
import { FirebaseFirestore } from '../src/exp/database';
2828
import { Settings } from '../src/exp/settings';
29+
import { PrivateSettings } from '../src/lite/settings';
2930

3031
declare module '@firebase/component' {
3132
interface NameServiceMapping {
@@ -38,15 +39,14 @@ export function registerFirestore(variant?: string): void {
3839
_registerComponent(
3940
new Component(
4041
'firestore-exp',
41-
(container, { options: settings }: { options?: Settings }) => {
42+
(container, { options: settings }: { options?: PrivateSettings }) => {
4243
const app = container.getProvider('app-exp').getImmediate()!;
4344
const firestoreInstance = new FirebaseFirestore(
4445
app,
4546
container.getProvider('auth-internal')
4647
);
47-
if (settings) {
48-
firestoreInstance._setSettings(settings);
49-
}
48+
settings = { useFetchStreams: true, ...settings };
49+
firestoreInstance._setSettings(settings);
5050
return firestoreInstance;
5151
},
5252
ComponentType.PUBLIC

packages/firestore/rollup.config.exp.js

Lines changed: 2 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ const browserPlugins = function () {
7777
]
7878
}),
7979
json({ preferConst: true }),
80-
terser(util.manglePrivatePropertiesOptions)
80+
terser()
8181
];
8282
};
8383

@@ -97,20 +97,6 @@ const allBuilds = [
9797
},
9898
onwarn: util.onwarn
9999
},
100-
// Node CJS build
101-
{
102-
input: path.resolve('./exp', pkg['main-esm']),
103-
output: {
104-
file: path.resolve('./exp', pkg.main),
105-
format: 'cjs',
106-
sourcemap: true
107-
},
108-
plugins: util.es2017ToEs5Plugins(/* mangled= */ false),
109-
external: util.resolveNodeExterns,
110-
treeshake: {
111-
moduleSideEffects: false
112-
}
113-
},
114100
// Browser build
115101
{
116102
input: './exp/index.ts',
@@ -135,21 +121,7 @@ const allBuilds = [
135121
sourcemap: true
136122
}
137123
],
138-
plugins: util.es2017ToEs5Plugins(/* mangled= */ true),
139-
external: util.resolveBrowserExterns,
140-
treeshake: {
141-
moduleSideEffects: false
142-
}
143-
},
144-
// RN build
145-
{
146-
input: './exp/index.rn.ts',
147-
output: {
148-
file: path.resolve('./exp', pkg['react-native']),
149-
format: 'es',
150-
sourcemap: true
151-
},
152-
plugins: [alias(util.generateAliasConfig('rn')), ...browserPlugins()],
124+
plugins: util.es2017ToEs5Plugins(/* mangled= */ false),
153125
external: util.resolveBrowserExterns,
154126
treeshake: {
155127
moduleSideEffects: false

packages/firestore/src/core/database_info.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@ export class DatabaseInfo {
3030
* when using WebChannel as the network transport.
3131
* @param autoDetectLongPolling - Whether to use the detectBufferingProxy
3232
* option when using WebChannel as the network transport.
33+
* @param useFetchStreams Whether to use the Fetch API instead of
34+
* XMLHTTPRequest
3335
*/
3436
constructor(
3537
readonly databaseId: DatabaseId,
@@ -38,7 +40,8 @@ export class DatabaseInfo {
3840
readonly host: string,
3941
readonly ssl: boolean,
4042
readonly forceLongPolling: boolean,
41-
readonly autoDetectLongPolling: boolean
43+
readonly autoDetectLongPolling: boolean,
44+
readonly useFetchStreams: boolean
4245
) {}
4346
}
4447

packages/firestore/src/lite/components.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,7 @@ export function makeDatabaseInfo(
113113
settings.host,
114114
settings.ssl,
115115
settings.experimentalForceLongPolling,
116-
settings.experimentalAutoDetectLongPolling
116+
settings.experimentalAutoDetectLongPolling,
117+
settings.useFetchStreams
117118
);
118119
}

packages/firestore/src/lite/settings.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,8 @@ export interface PrivateSettings extends Settings {
5858
experimentalForceLongPolling?: boolean;
5959
// Used in firestore@exp
6060
experimentalAutoDetectLongPolling?: boolean;
61+
// Used in firestore@exp
62+
useFetchStreams?: boolean;
6163
}
6264

6365
/**
@@ -80,6 +82,8 @@ export class FirestoreSettings {
8082

8183
readonly ignoreUndefinedProperties: boolean;
8284

85+
readonly useFetchStreams: boolean;
86+
8387
// Can be a google-auth-library or gapi client.
8488
// eslint-disable-next-line @typescript-eslint/no-explicit-any
8589
credentials?: any;
@@ -120,6 +124,7 @@ export class FirestoreSettings {
120124

121125
this.experimentalForceLongPolling = !!settings.experimentalForceLongPolling;
122126
this.experimentalAutoDetectLongPolling = !!settings.experimentalAutoDetectLongPolling;
127+
this.useFetchStreams = !!settings.useFetchStreams;
123128

124129
validateIsNotUsedTogether(
125130
'experimentalForceLongPolling',
@@ -139,7 +144,8 @@ export class FirestoreSettings {
139144
other.experimentalForceLongPolling &&
140145
this.experimentalAutoDetectLongPolling ===
141146
other.experimentalAutoDetectLongPolling &&
142-
this.ignoreUndefinedProperties === other.ignoreUndefinedProperties
147+
this.ignoreUndefinedProperties === other.ignoreUndefinedProperties &&
148+
this.useFetchStreams === other.useFetchStreams
143149
);
144150
}
145151
}

packages/firestore/src/platform/browser/webchannel_connection.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ import {
3030
WebChannel,
3131
WebChannelError,
3232
WebChannelOptions,
33+
FetchXmlHttpFactory,
3334
XhrIo,
3435
getStatEventTarget,
3536
EventTarget,
@@ -62,11 +63,13 @@ const XHR_TIMEOUT_SECS = 15;
6263
export class WebChannelConnection extends RestConnection {
6364
private readonly forceLongPolling: boolean;
6465
private readonly autoDetectLongPolling: boolean;
66+
private readonly useFetchStreams: boolean;
6567

6668
constructor(info: DatabaseInfo) {
6769
super(info);
6870
this.forceLongPolling = info.forceLongPolling;
6971
this.autoDetectLongPolling = info.autoDetectLongPolling;
72+
this.useFetchStreams = info.useFetchStreams;
7073
}
7174

7275
protected performRPCRequest<Req, Resp>(
@@ -194,6 +197,10 @@ export class WebChannelConnection extends RestConnection {
194197
detectBufferingProxy: this.autoDetectLongPolling
195198
};
196199

200+
if (this.useFetchStreams) {
201+
request.xmlHttpFactory = new FetchXmlHttpFactory({});
202+
}
203+
197204
this.modifyHeadersForRequest(request.initMessageHeaders!, token);
198205

199206
// Sending the custom headers we just added to request.initMessageHeaders

packages/webchannel-wrapper/externs/overrides.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,12 @@ goog.net.WebChannel.Options.forceLongPolling;
6868
/** @type {boolean|undefined} */
6969
goog.net.WebChannel.Options.detectBufferingProxy;
7070

71+
/** @type {boolean|undefined} */
72+
goog.net.WebChannel.Options.useFetchStreams;
73+
74+
/** @type {unknown} */
75+
goog.net.WebChannel.Options.xmlHttpFactory;
76+
7177
goog.labs.net.webChannel.requestStats.Event = {};
7278
goog.labs.net.webChannel.requestStats.Event.STAT_EVENT;
7379

packages/webchannel-wrapper/gulpfile.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ const closureDefines = [
4646
// Avoid unsafe eval() calls (https://github.com/firebase/firebase-js-sdk/issues/798)
4747
'goog.json.USE_NATIVE_JSON=true',
4848
// Disable debug logging (saves 8780 bytes).
49-
'goog.DEBUG=false',
49+
'goog.DEBUG=true',
5050
// Disable fallbacks for running async code (saves 1472 bytes).
5151
'goog.ASSUME_NATIVE_PROMISE=true',
5252
// Disables IE8-specific event fallback code (saves 523 bytes).

packages/webchannel-wrapper/package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@
1515
},
1616
"license": "Apache-2.0",
1717
"devDependencies": {
18-
"google-closure-compiler": "20201102.0.1",
19-
"google-closure-library": "20201006.0.0",
18+
"google-closure-compiler": "20210505.0.0",
19+
"google-closure-library": "20210406.0.0",
2020
"gulp": "4.0.2",
2121
"gulp-sourcemaps": "3.0.0",
2222
"rollup": "2.35.1",

packages/webchannel-wrapper/src/index.d.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,3 +130,7 @@ export interface WebChannelTransport {
130130
export function createWebChannelTransport(): WebChannelTransport;
131131

132132
export function getStatEventTarget(): EventTarget;
133+
134+
export class FetchXmlHttpFactory {
135+
constructor(options: {});
136+
}

0 commit comments

Comments
 (0)