From e502f20e1fe6c12740442fe4f918046fd483f351 Mon Sep 17 00:00:00 2001 From: Alexey Vasiliev Date: Wed, 6 Nov 2024 02:21:33 +0200 Subject: [PATCH 1/9] Make combine_pdf optional --- grover.gemspec | 5 +++-- lib/grover/configuration.rb | 4 +++- lib/grover/middleware.rb | 11 +++++++++-- lib/grover/processor.rb | 2 +- 4 files changed, 16 insertions(+), 6 deletions(-) diff --git a/grover.gemspec b/grover.gemspec index 60ad719..8272b95 100644 --- a/grover.gemspec +++ b/grover.gemspec @@ -29,6 +29,7 @@ Gem::Specification.new do |spec| spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) } spec.require_paths = ['lib'] - spec.add_dependency 'combine_pdf', '~> 1.0' - spec.add_dependency 'nokogiri', '~> 1.0' + spec.add_dependency 'nokogiri', '~> 1' + + spec.add_development_dependency 'combine_pdf', '~> 1.0' end diff --git a/lib/grover/configuration.rb b/lib/grover/configuration.rb index d015f0f..cf9e2e6 100644 --- a/lib/grover/configuration.rb +++ b/lib/grover/configuration.rb @@ -7,7 +7,8 @@ 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, :node_env_vars, :allow_file_uris + :use_jpeg_middleware, :js_runtime_bin, + :node_env_vars, :allow_file_uris def initialize @options = {} @@ -18,6 +19,7 @@ def initialize @use_pdf_middleware = true @use_png_middleware = false @use_jpeg_middleware = false + @js_runtime_bin = 'node' @node_env_vars = {} @allow_file_uris = false end diff --git a/lib/grover/middleware.rb b/lib/grover/middleware.rb index 0f1f8e6..f251baf 100644 --- a/lib/grover/middleware.rb +++ b/lib/grover/middleware.rb @@ -1,7 +1,5 @@ # frozen_string_literal: true -require 'combine_pdf' - class Grover # # Rack middleware for catching PDF requests and returning the upstream HTML as a PDF @@ -132,12 +130,21 @@ def create_grover_for_response(response) # rubocop:disable Metrics/AbcSize end def add_cover_content(grover) + load_combine_pdf pdf = CombinePDF.parse grover.to_pdf pdf >> fetch_cover_pdf(grover.front_cover_path) if grover.show_front_cover? pdf << fetch_cover_pdf(grover.back_cover_path) if grover.show_back_cover? pdf.to_pdf end + def load_combine_pdf + begin + require 'combine_pdf' + rescue ::LoadError + raise Grover::Error, "You need install combine_pdf gem" + end + end + def fetch_cover_pdf(path) temp_env = env.deep_dup scrub_env! temp_env diff --git a/lib/grover/processor.rb b/lib/grover/processor.rb index 839d07d..f3dd397 100644 --- a/lib/grover/processor.rb +++ b/lib/grover/processor.rb @@ -33,7 +33,7 @@ def convert(method, url_or_html, options) def spawn_process @stdin, @stdout, @stderr, @wait_thr = Open3.popen3( Grover.configuration.node_env_vars, - 'node', + Grover.configuration.js_runtime_bin, File.expand_path(File.join(__dir__, 'js/processor.cjs')), chdir: app_root ) From 6d0a5498804cdab4764bd8fc65e59715d3623559 Mon Sep 17 00:00:00 2001 From: Alexey Vasiliev Date: Wed, 6 Nov 2024 02:24:05 +0200 Subject: [PATCH 2/9] Support yarn pnp --- lib/grover/configuration.rb | 2 +- lib/grover/processor.rb | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/grover/configuration.rb b/lib/grover/configuration.rb index cf9e2e6..73b8a08 100644 --- a/lib/grover/configuration.rb +++ b/lib/grover/configuration.rb @@ -19,7 +19,7 @@ def initialize @use_pdf_middleware = true @use_png_middleware = false @use_jpeg_middleware = false - @js_runtime_bin = 'node' + @js_runtime_bin = ['node'] @node_env_vars = {} @allow_file_uris = false end diff --git a/lib/grover/processor.rb b/lib/grover/processor.rb index f3dd397..92cf913 100644 --- a/lib/grover/processor.rb +++ b/lib/grover/processor.rb @@ -33,7 +33,7 @@ def convert(method, url_or_html, options) def spawn_process @stdin, @stdout, @stderr, @wait_thr = Open3.popen3( Grover.configuration.node_env_vars, - Grover.configuration.js_runtime_bin, + *Grover.configuration.js_runtime_bin, File.expand_path(File.join(__dir__, 'js/processor.cjs')), chdir: app_root ) From 454ad8d3d078d67ee2706b20b0e8dd4db1b41d61 Mon Sep 17 00:00:00 2001 From: Alexey Vasiliev Date: Wed, 6 Nov 2024 02:30:05 +0200 Subject: [PATCH 3/9] Update readme --- README.md | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 4fae14f..7b6a8bb 100644 --- a/README.md +++ b/README.md @@ -300,6 +300,16 @@ Grover.configure do |config| end ``` +#### Yarn pnp strategy + +If you ar using yarn pnp strategy, you can override how to run js runtime for grover: + +```ruby +Grover.configure do |config| + config.js_runtime_bin = ['yarn', 'node'] +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. @@ -325,7 +335,7 @@ To enable them, there are configuration options for each image type as well as a If either of the image handling middleware options are enabled, the [ignore_path](#ignore_path) and/or [ignore_request](#ignore_request) should also be configured, otherwise assets are likely to be handled -which would likely result in 404 responses. +which would likely result in 404 responses. ```ruby # config/initializers/grover.rb @@ -402,7 +412,7 @@ end ``` ### allow_file_uris -The `allow_file_uris` option can be used to render an html document from the file system. +The `allow_file_uris` option can be used to render an html document from the file system. This should be used with *EXTREME CAUTION*. If used improperly it could potentially be manipulated to reveal sensitive files on the system. Do not enable if rendering content from outside entities (user uploads, external URLs, etc). @@ -435,6 +445,8 @@ For direct execution, you can make multiple calls and combine the resulting PDFs ### Using middleware +To use this functionality you need add [combine_pdf](https://rubygems.org/gems/combine_pdf) gem in your app. + You can specify relative paths to the cover page contents using the `front_cover_path` and `back_cover_path` options either via the global configuration, or via meta tags. These paths (with query parameters) are then requested from the downstream app. @@ -560,7 +572,7 @@ The middleware and HTML preprocessing components were used heavily in the implem Thanks are also given to the excellent [Schmooze project](https://github.com/Shopify/schmooze). The Ruby to NodeJS interface in Grover is heavily based off that work. Grover previously used that gem, -however migrated away due to differing requirements over persistence/cleanup of the NodeJS worker process. +however migrated away due to differing requirements over persistence/cleanup of the NodeJS worker process. ## License From a55ffc7428861f3053cdd2a5676ff244c078452a Mon Sep 17 00:00:00 2001 From: Oleksii Vasyliev Date: Mon, 18 Nov 2024 11:00:03 +0200 Subject: [PATCH 4/9] Update README.md Co-authored-by: Andrew Bromwich --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 7b6a8bb..c4f3214 100644 --- a/README.md +++ b/README.md @@ -302,7 +302,7 @@ end #### Yarn pnp strategy -If you ar using yarn pnp strategy, you can override how to run js runtime for grover: +If you are using the Yarn PnP strategy, you can override the run JS runtime for grover: ```ruby Grover.configure do |config| From 0b0b3aec28f6df0751d1426709a6ace82cf302c0 Mon Sep 17 00:00:00 2001 From: Oleksii Vasyliev Date: Mon, 18 Nov 2024 11:00:12 +0200 Subject: [PATCH 5/9] Update README.md Co-authored-by: Andrew Bromwich --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index c4f3214..0876b27 100644 --- a/README.md +++ b/README.md @@ -445,7 +445,7 @@ For direct execution, you can make multiple calls and combine the resulting PDFs ### Using middleware -To use this functionality you need add [combine_pdf](https://rubygems.org/gems/combine_pdf) gem in your app. +Note, to use this functionality you need to add the [combine_pdf](https://rubygems.org/gems/combine_pdf) gem to your app. You can specify relative paths to the cover page contents using the `front_cover_path` and `back_cover_path` options either via the global configuration, or via meta tags. These paths (with query parameters) are then From 399e53aa81b18eedd43ac7d288c6998f37c5eb0b Mon Sep 17 00:00:00 2001 From: Oleksii Vasyliev Date: Mon, 18 Nov 2024 11:00:18 +0200 Subject: [PATCH 6/9] Update lib/grover/middleware.rb Co-authored-by: Andrew Bromwich --- lib/grover/middleware.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/grover/middleware.rb b/lib/grover/middleware.rb index f251baf..e22ca15 100644 --- a/lib/grover/middleware.rb +++ b/lib/grover/middleware.rb @@ -141,7 +141,7 @@ def load_combine_pdf begin require 'combine_pdf' rescue ::LoadError - raise Grover::Error, "You need install combine_pdf gem" + raise Grover::Error, 'Please add/install the "combine_pdf" gem to use the front/back cover page feature' end end From fe915656b117b15a280c2d0707376f0984432a57 Mon Sep 17 00:00:00 2001 From: Oleksii Vasyliev Date: Mon, 18 Nov 2024 11:00:24 +0200 Subject: [PATCH 7/9] Update README.md Co-authored-by: Andrew Bromwich --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 0876b27..035fd39 100644 --- a/README.md +++ b/README.md @@ -300,7 +300,7 @@ Grover.configure do |config| end ``` -#### Yarn pnp strategy +#### Yarn PnP strategy If you are using the Yarn PnP strategy, you can override the run JS runtime for grover: From 911b0233867798a3cd2d601929f4fe131789f495 Mon Sep 17 00:00:00 2001 From: Alexey Vasiliev Date: Mon, 18 Nov 2024 11:03:27 +0200 Subject: [PATCH 8/9] move paragraph --- README.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 035fd39..ffafeee 100644 --- a/README.md +++ b/README.md @@ -445,12 +445,12 @@ For direct execution, you can make multiple calls and combine the resulting PDFs ### Using middleware -Note, to use this functionality you need to add the [combine_pdf](https://rubygems.org/gems/combine_pdf) gem to your app. - You can specify relative paths to the cover page contents using the `front_cover_path` and `back_cover_path` options either via the global configuration, or via meta tags. These paths (with query parameters) are then requested from the downstream app. +Note, to use this functionality you need to add the [combine_pdf](https://rubygems.org/gems/combine_pdf) gem to your app. + The cover pages are converted to PDF in isolation, and then combined together with the original PDF response, before being returned back up through the Rack stack. From bdbc3e833b3a24263080c6f43870a56a76692009 Mon Sep 17 00:00:00 2001 From: Andrew Bromwich Date: Tue, 19 Nov 2024 13:28:36 +1000 Subject: [PATCH 9/9] Address code lints --- Gemfile | 1 + grover.gemspec | 2 -- lib/grover/configuration.rb | 2 +- lib/grover/middleware.rb | 8 +++----- 4 files changed, 5 insertions(+), 8 deletions(-) diff --git a/Gemfile b/Gemfile index 5a8f72e..c920a2c 100644 --- a/Gemfile +++ b/Gemfile @@ -6,6 +6,7 @@ source 'https://rubygems.org' gemspec gem 'childprocess', '~> 5.0' +gem 'combine_pdf', '~> 1.0' gem 'mini_magick', '~> 4.12' gem 'pdf-reader', '~> 2.11' gem 'puma', '~> 6.4' diff --git a/grover.gemspec b/grover.gemspec index 8272b95..b87f59d 100644 --- a/grover.gemspec +++ b/grover.gemspec @@ -30,6 +30,4 @@ Gem::Specification.new do |spec| spec.require_paths = ['lib'] spec.add_dependency 'nokogiri', '~> 1' - - spec.add_development_dependency 'combine_pdf', '~> 1.0' end diff --git a/lib/grover/configuration.rb b/lib/grover/configuration.rb index 73b8a08..ea71e3a 100644 --- a/lib/grover/configuration.rb +++ b/lib/grover/configuration.rb @@ -10,7 +10,7 @@ class Configuration :use_jpeg_middleware, :js_runtime_bin, :node_env_vars, :allow_file_uris - def initialize + def initialize # rubocop:disable Metrics/MethodLength @options = {} @meta_tag_prefix = 'grover-' @ignore_path = nil diff --git a/lib/grover/middleware.rb b/lib/grover/middleware.rb index e22ca15..5129379 100644 --- a/lib/grover/middleware.rb +++ b/lib/grover/middleware.rb @@ -138,11 +138,9 @@ def add_cover_content(grover) end def load_combine_pdf - begin - require 'combine_pdf' - rescue ::LoadError - raise Grover::Error, 'Please add/install the "combine_pdf" gem to use the front/back cover page feature' - end + require 'combine_pdf' + rescue ::LoadError + raise Grover::Error, 'Please add/install the "combine_pdf" gem to use the front/back cover page feature' end def fetch_cover_pdf(path)