|
| 1 | +import 'dart:async'; |
| 2 | +import 'dart:convert'; |
| 3 | +import 'dart:typed_data'; |
| 4 | + |
| 5 | +/// Default max UTF-8 byte length of one JSON-RPC stdout line (NDJSON). |
| 6 | +/// |
| 7 | +/// Large `db.query` results are one object per line; without a bound the host |
| 8 | +/// can OOM before Preferences row caps apply. Drivers should honor `limit`. |
| 9 | +const int kDefaultJsonRpcMaxLineBytes = 32 * 1024 * 1024; |
| 10 | + |
| 11 | +/// Lines above this UTF-8 length are `jsonDecode`d off the UI isolate. |
| 12 | +const int kJsonRpcOffIsolateDecodeThresholdBytes = 64 * 1024; |
| 13 | + |
| 14 | +/// Thrown when a plugin emits a newline-delimited JSON line larger than the |
| 15 | +/// configured maximum. |
| 16 | +class JsonRpcPayloadTooLargeException implements Exception { |
| 17 | + JsonRpcPayloadTooLargeException({ |
| 18 | + required this.maxLineBytes, |
| 19 | + required this.receivedBytes, |
| 20 | + }); |
| 21 | + |
| 22 | + final int maxLineBytes; |
| 23 | + final int receivedBytes; |
| 24 | + |
| 25 | + @override |
| 26 | + String toString() => |
| 27 | + 'JsonRpcPayloadTooLargeException: JSON-RPC line is $receivedBytes bytes ' |
| 28 | + '(max $maxLineBytes). Reduce result size or pass a smaller `limit`.'; |
| 29 | +} |
| 30 | + |
| 31 | +/// Splits a byte stream into UTF-8 lines, failing closed if any line exceeds |
| 32 | +/// [maxLineBytes] (counted before decode). |
| 33 | +StreamTransformer<List<int>, String> boundedUtf8LineSplitter({ |
| 34 | + int maxLineBytes = kDefaultJsonRpcMaxLineBytes, |
| 35 | +}) { |
| 36 | + return _BoundedUtf8LineSplitter(maxLineBytes: maxLineBytes); |
| 37 | +} |
| 38 | + |
| 39 | +class _BoundedUtf8LineSplitter |
| 40 | + extends StreamTransformerBase<List<int>, String> { |
| 41 | + _BoundedUtf8LineSplitter({required this.maxLineBytes}); |
| 42 | + |
| 43 | + final int maxLineBytes; |
| 44 | + |
| 45 | + @override |
| 46 | + Stream<String> bind(Stream<List<int>> stream) { |
| 47 | + final controller = StreamController<String>(sync: true); |
| 48 | + final pending = BytesBuilder(copy: false); |
| 49 | + late final StreamSubscription<List<int>> sub; |
| 50 | + |
| 51 | + void fail(Object error, [StackTrace? st]) { |
| 52 | + if (!controller.isClosed) { |
| 53 | + controller.addError(error, st); |
| 54 | + controller.close(); |
| 55 | + } |
| 56 | + sub.cancel(); |
| 57 | + } |
| 58 | + |
| 59 | + void emitLine() { |
| 60 | + var bytes = pending.takeBytes(); |
| 61 | + if (bytes.isNotEmpty && bytes.last == 0x0d) { |
| 62 | + bytes = Uint8List.sublistView(bytes, 0, bytes.length - 1); |
| 63 | + } |
| 64 | + if (bytes.isEmpty) return; |
| 65 | + try { |
| 66 | + controller.add(utf8.decode(bytes)); |
| 67 | + } catch (e, st) { |
| 68 | + fail(e, st); |
| 69 | + } |
| 70 | + } |
| 71 | + |
| 72 | + sub = stream.listen( |
| 73 | + (chunk) { |
| 74 | + for (var i = 0; i < chunk.length; i++) { |
| 75 | + final b = chunk[i]; |
| 76 | + if (b == 0x0a) { |
| 77 | + emitLine(); |
| 78 | + continue; |
| 79 | + } |
| 80 | + if (pending.length >= maxLineBytes) { |
| 81 | + fail( |
| 82 | + JsonRpcPayloadTooLargeException( |
| 83 | + maxLineBytes: maxLineBytes, |
| 84 | + receivedBytes: pending.length + 1, |
| 85 | + ), |
| 86 | + ); |
| 87 | + return; |
| 88 | + } |
| 89 | + pending.addByte(b); |
| 90 | + } |
| 91 | + }, |
| 92 | + onError: fail, |
| 93 | + onDone: () { |
| 94 | + if (pending.length > 0) { |
| 95 | + if (pending.length > maxLineBytes) { |
| 96 | + fail( |
| 97 | + JsonRpcPayloadTooLargeException( |
| 98 | + maxLineBytes: maxLineBytes, |
| 99 | + receivedBytes: pending.length, |
| 100 | + ), |
| 101 | + ); |
| 102 | + return; |
| 103 | + } |
| 104 | + emitLine(); |
| 105 | + } |
| 106 | + controller.close(); |
| 107 | + }, |
| 108 | + cancelOnError: true, |
| 109 | + ); |
| 110 | + |
| 111 | + controller.onCancel = () => sub.cancel(); |
| 112 | + return controller.stream; |
| 113 | + } |
| 114 | +} |
0 commit comments