File: reader.rb

package info (click to toggle)
ruby-language-server-protocol 3.17.0.5-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,636 kB
  • sloc: ruby: 10,741; makefile: 4
file content (35 lines) | stat: -rw-r--r-- 715 bytes parent folder | download
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