"use strict";var __create=Object.create,__defProp=Object.defineProperty,__getOwnPropDesc=Object.getOwnPropertyDescriptor,__getOwnPropNames=Object.getOwnPropertyNames,__getProtoOf=Object.getPrototypeOf,__hasOwnProp=Object.prototype.hasOwnProperty,__export=(e,t)=>{for(var o in t)__defProp(e,o,{get:t[o],enumerable:!0})},__copyProps=(t,o,i,r)=>{if(o&&"object"==typeof o||"function"==typeof o)for(let e of __getOwnPropNames(o))__hasOwnProp.call(t,e)||e===i||__defProp(t,e,{get:()=>o[e],enumerable:!(r=__getOwnPropDesc(o,e))||r.enumerable});return t},__toESM=(e,t,o)=>(o=null!=e?__create(__getProtoOf(e)):{},__copyProps(!t&&e&&e.__esModule?o:__defProp(o,"default",{value:e,enumerable:!0}),e)),__toCommonJS=e=>__copyProps(__defProp({},"__esModule",{value:!0}),e),repo_files_exports={},fs2=(__export(repo_files_exports,{handler:()=>handler,method:()=>method,path:()=>path2}),module.exports=__toCommonJS(repo_files_exports),__toESM(require("fs"))),pathModule=__toESM(require("path")),CORS_HEADERS={"Access-Control-Allow-Origin":"*"},jsonHeaders=(e={})=>({"Content-Type":"application/json",...CORS_HEADERS,...e}),sendJson=(e,t,o,i={})=>{e.sendResponse(t,jsonHeaders(i),JSON.stringify(o))},sendError=(e,t,o)=>{sendJson(e,t,{success:!1,error:o})},decodeRouteParam=e=>decodeURIComponent(e??""),fs=__toESM(require("fs")),path=__toESM(require("path")),PROJECT_ROOT="undefined"!=typeof __dirname?path.resolve(__dirname,"../../.."):path.resolve(process.cwd()),bootWikiPath=globalThis.$tw?.boot?.wikiPath,wikiRootCandidates=[bootWikiPath?path.resolve(bootWikiPath):null,process.env.WIKI_PATH?path.resolve(process.env.WIKI_PATH,process.env.WIKI_SUBPATH??""):null,path.join(PROJECT_ROOT,"wiki"),PROJECT_ROOT].filter(e=>null!==e),expandedWikiRootCandidates=Array.from(new Set(wikiRootCandidates.flatMap(e=>{var t=[e];let o=path.dirname(e);for(;o!==t[t.length-1];){t.push(o);var i=path.dirname(o);if(i===o)break;o=i}return t}))),DEPLOYED_WIKI_ROOT=expandedWikiRootCandidates.find(e=>fs.existsSync(path.join(e,"tiddlywiki.info"))&&fs.existsSync(path.join(e,"tiddlers")))??wikiRootCandidates[0]??null,isTestMode=()=>"true"===process.env.CPL_TEST_MODE,getWikiRoot=()=>isTestMode()?path.join(PROJECT_ROOT,"tmp","test-wiki"):DEPLOYED_WIKI_ROOT??path.join(PROJECT_ROOT,"wiki"),paths={projectRoot:PROJECT_ROOT,data:process.env.CPL_DATA_DIR?path.resolve(process.env.CPL_DATA_DIR):path.join(DEPLOYED_WIKI_ROOT??PROJECT_ROOT,".cpl-data"),wiki:DEPLOYED_WIKI_ROOT??path.join(PROJECT_ROOT,"wiki"),testWiki:path.join(PROJECT_ROOT,"tmp","test-wiki"),get wikiRoot(){return getWikiRoot()},get tiddlers(){return path.join(getWikiRoot(),"tiddlers")},get comments(){return{root:path.join(getWikiRoot(),"tiddlers","comments"),pending:path.join(getWikiRoot(),"tiddlers","comments","pending"),approved:path.join(getWikiRoot(),"tiddlers","comments","approved")}},get ratings(){return path.join(getWikiRoot(),"tiddlers","ratings")},get compatibility(){return path.join(getWikiRoot(),"tiddlers","compatibility")},get downloadStats(){return path.join(getWikiRoot(),"tiddlers","download-stats")},get files(){return path.join(getWikiRoot(),"files")},get pluginFetched(){return path.join(getWikiRoot(),"files","plugin-fetched")},get pluginOffline(){return path.join(getWikiRoot(),"files","plugin-offline")},get pluginFetchedHistory(){return path.join(getWikiRoot(),"files","plugin-fetched-history")},cache:{root:path.join(PROJECT_ROOT,"cache"),plugins:path.join(PROJECT_ROOT,"cache","plugins"),runtimePlugins:path.join(PROJECT_ROOT,"cache","runtime-plugins"),runtimePluginDirs:path.join(PROJECT_ROOT,"cache","runtime-plugin-dirs")},tmp:path.join(PROJECT_ROOT,"tmp"),repoCache:path.join(PROJECT_ROOT,"repo-cache"),"public":path.join(PROJECT_ROOT,"public"),dist:path.join(PROJECT_ROOT,"dist")},REPO_DIR=paths.cache.plugins,MIME_TYPES={".json":"application/json",".js":"application/javascript",".css":"text/css",".html":"text/html",".svg":"image/svg+xml",".png":"image/png",".jpg":"image/jpeg",".gif":"image/gif"},getMimeType=e=>MIME_TYPES[pathModule.extname(e).toLowerCase()]??"application/octet-stream",resolveSafePath=e=>{var t=pathModule.normalize(e).replace(/^(\.\.(\/|\\|$))+/,""),t=pathModule.resolve(REPO_DIR,t);return t.startsWith(REPO_DIR+pathModule.sep)||t===REPO_DIR?t:null},method="GET",path2=/^\/repo\/(.+)$/,handler=(e,t,o)=>{try{var i,r,n,a=decodeRouteParam(o.params[0]),s=resolveSafePath(a);s&&fs2.existsSync(s)?(i=fs2.statSync(s)).isFile()?(r=fs2.readFileSync(s),n=getMimeType(s),o.sendResponse(200,{"Content-Type":n,...CORS_HEADERS,"Cache-Control":"public, max-age=300","Content-Length":String(i.size)},r instanceof Buffer?r.toString("utf-8"):r)):sendError(o,404,"Not a file"):sendError(o,404,"File not found")}catch(p){sendError(o,500,"Internal server error"),console.error("[CPL-Server] Error serving repo file:",p)}};