File size: 14,257 Bytes
31e8457
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e391660
 
 
31e8457
e391660
31e8457
 
 
 
 
 
e391660
 
 
 
31e8457
e391660
31e8457
 
 
 
 
 
 
 
 
 
 
 
e391660
 
 
 
31e8457
e391660
31e8457
 
 
 
 
 
 
 
 
e391660
 
 
31e8457
e391660
31e8457
 
 
 
 
 
 
 
 
e391660
31e8457
e391660
31e8457
e391660
 
 
 
31e8457
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e391660
 
 
 
 
 
 
 
 
 
31e8457
 
 
 
e391660
 
31e8457
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e391660
 
 
 
31e8457
 
 
 
 
 
 
 
 
 
 
 
 
 
e391660
31e8457
e391660
31e8457
 
 
 
e391660
 
 
 
31e8457
 
 
 
 
 
 
e391660
 
 
 
31e8457
 
 
 
 
 
e391660
31e8457
e391660
31e8457
 
 
 
 
 
e391660
 
 
 
 
31e8457
 
 
 
 
 
 
e391660
 
 
 
31e8457
 
 
 
 
 
e391660
 
31e8457
e391660
31e8457
 
 
 
 
 
e391660
 
 
31e8457
e391660
31e8457
 
 
 
 
 
 
 
e391660
 
 
31e8457
e391660
31e8457
 
 
 
 
 
 
 
e391660
 
31e8457
e391660
31e8457
 
 
 
 
 
 
 
 
 
 
 
 
 
e391660
 
31e8457
e391660
31e8457
 
 
 
e391660
 
 
 
31e8457
 
 
 
e391660
 
 
31e8457
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
import * as Context from "../../Context.ts";
import * as Effect from "../../Effect.ts";
import * as Layer from "../../Layer.ts";
import * as Option from "../../Option.ts";
import * as Queue from "../../Queue.ts";
import * as Scope from "../../Scope.ts";
import { Stdio } from "../../Stdio.ts";
import * as HttpRouter from "../http/HttpRouter.ts";
import * as HttpServerRequest from "../http/HttpServerRequest.ts";
import * as HttpServerResponse from "../http/HttpServerResponse.ts";
import * as SocketServer from "../socket/SocketServer.ts";
import type { WorkerError } from "../workers/WorkerError.ts";
import * as WorkerRunner from "../workers/WorkerRunner.ts";
import * as Rpc from "./Rpc.ts";
import type * as RpcGroup from "./RpcGroup.ts";
import type { FromClient, FromClientEncoded, FromServer, FromServerEncoded } from "./RpcMessage.ts";
import * as RpcSerialization from "./RpcSerialization.ts";
/**
 * The decoded RPC server boundary, accepting client messages for a client id
 * and allowing that client to be disconnected.
 *
 * @category server
 * @since 4.0.0
 */
export interface RpcServer<A extends Rpc.Any> {
    readonly write: (clientId: number, message: FromClient<A>) => Effect.Effect<void>;
    readonly disconnect: (clientId: number) => Effect.Effect<void>;
}
/**
 * Creates an RPC server for an already-decoded message channel, running
 * handlers for a group and sending decoded server responses through
 * `onFromServer`.
 *
 * @category server
 * @since 4.0.0
 */
export declare const makeNoSerialization: <Rpcs extends Rpc.Any>(group: RpcGroup.RpcGroup<Rpcs>, options: {
    readonly onFromServer: (response: FromServer<Rpcs>) => Effect.Effect<void>;
    readonly disableTracing?: boolean | undefined;
    readonly disableSpanPropagation?: boolean | undefined;
    readonly spanPrefix?: string | undefined;
    readonly spanAttributes?: Record<string, unknown> | undefined;
    readonly disableClientAcks?: boolean | undefined;
    readonly concurrency?: number | "unbounded" | undefined;
    readonly disableFatalDefects?: boolean | undefined;
}) => Effect.Effect<RpcServer<Rpcs>, never, Rpc.ToHandler<Rpcs> | Rpc.Middleware<Rpcs> | Scope.Scope>;
/**
 * Runs an RPC server for a group using the current server `Protocol`, decoding
 * requests, invoking handlers, encoding responses, and managing in-flight
 * request lifetime.
 *
 * @category server
 * @since 4.0.0
 */
