stream method

  1. @override
Stream<Uint8List> stream(
  1. KataCxRequest request
)
override

Performs request and yields body bytes as they arrive.

Yields raw bytes rather than strings on purpose: /chat-streaming sends unframed UTF-8, and a multi-byte character can straddle two reads. Callers must decode with utf8.decoder as a stream transformer — decoding each chunk on its own corrupts it.

Throws when the response status is not 2xx. No read deadline applies, matching the Android module's streaming client.

Implementation

@override
Stream<Uint8List> stream(KataCxRequest request) async* {
  requests.add(request);
  if (responseDelay > Duration.zero) {
    await Future<void>.delayed(responseDelay);
  }
  final failure = streamFailureStatus;
  if (failure != null) {
    throw KataCxHttpException(
      'scripted failure',
      statusCode: failure,
      url: request.url,
    );
  }

  final index = _streamCalls.clamp(0, streamReplies.length - 1);
  _streamCalls++;
  for (final chunk in streamReplies[index]) {
    if (chunkDelay > Duration.zero) {
      await Future<void>.delayed(chunkDelay);
    }
    yield Uint8List.fromList(utf8.encode(chunk));
  }
}