@@ -12,7 +12,9 @@ import {
1212 RemoteConnector
1313} from '@powersync/common' ;
1414import { BSON } from 'bson' ;
15+ import Agent from 'proxy-agent' ;
1516import { ProxyAgent } from 'undici' ;
17+ import { WebSocket } from 'ws' ;
1618
1719export const STREAMING_POST_TIMEOUT_MS = 30_000 ;
1820
@@ -23,6 +25,8 @@ class NodeFetchProvider extends FetchImplementationProvider {
2325}
2426
2527export class NodeRemote extends AbstractRemote {
28+ protected agent : ProxyAgent | undefined ;
29+
2630 constructor (
2731 protected connector : RemoteConnector ,
2832 protected logger : ILogger = DEFAULT_REMOTE_LOGGER ,
@@ -31,23 +35,28 @@ export class NodeRemote extends AbstractRemote {
3135 // Automatic env vars are not supported by undici
3236 // proxy-agent does not work directly with dispatcher
3337 const proxy = process . env . HTTPS_PROXY ?? process . env . HTTP_PROXY ;
38+ const agent = proxy ? new ProxyAgent ( proxy ) : undefined ;
39+
3440 super ( connector , logger , {
3541 ...( options ?? { } ) ,
3642 fetchImplementation : options ?. fetchImplementation ?? new NodeFetchProvider ( ) ,
3743 fetchOptions : {
38- dispatcher : proxy ? new ProxyAgent ( proxy ) : undefined
44+ dispatcher : agent
3945 }
4046 } ) ;
47+
48+ this . agent = agent ;
4149 }
4250
43- // protected createSocket(url: string): globalThis.WebSocket {
44- // return new WebSocket(url, {
45- // // agent: new ProxyAgent(),
46- // headers: {
47- // 'User-Agent': this.getUserAgent()
48- // }
49- // }) as any as globalThis.WebSocket;
50- // }
51+ protected createSocket ( url : string ) : globalThis . WebSocket {
52+ return new WebSocket ( url , {
53+ // Undici does not seem to be compatible with ws
54+ agent : new Agent . ProxyAgent ( ) ,
55+ headers : {
56+ 'User-Agent' : this . getUserAgent ( )
57+ }
58+ } ) as any as globalThis . WebSocket ; // TODO
59+ }
5160
5261 getUserAgent ( ) : string {
5362 return [
0 commit comments