Skip to content

Commit acd81d4

Browse files
committed
Type-annotate all unsafeCoerce JavaScript object casts
1 parent c972482 commit acd81d4

File tree

8 files changed

+116
-189
lines changed

8 files changed

+116
-189
lines changed

src/Node/HTTP2/Client.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,13 @@ export const onceReady = socket => callback => () => {
2020
return () => socket.removeEventListener("ready", callback);
2121
};
2222

23+
// https://nodejs.org/docs/latest/api/http2.html#event-stream
24+
export const onceStream = foreign => callback => () => {
25+
const cb = (stream, headers, flags) => callback(stream)(headers)(flags)();
26+
foreign.once("stream", cb);
27+
return () => {foreign.removeListener("stream", cb);};
28+
};
29+
2330
// https://nodejs.org/docs/latest/api/http2.html#clienthttp2sessionrequestheaders-options
2431
export const request = clienthttp2session => headers => options => () => {
2532
return clienthttp2session.request(headers, options);

src/Node/HTTP2/Client.purs

Lines changed: 27 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,6 @@ module Node.HTTP2.Client
4141
, request
4242
, onceErrorSession
4343
, onceResponse
44-
, onStream
4544
, onceStream
4645
, onceHeaders
4746
, closeSession
@@ -64,6 +63,7 @@ import Effect (Effect)
6463
import Effect.Exception (Error)
6564
import Node.Buffer (Buffer)
6665
import Node.HTTP2 (Flags, HeadersObject, OptionsObject)
66+
import Node.HTTP2.Internal (Http2Session, Http2Stream)
6767
import Node.HTTP2.Internal as Internal
6868
import Node.Net.Socket (Socket)
6969
import Node.Stream (Duplex)
@@ -75,6 +75,9 @@ import Unsafe.Coerce (unsafeCoerce)
7575
-- | See [__Class: ClientHttp2Session__](https://nodejs.org/docs/latest/api/http2.html#class-clienthttp2session)
7676
foreign import data ClientHttp2Session :: Type
7777

78+
upcastClientHttp2Session :: ClientHttp2Session -> Http2Session
79+
upcastClientHttp2Session = unsafeCoerce
80+
7881
-- | https://nodejs.org/docs/latest/api/http2.html#http2connectauthority-options-listener
7982
foreign import connect :: URL -> OptionsObject -> (ClientHttp2Session -> Socket -> Effect Unit) -> Effect ClientHttp2Session
8083

@@ -89,6 +92,9 @@ foreign import onceReady :: Socket -> (Effect Unit) -> Effect (Effect Unit)
8992
-- | See [__Class: ClientHttp2Stream__](https://nodejs.org/docs/latest/api/http2.html#class-clienthttp2stream)
9093
foreign import data ClientHttp2Stream :: Type
9194

95+
upcastClientHttp2Stream :: ClientHttp2Stream -> Http2Stream
96+
upcastClientHttp2Stream = unsafeCoerce
97+
9298
-- |https://nodejs.org/docs/latest/api/http2.html#clienthttp2sessionrequestheaders-options
9399
foreign import request :: ClientHttp2Session -> HeadersObject -> OptionsObject -> Effect ClientHttp2Stream
94100

@@ -97,19 +103,19 @@ foreign import destroy :: ClientHttp2Stream -> Effect Unit
97103

98104
-- | https://nodejs.org/docs/latest/api/http2.html#http2sessionclosecallback
99105
closeSession :: ClientHttp2Session -> Effect Unit -> Effect Unit
100-
closeSession http2session = Internal.closeSession (unsafeCoerce http2session)
106+
closeSession http2session = Internal.closeSession (upcastClientHttp2Session http2session)
101107

102108
-- | https://nodejs.org/docs/latest/api/http2.html#event-response
103109
-- |
104-
-- | Listen for one event, then remove the event listener.
110+
-- | Listen for one event, call the callback, then remove the event listener.
105111
-- |
106112
-- | Returns an effect for removing the event listener before the event
107113
-- | is raised.
108114
foreign import onceResponse :: ClientHttp2Stream -> (HeadersObject -> Flags -> Effect Unit) -> Effect (Effect Unit)
109115

110116
-- | https://nodejs.org/docs/latest/api/http2.html#event-headers
111117
-- |
112-
-- | Listen for one event, then remove the event listener.
118+
-- | Listen for one event, call the callback, then remove the event listener.
113119
-- |
114120
-- | Returns an effect for removing the event listener before the event
115121
-- | is raised.
@@ -119,38 +125,31 @@ foreign import onceHeaders :: ClientHttp2Stream -> (HeadersObject -> Flags -> Ef
119125
-- |
120126
-- | https://nodejs.org/docs/latest/api/http2.html#push-streams-on-the-client
121127
-- |
122-
-- | Listen for one event, then remove the event listener.
128+
-- | Listen for one event, call the callback, then remove the event listener.
123129
-- |
124130
-- | Returns an effect for removing the event listener before the event
125-
-- | is raised.
126-
onceStream :: ClientHttp2Session -> (ClientHttp2Stream -> HeadersObject -> Flags -> Effect Unit) -> Effect (Effect Unit)
127-
onceStream http2session callback = Internal.onceStream (unsafeCoerce http2session) (\http2stream -> callback (unsafeCoerce http2stream))
128-
129-
-- | https://nodejs.org/docs/latest/api/http2.html#event-stream
130131
-- |
131-
-- | https://nodejs.org/docs/latest/api/http2.html#push-streams-on-the-client
132-
-- |
133-
-- | Returns an effect for removing the event listener.
134-
onStream :: ClientHttp2Session -> (ClientHttp2Stream -> HeadersObject -> Flags -> Effect Unit) -> Effect (Effect Unit)
135-
onStream http2session callback = Internal.onStream (unsafeCoerce http2session) (\http2stream -> callback (unsafeCoerce http2stream))
132+
-- | https://nodejs.org/docs/latest/api/http2.html#event-stream
133+
-- | is raised.
134+
foreign import onceStream :: ClientHttp2Session -> (ClientHttp2Stream -> HeadersObject -> Flags -> Effect Unit) -> Effect (Effect Unit)
136135

137136
-- | https://nodejs.org/docs/latest/api/http2.html#event-error
138137
-- |
139-
-- | Listen for one event, then remove the event listener.
138+
-- | Listen for one event, call the callback, then remove the event listener.
140139
-- |
141140
-- | Returns an effect for removing the event listener before the event
142141
-- | is raised.
143142
onceErrorSession :: ClientHttp2Session -> (Error -> Effect Unit) -> Effect (Effect Unit)
144-
onceErrorSession http2session = Internal.onceEmitterError (unsafeCoerce http2session)
143+
onceErrorSession http2session = Internal.onceSessionEmitterError (upcastClientHttp2Session http2session)
145144

146145
-- | https://nodejs.org/docs/latest/api/http2.html#event-error_1
147146
-- |
148-
-- | Listen for one event, then remove the event listener.
147+
-- | Listen for one event, call the callback, then remove the event listener.
149148
-- |
150149
-- | Returns an effect for removing the event listener before the event
151150
-- | is raised.
152151
onceErrorStream :: ClientHttp2Stream -> (Error -> Effect Unit) -> Effect (Effect Unit)
153-
onceErrorStream http2stream = Internal.onceEmitterError (unsafeCoerce http2stream)
152+
onceErrorStream http2stream = Internal.onceStreamEmitterError (upcastClientHttp2Stream http2stream)
154153

155154
-- | https://nodejs.org/docs/latest/api/http2.html#event-push
156155
-- |
@@ -159,46 +158,46 @@ foreign import oncePush :: ClientHttp2Stream -> (HeadersObject -> Flags -> Effec
159158

160159
-- | https://nodejs.org/docs/latest/api/http2.html#event-trailers
161160
-- |
162-
-- | Listen for one event, then remove the event listener.
161+
-- | Listen for one event, call the callback, then remove the event listener.
163162
-- |
164163
-- | Returns an effect for removing the event listener before the event
165164
-- | is raised.
166165
onceTrailers :: ClientHttp2Stream -> (HeadersObject -> Flags -> Effect Unit) -> Effect (Effect Unit)
167-
onceTrailers http2stream = Internal.onceTrailers (unsafeCoerce http2stream)
166+
onceTrailers http2stream = Internal.onceTrailers (upcastClientHttp2Stream http2stream)
168167

169168
-- | https://nodejs.org/docs/latest/api/http2.html#event-wanttrailers
170169
-- |
171-
-- | Listen for one event, then remove the event listener.
170+
-- | Listen for one event, call the callback, then remove the event listener.
172171
-- |
173172
-- | Returns an effect for removing the event listener before the event
174173
-- | is raised.
175174
onceWantTrailers :: ClientHttp2Stream -> Effect Unit -> Effect (Effect Unit)
176-
onceWantTrailers http2stream = Internal.onceWantTrailers (unsafeCoerce http2stream)
175+
onceWantTrailers http2stream = Internal.onceWantTrailers (upcastClientHttp2Stream http2stream)
177176

178177
-- | https://nodejs.org/docs/latest/api/http2.html#http2streamsendtrailersheaders
179178
-- |
180179
-- | > When sending a request or sending a response, the `options.waitForTrailers` option must be set in order to keep the `Http2Stream` open after the final `DATA` frame so that trailers can be sent.
181180
sendTrailers :: ClientHttp2Stream -> HeadersObject -> Effect Unit
182-
sendTrailers http2stream = Internal.sendTrailers (unsafeCoerce http2stream)
181+
sendTrailers http2stream = Internal.sendTrailers (upcastClientHttp2Stream http2stream)
183182

184183
-- | https://nodejs.org/docs/latest/api/stream.html#event-data
185184
-- |
186185
-- | Returns an effect for removing the event listener.
187186
onData :: ClientHttp2Stream -> (Buffer -> Effect Unit) -> Effect (Effect Unit)
188-
onData http2stream = Internal.onData (unsafeCoerce http2stream)
187+
onData http2stream = Internal.onData (upcastClientHttp2Stream http2stream)
189188

190189
-- | https://nodejs.org/docs/latest/api/net.html#event-end
191190
-- |
192-
-- | Listen for one event, then remove the event listener.
191+
-- | Listen for one event, call the callback, then remove the event listener.
193192
-- |
194193
-- | Returns an effect for removing the event listener before the event
195194
-- | is raised.
196195
onceEnd :: ClientHttp2Stream -> Effect Unit -> Effect (Effect Unit)
197-
onceEnd http2stream = Internal.onceEnd (unsafeCoerce http2stream)
196+
onceEnd http2stream = Internal.onceEnd (upcastClientHttp2Stream http2stream)
198197

199198
-- | https://nodejs.org/docs/latest/api/http2.html#http2streamclosecode-callback
200199
closeStream :: ClientHttp2Stream -> Int -> Effect Unit -> Effect Unit
201-
closeStream stream = Internal.closeStream (unsafeCoerce stream)
200+
closeStream stream = Internal.closeStream (upcastClientHttp2Stream stream)
202201

203202
-- | Coerce to a duplex stream.
204203
toDuplex :: ClientHttp2Stream -> Duplex

src/Node/HTTP2/Internal.js

Lines changed: 8 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -18,63 +18,31 @@ export const closeSession = http2session => callback => () => {
1818
}
1919
};
2020

21-
// https://nodejs.org/docs/latest/api/http2.html#serverclosecallback
22-
export const closeServer = http2server => callback => () => {
23-
http2server.close(() => callback());
24-
};
25-
2621
// https://nodejs.org/docs/latest/api/http2.html#event-close_1
2722
export const onceClose = http2stream => callback => () => {
2823
const cb = () => callback(http2stream.rstCode)();
2924
http2stream.once("close", cb);
3025
return () => {http2stream.removeEventListener("close", cb);};
3126
};
3227

33-
// https://nodejs.org/docs/latest/api/http2.html#event-stream
34-
export const onceStream = foreign => callback => () => {
35-
const cb = (stream, headers, flags) => callback(stream)(headers)(flags)();
36-
foreign.once("stream", cb);
37-
return () => {foreign.removeListener("stream", cb);};
38-
};
39-
40-
// https://nodejs.org/docs/latest/api/http2.html#event-stream
41-
export const onStream = foreign => callback => () => {
42-
const cb = (stream, headers, flags) => callback(stream)(headers)(flags)();
43-
foreign.on("stream", cb);
44-
return () => {foreign.removeListener("stream", cb);};
45-
};
46-
47-
// https://nodejs.org/docs/latest/api/events.html#nodeeventtargetoncetype-listener-options
48-
export const onceError = eventtarget => callback => () => {
49-
const cb = error => callback(error)();
50-
eventtarget.once("error", cb);
51-
return () => {eventtarget.removeEventListener("error", cb);};
52-
};
53-
54-
// https://nodejs.org/docs/latest/api/net.html#event-close
55-
export const onceServerClose = server => callback => () => {
56-
const cb = () => callback();
57-
server.once("close", cb);
58-
return () => {server.removeEventListener("close", cb);};
59-
};
60-
6128
// https://nodejs.org/docs/latest/api/events.html#emitteronceeventname-listener
62-
export const onceEmitterError = eventemitter => callback => () => {
29+
export const onceStreamEmitterError = eventemitter => callback => () => {
6330
const cb = error => callback(error)();
6431
eventemitter.once("error", cb);
6532
return () => {eventemitter.removeListener("error", cb);};
6633
};
6734

68-
export const onEmitterError = eventemitter => callback => () => {
35+
// https://nodejs.org/docs/latest/api/events.html#emitteronceeventname-listener
36+
//
37+
// Same as onceStreamEmitterError.
38+
// During PR review it was requested that there be no unsafeCoerce, so this
39+
// function is duplicated.
40+
export const onceSessionEmitterError = eventemitter => callback => () => {
6941
const cb = error => callback(error)();
70-
eventemitter.on("error", cb);
42+
eventemitter.once("error", cb);
7143
return () => {eventemitter.removeListener("error", cb);};
7244
};
7345

74-
export const throwAllErrors = eventtarget => () => {
75-
eventtarget.addEventListener("error", error => {throw error;});
76-
};
77-
7846
export const onceWantTrailers = http2stream => callback => () => {
7947
const cb = () => callback();
8048
http2stream.once("wantTrailers", cb);
@@ -104,10 +72,6 @@ export const onceEnd = netsocket => callback => () => {
10472
return () => {netsocket.removeListener("end", cb);};
10573
};
10674

107-
export const session = http2stream => {
108-
return http2stream.session;
109-
};
110-
11175
// https://nodejs.org/docs/latest/api/http2.html#http2streamclosecode-callback
11276
export const closeStream = http2stream => code => callback => () => {
11377
http2stream.close(code, () => callback());

src/Node/HTTP2/Internal.purs

Lines changed: 3 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ import Prelude
77

88
import Effect (Effect)
99
import Effect.Exception (Error)
10-
import Foreign (Foreign)
1110
import Node.Buffer (Buffer)
1211
import Node.HTTP2 (Flags, HeadersObject, OptionsObject, SettingsObject)
1312
import Node.HTTP2.Constants (NGHTTP2)
@@ -25,59 +24,23 @@ foreign import data Http2Session :: Type
2524
-- | https://nodejs.org/api/http2.html#http2sessionlocalsettings
2625
foreign import localSettings :: Http2Session -> Effect SettingsObject
2726

28-
-- | Listen for one event, call the callback, then remove
27+
-- | Listen for one EventEmitter `'error'`, call the callback, then remove
2928
-- | the event listener.
30-
-- | Returns an effect for removing the event listener before the event
31-
-- | is raised.
3229
-- |
33-
-- | https://nodejs.org/docs/latest/api/http2.html#event-stream
34-
foreign import onceStream :: Foreign -> (Http2Stream -> HeadersObject -> Flags -> Effect Unit) -> Effect (Effect Unit)
35-
36-
-- | https://nodejs.org/docs/latest/api/http2.html#event-stream
37-
foreign import onStream :: Foreign -> (Http2Stream -> HeadersObject -> Flags -> Effect Unit) -> Effect (Effect Unit)
38-
39-
-- | Listen for one NodeEventTarget `'error'`, call the callback, then remove
40-
-- | the event listener.
4130
-- | Returns an effect for removing the event listener before the event
4231
-- | is raised.
43-
foreign import onceError :: Foreign -> (Error -> Effect Unit) -> Effect (Effect Unit)
32+
foreign import onceStreamEmitterError :: Http2Stream -> (Error -> Effect Unit) -> Effect (Effect Unit)
4433

4534
-- | Listen for one EventEmitter `'error'`, call the callback, then remove
4635
-- | the event listener.
47-
-- | Returns an effect for removing the event listener before the event
48-
-- | is raised.
49-
foreign import onceEmitterError :: Foreign -> (Error -> Effect Unit) -> Effect (Effect Unit)
50-
51-
-- | EventEmitter `on 'error'`
5236
-- |
5337
-- | Returns an effect for removing the event listener before the event
5438
-- | is raised.
55-
foreign import onEmitterError :: Foreign -> (Error -> Effect Unit) -> Effect (Effect Unit)
39+
foreign import onceSessionEmitterError :: Http2Session -> (Error -> Effect Unit) -> Effect (Effect Unit)
5640

5741
-- | https://nodejs.org/docs/latest/api/http2.html#http2sessionclosecallback
5842
foreign import closeSession :: Http2Session -> Effect Unit -> Effect Unit
5943

60-
-- | https://nodejs.org/docs/latest/api/http2.html#serverclosecallback
61-
foreign import closeServer :: Foreign -> Effect Unit -> Effect Unit
62-
63-
-- | https://nodejs.org/docs/latest/api/net.html#event-close
64-
-- |
65-
-- | Returns an effect for removing the event listener before the event
66-
-- | is raised.
67-
foreign import onceServerClose :: Foreign -> Effect Unit -> Effect (Effect Unit)
68-
69-
-- | To an `EventTarget` attach an `'error'` listener which will always throw
70-
-- | a synchronous `Error`.
71-
-- |
72-
-- | https://nodejs.org/docs/latest/api/http2.html#error-handling
73-
-- |
74-
-- | > (Errors) will be reported using either a synchronous throw or via
75-
-- | > an 'error' event on the `Http2Stream`, `Http2Session` or
76-
-- | > `Http2Server` objects, depending on where and when the error occurs.
77-
-- |
78-
-- | https://nodejs.org/api/events.html#eventtargetaddeventlistenertype-listener-options
79-
foreign import throwAllErrors :: Foreign -> Effect Unit
80-
8144
-- | Private type which can be coerced into ClientHttp2Stream
8245
-- | or ServerHttp2Stream or Duplex.
8346
-- |
@@ -105,8 +68,5 @@ foreign import onData :: Http2Stream -> (Buffer -> Effect Unit) -> Effect (Effec
10568
-- | https://nodejs.org/docs/latest/api/net.html#event-end
10669
foreign import onceEnd :: Http2Stream -> Effect Unit -> Effect (Effect Unit)
10770

108-
-- | https://nodejs.org/api/http2.html#http2streamsession
109-
foreign import session :: Http2Stream -> Http2Session
110-
11171
-- | https://nodejs.org/docs/latest/api/http2.html#http2streamclosecode-callback
11272
foreign import closeStream :: Http2Stream -> Int -> Effect Unit -> Effect Unit

src/Node/HTTP2/Server.js

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,33 @@ export const listen = server => options => callback => () => {
1616
server.listen(options, () => callback());
1717
};
1818

19-
export const onceSession = http2server => callback => () => {
20-
const cb = session => callback(session)();
21-
http2server.once("session", cb);
22-
return () => http2server.removeEventListener("session", cb);
19+
// https://nodejs.org/docs/latest/api/http2.html#serverclosecallback
20+
export const closeServer = http2server => callback => () => {
21+
http2server.close(() => callback());
22+
};
23+
24+
// https://nodejs.org/docs/latest/api/net.html#event-close
25+
export const onceServerClose = server => callback => () => {
26+
const cb = () => callback();
27+
server.once("close", cb);
28+
return () => {server.removeEventListener("close", cb);};
29+
};
30+
31+
export const onEmitterError = eventemitter => callback => () => {
32+
const cb = error => callback(error)();
33+
eventemitter.on("error", cb);
34+
return () => {eventemitter.removeListener("error", cb);};
35+
};
36+
37+
export const session = http2stream => {
38+
return http2stream.session;
39+
};
40+
41+
// https://nodejs.org/docs/latest/api/http2.html#event-stream
42+
export const onStream = http2server => callback => () => {
43+
const cb = (stream, headers, flags) => callback(stream)(headers)(flags)();
44+
http2server.on("stream", cb);
45+
return () => {http2server.removeListener("stream", cb);};
2346
};
2447

2548
// https://nodejs.org/docs/latest/api/http2.html#http2streampushallowed

0 commit comments

Comments
 (0)