Network - Full Reference
Exhaustive reference for BaseNetworkRequest and its fetchers, the TCP/TLS client-server hierarchy, NetworkUdpClient, and every option type. For a readable introduction and the most common tasks, start with the Network overview.
Files:
packages/helpers/src/modules/network/http-request/base-network-request.helper.ts-BaseNetworkRequestpackages/helpers/src/modules/network/http-request/fetcher/base-fetcher.ts-IFetchable,IRequestOptions,AbstractNetworkFetchableHelperpackages/helpers/src/modules/network/http-request/fetcher/node-fetcher.ts-NodeFetcher,NodeFetchNetworkRequestpackages/helpers/src/modules/network/http-request/fetcher/axios-fetcher.ts-AxiosFetcher,AxiosNetworkRequest(@venizia/ignis-helpers/axiossub-path)packages/helpers/src/modules/network/http-request/types.ts-TFetcherVariant,TFetcherResponse,TFetcherWorkerpackages/helpers/src/modules/network/tcp-socket/base-tcp-server.helper.ts-BaseNetworkTcpServer,ITcpSocketClient,ITcpSocketServerOptionspackages/helpers/src/modules/network/tcp-socket/base-tcp-client.helper.ts-BaseNetworkTcpClient,INetworkTcpClientPropspackages/helpers/src/modules/network/tcp-socket/network-tcp-server.helper.ts-NetworkTcpServerpackages/helpers/src/modules/network/tcp-socket/network-tcp-client.helper.ts-NetworkTcpClientpackages/helpers/src/modules/network/tcp-socket/network-tls-tcp-server.helper.ts-NetworkTlsTcpServerpackages/helpers/src/modules/network/tcp-socket/network-tls-tcp-client.helper.ts-NetworkTlsTcpClientpackages/helpers/src/modules/network/udp-socket/network-udp-client.helper.ts-NetworkUdpClient,INetworkUdpClientPropspackages/helpers/src/common/redact.ts-redactSecrets,redactUrlCredentialspackages/helpers/src/common/constants/http.ts-HTTP.Methods,THttpMethod
Architecture
BaseHelper
├── BaseNetworkRequest<T extends TFetcherVariant>
│ ├── AxiosNetworkRequest (T = 'axios')
│ └── NodeFetchNetworkRequest (T = 'node-fetch')
│
├── BaseNetworkTcpServer<ServerOpts, ServerType, ClientType>
│ ├── NetworkTcpServer (net.createServer, net.Socket)
│ └── NetworkTlsTcpServer (tls.createServer, tls.TLSSocket)
│
├── BaseNetworkTcpClient<ClientOpts, ClientType>
│ ├── NetworkTcpClient (net.connect, net.Socket)
│ └── NetworkTlsTcpClient (tls.connect, tls.TLSSocket)
│
└── NetworkUdpClient
AbstractNetworkFetchableHelper<V, RQ, RS> (implements IFetchable, NOT a BaseHelper)
├── AxiosFetcher (V = 'axios')
└── NodeFetcher (V = 'node-fetch')All classes that extend BaseHelper inherit scoped logging via this.logger. AbstractNetworkFetchableHelper and its two fetchers do not extend BaseHelper - they accept an optional logger parameter per call instead (see the Request Logging & Redaction section under HTTP Request API).
HTTP Request API
BaseNetworkRequest
class BaseNetworkRequest<T extends TFetcherVariant> extends BaseHelperBase class for HTTP request helpers. Holds a base URL and delegates to an IFetchable fetcher.
Constructor
constructor(opts: {
name: string;
baseUrl?: string;
fetcher: IFetchable<T, IRequestOptions, TFetcherResponse<T>>;
})| Parameter | Type | Description |
|---|---|---|
name | string | Helper name, used as both scope and identifier for logging |
baseUrl | string | Base URL prepended to request paths. Defaults to '' |
fetcher | IFetchable | The underlying HTTP fetcher implementation |
Methods
getRequestUrl(opts)
Builds a full URL by combining a base URL with path segments. Throws if no base URL is available.
getRequestUrl(opts: {
baseUrl?: string;
paths: Array<string>;
}): string| Parameter | Type | Description |
|---|---|---|
baseUrl | string | Overrides the instance's base URL. Falls back to this.baseUrl. An explicit empty string does not fall back - it throws |
paths | string[] | Path segments to append. Each is prefixed with / if missing |
Throws: ApplicationError (via getError, statusCode: 500) with message '[getRequestUrl] Invalid configuration for third party request base url!' when both opts.baseUrl and this.baseUrl resolve to empty.
Example:
client.getRequestUrl({ paths: ['v1', 'users', '123'] });
// => 'https://api.example.com/v1/users/123'
client.getRequestUrl({ baseUrl: 'https://other.api.com', paths: ['health'] });
// => 'https://other.api.com/health'getRequestPath(opts)
Joins path segments, ensuring each starts with /. An empty paths array joins to ''.
getRequestPath(opts: { paths: Array<string> }): stringExample:
client.getRequestPath({ paths: ['v1', 'users'] });
// => '/v1/users'getNetworkService()
Returns the underlying IFetchable fetcher instance.
getNetworkService(): IFetchable<T, IRequestOptions, TFetcherResponse<T>>getWorker()
Returns the raw HTTP client from the fetcher (AxiosInstance for Axios, typeof fetch for Node Fetch). Delegates to fetcher.getWorker().
getWorker(): TFetcherWorker<T>IFetchable Interface
interface IFetchable<
V extends TFetcherVariant,
RQ extends IRequestOptions,
RS extends TFetcherResponse<V>,
> {
send(opts: RQ, logger?: any): Promise<RS>;
get(opts: RQ, logger?: any): Promise<RS>;
post(opts: RQ, logger?: any): Promise<RS>;
put(opts: RQ, logger?: any): Promise<RS>;
patch(opts: RQ, logger?: any): Promise<RS>;
delete(opts: RQ, logger?: any): Promise<RS>;
query(opts: RQ, logger?: any): Promise<RS>;
getWorker(): TFetcherWorker<V>;
}All HTTP method shortcuts (get, post, put, patch, delete, query) delegate to send() with the method field set accordingly. query sends the HTTP QUERY method (RFC 9110/10008) - a GET-semantics request that carries a body, useful for search payloads too large for a query string.
IRequestOptions
interface IRequestOptions {
url: string;
method?: THttpMethod;
params?: AnyObject;
timeout?: number;
[extra: symbol | string]: any;
}HTTP.Methods - the const-class every fetcher dispatches on
HTTP.Methods = {
GET: 'get', POST: 'post', PUT: 'put', PATCH: 'patch',
DELETE: 'delete', HEAD: 'head', OPTIONS: 'options', QUERY: 'query',
} as const;
type THttpMethod = ValueOf<typeof HTTP.Methods> | Uppercase<ValueOf<typeof HTTP.Methods>>;IMPORTANT
- Every
HTTP.Methodstoken is lowercase.@hono/zod-openapiroute definitions accept no other case. - Both fetchers accept either case on input (
method: 'post'ormethod: 'POST') but always callmethod.toUpperCase()immediately before dispatching to their transport. - This is not cosmetic. Node's undici (the
fetchimplementation on Node, not Bun) auto-normalizes onlyDELETE/GET/HEAD/OPTIONS/POST/PUTand sends any other token (PATCH,QUERY) through verbatim - a lowercasepatchwould reach the server unchanged and most servers reject it. - Bun and Axios hide the bug. Bun's
fetchand Axios (vianode:http) uppercase every method themselves, so the bug surfaces only once the app runs on Node with undici.
AbstractNetworkFetchableHelper
abstract class AbstractNetworkFetchableHelper<
V extends TFetcherVariant,
RQ extends IRequestOptions,
RS extends TFetcherResponse<V>,
> implements IFetchable<V, RQ, RS>Abstract base for fetcher implementations. Provides convenience HTTP method wrappers and protocol detection. Does not extend BaseHelper - it has no this.logger.
Constructor
constructor(opts: { name: string; variant: V })Methods
abstract send(opts, logger?)
Subclasses must implement the actual request dispatch.
abstract send(opts: RQ, logger?: any): Promise<RS>;get(opts, logger?) / post(opts, logger?) / put(opts, logger?) / patch(opts, logger?) / delete(opts, logger?) / query(opts, logger?)
get(opts: RQ, logger?: any): Promise<RS>
post(opts: RQ, logger?: any): Promise<RS>
put(opts: RQ, logger?: any): Promise<RS>
patch(opts: RQ, logger?: any): Promise<RS>
delete(opts: RQ, logger?: any): Promise<RS>
query(opts: RQ, logger?: any): Promise<RS>Each calls send() with method set to the matching HTTP.Methods token (get → HTTP.Methods.GET, ... query → HTTP.Methods.QUERY). Any other field on opts (body, headers, params, ...) passes through untouched.
getProtocol(url)
Returns HTTP.Protocols.HTTP ('http') if url starts with 'http:', otherwise HTTP.Protocols.HTTPS ('https').
getProtocol(url: string): 'http' | 'https'getWorker()
Returns the underlying HTTP client instance (set by the concrete fetcher's constructor).
getWorker(): TFetcherWorker<V>AxiosFetcher
class AxiosFetcher extends AbstractNetworkFetchableHelper<
'axios',
IAxiosRequestOptions,
AxiosResponse['data']
>Axios-based fetcher implementation. Creates an axios instance with the provided default configuration.
Constructor
constructor(opts: {
name: string;
defaultConfigs: AxiosRequestConfig;
logger?: any;
})opts.logger, if provided, logs 'Creating new network request worker instance! Name: %s' once at construction time - unrelated to the per-call logger argument on send().
IAxiosRequestOptions
interface IAxiosRequestOptions extends AxiosRequestConfig, IRequestOptions {
url: string;
method?: THttpMethod;
params?: AnyObject;
body?: AnyObject; // Mapped to Axios `data`
headers?: AnyObject;
}NOTE
bodymaps to Axios'sdatafield internally.- Query
paramsare serialized usingnode:querystringvia Axios'sparamsSerializer. - HTTPS gets an
https.Agentautomatically, withrejectUnauthorizeddefaulting tofalse- override it per request withrejectUnauthorized: true.
Methods
send(opts, logger?)
override send<T = any>(opts: IAxiosRequestOptions, logger?: any): Promise<AxiosResponse<T>>Dispatches the request via the internal axios instance.
methoddefaults toHTTP.Methods.GETand is uppercased before dispatch.- HTTPS URLs automatically get an
https.Agentconfigured. - If
loggeris passed, logs'URL: %s | Props: %s'atinfolevel with the assembled request config run throughredactSecrets().
AxiosNetworkRequest
class AxiosNetworkRequest extends BaseNetworkRequest<'axios'>Pre-configured HTTP client using Axios. Import only from the sub-path - import { AxiosNetworkRequest } from '@venizia/ignis-helpers/axios'.
Constructor
constructor(opts: IAxiosNetworkRequestOptions)interface IAxiosNetworkRequestOptions {
name: string;
networkOptions: Omit<AxiosRequestConfig, 'baseURL'> & {
baseUrl?: string;
};
}Default configuration applied:
| Setting | Default |
|---|---|
headers['content-type'] | 'application/json; charset=utf-8' |
withCredentials | true |
validateStatus | (status) => status < 500 |
timeout | 60000 (1 minute) |
User-provided values in networkOptions override all defaults. networkOptions.baseUrl maps to Axios's baseURL.
NodeFetcher
class NodeFetcher extends AbstractNetworkFetchableHelper<
'node-fetch',
INodeFetchRequestOptions,
Awaited<ReturnType<typeof fetch>>
>Native fetch-based fetcher implementation.
Constructor
constructor(opts: {
name: string;
defaultConfigs: RequestInit;
logger?: any;
})INodeFetchRequestOptions
interface INodeFetchRequestOptions extends RequestInit, IRequestOptions {
url: string;
method?: THttpMethod;
params?: AnyObject;
}Methods
send(opts, logger?)
override async send(opts: INodeFetchRequestOptions, logger?: any): Promise<Response>Dispatches the request using the native fetch API. Behavior:
methoddefaults toHTTP.Methods.GETand is uppercased before dispatch.- Query
paramsare serialized withnode:querystringand appended tourl- with?if the URL carries no query string yet,&if it already does (never a double?). - If
timeoutis provided, an internalAbortControlleraborts the request after that many milliseconds.- The internal timeout signal is composed, never substituted, with a caller-supplied
signal:AbortSignal.any([signal, timeoutController.signal])when both are present - so a caller aborting its own signal still cancels the request even while a timeout is also armed. - The timer is cleared as soon as the request settles.
- The internal timeout signal is composed, never substituted, with a caller-supplied
- If
loggeris passed, logs'URL: %s | Props: %s | Timeout: %s'atinfolevel with the request config run throughredactSecrets().
NodeFetchNetworkRequest
class NodeFetchNetworkRequest extends BaseNetworkRequest<'node-fetch'>Pre-configured HTTP client using native fetch. Exported from the root barrel - no extra dependency.
Constructor
constructor(opts: INodeFetchNetworkRequestOptions)interface INodeFetchNetworkRequestOptions {
name: string;
networkOptions: RequestInit & {
baseUrl?: string;
};
}Default configuration applied:
| Setting | Default |
|---|---|
headers['content-type'] | 'application/json; charset=utf-8' |
If headers is a Headers instance, it is converted to a plain object via Object.fromEntries(headers.entries()) before merging with the default.
`timeout` is per-call, not per-instance
networkOptions is RequestInit, which has no timeout field - passing one there has no effect on request cancellation. NodeFetcher.send() only reads timeout from the arguments of each individual send()/get()/... call. Pass it every time you need an abort:
await this.getNetworkService().send({ url: '/slow-endpoint', method: 'get', timeout: 5000 });Request Logging & Redaction
Neither fetcher logs anything by default - send() and every shortcut accept an optional logger as the second argument, and the log call is guarded with logger?.for(...). Pass one (typically this.logger from a BaseHelper subclass, or this.logger on a class extending BaseNetworkRequest) to get an info-level line per request:
await this.getNetworkService().post({ url, body }, this.logger);Whenever a logger is passed, the assembled request config - url, method, headers, body/data, and any other options - is run through redactSecrets() before the log line is written. Redaction matches by key name, case-insensitively, at any depth, against SECRET_KEY_PATTERN:
| Key group | Matched spellings |
|---|---|
| Options-object (camelCase) | pass, password, passphrase, secret, token, apiKey, accessKey, secretKey, privateKey, key, cert, ca, pfx, credentials, authorization, auth, jwtSecret, applicationSecret, connectionString |
| Options-object (snake_case) | api_key, access_key, secret_key, private_key |
| HTTP header spellings | cookie, set-cookie, proxy-authorization, www-authenticate, and any (x-)?(api|auth|access|secret|session|csrf|xsrf)-(key|token|secret|id) pattern (e.g. x-api-key, x-auth-token, x-csrf-token) |
Source: redact.ts SECRET_KEY_PATTERN.
A matched value is replaced with '[REDACTED]'; Buffer/typed-array values under a non-matching key are summarized as '[Binary N bytes]' rather than serialized. There is nothing to configure beyond passing the logger - you never call redactSecrets() yourself at the call site.
TCP Socket API
BaseNetworkTcpServer
class BaseNetworkTcpServer<
SocketServerOptions extends ServerOpts = ServerOpts,
SocketServerType extends SocketServer = SocketServer,
SocketClientType extends SocketClient = SocketClient,
> extends BaseHelperAbstract TCP server with client tracking, authentication flow, and event delegation.
Constructor
constructor(opts: ITcpSocketServerOptions<SocketServerOptions, SocketServerType, SocketClientType>)Throws: ApplicationError with message 'TCP Server | Invalid authenticate duration | Required duration for authenticateOptions' when authenticateOptions.required is true and duration is missing, 0, or negative.
The constructor calls configure(), which creates the server via createServerFn and starts listening.
Protected Properties
| Property | Type | Description |
|---|---|---|
serverOptions | Partial<SocketServerOptions> | Server creation options |
listenOptions | Partial<ListenOptions> | Listen configuration |
authenticateOptions | { required: boolean; duration?: number } | Auth settings |
clients | Record<string, ITcpSocketClient<SocketClientType>> | Connected client registry |
server | SocketServerType | The underlying server instance |
extraEvents | Record<string, (opts) => ValueOrPromise<void>> | Additional per-client socket events to register |
- Hooks never crash the process.
onClientData,onClientConnected,onClientClose,onClientError,onServerReady,onServerError, and eachextraEventsentry all run through an internalinvokeHook()wrapper. - Why it exists. A hook throwing synchronously inside a raw
net/tlsevent listener would otherwise be an uncaught exception that crashes the process;invokeHook()catches it and logs instead.
Methods
configure()
Creates the server using createServerFn, registers a server-level error listener (routed to onServerError, never left to crash the process), and starts listening. Called automatically by the constructor.
configure(): voidonNewConnection(opts)
Handles a new client connection:
- Assigns a unique ID via
getUID(). - Registers
data/error/close/extraEventslisteners on the socket. - Tracks the client in the
clientsregistry. - Invokes
onClientConnected. - Starts the authentication kick-timer when
authenticateOptions.requiredistrue.
onNewConnection(opts: { socket: SocketClientType }): voidgetClients() / getClient(opts) / getServer()
getClients(): Record<string, ITcpSocketClient<SocketClientType>>
getClient(opts: { id: string }): ITcpSocketClient<SocketClientType> | undefined
getServer(): SocketServerTypedoAuthenticate(opts)
Transitions a client's authentication state. Sets storage.authenticatedAt when the state becomes 'authenticated' and clears the pending kick-timer; clears authenticatedAt for the other two states.
doAuthenticate(opts: {
id: string;
state: 'unauthorized' | 'authenticating' | 'authenticated';
}): voidemit(opts)
Writes data to a specific client's socket. Never throws - each failure case logs and returns instead:
| Condition | Log level |
|---|---|
Client not found for clientId | error |
| Socket not writable | error |
Payload empty (!payload?.length) | info |
emit(opts: { clientId: string; payload: Buffer | string }): voidshutdown()
Tears the server down completely and resolves even while clients are still attached.
async shutdown(): Promise<void>Order of operations:
- Clears every client's pending authenticate kick-timer.
- Destroys every client socket.
- Empties the
clientsregistry. - Calls
server.close()and awaits its callback.
- Why this order.
server.close()alone never resolves while a socket is still attached, so a caller reaching throughgetServer().close()on a busy server hangs forever. - Idempotent. A second call is a no-op that resolves cleanly.
- Safe on a server that never finished
listen(). Logs the resultingERR_SERVER_NOT_RUNNINGrather than throwing. - After
shutdown(), new connection attempts are refused.
ITcpSocketClient
interface ITcpSocketClient<SocketClientType> {
id: string;
socket: SocketClientType;
state: 'unauthorized' | 'authenticating' | 'authenticated';
subscriptions: Set<string>;
storage: {
connectedAt: dayjs.Dayjs;
authenticatedAt: dayjs.Dayjs | null;
authenticateTimeout?: ReturnType<typeof setTimeout> | null;
[additionField: symbol | string]: any;
};
}NetworkTcpServer
class NetworkTcpServer extends BaseNetworkTcpServer<ServerOpts, Server, Socket>Plain TCP server using net.createServer.
Constructor
constructor(opts: Omit<ITcpSocketServerOptions, 'createServerFn'>)The createServerFn is pre-set to net.createServer. scope is hardcoded to 'NetworkTcpServer', overriding any opts.scope.
Static Methods
newInstance(opts)
static newInstance(
opts: Omit<ITcpSocketServerOptions, 'createServerFn'>
): NetworkTcpServerNetworkTlsTcpServer
class NetworkTlsTcpServer extends BaseNetworkTcpServer<TlsOptions, tls.Server, TLSSocket>TLS-encrypted TCP server using tls.createServer.
Constructor
constructor(opts: Omit<ITcpSocketServerOptions<TlsOptions, tls.Server, TLSSocket>, 'createServerFn'>)The createServerFn is pre-set to tls.createServer. scope is hardcoded to 'NetworkTlsTcpServer'. Pass TLS certificates and keys in serverOptions (type TlsOptions from node:tls). Every method, including shutdown(), is identical to NetworkTcpServer.
Static Methods
newInstance(opts)
static newInstance(
opts: Omit<ITcpSocketServerOptions<TlsOptions, tls.Server, TLSSocket>, 'createServerFn'>
): NetworkTlsTcpServerBaseNetworkTcpClient
class BaseNetworkTcpClient<
SocketClientOptions extends PlainConnectionOptions | TlsConnectionOptions,
SocketClientType extends PlainSocketClient | TlsSocketClient,
> extends BaseHelperAbstract TCP client with auto-reconnect, encoding support, and lifecycle hooks.
Constructor
constructor(opts: INetworkTcpClientProps<SocketClientOptions, SocketClientType>)Does not call connect() automatically - construction only stores options; call connect({ resetReconnectCounter }) explicitly.
Protected Properties
| Property | Type | Description |
|---|---|---|
client | SocketClientType | null | The underlying socket, or null/undefined when disconnected |
options | SocketClientOptions | Connection options |
reconnect | boolean | Whether auto-reconnect is enabled (default: false) |
retry | { maxReconnect: number; currentReconnect: number } | Reconnect state. maxReconnect defaults to 5 |
encoding | BufferEncoding | undefined | Socket encoding |
getLoggableOptions()
protected getLoggableOptions(): unknownReturns redactSecrets(this.options). A TLS client's options is its private key material (key/cert/passphrase), so every internal log call uses this instead of logging this.options directly - otherwise the key would be written to every log file and aggregator downstream.
Methods
connect(opts)
Establishes the connection:
- No-op with a log line if already connected (
isConnected()) or ifoptionsis empty. - Otherwise, destroys any stale
clientfirst, creates the socket viacreateClientFn, registersdata/close/errorlisteners, and appliesencodingif set.
connect(opts: { resetReconnectCounter: boolean }): void| Parameter | Type | Description |
|---|---|---|
resetReconnectCounter | boolean | If true, resets retry.currentReconnect to 0 before connecting |
disconnect()
Destroys the socket, clears the reconnect timeout, and sets client to null. No-op with a log line if client is not set.
disconnect(): voidforceReconnect()
Calls disconnect() then connect({ resetReconnectCounter: true }).
forceReconnect(): voidisConnected()
Returns a truthy value if client exists and its readyState is not 'closed'.
isConnected(): SocketClientType | null | undefinedemit(opts)
Writes data to the server. Logs and returns without throwing if client is not initialized or the payload is empty.
emit(opts: { payload: Buffer | string }): voidgetClient()
getClient(): SocketClientType | null | undefinedhandleConnected() / handleData(_opts) / handleClosed() / handleError(error)
Default handlers used when the matching onConnected/onData/onClosed/onError option is omitted.
handleConnected(): void // Logs, resets retry.currentReconnect to 0
handleData(_opts: { identifier: string; message: string | Buffer }): void // No-op
handleClosed(): void // Logs the closure
handleError(error: any): void // Logs; schedules a reconnect if eligibleReconnect gate on handleError:
| Condition | Result |
|---|---|
reconnect is false | No reconnect - the first guard returns immediately |
retry.currentReconnect >= retry.maxReconnect | No reconnect - the first guard returns immediately |
maxReconnect === -1 | No reconnect, ever. currentReconnect starts at 0, and 0 >= -1 is already true, so the first guard returns before any attempt is made |
reconnect is true and currentReconnect < maxReconnect (with maxReconnect >= 0) | Reconnect scheduled |
IMPORTANT
maxReconnect: -1does not mean unlimited reconnects - it disables reconnection entirely. This is the opposite of the common "-1= unlimited" convention elsewhere in the framework (e.g. Redis retry).- A second guard further down the method is dead code.
if (maxReconnect > -1 && currentReconnect >= maxReconnect)can never be true: by the time control reaches it, the first guard has already ruled outcurrentReconnect >= maxReconnect. - Source:
base-tcp-client.helper.ts.
The reconnect delay is a fixed 5000 ms - there is no backoff growth on the TCP/TLS client, unlike the Redis helper's exponential strategy.
NetworkTcpClient
class NetworkTcpClient extends BaseNetworkTcpClient<TcpSocketConnectOpts, Socket>Plain TCP client using net.connect.
Constructor
constructor(
opts: Omit<INetworkTcpClientProps<TcpSocketConnectOpts, Socket>, 'createClientFn'>
)The createClientFn is pre-set to net.connect. scope is hardcoded to 'NetworkTcpClient'.
Static Methods
newInstance(opts)
static newInstance(
opts: Omit<INetworkTcpClientProps<TcpSocketConnectOpts, Socket>, 'createClientFn'>
): NetworkTcpClientNetworkTlsTcpClient
class NetworkTlsTcpClient extends BaseNetworkTcpClient<ConnectionOptions, TLSSocket>TLS-encrypted TCP client using tls.connect.
Constructor
constructor(
opts: Omit<INetworkTcpClientProps<ConnectionOptions, TLSSocket>, 'createClientFn'>
)The createClientFn is pre-set to tls.connect. scope is hardcoded to 'NetworkTlsTcpClient'. Pass TLS certificates and keys in options (type ConnectionOptions from node:tls). Every method is identical to NetworkTcpClient.
Static Methods
newInstance(opts)
static newInstance(
opts: Omit<INetworkTcpClientProps<ConnectionOptions, TLSSocket>, 'createClientFn'>
): NetworkTlsTcpClientUDP Socket API
NetworkUdpClient
class NetworkUdpClient extends BaseHelperUDP datagram client with multicast support, using node:dgram internally (type: 'udp4'). No client/server split - one class handles both send and receive. scope is always hardcoded to 'NetworkUdpClient' (no scope option exists on INetworkUdpClientProps).
Constructor
constructor(opts: INetworkUdpClientProps)Construction only stores options; call connect() explicitly to bind the socket.
Static Methods
newInstance(opts)
static newInstance(opts: INetworkUdpClientProps): NetworkUdpClientMethods
connect()
- Creates a
dgram.Socket(type: 'udp4',reuseAddrfrom options), registersclose/error/listening/messagelisteners, then binds toport/host. - Each listener routes through an internal
invokeHook()wrapper (same synchronous-throw guard as the TCP server). onBindfires after binding completes - the place to join multicast groups viasocket.addMembership(group, iface).
connect(): voidNo-op with a log line if client is already set, or if port is not a non-negative integer (Number.isInteger(port) && port >= 0) - port 0 is a valid "OS assigns a free port" request and is accepted.
disconnect()
Closes the socket and sets client to null. No-op with a log line if client is not set.
disconnect(): voidisConnected()
Returns the underlying socket if bound, or null/undefined otherwise.
isConnected(): dgram.Socket | null | undefinedgetClient()
getClient(): dgram.Socket | null | undefinedhandleConnected() / handleData(opts) / handleClosed() / handleError(opts)
Default handlers used when the matching onConnected/onData/onClosed/onError option is omitted:
| Handler | Log level | Logged context |
|---|---|---|
handleConnected | info | host, port, multicastAddress |
handleClosed | info | host, port, multicastAddress |
handleData | info | host, port only - no multicastAddress |
handleError | error | host, port only - no multicastAddress |
handleConnected(): void
handleData(opts: { identifier: string; message: string | Buffer; remoteInfo: dgram.RemoteInfo }): void
handleClosed(): void
handleError(opts: { identifier: string; error: Error }): voidTypes Reference
TFetcherVariant / TFetcherResponse / TFetcherWorker
type TFetcherVariant = 'node-fetch' | 'axios';
type TFetcherResponse<T extends TFetcherVariant> =
T extends 'node-fetch' ? Response : AxiosResponse;
type TFetcherWorker<T extends TFetcherVariant> =
T extends 'axios' ? AxiosInstance : typeof fetch;THttpMethod
type THttpMethod = ValueOf<typeof HTTP.Methods> | Uppercase<ValueOf<typeof HTTP.Methods>>;
// 'get' | 'post' | 'put' | 'patch' | 'delete' | 'head' | 'options' | 'query'
// | 'GET' | 'POST' | 'PUT' | 'PATCH' | 'DELETE' | 'HEAD' | 'OPTIONS' | 'QUERY'ITcpSocketServerOptions
interface ITcpSocketServerOptions<
SocketServerOptions extends ServerOpts = ServerOpts,
SocketServerType extends SocketServer = SocketServer,
SocketClientType extends SocketClient = SocketClient,
> {
scope?: string;
identifier: string;
serverOptions: Partial<SocketServerOptions>;
listenOptions: Partial<ListenOptions>;
authenticateOptions: { required: boolean; duration?: number };
extraEvents?: Record<
string,
(opts: { id: string; socket: SocketClientType; args: any }) => ValueOrPromise<void>
>;
createServerFn: (
options: Partial<SocketServerOptions>,
connectionListener: (socket: SocketClientType) => void,
) => SocketServerType;
onServerReady?: (opts: { server: SocketServerType }) => void;
onClientConnected?: (opts: { id: string; socket: SocketClientType }) => void;
onClientData?: (opts: { id: string; socket: SocketClientType; data: Buffer | string }) => void;
onClientClose?: (opts: { id: string; socket: SocketClientType }) => void;
onClientError?: (opts: { id: string; socket: SocketClientType; error: Error }) => void;
onServerError?: (opts: { server: SocketServerType; error: Error }) => void;
}onServerError fires when the underlying net/tls server emits 'error' (for example EADDRINUSE from a port already in use) - without this listener the event is unhandled and takes the whole process down; with it, the error is routed here and the process keeps running.
INetworkTcpClientProps
interface INetworkTcpClientProps<
SocketClientOptions extends PlainConnectionOptions | TlsConnectionOptions,
SocketClientType extends PlainSocketClient | TlsSocketClient,
> {
identifier: string;
scope?: string;
options: SocketClientOptions;
reconnect?: boolean;
maxRetry?: number;
encoding?: BufferEncoding;
createClientFn: (
options: SocketClientOptions,
connectionListener?: () => void,
) => SocketClientType;
onConnected?: (opts: { client: SocketClientType }) => ValueOrPromise<void>;
onData?: (opts: { identifier: string; message: string | Buffer }) => ValueOrPromise<void>;
onClosed?: (opts: { client: SocketClientType }) => void;
onError?: (error: any) => void;
}INetworkUdpClientProps
interface INetworkUdpClientProps {
identifier: string;
host?: string;
port: number;
reuseAddr?: boolean;
multicastAddress?: {
groups?: Array<string>;
interface?: string;
};
onConnected?: (opts: { identifier: string; host?: string; port: number }) => void;
onData?: (opts: {
identifier: string;
message: string | Buffer;
remoteInfo: dgram.RemoteInfo;
}) => void;
onClosed?: (opts: { identifier: string; host?: string; port: number }) => void;
onError?: (opts: { identifier: string; host?: string; port: number; error: Error }) => void;
onBind?: (opts: {
identifier: string;
socket: dgram.Socket;
host?: string;
port: number;
reuseAddr?: boolean;
multicastAddress?: { groups?: Array<string>; interface?: string };
}) => ValueOrPromise<void>;
}See also
- Network overview - introduction and the most common tasks