Skip to content

Commit a648565

Browse files
authored
Merge pull request #29 from SimonFrings/decoder-types
Check type of incoming `data` before trying to decode NDJSON
2 parents 5869138 + 5f0a150 commit a648565

2 files changed

Lines changed: 13 additions & 0 deletions

File tree

src/Decoder.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,11 @@ public function pipe(WritableStreamInterface $dest, array $options = array())
9999
/** @internal */
100100
public function handleData($data)
101101
{
102+
if (!\is_string($data)) {
103+
$this->handleError(new \UnexpectedValueException('Expected stream to emit string, but got ' . \gettype($data)));
104+
return;
105+
}
106+
102107
$this->buffer .= $data;
103108

104109
// keep parsing while a newline has been found

tests/DecoderTest.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,14 @@ public function testEmitDataBigIntOptionWillForwardAsString()
6767
$this->input->emit('data', array("999888777666555444333222111000\n"));
6868
}
6969

70+
public function testEmitDataWithInvalidTypeWillForwardErrorWithUnexpectedValueException()
71+
{
72+
$this->decoder->on('data', $this->expectCallableNever());
73+
$this->decoder->on('error', $this->expectCallableOnceWith($this->isInstanceOf('UnexpectedValueException')));
74+
75+
$this->input->emit('data', array(false));
76+
}
77+
7078
public function testEmitDataErrorWillForwardError()
7179
{
7280
$this->decoder->on('data', $this->expectCallableNever());

0 commit comments

Comments
 (0)