-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add straming support on JSON RPC parser
Add event streaming capabilities on the JSON RPC parser, so that operations such as StartLiveTail from CloudwatchLogs are properly handled/parsed. This change also includes an extension of the DecodingEventStreamIterator for handling streamable responses where the stream as response is not seekable, and a handling for when the `event-type` is `initial-response`.
- Loading branch information
1 parent
2fd8cae
commit 3697717
Showing
10 changed files
with
618 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
[ | ||
{ | ||
"type": "feature", | ||
"category": "Parser", | ||
"description": "Adds support for event streaming on JSON-RPC protocol parser" | ||
} | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
101 changes: 101 additions & 0 deletions
101
src/Api/Parser/NonSeekableStreamDecodingEventStreamIterator.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
<?php | ||
|
||
namespace Aws\Api\Parser; | ||
|
||
use GuzzleHttp\Psr7; | ||
use Psr\Http\Message\StreamInterface; | ||
use Aws\Api\Parser\Exception\ParserException; | ||
|
||
/** | ||
* @inheritDoc | ||
*/ | ||
class NonSeekableStreamDecodingEventStreamIterator extends DecodingEventStreamIterator | ||
{ | ||
/** @var array $tempBuffer */ | ||
private $tempBuffer; | ||
|
||
/** | ||
* NonSeekableStreamDecodingEventStreamIterator constructor. | ||
* | ||
* @param StreamInterface $stream | ||
*/ | ||
public function __construct(StreamInterface $stream) | ||
{ | ||
$this->stream = $stream; | ||
if ($this->stream->isSeekable()) { | ||
throw new \InvalidArgumentException('The stream provided must be not seekable.'); | ||
} | ||
|
||
$this->tempBuffer = []; | ||
} | ||
|
||
/** | ||
* @inheritDoc | ||
* | ||
* @return array | ||
*/ | ||
protected function parseEvent(): array | ||
{ | ||
$event = []; | ||
$this->hashContext = hash_init('crc32b'); | ||
$prelude = $this->parsePrelude()[0]; | ||
list( | ||
$event[self::HEADERS], | ||
$numBytes | ||
) = $this->parseHeaders($prelude[self::LENGTH_HEADERS]); | ||
$event[self::PAYLOAD] = Psr7\Utils::streamFor( | ||
$this->readAndHashBytes( | ||
$prelude[self::LENGTH_TOTAL] - self::BYTES_PRELUDE | ||
- $numBytes - self::BYTES_TRAILING | ||
) | ||
); | ||
$calculatedCrc = hash_final($this->hashContext, true); | ||
$messageCrc = $this->stream->read(4); | ||
if ($calculatedCrc !== $messageCrc) { | ||
throw new ParserException('Message checksum mismatch.'); | ||
} | ||
|
||
return $event; | ||
} | ||
|
||
protected function readAndHashBytes($num): string | ||
{ | ||
$bytes = ''; | ||
while (!empty($this->tempBuffer) && $num > 0) { | ||
$byte = array_shift($this->tempBuffer); | ||
$bytes .= $byte; | ||
$num = $num - 1; | ||
} | ||
|
||
$bytes = $bytes . $this->stream->read($num); | ||
hash_update($this->hashContext, $bytes); | ||
|
||
return $bytes; | ||
} | ||
|
||
// Iterator Functionality | ||
|
||
#[\ReturnTypeWillChange] | ||
public function rewind() | ||
{ | ||
$this->currentEvent = $this->parseEvent(); | ||
} | ||
|
||
public function next() | ||
{ | ||
$this->tempBuffer[] = $this->stream->read(1); | ||
if ($this->valid()) { | ||
$this->key++; | ||
$this->currentEvent = $this->parseEvent(); | ||
} | ||
} | ||
|
||
/** | ||
* @return bool | ||
*/ | ||
#[\ReturnTypeWillChange] | ||
public function valid() | ||
{ | ||
return !$this->stream->eof(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.