Skip to content

Commit e4eea3f

Browse files
authored
treat the ns= query param as the namespace name if it is present (#2060)
* treat `ns=` query param as the namespace name if it is present * [AUTOMATED]: Prettier Code Styling * record ns queryparam override in RepoInfo class * [AUTOMATED]: Prettier Code Styling * update database test suite * more descriptive RepoInfo member name * Add to changelog
1 parent 4ebedd0 commit e4eea3f

File tree

5 files changed

+31
-17
lines changed

5 files changed

+31
-17
lines changed

packages/database/CHANGELOG.md

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
# Unreleased
22
- [fixed] Fixed an issue that caused `.info/serverTimeOffset` events not to fire (#2043).
3+
- [changed] Treat `ns` url query parameter as the default Realtime Database
4+
namespace name.
35

46
# 6.3.4
57
- [fixed] Fixed an issue where multi-byte UTF-8 characters would not be written correctly when using `firebase.js` or `firebase-database.js` (#2035).
68

79
# 6.0.0
8-
- [changed] Improved consistency between the type annotations for `Query.on`/`Reference.on`,
9-
`Query.off`/`Reference.off` and `Query.once`/`Reference.once` (#1188, #1204).
10+
- [changed] Improved consistency between the type annotations for `Query.on`/`Reference.on`,
11+
`Query.off`/`Reference.off` and `Query.once`/`Reference.once` (#1188, #1204).

packages/database/src/core/RepoInfo.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,15 +42,20 @@ export class RepoInfo {
4242
public secure: boolean,
4343
public namespace: string,
4444
public webSocketOnly: boolean,
45-
public persistenceKey: string = ''
45+
public persistenceKey: string = '',
46+
public includeNamespaceInQueryParams: boolean = false
4647
) {
4748
this.host = host.toLowerCase();
4849
this.domain = this.host.substr(this.host.indexOf('.') + 1);
4950
this.internalHost = PersistentStorage.get('host:' + host) || this.host;
5051
}
5152

5253
needsQueryParam(): boolean {
53-
return this.host !== this.internalHost || this.isCustomHost();
54+
return (
55+
this.host !== this.internalHost ||
56+
this.isCustomHost() ||
57+
this.includeNamespaceInQueryParams
58+
);
5459
}
5560

5661
isCacheableHost(): boolean {

packages/database/src/core/RepoManager.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ import { FirebaseApp } from '@firebase/app-types';
1919
import { safeGet } from '@firebase/util';
2020
import { Repo } from './Repo';
2121
import { fatal, FIREBASE_DATABASE_EMULATOR_HOST_VAR } from './util/util';
22-
import { parseRepoInfo, parseURL } from './util/libs/parser';
22+
import { parseRepoInfo, parseDatabaseURL } from './util/libs/parser';
2323
import { validateUrl } from './util/validation';
2424
import './Repo_transaction';
2525
import { Database } from '../api/Database';

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

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -69,8 +69,8 @@ function decodeQuery(queryString: string): { [key: string]: string } {
6969
export const parseRepoInfo = function(
7070
dataURL: string
7171
): { repoInfo: RepoInfo; path: Path } {
72-
const parsedUrl = parseURL(dataURL),
73-
namespace = parsedUrl.subdomain;
72+
const parsedUrl = parseDatabaseURL(dataURL),
73+
namespace = parsedUrl.namespace;
7474

7575
if (parsedUrl.domain === 'firebase') {
7676
fatal(
@@ -101,7 +101,9 @@ export const parseRepoInfo = function(
101101
parsedUrl.host,
102102
parsedUrl.secure,
103103
namespace,
104-
webSocketOnly
104+
webSocketOnly,
105+
/*persistenceKey=*/ '',
106+
/*includeNamespaceInQueryParams=*/ namespace != parsedUrl.subdomain
105107
),
106108
path: new Path(parsedUrl.pathString)
107109
};
@@ -110,9 +112,9 @@ export const parseRepoInfo = function(
110112
/**
111113
*
112114
* @param {!string} dataURL
113-
* @return {{host: string, port: number, domain: string, subdomain: string, secure: boolean, scheme: string, pathString: string}}
115+
* @return {{host: string, port: number, domain: string, subdomain: string, secure: boolean, scheme: string, pathString: string, namespace: string}}
114116
*/
115-
export const parseURL = function(
117+
export const parseDatabaseURL = function(
116118
dataURL: string
117119
): {
118120
host: string;
@@ -122,12 +124,14 @@ export const parseURL = function(
122124
secure: boolean;
123125
scheme: string;
124126
pathString: string;
127+
namespace: string;
125128
} {
126129
// Default to empty strings in the event of a malformed string.
127130
let host = '',
128131
domain = '',
129132
subdomain = '',
130-
pathString = '';
133+
pathString = '',
134+
namespace = '';
131135

132136
// Always default to SSL, unless otherwise specified.
133137
let secure = true,
@@ -175,14 +179,16 @@ export const parseURL = function(
175179
// Normalize namespaces to lowercase to share storage / connection.
176180
domain = parts[1];
177181
subdomain = parts[0].toLowerCase();
182+
// We interpret the subdomain of a 3 component URL as the namespace name.
183+
namespace = subdomain;
178184
} else if (parts.length === 2) {
179185
domain = parts[0];
180186
} else if (parts[0].slice(0, colonInd).toLowerCase() === 'localhost') {
181187
domain = 'localhost';
182188
}
183-
// Support `ns` query param if subdomain not already set
184-
if (subdomain === '' && 'ns' in queryParams) {
185-
subdomain = queryParams['ns'];
189+
// Always treat the value of the `ns` as the namespace name if it is present.
190+
if ('ns' in queryParams) {
191+
namespace = queryParams['ns'];
186192
}
187193
}
188194

@@ -193,6 +199,7 @@ export const parseURL = function(
193199
subdomain,
194200
secure,
195201
scheme,
196-
pathString
202+
pathString,
203+
namespace
197204
};
198205
};

packages/database/test/database.test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -81,10 +81,10 @@ describe('Database Tests', function() {
8181
expect(db.ref().toString()).to.equal('http://localhost:80/');
8282
});
8383

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

0 commit comments

Comments
 (0)