Skip to content

Commit c6d2bd9

Browse files
committed
fix all pro rubocop errors
1 parent 37f5894 commit c6d2bd9

File tree

10 files changed

+98
-71
lines changed

10 files changed

+98
-71
lines changed

react_on_rails_pro/lib/react_on_rails_pro/license_public_key.rb

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -16,15 +16,15 @@ module LicensePublicKey
1616
# TODO: Add a prepublish check to ensure this key matches the latest public key from the API.
1717
# This should be implemented after publishing the API endpoint on the ShakaCode website.
1818
KEY = OpenSSL::PKey::RSA.new(<<~PEM.strip.strip_heredoc)
19-
-----BEGIN PUBLIC KEY-----
20-
MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAzcS/fpHz5CbnTQxb4Zot
21-
khjzXu7xNS+Y9VKfapMaHOMzNoCMfy1++hxHJatRedr+YQfZRCjfiN168Cpe+dhe
22-
yfNtOoLU9/+/5jTsxH+WQJWNRswyKms5HNajlIMN1GEYdZmZbvOPaZvh6ENsT+EV
23-
HnhjJtsHl7qltBoL0ul7rONxaNHCzJcKk4lf3B2/1j1wpA91MKz4bbQVh4/6Th0E
24-
/39f0PWvvBXzQS+yt1qaa1DIX5YL6Aug5uEpb1+6QWcN3hCzqSPBv1HahrG50rsD
25-
gf8KORV3X2N9t6j6iqPmRqfRcTBKtmPhM9bORtKiSwBK8LsIUzp2/UUmkdHnkyzu
26-
NQIDAQAB
27-
-----END PUBLIC KEY-----
19+
-----BEGIN PUBLIC KEY-----
20+
MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAzcS/fpHz5CbnTQxb4Zot
21+
khjzXu7xNS+Y9VKfapMaHOMzNoCMfy1++hxHJatRedr+YQfZRCjfiN168Cpe+dhe
22+
yfNtOoLU9/+/5jTsxH+WQJWNRswyKms5HNajlIMN1GEYdZmZbvOPaZvh6ENsT+EV
23+
HnhjJtsHl7qltBoL0ul7rONxaNHCzJcKk4lf3B2/1j1wpA91MKz4bbQVh4/6Th0E
24+
/39f0PWvvBXzQS+yt1qaa1DIX5YL6Aug5uEpb1+6QWcN3hCzqSPBv1HahrG50rsD
25+
gf8KORV3X2N9t6j6iqPmRqfRcTBKtmPhM9bORtKiSwBK8LsIUzp2/UUmkdHnkyzu
26+
NQIDAQAB
27+
-----END PUBLIC KEY-----
2828
PEM
2929
end
3030
end

react_on_rails_pro/lib/react_on_rails_pro/request.rb

Lines changed: 32 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -217,7 +217,7 @@ def common_form_data
217217
ReactOnRailsPro::Utils.common_form_data
218218
end
219219

