44 * Persists files returned by node-hosted browser proxy calls and rewrites
55 * proxied result paths to local saved media paths.
66 */
7+ import { canonicalizeBase64 , estimateBase64DecodedBytes } from "openclaw/plugin-sdk/media-runtime" ;
78import {
89 assertBrowserProxyFileCountWithinLimit ,
910 assertBrowserProxyFileBytesWithinLimits ,
@@ -13,6 +14,18 @@ import {
1314} from "../browser-proxy-envelope.js" ;
1415import { saveMediaBuffer } from "../media/store.js" ;
1516
17+ function decodeBrowserProxyFileBase64 ( file : BrowserProxyFile , totalBytes : number ) : Buffer {
18+ const estimatedBytes = estimateBase64DecodedBytes ( file . base64 ) ;
19+ assertBrowserProxyFileBytesWithinLimits ( estimatedBytes , totalBytes + estimatedBytes ) ;
20+ const canonicalBase64 = canonicalizeBase64 ( file . base64 ) ;
21+ if ( ! canonicalBase64 ) {
22+ throw new Error ( "browser proxy file contains malformed base64 data" ) ;
23+ }
24+ const buffer = Buffer . from ( canonicalBase64 , "base64" ) ;
25+ assertBrowserProxyFileBytesWithinLimits ( buffer . byteLength , totalBytes + buffer . byteLength ) ;
26+ return buffer ;
27+ }
28+
1629/** Persist proxy-returned files and return a remote-path to local-path map. */
1730export async function persistBrowserProxyFiles ( files : BrowserProxyFile [ ] | undefined ) {
1831 if ( ! files || files . length === 0 ) {
@@ -22,9 +35,8 @@ export async function persistBrowserProxyFiles(files: BrowserProxyFile[] | undef
2235 const decoded : Array < { file : BrowserProxyFile ; buffer : Buffer } > = [ ] ;
2336 let totalBytes = 0 ;
2437 for ( const file of files ) {
25- const buffer = Buffer . from ( file . base64 , "base64" ) ;
38+ const buffer = decodeBrowserProxyFileBase64 ( file , totalBytes ) ;
2639 totalBytes += buffer . byteLength ;
27- assertBrowserProxyFileBytesWithinLimits ( buffer . byteLength , totalBytes ) ;
2840 decoded . push ( { file, buffer } ) ;
2941 }
3042
0 commit comments