@xdcobra/react-native-grpc 1.1.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 -284
- 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 -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 +625 -625
- 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 +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/package.json +209 -210
- package/react-native-grpc.podspec +29 -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/src/bidi-streaming.ts
CHANGED
|
@@ -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
CHANGED
|
@@ -1,110 +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
|
+
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
|
+
}
|
package/src/client-streaming.ts
CHANGED
|
@@ -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
|
+
}
|