call.ts 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. /*
  2. * Copyright 2019 gRPC authors.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. *
  16. */
  17. import { EventEmitter } from 'events';
  18. import { Duplex, Readable, Writable } from 'stream';
  19. import { StatusObject, MessageContext } from './call-interface';
  20. import { Status } from './constants';
  21. import { EmitterAugmentation1 } from './events';
  22. import { Metadata } from './metadata';
  23. import { ObjectReadable, ObjectWritable, WriteCallback } from './object-stream';
  24. import { InterceptingCallInterface } from './client-interceptors';
  25. /**
  26. * A type extending the built-in Error object with additional fields.
  27. */
  28. export type ServiceError = StatusObject & Error;
  29. /**
  30. * A base type for all user-facing values returned by client-side method calls.
  31. */
  32. export type SurfaceCall = {
  33. call?: InterceptingCallInterface;
  34. cancel(): void;
  35. getPeer(): string;
  36. } & EmitterAugmentation1<'metadata', Metadata> &
  37. EmitterAugmentation1<'status', StatusObject> &
  38. EventEmitter;
  39. /**
  40. * A type representing the return value of a unary method call.
  41. */
  42. export type ClientUnaryCall = SurfaceCall;
  43. /**
  44. * A type representing the return value of a server stream method call.
  45. */
  46. export type ClientReadableStream<ResponseType> = {
  47. deserialize: (chunk: Buffer) => ResponseType;
  48. } & SurfaceCall &
  49. ObjectReadable<ResponseType>;
  50. /**
  51. * A type representing the return value of a client stream method call.
  52. */
  53. export type ClientWritableStream<RequestType> = {
  54. serialize: (value: RequestType) => Buffer;
  55. } & SurfaceCall &
  56. ObjectWritable<RequestType>;
  57. /**
  58. * A type representing the return value of a bidirectional stream method call.
  59. */
  60. export type ClientDuplexStream<RequestType, ResponseType> =
  61. ClientWritableStream<RequestType> & ClientReadableStream<ResponseType>;
  62. /**
  63. * Construct a ServiceError from a StatusObject. This function exists primarily
  64. * as an attempt to make the error stack trace clearly communicate that the
  65. * error is not necessarily a problem in gRPC itself.
  66. * @param status
  67. */
  68. export function callErrorFromStatus(
  69. status: StatusObject,
  70. callerStack: string
  71. ): ServiceError {
  72. const message = `${status.code} ${Status[status.code]}: ${status.details}`;
  73. const error = new Error(message);
  74. const stack = `${error.stack}\nfor call at\n${callerStack}`;
  75. return Object.assign(new Error(message), status, { stack });
  76. }
  77. export class ClientUnaryCallImpl
  78. extends EventEmitter
  79. implements ClientUnaryCall
  80. {
  81. public call?: InterceptingCallInterface;
  82. constructor() {
  83. super();
  84. }
  85. cancel(): void {
  86. this.call?.cancelWithStatus(Status.CANCELLED, 'Cancelled on client');
  87. }
  88. getPeer(): string {
  89. return this.call?.getPeer() ?? 'unknown';
  90. }
  91. }
  92. export class ClientReadableStreamImpl<ResponseType>
  93. extends Readable
  94. implements ClientReadableStream<ResponseType>
  95. {
  96. public call?: InterceptingCallInterface;
  97. constructor(readonly deserialize: (chunk: Buffer) => ResponseType) {
  98. super({ objectMode: true });
  99. }
  100. cancel(): void {
  101. this.call?.cancelWithStatus(Status.CANCELLED, 'Cancelled on client');
  102. }
  103. getPeer(): string {
  104. return this.call?.getPeer() ?? 'unknown';
  105. }
  106. _read(_size: number): void {
  107. this.call?.startRead();
  108. }
  109. }
  110. export class ClientWritableStreamImpl<RequestType>
  111. extends Writable
  112. implements ClientWritableStream<RequestType>
  113. {
  114. public call?: InterceptingCallInterface;
  115. constructor(readonly serialize: (value: RequestType) => Buffer) {
  116. super({ objectMode: true });
  117. }
  118. cancel(): void {
  119. this.call?.cancelWithStatus(Status.CANCELLED, 'Cancelled on client');
  120. }
  121. getPeer(): string {
  122. return this.call?.getPeer() ?? 'unknown';
  123. }
  124. _write(chunk: RequestType, encoding: string, cb: WriteCallback) {
  125. const context: MessageContext = {
  126. callback: cb,
  127. };
  128. const flags = Number(encoding);
  129. if (!Number.isNaN(flags)) {
  130. context.flags = flags;
  131. }
  132. this.call?.sendMessageWithContext(context, chunk);
  133. }
  134. _final(cb: Function) {
  135. this.call?.halfClose();
  136. cb();
  137. }
  138. }
  139. export class ClientDuplexStreamImpl<RequestType, ResponseType>
  140. extends Duplex
  141. implements ClientDuplexStream<RequestType, ResponseType>
  142. {
  143. public call?: InterceptingCallInterface;
  144. constructor(
  145. readonly serialize: (value: RequestType) => Buffer,
  146. readonly deserialize: (chunk: Buffer) => ResponseType
  147. ) {
  148. super({ objectMode: true });
  149. }
  150. cancel(): void {
  151. this.call?.cancelWithStatus(Status.CANCELLED, 'Cancelled on client');
  152. }
  153. getPeer(): string {
  154. return this.call?.getPeer() ?? 'unknown';
  155. }
  156. _read(_size: number): void {
  157. this.call?.startRead();
  158. }
  159. _write(chunk: RequestType, encoding: string, cb: WriteCallback) {
  160. const context: MessageContext = {
  161. callback: cb,
  162. };
  163. const flags = Number(encoding);
  164. if (!Number.isNaN(flags)) {
  165. context.flags = flags;
  166. }
  167. this.call?.sendMessageWithContext(context, chunk);
  168. }
  169. _final(cb: Function) {
  170. this.call?.halfClose();
  171. cb();
  172. }
  173. }