Skip to content

Commit 9fa7d9d

Browse files
committed
Use X-User-Agent on web.
1 parent 7e81810 commit 9fa7d9d

File tree

5 files changed

+34
-17
lines changed

5 files changed

+34
-17
lines changed

packages/powersync/lib/src/streaming_sync.dart

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,8 @@ class StreamingSyncImplementation {
4949

5050
final Mutex syncMutex, crudMutex;
5151

52-
late final String? userAgent;
52+
final Map<String, String> _userAgentHeaders;
53+
5354
String? clientId;
5455

5556
StreamingSyncImplementation(
@@ -66,10 +67,10 @@ class StreamingSyncImplementation {
6667
/// A good value is typically the DB file path which it will mutate when syncing.
6768
String? identifier = "unknown"})
6869
: syncMutex = Mutex(identifier: "sync-$identifier"),
69-
crudMutex = Mutex(identifier: "crud-$identifier") {
70+
crudMutex = Mutex(identifier: "crud-$identifier"),
71+
_userAgentHeaders = userAgentHeaders() {
7072
_client = client;
7173
statusStream = _statusStreamController.stream;
72-
userAgent = powerSyncUserAgent();
7374
}
7475

7576
/// Close any active streams.
@@ -204,11 +205,9 @@ class StreamingSyncImplementation {
204205
Map<String, String> headers = {
205206
'Content-Type': 'application/json',
206207
'Authorization': "Token ${credentials.token}",
207-
'Client-ID': clientId!
208+
'Client-ID': clientId!,
209+
..._userAgentHeaders
208210
};
209-
if (userAgent != null) {
210-
headers['User-Agent'] = userAgent!;
211-
}
212211

213212
final response = await _client.get(uri, headers: headers);
214213
if (response.statusCode == 401) {
@@ -417,9 +416,8 @@ class StreamingSyncImplementation {
417416
request.headers['Content-Type'] = 'application/json';
418417
request.headers['Authorization'] = "Token ${credentials.token}";
419418
request.headers['Client-ID'] = clientId!;
420-
if (userAgent != null) {
421-
request.headers['User-Agent'] = userAgent!;
422-
}
419+
request.headers.addAll(_userAgentHeaders);
420+
423421
request.body = convert.jsonEncode(data);
424422

425423
http.StreamedResponse res;
Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
1-
// For native, expose a user agent.
2-
// For web, we're not able to override the user agent anyway.
3-
41
export './user_agent_stub.dart'
52
// ignore: uri_does_not_exist
6-
if (dart.library.io) './user_agent_native.dart';
3+
if (dart.library.io) './user_agent_native.dart'
4+
// ignore: uri_does_not_exist
5+
if (dart.library.html) './user_agent_web.dart';

packages/powersync/lib/src/user_agent/user_agent_native.dart

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import 'dart:io';
22

33
import 'package:powersync/src/version.dart';
44

5-
String? powerSyncUserAgent() {
5+
String powerSyncUserAgent() {
66
var dartVersion = RegExp(r'[\w.]+').stringMatch(Platform.version);
77
var dart = 'Dart';
88
if (dartVersion != null) {
@@ -12,3 +12,7 @@ String? powerSyncUserAgent() {
1212
// Platform.operatingSystemVersion is very verbose.
1313
return 'powersync-dart/$libraryVersion ($dart; ${Platform.operatingSystem})';
1414
}
15+
16+
Map<String, String> userAgentHeaders() {
17+
return {'User-Agent': powerSyncUserAgent()};
18+
}
Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1-
String? powerSyncUserAgent() {
2-
return null;
1+
String powerSyncUserAgent() {
2+
return '';
3+
}
4+
5+
Map<String, String> userAgentHeaders() {
6+
return {};
37
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import 'package:powersync/src/version.dart';
2+
3+
String powerSyncUserAgent() {
4+
return 'powersync-dart/$libraryVersion (flutter-web)';
5+
}
6+
7+
Map<String, String> userAgentHeaders() {
8+
var ua = powerSyncUserAgent();
9+
// Chrome doesn't allow overriding the user agent.
10+
// Instead, we send an additional x-user-agent header.
11+
return {'X-User-Agent': ua};
12+
}

0 commit comments

Comments
 (0)