@@ -5,22 +5,31 @@ export async function getTransformedResponse(_: {
55 transformer : ( base64Image : string ) => Record < string , any > ;
66} ) : Promise < Response | undefined > {
77 const { response, transformer } = _ ;
8-
9- console . info ( 'imageToJsonHandler > converting image response to base64 JSON' ) ;
10- const imageBuffer = await response . arrayBuffer ( ) ;
11- // Simple ArrayBuffer to base64 conversion for environments like Cloudflare Workers
12- let binary = '' ;
13- const bytes = new Uint8Array ( imageBuffer ) ;
14- const len = bytes . byteLength ;
15- for ( let i = 0 ; i < len ; i ++ ) {
16- binary += String . fromCharCode ( bytes [ i ] ) ;
8+ try {
9+ console . info (
10+ 'imageToJsonHandler > converting image response to base64 JSON'
11+ ) ;
12+ const imageBuffer = await response . arrayBuffer ( ) ;
13+ // Simple ArrayBuffer to base64 conversion for environments like Cloudflare Workers
14+ let binary = '' ;
15+ const bytes = new Uint8Array ( imageBuffer ) ;
16+ const len = bytes . byteLength ;
17+ for ( let i = 0 ; i < len ; i ++ ) {
18+ binary += String . fromCharCode ( bytes [ i ] ) ;
19+ }
20+ return new Response ( JSON . stringify ( transformer ( btoa ( binary ) ) ) , {
21+ headers : {
22+ ...Object . fromEntries ( response . headers ) , // keep original headers
23+ 'content-type' : CONTENT_TYPES . APPLICATION_JSON ,
24+ } ,
25+ status : response . status ,
26+ statusText : response . statusText ,
27+ } ) ;
28+ } catch ( error ) {
29+ console . error (
30+ 'imageToJsonHandler > error converting image response to base64 JSON' ,
31+ error
32+ ) ;
33+ return response ;
1734 }
18- return new Response ( JSON . stringify ( transformer ( btoa ( binary ) ) ) , {
19- headers : {
20- ...Object . fromEntries ( response . headers ) , // keep original headers
21- 'content-type' : CONTENT_TYPES . APPLICATION_JSON ,
22- } ,
23- status : response . status ,
24- statusText : response . statusText ,
25- } ) ;
2635}
0 commit comments