faye 1.0.4 → 1.1.0

Sign up to get free protection for your applications and to get access to all the features.

Potentially problematic release.


This version of faye might be problematic. Click here for more details.

@@ -1,7 +1,7 @@
1
1
  module Faye
2
2
 
3
3
  class Transport::Local < Transport
4
- def self.usable?(client, endpoint, &callback)
4
+ def self.usable?(dispatcher, endpoint, &callback)
5
5
  callback.call(Server === endpoint)
6
6
  end
7
7
 
@@ -9,10 +9,9 @@ module Faye
9
9
  false
10
10
  end
11
11
 
12
- def request(envelopes)
13
- messages = Faye.copy_object(envelopes.map { |e| e.message })
14
- @endpoint.process(messages, nil) do |responses|
15
- receive(envelopes, Faye.copy_object(responses))
12
+ def request(messages)
13
+ @endpoint.process(messages, nil) do |replies|
14
+ receive(Faye.copy_object(replies))
16
15
  end
17
16
  end
18
17
  end
@@ -5,13 +5,21 @@ module Faye
5
5
  include Publisher
6
6
  include Timeouts
7
7
 
8
+ DEFAULT_PORTS = {'http' => 80, 'https' => 433, 'ws' => 80, 'wss' => 443}
9
+ SECURE_PROTOCOLS = ['https', 'wss']
10
+
8
11
  attr_reader :endpoint
9
12
 
10
- def initialize(client, endpoint)
13
+ def initialize(dispatcher, endpoint)
11
14
  super()
12
- @client = client
13
- @endpoint = endpoint
14
- @outbox = []
15
+ @dispatcher = dispatcher
16
+ @endpoint = endpoint
17
+ @outbox = []
18
+ @proxy = @dispatcher.proxy.dup
19
+
20
+ @proxy[:origin] ||= SECURE_PROTOCOLS.include?(@endpoint.scheme) ?
21
+ (ENV['HTTPS_PROXY'] || ENV['https_proxy']) :
22
+ (ENV['HTTP_PROXY'] || ENV['http_proxy'])
15
23
  end
16
24
 
17
25
  def batching?
@@ -21,7 +29,7 @@ module Faye
21
29
  def close
22
30
  end
23
31
 
24
- def encode(envelopes)
32
+ def encode(messages)
25
33
  ''
26
34
  end
27
35
 
@@ -29,27 +37,35 @@ module Faye
29
37
  self.class.connection_type
30
38
  end
31
39
 
32
- def send(envelope)
33
- message = envelope.message
34
- client_id = @client.instance_eval { @client_id }
35
- debug('Client ? sending message to ?: ?', client_id, @endpoint, message)
40
+ def send_message(message)
41
+ client_id = @dispatcher.client_id
42
+ debug('Client ? sending message to ? via ?: ?', client_id, @endpoint, connection_type, message)
36
43
 
37
- return request([envelope]) unless batching?
44
+ unless batching?
45
+ promise = EventMachine::DefaultDeferrable.new
46
+ promise.succeed(request([message]))
47
+ return promise
48
+ end
38
49
 
39
- @outbox << envelope
50
+ @outbox << message
51
+ flush_large_batch
52
+ @promise ||= EventMachine::DefaultDeferrable.new
40
53
 
41
54
  if message['channel'] == Channel::HANDSHAKE
42
- return add_timeout(:publish, 0.01) { flush }
55
+ add_timeout(:publish, 0.01) { flush }
56
+ return @promise
43
57
  end
44
58
 
45
59
  if message['channel'] == Channel::CONNECT
46
60
  @connection_message = message
47
61
  end
48
62
 
49
- flush_large_batch
50
63
  add_timeout(:publish, Engine::MAX_DELAY) { flush }
64
+ @promise
51
65
  end
52
66
 
67
+ private
68
+
53
69
  def flush
54
70
  remove_timeout(:publish)
55
71
 
@@ -57,7 +73,8 @@ module Faye
57
73
  @connection_message['advice'] = {'timeout' => 0}
58
74
  end
59
75
 
60
- request(@outbox)
76
+ @promise.succeed(request(@outbox))
77
+ @promise = nil
61
78
 
62
79
  @connection_message = nil
63
80
  @outbox = []
@@ -65,35 +82,36 @@ module Faye
65
82
 
