@xdcobra/react-native-grpc 1.0.0 → 1.1.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/README.md +176 -6
- package/android/src/main/java/com/reactnativegrpc/GrpcModule.java +233 -88
- package/ios/Grpc.h +1 -1
- package/ios/Grpc.m +210 -24
- package/lib/commonjs/channel.js +84 -0
- package/lib/commonjs/channel.js.map +1 -0
- package/lib/commonjs/client.js +531 -128
- package/lib/commonjs/client.js.map +1 -1
- package/lib/commonjs/create-client.js +206 -0
- package/lib/commonjs/create-client.js.map +1 -0
- package/lib/commonjs/index.js +32 -1
- package/lib/commonjs/index.js.map +1 -1
- package/lib/commonjs/interceptors.js +116 -0
- package/lib/commonjs/interceptors.js.map +1 -0
- package/lib/commonjs/native.js +24 -0
- package/lib/commonjs/native.js.map +1 -0
- package/lib/commonjs/retry.js +153 -0
- package/lib/commonjs/retry.js.map +1 -0
- package/lib/commonjs/status.js +54 -0
- package/lib/commonjs/status.js.map +1 -0
- package/lib/commonjs/types.js +29 -0
- package/lib/commonjs/types.js.map +1 -1
- package/lib/module/channel.js +76 -0
- package/lib/module/channel.js.map +1 -0
- package/lib/module/client.js +518 -122
- package/lib/module/client.js.map +1 -1
- package/lib/module/create-client.js +201 -0
- package/lib/module/create-client.js.map +1 -0
- package/lib/module/index.js +3 -0
- package/lib/module/index.js.map +1 -1
- package/lib/module/interceptors.js +104 -0
- package/lib/module/interceptors.js.map +1 -0
- package/lib/module/native.js +17 -0
- package/lib/module/native.js.map +1 -0
- package/lib/module/retry.js +141 -0
- package/lib/module/retry.js.map +1 -0
- package/lib/module/status.js +46 -0
- package/lib/module/status.js.map +1 -0
- package/lib/module/types.js +17 -0
- package/lib/module/types.js.map +1 -1
- package/lib/typescript/channel.d.ts +27 -0
- package/lib/typescript/channel.d.ts.map +1 -0
- package/lib/typescript/client.d.ts +56 -49
- package/lib/typescript/client.d.ts.map +1 -1
- package/lib/typescript/create-client.d.ts +65 -0
- package/lib/typescript/create-client.d.ts.map +1 -0
- package/lib/typescript/index.d.ts +5 -0
- package/lib/typescript/index.d.ts.map +1 -1
- package/lib/typescript/interceptors.d.ts +43 -0
- package/lib/typescript/interceptors.d.ts.map +1 -0
- package/lib/typescript/native.d.ts +53 -0
- package/lib/typescript/native.d.ts.map +1 -0
- package/lib/typescript/retry.d.ts +56 -0
- package/lib/typescript/retry.d.ts.map +1 -0
- package/lib/typescript/status.d.ts +24 -0
- package/lib/typescript/status.d.ts.map +1 -0
- package/lib/typescript/types.d.ts +60 -0
- package/lib/typescript/types.d.ts.map +1 -1
- package/package.json +48 -7
- package/src/channel.ts +110 -0
- package/src/client.ts +725 -200
- package/src/create-client.ts +391 -0
- package/src/index.ts +17 -0
- package/src/interceptors.ts +167 -0
- package/src/native.ts +99 -0
- package/src/retry.ts +208 -0
- package/src/status.ts +50 -0
- package/src/types.ts +64 -0
package/lib/commonjs/client.js
CHANGED
|
@@ -3,24 +3,29 @@
|
|
|
3
3
|
Object.defineProperty(exports, "__esModule", {
|
|
4
4
|
value: true
|
|
5
5
|
});
|
|
6
|
-
exports
|
|
6
|
+
Object.defineProperty(exports, "Grpc", {
|
|
7
|
+
enumerable: true,
|
|
8
|
+
get: function () {
|
|
9
|
+
return _native.Grpc;
|
|
10
|
+
}
|
|
11
|
+
});
|
|
12
|
+
exports.GrpcClient = exports.GrpcCallApi = void 0;
|
|
13
|
+
exports.normalizeTlsOptions = normalizeTlsOptions;
|
|
7
14
|
var _abortController = require("abort-controller");
|
|
8
15
|
var _base64Js = require("base64-js");
|
|
9
16
|
var _reactNative = require("react-native");
|
|
10
17
|
var _bidiStreaming = require("./bidi-streaming");
|
|
11
18
|
var _clientStreaming = require("./client-streaming");
|
|
12
19
|
var _errors = require("./errors");
|
|
20
|
+
var _interceptors = require("./interceptors");
|
|
21
|
+
var _native = require("./native");
|
|
13
22
|
var _serverStreaming = require("./server-streaming");
|
|
23
|
+
var _retry = require("./retry");
|
|
24
|
+
var _status = require("./status");
|
|
25
|
+
var _types = require("./types");
|
|
14
26
|
var _unary = require("./unary");
|
|
15
27
|
/* prettier-ignore */
|
|
16
28
|
|
|
17
|
-
function nativeGrpc() {
|
|
18
|
-
const grpc = _reactNative.NativeModules.Grpc;
|
|
19
|
-
if (!grpc) {
|
|
20
|
-
throw new Error('NativeModules.Grpc is not linked');
|
|
21
|
-
}
|
|
22
|
-
return grpc;
|
|
23
|
-
}
|
|
24
29
|
const Emitter = new _reactNative.NativeEventEmitter(_reactNative.NativeModules.Grpc);
|
|
25
30
|
function createDeferred(signal) {
|
|
26
31
|
let completed = false;
|
|
@@ -44,32 +49,45 @@ function createDeferred(signal) {
|
|
|
44
49
|
}
|
|
45
50
|
let idCtr = 1;
|
|
46
51
|
const deferredMap = {};
|
|
52
|
+
function rejectDeferredCall(deferred, error) {
|
|
53
|
+
var _deferred$headers, _deferred$trailers, _deferred$response, _deferred$data;
|
|
54
|
+
(_deferred$headers = deferred.headers) === null || _deferred$headers === void 0 || _deferred$headers.reject(error);
|
|
55
|
+
(_deferred$trailers = deferred.trailers) === null || _deferred$trailers === void 0 || _deferred$trailers.reject(error);
|
|
56
|
+
(_deferred$response = deferred.response) === null || _deferred$response === void 0 || _deferred$response.reject(error);
|
|
57
|
+
(_deferred$data = deferred.data) === null || _deferred$data === void 0 || _deferred$data.noitfyError(error);
|
|
58
|
+
}
|
|
47
59
|
function handleGrpcEvent(event) {
|
|
48
|
-
var _deferred$
|
|
60
|
+
var _deferred$headers2, _deferred$trailers2, _deferred$data3;
|
|
49
61
|
const deferred = deferredMap[event.id];
|
|
50
62
|
if (deferred) {
|
|
63
|
+
const {
|
|
64
|
+
method,
|
|
65
|
+
interceptors
|
|
66
|
+
} = deferred;
|
|
51
67
|
switch (event.type) {
|
|
52
68
|
case 'headers':
|
|
53
|
-
(_deferred$
|
|
69
|
+
(_deferred$headers2 = deferred.headers) === null || _deferred$headers2 === void 0 || _deferred$headers2.resolve((0, _interceptors.runOnHeaders)(interceptors, event.payload, method));
|
|
54
70
|
break;
|
|
55
71
|
case 'response':
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
72
|
+
{
|
|
73
|
+
var _deferred$data2, _deferred$response2;
|
|
74
|
+
const data = (0, _interceptors.runOnMessage)(interceptors, (0, _base64Js.toByteArray)(event.payload), method);
|
|
75
|
+
(_deferred$data2 = deferred.data) === null || _deferred$data2 === void 0 || _deferred$data2.notifyData(data);
|
|
76
|
+
(_deferred$response2 = deferred.response) === null || _deferred$response2 === void 0 || _deferred$response2.resolve(data);
|
|
77
|
+
break;
|
|
78
|
+
}
|
|
60
79
|
case 'trailers':
|
|
61
|
-
(_deferred$
|
|
62
|
-
(_deferred$
|
|
80
|
+
(_deferred$trailers2 = deferred.trailers) === null || _deferred$trailers2 === void 0 || _deferred$trailers2.resolve((0, _interceptors.runOnTrailers)(interceptors, event.payload, method));
|
|
81
|
+
(_deferred$data3 = deferred.data) === null || _deferred$data3 === void 0 || _deferred$data3.notifyComplete();
|
|
63
82
|
delete deferredMap[event.id];
|
|
64
83
|
break;
|
|
65
84
|
case 'error':
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
break;
|
|
85
|
+
{
|
|
86
|
+
const error = (0, _interceptors.runOnError)(interceptors, new _errors.GrpcError(event.error, event.code, event.trailers), method);
|
|
87
|
+
rejectDeferredCall(deferred, error);
|
|
88
|
+
delete deferredMap[event.id];
|
|
89
|
+
break;
|
|
90
|
+
}
|
|
73
91
|
}
|
|
74
92
|
}
|
|
75
93
|
}
|
|
@@ -85,107 +103,304 @@ function buildRequestObject(data, options) {
|
|
|
85
103
|
}
|
|
86
104
|
return obj;
|
|
87
105
|
}
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
106
|
+
function normalizeTlsOptions(options = {}) {
|
|
107
|
+
const rootCertsPem = options.rootCertsPem ?? null;
|
|
108
|
+
const certificateChainPem = options.certificateChainPem ?? null;
|
|
109
|
+
const privateKeyPem = options.privateKeyPem ?? null;
|
|
110
|
+
const hostNameOverride = options.hostNameOverride ?? null;
|
|
111
|
+
const spkiSha256Pins = options.spkiSha256Pins && options.spkiSha256Pins.length > 0 ? options.spkiSha256Pins : null;
|
|
112
|
+
const hasCert = !!certificateChainPem;
|
|
113
|
+
const hasKey = !!privateKeyPem;
|
|
114
|
+
if (hasCert !== hasKey) {
|
|
115
|
+
throw new Error('mTLS requires both certificateChainPem and privateKeyPem');
|
|
94
116
|
}
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
117
|
+
return {
|
|
118
|
+
rootCertsPem,
|
|
119
|
+
certificateChainPem,
|
|
120
|
+
privateKeyPem,
|
|
121
|
+
hostNameOverride,
|
|
122
|
+
spkiSha256Pins
|
|
123
|
+
};
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
/**
|
|
127
|
+
* Shared RPC surface bound to a native channel id.
|
|
128
|
+
* Used by the singleton {@link GrpcClient} and by {@link GrpcChannel}.
|
|
129
|
+
*/
|
|
130
|
+
class GrpcCallApi {
|
|
131
|
+
channelDeadlineSeconds = 120;
|
|
132
|
+
channelRetry = null;
|
|
133
|
+
channelHedging = null;
|
|
134
|
+
constructor(channelId, channelInterceptors = [], channelDeadlineSeconds = 120, channelRetry = null, channelHedging = null) {
|
|
135
|
+
this.channelId = channelId;
|
|
136
|
+
this.channelInterceptors = [...channelInterceptors];
|
|
137
|
+
this.channelDeadlineSeconds = channelDeadlineSeconds;
|
|
138
|
+
this.channelRetry = channelRetry;
|
|
139
|
+
this.channelHedging = channelHedging;
|
|
100
140
|
}
|
|
101
|
-
|
|
102
|
-
return
|
|
141
|
+
resolveInterceptors(options) {
|
|
142
|
+
return (0, _interceptors.combineInterceptors)(this.channelInterceptors, options === null || options === void 0 ? void 0 : options.interceptors);
|
|
103
143
|
}
|
|
104
|
-
|
|
105
|
-
|
|
144
|
+
resolvePolicy(options) {
|
|
145
|
+
return (0, _retry.resolveEffectivePolicy)(this.channelRetry, this.channelHedging, options === null || options === void 0 ? void 0 : options.retry, options === null || options === void 0 ? void 0 : options.hedging);
|
|
106
146
|
}
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
* Apply before `initGrpcChannel()`. Ignored while insecure/plaintext.
|
|
111
|
-
*/
|
|
112
|
-
setTlsOptions(options = {}) {
|
|
113
|
-
const rootCertsPem = options.rootCertsPem ?? null;
|
|
114
|
-
const certificateChainPem = options.certificateChainPem ?? null;
|
|
115
|
-
const privateKeyPem = options.privateKeyPem ?? null;
|
|
116
|
-
const hostNameOverride = options.hostNameOverride ?? null;
|
|
117
|
-
const spkiSha256Pins = options.spkiSha256Pins && options.spkiSha256Pins.length > 0 ? options.spkiSha256Pins : null;
|
|
118
|
-
const hasCert = !!certificateChainPem;
|
|
119
|
-
const hasKey = !!privateKeyPem;
|
|
120
|
-
if (hasCert !== hasKey) {
|
|
121
|
-
throw new Error('mTLS requires both certificateChainPem and privateKeyPem');
|
|
147
|
+
resolveDeadlineBudgetSeconds(options) {
|
|
148
|
+
if ((options === null || options === void 0 ? void 0 : options.deadlineSeconds) !== undefined) {
|
|
149
|
+
return options.deadlineSeconds;
|
|
122
150
|
}
|
|
123
|
-
|
|
124
|
-
rootCertsPem,
|
|
125
|
-
certificateChainPem,
|
|
126
|
-
privateKeyPem,
|
|
127
|
-
hostNameOverride,
|
|
128
|
-
spkiSha256Pins
|
|
129
|
-
});
|
|
130
|
-
}
|
|
131
|
-
setCompression(enable, compressorName) {
|
|
132
|
-
nativeGrpc().setCompression(enable, compressorName);
|
|
133
|
-
}
|
|
134
|
-
setResponseSizeLimit(limitInBytes) {
|
|
135
|
-
nativeGrpc().setResponseSizeLimit(limitInBytes);
|
|
136
|
-
}
|
|
137
|
-
/** Global per-call deadline in seconds (Android/iOS). Default 120. Overridable per RPC via options. */
|
|
138
|
-
setCallDeadlineSeconds(seconds) {
|
|
139
|
-
nativeGrpc().setCallDeadlineSeconds(seconds);
|
|
140
|
-
}
|
|
141
|
-
initGrpcChannel() {
|
|
142
|
-
nativeGrpc().initGrpcChannel();
|
|
143
|
-
}
|
|
144
|
-
setKeepAlive(enable, keepAliveTime, keepAliveTimeOut) {
|
|
145
|
-
nativeGrpc().setKeepAlive(enable, keepAliveTime, keepAliveTimeOut);
|
|
151
|
+
return this.channelDeadlineSeconds;
|
|
146
152
|
}
|
|
147
|
-
|
|
148
|
-
if (
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
153
|
+
async runUnaryAttempt(method, data, requestHeaders, options, interceptors, parentSignal, attemptIds) {
|
|
154
|
+
if (parentSignal.aborted) {
|
|
155
|
+
throw new _errors.GrpcError('aborted', _status.GrpcStatusCode.CANCELLED);
|
|
156
|
+
}
|
|
157
|
+
const id = getId();
|
|
158
|
+
attemptIds.add(id);
|
|
159
|
+
const attemptAbort = new _abortController.AbortController();
|
|
160
|
+
const onParentAbort = () => attemptAbort.abort();
|
|
161
|
+
parentSignal.addEventListener('abort', onParentAbort);
|
|
162
|
+
const response = createDeferred(attemptAbort.signal);
|
|
163
|
+
const headers = createDeferred(attemptAbort.signal);
|
|
164
|
+
const trailers = createDeferred(attemptAbort.signal);
|
|
165
|
+
deferredMap[id] = {
|
|
166
|
+
response,
|
|
167
|
+
headers,
|
|
168
|
+
trailers,
|
|
169
|
+
method,
|
|
170
|
+
interceptors
|
|
171
|
+
};
|
|
172
|
+
try {
|
|
173
|
+
let start = await (0, _interceptors.runOnStart)(interceptors, {
|
|
174
|
+
method,
|
|
175
|
+
headers: {
|
|
176
|
+
...requestHeaders
|
|
177
|
+
},
|
|
178
|
+
options: {
|
|
179
|
+
...(options || {})
|
|
180
|
+
},
|
|
181
|
+
request: data
|
|
182
|
+
});
|
|
183
|
+
const callOptions = {
|
|
184
|
+
...start.options
|
|
185
|
+
};
|
|
186
|
+
delete callOptions.interceptors;
|
|
187
|
+
delete callOptions.retry;
|
|
188
|
+
delete callOptions.hedging;
|
|
189
|
+
start = {
|
|
190
|
+
...start,
|
|
191
|
+
options: callOptions
|
|
192
|
+
};
|
|
193
|
+
const requestBytes = await (0, _interceptors.runOnSendMessage)(interceptors, start.request ?? data, start.method);
|
|
194
|
+
const obj = buildRequestObject(requestBytes, start.options);
|
|
195
|
+
await (0, _native.nativeGrpc)().unaryCall(id, start.method, obj, start.headers, this.channelId);
|
|
196
|
+
const [hdrs, body, trl] = await Promise.all([headers.promise, response.promise, trailers.promise]);
|
|
197
|
+
return {
|
|
198
|
+
headers: hdrs,
|
|
199
|
+
response: body,
|
|
200
|
+
trailers: trl
|
|
201
|
+
};
|
|
202
|
+
} catch (reason) {
|
|
203
|
+
if (attemptAbort.signal.aborted || parentSignal.aborted) {
|
|
204
|
+
throw new _errors.GrpcError('aborted', _status.GrpcStatusCode.CANCELLED);
|
|
205
|
+
}
|
|
206
|
+
throw (0, _interceptors.toGrpcError)(reason);
|
|
207
|
+
} finally {
|
|
208
|
+
parentSignal.removeEventListener('abort', onParentAbort);
|
|
209
|
+
attemptIds.delete(id);
|
|
210
|
+
if (attemptAbort.signal.aborted) {
|
|
211
|
+
(0, _native.nativeGrpc)().cancelGrpcCall(id);
|
|
212
|
+
}
|
|
213
|
+
}
|
|
158
214
|
}
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
215
|
+
async runUnaryWithPolicy(method, data, requestHeaders, options, interceptors, parentSignal, policy, deadlineBudgetSeconds) {
|
|
216
|
+
const startedAt = Date.now();
|
|
217
|
+
const attemptIds = new Set();
|
|
218
|
+
const withRemainingDeadline = () => {
|
|
219
|
+
const next = {
|
|
220
|
+
...(options || {})
|
|
221
|
+
};
|
|
222
|
+
delete next.interceptors;
|
|
223
|
+
delete next.retry;
|
|
224
|
+
delete next.hedging;
|
|
225
|
+
if (policy.kind === 'none') {
|
|
226
|
+
if ((options === null || options === void 0 ? void 0 : options.deadlineSeconds) === undefined) {
|
|
227
|
+
delete next.deadlineSeconds;
|
|
228
|
+
}
|
|
229
|
+
return next;
|
|
230
|
+
}
|
|
231
|
+
const remaining = (0, _retry.remainingDeadlineSeconds)(deadlineBudgetSeconds, startedAt);
|
|
232
|
+
if (remaining !== undefined) {
|
|
233
|
+
next.deadlineSeconds = remaining;
|
|
234
|
+
}
|
|
235
|
+
return next;
|
|
236
|
+
};
|
|
237
|
+
const cancelAllAttempts = () => {
|
|
238
|
+
for (const id of [...attemptIds]) {
|
|
239
|
+
(0, _native.nativeGrpc)().cancelGrpcCall(id);
|
|
240
|
+
}
|
|
241
|
+
};
|
|
242
|
+
const onAbort = () => cancelAllAttempts();
|
|
243
|
+
parentSignal.addEventListener('abort', onAbort);
|
|
244
|
+
try {
|
|
245
|
+
if (policy.kind === 'none') {
|
|
246
|
+
return await this.runUnaryAttempt(method, data, requestHeaders, withRemainingDeadline(), interceptors, parentSignal, attemptIds);
|
|
247
|
+
}
|
|
248
|
+
if (policy.kind === 'retry') {
|
|
249
|
+
let lastError;
|
|
250
|
+
for (let attempt = 1; attempt <= policy.policy.maxAttempts; attempt++) {
|
|
251
|
+
if (parentSignal.aborted) {
|
|
252
|
+
throw new _errors.GrpcError('aborted', _status.GrpcStatusCode.CANCELLED);
|
|
253
|
+
}
|
|
254
|
+
const remaining = (0, _retry.remainingDeadlineSeconds)(deadlineBudgetSeconds, startedAt);
|
|
255
|
+
if (deadlineBudgetSeconds !== undefined && deadlineBudgetSeconds > 0 && remaining !== undefined && remaining <= 0) {
|
|
256
|
+
throw new _errors.GrpcError('DEADLINE_EXCEEDED', _status.GrpcStatusCode.DEADLINE_EXCEEDED);
|
|
257
|
+
}
|
|
258
|
+
try {
|
|
259
|
+
return await this.runUnaryAttempt(method, data, requestHeaders, withRemainingDeadline(), interceptors, parentSignal, attemptIds);
|
|
260
|
+
} catch (reason) {
|
|
261
|
+
const error = (0, _interceptors.toGrpcError)(reason);
|
|
262
|
+
lastError = error;
|
|
263
|
+
if (error.code === _status.GrpcStatusCode.CANCELLED || parentSignal.aborted) {
|
|
264
|
+
throw error;
|
|
265
|
+
}
|
|
266
|
+
const retryable = error.code !== undefined && policy.policy.retryableStatusCodes.has(error.code);
|
|
267
|
+
if (!retryable || attempt >= policy.policy.maxAttempts) {
|
|
268
|
+
throw error;
|
|
269
|
+
}
|
|
270
|
+
const backoffMs = (0, _retry.computeRetryBackoffMs)(policy.policy, attempt);
|
|
271
|
+
await (0, _retry.delay)(backoffMs, parentSignal);
|
|
272
|
+
}
|
|
273
|
+
}
|
|
274
|
+
throw lastError ?? new _errors.GrpcError('UNAVAILABLE', _status.GrpcStatusCode.UNAVAILABLE);
|
|
275
|
+
}
|
|
276
|
+
|
|
277
|
+
// hedging
|
|
278
|
+
|
|
279
|
+
return await new Promise((resolve, reject) => {
|
|
280
|
+
let settled = false;
|
|
281
|
+
let started = 0;
|
|
282
|
+
let finished = 0;
|
|
283
|
+
const timers = [];
|
|
284
|
+
const inFlight = new Map();
|
|
285
|
+
const finishSuccess = value => {
|
|
286
|
+
if (settled) {
|
|
287
|
+
return;
|
|
288
|
+
}
|
|
289
|
+
settled = true;
|
|
290
|
+
timers.forEach(clearTimeout);
|
|
291
|
+
cancelAllAttempts();
|
|
292
|
+
resolve(value);
|
|
293
|
+
};
|
|
294
|
+
const finishError = error => {
|
|
295
|
+
if (settled) {
|
|
296
|
+
return;
|
|
297
|
+
}
|
|
298
|
+
settled = true;
|
|
299
|
+
timers.forEach(clearTimeout);
|
|
300
|
+
cancelAllAttempts();
|
|
301
|
+
reject(error);
|
|
302
|
+
};
|
|
303
|
+
const launch = () => {
|
|
304
|
+
if (settled || parentSignal.aborted) {
|
|
305
|
+
return;
|
|
306
|
+
}
|
|
307
|
+
const remaining = (0, _retry.remainingDeadlineSeconds)(deadlineBudgetSeconds, startedAt);
|
|
308
|
+
if (deadlineBudgetSeconds !== undefined && deadlineBudgetSeconds > 0 && remaining !== undefined && remaining <= 0) {
|
|
309
|
+
finishError(new _errors.GrpcError('DEADLINE_EXCEEDED', _status.GrpcStatusCode.DEADLINE_EXCEEDED));
|
|
310
|
+
return;
|
|
311
|
+
}
|
|
312
|
+
started += 1;
|
|
313
|
+
const launchIndex = started;
|
|
314
|
+
const p = this.runUnaryAttempt(method, data, requestHeaders, withRemainingDeadline(), interceptors, parentSignal, attemptIds).then(value => ({
|
|
315
|
+
ok: true,
|
|
316
|
+
value
|
|
317
|
+
}), reason => ({
|
|
318
|
+
ok: false,
|
|
319
|
+
error: (0, _interceptors.toGrpcError)(reason)
|
|
320
|
+
})).then(outcome => {
|
|
321
|
+
inFlight.delete(launchIndex);
|
|
322
|
+
finished += 1;
|
|
323
|
+
if (settled) {
|
|
324
|
+
return outcome;
|
|
325
|
+
}
|
|
326
|
+
if (outcome.ok) {
|
|
327
|
+
finishSuccess(outcome.value);
|
|
328
|
+
return outcome;
|
|
329
|
+
}
|
|
330
|
+
const {
|
|
331
|
+
error
|
|
332
|
+
} = outcome;
|
|
333
|
+
if (error.code === _status.GrpcStatusCode.CANCELLED || parentSignal.aborted) {
|
|
334
|
+
if (finished >= started && inFlight.size === 0) {
|
|
335
|
+
finishError(error);
|
|
336
|
+
}
|
|
337
|
+
return outcome;
|
|
338
|
+
}
|
|
339
|
+
const nonFatal = error.code !== undefined && policy.policy.nonFatalStatusCodes.has(error.code);
|
|
340
|
+
if (!nonFatal) {
|
|
341
|
+
finishError(error);
|
|
342
|
+
return outcome;
|
|
343
|
+
}
|
|
344
|
+
if (finished >= policy.policy.maxAttempts || started >= policy.policy.maxAttempts && inFlight.size === 0) {
|
|
345
|
+
finishError(error);
|
|
346
|
+
}
|
|
347
|
+
return outcome;
|
|
348
|
+
});
|
|
349
|
+
inFlight.set(launchIndex, p);
|
|
350
|
+
};
|
|
351
|
+
launch();
|
|
352
|
+
for (let i = 2; i <= policy.policy.maxAttempts; i++) {
|
|
353
|
+
const delayMs = policy.policy.hedgingDelayMs * (i - 1);
|
|
354
|
+
if (delayMs <= 0) {
|
|
355
|
+
launch();
|
|
356
|
+
} else {
|
|
357
|
+
timers.push(setTimeout(launch, delayMs));
|
|
358
|
+
}
|
|
359
|
+
}
|
|
360
|
+
parentSignal.addEventListener('abort', () => {
|
|
361
|
+
finishError(new _errors.GrpcError('aborted', _status.GrpcStatusCode.CANCELLED));
|
|
362
|
+
});
|
|
363
|
+
});
|
|
364
|
+
} finally {
|
|
365
|
+
parentSignal.removeEventListener('abort', onAbort);
|
|
366
|
+
}
|
|
162
367
|
}
|
|
163
368
|
unaryCall(method, data, requestHeaders, options) {
|
|
164
|
-
const obj = buildRequestObject(data, options);
|
|
165
|
-
const id = getId();
|
|
166
369
|
const abort = new _abortController.AbortController();
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
370
|
+
const interceptors = this.resolveInterceptors(options);
|
|
371
|
+
const policy = this.resolvePolicy(options);
|
|
372
|
+
const deadlineBudgetSeconds = this.resolveDeadlineBudgetSeconds(options);
|
|
170
373
|
const response = createDeferred(abort.signal);
|
|
171
374
|
const headers = createDeferred(abort.signal);
|
|
172
375
|
const trailers = createDeferred(abort.signal);
|
|
173
|
-
deferredMap[id] = {
|
|
174
|
-
response,
|
|
175
|
-
headers,
|
|
176
|
-
trailers
|
|
177
|
-
};
|
|
178
|
-
nativeGrpc().unaryCall(id, method, obj, requestHeaders || {});
|
|
179
376
|
const call = new _unary.GrpcUnaryCall(method, data, requestHeaders || {}, headers.promise, response.promise, trailers.promise, abort);
|
|
180
377
|
call.then(result => result, () => abort.abort());
|
|
378
|
+
Promise.resolve().then(async () => {
|
|
379
|
+
const result = await this.runUnaryWithPolicy(method, data, requestHeaders || {}, options, interceptors, abort.signal, policy, deadlineBudgetSeconds);
|
|
380
|
+
headers.resolve(result.headers);
|
|
381
|
+
response.resolve(result.response);
|
|
382
|
+
trailers.resolve(result.trailers);
|
|
383
|
+
}).catch(reason => {
|
|
384
|
+
if (abort.signal.aborted) {
|
|
385
|
+
const error = new _errors.GrpcError('aborted', _status.GrpcStatusCode.CANCELLED);
|
|
386
|
+
headers.reject(error);
|
|
387
|
+
response.reject(error);
|
|
388
|
+
trailers.reject(error);
|
|
389
|
+
return;
|
|
390
|
+
}
|
|
391
|
+
const error = (0, _interceptors.runOnError)(interceptors, (0, _interceptors.toGrpcError)(reason), method);
|
|
392
|
+
headers.reject(error);
|
|
393
|
+
response.reject(error);
|
|
394
|
+
trailers.reject(error);
|
|
395
|
+
});
|
|
181
396
|
return call;
|
|
182
397
|
}
|
|
183
398
|
serverStreamCall(method, data, requestHeaders, options) {
|
|
184
|
-
const obj = buildRequestObject(data, options);
|
|
185
399
|
const id = getId();
|
|
186
400
|
const abort = new _abortController.AbortController();
|
|
401
|
+
const interceptors = this.resolveInterceptors(options);
|
|
187
402
|
abort.signal.addEventListener('abort', () => {
|
|
188
|
-
nativeGrpc().cancelGrpcCall(id);
|
|
403
|
+
(0, _native.nativeGrpc)().cancelGrpcCall(id);
|
|
189
404
|
});
|
|
190
405
|
const headers = createDeferred(abort.signal);
|
|
191
406
|
const trailers = createDeferred(abort.signal);
|
|
@@ -193,11 +408,43 @@ class GrpcClient {
|
|
|
193
408
|
deferredMap[id] = {
|
|
194
409
|
headers,
|
|
195
410
|
trailers,
|
|
196
|
-
data: stream
|
|
411
|
+
data: stream,
|
|
412
|
+
method,
|
|
413
|
+
interceptors
|
|
197
414
|
};
|
|
198
|
-
nativeGrpc().serverStreamingCall(id, method, obj, requestHeaders || {});
|
|
199
415
|
const call = new _serverStreaming.GrpcServerStreamingCall(method, data, requestHeaders || {}, headers.promise, stream, trailers.promise, abort);
|
|
200
416
|
call.then(result => result, () => abort.abort());
|
|
417
|
+
Promise.resolve().then(async () => {
|
|
418
|
+
let start = await (0, _interceptors.runOnStart)(interceptors, {
|
|
419
|
+
method,
|
|
420
|
+
headers: {
|
|
421
|
+
...(requestHeaders || {})
|
|
422
|
+
},
|
|
423
|
+
options: {
|
|
424
|
+
...(options || {})
|
|
425
|
+
},
|
|
426
|
+
request: data
|
|
427
|
+
});
|
|
428
|
+
const callOptions = {
|
|
429
|
+
...start.options
|
|
430
|
+
};
|
|
431
|
+
delete callOptions.interceptors;
|
|
432
|
+
start = {
|
|
433
|
+
...start,
|
|
434
|
+
options: callOptions
|
|
435
|
+
};
|
|
436
|
+
const requestBytes = await (0, _interceptors.runOnSendMessage)(interceptors, start.request ?? data, start.method);
|
|
437
|
+
const obj = buildRequestObject(requestBytes, start.options);
|
|
438
|
+
await (0, _native.nativeGrpc)().serverStreamingCall(id, start.method, obj, start.headers, this.channelId);
|
|
439
|
+
}).catch(reason => {
|
|
440
|
+
const deferred = deferredMap[id];
|
|
441
|
+
if (!deferred) {
|
|
442
|
+
return;
|
|
443
|
+
}
|
|
444
|
+
const error = (0, _interceptors.runOnError)(interceptors, (0, _interceptors.toGrpcError)(reason), method);
|
|
445
|
+
rejectDeferredCall(deferred, error);
|
|
446
|
+
delete deferredMap[id];
|
|
447
|
+
});
|
|
201
448
|
return call;
|
|
202
449
|
}
|
|
203
450
|
|
|
@@ -208,9 +455,17 @@ class GrpcClient {
|
|
|
208
455
|
clientStreamCall(method, requestHeaders, options) {
|
|
209
456
|
const id = getId();
|
|
210
457
|
const abort = new _abortController.AbortController();
|
|
211
|
-
const
|
|
458
|
+
const interceptors = this.resolveInterceptors(options);
|
|
459
|
+
let headersMeta = {
|
|
460
|
+
...(requestHeaders || {})
|
|
461
|
+
};
|
|
462
|
+
let callOptions = {
|
|
463
|
+
...(options || {})
|
|
464
|
+
};
|
|
465
|
+
const channelId = this.channelId;
|
|
466
|
+
let started = false;
|
|
212
467
|
abort.signal.addEventListener('abort', () => {
|
|
213
|
-
nativeGrpc().cancelGrpcCall(id);
|
|
468
|
+
(0, _native.nativeGrpc)().cancelGrpcCall(id);
|
|
214
469
|
});
|
|
215
470
|
const response = createDeferred(abort.signal);
|
|
216
471
|
const headers = createDeferred(abort.signal);
|
|
@@ -218,12 +473,40 @@ class GrpcClient {
|
|
|
218
473
|
deferredMap[id] = {
|
|
219
474
|
response,
|
|
220
475
|
headers,
|
|
221
|
-
trailers
|
|
476
|
+
trailers,
|
|
477
|
+
method,
|
|
478
|
+
interceptors
|
|
222
479
|
};
|
|
223
|
-
const requests = new _clientStreaming.ServerInputStream((data, isFirst) => {
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
480
|
+
const requests = new _clientStreaming.ServerInputStream(async (data, isFirst) => {
|
|
481
|
+
try {
|
|
482
|
+
let message = await (0, _interceptors.runOnSendMessage)(interceptors, data, method);
|
|
483
|
+
if (isFirst || !started) {
|
|
484
|
+
const start = await (0, _interceptors.runOnStart)(interceptors, {
|
|
485
|
+
method,
|
|
486
|
+
headers: headersMeta,
|
|
487
|
+
options: callOptions,
|
|
488
|
+
request: message
|
|
489
|
+
});
|
|
490
|
+
headersMeta = start.headers;
|
|
491
|
+
callOptions = {
|
|
492
|
+
...start.options
|
|
493
|
+
};
|
|
494
|
+
delete callOptions.interceptors;
|
|
495
|
+
message = start.request ?? message;
|
|
496
|
+
started = true;
|
|
497
|
+
}
|
|
498
|
+
const obj = buildRequestObject(message, isFirst ? callOptions : undefined);
|
|
499
|
+
return (0, _native.nativeGrpc)().clientStreamingCall(id, method, obj, isFirst ? headersMeta : {}, channelId);
|
|
500
|
+
} catch (reason) {
|
|
501
|
+
const deferred = deferredMap[id];
|
|
502
|
+
if (deferred) {
|
|
503
|
+
const error = (0, _interceptors.runOnError)(interceptors, (0, _interceptors.toGrpcError)(reason), method);
|
|
504
|
+
rejectDeferredCall(deferred, error);
|
|
505
|
+
delete deferredMap[id];
|
|
506
|
+
}
|
|
507
|
+
throw (0, _interceptors.toGrpcError)(reason);
|
|
508
|
+
}
|
|
509
|
+
}, () => (0, _native.nativeGrpc)().finishClientStreaming(id));
|
|
227
510
|
const call = new _clientStreaming.GrpcClientStreamingCall(method, headersMeta, requests, headers.promise, response.promise, trailers.promise, abort);
|
|
228
511
|
call.then(result => result, () => abort.abort());
|
|
229
512
|
return call;
|
|
@@ -237,9 +520,17 @@ class GrpcClient {
|
|
|
237
520
|
bidiStreamCall(method, requestHeaders, options) {
|
|
238
521
|
const id = getId();
|
|
239
522
|
const abort = new _abortController.AbortController();
|
|
240
|
-
const
|
|
523
|
+
const interceptors = this.resolveInterceptors(options);
|
|
524
|
+
let headersMeta = {
|
|
525
|
+
...(requestHeaders || {})
|
|
526
|
+
};
|
|
527
|
+
let callOptions = {
|
|
528
|
+
...(options || {})
|
|
529
|
+
};
|
|
530
|
+
const channelId = this.channelId;
|
|
531
|
+
let started = false;
|
|
241
532
|
abort.signal.addEventListener('abort', () => {
|
|
242
|
-
nativeGrpc().cancelGrpcCall(id);
|
|
533
|
+
(0, _native.nativeGrpc)().cancelGrpcCall(id);
|
|
243
534
|
});
|
|
244
535
|
const headers = createDeferred(abort.signal);
|
|
245
536
|
const trailers = createDeferred(abort.signal);
|
|
@@ -247,28 +538,140 @@ class GrpcClient {
|
|
|
247
538
|
deferredMap[id] = {
|
|
248
539
|
headers,
|
|
249
540
|
trailers,
|
|
250
|
-
data: stream
|
|
541
|
+
data: stream,
|
|
542
|
+
method,
|
|
543
|
+
interceptors
|
|
251
544
|
};
|
|
252
|
-
const requests = new _clientStreaming.ServerInputStream((data, isFirst) => {
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
545
|
+
const requests = new _clientStreaming.ServerInputStream(async (data, isFirst) => {
|
|
546
|
+
try {
|
|
547
|
+
let message = await (0, _interceptors.runOnSendMessage)(interceptors, data, method);
|
|
548
|
+
if (isFirst || !started) {
|
|
549
|
+
const start = await (0, _interceptors.runOnStart)(interceptors, {
|
|
550
|
+
method,
|
|
551
|
+
headers: headersMeta,
|
|
552
|
+
options: callOptions,
|
|
553
|
+
request: message
|
|
554
|
+
});
|
|
555
|
+
headersMeta = start.headers;
|
|
556
|
+
callOptions = {
|
|
557
|
+
...start.options
|
|
558
|
+
};
|
|
559
|
+
delete callOptions.interceptors;
|
|
560
|
+
message = start.request ?? message;
|
|
561
|
+
started = true;
|
|
562
|
+
}
|
|
563
|
+
const obj = buildRequestObject(message, isFirst ? callOptions : undefined);
|
|
564
|
+
return (0, _native.nativeGrpc)().bidiStreamingCall(id, method, obj, isFirst ? headersMeta : {}, channelId);
|
|
565
|
+
} catch (reason) {
|
|
566
|
+
const deferred = deferredMap[id];
|
|
567
|
+
if (deferred) {
|
|
568
|
+
const error = (0, _interceptors.runOnError)(interceptors, (0, _interceptors.toGrpcError)(reason), method);
|
|
569
|
+
rejectDeferredCall(deferred, error);
|
|
570
|
+
delete deferredMap[id];
|
|
571
|
+
}
|
|
572
|
+
throw (0, _interceptors.toGrpcError)(reason);
|
|
573
|
+
}
|
|
574
|
+
}, () => (0, _native.nativeGrpc)().finishClientStreaming(id));
|
|
256
575
|
const call = new _bidiStreaming.GrpcBidiStreamingCall(method, headersMeta, requests, headers.promise, stream, trailers.promise, abort);
|
|
257
576
|
call.then(result => result, () => abort.abort());
|
|
258
577
|
return call;
|
|
259
578
|
}
|
|
579
|
+
}
|
|
580
|
+
exports.GrpcCallApi = GrpcCallApi;
|
|
581
|
+
class GrpcClient extends GrpcCallApi {
|
|
582
|
+
constructor() {
|
|
583
|
+
super(_types.DEFAULT_CHANNEL_ID, [], 120, null, null);
|
|
584
|
+
Emitter.addListener('grpc-call', handleGrpcEvent);
|
|
585
|
+
}
|
|
586
|
+
destroy() {
|
|
587
|
+
Emitter.removeAllListeners('grpc-call');
|
|
588
|
+
}
|
|
589
|
+
/**
|
|
590
|
+
* Replace default-channel interceptors (same replace semantics as `setTlsOptions`).
|
|
591
|
+
* Prefer `createChannel({ interceptors })` for multi-host apps.
|
|
592
|
+
*/
|
|
593
|
+
setInterceptors(interceptors = []) {
|
|
594
|
+
this.channelInterceptors = [...interceptors];
|
|
595
|
+
}
|
|
596
|
+
/**
|
|
597
|
+
* Replace unary retry policy on the default channel.
|
|
598
|
+
* Clears any hedging policy (mutually exclusive). Pass `null` to disable.
|
|
599
|
+
*/
|
|
600
|
+
setRetryPolicy(policy) {
|
|
601
|
+
if (policy) {
|
|
602
|
+
(0, _retry.normalizeRetryPolicy)(policy);
|
|
603
|
+
}
|
|
604
|
+
this.channelRetry = policy;
|
|
605
|
+
this.channelHedging = null;
|
|
606
|
+
}
|
|
607
|
+
/**
|
|
608
|
+
* Replace unary hedging policy on the default channel.
|
|
609
|
+
* Clears any retry policy (mutually exclusive). Pass `null` to disable.
|
|
610
|
+
*/
|
|
611
|
+
setHedgingPolicy(policy) {
|
|
612
|
+
if (policy) {
|
|
613
|
+
(0, _retry.normalizeHedgingPolicy)(policy);
|
|
614
|
+
}
|
|
615
|
+
this.channelHedging = policy;
|
|
616
|
+
this.channelRetry = null;
|
|
617
|
+
}
|
|
618
|
+
getHost() {
|
|
619
|
+
return (0, _native.nativeGrpc)().getHost();
|
|
620
|
+
}
|
|
621
|
+
setHost(host) {
|
|
622
|
+
(0, _native.nativeGrpc)().setHost(host);
|
|
623
|
+
}
|
|
624
|
+
getInsecure() {
|
|
625
|
+
return (0, _native.nativeGrpc)().getIsInsecure();
|
|
626
|
+
}
|
|
627
|
+
setInsecure(insecure) {
|
|
628
|
+
(0, _native.nativeGrpc)().setInsecure(insecure);
|
|
629
|
+
}
|
|
630
|
+
/**
|
|
631
|
+
* Replace channel TLS options (custom CA, mTLS, hostname override).
|
|
632
|
+
* Each call replaces the previous config; omitted fields are cleared.
|
|
633
|
+
* Apply before `initGrpcChannel()`. Ignored while insecure/plaintext.
|
|
634
|
+
* For multi-host apps prefer {@link createChannel} with `tls` in config.
|
|
635
|
+
*/
|
|
636
|
+
setTlsOptions(options = {}) {
|
|
637
|
+
(0, _native.nativeGrpc)().setTlsOptions(normalizeTlsOptions(options));
|
|
638
|
+
}
|
|
639
|
+
setCompression(enable, compressorName) {
|
|
640
|
+
(0, _native.nativeGrpc)().setCompression(enable, compressorName);
|
|
641
|
+
}
|
|
642
|
+
setResponseSizeLimit(limitInBytes) {
|
|
643
|
+
(0, _native.nativeGrpc)().setResponseSizeLimit(limitInBytes);
|
|
644
|
+
}
|
|
645
|
+
/** Global per-call deadline in seconds (Android/iOS). Default 120. Overridable per RPC via options. */
|
|
646
|
+
setCallDeadlineSeconds(seconds) {
|
|
647
|
+
this.channelDeadlineSeconds = Math.max(0, seconds);
|
|
648
|
+
(0, _native.nativeGrpc)().setCallDeadlineSeconds(seconds);
|
|
649
|
+
}
|
|
650
|
+
initGrpcChannel() {
|
|
651
|
+
(0, _native.nativeGrpc)().initGrpcChannel();
|
|
652
|
+
}
|
|
653
|
+
setKeepAlive(enable, keepAliveTime, keepAliveTimeOut) {
|
|
654
|
+
(0, _native.nativeGrpc)().setKeepAlive(enable, keepAliveTime, keepAliveTimeOut);
|
|
655
|
+
}
|
|
656
|
+
resetConnection(message) {
|
|
657
|
+
if (!this.isAndroid()) return;
|
|
658
|
+
(0, _native.nativeGrpc)().resetConnection(message);
|
|
659
|
+
}
|
|
660
|
+
setUiLogEnabled(enable) {
|
|
661
|
+
if (!this.isAndroid()) return;
|
|
662
|
+
(0, _native.nativeGrpc)().setUiLogEnabled(enable);
|
|
663
|
+
}
|
|
664
|
+
onConnectionStateChange() {
|
|
665
|
+
if (!this.isAndroid()) return;
|
|
666
|
+
(0, _native.nativeGrpc)().onConnectionStateChange();
|
|
667
|
+
}
|
|
668
|
+
enterIdle() {
|
|
669
|
+
if (!this.isAndroid()) return;
|
|
670
|
+
(0, _native.nativeGrpc)().enterIdle();
|
|
671
|
+
}
|
|
260
672
|
isAndroid() {
|
|
261
673
|
return _reactNative.Platform.OS === 'android';
|
|
262
674
|
}
|
|
263
675
|
}
|
|
264
|
-
|
|
265
|
-
/** Lazy binding to NativeModules.Grpc (works with Jest mocks / late link). */
|
|
266
676
|
exports.GrpcClient = GrpcClient;
|
|
267
|
-
const Grpc = exports.Grpc = new Proxy({}, {
|
|
268
|
-
get(_target, prop) {
|
|
269
|
-
const native = nativeGrpc();
|
|
270
|
-
const value = native[prop];
|
|
271
|
-
return typeof value === 'function' ? value.bind(native) : value;
|
|
272
|
-
}
|
|
273
|
-
});
|
|
274
677
|
//# sourceMappingURL=client.js.map
|