Updated file_service

This commit is contained in:
thomashii 2021-06-26 20:06:30 +08:00
parent 16be2e8f94
commit 7287bf12c1
10 changed files with 67 additions and 64 deletions

View file

@ -1,21 +0,0 @@
import 'package:glob/glob.dart';
import 'package:glob/list_local_fs.dart';
void main() {
/*
var filePat = Glob('**.txt');
for (var entity in filePat.listSync()) {
print(entity.path);
}
var result = filePat.allMatches(path);
*/
var path = "ababa99.txt";
//var regPat = RegExp('\w+\.txt');
var regPat = RegExp('^/?\\w+\\.txt');
var result = regPat.allMatches(path);
print(result.length);
}

View file

@ -1,33 +1,50 @@
# 4.0.1 # Change Log
## 4.0.2
* Updated README
* Removed redundant code
## 4.0.1
* Updated package description * Updated package description
# 4.0.0 ## 4.0.0
* Migrated to support Dart SDK 2.12.x NNBD * Migrated to support Dart SDK 2.12.x NNBD
# 3.0.0 ## 3.0.0
* Migrated to work with Dart SDK 2.12.x Non NNBD * Migrated to work with Dart SDK 2.12.x Non NNBD
# 2.0.1 ## 2.0.1
* Pass everything through `_jsonifyToSD` when returning responses. * Pass everything through `_jsonifyToSD` when returning responses.
# 2.0.0 ## 2.0.0
* Dart/Angel 2 update. * Dart/Angel 2 update.
* Remove `package:dart2_constant` * Remove `package:dart2_constant`
* Update `package:file` to `^5.0.0`. * Update `package:file` to `^5.0.0`.
# 1.1.2 ## 1.1.2
* Added tests, because tests. * Added tests, because tests.
# 1.1.1 ## 1.1.1
* Dart 2 fixes. * Dart 2 fixes.
# 1.1.0+2 ## 1.1.0+2
* `create` now uses the underlying store, instead of manually patching * `create` now uses the underlying store, instead of manually patching
# 1.1.0+1 ## 1.1.0+1
* Analyzer nitpick for pana * Analyzer nitpick for pana
# 1.1.0 ## 1.1.0
* Updated to framework v1.1.x * Updated to framework v1.1.x
* Use `package:file` * Use `package:file`
* Allow a custom `store` * Allow a custom `store`

View file

