@trevrpc/trevrpc-js 0.2.2 → 1.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/README.md +89 -47
- package/bin/protoc-gen-trevrpc-js.js +1 -1
- package/package.json +11 -41
- package/src/bytes.js +10 -0
- package/src/call.js +287 -0
- package/src/channel.js +131 -296
- package/src/client.js +203 -1292
- package/src/framing.js +125 -519
- package/src/generator.js +26 -103
- package/src/index.d.ts +79 -452
- package/src/index.js +4 -3
- package/src/status.js +24 -70
- package/src/wire.js +4 -59
- package/examples/greeter/README.md +0 -35
- package/examples/greeter/client.js +0 -172
- package/examples/greeter/greeter.node.trevrpc.d.ts +0 -53
- package/examples/greeter/greeter.node.trevrpc.js +0 -9
- package/examples/greeter/greeter.proto +0 -18
- package/examples/greeter/greeter.trevrpc.d.ts +0 -55
- package/examples/greeter/greeter.trevrpc.js +0 -103
- package/examples/greeter/index.html +0 -140
- package/examples/greeter/static-server.js +0 -168
- package/src/advanced.d.ts +0 -29
- package/src/advanced.js +0 -1
- package/src/browser-channel.js +0 -49
- package/src/connect.js +0 -6
- package/src/connect.node.js +0 -6
- package/src/index.node.d.ts +0 -10
- package/src/index.node.js +0 -3
- package/src/native-loader.js +0 -118
- package/src/node-advanced.d.ts +0 -27
- package/src/node-advanced.js +0 -1
- package/src/node-generated.d.ts +0 -24
- package/src/node-generated.js +0 -126
- package/src/node-index.d.ts +0 -1
- package/src/node-index.js +0 -7
- package/src/node.d.ts +0 -298
- package/src/node.js +0 -1456
- package/src/runtime.js +0 -66
- package/src/server-response.js +0 -49
- package/src/timer.js +0 -29
- package/src/webtransport.js +0 -639
package/src/index.d.ts
CHANGED
|
@@ -1,7 +1,10 @@
|
|
|
1
|
-
import type {
|
|
1
|
+
import type { INamespace, Root } from "protobufjs";
|
|
2
2
|
|
|
3
3
|
export const protobuf: typeof import("protobufjs");
|
|
4
4
|
|
|
5
|
+
/** Creates a protobuf root from a JSON namespace definition. */
|
|
6
|
+
export function createRoot(json: INamespace): Root;
|
|
7
|
+
|
|
5
8
|
export const Code: Readonly<{
|
|
6
9
|
Ok: 0;
|
|
7
10
|
Cancelled: 1;
|
|
@@ -24,118 +27,49 @@ export const Code: Readonly<{
|
|
|
24
27
|
|
|
25
28
|
export type StatusCode = (typeof Code)[keyof typeof Code];
|
|
26
29
|
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
+
/** Returns the canonical status code name, such as "NotFound". */
|
|
31
|
+
export function codeName(code: number): string;
|
|
32
|
+
|
|
33
|
+
export type MetadataValue = string | Uint8Array | ArrayBuffer | ArrayBufferView | readonly number[];
|
|
34
|
+
export type MetadataInput = Record<string, MetadataValue> | ReadonlyMap<string, MetadataValue>;
|
|
35
|
+
export type Metadata = Record<string, Uint8Array>;
|
|
30
36
|
|
|
31
37
|
/** Error carrying a TrevRPC status code, message, and metadata. */
|
|
32
38
|
export class TrevRpcError extends Error {
|
|
33
39
|
readonly code: StatusCode;
|
|
34
40
|
readonly statusMessage: string;
|
|
35
41
|
readonly metadata: Metadata;
|
|
36
|
-
readonly nativeCode?: number;
|
|
37
|
-
override readonly cause?: unknown;
|
|
38
42
|
|
|
39
|
-
|
|
40
|
-
constructor(code: number, message?: string, metadata?: Metadata, options?: TrevRpcErrorOptions);
|
|
43
|
+
constructor(code: number, message?: string, metadata?: Metadata, options?: ErrorOptions);
|
|
41
44
|
}
|
|
42
45
|
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
/**
|
|
49
|
-
|
|
46
|
+
export interface CallOptions {
|
|
47
|
+
/** Deadline for the whole call, from the start of the request to its final status. */
|
|
48
|
+
timeoutMs?: number;
|
|
49
|
+
/** Largest frame accepted or sent, in bytes. Defaults to 4 MiB. */
|
|
50
|
+
maxFrameSize?: number;
|
|
51
|
+
/** Request metadata. Keys are lowercase ASCII; the `trevrpc-` prefix is reserved. */
|
|
52
|
+
metadata?: MetadataInput;
|
|
53
|
+
/** Cancels the call. */
|
|
54
|
+
signal?: AbortSignal;
|
|
50
55
|
}
|
|
51
56
|
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
/** Creates a TrevRPC status error. */
|
|
57
|
-
export function statusError(code: number, message?: string, metadata?: Metadata): TrevRpcError;
|
|
58
|
-
/** Builds a status error from an RPC response. */
|
|
59
|
-
export function statusFromResponse(response?: RpcResponseMessage | null): TrevRpcError;
|
|
60
|
-
/** Reports whether a status-like value is OK. */
|
|
61
|
-
export function isOkStatus(status?: { code?: number } | null): boolean;
|
|
62
|
-
/** Creates an internal error status. */
|
|
63
|
-
export function internal(message: string): TrevRpcError;
|
|
64
|
-
/** Creates an invalid-argument status. */
|
|
65
|
-
export function invalidArgument(message: string): TrevRpcError;
|
|
66
|
-
/** Creates a deadline-exceeded status. */
|
|
67
|
-
export function deadlineExceeded(message: string): TrevRpcError;
|
|
68
|
-
/** Creates an unavailable status. */
|
|
69
|
-
export function unavailable(message: string): TrevRpcError;
|
|
70
|
-
/** Creates a resource-exhausted status. */
|
|
71
|
-
export function resourceExhausted(message: string): TrevRpcError;
|
|
72
|
-
/** Creates a cancelled status. */
|
|
73
|
-
export function cancelled(message: string): TrevRpcError;
|
|
74
|
-
|
|
75
|
-
export const MaxMetadataEntries: 64;
|
|
76
|
-
export const MaxMetadataKeyLen: 128;
|
|
77
|
-
export const MaxMetadataValueLen: number;
|
|
78
|
-
export const MaxMetadataTotalSize: number;
|
|
79
|
-
export const ReservedMetadataPrefix: "trevrpc-";
|
|
80
|
-
|
|
81
|
-
export type MetadataValue = string | Uint8Array | ArrayBuffer | ArrayBufferView | readonly number[];
|
|
82
|
-
export type MetadataInput = Record<string, MetadataValue> | ReadonlyMap<string, MetadataValue>;
|
|
83
|
-
export type Metadata = Record<string, Uint8Array>;
|
|
84
|
-
|
|
85
|
-
/** Converts a metadata value into bytes. */
|
|
86
|
-
export function metadataValueToBytes(value: MetadataValue): Uint8Array;
|
|
87
|
-
/** Normalizes a metadata key to lowercase ASCII. */
|
|
88
|
-
export function normalizeMetadataKey(key: string): string;
|
|
89
|
-
/** Normalizes metadata keys and converts metadata values to bytes. */
|
|
90
|
-
export function normalizeMetadata(metadata?: MetadataInput): Metadata;
|
|
91
|
-
/** Validates metadata key syntax, value sizes, and total metadata limits. */
|
|
92
|
-
export function validateMetadata(metadata?: MetadataInput): void;
|
|
57
|
+
export interface TransportStream {
|
|
58
|
+
readable: ReadableStream<Uint8Array>;
|
|
59
|
+
writable: WritableStream<Uint8Array>;
|
|
60
|
+
}
|
|
93
61
|
|
|
94
|
-
|
|
95
|
-
export
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
ServerStreaming: 2;
|
|
99
|
-
BidirectionalStreaming: 3;
|
|
100
|
-
}>;
|
|
101
|
-
export const RpcStreamFrameKind: Readonly<{
|
|
102
|
-
Message: 0;
|
|
103
|
-
Status: 1;
|
|
104
|
-
}>;
|
|
62
|
+
/** Anything that opens a bidirectional byte stream per call, such as a Channel or WebTransport. */
|
|
63
|
+
export interface Transport {
|
|
64
|
+
createBidirectionalStream(): Promise<TransportStream>;
|
|
65
|
+
}
|
|
105
66
|
|
|
106
|
-
export type RpcKindValue = (typeof RpcKind)[keyof typeof RpcKind];
|
|
107
|
-
export type RpcStreamFrameKindValue = (typeof RpcStreamFrameKind)[keyof typeof RpcStreamFrameKind];
|
|
108
67
|
export type RpcMethodKind =
|
|
109
68
|
| "unary"
|
|
110
69
|
| "clientStreaming"
|
|
111
70
|
| "serverStreaming"
|
|
112
71
|
| "bidirectionalStreaming";
|
|
113
72
|
|
|
114
|
-
export interface RpcRequestMessage {
|
|
115
|
-
service: string;
|
|
116
|
-
method: string;
|
|
117
|
-
body: Uint8Array;
|
|
118
|
-
metadata: Metadata;
|
|
119
|
-
kind: RpcKindValue;
|
|
120
|
-
version: number;
|
|
121
|
-
timeoutNanos?: number | string | Long;
|
|
122
|
-
}
|
|
123
|
-
|
|
124
|
-
export interface RpcResponseMessage {
|
|
125
|
-
status?: number;
|
|
126
|
-
message?: string;
|
|
127
|
-
body?: Uint8Array;
|
|
128
|
-
metadata?: Metadata;
|
|
129
|
-
}
|
|
130
|
-
|
|
131
|
-
export interface RpcStreamFrameMessage {
|
|
132
|
-
kind: RpcStreamFrameKindValue;
|
|
133
|
-
status?: number;
|
|
134
|
-
message?: string;
|
|
135
|
-
body?: Uint8Array;
|
|
136
|
-
metadata?: Metadata;
|
|
137
|
-
}
|
|
138
|
-
|
|
139
73
|
export interface RpcMethodDescriptor {
|
|
140
74
|
name: string;
|
|
141
75
|
kind: RpcMethodKind;
|
|
@@ -143,143 +77,11 @@ export interface RpcMethodDescriptor {
|
|
|
143
77
|
outputType: string;
|
|
144
78
|
}
|
|
145
79
|
|
|
146
|
-
export interface RpcServiceDescriptor
|
|
147
|
-
TMethods extends Record<string, RpcMethodDescriptor> = Record<string, RpcMethodDescriptor>,
|
|
148
|
-
> {
|
|
80
|
+
export interface RpcServiceDescriptor {
|
|
149
81
|
name: string;
|
|
150
82
|
fullName: string;
|
|
151
83
|
exportName: string;
|
|
152
|
-
methods:
|
|
153
|
-
}
|
|
154
|
-
|
|
155
|
-
export const wireRoot: Root;
|
|
156
|
-
export const RpcRequest: Type;
|
|
157
|
-
export const RpcResponse: Type;
|
|
158
|
-
export const RpcStreamFrame: Type;
|
|
159
|
-
|
|
160
|
-
/** Creates a protobuf root from a JSON namespace definition. */
|
|
161
|
-
export function createRoot(json: import("protobufjs").INamespace): Root;
|
|
162
|
-
/** Creates a stream message frame carrying a protobuf body. */
|
|
163
|
-
export function messageFrame(body: Uint8Array): RpcStreamFrameMessage;
|
|
164
|
-
|
|
165
|
-
export const DefaultMaxFrameSize: number;
|
|
166
|
-
|
|
167
|
-
/** Encodes a protobuf message body. */
|
|
168
|
-
export function marshalMessage<TRequest extends object>(
|
|
169
|
-
messageType: Type,
|
|
170
|
-
message: TRequest,
|
|
171
|
-
): Uint8Array;
|
|
172
|
-
/** Decodes a protobuf message body. */
|
|
173
|
-
export function unmarshalMessage<TResponse = Message<Record<string, unknown>>>(
|
|
174
|
-
messageType: Type,
|
|
175
|
-
body: Uint8Array,
|
|
176
|
-
): TResponse;
|
|
177
|
-
/** Encodes a protobuf message into a length-prefixed TrevRPC frame. */
|
|
178
|
-
export function encodeFrame<TMessage extends object>(
|
|
179
|
-
messageType: Type,
|
|
180
|
-
message: TMessage,
|
|
181
|
-
maxFrameSize?: number,
|
|
182
|
-
): Uint8Array;
|
|
183
|
-
/** Encodes one streaming message frame carrying an already-encoded protobuf body. */
|
|
184
|
-
export function encodeMessageStreamFrame(body: Uint8Array, maxFrameSize?: number): Uint8Array;
|
|
185
|
-
/** Encodes multiple streaming message frames into one contiguous write buffer. */
|
|
186
|
-
export function encodeMessageStreamFrames(
|
|
187
|
-
bodies: Iterable<Uint8Array>,
|
|
188
|
-
maxFrameSize?: number,
|
|
189
|
-
): Uint8Array;
|
|
190
|
-
/** Decodes a protobuf message from a TrevRPC frame body. */
|
|
191
|
-
export function decodeFrame<TMessage = Message<Record<string, unknown>>>(
|
|
192
|
-
messageType: Type,
|
|
193
|
-
body: Uint8Array,
|
|
194
|
-
): TMessage;
|
|
195
|
-
/** Decodes a streaming RPC frame body. */
|
|
196
|
-
export function decodeStreamFrameBody(body: Uint8Array): RpcStreamFrameMessage;
|
|
197
|
-
/** Writes one length-prefixed protobuf frame. */
|
|
198
|
-
export function writeFrame<TMessage extends object>(
|
|
199
|
-
writer: WritableStreamDefaultWriter<Uint8Array>,
|
|
200
|
-
messageType: Type,
|
|
201
|
-
message: TMessage,
|
|
202
|
-
maxFrameSize?: number,
|
|
203
|
-
): Promise<void>;
|
|
204
|
-
/** Writes multiple streaming message frames in one write. */
|
|
205
|
-
export function writeMessageStreamFrames(
|
|
206
|
-
writer: WritableStreamDefaultWriter<Uint8Array>,
|
|
207
|
-
bodies: Iterable<Uint8Array>,
|
|
208
|
-
maxFrameSize?: number,
|
|
209
|
-
): Promise<void>;
|
|
210
|
-
/** Decodes and validates the body length stored in a TrevRPC frame header. */
|
|
211
|
-
export function frameBodyLength(header: Uint8Array, maxFrameSize?: number): number;
|
|
212
|
-
|
|
213
|
-
/** Reads length-prefixed protobuf frames from a byte stream reader. */
|
|
214
|
-
export class FrameReader {
|
|
215
|
-
/** Creates a frame reader over a stream reader. */
|
|
216
|
-
constructor(reader: ReadableStreamDefaultReader<Uint8Array>);
|
|
217
|
-
|
|
218
|
-
/** Reads and decodes one frame. */
|
|
219
|
-
readFrame<TMessage = Message<Record<string, unknown>>>(
|
|
220
|
-
messageType: Type,
|
|
221
|
-
maxFrameSize?: number,
|
|
222
|
-
): Promise<TMessage>;
|
|
223
|
-
/** Reads and decodes one frame, or returns null when already at EOF. */
|
|
224
|
-
readFrameOrEOF<TMessage = Message<Record<string, unknown>>>(
|
|
225
|
-
messageType: Type,
|
|
226
|
-
maxFrameSize?: number,
|
|
227
|
-
): Promise<TMessage | null>;
|
|
228
|
-
/** Reads and decodes one streaming RPC frame, or returns null when already at EOF. */
|
|
229
|
-
readStreamFrameOrEOF(maxFrameSize?: number): Promise<RpcStreamFrameMessage | null>;
|
|
230
|
-
/** Reads one streaming RPC frame, then drains complete frames already buffered. */
|
|
231
|
-
readStreamFrameBatchOrEOF(
|
|
232
|
-
maxBatch?: number,
|
|
233
|
-
maxFrameSize?: number,
|
|
234
|
-
): Promise<RpcStreamFrameMessage[] | null>;
|
|
235
|
-
/** Reads stream message body batches, with at most one terminal status. */
|
|
236
|
-
readStreamMessageBodyBatchOrEOF(
|
|
237
|
-
maxBatch?: number,
|
|
238
|
-
maxFrameSize?: number,
|
|
239
|
-
): Promise<{ bodies: Uint8Array[]; status: RpcStreamFrameMessage | null } | null>;
|
|
240
|
-
/** Reads one raw frame body, or returns null when already at EOF. */
|
|
241
|
-
readRawFrameBodyOrEOF(maxFrameSize?: number): Promise<Uint8Array | null>;
|
|
242
|
-
/** Reads exactly size bytes from the underlying reader. */
|
|
243
|
-
readExact(size: number, allowEofAtStart: boolean): Promise<Uint8Array | null>;
|
|
244
|
-
/** Reads a complete buffered frame body without waiting, or undefined. */
|
|
245
|
-
tryReadFrameBody(maxFrameSize?: number): Uint8Array | undefined;
|
|
246
|
-
/** Removes size bytes from the buffered data. */
|
|
247
|
-
consume(size: number): Uint8Array;
|
|
248
|
-
/** Copies size bytes from the buffered data without removing them. */
|
|
249
|
-
peek(size: number): Uint8Array;
|
|
250
|
-
}
|
|
251
|
-
|
|
252
|
-
export interface CallOptions {
|
|
253
|
-
timeoutMs?: number;
|
|
254
|
-
maxFrameSize?: number;
|
|
255
|
-
maxResponseBodySize?: number;
|
|
256
|
-
maxResponseMessages?: number;
|
|
257
|
-
maxResponseStreamBodySize?: number;
|
|
258
|
-
streamIdleTimeoutMs?: number;
|
|
259
|
-
metadata?: MetadataInput;
|
|
260
|
-
signal?: AbortSignal;
|
|
261
|
-
}
|
|
262
|
-
|
|
263
|
-
export interface ResolvedCallOptions {
|
|
264
|
-
timeoutMs?: number;
|
|
265
|
-
maxFrameSize?: number;
|
|
266
|
-
maxResponseBodySize: number;
|
|
267
|
-
maxResponseMessages: number;
|
|
268
|
-
maxResponseStreamBodySize: number;
|
|
269
|
-
streamIdleTimeoutMs: number;
|
|
270
|
-
metadata: Metadata;
|
|
271
|
-
signal?: AbortSignal;
|
|
272
|
-
}
|
|
273
|
-
|
|
274
|
-
export interface Transport {
|
|
275
|
-
/** Sends a unary RPC request and returns its response. */
|
|
276
|
-
call(request: RpcRequestMessage, options?: ResolvedCallOptions): Promise<RpcResponseMessage>;
|
|
277
|
-
/** Sends a streaming RPC request and returns response frames. */
|
|
278
|
-
streamingCall(
|
|
279
|
-
request: RpcRequestMessage,
|
|
280
|
-
requestBody: AsyncIterable<Uint8Array>,
|
|
281
|
-
options?: ResolvedCallOptions,
|
|
282
|
-
): Promise<AsyncIterable<RpcStreamFrameMessage>>;
|
|
84
|
+
methods: Record<string, RpcMethodDescriptor>;
|
|
283
85
|
}
|
|
284
86
|
|
|
285
87
|
export interface UnaryResponse<TResponse> {
|
|
@@ -287,80 +89,45 @@ export interface UnaryResponse<TResponse> {
|
|
|
287
89
|
readonly metadata: Metadata;
|
|
288
90
|
}
|
|
289
91
|
|
|
92
|
+
/** The final status of a call that ended with OK. */
|
|
290
93
|
export interface StreamStatus {
|
|
291
94
|
readonly code: StatusCode;
|
|
292
95
|
readonly message: string;
|
|
293
96
|
readonly metadata: Metadata;
|
|
294
97
|
}
|
|
295
98
|
|
|
296
|
-
|
|
99
|
+
/**
|
|
100
|
+
* Responses of a server-streaming call. Iteration ends after the final OK status and throws a
|
|
101
|
+
* TrevRpcError for any other outcome. Leaving the loop early cancels the call.
|
|
102
|
+
*/
|
|
103
|
+
export interface ResponseStream<TResponse> extends AsyncIterable<TResponse> {
|
|
104
|
+
/** Settles with the final status, or rejects with the error that ended the call. */
|
|
297
105
|
readonly status: Promise<StreamStatus>;
|
|
106
|
+
/** Cancels the call unless it already finished. */
|
|
298
107
|
close(reason?: unknown): Promise<void>;
|
|
299
108
|
}
|
|
300
109
|
|
|
301
|
-
|
|
302
|
-
declare const streamingServerResponseBrand: unique symbol;
|
|
303
|
-
|
|
304
|
-
export interface UnaryServerResponse<T> {
|
|
305
|
-
/** Opaque brand; construct responses with createUnaryResponse(). */
|
|
306
|
-
readonly [unaryServerResponseBrand]: true;
|
|
307
|
-
readonly message: T;
|
|
308
|
-
readonly metadata: Metadata;
|
|
309
|
-
}
|
|
310
|
-
|
|
311
|
-
export interface StreamingServerResponse<T> {
|
|
312
|
-
/** Opaque brand; construct responses with createStreamingResponse(). */
|
|
313
|
-
readonly [streamingServerResponseBrand]: true;
|
|
314
|
-
readonly messages: Iterable<T> | AsyncIterable<T>;
|
|
315
|
-
readonly status: StreamStatus;
|
|
316
|
-
}
|
|
317
|
-
|
|
318
|
-
/** Creates an opaque unary generated-server response envelope. */
|
|
319
|
-
export function createUnaryResponse<T>(
|
|
320
|
-
message: T,
|
|
321
|
-
metadata?: MetadataInput,
|
|
322
|
-
): UnaryServerResponse<T>;
|
|
323
|
-
|
|
324
|
-
/** Creates an opaque streaming generated-server response envelope. */
|
|
325
|
-
export function createStreamingResponse<T>(
|
|
326
|
-
messages: Iterable<T> | AsyncIterable<T>,
|
|
327
|
-
status?: {
|
|
328
|
-
code?: StatusCode;
|
|
329
|
-
message?: string;
|
|
330
|
-
metadata?: MetadataInput;
|
|
331
|
-
},
|
|
332
|
-
): StreamingServerResponse<T>;
|
|
333
|
-
|
|
334
|
-
export interface ClientStreamingCall<TRequest extends object, TResponse> {
|
|
110
|
+
export interface ClientStreamingCall<TRequest, TResponse> {
|
|
335
111
|
/** Sends one request message. */
|
|
336
112
|
send(request: TRequest): Promise<void>;
|
|
337
|
-
/** Sends multiple request messages. */
|
|
338
|
-
sendMany(requests: Iterable<TRequest>): Promise<void>;
|
|
339
|
-
/** Closes the request stream. */
|
|
340
|
-
closeSend(): Promise<void>;
|
|
341
113
|
/** Closes the request stream and returns the final response. */
|
|
342
114
|
closeAndRecv(): Promise<TResponse>;
|
|
343
|
-
/** Closes the request stream and returns the final response
|
|
115
|
+
/** Closes the request stream and returns the final response with its metadata. */
|
|
344
116
|
closeAndRecvWithResponse(): Promise<UnaryResponse<TResponse>>;
|
|
345
|
-
/**
|
|
117
|
+
/** Cancels the call unless it already finished. */
|
|
346
118
|
close(reason?: unknown): Promise<void>;
|
|
347
119
|
}
|
|
348
120
|
|
|
349
|
-
export interface BidirectionalStreamingCall<
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
> extends AsyncIterable<TResponse> {
|
|
353
|
-
/** Resolves to the terminal stream status after completion. */
|
|
354
|
-
status: Promise<StreamStatus>;
|
|
121
|
+
export interface BidirectionalStreamingCall<TRequest, TResponse> extends AsyncIterable<TResponse> {
|
|
122
|
+
/** Settles with the final status, or rejects with the error that ended the call. */
|
|
123
|
+
readonly status: Promise<StreamStatus>;
|
|
355
124
|
/** Sends one request message. */
|
|
356
125
|
send(request: TRequest): Promise<void>;
|
|
357
|
-
/**
|
|
358
|
-
sendMany(requests: Iterable<TRequest>): Promise<void>;
|
|
359
|
-
/** Receives one response message, or undefined after the response stream completes. */
|
|
126
|
+
/** Receives the next response message, or undefined after the final OK status. */
|
|
360
127
|
recv(): Promise<TResponse | undefined>;
|
|
361
128
|
/** Closes the request stream while keeping the response stream readable. */
|
|
362
129
|
closeSend(): Promise<void>;
|
|
363
|
-
/**
|
|
130
|
+
/** Cancels the call unless it already finished. */
|
|
364
131
|
close(reason?: unknown): Promise<void>;
|
|
365
132
|
}
|
|
366
133
|
|
|
@@ -369,73 +136,6 @@ export type ServiceClient = Record<
|
|
|
369
136
|
(requestOrOptions?: unknown, options?: CallOptions) => Promise<unknown>
|
|
370
137
|
>;
|
|
371
138
|
|
|
372
|
-
/** Returns the default client call options. */
|
|
373
|
-
export function defaultCallOptions(): ResolvedCallOptions;
|
|
374
|
-
/** Merges call options and normalizes metadata. */
|
|
375
|
-
export function mergeCallOptions(base?: CallOptions, override?: CallOptions): ResolvedCallOptions;
|
|
376
|
-
/** Calls a unary RPC and decodes the protobuf response. */
|
|
377
|
-
export function unary<
|
|
378
|
-
TRequest extends object = Record<string, unknown>,
|
|
379
|
-
TResponse = Message<Record<string, unknown>>,
|
|
380
|
-
>(
|
|
381
|
-
transport: Transport,
|
|
382
|
-
service: string,
|
|
383
|
-
method: string,
|
|
384
|
-
requestType: Type,
|
|
385
|
-
responseType: Type,
|
|
386
|
-
request: TRequest,
|
|
387
|
-
options?: CallOptions,
|
|
388
|
-
): Promise<TResponse>;
|
|
389
|
-
/** Calls a unary RPC and returns the decoded response envelope. */
|
|
390
|
-
export function unaryWithResponse<
|
|
391
|
-
TRequest extends object = Record<string, unknown>,
|
|
392
|
-
TResponse = Message<Record<string, unknown>>,
|
|
393
|
-
>(
|
|
394
|
-
transport: Transport,
|
|
395
|
-
service: string,
|
|
396
|
-
method: string,
|
|
397
|
-
requestType: Type,
|
|
398
|
-
responseType: Type,
|
|
399
|
-
request: TRequest,
|
|
400
|
-
options?: CallOptions,
|
|
401
|
-
): Promise<UnaryResponse<TResponse>>;
|
|
402
|
-
/** Calls a server-streaming RPC and returns a decoded response stream. */
|
|
403
|
-
export function serverStreaming<
|
|
404
|
-
TRequest extends object = Record<string, unknown>,
|
|
405
|
-
TResponse = Message<Record<string, unknown>>,
|
|
406
|
-
>(
|
|
407
|
-
transport: Transport,
|
|
408
|
-
service: string,
|
|
409
|
-
method: string,
|
|
410
|
-
requestType: Type,
|
|
411
|
-
responseType: Type,
|
|
412
|
-
request: TRequest,
|
|
413
|
-
options?: CallOptions,
|
|
414
|
-
): Promise<ResponseAsyncIterable<TResponse>>;
|
|
415
|
-
/** Calls a client-streaming RPC and returns a sendable call object. */
|
|
416
|
-
export function clientStreaming<
|
|
417
|
-
TRequest extends object = Record<string, unknown>,
|
|
418
|
-
TResponse = Message<Record<string, unknown>>,
|
|
419
|
-
>(
|
|
420
|
-
transport: Transport,
|
|
421
|
-
service: string,
|
|
422
|
-
method: string,
|
|
423
|
-
requestType: Type,
|
|
424
|
-
responseType: Type,
|
|
425
|
-
options?: CallOptions,
|
|
426
|
-
): Promise<ClientStreamingCall<TRequest, TResponse>>;
|
|
427
|
-
/** Calls a bidirectional-streaming RPC and returns a sendable call object. */
|
|
428
|
-
export function bidirectionalStreaming<
|
|
429
|
-
TRequest extends object = Record<string, unknown>,
|
|
430
|
-
TResponse = Message<Record<string, unknown>>,
|
|
431
|
-
>(
|
|
432
|
-
transport: Transport,
|
|
433
|
-
service: string,
|
|
434
|
-
method: string,
|
|
435
|
-
requestType: Type,
|
|
436
|
-
responseType: Type,
|
|
437
|
-
options?: CallOptions,
|
|
438
|
-
): Promise<BidirectionalStreamingCall<TRequest, TResponse>>;
|
|
439
139
|
/** Creates a service client from a generated service descriptor. */
|
|
440
140
|
export function createServiceClient<TClient extends ServiceClient = ServiceClient>(
|
|
441
141
|
transport: Transport,
|
|
@@ -444,134 +144,61 @@ export function createServiceClient<TClient extends ServiceClient = ServiceClien
|
|
|
444
144
|
options?: CallOptions,
|
|
445
145
|
): TClient;
|
|
446
146
|
|
|
447
|
-
export interface WebTransportCloseInfoLike {
|
|
448
|
-
closeCode?: number;
|
|
449
|
-
reason?: string;
|
|
450
|
-
}
|
|
451
|
-
|
|
452
|
-
export interface WebTransportBidirectionalStreamLike {
|
|
453
|
-
readable: ReadableStream<Uint8Array>;
|
|
454
|
-
writable: WritableStream<Uint8Array>;
|
|
455
|
-
}
|
|
456
|
-
|
|
457
|
-
export interface WebTransportSessionLike {
|
|
458
|
-
ready: Promise<void>;
|
|
459
|
-
closed?: Promise<WebTransportCloseInfoLike>;
|
|
460
|
-
close?(closeInfo?: WebTransportCloseInfoLike): void;
|
|
461
|
-
createBidirectionalStream?(): Promise<WebTransportBidirectionalStreamLike>;
|
|
462
|
-
}
|
|
463
|
-
|
|
464
|
-
export interface WebTransportConstructorLike {
|
|
465
|
-
new (url: string | URL, options?: BrowserWebTransportOptions): WebTransportSessionLike;
|
|
466
|
-
}
|
|
467
|
-
|
|
468
147
|
export interface WebTransportCertificateHash {
|
|
469
148
|
algorithm: string;
|
|
470
149
|
value: ArrayBuffer | ArrayBufferView;
|
|
471
150
|
}
|
|
472
151
|
|
|
473
|
-
export interface
|
|
152
|
+
export interface WebTransportSessionLike extends Transport {
|
|
153
|
+
readonly ready: Promise<unknown>;
|
|
154
|
+
readonly closed: Promise<unknown>;
|
|
155
|
+
close(closeInfo?: { closeCode?: number; reason?: string }): void;
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
export interface ConnectOptions {
|
|
474
159
|
allowPooling?: boolean;
|
|
475
160
|
congestionControl?: "default" | "low-latency" | "throughput";
|
|
476
161
|
requireUnreliable?: boolean;
|
|
162
|
+
/** Trusts a self-signed ECDSA certificate (at most 14 days old) by its SHA-256 hash. */
|
|
477
163
|
serverCertificateHashes?: readonly WebTransportCertificateHash[];
|
|
164
|
+
/** WebTransport implementation to use instead of the global one. */
|
|
165
|
+
WebTransport?: new (url: string | URL, options?: object) => WebTransportSessionLike;
|
|
166
|
+
/** Cancels the initial connection attempt only. */
|
|
167
|
+
signal?: AbortSignal;
|
|
168
|
+
/** Receives channel state transitions. */
|
|
169
|
+
onStateChange?(change: ChannelStateChange): void;
|
|
478
170
|
}
|
|
479
171
|
|
|
480
|
-
export
|
|
481
|
-
WebTransport?: WebTransportConstructorLike;
|
|
482
|
-
maxFrameSize?: number;
|
|
483
|
-
}
|
|
484
|
-
|
|
485
|
-
export type ChannelState = "connecting" | "ready" | "reconnecting" | "closed";
|
|
172
|
+
export type ChannelState = "ready" | "reconnecting" | "closed";
|
|
486
173
|
|
|
487
|
-
export interface
|
|
174
|
+
export interface ChannelStateChange {
|
|
488
175
|
state: ChannelState;
|
|
489
176
|
previousState: ChannelState;
|
|
490
|
-
|
|
177
|
+
/** Reconnect attempt number while reconnecting, starting at one. */
|
|
491
178
|
attempt?: number;
|
|
179
|
+
/** Delay before the reconnect attempt. */
|
|
492
180
|
delayMs?: number;
|
|
181
|
+
/** Why the session was lost or the previous attempt failed. */
|
|
493
182
|
error?: unknown;
|
|
494
183
|
}
|
|
495
184
|
|
|
496
|
-
|
|
497
|
-
|
|
498
|
-
};
|
|
185
|
+
/** Opens a channel to a WebTransport server and waits for its session to be ready. */
|
|
186
|
+
export function connect(url: string | URL, options?: ConnectOptions): Promise<Channel>;
|
|
499
187
|
|
|
500
|
-
|
|
501
|
-
|
|
502
|
-
|
|
503
|
-
|
|
188
|
+
/**
|
|
189
|
+
* A WebTransport session that reconnects in the background after it is lost. Calls fail fast
|
|
190
|
+
* with Unavailable while it reconnects and are never replayed.
|
|
191
|
+
*/
|
|
192
|
+
export class Channel implements Transport {
|
|
193
|
+
private constructor();
|
|
504
194
|
|
|
505
|
-
|
|
506
|
-
|
|
507
|
-
capture?: boolean;
|
|
508
|
-
}
|
|
509
|
-
interface TrevRpcAddEventListenerOptions extends TrevRpcEventListenerOptions {
|
|
510
|
-
once?: boolean;
|
|
511
|
-
passive?: boolean;
|
|
512
|
-
signal?: AbortSignal;
|
|
513
|
-
}
|
|
195
|
+
/** Opens a channel to a WebTransport server and waits for its session to be ready. */
|
|
196
|
+
static connect(url: string | URL, options?: ConnectOptions): Promise<Channel>;
|
|
514
197
|
|
|
515
|
-
export interface RpcChannel extends Transport, EventTarget {
|
|
516
|
-
readonly ready: boolean;
|
|
517
198
|
readonly state: ChannelState;
|
|
518
|
-
|
|
519
|
-
|
|
520
|
-
close(closeInfo?: WebTransportCloseInfoLike): void;
|
|
521
|
-
}
|
|
522
|
-
|
|
523
|
-
export interface BrowserChannelOptions extends WebTransportOptions, ChannelLifecycleOptions {
|
|
524
|
-
/** Bounds initial readiness only. Later reconnects continue until close(). */
|
|
525
|
-
timeoutMs?: number;
|
|
526
|
-
/** Cancels initial readiness only. Later reconnects continue until close(). */
|
|
527
|
-
signal?: AbortSignal;
|
|
528
|
-
}
|
|
529
|
-
|
|
530
|
-
/** Opens a browser channel and waits for its first ready generation. */
|
|
531
|
-
export function connect(url: string | URL, options?: BrowserChannelOptions): Promise<Channel>;
|
|
532
|
-
|
|
533
|
-
/** Application channel with background connection reconnection. */
|
|
534
|
-
export class Channel extends EventTarget implements RpcChannel {
|
|
535
|
-
readonly url: string | URL;
|
|
536
|
-
readonly options: Readonly<BrowserChannelOptions>;
|
|
537
|
-
readonly ready: boolean;
|
|
538
|
-
readonly state: ChannelState;
|
|
539
|
-
readonly generation: number;
|
|
540
|
-
|
|
541
|
-
constructor(url: string | URL, options?: BrowserChannelOptions);
|
|
542
|
-
|
|
543
|
-
/** Creates a channel and waits for its first ready generation. */
|
|
544
|
-
static connect(url: string | URL, options?: BrowserChannelOptions): Promise<Channel>;
|
|
545
|
-
/** Waits for the current or next connection generation to become ready. */
|
|
199
|
+
createBidirectionalStream(): Promise<TransportStream>;
|
|
200
|
+
/** Resolves once the channel is ready, immediately if it already is. */
|
|
546
201
|
waitUntilReady(): Promise<void>;
|
|
547
|
-
/**
|
|
548
|
-
|
|
549
|
-
/** Starts a streaming call on the current generation without resuming it later. */
|
|
550
|
-
streamingCall(
|
|
551
|
-
request: RpcRequestMessage,
|
|
552
|
-
requestBody: AsyncIterable<Uint8Array>,
|
|
553
|
-
options?: ResolvedCallOptions,
|
|
554
|
-
): Promise<AsyncIterableIterator<RpcStreamFrameMessage>>;
|
|
555
|
-
/** Stops reconnecting and closes the current generation. */
|
|
556
|
-
close(closeInfo?: WebTransportCloseInfoLike): void;
|
|
557
|
-
addEventListener(
|
|
558
|
-
type: "statechange" | ChannelState,
|
|
559
|
-
callback: (event: ChannelLifecycleEvent) => void,
|
|
560
|
-
options?: boolean | TrevRpcAddEventListenerOptions,
|
|
561
|
-
): void;
|
|
562
|
-
addEventListener(
|
|
563
|
-
type: string,
|
|
564
|
-
callback: TrevRpcEventListener | null,
|
|
565
|
-
options?: boolean | TrevRpcAddEventListenerOptions,
|
|
566
|
-
): void;
|
|
567
|
-
removeEventListener(
|
|
568
|
-
type: "statechange" | ChannelState,
|
|
569
|
-
callback: (event: ChannelLifecycleEvent) => void,
|
|
570
|
-
options?: boolean | TrevRpcEventListenerOptions,
|
|
571
|
-
): void;
|
|
572
|
-
removeEventListener(
|
|
573
|
-
type: string,
|
|
574
|
-
callback: TrevRpcEventListener | null,
|
|
575
|
-
options?: boolean | TrevRpcEventListenerOptions,
|
|
576
|
-
): void;
|
|
202
|
+
/** Stops reconnecting and closes the current session. */
|
|
203
|
+
close(closeInfo?: { closeCode?: number; reason?: string }): void;
|
|
577
204
|
}
|
package/src/index.js
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
-
export
|
|
2
|
-
export {
|
|
3
|
-
export {
|
|
1
|
+
export { Channel, connect } from "./channel.js";
|
|
2
|
+
export { createServiceClient } from "./client.js";
|
|
3
|
+
export { Code, TrevRpcError, codeName } from "./status.js";
|
|
4
|
+
export { createRoot, protobuf } from "./wire.js";
|