Don't loop endlessly on EOF in server mode.

This commit is contained in:
Phil Hagelberg 2025-10-19 13:28:44 -07:00
parent 7313599907
commit 53426c0a3e
3 changed files with 11 additions and 12 deletions

View File

@ -19,16 +19,15 @@ on the empty table to tell dkjson to serialize as {}."
(λ read-header [in ?header]
"Reads the header of a JSON-RPC message"
(let [header (or ?header {})]
(case (in:read)
nil nil ;; I've hit end of stream, return nil instead of a header
line (case (line:match "^(.-)\r?$") ;; strip trailing \r
"" header ;; base case. empty line marks end of header
line (let [(k v) (line:match "^(.-): (.-)$")]
(if (not (and k v))
(error (.. "fennel-ls encountered a malformed json-rpc header: \"" line "\"")))
(tset header k v)
(read-header in header))))))
(let [header (or ?header {})
line (assert (in:read) "EOF")]
(case (line:match "^(.-)\r?$") ;; strip trailing \r
"" header ;; base case. empty line marks end of header
line (let [(k v) (line:match "^(.-): (.-)$")]
(if (not (and k v))
(error (.. "fennel-ls encountered a malformed json-rpc header: \"" line "\"")))
(tset header k v)
(read-header in header)))))
(λ read-n [in len ?buffer]
"read a string of exactly `len` characters from the `in` stream.

View File

@ -26,7 +26,7 @@
(with-temp-form [f [(let [(a b) (values 1 (+ 2))] (+ a b))
[(do (print "done doing all the fun things we did!"))]]
out "./fennel-ls --fix --yes"]
(faith.= "[(let [[a b] [(values 1 2)]] (+ a b))
(faith.= "[(let [(a b) (values 1 2)] (+ a b))
[(print \"done doing all the fun things we did!\")]]"
(with-open [file (assert (io.open f))]
(file:read :*a)) out)))

View File

@ -9,7 +9,7 @@
(let [out (stringio.open "Content-Length: 29\r\n\r\n{\"my json content\":\"is cool\"}Content-Length: 29\r\n\r\n{\"my json content\":\"is neat\"}")]
(faith.= {"my json content" "is cool"} (json-rpc.read out))
(faith.= {"my json content" "is neat"} (json-rpc.read out))
(faith.= nil (json-rpc.read out)))
(faith.error "EOF" #(json-rpc.read out)))
(let [out (stringio.open "Content-Length: 9\r\n\r\n{{{{{}}}}")]
(faith.= :string (type (json-rpc.read out)) "json-rpc returns a table on successful read, and a string on unsuccessful read. It's jank and should probably be replaced with an ok, err system")))