event_stream_parser 1.0.1 → 1.0.2

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: 06cff98dad06559a4b803723dd45890af9e995a20d9075482028dddb415d5618
4
- data.tar.gz: 251f89c451dbee9fb3914afa46dedc86697955bf02b342016baa5654f26563c7
3
+ metadata.gz: 8ba77ffdd9dfae1472521fa58d3097d85f3aa7d1e0d14221abc14b9fb001b9c5
4
+ data.tar.gz: 03cf9e93ad9833ea2b504487bacd5354bf1123ba2081473c8df267125133102d
5
5
  SHA512:
6
- metadata.gz: f179630a15295755bab456c47c81a0c013305d3f6a21b42f4ffa3e2208203f29958643d2fc7c21c5cc7be09ed8ec36494a674047f4ddd3deaa7f6d58a0444db4
7
- data.tar.gz: 8d8b918bd1eafc6c06dcbfc788b1610c89fffa2358d2eaf3b6b84767fbe1632fa4e970f584dedbc48352e53b10cb5300a08c2d9797474d7e197d79b9ee513a0d
6
+ metadata.gz: 2529fc75f8d41dfff5e7132d42864340a75a0bf7b93faf9be5f178668c35c4714d0614fc6cc9db0f23ddbf09870a89a7f32ae2211d3f0ef40c72400f3b847986
7
+ data.tar.gz: 30c09a7a4aa4a45ae6e167832c94ad8f8087ab631f6b5f2c40180f66ae2e9a79d70572b3602ac0789f1e97bd831b6dc9d3dc0be34c254fbc4fa391438b012128
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module EventStreamParser
4
- VERSION = '1.0.1'
4
+ VERSION = '1.0.2'
5
5
  end
@@ -1,5 +1,6 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ require 'strscan'
3
4
  require 'event_stream_parser/version'
4
5
 
5
6
  module EventStreamParser
@@ -12,6 +13,10 @@ module EventStreamParser
12
13
  # Code comments are copied from the spec.
13
14
  #
14
15
  class Parser
16
+ # Largest value of an unsigned 64-bit integer, as digits.
17
+ MAX_RECONNECTION_TIME_DIGITS = '18446744073709551615'
18
+ private_constant :MAX_RECONNECTION_TIME_DIGITS
19
+
15
20
  def initialize
16
21
  ##
17
22
  # When a stream is parsed, a data buffer, an event type buffer, and a last
@@ -24,11 +29,15 @@ module EventStreamParser
24
29
 
25
30
  @reconnection_time = nil
26
31
  @buffer = +''
32
+ # Kept as state so a block that breaks, raises or feeds again resumes after the last processed line.
33
+ @processed_bytes = 0
27
34
  @buffer_scanned = true
28
35
  @last_delimiter = nil
29
36
  end
30
37
 
31
38
  def feed(chunk, &proc)
39
+ # A block that left the last call early, or that makes this call, leaves processed lines in the buffer.
40
+ drop_processed_lines
32
41
  @buffer << chunk
33
42
 
34
43
  ##
@@ -39,7 +48,7 @@ module EventStreamParser
39
48
  # followed by a U+000A LINE FEED (LF) character being the ways in which a
40
49
  # line can end.
41
50
  #
42
- @buffer.delete_prefix!("\n") if @last_delimiter == "\r"
51
+ @last_delimiter = "\r\n" if @last_delimiter == "\r" && @buffer.delete_prefix!("\n")
43
52
 
44
53
  # Only skip the search after the previous call drained complete lines.
45
54
  buffer_scanned = @buffer_scanned
@@ -49,11 +58,8 @@ module EventStreamParser
49
58
  return
50
59
  end
51
60
 
52
- while (line = @buffer.slice!(/.*?(?<delim>\r\n|\r|\n)/))
53
- line.chomp!
54
- @last_delimiter = $~[:delim]
55
- process_line(line, &proc)
56
- end
61
+ scan_lines(&proc)
62
+ drop_processed_lines
57
63
  @buffer_scanned = true
58
64
  nil
59
65
  ##
@@ -69,6 +75,29 @@ module EventStreamParser
69
75
 
70
76
  private
71
77
 
78
+ def scan_lines(&proc)
79
+ scanner = StringScanner.new(@buffer)
80
+ while (line = scanner.scan_until(/\r\n|\r|\n/))
81
+ @processed_bytes = scanner.pos
82
+ line.chomp!
83
+ @last_delimiter = scanner.matched
84
+ process_line(line, &proc)
85
+ # A feed call from the block moves the unprocessed bytes to the front.
86
+ scanner.pos = @processed_bytes
87
+ end
88
+ end
89
+
90
+ # Removing each line from the front as it is read would copy the rest of the buffer every time.
91
+ def drop_processed_lines
92
+ return if @processed_bytes.zero?
93
+
94
+ # Copied, because the slice shares the memory of the whole chunk.
95
+ unprocessed = @buffer.byteslice(@processed_bytes..-1)
96
+ @buffer.clear
97
+ @buffer << unprocessed
98
+ @processed_bytes = 0
99
+ end
100
+
72
101
  def process_line(line, &proc)
73
102
  ##
74
103
  # Lines must be processed, in the order they are received, as follows:
@@ -161,7 +190,8 @@ module EventStreamParser
161
190
  # field value as an integer in base ten, and set the event stream's
162
191
  # reconnection time to that integer. Otherwise, ignore the field.
163
192
  #
164
- @reconnection_time = value.to_i if /\A\d+\z/.match?(value)
193
+ time = parse_reconnection_time(value)
194
+ @reconnection_time = time if time
165
195
  ##
166
196
  # Otherwise
167
197
  #
@@ -228,6 +258,19 @@ module EventStreamParser
228
258
  yield type, data, id, @reconnection_time
229
259
  end
230
260
 
261
+ # Returns nil when the value has a non-digit or does not fit in 64 bits.
262
+ # Like browsers, ignore values above the unsigned 64-bit range. The length
263
+ # check comes before the conversion, so the cost stays bounded.
264
+ def parse_reconnection_time(value)
265
+ return unless /\A\d+\z/.match?(value)
266
+
267
+ digits = value.sub(/\A0+/, '')
268
+ return unless digits.length < MAX_RECONNECTION_TIME_DIGITS.length ||
269
+ (digits.length == MAX_RECONNECTION_TIME_DIGITS.length && digits <= MAX_RECONNECTION_TIME_DIGITS)
270
+
271
+ digits.to_i
272
+ end
273
+
231
274
  def ignore; end
232
275
  end
233
276
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: event_stream_parser
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.1
4
+ version: 1.0.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ates Goral
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2026-11-10 00:00:00.000000000 Z
11
+ date: 2026-11-19 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description:
14
14
  email: