openclaw
Version:
Multi-channel AI gateway with extensible messaging integrations
3 lines • 4.55 kB
JavaScript
import{a as extractErrorCode}from"./error-coercion-DlKnt6w-.mjs";import{r as canonicalPathFromExistingAncestor,t as FsSafeError,v as resolveAbsolutePathForWrite}from"./fs-safe-Bxzu1j4h.mjs";import"./security-runtime-Y28JTB1K.mjs";import{n as matchesFileIdentity,r as readPathBinding,t as fileIdentity}from"./path-binding-DEwRklUS.mjs";import{r as rejectCanonicalPathChange}from"./path-errors-BePH4DjW.mjs";import{n as readFileCreateMetadata,t as FILE_CREATE_CHUNK_BYTES}from"./file-create-protocol-BCmesQ8y.mjs";import{a as symlinkRedirectError,i as openBoundWriteRoot,n as captureWriteBinding,o as writeFsSafeError,r as fileWriteError,t as canonicalTargetForSymlinkError}from"./file-write-path-D1RZarC9.mjs";import crypto from"node:crypto";import fs from"node:fs/promises";
//!extensions/file-transfer/src/node-host/file-create.ts
async function receiveContent(e,t){const n=e.frames;if(!n)throw new Error(`file.create requires binary duplex transport`);e.signal.throwIfAborted();const r=[];const i=crypto.createHash(`sha256`);let a=0;let o=false;let s;let removeAbortListener;try{return await new Promise((c,l)=>{const abort=()=>{const t=e.signal.reason;l(t instanceof Error?t:/* @__PURE__ */ new Error(`file.create cancelled`))};e.signal.addEventListener(`abort`,abort,{once:true});removeAbortListener=()=>e.signal.removeEventListener(`abort`,abort);const fail=t=>{o=true;e.signal.removeEventListener(`abort`,abort);l(t instanceof Error?t:new Error(String(t)))};s=n.onMessage(async s=>{try{e.signal.throwIfAborted();if(o)throw new Error(`file.create input is out of sequence`);if(s.byteLength===0){o=true;if(a!==t.sizeBytes||i.digest(`hex`)!==t.expectedSha256)throw new Error(`file.create content size or digest does not match`);e.signal.removeEventListener(`abort`,abort);c(r);return}if(s.byteLength>1048576||a+s.byteLength>t.sizeBytes)throw new Error(`file.create chunk exceeds the admitted byte limit`);const l=Buffer.from(s);a+=l.byteLength;i.update(l);r.push(l);await n.send(Buffer.from(`ack`))}catch(e){fail(e)}})})}finally{s?.();removeAbortListener?.()}}async function handleFileCreate(e,t){let n;try{n=readFileCreateMetadata(e)}catch(e){return fileWriteError(`INVALID_PARAMS`,String(e))}const r=typeof e.path===`string`?e.path:``;const i=e.followSymlinks===true;t?.signal.throwIfAborted();let a;try{a=await resolveAbsolutePathForWrite(r,{symlinks:i?`follow`:`reject`})}catch(e){if(e instanceof FsSafeError&&e.code===`symlink`)return symlinkRedirectError(`SYMLINK_REDIRECT`,await canonicalTargetForSymlinkError(e,r));return fileWriteError(`INVALID_PATH`,String(e))}if(!a.parentExists&&e.createParents!==true)return fileWriteError(`PARENT_NOT_FOUND`,`parent directory does not exist`);const o=await canonicalPathFromExistingAncestor(a.path);const s=rejectCanonicalPathChange(e.expectedCanonicalPath,o);if(s)return s;let c;try{c=await fs.lstat(a.path,{bigint:true});if(!c.isFile())return fileWriteError(`NOT_FILE`,`create target is not a regular file`)}catch(e){if(extractErrorCode(e)!==`ENOENT`)throw e}if(e.preflightOnly===true)return{ok:true,path:o,size:n.sizeBytes,sha256:n.expectedSha256,binding:await captureWriteBinding(o,c&&fileIdentity(c))};const l=readPathBinding(e.expectedBinding);if(l?.kind!==`write`)return fileWriteError(`CANONICAL_PATH_CHANGED`,`file.create requires its authorized write binding`);if(l.targetDevice!==void 0&&(!c||!matchesFileIdentity(c,{device:l.targetDevice,inode:l.targetInode})))return fileWriteError(`CANONICAL_PATH_CHANGED`,`create target changed after authorization`);const u=await openBoundWriteRoot({binding:l,canonicalTargetPath:o});if(!u.ok)return u;if(!t?.frames)return fileWriteError(`DUPLEX_REQUIRED`,`file.create requires binary duplex transport`);const d=await receiveContent(t,n);t.signal.throwIfAborted();const{anchorRoot:f,relativeTarget:p}=u;let m=`created`;try{await f.create(p,(async function*(){yield*d})(),{mkdir:e.createParents===true,mode:384,maxBytes:n.sizeBytes,signal:t.signal,assertBeforeMutation:()=>t.signal.throwIfAborted()})}catch(e){t.signal.throwIfAborted();if(!(e instanceof FsSafeError))throw e;if(e.code!==`already-exists`)return writeFsSafeError(e,o);m=`exists`}const h=await f.open(p,{symlinks:`reject`});try{t.signal.throwIfAborted();const e=await h.handle.stat({bigint:true});if(!e.isFile())return fileWriteError(`NOT_FILE`,`create target is not a regular file`);return{ok:true,path:h.realPath,status:m,...m===`created`?{size:n.sizeBytes,sha256:n.expectedSha256}:{},binding:{kind:`existing`,...fileIdentity(e)}}}finally{await h.handle.close()}}export{handleFileCreate};