@xdcobra/react-native-grpc 1.0.0 → 1.1.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/README.md +176 -6
- package/android/src/main/java/com/reactnativegrpc/GrpcModule.java +233 -88
- package/ios/Grpc.h +1 -1
- package/ios/Grpc.m +210 -24
- package/lib/commonjs/channel.js +84 -0
- package/lib/commonjs/channel.js.map +1 -0
- package/lib/commonjs/client.js +531 -128
- 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/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/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/module/channel.js +76 -0
- package/lib/module/channel.js.map +1 -0
- package/lib/module/client.js +518 -122
- 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/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/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/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 +48 -7
- package/src/channel.ts +110 -0
- package/src/client.ts +725 -200
- package/src/create-client.ts +391 -0
- package/src/index.ts +17 -0
- package/src/interceptors.ts +167 -0
- package/src/native.ts +99 -0
- package/src/retry.ts +208 -0
- package/src/status.ts +50 -0
- package/src/types.ts +64 -0
package/src/retry.ts
ADDED
|
@@ -0,0 +1,208 @@
|
|
|
1
|
+
import { parseStatusCodeList } from './status';
|
|
2
|
+
|
|
3
|
+
/** A6-style retry policy (unary only in this client). */
|
|
4
|
+
export type GrpcRetryPolicy = {
|
|
5
|
+
/** Total attempts including the first. Required; clamped to [2, 5]. */
|
|
6
|
+
maxAttempts: number;
|
|
7
|
+
/** Initial backoff, e.g. `"0.1s"` (proto3 Duration JSON). */
|
|
8
|
+
initialBackoff: string;
|
|
9
|
+
maxBackoff: string;
|
|
10
|
+
backoffMultiplier: number;
|
|
11
|
+
/** Status codes that trigger another attempt (names or numbers). */
|
|
12
|
+
retryableStatusCodes: Array<string | number>;
|
|
13
|
+
};
|
|
14
|
+
|
|
15
|
+
/** A6-style hedging policy (unary only in this client). */
|
|
16
|
+
export type GrpcHedgingPolicy = {
|
|
17
|
+
/** Total parallel attempts including the first. Required; clamped to [2, 5]. */
|
|
18
|
+
maxAttempts: number;
|
|
19
|
+
/** Delay before next hedge, e.g. `"0.5s"`; omit / `"0s"` = send promptly. */
|
|
20
|
+
hedgingDelay?: string;
|
|
21
|
+
/** Non-fatal codes: continue hedging; other errors cancel siblings and fail. */
|
|
22
|
+
nonFatalStatusCodes?: Array<string | number>;
|
|
23
|
+
};
|
|
24
|
+
|
|
25
|
+
export type NormalizedRetryPolicy = {
|
|
26
|
+
maxAttempts: number;
|
|
27
|
+
initialBackoffMs: number;
|
|
28
|
+
maxBackoffMs: number;
|
|
29
|
+
backoffMultiplier: number;
|
|
30
|
+
retryableStatusCodes: Set<number>;
|
|
31
|
+
};
|
|
32
|
+
|
|
33
|
+
export type NormalizedHedgingPolicy = {
|
|
34
|
+
maxAttempts: number;
|
|
35
|
+
hedgingDelayMs: number;
|
|
36
|
+
nonFatalStatusCodes: Set<number>;
|
|
37
|
+
};
|
|
38
|
+
|
|
39
|
+
export type EffectiveCallPolicy =
|
|
40
|
+
| { kind: 'none' }
|
|
41
|
+
| { kind: 'retry'; policy: NormalizedRetryPolicy }
|
|
42
|
+
| { kind: 'hedging'; policy: NormalizedHedgingPolicy };
|
|
43
|
+
|
|
44
|
+
const MAX_ATTEMPTS_CAP = 5;
|
|
45
|
+
|
|
46
|
+
/** Parse proto3 JSON Duration (`"0.1s"`, `"100ms"`, `"1s"`) to milliseconds. */
|
|
47
|
+
export function parseDurationMs(value: string): number {
|
|
48
|
+
const raw = String(value).trim();
|
|
49
|
+
const match = /^(-?\d+(?:\.\d+)?)(ms|s)$/i.exec(raw);
|
|
50
|
+
if (!match) {
|
|
51
|
+
throw new Error(
|
|
52
|
+
`Invalid duration "${value}" (expected proto3 JSON like "0.1s" or "100ms")`
|
|
53
|
+
);
|
|
54
|
+
}
|
|
55
|
+
const amount = Number(match[1]);
|
|
56
|
+
if (!Number.isFinite(amount) || amount < 0) {
|
|
57
|
+
throw new Error(`Invalid duration "${value}"`);
|
|
58
|
+
}
|
|
59
|
+
const unit = match[2].toLowerCase();
|
|
60
|
+
return unit === 'ms' ? amount : amount * 1000;
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
function clampAttempts(maxAttempts: number): number {
|
|
64
|
+
if (!Number.isFinite(maxAttempts) || maxAttempts < 2) {
|
|
65
|
+
throw new Error('maxAttempts must be >= 2');
|
|
66
|
+
}
|
|
67
|
+
return Math.min(MAX_ATTEMPTS_CAP, Math.floor(maxAttempts));
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
export function normalizeRetryPolicy(
|
|
71
|
+
policy: GrpcRetryPolicy
|
|
72
|
+
): NormalizedRetryPolicy {
|
|
73
|
+
if (!policy.retryableStatusCodes?.length) {
|
|
74
|
+
throw new Error('retryableStatusCodes must be non-empty');
|
|
75
|
+
}
|
|
76
|
+
if (
|
|
77
|
+
!Number.isFinite(policy.backoffMultiplier) ||
|
|
78
|
+
policy.backoffMultiplier <= 0
|
|
79
|
+
) {
|
|
80
|
+
throw new Error('backoffMultiplier must be > 0');
|
|
81
|
+
}
|
|
82
|
+
return {
|
|
83
|
+
maxAttempts: clampAttempts(policy.maxAttempts),
|
|
84
|
+
initialBackoffMs: parseDurationMs(policy.initialBackoff),
|
|
85
|
+
maxBackoffMs: parseDurationMs(policy.maxBackoff),
|
|
86
|
+
backoffMultiplier: policy.backoffMultiplier,
|
|
87
|
+
retryableStatusCodes: parseStatusCodeList(policy.retryableStatusCodes),
|
|
88
|
+
};
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
export function normalizeHedgingPolicy(
|
|
92
|
+
policy: GrpcHedgingPolicy
|
|
93
|
+
): NormalizedHedgingPolicy {
|
|
94
|
+
return {
|
|
95
|
+
maxAttempts: clampAttempts(policy.maxAttempts),
|
|
96
|
+
hedgingDelayMs: policy.hedgingDelay
|
|
97
|
+
? parseDurationMs(policy.hedgingDelay)
|
|
98
|
+
: 0,
|
|
99
|
+
nonFatalStatusCodes: policy.nonFatalStatusCodes?.length
|
|
100
|
+
? parseStatusCodeList(policy.nonFatalStatusCodes)
|
|
101
|
+
: new Set(),
|
|
102
|
+
};
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
export function assertExclusivePolicies(
|
|
106
|
+
retry?: GrpcRetryPolicy | false | null,
|
|
107
|
+
hedging?: GrpcHedgingPolicy | false | null
|
|
108
|
+
): void {
|
|
109
|
+
const hasRetry = retry != null && retry !== false;
|
|
110
|
+
const hasHedging = hedging != null && hedging !== false;
|
|
111
|
+
if (hasRetry && hasHedging) {
|
|
112
|
+
throw new Error(
|
|
113
|
+
'retry and hedging policies are mutually exclusive (gRPC A6)'
|
|
114
|
+
);
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
export function resolveEffectivePolicy(
|
|
119
|
+
channelRetry: GrpcRetryPolicy | null,
|
|
120
|
+
channelHedging: GrpcHedgingPolicy | null,
|
|
121
|
+
callRetry?: GrpcRetryPolicy | false,
|
|
122
|
+
callHedging?: GrpcHedgingPolicy | false
|
|
123
|
+
): EffectiveCallPolicy {
|
|
124
|
+
assertExclusivePolicies(callRetry, callHedging);
|
|
125
|
+
assertExclusivePolicies(channelRetry, channelHedging);
|
|
126
|
+
|
|
127
|
+
if (typeof callRetry === 'object' && callRetry) {
|
|
128
|
+
return { kind: 'retry', policy: normalizeRetryPolicy(callRetry) };
|
|
129
|
+
}
|
|
130
|
+
if (typeof callHedging === 'object' && callHedging) {
|
|
131
|
+
return { kind: 'hedging', policy: normalizeHedgingPolicy(callHedging) };
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
let retry = channelRetry;
|
|
135
|
+
let hedging = channelHedging;
|
|
136
|
+
if (callRetry === false) {
|
|
137
|
+
retry = null;
|
|
138
|
+
}
|
|
139
|
+
if (callHedging === false) {
|
|
140
|
+
hedging = null;
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
if (retry) {
|
|
144
|
+
return { kind: 'retry', policy: normalizeRetryPolicy(retry) };
|
|
145
|
+
}
|
|
146
|
+
if (hedging) {
|
|
147
|
+
return { kind: 'hedging', policy: normalizeHedgingPolicy(hedging) };
|
|
148
|
+
}
|
|
149
|
+
return { kind: 'none' };
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
/** A6-style backoff with ±20% jitter. */
|
|
153
|
+
export function computeRetryBackoffMs(
|
|
154
|
+
policy: NormalizedRetryPolicy,
|
|
155
|
+
failedAttemptIndex: number,
|
|
156
|
+
random: () => number = Math.random
|
|
157
|
+
): number {
|
|
158
|
+
const exp = Math.max(0, failedAttemptIndex - 1);
|
|
159
|
+
const raw = Math.min(
|
|
160
|
+
policy.initialBackoffMs * Math.pow(policy.backoffMultiplier, exp),
|
|
161
|
+
policy.maxBackoffMs
|
|
162
|
+
);
|
|
163
|
+
const jitter = 0.8 + random() * 0.4;
|
|
164
|
+
return Math.max(0, raw * jitter);
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
export function delay(
|
|
168
|
+
ms: number,
|
|
169
|
+
signal?: {
|
|
170
|
+
aborted: boolean;
|
|
171
|
+
addEventListener: Function;
|
|
172
|
+
removeEventListener: Function;
|
|
173
|
+
}
|
|
174
|
+
): Promise<void> {
|
|
175
|
+
if (ms <= 0) {
|
|
176
|
+
return Promise.resolve();
|
|
177
|
+
}
|
|
178
|
+
return new Promise((resolve, reject) => {
|
|
179
|
+
if (signal?.aborted) {
|
|
180
|
+
reject(Object.assign(new Error('aborted'), { name: 'AbortError' }));
|
|
181
|
+
return;
|
|
182
|
+
}
|
|
183
|
+
const timer = setTimeout(() => {
|
|
184
|
+
signal?.removeEventListener('abort', onAbort);
|
|
185
|
+
resolve();
|
|
186
|
+
}, ms);
|
|
187
|
+
const onAbort = () => {
|
|
188
|
+
clearTimeout(timer);
|
|
189
|
+
reject(Object.assign(new Error('aborted'), { name: 'AbortError' }));
|
|
190
|
+
};
|
|
191
|
+
signal?.addEventListener('abort', onAbort);
|
|
192
|
+
});
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
export function remainingDeadlineSeconds(
|
|
196
|
+
deadlineSeconds: number | undefined,
|
|
197
|
+
startedAtMs: number,
|
|
198
|
+
nowMs: number = Date.now()
|
|
199
|
+
): number | undefined {
|
|
200
|
+
if (deadlineSeconds === undefined) {
|
|
201
|
+
return undefined;
|
|
202
|
+
}
|
|
203
|
+
if (deadlineSeconds <= 0) {
|
|
204
|
+
return 0;
|
|
205
|
+
}
|
|
206
|
+
const elapsed = (nowMs - startedAtMs) / 1000;
|
|
207
|
+
return Math.max(0, deadlineSeconds - elapsed);
|
|
208
|
+
}
|
package/src/status.ts
ADDED
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
/** Canonical gRPC status codes (numeric). */
|
|
2
|
+
export enum GrpcStatusCode {
|
|
3
|
+
OK = 0,
|
|
4
|
+
CANCELLED = 1,
|
|
5
|
+
UNKNOWN = 2,
|
|
6
|
+
INVALID_ARGUMENT = 3,
|
|
7
|
+
DEADLINE_EXCEEDED = 4,
|
|
8
|
+
NOT_FOUND = 5,
|
|
9
|
+
ALREADY_EXISTS = 6,
|
|
10
|
+
PERMISSION_DENIED = 7,
|
|
11
|
+
RESOURCE_EXHAUSTED = 8,
|
|
12
|
+
FAILED_PRECONDITION = 9,
|
|
13
|
+
ABORTED = 10,
|
|
14
|
+
OUT_OF_RANGE = 11,
|
|
15
|
+
UNIMPLEMENTED = 12,
|
|
16
|
+
INTERNAL = 13,
|
|
17
|
+
UNAVAILABLE = 14,
|
|
18
|
+
DATA_LOSS = 15,
|
|
19
|
+
UNAUTHENTICATED = 16,
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
const NAME_TO_CODE: Record<string, number> = Object.keys(GrpcStatusCode)
|
|
23
|
+
.filter((k) => Number.isNaN(Number(k)))
|
|
24
|
+
.reduce((acc, name) => {
|
|
25
|
+
acc[name] = (GrpcStatusCode as any)[name] as number;
|
|
26
|
+
acc[name.toUpperCase()] = (GrpcStatusCode as any)[name] as number;
|
|
27
|
+
return acc;
|
|
28
|
+
}, {} as Record<string, number>);
|
|
29
|
+
|
|
30
|
+
/** Parse a status code from number or A6-style name (`UNAVAILABLE` / `14`). */
|
|
31
|
+
export function parseStatusCode(code: string | number): number {
|
|
32
|
+
if (typeof code === 'number' && Number.isFinite(code)) {
|
|
33
|
+
return code;
|
|
34
|
+
}
|
|
35
|
+
const raw = String(code).trim();
|
|
36
|
+
if (/^\d+$/.test(raw)) {
|
|
37
|
+
return Number(raw);
|
|
38
|
+
}
|
|
39
|
+
const upper = raw.toUpperCase();
|
|
40
|
+
if (upper in NAME_TO_CODE) {
|
|
41
|
+
return NAME_TO_CODE[upper];
|
|
42
|
+
}
|
|
43
|
+
throw new Error(`Unknown gRPC status code: ${code}`);
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
export function parseStatusCodeList(
|
|
47
|
+
codes: ReadonlyArray<string | number>
|
|
48
|
+
): Set<number> {
|
|
49
|
+
return new Set(codes.map(parseStatusCode));
|
|
50
|
+
}
|
package/src/types.ts
CHANGED
|
@@ -1,3 +1,6 @@
|
|
|
1
|
+
import type { GrpcInterceptor } from './interceptors';
|
|
2
|
+
import type { GrpcHedgingPolicy, GrpcRetryPolicy } from './retry';
|
|
3
|
+
|
|
1
4
|
export type GrpcMetadata = Record<string, string>;
|
|
2
5
|
|
|
3
6
|
/** Options for unary / streaming RPCs. */
|
|
@@ -8,12 +11,28 @@ export type GrpcCallOptions = {
|
|
|
8
11
|
* `0` = no deadline (Android); iOS still applies a positive timeout when set.
|
|
9
12
|
*/
|
|
10
13
|
deadlineSeconds?: number;
|
|
14
|
+
/**
|
|
15
|
+
* Extra interceptors for this RPC only (appended after channel interceptors).
|
|
16
|
+
* Prefer channel-level `interceptors` / `setInterceptors` for shared middleware.
|
|
17
|
+
*/
|
|
18
|
+
interceptors?: GrpcInterceptor[];
|
|
19
|
+
/**
|
|
20
|
+
* Unary-only retry policy override, or `false` to disable channel retry for this call.
|
|
21
|
+
* Mutually exclusive with `hedging`.
|
|
22
|
+
*/
|
|
23
|
+
retry?: GrpcRetryPolicy | false;
|
|
24
|
+
/**
|
|
25
|
+
* Unary-only hedging policy override, or `false` to disable channel hedging for this call.
|
|
26
|
+
* Mutually exclusive with `retry`.
|
|
27
|
+
*/
|
|
28
|
+
hedging?: GrpcHedgingPolicy | false;
|
|
11
29
|
};
|
|
12
30
|
|
|
13
31
|
/**
|
|
14
32
|
* Channel TLS configuration. Each `setTlsOptions` call **replaces** the prior
|
|
15
33
|
* config (omitted / null / undefined fields are cleared). Call before
|
|
16
34
|
* `initGrpcChannel()`. Ignored while `setInsecure(true)`.
|
|
35
|
+
* Prefer binding TLS at `createChannel({ tls })` for multi-host apps.
|
|
17
36
|
*/
|
|
18
37
|
export type GrpcTlsOptions = {
|
|
19
38
|
/** PEM trust roots (one or more certs). Clears → platform/gRPC defaults. */
|
|
@@ -34,6 +53,51 @@ export type GrpcTlsOptions = {
|
|
|
34
53
|
spkiSha256Pins?: string[] | null;
|
|
35
54
|
};
|
|
36
55
|
|
|
56
|
+
/** Immutable config for {@link createChannel}. Reconfigure by creating a new channel. */
|
|
57
|
+
export type GrpcChannelConfig = {
|
|
58
|
+
/** Target host, e.g. `api.example.com:443` or `10.0.2.2:50051`. */
|
|
59
|
+
host: string;
|
|
60
|
+
/** Plaintext (dev). Default `false`. */
|
|
61
|
+
insecure?: boolean;
|
|
62
|
+
/** TLS options (ignored when `insecure` is true). */
|
|
63
|
+
tls?: GrpcTlsOptions;
|
|
64
|
+
/** Default per-call deadline in seconds. Default 120. */
|
|
65
|
+
callDeadlineSeconds?: number;
|
|
66
|
+
/** Max inbound message size in bytes (Android / iOS). */
|
|
67
|
+
responseSizeLimit?: number;
|
|
68
|
+
/** Message compression. */
|
|
69
|
+
compression?: {
|
|
70
|
+
enable: boolean;
|
|
71
|
+
compressorName: string;
|
|
72
|
+
};
|
|
73
|
+
/** HTTP/2 keepalive (Android primarily). */
|
|
74
|
+
keepAlive?: {
|
|
75
|
+
enable: boolean;
|
|
76
|
+
keepAliveTime: number;
|
|
77
|
+
keepAliveTimeOut: number;
|
|
78
|
+
};
|
|
79
|
+
/**
|
|
80
|
+
* Request/response middleware for all RPCs on this channel (immutable).
|
|
81
|
+
* Per-RPC extras: `GrpcCallOptions.interceptors`.
|
|
82
|
+
*/
|
|
83
|
+
interceptors?: GrpcInterceptor[];
|
|
84
|
+
/**
|
|
85
|
+
* Unary-only retry policy (A6-style). Mutually exclusive with `hedging`.
|
|
86
|
+
*/
|
|
87
|
+
retry?: GrpcRetryPolicy;
|
|
88
|
+
/**
|
|
89
|
+
* Unary-only hedging policy (A6-style). Mutually exclusive with `retry`.
|
|
90
|
+
*/
|
|
91
|
+
hedging?: GrpcHedgingPolicy;
|
|
92
|
+
};
|
|
93
|
+
|
|
94
|
+
/** Id of the singleton / legacy channel used by `GrpcClient` setters. */
|
|
95
|
+
export const DEFAULT_CHANNEL_ID = 'default';
|
|
96
|
+
|
|
97
|
+
export type { GrpcInterceptStart, GrpcInterceptor } from './interceptors';
|
|
98
|
+
export type { GrpcHedgingPolicy, GrpcRetryPolicy } from './retry';
|
|
99
|
+
export { GrpcStatusCode } from './status';
|
|
100
|
+
|
|
37
101
|
export type RemoveListener = () => void;
|
|
38
102
|
|
|
39
103
|
export interface GrpcServerInputStream {
|