@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/src/retry.ts
CHANGED
|
@@ -1,208 +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
|
-
}
|
|
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/server-streaming.ts
CHANGED
|
@@ -1,105 +1,105 @@
|
|
|
1
|
-
/* eslint-disable eslint-comments/no-unlimited-disable */
|
|
2
|
-
import { AbortController } from 'abort-controller';
|
|
3
|
-
import { EventEmitter } from 'eventemitter3';
|
|
4
|
-
import {
|
|
5
|
-
CompletedGrpcStreamingCall,
|
|
6
|
-
GrpcMetadata,
|
|
7
|
-
GrpcServerOutputStream,
|
|
8
|
-
ServerOutputEvent,
|
|
9
|
-
ServerOutputEventCallback,
|
|
10
|
-
} from './types';
|
|
11
|
-
|
|
12
|
-
/* eslint-disable */
|
|
13
|
-
|
|
14
|
-
export class GrpcServerStreamingCall
|
|
15
|
-
implements PromiseLike<CompletedGrpcStreamingCall> {
|
|
16
|
-
readonly method: string;
|
|
17
|
-
readonly requestHeaders: GrpcMetadata;
|
|
18
|
-
readonly request: Uint8Array;
|
|
19
|
-
readonly headers: Promise<GrpcMetadata>;
|
|
20
|
-
readonly responses: GrpcServerOutputStream;
|
|
21
|
-
readonly trailers: Promise<GrpcMetadata>;
|
|
22
|
-
|
|
23
|
-
#abort: AbortController;
|
|
24
|
-
|
|
25
|
-
constructor(
|
|
26
|
-
method: string,
|
|
27
|
-
data: Uint8Array,
|
|
28
|
-
requestHeaders: GrpcMetadata,
|
|
29
|
-
headers: Promise<GrpcMetadata>,
|
|
30
|
-
responses: ServerOutputStream,
|
|
31
|
-
trailers: Promise<GrpcMetadata>,
|
|
32
|
-
abort: AbortController,
|
|
33
|
-
) {
|
|
34
|
-
this.method = method;
|
|
35
|
-
this.request = data;
|
|
36
|
-
this.requestHeaders = requestHeaders;
|
|
37
|
-
this.headers = headers;
|
|
38
|
-
this.responses = responses;
|
|
39
|
-
this.trailers = trailers;
|
|
40
|
-
this.#abort = abort;
|
|
41
|
-
}
|
|
42
|
-
then<TResult1 = CompletedGrpcStreamingCall, TResult2 = unknown>(
|
|
43
|
-
onfulfilled?:
|
|
44
|
-
| ((
|
|
45
|
-
value: CompletedGrpcStreamingCall
|
|
46
|
-
) => TResult1 | PromiseLike<TResult1>)
|
|
47
|
-
| null,
|
|
48
|
-
onrejected?: ((reason: any) => TResult2 | PromiseLike<TResult2>) | null
|
|
49
|
-
): PromiseLike<TResult1 | TResult2> {
|
|
50
|
-
return this.completedPromise().then(
|
|
51
|
-
(value) =>
|
|
52
|
-
onfulfilled
|
|
53
|
-
? Promise.resolve(onfulfilled(value))
|
|
54
|
-
: ((value as unknown) as TResult1),
|
|
55
|
-
(reason) =>
|
|
56
|
-
onrejected
|
|
57
|
-
? Promise.resolve(onrejected(reason))
|
|
58
|
-
: Promise.reject(reason)
|
|
59
|
-
);
|
|
60
|
-
}
|
|
61
|
-
|
|
62
|
-
cancel() {
|
|
63
|
-
this.#abort.abort();
|
|
64
|
-
}
|
|
65
|
-
|
|
66
|
-
private async completedPromise(): Promise<CompletedGrpcStreamingCall> {
|
|
67
|
-
const [headers, trailers] = await Promise.all([
|
|
68
|
-
this.headers,
|
|
69
|
-
this.trailers,
|
|
70
|
-
]);
|
|
71
|
-
|
|
72
|
-
return {
|
|
73
|
-
method: this.method,
|
|
74
|
-
requestHeaders: this.requestHeaders,
|
|
75
|
-
request: this.request,
|
|
76
|
-
headers,
|
|
77
|
-
trailers,
|
|
78
|
-
status: 0,
|
|
79
|
-
};
|
|
80
|
-
}
|
|
81
|
-
}
|
|
82
|
-
|
|
83
|
-
export class ServerOutputStream implements GrpcServerOutputStream {
|
|
84
|
-
#emitter = new EventEmitter<ServerOutputEvent>();
|
|
85
|
-
|
|
86
|
-
on<T extends ServerOutputEvent>(event: T, callback: ServerOutputEventCallback<T>) {
|
|
87
|
-
this.#emitter.addListener(event, callback);
|
|
88
|
-
|
|
89
|
-
return () => {
|
|
90
|
-
this.#emitter.removeListener(event, callback);
|
|
91
|
-
}
|
|
92
|
-
}
|
|
93
|
-
|
|
94
|
-
notifyData(data: Uint8Array): void {
|
|
95
|
-
this.#emitter.emit('data', data);
|
|
96
|
-
}
|
|
97
|
-
|
|
98
|
-
notifyComplete(): void {
|
|
99
|
-
this.#emitter.emit('complete')
|
|
100
|
-
}
|
|
101
|
-
|
|
102
|
-
noitfyError(reason: any): void {
|
|
103
|
-
this.#emitter.emit('error', reason);
|
|
104
|
-
}
|
|
105
|
-
}
|
|
1
|
+
/* eslint-disable eslint-comments/no-unlimited-disable */
|
|
2
|
+
import { AbortController } from 'abort-controller';
|
|
3
|
+
import { EventEmitter } from 'eventemitter3';
|
|
4
|
+
import {
|
|
5
|
+
CompletedGrpcStreamingCall,
|
|
6
|
+
GrpcMetadata,
|
|
7
|
+
GrpcServerOutputStream,
|
|
8
|
+
ServerOutputEvent,
|
|
9
|
+
ServerOutputEventCallback,
|
|
10
|
+
} from './types';
|
|
11
|
+
|
|
12
|
+
/* eslint-disable */
|
|
13
|
+
|
|
14
|
+
export class GrpcServerStreamingCall
|
|
15
|
+
implements PromiseLike<CompletedGrpcStreamingCall> {
|
|
16
|
+
readonly method: string;
|
|
17
|
+
readonly requestHeaders: GrpcMetadata;
|
|
18
|
+
readonly request: Uint8Array;
|
|
19
|
+
readonly headers: Promise<GrpcMetadata>;
|
|
20
|
+
readonly responses: GrpcServerOutputStream;
|
|
21
|
+
readonly trailers: Promise<GrpcMetadata>;
|
|
22
|
+
|
|
23
|
+
#abort: AbortController;
|
|
24
|
+
|
|
25
|
+
constructor(
|
|
26
|
+
method: string,
|
|
27
|
+
data: Uint8Array,
|
|
28
|
+
requestHeaders: GrpcMetadata,
|
|
29
|
+
headers: Promise<GrpcMetadata>,
|
|
30
|
+
responses: ServerOutputStream,
|
|
31
|
+
trailers: Promise<GrpcMetadata>,
|
|
32
|
+
abort: AbortController,
|
|
33
|
+
) {
|
|
34
|
+
this.method = method;
|
|
35
|
+
this.request = data;
|
|
36
|
+
this.requestHeaders = requestHeaders;
|
|
37
|
+
this.headers = headers;
|
|
38
|
+
this.responses = responses;
|
|
39
|
+
this.trailers = trailers;
|
|
40
|
+
this.#abort = abort;
|
|
41
|
+
}
|
|
42
|
+
then<TResult1 = CompletedGrpcStreamingCall, TResult2 = unknown>(
|
|
43
|
+
onfulfilled?:
|
|
44
|
+
| ((
|
|
45
|
+
value: CompletedGrpcStreamingCall
|
|
46
|
+
) => TResult1 | PromiseLike<TResult1>)
|
|
47
|
+
| null,
|
|
48
|
+
onrejected?: ((reason: any) => TResult2 | PromiseLike<TResult2>) | null
|
|
49
|
+
): PromiseLike<TResult1 | TResult2> {
|
|
50
|
+
return this.completedPromise().then(
|
|
51
|
+
(value) =>
|
|
52
|
+
onfulfilled
|
|
53
|
+
? Promise.resolve(onfulfilled(value))
|
|
54
|
+
: ((value as unknown) as TResult1),
|
|
55
|
+
(reason) =>
|
|
56
|
+
onrejected
|
|
57
|
+
? Promise.resolve(onrejected(reason))
|
|
58
|
+
: Promise.reject(reason)
|
|
59
|
+
);
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
cancel() {
|
|
63
|
+
this.#abort.abort();
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
private async completedPromise(): Promise<CompletedGrpcStreamingCall> {
|
|
67
|
+
const [headers, trailers] = await Promise.all([
|
|
68
|
+
this.headers,
|
|
69
|
+
this.trailers,
|
|
70
|
+
]);
|
|
71
|
+
|
|
72
|
+
return {
|
|
73
|
+
method: this.method,
|
|
74
|
+
requestHeaders: this.requestHeaders,
|
|
75
|
+
request: this.request,
|
|
76
|
+
headers,
|
|
77
|
+
trailers,
|
|
78
|
+
status: 0,
|
|
79
|
+
};
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
export class ServerOutputStream implements GrpcServerOutputStream {
|
|
84
|
+
#emitter = new EventEmitter<ServerOutputEvent>();
|
|
85
|
+
|
|
86
|
+
on<T extends ServerOutputEvent>(event: T, callback: ServerOutputEventCallback<T>) {
|
|
87
|
+
this.#emitter.addListener(event, callback);
|
|
88
|
+
|
|
89
|
+
return () => {
|
|
90
|
+
this.#emitter.removeListener(event, callback);
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
notifyData(data: Uint8Array): void {
|
|
95
|
+
this.#emitter.emit('data', data);
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
notifyComplete(): void {
|
|
99
|
+
this.#emitter.emit('complete')
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
noitfyError(reason: any): void {
|
|
103
|
+
this.#emitter.emit('error', reason);
|
|
104
|
+
}
|
|
105
|
+
}
|