Skip to content

Commit e6bfe5a

Browse files
author
Connor Prussin
committed
Fully type options
1 parent 60f9c5e commit e6bfe5a

File tree

3 files changed

+134
-30
lines changed

3 files changed

+134
-30
lines changed

bower.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
"purescript-node-url": "^3.0.0",
2121
"purescript-options": "^3.0.0",
2222
"purescript-unsafe-coerce": "^3.0.0",
23-
"purescript-node-buffer": "^3.0.1"
23+
"purescript-node-buffer": "^3.0.1",
24+
"purescript-arraybuffer-types": "^2.0.0"
2425
}
2526
}

src/Node/HTTP/HTTPS.purs

Lines changed: 127 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -7,19 +7,27 @@ module Node.HTTP.HTTPS
77
, handshakeTimeout
88
, requestCert
99
, rejectUnauthorized
10+
, NPNProtocols(..)
1011
, npnProtocols
12+
, ALPNProtocols(..)
1113
, alpnProtocols
1214
, sessionTimeout
1315
, ticketKeys
16+
, PFX(..)
1417
, pfx
18+
, Key(..)
1519
, key
1620
, passphrase
21+
, Cert(..)
1722
, cert
23+
, CA(..)
1824
, ca
25+
, CRL(..)
1926
, crl
2027
, ciphers
2128
, honorCipherOrder
2229
, ecdhCurve
30+
, DHParam(..)
2331
, dhparam
2432
, secureProtocol
2533
, secureOptions
@@ -29,8 +37,10 @@ module Node.HTTP.HTTPS
2937
import Prelude
3038

3139
import Control.Monad.Eff (Eff)
40+
import Data.ArrayBuffer.Types (Uint8Array)
41+
import Data.Foreign (Foreign, toForeign)
42+
import Data.Functor.Contravariant (cmap)
3243
import Data.Options (Options, Option, options, opt)
33-
import Data.Foreign (Foreign)
3444
import Node.Buffer (Buffer)
3545
import Node.HTTP (Request, Response, Server, HTTP)
3646

@@ -49,17 +59,47 @@ requestCert = opt "requestCert"
4959
rejectUnauthorized :: Option SSLOptions Boolean
5060
rejectUnauthorized = opt "rejectUnauthorized"
5161

52-
-- | The type variable t should be a string[], Buffer[], Uint8Array[], Buffer,
53-
-- | or Uint8Array.
62+
-- | The npnProtocols option can be a String, a Buffer, a Uint8Array, or an
63+
-- | array of any of those types.
64+
data NPNProtocols
65+
= NPNProtocolsString String
66+
| NPNProtocolsBuffer Buffer
67+
| NPNProtocolsUint8Array Uint8Array
68+
| NPNProtocolsStringArray (Array String)
69+
| NPNProtocolsBufferArray (Array Buffer)
70+
| NPNProtocolsUint8ArrayArray (Array Uint8Array)
71+
5472
-- | 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"
73+
npnProtocols :: Option SSLOptions NPNProtocols
74+
npnProtocols = cmap extract $ opt "NPNProtocols"
75+
where
76+
extract (NPNProtocolsString s) = toForeign s
77+
extract (NPNProtocolsBuffer b) = toForeign b
78+
extract (NPNProtocolsUint8Array u) = toForeign u
79+
extract (NPNProtocolsStringArray sa) = toForeign sa
80+
extract (NPNProtocolsBufferArray ba) = toForeign ba
81+
extract (NPNProtocolsUint8ArrayArray ua) = toForeign ua
82+
83+
-- | The alpnProtocols option can be a String, a Buffer, a Uint8Array, or an
84+
-- | array of any of those types.
85+
data ALPNProtocols
86+
= ALPNProtocolsString String
87+
| ALPNProtocolsBuffer Buffer
88+
| ALPNProtocolsUint8Array Uint8Array
89+
| ALPNProtocolsStringArray (Array String)
90+
| ALPNProtocolsBufferArray (Array Buffer)
91+
| ALPNProtocolsUint8ArrayArray (Array Uint8Array)
5792

58-
-- | The type variable t should be a string[], Buffer[], Uint8Array[], Buffer,
59-
-- | or Uint8Array.
6093
-- | 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"
94+
alpnProtocols :: Option SSLOptions ALPNProtocols
95+
alpnProtocols = cmap extract $ opt "ALPNProtocols"
96+
where
97+
extract (ALPNProtocolsString s) = toForeign s
98+
extract (ALPNProtocolsBuffer b) = toForeign b
99+
extract (ALPNProtocolsUint8Array u) = toForeign u
100+
extract (ALPNProtocolsStringArray sa) = toForeign sa
101+
extract (ALPNProtocolsBufferArray ba) = toForeign ba
102+
extract (ALPNProtocolsUint8ArrayArray ua) = toForeign ua
63103

