3232 */
3333'use strict' ;
3434
35- var GoogleAuth = require ( 'google-auth-library' ) ;
36- var AuthFactory = new GoogleAuth ( ) ;
35+ var autoAuth = require ( 'google-auto-auth' ) ;
3736
38- var createCredPromise = require ( './auth' ) . createCredPromise ;
3937var gax = require ( './gax' ) ;
4038
4139/**
42- * @callback CredentialsCallback
43- * @param {?Error } err - An error if authentication failed.
44- * @param {?grpc.ChannelCredentials } creds - A gRPC credentials if succeeded.
40+ * A class which keeps the context of gRPC and auth for the gRPC.
41+ *
42+ * @param {Object= } options - The optional parameters. It will be directly
43+ * passed to google-auto-auth library, so parameters like keyFile or
44+ * credentials will be valid.
45+ * @param {Object= } options.auth - An instance of google-auto-auth.
46+ * When specified, this auth instance will be used instead of creating
47+ * a new one.
48+ * @param {Object= } options.grpc - When specified, this will be used
49+ * for the 'grpc' module in this context. By default, it will load the grpc
50+ * module in the standard way.
51+ * @constructor
4552 */
53+ function GrpcClient ( options ) {
54+ options = options || { } ;
55+ this . auth = options . auth || autoAuth ( options ) ;
56+ this . grpc = options . grpc || require ( 'grpc' ) ;
57+ }
58+ module . exports = GrpcClient ;
4659
4760/**
48- * To authorize requests through gRPC, we must get the raw google-auth-library
49- * auth client object.
50- *
61+ * Creates a gRPC credentials from the authentications.
5162 * @private
5263 *
53- * @param {CredentialsCallback } callback - The callback function.
54- * @param {Object= } opts - options values for configuring auth
55- * @param {(String|String[]) } opts.scopes - the scope or scopes to use when
56- * obtaining the credentials.
64+ * @param {Object } opts - options values for configuring credentials.
5765 * @param {Object= } opts.sslCreds - when specified, this is used instead
58- * of default credentials.
59- * @param {Object= } opts.grpc - when specified, this is used as the module
60- * for grpc .
66+ * of default channel credentials.
67+ * @param {Object } auth - the authentication object.
68+ * @return { Object } The gRPC credential object .
6169 */
62- function getCredentials ( callback , opts ) {
63- AuthFactory . getApplicationDefault ( function ( err , auth ) {
64- if ( err ) {
65- callback ( err ) ;
66- return ;
67- }
68-
69- /* Apply any provided scope if there are required */
70- if ( opts . scopes && auth . createScopedRequired &&
71- auth . createScopedRequired ( ) ) {
72- auth = auth . createScoped ( opts . scopes ) ;
73- }
74- var grpc = opts . grpc || require ( 'grpc' ) ;
75- var sslCreds = opts . sslCreds || grpc . credentials . createSsl ( ) ;
76- var credentials = grpc . credentials . combineChannelCredentials (
70+ GrpcClient . prototype . _getCredentials = function _getCredentials ( opts , auth ) {
71+ var sslCreds = opts . sslCreds || this . grpc . credentials . createSsl ( ) ;
72+ var credentials = this . grpc . credentials . combineChannelCredentials (
7773 sslCreds ,
78- grpc . credentials . createFromGoogleCredential ( auth )
79- ) ;
80-
81- callback ( null , credentials ) ;
82- } ) ;
83- }
74+ this . grpc . credentials . createFromGoogleCredential ( auth )
75+ ) ;
76+ return credentials ;
77+ } ;
8478
8579/**
8680 * Load grpc proto services with the specific arguments.
87- * @param {Object= } optGrpc - The grpc module when specified. If not, it will
88- * load the default grpc module.
89- * @param {Array= } optArguments - The argument list to be passed to grpc.load().
90- * @return {Object } The grpc loaded result (the toplevel namespace object).
81+ * @param {Array= } args - The argument list to be passed to grpc.load().
82+ * @return {Object } The gRPC loaded result (the toplevel namespace object).
9183 */
92- exports . loadGrpc = function loadGrpc ( optGrpc , optArguments ) {
93- if ( optArguments === undefined && Array . isArray ( optGrpc ) ) {
94- optArguments = optGrpc ;
95- optGrpc = null ;
84+ GrpcClient . prototype . load = function ( args ) {
85+ if ( ! args ) {
86+ args = [ ] ;
87+ } else if ( ! Array . isArray ( args ) ) {
88+ args = [ args ] ;
9689 }
97- var grpc = optGrpc || require ( 'grpc' ) ;
98- return grpc . load . apply ( grpc , optArguments || [ ] ) ;
90+ return this . grpc . load . apply ( this . grpc , args ) ;
9991} ;
10092
101- exports . constructSettingsGrpc = function constructSettingsGrpc (
93+ /**
94+ * A wrapper of {@link constructSettings} function with under the gRPC context.
95+ *
96+ * Most of parameters are common among constructSettings, please take a look.
97+ * @param {string } serviceName - The fullly-qualified name of the service.
98+ * @param {Object } clientConfig - A dictionary of the client config.
99+ * @param {Object } configOverrides - A dictionary of overriding configs.
100+ * @param {number } timeout - The timeout parameter.
101+ * @param {Object } pageDescriptors - A dictionary of method names to page
102+ * descriptor instances.
103+ * @param {Object } bundleDescriptors - A dictionary of method names to bundle
104+ * descriptor instances.
105+ * @param {Object } headers - A dictionary of additional HTTP header name to
106+ * its value.
107+ * @return {Object } A mapping of method names to CallSettings.
108+ */
109+ GrpcClient . prototype . constructSettings = function constructSettings (
102110 serviceName ,
103111 clientConfig ,
104112 configOverrides ,
105113 timeout ,
106114 pageDescriptors ,
107115 bundleDescriptors ,
108- headers ,
109- optGrpc ) {
110- var grpc = optGrpc || require ( 'grpc' ) ;
111- var metadata = new grpc . Metadata ( ) ;
116+ headers ) {
117+ var metadata = new this . grpc . Metadata ( ) ;
112118 for ( var key in headers ) {
113119 metadata . set ( key , headers [ key ] ) ;
114120 }
115121 return gax . constructSettings (
116122 serviceName ,
117123 clientConfig ,
118124 configOverrides ,
119- grpc . status ,
125+ this . grpc . status ,
120126 timeout ,
121127 pageDescriptors ,
122128 bundleDescriptors ,
123129 { metadata : metadata } ) ;
124130} ;
125131
126132/**
127- * Creates a promise which resolves to an rpc stub.
128- *
129- * @param {String } servicePath - The domain name of the API remote host.
130- * @param {Number } port - The port on which to connect to the remote host.
131- * @param {function() } CreateStub - The constructor used to create a grpc stub
132- * instance.
133- * @param {Object } options - optional settings. This options will be passed
134- * to `getCredentials`.
135- * @param {GetCredentialsFunc= } options.getCredentials - the callback used
136- * to obtain the credentials. If not specified, use the default
137- * implementation.
138- * @return {Promise } A promise which resolves to an rpc stub.
133+ * Creates a gRPC stub with current gRPC and auth.
134+ * @param {string } servicePath - The name of the server of the service.
135+ * @param {number } port - The port of the service.
136+ * @param {function } CreateStub - The constructor function of the stub.
137+ * @param {Object= } options - The optional arguments to customize
138+ * gRpc connection.
139+ * @param {grpc.ClientCredentials } options.sslCreds - The credentials to be used
140+ * to set up gRPC connection.
141+ * @return {Promise } A promse which resolves to a gRPC stub instance.
139142 */
140- exports . createStub = function createStub (
143+ GrpcClient . prototype . createStub = function (
141144 servicePath , port , CreateStub , options ) {
142145 options = options || { } ;
143- var creds = createCredPromise ( options . getCredentials || getCredentials ,
144- options ) ;
145- return creds . then ( function buildStub ( credentials ) {
146- return new CreateStub ( servicePath + ':' + port , credentials ) ;
147- } ) ;
146+ var serviceAddress = servicePath + ':' + port ;
147+ return new Promise ( function ( resolve , reject ) {
148+ this . auth . getAuthClient ( function ( err , auth ) {
149+ if ( err ) {
150+ reject ( err ) ;
151+ } else {
152+ resolve ( auth ) ;
153+ }
154+ } ) ;
155+ } . bind ( this ) ) . then ( this . _getCredentials . bind ( this , options ) )
156+ . then ( function buildStub ( credentials ) {
157+ return new CreateStub ( serviceAddress , credentials ) ;
158+ } ) ;
148159} ;
149160
150161/**
@@ -157,8 +168,9 @@ exports.createStub = function createStub(
157168 * @return {function(Object):number } - a function to compute the byte length
158169 * for an object.
159170 */
160- exports . createByteLengthFunction = function createByteLengthFunction ( message ) {
161- return function getByteLength ( obj ) {
162- return message . encode ( obj ) . buffer . length ;
163- } ;
164- } ;
171+ GrpcClient . createByteLengthFunction =
172+ function createByteLengthFunction ( message ) {
173+ return function getByteLength ( obj ) {
174+ return message . encode ( obj ) . buffer . length ;
175+ } ;
176+ } ;
0 commit comments