Skip to content

Commit

Permalink
Merge pull request #29 from SimonFrings/decoder-types
Browse files Browse the repository at this point in the history
Check type of incoming `data` before trying to decode NDJSON
  • Loading branch information
clue authored May 10, 2022
2 parents 5869138 + 5f0a150 commit a648565
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 0 deletions.
5 changes: 5 additions & 0 deletions src/Decoder.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
8 changes: 8 additions & 0 deletions tests/DecoderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand Down

0 comments on commit a648565

Please sign in to comment.