MCPcopy Create free account
hub / github.com/Effect-TS/effect / makeHttpExchange

Function makeHttpExchange

packages/effect/src/rpc/RpcClient.ts:1128–1248  ·  view source on GitHub ↗
(
  client: HttpClient.HttpClient,
  serialization: RpcSerialization.RpcSerialization["Service"],
  writeResponse: (clientId: number, response: FromServerEncoded) => Effect.Effect<void>
)

Source from the content-addressed store, hash-verified

1126// Sends RPC requests in one HTTP request, shared by the batched and unbatched
1127// HTTP protocols.
1128const makeHttpExchange = (
1129 client: HttpClient.HttpClient,
1130 serialization: RpcSerialization.RpcSerialization["Service"],
1131 writeResponse: (clientId: number, response: FromServerEncoded) => Effect.Effect<void>
1132) => {
1133 const isFramed = serialization.includesFraming
1134 // Closing the scope aborts the unread notification response body.
1135 const scopedClient = HttpClient.withScope(client)
1136 const isNotification = (request: RequestEncoded) =>
1137 request.discard === true && serialization.supportsControlMessages === false
1138 const httpClientError = (cause: any) =>
1139 new RpcClientError({
1140 reason: HttpClientErrorSchema.fromHttpClientError(cause)
1141 })
1142 const protocolDefect = (message: string, cause: unknown) =>
1143 new RpcClientError({
1144 reason: new RpcClientDefect({ message, cause })
1145 })
1146
1147 // Writes each response as it is decoded. Returns the ids of the requests left
1148 // without a terminal response, or `undefined` when the server sent no
1149 // responses.
1150 const send = Effect.fnUntraced(function*(
1151 clientId: number,
1152 requests: ReadonlyArray<RequestEncoded>,
1153 accepted?: Deferred.Deferred<void, RpcClientError>
1154 ) {
1155 const parser = serialization.makeUnsafe()
1156
1157 const encoded = parser.encode(requests.length === 1 ? requests[0] : requests)!
1158 const body = typeof encoded === "string" ?
1159 HttpBody.text(encoded, serialization.contentType) :
1160 HttpBody.uint8Array(encoded, serialization.contentType)
1161
1162 // Finish on the status and release the body without reading it.
1163 if (requests.every(isNotification)) {
1164 yield* Effect.scoped(Effect.flatMap(scopedClient.post("", { body }), HttpClientResponse.filterStatusOk)).pipe(
1165 Effect.mapError(httpClientError)
1166 )
1167 return new Set<string>()
1168 }
1169
1170 const response = yield* client.post("", { body }).pipe(Effect.mapError(httpClientError))
1171 if (accepted !== undefined) {
1172 yield* Deferred.into(
1173 Effect.asVoid(Effect.mapError(HttpClientResponse.filterStatusOk(response), httpClientError)),
1174 accepted
1175 )
1176 }
1177
1178 const pending = new Set<string>()
1179 for (let i = 0; i < requests.length; i++) {
1180 pending.add(String(requests[i].id))
1181 }
1182 const onResponse = (response: FromServerEncoded) => {
1183 if (response._tag === "Exit") {
1184 pending.delete(String(response.requestId))
1185 } else if (isTerminalResponse(response)) {

Callers 2

RpcClient.tsFile · 0.85

Calls 11

protocolDefectFunction · 0.85
isRpcClientErrorFunction · 0.85
httpClientErrorFunction · 0.85
encodeMethod · 0.80
onResponseFunction · 0.70
textMethod · 0.65
pipeMethod · 0.65
addMethod · 0.65
StringFunction · 0.50
postMethod · 0.45
decodeMethod · 0.45

Tested by

no test coverage detected