From 9b1828f4c2456e8ee5612705d5992f60c6378e29 Mon Sep 17 00:00:00 2001 From: Nate Bosch Date: Wed, 9 Aug 2023 15:01:05 -0700 Subject: [PATCH] Send a MessagePort to host on frame startup (#2072) Towards #2065 All host implementations are updated to allow either the legacy format, or this new format. Simplify the communication and avoid a listener on the frame window message events. Upon startup replace the `{'ready': true}` message with a `'port'` message and the `MessagePort` for a channel created on the frame side. Omit the manual 'href' field, no host implementations would read it. --- pkgs/test/CHANGELOG.md | 5 ++ .../runner/browser/post_message_channel.dart | 50 +++++-------------- pkgs/test/pubspec.yaml | 2 +- 3 files changed, 19 insertions(+), 38 deletions(-) diff --git a/pkgs/test/CHANGELOG.md b/pkgs/test/CHANGELOG.md index 5768784fc..b5b76d7be 100644 --- a/pkgs/test/CHANGELOG.md +++ b/pkgs/test/CHANGELOG.md @@ -1,3 +1,8 @@ +## 1.24.7-wip + +* Simplify the initialization of the per-suite message channel within browser + tests. See https://github.com/dart-lang/test/issues/2065 + ## 1.24.6 * Fix communication failures between minified test apps and the non-minified diff --git a/pkgs/test/lib/src/runner/browser/post_message_channel.dart b/pkgs/test/lib/src/runner/browser/post_message_channel.dart index 55a602076..fa71d3db3 100644 --- a/pkgs/test/lib/src/runner/browser/post_message_channel.dart +++ b/pkgs/test/lib/src/runner/browser/post_message_channel.dart @@ -8,47 +8,23 @@ import 'package:stream_channel/stream_channel.dart'; import 'dom.dart' as dom; -/// Constructs a [StreamChannel] wrapping [MessageChannel] communication with -/// the host page. +/// Constructs a [StreamChannel] wrapping a new [MessageChannel] communicating +/// with the host page. +/// +/// Sends a [MessagePort] to the host page for the channel. StreamChannel postMessageChannel() { var controller = StreamChannelController(sync: true); - - // Listen for a message from the host that transfers a message port, then - // cancel the subscription. This is important to prevent multiple - // subscriptions if the test is ever hot restarted. - late final dom.Subscription subscription; - subscription = - dom.Subscription(dom.window, 'message', allowInterop((dom.Event event) { - // A message on the Window can theoretically come from any website. It's - // very unlikely that a malicious site would care about hacking someone's - // unit tests, let alone be able to find the test server while it's - // running, but it's good practice to check the origin anyway. - final message = event as dom.MessageEvent; - if (message.origin == dom.window.location.origin && - message.data == 'port') { - subscription.cancel(); - var port = message.ports.first; - port.start(); - var portSubscription = - dom.Subscription(port, 'message', allowInterop((dom.Event event) { - controller.local.sink.add((event as dom.MessageEvent).data); - })); - - controller.local.stream.listen((data) { - port.postMessage({'data': data}); - }, onDone: () { - port.postMessage({'event': 'done'}); - portSubscription.cancel(); - }); - } + var channel = dom.createMessageChannel(); + dom.window.parent + .postMessage('port', dom.window.location.origin, [channel.port2]); + var portSubscription = dom.Subscription(channel.port1, 'message', + allowInterop((dom.Event event) { + controller.local.sink.add((event as dom.MessageEvent).data); })); + channel.port1.start(); - // Send a ready message once we're listening so the host knows it's safe to - // start sending events. - // TODO: https://github.com/dart-lang/test/issues/2065 - remove href - dom.window.parent.postMessage( - jsify({'href': dom.window.location.href, 'ready': true}) as Object, - dom.window.location.origin); + controller.local.stream + .listen(channel.port1.postMessage, onDone: portSubscription.cancel); return controller.foreign; } diff --git a/pkgs/test/pubspec.yaml b/pkgs/test/pubspec.yaml index fc11f4dd7..872b96aea 100644 --- a/pkgs/test/pubspec.yaml +++ b/pkgs/test/pubspec.yaml @@ -1,5 +1,5 @@ name: test -version: 1.24.6 +version: 1.24.7-wip description: >- A full featured library for writing and running Dart tests across platforms. repository: https://github.com/dart-lang/test/tree/master/pkgs/test