1
1
package io .scalajs .nodejs
2
2
package http
3
3
4
+ import com .thoughtworks .enableIf
4
5
import io .scalajs .nodejs .buffer .Buffer
5
6
import io .scalajs .nodejs .net .Socket
6
- import io .scalajs .nodejs .stream .Readable
7
7
import io .scalajs .util .PromiseHelper ._
8
8
9
9
import scala .concurrent .Future
10
10
import scala .scalajs .js
11
11
import scala .scalajs .js .annotation .JSImport
12
+ import scala .scalajs .js .typedarray .Uint8Array
13
+ import scala .scalajs .js .{Any , | }
12
14
13
15
/**
14
16
* http.ClientRequest - This object is created internally and returned from http.request(). It represents an in-progress
@@ -31,11 +33,25 @@ import scala.scalajs.js.annotation.JSImport
31
33
*/
32
34
@ js.native
33
35
@ JSImport (" http" , " ClientRequest" )
34
- class ClientRequest extends Readable {
36
+ class ClientRequest extends stream. Writable {
35
37
36
- def headers : js. Dictionary [ String ] = js.native
38
+ def aborted : Int | Boolean = js.native
37
39
38
- def socket : Socket = js.native
40
+ def connection : net.Socket = js.native
41
+
42
+ def finished : Boolean = js.native
43
+
44
+ var maxHeadersCount : Int | Null = js.native
45
+
46
+ def path : String = js.native
47
+
48
+ def socket : net.Socket = js.native
49
+
50
+ @ enableIf(io.scalajs.nodejs.CompilerSwitches .gteNodeJs12)
51
+ def writableEnded : Boolean = js.native
52
+
53
+ @ enableIf(io.scalajs.nodejs.CompilerSwitches .gteNodeJs12)
54
+ def writableFinished : Boolean = js.native
39
55
40
56
// ///////////////////////////////////////////////////////////////////////////////
41
57
// Methods
@@ -48,34 +64,6 @@ class ClientRequest extends Readable {
48
64
*/
49
65
def abort (): Unit = js.native
50
66
51
- /**
52
- * Finishes sending the request. If any parts of the body are unsent, it will flush them to the stream.
53
- * If the request is chunked, this will send the terminating '0\r\n\r\n'.
54
- * @see [[https://nodejs.org/api/http.html#http_request_end_data_encoding_callback ]]
55
- */
56
- def end (data : js.Any , encoding : String , callback : js.Function ): Unit = js.native
57
-
58
- /**
59
- * Finishes sending the request. If any parts of the body are unsent, it will flush them to the stream.
60
- * If the request is chunked, this will send the terminating '0\r\n\r\n'.
61
- * @see [[https://nodejs.org/api/http.html#http_request_end_data_encoding_callback ]]
62
- */
63
- def end (data : js.Any , encoding : String ): Unit = js.native
64
-
65
- /**
66
- * Finishes sending the request. If any parts of the body are unsent, it will flush them to the stream.
67
- * If the request is chunked, this will send the terminating '0\r\n\r\n'.
68
- * @see [[https://nodejs.org/api/http.html#http_request_end_data_encoding_callback ]]
69
- */
70
- def end (data : js.Any ): Unit = js.native
71
-
72
- /**
73
- * Finishes sending the request. If any parts of the body are unsent, it will flush them to the stream.
74
- * If the request is chunked, this will send the terminating '0\r\n\r\n'.
75
- * @see [[https://nodejs.org/api/http.html#http_request_end_data_encoding_callback ]]
76
- */
77
- def end (): Unit = js.native
78
-
79
67
/**
80
68
* Flush the request headers.
81
69
*
@@ -90,39 +78,27 @@ class ClientRequest extends Readable {
90
78
*/
91
79
def flushHeaders (): Unit = js.native
92
80
93
- /**
94
- * Once a socket is assigned to this request and is connected socket.setNoDelay() will be called.
95
- */
96
- def setNoDelay (noDelay : Int ): Unit = js.native
81
+ def getHeader [T <: js.Any ](name : String ): T = js.native
97
82
98
- /**
99
- * Once a socket is assigned to this request and is connected socket.setNoDelay() will be called.
100
- */
101
- def setNoDelay (): Unit = js.native
83
+ def removeHeader (name : String ): Unit = js.native
102
84
103
- /**
104
- * Once a socket is assigned to this request and is connected socket.setKeepAlive() will be called.
105
- * @see [[https://nodejs.org/api/http.html#http_request_setsocketkeepalive_enable_initialdelay ]]
106
- */
107
- def setSocketKeepAlive (enable : Boolean , initialDelay : Int ): Unit = js.native
85
+ def setHeader (name : String , value : js.Any ): Unit = js.native
108
86
109
87
/**
110
- * Once a socket is assigned to this request and is connected socket.setKeepAlive() will be called.
111
- * @see [[https://nodejs.org/api/http.html#http_request_setsocketkeepalive_enable_initialdelay ]]
88
+ * Once a socket is assigned to this request and is connected socket.setNoDelay() will be called.
112
89
*/
113
- def setSocketKeepAlive ( enable : Boolean ): Unit = js.native
90
+ def setNoDelay ( noDelay : Int ): Unit = js.native
114
91
115
92
/**
116
- * Once a socket is assigned to this request and is connected socket.setKeepAlive() will be called.
117
- * @see [[https://nodejs.org/api/http.html#http_request_setsocketkeepalive_enable_initialdelay ]]
93
+ * Once a socket is assigned to this request and is connected socket.setNoDelay() will be called.
118
94
*/
119
- def setSocketKeepAlive ( initialDelay : Int ): Unit = js.native
95
+ def setNoDelay ( noDelay : Boolean = js.native ): Unit = js.native
120
96
121
97
/**
122
98
* Once a socket is assigned to this request and is connected socket.setKeepAlive() will be called.
123
99
* @see [[https://nodejs.org/api/http.html#http_request_setsocketkeepalive_enable_initialdelay ]]
124
100
*/
125
- def setSocketKeepAlive (): Unit = js.native
101
+ def setSocketKeepAlive (enable : Boolean = js.native, initialDelay : Int = js.native ): Unit = js.native
126
102
127
103
/**
128
104
* Once a socket is assigned to this request and is connected socket.setTimeout() will be called.
@@ -132,53 +108,7 @@ class ClientRequest extends Readable {
132
108
* Same as binding to the timeout event.</li>
133
109
* </ul>
134
110
*/
135
- def setTimeout (timeout : Int , callback : js.Function ): Unit = js.native
136
-
137
- /**
138
- * Once a socket is assigned to this request and is connected socket.setTimeout() will be called.
139
- * <ul>
140
- * <li>timeout {Number} Milliseconds before a request is considered to be timed out.</li>
141
- * <li>callback {Function} Optional function to be called when a timeout occurs.
142
- * Same as binding to the timeout event.</li>
143
- * </ul>
144
- */
145
- def setTimeout (timeout : Int ): Unit = js.native
146
-
147
- /**
148
- * Sends a chunk of the body. By calling this method many times, the user can stream a
149
- * request body to a server--in that case it is suggested to use the ['Transfer-Encoding', 'chunked']
150
- * header line when creating the request.
151
- *
152
- * The chunk argument should be a Buffer or a string.
153
- * The encoding argument is optional and only applies when chunk is a string. Defaults to 'utf8'.
154
- * The callback argument is optional and will be called when this chunk of data is flushed.
155
- * Returns request.
156
- */
157
- def write (chunk : js.Any , encoding : String , callback : js.Function ): Unit = js.native
158
-
159
- /**
160
- * Sends a chunk of the body. By calling this method many times, the user can stream a
161
- * request body to a server--in that case it is suggested to use the ['Transfer-Encoding', 'chunked']
162
- * header line when creating the request.
163
- *
164
- * The chunk argument should be a Buffer or a string.
165
- * The encoding argument is optional and only applies when chunk is a string. Defaults to 'utf8'.
166
- * The callback argument is optional and will be called when this chunk of data is flushed.
167
- * Returns request.
168
- */
169
- def write (chunk : js.Any , encoding : String ): Unit = js.native
170
-
171
- /**
172
- * Sends a chunk of the body. By calling this method many times, the user can stream a
173
- * request body to a server--in that case it is suggested to use the ['Transfer-Encoding', 'chunked']
174
- * header line when creating the request.
175
- *
176
- * The chunk argument should be a Buffer or a string.
177
- * The encoding argument is optional and only applies when chunk is a string. Defaults to 'utf8'.
178
- * The callback argument is optional and will be called when this chunk of data is flushed.
179
- * Returns request.
180
- */
181
- def write (chunk : js.Any ): Unit = js.native
111
+ def setTimeout (timeout : Int , callback : js.Function = js.native): Unit = js.native
182
112
183
113
}
184
114
@@ -187,10 +117,7 @@ class ClientRequest extends Readable {
187
117
*/
188
118
object ClientRequest {
189
119
190
- /**
191
- * Client Request Events
192
- */
193
- implicit class ClientRequestEvents (val client : ClientRequest ) extends AnyVal {
120
+ implicit final class ClientRequestExtensions (val client : ClientRequest ) extends AnyVal {
194
121
195
122
/**
196
123
* Emitted when the request has been aborted by the client. This event is only emitted on the first call to abort().
@@ -246,23 +173,25 @@ object ClientRequest {
246
173
@ inline
247
174
def onUpgrade (callback : (IncomingMessage , Socket , Buffer ) => Any ): client.type = client.on(" upgrade" , callback)
248
175
249
- }
250
-
251
- /**
252
- * Client Request Extensions
253
- */
254
- implicit class ClientRequestEnrichment (val client : ClientRequest ) extends AnyVal {
176
+ @ inline
177
+ def endFuture (data : Uint8Array ): Future [Unit ] = {
178
+ promiseWithError0[Error ](client.end(data, _))
179
+ }
255
180
256
181
@ inline
257
- def endFuture (data : js. Any , encoding : String ): Future [js. Any ] = {
258
- promiseWithError1[ SystemError , js. Any ](client.end(data, encoding, _))
182
+ def endFuture (data : String , encoding : String ): Future [Unit ] = {
183
+ promiseWithError0[ Error ](client.end(data, encoding, _))
259
184
}
260
185
261
186
@ inline
262
- def writeFuture (chunk : js. Any , encoding : String ): Future [js. Any ] = {
263
- promiseWithError1[ SystemError , js. Any ](client.write(chunk, encoding , _))
187
+ def writeFuture (chunk : Uint8Array ): Future [Unit ] = {
188
+ promiseWithError0[ Error ](client.write(chunk, _))
264
189
}
265
190
191
+ @ inline
192
+ def writeFuture (chunk : String , encoding : String ): Future [Unit ] = {
193
+ promiseWithError0[Error ](client.write(chunk, encoding, _))
194
+ }
266
195
}
267
196
268
197
}
0 commit comments