isomorfeus-transport 1.0.0.zeta23 → 2.0.0.rc2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (47) hide show
  1. checksums.yaml +4 -4
  2. data/LICENSE +21 -21
  3. data/README.md +27 -36
  4. data/lib/isomorfeus/transport/client_processor.rb +35 -30
  5. data/lib/isomorfeus/transport/config.rb +182 -158
  6. data/lib/isomorfeus/transport/hamster_session_store.rb +96 -0
  7. data/lib/isomorfeus/transport/handler/authentication_handler.rb +70 -70
  8. data/lib/isomorfeus/transport/imports.rb +9 -0
  9. data/lib/isomorfeus/transport/middlewares.rb +13 -13
  10. data/lib/isomorfeus/transport/rack_middleware.rb +59 -55
  11. data/lib/isomorfeus/transport/request_agent.rb +34 -34
  12. data/lib/isomorfeus/transport/response_agent.rb +23 -23
  13. data/lib/isomorfeus/transport/server_processor.rb +129 -101
  14. data/lib/isomorfeus/transport/server_socket_processor.rb +54 -54
  15. data/lib/isomorfeus/transport/ssr_login.rb +28 -28
  16. data/lib/isomorfeus/transport/version.rb +5 -5
  17. data/lib/isomorfeus/transport/{websocket.rb → websocket_client.rb} +123 -123
  18. data/lib/isomorfeus/transport.rb +200 -213
  19. data/lib/isomorfeus-transport.rb +70 -62
  20. data/lib/lucid_authentication/mixin.rb +122 -124
  21. data/lib/lucid_channel/base.rb +8 -11
  22. data/lib/lucid_channel/mixin.rb +105 -50
  23. data/lib/lucid_handler/base.rb +8 -9
  24. data/lib/lucid_handler/mixin.rb +27 -27
  25. data/node_modules/.package-lock.json +27 -0
  26. data/node_modules/ws/LICENSE +19 -0
  27. data/node_modules/ws/README.md +496 -0
  28. data/node_modules/ws/browser.js +8 -0
  29. data/node_modules/ws/index.js +13 -0
  30. data/node_modules/ws/lib/buffer-util.js +126 -0
  31. data/node_modules/ws/lib/constants.js +12 -0
  32. data/node_modules/ws/lib/event-target.js +266 -0
  33. data/node_modules/ws/lib/extension.js +203 -0
  34. data/node_modules/ws/lib/limiter.js +55 -0
  35. data/node_modules/ws/lib/permessage-deflate.js +511 -0
  36. data/node_modules/ws/lib/receiver.js +612 -0
  37. data/node_modules/ws/lib/sender.js +414 -0
  38. data/node_modules/ws/lib/stream.js +180 -0
  39. data/node_modules/ws/lib/subprotocol.js +62 -0
  40. data/node_modules/ws/lib/validation.js +124 -0
  41. data/node_modules/ws/lib/websocket-server.js +485 -0
  42. data/node_modules/ws/lib/websocket.js +1144 -0
  43. data/node_modules/ws/package.json +61 -0
  44. data/node_modules/ws/wrapper.mjs +8 -0
  45. data/package.json +6 -0
  46. metadata +76 -54
  47. data/lib/isomorfeus/transport/dbm_session_store.rb +0 -51
