(client: HttpClient.HttpClient)
| 874 | * @since 4.0.0 |
| 875 | */ |
| 876 | export const makeProtocolHttp = (client: HttpClient.HttpClient): Effect.Effect< |
| 877 | Protocol["Service"], |
| 878 | never, |
| 879 | RpcSerialization.RpcSerialization |
| 880 | > => |
| 881 | Protocol.make(Effect.fnUntraced(function*(writeResponse) { |
| 882 | const serialization = yield* RpcSerialization.RpcSerialization |
| 883 | const isFramed = serialization.includesFraming |
| 884 | const httpClientError = (cause: any) => |
| 885 | new RpcClientError({ |
| 886 | reason: HttpClientErrorSchema.fromHttpClientError(cause) |
| 887 | }) |
| 888 | const protocolDefect = (message: string, cause: unknown) => |
| 889 | new RpcClientError({ |
| 890 | reason: new RpcClientDefect({ message, cause }) |
| 891 | }) |
| 892 | const emptyResponseError = (request: FromClientEncoded) => |
| 893 | protocolDefect("Received empty HTTP response from RPC server", request) |
| 894 | const incompleteResponseError = (request: FromClientEncoded) => |
| 895 | protocolDefect("HTTP response ended before RPC request completed", request) |
| 896 | |
| 897 | const send = Effect.fnUntraced(function*(clientId: number, request: FromClientEncoded) { |
| 898 | if (request._tag !== "Request") { |
| 899 | return |
| 900 | } |
| 901 | |
| 902 | const parser = serialization.makeUnsafe() |
| 903 | |
| 904 | const encoded = parser.encode(request)! |
| 905 | const body = typeof encoded === "string" ? |
| 906 | HttpBody.text(encoded, serialization.contentType) : |
| 907 | HttpBody.uint8Array(encoded, serialization.contentType) |
| 908 | |
| 909 | const response = yield* client.post("", { body }).pipe(Effect.mapError(httpClientError)) |
| 910 | |
| 911 | if (!isFramed) { |
| 912 | const text = yield* response.text.pipe(Effect.mapError(httpClientError)) |
| 913 | const responses = yield* Effect.try({ |
| 914 | try: () => parser.decode(text), |
| 915 | catch: (cause) => protocolDefect("Error decoding HTTP response", cause) |
| 916 | }) |
| 917 | if (!Array.isArray(responses)) { |
| 918 | return yield* protocolDefect("Expected an array of responses", responses) |
| 919 | } |
| 920 | if (responses.length === 0) { |
| 921 | return yield* emptyResponseError(request) |
| 922 | } |
| 923 | let completed = false |
| 924 | let i = 0 |
| 925 | yield* Effect.whileLoop({ |
| 926 | while: () => i < responses.length, |
| 927 | body: () => { |
| 928 | const response = responses[i++] |
| 929 | if (isTerminalResponse(response)) { |
| 930 | completed = true |
| 931 | } |
| 932 | return writeResponse(clientId, response) |
| 933 | }, |
no test coverage detected