Skip to content

Commit e980980

Browse files
committed
run on local
1 parent 5687c40 commit e980980

File tree

5 files changed

+144
-15
lines changed

5 files changed

+144
-15
lines changed

README.md

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,30 +2,34 @@
22

33
Boilerplate for running Headless-Chrome by [Puppeteer](https://github.com/GoogleChrome/puppeteer) on AWS Lambda.
44

5+
## Run on local
6+
7+
By executing `npm run local`, you can check the operation while actually viewing the chrome. (non-headless, slowmo)
8+
59
## Packaging & Deploy
610

711
Lambda's memory is set to 384 MB or more.
812

9-
### chrome in package (recommend)
13+
### chrome in package (recommended)
1014

1115
Run `npm run package`, and deploy the package.zip.
1216

1317
### chrome NOT in package
1418

15-
Due to the large size of Chrome, it may exceed the [Lambda package size limit](http://docs.aws.amazon.com/lambda/latest/dg/limits.html) (50 MB) depending on the other module to include.
19+
Due to the large size of Chrome, it may exceed the [Lambda package size limit](http://docs.aws.amazon.com/lambda/latest/dg/limits.html) (50MB) depending on the other module to include.
1620
In that case, put Chrome in S3 and download it at container startup so startup time will be longer.
1721

1822
Run `npm run package-nochrome`, deploy the package.zip, and set following env valiables on Lambda.
1923

2024
- `CHROME_BUCKET`(required): S3 bucket where Chrome is put
21-
- `CHROME_KEY`(optional): S3 key. default: headless_shell.tar.gz
25+
- `CHROME_KEY`(optional): S3 key. default: `headless_shell.tar.gz`
2226

2327
## Build Headless-Chrome (optional)
2428

2529
If you want to use latest chrome, run chrome/buildChrome.sh on EC2 having at least 16GB memory and 30GB volume.
2630
See also [serverless-chrome](https://github.com/adieuadieu/serverless-chrome/blob/master/chrome/README.md).
27-
Once you build it, link to headless_shell.tar.gz in /chrome.
31+
Once you build it, link to `headless_shell.tar.gz` in `chrome` dir.
2832

29-
## Reference
33+
## Article
3034

31-
[記事](https://www.sambaiz.net/article/132/)
35+
[PuppeteerでHeadless Chromeを動かすコードをLambdaで実行する - sambaiz-net](https://www.sambaiz.net/article/132/)

package-lock.json

Lines changed: 112 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@
33
"package": "npm run package-ready && cp chrome/headless_shell.tar.gz dist && cd dist && zip -rq ../package.zip .",
44
"package-nochrome": "npm run package-ready && zip -rq ../package.zip .",
55
"package-ready": "npm run babel && cp -r package.json dist && cd dist && PUPPETEER_SKIP_CHROMIUM_DOWNLOAD=1 npm install --production",
6-
"babel": "rm -rf dist && mkdir dist && ./node_modules/.bin/babel src --out-dir dist"
6+
"babel": "rm -rf dist && mkdir dist && ./node_modules/.bin/babel src --out-dir dist",
7+
"local": "npm run babel && cp -r node_modules dist && node dist/local.js"
78
},
89
"dependencies": {
910
"puppeteer": "^0.10.2",

src/index.js

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,21 +3,22 @@ const puppeteer = require('puppeteer'),
33
util = require('./util');
44

55
exports.handler = async (event, context, callback) => {
6-
run().then(
6+
exports.run({
7+
headless: true,
8+
chromePath: await util.setupChrome()
9+
}).then(
710
(result) => { callback(null, result); }
811
).catch(
912
(err) => { callback(err); }
1013
)
1114
}
1215

13-
14-
const run = async () => {
15-
16-
const chromePath = await util.setupChrome();
16+
exports.run = async (option) => {
1717

1818
const browser = await puppeteer.launch({
19-
headless: true,
20-
executablePath: chromePath,
19+
headless: option.headless,
20+
executablePath: option.chromePath,
21+
slowMo: option.slowMoMs,
2122
args: config.launchOptionForLambda,
2223
dumpio: !!util.DEBUG,
2324
});
@@ -29,4 +30,4 @@ const run = async () => {
2930
browser.close();
3031

3132
return 'done'
32-
}
33+
}

src/local.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
const index = require('./index');
2+
3+
index.run({
4+
headless: false,
5+
slowMoMs: 250,
6+
// use chrome installed by puppeteer
7+
}).then(
8+
(result) => { console.log(result); }
9+
).catch(
10+
(err) => { console.error(err); }
11+
);

0 commit comments

Comments
 (0)