66
83
  def flush_large_batch
67
84
  string = encode(@outbox)
68
- return if string.size < @client.max_request_size
85
+ return if string.size < @dispatcher.max_request_size
69
86
  last = @outbox.pop
70
87
  flush
71
88
  @outbox.push(last) if last
72
89
  end
73
90
 
74
- def receive(envelopes, responses)
75
- envelopes.each { |e| e.set_deferred_status(:succeeded) }
76
- responses = [responses].flatten
77
- client_id = @client.instance_eval { @client_id }
78
- debug('Client ? received from ?: ?', client_id, @endpoint, responses)
79
- responses.each { |response| @client.receive_message(response) }
91
+ def receive(replies)
92
+ replies = [replies].flatten
93
+ client_id = @dispatcher.client_id
94
+ debug('Client ? received from ? via ?: ?', client_id, @endpoint, connection_type, replies)
95
+ replies.each do |reply|
96
+ @dispatcher.handle_response(reply)
97
+ end
80
98
  end
81
99
 
82
- def handle_error(envelopes, immediate = false)
83
- envelopes.each { |e| e.set_deferred_status(:failed, immediate) }
100
+ def handle_error(messages, immediate = false)
101
+ client_id = @dispatcher.client_id
102
+ debug('Client ? failed to send to ? via ?: ?', client_id, @endpoint, connection_type, messages)
103
+ messages.each do |message|
104
+ @dispatcher.handle_error(message, immediate)
105
+ end
84
106
  end
85
107
 
86
- private
87
-
88
108
  def get_cookies
89
- return @client.cookies
90
- @client.cookies.get_cookies(@endpoint.to_s) * ';'
109
+ @dispatcher.cookies.get_cookies(@endpoint.to_s) * ';'
91
110
  end
92
111
 
93
112
  def store_cookies(set_cookie)
94
- return @client.cookies
95
113
  [*set_cookie].compact.each do |cookie|
96
- @client.cookies.set_cookie(@endpoint.to_s, cookie)
114
+ @dispatcher.cookies.set_cookie(@endpoint.to_s, cookie)
97
115
  end
98
116
  end
99
117
 
@@ -102,24 +120,24 @@ module Faye
102
120
  class << self
103
121
  attr_accessor :connection_type
104
122
 
105
- def get(client, allowed, disabled, &callback)
106
- endpoint = client.endpoint
123
+ def get(dispatcher, allowed, disabled, &callback)
124
+ endpoint = dispatcher.endpoint
107
125
 
108
126
  select = lambda do |(conn_type, klass), resume|
109
- conn_endpoint = client.endpoints[conn_type] || endpoint
127
+ conn_endpoint = dispatcher.endpoint_for(conn_type)
110
128
 
111
129
  if disabled.include?(conn_type)
112
130
  next resume.call
113
131
  end
114
132
 
115
133
  unless allowed.include?(conn_type)
116
- klass.usable?(client, conn_endpoint) { |u| }
134
+ klass.usable?(dispatcher, conn_endpoint) { |u| }
117
135
  next resume.call
118
136
  end
119
137
 
120
- klass.usable?(client, conn_endpoint) do |is_usable|
138
+ klass.usable?(dispatcher, conn_endpoint) do |is_usable|
121
139
  next resume.call unless is_usable
122
- transport = klass.respond_to?(:create) ? klass.create(client, conn_endpoint) : klass.new(client, conn_endpoint)
140
+ transport = klass.respond_to?(:create) ? klass.create(dispatcher, conn_endpoint) : klass.new(dispatcher, conn_endpoint)
123
141
  callback.call(transport)
124
142
  end
125
143
  end
@@ -135,6 +153,10 @@ module Faye
135
153
  @transports << [type, klass]
136
154
  klass.connection_type = type
137
155
  end
156
+
157
+ def connection_types
158
+ @transports.map { |t| t[0] }
159
+ end
138
160
  end
139
161
 
140
162
  %w[local web_socket http].each do |type|
@@ -143,4 +165,3 @@ module Faye
143
165
 
144
166
  end
145
167
  end
146
-
@@ -12,13 +12,21 @@ module Faye
12
12
 
13
13
  include Deferrable
14
14
 
