Skip to content

Commit

Permalink
Add internal support for Chromium Result (#105)
Browse files Browse the repository at this point in the history
* Bump min version of spatie/browsershot

* Support ChromiumResult

* Fix Test

* Fix styling
  • Loading branch information
stefanzweifel authored Dec 16, 2023
1 parent 25a32d6 commit 623ae0b
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 5 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
"php": "^8.0",
"hammerstone/sidecar": "^0.4.0",
"illuminate/contracts": "^8.0|^9.0|^10.0",
"spatie/browsershot": "^3.57.8",
"spatie/browsershot": "^3.60",
"spatie/laravel-package-tools": "^1.9.2"
},
"require-dev": {
Expand Down
28 changes: 25 additions & 3 deletions src/BrowsershotLambda.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use Illuminate\Support\Facades\Storage;
use Illuminate\Support\Str;
use Spatie\Browsershot\Browsershot;
use Spatie\Browsershot\ChromiumResult;
use Spatie\Browsershot\Exceptions\CouldNotTakeBrowsershot;
use Spatie\Browsershot\Exceptions\ElementNotFound;
use Wnx\SidecarBrowsershot\Functions\BrowsershotFunction;
Expand All @@ -29,16 +30,37 @@ protected function callBrowser(array $command): string
$this->throwError($response);
}

// If the response is not valid JSON, it's probably a base64 encoded string representing a binary file.
// In this case, we will return the base64 decoded string.
if (json_decode($response->body(), true) === null) {
$result = base64_decode($response->body());
} else {
$result = $response->body();

// If the response is valid JSON, we can cast it to a Chromium Result.
// It will contain the result and additional information about the Chromium process.
if (is_array($chromiumResult = json_decode($response->body(), true))) {
$this->chromiumResult = new ChromiumResult($chromiumResult);
}
}

$s3 = Arr::get($command, 'options.s3');
$path = Arr::get($command, 'options.path');

if ($path && ! $s3) {
file_put_contents($path, base64_decode($response->body()));
file_put_contents($path, $result);

return $path;
} else {
return $response->body();
}

// If ChromiumResult is available, return the result from there.
if ($this->chromiumResult) {
return $this->chromiumResult->getResult();
}

// The result can now be either a base64 deocded string representing a binary file or a string representing
// the ETag of a file on S3.
return $result;
}

/**
Expand Down
2 changes: 1 addition & 1 deletion tests/BrowsershotLambdaTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@
it('returns raw html from html', function () {
$html = BrowsershotLambda::html('<h1>Hello world!!</h1>')->bodyHtml();

$this->assertEquals("<html><head></head><body><h1>Hello world!!</h1></body></html>\n", $html);
$this->assertEquals("<html><head></head><body><h1>Hello world!!</h1></body></html>", $html);
});

it('throws LambdaExecutionException error if browsershot fails', function () {
Expand Down

0 comments on commit 623ae0b

Please sign in to comment.