Skip to content

Commit 60f9c5e

Browse files
author
Connor Prussin
committed
Move SSL stuff to Node.HTTP.HTTPS
1 parent 5e01f06 commit 60f9c5e

File tree

5 files changed

+159
-147
lines changed

5 files changed

+159
-147
lines changed

src/Node/HTTP.js

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,6 @@
11
"use strict";
22

33
var http = require("http");
4-
var https = require("https");
5-
6-
exports.createServerSImpl = function (options) {
7-
return function (handleRequest) {
8-
return function () {
9-
return https.createServer(options, function (req, res) {
10-
handleRequest(req)(res)();
11-
});
12-
};
13-
};
14-
};
154

165
exports.createServer = function (handleRequest) {
176
return function () {

src/Node/HTTP.purs

Lines changed: 0 additions & 134 deletions
Original file line numberDiff line numberDiff line change
@@ -6,29 +6,6 @@ module Node.HTTP
66
, Response
77
, HTTP
88

9-
, createServerS
10-
, SSLOptions
11-
, handshakeTimeout
12-
, requestCert
13-
, rejectUnauthorized
14-
, npnProtocols
15-
, alpnProtocols
16-
, sessionTimeout
17-
, ticketKeys
18-
, pfx
19-
, key
20-
, passphrase
21-
, cert
22-
, ca
23-
, crl
24-
, ciphers
25-
, honorCipherOrder
26-
, ecdhCurve
27-
, dhparam
28-
, secureProtocol
29-
, secureOptions
30-
, sessionIdContext
31-
329
, createServer
3310
, listen
3411
, ListenOptions
@@ -51,13 +28,10 @@ import Prelude
5128

5229
import Control.Monad.Eff (Eff, kind Effect)
5330

54-
import Data.Foreign (Foreign)
5531
import Data.Maybe (Maybe)
5632
import Data.Nullable (Nullable, toNullable)
57-
import Data.Options (Options, Option, options, opt)
5833
import Data.StrMap (StrMap)
5934

60-
import Node.Buffer (Buffer)
6135
import Node.Stream (Writable, Readable)
6236

6337
import Unsafe.Coerce (unsafeCoerce)
@@ -77,114 +51,6 @@ foreign import data HTTP :: Effect
7751
-- | Create a HTTP server, given a function to be executed when a request is received.
7852
foreign import createServer :: forall eff. (Request -> Response -> Eff (http :: HTTP | eff) Unit) -> Eff (http :: HTTP | eff) Server
7953

80-
-- | The type of HTTPS server options
81-
data SSLOptions
82-
83-
-- | See the [node docs](https://nodejs.org/api/tls.html#tls_tls_createserver_options_secureconnectionlistener)
84-
handshakeTimeout :: Option SSLOptions Int
85-
handshakeTimeout = opt "handshakeTimeout"
86-
87-
-- | See the [node docs](https://nodejs.org/api/tls.html#tls_tls_createserver_options_secureconnectionlistener)
88-
requestCert :: Option SSLOptions Boolean
89-
requestCert = opt "requestCert"
90-
91-
-- | See the [node docs](https://nodejs.org/api/tls.html#tls_tls_createserver_options_secureconnectionlistener)
92-
rejectUnauthorized :: Option SSLOptions Boolean
93-
rejectUnauthorized = opt "rejectUnauthorized"
94-
95-
-- | The type variable t should be a string[], Buffer[], Uint8Array[], Buffer,
96-
-- | or Uint8Array.
97-
-- | See the [node docs](https://nodejs.org/api/tls.html#tls_tls_createserver_options_secureconnectionlistener)
98-
npnProtocols :: forall t. Option SSLOptions t
99-
npnProtocols = opt "NPNProtocols"
100-
101-
-- | The type variable t should be a string[], Buffer[], Uint8Array[], Buffer,
102-
-- | or Uint8Array.
103-
-- | See the [node docs](https://nodejs.org/api/tls.html#tls_tls_createserver_options_secureconnectionlistener)
104-
alpnProtocols :: forall t. Option SSLOptions t
105-
alpnProtocols = opt "ALPNProtocols"
106-
107-
-- | See the [node docs](https://nodejs.org/api/tls.html#tls_tls_createserver_options_secureconnectionlistener)
108-
sessionTimeout :: Option SSLOptions Int
109-
sessionTimeout = opt "sessionTimeout"
110-
111-
-- | See the [node docs](https://nodejs.org/api/tls.html#tls_tls_createserver_options_secureconnectionlistener)
112-
ticketKeys :: Option SSLOptions Buffer
113-
ticketKeys = opt "ticketKeys"
114-
115-
-- | The type variable t should be a string or Buffer.
116-
-- | See the [node docs](https://nodejs.org/api/tls.html#tls_tls_createsecurecontext_options)
117-
pfx :: forall t. Option SSLOptions t
118-
pfx = opt "pfx"
119-
120-
-- | The type variable t should be a string, string[], Buffer, Buffer[], or
121-
-- | Object[].
122-
-- | See the [node docs](https://nodejs.org/api/tls.html#tls_tls_createsecurecontext_options)
123-
key :: forall t. Option SSLOptions t
124-
key = opt "key"
125-
126-
-- | See the [node docs](https://nodejs.org/api/tls.html#tls_tls_createsecurecontext_options)
127-
passphrase :: Option SSLOptions String
128-
passphrase = opt "passphrase"
129-
130-
-- | The type variable t should be a string, string[], Buffer, or Buffer[].
131-
-- | See the [node docs](https://nodejs.org/api/tls.html#tls_tls_createsecurecontext_options)
132-
cert :: forall t. Option SSLOptions t
133-
cert = opt "cert"
134-
135-
-- | The type variable t should be a string, string[], Buffer, or Buffer[].
136-
-- | See the [node docs](https://nodejs.org/api/tls.html#tls_tls_createsecurecontext_options)
137-
ca :: forall t. Option SSLOptions t
138-
ca = opt "ca"
139-
140-
-- | The type variable t should be a string, string[], Buffer, or Buffer[].
141-
-- | See the [node docs](https://nodejs.org/api/tls.html#tls_tls_createsecurecontext_options)
142-
crl :: forall t. Option SSLOptions t
143-
crl = opt "crl"
144-
145-
-- | See the [node docs](https://nodejs.org/api/tls.html#tls_tls_createsecurecontext_options)
146-
ciphers :: Option SSLOptions String
147-
ciphers = opt "ciphers"
148-
149-
-- | See the [node docs](https://nodejs.org/api/tls.html#tls_tls_createsecurecontext_options)
150-
honorCipherOrder :: Option SSLOptions Boolean
151-
honorCipherOrder = opt "honorCipherOrder"
152-
153-
-- | See the [node docs](https://nodejs.org/api/tls.html#tls_tls_createsecurecontext_options)
154-
ecdhCurve :: Option SSLOptions String
155-
ecdhCurve = opt "ecdhCurve"
156-
157-
-- | The type variable t should be a string or Buffer.
158-
-- | See the [node docs](https://nodejs.org/api/tls.html#tls_tls_createsecurecontext_options)
159-
dhparam :: forall t. Option SSLOptions t
160-
dhparam = opt "dhparam"
161-
162-
-- | See the [node docs](https://nodejs.org/api/tls.html#tls_tls_createsecurecontext_options)
163-
secureProtocol :: Option SSLOptions String
164-
secureProtocol = opt "secureProtocol"
165-
166-
-- | See the [node docs](https://nodejs.org/api/tls.html#tls_tls_createsecurecontext_options)
167-
secureOptions :: Option SSLOptions Int
168-
secureOptions = opt "secureOptions"
169-
170-
-- | See the [node docs](https://nodejs.org/api/tls.html#tls_tls_createsecurecontext_options)
171-
sessionIdContext :: Option SSLOptions String
172-
sessionIdContext = opt "sessionIdContext"
173-
174-
foreign import createServerSImpl ::
175-
forall eff.
176-
Foreign ->
177-
(Request -> Response -> Eff (http :: HTTP | eff) Unit) ->
178-
Eff (http :: HTTP | eff) Server
179-
180-
-- | Create an HTTPS server, given the SSL options and a function to be executed
181-
-- | when a request is received.
182-
createServerS :: forall eff.
183-
Options SSLOptions ->
184-
(Request -> Response -> Eff (http :: HTTP | eff) Unit) ->
185-
Eff (http :: HTTP | eff) Server
186-
createServerS = createServerSImpl <<< options
187-
18854
foreign import listenImpl :: forall eff. Server -> Int -> String -> Nullable Int -> Eff (http :: HTTP | eff) Unit -> Eff (http :: HTTP | eff) Unit
18955

19056
-- | Listen on a port in order to start accepting HTTP requests. The specified callback will be run when setup is complete.

src/Node/HTTP/HTTPS.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
"use strict";
2+
3+
var https = require("https");
4+
5+
exports.createServerImpl = function (options) {
6+
return function (handleRequest) {
7+
return function () {
8+
return https.createServer(options, function (req, res) {
9+
handleRequest(req)(res)();
10+
});
11+
};
12+
};
13+
};

src/Node/HTTP/HTTPS.purs

Lines changed: 143 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,143 @@
1+
-- | This module defines low-level bindings to the Node HTTPS module.
2+
3+
module Node.HTTP.HTTPS
4+
( createServer
5+
6+
, SSLOptions
7+
, handshakeTimeout
8+
, requestCert
9+
, rejectUnauthorized
10+
, npnProtocols
11+
, alpnProtocols
12+
, sessionTimeout
13+
, ticketKeys
14+
, pfx
15+
, key
16+
, passphrase
17+
, cert
18+
, ca
19+
, crl
20+
, ciphers
21+
, honorCipherOrder
22+
, ecdhCurve
23+
, dhparam
24+
, secureProtocol
25+
, secureOptions
26+
, sessionIdContext
27+
) where
28+
29+
import Prelude
30+
31+
import Control.Monad.Eff (Eff)
32+
import Data.Options (Options, Option, options, opt)
33+
import Data.Foreign (Foreign)
34+
import Node.Buffer (Buffer)
35+
import Node.HTTP (Request, Response, Server, HTTP)
36+
37+
-- | The type of HTTPS server options
38+
data SSLOptions
39+
40+
-- | See the [node docs](https://nodejs.org/api/tls.html#tls_tls_createserver_options_secureconnectionlistener)
41+
handshakeTimeout :: Option SSLOptions Int
42+
handshakeTimeout = opt "handshakeTimeout"
43+
44+
-- | See the [node docs](https://nodejs.org/api/tls.html#tls_tls_createserver_options_secureconnectionlistener)
45+
requestCert :: Option SSLOptions Boolean
46+
requestCert = opt "requestCert"
47+
48+
-- | See the [node docs](https://nodejs.org/api/tls.html#tls_tls_createserver_options_secureconnectionlistener)
49+
rejectUnauthorized :: Option SSLOptions Boolean
50+
rejectUnauthorized = opt "rejectUnauthorized"
51+
52+
-- | The type variable t should be a string[], Buffer[], Uint8Array[], Buffer,
53+
-- | or Uint8Array.
54+
-- | See the [node docs](https://nodejs.org/api/tls.html#tls_tls_createserver_options_secureconnectionlistener)
55+
npnProtocols :: forall t. Option SSLOptions t
56+
npnProtocols = opt "NPNProtocols"
57+
58+
-- | The type variable t should be a string[], Buffer[], Uint8Array[], Buffer,
59+
-- | or Uint8Array.
60+
-- | See the [node docs](https://nodejs.org/api/tls.html#tls_tls_createserver_options_secureconnectionlistener)
61+
alpnProtocols :: forall t. Option SSLOptions t
62+
alpnProtocols = opt "ALPNProtocols"
63+
64+
-- | See the [node docs](https://nodejs.org/api/tls.html#tls_tls_createserver_options_secureconnectionlistener)
65+
sessionTimeout :: Option SSLOptions Int
66+
sessionTimeout = opt "sessionTimeout"
67+
68+
-- | See the [node docs](https://nodejs.org/api/tls.html#tls_tls_createserver_options_secureconnectionlistener)
69+
ticketKeys :: Option SSLOptions Buffer
70+
ticketKeys = opt "ticketKeys"
71+
72+
-- | The type variable t should be a string or Buffer.
73+
-- | See the [node docs](https://nodejs.org/api/tls.html#tls_tls_createsecurecontext_options)
74+
pfx :: forall t. Option SSLOptions t
75+
pfx = opt "pfx"
76+
77+
-- | The type variable t should be a string, string[], Buffer, Buffer[], or
78+
-- | Object[].
79+
-- | See the [node docs](https://nodejs.org/api/tls.html#tls_tls_createsecurecontext_options)
80+
key :: forall t. Option SSLOptions t
81+
key = opt "key"
82+
83+
-- | See the [node docs](https://nodejs.org/api/tls.html#tls_tls_createsecurecontext_options)
84+
passphrase :: Option SSLOptions String
85+
passphrase = opt "passphrase"
86+
87+
-- | The type variable t should be a string, string[], Buffer, or Buffer[].
88+
-- | See the [node docs](https://nodejs.org/api/tls.html#tls_tls_createsecurecontext_options)
89+
cert :: forall t. Option SSLOptions t
90+
cert = opt "cert"
91+
92+
-- | The type variable t should be a string, string[], Buffer, or Buffer[].
93+
-- | See the [node docs](https://nodejs.org/api/tls.html#tls_tls_createsecurecontext_options)
94+
ca :: forall t. Option SSLOptions t
95+
ca = opt "ca"
96+
97+
-- | The type variable t should be a string, string[], Buffer, or Buffer[].
98+
-- | See the [node docs](https://nodejs.org/api/tls.html#tls_tls_createsecurecontext_options)
99+
crl :: forall t. Option SSLOptions t
100+
crl = opt "crl"
101+
102+
-- | See the [node docs](https://nodejs.org/api/tls.html#tls_tls_createsecurecontext_options)
103+
ciphers :: Option SSLOptions String
104+
ciphers = opt "ciphers"
105+
106+
-- | See the [node docs](https://nodejs.org/api/tls.html#tls_tls_createsecurecontext_options)
107+
honorCipherOrder :: Option SSLOptions Boolean
108+
honorCipherOrder = opt "honorCipherOrder"
109+
110+
-- | See the [node docs](https://nodejs.org/api/tls.html#tls_tls_createsecurecontext_options)
111+
ecdhCurve :: Option SSLOptions String
112+
ecdhCurve = opt "ecdhCurve"
113+
114+
-- | The type variable t should be a string or Buffer.
115+
-- | See the [node docs](https://nodejs.org/api/tls.html#tls_tls_createsecurecontext_options)
116+
dhparam :: forall t. Option SSLOptions t
117+
dhparam = opt "dhparam"
118+
119+
-- | See the [node docs](https://nodejs.org/api/tls.html#tls_tls_createsecurecontext_options)
120+
secureProtocol :: Option SSLOptions String
121+
secureProtocol = opt "secureProtocol"
122+
123+
-- | See the [node docs](https://nodejs.org/api/tls.html#tls_tls_createsecurecontext_options)
124+
secureOptions :: Option SSLOptions Int
125+
secureOptions = opt "secureOptions"
126+
127+
-- | See the [node docs](https://nodejs.org/api/tls.html#tls_tls_createsecurecontext_options)
128+
sessionIdContext :: Option SSLOptions String
129+
sessionIdContext = opt "sessionIdContext"
130+
131+
foreign import createServerImpl ::
132+
forall eff.
133+
Foreign ->
134+
(Request -> Response -> Eff (http :: HTTP | eff) Unit) ->
135+
Eff (http :: HTTP | eff) Server
136+
137+
-- | Create an HTTPS server, given the SSL options and a function to be executed
138+
-- | when a request is received.
139+
createServer :: forall eff.
140+
Options SSLOptions ->
141+
(Request -> Response -> Eff (http :: HTTP | eff) Unit) ->
142+
Eff (http :: HTTP | eff) Server
143+
createServer = createServerImpl <<< options

test/Main.purs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,9 @@ import Data.Maybe (Maybe(..))
1010
import Data.Options (Options, (:=))
1111

1212
import Node.Encoding (Encoding(..))
13-
import Node.HTTP (HTTP, Request, Response, listen, createServer, createServerS, key, cert, setHeader, requestMethod, requestURL, responseAsStream, requestAsStream, setStatusCode)
13+
import Node.HTTP (HTTP, Request, Response, listen, createServer, setHeader, requestMethod, requestURL, responseAsStream, requestAsStream, setStatusCode)
1414
import Node.HTTP.Client as Client
15+
import Node.HTTP.HTTPS as HTTPS
1516
import Node.Stream (Writable, end, pipe, writeString)
1617

1718
import Partial.Unsafe (unsafeCrashWith)
@@ -109,7 +110,7 @@ TbGfXbnVfNmqgQh71+k02p6S
109110

110111
testHttpsServer :: forall eff. Eff (console :: CONSOLE, http :: HTTP | eff) Unit
111112
testHttpsServer = do
112-
server <- createServerS (key := mockKey <> cert := mockCert) respond
113+
server <- HTTPS.createServer (HTTPS.key := mockKey <> HTTPS.cert := mockCert) respond
113114
listen server { hostname: "localhost", port: 8081, backlog: Nothing } $ void do
114115
log "Listening on port 8081."
115116
complexReq $

0 commit comments

Comments
 (0)