@zmdb/transport-grpc 1.0.0-beta.1
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 +674 -0
- package/README.md +83 -0
- package/dist/index.d.ts +3 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +3 -0
- package/dist/index.js.map +1 -0
- package/dist/runtime.d.ts +16 -0
- package/dist/runtime.d.ts.map +1 -0
- package/dist/runtime.js +711 -0
- package/dist/runtime.js.map +1 -0
- package/dist/types.d.ts +120 -0
- package/dist/types.d.ts.map +1 -0
- package/dist/types.js +12 -0
- package/dist/types.js.map +1 -0
- package/package.json +52 -0
- package/src/index.ts +22 -0
- package/src/runtime.ts +947 -0
- package/src/types.ts +157 -0
package/src/types.ts
ADDED
|
@@ -0,0 +1,157 @@
|
|
|
1
|
+
import type { GrpcLoadedService, GrpcMethodDef, GrpcServiceDef } from '@zmdb/protobuf';
|
|
2
|
+
|
|
3
|
+
/** Text and binary gRPC metadata are kept in separate maps. */
|
|
4
|
+
export interface GrpcMetadata {
|
|
5
|
+
readonly headers: Readonly<Record<string, string>>;
|
|
6
|
+
readonly binaryHeaders: Readonly<Record<string, Uint8Array>>;
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
/** Required validation boundary for metadata exposed to application code. */
|
|
10
|
+
export type GrpcMetadataValidator = (metadata: GrpcMetadata) => GrpcMetadata;
|
|
11
|
+
|
|
12
|
+
/** The application-facing context for one gRPC handler invocation. */
|
|
13
|
+
export interface GrpcCall<T> {
|
|
14
|
+
readonly kind: 'grpc';
|
|
15
|
+
readonly service: string;
|
|
16
|
+
readonly method: string;
|
|
17
|
+
readonly payload: T;
|
|
18
|
+
readonly headers: Readonly<Record<string, string>>;
|
|
19
|
+
readonly binaryHeaders: Readonly<Record<string, Uint8Array>>;
|
|
20
|
+
readonly peer: string;
|
|
21
|
+
readonly signal: AbortSignal;
|
|
22
|
+
remainingMs(): number;
|
|
23
|
+
setTrailer(key: string, value: string): void;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
/** Handler shape selected from the request/response streaming flags. */
|
|
27
|
+
export type GrpcHandler<D extends GrpcMethodDef> = D extends { requestStream: true }
|
|
28
|
+
? D extends { responseStream: true }
|
|
29
|
+
? (call: GrpcCall<AsyncIterable<D['request']>>) => AsyncIterable<D['response']>
|
|
30
|
+
: (call: GrpcCall<AsyncIterable<D['request']>>) => Promise<D['response']>
|
|
31
|
+
: D extends { responseStream: true }
|
|
32
|
+
? (call: GrpcCall<D['request']>) => AsyncIterable<D['response']>
|
|
33
|
+
: (call: GrpcCall<D['request']>) => Promise<D['response']>;
|
|
34
|
+
|
|
35
|
+
/** A total handler map: every declared service method must be implemented. */
|
|
36
|
+
export type GrpcHandlers<S extends GrpcServiceDef> = {
|
|
37
|
+
readonly [M in keyof S]: GrpcHandler<S[M]>;
|
|
38
|
+
};
|
|
39
|
+
|
|
40
|
+
export type GrpcStatus =
|
|
41
|
+
| 'OK'
|
|
42
|
+
| 'CANCELLED'
|
|
43
|
+
| 'INVALID_ARGUMENT'
|
|
44
|
+
| 'DEADLINE_EXCEEDED'
|
|
45
|
+
| 'NOT_FOUND'
|
|
46
|
+
| 'ALREADY_EXISTS'
|
|
47
|
+
| 'PERMISSION_DENIED'
|
|
48
|
+
| 'RESOURCE_EXHAUSTED'
|
|
49
|
+
| 'FAILED_PRECONDITION'
|
|
50
|
+
| 'UNIMPLEMENTED'
|
|
51
|
+
| 'INTERNAL'
|
|
52
|
+
| 'UNAVAILABLE'
|
|
53
|
+
| 'UNAUTHENTICATED';
|
|
54
|
+
|
|
55
|
+
/** A deliberately safe error that may cross the gRPC boundary. */
|
|
56
|
+
export class GrpcError extends Error {
|
|
57
|
+
readonly status: GrpcStatus;
|
|
58
|
+
readonly details: string;
|
|
59
|
+
|
|
60
|
+
constructor(status: GrpcStatus, details: string) {
|
|
61
|
+
super(details);
|
|
62
|
+
this.name = 'GrpcError';
|
|
63
|
+
this.status = status;
|
|
64
|
+
this.details = details;
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
/** The private failure sent to the required observation sink. */
|
|
69
|
+
export interface GrpcFailure {
|
|
70
|
+
readonly service: string;
|
|
71
|
+
readonly method: string;
|
|
72
|
+
readonly status: GrpcStatus;
|
|
73
|
+
readonly error: unknown;
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
/** A server identity certificate and private key. */
|
|
77
|
+
export interface GrpcKeyCertPair {
|
|
78
|
+
readonly privateKey: Uint8Array;
|
|
79
|
+
readonly certificateChain: Uint8Array;
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
/** TLS material accepted by a gRPC server. */
|
|
83
|
+
export interface GrpcServerTlsOptions {
|
|
84
|
+
readonly rootCertificates?: Uint8Array;
|
|
85
|
+
readonly keyCertPairs: readonly GrpcKeyCertPair[];
|
|
86
|
+
readonly checkClientCertificate: boolean;
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
/** TLS material accepted by a gRPC client. */
|
|
90
|
+
export interface GrpcClientTlsOptions {
|
|
91
|
+
readonly rootCertificates?: Uint8Array;
|
|
92
|
+
readonly privateKey?: Uint8Array;
|
|
93
|
+
readonly certificateChain?: Uint8Array;
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
/** Compatibility name for code that shares a credentials helper. */
|
|
97
|
+
export type GrpcTlsOptions = GrpcServerTlsOptions | GrpcClientTlsOptions;
|
|
98
|
+
|
|
99
|
+
/**
|
|
100
|
+
* One bound service. Runtime state is owned by the binding object; the public
|
|
101
|
+
* surface exposes only its declared identity.
|
|
102
|
+
*/
|
|
103
|
+
export interface GrpcBinding {
|
|
104
|
+
readonly service: string;
|
|
105
|
+
readonly methods: readonly string[];
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
/**
|
|
109
|
+
* A typed build artifact plus the two runtime policies that cannot be inferred
|
|
110
|
+
* from a TypeScript declaration.
|
|
111
|
+
*/
|
|
112
|
+
export interface GrpcServiceSpec<S extends GrpcServiceDef> {
|
|
113
|
+
readonly definition: GrpcLoadedService<S>;
|
|
114
|
+
readonly validateMetadata: GrpcMetadataValidator;
|
|
115
|
+
readonly onError: (failure: GrpcFailure) => void;
|
|
116
|
+
readonly maxDurationMs?: number;
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
export interface GrpcServerOptions {
|
|
120
|
+
readonly address: string;
|
|
121
|
+
readonly bindings: readonly GrpcBinding[];
|
|
122
|
+
readonly credentials: 'insecure' | GrpcServerTlsOptions;
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
/** Per-call metadata, cancellation and deadline propagation. */
|
|
126
|
+
export interface GrpcClientCallOptions {
|
|
127
|
+
readonly deadlineMs?: number;
|
|
128
|
+
readonly signal?: AbortSignal;
|
|
129
|
+
readonly metadata?: GrpcMetadata;
|
|
130
|
+
readonly onMetadata?: (metadata: GrpcMetadata) => void;
|
|
131
|
+
readonly onTrailer?: (metadata: GrpcMetadata) => void;
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
/** Typed caller selected from the same four stream flags as the handler. */
|
|
135
|
+
export type GrpcCaller<D extends GrpcMethodDef> = D extends { requestStream: true }
|
|
136
|
+
? D extends { responseStream: true }
|
|
137
|
+
? (payload: AsyncIterable<D['request']>, options?: GrpcClientCallOptions) => AsyncIterable<D['response']>
|
|
138
|
+
: (payload: AsyncIterable<D['request']>, options?: GrpcClientCallOptions) => Promise<D['response']>
|
|
139
|
+
: D extends { responseStream: true }
|
|
140
|
+
? (payload: D['request'], options?: GrpcClientCallOptions) => AsyncIterable<D['response']>
|
|
141
|
+
: (payload: D['request'], options?: GrpcClientCallOptions) => Promise<D['response']>;
|
|
142
|
+
|
|
143
|
+
/** One typed property per service method, plus explicit channel ownership. */
|
|
144
|
+
export type GrpcClient<S extends GrpcServiceDef> = {
|
|
145
|
+
readonly [M in keyof S]: GrpcCaller<S[M]>;
|
|
146
|
+
} & {
|
|
147
|
+
close(): void;
|
|
148
|
+
[Symbol.dispose](): void;
|
|
149
|
+
};
|
|
150
|
+
|
|
151
|
+
export interface GrpcClientOptions<S extends GrpcServiceDef> {
|
|
152
|
+
readonly definition: GrpcLoadedService<S>;
|
|
153
|
+
readonly address: string;
|
|
154
|
+
readonly credentials: 'insecure' | GrpcClientTlsOptions;
|
|
155
|
+
readonly deadlineMs: number;
|
|
156
|
+
readonly validateMetadata: GrpcMetadataValidator;
|
|
157
|
+
}
|