export declare const make: <Rpcs extends Rpc.Any>(group: RpcGroup.RpcGroup<Rpcs>, options?: {
    readonly disableTracing?: boolean | undefined;
    readonly spanPrefix?: string | undefined;
    readonly spanAttributes?: Record<string, unknown> | undefined;
    readonly concurrency?: number | "unbounded" | undefined;
    readonly disableFatalDefects?: boolean | undefined;
} | undefined) => Effect.Effect<never, never, Protocol | Rpc.ToHandler<Rpcs> | Rpc.Middleware<Rpcs> | Rpc.ServicesServer<Rpcs>>;
/**
 * Provides a scoped layer that starts an RPC server for a group using the
 * current server `Protocol`.
 *
 * @category server
 * @since 4.0.0
 */
export declare const layer: <Rpcs extends Rpc.Any>(group: RpcGroup.RpcGroup<Rpcs>, options?: {
    readonly disableTracing?: boolean | undefined;
    readonly spanPrefix?: string | undefined;
    readonly spanAttributes?: Record<string, unknown> | undefined;
    readonly concurrency?: number | "unbounded" | undefined;
    readonly disableFatalDefects?: boolean | undefined;
}) => Layer.Layer<never, never, Protocol | Rpc.ToHandler<Rpcs> | Rpc.Middleware<Rpcs> | Rpc.ServicesServer<Rpcs>>;
/**
 * Creates a RPC server that registers a HTTP route with a `HttpRouter`.
 *
 * **Details**
 *
 * Defaults to using websockets for communication, but can be configured to use
 * HTTP.
 *
 * @category protocols
 * @since 4.0.0
 */
export declare const layerHttp: <Rpcs extends Rpc.Any>(options: {
    readonly group: RpcGroup.RpcGroup<Rpcs>;
    readonly path: HttpRouter.PathInput;
    readonly protocol?: "http" | "websocket" | undefined;
    readonly disableTracing?: boolean | undefined;
    readonly spanPrefix?: string | undefined;
    readonly spanAttributes?: Record<string, unknown> | undefined;
    readonly concurrency?: number | "unbounded" | undefined;
    readonly disableFatalDefects?: boolean | undefined;
}) => Layer.Layer<never, never, RpcSerialization.RpcSerialization | HttpRouter.HttpRouter | Rpc.ToHandler<Rpcs> | Rpc.Middleware<Rpcs> | Rpc.ServicesServer<Rpcs>>;
declare const Protocol_base: Context.ServiceClass<Protocol, "effect/rpc/RpcServer/Protocol", {
    readonly run: (f: (clientId: number, data: FromClientEncoded) => Effect.Effect<void>) => Effect.Effect<never>;
    readonly disconnects: Queue.Dequeue<number>;
    readonly send: (clientId: number, response: FromServerEncoded, transferables?: ReadonlyArray<globalThis.Transferable>) => Effect.Effect<void>;
    readonly end: (clientId: number) => Effect.Effect<void>;
    readonly clientIds: Effect.Effect<ReadonlySet<number>>;
    readonly initialMessage: Effect.Effect<Option.Option<unknown>>;
    readonly supportsAck: boolean;
    readonly supportsTransferables: boolean;
    readonly supportsSpanPropagation: boolean;
}>;
/**
 * Defines the service interface for an RPC server transport, responsible for receiving
 * encoded client messages, sending encoded responses, tracking clients, and
 * declaring transport capabilities.
 *
 * **When to use**
 *
 * Use to provide the transport boundary for RPC servers over HTTP, WebSocket,
 * workers, sockets, or custom protocols.
 *
 * @category protocols
 * @since 4.0.0
 */
