Skip to content

NodeJSBackend

hhh edited this page Oct 2, 2023 · 2 revisions

NodeJSBackend

/**
 * Type of NodeJS-like response objects.
 */
interface NodeJSResponseLike {
    setHeader(key: string, value: string): void;
    write(content: string): void;
    end(): void;
}

/**
 * Type of options of {@link NodeJSBackend}.
 */
interface NodeJSBackendOptions<ResponseType extends NodeJSResponseLike> {
    /**
     * Response objects.
     * @default []
     */
    responses?: Iterable<ResponseType>;
    /**
     * HTTP headers used in initialization.
     * @default defaultHeaders
     */
    headers?: HTTPHeaders;
}

/**
 * Class of NodeJS response adaptors.
 */
class NodeJSBackend<ResponseType extends NodeJSResponseLike = NodeJSResponseLike> implements BackendAdaptor {

    /**
     * Constructor of {@link NodeJSBackend}.
     */
    constructor(options?: NodeJSBackendOptions<ResponseType>);

    /**
     * Response objects.
     */
    responses: Set<ResponseType>;

    /**
     * HTTP headers used in initialization.
     */
    headers: HTTPHeaders;

    /**
     * Add a response and initialize it.
     */
    addResponse(response: ResponseType): void;

    /**
     * Remove a response.
     */
    removeResponse(response: ResponseType): void;

    /**
     * Initialize a single response.
     */
    initializeResponse(response: ResponseType): void;

    /**
     * Initialize responses.
     */
    initialize(): void;

    /**
     * Send content using `response.write()`.
     */
    send(content: string): void;

    /**
     * End all responses and clear `this.responses`.
     */
    clear(): void;
}
Clone this wiki locally