UNPKG

azion

Version:

Azion Packages for Edge Computing.

2 lines (1 loc) 2.14 kB
var u=(o,r)=>{let t=o.trim().split(";").reduce((e,n)=>{let[i,m]=n.split("="),a=i;if(a=i.trim(),a){let s=decodeURIComponent(m).trim();s.startsWith('"')&&s.endsWith('"')&&(s=s.slice(1,-1)),e[a]=s}return e},{});return r?t[r.trim()]:t};var f=(o,r,t)=>{if(!(o instanceof Request))return;let e=o.headers.get("Cookie");if(!e)return typeof r=="string"?void 0:{};let n=t?`__${t.charAt(0).toUpperCase()+t.slice(1)}-${r}`:r;return u(e,n)};var c=(o,r,t,e)=>{if(!(o instanceof Response))throw new Error("response is not an instance of Response");let n=g(r,e);(e?.prefix==="host"||e?.prefix==="secure")&&(e={...e,secure:!0});let i=`${n}=${t}`;if(e?.domain&&(i+=`; Domain=${e.domain}`),e?.expires){if(e?.expires<new Date)throw new Error("expires option must be a future date");if(e.expires.getTime()-Date.now()>3456e7)throw new Error("expires option must be less than or equal to (400 days) 34560000");i+=`; Expires=${e.expires.toUTCString()}`}if(e?.httpOnly&&(i+="; HttpOnly"),e?.maxAge){if(e?.maxAge<0)throw new Error("maxAge must be a positive number");if(e?.maxAge>3456e4)throw new Error("maxAge must be less than or equal to (400 days) 34560000");i+=`; Max-Age=${e.maxAge}`}if(e?.path&&(i+=`; Path=${e.path}`),e?.sameSite){if(e.sameSite==="None"&&!e.secure)throw new Error("secure option must be true when using SameSite=None");if(e.sameSite==="None"&&e.secure&&e.partitioned)throw new Error("Partitioned cookies cannot be used with SameSite=None and Secure");i+=`; SameSite=${e.sameSite}`}return e?.secure&&(i+="; Secure"),e?.partitioned&&(i+="; Partitioned"),o.headers.append("Set-Cookie",i),o},g=(o,r)=>{let t=o;if(r?.prefix==="secure"){if(r?.secure===!1)throw new Error("secure option must be true when using secure prefix");t=`__Secure-${t}`}if(r?.prefix==="host"){if(r?.secure===!1)throw new Error("secure option must be true when using host prefix");if(r?.domain)throw new Error("domain option must not be set when using host prefix");if(r?.path!=="/")throw new Error("path option must be set to / when using host prefix");t=`__Host-${t}`}return t};var d={getCookie:f,setCookie:c};var S=d;export{S as default,f as getCookie,c as setCookie};