15
- def self.usable?(client, endpoint, &callback)
16
- create(client, endpoint).usable?(&callback)
15
+ class Request
16
+ include Deferrable
17
+
18
+ def close
19
+ callback { |socket| socket.close }
20
+ end
17
21
  end
18
22
 
19
- def self.create(client, endpoint)
20
- sockets = client.transports[:websocket] ||= {}
21
- sockets[endpoint.to_s] ||= new(client, endpoint)
23
+ def self.usable?(dispatcher, endpoint, &callback)
24
+ create(dispatcher, endpoint).usable?(&callback)
25
+ end
26
+
27
+ def self.create(dispatcher, endpoint)
28
+ sockets = dispatcher.transports[:websocket] ||= {}
29
+ sockets[endpoint.to_s] ||= new(dispatcher, endpoint)
22
30
  end
23
31
 
24
32
  def batching?
@@ -31,16 +39,20 @@ module Faye
31
39
  connect
32
40
  end
33
41
 
34
- def request(envelopes)
42
+ def request(messages)
35
43
  @pending ||= Set.new
36
- envelopes.each { |envelope| @pending.add(envelope) }
44
+ messages.each { |message| @pending.add(message) }
45
+
46
+ promise = Request.new
37
47
 
38
48
  callback do |socket|
39
49
  next unless socket
40
- messages = envelopes.map { |e| e.message }
41
50
  socket.send(Faye.to_json(messages))
51
+ promise.succeed(socket)
42
52
  end
53
+
43
54
  connect
55
+ promise
44
56
  end
45
57
 
46
58
  def connect
@@ -48,12 +60,16 @@ module Faye
48
60
  return unless @state == UNCONNECTED
49
61
  @state = CONNECTING
50
62
 
51
- headers = @client.headers.dup
52
- headers['Cookie'] = get_cookies
63
+ url = @endpoint.dup
64
+ headers = @dispatcher.headers.dup
65
+ extensions = @dispatcher.ws_extensions
66
+ cookie = get_cookies
53
67
 
54
- url = @endpoint.dup
55
68
  url.scheme = PROTOCOLS[url.scheme]
56
- socket = Faye::WebSocket::Client.new(url.to_s, [], :headers => headers)
69
+ headers['Cookie'] = cookie unless cookie == ''
70
+
71
+ options = {:extensions => extensions, :headers => headers, :proxy => @proxy}
72
+ socket = Faye::WebSocket::Client.new(url.to_s, [], options)
57
73
 
58
74
  socket.onopen = lambda do |*args|
59
75
  store_cookies(socket.headers['Set-Cookie'])
@@ -90,19 +106,16 @@ module Faye
90
106
  end
91
107
 
92
108
  socket.onmessage = lambda do |event|
93
- messages = MultiJson.load(event.data)
94
- envelopes = []
95
-
96
- next if messages.nil?
97
- messages = [messages].flatten
98
-
99
- messages.each do |message|
100
- next unless message.has_key?('successful')
101
- next unless envelope = @pending.find { |e| e.id == message['id'] }
102
- @pending.delete(envelope)
103
- envelopes << envelope
109
+ replies = MultiJson.load(event.data)
110
+ next if replies.nil?
111
+ replies = [replies].flatten
112
+
113
+ replies.each do |reply|
114
+ next unless reply.has_key?('successful')
115
+ next unless message = @pending.find { |m| m['id'] == reply['id'] }
116
+ @pending.delete(message)
104
117
  end
105
- receive(envelopes, messages)
118
+ receive(replies)
106
119
  end
107
120
  end
108
121
 
@@ -116,8 +129,7 @@ module Faye
116
129
  def ping
117
130
  return unless @socket
118
131
  @socket.send('[]')
119
- timeout = @client.instance_eval { @advice['timeout'] }
120
- add_timeout(:ping, timeout/2000.0) { ping }
132
+ add_timeout(:ping, @dispatcher.timeout / 2) { ping }
121
133
  end
122
134
  end
123
135
 
@@ -17,4 +17,3 @@ module Faye
17
17
 
18
18
  end
19
19
  end
20
-
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: faye
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.4
4
+ version: 1.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - James Coglan
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2020-04-28 00:00:00.000000000 Z
11
+ date: 2014-12-22 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: cookiejar
@@ -58,14 +58,14 @@ dependencies:
58
58
  requirements:
59
59
  - - ">="
60
60
  - !ruby/object:Gem::Version
