Skip to content

Commit

Permalink
validates page capture and cancels upload
Browse files Browse the repository at this point in the history
if the root html capture doesn't return a status code 200, the upload process is canceled
  • Loading branch information
jdub233 committed May 20, 2020
1 parent c5bf567 commit ad351aa
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 0 deletions.
3 changes: 3 additions & 0 deletions handler.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ const s3 = require('s3-node-client');
const del = require('del');
const Url = require('url-parse');

const ValidatePlugin = require('./validatePlugin');

const captureURL = new Url(process.env.CAPTURE_URL);

// Allow for a prefix to the subdirectory, and add a slash if it is set.
Expand All @@ -20,6 +22,7 @@ const scrapeOptions = {
{directory: `${subDirPrefix}css`, extensions: ['.css']},
{directory: `${subDirPrefix}font`, extensions: ['.woff', '.woff2', '.ttf', '.eot']},
],
plugins: [ new ValidatePlugin() ],
};

const client = s3.createClient();
Expand Down
20 changes: 20 additions & 0 deletions validatePlugin.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
module.exports = class ValidatePlugin {
apply(registerAction) {
registerAction('error', async ({error}) => {console.error(error)});
registerAction('onResourceError', ({resource, error}) => console.log(`Resource ${resource.url} has error ${error}`));
registerAction('afterResponse', async ({response}) => {
if (response.statusCode !== 200) {
// Don't capture bad assets. Also cancels the upload phase if the root page has a bad status code.
console.log( `Bad status code ${response.statusCode} , cancelling asset capture` );
return null;
} else {
return {
body: response.body,
metadata: {
headers: response.headers,
}
};
}
});
}
}

0 comments on commit ad351aa

Please sign in to comment.