diff --git a/docker/utils/json_stream.py b/docker/utils/json_stream.py index 41d25920..c7fd46fa 100644 --- a/docker/utils/json_stream.py +++ b/docker/utils/json_stream.py @@ -1,4 +1,3 @@ -import json import json.decoder from ..errors import StreamParseError @@ -37,30 +36,12 @@ def json_stream(stream): This handles streams which are inconsistently buffered (some entries may be newline delimited, and others are not). """ - return split_buffer(stream, json_splitter, json_decoder.decode) - - -def line_splitter(buffer, separator='\n'): - index = buffer.find(str(separator)) - if index == -1: - return None - return buffer[:index + 1], buffer[index + 1:] - - -def split_buffer(stream, splitter=None, decoder=lambda a: a): - """Given a generator which yields strings and a splitter function, - joins all input, splits on the separator and yields each chunk. - Unlike string.split(), each chunk includes the trailing - separator, except for the last one if none was found on the end - of the input. - """ - splitter = splitter or line_splitter buffered = '' for data in stream_as_text(stream): buffered += data while True: - buffer_split = splitter(buffered) + buffer_split = json_splitter(buffered) if buffer_split is None: break @@ -69,6 +50,13 @@ def split_buffer(stream, splitter=None, decoder=lambda a: a): if buffered: try: - yield decoder(buffered) + yield json_decoder.decode(buffered) except Exception as e: raise StreamParseError(e) from e + + +def line_splitter(buffer: str, separator='\n'): + index = buffer.find(str(separator)) + if index == -1: + return None + return buffer[:index + 1], buffer[index + 1:]