Skip to content

Commit

Permalink
Enable passing environment variables to the node process (#240)
Browse files Browse the repository at this point in the history
  • Loading branch information
jukra authored Jun 23, 2024
1 parent ad84cc4 commit 29d1794
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 3 deletions.
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -287,6 +287,16 @@ respond_to do |format|
end
```

#### Setting custom environment variable for node
The `node_env_vars` configuration option enables you to set custom environment variables for the spawned node process. For example you might need to disable jemalloc in some environments (https://github.com/Studiosity/grover/issues/80).

```ruby
# config/initializers/grover.rb
Grover.configure do |config|
config.node_env_vars = { "LD_PRELOAD" => "" }
end
```

## Middleware
Grover comes with a middleware that allows users to get a PDF, PNG or JPEG view of
any page on your site by appending .pdf, .png or .jpeg/.jpg to the URL.
Expand Down
3 changes: 2 additions & 1 deletion lib/grover/configuration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ class Grover
class Configuration
attr_accessor :options, :meta_tag_prefix, :ignore_path, :ignore_request,
:root_url, :use_pdf_middleware, :use_png_middleware,
:use_jpeg_middleware
:use_jpeg_middleware, :node_env_vars

def initialize
@options = {}
Expand All @@ -18,6 +18,7 @@ def initialize
@use_pdf_middleware = true
@use_png_middleware = false
@use_jpeg_middleware = false
@node_env_vars = {}
end
end
end
1 change: 1 addition & 0 deletions lib/grover/processor.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ def convert(method, url_or_html, options)

def spawn_process
@stdin, @stdout, @stderr, @wait_thr = Open3.popen3(
Grover.configuration.node_env_vars,
'node',
File.expand_path(File.join(__dir__, 'js/processor.cjs')),
chdir: app_root
Expand Down
15 changes: 13 additions & 2 deletions spec/grover/processor_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,16 @@
end
end

context 'when passing environment variables to the Node process through configuration' do
before { allow(Grover.configuration).to receive(:node_env_vars).and_return 'FOO' => 'bar' }

it 'passes the environment variables to the Node process' do
expect(Open3).to receive(:popen3).with({ 'FOO' => 'bar' }, any_args).and_call_original

convert
end
end

if puppeteer_version_on_or_after? '18.0.0'
context 'when only the puppeteer-core package is installed', :remote_browser do
before { FileUtils.move 'node_modules/puppeteer', 'node_modules/puppeteer_temp' }
Expand All @@ -129,7 +139,8 @@
before do
allow(Open3).to(
receive(:popen3).
with('node', File.expand_path(File.join(__dir__, '../../lib/grover/js/processor.cjs')), chdir: Dir.pwd).
with({}, 'node', File.expand_path(File.join(__dir__,
'../../lib/grover/js/processor.cjs')), chdir: Dir.pwd).
and_return([stdin, stdout, stderr, wait_thr])
)

Expand Down Expand Up @@ -703,7 +714,7 @@
<html>
<head><link rel='icon' href='data:;base64,iVBORw0KGgo='></head>
<body>
<img src="https://placekitten.com/200/200" />
<img src="http://localhost:4567/cat.png" />
</body>
</html>
HTML
Expand Down

0 comments on commit 29d1794

Please sign in to comment.