ld-eventsource 2.4.0 → 2.5.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 5199039ee9ec2f9b9843d5bbb79dbc12ca2a625cc0d79cb99b97bb41de729a6c
4
- data.tar.gz: 37b524d87e083d857ab4202749f6f2aac649cb0b31c04401ecfe4dfa68d082ca
3
+ metadata.gz: 3ba844140cea3837caa7a02aa4676af7483faa11230a353da421e435932553b3
4
+ data.tar.gz: 30619609829908038a63d8302cefdbe8bda6935dc691ceaaabff225f2747da8c
5
5
  SHA512:
6
- metadata.gz: 6d1243db76fb427d8c5bcaae4976ce2942fe10c3ec45d4846f0b2b0d57bcb8ceb7a629da61f5f5a1f219afe29f8c1e5b8039dfad44af9b405225773499c9b4fd
7
- data.tar.gz: 3a01a8c90f08f4740e27faf2d0789750fb15076a1e1242e45eebf09219ae500848003d09eeaa53fa16a14009383bbb424690b78419a01956d4cd7bbebfca0673
6
+ metadata.gz: 6b0ffc7be77854b053bc561d1389c3cb6327290066697ca40e6d7a96e29ad9192013b733e9b2973d0b1ebcb5aee66e0eb530066f6bbc3762f95fa80d28183b33
7
+ data.tar.gz: 7efe744ebf4c9cb9b4302fbbf5d2348a31a3f99f45de8cbd54346d2ac5eed8de8c8e79d5120888a2103d712afaf71035ebeec712664a797c229694a561b7dbe9
@@ -162,7 +162,7 @@ module SSE
162
162
  reconnect_reset_interval: reconnect_reset_interval)
163
163
  @first_attempt = true
164
164
 
165
- @on = { event: ->(_) {}, error: ->(_) {} }
165
+ @on = { event: ->(_) {}, error: ->(_) {}, connect: ->(_) {} }
166
166
  @last_id = last_event_id
167
167
  @query_params_callback = nil
168
168
 
@@ -207,6 +207,26 @@ module SSE
207
207
  @on[:error] = action
208
208
  end
209
209
 
210
+ #
211
+ # Specifies a block or Proc to be called when a successful connection is established. This will
212
+ # be called with a single parameter containing the HTTP response headers. It is called
213
+ # from the same worker thread that reads the stream, so no more events will be dispatched until
214
+ # it returns.
215
+ #
216
+ # This is called every time a connection is successfully established, including on reconnections
217
+ # after a failure. It allows you to inspect server response headers such as rate limits, custom
218
+ # metadata, or fallback directives (e.g., `X-LD-FD-FALLBACK`).
219
+ #
220
+ # Any previously specified connect handler will be replaced.
221
+ #
222
+ # @yieldparam headers [Hash, nil] the HTTP response headers from the successful connection,
223
+ # or nil if not available. The headers object uses case-insensitive keys (via the http gem's
224
+ # HTTP::Headers).
225
+ #
226
+ def on_connect(&action)
227
+ @on[:connect] = action
228
+ end
229
+
210
230
  #
211
231
  # Specifies a block or Proc to generate query parameters dynamically. This will be called before
212
232
  # each connection attempt (both initial connection and reconnections), allowing you to update
@@ -323,13 +343,15 @@ module SSE
323
343
  uri = build_uri_with_query_params
324
344
  @logger.info { "Connecting to event stream at #{uri}" }
325
345
  cxn = @http_client.request(@method, uri, build_opts)
346
+ headers = cxn.headers
326
347
  if cxn.status.code == 200
327
348
  content_type = cxn.content_type.mime_type
328
349
  if content_type && content_type.start_with?("text/event-stream")
350
+ @on[:connect].call(headers) # Notify connect callback with headers
329
351
  return cxn # we're good to proceed
330
352
  else
331
353
  reset_http
332
- err = Errors::HTTPContentTypeError.new(content_type)
354
+ err = Errors::HTTPContentTypeError.new(content_type, headers)
333
355
  @on[:error].call(err)
334
356
  @logger.warn { "Event source returned unexpected content type '#{content_type}'" }
335
357
  end
@@ -337,7 +359,7 @@ module SSE
337
359
  body = cxn.to_s # grab the whole response body in case it has error details
338
360
  reset_http
339
361
  @logger.info { "Server returned error status #{cxn.status.code}" }
340
- err = Errors::HTTPStatusError.new(cxn.status.code, body)
362
+ err = Errors::HTTPStatusError.new(cxn.status.code, body, headers)
341
363
  @on[:error].call(err)
342
364
  end
343
365
  rescue
@@ -9,9 +9,10 @@ module SSE
9
9
  # handler specified in {Client#on_error}.
10
10
  #
11
11
  class HTTPStatusError < StandardError
12
- def initialize(status, message)
12
+ def initialize(status, message, headers = nil)
13
13
  @status = status
14
14
  @message = message
15
+ @headers = headers
15
16
  super("HTTP error #{status}")
16
17
  end
17
18
 
@@ -22,6 +23,13 @@ module SSE
22
23
  # The response body, if any.
23
24
  # @return [String]
24
25
  attr_reader :message
26
+
27
+ # The HTTP response headers, if any.
28
+ #
29
+ # The headers object uses case-insensitive keys (via the http gem's HTTP::Headers).
30
+ #
31
+ # @return [Hash, nil] the response headers, or nil if not available
32
+ attr_reader :headers
25
33
  end
26
34
 
27
35
  #
@@ -29,14 +37,22 @@ module SSE
29
37
  # handler specified in {Client#on_error}.
30
38
  #
31
39
  class HTTPContentTypeError < StandardError
32
- def initialize(type)
40
+ def initialize(type, headers = nil)
33
41
  @content_type = type
42
+ @headers = headers
34
43
  super("invalid content type \"#{type}\"")
35
44
  end
36
45
 
37
46
  # The HTTP content type.
38
47
  # @return [String]
39
48
  attr_reader :type
49
+
50
+ # The HTTP response headers, if any.
51
+ #
52
+ # The headers object uses case-insensitive keys (via the http gem's HTTP::Headers).
53
+ #
54
+ # @return [Hash, nil] the response headers, or nil if not available
55
+ attr_reader :headers
40
56
  end
41
57
 
42
58
  #
@@ -1,3 +1,3 @@
1
1
  module SSE
2
- VERSION = "2.4.0" # x-release-please-version
2
+ VERSION = "2.5.0" # x-release-please-version
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ld-eventsource
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.4.0
4
+ version: 2.5.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - LaunchDarkly
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2026-01-15 00:00:00.000000000 Z
11
+ date: 2026-01-23 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: logger