import http from 'http'; import { TypedEmitter } from './events.js'; export type Port = number; export declare class Host { /** Hosname, if IPv6, it will be bracketed. */ readonly name: string; /** Explicit port, if any. */ readonly port?: Port | undefined; private constructor(); /** * Parses a host from a string or host instance, optionally adding a default * port if it was missing. */ static from(arg: string | Host, defaultPort: Port): Host & HasPort; static from(arg: string | Host, defaultPort?: Port): Host; toString(): string; } export interface HasPort { readonly port: Port; } /** * Returns the (string-formatted) address to which the server is bound, * or nothing if the server is not listening. HTTP protocol is assumed. */ export declare function serverHost(server: http.Server): Host | undefined; export interface BindableListeners { /** Emitted when the bindable was successfully bound to a given port. */ bound(host: Host): void; /** Emitted when the bindable was unbound. */ unbound(): void; } export type BindableStatus = 'unbound' | 'binding' | 'bound' | 'unbinding' | 'error'; declare const isBindableMarker: "@opvious/stl-utils/bindable:isBindable+v1"; export interface BindableOptions { /** * Emit events using process.send. If unset, events are set when process.send * is defined and NODE_ENV is not test (sending objects otherwise causes * errors). Currently, the following events are supported: * * * bindableBound, when the bindable is successfully bound. */ readonly emitProcessEvents?: boolean; } export declare abstract class Bindable extends TypedEmitter { static [Symbol.hasInstance](val: unknown): val is Bindable; private readonly [isBindableMarker]; private readonly sendProcessEvent; private status; private activeHost; protected constructor(opts?: BindableOptions); /** Returns the host to bind to. Called internally by `start`. */ protected abstract bind(): Promise; private updateStatus; /** Resolves to the current host, throwing if unbound. */ host(): Promise; /** * Start listening The new host is available via the `'bound'` event and the * `host` method. */ start(): this; /** * Stops listening. Safe to call multiple times. If the bindable is currently * binding it will be stopped right after it is bound. The returned promise * resolves when the bindable is unbound or immediately if it is not currently * bound. */ stop(): Promise; /** * Unbinds a bindable. The promise should resolve when the host is not bound * to anymore. This function is guaranteed to be called only when a host is * currently bound (and at most once per binding). */ protected abstract onStop(host: Host & HasPort): Promise; /** * Marks the bindable as unbound. This can be useful when the underlying * server may become unbound from external sources. */ protected unbind(): void; /** Starts the bindable then waits until it is unbound. */ run(): Promise; } export {};