Skip to content

Commit

Permalink
Merge branch 'develop'
Browse files Browse the repository at this point in the history
* develop:
  specify next release
  fix reading frames when reaching stream end
  • Loading branch information
Baptouuuu committed Nov 25, 2023
2 parents 9f75a40 + 38db641 commit 1973c26
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 0 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# Changelog

## 2.3.1 - 2023-10-25

### Fixed

- Fix reading frames when reading triggers the stream end

## 2.3.0 - 2023-10-25

### Added
Expand Down
6 changes: 6 additions & 0 deletions src/Readable/Frames.php
Original file line number Diff line number Diff line change
Expand Up @@ -79,12 +79,18 @@ public function one(): Maybe
*/
$read = fn(?int $size): Maybe => ($this->ready)($this->stream)
->flatMap(static fn($stream) => $stream->read($size))
->otherwise(fn() => Maybe::just(Str::of(''))->filter(
fn() => $this->stream->end(),
))
->map(fn($chunk) => $this->encoding->match(
static fn($encoding) => $chunk->toEncoding($encoding),
static fn() => $chunk,
));
$readLine = fn(): Maybe => ($this->ready)($this->stream)
->flatMap(static fn($stream) => $stream->readLine())
->otherwise(fn() => Maybe::just(Str::of(''))->filter(
fn() => $this->stream->end(),
))
->map(fn($chunk) => $this->encoding->match(
static fn($encoding) => $chunk->toEncoding($encoding),
static fn() => $chunk,
Expand Down
38 changes: 38 additions & 0 deletions tests/FunctionalTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -435,4 +435,42 @@ public function testFilterOutFrame()

$this->assertNull($payload);
}

public function testReadingSequenceOfLineFrames()
{
$someStream = <<<RAW
--PAYLOAD START--
some payload A
--PAYLOAD END--
RAW;

$stream = Stream::ofContent($someStream);
$payload = IO::of(Select::waitForever(...))
->readable()
->wrap($stream)
->toEncoding(Str\Encoding::ascii)
->watch()
->frames(Frame\Sequence::of(
Frame\Line::new(),
)->until(static fn($line) => $line->empty()))
->one()
->match(
static fn($payload) => $payload,
static fn() => null,
);

$this->assertNotNull($payload);
$this->assertSame(
[
"--PAYLOAD START--\n",
"some payload A\n",
"--PAYLOAD END--\n",
'',
],
$payload
->map(static fn($line) => $line->toString())
->toList(),
);
}
}

0 comments on commit 1973c26

Please sign in to comment.