UNPKG

openclaw

Version:

Multi-channel AI gateway with extensible messaging integrations

1 lines • 5.86 kB
import{M as e,R as t,p as n,t as r,u as i}from"./openclaw-state-read-read-open-flags-CtNL1rJs.mjs";import{vt as a,xt as o}from"./openclaw-state-read-root-symlink-policy-AldRD6ik.mjs";import s from"node:fs";import c from"node:fs/promises";import{createHash as l}from"node:crypto";var __addDisposableResource=function(e,t,n){if(t!=null){if(typeof t!=`object`&&typeof t!=`function`)throw TypeError(`Object expected.`);var dispose,r;if(n){if(!Symbol.asyncDispose)throw TypeError(`Symbol.asyncDispose is not defined.`);dispose=t[Symbol.asyncDispose]}if(dispose===void 0){if(!Symbol.dispose)throw TypeError(`Symbol.dispose is not defined.`);dispose=t[Symbol.dispose],n&&(r=dispose)}if(typeof dispose!=`function`)throw TypeError(`Object not disposable.`);r&&(dispose=function(){try{r.call(this)}catch(e){return Promise.reject(e)}}),e.stack.push({value:t,dispose,async:n})}else n&&e.stack.push({async:!0});return t},u=(function(e){return function(t){function fail(n){t.error=t.hasError?new e(n,t.error,`An error was suppressed during disposal.`):n,t.hasError=!0}var n,r=0;function next(){for(;n=t.stack.pop();)try{if(!n.async&&r===1)return r=0,t.stack.push(n),Promise.resolve().then(next);if(n.dispose){var e=n.dispose.call(n.value);if(n.async)return r|=2,Promise.resolve(e).then(next,function(e){return fail(e),next()})}else r|=1}catch(e){fail(e)}if(r===1)return t.hasError?Promise.reject(t.error):Promise.resolve();if(t.hasError)throw t.error}return next()}})(typeof SuppressedError==`function`?SuppressedError:function(e,t,n){var r=Error(n);return r.name=`SuppressedError`,r.error=e,r.suppressed=t,r});const d=[];function takeHashScratch(e){let t=d.findIndex(t=>t.length>=e);return t<0?Buffer.allocUnsafe(e):d.splice(t,1)[0]}var HashScratch=class{#e;buffer;constructor(e){this.#e=takeHashScratch(e),this.buffer=this.#e.subarray(0,e)}grow(e){let t=takeHashScratch(e);return this[Symbol.dispose](),this.#e=t,this.buffer=t.subarray(0,e)}[Symbol.dispose](){this.buffer.fill(0),d.push(this.#e),d.sort((e,t)=>e.length-t.length),d.length>4&&d.shift()}};async function hashFileHandle(e,n=a(),{maxBytes:r=1/0,signal:i}={},c=!1){let d={stack:[],error:void 0,hasError:!1};try{i?.throwIfAborted();let a=s.fstatSync(e.fd);if(!a.isFile())throw new t(`not-file`,`SHA-256 input is not a regular file`);if(a.size>r)throw new t(`too-large`,`SHA-256 input exceeds ${r} bytes`);let u=o(n,`sha256File`);if(u){let n=i?AbortSignal.any([i]):void 0;try{let t=await u.sha256File(e.fd,Number.isFinite(r)?r:void 0,n,c);return i?.throwIfAborted(),t}catch(e){throw i?.throwIfAborted(),e?.code===`too-large`?new t(`too-large`,`SHA-256 input exceeds ${r} bytes`,{cause:e}):e}finally{n&&(n.onabort=null)}}let f=l(`sha256`),p=__addDisposableResource(d,new HashScratch(Math.min(262144,Math.max(1,a.size+1),r+1)),!1),m=p.buffer,h=0;for(;;){i?.throwIfAborted();let n=Math.min(m.length,r-h+1),{bytesRead:a}=await e.read(m,0,n,h);if(i?.throwIfAborted(),a>r-h)throw new t(`too-large`,`SHA-256 input exceeds ${r} bytes`);if(a===0)return{bytes:h,digest:f.digest(`hex`)};f.update(m.subarray(0,a)),h+=a,a===m.length&&m.length<65536&&(m=p.grow(Math.min(65536,r+1)))}}catch(e){d.error=e,d.hasError=!0}finally{u(d)}}function hashPathIdentity(e,n=!1){let r=s.lstatSync(e,{bigint:!0});if(n&&(r.isSymbolicLink()||!r.isFile()))throw new t(`path-mismatch`,`SHA-256 path changed while opening`);if(r.isSymbolicLink())throw new t(`symlink`,`SHA-256 path must not be a symbolic link`);if(!r.isFile())throw new t(`not-file`,`SHA-256 path is not a regular file`);return r}function hashDescriptorIdentity(e){let n=s.fstatSync(e,{bigint:!0});if(!n.isFile())throw new t(`not-file`,`SHA-256 path is not a regular file`);return n}function rethrowHashOpenError(e){throw e?.code===`ELOOP`?new t(`symlink`,`SHA-256 path must not be a symbolic link`,{cause:e}):e}async function hashPath(t,i){e(t,`filesystem`,`SHA-256 path uses a Windows filesystem namespace alias`);let o=n(()=>hashPathIdentity(t)),s;try{i.signal?.throwIfAborted(),s=await c.open(t,r())}catch(e){rethrowHashOpenError(e)}let l=!1;try{i.signal?.throwIfAborted();let e=n(()=>hashDescriptorIdentity(s.fd),o);n(()=>hashPathIdentity(t,!0),e);let r=await hashFileHandle(s,a(),i,!0);return l=!0,r}finally{l?await s.close():await s.close().catch(()=>void 0)}}async function sha256File(e,t={}){t.signal?.throwIfAborted();let n={maxBytes:i(t.maxBytes),signal:t.signal};return typeof e==`string`?await hashPath(e,n):await hashFileHandle(e,a(),n)}function hashDescriptorSync(e,{maxBytes:n=1/0,signal:r}){let i={stack:[],error:void 0,hasError:!1};try{r?.throwIfAborted();let a=s.fstatSync(e);if(!a.isFile())throw new t(`not-file`,`SHA-256 input is not a regular file`);if(a.size>n)throw new t(`too-large`,`SHA-256 input exceeds ${n} bytes`);let o=l(`sha256`),c=__addDisposableResource(i,new HashScratch(Math.min(262144,Math.max(1,a.size+1),n+1)),!1),u=c.buffer,d=0;for(;;){r?.throwIfAborted();let i=Math.min(u.length,n-d+1),a=s.readSync(e,u,0,i,d);if(r?.throwIfAborted(),a>n-d)throw new t(`too-large`,`SHA-256 input exceeds ${n} bytes`);if(a===0)return{bytes:d,digest:o.digest(`hex`)};o.update(u.subarray(0,a)),d+=a,a===u.length&&u.length<65536&&(u=c.grow(Math.min(65536,n+1)))}}catch(e){i.error=e,i.hasError=!0}finally{u(i)}}function hashPathSync(t,i){e(t,`filesystem`,`SHA-256 path uses a Windows filesystem namespace alias`);let a=n(()=>hashPathIdentity(t)),o;try{i.signal?.throwIfAborted(),o=s.openSync(t,r())}catch(e){rethrowHashOpenError(e)}let c=!1;try{i.signal?.throwIfAborted();let e=n(()=>hashDescriptorIdentity(o),a);n(()=>hashPathIdentity(t,!0),e);let r=hashDescriptorSync(o,i);return c=!0,r}finally{try{s.closeSync(o)}catch(e){if(c)throw e}}}function sha256FileSync(e,t={}){t.signal?.throwIfAborted();let n={maxBytes:i(t.maxBytes),signal:t.signal};return typeof e==`string`?hashPathSync(e,n):hashDescriptorSync(e,n)}export{sha256FileSync as n,sha256File as t};