Skip to content

Commit 468dc58

Browse files
proxy for websockets
1 parent 57c4ab1 commit 468dc58

File tree

3 files changed

+35
-40
lines changed

3 files changed

+35
-40
lines changed

packages/node/package.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,9 @@
5252
"async-lock": "^1.4.0",
5353
"bson": "^6.6.0",
5454
"comlink": "^4.4.2",
55-
"undici": "^7.8.0"
55+
"proxy-agent": "^6.5.0",
56+
"undici": "^7.8.0",
57+
"ws": "^8.18.1"
5658
},
5759
"devDependencies": {
5860
"@powersync/drizzle-driver": "workspace:*",

packages/node/src/sync/stream/NodeRemote.ts

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,9 @@ import {
1212
RemoteConnector
1313
} from '@powersync/common';
1414
import { BSON } from 'bson';
15+
import Agent from 'proxy-agent';
1516
import { ProxyAgent } from 'undici';
17+
import { WebSocket } from 'ws';
1618

1719
export const STREAMING_POST_TIMEOUT_MS = 30_000;
1820

@@ -23,6 +25,8 @@ class NodeFetchProvider extends FetchImplementationProvider {
2325
}
2426

2527
export 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 [

pnpm-lock.yaml

Lines changed: 14 additions & 30 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)