Skip to content

Commit 78c7e92

Browse files
Update Stream to EE events
1 parent fadc883 commit 78c7e92

File tree

4 files changed

+66
-97
lines changed

4 files changed

+66
-97
lines changed
Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,18 @@
11
module Node.Http2.Constants.NGHTTP2 where
22

3-
foreign import noError :: Int
4-
foreign import protocolError :: Int
5-
foreign import internalError :: Int
6-
foreign import flowControlError :: Int
7-
foreign import settingsTimeout :: Int
8-
foreign import streamClosed :: Int
9-
foreign import frameSizeError :: Int
10-
foreign import refusedStream :: Int
11-
foreign import cancel :: Int
12-
foreign import compressionError :: Int
13-
foreign import connectError :: Int
14-
foreign import enhanceYourCalm :: Int
15-
foreign import inadequateSecurity :: Int
16-
foreign import http1_1Required :: Int
3+
import Node.Http2.Types (ErrorCode)
4+
5+
foreign import noError :: ErrorCode
6+
foreign import protocolError :: ErrorCode
7+
foreign import internalError :: ErrorCode
8+
foreign import flowControlError :: ErrorCode
9+
foreign import settingsTimeout :: ErrorCode
10+
foreign import streamClosed :: ErrorCode
11+
foreign import frameSizeError :: ErrorCode
12+
foreign import refusedStream :: ErrorCode
13+
foreign import cancel :: ErrorCode
14+
foreign import compressionError :: ErrorCode
15+
foreign import connectError :: ErrorCode
16+
foreign import enhanceYourCalm :: ErrorCode
17+
foreign import inadequateSecurity :: ErrorCode
18+
foreign import http1_1Required :: ErrorCode

src/Node/Http2/Stream.js

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,3 @@
1-
export const onAbortImpl = (stream, cb) => stream.on("abort", cb);
2-
export const onCloseImpl = (stream, cb) => stream.on("close", cb);
3-
export const onErrorImpl = (stream, cb) => stream.on("error", cb);
4-
export const onFrameErrorImpl = (stream, cb) => stream.on("frameError", cb);
5-
export const onReadyImpl = (stream, cb) => stream.on("ready", cb);
6-
export const onTimeoutImpl = (stream, cb) => stream.on("timeout", cb);
7-
export const onTrailersImpl = (stream, cb) => stream.on("trailers", cb);
8-
export const onWantTrailersImpl = (stream, cb) => stream.on("wantTrailers", cb);
91
export const bufferSizeImpl = (stream) => stream.bufferSize;
102
export const closeImpl = (stream, code) => stream.close(code);
113
export const closedImpl = (stream) => stream.closed;
@@ -34,10 +26,5 @@ export const respondImpl = (s, headers, options) => s.respond(headers, options);
3426
export const respondWithFdImpl = (s, fd, h, o) => s.respondWithFd(fd, h, o);
3527
export const respondWithFileImpl = (s, file, headers, o) => s.respondWithFile(file, headers, o);
3628

37-
3829
// client
39-
export const onContinueImpl = (s, cb) => s.on("continue", cb);
40-
export const onHeadersImpl = (s, cb) => s.on("headers", cb);
41-
export const onPushImpl = (s, cb) => s.on("push", cb);
42-
export const onResponseImpl = (s, cb) => s.on("response", cb);
4330

src/Node/Http2/Stream.purs

Lines changed: 49 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
module Node.Http2.Stream
22
( toDuplex
3-
, onAbort
4-
, onClose
5-
, onError
6-
, onFrameError
7-
, onReady
8-
, onTimeout
9-
, onTrailers
10-
, onWantTrailers
3+
, abortedHandle
4+
, closeHandle
5+
, errorHandle
6+
, frameErrorHandle
7+
, readyHandle
8+
, timeoutHandle
9+
, trailersHandle
10+
, wantTrailersHandle
1111
, bufferSize
1212
, close
1313
, closed
@@ -38,10 +38,10 @@ module Node.Http2.Stream
3838
, respondWithFd
3939
, RespondWithFileOptions
4040
, respondWithFile
41-
, onContinue
42-
, onHeaders
43-
, onPush
44-
, onResponse
41+
, continueHandle
42+
, headersHandle
43+
, pushHandle
44+
, responseHandle
4545
) where
4646

4747
import Prelude
@@ -53,64 +53,52 @@ import Data.Time.Duration (Milliseconds)
5353
import Effect (Effect)
5454
import Effect.Exception (Error)
5555
import Effect.Uncurried (EffectFn1, EffectFn2, EffectFn3, EffectFn4, mkEffectFn1, mkEffectFn2, mkEffectFn3, runEffectFn1, runEffectFn2, runEffectFn3, runEffectFn4)
56+
import Node.EventEmitter (EventHandle(..))
57+
import Node.EventEmitter.UtilTypes (EventHandle0, EventHandle1, EventHandle3, EventHandle2)
5658
import Node.FS (FileDescriptor)
57-
import Node.Http2.Types (Headers, Http2Session, Http2Stream)
58-
import Node.TLS.Types (Client, Server)
59+
import Node.Http2.Flags (BitwiseFlag)
60+
import Node.Http2.Types (ErrorCode(..), FrameType, Headers, Http2Session, Http2Stream, Settings, StreamId(..))
5961
import Node.Path (FilePath)
6062
import Node.Stream (Duplex)
63+
import Node.TLS.Types (Client, Server)
6164
import Partial.Unsafe (unsafeCrashWith)
65+
import Safe.Coerce (coerce)
6266
import Unsafe.Coerce (unsafeCoerce)
6367

6468
toDuplex :: forall endpoint. Http2Stream endpoint -> Duplex
6569
toDuplex = unsafeCoerce
6670

67-
onAbort :: forall endpoint. Http2Stream endpoint -> Effect Unit -> Effect Unit
68-
onAbort s cb = runEffectFn2 onAbortImpl s cb
69-
70-
foreign import onAbortImpl :: forall endpoint. EffectFn2 (Http2Stream endpoint) (Effect Unit) Unit
71-
72-
onClose :: forall endpoint. Http2Stream endpoint -> Effect Unit -> Effect Unit
73-
onClose s cb = runEffectFn2 onCloseImpl s cb
74-
75-
foreign import onCloseImpl :: forall endpoint. EffectFn2 (Http2Stream endpoint) (Effect Unit) (Unit)
76-
77-
onError :: forall endpoint. Http2Stream endpoint -> (Error -> Effect Unit) -> Effect Unit
78-
onError s cb = runEffectFn2 onErrorImpl s $ mkEffectFn1 cb
79-
80-
foreign import onErrorImpl :: forall endpoint. EffectFn2 (Http2Stream endpoint) (EffectFn1 Error Unit) (Unit)
71+
abortedHandle :: forall endpoint. EventHandle0 (Http2Stream endpoint)
72+
abortedHandle = EventHandle "aborted" identity
8173

82-
onFrameError :: forall endpoint. Http2Stream endpoint -> (Int -> Int -> Int -> Effect Unit) -> Effect Unit
83-
onFrameError s cb = runEffectFn2 onFrameErrorImpl s $ mkEffectFn3 cb
74+
closeHandle :: forall endpoint. EventHandle0 (Http2Stream endpoint)
75+
closeHandle = EventHandle "close" identity
8476

85-
foreign import onFrameErrorImpl :: forall endpoint. EffectFn2 (Http2Stream endpoint) (EffectFn3 Int Int Int Unit) (Unit)
77+
errorHandle :: forall endpoint. EventHandle1 (Http2Stream endpoint) Error
78+
errorHandle = EventHandle "error" mkEffectFn1
8679

87-
onReady :: forall endpoint. Http2Stream endpoint -> Effect Unit -> Effect Unit
88-
onReady s cb = runEffectFn2 onReadyImpl s cb
80+
frameErrorHandle :: forall endpoint. EventHandle3 (Http2Stream endpoint) FrameType ErrorCode StreamId
81+
frameErrorHandle = EventHandle "frameError" \cb -> mkEffectFn3 \a b c -> cb a b c
8982

90-
foreign import onReadyImpl :: forall endpoint. EffectFn2 (Http2Stream endpoint) (Effect Unit) (Unit)
83+
readyHandle :: forall endpoint. EventHandle0 (Http2Stream endpoint)
84+
readyHandle = EventHandle "ready" identity
9185

92-
onTimeout :: forall endpoint. Http2Stream endpoint -> Effect Unit -> Effect Unit
93-
onTimeout s cb = runEffectFn2 onTimeoutImpl s cb
86+
timeoutHandle :: forall endpoint. EventHandle0 (Http2Stream endpoint)
87+
timeoutHandle = EventHandle "timeout" identity
9488

95-
foreign import onTimeoutImpl :: forall endpoint. EffectFn2 (Http2Stream endpoint) (Effect Unit) (Unit)
89+
trailersHandle :: forall endpoint. EventHandle2 (Http2Stream endpoint) Settings BitwiseFlag
90+
trailersHandle = EventHandle "trailers" \cb -> mkEffectFn2 \a b -> cb a b
9691

97-
onTrailers :: forall endpoint. Http2Stream endpoint -> (Headers -> Int -> Effect Unit) -> Effect Unit
98-
onTrailers s cb = runEffectFn2 onTrailersImpl s $ mkEffectFn2 cb
99-
100-
foreign import onTrailersImpl :: forall endpoint. EffectFn2 (Http2Stream endpoint) (EffectFn2 Headers Int Unit) (Unit)
101-
102-
onWantTrailers :: forall endpoint. Http2Stream endpoint -> Effect Unit -> Effect Unit
103-
onWantTrailers s cb = runEffectFn2 onWantTrailersImpl s cb
104-
105-
foreign import onWantTrailersImpl :: forall endpoint. EffectFn2 (Http2Stream endpoint) (Effect Unit) (Unit)
92+
wantTrailersHandle :: forall endpoint. EventHandle0 (Http2Stream endpoint)
93+
wantTrailersHandle = EventHandle "wantTrailers" identity
10694

10795
bufferSize :: forall endpoint. Http2Stream endpoint -> Effect Int
10896
bufferSize s = runEffectFn1 bufferSizeImpl s
10997

11098
foreign import bufferSizeImpl :: forall endpoint. EffectFn1 (Http2Stream endpoint) (Int)
11199

112-
close :: forall endpoint. Http2Stream endpoint -> Int -> Effect Unit
113-
close s code = runEffectFn2 closeImpl s code
100+
close :: forall endpoint. Http2Stream endpoint -> ErrorCode -> Effect Unit
101+
close s code = runEffectFn2 closeImpl s (coerce code)
114102

115103
foreign import closeImpl :: forall endpoint. EffectFn2 (Http2Stream endpoint) Int (Unit)
116104

@@ -129,8 +117,8 @@ endAfterHeaders s = runEffectFn1 endAfterHeadersImpl s
129117

130118
foreign import endAfterHeadersImpl :: forall endpoint. EffectFn1 (Http2Stream endpoint) (Boolean)
131119

132-
id :: forall endpoint. Http2Stream endpoint -> Effect (Maybe Int)
133-
id s = map toMaybe $ runEffectFn1 idImpl s
120+
id :: forall endpoint. Http2Stream endpoint -> Effect (Maybe StreamId)
121+
id s = map (coerce <<< toMaybe) $ runEffectFn1 idImpl s
134122

135123
foreign import idImpl :: forall endpoint. EffectFn1 (Http2Stream endpoint) (Nullable Int)
136124

@@ -156,8 +144,8 @@ priority s p = runEffectFn2 priorityImpl s $ p { weight = clamp 1 256 p.weight }
156144

157145
foreign import priorityImpl :: forall endpoint. EffectFn2 (Http2Stream endpoint) (PriorityOptions) (Unit)
158146

159-
rstCode :: forall endpoint. Http2Stream endpoint -> Effect (Maybe Int)
160-
rstCode s = map toMaybe $ runEffectFn1 rstCodeImpl s
147+
rstCode :: forall endpoint. Http2Stream endpoint -> Effect (Maybe ErrorCode)
148+
rstCode s = map (coerce <<< toMaybe) $ runEffectFn1 rstCodeImpl s
161149

162150
foreign import rstCodeImpl :: forall endpoint. EffectFn1 (Http2Stream endpoint) (Nullable Int)
163151

@@ -194,7 +182,7 @@ foreign import setTimeoutImpl :: forall endpoint. EffectFn3 (Http2Stream endpoin
194182
-- | `weight` <number> The priority weight of this Http2Stream peer.
195183
type Http2StreamState =
196184
{ localWindowSize :: Int
197-
, state :: Int
185+
, state :: BitwiseFlag
198186
, localClose :: Int
199187
, remoteClose :: Int
200188
, sumDependencyWeight :: Int
@@ -295,23 +283,15 @@ respondWithFile s fp h o = runEffectFn4 respondWithFileImpl s fp h o
295283

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

298-
onContinue :: Http2Stream Client -> Effect Unit -> Effect Unit
299-
onContinue s cb = runEffectFn2 onContinueImpl s cb
300-
301-
foreign import onContinueImpl :: EffectFn2 (Http2Stream Client) (Effect Unit) (Unit)
302-
303-
onHeaders :: Http2Stream Client -> (Headers -> Int -> Effect Unit) -> Effect Unit
304-
onHeaders s cb = runEffectFn2 onHeadersImpl s $ mkEffectFn2 cb
305-
306-
foreign import onHeadersImpl :: EffectFn2 (Http2Stream Client) (EffectFn2 Headers Int Unit) (Unit)
307-
308-
onPush :: Http2Stream Client -> (Headers -> Int -> Effect Unit) -> Effect Unit
309-
onPush s cb = runEffectFn2 onPushImpl s $ mkEffectFn2 cb
286+
continueHandle :: EventHandle0 (Http2Stream Client)
287+
continueHandle = EventHandle "continue" identity
310288

311-
foreign import onPushImpl :: EffectFn2 (Http2Stream Client) (EffectFn2 Headers Int Unit) (Unit)
289+
headersHandle :: EventHandle2 (Http2Stream Client) Headers BitwiseFlag
290+
headersHandle = EventHandle "headers" \cb -> mkEffectFn2 \a b -> cb a b
312291

313-
onResponse :: Http2Stream Client -> (Headers -> Int -> Effect Unit) -> Effect Unit
314-
onResponse s cb = runEffectFn2 onResponseImpl s $ mkEffectFn2 cb
292+
pushHandle :: EventHandle2 (Http2Stream Client) Headers BitwiseFlag
293+
pushHandle = EventHandle "push" \cb -> mkEffectFn2 \a b -> cb a b
315294

316-
foreign import onResponseImpl :: EffectFn2 (Http2Stream Client) (EffectFn2 Headers Int Unit) (Unit)
295+
responseHandle :: EventHandle2 (Http2Stream Client) Headers BitwiseFlag
296+
responseHandle = EventHandle "response" \cb -> mkEffectFn2 \a b -> cb a b
317297

test/Test/Main.purs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ main = do
100100
)
101101
let duplex = toDuplex stream
102102
Stream.end duplex
103-
H2Stream.onResponse stream \headers flags -> do
103+
on H2Stream.responseHandle stream \headers flags -> do
104104
log "client - onResponse"
105105
forWithIndex_ (unsafeCoerce headers :: Object String) \k v ->
106106
log $ k <> ": " <> v

0 commit comments

Comments
 (0)