Skip to content

Commit

Permalink
cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
medilies committed Aug 12, 2024
1 parent 720d19d commit ce373d6
Show file tree
Hide file tree
Showing 6 changed files with 35 additions and 28 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
<!-- [![Total Downloads](https://img.shields.io/packagist/dt/medilies/xssless.svg?style=flat-square)](https://packagist.org/packages/medilies/xssless) -->

![workflow](./workflow.png)

Expand Down
20 changes: 6 additions & 14 deletions src/Dompurify/DompurifyCli.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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();
Expand Down
6 changes: 3 additions & 3 deletions src/Dompurify/DompurifyService.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 */
Expand Down Expand Up @@ -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);
}

Expand Down
1 change: 0 additions & 1 deletion src/laravel/Commands/StartCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@ public function handle(): void

pcntl_signal_dispatch();

// Sleep for a short period to avoid busy-waiting
usleep(100_000);
}
}
Expand Down
26 changes: 17 additions & 9 deletions tests/Dompurify/DompurifyCliTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand All @@ -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 () {
Expand All @@ -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);

Expand All @@ -53,11 +69,3 @@

expect($clean)->toBe('<img>"&gt;');
})->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);
});
8 changes: 8 additions & 0 deletions tests/Dompurify/DompurifyServiceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down Expand Up @@ -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);

Expand Down

0 comments on commit ce373d6

Please sign in to comment.