11import { MessageType , RequestMessage } from "../models/WorkerMessage" ;
22import { isNodeJs } from "./EnvironmentService" ;
3+ import { numberToHex } from "./HexService" ;
34
45export function createWorker ( url : URL , options ?: any ) : Worker {
56 let WorkerConstructor : any = null ;
@@ -90,7 +91,8 @@ export function callMethodOnMainThread(notifierBuffer: SharedArrayBuffer, method
9091
9192 // The main thread only writes the length of the result inside the notifierbuffer
9293 // We create a big enough shared buffer so the main thread can write the full result
93- const bufferLength = parseInt ( Buffer . from ( notifierBuffer ) . toString ( 'hex' ) , 16 ) ;
94+ const writtenToBuffer = Buffer . from ( notifierBuffer ) . slice ( 0 ) ; // slice 1 of since that is our notify index
95+ const bufferLength = parseInt ( writtenToBuffer . toString ( 'hex' ) , 16 ) ;
9496
9597 if ( bufferLength === 0 ) {
9698 return null ;
@@ -110,4 +112,12 @@ export function callMethodOnMainThread(notifierBuffer: SharedArrayBuffer, method
110112 resetSharedBuffer ( notifierBuffer , 0 ) ;
111113
112114 return Buffer . from ( valueBuffer ) ;
115+ }
116+
117+ export function sendBufferLengthToWorker ( notifierBuffer : SharedArrayBuffer , value ?: Buffer ) {
118+ const lengthInHex = numberToHex ( value ?. length ?? 0 , ( notifierBuffer . byteLength * 2 ) ) ;
119+ const u8NotifierBuffer = new Uint8Array ( notifierBuffer ) ;
120+ u8NotifierBuffer . set ( Buffer . from ( lengthInHex , 'hex' ) , 0 ) ;
121+
122+ storeAndNotify ( notifierBuffer , 0 , 1 ) ;
113123}
0 commit comments