1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
|
# frozen_string_literal: true
require "json"
module LanguageServer
module Protocol
module Transport
module Io
class Reader
def initialize(io)
@io = io
io.binmode
end
def read(&block)
while buffer = io.gets("\r\n\r\n")
content_length = buffer.match(/Content-Length: (\d+)/i)[1].to_i
message = io.read(content_length) or raise
request = JSON.parse(message, symbolize_names: true)
block.call(request)
end
end
def close
io.close
end
private
attr_reader :io
end
end
end
end
end
|