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

Function handle

packages/effect/src/graphql/GraphQLProtocol.ts:832–888  ·  view source on GitHub ↗
(conn: Connection, frame: string | Uint8Array)

Source from the content-addressed store, hash-verified

830 )
831
832 const handle = (conn: Connection, frame: string | Uint8Array): Effect.Effect<void, TransportError> => {
833 // Any frame answers a keep-alive ping; an invalid one drops the connection anyway.
834 conn.receivedFrame = true
835 let message: WsMessage
836 try {
837 message = JSON.parse(typeof frame === "string" ? frame : textDecoder.decode(frame))
838 } catch (cause) {
839 return invalid(errorMessage(cause), cause)
840 }
841 if (!isObject(message)) {
842 return invalid("expected an object")
843 }
844 switch (message.type) {
845 case "connection_ack":
846 case "ping":
847 case "pong": {
848 if (message.payload != null && !isObject(message.payload)) {
849 return invalid(`${message.type} with a payload that is not an object`)
850 }
851 if (message.type === "connection_ack") {
852 Deferred.doneUnsafe(conn.ack, Effect.void)
853 }
854 return message.type === "ping" ? send({ type: "pong" }) : Effect.void
855 }
856 case "next":
857 case "error":
858 case "complete": {
859 const id = message.id
860 if (typeof id !== "string" || id === "") {
861 return invalid(`${message.type} without an id`)
862 }
863 if (message.type === "next" && !isObject(message.payload)) {
864 return invalid("next with a payload that is not an object")
865 }
866 if (message.type === "error" && !isFormattedErrors(message.payload)) {
867 return invalid("error with a payload that is not a list of GraphQL errors")
868 }
869 const operation = operations.get(id)
870 // Unknown ids, including operations this client already completed, are dropped.
871 if (operation === undefined || operation.connection !== conn) return Effect.void
872 if (message.type === "next") {
873 Queue.offerUnsafe(operation.queue, { body: message.payload })
874 return Effect.void
875 }
876 operations.delete(id)
877 if (message.type === "error") {
878 // The server rejected the operation; the client fails it with a ResponseError.
879 Queue.offerUnsafe(operation.queue, { body: { errors: message.payload } })
880 }
881 Queue.endUnsafe(operation.queue)
882 return Effect.void
883 }
884 default: {
885 return invalid(`unknown type ${JSON.stringify(message.type)}`)
886 }
887 }
888 }
889

Callers 1

makeWebSocketFunction · 0.70

Calls 10

errorMessageFunction · 0.85
isFormattedErrorsFunction · 0.85
offerUnsafeMethod · 0.80
invalidFunction · 0.70
isObjectFunction · 0.70
sendFunction · 0.70
getMethod · 0.65
deleteMethod · 0.65
parseMethod · 0.45
decodeMethod · 0.45

Tested by

no test coverage detected