opal-drb 0.2.1 → 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
- SHA1:
3
- metadata.gz: b15bcbd00eb42ad9a6e0bbd97eff77a0cf839d67
4
- data.tar.gz: 1e1c07dddf6707a8f1aee7d102b90a091918a027
2
+ SHA256:
3
+ metadata.gz: e4bf2949ad3c3cad7b882abb2b3f59b7d867ab2a4653b774742e47b292a21c60
4
+ data.tar.gz: 658007bd8f1d9766569b48b7b62e5bdb2b5851870dc3f1acb56d2c4145975610
5
5
  SHA512:
6
- metadata.gz: fbae91fd2584a7d00a86b888ef1066e66cd170108d5ef10cdeb3ff87cb37f6bf40a2a38927e57bfb023cfe34a7162cc64cdf395b5ddc058c88f7cb7dca0b1eac
7
- data.tar.gz: faffca7fce4a793ea4a1109cf1dadb582d5332345113f5612917c12d14e0653fd4afbd4a754258fdefb38b51a15d2d05545862dfcab775ff361d3912a216252a
6
+ metadata.gz: 57b4e20deac64183bacc23d3f5bc304d881306ff75af2b44215dff42cf66c35c034e2921734673d4991becf2ef37d17b4861c3639a2d72f93e51da27448f1cde
7
+ data.tar.gz: 2293ad912ac41784870eb35d8823c10f06d84b3d940a908605b481a9196d18ed21f672c48ef98d486291551116fc920db3dbddb12d16e220c8090a7042e6adee
data/example/Gemfile CHANGED
@@ -4,7 +4,7 @@ gem 'sinatra'
4
4
  gem 'sinatra-contrib'
5
5
  gem 'thin'
6
6
 
7
- gem 'opal', github: 'youchan/opal', branch: 'marshal-dump-bignum'
7
+ gem 'opal'
8
8
  gem 'opal-sprockets', github: 'opal/opal-sprockets'
9
9
  gem 'drb-websocket'
10
10
 
data/example/Gemfile.lock CHANGED
@@ -1,87 +1,93 @@
1
1
  GIT
2
- remote: https://github.com/opal/opal-sprockets.git
3
- revision: a063dffd884b495ed0c54b4c3b0b1cacc4abb322
2
+ remote: git://github.com/opal/opal-sprockets.git
3
+ revision: 8a8faeb1cfbc51cfa2e71ddb6cf219bd4a14f333
4
4
  specs:
5
- opal-sprockets (0.4.1.0.11.0.rc1.3.1.beta2)
6
- opal (~> 0.11.0.rc1)
5
+ opal-sprockets (0.4.2.0.11.0.3.1)
6
+ opal (~> 0.11.0)
7
7
  sprockets (~> 3.1)
8
8
  tilt (>= 1.4)
9
9
 
10
- GIT
11
- remote: https://github.com/youchan/opal.git
12
- revision: 1a5a59b66a0469c73c18e90c66591a7a66421de5
13
- branch: marshal-dump-bignum
14
- specs:
15
- opal (0.11.0.rc1)
16
- ast (>= 2.3.0)
17
- hike (~> 1.2)
18
- parser (= 2.3.3.1)
19
- sourcemap (~> 0.1.0)
20
-
21
10
  GEM
22
11
  remote: https://rubygems.org/
23
12
  specs:
24
- ast (2.3.0)
25
- backports (3.8.0)
26
- concurrent-ruby (1.0.5)
27
- daemons (1.2.4)
28
- drb-websocket (0.2.0)
13
+ activesupport (5.2.1)
14
+ concurrent-ruby (~> 1.0, >= 1.0.2)
15
+ i18n (>= 0.7, < 2)
16
+ minitest (~> 5.1)
17
+ tzinfo (~> 1.1)
18
+ ast (2.4.0)
19
+ backports (3.11.4)
20
+ concurrent-ruby (1.1.3)
21
+ daemons (1.2.6)
22
+ drb-websocket (0.4.0)
29
23
  faye-websocket