64104
-- | See the [node docs](https://nodejs.org/api/tls.html#tls_tls_createserver_options_secureconnectionlistener)
65105
sessionTimeout :: Option SSLOptions Int
@@ -69,35 +109,87 @@ sessionTimeout = opt "sessionTimeout"
69109
ticketKeys :: Option SSLOptions Buffer
70110
ticketKeys = opt "ticketKeys"
71111

72-
-- | The type variable t should be a string or Buffer.
112+
-- | The PFX option can take either a String or a Buffer
113+
data PFX = PFXString String | PFXBuffer Buffer
114+
73115
-- | 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"
116+
pfx :: Option SSLOptions PFX
117+
pfx = cmap extract $ opt "pfx"
118+
where
119+
extract (PFXString s) = toForeign s
120+
extract (PFXBuffer b) = toForeign b
121+
122+
-- | The key option can be a String, a Buffer, an array of strings, or an array
123+
-- | of buffers.
124+
data Key
125+
= KeyString String
126+
| KeyBuffer Buffer
127+
| KeyStringArray (Array String)
128+
| KeyBufferArray (Array Buffer)
76129

77-
-- | The type variable t should be a string, string[], Buffer, Buffer[], or
78-
-- | Object[].
79130
-- | 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"
131+
key :: Option SSLOptions Key
132+
key = cmap extract $ opt "key"
133+
where
134+
extract (KeyString s) = toForeign s
135+
extract (KeyBuffer b) = toForeign b
136+
extract (KeyStringArray sa) = toForeign sa
137+
extract (KeyBufferArray ba) = toForeign ba
82138

83139
-- | See the [node docs](https://nodejs.org/api/tls.html#tls_tls_createsecurecontext_options)
84140
passphrase :: Option SSLOptions String
85141
passphrase = opt "passphrase"
86142

87-
-- | The type variable t should be a string, string[], Buffer, or Buffer[].
143+
-- | The cert option can be a String, a Buffer, an array of strings, or an array
144+
-- | of buffers.
145+
data Cert
146+
= CertString String
147+
| CertBuffer Buffer
148+
| CertStringArray (Array String)
149+
| CertBufferArray (Array Buffer)
150+
88151
-- | 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"
152+
cert :: Option SSLOptions Cert
153+
cert = cmap extract $ opt "cert"
154+
where
155+
extract (CertString s) = toForeign s
156+
extract (CertBuffer b) = toForeign b
157+
extract (CertStringArray sa) = toForeign sa
158+
extract (CertBufferArray ba) = toForeign ba
159+
160+
-- | The CA option can be a String, a Buffer, an array of strings, or an array
161+
-- | of buffers.
162+
data CA
163+
= CAString String
164+
| CABuffer Buffer
165+
| CAStringArray (Array String)
166+
| CABufferArray (Array Buffer)
91167

92-
-- | The type variable t should be a string, string[], Buffer, or Buffer[].
93168
-- | 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"
169+
ca :: Option SSLOptions CA
170+
ca = cmap extract $ opt "ca"
171+
where
172+
extract (CAString s) = toForeign s
173+
extract (CABuffer b) = toForeign b
174+
extract (CAStringArray sa) = toForeign sa
175+
extract (CABufferArray ba) = toForeign ba
176+
177+
-- | The CRL option can be a String, a Buffer, an array of strings, or an array
178+
-- | of buffers.
179+
data CRL
180+
= CRLString String
181+
| CRLBuffer Buffer
182+
| CRLStringArray (Array String)
183+
| CRLBufferArray (Array Buffer)
96184

97-
-- | The type variable t should be a string, string[], Buffer, or Buffer[].
98185
-- | 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"
186+
crl :: Option SSLOptions CRL
187+
crl = cmap extract $ opt "crl"
188+
where
189+
extract (CRLString s) = toForeign s
190+
extract (CRLBuffer b) = toForeign b
191+
extract (CRLStringArray sa) = toForeign sa
192+
extract (CRLBufferArray ba) = toForeign ba
101193

102194
-- | See the [node docs](https://nodejs.org/api/tls.html#tls_tls_createsecurecontext_options)
103195
ciphers :: Option SSLOptions String
@@ -111,10 +203,15 @@ honorCipherOrder = opt "honorCipherOrder"
111203
ecdhCurve :: Option SSLOptions String
112204
ecdhCurve = opt "ecdhCurve"
113205

114-
-- | The type variable t should be a string or Buffer.
206+
-- | The DHParam option can take either a String or a Buffer
207+
data DHParam = DHParamString String | DHParamBuffer Buffer
208+
115209
-- | 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"
210+
dhparam :: Option SSLOptions DHParam
211+
dhparam = cmap extract $ opt "dhparam"
212+
where
213+
extract (DHParamString s) = toForeign s
214+
extract (DHParamBuffer b) = toForeign b
118215

119216
-- | See the [node docs](https://nodejs.org/api/tls.html#tls_tls_createsecurecontext_options)
120217
secureProtocol :: Option SSLOptions String
@@ -128,6 +225,8 @@ secureOptions = opt "secureOptions"
128225
sessionIdContext :: Option SSLOptions String
129226
sessionIdContext = opt "sessionIdContext"
130227

228+
-- | Create an HTTPS server, given the SSL options and a function to be executed
229+
-- | when a request is received.
131230
foreign import createServerImpl ::
132231
forall eff.
133232
Foreign ->

test/Main.purs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ TbGfXbnVfNmqgQh71+k02p6S
110110

111111
testHttpsServer :: forall eff. Eff (console :: CONSOLE, http :: HTTP | eff) Unit
112112
testHttpsServer = do
113-
server <- HTTPS.createServer (HTTPS.key := mockKey <> HTTPS.cert := mockCert) respond
113+
server <- HTTPS.createServer sslOpts respond
114114
listen server { hostname: "localhost", port: 8081, backlog: Nothing } $ void do
115115
log "Listening on port 8081."
116116
complexReq $
@@ -120,6 +120,10 @@ testHttpsServer = do
120120
Client.port := 8081 <>
121121
Client.path := "/" <>
122122
Client.rejectUnauthorized := false
123+
where
124+
sslOpts =
125+
HTTPS.key := HTTPS.KeyString mockKey <>
126+
HTTPS.cert := HTTPS.CertString mockCert
123127

124128
testHttps :: forall eff. Eff (console :: CONSOLE, http :: HTTP | eff) Unit
125129
testHttps =

0 commit comments

Comments
 (0)