Files

66 lines
2.1 KiB
Dart

import '../core/transport/server_url.dart';
/// The single endpoints decision for an app launch.
///
/// A web build is served by the Pipelines proxy itself, so its own page origin
/// is the only endpoint it may use: the browser reaches the same host over
/// ws(s) at `/connect`. A native build has no trustworthy origin and must be
/// given an explicit endpoint at build time; there is no default host.
class ServerEndpoint {
const ServerEndpoint._({
this.uri,
this.error,
this.requiresConfiguration = false,
});
/// Validated websocket endpoint, or null when configuration is missing or
/// invalid. Never a guessed fallback.
final Uri? uri;
/// Why no endpoint could be resolved, shown verbatim by the connection gate.
final String? error;
/// Native builds without an explicit endpoint. This is a configuration
/// state, not a server error, so the gate can explain the required setting.
final bool requiresConfiguration;
static ServerEndpoint resolve({
required Uri base,
required bool web,
String configured = '',
}) {
if (web) {
if ((base.scheme != 'http' && base.scheme != 'https') ||
base.host.isEmpty) {
return const ServerEndpoint._(
error:
'The Pipelines endpoint is inferred from the page URL, '
'which is not HTTP(S).',
);
}
final secure = base.scheme == 'https';
final sameOrigin = Uri(
scheme: secure ? 'wss' : 'ws',
host: base.host,
port: base.hasPort ? base.port : null,
path: '/connect',
);
return _validated(sameOrigin);
}
final value = configured.trim();
if (value.isEmpty) {
return const ServerEndpoint._(requiresConfiguration: true);
}
return _validated(Uri.tryParse(value) ?? Uri());
}
static ServerEndpoint _validated(Uri value) {
try {
final endpoint = ServerUrl.validate(value.toString());
return ServerEndpoint._(uri: Uri.parse(endpoint.wsUrl));
} on FormatException catch (error) {
return ServerEndpoint._(error: error.message);
}
}
}