Skip to content

Commit e9ce94f

Browse files
author
Connor Prussin
committed
Use functions to map each possible argument type
1 parent a44ab69 commit e9ce94f

File tree

2 files changed

+130
-90
lines changed

2 files changed

+130
-90
lines changed

src/Node/HTTP/Secure.purs

+128-88
Original file line numberDiff line numberDiff line change
@@ -7,28 +7,72 @@ module Node.HTTP.Secure
77
, handshakeTimeout
88
, requestCert
99
, rejectUnauthorized
10-
, NPNProtocols(..)
10+
11+
, NPNProtocols
12+
, npnProtocolsString
13+
, npnProtocolsBuffer
14+
, npnProtocolsUint8Array
15+
, npnProtocolsStringArray
16+
, npnProtocolsBufferArray
17+
, npnProtocolsUint8ArrayArray
1118
, npnProtocols
12-
, ALPNProtocols(..)
19+
20+
, ALPNProtocols
21+
, alpnProtocolsString
22+
, alpnProtocolsBuffer
23+
, alpnProtocolsUint8Array
24+
, alpnProtocolsStringArray
25+
, alpnProtocolsBufferArray
26+
, alpnProtocolsUint8ArrayArray
1327
, alpnProtocols
28+
1429
, sessionTimeout
1530
, ticketKeys
16-
, PFX(..)
31+
32+
, PFX
33+
, pfxString
34+
, pfxBuffer
1735
, pfx
18-
, Key(..)
36+
37+
, Key
38+
, keyString
39+
, keyBuffer
40+
, keyStringArray
41+
, keyBufferArray
1942
, key
43+
2044
, passphrase
21-
, Cert(..)
45+
46+
, Cert
47+
, certString
48+
, certBuffer
49+
, certStringArray
50+
, certBufferArray
2251
, cert
23-
, CA(..)
52+
53+
, CA
54+
, caString
55+
, caBuffer
56+
, caStringArray
57+
, caBufferArray
2458
, ca
25-
, CRL(..)
59+
60+
, CRL
61+
, crlString
62+
, crlBuffer
63+
, crlStringArray
64+
, crlBufferArray
2665
, crl
66+
2767
, ciphers
2868
, honorCipherOrder
2969
, ecdhCurve
30-
, DHParam(..)
70+
71+
, DHParam
72+
, dhparamString
73+
, dhparamBuffer
3174
, dhparam
75+
3276
, secureProtocol
3377
, secureOptions
3478
, sessionIdContext
@@ -38,11 +82,11 @@ import Prelude
3882

3983
import Control.Monad.Eff (Eff)
4084
import Data.ArrayBuffer.Types (Uint8Array)
41-
import Data.Foreign (Foreign, toForeign)
42-
import Data.Functor.Contravariant (cmap)
85+
import Data.Foreign (Foreign)
4386
import Data.Options (Options, Option, options, opt)
4487
import Node.Buffer (Buffer)
4588
import Node.HTTP (Request, Response, Server, HTTP)
89+
import Unsafe.Coerce (unsafeCoerce)
4690

4791
-- | The type of HTTPS server options
4892
data SSLOptions
@@ -62,44 +106,42 @@ rejectUnauthorized = opt "rejectUnauthorized"
62106
-- | The npnProtocols option can be a String, a Buffer, a Uint8Array, or an
63107
-- | array of any of those types.
64108
data NPNProtocols
65-
= NPNProtocolsString String
66-
| NPNProtocolsBuffer Buffer
67-
| NPNProtocolsUint8Array Uint8Array
68-
| NPNProtocolsStringArray (Array String)
69-
| NPNProtocolsBufferArray (Array Buffer)
70-
| NPNProtocolsUint8ArrayArray (Array Uint8Array)
109+
npnProtocolsString :: String -> NPNProtocols
110+
npnProtocolsString = unsafeCoerce
111+
npnProtocolsBuffer :: Buffer -> NPNProtocols
112+
npnProtocolsBuffer = unsafeCoerce
113+
npnProtocolsUint8Array :: Uint8Array -> NPNProtocols
114+
npnProtocolsUint8Array = unsafeCoerce
115+
npnProtocolsStringArray :: Array String -> NPNProtocols
116+
npnProtocolsStringArray = unsafeCoerce
117+
npnProtocolsBufferArray :: Array Buffer -> NPNProtocols
118+
npnProtocolsBufferArray = unsafeCoerce
119+
npnProtocolsUint8ArrayArray :: Array Uint8Array -> NPNProtocols
120+
npnProtocolsUint8ArrayArray = unsafeCoerce
71121

