UNPKG

openclaw

Version:

Multi-channel AI gateway with extensible messaging integrations

1 lines • 5.78 kB
import{Ct as e,Dt as t,Xt as n,an as r,cn as i,h as a,pn as o}from"./openclaw-state-read-src-RL5xtYG2.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,t=r(),{maxBytes:n=1/0,signal:a}={},c=!1){let d={stack:[],error:void 0,hasError:!1};try{a?.throwIfAborted();let r=s.fstatSync(e.fd);if(!r.isFile())throw new o(`not-file`,`SHA-256 input is not a regular file`);if(r.size>n)throw new o(`too-large`,`SHA-256 input exceeds ${n} bytes`);let u=i(t,`sha256File`);if(u){let t=a?AbortSignal.any([a]):void 0;try{let r=await u.sha256File(e.fd,Number.isFinite(n)?n:void 0,t,c);return a?.throwIfAborted(),r}catch(e){throw a?.throwIfAborted(),e?.code===`too-large`?new o(`too-large`,`SHA-256 input exceeds ${n} bytes`,{cause:e}):e}finally{t&&(t.onabort=null)}}let f=l(`sha256`),p=__addDisposableResource(d,new HashScratch(Math.min(262144,Math.max(1,r.size+1),n+1)),!1),m=p.buffer,h=0;for(;;){a?.throwIfAborted();let t=Math.min(m.length,n-h+1),{bytesRead:r}=await e.read(m,0,t,h);if(a?.throwIfAborted(),r>n-h)throw new o(`too-large`,`SHA-256 input exceeds ${n} bytes`);if(r===0)return{bytes:h,digest:f.digest(`hex`)};f.update(m.subarray(0,r)),h+=r,r===m.length&&m.length<65536&&(m=p.grow(Math.min(65536,n+1)))}}catch(e){d.error=e,d.hasError=!0}finally{u(d)}}function hashPathIdentity(e,t=!1){let n=s.lstatSync(e,{bigint:!0});if(t&&(n.isSymbolicLink()||!n.isFile()))throw new o(`path-mismatch`,`SHA-256 path changed while opening`);if(n.isSymbolicLink())throw new o(`symlink`,`SHA-256 path must not be a symbolic link`);if(!n.isFile())throw new o(`not-file`,`SHA-256 path is not a regular file`);return n}function hashDescriptorIdentity(e){let t=s.fstatSync(e,{bigint:!0});if(!t.isFile())throw new o(`not-file`,`SHA-256 path is not a regular file`);return t}function rethrowHashOpenError(e){throw e?.code===`ELOOP`?new o(`symlink`,`SHA-256 path must not be a symbolic link`,{cause:e}):e}async function hashPath(t,i){n(t,`filesystem`,`SHA-256 path uses a Windows filesystem namespace alias`);let o=e(()=>hashPathIdentity(t)),s;try{i.signal?.throwIfAborted(),s=await c.open(t,a())}catch(e){rethrowHashOpenError(e)}let l=!1;try{i.signal?.throwIfAborted();let n=e(()=>hashDescriptorIdentity(s.fd),o);e(()=>hashPathIdentity(t,!0),n);let a=await hashFileHandle(s,r(),i,!0);return l=!0,a}finally{l?await s.close():await s.close().catch(()=>void 0)}}async function sha256File(e,n={}){n.signal?.throwIfAborted();let i={maxBytes:t(n.maxBytes),signal:n.signal};return typeof e==`string`?await hashPath(e,i):await hashFileHandle(e,r(),i)}function hashDescriptorSync(e,{maxBytes:t=1/0,signal:n}){let r={stack:[],error:void 0,hasError:!1};try{n?.throwIfAborted();let i=s.fstatSync(e);if(!i.isFile())throw new o(`not-file`,`SHA-256 input is not a regular file`);if(i.size>t)throw new o(`too-large`,`SHA-256 input exceeds ${t} bytes`);let a=l(`sha256`),c=__addDisposableResource(r,new HashScratch(Math.min(262144,Math.max(1,i.size+1),t+1)),!1),u=c.buffer,d=0;for(;;){n?.throwIfAborted();let r=Math.min(u.length,t-d+1),i=s.readSync(e,u,0,r,d);if(n?.throwIfAborted(),i>t-d)throw new o(`too-large`,`SHA-256 input exceeds ${t} bytes`);if(i===0)return{bytes:d,digest:a.digest(`hex`)};a.update(u.subarray(0,i)),d+=i,i===u.length&&u.length<65536&&(u=c.grow(Math.min(65536,t+1)))}}catch(e){r.error=e,r.hasError=!0}finally{u(r)}}function hashPathSync(t,r){n(t,`filesystem`,`SHA-256 path uses a Windows filesystem namespace alias`);let i=e(()=>hashPathIdentity(t)),o;try{r.signal?.throwIfAborted(),o=s.openSync(t,a())}catch(e){rethrowHashOpenError(e)}let c=!1;try{r.signal?.throwIfAborted();let n=e(()=>hashDescriptorIdentity(o),i);e(()=>hashPathIdentity(t,!0),n);let a=hashDescriptorSync(o,r);return c=!0,a}finally{try{s.closeSync(o)}catch(e){if(c)throw e}}}function sha256FileSync(e,n={}){n.signal?.throwIfAborted();let r={maxBytes:t(n.maxBytes),signal:n.signal};return typeof e==`string`?hashPathSync(e,r):hashDescriptorSync(e,r)}export{sha256FileSync as n,sha256File as t};