import n from"path";import{readFile as c,writeFileSync as l}from"fs";import{getSharedDataUrl as f}from"../../utils/url/get-shared-data-url.js";import{logger as m}from"../tools/notifiers/logger.js";import{ensureDir as p}from"./fs.js";const s={},v=async(e,t,r)=>{const a=d(e,r);await s[a],l(p(a),t,{encoding:"utf-8",flush:!0})},P=async(e,t)=>{try{const r=d(e,t),a=new Promise(o=>c(r,"utf-8",(i,h)=>{o(i?void 0:JSON.parse(h))}));return s[r]=a,a.finally(()=>{delete s[r]})}catch(r){m.warnProd(`Failed to read shared data ${e} with error: ${r instanceof Error?r.message:"Unknown error"}`);return}},d=(e,t)=>{const r=n.join(t,f(e)),a=n.resolve(t,"page-data/shared"),o=n.resolve(t,r);if(n.relative(a,o).startsWith(".."))throw new Error(`Invalid shared data id: ${e}`);return o};export{P as readSharedData,v as writeSharedData};
