Skip to content

fix(app): Allow Object.prototype member names as app names #70

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

Merged
merged 1 commit into from
Jun 20, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 7 additions & 4 deletions src/app/firebase_app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,10 @@ export interface FirebaseNamespace {
}
}

const contains = function(obj, key) {
return Object.prototype.hasOwnProperty.call(obj, key);
};

let LocalPromise = local.Promise as typeof Promise;

const DEFAULT_ENTRY_NAME = '[DEFAULT]';
Expand Down Expand Up @@ -438,11 +442,10 @@ export function createFirebaseNamespace(): FirebaseNamespace {
*/
function app(name?: string): FirebaseApp {
name = name || DEFAULT_ENTRY_NAME;
let result = apps_[name];
if (result === undefined) {
if (!contains(apps_, name)) {
error('no-app', {'name': name});
}
return result;
return apps_[name];
}

patchProperty(app, 'App', FirebaseAppImpl);
Expand All @@ -458,7 +461,7 @@ export function createFirebaseNamespace(): FirebaseNamespace {
error('bad-app-name', {'name': name + ''});
}
}
if (apps_[name!] !== undefined) {
if (contains(apps_, name) ) {
error('duplicate-app', {'name': name});
}

Expand Down
13 changes: 13 additions & 0 deletions tests/app/unit/firebase_app.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,19 @@ describe("Firebase App Class", () => {
});
});

it("OK to use Object.prototype member names as app name.", () => {
let app = firebase.initializeApp({}, 'toString');
assert.equal(firebase.apps.length, 1);
assert.equal(app.name, 'toString');
assert.strictEqual(firebase.app('toString'), app);
});

it("Error to get uninitialized app using Object.prototype member name.", () => {
assert.throws(() => {
firebase.app('toString');
}, /'toString'.*created/i);
});

it("Only calls createService on first use (per app).", () => {
let registrations = 0;
firebase.INTERNAL.registerService('test', (app: FirebaseApp) => {
Expand Down