openclaw
Version:
Multi-channel AI gateway with extensible messaging integrations
1 lines • 3.16 kB
JavaScript
import{s as e}from"./transcript-plugin-instance-invocation-7pP9ZY8b.mjs";import{t}from"./transcript-usingCtx-03vOGQ3v.mjs";import{n}from"./transcript-tmp-openclaw-dir-BXMyB42n.mjs";import"./transcript-artifacts-BzUw22qv.mjs";import{t as r}from"./transcript-zstd-codec-Bwg8OR0e.mjs";import i from"node:path";import{createHash as a}from"node:crypto";import o from"node:fs";import{readFileWindowFully as s}from"@openclaw/fs-safe/advanced";import c from"node:zlib";import{Readable as l,Transform as u,pipeline as d}from"node:stream";import{finished as f}from"node:stream/promises";r();const p=4294967296;function isSessionArchiveDecodeError(e){let t=e instanceof Error&&`code`in e?e.code:void 0;return typeof t==`string`&&(t===`ERR_BUFFER_TOO_LARGE`||t===`Z_BUF_ERROR`||t.startsWith(`ZSTD_error_`))}function limitDecodedBytes(e){let t=0;return new u({transform(n,r,i){if(t+=n.length,t>p){let t=/* @__PURE__ */ RangeError(`Session archive ${e} expands past the ${p}-byte read budget; the archive is unchanged. Move it out of the sessions directory to stop reading it, or decompress it elsewhere with zstd to inspect it.`);i(Object.assign(t,{code:`ERR_BUFFER_TOO_LARGE`}))}else i(null,n)}})}function openSessionArchiveDecodedStream(e){return d(o.createReadStream(e),c.createZstdDecompress(),limitDecodedBytes(e),()=>void 0)}function resolveSessionArchiveReadPathPrefix(e,t){let r=a(`sha256`).update(e).digest(`hex`).slice(0,32);return i.join(t??i.join(n(),`session-archive-read-cache`),`${r}-`)}function resolveSessionArchiveReadPath(e,t,n){return e.endsWith(`.zst`)?`${resolveSessionArchiveReadPathPrefix(e,n)}${t.size}-${Math.trunc(t.mtimeMs)}.jsonl`:e}async function openSessionArchiveReader(e){let t=await o.promises.open(e,`r`),n,r,i=Buffer.alloc(0),a=0,stop=async()=>{let e=n;n=void 0,r=void 0,i=Buffer.alloc(0),e?.destroy(),e&&await f(e,{cleanup:!0}).catch(()=>void 0)};async function*readSource(){let e=0;for(;;){let n=Buffer.allocUnsafe(65536),{bytesRead:r}=await t.read(n,0,n.length,e);if(r===0)return;e+=r,yield n.subarray(0,r)}}return{stat:()=>t.stat(),async read(o,u){if(!e.endsWith(`.zst`))return s(t,o,u);u<a&&await stop(),r||=(a=0,n=d(l.from(readSource(),{objectMode:!1}),c.createZstdDecompress(),limitDecodedBytes(e),()=>void 0),n[Symbol.asyncIterator]());let f=0;for(;f<o.length;){if(i.length===0){let e=await r.next();if(e.done)break;if(!Buffer.isBuffer(e.value))throw TypeError(`Expected decoded archive bytes`);i=e.value}let e=Math.min(Math.max(0,u-a),i.length);if(a+=e,i=i.subarray(e),i.length>0){let e=i.copy(o,f,0,o.length-f);i=i.subarray(e),a+=e,f+=e}}return f},async close(){await stop(),await t.close()}}}async function readSessionArchivePrefix(n,r){let i=Buffer.alloc(r);if(!n.endsWith(`.zst`))try{var a=t();let r=a.a(await e({filePath:n}));return i.subarray(0,await s(r.handle,i,0))}catch(e){a.e=e}finally{await a.d()}let o=await openSessionArchiveReader(n);try{return i.subarray(0,await o.read(i,0))}finally{await o.close()}}export{resolveSessionArchiveReadPath as a,readSessionArchivePrefix as i,openSessionArchiveDecodedStream as n,resolveSessionArchiveReadPathPrefix as o,openSessionArchiveReader as r,isSessionArchiveDecodeError as t};