Skip to content

Commit 19e8ae4

Browse files
Ayase-252targos
authored andcommitted
doc: add document for http.OutgoingMessage
OutgoingMessage is a very old feature which is exported to public in http module dated to v0.1.x. But it is not documented at all. This commit adds document for http.OutgogingMessage. Fixes: #33847 PR-URL: #37265 Reviewed-By: James M Snell <[email protected]> Reviewed-By: Robert Nagy <[email protected]> Reviewed-By: Benjamin Gruenbaum <[email protected]>
1 parent a6c1233 commit 19e8ae4

File tree

1 file changed

+374
-0
lines changed

1 file changed

+374
-0
lines changed

doc/api/http.md

+374
Original file line numberDiff line numberDiff line change
@@ -2190,6 +2190,378 @@ URL {
21902190
}
21912191
```
21922192

2193+
## Class: `http.OutgoingMessage`
2194+
<!-- YAML
2195+
added: v0.1.17
2196+
-->
2197+
2198+
* Extends: {Stream}
2199+
2200+
This class serves as the parent class of [`http.ClientRequest`][]
2201+
and [`http.ServerResponse`][]. It is an abstract of outgoing message from
2202+
the perspective of the participants of HTTP transaction.
2203+
2204+
### Event: `drain`
2205+
<!-- YAML
2206+
added: v0.3.6
2207+
-->
2208+
2209+
Emitted when the buffer of the message is free again.
2210+
2211+
### Event: `finish`
2212+
<!-- YAML
2213+
added: v0.1.17
2214+
-->
2215+
2216+
Emitted when transmission is finished successfully.
2217+
2218+
### Event: `prefinish`
2219+
<!-- YAML
2220+
added: v0.11.6
2221+
-->
2222+
2223+
Emitted when `outgoingMessage.end` was called.
2224+
When the event is emitted, all data has been processed but not necessarily
2225+
completely flushed.
2226+
2227+
### `outgoingMessage.addTrailers(headers)`
2228+
<!-- YAML
2229+
added: v0.3.0
2230+
-->
2231+
2232+
* `headers` {Object}
2233+
2234+
Adds HTTP trailers (headers but at the end of the message) to the message.
2235+
2236+
Trailers are **only** be emitted if the message is chunked encoded. If not,
2237+
trailer will be silently discarded.
2238+
2239+
HTTP requires the `Trailer` header to be sent in order to emit trailers,
2240+
with a list of header fields in its value, e.g.
2241+
2242+
```js
2243+
message.writeHead(200, { 'Content-Type': 'text/plain',
2244+
'Trailer': 'Content-MD5' });
2245+
message.write(fileData);
2246+
message.addTrailers({ 'Content-MD5': '7895bf4b8828b55ceaf47747b4bca667' });
2247+
message.end();
2248+
```
2249+
2250+
Attempting to set a header field name or value that contains invalid characters
2251+
will result in a `TypeError` being thrown.
2252+
2253+
### `outgoingMessage.connection`
2254+
<!-- YAML
2255+
added: v0.3.0
2256+
deprecated: REPLACEME
2257+
-->
2258+
2259+
> Stability: 0 - Deprecated: Use [`outgoingMessage.socket`][] instead.
2260+
2261+
Aliases of `outgoingMessage.socket`
2262+
### `outgoingMessage.cork()`
2263+
<!-- YAML
2264+
added: v14.0.0
2265+
-->
2266+
2267+
See [`writable.cork()`][].
2268+
2269+
### `outgoingMessage.destroy([error])`
2270+
<!-- YAML
2271+
added: v0.3.0
2272+
-->
2273+
2274+
* `error` {Error} Optional, an error to emit with `error` event
2275+
* Returns: {this}
2276+
2277+
Destroys the message. Once a socket is associated with the message
2278+
and is connected, that socket will be destroyed as well.
2279+
2280+
### `outgoingMessage.end(chunk[, encoding][, callback])`
2281+
<!-- YAML
2282+
added: v0.1.90
2283+
changes:
2284+
- version: v0.11.6
2285+
description: add `callback` argument.
2286+
-->
2287+
2288+
* `chunk` {string | Buffer}
2289+
* `encoding` {string} Optional, **Default**: `utf-8`
2290+
* `callback` {Function} Optional
2291+
* Returns: {this}
2292+
2293+
Finishes the outgoing message. If any parts of the body are unsent, it will
2294+
flush them to the underlying system. If the message is chunked, it will
2295+
send the terminating chunk `0\r\n\r\n`, and send the trailer (if any).
2296+
2297+
If `chunk` is specified, it is equivalent to call
2298+
`outgoingMessage.write(chunk, encoding)`, followed by
2299+
`outgoingMessage.end(callback)`.
2300+
2301+
If `callback` is provided, it will be called when the message is finished.
2302+
(equivalent to the callback to event `finish`)
2303+
2304+
### `outgoingMessage.flushHeaders()`
2305+
<!-- YAML
2306+
added: v1.6.0
2307+
-->
2308+
2309+
Compulsorily flushes the message headers
2310+
2311+
For efficiency reason, Node.js normally buffers the message headers
2312+
until `outgoingMessage.end()` is called or the first chunk of message data
2313+
is written. It then tries to pack the headers and data into a single TCP
2314+
packet.
2315+
2316+
It is usually desired (it saves a TCP round-trip), but not when the first
2317+
data is not sent until possibly much later. `outgoingMessage.flushHeaders()`
2318+
bypasses the optimization and kickstarts the request.
2319+
2320+
### `outgoingMessage.getHeader(name)`
2321+
<!-- YAML
2322+
added: v0.4.0
2323+
-->
2324+
2325+
* `name` {string} Name of header
2326+
* Returns {string | undefined}
2327+
2328+
Gets value of HTTP header with given name. If such name doesn't exist in
2329+
message, it will be `undefined`.
2330+
2331+
### `outgoingMessage.getHeaderNames()`
2332+
<!-- YAML
2333+
added: v8.0.0
2334+
-->
2335+
2336+
* Returns {string[]}
2337+
2338+
Returns an array of names of headers of the outgoing outgoingMessage. All
2339+
names are lowercase.
2340+
2341+
### `outgoingMessage.getHeaders()`
2342+
<!-- YAML
2343+
added: v8.0.0
2344+
-->
2345+
2346+
* Returns: {Object}
2347+
2348+
Returns a shallow copy of the current outgoing headers. Since a shallow
2349+
copy is used, array values may be mutated without additional calls to
2350+
various header-related http module methods. The keys of the returned
2351+
object are the header names and the values are the respective header
2352+
values. All header names are lowercase.
2353+
2354+
The object returned by the `outgoingMessage.getHeaders()` method does
2355+
not prototypically inherit from the JavaScript Object. This means that
2356+
typical Object methods such as `obj.toString()`, `obj.hasOwnProperty()`,
2357+
and others are not defined and will not work.
2358+
2359+
```js
2360+
outgoingMessage.setHeader('Foo', 'bar');
2361+
outgoingMessage.setHeader('Set-Cookie', ['foo=bar', 'bar=baz']);
2362+
2363+
const headers = outgoingMessage.getHeaders();
2364+
// headers === { foo: 'bar', 'set-cookie': ['foo=bar', 'bar=baz'] }
2365+
```
2366+
2367+
### `outgoingMessage.hasHeader(name)`
2368+
<!-- YAML
2369+
added: v8.0.0
2370+
-->
2371+
2372+
* `name` {string}
2373+
* Returns {boolean}
2374+
2375+
Returns `true` if the header identified by `name` is currently set in the
2376+
outgoing headers. The header name is case-insensitive.
2377+
2378+
```js
2379+
const hasContentType = outgoingMessage.hasHeader('content-type');
2380+
```
2381+
2382+
### `outgoingMessage.headersSent`
2383+
<!-- YAML
2384+
added: v0.9.3
2385+
-->
2386+
2387+
* {boolean}
2388+
2389+
Read-only. `true` if the headers were sent, otherwise `false`.
2390+
2391+
### `outgoingMessage.pipe()`
2392+
<!-- YAML
2393+
added: v9.0.0
2394+
-->
2395+
2396+
Overrides the pipe method of legacy `Stream` which is the parent class of
2397+
`http.outgoingMessage`.
2398+
2399+
Since `OutgoingMessage` should be a write-only stream,
2400+
call this function will throw an `Error`. Thus, it disabled the pipe method
2401+
it inherits from `Stream`.
2402+
2403+
User should not call this function directly.
2404+
2405+
### `outgoingMessage.removeHeader()`
2406+
<!-- YAML
2407+
added: v0.4.0
2408+
-->
2409+
2410+
Removes a header that is queued for implicit sending.
2411+
2412+
```js
2413+
outgoingMessage.removeHeader('Content-Encoding');
2414+
```
2415+
2416+
### `outgoingMessage.setHeader(name, value)`
2417+
<!-- YAML
2418+
added: v0.4.0
2419+
-->
2420+
2421+
* `name` {string} Header name
2422+
* `value` {string} Header value
2423+
* Returns: {this}
2424+
2425+
Sets a single header value for header object.
2426+
2427+
### `outgoingMessage.setTimeout(msesc[, callback])`
2428+
<!-- YAML
2429+
added: v0.9.12
2430+
-->
2431+
2432+
* `msesc` {number}
2433+
* `callback` {Function} Optional function to be called when a timeout
2434+
occurs, Same as binding to the `timeout` event.
2435+
* Returns: {this}
2436+
2437+
Once a socket is associated with the message and is connected,
2438+
[`socket.setTimeout()`][] will be called with `msecs` as the first parameter.
2439+
2440+
### `outgoingMessage.socket`
2441+
<!-- YAML
2442+
added: v0.3.0
2443+
-->
2444+
2445+
* {stream.Duplex}
2446+
2447+
Reference to the underlying socket. Usually users will not want to access
2448+
this property.
2449+
2450+
After calling `outgoingMessage.end()`, this property will be nulled.
2451+
2452+
### `outgoingMessage.uncork()`
2453+
<!-- YAML
2454+
added: v14.0.0
2455+
-->
2456+
2457+
See [`writable.uncork()`][]
2458+
2459+
### `outgoingMessage.writableCorked`
2460+
<!-- YAML
2461+
added: v14.0.0
2462+
-->
2463+
2464+
* {number}
2465+
2466+
This `outgoingMessage.writableCorked` will return the time how many
2467+
`outgoingMessage.cork()` have been called.
2468+
2469+
### `outgoingMessage.writableEnded`
2470+
<!-- YAML
2471+
added: v13.0.0
2472+
-->
2473+
2474+
* {boolean}
2475+
2476+
Readonly, `true` if `outgoingMessage.end()` has been called. Noted that
2477+
this property does not reflect whether the data has been flush. For that
2478+
purpose, use `message.writableFinished` instead.
2479+
2480+
### `outgoingMessage.writableFinished`
2481+
<!-- YAML
2482+
added: v13.0.0
2483+
-->
2484+
2485+
* {boolean}
2486+
2487+
Readonly. `true` if all data has been flushed to the underlying system.
2488+
2489+
### `outgoingMessage.writableHighWaterMark`
2490+
<!-- YAML
2491+
added: v13.0.0
2492+
-->
2493+
2494+
* {number}
2495+
2496+
This `outgoingMessage.writableHighWaterMark` will be the `highWaterMark` of
2497+
underlying socket if socket exists. Else, it would be the default
2498+
`highWaterMark`.
2499+
2500+
`highWaterMark` is the maximum amount of data which can be potentially
2501+
buffered by socket.
2502+
2503+
### `outgoingMessage.writableLength`
2504+
<!-- YAML
2505+
added: v13.0.0
2506+
-->
2507+
2508+
* {number}
2509+
2510+
Readonly, This `outgoingMessage.writableLength` contains the number of
2511+
bytes (or objects) in the buffer ready to send.
2512+
2513+
### `outgoingMessage.writableObjectMode`
2514+
<!-- YAML
2515+
added: v13.0.0
2516+
-->
2517+
2518+
* {boolean}
2519+
2520+
Readonly, always returns `false`.
2521+
2522+
### `outgoingMessage.write(chunk[, encoding][, callback])`
2523+
<!-- YAML
2524+
added: v0.1.29
2525+
changes:
2526+
- version: v0.11.6
2527+
description: add `callback` argument.
2528+
-->
2529+
2530+
* `chunk` {string | Buffer}
2531+
* `encoding` {string} **Default**: `utf-8`
2532+
* `callback` {Function}
2533+
* Returns {boolean}
2534+
2535+
If this method is called and header is not sent, it will call
2536+
`this._implicitHeader` to flush implicit header.
2537+
If the message should not have a body (indicated by `this._hasBody`),
2538+
the call is ignored and `chunk` will not be sent. It could be useful
2539+
when handling particular message which must not include a body.
2540+
e.g. response to `HEAD` request, `204` and `304` response.
2541+
2542+
`chunk` can be a string or a buffer. When `chunk` is a string, the
2543+
`encoding` parameter specifies how to encode `chunk` into a byte stream.
2544+
`callback` will be called when the `chunk` is flushed.
2545+
2546+
If the message is transferred in chucked encoding
2547+
(indicated by `this.chunkedEncoding`), `chunk` will be flushed as
2548+
one chunk among a stream of chunks. Otherwise, it will be flushed as body
2549+
of message.
2550+
2551+
This method handles the raw body of HTTP message and has nothing to do with
2552+
higher-level multi-part body encodings that may be used.
2553+
2554+
If it is the first call to this method of a message, it will send the
2555+
buffered header first, then flush the the `chunk` as described above.
2556+
2557+
The second and successive calls to this method, it will assume the data
2558+
will streamed and send the new data separately. It means that the response
2559+
is buffered up to the first chunk of the body.
2560+
2561+
Returns `true` if the entire data was flushed successfully to the kernel
2562+
buffer. Returns `false` if all or part of the data was queued in user
2563+
memory. Event `drain` will be emitted when the buffer is free again.
2564+
21932565
## `http.METHODS`
21942566
<!-- YAML
21952567
added: v0.11.8
@@ -2722,6 +3094,7 @@ try {
27223094
[`http.ClientRequest`]: #http_class_http_clientrequest
27233095
[`http.IncomingMessage`]: #http_class_http_incomingmessage
27243096
[`http.Server`]: #http_class_http_server
3097+
[`http.ServerResponse`]: #http_class_http_serverresponse
27253098
[`http.get()`]: #http_http_get_options_callback
27263099
[`http.globalAgent`]: #http_http_globalagent
27273100
[`http.request()`]: #http_http_request_options_callback
@@ -2731,6 +3104,7 @@ try {
27313104
[`net.Socket`]: net.md#net_class_net_socket
27323105
[`net.createConnection()`]: net.md#net_net_createconnection_options_connectlistener
27333106
[`new URL()`]: url.md#url_new_url_input_base
3107+
[`outgoingMessage.socket`]: #http_outgoingMessage.socket
27343108
[`removeHeader(name)`]: #http_request_removeheader_name
27353109
[`request.end()`]: #http_request_end_data_encoding_callback
27363110
[`request.destroy()`]: #http_request_destroy_error

0 commit comments

Comments
 (0)