@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
|
@@ -1,730 +1,730 @@
|
|
|
1
|
-
package com.reactnativegrpc;
|
|
2
|
-
|
|
3
|
-
import android.util.Base64;
|
|
4
|
-
import android.util.Log;
|
|
5
|
-
import android.widget.Toast;
|
|
6
|
-
|
|
7
|
-
import androidx.annotation.NonNull;
|
|
8
|
-
import androidx.annotation.UiThread;
|
|
9
|
-
|
|
10
|
-
import com.facebook.react.bridge.Arguments;
|
|
11
|
-
import com.facebook.react.bridge.Promise;
|
|
12
|
-
import com.facebook.react.bridge.ReactApplicationContext;
|
|
13
|
-
import com.facebook.react.bridge.ReactContextBaseJavaModule;
|
|
14
|
-
import com.facebook.react.bridge.ReactMethod;
|
|
15
|
-
import com.facebook.react.bridge.ReadableArray;
|
|
16
|
-
import com.facebook.react.bridge.ReadableMap;
|
|
17
|
-
import com.facebook.react.bridge.WritableMap;
|
|
18
|
-
import com.facebook.react.modules.core.DeviceEventManagerModule;
|
|
19
|
-
|
|
20
|
-
import java.io.ByteArrayInputStream;
|
|
21
|
-
import java.io.IOException;
|
|
22
|
-
import java.nio.charset.StandardCharsets;
|
|
23
|
-
import java.util.ArrayList;
|
|
24
|
-
import java.util.HashMap;
|
|
25
|
-
import java.util.List;
|
|
26
|
-
import java.util.Map;
|
|
27
|
-
import java.util.concurrent.TimeUnit;
|
|
28
|
-
|
|
29
|
-
import javax.net.ssl.X509TrustManager;
|
|
30
|
-
|
|
31
|
-
import io.grpc.CallOptions;
|
|
32
|
-
import io.grpc.ClientCall;
|
|
33
|
-
import io.grpc.ConnectivityState;
|
|
34
|
-
import io.grpc.Grpc;
|
|
35
|
-
import io.grpc.ManagedChannel;
|
|
36
|
-
import io.grpc.ManagedChannelBuilder;
|
|
37
|
-
import io.grpc.Metadata;
|
|
38
|
-
import io.grpc.MethodDescriptor;
|
|
39
|
-
import io.grpc.Status;
|
|
40
|
-
import io.grpc.TlsChannelCredentials;
|
|
41
|
-
|
|
42
|
-
public class GrpcModule extends ReactContextBaseJavaModule {
|
|
43
|
-
static final String DEFAULT_CHANNEL_ID = "default";
|
|
44
|
-
|
|
45
|
-
private final ReactApplicationContext context;
|
|
46
|
-
private final HashMap<Integer, ClientCall> callsMap = new HashMap<>();
|
|
47
|
-
private final HashMap<String, ChannelState> channels = new HashMap<>();
|
|
48
|
-
|
|
49
|
-
private boolean isUiLogEnabled = false;
|
|
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
|
-
}
|
|
76
|
-
|
|
77
|
-
public GrpcModule(ReactApplicationContext context) {
|
|
78
|
-
this.context = context;
|
|
79
|
-
channels.put(DEFAULT_CHANNEL_ID, new ChannelState());
|
|
80
|
-
}
|
|
81
|
-
|
|
82
|
-
@NonNull
|
|
83
|
-
@Override
|
|
84
|
-
public String getName() {
|
|
85
|
-
return "Grpc";
|
|
86
|
-
}
|
|
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
|
-
|
|
106
|
-
@ReactMethod()
|
|
107
|
-
public void getHost(final Promise promise) {
|
|
108
|
-
promise.resolve(defaultChannel().host);
|
|
109
|
-
}
|
|
110
|
-
|
|
111
|
-
@ReactMethod()
|
|
112
|
-
public void getIsInsecure(final Promise promise) {
|
|
113
|
-
promise.resolve(defaultChannel().isInsecure);
|
|
114
|
-
}
|
|
115
|
-
|
|
116
|
-
@ReactMethod
|
|
117
|
-
public void setHost(String host) {
|
|
118
|
-
defaultChannel().host = host;
|
|
119
|
-
}
|
|
120
|
-
|
|
121
|
-
@ReactMethod
|
|
122
|
-
public void setInsecure(boolean insecure) {
|
|
123
|
-
defaultChannel().isInsecure = insecure;
|
|
124
|
-
}
|
|
125
|
-
|
|
126
|
-
/**
|
|
127
|
-
* Replace TLS configuration (custom CA / mTLS / hostname override).
|
|
128
|
-
* Omitted or null fields clear the previous value. Apply before initGrpcChannel().
|
|
129
|
-
*/
|
|
130
|
-
@ReactMethod
|
|
131
|
-
public void setTlsOptions(ReadableMap options) {
|
|
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(
|
|
138
|
-
readOptionalStringList(options, "spkiSha256Pins")
|
|
139
|
-
);
|
|
140
|
-
|
|
141
|
-
boolean hasCert = state.certificateChainPem != null && !state.certificateChainPem.isEmpty();
|
|
142
|
-
boolean hasKey = state.privateKeyPem != null && !state.privateKeyPem.isEmpty();
|
|
143
|
-
if (hasCert != hasKey) {
|
|
144
|
-
throw new IllegalArgumentException(
|
|
145
|
-
"mTLS requires both certificateChainPem and privateKeyPem"
|
|
146
|
-
);
|
|
147
|
-
}
|
|
148
|
-
}
|
|
149
|
-
|
|
150
|
-
private static String readOptionalString(ReadableMap options, String key) {
|
|
151
|
-
if (options == null || !options.hasKey(key) || options.isNull(key)) {
|
|
152
|
-
return null;
|
|
153
|
-
}
|
|
154
|
-
String value = options.getString(key);
|
|
155
|
-
if (value == null || value.isEmpty()) {
|
|
156
|
-
return null;
|
|
157
|
-
}
|
|
158
|
-
return value;
|
|
159
|
-
}
|
|
160
|
-
|
|
161
|
-
private static List<String> readOptionalStringList(ReadableMap options, String key) {
|
|
162
|
-
if (options == null || !options.hasKey(key) || options.isNull(key)) {
|
|
163
|
-
return null;
|
|
164
|
-
}
|
|
165
|
-
ReadableArray array = options.getArray(key);
|
|
166
|
-
if (array == null || array.size() == 0) {
|
|
167
|
-
return null;
|
|
168
|
-
}
|
|
169
|
-
List<String> values = new ArrayList<>();
|
|
170
|
-
for (int i = 0; i < array.size(); i++) {
|
|
171
|
-
String value = array.getString(i);
|
|
172
|
-
if (value != null && !value.isEmpty()) {
|
|
173
|
-
values.add(value);
|
|
174
|
-
}
|
|
175
|
-
}
|
|
176
|
-
return values.isEmpty() ? null : values;
|
|
177
|
-
}
|
|
178
|
-
|
|
179
|
-
@ReactMethod
|
|
180
|
-
public void setCompression(Boolean enable, String compressorName) {
|
|
181
|
-
ChannelState state = defaultChannel();
|
|
182
|
-
state.withCompression = enable;
|
|
183
|
-
state.compressorName = compressorName;
|
|
184
|
-
}
|
|
185
|
-
|
|
186
|
-
@ReactMethod
|
|
187
|
-
public void setResponseSizeLimit(int limit) {
|
|
188
|
-
defaultChannel().responseSizeLimit = limit;
|
|
189
|
-
}
|
|
190
|
-
|
|
191
|
-
@ReactMethod
|
|
192
|
-
public void setKeepAlive(boolean enabled, int time, int timeout) {
|
|
193
|
-
ChannelState state = defaultChannel();
|
|
194
|
-
state.keepAliveEnabled = enabled;
|
|
195
|
-
state.keepAliveTime = time;
|
|
196
|
-
state.keepAliveTimeout = timeout;
|
|
197
|
-
}
|
|
198
|
-
|
|
199
|
-
@ReactMethod
|
|
200
|
-
public void setCallDeadlineSeconds(double seconds) {
|
|
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
|
-
}
|
|
282
|
-
}
|
|
283
|
-
|
|
284
|
-
@ReactMethod
|
|
285
|
-
public void unaryCall(
|
|
286
|
-
int id,
|
|
287
|
-
String path,
|
|
288
|
-
ReadableMap obj,
|
|
289
|
-
ReadableMap headers,
|
|
290
|
-
String channelId,
|
|
291
|
-
final Promise promise
|
|
292
|
-
) {
|
|
293
|
-
ClientCall call;
|
|
294
|
-
|
|
295
|
-
try {
|
|
296
|
-
ChannelState state = requireChannel(channelId);
|
|
297
|
-
call = this.startGrpcCall(id, path, MethodDescriptor.MethodType.UNARY, headers, resolveDeadline(obj, state), state);
|
|
298
|
-
} catch (Exception e) {
|
|
299
|
-
promise.reject(e);
|
|
300
|
-
|
|
301
|
-
return;
|
|
302
|
-
}
|
|
303
|
-
|
|
304
|
-
byte[] data = Base64.decode(obj.getString("data"), Base64.NO_WRAP);
|
|
305
|
-
|
|
306
|
-
call.sendMessage(data);
|
|
307
|
-
call.request(1);
|
|
308
|
-
call.halfClose();
|
|
309
|
-
|
|
310
|
-
callsMap.put(id, call);
|
|
311
|
-
|
|
312
|
-
promise.resolve(null);
|
|
313
|
-
}
|
|
314
|
-
|
|
315
|
-
@ReactMethod
|
|
316
|
-
public void serverStreamingCall(
|
|
317
|
-
int id,
|
|
318
|
-
String path,
|
|
319
|
-
ReadableMap obj,
|
|
320
|
-
ReadableMap headers,
|
|
321
|
-
String channelId,
|
|
322
|
-
final Promise promise
|
|
323
|
-
) {
|
|
324
|
-
ClientCall call;
|
|
325
|
-
|
|
326
|
-
try {
|
|
327
|
-
ChannelState state = requireChannel(channelId);
|
|
328
|
-
call = this.startGrpcCall(id, path, MethodDescriptor.MethodType.SERVER_STREAMING, headers, resolveDeadline(obj, state), state);
|
|
329
|
-
} catch (Exception e) {
|
|
330
|
-
promise.reject(e);
|
|
331
|
-
|
|
332
|
-
return;
|
|
333
|
-
}
|
|
334
|
-
|
|
335
|
-
byte[] data = Base64.decode(obj.getString("data"), Base64.NO_WRAP);
|
|
336
|
-
|
|
337
|
-
call.sendMessage(data);
|
|
338
|
-
call.request(1);
|
|
339
|
-
call.halfClose();
|
|
340
|
-
|
|
341
|
-
callsMap.put(id, call);
|
|
342
|
-
|
|
343
|
-
promise.resolve(null);
|
|
344
|
-
}
|
|
345
|
-
|
|
346
|
-
@ReactMethod
|
|
347
|
-
public void clientStreamingCall(
|
|
348
|
-
int id,
|
|
349
|
-
String path,
|
|
350
|
-
ReadableMap obj,
|
|
351
|
-
ReadableMap headers,
|
|
352
|
-
String channelId,
|
|
353
|
-
final Promise promise
|
|
354
|
-
) {
|
|
355
|
-
ClientCall call = callsMap.get(id);
|
|
356
|
-
|
|
357
|
-
if (call == null) {
|
|
358
|
-
try {
|
|
359
|
-
ChannelState state = requireChannel(channelId);
|
|
360
|
-
call = this.startGrpcCall(id, path, MethodDescriptor.MethodType.CLIENT_STREAMING, headers, resolveDeadline(obj, state), state);
|
|
361
|
-
} catch (Exception e) {
|
|
362
|
-
promise.reject(e);
|
|
363
|
-
|
|
364
|
-
return;
|
|
365
|
-
}
|
|
366
|
-
|
|
367
|
-
callsMap.put(id, call);
|
|
368
|
-
}
|
|
369
|
-
|
|
370
|
-
byte[] data = Base64.decode(obj.getString("data"), Base64.NO_WRAP);
|
|
371
|
-
|
|
372
|
-
call.sendMessage(data);
|
|
373
|
-
call.request(1);
|
|
374
|
-
|
|
375
|
-
promise.resolve(null);
|
|
376
|
-
}
|
|
377
|
-
|
|
378
|
-
@ReactMethod
|
|
379
|
-
public void bidiStreamingCall(
|
|
380
|
-
int id,
|
|
381
|
-
String path,
|
|
382
|
-
ReadableMap obj,
|
|
383
|
-
ReadableMap headers,
|
|
384
|
-
String channelId,
|
|
385
|
-
final Promise promise
|
|
386
|
-
) {
|
|
387
|
-
ClientCall call = callsMap.get(id);
|
|
388
|
-
|
|
389
|
-
if (call == null) {
|
|
390
|
-
try {
|
|
391
|
-
ChannelState state = requireChannel(channelId);
|
|
392
|
-
call = this.startGrpcCall(id, path, MethodDescriptor.MethodType.BIDI_STREAMING, headers, resolveDeadline(obj, state), state);
|
|
393
|
-
} catch (Exception e) {
|
|
394
|
-
promise.reject(e);
|
|
395
|
-
|
|
396
|
-
return;
|
|
397
|
-
}
|
|
398
|
-
|
|
399
|
-
callsMap.put(id, call);
|
|
400
|
-
}
|
|
401
|
-
|
|
402
|
-
byte[] data = Base64.decode(obj.getString("data"), Base64.NO_WRAP);
|
|
403
|
-
|
|
404
|
-
call.sendMessage(data);
|
|
405
|
-
call.request(1);
|
|
406
|
-
|
|
407
|
-
promise.resolve(null);
|
|
408
|
-
}
|
|
409
|
-
|
|
410
|
-
@ReactMethod
|
|
411
|
-
public void finishClientStreaming(int id, final Promise promise) {
|
|
412
|
-
if (callsMap.containsKey(id)) {
|
|
413
|
-
ClientCall call = callsMap.get(id);
|
|
414
|
-
|
|
415
|
-
call.halfClose();
|
|
416
|
-
|
|
417
|
-
promise.resolve(true);
|
|
418
|
-
} else {
|
|
419
|
-
promise.resolve(false);
|
|
420
|
-
}
|
|
421
|
-
}
|
|
422
|
-
|
|
423
|
-
@ReactMethod
|
|
424
|
-
public void cancelGrpcCall(int id, final Promise promise) {
|
|
425
|
-
if (callsMap.containsKey(id)) {
|
|
426
|
-
ClientCall call = callsMap.get(id);
|
|
427
|
-
call.cancel("Cancelled", new Exception("Cancelled by app"));
|
|
428
|
-
|
|
429
|
-
promise.resolve(true);
|
|
430
|
-
} else {
|
|
431
|
-
promise.resolve(false);
|
|
432
|
-
}
|
|
433
|
-
}
|
|
434
|
-
|
|
435
|
-
/** Resolve per-call deadline: obj.deadlineSeconds if set, else channel default. */
|
|
436
|
-
private long resolveDeadline(ReadableMap obj, ChannelState state) {
|
|
437
|
-
if (obj != null && obj.hasKey("deadlineSeconds") && !obj.isNull("deadlineSeconds")) {
|
|
438
|
-
return Math.max(0, (long) obj.getDouble("deadlineSeconds"));
|
|
439
|
-
}
|
|
440
|
-
return state.callDeadlineSeconds;
|
|
441
|
-
}
|
|
442
|
-
|
|
443
|
-
private ClientCall startGrpcCall(
|
|
444
|
-
int id,
|
|
445
|
-
String path,
|
|
446
|
-
MethodDescriptor.MethodType methodType,
|
|
447
|
-
ReadableMap headers,
|
|
448
|
-
long deadlineSeconds,
|
|
449
|
-
ChannelState state
|
|
450
|
-
) throws Exception {
|
|
451
|
-
if (state.managedChannel == null) {
|
|
452
|
-
throw new Exception("Channel not created");
|
|
453
|
-
}
|
|
454
|
-
|
|
455
|
-
path = normalizePath(path);
|
|
456
|
-
|
|
457
|
-
final Metadata headersMetadata = new Metadata();
|
|
458
|
-
|
|
459
|
-
for (Map.Entry<String, Object> headerEntry : headers.toHashMap().entrySet()) {
|
|
460
|
-
headersMetadata.put(Metadata.Key.of(headerEntry.getKey(), Metadata.ASCII_STRING_MARSHALLER), headerEntry.getValue().toString());
|
|
461
|
-
}
|
|
462
|
-
|
|
463
|
-
MethodDescriptor.Marshaller<byte[]> marshaller = new GrpcMarshaller();
|
|
464
|
-
|
|
465
|
-
MethodDescriptor descriptor = MethodDescriptor.<byte[], byte[]>newBuilder()
|
|
466
|
-
.setFullMethodName(path)
|
|
467
|
-
.setType(methodType)
|
|
468
|
-
.setRequestMarshaller(marshaller)
|
|
469
|
-
.setResponseMarshaller(marshaller)
|
|
470
|
-
.build();
|
|
471
|
-
|
|
472
|
-
CallOptions callOptions = CallOptions.DEFAULT;
|
|
473
|
-
|
|
474
|
-
if (!state.compressorName.isEmpty()) {
|
|
475
|
-
callOptions = callOptions.withCompression(state.compressorName);
|
|
476
|
-
}
|
|
477
|
-
if (deadlineSeconds > 0) {
|
|
478
|
-
callOptions = callOptions.withDeadlineAfter(deadlineSeconds, TimeUnit.SECONDS);
|
|
479
|
-
}
|
|
480
|
-
|
|
481
|
-
ClientCall call = state.managedChannel.newCall(descriptor, callOptions);
|
|
482
|
-
|
|
483
|
-
call.start(new ClientCall.Listener() {
|
|
484
|
-
@Override
|
|
485
|
-
public void onHeaders(Metadata headers) {
|
|
486
|
-
super.onHeaders(headers);
|
|
487
|
-
|
|
488
|
-
WritableMap event = Arguments.createMap();
|
|
489
|
-
WritableMap payload = Arguments.createMap();
|
|
490
|
-
|
|
491
|
-
for (String key : headers.keys()) {
|
|
492
|
-
if (key.endsWith(Metadata.BINARY_HEADER_SUFFIX)) {
|
|
493
|
-
byte[] data = headers.get(Metadata.Key.of(key, Metadata.BINARY_BYTE_MARSHALLER));
|
|
494
|
-
|
|
495
|
-
payload.putString(key, new String(Base64.encode(data, Base64.NO_WRAP)));
|
|
496
|
-
} else if (!key.startsWith(":")) {
|
|
497
|
-
String data = headers.get(Metadata.Key.of(key, Metadata.ASCII_STRING_MARSHALLER));
|
|
498
|
-
|
|
499
|
-
payload.putString(key, data);
|
|
500
|
-
}
|
|
501
|
-
}
|
|
502
|
-
|
|
503
|
-
event.putInt("id", id);
|
|
504
|
-
event.putString("type", "headers");
|
|
505
|
-
event.putMap("payload", payload);
|
|
506
|
-
|
|
507
|
-
emitEvent("grpc-call", event);
|
|
508
|
-
}
|
|
509
|
-
|
|
510
|
-
@Override
|
|
511
|
-
public void onMessage(Object messageObj) {
|
|
512
|
-
super.onMessage(messageObj);
|
|
513
|
-
|
|
514
|
-
byte[] data = (byte[]) messageObj;
|
|
515
|
-
|
|
516
|
-
WritableMap event = Arguments.createMap();
|
|
517
|
-
|
|
518
|
-
event.putInt("id", id);
|
|
519
|
-
event.putString("type", "response");
|
|
520
|
-
event.putString("payload", Base64.encodeToString(data, Base64.NO_WRAP));
|
|
521
|
-
|
|
522
|
-
emitEvent("grpc-call", event);
|
|
523
|
-
|
|
524
|
-
if (methodType == MethodDescriptor.MethodType.SERVER_STREAMING
|
|
525
|
-
|| methodType == MethodDescriptor.MethodType.BIDI_STREAMING) {
|
|
526
|
-
call.request(1);
|
|
527
|
-
}
|
|
528
|
-
}
|
|
529
|
-
|
|
530
|
-
@Override
|
|
531
|
-
public void onClose(Status status, Metadata trailers) {
|
|
532
|
-
super.onClose(status, trailers);
|
|
533
|
-
|
|
534
|
-
callsMap.remove(id);
|
|
535
|
-
|
|
536
|
-
WritableMap event = Arguments.createMap();
|
|
537
|
-
event.putInt("id", id);
|
|
538
|
-
|
|
539
|
-
WritableMap trailersMap = Arguments.createMap();
|
|
540
|
-
|
|
541
|
-
for (String key : trailers.keys()) {
|
|
542
|
-
if (key.endsWith(Metadata.BINARY_HEADER_SUFFIX)) {
|
|
543
|
-
byte[] data = trailers.get(Metadata.Key.of(key, Metadata.BINARY_BYTE_MARSHALLER));
|
|
544
|
-
|
|
545
|
-
trailersMap.putString(key, new String(Base64.encode(data, Base64.NO_WRAP)));
|
|
546
|
-
} else if (!key.startsWith(":")) {
|
|
547
|
-
String data = trailers.get(Metadata.Key.of(key, Metadata.ASCII_STRING_MARSHALLER));
|
|
548
|
-
|
|
549
|
-
trailersMap.putString(key, data);
|
|
550
|
-
}
|
|
551
|
-
}
|
|
552
|
-
|
|
553
|
-
if (!status.isOk()) {
|
|
554
|
-
event.putString("type", "error");
|
|
555
|
-
event.putString("error", status.asException(trailers).getLocalizedMessage());
|
|
556
|
-
event.putInt("code", status.getCode().value());
|
|
557
|
-
event.putMap("trailers", trailersMap);
|
|
558
|
-
} else {
|
|
559
|
-
event.putString("type", "trailers");
|
|
560
|
-
event.putMap("payload", trailersMap);
|
|
561
|
-
}
|
|
562
|
-
|
|
563
|
-
emitEvent("grpc-call", event);
|
|
564
|
-
}
|
|
565
|
-
}, headersMetadata);
|
|
566
|
-
|
|
567
|
-
if (state.withCompression) {
|
|
568
|
-
call.setMessageCompression(true);
|
|
569
|
-
}
|
|
570
|
-
|
|
571
|
-
return call;
|
|
572
|
-
}
|
|
573
|
-
|
|
574
|
-
@ReactMethod
|
|
575
|
-
public void addListener(String eventName) {
|
|
576
|
-
}
|
|
577
|
-
|
|
578
|
-
@ReactMethod
|
|
579
|
-
public void removeListeners(Integer count) {
|
|
580
|
-
}
|
|
581
|
-
|
|
582
|
-
private void emitEvent(String eventName, Object params) {
|
|
583
|
-
context
|
|
584
|
-
.getJSModule(DeviceEventManagerModule.RCTDeviceEventEmitter.class)
|
|
585
|
-
.emit(eventName, params);
|
|
586
|
-
}
|
|
587
|
-
|
|
588
|
-
private static String normalizePath(String path) {
|
|
589
|
-
if (path.startsWith("/")) {
|
|
590
|
-
path = path.substring(1);
|
|
591
|
-
}
|
|
592
|
-
return path;
|
|
593
|
-
}
|
|
594
|
-
|
|
595
|
-
@ReactMethod
|
|
596
|
-
public void initGrpcChannel() {
|
|
597
|
-
ChannelState state = defaultChannel();
|
|
598
|
-
if (state.managedChannel != null) {
|
|
599
|
-
state.managedChannel.shutdown();
|
|
600
|
-
}
|
|
601
|
-
state.managedChannel = buildManagedChannel(state);
|
|
602
|
-
}
|
|
603
|
-
|
|
604
|
-
private ManagedChannel buildManagedChannel(ChannelState state) {
|
|
605
|
-
ManagedChannelBuilder<?> channelBuilder;
|
|
606
|
-
|
|
607
|
-
if (state.isInsecure) {
|
|
608
|
-
channelBuilder = ManagedChannelBuilder.forTarget(state.host).usePlaintext();
|
|
609
|
-
} else {
|
|
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();
|
|
614
|
-
|
|
615
|
-
if (hasCert != hasKey) {
|
|
616
|
-
throw new IllegalArgumentException(
|
|
617
|
-
"mTLS requires both certificateChainPem and privateKeyPem"
|
|
618
|
-
);
|
|
619
|
-
}
|
|
620
|
-
|
|
621
|
-
if (hasRoots || hasCert || hasPins) {
|
|
622
|
-
try {
|
|
623
|
-
TlsChannelCredentials.Builder tls = TlsChannelCredentials.newBuilder();
|
|
624
|
-
if (hasRoots || hasPins) {
|
|
625
|
-
X509TrustManager base = hasRoots
|
|
626
|
-
? TrustManagers.fromPemRoots(state.rootCertsPem)
|
|
627
|
-
: TrustManagers.systemTrustManager();
|
|
628
|
-
if (hasPins) {
|
|
629
|
-
tls.trustManager(new SpkiPinTrustManager(base, state.spkiSha256Pins));
|
|
630
|
-
} else {
|
|
631
|
-
tls.trustManager(base);
|
|
632
|
-
}
|
|
633
|
-
}
|
|
634
|
-
if (hasCert) {
|
|
635
|
-
tls.keyManager(
|
|
636
|
-
new ByteArrayInputStream(state.certificateChainPem.getBytes(StandardCharsets.UTF_8)),
|
|
637
|
-
new ByteArrayInputStream(state.privateKeyPem.getBytes(StandardCharsets.UTF_8))
|
|
638
|
-
);
|
|
639
|
-
}
|
|
640
|
-
channelBuilder = Grpc.newChannelBuilder(state.host, tls.build());
|
|
641
|
-
} catch (IOException e) {
|
|
642
|
-
throw new RuntimeException("Failed to configure TLS credentials", e);
|
|
643
|
-
} catch (RuntimeException e) {
|
|
644
|
-
throw e;
|
|
645
|
-
} catch (Exception e) {
|
|
646
|
-
throw new RuntimeException("Failed to configure TLS credentials", e);
|
|
647
|
-
}
|
|
648
|
-
} else {
|
|
649
|
-
channelBuilder = ManagedChannelBuilder.forTarget(state.host);
|
|
650
|
-
}
|
|
651
|
-
|
|
652
|
-
if (state.hostNameOverride != null && !state.hostNameOverride.isEmpty()) {
|
|
653
|
-
channelBuilder = channelBuilder.overrideAuthority(state.hostNameOverride);
|
|
654
|
-
}
|
|
655
|
-
}
|
|
656
|
-
|
|
657
|
-
if (state.responseSizeLimit != null) {
|
|
658
|
-
channelBuilder = channelBuilder.maxInboundMessageSize(state.responseSizeLimit);
|
|
659
|
-
}
|
|
660
|
-
|
|
661
|
-
if (state.keepAliveEnabled) {
|
|
662
|
-
channelBuilder = channelBuilder
|
|
663
|
-
.keepAliveWithoutCalls(true)
|
|
664
|
-
.keepAliveTime(state.keepAliveTime, TimeUnit.SECONDS)
|
|
665
|
-
.keepAliveTimeout(state.keepAliveTimeout, TimeUnit.SECONDS);
|
|
666
|
-
}
|
|
667
|
-
|
|
668
|
-
return channelBuilder.build();
|
|
669
|
-
}
|
|
670
|
-
|
|
671
|
-
@ReactMethod
|
|
672
|
-
public void resetConnection(final String message){
|
|
673
|
-
ChannelState state = defaultChannel();
|
|
674
|
-
if(null == state.managedChannel) return;
|
|
675
|
-
|
|
676
|
-
state.managedChannel.resetConnectBackoff();
|
|
677
|
-
|
|
678
|
-
this.initGrpcChannel();
|
|
679
|
-
|
|
680
|
-
showToast("resetConnection "+message);
|
|
681
|
-
}
|
|
682
|
-
|
|
683
|
-
@ReactMethod
|
|
684
|
-
public void onConnectionStateChange(){
|
|
685
|
-
ChannelState state = defaultChannel();
|
|
686
|
-
ManagedChannel managedChannel = state.managedChannel;
|
|
687
|
-
if(null == managedChannel) return;
|
|
688
|
-
|
|
689
|
-
final ConnectivityState connectivityState = managedChannel.getState(true);
|
|
690
|
-
if(ConnectivityState.CONNECTING == connectivityState){
|
|
691
|
-
showToast("onConnectionState CONNECTING");
|
|
692
|
-
} else if(ConnectivityState.IDLE == connectivityState){
|
|
693
|
-
showToast("onConnectionState IDLE");
|
|
694
|
-
} else if(ConnectivityState.READY == connectivityState){
|
|
695
|
-
showToast("onConnectionState READY");
|
|
696
|
-
} else if(ConnectivityState.TRANSIENT_FAILURE == connectivityState){
|
|
697
|
-
showToast("onConnectionState TRANSIENT_FAILURE");
|
|
698
|
-
} else if(ConnectivityState.SHUTDOWN == connectivityState){
|
|
699
|
-
showToast("onConnectionState SHUTDOWN");
|
|
700
|
-
} else {
|
|
701
|
-
showToast("onConnectionState UNKNOWN");
|
|
702
|
-
}
|
|
703
|
-
if(ConnectivityState.TRANSIENT_FAILURE == connectivityState && managedChannel.isTerminated() || managedChannel.isShutdown()){
|
|
704
|
-
resetConnection("onConnectionStateChange");
|
|
705
|
-
}
|
|
706
|
-
}
|
|
707
|
-
|
|
708
|
-
@ReactMethod
|
|
709
|
-
public void enterIdle(){
|
|
710
|
-
ChannelState state = defaultChannel();
|
|
711
|
-
if(null == state.managedChannel) return;
|
|
712
|
-
|
|
713
|
-
state.managedChannel.enterIdle();
|
|
714
|
-
|
|
715
|
-
showToast("enterIdle");
|
|
716
|
-
}
|
|
717
|
-
|
|
718
|
-
@ReactMethod
|
|
719
|
-
public void setUiLogEnabled(boolean isUiLogEnabled){
|
|
720
|
-
this.isUiLogEnabled = isUiLogEnabled;
|
|
721
|
-
}
|
|
722
|
-
|
|
723
|
-
@UiThread
|
|
724
|
-
private void showToast(final String message){
|
|
725
|
-
if(!isUiLogEnabled || null == context) return;
|
|
726
|
-
|
|
727
|
-
Toast.makeText(context,message,Toast.LENGTH_SHORT).show();
|
|
728
|
-
Log.d("GRPC_MODULE",message);
|
|
729
|
-
}
|
|
730
|
-
}
|
|
1
|
+
package com.reactnativegrpc;
|
|
2
|
+
|
|
3
|
+
import android.util.Base64;
|
|
4
|
+
import android.util.Log;
|
|
5
|
+
import android.widget.Toast;
|
|
6
|
+
|
|
7
|
+
import androidx.annotation.NonNull;
|
|
8
|
+
import androidx.annotation.UiThread;
|
|
9
|
+
|
|
10
|
+
import com.facebook.react.bridge.Arguments;
|
|
11
|
+
import com.facebook.react.bridge.Promise;
|
|
12
|
+
import com.facebook.react.bridge.ReactApplicationContext;
|
|
13
|
+
import com.facebook.react.bridge.ReactContextBaseJavaModule;
|
|
14
|
+
import com.facebook.react.bridge.ReactMethod;
|
|
15
|
+
import com.facebook.react.bridge.ReadableArray;
|
|
16
|
+
import com.facebook.react.bridge.ReadableMap;
|
|
17
|
+
import com.facebook.react.bridge.WritableMap;
|
|
18
|
+
import com.facebook.react.modules.core.DeviceEventManagerModule;
|
|
19
|
+
|
|
20
|
+
import java.io.ByteArrayInputStream;
|
|
21
|
+
import java.io.IOException;
|
|
22
|
+
import java.nio.charset.StandardCharsets;
|
|
23
|
+
import java.util.ArrayList;
|
|
24
|
+
import java.util.HashMap;
|
|
25
|
+
import java.util.List;
|
|
26
|
+
import java.util.Map;
|
|
27
|
+
import java.util.concurrent.TimeUnit;
|
|
28
|
+
|
|
29
|
+
import javax.net.ssl.X509TrustManager;
|
|
30
|
+
|
|
31
|
+
import io.grpc.CallOptions;
|
|
32
|
+
import io.grpc.ClientCall;
|
|
33
|
+
import io.grpc.ConnectivityState;
|
|
34
|
+
import io.grpc.Grpc;
|
|
35
|
+
import io.grpc.ManagedChannel;
|
|
36
|
+
import io.grpc.ManagedChannelBuilder;
|
|
37
|
+
import io.grpc.Metadata;
|
|
38
|
+
import io.grpc.MethodDescriptor;
|
|
39
|
+
import io.grpc.Status;
|
|
40
|
+
import io.grpc.TlsChannelCredentials;
|
|
41
|
+
|
|
42
|
+
public class GrpcModule extends ReactContextBaseJavaModule {
|
|
43
|
+
static final String DEFAULT_CHANNEL_ID = "default";
|
|
44
|
+
|
|
45
|
+
private final ReactApplicationContext context;
|
|
46
|
+
private final HashMap<Integer, ClientCall> callsMap = new HashMap<>();
|
|
47
|
+
private final HashMap<String, ChannelState> channels = new HashMap<>();
|
|
48
|
+
|
|
49
|
+
private boolean isUiLogEnabled = false;
|
|
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
|
+
}
|
|
76
|
+
|
|
77
|
+
public GrpcModule(ReactApplicationContext context) {
|
|
78
|
+
this.context = context;
|
|
79
|
+
channels.put(DEFAULT_CHANNEL_ID, new ChannelState());
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
@NonNull
|
|
83
|
+
@Override
|
|
84
|
+
public String getName() {
|
|
85
|
+
return "Grpc";
|
|
86
|
+
}
|
|
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
|
+
|
|
106
|
+
@ReactMethod()
|
|
107
|
+
public void getHost(final Promise promise) {
|
|
108
|
+
promise.resolve(defaultChannel().host);
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
@ReactMethod()
|
|
112
|
+
public void getIsInsecure(final Promise promise) {
|
|
113
|
+
promise.resolve(defaultChannel().isInsecure);
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
@ReactMethod
|
|
117
|
+
public void setHost(String host) {
|
|
118
|
+
defaultChannel().host = host;
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
@ReactMethod
|
|
122
|
+
public void setInsecure(boolean insecure) {
|
|
123
|
+
defaultChannel().isInsecure = insecure;
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
/**
|
|
127
|
+
* Replace TLS configuration (custom CA / mTLS / hostname override).
|
|
128
|
+
* Omitted or null fields clear the previous value. Apply before initGrpcChannel().
|
|
129
|
+
*/
|
|
130
|
+
@ReactMethod
|
|
131
|
+
public void setTlsOptions(ReadableMap options) {
|
|
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(
|
|
138
|
+
readOptionalStringList(options, "spkiSha256Pins")
|
|
139
|
+
);
|
|
140
|
+
|
|
141
|
+
boolean hasCert = state.certificateChainPem != null && !state.certificateChainPem.isEmpty();
|
|
142
|
+
boolean hasKey = state.privateKeyPem != null && !state.privateKeyPem.isEmpty();
|
|
143
|
+
if (hasCert != hasKey) {
|
|
144
|
+
throw new IllegalArgumentException(
|
|
145
|
+
"mTLS requires both certificateChainPem and privateKeyPem"
|
|
146
|
+
);
|
|
147
|
+
}
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
private static String readOptionalString(ReadableMap options, String key) {
|
|
151
|
+
if (options == null || !options.hasKey(key) || options.isNull(key)) {
|
|
152
|
+
return null;
|
|
153
|
+
}
|
|
154
|
+
String value = options.getString(key);
|
|
155
|
+
if (value == null || value.isEmpty()) {
|
|
156
|
+
return null;
|
|
157
|
+
}
|
|
158
|
+
return value;
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
private static List<String> readOptionalStringList(ReadableMap options, String key) {
|
|
162
|
+
if (options == null || !options.hasKey(key) || options.isNull(key)) {
|
|
163
|
+
return null;
|
|
164
|
+
}
|
|
165
|
+
ReadableArray array = options.getArray(key);
|
|
166
|
+
if (array == null || array.size() == 0) {
|
|
167
|
+
return null;
|
|
168
|
+
}
|
|
169
|
+
List<String> values = new ArrayList<>();
|
|
170
|
+
for (int i = 0; i < array.size(); i++) {
|
|
171
|
+
String value = array.getString(i);
|
|
172
|
+
if (value != null && !value.isEmpty()) {
|
|
173
|
+
values.add(value);
|
|
174
|
+
}
|
|
175
|
+
}
|
|
176
|
+
return values.isEmpty() ? null : values;
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
@ReactMethod
|
|
180
|
+
public void setCompression(Boolean enable, String compressorName) {
|
|
181
|
+
ChannelState state = defaultChannel();
|
|
182
|
+
state.withCompression = enable;
|
|
183
|
+
state.compressorName = compressorName;
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
@ReactMethod
|
|
187
|
+
public void setResponseSizeLimit(int limit) {
|
|
188
|
+
defaultChannel().responseSizeLimit = limit;
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
@ReactMethod
|
|
192
|
+
public void setKeepAlive(boolean enabled, int time, int timeout) {
|
|
193
|
+
ChannelState state = defaultChannel();
|
|
194
|
+
state.keepAliveEnabled = enabled;
|
|
195
|
+
state.keepAliveTime = time;
|
|
196
|
+
state.keepAliveTimeout = timeout;
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
@ReactMethod
|
|
200
|
+
public void setCallDeadlineSeconds(double seconds) {
|
|
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
|
+
}
|
|
282
|
+
}
|
|
283
|
+
|
|
284
|
+
@ReactMethod
|
|
285
|
+
public void unaryCall(
|
|
286
|
+
int id,
|
|
287
|
+
String path,
|
|
288
|
+
ReadableMap obj,
|
|
289
|
+
ReadableMap headers,
|
|
290
|
+
String channelId,
|
|
291
|
+
final Promise promise
|
|
292
|
+
) {
|
|
293
|
+
ClientCall call;
|
|
294
|
+
|
|
295
|
+
try {
|
|
296
|
+
ChannelState state = requireChannel(channelId);
|
|
297
|
+
call = this.startGrpcCall(id, path, MethodDescriptor.MethodType.UNARY, headers, resolveDeadline(obj, state), state);
|
|
298
|
+
} catch (Exception e) {
|
|
299
|
+
promise.reject(e);
|
|
300
|
+
|
|
301
|
+
return;
|
|
302
|
+
}
|
|
303
|
+
|
|
304
|
+
byte[] data = Base64.decode(obj.getString("data"), Base64.NO_WRAP);
|
|
305
|
+
|
|
306
|
+
call.sendMessage(data);
|
|
307
|
+
call.request(1);
|
|
308
|
+
call.halfClose();
|
|
309
|
+
|
|
310
|
+
callsMap.put(id, call);
|
|
311
|
+
|
|
312
|
+
promise.resolve(null);
|
|
313
|
+
}
|
|
314
|
+
|
|
315
|
+
@ReactMethod
|
|
316
|
+
public void serverStreamingCall(
|
|
317
|
+
int id,
|
|
318
|
+
String path,
|
|
319
|
+
ReadableMap obj,
|
|
320
|
+
ReadableMap headers,
|
|
321
|
+
String channelId,
|
|
322
|
+
final Promise promise
|
|
323
|
+
) {
|
|
324
|
+
ClientCall call;
|
|
325
|
+
|
|
326
|
+
try {
|
|
327
|
+
ChannelState state = requireChannel(channelId);
|
|
328
|
+
call = this.startGrpcCall(id, path, MethodDescriptor.MethodType.SERVER_STREAMING, headers, resolveDeadline(obj, state), state);
|
|
329
|
+
} catch (Exception e) {
|
|
330
|
+
promise.reject(e);
|
|
331
|
+
|
|
332
|
+
return;
|
|
333
|
+
}
|
|
334
|
+
|
|
335
|
+
byte[] data = Base64.decode(obj.getString("data"), Base64.NO_WRAP);
|
|
336
|
+
|
|
337
|
+
call.sendMessage(data);
|
|
338
|
+
call.request(1);
|
|
339
|
+
call.halfClose();
|
|
340
|
+
|
|
341
|
+
callsMap.put(id, call);
|
|
342
|
+
|
|
343
|
+
promise.resolve(null);
|
|
344
|
+
}
|
|
345
|
+
|
|
346
|
+
@ReactMethod
|
|
347
|
+
public void clientStreamingCall(
|
|
348
|
+
int id,
|
|
349
|
+
String path,
|
|
350
|
+
ReadableMap obj,
|
|
351
|
+
ReadableMap headers,
|
|
352
|
+
String channelId,
|
|
353
|
+
final Promise promise
|
|
354
|
+
) {
|
|
355
|
+
ClientCall call = callsMap.get(id);
|
|
356
|
+
|
|
357
|
+
if (call == null) {
|
|
358
|
+
try {
|
|
359
|
+
ChannelState state = requireChannel(channelId);
|
|
360
|
+
call = this.startGrpcCall(id, path, MethodDescriptor.MethodType.CLIENT_STREAMING, headers, resolveDeadline(obj, state), state);
|
|
361
|
+
} catch (Exception e) {
|
|
362
|
+
promise.reject(e);
|
|
363
|
+
|
|
364
|
+
return;
|
|
365
|
+
}
|
|
366
|
+
|
|
367
|
+
callsMap.put(id, call);
|
|
368
|
+
}
|
|
369
|
+
|
|
370
|
+
byte[] data = Base64.decode(obj.getString("data"), Base64.NO_WRAP);
|
|
371
|
+
|
|
372
|
+
call.sendMessage(data);
|
|
373
|
+
call.request(1);
|
|
374
|
+
|
|
375
|
+
promise.resolve(null);
|
|
376
|
+
}
|
|
377
|
+
|
|
378
|
+
@ReactMethod
|
|
379
|
+
public void bidiStreamingCall(
|
|
380
|
+
int id,
|
|
381
|
+
String path,
|
|
382
|
+
ReadableMap obj,
|
|
383
|
+
ReadableMap headers,
|
|
384
|
+
String channelId,
|
|
385
|
+
final Promise promise
|
|
386
|
+
) {
|
|
387
|
+
ClientCall call = callsMap.get(id);
|
|
388
|
+
|
|
389
|
+
if (call == null) {
|
|
390
|
+
try {
|
|
391
|
+
ChannelState state = requireChannel(channelId);
|
|
392
|
+
call = this.startGrpcCall(id, path, MethodDescriptor.MethodType.BIDI_STREAMING, headers, resolveDeadline(obj, state), state);
|
|
393
|
+
} catch (Exception e) {
|
|
394
|
+
promise.reject(e);
|
|
395
|
+
|
|
396
|
+
return;
|
|
397
|
+
}
|
|
398
|
+
|
|
399
|
+
callsMap.put(id, call);
|
|
400
|
+
}
|
|
401
|
+
|
|
402
|
+
byte[] data = Base64.decode(obj.getString("data"), Base64.NO_WRAP);
|
|
403
|
+
|
|
404
|
+
call.sendMessage(data);
|
|
405
|
+
call.request(1);
|
|
406
|
+
|
|
407
|
+
promise.resolve(null);
|
|
408
|
+
}
|
|
409
|
+
|
|
410
|
+
@ReactMethod
|
|
411
|
+
public void finishClientStreaming(int id, final Promise promise) {
|
|
412
|
+
if (callsMap.containsKey(id)) {
|
|
413
|
+
ClientCall call = callsMap.get(id);
|
|
414
|
+
|
|
415
|
+
call.halfClose();
|
|
416
|
+
|
|
417
|
+
promise.resolve(true);
|
|
418
|
+
} else {
|
|
419
|
+
promise.resolve(false);
|
|
420
|
+
}
|
|
421
|
+
}
|
|
422
|
+
|
|
423
|
+
@ReactMethod
|
|
424
|
+
public void cancelGrpcCall(int id, final Promise promise) {
|
|
425
|
+
if (callsMap.containsKey(id)) {
|
|
426
|
+
ClientCall call = callsMap.get(id);
|
|
427
|
+
call.cancel("Cancelled", new Exception("Cancelled by app"));
|
|
428
|
+
|
|
429
|
+
promise.resolve(true);
|
|
430
|
+
} else {
|
|
431
|
+
promise.resolve(false);
|
|
432
|
+
}
|
|
433
|
+
}
|
|
434
|
+
|
|
435
|
+
/** Resolve per-call deadline: obj.deadlineSeconds if set, else channel default. */
|
|
436
|
+
private long resolveDeadline(ReadableMap obj, ChannelState state) {
|
|
437
|
+
if (obj != null && obj.hasKey("deadlineSeconds") && !obj.isNull("deadlineSeconds")) {
|
|
438
|
+
return Math.max(0, (long) obj.getDouble("deadlineSeconds"));
|
|
439
|
+
}
|
|
440
|
+
return state.callDeadlineSeconds;
|
|
441
|
+
}
|
|
442
|
+
|
|
443
|
+
private ClientCall startGrpcCall(
|
|
444
|
+
int id,
|
|
445
|
+
String path,
|
|
446
|
+
MethodDescriptor.MethodType methodType,
|
|
447
|
+
ReadableMap headers,
|
|
448
|
+
long deadlineSeconds,
|
|
449
|
+
ChannelState state
|
|
450
|
+
) throws Exception {
|
|
451
|
+
if (state.managedChannel == null) {
|
|
452
|
+
throw new Exception("Channel not created");
|
|
453
|
+
}
|
|
454
|
+
|
|
455
|
+
path = normalizePath(path);
|
|
456
|
+
|
|
457
|
+
final Metadata headersMetadata = new Metadata();
|
|
458
|
+
|
|
459
|
+
for (Map.Entry<String, Object> headerEntry : headers.toHashMap().entrySet()) {
|
|
460
|
+
headersMetadata.put(Metadata.Key.of(headerEntry.getKey(), Metadata.ASCII_STRING_MARSHALLER), headerEntry.getValue().toString());
|
|
461
|
+
}
|
|
462
|
+
|
|
463
|
+
MethodDescriptor.Marshaller<byte[]> marshaller = new GrpcMarshaller();
|
|
464
|
+
|
|
465
|
+
MethodDescriptor descriptor = MethodDescriptor.<byte[], byte[]>newBuilder()
|
|
466
|
+
.setFullMethodName(path)
|
|
467
|
+
.setType(methodType)
|
|
468
|
+
.setRequestMarshaller(marshaller)
|
|
469
|
+
.setResponseMarshaller(marshaller)
|
|
470
|
+
.build();
|
|
471
|
+
|
|
472
|
+
CallOptions callOptions = CallOptions.DEFAULT;
|
|
473
|
+
|
|
474
|
+
if (!state.compressorName.isEmpty()) {
|
|
475
|
+
callOptions = callOptions.withCompression(state.compressorName);
|
|
476
|
+
}
|
|
477
|
+
if (deadlineSeconds > 0) {
|
|
478
|
+
callOptions = callOptions.withDeadlineAfter(deadlineSeconds, TimeUnit.SECONDS);
|
|
479
|
+
}
|
|
480
|
+
|
|
481
|
+
ClientCall call = state.managedChannel.newCall(descriptor, callOptions);
|
|
482
|
+
|
|
483
|
+
call.start(new ClientCall.Listener() {
|
|
484
|
+
@Override
|
|
485
|
+
public void onHeaders(Metadata headers) {
|
|
486
|
+
super.onHeaders(headers);
|
|
487
|
+
|
|
488
|
+
WritableMap event = Arguments.createMap();
|
|
489
|
+
WritableMap payload = Arguments.createMap();
|
|
490
|
+
|
|
491
|
+
for (String key : headers.keys()) {
|
|
492
|
+
if (key.endsWith(Metadata.BINARY_HEADER_SUFFIX)) {
|
|
493
|
+
byte[] data = headers.get(Metadata.Key.of(key, Metadata.BINARY_BYTE_MARSHALLER));
|
|
494
|
+
|
|
495
|
+
payload.putString(key, new String(Base64.encode(data, Base64.NO_WRAP)));
|
|
496
|
+
} else if (!key.startsWith(":")) {
|
|
497
|
+
String data = headers.get(Metadata.Key.of(key, Metadata.ASCII_STRING_MARSHALLER));
|
|
498
|
+
|
|
499
|
+
payload.putString(key, data);
|
|
500
|
+
}
|
|
501
|
+
}
|
|
502
|
+
|
|
503
|
+
event.putInt("id", id);
|
|
504
|
+
event.putString("type", "headers");
|
|
505
|
+
event.putMap("payload", payload);
|
|
506
|
+
|
|
507
|
+
emitEvent("grpc-call", event);
|
|
508
|
+
}
|
|
509
|
+
|
|
510
|
+
@Override
|
|
511
|
+
public void onMessage(Object messageObj) {
|
|
512
|
+
super.onMessage(messageObj);
|
|
513
|
+
|
|
514
|
+
byte[] data = (byte[]) messageObj;
|
|
515
|
+
|
|
516
|
+
WritableMap event = Arguments.createMap();
|
|
517
|
+
|
|
518
|
+
event.putInt("id", id);
|
|
519
|
+
event.putString("type", "response");
|
|
520
|
+
event.putString("payload", Base64.encodeToString(data, Base64.NO_WRAP));
|
|
521
|
+
|
|
522
|
+
emitEvent("grpc-call", event);
|
|
523
|
+
|
|
524
|
+
if (methodType == MethodDescriptor.MethodType.SERVER_STREAMING
|
|
525
|
+
|| methodType == MethodDescriptor.MethodType.BIDI_STREAMING) {
|
|
526
|
+
call.request(1);
|
|
527
|
+
}
|
|
528
|
+
}
|
|
529
|
+
|
|
530
|
+
@Override
|
|
531
|
+
public void onClose(Status status, Metadata trailers) {
|
|
532
|
+
super.onClose(status, trailers);
|
|
533
|
+
|
|
534
|
+
callsMap.remove(id);
|
|
535
|
+
|
|
536
|
+
WritableMap event = Arguments.createMap();
|
|
537
|
+
event.putInt("id", id);
|
|
538
|
+
|
|
539
|
+
WritableMap trailersMap = Arguments.createMap();
|
|
540
|
+
|
|
541
|
+
for (String key : trailers.keys()) {
|
|
542
|
+
if (key.endsWith(Metadata.BINARY_HEADER_SUFFIX)) {
|
|
543
|
+
byte[] data = trailers.get(Metadata.Key.of(key, Metadata.BINARY_BYTE_MARSHALLER));
|
|
544
|
+
|
|
545
|
+
trailersMap.putString(key, new String(Base64.encode(data, Base64.NO_WRAP)));
|
|
546
|
+
} else if (!key.startsWith(":")) {
|
|
547
|
+
String data = trailers.get(Metadata.Key.of(key, Metadata.ASCII_STRING_MARSHALLER));
|
|
548
|
+
|
|
549
|
+
trailersMap.putString(key, data);
|
|
550
|
+
}
|
|
551
|
+
}
|
|
552
|
+
|
|
553
|
+
if (!status.isOk()) {
|
|
554
|
+
event.putString("type", "error");
|
|
555
|
+
event.putString("error", status.asException(trailers).getLocalizedMessage());
|
|
556
|
+
event.putInt("code", status.getCode().value());
|
|
557
|
+
event.putMap("trailers", trailersMap);
|
|
558
|
+
} else {
|
|
559
|
+
event.putString("type", "trailers");
|
|
560
|
+
event.putMap("payload", trailersMap);
|
|
561
|
+
}
|
|
562
|
+
|
|
563
|
+
emitEvent("grpc-call", event);
|
|
564
|
+
}
|
|
565
|
+
}, headersMetadata);
|
|
566
|
+
|
|
567
|
+
if (state.withCompression) {
|
|
568
|
+
call.setMessageCompression(true);
|
|
569
|
+
}
|
|
570
|
+
|
|
571
|
+
return call;
|
|
572
|
+
}
|
|
573
|
+
|
|
574
|
+
@ReactMethod
|
|
575
|
+
public void addListener(String eventName) {
|
|
576
|
+
}
|
|
577
|
+
|
|
578
|
+
@ReactMethod
|
|
579
|
+
public void removeListeners(Integer count) {
|
|
580
|
+
}
|
|
581
|
+
|
|
582
|
+
private void emitEvent(String eventName, Object params) {
|
|
583
|
+
context
|
|
584
|
+
.getJSModule(DeviceEventManagerModule.RCTDeviceEventEmitter.class)
|
|
585
|
+
.emit(eventName, params);
|
|
586
|
+
}
|
|
587
|
+
|
|
588
|
+
private static String normalizePath(String path) {
|
|
589
|
+
if (path.startsWith("/")) {
|
|
590
|
+
path = path.substring(1);
|
|
591
|
+
}
|
|
592
|
+
return path;
|
|
593
|
+
}
|
|
594
|
+
|
|
595
|
+
@ReactMethod
|
|
596
|
+
public void initGrpcChannel() {
|
|
597
|
+
ChannelState state = defaultChannel();
|
|
598
|
+
if (state.managedChannel != null) {
|
|
599
|
+
state.managedChannel.shutdown();
|
|
600
|
+
}
|
|
601
|
+
state.managedChannel = buildManagedChannel(state);
|
|
602
|
+
}
|
|
603
|
+
|
|
604
|
+
private ManagedChannel buildManagedChannel(ChannelState state) {
|
|
605
|
+
ManagedChannelBuilder<?> channelBuilder;
|
|
606
|
+
|
|
607
|
+
if (state.isInsecure) {
|
|
608
|
+
channelBuilder = ManagedChannelBuilder.forTarget(state.host).usePlaintext();
|
|
609
|
+
} else {
|
|
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();
|
|
614
|
+
|
|
615
|
+
if (hasCert != hasKey) {
|
|
616
|
+
throw new IllegalArgumentException(
|
|
617
|
+
"mTLS requires both certificateChainPem and privateKeyPem"
|
|
618
|
+
);
|
|
619
|
+
}
|
|
620
|
+
|
|
621
|
+
if (hasRoots || hasCert || hasPins) {
|
|
622
|
+
try {
|
|
623
|
+
TlsChannelCredentials.Builder tls = TlsChannelCredentials.newBuilder();
|
|
624
|
+
if (hasRoots || hasPins) {
|
|
625
|
+
X509TrustManager base = hasRoots
|
|
626
|
+
? TrustManagers.fromPemRoots(state.rootCertsPem)
|
|
627
|
+
: TrustManagers.systemTrustManager();
|
|
628
|
+
if (hasPins) {
|
|
629
|
+
tls.trustManager(new SpkiPinTrustManager(base, state.spkiSha256Pins));
|
|
630
|
+
} else {
|
|
631
|
+
tls.trustManager(base);
|
|
632
|
+
}
|
|
633
|
+
}
|
|
634
|
+
if (hasCert) {
|
|
635
|
+
tls.keyManager(
|
|
636
|
+
new ByteArrayInputStream(state.certificateChainPem.getBytes(StandardCharsets.UTF_8)),
|
|
637
|
+
new ByteArrayInputStream(state.privateKeyPem.getBytes(StandardCharsets.UTF_8))
|
|
638
|
+
);
|
|
639
|
+
}
|
|
640
|
+
channelBuilder = Grpc.newChannelBuilder(state.host, tls.build());
|
|
641
|
+
} catch (IOException e) {
|
|
642
|
+
throw new RuntimeException("Failed to configure TLS credentials", e);
|
|
643
|
+
} catch (RuntimeException e) {
|
|
644
|
+
throw e;
|
|
645
|
+
} catch (Exception e) {
|
|
646
|
+
throw new RuntimeException("Failed to configure TLS credentials", e);
|
|
647
|
+
}
|
|
648
|
+
} else {
|
|
649
|
+
channelBuilder = ManagedChannelBuilder.forTarget(state.host);
|
|
650
|
+
}
|
|
651
|
+
|
|
652
|
+
if (state.hostNameOverride != null && !state.hostNameOverride.isEmpty()) {
|
|
653
|
+
channelBuilder = channelBuilder.overrideAuthority(state.hostNameOverride);
|
|
654
|
+
}
|
|
655
|
+
}
|
|
656
|
+
|
|
657
|
+
if (state.responseSizeLimit != null) {
|
|
658
|
+
channelBuilder = channelBuilder.maxInboundMessageSize(state.responseSizeLimit);
|
|
659
|
+
}
|
|
660
|
+
|
|
661
|
+
if (state.keepAliveEnabled) {
|
|
662
|
+
channelBuilder = channelBuilder
|
|
663
|
+
.keepAliveWithoutCalls(true)
|
|
664
|
+
.keepAliveTime(state.keepAliveTime, TimeUnit.SECONDS)
|
|
665
|
+
.keepAliveTimeout(state.keepAliveTimeout, TimeUnit.SECONDS);
|
|
666
|
+
}
|
|
667
|
+
|
|
668
|
+
return channelBuilder.build();
|
|
669
|
+
}
|
|
670
|
+
|
|
671
|
+
@ReactMethod
|
|
672
|
+
public void resetConnection(final String message){
|
|
673
|
+
ChannelState state = defaultChannel();
|
|
674
|
+
if(null == state.managedChannel) return;
|
|
675
|
+
|
|
676
|
+
state.managedChannel.resetConnectBackoff();
|
|
677
|
+
|
|
678
|
+
this.initGrpcChannel();
|
|
679
|
+
|
|
680
|
+
showToast("resetConnection "+message);
|
|
681
|
+
}
|
|
682
|
+
|
|
683
|
+
@ReactMethod
|
|
684
|
+
public void onConnectionStateChange(){
|
|
685
|
+
ChannelState state = defaultChannel();
|
|
686
|
+
ManagedChannel managedChannel = state.managedChannel;
|
|
687
|
+
if(null == managedChannel) return;
|
|
688
|
+
|
|
689
|
+
final ConnectivityState connectivityState = managedChannel.getState(true);
|
|
690
|
+
if(ConnectivityState.CONNECTING == connectivityState){
|
|
691
|
+
showToast("onConnectionState CONNECTING");
|
|
692
|
+
} else if(ConnectivityState.IDLE == connectivityState){
|
|
693
|
+
showToast("onConnectionState IDLE");
|
|
694
|
+
} else if(ConnectivityState.READY == connectivityState){
|
|
695
|
+
showToast("onConnectionState READY");
|
|
696
|
+
} else if(ConnectivityState.TRANSIENT_FAILURE == connectivityState){
|
|
697
|
+
showToast("onConnectionState TRANSIENT_FAILURE");
|
|
698
|
+
} else if(ConnectivityState.SHUTDOWN == connectivityState){
|
|
699
|
+
showToast("onConnectionState SHUTDOWN");
|
|
700
|
+
} else {
|
|
701
|
+
showToast("onConnectionState UNKNOWN");
|
|
702
|
+
}
|
|
703
|
+
if(ConnectivityState.TRANSIENT_FAILURE == connectivityState && managedChannel.isTerminated() || managedChannel.isShutdown()){
|
|
704
|
+
resetConnection("onConnectionStateChange");
|
|
705
|
+
}
|
|
706
|
+
}
|
|
707
|
+
|
|
708
|
+
@ReactMethod
|
|
709
|
+
public void enterIdle(){
|
|
710
|
+
ChannelState state = defaultChannel();
|
|
711
|
+
if(null == state.managedChannel) return;
|
|
712
|
+
|
|
713
|
+
state.managedChannel.enterIdle();
|
|
714
|
+
|
|
715
|
+
showToast("enterIdle");
|
|
716
|
+
}
|
|
717
|
+
|
|
718
|
+
@ReactMethod
|
|
719
|
+
public void setUiLogEnabled(boolean isUiLogEnabled){
|
|
720
|
+
this.isUiLogEnabled = isUiLogEnabled;
|
|
721
|
+
}
|
|
722
|
+
|
|
723
|
+
@UiThread
|
|
724
|
+
private void showToast(final String message){
|
|
725
|
+
if(!isUiLogEnabled || null == context) return;
|
|
726
|
+
|
|
727
|
+
Toast.makeText(context,message,Toast.LENGTH_SHORT).show();
|
|
728
|
+
Log.d("GRPC_MODULE",message);
|
|
729
|
+
}
|
|
730
|
+
}
|