220-
def create_connection
220+
def create_connection # rubocop:disable Metrics/MethodLength, Metrics/AbcSize
221221
url = ReactOnRailsPro.configuration.renderer_url
222222
Rails.logger.info do
223223
"[ReactOnRailsPro] Setting up Node Renderer connection to #{url}"
@@ -229,28 +229,37 @@ def create_connection
229229
# https://honeyryderchuck.gitlab.io/httpx/wiki/Persistent
230230
.plugin(
231231
:retries, max_retries: 1,
232-
retry_change_requests: true,
233-
# Official HTTPx docs says that we should use the retry_on option to decide if teh request should be retried or not
234-
# However, HTTPx assumes that connection errors such as timeout error should be retried by default and it doesn't consider retry_on block at all at that case
235-
# So, we have to do the following trick to avoid retries when a Timeout error happens while streaming a component
236-
# If the streamed component returned any chunks, it shouldn't retry on errors, as it would cause page duplication
237-
# The SSR-generated html will be written to the page two times in this case
238-
retry_after: ->(request, response) do
239-
if (request.stream.instance_variable_get(:@react_on_rails_received_first_chunk))
240-
e = response.error
241-
raise ReactOnRailsPro::Error, "An error happened during server side render streaming of a component.\n" \
242-
"Original error:\n#{e}\n#{e.backtrace}"
243-
end
244-
245-
Rails.logger.info do
246-
"[ReactOnRailsPro] An error happneding while making a request to the Node Renderer.\n" \
247-
"Error: #{response.error}.\n" \
248-
"Retrying by HTTPX \"retries\" plugin..."
249-
end
250-
# The retry_after block expects to return a delay to wait before retrying the request
251-
# nil means no waiting delay
252-
nil
253-
end
232+
retry_change_requests: true,
233+
# Official HTTPx docs says that we should use the retry_on option to decide if the
234+
# request should be retried or not
235+
# However, HTTPx assumes that connection errors such as timeout error should be retried
236+
# by default and it doesn't consider retry_on block at all at that case
237+
# So, we have to do the following trick to avoid retries when a Timeout error happens
238+
# while streaming a component
239+
# If the streamed component returned any chunks, it shouldn't retry on errors, as it
240+
# would cause page duplication
241+
# The SSR-generated html will be written to the page two times in this case
242+
retry_after: lambda do |request, response|
243+
if request.stream.instance_variable_get(:@react_on_rails_received_first_chunk)
244+
e = response.error
245+
raise(
246+
ReactOnRailsPro::Error,
247+
"An error happened during server side render streaming " \
248+
"of a component.\nOriginal error:\n#{e}\n#{e.backtrace}"
249+
)
250+
end
251+
252+
Rails.logger.info do
253+
"[ReactOnRailsPro] An error occurred while making " \
254+
"a request to the Node Renderer.\n" \
255+
"Error: #{response.error}.\n" \
256+
"Retrying by HTTPX \"retries\" plugin..."
257+
end
258+
# The retry_after block expects to return a delay to wait before
259+
# retrying the request
260+
# nil means no waiting delay
261+
nil
262+
end
254263
)
255264
.plugin(:stream)
256265
# See https://www.rubydoc.info/gems/httpx/1.3.3/HTTPX%2FOptions:initialize for the available options

react_on_rails_pro/lib/react_on_rails_pro/stream_request.rb

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -51,23 +51,23 @@ def handle_chunk(chunk, position)
5151
end
5252
end
5353

54-
def each_chunk(&block)
54+
def each_chunk(&block) # rubocop:disable Metrics/CyclomaticComplexity
5555
return enum_for(:each_chunk) unless block
5656

5757
first_chunk = true
5858
@component.each_chunk do |chunk|
5959
position = first_chunk ? :first : :middle
6060
modified_chunk = handle_chunk(chunk, position)
61-
block.call(modified_chunk)
61+
yield(modified_chunk)
6262
first_chunk = false
6363
end
6464

6565
# The last chunk contains the append content after the transformation
6666
# All transformations are applied to the append content
6767
last_chunk = handle_chunk("", :last)
68-
block.call(last_chunk) unless last_chunk.empty?
69-
rescue StandardError => err
70-
current_error = err
68+
yield(last_chunk) unless last_chunk.empty?
69+
rescue StandardError => e
70+
current_error = e
7171
rescue_block_index = 0
7272
while current_error.present? && (rescue_block_index < @rescue_blocks.size)
7373
begin

react_on_rails_pro/rakelib/public_key_management.rake

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,9 @@ require "uri"
1313
# rake react_on_rails_pro:verify_public_key # Verify current configuration
1414
# rake react_on_rails_pro:public_key_help # Show help
1515

16-
namespace :react_on_rails_pro do
16+
namespace :react_on_rails_pro do # rubocop:disable Metrics/BlockLength
1717
desc "Update the public key for React on Rails Pro license validation"
18-
task :update_public_key, [:source] do |_task, args|
18+
task :update_public_key, [:source] do |_task, args| # rubocop:disable Metrics/BlockLength
1919
source = args[:source] || "production"
2020

