Skip to content

Commit

Permalink
fix: switch to event source log streaming (#41)
Browse files Browse the repository at this point in the history
  • Loading branch information
Ehesp committed Feb 13, 2024
1 parent fd3a384 commit 6b6709f
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 13 deletions.
30 changes: 18 additions & 12 deletions packages/globe_cli/lib/src/utils/logs.dart
Original file line number Diff line number Diff line change
@@ -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';
Expand Down Expand Up @@ -114,27 +114,33 @@ Future<Stream<BuildLogEvent>> streamBuildLogs({
required String projectId,
required String deploymentId,
}) async {
final host = Uri.parse(api.metadata.endpoint).host;
final ctrl = StreamController<BuildLogEvent>.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<String, dynamic>;
final event = BuildLogEvent.fromJson(json);
es.listen((e) {
if (e.data == null) {
return;
}

final json = jsonDecode(e.data!) as Map<String, dynamic>;

// 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;
}
Expand Down
2 changes: 1 addition & 1 deletion packages/globe_cli/lib/src/utils/metadata.dart
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ class GlobeMetadata {
);

static const local = GlobeMetadata(
endpoint: 'http://127.0.0.1:8788',
endpoint: 'http://localhost:8788',
isLocal: true,
);

Expand Down
16 changes: 16 additions & 0 deletions packages/globe_cli/pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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:
Expand Down
1 change: 1 addition & 0 deletions packages/globe_cli/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 6b6709f

Please sign in to comment.