forked from smithclay/lambdium
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
47 lines (38 loc) · 1.33 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
const chromium = require('./lib/chromium');
const sandbox = require('./lib/sandbox');
const log = require('lambda-log');
const apiHandler = require('./lib/api-handler');
if (process.env.DEBUG_ENV || process.env.SAM_LOCAL) {
log.config.debug = true;
log.config.dev = true;
}
log.info('Loading function');
// Create new reusable session (spawns chromium and webdriver)
if (!process.env.CLEAN_SESSIONS) {
$browser = chromium.createSession();
}
// Handler for POST events from API gateway
// curl -v -F "script=@examples/visitgoogle.js" <<API Gateway URL>>
exports.postApiGatewayHandler = apiHandler;
// Default function event handler
// Accepts events:
// * {"Base64Script": "<<encoded selenium script>>"}
// * {"pageUrl": "<<URI to visit>>"}
// Accepts environment variables:
// * BASE64_SCRIPT: encoded selenium script
// * PAGE_URL: URI to visit
exports.handler = (event, context, callback) => {
$browser = sandbox.initBrowser(event, context);
var opts = sandbox.buildOptions(event, $browser);
sandbox.executeScript(opts, function(err) {
if (process.env.LOG_DEBUG) {
log.debug(child.execSync('ps aux').toString());
log.debug(child.execSync('cat /tmp/chromedriver.log').toString())
}
if (err) {
log.error(err);
return callback(err, null);
}
callback(null, 'Finished executing script');
});
};