Skip to content

Commit

Permalink
chore(core): Split storage interfaces
Browse files Browse the repository at this point in the history
  • Loading branch information
dnys1 committed Mar 7, 2024
1 parent eeae84b commit ff13fb4
Show file tree
Hide file tree
Showing 21 changed files with 146 additions and 64 deletions.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import 'dart:math';

import 'package:celest_core/src/secure_storage/secure_storage.dart';
import 'package:celest_core/src/storage/storage.dart';
import 'package:test/test.dart';

void sharedTests() {
void sharedTests(String name, Storage Function({String? scope}) factory) {
group('SecureStorage', () {
late String key;
final storage = SecureStorage(scope: 'test');
final storage = factory(scope: 'test');

setUp(() {
storage.clear();
Expand Down
11 changes: 11 additions & 0 deletions packages/celest_core/example/integration_test/storage_test.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import 'package:celest_core/src/storage/local/local_storage.dart';
import 'package:celest_core/src/storage/secure/secure_storage.dart';
import 'package:integration_test/integration_test.dart';

import 'storage_shared.dart';

void main() {
IntegrationTestWidgetsFlutterBinding.ensureInitialized();
sharedTests('SecureStorage', SecureStorage.new);
sharedTests('LocalStorage', LocalStorage.new);
}
33 changes: 0 additions & 33 deletions packages/celest_core/lib/src/secure_storage/secure_storage.dart

This file was deleted.

7 changes: 7 additions & 0 deletions packages/celest_core/lib/src/storage/local/local_storage.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import 'package:celest_core/src/storage/local/local_storage_platform.vm.dart'
if (dart.library.js_interop) 'package:celest_core/src/storage/local/local_storage_platform.web.dart';
import 'package:celest_core/src/storage/storage.dart';

abstract interface class LocalStorage implements Storage {
factory LocalStorage({String? scope}) = LocalStoragePlatform;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import 'package:celest_core/src/storage/local/local_storage.dart';
import 'package:celest_core/src/storage/secure/secure_storage_platform.vm.dart';

extension type LocalStoragePlatform._(LocalStorage _impl)
implements LocalStorage {
LocalStoragePlatform({String? scope})
: _impl = SecureStoragePlatform(scope: scope ?? _defaultScope);

static const _defaultScope = 'dev.celest.celest';
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import 'package:celest_core/src/storage/local/local_storage.dart';
import 'package:web/web.dart' as web;

final class LocalStoragePlatform implements LocalStorage {
LocalStoragePlatform({String? scope}) : scope = scope ?? _defaultScope;

static const _defaultScope = 'dev.celest.celest';

final String scope;
final web.Storage _storage = web.window.localStorage;

@override
void clear() {
for (final key in _storage.keys) {
if (key.startsWith('$scope/')) {
_storage.removeItem(key);
}
}
}

@override
String? delete(String key) {
final value = read(key);
if (value != null) {
_storage.removeItem('$scope/$key');
}
return null;
}

@override
String? read(String key) => _storage['$scope/$key'];

@override
String write(String key, String value) {
_storage.setItem('$scope/$key', value);
return value;
}
}

extension on web.Storage {
List<String> get keys => [for (var i = 0; i < length; i++) key(i)!];
}
24 changes: 24 additions & 0 deletions packages/celest_core/lib/src/storage/memory_storage.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import 'package:celest_core/src/storage/secure/secure_storage.dart';
import 'package:celest_core/src/storage/storage.dart';

/// An in-memory implementation of [Storage] and [SecureStorage].
final class MemoryStorage implements Storage, SecureStorage {
MemoryStorage({
required this.scope,
});

final _storage = <String, String>{};
final String scope;

@override
void clear() => _storage.removeWhere((key, _) => key.startsWith('$scope/'));

@override
String? delete(String key) => _storage.remove('$scope/$key');

@override
String? read(String key) => _storage['$scope/$key'];

@override
String write(String key, String value) => _storage['$scope/$key'] = value;
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import 'package:celest_core/src/native/android/jni_bindings.ffi.dart';
import 'package:celest_core/src/secure_storage/secure_storage_platform.vm.dart';
import 'package:celest_core/src/storage/secure/secure_storage_platform.vm.dart';
import 'package:jni/jni.dart';

final class SecureStoragePlatformAndroid extends SecureStoragePlatform {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import 'package:celest_core/src/storage/secure/secure_storage_platform.vm.dart'
if (dart.library.js_interop) 'package:celest_core/src/storage/secure/secure_storage_platform.web.dart';
import 'package:celest_core/src/storage/storage.dart';

abstract interface class SecureStorage implements Storage {
factory SecureStorage({String? scope}) = SecureStoragePlatform;
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ import 'dart:io';
import 'package:celest_core/src/native/darwin/core_foundation.ffi.dart';
import 'package:celest_core/src/native/darwin/darwin_ffi_helpers.dart';
import 'package:celest_core/src/native/darwin/security.ffi.dart';
import 'package:celest_core/src/secure_storage/secure_storage_exception.dart';
import 'package:celest_core/src/secure_storage/secure_storage_platform.vm.dart';
import 'package:celest_core/src/storage/secure/secure_storage_exception.dart';
import 'package:celest_core/src/storage/secure/secure_storage_platform.vm.dart';
import 'package:celest_core/src/util/globals.dart';
import 'package:ffi/ffi.dart';

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ import 'dart:io';

import 'package:celest_core/src/native/linux/glib.ffi.dart';
import 'package:celest_core/src/native/linux/libsecret.ffi.dart';
import 'package:celest_core/src/secure_storage/secure_storage_exception.dart';
import 'package:celest_core/src/secure_storage/secure_storage_platform.vm.dart';
import 'package:celest_core/src/storage/secure/secure_storage_exception.dart';
import 'package:celest_core/src/storage/secure/secure_storage_platform.vm.dart';
import 'package:ffi/ffi.dart';

final class SecureStoragePlatformLinux extends SecureStoragePlatform {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@ import 'dart:io';
import 'dart:typed_data';

import 'package:celest_core/src/native/windows/windows_paths.dart';
import 'package:celest_core/src/secure_storage/secure_storage_exception.dart';
import 'package:celest_core/src/secure_storage/secure_storage_platform.vm.dart';
import 'package:path/path.dart' as p;
import 'package:celest_core/src/storage/secure/secure_storage_exception.dart';
import 'package:celest_core/src/storage/secure/secure_storage_platform.vm.dart';
import 'package:ffi/ffi.dart';
import 'package:path/path.dart' as p;
import 'package:win32/win32.dart';

final class SecureStoragePlatformWindows extends SecureStoragePlatform {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
import 'dart:io';

import 'package:celest_core/src/secure_storage/secure_storage.android.dart';
import 'package:celest_core/src/secure_storage/secure_storage.dart';
import 'package:celest_core/src/secure_storage/secure_storage.darwin.dart';
import 'package:celest_core/src/secure_storage/secure_storage.linux.dart';
import 'package:celest_core/src/secure_storage/secure_storage.windows.dart';
import 'package:celest_core/src/storage/local/local_storage.dart';
import 'package:celest_core/src/storage/secure/secure_storage.android.dart';
import 'package:celest_core/src/storage/secure/secure_storage.dart';
import 'package:celest_core/src/storage/secure/secure_storage.darwin.dart';
import 'package:celest_core/src/storage/secure/secure_storage.linux.dart';
import 'package:celest_core/src/storage/secure/secure_storage.windows.dart';
import 'package:meta/meta.dart';

abstract base class SecureStoragePlatform implements SecureStorage {
abstract base class SecureStoragePlatform
implements SecureStorage, LocalStorage {
factory SecureStoragePlatform({
String? scope,
}) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import 'package:celest_core/src/secure_storage/secure_storage.dart';
import 'package:celest_core/src/storage/memory_storage.dart';
import 'package:celest_core/src/storage/secure/secure_storage.dart';

extension type SecureStoragePlatform._(SecureStorage _impl)
implements SecureStorage {
SecureStoragePlatform({String? scope})
: _impl = MemorySecureStorage(scope: scope ?? _defaultScope);
: _impl = MemoryStorage(scope: scope ?? _defaultScope);

static const _defaultScope = 'dev.celest.celest';
}
12 changes: 12 additions & 0 deletions packages/celest_core/lib/src/storage/storage.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import 'package:celest_core/src/storage/local/local_storage.dart';
import 'package:celest_core/src/storage/secure/secure_storage.dart';

abstract interface class Storage {
factory Storage.local({String? scope}) = LocalStorage;
factory Storage.secure({String? scope}) = SecureStorage;

String? read(String key);
String write(String key, String value);
String? delete(String key);
void clear();
}
1 change: 1 addition & 0 deletions packages/celest_core/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ dependencies:
meta: ^1.10.0
os_detect: ^2.0.1
path: ^1.9.0
web: ^0.5.1
win32: ^5.2.0

dev_dependencies:
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
@TestOn('mac-os')

import 'package:celest_core/src/native/darwin/security.ffi.dart';
import 'package:celest_core/src/secure_storage/secure_storage.darwin.dart';
import 'package:celest_core/src/storage/secure/secure_storage.darwin.dart';
import 'package:test/test.dart';

void main() {
Expand Down
9 changes: 9 additions & 0 deletions packages/celest_core/test/storage/storage_test.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import 'package:celest_core/src/storage/local/local_storage.dart';
import 'package:celest_core/src/storage/secure/secure_storage.dart';

import '../../example/integration_test/storage_shared.dart';

void main() {
sharedTests('SecureStorage', SecureStorage.new);
sharedTests('LocalStorage', LocalStorage.new);
}

0 comments on commit ff13fb4

Please sign in to comment.