Skip to content
Open
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
8 changes: 5 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,19 +12,21 @@
},
"devDependencies": {
"@types/aws-lambda": "0.0.23",
"@types/etag": "1.8.0",
"@types/node": "8.5.2",
"ava": "0.23.0",
"husky": "0.14.3",
"lint-staged": "6.0.0",
"prettier": "1.9.2",
"serverless-plugin-typescript": "1.1.5",
"serverless-apigwy-binary": "0.1.0"
"serverless-apigwy-binary": "0.1.0",
"serverless-plugin-typescript": "1.1.5"
},
"dependencies": {
"aws-sdk": "2.176.0",
"graphql-request": "1.4.1",
"lambda-helpers": "0.2.2",
"sharp": "0.18.4",
"source-map-support": "0.5.0"
"source-map-support": "0.5.0",
"etag": "1.8.1"
}
}
29 changes: 22 additions & 7 deletions src/handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { getConfig, parseParams, Params } from './parser'
import { callbackRuntime } from 'lambda-helpers'
import { APIGatewayEvent, ProxyResult } from 'aws-lambda'
import { GraphQLClient } from 'graphql-request'
const etag = require('etag')
const sharp = require('sharp')

import 'source-map-support/register'
Expand Down Expand Up @@ -38,6 +39,7 @@ export default callbackRuntime(async (event: APIGatewayEvent) => {
}
}

const headerETag = event.headers['If-None-Match']
const [paramsErr, params] = parseParams(event.path)

if (paramsErr) {
Expand All @@ -57,7 +59,7 @@ export default callbackRuntime(async (event: APIGatewayEvent) => {
const {
ContentLength,
ContentType,
ContentDisposition,
ContentDisposition
} = await s3.headObject(options).promise()

if (ContentLength! > 25 * 1024 * 1024) {
Expand All @@ -82,7 +84,7 @@ export default callbackRuntime(async (event: APIGatewayEvent) => {
) {
const obj = await s3.getObject(options).promise()
const body = (obj.Body as Buffer).toString('base64')
return base64Response(body, ContentType!, ContentDisposition!)
return base64Response(body, ContentType!, ContentDisposition!, headerETag!)
}

const s3Resp = await s3.getObject(options).promise()
Expand Down Expand Up @@ -125,21 +127,34 @@ export default callbackRuntime(async (event: APIGatewayEvent) => {
buf.toString('base64'),
ContentType!,
ContentDisposition!,
headerETag!
)
})

function base64Response(
body: string,
ContentType: string,
ContentDisposition: string,
headerETag: string
) {
const bodyETag = etag(body);
const headers = {
'Content-Type': ContentType,
'Content-Disposition': ContentDisposition,
'Cache-Control': 'max-age=31536000',
'ETag': bodyETag
};

if(headerETag && headerETag === bodyETag){
return {
statusCode: 304,
headers: headers
}
}

return {
statusCode: 200,
headers: {
'Content-Type': ContentType,
'Content-Disposition': ContentDisposition,
'Cache-Control': 'max-age=31536000',
},
headers: headers,
body,
isBase64Encoded: true,
}
Expand Down