UNPKG

one

Version:

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

96 lines 3.54 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: true }); }; var __copyProps = (to, from, except, desc) => { if (from && typeof from === "object" || typeof from === "function") { for (let key of __getOwnPropNames(from)) if (!__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: true }), mod); var imageDataPlugin_exports = {}; __export(imageDataPlugin_exports, { imageDataPlugin: () => imageDataPlugin }); module.exports = __toCommonJS(imageDataPlugin_exports); var import_node_fs = require("node:fs"); var import_node_path = require("node:path"); var import_getImageData = require("../../image/getImageData.cjs"); const IMAGEDATA_SUFFIX = "?imagedata"; const VIRTUAL_PREFIX = "\0imagedata:"; function imageDataPlugin() { let publicDir; let 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); const 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; let allowedDir; if (cleanId.startsWith("/")) { filePath = (0, import_node_path.resolve)(publicDir, cleanId.slice(1)); allowedDir = publicDir; } else if (importer) { filePath = (0, import_node_path.resolve)((0, import_node_path.dirname)(importer.replace(VIRTUAL_PREFIX, "")), cleanId); allowedDir = root; } else { filePath = (0, import_node_path.resolve)(root, cleanId); allowedDir = root; } if (!isPathWithinBounds(filePath, allowedDir)) { console.warn(`[one] ?imagedata: Path traversal blocked: ${cleanId}`); return null; } if (!(0, import_node_fs.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 (0, import_getImageData.processImageMeta)(filePath); if (!meta) { return createImageDataExport(src); } return createImageDataExport(src, meta.width, meta.height, meta.blurDataURL); } }; }