-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.ls
41 lines (35 loc) · 922 Bytes
/
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
39
40
41
{Part, Status} = require \peat
{nil} = require \highland
# handle-error :: (Error → Stream a) → Stream a → Stream a
handle-error = (f, stream)-->
stream.consume (e, x, p, n)->
if e
try n f e
catch e2
p e2
p null nil
else if x is nil
p null nil
else
p null x
n!
# dev-err :: Error → Result
dev-err = (err)-> [
Status 500
err.stack
]
# is-body :: Part → Boolean
is-body = (chunk)-> chunk instanceof Buffer or typeof chunk is \string
# handle-with-error :: (Error -> Result) -> (Request -> Result) -> (Request, Response) -> ()
handle-with-error = (err-handler, handler, req, res)-->
handle-error err-handler, handler req
.filter (part)-> match part
| Part.is => that.run res; false
| is-body => true
.pipe res
# handle :: (Request -> Result) -> (Request, Response) -> ()
handle = handle-with-error dev-err
module.exports = handle import {
handle-with-error,
handle
}