diff --git a/functions/headless-chrome/index.js b/functions/headless-chrome/index.js new file mode 100644 index 0000000000..c78494fda9 --- /dev/null +++ b/functions/headless-chrome/index.js @@ -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 functions_headless_chrome] +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: ?url=https://example.com'); + } + + if (!page) { + page = await getBrowserPage(); + } + + await page.goto(url); + const imageBuffer = await page.screenshot(); + res.set('Content-Type', 'image/png'); + res.send(imageBuffer); +}; +// [END functions_headless_chrome] diff --git a/functions/headless-chrome/package.json b/functions/headless-chrome/package.json new file mode 100644 index 0000000000..63173ccb86 --- /dev/null +++ b/functions/headless-chrome/package.json @@ -0,0 +1,36 @@ +{ + "name": "nodejs-docs-samples-functions-headless-chrome", + "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" + ] + } +} diff --git a/functions/headless-chrome/test/index.test.js b/functions/headless-chrome/test/index.test.js new file mode 100644 index 0000000000..1e24c38dd3 --- /dev/null +++ b/functions/headless-chrome/test/index.test.js @@ -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); +}); diff --git a/functions/headless-chrome/test/updateFunctions.sh b/functions/headless-chrome/test/updateFunctions.sh new file mode 100644 index 0000000000..508aa29139 --- /dev/null +++ b/functions/headless-chrome/test/updateFunctions.sh @@ -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 \ No newline at end of file