Files

217 lines
7.2 KiB
Dart

part of 'app_route.dart';
/// The largest integer a browser can represent exactly, so ids parsed here
/// survive a round trip through `Number`.
const _maxSafe = 9007199254740991;
final _digits = RegExp(r'^[0-9]+$');
String _routePath(AppRoute route) => switch (route.kind) {
AppRouteKind.pipelines => _pipelinePath(route),
AppRouteKind.runs => _runsPath(route),
AppRouteKind.chat => _chatPath(route),
AppRouteKind.search => '/search',
AppRouteKind.settings => '/settings',
AppRouteKind.templates => _templatesPath(route),
AppRouteKind.schedules => '/schedules',
};
String _pipelinePath(AppRoute route) {
final id = _validId(route.pipelineId);
if (id == null) return '/pipelines';
final revision = _latest(route.version);
final base = revision == 0 ? '/pipelines/$id' : '/pipelines/$id/$revision';
final key = route.stageKey;
return key == null || key.isEmpty
? base
: '$base/stage/${Uri.encodeComponent(key)}';
}
String _runsPath(AppRoute route) {
if (route.runId != null) {
final id = _validId(route.runId);
if (id == null) return '/runs';
final base = '/runs/$id';
final key = route.stageKey;
if (key != null && key.isNotEmpty) {
final index = _nonNegative(route.visit);
return index == null
? '$base/stage/${Uri.encodeComponent(key)}'
: '$base/stage/${Uri.encodeComponent(key)}?visit=$index';
}
return route.decide ? '$base?action=decide' : base;
}
final filter = _validId(route.pipelineId);
return filter == null ? '/runs' : '/runs?pipeline=$filter';
}
String _templatesPath(AppRoute route) {
final key = route.templateKey;
return key == null || key.isEmpty
? '/templates'
: '/templates/${Uri.encodeComponent(key)}';
}
String _chatPath(AppRoute route) {
final id = route.chatSessionId;
return id == null || !AppRoute.isChatSessionId(id)
? '/chat'
: '/chat/${Uri.encodeComponent(id)}';
}
/// Parses a pathname plus its optional query. Total: anything unrecognised is
/// the Pipelines list. Unknown query parameters are ignored, and an invalid
/// `visit`, `action` or `pipeline` value is dropped without failing the route.
AppRoute _parseRoute(String location) {
var raw = location.trim();
if (raw.isEmpty) return AppRoute.pipelines;
var query = '';
if (raw.contains('://')) {
final uri = Uri.tryParse(raw);
if (uri == null) return AppRoute.pipelines;
raw = uri.path;
query = uri.query;
}
final hash = raw.indexOf('#');
if (hash != -1) raw = raw.substring(0, hash);
final mark = raw.indexOf('?');
if (mark != -1) {
if (query.isEmpty) query = raw.substring(mark + 1);
raw = raw.substring(0, mark);
}
raw = raw.replaceAll(RegExp(r'/+$'), '');
final segments = _decodeSegments(raw);
if (segments == null || segments.isEmpty) return AppRoute.pipelines;
final params = _decodeQuery(query);
return switch (segments.first) {
'pipelines' => _pipelineRoute(segments),
'runs' => _runsRoute(segments, params),
'chat' => _chatRoute(segments),
'search' when segments.length == 1 => AppRoute.search,
'settings' => _settingsRoute(segments),
'templates' => _templatesRoute(segments),
'schedules' when segments.length == 1 => AppRoute.schedules,
_ => AppRoute.pipelines,
};
}
List<String>? _decodeSegments(String path) {
final parts = path.split('/');
final first = parts.isNotEmpty && parts.first.isEmpty ? 1 : 0;
final values = <String>[];
for (var index = first; index < parts.length; index++) {
if (parts[index].isEmpty) return null;
try {
values.add(Uri.decodeComponent(parts[index]));
} on ArgumentError {
return null;
}
}
return values;
}
Map<String, String> _decodeQuery(String query) {
if (query.isEmpty) return const {};
try {
return Uri.splitQueryString(query);
} on ArgumentError {
return const {};
} on FormatException {
return const {};
}
}
/// `/pipelines/:id[/:version][/stage/:key]`.
AppRoute _pipelineRoute(List<String> segments) {
if (segments.length == 1) return AppRoute.pipelines;
final id = _positiveId(segments[1]);
if (id == null) return AppRoute.pipelines;
if (segments.length == 2) return AppRoute.pipeline(id);
// `stage` comes before a version on purpose: /pipelines/9/stage/7 is the
// pane for stage `7`, not version 7 of pipeline stage `stage`.
if (segments[2] == 'stage') {
return segments.length == 4
? AppRoute.pipelineStage(id, segments[3])
: AppRoute.pipelines;
}
final revision = _number(segments[2]);
if (revision == null) return AppRoute.pipelines;
if (segments.length == 3) return AppRoute.pipeline(id, version: revision);
return segments[3] == 'stage' && segments.length == 5
? AppRoute.pipelineStage(id, segments[4], version: revision)
: AppRoute.pipelines;
}
/// `/runs[?pipeline=]`, `/runs/:id[?action=decide]` and the stage pane.
///
/// `decide` is the only URL action the app has, so any other `action` value is
/// dropped instead of carried as an opaque string.
AppRoute _runsRoute(List<String> segments, Map<String, String> params) {
if (segments.length == 1) {
final filter = _positiveId(params['pipeline']);
return filter == null ? AppRoute.runs : AppRoute.runsForPipeline(filter);
}
final id = _positiveId(segments[1]);
if (id == null) return AppRoute.pipelines;
if (segments.length == 2) {
return params['action'] == 'decide'
? AppRoute.run.decide(id)
: AppRoute.run(id);
}
if (segments[2] == 'stage' && segments.length == 4) {
return AppRoute.run.stage(id, segments[3], visit: _number(params['visit']));
}
return AppRoute.pipelines;
}
AppRoute _chatRoute(List<String> segments) {
if (segments.length == 1) return AppRoute.chat;
if (segments.length != 2) return AppRoute.pipelines;
final id = segments[1];
return AppRoute.isChatSessionId(id)
? AppRoute.chatSession(id)
: AppRoute.pipelines;
}
/// `/settings` and the `/settings/templates[/:key]` aliases.
AppRoute _settingsRoute(List<String> segments) {
if (segments.length == 1) return AppRoute.settings;
if (segments[1] != 'templates') return AppRoute.pipelines;
if (segments.length == 2) return AppRoute.templates;
return segments.length == 3
? AppRoute.template(segments[2])
: AppRoute.pipelines;
}
AppRoute _templatesRoute(List<String> segments) {
if (segments.length == 1) return AppRoute.templates;
return segments.length == 2
? AppRoute.template(segments[1])
: AppRoute.pipelines;
}
/// A non-negative safe integer, or null. Digits only, so `-1`, `1.5` and an
/// empty value are all dropped exactly where the web parser drops them.
int? _number(String? value) {
if (value == null || !_digits.hasMatch(value)) return null;
final number = int.tryParse(value);
if (number == null || number < 0 || number > _maxSafe) return null;
return number;
}
/// A positive safe integer, or null.
int? _positiveId(String? value) {
final number = _number(value);
return number == null || number == 0 ? null : number;
}
int? _validId(int? value) =>
value == null || value <= 0 || value > _maxSafe ? null : value;
int? _nonNegative(int? value) =>
value == null || value < 0 || value > _maxSafe ? null : value;
int _latest(int version) => version > 0 && version <= _maxSafe ? version : 0;