@xdcobra/react-native-grpc 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (72) hide show
  1. package/LICENSE.md +22 -0
  2. package/README.md +114 -0
  3. package/android/build.gradle +127 -0
  4. package/android/gradle.properties +4 -0
  5. package/android/src/main/AndroidManifest.xml +3 -0
  6. package/android/src/main/java/com/reactnativegrpc/GrpcMarshaller.java +27 -0
  7. package/android/src/main/java/com/reactnativegrpc/GrpcModule.java +585 -0
  8. package/android/src/main/java/com/reactnativegrpc/GrpcPackage.java +27 -0
  9. package/android/src/main/java/com/reactnativegrpc/SpkiPinTrustManager.java +101 -0
  10. package/android/src/main/java/com/reactnativegrpc/TrustManagers.java +51 -0
  11. package/ios/Grpc.h +22 -0
  12. package/ios/Grpc.m +439 -0
  13. package/ios/SpkiPinning.h +22 -0
  14. package/ios/SpkiPinning.m +114 -0
  15. package/lib/commonjs/bidi-streaming.js +37 -0
  16. package/lib/commonjs/bidi-streaming.js.map +1 -0
  17. package/lib/commonjs/client-streaming.js +59 -0
  18. package/lib/commonjs/client-streaming.js.map +1 -0
  19. package/lib/commonjs/client.js +274 -0
  20. package/lib/commonjs/client.js.map +1 -0
  21. package/lib/commonjs/errors.js +16 -0
  22. package/lib/commonjs/errors.js.map +1 -0
  23. package/lib/commonjs/index.js +84 -0
  24. package/lib/commonjs/index.js.map +1 -0
  25. package/lib/commonjs/server-streaming.js +61 -0
  26. package/lib/commonjs/server-streaming.js.map +1 -0
  27. package/lib/commonjs/types.js +2 -0
  28. package/lib/commonjs/types.js.map +1 -0
  29. package/lib/commonjs/unary.js +38 -0
  30. package/lib/commonjs/unary.js.map +1 -0
  31. package/lib/module/bidi-streaming.js +30 -0
  32. package/lib/module/bidi-streaming.js.map +1 -0
  33. package/lib/module/client-streaming.js +51 -0
  34. package/lib/module/client-streaming.js.map +1 -0
  35. package/lib/module/client.js +268 -0
  36. package/lib/module/client.js.map +1 -0
  37. package/lib/module/errors.js +9 -0
  38. package/lib/module/errors.js.map +1 -0
  39. package/lib/module/index.js +10 -0
  40. package/lib/module/index.js.map +1 -0
  41. package/lib/module/server-streaming.js +53 -0
  42. package/lib/module/server-streaming.js.map +1 -0
  43. package/lib/module/types.js +2 -0
  44. package/lib/module/types.js.map +1 -0
  45. package/lib/module/unary.js +31 -0
  46. package/lib/module/unary.js.map +1 -0
  47. package/lib/typescript/bidi-streaming.d.ts +16 -0
  48. package/lib/typescript/bidi-streaming.d.ts.map +1 -0
  49. package/lib/typescript/client-streaming.d.ts +29 -0
  50. package/lib/typescript/client-streaming.d.ts.map +1 -0
  51. package/lib/typescript/client.d.ts +80 -0
  52. package/lib/typescript/client.d.ts.map +1 -0
  53. package/lib/typescript/errors.d.ts +8 -0
  54. package/lib/typescript/errors.d.ts.map +1 -0
  55. package/lib/typescript/index.d.ts +10 -0
  56. package/lib/typescript/index.d.ts.map +1 -0
  57. package/lib/typescript/server-streaming.d.ts +23 -0
  58. package/lib/typescript/server-streaming.d.ts.map +1 -0
  59. package/lib/typescript/types.d.ts +87 -0
  60. package/lib/typescript/types.d.ts.map +1 -0
  61. package/lib/typescript/unary.d.ts +16 -0
  62. package/lib/typescript/unary.d.ts.map +1 -0
  63. package/package.json +169 -0
  64. package/react-native-grpc.podspec +30 -0
  65. package/src/bidi-streaming.ts +78 -0
  66. package/src/client-streaming.ts +106 -0
  67. package/src/client.ts +521 -0
  68. package/src/errors.ts +11 -0
  69. package/src/index.ts +11 -0
  70. package/src/server-streaming.ts +105 -0
  71. package/src/types.ts +107 -0
  72. package/src/unary.ts +71 -0
