Skip to content

Commit

Permalink
FirebaseServerAppImpl: guard use of FinalizationRegistry based on its…
Browse files Browse the repository at this point in the history
… availability. (#8335)

There was code that would errantly attempt to allocate a FinalizationRegstiry in FirebaseServerAppImpl regardless of the existence of releaseOnDeref field, and this caused issues in some edge environments.

This change fixes that issue.
  • Loading branch information
DellaBitta committed Jun 28, 2024
1 parent 192561b commit ed1c993
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 5 deletions.
6 changes: 6 additions & 0 deletions .changeset/clever-dryers-double.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
'@firebase/app': patch
'firebase': patch
---

Guard the use of `FinalizationRegistry` in `FirebaseServerApp` initialization based on the availability of `FinalizationRegistry` in the runtime.
13 changes: 8 additions & 5 deletions packages/app/src/firebaseServerApp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ export class FirebaseServerAppImpl
implements FirebaseServerApp
{
private readonly _serverConfig: FirebaseServerAppSettings;
private _finalizationRegistry: FinalizationRegistry<object>;
private _finalizationRegistry: FinalizationRegistry<object> | null;
private _refCount: number;

constructor(
Expand Down Expand Up @@ -67,9 +67,12 @@ export class FirebaseServerAppImpl
...serverConfig
};

this._finalizationRegistry = new FinalizationRegistry(() => {
this.automaticCleanup();
});
this._finalizationRegistry = null;
if (typeof FinalizationRegistry !== 'undefined') {
this._finalizationRegistry = new FinalizationRegistry(() => {
this.automaticCleanup();
});
}

this._refCount = 0;
this.incRefCount(this._serverConfig.releaseOnDeref);
Expand Down Expand Up @@ -97,7 +100,7 @@ export class FirebaseServerAppImpl
return;
}
this._refCount++;
if (obj !== undefined) {
if (obj !== undefined && this._finalizationRegistry !== null) {
this._finalizationRegistry.register(obj, this);
}
}
Expand Down

0 comments on commit ed1c993

Please sign in to comment.