Repository navigation
Expand file tree
/
Copy pathweb.ts
More file actions
60 lines (50 loc) · 1.98 KB
/
Copy pathweb.ts
File metadata and controls
60 lines (50 loc) · 1.98 KB
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
// web.ts — the tiny HTTP vocabulary the surface modules share.
export interface HttpRequest {
method: string;
/** Decoded path, no query string. */
path: string;
query: URLSearchParams;
headers: Readonly<Record<string, string | string[] | undefined>>;
body: Buffer;
/** The request's Host header, for building absolute Link URLs. */
host: string;
}
export interface HttpResponse {
status: number;
contentType: string;
body: Buffer | string;
headers?: Record<string, string>;
}
export const JSON_TYPE = 'application/json; charset=utf-8';
export function json(status: number, value: unknown, headers: Record<string, string> = {}): HttpResponse {
return { status, contentType: JSON_TYPE, body: `${JSON.stringify(value)}\n`, headers };
}
export function jsonError(status: number, message: string): HttpResponse {
return json(status, { message });
}
/** Parse a JSON request body; { ok: false } when it is not valid JSON. */
export function parseJsonBody(body: Buffer): { ok: true; value: Record<string, unknown> } | { ok: false } {
if (body.length === 0) return { ok: true, value: {} };
try {
const value = JSON.parse(body.toString('utf8')) as unknown;
if (value === null || typeof value !== 'object' || Array.isArray(value)) return { ok: true, value: {} };
return { ok: true, value: value as Record<string, unknown> };
} catch {
return { ok: false };
}
}
export function str(value: unknown): string {
return typeof value === 'string' ? value : '';
}
export function optStr(value: unknown): string | undefined {
return typeof value === 'string' ? value : undefined;
}
export function bool(value: unknown, fallback: boolean): boolean {
return typeof value === 'boolean' ? value : fallback;
}
export function intParam(query: URLSearchParams, name: string, fallback: number): number {
const raw = query.get(name);
if (raw === null) return fallback;
const parsed = Number.parseInt(raw, 10);
return Number.isFinite(parsed) && parsed > 0 ? parsed : fallback;
}