From 025680d7ba9eb2083025b46b76e26effbe2881cd Mon Sep 17 00:00:00 2001 From: yugasun Date: Mon, 9 Mar 2020 17:18:49 +0800 Subject: [PATCH] fix: migrate to slsplus --- CHANGELOG.md | 12 +++---- README.md | 15 ++++---- SCOPE.txt | 25 ------------- __tests__/integration.js | 4 +-- examples/basic-starter/README.md | 36 ------------------- examples/basic-starter/app.js | 28 +++++++++------ .../basic-starter/{lambda.js => handler.js} | 6 ++-- examples/basic-starter/package-lock.json | 16 ++++----- examples/basic-starter/package.json | 11 ++---- examples/basic-starter/views/index.pug | 4 +-- package.json | 11 +++--- 11 files changed, 53 insertions(+), 115 deletions(-) delete mode 100644 SCOPE.txt delete mode 100644 examples/basic-starter/README.md rename examples/basic-starter/{lambda.js => handler.js} (71%) diff --git a/CHANGELOG.md b/CHANGELOG.md index 7b186cf..fd56390 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,20 +1,20 @@ -## [1.0.5](https://github.com/yugasun/tencent-serverless-http/compare/v1.0.4...v1.0.5) (2020-02-09) +## [1.0.5](https://github.com/serverless-plus/tencent-serverless-http/compare/v1.0.4...v1.0.5) (2020-02-09) ### Bug Fixes -* get tencent api query ([6e740ab](https://github.com/yugasun/tencent-serverless-http/commit/6e740ab)) +* get tencent api query ([6e740ab](https://github.com/serverless-plus/tencent-serverless-http/commit/6e740ab)) -## [1.0.4](https://github.com/yugasun/tencent-serverless-http/compare/v1.0.3...v1.0.4) (2020-01-16) +## [1.0.4](https://github.com/serverless-plus/tencent-serverless-http/compare/v1.0.3...v1.0.4) (2020-01-16) ### Bug Fixes -* update lowest node version to 8 ([654b6b8](https://github.com/yugasun/tencent-serverless-http/commit/654b6b8)) +* update lowest node version to 8 ([654b6b8](https://github.com/serverless-plus/tencent-serverless-http/commit/654b6b8)) -## [1.0.1](https://github.com/yugasun/tencent-serverless-http/compare/v1.0.0...v1.0.1) (2020-01-15) +## [1.0.1](https://github.com/serverless-plus/tencent-serverless-http/compare/v1.0.0...v1.0.1) (2020-01-15) ### Bug Fixes -* update readme ([bc3491e](https://github.com/yugasun/tencent-serverless-http/commit/bc3491e)) +* update readme ([bc3491e](https://github.com/serverless-plus/tencent-serverless-http/commit/bc3491e)) diff --git a/README.md b/README.md index 01bf941..c07f82f 100644 --- a/README.md +++ b/README.md @@ -1,9 +1,8 @@ # Tencent Serverless Http -[![Build Status](https://travis-ci.com/yugasun/tencent-serverless-http.svg?branch=master)](https://travis-ci.com/yugasun/tencent-serverless-http) -[![npm](https://img.shields.io/npm/v/tencent-serverless-http.svg)]() -[![npm](https://img.shields.io/npm/dm/tencent-serverless-http.svg)]() -[![dependencies Status](https://david-dm.org/yugasun/tencent-serverless-http/status.svg)](https://david-dm.org/yugasun/tencent-serverless-http) +[![Build Status](https://travis-ci.com/serverless-plus/tencent-serverless-http.svg?branch=master)](https://travis-ci.com/serverless-plus/tencent-serverless-http) +[![npm](https://img.shields.io/npm/v/%40slsplus%2Ftencent-serverless-http.svg)](http://www.npmtrends.com/%40slsplus%2Ftencent-serverless-http) +[![NPM downloads](https://img.shields.io/npm/dm/%40slsplus%2Ftencent-serverless-http.svg)](http://www.npmtrends.com/%40slsplus%2Ftencent-serverless-http) This project is a fork of [aws-serverless-express](https://github.com/awslabs/aws-serverless-express.git), @@ -12,7 +11,7 @@ and modify for tencent cloud scf. ## Install ```bash -npm install tencent-serverless-http +npm install @slsplus/tencent-serverless-http ``` ## Usage @@ -20,7 +19,7 @@ npm install tencent-serverless-http ```js // handler.js 'use strict'; -const tencentServerlessHttp = require('tencent-serverless-http'); +const tencentServerlessHttp = require('@slsplus/tencent-serverless-http'); const app = require('./app'); const server = tencentServerlessHttp.createServer(app); @@ -29,11 +28,11 @@ exports.handler = (event, context) => { }; ``` -This package includes middleware to easily get the event object Lambda receives +This package includes middleware to easily get the event object SCF receives from API Gateway ```js -const tencentServerlessHttpMiddleware = require('tencent-serverless-http/middleware'); +const tencentServerlessHttpMiddleware = require('@slsplus/tencent-serverless-http/middleware'); app.use(tencentServerlessHttpMiddleware.eventContext()); app.get('/', (req, res) => { res.json(req.apiGateway.event); diff --git a/SCOPE.txt b/SCOPE.txt deleted file mode 100644 index da92343..0000000 --- a/SCOPE.txt +++ /dev/null @@ -1,25 +0,0 @@ -############################################################################ -## SCOPE.txt - version 1.0 ## -## Created by Brett Andrews - andrbret@amazon.com ## -## ---------------------------------------------------------------------- ## -## Version history: ## -## ---------------------------------------------------------------------- ## -## v1.0 - 02/09/18 - Initial scope document ## -############################################################################ - -# Objective - -The goal of this library is to provide developers with a way to run Node.js -HTTP applications and REST APIs (including but not limited to the usage of -frameworks such as Express) on AWS Lambda. This repository includes an example -directory which allows developers to quickly deploy a running application using SAM. - -This repository should contain: - -- The tencent-serverless-http library. -- Documentation on using tencent-serverless-http -- Examples of using the tencent-serverless-http library. - -This repository does not contain: - -- Code that comprises a complete application. \ No newline at end of file diff --git a/__tests__/integration.js b/__tests__/integration.js index 9c6dc63..760706a 100644 --- a/__tests__/integration.js +++ b/__tests__/integration.js @@ -24,9 +24,9 @@ function makeEvent (eventOverrides) { function expectedRootResponse () { return makeResponse({ 'headers': { - 'content-length': '3682', + 'content-length': '3446', 'content-type': 'text/html; charset=utf-8', - 'etag': 'W/"e62-NHyvvU4yLNxWfyDMGcW+pYANLZ4"' + 'etag': 'W/"d76-UV3AoJfpe2/C0l+IRqFcasVbyD0"' } }) } diff --git a/examples/basic-starter/README.md b/examples/basic-starter/README.md deleted file mode 100644 index 3ac3379..0000000 --- a/examples/basic-starter/README.md +++ /dev/null @@ -1,36 +0,0 @@ -## Example - -In addition to a basic Lambda function and Express server, the `example` directory includes a [Swagger file](http://swagger.io/specification/), [CloudFormation template](https://aws.amazon.com/cloudformation/aws-cloudformation-templates/) with [Serverless Application Model (SAM)](https://github.com/awslabs/serverless-application-model), and helper scripts to help you set up and manage your application. - -### Steps for running the example -This guide assumes you have already [set up an AWS account](http://docs.aws.amazon.com/AmazonSimpleDB/latest/DeveloperGuide/AboutAWSAccounts.html) and have the latest version of the [AWS CLI](https://aws.amazon.com/cli/) installed. - -1. From your preferred project directory: `git clone https://github.com/awslabs/aws-serverless-express.git && cd aws-serverless-express/examples/basic-starter`. -2. Run `npm run config -- --account-id="" --bucket-name="" [--region="" --function-name=""]` to configure the example, eg. `npm run config -- --account-id="123456789012" --bucket-name="my-unique-bucket"`. This modifies `package.json`, `simple-proxy-api.yaml` and `cloudformation.yaml` with your account ID, bucket, region and function name (region defaults to `us-east-1` and function name defaults to `AwsServerlessExpressFunction`). If the bucket you specify does not yet exist, the next step will create it for you. This step modifies the existing files in-place; if you wish to make changes to these settings, you will need to modify `package.json`, `simple-proxy-api.yaml` and `cloudformation.yaml` manually. -3. Run `npm run setup` (Windows users: `npm run win-setup`) - this installs the node dependencies, creates an S3 bucket (if it does not already exist), packages and deploys your serverless Express application to AWS Lambda, and creates an API Gateway proxy API. -4. After the setup command completes, open the AWS CloudFormation console https://console.aws.amazon.com/cloudformation/home and switch to the region you specified. Select the `AwsServerlessExpressStack` stack, then click the `ApiUrl` value under the __Outputs__ section - this will open a new page with your running API. The API index lists the resources available in the example Express server (`app.js`), along with example `curl` commands. - -See the sections below for details on how to migrate an existing (or create a new) Node.js project based on this example. If you would prefer to delete AWS assets that were just created, simply run `npm run delete-stack` to delete the CloudFormation Stack, including the API and Lambda Function. If you specified a new bucket in the `config` command for step 1 and want to delete that bucket, run `npm run delete-bucket`. - -### Creating or migrating a Node.js project based on the example - -To use this example as a base for a new Node.js project: - -1. Copy the files in the `examples/basic-starter` directory into a new project directory (`cp -r ./examples/basic-starter ~/projects/my-new-node-project`). If you have not already done so, follow the [steps for running the example](#steps-for-running-the-example) (you may want to first modify some of the resource names to something more project-specific, eg. the CloudFormation stack, Lambda function, and API Gateway API). -2. After making updates to `app.js`, simply run `npm run package-deploy` (Windows users: `npm run win-package-deploy`). - -To migrate an existing Node server: - -1. Copy the following files from the `example` directory: `api-gateway-event.json`, `cloudformation.yaml`, `lambda.js`, and `simple-proxy-api.yaml`. Additionally, copy the `scripts` and `config` sections of `example/package.json` into your existing `package.json` - this includes many helpful commands to manage your AWS serverless assets and perform _basic_ local simulation of API Gateway and Lambda. If you have not already done so, follow the [steps for running the example](#steps-for-running-the-example) (be sure to copy over `scripts/configure.js`. You may want to first modify some of the resource names to something more project-specific, eg. the CloudFormation stack, Lambda function, and API Gateway API). -2. From your existing project directory, run `npm install --save aws-serverless-express`. -3. Modify `lambda.js` to import your own server configuration (eg. change `require('./app')` to `require('./server')`). You will need to ensure you export your app configuration from the necessary file (eg. `module.exports = app`). This library takes your app configuration and listens on a Unix Domain Socket for you, so you can remove your call to `app.listen()` (if you have a `server.listen` callback, you can provide it as the second parameter in the `awsServerlessExpress.createServer` method). -4. Modify the `CodeUri` property of the Lambda function resource in `cloudformation.yaml` to point to your application directory (e.g. `CodeUri: ./src`). If you are using a build tool (e.g. Gulp, Grunt, Webpack, Rollup, etc.), you will instead want to point to your build output directory. -5. Run `npm run package-deploy` (Windows users: `npm run win-package-deploy`) to package and deploy your application. - -To perform a basic, local simulation of API Gateway and Lambda with your Node server, update `api-gateway-event.json` with some values that are valid for your server (`httpMethod`, `path`, `body` etc.) and run `npm run local`. AWS Lambda uses NodeJS 4.3 LTS, and it is recommended to use the same version for testing purposes. - -If you need to make modifications to your API Gateway API, modify `simple-proxy-api.yaml` and run `npm run package-deploy`. If your API requires CORS, be sure to modify the two `options` methods defined in the Swagger file, otherwise you can safely remove them. To modify your other AWS assets, make your changes to `cloudformation.yaml` and run `npm run package-deploy`. Alternatively, you can manage these assets via the AWS console. - -## Node.js version - -This example was written against Node.js version 6.10 diff --git a/examples/basic-starter/app.js b/examples/basic-starter/app.js index 276f535..abb2b46 100644 --- a/examples/basic-starter/app.js +++ b/examples/basic-starter/app.js @@ -4,7 +4,7 @@ const express = require('express') const bodyParser = require('body-parser') const cors = require('cors') const compression = require('compression') -const tencentServerlessNodejsMiddleware = require('tencent-serverless-http/middleware') +const tencentServerlessNodejsMiddleware = require('@slsplus/tencent-serverless-http/middleware') const app = express() const router = express.Router() @@ -28,7 +28,9 @@ app.set('views', path.join(__dirname, 'views')) router.get('/', (req, res) => { res.render('index', { - apiUrl: req.apiGateway ? `https://${req.apiGateway.event.headers.Host}/${req.apiGateway.event.requestContext.stage}` : 'http://localhost:3000' + apiUrl: req.apiGateway + ? `https://${req.apiGateway.event.headers.Host}/${req.apiGateway.event.requestContext.stage}` + : 'http://localhost:3000' }) }) @@ -75,17 +77,21 @@ router.delete('/users/:userId', (req, res) => { res.json(users) }) -const getUser = (userId) => users.find(u => u.id === parseInt(userId)) -const getUserIndex = (userId) => users.findIndex(u => u.id === parseInt(userId)) +const getUser = (userId) => users.find((u) => u.id === parseInt(userId)) +const getUserIndex = (userId) => + users.findIndex((u) => u.id === parseInt(userId)) // Ephemeral in-memory data store -const users = [{ - id: 1, - name: 'Joe' -}, { - id: 2, - name: 'Jane' -}] +const users = [ + { + id: 1, + name: 'Joe' + }, + { + id: 2, + name: 'Jane' + } +] let userIdCounter = users.length // The tencent-serverless-http library creates a server and listens on a Unix diff --git a/examples/basic-starter/lambda.js b/examples/basic-starter/handler.js similarity index 71% rename from examples/basic-starter/lambda.js rename to examples/basic-starter/handler.js index 89efea6..7fde6b9 100644 --- a/examples/basic-starter/lambda.js +++ b/examples/basic-starter/handler.js @@ -1,5 +1,5 @@ 'use strict' -const awsServerlessExpress = require(process.env.NODE_ENV === 'test' ? '../../index' : 'tencent-serverless-http') +const serverlessExpress = require(process.env.NODE_ENV === 'test' ? '../../index' : '@slsplus/tencent-serverless-http') const app = require('./app') // NOTE: If you get ERR_CONTENT_DECODING_FAILED in your browser, this is likely @@ -25,6 +25,6 @@ const binaryMimeTypes = [ 'text/text', 'text/xml' ] -const server = awsServerlessExpress.createServer(app, null, binaryMimeTypes) +const server = serverlessExpress.createServer(app, null, binaryMimeTypes) -exports.handler = (event, context) => awsServerlessExpress.proxy(server, event, context) +exports.handler = (event, context) => serverlessExpress.proxy(server, event, context) diff --git a/examples/basic-starter/package-lock.json b/examples/basic-starter/package-lock.json index 46c08f7..acfbe1d 100644 --- a/examples/basic-starter/package-lock.json +++ b/examples/basic-starter/package-lock.json @@ -4,6 +4,14 @@ "lockfileVersion": 1, "requires": true, "dependencies": { + "@slsplus/tencent-serverless-http": { + "version": "1.0.5", + "resolved": "https://registry.npmjs.org/@slsplus/tencent-serverless-http/-/tencent-serverless-http-1.0.5.tgz", + "integrity": "sha512-/7Zd5jcByoJBBZ8xNHunDYGRCyxMajbwtkb5ly+tJH4nYzcrnYw+xoWxEKdXv6Y0rGbm3vuXh2SAb3P3rsh2vA==", + "requires": { + "type-is": "^1.6.16" + } + }, "@types/babel-types": { "version": "7.0.1", "resolved": "https://registry.npmjs.org/@types/babel-types/-/babel-types-7.0.1.tgz", @@ -793,14 +801,6 @@ "resolved": "https://registry.npmjs.org/statuses/-/statuses-1.5.0.tgz", "integrity": "sha1-Fhx9rBd2Wf2YEfQ3cfqZOBR4Yow=" }, - "tencent-serverless-http": { - "version": "1.0.5", - "resolved": "https://registry.npmjs.org/tencent-serverless-http/-/tencent-serverless-http-1.0.5.tgz", - "integrity": "sha512-84IBl96Nqho0mhXXlDHAhgjfh9iNatvU9fD2YVwCp1LpZ1CgCg/2PLeKrWimYHSAgfmVhpQDl/FDi9QRjYVPlw==", - "requires": { - "type-is": "^1.6.16" - } - }, "to-fast-properties": { "version": "1.0.3", "resolved": "https://registry.npmjs.org/to-fast-properties/-/to-fast-properties-1.0.3.tgz", diff --git a/examples/basic-starter/package.json b/examples/basic-starter/package.json index f1b3906..a9babcd 100644 --- a/examples/basic-starter/package.json +++ b/examples/basic-starter/package.json @@ -2,20 +2,13 @@ "name": "tencent-serverless-express-example", "version": "2.1.1", "description": "Example application for running a Node Express app on Tencent Cloud SCF using API Gateway.", - "main": "lambda.js", - "config": { - "s3BucketName": "YOUR_UNIQUE_BUCKET_NAME", - "region": "YOUR_AWS_REGION", - "cloudFormationStackName": "AwsServerlessExpressStack", - "functionName": "YOUR_SERVERLESS_EXPRESS_LAMBDA_FUNCTION_NAME", - "accountId": "YOUR_ACCOUNT_ID" - }, + "main": "handler.js", "scripts": { "start": "node app.local.js" }, "license": "Apache-2.0", "dependencies": { - "tencent-serverless-http": "^1.0.5", + "@slsplus/tencent-serverless-http": "^1.0.5", "body-parser": "^1.17.1", "compression": "^1.6.2", "cors": "^2.8.3", diff --git a/examples/basic-starter/views/index.pug b/examples/basic-starter/views/index.pug index b2d78bf..8e8275a 100644 --- a/examples/basic-starter/views/index.pug +++ b/examples/basic-starter/views/index.pug @@ -44,9 +44,7 @@ html h1 My Serverless Application img.sam-logo(src='sam') p - | Welcome to your AWS serverless application. This example application has several resources configured for you to explore. State is stored in memory in a given container, and is therfore ephemeral - see - a(href='https://aws.amazon.com/blogs/compute/container-reuse-in-lambda/', target='_blank') Understanding Container Reuse in AWS Lambda - |  for more information. + | Welcome to your Tencetn SCF serverless application. This example application has several resources configured for you to explore. section.resources h2 Resources section.resource diff --git a/package.json b/package.json index ff1b625..08dea7c 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,9 @@ { - "name": "tencent-serverless-http", + "name": "@slsplus/tencent-serverless-http", "version": "1.0.5", + "publishConfig": { + "access": "public" + }, "description": "This library enables you to utilize Tencent Cloud API Gateway to respond to web and API requests using your existing Node.js application framework.", "keywords": [ "tencent-cloud", @@ -10,9 +13,9 @@ "scf", "nodejs" ], - "homepage": "https://github.com/yugasun/tencent-serverless-http", + "homepage": "https://github.com/serverless-plus/tencent-serverless-http", "bugs": { - "url": "https://github.com/yugasun/tencent-serverless-http/issues" + "url": "https://github.com/serverless-plus/tencent-serverless-http/issues" }, "license": "Apache-2.0", "files": [ @@ -23,7 +26,7 @@ "main": "index.js", "repository": { "type": "git", - "url": "https://github.com/yugasun/tencent-serverless-http.git" + "url": "https://github.com/serverless-plus/tencent-serverless-http.git" }, "engines": { "node": ">=8"