protocol-redis 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.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: eab7d3758fbb84ad4e85cbeae48dc4a370c5dcab35c6db79cf4ac7d0b4f23e03
4
+ data.tar.gz: d052a525167a7f439308f143780d6b4bfe4884b4593fc56b7e4bea6f1caf8598
5
+ SHA512:
6
+ metadata.gz: 5ba96c540da05dc4a029dbaba7599bcc267b4cb94e311f8b5064116fc77a1cb581bcec7bc98bb70c8e624ac277d39b8e9cfedb3b3084ec0b9e5ce677123f2d88
7
+ data.tar.gz: af8434897aa46a45c949029dc180c478aeeae818524100da114e09e8fb8b2268914b184c23de9709ea85b1afd1de6bc8e0527c67bdbadf73838510f6738c70bf
@@ -0,0 +1,6 @@
1
+ root = true
2
+
3
+ [*]
4
+ indent_style = tab
5
+ indent_size = 2
6
+
@@ -0,0 +1,13 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+
11
+ # rspec failure tracking
12
+ .rspec_status
13
+ .covered.db
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --warnings
3
+ --require spec_helper
@@ -0,0 +1,23 @@
1
+ language: ruby
2
+ dist: xenial
3
+ cache: bundler
4
+
5
+ services:
6
+ - redis-server
7
+
8
+ matrix:
9
+ include:
10
+ - rvm: 2.3
11
+ - rvm: 2.4
12
+ - rvm: 2.5
13
+ - rvm: 2.6
14
+ - rvm: 2.6
15
+ env: COVERAGE=BriefSummary,Coveralls
16
+ - rvm: ruby-head
17
+ - rvm: truffleruby
18
+ - rvm: jruby-head
19
+ env: JRUBY_OPTS="--debug -X+O"
20
+ allow_failures:
21
+ - rvm: ruby-head
22
+ - rvm: truffleruby
23
+ - rvm: jruby-head
data/Gemfile ADDED
@@ -0,0 +1,8 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in async-io.gemspec
4
+ gemspec
5
+
6
+ group :development do
7
+ gem 'pry'
8
+ end
@@ -0,0 +1,58 @@
1
+ # Protocol::Redis
2
+
3
+ Implements the RESP2 and future redis protocols.
4
+
5
+ [![Build Status](https://travis-ci.com/socketry/protocol-redis.svg?branch=master)](https://travis-ci.com/socketry/protocol-redis)
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ ```ruby
12
+ gem 'protocol-redis'
13
+ ```
14
+
15
+ And then execute:
16
+
17
+ $ bundle
18
+
19
+ Or install it yourself as:
20
+
21
+ $ gem install protocol-redis
22
+
23
+ ## Usage
24
+
25
+ ...
26
+
27
+ ## Contributing
28
+
29
+ 1. Fork it
30
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
31
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
32
+ 4. Push to the branch (`git push origin my-new-feature`)
33
+ 5. Create new Pull Request
34
+
35
+ ## License
36
+
37
+ Released under the MIT license.
38
+
39
+ Copyright, 2019, by [Samuel G. D. Williams](http://www.codeotaku.com/samuel-williams).
40
+ Copyright, 2019, by Huba Z. Nagy.
41
+
42
+ Permission is hereby granted, free of charge, to any person obtaining a copy
43
+ of this software and associated documentation files (the "Software"), to deal
44
+ in the Software without restriction, including without limitation the rights
45
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
46
+ copies of the Software, and to permit persons to whom the Software is
47
+ furnished to do so, subject to the following conditions:
48
+
49
+ The above copyright notice and this permission notice shall be included in
50
+ all copies or substantial portions of the Software.
51
+
52
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
53
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
54
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
55
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
56
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
57
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
58
+ THE SOFTWARE.
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:test)
5
+
6
+ task :default => :test
@@ -0,0 +1,22 @@
1
+ # Copyright, 2018, by Samuel G. D. Williams. <http://www.codeotaku.com>
2
+ #
3
+ # Permission is hereby granted, free of charge, to any person obtaining a copy
4
+ # of this software and associated documentation files (the "Software"), to deal
5
+ # in the Software without restriction, including without limitation the rights
6
+ # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7
+ # copies of the Software, and to permit persons to whom the Software is
8
+ # furnished to do so, subject to the following conditions:
9
+ #
10
+ # The above copyright notice and this permission notice shall be included in
11
+ # all copies or substantial portions of the Software.
12
+ #
13
+ # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14
+ # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15
+ # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16
+ # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17
+ # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18
+ # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
19
+ # THE SOFTWARE.
20
+
21
+ require_relative 'redis/version'
22
+ require_relative 'redis/client'
@@ -0,0 +1,147 @@
1
+ # Copyright, 2019, by Samuel G. D. Williams. <http://www.codeotaku.com>
2
+ #
3
+ # Permission is hereby granted, free of charge, to any person obtaining a copy
4
+ # of this software and associated documentation files (the "Software"), to deal
5
+ # in the Software without restriction, including without limitation the rights
6
+ # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7
+ # copies of the Software, and to permit persons to whom the Software is
8
+ # furnished to do so, subject to the following conditions:
9
+ #
10
+ # The above copyright notice and this permission notice shall be included in
11
+ # all copies or substantial portions of the Software.
12
+ #
13
+ # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14
+ # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15
+ # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16
+ # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17
+ # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18
+ # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
19
+ # THE SOFTWARE.
20
+
21
+ require 'async/io/protocol/line'
22
+
23
+ module Protocol
24
+ module Redis
25
+ class Connection
26
+ CRLF = "\r\n".freeze
27
+
28
+ def initialize(stream)
29
+ @stream = stream
30
+ end
31
+
32
+ attr :stream
33
+
34
+ def close
35
+ @stream.close
36
+ end
37
+
38
+ class << self
39
+ alias client new
40
+ end
41
+
42
+ def flush
43
+ @stream.flush
44
+ end
45
+
46
+ def closed?
47
+ @stream.closed?
48
+ end
49
+
50
+ # The redis server doesn't want actual objects (e.g. integers) but only bulk strings. So, we inline it for performance.
51
+ def write_request(arguments)
52
+ write_lines("*#{arguments.count}")
53
+
54
+ arguments.each do |argument|
55
+ string = argument.to_s
56
+
57
+ write_lines("$#{string.bytesize}", string)
58
+ end
59
+ end
60
+
61
+ def write_object(object)
62
+ case object
63
+ when String
64
+ write_lines("$#{object.bytesize}", object)
65
+ when Array
66
+ write_array(object)
67
+ when Integer
68
+ write_lines(":#{object}")
69
+ else
70
+ write_object(object.to_redis)
71
+ end
72
+ end
73
+
74
+ def read_data(length)
75
+ buffer = @stream.read(length) or @stream.eof!
76
+
77
+ # Eat trailing whitespace because length does not include the CRLF:
78
+ @stream.read(2) or @stream.eof!
79
+
80
+ return buffer
81
+ end
82
+
83
+ def read_object
84
+ line = read_line
85
+ token = line.slice!(0, 1)
86
+
87
+ case token
88
+ when '$'
89
+ length = line.to_i
90
+
91
+ if length == -1
92
+ return nil
93
+ else
94
+ return read_data(length)
95
+ end
96
+ when '*'
97
+ count = line.to_i
98
+
99
+ # Null array (https://redis.io/topics/protocol#resp-arrays):
100
+ return nil if count == -1
101
+
102
+ array = Array.new(count) {read_object}
103
+
104
+ return array
105
+ when ':'
106
+ return line.to_i
107
+
108
+ when '-'
109
+ raise ServerError.new(line)
110
+
111
+ when '+'
112
+ return line
113
+
114
+ else
115
+ @stream.flush
116
+
117
+ raise NotImplementedError, "Implementation for token #{token} missing"
118
+ end
119
+
120
+ # TODO: If an exception (e.g. Async::TimeoutError) propagates out of this function, perhaps @stream should be closed? Otherwise it might be in a weird state.
121
+ end
122
+
123
+ alias read_response read_object
124
+
125
+ private
126
+
127
+ # In the case of Redis, we do not want to perform a flush in every line,
128
+ # because each Redis command contains several lines. Flushing once per
129
+ # command is more efficient because it avoids unnecessary writes to the
130
+ # socket.
131
+ def write_lines(*args)
132
+ if args.empty?
133
+ @stream.write(CRLF)
134
+ else
135
+ args.each do |arg|
136
+ @stream.write(arg)
137
+ @stream.write(CRLF)
138
+ end
139
+ end
140
+ end
141
+
142
+ def read_line
143
+ @stream.gets(CRLF, chomp: true)
144
+ end
145
+ end
146
+ end
147
+ end
@@ -0,0 +1,29 @@
1
+ # Copyright, 2019, by Samuel G. D. Williams. <http://www.codeotaku.com>
2
+ #
3
+ # Permission is hereby granted, free of charge, to any person obtaining a copy
4
+ # of this software and associated documentation files (the "Software"), to deal
5
+ # in the Software without restriction, including without limitation the rights
6
+ # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7
+ # copies of the Software, and to permit persons to whom the Software is
8
+ # furnished to do so, subject to the following conditions:
9
+ #
10
+ # The above copyright notice and this permission notice shall be included in
11
+ # all copies or substantial portions of the Software.
12
+ #
13
+ # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14
+ # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15
+ # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16
+ # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17
+ # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18
+ # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
19
+ # THE SOFTWARE.
20
+
21
+ module Protocol
22
+ module Redis
23
+ class Error < StandardError
24
+ end
25
+
26
+ class ServerError < Error
27
+ end
28
+ end
29
+ end
@@ -0,0 +1,25 @@
1
+ # Copyright, 2018, by Samuel G. D. Williams. <http://www.codeotaku.com>
2
+ #
3
+ # Permission is hereby granted, free of charge, to any person obtaining a copy
4
+ # of this software and associated documentation files (the "Software"), to deal
5
+ # in the Software without restriction, including without limitation the rights
6
+ # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7
+ # copies of the Software, and to permit persons to whom the Software is
8
+ # furnished to do so, subject to the following conditions:
9
+ #
10
+ # The above copyright notice and this permission notice shall be included in
11
+ # all copies or substantial portions of the Software.
12
+ #
13
+ # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14
+ # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15
+ # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16
+ # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17
+ # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18
+ # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
19
+ # THE SOFTWARE.
20
+
21
+ module Protocol
22
+ module Redis
23
+ VERSION = "0.1.0"
24
+ end
25
+ end
@@ -0,0 +1,27 @@
1
+
2
+ require_relative 'lib/protocol/redis/version'
3
+
4
+ Gem::Specification.new do |spec|
5
+ spec.name = "protocol-redis"
6
+ spec.version = Protocol::Redis::VERSION
7
+ spec.authors = ["Samuel Williams", "Huba Nagy"]
8
+ spec.email = ["samuel.williams@oriontransfer.co.nz", "12huba@gmail.com"]
9
+
10
+ spec.summary = "A RESP protocol client/server parser."
11
+ spec.homepage = "https://github.com/socketry/protocol-redis"
12
+
13
+ spec.files = `git ls-files -z`.split("\x0").reject do |f|
14
+ f.match(%r{^(test|spec|features)/})
15
+ end
16
+
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.require_paths = ["lib"]
19
+
20
+ spec.add_development_dependency "async-rspec", "~> 1.1"
21
+ spec.add_development_dependency "benchmark-ips"
22
+
23
+ spec.add_development_dependency "covered"
24
+ spec.add_development_dependency "bundler"
25
+ spec.add_development_dependency "rspec", "~> 3.6"
26
+ spec.add_development_dependency "rake"
27
+ end
metadata ADDED
@@ -0,0 +1,140 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: protocol-redis
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Samuel Williams
8
+ - Huba Nagy
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2019-07-10 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: async-rspec
16
+ requirement: !ruby/object:Gem::Requirement
17
+ requirements:
18
+ - - "~>"
19
+ - !ruby/object:Gem::Version
20
+ version: '1.1'
21
+ type: :development
22
+ prerelease: false
23
+ version_requirements: !ruby/object:Gem::Requirement
24
+ requirements:
25
+ - - "~>"
26
+ - !ruby/object:Gem::Version
27
+ version: '1.1'
28
+ - !ruby/object:Gem::Dependency
29
+ name: benchmark-ips
30
+ requirement: !ruby/object:Gem::Requirement
31
+ requirements:
32
+ - - ">="
33
+ - !ruby/object:Gem::Version
34
+ version: '0'
35
+ type: :development
36
+ prerelease: false
37
+ version_requirements: !ruby/object:Gem::Requirement
38
+ requirements:
39
+ - - ">="
40
+ - !ruby/object:Gem::Version
41
+ version: '0'
42
+ - !ruby/object:Gem::Dependency
43
+ name: covered
44
+ requirement: !ruby/object:Gem::Requirement
45
+ requirements:
46
+ - - ">="
47
+ - !ruby/object:Gem::Version
48
+ version: '0'
49
+ type: :development
50
+ prerelease: false
51
+ version_requirements: !ruby/object:Gem::Requirement
52
+ requirements:
53
+ - - ">="
54
+ - !ruby/object:Gem::Version
55
+ version: '0'
56
+ - !ruby/object:Gem::Dependency
57
+ name: bundler
58
+ requirement: !ruby/object:Gem::Requirement
59
+ requirements:
60
+ - - ">="
61
+ - !ruby/object:Gem::Version
62
+ version: '0'
63
+ type: :development
64
+ prerelease: false
65
+ version_requirements: !ruby/object:Gem::Requirement
66
+ requirements:
67
+ - - ">="
68
+ - !ruby/object:Gem::Version
69
+ version: '0'
70
+ - !ruby/object:Gem::Dependency
71
+ name: rspec
72
+ requirement: !ruby/object:Gem::Requirement
73
+ requirements:
74
+ - - "~>"
75
+ - !ruby/object:Gem::Version
76
+ version: '3.6'
77
+ type: :development
78
+ prerelease: false
79
+ version_requirements: !ruby/object:Gem::Requirement
80
+ requirements:
81
+ - - "~>"
82
+ - !ruby/object:Gem::Version
83
+ version: '3.6'
84
+ - !ruby/object:Gem::Dependency
85
+ name: rake
86
+ requirement: !ruby/object:Gem::Requirement
87
+ requirements:
88
+ - - ">="
89
+ - !ruby/object:Gem::Version
90
+ version: '0'
91
+ type: :development
92
+ prerelease: false
93
+ version_requirements: !ruby/object:Gem::Requirement
94
+ requirements:
95
+ - - ">="
96
+ - !ruby/object:Gem::Version
97
+ version: '0'
98
+ description:
99
+ email:
100
+ - samuel.williams@oriontransfer.co.nz
101
+ - 12huba@gmail.com
102
+ executables: []
103
+ extensions: []
104
+ extra_rdoc_files: []
105
+ files:
106
+ - ".editorconfig"
107
+ - ".gitignore"
108
+ - ".rspec"
109
+ - ".travis.yml"
110
+ - Gemfile
111
+ - README.md
112
+ - Rakefile
113
+ - lib/protocol/redis.rb
114
+ - lib/protocol/redis/connection.rb
115
+ - lib/protocol/redis/error.rb
116
+ - lib/protocol/redis/version.rb
117
+ - protocol-redis.gemspec
118
+ homepage: https://github.com/socketry/protocol-redis
119
+ licenses: []
120
+ metadata: {}
121
+ post_install_message:
122
+ rdoc_options: []
123
+ require_paths:
124
+ - lib
125
+ required_ruby_version: !ruby/object:Gem::Requirement
126
+ requirements:
127
+ - - ">="
128
+ - !ruby/object:Gem::Version
129
+ version: '0'
130
+ required_rubygems_version: !ruby/object:Gem::Requirement
131
+ requirements:
132
+ - - ">="
133
+ - !ruby/object:Gem::Version
134
+ version: '0'
135
+ requirements: []
136
+ rubygems_version: 3.0.3
137
+ signing_key:
138
+ specification_version: 4
139
+ summary: A RESP protocol client/server parser.
140
+ test_files: []