@@ -1,124 +1,122 @@
1
- module LucidAuthentication
2
- module Mixin
3
- def anonymous?
4
- self.class == Anonymous
5
- end
6
-
7
- if RUBY_ENGINE == 'opal'
8
- def self.included(base)
9
- base.instance_exec do
10
- def execute_login(&block)
11
- end
12
-
13
- def promise_login(user: nil, pass: nil, scheme: :isomorfeus, &block)
14
- send("promise_authentication_with_#{scheme}", user: user, pass: pass, &block)
15
- end
16
-
17
- def promise_authentication_with_isomorfeus(user: nil, pass: nil, &block)
18
- if Isomorfeus.production?
19
- Isomorfeus.raise_error(message: "Connection not secure, can't login") unless Isomorfeus::Transport.socket.url.start_with?('wss:')
20
- else
21
- `console.warn("Connection not secure, ensure a secure connection in production, otherwise login will fail!")` unless Isomorfeus::Transport.socket.url.start_with?('wss:')
22
- end
23
- Isomorfeus::Transport.promise_send_path('Isomorfeus::Transport::Handler::AuthenticationHandler', 'login', self.name, user, pass).then do |agent|
24
- if agent.processed
25
- agent.result
26
- else
27
- agent.processed = true
28
- if agent.response.key?(:success)
29
- Isomorfeus.store.dispatch(type: 'DATA_LOAD', data: agent.response[:data])
30
- class_name = agent.response[:data].keys.first
31
- key = agent.response[:data][class_name].keys.first
32
- logged_in_user = Isomorfeus.cached_data_class(class_name).new(key: key)
33
- cookie_accessor = agent.response[:session_cookie_accessor]
34
- begin
35
- target = if block_given?
36
- block.call(logged_in_user)
37
- else
38
- `window.location.pathname`
39
- end
40
- unless target.class == String && target.start_with?('/')
41
- Isomorfeus.raise_error(message: "A path must be returned as string starting with '/', returned was #{target}!")
42
- end
43
- rescue
44
- target = `window.location.pathname`
45
- end
46
- cookie_query = "#{Isomorfeus.cookie_eater_path}?#{cookie_accessor}=#{target}"
47
- `window.location = cookie_query` # doing page load and redirect
48
- nil
49
- else
50
- error = agent.response[:error]
51
- `console.err(error)` if error
52
- Isomorfeus.raise_error(message: 'Login failed!') # triggers .fail
53
- end
54
- end
55
- end
56
- end
57
- end
58
- end
59
-
60
- def promise_logout(scheme: :isomorfeus)
61
- send("promise_deauthentication_with_#{scheme}")
62
- end
63
-
64
- def promise_deauthentication_with_isomorfeus
65
- Isomorfeus::Transport.promise_send_path('Isomorfeus::Transport::Handler::AuthenticationHandler', 'logout', 'logout').then do |agent|
66
- `document.cookie = "session="`
67
- Isomorfeus.set_current_user(nil)
68
- Isomorfeus.force_init_store!
69
- agent.processed = true
70
- agent.response.key?(:success) ? true : raise('Logout failed!')
71
- end
72
- end
73
- else
74
- def self.included(base)
75
- Isomorfeus.add_valid_user_class(base)
76
-
77
- base.instance_exec do
78
- def execute_login(&block)
79
- @execute_login_block = block
80
- end
81
-
82
- def promise_login(user: nil, pass: nil, scheme: :isomorfeus, &block)
83
- send("promise_authentication_with_#{scheme}", user: user, pass: pass, &block)
84
- end
85
-
86
- def promise_authentication_with_isomorfeus(user: nil, pass: nil, &block)
87
- promise_or_user = @execute_login_block.call(user: user, pass: pass)
88
- if promise_or_user.class == Promise
89
- if block_given?
90
- promise_or_user.then do |user|
91
- block.call(user)
92
- user
93
- end
94
- else
95
- promise_or_user
96
- end
97
- else
98
- block.call(user) if block_given?
99
- Promise.new.resolve(promise_or_user)
100
- end
101
- end
102
- end
103
- end
104
-
105
- def encrypt_password(password, password_confirmation)
106
- raise "Password and confirmation don't match!" unless password == password_confirmation
107
- BCrypt::Password.create(password).to_s
108
- end
109
-
110
- def passwords_match?(encrypted_password, given_password)
111
- bcrypt_pass = BCrypt::Password.new(encrypted_password)
112
- bcrypt_pass == given_password
113
- end
114
-
115
- def promise_logout(scheme: :isomorfeus)
116
- send("promise_deauthentication_with_#{scheme}")
117
- end
118
-
119
- def promise_deauthentication_with_isomorfeus
120
- Promise.new.resolve(true)
121
- end
122
- end
123
- end
124
- end
1
+ module LucidAuthentication
2
+ module Mixin
3
+ def anonymous?
4
+ self.class == Anonymous
5
+ end
6
+
7
+ if RUBY_ENGINE == 'opal'
8
+ def self.included(base)
9
+ base.instance_exec do
10
+ def execute_login(&block)
11
+ end
12
+
13
+ def promise_login(user: nil, pass: nil, scheme: :isomorfeus, &block)
14
+ send("promise_authentication_with_#{scheme}", user: user, pass: pass, &block)
15
+ end
16
+
17
+ def promise_authentication_with_isomorfeus(user: nil, pass: nil, &block)
18
+ if Isomorfeus.production?
19
+ Isomorfeus.raise_error(message: "Connection not secure, can't login!") unless Isomorfeus::Transport.socket.url.start_with?('wss:')
20
+ else
21
+ `console.warn("Connection not secure, ensure a secure connection in production, otherwise login will fail!")` unless Isomorfeus::Transport.socket.url.start_with?('wss:')
22
+ end
23
+ Isomorfeus::Transport.promise_send_path('Isomorfeus::Transport::Handler::AuthenticationHandler', 'login', self.name, user, pass).then do |agent|
24
+ if agent.processed
25
+ agent.result
26
+ else
27
+ agent.processed = true
28
+ if agent.response.key?(:success)
29
+ Isomorfeus.store.dispatch(type: 'DATA_LOAD', data: agent.response[:data])
30
+ class_name = agent.response[:data].keys.first
31
+ key = agent.response[:data][class_name].keys.first
32
+ logged_in_user = Isomorfeus.cached_data_class(class_name).new(key: key)
33
+ cookie_accessor = agent.response[:session_cookie_accessor]
34
+ begin
35
+ target = if block_given?
36
+ block.call(logged_in_user)
37
+ else
38
+ `window.location.pathname`
39
+ end
40
+ unless target.class == String && target.start_with?('/')
41
+ Isomorfeus.raise_error(message: "A path must be returned as string starting with '/', returned was #{target}!")
42
+ end
43
+ rescue
44
+ target = `window.location.pathname`
45
+ end
46
+ cookie_query = "#{Isomorfeus.cookie_eater_path}?#{cookie_accessor}=#{target}"
47
+ `window.location = cookie_query` # doing page load and redirect
48
+ nil
49
+ else
50
+ Isomorfeus.raise_error(message: "Login failed with '#{agent.response[:error]}'!") # triggers .fail
51
+ end
52
+ end
53
+ end
54
+ end
55
+ end
56
+ end
57
+
58
+ def promise_logout(scheme: :isomorfeus)
59
+ send("promise_deauthentication_with_#{scheme}")
60
+ end
61
+
62
+ def promise_deauthentication_with_isomorfeus
63
+ Isomorfeus::Transport.promise_send_path('Isomorfeus::Transport::Handler::AuthenticationHandler', 'logout', 'logout').then do |agent|
64
+ `document.cookie = "session="`
65
+ Isomorfeus.set_current_user(nil)
66
+ Isomorfeus.force_init_store!
67
+ agent.processed = true
68
+ agent.response.key?(:success) ? true : raise('Logout failed!')
69
+ end
70
+ end
71
+ else
72
+ def self.included(base)
73
+ Isomorfeus.add_valid_user_class(base)
74
+
75
+ base.instance_exec do
76
+ def execute_login(&block)
77
+ @execute_login_block = block
78
+ end
79
+
80
+ def promise_login(user: nil, pass: nil, scheme: :isomorfeus, &block)
81
+ send("promise_authentication_with_#{scheme}", user: user, pass: pass, &block)
82
+ end
83
+
84
+ def promise_authentication_with_isomorfeus(user: nil, pass: nil, &block)
85
+ promise_or_user = @execute_login_block.call(user: user, pass: pass)
86
+ if promise_or_user.class == Promise
87
+ if block_given?
88
+ promise_or_user.then do |user|
89
+ block.call(user)
90
+ user
91
+ end
92
+ else
93
+ promise_or_user
94
+ end
95
+ else
96
+ block.call(user) if block_given?
97
+ Promise.new.resolve(promise_or_user)
98
+ end
99
+ end
100
+ end
101
+ end
102
+
103
+ def encrypt_password(password, password_confirmation)
104
+ raise "Password and confirmation don't match!" unless password == password_confirmation
105
+ BCrypt::Password.create(password).to_s
106
+ end
107
+
108
+ def passwords_match?(encrypted_password, given_password)
109
+ bcrypt_pass = BCrypt::Password.new(encrypted_password)
110
+ bcrypt_pass == given_password
111
+ end
112
+
113
+ def promise_logout(scheme: :isomorfeus)
114
+ send("promise_deauthentication_with_#{scheme}")
115
+ end
116
+
117
+ def promise_deauthentication_with_isomorfeus
118
+ Promise.new.resolve(true)
119
+ end
120
+ end
121
+ end
122
+ end
@@ -1,11 +1,8 @@
1
- module LucidChannel
2
- class Base
3
- if RUBY_ENGINE != 'opal'
4
- def self.inherited(base)
5
- Isomorfeus.add_valid_channel_class(base)
6
- end
7
- end
8
-
9
- include LucidChannel::Mixin
10
- end
11
- end
1
+ module LucidChannel
2
+ class Base
3
+ def self.inherited(base)
4
+ base.include LucidChannel::Mixin
5
+ Isomorfeus.add_valid_channel_class(base)
6
+ end
7
+ end
8
+ end
@@ -1,50 +1,105 @@
1
- module LucidChannel
2
- module Mixin
3
- def self.included(base)
4
-
5
- if RUBY_ENGINE != 'opal'
6
- Isomorfeus.add_valid_channel_class(base) unless base == LucidChannel::Base
7
- end
8
-
9
- base.instance_exec do
10
- def process_message(channel, message = nil)
11
- if @message_processor
12
- if channel == self.name
13
- @message_processor.call(message)
14
- else
15
- @message_processor.call(channel, message)
16
- end
17
- else
18
- puts "#{self} received: #{channel} #{message}, but no 'on_message' block defined!"
19
- end
20
- end
21
-
22
- def on_message(&block)
23
- @message_processor = block
24
- end
25
-
26
- def send_message(channel, message = nil)
27
- unless message
28
- message = channel
29
- channel = self.name
30
- end
31
- Isomorfeus::Transport.send_notification(self, channel, message)
32
- end
33
-
34
- def subscribe(channel = nil)
35
- channel = channel ? channel : self.name
36
- Isomorfeus::Transport.subscribe(self, channel)
37
- end
38
-
39
- def unsubscribe(channel = nil)
40
- channel = channel ? channel : self.name
41
- Isomorfeus::Transport.unsubscribe(self, channel)
42
- end
43
-
44
- def current_user
45
- Isomorfeus.current_user
46
- end
47
- end
48
- end
49
- end
50
- end
1
+ module LucidChannel
2
+ module Mixin
3
+ def self.included(base)
4
+ Isomorfeus.add_valid_channel_class(base) unless base == LucidChannel::Base
5
+
6
+ base.instance_exec do
7
+ def subscription_channels
8
+ @subscription_channels ||= {}
9
+ end
10
+
11
+ def channel(name, options = {})
12
+ subscription_channels[name.to_s] = options
13
+ end
14
+
15
+ def valid_channel?(name)
16
+ name = name.to_s
17
+ subscription_channels.key?(name) || name == self.name
18
+ end
19
+
20
+ def process_message(message, error, channel = nil)
21
+ channel = self.name unless channel
22
+ channel = channel.to_s
23
+ unless valid_channel?(channel)
24
+ Isomorfeus.raise_error(message: "No such channel '#{channel}' declared for #{self.name}! Cannot process message")
25
+ end
26
+ block = subscription_channels[channel][:block]
27
+ Isomorfeus.raise_error(message: "#{self} received: #{channel} #{message}, but no 'on_message' block defined!") unless block
28
+ block.call(message, error)
29
+ nil
30
+ end
31
+
32
+ def on_message(channel = nil, &block)
33
+ channel = self.name unless channel
34
+ channel = channel.to_s
35
+ unless valid_channel?(channel)
36
+ Isomorfeus.raise_error(message: "No such channel #{channel} declared, please declare it first!")
37
+ end
38
+ subscription_channels[channel] = {} unless subscription_channels.key?(channel)
39
+ subscription_channels[channel][:block] = block
40
+ end
41
+
42
+ def send_message(message, channel = nil)
43
+ channel = self.name unless channel
44
+ unless valid_channel?(channel)
45
+ Isomorfeus.raise_error(message: "No such channel '#{channel}' declared for #{self.name}! Cannot send message")
46
+ end
47
+ Isomorfeus::Transport.send_message(self, channel, message)
48
+ end
49
+
50
+ def subscribe(channel = nil)
51
+ promise_subscribe(channel)
52
+ nil
53
+ end
54
+
55
+ def promise_subscribe(channel = nil)
56
+ channel = channel ? channel : self.name
57
+ Isomorfeus::Transport.promise_subscribe(self.name, channel)
58
+ end
59
+
60
+ def unsubscribe(channel = nil)
61
+ promise_unsubscribe(channel)
62
+ nil
63
+ end
64
+
65
+ def promise_unsubscribe(channel = nil)
66
+ channel = channel ? channel : self.name
67
+ Isomorfeus::Transport.promise_unsubscribe(self.name, channel)
68
+ end
69
+
70
+ if RUBY_ENGINE == 'opal'
71
+ def server_subscription_channels; end
72
+ def server_process_message(message, channel = nil); end
73
+ def server_on_message(channel = nil, &block); end
74
+ else
75
+ def server_is_processing_messages?(channel)
76
+ return false if server_subscription_channels.empty?
77
+ return true if server_subscription_channels.key?(channel) && server_subscription_channels[channel].key?(:block)
78
+ false
79
+ end
80
+
81
+ def server_subscription_channels
82
+ @server_subscription_channels ||= {}
83
+ end
84
+
85
+ def server_process_message(message, channel = nil)
86
+ channel = self.name unless channel
87
+ channel = channel.to_s
88
+ block = server_subscription_channels[channel][:block]
89
+ block.call(message)
90
+ end
91
+
92
+ def server_on_message(channel = nil, &block)
93
+ channel = self.name unless channel
94
+ channel = channel.to_s
95
+ unless valid_channel?(channel)
96
+ Isomorfeus.raise_error(message: "No such channel #{channel} declared, please declare it first!")
97
+ end
98
+ server_subscription_channels[channel] = {} unless server_subscription_channels.key?(channel)
99
+ server_subscription_channels[channel][:block] = block
100
+ end
101
+ end
102
+ end
103
+ end
104
+ end
105
+ end
@@ -1,9 +1,8 @@
1
- module LucidHandler
2
- class Base
3
- def self.inherited(base)
4
- Isomorfeus.add_valid_handler_class(base)
5
- end
6
-
7
- include LucidHandler::Mixin
8
- end
9
- end
1
+ module LucidHandler
2
+ class Base
3
+ def self.inherited(base)
4
+ base.include LucidHandler::Mixin
5
+ Isomorfeus.add_valid_handler_class(base)
6
+ end
7
+ end
8
+ end
@@ -1,27 +1,27 @@
1
- module LucidHandler
2
- module Mixin
3
- def self.included(base)
4
- Isomorfeus.add_valid_handler_class(base) unless base == LucidHandler::Base
5
-
6
- base.instance_exec do
7
- def on_request(&block)
8
- define_method :process_request do |*args|
9
- instance_exec(*args, &block)
10
- end
11
- end
12
- end
13
- end
14
-
15
- def resolving?
16
- false
17
- end
18
-
19
- def current_user
20
- Isomorfeus.current_user
21
- end
22
-
23
- def pub_sub_client
24
- Isomorfeus.pub_sub_client
25
- end
26
- end
27
- end
1
+ module LucidHandler
2
+ module Mixin
3
+ def self.included(base)
4
+ Isomorfeus.add_valid_handler_class(base) unless base == LucidHandler::Base
5
+
6
+ base.instance_exec do
7
+ def on_request(&block)
8
+ define_method :process_request do |*args|
9
+ instance_exec(*args, &block)
10
+ end
11
+ end
12
+ end
13
+ end
14
+
15
+ def resolving?
16
+ false
17
+ end
18
+
19
+ def current_user
20
+ Isomorfeus.current_user
21
+ end
22
+
23
+ def pub_sub_client
24
+ Isomorfeus.pub_sub_client
25
+ end
26
+ end
27
+ end
@@ -0,0 +1,27 @@
1
+ {
2
+ "name": "isomorfeus-transport",
3
+ "lockfileVersion": 2,
4
+ "requires": true,
5
+ "packages": {
6
+ "node_modules/ws": {
7
+ "version": "8.2.1",
8
+ "resolved": "https://registry.npmjs.org/ws/-/ws-8.2.1.tgz",
9
+ "integrity": "sha512-XkgWpJU3sHU7gX8f13NqTn6KQ85bd1WU7noBHTT8fSohx7OS1TPY8k+cyRPCzFkia7C4mM229yeHr1qK9sM4JQ==",
10
+ "engines": {
11
+ "node": ">=10.0.0"
12
+ },
13
+ "peerDependencies": {
14
+ "bufferutil": "^4.0.1",
15
+ "utf-8-validate": "^5.0.2"
16
+ },
17
+ "peerDependenciesMeta": {
18
+ "bufferutil": {
19
+ "optional": true
20
+ },
21
+ "utf-8-validate": {
22
+ "optional": true
23
+ }
24
+ }
25
+ }
26
+ }
27
+ }
@@ -0,0 +1,19 @@
1
+ Copyright (c) 2011 Einar Otto Stangvik <einaros@gmail.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 all
11
+ 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 THE
19
+ SOFTWARE.