Skip to content

Commit c83c7af

Browse files
Drop fromMaybe undefined approach to handling options
1 parent 4f1a7d6 commit c83c7af

File tree

3 files changed

+82
-314
lines changed

3 files changed

+82
-314
lines changed

src/Node/Http2/Client.purs

Lines changed: 7 additions & 120 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,12 @@ module Node.Http2.Client
44
, connect'
55
) where
66

7-
import Data.Maybe (Maybe(..), fromMaybe)
87
import Effect (Effect)
98
import Effect.Uncurried (EffectFn1, EffectFn2, runEffectFn1, runEffectFn2)
109
import Node.Buffer.Immutable (ImmutableBuffer)
1110
import Node.Http2.Types (Client, Http2Session, Settings, TlsSecureContextOptions)
1211
import Node.Net.Socket (Socket)
12+
import Prim.Row as Row
1313
import Type.Row (type (+))
1414

1515
connect :: String -> Effect (Http2Session Client)
@@ -86,127 +86,14 @@ type TcpConnectOptions f r =
8686
)
8787

8888
connect'
89-
:: String
90-
-> ({ | ConnectOptions Maybe + TlsConnectOptions Maybe + TlsSecureContextOptions Maybe + TcpConnectOptions Maybe + () } -> { | ConnectOptions Maybe + TlsConnectOptions Maybe + TlsSecureContextOptions Maybe + TcpConnectOptions Maybe + () })
89+
:: forall rec trash
90+
. Row.Union rec trash (ConnectOptions Unlift + TlsConnectOptions Unlift + TlsSecureContextOptions Unlift + TcpConnectOptions Unlift + ())
91+
=> String
92+
-> { | rec }
9193
-> Effect (Http2Session Client)
92-
connect' authority buildOptions = do
93-
let
94-
o = buildOptions
95-
{ maxDeflateDynamicTableSize: Nothing
96-
, maxSettings: Nothing
97-
, maxSessionMemory: Nothing
98-
, maxHeaderListPairs: Nothing
99-
, maxOutstandingPings: Nothing
100-
, maxReservedRemoteStreams: Nothing
101-
, maxSendHeaderBlockLength: Nothing
102-
, paddingStrategy: Nothing
103-
, peerMaxConcurrentStreams: Nothing
104-
, protocol: Nothing
105-
, settings: Nothing
106-
, unknownProtocolTimeout: Nothing
107-
-- TlsConnect
108-
, enableTrace: Nothing
109-
, socket: Nothing
110-
, allowHalfOpen: Nothing
111-
, rejectUnauthorized: Nothing
112-
, "ALPNProtocols": Nothing
113-
, servername: Nothing
114-
, session: Nothing
115-
, minDHSize: Nothing
116-
, highWaterMark: Nothing
117-
-- TlsSecureContext
118-
, ca: Nothing
119-
, cert: Nothing
120-
, sigalgs: Nothing
121-
, ciphers: Nothing
122-
, clientCertEngine: Nothing
123-
, crl: Nothing
124-
, dhparam: Nothing
125-
, ecdhCurve: Nothing
126-
, honorCipherOrder: Nothing
127-
, key: Nothing
128-
, privateKeyEngine: Nothing
129-
, privateKeyIdentifier: Nothing
130-
, maxVersion: Nothing
131-
, minVersion: Nothing
132-
, passphrase: Nothing
133-
, pfx: Nothing
134-
, secureOptions: Nothing
135-
, secureProtocol: Nothing
136-
, sessionIdContext: Nothing
137-
, ticketKeys: Nothing
138-
, sessionTimeout: Nothing
139-
-- TcpConnect
140-
, port: Nothing
141-
, host: Nothing
142-
, localAddress: Nothing
143-
, localPort: Nothing
144-
, family: Nothing
145-
, noDelay: Nothing
146-
, keepAlive: Nothing
147-
, keepAliveInitialDelay: Nothing
148-
}
94+
connect' authority rec = runEffectFn2 connectAuthOptionsImpl authority rec
14995

