faye 0.8.8 → 0.8.9

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.

Files changed (67) hide show
  1. data/History.txt +16 -10
  2. data/README.rdoc +1 -1
  3. data/lib/faye-browser-min.js +1 -1
  4. data/lib/faye-browser-min.js.map +2 -2
  5. data/lib/faye-browser.js +302 -287
  6. data/lib/faye.rb +21 -21
  7. data/lib/faye/adapters/rack_adapter.rb +50 -48
  8. data/lib/faye/adapters/static_server.rb +22 -22
  9. data/lib/faye/engines/connection.rb +13 -13
  10. data/lib/faye/engines/memory.rb +21 -21
  11. data/lib/faye/engines/proxy.rb +23 -23
  12. data/lib/faye/error.rb +6 -6
  13. data/lib/faye/mixins/logging.rb +12 -12
  14. data/lib/faye/mixins/publisher.rb +6 -6
  15. data/lib/faye/mixins/timeouts.rb +1 -1
  16. data/lib/faye/protocol/channel.rb +24 -24
  17. data/lib/faye/protocol/client.rb +71 -73
  18. data/lib/faye/protocol/extensible.rb +7 -7
  19. data/lib/faye/protocol/grammar.rb +13 -13
  20. data/lib/faye/protocol/server.rb +57 -57
  21. data/lib/faye/protocol/socket.rb +4 -4
  22. data/lib/faye/protocol/subscription.rb +4 -4
  23. data/lib/faye/transport/http.rb +13 -13
  24. data/lib/faye/transport/local.rb +5 -5
  25. data/lib/faye/transport/transport.rb +25 -25
  26. data/lib/faye/transport/web_socket.rb +34 -30
  27. data/lib/faye/util/namespace.rb +4 -4
  28. data/spec/browser.html +5 -5
  29. data/spec/javascript/channel_spec.js +3 -3
  30. data/spec/javascript/client_spec.js +104 -98
  31. data/spec/javascript/engine/memory_spec.js +1 -1
  32. data/spec/javascript/engine_spec.js +70 -70
  33. data/spec/javascript/faye_spec.js +6 -6
  34. data/spec/javascript/grammar_spec.js +12 -12
  35. data/spec/javascript/node_adapter_spec.js +46 -46
  36. data/spec/javascript/publisher_spec.js +4 -4
  37. data/spec/javascript/server/connect_spec.js +21 -21
  38. data/spec/javascript/server/disconnect_spec.js +15 -15
  39. data/spec/javascript/server/extensions_spec.js +6 -6
  40. data/spec/javascript/server/handshake_spec.js +18 -18
  41. data/spec/javascript/server/integration_spec.js +23 -23
  42. data/spec/javascript/server/publish_spec.js +9 -9
  43. data/spec/javascript/server/subscribe_spec.js +30 -30
  44. data/spec/javascript/server/unsubscribe_spec.js +30 -30
  45. data/spec/javascript/server_spec.js +15 -15
  46. data/spec/javascript/transport_spec.js +32 -27
  47. data/spec/node.js +2 -2
  48. data/spec/ruby/channel_spec.rb +2 -2
  49. data/spec/ruby/client_spec.rb +100 -92
  50. data/spec/ruby/engine_examples.rb +51 -51
  51. data/spec/ruby/faye_spec.rb +5 -5
  52. data/spec/ruby/grammar_spec.rb +12 -12
  53. data/spec/ruby/publisher_spec.rb +4 -4
  54. data/spec/ruby/rack_adapter_spec.rb +34 -34
  55. data/spec/ruby/server/connect_spec.rb +22 -22
  56. data/spec/ruby/server/disconnect_spec.rb +16 -16
  57. data/spec/ruby/server/extensions_spec.rb +8 -8
  58. data/spec/ruby/server/handshake_spec.rb +20 -20
  59. data/spec/ruby/server/integration_spec.rb +22 -24
  60. data/spec/ruby/server/publish_spec.rb +9 -9
  61. data/spec/ruby/server/subscribe_spec.rb +31 -31
  62. data/spec/ruby/server/unsubscribe_spec.rb +31 -31
  63. data/spec/ruby/server_spec.rb +17 -17
  64. data/spec/ruby/transport_spec.rb +23 -23
  65. data/spec/testswarm +23 -10
  66. data/spec/thin_proxy.rb +5 -5
  67. metadata +90 -59
@@ -1,13 +1,13 @@
1
1
  module Faye
2
2
  class Server
3
-
3
+
4
4
  autoload :Socket, File.join(ROOT, 'faye', 'protocol', 'socket')
5
-
5
+
6
6
  include Logging
7
7
  include Extensible
8
8
 
9
9
  META_METHODS = %w[handshake connect disconnect subscribe unsubscribe]
10
-
10
+
11
11
  attr_reader :engine
