@@ -676,10 +676,17 @@ class WebuiBridge {
676676 const scriptSanitize = script . replace ( / (?: \r \n | \r | \n ) / g, '\n' ) ;
677677 if ( this . #log) console . log ( `WebUI -> CMD -> JS [${ scriptSanitize } ]` ) ;
678678 // Get callback result
679- let FunReturn = 'undefined' ;
679+ let FunReturn : string | Uint8Array = 'undefined' ;
680680 let FunError = false ;
681+ let isBinaryReturn = false ;
681682 try {
682- FunReturn = await AsyncFunction ( scriptSanitize ) ( ) ;
683+ const result = await AsyncFunction ( scriptSanitize ) ( ) ;
684+ if ( result instanceof Uint8Array ) {
685+ FunReturn = result ;
686+ isBinaryReturn = true ;
687+ } else {
688+ FunReturn = String ( result ) ;
689+ }
683690 } catch ( e ) {
684691 FunError = true ;
685692 FunReturn = e . message ;
@@ -691,8 +698,20 @@ class WebuiBridge {
691698 FunReturn = 'undefined' ;
692699 }
693700 // Logging
694- if ( this . #log && ! FunError ) console . log ( `WebUI -> CMD -> JS -> Return Success [${ FunReturn } ]` ) ;
695- if ( this . #log && FunError ) console . log ( `WebUI -> CMD -> JS -> Return Error [${ FunReturn } ]` ) ;
701+ if ( this . #log && ! FunError ) {
702+ if ( isBinaryReturn ) {
703+ const binaryData = FunReturn as Uint8Array ;
704+ const hexPreview = Array . from ( binaryData . slice ( 0 , 64 ) ) . map ( b => `0x${ b . toString ( 16 ) . padStart ( 2 , '0' ) } ` ) . join ( ', ' ) ;
705+ console . log ( `WebUI -> CMD -> JS -> Return Success (${ binaryData . length } Bytes) [${ hexPreview } ${ binaryData . length > 64 ? '...' : '' } ]` ) ;
706+ } else {
707+ const stringData = String ( FunReturn ) ;
708+ console . log ( `WebUI -> CMD -> JS -> Return Success (${ new TextEncoder ( ) . encode ( stringData ) . length } Bytes) [${ stringData . substring ( 0 , 64 ) } ${ stringData . length > 64 ? '...' : '' } ]` ) ;
709+ }
710+ }
711+ else if ( this . #log && FunError ) {
712+ const errorString = String ( FunReturn ) ;
713+ console . log ( `WebUI -> CMD -> JS -> Return Error [${ errorString . substring ( 0 , 64 ) } ${ errorString . length > 64 ? '...' : '' } ]` ) ;
714+ }
696715 // Protocol
697716 // 0: [SIGNATURE]
698717 // 1: [TOKEN]
@@ -724,7 +743,11 @@ class WebuiBridge {
724743 packetPush ( new Uint8Array ( [ 0 , 0 ] ) ) ; // ID (2 Bytes)
725744 packetPush ( new Uint8Array ( [ this . #CMD_JS] ) ) ;
726745 packetPush ( new Uint8Array ( FunError ? [ 1 ] : [ 0 ] ) ) ;
727- packetPushStr ( FunReturn ) ;
746+ if ( isBinaryReturn ) {
747+ packetPush ( FunReturn as Uint8Array ) ;
748+ } else {
749+ packetPushStr ( FunReturn as string ) ;
750+ }
728751 packetPush ( new Uint8Array ( [ 0 ] ) ) ;
729752 this . #addToken( packet , this . #token, this . #PROTOCOL_TOKEN) ;
730753 this . #addID( packet , callId , this . #PROTOCOL_ID) ;
0 commit comments