Skip to content
This repository has been archived by the owner on Sep 14, 2021. It is now read-only.

Commit

Permalink
Add helper to convert Observatory URI to WS URI (#232)
Browse files Browse the repository at this point in the history
* Add helper to convert Observatory URI to WS URI

This is to make it easier to reuse in devtools+devtools_server.

See flutter/devtools#563 (comment).

* Simplify test

* Add changelog + rev version
  • Loading branch information
DanTup authored and devoncarew committed Apr 23, 2019
1 parent 8cbee89 commit 007acf0
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 1 deletion.
4 changes: 4 additions & 0 deletions dart/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Changelog

## 3.15.1
- Add `getVmWsUriFromObservatoryUri`, a helper function to convert observatory URIs
into the required WebSocket URI for connecting to the VM service.

## 3.15.0
- support service protocol version 3.15
- fix an issue decoding null `Script.tokenPosTable` values
Expand Down
18 changes: 18 additions & 0 deletions dart/lib/utils.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
// Copyright (c) 2019, the Dart project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.

/// Map the URI (which may already be Observatory web app) to a WebSocket URI
/// for the VM service.
///
/// If the URI is already a VM Service WebSocket URI it will not be modified.
Uri getVmWsUriFromObservatoryUri(Uri uri) {
final isSecure = uri.isScheme('wss') || uri.isScheme('https');
final scheme = isSecure ? 'wss' : 'ws';

final path = uri.path.endsWith('/ws')
? uri.path
: (uri.path.endsWith('/') ? '${uri.path}ws' : '${uri.path}/ws');

return uri.replace(scheme: scheme, path: path);
}
2 changes: 1 addition & 1 deletion dart/pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name: vm_service_lib
description: A library to access the VM Service API.
version: 3.15.0
version: 3.15.1

author: Dart Team <[email protected]>
homepage: https://github.com/dart-lang/vm_service_drivers
Expand Down
35 changes: 35 additions & 0 deletions dart/test/utils_test.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
// Copyright (c) 2019, the Dart project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.

import 'package:test/test.dart';
import 'package:vm_service_lib/utils.dart';

void main() {
test('getVmWsUriFromObservatoryUri maps URIs correctly', () {
final testCases = {
'http://localhost:123/': 'ws://localhost:123/ws',
'https://localhost:123/': 'wss://localhost:123/ws',
'ws://localhost:123/': 'ws://localhost:123/ws',
'wss://localhost:123/': 'wss://localhost:123/ws',
'http://localhost:123/ABCDEF=/': 'ws://localhost:123/ABCDEF=/ws',
'https://localhost:123/ABCDEF=/': 'wss://localhost:123/ABCDEF=/ws',
'ws://localhost:123/ABCDEF=/': 'ws://localhost:123/ABCDEF=/ws',
'wss://localhost:123/ABCDEF=/': 'wss://localhost:123/ABCDEF=/ws',
'http://localhost:123': 'ws://localhost:123/ws',
'https://localhost:123': 'wss://localhost:123/ws',
'ws://localhost:123': 'ws://localhost:123/ws',
'wss://localhost:123': 'wss://localhost:123/ws',
'http://localhost:123/ABCDEF=': 'ws://localhost:123/ABCDEF=/ws',
'https://localhost:123/ABCDEF=': 'wss://localhost:123/ABCDEF=/ws',
'ws://localhost:123/ABCDEF=': 'ws://localhost:123/ABCDEF=/ws',
'wss://localhost:123/ABCDEF=': 'wss://localhost:123/ABCDEF=/ws',
};

testCases.forEach((String input, String expected) {
final inputUri = Uri.parse(input);
final actualUri = getVmWsUriFromObservatoryUri(inputUri);
expect(actualUri.toString(), equals(expected));
});
});
}

0 comments on commit 007acf0

Please sign in to comment.