12
12
 
13
13
  def initialize(options = {})
@@ -18,28 +18,28 @@ module Faye
18
18
 
19
19
  info 'Created new server: ?', @options
20
20
  end
21
-
21
+
22
22
  def flush_connection(messages)
23
23
  client_id = Faye.client_id_from_messages(messages)
24
24
  info 'Flushing connection for ?', client_id
25
25
  @engine.flush(client_id) if client_id
26
26
  end
27
-
27
+
28
28
  def open_socket(client_id, socket)
29
29
  @engine.open_socket(client_id, Socket.new(self, socket))
30
30
  end
31
-
31
+
32
32
  def close_socket(client_id)
33
33
  @engine.flush(client_id)
34
34
  end
35
-
35
+
36
36
  def process(messages, local = false, &callback)
37
37
  messages = [messages].flatten
38
38
  info 'Processing messages: ? (local: ?)', messages, local
39
39
 
40
40
  return callback.call([]) if messages.size == 0
41
41
  processed, responses = 0, []
42
-
42
+
43
43
  gather_replies = lambda do |replies|
44
44
  responses.concat(replies)
45
45
  processed += 1
@@ -47,11 +47,11 @@ module Faye
47
47
  info 'Returning replies: ?', responses
48
48
  callback.call(responses) if processed == messages.size
49
49
  end
50
-
50
+
51
51
  handle_reply = lambda do |replies|
52
52
  extended, expected = 0, replies.size
53
53
  gather_replies.call(replies) if expected == 0
54
-
54
+
55
55
  replies.each_with_index do |reply, i|
56
56
  debug 'Processing reply: ?', reply
57
57
  pipe_through_extensions(:outgoing, reply) do |message|
@@ -61,47 +61,47 @@ module Faye
61
61
  end
62
62
  end
63
63
  end
64
-
64
+
65
65
  messages.each do |message|
66
66
  pipe_through_extensions(:incoming, message) do |piped_message|
67
67
  handle(piped_message, local, &handle_reply)
68
68
  end
69
69
  end
70
70
  end
71
-
71
+
72
72
  def make_response(message)
73
73
  response = {}
74
-
74
+
75
75
  response['id'] = message['id'] if message['id']
76
76
  response['clientId'] = message['clientId'] if message['clientId']
77
77
  response['channel'] = message['channel'] if message['channel']
78
78
  response['error'] = message['error'] if message['error']
79
-
79
+
80
80
  response['successful'] = !response['error']
81
81
  response
82
82
  end
83
-
83
+
84
84
  def handle(message, local = false, &callback)
85
85
  return callback.call([]) if !message
86
86
  info 'Handling message: ? (local: ?)', message, local
87
-
87
+
88
88
  channel_name = message['channel']
89
89
  error = message['error']
90
-
90
+
91
91
  return handle_meta(message, local, &callback) if Channel.meta?(channel_name)
92
-
92
+
93
93
  if Grammar::CHANNEL_NAME !~ channel_name
94
94
  error = Faye::Error.channel_invalid(channel_name)
95
95
  end
96
-
96
+
97
97
  @engine.publish(message) unless error
98
-
98
+
99
99
  response = make_response(message)
100
100
  response['error'] = error if error
101
101
  response['successful'] = !response['error']
102
102
  callback.call([response])
103
103
  end
104
-
104
+
105
105
  def handle_meta(message, local, &callback)
106
106
  method = Channel.parse(message['channel'])[1]
107
107
  client_id = message['clientId']
@@ -119,10 +119,10 @@ module Faye
119
119
  callback.call(responses)
120
120
  end
121
121
  end
122
-
122
+
123
123
  def advize(response, connection_type)
124
124
  return unless [Channel::HANDSHAKE, Channel::CONNECT].include?(response['channel'])
125
-
125
+
126
126
  if connection_type == 'eventsource'
127
127
  interval = (@engine.timeout * 1000).floor
128
128
  timeout = 0
@@ -130,7 +130,7 @@ module Faye
130
130
  interval = (@engine.interval * 1000).floor
131
131
  timeout = (@engine.timeout * 1000).floor
132
132
  end
133
-
133
+
134
134
  advice = response['advice'] ||= {}
135
135
  if response['error']
136
136
  advice['reconnect'] ||= 'handshake'
@@ -140,7 +140,7 @@ module Faye
140
140
  advice['timeout'] ||= timeout
141
141
  end
142
142
  end
143
-
143
+
144
144
  # MUST contain * version
145
145
  # * supportedConnectionTypes
146
146
  # MAY contain * minimumVersion
@@ -149,29 +149,29 @@ module Faye
149
149
  def handshake(message, local = false, &callback)
150
150
  response = make_response(message)
151
151
  response['version'] = BAYEUX_VERSION
