Skip to content

Commit 7af6ecd

Browse files
committed
abstract SDK_VERSION
1 parent be8940f commit 7af6ecd

File tree

9 files changed

+95
-46
lines changed

9 files changed

+95
-46
lines changed

packages/app/src/firebaseNamespaceCore.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ import {
2929
FirebaseServiceNamespace,
3030
AppHook
3131
} from '@firebase/app-types/private';
32-
import { deepExtend, contains } from '@firebase/util';
32+
import { deepExtend, contains, CONSTANTS } from '@firebase/util';
3333
import { FirebaseAppImpl } from './firebaseApp';
3434
import { ERROR_FACTORY, AppError } from './errors';
3535
import { FirebaseAppLiteImpl } from './lite/firebaseAppLite';
@@ -70,6 +70,14 @@ export function createFirebaseNamespaceCore(
7070
}
7171
};
7272

73+
// setting version on CONSTANTS, so @firebase/database can get the version from
74+
// @firebase/util instead of @firebase/app. So we can provide a special build target
75+
// for @firebase/database that has no dependency on @firebase/app. The special build target will
76+
// be used by firebase-admin. It is to solve a version conflict (@firebase/app) that could happen
77+
// when firebase and firebase-admin are used in the same project.
78+
// https://github.com/firebase/firebase-js-sdk/issues/1696#issuecomment-501546596
79+
CONSTANTS.SDK_VERSION = version;
80+
7381
// Inject a circular default export to allow Babel users who were previously
7482
// using:
7583
//

packages/database/index.admin.ts

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
import { CONSTANTS } from '@firebase/util';
2+
import { FirebaseApp } from '@firebase/app-types';
3+
import { _FirebaseNamespace } from '@firebase/app-types/private';
4+
import { Database } from './src/api/Database';
5+
import { DataSnapshot } from './src/api/DataSnapshot';
6+
import { Query } from './src/api/Query';
7+
import { Reference } from './src/api/Reference';
8+
import { enableLogging } from './src/core/util/util';
9+
import { RepoManager } from './src/core/RepoManager';
10+
import * as INTERNAL from './src/api/internal';
11+
import * as TEST_ACCESS from './src/api/test_access';
12+
import './src/nodePatches';
13+
import { setSDKVersion } from './src/core/version';
14+
15+
/**
16+
* A one off register function which returns a database based on the app and
17+
* passed database URL.
18+
*
19+
* @param app A valid FirebaseApp-like object
20+
* @param url A valid Firebase databaseURL
21+
*/
22+
23+
const ServerValue = Database.ServerValue;
24+
25+
export function initStandalone(
26+
app: FirebaseApp,
27+
url: string,
28+
version: string
29+
) {
30+
/**
31+
* This should allow the firebase-admin package to provide a custom version
32+
* to the backend
33+
*/
34+
CONSTANTS.NODE_ADMIN = true;
35+
setSDKVersion(version);
36+
37+
return {
38+
instance: RepoManager.getInstance().databaseFromApp(app, url),
39+
namespace: {
40+
Reference,
41+
Query,
42+
Database,
43+
DataSnapshot,
44+
enableLogging,
45+
INTERNAL,
46+
ServerValue,
47+
TEST_ACCESS
48+
}
49+
};
50+
}

packages/database/index.node.ts

Lines changed: 7 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,7 @@
1616
*/
1717

1818
import firebase from '@firebase/app';
19-
import { CONSTANTS, isNodeSdk } from '@firebase/util';
20-
import { FirebaseApp, FirebaseNamespace } from '@firebase/app-types';
19+
import { FirebaseNamespace } from '@firebase/app-types';
2120
import { _FirebaseNamespace } from '@firebase/app-types/private';
2221
import { Database } from './src/api/Database';
2322
import { DataSnapshot } from './src/api/DataSnapshot';
@@ -29,6 +28,7 @@ import * as INTERNAL from './src/api/internal';
2928
import * as TEST_ACCESS from './src/api/test_access';
3029
import './src/nodePatches';
3130
import * as types from '@firebase/database-types';
31+
import { setSDKVersion } from './src/core/version';
3232

