"use strict";var __defProp=Object.defineProperty,__getOwnPropDesc=Object.getOwnPropertyDescriptor,__getOwnPropNames=Object.getOwnPropertyNames,__hasOwnProp=Object.prototype.hasOwnProperty,__export=(e,r)=>{for(var t in r)__defProp(e,t,{get:r[t],enumerable:!0})},__copyProps=(r,t,o,a)=>{if(t&&"object"==typeof t||"function"==typeof t)for(let e of __getOwnPropNames(t))__hasOwnProp.call(r,e)||e===o||__defProp(r,e,{get:()=>t[e],enumerable:!(a=__getOwnPropDesc(t,e))||a.enumerable});return r},__toCommonJS=e=>__copyProps(__defProp({},"__esModule",{value:!0}),e),readonly_api_exports={},CPL_API_WRITE_METHODS=(__export(readonly_api_exports,{before:()=>before,name:()=>name,platforms:()=>platforms,shouldUseReaderAuthorizationForCplApi:()=>shouldUseReaderAuthorizationForCplApi,startup:()=>startup,synchronous:()=>synchronous}),module.exports=__toCommonJS(readonly_api_exports),new Set(["POST","PUT","DELETE"])),CPL_WRITE_ROUTES=[/^\/cpl\/download\//,/^\/cpl\/rate\//,/^\/cpl\/comments\//,/^\/cpl\/compatibility\//,/^\/cpl\/auth\/logout/],homepageEtag=`"${Date.now().toString(36)}"`,normalizeHeaderValue=e=>"string"==typeof e&&e?e:Array.isArray(e)&&"string"==typeof e[0]&&e[0]?e[0]:null,stripWeakEtagPrefix=e=>e.startsWith("W/")?e.slice(2):e,rewriteCorsHeaders=(e,r)=>{var t=Object.keys(e).find(e=>"access-control-allow-origin"===e.toLowerCase())??"Access-Control-Allow-Origin";"*"===e[t]&&(e[t]=r,e["Access-Control-Allow-Credentials"]="true",e.Vary=e.Vary?String(e.Vary)+", Origin":"Origin")},shouldUseReaderAuthorizationForCplApi=(e,r)=>{var t=e.method??"";if(!CPL_API_WRITE_METHODS.has(t))return!1;let o=e.url??"/";t=o.indexOf("?"),t=(o=-1!==t?o.slice(0,t):o).indexOf("#"),-1!==t&&(o=o.slice(0,t)),t=r.get?.("path-prefix")??"";const a=t&&o.startsWith(t)?o.slice(t.length)||"/":o;return CPL_WRITE_ROUTES.some(e=>e.test(a))},name="cpl-server-readonly-api",platforms=["node"],before=["commands"],synchronous=!0,startup=()=>{if($tw.node){var e=require("$:/core/modules/server/server.js").Server?.prototype;if(e&&!e.cplApiReadonlyPatchApplied){const d=e.requestHandler;e.requestHandler=function(e,r,t){var o=e,a=(o.url??"/").split("?")[0].split("#")[0];if(("/"===a||""===a)&&"GET"===o.method){var a=o.headers,a=normalizeHeaderValue(a["if-none-match"]);if((a?stripWeakEtagPrefix(a):null)===homepageEtag)return(a=r).writeHead(304,{ETag:homepageEtag,"Cache-Control":"public, max-age=300"}),void a.end();const i=r,s=i.writeHead.bind(i),l=i.write.bind(i);let e=!1;const p=()=>{if(!e){e=!0;try{i.setHeader("ETag",homepageEtag)}catch{}}};i.writeHead=function(...e){return p(),s(...e)},i.write=function(...e){return p(),l(...e)}}const n=normalizeHeaderValue(o.headers?.origin);if(n){a=r;const c=a.writeHead.bind(a);a.writeHead=function(...e){var r="object"==typeof e[1]&&null!==e[1]?e[1]:"object"==typeof e[2]&&null!==e[2]?e[2]:null;return r&&rewriteCorsHeaders(r,n),c(...e)}}return shouldUseReaderAuthorizationForCplApi(o,this)?d.call(this,e,r,{...t,authorizationType:"readers"}):d.call(this,e,r,t)},e.cplApiReadonlyPatchApplied=!0}}};