Skip to content

@fkn/lib/http

new Agent(options?): Agent;

Record<string, unknown> = {}

Agent

keepAlive: boolean;
maxFreeSockets: number;
maxSockets: number;
options: Record<string, unknown>;
destroy(): void;

void

getName(options?): string;

string

string

number

string


  • OutgoingMessage
new ClientRequest(options, callback?): ClientRequest;

ClientRequestArgs

(res) => void

ClientRequest

OutgoingMessage.constructor
aborted: boolean = false;
chunkedEncoding: boolean = false;
OutgoingMessage.chunkedEncoding
finished: boolean = false;
OutgoingMessage.finished
headersSent: boolean = false;
OutgoingMessage.headersSent
host: string;
method: string;
path: string;
protocol: string;
res: IncomingMessage | null = null;
socket: Socket;
OutgoingMessage.socket
get connection(): Socket;

alias retained for Node compatibility

Socket

abort(): void;

void

destroy(error?): this;

Destroy the stream. Optionally emit an 'error' event, and emit a 'close' event (unless emitClose is set to false). After this call, the writable stream has ended and subsequent calls to write() or end() will result in an ERR_STREAM_DESTROYED error. This is a destructive and immediate way to destroy a stream. Previous calls to write() may not have drained, and may trigger an ERR_STREAM_DESTROYED error. Use end() instead of destroy if data should flush before close, or wait for the 'drain' event before destroying the stream.

Once destroy() has been called any further calls will be a no-op and no further errors except from _destroy() may be emitted as 'error'.

Implementors should not override this method, but instead implement writable._destroy().

Error

Optional, an error to emit with 'error' event.

this

v8.0.0

OutgoingMessage.destroy
end(
chunk?,
encoding?,
callback?): this;

unknown

unknown

unknown

this

OutgoingMessage.end
getHeader(name): string | number | string[] | undefined;

string

string | number | string[] | undefined

OutgoingMessage.getHeader
getHeaderNames(): string[];

string[]

OutgoingMessage.getHeaderNames
getHeaders(): OutgoingHttpHeaders;

OutgoingHttpHeaders

OutgoingMessage.getHeaders
hasHeader(name): boolean;

string

boolean

OutgoingMessage.hasHeader
removeHeader(name): void;

string

void

OutgoingMessage.removeHeader
setHeader(name, value): this;

string

string | number | readonly string[]

this

OutgoingMessage.setHeader
setNoDelay(noDelay?): void;

boolean

void

setSocketKeepAlive(enable?, initialDelay?): void;

boolean

number

void

setTimeout(timeout, callback?): this;

number

() => void

this

write(
chunk,
encoding?,
callback?): boolean;

The writable.write() method writes some data to the stream, and calls the supplied callback once the data has been fully handled. If an error occurs, the callback will be called with the error as its first argument. The callback is called asynchronously and before 'error' is emitted.

The return value is true if the internal buffer is less than the highWaterMark configured when the stream was created after admitting chunk. If false is returned, further attempts to write data to the stream should stop until the 'drain' event is emitted.

While a stream is not draining, calls to write() will buffer chunk, and return false. Once all currently buffered chunks are drained (accepted for delivery by the operating system), the 'drain' event will be emitted. Once write() returns false, do not write more chunks until the 'drain' event is emitted. While calling write() on a stream that is not draining is allowed, Node.js will buffer all written chunks until maximum memory usage occurs, at which point it will abort unconditionally. Even before it aborts, high memory usage will cause poor garbage collector performance and high RSS (which is not typically released back to the system, even after the memory is no longer required). Since TCP sockets may never drain if the remote peer does not read the data, writing a socket that is not draining can keep buffering until the process runs out of memory.

Writing data while the stream is not draining is particularly problematic for a Transform, because the Transform streams are paused by default until they are piped or a 'data' or 'readable' event handler is added.

If the data to be written can be generated or fetched on demand, it is recommended to encapsulate the logic into a Readable and use pipe. However, if calling write() is preferred, it is possible to respect backpressure and avoid memory issues using the 'drain' event:

