redi_sesh 0.1.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 ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 3742948ebd92791a925900f4429c9b1d608b66e6aeba1e25559d1ddef00c10ce
4
+ data.tar.gz: fd3aabd31233da84f89dfda73a04813ffe8d2e62306f09431a1566d8c3948f64
5
+ SHA512:
6
+ metadata.gz: 4d38609c4528be9ae8ba62845df5a79f9b56bd795e37f227810df013504a1d60b054e0b9d5463800680fa59f485557242540935ec9eaae47e6fd3c77167cc5a4
7
+ data.tar.gz: a8166a9d4902d56783d2af0289908ed6585b883b95c3163f9d0009c85b55f10662f699819611af8ceb5976118f5297a2c226aede6449840f544db02f5aa76300
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --color
2
+ --require spec_helper
data/.rubocop.yml ADDED
@@ -0,0 +1,46 @@
1
+ require: rubocop-rspec
2
+
3
+ AllCops:
4
+ NewCops: enable
5
+ TargetRubyVersion: 2.7
6
+ Exclude:
7
+ - vendor/**/*
8
+
9
+ Lint/AmbiguousBlockAssociation:
10
+ Exclude:
11
+ - spec/**/*
12
+
13
+ Metrics/AbcSize:
14
+ Max: 25
15
+ Metrics/BlockLength:
16
+ Exclude:
17
+ - spec/**/*
18
+ - redi_sesh.gemspec
19
+ Metrics/CyclomaticComplexity:
20
+ Max: 10
21
+ Metrics/MethodLength:
22
+ Max: 20
23
+ Metrics/PerceivedComplexity:
24
+ Max: 10
25
+
26
+ RSpec/ExampleLength:
27
+ Enabled: false
28
+ RSpec/ExpectInHook:
29
+ Enabled: false
30
+ RSpec/MessageSpies:
31
+ Enabled: false
32
+ RSpec/MultipleExpectations:
33
+ Enabled: false
34
+ RSpec/MultipleMemoizedHelpers:
35
+ Enabled: false
36
+ RSpec/NamedSubject:
37
+ Enabled: false
38
+ RSpec/StubbedMock:
39
+ Enabled: false
40
+
41
+ # NOTE: we need to replicate existing functionality of redis-store
42
+ Security/MarshalLoad:
43
+ Enabled: false
44
+
45
+ Style/Documentation:
46
+ Enabled: false
data/CHANGELOG.md ADDED
@@ -0,0 +1,5 @@
1
+ ## [Unreleased]
2
+
3
+ ## [0.1.0] - 2023-02-01
4
+
5
+ - Initial release
data/Gemfile ADDED
@@ -0,0 +1,14 @@
1
+ # frozen_string_literal: true
2
+
3
+ source 'https://rubygems.org'
4
+
5
+ # Specify your gem's dependencies in redi_sesh.gemspec
6
+ gemspec
7
+
8
+ gem 'rake', '~> 13.0'
9
+ gem 'rspec'
10
+ gem 'rubocop'
11
+ gem 'rubocop-rspec'
12
+
13
+ gem 'byebug'
14
+ gem 'mock_redis'
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2023 Kenneth Teh
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
13
+ all 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 NONINFRINGEMENT. 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
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,33 @@
1
+ # RediSesh
2
+
3
+ Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/redi_sesh`. To experiment with that code, run `bin/console` for an interactive prompt.
4
+
5
+ TODO: Delete this and the text above, and describe your gem
6
+
7
+ ## Installation
8
+
9
+ Install the gem and add to the application's Gemfile by executing:
10
+
11
+ $ bundle add redi_sesh
12
+
13
+ If bundler is not being used to manage dependencies, install the gem by executing:
14
+
15
+ $ gem install redi_sesh
16
+
17
+ ## Usage
18
+
19
+ TODO: Write usage instructions here
20
+
21
+ ## Development
22
+
23
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
24
+
25
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and the created tag, and push the `.gem` file to [rubygems.org](https://rubygems.org).
26
+
27
+ ## Contributing
28
+
29
+ Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/redi_sesh.
30
+
31
+ ## License
32
+
33
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
data/Rakefile ADDED
@@ -0,0 +1,13 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'bundler/gem_tasks'
4
+
5
+ require 'rubocop/rake_task'
6
+
7
+ require 'rspec/core/rake_task'
8
+
9
+ RuboCop::RakeTask.new
10
+
11
+ RSpec::Core::RakeTask.new
12
+
13
+ task default: %i[rubocop spec]
@@ -0,0 +1,83 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'English'
4
+ require 'rack/session/abstract/id'
5
+ require_relative '../../redi_sesh/connection_wrapper'
6
+ require_relative '../../redi_sesh/store'
7
+
8
+ module Rack
9
+ module Session
10
+ class Redis < Abstract::PersistedSecure
11
+ attr_reader :mutex
12
+
13
+ DEFAULT_OPTIONS = Abstract::ID::DEFAULT_OPTIONS.merge(
14
+ redis_server: 'redis://127.0.0.1:6379/0/rack:session'
15
+ )
16
+
17
+ def initialize(app, options = {})
18
+ super
19
+
20
+ @mutex = Mutex.new
21
+ @conn = RediSesh::ConnectionWrapper.new(@default_options)
22
+ end
23
+
24
+ def find_session(req, sid)
25
+ if req.session.options[:skip]
26
+ [generate_sid, {}]
27
+ else
28
+ with_lock(req, [nil, {}]) do
29
+ if sid && (session = get_session_with_fallback(sid))
30
+ [sid, session]
31
+ else
32
+ [generate_sid, {}]
33
+ end
34
+ end
35
+ end
36
+ end
37
+
38
+ def write_session(req, sid, new_session, options = {})
39
+ with_lock(req, false) do
40
+ with { |c| c.set(sid.private_id, new_session, options.to_hash) }
41
+ sid
42
+ end
43
+ end
44
+
45
+ def delete_session(req, sid, options)
46
+ with_lock(req) do
47
+ with do |c|
48
+ c.del(sid.public_id)
49
+ c.del(sid.private_id)
50
+ end
51
+ generate_sid unless options[:drop]
52
+ end
53
+ end
54
+
55
+ def threadsafe?
56
+ @default_options.fetch(:threadsafe, true)
57
+ end
58
+
59
+ private
60
+
61
+ def get_session_with_fallback(sid)
62
+ with { |c| c.get(sid.private_id) || c.get(sid.public_id) }
63
+ end
64
+
65
+ def with_lock(_req, default = nil)
66
+ @mutex.lock if threadsafe?
67
+ yield
68
+ rescue Errno::ECONNREFUSED
69
+ if $VERBOSE
70
+ warn "#{self} is unable to find Redis server."
71
+ warn $ERROR_INFO.inspect
72
+ end
73
+ default
74
+ ensure
75
+ @mutex.unlock if @mutex.locked?
76
+ end
77
+
78
+ def with(&block)
79
+ @conn.with(&block)
80
+ end
81
+ end
82
+ end
83
+ end
@@ -0,0 +1,48 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RediSesh
4
+ class ConnectionWrapper
5
+ POOL_KEYS = %i[pool pool_size pool_timeout].freeze
6
+
7
+ def initialize(options = {})
8
+ @options = options
9
+ @store = options[:redis_store]
10
+ @pool = options[:pool]
11
+
12
+ raise ArgumentError, 'pool must be an instance of ConnectionPool' if @pool && !@pool.is_a?(ConnectionPool)
13
+
14
+ return unless @store && !@store.is_a?(RediSesh::Store)
15
+
16
+ raise ArgumentError, "redis_store must be an instance of RediSesh::Store (currently #{@store.class.name})"
17
+ end
18
+
19
+ def with(&block)
20
+ if pooled?
21
+ pool.with(&block)
22
+ else
23
+ block.call(store)
24
+ end
25
+ end
26
+
27
+ def pooled?
28
+ return @pooled if defined?(@pooled)
29
+
30
+ @pooled = POOL_KEYS.any? { |key| @options.key?(key) }
31
+ end
32
+
33
+ def pool
34
+ @pool ||= ConnectionPool.new(pool_options) { store } if pooled?
35
+ end
36
+
37
+ def store
38
+ @store ||= Store.create(@options[:redis_server])
39
+ end
40
+
41
+ def pool_options
42
+ {
43
+ size: @options[:pool_size],
44
+ timeout: @options[:pool_timeout]
45
+ }.compact.to_h
46
+ end
47
+ end
48
+ end
@@ -0,0 +1,73 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'redis'
4
+
5
+ module RediSesh
6
+ class Store
7
+ DEFAULT_EXPIRES_IN = 60 * 60 * 24 # 1 day
8
+
9
+ def self.create(url)
10
+ uri = URI.parse(url)
11
+ parts = uri.path.split('/')
12
+
13
+ parts.shift if parts.first.empty?
14
+
15
+ if parts.size > 1
16
+ path, prefix = parts
17
+ uri.path = "/#{path}"
18
+ else
19
+ prefix = 'sessions'
20
+ end
21
+
22
+ redis = Redis.new(url: uri)
23
+ new(redis: redis, prefix: prefix)
24
+ end
25
+
26
+ def initialize(redis:, prefix:)
27
+ @redis = redis
28
+ @prefix = prefix
29
+ end
30
+
31
+ def info
32
+ redis.info
33
+ end
34
+
35
+ def set(key, value, options = {})
36
+ key = build_key(key)
37
+ expires_in = options.fetch(:expires_in, DEFAULT_EXPIRES_IN)
38
+ value = serialize(value)
39
+ redis.set(key, value, ex: expires_in)
40
+ end
41
+
42
+ def get(key)
43
+ data = redis.get(build_key(key))
44
+ deserialize(data)
45
+ end
46
+
47
+ def del(*keys)
48
+ redis.del(*keys.map { |key| build_key(key) })
49
+ end
50
+
51
+ def keys
52
+ redis.keys("#{prefix}:*").map do |key|
53
+ key.delete_prefix("#{prefix}:")
54
+ end
55
+ end
56
+
57
+ private
58
+
59
+ attr_reader :redis, :prefix
60
+
61
+ def build_key(key)
62
+ "#{prefix}:#{key}"
63
+ end
64
+
65
+ def serialize(data)
66
+ Marshal.dump(data)
67
+ end
68
+
69
+ def deserialize(data)
70
+ data ? Marshal.load(data) : nil
71
+ end
72
+ end
73
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RediSesh
4
+ VERSION = '0.1.0'
5
+ end
data/lib/redi_sesh.rb ADDED
@@ -0,0 +1,7 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative 'redi_sesh/version'
4
+ require_relative 'rack/session/redis'
5
+
6
+ module RediSesh
7
+ end
data/redi_sesh.gemspec ADDED
@@ -0,0 +1,36 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative 'lib/redi_sesh/version'
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = 'redi_sesh'
7
+ spec.version = RediSesh::VERSION
8
+ spec.authors = ['Hieu Nguyen', 'Kenneth Teh']
9
+ spec.email = ['hieu.nguyen@kaligo.com', 'kenneth.teh@kaligo.com']
10
+
11
+ spec.summary = 'Modified version of Rack::Session::Redis to support Redis 4.6.0 and 5.0.0'
12
+ spec.homepage = 'https://github.com/Kaligo/redi_sesh'
13
+ spec.license = 'MIT'
14
+ spec.required_ruby_version = '>= 2.7.0'
15
+
16
+ spec.metadata['homepage_uri'] = spec.homepage
17
+ spec.metadata['source_code_uri'] = 'https://github.com/Kaligo/redi_sesh'
18
+ spec.metadata['changelog_uri'] = 'https://github.com/Kaligo/redi_sesh/blob/master/CHANGELOG.md'
19
+
20
+ # Specify which files should be added to the gem when it is released.
21
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
22
+ spec.files = Dir.chdir(__dir__) do
23
+ `git ls-files -z`.split("\x0").reject do |f|
24
+ (f == __FILE__) || f.match(%r{\A(?:(?:bin|test|spec|features)/|\.(?:git|travis|circleci)|appveyor)})
25
+ end
26
+ end
27
+ spec.bindir = 'exe'
28
+ spec.executables = spec.files.grep(%r{\Aexe/}) { |f| File.basename(f) }
29
+ spec.require_paths = ['lib']
30
+
31
+ spec.add_runtime_dependency 'connection_pool'
32
+ spec.add_runtime_dependency 'rack-session', '>= 0.2.0'
33
+ spec.add_runtime_dependency 'redis', '>= 4.0'
34
+
35
+ spec.metadata['rubygems_mfa_required'] = 'true'
36
+ end
metadata ADDED
@@ -0,0 +1,104 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: redi_sesh
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Hieu Nguyen
8
+ - Kenneth Teh
9
+ autorequire:
10
+ bindir: exe
11
+ cert_chain: []
12
+ date: 2023-05-24 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: connection_pool
16
+ requirement: !ruby/object:Gem::Requirement
17
+ requirements:
18
+ - - ">="
19
+ - !ruby/object:Gem::Version
20
+ version: '0'
21
+ type: :runtime
22
+ prerelease: false
23
+ version_requirements: !ruby/object:Gem::Requirement
24
+ requirements:
25
+ - - ">="
26
+ - !ruby/object:Gem::Version
27
+ version: '0'
28
+ - !ruby/object:Gem::Dependency
29
+ name: rack-session
30
+ requirement: !ruby/object:Gem::Requirement
31
+ requirements:
32
+ - - ">="
33
+ - !ruby/object:Gem::Version
34
+ version: 0.2.0
35
+ type: :runtime
36
+ prerelease: false
37
+ version_requirements: !ruby/object:Gem::Requirement
38
+ requirements:
39
+ - - ">="
40
+ - !ruby/object:Gem::Version
41
+ version: 0.2.0
42
+ - !ruby/object:Gem::Dependency
43
+ name: redis
44
+ requirement: !ruby/object:Gem::Requirement
45
+ requirements:
46
+ - - ">="
47
+ - !ruby/object:Gem::Version
48
+ version: '4.0'
49
+ type: :runtime
50
+ prerelease: false
51
+ version_requirements: !ruby/object:Gem::Requirement
52
+ requirements:
53
+ - - ">="
54
+ - !ruby/object:Gem::Version
55
+ version: '4.0'
56
+ description:
57
+ email:
58
+ - hieu.nguyen@kaligo.com
59
+ - kenneth.teh@kaligo.com
60
+ executables: []
61
+ extensions: []
62
+ extra_rdoc_files: []
63
+ files:
64
+ - ".rspec"
65
+ - ".rubocop.yml"
66
+ - CHANGELOG.md
67
+ - Gemfile
68
+ - LICENSE.txt
69
+ - README.md
70
+ - Rakefile
71
+ - lib/rack/session/redis.rb
72
+ - lib/redi_sesh.rb
73
+ - lib/redi_sesh/connection_wrapper.rb
74
+ - lib/redi_sesh/store.rb
75
+ - lib/redi_sesh/version.rb
76
+ - redi_sesh.gemspec
77
+ homepage: https://github.com/Kaligo/redi_sesh
78
+ licenses:
79
+ - MIT
80
+ metadata:
81
+ homepage_uri: https://github.com/Kaligo/redi_sesh
82
+ source_code_uri: https://github.com/Kaligo/redi_sesh
83
+ changelog_uri: https://github.com/Kaligo/redi_sesh/blob/master/CHANGELOG.md
84
+ rubygems_mfa_required: 'true'
85
+ post_install_message:
86
+ rdoc_options: []
87
+ require_paths:
88
+ - lib
89
+ required_ruby_version: !ruby/object:Gem::Requirement
90
+ requirements:
91
+ - - ">="
92
+ - !ruby/object:Gem::Version
93
+ version: 2.7.0
94
+ required_rubygems_version: !ruby/object:Gem::Requirement
95
+ requirements:
96
+ - - ">="
97
+ - !ruby/object:Gem::Version
98
+ version: '0'
99
+ requirements: []
100
+ rubygems_version: 3.2.28
101
+ signing_key:
102
+ specification_version: 4
103
+ summary: Modified version of Rack::Session::Redis to support Redis 4.6.0 and 5.0.0
104
+ test_files: []