@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/src/native.ts
ADDED
|
@@ -0,0 +1,99 @@
|
|
|
1
|
+
import { NativeModules } from 'react-native';
|
|
2
|
+
import { GrpcMetadata } from './types';
|
|
3
|
+
|
|
4
|
+
export type GrpcRequestObject = {
|
|
5
|
+
data: string;
|
|
6
|
+
/** Optional per-call override; native falls back to channel default when omitted. */
|
|
7
|
+
deadlineSeconds?: number;
|
|
8
|
+
};
|
|
9
|
+
|
|
10
|
+
export type NativeTlsOptions = {
|
|
11
|
+
rootCertsPem: string | null;
|
|
12
|
+
certificateChainPem: string | null;
|
|
13
|
+
privateKeyPem: string | null;
|
|
14
|
+
hostNameOverride: string | null;
|
|
15
|
+
spkiSha256Pins: string[] | null;
|
|
16
|
+
};
|
|
17
|
+
|
|
18
|
+
export type NativeChannelConfig = {
|
|
19
|
+
host: string;
|
|
20
|
+
insecure: boolean;
|
|
21
|
+
tls: NativeTlsOptions | null;
|
|
22
|
+
callDeadlineSeconds: number;
|
|
23
|
+
responseSizeLimit: number | null;
|
|
24
|
+
compressionEnable: boolean;
|
|
25
|
+
compressorName: string;
|
|
26
|
+
keepAliveEnable: boolean;
|
|
27
|
+
keepAliveTime: number;
|
|
28
|
+
keepAliveTimeOut: number;
|
|
29
|
+
};
|
|
30
|
+
|
|
31
|
+
export type GrpcNativeModule = {
|
|
32
|
+
getHost: () => Promise<string>;
|
|
33
|
+
getIsInsecure: () => Promise<boolean>;
|
|
34
|
+
setHost(host: string): void;
|
|
35
|
+
setInsecure(insecure: boolean): void;
|
|
36
|
+
setTlsOptions(options: NativeTlsOptions): void;
|
|
37
|
+
setCompression(enable: boolean, compressorName: string): void;
|
|
38
|
+
setResponseSizeLimit(limitInBytes: number): void;
|
|
39
|
+
setCallDeadlineSeconds(seconds: number): void;
|
|
40
|
+
initGrpcChannel(): void;
|
|
41
|
+
createChannel(channelId: string, config: NativeChannelConfig): void;
|
|
42
|
+
closeChannel(channelId: string): void;
|
|
43
|
+
unaryCall(
|
|
44
|
+
id: number,
|
|
45
|
+
path: string,
|
|
46
|
+
obj: GrpcRequestObject,
|
|
47
|
+
requestHeaders: GrpcMetadata,
|
|
48
|
+
channelId: string
|
|
49
|
+
): Promise<void>;
|
|
50
|
+
serverStreamingCall(
|
|
51
|
+
id: number,
|
|
52
|
+
path: string,
|
|
53
|
+
obj: GrpcRequestObject,
|
|
54
|
+
requestHeaders: GrpcMetadata,
|
|
55
|
+
channelId: string
|
|
56
|
+
): Promise<void>;
|
|
57
|
+
cancelGrpcCall: (id: number) => Promise<boolean>;
|
|
58
|
+
clientStreamingCall(
|
|
59
|
+
id: number,
|
|
60
|
+
path: string,
|
|
61
|
+
obj: GrpcRequestObject,
|
|
62
|
+
requestHeaders: GrpcMetadata,
|
|
63
|
+
channelId: string
|
|
64
|
+
): Promise<void>;
|
|
65
|
+
bidiStreamingCall(
|
|
66
|
+
id: number,
|
|
67
|
+
path: string,
|
|
68
|
+
obj: GrpcRequestObject,
|
|
69
|
+
requestHeaders: GrpcMetadata,
|
|
70
|
+
channelId: string
|
|
71
|
+
): Promise<void>;
|
|
72
|
+
finishClientStreaming(id: number): Promise<void>;
|
|
73
|
+
resetConnection(message: string): void;
|
|
74
|
+
setKeepAlive(
|
|
75
|
+
enable: boolean,
|
|
76
|
+
keepAliveTime: number,
|
|
77
|
+
keepAliveTimeOut: number
|
|
78
|
+
): void;
|
|
79
|
+
onConnectionStateChange(): void;
|
|
80
|
+
setUiLogEnabled(enable: boolean): void;
|
|
81
|
+
enterIdle(): void;
|
|
82
|
+
};
|
|
83
|
+
|
|
84
|
+
/** Lazy binding to NativeModules.Grpc (works with Jest mocks / late link). */
|
|
85
|
+
export function nativeGrpc(): GrpcNativeModule {
|
|
86
|
+
const grpc = (NativeModules as { Grpc: GrpcNativeModule }).Grpc;
|
|
87
|
+
if (!grpc) {
|
|
88
|
+
throw new Error('NativeModules.Grpc is not linked');
|
|
89
|
+
}
|
|
90
|
+
return grpc;
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
export const Grpc: GrpcNativeModule = new Proxy({} as GrpcNativeModule, {
|
|
94
|
+
get(_target, prop) {
|
|
95
|
+
const native = nativeGrpc();
|
|
96
|
+
const value = (native as any)[prop as string];
|
|
97
|
+
return typeof value === 'function' ? value.bind(native) : value;
|
|
98
|
+
},
|
|
99
|
+
});
|
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/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
|
+
}
|
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
|
+
}
|