@xdcobra/react-native-grpc 1.0.0 → 1.1.8
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 +284 -114
- package/android/build.gradle +127 -127
- package/android/gradle.properties +4 -4
- 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 -585
- 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 +625 -439
- package/ios/SpkiPinning.h +22 -22
- package/ios/SpkiPinning.m +114 -114
- package/lib/commonjs/bidi-streaming.js.map +1 -1
- package/lib/commonjs/channel.js +84 -0
- package/lib/commonjs/channel.js.map +1 -0
- package/lib/commonjs/client-streaming.js +3 -3
- package/lib/commonjs/client-streaming.js.map +1 -1
- package/lib/commonjs/client.js +538 -135
- package/lib/commonjs/client.js.map +1 -1
- package/lib/commonjs/create-client.js +206 -0
- package/lib/commonjs/create-client.js.map +1 -0
- package/lib/commonjs/errors.js.map +1 -1
- package/lib/commonjs/index.js +32 -1
- package/lib/commonjs/index.js.map +1 -1
- package/lib/commonjs/interceptors.js +116 -0
- package/lib/commonjs/interceptors.js.map +1 -0
- package/lib/commonjs/native.js +24 -0
- package/lib/commonjs/native.js.map +1 -0
- package/lib/commonjs/retry.js +153 -0
- package/lib/commonjs/retry.js.map +1 -0
- package/lib/commonjs/server-streaming.js.map +1 -1
- package/lib/commonjs/status.js +54 -0
- package/lib/commonjs/status.js.map +1 -0
- package/lib/commonjs/types.js +29 -0
- package/lib/commonjs/types.js.map +1 -1
- package/lib/commonjs/unary.js.map +1 -1
- package/lib/module/bidi-streaming.js.map +1 -1
- package/lib/module/channel.js +76 -0
- package/lib/module/channel.js.map +1 -0
- package/lib/module/client-streaming.js +3 -3
- package/lib/module/client-streaming.js.map +1 -1
- package/lib/module/client.js +525 -129
- package/lib/module/client.js.map +1 -1
- package/lib/module/create-client.js +201 -0
- package/lib/module/create-client.js.map +1 -0
- package/lib/module/errors.js.map +1 -1
- package/lib/module/index.js +3 -0
- package/lib/module/index.js.map +1 -1
- package/lib/module/interceptors.js +104 -0
- package/lib/module/interceptors.js.map +1 -0
- package/lib/module/native.js +17 -0
- package/lib/module/native.js.map +1 -0
- package/lib/module/retry.js +141 -0
- package/lib/module/retry.js.map +1 -0
- package/lib/module/server-streaming.js.map +1 -1
- package/lib/module/status.js +46 -0
- package/lib/module/status.js.map +1 -0
- package/lib/module/types.js +17 -0
- package/lib/module/types.js.map +1 -1
- package/lib/module/unary.js.map +1 -1
- package/lib/typescript/channel.d.ts +27 -0
- package/lib/typescript/channel.d.ts.map +1 -0
- package/lib/typescript/client.d.ts +56 -49
- package/lib/typescript/client.d.ts.map +1 -1
- package/lib/typescript/create-client.d.ts +65 -0
- package/lib/typescript/create-client.d.ts.map +1 -0
- package/lib/typescript/index.d.ts +5 -0
- package/lib/typescript/index.d.ts.map +1 -1
- package/lib/typescript/interceptors.d.ts +43 -0
- package/lib/typescript/interceptors.d.ts.map +1 -0
- package/lib/typescript/native.d.ts +53 -0
- package/lib/typescript/native.d.ts.map +1 -0
- package/lib/typescript/retry.d.ts +56 -0
- package/lib/typescript/retry.d.ts.map +1 -0
- package/lib/typescript/status.d.ts +24 -0
- package/lib/typescript/status.d.ts.map +1 -0
- package/lib/typescript/types.d.ts +60 -0
- package/lib/typescript/types.d.ts.map +1 -1
- package/package.json +209 -169
- package/react-native-grpc.podspec +29 -29
- package/src/bidi-streaming.ts +78 -78
- package/src/channel.ts +110 -0
- package/src/client-streaming.ts +106 -106
- package/src/client.ts +1046 -521
- package/src/create-client.ts +391 -0
- package/src/errors.ts +11 -11
- package/src/index.ts +28 -11
- package/src/interceptors.ts +167 -0
- package/src/native.ts +99 -0
- package/src/retry.ts +208 -0
- package/src/server-streaming.ts +105 -105
- package/src/status.ts +50 -0
- package/src/types.ts +171 -107
- package/src/unary.ts +71 -71
package/LICENSE.md
CHANGED
|
@@ -1,22 +1,22 @@
|
|
|
1
|
-
|
|
2
|
-
The MIT License (MIT)
|
|
3
|
-
|
|
4
|
-
Copyright (c) 2021 Mitchell Kutchuk
|
|
5
|
-
|
|
6
|
-
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
7
|
-
of this software and associated documentation files (the "Software"), to deal
|
|
8
|
-
in the Software without restriction, including without limitation the rights
|
|
9
|
-
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
10
|
-
copies of the Software, and to permit persons to whom the Software is
|
|
11
|
-
furnished to do so, subject to the following conditions:
|
|
12
|
-
|
|
13
|
-
The above copyright notice and this permission notice shall be included in all
|
|
14
|
-
copies or substantial portions of the Software.
|
|
15
|
-
|
|
16
|
-
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
17
|
-
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
18
|
-
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
19
|
-
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
20
|
-
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
21
|
-
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
22
|
-
SOFTWARE.
|
|
1
|
+
|
|
2
|
+
The MIT License (MIT)
|
|
3
|
+
|
|
4
|
+
Copyright (c) 2021 Mitchell Kutchuk
|
|
5
|
+
|
|
6
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
7
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
8
|
+
in the Software without restriction, including without limitation the rights
|
|
9
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
10
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
11
|
+
furnished to do so, subject to the following conditions:
|
|
12
|
+
|
|
13
|
+
The above copyright notice and this permission notice shall be included in all
|
|
14
|
+
copies or substantial portions of the Software.
|
|
15
|
+
|
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
17
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
18
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
19
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
20
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
21
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
22
|
+
SOFTWARE.
|
package/README.md
CHANGED
|
@@ -1,114 +1,284 @@
|
|
|
1
|
-
# @xdcobra/react-native-grpc
|
|
2
|
-
|
|
3
|
-
Fork of [`@krishnafkh/react-native-grpc`](https://github.com/krishnafkh/react-native-grpc)
|
|
4
|
-
(itself based on [Mitch528/react-native-grpc](https://github.com/Mitch528/react-native-grpc)).
|
|
5
|
-
|
|
6
|
-
## Changes vs upstream
|
|
7
|
-
|
|
8
|
-
- Builds and runs with React Native New Architecture (Native Module interop)
|
|
9
|
-
- Android `namespace 'com.reactnativegrpc'` (no `package` in AndroidManifest)
|
|
10
|
-
- `minSdkVersion` 24
|
|
11
|
-
- Per-call deadline via `setCallDeadlineSeconds` (default 120s) and per-RPC `options.deadlineSeconds`
|
|
12
|
-
- Client streaming via `GrpcClient.clientStreamCall`
|
|
13
|
-
- Bidirectional streaming via `GrpcClient.bidiStreamCall`
|
|
14
|
-
- TLS options via `GrpcClient.setTlsOptions` (custom CA, mTLS, hostname override, SPKI pins)
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
```
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
//
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
'
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
```sh
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
1
|
+
# @xdcobra/react-native-grpc
|
|
2
|
+
|
|
3
|
+
Fork of [`@krishnafkh/react-native-grpc`](https://github.com/krishnafkh/react-native-grpc)
|
|
4
|
+
(itself based on [Mitch528/react-native-grpc](https://github.com/Mitch528/react-native-grpc)).
|
|
5
|
+
|
|
6
|
+
## Changes vs upstream
|
|
7
|
+
|
|
8
|
+
- Builds and runs with React Native New Architecture (Native Module interop)
|
|
9
|
+
- Android `namespace 'com.reactnativegrpc'` (no `package` in AndroidManifest)
|
|
10
|
+
- `minSdkVersion` 24
|
|
11
|
+
- Per-call deadline via `setCallDeadlineSeconds` (default 120s) and per-RPC `options.deadlineSeconds`
|
|
12
|
+
- Client streaming via `GrpcClient.clientStreamCall`
|
|
13
|
+
- Bidirectional streaming via `GrpcClient.bidiStreamCall`
|
|
14
|
+
- TLS options via `GrpcClient.setTlsOptions` (custom CA, mTLS, hostname override, SPKI pins)
|
|
15
|
+
- Multiple concurrent channels via `createChannel` / `GrpcChannel` (immutable per-channel config)
|
|
16
|
+
- Interceptors via `createChannel({ interceptors })` / `GrpcClient.setInterceptors` (auth, logging, remapping)
|
|
17
|
+
- Unary retry / hedging via `createChannel({ retry | hedging })` / `GrpcClient.setRetryPolicy` / `setHedgingPolicy` ([gRFC A6](https://github.com/grpc/proposal/blob/master/A6-client-retries.md)-style)
|
|
18
|
+
- Typed clients via Protobuf-ES `createClient` (Buf + `protoc-gen-es`)
|
|
19
|
+
|
|
20
|
+
## Installation
|
|
21
|
+
|
|
22
|
+
```sh
|
|
23
|
+
npm install @xdcobra/react-native-grpc
|
|
24
|
+
# Typed clients (optional peer):
|
|
25
|
+
npm install @bufbuild/protobuf
|
|
26
|
+
# or
|
|
27
|
+
npm install git+https://github.com/XDcobra/react-native-grpc.git
|
|
28
|
+
```
|
|
29
|
+
|
|
30
|
+
## Usage
|
|
31
|
+
|
|
32
|
+
```ts
|
|
33
|
+
import { GrpcClient, GrpcMetadata } from '@xdcobra/react-native-grpc';
|
|
34
|
+
|
|
35
|
+
GrpcClient.setHost('api.example.com:443');
|
|
36
|
+
GrpcClient.setInsecure(false); // TLS (system / gRPC default roots)
|
|
37
|
+
GrpcClient.setCallDeadlineSeconds(120); // global default
|
|
38
|
+
GrpcClient.initGrpcChannel();
|
|
39
|
+
|
|
40
|
+
// Plaintext (dev only)
|
|
41
|
+
// GrpcClient.setInsecure(true);
|
|
42
|
+
|
|
43
|
+
// Custom CA and/or mTLS and/or dial-by-IP hostname override
|
|
44
|
+
// (each setTlsOptions call replaces the previous TLS config)
|
|
45
|
+
GrpcClient.setTlsOptions({
|
|
46
|
+
rootCertsPem: caPem, // optional (required on iOS when using spkiSha256Pins)
|
|
47
|
+
certificateChainPem: clientCertPem, // mTLS (with privateKeyPem)
|
|
48
|
+
privateKeyPem: clientKeyPem,
|
|
49
|
+
hostNameOverride: 'api.example.com', // optional
|
|
50
|
+
spkiSha256Pins: ['sha256/AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA='],
|
|
51
|
+
});
|
|
52
|
+
GrpcClient.initGrpcChannel();
|
|
53
|
+
|
|
54
|
+
const { response } = await GrpcClient.unaryCall(
|
|
55
|
+
'/package.Service/Method',
|
|
56
|
+
requestBytes,
|
|
57
|
+
headers,
|
|
58
|
+
{ deadlineSeconds: 60 } // optional per-RPC override
|
|
59
|
+
);
|
|
60
|
+
|
|
61
|
+
// Client streaming: many request chunks, one response
|
|
62
|
+
const stream = GrpcClient.clientStreamCall('/package.Service/Upload', headers);
|
|
63
|
+
await stream.requests.send(chunk1);
|
|
64
|
+
await stream.requests.send(chunk2);
|
|
65
|
+
await stream.requests.complete();
|
|
66
|
+
const { response: uploadResponse } = await stream;
|
|
67
|
+
|
|
68
|
+
// Bidirectional streaming: interleaved requests and responses
|
|
69
|
+
const bidi = GrpcClient.bidiStreamCall('/package.Service/Chat', headers, {
|
|
70
|
+
deadlineSeconds: 60,
|
|
71
|
+
});
|
|
72
|
+
bidi.responses.on('data', (chunk) => {
|
|
73
|
+
/* handle server message */
|
|
74
|
+
});
|
|
75
|
+
await bidi.requests.send(chunk1);
|
|
76
|
+
await bidi.requests.send(chunk2);
|
|
77
|
+
await bidi.requests.complete(); // half-close outbound
|
|
78
|
+
const done = await bidi; // resolves on trailers
|
|
79
|
+
```
|
|
80
|
+
|
|
81
|
+
### Typed clients (Buf + createClient)
|
|
82
|
+
|
|
83
|
+
Generate messages **and** `GenService` schemas with Buf + `@bufbuild/protoc-gen-es` (no custom plugin). Then call `createClient(service, GrpcClient | channel)`.
|
|
84
|
+
|
|
85
|
+
```yaml
|
|
86
|
+
# buf.gen.yaml
|
|
87
|
+
version: v2
|
|
88
|
+
plugins:
|
|
89
|
+
- local: protoc-gen-es
|
|
90
|
+
out: src/gen
|
|
91
|
+
opt: target=ts
|
|
92
|
+
```
|
|
93
|
+
|
|
94
|
+
```sh
|
|
95
|
+
npm i @bufbuild/protobuf
|
|
96
|
+
npm i -D @bufbuild/buf @bufbuild/protoc-gen-es
|
|
97
|
+
npx buf generate
|
|
98
|
+
```
|
|
99
|
+
|
|
100
|
+
```ts
|
|
101
|
+
import { create } from '@bufbuild/protobuf';
|
|
102
|
+
import { createClient, GrpcClient } from '@xdcobra/react-native-grpc';
|
|
103
|
+
import { ExampleRequestSchema, Examples } from './gen/example_pb';
|
|
104
|
+
|
|
105
|
+
GrpcClient.setHost('api.example.com:443');
|
|
106
|
+
GrpcClient.initGrpcChannel();
|
|
107
|
+
|
|
108
|
+
const client = createClient(Examples, GrpcClient);
|
|
109
|
+
// or: createClient(Examples, createChannel({ host: '...' }))
|
|
110
|
+
|
|
111
|
+
const res = await client.sendExampleMessage(
|
|
112
|
+
create(ExampleRequestSchema, { message: 'Ada' }),
|
|
113
|
+
{ authorization: 'Bearer …' },
|
|
114
|
+
{ deadlineSeconds: 30 }
|
|
115
|
+
);
|
|
116
|
+
```
|
|
117
|
+
|
|
118
|
+
| `methodKind` | Client shape |
|
|
119
|
+
|---|---|
|
|
120
|
+
| `unary` | `Promise<O>` |
|
|
121
|
+
| `server_streaming` | async iterable + `.responses` / `.cancel()` |
|
|
122
|
+
| `client_streaming` | `{ send(I), complete(), response: Promise<O> }` |
|
|
123
|
+
| `bidi_streaming` | `{ send(I), complete(), responses }` (+ async iterable) |
|
|
124
|
+
|
|
125
|
+
Path form: `/${service.typeName}/${MethodName}` (protobuf RPC name). Raw `unaryCall` / stream APIs remain for BYO bytes.
|
|
126
|
+
|
|
127
|
+
Subpath import (optional): `import { createClient } from '@xdcobra/react-native-grpc/create-client'`.
|
|
128
|
+
|
|
129
|
+
### Multiple channels / hosts
|
|
130
|
+
|
|
131
|
+
Prefer `createChannel` when talking to more than one host (or when you want immutable per-channel TLS/deadline config). The singleton `GrpcClient` path above remains supported for single-host apps.
|
|
132
|
+
|
|
133
|
+
`createChannel` exposes the same RPC methods as `GrpcClient` (`unaryCall`, `serverStreamCall`, `clientStreamCall`, `bidiStreamCall`). Channel options (`host`, `insecure`, `tls`, deadline, keepalive, compression, response size) are set **once** in the config — there are no `setHost` / `setTlsOptions` mutators on the returned channel; reconfigure by creating a new channel.
|
|
134
|
+
|
|
135
|
+
```ts
|
|
136
|
+
import { createChannel } from '@xdcobra/react-native-grpc';
|
|
137
|
+
|
|
138
|
+
const primary = createChannel({
|
|
139
|
+
host: 'api.example.com:443',
|
|
140
|
+
insecure: false,
|
|
141
|
+
callDeadlineSeconds: 120,
|
|
142
|
+
tls: {
|
|
143
|
+
rootCertsPem: caPem, // optional (required on iOS when using spkiSha256Pins)
|
|
144
|
+
certificateChainPem: clientCertPem, // mTLS (with privateKeyPem)
|
|
145
|
+
privateKeyPem: clientKeyPem,
|
|
146
|
+
hostNameOverride: 'api.example.com', // optional
|
|
147
|
+
spkiSha256Pins: ['sha256/AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA='],
|
|
148
|
+
},
|
|
149
|
+
// optional:
|
|
150
|
+
// responseSizeLimit: 4 * 1024 * 1024,
|
|
151
|
+
// compression: { enable: true, compressorName: 'gzip' },
|
|
152
|
+
// keepAlive: { enable: true, keepAliveTime: 30, keepAliveTimeOut: 10 },
|
|
153
|
+
});
|
|
154
|
+
|
|
155
|
+
const staging = createChannel({
|
|
156
|
+
host: '10.0.2.2:50051',
|
|
157
|
+
insecure: true, // plaintext; `tls` is ignored
|
|
158
|
+
});
|
|
159
|
+
|
|
160
|
+
await primary.unaryCall('/package.Service/Method', requestBytes);
|
|
161
|
+
await staging.unaryCall('/package.Service/Method', requestBytes);
|
|
162
|
+
|
|
163
|
+
primary.close();
|
|
164
|
+
staging.close();
|
|
165
|
+
```
|
|
166
|
+
|
|
167
|
+
`tls` uses the same `GrpcTlsOptions` fields as `GrpcClient.setTlsOptions` (see TLS matrix below). `close()` does not affect the singleton default channel.
|
|
168
|
+
|
|
169
|
+
### Interceptors
|
|
170
|
+
|
|
171
|
+
`createChannel` accepts the same interceptor hooks as `GrpcClient.setInterceptors` (replace semantics on the singleton). Outbound hooks run index 0→n; inbound hooks run n→0 (onion). Throw from `onStart` to abort before the native call. Per-RPC extras: `options.interceptors` (appended after channel interceptors).
|
|
172
|
+
|
|
173
|
+
```ts
|
|
174
|
+
import { createChannel, GrpcClient, GrpcInterceptor } from '@xdcobra/react-native-grpc';
|
|
175
|
+
|
|
176
|
+
const auth: GrpcInterceptor = {
|
|
177
|
+
onStart(start) {
|
|
178
|
+
return {
|
|
179
|
+
...start,
|
|
180
|
+
headers: {
|
|
181
|
+
...start.headers,
|
|
182
|
+
authorization: 'Bearer <token>',
|
|
183
|
+
},
|
|
184
|
+
};
|
|
185
|
+
},
|
|
186
|
+
};
|
|
187
|
+
|
|
188
|
+
// Multi-host / preferred
|
|
189
|
+
const channel = createChannel({
|
|
190
|
+
host: 'api.example.com:443',
|
|
191
|
+
interceptors: [auth],
|
|
192
|
+
});
|
|
193
|
+
|
|
194
|
+
// Singleton
|
|
195
|
+
GrpcClient.setInterceptors([auth]);
|
|
196
|
+
|
|
197
|
+
await channel.unaryCall('/package.Service/Method', requestBytes, {}, {
|
|
198
|
+
interceptors: [
|
|
199
|
+
{
|
|
200
|
+
onError(error) {
|
|
201
|
+
// remap or log
|
|
202
|
+
return error;
|
|
203
|
+
},
|
|
204
|
+
},
|
|
205
|
+
],
|
|
206
|
+
});
|
|
207
|
+
```
|
|
208
|
+
|
|
209
|
+
Hooks: `onStart`, `onSendMessage`, `onHeaders`, `onMessage`, `onTrailers`, `onError`.
|
|
210
|
+
|
|
211
|
+
### Retry / hedging (unary only)
|
|
212
|
+
|
|
213
|
+
[gRFC A6](https://github.com/grpc/proposal/blob/master/A6-client-retries.md)-inspired client policies for **unary** RPCs. Retry and hedging are mutually exclusive. Streams ignore these policies. Without a policy there is no automatic retry.
|
|
214
|
+
|
|
215
|
+
```ts
|
|
216
|
+
import {
|
|
217
|
+
createChannel,
|
|
218
|
+
GrpcClient,
|
|
219
|
+
GrpcStatusCode,
|
|
220
|
+
} from '@xdcobra/react-native-grpc';
|
|
221
|
+
|
|
222
|
+
const retry = {
|
|
223
|
+
maxAttempts: 4, // clamped to max 5
|
|
224
|
+
initialBackoff: '0.1s',
|
|
225
|
+
maxBackoff: '1s',
|
|
226
|
+
backoffMultiplier: 2,
|
|
227
|
+
retryableStatusCodes: ['UNAVAILABLE', GrpcStatusCode.RESOURCE_EXHAUSTED],
|
|
228
|
+
};
|
|
229
|
+
|
|
230
|
+
// Preferred multi-host path
|
|
231
|
+
const channel = createChannel({
|
|
232
|
+
host: 'api.example.com:443',
|
|
233
|
+
retry,
|
|
234
|
+
});
|
|
235
|
+
|
|
236
|
+
// Singleton
|
|
237
|
+
GrpcClient.setRetryPolicy(retry);
|
|
238
|
+
// GrpcClient.setHedgingPolicy({ maxAttempts: 3, hedgingDelay: '0.5s', nonFatalStatusCodes: ['UNAVAILABLE'] });
|
|
239
|
+
|
|
240
|
+
await channel.unaryCall('/package.Service/Method', requestBytes, {}, {
|
|
241
|
+
retry: false, // disable for this call
|
|
242
|
+
});
|
|
243
|
+
```
|
|
244
|
+
|
|
245
|
+
Hedging sends parallel attempts (first success wins; siblings are cancelled). Call deadline applies across the whole retry/hedge chain.
|
|
246
|
+
|
|
247
|
+
Unary, server streaming, client streaming, and bidirectional streaming. Protobuf encode/decode is BYO (e.g. `@bufbuild/protobuf`) or via `createClient`.
|
|
248
|
+
|
|
249
|
+
### TLS matrix
|
|
250
|
+
|
|
251
|
+
| Mode | How |
|
|
252
|
+
|------|-----|
|
|
253
|
+
| Plaintext | `setInsecure(true)` |
|
|
254
|
+
| Public CA (e.g. Let's Encrypt) | `setInsecure(false)`, no `setTlsOptions` |
|
|
255
|
+
| Custom / private CA | `rootCertsPem` |
|
|
256
|
+
| mTLS | `certificateChainPem` + `privateKeyPem` (both required) |
|
|
257
|
+
| Dial IP, cert for DNS name | `hostNameOverride` |
|
|
258
|
+
| SPKI pin (SHA-256) | `spkiSha256Pins: ['base64…']` (optional `sha256/` prefix) |
|
|
259
|
+
|
|
260
|
+
PEM values are strings (load from app assets/bundle yourself). On the singleton path, call `setTlsOptions` before `initGrpcChannel()`. With `createChannel`, pass the same fields under `tls` in the config.
|
|
261
|
+
|
|
262
|
+
**SPKI pins:** at least one cert in the server chain must match. On **Android**, pins work with the system trust store and/or `rootCertsPem`. On **iOS** (gRPC ObjC has no handshake SPKI hook), set `rootCertsPem` to the pinned leaf/intermediate PEM as well. Pins are checked against that PEM at config time, then used as trust roots.
|
|
263
|
+
|
|
264
|
+
Generate a pin (OpenSSL):
|
|
265
|
+
|
|
266
|
+
```sh
|
|
267
|
+
openssl x509 -in leaf.pem -pubkey -noout \
|
|
268
|
+
| openssl pkey -pubin -outform DER \
|
|
269
|
+
| openssl dgst -sha256 -binary \
|
|
270
|
+
| openssl enc -base64
|
|
271
|
+
```
|
|
272
|
+
|
|
273
|
+
## TODOs
|
|
274
|
+
|
|
275
|
+
Gaps vs a full gRPC client (not yet supported or not exposed):
|
|
276
|
+
|
|
277
|
+
- **Retry / hedging — remaining**: DNS/service-config JSON; retry throttling ([gRFC A6](https://github.com/grpc/proposal/blob/master/A6-client-retries.md)); stream retry / transparent retry after first response message; native `enableRetry` / C-core service config (JS unary policies are implemented)
|
|
278
|
+
- **Cross-platform connection events**: Android-only (`onConnectionStateChange`, `enterIdle`, …)
|
|
279
|
+
- **Codegen — advanced**: no custom `protoc` plugin; Buf + `createClient` covers the typed path (raw bytes remain available)
|
|
280
|
+
|
|
281
|
+
|
|
282
|
+
## License
|
|
283
|
+
|
|
284
|
+
MIT (same as upstream).
|