openclaw
Version:
Multi-channel AI gateway with extensible messaging integrations
3 lines • 851 B
JavaScript
import crypto from"node:crypto";
//!extensions/file-transfer/src/tools/fetch-payload.ts
function decodeFetchPayload(e,t){const n=e===`file.fetch`;const r=typeof t.path===`string`?t.path:``;const i=t[n?`size`:`tarBytes`];const a=typeof i===`number`?i:-1;const o=t[n?`base64`:`tarBase64`];const s=n&&typeof t.mimeType===`string`?t.mimeType:``;const c=typeof t.sha256===`string`?t.sha256:``;if(!r||a<0||typeof o!==`string`||!(n?s:o)||!c)throw new Error(`invalid ${e} payload (missing fields)`);const l=Buffer.from(o,`base64`);if(l.byteLength!==a)throw new Error(`${e} size mismatch: payload says ${a} bytes, decoded ${l.byteLength}`);if(crypto.createHash(`sha256`).update(l).digest(`hex`)!==c)throw new Error(`${e} sha256 mismatch (integrity failure)`);return{canonicalPath:r,size:a,base64:o,mimeType:s,sha256:c,buffer:l}}export{decodeFetchPayload as t};