UNPKG

one

Version:

One is a new React Framework that makes Vite serve both native and web.

72 lines (71 loc) 2.43 kB
import { existsSync } from "node:fs"; import { dirname, relative, resolve } from "node:path"; import { processImageMeta } from "../../image/getImageData.mjs"; const IMAGEDATA_SUFFIX = "?imagedata"; const VIRTUAL_PREFIX = "\0imagedata:"; function imageDataPlugin() { let publicDir; let root; function getSrcPath(filePath) { return filePath.startsWith(publicDir) ? "/" + relative(publicDir, filePath) : "/" + relative(root, filePath); } function isPathWithinBounds(filePath, allowedDir) { const resolved = resolve(filePath); const allowed = resolve(allowedDir); return resolved.startsWith(allowed + "/") || resolved === allowed; } function createImageDataExport(src, width = 0, height = 0, blurDataURL = "") { return `export default ${JSON.stringify({ src, width, height, blurDataURL })}`; } return { name: "one:imagedata", enforce: "pre", configResolved(resolvedConfig) { publicDir = resolvedConfig.publicDir; root = resolvedConfig.root; }, async resolveId(id, importer) { if (!id.endsWith(IMAGEDATA_SUFFIX)) return null; const cleanId = id.slice(0, -IMAGEDATA_SUFFIX.length); let filePath; let allowedDir; if (cleanId.startsWith("/")) { filePath = resolve(publicDir, cleanId.slice(1)); allowedDir = publicDir; } else if (importer) { filePath = resolve(dirname(importer.replace(VIRTUAL_PREFIX, "")), cleanId); allowedDir = root; } else { filePath = resolve(root, cleanId); allowedDir = root; } if (!isPathWithinBounds(filePath, allowedDir)) { console.warn(`[one] ?imagedata: Path traversal blocked: ${cleanId}`); return null; } if (!existsSync(filePath)) { console.warn(`[one] ?imagedata: File not found: ${filePath}`); return null; } return VIRTUAL_PREFIX + filePath; }, async load(id) { if (!id.startsWith(VIRTUAL_PREFIX)) return null; const filePath = id.slice(VIRTUAL_PREFIX.length); const src = getSrcPath(filePath); this.addWatchFile(filePath); const meta = await processImageMeta(filePath); if (!meta) { return createImageDataExport(src); } return createImageDataExport(src, meta.width, meta.height, meta.blurDataURL); } }; } export { imageDataPlugin }; //# sourceMappingURL=imageDataPlugin.mjs.map