runlight 0.4.3 → 0.5.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.
Files changed (44) hide show
  1. checksums.yaml +4 -4
  2. data/LICENSE +1 -1
  3. data/README.md +1 -1
  4. data/assets/build.json +4 -4
  5. data/assets/dashboard.js +12 -12
  6. data/assets/locales.json +5 -5
  7. data/assets/picker.js +1 -1
  8. data/assets/tracker.js +1 -1
  9. data/lib/runlight/accounts/accounts.rb +40 -10
  10. data/lib/runlight/accounts/crypto.rb +6 -3
  11. data/lib/runlight/accounts/lockout.rb +98 -0
  12. data/lib/runlight/accounts/throttle.rb +70 -16
  13. data/lib/runlight/accounts/web.rb +71 -28
  14. data/lib/runlight/body.rb +13 -0
  15. data/lib/runlight/core.rb +194 -28
  16. data/lib/runlight/data/sources.rb +1 -2
  17. data/lib/runlight/db/connect.rb +2 -2
  18. data/lib/runlight/db/db.rb +52 -32
  19. data/lib/runlight/geo.rb +2 -2
  20. data/lib/runlight/http/response.rb +22 -3
  21. data/lib/runlight/icon.rb +2 -1
  22. data/lib/runlight/importers/csv_visits.rb +13 -3
  23. data/lib/runlight/importers/dub.rb +2 -1
  24. data/lib/runlight/importers/index.rb +2 -1
  25. data/lib/runlight/importers/umami.rb +3 -0
  26. data/lib/runlight/importers/visits.rb +16 -5
  27. data/lib/runlight/importers/write.rb +50 -12
  28. data/lib/runlight/links.rb +10 -1
  29. data/lib/runlight/mcp.rb +10 -1
  30. data/lib/runlight/oauth.rb +34 -23
  31. data/lib/runlight/payload.rb +26 -7
  32. data/lib/runlight/rack_app.rb +7 -1
  33. data/lib/runlight/routes.rb +51 -21
  34. data/lib/runlight/server/cli.rb +9 -0
  35. data/lib/runlight/server/db_ip.rb +287 -45
  36. data/lib/runlight/server/standalone.rb +41 -5
  37. data/lib/runlight/sources.rb +3 -2
  38. data/lib/runlight/store/sql.rb +14 -0
  39. data/lib/runlight/store/sql_store.rb +88 -59
  40. data/lib/runlight/ua.rb +14 -4
  41. data/lib/runlight/version.rb +1 -1
  42. data/lib/runlight/weak_keys.rb +31 -0
  43. data/lib/runlight.rb +2 -0
  44. metadata +6 -4
@@ -374,7 +374,9 @@ module Runlight
374
374
 
375
375
  # Deletes every salt but those of these days, so the hashes made with them can never be made again.
376
376
  def drop_salts_except(days)
377
- @db.run("DELETE FROM rl_salts WHERE day NOT IN (#{(["?"] * days.length).join(", ")})", days)
377
+ # Never a day after the newest of these: a process whose clock is a moment behind must not delete the
378
+ # salt another has just made for the day that began.
379
+ @db.run("DELETE FROM rl_salts WHERE day NOT IN (#{(["?"] * days.length).join(", ")}) AND day < ?", [*days, days.max])
378
380
  end
379
381
 
380
382
  # Whether a visit to one of these sites that started in a stretch of time was still going at `since`.
@@ -509,6 +511,12 @@ module Runlight
509
511
  rows[0].nil? ? nil : Sql.link_row(rows[0])
510
512
  end
511
513
 
514
+ # Whether a link with this id was ever made, a deleted one included: its row stays, hidden, with its id and
515
+ # its clicks. An import asks this, so a link it brought in once and that was deleted since is left deleted.
516
+ def link_exists(id)
517
+ !@db.all("SELECT 1 AS x FROM rl_links WHERE id = ? LIMIT 1", [id]).empty?
518
+ end
519
+
512
520
  def insert_link(link)
