Skip to content

Commit 94c20ef

Browse files
Merge branch 'master' into firestore-multi-tab
2 parents d425ffb + db389db commit 94c20ef

File tree

4 files changed

+448
-54
lines changed

4 files changed

+448
-54
lines changed

packages/database/src/core/RepoInfo.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ export class RepoInfo {
4949
}
5050

5151
needsQueryParam(): boolean {
52-
return this.host !== this.internalHost;
52+
return this.host !== this.internalHost || this.isCustomHost();
5353
}
5454

5555
isCacheableHost(): boolean {

packages/database/src/core/util/libs/parser.ts

+38-4
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616

1717
import { Path } from '../Path';
1818
import { RepoInfo } from '../../RepoInfo';
19-
import { warnIfPageIsSecure, fatal } from '../util';
19+
import { warnIfPageIsSecure, warn, fatal } from '../util';
2020

2121
/**
2222
* @param {!string} pathString
@@ -37,6 +37,26 @@ function decodePath(pathString: string): string {
3737
return pathStringDecoded;
3838
}
3939

40+
/**
41+
* @param {!string} queryString
42+
* @return {!{[key:string]:string}} key value hash
43+
*/
44+
function decodeQuery(queryString: string): { [key: string]: string } {
45+
let results = {};
46+
if (queryString.startsWith('?')) {
47+
queryString = queryString.substring(1);
48+
}
49+
for (const segment of queryString.split('&')) {
50+
const kv = segment.split('=');
51+
if (kv.length === 2) {
52+
results[decodeURIComponent(kv[0])] = decodeURIComponent(kv[1]);
53+
} else {
54+
warn('Invalid query string segment: ' + segment);
55+
}
56+
}
57+
return results;
58+
}
59+
4060
/**
4161
*
4262
* @param {!string} dataURL
@@ -119,13 +139,23 @@ export const parseURL = function(
119139
dataURL = dataURL.substring(colonInd + 2);
120140
}
121141

122-
// Parse host and path.
142+
// Parse host, path, and query string.
123143
let slashInd = dataURL.indexOf('/');
124144
if (slashInd === -1) {
125145
slashInd = dataURL.length;
126146
}
127-
host = dataURL.substring(0, slashInd);
128-
pathString = decodePath(dataURL.substring(slashInd));
147+
let questionMarkInd = dataURL.indexOf('?');
148+
if (questionMarkInd === -1) {
149+
questionMarkInd = dataURL.length;
150+
}
151+
host = dataURL.substring(0, Math.min(slashInd, questionMarkInd));
152+
if (slashInd < questionMarkInd) {
153+
// For pathString, questionMarkInd will always come after slashInd
154+
pathString = decodePath(dataURL.substring(slashInd, questionMarkInd));
155+
}
156+
let queryParams = decodeQuery(
157+
dataURL.substring(Math.min(dataURL.length, questionMarkInd))
158+
);
129159

130160
// If we have a port, use scheme for determining if it's secure.
131161
colonInd = host.indexOf(':');
@@ -146,6 +176,10 @@ export const parseURL = function(
146176
} else if (parts[0].slice(0, colonInd).toLowerCase() === 'localhost') {
147177
domain = 'localhost';
148178
}
179+
// Support `ns` query param if subdomain not already set
180+
if (subdomain === '' && 'ns' in queryParams) {
181+
subdomain = queryParams['ns'];
182+
}
149183
}
150184

151185
return {

packages/database/test/database.test.ts

+14
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,20 @@ describe('Database Tests', function() {
7676
expect(db.ref().toString()).to.equal('https://localhost/');
7777
});
7878

79+
it('Can read ns query param', function() {
80+
var db = defaultApp.database('http://localhost:80/?ns=foo&unused=true');
81+
expect(db).to.be.ok;
82+
expect(db.repo_.repoInfo_.namespace).to.equal('foo');
83+
expect(db.ref().toString()).to.equal('http://localhost:80/');
84+
});
85+
86+
it('Only reads ns query param when subdomain not set', function() {
87+
var db = defaultApp.database('http://bar.firebaseio.com?ns=foo');
88+
expect(db).to.be.ok;
89+
expect(db.repo_.repoInfo_.namespace).to.equal('bar');
90+
expect(db.ref().toString()).to.equal('https://bar.firebaseio.com/');
91+
});
92+
7993
it('Different instances for different URLs', function() {
8094
var db1 = defaultApp.database('http://foo1.bar.com');
8195
var db2 = defaultApp.database('http://foo2.bar.com');

0 commit comments

Comments
 (0)