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

Function streamToResponse

packages/effect/src/unstable/httpapi/HttpApiClient.ts:865–890  ·  view source on GitHub ↗
(successSchema: StreamSuccessSchema)

Source from the content-addressed store, hash-verified

863}
864
865function streamToResponse(successSchema: StreamSuccessSchema) {
866 const isWithHeaders = isWithHeadersStreamSuccess(successSchema)
867 const streamSchema = isWithHeaders ? successSchema.schema : successSchema
868 const sse = HttpApiSchema.isStreamUint8Array(streamSchema)
869 ? undefined
870 : {
871 declaration: streamSchema,
872 decoder: makeSseDecoder(streamSchema)
873 }
874 const toStream = (response: HttpClientResponse.HttpClientResponse) =>
875 Effect.map(Effect.context<never>(), (context) =>
876 Stream.provideContext(
877 sse === undefined ?
878 response.stream :
879 decodeSseStream(response.stream, sse.declaration, sse.decoder),
880 context as Context.Context<unknown>
881 ))
882 if (!isWithHeaders) return toStream
883
884 const decodeHeaders = Schema.decodeUnknownEffect(successSchema.headers)
885 return (response: HttpClientResponse.HttpClientResponse) =>
886 Effect.flatMap(
887 decodeHeaders(response.headers),
888 (headers) => Effect.map(toStream(response), (body) => HttpApiSchema.withHeaders({ body, headers }))
889 )
890}
891
892function makeSseDecoder(
893 declaration: HttpApiSchema.StreamSse<Sse.EventCodec, Schema.Constraint, unknown>

Callers 1

onEndpointFunction · 0.85

Calls 4

makeSseDecoderFunction · 0.85
toStreamFunction · 0.85
mapMethod · 0.45

Tested by

no test coverage detected