function write(data, cb) {
if (!stream.write(data)) {
stream.once('drain', cb);
} else {
process.nextTick(cb);
}
}
// Wait for cb to be called before doing any other write.
write('hello', () => {
console.log('Write completed, do more writes now.');
});

A Writable stream in object mode will always ignore the encoding argument.

unknown

Optional data to write. For streams not operating in object mode, chunk must be a {string}, {Buffer}, {TypedArray} or {DataView}. For object mode streams, chunk may be any JavaScript value other than null.

BufferEncoding | ((error) => void)

The encoding, if chunk is a string.

(error) => void

Callback for when this chunk of data is flushed.

boolean

false if the stream wishes for the calling code to wait for the 'drain' event to be emitted before continuing to write additional data; otherwise true.

v0.9.4

OutgoingMessage.write

new HTTPParser(mode, handlers): HTTPParser;

"request" | "response"

ParserHandlers

HTTPParser

skipBody: boolean = false;
execute(chunk): void;

Uint8Array

void

finish(): void;

void


  • Readable
new IncomingMessage(socket): IncomingMessage;

Socket

IncomingMessage

Stream.Readable.constructor
complete: boolean = false;
headers: IncomingHttpHeaders = {};
httpVersion: string = '1.1';
httpVersionMajor: number = 1;
httpVersionMinor: number = 1;
optional method?: string;

request only

rawHeaders: string[] = [];
rawTrailers: string[] = [];
socket: Socket;
optional statusCode?: number;

response only

optional statusMessage?: string;

response only

trailers: Record<string, string> = {};
optional url?: string;

request only

get connection(): Socket;

Socket

setTimeout(msecs, callback?): this;

number

() => void

this


  • OutgoingMessage
new ServerResponse(req): ServerResponse;

IncomingMessage

ServerResponse

OutgoingMessage.constructor
chunkedEncoding: boolean = false;

ClientRequest.chunkedEncoding

finished: boolean = false;

ClientRequest.finished

headersSent: boolean = false;

ClientRequest.headersSent

req: IncomingMessage;
sendDate: boolean = true;
socket: Socket | null;
OutgoingMessage.socket
statusCode: number = 200;
optional statusMessage?: string;
end(
chunk?,
encoding?,
callback?): this;

Calling the writable.end() method signals that no more data will be written to the Writable. The optional chunk and encoding arguments allow one final additional chunk of data to be written immediately before closing the stream.

Calling the write method after calling end will raise an error.

// Write 'hello, ' and then end with 'world!'.
import fs from 'node:fs';
const file = fs.createWriteStream('example.txt');
file.write('hello, ');
file.end('world!');
// Writing more now is not allowed!

unknown

Optional data to write. For streams not operating in object mode, chunk must be a {string}, {Buffer}, {TypedArray} or {DataView}. For object mode streams, chunk may be any JavaScript value other than null.

BufferEncoding | (() => void)

The encoding if chunk is a string

() => void

Callback for when the stream is finished.

this

v0.9.4

OutgoingMessage.end
getHeader(name): string | number | string[] | undefined;

string

string | number | string[] | undefined

OutgoingMessage.getHeader
getHeaderNames(): string[];

string[]

OutgoingMessage.getHeaderNames
getHeaders(): OutgoingHttpHeaders;

OutgoingHttpHeaders

OutgoingMessage.getHeaders
hasHeader(name): boolean;

string

boolean

OutgoingMessage.hasHeader
removeHeader(name): void;

string

void

OutgoingMessage.removeHeader
setHeader(name, value): this;

string

string | number | readonly string[]

this

OutgoingMessage.setHeader
setTimeout(msecs, callback?): this;

number

() => void

this

write(
chunk,
encoding?,
callback?): boolean;

The writable.write() method writes some data to the stream, and calls the supplied callback once the data has been fully handled. If an error occurs, the callback will be called with the error as its first argument. The callback is called asynchronously and before 'error' is emitted.

The return value is true if the internal buffer is less than the highWaterMark configured when the stream was created after admitting chunk. If false is returned, further attempts to write data to the stream should stop until the 'drain' event is emitted.

