@xdcobra/react-native-grpc 1.1.0 → 2.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.
- package/LICENSE.md +22 -22
- package/README.md +302 -284
- package/android/build.gradle +31 -127
- package/android/src/main/AndroidManifest.xml +3 -3
- package/android/src/main/java/com/reactnativegrpc/GrpcMarshaller.java +27 -27
- package/android/src/main/java/com/reactnativegrpc/GrpcModule.java +730 -730
- package/android/src/main/java/com/reactnativegrpc/GrpcPackage.java +27 -27
- package/android/src/main/java/com/reactnativegrpc/SpkiPinTrustManager.java +101 -101
- package/android/src/main/java/com/reactnativegrpc/TrustManagers.java +51 -51
- package/ios/Grpc.h +22 -22
- package/ios/Grpc.m +627 -625
- package/ios/SpkiPinning.h +22 -22
- package/ios/SpkiPinning.m +162 -114
- package/lib/commonjs/bidi-streaming.js.map +1 -1
- package/lib/commonjs/channel.js +9 -9
- package/lib/commonjs/channel.js.map +1 -1
- package/lib/commonjs/client-streaming.js +3 -3
- package/lib/commonjs/client-streaming.js.map +1 -1
- package/lib/commonjs/client.js +24 -24
- package/lib/commonjs/client.js.map +1 -1
- package/lib/commonjs/create-client.js +5 -5
- package/lib/commonjs/create-client.js.map +1 -1
- package/lib/commonjs/errors.js.map +1 -1
- package/lib/commonjs/index.js.map +1 -1
- package/lib/commonjs/interceptors.js +3 -3
- package/lib/commonjs/interceptors.js.map +1 -1
- package/lib/commonjs/native.js.map +1 -1
- package/lib/commonjs/retry.js.map +1 -1
- package/lib/commonjs/server-streaming.js.map +1 -1
- package/lib/commonjs/status.js.map +1 -1
- package/lib/commonjs/types.js +5 -5
- package/lib/commonjs/types.js.map +1 -1
- package/lib/commonjs/unary.js.map +1 -1
- package/lib/module/bidi-streaming.js.map +1 -1
- package/lib/module/channel.js +9 -9
- package/lib/module/channel.js.map +1 -1
- package/lib/module/client-streaming.js +3 -3
- package/lib/module/client-streaming.js.map +1 -1
- package/lib/module/client.js +24 -24
- package/lib/module/client.js.map +1 -1
- package/lib/module/create-client.js +5 -5
- package/lib/module/create-client.js.map +1 -1
- package/lib/module/errors.js.map +1 -1
- package/lib/module/index.js.map +1 -1
- package/lib/module/interceptors.js +3 -3
- package/lib/module/interceptors.js.map +1 -1
- package/lib/module/native.js.map +1 -1
- package/lib/module/retry.js.map +1 -1
- package/lib/module/server-streaming.js.map +1 -1
- package/lib/module/status.js.map +1 -1
- package/lib/module/types.js +5 -5
- package/lib/module/types.js.map +1 -1
- package/lib/module/unary.js.map +1 -1
- package/lib/typescript/src/bidi-streaming.d.ts.map +1 -0
- package/lib/typescript/src/channel.d.ts.map +1 -0
- package/lib/typescript/src/client-streaming.d.ts.map +1 -0
- package/lib/typescript/src/client.d.ts.map +1 -0
- package/lib/typescript/src/create-client.d.ts.map +1 -0
- package/lib/typescript/src/errors.d.ts.map +1 -0
- package/lib/typescript/src/index.d.ts.map +1 -0
- package/lib/typescript/src/interceptors.d.ts.map +1 -0
- package/lib/typescript/src/native.d.ts.map +1 -0
- package/lib/typescript/src/retry.d.ts.map +1 -0
- package/lib/typescript/src/server-streaming.d.ts.map +1 -0
- package/lib/typescript/src/status.d.ts.map +1 -0
- package/lib/typescript/src/types.d.ts.map +1 -0
- package/lib/typescript/src/unary.d.ts.map +1 -0
- package/lib/typescript/test/app/App.d.ts +3 -0
- package/lib/typescript/test/app/App.d.ts.map +1 -0
- package/package.json +209 -210
- package/react-native-grpc.podspec +33 -29
- package/src/bidi-streaming.ts +78 -78
- package/src/channel.ts +110 -110
- package/src/client-streaming.ts +106 -106
- package/src/client.ts +1046 -1046
- package/src/create-client.ts +391 -391
- package/src/errors.ts +11 -11
- package/src/index.ts +28 -28
- package/src/interceptors.ts +167 -167
- package/src/native.ts +99 -99
- package/src/retry.ts +208 -208
- package/src/server-streaming.ts +105 -105
- package/src/status.ts +50 -50
- package/src/types.ts +171 -171
- package/src/unary.ts +71 -71
- package/android/gradle.properties +0 -4
- package/lib/typescript/bidi-streaming.d.ts.map +0 -1
- package/lib/typescript/channel.d.ts.map +0 -1
- package/lib/typescript/client-streaming.d.ts.map +0 -1
- package/lib/typescript/client.d.ts.map +0 -1
- package/lib/typescript/create-client.d.ts.map +0 -1
- package/lib/typescript/errors.d.ts.map +0 -1
- package/lib/typescript/index.d.ts.map +0 -1
- package/lib/typescript/interceptors.d.ts.map +0 -1
- package/lib/typescript/native.d.ts.map +0 -1
- package/lib/typescript/retry.d.ts.map +0 -1
- package/lib/typescript/server-streaming.d.ts.map +0 -1
- package/lib/typescript/status.d.ts.map +0 -1
- package/lib/typescript/types.d.ts.map +0 -1
- package/lib/typescript/unary.d.ts.map +0 -1
- /package/lib/typescript/{bidi-streaming.d.ts → src/bidi-streaming.d.ts} +0 -0
- /package/lib/typescript/{channel.d.ts → src/channel.d.ts} +0 -0
- /package/lib/typescript/{client-streaming.d.ts → src/client-streaming.d.ts} +0 -0
- /package/lib/typescript/{client.d.ts → src/client.d.ts} +0 -0
- /package/lib/typescript/{create-client.d.ts → src/create-client.d.ts} +0 -0
- /package/lib/typescript/{errors.d.ts → src/errors.d.ts} +0 -0
- /package/lib/typescript/{index.d.ts → src/index.d.ts} +0 -0
- /package/lib/typescript/{interceptors.d.ts → src/interceptors.d.ts} +0 -0
- /package/lib/typescript/{native.d.ts → src/native.d.ts} +0 -0
- /package/lib/typescript/{retry.d.ts → src/retry.d.ts} +0 -0
- /package/lib/typescript/{server-streaming.d.ts → src/server-streaming.d.ts} +0 -0
- /package/lib/typescript/{status.d.ts → src/status.d.ts} +0 -0
- /package/lib/typescript/{types.d.ts → src/types.d.ts} +0 -0
- /package/lib/typescript/{unary.d.ts → src/unary.d.ts} +0 -0
package/src/client.ts
CHANGED
|
@@ -1,1046 +1,1046 @@
|
|
|
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
|
-
combineInterceptors,
|
|
9
|
-
GrpcInterceptor,
|
|
10
|
-
runOnError,
|
|
11
|
-
runOnHeaders,
|
|
12
|
-
runOnMessage,
|
|
13
|
-
runOnSendMessage,
|
|
14
|
-
runOnStart,
|
|
15
|
-
runOnTrailers,
|
|
16
|
-
toGrpcError,
|
|
17
|
-
} from './interceptors';
|
|
18
|
-
import { GrpcRequestObject, nativeGrpc } from './native';
|
|
19
|
-
import {
|
|
20
|
-
GrpcServerStreamingCall,
|
|
21
|
-
ServerOutputStream,
|
|
22
|
-
} from './server-streaming';
|
|
23
|
-
import {
|
|
24
|
-
computeRetryBackoffMs,
|
|
25
|
-
delay,
|
|
26
|
-
EffectiveCallPolicy,
|
|
27
|
-
GrpcHedgingPolicy,
|
|
28
|
-
GrpcRetryPolicy,
|
|
29
|
-
normalizeHedgingPolicy,
|
|
30
|
-
normalizeRetryPolicy,
|
|
31
|
-
remainingDeadlineSeconds,
|
|
32
|
-
resolveEffectivePolicy,
|
|
33
|
-
} from './retry';
|
|
34
|
-
import { GrpcStatusCode } from './status';
|
|
35
|
-
import {
|
|
36
|
-
DEFAULT_CHANNEL_ID,
|
|
37
|
-
GrpcCallOptions,
|
|
38
|
-
GrpcMetadata,
|
|
39
|
-
GrpcTlsOptions,
|
|
40
|
-
} from './types';
|
|
41
|
-
import { GrpcUnaryCall } from './unary';
|
|
42
|
-
|
|
43
|
-
type GrpcEventType = 'response' | 'error' | 'headers' | 'trailers';
|
|
44
|
-
|
|
45
|
-
/* prettier-ignore */
|
|
46
|
-
type GrpcEventPayload =
|
|
47
|
-
{
|
|
48
|
-
type: 'response';
|
|
49
|
-
payload: string;
|
|
50
|
-
} | {
|
|
51
|
-
type: 'error';
|
|
52
|
-
error: string;
|
|
53
|
-
code?: number;
|
|
54
|
-
trailers?: GrpcMetadata;
|
|
55
|
-
} | {
|
|
56
|
-
type: 'headers';
|
|
57
|
-
payload: GrpcMetadata;
|
|
58
|
-
} | {
|
|
59
|
-
type: 'trailers';
|
|
60
|
-
payload: GrpcMetadata;
|
|
61
|
-
} | {
|
|
62
|
-
type: 'status';
|
|
63
|
-
payload: number;
|
|
64
|
-
};
|
|
65
|
-
|
|
66
|
-
type GrpcEvent = {
|
|
67
|
-
id: number;
|
|
68
|
-
type: GrpcEventType;
|
|
69
|
-
} & GrpcEventPayload;
|
|
70
|
-
|
|
71
|
-
const Emitter = new NativeEventEmitter(NativeModules.Grpc);
|
|
72
|
-
|
|
73
|
-
type Deferred<T> = {
|
|
74
|
-
promise: Promise<T>;
|
|
75
|
-
resolve: (value: T) => void;
|
|
76
|
-
reject: (reason: any) => void;
|
|
77
|
-
};
|
|
78
|
-
|
|
79
|
-
type DeferredCalls = {
|
|
80
|
-
headers?: Deferred<GrpcMetadata>;
|
|
81
|
-
response?: Deferred<Uint8Array>;
|
|
82
|
-
trailers?: Deferred<GrpcMetadata>;
|
|
83
|
-
data?: ServerOutputStream;
|
|
84
|
-
method: string;
|
|
85
|
-
interceptors: GrpcInterceptor[];
|
|
86
|
-
};
|
|
87
|
-
|
|
88
|
-
type DeferredCallMap = {
|
|
89
|
-
[id: number]: DeferredCalls;
|
|
90
|
-
};
|
|
91
|
-
|
|
92
|
-
function createDeferred<T>(signal: AbortSignal) {
|
|
93
|
-
let completed = false;
|
|
94
|
-
|
|
95
|
-
const deferred: Deferred<T> = {} as any;
|
|
96
|
-
|
|
97
|
-
deferred.promise = new Promise<T>((resolve, reject) => {
|
|
98
|
-
deferred.resolve = (value) => {
|
|
99
|
-
completed = true;
|
|
100
|
-
|
|
101
|
-
resolve(value);
|
|
102
|
-
};
|
|
103
|
-
deferred.reject = (reason) => {
|
|
104
|
-
completed = true;
|
|
105
|
-
|
|
106
|
-
reject(reason);
|
|
107
|
-
};
|
|
108
|
-
});
|
|
109
|
-
|
|
110
|
-
signal.addEventListener('abort', () => {
|
|
111
|
-
if (!completed) {
|
|
112
|
-
deferred.reject('aborted');
|
|
113
|
-
}
|
|
114
|
-
});
|
|
115
|
-
|
|
116
|
-
return deferred;
|
|
117
|
-
}
|
|
118
|
-
|
|
119
|
-
let idCtr = 1;
|
|
120
|
-
|
|
121
|
-
const deferredMap: DeferredCallMap = {};
|
|
122
|
-
|
|
123
|
-
function rejectDeferredCall(deferred: DeferredCalls, error: GrpcError) {
|
|
124
|
-
deferred.headers?.reject(error);
|
|
125
|
-
deferred.trailers?.reject(error);
|
|
126
|
-
deferred.response?.reject(error);
|
|
127
|
-
deferred.data?.noitfyError(error);
|
|
128
|
-
}
|
|
129
|
-
|
|
130
|
-
function handleGrpcEvent(event: GrpcEvent) {
|
|
131
|
-
const deferred = deferredMap[event.id];
|
|
132
|
-
|
|
133
|
-
if (deferred) {
|
|
134
|
-
const { method, interceptors } = deferred;
|
|
135
|
-
switch (event.type) {
|
|
136
|
-
case 'headers':
|
|
137
|
-
deferred.headers?.resolve(
|
|
138
|
-
runOnHeaders(interceptors, event.payload, method)
|
|
139
|
-
);
|
|
140
|
-
break;
|
|
141
|
-
case 'response': {
|
|
142
|
-
const data = runOnMessage(
|
|
143
|
-
interceptors,
|
|
144
|
-
toByteArray(event.payload),
|
|
145
|
-
method
|
|
146
|
-
);
|
|
147
|
-
|
|
148
|
-
deferred.data?.notifyData(data);
|
|
149
|
-
deferred.response?.resolve(data);
|
|
150
|
-
break;
|
|
151
|
-
}
|
|
152
|
-
case 'trailers':
|
|
153
|
-
deferred.trailers?.resolve(
|
|
154
|
-
runOnTrailers(interceptors, event.payload, method)
|
|
155
|
-
);
|
|
156
|
-
deferred.data?.notifyComplete();
|
|
157
|
-
|
|
158
|
-
delete deferredMap[event.id];
|
|
159
|
-
break;
|
|
160
|
-
case 'error': {
|
|
161
|
-
const error = runOnError(
|
|
162
|
-
interceptors,
|
|
163
|
-
new GrpcError(event.error, event.code, event.trailers),
|
|
164
|
-
method
|
|
165
|
-
);
|
|
166
|
-
|
|
167
|
-
rejectDeferredCall(deferred, error);
|
|
168
|
-
|
|
169
|
-
delete deferredMap[event.id];
|
|
170
|
-
break;
|
|
171
|
-
}
|
|
172
|
-
}
|
|
173
|
-
}
|
|
174
|
-
}
|
|
175
|
-
|
|
176
|
-
function getId(): number {
|
|
177
|
-
return idCtr++;
|
|
178
|
-
}
|
|
179
|
-
|
|
180
|
-
function buildRequestObject(
|
|
181
|
-
data: Uint8Array,
|
|
182
|
-
options?: GrpcCallOptions
|
|
183
|
-
): GrpcRequestObject {
|
|
184
|
-
const obj: GrpcRequestObject = {
|
|
185
|
-
data: fromByteArray(data),
|
|
186
|
-
};
|
|
187
|
-
if (options?.deadlineSeconds !== undefined) {
|
|
188
|
-
obj.deadlineSeconds = options.deadlineSeconds;
|
|
189
|
-
}
|
|
190
|
-
return obj;
|
|
191
|
-
}
|
|
192
|
-
|
|
193
|
-
export function normalizeTlsOptions(options: GrpcTlsOptions = {}): {
|
|
194
|
-
rootCertsPem: string | null;
|
|
195
|
-
certificateChainPem: string | null;
|
|
196
|
-
privateKeyPem: string | null;
|
|
197
|
-
hostNameOverride: string | null;
|
|
198
|
-
spkiSha256Pins: string[] | null;
|
|
199
|
-
} {
|
|
200
|
-
const rootCertsPem = options.rootCertsPem ?? null;
|
|
201
|
-
const certificateChainPem = options.certificateChainPem ?? null;
|
|
202
|
-
const privateKeyPem = options.privateKeyPem ?? null;
|
|
203
|
-
const hostNameOverride = options.hostNameOverride ?? null;
|
|
204
|
-
const spkiSha256Pins =
|
|
205
|
-
options.spkiSha256Pins && options.spkiSha256Pins.length > 0
|
|
206
|
-
? options.spkiSha256Pins
|
|
207
|
-
: null;
|
|
208
|
-
|
|
209
|
-
const hasCert = !!certificateChainPem;
|
|
210
|
-
const hasKey = !!privateKeyPem;
|
|
211
|
-
if (hasCert !== hasKey) {
|
|
212
|
-
throw new Error('mTLS requires both certificateChainPem and privateKeyPem');
|
|
213
|
-
}
|
|
214
|
-
|
|
215
|
-
return {
|
|
216
|
-
rootCertsPem,
|
|
217
|
-
certificateChainPem,
|
|
218
|
-
privateKeyPem,
|
|
219
|
-
hostNameOverride,
|
|
220
|
-
spkiSha256Pins,
|
|
221
|
-
};
|
|
222
|
-
}
|
|
223
|
-
|
|
224
|
-
/**
|
|
225
|
-
* Shared RPC surface bound to a native channel id.
|
|
226
|
-
* Used by the singleton {@link GrpcClient} and by {@link GrpcChannel}.
|
|
227
|
-
*/
|
|
228
|
-
export class GrpcCallApi {
|
|
229
|
-
protected channelInterceptors: GrpcInterceptor[];
|
|
230
|
-
protected channelDeadlineSeconds = 120;
|
|
231
|
-
protected channelRetry: GrpcRetryPolicy | null = null;
|
|
232
|
-
protected channelHedging: GrpcHedgingPolicy | null = null;
|
|
233
|
-
|
|
234
|
-
constructor(
|
|
235
|
-
protected readonly channelId: string,
|
|
236
|
-
channelInterceptors: readonly GrpcInterceptor[] = [],
|
|
237
|
-
channelDeadlineSeconds = 120,
|
|
238
|
-
channelRetry: GrpcRetryPolicy | null = null,
|
|
239
|
-
channelHedging: GrpcHedgingPolicy | null = null
|
|
240
|
-
) {
|
|
241
|
-
this.channelInterceptors = [...channelInterceptors];
|
|
242
|
-
this.channelDeadlineSeconds = channelDeadlineSeconds;
|
|
243
|
-
this.channelRetry = channelRetry;
|
|
244
|
-
this.channelHedging = channelHedging;
|
|
245
|
-
}
|
|
246
|
-
|
|
247
|
-
protected resolveInterceptors(options?: GrpcCallOptions): GrpcInterceptor[] {
|
|
248
|
-
return combineInterceptors(this.channelInterceptors, options?.interceptors);
|
|
249
|
-
}
|
|
250
|
-
|
|
251
|
-
protected resolvePolicy(options?: GrpcCallOptions): EffectiveCallPolicy {
|
|
252
|
-
return resolveEffectivePolicy(
|
|
253
|
-
this.channelRetry,
|
|
254
|
-
this.channelHedging,
|
|
255
|
-
options?.retry,
|
|
256
|
-
options?.hedging
|
|
257
|
-
);
|
|
258
|
-
}
|
|
259
|
-
|
|
260
|
-
protected resolveDeadlineBudgetSeconds(
|
|
261
|
-
options?: GrpcCallOptions
|
|
262
|
-
): number | undefined {
|
|
263
|
-
if (options?.deadlineSeconds !== undefined) {
|
|
264
|
-
return options.deadlineSeconds;
|
|
265
|
-
}
|
|
266
|
-
return this.channelDeadlineSeconds;
|
|
267
|
-
}
|
|
268
|
-
|
|
269
|
-
private async runUnaryAttempt(
|
|
270
|
-
method: string,
|
|
271
|
-
data: Uint8Array,
|
|
272
|
-
requestHeaders: GrpcMetadata,
|
|
273
|
-
options: GrpcCallOptions | undefined,
|
|
274
|
-
interceptors: GrpcInterceptor[],
|
|
275
|
-
parentSignal: AbortSignal,
|
|
276
|
-
attemptIds: Set<number>
|
|
277
|
-
): Promise<{
|
|
278
|
-
headers: GrpcMetadata;
|
|
279
|
-
response: Uint8Array;
|
|
280
|
-
trailers: GrpcMetadata;
|
|
281
|
-
}> {
|
|
282
|
-
if (parentSignal.aborted) {
|
|
283
|
-
throw new GrpcError('aborted', GrpcStatusCode.CANCELLED);
|
|
284
|
-
}
|
|
285
|
-
|
|
286
|
-
const id = getId();
|
|
287
|
-
attemptIds.add(id);
|
|
288
|
-
const attemptAbort = new AbortController();
|
|
289
|
-
const onParentAbort = () => attemptAbort.abort();
|
|
290
|
-
parentSignal.addEventListener('abort', onParentAbort);
|
|
291
|
-
|
|
292
|
-
const response = createDeferred<Uint8Array>(attemptAbort.signal);
|
|
293
|
-
const headers = createDeferred<GrpcMetadata>(attemptAbort.signal);
|
|
294
|
-
const trailers = createDeferred<GrpcMetadata>(attemptAbort.signal);
|
|
295
|
-
|
|
296
|
-
deferredMap[id] = {
|
|
297
|
-
response,
|
|
298
|
-
headers,
|
|
299
|
-
trailers,
|
|
300
|
-
method,
|
|
301
|
-
interceptors,
|
|
302
|
-
};
|
|
303
|
-
|
|
304
|
-
try {
|
|
305
|
-
let start = await runOnStart(interceptors, {
|
|
306
|
-
method,
|
|
307
|
-
headers: { ...requestHeaders },
|
|
308
|
-
options: { ...(options || {}) },
|
|
309
|
-
request: data,
|
|
310
|
-
});
|
|
311
|
-
const callOptions = { ...start.options };
|
|
312
|
-
delete callOptions.interceptors;
|
|
313
|
-
delete callOptions.retry;
|
|
314
|
-
delete callOptions.hedging;
|
|
315
|
-
start = {
|
|
316
|
-
...start,
|
|
317
|
-
options: callOptions,
|
|
318
|
-
};
|
|
319
|
-
const requestBytes = await runOnSendMessage(
|
|
320
|
-
interceptors,
|
|
321
|
-
start.request ?? data,
|
|
322
|
-
start.method
|
|
323
|
-
);
|
|
324
|
-
const obj = buildRequestObject(requestBytes, start.options);
|
|
325
|
-
await nativeGrpc().unaryCall(
|
|
326
|
-
id,
|
|
327
|
-
start.method,
|
|
328
|
-
obj,
|
|
329
|
-
start.headers,
|
|
330
|
-
this.channelId
|
|
331
|
-
);
|
|
332
|
-
|
|
333
|
-
const [hdrs, body, trl] = await Promise.all([
|
|
334
|
-
headers.promise,
|
|
335
|
-
response.promise,
|
|
336
|
-
trailers.promise,
|
|
337
|
-
]);
|
|
338
|
-
return { headers: hdrs, response: body, trailers: trl };
|
|
339
|
-
} catch (reason) {
|
|
340
|
-
if (attemptAbort.signal.aborted || parentSignal.aborted) {
|
|
341
|
-
throw new GrpcError('aborted', GrpcStatusCode.CANCELLED);
|
|
342
|
-
}
|
|
343
|
-
throw toGrpcError(reason);
|
|
344
|
-
} finally {
|
|
345
|
-
parentSignal.removeEventListener('abort', onParentAbort);
|
|
346
|
-
attemptIds.delete(id);
|
|
347
|
-
if (attemptAbort.signal.aborted) {
|
|
348
|
-
nativeGrpc().cancelGrpcCall(id);
|
|
349
|
-
}
|
|
350
|
-
}
|
|
351
|
-
}
|
|
352
|
-
|
|
353
|
-
private async runUnaryWithPolicy(
|
|
354
|
-
method: string,
|
|
355
|
-
data: Uint8Array,
|
|
356
|
-
requestHeaders: GrpcMetadata,
|
|
357
|
-
options: GrpcCallOptions | undefined,
|
|
358
|
-
interceptors: GrpcInterceptor[],
|
|
359
|
-
parentSignal: AbortSignal,
|
|
360
|
-
policy: EffectiveCallPolicy,
|
|
361
|
-
deadlineBudgetSeconds: number | undefined
|
|
362
|
-
): Promise<{
|
|
363
|
-
headers: GrpcMetadata;
|
|
364
|
-
response: Uint8Array;
|
|
365
|
-
trailers: GrpcMetadata;
|
|
366
|
-
}> {
|
|
367
|
-
const startedAt = Date.now();
|
|
368
|
-
const attemptIds = new Set<number>();
|
|
369
|
-
|
|
370
|
-
const withRemainingDeadline = (): GrpcCallOptions => {
|
|
371
|
-
const next: GrpcCallOptions = { ...(options || {}) };
|
|
372
|
-
delete next.interceptors;
|
|
373
|
-
delete next.retry;
|
|
374
|
-
delete next.hedging;
|
|
375
|
-
if (policy.kind === 'none') {
|
|
376
|
-
if (options?.deadlineSeconds === undefined) {
|
|
377
|
-
delete next.deadlineSeconds;
|
|
378
|
-
}
|
|
379
|
-
return next;
|
|
380
|
-
}
|
|
381
|
-
const remaining = remainingDeadlineSeconds(
|
|
382
|
-
deadlineBudgetSeconds,
|
|
383
|
-
startedAt
|
|
384
|
-
);
|
|
385
|
-
if (remaining !== undefined) {
|
|
386
|
-
next.deadlineSeconds = remaining;
|
|
387
|
-
}
|
|
388
|
-
return next;
|
|
389
|
-
};
|
|
390
|
-
|
|
391
|
-
const cancelAllAttempts = () => {
|
|
392
|
-
for (const id of [...attemptIds]) {
|
|
393
|
-
nativeGrpc().cancelGrpcCall(id);
|
|
394
|
-
}
|
|
395
|
-
};
|
|
396
|
-
const onAbort = () => cancelAllAttempts();
|
|
397
|
-
parentSignal.addEventListener('abort', onAbort);
|
|
398
|
-
|
|
399
|
-
try {
|
|
400
|
-
if (policy.kind === 'none') {
|
|
401
|
-
return await this.runUnaryAttempt(
|
|
402
|
-
method,
|
|
403
|
-
data,
|
|
404
|
-
requestHeaders,
|
|
405
|
-
withRemainingDeadline(),
|
|
406
|
-
interceptors,
|
|
407
|
-
parentSignal,
|
|
408
|
-
attemptIds
|
|
409
|
-
);
|
|
410
|
-
}
|
|
411
|
-
|
|
412
|
-
if (policy.kind === 'retry') {
|
|
413
|
-
let lastError: GrpcError | undefined;
|
|
414
|
-
for (let attempt = 1; attempt <= policy.policy.maxAttempts; attempt++) {
|
|
415
|
-
if (parentSignal.aborted) {
|
|
416
|
-
throw new GrpcError('aborted', GrpcStatusCode.CANCELLED);
|
|
417
|
-
}
|
|
418
|
-
const remaining = remainingDeadlineSeconds(
|
|
419
|
-
deadlineBudgetSeconds,
|
|
420
|
-
startedAt
|
|
421
|
-
);
|
|
422
|
-
if (
|
|
423
|
-
deadlineBudgetSeconds !== undefined &&
|
|
424
|
-
deadlineBudgetSeconds > 0 &&
|
|
425
|
-
remaining !== undefined &&
|
|
426
|
-
remaining <= 0
|
|
427
|
-
) {
|
|
428
|
-
throw new GrpcError(
|
|
429
|
-
'DEADLINE_EXCEEDED',
|
|
430
|
-
GrpcStatusCode.DEADLINE_EXCEEDED
|
|
431
|
-
);
|
|
432
|
-
}
|
|
433
|
-
try {
|
|
434
|
-
return await this.runUnaryAttempt(
|
|
435
|
-
method,
|
|
436
|
-
data,
|
|
437
|
-
requestHeaders,
|
|
438
|
-
withRemainingDeadline(),
|
|
439
|
-
interceptors,
|
|
440
|
-
parentSignal,
|
|
441
|
-
attemptIds
|
|
442
|
-
);
|
|
443
|
-
} catch (reason) {
|
|
444
|
-
const error = toGrpcError(reason);
|
|
445
|
-
lastError = error;
|
|
446
|
-
if (
|
|
447
|
-
error.code === GrpcStatusCode.CANCELLED ||
|
|
448
|
-
parentSignal.aborted
|
|
449
|
-
) {
|
|
450
|
-
throw error;
|
|
451
|
-
}
|
|
452
|
-
const retryable =
|
|
453
|
-
error.code !== undefined &&
|
|
454
|
-
policy.policy.retryableStatusCodes.has(error.code);
|
|
455
|
-
if (!retryable || attempt >= policy.policy.maxAttempts) {
|
|
456
|
-
throw error;
|
|
457
|
-
}
|
|
458
|
-
const backoffMs = computeRetryBackoffMs(policy.policy, attempt);
|
|
459
|
-
await delay(backoffMs, parentSignal as any);
|
|
460
|
-
}
|
|
461
|
-
}
|
|
462
|
-
throw (
|
|
463
|
-
lastError ?? new GrpcError('UNAVAILABLE', GrpcStatusCode.UNAVAILABLE)
|
|
464
|
-
);
|
|
465
|
-
}
|
|
466
|
-
|
|
467
|
-
// hedging
|
|
468
|
-
type AttemptOutcome =
|
|
469
|
-
| {
|
|
470
|
-
ok: true;
|
|
471
|
-
value: {
|
|
472
|
-
headers: GrpcMetadata;
|
|
473
|
-
response: Uint8Array;
|
|
474
|
-
trailers: GrpcMetadata;
|
|
475
|
-
};
|
|
476
|
-
}
|
|
477
|
-
| { ok: false; error: GrpcError };
|
|
478
|
-
|
|
479
|
-
return await new Promise((resolve, reject) => {
|
|
480
|
-
let settled = false;
|
|
481
|
-
let started = 0;
|
|
482
|
-
let finished = 0;
|
|
483
|
-
const timers: ReturnType<typeof setTimeout>[] = [];
|
|
484
|
-
const inFlight = new Map<number, Promise<AttemptOutcome>>();
|
|
485
|
-
|
|
486
|
-
const finishSuccess = (value: {
|
|
487
|
-
headers: GrpcMetadata;
|
|
488
|
-
response: Uint8Array;
|
|
489
|
-
trailers: GrpcMetadata;
|
|
490
|
-
}) => {
|
|
491
|
-
if (settled) {
|
|
492
|
-
return;
|
|
493
|
-
}
|
|
494
|
-
settled = true;
|
|
495
|
-
timers.forEach(clearTimeout);
|
|
496
|
-
cancelAllAttempts();
|
|
497
|
-
resolve(value);
|
|
498
|
-
};
|
|
499
|
-
|
|
500
|
-
const finishError = (error: GrpcError) => {
|
|
501
|
-
if (settled) {
|
|
502
|
-
return;
|
|
503
|
-
}
|
|
504
|
-
settled = true;
|
|
505
|
-
timers.forEach(clearTimeout);
|
|
506
|
-
cancelAllAttempts();
|
|
507
|
-
reject(error);
|
|
508
|
-
};
|
|
509
|
-
|
|
510
|
-
const launch = () => {
|
|
511
|
-
if (settled || parentSignal.aborted) {
|
|
512
|
-
return;
|
|
513
|
-
}
|
|
514
|
-
const remaining = remainingDeadlineSeconds(
|
|
515
|
-
deadlineBudgetSeconds,
|
|
516
|
-
startedAt
|
|
517
|
-
);
|
|
518
|
-
if (
|
|
519
|
-
deadlineBudgetSeconds !== undefined &&
|
|
520
|
-
deadlineBudgetSeconds > 0 &&
|
|
521
|
-
remaining !== undefined &&
|
|
522
|
-
remaining <= 0
|
|
523
|
-
) {
|
|
524
|
-
finishError(
|
|
525
|
-
new GrpcError(
|
|
526
|
-
'DEADLINE_EXCEEDED',
|
|
527
|
-
GrpcStatusCode.DEADLINE_EXCEEDED
|
|
528
|
-
)
|
|
529
|
-
);
|
|
530
|
-
return;
|
|
531
|
-
}
|
|
532
|
-
|
|
533
|
-
started += 1;
|
|
534
|
-
const launchIndex = started;
|
|
535
|
-
const p = this.runUnaryAttempt(
|
|
536
|
-
method,
|
|
537
|
-
data,
|
|
538
|
-
requestHeaders,
|
|
539
|
-
withRemainingDeadline(),
|
|
540
|
-
interceptors,
|
|
541
|
-
parentSignal,
|
|
542
|
-
attemptIds
|
|
543
|
-
)
|
|
544
|
-
.then(
|
|
545
|
-
(value): AttemptOutcome => ({ ok: true, value }),
|
|
546
|
-
(reason): AttemptOutcome => ({
|
|
547
|
-
ok: false,
|
|
548
|
-
error: toGrpcError(reason),
|
|
549
|
-
})
|
|
550
|
-
)
|
|
551
|
-
.then((outcome) => {
|
|
552
|
-
inFlight.delete(launchIndex);
|
|
553
|
-
finished += 1;
|
|
554
|
-
if (settled) {
|
|
555
|
-
return outcome;
|
|
556
|
-
}
|
|
557
|
-
if (outcome.ok) {
|
|
558
|
-
finishSuccess(outcome.value);
|
|
559
|
-
return outcome;
|
|
560
|
-
}
|
|
561
|
-
const { error } = outcome;
|
|
562
|
-
if (
|
|
563
|
-
error.code === GrpcStatusCode.CANCELLED ||
|
|
564
|
-
parentSignal.aborted
|
|
565
|
-
) {
|
|
566
|
-
if (finished >= started && inFlight.size === 0) {
|
|
567
|
-
finishError(error);
|
|
568
|
-
}
|
|
569
|
-
return outcome;
|
|
570
|
-
}
|
|
571
|
-
const nonFatal =
|
|
572
|
-
error.code !== undefined &&
|
|
573
|
-
policy.policy.nonFatalStatusCodes.has(error.code);
|
|
574
|
-
if (!nonFatal) {
|
|
575
|
-
finishError(error);
|
|
576
|
-
return outcome;
|
|
577
|
-
}
|
|
578
|
-
if (
|
|
579
|
-
finished >= policy.policy.maxAttempts ||
|
|
580
|
-
(started >= policy.policy.maxAttempts && inFlight.size === 0)
|
|
581
|
-
) {
|
|
582
|
-
finishError(error);
|
|
583
|
-
}
|
|
584
|
-
return outcome;
|
|
585
|
-
});
|
|
586
|
-
inFlight.set(launchIndex, p);
|
|
587
|
-
};
|
|
588
|
-
|
|
589
|
-
launch();
|
|
590
|
-
for (let i = 2; i <= policy.policy.maxAttempts; i++) {
|
|
591
|
-
const delayMs = policy.policy.hedgingDelayMs * (i - 1);
|
|
592
|
-
if (delayMs <= 0) {
|
|
593
|
-
launch();
|
|
594
|
-
} else {
|
|
595
|
-
timers.push(setTimeout(launch, delayMs));
|
|
596
|
-
}
|
|
597
|
-
}
|
|
598
|
-
|
|
599
|
-
parentSignal.addEventListener('abort', () => {
|
|
600
|
-
finishError(new GrpcError('aborted', GrpcStatusCode.CANCELLED));
|
|
601
|
-
});
|
|
602
|
-
});
|
|
603
|
-
} finally {
|
|
604
|
-
parentSignal.removeEventListener('abort', onAbort);
|
|
605
|
-
}
|
|
606
|
-
}
|
|
607
|
-
|
|
608
|
-
unaryCall(
|
|
609
|
-
method: string,
|
|
610
|
-
data: Uint8Array,
|
|
611
|
-
requestHeaders?: GrpcMetadata,
|
|
612
|
-
options?: GrpcCallOptions
|
|
613
|
-
): GrpcUnaryCall {
|
|
614
|
-
const abort = new AbortController();
|
|
615
|
-
const interceptors = this.resolveInterceptors(options);
|
|
616
|
-
const policy = this.resolvePolicy(options);
|
|
617
|
-
const deadlineBudgetSeconds = this.resolveDeadlineBudgetSeconds(options);
|
|
618
|
-
|
|
619
|
-
const response = createDeferred<Uint8Array>(abort.signal);
|
|
620
|
-
const headers = createDeferred<GrpcMetadata>(abort.signal);
|
|
621
|
-
const trailers = createDeferred<GrpcMetadata>(abort.signal);
|
|
622
|
-
|
|
623
|
-
const call = new GrpcUnaryCall(
|
|
624
|
-
method,
|
|
625
|
-
data,
|
|
626
|
-
requestHeaders || {},
|
|
627
|
-
headers.promise,
|
|
628
|
-
response.promise,
|
|
629
|
-
trailers.promise,
|
|
630
|
-
abort
|
|
631
|
-
);
|
|
632
|
-
|
|
633
|
-
call.then(
|
|
634
|
-
(result) => result,
|
|
635
|
-
() => abort.abort()
|
|
636
|
-
);
|
|
637
|
-
|
|
638
|
-
Promise.resolve()
|
|
639
|
-
.then(async () => {
|
|
640
|
-
const result = await this.runUnaryWithPolicy(
|
|
641
|
-
method,
|
|
642
|
-
data,
|
|
643
|
-
requestHeaders || {},
|
|
644
|
-
options,
|
|
645
|
-
interceptors,
|
|
646
|
-
abort.signal,
|
|
647
|
-
policy,
|
|
648
|
-
deadlineBudgetSeconds
|
|
649
|
-
);
|
|
650
|
-
headers.resolve(result.headers);
|
|
651
|
-
response.resolve(result.response);
|
|
652
|
-
trailers.resolve(result.trailers);
|
|
653
|
-
})
|
|
654
|
-
.catch((reason) => {
|
|
655
|
-
if (abort.signal.aborted) {
|
|
656
|
-
const error = new GrpcError('aborted', GrpcStatusCode.CANCELLED);
|
|
657
|
-
headers.reject(error);
|
|
658
|
-
response.reject(error);
|
|
659
|
-
trailers.reject(error);
|
|
660
|
-
return;
|
|
661
|
-
}
|
|
662
|
-
const error = runOnError(interceptors, toGrpcError(reason), method);
|
|
663
|
-
headers.reject(error);
|
|
664
|
-
response.reject(error);
|
|
665
|
-
trailers.reject(error);
|
|
666
|
-
});
|
|
667
|
-
|
|
668
|
-
return call;
|
|
669
|
-
}
|
|
670
|
-
|
|
671
|
-
serverStreamCall(
|
|
672
|
-
method: string,
|
|
673
|
-
data: Uint8Array,
|
|
674
|
-
requestHeaders?: GrpcMetadata,
|
|
675
|
-
options?: GrpcCallOptions
|
|
676
|
-
): GrpcServerStreamingCall {
|
|
677
|
-
const id = getId();
|
|
678
|
-
const abort = new AbortController();
|
|
679
|
-
const interceptors = this.resolveInterceptors(options);
|
|
680
|
-
|
|
681
|
-
abort.signal.addEventListener('abort', () => {
|
|
682
|
-
nativeGrpc().cancelGrpcCall(id);
|
|
683
|
-
});
|
|
684
|
-
|
|
685
|
-
const headers = createDeferred<GrpcMetadata>(abort.signal);
|
|
686
|
-
const trailers = createDeferred<GrpcMetadata>(abort.signal);
|
|
687
|
-
|
|
688
|
-
const stream = new ServerOutputStream();
|
|
689
|
-
|
|
690
|
-
deferredMap[id] = {
|
|
691
|
-
headers,
|
|
692
|
-
trailers,
|
|
693
|
-
data: stream,
|
|
694
|
-
method,
|
|
695
|
-
interceptors,
|
|
696
|
-
};
|
|
697
|
-
|
|
698
|
-
const call = new GrpcServerStreamingCall(
|
|
699
|
-
method,
|
|
700
|
-
data,
|
|
701
|
-
requestHeaders || {},
|
|
702
|
-
headers.promise,
|
|
703
|
-
stream,
|
|
704
|
-
trailers.promise,
|
|
705
|
-
abort
|
|
706
|
-
);
|
|
707
|
-
|
|
708
|
-
call.then(
|
|
709
|
-
(result) => result,
|
|
710
|
-
() => abort.abort()
|
|
711
|
-
);
|
|
712
|
-
|
|
713
|
-
Promise.resolve()
|
|
714
|
-
.then(async () => {
|
|
715
|
-
let start = await runOnStart(interceptors, {
|
|
716
|
-
method,
|
|
717
|
-
headers: { ...(requestHeaders || {}) },
|
|
718
|
-
options: { ...(options || {}) },
|
|
719
|
-
request: data,
|
|
720
|
-
});
|
|
721
|
-
const callOptions = { ...start.options };
|
|
722
|
-
delete callOptions.interceptors;
|
|
723
|
-
start = {
|
|
724
|
-
...start,
|
|
725
|
-
options: callOptions,
|
|
726
|
-
};
|
|
727
|
-
const requestBytes = await runOnSendMessage(
|
|
728
|
-
interceptors,
|
|
729
|
-
start.request ?? data,
|
|
730
|
-
start.method
|
|
731
|
-
);
|
|
732
|
-
const obj = buildRequestObject(requestBytes, start.options);
|
|
733
|
-
await nativeGrpc().serverStreamingCall(
|
|
734
|
-
id,
|
|
735
|
-
start.method,
|
|
736
|
-
obj,
|
|
737
|
-
start.headers,
|
|
738
|
-
this.channelId
|
|
739
|
-
);
|
|
740
|
-
})
|
|
741
|
-
.catch((reason) => {
|
|
742
|
-
const deferred = deferredMap[id];
|
|
743
|
-
if (!deferred) {
|
|
744
|
-
return;
|
|
745
|
-
}
|
|
746
|
-
const error = runOnError(interceptors, toGrpcError(reason), method);
|
|
747
|
-
rejectDeferredCall(deferred, error);
|
|
748
|
-
delete deferredMap[id];
|
|
749
|
-
});
|
|
750
|
-
|
|
751
|
-
return call;
|
|
752
|
-
}
|
|
753
|
-
|
|
754
|
-
/**
|
|
755
|
-
* Client-streaming RPC: many request messages, one response.
|
|
756
|
-
* Native call starts on the first `requests.send(...)`.
|
|
757
|
-
*/
|
|
758
|
-
clientStreamCall(
|
|
759
|
-
method: string,
|
|
760
|
-
requestHeaders?: GrpcMetadata,
|
|
761
|
-
options?: GrpcCallOptions
|
|
762
|
-
): GrpcClientStreamingCall {
|
|
763
|
-
const id = getId();
|
|
764
|
-
const abort = new AbortController();
|
|
765
|
-
const interceptors = this.resolveInterceptors(options);
|
|
766
|
-
let headersMeta = { ...(requestHeaders || {}) };
|
|
767
|
-
let callOptions: GrpcCallOptions = { ...(options || {}) };
|
|
768
|
-
const channelId = this.channelId;
|
|
769
|
-
let started = false;
|
|
770
|
-
|
|
771
|
-
abort.signal.addEventListener('abort', () => {
|
|
772
|
-
nativeGrpc().cancelGrpcCall(id);
|
|
773
|
-
});
|
|
774
|
-
|
|
775
|
-
const response = createDeferred<Uint8Array>(abort.signal);
|
|
776
|
-
const headers = createDeferred<GrpcMetadata>(abort.signal);
|
|
777
|
-
const trailers = createDeferred<GrpcMetadata>(abort.signal);
|
|
778
|
-
|
|
779
|
-
deferredMap[id] = {
|
|
780
|
-
response,
|
|
781
|
-
headers,
|
|
782
|
-
trailers,
|
|
783
|
-
method,
|
|
784
|
-
interceptors,
|
|
785
|
-
};
|
|
786
|
-
|
|
787
|
-
const requests = new ServerInputStream(
|
|
788
|
-
async (data, isFirst) => {
|
|
789
|
-
try {
|
|
790
|
-
let message = await runOnSendMessage(interceptors, data, method);
|
|
791
|
-
if (isFirst || !started) {
|
|
792
|
-
const start = await runOnStart(interceptors, {
|
|
793
|
-
method,
|
|
794
|
-
headers: headersMeta,
|
|
795
|
-
options: callOptions,
|
|
796
|
-
request: message,
|
|
797
|
-
});
|
|
798
|
-
headersMeta = start.headers;
|
|
799
|
-
callOptions = { ...start.options };
|
|
800
|
-
delete callOptions.interceptors;
|
|
801
|
-
message = start.request ?? message;
|
|
802
|
-
started = true;
|
|
803
|
-
}
|
|
804
|
-
const obj = buildRequestObject(
|
|
805
|
-
message,
|
|
806
|
-
isFirst ? callOptions : undefined
|
|
807
|
-
);
|
|
808
|
-
return nativeGrpc().clientStreamingCall(
|
|
809
|
-
id,
|
|
810
|
-
method,
|
|
811
|
-
obj,
|
|
812
|
-
isFirst ? headersMeta : {},
|
|
813
|
-
channelId
|
|
814
|
-
);
|
|
815
|
-
} catch (reason) {
|
|
816
|
-
const deferred = deferredMap[id];
|
|
817
|
-
if (deferred) {
|
|
818
|
-
const error = runOnError(interceptors, toGrpcError(reason), method);
|
|
819
|
-
rejectDeferredCall(deferred, error);
|
|
820
|
-
delete deferredMap[id];
|
|
821
|
-
}
|
|
822
|
-
throw toGrpcError(reason);
|
|
823
|
-
}
|
|
824
|
-
},
|
|
825
|
-
() => nativeGrpc().finishClientStreaming(id)
|
|
826
|
-
);
|
|
827
|
-
|
|
828
|
-
const call = new GrpcClientStreamingCall(
|
|
829
|
-
method,
|
|
830
|
-
headersMeta,
|
|
831
|
-
requests,
|
|
832
|
-
headers.promise,
|
|
833
|
-
response.promise,
|
|
834
|
-
trailers.promise,
|
|
835
|
-
abort
|
|
836
|
-
);
|
|
837
|
-
|
|
838
|
-
call.then(
|
|
839
|
-
(result) => result,
|
|
840
|
-
() => abort.abort()
|
|
841
|
-
);
|
|
842
|
-
|
|
843
|
-
return call;
|
|
844
|
-
}
|
|
845
|
-
|
|
846
|
-
/**
|
|
847
|
-
* Bidirectional streaming RPC: interleaved request and response messages.
|
|
848
|
-
* Native call starts on the first `requests.send(...)`.
|
|
849
|
-
* Registers only the `data` stream (plus headers/trailers) — no unary `response`.
|
|
850
|
-
*/
|
|
851
|
-
bidiStreamCall(
|
|
852
|
-
method: string,
|
|
853
|
-
requestHeaders?: GrpcMetadata,
|
|
854
|
-
options?: GrpcCallOptions
|
|
855
|
-
): GrpcBidiStreamingCall {
|
|
856
|
-
const id = getId();
|
|
857
|
-
const abort = new AbortController();
|
|
858
|
-
const interceptors = this.resolveInterceptors(options);
|
|
859
|
-
let headersMeta = { ...(requestHeaders || {}) };
|
|
860
|
-
let callOptions: GrpcCallOptions = { ...(options || {}) };
|
|
861
|
-
const channelId = this.channelId;
|
|
862
|
-
let started = false;
|
|
863
|
-
|
|
864
|
-
abort.signal.addEventListener('abort', () => {
|
|
865
|
-
nativeGrpc().cancelGrpcCall(id);
|
|
866
|
-
});
|
|
867
|
-
|
|
868
|
-
const headers = createDeferred<GrpcMetadata>(abort.signal);
|
|
869
|
-
const trailers = createDeferred<GrpcMetadata>(abort.signal);
|
|
870
|
-
const stream = new ServerOutputStream();
|
|
871
|
-
|
|
872
|
-
deferredMap[id] = {
|
|
873
|
-
headers,
|
|
874
|
-
trailers,
|
|
875
|
-
data: stream,
|
|
876
|
-
method,
|
|
877
|
-
interceptors,
|
|
878
|
-
};
|
|
879
|
-
|
|
880
|
-
const requests = new ServerInputStream(
|
|
881
|
-
async (data, isFirst) => {
|
|
882
|
-
try {
|
|
883
|
-
let message = await runOnSendMessage(interceptors, data, method);
|
|
884
|
-
if (isFirst || !started) {
|
|
885
|
-
const start = await runOnStart(interceptors, {
|
|
886
|
-
method,
|
|
887
|
-
headers: headersMeta,
|
|
888
|
-
options: callOptions,
|
|
889
|
-
request: message,
|
|
890
|
-
});
|
|
891
|
-
headersMeta = start.headers;
|
|
892
|
-
callOptions = { ...start.options };
|
|
893
|
-
delete callOptions.interceptors;
|
|
894
|
-
message = start.request ?? message;
|
|
895
|
-
started = true;
|
|
896
|
-
}
|
|
897
|
-
const obj = buildRequestObject(
|
|
898
|
-
message,
|
|
899
|
-
isFirst ? callOptions : undefined
|
|
900
|
-
);
|
|
901
|
-
return nativeGrpc().bidiStreamingCall(
|
|
902
|
-
id,
|
|
903
|
-
method,
|
|
904
|
-
obj,
|
|
905
|
-
isFirst ? headersMeta : {},
|
|
906
|
-
channelId
|
|
907
|
-
);
|
|
908
|
-
} catch (reason) {
|
|
909
|
-
const deferred = deferredMap[id];
|
|
910
|
-
if (deferred) {
|
|
911
|
-
const error = runOnError(interceptors, toGrpcError(reason), method);
|
|
912
|
-
rejectDeferredCall(deferred, error);
|
|
913
|
-
delete deferredMap[id];
|
|
914
|
-
}
|
|
915
|
-
throw toGrpcError(reason);
|
|
916
|
-
}
|
|
917
|
-
},
|
|
918
|
-
() => nativeGrpc().finishClientStreaming(id)
|
|
919
|
-
);
|
|
920
|
-
|
|
921
|
-
const call = new GrpcBidiStreamingCall(
|
|
922
|
-
method,
|
|
923
|
-
headersMeta,
|
|
924
|
-
requests,
|
|
925
|
-
headers.promise,
|
|
926
|
-
stream,
|
|
927
|
-
trailers.promise,
|
|
928
|
-
abort
|
|
929
|
-
);
|
|
930
|
-
|
|
931
|
-
call.then(
|
|
932
|
-
(result) => result,
|
|
933
|
-
() => abort.abort()
|
|
934
|
-
);
|
|
935
|
-
|
|
936
|
-
return call;
|
|
937
|
-
}
|
|
938
|
-
}
|
|
939
|
-
|
|
940
|
-
export class GrpcClient extends GrpcCallApi {
|
|
941
|
-
constructor() {
|
|
942
|
-
super(DEFAULT_CHANNEL_ID, [], 120, null, null);
|
|
943
|
-
Emitter.addListener('grpc-call', handleGrpcEvent);
|
|
944
|
-
}
|
|
945
|
-
destroy() {
|
|
946
|
-
Emitter.removeAllListeners('grpc-call');
|
|
947
|
-
}
|
|
948
|
-
/**
|
|
949
|
-
* Replace default-channel interceptors (same replace semantics as `setTlsOptions`).
|
|
950
|
-
* Prefer `createChannel({ interceptors })` for multi-host apps.
|
|
951
|
-
*/
|
|
952
|
-
setInterceptors(interceptors: GrpcInterceptor[] = []): void {
|
|
953
|
-
this.channelInterceptors = [...interceptors];
|
|
954
|
-
}
|
|
955
|
-
/**
|
|
956
|
-
* Replace unary retry policy on the default channel.
|
|
957
|
-
* Clears any hedging policy (mutually exclusive). Pass `null` to disable.
|
|
958
|
-
*/
|
|
959
|
-
setRetryPolicy(policy: GrpcRetryPolicy | null): void {
|
|
960
|
-
if (policy) {
|
|
961
|
-
normalizeRetryPolicy(policy);
|
|
962
|
-
}
|
|
963
|
-
this.channelRetry = policy;
|
|
964
|
-
this.channelHedging = null;
|
|
965
|
-
}
|
|
966
|
-
/**
|
|
967
|
-
* Replace unary hedging policy on the default channel.
|
|
968
|
-
* Clears any retry policy (mutually exclusive). Pass `null` to disable.
|
|
969
|
-
*/
|
|
970
|
-
setHedgingPolicy(policy: GrpcHedgingPolicy | null): void {
|
|
971
|
-
if (policy) {
|
|
972
|
-
normalizeHedgingPolicy(policy);
|
|
973
|
-
}
|
|
974
|
-
this.channelHedging = policy;
|
|
975
|
-
this.channelRetry = null;
|
|
976
|
-
}
|
|
977
|
-
getHost(): Promise<string> {
|
|
978
|
-
return nativeGrpc().getHost();
|
|
979
|
-
}
|
|
980
|
-
setHost(host: string): void {
|
|
981
|
-
nativeGrpc().setHost(host);
|
|
982
|
-
}
|
|
983
|
-
getInsecure(): Promise<boolean> {
|
|
984
|
-
return nativeGrpc().getIsInsecure();
|
|
985
|
-
}
|
|
986
|
-
setInsecure(insecure: boolean): void {
|
|
987
|
-
nativeGrpc().setInsecure(insecure);
|
|
988
|
-
}
|
|
989
|
-
/**
|
|
990
|
-
* Replace channel TLS options (custom CA, mTLS, hostname override).
|
|
991
|
-
* Each call replaces the previous config; omitted fields are cleared.
|
|
992
|
-
* Apply before `initGrpcChannel()`. Ignored while insecure/plaintext.
|
|
993
|
-
* For multi-host apps prefer {@link createChannel} with `tls` in config.
|
|
994
|
-
*/
|
|
995
|
-
setTlsOptions(options: GrpcTlsOptions = {}): void {
|
|
996
|
-
nativeGrpc().setTlsOptions(normalizeTlsOptions(options));
|
|
997
|
-
}
|
|
998
|
-
setCompression(enable: boolean, compressorName: string): void {
|
|
999
|
-
nativeGrpc().setCompression(enable, compressorName);
|
|
1000
|
-
}
|
|
1001
|
-
setResponseSizeLimit(limitInBytes: number): void {
|
|
1002
|
-
nativeGrpc().setResponseSizeLimit(limitInBytes);
|
|
1003
|
-
}
|
|
1004
|
-
/** Global per-call deadline in seconds (Android/iOS). Default 120. Overridable per RPC via options. */
|
|
1005
|
-
setCallDeadlineSeconds(seconds: number): void {
|
|
1006
|
-
this.channelDeadlineSeconds = Math.max(0, seconds);
|
|
1007
|
-
nativeGrpc().setCallDeadlineSeconds(seconds);
|
|
1008
|
-
}
|
|
1009
|
-
|
|
1010
|
-
initGrpcChannel() {
|
|
1011
|
-
nativeGrpc().initGrpcChannel();
|
|
1012
|
-
}
|
|
1013
|
-
|
|
1014
|
-
setKeepAlive(
|
|
1015
|
-
enable: boolean,
|
|
1016
|
-
keepAliveTime: number,
|
|
1017
|
-
keepAliveTimeOut: number
|
|
1018
|
-
): void {
|
|
1019
|
-
nativeGrpc().setKeepAlive(enable, keepAliveTime, keepAliveTimeOut);
|
|
1020
|
-
}
|
|
1021
|
-
|
|
1022
|
-
resetConnection(message: string): void {
|
|
1023
|
-
if (!this.isAndroid()) return;
|
|
1024
|
-
nativeGrpc().resetConnection(message);
|
|
1025
|
-
}
|
|
1026
|
-
setUiLogEnabled(enable: boolean): void {
|
|
1027
|
-
if (!this.isAndroid()) return;
|
|
1028
|
-
nativeGrpc().setUiLogEnabled(enable);
|
|
1029
|
-
}
|
|
1030
|
-
|
|
1031
|
-
onConnectionStateChange(): void {
|
|
1032
|
-
if (!this.isAndroid()) return;
|
|
1033
|
-
nativeGrpc().onConnectionStateChange();
|
|
1034
|
-
}
|
|
1035
|
-
|
|
1036
|
-
enterIdle(): void {
|
|
1037
|
-
if (!this.isAndroid()) return;
|
|
1038
|
-
nativeGrpc().enterIdle();
|
|
1039
|
-
}
|
|
1040
|
-
|
|
1041
|
-
private isAndroid(): Boolean {
|
|
1042
|
-
return Platform.OS === 'android';
|
|
1043
|
-
}
|
|
1044
|
-
}
|
|
1045
|
-
|
|
1046
|
-
export { Grpc } from './native';
|
|
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
|
+
combineInterceptors,
|
|
9
|
+
GrpcInterceptor,
|
|
10
|
+
runOnError,
|
|
11
|
+
runOnHeaders,
|
|
12
|
+
runOnMessage,
|
|
13
|
+
runOnSendMessage,
|
|
14
|
+
runOnStart,
|
|
15
|
+
runOnTrailers,
|
|
16
|
+
toGrpcError,
|
|
17
|
+
} from './interceptors';
|
|
18
|
+
import { GrpcRequestObject, nativeGrpc } from './native';
|
|
19
|
+
import {
|
|
20
|
+
GrpcServerStreamingCall,
|
|
21
|
+
ServerOutputStream,
|
|
22
|
+
} from './server-streaming';
|
|
23
|
+
import {
|
|
24
|
+
computeRetryBackoffMs,
|
|
25
|
+
delay,
|
|
26
|
+
EffectiveCallPolicy,
|
|
27
|
+
GrpcHedgingPolicy,
|
|
28
|
+
GrpcRetryPolicy,
|
|
29
|
+
normalizeHedgingPolicy,
|
|
30
|
+
normalizeRetryPolicy,
|
|
31
|
+
remainingDeadlineSeconds,
|
|
32
|
+
resolveEffectivePolicy,
|
|
33
|
+
} from './retry';
|
|
34
|
+
import { GrpcStatusCode } from './status';
|
|
35
|
+
import {
|
|
36
|
+
DEFAULT_CHANNEL_ID,
|
|
37
|
+
GrpcCallOptions,
|
|
38
|
+
GrpcMetadata,
|
|
39
|
+
GrpcTlsOptions,
|
|
40
|
+
} from './types';
|
|
41
|
+
import { GrpcUnaryCall } from './unary';
|
|
42
|
+
|
|
43
|
+
type GrpcEventType = 'response' | 'error' | 'headers' | 'trailers';
|
|
44
|
+
|
|
45
|
+
/* prettier-ignore */
|
|
46
|
+
type GrpcEventPayload =
|
|
47
|
+
{
|
|
48
|
+
type: 'response';
|
|
49
|
+
payload: string;
|
|
50
|
+
} | {
|
|
51
|
+
type: 'error';
|
|
52
|
+
error: string;
|
|
53
|
+
code?: number;
|
|
54
|
+
trailers?: GrpcMetadata;
|
|
55
|
+
} | {
|
|
56
|
+
type: 'headers';
|
|
57
|
+
payload: GrpcMetadata;
|
|
58
|
+
} | {
|
|
59
|
+
type: 'trailers';
|
|
60
|
+
payload: GrpcMetadata;
|
|
61
|
+
} | {
|
|
62
|
+
type: 'status';
|
|
63
|
+
payload: number;
|
|
64
|
+
};
|
|
65
|
+
|
|
66
|
+
type GrpcEvent = {
|
|
67
|
+
id: number;
|
|
68
|
+
type: GrpcEventType;
|
|
69
|
+
} & GrpcEventPayload;
|
|
70
|
+
|
|
71
|
+
const Emitter = new NativeEventEmitter(NativeModules.Grpc);
|
|
72
|
+
|
|
73
|
+
type Deferred<T> = {
|
|
74
|
+
promise: Promise<T>;
|
|
75
|
+
resolve: (value: T) => void;
|
|
76
|
+
reject: (reason: any) => void;
|
|
77
|
+
};
|
|
78
|
+
|
|
79
|
+
type DeferredCalls = {
|
|
80
|
+
headers?: Deferred<GrpcMetadata>;
|
|
81
|
+
response?: Deferred<Uint8Array>;
|
|
82
|
+
trailers?: Deferred<GrpcMetadata>;
|
|
83
|
+
data?: ServerOutputStream;
|
|
84
|
+
method: string;
|
|
85
|
+
interceptors: GrpcInterceptor[];
|
|
86
|
+
};
|
|
87
|
+
|
|
88
|
+
type DeferredCallMap = {
|
|
89
|
+
[id: number]: DeferredCalls;
|
|
90
|
+
};
|
|
91
|
+
|
|
92
|
+
function createDeferred<T>(signal: AbortSignal) {
|
|
93
|
+
let completed = false;
|
|
94
|
+
|
|
95
|
+
const deferred: Deferred<T> = {} as any;
|
|
96
|
+
|
|
97
|
+
deferred.promise = new Promise<T>((resolve, reject) => {
|
|
98
|
+
deferred.resolve = (value) => {
|
|
99
|
+
completed = true;
|
|
100
|
+
|
|
101
|
+
resolve(value);
|
|
102
|
+
};
|
|
103
|
+
deferred.reject = (reason) => {
|
|
104
|
+
completed = true;
|
|
105
|
+
|
|
106
|
+
reject(reason);
|
|
107
|
+
};
|
|
108
|
+
});
|
|
109
|
+
|
|
110
|
+
signal.addEventListener('abort', () => {
|
|
111
|
+
if (!completed) {
|
|
112
|
+
deferred.reject('aborted');
|
|
113
|
+
}
|
|
114
|
+
});
|
|
115
|
+
|
|
116
|
+
return deferred;
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
let idCtr = 1;
|
|
120
|
+
|
|
121
|
+
const deferredMap: DeferredCallMap = {};
|
|
122
|
+
|
|
123
|
+
function rejectDeferredCall(deferred: DeferredCalls, error: GrpcError) {
|
|
124
|
+
deferred.headers?.reject(error);
|
|
125
|
+
deferred.trailers?.reject(error);
|
|
126
|
+
deferred.response?.reject(error);
|
|
127
|
+
deferred.data?.noitfyError(error);
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
function handleGrpcEvent(event: GrpcEvent) {
|
|
131
|
+
const deferred = deferredMap[event.id];
|
|
132
|
+
|
|
133
|
+
if (deferred) {
|
|
134
|
+
const { method, interceptors } = deferred;
|
|
135
|
+
switch (event.type) {
|
|
136
|
+
case 'headers':
|
|
137
|
+
deferred.headers?.resolve(
|
|
138
|
+
runOnHeaders(interceptors, event.payload, method)
|
|
139
|
+
);
|
|
140
|
+
break;
|
|
141
|
+
case 'response': {
|
|
142
|
+
const data = runOnMessage(
|
|
143
|
+
interceptors,
|
|
144
|
+
toByteArray(event.payload),
|
|
145
|
+
method
|
|
146
|
+
);
|
|
147
|
+
|
|
148
|
+
deferred.data?.notifyData(data);
|
|
149
|
+
deferred.response?.resolve(data);
|
|
150
|
+
break;
|
|
151
|
+
}
|
|
152
|
+
case 'trailers':
|
|
153
|
+
deferred.trailers?.resolve(
|
|
154
|
+
runOnTrailers(interceptors, event.payload, method)
|
|
155
|
+
);
|
|
156
|
+
deferred.data?.notifyComplete();
|
|
157
|
+
|
|
158
|
+
delete deferredMap[event.id];
|
|
159
|
+
break;
|
|
160
|
+
case 'error': {
|
|
161
|
+
const error = runOnError(
|
|
162
|
+
interceptors,
|
|
163
|
+
new GrpcError(event.error, event.code, event.trailers),
|
|
164
|
+
method
|
|
165
|
+
);
|
|
166
|
+
|
|
167
|
+
rejectDeferredCall(deferred, error);
|
|
168
|
+
|
|
169
|
+
delete deferredMap[event.id];
|
|
170
|
+
break;
|
|
171
|
+
}
|
|
172
|
+
}
|
|
173
|
+
}
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
function getId(): number {
|
|
177
|
+
return idCtr++;
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
function buildRequestObject(
|
|
181
|
+
data: Uint8Array,
|
|
182
|
+
options?: GrpcCallOptions
|
|
183
|
+
): GrpcRequestObject {
|
|
184
|
+
const obj: GrpcRequestObject = {
|
|
185
|
+
data: fromByteArray(data),
|
|
186
|
+
};
|
|
187
|
+
if (options?.deadlineSeconds !== undefined) {
|
|
188
|
+
obj.deadlineSeconds = options.deadlineSeconds;
|
|
189
|
+
}
|
|
190
|
+
return obj;
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
export function normalizeTlsOptions(options: GrpcTlsOptions = {}): {
|
|
194
|
+
rootCertsPem: string | null;
|
|
195
|
+
certificateChainPem: string | null;
|
|
196
|
+
privateKeyPem: string | null;
|
|
197
|
+
hostNameOverride: string | null;
|
|
198
|
+
spkiSha256Pins: string[] | null;
|
|
199
|
+
} {
|
|
200
|
+
const rootCertsPem = options.rootCertsPem ?? null;
|
|
201
|
+
const certificateChainPem = options.certificateChainPem ?? null;
|
|
202
|
+
const privateKeyPem = options.privateKeyPem ?? null;
|
|
203
|
+
const hostNameOverride = options.hostNameOverride ?? null;
|
|
204
|
+
const spkiSha256Pins =
|
|
205
|
+
options.spkiSha256Pins && options.spkiSha256Pins.length > 0
|
|
206
|
+
? options.spkiSha256Pins
|
|
207
|
+
: null;
|
|
208
|
+
|
|
209
|
+
const hasCert = !!certificateChainPem;
|
|
210
|
+
const hasKey = !!privateKeyPem;
|
|
211
|
+
if (hasCert !== hasKey) {
|
|
212
|
+
throw new Error('mTLS requires both certificateChainPem and privateKeyPem');
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
return {
|
|
216
|
+
rootCertsPem,
|
|
217
|
+
certificateChainPem,
|
|
218
|
+
privateKeyPem,
|
|
219
|
+
hostNameOverride,
|
|
220
|
+
spkiSha256Pins,
|
|
221
|
+
};
|
|
222
|
+
}
|
|
223
|
+
|
|
224
|
+
/**
|
|
225
|
+
* Shared RPC surface bound to a native channel id.
|
|
226
|
+
* Used by the singleton {@link GrpcClient} and by {@link GrpcChannel}.
|
|
227
|
+
*/
|
|
228
|
+
export class GrpcCallApi {
|
|
229
|
+
protected channelInterceptors: GrpcInterceptor[];
|
|
230
|
+
protected channelDeadlineSeconds = 120;
|
|
231
|
+
protected channelRetry: GrpcRetryPolicy | null = null;
|
|
232
|
+
protected channelHedging: GrpcHedgingPolicy | null = null;
|
|
233
|
+
|
|
234
|
+
constructor(
|
|
235
|
+
protected readonly channelId: string,
|
|
236
|
+
channelInterceptors: readonly GrpcInterceptor[] = [],
|
|
237
|
+
channelDeadlineSeconds = 120,
|
|
238
|
+
channelRetry: GrpcRetryPolicy | null = null,
|
|
239
|
+
channelHedging: GrpcHedgingPolicy | null = null
|
|
240
|
+
) {
|
|
241
|
+
this.channelInterceptors = [...channelInterceptors];
|
|
242
|
+
this.channelDeadlineSeconds = channelDeadlineSeconds;
|
|
243
|
+
this.channelRetry = channelRetry;
|
|
244
|
+
this.channelHedging = channelHedging;
|
|
245
|
+
}
|
|
246
|
+
|
|
247
|
+
protected resolveInterceptors(options?: GrpcCallOptions): GrpcInterceptor[] {
|
|
248
|
+
return combineInterceptors(this.channelInterceptors, options?.interceptors);
|
|
249
|
+
}
|
|
250
|
+
|
|
251
|
+
protected resolvePolicy(options?: GrpcCallOptions): EffectiveCallPolicy {
|
|
252
|
+
return resolveEffectivePolicy(
|
|
253
|
+
this.channelRetry,
|
|
254
|
+
this.channelHedging,
|
|
255
|
+
options?.retry,
|
|
256
|
+
options?.hedging
|
|
257
|
+
);
|
|
258
|
+
}
|
|
259
|
+
|
|
260
|
+
protected resolveDeadlineBudgetSeconds(
|
|
261
|
+
options?: GrpcCallOptions
|
|
262
|
+
): number | undefined {
|
|
263
|
+
if (options?.deadlineSeconds !== undefined) {
|
|
264
|
+
return options.deadlineSeconds;
|
|
265
|
+
}
|
|
266
|
+
return this.channelDeadlineSeconds;
|
|
267
|
+
}
|
|
268
|
+
|
|
269
|
+
private async runUnaryAttempt(
|
|
270
|
+
method: string,
|
|
271
|
+
data: Uint8Array,
|
|
272
|
+
requestHeaders: GrpcMetadata,
|
|
273
|
+
options: GrpcCallOptions | undefined,
|
|
274
|
+
interceptors: GrpcInterceptor[],
|
|
275
|
+
parentSignal: AbortSignal,
|
|
276
|
+
attemptIds: Set<number>
|
|
277
|
+
): Promise<{
|
|
278
|
+
headers: GrpcMetadata;
|
|
279
|
+
response: Uint8Array;
|
|
280
|
+
trailers: GrpcMetadata;
|
|
281
|
+
}> {
|
|
282
|
+
if (parentSignal.aborted) {
|
|
283
|
+
throw new GrpcError('aborted', GrpcStatusCode.CANCELLED);
|
|
284
|
+
}
|
|
285
|
+
|
|
286
|
+
const id = getId();
|
|
287
|
+
attemptIds.add(id);
|
|
288
|
+
const attemptAbort = new AbortController();
|
|
289
|
+
const onParentAbort = () => attemptAbort.abort();
|
|
290
|
+
parentSignal.addEventListener('abort', onParentAbort);
|
|
291
|
+
|
|
292
|
+
const response = createDeferred<Uint8Array>(attemptAbort.signal);
|
|
293
|
+
const headers = createDeferred<GrpcMetadata>(attemptAbort.signal);
|
|
294
|
+
const trailers = createDeferred<GrpcMetadata>(attemptAbort.signal);
|
|
295
|
+
|
|
296
|
+
deferredMap[id] = {
|
|
297
|
+
response,
|
|
298
|
+
headers,
|
|
299
|
+
trailers,
|
|
300
|
+
method,
|
|
301
|
+
interceptors,
|
|
302
|
+
};
|
|
303
|
+
|
|
304
|
+
try {
|
|
305
|
+
let start = await runOnStart(interceptors, {
|
|
306
|
+
method,
|
|
307
|
+
headers: { ...requestHeaders },
|
|
308
|
+
options: { ...(options || {}) },
|
|
309
|
+
request: data,
|
|
310
|
+
});
|
|
311
|
+
const callOptions = { ...start.options };
|
|
312
|
+
delete callOptions.interceptors;
|
|
313
|
+
delete callOptions.retry;
|
|
314
|
+
delete callOptions.hedging;
|
|
315
|
+
start = {
|
|
316
|
+
...start,
|
|
317
|
+
options: callOptions,
|
|
318
|
+
};
|
|
319
|
+
const requestBytes = await runOnSendMessage(
|
|
320
|
+
interceptors,
|
|
321
|
+
start.request ?? data,
|
|
322
|
+
start.method
|
|
323
|
+
);
|
|
324
|
+
const obj = buildRequestObject(requestBytes, start.options);
|
|
325
|
+
await nativeGrpc().unaryCall(
|
|
326
|
+
id,
|
|
327
|
+
start.method,
|
|
328
|
+
obj,
|
|
329
|
+
start.headers,
|
|
330
|
+
this.channelId
|
|
331
|
+
);
|
|
332
|
+
|
|
333
|
+
const [hdrs, body, trl] = await Promise.all([
|
|
334
|
+
headers.promise,
|
|
335
|
+
response.promise,
|
|
336
|
+
trailers.promise,
|
|
337
|
+
]);
|
|
338
|
+
return { headers: hdrs, response: body, trailers: trl };
|
|
339
|
+
} catch (reason) {
|
|
340
|
+
if (attemptAbort.signal.aborted || parentSignal.aborted) {
|
|
341
|
+
throw new GrpcError('aborted', GrpcStatusCode.CANCELLED);
|
|
342
|
+
}
|
|
343
|
+
throw toGrpcError(reason);
|
|
344
|
+
} finally {
|
|
345
|
+
parentSignal.removeEventListener('abort', onParentAbort);
|
|
346
|
+
attemptIds.delete(id);
|
|
347
|
+
if (attemptAbort.signal.aborted) {
|
|
348
|
+
nativeGrpc().cancelGrpcCall(id);
|
|
349
|
+
}
|
|
350
|
+
}
|
|
351
|
+
}
|
|
352
|
+
|
|
353
|
+
private async runUnaryWithPolicy(
|
|
354
|
+
method: string,
|
|
355
|
+
data: Uint8Array,
|
|
356
|
+
requestHeaders: GrpcMetadata,
|
|
357
|
+
options: GrpcCallOptions | undefined,
|
|
358
|
+
interceptors: GrpcInterceptor[],
|
|
359
|
+
parentSignal: AbortSignal,
|
|
360
|
+
policy: EffectiveCallPolicy,
|
|
361
|
+
deadlineBudgetSeconds: number | undefined
|
|
362
|
+
): Promise<{
|
|
363
|
+
headers: GrpcMetadata;
|
|
364
|
+
response: Uint8Array;
|
|
365
|
+
trailers: GrpcMetadata;
|
|
366
|
+
}> {
|
|
367
|
+
const startedAt = Date.now();
|
|
368
|
+
const attemptIds = new Set<number>();
|
|
369
|
+
|
|
370
|
+
const withRemainingDeadline = (): GrpcCallOptions => {
|
|
371
|
+
const next: GrpcCallOptions = { ...(options || {}) };
|
|
372
|
+
delete next.interceptors;
|
|
373
|
+
delete next.retry;
|
|
374
|
+
delete next.hedging;
|
|
375
|
+
if (policy.kind === 'none') {
|
|
376
|
+
if (options?.deadlineSeconds === undefined) {
|
|
377
|
+
delete next.deadlineSeconds;
|
|
378
|
+
}
|
|
379
|
+
return next;
|
|
380
|
+
}
|
|
381
|
+
const remaining = remainingDeadlineSeconds(
|
|
382
|
+
deadlineBudgetSeconds,
|
|
383
|
+
startedAt
|
|
384
|
+
);
|
|
385
|
+
if (remaining !== undefined) {
|
|
386
|
+
next.deadlineSeconds = remaining;
|
|
387
|
+
}
|
|
388
|
+
return next;
|
|
389
|
+
};
|
|
390
|
+
|
|
391
|
+
const cancelAllAttempts = () => {
|
|
392
|
+
for (const id of [...attemptIds]) {
|
|
393
|
+
nativeGrpc().cancelGrpcCall(id);
|
|
394
|
+
}
|
|
395
|
+
};
|
|
396
|
+
const onAbort = () => cancelAllAttempts();
|
|
397
|
+
parentSignal.addEventListener('abort', onAbort);
|
|
398
|
+
|
|
399
|
+
try {
|
|
400
|
+
if (policy.kind === 'none') {
|
|
401
|
+
return await this.runUnaryAttempt(
|
|
402
|
+
method,
|
|
403
|
+
data,
|
|
404
|
+
requestHeaders,
|
|
405
|
+
withRemainingDeadline(),
|
|
406
|
+
interceptors,
|
|
407
|
+
parentSignal,
|
|
408
|
+
attemptIds
|
|
409
|
+
);
|
|
410
|
+
}
|
|
411
|
+
|
|
412
|
+
if (policy.kind === 'retry') {
|
|
413
|
+
let lastError: GrpcError | undefined;
|
|
414
|
+
for (let attempt = 1; attempt <= policy.policy.maxAttempts; attempt++) {
|
|
415
|
+
if (parentSignal.aborted) {
|
|
416
|
+
throw new GrpcError('aborted', GrpcStatusCode.CANCELLED);
|
|
417
|
+
}
|
|
418
|
+
const remaining = remainingDeadlineSeconds(
|
|
419
|
+
deadlineBudgetSeconds,
|
|
420
|
+
startedAt
|
|
421
|
+
);
|
|
422
|
+
if (
|
|
423
|
+
deadlineBudgetSeconds !== undefined &&
|
|
424
|
+
deadlineBudgetSeconds > 0 &&
|
|
425
|
+
remaining !== undefined &&
|
|
426
|
+
remaining <= 0
|
|
427
|
+
) {
|
|
428
|
+
throw new GrpcError(
|
|
429
|
+
'DEADLINE_EXCEEDED',
|
|
430
|
+
GrpcStatusCode.DEADLINE_EXCEEDED
|
|
431
|
+
);
|
|
432
|
+
}
|
|
433
|
+
try {
|
|
434
|
+
return await this.runUnaryAttempt(
|
|
435
|
+
method,
|
|
436
|
+
data,
|
|
437
|
+
requestHeaders,
|
|
438
|
+
withRemainingDeadline(),
|
|
439
|
+
interceptors,
|
|
440
|
+
parentSignal,
|
|
441
|
+
attemptIds
|
|
442
|
+
);
|
|
443
|
+
} catch (reason) {
|
|
444
|
+
const error = toGrpcError(reason);
|
|
445
|
+
lastError = error;
|
|
446
|
+
if (
|
|
447
|
+
error.code === GrpcStatusCode.CANCELLED ||
|
|
448
|
+
parentSignal.aborted
|
|
449
|
+
) {
|
|
450
|
+
throw error;
|
|
451
|
+
}
|
|
452
|
+
const retryable =
|
|
453
|
+
error.code !== undefined &&
|
|
454
|
+
policy.policy.retryableStatusCodes.has(error.code);
|
|
455
|
+
if (!retryable || attempt >= policy.policy.maxAttempts) {
|
|
456
|
+
throw error;
|
|
457
|
+
}
|
|
458
|
+
const backoffMs = computeRetryBackoffMs(policy.policy, attempt);
|
|
459
|
+
await delay(backoffMs, parentSignal as any);
|
|
460
|
+
}
|
|
461
|
+
}
|
|
462
|
+
throw (
|
|
463
|
+
lastError ?? new GrpcError('UNAVAILABLE', GrpcStatusCode.UNAVAILABLE)
|
|
464
|
+
);
|
|
465
|
+
}
|
|
466
|
+
|
|
467
|
+
// hedging
|
|
468
|
+
type AttemptOutcome =
|
|
469
|
+
| {
|
|
470
|
+
ok: true;
|
|
471
|
+
value: {
|
|
472
|
+
headers: GrpcMetadata;
|
|
473
|
+
response: Uint8Array;
|
|
474
|
+
trailers: GrpcMetadata;
|
|
475
|
+
};
|
|
476
|
+
}
|
|
477
|
+
| { ok: false; error: GrpcError };
|
|
478
|
+
|
|
479
|
+
return await new Promise((resolve, reject) => {
|
|
480
|
+
let settled = false;
|
|
481
|
+
let started = 0;
|
|
482
|
+
let finished = 0;
|
|
483
|
+
const timers: ReturnType<typeof setTimeout>[] = [];
|
|
484
|
+
const inFlight = new Map<number, Promise<AttemptOutcome>>();
|
|
485
|
+
|
|
486
|
+
const finishSuccess = (value: {
|
|
487
|
+
headers: GrpcMetadata;
|
|
488
|
+
response: Uint8Array;
|
|
489
|
+
trailers: GrpcMetadata;
|
|
490
|
+
}) => {
|
|
491
|
+
if (settled) {
|
|
492
|
+
return;
|
|
493
|
+
}
|
|
494
|
+
settled = true;
|
|
495
|
+
timers.forEach(clearTimeout);
|
|
496
|
+
cancelAllAttempts();
|
|
497
|
+
resolve(value);
|
|
498
|
+
};
|
|
499
|
+
|
|
500
|
+
const finishError = (error: GrpcError) => {
|
|
501
|
+
if (settled) {
|
|
502
|
+
return;
|
|
503
|
+
}
|
|
504
|
+
settled = true;
|
|
505
|
+
timers.forEach(clearTimeout);
|
|
506
|
+
cancelAllAttempts();
|
|
507
|
+
reject(error);
|
|
508
|
+
};
|
|
509
|
+
|
|
510
|
+
const launch = () => {
|
|
511
|
+
if (settled || parentSignal.aborted) {
|
|
512
|
+
return;
|
|
513
|
+
}
|
|
514
|
+
const remaining = remainingDeadlineSeconds(
|
|
515
|
+
deadlineBudgetSeconds,
|
|
516
|
+
startedAt
|
|
517
|
+
);
|
|
518
|
+
if (
|
|
519
|
+
deadlineBudgetSeconds !== undefined &&
|
|
520
|
+
deadlineBudgetSeconds > 0 &&
|
|
521
|
+
remaining !== undefined &&
|
|
522
|
+
remaining <= 0
|
|
523
|
+
) {
|
|
524
|
+
finishError(
|
|
525
|
+
new GrpcError(
|
|
526
|
+
'DEADLINE_EXCEEDED',
|
|
527
|
+
GrpcStatusCode.DEADLINE_EXCEEDED
|
|
528
|
+
)
|
|
529
|
+
);
|
|
530
|
+
return;
|
|
531
|
+
}
|
|
532
|
+
|
|
533
|
+
started += 1;
|
|
534
|
+
const launchIndex = started;
|
|
535
|
+
const p = this.runUnaryAttempt(
|
|
536
|
+
method,
|
|
537
|
+
data,
|
|
538
|
+
requestHeaders,
|
|
539
|
+
withRemainingDeadline(),
|
|
540
|
+
interceptors,
|
|
541
|
+
parentSignal,
|
|
542
|
+
attemptIds
|
|
543
|
+
)
|
|
544
|
+
.then(
|
|
545
|
+
(value): AttemptOutcome => ({ ok: true, value }),
|
|
546
|
+
(reason): AttemptOutcome => ({
|
|
547
|
+
ok: false,
|
|
548
|
+
error: toGrpcError(reason),
|
|
549
|
+
})
|
|
550
|
+
)
|
|
551
|
+
.then((outcome) => {
|
|
552
|
+
inFlight.delete(launchIndex);
|
|
553
|
+
finished += 1;
|
|
554
|
+
if (settled) {
|
|
555
|
+
return outcome;
|
|
556
|
+
}
|
|
557
|
+
if (outcome.ok) {
|
|
558
|
+
finishSuccess(outcome.value);
|
|
559
|
+
return outcome;
|
|
560
|
+
}
|
|
561
|
+
const { error } = outcome;
|
|
562
|
+
if (
|
|
563
|
+
error.code === GrpcStatusCode.CANCELLED ||
|
|
564
|
+
parentSignal.aborted
|
|
565
|
+
) {
|
|
566
|
+
if (finished >= started && inFlight.size === 0) {
|
|
567
|
+
finishError(error);
|
|
568
|
+
}
|
|
569
|
+
return outcome;
|
|
570
|
+
}
|
|
571
|
+
const nonFatal =
|
|
572
|
+
error.code !== undefined &&
|
|
573
|
+
policy.policy.nonFatalStatusCodes.has(error.code);
|
|
574
|
+
if (!nonFatal) {
|
|
575
|
+
finishError(error);
|
|
576
|
+
return outcome;
|
|
577
|
+
}
|
|
578
|
+
if (
|
|
579
|
+
finished >= policy.policy.maxAttempts ||
|
|
580
|
+
(started >= policy.policy.maxAttempts && inFlight.size === 0)
|
|
581
|
+
) {
|
|
582
|
+
finishError(error);
|
|
583
|
+
}
|
|
584
|
+
return outcome;
|
|
585
|
+
});
|
|
586
|
+
inFlight.set(launchIndex, p);
|
|
587
|
+
};
|
|
588
|
+
|
|
589
|
+
launch();
|
|
590
|
+
for (let i = 2; i <= policy.policy.maxAttempts; i++) {
|
|
591
|
+
const delayMs = policy.policy.hedgingDelayMs * (i - 1);
|
|
592
|
+
if (delayMs <= 0) {
|
|
593
|
+
launch();
|
|
594
|
+
} else {
|
|
595
|
+
timers.push(setTimeout(launch, delayMs));
|
|
596
|
+
}
|
|
597
|
+
}
|
|
598
|
+
|
|
599
|
+
parentSignal.addEventListener('abort', () => {
|
|
600
|
+
finishError(new GrpcError('aborted', GrpcStatusCode.CANCELLED));
|
|
601
|
+
});
|
|
602
|
+
});
|
|
603
|
+
} finally {
|
|
604
|
+
parentSignal.removeEventListener('abort', onAbort);
|
|
605
|
+
}
|
|
606
|
+
}
|
|
607
|
+
|
|
608
|
+
unaryCall(
|
|
609
|
+
method: string,
|
|
610
|
+
data: Uint8Array,
|
|
611
|
+
requestHeaders?: GrpcMetadata,
|
|
612
|
+
options?: GrpcCallOptions
|
|
613
|
+
): GrpcUnaryCall {
|
|
614
|
+
const abort = new AbortController();
|
|
615
|
+
const interceptors = this.resolveInterceptors(options);
|
|
616
|
+
const policy = this.resolvePolicy(options);
|
|
617
|
+
const deadlineBudgetSeconds = this.resolveDeadlineBudgetSeconds(options);
|
|
618
|
+
|
|
619
|
+
const response = createDeferred<Uint8Array>(abort.signal);
|
|
620
|
+
const headers = createDeferred<GrpcMetadata>(abort.signal);
|
|
621
|
+
const trailers = createDeferred<GrpcMetadata>(abort.signal);
|
|
622
|
+
|
|
623
|
+
const call = new GrpcUnaryCall(
|
|
624
|
+
method,
|
|
625
|
+
data,
|
|
626
|
+
requestHeaders || {},
|
|
627
|
+
headers.promise,
|
|
628
|
+
response.promise,
|
|
629
|
+
trailers.promise,
|
|
630
|
+
abort
|
|
631
|
+
);
|
|
632
|
+
|
|
633
|
+
call.then(
|
|
634
|
+
(result) => result,
|
|
635
|
+
() => abort.abort()
|
|
636
|
+
);
|
|
637
|
+
|
|
638
|
+
Promise.resolve()
|
|
639
|
+
.then(async () => {
|
|
640
|
+
const result = await this.runUnaryWithPolicy(
|
|
641
|
+
method,
|
|
642
|
+
data,
|
|
643
|
+
requestHeaders || {},
|
|
644
|
+
options,
|
|
645
|
+
interceptors,
|
|
646
|
+
abort.signal,
|
|
647
|
+
policy,
|
|
648
|
+
deadlineBudgetSeconds
|
|
649
|
+
);
|
|
650
|
+
headers.resolve(result.headers);
|
|
651
|
+
response.resolve(result.response);
|
|
652
|
+
trailers.resolve(result.trailers);
|
|
653
|
+
})
|
|
654
|
+
.catch((reason) => {
|
|
655
|
+
if (abort.signal.aborted) {
|
|
656
|
+
const error = new GrpcError('aborted', GrpcStatusCode.CANCELLED);
|
|
657
|
+
headers.reject(error);
|
|
658
|
+
response.reject(error);
|
|
659
|
+
trailers.reject(error);
|
|
660
|
+
return;
|
|
661
|
+
}
|
|
662
|
+
const error = runOnError(interceptors, toGrpcError(reason), method);
|
|
663
|
+
headers.reject(error);
|
|
664
|
+
response.reject(error);
|
|
665
|
+
trailers.reject(error);
|
|
666
|
+
});
|
|
667
|
+
|
|
668
|
+
return call;
|
|
669
|
+
}
|
|
670
|
+
|
|
671
|
+
serverStreamCall(
|
|
672
|
+
method: string,
|
|
673
|
+
data: Uint8Array,
|
|
674
|
+
requestHeaders?: GrpcMetadata,
|
|
675
|
+
options?: GrpcCallOptions
|
|
676
|
+
): GrpcServerStreamingCall {
|
|
677
|
+
const id = getId();
|
|
678
|
+
const abort = new AbortController();
|
|
679
|
+
const interceptors = this.resolveInterceptors(options);
|
|
680
|
+
|
|
681
|
+
abort.signal.addEventListener('abort', () => {
|
|
682
|
+
nativeGrpc().cancelGrpcCall(id);
|
|
683
|
+
});
|
|
684
|
+
|
|
685
|
+
const headers = createDeferred<GrpcMetadata>(abort.signal);
|
|
686
|
+
const trailers = createDeferred<GrpcMetadata>(abort.signal);
|
|
687
|
+
|
|
688
|
+
const stream = new ServerOutputStream();
|
|
689
|
+
|
|
690
|
+
deferredMap[id] = {
|
|
691
|
+
headers,
|
|
692
|
+
trailers,
|
|
693
|
+
data: stream,
|
|
694
|
+
method,
|
|
695
|
+
interceptors,
|
|
696
|
+
};
|
|
697
|
+
|
|
698
|
+
const call = new GrpcServerStreamingCall(
|
|
699
|
+
method,
|
|
700
|
+
data,
|
|
701
|
+
requestHeaders || {},
|
|
702
|
+
headers.promise,
|
|
703
|
+
stream,
|
|
704
|
+
trailers.promise,
|
|
705
|
+
abort
|
|
706
|
+
);
|
|
707
|
+
|
|
708
|
+
call.then(
|
|
709
|
+
(result) => result,
|
|
710
|
+
() => abort.abort()
|
|
711
|
+
);
|
|
712
|
+
|
|
713
|
+
Promise.resolve()
|
|
714
|
+
.then(async () => {
|
|
715
|
+
let start = await runOnStart(interceptors, {
|
|
716
|
+
method,
|
|
717
|
+
headers: { ...(requestHeaders || {}) },
|
|
718
|
+
options: { ...(options || {}) },
|
|
719
|
+
request: data,
|
|
720
|
+
});
|
|
721
|
+
const callOptions = { ...start.options };
|
|
722
|
+
delete callOptions.interceptors;
|
|
723
|
+
start = {
|
|
724
|
+
...start,
|
|
725
|
+
options: callOptions,
|
|
726
|
+
};
|
|
727
|
+
const requestBytes = await runOnSendMessage(
|
|
728
|
+
interceptors,
|
|
729
|
+
start.request ?? data,
|
|
730
|
+
start.method
|
|
731
|
+
);
|
|
732
|
+
const obj = buildRequestObject(requestBytes, start.options);
|
|
733
|
+
await nativeGrpc().serverStreamingCall(
|
|
734
|
+
id,
|
|
735
|
+
start.method,
|
|
736
|
+
obj,
|
|
737
|
+
start.headers,
|
|
738
|
+
this.channelId
|
|
739
|
+
);
|
|
740
|
+
})
|
|
741
|
+
.catch((reason) => {
|
|
742
|
+
const deferred = deferredMap[id];
|
|
743
|
+
if (!deferred) {
|
|
744
|
+
return;
|
|
745
|
+
}
|
|
746
|
+
const error = runOnError(interceptors, toGrpcError(reason), method);
|
|
747
|
+
rejectDeferredCall(deferred, error);
|
|
748
|
+
delete deferredMap[id];
|
|
749
|
+
});
|
|
750
|
+
|
|
751
|
+
return call;
|
|
752
|
+
}
|
|
753
|
+
|
|
754
|
+
/**
|
|
755
|
+
* Client-streaming RPC: many request messages, one response.
|
|
756
|
+
* Native call starts on the first `requests.send(...)`.
|
|
757
|
+
*/
|
|
758
|
+
clientStreamCall(
|
|
759
|
+
method: string,
|
|
760
|
+
requestHeaders?: GrpcMetadata,
|
|
761
|
+
options?: GrpcCallOptions
|
|
762
|
+
): GrpcClientStreamingCall {
|
|
763
|
+
const id = getId();
|
|
764
|
+
const abort = new AbortController();
|
|
765
|
+
const interceptors = this.resolveInterceptors(options);
|
|
766
|
+
let headersMeta = { ...(requestHeaders || {}) };
|
|
767
|
+
let callOptions: GrpcCallOptions = { ...(options || {}) };
|
|
768
|
+
const channelId = this.channelId;
|
|
769
|
+
let started = false;
|
|
770
|
+
|
|
771
|
+
abort.signal.addEventListener('abort', () => {
|
|
772
|
+
nativeGrpc().cancelGrpcCall(id);
|
|
773
|
+
});
|
|
774
|
+
|
|
775
|
+
const response = createDeferred<Uint8Array>(abort.signal);
|
|
776
|
+
const headers = createDeferred<GrpcMetadata>(abort.signal);
|
|
777
|
+
const trailers = createDeferred<GrpcMetadata>(abort.signal);
|
|
778
|
+
|
|
779
|
+
deferredMap[id] = {
|
|
780
|
+
response,
|
|
781
|
+
headers,
|
|
782
|
+
trailers,
|
|
783
|
+
method,
|
|
784
|
+
interceptors,
|
|
785
|
+
};
|
|
786
|
+
|
|
787
|
+
const requests = new ServerInputStream(
|
|
788
|
+
async (data, isFirst) => {
|
|
789
|
+
try {
|
|
790
|
+
let message = await runOnSendMessage(interceptors, data, method);
|
|
791
|
+
if (isFirst || !started) {
|
|
792
|
+
const start = await runOnStart(interceptors, {
|
|
793
|
+
method,
|
|
794
|
+
headers: headersMeta,
|
|
795
|
+
options: callOptions,
|
|
796
|
+
request: message,
|
|
797
|
+
});
|
|
798
|
+
headersMeta = start.headers;
|
|
799
|
+
callOptions = { ...start.options };
|
|
800
|
+
delete callOptions.interceptors;
|
|
801
|
+
message = start.request ?? message;
|
|
802
|
+
started = true;
|
|
803
|
+
}
|
|
804
|
+
const obj = buildRequestObject(
|
|
805
|
+
message,
|
|
806
|
+
isFirst ? callOptions : undefined
|
|
807
|
+
);
|
|
808
|
+
return nativeGrpc().clientStreamingCall(
|
|
809
|
+
id,
|
|
810
|
+
method,
|
|
811
|
+
obj,
|
|
812
|
+
isFirst ? headersMeta : {},
|
|
813
|
+
channelId
|
|
814
|
+
);
|
|
815
|
+
} catch (reason) {
|
|
816
|
+
const deferred = deferredMap[id];
|
|
817
|
+
if (deferred) {
|
|
818
|
+
const error = runOnError(interceptors, toGrpcError(reason), method);
|
|
819
|
+
rejectDeferredCall(deferred, error);
|
|
820
|
+
delete deferredMap[id];
|
|
821
|
+
}
|
|
822
|
+
throw toGrpcError(reason);
|
|
823
|
+
}
|
|
824
|
+
},
|
|
825
|
+
() => nativeGrpc().finishClientStreaming(id)
|
|
826
|
+
);
|
|
827
|
+
|
|
828
|
+
const call = new GrpcClientStreamingCall(
|
|
829
|
+
method,
|
|
830
|
+
headersMeta,
|
|
831
|
+
requests,
|
|
832
|
+
headers.promise,
|
|
833
|
+
response.promise,
|
|
834
|
+
trailers.promise,
|
|
835
|
+
abort
|
|
836
|
+
);
|
|
837
|
+
|
|
838
|
+
call.then(
|
|
839
|
+
(result) => result,
|
|
840
|
+
() => abort.abort()
|
|
841
|
+
);
|
|
842
|
+
|
|
843
|
+
return call;
|
|
844
|
+
}
|
|
845
|
+
|
|
846
|
+
/**
|
|
847
|
+
* Bidirectional streaming RPC: interleaved request and response messages.
|
|
848
|
+
* Native call starts on the first `requests.send(...)`.
|
|
849
|
+
* Registers only the `data` stream (plus headers/trailers) — no unary `response`.
|
|
850
|
+
*/
|
|
851
|
+
bidiStreamCall(
|
|
852
|
+
method: string,
|
|
853
|
+
requestHeaders?: GrpcMetadata,
|
|
854
|
+
options?: GrpcCallOptions
|
|
855
|
+
): GrpcBidiStreamingCall {
|
|
856
|
+
const id = getId();
|
|
857
|
+
const abort = new AbortController();
|
|
858
|
+
const interceptors = this.resolveInterceptors(options);
|
|
859
|
+
let headersMeta = { ...(requestHeaders || {}) };
|
|
860
|
+
let callOptions: GrpcCallOptions = { ...(options || {}) };
|
|
861
|
+
const channelId = this.channelId;
|
|
862
|
+
let started = false;
|
|
863
|
+
|
|
864
|
+
abort.signal.addEventListener('abort', () => {
|
|
865
|
+
nativeGrpc().cancelGrpcCall(id);
|
|
866
|
+
});
|
|
867
|
+
|
|
868
|
+
const headers = createDeferred<GrpcMetadata>(abort.signal);
|
|
869
|
+
const trailers = createDeferred<GrpcMetadata>(abort.signal);
|
|
870
|
+
const stream = new ServerOutputStream();
|
|
871
|
+
|
|
872
|
+
deferredMap[id] = {
|
|
873
|
+
headers,
|
|
874
|
+
trailers,
|
|
875
|
+
data: stream,
|
|
876
|
+
method,
|
|
877
|
+
interceptors,
|
|
878
|
+
};
|
|
879
|
+
|
|
880
|
+
const requests = new ServerInputStream(
|
|
881
|
+
async (data, isFirst) => {
|
|
882
|
+
try {
|
|
883
|
+
let message = await runOnSendMessage(interceptors, data, method);
|
|
884
|
+
if (isFirst || !started) {
|
|
885
|
+
const start = await runOnStart(interceptors, {
|
|
886
|
+
method,
|
|
887
|
+
headers: headersMeta,
|
|
888
|
+
options: callOptions,
|
|
889
|
+
request: message,
|
|
890
|
+
});
|
|
891
|
+
headersMeta = start.headers;
|
|
892
|
+
callOptions = { ...start.options };
|
|
893
|
+
delete callOptions.interceptors;
|
|
894
|
+
message = start.request ?? message;
|
|
895
|
+
started = true;
|
|
896
|
+
}
|
|
897
|
+
const obj = buildRequestObject(
|
|
898
|
+
message,
|
|
899
|
+
isFirst ? callOptions : undefined
|
|
900
|
+
);
|
|
901
|
+
return nativeGrpc().bidiStreamingCall(
|
|
902
|
+
id,
|
|
903
|
+
method,
|
|
904
|
+
obj,
|
|
905
|
+
isFirst ? headersMeta : {},
|
|
906
|
+
channelId
|
|
907
|
+
);
|
|
908
|
+
} catch (reason) {
|
|
909
|
+
const deferred = deferredMap[id];
|
|
910
|
+
if (deferred) {
|
|
911
|
+
const error = runOnError(interceptors, toGrpcError(reason), method);
|
|
912
|
+
rejectDeferredCall(deferred, error);
|
|
913
|
+
delete deferredMap[id];
|
|
914
|
+
}
|
|
915
|
+
throw toGrpcError(reason);
|
|
916
|
+
}
|
|
917
|
+
},
|
|
918
|
+
() => nativeGrpc().finishClientStreaming(id)
|
|
919
|
+
);
|
|
920
|
+
|
|
921
|
+
const call = new GrpcBidiStreamingCall(
|
|
922
|
+
method,
|
|
923
|
+
headersMeta,
|
|
924
|
+
requests,
|
|
925
|
+
headers.promise,
|
|
926
|
+
stream,
|
|
927
|
+
trailers.promise,
|
|
928
|
+
abort
|
|
929
|
+
);
|
|
930
|
+
|
|
931
|
+
call.then(
|
|
932
|
+
(result) => result,
|
|
933
|
+
() => abort.abort()
|
|
934
|
+
);
|
|
935
|
+
|
|
936
|
+
return call;
|
|
937
|
+
}
|
|
938
|
+
}
|
|
939
|
+
|
|
940
|
+
export class GrpcClient extends GrpcCallApi {
|
|
941
|
+
constructor() {
|
|
942
|
+
super(DEFAULT_CHANNEL_ID, [], 120, null, null);
|
|
943
|
+
Emitter.addListener('grpc-call', handleGrpcEvent);
|
|
944
|
+
}
|
|
945
|
+
destroy() {
|
|
946
|
+
Emitter.removeAllListeners('grpc-call');
|
|
947
|
+
}
|
|
948
|
+
/**
|
|
949
|
+
* Replace default-channel interceptors (same replace semantics as `setTlsOptions`).
|
|
950
|
+
* Prefer `createChannel({ interceptors })` for multi-host apps.
|
|
951
|
+
*/
|
|
952
|
+
setInterceptors(interceptors: GrpcInterceptor[] = []): void {
|
|
953
|
+
this.channelInterceptors = [...interceptors];
|
|
954
|
+
}
|
|
955
|
+
/**
|
|
956
|
+
* Replace unary retry policy on the default channel.
|
|
957
|
+
* Clears any hedging policy (mutually exclusive). Pass `null` to disable.
|
|
958
|
+
*/
|
|
959
|
+
setRetryPolicy(policy: GrpcRetryPolicy | null): void {
|
|
960
|
+
if (policy) {
|
|
961
|
+
normalizeRetryPolicy(policy);
|
|
962
|
+
}
|
|
963
|
+
this.channelRetry = policy;
|
|
964
|
+
this.channelHedging = null;
|
|
965
|
+
}
|
|
966
|
+
/**
|
|
967
|
+
* Replace unary hedging policy on the default channel.
|
|
968
|
+
* Clears any retry policy (mutually exclusive). Pass `null` to disable.
|
|
969
|
+
*/
|
|
970
|
+
setHedgingPolicy(policy: GrpcHedgingPolicy | null): void {
|
|
971
|
+
if (policy) {
|
|
972
|
+
normalizeHedgingPolicy(policy);
|
|
973
|
+
}
|
|
974
|
+
this.channelHedging = policy;
|
|
975
|
+
this.channelRetry = null;
|
|
976
|
+
}
|
|
977
|
+
getHost(): Promise<string> {
|
|
978
|
+
return nativeGrpc().getHost();
|
|
979
|
+
}
|
|
980
|
+
setHost(host: string): void {
|
|
981
|
+
nativeGrpc().setHost(host);
|
|
982
|
+
}
|
|
983
|
+
getInsecure(): Promise<boolean> {
|
|
984
|
+
return nativeGrpc().getIsInsecure();
|
|
985
|
+
}
|
|
986
|
+
setInsecure(insecure: boolean): void {
|
|
987
|
+
nativeGrpc().setInsecure(insecure);
|
|
988
|
+
}
|
|
989
|
+
/**
|
|
990
|
+
* Replace channel TLS options (custom CA, mTLS, hostname override).
|
|
991
|
+
* Each call replaces the previous config; omitted fields are cleared.
|
|
992
|
+
* Apply before `initGrpcChannel()`. Ignored while insecure/plaintext.
|
|
993
|
+
* For multi-host apps prefer {@link createChannel} with `tls` in config.
|
|
994
|
+
*/
|
|
995
|
+
setTlsOptions(options: GrpcTlsOptions = {}): void {
|
|
996
|
+
nativeGrpc().setTlsOptions(normalizeTlsOptions(options));
|
|
997
|
+
}
|
|
998
|
+
setCompression(enable: boolean, compressorName: string): void {
|
|
999
|
+
nativeGrpc().setCompression(enable, compressorName);
|
|
1000
|
+
}
|
|
1001
|
+
setResponseSizeLimit(limitInBytes: number): void {
|
|
1002
|
+
nativeGrpc().setResponseSizeLimit(limitInBytes);
|
|
1003
|
+
}
|
|
1004
|
+
/** Global per-call deadline in seconds (Android/iOS). Default 120. Overridable per RPC via options. */
|
|
1005
|
+
setCallDeadlineSeconds(seconds: number): void {
|
|
1006
|
+
this.channelDeadlineSeconds = Math.max(0, seconds);
|
|
1007
|
+
nativeGrpc().setCallDeadlineSeconds(seconds);
|
|
1008
|
+
}
|
|
1009
|
+
|
|
1010
|
+
initGrpcChannel() {
|
|
1011
|
+
nativeGrpc().initGrpcChannel();
|
|
1012
|
+
}
|
|
1013
|
+
|
|
1014
|
+
setKeepAlive(
|
|
1015
|
+
enable: boolean,
|
|
1016
|
+
keepAliveTime: number,
|
|
1017
|
+
keepAliveTimeOut: number
|
|
1018
|
+
): void {
|
|
1019
|
+
nativeGrpc().setKeepAlive(enable, keepAliveTime, keepAliveTimeOut);
|
|
1020
|
+
}
|
|
1021
|
+
|
|
1022
|
+
resetConnection(message: string): void {
|
|
1023
|
+
if (!this.isAndroid()) return;
|
|
1024
|
+
nativeGrpc().resetConnection(message);
|
|
1025
|
+
}
|
|
1026
|
+
setUiLogEnabled(enable: boolean): void {
|
|
1027
|
+
if (!this.isAndroid()) return;
|
|
1028
|
+
nativeGrpc().setUiLogEnabled(enable);
|
|
1029
|
+
}
|
|
1030
|
+
|
|
1031
|
+
onConnectionStateChange(): void {
|
|
1032
|
+
if (!this.isAndroid()) return;
|
|
1033
|
+
nativeGrpc().onConnectionStateChange();
|
|
1034
|
+
}
|
|
1035
|
+
|
|
1036
|
+
enterIdle(): void {
|
|
1037
|
+
if (!this.isAndroid()) return;
|
|
1038
|
+
nativeGrpc().enterIdle();
|
|
1039
|
+
}
|
|
1040
|
+
|
|
1041
|
+
private isAndroid(): Boolean {
|
|
1042
|
+
return Platform.OS === 'android';
|
|
1043
|
+
}
|
|
1044
|
+
}
|
|
1045
|
+
|
|
1046
|
+
export { Grpc } from './native';
|