@celljs/grpc 0.0.0-stage → 4.3.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 +123 -2
- package/cell.yml +12 -0
- package/esm/node/call/async-queue.d.ts +31 -0
- package/esm/node/call/async-queue.js +80 -0
- package/esm/node/call/async-queue.js.map +1 -0
- package/esm/node/call/grpc-status.d.ts +33 -0
- package/esm/node/call/grpc-status.js +48 -0
- package/esm/node/call/grpc-status.js.map +1 -0
- package/esm/node/call/message-framer.d.ts +24 -0
- package/esm/node/call/message-framer.js +105 -0
- package/esm/node/call/message-framer.js.map +1 -0
- package/esm/node/call/metadata.d.ts +15 -0
- package/esm/node/call/metadata.js +81 -0
- package/esm/node/call/metadata.js.map +1 -0
- package/esm/node/client/grpc-channel.d.ts +64 -0
- package/esm/node/client/grpc-channel.js +285 -0
- package/esm/node/client/grpc-channel.js.map +1 -0
- package/esm/node/codec/proto-reader.d.ts +26 -0
- package/esm/node/codec/proto-reader.js +153 -0
- package/esm/node/codec/proto-reader.js.map +1 -0
- package/esm/node/codec/proto-writer.d.ts +29 -0
- package/esm/node/codec/proto-writer.js +150 -0
- package/esm/node/codec/proto-writer.js.map +1 -0
- package/esm/node/index.d.ts +15 -0
- package/esm/node/index.js +16 -0
- package/esm/node/index.js.map +1 -0
- package/esm/node/module.d.ts +3 -0
- package/esm/node/module.js +4 -0
- package/esm/node/module.js.map +1 -0
- package/esm/node/proto/proto-ast.d.ts +46 -0
- package/esm/node/proto/proto-ast.js +2 -0
- package/esm/node/proto/proto-ast.js.map +1 -0
- package/esm/node/proto/proto-parser.d.ts +45 -0
- package/esm/node/proto/proto-parser.js +451 -0
- package/esm/node/proto/proto-parser.js.map +1 -0
- package/esm/node/proto/proto-types.d.ts +76 -0
- package/esm/node/proto/proto-types.js +444 -0
- package/esm/node/proto/proto-types.js.map +1 -0
- package/esm/node/proto/proto.d.ts +57 -0
- package/esm/node/proto/proto.js +174 -0
- package/esm/node/proto/proto.js.map +1 -0
- package/esm/node/proto/well-known.d.ts +5 -0
- package/esm/node/proto/well-known.js +41 -0
- package/esm/node/proto/well-known.js.map +1 -0
- package/esm/node/server/grpc-server-lifecycle.d.ts +21 -0
- package/esm/node/server/grpc-server-lifecycle.js +65 -0
- package/esm/node/server/grpc-server-lifecycle.js.map +1 -0
- package/esm/node/server/grpc-server.d.ts +60 -0
- package/esm/node/server/grpc-server.js +312 -0
- package/esm/node/server/grpc-server.js.map +1 -0
- package/esm/node/server/grpc-service.d.ts +24 -0
- package/esm/node/server/grpc-service.js +38 -0
- package/esm/node/server/grpc-service.js.map +1 -0
- package/esm/node/test/proto-loader.d.ts +2 -0
- package/esm/node/test/proto-loader.js +13 -0
- package/esm/node/test/proto-loader.js.map +1 -0
- package/esm/node/test/test-protos.d.ts +28 -0
- package/esm/node/test/test-protos.js +145 -0
- package/esm/node/test/test-protos.js.map +1 -0
- package/esm/package.json +3 -0
- package/lib/node/call/async-queue.d.ts +32 -0
- package/lib/node/call/async-queue.d.ts.map +1 -0
- package/lib/node/call/async-queue.js +84 -0
- package/lib/node/call/async-queue.js.map +1 -0
- package/lib/node/call/grpc-status.d.ts +34 -0
- package/lib/node/call/grpc-status.d.ts.map +1 -0
- package/lib/node/call/grpc-status.js +52 -0
- package/lib/node/call/grpc-status.js.map +1 -0
- package/lib/node/call/message-framer.d.ts +25 -0
- package/lib/node/call/message-framer.d.ts.map +1 -0
- package/lib/node/call/message-framer.js +109 -0
- package/lib/node/call/message-framer.js.map +1 -0
- package/lib/node/call/metadata.d.ts +16 -0
- package/lib/node/call/metadata.d.ts.map +1 -0
- package/lib/node/call/metadata.js +84 -0
- package/lib/node/call/metadata.js.map +1 -0
- package/lib/node/client/grpc-channel.d.ts +65 -0
- package/lib/node/client/grpc-channel.d.ts.map +1 -0
- package/lib/node/client/grpc-channel.js +289 -0
- package/lib/node/client/grpc-channel.js.map +1 -0
- package/lib/node/codec/proto-reader.d.ts +27 -0
- package/lib/node/codec/proto-reader.d.ts.map +1 -0
- package/lib/node/codec/proto-reader.js +157 -0
- package/lib/node/codec/proto-reader.js.map +1 -0
- package/lib/node/codec/proto-writer.d.ts +30 -0
- package/lib/node/codec/proto-writer.d.ts.map +1 -0
- package/lib/node/codec/proto-writer.js +154 -0
- package/lib/node/codec/proto-writer.js.map +1 -0
- package/lib/node/grpc.spec.d.ts +2 -0
- package/lib/node/grpc.spec.d.ts.map +1 -0
- package/lib/node/grpc.spec.js +148 -0
- package/lib/node/grpc.spec.js.map +1 -0
- package/lib/node/index.d.ts +16 -0
- package/lib/node/index.d.ts.map +1 -0
- package/lib/node/index.js +32 -0
- package/lib/node/index.js.map +1 -0
- package/lib/node/interop.spec.d.ts +2 -0
- package/lib/node/interop.spec.d.ts.map +1 -0
- package/lib/node/interop.spec.js +194 -0
- package/lib/node/interop.spec.js.map +1 -0
- package/lib/node/module.d.ts +4 -0
- package/lib/node/module.d.ts.map +1 -0
- package/lib/node/module.js +6 -0
- package/lib/node/module.js.map +1 -0
- package/lib/node/proto/proto-ast.d.ts +47 -0
- package/lib/node/proto/proto-ast.d.ts.map +1 -0
- package/lib/node/proto/proto-ast.js +3 -0
- package/lib/node/proto/proto-ast.js.map +1 -0
- package/lib/node/proto/proto-parser.d.ts +46 -0
- package/lib/node/proto/proto-parser.d.ts.map +1 -0
- package/lib/node/proto/proto-parser.js +455 -0
- package/lib/node/proto/proto-parser.js.map +1 -0
- package/lib/node/proto/proto-parser.spec.d.ts +2 -0
- package/lib/node/proto/proto-parser.spec.d.ts.map +1 -0
- package/lib/node/proto/proto-parser.spec.js +95 -0
- package/lib/node/proto/proto-parser.spec.js.map +1 -0
- package/lib/node/proto/proto-types.d.ts +77 -0
- package/lib/node/proto/proto-types.d.ts.map +1 -0
- package/lib/node/proto/proto-types.js +449 -0
- package/lib/node/proto/proto-types.js.map +1 -0
- package/lib/node/proto/proto-types.spec.d.ts +2 -0
- package/lib/node/proto/proto-types.spec.d.ts.map +1 -0
- package/lib/node/proto/proto-types.spec.js +120 -0
- package/lib/node/proto/proto-types.spec.js.map +1 -0
- package/lib/node/proto/proto.d.ts +58 -0
- package/lib/node/proto/proto.d.ts.map +1 -0
- package/lib/node/proto/proto.js +178 -0
- package/lib/node/proto/proto.js.map +1 -0
- package/lib/node/proto/well-known.d.ts +6 -0
- package/lib/node/proto/well-known.d.ts.map +1 -0
- package/lib/node/proto/well-known.js +44 -0
- package/lib/node/proto/well-known.js.map +1 -0
- package/lib/node/server/grpc-server-lifecycle.d.ts +22 -0
- package/lib/node/server/grpc-server-lifecycle.d.ts.map +1 -0
- package/lib/node/server/grpc-server-lifecycle.js +68 -0
- package/lib/node/server/grpc-server-lifecycle.js.map +1 -0
- package/lib/node/server/grpc-server-lifecycle.spec.d.ts +2 -0
- package/lib/node/server/grpc-server-lifecycle.spec.d.ts.map +1 -0
- package/lib/node/server/grpc-server-lifecycle.spec.js +100 -0
- package/lib/node/server/grpc-server-lifecycle.spec.js.map +1 -0
- package/lib/node/server/grpc-server.d.ts +61 -0
- package/lib/node/server/grpc-server.d.ts.map +1 -0
- package/lib/node/server/grpc-server.js +316 -0
- package/lib/node/server/grpc-server.js.map +1 -0
- package/lib/node/server/grpc-service.d.ts +25 -0
- package/lib/node/server/grpc-service.d.ts.map +1 -0
- package/lib/node/server/grpc-service.js +42 -0
- package/lib/node/server/grpc-service.js.map +1 -0
- package/lib/node/test/proto-loader.d.ts +3 -0
- package/lib/node/test/proto-loader.d.ts.map +1 -0
- package/lib/node/test/proto-loader.js +16 -0
- package/lib/node/test/proto-loader.js.map +1 -0
- package/lib/node/test/test-protos.d.ts +29 -0
- package/lib/node/test/test-protos.d.ts.map +1 -0
- package/lib/node/test/test-protos.js +150 -0
- package/lib/node/test/test-protos.js.map +1 -0
- package/lib/package.spec.d.ts +2 -0
- package/lib/package.spec.d.ts.map +1 -0
- package/lib/package.spec.js +7 -0
- package/lib/package.spec.js.map +1 -0
- package/package.json +69 -4
- package/src/node/call/async-queue.ts +79 -0
- package/src/node/call/grpc-status.ts +49 -0
- package/src/node/call/message-framer.ts +109 -0
- package/src/node/call/metadata.ts +85 -0
- package/src/node/client/grpc-channel.ts +327 -0
- package/src/node/codec/proto-reader.ts +172 -0
- package/src/node/codec/proto-writer.ts +170 -0
- package/src/node/grpc.spec.ts +160 -0
- package/src/node/index.ts +15 -0
- package/src/node/interop.spec.ts +174 -0
- package/src/node/module.ts +4 -0
- package/src/node/proto/proto-ast.ts +48 -0
- package/src/node/proto/proto-parser.spec.ts +104 -0
- package/src/node/proto/proto-parser.ts +477 -0
- package/src/node/proto/proto-types.spec.ts +128 -0
- package/src/node/proto/proto-types.ts +497 -0
- package/src/node/proto/proto.ts +218 -0
- package/src/node/proto/well-known.ts +40 -0
- package/src/node/server/grpc-server-lifecycle.spec.ts +88 -0
- package/src/node/server/grpc-server-lifecycle.ts +56 -0
- package/src/node/server/grpc-server.ts +368 -0
- package/src/node/server/grpc-service.ts +40 -0
- package/src/node/test/proto-loader.ts +13 -0
- package/src/node/test/test-protos.ts +159 -0
- package/src/package.spec.ts +5 -0
|
@@ -0,0 +1,327 @@
|
|
|
1
|
+
import { type ClientHttp2Session, type ClientHttp2Stream, connect, constants, type IncomingHttpHeaders } from 'node:http2';
|
|
2
|
+
import { AsyncQueue } from '../call/async-queue';
|
|
3
|
+
import { GrpcError, GrpcStatus } from '../call/grpc-status';
|
|
4
|
+
import { ACCEPT_ENCODING, DEFAULT_MAX_RECEIVE_MESSAGE_SIZE, MessageDeframer, MessageFramer } from '../call/message-framer';
|
|
5
|
+
import { type Metadata, MetadataUtil } from '../call/metadata';
|
|
6
|
+
import type { MethodDefinition, ServiceDefinition } from '../proto/proto';
|
|
7
|
+
|
|
8
|
+
export interface CallOptions {
|
|
9
|
+
/** Request metadata, added to the channel's. */
|
|
10
|
+
metadata?: Metadata;
|
|
11
|
+
/** Deadline in milliseconds from now; the call fails with `DEADLINE_EXCEEDED` after it. */
|
|
12
|
+
timeout?: number;
|
|
13
|
+
/** Cancels the call (`CANCELLED`). */
|
|
14
|
+
signal?: AbortSignal;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
export interface GrpcChannelOptions {
|
|
18
|
+
/** Metadata sent with every call. */
|
|
19
|
+
metadata?: Metadata;
|
|
20
|
+
/** Default {@link CallOptions.timeout}, in milliseconds. */
|
|
21
|
+
timeout?: number;
|
|
22
|
+
/** Largest response message accepted, in bytes. Default 4 MiB. */
|
|
23
|
+
maxReceiveMessageSize?: number;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
type Stream<T> = Iterable<T> | AsyncIterable<T>;
|
|
27
|
+
|
|
28
|
+
/** `rpc M (Req) returns (Res)` */
|
|
29
|
+
export type UnaryCall<Req = any, Res = any> = (request: Req, options?: CallOptions) => Promise<Res>;
|
|
30
|
+
/** `rpc M (stream Req) returns (Res)` */
|
|
31
|
+
export type ClientStreamCall<Req = any, Res = any> = (requests: Stream<Req>, options?: CallOptions) => Promise<Res>;
|
|
32
|
+
/** `rpc M (Req) returns (stream Res)`: iterate the responses with `for await`; breaking out cancels the call. */
|
|
33
|
+
export type ServerStreamCall<Req = any, Res = any> = (request: Req, options?: CallOptions) => AsyncIterable<Res>;
|
|
34
|
+
/** `rpc M (stream Req) returns (stream Res)` */
|
|
35
|
+
export type BidiStreamCall<Req = any, Res = any> = (requests: Stream<Req>, options?: CallOptions) => AsyncIterable<Res>;
|
|
36
|
+
|
|
37
|
+
const USER_AGENT = 'cell-grpc';
|
|
38
|
+
|
|
39
|
+
/** HTTP statuses of non-gRPC answers, mapped as the gRPC specification says. */
|
|
40
|
+
const HTTP_STATUS: Record<number, GrpcStatus> = {
|
|
41
|
+
400: GrpcStatus.INTERNAL,
|
|
42
|
+
401: GrpcStatus.UNAUTHENTICATED,
|
|
43
|
+
403: GrpcStatus.PERMISSION_DENIED,
|
|
44
|
+
404: GrpcStatus.UNIMPLEMENTED,
|
|
45
|
+
429: GrpcStatus.UNAVAILABLE,
|
|
46
|
+
502: GrpcStatus.UNAVAILABLE,
|
|
47
|
+
503: GrpcStatus.UNAVAILABLE,
|
|
48
|
+
504: GrpcStatus.UNAVAILABLE,
|
|
49
|
+
};
|
|
50
|
+
|
|
51
|
+
/**
|
|
52
|
+
* A connection to a gRPC server (one HTTP/2 session, opened on the first call and reopened when lost)
|
|
53
|
+
* and the factory of its clients. An idle channel does not keep the process alive.
|
|
54
|
+
*
|
|
55
|
+
* ```ts
|
|
56
|
+
* const channel = new GrpcChannel('localhost:50051');
|
|
57
|
+
* const greeter = channel.createClient<GreeterClient>(Greeter);
|
|
58
|
+
* const { message } = await greeter.sayHello({ name: 'Cell' });
|
|
59
|
+
* ```
|
|
60
|
+
*/
|
|
61
|
+
export class GrpcChannel {
|
|
62
|
+
/** `http://host:port`; an address without a scheme gets `http://`. */
|
|
63
|
+
readonly address: string;
|
|
64
|
+
protected session?: ClientHttp2Session;
|
|
65
|
+
protected active = 0;
|
|
66
|
+
/** The connection error of each session, to explain the failure of its calls. */
|
|
67
|
+
protected readonly errors = new WeakMap<ClientHttp2Session, string>();
|
|
68
|
+
|
|
69
|
+
constructor(
|
|
70
|
+
address: string,
|
|
71
|
+
protected readonly options: GrpcChannelOptions = {},
|
|
72
|
+
) {
|
|
73
|
+
this.address = /^[a-z][a-z0-9+.-]*:\/\//i.test(address) ? address : `http://${address}`;
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
/**
|
|
77
|
+
* A client of `service`: one function per method, named in lowerCamelCase (`SayHello` → `sayHello`).
|
|
78
|
+
* Type it with an interface of {@link UnaryCall}, {@link ServerStreamCall}... members.
|
|
79
|
+
*/
|
|
80
|
+
createClient<T = Record<string, (...args: any[]) => any>>(service: ServiceDefinition): T {
|
|
81
|
+
const client: Record<string, unknown> = {};
|
|
82
|
+
for (const method of service.methods) {
|
|
83
|
+
client[method.jsName] = (input: unknown, options?: CallOptions) => this.call(method, input, options);
|
|
84
|
+
}
|
|
85
|
+
return client as T;
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
/** Calls `method`: a promise of the response, or an async iterable of them when the method streams responses. */
|
|
89
|
+
call(method: MethodDefinition, input: unknown, options: CallOptions = {}): Promise<unknown> | AsyncIterable<unknown> {
|
|
90
|
+
const responses = this.start(method, input, options);
|
|
91
|
+
return method.responseStream ? responses : single(responses, method);
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
/** Closes the connection once the calls in progress end. */
|
|
95
|
+
close(): void {
|
|
96
|
+
this.session?.close();
|
|
97
|
+
this.session = undefined;
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
protected start(method: MethodDefinition, input: unknown, options: CallOptions): AsyncQueue<unknown> {
|
|
101
|
+
let stream: ClientHttp2Stream | undefined;
|
|
102
|
+
const queue = new AsyncQueue<unknown>({
|
|
103
|
+
pause: () => stream?.pause(),
|
|
104
|
+
resume: () => stream?.resume(),
|
|
105
|
+
onReturn: () => cancel(new GrpcError(GrpcStatus.CANCELLED, 'The call was cancelled')),
|
|
106
|
+
});
|
|
107
|
+
let request: Uint8Array | undefined;
|
|
108
|
+
if (!method.requestStream) {
|
|
109
|
+
try {
|
|
110
|
+
request = MessageFramer.frame(method.requestType.encode(input));
|
|
111
|
+
} catch (error) {
|
|
112
|
+
queue.end(error);
|
|
113
|
+
return queue;
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
if (options.signal?.aborted) {
|
|
117
|
+
queue.end(new GrpcError(GrpcStatus.CANCELLED, 'The call was cancelled'));
|
|
118
|
+
return queue;
|
|
119
|
+
}
|
|
120
|
+
const timeout = options.timeout ?? this.options.timeout;
|
|
121
|
+
const headers = MetadataUtil.toHeaders(
|
|
122
|
+
{ ...this.options.metadata, ...options.metadata },
|
|
123
|
+
{
|
|
124
|
+
':method': 'POST',
|
|
125
|
+
':path': method.path,
|
|
126
|
+
'content-type': 'application/grpc+proto',
|
|
127
|
+
te: 'trailers',
|
|
128
|
+
'grpc-accept-encoding': ACCEPT_ENCODING,
|
|
129
|
+
'user-agent': USER_AGENT,
|
|
130
|
+
},
|
|
131
|
+
);
|
|
132
|
+
if (timeout !== undefined) {
|
|
133
|
+
headers['grpc-timeout'] = MetadataUtil.formatTimeout(timeout);
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
// undefined: no status yet; null: OK.
|
|
137
|
+
let status: GrpcError | null | undefined;
|
|
138
|
+
let failure: unknown;
|
|
139
|
+
const cancel = (error: unknown) => {
|
|
140
|
+
if (failure === undefined && status === undefined) {
|
|
141
|
+
failure = error;
|
|
142
|
+
}
|
|
143
|
+
if (stream && !stream.closed && !stream.destroyed) {
|
|
144
|
+
stream.close(constants.NGHTTP2_CANCEL);
|
|
145
|
+
}
|
|
146
|
+
};
|
|
147
|
+
|
|
148
|
+
let session: ClientHttp2Session;
|
|
149
|
+
try {
|
|
150
|
+
session = this.connect();
|
|
151
|
+
stream = session.request(headers);
|
|
152
|
+
} catch (error) {
|
|
153
|
+
queue.end(new GrpcError(GrpcStatus.UNAVAILABLE, (error as Error).message));
|
|
154
|
+
return queue;
|
|
155
|
+
}
|
|
156
|
+
this.retain();
|
|
157
|
+
const timer = timeout === undefined ? undefined : setTimeout(() => cancel(new GrpcError(GrpcStatus.DEADLINE_EXCEEDED, 'Deadline exceeded')), timeout);
|
|
158
|
+
const onAbort = () => cancel(new GrpcError(GrpcStatus.CANCELLED, 'The call was cancelled'));
|
|
159
|
+
options.signal?.addEventListener('abort', onAbort, { once: true });
|
|
160
|
+
|
|
161
|
+
let deframer: MessageDeframer | undefined;
|
|
162
|
+
let responded = false;
|
|
163
|
+
// Deno emits DATA before the response headers: keep it until they arrive.
|
|
164
|
+
let early: Uint8Array[] | undefined = [];
|
|
165
|
+
const receive = (into: MessageDeframer, chunk: Uint8Array) => {
|
|
166
|
+
try {
|
|
167
|
+
for (const message of into.push(chunk)) {
|
|
168
|
+
queue.push(decode(method, message));
|
|
169
|
+
}
|
|
170
|
+
} catch (error) {
|
|
171
|
+
cancel(error);
|
|
172
|
+
}
|
|
173
|
+
};
|
|
174
|
+
stream.on('response', responseHeaders => {
|
|
175
|
+
responded = true;
|
|
176
|
+
const httpStatus = Number(responseHeaders[':status']);
|
|
177
|
+
const contentType = String(responseHeaders['content-type'] ?? '');
|
|
178
|
+
if (responseHeaders['grpc-status'] !== undefined) {
|
|
179
|
+
status = statusOf(responseHeaders);
|
|
180
|
+
} else if (httpStatus !== 200) {
|
|
181
|
+
cancel(new GrpcError(HTTP_STATUS[httpStatus] ?? GrpcStatus.UNKNOWN, `Received HTTP status ${httpStatus}`));
|
|
182
|
+
} else if (!contentType.startsWith('application/grpc')) {
|
|
183
|
+
cancel(new GrpcError(GrpcStatus.UNKNOWN, `Received a non-gRPC response (content-type '${contentType}')`));
|
|
184
|
+
} else {
|
|
185
|
+
try {
|
|
186
|
+
const created = new MessageDeframer(
|
|
187
|
+
String(responseHeaders['grpc-encoding'] ?? 'identity'),
|
|
188
|
+
this.options.maxReceiveMessageSize ?? DEFAULT_MAX_RECEIVE_MESSAGE_SIZE,
|
|
189
|
+
);
|
|
190
|
+
deframer = created;
|
|
191
|
+
for (const chunk of early ?? []) {
|
|
192
|
+
receive(created, chunk);
|
|
193
|
+
}
|
|
194
|
+
} catch (error) {
|
|
195
|
+
cancel(error);
|
|
196
|
+
}
|
|
197
|
+
}
|
|
198
|
+
early = undefined;
|
|
199
|
+
});
|
|
200
|
+
stream.on('data', (chunk: Uint8Array) => {
|
|
201
|
+
if (failure !== undefined) {
|
|
202
|
+
return;
|
|
203
|
+
}
|
|
204
|
+
if (deframer) {
|
|
205
|
+
receive(deframer, chunk);
|
|
206
|
+
} else {
|
|
207
|
+
early?.push(chunk);
|
|
208
|
+
}
|
|
209
|
+
});
|
|
210
|
+
stream.on('trailers', trailers => {
|
|
211
|
+
if (status === undefined) {
|
|
212
|
+
status = statusOf(trailers);
|
|
213
|
+
}
|
|
214
|
+
});
|
|
215
|
+
let streamError: string | undefined;
|
|
216
|
+
stream.on('error', error => {
|
|
217
|
+
streamError = error.message;
|
|
218
|
+
});
|
|
219
|
+
stream.on('close', () => {
|
|
220
|
+
clearTimeout(timer);
|
|
221
|
+
options.signal?.removeEventListener('abort', onAbort);
|
|
222
|
+
this.release();
|
|
223
|
+
if (failure !== undefined) {
|
|
224
|
+
queue.end(failure);
|
|
225
|
+
} else if (status !== undefined) {
|
|
226
|
+
queue.end(status ?? undefined);
|
|
227
|
+
} else if (!responded) {
|
|
228
|
+
// Not connected, or the stream was refused or reset before the server answered.
|
|
229
|
+
const reason = this.errors.get(session) ?? streamError ?? `the stream was closed with HTTP/2 error code ${stream?.rstCode}`;
|
|
230
|
+
queue.end(new GrpcError(GrpcStatus.UNAVAILABLE, `No response from ${this.address}: ${reason}`));
|
|
231
|
+
} else {
|
|
232
|
+
queue.end(new GrpcError(GrpcStatus.INTERNAL, `The stream closed without a status (HTTP/2 error code ${stream?.rstCode})`));
|
|
233
|
+
}
|
|
234
|
+
});
|
|
235
|
+
|
|
236
|
+
if (request) {
|
|
237
|
+
stream.end(request);
|
|
238
|
+
} else {
|
|
239
|
+
this.pump(stream, method, input as Stream<unknown>).catch(error => cancel(error));
|
|
240
|
+
}
|
|
241
|
+
return queue;
|
|
242
|
+
}
|
|
243
|
+
|
|
244
|
+
protected async pump(stream: ClientHttp2Stream, method: MethodDefinition, requests: Stream<unknown>): Promise<void> {
|
|
245
|
+
for await (const message of requests) {
|
|
246
|
+
if (stream.closed || stream.destroyed) {
|
|
247
|
+
return;
|
|
248
|
+
}
|
|
249
|
+
if (!stream.write(MessageFramer.frame(method.requestType.encode(message)))) {
|
|
250
|
+
await new Promise<void>(resolve => {
|
|
251
|
+
const done = () => {
|
|
252
|
+
stream.off('drain', done);
|
|
253
|
+
stream.off('close', done);
|
|
254
|
+
resolve();
|
|
255
|
+
};
|
|
256
|
+
stream.on('drain', done);
|
|
257
|
+
stream.on('close', done);
|
|
258
|
+
});
|
|
259
|
+
}
|
|
260
|
+
}
|
|
261
|
+
if (!stream.closed && !stream.destroyed) {
|
|
262
|
+
stream.end();
|
|
263
|
+
}
|
|
264
|
+
}
|
|
265
|
+
|
|
266
|
+
protected connect(): ClientHttp2Session {
|
|
267
|
+
if (this.session && !this.session.closed && !this.session.destroyed) {
|
|
268
|
+
return this.session;
|
|
269
|
+
}
|
|
270
|
+
const session = connect(this.address);
|
|
271
|
+
const forget = () => {
|
|
272
|
+
if (this.session === session) {
|
|
273
|
+
this.session = undefined;
|
|
274
|
+
}
|
|
275
|
+
};
|
|
276
|
+
session.on('error', error => this.errors.set(session, error.message));
|
|
277
|
+
session.on('close', forget);
|
|
278
|
+
session.on('goaway', forget);
|
|
279
|
+
this.session = session;
|
|
280
|
+
return session;
|
|
281
|
+
}
|
|
282
|
+
|
|
283
|
+
protected retain(): void {
|
|
284
|
+
if (this.active++ === 0) {
|
|
285
|
+
this.session?.ref();
|
|
286
|
+
}
|
|
287
|
+
}
|
|
288
|
+
|
|
289
|
+
protected release(): void {
|
|
290
|
+
if (--this.active === 0) {
|
|
291
|
+
this.session?.unref();
|
|
292
|
+
}
|
|
293
|
+
}
|
|
294
|
+
}
|
|
295
|
+
|
|
296
|
+
function statusOf(headers: IncomingHttpHeaders): GrpcError | null {
|
|
297
|
+
const code = Number(headers['grpc-status']);
|
|
298
|
+
if (code === GrpcStatus.OK) {
|
|
299
|
+
return null;
|
|
300
|
+
}
|
|
301
|
+
return new GrpcError(
|
|
302
|
+
Number.isInteger(code) ? code : GrpcStatus.UNKNOWN,
|
|
303
|
+
MetadataUtil.decodeMessage(headers['grpc-message']),
|
|
304
|
+
MetadataUtil.fromHeaders(headers),
|
|
305
|
+
);
|
|
306
|
+
}
|
|
307
|
+
|
|
308
|
+
function decode(method: MethodDefinition, message: Uint8Array): unknown {
|
|
309
|
+
try {
|
|
310
|
+
return method.responseType.decode(message);
|
|
311
|
+
} catch (error) {
|
|
312
|
+
throw new GrpcError(GrpcStatus.INTERNAL, `Could not parse a ${method.responseType.fullName}: ${(error as Error).message}`);
|
|
313
|
+
}
|
|
314
|
+
}
|
|
315
|
+
|
|
316
|
+
async function single(responses: AsyncIterable<unknown>, method: MethodDefinition): Promise<unknown> {
|
|
317
|
+
let response: unknown;
|
|
318
|
+
let count = 0;
|
|
319
|
+
for await (const message of responses) {
|
|
320
|
+
response = message;
|
|
321
|
+
count++;
|
|
322
|
+
}
|
|
323
|
+
if (count !== 1) {
|
|
324
|
+
throw new GrpcError(GrpcStatus.INTERNAL, `${method.path} answered ${count} response messages instead of one`);
|
|
325
|
+
}
|
|
326
|
+
return response;
|
|
327
|
+
}
|
|
@@ -0,0 +1,172 @@
|
|
|
1
|
+
const decoder = new TextDecoder('utf-8', { fatal: false, ignoreBOM: true });
|
|
2
|
+
|
|
3
|
+
/** Reads the protobuf binary format. Every read past the end throws a `RangeError`. */
|
|
4
|
+
export class ProtoReader {
|
|
5
|
+
protected readonly view: DataView;
|
|
6
|
+
pos = 0;
|
|
7
|
+
readonly end: number;
|
|
8
|
+
|
|
9
|
+
constructor(protected readonly buf: Uint8Array) {
|
|
10
|
+
this.view = new DataView(buf.buffer, buf.byteOffset, buf.byteLength);
|
|
11
|
+
this.end = buf.length;
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
uint32(): number {
|
|
15
|
+
const buf = this.buf;
|
|
16
|
+
let p = this.pos;
|
|
17
|
+
let b = buf[p++];
|
|
18
|
+
let value = b & 127;
|
|
19
|
+
if (b < 128) {
|
|
20
|
+
return this.done(p, value);
|
|
21
|
+
}
|
|
22
|
+
b = buf[p++];
|
|
23
|
+
value |= (b & 127) << 7;
|
|
24
|
+
if (b < 128) {
|
|
25
|
+
return this.done(p, value);
|
|
26
|
+
}
|
|
27
|
+
b = buf[p++];
|
|
28
|
+
value |= (b & 127) << 14;
|
|
29
|
+
if (b < 128) {
|
|
30
|
+
return this.done(p, value);
|
|
31
|
+
}
|
|
32
|
+
b = buf[p++];
|
|
33
|
+
value |= (b & 127) << 21;
|
|
34
|
+
if (b < 128) {
|
|
35
|
+
return this.done(p, value);
|
|
36
|
+
}
|
|
37
|
+
b = buf[p++];
|
|
38
|
+
value |= (b & 15) << 28;
|
|
39
|
+
// The rest of a 64-bit varint (negative int32 values) does not change the low 32 bits.
|
|
40
|
+
for (let i = 0; b >= 128 && i < 5; i++) {
|
|
41
|
+
b = buf[p++];
|
|
42
|
+
}
|
|
43
|
+
if (b >= 128 || b === undefined) {
|
|
44
|
+
this.pos = p;
|
|
45
|
+
this.check();
|
|
46
|
+
throw new RangeError('invalid varint');
|
|
47
|
+
}
|
|
48
|
+
return this.done(p, value >>> 0);
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
int32(): number {
|
|
52
|
+
return this.uint32() | 0;
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
sint32(): number {
|
|
56
|
+
const v = this.uint32();
|
|
57
|
+
return (v >>> 1) ^ -(v & 1);
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
bool(): boolean {
|
|
61
|
+
return this.uint32() !== 0;
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
varint64(): bigint {
|
|
65
|
+
const buf = this.buf;
|
|
66
|
+
let b = buf[this.pos];
|
|
67
|
+
if (b < 128) {
|
|
68
|
+
this.pos++;
|
|
69
|
+
return BigInt(b);
|
|
70
|
+
}
|
|
71
|
+
let value = BigInt(0);
|
|
72
|
+
let shift = BigInt(0);
|
|
73
|
+
for (let i = 0; i < 10; i++) {
|
|
74
|
+
b = buf[this.pos++];
|
|
75
|
+
if (b === undefined) {
|
|
76
|
+
break;
|
|
77
|
+
}
|
|
78
|
+
value |= BigInt(b & 127) << shift;
|
|
79
|
+
if (b < 128) {
|
|
80
|
+
return BigInt.asUintN(64, value);
|
|
81
|
+
}
|
|
82
|
+
shift += BigInt(7);
|
|
83
|
+
}
|
|
84
|
+
this.check();
|
|
85
|
+
throw new RangeError('invalid varint');
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
fixed32(): number {
|
|
89
|
+
const v = this.view.getUint32(this.advance(4), true);
|
|
90
|
+
return v;
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
sfixed32(): number {
|
|
94
|
+
return this.view.getInt32(this.advance(4), true);
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
float(): number {
|
|
98
|
+
return this.view.getFloat32(this.advance(4), true);
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
double(): number {
|
|
102
|
+
return this.view.getFloat64(this.advance(8), true);
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
fixed64(): bigint {
|
|
106
|
+
return this.view.getBigUint64(this.advance(8), true);
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
sfixed64(): bigint {
|
|
110
|
+
return this.view.getBigInt64(this.advance(8), true);
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
/** A copy of the next length-delimited value. */
|
|
114
|
+
bytes(): Uint8Array {
|
|
115
|
+
const length = this.uint32();
|
|
116
|
+
const start = this.advance(length);
|
|
117
|
+
return this.buf.slice(start, start + length);
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
string(): string {
|
|
121
|
+
const length = this.uint32();
|
|
122
|
+
const start = this.advance(length);
|
|
123
|
+
return decoder.decode(this.buf.subarray(start, start + length));
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
skip(wireType: number): void {
|
|
127
|
+
switch (wireType) {
|
|
128
|
+
case 0:
|
|
129
|
+
this.varint64();
|
|
130
|
+
break;
|
|
131
|
+
case 1:
|
|
132
|
+
this.advance(8);
|
|
133
|
+
break;
|
|
134
|
+
case 2:
|
|
135
|
+
this.advance(this.uint32());
|
|
136
|
+
break;
|
|
137
|
+
case 3:
|
|
138
|
+
for (;;) {
|
|
139
|
+
const tag = this.uint32();
|
|
140
|
+
if ((tag & 7) === 4) {
|
|
141
|
+
break;
|
|
142
|
+
}
|
|
143
|
+
this.skip(tag & 7);
|
|
144
|
+
}
|
|
145
|
+
break;
|
|
146
|
+
case 5:
|
|
147
|
+
this.advance(4);
|
|
148
|
+
break;
|
|
149
|
+
default:
|
|
150
|
+
throw new RangeError(`invalid wire type ${wireType} at offset ${this.pos}`);
|
|
151
|
+
}
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
protected advance(count: number): number {
|
|
155
|
+
const start = this.pos;
|
|
156
|
+
this.pos += count;
|
|
157
|
+
this.check();
|
|
158
|
+
return start;
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
protected done(pos: number, value: number): number {
|
|
162
|
+
this.pos = pos;
|
|
163
|
+
this.check();
|
|
164
|
+
return value;
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
protected check(): void {
|
|
168
|
+
if (this.pos > this.end) {
|
|
169
|
+
throw new RangeError('truncated message');
|
|
170
|
+
}
|
|
171
|
+
}
|
|
172
|
+
}
|
|
@@ -0,0 +1,170 @@
|
|
|
1
|
+
const encoder = new TextEncoder();
|
|
2
|
+
|
|
3
|
+
/** Writes the protobuf binary format into a growing buffer. */
|
|
4
|
+
export class ProtoWriter {
|
|
5
|
+
protected buf: Uint8Array;
|
|
6
|
+
protected view: DataView;
|
|
7
|
+
pos = 0;
|
|
8
|
+
|
|
9
|
+
constructor(initialSize = 64) {
|
|
10
|
+
this.buf = new Uint8Array(initialSize);
|
|
11
|
+
this.view = new DataView(this.buf.buffer);
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
tag(fieldNumber: number, wireType: number): void {
|
|
15
|
+
this.uint32(((fieldNumber << 3) | wireType) >>> 0);
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
uint32(value: number): void {
|
|
19
|
+
this.ensure(5);
|
|
20
|
+
const buf = this.buf;
|
|
21
|
+
let v = value >>> 0;
|
|
22
|
+
while (v > 127) {
|
|
23
|
+
buf[this.pos++] = (v & 127) | 128;
|
|
24
|
+
v >>>= 7;
|
|
25
|
+
}
|
|
26
|
+
buf[this.pos++] = v;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
int32(value: number): void {
|
|
30
|
+
if (value >= 0) {
|
|
31
|
+
this.uint32(value);
|
|
32
|
+
} else {
|
|
33
|
+
// Negative int32 values are sign-extended to 64 bits: always 10 bytes.
|
|
34
|
+
this.ensure(10);
|
|
35
|
+
const buf = this.buf;
|
|
36
|
+
let v = value >>> 0;
|
|
37
|
+
for (let i = 0; i < 4; i++) {
|
|
38
|
+
buf[this.pos++] = (v & 127) | 128;
|
|
39
|
+
v >>>= 7;
|
|
40
|
+
}
|
|
41
|
+
buf[this.pos++] = (v & 15) | 0xf0;
|
|
42
|
+
for (let i = 0; i < 4; i++) {
|
|
43
|
+
buf[this.pos++] = 0xff;
|
|
44
|
+
}
|
|
45
|
+
buf[this.pos++] = 1;
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
sint32(value: number): void {
|
|
50
|
+
this.uint32((value << 1) ^ (value >> 31));
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
bool(value: boolean): void {
|
|
54
|
+
this.ensure(1);
|
|
55
|
+
this.buf[this.pos++] = value ? 1 : 0;
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
/** An unsigned 64-bit varint; `value` must be in [0, 2^64). */
|
|
59
|
+
varint64(value: bigint): void {
|
|
60
|
+
if (value < 0x80000000) {
|
|
61
|
+
this.uint32(Number(value));
|
|
62
|
+
return;
|
|
63
|
+
}
|
|
64
|
+
this.ensure(10);
|
|
65
|
+
let v = value;
|
|
66
|
+
while (v > 127) {
|
|
67
|
+
this.buf[this.pos++] = Number(v & BigInt(127)) | 128;
|
|
68
|
+
v >>= BigInt(7);
|
|
69
|
+
}
|
|
70
|
+
this.buf[this.pos++] = Number(v);
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
fixed32(value: number): void {
|
|
74
|
+
this.ensure(4);
|
|
75
|
+
this.view.setUint32(this.pos, value >>> 0, true);
|
|
76
|
+
this.pos += 4;
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
sfixed32(value: number): void {
|
|
80
|
+
this.ensure(4);
|
|
81
|
+
this.view.setInt32(this.pos, value | 0, true);
|
|
82
|
+
this.pos += 4;
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
float(value: number): void {
|
|
86
|
+
this.ensure(4);
|
|
87
|
+
this.view.setFloat32(this.pos, value, true);
|
|
88
|
+
this.pos += 4;
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
double(value: number): void {
|
|
92
|
+
this.ensure(8);
|
|
93
|
+
this.view.setFloat64(this.pos, value, true);
|
|
94
|
+
this.pos += 8;
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
fixed64(value: bigint): void {
|
|
98
|
+
this.ensure(8);
|
|
99
|
+
this.view.setBigUint64(this.pos, value, true);
|
|
100
|
+
this.pos += 8;
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
sfixed64(value: bigint): void {
|
|
104
|
+
this.ensure(8);
|
|
105
|
+
this.view.setBigInt64(this.pos, value, true);
|
|
106
|
+
this.pos += 8;
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
bytes(value: Uint8Array): void {
|
|
110
|
+
this.uint32(value.length);
|
|
111
|
+
this.ensure(value.length);
|
|
112
|
+
this.buf.set(value, this.pos);
|
|
113
|
+
this.pos += value.length;
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
string(value: string): void {
|
|
117
|
+
const start = this.fork();
|
|
118
|
+
this.ensure(value.length * 3);
|
|
119
|
+
this.pos += encoder.encodeInto(value, this.buf.subarray(this.pos)).written ?? 0;
|
|
120
|
+
this.ldelim(start);
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
/** Starts a length-delimited value; finish it with {@link ldelim}. */
|
|
124
|
+
fork(): number {
|
|
125
|
+
this.ensure(1);
|
|
126
|
+
return this.pos++;
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
/** Writes the length of the value started at `start` by {@link fork}, moving the value when the length needs more than one byte. */
|
|
130
|
+
ldelim(start: number): void {
|
|
131
|
+
const length = this.pos - start - 1;
|
|
132
|
+
if (length < 128) {
|
|
133
|
+
this.buf[start] = length;
|
|
134
|
+
return;
|
|
135
|
+
}
|
|
136
|
+
let size = 1;
|
|
137
|
+
for (let v = length >>> 7; v > 0; v >>>= 7) {
|
|
138
|
+
size++;
|
|
139
|
+
}
|
|
140
|
+
this.ensure(size - 1);
|
|
141
|
+
const buf = this.buf;
|
|
142
|
+
buf.copyWithin(start + size, start + 1, this.pos);
|
|
143
|
+
this.pos += size - 1;
|
|
144
|
+
let p = start;
|
|
145
|
+
let v = length;
|
|
146
|
+
while (v > 127) {
|
|
147
|
+
buf[p++] = (v & 127) | 128;
|
|
148
|
+
v >>>= 7;
|
|
149
|
+
}
|
|
150
|
+
buf[p] = v;
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
skip(count: number): void {
|
|
154
|
+
this.ensure(count);
|
|
155
|
+
this.pos += count;
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
finish(): Uint8Array {
|
|
159
|
+
return this.buf.subarray(0, this.pos);
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
protected ensure(count: number): void {
|
|
163
|
+
if (this.pos + count > this.buf.length) {
|
|
164
|
+
const buf = new Uint8Array(Math.max(this.buf.length * 2, this.pos + count));
|
|
165
|
+
buf.set(this.buf.subarray(0, this.pos));
|
|
166
|
+
this.buf = buf;
|
|
167
|
+
this.view = new DataView(buf.buffer);
|
|
168
|
+
}
|
|
169
|
+
}
|
|
170
|
+
}
|