@@ -24,8 +24,8 @@ import { each } from './util/util';
24
24
* A class that holds metadata about a Repo object
25
25
*/
26
26
export class RepoInfo {
27
- host : string ;
28
- domain : string ;
27
+ private _host : string ;
28
+ private _domain : string ;
29
29
internalHost : string ;
30
30
31
31
/**
@@ -45,77 +45,36 @@ export class RepoInfo {
45
45
public readonly persistenceKey : string = '' ,
46
46
public readonly includeNamespaceInQueryParams : boolean = false
47
47
) {
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 ) ;
50
50
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 ;
60
52
}
61
53
62
54
isCacheableHost ( ) : boolean {
63
55
return this . internalHost . substr ( 0 , 2 ) === 's-' ;
64
56
}
65
57
66
- isDemoHost ( ) {
67
- return this . domain === 'firebaseio-demo.com' ;
68
- }
69
-
70
58
isCustomHost ( ) {
71
59
return (
72
- this . domain !== 'firebaseio.com' && this . domain !== 'firebaseio-demo.com'
60
+ this . _domain !== 'firebaseio.com' &&
61
+ this . _domain !== 'firebaseio-demo.com'
73
62
) ;
74
63
}
75
64
76
- updateHost ( newHost : string ) {
65
+ get host ( ) {
66
+ return this . _host ;
67
+ }
68
+
69
+ set host ( newHost : string ) {
77
70
if ( newHost !== this . internalHost ) {
78
71
this . internalHost = newHost ;
79
72
if ( this . isCacheableHost ( ) ) {
80
- PersistentStorage . set ( 'host:' + this . host , this . internalHost ) ;
73
+ PersistentStorage . set ( 'host:' + this . _host , this . internalHost ) ;
81
74
}
82
75
}
83
76
}
84
77
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 } */
119
78
toString ( ) : string {
120
79
let str = this . toURLString ( ) ;
121
80
if ( this . persistenceKey ) {
@@ -124,7 +83,6 @@ export class RepoInfo {
124
83
return str ;
125
84
}
126
85
127
- /** @return {string } */
128
86
toURLString ( ) : string {
129
87
const protocol = this . secure ? 'https://' : 'http://' ;
130
88
const query = this . includeNamespaceInQueryParams
@@ -133,3 +91,51 @@ export class RepoInfo {
133
91
return `${ protocol } ${ this . host } /${ query } ` ;
134
92
}
135
93
}
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