UNPKG

one

Version:

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

103 lines (101 loc) 3.74 kB
"use strict"; 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_fs = require("fs"); var import_path = require("path"); var import_getImageData = require("../../image/getImageData.native.js"); var IMAGEDATA_SUFFIX = "?imagedata"; var VIRTUAL_PREFIX = "\0imagedata:"; function imageDataPlugin() { var publicDir; var root; function getSrcPath(filePath) { return filePath.startsWith(publicDir) ? "/" + (0, import_path.relative)(publicDir, filePath) : "/" + (0, import_path.relative)(root, filePath); } function isPathWithinBounds(filePath, allowedDir) { var resolved = (0, import_path.resolve)(filePath); var allowed = (0, import_path.resolve)(allowedDir); return resolved.startsWith(allowed + "/") || resolved === allowed; } function createImageDataExport(src) { var width = arguments.length > 1 && arguments[1] !== void 0 ? arguments[1] : 0, height = arguments.length > 2 && arguments[2] !== void 0 ? arguments[2] : 0, blurDataURL = arguments.length > 3 && arguments[3] !== void 0 ? arguments[3] : ""; 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; var cleanId = id.slice(0, -IMAGEDATA_SUFFIX.length); var filePath; var allowedDir; if (cleanId.startsWith("/")) { filePath = (0, import_path.resolve)(publicDir, cleanId.slice(1)); allowedDir = publicDir; } else if (importer) { filePath = (0, import_path.resolve)((0, import_path.dirname)(importer.replace(VIRTUAL_PREFIX, "")), cleanId); allowedDir = root; } else { filePath = (0, import_path.resolve)(root, cleanId); allowedDir = root; } if (!isPathWithinBounds(filePath, allowedDir)) { console.warn(`[one] ?imagedata: Path traversal blocked: ${cleanId}`); return null; } if (!(0, import_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; var filePath = id.slice(VIRTUAL_PREFIX.length); var src = getSrcPath(filePath); this.addWatchFile(filePath); var meta = await (0, import_getImageData.processImageMeta)(filePath); if (!meta) { return createImageDataExport(src); } return createImageDataExport(src, meta.width, meta.height, meta.blurDataURL); } }; } //# sourceMappingURL=imageDataPlugin.native.js.map