Skip to content

Commit

Permalink
Lazy-load all GRPC dependencies (#197)
Browse files Browse the repository at this point in the history
  • Loading branch information
schmidt-sebastian authored May 23, 2018
1 parent 54e3ca4 commit 1f2f525
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 142 deletions.
105 changes: 21 additions & 84 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 1 addition & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,7 @@
"test": "repo-tools test run --cmd npm -- run cover"
},
"dependencies": {
"@google-cloud/common": "^0.18.5",
"@google-cloud/common-grpc": "^0.6.1",
"@google-cloud/common": "^0.18.7",
"bun": "^0.0.12",
"deep-equal": "^1.0.1",
"extend": "^3.0.1",
Expand Down
43 changes: 26 additions & 17 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,11 @@
'use strict';

const bun = require('bun');
const common = require('@google-cloud/common');
const commonGrpc = require('@google-cloud/common-grpc');
const extend = require('extend');
const is = require('is');
const through = require('through2');
const util = require('util');

const v1beta1 = require('./v1beta1');
const libVersion = require('../package.json').version;

const path = require('./path');
Expand Down Expand Up @@ -94,6 +91,16 @@ let WriteBatch;
*/
let Transaction;

/*!
* @see v1beta1
*/
let v1beta1; // Lazy-loaded in `_ensureClient()`

/*!
* @see @google-cloud/common
*/
let common; // Lazy-loaded in `_ensureClient()`

/*!
* HTTP header for the resource prefix to improve routing and project isolation
* by the backend.
Expand Down Expand Up @@ -187,25 +194,16 @@ const GRPC_UNAVAILABLE = 14;
* region_tag:firestore_quickstart
* Full quickstart example:
*/
class Firestore extends commonGrpc.Service {
class Firestore {
/**
* @param {Object=} options - [Configuration object](#/docs).
*/
constructor(options) {
let config = {
service: 'firestore',
apiVersion: 'v1beta1',
protoServices: {},
packageJson: require('../package.json'),
};

options = extend({}, options, {
libName: 'gccl',
libVersion: libVersion,
});

super(config, options);

/**
* @private
* @type {object|null}
Expand Down Expand Up @@ -701,10 +699,12 @@ class Firestore extends commonGrpc.Service {
*/
_ensureClient() {
if (!this._clientInitialized) {
common = require('@google-cloud/common');

this._clientInitialized = new Promise((resolve, reject) => {
this._firestoreClient = v1beta1(
this._initalizationOptions
).firestoreClient(this._initalizationOptions);
this._firestoreClient = module.exports
.v1beta1(this._initalizationOptions)
.firestoreClient(this._initalizationOptions);

Firestore.log('Firestore', 'Initialized Firestore GAPIC Client');

Expand Down Expand Up @@ -1190,7 +1190,16 @@ module.exports.Firestore = Firestore;
* @see v1beta1
* @type {function}
*/
module.exports.v1beta1 = v1beta1;
Object.defineProperty(module.exports, 'v1beta1', {
// The v1beta1 module is very large. To avoid pulling it in from static
// scope, we lazy-load and cache the module.
get: () => {
if (!v1beta1) {
v1beta1 = require('./v1beta1');
}
return v1beta1;
},
});

/**
* {@link GeoPoint} class.
Expand Down
39 changes: 0 additions & 39 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,6 @@
const assert = require('power-assert');
const extend = require('extend');
const grpc = require('google-gax').grpc().grpc;
const GrpcService = require('@google-cloud/common').GrpcService;
const proxyquire = require('proxyquire');
const util = require('@google-cloud/common').util;
const is = require('is');
const Buffer = require('safe-buffer').Buffer;
const through = require('through2');
Expand Down Expand Up @@ -279,37 +276,12 @@ function stream() {
}

describe('instantiation', function() {
function FakeGrpcService() {
this.calledWith_ = arguments;
}

let fakeUtil = extend({}, util, {});
extend(FakeGrpcService, GrpcService);

let Firestore;

before(function() {
Firestore = proxyquire('../', {
'@google-cloud/common': {
util: fakeUtil,
},
'@google-cloud/common-grpc': {
Service: FakeGrpcService,
},
});
});

beforeEach(() => {
extend(FakeGrpcService, GrpcService);
});

it('creates instance', function() {
let firestore = new Firestore({
projectId: 'test-project',
sslCreds: grpc.credentials.createInsecure(),
});
assert(firestore instanceof Firestore);
assert.strictEqual(firestore.calledWith_[1].projectId, 'test-project');
});

it('detects project id', function() {
Expand Down Expand Up @@ -365,17 +337,6 @@ describe('instantiation', function() {
);
});

it('inherits from GrpcService', function() {
let firestore = new Firestore({
projectId: 'test-project',
sslCreds: grpc.credentials.createInsecure(),
});
assert(firestore instanceof FakeGrpcService);

let calledWith = firestore.calledWith_[0];
assert.equal(calledWith.service, 'firestore');
});

it('exports all types', function() {
// Ordering as per firestore.d.ts
assert.ok(is.defined(Firestore.Firestore));
Expand Down

0 comments on commit 1f2f525

Please sign in to comment.