-
Notifications
You must be signed in to change notification settings - Fork 2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
GCF: Headless Chrome sample #705
Merged
+125
−0
Merged
Changes from 5 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
e0017df
Headless Chrome sample
steren 8ae2643
Remove test script
steren 48f1b63
update puppeteer
steren 65f4240
await browser close
steren b3f90a5
Do not instantiate browser at each requests
steren ddf13d4
use more descriptive tag
steren 26f1016
use better package name
steren File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
/** | ||
* Copyright 2018, Google, Inc. | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
'use strict'; | ||
|
||
// [START full_sample] | ||
const puppeteer = require('puppeteer'); | ||
|
||
let page; | ||
|
||
async function getBrowserPage () { | ||
// [START start_browser] | ||
const browser = await puppeteer.launch({args: ['--no-sandbox']}); | ||
// [END start_browser] | ||
return browser.newPage(); | ||
} | ||
|
||
exports.screenshot = async (req, res) => { | ||
const url = req.query.url; | ||
|
||
if (!url) { | ||
return res.send('Please provide URL as GET parameter, for example: <a href="?url=https://example.com">?url=https://example.com</a>'); | ||
} | ||
|
||
if (!page) { | ||
page = await getBrowserPage(); | ||
} | ||
|
||
await page.goto(url); | ||
const imageBuffer = await page.screenshot(); | ||
res.set('Content-Type', 'image/png'); | ||
res.send(imageBuffer); | ||
}; | ||
// [END full_sample] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
{ | ||
"name": "nodejs-docs-samples-functions-hello-world", | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nit: s/ |
||
"version": "0.0.1", | ||
"private": true, | ||
"license": "Apache-2.0", | ||
"author": "Google Inc.", | ||
"repository": { | ||
"type": "git", | ||
"url": "https://github.com/GoogleCloudPlatform/nodejs-docs-samples.git" | ||
}, | ||
"engines": { | ||
"node": ">=8" | ||
}, | ||
"scripts": { | ||
"lint": "repo-tools lint", | ||
"pretest": "npm run lint", | ||
"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" | ||
}, | ||
"dependencies": { | ||
"puppeteer": "^1.6.2" | ||
}, | ||
"devDependencies": { | ||
"@google-cloud/nodejs-repo-tools": "^2.2.5", | ||
"ava": "0.25.0", | ||
"supertest": "^3.0.0" | ||
}, | ||
"cloud-repo-tools": { | ||
"requiresKeyFile": true, | ||
"requiresProjectId": true, | ||
"requiredEnvVars": [ | ||
"BASE_URL", | ||
"GCF_REGION", | ||
"FUNCTIONS_CMD" | ||
] | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
/** | ||
* Copyright 2017, Google, Inc. | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
const test = require(`ava`); | ||
const tools = require(`@google-cloud/nodejs-repo-tools`); | ||
const supertest = require(`supertest`); | ||
|
||
const BASE_URL = process.env.BASE_URL; | ||
|
||
test.before(`Must specify BASE_URL`, t => { | ||
t.truthy(BASE_URL); | ||
}); | ||
|
||
test.before(tools.checkCredentials); | ||
|
||
test.cb(`screenshot: should return a screenshot`, (t) => { | ||
supertest(BASE_URL) | ||
.get(`/screenshot?url=https://example.com`) | ||
.send() | ||
.expect(200) | ||
.expect(response => { | ||
t.is(response.type, `image/png`); | ||
t.true(response.body instanceof Buffer); | ||
t.true(response.body.length > 0); | ||
}) | ||
.end(t.end); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
#!/bin/bash | ||
# Shell script to emulate/deploy all Cloud Functions in the file | ||
|
||
${FUNCTIONS_CMD} deploy screenshot --trigger-http --runtime nodejs8 --memory 1024MB |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nit: use a more descriptive region tag? e.g.
functions_headless_chrome