Skip to content

Commit 2bc4fac

Browse files
committed
initial codebase setup
0 parents  commit 2bc4fac

9 files changed

+158
-0
lines changed

.gitignore

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
node_modules
2+
.env

README.md

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
# CasperJS AWS Lambda Template
2+
3+
A [CasperJS](http://casperjs.org/) node.js app for [Amazon Lambda](http://aws.amazon.com/lambda/).
4+
Based on [node-lambda-template](https://github.com/rebelmail/node-lambda-template) using [node-lambda](https://github.com/rebelmail/node-lambda).
5+
The app includes a PhantomJS binary (`phantomjs`) compiled for AWS Linux (https://bitbucket.org/ariya/phantomjs/downloads/phantomjs-1.9.8-linux-x86_64.tar.bz2).
6+
7+
## Setup
8+
9+
Install dependencies using npm. It'll install the AWS SDK as well as PhantomJS on the development machine.
10+
11+
```shell
12+
npm install
13+
```
14+
15+
## Usage
16+
17+
After installing use the following `npm` commands as described below. They're only wrapping the `node-lambda` functionality to allow `node-lambda` to be installed only locally. Additional params can be provided using `-- args`. For a list of available options see the `node-lambda` [documentation](https://github.com/RebelMail/node-lambda).
18+
19+
Run the setup command to generate the environment file with the configuration used for the Amazon Lambda function. Edit the resulting `.env.` file with your custom settings.
20+
```shell
21+
npm run setup
22+
```
23+
24+
To run the function locally execute the following command.
25+
```shell
26+
npm run start
27+
```
28+
29+
Run the following command to deploy the app to Amazon Lambda.
30+
```shell
31+
npm run deploy
32+
```
33+
34+
> **Note:** npm version 2.x or newer required to pass arguments to the scripts using `-- args`

casperjs-script.js

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
// Simple Javascript example
2+
var casper = require('casper').create();
3+
var url = 'http://casperjs.org/';
4+
5+
console.log('Loading a web page: ' + url);
6+
7+
casper.start(url, function() {
8+
this.echo('Page title is: ' + this.getTitle());
9+
});
10+
11+
casper.run();

deploy.env

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
EXEC_MODE=lambda

event.json

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"key": "value",
3+
"key2": "value2",
4+
"other_key": "other_value"
5+
}

index.js

+47
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
var path = require('path'),
2+
fs = require('fs'),
3+
childProcess = require('child_process');
4+
5+
// Get the path to the phantomjs application
6+
function getPhantomFileName() {
7+
var phantomPath = path.join(__dirname, 'node_modules', 'phantomjs', 'bin', 'phantomjs');
8+
if (process.env.DEBUG && fs.existsSync(phantomPath)) {
9+
return phantomPath;
10+
}
11+
return path.join(__dirname, 'phantomjs');
12+
}
13+
14+
// Call the casperJS script
15+
function runCasper(scriptName, callback) {
16+
var casperPath = path.join(__dirname, 'node_modules', 'casperjs', 'bin', 'casperjs');
17+
var outputData = [];
18+
var error = null;
19+
var childArgs = [
20+
path.join(__dirname, scriptName)
21+
];
22+
23+
console.log('Calling casperJS: ', casperPath, childArgs);
24+
25+
var ps = childProcess.execFile(casperPath, childArgs);
26+
27+
ps.stdout.on('data', function (data) {
28+
console.log(data);
29+
outputData.push(data);
30+
});
31+
32+
ps.stderr.on('data', function (data) {
33+
console.log('casper error ---:> ' + data);
34+
error = new Error(data);
35+
});
36+
37+
ps.on('exit', function (code) {
38+
console.log('child process exited with code ' + code);
39+
callback(error, outputData);
40+
});
41+
}
42+
43+
// Entry Point
44+
exports.handler = function (event, context) {
45+
// Execute the casperJS call and exit
46+
runCasper('casperjs-script.js', context.done);
47+
};

package.json

+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
{
2+
"name": "node-casperjs-aws-lambda",
3+
"version": "0.0.2",
4+
"description": "The bare minimum for a phantomjs app running on Amazon Lambda.",
5+
"main": "index.js",
6+
"repository": {
7+
"type": "git",
8+
"url": "git://github.com/narainsagar/node-casperjs-aws-lambda.git"
9+
},
10+
"author": "Narain Sagar",
11+
"license": "isc",
12+
"bugs": {
13+
"url": "https://github.com/narainsagar/node-casperjs-aws-lambda/issues"
14+
},
15+
"homepage": "https://github.com/narainsagar/node-casperjs-aws-lambda",
16+
"dependencies": {
17+
"casperjs": "^1.1.0-beta3",
18+
},
19+
"devDependencies": {
20+
"aws-sdk": "^2.1.39",
21+
"node-lambda": "^0.6.1",
22+
"phantomjs": "^1.9.17",
23+
"mocha": "^2.3.2"
24+
},
25+
"scripts": {
26+
"setup": "node-lambda setup",
27+
"start": "node-lambda run",
28+
"deploy": "node-lambda deploy",
29+
"test": "mocha test/*"
30+
},
31+
"keywords": [
32+
"phantomjs",
33+
"aws",
34+
"lambda"
35+
]
36+
}

phantomjs

36.6 MB
Binary file not shown.

test/basic.js

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
var assert = require('assert');
2+
3+
describe('basic', function () {
4+
this.timeout(60000);
5+
it('Should create', function (testDone) {
6+
var event = require('../event.json');
7+
8+
var context = {
9+
invokeid: 'invokeid',
10+
done: function (message) {
11+
testDone();
12+
},
13+
succeed: function (message) {
14+
testDone();
15+
}
16+
};
17+
18+
var lambda = require("../index.js");
19+
lambda.handler(event, context);
20+
assert(lambda);
21+
});
22+
});

0 commit comments

Comments
 (0)