Skip to content

Commit d41152a

Browse files
authored
console build for rtdb (#3709)
* console build for rtdb * Make initStandalone for console * move admin specific logic to the wrapper function for admin sdk * fix lint error
1 parent ff66d8c commit d41152a

File tree

2 files changed

+80
-44
lines changed

2 files changed

+80
-44
lines changed

packages/database/index.node.ts

Lines changed: 14 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -30,13 +30,8 @@ import { setSDKVersion } from './src/core/version';
3030
import { CONSTANTS, isNodeSdk } from '@firebase/util';
3131
import { setWebSocketImpl } from './src/realtime/WebSocketConnection';
3232
import { Client } from 'faye-websocket';
33-
import {
34-
Component,
35-
ComponentType,
36-
Provider,
37-
ComponentContainer
38-
} from '@firebase/component';
39-
import { FirebaseAuthInternalName } from '@firebase/auth-interop-types';
33+
import { Component, ComponentType } from '@firebase/component';
34+
import { FirebaseAuthInternal } from '@firebase/auth-interop-types';
4035

4136
import { name, version } from './package.json';
4237

@@ -46,7 +41,7 @@ const ServerValue = Database.ServerValue;
4641

4742
/**
4843
* A one off register function which returns a database based on the app and
49-
* passed database URL.
44+
* passed database URL. (Used by the Admin SDK)
5045
*
5146
* @param app A valid FirebaseApp-like object
5247
* @param url A valid Firebase databaseURL
@@ -57,42 +52,16 @@ export function initStandalone(
5752
app: FirebaseApp,
5853
url: string,
5954
version: string,
60-
nodeAdmin: boolean = true
55+
nodeAdmin = true
6156
) {
62-
/**
63-
* This should allow the firebase-admin package to provide a custom version
64-
* to the backend
65-
*/
6657
CONSTANTS.NODE_ADMIN = nodeAdmin;
67-
setSDKVersion(version);
68-
69-
/**
70-
* Create a 'auth-internal' component using firebase-admin-node's implementation
71-
* that implements FirebaseAuthInternal.
72-
* ComponentContainer('database-admin') is just a placeholder that doesn't perform
73-
* any actual function.
74-
*/
75-
const authProvider = new Provider<FirebaseAuthInternalName>(
76-
'auth-internal',
77-
new ComponentContainer('database-admin')
78-
);
79-
authProvider.setComponent(
80-
new Component(
81-
'auth-internal',
82-
// firebase-admin-node's app.INTERNAL implements FirebaseAuthInternal interface
83-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
84-
() => (app as any).INTERNAL,
85-
ComponentType.PRIVATE
86-
)
87-
);
88-
89-
return {
90-
instance: RepoManager.getInstance().databaseFromApp(
91-
app,
92-
authProvider,
93-
url,
94-
nodeAdmin
95-
) as types.Database,
58+
return INTERNAL.initStandalone({
59+
app,
60+
url,
61+
version,
62+
// firebase-admin-node's app.INTERNAL implements FirebaseAuthInternal interface
63+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
64+
customAuthImpl: (app as any).INTERNAL as FirebaseAuthInternal,
9665
namespace: {
9766
Reference,
9867
Query,
@@ -102,8 +71,9 @@ export function initStandalone(
10271
INTERNAL,
10372
ServerValue,
10473
TEST_ACCESS
105-
}
106-
};
74+
},
75+
nodeAdmin
76+
});
10777
}
10878

10979
export function registerDatabase(instance: FirebaseNamespace) {

packages/database/src/api/internal.ts

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,20 @@
1818
import { WebSocketConnection } from '../realtime/WebSocketConnection';
1919
import { BrowserPollConnection } from '../realtime/BrowserPollConnection';
2020
import { Reference } from './Reference';
21+
import { RepoManager } from '../core/RepoManager';
22+
import { setSDKVersion } from '../core/version';
23+
import { FirebaseApp } from '@firebase/app-types';
24+
import {
25+
FirebaseAuthInternal,
26+
FirebaseAuthInternalName
27+
} from '@firebase/auth-interop-types';
28+
import * as types from '@firebase/database-types';
29+
import {
30+
Provider,
31+
ComponentContainer,
32+
Component,
33+
ComponentType
34+
} from '@firebase/component';
2135

2236
/**
2337
* INTERNAL methods for internal-use only (tests, etc.).
@@ -67,3 +81,55 @@ export const interceptServerData = function (
6781
) {
6882
return ref.repo.interceptServerData_(callback);
6983
};
84+
85+
/**
86+
* Used by console to create a database based on the app,
87+
* passed database URL and a custom auth implementation.
88+
*
89+
* @param app A valid FirebaseApp-like object
90+
* @param url A valid Firebase databaseURL
91+
* @param version custom version e.g. firebase-admin version
92+
* @param customAuthImpl custom auth implementation
93+
*/
94+
export function initStandalone<T>({
95+
app,
96+
url,
97+
version,
98+
customAuthImpl,
99+
namespace,
100+
nodeAdmin = false
101+
}: {
102+
app: FirebaseApp;
103+
url: string;
104+
version: string;
105+
customAuthImpl: FirebaseAuthInternal;
106+
namespace: T;
107+
nodeAdmin?: boolean;
108+
}): {
109+
instance: types.Database;
110+
namespace: T;
111+
} {
112+
setSDKVersion(version);
113+
114+
/**
115+
* ComponentContainer('database-standalone') is just a placeholder that doesn't perform
116+
* any actual function.
117+
*/
118+
const authProvider = new Provider<FirebaseAuthInternalName>(
119+
'auth-internal',
120+
new ComponentContainer('database-standalone')
121+
);
122+
authProvider.setComponent(
123+
new Component('auth-internal', () => customAuthImpl, ComponentType.PRIVATE)
124+
);
125+
126+
return {
127+
instance: RepoManager.getInstance().databaseFromApp(
128+
app,
129+
authProvider,
130+
url,
131+
nodeAdmin
132+
) as types.Database,
133+
namespace
134+
};
135+
}

0 commit comments

Comments
 (0)