summaryrefslogtreecommitdiff
path: root/lib/server/response/pengueno.ts
blob: e15d3f14097107eacb7b04886c8eed457d2980fb (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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
import {
    BaseResponse,
    Body,
    HttpStatusCodes,
    isEither,
    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;
    }
}

type Jsonable = any;
export class JsonResponse extends PenguenoResponse {
    constructor(req: ITraceable<PenguenoRequest, ServerTrace>, e: Jsonable, _opts: ResponseOpts) {
        const opts = { ..._opts, headers: { ..._opts.headers, 'Content-Type': 'application/json' } };
        if (isEither<Jsonable, Jsonable>(e)) {
            super(
                req,
                JSON.stringify(
                    e.fold(
                        (error) => ({ error, ok: undefined }),
                        (ok) => ({ ok }),
                    ),
                ),
                opts,
            );
            return;
        }
        super(req, JSON.stringify(Math.floor(opts.status / 100) > 4 ? { error: e } : { ok: e }), opts);
    }
}