import type * as http from './HTTP/index.ts' import type { Input, Output } from './io.ts' export class Interception implements Interceptor { private readonly interceptors: Interceptor[] public constructor(interceptors: Interceptor[], options: http.Options) { this.interceptors = interceptors // interceptors are module singletons, so a second gateway in one process — // which is how the features run — would otherwise inherit the first one's state for (const interceptor of interceptors) { interceptor.reset?.() interceptor.mount?.(options) } } public async intercept(input: Input): Promise { for (const interceptor of this.interceptors) { let output = interceptor.intercept(input) // most answer at once, and awaiting a value that is not a promise still makes one if (output instanceof Promise) output = await output if (output !== null) return output } return null } } export interface Interceptor { intercept: (input: Input) => Output | Promise /** Discards whatever the interceptor accumulated while serving. */ reset?: () => void /** What the annotation says, for an interceptor that answers from it. */ mount?: (options: http.Options) => void }