export declare class Protocol extends Protocol_base {
    /**
     * Creates a server protocol service from the supplied RPC implementation.
     *
     * @since 4.0.0
     */
    static make: <EX, RX>(f: (write: (clientId: number, data: FromClientEncoded) => Effect.Effect<void>) => Effect.Effect<Omit<{
        readonly run: (f: (clientId: number, data: FromClientEncoded) => Effect.Effect<void>) => Effect.Effect<never>;
        readonly disconnects: Queue.Dequeue<number>;
        readonly send: (clientId: number, response: FromServerEncoded, transferables?: ReadonlyArray<globalThis.Transferable>) => Effect.Effect<void>;
        readonly end: (clientId: number) => Effect.Effect<void>;
        readonly clientIds: Effect.Effect<ReadonlySet<number>>;
        readonly initialMessage: Effect.Effect<Option.Option<unknown>>;
        readonly supportsAck: boolean;
        readonly supportsTransferables: boolean;
        readonly supportsSpanPropagation: boolean;
    }, "run">, EX, RX>) => Effect.Effect<{
        readonly run: (f: (clientId: number, data: FromClientEncoded) => Effect.Effect<void>) => Effect.Effect<never>;
        readonly disconnects: Queue.Dequeue<number>;
        readonly send: (clientId: number, response: FromServerEncoded, transferables?: ReadonlyArray<globalThis.Transferable>) => Effect.Effect<void>;
        readonly end: (clientId: number) => Effect.Effect<void>;
        readonly clientIds: Effect.Effect<ReadonlySet<number>>;
        readonly initialMessage: Effect.Effect<Option.Option<unknown>>;
        readonly supportsAck: boolean;
        readonly supportsTransferables: boolean;
        readonly supportsSpanPropagation: boolean;
    }, EX, RX>;
}
/**
 * Creates a server `Protocol` backed by the current `SocketServer`, accepting
 * socket connections and routing decoded RPC messages.
 *
 * @category protocols
 * @since 4.0.0
 */
export declare const makeProtocolSocketServer: Effect.Effect<{
    readonly run: (f: (clientId: number, data: FromClientEncoded) => Effect.Effect<void>) => Effect.Effect<never>;
    readonly disconnects: Queue.Dequeue<number>;
    readonly send: (clientId: number, response: FromServerEncoded, transferables?: ReadonlyArray<globalThis.Transferable>) => Effect.Effect<void>;
    readonly end: (clientId: number) => Effect.Effect<void>;
    readonly clientIds: Effect.Effect<ReadonlySet<number>>;
    readonly initialMessage: Effect.Effect<Option.Option<unknown>>;
    readonly supportsAck: boolean;
    readonly supportsTransferables: boolean;
    readonly supportsSpanPropagation: boolean;
}, never, Scope.Scope | RpcSerialization.RpcSerialization | SocketServer.SocketServer>;
/**
 * RPC protocol that uses `SocketServer` for communication.
 *
 * @category protocols
 * @since 4.0.0
 */
export declare const layerProtocolSocketServer: Layer.Layer<Protocol, never, RpcSerialization.RpcSerialization | SocketServer.SocketServer>;
/**
 * Creates a websocket server `Protocol` together with an HTTP effect that
 * upgrades the current request to a websocket and attaches it to the protocol.
 *
 * @category protocols
 * @since 4.0.0
 */
export declare const makeProtocolWithHttpEffectWebsocket: Effect.Effect<{
    readonly protocol: Protocol["Service"];
    readonly httpEffect: Effect.Effect<HttpServerResponse.HttpServerResponse, never, Scope.Scope | HttpServerRequest.HttpServerRequest>;
}, never, RpcSerialization.RpcSerialization>;
/**
 * Creates a websocket server `Protocol` and registers its upgrade handler as a
 * GET route on the current `HttpRouter`.
 *
 * @category protocols
 * @since 4.0.0
 */
export declare const makeProtocolWebsocket: (options: {
    readonly path: HttpRouter.PathInput;
}) => Effect.Effect<Protocol["Service"], never, RpcSerialization.RpcSerialization | HttpRouter.HttpRouter>;
/**
 * RPC protocol that uses WebSockets for communication.
 *
 * @category protocols
 * @since 4.0.0
 */
export declare const layerProtocolWebsocket: (options: {
    readonly path: HttpRouter.PathInput;
}) => Layer.Layer<Protocol, never, RpcSerialization.RpcSerialization | HttpRouter.HttpRouter>;
/**
 * Creates an HTTP request/response server `Protocol` together with an HTTP
 * effect that decodes the current request and streams or returns encoded RPC
 * responses.
 *
 * @category protocols
 * @since 4.0.0
 */
export declare const makeProtocolWithHttpEffect: Effect.Effect<{
    readonly protocol: Protocol["Service"];
    readonly httpEffect: Effect.Effect<HttpServerResponse.HttpServerResponse, never, Scope.Scope | HttpServerRequest.HttpServerRequest>;
}, never, RpcSerialization.RpcSerialization>;
/**
 * Creates an HTTP server `Protocol` and registers its request handler as a POST
 * route on the current `HttpRouter`.
 *
 * @category protocols
 * @since 4.0.0
 */
export declare const makeProtocolHttp: (options: {
    readonly path: HttpRouter.PathInput;
}) => Effect.Effect<Protocol["Service"], never, RpcSerialization.RpcSerialization | HttpRouter.HttpRouter>;
/**
 * Provides a server `Protocol` that uses HTTP POST requests for RPC
 * communication.
 *
 * @category protocols
 * @since 4.0.0
 */
