@@ -24,8 +24,8 @@ import { each } from './util/util';
2424 * A class that holds metadata about a Repo object
2525 */
2626export 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+ }
0 commit comments