grover 0.12.1 → 0.13.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 18d371bdb009b39ba004d06f24c35ddb4f51a0cd253082481d04b1c1dac45b57
4
- data.tar.gz: f20bfd80f394ef4c3c7e98128a6d0d5615387af7c1c6733f867ee24ead91c9e5
3
+ metadata.gz: dc12ff02b99d418681762184d059363cc0a8b313172387bb2e30813804cdbf92
4
+ data.tar.gz: 7c5287275ae6cb24fbeb52201fc982b54d1766f65d83f5ea21e0e1d63ee563d9
5
5
  SHA512:
6
- metadata.gz: 280be92cb4f2fd14c94341d75b3564add657ec7acc496fe3cffd0b1d0d7bcde86ab492112fec7522fe6c666cc95045f8f5eaf06e5e6cd6d034abab443c8955ed
7
- data.tar.gz: 5a30bf3b21ea80eeb6b6f54c559f0fe7d0fd0559941028cebc71fd96fbd00dae24b2d67ecec29a413931cc0f883b4323282cc3308f69f370649720bd7a64639d
6
+ metadata.gz: f4397920cf427e04bf3eb904fa94e50e48ce0d09c9e4fd373f25fefd6360fb4cb8a0e3497026dc554fcdd647e387faa516ed6ad215bfb8dca61e2609195c23dc
7
+ data.tar.gz: b87cf9ebf9caf4d6a2d0bf9d14d0b17f9f6b523b3908b10abddeb1d79b4abd298051e5221f55b8ffa1c3623e8c7419ad6f48831a5f12cc71251bd0f0c6e25fdf
data/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2018-2020 Studiosity
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
@@ -33,8 +33,8 @@ class Grover
33
33
  # see https://github.com/GoogleChrome/puppeteer/blob/master/docs/api.md#pagepdfoptions
34
34
  #
35
35
  def initialize(url, options = {})
36
- @url = url
37
- @options = OptionsBuilder.new(options, url)
36
+ @url = url.to_s
37
+ @options = OptionsBuilder.new(options, @url)
38
38
  @root_path = @options.delete 'root_path'
39
39
  @front_cover_path = @options.delete 'front_cover_path'
40
40
  @back_cover_path = @options.delete 'back_cover_path'
@@ -5,13 +5,14 @@ class Grover
5
5
  # Configuration of the options for Grover HTML to PDF conversion
6
6
  #
7
7
  class Configuration
8
- attr_accessor :options, :meta_tag_prefix, :ignore_path,
8
+ attr_accessor :options, :meta_tag_prefix, :ignore_path, :root_url,
9
9
  :use_pdf_middleware, :use_png_middleware, :use_jpeg_middleware
10
10
 
11
11
  def initialize
12
12
  @options = {}
13
13
  @meta_tag_prefix = 'grover-'
14
14
  @ignore_path = nil
15
+ @root_url = nil
15
16
  @use_pdf_middleware = true
16
17
  @use_png_middleware = false
17
18
  @use_jpeg_middleware = false
@@ -17,13 +17,13 @@ class Grover
17
17
 
18
18
  def self.translate_relative_paths(html, root_url)
19
19
  # Try out this regexp using rubular http://rubular.com/r/hiAxBNX7KE
