Skip to content

chore: Remove instances of XXX_SDK_VERSION_XXX by reading version from package.json #952

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Jul 17, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 0 additions & 3 deletions gulpfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,9 +76,6 @@ gulp.task('compile', function() {
// Compile Typescript into .js and .d.ts files
.pipe(buildProject())

// Replace SDK version
.pipe(replace(/\<XXX_SDK_VERSION_XXX\>/g, pkg.version))

// Add header
.pipe(header(banner))

Expand Down
2 changes: 1 addition & 1 deletion src/auth/auth-api-request.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ import { Tenant, TenantOptions, TenantServerResponse } from './tenant';

/** Firebase Auth request header. */
const FIREBASE_AUTH_HEADER = {
'X-Client-Version': 'Node/Admin/<XXX_SDK_VERSION_XXX>',
'X-Client-Version': `Node/Admin/${utils.getSdkVersion()}`,
};
/** Firebase Auth request timeout duration in milliseconds. */
const FIREBASE_AUTH_TIMEOUT = 25000;
Expand Down
4 changes: 2 additions & 2 deletions src/database/database.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { Database } from '@firebase/database';

import * as validator from '../utils/validator';
import { AuthorizedHttpClient, HttpRequestConfig, HttpError } from '../utils/api-request';
import { getSdkVersion } from '../utils/index';


/**
Expand Down Expand Up @@ -78,8 +79,7 @@ export class DatabaseService implements FirebaseServiceInterface {
let db: Database = this.INTERNAL.databases[dbUrl];
if (typeof db === 'undefined') {
const rtdb = require('@firebase/database'); // eslint-disable-line @typescript-eslint/no-var-requires
const { version } = require('../../package.json'); // eslint-disable-line @typescript-eslint/no-var-requires
db = rtdb.initStandalone(this.appInternal, dbUrl, version).instance;
db = rtdb.initStandalone(this.appInternal, dbUrl, getSdkVersion()).instance;

const rulesClient = new DatabaseRulesClient(this.app, dbUrl);
db.getRules = () => {
Expand Down
3 changes: 2 additions & 1 deletion src/firebase-namespace.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ import { SecurityRules } from './security-rules/security-rules';
import { RemoteConfig } from './remote-config/remote-config';

import * as validator from './utils/validator';
import { getSdkVersion } from './utils/index';

const DEFAULT_APP_NAME = '[DEFAULT]';

Expand Down Expand Up @@ -309,7 +310,7 @@ export class FirebaseNamespace {
/* tslint:enable:variable-name */

public credential = firebaseCredential;
public SDK_VERSION = '<XXX_SDK_VERSION_XXX>';
public SDK_VERSION = getSdkVersion();
public INTERNAL: FirebaseNamespaceInternals;

/* tslint:disable */
Expand Down
2 changes: 1 addition & 1 deletion src/machine-learning/machine-learning-api-client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import { FirebaseApp } from '../firebase-app';

const ML_V1BETA2_API = 'https://firebaseml.googleapis.com/v1beta2';
const FIREBASE_VERSION_HEADER = {
'X-Firebase-Client': 'fire-admin-node/<XXX_SDK_VERSION_XXX>',
'X-Firebase-Client': `fire-admin-node/${utils.getSdkVersion()}`,
};

export interface StatusErrorResponse {
Expand Down
5 changes: 3 additions & 2 deletions src/messaging/messaging-api-request.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,16 +21,17 @@ import {
import { createFirebaseError, getErrorCode } from './messaging-errors';
import { SubRequest, BatchRequestClient } from './batch-request';
import { SendResponse, BatchResponse } from './messaging-types';
import { getSdkVersion } from '../utils/index';

// FCM backend constants
const FIREBASE_MESSAGING_TIMEOUT = 10000;
const FIREBASE_MESSAGING_BATCH_URL = 'https://fcm.googleapis.com/batch';
const FIREBASE_MESSAGING_HTTP_METHOD: HttpMethod = 'POST';
const FIREBASE_MESSAGING_HEADERS = {
'X-Firebase-Client': 'fire-admin-node/<XXX_SDK_VERSION_XXX>',
'X-Firebase-Client': `fire-admin-node/${getSdkVersion()}`,
};
const LEGACY_FIREBASE_MESSAGING_HEADERS = {
'X-Firebase-Client': 'fire-admin-node/<XXX_SDK_VERSION_XXX>',
'X-Firebase-Client': `fire-admin-node/${getSdkVersion()}`,
'access_token_auth': 'true',
};

Expand Down
3 changes: 2 additions & 1 deletion src/project-management/project-management-api-request.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import {
import { FirebaseProjectManagementError, ProjectManagementErrorCode } from '../utils/error';
import * as validator from '../utils/validator';
import { ShaCertificate } from './android-app';
import { getSdkVersion } from '../utils/index';

/** Project management backend host and port. */
const PROJECT_MANAGEMENT_HOST_AND_PORT = 'firebase.googleapis.com:443';
Expand All @@ -30,7 +31,7 @@ const PROJECT_MANAGEMENT_PATH = '/v1/';
const PROJECT_MANAGEMENT_BETA_PATH = '/v1beta1/';
/** Project management request header. */
const PROJECT_MANAGEMENT_HEADERS = {
'X-Client-Version': 'Node/Admin/<XXX_SDK_VERSION_XXX>',
'X-Client-Version': `Node/Admin/${getSdkVersion()}`,
};
/** Project management request timeout duration in milliseconds. */
const PROJECT_MANAGEMENT_TIMEOUT_MILLIS = 10000;
Expand Down
2 changes: 1 addition & 1 deletion src/remote-config/remote-config-api-client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ import { deepCopy } from '../utils/deep-copy';
// Remote Config backend constants
const FIREBASE_REMOTE_CONFIG_V1_API = 'https://firebaseremoteconfig.googleapis.com/v1';
const FIREBASE_REMOTE_CONFIG_HEADERS = {
'X-Firebase-Client': 'fire-admin-node/<XXX_SDK_VERSION_XXX>',
'X-Firebase-Client': `fire-admin-node/${utils.getSdkVersion()}`,
// There is a known issue in which the ETag is not properly returned in cases where the request
// does not specify a compression type. Currently, it is required to include the header
// `Accept-Encoding: gzip` or equivalent in all requests.
Expand Down
2 changes: 1 addition & 1 deletion src/security-rules/security-rules-api-client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import { FirebaseApp } from '../firebase-app';

const RULES_V1_API = 'https://firebaserules.googleapis.com/v1';
const FIREBASE_VERSION_HEADER = {
'X-Firebase-Client': 'fire-admin-node/<XXX_SDK_VERSION_XXX>',
'X-Firebase-Client': `fire-admin-node/${utils.getSdkVersion()}`,
};

export interface Release {
Expand Down
10 changes: 10 additions & 0 deletions src/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,16 @@ import { ServiceAccountCredential, ComputeEngineCredential } from '../auth/crede

import * as validator from './validator';

let sdkVersion: string;

export function getSdkVersion(): string {
if (!sdkVersion) {
const { version } = require('../../package.json'); // eslint-disable-line @typescript-eslint/no-var-requires
sdkVersion = version;
}
return sdkVersion;
}

/**
* Renames properties on an object given a mapping from old to new property names.
*
Expand Down
3 changes: 2 additions & 1 deletion test/unit/auth/auth-api-request.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ import { UserIdentifier } from '../../../src/auth/identifier';
import { TenantOptions } from '../../../src/auth/tenant';
import { UpdateRequest, UpdateMultiFactorInfoRequest } from '../../../src/auth/user-record';
import { expectUserImportResult } from './user-import-builder.spec';
import { getSdkVersion } from '../../../src/utils/index';

chai.should();
chai.use(sinonChai);
Expand Down Expand Up @@ -849,7 +850,7 @@ AUTH_REQUEST_HANDLER_TESTS.forEach((handler) => {
let getTokenStub: sinon.SinonStub;
const mockAccessToken: string = utils.generateRandomAccessToken();
const expectedHeaders: {[key: string]: string} = {
'X-Client-Version': 'Node/Admin/<XXX_SDK_VERSION_XXX>',
'X-Client-Version': `Node/Admin/${getSdkVersion()}`,
'Authorization': 'Bearer ' + mockAccessToken,
};
const callParams = (path: string, method: any, data: any): HttpRequestConfig => {
Expand Down
3 changes: 2 additions & 1 deletion test/unit/firebase-namespace.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ import { InstanceId } from '../../src/instance-id/instance-id';
import { ProjectManagement } from '../../src/project-management/project-management';
import { SecurityRules } from '../../src/security-rules/security-rules';
import { RemoteConfig } from '../../src/remote-config/remote-config';
import { getSdkVersion } from '../../src/utils/index';

chai.should();
chai.use(sinonChai);
Expand All @@ -73,7 +74,7 @@ describe('FirebaseNamespace', () => {

describe('#SDK_VERSION', () => {
it('should return the SDK version', () => {
expect(firebaseNamespace.SDK_VERSION).to.equal('<XXX_SDK_VERSION_XXX>');
expect(firebaseNamespace.SDK_VERSION).to.equal(getSdkVersion());
});
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import * as utils from '../utils';
import * as mocks from '../../resources/mocks';
import { FirebaseAppError } from '../../../src/utils/error';
import { FirebaseApp } from '../../../src/firebase-app';
import { getSdkVersion } from '../../../src/utils/index';

const expect = chai.expect;

Expand Down Expand Up @@ -86,7 +87,7 @@ describe('MachineLearningApiClient', () => {
};
const EXPECTED_HEADERS = {
'Authorization': 'Bearer mock-token',
'X-Firebase-Client': 'fire-admin-node/<XXX_SDK_VERSION_XXX>',
'X-Firebase-Client': `fire-admin-node/${getSdkVersion()}`,
};
const noProjectId = 'Failed to determine project ID. Initialize the SDK with service '
+ 'account credentials, or set project ID as an app option. Alternatively, set the '
Expand Down
3 changes: 2 additions & 1 deletion test/unit/messaging/messaging.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ import {
Messaging, BLACKLISTED_OPTIONS_KEYS, BLACKLISTED_DATA_PAYLOAD_KEYS,
} from '../../../src/messaging/messaging';
import { HttpClient } from '../../../src/utils/api-request';
import { getSdkVersion } from '../../../src/utils/index';

chai.should();
chai.use(sinonChai);
Expand Down Expand Up @@ -321,7 +322,7 @@ describe('Messaging', () => {
const mockAccessToken: string = utils.generateRandomAccessToken();
const expectedHeaders = {
'Authorization': 'Bearer ' + mockAccessToken,
'X-Firebase-Client': 'fire-admin-node/<XXX_SDK_VERSION_XXX>',
'X-Firebase-Client': `fire-admin-node/${getSdkVersion()}`,
'access_token_auth': 'true',
};
const emptyResponse = utils.responseFrom({});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import { ProjectManagementRequestHandler } from '../../../src/project-management
import { HttpClient } from '../../../src/utils/api-request';
import * as mocks from '../../resources/mocks';
import * as utils from '../utils';
import { getSdkVersion } from '../../../src/utils/index';
import { ShaCertificate } from '../../../src/project-management/android-app';
import { AppPlatform } from '../../../src/project-management/app-metadata';

Expand Down Expand Up @@ -72,7 +73,7 @@ describe('ProjectManagementRequestHandler', () => {
beforeEach(() => {
mockApp = mocks.app();
expectedHeaders = {
'X-Client-Version': 'Node/Admin/<XXX_SDK_VERSION_XXX>',
'X-Client-Version': `Node/Admin/${getSdkVersion()}`,
'Authorization': 'Bearer ' + mockAccessToken,
};
requestHandler = new ProjectManagementRequestHandler(mockApp);
Expand Down
3 changes: 2 additions & 1 deletion test/unit/remote-config/remote-config-api-client.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ import * as mocks from '../../resources/mocks';
import { FirebaseAppError } from '../../../src/utils/error';
import { FirebaseApp } from '../../../src/firebase-app';
import { deepCopy } from '../../../src/utils/deep-copy';
import { getSdkVersion } from '../../../src/utils/index';

const expect = chai.expect;

Expand All @@ -53,7 +54,7 @@ describe('RemoteConfigApiClient', () => {

const EXPECTED_HEADERS = {
'Authorization': 'Bearer mock-token',
'X-Firebase-Client': 'fire-admin-node/<XXX_SDK_VERSION_XXX>',
'X-Firebase-Client': `fire-admin-node/${getSdkVersion()}`,
'Accept-Encoding': 'gzip',
};

Expand Down
3 changes: 2 additions & 1 deletion test/unit/security-rules/security-rules-api-client.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import * as utils from '../utils';
import * as mocks from '../../resources/mocks';
import { FirebaseAppError } from '../../../src/utils/error';
import { FirebaseApp } from '../../../src/firebase-app';
import { getSdkVersion } from '../../../src/utils/index';

const expect = chai.expect;

Expand All @@ -42,7 +43,7 @@ describe('SecurityRulesApiClient', () => {
};
const EXPECTED_HEADERS = {
'Authorization': 'Bearer mock-token',
'X-Firebase-Client': 'fire-admin-node/<XXX_SDK_VERSION_XXX>',
'X-Firebase-Client': `fire-admin-node/${getSdkVersion()}`,
};
const noProjectId = 'Failed to determine project ID. Initialize the SDK with service '
+ 'account credentials, or set project ID as an app option. Alternatively, set the '
Expand Down
8 changes: 8 additions & 0 deletions test/unit/utils/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,19 @@ import { ComputeEngineCredential } from '../../../src/auth/credential';
import { HttpClient } from '../../../src/utils/api-request';
import * as utils from '../utils';
import { FirebaseAppError } from '../../../src/utils/error';
import { getSdkVersion } from '../../../src/utils/index';

interface Obj {
[key: string]: any;
}

describe('SDK_VERSION', () => {
it('utils index should retrieve the SDK_VERSION from package.json', () => {
const { version } = require('../../../package.json'); // eslint-disable-line @typescript-eslint/no-var-requires
expect(getSdkVersion()).to.equal(version);
});
});

describe('addReadonlyGetter()', () => {
it('should add a new property to the provided object', () => {
const obj: Obj = {};
Expand Down