Skip to content

Commit

Permalink
Pass codec to worker, allow spiral/roadrunner 2024
Browse files Browse the repository at this point in the history
  • Loading branch information
msmakouz committed Mar 11, 2024
1 parent 7ea1cff commit cfd3dcb
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 17 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@
"ext-json": "*",
"psr/http-factory": "^1.0.1",
"psr/http-message": "^1.0.1 || ^2.0",
"spiral/roadrunner": "^2023.3",
"spiral/roadrunner": "^2023.3 || ^2024.1",
"spiral/roadrunner-worker": "^3.1",
"roadrunner-php/roadrunner-api-dto": "^1.4",
"symfony/polyfill-php83": "^1.29"
Expand Down
36 changes: 20 additions & 16 deletions src/HttpWorker.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use RoadRunner\HTTP\DTO\V1BETA1\HeaderValue;
use RoadRunner\HTTP\DTO\V1BETA1\Request as RequestProto;
use RoadRunner\HTTP\DTO\V1BETA1\Response;
use Spiral\Goridge\Frame;
use Spiral\RoadRunner\Http\Exception\StreamStoppedException;
use Spiral\RoadRunner\Message\Command\StreamStop;
use Spiral\RoadRunner\Payload;
Expand Down Expand Up @@ -38,7 +39,7 @@
*/
class HttpWorker implements HttpWorkerInterface
{
private static ?bool $isProto = null;
private static ?int $codec = null;

public function __construct(
private readonly WorkerInterface $worker,
Expand All @@ -62,7 +63,11 @@ public function waitRequest(): ?Request
return null;
}

if ($this->isProtoPayload($payload)) {
if (static::$codec === null) {
static::$codec = json_validate($payload->header) ? Frame::CODEC_JSON : Frame::CODEC_PROTO;

Check warning on line 67 in src/HttpWorker.php

View check run for this annotation

Codecov / codecov/patch

src/HttpWorker.php#L66-L67

Added lines #L66 - L67 were not covered by tests
}

if (static::$codec === Frame::CODEC_PROTO) {
$message = new RequestProto();
$message->mergeFromString($payload->header);

Check warning on line 72 in src/HttpWorker.php

View check run for this annotation

Codecov / codecov/patch

src/HttpWorker.php#L70-L72

Added lines #L70 - L72 were not covered by tests

Expand Down Expand Up @@ -90,7 +95,8 @@ public function respond(int $status, string|Generator $body = '', array $headers
return;
}

$this->worker->respond($this->createRespondPayload($status, $body, $headers, $endOfStream));
/** @psalm-suppress TooManyArguments */
$this->worker->respond($this->createRespondPayload($status, $body, $headers, $endOfStream), static::$codec);

Check warning on line 99 in src/HttpWorker.php

View check run for this annotation

Codecov / codecov/patch

src/HttpWorker.php#L99

Added line #L99 was not covered by tests
}

/**
Expand All @@ -110,7 +116,11 @@ private function respondStream(int $status, Generator $body, array $headers = []
// We don't need to send an empty frame if the stream is not ended
return;
}
$worker->respond($this->createRespondPayload($status, $content, $headers, $endOfStream));
/** @psalm-suppress TooManyArguments */
$worker->respond(
$this->createRespondPayload($status, $content, $headers, $endOfStream),
static::$codec
);
break;
}

Expand All @@ -124,8 +134,11 @@ private function respondStream(int $status, Generator $body, array $headers = []
return;
}

// Send a chunk of data
$worker->respond($this->createRespondPayload($status, $content, $headers, false));
/**
* Send a chunk of data
* @psalm-suppress TooManyArguments
*/
$worker->respond($this->createRespondPayload($status, $content, $headers, false), static::$codec);

try {
$body->next();
Expand Down Expand Up @@ -260,20 +273,11 @@ private function arrayToHeaderValue(array $headers = []): array
*/
private function createRespondPayload(int $status, string $body, array $headers = [], bool $eos = true): Payload
{
$head = static::$isProto
$head = static::$codec === Frame::CODEC_PROTO
? (new Response(['status' => $status, 'headers' => $this->arrayToHeaderValue($headers)]))
->serializeToString()

Check warning on line 278 in src/HttpWorker.php

View check run for this annotation

Codecov / codecov/patch

src/HttpWorker.php#L277-L278

Added lines #L277 - L278 were not covered by tests
: \json_encode(['status' => $status, 'headers' => $headers ?: (object)[]], \JSON_THROW_ON_ERROR);

return new Payload(body: $body, header: $head, eos: $eos);
}

private function isProtoPayload(Payload $payload): bool
{
if (static::$isProto === null) {
static::$isProto = !json_validate($payload->header);
}

return static::$isProto;
}
}

0 comments on commit cfd3dcb

Please sign in to comment.