Skip to content

Commit a46ebd9

Browse files
authored
Merge pull request #24 from purescript-node/compiler/0.12
Compiler/0.12
2 parents 71c8050 + bb1daa2 commit a46ebd9

File tree

7 files changed

+77
-81
lines changed

7 files changed

+77
-81
lines changed

.travis.yml

+6
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,13 @@ language: node_js
22
dist: trusty
33
sudo: required
44
node_js: 6
5+
env:
6+
- PATH=$HOME/purescript:$PATH
57
install:
8+
- TAG=$(wget -q -O - https://github.com/purescript/purescript/releases/latest --server-response --max-redirect 0 2>&1 | sed -n -e 's/.*Location:.*tag\///p')
9+
- wget -O $HOME/purescript.tar.gz https://github.com/purescript/purescript/releases/download/$TAG/linux64.tar.gz
10+
- tar -xvf $HOME/purescript.tar.gz -C $HOME/
11+
- chmod a+x $HOME/purescript
612
- npm install -g bower
713
- npm install
814
script:

bower.json

+14-8
Original file line numberDiff line numberDiff line change
@@ -12,15 +12,21 @@
1212
"url": "git://github.com/purescript-node/purescript-node-http.git"
1313
},
1414
"devDependencies": {
15-
"purescript-console": "^3.0.0"
15+
"purescript-console": "^4.1.0"
1616
},
1717
"dependencies": {
18-
"purescript-maps": "^3.0.0",
19-
"purescript-node-streams": "^3.0.0",
20-
"purescript-node-url": "^3.0.0",
21-
"purescript-options": "^3.0.0",
22-
"purescript-unsafe-coerce": "^3.0.0",
23-
"purescript-node-buffer": "^3.0.1",
24-
"purescript-arraybuffer-types": "^2.0.0"
18+
"purescript-arraybuffer-types": "^2.0.0",
19+
"purescript-contravariant": "^4.0.0",
20+
"purescript-effect": "^2.0.0",
21+
"purescript-foreign": "^5.0.0",
22+
"purescript-foreign-object": "^1.0.0",
23+
"purescript-maybe": "^4.0.0",
24+
"purescript-node-buffer": "^5.0.0",
25+
"purescript-node-streams": "^4.0.0",
26+
"purescript-node-url": "^4.0.0",
27+
"purescript-nullable": "^4.0.0",
28+
"purescript-options": "^4.0.0",
29+
"purescript-prelude": "^4.0.0",
30+
"purescript-unsafe-coerce": "^4.0.0"
2531
}
2632
}

package.json

+4-5
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,9 @@
77
},
88
"devDependencies": {
99
"jscs": "^3.0.7",
10-
"jshint": "^2.9.4",
11-
"pulp": "^11.0.0",
12-
"purescript": "^0.11.1",
13-
"purescript-psa": "^0.5.0",
14-
"rimraf": "^2.6.1"
10+
"jshint": "^2.9.5",
11+
"pulp": "^12.2.0",
12+
"purescript-psa": "^0.6.0",
13+
"rimraf": "^2.6.2"
1514
}
1615
}

src/Node/HTTP.purs

