Skip to content

Commit

Permalink
update to f78d3cb5fc9194062a8d5f6f058d2dd37d2b9c3a
Browse files Browse the repository at this point in the history
  • Loading branch information
davidmartos96 committed Jul 28, 2023
1 parent bfb3687 commit 1063f9d
Show file tree
Hide file tree
Showing 4 changed files with 104 additions and 17 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

# Electric Dart ⚡🎯

Dart client implementation for [Electric](https://electric-sql.com/) based on commit `dc48f1fe788a1390448b510c7222c4b4bc1be2d5` of the [electric git repository](https://github.com/electric-sql/electric)
Dart client implementation for [Electric](https://electric-sql.com/) based on commit `f78d3cb5fc9194062a8d5f6f058d2dd37d2b9c3a` of the [electric git repository](https://github.com/electric-sql/electric)

- Client based on the typescript client from the `clients/typescript` subfolder.

Expand Down
35 changes: 20 additions & 15 deletions lib/src/config/config.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,19 @@ typedef EnvName = String;
class ElectricConfig {
final AuthConfig auth;

/// Optional path to the Electric sync service.
/// Optional URL string to connect to the Electric sync service.
///
/// Should have the following format:
/// `electric://<host>:<port>`
/// `protocol://<host>:<port>[?ssl=true]`
///
/// If the protocol is `https` or `wss` then `ssl`
/// defaults to true. Otherwise it defaults to false.
///
/// If port is not provided, defaults to 443 when
/// ssl is enabled or 80 when it isn't.
///
/// Defaults to:
/// `electric://127.0.0.1:5133`
/// `http://127.0.0.1:5133`
final String? url;

/// Optional flag to activate debug mode
Expand Down Expand Up @@ -78,21 +86,18 @@ HydratedConfig hydrateConfig(ElectricConfig config) {

final debug = config.debug ?? false;

final url = config.url ?? 'electric://127.0.0.1:5133';
final match = RegExp(r'(?:electric:\/\/)(.+):([0-9]*)').firstMatch(url);
if (match == null) {
throw Exception(
"Invalid Electric URL. Must be of the form: 'electric://<host>:<port>'",
);
}
final url = Uri.parse(config.url ?? 'http://127.0.0.1:5133');

final host = match.group(1)!;
final portStr = match.group(2)!;
final isSecureProtocol = url.scheme == 'https' || url.scheme == 'wss';
final sslEnabled = isSecureProtocol || url.queryParameters['ssl'] == 'true';

final defaultPort = sslEnabled ? 443 : 80;
final port = url.hasPort ? url.port : defaultPort;

final replication = ReplicationConfig(
host: host,
port: int.parse(portStr),
ssl: false,
host: url.host,
port: port,
ssl: sslEnabled,
);

return HydratedConfig(
Expand Down
2 changes: 1 addition & 1 deletion lib/src/satellite/process.dart
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ class SatelliteProcess implements Satellite {
String? _potentialDataChangeSubscription;
String? _connectivityChangeSubscription;

late Throttle<DateTime> throttledSnapshot;
late final Throttle<DateTime> throttledSnapshot;

int _lastAckdRowId = 0;
@visibleForTesting
Expand Down
82 changes: 82 additions & 0 deletions test/config/config_test.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
import 'package:electric_client/src/auth/auth.dart';
import 'package:electric_client/src/config/config.dart';
import 'package:test/test.dart';

void main() {
test('hydrateConfig adds expected defaults', () {
final hydrated = hydrateConfig(
ElectricConfig(
auth: const AuthConfig(
token: 'test-token',
),
),
);

expect(hydrated.replication.host, '127.0.0.1');
expect(hydrated.replication.port, 5133);
expect(hydrated.replication.ssl, false);

expect(hydrated.auth.token, 'test-token');

expect(hydrated.debug, false);
});

test('custom config', () {
final hydrated = hydrateConfig(
ElectricConfig(
auth: const AuthConfig(
token: 'test-token-2',
),
url: 'https://192.169.2.10',
debug: true,
),
);

expect(hydrated.replication.host, '192.169.2.10');
expect(hydrated.replication.port, 443);
expect(hydrated.replication.ssl, true);

expect(hydrated.auth.token, 'test-token-2');

expect(hydrated.debug, true);
});

test('port inference', () {
final httpConfig = hydrateConfig(
ElectricConfig(
auth: const AuthConfig(
token: 'test-token-3',
),
url: 'http://1.1.1.1',
),
);

expect(httpConfig.replication.port, 80);
expect(httpConfig.replication.ssl, false);

final httpsConfig = hydrateConfig(
ElectricConfig(
auth: const AuthConfig(
token: 'test-token-3',
),
url: 'https://1.1.1.1',
),
);

expect(httpsConfig.replication.port, 443);
expect(httpsConfig.replication.ssl, true);
});

test('ssl', () {
final httpsConfig = hydrateConfig(
ElectricConfig(
auth: const AuthConfig(
token: 'test-token-3',
),
url: '1.1.1.1?ssl=true',
),
);

expect(httpsConfig.replication.ssl, true);
});
}

0 comments on commit 1063f9d

Please sign in to comment.