513
521
  @db.run(
514
522
  "INSERT INTO rl_links (id, site, domain, slug, name, url, created_at, updated_at) VALUES (?, ?, ?, ?, ?, ?, ?, ?)",
@@ -861,13 +869,13 @@ module Runlight
861
869
  # Each key as a row of its own, compared and sorted by code point like every other value.
862
870
  @db.all(
863
871
  "SELECT j.k AS \"key\", COUNT(*) AS events FROM #{v["from"]}
864
- CROSS JOIN JSON_TABLE(JSON_KEYS(CASE WHEN JSON_TYPE(e.props) = 'OBJECT' THEN e.props ELSE '{}' END), '$[*]' COLUMNS (k VARCHAR(255) COLLATE #{Sql::MYSQL_COLLATION} PATH '$')) j
872
+ CROSS JOIN JSON_TABLE(JSON_KEYS(CASE WHEN JSON_TYPE(#{Sql.readable_props("mysql")}) = 'OBJECT' THEN e.props ELSE '{}' END), '$[*]' COLUMNS (k VARCHAR(255) COLLATE #{Sql::MYSQL_COLLATION} PATH '$')) j
865
873
  WHERE #{where} GROUP BY j.k ORDER BY events DESC, j.k LIMIT 30",
866
874
  params,
867
875
  )
868
876
  when "postgres"
869
877
  @db.all(
870
- "SELECT k AS \"key\", COUNT(*) AS events FROM #{v["from"]} CROSS JOIN LATERAL jsonb_object_keys(CASE WHEN jsonb_typeof(e.props::jsonb) = 'object' THEN e.props::jsonb ELSE '{}'::jsonb END) AS k
878
+ "SELECT k AS \"key\", COUNT(*) AS events FROM #{v["from"]} CROSS JOIN LATERAL jsonb_object_keys(CASE WHEN jsonb_typeof(#{Sql.readable_props("postgres")}) = 'object' THEN #{Sql.readable_props("postgres")} ELSE '{}'::jsonb END) AS k
871
879
  WHERE #{where} GROUP BY k ORDER BY events DESC, k#{text_order} LIMIT 30",
872
880
  params,
873
881
  )
@@ -886,8 +894,8 @@ module Runlight
886
894
  def event_prop_values(query, event, key, limit)
887
895
  v = Sql.visit_rows(query["filters"], query["site"], query["from"], query["to"], @db.dialect)
888
896
  value = case @db.dialect
889
- when "postgres" then "(e.props::jsonb ->> ?)"
890
- when "mysql" then "(JSON_UNQUOTE(JSON_EXTRACT(e.props, ?)) COLLATE #{Sql::MYSQL_COLLATION})"
897
+ when "postgres" then "(#{Sql.readable_props("postgres")} ->> ?)"
898
+ when "mysql" then "(JSON_UNQUOTE(JSON_EXTRACT(#{Sql.readable_props("mysql")}, ?)) COLLATE #{Sql::MYSQL_COLLATION})"
891
899
  else "CAST(json_extract(e.props, ?) AS TEXT)"
892
900
  end
893
901
  path = @db.dialect == "postgres" ? key : "$.\"#{key}\""
@@ -1139,6 +1147,13 @@ module Runlight
1139
1147
  row.nil? || row["t"].nil? ? nil : Sql.num(row["t"])
1140
1148
  end
1141
1149
 
1150
+ # When a site's oldest short link click was, or nil with none. Clicks imported as daily counts belong to
1151
+ # no visit, so first_seen does not reach them.
1152
+ def first_click(site)
1153
+ row = @db.all("SELECT MIN(ts) AS t FROM rl_events WHERE site = ? AND kind = 'click'", [site])[0]
1154
+ row.nil? || row["t"].nil? ? nil : Sql.num(row["t"])
1155
+ end
1156
+
1142
1157
  # Just the visitor count from stats(), in one query, for conversion rates.
1143
1158
  def visitors(query)
1144
1159
  # Built days add up their visitors, as the stats do.
@@ -1658,7 +1673,7 @@ module Runlight
1658
1673
  end
1659
1674
 
1660
1675
  # A breakdown of a visit dimension or of pages from rollups and the visits
1661
- # left over, merged, then sorted and cut to the page asked for.
1676
+ # left over, merged, sorted, and cut to the page asked for by the database.
1662
1677
  def rolled_breakdown(query, dimension, limit, offset)
1663
1678
  page = dimension == "page"
1664
1679
  event = dimension == "event"
@@ -1669,83 +1684,97 @@ module Runlight
1669
1684
  plan = rollup_plan(query, query["from"], query["to"]) || (page || event ? { "days" => [], "rest" => [[query["from"], query["to"]]] } : nil)
1670
1685
  return nil if plan.nil?
1671
1686
 
1672
- zero = { "visitors" => 0, "visits" => 0, "pageviews" => 0, "bounced" => 0, "duration" => 0, "engaged" => 0, "views" => 0, "scroll_sum" => 0, "scroll_n" => 0, "events" => 0 }
1673
- # Keyed by value in the order first seen.
1674
- sums = {}
1675
- bump = lambda do |row|
1676
- key = Sql.string(row["value"])
1677
- sums[key] = (sums[key] || zero).to_h { |k, n| [k, n + Sql.num(row[k])] }
1678
- end
1687
+ # Each source adds up its own rows by value, and the database adds the sources together, sorts them,
1688
+ # and cuts them to the page asked for, so no more than that page is ever read into memory.
1689
+ columns = %w[visitors visits pageviews bounced duration engaged views scroll_sum scroll_n events]
1690
+ part = ->(value, counted) { "#{value} AS value, #{columns.map { |c| "#{counted[c] || "0"} AS #{c}" }.join(", ")}" }
1691
+ parts = []
1692
+ params = []
1679
1693
  unless plan["days"].empty?
1680
- rolled = @db.all(
1681
- "SELECT value, SUM(visitors) AS visitors, SUM(visits) AS visits, SUM(pageviews) AS pageviews, SUM(bounced) AS bounced, SUM(duration) AS duration,
1682
- SUM(engaged) AS engaged, SUM(views) AS views, SUM(scroll_sum) AS scroll_sum, SUM(scroll_n) AS scroll_n, SUM(events) AS events
1683
- FROM rl_rollups WHERE site = ? AND dim = ? AND day IN (#{Sql::BUILT_DAYS}) GROUP BY value",
1684
- [query["site"], dimension, query["site"], query["from"], query["to"]],
1685
- )
1686
- rolled.each { |row| bump.call(row) }
1694
+ parts << "SELECT value, #{columns.join(", ")} FROM rl_rollups WHERE site = ? AND dim = ? AND day IN (#{Sql::BUILT_DAYS})"
1695
+ params.push(query["site"], dimension, query["site"], query["from"], query["to"])
1687
1696
  end
1688
1697
  w = within(plan["rest"])
1689
- if (page || event) && !plan["rest"].empty?
1698
+ of_rest = nil
1699
+ at = []
1700
+ unless plan["rest"].empty?
1690
1701
  # A visit's pageviews and events belong to the day it started, as in the rollups.
1691
1702
  # Bounded by time as well, so the events index finds them (see build_rollup_day).
1692
1703
  lo = plan["rest"].map { |r| r[0] }.min
1693
1704
  hi = plan["rest"].map { |r| r[1] }.max + Sql::EVENT_TAIL_MS
1694
- of_rest = ->(kind) { "FROM rl_events e JOIN rl_sessions s ON s.id = e.session WHERE e.site = ? AND e.kind = '#{kind}' AND e.ts >= ? AND e.ts < ? AND #{Sql::IS_VISIT} AND #{w["sql"]}" }
1705
+ of_rest = lambda do |kind|
1706
+ "FROM #{Sql.event_kind_table(@db.dialect)} CROSS JOIN rl_sessions s WHERE s.id = e.session AND e.site = ? AND e.kind = '#{kind}' AND e.ts >= ? AND e.ts < ? AND #{Sql::IS_VISIT} AND #{w["sql"]}"
1707
+ end
1695
1708
  at = [query["site"], lo, hi, *w["params"]]
1696
1709
  if page
1697
- @db.all("SELECT e.path AS value, COUNT(DISTINCT e.visitor) AS visitors, COUNT(DISTINCT e.session) AS visits, COUNT(*) AS pageviews, #{Sql::LIVE_VIEWS} AS views #{of_rest.call("pageview")} GROUP BY e.path", at).each { |row| bump.call(row) }
1698
- @db.all(
1699
- "SELECT value, SUM(total) AS engaged, SUM(deepest) AS scroll_sum, COUNT(deepest) AS scroll_n FROM (
1700
- SELECT e.path AS value, SUM(e.engaged_ms) AS total, MAX(e.scroll) AS deepest #{of_rest.call("engagement")} GROUP BY e.path, e.pageview) t GROUP BY value",
1701
- at,
1702
- ).each { |row| bump.call(row) }
1710
+ parts << "SELECT #{part.call("e.path", { "visitors" => "COUNT(DISTINCT e.visitor)", "pageviews" => "COUNT(*)", "views" => Sql::LIVE_VIEWS })} #{of_rest.call("pageview")} GROUP BY e.path"
1711
+ params.concat(at)
1712
+ elsif event
1713
+ parts << "SELECT #{part.call("e.name", { "visitors" => "COUNT(DISTINCT e.visitor)", "events" => "COUNT(*)" })} #{of_rest.call("event")} GROUP BY e.name"
1714
+ params.concat(at)
1703
1715
  else
1704
- @db.all("SELECT e.name AS value, COUNT(DISTINCT e.visitor) AS visitors, COUNT(*) AS events #{of_rest.call("event")} GROUP BY e.name", at).each { |row| bump.call(row) }
1716
+ col = "s.#{Query::SESSION_DIMENSIONS[dimension]}"
1717
+ counted = { "visitors" => "COUNT(DISTINCT s.visitor)", "visits" => "COUNT(*)", "pageviews" => "SUM(s.pageviews)",
1718
+ "bounced" => "SUM(CASE WHEN #{Sql::BOUNCE} THEN 1 ELSE 0 END)", "duration" => "SUM(#{Sql::DURATION})" }
1719
+ parts << "SELECT #{part.call(col, counted)} FROM rl_sessions s WHERE s.site = ? AND #{Sql::IS_VISIT} AND #{w["sql"]} AND #{col} <> '' GROUP BY #{col}"
1720
+ params.push(query["site"], *w["params"])
1705
1721
  end
1706
- elsif page || event
1707
- # Every day of the range is built.
1708
- else
1709
- col = "s.#{Query::SESSION_DIMENSIONS[dimension]}"
1710
- @db.all(
1711
- "SELECT #{col} AS value, COUNT(DISTINCT s.visitor) AS visitors, COUNT(*) AS visits, SUM(s.pageviews) AS pageviews,
1712
- SUM(CASE WHEN #{Sql::BOUNCE} THEN 1 ELSE 0 END) AS bounced, SUM(#{Sql::DURATION}) AS duration
1713
- FROM rl_sessions s WHERE s.site = ? AND #{Sql::IS_VISIT} AND #{w["sql"]} AND #{col} <> '' GROUP BY #{col}",
1714
- [query["site"], *w["params"]],
1715
- ).each { |row| bump.call(row) }
1716
1722
  end
1723
+ # Every day of a range of pages or events may be built already, and none of one with nothing in it.
1724
+ return [] if parts.empty?
1725
+
1717
1726
  entry_exit = dimension == "entry" || dimension == "exit"
1718
- rows = sums.select do |value, x|
1719
- (event || value != "") && (page ? x["pageviews"].positive? : (event ? x["events"].positive? : x["visits"].positive?))
1720
- end.to_a
1721
- rows.sort! do |(a, x), (b, y)|
1722
- order = if entry_exit then [y["visits"] - x["visits"]]
1723
- elsif event then [y["visitors"] - x["visitors"], y["events"] - x["events"]]
1724
- elsif page then [y["visitors"] - x["visitors"], y["pageviews"] - x["pageviews"]]
1725
- else [y["visitors"] - x["visitors"], y["visits"] - x["visits"]]
1726
- end
1727
- d = order.find { |n| n != 0 }
1728
- d.nil? ? Sql.code_order(a, b) : (d.negative? ? -1 : 1)
1727
+ order = if entry_exit then "n_visits DESC"
1728
+ elsif event then "n_visitors DESC, n_events DESC"
1729
+ elsif page then "n_visitors DESC, n_pageviews DESC"
1730
+ else "n_visitors DESC, n_visits DESC"
1731
+ end
1732
+ rows = @db.all(
1733
+ "SELECT value, #{columns.map { |c| "SUM(#{c}) AS n_#{c}" }.join(", ")} FROM (#{parts.join(" UNION ALL ")}) t
1734
+ #{event ? "" : "WHERE value <> ''"} GROUP BY value ORDER BY #{order}, value#{text_order} LIMIT ? OFFSET ?",
1735
+ [*params, limit, offset],
1736
+ )
1737
+ # The engaged time and scroll of the pages on this page of the report, from the visits no built day holds.
1738
+ rest = {}
1739
+ if page && !plan["rest"].empty? && !rows.empty?
1740
+ # Engaged time is added up per pageview first.
1741
+ size = [1, [Sql::VALUES_PER_QUERY, Sql::MAX_PARAMS - at.length - 4].min].max
1742
+ engaged = Sql.in_pieces(rows.map { |row| Sql.string(row["value"]) }, size) do |piece|
1743
+ @db.all(
1744
+ "SELECT value, SUM(total) AS engaged, SUM(deepest) AS scroll_sum, COUNT(deepest) AS scroll_n FROM (
1745
+ SELECT e.path AS value, SUM(e.engaged_ms) AS total, MAX(e.scroll) AS deepest #{of_rest.call("engagement")} AND e.path IN (#{(["?"] * piece.length).join(", ")})
1746
+ GROUP BY e.path, e.pageview) t GROUP BY value",
1747
+ [*at, *piece],
1748
+ )
1749
+ end
1750
+ engaged.each do |row|
1751
+ into = (rest[Sql.string(row["value"])] ||= { "engaged" => 0, "scroll_sum" => 0, "scroll_n" => 0 })
1752
+ %w[engaged scroll_sum scroll_n].each { |k| into[k] += Sql.num(row[k]) }
1753
+ end
1729
1754
  end
1730
- (rows[offset, limit] || []).map do |value, x|
1755
+ rows.map do |row|
1756
+ value = Sql.string(row["value"])
1757
+ x = columns.to_h { |c| [c, Sql.num(row["n_#{c}"])] }
1731
1758
  if event
1732
1759
  { "value" => value, "visitors" => x["visitors"], "events" => x["events"] }
1733
1760
  elsif page
1761
+ more = rest[value] || { "engaged" => 0, "scroll_sum" => 0, "scroll_n" => 0 }
1762
+ scroll_n = x["scroll_n"] + more["scroll_n"]
1734
1763
  {
1735
1764
  "value" => value,
1736
1765
  "visitors" => x["visitors"],
1737
1766
  "pageviews" => x["pageviews"],
1738
1767
  # Over every pageview that could report its time, counting those that sent none (under a second) as none.
1739
- "timeOnPage" => x["views"].positive? ? whole(Js.round(x["engaged"].fdiv(x["views"]))) : 0,
1740
- "scrollDepth" => x["scroll_n"].positive? ? whole(Js.round(x["scroll_sum"].fdiv(x["scroll_n"]))) : 0,
1768
+ "timeOnPage" => x["views"].positive? ? whole(Js.round((x["engaged"] + more["engaged"]).fdiv(x["views"]))) : 0,
1769
+ "scrollDepth" => scroll_n.positive? ? whole(Js.round((x["scroll_sum"] + more["scroll_sum"]).fdiv(scroll_n))) : 0,
1741
1770
  }
1742
1771
  else
1743
- row = { "value" => value, "visitors" => x["visitors"], "visits" => x["visits"], "bounceRate" => x["visits"].positive? ? whole(x["bounced"].fdiv(x["visits"])) : 0 }
1772
+ out = { "value" => value, "visitors" => x["visitors"], "visits" => x["visits"], "bounceRate" => x["visits"].positive? ? whole(x["bounced"].fdiv(x["visits"])) : 0 }
1744
1773
  unless entry_exit
1745
- row["pageviews"] = x["pageviews"]
1746
- row["visitDuration"] = x["visits"].positive? ? whole(Js.round(x["duration"].fdiv(x["visits"]))) : 0
1774
+ out["pageviews"] = x["pageviews"]
1775
+ out["visitDuration"] = x["visits"].positive? ? whole(Js.round(x["duration"].fdiv(x["visits"]))) : 0
1747
1776
  end
1748
- row
1777
+ out
1749
1778
  end
1750
1779
  end
1751
1780
  end
@@ -1823,14 +1852,14 @@ module Runlight
1823
1852
  def prop_value(prop)
1824
1853
  if @db.dialect == "postgres"
1825
1854
  return {
1826
- "sql" => '(CASE WHEN (e.props::jsonb ->> ?) ~ \'^-?[0-9]+(\.[0-9]+)?$\' THEN (e.props::jsonb ->> ?)::numeric ELSE 0 END)',
1855
+ "sql" => "(CASE WHEN (#{Sql.readable_props("postgres")} ->> ?) ~ '^-?[0-9]+(\\.[0-9]+)?$' THEN (#{Sql.readable_props("postgres")} ->> ?)::numeric ELSE 0 END)",
1827
1856
  "params" => [prop, prop],
1828
1857
  }
1829
1858
  end
1830
1859
  path = "$.\"#{prop}\""
1831
1860
  if @db.dialect == "mysql"
1832
1861
  # As SQLite: a JSON number as it is, or text of digits with an optional sign and one decimal point.
1833
- value = "JSON_EXTRACT(e.props, ?)"
1862
+ value = "JSON_EXTRACT(#{Sql.readable_props("mysql")}, ?)"
1834
1863
  return {
1835
1864
  "sql" => "(CASE
1836
1865
  WHEN JSON_TYPE(#{value}) IN ('INTEGER', 'UNSIGNED INTEGER', 'DOUBLE', 'DECIMAL') THEN CAST(#{value} AS DOUBLE)
data/lib/runlight/ua.rb CHANGED
@@ -25,7 +25,7 @@ module Runlight
25
25
  ["Instagram", /Instagram (\d+)/],
26
26
  ["Firefox", %r{(?:Firefox|FxiOS)/(\d+)}],
27
27
  ["Chrome", %r{(?:CriOS|Chrome)/(\d+)}],
28
- ["Safari", %r{Version/(\d+)[\d.]* (?:Mobile/[^#{Js::SPACE}]+ )?Safari/}],
28
+ ["Safari", %r{Version/(\d+)(?:\.[\d.]*)? (?:Mobile/[^#{Js::SPACE}]+ )?Safari/}],
29
29
  ["Internet Explorer", %r{(?:MSIE |Trident/#{DOT}*rv:)(\d+)}],
30
30
  ].freeze
31
31
 
@@ -42,15 +42,24 @@ module Runlight
42
42
  PERSON = /mozilla|opera/in
43
43
  private_constant :BROWSERS, :WINDOWS, :IOS, :PERSON
44
44
 
45
+ # The most of a user agent, or of a client hint, that is read: a real one is far shorter, and nothing past
46
+ # it is parsed.
47
+ MAX_UA = 512
48
+
45
49
  module_function
46
50
 
51
+ def cap(value)
52
+ Js.length(value) > MAX_UA ? Js.slice(value, 0, MAX_UA) : Js.scrub(value)
53
+ end
54
+
47
55
  # The AI agent a user agent names: a Hash {"name", "company", "kind", "token"}, or nil.
48
56
  def ai_agent(ua)
49
- lower = Js.lower(ua)
57
+ lower = Js.lower(cap(ua))
50
58
  Data::Agents::AI_AGENTS.find { |agent| lower.include?(agent["token"]) }
51
59
  end
52
60
 
53
61
  def bot?(ua)
62
+ ua = cap(ua)
54
63
  return true if Js.length(ua) < 20 || !ua.b.match?(PERSON)
55
64
 
56
65
  ua.b.match?(Data::Agents::BOT_PATTERN)
@@ -58,7 +67,8 @@ module Runlight
58
67
 
59
68
  # hints: {"brands", "mobile", "platform"}, each optional. screen_width: Integer, Float, or nil.
60
69
  def parse_client(ua, hints = {}, screen_width = nil)
61
- ua = Js.scrub(ua)
70
+ ua = cap(ua)
71
+ hints = { "brands" => cap(hints["brands"] || ""), "mobile" => cap(hints["mobile"] || ""), "platform" => cap(hints["platform"] || "") }
62
72
  browser = "Other"
63
73
  browser_version = ""
64
74
  BROWSERS.each do |name, pattern|
@@ -115,6 +125,6 @@ module Runlight
115
125
  Js.trim((value || "").delete('"'))
116
126
  end
117
127
 
118
- private_class_method :unquote
128
+ private_class_method :unquote, :cap
119
129
  end
120
130
  end
@@ -4,7 +4,7 @@ require "json"
4
4
 
5
5
  module Runlight
6
6
  # The gem's own version, which the release keeps equal to the npm package's.
7
- GEM_VERSION = "0.4.3"
7
+ GEM_VERSION = "0.5.0"
8
8
 
9
9
  # The SDK's version and the HTTP API's, read from assets/build.json, which
10
10
  # scripts/ruby-assets.mts writes from the TypeScript SDK, so the two always
@@ -0,0 +1,31 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Runlight
4
+ # A map whose entries go when their key is collected, and whose values are held firmly until then: on Ruby
5
+ # 3.3 and later, ObjectSpace::WeakKeyMap itself. Ruby 3.2 has no WeakKeyMap, and its ObjectSpace::WeakMap
6
+ # holds values weakly too, so a value nothing else holds (a rate count, a request's context) could be
7
+ # collected while its key is in use. There, the value is kept on the key, in an instance variable this map
8
+ # alone uses, and goes with the key. Only [], []=, and key? are offered, which is all Runlight uses.
9
+ WeakKeys =
10
+ if defined?(ObjectSpace::WeakKeyMap)
11
+ ObjectSpace::WeakKeyMap
12
+ else
13
+ Class.new do
14
+ def initialize
15
+ @name = :"@__runlight_weak_keys_#{object_id}"
16
+ end
17
+
18
+ def [](key)
19
+ key.instance_variable_defined?(@name) ? key.instance_variable_get(@name) : nil
20
+ end
21
+
22
+ def []=(key, value)
23
+ key.instance_variable_set(@name, value)
24
+ end
25
+
26
+ def key?(key)
27
+ key.instance_variable_defined?(@name)
28
+ end
29
+ end
30
+ end
31
+ end
data/lib/runlight.rb CHANGED
@@ -55,6 +55,7 @@ module Runlight
55
55
  autoload :SettingsError, "runlight/settings_error"
56
56
  autoload :Sources, "runlight/sources"
57
57
  autoload :TablesBusyError, "runlight/tables_busy_error"
58
+ autoload :WeakKeys, "runlight/weak_keys"
58
59
  autoload :Ua, "runlight/ua"
59
60
  autoload :Zip, "runlight/zip"
60
61
 
@@ -64,6 +65,7 @@ module Runlight
64
65
  autoload :Crypto, "runlight/accounts/crypto"
65
66
  autoload :Pages, "runlight/accounts/pages"
66
67
  autoload :Scrypt, "runlight/accounts/scrypt"
68
+ autoload :Lockout, "runlight/accounts/lockout"
67
69
  autoload :Throttle, "runlight/accounts/throttle"
68
70
  autoload :Web, "runlight/accounts/web"
69
71
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: runlight
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.3
4
+ version: 0.5.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jon C. Phillips
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2026-11-10 00:00:00.000000000 Z
11
+ date: 2026-11-15 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activerecord
@@ -16,7 +16,7 @@ dependencies:
16
16
  requirements:
17
17
  - - ">="
18
18
  - !ruby/object:Gem::Version
19
- version: '7.2'
19
+ version: '8.0'
20
20
  - - "<"
21
21
  - !ruby/object:Gem::Version
22
22
  version: '8.2'
@@ -26,7 +26,7 @@ dependencies:
26
26
  requirements:
27
27
  - - ">="
28
28
  - !ruby/object:Gem::Version
29
- version: '7.2'
29
+ version: '8.0'
30
30
  - - "<"
31
31
  - !ruby/object:Gem::Version
32
32
  version: '8.2'
@@ -58,6 +58,7 @@ files:
58
58
  - lib/runlight/accounts/account_error.rb
59
59
  - lib/runlight/accounts/accounts.rb
60
60
  - lib/runlight/accounts/crypto.rb
61
+ - lib/runlight/accounts/lockout.rb
61
62
  - lib/runlight/accounts/pages.rb
62
63
  - lib/runlight/accounts/scrypt.rb
63
64
  - lib/runlight/accounts/throttle.rb
@@ -148,6 +149,7 @@ files:
148
149
  - lib/runlight/ua.rb
149
150
  - lib/runlight/undefined.rb
150
151
  - lib/runlight/version.rb
152
+ - lib/runlight/weak_keys.rb
151
153
  - lib/runlight/zip.rb
152
154
  homepage: https://runlight.sh
153
155
  licenses: