1
- import { PGlite } from '@electric-sql/pglite ' ;
1
+ import fs from 'node:fs ' ;
2
2
import net from 'node:net' ;
3
- import { PostgresConnection , hashMd5Password } from 'pg-gateway' ;
3
+ import { PGlite } from '@electric-sql/pglite' ;
4
+ import { type BackendError , PostgresConnection } from 'pg-gateway' ;
4
5
5
6
const db = new PGlite ( ) ;
6
7
7
8
const server = net . createServer ( ( socket ) => {
8
9
const connection = new PostgresConnection ( socket , {
9
10
serverVersion : '16.3 (PGlite 0.2.0)' ,
10
- authMode : 'md5Password' ,
11
- async validateCredentials ( credentials ) {
12
- if ( credentials . authMode === 'md5Password' ) {
13
- const { hash, salt } = credentials ;
14
- const expectedHash = await hashMd5Password (
15
- 'postgres' ,
16
- 'postgres' ,
17
- salt
18
- ) ;
19
- return hash === expectedHash ;
20
- }
21
- return false ;
11
+ tls : {
12
+ key : fs . readFileSync ( 'key.pem' ) ,
13
+ cert : fs . readFileSync ( 'cert.pem' ) ,
14
+ } ,
15
+ auth : {
16
+ method : 'cert' ,
22
17
} ,
18
+
23
19
async onStartup ( ) {
24
20
// Wait for PGlite to be ready before further processing
25
21
await db . waitReady ;
@@ -33,10 +29,13 @@ const server = net.createServer((socket) => {
33
29
34
30
// Forward raw message to PGlite
35
31
try {
36
- const [ [ _ , responseData ] ] = await db . execProtocol ( data ) ;
37
- connection . sendData ( responseData ) ;
32
+ const [ result ] = await db . execProtocol ( data ) ;
33
+ if ( result ) {
34
+ const [ _ , responseData ] = result ;
35
+ connection . sendData ( responseData ) ;
36
+ }
38
37
} catch ( err ) {
39
- connection . sendError ( err ) ;
38
+ connection . sendError ( err as BackendError ) ;
40
39
connection . sendReadyForQuery ( ) ;
41
40
}
42
41
return true ;
0 commit comments