svelte-on-rails 22.1.1 → 22.3.0

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: e4321bcf9f02af3beafeef6f841ae2b428415633b3554b67c03b95c7841fcf8c
4
- data.tar.gz: d39b7db4a9ae20f6edc43ff5615ac2547d98d1dbff9c764c1424ac702701de5e
3
+ metadata.gz: a95b04eb2bc4fe9f3b9cf489af8550377bb35bd8297b7c8c9849fae0b4985e89
4
+ data.tar.gz: d648e0c9240757feb39c42904165bbd985ee24c19de78e2b1d5068c60c2b9539
5
5
  SHA512:
6
- metadata.gz: dbadd0e2a4d1b92288edbaaa38b83bb297294610df6fe39dfa9ed59ded3b05aa9d039ac9416038cb54ddb920fd413f4d2a043f1398a9cc4e7efcc71cd30854b7
7
- data.tar.gz: 3a22b355a4be460ca6a0262442959c2f778f2d0f7b11a6a70b79b47b8070de438efb13621c50c4118a58c492f5a1a3c4879c76a5e7df6c5b1f007c5dceb9593d
6
+ metadata.gz: f32d6e15c061f5c95abaffdf1e74ac295bd89058a9eb226e8ead4ac34321890509f0dddfe2af0e3e8c5a0e0d0696ac4236eb224c41928ca1ed2cd2f06ac51aca
7
+ data.tar.gz: 588239e64c3a43f660c3099710d0b0a41f59fc0636be72d7458756a8b55fd6e773fae911937de0099d5b7d4d30466d2856a62b0405f5e2a6ca2e19fe23e208b7
@@ -179,7 +179,7 @@ module SvelteOnRails
179
179
 
180
180
  def redis_cache_store_configs
181
181
  if defined?(Rails.application) && Rails.application.config.cache_store.is_a?(Array) && Rails.application.config.cache_store.first == :redis_cache_store
182
- { 'redis_cache_store' => Rails.application.config.cache_store.second.stringify_keys }
182
+ { redis_cache_store: Rails.application.config.cache_store.second.stringify_keys }
183
183
  else
184
184
  {}
185
185
  end
@@ -24,24 +24,38 @@ module SvelteOnRails
24
24
 
25
25
  # Defining methods to log errors and warnings using Rails logger
26
26
  def self.puts_error(text)