30
24
  rack
31
25
  thin
32
- eventmachine (1.2.5)
26
+ eventmachine (1.2.7)
33
27
  faye-websocket (0.10.7)
34
28
  eventmachine (>= 0.12.0)
35
29
  websocket-driver (>= 0.5.1)
36
- ffi (1.9.18)
30
+ ffi (1.9.25)
37
31
  hike (1.2.3)
38
- multi_json (1.12.1)
39
- mustermann (1.0.0)
32
+ i18n (1.1.1)
33
+ concurrent-ruby (~> 1.0)
34
+ minitest (5.11.3)
35
+ multi_json (1.13.1)
36
+ mustermann (1.0.3)
37
+ opal (0.11.4)
38
+ ast (>= 2.3.0)
39
+ hike (~> 1.2)
40
+ parser (= 2.3.3.1)
41
+ sourcemap (~> 0.1.0)
40
42
  parser (2.3.3.1)
41
43
  ast (~> 2.2)
42
- rack (2.0.3)
43
- rack-protection (2.0.0)
44
+ rack (2.0.6)
45
+ rack-protection (2.0.4)
44
46
  rack
45
- rake (12.0.0)
46
- rb-fsevent (0.10.2)
47
+ rake (12.3.1)
48
+ rb-fsevent (0.10.3)
47
49
  rb-inotify (0.9.10)
48
50
  ffi (>= 0.5.0, < 2)
49
- sass (3.5.1)
51
+ sass (3.7.2)
50
52
  sass-listen (~> 4.0.0)
51
53
  sass-listen (4.0.0)
52
54
  rb-fsevent (~> 0.9, >= 0.9.4)
53
55
  rb-inotify (~> 0.9, >= 0.9.7)
54
- sinatra (2.0.0)
56
+ sinatra (2.0.4)
55
57
  mustermann (~> 1.0)
56
58
  rack (~> 2.0)
57
- rack-protection (= 2.0.0)
59
+ rack-protection (= 2.0.4)
58
60
  tilt (~> 2.0)
59
- sinatra-contrib (2.0.0)
60
- backports (>= 2.0)
61
+ sinatra-contrib (2.0.4)
62
+ activesupport (>= 4.0.0)
63
+ backports (>= 2.8.2)
61
64
  multi_json
62
65
  mustermann (~> 1.0)
63
- rack-protection (= 2.0.0)
64
- sinatra (= 2.0.0)
66
+ rack-protection (= 2.0.4)
67
+ sinatra (= 2.0.4)
65
68
  tilt (>= 1.3, < 3)
66
69
  sourcemap (0.1.1)
67
- sprockets (3.7.1)
70
+ sprockets (3.7.2)
68
71
  concurrent-ruby (~> 1.0)
69
72
  rack (> 1, < 3)
70
73
  thin (1.7.2)
71
74
  daemons (~> 1.0, >= 1.0.9)
72
75
  eventmachine (~> 1.0, >= 1.0.4)
73
76
  rack (>= 1, < 3)
77
+ thread_safe (0.3.6)
74
78
  tilt (2.0.8)
75
- websocket-driver (0.6.5)
79
+ tzinfo (1.2.5)
80
+ thread_safe (~> 0.1)
81
+ websocket-driver (0.7.0)
76
82
  websocket-extensions (>= 0.1.0)
77
- websocket-extensions (0.1.2)
83
+ websocket-extensions (0.1.3)
78
84
 
79
85
  PLATFORMS
80
86
  ruby
81
87
 
82
88
  DEPENDENCIES
83
89
  drb-websocket
84
- opal!
90
+ opal
85
91
  opal-sprockets!
86
92
  rake
87
93
  sass
@@ -90,4 +96,4 @@ DEPENDENCIES
90
96
  thin
91
97
 
92
98
  BUNDLED WITH
93
- 1.15.3
99
+ 1.17.1
data/example/config.ru CHANGED
@@ -16,10 +16,6 @@ app = Rack::Builder.app do
16
16
  map '/assets' do
