Skip to content

Commit 250e4bd

Browse files
committed
add flag that forces clean sessions per invocation
1 parent 2da4f57 commit 250e4bd

File tree

2 files changed

+23
-10
lines changed

2 files changed

+23
-10
lines changed

index.js

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,16 +8,18 @@ const log = require('lambda-log');
88

99
log.info('Loading function');
1010

11-
// Create new session (spawns chromium and webdriver)
12-
$browser = chromium.createSession();
11+
// Create new reusable session (spawns chromium and webdriver)
12+
if (!process.env.CLEAN_SESSIONS) {
13+
$browser = chromium.createSession();
14+
}
1315

1416
exports.handler = (event, context, callback) => {
1517
context.callbackWaitsForEmptyEventLoop = false;
1618

17-
/*if (process.env.CLEAR_TMP) {
19+
if (process.env.CLEAN_SESSIONS) {
1820
log.info('attempting to clear /tmp directory')
1921
log.info(child.execSync('rm -rf /tmp/core*').toString());
20-
}*/
22+
}
2123

2224
if (process.env.DEBUG_ENV || process.env.SAM_LOCAL) {
2325
log.config.debug = true;
@@ -41,6 +43,11 @@ exports.handler = (event, context, callback) => {
4143
const inputBuffer = Buffer.from(inputParam, 'base64').toString('utf8');
4244
log.debug(`Executing script "${inputBuffer}"`);
4345

46+
// Creates a new session on each event (instead of reusing for performance benefits)
47+
if (process.env.CLEAN_SESSIONS) {
48+
$browser = chromium.createSession();
49+
}
50+
4451
sandbox.executeScript(inputBuffer, $browser, webdriver, function(err) {
4552
if (process.env.LOG_DEBUG) {
4653
log.debug(child.execSync('ps aux').toString());

lib/sandbox.js

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -30,11 +30,17 @@ exports.executeScript = function(scriptText, browser, driver, cb) {
3030
*/
3131
// https://github.com/GoogleChrome/puppeteer/issues/1825#issuecomment-372241101
3232
// Reuse existing session, likely some edge cases around this...
33-
browser.manage().deleteAllCookies().then(function() {
34-
return $browser.get('about:blank').then(function() {
33+
if (process.env.CLEAN_SESSIONS) {
34+
$browser.quit().then(function() {
3535
cb(null);
36-
}).catch(function(err) {
37-
cb(err);
3836
});
39-
});
40-
}
37+
} else {
38+
browser.manage().deleteAllCookies().then(function() {
39+
return $browser.get('about:blank').then(function() {
40+
cb(null);
41+
}).catch(function(err) {
42+
cb(err);
43+
});
44+
});
45+
}
46+
}

0 commit comments

Comments
 (0)