diff --git a/docs/1.router.md b/docs/1.router.md index 659d5c19..9054fb75 100755 --- a/docs/1.router.md +++ b/docs/1.router.md @@ -26,7 +26,7 @@ function main(): Unsafe { $server = Server::create()->try($error) or yield $error; - $server->start()->await()->try($error) + $server->start()->try($error) or yield $error; }); } @@ -60,7 +60,7 @@ function main(): Unsafe { $server->router->get('/cats', handler(...))->try($error) or yield $error; - $server->start()->await()->try($error) + $server->start()->try($error) or yield $error; }); } @@ -114,7 +114,7 @@ function main(): Unsafe { $server->router->post('/cats', handler(...))->try($error) or yield $error; - $server->start()->await()->try($error) + $server->start()->try($error) or yield $error; }); } diff --git a/docs/18.open-api.md b/docs/18.open-api.md index b9c3dfa5..11fd38ed 100644 --- a/docs/18.open-api.md +++ b/docs/18.open-api.md @@ -50,7 +50,7 @@ function main():Unsafe { $server->router->get("/openapi", openapi(...))->try($error) or yield $error; - $server->start()->await()->try($error) + $server->start()->try($error) or yield $error; }); } @@ -124,7 +124,7 @@ function main(): Unsafe { $server->router->get('/test/{value}', handler(...))->try($error) or yield $error; - $server->start()->await()->try($error) + $server->start()->try($error) or yield $error; }); } diff --git a/docs/2.path-parameters.md b/docs/2.path-parameters.md index f6e3e276..e3394eb9 100755 --- a/docs/2.path-parameters.md +++ b/docs/2.path-parameters.md @@ -26,7 +26,7 @@ function main(): Unsafe { $server->router->get('/about/{name}', handler(...))->try($error) or yield $error; - $server->start()->await()->try($error) + $server->start()->try($error) or yield $error; }); } @@ -62,7 +62,7 @@ function main(): Unsafe { $server->router->post('/set/age/{age}', handler(...))->try($error) or yield $error; - $server->start()->await()->try($error) + $server->start()->try($error) or yield $error; }); } @@ -97,7 +97,7 @@ function main(): Unsafe { $server->router->get('/about/{name}', handler(...))->try($error) or yield $error; - $server->start()->await()->try($error) + $server->start()->try($error) or yield $error; }); } @@ -128,7 +128,7 @@ function main(): Unsafe { $server->router->get('/about/{name}/child/{childName}', handler(...))->try($error) or yield $error; - $server->start()->await()->try($error) + $server->start()->try($error) or yield $error; }); } diff --git a/docs/4.session.md b/docs/4.session.md index fe368d80..6e1caf69 100755 --- a/docs/4.session.md +++ b/docs/4.session.md @@ -35,7 +35,7 @@ function main(): Unsafe { $server->router->get('/', handler(...))->try($error) or yield $error; - $server->start()->await()->try($error) + $server->start()->try($error) or yield $error; }); } @@ -84,7 +84,7 @@ function main(): Unsafe { $server->router->get('/', handler(...))->try($error) or yield $error; - $server->start()->await()->try($error) + $server->start()->try($error) or yield $error; }); } @@ -117,7 +117,7 @@ function main(): Unsafe { return anyError(function(){ Container::provide(SessionInterface::class, fn(Request $request) => new CustomSession($request)); $server = Server::create()->try($error) or yield $error; - $server->start()->await()->try($error) or yield $error; + $server->start()->try($error) or yield $error; }); } ``` diff --git a/docs/7.byte-range-requests.md b/docs/7.byte-range-requests.md index 1e61f49f..067f5708 100755 --- a/docs/7.byte-range-requests.md +++ b/docs/7.byte-range-requests.md @@ -42,7 +42,7 @@ function main(): Unsafe { $server->router->get('/{fileName}', handler(...))->try($error) or yield $error; - $server->start()->await()->try($error) + $server->start()->try($error) or yield $error; }); } diff --git a/docs/8.custom-attributes.md b/docs/8.custom-attributes.md index 52b31776..56470cc9 100755 --- a/docs/8.custom-attributes.md +++ b/docs/8.custom-attributes.md @@ -34,7 +34,7 @@ function main(): Unsafe { $server->router->get("/", handler(...))->try($error) or yield $error; - $server->start()->await()->try($error) + $server->start()->try($error) or yield $error; }); } diff --git a/src/lib/Core/File.php b/src/lib/Core/File.php index d4909f50..447cedde 100644 --- a/src/lib/Core/File.php +++ b/src/lib/Core/File.php @@ -10,7 +10,6 @@ use function Amp\File\getSize; use function Amp\File\getStatus; use function Amp\File\openFile; -use Amp\Future; use Dotenv\Dotenv; use Error; use Throwable; @@ -66,48 +65,46 @@ public static function checksum(string $fileNameA, string $fileNameB, bool $bina /** * Copy a file. - * @param string $from - * @param string $to - * @return Future> + * @param string $from + * @param string $to + * @return Unsafe */ - public static function copy(string $from, string $to):Future { - return async(static function() use ($from, $to) { - $source = File::open($from)->try($error); - if ($error) { - return error($error); - } + public static function copy(string $from, string $to):Unsafe { + $source = File::open($from)->try($error); + if ($error) { + return error($error); + } - $toDirectory = dirname($to); + $toDirectory = dirname($to); - if (!File::exists($toDirectory)) { - Directory::create($toDirectory)->try($error); - if ($error) { - return error($error); - } + if (!File::exists($toDirectory)) { + Directory::create($toDirectory)->try($error); + if ($error) { + return error($error); } + } - $dirname = dirname($to); + $dirname = dirname($to); - if (false === $dirname) { - Directory::create($dirname)->try($error); - if ($error) { - return $error; - } + if (false === $dirname) { + Directory::create($dirname)->try($error); + if ($error) { + return $error; } + } - if (!File::exists($dirname)) { - } + if (!File::exists($dirname)) { + } - $destination = File::open($to, 'x')->try($error); + $destination = File::open($to, 'x')->try($error); - if ($error) { - return error($error); - } + if ($error) { + return error($error); + } - $stream = $source->getAmpFile(); + $stream = $source->getAmpFile(); - return $destination->writeStream($stream)->await(); - }); + return $destination->writeStream($stream); } @@ -323,51 +320,46 @@ public function truncate(int $size) { } /** - * @param string $content - * @param int $chunkSize - * @return Future> + * @param string $content + * @param int $chunkSize + * @return Unsafe */ - public function write(string $content, int $chunkSize = 8192):Future { - $ampFile = $this->ampFile; - return async(static function() use ($ampFile, $content, $chunkSize) { - try { - $wroteSoFar = 0; - $length = strlen($content); - while (true) { - $step = $wroteSoFar + $chunkSize; - $chunk = substr($content, $wroteSoFar, $step); - async(static fn () => $ampFile->write($chunk))->await(); - $wroteSoFar = $wroteSoFar + $step; - if ($wroteSoFar >= $length) { - return ok(); - } + public function write(string $content, int $chunkSize = 8192):Unsafe { + try { + $wroteSoFar = 0; + $length = strlen($content); + while (true) { + $step = $wroteSoFar + $chunkSize; + $chunk = substr($content, $wroteSoFar, $step); + async(static fn () => $this->ampFile->write($chunk))->await(); + $wroteSoFar = $wroteSoFar + $step; + if ($wroteSoFar >= $length) { + return ok(); } - } catch(Throwable $e) { - return error($e); } - }); + } catch(Throwable $e) { + return error($e); + } } /** - * @param ReadableStream $readableStream - * @param int $chunkSize - * @return Future> + * @param ReadableStream $readableStream + * @param int $chunkSize + * @return Unsafe */ - public function writeStream(ReadableStream $readableStream, int $chunkSize = 8192):Future { + public function writeStream(ReadableStream $readableStream, int $chunkSize = 8192):Unsafe { $ampFile = $this->ampFile; - return async(function() use ($ampFile, $readableStream, $chunkSize) { - try { - while (true) { - $chunk = async(static fn () => $readableStream->read(null, $chunkSize))->await(); - if (null === $chunk) { - return ok(); - } - async(static fn () => $ampFile->write($chunk))->await(); + try { + while (true) { + $chunk = async(static fn () => $readableStream->read(null, $chunkSize))->await(); + if (null === $chunk) { + return ok(); } - } catch(Throwable $e) { - return error($e); + async(static fn () => $ampFile->write($chunk))->await(); } - }); + } catch(Throwable $e) { + return error($e); + } } /** diff --git a/src/lib/Core/GoffiContract.php b/src/lib/Core/GoffiContract.php index f36bae49..4b233007 100644 --- a/src/lib/Core/GoffiContract.php +++ b/src/lib/Core/GoffiContract.php @@ -31,7 +31,7 @@ public static function create(string $interface, string $fileName):Unsafe { $phar = Phar::running(); $headerFileName = './'.basename('.'.str_replace($phar, '', $localHeaderFileName)); if (!File::exists($headerFileName)) { - File::copy($localHeaderFileName, $headerFileName)->await()->try($error); + File::copy($localHeaderFileName, $headerFileName)->try($error); if ($error) { return error($error); } @@ -65,7 +65,7 @@ public static function create(string $interface, string $fileName):Unsafe { $externalFileName = './'.basename('.'.str_replace($phar, '', $fileName)); if (!File::exists($externalFileName)) { - File::copy($fileName, $externalFileName)->await()->try($error); + File::copy($fileName, $externalFileName)->try($error); if ($error) { return error($error); } diff --git a/src/lib/Web/Server.php b/src/lib/Web/Server.php index da920304..ea662cd3 100644 --- a/src/lib/Web/Server.php +++ b/src/lib/Web/Server.php @@ -2,11 +2,9 @@ namespace CatPaw\Web; -use function Amp\async; use Amp\CompositeException; use Amp\DeferredFuture; use function Amp\File\isDirectory; -use Amp\Future; use Amp\Http\Server\HttpServer; use Amp\Http\Server\Middleware; use function Amp\Http\Server\Middleware\stackMiddleware; @@ -229,82 +227,80 @@ public function setFileServer(FileServerInterface $fileServer):self { * Start the server. * * This method will resolve when `::stop` is invoked or one of the following signals is sent to the program `SIGHUP`, `SIGINT`, `SIGQUIT`, `SIGTERM`. - * @param false|Signal $ready the server will trigger this signal whenever it's ready to serve requests. - * @return Future> + * @param false|Signal $ready the server will trigger this signal whenever it's ready to serve requests. + * @return Unsafe */ - public function start(false|Signal $ready = false):Future { - return async(function() use ($ready) { - if (isset($this->httpServer)) { - if ($this->httpServerStarted) { - return error("Server already started."); - } - return error("Server already created."); + public function start(false|Signal $ready = false):Unsafe { + if (isset($this->httpServer)) { + if ($this->httpServerStarted) { + return error("Server already started."); } - $endSignal = new DeferredFuture; - try { - if (!isset($this->fileServer)) { - $fileServer = FileServer::create($this)->try($error); - if ($error) { - return error($error); - } - $this->fileServer = $fileServer; + return error("Server already created."); + } + $endSignal = new DeferredFuture; + try { + if (!isset($this->fileServer)) { + $fileServer = FileServer::create($this)->try($error); + if ($error) { + return error($error); } + $this->fileServer = $fileServer; + } - $stopper = function(string $callbackId) { - EventLoop::cancel($callbackId); - $this->stop(); - Bootstrap::kill(); - }; - - EventLoop::onSignal(SIGHUP, $stopper); - EventLoop::onSignal(SIGINT, $stopper); - EventLoop::onSignal(SIGQUIT, $stopper); - EventLoop::onSignal(SIGTERM, $stopper); - - $requestHandler = ServerRequestHandler::create($this->logger, $this->fileServer, $this->resolver); - $stackedHandler = stackMiddleware(...$this->middleware, ...[$requestHandler]); - $errorHandler = ServerErrorHandler::create($this->logger); - $this->httpServer = SocketHttpServer::createForDirectAccess( - logger: $this->logger, - enableCompression: $this->enableCompression, - connectionLimit: $this->connectionLimit, - connectionLimitPerIp: $this->connectionLimitPerIp, - concurrencyLimit: $this->concurrencyLimit, - allowedMethods: $this->allowedMethods?:null, - ); - - $this->httpServer->onStop(static function() use ($endSignal) { - if (!$endSignal->isComplete()) { - $endSignal->complete(); - } - }); - $this->httpServer->expose($this->interface); - - $this->httpServer->start($stackedHandler, $errorHandler); - $this->httpServerStarted = true; - if ($ready) { - $ready->send(); + $stopper = function(string $callbackId) { + EventLoop::cancel($callbackId); + $this->stop(); + Bootstrap::kill(); + }; + + EventLoop::onSignal(SIGHUP, $stopper); + EventLoop::onSignal(SIGINT, $stopper); + EventLoop::onSignal(SIGQUIT, $stopper); + EventLoop::onSignal(SIGTERM, $stopper); + + $requestHandler = ServerRequestHandler::create($this->logger, $this->fileServer, $this->resolver); + $stackedHandler = stackMiddleware(...$this->middleware, ...[$requestHandler]); + $errorHandler = ServerErrorHandler::create($this->logger); + $this->httpServer = SocketHttpServer::createForDirectAccess( + logger: $this->logger, + enableCompression: $this->enableCompression, + connectionLimit: $this->connectionLimit, + connectionLimitPerIp: $this->connectionLimitPerIp, + concurrencyLimit: $this->concurrencyLimit, + allowedMethods: $this->allowedMethods?:null, + ); + + $this->httpServer->onStop(static function() use ($endSignal) { + if (!$endSignal->isComplete()) { + $endSignal->complete(); } + }); + $this->httpServer->expose($this->interface); - foreach (self::$onStartListeners as $function) { - $result = $function($this->httpServer); - if ($result instanceof Unsafe) { - $result->try($error); - if ($error) { - return error($error); - } + $this->httpServer->start($stackedHandler, $errorHandler); + $this->httpServerStarted = true; + if ($ready) { + $ready->send(); + } + + foreach (self::$onStartListeners as $function) { + $result = $function($this->httpServer); + if ($result instanceof Unsafe) { + $result->try($error); + if ($error) { + return error($error); } } + } - $endSignal->getFuture()->await(); - return ok(); - } catch (Throwable $e) { - if (!$endSignal->isComplete()) { - $endSignal->complete(); - } - return error($e); + $endSignal->getFuture()->await(); + return ok(); + } catch (Throwable $e) { + if (!$endSignal->isComplete()) { + $endSignal->complete(); } - }); + return error($e); + } } /** diff --git a/src/lib/Web/Services/ByteRangeService.php b/src/lib/Web/Services/ByteRangeService.php index 9dcfaa64..6b49b8c6 100755 --- a/src/lib/Web/Services/ByteRangeService.php +++ b/src/lib/Web/Services/ByteRangeService.php @@ -248,7 +248,7 @@ public function send(int $start, int $length):Unsafe { return error("Trying to send payload but the file is not opened."); } $this->file->seek($start); - return $this->file->read($length)->await(); + return $this->file->read($length); } public function close():Unsafe { diff --git a/tests/WebTest.php b/tests/WebTest.php index 98f7bc97..39d58bf9 100755 --- a/tests/WebTest.php +++ b/tests/WebTest.php @@ -57,7 +57,7 @@ interface: '127.0.0.1:5858', } }); - $server->start($readySignal)->await()->try($error); + $server->start($readySignal)->try($error); $this->assertFalse($error); }