@@ -351,31 +351,36 @@ interactive_getc(void)
351351#ifdef EMSCRIPTEN
352352EM_ASYNC_JS (char * , await_query , ( ), {
353353 // Await a query from JS land
354- var event = new Module .Event ("waiting" );
355- Module .eventTarget .dispatchEvent (event );
354+ Module .eventTarget .dispatchEvent (new Module .Event ("waiting" ));
356355 var query = await new Promise ((resolve , reject ) = > {
357356 Module .eventTarget .addEventListener ("query" , (e ) = > {
358357 resolve (e .detail );
359358 }, {once : true});
360359 });
361- var cstring_ptr = allocateUTF8 (query );
362- return cstring_ptr ;
360+ // `query` is a Uint8Array containing the query in pg wire format
361+ var bytes = query .length ;
362+ var ptr = _malloc (bytes );
363+ Module .HEAPU8 .set (query , ptr );
364+ return ptr ;
363365});
364366#endif
365367
366368static int
367369EmscriptenBackend (StringInfo inBuf )
368370{
369371 char * query = await_query ();
370- char qtype = * query ; // First character is qtype
371- int qlen = strlen (query );
372+ char qtype = * query ; // First byte is qtype
373+
374+ int32 msgLen = * ((int32 * )(query + 1 )); // Next 4 bytes are message length
375+ msgLen = pg_ntoh32 (msgLen );
376+ int dataLen = msgLen - 4 ; // The rest of the message is the data
372377
373378 resetStringInfo (inBuf );
374- if (qlen > 1 )
375- {
376- appendBinaryStringInfoNT ( inBuf , query + 1 , qlen - 1 );
377- appendStringInfoChar (inBuf , ( char ) '\0' );
378- }
379+ if (dataLen > 0 )
380+ {
381+ // Append the data to the buffer
382+ appendBinaryStringInfo (inBuf , query + 5 , dataLen );
383+ }
379384
380385 free (query );
381386
0 commit comments