MCPcopy Create free account
hub / github.com/backnotprop/plannotator / dataUrlFor

Function dataUrlFor

packages/shared/html-assets-node.ts:18–50  ·  view source on GitHub ↗
(assetPath: string)

Source from the content-addressed store, hash-verified

16 const activeCss = new Set<string>();
17
18 const dataUrlFor = (assetPath: string): string | null => {
19 try {
20 const contentType = htmlAssetContentType(assetPath);
21 if (!contentType) return null;
22
23 const resolved = resolvePath(root, assetPath);
24 if (!isWithinDirectory(resolved, root)) return null;
25 if (!existsSync(resolved)) return null;
26
27 const stat = statSync(resolved);
28 if (!stat.isFile() || stat.size > MAX_HTML_ASSET_BYTES) return null;
29
30 let bytes = readFileSync(resolved);
31 if (contentType.startsWith("text/css") && !activeCss.has(assetPath)) {
32 activeCss.add(assetPath);
33 try {
34 const cssBase = pathPosix.dirname(assetPath);
35 const rewrittenCss = rewriteCssAssetReferences(
36 bytes.toString("utf-8"),
37 dataUrlFor,
38 cssBase === "." ? "" : cssBase,
39 );
40 bytes = Buffer.from(rewrittenCss, "utf-8");
41 } finally {
42 activeCss.delete(assetPath);
43 }
44 }
45
46 return `data:${contentType.replace(/;\s*/g, ";")};base64,${Buffer.from(bytes).toString("base64")}`;
47 } catch {
48 return null;
49 }
50 };
51
52 return rewriteHtmlAssetReferences(html, dataUrlFor);
53 } catch {

Callers

nothing calls this directly

Calls 8

htmlAssetContentTypeFunction · 0.90
isWithinDirectoryFunction · 0.85
hasMethod · 0.80
addMethod · 0.80
toStringMethod · 0.80
deleteMethod · 0.80
replaceMethod · 0.80

Tested by

no test coverage detected