+15-22
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ module Node.HTTP
44
( Server
55
, Request
66
, Response
7-
, HTTP
87

98
, createServer
109
, listen
@@ -27,14 +26,11 @@ module Node.HTTP
2726

2827
import Prelude
2928

30-
import Control.Monad.Eff (Eff, kind Effect)
31-
3229
import Data.Maybe (Maybe)
3330
import Data.Nullable (Nullable, toNullable)
34-
import Data.StrMap (StrMap)
35-
31+
import Effect (Effect)
32+
import Foreign.Object (Object)
3633
import Node.Stream (Writable, Readable)
37-
3834
import Unsafe.Coerce (unsafeCoerce)
3935

4036
-- | The type of a HTTP server object
@@ -46,22 +42,19 @@ foreign import data Request :: Type
4642
-- | A HTTP response object
4743
foreign import data Response :: Type
4844

49-
-- | The effect associated with using the HTTP module.
50-
foreign import data HTTP :: Effect
51-
5245
-- | Create a HTTP server, given a function to be executed when a request is received.
53-
foreign import createServer :: forall eff. (Request -> Response -> Eff (http :: HTTP | eff) Unit) -> Eff (http :: HTTP | eff) Server
46+
foreign import createServer :: (Request -> Response -> Effect Unit) -> Effect Server
5447

55-
foreign import listenImpl :: forall eff. Server -> Int -> String -> Nullable Int -> Eff (http :: HTTP | eff) Unit -> Eff (http :: HTTP | eff) Unit
48+
foreign import listenImpl :: Server -> Int -> String -> Nullable Int -> Effect Unit -> Effect Unit
5649

57-
foreign import closeImpl :: forall eff. Server -> Eff (http :: HTTP | eff) Unit -> Eff (http :: HTTP | eff) Unit
50+
foreign import closeImpl :: Server -> Effect Unit -> Effect Unit
5851

5952
-- | Listen on a port in order to start accepting HTTP requests. The specified callback will be run when setup is complete.
60-
listen :: forall eff. Server -> ListenOptions -> Eff (http :: HTTP | eff) Unit -> Eff (http :: HTTP | eff) Unit
53+
listen :: Server -> ListenOptions -> Effect Unit -> Effect Unit
6154
listen server opts done = listenImpl server opts.port opts.hostname (toNullable opts.backlog) done
6255

6356
-- | Close a listening HTTP server. The specified callback will be run the server closing is complete.
64-
close :: forall eff. Server -> Eff (http :: HTTP | eff) Unit -> Eff (http :: HTTP | eff) Unit
57+
close :: Server -> Effect Unit -> Effect Unit
6558
close server done = closeImpl server done
6659

6760
-- | Options to be supplied to `listen`. See the [Node API](https://nodejs.org/dist/latest-v6.x/docs/api/http.html#http_server_listen_handle_callback) for detailed information about these.
@@ -72,14 +65,14 @@ type ListenOptions =
7265
}
7366

7467
-- | Listen on a unix socket. The specified callback will be run when setup is complete.
75-
foreign import listenSocket :: forall eff. Server -> String -> Eff (http :: HTTP | eff) Unit -> Eff (http :: HTTP | eff) Unit
68+
foreign import listenSocket :: Server -> String -> Effect Unit -> Effect Unit
7669

7770
-- | Get the request HTTP version
7871
httpVersion :: Request -> String
7972
httpVersion = _.httpVersion <<< unsafeCoerce
8073

8174
-- | Get the request headers as a hash
82-
requestHeaders :: Request -> StrMap String
75+
requestHeaders :: Request -> Object String
8376
requestHeaders = _.headers <<< unsafeCoerce
8477

8578
-- | Get the request method (GET, POST, etc.)
@@ -91,21 +84,21 @@ requestURL :: Request -> String
9184
requestURL = _.url <<< unsafeCoerce
9285

9386
-- | Coerce the request object into a readable stream.
94-
requestAsStream :: forall eff. Request -> Readable () (http :: HTTP | eff)
87+
requestAsStream :: Request -> Readable ()
9588
requestAsStream = unsafeCoerce
9689

9790
-- | Set a header with a single value.
98-
foreign import setHeader :: forall eff. Response -> String -> String -> Eff (http :: HTTP | eff) Unit
91+
foreign import setHeader :: Response -> String -> String -> Effect Unit
9992

10093
-- | Set a header with multiple values.
101-
foreign import setHeaders :: forall eff. Response -> String -> Array String -> Eff (http :: HTTP | eff) Unit
94+
foreign import setHeaders :: Response -> String -> Array String -> Effect Unit
10295

10396
-- | Set the status code.
104-
foreign import setStatusCode :: forall eff. Response -> Int -> Eff (http :: HTTP | eff) Unit
97+
foreign import setStatusCode :: Response -> Int -> Effect Unit
10598

10699
-- | Set the status message.
107-
foreign import setStatusMessage :: forall eff. Response -> String -> Eff (http :: HTTP | eff) Unit
100+
foreign import setStatusMessage :: Response -> String -> Effect Unit
108101

109102
-- | Coerce the response object into a writable stream.
110-
responseAsStream :: forall eff. Response -> Writable () (http :: HTTP | eff)
103+
responseAsStream :: Response -> Writable ()
111104
responseAsStream = unsafeCoerce

src/Node/HTTP/Client.purs

+17-20
Original file line numberDiff line numberDiff line change
@@ -31,18 +31,15 @@ module Node.HTTP.Client
3131

3232
import Prelude
3333

34-
import Control.Monad.Eff (Eff)
35-
36-
import Data.Foreign (Foreign, toForeign)
3734
import Data.Functor.Contravariant ((>$<))
3835
import Data.Maybe (Maybe)
39-
import Data.Options (Options, Option, options, opt)
40-
import Data.StrMap (StrMap, delete, lookup)
41-
42-
import Node.HTTP (HTTP)
36+
import Data.Options (Option, Options, opt, options)
37+
import Effect (Effect)
38+
import Foreign (Foreign, unsafeToForeign)
39+
import Foreign.Object (Object)
40+
import Foreign.Object as Object
4341
import Node.Stream (Readable, Writable)
4442
import Node.URL as URL
45-
4643
import Unsafe.Coerce (unsafeCoerce)
4744

4845
-- | A HTTP request object
@@ -52,7 +49,7 @@ foreign import data Request :: Type
5249
foreign import data Response :: Type
5350

5451
-- | A HTTP request object
55-
newtype RequestHeaders = RequestHeaders (StrMap String)
52+
newtype RequestHeaders = RequestHeaders (Object String)
5653

5754
-- | The type of HTTP request options
5855
data RequestOptions
@@ -110,42 +107,42 @@ familyToOption IPV4 = 4
110107
familyToOption IPV6 = 6
111108

112109
-- | Make a HTTP request using the specified options and response callback.
113-
foreign import requestImpl :: forall eff. Foreign -> (Response -> Eff (http :: HTTP | eff) Unit) -> Eff (http :: HTTP | eff) Request
110+
foreign import requestImpl :: Foreign -> (Response -> Effect Unit) -> Effect Request
114111

115112
-- | Make a HTTP request using the specified options and response callback.
116-
request :: forall eff. Options RequestOptions -> (Response -> Eff (http :: HTTP | eff) Unit) -> Eff (http :: HTTP | eff) Request
113+
request :: Options RequestOptions -> (Response -> Effect Unit) -> Effect Request
117114
request = requestImpl <<< options
118115

119116
-- | Make a HTTP request from a URI string and response callback.
120-
requestFromURI :: forall eff. String -> (Response -> Eff (http :: HTTP | eff) Unit) -> Eff (http :: HTTP | eff) Request
121-
requestFromURI = requestImpl <<< toForeign <<< URL.parse
117+
requestFromURI :: String -> (Response -> Effect Unit) -> Effect Request
118+
requestFromURI = requestImpl <<< unsafeToForeign <<< URL.parse
122119

123120
-- | Create a writable stream from a request object.
124-
requestAsStream :: forall eff r. Request -> Writable r (http :: HTTP | eff)
121+
requestAsStream :: forall r. Request -> Writable r
125122
requestAsStream = unsafeCoerce
126123

127124
-- | Create a readable stream from a response object.
128-
responseAsStream :: forall eff w. Response -> Readable w (http :: HTTP | eff)
125+
responseAsStream :: forall w. Response -> Readable w
129126
responseAsStream = unsafeCoerce
130127

131128
-- | Set the socket timeout for a `Request`
132-
foreign import setTimeout :: forall eff. Request -> Int -> Eff (http :: HTTP | eff) Unit -> Eff (http :: HTTP | eff) Unit
129+
foreign import setTimeout :: Request -> Int -> Effect Unit -> Effect Unit
133130

134131
-- | Get the request HTTP version
135132
httpVersion :: Response -> String
136133
httpVersion = _.httpVersion <<< unsafeCoerce
137134

138-
headers' :: forall a. Response -> StrMap a
135+
headers' :: forall a. Response -> Object a
139136
headers' = _.headers <<< unsafeCoerce
140137

141138
-- | Get the response headers as a hash
142139
-- | Cookies are not included and could be retrieved with responseCookies
143-
responseHeaders :: Response -> StrMap String
144-
responseHeaders res = delete "set-cookie" $ headers' res
140+
responseHeaders :: Response -> Object String
141+
responseHeaders res = Object.delete "set-cookie" $ headers' res
145142

146143
-- | Get the response cookies as Just (Array String) or Nothing if no cookies
147144
responseCookies :: Response -> Maybe (Array String)
148-
responseCookies res = lookup "set-cookie" $ headers' res
145+
responseCookies res = Object.lookup "set-cookie" $ headers' res
149146

150147
-- | Get the response status code
151148
statusCode :: Response -> Int

src/Node/HTTP/Secure.purs

+8-10
Original file line numberDiff line numberDiff line change
@@ -80,28 +80,26 @@ module Node.HTTP.Secure
8080

8181
import Prelude
8282

83-
import Control.Monad.Eff (Eff)
8483
import Data.ArrayBuffer.Types (Uint8Array)
85-
import Data.Foreign (Foreign)
8684
import Data.Options (Options, Option, options, opt)
85+
import Effect (Effect)
86+
import Foreign (Foreign)
8787
import Node.Buffer (Buffer)
88-
import Node.HTTP (Request, Response, Server, HTTP)
88+
import Node.HTTP (Server, Request, Response)
8989
import Unsafe.Coerce (unsafeCoerce)
9090

9191
-- | Create an HTTPS server, given the SSL options and a function to be executed
9292
-- | when a request is received.
9393
foreign import createServerImpl ::
94-
forall eff.
9594
Foreign ->
96-
(Request -> Response -> Eff (http :: HTTP | eff) Unit) ->
97-
Eff (http :: HTTP | eff) Server
95+
(Request -> Response -> Effect Unit) ->
96+
Effect Server
9897

9998
-- | Create an HTTPS server, given the SSL options and a function to be executed
10099
-- | when a request is received.
101-
createServer :: forall eff.
102-
Options SSLOptions ->
103-
(Request -> Response -> Eff (http :: HTTP | eff) Unit) ->
104-
Eff (http :: HTTP | eff) Server
100+
createServer :: Options SSLOptions ->
101+
(Request -> Response -> Effect Unit) ->
102+
Effect Server
105103
createServer = createServerImpl <<< options
106104

107105
-- | The type of HTTPS server options

test/Main.purs

+13-16
Original file line numberDiff line numberDiff line change
@@ -2,32 +2,29 @@ module Test.Main where
22

33
import Prelude
44

5-
import Control.Monad.Eff (Eff)
6-
import Control.Monad.Eff.Console (CONSOLE, log, logShow)
7-
85
import Data.Foldable (foldMap)
96
import Data.Maybe (Maybe(..))
107
import Data.Options (Options, options, (:=))
11-
8+
import Effect (Effect)
9+
import Effect.Console (log, logShow)
1210
import Node.Encoding (Encoding(..))
13-
import Node.HTTP (HTTP, Request, Response, listen, createServer, setHeader, requestMethod, requestURL, responseAsStream, requestAsStream, setStatusCode)
11+
import Node.HTTP (Request, Response, listen, createServer, setHeader, requestMethod, requestURL, responseAsStream, requestAsStream, setStatusCode)
1412
import Node.HTTP.Client as Client
1513
import Node.HTTP.Secure as HTTPS
1614
import Node.Stream (Writable, end, pipe, writeString)
17-
1815
import Partial.Unsafe (unsafeCrashWith)
1916
import Unsafe.Coerce (unsafeCoerce)
2017

21-
foreign import stdout :: forall eff r. Writable r eff
18+
foreign import stdout :: forall r. Writable r
2219

23-
main :: forall eff. Eff (console :: CONSOLE, http :: HTTP | eff) Unit
20+
main :: Effect Unit
2421
main = do
2522
testBasic
2623
testHttpsServer
2724
testHttps
2825
testCookies
2926

30-
respond :: forall eff. Request -> Response -> Eff (console :: CONSOLE, http :: HTTP | eff) Unit
27+
respond :: Request -> Response -> Effect Unit
3128
respond req res = do
3229
setStatusCode res 200
3330
let inputStream = requestAsStream req
@@ -47,7 +44,7 @@ respond req res = do
4744
"POST" -> void $ pipe inputStream outputStream
4845
_ -> unsafeCrashWith "Unexpected HTTP method"
4946

50-
testBasic :: forall eff. Eff (console :: CONSOLE, http :: HTTP | eff) Unit
47+
testBasic :: Effect Unit
5148
testBasic = do
5249
server <- createServer respond
5350
listen server { hostname: "localhost", port: 8080, backlog: Nothing } $ void do
@@ -108,7 +105,7 @@ FO0u08Tb/091Bir5kgglUSi7VnFD3v8ffeKpkkJvtYUj7S9yoH9NQPVhKVCq6mna
108105
TbGfXbnVfNmqgQh71+k02p6S
109106
-----END PRIVATE KEY-----"""
110107

111-
testHttpsServer :: forall eff. Eff (console :: CONSOLE, http :: HTTP | eff) Unit
108+
testHttpsServer :: Effect Unit
112109
testHttpsServer = do
113110
server <- HTTPS.createServer sslOpts respond
114111
listen server { hostname: "localhost", port: 8081, backlog: Nothing } $ void do
@@ -125,30 +122,30 @@ testHttpsServer = do
125122
HTTPS.key := HTTPS.keyString mockKey <>
126123
HTTPS.cert := HTTPS.certString mockCert
127124

128-
testHttps :: forall eff. Eff (console :: CONSOLE, http :: HTTP | eff) Unit
125+
testHttps :: Effect Unit
129126
testHttps =
130127
simpleReq "https://pursuit.purescript.org/packages/purescript-node-http/badge"
131128

132-
testCookies :: forall eff. Eff (console :: CONSOLE, http :: HTTP | eff) Unit
129+
testCookies :: Effect Unit
133130
testCookies =
134131
simpleReq
135132
"https://httpbin.org/cookies/set?cookie1=firstcookie&cookie2=secondcookie"
136133

137-
simpleReq :: forall eff. String -> Eff (console :: CONSOLE, http :: HTTP | eff) Unit
134+
simpleReq :: String -> Effect Unit
138135
simpleReq uri = do
139136
log ("GET " <> uri <> ":")
140137
req <- Client.requestFromURI uri logResponse
141138
end (Client.requestAsStream req) (pure unit)
142139

143-
complexReq :: forall eff. Options Client.RequestOptions -> Eff (console :: CONSOLE, http :: HTTP | eff) Unit
140+
complexReq :: Options Client.RequestOptions -> Effect Unit
144141
complexReq opts = do
145142
log $ optsR.method <> " " <> optsR.protocol <> "//" <> optsR.hostname <> ":" <> optsR.port <> optsR.path <> ":"
146143
req <- Client.request opts logResponse
147144
end (Client.requestAsStream req) (pure unit)
148145
where
149146
optsR = unsafeCoerce $ options opts
150147

151-
logResponse :: forall eff. Client.Response -> Eff (console :: CONSOLE, http :: HTTP | eff) Unit
148+
logResponse :: Client.Response -> Effect Unit
152149
logResponse response = void do
153150
log "Headers:"
154151
logShow $ Client.responseHeaders response

0 commit comments

Comments
 (0)