Skip to content

Commit cfd3dcb

Browse files
committed
Pass codec to worker, allow spiral/roadrunner 2024
1 parent 7ea1cff commit cfd3dcb

File tree

2 files changed

+21
-17
lines changed

2 files changed

+21
-17
lines changed

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@
4141
"ext-json": "*",
4242
"psr/http-factory": "^1.0.1",
4343
"psr/http-message": "^1.0.1 || ^2.0",
44-
"spiral/roadrunner": "^2023.3",
44+
"spiral/roadrunner": "^2023.3 || ^2024.1",
4545
"spiral/roadrunner-worker": "^3.1",
4646
"roadrunner-php/roadrunner-api-dto": "^1.4",
4747
"symfony/polyfill-php83": "^1.29"

src/HttpWorker.php

Lines changed: 20 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
use RoadRunner\HTTP\DTO\V1BETA1\HeaderValue;
1010
use RoadRunner\HTTP\DTO\V1BETA1\Request as RequestProto;
1111
use RoadRunner\HTTP\DTO\V1BETA1\Response;
12+
use Spiral\Goridge\Frame;
1213
use Spiral\RoadRunner\Http\Exception\StreamStoppedException;
1314
use Spiral\RoadRunner\Message\Command\StreamStop;
1415
use Spiral\RoadRunner\Payload;
@@ -38,7 +39,7 @@
3839
*/
3940
class HttpWorker implements HttpWorkerInterface
4041
{
41-
private static ?bool $isProto = null;
42+
private static ?int $codec = null;
4243

4344
public function __construct(
4445
private readonly WorkerInterface $worker,
@@ -62,7 +63,11 @@ public function waitRequest(): ?Request
6263
return null;
6364
}
6465

65-
if ($this->isProtoPayload($payload)) {
66+
if (static::$codec === null) {
67+
static::$codec = json_validate($payload->header) ? Frame::CODEC_JSON : Frame::CODEC_PROTO;
68+
}
69+
70+
if (static::$codec === Frame::CODEC_PROTO) {
6671
$message = new RequestProto();
6772
$message->mergeFromString($payload->header);
6873

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

93-
$this->worker->respond($this->createRespondPayload($status, $body, $headers, $endOfStream));
98+
/** @psalm-suppress TooManyArguments */
99+
$this->worker->respond($this->createRespondPayload($status, $body, $headers, $endOfStream), static::$codec);
94100
}
95101

96102
/**
@@ -110,7 +116,11 @@ private function respondStream(int $status, Generator $body, array $headers = []
110116
// We don't need to send an empty frame if the stream is not ended
111117
return;
112118
}
113-
$worker->respond($this->createRespondPayload($status, $content, $headers, $endOfStream));
119+
/** @psalm-suppress TooManyArguments */
120+
$worker->respond(
121+
$this->createRespondPayload($status, $content, $headers, $endOfStream),
122+
static::$codec
123+
);
114124
break;
115125
}
116126

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

127-
// Send a chunk of data
128-
$worker->respond($this->createRespondPayload($status, $content, $headers, false));
137+
/**
138+
* Send a chunk of data
139+
* @psalm-suppress TooManyArguments
140+
*/
141+
$worker->respond($this->createRespondPayload($status, $content, $headers, false), static::$codec);
129142

130143
try {
131144
$body->next();
@@ -260,20 +273,11 @@ private function arrayToHeaderValue(array $headers = []): array
260273
*/
261274
private function createRespondPayload(int $status, string $body, array $headers = [], bool $eos = true): Payload
262275
{
263-
$head = static::$isProto
276+
$head = static::$codec === Frame::CODEC_PROTO
264277
? (new Response(['status' => $status, 'headers' => $this->arrayToHeaderValue($headers)]))
265278
->serializeToString()
266279
: \json_encode(['status' => $status, 'headers' => $headers ?: (object)[]], \JSON_THROW_ON_ERROR);
267280

268281
return new Payload(body: $body, header: $head, eos: $eos);
269282
}
270-
271-
private function isProtoPayload(Payload $payload): bool
272-
{
273-
if (static::$isProto === null) {
274-
static::$isProto = !json_validate($payload->header);
275-
}
276-
277-
return static::$isProto;
278-
}
279283
}

0 commit comments

Comments
 (0)