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

Function makeStreamBodyDecoder

packages/effect/src/http-api/HttpApiClient.ts:1432–1464  ·  view source on GitHub ↗
(
  streamSchema: HttpApiSchema.StreamSchema,
  options: SchemaAST.ParseOptions | undefined
)

Source from the content-addressed store, hash-verified

1430}
1431
1432function makeStreamBodyDecoder(
1433 streamSchema: HttpApiSchema.StreamSchema,
1434 options: SchemaAST.ParseOptions | undefined
1435): (
1436 response: HttpClientResponse.HttpClientResponse,
1437 sseOptions: Sse.DecodeOptions | undefined
1438) => Stream.Stream<unknown, unknown, unknown> {
1439 if (HttpApiSchema.isStreamUint8Array(streamSchema)) {
1440 return (response) => response.stream
1441 }
1442 if (HttpApiSchema.isStreamNdjson(streamSchema)) {
1443 const decodeElement = Schema.decodeUnknownEffect(Schema.toCodecJson(streamSchema.schema), options)
1444 const maxElementBytes = ByteSize.toNumberUnsafe(streamSchema.maxElementBytes)
1445 return (response) =>
1446 response.stream.pipe(
1447 Stream.pipeThroughChannel(Ndjson.decode({ ignoreEmptyLines: true, maxLineBytes: maxElementBytes })),
1448 Stream.mapError((error) => {
1449 if (!Ndjson.isNdjsonError(error)) return error
1450 if (error.reason !== "LineTooLarge") return ndjsonSchemaError(error)
1451 return new HttpClientError.HttpClientError({
1452 reason: new HttpClientError.DecodeError({
1453 request: response.request,
1454 response,
1455 description: `NDJSON line exceeds maxElementBytes of ${maxElementBytes} bytes`
1456 })
1457 })
1458 }),
1459 Stream.mapEffect((json) => decodeElement(json))
1460 )
1461 }
1462 const decoder = makeSseDecoder(streamSchema, options)
1463 return (response, sseOptions) => decodeSseStream(response.stream, streamSchema, decoder(sseOptions))
1464}
1465
1466// `JSON.stringify` found no JSON representation for an encoded element
1467const isNdjsonPackError = (error: unknown): error is Ndjson.NdjsonError =>

Callers 1

streamToResponseFunction · 0.85

Calls 6

makeSseDecoderFunction · 0.85
decodeSseStreamFunction · 0.85
ndjsonSchemaErrorFunction · 0.70
toCodecJsonMethod · 0.65
pipeMethod · 0.65
decodeMethod · 0.45

Tested by

no test coverage detected