Skip to content

Commit 3ec99fc

Browse files
committed
Avoid Dart utf-8 decoder on the web
1 parent f7dc26f commit 3ec99fc

7 files changed

Lines changed: 31 additions & 5 deletions

File tree

sqlite3/lib/src/implementation/database.dart

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import '../constants.dart';
99
import '../database.dart';
1010
import '../exception.dart';
1111
import '../functions.dart';
12+
import '../platform/platform.dart';
1213
import '../result_set.dart';
1314
import '../statement.dart';
1415
import 'bindings.dart';
@@ -393,7 +394,9 @@ base class DatabaseImplementation implements CommonDatabase {
393394
// or comments were parsed. That's fine, just skip over it then.
394395
final stmt = result.result;
395396
if (stmt != null) {
396-
final stmtSql = utf8.decoder.convert(bytes, offset, endOffset);
397+
final stmtSql = utf8Decode(
398+
Uint8List.sublistView(bytes, offset, endOffset),
399+
);
397400

398401
createdStatements.add(wrapStatement(stmtSql, stmt));
399402
}

sqlite3/lib/src/jsonb.dart

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ import 'dart:typed_data';
33

44
import 'package:typed_data/typed_buffers.dart';
55

6+
import 'platform/platform.dart';
7+
68
/// A [Codec] capable of converting Dart objects from and to the [JSONB] format
79
/// used by sqlite3.
810
///
@@ -176,7 +178,7 @@ final class _JsonbDecodingState {
176178
}
177179

178180
String payloadString() {
179-
return utf8.decode(payloadBytes());
181+
return utf8Decode(payloadBytes());
180182
}
181183

182184
final value = switch (type) {
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,12 @@
1+
import 'dart:convert';
2+
import 'dart:typed_data';
3+
14
import 'package:path/path.dart' as p;
25

36
String pathToAbsoluteAndNormalize(String source) {
47
return p.url.normalize('/$source');
58
}
9+
10+
String utf8Decode(Uint8List bytes) {
11+
return utf8.decode(bytes);
12+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export 'fallback.dart' if (dart.library.js_interop) 'web.dart';

sqlite3/lib/src/platform/web.dart

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,13 @@
44
/// In compiled web workers, Dart's [Uri.parse] implementation takes up a good
55
/// chunk of the total file size. By using [URL] directly (which is good enough
66
/// for our use case), we can avoid that implementation.
7+
///
8+
/// Additionally, we can avoid Dart's utf-8 decoder.
79
library;
810

11+
import 'dart:js_interop';
12+
import 'dart:typed_data';
13+
914
import 'package:web/web.dart';
1015

1116
String pathToAbsoluteAndNormalize(String source) {
@@ -15,3 +20,9 @@ String pathToAbsoluteAndNormalize(String source) {
1520
Iterable<String> pathComponents(String path) {
1621
return URL(path, 'file:///').pathname.split('/').where((e) => e.isNotEmpty);
1722
}
23+
24+
String utf8Decode(Uint8List bytes) {
25+
return _decoder.decode(bytes.toJS);
26+
}
27+
28+
final _decoder = TextDecoder();

sqlite3/lib/src/wasm/vfs/async_opfs/sync_channel.dart

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import 'dart:convert';
22
import 'dart:typed_data';
33

4+
import '../../../platform/platform.dart';
45
import '../../js_interop.dart';
56

67
const protocolVersion = 1;
@@ -121,7 +122,7 @@ class MessageSerializer {
121122

122123
String _readString(int offset) {
123124
final length = dataView.getInt32(offset);
124-
return utf8.decode(buffer.asUint8ListSlice(offset + 4, length));
125+
return utf8Decode(buffer.asUint8ListSlice(offset + 4, length));
125126
}
126127

127128
void _writeString(int offset, String data) {

sqlite3/lib/src/wasm/wasm_interop.dart

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import 'dart:typed_data';
66
import 'package:web/web.dart';
77

88
import '../implementation/bindings.dart';
9+
import '../platform/web.dart';
910
import 'injected_values.dart';
1011
import 'js_interop.dart';
1112

@@ -475,15 +476,15 @@ extension WrappedMemory on Memory {
475476

476477
String readString(int address, [int? length]) {
477478
assert(address != 0, 'Null pointer dereference');
478-
return utf8.decode(
479+
return utf8Decode(
479480
dartBuffer.asUint8List(address, length ?? strlen(address)),
480481
);
481482
}
482483

483484
String? readNullableString(int address, [int? length]) {
484485
if (address == 0) return null;
485486

486-
return utf8.decode(
487+
return utf8Decode(
487488
dartBuffer.asUint8List(address, length ?? strlen(address)),
488489
);
489490
}

0 commit comments

Comments
 (0)