2121
# Determine the API URL based on the source
@@ -68,7 +68,7 @@ namespace :react_on_rails_pro do
6868
# ShakaCode's public key for React on Rails Pro license verification
6969
# The private key corresponding to this public key is held by ShakaCode
7070
# and is never committed to the repository
71-
# Last updated: #{Time.now.utc.strftime("%Y-%m-%d %H:%M:%S UTC")}
71+
# Last updated: #{Time.now.utc.strftime('%Y-%m-%d %H:%M:%S UTC')}
7272
# Source: #{api_url}
7373
#
7474
# You can update this public key by running the rake task:
@@ -86,12 +86,13 @@ namespace :react_on_rails_pro do
8686
puts "✅ Updated Ruby public key: #{ruby_file_path}"
8787

8888
# Update Node/TypeScript public key file
89-
node_file_path = File.join(File.dirname(__FILE__), "..", "packages", "node-renderer", "src", "shared", "licensePublicKey.ts")
89+
node_file_path = File.join(File.dirname(__FILE__), "..", "packages", "node-renderer", "src", "shared",
90+
"licensePublicKey.ts")
9091
node_content = <<~TYPESCRIPT
9192
// ShakaCode's public key for React on Rails Pro license verification
9293
// The private key corresponding to this public key is held by ShakaCode
9394
// and is never committed to the repository
94-
// Last updated: #{Time.now.utc.strftime("%Y-%m-%d %H:%M:%S UTC")}
95+
// Last updated: #{Time.now.utc.strftime('%Y-%m-%d %H:%M:%S UTC')}
9596
// Source: #{api_url}
9697
//
9798
// You can update this public key by running the rake task:

react_on_rails_pro/spec/dummy/app/controllers/pages_controller.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# frozen_string_literal: true
22

3-
class PagesController < ApplicationController
3+
class PagesController < ApplicationController # rubocop:disable Metrics/ClassLength
44
include ReactOnRailsPro::RSCPayloadRenderer
55
include RscPostsPageOverRedisHelper
66

@@ -85,8 +85,8 @@ def redis_receiver
8585
ensure
8686
begin
8787
redis&.close
88-
rescue StandardError => close_err
89-
Rails.logger.warn "Failed to close Redis: #{close_err.message}"
88+
rescue StandardError => e
89+
Rails.logger.warn "Failed to close Redis: #{e.message}"
9090
end
9191
end
9292

react_on_rails_pro/spec/dummy/spec/system/integration_spec.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -396,14 +396,14 @@ def change_text_expect_dom_selector(dom_selector, expect_no_change: false)
396396

