Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -125,5 +125,4 @@ dist
### Node Patch ###
# Serverless Webpack directories
.webpack/
app/**/*.js
app/**/*.d.ts
build
3 changes: 1 addition & 2 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
FROM public.ecr.aws/lambda/nodejs:14

COPY . ${LAMBDA_TASK_ROOT}

COPY ./build ${LAMBDA_TASK_ROOT}
RUN npm ci --only=production

# Set the CMD to your handler (could also be done as a parameter override outside of the Dockerfile)
Expand Down
97 changes: 92 additions & 5 deletions dev-server.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const express = require('express')
const { existsSync } = require('fs')
const { handler } = require('./app/index')
const { handler } = require('./build/src/index')
const newman = require('newman')

const app = express();
Expand All @@ -10,7 +10,7 @@ let server = null;

app.post('/2015-03-31/functions/function/invocations', async (req, res) => {
try {
const body = req.body ? JSON.parse(req.body) : req;
const body = req.body ? JSON.parse(req.body) : ExpressToAWSAPIGatewayProxyEventV2(req);
handler(body, {}).then(result => {
res.send(result);
}).catch(error => console.error(error));
Expand All @@ -20,7 +20,12 @@ app.post('/2015-03-31/functions/function/invocations', async (req, res) => {
})

app.all('/api', async (req, res) => {
handler(req, {}).then(result => {
handler(ExpressToAWSAPIGatewayProxyEventV2(req), {}).then(result => {
res.send(result);
}).catch(error => console.error(error));
});
app.all('/__health', async (req, res) => {
handler(ExpressToAWSAPIGatewayProxyEventV2(req), {}).then(result => {
res.send(result);
}).catch(error => console.error(error));
});
Expand All @@ -34,8 +39,8 @@ server = app.listen(port, () => {
if (process.argv[2]) {
const testFilename = process.argv[3] || 'service-collection.postman_collection.json'

console.log('Running Newman tests');
if (!existsSync(`./${testFilename}`)) {
console.log('Running Newman tests');
console.error(`Error: test collection not found
./${testFilename}

Expand All @@ -58,4 +63,86 @@ server = app.listen(port, () => {
process.exit();
})
}
})
})

function ExpressToAWSAPIGatewayProxyEventV2(request) {
return {
"resource": request.path,
"path": request.path,
"httpMethod": request.method,
"headers": {
"header1": "value1",
"header2": "value2"
},
"multiValueHeaders": {
"header1": [
"value1"
],
"header2": [
"value1",
"value2"
]
},
"queryStringParameters": {
"parameter1": "value1",
"parameter2": "value"
},
"multiValueQueryStringParameters": {
"parameter1": [
"value1",
"value2"
],
"parameter2": [
"value"
]
},
"requestContext": {
"accountId": "123456789012",
"apiId": "id",
"authorizer": {
"claims": null,
"scopes": null
},
"domainName": "id.execute-api.us-east-1.amazonaws.com",
"domainPrefix": "id",
"extendedRequestId": "request-id",
"httpMethod": "GET",
"identity": {
"accessKey": null,
"accountId": null,
"caller": null,
"cognitoAuthenticationProvider": null,
"cognitoAuthenticationType": null,
"cognitoIdentityId": null,
"cognitoIdentityPoolId": null,
"principalOrgId": null,
"sourceIp": "IP",
"user": null,
"userAgent": "user-agent",
"userArn": null,
"clientCert": {
"clientCertPem": "CERT_CONTENT",
"subjectDN": "www.example.com",
"issuerDN": "Example issuer",
"serialNumber": "a1:a1:a1:a1:a1:a1:a1:a1:a1:a1:a1:a1:a1:a1:a1:a1",
"validity": {
"notBefore": "May 28 12:30:02 2019 GMT",
"notAfter": "Aug 5 09:36:04 2021 GMT"
}
}
},
"path": "/my/path",
"protocol": "HTTP/1.1",
"requestId": "id=",
"requestTime": "04/Mar/2020:19:15:17 +0000",
"requestTimeEpoch": 1583349317135,
"resourceId": null,
"resourcePath": "/my/path",
"stage": "$default"
},
"pathParameters": null,
"stageVariables": null,
"body": "Hello from Lambda!",
"isBase64Encoded": false
}
}
5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@
"scripts": {
"start": "tsc --watch --preserveWatchOutput & nodemon dev-server.js -e js -w ./app",
"build": "tsc",
"test": "find app -wholename '*.d.ts' -delete && find app -wholename '*.js' -delete && jest",
"build:image": "./scripts/gen-build",
"test": "jest",
"test:watch": "npm t -- --watch",
"test:newman": "npm run build && node dev-server.js --test",
"lint": "tslint --fix -p ./tsconfig.json",
Expand All @@ -33,7 +34,7 @@
},
"husky": {
"hooks": {
"pre-commit": "tsdx lint"
"pre-commit": "npm run lint && npm run prettier"
}
},
"prettier": {
Expand Down
11 changes: 11 additions & 0 deletions scripts/gen-build
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#!/bin/bash

rm -rf ./build
npm run build
cp ./package-lock.json ./build
cd ./build
mv src app
../scripts/gen-prod-package-json package.json package.json
find . -wholename '*.d.ts' -delete
find . -wholename '*.test.js' -delete
cd ..
22 changes: 22 additions & 0 deletions scripts/gen-prod-package-json
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#!/bin/env node

const { readFileSync, writeFileSync } = require("fs");

const packageString = readFileSync(process.argv[2]);

const { name, version, dependencies, engines, keywords, repository } = JSON.parse(packageString);

const prodJSON = {
name,
version,
dependencies,
engines,
keywords,
repository
};


console.log(prodJSON);
if (process.argv[3]) {
writeFileSync(process.argv[3], JSON.stringify(prodJSON, null, ' '));
}
6 changes: 4 additions & 2 deletions app/index.test.ts → src/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,8 +90,10 @@ describe('Index Handler', () => {
const response = await handler(mockRequest(), mockContext());
expect(response).toStrictEqual({
body: JSON.stringify({
hello: 'world',
foo: 'bar',
version,
name,
event: mockRequest(),
context: mockContext(),
}),
headers: {
'content-type': 'text/json',
Expand Down
6 changes: 4 additions & 2 deletions app/index.ts → src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,10 @@ export const handler = async (
'content-type': 'text/json',
},
body: JSON.stringify({
hello: 'world',
foo: 'bar',
version: pkg.version,
name: pkg.name,
event,
context: _context,
}),
};
};
File renamed without changes.
4 changes: 1 addition & 3 deletions app/lib/logger.ts → src/lib/logger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,7 @@ const format = winston.format.combine(
)
);

const transports = [
new winston.transports.Console()
];
const transports = [new winston.transports.Console()];

const Logger = winston.createLogger({
level: level(),
Expand Down
4 changes: 2 additions & 2 deletions tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@
// "declarationMap": true, /* Generates a sourcemap for each corresponding '.d.ts' file. */
// "sourceMap": true, /* Generates corresponding '.map' file. */
// "outFile": "./", /* Concatenate and emit output to single file. */
// "outDir": "app", /* Redirect output structure to the directory. */
// "rootDir": "src", /* Specify the root directory of input files. Use to control the output directory structure with --outDir. */
"outDir": "build", /* Redirect output structure to the directory. */
// "rootDir": ".", /* Specify the root directory of input files. Use to control the output directory structure with --outDir. */
// "composite": true, /* Enable project compilation */
// "removeComments": true, /* Do not emit comments to output. */
// "noEmit": true, /* Do not emit outputs. */
Expand Down