package/src/client.ts ADDED
@@ -0,0 +1,521 @@
1
+ import { AbortController, AbortSignal } from 'abort-controller';
2
+ import { fromByteArray, toByteArray } from 'base64-js';
3
+ import { NativeEventEmitter, NativeModules, Platform } from 'react-native';
4
+ import { GrpcBidiStreamingCall } from './bidi-streaming';
5
+ import { GrpcClientStreamingCall, ServerInputStream } from './client-streaming';
6
+ import { GrpcError } from './errors';
7
+ import {
8
+ GrpcServerStreamingCall,
9
+ ServerOutputStream,
10
+ } from './server-streaming';
11
+ import { GrpcCallOptions, GrpcMetadata, GrpcTlsOptions } from './types';
12
+ import { GrpcUnaryCall } from './unary';
13
+
14
+ type GrpcRequestObject = {
15
+ data: string;
16
+ /** Optional per-call override; native falls back to global default when omitted. */
17
+ deadlineSeconds?: number;
18
+ };
19
+
20
+ type GrpcType = {
21
+ getHost: () => Promise<string>;
22
+ getIsInsecure: () => Promise<boolean>;
23
+ setHost(host: string): void;
24
+ setInsecure(insecure: boolean): void;
25
+ setTlsOptions(options: {
26
+ rootCertsPem: string | null;
27
+ certificateChainPem: string | null;
28
+ privateKeyPem: string | null;
29
+ hostNameOverride: string | null;
30
+ spkiSha256Pins: string[] | null;
31
+ }): void;
32
+ setCompression(enable: boolean, compressorName: string): void;
33
+ setResponseSizeLimit(limitInBytes: number): void;
34
+ setCallDeadlineSeconds(seconds: number): void;
35
+ initGrpcChannel(): void;
36
+ unaryCall(
37
+ id: number,
38
+ path: string,
39
+ obj: GrpcRequestObject,
40
+ requestHeaders?: GrpcMetadata
41
+ ): Promise<void>;
42
+ serverStreamingCall(
43
+ id: number,
44
+ path: string,
45
+ obj: GrpcRequestObject,
46
+ requestHeaders?: GrpcMetadata
47
+ ): Promise<void>;
48
+ cancelGrpcCall: (id: number) => Promise<boolean>;
49
+ clientStreamingCall(
50
+ id: number,
51
+ path: string,
52
+ obj: GrpcRequestObject,
53
+ requestHeaders?: GrpcMetadata
54
+ ): Promise<void>;
55
+ bidiStreamingCall(
56
+ id: number,
57
+ path: string,
58
+ obj: GrpcRequestObject,
59
+ requestHeaders?: GrpcMetadata
60
+ ): Promise<void>;
61
+ finishClientStreaming(id: number): Promise<void>;
62
+ resetConnection(message: string): void;
63
+ setKeepAlive(
64
+ enable: boolean,
65
+ keepAliveTime: number,
66
+ keepAliveTimeOut: number
67
+ ): void;
68
+ onConnectionStateChange(): void;
69
+ setUiLogEnabled(enable: boolean): void;
70
+ enterIdle(): void;
71
+ };
72
+
73
+ type GrpcEventType = 'response' | 'error' | 'headers' | 'trailers';
74
+
75
+ /* prettier-ignore */
76
+ type GrpcEventPayload =
77
+ {
78
+ type: 'response';
79
+ payload: string;
80
+ } | {
81
+ type: 'error';
82
+ error: string;
83
+ code?: number;
84
+ trailers?: GrpcMetadata;
85
+ } | {
86
+ type: 'headers';
87
+ payload: GrpcMetadata;
88
+ } | {
89
+ type: 'trailers';
90
+ payload: GrpcMetadata;
91
+ } | {
92
+ type: 'status';
93
+ payload: number;
94
+ };
95
+
96
+ type GrpcEvent = {
97
+ id: number;
98
+ type: GrpcEventType;
99
+ } & GrpcEventPayload;
100
+
101
+ function nativeGrpc(): GrpcType {
102
+ const grpc = (NativeModules as { Grpc: GrpcType }).Grpc;
103
+ if (!grpc) {
104
+ throw new Error('NativeModules.Grpc is not linked');
105
+ }
106
+ return grpc;
107
+ }
108
+
109
+ const Emitter = new NativeEventEmitter(NativeModules.Grpc);
110
+
111
+ type Deferred<T> = {
112
+ promise: Promise<T>;
113
+ resolve: (value: T) => void;
114
+ reject: (reason: any) => void;
115
+ };
116
+
117
+ type DeferredCalls = {
118
+ headers?: Deferred<GrpcMetadata>;
119
+ response?: Deferred<Uint8Array>;
120
+ trailers?: Deferred<GrpcMetadata>;
121
+ data?: ServerOutputStream;
122
+ };
123
+
124
+ type DeferredCallMap = {
125
+ [id: number]: DeferredCalls;
126
+ };
127
+
128
+ function createDeferred<T>(signal: AbortSignal) {
129
+ let completed = false;
130
+
131
+ const deferred: Deferred<T> = {} as any;
132
+
133
+ deferred.promise = new Promise<T>((resolve, reject) => {
134
+ deferred.resolve = (value) => {
135
+ completed = true;
136
+
137
+ resolve(value);
138
+ };
139
+ deferred.reject = (reason) => {
140
+ completed = true;
141
+
142
+ reject(reason);
143
+ };
144
+ });
145
+
146
+ signal.addEventListener('abort', () => {
147
+ if (!completed) {
148
+ deferred.reject('aborted');
149
+ }
150
+ });
151
+
152
+ return deferred;
153
+ }
154
+
155
+ let idCtr = 1;
156
+
157
+ const deferredMap: DeferredCallMap = {};
158
+
159
+ function handleGrpcEvent(event: GrpcEvent) {
160
+ const deferred = deferredMap[event.id];
161
+
162
+ if (deferred) {
163
+ switch (event.type) {
164
+ case 'headers':
165
+ deferred.headers?.resolve(event.payload);
166
+ break;
167
+ case 'response':
168
+ const data = toByteArray(event.payload);
169
+
170
+ deferred.data?.notifyData(data);
171
+ deferred.response?.resolve(data);
172
+ break;
173
+ case 'trailers':
174
+ deferred.trailers?.resolve(event.payload);
175
+ deferred.data?.notifyComplete();
176
+
177
+ delete deferredMap[event.id];
178
+ break;
179
+ case 'error':
180
+ const error = new GrpcError(event.error, event.code, event.trailers);
181
+
182
+ deferred.headers?.reject(error);
183
+ deferred.trailers?.reject(error);
184
+ deferred.response?.reject(error);
185
+ deferred.data?.noitfyError(error);
186
+
187
+ delete deferredMap[event.id];
188
+ break;
189
+ }
190
+ }
191
+ }
192
+
193
+ function getId(): number {
194
+ return idCtr++;
195
+ }
196
+
197
+ function buildRequestObject(
198
+ data: Uint8Array,
199
+ options?: GrpcCallOptions
200
+ ): GrpcRequestObject {
201
+ const obj: GrpcRequestObject = {
202
+ data: fromByteArray(data),
203
+ };
204
+ if (options?.deadlineSeconds !== undefined) {
205
+ obj.deadlineSeconds = options.deadlineSeconds;
206
+ }
207
+ return obj;
208
+ }
209
+
210
+ export class GrpcClient {
211
+ constructor() {
212
+ Emitter.addListener('grpc-call', handleGrpcEvent);
213
+ }
214
+ destroy() {
215
+ Emitter.removeAllListeners('grpc-call');
216
+ }
217
+ getHost(): Promise<string> {
218
+ return nativeGrpc().getHost();
219
+ }
220
+ setHost(host: string): void {
221
+ nativeGrpc().setHost(host);
222
+ }
223
+ getInsecure(): Promise<boolean> {
224
+ return nativeGrpc().getIsInsecure();
225
+ }
226
+ setInsecure(insecure: boolean): void {
227
+ nativeGrpc().setInsecure(insecure);
228
+ }
229
+ /**
230
+ * Replace channel TLS options (custom CA, mTLS, hostname override).
231
+ * Each call replaces the previous config; omitted fields are cleared.
232
+ * Apply before `initGrpcChannel()`. Ignored while insecure/plaintext.
233
+ */
234
+ setTlsOptions(options: GrpcTlsOptions = {}): void {
235
+ const rootCertsPem = options.rootCertsPem ?? null;
236
+ const certificateChainPem = options.certificateChainPem ?? null;
237
+ const privateKeyPem = options.privateKeyPem ?? null;
238
+ const hostNameOverride = options.hostNameOverride ?? null;
239
+ const spkiSha256Pins =
240
+ options.spkiSha256Pins && options.spkiSha256Pins.length > 0
241
+ ? options.spkiSha256Pins
242
+ : null;
243
+
244
+ const hasCert = !!certificateChainPem;
245
+ const hasKey = !!privateKeyPem;
246
+ if (hasCert !== hasKey) {
247
+ throw new Error(
248
+ 'mTLS requires both certificateChainPem and privateKeyPem'
249
+ );
250
+ }
251
+
252
+ nativeGrpc().setTlsOptions({
253
+ rootCertsPem,
254
+ certificateChainPem,
255
+ privateKeyPem,
256
+ hostNameOverride,
257
+ spkiSha256Pins,
258
+ });
259
+ }
260
+ setCompression(enable: boolean, compressorName: string): void {
261
+ nativeGrpc().setCompression(enable, compressorName);
262
+ }
263
+ setResponseSizeLimit(limitInBytes: number): void {
264
+ nativeGrpc().setResponseSizeLimit(limitInBytes);
265
+ }
266
+ /** Global per-call deadline in seconds (Android/iOS). Default 120. Overridable per RPC via options. */
267
+ setCallDeadlineSeconds(seconds: number): void {
268
+ nativeGrpc().setCallDeadlineSeconds(seconds);
269
+ }
270
+
271
+ initGrpcChannel() {
272
+ nativeGrpc().initGrpcChannel();
273
+ }
274
+
275
+ setKeepAlive(
276
+ enable: boolean,
277
+ keepAliveTime: number,
278
+ keepAliveTimeOut: number
279
+ ): void {
280
+ nativeGrpc().setKeepAlive(enable, keepAliveTime, keepAliveTimeOut);
281
+ }
282
+
283
+ resetConnection(message: string): void {
284
+ if (!this.isAndroid()) return;
285
+ nativeGrpc().resetConnection(message);
286
+ }
287
+ setUiLogEnabled(enable: boolean): void {
288
+ if (!this.isAndroid()) return;
289
+ nativeGrpc().setUiLogEnabled(enable);
290
+ }
291
+
292
+ onConnectionStateChange(): void {
293
+ if (!this.isAndroid()) return;
294
+ nativeGrpc().onConnectionStateChange();
295
+ }
296
+
297
+ enterIdle(): void {
298
+ if (!this.isAndroid()) return;
299
+ nativeGrpc().enterIdle();
300
+ }
301
+
302
+ unaryCall(
303
+ method: string,
304
+ data: Uint8Array,
305
+ requestHeaders?: GrpcMetadata,
306
+ options?: GrpcCallOptions
307
+ ): GrpcUnaryCall {
308
+ const obj = buildRequestObject(data, options);
309
+
310
+ const id = getId();
311
+ const abort = new AbortController();
312
+
313
+ abort.signal.addEventListener('abort', () => {
314
+ nativeGrpc().cancelGrpcCall(id);
315
+ });
316
+
317
+ const response = createDeferred<Uint8Array>(abort.signal);
318
+ const headers = createDeferred<GrpcMetadata>(abort.signal);
319
+ const trailers = createDeferred<GrpcMetadata>(abort.signal);
320
+
321
+ deferredMap[id] = {
322
+ response,
323
+ headers,
324
+ trailers,
325
+ };
326
+
327
+ nativeGrpc().unaryCall(id, method, obj, requestHeaders || {});
328
+
329
+ const call = new GrpcUnaryCall(
330
+ method,
331
+ data,
332
+ requestHeaders || {},
333
+ headers.promise,
334
+ response.promise,
335
+ trailers.promise,
336
+ abort
337
+ );
338
+
339
+ call.then(
340
+ (result) => result,
341
+ () => abort.abort()
342
+ );
343
+
344
+ return call;
345
+ }
346
+ serverStreamCall(
347
+ method: string,
348
+ data: Uint8Array,
349
+ requestHeaders?: GrpcMetadata,
350
+ options?: GrpcCallOptions
351
+ ): GrpcServerStreamingCall {
352
+ const obj = buildRequestObject(data, options);
353
+
354
+ const id = getId();
355
+ const abort = new AbortController();
356
+
357
+ abort.signal.addEventListener('abort', () => {
358
+ nativeGrpc().cancelGrpcCall(id);
359
+ });
360
+
361
+ const headers = createDeferred<GrpcMetadata>(abort.signal);
362
+ const trailers = createDeferred<GrpcMetadata>(abort.signal);
363
+
364
+ const stream = new ServerOutputStream();
365
+
366
+ deferredMap[id] = {
367
+ headers,
368
+ trailers,
369
+ data: stream,
370
+ };
371
+
372
+ nativeGrpc().serverStreamingCall(id, method, obj, requestHeaders || {});
373
+
374
+ const call = new GrpcServerStreamingCall(
375
+ method,
376
+ data,
377
+ requestHeaders || {},
378
+ headers.promise,
379
+ stream,
380
+ trailers.promise,
381
+ abort
382
+ );
383
+
384
+ call.then(
385
+ (result) => result,
386
+ () => abort.abort()
387
+ );
388
+
389
+ return call;
390
+ }
391
+
392
+ /**
393
+ * Client-streaming RPC: many request messages, one response.
394
+ * Native call starts on the first `requests.send(...)`.
395
+ */
396
+ clientStreamCall(
397
+ method: string,
398
+ requestHeaders?: GrpcMetadata,
399
+ options?: GrpcCallOptions
400
+ ): GrpcClientStreamingCall {
401
+ const id = getId();
402
+ const abort = new AbortController();
403
+ const headersMeta = requestHeaders || {};
404
+
405
+ abort.signal.addEventListener('abort', () => {
406
+ nativeGrpc().cancelGrpcCall(id);
407
+ });
408
+
409
+ const response = createDeferred<Uint8Array>(abort.signal);
410
+ const headers = createDeferred<GrpcMetadata>(abort.signal);
411
+ const trailers = createDeferred<GrpcMetadata>(abort.signal);
412
+
413
+ deferredMap[id] = {
414
+ response,
415
+ headers,
416
+ trailers,
417
+ };
418
+
419
+ const requests = new ServerInputStream(
420
+ (data, isFirst) => {
421
+ const obj = buildRequestObject(data, isFirst ? options : undefined);
422
+ return nativeGrpc().clientStreamingCall(
423
+ id,
424
+ method,
425
+ obj,
426
+ isFirst ? headersMeta : {}
427
+ );
428
+ },
429
+ () => nativeGrpc().finishClientStreaming(id)
430
+ );
431
+
432
+ const call = new GrpcClientStreamingCall(
433
+ method,
434
+ headersMeta,
435
+ requests,
436
+ headers.promise,
437
+ response.promise,
438
+ trailers.promise,
439
+ abort
440
+ );
441
+
442
+ call.then(
443
+ (result) => result,
444
+ () => abort.abort()
445
+ );
446
+
447
+ return call;
448
+ }
449
+
450
+ /**
451
+ * Bidirectional streaming RPC: interleaved request and response messages.
452
+ * Native call starts on the first `requests.send(...)`.
453
+ * Registers only the `data` stream (plus headers/trailers) — no unary `response`.
454
+ */
455
+ bidiStreamCall(
456
+ method: string,
457
+ requestHeaders?: GrpcMetadata,
458
+ options?: GrpcCallOptions
459
+ ): GrpcBidiStreamingCall {
460
+ const id = getId();
461
+ const abort = new AbortController();
462
+ const headersMeta = requestHeaders || {};
463
+
464
+ abort.signal.addEventListener('abort', () => {
465
+ nativeGrpc().cancelGrpcCall(id);
466
+ });
467
+
468
+ const headers = createDeferred<GrpcMetadata>(abort.signal);
469
+ const trailers = createDeferred<GrpcMetadata>(abort.signal);
470
+ const stream = new ServerOutputStream();
471
+
472
+ deferredMap[id] = {
473
+ headers,
474
+ trailers,
475
+ data: stream,
476
+ };
477
+
478
+ const requests = new ServerInputStream(
479
+ (data, isFirst) => {
480
+ const obj = buildRequestObject(data, isFirst ? options : undefined);
481
+ return nativeGrpc().bidiStreamingCall(
482
+ id,
483
+ method,
484
+ obj,
485
+ isFirst ? headersMeta : {}
486
+ );
487
+ },
488
+ () => nativeGrpc().finishClientStreaming(id)
489
+ );
490
+
491
+ const call = new GrpcBidiStreamingCall(
492
+ method,
493
+ headersMeta,
494
+ requests,
495
+ headers.promise,
496
+ stream,
497
+ trailers.promise,
498
+ abort
499
+ );
500
+
501
+ call.then(
502
+ (result) => result,
503
+ () => abort.abort()
504
+ );
505
+
506
+ return call;
507
+ }
508
+
509
+ private isAndroid(): Boolean {
510
+ return Platform.OS === 'android';
511
+ }
512
+ }
513
+
514
+ /** Lazy binding to NativeModules.Grpc (works with Jest mocks / late link). */
515
+ export const Grpc: GrpcType = new Proxy({} as GrpcType, {
516
+ get(_target, prop) {
517
+ const native = nativeGrpc();
518
+ const value = (native as any)[prop as string];
519
+ return typeof value === 'function' ? value.bind(native) : value;
520
+ },
521
+ });
package/src/errors.ts ADDED
@@ -0,0 +1,11 @@
1
+ import { GrpcMetadata } from './types';
2
+
3
+ export class GrpcError extends Error {
4
+ constructor(
5
+ public error: string,
6
+ public code?: number,
7
+ public trailers?: GrpcMetadata
8
+ ) {
9
+ super(error);
10
+ }
11
+ }
package/src/index.ts ADDED
@@ -0,0 +1,11 @@
1
+ import { GrpcClient as GrpcClientImpl } from './client';
2
+
3
+ const GrpcClient = new GrpcClientImpl();
4
+
5
+ export * from './types';
6
+ export * from './unary';
7
+ export * from './server-streaming';
8
+ export * from './client-streaming';
9
+ export * from './bidi-streaming';
10
+ export * from './errors';
11
+ export { GrpcClient };
@@ -0,0 +1,105 @@
1
+ /* eslint-disable eslint-comments/no-unlimited-disable */
2
+ import { AbortController } from 'abort-controller';
3
+ import { EventEmitter } from 'eventemitter3';
4
+ import {
5
+ CompletedGrpcStreamingCall,
6
+ GrpcMetadata,
7
+ GrpcServerOutputStream,
8
+ ServerOutputEvent,
9
+ ServerOutputEventCallback,
10
+ } from './types';
11
+
12
+ /* eslint-disable */
13
+
14
+ export class GrpcServerStreamingCall
15
+ implements PromiseLike<CompletedGrpcStreamingCall> {
16
+ readonly method: string;
17
+ readonly requestHeaders: GrpcMetadata;
18
+ readonly request: Uint8Array;
19
+ readonly headers: Promise<GrpcMetadata>;
20
+ readonly responses: GrpcServerOutputStream;
21
+ readonly trailers: Promise<GrpcMetadata>;
22
+
23
+ #abort: AbortController;
24
+
25
+ constructor(
26
+ method: string,
27
+ data: Uint8Array,
28
+ requestHeaders: GrpcMetadata,
29
+ headers: Promise<GrpcMetadata>,
30
+ responses: ServerOutputStream,
31
+ trailers: Promise<GrpcMetadata>,
32
+ abort: AbortController,
33
+ ) {
34
+ this.method = method;
35
+ this.request = data;
36
+ this.requestHeaders = requestHeaders;
37
+ this.headers = headers;
38
+ this.responses = responses;
39
+ this.trailers = trailers;
40
+ this.#abort = abort;
41
+ }
42
+ then<TResult1 = CompletedGrpcStreamingCall, TResult2 = unknown>(
43
+ onfulfilled?:
44
+ | ((
45
+ value: CompletedGrpcStreamingCall
46
+ ) => TResult1 | PromiseLike<TResult1>)
47
+ | null,
48
+ onrejected?: ((reason: any) => TResult2 | PromiseLike<TResult2>) | null
49
+ ): PromiseLike<TResult1 | TResult2> {
50
+ return this.completedPromise().then(
51
+ (value) =>
52
+ onfulfilled
53
+ ? Promise.resolve(onfulfilled(value))
54
+ : ((value as unknown) as TResult1),
55
+ (reason) =>
56
+ onrejected
57
+ ? Promise.resolve(onrejected(reason))
58
+ : Promise.reject(reason)
59
+ );
60
+ }
61
+
62
+ cancel() {
63
+ this.#abort.abort();
64
+ }
65
+
66
+ private async completedPromise(): Promise<CompletedGrpcStreamingCall> {
67
+ const [headers, trailers] = await Promise.all([
68
+ this.headers,
69
+ this.trailers,
70
+ ]);
71
+
72
+ return {
73
+ method: this.method,
74
+ requestHeaders: this.requestHeaders,
75
+ request: this.request,
76
+ headers,
77
+ trailers,
78
+ status: 0,
79
+ };
80
+ }
81
+ }
82
+
83
+ export class ServerOutputStream implements GrpcServerOutputStream {
84
+ #emitter = new EventEmitter<ServerOutputEvent>();
85
+
86
+ on<T extends ServerOutputEvent>(event: T, callback: ServerOutputEventCallback<T>) {
87
+ this.#emitter.addListener(event, callback);
88
+
89
+ return () => {
90
+ this.#emitter.removeListener(event, callback);
91
+ }
92
+ }
93
+
94
+ notifyData(data: Uint8Array): void {
95
+ this.#emitter.emit('data', data);
96
+ }
97
+
98
+ notifyComplete(): void {
99
+ this.#emitter.emit('complete')
100
+ }
101
+
102
+ noitfyError(reason: any): void {
103
+ this.#emitter.emit('error', reason);
104
+ }
105
+ }