From 5f0a1500c9d80d479a11f29edac0abac68a67c58 Mon Sep 17 00:00:00 2001 From: Simon Frings Date: Tue, 10 May 2022 12:16:57 +0200 Subject: [PATCH] Check type of incoming `data` before trying to decode NDJSON --- src/Decoder.php | 5 +++++ tests/DecoderTest.php | 8 ++++++++ 2 files changed, 13 insertions(+) diff --git a/src/Decoder.php b/src/Decoder.php index 6bd07ef..9ef99a3 100644 --- a/src/Decoder.php +++ b/src/Decoder.php @@ -99,6 +99,11 @@ public function pipe(WritableStreamInterface $dest, array $options = array()) /** @internal */ public function handleData($data) { + if (!\is_string($data)) { + $this->handleError(new \UnexpectedValueException('Expected stream to emit string, but got ' . \gettype($data))); + return; + } + $this->buffer .= $data; // keep parsing while a newline has been found diff --git a/tests/DecoderTest.php b/tests/DecoderTest.php index 4e25b45..0743e6b 100644 --- a/tests/DecoderTest.php +++ b/tests/DecoderTest.php @@ -67,6 +67,14 @@ public function testEmitDataBigIntOptionWillForwardAsString() $this->input->emit('data', array("999888777666555444333222111000\n")); } + public function testEmitDataWithInvalidTypeWillForwardErrorWithUnexpectedValueException() + { + $this->decoder->on('data', $this->expectCallableNever()); + $this->decoder->on('error', $this->expectCallableOnceWith($this->isInstanceOf('UnexpectedValueException'))); + + $this->input->emit('data', array(false)); + } + public function testEmitDataErrorWillForwardError() { $this->decoder->on('data', $this->expectCallableNever());