Skip to content

Commit

Permalink
Chore: Unblock spinner
Browse files Browse the repository at this point in the history
Fix #485
  • Loading branch information
qzhou1607-zz committed Sep 6, 2017
1 parent 574b210 commit 536d72f
Show file tree
Hide file tree
Showing 3 changed files with 84 additions and 13 deletions.
41 changes: 41 additions & 0 deletions src/lib/connectors/jsdom/evaluate-runner.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import * as vm from 'vm';

import * as jsdom from 'jsdom/lib/old-api';
import * as jsdomutils from 'jsdom/lib/jsdom/living/generated/utils';

const run = (data) => {
const { source, url } = data;
const result = {
error: null,
evaluate: 'result'
};

jsdom.env({
done: async (error, window) => {
if (error) {
result.error = error;

return process.send(result);
}

try {
const script: vm.Script = new vm.Script(source);
const evaluteResult = await script.runInContext(jsdomutils.implForWrapper(window.document)._global);

result.evaluate = evaluteResult;
} catch (err) {
result.error = err;
}

return process.send(result);
},
features: {
FetchExternalResources: ['script', 'link', 'img'],
ProcessExternalResources: ['script'],
SkipExternalResources: false
},
url
});
};

process.on('message', run);
48 changes: 39 additions & 9 deletions src/lib/connectors/jsdom/jsdom.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,9 @@
import * as path from 'path';
import * as url from 'url';
import * as util from 'util';
import * as vm from 'vm';
import { fork, ChildProcess } from 'child_process';

import * as jsdom from 'jsdom/lib/old-api';
import * as jsdomutils from 'jsdom/lib/jsdom/living/generated/utils';

import { debug as d } from '../../utils/debug';
import { resolveUrl } from '../utils/resolver';
Expand Down Expand Up @@ -464,15 +463,46 @@ class JSDOMConnector implements IConnector {
return this._fetchUrl(parsedTarget, customHeaders);
}

public evaluate(source: string): Promise<any> {
const script: vm.Script = new vm.Script(source);
const result = script.runInContext(jsdomutils.implForWrapper(this._window.document)._global, { timeout: 60000 });

if (result[Symbol.toStringTag] === 'Promise') {
return result;
private killProcess = (runner: ChildProcess) => {
try {
runner.kill('SIGKILL');
} catch (err) {
debug('Error closing evaluate process');
}
};

public evaluate(source: string, waitBeforeTimeOut: number = 60000): Promise<any> {
return new Promise((resolve, reject) => {
const runner: ChildProcess = fork(path.join(__dirname, 'evaluate-runner'), [], { execArgv: [] });
let timeoutId;

runner.on('message', (result) => {
if (timeoutId) {
clearTimeout(timeoutId);
timeoutId = null;
}

this.killProcess(runner);

if (result.error) {
return reject(result.error);
}

return Promise.resolve(result);
return resolve(result.evaluate);
});

runner.send({
source,
url: this._finalHref
});

timeoutId = setTimeout(() => {
debug(`Evaluation times out. Killing process and reporting and error.`);
this.killProcess(runner);

return reject(new Error('TIMEOUT'));
}, waitBeforeTimeOut);
});
}

public querySelectorAll(selector: string): Promise<Array<JSDOMAsyncHTMLElement>> {
Expand Down
8 changes: 4 additions & 4 deletions src/lib/rules/axe/axe.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import { AxeResults, Result as AxeResult, NodeResult as AxeNodeResult } from 'ax

import { readFileAsync } from '../../utils/misc';
import { debug as d } from '../../utils/debug';
import { IAsyncHTMLElement, ITraverseEnd, IRule, IRuleBuilder, Severity } from '../../types'; // eslint-disable-line no-unused-vars
import { IAsyncHTMLElement, IRule, IRuleBuilder, Severity, ITraverseStart } from '../../types'; // eslint-disable-line no-unused-vars
import { RuleContext } from '../../rule-context'; // eslint-disable-line no-unused-vars

const debug = d(__filename);
Expand Down Expand Up @@ -52,8 +52,8 @@ const rule: IRuleBuilder = {
return elements[0];
};

const validate = async (traverseEnd: ITraverseEnd) => {
const { resource } = traverseEnd;
const validate = async (traverseStart: ITraverseStart) => {
const { resource } = traverseStart;
const axeCore: string = await readFileAsync(require.resolve('axe-core'));
const script: string = `(function () {
${axeCore};
Expand Down Expand Up @@ -104,7 +104,7 @@ const rule: IRuleBuilder = {

loadRuleConfig();

return { 'traverse::end': validate };
return { 'traverse::start': validate };
},
meta: {
docs: {
Expand Down

0 comments on commit 536d72f

Please sign in to comment.