Skip to content

Commit 5a2bd21

Browse files
Handle typo in powersync_url env variable
1 parent aad9ba5 commit 5a2bd21

File tree

3 files changed

+21
-2
lines changed

3 files changed

+21
-2
lines changed

.changeset/fuzzy-bears-nail.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
'@powersync/common': patch
3+
'@powersync/web': patch
4+
---
5+
6+
Handle additional forward slash in the POWERSYNC_URL environment variable

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

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import { version as POWERSYNC_JS_VERSION } from '../../../../package.json';
1414

1515
export type BSONImplementation = typeof BSON;
1616

17+
const POWERSYNC_TRAILING_SLASH_MATCH =/\/+$/;
1718
export type RemoteConnector = {
1819
fetchCredentials: () => Promise<PowerSyncCredentials | null>;
1920
};
@@ -117,18 +118,23 @@ export abstract class AbstractRemote {
117118

118119
protected async buildRequest(path: string) {
119120
const credentials = await this.getCredentials();
121+
let endPoint = credentials?.endpoint;
120122
if (credentials != null && (credentials.endpoint == null || credentials.endpoint == '')) {
121123
throw new Error('PowerSync endpoint not configured');
122124
} else if (credentials?.token == null || credentials?.token == '') {
123125
const error: any = new Error(`Not signed in`);
124126
error.status = 401;
125127
throw error;
128+
} else if (credentials?.endpoint.match(POWERSYNC_TRAILING_SLASH_MATCH)) {
129+
this.logger.warn('A trailing forward slash "/" was entered after the POWERSYNC_URL environment variable. ' +
130+
'Remove the trailing "/" from the variable to get rid of this warning.');
131+
endPoint = credentials?.endpoint.replace(POWERSYNC_TRAILING_SLASH_MATCH, "");
126132
}
127133

128134
const userAgent = this.getUserAgent();
129135

130136
return {
131-
url: credentials.endpoint + path,
137+
url: endPoint + path,
132138
headers: {
133139
'content-type': 'application/json',
134140
Authorization: `Token ${credentials.token}`,

packages/web/src/db/sync/SharedWebStreamingSyncImplementation.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ import {
1313
} from './WebStreamingSyncImplementation';
1414
import { resolveWebSQLFlags } from '../adapters/web-sql-flags';
1515

16+
const POWERSYNC_TRAILING_SLASH_MATCH =/\/+$/;
17+
1618
/**
1719
* The shared worker will trigger methods on this side of the message port
1820
* via this client provider.
@@ -27,8 +29,13 @@ class SharedSyncClientProvider extends AbstractSharedSyncClientProvider {
2729

2830
async fetchCredentials(): Promise<PowerSyncCredentials | null> {
2931
const credentials = await this.options.remote.getCredentials();
32+
let endPoint = credentials?.endpoint;
3033
if (credentials == null) {
3134
return null;
35+
} else if (credentials?.endpoint.match(POWERSYNC_TRAILING_SLASH_MATCH)) {
36+
this.logger?.warn('A trailing forward slash "/" was entered after the POWERSYNC_URL environment variable. ' +
37+
'Remove the trailing "/" from the variable to get rid of this warning.');
38+
endPoint = credentials.endpoint.replace(POWERSYNC_TRAILING_SLASH_MATCH, "");
3239
}
3340
/**
3441
* The credentials need to be serializable.
@@ -37,7 +44,7 @@ class SharedSyncClientProvider extends AbstractSharedSyncClientProvider {
3744
* This returns only the essential fields.
3845
*/
3946
return {
40-
endpoint: credentials.endpoint,
47+
endpoint: endPoint || '',
4148
token: credentials.token,
4249
expiresAt: credentials.expiresAt
4350
};

0 commit comments

Comments
 (0)