/
ws.clj
336 lines (264 loc) · 9.41 KB
/
ws.clj
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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
(ns shadow.cljs.devtools.server.worker.ws
"the websocket which is injected into the app, responsible for live-reload, repl-eval, etc"
(:require [clojure.core.async :as async :refer (go <! >! thread alt!! >!!)]
[clojure.edn :as edn]
[shadow.build.output :as output]
[shadow.cljs.devtools.server.system-bus :as sys-bus]
[shadow.cljs.devtools.server.system-msg :as sys-msg]
[shadow.cljs.devtools.server.supervisor :as super]
[shadow.cljs.devtools.server.worker :as worker]
[shadow.build :as comp]
[shadow.http.router :as http]
[shadow.build.data :as data]
[shadow.build.resource :as rc]
[clojure.tools.logging :as log])
(:import (java.util UUID)))
(defn ws-loop!
[{:keys [worker-proc watch-chan eval-out in out result-chan] :as client-state}]
(let [{:keys [system-bus]} worker-proc]
(worker/watch worker-proc watch-chan true)
;; FIXME: the client should probably trigger this
;; a node-repl isn't interested in this at all
(sys-bus/sub system-bus ::sys-msg/css-reload out false)
(loop [client-state client-state]
(alt!!
eval-out
([msg]
(when-not (nil? msg)
(>!! out msg)
(recur client-state)))
;; forward some build watch messages to the client
watch-chan
([msg]
(when-not (nil? msg)
(>!! out msg)
(recur client-state)
))
in
([msg]
(when-not (nil? msg)
(>!! result-chan msg)
(recur client-state))
)))
(async/close! result-chan)))
(defn ws-connect
[{:keys [ring-request] :as ctx}
{:keys [output] :as worker-proc} client-id client-type]
(let [{:keys [ws-in ws-out]} ring-request
eval-out
(-> (async/sliding-buffer 10)
(async/chan))
result-chan
(worker/repl-eval-connect worker-proc client-id eval-out)
;; no need to forward :build-log messages to the client
watch-ignore
#{:build-log
:repl/result
:repl/error ;; server-side error
:repl/action
:repl/eval-start
:repl/eval-stop
:repl/client-start
:repl/client-stop}
watch-chan
(-> (async/sliding-buffer 10)
(async/chan
(remove #(contains? watch-ignore (:type %)))))
client-state
{:worker-proc worker-proc
:client-id client-id
:client-type client-type
:in ws-in
:out ws-out
:eval-out eval-out
:result-chan result-chan
:watch-chan watch-chan}]
(thread (ws-loop! client-state))
))
(defn ws-listener-connect
[{:keys [ring-request] :as ctx}
{:keys [output] :as worker-proc} client-id]
(let [{:keys [ws-in ws-out]} ring-request
;; FIXME: let the client decide?
watch-ignore
#{:build-log
:repl/result
:repl/error ;; server-side error
:repl/action
:repl/eval-start
:repl/eval-stop
:repl/client-start
:repl/client-stop}
watch-chan
(-> (async/sliding-buffer 10)
(async/chan
(remove #(contains? watch-ignore (:type %)))))]
(worker/watch worker-proc watch-chan true)
(let [last-known-state
(-> worker-proc :state-ref deref :build-state ::comp/build-info)]
(>!! ws-out {:type :build-init
:info last-known-state}))
;; close watch when websocket closes
(go (loop []
(when-some [msg (<! ws-in)]
(log/warn "ignored listener msg" msg)
(recur)))
(async/close! watch-chan))
(go (loop []
(when-some [msg (<! watch-chan)]
(>! ws-out msg)
(recur))
))))
(defn compile-req
[{:keys [ring-request] :as ctx} worker-proc]
(let [{:keys [request-method body]}
ring-request
headers
{"Access-Control-Allow-Origin" "*"
"Access-Control-Allow-Headers"
(or (get-in ring-request [:headers "access-control-request-headers"])
"content-type")
"content-type" "application/edn; charset=utf-8"}]
;; CORS sends OPTIONS first
(case request-method
:options
{:status 200
:headers headers
:body ""}
:post
(let [{:keys [input]}
(-> (slurp body)
(edn/read-string))
result (worker/repl-compile worker-proc input)]
{:status 200
:headers headers
:body (pr-str result)})
;; bad requests
{:status 400
:body "Only POST or OPTIONS requests allowed."}
)))
(defn files-req
"a POST request from the REPL client asking for the compile JS for sources by name
sends a {:sources [...]} structure with a vector of source names
the response will include [{:js code :name ...} ...] with :js ready to eval"
[{:keys [ring-request] :as ctx}
{:keys [state-ref] :as worker-proc}]
(let [{:keys [request-method body]}
ring-request
headers
{"Access-Control-Allow-Origin" "*"
"Access-Control-Allow-Headers"
(or (get-in ring-request [:headers "access-control-request-headers"])
"content-type")
"content-type" "application/edn; charset=utf-8"}]
;; CORS sends OPTIONS first
(case request-method
:options
{:status 200
:headers headers
:body ""}
:post
(let [text
(slurp body)
{:keys [sources] :as req}
(edn/read-string text)
build-state
(:build-state @state-ref)
module-format
(get-in build-state [:build-options :module-format])]
{:status 200
:headers headers
:body
(->> sources
(map (fn [src-id]
(assert (rc/valid-resource-id? src-id))
(let [{:keys [resource-name type output-name ns output] :as src}
(data/get-source-by-id build-state src-id)
{:keys [js] :as output}
(data/get-output! build-state src)]
{:resource-name resource-name
:resource-id src-id
:output-name output-name
;; FIXME: make this pretty ...
:js
(case module-format
:goog
(let [sm-text (output/generate-source-map-inline build-state src output "")]
(str js sm-text))
:js
(let [prepend
(output/js-module-src-prepend build-state src false)
append
"" #_ (output/js-module-src-append build-state src)
sm-text
(output/generate-source-map-inline build-state src output prepend)]
(str prepend js append sm-text)))
})))
(into [])
(pr-str))})
;; bad requests
{:status 400
:body "Only POST or OPTIONS requests allowed."}
)))
(defn process-req
[{::http/keys [path-tokens] :keys [supervisor] :as ctx}]
;; "/worker/browser/430da920-ffe8-4021-be47-c9ca77c6defd/305de5d9-0272-408f-841e-479937512782/browser"
;; _ _ to drop / and worker
(let [[action build-id proc-id client-id client-type :as x]
path-tokens
build-id
(keyword build-id)
proc-id
(UUID/fromString proc-id)
worker-proc
(super/get-worker supervisor build-id)]
(cond
(nil? worker-proc)
{:status 404
:body "No worker for build."}
(not= proc-id (:proc-id worker-proc))
{:status 403
:body "stale client, please reload"}
:else
(case action
"files"
(files-req ctx worker-proc)
"compile"
(compile-req ctx worker-proc)
:else
{:status 404
:headers {"content-type" "text/plain"}
:body "Not found."}))))
(defn process-ws
[{::http/keys [path-tokens] :keys [supervisor] :as ctx}]
;; "/worker/browser/430da920-ffe8-4021-be47-c9ca77c6defd/305de5d9-0272-408f-841e-479937512782/browser"
;; _ _ to drop / and worker
(let [[action build-id proc-id client-id client-type :as x]
path-tokens
build-id
(keyword build-id)
proc-id
(UUID/fromString proc-id)
ws-out
(get-in ctx [:ring-request :ws-out])
worker-proc
(super/get-worker supervisor build-id)]
(cond
(nil? worker-proc)
(go (>! ws-out {:type :client/no-worker}))
;; can't send {:status 404 :body "no worker"}
;; as there appears to be no way to access either the status code or body
;; on the client via the WebSocket API to know why a websocket connection failed
;; onerror returns nothing useful only that it failed
;; so instead we pretend to handshake properly, send one message and disconnect
(not= proc-id (:proc-id worker-proc))
(go (>! ws-out {:type :client/stale}))
:else
(case action
"worker"
(ws-connect ctx worker-proc client-id client-type)
"listener"
(ws-listener-connect ctx worker-proc client-id)
:else
nil
))))