File tree Expand file tree Collapse file tree 1 file changed +10
-4
lines changed Expand file tree Collapse file tree 1 file changed +10
-4
lines changed Original file line number Diff line number Diff line change @@ -54,10 +54,16 @@ def to_json(string)
54
54
# @return [Proc] An outer proc that iterates over a raw stream, converting it to JSON.
55
55
def to_json_stream ( user_proc :)
56
56
proc do |chunk , _ |
57
- chunk . scan ( /(?:data|error): (\{ .*\} )/i ) . flatten . each do |data |
58
- user_proc . call ( JSON . parse ( data ) )
59
- rescue JSON ::ParserError
60
- # Ignore invalid JSON.
57
+ @buffer ||= ""
58
+ @buffer += chunk
59
+ while ( match = @buffer . match ( /(?:data|error): (\{ .*\} )/i ) )
60
+ data = match [ 1 ]
61
+ @buffer = @buffer [ match . end ( 0 ) ..-1 ] # Remove the processed data from the buffer
62
+ begin
63
+ user_proc . call ( JSON . parse ( data ) )
64
+ rescue JSON ::ParserError => e
65
+ Rails . logger . error { "JSON parsing error: #{ e . message } " }
66
+ end
61
67
end
62
68
end
63
69
end
You can’t perform that action at this time.
0 commit comments