{"version":3,"file":"prerender.mjs","names":[],"sources":["../../../../../../vite/src/prerender.ts"],"sourcesContent":["// Escapes a value for safe embedding inside a single-quoted PHP string\r\n// literal. PHP single-quoted strings only treat `\\` and `'` specially (no\r\n// variable interpolation), so escaping those two is sufficient to prevent\r\n// breaking out of the literal.\r\nfunction toPhpSingleQuotedString(value: unknown): string {\r\n  const stringValue = String(value ?? \"\");\r\n\r\n  return `'${stringValue.replace(/\\\\/g, \"\\\\\\\\\").replace(/'/g, \"\\\\'\")}'`;\r\n}\r\n\r\n// Coerces a value to a finite number literal, falling back to `5000` (the\r\n// documented default) for anything else so a non-numeric `delay` can't be\r\n// used to break out of the generated PHP array literal.\r\nfunction toPhpNumberLiteral(value: unknown, fallback: number): string {\r\n  const numberValue = typeof value === \"number\" ? value : Number(value);\r\n\r\n  return String(Number.isFinite(numberValue) ? numberValue : fallback);\r\n}\r\n\r\n// Coerces a value to a PHP boolean literal (`true`/`false`) so a\r\n// non-boolean `cache` can't be used to break out of the generated PHP\r\n// array literal.\r\nfunction toPhpBooleanLiteral(value: unknown): string {\r\n  return value === true ? \"true\" : \"false\";\r\n}\r\n\r\nexport default function generatePreRenderContent({\r\n  prerenderUrl,\r\n  cache,\r\n  delay,\r\n}) {\r\n  const phpPrerenderUrl = toPhpSingleQuotedString(prerenderUrl);\r\n  const phpDelay = toPhpNumberLiteral(delay, 5000);\r\n  const phpCache = toPhpBooleanLiteral(cache);\r\n\r\n  return `<?php\r\n\r\n  function get_content($URL)\r\n  {\r\n      $hashedUrl = sha1($URL);\r\n  \r\n      $cachedPagesDirectory = __DIR__ . '/cache';\r\n      $cachedFile = $cachedPagesDirectory . '/' . $hashedUrl . '.html';\r\n  \r\n      if (file_exists($cachedFile)) {\r\n          $file = file_get_contents($cachedFile);\r\n          if ($file) {\r\n              return $file;\r\n          }\r\n      }\r\n  \r\n      $ch = curl_init();\r\n      curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);\r\n      curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, true);\r\n      curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);\r\n  \r\n      curl_setopt($ch, CURLOPT_URL, $URL);\r\n      $data = curl_exec($ch);\r\n  \r\n      curl_close($ch);\r\n  \r\n      if (!file_exists($cachedPagesDirectory)) {\r\n          mkdir($cachedPagesDirectory, 0777, true);\r\n      }\r\n  \r\n      file_put_contents($cachedFile, $data);\r\n  \r\n      return $data;\r\n  }\r\n\r\n$prerenderUrl = ${phpPrerenderUrl};\r\n\r\n$url = $_SERVER['SCRIPT_URI'] ?? $_SERVER['REQUEST_SCHEME'] . '://' . $_SERVER['SERVER_NAME'] . $_SERVER['REQUEST_URI'];\r\n\r\nif (! empty($_GET)) {\r\n    $url .= '?' . http_build_query($_GET);\r\n}\r\n\r\n// for arabic letters and utf8 in general\r\n$url = urlencode($url);\r\n\r\n$userAgent = $_SERVER['HTTP_USER_AGENT'];\r\n\r\n$params = [\r\n    'url' => $url,\r\n    'delay' => ${phpDelay},\r\n    'cache' => ${phpCache},\r\n    '__agent' => $userAgent,\r\n];\r\n\r\n$url = \"$prerenderUrl?\" . http_build_query($params);\r\n\r\n$content = get_content($url);\r\n\r\necho $content;`;\r\n}\r\n"],"mappings":";AAIA,SAAS,wBAAwB,OAAwB;CAGvD,OAAO,IAFa,OAAO,SAAS,EAEf,CAAC,CAAC,QAAQ,OAAO,MAAM,CAAC,CAAC,QAAQ,MAAM,KAAK,EAAE;AACrE;AAKA,SAAS,mBAAmB,OAAgB,UAA0B;CACpE,MAAM,cAAc,OAAO,UAAU,WAAW,QAAQ,OAAO,KAAK;CAEpE,OAAO,OAAO,OAAO,SAAS,WAAW,IAAI,cAAc,QAAQ;AACrE;AAKA,SAAS,oBAAoB,OAAwB;CACnD,OAAO,UAAU,OAAO,SAAS;AACnC;AAEA,SAAwB,yBAAyB,EAC/C,cACA,OACA,SACC;CAKD,OAAO;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;kBAJiB,wBAAwB,YAuClB,EAAE;;;;;;;;;;;;;;;iBAtCf,mBAAmB,OAAO,GAqDrB,EAAE;iBApDP,oBAAoB,KAqDf,EAAE;;;;;;;;;AAS1B"}