20
- html.gsub(%r{(href|src)=(['"])/([^/"']([^\"']*|[^"']*))?['"]}, "\\1=\\2#{root_url}\\3\\2")
20
+ html.gsub(%r{(href|src)=(['"])/([^/"']([^"']*|[^"']*))?['"]}, "\\1=\\2#{root_url}\\3\\2")
21
21
  end
22
22
  private_class_method :translate_relative_paths
23
23
 
24
24
  def self.translate_relative_protocols(body, protocol)
25
25
  # Try out this regexp using rubular http://rubular.com/r/0Ohk0wFYxV
26
- body.gsub(%r{(href|src)=(['"])//([^\"']*|[^"']*)['"]}, "\\1=\\2#{protocol}://\\3\\2")
26
+ body.gsub(%r{(href|src)=(['"])//([^"']*|[^"']*)['"]}, "\\1=\\2#{protocol}://\\3\\2")
27
27
  end
28
28
  private_class_method :translate_relative_protocols
29
29
  end
@@ -82,7 +82,7 @@ const _processPage = (async (convertAction, urlOrHtml, options) => {
82
82
  requestOptions.waitUntil = waitUntil || 'networkidle0';
83
83
  await page.setRequestInterception(true);
84
84
  page.once('request', request => {
85
- request.respond({ body: urlOrHtml });
85
+ request.respond({ body: urlOrHtml === '' ? ' ' : urlOrHtml });
86
86
  // Reset the request interception
87
87
  // (we only want to intercept the first request - ie our HTML)
88
88
  page.on('request', request => request.continue());
@@ -107,6 +107,13 @@ const _processPage = (async (convertAction, urlOrHtml, options) => {
107
107
  await page.evaluate(executeScript);
108
108
  }
109
109
 
110
+ // If specified, wait for selector
111
+ const waitForSelector = options.waitForSelector; delete options.waitForSelector;
112
+ const waitForSelectorOptions = options.waitForSelectorOptions; delete options.waitForSelectorOptions;
113
+ if (waitForSelector !== undefined) {
114
+ await page.waitForSelector(waitForSelector, waitForSelectorOptions)
115
+ }
116
+
110
117
  // If we're running puppeteer in headless mode, return the converted PDF
111
118
  if (debug === undefined || (typeof debug === 'object' && (debug.headless === undefined || debug.headless))) {
112
119
  return await page[convertAction](options);
@@ -9,12 +9,15 @@ class Grover
9
9
  # Much of this code was sourced from the PDFKit project
10
10
  # @see https://github.com/pdfkit/pdfkit
11
11
  #
12
- class Middleware
13
- def initialize(app)
12
+ class Middleware # rubocop:disable Metrics/ClassLength
13
+ def initialize(app, *args)
14
14
  @app = app
15
15
  @pdf_request = false
16
16
  @png_request = false
17
17
  @jpeg_request = false
18
+
19
+ @root_url =
20
+ args.last.is_a?(Hash) && args.last.key?(:root_url) ? args.last[:root_url] : Grover.configuration.root_url
18
21
  end
19
22
 
20
23
  def call(env)
@@ -30,6 +33,8 @@ class Grover
30
33
  response = update_response response, headers if grover_request? && html_content?(headers)
31
34
 
32
35
  [status, headers, response]
36
+ ensure
37
+ restore_env_from_grover_request(env) if grover_request?
33
38
  end
34
39
 
35
40
  private
@@ -94,12 +99,18 @@ class Grover
94
99
  end
95
100
  end
96
101
 
97
- def create_grover_for_response(response)
102
+ def create_grover_for_response(response) # rubocop:disable Metrics/AbcSize
98
103
  body = response.respond_to?(:body) ? response.body : response.join
99
104
  body = body.join if body.is_a?(Array)
100
-
101
105
  body = HTMLPreprocessor.process body, root_url, protocol
102
- Grover.new(body, display_url: request_url)
106
+
107
+ options = { display_url: request_url }
108
+ cookies = Rack::Utils.parse_cookies(env).map do |name, value|
109
+ { name: name, value: Rack::Utils.escape(value), domain: env['HTTP_HOST'] }
110
+ end
111
+ options[:cookies] = cookies if cookies.any?
112
+
113
+ Grover.new(body, options)
103
114
  end
104
115
 
105
116
  def add_cover_content(grover)
@@ -129,11 +140,24 @@ class Grover
129
140
  end
130
141
 
131
142
  def configure_env_for_grover_request(env)
132
- env['PATH_INFO'] = env['REQUEST_URI'] = path_without_extension
143
+ # Save the env params we're overriding so we can restore them after the response is fetched
144
+ @pre_request_env_params = env.slice('PATH_INFO', 'REQUEST_URI', 'HTTP_ACCEPT')
145
+
146
+ # Override path/URI so any downstream middleware/app doesn't try actioning the request as PDF
147
+ env['PATH_INFO'] = path_without_extension
148
+ env['REQUEST_URI'] = @request.url
133
149
  env['HTTP_ACCEPT'] = concat(env['HTTP_ACCEPT'], Rack::Mime.mime_type('.html'))
134
150
  env['Rack-Middleware-Grover'] = 'true'
135
151
  end
136
152
 
153
+ def restore_env_from_grover_request(env)
154
+ return unless @pre_request_env_params.is_a? Hash
155
+
156
+ # Restore the path/URI so any upstream middleware doesn't get confused
157
+ env.merge! @pre_request_env_params
158
+ env['REQUEST_URI'] = @request.url unless @pre_request_env_params.key? 'REQUEST_URI'
159
+ end
160
+
137
161
  def concat(accepts, type)
138
162
  (accepts || '').split(',').unshift(type).compact.join(',')
139
163
  end
@@ -8,7 +8,7 @@ class Grover
8
8
  # Build options from Grover.configuration, meta_options, and passed-in options
9
9
  #
10
10
  class OptionsBuilder < Hash
11
- def initialize(options, url)
11
+ def initialize(options, url) # rubocop:disable Lint/MissingSuper
12
12
  @url = url
13
13
  combined = grover_configuration
14
14
  Utils.deep_merge! combined, Utils.deep_stringify_keys(options)
@@ -22,7 +22,7 @@ class Grover
22
22
 
23
23
  result['data'].pack('C*')
24
24
  ensure
25
- cleanup_process
25
+ cleanup_process if stdin
26
26
  end
27
27
 
28
28
  private
@@ -76,7 +76,7 @@ class Grover
76
76
  @package_json ||= JSON.parse(File.read(package_json_path))
77
77
  end
78
78
 
79
- def call_js_method(method, url_or_html, options) # rubocop:disable Metrics/MethodLength
79
+ def call_js_method(method, url_or_html, options) # rubocop:disable Metrics/AbcSize, Metrics/MethodLength
80
80
  stdin.puts JSON.dump([method, url_or_html, options])
81
81
  input = stdout.gets
82
82
  raise Errno::EPIPE, "Can't read from worker" if input.nil?
@@ -90,6 +90,8 @@ class Grover
90
90
  else
91
91
  raise Grover::JavaScript.const_get(error_class, false), message
92
92
  end
93
+ rescue JSON::ParserError
94
+ raise Grover::Error, 'Malformed worker response'
93
95
  rescue Errno::EPIPE, IOError
94
96
  raise Grover::Error, "Worker process failed:\n#{stderr.read}"
95
97
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  class Grover
4
- VERSION = '0.12.1'
4
+ VERSION = '0.13.3'
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: grover
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.12.1
4
+ version: 0.13.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Andrew Bromwich
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2020-05-12 00:00:00.000000000 Z
11
+ date: 2020-12-14 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: combine_pdf
@@ -163,6 +163,7 @@ executables: []
163
163
  extensions: []
164
164
  extra_rdoc_files: []
165
165
  files:
166
+ - LICENSE
166
167
  - lib/active_support_ext/object/deep_dup.rb
167
168
  - lib/active_support_ext/object/duplicable.rb
168
169
  - lib/grover.rb
@@ -188,7 +189,10 @@ required_ruby_version: !ruby/object:Gem::Requirement
188
189
  requirements:
189
190
  - - ">="
190
191
  - !ruby/object:Gem::Version
191
- version: '0'
192
+ version: 2.5.0
193
+ - - "<"
194
+ - !ruby/object:Gem::Version
195
+ version: 2.8.0
192
196
  required_rubygems_version: !ruby/object:Gem::Requirement
193
197
  requirements:
194
198
  - - ">="