Skip to content

Commit

Permalink
Update handlers to drop suffix
Browse files Browse the repository at this point in the history
  • Loading branch information
JordanMartinez committed Jul 14, 2023
1 parent 1e17e63 commit 4d9adbe
Show file tree
Hide file tree
Showing 4 changed files with 107 additions and 104 deletions.
42 changes: 21 additions & 21 deletions src/Node/Http2/Server.purs
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
module Node.Http2.Server
( toTlsServer
, createSecureServer
, checkContinueHandle
, requestHandle
, sessionHandle
, sessionErrorHandle
, streamHandle
, timeoutHandle
, unknownProtocolHandle
, checkContinueH
, requestH
, sessionH
, sessionErrorH
, streamH
, timeoutH
, unknownProtocolH
, setTimeout
, timeout
, updateSettings
Expand Down Expand Up @@ -41,26 +41,26 @@ createSecureServer options = runEffectFn1 createSecureServerImpl options

foreign import createSecureServerImpl :: forall r. EffectFn1 { | r } (Http2SecureServer)

checkContinueHandle :: EventHandle2 Http2SecureServer Http2ServerRequest Http2ServerResponse
checkContinueHandle = EventHandle "checkContinue" \cb -> mkEffectFn2 \a b -> cb a b
checkContinueH :: EventHandle2 Http2SecureServer Http2ServerRequest Http2ServerResponse
checkContinueH = EventHandle "checkContinue" \cb -> mkEffectFn2 \a b -> cb a b

requestHandle :: EventHandle2 Http2SecureServer Http2ServerRequest Http2ServerResponse
requestHandle = EventHandle "request" \cb -> mkEffectFn2 \a b -> cb a b
requestH :: EventHandle2 Http2SecureServer Http2ServerRequest Http2ServerResponse
requestH = EventHandle "request" \cb -> mkEffectFn2 \a b -> cb a b

sessionHandle :: EventHandle1 Http2SecureServer (Http2Session Server)
sessionHandle = EventHandle "session" mkEffectFn1
sessionH :: EventHandle1 Http2SecureServer (Http2Session Server)
sessionH = EventHandle "session" mkEffectFn1

sessionErrorHandle :: EventHandle2 Http2SecureServer Error (Http2Session Server)
sessionErrorHandle = EventHandle "sessionError" \cb -> mkEffectFn2 \a b -> cb a b
sessionErrorH :: EventHandle2 Http2SecureServer Error (Http2Session Server)
sessionErrorH = EventHandle "sessionError" \cb -> mkEffectFn2 \a b -> cb a b

streamHandle :: EventHandle4 Http2SecureServer (Http2Stream Server) (Headers) BitwiseFlag (Array String)
streamHandle = EventHandle "stream" \cb -> mkEffectFn4 \a b c d -> cb a b c d
streamH :: EventHandle4 Http2SecureServer (Http2Stream Server) (Headers) BitwiseFlag (Array String)
streamH = EventHandle "stream" \cb -> mkEffectFn4 \a b c d -> cb a b c d

timeoutHandle :: EventHandle0 TlsServer
timeoutHandle = EventHandle "timeout" identity
timeoutH :: EventHandle0 TlsServer
timeoutH = EventHandle "timeout" identity

unknownProtocolHandle :: EventHandle1 Http2SecureServer Duplex
unknownProtocolHandle = EventHandle "unknownProtocol" mkEffectFn1
unknownProtocolH :: EventHandle1 Http2SecureServer Duplex
unknownProtocolH = EventHandle "unknownProtocol" mkEffectFn1

setTimeout :: Http2SecureServer -> Milliseconds -> Effect Http2SecureServer
setTimeout s ms = runEffectFn2 setTimeoutImpl s ms
Expand Down
60 changes: 30 additions & 30 deletions src/Node/Http2/Session.purs
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
module Node.Http2.Session
( toEventEmitter
, closeHandle
, connectHandle
, errorHandle
, frameErrorHandle
, goAwayHandle
, localSettingsHandle
, pingHandle
, remoteSettingsHandle
, streamHandle
, timeoutHandle
, closeH
, connectH
, errorH
, frameErrorH
, goAwayH
, localSettingsH
, pingH
, remoteSettingsH
, streamH
, timeoutH
, alpnProtocol
, close
, closed
Expand Down Expand Up @@ -72,40 +72,40 @@ import Unsafe.Coerce (unsafeCoerce)
toEventEmitter :: forall endpoint. Http2Session endpoint -> EventEmitter
toEventEmitter = unsafeCoerce

closeHandle :: forall endpoint. EventHandle0 (Http2Session endpoint)
closeHandle = EventHandle "close" identity
closeH :: forall endpoint. EventHandle0 (Http2Session endpoint)
closeH = EventHandle "close" identity

connectHandle :: forall endpoint. EventHandle2 (Http2Session endpoint) (Http2Session endpoint) (Socket TCP)
connectHandle = EventHandle "connect" \cb -> mkEffectFn2 \a b -> cb a b
connectH :: forall endpoint. EventHandle2 (Http2Session endpoint) (Http2Session endpoint) (Socket TCP)
connectH = EventHandle "connect" \cb -> mkEffectFn2 \a b -> cb a b

errorHandle :: forall endpoint. EventHandle1 (Http2Session endpoint) Error
errorHandle = EventHandle "error" mkEffectFn1
errorH :: forall endpoint. EventHandle1 (Http2Session endpoint) Error
errorH = EventHandle "error" mkEffectFn1

frameErrorHandle :: forall endpoint. EventHandle3 (Http2Session endpoint) FrameType ErrorCode StreamId
frameErrorHandle = EventHandle "frameError" \cb -> mkEffectFn3 \a b c -> cb a b c
frameErrorH :: forall endpoint. EventHandle3 (Http2Session endpoint) FrameType ErrorCode StreamId
frameErrorH = EventHandle "frameError" \cb -> mkEffectFn3 \a b c -> cb a b c

goAwayHandle
goAwayH
:: forall endpoint
. EventHandle
(Http2Session endpoint)
(ErrorCode -> StreamId -> (Maybe Buffer) -> Effect Unit)
(EffectFn3 ErrorCode StreamId (Nullable Buffer) Unit)
goAwayHandle = EventHandle "goAway" \cb -> mkEffectFn3 \a b c -> cb a b (toMaybe c)
goAwayH = EventHandle "goAway" \cb -> mkEffectFn3 \a b c -> cb a b (toMaybe c)

localSettingsHandle :: forall endpoint. EventHandle1 (Http2Session endpoint) Settings
localSettingsHandle = EventHandle "localSettings" mkEffectFn1
localSettingsH :: forall endpoint. EventHandle1 (Http2Session endpoint) Settings
localSettingsH = EventHandle "localSettings" mkEffectFn1

pingHandle :: forall endpoint. EventHandle1 (Http2Session endpoint) Buffer
pingHandle = EventHandle "ping" mkEffectFn1
pingH :: forall endpoint. EventHandle1 (Http2Session endpoint) Buffer
pingH = EventHandle "ping" mkEffectFn1

remoteSettingsHandle :: forall endpoint. EventHandle1 (Http2Session endpoint) Settings
remoteSettingsHandle = EventHandle "remoteSettings" mkEffectFn1
remoteSettingsH :: forall endpoint. EventHandle1 (Http2Session endpoint) Settings
remoteSettingsH = EventHandle "remoteSettings" mkEffectFn1

streamHandle :: forall endpoint. EventHandle4 (Http2Session endpoint) (Http2Stream endpoint) Headers BitwiseFlag (Array String)
streamHandle = EventHandle "stream" \cb -> mkEffectFn4 \a b c d -> cb a b c d
streamH :: forall endpoint. EventHandle4 (Http2Session endpoint) (Http2Stream endpoint) Headers BitwiseFlag (Array String)
streamH = EventHandle "stream" \cb -> mkEffectFn4 \a b c d -> cb a b c d

timeoutHandle :: forall endpoint. EventHandle0 (Http2Session endpoint)
timeoutHandle = EventHandle "timeout" identity
timeoutH :: forall endpoint. EventHandle0 (Http2Session endpoint)
timeoutH = EventHandle "timeout" identity

alpnProtocol :: forall endpoint. Http2Session endpoint -> Effect (Maybe String)
alpnProtocol session = map toMaybe $ runEffectFn1 alpnProtocolImpl session
Expand Down
72 changes: 36 additions & 36 deletions src/Node/Http2/Stream.purs
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
module Node.Http2.Stream
( toDuplex
, abortedHandle
, closeHandle
, errorHandle
, frameErrorHandle
, readyHandle
, timeoutHandle
, trailersHandle
, wantTrailersHandle
, abortedH
, closeH
, errorH
, frameErrorH
, readyH
, timeoutH
, trailersH
, wantTrailersH
, bufferSize
, close
, closed
Expand Down Expand Up @@ -38,10 +38,10 @@ module Node.Http2.Stream
, respondWithFd
, RespondWithFileOptions
, respondWithFile
, continueHandle
, headersHandle
, pushHandle
, responseHandle
, continueH
, headersH
, pushH
, responseH
) where

import Prelude
Expand Down Expand Up @@ -70,29 +70,29 @@ import Unsafe.Coerce (unsafeCoerce)
toDuplex :: forall endpoint. Http2Stream endpoint -> Duplex
toDuplex = unsafeCoerce

abortedHandle :: forall endpoint. EventHandle0 (Http2Stream endpoint)
abortedHandle = EventHandle "aborted" identity
abortedH :: forall endpoint. EventHandle0 (Http2Stream endpoint)
abortedH = EventHandle "aborted" identity

closeHandle :: forall endpoint. EventHandle0 (Http2Stream endpoint)
closeHandle = EventHandle "close" identity
closeH :: forall endpoint. EventHandle0 (Http2Stream endpoint)
closeH = EventHandle "close" identity

errorHandle :: forall endpoint. EventHandle1 (Http2Stream endpoint) Error
errorHandle = EventHandle "error" mkEffectFn1
errorH :: forall endpoint. EventHandle1 (Http2Stream endpoint) Error
errorH = EventHandle "error" mkEffectFn1

frameErrorHandle :: forall endpoint. EventHandle3 (Http2Stream endpoint) FrameType ErrorCode StreamId
frameErrorHandle = EventHandle "frameError" \cb -> mkEffectFn3 \a b c -> cb a b c
frameErrorH :: forall endpoint. EventHandle3 (Http2Stream endpoint) FrameType ErrorCode StreamId
frameErrorH = EventHandle "frameError" \cb -> mkEffectFn3 \a b c -> cb a b c

readyHandle :: forall endpoint. EventHandle0 (Http2Stream endpoint)
readyHandle = EventHandle "ready" identity
readyH :: forall endpoint. EventHandle0 (Http2Stream endpoint)
readyH = EventHandle "ready" identity

timeoutHandle :: forall endpoint. EventHandle0 (Http2Stream endpoint)
timeoutHandle = EventHandle "timeout" identity
timeoutH :: forall endpoint. EventHandle0 (Http2Stream endpoint)
timeoutH = EventHandle "timeout" identity

trailersHandle :: forall endpoint. EventHandle2 (Http2Stream endpoint) Settings BitwiseFlag
trailersHandle = EventHandle "trailers" \cb -> mkEffectFn2 \a b -> cb a b
trailersH :: forall endpoint. EventHandle2 (Http2Stream endpoint) Settings BitwiseFlag
trailersH = EventHandle "trailers" \cb -> mkEffectFn2 \a b -> cb a b

wantTrailersHandle :: forall endpoint. EventHandle0 (Http2Stream endpoint)
wantTrailersHandle = EventHandle "wantTrailers" identity
wantTrailersH :: forall endpoint. EventHandle0 (Http2Stream endpoint)
wantTrailersH = EventHandle "wantTrailers" identity

bufferSize :: forall endpoint. Http2Stream endpoint -> Effect Int
bufferSize s = runEffectFn1 bufferSizeImpl s
Expand Down Expand Up @@ -285,15 +285,15 @@ respondWithFile s fp h o = runEffectFn4 respondWithFileImpl s fp h o

foreign import respondWithFileImpl :: EffectFn4 (Http2Stream Server) (FilePath) (Headers) (RespondWithFileOptions) (Unit)

continueHandle :: EventHandle0 (Http2Stream Client)
continueHandle = EventHandle "continue" identity
continueH :: EventHandle0 (Http2Stream Client)
continueH = EventHandle "continue" identity

headersHandle :: EventHandle2 (Http2Stream Client) Headers BitwiseFlag
headersHandle = EventHandle "headers" \cb -> mkEffectFn2 \a b -> cb a b
headersH :: EventHandle2 (Http2Stream Client) Headers BitwiseFlag
headersH = EventHandle "headers" \cb -> mkEffectFn2 \a b -> cb a b

pushHandle :: EventHandle2 (Http2Stream Client) Headers BitwiseFlag
pushHandle = EventHandle "push" \cb -> mkEffectFn2 \a b -> cb a b
pushH :: EventHandle2 (Http2Stream Client) Headers BitwiseFlag
pushH = EventHandle "push" \cb -> mkEffectFn2 \a b -> cb a b

responseHandle :: EventHandle2 (Http2Stream Client) Headers BitwiseFlag
responseHandle = EventHandle "response" \cb -> mkEffectFn2 \a b -> cb a b
responseH :: EventHandle2 (Http2Stream Client) Headers BitwiseFlag
responseH = EventHandle "response" \cb -> mkEffectFn2 \a b -> cb a b

37 changes: 20 additions & 17 deletions test/Test/Main.purs
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,8 @@ import Effect.Class.Console (log)
import Effect.Exception as Exception
import Effect.Ref as Ref
import Node.Buffer as Buffer
import Node.Buffer.Immutable (ImmutableBuffer)
import Node.Encoding (Encoding(..))
import Node.EventEmitter (on)
import Node.EventEmitter (on_)
import Node.FS.Sync as FS
import Node.Http2.Client as Client
import Node.Http2.ErrorCode as ErrorCode
Expand All @@ -24,6 +23,7 @@ import Node.Http2.Types (Http2Session)
import Node.Net.Server as NServer
import Node.Path as Path
import Node.Stream as Stream
import Node.TLS.Server as Tls
import Unsafe.Coerce (unsafeCoerce)

logWith :: String -> String -> Effect Unit
Expand All @@ -40,20 +40,23 @@ main = do
{ key: [ privateKey ]
, cert: [ cert ]
}
on Server.checkContinueHandle server.http2 \req res -> do
let
tlsServer = Server.toTlsServer server
tcpServer = Tls.toTcpServer tlsServer
server # on_ Server.checkContinueH \req res -> do
log "server - onCheckContinue"
on NServer.connectionHandle server.net \duplex -> do
tcpServer # on_ NServer.connectionH \duplex -> do
log "server - onConnection"
on Server.sessionHandle server.http2 \session -> do
server # on_ Server.sessionH \session -> do
log "server - onSession"
log "Testing properties for any thrown errors"
printHttp2SessionState session

on Server.sessionErrorHandle server.http2 \err session -> do
server # on_ Server.sessionErrorH \err session -> do
log "server - onSessionError"
log (unsafeCoerce err)
printHttp2SessionState session
on Server.streamHandle server.http2 \stream headers flags rawHeaders -> do
server # on_ Server.streamH \stream headers flags rawHeaders -> do
streamId <- H2Stream.id stream
log $ "server - onStream for id: " <> show streamId
log $ printHeaders' "\n" headers
Expand All @@ -65,28 +68,28 @@ main = do
, waitForTrailers: true
}
-- H2Stream.close stream ErrorCode.noError
Stream.writeStringCb_ duplex UTF8 "hello from server" \err -> do
void $ Stream.writeString' duplex UTF8 "hello from server" \err -> do
log (unsafeCoerce err)
Stream.end' duplex \_ -> do
log $ "server - onStream - closing for id: " <> show streamId
H2Stream.close stream ErrorCode.noError

on Server.timeoutHandle server.tls do
tlsServer # on_ Server.timeoutH do
log "onTimeout"
on Server.unknownProtocolHandle server.http2 \duplex -> do
server # on_ Server.unknownProtocolH \duplex -> do
log "onUnknownProtocol"
-- https://stackoverflow.com/a/63173619
-- "In UNIX-like systems, non-root users are unable to bind to ports lower than 1024."
let httpsPort = 8443
NServer.listenTcp server.net
NServer.listenTcp tcpServer
{ port: httpsPort
}
on NServer.listeningHandle server.net do
tcpServer # on_ NServer.listeningH do
log "server listening"
session <- Client.connect' ("https://localhost:" <> show httpsPort)
{ ca: [ cert ]
}
on Session.errorHandle session \error ->
session # on_ Session.errorH \error ->
log $ "Client session encountered error: " <> Exception.message error
stream <- Session.request session
( unsafeCoerce
Expand All @@ -96,21 +99,21 @@ main = do
)
let duplex = toDuplex stream
Stream.end duplex
on H2Stream.responseHandle stream \headers flags -> do
stream # on_ H2Stream.responseH \headers flags -> do
log "client - onResponse"
log $ printHeaders' "\n" headers
log $ "Flags: " <> printFlags flags
chunksRef <- Ref.new []
on Stream.dataHandle duplex \buf ->
duplex # on_ Stream.dataH \buf ->
Ref.modify_ (flip Array.snoc buf) chunksRef
on Stream.endHandle duplex do
duplex # on_ Stream.endH do
chunks <- Ref.read chunksRef
buffer <- Buffer.concat chunks :: Effect Buffer.Buffer
str <- Buffer.toString UTF8 buffer :: Effect String
log $ "client - onResponse body: " <> show str
H2Stream.close stream ErrorCode.noError
Session.destroy session
NServer.close server.net
NServer.close tcpServer

printHttp2SessionState :: forall endpoint. Http2Session endpoint -> Effect Unit
printHttp2SessionState session = do
Expand Down

0 comments on commit 4d9adbe

Please sign in to comment.