17
17
  run Server::OPAL.sprockets
18
18
  end
19
-
20
- map '/__OPAL_SOURCE_MAPS__' do
21
- run Opal::SourceMapServer.new(Server::OPAL.sprockets, '/__OPAL_SOURCE_MAPS__')
22
- end
23
19
  end
24
20
 
25
21
  require_relative './drb_server'
@@ -1,5 +1,5 @@
1
1
  module Opal
2
2
  module DRb
3
- VERSION = "0.2.1"
3
+ VERSION = "0.3.0"
4
4
  end
5
5
  end
@@ -27,23 +27,51 @@ class WebSocket
27
27
  def initialize(url)
28
28
  super `new WebSocket(url)`
29
29
  `self.native.binaryType = 'arraybuffer'`
30
+ @listeners = {}
30
31
  end
31
32
 
32
- def onmessage
33
- add_event_listener('message') {|event| yield MessageEvent.new(event) if self.open? }
33
+ def onmessage(&block)
34
+ listener = Proc.new {|event| yield MessageEvent.new(event) if self.open? }
35
+ @listeners[block] = [:message, listener]
36
+ add_event_listener('message', &listener)
34
37
  end
35
38
 
36
- def onopen
37
- add_event_listener('open') {|event| yield MessageEvent.new(event) if self.open? }
39
+ def onopen(&block)
40
+ listener = Proc.new {|event| yield Native(event) }
41
+ @listeners[block] = [:open, listener]
42
+ add_event_listener('open', &listener)
43
+ end
44
+
45
+ def onclose(&block)
46
+ listener = Proc.new {|event| yield Native(event) }
47
+ @listeners[block] = [:close, listener]
48
+ add_event_listener('close', &listener)
49
+ end
50
+
51
+ def off handler
52
+ remove_event_listener(*@listeners[handler])
53
+ end
54
+
55
+ def connecting?
56
+ `#@native.readyState === 0`
38
57
  end
39
58
 
40
59
  def open?
41
- `self.native.readyState == 1`
60
+ `#@native.readyState === 1`
61
+ end
62
+
63
+ def closing?
64
+ `#@native.readyState === 2`
65
+ end
66
+
67
+ def closed?
68
+ `#@native.readyState === 3`
42
69
  end
43
70
 
44
71
  alias_native :close
45
72
  alias_native :send
46
73
  alias_native :add_event_listener, :addEventListener
74
+ alias_native :remove_event_listener, :removeEventListener
47
75
 
48
76
  class MessageEvent
49
77
  include Native
@@ -56,6 +84,18 @@ end
56
84
 
57
85
  module DRb
58
86
  module WebSocket
87
+ class SocketPool
88
+ def initialize
89
+ @sockets = {}
90
+ @proxy = ENV['HTTP_PROXY']
91
+ end
92
+
93
+ def open(uri)
94
+ @sockets[uri] ||= ::WebSocket.new(uri)
95
+ end
96
+
97
+ alias_method :[], :open
98
+ end
59
99
  class StrStream
60
100
  def initialize(str='')
61
101
  @buf = str
@@ -80,11 +120,12 @@ module DRb
80
120
  end
81
121
 
82
122
  def self.open(uri, config)
123
+ @pool ||= SocketPool.new
83
124
  unless uri =~ /^ws:\/\/(.*?):(\d+)(\/(.*))?$/
84
125
  raise(DRbBadScheme, uri) unless uri =~ /^ws:/
85
126
  raise(DRbBadURI, 'can\'t parse uri:' + uri)
86
127
  end
87
- ClientSide.new(uri, config)
128
+ ClientSide.new(uri, @pool[uri], config)
88
129
  end
89
130
 
90
131
  def self.open_server(uri, config)
@@ -111,10 +152,16 @@ module DRb
111
152
  def accept
112
153
  ws = ::WebSocket.new(@uri)
113
154
  ws.onmessage do |event|
