diff --git a/README.md b/README.md index 2d925ef..27d1a6c 100644 --- a/README.md +++ b/README.md @@ -3,7 +3,7 @@ [![Latest Version on Packagist](https://img.shields.io/packagist/v/medilies/xssless.svg?style=flat-square)](https://packagist.org/packages/medilies/xssless) [![pest](https://img.shields.io/github/actions/workflow/status/medilies/xssless/run-tests.yml?branch=main&label=tests&style=flat-square)](https://github.com/medilies/xssless/actions?query=workflow%3Arun-tests+branch%3Amain) [![phpstan](https://img.shields.io/github/actions/workflow/status/medilies/xssless/phpstan.yml?branch=main&label=phpstan&style=flat-square)](https://github.com/medilies/xssless/actions?query=workflow%3A"phpstan"+branch%3Amain) -[![Total Downloads](https://img.shields.io/packagist/dt/medilies/xssless.svg?style=flat-square)](https://packagist.org/packages/medilies/xssless) + ![workflow](./workflow.png) diff --git a/src/Dompurify/DompurifyCli.php b/src/Dompurify/DompurifyCli.php index 0446e69..bf34ba5 100644 --- a/src/Dompurify/DompurifyCli.php +++ b/src/Dompurify/DompurifyCli.php @@ -32,7 +32,12 @@ public function exec(string $html): string { $htmlFile = $this->saveHtml($html); - $process = new Process([$this->config->node, $this->binPath(), $htmlFile]); + $process = new Process([ + $this->config->node, + $this->config->binary ?? __DIR__.DIRECTORY_SEPARATOR.'cli.js', // ? check file explicitly + $htmlFile, + ]); + $process->mustRun(); $output = $process->getOutput(); @@ -55,19 +60,6 @@ public function exec(string $html): string return $clean; } - private function binPath(): string - { - $binPath = $this->config->binary ?? __DIR__.DIRECTORY_SEPARATOR.'cli.js'; - - $binAbsPath = realpath($binPath); - - if ($binAbsPath === false) { - throw new XsslessException("Cannot locate '$binPath'"); - } - - return $binAbsPath; - } - private function saveHtml(string $value): string { $dir = $this->tempDir(); diff --git a/src/Dompurify/DompurifyService.php b/src/Dompurify/DompurifyService.php index 781e978..3ccc512 100644 --- a/src/Dompurify/DompurifyService.php +++ b/src/Dompurify/DompurifyService.php @@ -16,8 +16,8 @@ class DompurifyService implements HasSetupInterface, ServiceInterface // TODO: better injection (fs and process http) - // TODO: private - public Process $serviceProcess; + private Process $serviceProcess; + // ? add static array for all processes /** @param DompurifyServiceConfig $config */ @@ -73,8 +73,8 @@ public function start(): static $this->serviceProcess->setIdleTimeout(null); - // ? rm check if (! $this->isRunning()) { + // Triggers on bad note path throw new ProcessFailedException($this->serviceProcess); } diff --git a/src/laravel/Commands/StartCommand.php b/src/laravel/Commands/StartCommand.php index 35c1213..1716c1b 100644 --- a/src/laravel/Commands/StartCommand.php +++ b/src/laravel/Commands/StartCommand.php @@ -41,7 +41,6 @@ public function handle(): void pcntl_signal_dispatch(); - // Sleep for a short period to avoid busy-waiting usleep(100_000); } } diff --git a/tests/Dompurify/DompurifyCliTest.php b/tests/Dompurify/DompurifyCliTest.php index 1b26b10..a6c69da 100644 --- a/tests/Dompurify/DompurifyCliTest.php +++ b/tests/Dompurify/DompurifyCliTest.php @@ -6,6 +6,10 @@ use Medilies\Xssless\Xssless; use Symfony\Component\Process\Exception\ProcessFailedException; +// ---------------------------------------------------------------------------- +// Errors and mocked binaries +// ---------------------------------------------------------------------------- + it('throws on bad node path', function () { $cleaner = (new DompurifyCli)->configure(new DompurifyCliConfig( node: 'nodeZz', @@ -19,7 +23,7 @@ binary: __DIR__.'/js-mocks/x.js', )); - expect(fn () => $cleaner->exec('foo'))->toThrow(XsslessException::class); + expect(fn () => $cleaner->exec('foo'))->toThrow(ProcessFailedException::class); }); it('throws when cannot locate temp folder', function () { @@ -30,6 +34,18 @@ expect(fn () => $cleaner->exec('foo'))->toThrow(XsslessException::class); }); +it('throws when cannot read cleaned file', function () { + $cleaner = (new DompurifyCli)->configure(new DompurifyCliConfig( + binary: __DIR__.'/js-mocks/cli-returns-bad-path.js', + )); + + expect(fn () => $cleaner->exec('foo'))->toThrow(XsslessException::class); +}); + +// ---------------------------------------------------------------------------- +// Real setup and clean +// ---------------------------------------------------------------------------- + test('setup()', function () { $cleaner = (new Xssless)->using(new DompurifyCliConfig); @@ -53,11 +69,3 @@ expect($clean)->toBe('">'); })->depends('setup()'); - -it('throws when cannot read cleaned file', function () { - $cleaner = (new DompurifyCli)->configure(new DompurifyCliConfig( - binary: __DIR__.'/js-mocks/cli-returns-bad-path.js', - )); - - expect(fn () => $cleaner->exec('foo'))->toThrow(XsslessException::class); -}); diff --git a/tests/Dompurify/DompurifyServiceTest.php b/tests/Dompurify/DompurifyServiceTest.php index 95747ff..4cde14e 100644 --- a/tests/Dompurify/DompurifyServiceTest.php +++ b/tests/Dompurify/DompurifyServiceTest.php @@ -7,6 +7,10 @@ use Symfony\Component\Process\Exception\ProcessFailedException; use Symfony\Component\Process\Exception\ProcessTimedOutException; +// ---------------------------------------------------------------------------- +// Errors and mocked binaries +// ---------------------------------------------------------------------------- + it('throws on bad node path', function () { $service = (new DompurifyService)->configure(new DompurifyServiceConfig( node: 'nodeZz', @@ -42,6 +46,10 @@ expect(fn () => $cleaner->send($dirty))->toThrow(ConnectException::class); }); +// ---------------------------------------------------------------------------- +// Real setup and clean +// ---------------------------------------------------------------------------- + test('setup()', function () { $cleaner = (new DompurifyService)->configure(new DompurifyServiceConfig);