export declare const layerProtocolHttp: (options: {
    readonly path: HttpRouter.PathInput;
}) => Layer.Layer<Protocol, never, RpcSerialization.RpcSerialization | HttpRouter.HttpRouter>;
/**
 * Starts an RPC server for a group and returns the HTTP request/response effect
 * that serves the non-websocket HTTP RPC protocol.
 *
 * @category http app
 * @since 4.0.0
 */
export declare const toHttpEffect: <Rpcs extends Rpc.Any>(group: RpcGroup.RpcGroup<Rpcs>, options?: {
    readonly disableTracing?: boolean | undefined;
    readonly spanPrefix?: string | undefined;
    readonly spanAttributes?: Record<string, unknown> | undefined;
    readonly disableFatalDefects?: boolean | undefined;
} | undefined) => Effect.Effect<Effect.Effect<HttpServerResponse.HttpServerResponse, never, Scope.Scope | HttpServerRequest.HttpServerRequest>, never, Scope.Scope | RpcSerialization.RpcSerialization | Rpc.ToHandler<Rpcs> | Rpc.Middleware<Rpcs> | Rpc.ServicesServer<Rpcs>>;
/**
 * Starts an RPC server for a group and returns the HTTP effect that upgrades
 * requests to the websocket RPC protocol.
 *
 * @category http app
 * @since 4.0.0
 */
export declare const toHttpEffectWebsocket: <Rpcs extends Rpc.Any>(group: RpcGroup.RpcGroup<Rpcs>, options?: {
    readonly disableTracing?: boolean | undefined;
    readonly spanPrefix?: string | undefined;
    readonly spanAttributes?: Record<string, unknown> | undefined;
    readonly disableFatalDefects?: boolean | undefined;
} | undefined) => Effect.Effect<Effect.Effect<HttpServerResponse.HttpServerResponse, never, Scope.Scope | HttpServerRequest.HttpServerRequest>, never, Scope.Scope | RpcSerialization.RpcSerialization | Rpc.ToHandler<Rpcs> | Rpc.Middleware<Rpcs> | Rpc.ServicesServer<Rpcs>>;
/**
 * Creates a server `Protocol` that reads RPC messages from `Stdio.stdin` and
 * writes encoded responses to `Stdio.stdout`.
 *
 * @category protocols
 * @since 4.0.0
 */
export declare const makeProtocolStdio: Effect.Effect<{
    readonly run: (f: (clientId: number, data: FromClientEncoded) => Effect.Effect<void>) => Effect.Effect<never>;
    readonly disconnects: Queue.Dequeue<number>;
    readonly send: (clientId: number, response: FromServerEncoded, transferables?: ReadonlyArray<globalThis.Transferable>) => Effect.Effect<void>;
    readonly end: (clientId: number) => Effect.Effect<void>;
    readonly clientIds: Effect.Effect<ReadonlySet<number>>;
    readonly initialMessage: Effect.Effect<Option.Option<unknown>>;
    readonly supportsAck: boolean;
    readonly supportsTransferables: boolean;
    readonly supportsSpanPropagation: boolean;
}, never, Scope.Scope | Stdio | RpcSerialization.RpcSerialization>;
/**
 * Provides a server `Protocol` that reads RPC messages from `Stdio.stdin` and
 * writes encoded responses to `Stdio.stdout`.
 *
 * @category protocols
 * @since 4.0.0
 */
export declare const layerProtocolStdio: Layer.Layer<Protocol, never, RpcSerialization.RpcSerialization | Stdio>;
/**
 * Creates a server `Protocol` backed by `WorkerRunnerPlatform`, routing worker
 * messages to the RPC server and server responses back to workers.
 *
 * @category protocols
 * @since 4.0.0
 */
export declare const makeProtocolWorkerRunner: Effect.Effect<Protocol["Service"], WorkerError, WorkerRunner.WorkerRunnerPlatform | Scope.Scope>;
/**
 * Provides a server `Protocol` backed by the current `WorkerRunnerPlatform`.
 *
 * @category protocols
 * @since 4.0.0
 */
export declare const layerProtocolWorkerRunner: Layer.Layer<Protocol, WorkerError, WorkerRunner.WorkerRunnerPlatform>;
export {};
//# sourceMappingURL=RpcServer.d.ts.map