A music player that connects to your cloud/distributed storage.
at v4 85 lines 2.2 kB view raw
1// import { fragments, serializeFragments } from "@fcrozatier/htmlcrunch"; 2 3import * as TID from "@atcute/tid"; 4 5import { loadURI } from "../loader.js"; 6import * as CID from "../cid.js"; 7 8/** 9 * @import {Facet} from "~/definitions/types.d.ts" 10 */ 11 12/** 13 * @param {{ description?: string; kind: string | undefined; name: string; uri: string }} _args 14 * @param {{ fetchHTML: boolean }} options 15 */ 16export async function facetFromURI( 17 { description, kind, name, uri }, 18 { fetchHTML }, 19) { 20 const html = fetchHTML ? await loadURI(uri) : undefined; 21 const cid = html 22 ? await CID.create(0x55, new TextEncoder().encode(html)) 23 : undefined; 24 const timestamp = new Date().toISOString(); 25 26 /** @type {Facet} */ 27 const facet = { 28 $type: "sh.diffuse.output.facet", 29 createdAt: timestamp, 30 id: TID.now(), 31 cid, 32 description, 33 html, 34 name, 35 kind: kind === "interactive" || kind === "prelude" ? kind : undefined, 36 updatedAt: timestamp, 37 uri, 38 }; 39 40 return facet; 41} 42 43// /** 44// * @param {string} html 45// */ 46// export async function inlineModuleScripts(html) { 47// const docPromises = fragments.parseOrThrow(html).map(async (frag) => { 48// if ("tagName" in frag && frag.tagName === "script") { 49// const isModScript = frag.attributes.find((a) => 50// a[0] === "type" 51// )?.[1] === "module"; 52// if (!isModScript) return frag; 53// 54// const src = frag.attributes.find((a) => a[0] === "src")?.[1]; 55// if (!src) return frag; 56// 57// const scriptContents = await fetch(src).then((r) => r.text()).catch(() => 58// null 59// ); 60// 61// if (!scriptContents) return frag; 62// 63// /** 64// * @type {import("@fcrozatier/htmlcrunch").MTextNode} 65// */ 66// const child = { 67// kind: "TEXT", 68// text: "\n" + scriptContents.split("\n").map((l) => 69// ` ${l}` 70// ).join("\n") + "\n", 71// }; 72// 73// return { 74// ...frag, 75// attributes: frag.attributes.filter((a) => a[0] !== "src"), 76// children: [child], 77// }; 78// } 79// 80// return frag; 81// }); 82// 83// const doc = await Promise.all(docPromises); 84// return serializeFragments(doc); 85// }