From 7af6ecddeed130073e3e2c783b9a13229fcecb22 Mon Sep 17 00:00:00 2001 From: Feiyang1 Date: Tue, 25 Jun 2019 14:42:00 -0700 Subject: [PATCH 01/11] abstract SDK_VERSION --- packages/app/src/firebaseNamespaceCore.ts | 10 +++- packages/database/index.admin.ts | 50 +++++++++++++++++++ packages/database/index.node.ts | 43 +++------------- packages/database/index.ts | 7 ++- packages/database/package.json | 1 + packages/database/rollup.config.js | 9 ++++ .../database/src/core/PersistentConnection.ts | 7 ++- packages/database/src/core/version.ts | 7 +++ .../src/realtime/WebSocketConnection.ts | 7 ++- 9 files changed, 95 insertions(+), 46 deletions(-) create mode 100644 packages/database/index.admin.ts create mode 100644 packages/database/src/core/version.ts diff --git a/packages/app/src/firebaseNamespaceCore.ts b/packages/app/src/firebaseNamespaceCore.ts index 4b6dcdcc9fe..a269406b556 100644 --- a/packages/app/src/firebaseNamespaceCore.ts +++ b/packages/app/src/firebaseNamespaceCore.ts @@ -29,7 +29,7 @@ import { FirebaseServiceNamespace, AppHook } from '@firebase/app-types/private'; -import { deepExtend, contains } from '@firebase/util'; +import { deepExtend, contains, CONSTANTS } from '@firebase/util'; import { FirebaseAppImpl } from './firebaseApp'; import { ERROR_FACTORY, AppError } from './errors'; import { FirebaseAppLiteImpl } from './lite/firebaseAppLite'; @@ -70,6 +70,14 @@ export function createFirebaseNamespaceCore( } }; + // setting version on CONSTANTS, so @firebase/database can get the version from + // @firebase/util instead of @firebase/app. So we can provide a special build target + // for @firebase/database that has no dependency on @firebase/app. The special build target will + // be used by firebase-admin. It is to solve a version conflict (@firebase/app) that could happen + // when firebase and firebase-admin are used in the same project. + // https://github.com/firebase/firebase-js-sdk/issues/1696#issuecomment-501546596 + CONSTANTS.SDK_VERSION = version; + // Inject a circular default export to allow Babel users who were previously // using: // diff --git a/packages/database/index.admin.ts b/packages/database/index.admin.ts new file mode 100644 index 00000000000..5af339b488f --- /dev/null +++ b/packages/database/index.admin.ts @@ -0,0 +1,50 @@ +import { CONSTANTS } from '@firebase/util'; +import { FirebaseApp } from '@firebase/app-types'; +import { _FirebaseNamespace } from '@firebase/app-types/private'; +import { Database } from './src/api/Database'; +import { DataSnapshot } from './src/api/DataSnapshot'; +import { Query } from './src/api/Query'; +import { Reference } from './src/api/Reference'; +import { enableLogging } from './src/core/util/util'; +import { RepoManager } from './src/core/RepoManager'; +import * as INTERNAL from './src/api/internal'; +import * as TEST_ACCESS from './src/api/test_access'; +import './src/nodePatches'; +import { setSDKVersion } from './src/core/version'; + +/** + * A one off register function which returns a database based on the app and + * passed database URL. + * + * @param app A valid FirebaseApp-like object + * @param url A valid Firebase databaseURL + */ + +const ServerValue = Database.ServerValue; + +export function initStandalone( + app: FirebaseApp, + url: string, + version: string +) { + /** + * This should allow the firebase-admin package to provide a custom version + * to the backend + */ + CONSTANTS.NODE_ADMIN = true; + setSDKVersion(version); + + return { + instance: RepoManager.getInstance().databaseFromApp(app, url), + namespace: { + Reference, + Query, + Database, + DataSnapshot, + enableLogging, + INTERNAL, + ServerValue, + TEST_ACCESS + } + }; +} \ No newline at end of file diff --git a/packages/database/index.node.ts b/packages/database/index.node.ts index 71bcfa964ea..55cf64c08b0 100644 --- a/packages/database/index.node.ts +++ b/packages/database/index.node.ts @@ -16,8 +16,7 @@ */ import firebase from '@firebase/app'; -import { CONSTANTS, isNodeSdk } from '@firebase/util'; -import { FirebaseApp, FirebaseNamespace } from '@firebase/app-types'; +import { FirebaseNamespace } from '@firebase/app-types'; import { _FirebaseNamespace } from '@firebase/app-types/private'; import { Database } from './src/api/Database'; import { DataSnapshot } from './src/api/DataSnapshot'; @@ -29,6 +28,7 @@ import * as INTERNAL from './src/api/internal'; import * as TEST_ACCESS from './src/api/test_access'; import './src/nodePatches'; import * as types from '@firebase/database-types'; +import { setSDKVersion } from './src/core/version'; /** * 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'; const ServerValue = Database.ServerValue; -export function initStandalone( - app: FirebaseApp, - url: string, - version?: string -) { - /** - * This should allow the firebase-admin package to provide a custom version - * to the backend - */ - CONSTANTS.NODE_ADMIN = true; - if (version) { - firebase.SDK_VERSION = version; - } - - return { - instance: RepoManager.getInstance().databaseFromApp(app, url), - namespace: { - Reference, - Query, - Database, - DataSnapshot, - enableLogging, - INTERNAL, - ServerValue, - TEST_ACCESS - } - }; -} - export function registerDatabase(instance: FirebaseNamespace) { + + // set SDK_VERSION + setSDKVersion(firebase.SDK_VERSION); + // Register the Database Service with the 'firebase' namespace. - const namespace = (instance as _FirebaseNamespace).INTERNAL.registerService( + (instance as _FirebaseNamespace).INTERNAL.registerService( 'database', (app, unused, url) => RepoManager.getInstance().databaseFromApp(app, url), // firebase.database namespace properties @@ -88,10 +63,6 @@ export function registerDatabase(instance: FirebaseNamespace) { null, true ); - - if (isNodeSdk()) { - module.exports = Object.assign({}, namespace, { initStandalone }); - } } registerDatabase(firebase); diff --git a/packages/database/index.ts b/packages/database/index.ts index 5039eb0c6ae..5de3b8bb385 100644 --- a/packages/database/index.ts +++ b/packages/database/index.ts @@ -16,7 +16,7 @@ */ import firebase from '@firebase/app'; -import { FirebaseApp, FirebaseNamespace } from '@firebase/app-types'; +import { FirebaseNamespace } from '@firebase/app-types'; import { _FirebaseNamespace } from '@firebase/app-types/private'; import { Database } from './src/api/Database'; import { DataSnapshot } from './src/api/DataSnapshot'; @@ -28,10 +28,15 @@ import * as INTERNAL from './src/api/internal'; import * as TEST_ACCESS from './src/api/test_access'; import { isNodeSdk } from '@firebase/util'; import * as types from '@firebase/database-types'; +import { setSDKVersion } from './src/core/version'; const ServerValue = Database.ServerValue; export function registerDatabase(instance: FirebaseNamespace) { + + // set SDK_VERSION + setSDKVersion(firebase.SDK_VERSION); + // Register the Database Service with the 'firebase' namespace. const namespace = (instance as _FirebaseNamespace).INTERNAL.registerService( 'database', diff --git a/packages/database/package.json b/packages/database/package.json index 6b88211f952..fe04070a22c 100644 --- a/packages/database/package.json +++ b/packages/database/package.json @@ -7,6 +7,7 @@ "browser": "dist/index.cjs.js", "module": "dist/index.esm.js", "esm2017": "dist/index.esm2017.js", + "main-admin": "dist/index.admin.node.cjs.js", "files": [ "dist" ], diff --git a/packages/database/rollup.config.js b/packages/database/rollup.config.js index 365301f11e9..20c09dfecf4 100644 --- a/packages/database/rollup.config.js +++ b/packages/database/rollup.config.js @@ -42,6 +42,15 @@ const es5Builds = [ plugins: es5BuildPlugins, external: id => deps.some(dep => id === dep || id.startsWith(`${dep}/`)) }, + /** + * Node.js Build for firebase-admin (no dep on @firebase/app) + */ + { + input: 'index.admin.ts', + output: [{ file: pkg['main-admin'], format: 'cjs', sourcemap: true }], + plugins: es5BuildPlugins, + external: id => deps.some(dep => id === dep || id.startsWith(`${dep}/`)) + }, /** * Browser Builds */ diff --git a/packages/database/src/core/PersistentConnection.ts b/packages/database/src/core/PersistentConnection.ts index b53c500b989..0c247cecf57 100644 --- a/packages/database/src/core/PersistentConnection.ts +++ b/packages/database/src/core/PersistentConnection.ts @@ -15,8 +15,7 @@ * limitations under the License. */ -import firebase from '@firebase/app'; -import { contains, isEmpty, safeGet } from '@firebase/util'; +import { contains, isEmpty, safeGet, CONSTANTS } from '@firebase/util'; import { stringify } from '@firebase/util'; import { assert } from '@firebase/util'; import { error, log, logWrapper, warn, ObjectToUniqueKey } from './util/util'; @@ -25,12 +24,12 @@ import { VisibilityMonitor } from './util/VisibilityMonitor'; import { OnlineMonitor } from './util/OnlineMonitor'; import { isAdmin, isValidFormat } from '@firebase/util'; import { Connection } from '../realtime/Connection'; -import { CONSTANTS } from '@firebase/util'; import { isMobileCordova, isReactNative, isNodeSdk } from '@firebase/util'; import { ServerActions } from './ServerActions'; import { AuthTokenProvider } from './AuthTokenProvider'; import { RepoInfo } from './RepoInfo'; import { Query } from '../api/Query'; +import { SDK_VERSION } from './version'; const RECONNECT_MIN_DELAY = 1000; const RECONNECT_MAX_DELAY_DEFAULT = 60 * 5 * 1000; // 5 minutes in milliseconds (Case: 1858) @@ -927,7 +926,7 @@ export class PersistentConnection extends ServerActions { } stats[ - 'sdk.' + clientName + '.' + firebase.SDK_VERSION.replace(/\./g, '-') + 'sdk.' + clientName + '.' + SDK_VERSION.replace(/\./g, '-') ] = 1; if (isMobileCordova()) { diff --git a/packages/database/src/core/version.ts b/packages/database/src/core/version.ts new file mode 100644 index 00000000000..f63e9b8619a --- /dev/null +++ b/packages/database/src/core/version.ts @@ -0,0 +1,7 @@ + +/** The semver (www.semver.org) version of the SDK. */ +export let SDK_VERSION = ''; + +export function setSDKVersion(version: string): void { + SDK_VERSION = version; +} diff --git a/packages/database/src/realtime/WebSocketConnection.ts b/packages/database/src/realtime/WebSocketConnection.ts index 58d9423206d..e6dcc0c5c62 100644 --- a/packages/database/src/realtime/WebSocketConnection.ts +++ b/packages/database/src/realtime/WebSocketConnection.ts @@ -19,8 +19,7 @@ import { RepoInfo } from '../core/RepoInfo'; declare const MozWebSocket: any; -import firebase from '@firebase/app'; -import { assert } from '@firebase/util'; +import { assert, CONSTANTS as ENV_CONSTANTS } from '@firebase/util'; import { logWrapper, splitStringBySize } from '../core/util/util'; import { StatsManager } from '../core/stats/StatsManager'; import { @@ -33,12 +32,12 @@ import { VERSION_PARAM, WEBSOCKET } from './Constants'; -import { CONSTANTS as ENV_CONSTANTS } from '@firebase/util'; import { PersistentStorage } from '../core/storage/storage'; import { jsonEval, stringify } from '@firebase/util'; import { isNodeSdk } from '@firebase/util'; import { Transport } from './Transport'; import { StatsCollection } from '../core/stats/StatsCollection'; +import { SDK_VERSION } from '../core/version'; const WEBSOCKET_MAX_FRAME_SIZE = 16384; const WEBSOCKET_KEEPALIVE_INTERVAL = 45000; @@ -151,7 +150,7 @@ export class WebSocketConnection implements Transport { const options: { [k: string]: object } = { headers: { 'User-Agent': `Firebase/${PROTOCOL_VERSION}/${ - firebase.SDK_VERSION + SDK_VERSION }/${process.platform}/${device}` } }; From 9088571fdc7d48f15064e459fefda50b7ef98ba4 Mon Sep 17 00:00:00 2001 From: Feiyang1 Date: Tue, 25 Jun 2019 14:43:10 -0700 Subject: [PATCH 02/11] some comment --- packages/database/src/core/version.ts | 1 + 1 file changed, 1 insertion(+) diff --git a/packages/database/src/core/version.ts b/packages/database/src/core/version.ts index f63e9b8619a..c5e51fc4248 100644 --- a/packages/database/src/core/version.ts +++ b/packages/database/src/core/version.ts @@ -2,6 +2,7 @@ /** The semver (www.semver.org) version of the SDK. */ export let SDK_VERSION = ''; +// SDK_VERSION should be set before any database instance is created export function setSDKVersion(version: string): void { SDK_VERSION = version; } From 282b9163b1c0bff29bc633f3864af9e971301a00 Mon Sep 17 00:00:00 2001 From: Feiyang1 Date: Tue, 25 Jun 2019 15:30:58 -0700 Subject: [PATCH 03/11] remove the unnecessary build target --- packages/app/src/firebaseNamespaceCore.ts | 8 ---- packages/database/index.admin.ts | 50 ----------------------- packages/database/index.node.ts | 49 +++++++++++++++++++--- packages/database/index.ts | 2 +- packages/database/rollup.config.js | 9 ---- 5 files changed, 45 insertions(+), 73 deletions(-) delete mode 100644 packages/database/index.admin.ts diff --git a/packages/app/src/firebaseNamespaceCore.ts b/packages/app/src/firebaseNamespaceCore.ts index a269406b556..eefbc91607e 100644 --- a/packages/app/src/firebaseNamespaceCore.ts +++ b/packages/app/src/firebaseNamespaceCore.ts @@ -70,14 +70,6 @@ export function createFirebaseNamespaceCore( } }; - // setting version on CONSTANTS, so @firebase/database can get the version from - // @firebase/util instead of @firebase/app. So we can provide a special build target - // for @firebase/database that has no dependency on @firebase/app. The special build target will - // be used by firebase-admin. It is to solve a version conflict (@firebase/app) that could happen - // when firebase and firebase-admin are used in the same project. - // https://github.com/firebase/firebase-js-sdk/issues/1696#issuecomment-501546596 - CONSTANTS.SDK_VERSION = version; - // Inject a circular default export to allow Babel users who were previously // using: // diff --git a/packages/database/index.admin.ts b/packages/database/index.admin.ts deleted file mode 100644 index 5af339b488f..00000000000 --- a/packages/database/index.admin.ts +++ /dev/null @@ -1,50 +0,0 @@ -import { CONSTANTS } from '@firebase/util'; -import { FirebaseApp } from '@firebase/app-types'; -import { _FirebaseNamespace } from '@firebase/app-types/private'; -import { Database } from './src/api/Database'; -import { DataSnapshot } from './src/api/DataSnapshot'; -import { Query } from './src/api/Query'; -import { Reference } from './src/api/Reference'; -import { enableLogging } from './src/core/util/util'; -import { RepoManager } from './src/core/RepoManager'; -import * as INTERNAL from './src/api/internal'; -import * as TEST_ACCESS from './src/api/test_access'; -import './src/nodePatches'; -import { setSDKVersion } from './src/core/version'; - -/** - * A one off register function which returns a database based on the app and - * passed database URL. - * - * @param app A valid FirebaseApp-like object - * @param url A valid Firebase databaseURL - */ - -const ServerValue = Database.ServerValue; - -export function initStandalone( - app: FirebaseApp, - url: string, - version: string -) { - /** - * This should allow the firebase-admin package to provide a custom version - * to the backend - */ - CONSTANTS.NODE_ADMIN = true; - setSDKVersion(version); - - return { - instance: RepoManager.getInstance().databaseFromApp(app, url), - namespace: { - Reference, - Query, - Database, - DataSnapshot, - enableLogging, - INTERNAL, - ServerValue, - TEST_ACCESS - } - }; -} \ No newline at end of file diff --git a/packages/database/index.node.ts b/packages/database/index.node.ts index 55cf64c08b0..da0811d49b7 100644 --- a/packages/database/index.node.ts +++ b/packages/database/index.node.ts @@ -15,8 +15,7 @@ * limitations under the License. */ -import firebase from '@firebase/app'; -import { FirebaseNamespace } from '@firebase/app-types'; +import { FirebaseNamespace, FirebaseApp } from '@firebase/app-types'; import { _FirebaseNamespace } from '@firebase/app-types/private'; import { Database } from './src/api/Database'; import { DataSnapshot } from './src/api/DataSnapshot'; @@ -29,6 +28,10 @@ import * as TEST_ACCESS from './src/api/test_access'; import './src/nodePatches'; import * as types from '@firebase/database-types'; import { setSDKVersion } from './src/core/version'; +import { CONSTANTS } from '@firebase/util'; + + +const ServerValue = Database.ServerValue; /** * A one off register function which returns a database based on the app and @@ -36,14 +39,39 @@ import { setSDKVersion } from './src/core/version'; * * @param app A valid FirebaseApp-like object * @param url A valid Firebase databaseURL + * @param version custom version e.g. firebase-admin version */ +export function initStandalone( + app: FirebaseApp, + url: string, + version: string +) { + /** + * This should allow the firebase-admin package to provide a custom version + * to the backend + */ + CONSTANTS.NODE_ADMIN = true; + setSDKVersion(version); -const ServerValue = Database.ServerValue; + return { + instance: RepoManager.getInstance().databaseFromApp(app, url), + namespace: { + Reference, + Query, + Database, + DataSnapshot, + enableLogging, + INTERNAL, + ServerValue, + TEST_ACCESS + } + }; +} export function registerDatabase(instance: FirebaseNamespace) { // set SDK_VERSION - setSDKVersion(firebase.SDK_VERSION); + setSDKVersion(instance.SDK_VERSION); // Register the Database Service with the 'firebase' namespace. (instance as _FirebaseNamespace).INTERNAL.registerService( @@ -65,7 +93,18 @@ export function registerDatabase(instance: FirebaseNamespace) { ); } -registerDatabase(firebase); +try { + // If @firebase/app is not present, skip registering database. + // It could happen when this package is used in firebase-admin which doesn't depend on @firebase/app. + // Previously firebase-admin depends on @firebase/app, which causes version conflict on + // @firebase/app when used together with the js sdk. More detail: + // https://github.com/firebase/firebase-js-sdk/issues/1696#issuecomment-501546596 + const firebase = require('@firebase/app'); + registerDatabase(firebase); +} catch(err) { + +} + // Types to export for the admin SDK export { Database, Query, Reference, enableLogging, ServerValue }; diff --git a/packages/database/index.ts b/packages/database/index.ts index 5de3b8bb385..76e3e55a647 100644 --- a/packages/database/index.ts +++ b/packages/database/index.ts @@ -35,7 +35,7 @@ const ServerValue = Database.ServerValue; export function registerDatabase(instance: FirebaseNamespace) { // set SDK_VERSION - setSDKVersion(firebase.SDK_VERSION); + setSDKVersion(instance.SDK_VERSION); // Register the Database Service with the 'firebase' namespace. const namespace = (instance as _FirebaseNamespace).INTERNAL.registerService( diff --git a/packages/database/rollup.config.js b/packages/database/rollup.config.js index 20c09dfecf4..365301f11e9 100644 --- a/packages/database/rollup.config.js +++ b/packages/database/rollup.config.js @@ -42,15 +42,6 @@ const es5Builds = [ plugins: es5BuildPlugins, external: id => deps.some(dep => id === dep || id.startsWith(`${dep}/`)) }, - /** - * Node.js Build for firebase-admin (no dep on @firebase/app) - */ - { - input: 'index.admin.ts', - output: [{ file: pkg['main-admin'], format: 'cjs', sourcemap: true }], - plugins: es5BuildPlugins, - external: id => deps.some(dep => id === dep || id.startsWith(`${dep}/`)) - }, /** * Browser Builds */ From 174346767614d6964b3ea362f9f87dcc57e245c6 Mon Sep 17 00:00:00 2001 From: Feiyang1 Date: Tue, 25 Jun 2019 16:35:06 -0700 Subject: [PATCH 04/11] require default --- packages/database/index.node.ts | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/packages/database/index.node.ts b/packages/database/index.node.ts index da0811d49b7..d95826c536a 100644 --- a/packages/database/index.node.ts +++ b/packages/database/index.node.ts @@ -99,10 +99,11 @@ try { // Previously firebase-admin depends on @firebase/app, which causes version conflict on // @firebase/app when used together with the js sdk. More detail: // https://github.com/firebase/firebase-js-sdk/issues/1696#issuecomment-501546596 - const firebase = require('@firebase/app'); + const firebase = require('@firebase/app').default; registerDatabase(firebase); } catch(err) { - + // catch 'MODULE_NOT_FOUND' error in firebase-admin + // we can safely ignore this error because RTDB in firebase-admin works without @firebase/app } From 5ef2697d71daf2a3b850324e1adce4e9bb444442 Mon Sep 17 00:00:00 2001 From: Feiyang1 Date: Tue, 25 Jun 2019 16:35:25 -0700 Subject: [PATCH 05/11] [AUTOMATED]: Prettier Code Styling --- packages/database/index.node.ts | 13 +++---------- packages/database/index.ts | 3 +-- packages/database/src/core/PersistentConnection.ts | 11 +++++++---- packages/database/src/core/version.ts | 3 +-- .../database/src/realtime/WebSocketConnection.ts | 6 +++--- 5 files changed, 15 insertions(+), 21 deletions(-) diff --git a/packages/database/index.node.ts b/packages/database/index.node.ts index d95826c536a..2050fa03b7a 100644 --- a/packages/database/index.node.ts +++ b/packages/database/index.node.ts @@ -30,7 +30,6 @@ import * as types from '@firebase/database-types'; import { setSDKVersion } from './src/core/version'; import { CONSTANTS } from '@firebase/util'; - const ServerValue = Database.ServerValue; /** @@ -41,11 +40,7 @@ const ServerValue = Database.ServerValue; * @param url A valid Firebase databaseURL * @param version custom version e.g. firebase-admin version */ -export function initStandalone( - app: FirebaseApp, - url: string, - version: string -) { +export function initStandalone(app: FirebaseApp, url: string, version: string) { /** * This should allow the firebase-admin package to provide a custom version * to the backend @@ -69,7 +64,6 @@ export function initStandalone( } export function registerDatabase(instance: FirebaseNamespace) { - // set SDK_VERSION setSDKVersion(instance.SDK_VERSION); @@ -96,17 +90,16 @@ export function registerDatabase(instance: FirebaseNamespace) { try { // If @firebase/app is not present, skip registering database. // It could happen when this package is used in firebase-admin which doesn't depend on @firebase/app. - // Previously firebase-admin depends on @firebase/app, which causes version conflict on + // Previously firebase-admin depends on @firebase/app, which causes version conflict on // @firebase/app when used together with the js sdk. More detail: // https://github.com/firebase/firebase-js-sdk/issues/1696#issuecomment-501546596 const firebase = require('@firebase/app').default; registerDatabase(firebase); -} catch(err) { +} catch (err) { // catch 'MODULE_NOT_FOUND' error in firebase-admin // we can safely ignore this error because RTDB in firebase-admin works without @firebase/app } - // Types to export for the admin SDK export { Database, Query, Reference, enableLogging, ServerValue }; diff --git a/packages/database/index.ts b/packages/database/index.ts index 76e3e55a647..00a3848447c 100644 --- a/packages/database/index.ts +++ b/packages/database/index.ts @@ -33,10 +33,9 @@ import { setSDKVersion } from './src/core/version'; const ServerValue = Database.ServerValue; export function registerDatabase(instance: FirebaseNamespace) { - // set SDK_VERSION setSDKVersion(instance.SDK_VERSION); - + // Register the Database Service with the 'firebase' namespace. const namespace = (instance as _FirebaseNamespace).INTERNAL.registerService( 'database', diff --git a/packages/database/src/core/PersistentConnection.ts b/packages/database/src/core/PersistentConnection.ts index 0c247cecf57..b9c1ca7a76a 100644 --- a/packages/database/src/core/PersistentConnection.ts +++ b/packages/database/src/core/PersistentConnection.ts @@ -15,7 +15,12 @@ * limitations under the License. */ -import { contains, isEmpty, safeGet, CONSTANTS } from '@firebase/util'; +import { + contains, + isEmpty, + safeGet, + CONSTANTS +} from '@firebase/util'; import { stringify } from '@firebase/util'; import { assert } from '@firebase/util'; import { error, log, logWrapper, warn, ObjectToUniqueKey } from './util/util'; @@ -925,9 +930,7 @@ export class PersistentConnection extends ServerActions { clientName = 'node'; } - stats[ - 'sdk.' + clientName + '.' + SDK_VERSION.replace(/\./g, '-') - ] = 1; + stats['sdk.' + clientName + '.' + SDK_VERSION.replace(/\./g, '-')] = 1; if (isMobileCordova()) { stats['framework.cordova'] = 1; diff --git a/packages/database/src/core/version.ts b/packages/database/src/core/version.ts index c5e51fc4248..026d142a2a9 100644 --- a/packages/database/src/core/version.ts +++ b/packages/database/src/core/version.ts @@ -1,8 +1,7 @@ - /** The semver (www.semver.org) version of the SDK. */ export let SDK_VERSION = ''; // SDK_VERSION should be set before any database instance is created export function setSDKVersion(version: string): void { - SDK_VERSION = version; + SDK_VERSION = version; } diff --git a/packages/database/src/realtime/WebSocketConnection.ts b/packages/database/src/realtime/WebSocketConnection.ts index e6dcc0c5c62..4601e9b4a76 100644 --- a/packages/database/src/realtime/WebSocketConnection.ts +++ b/packages/database/src/realtime/WebSocketConnection.ts @@ -149,9 +149,9 @@ export class WebSocketConnection implements Transport { // UA Format: Firebase//// const options: { [k: string]: object } = { headers: { - 'User-Agent': `Firebase/${PROTOCOL_VERSION}/${ - SDK_VERSION - }/${process.platform}/${device}` + 'User-Agent': `Firebase/${PROTOCOL_VERSION}/${SDK_VERSION}/${ + process.platform + }/${device}` } }; From 35efa6dcef93800ea09a7cb2ac10644c5859697e Mon Sep 17 00:00:00 2001 From: Feiyang1 Date: Tue, 25 Jun 2019 16:35:27 -0700 Subject: [PATCH 06/11] [AUTOMATED]: License Headers --- packages/database/src/core/version.ts | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/packages/database/src/core/version.ts b/packages/database/src/core/version.ts index 026d142a2a9..cbdb1b09894 100644 --- a/packages/database/src/core/version.ts +++ b/packages/database/src/core/version.ts @@ -1,3 +1,20 @@ +/** + * @license + * Copyright 2019 Google Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + /** The semver (www.semver.org) version of the SDK. */ export let SDK_VERSION = ''; From da4717cbb01be2bb793ae484f6baefe088a85292 Mon Sep 17 00:00:00 2001 From: Feiyang1 Date: Tue, 25 Jun 2019 16:40:46 -0700 Subject: [PATCH 07/11] remove experimental changes --- packages/database/package.json | 1 - 1 file changed, 1 deletion(-) diff --git a/packages/database/package.json b/packages/database/package.json index fe04070a22c..6b88211f952 100644 --- a/packages/database/package.json +++ b/packages/database/package.json @@ -7,7 +7,6 @@ "browser": "dist/index.cjs.js", "module": "dist/index.esm.js", "esm2017": "dist/index.esm2017.js", - "main-admin": "dist/index.admin.node.cjs.js", "files": [ "dist" ], From 092c99ca915fb6e108dd5bb7cbf19cd4387eebd5 Mon Sep 17 00:00:00 2001 From: Feiyang1 Date: Tue, 25 Jun 2019 17:00:56 -0700 Subject: [PATCH 08/11] remove unused var --- packages/app/src/firebaseNamespaceCore.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/app/src/firebaseNamespaceCore.ts b/packages/app/src/firebaseNamespaceCore.ts index eefbc91607e..4b6dcdcc9fe 100644 --- a/packages/app/src/firebaseNamespaceCore.ts +++ b/packages/app/src/firebaseNamespaceCore.ts @@ -29,7 +29,7 @@ import { FirebaseServiceNamespace, AppHook } from '@firebase/app-types/private'; -import { deepExtend, contains, CONSTANTS } from '@firebase/util'; +import { deepExtend, contains } from '@firebase/util'; import { FirebaseAppImpl } from './firebaseApp'; import { ERROR_FACTORY, AppError } from './errors'; import { FirebaseAppLiteImpl } from './lite/firebaseAppLite'; From e794be100ef503855394d32760047c5b4b258509 Mon Sep 17 00:00:00 2001 From: Feiyang1 Date: Wed, 26 Jun 2019 15:07:43 -0700 Subject: [PATCH 09/11] only ignore MODULE_NOT_FOUND error --- packages/database/index.node.ts | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/packages/database/index.node.ts b/packages/database/index.node.ts index 2050fa03b7a..10e307a95a8 100644 --- a/packages/database/index.node.ts +++ b/packages/database/index.node.ts @@ -96,8 +96,11 @@ try { const firebase = require('@firebase/app').default; registerDatabase(firebase); } catch (err) { - // catch 'MODULE_NOT_FOUND' error in firebase-admin + // catch and ignore 'MODULE_NOT_FOUND' error in firebase-admin context // we can safely ignore this error because RTDB in firebase-admin works without @firebase/app + if (err.code !== 'MODULE_NOT_FOUND') { + throw err; + } } // Types to export for the admin SDK From c47ea2ba4fd829b1e5f2183d242adff8c51b48df Mon Sep 17 00:00:00 2001 From: Feiyang1 Date: Fri, 28 Jun 2019 16:41:41 -0700 Subject: [PATCH 10/11] [AUTOMATED]: Prettier Code Styling --- packages/database/src/core/PersistentConnection.ts | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-) diff --git a/packages/database/src/core/PersistentConnection.ts b/packages/database/src/core/PersistentConnection.ts index b9c1ca7a76a..08f9261f8d4 100644 --- a/packages/database/src/core/PersistentConnection.ts +++ b/packages/database/src/core/PersistentConnection.ts @@ -15,12 +15,7 @@ * limitations under the License. */ -import { - contains, - isEmpty, - safeGet, - CONSTANTS -} from '@firebase/util'; +import { contains, isEmpty, safeGet, CONSTANTS } from '@firebase/util'; import { stringify } from '@firebase/util'; import { assert } from '@firebase/util'; import { error, log, logWrapper, warn, ObjectToUniqueKey } from './util/util'; From 05f667fd9f214b46a5911a57c886a890440c27ca Mon Sep 17 00:00:00 2001 From: Feiyang1 Date: Mon, 1 Jul 2019 09:56:23 -0700 Subject: [PATCH 11/11] revert module export change --- packages/database/index.node.ts | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/packages/database/index.node.ts b/packages/database/index.node.ts index 10e307a95a8..a58988c507f 100644 --- a/packages/database/index.node.ts +++ b/packages/database/index.node.ts @@ -28,7 +28,7 @@ import * as TEST_ACCESS from './src/api/test_access'; import './src/nodePatches'; import * as types from '@firebase/database-types'; import { setSDKVersion } from './src/core/version'; -import { CONSTANTS } from '@firebase/util'; +import { CONSTANTS, isNodeSdk } from '@firebase/util'; const ServerValue = Database.ServerValue; @@ -68,7 +68,7 @@ export function registerDatabase(instance: FirebaseNamespace) { setSDKVersion(instance.SDK_VERSION); // Register the Database Service with the 'firebase' namespace. - (instance as _FirebaseNamespace).INTERNAL.registerService( + const namespace = (instance as _FirebaseNamespace).INTERNAL.registerService( 'database', (app, unused, url) => RepoManager.getInstance().databaseFromApp(app, url), // firebase.database namespace properties @@ -85,6 +85,10 @@ export function registerDatabase(instance: FirebaseNamespace) { null, true ); + + if (isNodeSdk()) { + module.exports = Object.assign({}, namespace, { initStandalone }); + } } try {