Skip to content

Commit 97f7e38

Browse files
authored
Tree-shake RepoInfo (#4515)
1 parent 39a8be6 commit 97f7e38

File tree

6 files changed

+75
-64
lines changed

6 files changed

+75
-64
lines changed

common/api-review/database.api.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
55
```ts
66

7-
import { FirebaseApp } from '@firebase/app-types';
7+
import { FirebaseApp } from '@firebase/app';
88

99
// @public (undocumented)
1010
export interface Database {

packages/database/src/core/RepoInfo.ts

Lines changed: 61 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,8 @@ import { each } from './util/util';
2424
* A class that holds metadata about a Repo object
2525
*/
2626
export class RepoInfo {
27-
host: string;
28-
domain: string;
27+
private _host: string;
28+
private _domain: string;
2929
internalHost: string;
3030

3131
/**
@@ -45,77 +45,36 @@ export class RepoInfo {
4545
public readonly persistenceKey: string = '',
4646
public readonly includeNamespaceInQueryParams: boolean = false
4747
) {
48-
this.host = host.toLowerCase();
49-
this.domain = this.host.substr(this.host.indexOf('.') + 1);
48+
this._host = host.toLowerCase();
49+
this._domain = this._host.substr(this._host.indexOf('.') + 1);
5050
this.internalHost =
51-
(PersistentStorage.get('host:' + host) as string) || this.host;
52-
}
53-
54-
needsQueryParam(): boolean {
55-
return (
56-
this.host !== this.internalHost ||
57-
this.isCustomHost() ||
58-
this.includeNamespaceInQueryParams
59-
);
51+
(PersistentStorage.get('host:' + host) as string) || this._host;
6052
}
6153

6254
isCacheableHost(): boolean {
6355
return this.internalHost.substr(0, 2) === 's-';
6456
}
6557

66-
isDemoHost() {
67-
return this.domain === 'firebaseio-demo.com';
68-
}
69-
7058
isCustomHost() {
7159
return (
72-
this.domain !== 'firebaseio.com' && this.domain !== 'firebaseio-demo.com'
60+
this._domain !== 'firebaseio.com' &&
61+
this._domain !== 'firebaseio-demo.com'
7362
);
7463
}
7564

76-
updateHost(newHost: string) {
65+
get host() {
66+
return this._host;
67+
}
68+
69+
set host(newHost: string) {
7770
if (newHost !== this.internalHost) {
7871
this.internalHost = newHost;
7972
if (this.isCacheableHost()) {
80-
PersistentStorage.set('host:' + this.host, this.internalHost);
73+
PersistentStorage.set('host:' + this._host, this.internalHost);
8174
}
8275
}
8376
}
8477

85-
/**
86-
* Returns the websocket URL for this repo
87-
* @param type of connection
88-
* @param params list
89-
* @return The URL for this repo
90-
*/
91-
connectionURL(type: string, params: { [k: string]: string }): string {
92-
assert(typeof type === 'string', 'typeof type must == string');
93-
assert(typeof params === 'object', 'typeof params must == object');
94-
95-
let connURL: string;
96-
if (type === WEBSOCKET) {
97-
connURL =
98-
(this.secure ? 'wss://' : 'ws://') + this.internalHost + '/.ws?';
99-
} else if (type === LONG_POLLING) {
100-
connURL =
101-
(this.secure ? 'https://' : 'http://') + this.internalHost + '/.lp?';
102-
} else {
103-
throw new Error('Unknown connection type: ' + type);
104-
}
105-
if (this.needsQueryParam()) {
106-
params['ns'] = this.namespace;
107-
}
108-
109-
const pairs: string[] = [];
110-
111-
each(params, (key: string, value: string) => {
112-
pairs.push(key + '=' + value);
113-
});
114-
115-
return connURL + pairs.join('&');
116-
}
117-
118-
/** @return {string} */
11978
toString(): string {
12079
let str = this.toURLString();
12180
if (this.persistenceKey) {
@@ -124,7 +83,6 @@ export class RepoInfo {
12483
return str;
12584
}
12685

127-
/** @return {string} */
12886
toURLString(): string {
12987
const protocol = this.secure ? 'https://' : 'http://';
13088
const query = this.includeNamespaceInQueryParams
@@ -133,3 +91,51 @@ export class RepoInfo {
13391
return `${protocol}${this.host}/${query}`;
13492
}
13593
}
94+
95+
function repoInfoNeedsQueryParam(repoInfo: RepoInfo): boolean {
96+
return (
97+
repoInfo.host !== repoInfo.internalHost ||
98+
repoInfo.isCustomHost() ||
99+
repoInfo.includeNamespaceInQueryParams
100+
);
101+
}
102+
103+
/**
104+
* Returns the websocket URL for this repo
105+
* @param repoInfo - RepoInfo object
106+
* @param type - of connection
107+
* @param params - list
108+
* @returns The URL for this repo
109+
*/
110+
export function repoInfoConnectionURL(
111+
repoInfo: RepoInfo,
112+
type: string,
113+
params: { [k: string]: string }
114+
): string {
115+
assert(typeof type === 'string', 'typeof type must == string');
116+
assert(typeof params === 'object', 'typeof params must == object');
117+
118+
let connURL: string;
119+
if (type === WEBSOCKET) {
120+
connURL =
121+
(repoInfo.secure ? 'wss://' : 'ws://') + repoInfo.internalHost + '/.ws?';
122+
} else if (type === LONG_POLLING) {
123+
connURL =
124+
(repoInfo.secure ? 'https://' : 'http://') +
125+
repoInfo.internalHost +
126+
'/.lp?';
127+
} else {
128+
throw new Error('Unknown connection type: ' + type);
129+
}
130+
if (repoInfoNeedsQueryParam(repoInfo)) {
131+
params['ns'] = repoInfo.namespace;
132+
}
133+
134+
const pairs: string[] = [];
135+
136+
each(params, (key: string, value: string) => {
137+
pairs.push(key + '=' + value);
138+
});
139+
140+
return connURL + pairs.join('&');
141+
}

packages/database/src/realtime/BrowserPollConnection.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ import {
4040
import { base64Encode, stringify, isNodeSdk } from '@firebase/util';
4141

4242
import { Transport } from './Transport';
43-
import { RepoInfo } from '../core/RepoInfo';
43+
import { RepoInfo, repoInfoConnectionURL } from '../core/RepoInfo';
4444
import { StatsCollection } from '../core/stats/StatsCollection';
4545

4646
// URL query parameters associated with longpolling
@@ -116,7 +116,7 @@ export class BrowserPollConnection implements Transport {
116116
this.log_ = logWrapper(connId);
117117
this.stats_ = StatsManager.getCollection(repoInfo);
118118
this.urlFn = (params: { [k: string]: string }) =>
119-
repoInfo.connectionURL(LONG_POLLING, params);
119+
repoInfoConnectionURL(repoInfo, LONG_POLLING, params);
120120
}
121121

122122
/**

packages/database/src/realtime/Connection.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -378,7 +378,7 @@ export class Connection {
378378
const version = handshake.v;
379379
const host = handshake.h;
380380
this.sessionId = handshake.s;
381-
this.repoInfo_.updateHost(host);
381+
this.repoInfo_.host = host;
382382
// if we've already closed the connection, then don't bother trying to progress further
383383
if (this.state_ === RealtimeState.CONNECTING) {
384384
this.conn_.start();
@@ -425,7 +425,7 @@ export class Connection {
425425

426426
private onReset_(host: string) {
427427
this.log_('Reset packet received. New host: ' + host);
428-
this.repoInfo_.updateHost(host);
428+
this.repoInfo_.host = host;
429429
// TODO: if we're already "connected", we need to trigger a disconnect at the next layer up.
430430
// We don't currently support resets after the connection has already been established
431431
if (this.state_ === RealtimeState.CONNECTED) {

packages/database/src/realtime/WebSocketConnection.ts

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

18-
import { RepoInfo } from '../core/RepoInfo';
18+
import { RepoInfo, repoInfoConnectionURL } from '../core/RepoInfo';
1919
import { assert, jsonEval, stringify, isNodeSdk } from '@firebase/util';
2020
import { logWrapper, splitStringBySize } from '../core/util/util';
2121
import { StatsManager } from '../core/stats/StatsManager';
@@ -124,7 +124,7 @@ export class WebSocketConnection implements Transport {
124124
if (lastSessionId) {
125125
urlParams[LAST_SESSION_PARAM] = lastSessionId;
126126
}
127-
return repoInfo.connectionURL(WEBSOCKET, urlParams);
127+
return repoInfoConnectionURL(repoInfo, WEBSOCKET, urlParams);
128128
}
129129

130130
/**

packages/database/test/repoinfo.test.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import {
2424
WEBSOCKET
2525
} from '../src/realtime/Constants';
2626
import { expect } from 'chai';
27+
import { repoInfoConnectionURL } from '../src/core/RepoInfo';
2728

2829
describe('RepoInfo', () => {
2930
it('should return the correct URL', () => {
@@ -33,12 +34,16 @@ describe('RepoInfo', () => {
3334
urlParams[VERSION_PARAM] = PROTOCOL_VERSION;
3435
urlParams[LAST_SESSION_PARAM] = 'test';
3536

36-
const websocketUrl = repoInfo.connectionURL(WEBSOCKET, urlParams);
37+
const websocketUrl = repoInfoConnectionURL(repoInfo, WEBSOCKET, urlParams);
3738
expect(websocketUrl).to.equal(
3839
'wss://test-ns.firebaseio.com/.ws?v=5&ls=test'
3940
);
4041

41-
const longPollingUrl = repoInfo.connectionURL(LONG_POLLING, urlParams);
42+
const longPollingUrl = repoInfoConnectionURL(
43+
repoInfo,
44+
LONG_POLLING,
45+
urlParams
46+
);
4247
expect(longPollingUrl).to.equal(
4348
'https://test-ns.firebaseio.com/.lp?v=5&ls=test'
4449
);

0 commit comments

Comments
 (0)