// @bun
import{randomBytes as F}from"crypto";import*as n from"fs/promises";import{tmpdir as O}from"os";import{join as d}from"path";function a(e){return typeof e==="object"&&e!==null}function l(e){if(!a(e)||typeof e.op!=="string")return!1;let r=typeof e.path==="string",o=typeof e.from==="string",i=typeof e.to==="string";switch(e.op){case"chmod":return r&&typeof e.mode==="number";case"chown":return r&&typeof e.uid==="number"&&typeof e.gid==="number";case"copy":case"copyFile":case"link":case"rename":case"symlink":return o&&i;case"makeDirectory":case"remove":case"truncate":return r;case"makeTempDirectory":case"makeTempFile":return!0;case"utimes":return r&&a(e.atime)&&a(e.mtime);case"writeFile":case"writeFileString":return r&&typeof e.data==="string";default:return!1}}function k(e){return l(e)&&e.op!=="makeTempDirectory"&&e.op!=="makeTempFile"}function m(e){if(!a(e)||typeof e.op!=="string")return!1;if(e.op!=="batch")return l(e);return Array.isArray(e.operations)&&e.operations.length>0&&e.operations.every(k)}function u(e){return e.type==="date"?new Date(e.value):e.value}function s(e){return typeof e==="object"&&e!==null}function t(e,r){if(!s(e))return;return e[r]}function b(e){return s(e)&&s(e.error)&&typeof e.error.message==="string"}function h(e,r){let o=e instanceof Error?e:Error(String(e)),i=t(e,"code"),p=t(e,"errno"),f=t(e,"syscall"),y=t(e,"path"),c=t(e,"dest");return{name:o.name,message:o.message,code:typeof i==="string"?i:void 0,errno:typeof p==="number"?p:void 0,syscall:typeof f==="string"?f:void 0,path:typeof y==="string"?y:void 0,dest:typeof c==="string"?c:void 0,operation:r}}async function g(e){let r=e.options?.prefix??"",o=e.options?.directory?d(e.options.directory,"."):O();return n.mkdtemp(r?d(o,r):o+"/")}async function T(e){switch(e.op){case"chmod":await n.chmod(e.path,e.mode);return;case"chown":await n.chown(e.path,e.uid,e.gid);return;case"copy":await n.cp(e.from,e.to,{force:e.options?.overwrite??!1,preserveTimestamps:e.options?.preserveTimestamps??!1,recursive:!0});return;case"copyFile":await n.copyFile(e.from,e.to);return;case"link":await n.link(e.from,e.to);return;case"makeDirectory":await n.mkdir(e.path,{recursive:e.options?.recursive??!1,mode:e.options?.mode});return;case"makeTempDirectory":return g(e);case"makeTempFile":{let r=await g(e),o=d(r,F(6).toString("hex")+(e.options?.suffix??""));return await n.writeFile(o,new Uint8Array(0)),o}case"remove":await n.rm(e.path,{recursive:e.options?.recursive??!1,force:e.options?.force??!1});return;case"rename":await n.rename(e.from,e.to);return;case"symlink":await n.symlink(e.from,e.to);return;case"truncate":await n.truncate(e.path,e.length);return;case"utimes":await n.utimes(e.path,u(e.atime),u(e.mtime));return;case"writeFile":await n.writeFile(e.path,Buffer.from(e.data,"base64"),e.options);return;case"writeFileString":await n.writeFile(e.path,e.data,e.options);return}throw TypeError("Unsupported filesystem mutation")}async function x(){let e=[];for await(let o of process.stdin)e.push(Buffer.isBuffer(o)?o:Buffer.from(o));let r=JSON.parse(Buffer.concat(e).toString("utf8"));if(!m(r))throw TypeError("Invalid filesystem mutation request");return r}function w(e){return T(e).catch((r)=>Promise.reject({error:h(r,e.op)}))}async function R(e){if(e.op!=="batch")return w(e);for(let r of e.operations)await w(r);return}var D=await x().then(R).then((e)=>({ok:!0,value:e}),(e)=>({ok:!1,error:b(e)?e.error:h(e)}));await new Promise((e,r)=>{process.stdout.write(JSON.stringify(D),(o)=>o?r(o):e())});
