Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix/cache #1202

Merged
merged 2 commits into from
Mar 8, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion kraken/lib/src/devtools/module.dart
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ abstract class UIInspectorModule extends _InspectorModule {

@override
void sendEventToFrontend(InspectorEvent event) {
devtoolsService.isolateServerPort!.send(event);
devtoolsService.isolateServerPort?.send(event);
}
}

Expand Down
13 changes: 11 additions & 2 deletions kraken/lib/src/foundation/http_cache.dart
Original file line number Diff line number Diff line change
Expand Up @@ -102,12 +102,17 @@ class HttpCacheController {
// Add or update the httpCacheObject to memory cache.
void putObject(Uri uri, HttpCacheObject cacheObject) {
if (_caches.length == _maxCachedObjects) {
_caches.remove(_caches.lastKey());
_caches.remove(_caches.lastKey());
}
final String key = _getCacheKey(uri);
_caches.update(key, (value) => cacheObject, ifAbsent: () => cacheObject);
}

void removeObject(Uri uri) {
final String key = _getCacheKey(uri);
_caches.remove(key);
}

Future<HttpClientResponse> interceptResponse(
HttpClientRequest request,
HttpClientResponse response,
Expand All @@ -132,7 +137,11 @@ class HttpCacheController {
);

// Cache the object.
putObject(request.uri, cacheObject);
if (cacheObject.valid) {
putObject(request.uri, cacheObject);
} else {
removeObject(request.uri);
}

return HttpClientCachedResponse(response, cacheObject);
}
Expand Down
16 changes: 12 additions & 4 deletions kraken/lib/src/foundation/http_cache_object.dart
Original file line number Diff line number Diff line change
Expand Up @@ -146,13 +146,17 @@ class HttpCacheObject {

/// Read the index file.
Future<void> read() async {
if (_valid) return;
final bool isIndexFileExist = await _file.exists();
if (!isIndexFileExist) {
// Index file not exist, dispose.
// Make sure file exists, or causing io exception.
if (!await _file.exists() || !await _blob.exists()) {
_valid = false;
return;
}

// If index read before, ignoring to read again.
// Note: if index or blob file were being changed, this will make chaos,
// usually this is an abnormal operation.
if (_valid) return;

try {
Uint8List bytes = await _file.readAsBytes();
ByteData byteData = bytes.buffer.asByteData();
Expand Down Expand Up @@ -286,6 +290,10 @@ class HttpCacheObject {
// Store shorted response headers, 4B.
writeString(bytesBuilder, headers ?? '', 4);

// In case of cache object file is deleted by accident.
if (!await _file.exists()) {
await _file.create(recursive: true);
}
// The index file will not be TOO LARGE,
// so take bytes at one time.
await _file.writeAsBytes(bytesBuilder.takeBytes(), flush: true);
Expand Down
2 changes: 1 addition & 1 deletion kraken/lib/src/foundation/http_client_request.dart
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ class ProxyHttpClientRequest extends HttpClientRequest {
}

// Step 3: Handle negotiate cache request header.
if (headers.ifModifiedSince == null && headers.value(HttpHeaders.ifNoneMatchHeader) == null) {
if (cacheObject.valid && headers.ifModifiedSince == null && headers.value(HttpHeaders.ifNoneMatchHeader) == null) {
// ETag has higher priority of lastModified.
if (cacheObject.eTag != null) {
headers.set(HttpHeaders.ifNoneMatchHeader, cacheObject.eTag!);
Expand Down
4 changes: 2 additions & 2 deletions kraken/lib/src/launcher/bundle.dart
Original file line number Diff line number Diff line change
Expand Up @@ -196,12 +196,12 @@ String _resolveStringFromData(ByteData data) {
class NetworkAssetBundle extends AssetBundle {
/// Creates an network asset bundle that resolves asset keys as URLs relative
/// to the given base URL.
NetworkAssetBundle(Uri baseUrl, {int? contextId, Map<String, String>? additionalHttpHeaders })
NetworkAssetBundle(Uri baseUrl, {this.contextId, Map<String, String>? additionalHttpHeaders })
: _baseUrl = baseUrl,
_additionalHttpHeaders = additionalHttpHeaders,
httpClient = HttpClient();

int? contextId;
final int? contextId;
final Uri _baseUrl;
final HttpClient httpClient;
final Map<String, String>? _additionalHttpHeaders;
Expand Down
6 changes: 6 additions & 0 deletions kraken/test/kraken_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ import 'src/css/values.dart' as css_values;

import 'src/gesture/scroll_physics.dart' as scroll_physics;

import 'src/launcher/bundle.dart' as bundle;

// The main entry for kraken unit test.
// Setup all common logic.
void main() {
Expand Down Expand Up @@ -64,6 +66,10 @@ void main() {
scroll_physics.main();
});

group('launcher', () {
bundle.main();
});

tearDownAll(() {
if (tempDirectory.existsSync()) {
tempDirectory.deleteSync(recursive: true);
Expand Down
23 changes: 23 additions & 0 deletions kraken/test/src/launcher/bundle.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import 'dart:convert';
import 'dart:typed_data';

import 'package:kraken/launcher.dart';
import 'package:test/test.dart';

import '../../local_http_server.dart';

void main() {
var server = LocalHttpServer.getInstance();

group('Bundle', () {
test('NetworkAssetsBundle basic', () async {
Uri uri = server.getUri('js_over_128k');
// Using contextId to active cache.
var bundle = NetworkAssetBundle(uri, contextId: 1);
ByteData data = await bundle.load(uri.toString());
var code = utf8.decode(data.buffer.asUint8List(data.offsetInBytes, data.lengthInBytes));

expect(code.length > 128 * 1024, true);
});
});
}