While a stream is not draining, calls to write() will buffer chunk, and return false. Once all currently buffered chunks are drained (accepted for delivery by the operating system), the 'drain' event will be emitted. Once write() returns false, do not write more chunks until the 'drain' event is emitted. While calling write() on a stream that is not draining is allowed, Node.js will buffer all written chunks until maximum memory usage occurs, at which point it will abort unconditionally. Even before it aborts, high memory usage will cause poor garbage collector performance and high RSS (which is not typically released back to the system, even after the memory is no longer required). Since TCP sockets may never drain if the remote peer does not read the data, writing a socket that is not draining can keep buffering until the process runs out of memory.

Writing data while the stream is not draining is particularly problematic for a Transform, because the Transform streams are paused by default until they are piped or a 'data' or 'readable' event handler is added.

If the data to be written can be generated or fetched on demand, it is recommended to encapsulate the logic into a Readable and use pipe. However, if calling write() is preferred, it is possible to respect backpressure and avoid memory issues using the 'drain' event:

function write(data, cb) {
if (!stream.write(data)) {
stream.once('drain', cb);
} else {
process.nextTick(cb);
}
}
// Wait for cb to be called before doing any other write.
write('hello', () => {
console.log('Write completed, do more writes now.');
});

A Writable stream in object mode will always ignore the encoding argument.

unknown

Optional data to write. For streams not operating in object mode, chunk must be a {string}, {Buffer}, {TypedArray} or {DataView}. For object mode streams, chunk may be any JavaScript value other than null.

BufferEncoding | ((error) => void)

The encoding, if chunk is a string.

(error) => void

Callback for when this chunk of data is flushed.

boolean

false if the stream wishes for the calling code to wait for the 'drain' event to be emitted before continuing to write additional data; otherwise true.

v0.9.4

OutgoingMessage.write
writeContinue(): void;

void

writeHead(
statusCode,
statusMessage?,
headers?): this;

number

string | OutgoingHttpHeaders | (OutgoingHttpHeader | undefined)[]

OutgoingHttpHeaders

this

  • RequestOptions
optional host?: string;
RequestOptions.host
optional hostname?: string;
RequestOptions.hostname
optional path?: string;
RequestOptions.path
optional port?: string | number;
RequestOptions.port

headers: IncomingHttpHeaders;
httpVersion: string;
httpVersionMajor: number;
httpVersionMinor: number;
optional method?: string;

request only

rawHeaders: string[];
optional statusCode?: number;

response only

optional statusMessage?: string;

response only

optional url?: string;

request only


req: IncomingMessage;
type RequestListener = (req, res) => void;

IncomingMessage

ServerResponse

void


type Server = ServerImpl;
const _default: object;
Agent: typeof Agent;
ClientRequest: typeof ClientRequest;
createServer: (options?, requestListener?) => ServerImpl;

object | RequestListener

RequestListener

ServerImpl

get: (...args) => ClientRequest;

| [string | URL | ClientRequestArgs, (res) => void] | [string | URL, ClientRequestArgs, (res) => void]

ClientRequest

globalAgent: Agent;
IncomingMessage: typeof IncomingMessage;
METHODS: string[];
OutgoingMessage: typeof OutgoingMessage;
request: (...args) => ClientRequest;

| [string | URL | ClientRequestArgs, (res) => void] | [string | URL, ClientRequestArgs, (res) => void]

ClientRequest

Server: typeof ServerImpl;
ServerResponse: typeof ServerResponse;
STATUS_CODES: Record<number, string>;

const globalAgent: Agent;

const METHODS: string[];

const Server: typeof ServerImpl;

const STATUS_CODES: Record<number, string>;
function createServer(options?, requestListener?): ServerImpl;

object | RequestListener

RequestListener

ServerImpl


function get(...args): ClientRequest;

| [string | URL | ClientRequestArgs, (res) => void] | [string | URL, ClientRequestArgs, (res) => void]

ClientRequest


function request(...args): ClientRequest;

| [string | URL | ClientRequestArgs, (res) => void] | [string | URL, ClientRequestArgs, (res) => void]

ClientRequest

Renames and re-exports _default