Skip to content

Commit 0921caa

Browse files
committed
chunk size 256 => 131072, and make it changeable
1 parent 9936274 commit 0921caa

File tree

4 files changed

+11
-5
lines changed

4 files changed

+11
-5
lines changed

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@
6969
"@test:cs"
7070
],
7171
"test:cs": "mkdir -p build && vendor/bin/php-cs-fixer fix --dry-run --stop-on-violation --cache-file=build/phpcs.cache",
72-
"test:infection": "vendor/bin/infection --threads=$(nproc) --min-msi=96 --verbose --coverage=build/phpunit",
72+
"test:infection": "vendor/bin/infection --threads=$(nproc) --min-msi=100 --verbose --coverage=build/phpunit",
7373
"test:integration": "vendor/bin/phpunit --testsuite=Integration --cache-directory=build/phpunit",
7474
"test:lint": "mkdir -p build && find src tests -name '*.php' -print0 | xargs -0 -n1 -P$(nproc) php -l | tee build/phplint.log",
7575
"test:static-analysis": "mkdir -p build && bash -c 'vendor/bin/phpstan analyse src --no-progress --level=8 --error-format=junit | tee build/phpstan.junit.xml; if [ ${PIPESTATUS[0]} -ne \"0\" ]; then exit 1; fi'",

src/SwooleResponseEmitter.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,10 @@
1111

1212
final class SwooleResponseEmitter implements SwooleResponseEmitterInterface
1313
{
14+
public function __construct(
15+
private int $chunkSize = self::DEFAULT_CHUNK_SIZE
16+
) {}
17+
1418
public function emit(ResponseInterface $response, SwooleResponse $swooleResponse): void
1519
{
1620
$swooleResponse->status($response->getStatusCode(), $response->getReasonPhrase());
@@ -49,7 +53,7 @@ private function mapSameSite(SetCookie $cookie): string
4953
return '';
5054
}
5155

52-
return str_replace('SameSite=', '', $sameSite->asString());
56+
return substr($sameSite->asString(), 9);
5357
}
5458

5559
private function mapBody(ResponseInterface $response, SwooleResponse $swooleResponse): void
@@ -61,7 +65,7 @@ private function mapBody(ResponseInterface $response, SwooleResponse $swooleResp
6165
}
6266

6367
while (!$body->eof()) {
64-
if ('' !== $chunk = $body->read(256)) {
68+
if ('' !== $chunk = $body->read($this->chunkSize)) {
6569
$swooleResponse->write($chunk);
6670
}
6771
}

src/SwooleResponseEmitterInterface.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,5 +9,7 @@
99

1010
interface SwooleResponseEmitterInterface
1111
{
12+
public const int DEFAULT_CHUNK_SIZE = 131072;
13+
1214
public function emit(ResponseInterface $response, SwooleResponse $swooleResponse): void;
1315
}

tests/Unit/SwooleResponseEmitterTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ public function testInvoke(): void
3636
new WithReturn('isSeekable', [], true),
3737
new WithoutReturn('rewind', []),
3838
new WithReturn('eof', [], false),
39-
new WithReturn('read', [256], 'This is the body.'),
39+
new WithReturn('read', [131072], 'This is the body.'),
4040
new WithReturn('eof', [], true),
4141
]);
4242

@@ -113,7 +113,7 @@ public function testInvokeWithEmptyBody(): void
113113
new WithReturn('isSeekable', [], true),
114114
new WithoutReturn('rewind', []),
115115
new WithReturn('eof', [], false),
116-
new WithReturn('read', [256], ''),
116+
new WithReturn('read', [131072], ''),
117117
new WithReturn('eof', [], true),
118118
]);
119119

0 commit comments

Comments
 (0)