grover 0.12.3 → 0.14.2

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: 7376874743ddb5f08d3fc68c02f84a72eb6cbe98c63306b2ce2a5f57d37867c9
4
- data.tar.gz: 546007fe354d88c117f70469c8cf80ba88fd22415ebb350aa79512c64592be3f
3
+ metadata.gz: 5dae47620d2a732cab442444c25a6954f103c669fa7eea7c092b34297f1d71c0
4
+ data.tar.gz: 84d886147feff66e75276b00d5e6529883396763dcc424d6e42186c5dbb9b9d0
5
5
  SHA512:
6
- metadata.gz: 010bb2d49e78af9cfe56cc0480d803f217dd1f644d99d36640fad060e8a70a2be7b4d422057a761a5d0710347649dd44d3da9efde553b4ff23bff62838973af0
7
- data.tar.gz: 5879e39b9d00a6b1c5904a4e71cdf00fc695caa29de7b85ca4dbcbf7ec857d87822c2be42ded362efd3ef2466f8c5db338182e8115cbe6b4b822a6114d5e50d8
6
+ metadata.gz: bad7d5f4c5a15b8a9bc7ea7041ad1184c72416944b4c837c16592197e5105cebccee53eae564b9fbf8776884fb4b4e1d7d888de7c2018b0040c085979c65e55f
7
+ data.tar.gz: b8837766f459146e6349df80952f655dc330c7d9ca7cdebcbdd2a69fe17a10701addb1ff029ebfacd3458780b1d134679128df8766c1b017904c76dd876e43d2
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.
data/lib/grover.rb CHANGED
@@ -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'
@@ -50,6 +50,15 @@ class Grover
50
50
  processor.convert :pdf, @url, normalized_options(path: path)
51
51
  end
52
52
 
53
+ #
54
+ # Request URL with provided options and render HTML
55
+ #
56
+ # @return [String] The resulting HTML string
57
+ #
58
+ def to_html
59
+ processor.convert :content, @url, normalized_options(path: nil)
60
+ end
61
+
53
62
  #
54
63
  # Request URL with provided options and create screenshot
55
64
  #
@@ -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
@@ -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());
@@ -101,6 +101,22 @@ const _processPage = (async (convertAction, urlOrHtml, options) => {
101
101
  }
102
102
  }
103
103
 
104
+ // add styles (if provided)
105
+ const styleTagOptions = options.styleTagOptions; delete options.styleTagOptions;
106
+ if (Array.isArray(styleTagOptions)) {
107
+ for (const styleTagOption of styleTagOptions) {
108
+ await page.addStyleTag(styleTagOption);
109
+ }
110
+ }
111
+
112
+ // add scripts (if provided)
113
+ const scriptTagOptions = options.scriptTagOptions; delete options.scriptTagOptions;
114
+ if (Array.isArray(scriptTagOptions)) {
115
+ for (const scriptTagOption of scriptTagOptions) {
116
+ await page.addScriptTag(scriptTagOption);
117
+ }
118
+ }
119
+
104
120
  // If specified, evaluate script on the page
105
121
  const executeScript = options.executeScript; delete options.executeScript;
106
122
  if (executeScript !== undefined) {
@@ -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,7 +99,7 @@ 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
105
  body = HTMLPreprocessor.process body, root_url, protocol
@@ -135,11 +140,24 @@ class Grover
135
140
  end
136
141
 
137
142
  def configure_env_for_grover_request(env)
138
- 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
139
149
  env['HTTP_ACCEPT'] = concat(env['HTTP_ACCEPT'], Rack::Mime.mime_type('.html'))
140
150
  env['Rack-Middleware-Grover'] = 'true'
141
151
  end
142
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
+
143
161
  def concat(accepts, type)
144
162
  (accepts || '').split(',').unshift(type).compact.join(',')
145
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)
@@ -19,6 +19,7 @@ class Grover
19
19
  ensure_packages_are_initiated
20
20
  result = call_js_method method, url_or_html, options
21
21
  return unless result
22
+ return result if result.is_a?(String)
22
23
 
23
24
  result['data'].pack('C*')
24
25
  ensure
@@ -76,7 +77,7 @@ class Grover
76
77
  @package_json ||= JSON.parse(File.read(package_json_path))
77
78
  end
78
79
 
79
- def call_js_method(method, url_or_html, options) # rubocop:disable Metrics/MethodLength
80
+ def call_js_method(method, url_or_html, options) # rubocop:disable Metrics/AbcSize, Metrics/MethodLength
80
81
  stdin.puts JSON.dump([method, url_or_html, options])
81
82
  input = stdout.gets
82
83
  raise Errno::EPIPE, "Can't read from worker" if input.nil?
@@ -90,6 +91,8 @@ class Grover
90
91
  else
91
92
  raise Grover::JavaScript.const_get(error_class, false), message
92
93
  end
94
+ rescue JSON::ParserError
95
+ raise Grover::Error, 'Malformed worker response'
93
96
  rescue Errno::EPIPE, IOError
94
97
  raise Grover::Error, "Worker process failed:\n#{stderr.read}"
95
98
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  class Grover
4
- VERSION = '0.12.3'
4
+ VERSION = '0.14.2'
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.3
4
+ version: 0.14.2
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-06-30 00:00:00.000000000 Z
11
+ date: 2021-02-22 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: 3.1.0
192
196
  required_rubygems_version: !ruby/object:Gem::Requirement
193
197
  requirements:
194
198
  - - ">="