-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.ls
38 lines (30 loc) · 1.04 KB
/
index.ls
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
IO = require \fantasy-io
http = require \http
# type Response = { body :: Readable Buffer
# , status-code :: Int
# , status :: String
# , headers :: Map String String }
# wrap-handler :: (Request → Promise Response) → Request → NodeRes → IO Readable
wrap-handler = (handler, req, res)-->
handler req
.map write-head res
.map (.chain (.body `pipe` res))
# run-promise :: Promise IO a → a
# unsafe! please don't call in pure code
run-promise = (.fork (.unsafe-perform!))
# pipe :: Readable → Writable → IO Readable
pipe = (src,dst)-->
new IO -> src.pipe dst
# write-head :: NodeRes → Response → IO Response
write-head = (res, response)-->
new IO ->
res.write-head do
response.status-code ? 200
response.status ? \OK
response.headers ? {}
return response
# listen :: Integer → Server → IO ()
export listen = (port, server)-->
new IO -> server.listen port
# serve :: (Request → Promise Response) → Server
export serve = http.create-server . (>> run-promise) . wrap-handler