generated from spatie/package-skeleton-laravel
-
-
Notifications
You must be signed in to change notification settings - Fork 27
/
Copy pathBrowsershotFunction.php
156 lines (132 loc) · 4.31 KB
/
BrowsershotFunction.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
<?php
namespace Wnx\SidecarBrowsershot\Functions;
use Hammerstone\Sidecar\Architecture;
use Hammerstone\Sidecar\LambdaFunction;
use Hammerstone\Sidecar\Package;
use Hammerstone\Sidecar\WarmingConfig;
use Illuminate\Support\Str;
class BrowsershotFunction extends LambdaFunction
{
public function handler()
{
return 'browsershot.handle';
}
public function name()
{
return 'browsershot';
}
public function package()
{
return Package::make()
->includeStrings([
'browser.cjs' => $this->modifiedBrowserJs(),
])
->includeExactly([
__DIR__.'/../../resources/lambda/browsershot.js' => 'browsershot.js',
])
->includeExactly($this->customFonts());
}
protected function customFonts(): array
{
$fonts = collect();
$fontDirectory = Str::finish(config('sidecar-browsershot.fonts'), DIRECTORY_SEPARATOR);
// Check if the custom fonts folder exists.
if (file_exists($fontDirectory)) {
// Loop through all files in the custom fonts folder.
/** @var array $fontFiles */
$fontFiles = scandir($fontDirectory);
foreach ($fontFiles as $file) {
if (is_file($fontDirectory.$file)) {
$fonts->prepend("fonts/$file", $fontDirectory.$file);
}
}
}
// By default, we include the NotoColorEmoji font.
$fonts->prepend('fonts/NotoColorEmoji.ttf', __DIR__.'/../../resources/lambda/fonts/NotoColorEmoji.ttf');
// Ensure that we only have unique font values.
return $fonts->unique()->toArray();
}
/**
* We get puppeteer out of the layer, which Spatie doesn't allow
* for. We'll just overwrite their browser.cjs to add it.
*
* @return string
*/
protected function modifiedBrowserJs()
{
if (app()->environment('testing')) {
/** @var string $browser */
$browser = file_get_contents('vendor/spatie/browsershot/bin/browser.cjs');
} else {
/** @var string $browser */
$browser = file_get_contents(base_path('vendor/spatie/browsershot/bin/browser.cjs'));
}
// Remove their reference.
$browser = str_replace('const puppet = (pup || require(\'puppeteer\'));', '', $browser);
// Use pupeteer-core instead.
return "const puppet = require('puppeteer-core'); \n".$browser;
}
/**
* {@inheritDoc}
*/
public function runtime()
{
return 'nodejs20.x';
}
/**
* {@inheritDoc}
*/
public function memory()
{
return config('sidecar-browsershot.memory');
}
/**
* {@inheritDoc}
*/
public function storage()
{
// Defaults to the main sidecar config value if the sidecar-browsershot config hasn't been updated to include this new key.
return config('sidecar-browsershot.storage', parent::storage());
}
/**
* {@inheritDoc}
*/
public function timeout()
{
// Defaults to the main sidecar config value if the sidecar-browsershot config hasn't been updated to include this new key.
return config('sidecar-browsershot.timeout', parent::timeout());
}
/**
* {@inheritDoc}
*/
public function architecture()
{
return Architecture::X86_64;
}
/**
* {@inheritDoc}
*/
public function warmingConfig()
{
return WarmingConfig::instances(config('sidecar-browsershot.warming'));
}
public function layers()
{
if ($layers = config('sidecar-browsershot.layers')) {
return $layers;
}
$region = config('sidecar.aws_region');
if ($region === 'ap-northeast-2') {
$chromeAwsLambdaVersion = 41;
} else {
$chromeAwsLambdaVersion = 42;
}
// Add Layers that each contain `puppeteer-core` and `@sparticuz/chromium`
// https://github.com/stefanzweifel/sidecar-browsershot-layer
// https://github.com/shelfio/chrome-aws-lambda-layer
return [
"arn:aws:lambda:{$region}:821527532446:layer:sidecar-browsershot-layer:2",
"arn:aws:lambda:{$region}:764866452798:layer:chrome-aws-lambda:{$chromeAwsLambdaVersion}",
];
}
}