Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

treat the ns= query param as the namespace name if it is present #2060

Merged
merged 9 commits into from
Aug 10, 2019
9 changes: 7 additions & 2 deletions packages/database/src/core/RepoInfo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,15 +42,20 @@ export class RepoInfo {
public secure: boolean,
public namespace: string,
public webSocketOnly: boolean,
public persistenceKey: string = ''
public persistenceKey: string = '',
public includeNamespaceInQueryParams: boolean = false
) {
this.host = host.toLowerCase();
this.domain = this.host.substr(this.host.indexOf('.') + 1);
this.internalHost = PersistentStorage.get('host:' + host) || this.host;
}

needsQueryParam(): boolean {
return this.host !== this.internalHost || this.isCustomHost();
return (
this.host !== this.internalHost ||
this.isCustomHost() ||
this.includeNamespaceInQueryParams
);
}

isCacheableHost(): boolean {
Expand Down
2 changes: 1 addition & 1 deletion packages/database/src/core/RepoManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import { FirebaseApp } from '@firebase/app-types';
import { safeGet } from '@firebase/util';
import { Repo } from './Repo';
import { fatal } from './util/util';
import { parseRepoInfo, parseURL } from './util/libs/parser';
import { parseRepoInfo, parseDatabaseURL } from './util/libs/parser';
import { validateUrl } from './util/validation';
import './Repo_transaction';
import { Database } from '../api/Database';
Expand Down
27 changes: 17 additions & 10 deletions packages/database/src/core/util/libs/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,8 @@ function decodeQuery(queryString: string): { [key: string]: string } {
export const parseRepoInfo = function(
dataURL: string
): { repoInfo: RepoInfo; path: Path } {
const parsedUrl = parseURL(dataURL),
namespace = parsedUrl.subdomain;
const parsedUrl = parseDatabaseURL(dataURL),
namespace = parsedUrl.namespace;

if (parsedUrl.domain === 'firebase') {
fatal(
Expand Down Expand Up @@ -101,7 +101,9 @@ export const parseRepoInfo = function(
parsedUrl.host,
parsedUrl.secure,
namespace,
webSocketOnly
webSocketOnly,
/*persistenceKey=*/ '',
/*includeNamespaceInQueryParams=*/ namespace != parsedUrl.subdomain
),
path: new Path(parsedUrl.pathString)
};
Expand All @@ -110,9 +112,9 @@ export const parseRepoInfo = function(
/**
*
* @param {!string} dataURL
* @return {{host: string, port: number, domain: string, subdomain: string, secure: boolean, scheme: string, pathString: string}}
* @return {{host: string, port: number, domain: string, subdomain: string, secure: boolean, scheme: string, pathString: string, namespace: string}}
*/
export const parseURL = function(
export const parseDatabaseURL = function(
dataURL: string
): {
host: string;
Expand All @@ -122,12 +124,14 @@ export const parseURL = function(
secure: boolean;
scheme: string;
pathString: string;
namespace: string;
jmwski marked this conversation as resolved.
Show resolved Hide resolved
} {
// Default to empty strings in the event of a malformed string.
let host = '',
domain = '',
subdomain = '',
pathString = '';
pathString = '',
namespace = '';

// Always default to SSL, unless otherwise specified.
let secure = true,
Expand Down Expand Up @@ -175,14 +179,16 @@ export const parseURL = function(
// Normalize namespaces to lowercase to share storage / connection.
domain = parts[1];
subdomain = parts[0].toLowerCase();
// We interpret the subdomain of a 3 component URL as the namespace name.
namespace = subdomain;
} else if (parts.length === 2) {
domain = parts[0];
} else if (parts[0].slice(0, colonInd).toLowerCase() === 'localhost') {
domain = 'localhost';
}
// Support `ns` query param if subdomain not already set
if (subdomain === '' && 'ns' in queryParams) {
subdomain = queryParams['ns'];
// Always treat the value of the `ns` as the namespace name if it is present.
if ('ns' in queryParams) {
namespace = queryParams['ns'];
}
}

Expand All @@ -193,6 +199,7 @@ export const parseURL = function(
subdomain,
secure,
scheme,
pathString
pathString,
namespace
};
};
4 changes: 2 additions & 2 deletions packages/database/test/database.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,10 +81,10 @@ describe('Database Tests', function() {
expect(db.ref().toString()).to.equal('http://localhost:80/');
});

it('Only reads ns query param when subdomain not set', function() {
it('Reads ns query param even when subdomain is set', function() {
var db = defaultApp.database('http://bar.firebaseio.com?ns=foo');
expect(db).to.be.ok;
expect(db.repo_.repoInfo_.namespace).to.equal('bar');
expect(db.repo_.repoInfo_.namespace).to.equal('foo');
expect(db.ref().toString()).to.equal('https://bar.firebaseio.com/');
});

Expand Down