27
- # Using Rails logger to log error with custom formatting
28
- caller_info = caller[1] =~ /`([^']*)'/ ? $1 : "unknown"
29
- Rails.logger.error(" => [SOR] #{caller_info} ERROR")
30
- text.split("\n").each do |line|
31
- Rails.logger.error(" => #{line}")
32
- end
27
+ log_with_severity(:error, text, "ERROR", "\e[31m") # red
33
28
  end
34
29
 
35
30
  # Defining method to log warnings using Rails logger
36
31
  def self.puts_warning(text)
37
- # Using Rails logger to log warning with custom formatting
38
- caller_info = caller[1] =~ /`([^']*)'/ ? $1 : "unknown"
39
- Rails.logger.warn(" => [SOR] #{caller_info} WARNING \e[0m")
40
- text.split("\n").each do |line|
41
- Rails.logger.warn(" => #{line}")
32
+ log_with_severity(:warn, text, "WARNING", "\e[33m")
33
+ end
34
+
35
+ # Internal: emits a tagged, colored block via Rails.logger.
36
+ # ANSI colors are always included; modern terminals + the Rails
37
+ # dev logger render them. In production the logger strips them
38
+ # or they pass through harmlessly to aggregators.
39
+ def self.log_with_severity(level, text, label, color_code)
40
+
41
+ clear = "\e[0m"
42
+ bold = "\e[1m"
43
+
44
+ header = "#{color_code}#{bold}[SOR] #{label}#{clear}"
45
+ Rails.logger.public_send(level, header)
46
+ text.to_s.split("\n").each do |line|
47
+ Rails.logger.public_send(level, "#{color_code} => #{line}#{clear}")
42
48
  end
43
49
  end
44
50
 
51
+ def self.color_supported?
52
+ return false if Rails.env.production?
53
+ # Rails dev logger writes to STDOUT; check that it's a TTY.
54
+ $stdout.tty?
55
+ rescue
56
+ false
57
+ end
58
+
45
59
  def self.component_paths_uncached(component, current_controller_path)
46
60
 
47
61
  file_basename = File.basename(component, ".svelte")
@@ -51,6 +51,7 @@ module SvelteOnRails
51
51
 
52
52
  rake_tasks do
53
53
  load File.expand_path("../../tasks/svelte_on_rails_tasks.rake", __FILE__)
54
+ load File.expand_path("../../tasks/cache_tasks.rake", __FILE__)
54
55
  end
55
56
 
56
57
  end
@@ -0,0 +1,156 @@
1
+ namespace :svelte_on_rails do
2
+ namespace :cache do
3
+
4
+ desc "Delete all svelte-on-rails cache keys from Redis (scoped to the configured redis_namespace)"
5
+ task clear: :environment do
6
+ conf = SvelteOnRails::Configuration.instance
7
+ redis = conf.redis_instance
8
+ namespace = conf.redis_namespace
9
+
10
+ unless redis
11
+ warn "svelte_on_rails:clear_cache — no redis_instance configured, nothing to do."
12
+ next
13
+ end
14
+
15
+ if Rails.env.production? && ENV["FORCE"] != "1"
16
+ print "You are about to delete all keys matching «#{namespace}:*» in PRODUCTION. Continue? (y/N) "
17
+ answer = STDIN.gets.to_s.strip.downcase
18
+ unless %w[y yes].include?(answer)
19
+ puts "Aborted."
20
+ next
21
+ end
22
+ end
23
+
24
+ pattern = "#{namespace}:*"
25
+ cursor = "0"
26
+ total_deleted = 0
27
+ batches = 0
28
+
29
+ puts "Scanning Redis for keys matching «#{pattern}» ..."
30
+
31
+ loop do
32
+ cursor, keys = redis.scan(cursor, match: pattern, count: 500)
33
+ if keys.any?
34
+ # UNLINK is non-blocking on Redis >= 4; fall back to DEL if not available.
35
+ deleted = begin
36
+ redis.unlink(*keys)
37
+ rescue Redis::CommandError
38
+ redis.del(*keys)
39
+ end
40
+ total_deleted += deleted
41
+ batches += 1
42
+ end
43
+ break if cursor == "0"
44
+ rescue => e
45
+ warn "svelte_on_rails:clear_cache — error during scan/delete: #{e.class}: #{e.message}"
46
+ break
47
+ end
48
+
49
+ puts "svelte_on_rails:clear_cache — deleted #{total_deleted} key(s) in #{batches} batch(es) under «#{pattern}»."
50
+ end
51
+
52
+ desc "Show all svelte-on-rails Redis keys with their memory footprint (scoped to redis_namespace). Env: LIMIT=n, SORT=size|key, PATTERN=extra-glob"
53
+ task show: :environment do
54
+ conf = SvelteOnRails::Configuration.instance
55
+ redis = conf.redis_instance
56
+ namespace = conf.redis_namespace
57
+
58
+ unless redis
59
+ warn "svelte_on_rails:show_cache — no redis_instance configured, nothing to show."
60
+ next
61
+ end
62
+
63
+ extra = ENV["PATTERN"].to_s.strip
64
+ pattern = extra.empty? ? "#{namespace}:*" : "#{namespace}:*#{extra}*"
65
+ limit = ENV["LIMIT"].to_s =~ /\A\d+\z/ ? ENV["LIMIT"].to_i : nil
66
+ sort_by = %w[size key].include?(ENV["SORT"]) ? ENV["SORT"] : "size"
67
+
68
+ puts "Scanning Redis for keys matching «#{pattern}» ..."
69
+
70
+ entries = [] # [{ key:, bytes:, ttl:, type: }]
71
+ cursor = "0"
72
+
73
+ loop do
74
+ cursor, keys = redis.scan(cursor, match: pattern, count: 500)
75
+
76
+ if keys.any?
77
+ # Pipeline MEMORY USAGE / TTL / TYPE for the whole batch.
78
+ results = redis.pipelined do |p|
79
+ keys.each do |k|
80
+ p.call(["MEMORY", "USAGE", k])
81
+ p.ttl(k)
82
+ p.type(k)
83
+ end
84
+ end
85
+
86
+ keys.each_with_index do |k, i|
87
+ bytes = results[i * 3]
88
+ ttl = results[i * 3 + 1]
89
+ type = results[i * 3 + 2]
90
+ entries << { key: k, bytes: bytes.to_i, ttl: ttl.to_i, type: type }
91
+ end
92
+ end
93
+
94
+ break if cursor == "0"
95
+ rescue => e
96
+ warn "svelte_on_rails:show_cache — error during scan: #{e.class}: #{e.message}"
97
+ break
98
+ end
99
+
100
+ if entries.empty?
101
+ puts "No keys found under «#{pattern}»."
102
+ next
103
+ end
104
+
105
+ entries.sort_by! { |e| sort_by == "size" ? -e[:bytes] : e[:key] }
106
+ shown = limit ? entries.first(limit) : entries
107
+
108
+ key_w = [shown.map { |e| e[:key].length }.max, 3].max
109
+ size_w = [shown.map { |e| human_bytes(e[:bytes]).length }.max, 4].max
110
+ ttl_w = [shown.map { |e| ttl_str(e[:ttl]).length }.max, 3].max
111
+ type_w = [shown.map { |e| e[:type].to_s.length }.max, 4].max
112
+
113
+ header = format("%-#{key_w}s %#{size_w}s %#{ttl_w}s %-#{type_w}s", "KEY", "SIZE", "TTL", "TYPE")
114
+ puts header
115
+ puts "-" * header.length
116
+
117
+ shown.each do |e|
118
+ puts format(
119
+ "%-#{key_w}s %#{size_w}s %#{ttl_w}s %-#{type_w}s",
120
+ e[:key], human_bytes(e[:bytes]), ttl_str(e[:ttl]), e[:type]
121
+ )
122
+ end
123
+
124
+ total_bytes = entries.sum { |e| e[:bytes] }
125
+ puts "-" * header.length
126
+ puts "Total: #{entries.size} key(s), #{human_bytes(total_bytes)} in RAM" \
127
+ "#{limit && entries.size > limit ? " (showing top #{limit} by #{sort_by})" : ""}"
128
+ end
129
+ end
130
+ end
131
+
132
+ # Helpers used by svelte_on_rails:show_cache
133
+ def human_bytes(n)
134
+ return "0 B" if n.nil? || n <= 0
135
+ units = %w[B KB MB GB TB]
136
+ i = (Math.log(n) / Math.log(1024)).floor
137
+ i = units.length - 1 if i >= units.length
138
+ format("%.2f %s", n.to_f / (1024 ** i), units[i])
139
+ end
140
+
141
+ def ttl_str(ttl)
142
+ case ttl
143
+ when -2 then "gone"
144
+ when -1 then "∞"
145
+ else
146
+ if ttl >= 86_400 then
147
+ "#{(ttl / 86_400.0).round(1)}d"
148
+ elsif ttl >= 3_600 then
149
+ "#{(ttl / 3_600.0).round(1)}h"
150
+ elsif ttl >= 60 then
151
+ "#{(ttl / 60.0).round(1)}m"
152
+ else
153
+ "#{ttl}s"
154
+ end
155
+ end
156
+ end
@@ -157,4 +157,4 @@ namespace :svelte_on_rails do
157
157
 
158
158
  end
159
159
 
160
- Rake::Task["assets:precompile"].enhance ["svelte_on_rails:precompile"]
160
+ Rake::Task["assets:precompile"].enhance ["svelte_on_rails:precompile"]
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: svelte-on-rails
3
3
  version: !ruby/object:Gem::Version
4
- version: 22.1.1
4
+ version: 22.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Christian Sedlmair
@@ -100,6 +100,7 @@ files:
100
100
  - lib/svelte_on_rails/ssr_client.rb
101
101
  - lib/svelte_on_rails/turbo_stream.rb
102
102
  - lib/svelte_on_rails/view_helpers.rb
103
+ - lib/tasks/cache_tasks.rake
103
104
  - lib/tasks/svelte_on_rails_tasks.rake
104
105
  - templates/config_base/app/frontend/ssr/ssr.js
105
106
  - templates/config_base/config/svelte_on_rails.yml