abd696b0d9
Add the Settings destination under lib/features/settings with an editable websocket endpoint, truthful connection states, one ping per connect/mount, workspace validation and persistence under new feature-owned keys. Material and Cupertino controls, wrapping copy at large text, pending ping state, and isolated probe tools plus widget/unit coverage.
104 lines
3.4 KiB
Dart
104 lines
3.4 KiB
Dart
import 'dart:convert';
|
|
import 'dart:io';
|
|
|
|
/// An isolated loopback fixture for the Settings browser probe.
|
|
///
|
|
/// It serves the probe's `build/web` output and upgrades `/connect` to a
|
|
/// websocket that answers `ping` with a deterministic pong (build + time), so
|
|
/// the screen can be exercised in a real browser without the production server,
|
|
/// authentication changes or any deployment. It binds loopback on an OS-assigned
|
|
/// port unless `--port` is given.
|
|
///
|
|
/// Usage:
|
|
/// dart run tool/settings_probe_server.dart --root build/probe_web
|
|
Future<void> main(List<String> arguments) async {
|
|
var rootPath = 'build/probe_web';
|
|
var port = 0;
|
|
for (var i = 0; i < arguments.length; i++) {
|
|
if (arguments[i] == '--root' && i + 1 < arguments.length) {
|
|
rootPath = arguments[++i];
|
|
} else if (arguments[i] == '--port' && i + 1 < arguments.length) {
|
|
port = int.parse(arguments[++i]);
|
|
}
|
|
}
|
|
final root = Directory(rootPath).absolute;
|
|
if (!root.existsSync()) {
|
|
stderr.writeln('Probe build not found at ${root.path}');
|
|
exitCode = 2;
|
|
return;
|
|
}
|
|
final server = await HttpServer.bind(InternetAddress.loopbackIPv4, port);
|
|
stdout.writeln('http://127.0.0.1:${server.port}/');
|
|
await for (final request in server) {
|
|
unawaited(_handle(request, root));
|
|
}
|
|
}
|
|
|
|
Future<void> _handle(HttpRequest request, Directory root) async {
|
|
if (request.uri.path == '/connect' &&
|
|
WebSocketTransformer.isUpgradeRequest(request)) {
|
|
final socket = await WebSocketTransformer.upgrade(request);
|
|
stdout.writeln('connect');
|
|
socket.listen((data) {
|
|
if (data is! String) return;
|
|
final Object? decoded;
|
|
try {
|
|
decoded = jsonDecode(data);
|
|
} on FormatException {
|
|
return;
|
|
}
|
|
if (decoded is! Map<String, dynamic>) return;
|
|
if (decoded['type'] != 'ping') return;
|
|
stdout.writeln('ping');
|
|
socket.add(
|
|
jsonEncode(<String, dynamic>{
|
|
'id': decoded['id'],
|
|
'type': 'pong',
|
|
'protocol': 1,
|
|
'build': <String, dynamic>{
|
|
'version': 'probe-1.2.3',
|
|
'commit': 'abc1234',
|
|
},
|
|
'time': '2026-10-21T12:00:00Z',
|
|
}),
|
|
);
|
|
});
|
|
return;
|
|
}
|
|
|
|
var relative = request.uri.path;
|
|
if (relative.isEmpty || relative == '/') relative = '/index.html';
|
|
final file = File('${root.path}${relative.replaceAll('..', '')}');
|
|
if (!file.existsSync()) {
|
|
request.response.statusCode = HttpStatus.notFound;
|
|
await request.response.close();
|
|
return;
|
|
}
|
|
request.response.headers.contentType = _contentType(relative);
|
|
request.response.headers.set('Cache-Control', 'no-store');
|
|
await request.response.addStream(file.openRead());
|
|
await request.response.close();
|
|
}
|
|
|
|
ContentType _contentType(String path) {
|
|
final extension = path.split('.').last.toLowerCase();
|
|
return switch (extension) {
|
|
'html' => ContentType.html,
|
|
'js' || 'mjs' => ContentType('text', 'javascript', charset: 'utf-8'),
|
|
'css' => ContentType('text', 'css', charset: 'utf-8'),
|
|
'json' || 'map' => ContentType.json,
|
|
'png' => ContentType('image', 'png'),
|
|
'svg' => ContentType('image', 'svg+xml'),
|
|
'ico' => ContentType('image', 'x-icon'),
|
|
'wasm' => ContentType('application', 'wasm'),
|
|
'ttf' || 'otf' => ContentType('font', 'ttf'),
|
|
_ => ContentType.binary,
|
|
};
|
|
}
|
|
|
|
void unawaited(Future<void> future) {
|
|
future.catchError((Object error, StackTrace stack) {
|
|
stderr.writeln('probe server: $error');
|
|
});
|
|
}
|