152
-
152
+
153
153
  response['error'] = Error.parameter_missing('version') if message['version'].nil?
154
-
154
+
155
155
  client_conns = message['supportedConnectionTypes']
156
-
156
+
157
157
  response['supportedConnectionTypes'] = CONNECTION_TYPES
158
-
158
+
159
159
  if client_conns
160
160
  common_conns = client_conns.select { |c| CONNECTION_TYPES.include?(c) }
161
161
  response['error'] = Error.conntype_mismatch(*client_conns) if common_conns.empty?
162
162
  else
163
163
  response['error'] = Error.parameter_missing('supportedConnectionTypes')
164
164
  end
165
-
165
+
166
166
  response['successful'] = response['error'].nil?
167
167
  return callback.call(response) unless response['successful']
168
-
168
+
169
169
  @engine.create_client do |client_id|
170
170
  response['clientId'] = client_id
171
171
  callback.call(response)
172
172
  end
173
173
  end
174
-
174
+
175
175
  # MUST contain * clientId
176
176
  # * connectionType
177
177
  # MAY contain * ext
@@ -180,54 +180,54 @@ module Faye
180
180
  response = make_response(message)
181
181
  client_id = message['clientId']
182
182
  connection_type = message['connectionType']
183
-
183
+
184
184
  @engine.client_exists(client_id) do |exists|
185
185
  response['error'] = Error.client_unknown(client_id) unless exists
186
186
  response['error'] = Error.parameter_missing('clientId') if client_id.nil?
187
-
187
+
188
188
  unless CONNECTION_TYPES.include?(connection_type)
189
189
  response['error'] = Error.conntype_mismatch(connection_type)
190
190
  end
191
-
191
+
192
192
  response['error'] = Error.parameter_missing('connectionType') if connection_type.nil?
193
-
193
+
194
194
  response['successful'] = response['error'].nil?
195
-
195
+
196
196
  if !response['successful']
197
197
  response.delete('clientId')
198
198
  next callback.call(response)
199
199
  end
200
-
200
+
201
201
  if message['connectionType'] == 'eventsource'
202
202
  message['advice'] ||= {}
203
203
  message['advice']['timeout'] = 0
204
204
  end
205
-
205
+
206
206
  @engine.connect(response['clientId'], message['advice']) do |events|
207
207
  callback.call([response] + events)
208
208
  end
209
209
  end
210
210
  end
211
-
211
+
212
212
  # MUST contain * clientId
213
213
  # MAY contain * ext
214
214
  # * id
215
215
  def disconnect(message, local = false, &callback)
216
- response = make_response(message)
216
+ response = make_response(message)
217
217
  client_id = message['clientId']
218
-
218
+
219
219
  @engine.client_exists(client_id) do |exists|
220
220
  response['error'] = Error.client_unknown(client_id) unless exists
221
221
  response['error'] = Error.parameter_missing('clientId') if client_id.nil?
222
-
222
+
223
223
  response['successful'] = response['error'].nil?
224
224
  response.delete('clientId') unless response['successful']
225
-
225
+
226
226
  @engine.destroy_client(client_id) if response['successful']
227
227
  callback.call(response)
228
228
  end
229
229
  end
230
-
230
+
231
231
  # MUST contain * clientId
232
232
  # * subscription
233
233
  # MAY contain * ext
@@ -236,28 +236,28 @@ module Faye
236
236
  response = make_response(message)
237
237
  client_id = message['clientId']
238
238
  subscription = [message['subscription']].flatten
239
-
239
+
240
240
  @engine.client_exists(client_id) do |exists|
241
241
  response['error'] = Error.client_unknown(client_id) unless exists
242
242
  response['error'] = Error.parameter_missing('clientId') if client_id.nil?
243
243
  response['error'] = Error.parameter_missing('subscription') if message['subscription'].nil?
244
-
244
+
245
245
  response['subscription'] = message['subscription'] || []
246
-
246
+
247
247
  subscription.each do |channel|
248
248
  next if response['error']
249
249
  response['error'] = Error.channel_forbidden(channel) unless local or Channel.subscribable?(channel)
250
250
  response['error'] = Error.channel_invalid(channel) unless Channel.valid?(channel)
251
-
251
+
252
252
  next if response['error']
253
253
  @engine.subscribe(client_id, channel)
254
254
  end
255
-
255
+
256
256
  response['successful'] = response['error'].nil?
257
257
  callback.call(response)
258
258
  end
259
259
  end
260
-
260
+
261
261
  # MUST contain * clientId
262
262
  # * subscription
263
263
  # MAY contain * ext
@@ -266,28 +266,28 @@ module Faye
266
266
  response = make_response(message)
267
267
  client_id = message['clientId']
268
268
  subscription = [message['subscription']].flatten
