Skip to content

Commit d73a86c

Browse files
committed
Feedback
1 parent 3ccba53 commit d73a86c

File tree

2 files changed

+34
-15
lines changed

2 files changed

+34
-15
lines changed

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

Lines changed: 33 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,25 @@ function decodePath(pathString: string): string {
3737
return pathStringDecoded;
3838
}
3939

40+
/**
41+
* @param {!string} queryString
42+
* @return {!Map<string, string>} key value hash
43+
*/
44+
function decodeQuery(queryString: string): Map<string, string> {
45+
let results = new Map<string, string>();
46+
const pieces = queryString.split('&');
47+
for (let i = 0; i < pieces.length; i++) {
48+
if (pieces[i].length > 0) {
49+
try {
50+
const piece = decodeURIComponent(pieces[i]);
51+
const kv = piece.split('=');
52+
results.set(kv[0], kv[1]);
53+
} catch (e) {}
54+
}
55+
}
56+
return results;
57+
}
58+
4059
/**
4160
*
4261
* @param {!string} dataURL
@@ -119,13 +138,21 @@ export const parseURL = function(
119138
dataURL = dataURL.substring(colonInd + 2);
120139
}
121140

122-
// Parse host and path.
141+
// Parse host, path, and query string.
123142
let slashInd = dataURL.indexOf('/');
124143
if (slashInd === -1) {
125144
slashInd = dataURL.length;
126145
}
127-
host = dataURL.substring(0, slashInd);
128-
pathString = decodePath(dataURL.substring(slashInd));
146+
let questionInd = dataURL.indexOf('?');
147+
if (questionInd === -1) {
148+
questionInd = dataURL.length;
149+
}
150+
host = dataURL.substring(0, Math.min(slashInd, questionInd));
151+
if (slashInd < questionInd) {
152+
// For pathString, questionInd will always come after slashInd
153+
pathString = decodePath(dataURL.substring(slashInd, questionInd));
154+
}
155+
let queryParams = decodeQuery(dataURL.substring(Math.min(dataURL.length, questionInd + 1)));
129156

130157
// If we have a port, use scheme for determining if it's secure.
131158
colonInd = host.indexOf(':');
@@ -147,17 +174,9 @@ export const parseURL = function(
147174
domain = 'localhost';
148175
}
149176
// Support `ns` query param if subdomain not already set
150-
const queryStartIndex = host.indexOf('?');
151-
if (queryStartIndex > 0) {
152-
if (subdomain === '') {
153-
const queryString = host.substring(queryStartIndex, host.length);
154-
const match = queryString.match(/ns=([a-zA-Z0-9-]+)/);
155-
if (match.length > 0) {
156-
subdomain = match[1];
157-
}
158-
}
159-
// Always remove all query params from the host
160-
host = host.substring(0, queryStartIndex);
177+
if (subdomain === '' && queryParams.has('ns')) {
178+
subdomain = queryParams.get('ns');
179+
console.log("here", queryParams, subdomain);
161180
}
162181
}
163182

packages/database/test/database.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ describe('Database Tests', function() {
7777
});
7878

7979
it('Can read ns query param', function() {
80-
var db = defaultApp.database('http://localhost:80?ns=foo');
80+
var db = defaultApp.database('http://localhost:80/?ns=foo');
8181
expect(db).to.be.ok;
8282
expect(db.repo_.repoInfo_.namespace).to.equal('foo');
8383
expect(db.ref().toString()).to.equal('http://localhost:80/');

0 commit comments

Comments
 (0)