GitHub

API reference for server.

The DevTools side of the protocol (Effect's devtools/DevToolsServer): run hands each connected app to handle as a Client.

Re-exports

Types

TypeDefinitionDescription
RequestWithoutPingtype RequestWithoutPing = D.Span | D.SpanEvent | D.MetricsSnapshot

Classes

Functions

run

run[E = Never, R = Never](handle) -> Effect[Never, SocketServerError, R | SocketServer]

Serve the SocketServer's connections with handle.

ParameterTypeDescription
handleCallable[[Client], Effect[object, E, R]]

Returns Effect[Never, SocketServerError, R | SocketServer]

def run[E = Never, R = Never](
    handle: Callable[[Client], Effect[object, E, R]],
) -> Effect[Never, SocketServerError, R | SocketServer]:
    """Serve the ``SocketServer``'s connections with ``handle``."""

    def connection(socket: SocketService) -> Effect[object, E, R]:
        def body():
            responses = yield from Queue[D.Response, Any].unbounded()
            requests = yield from Queue[RequestWithoutPing, Any].unbounded()

            def on_request(request: D.Request) -> Effect[None]:
                if request["_tag"] == "Ping":
                    return responses.offer({"_tag": "Pong"}).as_void()
                return requests.offer(request).as_void()

            socket_channel: SocketChannel[str, NdjsonError | SchemaError] = to_channel_string(
                socket
            )
            channel = duplex_schema_string(
                socket_channel, input_schema=D.RESPONSE_CODEC, output_schema=D.REQUEST_CODEC
            )
            yield from (
                from_queue(responses)
                .pipe_through_channel(channel)
                .run_for_each(on_request)
                .ensuring(responses.shutdown().and_then(requests.shutdown()))
                .fork()
            )
            client = Client(requests, lambda response: responses.offer(response).as_void())
            return (yield from handle(client))

        return gen(body)

    return SocketServer().flat_map(lambda server: server.run(connection))