Skip to content

Commit 55ecffe

Browse files
Use Fetch in firestore@beta (#4939)
1 parent 22c2621 commit 55ecffe

File tree

8 files changed

+33
-8
lines changed

8 files changed

+33
-8
lines changed

packages/firestore/exp/register.ts

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ import { Component, ComponentType } from '@firebase/component';
2525
import { name, version } from '../package.json';
2626
import { setSDKVersion } from '../src/core/version';
2727
import { FirebaseFirestore } from '../src/exp/database';
28-
import { Settings } from '../src/exp/settings';
28+
import { PrivateSettings } from '../src/lite/settings';
2929

3030
declare module '@firebase/component' {
3131
interface NameServiceMapping {
@@ -38,15 +38,14 @@ export function registerFirestore(variant?: string): void {
3838
_registerComponent(
3939
new Component(
4040
'firestore-exp',
41-
(container, { options: settings }: { options?: Settings }) => {
41+
(container, { options: settings }: { options?: PrivateSettings }) => {
4242
const app = container.getProvider('app-exp').getImmediate()!;
4343
const firestoreInstance = new FirebaseFirestore(
4444
app,
4545
container.getProvider('auth-internal')
4646
);
47-
if (settings) {
48-
firestoreInstance._setSettings(settings);
49-
}
47+
settings = { useFetchStreams: true, ...settings };
48+
firestoreInstance._setSettings(settings);
5049
return firestoreInstance;
5150
},
5251
ComponentType.PUBLIC

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: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,9 @@ goog.net.WebChannel.Options.forceLongPolling;
6868
/** @type {boolean|undefined} */
6969
goog.net.WebChannel.Options.detectBufferingProxy;
7070

71+
/** @type {unknown} */
72+
goog.net.WebChannel.Options.xmlHttpFactory;
73+
7174
goog.labs.net.webChannel.requestStats.Event = {};
7275
goog.labs.net.webChannel.requestStats.Event.STAT_EVENT;
7376

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+
}

packages/webchannel-wrapper/src/index.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ goog.provide('firebase.webchannel.wrapper');
2323

2424
// goog.net.WebChannelTransport
2525
goog.require('goog.net.createWebChannelTransport');
26+
goog.require('goog.net.FetchXmlHttpFactory');
2627
goog.require('goog.labs.net.webChannel.requestStats');
2728
goog.require('goog.labs.net.webChannel.WebChannelBaseTransport');
2829

@@ -83,5 +84,6 @@ module['exports']['ErrorCode'] = goog.net.ErrorCode;
8384
module['exports']['EventType'] = goog.net.EventType;
8485
module['exports']['Event'] = goog.labs.net.webChannel.requestStats.Event;
8586
module['exports']['Stat'] = goog.labs.net.webChannel.requestStats.Stat;
87+
module['exports']['FetchXmlHttpFactory'] = goog.net.FetchXmlHttpFactory;
8688
module['exports']['WebChannel'] = goog.net.WebChannel;
8789
module['exports']['XhrIo'] = goog.net.XhrIo;

0 commit comments

Comments
 (0)