Skip to content

Commit

Permalink
Get rid of --user-data-dir, search for url when attaching, add logs a…
Browse files Browse the repository at this point in the history
…nd test
  • Loading branch information
roblourens committed Nov 6, 2015
1 parent 8232921 commit aee6dd2
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 28 deletions.
4 changes: 4 additions & 0 deletions test/webkit/utilities.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,10 @@ suite('Utilities', () => {
const url = 'http://site.com/My/Cool/Site/script.js?stuff';
testCanUrl(url, url);
});

test('strips trailing slash', () => {
testCanUrl('http://site.com/', 'http://site.com');
});
});

suite('remoteObjectToValue()', () => {
Expand Down
4 changes: 3 additions & 1 deletion webkit/utilities.ts
Original file line number Diff line number Diff line change
Expand Up @@ -233,11 +233,13 @@ export function webkitUrlToClientUrl(cwd: string, url: string): string {
* file:///Users/me/project/code.js => /Users/me/project/code.js
* c:\scripts\code.js => c:/scripts/code.js
* http://site.com/scripts/code.js => (no change)
* http://site.com/ => http://site.com
*/
export function canonicalizeUrl(url: string): string {
url = url
.replace('file:///', '')
.replace(/\\/g, '/'); // \ to /
.replace(/\\/g, '/') // \ to /
.replace(/\/$/, ''); // strip trailing slash

// Ensure osx path starts with /, it can be removed when file:/// was stripped.
// Don't add if the url still has a protocol
Expand Down
51 changes: 32 additions & 19 deletions webkit/webKitConnection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -120,35 +120,48 @@ export class WebKitConnection {
/**
* Attach the websocket to the first available tab in the chrome instance with the given remote debugging port number.
*/
public attach(port: number): Promise<void> {
public attach(port: number, url?: string): Promise<void> {
Logger.log('Attempting to attach on port ' + port);
return utils.retryAsync(() => this._attach(port), 6000)
return utils.retryAsync(() => this._attach(port, url), 6000)
.then(() => this.sendMessage('Debugger.enable'))
.then(() => this.sendMessage('Console.enable'))
.then(() => { });
}

public _attach(port: number): Promise<void> {
return getUrl(`http://127.0.0.1:${port}/json`)
.then(jsonResponse => {
// Validate every step of processing the response
try {
const responseArray = JSON.parse(jsonResponse);
if (Array.isArray(responseArray)) {
const pages = responseArray.filter(target => target && target.type === 'page');
if (pages.length) {
const wsUrl = pages[0].webSocketDebuggerUrl;
if (wsUrl) {
return this._socket.attach(wsUrl);
}
public _attach(port: number, url?: string): Promise<void> {
return getUrl(`http://127.0.0.1:${port}/json`).then(jsonResponse => {
// Validate every step of processing the response
try {
const responseArray = JSON.parse(jsonResponse);
if (Array.isArray(responseArray)) {
let pages = responseArray.filter(target => target && target.type === 'page');
if (url) {
const urlPages = pages.filter(page => utils.canonicalizeUrl(page.url) === utils.canonicalizeUrl(url));
if (!urlPages.length) {
Logger.log(`Can't find a page with url: ` + url);
} else {
pages = urlPages;
}
}

if (pages.length) {
if (pages.length > 1) {
Logger.log('Warning! Found more than one valid target page. Attaching to the first one.');
Logger.log('pages: ' + JSON.stringify(pages.map(page => page.url)));
}

const wsUrl = pages[0].webSocketDebuggerUrl;
if (wsUrl) {
return this._socket.attach(wsUrl);
}
}
} catch (e) {
// JSON.parse can throw
}
} catch (e) {
// JSON.parse can throw
}

return utils.errP('Got response, but no valid target pages found');
});
return utils.errP('Got response, but no valid target pages found');
});
}

public close(): void {
Expand Down
17 changes: 9 additions & 8 deletions webkit/webKitDebugAdapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import {formatConsoleMessage} from './consoleHelper';

import {spawn, ChildProcess} from 'child_process';
import * as path from 'path';
import * as os from 'os';

export class WebKitDebugAdapter implements IDebugAdapter {
private static THREAD_ID = 1;
Expand Down Expand Up @@ -82,28 +81,30 @@ export class WebKitDebugAdapter implements IDebugAdapter {
const port = args.port || 9222;
const chromeArgs: string[] = ['--remote-debugging-port=' + port];

// Also start with extra stuff disabled, and user-data-dir in tmp directory
chromeArgs.push(...['--no-first-run', '--no-default-browser-check', `--user-data-dir=${os.tmpdir() }/webkitdebugadapter${Date.now() }`]);
// Also start with extra stuff disabled
chromeArgs.push(...['--no-first-run', '--no-default-browser-check']);
if (args.runtimeArguments) {
chromeArgs.push(...args.runtimeArguments);
}

let launchUrl: string;
if (args.file) {
chromeArgs.push(path.resolve(args.cwd, args.file));
launchUrl = path.resolve(args.cwd, args.file);
} else if (args.url) {
chromeArgs.push(args.url);
launchUrl = args.url;
} else {
return utils.errP('The launch config must specify either the "file" or "url" field.');
}

chromeArgs.push(launchUrl);
Logger.log(`spawn('${chromePath}', ${JSON.stringify(chromeArgs) })`);
this._chromeProc = spawn(chromePath, chromeArgs);
this._chromeProc.on('error', (err) => {
Logger.log('chrome error: ' + err);
this.terminateSession();
});

return this._attach(port);
return this._attach(port, launchUrl);
}

public attach(args: IAttachRequestArgs): Promise<void> {
Expand All @@ -122,7 +123,7 @@ export class WebKitDebugAdapter implements IDebugAdapter {
return this._attach(args.port);
}

private _attach(port: number): Promise<void> {
private _attach(port: number, url?: string): Promise<void> {
// ODP client is attaching - if not attached to the webkit target, create a connection and attach
this._clientAttached = true;
if (!this._webKitConnection) {
Expand All @@ -139,7 +140,7 @@ export class WebKitDebugAdapter implements IDebugAdapter {
this._webKitConnection.on('close', () => this.terminateSession());
this._webKitConnection.on('error', () => this.terminateSession());

return this._webKitConnection.attach(port)
return this._webKitConnection.attach(port, url)
.then(
() => this.fireEvent(new InitializedEvent()),
e => {
Expand Down

0 comments on commit aee6dd2

Please sign in to comment.