1
export interface HttpRequest {
2
method: "GET" | "POST" | "PUT" | "DELETE";
5
headers: Record<string, string>;
6
body?: Uint8Array | ReadableStream<Uint8Array> | null;
8
export interface HttpResponse {
10
headers: Record<string, string>;
15
export function headersToASGI(
16
headers: HttpRequest["headers"]
17
): [string, string][] {
18
const result: [string, string][] = [];
19
for (const [key, value] of Object.entries(headers)) {
20
result.push([key, value]);
25
export function uint8ArrayToString(buf: Uint8Array): string {
27
for (let i = 0; i < buf.length; i++) {
28
result += String.fromCharCode(buf[i]);
33
export function asgiHeadersToRecord(headers: any): Record<string, string> {
34
headers = headers.map(([key, val]: [Uint8Array, Uint8Array]) => {
35
return [uint8ArrayToString(key), uint8ArrayToString(val)];
37
return Object.fromEntries(headers);
40
export function getHeaderValue(
41
headers: HttpRequest["headers"],
43
): string | undefined {
45
const unifiedKey = key.toLowerCase();
46
for (const [k, v] of Object.entries(headers)) {
47
if (k.toLowerCase() === unifiedKey) {
53
export function logHttpReqRes(
55
response: HttpResponse
57
if (Math.floor(response.status / 100) !== 2) {
59
let bodyJson: unknown;
61
bodyText = new TextDecoder().decode(response.body);
63
bodyText = "(failed to decode body)";
66
bodyJson = JSON.parse(bodyText);
68
bodyJson = "(failed to parse body as JSON)";
70
console.error("Wasm HTTP error", {