|
43 | 43 | (.write out (int 0)) ;; terminator |
44 | 44 | (.flush out)) |
45 | 45 |
|
46 | | -(defn read-response [^BufferedReader in] |
| 46 | +(defn ^String read-response [^BufferedReader in] |
47 | 47 | (let [sb (StringBuilder.)] |
48 | 48 | (loop [sb sb c (.read in)] |
49 | | - (cond |
50 | | - (= c 1) (let [ret (str sb)] |
51 | | - (print ret) |
52 | | - (recur (StringBuilder.) (.read in))) |
53 | | - (= c 0) (str sb) |
54 | | - :else (do |
55 | | - (.append sb (char c)) |
56 | | - (recur sb (.read in))))))) |
| 49 | + (case c |
| 50 | + -1 (throw (IOException. "Stream closed")) |
| 51 | + 0 (str sb) |
| 52 | + (do |
| 53 | + (.append sb (char c)) |
| 54 | + (recur sb (.read in))))))) |
57 | 55 |
|
58 | 56 | (defn node-eval |
59 | 57 | "Evaluate a JavaScript string in the Node REPL process." |
|
93 | 91 | (defn- pipe [^Process proc in stream ios] |
94 | 92 | ;; we really do want system-default encoding here |
95 | 93 | (with-open [^Reader in (-> in InputStreamReader. BufferedReader.)] |
96 | | - (loop [buf (char-array (* 64 1024))] |
97 | | - (when (alive? proc) |
98 | | - (try |
99 | | - (let [len (.read in buf)] |
100 | | - (when-not (neg? len) |
101 | | - (try |
102 | | - (let [{:strs [repl data]} (json/read-str (String. buf)) |
103 | | - stream (or (.get ios repl) stream)] |
104 | | - (.write stream data 0 (.length ^String data)) |
105 | | - (.flush stream)) |
106 | | - (catch Throwable _ |
107 | | - (.write stream buf 0 len) |
108 | | - (.flush stream))))) |
109 | | - (catch IOException e |
110 | | - (when (and (alive? proc) (not (.contains (.getMessage e) "Stream closed"))) |
111 | | - (.printStackTrace e *err*)))) |
112 | | - (recur buf))))) |
| 94 | + (while (alive? proc) |
| 95 | + (try |
| 96 | + (let [res (read-response in)] |
| 97 | + (try |
| 98 | + (let [{:keys [repl content]} (json/read-str res :key-fn keyword) |
| 99 | + stream (or (.get ios repl) stream)] |
| 100 | + (.write stream content 0 (.length ^String content)) |
| 101 | + (.flush stream)) |
| 102 | + (catch Throwable _ |
| 103 | + (.write stream res 0 (.length res)) |
| 104 | + (.flush stream)))) |
| 105 | + (catch IOException e |
| 106 | + (when (and (alive? proc) (not (.contains (.getMessage e) "Stream closed"))) |
| 107 | + (.printStackTrace e *err*))))))) |
113 | 108 |
|
114 | 109 | (defn- build-process |
115 | 110 | [opts repl-env input-src] |
|
0 commit comments