diff --git a/src/ChildProcess/Adapter.php b/src/ChildProcess/Adapter.php index 45fdd484..d1ebdee6 100644 --- a/src/ChildProcess/Adapter.php +++ b/src/ChildProcess/Adapter.php @@ -195,7 +195,7 @@ public function read($fileDescriptor, $length, $offset) 'length' => $length, 'offset' => $offset, ]))->then(function ($payload) { - return \React\Promise\resolve($payload['chunk']); + return \React\Promise\resolve(base64_decode($payload['chunk'])); }); } @@ -209,7 +209,7 @@ public function read($fileDescriptor, $length, $offset) public function write($fileDescriptor, $data, $length, $offset) { return $this->fileDescriptors[$fileDescriptor]->rpc(Factory::rpc('write', [ - 'chunk' => $data, + 'chunk' => base64_encode($data), 'length' => $length, 'offset' => $offset, ])); diff --git a/src/WoolTrait.php b/src/WoolTrait.php index 497a7e38..d786b6b4 100644 --- a/src/WoolTrait.php +++ b/src/WoolTrait.php @@ -172,7 +172,7 @@ public function read(array $payload) { fseek($this->fd, $payload['offset']); return \React\Promise\resolve([ - 'chunk' => fread($this->fd, $payload['length']), + 'chunk' => base64_encode(fread($this->fd, $payload['length'])), ]); } @@ -184,7 +184,7 @@ public function write(array $payload) { fseek($this->fd, $payload['offset']); return \React\Promise\resolve([ - 'written' => fwrite($this->fd, $payload['chunk'], $payload['length']), + 'written' => fwrite($this->fd, base64_decode($payload['chunk']), $payload['length']), ]); } diff --git a/tests/Adapters/FileTest.php b/tests/Adapters/FileTest.php index ce26747a..f0e43fe0 100644 --- a/tests/Adapters/FileTest.php +++ b/tests/Adapters/FileTest.php @@ -151,6 +151,17 @@ public function testGetContents(LoopInterface $loop, FilesystemInterface $filesy $this->assertSame($contents, $fileContents); } + /** + * @dataProvider filesystemProvider + */ + public function testGetBinaryContents(LoopInterface $loop, FilesystemInterface $filesystem) + { + $file = __DIR__ . DIRECTORY_SEPARATOR . 'reactphp-logo.png'; + $this->assertFileExists($file); + $fileContents = $this->await($filesystem->file($file)->getContents(), $loop); + $this->assertSame(file_get_contents($file), $fileContents); + } + /** * @dataProvider filesystemProvider */ @@ -257,4 +268,16 @@ public function testPutContents(LoopInterface $loop, FilesystemInterface $filesy $this->assertFileExists($tempFile); $this->assertSame($contents, file_get_contents($tempFile)); } + /** + * @dataProvider filesystemProvider + */ + public function testPutBinaryContents(LoopInterface $loop, FilesystemInterface $filesystem) + { + $contents = file_get_contents(__DIR__ . DIRECTORY_SEPARATOR . 'reactphp-logo.png'); + $filename = uniqid('', true); + $tempFile = $this->tmpDir . $filename; + $this->await($filesystem->file($tempFile)->putContents($contents), $loop); + $this->assertFileExists($tempFile); + $this->assertSame($contents, file_get_contents($tempFile)); + } } diff --git a/tests/Adapters/reactphp-logo.png b/tests/Adapters/reactphp-logo.png new file mode 100644 index 00000000..001a8c8d Binary files /dev/null and b/tests/Adapters/reactphp-logo.png differ