Skip to content
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

Lazy loading modules for faster load times #265

Merged
merged 2 commits into from
May 14, 2018
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
10 changes: 5 additions & 5 deletions src/auth/credential.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,10 @@
* limitations under the License.
*/

import * as jwt from 'jsonwebtoken';
import * as forge from 'node-forge';

// Use untyped import syntax for Node built-ins
import fs = require('fs');
import os = require('os');
import http = require('http');
import path = require('path');
import https = require('https');

import {AppErrorCodes, FirebaseAppError} from '../utils/error';

Expand Down Expand Up @@ -169,6 +164,7 @@ export class Certificate {
throw new FirebaseAppError(AppErrorCodes.INVALID_CREDENTIAL, errorMessage);
}

const forge = require('node-forge');
try {
forge.pki.privateKeyFromPem(this.privateKey);
} catch (error) {
Expand Down Expand Up @@ -257,6 +253,7 @@ export class CertCredential implements Credential {
'Content-Length': postData.length,
},
};
const https = require('https');
return requestAccessToken(https, options, postData);
}

Expand All @@ -275,6 +272,7 @@ export class CertCredential implements Credential {
].join(' '),
};

const jwt = require('jsonwebtoken');
// This method is actually synchronous so we can capture and return the buffer.
return jwt.sign(claims, this.certificate_.privateKey, {
audience: GOOGLE_TOKEN_AUDIENCE,
Expand Down Expand Up @@ -321,6 +319,7 @@ export class RefreshTokenCredential implements Credential {
'Content-Length': postData.length,
},
};
const https = require('https');
return requestAccessToken(https, options, postData);
}

Expand All @@ -345,6 +344,7 @@ export class MetadataServiceCredential implements Credential {
'Content-Length': 0,
},
};
const http = require('http');
return requestAccessToken(http, options);
}

Expand Down
18 changes: 12 additions & 6 deletions src/firebase-app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -286,7 +286,8 @@ export class FirebaseApp {
*/
public auth(): Auth {
return this.ensureService_('auth', () => {
return new Auth(this);
const authService: typeof Auth = require('./auth/auth').Auth;
return new authService(this);
});
}

Expand All @@ -297,7 +298,8 @@ export class FirebaseApp {
*/
public database(url?: string): Database {
const service: DatabaseService = this.ensureService_('database', () => {
return new DatabaseService(this);
const dbService: typeof DatabaseService = require('./database/database').DatabaseService;
return new dbService(this);
});
return service.getDatabase(url);
}
Expand All @@ -309,7 +311,8 @@ export class FirebaseApp {
*/
public messaging(): Messaging {
return this.ensureService_('messaging', () => {
return new Messaging(this);
const messagingService: typeof Messaging = require('./messaging/messaging').Messaging;
return new messagingService(this);
});
}

Expand All @@ -320,13 +323,15 @@ export class FirebaseApp {
*/
public storage(): Storage {
return this.ensureService_('storage', () => {
return new Storage(this);
const storageService: typeof Storage = require('./storage/storage').Storage;
return new storageService(this);
});
}

public firestore(): Firestore {
const service: FirestoreService = this.ensureService_('firestore', () => {
return new FirestoreService(this);
const firestoreService: typeof FirestoreService = require('./firestore/firestore').FirestoreService;
return new firestoreService(this);
});
return service.client;
}
Expand All @@ -338,7 +343,8 @@ export class FirebaseApp {
*/
public instanceId(): InstanceId {
return this.ensureService_('iid', () => {
return new InstanceId(this);
const iidService: typeof InstanceId = require('./instance-id/instance-id').InstanceId;
return new iidService(this);
});
}

Expand Down
12 changes: 8 additions & 4 deletions src/firebase-namespace.ts
Original file line number Diff line number Diff line change
Expand Up @@ -325,7 +325,8 @@ export class FirebaseNamespace {
const fn: FirebaseServiceNamespace<Auth> = (app?: FirebaseApp) => {
return this.ensureApp(app).auth();
};
return Object.assign(fn, {Auth});
const auth = require('./auth/auth').Auth;
return Object.assign(fn, {Auth: auth});
}

/**
Expand All @@ -347,7 +348,8 @@ export class FirebaseNamespace {
const fn: FirebaseServiceNamespace<Messaging> = (app?: FirebaseApp) => {
return this.ensureApp(app).messaging();
};
return Object.assign(fn, {Messaging});
const messaging = require('./messaging/messaging').Messaging;
return Object.assign(fn, {Messaging: messaging});
}

/**
Expand All @@ -358,7 +360,8 @@ export class FirebaseNamespace {
const fn: FirebaseServiceNamespace<Storage> = (app?: FirebaseApp) => {
return this.ensureApp(app).storage();
};
return Object.assign(fn, {Storage});
const storage = require('./storage/storage').Storage;
return Object.assign(fn, {Storage: storage});
}

/**
Expand All @@ -380,7 +383,8 @@ export class FirebaseNamespace {
const fn: FirebaseServiceNamespace<InstanceId> = (app?: FirebaseApp) => {
return this.ensureApp(app).instanceId();
};
return Object.assign(fn, {InstanceId});
const instanceId = require('./instance-id/instance-id').InstanceId;
return Object.assign(fn, {InstanceId: instanceId});
}

/**
Expand Down