Skip to content

Commit

Permalink
Enable passing environment variables to node
Browse files Browse the repository at this point in the history
  • Loading branch information
jukra committed Jun 13, 2024
1 parent ad84cc4 commit 8e42200
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 5 deletions.
5 changes: 4 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,8 @@ Grover.configure do |config|
request_timeout: 1000, # Timeout when fetching the content (overloads the `timeout` option)
convert_timeout: 2000, # Timeout when converting the content (overloads the `timeout` option, only applies to PDF conversion)
launch_args: ['--font-render-hinting=medium'],
wait_until: 'domcontentloaded'
wait_until: 'domcontentloaded',
node_env_vars: { 'LD_PRELOAD': '' } # Environment variables to pass for the spawned node process
}
end
```
Expand Down Expand Up @@ -171,6 +172,8 @@ by passing it to the `execute_script` option.
Grover.new(<some url>, { execute_script: 'document.getElementsByTagName("footer")[0].innerText = "Hey"' }).to_pdf
```

The `node_env_vars` 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).

#### Basic authentication
For requesting a page with basic authentication, `username` and `password` options can be provided. Note that this
only really makes sense if you're calling Grover directly (and not via middleware).
Expand Down
2 changes: 1 addition & 1 deletion lib/grover.rb
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ def processor
end

def normalized_options(path:)
normalized_options = Utils.normalize_object @options, excluding: ['extraHTTPHeaders']
normalized_options = Utils.normalize_object @options, excluding: %w[extraHTTPHeaders nodeEnvVars]
normalized_options['path'] = path if path.is_a? ::String
normalized_options
end
Expand Down
6 changes: 4 additions & 2 deletions lib/grover/processor.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ def initialize(app_root)
end

def convert(method, url_or_html, options)
spawn_process
spawn_process options
ensure_packages_are_initiated
result = call_js_method method, url_or_html, options
return unless result
Expand All @@ -30,8 +30,10 @@ def convert(method, url_or_html, options)

attr_reader :app_root, :stdin, :stdout, :stderr, :wait_thr

def spawn_process
def spawn_process(options)
node_env_vars = options['nodeEnvVars'] || {}
@stdin, @stdout, @stderr, @wait_thr = Open3.popen3(
node_env_vars,
'node',
File.expand_path(File.join(__dir__, 'js/processor.cjs')),
chdir: app_root
Expand Down
13 changes: 12 additions & 1 deletion 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
let(:options) { { 'nodeEnvVars' => { '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

0 comments on commit 8e42200

Please sign in to comment.