@@ -66,7 +66,7 @@ export class PostgresServer {
6666 await db . query ( "DEALLOCATE ALL" ) ;
6767 }
6868 const response = await db . execProtocolRaw ( data ) ;
69- for await ( const message of extendedQueryPatch . filterResponse ( data , response ) ) {
69+ for await ( const message of extendedQueryPatch . getMessages ( data , response ) ) {
7070 yield message ;
7171 }
7272
@@ -75,7 +75,7 @@ export class PostgresServer {
7575 } ,
7676 } ) ;
7777
78- const extendedQueryPatch : PGliteExtendedQueryPatch = new PGliteExtendedQueryPatch ( connection ) ;
78+ const extendedQueryPatch : PGliteLoggerPatch = new PGliteLoggerPatch ( connection ) ;
7979 socket . on ( "end" , ( ) => {
8080 logger . debug ( "Postgres client disconnected" ) ;
8181 } ) ;
@@ -279,52 +279,44 @@ export async function fromNodeSocket(socket: net.Socket, options?: PostgresConne
279279 return new PostgresConnection ( { readable : rs , writable : ws } , opts ) ;
280280}
281281
282- export class PGliteExtendedQueryPatch {
283- isExtendedQuery = false ;
284- eqpErrored = false ;
285- pgliteDebugLog : fs . WriteStream ;
286-
287- constructor ( public connection : PostgresConnection ) {
288- this . pgliteDebugLog = fs . createWriteStream ( "pglite-debug.log" ) ;
282+ const CODE_TO_FRONTEND_MESSAGE : Record < number , string > = { } ;
283+ for ( const key in FrontendMessageCode ) {
284+ if ( Object . prototype . hasOwnProperty . call ( FrontendMessageCode , key ) ) {
285+ CODE_TO_FRONTEND_MESSAGE [ FrontendMessageCode [ key as keyof typeof FrontendMessageCode ] ] = key ;
289286 }
287+ }
290288
291- async * filterResponse ( message : Uint8Array , response : Uint8Array ) {
292- // 'Parse' indicates the start of an extended query
293- const pipelineStartMessages : number [ ] = [
294- FrontendMessageCode . Parse ,
295- FrontendMessageCode . Bind ,
296- FrontendMessageCode . Close ,
297- ] ;
298- const decoded = decoder . write ( message as any as Buffer ) ;
289+ const CODE_TO_BACKEND_MESSAGE : Record < number , string > = { } ;
290+ for ( const key in BackendMessageCode ) {
291+ if ( Object . prototype . hasOwnProperty . call ( BackendMessageCode , key ) ) {
292+ CODE_TO_BACKEND_MESSAGE [ BackendMessageCode [ key as keyof typeof BackendMessageCode ] ] = key ;
293+ }
294+ }
299295
300- this . pgliteDebugLog . write ( "Front: " + decoded ) ;
296+ function codeToFrontendMessageName ( code : number ) : string {
297+ return CODE_TO_FRONTEND_MESSAGE [ code ] || `UNKNOWN_FRONTEND_CODE_${ code } ` ;
298+ }
301299
302- if ( pipelineStartMessages . includes ( message [ 0 ] ) ) {
303- this . isExtendedQuery = true ;
304- }
300+ function codeTogBackendMessageName ( code : number ) : string {
301+ return CODE_TO_BACKEND_MESSAGE [ code ] || `UNKNOWN_BACKEND_CODE_${ code } ` ;
302+ }
303+ export class PGliteLoggerPatch {
304+ pgliteDebugLog : fs . WriteStream ;
305305
306- // 'Sync' indicates the end of an extended query
307- if ( message [ 0 ] === FrontendMessageCode . Sync ) {
308- this . isExtendedQuery = false ;
309- this . eqpErrored = false ;
310- }
306+ constructor ( public connection : PostgresConnection ) {
307+ this . pgliteDebugLog = fs . createWriteStream ( "pglite-debug.log" ) ;
308+ }
311309
310+ async * getMessages ( request : Uint8Array , response : Uint8Array ) {
311+ this . pgliteDebugLog . write (
312+ `\n[-> ${ codeToFrontendMessageName ( request [ 0 ] ) } ] ` + decoder . write ( request as any as Buffer ) ,
313+ ) ;
312314 // A PGlite response can contain multiple messages
315+ // https://www.postgresql.org/docs/current/protocol-message-formats.html
313316 for await ( const bm of getMessages ( response ) ) {
314- // After an ErrorMessage in extended query protocol, we should throw away messages until the next Sync
315- // (per https://www.postgresql.org/docs/current/protocol-flow.html#PROTOCOL-FLOW-EXT-QUERY:~:text=When%20an%20error,for%20each%20Sync.))
316- if ( this . eqpErrored ) {
317- continue ;
318- }
319- if ( this . isExtendedQuery && bm [ 0 ] === BackendMessageCode . ErrorMessage ) {
320- this . eqpErrored = true ;
321- }
322- // Filter out incorrect `ReadyForQuery` messages during the extended query protocol
323- if ( this . isExtendedQuery && bm [ 0 ] === BackendMessageCode . ReadyForQuery ) {
324- this . pgliteDebugLog . write ( "Filtered: " + decoder . write ( bm as any as Buffer ) ) ;
325- continue ;
326- }
327- this . pgliteDebugLog . write ( "Sent: " + decoder . write ( bm as any as Buffer ) ) ;
317+ this . pgliteDebugLog . write (
318+ `\n[<- ${ codeTogBackendMessageName ( bm [ 0 ] ) } ] ${ decoder . write ( bm as any as Buffer ) } ` ,
319+ ) ;
328320 yield bm ;
329321 }
330322 }
0 commit comments