@ -1,5 +1,6 @@
# angel3_file_service # File Service for Angel3
[![version](https://img.shields.io/badge/pub-v4.0.1-brightgreen)](https://pub.dartlang.org/packages/angel3_file_service)
[![version](https://img.shields.io/badge/pub-v4.0.2-brightgreen)](https://pub.dartlang.org/packages/angel3_file_service)
[![Null Safety](https://img.shields.io/badge/null-safety-brightgreen)](https://dart.dev/null-safety) [![Null Safety](https://img.shields.io/badge/null-safety-brightgreen)](https://dart.dev/null-safety)
[![Gitter](https://img.shields.io/gitter/room/angel_dart/discussion)](https://gitter.im/angel_dart/discussion) [![Gitter](https://img.shields.io/gitter/room/angel_dart/discussion)](https://gitter.im/angel_dart/discussion)
@ -20,7 +21,8 @@ if possible, or one with very low load.
While not necessarily *slow*, this package makes no promises about performance. While not necessarily *slow*, this package makes no promises about performance.
# Usage ## Usage
```dart ```dart
configureServer(Angel app) async { configureServer(Angel app) async {
// Just like a normal service // Just like a normal service
@ -31,4 +33,4 @@ configureServer(Angel app) async {
), ),
); );
} }
``` ```

View file

@ -2,10 +2,10 @@ import 'package:angel3_file_service/angel3_file_service.dart';
import 'package:angel3_framework/angel3_framework.dart'; import 'package:angel3_framework/angel3_framework.dart';
import 'package:file/local.dart'; import 'package:file/local.dart';
configureServer(Angel app) async { void configureServer(Angel app) async {
// Just like a normal service // Just like a normal service
app.use( app.use(
'/api/todos', '/api/todos',
new JsonFileService(const LocalFileSystem().file('todos_db.json')), JsonFileService(const LocalFileSystem().file('todos_db.json')),
); );
} }

View file

@ -7,14 +7,16 @@ import 'package:pool/pool.dart';
/// Persists in-memory changes to a file on disk. /// Persists in-memory changes to a file on disk.
class JsonFileService extends Service<String, Map<String, dynamic>> { class JsonFileService extends Service<String, Map<String, dynamic>> {
FileStat? _lastStat; FileStat? _lastStat;
final Pool _mutex = new Pool(1); final Pool _mutex = Pool(1);
late MapService _store; late MapService _store;
final File file; final File file;
JsonFileService(this.file, JsonFileService(this.file,
{bool allowRemoveAll: false, bool allowQuery: true, MapService? store}) { {bool allowRemoveAll = false,
bool allowQuery = true,
MapService? store}) {
_store = store ?? _store = store ??
new MapService( MapService(
allowRemoveAll: allowRemoveAll == true, allowRemoveAll: allowRemoveAll == true,
allowQuery: allowQuery != false); allowQuery: allowQuery != false);
} }
@ -45,7 +47,7 @@ class JsonFileService extends Service<String, Map<String, dynamic>> {
}); });
} }
_save() { Future<File> _save() {
return _mutex.withResource(() { return _mutex.withResource(() {
return file return file
.writeAsString(json.encode(_store.items.map(_jsonify).toList())); .writeAsString(json.encode(_store.items.map(_jsonify).toList()));
@ -105,17 +107,18 @@ class JsonFileService extends Service<String, Map<String, dynamic>> {
} }
} }
_safeForJson(x) { dynamic _safeForJson(x) {
if (x is DateTime) if (x is DateTime) {
return x.toIso8601String(); return x.toIso8601String();
else if (x is Map) } else if (x is Map) {
return _jsonify(x); return _jsonify(x);
else if (x is num || x is String || x is bool || x == null) } else if (x is num || x is String || x is bool || x == null) {
return x; return x;
else if (x is Iterable) } else if (x is Iterable) {
return x.map(_safeForJson).toList(); return x.map(_safeForJson).toList();
else } else {
return x.toString(); return x.toString();
}
} }
Map _jsonify(Map map) { Map _jsonify(Map map) {
@ -129,14 +132,15 @@ dynamic _revive(x) {
if (x is Map) { if (x is Map) {
return x.keys.fold<Map<String, dynamic>>( return x.keys.fold<Map<String, dynamic>>(
{}, (out, k) => out..[k.toString()] = _revive(x[k])); {}, (out, k) => out..[k.toString()] = _revive(x[k]));
} else if (x is Iterable) } else if (x is Iterable) {
return x.map(_revive).toList(); return x.map(_revive).toList();
else if (x is String) { } else if (x is String) {
try { try {
return DateTime.parse(x); return DateTime.parse(x);
} catch (e) { } catch (e) {
return x; return x;
} }
} else } else {
return x; return x;
}
} }

View file

@ -1,5 +1,5 @@
name: angel3_file_service name: angel3_file_service
version: 4.0.1 version: 4.0.2
description: Angel service that persists data to a file on disk, stored as a JSON list. description: Angel service that persists data to a file on disk, stored as a JSON list.
homepage: https://github.com/dukefirehawk/angel/tree/angel3/packages/file_service homepage: https://github.com/dukefirehawk/angel/tree/angel3/packages/file_service
environment: environment:

View file

@ -9,9 +9,9 @@ void main() {
late JsonFileService service; late JsonFileService service;
setUp(() async { setUp(() async {
fs = new MemoryFileSystem(); fs = MemoryFileSystem();
dbFile = fs.file('db.json'); dbFile = fs.file('db.json');
service = new JsonFileService(dbFile); service = JsonFileService(dbFile);
await dbFile.writeAsString(''' await dbFile.writeAsString('''
[ [
@ -26,9 +26,9 @@ void main() {
test('index no params', () async { test('index no params', () async {
expect(await service.index(), [ expect(await service.index(), [
{"id": "0", "foo": "bar"}, {'id': '0', 'foo': 'bar'},
{"id": "1", "foo": "baz"}, {'id': '1', 'foo': 'baz'},
{"id": "2", "foo": "quux"} {'id': '2', 'foo': 'quux'}
]); ]);
}); });
@ -38,7 +38,7 @@ void main() {
'query': {'foo': 'bar'} 'query': {'foo': 'bar'}
}), }),
[ [
{"id": "0", "foo": "bar"} {'id': '0', 'foo': 'bar'}
], ],
); );
}); });
@ -46,7 +46,7 @@ void main() {
test('read', () async { test('read', () async {
expect( expect(
await service.read('2'), await service.read('2'),
{"id": "2", "foo": "quux"}, {'id': '2', 'foo': 'quux'},
); );
}); });
@ -64,8 +64,8 @@ void main() {
test('delete', () async { test('delete', () async {
await service.remove('2'); await service.remove('2');
expect(await service.index(), [ expect(await service.index(), [
{"id": "0", "foo": "bar"}, {'id': '0', 'foo': 'bar'},
{"id": "1", "foo": "baz"} {'id': '1', 'foo': 'baz'}
]); ]);
}); });
} }

View file

@ -6,4 +6,5 @@
## 2.0.0 ## 2.0.0
* Dart2 + Angel2 update. * Dart2 + Angel2 update.

View file

@ -16,4 +16,4 @@
## 2.0.0-alpha ## 2.0.0-alpha
* Angel 2 updates. Remove previous functionality. * Angel 2 updates. Remove previous functionality.
* Add `CookieSigner`, `RateLimiter`/`InMemoryRateLimiter`/`ServiceRateLimiter`. * Add `CookieSigner`, `RateLimiter`/`InMemoryRateLimiter`/`ServiceRateLimiter`

View file

@ -9,4 +9,4 @@
Angel3 middleware designed to enhance application security by patching common Web security Angel3 middleware designed to enhance application security by patching common Web security
holes. holes.
**This package is currently going through a major overhaul, for version 2.** **This package is currently going through a major overhaul, for version 2.**