Skip to content

Commit fadc883

Browse files
Update session to EE events; add newtypes
1 parent 24fbe8c commit fadc883

File tree

4 files changed

+89
-84
lines changed

4 files changed

+89
-84
lines changed

src/Node/Http2/Session.js

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,3 @@
1-
export const onCloseImpl = (http2Session, cb) => http2Session.on("close", cb);
2-
export const onConnectImpl = (http2Session, cb) => http2Session.on("connect", cb);
3-
export const onErrorImpl = (http2Session, cb) => http2Session.on("error", cb);
4-
export const onFrameErrorImpl = (http2Session, cb) => http2Session.on("frameError", cb);
5-
export const onGoAwayImpl = (http2Session, cb) => http2Session.on("goAway", cb);
6-
export const onLocalSettingsImpl = (http2Session, cb) => http2Session.on("localSettings", cb);
7-
export const onPingImpl = (http2Session, cb) => http2Session.on("ping", cb);
8-
export const onRemoteSettingsImpl = (http2Session, cb) => http2Session.on("remoteSettings", cb);
9-
export const onStreamImpl = (http2Session, cb) => http2Session.on("stream", cb);
10-
export const onTimeoutImpl = (http2Session, cb) => http2Session.on("timeout", cb);
111
export const alpnProtocolImpl = (http2Session) => http2Session.alpnProtocol;
122
export const closeImpl = (http2Session) => http2Session.close();
133
// Intentionally not supporting the `http2Session.close(cb)` API
@@ -45,7 +35,5 @@ export const altsvcStreamImpl = (http2session, alt, stream) => http2session.alts
4535
export const originImpl = (http2session, origins) => http2session.origin(origins);
4636

4737
// client
48-
export const onAltsvcImpl = (http2session, cb) => http2session.on("altsvc", cb);
49-
export const onOriginImpl = (http2session, cb) => http2session.on("origin", cb);
5038
export const requestHeadersImpl = (http2session, headers) => http2session.request(headers);
5139
export const requestHeadersOptionsImpl = (http2session, headers, options) => http2session.request(headers, options);

src/Node/Http2/Session.purs

Lines changed: 60 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
11
module Node.Http2.Session
2-
( onClose
3-
, onConnect
4-
, onError
5-
, onFrameError
6-
, onGoAway
7-
, onLocalSettings
8-
, onPing
9-
, onRemoteSettings
10-
, onStream
11-
, onTimeout
2+
( toEventEmitter
3+
, closeHandle
4+
, connectHandle
5+
, errorHandle
6+
, frameErrorHandle
7+
, goAwayHandle
8+
, localSettingsHandle
9+
, pingHandle
10+
, remoteSettingsHandle
11+
, streamHandle
12+
, timeoutHandle
1213
, alpnProtocol
1314
, close
1415
, closed
@@ -41,8 +42,8 @@ module Node.Http2.Session
4142
, altsvcStreamId
4243
, altsvcOrigin
4344
, origin
44-
, onAltsvc
45-
, onOrigin
45+
, altsvcHandle
46+
, originHandle
4647
, RequestOptions
4748
, request
4849
, request'
@@ -57,61 +58,53 @@ import Data.Time.Duration (Milliseconds)
5758
import Effect (Effect)
5859
import Effect.Exception (Error)
5960
import Effect.Uncurried (EffectFn1, EffectFn2, EffectFn3, EffectFn4, mkEffectFn1, mkEffectFn2, mkEffectFn3, mkEffectFn4, runEffectFn1, runEffectFn2, runEffectFn3, runEffectFn4)
61+
import Node.Buffer (Buffer)
6062
import Node.Buffer.Immutable (ImmutableBuffer)
61-
import Node.Http2.Types (Headers, Http2Session, Http2Stream, Settings)
63+
import Node.EventEmitter (EventEmitter, EventHandle(..))
64+
import Node.EventEmitter.UtilTypes (EventHandle0, EventHandle1, EventHandle2, EventHandle3, EventHandle4)
65+
import Node.Http2.Flags (BitwiseFlag)
66+
import Node.Http2.Types (ErrorCode, FrameType, Headers, Http2Session, Http2Stream, Settings, StreamId)
6267
import Node.Net.Types (Socket, TCP)
6368
import Node.TLS.Types (Client, Server)
69+
import Unsafe.Coerce (unsafeCoerce)
6470

65-
onClose :: forall endpoint. Http2Session endpoint -> Effect Unit -> Effect Unit
66-
onClose session cb = runEffectFn2 onCloseImpl session cb
71+
toEventEmitter :: forall endpoint. Http2Session endpoint -> EventEmitter
72+
toEventEmitter = unsafeCoerce
6773

68-
foreign import onCloseImpl :: forall endpoint. EffectFn2 (Http2Session endpoint) (Effect Unit) Unit
74+
closeHandle :: forall endpoint. EventHandle0 (Http2Session endpoint)
75+
closeHandle = EventHandle "close" identity
6976

70-
onConnect :: forall endpoint. Http2Session endpoint -> (Http2Session endpoint -> Socket TCP -> Effect Unit) -> Effect Unit
71-
onConnect h2s cb = runEffectFn2 onConnectImpl h2s $ mkEffectFn2 cb
77+
connectHandle :: forall endpoint. EventHandle2 (Http2Session endpoint) (Http2Session endpoint) (Socket TCP)
78+
connectHandle = EventHandle "connect" \cb -> mkEffectFn2 \a b -> cb a b
7279

73-
foreign import onConnectImpl :: forall endpoint. EffectFn2 (Http2Session endpoint) (EffectFn2 (Http2Session endpoint) (Socket TCP) Unit) Unit
80+
errorHandle :: forall endpoint. EventHandle1 (Http2Session endpoint) Error
81+
errorHandle = EventHandle "error" mkEffectFn1
7482

75-
onError :: forall endpoint. Http2Session endpoint -> (Error -> Effect Unit) -> Effect Unit
76-
onError session cb = runEffectFn2 onErrorImpl session $ mkEffectFn1 cb
83+
frameErrorHandle :: forall endpoint. EventHandle3 (Http2Session endpoint) FrameType ErrorCode StreamId
84+
frameErrorHandle = EventHandle "frameError" \cb -> mkEffectFn3 \a b c -> cb a b c
7785

78-
foreign import onErrorImpl :: forall endpoint. EffectFn2 (Http2Session endpoint) (EffectFn1 Error Unit) (Unit)
86+
goAwayHandle
87+
:: forall endpoint
88+
. EventHandle
89+
(Http2Session endpoint)
90+
(ErrorCode -> StreamId -> (Maybe Buffer) -> Effect Unit)
91+
(EffectFn3 ErrorCode StreamId (Nullable Buffer) Unit)
92+
goAwayHandle = EventHandle "goAway" \cb -> mkEffectFn3 \a b c -> cb a b (toMaybe c)
7993

80-
onFrameError :: forall endpoint. Http2Session endpoint -> (Int -> Int -> Int -> Effect Unit) -> Effect Unit
81-
onFrameError session cb = runEffectFn2 onFrameErrorImpl session $ mkEffectFn3 cb
94+
localSettingsHandle :: forall endpoint. EventHandle1 (Http2Session endpoint) Settings
95+
localSettingsHandle = EventHandle "localSettings" mkEffectFn1
8296

83-
foreign import onFrameErrorImpl :: forall endpoint. EffectFn2 (Http2Session endpoint) (EffectFn3 Int Int Int Unit) (Unit)
97+
pingHandle :: forall endpoint. EventHandle1 (Http2Session endpoint) Buffer
98+
pingHandle = EventHandle "ping" mkEffectFn1
8499

85-
onGoAway :: forall endpoint. Http2Session endpoint -> (Int -> Int -> Maybe ImmutableBuffer -> Effect Unit) -> Effect Unit
86-
onGoAway session cb = runEffectFn2 onGoAwayImpl session $ mkEffectFn3 \c lsi buf ->
87-
cb c lsi (toMaybe buf)
100+
remoteSettingsHandle :: forall endpoint. EventHandle1 (Http2Session endpoint) Settings
101+
remoteSettingsHandle = EventHandle "remoteSettings" mkEffectFn1
88102

89-
foreign import onGoAwayImpl :: forall endpoint. EffectFn2 (Http2Session endpoint) (EffectFn3 Int Int (Nullable ImmutableBuffer) Unit) (Unit)
103+
streamHandle :: forall endpoint. EventHandle4 (Http2Session endpoint) (Http2Stream endpoint) Headers BitwiseFlag (Array String)
104+
streamHandle = EventHandle "stream" \cb -> mkEffectFn4 \a b c d -> cb a b c d
90105

91-
onLocalSettings :: forall endpoint. Http2Session endpoint -> (Settings -> Effect Unit) -> Effect Unit
92-
onLocalSettings session cb = runEffectFn2 onLocalSettingsImpl session $ mkEffectFn1 cb
93-
94-
foreign import onLocalSettingsImpl :: forall endpoint. EffectFn2 (Http2Session endpoint) (EffectFn1 Settings Unit) (Unit)
95-
96-
onPing :: forall endpoint. Http2Session endpoint -> (Maybe ImmutableBuffer -> Effect Unit) -> Effect Unit
97-
onPing sesson cb = runEffectFn2 onPingImpl sesson $ mkEffectFn1 \a -> cb $ toMaybe a
98-
99-
foreign import onPingImpl :: forall endpoint. EffectFn2 (Http2Session endpoint) (EffectFn1 (Nullable ImmutableBuffer) Unit) (Unit)
100-
101-
onRemoteSettings :: forall endpoint. Http2Session endpoint -> (Settings -> Effect Unit) -> Effect Unit
102-
onRemoteSettings session cb = runEffectFn2 onRemoteSettingsImpl session $ mkEffectFn1 cb
103-
104-
foreign import onRemoteSettingsImpl :: forall endpoint. EffectFn2 (Http2Session endpoint) (EffectFn1 Settings Unit) (Unit)
105-
106-
onStream :: forall endpoint. Http2Session endpoint -> (Http2Stream endpoint -> Headers -> Number -> (Array String) -> Effect Unit) -> Effect Unit
107-
onStream session cb = runEffectFn2 onStreamImpl session $ mkEffectFn4 cb
108-
109-
foreign import onStreamImpl :: forall endpoint. EffectFn2 (Http2Session endpoint) (EffectFn4 (Http2Stream endpoint) Headers Number (Array String) Unit) (Unit)
110-
111-
onTimeout :: forall endpoint. Http2Session endpoint -> Effect Unit -> Effect Unit
112-
onTimeout session cb = runEffectFn2 onTimeoutImpl session cb
113-
114-
foreign import onTimeoutImpl :: forall endpoint. EffectFn2 (Http2Session endpoint) (Effect Unit) (Unit)
106+
timeoutHandle :: forall endpoint. EventHandle0 (Http2Session endpoint)
107+
timeoutHandle = EventHandle "timeout" identity
115108

116109
alpnProtocol :: forall endpoint. Http2Session endpoint -> Effect (Maybe String)
117110
alpnProtocol session = map toMaybe $ runEffectFn1 alpnProtocolImpl session
@@ -143,15 +136,15 @@ destroyWithError s e = runEffectFn2 destroyWithErrorImpl s e
143136

144137
foreign import destroyWithErrorImpl :: forall endpoint. EffectFn2 (Http2Session endpoint) (Error) (Unit)
145138

146-
destroyWithCode :: forall endpoint. Http2Session endpoint -> Int -> Effect Unit
139+
destroyWithCode :: forall endpoint. Http2Session endpoint -> ErrorCode -> Effect Unit
147140
destroyWithCode s c = runEffectFn2 destroyWithCodeImpl s c
148141

149-
foreign import destroyWithCodeImpl :: forall endpoint. EffectFn2 (Http2Session endpoint) (Int) (Unit)
142+
foreign import destroyWithCodeImpl :: forall endpoint. EffectFn2 (Http2Session endpoint) (ErrorCode) (Unit)
150143

151-
destroyWithErrorCode :: forall endpoint. Http2Session endpoint -> Error -> Int -> Effect Unit
144+
destroyWithErrorCode :: forall endpoint. Http2Session endpoint -> Error -> ErrorCode -> Effect Unit
152145
destroyWithErrorCode s e c = runEffectFn3 destroyWithErrorCodeImpl s e c
153146

154-
foreign import destroyWithErrorCodeImpl :: forall endpoint. EffectFn3 (Http2Session endpoint) (Error) (Int) (Unit)
147+
foreign import destroyWithErrorCodeImpl :: forall endpoint. EffectFn3 (Http2Session endpoint) (Error) (ErrorCode) (Unit)
155148

156149
destroyed :: forall endpoint. Http2Session endpoint -> Effect Boolean
157150
destroyed s = runEffectFn1 destroyedImpl s
@@ -168,20 +161,20 @@ goAway s = runEffectFn1 goAwayImpl s
168161

169162
foreign import goAwayImpl :: forall endpoint. EffectFn1 (Http2Session endpoint) (Unit)
170163

171-
goAwayCode :: forall endpoint. Http2Session endpoint -> Int -> Effect Unit
164+
goAwayCode :: forall endpoint. Http2Session endpoint -> ErrorCode -> Effect Unit
172165
goAwayCode s c = runEffectFn2 goAwayCodeImpl s c
173166

174-
foreign import goAwayCodeImpl :: forall endpoint. EffectFn2 (Http2Session endpoint) (Int) (Unit)
167+
foreign import goAwayCodeImpl :: forall endpoint. EffectFn2 (Http2Session endpoint) (ErrorCode) (Unit)
175168

176-
goAwayCodeLastStreamId :: forall endpoint. Http2Session endpoint -> Int -> Int -> Effect Unit
169+
goAwayCodeLastStreamId :: forall endpoint. Http2Session endpoint -> ErrorCode -> StreamId -> Effect Unit
177170
goAwayCodeLastStreamId s c lsi = runEffectFn3 goAwayCodeLastStreamIdImpl s c lsi
178171

179-
foreign import goAwayCodeLastStreamIdImpl :: forall endpoint. EffectFn3 (Http2Session endpoint) (Int) (Int) (Unit)
172+
foreign import goAwayCodeLastStreamIdImpl :: forall endpoint. EffectFn3 (Http2Session endpoint) (ErrorCode) (StreamId) (Unit)
180173

181-
goAwayCodeLastStreamIdData :: forall endpoint. Http2Session endpoint -> Int -> Int -> ImmutableBuffer -> Effect Unit
174+
goAwayCodeLastStreamIdData :: forall endpoint. Http2Session endpoint -> ErrorCode -> StreamId -> Buffer -> Effect Unit
182175
goAwayCodeLastStreamIdData s c lsi buf = runEffectFn4 goAwayCodeLastStreamIdOpaqueDataImpl s c lsi buf
183176

184-
foreign import goAwayCodeLastStreamIdOpaqueDataImpl :: forall endpoint. EffectFn4 (Http2Session endpoint) (Int) (Int) (ImmutableBuffer) (Unit)
177+
foreign import goAwayCodeLastStreamIdOpaqueDataImpl :: forall endpoint. EffectFn4 (Http2Session endpoint) (ErrorCode) (StreamId) (Buffer) (Unit)
185178

186179
localSettings :: forall endpoint. Http2Session endpoint -> Effect Settings
187180
localSettings s = runEffectFn1 localSettingsImpl s
@@ -292,15 +285,11 @@ origin s o = runEffectFn2 originImpl s o
292285

293286
foreign import originImpl :: EffectFn2 (Http2Session Server) (Array String) (Unit)
294287

295-
onAltsvc :: Http2Session Client -> (String -> String -> Int -> Effect Unit) -> Effect Unit
296-
onAltsvc s cb = runEffectFn2 onAltsvcImpl s $ mkEffectFn3 cb
297-
298-
foreign import onAltsvcImpl :: EffectFn2 (Http2Session Client) (EffectFn3 String String Int Unit) (Unit)
299-
300-
onOrigin :: Http2Session Client -> (Array String -> Effect Unit) -> Effect Unit
301-
onOrigin s cb = runEffectFn2 onOriginImpl s $ mkEffectFn1 cb
288+
altsvcHandle :: EventHandle3 (Http2Session Client) String String StreamId
289+
altsvcHandle = EventHandle "altsvc" \cb -> mkEffectFn3 \a b c -> cb a b c
302290

303-
foreign import onOriginImpl :: EffectFn2 (Http2Session Client) (EffectFn1 (Array String) Unit) (Unit)
291+
originHandle :: EventHandle1 (Http2Session Client) (Array String)
292+
originHandle = EventHandle "origin" mkEffectFn1
304293

305294
-- | `endStream` <boolean> true if the Http2Stream writable side should be closed initially, such as when sending a GET request that should not expect a payload body.
306295
-- | `exclusive` <boolean> When true and parent identifies a parent Stream, the created stream is made the sole direct dependency of the parent, with all other existing dependents made a dependent of the newly created stream. Default: false.

src/Node/Http2/Types.purs

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@
22
-- | is simply not supported in this library.
33
module Node.Http2.Types where
44

5+
import Prelude
6+
7+
import Data.Generic.Rep (class Generic)
8+
import Data.Newtype (class Newtype)
59
import Node.TLS.Types (Endpoint)
610

711
-- | `Http2Session` extends `EventEmitter`
@@ -12,6 +16,30 @@ foreign import data Http2Stream :: Endpoint -> Type
1216

1317
foreign import data Headers :: Type
1418

19+
newtype FrameType = FrameType Int
20+
21+
derive instance Eq FrameType
22+
derive instance Ord FrameType
23+
derive instance Newtype FrameType _
24+
derive instance Generic FrameType _
25+
derive newtype instance Show FrameType
26+
27+
newtype ErrorCode = ErrorCode Int
28+
29+
derive instance Eq ErrorCode
30+
derive instance Ord ErrorCode
31+
derive instance Newtype ErrorCode _
32+
derive instance Generic ErrorCode _
33+
derive newtype instance Show ErrorCode
34+
35+
newtype StreamId = StreamId Int
36+
37+
derive instance Eq StreamId
38+
derive instance Ord StreamId
39+
derive instance Newtype StreamId _
40+
derive instance Generic StreamId _
41+
derive newtype instance Show StreamId
42+
1543
-- | `headerTableSize` <number> Specifies the maximum number of bytes used for header compression. The minimum allowed value is 0. The maximum allowed value is 232-1. Default: 4096.
1644
-- | `enablePush` <boolean> Specifies true if HTTP/2 Push Streams are to be permitted on the Http2Session instances. Default: true.
1745
-- | `initialWindowSize` <number> Specifies the sender's initial window size in bytes for stream-level flow control. The minimum allowed value is 0. The maximum allowed value is 232-1. Default: 65535.

test/Test/Main.purs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ main = do
9090
session <- Client.connect' ("https://localhost:" <> show httpsPort)
9191
{ ca: [ cert ]
9292
}
93-
Session.onError session \error ->
93+
on Session.errorHandle session \error ->
9494
log $ "Client session encountered error: " <> Exception.message error
9595
stream <- Session.request session
9696
( unsafeCoerce

0 commit comments

Comments
 (0)