Skip to content

Commit

Permalink
feat: preferRest app option for Firestore
Browse files Browse the repository at this point in the history
  • Loading branch information
alexander-fenster committed Sep 15, 2022
1 parent 318f0e4 commit b07ac82
Show file tree
Hide file tree
Showing 7 changed files with 415 additions and 496 deletions.
1 change: 1 addition & 0 deletions etc/firebase-admin.api.md
Expand Up @@ -73,6 +73,7 @@ export interface AppOptions {
databaseAuthVariableOverride?: object | null;
databaseURL?: string;
httpAgent?: Agent;
preferRest?: boolean;
projectId?: string;
serviceAccountId?: string;
storageBucket?: string;
Expand Down
1 change: 1 addition & 0 deletions etc/firebase-admin.app.api.md
Expand Up @@ -23,6 +23,7 @@ export interface AppOptions {
databaseAuthVariableOverride?: object | null;
databaseURL?: string;
httpAgent?: Agent;
preferRest?: boolean;
projectId?: string;
serviceAccountId?: string;
storageBucket?: string;
Expand Down
856 changes: 362 additions & 494 deletions package-lock.json

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion package.json
Expand Up @@ -198,7 +198,7 @@
"uuid": "^8.3.2"
},
"optionalDependencies": {
"@google-cloud/firestore": "^6.0.0",
"@google-cloud/firestore": "^6.2.0",
"@google-cloud/storage": "^6.1.0"
},
"devDependencies": {
Expand Down
8 changes: 8 additions & 0 deletions src/app/core.ts
Expand Up @@ -83,6 +83,14 @@ export interface AppOptions {
* specifying an HTTP Agent in the corresponding factory methods.
*/
httpAgent?: Agent;

/**
* Prefer REST transport for Firestore.
* By default, Firestore will use gRPC. Pass preferRest: true to use
* HTTP/1.1 REST transport if possible. Note that gRPC will still be
* used for calls that need bi-directional streaming.
*/
preferRest?: boolean;
}

/**
Expand Down
6 changes: 5 additions & 1 deletion src/firestore/firestore-internal.ts
Expand Up @@ -57,6 +57,7 @@ export function getFirestoreOptions(app: App): Settings {

const projectId: string | null = utils.getExplicitProjectId(app);
const credential = app.options.credential;
const preferRest = app.options.preferRest;
// eslint-disable-next-line @typescript-eslint/no-var-requires
const { version: firebaseVersion } = require('../../package.json');
if (credential instanceof ServiceAccountCredential) {
Expand All @@ -69,12 +70,15 @@ export function getFirestoreOptions(app: App): Settings {
// guaranteed to be available.
projectId: projectId!,
firebaseVersion,
preferRest,
};
} else if (isApplicationDefault(app.options.credential)) {
// Try to use the Google application default credentials.
// If an explicit project ID is not available, let Firestore client discover one from the
// environment. This prevents the users from having to set GOOGLE_CLOUD_PROJECT in GCP runtimes.
return validator.isNonEmptyString(projectId) ? { projectId, firebaseVersion } : { firebaseVersion };
return validator.isNonEmptyString(projectId)
? { projectId, firebaseVersion, preferRest }
: { firebaseVersion, preferRest };
}

throw new FirebaseFirestoreError({
Expand Down
37 changes: 37 additions & 0 deletions test/unit/firestore/firestore.spec.ts
Expand Up @@ -31,6 +31,9 @@ describe('Firestore', () => {
let mockApp: FirebaseApp;
let mockCredentialApp: FirebaseApp;
let projectIdApp: FirebaseApp;
let preferRestAppDefault: FirebaseApp;
let preferRestAppCredentials: FirebaseApp;
let preferRestAppProjectId: FirebaseApp;
let firestore: any;

let appCredentials: string | undefined;
Expand Down Expand Up @@ -70,6 +73,18 @@ describe('Firestore', () => {
credential: mocks.credential,
projectId: 'explicit-project-id',
});
preferRestAppDefault = mocks.appWithOptions({
preferRest: true,
});
preferRestAppCredentials = mocks.appWithOptions({
credential: mocks.credential,
preferRest: true,
});
preferRestAppProjectId = mocks.appWithOptions({
credential: mocks.credential,
projectId: 'explicit-project-id',
preferRest: true,
});
firestore = new FirestoreService(mockApp);
});

Expand Down Expand Up @@ -210,4 +225,26 @@ describe('Firestore', () => {
});
});
});

describe('options.preferRest', () => {
it('should set preferRest in Firestore options - default', () => {
const options = getFirestoreOptions(preferRestAppDefault);
expect(options.preferRest).to.be.true;
});

it('should set preferRest in Firestore options - credentials', () => {
const options = getFirestoreOptions(preferRestAppCredentials);
expect(options.preferRest).to.be.true;
});

it('should set preferRest in Firestore options - projectId', () => {
const options = getFirestoreOptions(preferRestAppProjectId);
expect(options.preferRest).to.be.true;
});

it('should not set preferRest in Firestore options by default', () => {
const options = getFirestoreOptions(mockApp);
expect(options.preferRest).to.be.undefined;
});
});
});

0 comments on commit b07ac82

Please sign in to comment.