Skip to content

Commit

Permalink
add web impl, separate memory impl
Browse files Browse the repository at this point in the history
  • Loading branch information
dnys1 committed Mar 7, 2024
1 parent 8dabb71 commit 58c8d3f
Show file tree
Hide file tree
Showing 5 changed files with 78 additions and 32 deletions.
20 changes: 20 additions & 0 deletions .github/workflows/celest_core.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -151,3 +151,23 @@ jobs:
# - name: Test (Windows)
# working-directory: packages/celest_core/example
# run: flutter test -d windows integration_test/secure_storage_test.dart
test_web:
needs: analyze_and_format
runs-on: ubuntu-latest
timeout-minutes: 15
steps:
- name: Git Checkout
uses: actions/checkout@b4ffde65f46336ab88eb53be808477a3936bae11 # 4.1.1
- name: Setup Flutter
uses: subosito/flutter-action@62f096cacda5168a3bd7b95793373be14fa4fbaf # 2.13.0
with:
cache: true
- name: Get Packages
working-directory: packages/celest_core
run: dart pub get
- name: Test (Chrome, dart2js)
working-directory: packages/celest_core
run: dart test -p chrome
- name: Test (Chrome, dart2wasm)
working-directory: packages/celest_core
run: dart test -p chrome -c dart2wasm
22 changes: 22 additions & 0 deletions packages/celest_core/lib/src/secure_storage/secure_storage.dart
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,25 @@ abstract interface class SecureStorage {
String? delete(String key);
void clear();
}

/// An in-memory implementation of [SecureStorage].
final class MemorySecureStorage implements SecureStorage {
MemorySecureStorage({
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;
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,15 +1,42 @@
import 'package:celest_core/src/secure_storage/secure_storage.dart';
import 'package:celest_core/src/secure_storage/secure_storage.stub.dart';
import 'package:meta/meta.dart';
import 'package:web/web.dart' as web;

abstract base class SecureStoragePlatform implements SecureStorage {
factory SecureStoragePlatform({
final class SecureStoragePlatform implements SecureStorage {
SecureStoragePlatform({
String? scope,
}) =>
SecureStoragePlatformStub(scope: scope ?? _defaultScope);
}) : scope = scope ?? _defaultScope;

final String scope;

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

@protected
const SecureStoragePlatform.base();
List<String> get _keys => [
for (var i = 0; i < web.window.localStorage.length; i++)
web.window.localStorage.key(i)!,
];

@override
void clear() {
for (final key in _keys) {
if (key.startsWith('$scope/')) {
web.window.localStorage.removeItem(key);
}
}
}

@override
String? delete(String key) {
final value = read(key);
web.window.localStorage.removeItem('$scope/$key');
return value;
}

@override
String? read(String key) => web.window.localStorage.getItem('$scope/$key');

@override
String write(String key, String value) {
web.window.localStorage.setItem('$scope/$key', value);
return value;
}
}
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

0 comments on commit 58c8d3f

Please sign in to comment.