lambda-js-prerender
所属分类:微服务
开发工具:JavaScript
文件大小:0KB
下载次数:0
上传日期:2023-10-19 21:42:40
上 传 者:
sh-1993
说明: AWS CloudFront Lambda无服务器JavaScript预渲染器。
(AWS CloudFront Lambda serverless JavaScript prerenderer.)
文件列表:
.devcontainer/ (0, 2023-12-13)
.devcontainer/devcontainer.json (882, 2023-12-13)
.eslintrc (993, 2023-12-13)
.travis.yml (436, 2023-12-13)
.vscode/ (0, 2023-12-13)
.vscode/extensions.json (331, 2023-12-13)
.vscode/settings.json (114, 2023-12-13)
CHANGELOG.md (624, 2023-12-13)
Dockerfile (741, 2023-12-13)
ISSUE_TEMPLATE.md (730, 2023-12-13)
LICENSE (1094, 2023-12-13)
deploy (447, 2023-12-13)
package.json (632, 2023-12-13)
samconfig.toml (250, 2023-12-13)
src/ (0, 2023-12-13)
src/app.js (3575, 2023-12-13)
src/openapi.js (335, 2023-12-13)
swagger.json (2166, 2023-12-13)
template.yaml (2927, 2023-12-13)
test/ (0, 2023-12-13)
test/mocha.env.js (90, 2023-12-13)
test/unit/ (0, 2023-12-13)
test/unit/app.js (2303, 2023-12-13)
# lambda-js-prerender
![git Version](https://img.shields.io/github/package-json/v/nuxy/lambda-js-prerender?style=flat-square&svg=true&label=git+package) [![Build Status](https://api.travis-ci.com/nuxy/lambda-js-prerender.svg?branch=master)](https://app.travis-ci.com/github/nuxy/lambda-js-prerender)
AWS [CloudFront Lambda](https://docs.aws.amazon.com/lambda/latest/dg/welcome.html) serverless JavaScript [prerenderer](https://github.com/prerender/prerender).
## Features
- Ability to prerender SPA (Single-page application) pages for SE0
- Ability to create screenshots of dynamic pages for testing.
- Serverless function, scales [on a tight budget](https://s3.amazonaws.com/lambda-tools/pricing-calculator.html).
- Can be set-up easily (in minutes).
## Dependencies
- [AWS SAM CLI](https://docs.aws.amazon.com/serverless-application-model/latest/developerguide/serverless-sam-cli-install.html)
- [Docker](https://www.docker.com/get-started)
- [Node.js](https://nodejs.org)
### Get the [Chromium](https://opensource.google/projects/chromium) binary
$ wget -c https://github.com/nuxy/chromium-lambda-build/releases/download/0.0.1/headless_shell.tar.gz -O - | tar -xz
## Deploying to AWS
$ ./deploy --profile
The following operations are orchestrated by AWS [CloudFormation](https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/Welcome.html) during execution:
- Docker container image is created and uploaded to AWS [Elastic Container Registry](https://docs.aws.amazon.com/AmazonECR/latest/userguide/what-is-ecr.html).
- AWS [Lambda](https://docs.aws.amazon.com/lambda/latest/dg/welcome.html) function is created with a configured [Function URL](https://docs.aws.amazon.com/lambda/latest/dg/lambda-urls.html) endpoint.
- AWS [CloudFront](https://docs.aws.amazon.com/AmazonCloudFront/latest/DeveloperGuide/Introduction.html) distribution is created using the new function as origin.
- Network routing occurs thereby exposing your Lambda function URL
## Invoking the service
### Command-line
```sh
curl -X 'POST' \
'https://.lambda-url..on.aws' \
-H 'Accept: text/html' \
-H 'Content-Type: application/json' \
-d '{"url": "", "image": }'
```
### In Node.js
#### AWS SDK for JavaScript v3
```javascript
const {LambdaClient, InvokeCommand} = require('@aws-sdk/client-lambda');
const client = new LambdaClient({region: ''});
const params = {
FunctionName: 'PrerenderApi',
InvocationType: 'RequestResponse',
LogType: 'Tail',
Payload: JSON.stringify({
url: '',
image:
})
};
const command = new InvokeCommand(params);
try {
const {PayLoad} = await client.send(command);
console.log(Payload?.body));
} catch (err) {
console.warn(err.message);
throw err;
}
```
#### AWS SDK for JavaScript v2
```javascript
const AWS = require('aws-sdk');
const client = new AWS.Lambda({region: ''});
const params = {
FunctionName: 'PrerenderApi',
InvocationType: 'RequestResponse',
LogType: 'Tail',
Payload: JSON.stringify({
url: '',
image:
})
};
client.invoke(params).promise()
.then(function({Payload}) {
console.log(Payload?.body));
})
.catch(function(err) {
console.warn(err.message);
throw err;
});
```
## Environment variables
The following function environment overrides can be configured in the SAM [template](https://github.com/nuxy/human-face-detection/blob/master/template.yaml#L23) file:
| Variable name | Description | Default value |
|--------------------|----------------------|---------------|
| `PRERENDER_DEBUG` | Enable verbose logging to [CloudWatch](https://docs.aws.amazon.com/AmazonCloudWatch/latest/monitoring/WhatIsCloudWatch.html) group | false |
|`PRERENDER_TIMEOUT` | Execution timeout in seconds | 60 |
## AWS requirements
In order to successfully deploy your application you must have [set-up your AWS Config](https://docs.aws.amazon.com/config/latest/developerguide/gs-cli.html) and have [created an IAM user](https://docs.aws.amazon.com/IAM/latest/UserGuide/id_users_create.html) with the following [policies](https://docs.aws.amazon.com/IAM/latest/UserGuide/access_policies_manage.html):
- [IAMFullAccess](https://console.aws.amazon.com/iam/home#/policies/arn%3Aaws%3Aiam%3A%3Aaws%3Apolicy%2FIAMFullAccess)
- [CloudFrontFullAccess](https://console.aws.amazon.com/iam/home#/policies/arn%3Aaws%3Aiam%3A%3Aaws%3Apolicy%2FCloudFrontFullAccess)
- [AWSCloudFormationFullAccess](https://console.aws.amazon.com/iam/home#/policies/arn%3Aaws%3Aiam%3A%3Aaws%3Apolicy%2FAWSCloudFormationFullAccess)
- [AWSLambda_FullAccess](https://console.aws.amazon.com/iam/home#/policies/arn%3Aaws%3Aiam%3A%3Aaws%3Apolicy%2FAWSLambda_FullAccess)
- [AmazonEC2ContainerRegistryFullAccess](https://us-east-1.console.aws.amazon.com/iam/home#/policies/arn:aws:iam::aws:policy/AmazonEC2ContainerRegistryFullAccess)
WARNING: The policies above are provided to ensure a successful application deployment. It is recommended that you adjust these policies to meet the security requirements of your Lambda application. They should NOT be used in a Production environment.
## Developers
### CLI options
Run [ESLint](https://eslint.org/) on project sources:
$ npm run lint
Generate [Swagger](https://swagger.io) OpenAPI definitions:
$ npm run genapi
Run [Mocha](https://mochajs.org) unit tests:
$ npm run test
## Performance
Running in Lambda there is overhead that occurs ([cold start](https://docs.aws.amazon.com/lambda/latest/operatorguide/execution-environments.html)) when the environment is first launched. This overhead does not include [headless shell](https://github.com/nuxy/chromium-lambda-build) initialization which creates its own latency. Due to this, you can expect a longer response time for the first request. As long as there is an active Lambda handler (hot start) all subsequent requests will not incurr this overhead.
## References
- [Setting IAM Permissions and Roles](https://docs.aws.amazon.com/AmazonCloudFront/latest/DeveloperGuide/lambda-edge-permissions.html)
- [Scaling and concurrency in Lambda](https://docs.aws.amazon.com/lambda/latest/operatorguide/scaling-concurrency.html)
- [Lambda quotas](https://docs.aws.amazon.com/lambda/latest/dg/gettingstarted-limits.html)
- [AWS SDK for JavaScript](https://docs.aws.amazon.com/AWSJavaScriptSDK/latest/index.html)
- [chromium-lambda-build](https://github.com/nuxy/chromium-lambda-build)
- [content_switches.cc](https://source.chromium.org/chromium/chromium/src/+/main:content/public/common/content_switches.cc?q=kDisableGpu&ss=chromium)
## Versioning
This package is maintained under the [Semantic Versioning](https://semver.org) guidelines.
## License and Warranty
This package is distributed in the hope that it will be useful, but without any warranty; without even the implied warranty of merchantability or fitness for a particular purpose.
_lambda-js-prerender_ is provided under the terms of the [MIT license](http://www.opensource.org/licenses/mit-license.php)
[AWS](https://aws.amazon.com) is a registered trademark of Amazon Web Services, Inc.
## Author
[Marc S. Brooks](https://github.com/nuxy)
近期下载者:
相关文件:
收藏者: