Skip to content

Commit c972482

Browse files
committed
unsafeCoerce only when upcast a JavaScript object to its superclass.
1 parent e3fd62f commit c972482

File tree

2 files changed

+33
-33
lines changed

2 files changed

+33
-33
lines changed

src/Node/HTTP2/Client.purs

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ foreign import destroy :: ClientHttp2Stream -> Effect Unit
9797

9898
-- | https://nodejs.org/docs/latest/api/http2.html#http2sessionclosecallback
9999
closeSession :: ClientHttp2Session -> Effect Unit -> Effect Unit
100-
closeSession = unsafeCoerce Internal.closeSession
100+
closeSession http2session = Internal.closeSession (unsafeCoerce http2session)
101101

102102
-- | https://nodejs.org/docs/latest/api/http2.html#event-response
103103
-- |
@@ -124,15 +124,15 @@ foreign import onceHeaders :: ClientHttp2Stream -> (HeadersObject -> Flags -> Ef
124124
-- | Returns an effect for removing the event listener before the event
125125
-- | is raised.
126126
onceStream :: ClientHttp2Session -> (ClientHttp2Stream -> HeadersObject -> Flags -> Effect Unit) -> Effect (Effect Unit)
127-
onceStream = unsafeCoerce Internal.onceStream
127+
onceStream http2session callback = Internal.onceStream (unsafeCoerce http2session) (\http2stream -> callback (unsafeCoerce http2stream))
128128

129129
-- | https://nodejs.org/docs/latest/api/http2.html#event-stream
130130
-- |
131131
-- | https://nodejs.org/docs/latest/api/http2.html#push-streams-on-the-client
132132
-- |
133133
-- | Returns an effect for removing the event listener.
134134
onStream :: ClientHttp2Session -> (ClientHttp2Stream -> HeadersObject -> Flags -> Effect Unit) -> Effect (Effect Unit)
135-
onStream = unsafeCoerce Internal.onStream
135+
onStream http2session callback = Internal.onStream (unsafeCoerce http2session) (\http2stream -> callback (unsafeCoerce http2stream))
136136

137137
-- | https://nodejs.org/docs/latest/api/http2.html#event-error
138138
-- |
@@ -141,7 +141,7 @@ onStream = unsafeCoerce Internal.onStream
141141
-- | Returns an effect for removing the event listener before the event
142142
-- | is raised.
143143
onceErrorSession :: ClientHttp2Session -> (Error -> Effect Unit) -> Effect (Effect Unit)
144-
onceErrorSession = unsafeCoerce Internal.onceEmitterError
144+
onceErrorSession http2session = Internal.onceEmitterError (unsafeCoerce http2session)
145145

146146
-- | https://nodejs.org/docs/latest/api/http2.html#event-error_1
147147
-- |
@@ -150,7 +150,7 @@ onceErrorSession = unsafeCoerce Internal.onceEmitterError
150150
-- | Returns an effect for removing the event listener before the event
151151
-- | is raised.
152152
onceErrorStream :: ClientHttp2Stream -> (Error -> Effect Unit) -> Effect (Effect Unit)
153-
onceErrorStream = unsafeCoerce Internal.onceEmitterError
153+
onceErrorStream http2stream = Internal.onceEmitterError (unsafeCoerce http2stream)
154154

155155
-- | https://nodejs.org/docs/latest/api/http2.html#event-push
156156
-- |
@@ -164,7 +164,7 @@ foreign import oncePush :: ClientHttp2Stream -> (HeadersObject -> Flags -> Effec
164164
-- | Returns an effect for removing the event listener before the event
165165
-- | is raised.
166166
onceTrailers :: ClientHttp2Stream -> (HeadersObject -> Flags -> Effect Unit) -> Effect (Effect Unit)
167-
onceTrailers = unsafeCoerce Internal.onceTrailers
167+
onceTrailers http2stream = Internal.onceTrailers (unsafeCoerce http2stream)
168168

169169
-- | https://nodejs.org/docs/latest/api/http2.html#event-wanttrailers
170170
-- |
@@ -173,19 +173,19 @@ onceTrailers = unsafeCoerce Internal.onceTrailers
173173
-- | Returns an effect for removing the event listener before the event
174174
-- | is raised.
175175
onceWantTrailers :: ClientHttp2Stream -> Effect Unit -> Effect (Effect Unit)
176-
onceWantTrailers = unsafeCoerce Internal.onceWantTrailers
176+
onceWantTrailers http2stream = Internal.onceWantTrailers (unsafeCoerce http2stream)
177177

178178
-- | https://nodejs.org/docs/latest/api/http2.html#http2streamsendtrailersheaders
179179
-- |
180180
-- | > 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.
181181
sendTrailers :: ClientHttp2Stream -> HeadersObject -> Effect Unit
182-
sendTrailers = unsafeCoerce Internal.sendTrailers
182+
sendTrailers http2stream = Internal.sendTrailers (unsafeCoerce http2stream)
183183

184184
-- | https://nodejs.org/docs/latest/api/stream.html#event-data
185185
-- |
186186
-- | Returns an effect for removing the event listener.
187187
onData :: ClientHttp2Stream -> (Buffer -> Effect Unit) -> Effect (Effect Unit)
188-
onData = unsafeCoerce Internal.onData
188+
onData http2stream = Internal.onData (unsafeCoerce http2stream)
189189

190190
-- | https://nodejs.org/docs/latest/api/net.html#event-end
191191
-- |
@@ -194,11 +194,11 @@ onData = unsafeCoerce Internal.onData
194194
-- | Returns an effect for removing the event listener before the event
195195
-- | is raised.
196196
onceEnd :: ClientHttp2Stream -> Effect Unit -> Effect (Effect Unit)
197-
onceEnd = unsafeCoerce Internal.onceEnd
197+
onceEnd http2stream = Internal.onceEnd (unsafeCoerce http2stream)
198198

199199
-- | https://nodejs.org/docs/latest/api/http2.html#http2streamclosecode-callback
200200
closeStream :: ClientHttp2Stream -> Int -> Effect Unit -> Effect Unit
201-
closeStream stream code = Internal.closeStream (unsafeCoerce stream) code
201+
closeStream stream = Internal.closeStream (unsafeCoerce stream)
202202

203203
-- | Coerce to a duplex stream.
204204
toDuplex :: ClientHttp2Stream -> Duplex

src/Node/HTTP2/Server.purs

Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ foreign import listen :: Http2Server -> OptionsObject -> Effect Unit -> Effect U
9090

9191
-- | https://nodejs.org/docs/latest/api/http2.html#serverclosecallback
9292
closeServer :: Http2Server -> Effect Unit -> Effect Unit
93-
closeServer = unsafeCoerce Internal.closeServer
93+
closeServer http2server = Internal.closeServer (unsafeCoerce http2server)
9494

9595
-- | https://nodejs.org/docs/latest/api/net.html#event-close
9696
-- |
@@ -99,7 +99,7 @@ closeServer = unsafeCoerce Internal.closeServer
9999
-- | Returns an effect for removing the event listener before the event
100100
-- | is raised.
101101
onceCloseServer :: Http2Server -> Effect Unit -> Effect (Effect Unit)
102-
onceCloseServer = unsafeCoerce Internal.onceServerClose
102+
onceCloseServer http2server = Internal.onceServerClose (unsafeCoerce http2server)
103103

104104
-- | An HTTP/2 server with one listening socket for encrypted connections.
105105
-- |
@@ -113,11 +113,11 @@ foreign import createSecureServer :: OptionsObject -> Effect Http2SecureServer
113113

114114
-- | https://nodejs.org/docs/latest/api/net.html#serverlistenoptions-callback
115115
listenSecure :: Http2SecureServer -> OptionsObject -> Effect Unit -> Effect Unit
116-
listenSecure = unsafeCoerce listen
116+
listenSecure http2server = listen (unsafeCoerce http2server)
117117

118118
-- | https://nodejs.org/docs/latest/api/http2.html#serverclosecallback
119119
closeServerSecure :: Http2SecureServer -> Effect Unit -> Effect Unit
120-
closeServerSecure = unsafeCoerce Internal.closeServer
120+
closeServerSecure http2server = Internal.closeServer (unsafeCoerce http2server)
121121

122122
-- | https://nodejs.org/docs/latest/api/net.html#event-close
123123
-- |
@@ -126,23 +126,23 @@ closeServerSecure = unsafeCoerce Internal.closeServer
126126
-- | Returns an effect for removing the event listener before the event
127127
-- | is raised.
128128
onceCloseServerSecure :: Http2SecureServer -> Effect Unit -> Effect (Effect Unit)
129-
onceCloseServerSecure = unsafeCoerce Internal.onceServerClose
129+
onceCloseServerSecure http2server = Internal.onceServerClose (unsafeCoerce http2server)
130130

131131
-- | https://nodejs.org/docs/latest/api/net.html#event-error
132132
-- |
133133
-- | the 'close' event will not be emitted directly following this event unless server.close() is manually called.
134134
-- |
135135
-- | Returns an effect for removing the event listener.
136136
onErrorServer :: Http2Server -> (Error -> Effect Unit) -> Effect (Effect Unit)
137-
onErrorServer = unsafeCoerce Internal.onEmitterError
137+
onErrorServer http2server = Internal.onEmitterError (unsafeCoerce http2server)
138138

139139
-- | https://nodejs.org/docs/latest/api/net.html#event-error
140140
-- |
141141
-- | the 'close' event will not be emitted directly following this event unless server.close() is manually called.
142142
-- |
143143
-- | Returns an effect for removing the event listener.
144144
onErrorServerSecure :: Http2SecureServer -> (Error -> Effect Unit) -> Effect (Effect Unit)
145-
onErrorServerSecure = unsafeCoerce Internal.onEmitterError
145+
onErrorServerSecure http2server = Internal.onEmitterError (unsafeCoerce http2server)
146146

147147
-- | https://nodejs.org/docs/latest/api/http2.html#class-serverhttp2session
148148
-- |
@@ -169,11 +169,11 @@ foreign import onceSession :: Http2Server -> (ServerHttp2Session -> Effect Unit)
169169

170170
-- | https://nodejs.org/api/http2.html#http2sessionlocalsettings
171171
localSettings :: ServerHttp2Session -> Effect SettingsObject
172-
localSettings = unsafeCoerce Internal.localSettings
172+
localSettings http2session = Internal.localSettings (unsafeCoerce http2session)
173173

174174
-- | https://nodejs.org/docs/latest/api/http2.html#http2sessionclosecallback
175175
closeSession :: ServerHttp2Session -> Effect Unit -> Effect Unit
176-
closeSession = unsafeCoerce Internal.closeSession
176+
closeSession http2session = Internal.closeSession (unsafeCoerce http2session)
177177

178178
-- | Listen for one event, call the callback, then remove
179179
-- | the event listener.
@@ -183,13 +183,13 @@ closeSession = unsafeCoerce Internal.closeSession
183183
-- |
184184
-- | https://nodejs.org/docs/latest/api/http2.html#event-stream
185185
onceStream :: Http2Server -> (ServerHttp2Stream -> HeadersObject -> Flags -> Effect Unit) -> Effect (Effect Unit)
186-
onceStream = unsafeCoerce Internal.onceStream
186+
onceStream http2server callback = Internal.onceStream (unsafeCoerce http2server) (\http2stream -> callback (unsafeCoerce http2stream))
187187

188188
-- | https://nodejs.org/docs/latest/api/http2.html#event-stream
189189
-- |
190190
-- | Returns an effect for removing the event listener.
191191
onStream :: Http2Server -> (ServerHttp2Stream -> HeadersObject -> Flags -> Effect Unit) -> Effect (Effect Unit)
192-
onStream = unsafeCoerce Internal.onStream
192+
onStream http2server callback = Internal.onStream (unsafeCoerce http2server) (\http2stream -> callback (unsafeCoerce http2stream))
193193

194194
-- | Listen for one event, call the callback, then remove
195195
-- | the event listener.
@@ -199,26 +199,26 @@ onStream = unsafeCoerce Internal.onStream
199199
-- |
200200
-- | https://nodejs.org/docs/latest/api/http2.html#event-stream
201201
onceStreamSecure :: Http2SecureServer -> (ServerHttp2Stream -> HeadersObject -> Flags -> Effect Unit) -> Effect (Effect Unit)
202-
onceStreamSecure = unsafeCoerce Internal.onceStream
202+
onceStreamSecure http2server callback = Internal.onceStream (unsafeCoerce http2server) (\http2stream -> callback (unsafeCoerce http2stream))
203203

204204
-- | https://nodejs.org/docs/latest/api/http2.html#event-stream
205205
-- |
206206
-- | Returns an effect for removing the event listener.
207207
onStreamSecure :: Http2SecureServer -> (ServerHttp2Stream -> HeadersObject -> Flags -> Effect Unit) -> Effect (Effect Unit)
208-
onStreamSecure = unsafeCoerce Internal.onStream
208+
onStreamSecure http2server callback = Internal.onStream (unsafeCoerce http2server) (\http2stream -> callback (unsafeCoerce http2stream))
209209

210210
-- | https://nodejs.org/docs/latest/api/http2.html#http2streamrespondheaders-options
211211
respond :: ServerHttp2Stream -> HeadersObject -> OptionsObject -> Effect Unit
212-
respond = unsafeCoerce Internal.respond
212+
respond http2stream = Internal.respond (unsafeCoerce http2stream)
213213

214214
-- | https://nodejs.org/docs/latest/api/http2.html#event-session_1
215215
-- |
216216
-- | Listen for one event, then remove the event listener.
217217
-- |
218218
-- | Returns an effect for removing the event listener before the event
219219
-- | is raised.
220-
onceSessionSecure :: Http2SecureServer -> (ServerHttp2Session -> Effect Unit) -> Effect Unit
221-
onceSessionSecure = unsafeCoerce onceSession
220+
onceSessionSecure :: Http2SecureServer -> (ServerHttp2Session -> Effect Unit) -> Effect (Effect Unit)
221+
onceSessionSecure http2server = onceSession (unsafeCoerce http2server)
222222

223223
-- | An HTTP/2 server `Http2Stream` connected to a client.
224224
-- |
@@ -242,7 +242,7 @@ foreign import pushStream :: ServerHttp2Stream -> HeadersObject -> OptionsObject
242242
-- | Returns an effect for removing the event listener before the event
243243
-- | is raised.
244244
onceTrailers :: ServerHttp2Stream -> (HeadersObject -> Flags -> Effect Unit) -> Effect (Effect Unit)
245-
onceTrailers = unsafeCoerce Internal.onceTrailers
245+
onceTrailers http2stream = Internal.onceTrailers (unsafeCoerce http2stream)
246246

247247
-- | https://nodejs.org/docs/latest/api/http2.html#http2streamadditionalheadersheaders
248248
foreign import additionalHeaders :: ServerHttp2Stream -> HeadersObject -> Effect Unit
@@ -254,7 +254,7 @@ foreign import additionalHeaders :: ServerHttp2Stream -> HeadersObject -> Effect
254254
-- | Returns an effect for removing the event listener before the event
255255
-- | is raised.
256256
onceErrorStream :: ServerHttp2Stream -> (Error -> Effect Unit) -> Effect (Effect Unit)
257-
onceErrorStream = unsafeCoerce Internal.onceEmitterError
257+
onceErrorStream http2stream = Internal.onceEmitterError (unsafeCoerce http2stream)
258258

259259
-- | https://nodejs.org/docs/latest/api/http2.html#event-wanttrailers
260260
-- |
@@ -263,13 +263,13 @@ onceErrorStream = unsafeCoerce Internal.onceEmitterError
263263
-- | Returns an effect for removing the event listener before the event
264264
-- | is raised.
265265
onceWantTrailers :: ServerHttp2Stream -> Effect Unit -> Effect (Effect Unit)
266-
onceWantTrailers = unsafeCoerce Internal.onceWantTrailers
266+
onceWantTrailers http2stream = Internal.onceWantTrailers (unsafeCoerce http2stream)
267267

268268
-- | https://nodejs.org/docs/latest/api/http2.html#http2streamsendtrailersheaders
269269
-- |
270270
-- | > 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.
271271
sendTrailers :: ServerHttp2Stream -> HeadersObject -> Effect Unit
272-
sendTrailers = unsafeCoerce Internal.sendTrailers
272+
sendTrailers http2stream = Internal.sendTrailers (unsafeCoerce http2stream)
273273

274274
-- | https://nodejs.org/docs/latest/api/net.html#event-end
275275
-- |
@@ -278,11 +278,11 @@ sendTrailers = unsafeCoerce Internal.sendTrailers
278278
-- | Returns an effect for removing the event listener before the event
279279
-- | is raised.
280280
onceEnd :: ServerHttp2Stream -> Effect Unit -> Effect (Effect Unit)
281-
onceEnd = unsafeCoerce Internal.onceEnd
281+
onceEnd http2stream = Internal.onceEnd (unsafeCoerce http2stream)
282282

283283
-- | https://nodejs.org/docs/latest/api/http2.html#http2streamclosecode-callback
284284
closeStream :: ServerHttp2Stream -> Int -> Effect Unit -> Effect Unit
285-
closeStream stream code = Internal.closeStream (unsafeCoerce stream) code
285+
closeStream stream = Internal.closeStream (unsafeCoerce stream)
286286

287287
-- | Coerce to a duplex stream.
288288
toDuplex :: ServerHttp2Stream -> Duplex

0 commit comments

Comments
 (0)