From c3037f633f555228593551b3ad108761d6f03c1a Mon Sep 17 00:00:00 2001 From: Daniel Weck Date: Wed, 1 May 2019 19:26:34 +0100 Subject: [PATCH 01/10] Axe runner Puppeteer --- packages/ace-cli/package.json | 1 + packages/ace-cli/src/index.js | 4 ++- packages/ace-core/package.json | 2 -- .../ace-core/src/checker/checker-chromium.js | 32 ++++--------------- packages/ace-core/src/checker/checker.js | 4 +-- packages/ace-core/src/core/ace.js | 4 +-- packages/ace-http/package.json | 1 + packages/ace-http/src/index.js | 4 ++- tests/runAceJS.js | 4 ++- 9 files changed, 21 insertions(+), 35 deletions(-) diff --git a/packages/ace-cli/package.json b/packages/ace-cli/package.json index 8c8d22c8..610a8e11 100644 --- a/packages/ace-cli/package.json +++ b/packages/ace-cli/package.json @@ -18,6 +18,7 @@ "main": "lib/index.js", "bin": "bin/ace.js", "dependencies": { + "@daisy/ace-axe-runner-puppeteer": "^1.0.0", "@daisy/ace-config": "^1.0.0", "@daisy/ace-core": "^1.0.2", "@daisy/ace-logger": "^1.0.1", diff --git a/packages/ace-cli/src/index.js b/packages/ace-cli/src/index.js index 3a739f7c..a0a9671f 100755 --- a/packages/ace-cli/src/index.js +++ b/packages/ace-cli/src/index.js @@ -5,6 +5,8 @@ const meow = require('meow'); const path = require('path'); const winston = require('winston'); +const axeRunner = require('@daisy/ace-axe-runner-puppeteer'); + const logger = require('@daisy/ace-logger'); const ace = require('@daisy/ace-core'); @@ -102,7 +104,7 @@ ${overrides.map(file => ` - ${file}`).join('\n')} verbose: cli.flags.verbose, silent: cli.flags.silent, jobId: '', - }) + }, axeRunner) .then((jobData) => { var reportJson = jobData[1]; // if there were violations from the validation process, return 2 diff --git a/packages/ace-core/package.json b/packages/ace-core/package.json index 2b46f55d..847b7e27 100644 --- a/packages/ace-core/package.json +++ b/packages/ace-core/package.json @@ -21,12 +21,10 @@ "@daisy/ace-report": "^1.0.1", "@daisy/ace-report-axe": "^1.0.1", "@daisy/epub-utils": "^1.0.2", - "@daisy/puppeteer-utils": "^1.0.0", "axe-core": "~2.6.1", "file-url": "^2.0.2", "h5o": "^0.11.3", "p-map": "^1.2.0", - "puppeteer": "^1.0.0", "tmp": "^0.0.33", "winston": "^2.4.0" }, diff --git a/packages/ace-core/src/checker/checker-chromium.js b/packages/ace-core/src/checker/checker-chromium.js index 77743c73..6e332b62 100644 --- a/packages/ace-core/src/checker/checker-chromium.js +++ b/packages/ace-core/src/checker/checker-chromium.js @@ -4,13 +4,11 @@ const fileUrl = require('file-url'); const fs = require('fs-extra'); const path = require('path'); const pMap = require('p-map'); -const puppeteer = require('puppeteer'); const os = require('os'); const tmp = require('tmp'); const winston = require('winston'); const axe2ace = require('@daisy/ace-report-axe'); -const utils = require('@daisy/puppeteer-utils'); tmp.setGracefulCleanup(); @@ -22,7 +20,7 @@ const scripts = [ require.resolve('../scripts/ace-extraction.js'), ]; -async function checkSingle(spineItem, epub, browser) { +async function checkSingle(spineItem, epub, axeRunner) { winston.verbose(`- Processing ${spineItem.relpath}`); try { let url = spineItem.url; @@ -39,21 +37,7 @@ async function checkSingle(spineItem, epub, browser) { winston.debug(`checking copied file at ${url}`) } - const page = await browser.newPage(); - await page.goto(url); - await utils.addScripts(scripts, page); - - const results = await page.evaluate(() => new Promise((resolve, reject) => { - /* eslint-disable */ - window.daisy.ace.run((err, res) => { - if (err) { - return reject(err); - } - return resolve(res); - }); - /* eslint-enable */ - })); - await page.close(); + const results = await axeRunner.run(url, scripts); // Post-process results results.assertions = (results.axe != null) ? axe2ace.axe2ace(spineItem, results.axe) : []; @@ -96,16 +80,12 @@ async function checkSingle(spineItem, epub, browser) { } } -module.exports.check = async (epub) => { - const args = []; - if (os.platform() !== 'win32' && os.platform() !== 'darwin') { - args.push('--no-sandbox') - } - const browser = await puppeteer.launch({ args }); +module.exports.check = async (epub, axeRunner) => { + await axeRunner.launch(); winston.info('Checking documents...'); - return pMap(epub.contentDocs, doc => checkSingle(doc, epub, browser), { concurrency: 4 }) + return pMap(epub.contentDocs, doc => checkSingle(doc, epub, axeRunner), { concurrency: axeRunner.concurrency }) .then(async (results) => { - await browser.close(); + await axeRunner.close(); return results; }); }; diff --git a/packages/ace-core/src/checker/checker.js b/packages/ace-core/src/checker/checker.js index b4febd68..daa51b72 100644 --- a/packages/ace-core/src/checker/checker.js +++ b/packages/ace-core/src/checker/checker.js @@ -23,8 +23,8 @@ function consolidate(results, report) { return report; } -module.exports.check = function check(epub, report) { +module.exports.check = function check(epub, report, axeRunner) { return epubChecker.check(epub, report) - .then(() => htmlChecker.check(epub)) + .then(() => htmlChecker.check(epub, axeRunner)) .then(results => consolidate(results, report)); }; diff --git a/packages/ace-core/src/core/ace.js b/packages/ace-core/src/core/ace.js index c6f1aaf1..4ae5b1b9 100644 --- a/packages/ace-core/src/core/ace.js +++ b/packages/ace-core/src/core/ace.js @@ -13,7 +13,7 @@ const checker = require('../checker/checker.js'); tmp.setGracefulCleanup(); -module.exports = function ace(epubPath, options) { +module.exports = function ace(epubPath, options, axeRunner) { return new Promise((resolve, reject) => { // the jobid option just gets returned in the resolve/reject // so the calling function can track which job finished @@ -58,7 +58,7 @@ module.exports = function ace(epubPath, options) { // initialize the report .then(() => new Report(epub, options.outdir)) // Check each Content Doc - .then(report => checker.check(epub, report)) + .then(report => checker.check(epub, report, axeRunner)) // Process the Results .then((report) => { if (options.outdir === undefined) { diff --git a/packages/ace-http/package.json b/packages/ace-http/package.json index df23b8e0..ea47731c 100644 --- a/packages/ace-http/package.json +++ b/packages/ace-http/package.json @@ -18,6 +18,7 @@ "main": "lib/index.js", "bin": "bin/ace-http.js", "dependencies": { + "@daisy/ace-axe-runner-puppeteer": "^1.0.0", "@daisy/ace-core": "^1.0.2", "@daisy/ace-logger": "^1.0.1", "@daisy/ace-meta": "^1.0.3", diff --git a/packages/ace-http/src/index.js b/packages/ace-http/src/index.js index 6631c64a..6d4fc43f 100644 --- a/packages/ace-http/src/index.js +++ b/packages/ace-http/src/index.js @@ -12,6 +12,8 @@ const meow = require('meow'); const ace = require('@daisy/ace-core'); const logger = require('@daisy/ace-logger'); +const axeRunner = require('@daisy/ace-axe-runner-puppeteer'); + const pkg = require('@daisy/ace-meta/package'); const UPLOADS = tmp.dirSync({ unsafeCleanup: true }).name; @@ -156,7 +158,7 @@ function newJob(jobdata) { joblist.push(jobdata); // execute the job with Ace - ace(jobdata.internal.epubPath, {'jobid': jobdata.internal.id, 'outdir': jobdata.internal.outputDir}) + ace(jobdata.internal.epubPath, {'jobid': jobdata.internal.id, 'outdir': jobdata.internal.outputDir}, axeRunner) .then((jobData) => { var jobId = jobData[0]; var idx = joblist.findIndex(job => job.internal.id === jobId); diff --git a/tests/runAceJS.js b/tests/runAceJS.js index 8cd372d9..199ae57e 100644 --- a/tests/runAceJS.js +++ b/tests/runAceJS.js @@ -3,6 +3,8 @@ const ace = require('@daisy/ace-core'); const logger = require('@daisy/ace-logger'); +const axeRunner = require('@daisy/ace-axe-runner-puppeteer'); + function runAce(epub, { cwd = process.cwd(), outdir, @@ -17,7 +19,7 @@ function runAce(epub, { tmpdir, verbose, silent, - }); + }, axeRunner); } module.exports = runAce; From ce7d1e462ac4f234be8fd3bf3f27d1e15479e199 Mon Sep 17 00:00:00 2001 From: Daniel Weck Date: Wed, 1 May 2019 19:28:28 +0100 Subject: [PATCH 02/10] fixed import --- packages/ace-core/src/checker/checker-chromium.js | 1 - 1 file changed, 1 deletion(-) diff --git a/packages/ace-core/src/checker/checker-chromium.js b/packages/ace-core/src/checker/checker-chromium.js index 6e332b62..c9495070 100644 --- a/packages/ace-core/src/checker/checker-chromium.js +++ b/packages/ace-core/src/checker/checker-chromium.js @@ -4,7 +4,6 @@ const fileUrl = require('file-url'); const fs = require('fs-extra'); const path = require('path'); const pMap = require('p-map'); -const os = require('os'); const tmp = require('tmp'); const winston = require('winston'); From c59c47e533c6f545a00c3b3bd30661c3f46af7a8 Mon Sep 17 00:00:00 2001 From: Daniel Weck Date: Wed, 1 May 2019 19:31:13 +0100 Subject: [PATCH 03/10] added missing files --- .../ace-axe-runner-puppeteer/package.json | 27 +++++++++++++ .../ace-axe-runner-puppeteer/src/index.js | 39 +++++++++++++++++++ 2 files changed, 66 insertions(+) create mode 100644 packages/ace-axe-runner-puppeteer/package.json create mode 100644 packages/ace-axe-runner-puppeteer/src/index.js diff --git a/packages/ace-axe-runner-puppeteer/package.json b/packages/ace-axe-runner-puppeteer/package.json new file mode 100644 index 00000000..03c2b283 --- /dev/null +++ b/packages/ace-axe-runner-puppeteer/package.json @@ -0,0 +1,27 @@ +{ + "name": "@daisy/ace-axe-runner-puppeteer", + "version": "1.0.0", + "description": "Abstract Axe runner for Ace", + "author": { + "name": "DAISY developers", + "organization": "DAISY Consortium", + "url": "http://www.daisy.org/" + }, + "repository": { + "type": "git", + "url": "https://github.com/daisy/ace" + }, + "bugs": { + "url": "https://github.com/daisy/ace/issues" + }, + "license": "MIT", + "main": "lib/index.js", + "dependencies": { + "@daisy/ace-core": "^1.0.0", + "@daisy/puppeteer-utils": "^1.0.0", + "puppeteer": "^1.0.0" + }, + "publishConfig": { + "access": "public" + } +} diff --git a/packages/ace-axe-runner-puppeteer/src/index.js b/packages/ace-axe-runner-puppeteer/src/index.js new file mode 100644 index 00000000..d78aad08 --- /dev/null +++ b/packages/ace-axe-runner-puppeteer/src/index.js @@ -0,0 +1,39 @@ +'use strict'; + +const os = require('os'); +const puppeteer = require('puppeteer'); +const utils = require('@daisy/puppeteer-utils'); + +let _browser = undefined; + +module.exports = { + concurrency: 4, + launch: async function() { + const args = []; + if (os.platform() !== 'win32' && os.platform() !== 'darwin') { + args.push('--no-sandbox') + } + _browser = await puppeteer.launch({ args }); + }, + close: async function() { + _browser.close(); + }, + run: async function(url, scripts) { + const page = await _browser.newPage(); + await page.goto(url); + await utils.addScripts(scripts, page); + + const results = await page.evaluate(() => new Promise((resolve, reject) => { + /* eslint-disable */ + window.daisy.ace.run((err, res) => { + if (err) { + return reject(err); + } + return resolve(res); + }); + /* eslint-enable */ + })); + await page.close(); + return results; + } +}; \ No newline at end of file From 1b03b0b2529a136fbe160dd025a59e57717abc4a Mon Sep 17 00:00:00 2001 From: Daniel Weck Date: Wed, 1 May 2019 19:34:19 +0100 Subject: [PATCH 04/10] fixed missing await (async) --- packages/ace-axe-runner-puppeteer/src/index.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/ace-axe-runner-puppeteer/src/index.js b/packages/ace-axe-runner-puppeteer/src/index.js index d78aad08..e54cd44d 100644 --- a/packages/ace-axe-runner-puppeteer/src/index.js +++ b/packages/ace-axe-runner-puppeteer/src/index.js @@ -16,7 +16,7 @@ module.exports = { _browser = await puppeteer.launch({ args }); }, close: async function() { - _browser.close(); + await _browser.close(); }, run: async function(url, scripts) { const page = await _browser.newPage(); From fc0718e334c7a2d33239bca004095f2a5a32081b Mon Sep 17 00:00:00 2001 From: Daniel Weck Date: Wed, 1 May 2019 19:35:39 +0100 Subject: [PATCH 05/10] removed unused package dep --- packages/ace-axe-runner-puppeteer/package.json | 1 - 1 file changed, 1 deletion(-) diff --git a/packages/ace-axe-runner-puppeteer/package.json b/packages/ace-axe-runner-puppeteer/package.json index 03c2b283..a43b0a47 100644 --- a/packages/ace-axe-runner-puppeteer/package.json +++ b/packages/ace-axe-runner-puppeteer/package.json @@ -17,7 +17,6 @@ "license": "MIT", "main": "lib/index.js", "dependencies": { - "@daisy/ace-core": "^1.0.0", "@daisy/puppeteer-utils": "^1.0.0", "puppeteer": "^1.0.0" }, From 4746a491a214dc35d378c45a4a40715736a73ef0 Mon Sep 17 00:00:00 2001 From: Daniel Weck Date: Thu, 2 May 2019 21:13:49 +0100 Subject: [PATCH 06/10] filter out empty reports --- packages/ace-core/src/checker/checker.js | 28 +++++++++++++++++++----- 1 file changed, 23 insertions(+), 5 deletions(-) diff --git a/packages/ace-core/src/checker/checker.js b/packages/ace-core/src/checker/checker.js index daa51b72..d6093447 100644 --- a/packages/ace-core/src/checker/checker.js +++ b/packages/ace-core/src/checker/checker.js @@ -8,17 +8,35 @@ function consolidate(results, report) { winston.info('Consolidating results...'); // Integrate checker results to the report results.forEach((res) => { - report.addAssertions(res.assertions); - report.addProperties(res.properties); - report.addData(res.data); + if (res.assertions) { + report.addAssertions(res.assertions); + } + if (res.properties) { + report.addProperties(res.properties); + } + if (res.data) { + report.addData(res.data); + } }); // Get a flat array of all the headings in the documents const headings = [] - .concat(...results.map(docResult => docResult.outlines.headings)) + .concat(...results.map(docResult => { + if (docResult.outlines && docResult.outlines.headings) { + return docResult.outlines.headings; + } + return undefined; + })) .filter(e => e !== undefined); report.addHeadings(headings); // Aggregated array of the HTML outlines - const htmlOutlines = results.map(docResult => docResult.outlines.html); + const htmlOutlines = [] + .concat(results.map(docResult => { + if (docResult.outlines && docResult.outlines.html) { + return docResult.outlines.html; + } + return undefined; + })) + .filter(e => e !== undefined); report.addHTMLOutlines(htmlOutlines); return report; } From b18147b1f7c5640c15e47ee9a4b1395abfc34db4 Mon Sep 17 00:00:00 2001 From: Daniel Weck Date: Thu, 2 May 2019 21:14:07 +0100 Subject: [PATCH 07/10] Axe runner now take EPUB basedir param (tmp dir) --- packages/ace-axe-runner-puppeteer/src/index.js | 2 +- packages/ace-core/src/checker/checker-chromium.js | 8 ++++++-- 2 files changed, 7 insertions(+), 3 deletions(-) diff --git a/packages/ace-axe-runner-puppeteer/src/index.js b/packages/ace-axe-runner-puppeteer/src/index.js index e54cd44d..817883ad 100644 --- a/packages/ace-axe-runner-puppeteer/src/index.js +++ b/packages/ace-axe-runner-puppeteer/src/index.js @@ -18,7 +18,7 @@ module.exports = { close: async function() { await _browser.close(); }, - run: async function(url, scripts) { + run: async function(url, scripts, basedir) { const page = await _browser.newPage(); await page.goto(url); await utils.addScripts(scripts, page); diff --git a/packages/ace-core/src/checker/checker-chromium.js b/packages/ace-core/src/checker/checker-chromium.js index c9495070..60866895 100644 --- a/packages/ace-core/src/checker/checker-chromium.js +++ b/packages/ace-core/src/checker/checker-chromium.js @@ -36,13 +36,13 @@ async function checkSingle(spineItem, epub, axeRunner) { winston.debug(`checking copied file at ${url}`) } - const results = await axeRunner.run(url, scripts); + const results = await axeRunner.run(url, scripts, epub.basedir); // Post-process results results.assertions = (results.axe != null) ? axe2ace.axe2ace(spineItem, results.axe) : []; delete results.axe; winston.info(`- ${spineItem.relpath}: ${ - (results.assertions && results.assertions.assertions.length > 0) + (results.assertions && results.assertions.assertions && results.assertions.assertions.length > 0) ? results.assertions.assertions.length : 'No'} issues found`); // Resolve path and locators for extracted data @@ -86,5 +86,9 @@ module.exports.check = async (epub, axeRunner) => { .then(async (results) => { await axeRunner.close(); return results; + }).catch(async (err) => { + winston.info(`Error HTML check: ${err}`); + await axeRunner.close(); + return {}; }); }; From 8f068aa7d11265b99e4ec5bfc9cbe5f2f63e98da Mon Sep 17 00:00:00 2001 From: Daniel Weck Date: Thu, 2 May 2019 22:10:04 +0100 Subject: [PATCH 08/10] typo --- packages/ace-core/src/checker/checker-chromium.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/ace-core/src/checker/checker-chromium.js b/packages/ace-core/src/checker/checker-chromium.js index 60866895..ee12fc9f 100644 --- a/packages/ace-core/src/checker/checker-chromium.js +++ b/packages/ace-core/src/checker/checker-chromium.js @@ -89,6 +89,6 @@ module.exports.check = async (epub, axeRunner) => { }).catch(async (err) => { winston.info(`Error HTML check: ${err}`); await axeRunner.close(); - return {}; + return []; }); }; From 5fe4ec3772886cf35ec0e439910eacaaf66229fd Mon Sep 17 00:00:00 2001 From: Daniel Weck Date: Fri, 3 May 2019 11:41:25 +0100 Subject: [PATCH 09/10] version bumps in modified packages NPM --- packages/ace-cli/package.json | 2 +- packages/ace-core/package.json | 2 +- packages/ace-http/package.json | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/packages/ace-cli/package.json b/packages/ace-cli/package.json index 610a8e11..a9ea4563 100644 --- a/packages/ace-cli/package.json +++ b/packages/ace-cli/package.json @@ -1,6 +1,6 @@ { "name": "@daisy/ace-cli", - "version": "1.0.3", + "version": "1.0.4", "description": "Ace by DAISY, an Accessibility Checker for EPUB", "author": { "name": "DAISY developers", diff --git a/packages/ace-core/package.json b/packages/ace-core/package.json index 2f107d78..253854a4 100644 --- a/packages/ace-core/package.json +++ b/packages/ace-core/package.json @@ -1,6 +1,6 @@ { "name": "@daisy/ace-core", - "version": "1.0.3", + "version": "1.0.4", "description": "Core library for Ace", "author": { "name": "DAISY developers", diff --git a/packages/ace-http/package.json b/packages/ace-http/package.json index ea47731c..858cd054 100644 --- a/packages/ace-http/package.json +++ b/packages/ace-http/package.json @@ -1,6 +1,6 @@ { "name": "@daisy/ace-http", - "version": "1.0.1", + "version": "1.0.2", "description": "HTTP API for Ace", "author": { "name": "DAISY developers", From 39fb2bb3dd6fabc0193e263716891030833c6fb7 Mon Sep 17 00:00:00 2001 From: Daniel Weck Date: Sun, 5 May 2019 20:17:12 +0100 Subject: [PATCH 10/10] package.json directory, https://github.com/daisy/ace/pull/230 --- packages/ace-axe-runner-puppeteer/package.json | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/packages/ace-axe-runner-puppeteer/package.json b/packages/ace-axe-runner-puppeteer/package.json index a43b0a47..6aca4a8f 100644 --- a/packages/ace-axe-runner-puppeteer/package.json +++ b/packages/ace-axe-runner-puppeteer/package.json @@ -9,7 +9,8 @@ }, "repository": { "type": "git", - "url": "https://github.com/daisy/ace" + "url": "https://github.com/daisy/ace", + "directory": "packages/ace-axe-runner-puppeteer" }, "bugs": { "url": "https://github.com/daisy/ace/issues"