3333
/**
3434
* A one off register function which returns a database based on the app and
@@ -40,38 +40,13 @@ import * as types from '@firebase/database-types';
4040

4141
const ServerValue = Database.ServerValue;
4242

43-
export function initStandalone(
44-
app: FirebaseApp,
45-
url: string,
46-
version?: string
47-
) {
48-
/**
49-
* This should allow the firebase-admin package to provide a custom version
50-
* to the backend
51-
*/
52-
CONSTANTS.NODE_ADMIN = true;
53-
if (version) {
54-
firebase.SDK_VERSION = version;
55-
}
56-
57-
return {
58-
instance: RepoManager.getInstance().databaseFromApp(app, url),
59-
namespace: {
60-
Reference,
61-
Query,
62-
Database,
63-
DataSnapshot,
64-
enableLogging,
65-
INTERNAL,
66-
ServerValue,
67-
TEST_ACCESS
68-
}
69-
};
70-
}
71-
7243
export function registerDatabase(instance: FirebaseNamespace) {
44+
45+
// set SDK_VERSION
46+
setSDKVersion(firebase.SDK_VERSION);
47+
7348
// Register the Database Service with the 'firebase' namespace.
74-
const namespace = (instance as _FirebaseNamespace).INTERNAL.registerService(
49+
(instance as _FirebaseNamespace).INTERNAL.registerService(
7550
'database',
7651
(app, unused, url) => RepoManager.getInstance().databaseFromApp(app, url),
7752
// firebase.database namespace properties
@@ -88,10 +63,6 @@ export function registerDatabase(instance: FirebaseNamespace) {
8863
null,
8964
true
9065
);
91-
92-
if (isNodeSdk()) {
93-
module.exports = Object.assign({}, namespace, { initStandalone });
94-
}
9566
}
9667

9768
registerDatabase(firebase);

packages/database/index.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
*/
1717

1818
import firebase from '@firebase/app';
19-
import { FirebaseApp, FirebaseNamespace } from '@firebase/app-types';
19+
import { FirebaseNamespace } from '@firebase/app-types';
2020
import { _FirebaseNamespace } from '@firebase/app-types/private';
2121
import { Database } from './src/api/Database';
2222
import { DataSnapshot } from './src/api/DataSnapshot';
@@ -28,10 +28,15 @@ import * as INTERNAL from './src/api/internal';
2828
import * as TEST_ACCESS from './src/api/test_access';
2929
import { isNodeSdk } from '@firebase/util';
3030
import * as types from '@firebase/database-types';
31+
import { setSDKVersion } from './src/core/version';
3132

3233
const ServerValue = Database.ServerValue;
3334

3435
export function registerDatabase(instance: FirebaseNamespace) {
36+
37+
// set SDK_VERSION
38+
setSDKVersion(firebase.SDK_VERSION);
39+
3540
// Register the Database Service with the 'firebase' namespace.
3641
const namespace = (instance as _FirebaseNamespace).INTERNAL.registerService(
3742
'database',

packages/database/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
"browser": "dist/index.cjs.js",
88
"module": "dist/index.esm.js",
99
"esm2017": "dist/index.esm2017.js",
10+
"main-admin": "dist/index.admin.node.cjs.js",
1011
"files": [
1112
"dist"
1213
],

packages/database/rollup.config.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,15 @@ const es5Builds = [
4242
plugins: es5BuildPlugins,
4343
external: id => deps.some(dep => id === dep || id.startsWith(`${dep}/`))
4444
},
45+
/**
46+
* Node.js Build for firebase-admin (no dep on @firebase/app)
47+
*/
48+
{
49+
input: 'index.admin.ts',
50+
output: [{ file: pkg['main-admin'], format: 'cjs', sourcemap: true }],
51+
plugins: es5BuildPlugins,
52+
external: id => deps.some(dep => id === dep || id.startsWith(`${dep}/`))
53+
},
4554
/**
4655
* Browser Builds
4756
*/

packages/database/src/core/PersistentConnection.ts

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,7 @@
1515
* limitations under the License.
1616
*/
1717

18-
import firebase from '@firebase/app';
19-
import { contains, isEmpty, safeGet } from '@firebase/util';
18+
import { contains, isEmpty, safeGet, CONSTANTS } from '@firebase/util';
2019
import { stringify } from '@firebase/util';
2120
import { assert } from '@firebase/util';
2221
import { error, log, logWrapper, warn, ObjectToUniqueKey } from './util/util';
@@ -25,12 +24,12 @@ import { VisibilityMonitor } from './util/VisibilityMonitor';
2524
import { OnlineMonitor } from './util/OnlineMonitor';
2625
import { isAdmin, isValidFormat } from '@firebase/util';
2726
import { Connection } from '../realtime/Connection';
28-
import { CONSTANTS } from '@firebase/util';
2927
import { isMobileCordova, isReactNative, isNodeSdk } from '@firebase/util';
3028
import { ServerActions } from './ServerActions';
3129
import { AuthTokenProvider } from './AuthTokenProvider';
3230
import { RepoInfo } from './RepoInfo';
3331
import { Query } from '../api/Query';
32+
import { SDK_VERSION } from './version';
3433

3534
const RECONNECT_MIN_DELAY = 1000;
3635
const RECONNECT_MAX_DELAY_DEFAULT = 60 * 5 * 1000; // 5 minutes in milliseconds (Case: 1858)
@@ -927,7 +926,7 @@ export class PersistentConnection extends ServerActions {
927926
}
928927

929928
stats[
930-
'sdk.' + clientName + '.' + firebase.SDK_VERSION.replace(/\./g, '-')
929+
'sdk.' + clientName + '.' + SDK_VERSION.replace(/\./g, '-')
931930
] = 1;
932931

933932
if (isMobileCordova()) {

packages/database/src/core/version.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
2+
/** The semver (www.semver.org) version of the SDK. */
3+
export let SDK_VERSION = '';
4+
5+
export function setSDKVersion(version: string): void {
6+
SDK_VERSION = version;
7+
}

packages/database/src/realtime/WebSocketConnection.ts

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,7 @@ import { RepoInfo } from '../core/RepoInfo';
1919

2020
declare const MozWebSocket: any;
2121

22-
import firebase from '@firebase/app';
23-
import { assert } from '@firebase/util';
22+
import { assert, CONSTANTS as ENV_CONSTANTS } from '@firebase/util';
2423
import { logWrapper, splitStringBySize } from '../core/util/util';
2524
import { StatsManager } from '../core/stats/StatsManager';
2625
import {
@@ -33,12 +32,12 @@ import {
3332
VERSION_PARAM,
3433
WEBSOCKET
3534
} from './Constants';
36-
import { CONSTANTS as ENV_CONSTANTS } from '@firebase/util';
3735
import { PersistentStorage } from '../core/storage/storage';
3836
import { jsonEval, stringify } from '@firebase/util';
3937
import { isNodeSdk } from '@firebase/util';
4038
import { Transport } from './Transport';
4139
import { StatsCollection } from '../core/stats/StatsCollection';
40+
import { SDK_VERSION } from '../core/version';
4241

4342
const WEBSOCKET_MAX_FRAME_SIZE = 16384;
4443
const WEBSOCKET_KEEPALIVE_INTERVAL = 45000;
@@ -151,7 +150,7 @@ export class WebSocketConnection implements Transport {
151150
const options: { [k: string]: object } = {
152151
headers: {
153152
'User-Agent': `Firebase/${PROTOCOL_VERSION}/${
154-
firebase.SDK_VERSION
153+
SDK_VERSION
155154
}/${process.platform}/${device}`
156155
}
157156
};

0 commit comments

Comments
 (0)