Skip to content

Commit

Permalink
Configure user agent.
Browse files Browse the repository at this point in the history
  • Loading branch information
rkistner committed Sep 2, 2024
1 parent 8bc5391 commit 5968662
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 3 deletions.
19 changes: 17 additions & 2 deletions packages/common/src/client/sync/stream/AbstractRemote.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ import type { BSON } from 'bson';
import { AbortOperation } from '../../../utils/AbortOperation';
import { Buffer } from 'buffer';

import { version as POWERSYNC_JS_VERSION } from '../../../../package.json';

export type BSONImplementation = typeof BSON;

export type RemoteConnector = {
Expand Down Expand Up @@ -109,6 +111,10 @@ export abstract class AbstractRemote {
return this.credentials;
}

getUserAgent() {
return `powersync-js/${POWERSYNC_JS_VERSION}`;
}

protected async buildRequest(path: string) {
const credentials = await this.getCredentials();
if (credentials != null && (credentials.endpoint == null || credentials.endpoint == '')) {
Expand All @@ -119,11 +125,14 @@ export abstract class AbstractRemote {
throw error;
}

const userAgent = this.getUserAgent();

return {
url: credentials.endpoint + path,
headers: {
'content-type': 'application/json',
Authorization: `Token ${credentials.token}`
Authorization: `Token ${credentials.token}`,
'x-user-agent': userAgent
}
};
}
Expand Down Expand Up @@ -207,6 +216,11 @@ export abstract class AbstractRemote {

const bson = await this.getBSON();

// Add the user agent in the setup payload - we can't set custom
// headers with websockets. The browser userAgent is however added
// automatically as a header.
const userAgent = this.getUserAgent();

const connector = new RSocketConnector({
transport: new WebsocketClientTransport({
url: this.options.socketUrlTransformer(request.url)
Expand All @@ -220,7 +234,8 @@ export abstract class AbstractRemote {
data: null,
metadata: Buffer.from(
bson.serialize({
token: request.headers.Authorization
token: request.headers.Authorization,
user_agent: userAgent
})
)
}
Expand Down
6 changes: 6 additions & 0 deletions packages/react-native/src/sync/stream/ReactNativeRemote.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ import { BSON } from 'bson';

import { fetch } from 'react-native-fetch-api';

import { version as POWERSYNC_RN_VERSION } from '../../../package.json';

export const STREAMING_POST_TIMEOUT_MS = 30_000;

/**
Expand All @@ -43,6 +45,10 @@ export class ReactNativeRemote extends AbstractRemote {
});
}

getUserAgent(): string {
return `${super.getUserAgent()} (react-native/${POWERSYNC_RN_VERSION})`;
}

async getBSON(): Promise<BSONImplementation> {
return BSON;
}
Expand Down
6 changes: 6 additions & 0 deletions packages/web/src/db/sync/WebRemote.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ import {
RemoteConnector
} from '@powersync/common';

import { version as POWERSYNC_WEB_VERSION } from '../../../package.json';

/*
* Depends on browser's implementation of global fetch.
*/
Expand All @@ -33,6 +35,10 @@ export class WebRemote extends AbstractRemote {
});
}

getUserAgent(): string {
return `${super.getUserAgent()} (web/${POWERSYNC_WEB_VERSION})`;
}

async getBSON(): Promise<BSONImplementation> {
if (this._bson) {
return this._bson;
Expand Down
2 changes: 1 addition & 1 deletion packages/web/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,5 @@
"path": "../common"
}
],
"include": ["src/**/*", "tests/**/*"]
"include": ["src/**/*", "tests/**/*", "package.json"]
}

0 comments on commit 5968662

Please sign in to comment.