summaryrefslogtreecommitdiff
path: root/u/server/response/pengueno.ts
blob: 5a953db5b87c5d8087c0e878cbbbfcb5867c78c0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
import {
    BaseResponse,
    Body,
    HttpStatusCodes,
    ITraceable,
    Metric,
    Optional,
    PenguenoRequest,
    ResponseOpts,
    ServerTrace,
} from '@emprespresso/pengueno';

const getHeaders = (req: PenguenoRequest, extraHeaders: Record<string, string>) => {
    const optHeaders = {
        ...req.getResponseHeaders(),
        ...extraHeaders,
    };
    optHeaders['Content-Type'] = (optHeaders['Content-Type'] ?? 'text/plain') + '; charset=utf-8';
    return optHeaders;
};

const ResponseCodeMetrics = [0, 1, 2, 3, 4, 5].map((x) => Metric.fromName(`response.${x}xx`).asResult());
export const getResponseMetrics = (status: number, elapsedMs?: number) => {
    const index = Math.floor(status / 100);
    return ResponseCodeMetrics.flatMap((metric, i) =>
        Optional.from(i)
            .filter((i) => i === index)
            .map(() => [metric.count.withValue(1.0)])
            .flatMap((metricValues) =>
                Optional.from(elapsedMs)
                    .map((ms) => metricValues.concat(metric.time.withValue(ms)))
                    .orSome(() => metricValues),
            )
            .orSome(() => [metric.count.withValue(0.0)])
            .get(),
    );
};

export class PenguenoResponse implements BaseResponse {
    public readonly statusText: string;
    public readonly status: number;
    public readonly headers: Record<string, string>;

    constructor(
        req: ITraceable<PenguenoRequest, ServerTrace>,
        private readonly _body: Body,
        opts: ResponseOpts,
    ) {
        this.headers = getHeaders(req.get(), opts?.headers ?? {});
        this.status = opts.status;
        this.statusText = opts.statusText ?? HttpStatusCodes[this.status]!;

        req.trace.trace(getResponseMetrics(opts.status, req.get().elapsedTimeMs()));
    }

    public body() {
        return this._body;
    }
}