@xdcobra/react-native-grpc 1.1.0 → 2.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE.md +22 -22
- package/README.md +302 -284
- package/android/build.gradle +31 -127
- package/android/src/main/AndroidManifest.xml +3 -3
- package/android/src/main/java/com/reactnativegrpc/GrpcMarshaller.java +27 -27
- package/android/src/main/java/com/reactnativegrpc/GrpcModule.java +730 -730
- package/android/src/main/java/com/reactnativegrpc/GrpcPackage.java +27 -27
- package/android/src/main/java/com/reactnativegrpc/SpkiPinTrustManager.java +101 -101
- package/android/src/main/java/com/reactnativegrpc/TrustManagers.java +51 -51
- package/ios/Grpc.h +22 -22
- package/ios/Grpc.m +627 -625
- package/ios/SpkiPinning.h +22 -22
- package/ios/SpkiPinning.m +162 -114
- package/lib/commonjs/bidi-streaming.js.map +1 -1
- package/lib/commonjs/channel.js +9 -9
- package/lib/commonjs/channel.js.map +1 -1
- package/lib/commonjs/client-streaming.js +3 -3
- package/lib/commonjs/client-streaming.js.map +1 -1
- package/lib/commonjs/client.js +24 -24
- package/lib/commonjs/client.js.map +1 -1
- package/lib/commonjs/create-client.js +5 -5
- package/lib/commonjs/create-client.js.map +1 -1
- package/lib/commonjs/errors.js.map +1 -1
- package/lib/commonjs/index.js.map +1 -1
- package/lib/commonjs/interceptors.js +3 -3
- package/lib/commonjs/interceptors.js.map +1 -1
- package/lib/commonjs/native.js.map +1 -1
- package/lib/commonjs/retry.js.map +1 -1
- package/lib/commonjs/server-streaming.js.map +1 -1
- package/lib/commonjs/status.js.map +1 -1
- package/lib/commonjs/types.js +5 -5
- package/lib/commonjs/types.js.map +1 -1
- package/lib/commonjs/unary.js.map +1 -1
- package/lib/module/bidi-streaming.js.map +1 -1
- package/lib/module/channel.js +9 -9
- package/lib/module/channel.js.map +1 -1
- package/lib/module/client-streaming.js +3 -3
- package/lib/module/client-streaming.js.map +1 -1
- package/lib/module/client.js +24 -24
- package/lib/module/client.js.map +1 -1
- package/lib/module/create-client.js +5 -5
- package/lib/module/create-client.js.map +1 -1
- package/lib/module/errors.js.map +1 -1
- package/lib/module/index.js.map +1 -1
- package/lib/module/interceptors.js +3 -3
- package/lib/module/interceptors.js.map +1 -1
- package/lib/module/native.js.map +1 -1
- package/lib/module/retry.js.map +1 -1
- package/lib/module/server-streaming.js.map +1 -1
- package/lib/module/status.js.map +1 -1
- package/lib/module/types.js +5 -5
- package/lib/module/types.js.map +1 -1
- package/lib/module/unary.js.map +1 -1
- package/lib/typescript/src/bidi-streaming.d.ts.map +1 -0
- package/lib/typescript/src/channel.d.ts.map +1 -0
- package/lib/typescript/src/client-streaming.d.ts.map +1 -0
- package/lib/typescript/src/client.d.ts.map +1 -0
- package/lib/typescript/src/create-client.d.ts.map +1 -0
- package/lib/typescript/src/errors.d.ts.map +1 -0
- package/lib/typescript/src/index.d.ts.map +1 -0
- package/lib/typescript/src/interceptors.d.ts.map +1 -0
- package/lib/typescript/src/native.d.ts.map +1 -0
- package/lib/typescript/src/retry.d.ts.map +1 -0
- package/lib/typescript/src/server-streaming.d.ts.map +1 -0
- package/lib/typescript/src/status.d.ts.map +1 -0
- package/lib/typescript/src/types.d.ts.map +1 -0
- package/lib/typescript/src/unary.d.ts.map +1 -0
- package/lib/typescript/test/app/App.d.ts +3 -0
- package/lib/typescript/test/app/App.d.ts.map +1 -0
- package/package.json +209 -210
- package/react-native-grpc.podspec +33 -29
- package/src/bidi-streaming.ts +78 -78
- package/src/channel.ts +110 -110
- package/src/client-streaming.ts +106 -106
- package/src/client.ts +1046 -1046
- package/src/create-client.ts +391 -391
- package/src/errors.ts +11 -11
- package/src/index.ts +28 -28
- package/src/interceptors.ts +167 -167
- package/src/native.ts +99 -99
- package/src/retry.ts +208 -208
- package/src/server-streaming.ts +105 -105
- package/src/status.ts +50 -50
- package/src/types.ts +171 -171
- package/src/unary.ts +71 -71
- package/android/gradle.properties +0 -4
- package/lib/typescript/bidi-streaming.d.ts.map +0 -1
- package/lib/typescript/channel.d.ts.map +0 -1
- package/lib/typescript/client-streaming.d.ts.map +0 -1
- package/lib/typescript/client.d.ts.map +0 -1
- package/lib/typescript/create-client.d.ts.map +0 -1
- package/lib/typescript/errors.d.ts.map +0 -1
- package/lib/typescript/index.d.ts.map +0 -1
- package/lib/typescript/interceptors.d.ts.map +0 -1
- package/lib/typescript/native.d.ts.map +0 -1
- package/lib/typescript/retry.d.ts.map +0 -1
- package/lib/typescript/server-streaming.d.ts.map +0 -1
- package/lib/typescript/status.d.ts.map +0 -1
- package/lib/typescript/types.d.ts.map +0 -1
- package/lib/typescript/unary.d.ts.map +0 -1
- /package/lib/typescript/{bidi-streaming.d.ts → src/bidi-streaming.d.ts} +0 -0
- /package/lib/typescript/{channel.d.ts → src/channel.d.ts} +0 -0
- /package/lib/typescript/{client-streaming.d.ts → src/client-streaming.d.ts} +0 -0
- /package/lib/typescript/{client.d.ts → src/client.d.ts} +0 -0
- /package/lib/typescript/{create-client.d.ts → src/create-client.d.ts} +0 -0
- /package/lib/typescript/{errors.d.ts → src/errors.d.ts} +0 -0
- /package/lib/typescript/{index.d.ts → src/index.d.ts} +0 -0
- /package/lib/typescript/{interceptors.d.ts → src/interceptors.d.ts} +0 -0
- /package/lib/typescript/{native.d.ts → src/native.d.ts} +0 -0
- /package/lib/typescript/{retry.d.ts → src/retry.d.ts} +0 -0
- /package/lib/typescript/{server-streaming.d.ts → src/server-streaming.d.ts} +0 -0
- /package/lib/typescript/{status.d.ts → src/status.d.ts} +0 -0
- /package/lib/typescript/{types.d.ts → src/types.d.ts} +0 -0
- /package/lib/typescript/{unary.d.ts → src/unary.d.ts} +0 -0
package/ios/Grpc.m
CHANGED
|
@@ -1,625 +1,627 @@
|
|
|
1
|
-
#import "Grpc.h"
|
|
2
|
-
#import "SpkiPinning.h"
|
|
3
|
-
#import <GRPCClient/GRPCCall.h>
|
|
4
|
-
#import <GRPCClient/GRPCTransport.h>
|
|
5
|
-
|
|
6
|
-
static NSString * const kGrpcDefaultChannelId = @"default";
|
|
7
|
-
|
|
8
|
-
@interface GrpcResponseHandler : NSObject <GRPCResponseHandler>
|
|
9
|
-
|
|
10
|
-
- (instancetype)initWithInitialMetadataCallback:(void (^)(NSDictionary *))initialMetadataCallback
|
|
11
|
-
messageCallback:(void (^)(id))messageCallback
|
|
12
|
-
closeCallback:(void (^)(NSDictionary *, NSError *))closeCallback
|
|
13
|
-
writeDataCallback:(void (^)(void))writeDataCallback;
|
|
14
|
-
|
|
15
|
-
- (instancetype)initWithInitialMetadataCallback:(void (^)(NSDictionary *))initialMetadataCallback
|
|
16
|
-
messageCallback:(void (^)(id))messageCallback
|
|
17
|
-
closeCallback:(void (^)(NSDictionary *, NSError *))closeCallback;
|
|
18
|
-
|
|
19
|
-
@end
|
|
20
|
-
|
|
21
|
-
@implementation GrpcResponseHandler {
|
|
22
|
-
void (^_initialMetadataCallback)(NSDictionary *);
|
|
23
|
-
|
|
24
|
-
void (^_messageCallback)(id);
|
|
25
|
-
|
|
26
|
-
void (^_closeCallback)(NSDictionary *, NSError *);
|
|
27
|
-
|
|
28
|
-
void (^_writeDataCallback)(void);
|
|
29
|
-
|
|
30
|
-
dispatch_queue_t _dispatchQueue;
|
|
31
|
-
}
|
|
32
|
-
|
|
33
|
-
- (instancetype)initWithInitialMetadataCallback:(void (^)(NSDictionary *))initialMetadataCallback
|
|
34
|
-
messageCallback:(void (^)(id))messageCallback
|
|
35
|
-
closeCallback:(void (^)(NSDictionary *, NSError *))closeCallback
|
|
36
|
-
writeDataCallback:(void (^)(void))writeDataCallback {
|
|
37
|
-
if ((self = [super init])) {
|
|
38
|
-
_initialMetadataCallback = initialMetadataCallback;
|
|
39
|
-
_messageCallback = messageCallback;
|
|
40
|
-
_closeCallback = closeCallback;
|
|
41
|
-
_writeDataCallback = writeDataCallback;
|
|
42
|
-
_dispatchQueue = dispatch_queue_create(nil, DISPATCH_QUEUE_SERIAL);
|
|
43
|
-
}
|
|
44
|
-
return self;
|
|
45
|
-
}
|
|
46
|
-
|
|
47
|
-
- (instancetype)initWithInitialMetadataCallback:(void (^)(NSDictionary *))initialMetadataCallback
|
|
48
|
-
messageCallback:(void (^)(id))messageCallback
|
|
49
|
-
closeCallback:(void (^)(NSDictionary *, NSError *))closeCallback {
|
|
50
|
-
return [self initWithInitialMetadataCallback:initialMetadataCallback
|
|
51
|
-
messageCallback:messageCallback
|
|
52
|
-
closeCallback:closeCallback
|
|
53
|
-
writeDataCallback:nil];
|
|
54
|
-
}
|
|
55
|
-
|
|
56
|
-
- (void)didReceiveInitialMetadata:(NSDictionary *)initialMetadata {
|
|
57
|
-
if (self->_initialMetadataCallback) {
|
|
58
|
-
self->_initialMetadataCallback(initialMetadata);
|
|
59
|
-
}
|
|
60
|
-
}
|
|
61
|
-
|
|
62
|
-
- (void)didReceiveRawMessage:(id)message {
|
|
63
|
-
if (self->_messageCallback) {
|
|
64
|
-
self->_messageCallback(message);
|
|
65
|
-
}
|
|
66
|
-
}
|
|
67
|
-
|
|
68
|
-
- (void)didCloseWithTrailingMetadata:(NSDictionary *)trailingMetadata error:(NSError *)error {
|
|
69
|
-
if (self->_closeCallback) {
|
|
70
|
-
self->_closeCallback(trailingMetadata, error);
|
|
71
|
-
}
|
|
72
|
-
}
|
|
73
|
-
|
|
74
|
-
- (void)didWriteData {
|
|
75
|
-
if (self->_writeDataCallback) {
|
|
76
|
-
self->_writeDataCallback();
|
|
77
|
-
}
|
|
78
|
-
}
|
|
79
|
-
|
|
80
|
-
- (dispatch_queue_t)dispatchQueue {
|
|
81
|
-
return _dispatchQueue;
|
|
82
|
-
}
|
|
83
|
-
|
|
84
|
-
@end
|
|
85
|
-
|
|
86
|
-
@implementation Grpc {
|
|
87
|
-
bool hasListeners;
|
|
88
|
-
NSMutableDictionary<NSNumber *, GRPCCall2 *> *calls;
|
|
89
|
-
/** channelId → mutable config dictionary (extra channels; default uses properties). */
|
|
90
|
-
NSMutableDictionary<NSString *, NSMutableDictionary *> *channelConfigs;
|
|
91
|
-
}
|
|
92
|
-
|
|
93
|
-
- (instancetype)init {
|
|
94
|
-
if (self = [super init]) {
|
|
95
|
-
calls = [[NSMutableDictionary alloc] init];
|
|
96
|
-
channelConfigs = [[NSMutableDictionary alloc] init];
|
|
97
|
-
self.grpcCallDeadlineSeconds = 120;
|
|
98
|
-
}
|
|
99
|
-
|
|
100
|
-
return self;
|
|
101
|
-
}
|
|
102
|
-
|
|
103
|
-
// Will be called when this module's first listener is added.
|
|
104
|
-
- (void)startObserving {
|
|
105
|
-
hasListeners = YES;
|
|
106
|
-
// Set up any upstream listeners or background tasks as necessary
|
|
107
|
-
}
|
|
108
|
-
|
|
109
|
-
// Will be called when this module's last listener is removed, or on dealloc.
|
|
110
|
-
- (void)stopObserving {
|
|
111
|
-
hasListeners = NO;
|
|
112
|
-
// Remove upstream listeners, stop unnecessary background tasks
|
|
113
|
-
}
|
|
114
|
-
|
|
115
|
-
- (NSArray<NSString *> *)supportedEvents {
|
|
116
|
-
return @[@"grpc-call"];
|
|
117
|
-
}
|
|
118
|
-
|
|
119
|
-
- (NSMutableDictionary *)configForChannelId:(NSString *)channelId {
|
|
120
|
-
NSString *key = (channelId.length > 0) ? channelId : kGrpcDefaultChannelId;
|
|
121
|
-
if ([key isEqualToString:kGrpcDefaultChannelId]) {
|
|
122
|
-
NSMutableDictionary *cfg = [NSMutableDictionary dictionary];
|
|
123
|
-
if (self.grpcHost != nil) {
|
|
124
|
-
cfg[@"host"] = self.grpcHost;
|
|
125
|
-
}
|
|
126
|
-
cfg[@"insecure"] = @(self.grpcInsecure);
|
|
127
|
-
cfg[@"callDeadlineSeconds"] = @(self.grpcCallDeadlineSeconds > 0 ? self.grpcCallDeadlineSeconds : 120);
|
|
128
|
-
if (self.grpcResponseSizeLimit != nil) {
|
|
129
|
-
cfg[@"responseSizeLimit"] = self.grpcResponseSizeLimit;
|
|
130
|
-
}
|
|
131
|
-
if (self.grpcRootCertsPem.length > 0) {
|
|
132
|
-
cfg[@"rootCertsPem"] = self.grpcRootCertsPem;
|
|
133
|
-
}
|
|
134
|
-
if (self.grpcCertificateChainPem.length > 0) {
|
|
135
|
-
cfg[@"certificateChainPem"] = self.grpcCertificateChainPem;
|
|
136
|
-
}
|
|
137
|
-
if (self.grpcPrivateKeyPem.length > 0) {
|
|
138
|
-
cfg[@"privateKeyPem"] = self.grpcPrivateKeyPem;
|
|
139
|
-
}
|
|
140
|
-
if (self.grpcHostNameOverride.length > 0) {
|
|
141
|
-
cfg[@"hostNameOverride"] = self.grpcHostNameOverride;
|
|
142
|
-
}
|
|
143
|
-
if (self.grpcSpkiSha256Pins.count > 0) {
|
|
144
|
-
cfg[@"spkiSha256Pins"] = self.grpcSpkiSha256Pins;
|
|
145
|
-
}
|
|
146
|
-
return cfg;
|
|
147
|
-
}
|
|
148
|
-
return channelConfigs[key];
|
|
149
|
-
}
|
|
150
|
-
|
|
151
|
-
- (GRPCCallOptions *)getCallOptionsWithHeaders:(NSDictionary *)headers
|
|
152
|
-
deadlineSeconds:(NSNumber *)deadlineSeconds
|
|
153
|
-
channelConfig:(NSDictionary *)cfg {
|
|
154
|
-
GRPCMutableCallOptions *options = [[GRPCMutableCallOptions alloc] init];
|
|
155
|
-
options.initialMetadata = headers;
|
|
156
|
-
BOOL insecure = [cfg[@"insecure"] boolValue];
|
|
157
|
-
options.transport = insecure ? GRPCDefaultTransportImplList.core_insecure : GRPCDefaultTransportImplList.core_secure;
|
|
158
|
-
|
|
159
|
-
if (!insecure) {
|
|
160
|
-
NSString *rootCerts = cfg[@"rootCertsPem"];
|
|
161
|
-
NSString *certChain = cfg[@"certificateChainPem"];
|
|
162
|
-
NSString *privateKey = cfg[@"privateKeyPem"];
|
|
163
|
-
NSString *hostOverride = cfg[@"hostNameOverride"];
|
|
164
|
-
if (rootCerts.length > 0) {
|
|
165
|
-
options.PEMRootCertificates = rootCerts;
|
|
166
|
-
}
|
|
167
|
-
if (certChain.length > 0) {
|
|
168
|
-
options.PEMCertificateChain = certChain;
|
|
169
|
-
}
|
|
170
|
-
if (privateKey.length > 0) {
|
|
171
|
-
options.PEMPrivateKey = privateKey;
|
|
172
|
-
}
|
|
173
|
-
if (hostOverride.length > 0) {
|
|
174
|
-
options.hostNameOverride = hostOverride;
|
|
175
|
-
}
|
|
176
|
-
}
|
|
177
|
-
|
|
178
|
-
NSNumber *responseSizeLimit = cfg[@"responseSizeLimit"];
|
|
179
|
-
if (responseSizeLimit != nil) {
|
|
180
|
-
options.responseSizeLimit = responseSizeLimit.unsignedLongValue;
|
|
181
|
-
}
|
|
182
|
-
NSTimeInterval deadline;
|
|
183
|
-
if (deadlineSeconds != nil) {
|
|
184
|
-
deadline = MAX(0, [deadlineSeconds doubleValue]);
|
|
185
|
-
} else {
|
|
186
|
-
NSNumber *channelDeadline = cfg[@"callDeadlineSeconds"];
|
|
187
|
-
if (channelDeadline != nil && [channelDeadline doubleValue] > 0) {
|
|
188
|
-
deadline = [channelDeadline doubleValue];
|
|
189
|
-
} else {
|
|
190
|
-
deadline = 120;
|
|
191
|
-
}
|
|
192
|
-
}
|
|
193
|
-
if (deadline > 0) {
|
|
194
|
-
options.timeout = deadline;
|
|
195
|
-
}
|
|
196
|
-
|
|
197
|
-
return options;
|
|
198
|
-
}
|
|
199
|
-
|
|
200
|
-
RCT_EXPORT_METHOD(getHost:
|
|
201
|
-
(RCTPromiseResolveBlock) resolve
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
NSString *
|
|
226
|
-
NSString *
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
self.
|
|
255
|
-
self.
|
|
256
|
-
self.
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
cfg
|
|
308
|
-
|
|
309
|
-
cfg[@"
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
NSString *
|
|
323
|
-
NSString *
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
}
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
[
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
|
|
419
|
-
[
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
|
|
424
|
-
|
|
425
|
-
|
|
426
|
-
|
|
427
|
-
|
|
428
|
-
|
|
429
|
-
|
|
430
|
-
|
|
431
|
-
|
|
432
|
-
|
|
433
|
-
|
|
434
|
-
|
|
435
|
-
resolve([NSNumber numberWithBool:
|
|
436
|
-
}
|
|
437
|
-
|
|
438
|
-
|
|
439
|
-
|
|
440
|
-
|
|
441
|
-
|
|
442
|
-
|
|
443
|
-
|
|
444
|
-
|
|
445
|
-
|
|
446
|
-
|
|
447
|
-
|
|
448
|
-
|
|
449
|
-
|
|
450
|
-
|
|
451
|
-
|
|
452
|
-
|
|
453
|
-
|
|
454
|
-
|
|
455
|
-
|
|
456
|
-
|
|
457
|
-
|
|
458
|
-
|
|
459
|
-
|
|
460
|
-
|
|
461
|
-
|
|
462
|
-
|
|
463
|
-
|
|
464
|
-
|
|
465
|
-
|
|
466
|
-
|
|
467
|
-
|
|
468
|
-
|
|
469
|
-
|
|
470
|
-
|
|
471
|
-
|
|
472
|
-
|
|
473
|
-
|
|
474
|
-
|
|
475
|
-
|
|
476
|
-
|
|
477
|
-
|
|
478
|
-
|
|
479
|
-
|
|
480
|
-
|
|
481
|
-
|
|
482
|
-
|
|
483
|
-
|
|
484
|
-
|
|
485
|
-
|
|
486
|
-
|
|
487
|
-
|
|
488
|
-
|
|
489
|
-
|
|
490
|
-
|
|
491
|
-
|
|
492
|
-
|
|
493
|
-
|
|
494
|
-
|
|
495
|
-
|
|
496
|
-
|
|
497
|
-
|
|
498
|
-
|
|
499
|
-
|
|
500
|
-
|
|
501
|
-
|
|
502
|
-
|
|
503
|
-
|
|
504
|
-
|
|
505
|
-
|
|
506
|
-
resolve([NSNumber numberWithBool:
|
|
507
|
-
}
|
|
508
|
-
|
|
509
|
-
|
|
510
|
-
|
|
511
|
-
|
|
512
|
-
|
|
513
|
-
|
|
514
|
-
|
|
515
|
-
|
|
516
|
-
|
|
517
|
-
|
|
518
|
-
|
|
519
|
-
|
|
520
|
-
|
|
521
|
-
|
|
522
|
-
|
|
523
|
-
|
|
524
|
-
|
|
525
|
-
|
|
526
|
-
|
|
527
|
-
|
|
528
|
-
|
|
529
|
-
|
|
530
|
-
|
|
531
|
-
|
|
532
|
-
|
|
533
|
-
|
|
534
|
-
|
|
535
|
-
|
|
536
|
-
|
|
537
|
-
|
|
538
|
-
|
|
539
|
-
|
|
540
|
-
|
|
541
|
-
|
|
542
|
-
|
|
543
|
-
|
|
544
|
-
|
|
545
|
-
|
|
546
|
-
|
|
547
|
-
|
|
548
|
-
|
|
549
|
-
|
|
550
|
-
|
|
551
|
-
|
|
552
|
-
|
|
553
|
-
|
|
554
|
-
|
|
555
|
-
|
|
556
|
-
|
|
557
|
-
|
|
558
|
-
|
|
559
|
-
|
|
560
|
-
@"
|
|
561
|
-
|
|
562
|
-
|
|
563
|
-
|
|
564
|
-
|
|
565
|
-
|
|
566
|
-
|
|
567
|
-
|
|
568
|
-
|
|
569
|
-
|
|
570
|
-
|
|
571
|
-
|
|
572
|
-
|
|
573
|
-
@"
|
|
574
|
-
|
|
575
|
-
|
|
576
|
-
|
|
577
|
-
|
|
578
|
-
|
|
579
|
-
|
|
580
|
-
|
|
581
|
-
|
|
582
|
-
|
|
583
|
-
|
|
584
|
-
|
|
585
|
-
|
|
586
|
-
|
|
587
|
-
|
|
588
|
-
|
|
589
|
-
|
|
590
|
-
|
|
591
|
-
|
|
592
|
-
|
|
593
|
-
|
|
594
|
-
|
|
595
|
-
@"
|
|
596
|
-
@"
|
|
597
|
-
@"
|
|
598
|
-
|
|
599
|
-
|
|
600
|
-
|
|
601
|
-
|
|
602
|
-
|
|
603
|
-
|
|
604
|
-
|
|
605
|
-
@"
|
|
606
|
-
|
|
607
|
-
|
|
608
|
-
|
|
609
|
-
|
|
610
|
-
|
|
611
|
-
|
|
612
|
-
|
|
613
|
-
|
|
614
|
-
|
|
615
|
-
|
|
616
|
-
|
|
617
|
-
|
|
618
|
-
|
|
619
|
-
|
|
620
|
-
|
|
621
|
-
|
|
622
|
-
|
|
623
|
-
|
|
624
|
-
|
|
625
|
-
|
|
1
|
+
#import "Grpc.h"
|
|
2
|
+
#import "SpkiPinning.h"
|
|
3
|
+
#import <GRPCClient/GRPCCall.h>
|
|
4
|
+
#import <GRPCClient/GRPCTransport.h>
|
|
5
|
+
|
|
6
|
+
static NSString * const kGrpcDefaultChannelId = @"default";
|
|
7
|
+
|
|
8
|
+
@interface GrpcResponseHandler : NSObject <GRPCResponseHandler>
|
|
9
|
+
|
|
10
|
+
- (instancetype)initWithInitialMetadataCallback:(void (^)(NSDictionary *))initialMetadataCallback
|
|
11
|
+
messageCallback:(void (^)(id))messageCallback
|
|
12
|
+
closeCallback:(void (^)(NSDictionary *, NSError *))closeCallback
|
|
13
|
+
writeDataCallback:(void (^)(void))writeDataCallback;
|
|
14
|
+
|
|
15
|
+
- (instancetype)initWithInitialMetadataCallback:(void (^)(NSDictionary *))initialMetadataCallback
|
|
16
|
+
messageCallback:(void (^)(id))messageCallback
|
|
17
|
+
closeCallback:(void (^)(NSDictionary *, NSError *))closeCallback;
|
|
18
|
+
|
|
19
|
+
@end
|
|
20
|
+
|
|
21
|
+
@implementation GrpcResponseHandler {
|
|
22
|
+
void (^_initialMetadataCallback)(NSDictionary *);
|
|
23
|
+
|
|
24
|
+
void (^_messageCallback)(id);
|
|
25
|
+
|
|
26
|
+
void (^_closeCallback)(NSDictionary *, NSError *);
|
|
27
|
+
|
|
28
|
+
void (^_writeDataCallback)(void);
|
|
29
|
+
|
|
30
|
+
dispatch_queue_t _dispatchQueue;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
- (instancetype)initWithInitialMetadataCallback:(void (^)(NSDictionary *))initialMetadataCallback
|
|
34
|
+
messageCallback:(void (^)(id))messageCallback
|
|
35
|
+
closeCallback:(void (^)(NSDictionary *, NSError *))closeCallback
|
|
36
|
+
writeDataCallback:(void (^)(void))writeDataCallback {
|
|
37
|
+
if ((self = [super init])) {
|
|
38
|
+
_initialMetadataCallback = initialMetadataCallback;
|
|
39
|
+
_messageCallback = messageCallback;
|
|
40
|
+
_closeCallback = closeCallback;
|
|
41
|
+
_writeDataCallback = writeDataCallback;
|
|
42
|
+
_dispatchQueue = dispatch_queue_create(nil, DISPATCH_QUEUE_SERIAL);
|
|
43
|
+
}
|
|
44
|
+
return self;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
- (instancetype)initWithInitialMetadataCallback:(void (^)(NSDictionary *))initialMetadataCallback
|
|
48
|
+
messageCallback:(void (^)(id))messageCallback
|
|
49
|
+
closeCallback:(void (^)(NSDictionary *, NSError *))closeCallback {
|
|
50
|
+
return [self initWithInitialMetadataCallback:initialMetadataCallback
|
|
51
|
+
messageCallback:messageCallback
|
|
52
|
+
closeCallback:closeCallback
|
|
53
|
+
writeDataCallback:nil];
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
- (void)didReceiveInitialMetadata:(NSDictionary *)initialMetadata {
|
|
57
|
+
if (self->_initialMetadataCallback) {
|
|
58
|
+
self->_initialMetadataCallback(initialMetadata);
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
- (void)didReceiveRawMessage:(id)message {
|
|
63
|
+
if (self->_messageCallback) {
|
|
64
|
+
self->_messageCallback(message);
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
- (void)didCloseWithTrailingMetadata:(NSDictionary *)trailingMetadata error:(NSError *)error {
|
|
69
|
+
if (self->_closeCallback) {
|
|
70
|
+
self->_closeCallback(trailingMetadata, error);
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
- (void)didWriteData {
|
|
75
|
+
if (self->_writeDataCallback) {
|
|
76
|
+
self->_writeDataCallback();
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
- (dispatch_queue_t)dispatchQueue {
|
|
81
|
+
return _dispatchQueue;
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
@end
|
|
85
|
+
|
|
86
|
+
@implementation Grpc {
|
|
87
|
+
bool hasListeners;
|
|
88
|
+
NSMutableDictionary<NSNumber *, GRPCCall2 *> *calls;
|
|
89
|
+
/** channelId → mutable config dictionary (extra channels; default uses properties). */
|
|
90
|
+
NSMutableDictionary<NSString *, NSMutableDictionary *> *channelConfigs;
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
- (instancetype)init {
|
|
94
|
+
if (self = [super init]) {
|
|
95
|
+
calls = [[NSMutableDictionary alloc] init];
|
|
96
|
+
channelConfigs = [[NSMutableDictionary alloc] init];
|
|
97
|
+
self.grpcCallDeadlineSeconds = 120;
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
return self;
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
// Will be called when this module's first listener is added.
|
|
104
|
+
- (void)startObserving {
|
|
105
|
+
hasListeners = YES;
|
|
106
|
+
// Set up any upstream listeners or background tasks as necessary
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
// Will be called when this module's last listener is removed, or on dealloc.
|
|
110
|
+
- (void)stopObserving {
|
|
111
|
+
hasListeners = NO;
|
|
112
|
+
// Remove upstream listeners, stop unnecessary background tasks
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
- (NSArray<NSString *> *)supportedEvents {
|
|
116
|
+
return @[@"grpc-call"];
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
- (NSMutableDictionary *)configForChannelId:(NSString *)channelId {
|
|
120
|
+
NSString *key = (channelId.length > 0) ? channelId : kGrpcDefaultChannelId;
|
|
121
|
+
if ([key isEqualToString:kGrpcDefaultChannelId]) {
|
|
122
|
+
NSMutableDictionary *cfg = [NSMutableDictionary dictionary];
|
|
123
|
+
if (self.grpcHost != nil) {
|
|
124
|
+
cfg[@"host"] = self.grpcHost;
|
|
125
|
+
}
|
|
126
|
+
cfg[@"insecure"] = @(self.grpcInsecure);
|
|
127
|
+
cfg[@"callDeadlineSeconds"] = @(self.grpcCallDeadlineSeconds > 0 ? self.grpcCallDeadlineSeconds : 120);
|
|
128
|
+
if (self.grpcResponseSizeLimit != nil) {
|
|
129
|
+
cfg[@"responseSizeLimit"] = self.grpcResponseSizeLimit;
|
|
130
|
+
}
|
|
131
|
+
if (self.grpcRootCertsPem.length > 0) {
|
|
132
|
+
cfg[@"rootCertsPem"] = self.grpcRootCertsPem;
|
|
133
|
+
}
|
|
134
|
+
if (self.grpcCertificateChainPem.length > 0) {
|
|
135
|
+
cfg[@"certificateChainPem"] = self.grpcCertificateChainPem;
|
|
136
|
+
}
|
|
137
|
+
if (self.grpcPrivateKeyPem.length > 0) {
|
|
138
|
+
cfg[@"privateKeyPem"] = self.grpcPrivateKeyPem;
|
|
139
|
+
}
|
|
140
|
+
if (self.grpcHostNameOverride.length > 0) {
|
|
141
|
+
cfg[@"hostNameOverride"] = self.grpcHostNameOverride;
|
|
142
|
+
}
|
|
143
|
+
if (self.grpcSpkiSha256Pins.count > 0) {
|
|
144
|
+
cfg[@"spkiSha256Pins"] = self.grpcSpkiSha256Pins;
|
|
145
|
+
}
|
|
146
|
+
return cfg;
|
|
147
|
+
}
|
|
148
|
+
return channelConfigs[key];
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
- (GRPCCallOptions *)getCallOptionsWithHeaders:(NSDictionary *)headers
|
|
152
|
+
deadlineSeconds:(NSNumber *)deadlineSeconds
|
|
153
|
+
channelConfig:(NSDictionary *)cfg {
|
|
154
|
+
GRPCMutableCallOptions *options = [[GRPCMutableCallOptions alloc] init];
|
|
155
|
+
options.initialMetadata = headers;
|
|
156
|
+
BOOL insecure = [cfg[@"insecure"] boolValue];
|
|
157
|
+
options.transport = insecure ? GRPCDefaultTransportImplList.core_insecure : GRPCDefaultTransportImplList.core_secure;
|
|
158
|
+
|
|
159
|
+
if (!insecure) {
|
|
160
|
+
NSString *rootCerts = cfg[@"rootCertsPem"];
|
|
161
|
+
NSString *certChain = cfg[@"certificateChainPem"];
|
|
162
|
+
NSString *privateKey = cfg[@"privateKeyPem"];
|
|
163
|
+
NSString *hostOverride = cfg[@"hostNameOverride"];
|
|
164
|
+
if (rootCerts.length > 0) {
|
|
165
|
+
options.PEMRootCertificates = rootCerts;
|
|
166
|
+
}
|
|
167
|
+
if (certChain.length > 0) {
|
|
168
|
+
options.PEMCertificateChain = certChain;
|
|
169
|
+
}
|
|
170
|
+
if (privateKey.length > 0) {
|
|
171
|
+
options.PEMPrivateKey = privateKey;
|
|
172
|
+
}
|
|
173
|
+
if (hostOverride.length > 0) {
|
|
174
|
+
options.hostNameOverride = hostOverride;
|
|
175
|
+
}
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
NSNumber *responseSizeLimit = cfg[@"responseSizeLimit"];
|
|
179
|
+
if (responseSizeLimit != nil) {
|
|
180
|
+
options.responseSizeLimit = responseSizeLimit.unsignedLongValue;
|
|
181
|
+
}
|
|
182
|
+
NSTimeInterval deadline;
|
|
183
|
+
if (deadlineSeconds != nil) {
|
|
184
|
+
deadline = MAX(0, [deadlineSeconds doubleValue]);
|
|
185
|
+
} else {
|
|
186
|
+
NSNumber *channelDeadline = cfg[@"callDeadlineSeconds"];
|
|
187
|
+
if (channelDeadline != nil && [channelDeadline doubleValue] > 0) {
|
|
188
|
+
deadline = [channelDeadline doubleValue];
|
|
189
|
+
} else {
|
|
190
|
+
deadline = 120;
|
|
191
|
+
}
|
|
192
|
+
}
|
|
193
|
+
if (deadline > 0) {
|
|
194
|
+
options.timeout = deadline;
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
return options;
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
RCT_EXPORT_METHOD(getHost:
|
|
201
|
+
(RCTPromiseResolveBlock) resolve
|
|
202
|
+
rejecter:(RCTPromiseRejectBlock) reject) {
|
|
203
|
+
resolve(self.grpcHost);
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
RCT_EXPORT_METHOD(getIsInsecure:
|
|
207
|
+
(RCTPromiseResolveBlock) resolve
|
|
208
|
+
rejecter:(RCTPromiseRejectBlock) reject) {
|
|
209
|
+
resolve([NSNumber numberWithBool:self.grpcInsecure]);
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
RCT_EXPORT_METHOD(setHost:
|
|
213
|
+
(NSString *) host) {
|
|
214
|
+
self.grpcHost = host;
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
|
|
218
|
+
RCT_EXPORT_METHOD(setInsecure:
|
|
219
|
+
(nonnull NSNumber*) insecure) {
|
|
220
|
+
self.grpcInsecure = [insecure boolValue];
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
RCT_EXPORT_METHOD(setTlsOptions:
|
|
224
|
+
(NSDictionary *) options) {
|
|
225
|
+
NSString *rootCerts = [self tlsStringFromOptions:options key:@"rootCertsPem"];
|
|
226
|
+
NSString *certChain = [self tlsStringFromOptions:options key:@"certificateChainPem"];
|
|
227
|
+
NSString *privateKey = [self tlsStringFromOptions:options key:@"privateKeyPem"];
|
|
228
|
+
NSString *hostOverride = [self tlsStringFromOptions:options key:@"hostNameOverride"];
|
|
229
|
+
NSArray<NSString *> *pins = SpkiNormalizePins(options[@"spkiSha256Pins"]);
|
|
230
|
+
|
|
231
|
+
BOOL hasCert = certChain.length > 0;
|
|
232
|
+
BOOL hasKey = privateKey.length > 0;
|
|
233
|
+
if (hasCert != hasKey) {
|
|
234
|
+
@throw [NSException exceptionWithName:NSInvalidArgumentException
|
|
235
|
+
reason:@"mTLS requires both certificateChainPem and privateKeyPem"
|
|
236
|
+
userInfo:nil];
|
|
237
|
+
}
|
|
238
|
+
|
|
239
|
+
if (pins.count > 0) {
|
|
240
|
+
// gRPC ObjC has no handshake SPKI hook; pin by trusting only matching PEM certs.
|
|
241
|
+
if (rootCerts.length == 0) {
|
|
242
|
+
@throw [NSException exceptionWithName:NSInvalidArgumentException
|
|
243
|
+
reason:@"spkiSha256Pins on iOS requires rootCertsPem (include the pinned leaf or intermediate PEM)"
|
|
244
|
+
userInfo:nil];
|
|
245
|
+
}
|
|
246
|
+
NSError *pinError = nil;
|
|
247
|
+
if (!SpkiPemCertificatesMatchPins(rootCerts, pins, &pinError)) {
|
|
248
|
+
@throw [NSException exceptionWithName:NSInvalidArgumentException
|
|
249
|
+
reason:pinError.localizedDescription ?: @"SPKI pin mismatch for rootCertsPem"
|
|
250
|
+
userInfo:nil];
|
|
251
|
+
}
|
|
252
|
+
}
|
|
253
|
+
|
|
254
|
+
self.grpcRootCertsPem = rootCerts;
|
|
255
|
+
self.grpcCertificateChainPem = certChain;
|
|
256
|
+
self.grpcPrivateKeyPem = privateKey;
|
|
257
|
+
self.grpcHostNameOverride = hostOverride;
|
|
258
|
+
self.grpcSpkiSha256Pins = pins;
|
|
259
|
+
}
|
|
260
|
+
|
|
261
|
+
- (NSString *)tlsStringFromOptions:(NSDictionary *)options key:(NSString *)key {
|
|
262
|
+
id value = options[key];
|
|
263
|
+
if (value == nil || value == [NSNull null]) {
|
|
264
|
+
return nil;
|
|
265
|
+
}
|
|
266
|
+
if (![value isKindOfClass:[NSString class]]) {
|
|
267
|
+
return nil;
|
|
268
|
+
}
|
|
269
|
+
NSString *string = (NSString *)value;
|
|
270
|
+
return string.length > 0 ? string : nil;
|
|
271
|
+
}
|
|
272
|
+
|
|
273
|
+
RCT_EXPORT_METHOD(setResponseSizeLimit:
|
|
274
|
+
(nonnull NSNumber*) limit) {
|
|
275
|
+
self.grpcResponseSizeLimit = limit;
|
|
276
|
+
}
|
|
277
|
+
|
|
278
|
+
RCT_EXPORT_METHOD(setCallDeadlineSeconds:
|
|
279
|
+
(nonnull NSNumber*) seconds) {
|
|
280
|
+
self.grpcCallDeadlineSeconds = MAX(0, [seconds doubleValue]);
|
|
281
|
+
}
|
|
282
|
+
|
|
283
|
+
/** No-op on iOS (host applied per call); kept for API parity with Android. */
|
|
284
|
+
RCT_EXPORT_METHOD(initGrpcChannel) {
|
|
285
|
+
}
|
|
286
|
+
|
|
287
|
+
RCT_EXPORT_METHOD(createChannel:
|
|
288
|
+
(NSString *)channelId
|
|
289
|
+
config:(NSDictionary *)config) {
|
|
290
|
+
if (channelId.length == 0) {
|
|
291
|
+
@throw [NSException exceptionWithName:NSInvalidArgumentException
|
|
292
|
+
reason:@"channelId is required"
|
|
293
|
+
userInfo:nil];
|
|
294
|
+
}
|
|
295
|
+
if ([channelId isEqualToString:kGrpcDefaultChannelId]) {
|
|
296
|
+
@throw [NSException exceptionWithName:NSInvalidArgumentException
|
|
297
|
+
reason:@"channelId \"default\" is reserved for GrpcClient"
|
|
298
|
+
userInfo:nil];
|
|
299
|
+
}
|
|
300
|
+
NSString *host = config[@"host"];
|
|
301
|
+
if (![host isKindOfClass:[NSString class]] || host.length == 0) {
|
|
302
|
+
@throw [NSException exceptionWithName:NSInvalidArgumentException
|
|
303
|
+
reason:@"createChannel requires host"
|
|
304
|
+
userInfo:nil];
|
|
305
|
+
}
|
|
306
|
+
|
|
307
|
+
NSMutableDictionary *cfg = [NSMutableDictionary dictionary];
|
|
308
|
+
cfg[@"host"] = host;
|
|
309
|
+
cfg[@"insecure"] = @([config[@"insecure"] boolValue]);
|
|
310
|
+
NSNumber *deadline = config[@"callDeadlineSeconds"];
|
|
311
|
+
cfg[@"callDeadlineSeconds"] = (deadline != nil && deadline != (id)[NSNull null])
|
|
312
|
+
? @(MAX(0, [deadline doubleValue]))
|
|
313
|
+
: @(120);
|
|
314
|
+
id responseSize = config[@"responseSizeLimit"];
|
|
315
|
+
if (responseSize != nil && responseSize != [NSNull null]) {
|
|
316
|
+
cfg[@"responseSizeLimit"] = responseSize;
|
|
317
|
+
}
|
|
318
|
+
|
|
319
|
+
id tlsObj = config[@"tls"];
|
|
320
|
+
if ([tlsObj isKindOfClass:[NSDictionary class]]) {
|
|
321
|
+
NSDictionary *tls = (NSDictionary *)tlsObj;
|
|
322
|
+
NSString *rootCerts = [self tlsStringFromOptions:tls key:@"rootCertsPem"];
|
|
323
|
+
NSString *certChain = [self tlsStringFromOptions:tls key:@"certificateChainPem"];
|
|
324
|
+
NSString *privateKey = [self tlsStringFromOptions:tls key:@"privateKeyPem"];
|
|
325
|
+
NSString *hostOverride = [self tlsStringFromOptions:tls key:@"hostNameOverride"];
|
|
326
|
+
NSArray<NSString *> *pins = SpkiNormalizePins(tls[@"spkiSha256Pins"]);
|
|
327
|
+
|
|
328
|
+
BOOL hasCert = certChain.length > 0;
|
|
329
|
+
BOOL hasKey = privateKey.length > 0;
|
|
330
|
+
if (hasCert != hasKey) {
|
|
331
|
+
@throw [NSException exceptionWithName:NSInvalidArgumentException
|
|
332
|
+
reason:@"mTLS requires both certificateChainPem and privateKeyPem"
|
|
333
|
+
userInfo:nil];
|
|
334
|
+
}
|
|
335
|
+
if (pins.count > 0) {
|
|
336
|
+
if (rootCerts.length == 0) {
|
|
337
|
+
@throw [NSException exceptionWithName:NSInvalidArgumentException
|
|
338
|
+
reason:@"spkiSha256Pins on iOS requires rootCertsPem (include the pinned leaf or intermediate PEM)"
|
|
339
|
+
userInfo:nil];
|
|
340
|
+
}
|
|
341
|
+
NSError *pinError = nil;
|
|
342
|
+
if (!SpkiPemCertificatesMatchPins(rootCerts, pins, &pinError)) {
|
|
343
|
+
@throw [NSException exceptionWithName:NSInvalidArgumentException
|
|
344
|
+
reason:pinError.localizedDescription ?: @"SPKI pin mismatch for rootCertsPem"
|
|
345
|
+
userInfo:nil];
|
|
346
|
+
}
|
|
347
|
+
}
|
|
348
|
+
if (rootCerts.length > 0) {
|
|
349
|
+
cfg[@"rootCertsPem"] = rootCerts;
|
|
350
|
+
}
|
|
351
|
+
if (certChain.length > 0) {
|
|
352
|
+
cfg[@"certificateChainPem"] = certChain;
|
|
353
|
+
}
|
|
354
|
+
if (privateKey.length > 0) {
|
|
355
|
+
cfg[@"privateKeyPem"] = privateKey;
|
|
356
|
+
}
|
|
357
|
+
if (hostOverride.length > 0) {
|
|
358
|
+
cfg[@"hostNameOverride"] = hostOverride;
|
|
359
|
+
}
|
|
360
|
+
if (pins.count > 0) {
|
|
361
|
+
cfg[@"spkiSha256Pins"] = pins;
|
|
362
|
+
}
|
|
363
|
+
}
|
|
364
|
+
|
|
365
|
+
channelConfigs[channelId] = cfg;
|
|
366
|
+
}
|
|
367
|
+
|
|
368
|
+
RCT_EXPORT_METHOD(closeChannel:
|
|
369
|
+
(NSString *)channelId) {
|
|
370
|
+
if (channelId.length == 0 || [channelId isEqualToString:kGrpcDefaultChannelId]) {
|
|
371
|
+
return;
|
|
372
|
+
}
|
|
373
|
+
[channelConfigs removeObjectForKey:channelId];
|
|
374
|
+
}
|
|
375
|
+
|
|
376
|
+
RCT_EXPORT_METHOD(unaryCall:
|
|
377
|
+
(nonnull NSNumber*)callId
|
|
378
|
+
path:(NSString*)path
|
|
379
|
+
obj:(NSDictionary*)obj
|
|
380
|
+
headers:(NSDictionary*)headers
|
|
381
|
+
channelId:(NSString*)channelId
|
|
382
|
+
resolver:(RCTPromiseResolveBlock)resolve
|
|
383
|
+
rejecter:(RCTPromiseRejectBlock)reject) {
|
|
384
|
+
NSData *requestData = [[NSData alloc] initWithBase64EncodedString:[obj valueForKey:@"data"] options:NSDataBase64DecodingIgnoreUnknownCharacters];
|
|
385
|
+
|
|
386
|
+
NSError *startError = nil;
|
|
387
|
+
GRPCCall2 *call = [self startGrpcCallWithId:callId path:path headers:headers deadlineFromObj:obj channelId:channelId error:&startError];
|
|
388
|
+
if (call == nil) {
|
|
389
|
+
reject(@"channel_error", startError.localizedDescription ?: @"Channel not created", startError);
|
|
390
|
+
return;
|
|
391
|
+
}
|
|
392
|
+
|
|
393
|
+
[call writeData:requestData];
|
|
394
|
+
[call finish];
|
|
395
|
+
|
|
396
|
+
[calls setObject:call forKey:callId];
|
|
397
|
+
|
|
398
|
+
resolve([NSNull null]);
|
|
399
|
+
}
|
|
400
|
+
|
|
401
|
+
RCT_EXPORT_METHOD(serverStreamingCall:
|
|
402
|
+
(nonnull NSNumber*)callId
|
|
403
|
+
path:(NSString*)path
|
|
404
|
+
obj:(NSDictionary*)obj
|
|
405
|
+
headers:(NSDictionary*)headers
|
|
406
|
+
channelId:(NSString*)channelId
|
|
407
|
+
resolver:(RCTPromiseResolveBlock)resolve
|
|
408
|
+
rejecter:(RCTPromiseRejectBlock)reject) {
|
|
409
|
+
NSData *requestData = [[NSData alloc] initWithBase64EncodedString:[obj valueForKey:@"data"] options:NSDataBase64DecodingIgnoreUnknownCharacters];
|
|
410
|
+
|
|
411
|
+
NSError *startError = nil;
|
|
412
|
+
GRPCCall2 *call = [self startGrpcCallWithId:callId path:path headers:headers deadlineFromObj:obj channelId:channelId error:&startError];
|
|
413
|
+
if (call == nil) {
|
|
414
|
+
reject(@"channel_error", startError.localizedDescription ?: @"Channel not created", startError);
|
|
415
|
+
return;
|
|
416
|
+
}
|
|
417
|
+
|
|
418
|
+
[call writeData:requestData];
|
|
419
|
+
[call finish];
|
|
420
|
+
|
|
421
|
+
[calls setObject:call forKey:callId];
|
|
422
|
+
|
|
423
|
+
resolve([NSNull null]);
|
|
424
|
+
}
|
|
425
|
+
|
|
426
|
+
RCT_EXPORT_METHOD(cancelGrpcCall:
|
|
427
|
+
(nonnull NSNumber*)callId
|
|
428
|
+
resolver:(RCTPromiseResolveBlock)resolve
|
|
429
|
+
rejecter:(RCTPromiseRejectBlock)reject) {
|
|
430
|
+
GRPCCall2 *call = [calls objectForKey:callId];
|
|
431
|
+
|
|
432
|
+
if (call != nil) {
|
|
433
|
+
[call cancel];
|
|
434
|
+
|
|
435
|
+
resolve([NSNumber numberWithBool:true]);
|
|
436
|
+
} else {
|
|
437
|
+
resolve([NSNumber numberWithBool:false]);
|
|
438
|
+
}
|
|
439
|
+
}
|
|
440
|
+
|
|
441
|
+
RCT_EXPORT_METHOD(clientStreamingCall:
|
|
442
|
+
(nonnull NSNumber*)callId
|
|
443
|
+
path:(NSString*)path
|
|
444
|
+
obj:(NSDictionary*)obj
|
|
445
|
+
headers:(NSDictionary*)headers
|
|
446
|
+
channelId:(NSString*)channelId
|
|
447
|
+
resolver:(RCTPromiseResolveBlock)resolve
|
|
448
|
+
rejecter:(RCTPromiseRejectBlock)reject) {
|
|
449
|
+
NSData *requestData = [[NSData alloc] initWithBase64EncodedString:[obj valueForKey:@"data"] options:NSDataBase64DecodingIgnoreUnknownCharacters];
|
|
450
|
+
|
|
451
|
+
GRPCCall2 *call = [calls objectForKey:callId];
|
|
452
|
+
|
|
453
|
+
if (call == nil) {
|
|
454
|
+
NSError *startError = nil;
|
|
455
|
+
call = [self startGrpcCallWithId:callId path:path headers:headers deadlineFromObj:obj channelId:channelId error:&startError];
|
|
456
|
+
if (call == nil) {
|
|
457
|
+
reject(@"channel_error", startError.localizedDescription ?: @"Channel not created", startError);
|
|
458
|
+
return;
|
|
459
|
+
}
|
|
460
|
+
|
|
461
|
+
[calls setObject:call forKey:callId];
|
|
462
|
+
}
|
|
463
|
+
|
|
464
|
+
[call writeData:requestData];
|
|
465
|
+
|
|
466
|
+
resolve([NSNull null]);
|
|
467
|
+
}
|
|
468
|
+
|
|
469
|
+
RCT_EXPORT_METHOD(bidiStreamingCall:
|
|
470
|
+
(nonnull NSNumber*)callId
|
|
471
|
+
path:(NSString*)path
|
|
472
|
+
obj:(NSDictionary*)obj
|
|
473
|
+
headers:(NSDictionary*)headers
|
|
474
|
+
channelId:(NSString*)channelId
|
|
475
|
+
resolver:(RCTPromiseResolveBlock)resolve
|
|
476
|
+
rejecter:(RCTPromiseRejectBlock)reject) {
|
|
477
|
+
NSData *requestData = [[NSData alloc] initWithBase64EncodedString:[obj valueForKey:@"data"] options:NSDataBase64DecodingIgnoreUnknownCharacters];
|
|
478
|
+
|
|
479
|
+
GRPCCall2 *call = [calls objectForKey:callId];
|
|
480
|
+
|
|
481
|
+
if (call == nil) {
|
|
482
|
+
NSError *startError = nil;
|
|
483
|
+
call = [self startGrpcCallWithId:callId path:path headers:headers deadlineFromObj:obj channelId:channelId error:&startError];
|
|
484
|
+
if (call == nil) {
|
|
485
|
+
reject(@"channel_error", startError.localizedDescription ?: @"Channel not created", startError);
|
|
486
|
+
return;
|
|
487
|
+
}
|
|
488
|
+
|
|
489
|
+
[calls setObject:call forKey:callId];
|
|
490
|
+
}
|
|
491
|
+
|
|
492
|
+
[call writeData:requestData];
|
|
493
|
+
|
|
494
|
+
resolve([NSNull null]);
|
|
495
|
+
}
|
|
496
|
+
|
|
497
|
+
RCT_EXPORT_METHOD(finishClientStreaming:
|
|
498
|
+
(nonnull NSNumber*)callId
|
|
499
|
+
resolver:(RCTPromiseResolveBlock)resolve
|
|
500
|
+
rejecter:(RCTPromiseRejectBlock)reject) {
|
|
501
|
+
GRPCCall2 *call = [calls objectForKey:callId];
|
|
502
|
+
|
|
503
|
+
if (call != nil) {
|
|
504
|
+
[call finish];
|
|
505
|
+
|
|
506
|
+
resolve([NSNumber numberWithBool:true]);
|
|
507
|
+
} else {
|
|
508
|
+
resolve([NSNumber numberWithBool:false]);
|
|
509
|
+
}
|
|
510
|
+
}
|
|
511
|
+
|
|
512
|
+
- (GRPCCall2 *)startGrpcCallWithId:(NSNumber *)callId
|
|
513
|
+
path:(NSString *)path
|
|
514
|
+
headers:(NSDictionary *)headers
|
|
515
|
+
deadlineFromObj:(NSDictionary *)obj
|
|
516
|
+
channelId:(NSString *)channelId
|
|
517
|
+
error:(NSError **)outError {
|
|
518
|
+
NSDictionary *cfg = [self configForChannelId:channelId];
|
|
519
|
+
if (cfg == nil) {
|
|
520
|
+
if (outError) {
|
|
521
|
+
*outError = [NSError errorWithDomain:@"Grpc"
|
|
522
|
+
code:0
|
|
523
|
+
userInfo:@{NSLocalizedDescriptionKey: [NSString stringWithFormat:@"Unknown channel: %@", channelId]}];
|
|
524
|
+
}
|
|
525
|
+
return nil;
|
|
526
|
+
}
|
|
527
|
+
NSString *host = cfg[@"host"];
|
|
528
|
+
if (host.length == 0) {
|
|
529
|
+
if (outError) {
|
|
530
|
+
*outError = [NSError errorWithDomain:@"Grpc"
|
|
531
|
+
code:0
|
|
532
|
+
userInfo:@{NSLocalizedDescriptionKey: @"Channel host not set"}];
|
|
533
|
+
}
|
|
534
|
+
return nil;
|
|
535
|
+
}
|
|
536
|
+
|
|
537
|
+
GRPCRequestOptions *requestOptions = [[GRPCRequestOptions alloc] initWithHost:host
|
|
538
|
+
path:path
|
|
539
|
+
safety:GRPCCallSafetyDefault];
|
|
540
|
+
|
|
541
|
+
NSNumber *deadlineOverride = nil;
|
|
542
|
+
id rawDeadline = [obj valueForKey:@"deadlineSeconds"];
|
|
543
|
+
if ([rawDeadline isKindOfClass:[NSNumber class]]) {
|
|
544
|
+
deadlineOverride = (NSNumber *)rawDeadline;
|
|
545
|
+
}
|
|
546
|
+
|
|
547
|
+
GRPCCallOptions *callOptions = [self getCallOptionsWithHeaders:headers deadlineSeconds:deadlineOverride channelConfig:cfg];
|
|
548
|
+
|
|
549
|
+
GrpcResponseHandler *handler = [[GrpcResponseHandler alloc] initWithInitialMetadataCallback:^(NSDictionary *initialMetadata) {
|
|
550
|
+
if (self->hasListeners) {
|
|
551
|
+
NSDictionary *responseHeaders = [[NSMutableDictionary alloc] initWithDictionary:initialMetadata];
|
|
552
|
+
|
|
553
|
+
[responseHeaders enumerateKeysAndObjectsUsingBlock:^(id key, id object, BOOL *exit) {
|
|
554
|
+
if ([object isKindOfClass:[NSData class]]) {
|
|
555
|
+
[responseHeaders setValue:[object base64EncodedStringWithOptions:0] forKey:key];
|
|
556
|
+
}
|
|
557
|
+
}];
|
|
558
|
+
|
|
559
|
+
NSDictionary *event = @{
|
|
560
|
+
@"id": callId,
|
|
561
|
+
@"type": @"headers",
|
|
562
|
+
@"payload": responseHeaders,
|
|
563
|
+
};
|
|
564
|
+
|
|
565
|
+
[self sendEventWithName:@"grpc-call" body:event];
|
|
566
|
+
}
|
|
567
|
+
}
|
|
568
|
+
messageCallback:^(id message) {
|
|
569
|
+
NSData *data = (NSData *) message;
|
|
570
|
+
|
|
571
|
+
if (self->hasListeners) {
|
|
572
|
+
NSDictionary *event = @{
|
|
573
|
+
@"id": callId,
|
|
574
|
+
@"type": @"response",
|
|
575
|
+
@"payload": [data base64EncodedStringWithOptions:nil],
|
|
576
|
+
};
|
|
577
|
+
|
|
578
|
+
[self sendEventWithName:@"grpc-call" body:event];
|
|
579
|
+
}
|
|
580
|
+
}
|
|
581
|
+
closeCallback:^(NSDictionary *trailingMetadata, NSError *error) {
|
|
582
|
+
[calls removeObjectForKey:callId];
|
|
583
|
+
|
|
584
|
+
NSDictionary *responseTrailers = [[NSMutableDictionary alloc] initWithDictionary:trailingMetadata];
|
|
585
|
+
|
|
586
|
+
[responseTrailers enumerateKeysAndObjectsUsingBlock:^(id key, id object, BOOL *exit) {
|
|
587
|
+
if ([object isKindOfClass:[NSData class]]) {
|
|
588
|
+
[responseTrailers setValue:[object base64EncodedStringWithOptions:0] forKey:key];
|
|
589
|
+
}
|
|
590
|
+
}];
|
|
591
|
+
|
|
592
|
+
if (self->hasListeners) {
|
|
593
|
+
if (error != nil) {
|
|
594
|
+
NSDictionary *event = @{
|
|
595
|
+
@"id": callId,
|
|
596
|
+
@"type": @"error",
|
|
597
|
+
@"error": error.localizedDescription,
|
|
598
|
+
@"code": [NSNumber numberWithLong:error.code],
|
|
599
|
+
@"trailers": responseTrailers,
|
|
600
|
+
};
|
|
601
|
+
|
|
602
|
+
[self sendEventWithName:@"grpc-call" body:event];
|
|
603
|
+
} else {
|
|
604
|
+
NSDictionary *event = @{
|
|
605
|
+
@"id": callId,
|
|
606
|
+
@"type": @"trailers",
|
|
607
|
+
@"payload": responseTrailers,
|
|
608
|
+
};
|
|
609
|
+
|
|
610
|
+
[self sendEventWithName:@"grpc-call" body:event];
|
|
611
|
+
}
|
|
612
|
+
}
|
|
613
|
+
}
|
|
614
|
+
];
|
|
615
|
+
|
|
616
|
+
GRPCCall2 *call = [[GRPCCall2 alloc] initWithRequestOptions:requestOptions
|
|
617
|
+
responseHandler:handler
|
|
618
|
+
callOptions:callOptions];
|
|
619
|
+
|
|
620
|
+
[call start];
|
|
621
|
+
|
|
622
|
+
return call;
|
|
623
|
+
}
|
|
624
|
+
|
|
625
|
+
RCT_EXPORT_MODULE()
|
|
626
|
+
|
|
627
|
+
@end
|