MCPcopy
hub / github.com/Effect-TS/effect / send

Function send

packages/rpc/src/RpcClient.ts:850–907  ·  view source on GitHub ↗
(request: FromClientEncoded)

Source from the content-addressed store, hash-verified

848 const isJson = serialization.contentType === "application/json"
849
850 const send = (request: FromClientEncoded): Effect.Effect<void, RpcClientError> => {
851 if (request._tag !== "Request") {
852 return Effect.void
853 }
854
855 const parser = serialization.unsafeMake()
856
857 const encoded = parser.encode(request)!
858 const body = typeof encoded === "string" ?
859 HttpBody.text(encoded, serialization.contentType) :
860 HttpBody.uint8Array(encoded, serialization.contentType)
861
862 if (isJson) {
863 return client.post("", { body }).pipe(
864 Effect.flatMap((r) => r.json),
865 Effect.mapError((cause) =>
866 new RpcClientError({
867 reason: "Protocol",
868 message: "Failed to send HTTP request",
869 cause
870 })
871 ),
872 Effect.flatMap((u) => {
873 if (!Array.isArray(u)) {
874 return Effect.dieMessage(`Expected an array of responses, but got: ${u}`)
875 }
876 let i = 0
877 return Effect.whileLoop({
878 while: () => i < u.length,
879 body: () => writeResponse(u[i++]),
880 step: constVoid
881 })
882 })
883 )
884 }
885
886 return client.post("", { body }).pipe(
887 Effect.flatMap((r) =>
888 Stream.runForEachChunk(r.stream, (chunk) => {
889 const responses = Chunk.toReadonlyArray(chunk).flatMap(parser.decode) as Array<FromServerEncoded>
890 if (responses.length === 0) return Effect.void
891 let i = 0
892 return Effect.whileLoop({
893 while: () => i < responses.length,
894 body: () => writeResponse(responses[i++]),
895 step: constVoid
896 })
897 })
898 ),
899 Effect.mapError((cause) =>
900 new RpcClientError({
901 reason: "Protocol",
902 message: "Failed to send HTTP request",
903 cause
904 })
905 )
906 )
907 }

Callers 1

onFromClientFunction · 0.70

Calls 9

encodeMethod · 0.80
unsafeOpenMethod · 0.80
writeFunction · 0.70
pipeMethod · 0.65
dieMessageMethod · 0.65
failMethod · 0.65
setMethod · 0.65
getMethod · 0.65
textMethod · 0.45

Tested by

no test coverage detected