client.js 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. /*
  2. *
  3. * Copyright 2014, Google Inc.
  4. * All rights reserved.
  5. *
  6. * Redistribution and use in source and binary forms, with or without
  7. * modification, are permitted provided that the following conditions are
  8. * met:
  9. *
  10. * * Redistributions of source code must retain the above copyright
  11. * notice, this list of conditions and the following disclaimer.
  12. * * Redistributions in binary form must reproduce the above
  13. * copyright notice, this list of conditions and the following disclaimer
  14. * in the documentation and/or other materials provided with the
  15. * distribution.
  16. * * Neither the name of Google Inc. nor the names of its
  17. * contributors may be used to endorse or promote products derived from
  18. * this software without specific prior written permission.
  19. *
  20. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  21. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  22. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  23. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  24. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  25. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  26. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  27. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  28. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  29. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  30. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  31. *
  32. */
  33. var grpc = require('bindings')('grpc.node');
  34. var common = require('./common');
  35. var Duplex = require('stream').Duplex;
  36. var util = require('util');
  37. util.inherits(GrpcClientStream, Duplex);
  38. /**
  39. * Class for representing a gRPC client side stream as a Node stream. Extends
  40. * from stream.Duplex.
  41. * @constructor
  42. * @param {grpc.Call} call Call object to proxy
  43. * @param {object} options Stream options
  44. */
  45. function GrpcClientStream(call, options) {
  46. Duplex.call(this, options);
  47. var self = this;
  48. var finished = false;
  49. // Indicates that a read is currently pending
  50. var reading = false;
  51. // Indicates that a write is currently pending
  52. var writing = false;
  53. this._call = call;
  54. /**
  55. * Callback to be called when a READ event is received. Pushes the data onto
  56. * the read queue and starts reading again if applicable
  57. * @param {grpc.Event} event READ event object
  58. */
  59. function readCallback(event) {
  60. if (finished) {
  61. self.push(null);
  62. return;
  63. }
  64. var data = event.data;
  65. if (self.push(data) && data != null) {
  66. self._call.startRead(readCallback);
  67. } else {
  68. reading = false;
  69. }
  70. }
  71. call.invoke(function(event) {
  72. self.emit('metadata', event.data);
  73. }, function(event) {
  74. finished = true;
  75. self.emit('status', event.data);
  76. }, 0);
  77. this.on('finish', function() {
  78. call.writesDone(function() {});
  79. });
  80. /**
  81. * Start reading if there is not already a pending read. Reading will
  82. * continue until self.push returns false (indicating reads should slow
  83. * down) or the read data is null (indicating that there is no more data).
  84. */
  85. this.startReading = function() {
  86. if (finished) {
  87. self.push(null);
  88. } else {
  89. if (!reading) {
  90. reading = true;
  91. self._call.startRead(readCallback);
  92. }
  93. }
  94. };
  95. }
  96. /**
  97. * Start reading. This is an implementation of a method needed for implementing
  98. * stream.Readable.
  99. * @param {number} size Ignored
  100. */
  101. GrpcClientStream.prototype._read = function(size) {
  102. this.startReading();
  103. };
  104. /**
  105. * Attempt to write the given chunk. Calls the callback when done. This is an
  106. * implementation of a method needed for implementing stream.Writable.
  107. * @param {Buffer} chunk The chunk to write
  108. * @param {string} encoding Ignored
  109. * @param {function(Error=)} callback Ignored
  110. */
  111. GrpcClientStream.prototype._write = function(chunk, encoding, callback) {
  112. var self = this;
  113. self._call.startWrite(chunk, function(event) {
  114. callback();
  115. }, 0);
  116. };
  117. /**
  118. * Make a request on the channel to the given method with the given arguments
  119. * @param {grpc.Channel} channel The channel on which to make the request
  120. * @param {string} method The method to request
  121. * @param {array=} metadata Array of metadata key/value pairs to add to the call
  122. * @param {(number|Date)=} deadline The deadline for processing this request.
  123. * Defaults to infinite future.
  124. * @return {stream=} The stream of responses
  125. */
  126. function makeRequest(channel,
  127. method,
  128. metadata,
  129. deadline) {
  130. if (deadline === undefined) {
  131. deadline = Infinity;
  132. }
  133. var call = new grpc.Call(channel, method, deadline);
  134. if (metadata) {
  135. call.addMetadata(metadata);
  136. }
  137. return new GrpcClientStream(call);
  138. }
  139. /**
  140. * See documentation for makeRequest above
  141. */
  142. exports.makeRequest = makeRequest;
  143. /**
  144. * Represents a client side gRPC channel associated with a single host.
  145. */
  146. exports.Channel = grpc.Channel;
  147. /**
  148. * Status name to code number mapping
  149. */
  150. exports.status = grpc.status;
  151. /**
  152. * Call error name to code number mapping
  153. */
  154. exports.callError = grpc.callError;