114
- stream = StrStream.new(event.data.to_s)
155
+ message_data = event.data.to_s
156
+ sender_id = message_data.slice(0, 36)
157
+ message = message_data.slice(36, message_data.length - 36)
158
+ stream = StrStream.new(message)
115
159
  server_side = ServerSide.new(stream, @config, uri)
116
160
  yield server_side
117
- ws.send(`new Uint8Array(#{server_side.reply.bytes.each_slice(2).map(&:first)}).buffer`)
161
+
162
+ send_data = sender_id.bytes.each_slice(2).map(&:first)
163
+ send_data += server_side.reply.bytes.each_slice(2).map(&:first)
164
+ ws.send(`new Uint8Array(#{send_data}).buffer`)
118
165
  end
119
166
  end
120
167
  end
@@ -127,6 +174,7 @@ module DRb
127
174
  @config = config
128
175
  @msg = DRbMessage.new(@config)
129
176
  @req_stream = stream
177
+ @sender_id = SecureRandom.uuid
130
178
  end
131
179
 
132
180
  def close
@@ -156,19 +204,21 @@ module DRb
156
204
  end
157
205
 
158
206
  class ClientSide
159
- def initialize(uri, config)
207
+ def initialize(uri, ws, config)
160
208
  @uri = uri
209
+ @ws = ws
161
210
  @res = nil
162
211
  @config = config
163
212
  @msg = DRbMessage.new(@config)
164
- @proxy = ENV['HTTP_PROXY']
213
+ @sender_id = SecureRandom.uuid
165
214
  end
166
215
 
167
- def close
216
+ def alive?
217
+ !!@ws && @ws.open?
168
218
  end
169
219
 
170
- def alive?
171
- false
220
+ def close
221
+ @ws = nil
172
222
  end
173
223
 
174
224
  def send_request(ref, msg_id, *arg, &b)
@@ -178,28 +228,40 @@ module DRb
178
228
  end
179
229
 
180
230
  def recv_reply(reply_stream)
181
- @ws.close
182
231
  @msg.recv_reply(reply_stream)
183
232
  end
184
233
 
185
234
  def send(uri, data)
186
235
  promise = Promise.new
187
- @ws = ::WebSocket.new(uri)
188
- @ws.onmessage do |event|
236
+ event_handler = Proc.new do |event|
237
+ message_data = event.data.to_s
238
+ sender_id = message_data.slice(0, 36)
239
+ message = message_data.slice(36, message_data.length - 36)
240
+
241
+ unless sender_id == @sender_id
242
+ next
243
+ end
244
+
189
245
  reply_stream = StrStream.new
190
- reply_stream.write(event.data.to_s)
246
+ reply_stream.write(message.to_s)
191
247
 
192
248
  if @config[:load_limit] < reply_stream.buf.size
193
249
  raise TypeError, 'too large packet'
194
250
  end
195
251
 
196
252
  promise.resolve reply_stream
197
-
198
- @ws.close
253
+ @ws.off(event_handler)
199
254
  end
255
+ @ws.onmessage(&event_handler)
256
+ byte_data = @sender_id.bytes.each_slice(2).map(&:first)
257
+ byte_data += data.bytes.each_slice(2).map(&:first)
200
258
 
201
- @ws.onopen do
202
- @ws.send(`new Uint8Array(#{data.bytes.each_slice(2).map(&:first)}).buffer`)
259
+ if @ws.connecting?
260
+ @ws.onopen do
261
+ @ws.send(`new Uint8Array(#{byte_data}).buffer`)
262
+ end
263
+ else
264
+ @ws.send(`new Uint8Array(#{byte_data}).buffer`)
203
265
  end
204
266
 
205
267
  promise
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: opal-drb
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.1
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - youchan
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2017-09-17 00:00:00.000000000 Z
11
+ date: 2018-11-11 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: opal
@@ -120,7 +120,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
120
120
  version: '0'
121
121
  requirements: []
122
122
  rubyforge_project:
123
- rubygems_version: 2.6.11
123
+ rubygems_version: 2.7.6
124
124
  signing_key:
125
125
  specification_version: 4
126
126
  summary: A dRuby implementation for Opal