diff --git a/packages/globe_cli/lib/src/utils/logs.dart b/packages/globe_cli/lib/src/utils/logs.dart index d96274fe..abac6176 100644 --- a/packages/globe_cli/lib/src/utils/logs.dart +++ b/packages/globe_cli/lib/src/utils/logs.dart @@ -1,7 +1,7 @@ import 'dart:async'; import 'dart:convert'; -import 'dart:io'; +import 'package:eventsource/eventsource.dart'; import 'package:mason_logger/mason_logger.dart'; import 'api.dart'; @@ -114,27 +114,33 @@ Future> streamBuildLogs({ required String projectId, required String deploymentId, }) async { - final host = Uri.parse(api.metadata.endpoint).host; final ctrl = StreamController.broadcast(); - final ws = await WebSocket.connect( - 'wss://$host/api/orgs/$orgId/projects/$projectId/deployments/$deploymentId/build-logs', + final es = await EventSource.connect( + '${api.metadata.endpoint}/api/orgs/$orgId/projects/$projectId/deployments/$deploymentId/build-logs', headers: api.headers, ); - ws.listen((e) { - final json = jsonDecode(e as String) as Map; - final event = BuildLogEvent.fromJson(json); + es.listen((e) { + if (e.data == null) { + return; + } + + final json = jsonDecode(e.data!) as Map; + + // The server emits a status event when a connection is established. + if (json['status'] != null) { + return; + } + final event = BuildLogEvent.fromJson(json); ctrl.add(event); }); - unawaited(ws.done.then((_) => ctrl.close())); - - ctrl.onCancel = () { - ws.close(); + es.onError.listen((e) { + ctrl.add(ErrorBuildLogEvent(error: e.toString())); ctrl.close(); - }; + }); return ctrl.stream; } diff --git a/packages/globe_cli/lib/src/utils/metadata.dart b/packages/globe_cli/lib/src/utils/metadata.dart index 0d989d0a..161fff15 100644 --- a/packages/globe_cli/lib/src/utils/metadata.dart +++ b/packages/globe_cli/lib/src/utils/metadata.dart @@ -15,7 +15,7 @@ class GlobeMetadata { ); static const local = GlobeMetadata( - endpoint: 'http://127.0.0.1:8788', + endpoint: 'http://localhost:8788', isLocal: true, ); diff --git a/packages/globe_cli/pubspec.lock b/packages/globe_cli/pubspec.lock index b4591009..04aaa5e4 100644 --- a/packages/globe_cli/pubspec.lock +++ b/packages/globe_cli/pubspec.lock @@ -193,6 +193,14 @@ packages: url: "https://pub.dev" source: hosted version: "2.0.5" + eventsource: + dependency: "direct main" + description: + name: eventsource + sha256: e21d60cd2320df8c8c303ebe11c80aa96b10928f411a12be31215062d8091be0 + url: "https://pub.dev" + source: hosted + version: "0.4.0" file: dependency: "direct dev" description: @@ -536,6 +544,14 @@ packages: url: "https://pub.dev" source: hosted version: "1.2.0" + sync: + dependency: transitive + description: + name: sync + sha256: f2ebb89eac969abb02b498562a35c4da63d6843396c4fe81948cd06a76845fce + url: "https://pub.dev" + source: hosted + version: "0.3.0" term_glyph: dependency: transitive description: diff --git a/packages/globe_cli/pubspec.yaml b/packages/globe_cli/pubspec.yaml index e4275e8a..7c9f3579 100644 --- a/packages/globe_cli/pubspec.yaml +++ b/packages/globe_cli/pubspec.yaml @@ -20,6 +20,7 @@ dependencies: pub_updater: ^0.3.0 pubspec_parse: ^1.2.3 pool: ^1.5.1 + eventsource: ^0.4.0 dev_dependencies: build_runner: ^2.4.4