61
- version: 0.7.0
61
+ version: 0.9.1
62
62
  type: :runtime
63
63
  prerelease: false
64
64
  version_requirements: !ruby/object:Gem::Requirement
65
65
  requirements:
66
66
  - - ">="
67
67
  - !ruby/object:Gem::Version
68
- version: 0.7.0
68
+ version: 0.9.1
69
69
  - !ruby/object:Gem::Dependency
70
70
  name: multi_json
71
71
  requirement: !ruby/object:Gem::Requirement
@@ -100,14 +100,14 @@ dependencies:
100
100
  requirements:
101
101
  - - ">="
102
102
  - !ruby/object:Gem::Version
103
- version: 0.3.0
103
+ version: 0.5.1
104
104
  type: :runtime
105
105
  prerelease: false
106
106
  version_requirements: !ruby/object:Gem::Requirement
107
107
  requirements:
108
108
  - - ">="
109
109
  - !ruby/object:Gem::Version
110
- version: 0.3.0
110
+ version: 0.5.1
111
111
  - !ruby/object:Gem::Dependency
112
112
  name: compass
113
113
  requirement: !ruby/object:Gem::Requirement
@@ -136,6 +136,20 @@ dependencies:
136
136
  - - "~>"
137
137
  - !ruby/object:Gem::Version
138
138
  version: 3.1.0
139
+ - !ruby/object:Gem::Dependency
140
+ name: permessage_deflate
141
+ requirement: !ruby/object:Gem::Requirement
142
+ requirements:
143
+ - - ">="
144
+ - !ruby/object:Gem::Version
145
+ version: 0.1.0
146
+ type: :development
147
+ prerelease: false
148
+ version_requirements: !ruby/object:Gem::Requirement
149
+ requirements:
150
+ - - ">="
151
+ - !ruby/object:Gem::Version
152
+ version: 0.1.0
139
153
  - !ruby/object:Gem::Dependency
140
154
  name: puma
141
155
  requirement: !ruby/object:Gem::Requirement
@@ -212,14 +226,14 @@ dependencies:
212
226
  requirements:
213
227
  - - ">="
214
228
  - !ruby/object:Gem::Version
215
- version: '0'
229
+ version: 0.2.0
216
230
  type: :development
217
231
  prerelease: false
218
232
  version_requirements: !ruby/object:Gem::Requirement
219
233
  requirements:
220
234
  - - ">="
221
235
  - !ruby/object:Gem::Version
222
- version: '0'
236
+ version: 0.2.0
223
237
  - !ruby/object:Gem::Dependency
224
238
  name: RedCloth
225
239
  requirement: !ruby/object:Gem::Requirement
@@ -357,10 +371,11 @@ files:
357
371
  - lib/faye/mixins/timeouts.rb
358
372
  - lib/faye/protocol/channel.rb
359
373
  - lib/faye/protocol/client.rb
360
- - lib/faye/protocol/envelope.rb
374
+ - lib/faye/protocol/dispatcher.rb
361
375
  - lib/faye/protocol/extensible.rb
362
376
  - lib/faye/protocol/grammar.rb
363
377
  - lib/faye/protocol/publication.rb
378
+ - lib/faye/protocol/scheduler.rb
364
379
  - lib/faye/protocol/server.rb
365
380
  - lib/faye/protocol/socket.rb
366
381
  - lib/faye/protocol/subscription.rb
@@ -392,7 +407,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
392
407
  - !ruby/object:Gem::Version
393
408
  version: '0'
394
409
  requirements: []
395
- rubygems_version: 3.1.2
410
+ rubyforge_project:
411
+ rubygems_version: 2.2.2
396
412
  signing_key:
397
413
  specification_version: 4
398
414
  summary: Simple pub/sub messaging for the web
@@ -1,24 +0,0 @@
1
- module Faye
2
- class Envelope
3
-
4
- include Deferrable
5
- attr_reader :id, :message
6
-
7
- def initialize(message, timeout = nil)
8
- @id = message['id']
9
- @message = message
10
-
11
- self.timeout(timeout, false) if timeout
12
- end
13
-
14
- def eql?(other)
15
- Envelope === other and @id == other.id
16
- end
17
-
18
- def hash
19
- @id.hash
20
- end
21
-
22
- end
23
- end
24
-