1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
import { InjectCodeOnValTownStream } from "https://esm.town/v/andreterron/InjectCodeOnValTownStream?v=4";
import { ValRef } from "https://esm.town/v/andreterron/ValRef?v=1";
import { parse } from "npm:content-type";
/**
* @param res Response
* @param val Define which val should open. Defaults to the root reference.
*/
export function modifyResponse(
res: Response,
{ val, style }: { val?: ValRef; style?: string } = {},
) {
if (!res.headers.has("Content-Type") || parse(res.headers.get("Content-Type")).type !== "text/html") {
return res;
}
return new Response(
res.body ? res.body
.pipeThrough(new TextDecoderStream())
.pipeThrough(new InjectCodeOnValTownStream({ val, style }))
.pipeThrough(new TextEncoderStream()) : res.body,
res,
);
}