@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
|
@@ -40,36 +40,43 @@ import io.grpc.Status;
|
|
|
40
40
|
import io.grpc.TlsChannelCredentials;
|
|
41
41
|
|
|
42
42
|
public class GrpcModule extends ReactContextBaseJavaModule {
|
|
43
|
+
static final String DEFAULT_CHANNEL_ID = "default";
|
|
44
|
+
|
|
43
45
|
private final ReactApplicationContext context;
|
|
44
46
|
private final HashMap<Integer, ClientCall> callsMap = new HashMap<>();
|
|
47
|
+
private final HashMap<String, ChannelState> channels = new HashMap<>();
|
|
45
48
|
|
|
46
|
-
private String host;
|
|
47
|
-
private boolean isInsecure = false;
|
|
48
|
-
private boolean withCompression = false;
|
|
49
|
-
private String compressorName = "";
|
|
50
|
-
private Integer responseSizeLimit = null;
|
|
51
|
-
private boolean keepAliveEnabled = false;
|
|
52
|
-
private Integer keepAliveTime;
|
|
53
|
-
private Integer keepAliveTimeout;
|
|
54
49
|
private boolean isUiLogEnabled = false;
|
|
55
|
-
|
|
56
|
-
private
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
50
|
+
|
|
51
|
+
private static final class ChannelState {
|
|
52
|
+
String host;
|
|
53
|
+
boolean isInsecure = false;
|
|
54
|
+
boolean withCompression = false;
|
|
55
|
+
String compressorName = "";
|
|
56
|
+
Integer responseSizeLimit = null;
|
|
57
|
+
boolean keepAliveEnabled = false;
|
|
58
|
+
Integer keepAliveTime;
|
|
59
|
+
Integer keepAliveTimeout;
|
|
60
|
+
/** Per-call deadline in seconds (0 = no deadline). Default 120s. */
|
|
61
|
+
long callDeadlineSeconds = 120;
|
|
62
|
+
|
|
63
|
+
/** Custom PEM trust roots; null = platform/gRPC defaults. */
|
|
64
|
+
String rootCertsPem = null;
|
|
65
|
+
/** PEM client certificate chain for mTLS. */
|
|
66
|
+
String certificateChainPem = null;
|
|
67
|
+
/** PEM client private key for mTLS. */
|
|
68
|
+
String privateKeyPem = null;
|
|
69
|
+
/** TLS hostname / SNI override (e.g. dial by IP). */
|
|
70
|
+
String hostNameOverride = null;
|
|
71
|
+
/** SHA-256 SPKI pins (base64, without sha256/ prefix). */
|
|
72
|
+
List<String> spkiSha256Pins = null;
|
|
73
|
+
|
|
74
|
+
ManagedChannel managedChannel = null;
|
|
75
|
+
}
|
|
70
76
|
|
|
71
77
|
public GrpcModule(ReactApplicationContext context) {
|
|
72
78
|
this.context = context;
|
|
79
|
+
channels.put(DEFAULT_CHANNEL_ID, new ChannelState());
|
|
73
80
|
}
|
|
74
81
|
|
|
75
82
|
@NonNull
|
|
@@ -78,24 +85,42 @@ public class GrpcModule extends ReactContextBaseJavaModule {
|
|
|
78
85
|
return "Grpc";
|
|
79
86
|
}
|
|
80
87
|
|
|
88
|
+
private ChannelState defaultChannel() {
|
|
89
|
+
ChannelState state = channels.get(DEFAULT_CHANNEL_ID);
|
|
90
|
+
if (state == null) {
|
|
91
|
+
state = new ChannelState();
|
|
92
|
+
channels.put(DEFAULT_CHANNEL_ID, state);
|
|
93
|
+
}
|
|
94
|
+
return state;
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
private ChannelState requireChannel(String channelId) throws Exception {
|
|
98
|
+
String id = channelId == null || channelId.isEmpty() ? DEFAULT_CHANNEL_ID : channelId;
|
|
99
|
+
ChannelState state = channels.get(id);
|
|
100
|
+
if (state == null) {
|
|
101
|
+
throw new Exception("Unknown channel: " + id);
|
|
102
|
+
}
|
|
103
|
+
return state;
|
|
104
|
+
}
|
|
105
|
+
|
|
81
106
|
@ReactMethod()
|
|
82
107
|
public void getHost(final Promise promise) {
|
|
83
|
-
promise.resolve(
|
|
108
|
+
promise.resolve(defaultChannel().host);
|
|
84
109
|
}
|
|
85
110
|
|
|
86
111
|
@ReactMethod()
|
|
87
112
|
public void getIsInsecure(final Promise promise) {
|
|
88
|
-
promise.resolve(
|
|
113
|
+
promise.resolve(defaultChannel().isInsecure);
|
|
89
114
|
}
|
|
90
115
|
|
|
91
116
|
@ReactMethod
|
|
92
117
|
public void setHost(String host) {
|
|
93
|
-
|
|
118
|
+
defaultChannel().host = host;
|
|
94
119
|
}
|
|
95
120
|
|
|
96
121
|
@ReactMethod
|
|
97
122
|
public void setInsecure(boolean insecure) {
|
|
98
|
-
|
|
123
|
+
defaultChannel().isInsecure = insecure;
|
|
99
124
|
}
|
|
100
125
|
|
|
101
126
|
/**
|
|
@@ -104,16 +129,17 @@ public class GrpcModule extends ReactContextBaseJavaModule {
|
|
|
104
129
|
*/
|
|
105
130
|
@ReactMethod
|
|
106
131
|
public void setTlsOptions(ReadableMap options) {
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
132
|
+
ChannelState state = defaultChannel();
|
|
133
|
+
state.rootCertsPem = readOptionalString(options, "rootCertsPem");
|
|
134
|
+
state.certificateChainPem = readOptionalString(options, "certificateChainPem");
|
|
135
|
+
state.privateKeyPem = readOptionalString(options, "privateKeyPem");
|
|
136
|
+
state.hostNameOverride = readOptionalString(options, "hostNameOverride");
|
|
137
|
+
state.spkiSha256Pins = SpkiPinTrustManager.normalizePins(
|
|
112
138
|
readOptionalStringList(options, "spkiSha256Pins")
|
|
113
139
|
);
|
|
114
140
|
|
|
115
|
-
boolean hasCert =
|
|
116
|
-
boolean hasKey =
|
|
141
|
+
boolean hasCert = state.certificateChainPem != null && !state.certificateChainPem.isEmpty();
|
|
142
|
+
boolean hasKey = state.privateKeyPem != null && !state.privateKeyPem.isEmpty();
|
|
117
143
|
if (hasCert != hasKey) {
|
|
118
144
|
throw new IllegalArgumentException(
|
|
119
145
|
"mTLS requires both certificateChainPem and privateKeyPem"
|
|
@@ -152,33 +178,123 @@ public class GrpcModule extends ReactContextBaseJavaModule {
|
|
|
152
178
|
|
|
153
179
|
@ReactMethod
|
|
154
180
|
public void setCompression(Boolean enable, String compressorName) {
|
|
155
|
-
|
|
156
|
-
|
|
181
|
+
ChannelState state = defaultChannel();
|
|
182
|
+
state.withCompression = enable;
|
|
183
|
+
state.compressorName = compressorName;
|
|
157
184
|
}
|
|
158
185
|
|
|
159
186
|
@ReactMethod
|
|
160
187
|
public void setResponseSizeLimit(int limit) {
|
|
161
|
-
|
|
188
|
+
defaultChannel().responseSizeLimit = limit;
|
|
162
189
|
}
|
|
163
190
|
|
|
164
191
|
@ReactMethod
|
|
165
192
|
public void setKeepAlive(boolean enabled, int time, int timeout) {
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
193
|
+
ChannelState state = defaultChannel();
|
|
194
|
+
state.keepAliveEnabled = enabled;
|
|
195
|
+
state.keepAliveTime = time;
|
|
196
|
+
state.keepAliveTimeout = timeout;
|
|
169
197
|
}
|
|
170
198
|
|
|
171
199
|
@ReactMethod
|
|
172
200
|
public void setCallDeadlineSeconds(double seconds) {
|
|
173
|
-
|
|
201
|
+
defaultChannel().callDeadlineSeconds = Math.max(0, (long) seconds);
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
/**
|
|
205
|
+
* Create (or replace) a named channel from immutable config and open its ManagedChannel.
|
|
206
|
+
* Does not affect the default channel used by setHost / initGrpcChannel.
|
|
207
|
+
*/
|
|
208
|
+
@ReactMethod
|
|
209
|
+
public void createChannel(String channelId, ReadableMap config) {
|
|
210
|
+
if (channelId == null || channelId.isEmpty()) {
|
|
211
|
+
throw new IllegalArgumentException("channelId is required");
|
|
212
|
+
}
|
|
213
|
+
if (DEFAULT_CHANNEL_ID.equals(channelId)) {
|
|
214
|
+
throw new IllegalArgumentException("channelId \"default\" is reserved for GrpcClient");
|
|
215
|
+
}
|
|
216
|
+
if (config == null || !config.hasKey("host") || config.isNull("host")) {
|
|
217
|
+
throw new IllegalArgumentException("createChannel requires host");
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
ChannelState existing = channels.get(channelId);
|
|
221
|
+
if (existing != null && existing.managedChannel != null) {
|
|
222
|
+
existing.managedChannel.shutdown();
|
|
223
|
+
}
|
|
224
|
+
|
|
225
|
+
ChannelState state = new ChannelState();
|
|
226
|
+
state.host = config.getString("host");
|
|
227
|
+
state.isInsecure = config.hasKey("insecure") && !config.isNull("insecure") && config.getBoolean("insecure");
|
|
228
|
+
state.callDeadlineSeconds = config.hasKey("callDeadlineSeconds") && !config.isNull("callDeadlineSeconds")
|
|
229
|
+
? Math.max(0, (long) config.getDouble("callDeadlineSeconds"))
|
|
230
|
+
: 120;
|
|
231
|
+
if (config.hasKey("responseSizeLimit") && !config.isNull("responseSizeLimit")) {
|
|
232
|
+
state.responseSizeLimit = config.getInt("responseSizeLimit");
|
|
233
|
+
}
|
|
234
|
+
state.withCompression = config.hasKey("compressionEnable")
|
|
235
|
+
&& !config.isNull("compressionEnable")
|
|
236
|
+
&& config.getBoolean("compressionEnable");
|
|
237
|
+
state.compressorName = config.hasKey("compressorName") && !config.isNull("compressorName")
|
|
238
|
+
? config.getString("compressorName")
|
|
239
|
+
: "";
|
|
240
|
+
state.keepAliveEnabled = config.hasKey("keepAliveEnable")
|
|
241
|
+
&& !config.isNull("keepAliveEnable")
|
|
242
|
+
&& config.getBoolean("keepAliveEnable");
|
|
243
|
+
if (config.hasKey("keepAliveTime") && !config.isNull("keepAliveTime")) {
|
|
244
|
+
state.keepAliveTime = config.getInt("keepAliveTime");
|
|
245
|
+
}
|
|
246
|
+
if (config.hasKey("keepAliveTimeOut") && !config.isNull("keepAliveTimeOut")) {
|
|
247
|
+
state.keepAliveTimeout = config.getInt("keepAliveTimeOut");
|
|
248
|
+
}
|
|
249
|
+
|
|
250
|
+
if (config.hasKey("tls") && !config.isNull("tls")) {
|
|
251
|
+
ReadableMap tls = config.getMap("tls");
|
|
252
|
+
state.rootCertsPem = readOptionalString(tls, "rootCertsPem");
|
|
253
|
+
state.certificateChainPem = readOptionalString(tls, "certificateChainPem");
|
|
254
|
+
state.privateKeyPem = readOptionalString(tls, "privateKeyPem");
|
|
255
|
+
state.hostNameOverride = readOptionalString(tls, "hostNameOverride");
|
|
256
|
+
state.spkiSha256Pins = SpkiPinTrustManager.normalizePins(
|
|
257
|
+
readOptionalStringList(tls, "spkiSha256Pins")
|
|
258
|
+
);
|
|
259
|
+
boolean hasCert = state.certificateChainPem != null && !state.certificateChainPem.isEmpty();
|
|
260
|
+
boolean hasKey = state.privateKeyPem != null && !state.privateKeyPem.isEmpty();
|
|
261
|
+
if (hasCert != hasKey) {
|
|
262
|
+
throw new IllegalArgumentException(
|
|
263
|
+
"mTLS requires both certificateChainPem and privateKeyPem"
|
|
264
|
+
);
|
|
265
|
+
}
|
|
266
|
+
}
|
|
267
|
+
|
|
268
|
+
state.managedChannel = buildManagedChannel(state);
|
|
269
|
+
channels.put(channelId, state);
|
|
270
|
+
}
|
|
271
|
+
|
|
272
|
+
@ReactMethod
|
|
273
|
+
public void closeChannel(String channelId) {
|
|
274
|
+
if (channelId == null || channelId.isEmpty() || DEFAULT_CHANNEL_ID.equals(channelId)) {
|
|
275
|
+
return;
|
|
276
|
+
}
|
|
277
|
+
ChannelState state = channels.remove(channelId);
|
|
278
|
+
if (state != null && state.managedChannel != null) {
|
|
279
|
+
state.managedChannel.shutdown();
|
|
280
|
+
state.managedChannel = null;
|
|
281
|
+
}
|
|
174
282
|
}
|
|
175
283
|
|
|
176
284
|
@ReactMethod
|
|
177
|
-
public void unaryCall(
|
|
285
|
+
public void unaryCall(
|
|
286
|
+
int id,
|
|
287
|
+
String path,
|
|
288
|
+
ReadableMap obj,
|
|
289
|
+
ReadableMap headers,
|
|
290
|
+
String channelId,
|
|
291
|
+
final Promise promise
|
|
292
|
+
) {
|
|
178
293
|
ClientCall call;
|
|
179
294
|
|
|
180
295
|
try {
|
|
181
|
-
|
|
296
|
+
ChannelState state = requireChannel(channelId);
|
|
297
|
+
call = this.startGrpcCall(id, path, MethodDescriptor.MethodType.UNARY, headers, resolveDeadline(obj, state), state);
|
|
182
298
|
} catch (Exception e) {
|
|
183
299
|
promise.reject(e);
|
|
184
300
|
|
|
@@ -197,11 +313,19 @@ public class GrpcModule extends ReactContextBaseJavaModule {
|
|
|
197
313
|
}
|
|
198
314
|
|
|
199
315
|
@ReactMethod
|
|
200
|
-
public void serverStreamingCall(
|
|
316
|
+
public void serverStreamingCall(
|
|
317
|
+
int id,
|
|
318
|
+
String path,
|
|
319
|
+
ReadableMap obj,
|
|
320
|
+
ReadableMap headers,
|
|
321
|
+
String channelId,
|
|
322
|
+
final Promise promise
|
|
323
|
+
) {
|
|
201
324
|
ClientCall call;
|
|
202
325
|
|
|
203
326
|
try {
|
|
204
|
-
|
|
327
|
+
ChannelState state = requireChannel(channelId);
|
|
328
|
+
call = this.startGrpcCall(id, path, MethodDescriptor.MethodType.SERVER_STREAMING, headers, resolveDeadline(obj, state), state);
|
|
205
329
|
} catch (Exception e) {
|
|
206
330
|
promise.reject(e);
|
|
207
331
|
|
|
@@ -220,12 +344,20 @@ public class GrpcModule extends ReactContextBaseJavaModule {
|
|
|
220
344
|
}
|
|
221
345
|
|
|
222
346
|
@ReactMethod
|
|
223
|
-
public void clientStreamingCall(
|
|
347
|
+
public void clientStreamingCall(
|
|
348
|
+
int id,
|
|
349
|
+
String path,
|
|
350
|
+
ReadableMap obj,
|
|
351
|
+
ReadableMap headers,
|
|
352
|
+
String channelId,
|
|
353
|
+
final Promise promise
|
|
354
|
+
) {
|
|
224
355
|
ClientCall call = callsMap.get(id);
|
|
225
356
|
|
|
226
357
|
if (call == null) {
|
|
227
358
|
try {
|
|
228
|
-
|
|
359
|
+
ChannelState state = requireChannel(channelId);
|
|
360
|
+
call = this.startGrpcCall(id, path, MethodDescriptor.MethodType.CLIENT_STREAMING, headers, resolveDeadline(obj, state), state);
|
|
229
361
|
} catch (Exception e) {
|
|
230
362
|
promise.reject(e);
|
|
231
363
|
|
|
@@ -244,12 +376,20 @@ public class GrpcModule extends ReactContextBaseJavaModule {
|
|
|
244
376
|
}
|
|
245
377
|
|
|
246
378
|
@ReactMethod
|
|
247
|
-
public void bidiStreamingCall(
|
|
379
|
+
public void bidiStreamingCall(
|
|
380
|
+
int id,
|
|
381
|
+
String path,
|
|
382
|
+
ReadableMap obj,
|
|
383
|
+
ReadableMap headers,
|
|
384
|
+
String channelId,
|
|
385
|
+
final Promise promise
|
|
386
|
+
) {
|
|
248
387
|
ClientCall call = callsMap.get(id);
|
|
249
388
|
|
|
250
389
|
if (call == null) {
|
|
251
390
|
try {
|
|
252
|
-
|
|
391
|
+
ChannelState state = requireChannel(channelId);
|
|
392
|
+
call = this.startGrpcCall(id, path, MethodDescriptor.MethodType.BIDI_STREAMING, headers, resolveDeadline(obj, state), state);
|
|
253
393
|
} catch (Exception e) {
|
|
254
394
|
promise.reject(e);
|
|
255
395
|
|
|
@@ -292,12 +432,12 @@ public class GrpcModule extends ReactContextBaseJavaModule {
|
|
|
292
432
|
}
|
|
293
433
|
}
|
|
294
434
|
|
|
295
|
-
/** Resolve per-call deadline: obj.deadlineSeconds if set, else
|
|
296
|
-
private long resolveDeadline(ReadableMap obj) {
|
|
435
|
+
/** Resolve per-call deadline: obj.deadlineSeconds if set, else channel default. */
|
|
436
|
+
private long resolveDeadline(ReadableMap obj, ChannelState state) {
|
|
297
437
|
if (obj != null && obj.hasKey("deadlineSeconds") && !obj.isNull("deadlineSeconds")) {
|
|
298
438
|
return Math.max(0, (long) obj.getDouble("deadlineSeconds"));
|
|
299
439
|
}
|
|
300
|
-
return
|
|
440
|
+
return state.callDeadlineSeconds;
|
|
301
441
|
}
|
|
302
442
|
|
|
303
443
|
private ClientCall startGrpcCall(
|
|
@@ -305,9 +445,10 @@ public class GrpcModule extends ReactContextBaseJavaModule {
|
|
|
305
445
|
String path,
|
|
306
446
|
MethodDescriptor.MethodType methodType,
|
|
307
447
|
ReadableMap headers,
|
|
308
|
-
long deadlineSeconds
|
|
448
|
+
long deadlineSeconds,
|
|
449
|
+
ChannelState state
|
|
309
450
|
) throws Exception {
|
|
310
|
-
if (
|
|
451
|
+
if (state.managedChannel == null) {
|
|
311
452
|
throw new Exception("Channel not created");
|
|
312
453
|
}
|
|
313
454
|
|
|
@@ -330,14 +471,14 @@ public class GrpcModule extends ReactContextBaseJavaModule {
|
|
|
330
471
|
|
|
331
472
|
CallOptions callOptions = CallOptions.DEFAULT;
|
|
332
473
|
|
|
333
|
-
if (!
|
|
334
|
-
callOptions = callOptions.withCompression(
|
|
474
|
+
if (!state.compressorName.isEmpty()) {
|
|
475
|
+
callOptions = callOptions.withCompression(state.compressorName);
|
|
335
476
|
}
|
|
336
477
|
if (deadlineSeconds > 0) {
|
|
337
478
|
callOptions = callOptions.withDeadlineAfter(deadlineSeconds, TimeUnit.SECONDS);
|
|
338
479
|
}
|
|
339
480
|
|
|
340
|
-
ClientCall call =
|
|
481
|
+
ClientCall call = state.managedChannel.newCall(descriptor, callOptions);
|
|
341
482
|
|
|
342
483
|
call.start(new ClientCall.Listener() {
|
|
343
484
|
@Override
|
|
@@ -423,7 +564,7 @@ public class GrpcModule extends ReactContextBaseJavaModule {
|
|
|
423
564
|
}
|
|
424
565
|
}, headersMetadata);
|
|
425
566
|
|
|
426
|
-
if (
|
|
567
|
+
if (state.withCompression) {
|
|
427
568
|
call.setMessageCompression(true);
|
|
428
569
|
}
|
|
429
570
|
|
|
@@ -453,22 +594,23 @@ public class GrpcModule extends ReactContextBaseJavaModule {
|
|
|
453
594
|
|
|
454
595
|
@ReactMethod
|
|
455
596
|
public void initGrpcChannel() {
|
|
456
|
-
|
|
457
|
-
|
|
597
|
+
ChannelState state = defaultChannel();
|
|
598
|
+
if (state.managedChannel != null) {
|
|
599
|
+
state.managedChannel.shutdown();
|
|
458
600
|
}
|
|
459
|
-
|
|
601
|
+
state.managedChannel = buildManagedChannel(state);
|
|
460
602
|
}
|
|
461
603
|
|
|
462
|
-
private ManagedChannel
|
|
604
|
+
private ManagedChannel buildManagedChannel(ChannelState state) {
|
|
463
605
|
ManagedChannelBuilder<?> channelBuilder;
|
|
464
606
|
|
|
465
|
-
if (
|
|
466
|
-
channelBuilder = ManagedChannelBuilder.forTarget(
|
|
607
|
+
if (state.isInsecure) {
|
|
608
|
+
channelBuilder = ManagedChannelBuilder.forTarget(state.host).usePlaintext();
|
|
467
609
|
} else {
|
|
468
|
-
boolean hasRoots =
|
|
469
|
-
boolean hasCert =
|
|
470
|
-
boolean hasKey =
|
|
471
|
-
boolean hasPins =
|
|
610
|
+
boolean hasRoots = state.rootCertsPem != null && !state.rootCertsPem.isEmpty();
|
|
611
|
+
boolean hasCert = state.certificateChainPem != null && !state.certificateChainPem.isEmpty();
|
|
612
|
+
boolean hasKey = state.privateKeyPem != null && !state.privateKeyPem.isEmpty();
|
|
613
|
+
boolean hasPins = state.spkiSha256Pins != null && !state.spkiSha256Pins.isEmpty();
|
|
472
614
|
|
|
473
615
|
if (hasCert != hasKey) {
|
|
474
616
|
throw new IllegalArgumentException(
|
|
@@ -481,21 +623,21 @@ public class GrpcModule extends ReactContextBaseJavaModule {
|
|
|
481
623
|
TlsChannelCredentials.Builder tls = TlsChannelCredentials.newBuilder();
|
|
482
624
|
if (hasRoots || hasPins) {
|
|
483
625
|
X509TrustManager base = hasRoots
|
|
484
|
-
? TrustManagers.fromPemRoots(
|
|
626
|
+
? TrustManagers.fromPemRoots(state.rootCertsPem)
|
|
485
627
|
: TrustManagers.systemTrustManager();
|
|
486
628
|
if (hasPins) {
|
|
487
|
-
tls.trustManager(new SpkiPinTrustManager(base,
|
|
629
|
+
tls.trustManager(new SpkiPinTrustManager(base, state.spkiSha256Pins));
|
|
488
630
|
} else {
|
|
489
631
|
tls.trustManager(base);
|
|
490
632
|
}
|
|
491
633
|
}
|
|
492
634
|
if (hasCert) {
|
|
493
635
|
tls.keyManager(
|
|
494
|
-
new ByteArrayInputStream(
|
|
495
|
-
new ByteArrayInputStream(
|
|
636
|
+
new ByteArrayInputStream(state.certificateChainPem.getBytes(StandardCharsets.UTF_8)),
|
|
637
|
+
new ByteArrayInputStream(state.privateKeyPem.getBytes(StandardCharsets.UTF_8))
|
|
496
638
|
);
|
|
497
639
|
}
|
|
498
|
-
channelBuilder = Grpc.newChannelBuilder(
|
|
640
|
+
channelBuilder = Grpc.newChannelBuilder(state.host, tls.build());
|
|
499
641
|
} catch (IOException e) {
|
|
500
642
|
throw new RuntimeException("Failed to configure TLS credentials", e);
|
|
501
643
|
} catch (RuntimeException e) {
|
|
@@ -504,34 +646,34 @@ public class GrpcModule extends ReactContextBaseJavaModule {
|
|
|
504
646
|
throw new RuntimeException("Failed to configure TLS credentials", e);
|
|
505
647
|
}
|
|
506
648
|
} else {
|
|
507
|
-
channelBuilder = ManagedChannelBuilder.forTarget(
|
|
649
|
+
channelBuilder = ManagedChannelBuilder.forTarget(state.host);
|
|
508
650
|
}
|
|
509
651
|
|
|
510
|
-
if (
|
|
511
|
-
channelBuilder = channelBuilder.overrideAuthority(
|
|
652
|
+
if (state.hostNameOverride != null && !state.hostNameOverride.isEmpty()) {
|
|
653
|
+
channelBuilder = channelBuilder.overrideAuthority(state.hostNameOverride);
|
|
512
654
|
}
|
|
513
655
|
}
|
|
514
656
|
|
|
515
|
-
if (
|
|
516
|
-
channelBuilder = channelBuilder.maxInboundMessageSize(
|
|
657
|
+
if (state.responseSizeLimit != null) {
|
|
658
|
+
channelBuilder = channelBuilder.maxInboundMessageSize(state.responseSizeLimit);
|
|
517
659
|
}
|
|
518
660
|
|
|
519
|
-
if (
|
|
661
|
+
if (state.keepAliveEnabled) {
|
|
520
662
|
channelBuilder = channelBuilder
|
|
521
663
|
.keepAliveWithoutCalls(true)
|
|
522
|
-
.keepAliveTime(keepAliveTime, TimeUnit.SECONDS)
|
|
523
|
-
.keepAliveTimeout(keepAliveTimeout, TimeUnit.SECONDS);
|
|
664
|
+
.keepAliveTime(state.keepAliveTime, TimeUnit.SECONDS)
|
|
665
|
+
.keepAliveTimeout(state.keepAliveTimeout, TimeUnit.SECONDS);
|
|
524
666
|
}
|
|
525
667
|
|
|
526
|
-
|
|
527
|
-
return managedChannel;
|
|
668
|
+
return channelBuilder.build();
|
|
528
669
|
}
|
|
529
670
|
|
|
530
671
|
@ReactMethod
|
|
531
672
|
public void resetConnection(final String message){
|
|
532
|
-
|
|
673
|
+
ChannelState state = defaultChannel();
|
|
674
|
+
if(null == state.managedChannel) return;
|
|
533
675
|
|
|
534
|
-
|
|
676
|
+
state.managedChannel.resetConnectBackoff();
|
|
535
677
|
|
|
536
678
|
this.initGrpcChannel();
|
|
537
679
|
|
|
@@ -540,6 +682,8 @@ public class GrpcModule extends ReactContextBaseJavaModule {
|
|
|
540
682
|
|
|
541
683
|
@ReactMethod
|
|
542
684
|
public void onConnectionStateChange(){
|
|
685
|
+
ChannelState state = defaultChannel();
|
|
686
|
+
ManagedChannel managedChannel = state.managedChannel;
|
|
543
687
|
if(null == managedChannel) return;
|
|
544
688
|
|
|
545
689
|
final ConnectivityState connectivityState = managedChannel.getState(true);
|
|
@@ -554,7 +698,7 @@ public class GrpcModule extends ReactContextBaseJavaModule {
|
|
|
554
698
|
} else if(ConnectivityState.SHUTDOWN == connectivityState){
|
|
555
699
|
showToast("onConnectionState SHUTDOWN");
|
|
556
700
|
} else {
|
|
557
|
-
showToast("onConnectionState
|
|
701
|
+
showToast("onConnectionState UNKNOWN");
|
|
558
702
|
}
|
|
559
703
|
if(ConnectivityState.TRANSIENT_FAILURE == connectivityState && managedChannel.isTerminated() || managedChannel.isShutdown()){
|
|
560
704
|
resetConnection("onConnectionStateChange");
|
|
@@ -563,9 +707,10 @@ public class GrpcModule extends ReactContextBaseJavaModule {
|
|
|
563
707
|
|
|
564
708
|
@ReactMethod
|
|
565
709
|
public void enterIdle(){
|
|
566
|
-
|
|
710
|
+
ChannelState state = defaultChannel();
|
|
711
|
+
if(null == state.managedChannel) return;
|
|
567
712
|
|
|
568
|
-
managedChannel.enterIdle();
|
|
713
|
+
state.managedChannel.enterIdle();
|
|
569
714
|
|
|
570
715
|
showToast("enterIdle");
|
|
571
716
|
}
|
package/ios/Grpc.h
CHANGED