UNPKG

one

Version:

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

70 lines 3.29 kB
var __defProp = Object.defineProperty; var __getOwnPropDesc = Object.getOwnPropertyDescriptor; var __getOwnPropNames = Object.getOwnPropertyNames; var __hasOwnProp = Object.prototype.hasOwnProperty; var __export = (target, all) => { for (var name in all) __defProp(target, name, { get: all[name], enumerable: !0 }); }, __copyProps = (to, from, except, desc) => { if (from && typeof from == "object" || typeof from == "function") for (let key of __getOwnPropNames(from)) !__hasOwnProp.call(to, key) && key !== except && __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable }); return to; }; var __toCommonJS = mod => __copyProps(__defProp({}, "__esModule", { value: !0 }), mod); var imageDataPlugin_exports = {}; __export(imageDataPlugin_exports, { imageDataPlugin: () => imageDataPlugin }); module.exports = __toCommonJS(imageDataPlugin_exports); var import_node_fs = require("node:fs"), import_node_path = require("node:path"), import_getImageData = require("../../image/getImageData.cjs"); const IMAGEDATA_SUFFIX = "?imagedata", VIRTUAL_PREFIX = "\0imagedata:"; function imageDataPlugin() { let publicDir, root; function getSrcPath(filePath) { return filePath.startsWith(publicDir) ? "/" + (0, import_node_path.relative)(publicDir, filePath) : "/" + (0, import_node_path.relative)(root, filePath); } function isPathWithinBounds(filePath, allowedDir) { const resolved = (0, import_node_path.resolve)(filePath), allowed = (0, import_node_path.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, allowedDir; return cleanId.startsWith("/") ? (filePath = (0, import_node_path.resolve)(publicDir, cleanId.slice(1)), allowedDir = publicDir) : importer ? (filePath = (0, import_node_path.resolve)((0, import_node_path.dirname)(importer.replace(VIRTUAL_PREFIX, "")), cleanId), allowedDir = root) : (filePath = (0, import_node_path.resolve)(root, cleanId), allowedDir = root), isPathWithinBounds(filePath, allowedDir) ? (0, import_node_fs.existsSync)(filePath) ? VIRTUAL_PREFIX + filePath : (console.warn(`[one] ?imagedata: File not found: ${filePath}`), null) : (console.warn(`[one] ?imagedata: Path traversal blocked: ${cleanId}`), null); }, async load(id) { if (!id.startsWith(VIRTUAL_PREFIX)) return null; const filePath = id.slice(VIRTUAL_PREFIX.length), src = getSrcPath(filePath); this.addWatchFile(filePath); const meta = await (0, import_getImageData.processImageMeta)(filePath); return meta ? createImageDataExport(src, meta.width, meta.height, meta.blurDataURL) : createImageDataExport(src); } }; }