-
Notifications
You must be signed in to change notification settings - Fork 338
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Tweaks to browser-launching service #563
Changes from all commits
609fe73
b86c187
e8c4b9a
f1a1caa
a75b75e
bb80e3e
a794d89
8addfa3
3a6a9c5
cecfec5
3a19128
faba3b9
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -13,6 +13,7 @@ import 'package:path/path.dart' as path; | |
import 'package:shelf/shelf.dart' as shelf; | ||
import 'package:shelf/shelf_io.dart' as shelf; | ||
import 'package:shelf_static/shelf_static.dart'; | ||
import 'package:vm_service_lib/utils.dart'; | ||
import 'package:vm_service_lib/vm_service_lib.dart' hide Isolate; | ||
|
||
import 'chrome.dart'; | ||
|
@@ -102,6 +103,10 @@ void serveDevTools({ | |
printOutput( | ||
'Serving DevTools at $devToolsUrl', | ||
{ | ||
'event': 'server.started', | ||
// TODO(dantup): Remove this `method` field when we're sure VS Code users | ||
// are all on a newer version that uses `event`. We incorrectly used | ||
// `method` for the original releases. | ||
'method': 'server.started', | ||
'params': {'host': server.address.host, 'port': server.port, 'pid': pid} | ||
}, | ||
|
@@ -125,39 +130,68 @@ void serveDevTools({ | |
// } | ||
// } | ||
_stdinCommandStream.listen((Map<String, dynamic> json) async { | ||
final int id = json['id']; | ||
// ID can be String, int or null | ||
final dynamic id = json['id']; | ||
final Map<String, dynamic> params = json['params']; | ||
|
||
if (!params.containsKey('uri')) { | ||
printOutput( | ||
'Invalid input: $params does not contain the key \'uri\'', | ||
{ | ||
'id': id, | ||
'error': 'Invalid input: $params does not contain the key \'uri\'', | ||
}, | ||
machineMode: machineMode, | ||
); | ||
switch (json['method']) { | ||
case 'vm.register': | ||
await _handleVmRegister(id, params, machineMode, devToolsUrl); | ||
break; | ||
default: | ||
printOutput( | ||
'Unknown method ${json['method']}', | ||
{ | ||
'id': id, | ||
'error': 'Unknown method ${json['method']}', | ||
}, | ||
machineMode: machineMode, | ||
); | ||
} | ||
|
||
// json['uri'] should contain a vm service uri. | ||
final uri = Uri.parse(params['uri']); | ||
|
||
// Lots of things are considered valid URIs (including empty strings | ||
// and single letters) since they can be relative, so we need to do some | ||
// extra checks. | ||
if (uri != null && | ||
uri.isAbsolute && | ||
(uri.isScheme('ws') || | ||
uri.isScheme('wss') || | ||
uri.isScheme('http') || | ||
uri.isScheme('https'))) | ||
await registerLaunchDevToolsService(uri, id, devToolsUrl, machineMode); | ||
}); | ||
} | ||
|
||
Future<void> _handleVmRegister(dynamic id, Map<String, dynamic> params, | ||
bool machineMode, String devToolsUrl) async { | ||
if (!params.containsKey('uri')) { | ||
printOutput( | ||
'Invalid input: $params does not contain the key \'uri\'', | ||
{ | ||
'id': id, | ||
'error': 'Invalid input: $params does not contain the key \'uri\'', | ||
}, | ||
machineMode: machineMode, | ||
); | ||
} | ||
|
||
// json['uri'] should contain a vm service uri. | ||
final uri = Uri.tryParse(params['uri']); | ||
|
||
// Lots of things are considered valid URIs (including empty strings | ||
// and single letters) since they can be relative, so we need to do some | ||
// extra checks. | ||
if (uri != null && | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. while you are here we should really be failing if the uri is not valid instead of just quietly pretending everything succeeded. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Good spot - this code wasn't sending any response back (so if it happened, VS Code would just sit waiting). It also would throw on the |
||
uri.isAbsolute && | ||
(uri.isScheme('ws') || | ||
uri.isScheme('wss') || | ||
uri.isScheme('http') || | ||
uri.isScheme('https'))) { | ||
await registerLaunchDevToolsService(uri, id, devToolsUrl, machineMode); | ||
} else { | ||
printOutput( | ||
'Uri must be absolute with a http, https, ws or wss scheme', | ||
{ | ||
'id': id, | ||
'error': 'Uri must be absolute with a http, https, ws or wss scheme', | ||
}, | ||
machineMode: machineMode, | ||
); | ||
} | ||
} | ||
|
||
Future<void> registerLaunchDevToolsService( | ||
Uri uri, | ||
int id, | ||
dynamic id, | ||
String devToolsUrl, | ||
bool machineMode, | ||
) async { | ||
|
@@ -184,7 +218,7 @@ Future<void> registerLaunchDevToolsService( | |
'Successfully registered launchDevTools service', | ||
{ | ||
'id': id, | ||
'result': Success().toJson(), | ||
'result': {'success': true}, | ||
}, | ||
machineMode: machineMode, | ||
); | ||
|
@@ -201,6 +235,9 @@ Future<void> registerLaunchDevToolsService( | |
} | ||
|
||
Future<VmService> _connectToVmService(Uri uri) async { | ||
// Fix up the various acceptable URI formats into a WebSocket URI to connect. | ||
uri = convertToWebSocketUrl(serviceProtocolUrl: uri); | ||
|
||
final WebSocket ws = await WebSocket.connect(uri.toString()); | ||
|
||
final VmService service = VmService( | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What types might we get here?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
VS Code has always used strings. When I changed this, I thought I checked the spec and it said "any value", however that was actually Wikipedia! The spec says:
I don't think we follow JSON-RPC particularly strictly, but I do think we should support both int+string here (I'm using shared code used for Flutter/analysis server/etc. which used strings, and it was just crashing on this line).
I'm hoping one day we can support
int | String
!There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
(I've added a comment about the types)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The comment helps a lot. Thanks!