397397
it "hydrates the component" do
398398
visit path
399-
expect(page.html).to match(/client-bundle[^\"]*.js/)
399+
expect(page.html).to match(/client-bundle[^"]*.js/)
400400
change_text_expect_dom_selector(selector)
401401
end
402402

403403
it "renders the page completely on server and displays content on client even without JavaScript" do
404404
# Don't add client-bundle.js to the page to ensure that the app is not hydrated
405405
visit "#{path}?skip_js_packs=true"
406-
expect(page.html).not_to match(/client-bundle[^\"]*.js/)
406+
expect(page.html).not_to match(/client-bundle[^"]*.js/)
407407
# Ensure that the component state is not updated
408408
change_text_expect_dom_selector(selector, expect_no_change: true)
409409

@@ -432,7 +432,7 @@ def change_text_expect_dom_selector(dom_selector, expect_no_change: false)
432432
"#ServerComponentRouter-react-component-0"
433433

434434
# Skip the test that fails without JavaScript - being addressed in another PR
435-
it "renders the page completely on server and displays content on client even without JavaScript",
435+
it "renders the page completely on server and displays content on client even without JavaScript", # rubocop:disable RSpec/NoExpectationExample
436436
skip: "Being addressed in another PR" do
437437
# This test is overridden to skip it
438438
end

react_on_rails_pro/spec/react_on_rails_pro/configuration_spec.rb

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
require_relative "spec_helper"
44

5-
module ReactOnRailsPro
5+
module ReactOnRailsPro # rubocop:disable Metrics/ModuleLength
66
RSpec.describe Configuration do
77
after do
88
ReactOnRailsPro.instance_variable_set(:@configuration, nil)
@@ -205,7 +205,8 @@ def self.fetch(*)
205205

206206
expect(ReactOnRailsPro.configuration.rsc_bundle_js_file).to eq("rsc-bundle.js")
207207
expect(ReactOnRailsPro.configuration.react_client_manifest_file).to eq("react-client-manifest.json")
208-
expect(ReactOnRailsPro.configuration.react_server_client_manifest_file).to eq("react-server-client-manifest.json")
208+
expect(ReactOnRailsPro.configuration.react_server_client_manifest_file)
209+
.to eq("react-server-client-manifest.json")
209210
end
210211

211212
it "allows setting rsc_bundle_js_file" do
@@ -229,7 +230,8 @@ def self.fetch(*)
229230
config.react_server_client_manifest_file = "custom-server-client-manifest.json"
230231
end
231232

232-
expect(ReactOnRailsPro.configuration.react_server_client_manifest_file).to eq("custom-server-client-manifest.json")
233+
expect(ReactOnRailsPro.configuration.react_server_client_manifest_file)
234+
.to eq("custom-server-client-manifest.json")
233235
end
234236

235237
it "allows nil values for RSC configuration options" do

react_on_rails_pro/spec/react_on_rails_pro/license_validator_spec.rb

Lines changed: 27 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -75,26 +75,30 @@
7575
ENV["REACT_ON_RAILS_PRO_LICENSE"] = expired_token
7676
end
7777

78-
context "in development/test environment" do
78+
context "when in development/test environment" do
7979
before do
8080
allow(Rails).to receive(:env).and_return(ActiveSupport::StringInquirer.new("development"))
8181
end
8282

8383
it "raises error immediately" do
84-
expect { described_class.validated_license_data! }.to raise_error(ReactOnRailsPro::Error, /License has expired/)
84+
expect do
85+
described_class.validated_license_data!
86+
end.to raise_error(ReactOnRailsPro::Error, /License has expired/)
8587
end
8688

8789
it "includes FREE license information in error message" do
88-
expect { described_class.validated_license_data! }.to raise_error(ReactOnRailsPro::Error, /FREE evaluation license/)
90+
expect do
91+
described_class.validated_license_data!
92+
end.to raise_error(ReactOnRailsPro::Error, /FREE evaluation license/)
8993
end
9094
end
9195

92-
context "in production environment" do
96+
context "when in production environment" do
9397
before do
9498
allow(Rails).to receive(:env).and_return(ActiveSupport::StringInquirer.new("production"))
9599
end
96100

97-
context "within grace period (expired < 1 month ago)" do
101+
context "with grace period (expired < 1 month ago)" do
98102
let(:expired_within_grace) do
99103
{
100104
@@ -113,7 +117,8 @@
113117
end
114118

115119
it "logs warning with grace period remaining" do
116-
expect(mock_logger).to receive(:error).with(/WARNING:.*License has expired.*Grace period:.*day\(s\) remaining/)
120+
expect(mock_logger).to receive(:error)
121+
.with(/WARNING:.*License has expired.*Grace period:.*day\(s\) remaining/)
117122
described_class.validated_license_data!
118123
end
119124

@@ -123,7 +128,7 @@
123128
end
124129
end
125130

126-
context "outside grace period (expired > 1 month ago)" do
131+
context "when outside grace period (expired > 1 month ago)" do
127132
let(:expired_outside_grace) do
128133
{
129134
@@ -138,11 +143,15 @@
138143
end
139144

140145
it "raises error" do
141-
expect { described_class.validated_license_data! }.to raise_error(ReactOnRailsPro::Error, /License has expired/)
146+
expect do
147+
described_class.validated_license_data!
148+
end.to raise_error(ReactOnRailsPro::Error, /License has expired/)
142149
end
143150

144151
it "includes FREE license information in error message" do
145-
expect { described_class.validated_license_data! }.to raise_error(ReactOnRailsPro::Error, /FREE evaluation license/)
152+
expect do
153+
described_class.validated_license_data!
154+
end.to raise_error(ReactOnRailsPro::Error, /FREE evaluation license/)
146155
end
147156
end
148157
end
@@ -168,7 +177,9 @@
168177
end
169178

170179
it "includes FREE license information in error message" do
171-
expect { described_class.validated_license_data! }.to raise_error(ReactOnRailsPro::Error, /FREE evaluation license/)
180+
expect do
181+
described_class.validated_license_data!
182+
end.to raise_error(ReactOnRailsPro::Error, /FREE evaluation license/)
172183
end
173184
end
174185

@@ -180,11 +191,15 @@
180191
end
181192

182193
it "raises error" do
183-
expect { described_class.validated_license_data! }.to raise_error(ReactOnRailsPro::Error, /Invalid license signature/)
194+
expect do
195+
described_class.validated_license_data!
196+
end.to raise_error(ReactOnRailsPro::Error, /Invalid license signature/)
184197
end
185198

186199
it "includes FREE license information in error message" do
187-
expect { described_class.validated_license_data! }.to raise_error(ReactOnRailsPro::Error, /FREE evaluation license/)
200+
expect do
201+
described_class.validated_license_data!
202+
end.to raise_error(ReactOnRailsPro::Error, /FREE evaluation license/)
188203
end
189204
end
190205

react_on_rails_pro/spec/react_on_rails_pro/stream_decorator_spec.rb

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@
6565

6666
describe "#rescue" do
6767
it "catches the error happens inside the component" do
68-
allow(mock_component).to receive(:each_chunk).and_raise(StandardError.new "Fake Error")
68+
allow(mock_component).to receive(:each_chunk).and_raise(StandardError.new("Fake Error"))
6969
mocked_block = mock_block
7070

7171
stream_decorator.rescue(&mocked_block.block)
@@ -80,7 +80,7 @@
8080
end
8181

8282
it "catches the error happens inside subsequent component calls" do
83-
allow(mock_component).to receive(:each_chunk).and_yield("Chunk1").and_raise(ArgumentError.new "Fake Error")
83+
allow(mock_component).to receive(:each_chunk).and_yield("Chunk1").and_raise(ArgumentError.new("Fake Error"))
8484
mocked_block = mock_block
8585

8686
stream_decorator.rescue(&mocked_block.block)
@@ -96,7 +96,7 @@
9696
end
9797

9898
it "can yield values to the stream" do
99-
allow(mock_component).to receive(:each_chunk).and_yield("Chunk1").and_raise(ArgumentError.new "Fake Error")
99+
allow(mock_component).to receive(:each_chunk).and_yield("Chunk1").and_raise(ArgumentError.new("Fake Error"))
100100
mocked_block = mock_block
101101

102102
stream_decorator.rescue(&mocked_block.block)
@@ -115,11 +115,11 @@
115115
end
116116

117117
it "can convert the error into another error" do
118-
allow(mock_component).to receive(:each_chunk).and_raise(StandardError.new "Fake Error")
118+
allow(mock_component).to receive(:each_chunk).and_raise(StandardError.new("Fake Error"))
119119
mocked_block = mock_block do |error|
120120
expect(error).to be_a(StandardError)
121121
expect(error.message).to eq("Fake Error")
122-
raise ArgumentError.new "Another Error"
122+
raise ArgumentError, "Another Error"
123123
end
124124

125125
stream_decorator.rescue(&mocked_block.block)
@@ -129,12 +129,12 @@
129129
end
130130

131131
it "chains multiple rescue blocks" do
132-
allow(mock_component).to receive(:each_chunk).and_yield("Chunk1").and_raise(StandardError.new "Fake Error")
132+
allow(mock_component).to receive(:each_chunk).and_yield("Chunk1").and_raise(StandardError.new("Fake Error"))
133133
fist_rescue_block = mock_block do |error, &block|
134134
expect(error).to be_a(StandardError)
135135
expect(error.message).to eq("Fake Error")
136136
block.call "Chunk from first rescue block"
137-
raise ArgumentError.new "Another Error"
137+
raise ArgumentError, "Another Error"
138138
end
139139

140140
second_rescue_block = mock_block do |error, &block|

0 commit comments

Comments
 (0)