src_client.js.html 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="utf-8">
  5. <title>JSDoc: Source: src/client.js</title>
  6. <script src="scripts/prettify/prettify.js"> </script>
  7. <script src="scripts/prettify/lang-css.js"> </script>
  8. <!--[if lt IE 9]>
  9. <script src="//html5shiv.googlecode.com/svn/trunk/html5.js"></script>
  10. <![endif]-->
  11. <link type="text/css" rel="stylesheet" href="styles/prettify-tomorrow.css">
  12. <link type="text/css" rel="stylesheet" href="styles/jsdoc-default.css">
  13. </head>
  14. <body>
  15. <div id="main">
  16. <h1 class="page-title">Source: src/client.js</h1>
  17. <section>
  18. <article>
  19. <pre class="prettyprint source linenums"><code>/*
  20. *
  21. * Copyright 2015, Google Inc.
  22. * All rights reserved.
  23. *
  24. * Redistribution and use in source and binary forms, with or without
  25. * modification, are permitted provided that the following conditions are
  26. * met:
  27. *
  28. * * Redistributions of source code must retain the above copyright
  29. * notice, this list of conditions and the following disclaimer.
  30. * * Redistributions in binary form must reproduce the above
  31. * copyright notice, this list of conditions and the following disclaimer
  32. * in the documentation and/or other materials provided with the
  33. * distribution.
  34. * * Neither the name of Google Inc. nor the names of its
  35. * contributors may be used to endorse or promote products derived from
  36. * this software without specific prior written permission.
  37. *
  38. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  39. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  40. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  41. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  42. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  43. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  44. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  45. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  46. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  47. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  48. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  49. *
  50. */
  51. /**
  52. * Server module
  53. * @module
  54. */
  55. 'use strict';
  56. var _ = require('lodash');
  57. var grpc = require('bindings')('grpc.node');
  58. var common = require('./common.js');
  59. var EventEmitter = require('events').EventEmitter;
  60. var stream = require('stream');
  61. var Readable = stream.Readable;
  62. var Writable = stream.Writable;
  63. var Duplex = stream.Duplex;
  64. var util = require('util');
  65. var version = require('../package.json').version;
  66. util.inherits(ClientWritableStream, Writable);
  67. /**
  68. * A stream that the client can write to. Used for calls that are streaming from
  69. * the client side.
  70. * @constructor
  71. * @param {grpc.Call} call The call object to send data with
  72. * @param {function(*):Buffer=} serialize Serialization function for writes.
  73. */
  74. function ClientWritableStream(call, serialize) {
  75. Writable.call(this, {objectMode: true});
  76. this.call = call;
  77. this.serialize = common.wrapIgnoreNull(serialize);
  78. this.on('finish', function() {
  79. var batch = {};
  80. batch[grpc.opType.SEND_CLOSE_FROM_CLIENT] = true;
  81. call.startBatch(batch, function() {});
  82. });
  83. }
  84. /**
  85. * Attempt to write the given chunk. Calls the callback when done. This is an
  86. * implementation of a method needed for implementing stream.Writable.
  87. * @access private
  88. * @param {Buffer} chunk The chunk to write
  89. * @param {string} encoding Ignored
  90. * @param {function(Error=)} callback Called when the write is complete
  91. */
  92. function _write(chunk, encoding, callback) {
  93. /* jshint validthis: true */
  94. var batch = {};
  95. batch[grpc.opType.SEND_MESSAGE] = this.serialize(chunk);
  96. this.call.startBatch(batch, function(err, event) {
  97. if (err) {
  98. // Something has gone wrong. Stop writing by failing to call callback
  99. return;
  100. }
  101. callback();
  102. });
  103. }
  104. ClientWritableStream.prototype._write = _write;
  105. util.inherits(ClientReadableStream, Readable);
  106. /**
  107. * A stream that the client can read from. Used for calls that are streaming
  108. * from the server side.
  109. * @constructor
  110. * @param {grpc.Call} call The call object to read data with
  111. * @param {function(Buffer):*=} deserialize Deserialization function for reads
  112. */
  113. function ClientReadableStream(call, deserialize) {
  114. Readable.call(this, {objectMode: true});
  115. this.call = call;
  116. this.finished = false;
  117. this.reading = false;
  118. this.deserialize = common.wrapIgnoreNull(deserialize);
  119. }
  120. /**
  121. * Read the next object from the stream.
  122. * @access private
  123. * @param {*} size Ignored because we use objectMode=true
  124. */
  125. function _read(size) {
  126. /* jshint validthis: true */
  127. var self = this;
  128. /**
  129. * Callback to be called when a READ event is received. Pushes the data onto
  130. * the read queue and starts reading again if applicable
  131. * @param {grpc.Event} event READ event object
  132. */
  133. function readCallback(err, event) {
  134. if (err) {
  135. // Something has gone wrong. Stop reading and wait for status
  136. self.finished = true;
  137. return;
  138. }
  139. var data = event.read;
  140. if (self.push(self.deserialize(data)) &amp;&amp; data !== null) {
  141. var read_batch = {};
  142. read_batch[grpc.opType.RECV_MESSAGE] = true;
  143. self.call.startBatch(read_batch, readCallback);
  144. } else {
  145. self.reading = false;
  146. }
  147. }
  148. if (self.finished) {
  149. self.push(null);
  150. } else {
  151. if (!self.reading) {
  152. self.reading = true;
  153. var read_batch = {};
  154. read_batch[grpc.opType.RECV_MESSAGE] = true;
  155. self.call.startBatch(read_batch, readCallback);
  156. }
  157. }
  158. }
  159. ClientReadableStream.prototype._read = _read;
  160. util.inherits(ClientDuplexStream, Duplex);
  161. /**
  162. * A stream that the client can read from or write to. Used for calls with
  163. * duplex streaming.
  164. * @constructor
  165. * @param {grpc.Call} call Call object to proxy
  166. * @param {function(*):Buffer=} serialize Serialization function for requests
  167. * @param {function(Buffer):*=} deserialize Deserialization function for
  168. * responses
  169. */
  170. function ClientDuplexStream(call, serialize, deserialize) {
  171. Duplex.call(this, {objectMode: true});
  172. this.serialize = common.wrapIgnoreNull(serialize);
  173. this.deserialize = common.wrapIgnoreNull(deserialize);
  174. this.call = call;
  175. this.on('finish', function() {
  176. var batch = {};
  177. batch[grpc.opType.SEND_CLOSE_FROM_CLIENT] = true;
  178. call.startBatch(batch, function() {});
  179. });
  180. }
  181. ClientDuplexStream.prototype._read = _read;
  182. ClientDuplexStream.prototype._write = _write;
  183. /**
  184. * Cancel the ongoing call
  185. */
  186. function cancel() {
  187. /* jshint validthis: true */
  188. this.call.cancel();
  189. }
  190. ClientReadableStream.prototype.cancel = cancel;
  191. ClientWritableStream.prototype.cancel = cancel;
  192. ClientDuplexStream.prototype.cancel = cancel;
  193. /**
  194. * Get the endpoint this call/stream is connected to.
  195. * @return {string} The URI of the endpoint
  196. */
  197. function getPeer() {
  198. /* jshint validthis: true */
  199. return this.call.getPeer();
  200. }
  201. ClientReadableStream.prototype.getPeer = getPeer;
  202. ClientWritableStream.prototype.getPeer = getPeer;
  203. ClientDuplexStream.prototype.getPeer = getPeer;
  204. /**
  205. * Get a call object built with the provided options. Keys for options are
  206. * 'deadline', which takes a date or number, and 'host', which takes a string
  207. * and overrides the hostname to connect to.
  208. * @param {Object} options Options map.
  209. */
  210. function getCall(channel, method, options) {
  211. var deadline;
  212. var host;
  213. var parent;
  214. var propagate_flags;
  215. if (options) {
  216. deadline = options.deadline;
  217. host = options.host;
  218. parent = _.get(options, 'parent.call');
  219. propagate_flags = options.propagate_flags;
  220. }
  221. if (deadline === undefined) {
  222. deadline = Infinity;
  223. }
  224. return new grpc.Call(channel, method, deadline, host,
  225. parent, propagate_flags);
  226. }
  227. /**
  228. * Get a function that can make unary requests to the specified method.
  229. * @param {string} method The name of the method to request
  230. * @param {function(*):Buffer} serialize The serialization function for inputs
  231. * @param {function(Buffer)} deserialize The deserialization function for
  232. * outputs
  233. * @return {Function} makeUnaryRequest
  234. */
  235. function makeUnaryRequestFunction(method, serialize, deserialize) {
  236. /**
  237. * Make a unary request with this method on the given channel with the given
  238. * argument, callback, etc.
  239. * @this {Client} Client object. Must have a channel member.
  240. * @param {*} argument The argument to the call. Should be serializable with
  241. * serialize
  242. * @param {function(?Error, value=)} callback The callback to for when the
  243. * response is received
  244. * @param {array=} metadata Array of metadata key/value pairs to add to the
  245. * call
  246. * @param {Object=} options Options map
  247. * @return {EventEmitter} An event emitter for stream related events
  248. */
  249. function makeUnaryRequest(argument, callback, metadata, options) {
  250. /* jshint validthis: true */
  251. var emitter = new EventEmitter();
  252. var call = getCall(this.channel, method, options);
  253. if (metadata === null || metadata === undefined) {
  254. metadata = {};
  255. }
  256. emitter.cancel = function cancel() {
  257. call.cancel();
  258. };
  259. emitter.getPeer = function getPeer() {
  260. return call.getPeer();
  261. };
  262. this.updateMetadata(this.auth_uri, metadata, function(error, metadata) {
  263. if (error) {
  264. call.cancel();
  265. callback(error);
  266. return;
  267. }
  268. var client_batch = {};
  269. client_batch[grpc.opType.SEND_INITIAL_METADATA] = metadata;
  270. client_batch[grpc.opType.SEND_MESSAGE] = serialize(argument);
  271. client_batch[grpc.opType.SEND_CLOSE_FROM_CLIENT] = true;
  272. client_batch[grpc.opType.RECV_INITIAL_METADATA] = true;
  273. client_batch[grpc.opType.RECV_MESSAGE] = true;
  274. client_batch[grpc.opType.RECV_STATUS_ON_CLIENT] = true;
  275. call.startBatch(client_batch, function(err, response) {
  276. emitter.emit('status', response.status);
  277. if (response.status.code !== grpc.status.OK) {
  278. var error = new Error(response.status.details);
  279. error.code = response.status.code;
  280. error.metadata = response.status.metadata;
  281. callback(error);
  282. return;
  283. } else {
  284. if (err) {
  285. // Got a batch error, but OK status. Something went wrong
  286. callback(err);
  287. return;
  288. }
  289. }
  290. emitter.emit('metadata', response.metadata);
  291. callback(null, deserialize(response.read));
  292. });
  293. });
  294. return emitter;
  295. }
  296. return makeUnaryRequest;
  297. }
  298. /**
  299. * Get a function that can make client stream requests to the specified method.
  300. * @param {string} method The name of the method to request
  301. * @param {function(*):Buffer} serialize The serialization function for inputs
  302. * @param {function(Buffer)} deserialize The deserialization function for
  303. * outputs
  304. * @return {Function} makeClientStreamRequest
  305. */
  306. function makeClientStreamRequestFunction(method, serialize, deserialize) {
  307. /**
  308. * Make a client stream request with this method on the given channel with the
  309. * given callback, etc.
  310. * @this {Client} Client object. Must have a channel member.
  311. * @param {function(?Error, value=)} callback The callback to for when the
  312. * response is received
  313. * @param {array=} metadata Array of metadata key/value pairs to add to the
  314. * call
  315. * @param {Object=} options Options map
  316. * @return {EventEmitter} An event emitter for stream related events
  317. */
  318. function makeClientStreamRequest(callback, metadata, options) {
  319. /* jshint validthis: true */
  320. var call = getCall(this.channel, method, options);
  321. if (metadata === null || metadata === undefined) {
  322. metadata = {};
  323. }
  324. var stream = new ClientWritableStream(call, serialize);
  325. this.updateMetadata(this.auth_uri, metadata, function(error, metadata) {
  326. if (error) {
  327. call.cancel();
  328. callback(error);
  329. return;
  330. }
  331. var metadata_batch = {};
  332. metadata_batch[grpc.opType.SEND_INITIAL_METADATA] = metadata;
  333. metadata_batch[grpc.opType.RECV_INITIAL_METADATA] = true;
  334. call.startBatch(metadata_batch, function(err, response) {
  335. if (err) {
  336. // The call has stopped for some reason. A non-OK status will arrive
  337. // in the other batch.
  338. return;
  339. }
  340. stream.emit('metadata', response.metadata);
  341. });
  342. var client_batch = {};
  343. client_batch[grpc.opType.RECV_MESSAGE] = true;
  344. client_batch[grpc.opType.RECV_STATUS_ON_CLIENT] = true;
  345. call.startBatch(client_batch, function(err, response) {
  346. stream.emit('status', response.status);
  347. if (response.status.code !== grpc.status.OK) {
  348. var error = new Error(response.status.details);
  349. error.code = response.status.code;
  350. error.metadata = response.status.metadata;
  351. callback(error);
  352. return;
  353. } else {
  354. if (err) {
  355. // Got a batch error, but OK status. Something went wrong
  356. callback(err);
  357. return;
  358. }
  359. }
  360. callback(null, deserialize(response.read));
  361. });
  362. });
  363. return stream;
  364. }
  365. return makeClientStreamRequest;
  366. }
  367. /**
  368. * Get a function that can make server stream requests to the specified method.
  369. * @param {string} method The name of the method to request
  370. * @param {function(*):Buffer} serialize The serialization function for inputs
  371. * @param {function(Buffer)} deserialize The deserialization function for
  372. * outputs
  373. * @return {Function} makeServerStreamRequest
  374. */
  375. function makeServerStreamRequestFunction(method, serialize, deserialize) {
  376. /**
  377. * Make a server stream request with this method on the given channel with the
  378. * given argument, etc.
  379. * @this {SurfaceClient} Client object. Must have a channel member.
  380. * @param {*} argument The argument to the call. Should be serializable with
  381. * serialize
  382. * @param {array=} metadata Array of metadata key/value pairs to add to the
  383. * call
  384. * @param {Object} options Options map
  385. * @return {EventEmitter} An event emitter for stream related events
  386. */
  387. function makeServerStreamRequest(argument, metadata, options) {
  388. /* jshint validthis: true */
  389. var call = getCall(this.channel, method, options);
  390. if (metadata === null || metadata === undefined) {
  391. metadata = {};
  392. }
  393. var stream = new ClientReadableStream(call, deserialize);
  394. this.updateMetadata(this.auth_uri, metadata, function(error, metadata) {
  395. if (error) {
  396. call.cancel();
  397. stream.emit('error', error);
  398. return;
  399. }
  400. var start_batch = {};
  401. start_batch[grpc.opType.SEND_INITIAL_METADATA] = metadata;
  402. start_batch[grpc.opType.RECV_INITIAL_METADATA] = true;
  403. start_batch[grpc.opType.SEND_MESSAGE] = serialize(argument);
  404. start_batch[grpc.opType.SEND_CLOSE_FROM_CLIENT] = true;
  405. call.startBatch(start_batch, function(err, response) {
  406. if (err) {
  407. // The call has stopped for some reason. A non-OK status will arrive
  408. // in the other batch.
  409. return;
  410. }
  411. stream.emit('metadata', response.metadata);
  412. });
  413. var status_batch = {};
  414. status_batch[grpc.opType.RECV_STATUS_ON_CLIENT] = true;
  415. call.startBatch(status_batch, function(err, response) {
  416. stream.emit('status', response.status);
  417. if (response.status.code !== grpc.status.OK) {
  418. var error = new Error(response.status.details);
  419. error.code = response.status.code;
  420. error.metadata = response.status.metadata;
  421. stream.emit('error', error);
  422. return;
  423. } else {
  424. if (err) {
  425. // Got a batch error, but OK status. Something went wrong
  426. stream.emit('error', err);
  427. return;
  428. }
  429. }
  430. });
  431. });
  432. return stream;
  433. }
  434. return makeServerStreamRequest;
  435. }
  436. /**
  437. * Get a function that can make bidirectional stream requests to the specified
  438. * method.
  439. * @param {string} method The name of the method to request
  440. * @param {function(*):Buffer} serialize The serialization function for inputs
  441. * @param {function(Buffer)} deserialize The deserialization function for
  442. * outputs
  443. * @return {Function} makeBidiStreamRequest
  444. */
  445. function makeBidiStreamRequestFunction(method, serialize, deserialize) {
  446. /**
  447. * Make a bidirectional stream request with this method on the given channel.
  448. * @this {SurfaceClient} Client object. Must have a channel member.
  449. * @param {array=} metadata Array of metadata key/value pairs to add to the
  450. * call
  451. * @param {Options} options Options map
  452. * @return {EventEmitter} An event emitter for stream related events
  453. */
  454. function makeBidiStreamRequest(metadata, options) {
  455. /* jshint validthis: true */
  456. var call = getCall(this.channel, method, options);
  457. if (metadata === null || metadata === undefined) {
  458. metadata = {};
  459. }
  460. var stream = new ClientDuplexStream(call, serialize, deserialize);
  461. this.updateMetadata(this.auth_uri, metadata, function(error, metadata) {
  462. if (error) {
  463. call.cancel();
  464. stream.emit('error', error);
  465. return;
  466. }
  467. var start_batch = {};
  468. start_batch[grpc.opType.SEND_INITIAL_METADATA] = metadata;
  469. start_batch[grpc.opType.RECV_INITIAL_METADATA] = true;
  470. call.startBatch(start_batch, function(err, response) {
  471. if (err) {
  472. // The call has stopped for some reason. A non-OK status will arrive
  473. // in the other batch.
  474. return;
  475. }
  476. stream.emit('metadata', response.metadata);
  477. });
  478. var status_batch = {};
  479. status_batch[grpc.opType.RECV_STATUS_ON_CLIENT] = true;
  480. call.startBatch(status_batch, function(err, response) {
  481. stream.emit('status', response.status);
  482. if (response.status.code !== grpc.status.OK) {
  483. var error = new Error(response.status.details);
  484. error.code = response.status.code;
  485. error.metadata = response.status.metadata;
  486. stream.emit('error', error);
  487. return;
  488. } else {
  489. if (err) {
  490. // Got a batch error, but OK status. Something went wrong
  491. stream.emit('error', err);
  492. return;
  493. }
  494. }
  495. });
  496. });
  497. return stream;
  498. }
  499. return makeBidiStreamRequest;
  500. }
  501. /**
  502. * Map with short names for each of the requester maker functions. Used in
  503. * makeClientConstructor
  504. */
  505. var requester_makers = {
  506. unary: makeUnaryRequestFunction,
  507. server_stream: makeServerStreamRequestFunction,
  508. client_stream: makeClientStreamRequestFunction,
  509. bidi: makeBidiStreamRequestFunction
  510. };
  511. /**
  512. * Creates a constructor for a client with the given methods. The methods object
  513. * maps method name to an object with the following keys:
  514. * path: The path on the server for accessing the method. For example, for
  515. * protocol buffers, we use "/service_name/method_name"
  516. * requestStream: bool indicating whether the client sends a stream
  517. * resonseStream: bool indicating whether the server sends a stream
  518. * requestSerialize: function to serialize request objects
  519. * responseDeserialize: function to deserialize response objects
  520. * @param {Object} methods An object mapping method names to method attributes
  521. * @param {string} serviceName The fully qualified name of the service
  522. * @return {function(string, Object)} New client constructor
  523. */
  524. exports.makeClientConstructor = function(methods, serviceName) {
  525. /**
  526. * Create a client with the given methods
  527. * @constructor
  528. * @param {string} address The address of the server to connect to
  529. * @param {grpc.Credentials} credentials Credentials to use to connect
  530. * to the server
  531. * @param {Object} options Options to pass to the underlying channel
  532. * @param {function(string, Object, function)=} updateMetadata function to
  533. * update the metadata for each request
  534. */
  535. function Client(address, credentials, options, updateMetadata) {
  536. if (!updateMetadata) {
  537. updateMetadata = function(uri, metadata, callback) {
  538. callback(null, metadata);
  539. };
  540. }
  541. if (!options) {
  542. options = {};
  543. }
  544. options['grpc.primary_user_agent'] = 'grpc-node/' + version;
  545. this.channel = new grpc.Channel(address, credentials, options);
  546. // Remove the optional DNS scheme, trailing port, and trailing backslash
  547. address = address.replace(/^(dns:\/{3})?([^:\/]+)(:\d+)?\/?$/, '$2');
  548. this.server_address = address;
  549. this.auth_uri = 'https://' + this.server_address + '/' + serviceName;
  550. this.updateMetadata = updateMetadata;
  551. }
  552. /**
  553. * Wait for the client to be ready. The callback will be called when the
  554. * client has successfully connected to the server, and it will be called
  555. * with an error if the attempt to connect to the server has unrecoverablly
  556. * failed or if the deadline expires. This function will make the channel
  557. * start connecting if it has not already done so.
  558. * @param {(Date|Number)} deadline When to stop waiting for a connection. Pass
  559. * Infinity to wait forever.
  560. * @param {function(Error)} callback The callback to call when done attempting
  561. * to connect.
  562. */
  563. Client.prototype.$waitForReady = function(deadline, callback) {
  564. var self = this;
  565. var checkState = function(err) {
  566. if (err) {
  567. callback(new Error('Failed to connect before the deadline'));
  568. }
  569. var new_state = self.channel.getConnectivityState(true);
  570. if (new_state === grpc.connectivityState.READY) {
  571. callback();
  572. } else if (new_state === grpc.connectivityState.FATAL_FAILURE) {
  573. callback(new Error('Failed to connect to server'));
  574. } else {
  575. self.channel.watchConnectivityState(new_state, deadline, checkState);
  576. }
  577. };
  578. checkState();
  579. };
  580. _.each(methods, function(attrs, name) {
  581. var method_type;
  582. if (attrs.requestStream) {
  583. if (attrs.responseStream) {
  584. method_type = 'bidi';
  585. } else {
  586. method_type = 'client_stream';
  587. }
  588. } else {
  589. if (attrs.responseStream) {
  590. method_type = 'server_stream';
  591. } else {
  592. method_type = 'unary';
  593. }
  594. }
  595. var serialize = attrs.requestSerialize;
  596. var deserialize = attrs.responseDeserialize;
  597. Client.prototype[name] = requester_makers[method_type](
  598. attrs.path, serialize, deserialize);
  599. Client.prototype[name].serialize = serialize;
  600. Client.prototype[name].deserialize = deserialize;
  601. });
  602. return Client;
  603. };
  604. /**
  605. * Creates a constructor for clients for the given service
  606. * @param {ProtoBuf.Reflect.Service} service The service to generate a client
  607. * for
  608. * @return {function(string, Object)} New client constructor
  609. */
  610. exports.makeProtobufClientConstructor = function(service) {
  611. var method_attrs = common.getProtobufServiceAttrs(service, service.name);
  612. var Client = exports.makeClientConstructor(
  613. method_attrs, common.fullyQualifiedName(service));
  614. Client.service = service;
  615. return Client;
  616. };
  617. /**
  618. * Map of status code names to status codes
  619. */
  620. exports.status = grpc.status;
  621. /**
  622. * See docs for client.callError
  623. */
  624. exports.callError = grpc.callError;
  625. </code></pre>
  626. </article>
  627. </section>
  628. </div>
  629. <nav>
  630. <h2><a href="index.html">Home</a></h2><h3>Modules</h3><ul><li><a href="module-src_client.html">src/client</a></li><li><a href="module-src_common.html">src/common</a></li><li><a href="module-src_server.html">src/server</a></li></ul><h3>Classes</h3><ul><li><a href="module-src_client.makeClientConstructor-Client.html">Client</a></li><li><a href="module-src_client-ClientDuplexStream.html">ClientDuplexStream</a></li><li><a href="module-src_client-ClientReadableStream.html">ClientReadableStream</a></li><li><a href="module-src_client-ClientWritableStream.html">ClientWritableStream</a></li><li><a href="module-src_server-Server.html">Server</a></li><li><a href="module-src_server-ServerDuplexStream.html">ServerDuplexStream</a></li><li><a href="module-src_server-ServerReadableStream.html">ServerReadableStream</a></li><li><a href="module-src_server-ServerWritableStream.html">ServerWritableStream</a></li></ul><h3>Global</h3><ul><li><a href="global.html#callError">callError</a></li><li><a href="global.html#Credentials">Credentials</a></li><li><a href="global.html#getGoogleAuthDelegate">getGoogleAuthDelegate</a></li><li><a href="global.html#load">load</a></li><li><a href="global.html#loadObject">loadObject</a></li><li><a href="global.html#makeGenericClientConstructor">makeGenericClientConstructor</a></li><li><a href="global.html#propagate">propagate</a></li><li><a href="global.html#Server">Server</a></li><li><a href="global.html#ServerCredentials">ServerCredentials</a></li><li><a href="global.html#status">status</a></li></ul>
  631. </nav>
  632. <br class="clear">
  633. <footer>
  634. Documentation generated by <a href="https://github.com/jsdoc3/jsdoc">JSDoc 3.3.2</a> on Tue Aug 18 2015 18:25:05 GMT-0700 (PDT)
  635. </footer>
  636. <script> prettyPrint(); </script>
  637. <script src="scripts/linenumber.js"> </script>
  638. </body>
  639. </html>