diff --git a/Dockerfile b/Dockerfile index c73da28..826d4c8 100755 --- a/Dockerfile +++ b/Dockerfile @@ -22,7 +22,6 @@ RUN \ gcc \ g++ \ git \ - linux-headers \ openssl-dev \ curl-dev diff --git a/app/http.php b/app/http.php index d28d90c..b11503c 100644 --- a/app/http.php +++ b/app/http.php @@ -1040,7 +1040,13 @@ function (string $runtimeId, ?string $payload, string $path, string $method, mix 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) { @@ -1049,6 +1055,7 @@ function (string $runtimeId, ?string $payload, string $path, string $method, mix $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 ef91f4c..68ee908 100644 --- a/tests/ExecutorTest.php +++ b/tests/ExecutorTest.php @@ -644,6 +644,17 @@ public function provideScenarios(): array 'logging' => true, 'mimeType' => 'multipart/form-data' ], + [ + '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