UNPKG

image2uri

Version:
28 lines 1.08 kB
import fs from 'fs'; import path from 'path'; import fetch from 'node-fetch'; export const validUrl = (url) => /http(s)?:\/\/(\w+:?\w*@)?(\S+)(:\d+)?((?<=\.)\w+)+(\/([\w#!:.?+=&%@!\-/])*)?/gi.test(url); export const extTypeMap = { '.png': 'image/png', '.apng': 'image/apng', '.gif': 'image/gif', '.jpg': 'image/jpeg', '.jpeg': 'image/jpeg', '.bm': 'image/bmp', '.bmp': 'image/bmp', '.webp': 'image/webp', '.ico': 'image/x-icon', '.svg': 'image/svg+xml' }; export default function image2uri(file, options = {}) { const ext = (options.ext || path.extname(file)); const contentType = extTypeMap[ext]; if (validUrl(file)) { return fetch(file).then((response) => response.buffer()).then((buffer) => { return contentType ? `data:${contentType};base64,${buffer.toString('base64')}` : buffer.toString('base64'); }); } const image = fs.readFileSync(file); return contentType ? `data:${contentType};base64,${image.toString('base64')}` : image.toString('base64'); } //# sourceMappingURL=index.js.map