150-
finalOptions :: { | ConnectOptions Unlift + TlsConnectOptions Unlift + TlsSecureContextOptions Unlift + TcpConnectOptions Unlift + () }
151-
finalOptions =
152-
{ maxDeflateDynamicTableSize: fromMaybe undefined o.maxDeflateDynamicTableSize
153-
, maxSettings: fromMaybe undefined o.maxSettings
154-
, maxSessionMemory: fromMaybe undefined o.maxSessionMemory
155-
, maxHeaderListPairs: fromMaybe undefined o.maxHeaderListPairs
156-
, maxOutstandingPings: fromMaybe undefined o.maxOutstandingPings
157-
, maxReservedRemoteStreams: fromMaybe undefined o.maxReservedRemoteStreams
158-
, maxSendHeaderBlockLength: fromMaybe undefined o.maxSendHeaderBlockLength
159-
, paddingStrategy: fromMaybe undefined o.paddingStrategy
160-
, peerMaxConcurrentStreams: fromMaybe undefined o.peerMaxConcurrentStreams
161-
, protocol: fromMaybe undefined o.protocol
162-
, settings: fromMaybe undefined o.settings
163-
, unknownProtocolTimeout: fromMaybe undefined o.unknownProtocolTimeout
164-
-- TlsConnect
165-
, enableTrace: fromMaybe undefined o.enableTrace
166-
, socket: fromMaybe undefined o.socket
167-
, allowHalfOpen: fromMaybe undefined o.allowHalfOpen
168-
, rejectUnauthorized: fromMaybe undefined o.rejectUnauthorized
169-
, "ALPNProtocols": fromMaybe undefined o."ALPNProtocols"
170-
, servername: fromMaybe undefined o.servername
171-
, session: fromMaybe undefined o.session
172-
, minDHSize: fromMaybe undefined o.minDHSize
173-
, highWaterMark: fromMaybe undefined o.highWaterMark
174-
-- TlsSecureContext
175-
, ca: fromMaybe undefined o.ca
176-
, cert: fromMaybe undefined o.cert
177-
, sigalgs: fromMaybe undefined o.sigalgs
178-
, ciphers: fromMaybe undefined o.ciphers
179-
, clientCertEngine: fromMaybe undefined o.clientCertEngine
180-
, crl: fromMaybe undefined o.crl
181-
, dhparam: fromMaybe undefined o.dhparam
182-
, ecdhCurve: fromMaybe undefined o.ecdhCurve
183-
, honorCipherOrder: fromMaybe undefined o.honorCipherOrder
184-
, key: fromMaybe undefined o.key
185-
, privateKeyEngine: fromMaybe undefined o.privateKeyEngine
186-
, privateKeyIdentifier: fromMaybe undefined o.privateKeyIdentifier
187-
, maxVersion: fromMaybe undefined o.maxVersion
188-
, minVersion: fromMaybe undefined o.minVersion
189-
, passphrase: fromMaybe undefined o.passphrase
190-
, pfx: fromMaybe undefined o.pfx
191-
, secureOptions: fromMaybe undefined o.secureOptions
192-
, secureProtocol: fromMaybe undefined o.secureProtocol
193-
, sessionIdContext: fromMaybe undefined o.sessionIdContext
194-
, ticketKeys: fromMaybe undefined o.ticketKeys
195-
, sessionTimeout: fromMaybe undefined o.sessionTimeout
196-
-- TcpConnect
197-
, port: fromMaybe undefined o.port
198-
, host: fromMaybe undefined o.host
199-
, localAddress: fromMaybe undefined o.localAddress
200-
, localPort: fromMaybe undefined o.localPort
201-
, family: fromMaybe undefined o.family
202-
, noDelay: fromMaybe undefined o.noDelay
203-
, keepAlive: fromMaybe undefined o.keepAlive
204-
, keepAliveInitialDelay: fromMaybe undefined o.keepAliveInitialDelay
205-
}
206-
207-
runEffectFn2 connectAuthOptionsImpl authority finalOptions
208-
209-
foreign import connectAuthOptionsImpl :: EffectFn2 (String) ({ | ConnectOptions Unlift + TlsConnectOptions Unlift + TlsSecureContextOptions Unlift + TcpConnectOptions Unlift + () }) (Http2Session Client)
96+
foreign import connectAuthOptionsImpl :: forall r. EffectFn2 (String) ({ | r }) (Http2Session Client)
21097

21198
type Unlift :: Type -> Type
21299
type Unlift a = a

src/Node/Http2/Server.purs

Lines changed: 27 additions & 150 deletions
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,13 @@ module Node.Http2.Server
2020

2121
import Prelude
2222

23-
import Data.Maybe (Maybe(..), fromMaybe)
2423
import Data.Time.Duration (Milliseconds)
2524
import Effect (Effect)
2625
import Effect.Exception (Error)
2726
import Effect.Uncurried (EffectFn1, EffectFn2, EffectFn4, mkEffectFn1, mkEffectFn2, mkEffectFn4, runEffectFn1, runEffectFn2)
2827
import Node.Http2.Types (Headers, Http2SecureServer, Http2ServerRequest, Http2ServerResponse, Http2Session, Http2Stream, Server, Settings, TlsSecureContextOptions)
2928
import Node.Stream (Duplex)
29+
import Prim.Row as Row
3030
import Type.Row (type (+))
3131

3232
-- | `allowHTTP1` <boolean> Incoming client connections that do not support HTTP/2 will be downgraded to HTTP/1.x when set to true. See the 'unknownProtocol' event. See ALPN negotiation. Default: false.
@@ -115,120 +115,13 @@ type NetCreateServerOptions f r =
115115
-- | })
116116
-- | ```
117117
createSecureServer
118-
:: ( { | Http2CreateSecureServerOptions Maybe + TlsCreateServerOptions Maybe + TlsSecureContextOptions Maybe + NetCreateServerOptions Maybe + () }
119-
-> { | Http2CreateSecureServerOptions Maybe + TlsCreateServerOptions Maybe + TlsSecureContextOptions Maybe + NetCreateServerOptions Maybe + () }
120-
)
118+
:: forall rec trash
119+
. Row.Union rec trash (Http2CreateSecureServerOptions Unlift + TlsCreateServerOptions Unlift + TlsSecureContextOptions Unlift + NetCreateServerOptions Unlift + ())
120+
=> { | rec }
121121
-> Effect Http2SecureServer
122-
createSecureServer buildOptions = do
123-
let
124-
o = buildOptions
125-
{ allowHTTP1: Nothing
126-
, maxDeflateDynamicTableSize: Nothing
127-
, maxSettings: Nothing
128-
, maxSessionMemory: Nothing
129-
, maxHeaderListPairs: Nothing
130-
, maxOutstandingPings: Nothing
131-
, maxSendHeaderBlockLength: Nothing
132-
, paddingStrategy: Nothing
133-
, peerMaxConcurrentStreams: Nothing
134-
, maxSessionInvalidFrames: Nothing
135-
, maxSessionRejectedStreams: Nothing
136-
, settings: Nothing
137-
, origins: Nothing
138-
, unknownProtocolTimeout: Nothing
139-
-- tls create server options
140-
, "ALPNProtocols": Nothing
141-
, enableTrace: Nothing
142-
, handshakeTimeout: Nothing
143-
, rejectUnauthorized: Nothing
144-
, requestCert: Nothing
145-
, pskIdentityHint: Nothing
146-
-- tls secure context options
147-
, ca: Nothing
148-
, cert: Nothing
149-
, sigalgs: Nothing
150-
, ciphers: Nothing
151-
, clientCertEngine: Nothing
152-
, crl: Nothing
153-
, dhparam: Nothing
154-
, ecdhCurve: Nothing
155-
, honorCipherOrder: Nothing
156-
, key: Nothing
157-
, privateKeyEngine: Nothing
158-
, privateKeyIdentifier: Nothing
159-
, maxVersion: Nothing
160-
, minVersion: Nothing
161-
, passphrase: Nothing
162-
, pfx: Nothing
163-
, secureOptions: Nothing
164-
, secureProtocol: Nothing
165-
, sessionIdContext: Nothing
166-
, ticketKeys: Nothing
167-
, sessionTimeout: Nothing
168-
-- net create server options
169-
, allowHalfOpen: Nothing
170-
, pauseOnConnect: Nothing
171-
, noDelay: Nothing
172-
, keepAlive: Nothing
173-
, keepAliveInitialDelay: Nothing
174-
}
175-
176-
options' :: { | Http2CreateSecureServerOptions Unlift + TlsCreateServerOptions Unlift + TlsSecureContextOptions Unlift + NetCreateServerOptions Unlift + () }
177-
options' =
178-
-- Http2
179-
{ allowHTTP1: fromMaybe undefined o.allowHTTP1
180-
, maxDeflateDynamicTableSize: fromMaybe undefined o.maxDeflateDynamicTableSize
181-
, maxSettings: fromMaybe undefined o.maxSettings
182-
, maxSessionMemory: fromMaybe undefined o.maxSessionMemory
183-
, maxHeaderListPairs: fromMaybe undefined o.maxHeaderListPairs
184-
, maxOutstandingPings: fromMaybe undefined o.maxOutstandingPings
185-
, maxSendHeaderBlockLength: fromMaybe undefined o.maxSendHeaderBlockLength
186-
, paddingStrategy: fromMaybe undefined o.paddingStrategy
187-
, peerMaxConcurrentStreams: fromMaybe undefined o.peerMaxConcurrentStreams
188-
, maxSessionInvalidFrames: fromMaybe undefined o.maxSessionInvalidFrames
189-
, maxSessionRejectedStreams: fromMaybe undefined o.maxSessionRejectedStreams
190-
, settings: fromMaybe undefined o.settings
191-
, origins: fromMaybe undefined o.origins
192-
, unknownProtocolTimeout: fromMaybe undefined o.unknownProtocolTimeout
193-
-- tls create server options
194-
, "ALPNProtocols": fromMaybe undefined o."ALPNProtocols"
195-
, enableTrace: fromMaybe undefined o.enableTrace
196-
, handshakeTimeout: fromMaybe undefined o.handshakeTimeout
197-
, rejectUnauthorized: fromMaybe undefined o.rejectUnauthorized
198-
, requestCert: fromMaybe undefined o.requestCert
199-
, pskIdentityHint: fromMaybe undefined o.pskIdentityHint
200-
-- tls secure context options
201-
, ca: fromMaybe undefined o.ca
202-
, cert: fromMaybe undefined o.cert
203-
, sigalgs: fromMaybe undefined o.sigalgs
204-
, ciphers: fromMaybe undefined o.ciphers
205-
, clientCertEngine: fromMaybe undefined o.clientCertEngine
206-
, crl: fromMaybe undefined o.crl
207-
, dhparam: fromMaybe undefined o.dhparam
208-
, ecdhCurve: fromMaybe undefined o.ecdhCurve
209-
, honorCipherOrder: fromMaybe undefined o.honorCipherOrder
210-
, key: fromMaybe undefined o.key
211-
, privateKeyEngine: fromMaybe undefined o.privateKeyEngine
212-
, privateKeyIdentifier: fromMaybe undefined o.privateKeyIdentifier
213-
, maxVersion: fromMaybe undefined o.maxVersion
214-
, minVersion: fromMaybe undefined o.minVersion
215-
, passphrase: fromMaybe undefined o.passphrase
216-
, pfx: fromMaybe undefined o.pfx
217-
, secureOptions: fromMaybe undefined o.secureOptions
218-
, secureProtocol: fromMaybe undefined o.secureProtocol
219-
, sessionIdContext: fromMaybe undefined o.sessionIdContext
220-
, ticketKeys: fromMaybe undefined o.ticketKeys
221-
, sessionTimeout: fromMaybe undefined o.sessionTimeout
222-
-- net create server options
223-
, allowHalfOpen: fromMaybe undefined o.allowHalfOpen
224-
, pauseOnConnect: fromMaybe undefined o.pauseOnConnect
225-
, noDelay: fromMaybe undefined o.noDelay
226-
, keepAlive: fromMaybe undefined o.keepAlive
227-
, keepAliveInitialDelay: fromMaybe undefined o.keepAliveInitialDelay
228-
}
229-
runEffectFn1 createSecureServerImpl options'
230-
231-
foreign import createSecureServerImpl :: EffectFn1 { | Http2CreateSecureServerOptions Unlift + TlsCreateServerOptions Unlift + TlsSecureContextOptions Unlift + NetCreateServerOptions Unlift + () } (Http2SecureServer)
122+
createSecureServer options = runEffectFn1 createSecureServerImpl options
123+
124+
foreign import createSecureServerImpl :: forall r. EffectFn1 { | r } (Http2SecureServer)
232125

233126
-- | `port` <number>
234127
-- | `host` <string>
@@ -238,42 +131,26 @@ foreign import createSecureServerImpl :: EffectFn1 { | Http2CreateSecureServerOp
238131
-- | `readableAll` <boolean> For IPC servers makes the pipe readable for all users. Default: false.
239132
-- | `writableAll` <boolean> For IPC servers makes the pipe writable for all users. Default: false.
240133
-- | `ipv6Only` <boolean> For TCP servers, setting ipv6Only to true will disable dual-stack support, i.e., binding to host :: won't make 0.0.0.0 be bound. Default: false.
241-
type ListenOptions f =
242-
{ port :: f Int
243-
, host :: f String
244-
, backlog :: f Number
245-
, exclusive :: f Boolean
246-
, readableAll :: f Boolean
247-
, writableAll :: f Boolean
248-
, ipv6Only :: f Boolean
249-
}
250-
251-
listen :: Http2SecureServer -> (ListenOptions Maybe -> ListenOptions Maybe) -> Effect Unit
252-
listen s buildOptions = do
253-
let
254-
o = buildOptions
255-
{ port: Nothing
256-
, host: Nothing
257-
, backlog: Nothing
258-
, exclusive: Nothing
259-
, readableAll: Nothing
260-
, writableAll: Nothing
261-
, ipv6Only: Nothing
262-
}
263-
264-
finalOptions :: ListenOptions Unlift
265-
finalOptions =
266-
{ port: fromMaybe undefined o.port
267-
, host: fromMaybe undefined o.host
268-
, backlog: fromMaybe undefined o.backlog
269-
, exclusive: fromMaybe undefined o.exclusive
270-
, readableAll: fromMaybe undefined o.readableAll
271-
, writableAll: fromMaybe undefined o.writableAll
272-
, ipv6Only: fromMaybe undefined o.ipv6Only
273-
}
274-
runEffectFn2 listenImpl s finalOptions
275-
276-
foreign import listenImpl :: EffectFn2 (Http2SecureServer) (ListenOptions Unlift) (Unit)
134+
type ListenOptions r =
135+
( port :: Int
136+
, host :: String
137+
, backlog :: Number
138+
, exclusive :: Boolean
139+
, readableAll :: Boolean
140+
, writableAll :: Boolean
141+
, ipv6Only :: Boolean
142+
| r
143+
)
144+
145+
listen
146+
:: forall r trash
147+
. Row.Union r trash (ListenOptions + ())
148+
=> Http2SecureServer
149+
-> { | r }
150+
-> Effect Unit
151+
listen s o = runEffectFn2 listenImpl s o
152+
153+
foreign import listenImpl :: forall r. EffectFn2 (Http2SecureServer) { | r } (Unit)
277154

278155
type Unlift :: Type -> Type
279156
type Unlift a = a

0 commit comments

Comments
 (0)