@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.
Files changed (96) hide show
  1. package/LICENSE.md +22 -22
  2. package/README.md +284 -114
  3. package/android/build.gradle +127 -127
  4. package/android/gradle.properties +4 -4
  5. package/android/src/main/AndroidManifest.xml +3 -3
  6. package/android/src/main/java/com/reactnativegrpc/GrpcMarshaller.java +27 -27
  7. package/android/src/main/java/com/reactnativegrpc/GrpcModule.java +730 -585
  8. package/android/src/main/java/com/reactnativegrpc/GrpcPackage.java +27 -27
  9. package/android/src/main/java/com/reactnativegrpc/SpkiPinTrustManager.java +101 -101
  10. package/android/src/main/java/com/reactnativegrpc/TrustManagers.java +51 -51
  11. package/ios/Grpc.h +22 -22
  12. package/ios/Grpc.m +625 -439
  13. package/ios/SpkiPinning.h +22 -22
  14. package/ios/SpkiPinning.m +114 -114
  15. package/lib/commonjs/bidi-streaming.js.map +1 -1
  16. package/lib/commonjs/channel.js +84 -0
  17. package/lib/commonjs/channel.js.map +1 -0
  18. package/lib/commonjs/client-streaming.js +3 -3
  19. package/lib/commonjs/client-streaming.js.map +1 -1
  20. package/lib/commonjs/client.js +538 -135
  21. package/lib/commonjs/client.js.map +1 -1
  22. package/lib/commonjs/create-client.js +206 -0
  23. package/lib/commonjs/create-client.js.map +1 -0
  24. package/lib/commonjs/errors.js.map +1 -1
  25. package/lib/commonjs/index.js +32 -1
  26. package/lib/commonjs/index.js.map +1 -1
  27. package/lib/commonjs/interceptors.js +116 -0
  28. package/lib/commonjs/interceptors.js.map +1 -0
  29. package/lib/commonjs/native.js +24 -0
  30. package/lib/commonjs/native.js.map +1 -0
  31. package/lib/commonjs/retry.js +153 -0
  32. package/lib/commonjs/retry.js.map +1 -0
  33. package/lib/commonjs/server-streaming.js.map +1 -1
  34. package/lib/commonjs/status.js +54 -0
  35. package/lib/commonjs/status.js.map +1 -0
  36. package/lib/commonjs/types.js +29 -0
  37. package/lib/commonjs/types.js.map +1 -1
  38. package/lib/commonjs/unary.js.map +1 -1
  39. package/lib/module/bidi-streaming.js.map +1 -1
  40. package/lib/module/channel.js +76 -0
  41. package/lib/module/channel.js.map +1 -0
  42. package/lib/module/client-streaming.js +3 -3
  43. package/lib/module/client-streaming.js.map +1 -1
  44. package/lib/module/client.js +525 -129
  45. package/lib/module/client.js.map +1 -1
  46. package/lib/module/create-client.js +201 -0
  47. package/lib/module/create-client.js.map +1 -0
  48. package/lib/module/errors.js.map +1 -1
  49. package/lib/module/index.js +3 -0
  50. package/lib/module/index.js.map +1 -1
  51. package/lib/module/interceptors.js +104 -0
  52. package/lib/module/interceptors.js.map +1 -0
  53. package/lib/module/native.js +17 -0
  54. package/lib/module/native.js.map +1 -0
  55. package/lib/module/retry.js +141 -0
  56. package/lib/module/retry.js.map +1 -0
  57. package/lib/module/server-streaming.js.map +1 -1
  58. package/lib/module/status.js +46 -0
  59. package/lib/module/status.js.map +1 -0
  60. package/lib/module/types.js +17 -0
  61. package/lib/module/types.js.map +1 -1
  62. package/lib/module/unary.js.map +1 -1
  63. package/lib/typescript/channel.d.ts +27 -0
  64. package/lib/typescript/channel.d.ts.map +1 -0
  65. package/lib/typescript/client.d.ts +56 -49
  66. package/lib/typescript/client.d.ts.map +1 -1
  67. package/lib/typescript/create-client.d.ts +65 -0
  68. package/lib/typescript/create-client.d.ts.map +1 -0
  69. package/lib/typescript/index.d.ts +5 -0
  70. package/lib/typescript/index.d.ts.map +1 -1
  71. package/lib/typescript/interceptors.d.ts +43 -0
  72. package/lib/typescript/interceptors.d.ts.map +1 -0
  73. package/lib/typescript/native.d.ts +53 -0
  74. package/lib/typescript/native.d.ts.map +1 -0
  75. package/lib/typescript/retry.d.ts +56 -0
  76. package/lib/typescript/retry.d.ts.map +1 -0
  77. package/lib/typescript/status.d.ts +24 -0
  78. package/lib/typescript/status.d.ts.map +1 -0
  79. package/lib/typescript/types.d.ts +60 -0
  80. package/lib/typescript/types.d.ts.map +1 -1
  81. package/package.json +209 -169
  82. package/react-native-grpc.podspec +29 -29
  83. package/src/bidi-streaming.ts +78 -78
  84. package/src/channel.ts +110 -0
  85. package/src/client-streaming.ts +106 -106
  86. package/src/client.ts +1046 -521
  87. package/src/create-client.ts +391 -0
  88. package/src/errors.ts +11 -11
  89. package/src/index.ts +28 -11
  90. package/src/interceptors.ts +167 -0
  91. package/src/native.ts +99 -0
  92. package/src/retry.ts +208 -0
  93. package/src/server-streaming.ts +105 -105
  94. package/src/status.ts +50 -0
  95. package/src/types.ts +171 -107
  96. package/src/unary.ts +71 -71
