@@ -28,6 +28,7 @@ import { isMobileCordova, isReactNative, isNodeSdk } from '@firebase/util';
28
28
import { ServerActions } from './ServerActions' ;
29
29
import { AuthTokenProvider } from './AuthTokenProvider' ;
30
30
import { RepoInfo } from './RepoInfo' ;
31
+ import { FIREBASE_DATABASE_EMULATOR_HOST_VAR } from './RepoManager' ;
31
32
import { Query } from '../api/Query' ;
32
33
import { SDK_VERSION } from './version' ;
33
34
@@ -41,6 +42,8 @@ const SERVER_KILL_INTERRUPT_REASON = 'server_kill';
41
42
// If auth fails repeatedly, we'll assume something is wrong and log a warning / back off.
42
43
const INVALID_AUTH_TOKEN_THRESHOLD = 3 ;
43
44
45
+ const FIREBASE_DATABASE_EMULATOR_HOST_VAR = 'FIREBASE_DATABASE_EMULATOR_HOST' ;
46
+
44
47
interface ListenSpec {
45
48
onComplete ( s : string , p ?: any ) : void ;
46
49
@@ -740,41 +743,58 @@ export class PersistentConnection extends ServerActions {
740
743
const forceRefresh = this . forceTokenRefresh_ ;
741
744
this . forceTokenRefresh_ = false ;
742
745
743
- // First fetch auth token, and establish connection after fetching the token was successful
744
- this . authTokenProvider_
745
- . getToken ( forceRefresh )
746
- . then ( function ( result ) {
747
- if ( ! canceled ) {
748
- log ( 'getToken() completed. Creating connection.' ) ;
749
- self . authToken_ = result && result . accessToken ;
750
- connection = new Connection (
751
- connId ,
752
- self . repoInfo_ ,
753
- onDataMessage ,
754
- onReady ,
755
- onDisconnect ,
756
- /* onKill= */ function ( reason ) {
757
- warn ( reason + ' (' + self . repoInfo_ . toString ( ) + ')' ) ;
758
- self . interrupt ( SERVER_KILL_INTERRUPT_REASON ) ;
759
- } ,
760
- lastSessionId
761
- ) ;
762
- } else {
763
- log ( 'getToken() completed but was canceled' ) ;
764
- }
765
- } )
766
- . then ( null , function ( error ) {
767
- self . log_ ( 'Failed to get token: ' + error ) ;
768
- if ( ! canceled ) {
769
- if ( CONSTANTS . NODE_ADMIN ) {
770
- // This may be a critical error for the Admin Node.js SDK, so log a warning.
771
- // But getToken() may also just have temporarily failed, so we still want to
772
- // continue retrying.
773
- warn ( error ) ;
746
+ if ( typeof process !== 'undefined' && process . env [ FIREBASE_DATABASE_EMULATOR_HOST_VAR ] ) {
747
+ log ( 'Connecting to a database emulator, ommitting access token requests.' ) ;
748
+ self . authToken_ = "owner"
749
+ connection = new Connection (
750
+ connId ,
751
+ self . repoInfo_ ,
752
+ onDataMessage ,
753
+ onReady ,
754
+ onDisconnect ,
755
+ /* onKill= */ function ( reason ) {
756
+ warn ( reason + ' (' + self . repoInfo_ . toString ( ) + ')' ) ;
757
+ self . interrupt ( SERVER_KILL_INTERRUPT_REASON ) ;
758
+ } ,
759
+ lastSessionId
760
+ ) ;
761
+ } else {
762
+ // First fetch auth token, and establish connection after fetching the token was successful
763
+ this . authTokenProvider_
764
+ . getToken ( forceRefresh )
765
+ . then ( function ( result ) {
766
+ if ( ! canceled ) {
767
+ log ( 'getToken() completed. Creating connection.' ) ;
768
+ self . authToken_ = result && result . accessToken ;
769
+ connection = new Connection (
770
+ connId ,
771
+ self . repoInfo_ ,
772
+ onDataMessage ,
773
+ onReady ,
774
+ onDisconnect ,
775
+ /* onKill= */ function ( reason ) {
776
+ warn ( reason + ' (' + self . repoInfo_ . toString ( ) + ')' ) ;
777
+ self . interrupt ( SERVER_KILL_INTERRUPT_REASON ) ;
778
+ } ,
779
+ lastSessionId
780
+ ) ;
781
+ } else {
782
+ log ( 'getToken() completed but was canceled' ) ;
774
783
}
775
- closeFn ( ) ;
776
- }
777
- } ) ;
784
+ } )
785
+ . then ( null , function ( error ) {
786
+ self . log_ ( 'Failed to get token: ' + error ) ;
787
+ if ( ! canceled ) {
788
+ if ( CONSTANTS . NODE_ADMIN ) {
789
+ // This may be a critical error for the Admin Node.js SDK, so log a warning.
790
+ // But getToken() may also just have temporarily failed, so we still want to
791
+ // continue retrying.
792
+ warn ( error ) ;
793
+ }
794
+ closeFn ( ) ;
795
+ }
796
+ } ) ;
797
+ }
778
798
}
779
799
}
780
800
0 commit comments