269
-
269
+
270
270
  @engine.client_exists(client_id) do |exists|
271
271
  response['error'] = Error.client_unknown(client_id) unless exists
272
272
  response['error'] = Error.parameter_missing('clientId') if client_id.nil?
273
273
  response['error'] = Error.parameter_missing('subscription') if message['subscription'].nil?
274
-
274
+
275
275
  response['subscription'] = message['subscription'] || []
276
-
276
+
277
277
  subscription.each do |channel|
278
278
  next if response['error']
279
279
  response['error'] = Error.channel_forbidden(channel) unless local or Channel.subscribable?(channel)
280
280
  response['error'] = Error.channel_invalid(channel) unless Channel.valid?(channel)
281
-
281
+
282
282
  next if response['error']
283
283
  @engine.unsubscribe(client_id, channel)
284
284
  end
285
-
285
+
286
286
  response['successful'] = response['error'].nil?
287
287
  callback.call(response)
288
288
  end
289
289
  end
290
-
290
+
291
291
  end
292
292
  end
293
293
 
@@ -1,23 +1,23 @@
1
1
  module Faye
2
2
  class Server
3
-
3
+
4
4
  class Socket
5
5
  def initialize(server, socket)
6
6
  @server = server
7
7
  @socket = socket
8
8
  end
9
-
9
+
10
10
  def send(message)
11
11
  @server.pipe_through_extensions(:outgoing, message) do |piped_message|
12
12
  @socket.send(Faye.to_json([piped_message]))
13
13
  end
14
14
  end
15
-
15
+
16
16
  def close
17
17
  @socket.close
18
18
  end
19
19
  end
20
-
20
+
21
21
  end
22
22
  end
23
23
 
@@ -1,24 +1,24 @@
1
1
  module Faye
2
2
  class Subscription
3
3
  include EventMachine::Deferrable
4
-
4
+
5
5
  def initialize(client, channels, callback)
6
6
  @client = client
7
7
  @channels = channels
8
8
  @callback = callback
9
9
  @cancelled = false
10
10
  end
11
-
11
+
12
12
  def cancel
13
13
  return if @cancelled
14
14
  @client.unsubscribe(@channels, &@callback)
15
15
  @cancelled = true
16
16
  end
17
-
17
+
18
18
  def unsubscribe
19
19
  cancel
20
20
  end
21
-
21
+
22
22
  end
23
23
  end
24
24
 
@@ -1,18 +1,18 @@
1
1
  module Faye
2
-
2
+
3
3
  class Transport::Http < Transport
4
4
  def self.usable?(client, endpoint, &callback)
5
5
  callback.call(endpoint.is_a?(String))
6
6
  end
7
-
7
+
8
8
  def request(message, timeout)
9
9
  retry_block = retry_block(message, timeout)
10
-
10
+
11
11
  content = Faye.to_json(message)
12
12
  cookies = @cookies.get_cookies(@endpoint)
13
13
  params = build_params(URI.parse(@endpoint), content, cookies)
14
14
  request = create_request(params)
15
-
15
+
16
16
  request.callback do
17
17
  handle_response(request.response, retry_block)
18
18
  store_cookies([*request.response_header['SET_COOKIE']].compact)
@@ -22,9 +22,9 @@ module Faye
22
22
  trigger(:down)
23
23
  end
24
24
  end
25
-
25
+
26
26
  private
27
-
27
+
28
28
  def build_params(uri, content, cookies)
29
29
  {
30
30
  :head => {
@@ -33,12 +33,12 @@ module Faye
33
33
  'Cookie' => cookies * '; ',
34
34
  'Host' => uri.host
35
35
  }.merge(@headers),
36
-
36
+
37
37
  :body => content,
38
38
  :timeout => -1 # for em-http-request < 1.0
39
39
  }
40
40
  end
41
-
41
+
42
42
  def create_request(params)
43
43
  version = EventMachine::HttpRequest::VERSION.split('.')[0].to_i
44
44
  client = if version >= 1
@@ -49,10 +49,10 @@ module Faye
49
49
  else
50
50
  EventMachine::HttpRequest.new(@endpoint)
51
51
  end
52
-
52
+
53
53
  client.post(params)
54
54
  end
55
-
55
+
56
56
  def handle_response(response, retry_block)
57
57
  message = Yajl::Parser.parse(response) rescue nil
58
58
  if message
@@ -63,14 +63,14 @@ module Faye
63
63
  trigger(:down)
64
64
  end
65
65
  end
66
-
66
+
67
67
  def store_cookies(cookies)
68
68
  cookies.each do |cookie|
69
69
  @cookies.set_cookie(@endpoint, cookie)
70
70
  end
71
71
  end
72
72
  end
73
-
73
+
74
74
  Transport.register 'long-polling', Transport::Http
75
-
75
+
76
76
  end