import{o as e}from"./utils-kOqzSjf6.js";import t from"node:fs";const n=new Set([`__proto__`,`prototype`,`constructor`]);function r(e){return n.has(e)}const i=`default`,a=/^[a-z0-9][a-z0-9_-]{0,63}$/i,o=/[^a-z0-9_-]+/g,s=/^-+/,c=/-+$/,l=new Map,u=new Map;function d(e){return a.test(e)?e.toLowerCase():e.toLowerCase().replace(o,`-`).replace(s,``).replace(c,``).slice(0,64)}function f(e){let t=d(e);if(!(!t||r(t)))return t}function p(e){let t=(e??``).trim();if(!t)return i;let n=l.get(t);if(n)return n;let r=f(t)||`default`;return h(l,t,r),r}function m(e){let t=(e??``).trim();if(!t)return;if(u.has(t))return u.get(t);let n=f(t)||void 0;return h(u,t,n),n}function h(e,t,n){if(e.set(t,n),e.size<=512)return;let r=e.keys().next();r.done||e.delete(r.value)}function g(e){return e===0||e===0n}function _(e,t,n=process.platform){return e.ino===t.ino?e.dev===t.dev?!0:n===`win32`&&(g(e.dev)||g(t.dev)):!1}function v(e){let t=typeof e==`object`&&e&&`code`in e?String(e.code):``;return t===`ENOENT`||t===`ENOTDIR`||t===`ELOOP`}function y(e,t){return _(e,t)}function b(e){let n=e.ioFs??t,r=e.allowedType??`file`,i=n.constants.O_RDONLY|(typeof n.constants.O_NOFOLLOW==`number`?n.constants.O_NOFOLLOW:0),a=null;try{if(e.rejectPathSymlink&&n.lstatSync(e.filePath).isSymbolicLink())return{ok:!1,reason:`validation`};let t=e.resolvedPath??n.realpathSync(e.filePath),o=n.lstatSync(t);if(!x(o,r)||e.rejectHardlinks&&o.isFile()&&o.nlink>1||e.maxBytes!==void 0&&o.isFile()&&o.size>e.maxBytes)return{ok:!1,reason:`validation`};a=n.openSync(t,i);let s=n.fstatSync(a);if(!x(s,r)||e.rejectHardlinks&&s.isFile()&&s.nlink>1||e.maxBytes!==void 0&&s.isFile()&&s.size>e.maxBytes||!y(o,s))return{ok:!1,reason:`validation`};let c={ok:!0,path:t,fd:a,stat:s};return a=null,c}catch(e){return v(e)?{ok:!1,reason:`path`,error:e}:{ok:!1,reason:`io`,error:e}}finally{a!==null&&n.closeSync(a)}}function x(e,t){return t===`directory`?e.isDirectory():e.isFile()}function S(n,r,i={}){let a=e(n.trim());if(!a)return{ok:!1,message:`${r} file path is empty.`};let o=i.maxBytes??16384,s;try{s=t.lstatSync(a)}catch(e){return{ok:!1,resolvedPath:a,error:e,message:`Failed to inspect ${r} file at ${a}: ${String(e)}`}}if(i.rejectSymlink&&s.isSymbolicLink())return{ok:!1,resolvedPath:a,message:`${r} file at ${a} must not be a symlink.`};if(!s.isFile())return{ok:!1,resolvedPath:a,message:`${r} file at ${a} must be a regular file.`};if(s.size>o)return{ok:!1,resolvedPath:a,message:`${r} file at ${a} exceeds ${o} bytes.`};let c=b({filePath:a,rejectPathSymlink:i.rejectSymlink,maxBytes:o});if(!c.ok){let e=c.reason===`validation`?Error(`security validation failed`):c.error;return{ok:!1,resolvedPath:a,error:e,message:`Failed to read ${r} file at ${a}: ${String(e)}`}}try{let e=t.readFileSync(c.fd,`utf8`).trim();return e?{ok:!0,secret:e,resolvedPath:a}:{ok:!1,resolvedPath:a,message:`${r} file at ${a} is empty.`}}catch(e){return{ok:!1,resolvedPath:a,error:e,message:`Failed to read ${r} file at ${a}: ${String(e)}`}}finally{t.closeSync(c.fd)}}function C(e,t,n={}){if(!e?.trim())return;let r=S(e,t,n);return r.ok?r.secret:void 0}export{p as a,i,b as n,m as o,_ as r,r as s,C as t};
