Last active
September 1, 2024 18:25
-
-
Save wmakeev/865424600e3f54e528664b4317c7c188 to your computer and use it in GitHub Desktop.
[base64 data url] #base64 #url #data
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { fetch } from "undici"; | |
/** | |
* @param {string} url | |
* @param {string} [origin] | |
* @returns {Promise<string>} | |
*/ | |
export async function getResourceDataUrl(url, origin) { | |
const fetchUrl = new URL(url, origin); | |
const resp = await fetch(fetchUrl); | |
const contentType = resp.headers.get("content-type"); | |
const arrayBuffer = await resp.arrayBuffer(); | |
const base64Image = Buffer.from(arrayBuffer).toString("base64"); | |
const dataUri = `data:${contentType};base64,${base64Image}`; | |
return dataUri; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment