forked from spiritedmedia/tachyon-edge
-
Notifications
You must be signed in to change notification settings - Fork 0
/
lambda-handler.js
executable file
·71 lines (64 loc) · 1.99 KB
/
lambda-handler.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
const tachyon = require( './tachyon' );
exports.handler = function( event, context, callback ) {
let request = event.Records[0].cf.request;
console.log( 'The event!!!' );
console.log( JSON.stringify( event ) );
/**
* Helper function to return the original request back to CloudFront
*/
function sendUnmodifiedResponse() {
callback( null, request );
}
// If no query strings then there is nothing to do...
if ( ! request.querystring ) {
return sendUnmodifiedResponse();
}
/**
* Handles the response when the Promise chain completes without any rejections
*
* @param {object} data Data passed from the Promise chain
* @return {callback} Modified request to send back to CloudFront
*/
function handlePromiseSuccess( data ) {
console.log( 'Promise Success!' );
if ( ! ( 'S3Key' in data ) ) {
return sendUnmodifiedResponse();
}
request.uri = '/' + data.S3Key;
request.querystring = '';
console.log( 'RESULT!!!' );
console.log( JSON.stringify( request ) );
return callback(null, request);
}
/**
* Handles the response when the Promise chain completes with a rejection
*
* @param {object} data Data passed from the Promise chain
* @return {callback} Request to send back to CloudFront
*/
function handlePromiseError( data ) {
console.log( 'Promise Error!' );
switch( data.code ) {
case 'found-on-s3':
case 'cached-processed-image':
return handlePromiseSuccess( data );
break;
}
console.log( data );
return sendUnmodifiedResponse();
}
// Kick off our chain of Promises
//
// If we need to short circuit the chain we can reject a Promise and let
// handlePromiseError() take over.
//
// Otherwise we go from .then() call to .then() call until we're all done.
const requestURL = request.uri + '?' + request.querystring;
tachyon.setup(requestURL)
.then(tachyon.checkIfCached)
.then(tachyon.getOriginal)
.then(tachyon.processImage)
.then(tachyon.cacheProcessedImage)
.then(handlePromiseSuccess)
.catch(handlePromiseError);
};