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";
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,
);
}