Skip to content

Commit 57c4ab1

Browse files
wip: proxy agents
1 parent 81045a5 commit 57c4ab1

File tree

5 files changed

+71
-46
lines changed

5 files changed

+71
-46
lines changed

demos/example-node/src/main.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
import repl_factory from 'node:repl';
21
import { once } from 'node:events';
2+
import repl_factory from 'node:repl';
33

44
import { PowerSyncDatabase, SyncStreamConnectionMethod } from '@powersync/node';
55
import { default as Logger } from 'js-logger';
6-
import { AppSchema, DemoConnector } from './powersync.js';
76
import { exit } from 'node:process';
7+
import { AppSchema, DemoConnector } from './powersync.js';
88

99
const main = async () => {
1010
const logger = Logger.get('PowerSyncDemo');

packages/common/src/client/sync/stream/AbstractRemote.ts

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -85,14 +85,18 @@ export type AbstractRemoteOptions = {
8585
* Binding should be done before passing here.
8686
*/
8787
fetchImplementation: FetchImplementation | FetchImplementationProvider;
88+
89+
// TODO
90+
fetchOptions?: {};
8891
};
8992

9093
export const DEFAULT_REMOTE_OPTIONS: AbstractRemoteOptions = {
9194
socketUrlTransformer: (url) =>
9295
url.replace(/^https?:\/\//, function (match) {
9396
return match === 'https://' ? 'wss://' : 'ws://';
9497
}),
95-
fetchImplementation: new FetchImplementationProvider()
98+
fetchImplementation: new FetchImplementationProvider(),
99+
fetchOptions: {}
96100
};
97101

98102
export abstract class AbstractRemote {
@@ -231,6 +235,10 @@ export abstract class AbstractRemote {
231235
*/
232236
abstract getBSON(): Promise<BSONImplementation>;
233237

238+
protected createSocket(url: string): WebSocket {
239+
return new WebSocket(url);
240+
}
241+
234242
/**
235243
* Connects to the sync/stream websocket endpoint
236244
*/
@@ -249,7 +257,8 @@ export abstract class AbstractRemote {
249257

250258
const connector = new RSocketConnector({
251259
transport: new WebsocketClientTransport({
252-
url: this.options.socketUrlTransformer(request.url)
260+
url: this.options.socketUrlTransformer(request.url),
261+
wsCreator: (url) => this.createSocket(url)
253262
}),
254263
setup: {
255264
keepAlive: KEEP_ALIVE_MS,
@@ -421,6 +430,7 @@ export abstract class AbstractRemote {
421430
body: JSON.stringify(data),
422431
signal: controller.signal,
423432
cache: 'no-store',
433+
...(this.options.fetchOptions ?? {}),
424434
...options.fetchOptions
425435
}).catch((ex) => {
426436
if (ex.name == 'AbortError') {

packages/node/package.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,12 +51,13 @@
5151
"@powersync/common": "workspace:*",
5252
"async-lock": "^1.4.0",
5353
"bson": "^6.6.0",
54-
"comlink": "^4.4.2"
54+
"comlink": "^4.4.2",
55+
"undici": "^7.8.0"
5556
},
5657
"devDependencies": {
58+
"@powersync/drizzle-driver": "workspace:*",
5759
"@types/async-lock": "^1.4.0",
5860
"drizzle-orm": "^0.35.2",
59-
"@powersync/drizzle-driver": "workspace:*",
6061
"rollup": "4.14.3",
6162
"typescript": "^5.5.3",
6263
"vitest": "^3.0.5"

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

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import {
1212
RemoteConnector
1313
} from '@powersync/common';
1414
import { BSON } from 'bson';
15+
import { ProxyAgent } from 'undici';
1516

1617
export const STREAMING_POST_TIMEOUT_MS = 30_000;
1718

@@ -27,12 +28,27 @@ export class NodeRemote extends AbstractRemote {
2728
protected logger: ILogger = DEFAULT_REMOTE_LOGGER,
2829
options?: Partial<AbstractRemoteOptions>
2930
) {
31+
// Automatic env vars are not supported by undici
32+
// proxy-agent does not work directly with dispatcher
33+
const proxy = process.env.HTTPS_PROXY ?? process.env.HTTP_PROXY;
3034
super(connector, logger, {
3135
...(options ?? {}),
32-
fetchImplementation: options?.fetchImplementation ?? new NodeFetchProvider()
36+
fetchImplementation: options?.fetchImplementation ?? new NodeFetchProvider(),
37+
fetchOptions: {
38+
dispatcher: proxy ? new ProxyAgent(proxy) : undefined
39+
}
3340
});
3441
}
3542

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+
3652
getUserAgent(): string {
3753
return [
3854
super.getUserAgent(),

pnpm-lock.yaml

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

0 commit comments

Comments
 (0)