Skip to content

Commit ce373d6

Browse files
committed
cleanup
1 parent 720d19d commit ce373d6

File tree

6 files changed

+35
-28
lines changed

6 files changed

+35
-28
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
[![Latest Version on Packagist](https://img.shields.io/packagist/v/medilies/xssless.svg?style=flat-square)](https://packagist.org/packages/medilies/xssless)
44
[![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)
55
[![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)
6-
[![Total Downloads](https://img.shields.io/packagist/dt/medilies/xssless.svg?style=flat-square)](https://packagist.org/packages/medilies/xssless)
6+
<!-- [![Total Downloads](https://img.shields.io/packagist/dt/medilies/xssless.svg?style=flat-square)](https://packagist.org/packages/medilies/xssless) -->
77

88
![workflow](./workflow.png)
99

src/Dompurify/DompurifyCli.php

Lines changed: 6 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,12 @@ public function exec(string $html): string
3232
{
3333
$htmlFile = $this->saveHtml($html);
3434

35-
$process = new Process([$this->config->node, $this->binPath(), $htmlFile]);
35+
$process = new Process([
36+
$this->config->node,
37+
$this->config->binary ?? __DIR__.DIRECTORY_SEPARATOR.'cli.js', // ? check file explicitly
38+
$htmlFile,
39+
]);
40+
3641
$process->mustRun();
3742

3843
$output = $process->getOutput();
@@ -55,19 +60,6 @@ public function exec(string $html): string
5560
return $clean;
5661
}
5762

58-
private function binPath(): string
59-
{
60-
$binPath = $this->config->binary ?? __DIR__.DIRECTORY_SEPARATOR.'cli.js';
61-
62-
$binAbsPath = realpath($binPath);
63-
64-
if ($binAbsPath === false) {
65-
throw new XsslessException("Cannot locate '$binPath'");
66-
}
67-
68-
return $binAbsPath;
69-
}
70-
7163
private function saveHtml(string $value): string
7264
{
7365
$dir = $this->tempDir();

src/Dompurify/DompurifyService.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@ class DompurifyService implements HasSetupInterface, ServiceInterface
1616

1717
// TODO: better injection (fs and process http)
1818

19-
// TODO: private
20-
public Process $serviceProcess;
19+
private Process $serviceProcess;
20+
2121
// ? add static array for all processes
2222

2323
/** @param DompurifyServiceConfig $config */
@@ -73,8 +73,8 @@ public function start(): static
7373

7474
$this->serviceProcess->setIdleTimeout(null);
7575

76-
// ? rm check
7776
if (! $this->isRunning()) {
77+
// Triggers on bad note path
7878
throw new ProcessFailedException($this->serviceProcess);
7979
}
8080

src/laravel/Commands/StartCommand.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,6 @@ public function handle(): void
4141

4242
pcntl_signal_dispatch();
4343

44-
// Sleep for a short period to avoid busy-waiting
4544
usleep(100_000);
4645
}
4746
}

tests/Dompurify/DompurifyCliTest.php

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,10 @@
66
use Medilies\Xssless\Xssless;
77
use Symfony\Component\Process\Exception\ProcessFailedException;
88

9+
// ----------------------------------------------------------------------------
10+
// Errors and mocked binaries
11+
// ----------------------------------------------------------------------------
12+
913
it('throws on bad node path', function () {
1014
$cleaner = (new DompurifyCli)->configure(new DompurifyCliConfig(
1115
node: 'nodeZz',
@@ -19,7 +23,7 @@
1923
binary: __DIR__.'/js-mocks/x.js',
2024
));
2125

22-
expect(fn () => $cleaner->exec('foo'))->toThrow(XsslessException::class);
26+
expect(fn () => $cleaner->exec('foo'))->toThrow(ProcessFailedException::class);
2327
});
2428

2529
it('throws when cannot locate temp folder', function () {
@@ -30,6 +34,18 @@
3034
expect(fn () => $cleaner->exec('foo'))->toThrow(XsslessException::class);
3135
});
3236

37+
it('throws when cannot read cleaned file', function () {
38+
$cleaner = (new DompurifyCli)->configure(new DompurifyCliConfig(
39+
binary: __DIR__.'/js-mocks/cli-returns-bad-path.js',
40+
));
41+
42+
expect(fn () => $cleaner->exec('foo'))->toThrow(XsslessException::class);
43+
});
44+
45+
// ----------------------------------------------------------------------------
46+
// Real setup and clean
47+
// ----------------------------------------------------------------------------
48+
3349
test('setup()', function () {
3450
$cleaner = (new Xssless)->using(new DompurifyCliConfig);
3551

@@ -53,11 +69,3 @@
5369

5470
expect($clean)->toBe('<img>"&gt;');
5571
})->depends('setup()');
56-
57-
it('throws when cannot read cleaned file', function () {
58-
$cleaner = (new DompurifyCli)->configure(new DompurifyCliConfig(
59-
binary: __DIR__.'/js-mocks/cli-returns-bad-path.js',
60-
));
61-
62-
expect(fn () => $cleaner->exec('foo'))->toThrow(XsslessException::class);
63-
});

tests/Dompurify/DompurifyServiceTest.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@
77
use Symfony\Component\Process\Exception\ProcessFailedException;
88
use Symfony\Component\Process\Exception\ProcessTimedOutException;
99

10+
// ----------------------------------------------------------------------------
11+
// Errors and mocked binaries
12+
// ----------------------------------------------------------------------------
13+
1014
it('throws on bad node path', function () {
1115
$service = (new DompurifyService)->configure(new DompurifyServiceConfig(
1216
node: 'nodeZz',
@@ -42,6 +46,10 @@
4246
expect(fn () => $cleaner->send($dirty))->toThrow(ConnectException::class);
4347
});
4448

49+
// ----------------------------------------------------------------------------
50+
// Real setup and clean
51+
// ----------------------------------------------------------------------------
52+
4553
test('setup()', function () {
4654
$cleaner = (new DompurifyService)->configure(new DompurifyServiceConfig);
4755

0 commit comments

Comments
 (0)