ddp-server 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 4f43570a710a6b121e3cf96bb30e0aa5bf666198
4
+ data.tar.gz: d79437183194ffa2c78477d7d3a0bc7cea34021f
5
+ SHA512:
6
+ metadata.gz: 174a05bbda54f44569370e7db5fceed094892118528636805b8fe245580a86c70e4717c6fe143f982d3cb3af91d229c94d72150584cfef7c2aff6edabf08f49b
7
+ data.tar.gz: 12bc6b927a8d78ca61f1bbe6b62e4550f0848161470449ca26bf48fb89994db117cd64bf774cc4729071966278f1d3db48c101b4263b80709ed7d2f8e71e4437
data/.gitignore ADDED
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/.rubocop.yml ADDED
@@ -0,0 +1,8 @@
1
+ Style/Tab:
2
+ Enabled: false
3
+ Style/IndentationWidth:
4
+ Width: 1
5
+ Metrics/LineLength:
6
+ Max: 120
7
+ Style/SignalException:
8
+ Enabled: false
data/Gemfile ADDED
@@ -0,0 +1,17 @@
1
+ source 'https://rubygems.org'
2
+
3
+ group :development do
4
+ gem 'rspec'
5
+ gem 'rspec-mocks'
6
+ gem 'webmock'
7
+ gem 'guard'
8
+ gem 'guard-rspec'
9
+ gem 'cucumber', require: false
10
+ gem 'guard-cucumber'
11
+ gem 'rubocop', require: false
12
+ gem 'guard-rubocop'
13
+ gem 'simplecov', require: false
14
+ end
15
+
16
+ # Specify your gem's dependencies in ddp-server.gemspec
17
+ gemspec
data/Guardfile ADDED
@@ -0,0 +1,30 @@
1
+ # A sample Guardfile
2
+ # More info at https://github.com/guard/guard#readme
3
+
4
+ # Note: The cmd option is now required due to the increasing number of ways
5
+ # rspec may be run, below are examples of the most common uses.
6
+ # * bundler: 'bundle exec rspec'
7
+ # * bundler binstubs: 'bin/rspec'
8
+ # * spring: 'bin/rsspec' (This will use spring if running and you have
9
+ # installed the spring binstubs per the docs)
10
+ # * zeus: 'zeus rspec' (requires the server to be started separetly)
11
+ # * 'just' rspec: 'rspec'
12
+ guard :rspec, cmd: 'bundle exec rspec -c' do
13
+ watch(/^spec\/.+_spec\.rb$/) { 'spec' }
14
+ watch(/^lib\/.+\.rb$/) { 'spec' }
15
+ watch('spec/spec_helper.rb') { 'spec' }
16
+ end
17
+
18
+ guard :rubocop do
19
+ watch(/.+\.rb$/)
20
+ watch(/(?:.+\/)?\.rubocop\.yml$/) { |m| File.dirname(m[0]) }
21
+ end
22
+
23
+ guard 'cucumber' do
24
+ watch(/^features\/.+\.feature$/)
25
+ watch(%r{^features\/support\/.+$}) { 'features' }
26
+
27
+ watch(%r{^features/step_definitions/(.+)_steps\.rb$}) do |m|
28
+ Dir[File.join("**/#{m[1]}.feature")][0] || 'features'
29
+ end
30
+ end
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2015 Tinco Andringa
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,29 @@
1
+ # Ddp::Server
2
+
3
+ TODO: Write a gem description
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'ddp-server'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install ddp-server
18
+
19
+ ## Usage
20
+
21
+ TODO: Write usage instructions here
22
+
23
+ ## Contributing
24
+
25
+ 1. Fork it
26
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
27
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
28
+ 4. Push to the branch (`git push origin my-new-feature`)
29
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require 'bundler/gem_tasks'
@@ -0,0 +1,25 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'ddp/server/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = 'ddp-server'
8
+ spec.version = DDP::Server::VERSION
9
+ spec.authors = ['Tinco Andringa']
10
+ spec.email = ['mail@tinco.nl']
11
+ spec.description = 'DDP Protocol server for implementing Ruby DDP backends'
12
+ spec.summary = 'DDP Protocol server for implementing Ruby DDP backends'
13
+ spec.homepage = 'https://github.com/d-snp/ruby-ddp-server'
14
+ spec.license = 'MIT'
15
+
16
+ spec.files = `git ls-files`.split($RS)
17
+ spec.executables = spec.files.grep(/^bin\//) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(/^(test|spec|features)\//)
19
+ spec.require_paths = ['lib']
20
+
21
+ spec.add_development_dependency 'bundler', '~> 1.3'
22
+ spec.add_development_dependency 'rake'
23
+
24
+ spec.add_dependency 'celluloid-websocket'
25
+ end
@@ -0,0 +1,3 @@
1
+ require 'ddp/server/rack'
2
+
3
+ run DDP::Server::Rack
@@ -0,0 +1,66 @@
1
+ module DDP
2
+ module Server
3
+ module Protocol
4
+ # Protocol regarding handling data subscriptions
5
+ module Data
6
+ def handle_data
7
+ case @message['msg']
8
+ when 'sub'
9
+ handle_sub
10
+ true
11
+ when 'unsub'
12
+ handle_unsub
13
+ true
14
+ else
15
+ false
16
+ end
17
+ end
18
+
19
+ def handle_sub
20
+ raise 'Must be overridden'
21
+ end
22
+
23
+ def handle_unsub
24
+ raise 'Must be overridden'
25
+ end
26
+
27
+ def nosub(id, error = nil)
28
+ message = { msg: 'nosub', id: id }
29
+ message.merge!(error: error) if error
30
+ write_message message
31
+ end
32
+
33
+ def added(collection, id, fields = nil)
34
+ message = { msg: 'added', id: id, collection: collection }
35
+ message.merge!(fields: fields) if fields
36
+ write_message message
37
+ end
38
+
39
+ def changed(collection, id, fields = nil, cleared = nil)
40
+ message = { msg: 'changed', id: id, collection: collection }
41
+ message.merge!(fields: fields) if fields
42
+ message.merge!(cleared: cleared) if cleared
43
+ write_message message
44
+ end
45
+
46
+ def removed(collection, id)
47
+ write_message msg: 'removed', collection: collection, id: id
48
+ end
49
+
50
+ def ready(subs)
51
+ write_message msg: 'ready', subs: subs
52
+ end
53
+
54
+ def added_before(collection, id, fields = nil, before = nil)
55
+ message = { msg: 'addedBefore', id: id, collection: collection, before: before }
56
+ message.merge!(fields: fields) if fields
57
+ write_message message
58
+ end
59
+
60
+ def moved_before(collection, id, before = nil)
61
+ write_message msg: 'movedBefore', id: id, collection: collection, before: before
62
+ end
63
+ end
64
+ end
65
+ end
66
+ end
@@ -0,0 +1,20 @@
1
+ module DDP
2
+ module Server
3
+ module Protocol
4
+ # Protocol regarding heartbeat messages
5
+ module Heartbeat
6
+ def handle_heartbeat
7
+ case @message['msg']
8
+ when 'ping'
9
+ write_message msg: 'pong', id: @message['id']
10
+ true
11
+ when 'pong'
12
+ true
13
+ else
14
+ false
15
+ end
16
+ end
17
+ end
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,38 @@
1
+ module DDP
2
+ module Server
3
+ module Protocol
4
+ # Protocol regarding remote procedure calls
5
+ module RPC
6
+ def handle_rpc
7
+ case @message['msg']
8
+ when 'method'
9
+ handle_method
10
+ true
11
+ else
12
+ false
13
+ end
14
+ end
15
+
16
+ def handle_method
17
+ raise 'Must be overridden'
18
+ end
19
+
20
+ def result(id, result = nil)
21
+ message = { msg: 'result', id: id }
22
+ if result
23
+ if result['error']
24
+ message['error'] = result
25
+ else
26
+ message['result'] = result
27
+ end
28
+ end
29
+ write_message(message)
30
+ end
31
+
32
+ def updated(methods)
33
+ write_message msg: 'updated', methods: methods
34
+ end
35
+ end
36
+ end
37
+ end
38
+ end
@@ -0,0 +1,56 @@
1
+ require 'ddp/server/protocol/heartbeat'
2
+ require 'ddp/server/protocol/data'
3
+ require 'ddp/server/protocol/rpc'
4
+
5
+ module DDP
6
+ module Server
7
+ # Implementation of the DDP protocol
8
+ # Can be included into any class that has
9
+ # an on_open, a read_message and a write_message method
10
+ module Protocol
11
+ include Heartbeat
12
+ include Data
13
+ include RPC
14
+
15
+ DDP_VERSION = '1'
16
+
17
+ attr_accessor :session_id
18
+
19
+ def new_session_id
20
+ SecureRandom.hex
21
+ end
22
+
23
+ def handle_connect
24
+ message = read_message
25
+
26
+ if message['msg'] == 'connect' && message['version'] == DDP_VERSION
27
+ handle_session(message)
28
+ else
29
+ write_message('msg' => 'failed', 'version' => DDP_VERSION)
30
+ close
31
+ end
32
+ end
33
+
34
+ def handle_session(message)
35
+ @session_id = message['session'] || new_session_id
36
+
37
+ write_message('msg' => 'connected', 'session' => session_id)
38
+
39
+ handle_established
40
+ end
41
+
42
+ def handle_established
43
+ loop do
44
+ @message = read_message
45
+
46
+ next if handle_heartbeat
47
+ next if handle_data
48
+ next if handle_rpc
49
+ break
50
+ end
51
+
52
+ close
53
+ end
54
+ end
55
+ end
56
+ end
@@ -0,0 +1,26 @@
1
+ require 'ddp/server/version'
2
+ require 'ddp/server/protocol'
3
+ require 'celluloid/websocket/rack'
4
+ require 'json'
5
+ require 'securerandom'
6
+
7
+ module DDP
8
+ module Server
9
+ # Rack middleware for running DDP
10
+ class Rack < Celluloid::WebSocket::Rack
11
+ include DDP::Server::Protocol
12
+
13
+ def on_open
14
+ handle_connect
15
+ end
16
+
17
+ def read_message
18
+ JSON.parse read
19
+ end
20
+
21
+ def write_message(message)
22
+ write JSON.generate(message)
23
+ end
24
+ end
25
+ end
26
+ end
@@ -0,0 +1,6 @@
1
+ module DDP
2
+ # We use semantic versioning
3
+ module Server
4
+ VERSION = '0.0.1'
5
+ end
6
+ end
data/lib/ddp/server.rb ADDED
@@ -0,0 +1,10 @@
1
+ require 'ddp/server/version'
2
+ require 'celluloid/websocket/rack'
3
+ require 'json'
4
+ require 'securerandom'
5
+
6
+ module DDP
7
+ # An implementation of the Meteor DDP protocol
8
+ module Server
9
+ end
10
+ end
@@ -0,0 +1,81 @@
1
+ require 'simplecov'
2
+ SimpleCov.start
3
+
4
+ require 'bundler/setup'
5
+
6
+ def make_sockets
7
+ host = '127.0.0.1'
8
+ port = 10_151
9
+
10
+ server = TCPServer.new(host, port)
11
+ client = TCPSocket.new(host, port)
12
+ peer = server.accept
13
+
14
+ [server, client, peer]
15
+ end
16
+
17
+ # def with_socket_pair
18
+ # server, client, peer = make_sockets
19
+
20
+ # begin
21
+ # yield client, peer
22
+ # ensure
23
+ # server.close rescue nil
24
+ # client.close rescue nil
25
+ # peer.close rescue nil
26
+ # end
27
+ # end
28
+
29
+ def example_host
30
+ 'www.example.com'
31
+ end
32
+
33
+ def example_path
34
+ '/example'
35
+ end
36
+
37
+ def example_url
38
+ "ws://#{example_host}#{example_path}"
39
+ end
40
+
41
+ def handshake_headers
42
+ {
43
+ 'Host' => example_host,
44
+ 'Upgrade' => 'websocket',
45
+ 'Connection' => 'Upgrade',
46
+ 'Sec-WebSocket-Key' => 'dGhlIHNhbXBsZSBub25jZQ==',
47
+ 'Origin' => 'http://example.com',
48
+ 'Sec-WebSocket-Protocol' => 'chat, superchat',
49
+ 'Sec-WebSocket-Version' => '13'
50
+ }
51
+ end
52
+
53
+ def handshake
54
+ WebSocket::ClientHandshake.new(:get, example_url, handshake_headers)
55
+ end
56
+
57
+ def with_websocket_pair
58
+ with_socket_pair do |client, peer|
59
+ connection = Reel::Connection.new(peer)
60
+ client << handshake.to_data
61
+ request = connection.request
62
+
63
+ websocket = request.websocket
64
+
65
+ # Discard handshake
66
+ client.readpartial(4096)
67
+
68
+ yield client, websocket
69
+ end
70
+ end
71
+
72
+ RSpec.configure do |config|
73
+ config.before(:all) do
74
+ # nothing yet
75
+ end
76
+ config.before(:each) do
77
+ # nothing yet
78
+ end
79
+ config.after(:all) {}
80
+ config.after(:each) {}
81
+ end
metadata ADDED
@@ -0,0 +1,104 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: ddp-server
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Tinco Andringa
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-02-15 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.3'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.3'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: celluloid-websocket
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ description: DDP Protocol server for implementing Ruby DDP backends
56
+ email:
57
+ - mail@tinco.nl
58
+ executables: []
59
+ extensions: []
60
+ extra_rdoc_files: []
61
+ files:
62
+ - ".gitignore"
63
+ - ".rubocop.yml"
64
+ - Gemfile
65
+ - Guardfile
66
+ - LICENSE.txt
67
+ - README.md
68
+ - Rakefile
69
+ - ddp-server.gemspec
70
+ - examples/rack_example.ru
71
+ - lib/ddp/server.rb
72
+ - lib/ddp/server/protocol.rb
73
+ - lib/ddp/server/protocol/data.rb
74
+ - lib/ddp/server/protocol/heartbeat.rb
75
+ - lib/ddp/server/protocol/rpc.rb
76
+ - lib/ddp/server/rack.rb
77
+ - lib/ddp/server/version.rb
78
+ - spec/spec_helper.rb
79
+ homepage: https://github.com/d-snp/ruby-ddp-server
80
+ licenses:
81
+ - MIT
82
+ metadata: {}
83
+ post_install_message:
84
+ rdoc_options: []
85
+ require_paths:
86
+ - lib
87
+ required_ruby_version: !ruby/object:Gem::Requirement
88
+ requirements:
89
+ - - ">="
90
+ - !ruby/object:Gem::Version
91
+ version: '0'
92
+ required_rubygems_version: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ requirements: []
98
+ rubyforge_project:
99
+ rubygems_version: 2.2.2
100
+ signing_key:
101
+ specification_version: 4
102
+ summary: DDP Protocol server for implementing Ruby DDP backends
103
+ test_files:
104
+ - spec/spec_helper.rb