@@ -1,78 +1,78 @@
1
- import { AbortController } from 'abort-controller';
2
- import {
3
- CompletedGrpcBidiStreamingCall,
4
- GrpcMetadata,
5
- GrpcServerInputStream,
6
- GrpcServerOutputStream,
7
- } from './types';
8
-
9
- export class GrpcBidiStreamingCall
10
- implements PromiseLike<CompletedGrpcBidiStreamingCall>
11
- {
12
- readonly method: string;
13
- readonly requestHeaders: GrpcMetadata;
14
- readonly requests: GrpcServerInputStream;
15
- readonly headers: Promise<GrpcMetadata>;
16
- readonly responses: GrpcServerOutputStream;
17
- readonly trailers: Promise<GrpcMetadata>;
18
-
19
- #abort: AbortController;
20
-
21
- constructor(
22
- method: string,
23
- requestHeaders: GrpcMetadata,
24
- requests: GrpcServerInputStream,
25
- headers: Promise<GrpcMetadata>,
26
- responses: GrpcServerOutputStream,
27
- trailers: Promise<GrpcMetadata>,
28
- abort: AbortController
29
- ) {
30
- this.method = method;
31
- this.requestHeaders = requestHeaders;
32
- this.requests = requests;
33
- this.headers = headers;
34
- this.responses = responses;
35
- this.trailers = trailers;
36
- this.#abort = abort;
37
- }
38
-
39
- then<TResult1 = CompletedGrpcBidiStreamingCall, TResult2 = unknown>(
40
- onfulfilled?:
41
- | ((
42
- value: CompletedGrpcBidiStreamingCall
43
- ) => TResult1 | PromiseLike<TResult1>)
44
- | null,
45
- onrejected?: ((reason: any) => TResult2 | PromiseLike<TResult2>) | null
46
- ): PromiseLike<TResult1 | TResult2> {
47
- return this.completedPromise().then(
48
- (value) =>
49
- onfulfilled
50
- ? Promise.resolve(onfulfilled(value))
51
- : (value as unknown as TResult1),
52
- (reason) =>
53
- onrejected
54
- ? Promise.resolve(onrejected(reason))
55
- : Promise.reject(reason)
56
- );
57
- }
58
-
59
- cancel() {
60
- this.#abort.abort();
61
- }
62
-
63
- private async completedPromise(): Promise<CompletedGrpcBidiStreamingCall> {
64
- const [headers, trailers] = await Promise.all([
65
- this.headers,
66
- this.trailers,
67
- ]);
68
-
69
- return {
70
- method: this.method,
71
- requestHeaders: this.requestHeaders,
72
- headers,
73
- responses: this.responses,
74
- trailers,
75
- status: 0,
76
- };
77
- }
78
- }
1
+ import { AbortController } from 'abort-controller';
2
+ import {
3
+ CompletedGrpcBidiStreamingCall,
4
+ GrpcMetadata,
5
+ GrpcServerInputStream,
6
+ GrpcServerOutputStream,
7
+ } from './types';
8
+
9
+ export class GrpcBidiStreamingCall
10
+ implements PromiseLike<CompletedGrpcBidiStreamingCall>
11
+ {
12
+ readonly method: string;
13
+ readonly requestHeaders: GrpcMetadata;
14
+ readonly requests: GrpcServerInputStream;
15
+ readonly headers: Promise<GrpcMetadata>;
16
+ readonly responses: GrpcServerOutputStream;
17
+ readonly trailers: Promise<GrpcMetadata>;
18
+
19
+ #abort: AbortController;
20
+
21
+ constructor(
22
+ method: string,
23
+ requestHeaders: GrpcMetadata,
24
+ requests: GrpcServerInputStream,
25
+ headers: Promise<GrpcMetadata>,
26
+ responses: GrpcServerOutputStream,
27
+ trailers: Promise<GrpcMetadata>,
28
+ abort: AbortController
29
+ ) {
30
+ this.method = method;
31
+ this.requestHeaders = requestHeaders;
32
+ this.requests = requests;
33
+ this.headers = headers;
34
+ this.responses = responses;
35
+ this.trailers = trailers;
36
+ this.#abort = abort;
37
+ }
38
+
39
+ then<TResult1 = CompletedGrpcBidiStreamingCall, TResult2 = unknown>(
40
+ onfulfilled?:
41
+ | ((
42
+ value: CompletedGrpcBidiStreamingCall
43
+ ) => TResult1 | PromiseLike<TResult1>)
44
+ | null,
45
+ onrejected?: ((reason: any) => TResult2 | PromiseLike<TResult2>) | null
46
+ ): PromiseLike<TResult1 | TResult2> {
47
+ return this.completedPromise().then(
48
+ (value) =>
49
+ onfulfilled
50
+ ? Promise.resolve(onfulfilled(value))
51
+ : (value as unknown as TResult1),
52
+ (reason) =>
53
+ onrejected
54
+ ? Promise.resolve(onrejected(reason))
55
+ : Promise.reject(reason)
56
+ );
57
+ }
58
+
59
+ cancel() {
60
+ this.#abort.abort();
61
+ }
62
+
63
+ private async completedPromise(): Promise<CompletedGrpcBidiStreamingCall> {
64
+ const [headers, trailers] = await Promise.all([
65
+ this.headers,
66
+ this.trailers,
67
+ ]);
68
+
69
+ return {
70
+ method: this.method,
71
+ requestHeaders: this.requestHeaders,
72
+ headers,
73
+ responses: this.responses,
74
+ trailers,
75
+ status: 0,
76
+ };
77
+ }
78
+ }
package/src/channel.ts ADDED
@@ -0,0 +1,110 @@
1
+ import { GrpcCallApi, normalizeTlsOptions } from './client';
2
+ import type { GrpcInterceptor } from './interceptors';
3
+ import { nativeGrpc } from './native';
4
+ import {
5
+ assertExclusivePolicies,
6
+ normalizeHedgingPolicy,
7
+ normalizeRetryPolicy,
8
+ } from './retry';
9
+ import { GrpcChannelConfig } from './types';
10
+
11
+ let channelCtr = 1;
12
+
13
+ /**
14
+ * Independent gRPC channel bound to a host (and optional TLS / deadline / keepalive).
15
+ * Create via {@link createChannel}; close when finished. Concurrent channels are supported.
16
+ */
17
+ export class GrpcChannel extends GrpcCallApi {
18
+ private closed = false;
19
+
20
+ constructor(
21
+ channelId: string,
22
+ private readonly host: string,
23
+ interceptors: readonly GrpcInterceptor[] = [],
24
+ channelDeadlineSeconds = 120,
25
+ channelRetry: import('./retry').GrpcRetryPolicy | null = null,
26
+ channelHedging: import('./retry').GrpcHedgingPolicy | null = null
27
+ ) {
28
+ super(
29
+ channelId,
30
+ interceptors,
31
+ channelDeadlineSeconds,
32
+ channelRetry,
33
+ channelHedging
34
+ );
35
+ }
36
+
37
+ /** Host this channel was created with. */
38
+ getHost(): Promise<string> {
39
+ return Promise.resolve(this.host);
40
+ }
41
+
42
+ /** Native channel id (for diagnostics). */
43
+ getChannelId(): string {
44
+ return this.channelId;
45
+ }
46
+
47
+ /**
48
+ * Tear down this channel. Further RPCs will fail.
49
+ * Does not affect the singleton `GrpcClient` default channel.
50
+ */
51
+ close(): void {
52
+ if (this.closed) {
53
+ return;
54
+ }
55
+ this.closed = true;
56
+ nativeGrpc().closeChannel(this.channelId);
57
+ }
58
+ }
59
+
60
+ /**
61
+ * Create a concurrent channel with immutable config.
62
+ * Prefer this over mutating `GrpcClient.setHost` when talking to multiple hosts.
63
+ */
64
+ export function createChannel(config: GrpcChannelConfig): GrpcChannel {
65
+ if (!config?.host || typeof config.host !== 'string' || !config.host.trim()) {
66
+ throw new Error('createChannel requires a non-empty host');
67
+ }
68
+
69
+ assertExclusivePolicies(config.retry, config.hedging);
70
+ if (config.retry) {
71
+ normalizeRetryPolicy(config.retry);
72
+ }
73
+ if (config.hedging) {
74
+ normalizeHedgingPolicy(config.hedging);
75
+ }
76
+ const retry = config.retry ?? null;
77
+ const hedging = config.hedging ?? null;
78
+
79
+ const tls = config.tls ? normalizeTlsOptions(config.tls) : null;
80
+ const channelId = `ch_${channelCtr++}`;
81
+ const compression = config.compression;
82
+ const keepAlive = config.keepAlive;
83
+ const deadline =
84
+ config.callDeadlineSeconds !== undefined
85
+ ? Math.max(0, config.callDeadlineSeconds)
86
+ : 120;
87
+
88
+ nativeGrpc().createChannel(channelId, {
89
+ host: config.host.trim(),
90
+ insecure: !!config.insecure,
91
+ tls,
92
+ callDeadlineSeconds: deadline,
93
+ responseSizeLimit:
94
+ config.responseSizeLimit !== undefined ? config.responseSizeLimit : null,
95
+ compressionEnable: !!compression?.enable,
96
+ compressorName: compression?.compressorName ?? '',
97
+ keepAliveEnable: !!keepAlive?.enable,
98
+ keepAliveTime: keepAlive?.keepAliveTime ?? 0,
99
+ keepAliveTimeOut: keepAlive?.keepAliveTimeOut ?? 0,
100
+ });
101
+
102
+ return new GrpcChannel(
103
+ channelId,
104
+ config.host.trim(),
105
+ config.interceptors ?? [],
106
+ deadline,
107
+ retry,
108
+ hedging
109
+ );
110
+ }
@@ -1,106 +1,106 @@
1
- import { AbortController } from 'abort-controller';
2
- import {
3
- CompletedGrpcClientStreamingCall,
4
- GrpcMetadata,
5
- GrpcServerInputStream,
6
- } from './types';
7
-
8
- type SendNative = (data: Uint8Array, isFirst: boolean) => Promise<void>;
9
- type FinishNative = () => Promise<void>;
10
-
11
- /**
12
- * Client → server request stream for client-streaming RPCs.
13
- * Native call is created on the first `send`.
14
- */
15
- export class ServerInputStream implements GrpcServerInputStream {
16
- #started = false;
17
- #sendNative: SendNative;
18
- #finishNative: FinishNative;
19
-
20
- constructor(sendNative: SendNative, finishNative: FinishNative) {
21
- this.#sendNative = sendNative;
22
- this.#finishNative = finishNative;
23
- }
24
-
25
- send(data: Uint8Array): Promise<void> {
26
- const isFirst = !this.#started;
27
- this.#started = true;
28
- return this.#sendNative(data, isFirst);
29
- }
30
-
31
- complete(): Promise<void> {
32
- return this.#finishNative();
33
- }
34
- }
35
-
36
- export class GrpcClientStreamingCall
37
- implements PromiseLike<CompletedGrpcClientStreamingCall>
38
- {
39
- readonly method: string;
40
- readonly requestHeaders: GrpcMetadata;
41
- readonly requests: GrpcServerInputStream;
42
- readonly headers: Promise<GrpcMetadata>;
43
- readonly response: Promise<Uint8Array>;
44
- readonly trailers: Promise<GrpcMetadata>;
45
-
46
- #abort: AbortController;
47
-
48
- constructor(
49
- method: string,
50
- requestHeaders: GrpcMetadata,
51
- requests: GrpcServerInputStream,
52
- headers: Promise<GrpcMetadata>,
53
- response: Promise<Uint8Array>,
54
- trailers: Promise<GrpcMetadata>,
55
- abort: AbortController
56
- ) {
57
- this.method = method;
58
- this.requestHeaders = requestHeaders;
59
- this.requests = requests;
60
- this.headers = headers;
61
- this.response = response;
62
- this.trailers = trailers;
63
- this.#abort = abort;
64
- }
65
-
66
- then<TResult1 = CompletedGrpcClientStreamingCall, TResult2 = unknown>(
67
- onfulfilled?:
68
- | ((
69
- value: CompletedGrpcClientStreamingCall
70
- ) => TResult1 | PromiseLike<TResult1>)
71
- | null,
72
- onrejected?: ((reason: any) => TResult2 | PromiseLike<TResult2>) | null
73
- ): PromiseLike<TResult1 | TResult2> {
74
- return this.completedPromise().then(
75
- (value) =>
76
- onfulfilled
77
- ? Promise.resolve(onfulfilled(value))
78
- : (value as unknown as TResult1),
79
- (reason) =>
80
- onrejected
81
- ? Promise.resolve(onrejected(reason))
82
- : Promise.reject(reason)
83
- );
84
- }
85
-
86
- cancel() {
87
- this.#abort.abort();
88
- }
89
-
90
- private async completedPromise(): Promise<CompletedGrpcClientStreamingCall> {
91
- const [headers, response, trailers] = await Promise.all([
92
- this.headers,
93
- this.response,
94
- this.trailers,
95
- ]);
96
-
97
- return {
98
- method: this.method,
99
- requestHeaders: this.requestHeaders,
100
- headers,
101
- trailers,
102
- response,
103
- status: 0,
104
- };
105
- }
106
- }
1
+ import { AbortController } from 'abort-controller';
2
+ import {
3
+ CompletedGrpcClientStreamingCall,
4
+ GrpcMetadata,
5
+ GrpcServerInputStream,
6
+ } from './types';
7
+
8
+ type SendNative = (data: Uint8Array, isFirst: boolean) => Promise<void>;
9
+ type FinishNative = () => Promise<void>;
10
+
11
+ /**
12
+ * Client → server request stream for client-streaming RPCs.
13
+ * Native call is created on the first `send`.
14
+ */
15
+ export class ServerInputStream implements GrpcServerInputStream {
16
+ #started = false;
17
+ #sendNative: SendNative;
18
+ #finishNative: FinishNative;
19
+
20
+ constructor(sendNative: SendNative, finishNative: FinishNative) {
21
+ this.#sendNative = sendNative;
22
+ this.#finishNative = finishNative;
23
+ }
24
+
25
+ send(data: Uint8Array): Promise<void> {
26
+ const isFirst = !this.#started;
27
+ this.#started = true;
28
+ return this.#sendNative(data, isFirst);
29
+ }
30
+
31
+ complete(): Promise<void> {
32
+ return this.#finishNative();
33
+ }
34
+ }
35
+
36
+ export class GrpcClientStreamingCall
37
+ implements PromiseLike<CompletedGrpcClientStreamingCall>
38
+ {
39
+ readonly method: string;
40
+ readonly requestHeaders: GrpcMetadata;
41
+ readonly requests: GrpcServerInputStream;
42
+ readonly headers: Promise<GrpcMetadata>;
43
+ readonly response: Promise<Uint8Array>;
44
+ readonly trailers: Promise<GrpcMetadata>;
45
+
46
+ #abort: AbortController;
47
+
48
+ constructor(
49
+ method: string,
50
+ requestHeaders: GrpcMetadata,
51
+ requests: GrpcServerInputStream,
52
+ headers: Promise<GrpcMetadata>,
53
+ response: Promise<Uint8Array>,
54
+ trailers: Promise<GrpcMetadata>,
55
+ abort: AbortController
56
+ ) {
57
+ this.method = method;
58
+ this.requestHeaders = requestHeaders;
59
+ this.requests = requests;
60
+ this.headers = headers;
61
+ this.response = response;
62
+ this.trailers = trailers;
63
+ this.#abort = abort;
64
+ }
65
+
66
+ then<TResult1 = CompletedGrpcClientStreamingCall, TResult2 = unknown>(
67
+ onfulfilled?:
68
+ | ((
69
+ value: CompletedGrpcClientStreamingCall
70
+ ) => TResult1 | PromiseLike<TResult1>)
71
+ | null,
72
+ onrejected?: ((reason: any) => TResult2 | PromiseLike<TResult2>) | null
73
+ ): PromiseLike<TResult1 | TResult2> {
74
+ return this.completedPromise().then(
75
+ (value) =>
76
+ onfulfilled
77
+ ? Promise.resolve(onfulfilled(value))
78
+ : (value as unknown as TResult1),
79
+ (reason) =>
80
+ onrejected
81
+ ? Promise.resolve(onrejected(reason))
82
+ : Promise.reject(reason)
83
+ );
84
+ }
85
+
86
+ cancel() {
87
+ this.#abort.abort();
88
+ }
89
+
90
+ private async completedPromise(): Promise<CompletedGrpcClientStreamingCall> {
91
+ const [headers, response, trailers] = await Promise.all([
92
+ this.headers,
93
+ this.response,
94
+ this.trailers,
95
+ ]);
96
+
97
+ return {
98
+ method: this.method,
99
+ requestHeaders: this.requestHeaders,
100
+ headers,
101
+ trailers,
102
+ response,
103
+ status: 0,
104
+ };
105
+ }
106
+ }