Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: RoadRunner streaming through generators #939

Merged
merged 2 commits into from
Aug 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions src/RoadRunner/RoadRunnerClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Laravel\Octane\RoadRunner;

use Generator;
use Illuminate\Foundation\Application;
use Illuminate\Http\Request;
use Laravel\Octane\Contracts\Client;
Expand All @@ -10,6 +11,7 @@
use Laravel\Octane\Octane;
use Laravel\Octane\OctaneResponse;
use Laravel\Octane\RequestContext;
use ReflectionFunction;
use Spiral\RoadRunner\Http\PSR7Worker;
use Symfony\Component\HttpFoundation\BinaryFileResponse;
use Symfony\Component\HttpFoundation\StreamedResponse;
Expand Down Expand Up @@ -47,6 +49,20 @@ public function respond(RequestContext $context, OctaneResponse $octaneResponse)
);
}

if (
($octaneResponse->response instanceof StreamedResponse) &&
($responseCallback = $octaneResponse->response->getCallback()) &&
((new ReflectionFunction($responseCallback))->getReturnType()?->getName() === Generator::class)
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This could be:

((new ReflectionFunction($responseCallback))->isGenerator()

a typehint would not be needed to know if it's a generator or not

) {
$this->client->getHttpWorker()->respond(
$octaneResponse->response->getStatusCode(),
$responseCallback(),
$this->toPsr7Response($octaneResponse->response)->getHeaders(),
);

return;
}

$this->client->respond($this->toPsr7Response($octaneResponse->response));
}

Expand Down
29 changes: 29 additions & 0 deletions tests/RoadRunnerClientTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
namespace Laravel\Octane\Tests;

use Exception;
use Generator;
use Hamcrest\Core\IsInstanceOf;
use Illuminate\Http\Request;
use Illuminate\Http\Response;
use Laminas\Diactoros\ServerRequestFactory;
Expand All @@ -11,6 +13,7 @@
use Laravel\Octane\RoadRunner\RoadRunnerClient;
use Mockery;
use Psr\Http\Message\ResponseInterface;
use Spiral\RoadRunner\Http\HttpWorker;
use Spiral\RoadRunner\Http\PSR7Worker;
use Symfony\Component\HttpFoundation\StreamedResponse;

Expand Down Expand Up @@ -63,6 +66,32 @@ public function test_respond_method_send_streamed_response_to_roadrunner()
}, 200)));
}

/** @doesNotPerformAssertions @test */
public function test_respond_method_send_streamed_generator_response_to_roadrunner()
{
$client = new RoadRunnerClient($psr7Client = Mockery::mock(PSR7Worker::class));

$psr7Request = (new ServerRequestFactory)->createServerRequest('GET', '/home');
$psr7Request = $psr7Request->withQueryParams(['name' => 'Taylor']);

$httpWorker = Mockery::mock(HttpWorker::class);

$responseCallback = function (): Generator {
yield 'Hello World';
};

$psr7Client->shouldReceive('getHttpWorker')->once()->andReturn($httpWorker);
$httpWorker->shouldReceive('respond')->once()->with(
200,
IsInstanceOf::anInstanceOf(Generator::class),
Mockery::hasKey('cache-control'),
);

$client->respond(new RequestContext([
'psr7Request' => $psr7Request,
]), new OctaneResponse(new StreamedResponse($responseCallback, 200)));
}

/** @doesNotPerformAssertions @test */
public function test_error_method_sends_error_response_to_roadrunner()
{
Expand Down