Skip to content

Commit 66158d5

Browse files
sterenAce Nassri
authored andcommitted
GCF: Headless Chrome sample (#705)
* Headless Chrome sample * Remove test script Function emulator does not support node8... * update puppeteer * await browser close * Do not instantiate browser at each requests * use more descriptive tag * use better package name
1 parent ccdd400 commit 66158d5

File tree

4 files changed

+125
-0
lines changed

4 files changed

+125
-0
lines changed

functions/headless-chrome/index.js

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
/**
2+
* Copyright 2018, Google, Inc.
3+
* Licensed under the Apache License, Version 2.0 (the "License");
4+
* you may not use this file except in compliance with the License.
5+
* You may obtain a copy of the License at
6+
*
7+
* http://www.apache.org/licenses/LICENSE-2.0
8+
*
9+
* Unless required by applicable law or agreed to in writing, software
10+
* distributed under the License is distributed on an "AS IS" BASIS,
11+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
* See the License for the specific language governing permissions and
13+
* limitations under the License.
14+
*/
15+
16+
'use strict';
17+
18+
// [START functions_headless_chrome]
19+
const puppeteer = require('puppeteer');
20+
21+
let page;
22+
23+
async function getBrowserPage () {
24+
// [START start_browser]
25+
const browser = await puppeteer.launch({args: ['--no-sandbox']});
26+
// [END start_browser]
27+
return browser.newPage();
28+
}
29+
30+
exports.screenshot = async (req, res) => {
31+
const url = req.query.url;
32+
33+
if (!url) {
34+
return res.send('Please provide URL as GET parameter, for example: <a href="?url=https://example.com">?url=https://example.com</a>');
35+
}
36+
37+
if (!page) {
38+
page = await getBrowserPage();
39+
}
40+
41+
await page.goto(url);
42+
const imageBuffer = await page.screenshot();
43+
res.set('Content-Type', 'image/png');
44+
res.send(imageBuffer);
45+
};
46+
// [END functions_headless_chrome]
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
{
2+
"name": "nodejs-docs-samples-functions-headless-chrome",
3+
"version": "0.0.1",
4+
"private": true,
5+
"license": "Apache-2.0",
6+
"author": "Google Inc.",
7+
"repository": {
8+
"type": "git",
9+
"url": "https://github.com/GoogleCloudPlatform/nodejs-docs-samples.git"
10+
},
11+
"engines": {
12+
"node": ">=8"
13+
},
14+
"scripts": {
15+
"lint": "repo-tools lint",
16+
"pretest": "npm run lint",
17+
"e2e-test": "export FUNCTIONS_CMD='gcloud beta functions' && sh test/updateFunctions.sh && BASE_URL=\"https://$GCF_REGION-$GCLOUD_PROJECT.cloudfunctions.net/\" ava -T 20s --verbose test/*.test.js"
18+
},
19+
"dependencies": {
20+
"puppeteer": "^1.6.2"
21+
},
22+
"devDependencies": {
23+
"@google-cloud/nodejs-repo-tools": "^2.2.5",
24+
"ava": "0.25.0",
25+
"supertest": "^3.0.0"
26+
},
27+
"cloud-repo-tools": {
28+
"requiresKeyFile": true,
29+
"requiresProjectId": true,
30+
"requiredEnvVars": [
31+
"BASE_URL",
32+
"GCF_REGION",
33+
"FUNCTIONS_CMD"
34+
]
35+
}
36+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
/**
2+
* Copyright 2017, Google, Inc.
3+
* Licensed under the Apache License, Version 2.0 (the "License");
4+
* you may not use this file except in compliance with the License.
5+
* You may obtain a copy of the License at
6+
*
7+
* http://www.apache.org/licenses/LICENSE-2.0
8+
*
9+
* Unless required by applicable law or agreed to in writing, software
10+
* distributed under the License is distributed on an "AS IS" BASIS,
11+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
* See the License for the specific language governing permissions and
13+
* limitations under the License.
14+
*/
15+
16+
const test = require(`ava`);
17+
const tools = require(`@google-cloud/nodejs-repo-tools`);
18+
const supertest = require(`supertest`);
19+
20+
const BASE_URL = process.env.BASE_URL;
21+
22+
test.before(`Must specify BASE_URL`, t => {
23+
t.truthy(BASE_URL);
24+
});
25+
26+
test.before(tools.checkCredentials);
27+
28+
test.cb(`screenshot: should return a screenshot`, (t) => {
29+
supertest(BASE_URL)
30+
.get(`/screenshot?url=https://example.com`)
31+
.send()
32+
.expect(200)
33+
.expect(response => {
34+
t.is(response.type, `image/png`);
35+
t.true(response.body instanceof Buffer);
36+
t.true(response.body.length > 0);
37+
})
38+
.end(t.end);
39+
});
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
#!/bin/bash
2+
# Shell script to emulate/deploy all Cloud Functions in the file
3+
4+
${FUNCTIONS_CMD} deploy screenshot --trigger-http --runtime nodejs8 --memory 1024MB

0 commit comments

Comments
 (0)