From a71ea7928b0f76016cd01c9c4db8b8a56f94befe Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Matej=20Ba=C4=8Do?= Date: Tue, 25 Jun 2024 14:43:25 +0000 Subject: [PATCH 1/2] Fix chunked response --- Dockerfile | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/Dockerfile b/Dockerfile index 8aa5961..347bbbb 100755 --- a/Dockerfile +++ b/Dockerfile @@ -22,7 +22,8 @@ RUN \ gcc \ g++ \ git \ - openssl-dev + openssl-dev \ + curl-dev RUN docker-php-ext-install sockets @@ -33,7 +34,7 @@ RUN \ git clone --depth 1 --branch $PHP_SWOOLE_VERSION https://github.com/swoole/swoole-src.git && \ cd swoole-src && \ phpize && \ - ./configure --enable-sockets --enable-http2 --enable-openssl && \ + ./configure --enable-sockets --enable-http2 --enable-swoole-curl --enable-openssl && \ make && make install && \ cd .. From 991b29ca296a6a8f20e679fff383688afb7c52c9 Mon Sep 17 00:00:00 2001 From: Khushboo Verma <43381712+vermakhushboo@users.noreply.github.com> Date: Wed, 26 Jun 2024 16:17:47 +0530 Subject: [PATCH 2/2] Add support for chunked responses --- app/http.php | 9 ++++++++- tests/ExecutorTest.php | 11 +++++++++++ .../functions/node-streamed-response/index.js | 7 +++++++ .../functions/node-streamed-response/package.json | 12 ++++++++++++ 4 files changed, 38 insertions(+), 1 deletion(-) create mode 100644 tests/resources/functions/node-streamed-response/index.js create mode 100644 tests/resources/functions/node-streamed-response/package.json diff --git a/app/http.php b/app/http.php index 5c9ff44..90c5319 100644 --- a/app/http.php +++ b/app/http.php @@ -995,7 +995,13 @@ function (string $runtimeId, ?string $payload, string $path, string $method, arr return $len; }); - + $callback = function ($data) { + var_dump($data); // This will output each chunk of data as it arrives + }; + \curl_setopt($ch, CURLOPT_WRITEFUNCTION, function ($ch, $data) use ($callback) { + $callback($data); + return \strlen($data); + }); \curl_setopt($ch, CURLOPT_TIMEOUT, $timeout + 5); // Gives extra 5s after safe timeout to recieve response \curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5); if ($logging == true) { @@ -1004,6 +1010,7 @@ function (string $runtimeId, ?string $payload, string $path, string $method, arr $headers['x-open-runtimes-logging'] = 'disabled'; } + $headers['accept'] = 'text/event-stream'; $headers['x-open-runtimes-secret'] = $secret; $headers['x-open-runtimes-timeout'] = \max(\intval($timeout), 1); $headersArr = []; diff --git a/tests/ExecutorTest.php b/tests/ExecutorTest.php index d0a0662..9f33626 100644 --- a/tests/ExecutorTest.php +++ b/tests/ExecutorTest.php @@ -463,6 +463,17 @@ public function provideScenarios(): array return 1; }, ], + [ + 'image' => 'openruntimes/node:v4-21.0', + 'entrypoint' => 'index.js', + 'folder' => 'node-streamed-response', + 'version' => 'v4', + 'startCommand' => 'cp /tmp/code.tar.gz /mnt/code/code.tar.gz && nohup helpers/start.sh "pm2 start src/server.js --no-daemon"', + 'buildCommand' => 'tar -zxf /tmp/code.tar.gz -C /mnt/code && helpers/build.sh "npm i"', + 'assertions' => function ($response) { + $this->assertEquals(200, $response['headers']['status-code']); + }, + ] ]; } diff --git a/tests/resources/functions/node-streamed-response/index.js b/tests/resources/functions/node-streamed-response/index.js new file mode 100644 index 0000000..a9d3946 --- /dev/null +++ b/tests/resources/functions/node-streamed-response/index.js @@ -0,0 +1,7 @@ +module.exports = async (context) => { + context.res.start(); + context.res.writeText('OK1'); + await new Promise(resolve => setTimeout(resolve, 5000)); + context.res.writeText('OK2'); + return context.res.end(); +}; \ No newline at end of file diff --git a/tests/resources/functions/node-streamed-response/package.json b/tests/resources/functions/node-streamed-response/package.json new file mode 100644 index 0000000..8f2439f --- /dev/null +++ b/tests/resources/functions/node-streamed-response/package.json @@ -0,0 +1,12 @@ +{ + "name": "streamed-response", + "version": "1.0.0", + "description": "", + "main": "index.js", + "scripts": { + "test": "echo \"Error: no test specified\" && exit 1" + }, + "keywords": [], + "author": "", + "license": "ISC" + } \ No newline at end of file