UNPKG

one

Version:

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

78 lines (76 loc) 3.5 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: !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_fs = require("fs"), import_path = require("path"), import_getImageData = require("../../image/getImageData.native.js"), IMAGEDATA_SUFFIX = "?imagedata", VIRTUAL_PREFIX = "\0imagedata:"; function imageDataPlugin() { var publicDir, 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), 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), filePath, allowedDir; return cleanId.startsWith("/") ? (filePath = (0, import_path.resolve)(publicDir, cleanId.slice(1)), allowedDir = publicDir) : importer ? (filePath = (0, import_path.resolve)((0, import_path.dirname)(importer.replace(VIRTUAL_PREFIX, "")), cleanId), allowedDir = root) : (filePath = (0, import_path.resolve)(root, cleanId), allowedDir = root), isPathWithinBounds(filePath, allowedDir) ? (0, import_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; var filePath = id.slice(VIRTUAL_PREFIX.length), src = getSrcPath(filePath); this.addWatchFile(filePath); var meta = await (0, import_getImageData.processImageMeta)(filePath); return meta ? createImageDataExport(src, meta.width, meta.height, meta.blurDataURL) : createImageDataExport(src); } }; } //# sourceMappingURL=imageDataPlugin.native.js.map