@xdcobra/react-native-grpc 1.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.
Files changed (72) hide show
  1. package/LICENSE.md +22 -0
  2. package/README.md +114 -0
  3. package/android/build.gradle +127 -0
  4. package/android/gradle.properties +4 -0
  5. package/android/src/main/AndroidManifest.xml +3 -0
  6. package/android/src/main/java/com/reactnativegrpc/GrpcMarshaller.java +27 -0
  7. package/android/src/main/java/com/reactnativegrpc/GrpcModule.java +585 -0
  8. package/android/src/main/java/com/reactnativegrpc/GrpcPackage.java +27 -0
  9. package/android/src/main/java/com/reactnativegrpc/SpkiPinTrustManager.java +101 -0
  10. package/android/src/main/java/com/reactnativegrpc/TrustManagers.java +51 -0
  11. package/ios/Grpc.h +22 -0
  12. package/ios/Grpc.m +439 -0
  13. package/ios/SpkiPinning.h +22 -0
  14. package/ios/SpkiPinning.m +114 -0
  15. package/lib/commonjs/bidi-streaming.js +37 -0
  16. package/lib/commonjs/bidi-streaming.js.map +1 -0
  17. package/lib/commonjs/client-streaming.js +59 -0
  18. package/lib/commonjs/client-streaming.js.map +1 -0
  19. package/lib/commonjs/client.js +274 -0
  20. package/lib/commonjs/client.js.map +1 -0
  21. package/lib/commonjs/errors.js +16 -0
  22. package/lib/commonjs/errors.js.map +1 -0
  23. package/lib/commonjs/index.js +84 -0
  24. package/lib/commonjs/index.js.map +1 -0
  25. package/lib/commonjs/server-streaming.js +61 -0
  26. package/lib/commonjs/server-streaming.js.map +1 -0
  27. package/lib/commonjs/types.js +2 -0
  28. package/lib/commonjs/types.js.map +1 -0
  29. package/lib/commonjs/unary.js +38 -0
  30. package/lib/commonjs/unary.js.map +1 -0
  31. package/lib/module/bidi-streaming.js +30 -0
  32. package/lib/module/bidi-streaming.js.map +1 -0
  33. package/lib/module/client-streaming.js +51 -0
  34. package/lib/module/client-streaming.js.map +1 -0
  35. package/lib/module/client.js +268 -0
  36. package/lib/module/client.js.map +1 -0
  37. package/lib/module/errors.js +9 -0
  38. package/lib/module/errors.js.map +1 -0
  39. package/lib/module/index.js +10 -0
  40. package/lib/module/index.js.map +1 -0
  41. package/lib/module/server-streaming.js +53 -0
  42. package/lib/module/server-streaming.js.map +1 -0
  43. package/lib/module/types.js +2 -0
  44. package/lib/module/types.js.map +1 -0
  45. package/lib/module/unary.js +31 -0
  46. package/lib/module/unary.js.map +1 -0
  47. package/lib/typescript/bidi-streaming.d.ts +16 -0
  48. package/lib/typescript/bidi-streaming.d.ts.map +1 -0
  49. package/lib/typescript/client-streaming.d.ts +29 -0
  50. package/lib/typescript/client-streaming.d.ts.map +1 -0
  51. package/lib/typescript/client.d.ts +80 -0
  52. package/lib/typescript/client.d.ts.map +1 -0
  53. package/lib/typescript/errors.d.ts +8 -0
  54. package/lib/typescript/errors.d.ts.map +1 -0
  55. package/lib/typescript/index.d.ts +10 -0
  56. package/lib/typescript/index.d.ts.map +1 -0
  57. package/lib/typescript/server-streaming.d.ts +23 -0
  58. package/lib/typescript/server-streaming.d.ts.map +1 -0
  59. package/lib/typescript/types.d.ts +87 -0
  60. package/lib/typescript/types.d.ts.map +1 -0
  61. package/lib/typescript/unary.d.ts +16 -0
  62. package/lib/typescript/unary.d.ts.map +1 -0
  63. package/package.json +169 -0
  64. package/react-native-grpc.podspec +30 -0
  65. package/src/bidi-streaming.ts +78 -0
  66. package/src/client-streaming.ts +106 -0
  67. package/src/client.ts +521 -0
  68. package/src/errors.ts +11 -0
  69. package/src/index.ts +11 -0
  70. package/src/server-streaming.ts +105 -0
  71. package/src/types.ts +107 -0
  72. package/src/unary.ts +71 -0
package/LICENSE.md ADDED
@@ -0,0 +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.
package/README.md ADDED
@@ -0,0 +1,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
+
16
+ ## Installation
17
+
18
+ ```sh
19
+ npm install @xdcobra/react-native-grpc
20
+ # or
21
+ npm install git+https://github.com/XDcobra/react-native-grpc.git
22
+ ```
23
+
24
+ ## Usage
25
+
26
+ ```ts
27
+ import { GrpcClient, GrpcMetadata } from '@xdcobra/react-native-grpc';
28
+
29
+ GrpcClient.setHost('api.example.com:443');
30
+ GrpcClient.setInsecure(false); // TLS (system / gRPC default roots)
31
+ GrpcClient.setCallDeadlineSeconds(120); // global default
32
+ GrpcClient.initGrpcChannel();
33
+
34
+ // Plaintext (dev only)
35
+ // GrpcClient.setInsecure(true);
36
+
37
+ // Custom CA and/or mTLS and/or dial-by-IP hostname override
38
+ // (each setTlsOptions call replaces the previous TLS config)
39
+ GrpcClient.setTlsOptions({
40
+ rootCertsPem: caPem, // optional (required on iOS when using spkiSha256Pins)
41
+ certificateChainPem: clientCertPem, // mTLS (with privateKeyPem)
42
+ privateKeyPem: clientKeyPem,
43
+ hostNameOverride: 'api.example.com', // optional
44
+ spkiSha256Pins: ['sha256/AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA='],
45
+ });
46
+ GrpcClient.initGrpcChannel();
47
+
48
+ const { response } = await GrpcClient.unaryCall(
49
+ '/package.Service/Method',
50
+ requestBytes,
51
+ headers,
52
+ { deadlineSeconds: 60 } // optional per-RPC override
53
+ );
54
+
55
+ // Client streaming: many request chunks, one response
56
+ const stream = GrpcClient.clientStreamCall('/package.Service/Upload', headers);
57
+ await stream.requests.send(chunk1);
58
+ await stream.requests.send(chunk2);
59
+ await stream.requests.complete();
60
+ const { response: uploadResponse } = await stream;
61
+
62
+ // Bidirectional streaming: interleaved requests and responses
63
+ const bidi = GrpcClient.bidiStreamCall('/package.Service/Chat', headers, {
64
+ deadlineSeconds: 60,
65
+ });
66
+ bidi.responses.on('data', (chunk) => {
67
+ /* handle server message */
68
+ });
69
+ await bidi.requests.send(chunk1);
70
+ await bidi.requests.send(chunk2);
71
+ await bidi.requests.complete(); // half-close outbound
72
+ const done = await bidi; // resolves on trailers
73
+ ```
74
+
75
+ Unary, server streaming, client streaming, and bidirectional streaming. Protobuf encode/decode is BYO (e.g. `@bufbuild/protobuf`).
76
+
77
+ ### TLS matrix
78
+
79
+ | Mode | How |
80
+ |------|-----|
81
+ | Plaintext | `setInsecure(true)` |
82
+ | Public CA (e.g. Let's Encrypt) | `setInsecure(false)`, no `setTlsOptions` |
83
+ | Custom / private CA | `rootCertsPem` |
84
+ | mTLS | `certificateChainPem` + `privateKeyPem` (both required) |
85
+ | Dial IP, cert for DNS name | `hostNameOverride` |
86
+ | SPKI pin (SHA-256) | `spkiSha256Pins: ['base64…']` (optional `sha256/` prefix) |
87
+
88
+ PEM values are strings (load from app assets/bundle yourself). Call `setTlsOptions` before `initGrpcChannel()`.
89
+
90
+ **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.
91
+
92
+ Generate a pin (OpenSSL):
93
+
94
+ ```sh
95
+ openssl x509 -in leaf.pem -pubkey -noout \
96
+ | openssl pkey -pubin -outform DER \
97
+ | openssl dgst -sha256 -binary \
98
+ | openssl enc -base64
99
+ ```
100
+
101
+ ## TODOs
102
+
103
+ Gaps vs a full gRPC client (not yet supported or not exposed):
104
+
105
+ - **Multiple channels / hosts**: single global host; no concurrent channels
106
+ - **Interceptors**: no request/response middleware
107
+ - **Retry / hedging**: no client-side retry policy
108
+ - **Cross-platform connection events**: Android-only (`onConnectionStateChange`, `enterIdle`, …)
109
+ - **Codegen / stubs**: raw method paths + bytes; no generated service clients
110
+
111
+
112
+ ## License
113
+
114
+ MIT (same as upstream).
@@ -0,0 +1,127 @@
1
+ buildscript {
2
+ // Buildscript is evaluated before everything else so we can't use getExtOrDefault
3
+
4
+ repositories {
5
+ google()
6
+ mavenCentral()
7
+ mavenLocal()
8
+ }
9
+
10
+ dependencies {
11
+ classpath 'com.android.tools.build:gradle:4.2.1'
12
+ }
13
+ }
14
+
15
+ apply plugin: 'com.android.library'
16
+
17
+ def getExtOrDefault(name) {
18
+ return rootProject.ext.has(name) ? rootProject.ext.get(name) : project.properties['Grpc_' + name]
19
+ }
20
+
21
+ def getExtOrIntegerDefault(name) {
22
+ return rootProject.ext.has(name) ? rootProject.ext.get(name) : (project.properties['Grpc_' + name]).toInteger()
23
+ }
24
+
25
+ android {
26
+ namespace 'com.reactnativegrpc'
27
+ compileSdkVersion getExtOrIntegerDefault('compileSdkVersion')
28
+ buildToolsVersion getExtOrDefault('buildToolsVersion')
29
+ defaultConfig {
30
+ minSdkVersion 24
31
+ targetSdkVersion getExtOrIntegerDefault('targetSdkVersion')
32
+ versionCode 1
33
+ versionName "1.0"
34
+
35
+ }
36
+
37
+ buildTypes {
38
+ release {
39
+ minifyEnabled false
40
+ }
41
+ }
42
+ lintOptions {
43
+ disable 'GradleCompatible'
44
+ }
45
+ compileOptions {
46
+ sourceCompatibility JavaVersion.VERSION_1_8
47
+ targetCompatibility JavaVersion.VERSION_1_8
48
+ }
49
+ }
50
+
51
+ repositories {
52
+ mavenCentral()
53
+ google()
54
+
55
+ def found = false
56
+ def defaultDir = null
57
+ def androidSourcesName = 'React Native sources'
58
+
59
+ if (rootProject.ext.has('reactNativeAndroidRoot')) {
60
+ defaultDir = rootProject.ext.get('reactNativeAndroidRoot')
61
+ } else {
62
+ defaultDir = new File(
63
+ projectDir,
64
+ '/../../../node_modules/react-native/android'
65
+ )
66
+ }
67
+
68
+ if (defaultDir.exists()) {
69
+ maven {
70
+ url defaultDir.toString()
71
+ name androidSourcesName
72
+ }
73
+
74
+ logger.info(":${project.name}:reactNativeAndroidRoot ${defaultDir.canonicalPath}")
75
+ found = true
76
+ } else {
77
+ def parentDir = rootProject.projectDir
78
+
79
+ 1.upto(5, {
80
+ if (found) return true
81
+ parentDir = parentDir.parentFile
82
+
83
+ def androidSourcesDir = new File(
84
+ parentDir,
85
+ 'node_modules/react-native'
86
+ )
87
+
88
+ def androidPrebuiltBinaryDir = new File(
89
+ parentDir,
90
+ 'node_modules/react-native/android'
91
+ )
92
+
93
+ if (androidPrebuiltBinaryDir.exists()) {
94
+ maven {
95
+ url androidPrebuiltBinaryDir.toString()
96
+ name androidSourcesName
97
+ }
98
+
99
+ logger.info(":${project.name}:reactNativeAndroidRoot ${androidPrebuiltBinaryDir.canonicalPath}")
100
+ found = true
101
+ } else if (androidSourcesDir.exists()) {
102
+ maven {
103
+ url androidSourcesDir.toString()
104
+ name androidSourcesName
105
+ }
106
+
107
+ logger.info(":${project.name}:reactNativeAndroidRoot ${androidSourcesDir.canonicalPath}")
108
+ found = true
109
+ }
110
+ })
111
+ }
112
+
113
+ if (!found) {
114
+ throw new GradleException(
115
+ "${project.name}: unable to locate React Native android sources. " +
116
+ "Ensure you have you installed React Native as a dependency in your project and try again."
117
+ )
118
+ }
119
+ }
120
+
121
+ dependencies {
122
+ // noinspection GradleDynamicVersion
123
+ implementation 'io.grpc:grpc-okhttp:1.51.0'// CURRENT_GRPC_VERSION
124
+ implementation 'io.grpc:grpc-stub:1.51.0' // CURRENT_GRPC_VERSION
125
+
126
+ api 'com.facebook.react:react-native:+'
127
+ }
@@ -0,0 +1,4 @@
1
+ Grpc_kotlinVersion=1.3.50
2
+ Grpc_compileSdkVersion=29
3
+ Grpc_buildToolsVersion=29.0.2
4
+ Grpc_targetSdkVersion=29
@@ -0,0 +1,3 @@
1
+ <manifest xmlns:android="http://schemas.android.com/apk/res/android">
2
+
3
+ </manifest>
@@ -0,0 +1,27 @@
1
+ package com.reactnativegrpc;
2
+
3
+ import com.google.common.io.ByteStreams;
4
+
5
+ import java.io.ByteArrayInputStream;
6
+ import java.io.IOException;
7
+ import java.io.InputStream;
8
+
9
+ import io.grpc.MethodDescriptor;
10
+
11
+ public class GrpcMarshaller implements MethodDescriptor.Marshaller<byte[]> {
12
+ @Override
13
+ public InputStream stream(byte[] value) {
14
+ return new ByteArrayInputStream(value);
15
+ }
16
+
17
+ @Override
18
+ public byte[] parse(InputStream stream) {
19
+ try {
20
+ return ByteStreams.toByteArray(stream);
21
+ } catch (IOException e) {
22
+ e.printStackTrace();
23
+
24
+ return null;
25
+ }
26
+ }
27
+ }