Skip to content

Instantly share code, notes, and snippets.

@dimkir
Created March 13, 2017 04:36
Show Gist options
  • Save dimkir/4637f70b08145905dfd4b235fcaad603 to your computer and use it in GitHub Desktop.
Save dimkir/4637f70b08145905dfd4b235fcaad603 to your computer and use it in GitHub Desktop.
Using NigthmareJS on AWS Lambda
'use strict';
// Code in global scope (outside of event) runs once, when container is first created
var pack = require('./lib/nightmare-lambda-pack');
var electronPath = pack.installNightmare(); // this will install the electron on the Lambda
var Xvfb = require('./lib/xvfb');
var Snapper = require('./lib/nightmare-snapper');
exports.handler = function(event, context){
// how do I know that by the time this was run, the pack was installed?
var url, xvfb, snapper;
if ( !event.url ){
context.done('Error please specify .url property on the event');
return;
}
url = event.url;
xvfb = new Xvfb({
xvfb_executable : '/tmp/app/Xvfb' // will be part of package together with Electron
})
xvfb.start((err, xvfbProcess) => {
if (err){
console.error('Error starting xvfb', err);
return context.done(err);
}
snapper = new Snapper({
destBucket: 'rojet',
destBucketRegion: 'eu-west-1', // this is used to generate full screenshot url in form http://s3-region.amazonaws.com/key/screenshot-xxxx.png,
nightmareOptions: {
electronPath: electronPath
}
});
snapper.snap({ url: url })
.then((result) => {
console.log(result);
stopXvfbAndFinish(null, result);
}
, (err) => {
console.error(err);
stopXvfbAndFinish(err);
});
function stopXvfbAndFinish(err, result){
xvfb.stop((err) => {
context.done(err, result);
});
}
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment