Skip to content

Commit 7138982

Browse files
authored
Merge pull request #1 from d-markey/main
Support Web platforms.
2 parents 05b823b + 2d39d9f commit 7138982

14 files changed

+1850
-114
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
## 3.1.0
2+
3+
- Add support for Web patforms + corresponding tests.
4+
15
## 3.0.0
26

37
- Add `skip` method.

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,4 +50,4 @@ abstract class RandomAccessSource {
5050
Implementations:
5151

5252
- `BytesRASource` for `Uint8List`.
53-
- `FileRASource` for `RandomAccessFile`.
53+
- `FileRASource` for `RandomAccessFile` (`dart:io`) and `Blob` (`package:web`).

dart_test.yaml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
platforms:
2+
- vm
3+
- chrome
4+
5+
compilers:
6+
- dart2js
7+
- dart2wasm

lib/src/bytes_ra_source.dart

Lines changed: 6 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -12,34 +12,22 @@ class BytesRASource extends RandomAccessSource {
1212
}
1313

1414
@override
15-
Future<int> length() async {
16-
return bytes.length;
17-
}
15+
Future<int> length() async => bytes.length;
1816

1917
@override
20-
Future<int> readByte() async {
21-
return _syncSource.readByte();
22-
}
18+
Future<int> readByte() async => _syncSource.readByte();
2319

2420
@override
25-
Future<Uint8List> read(int count) async {
26-
return _syncSource.read(count);
27-
}
21+
Future<Uint8List> read(int count) async => _syncSource.read(count);
2822

2923
@override
30-
Future<int> position() async {
31-
return _syncSource.position();
32-
}
24+
Future<int> position() async => _syncSource.position();
3325

3426
@override
35-
Future<void> seek(int position) async {
36-
_syncSource.seek(position);
37-
}
27+
Future<void> seek(int position) async => _syncSource.seek(position);
3828

3929
@override
40-
Future<Uint8List> readToEnd() async {
41-
return _syncSource.readToEnd();
42-
}
30+
Future<Uint8List> readToEnd() async => _syncSource.readToEnd();
4331

4432
@override
4533
Future<void> close() async {}

lib/src/file_ra_source.dart

Lines changed: 8 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -1,50 +1,13 @@
1-
import 'dart:io';
2-
import 'dart:typed_data';
3-
41
import '../random_access_source.dart';
2+
import 'file_ra_source_stub.dart'
3+
if (dart.library.io) 'file_ra_source_vm.dart'
4+
if (dart.library.js_interop) 'file_ra_source_web.dart' as impl;
55

6-
class FileRASource extends RandomAccessSource {
7-
final RandomAccessFile _file;
8-
9-
FileRASource(this._file);
10-
11-
static Future<FileRASource> open(String path) async {
12-
final file = await File(path).open();
13-
return FileRASource(file);
14-
}
15-
16-
@override
17-
Future<int> length() async {
18-
return _file.length();
19-
}
20-
21-
@override
22-
Future<int> readByte() async {
23-
return _file.readByte();
24-
}
25-
26-
@override
27-
Future<Uint8List> read(int count) async {
28-
return _file.read(count);
29-
}
30-
31-
@override
32-
Future<int> position() async {
33-
return _file.position();
34-
}
35-
36-
@override
37-
Future<void> seek(int position) async {
38-
await _file.setPosition(position);
39-
}
6+
typedef PlatformFile = impl.PlatformFile;
407

41-
@override
42-
Future<Uint8List> readToEnd() async {
43-
return _file.read(await _file.length());
44-
}
8+
abstract class FileRASource extends RandomAccessSource {
9+
static Future<FileRASource> open(String path) => impl.FileRASource.open(path);
4510

46-
@override
47-
Future<void> close() async {
48-
await _file.close();
49-
}
11+
static Future<FileRASource> load(PlatformFile file) =>
12+
impl.FileRASource.load(file);
5013
}

lib/src/file_ra_source_stub.dart

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
import 'dart:typed_data';
2+
3+
import 'file_ra_source.dart' as base;
4+
5+
typedef PlatformFile = Object;
6+
7+
abstract class FileRASource extends base.FileRASource {
8+
static Future<FileRASource> open(String path) =>
9+
throw UnsupportedError('Not supported on this platform.');
10+
11+
static Future<FileRASource> load(PlatformFile file) =>
12+
throw UnsupportedError('Not supported on this platform.');
13+
14+
@override
15+
Future<int> length() =>
16+
throw UnsupportedError('Not supported on this platform.');
17+
18+
@override
19+
Future<int> readByte() =>
20+
throw UnsupportedError('Not supported on this platform.');
21+
22+
@override
23+
Future<Uint8List> read(int count) =>
24+
throw UnsupportedError('Not supported on this platform.');
25+
26+
@override
27+
Future<int> position() =>
28+
throw UnsupportedError('Not supported on this platform.');
29+
30+
@override
31+
Future<void> seek(int position) =>
32+
throw UnsupportedError('Not supported on this platform.');
33+
34+
@override
35+
Future<Uint8List> readToEnd() =>
36+
throw UnsupportedError('Not supported on this platform.');
37+
38+
@override
39+
Future<void> close() =>
40+
throw UnsupportedError('Not supported on this platform.');
41+
}

lib/src/file_ra_source_vm.dart

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
import 'dart:io' as io;
2+
import 'dart:typed_data';
3+
4+
import 'file_ra_source.dart' as base;
5+
6+
typedef PlatformFile = io.File;
7+
8+
class FileRASource extends base.FileRASource {
9+
final io.RandomAccessFile _file;
10+
11+
FileRASource._(this._file);
12+
13+
static Future<FileRASource> open(String path) async =>
14+
FileRASource._(await io.File(path).open());
15+
16+
static Future<FileRASource> load(PlatformFile file) async =>
17+
FileRASource._(await file.open());
18+
19+
@override
20+
Future<int> length() => _file.length();
21+
22+
@override
23+
Future<int> readByte() => _file.readByte();
24+
25+
@override
26+
Future<Uint8List> read(int count) => _file.read(count);
27+
28+
@override
29+
Future<int> position() => _file.position();
30+
31+
@override
32+
Future<void> seek(int position) => _file.setPosition(position);
33+
34+
@override
35+
Future<Uint8List> readToEnd() async => _file.read(await _file.length());
36+
37+
@override
38+
Future<void> close() => _file.close();
39+
}

lib/src/file_ra_source_web.dart

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
import 'dart:js_interop';
2+
import 'dart:typed_data';
3+
4+
import 'package:web/web.dart' as web;
5+
6+
import 'bytes_ra_source.dart';
7+
import 'file_ra_source.dart' as base;
8+
9+
typedef PlatformFile = web.Blob;
10+
11+
@JS('fetch')
12+
external JSPromise<web.Response> _fetch(JSAny url);
13+
14+
class FileRASource extends base.FileRASource {
15+
final BytesRASource _bytes;
16+
17+
FileRASource._(this._bytes);
18+
19+
static Future<base.FileRASource> open(String path) async {
20+
final res = await _fetch(path.toJS).toDart;
21+
final bytes = (await res.bytes().toDart).toDart;
22+
return FileRASource._(BytesRASource(bytes));
23+
}
24+
25+
static Future<FileRASource> load(PlatformFile file) async {
26+
final bytes = (await file.arrayBuffer().toDart).toDart.asUint8List();
27+
return FileRASource._(BytesRASource(bytes));
28+
}
29+
30+
@override
31+
Future<int> length() => _bytes.length();
32+
33+
@override
34+
Future<int> readByte() => _bytes.readByte();
35+
36+
@override
37+
Future<Uint8List> read(int count) => _bytes.read(count);
38+
39+
@override
40+
Future<int> position() => _bytes.position();
41+
42+
@override
43+
Future<void> seek(int position) => _bytes.seek(position);
44+
45+
@override
46+
Future<Uint8List> readToEnd() => _bytes.readToEnd();
47+
48+
@override
49+
Future<void> close() => _bytes.close();
50+
}

pubspec.yaml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
11
name: random_access_source
22
description: A shared interface for common random access data.
3-
version: 3.0.0
3+
version: 3.1.0
44
repository: https://github.com/flutter-cavalry/random_access_source
55

66
environment:
77
sdk: ^3.4.4
88

9+
dependencies:
10+
web: ^1.1.1
11+
912
dev_dependencies:
1013
mgenware_dart_lints: ^8.0.0
1114
test: ^1.24.0

0 commit comments

Comments
 (0)