|
| 1 | +// ember-cli-deploy-fastboot-api-lambda |
| 2 | + |
| 3 | +var config = require('./config.json'); |
| 4 | +var mime = require('mime'); |
| 5 | +var fs = require('fs-promise'); |
| 6 | +var FastBoot = require('fastboot'); |
| 7 | + |
| 8 | +var fancyACacheYeh = { |
| 9 | + yes: 'max-age=63072000, public', |
| 10 | + no: 'max-age=0, public' |
| 11 | +}; |
| 12 | + |
| 13 | +var defaults = { |
| 14 | + distPath: 'dist', |
| 15 | + path: '/', |
| 16 | + host: 'localhost', |
| 17 | + assetsPath: '/assets/', |
| 18 | + standardExtensions: [ |
| 19 | + 'html', |
| 20 | + 'css', |
| 21 | + 'js', |
| 22 | + 'json', |
| 23 | + 'xml', |
| 24 | + 'ico', |
| 25 | + 'txt', |
| 26 | + 'map' |
| 27 | + ], |
| 28 | + headers: { |
| 29 | + 'Content-Type': 'text/html;charset=UTF-8', |
| 30 | + 'Cache-Control': fancyACacheYeh.no |
| 31 | + }, |
| 32 | + fastBootOptions: { |
| 33 | + request: { |
| 34 | + headers: {}, |
| 35 | + get: function() {} |
| 36 | + }, |
| 37 | + response: {} |
| 38 | + } |
| 39 | +}; |
| 40 | + |
| 41 | +// Merge defaults with config overrides |
| 42 | +var standardExtensions = defaults.standardExtensions.concat(config.standardExtensions || []); |
| 43 | +var fallbackPath = config.defaultPath || defaults.path; |
| 44 | + |
| 45 | +// Instantiate Fastboot server |
| 46 | +var app = new FastBoot({ distPath: defaults.distPath }); |
| 47 | + |
| 48 | +exports.handler = function(event, context, callback) { |
| 49 | + console.log('INFO event:', event); |
| 50 | + |
| 51 | + var path = event.path || fallbackPath; |
| 52 | + var staticPath = defaults.distPath + '/' + path; |
| 53 | + |
| 54 | + console.log('INFO path:', path); |
| 55 | + console.log('INFO staticPath:', staticPath); |
| 56 | + |
| 57 | + return fs.readFile(staticPath) |
| 58 | + |
| 59 | + // STATIC FILE LOGIC |
| 60 | + .then(function(fileBuffer) { |
| 61 | + |
| 62 | + // 1. Look up files content type. |
| 63 | + var contentType = mime.lookup(staticPath); |
| 64 | + |
| 65 | + //2. Get file extension. |
| 66 | + var extension = mime.extension(contentType); |
| 67 | + |
| 68 | + //3. If it isn't a standard file, then base64 encode it. |
| 69 | + var shouldEncode = standardExtensions.indexOf(extension) < 0; |
| 70 | + |
| 71 | + //4. Determine if the item is fingerprinted/cacheable |
| 72 | + var shouldCache = staticPath.includes(defaults.assetsPath); |
| 73 | + |
| 74 | + //5. Set encoding value |
| 75 | + var encoding = shouldEncode ? 'base64' : 'utf8'; |
| 76 | + |
| 77 | + //6. Create headers |
| 78 | + var headers = { |
| 79 | + 'Content-Type': contentType, |
| 80 | + 'Cache-Control': shouldCache ? fancyACacheYeh.yes : fancyACacheYeh.no |
| 81 | + }; |
| 82 | + |
| 83 | + //7. Create body |
| 84 | + var body = fileBuffer.toString(encoding); |
| 85 | + |
| 86 | + //8. Create final output |
| 87 | + var payload = { |
| 88 | + statusCode: 200, |
| 89 | + headers: headers, |
| 90 | + body: body, |
| 91 | + isBase64Encoded: shouldEncode |
| 92 | + }; |
| 93 | + |
| 94 | + console.log('INFO: contentType:', contentType); |
| 95 | + console.log('INFO: extension:', extension); |
| 96 | + console.log('INFO: standardExtensions:', standardExtensions); |
| 97 | + console.log('INFO: shouldEncode:', shouldEncode); |
| 98 | + console.log('INFO: shouldCache:', shouldCache); |
| 99 | + console.log('INFO: encoding:', encoding); |
| 100 | + |
| 101 | + return callback(null, payload); |
| 102 | + }) |
| 103 | + |
| 104 | + // GO FASTBOOT GO! |
| 105 | + .catch(function() { |
| 106 | + |
| 107 | + // 1. Create options |
| 108 | + var options = defaults.fastBootOptions; |
| 109 | + options.request.headers = event.headers || {}; |
| 110 | + options.request.headers.host = (event.headers || {}).Host || defaults.host; |
| 111 | + if (event.cookie) { |
| 112 | + options.request.headers.cookie = event.cookie; |
| 113 | + } |
| 114 | + |
| 115 | + console.log('INFO: options:', options); |
| 116 | + |
| 117 | + // 2. Fire up fastboot server |
| 118 | + return app.visit(path, options) |
| 119 | + .then(function(result) { |
| 120 | + return result.html() |
| 121 | + .then(function(html) { |
| 122 | + |
| 123 | + console.log('INFO: html:', html); |
| 124 | + |
| 125 | + // 3. Create headers object |
| 126 | + var headers = Object.assign(result.headers.headers, defaults.headers); |
| 127 | + |
| 128 | + console.log('INFO: headers:', headers); |
| 129 | + |
| 130 | + // 4. Create payload |
| 131 | + var payload = { |
| 132 | + statusCode: result.statusCode, |
| 133 | + headers: headers, |
| 134 | + body: html |
| 135 | + }; |
| 136 | + |
| 137 | + // 5. Profit ??? |
| 138 | + return callback(null, payload); |
| 139 | + }); |
| 140 | + }) |
| 141 | + .catch(err => callback(err)); |
| 142 | + |
| 143 | + }); |
| 144 | + |
| 145 | +}; |
0 commit comments