72122
-- | See the [node docs](https://nodejs.org/api/tls.html#tls_tls_createserver_options_secureconnectionlistener)
73123
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
124+
npnProtocols = opt "NPNProtocols"
82125

83126
-- | The alpnProtocols option can be a String, a Buffer, a Uint8Array, or an
84127
-- | array of any of those types.
85128
data ALPNProtocols
86-
= ALPNProtocolsString String
87-
| ALPNProtocolsBuffer Buffer
88-
| ALPNProtocolsUint8Array Uint8Array
89-
| ALPNProtocolsStringArray (Array String)
90-
| ALPNProtocolsBufferArray (Array Buffer)
91-
| ALPNProtocolsUint8ArrayArray (Array Uint8Array)
129+
alpnProtocolsString :: String -> ALPNProtocols
130+
alpnProtocolsString = unsafeCoerce
131+
alpnProtocolsBuffer :: Buffer -> ALPNProtocols
132+
alpnProtocolsBuffer = unsafeCoerce
133+
alpnProtocolsUint8Array :: Uint8Array -> ALPNProtocols
134+
alpnProtocolsUint8Array = unsafeCoerce
135+
alpnProtocolsStringArray :: Array String -> ALPNProtocols
136+
alpnProtocolsStringArray = unsafeCoerce
137+
alpnProtocolsBufferArray :: Array Buffer -> ALPNProtocols
138+
alpnProtocolsBufferArray = unsafeCoerce
139+
alpnProtocolsUint8ArrayArray :: Array Uint8Array -> ALPNProtocols
140+
alpnProtocolsUint8ArrayArray = unsafeCoerce
92141

93142
-- | See the [node docs](https://nodejs.org/api/tls.html#tls_tls_createserver_options_secureconnectionlistener)
94143
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
144+
alpnProtocols = opt "ALPNProtocols"
103145

104146
-- | See the [node docs](https://nodejs.org/api/tls.html#tls_tls_createserver_options_secureconnectionlistener)
105147
sessionTimeout :: Option SSLOptions Int
@@ -110,31 +152,31 @@ ticketKeys :: Option SSLOptions Buffer
110152
ticketKeys = opt "ticketKeys"
111153

112154
-- | The PFX option can take either a String or a Buffer
113-
data PFX = PFXString String | PFXBuffer Buffer
155+
data PFX
156+
pfxString :: String -> PFX
157+
pfxString = unsafeCoerce
158+
pfxBuffer :: Buffer -> PFX
159+
pfxBuffer = unsafeCoerce
114160

115161
-- | See the [node docs](https://nodejs.org/api/tls.html#tls_tls_createsecurecontext_options)
116162
pfx :: Option SSLOptions PFX
117-
pfx = cmap extract $ opt "pfx"
118-
where
119-
extract (PFXString s) = toForeign s
120-
extract (PFXBuffer b) = toForeign b
163+
pfx = opt "pfx"
121164

122165
-- | The key option can be a String, a Buffer, an array of strings, or an array
123166
-- | of buffers.
124167
data Key
125-
= KeyString String
126-
| KeyBuffer Buffer
127-
| KeyStringArray (Array String)
128-
| KeyBufferArray (Array Buffer)
168+
keyString :: String -> Key
169+
keyString = unsafeCoerce
170+
keyBuffer :: Buffer -> Key
171+
keyBuffer = unsafeCoerce
172+
keyStringArray :: Array String -> Key
173+
keyStringArray = unsafeCoerce
174+
keyBufferArray :: Array Buffer -> Key
175+
keyBufferArray = unsafeCoerce
129176

130177
-- | See the [node docs](https://nodejs.org/api/tls.html#tls_tls_createsecurecontext_options)
131178
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
179+
key = opt "key"
138180

139181
-- | See the [node docs](https://nodejs.org/api/tls.html#tls_tls_createsecurecontext_options)
140182
passphrase :: Option SSLOptions String
@@ -143,53 +185,50 @@ passphrase = opt "passphrase"
143185
-- | The cert option can be a String, a Buffer, an array of strings, or an array
144186
-- | of buffers.
145187
data Cert
146-
= CertString String
147-
| CertBuffer Buffer
148-
| CertStringArray (Array String)
149-
| CertBufferArray (Array Buffer)
188+
certString :: String -> Cert
189+
certString = unsafeCoerce
190+
certBuffer :: Buffer -> Cert
191+
certBuffer = unsafeCoerce
192+
certStringArray :: Array String -> Cert
193+
certStringArray = unsafeCoerce
194+
certBufferArray :: Array Buffer -> Cert
195+
certBufferArray = unsafeCoerce
150196

151197
-- | See the [node docs](https://nodejs.org/api/tls.html#tls_tls_createsecurecontext_options)
152198
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
199+
cert = opt "cert"
159200

160201
-- | The CA option can be a String, a Buffer, an array of strings, or an array
161202
-- | of buffers.
162203
data CA
163-
= CAString String
164-
| CABuffer Buffer
165-
| CAStringArray (Array String)
166-
| CABufferArray (Array Buffer)
204+
caString :: String -> CA
205+
caString = unsafeCoerce
206+
caBuffer :: Buffer -> CA
207+
caBuffer = unsafeCoerce
208+
caStringArray :: Array String -> CA
209+
caStringArray = unsafeCoerce
210+
caBufferArray :: Array Buffer -> CA
211+
caBufferArray = unsafeCoerce
167212

168213
-- | See the [node docs](https://nodejs.org/api/tls.html#tls_tls_createsecurecontext_options)
169214
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
215+
ca = opt "ca"
176216

177217
-- | The CRL option can be a String, a Buffer, an array of strings, or an array
178218
-- | of buffers.
179219
data CRL
180-
= CRLString String
181-
| CRLBuffer Buffer
182-
| CRLStringArray (Array String)
183-
| CRLBufferArray (Array Buffer)
220+
crlString :: String -> CRL
221+
crlString = unsafeCoerce
222+
crlBuffer :: Buffer -> CRL
223+
crlBuffer = unsafeCoerce
224+
crlStringArray :: Array String -> CRL
225+
crlStringArray = unsafeCoerce
226+
crlBufferArray :: Array Buffer -> CRL
227+
crlBufferArray = unsafeCoerce
184228

185229
-- | See the [node docs](https://nodejs.org/api/tls.html#tls_tls_createsecurecontext_options)
186230
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
231+
crl = opt "crl"
193232

194233
-- | See the [node docs](https://nodejs.org/api/tls.html#tls_tls_createsecurecontext_options)
195234
ciphers :: Option SSLOptions String
@@ -204,14 +243,15 @@ ecdhCurve :: Option SSLOptions String
204243
ecdhCurve = opt "ecdhCurve"
205244

206245
-- | The DHParam option can take either a String or a Buffer
207-
data DHParam = DHParamString String | DHParamBuffer Buffer
246+
data DHParam
247+
dhparamString :: String -> DHParam
248+
dhparamString = unsafeCoerce
249+
dhparamBuffer :: Buffer -> DHParam
250+
dhparamBuffer = unsafeCoerce
208251

209252
-- | See the [node docs](https://nodejs.org/api/tls.html#tls_tls_createsecurecontext_options)
210253
dhparam :: Option SSLOptions DHParam
211-
dhparam = cmap extract $ opt "dhparam"
212-
where
213-
extract (DHParamString s) = toForeign s
214-
extract (DHParamBuffer b) = toForeign b
254+
dhparam = opt "dhparam"
215255

216256
-- | See the [node docs](https://nodejs.org/api/tls.html#tls_tls_createsecurecontext_options)
217257
secureProtocol :: Option SSLOptions String

test/Main.purs

+2-2
Original file line numberDiff line numberDiff line change
@@ -122,8 +122,8 @@ testHttpsServer = do
122122
Client.rejectUnauthorized := false
123123
where
124124
sslOpts =
125-
HTTPS.key := HTTPS.KeyString mockKey <>
126-
HTTPS.cert := HTTPS.CertString mockCert
125+
HTTPS.key := HTTPS.keyString mockKey <>
126+
HTTPS.cert := HTTPS.certString mockCert
127127

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

0 commit comments

Comments
 (0)