File: /var/www/quadcode.com/.svelte-kit/output/server/chunks/WLogger.js
import{nanoid as f}from"nanoid";import g from"@isaacs/ttlcache";import{createLogger as w,format as u,transports as m}from"winston";class L{rate;constructor(t){this.rate=t}async hash(t){return t.getClientAddress()}}class T{rate;constructor(t){this.rate=t}async hash(t){const e=t.request.headers.get("user-agent");return e?t.getClientAddress()+e:!1}}class p{rate;cookieOptions;secret;requirePreflight;cookieId;hashFunction;constructor(t){this.cookieId=t.name,this.secret=t.secret,this.rate=t.rate,this.requirePreflight=t.preflight,this.hashFunction=t.hashFunction??l,this.cookieOptions={path:"/",httpOnly:!0,maxAge:60*60*24*7,sameSite:"strict",...t.serializeOptions}}async hash(t){const e=await this.userIdFromCookie(t.cookies.get(this.cookieId),t);return e||!1}async preflight(t){const e=t.cookies.get(this.cookieId);if(e){const r=await this.userIdFromCookie(e,t);if(r)return r}const s=f();return t.cookies.set(this.cookieId,s+";"+await this.hashFunction(this.secret+s),this.cookieOptions),s}async userIdFromCookie(t,e){const s=()=>this.requirePreflight?null:this.preflight(e);if(!t)return s();const[r,i]=t.split(";");return!r||!i||await this.hashFunction(this.secret+r)!=i?s():r}}let l;globalThis?.crypto?.subtle&&(l=I);async function I(c){const t=await crypto.subtle.digest("SHA-256",new TextEncoder().encode(c));return[...new Uint8Array(t)].map(e=>e.toString(16).padStart(2,"0")).join("")}class a{store;plugins;onLimited;hashFunction;cookieLimiter;static TTLTime(t){switch(t){case"s":return 1e3;case"m":return 6e4;case"h":return 60*6e4;case"2s":return 2e3;case"5s":return 5e3;case"10s":return 1e4;case"15s":return 15e3;case"30s":return 3e4;case"45s":return 45e3;case"15m":return 15*6e4;case"30m":return 30*6e4;case"100ms":return 100;case"250ms":return 250;case"500ms":return 500;case"2h":return 2*60*6e4;case"6h":return 6*60*6e4;case"12h":return 12*60*6e4;case"d":return 24*60*6e4;case"ms":return 1}throw new Error("Invalid unit for TTLTime: "+t)}async isLimited(t){return(await this._isLimited(t)).limited}async clear(){return await this.store.clear()}async _isLimited(t){let e=!1;for(const s of this.plugins){const r=await s.hash(t);if(r===!1)return this.onLimited&&await this.onLimited(t,"rejected")===!0?{limited:!1,hash:null,unit:s.rate[1]}:{limited:!0,hash:null,unit:s.rate[1]};if(r===!0)return{limited:!1,hash:null,unit:s.rate[1]};if(r===null){e=!0;continue}else e=!1;if(!r)throw new Error("Empty hash returned from rate limiter "+s.constructor.name);const i=await this.hashFunction(r);if(await this.store.add(i,s.rate[1])>s.rate[0])return this.onLimited&&await this.onLimited(t,"rate")===!0?{limited:!1,hash:i,unit:s.rate[1]}:{limited:!0,hash:i,unit:s.rate[1]}}return{limited:e,hash:null,unit:this.plugins[this.plugins.length-1].rate[1]}}constructor(t={}){if(this.plugins=[...t.plugins??[]],this.onLimited=t.onLimited,this.hashFunction=t.hashFunction??l,!this.hashFunction)throw new Error("No RateLimiter hash function found. Please set one with the hashFunction option.");const e=t.IP??t.rates?.IP;e&&this.plugins.push(new L(e));const s=t.IPUA??t.rates?.IPUA;s&&this.plugins.push(new T(s));const r=t.cookie??t.rates?.cookie;if(r&&this.plugins.push(this.cookieLimiter=new p({hashFunction:this.hashFunction,...r})),!this.plugins.length)throw new Error("No plugins set for RateLimiter!");this.plugins.sort((o,n)=>{const h=a.TTLTime(o.rate[1])-a.TTLTime(n.rate[1]);return h==0?o.rate[0]-n.rate[0]:h});const i=this.plugins.reduce((o,n)=>{const h=n.rate[1];h=="ms"&&console.warn('RateLimiter: The "ms" unit is not reliable due to OS timing issues.');const d=a.TTLTime(h);return Math.max(d,o)},0);this.store=t.store??new k(i,t.maxItems)}}class k{cache;constructor(t,e=1/0){this.cache=new g({ttl:t,max:e,noUpdateTTL:!0})}async clear(){return this.cache.clear()}async add(t,e){const s=this.cache.get(t)??0;return this.set(t,s+1,e)}set(t,e,s){return this.cache.set(t,e,{ttl:a.TTLTime(s)}),e}}const A=new a({rates:{IP:[5,"h"]}}),C=w({level:"info",format:u.combine(u.timestamp({format:"YYYY-MM-DD HH:mm:ss"}),u.simple()),transports:[new m.Console,new m.File({filename:"Logger.log"})]});export{C as W,A as l};