From 524e50aad3adc3ea616c9cb3c97892fa46b6af5c Mon Sep 17 00:00:00 2001 From: Connor Kirkpatrick Date: Wed, 19 Jan 2022 11:53:40 +0000 Subject: [PATCH] Modernise witch.js (#43) * Use const instead of var * Incorporate feedback --- source/witch/witch.js | 166 ++++++++++++++++++++++-------------------- 1 file changed, 89 insertions(+), 77 deletions(-) diff --git a/source/witch/witch.js b/source/witch/witch.js index 5d1f12c..b4f163b 100644 --- a/source/witch/witch.js +++ b/source/witch/witch.js @@ -2,94 +2,106 @@ const aws = require("aws-sdk"); const fs = require("fs"); const path = require("path"); const mime = require("mime-types"); +const https = require("https"); +const url = require("url"); const s3 = new aws.S3(); const SUCCESS = "SUCCESS"; const FAILED = "FAILED"; -const BUCKET = process.env.BUCKET; +const { BUCKET } = process.env; -exports.staticHandler = function(event, context) { - if (event.RequestType !== "Create" && event.RequestType !== "Update") { - return respond(event, context, SUCCESS, {}); - } +exports.staticHandler = function (event, context) { + if (event.RequestType !== "Create" && event.RequestType !== "Update") { + return respond(event, context, SUCCESS, {}); + } + + Promise.all( + walkSync("./").map((file) => { + const fileType = mime.lookup(file) || "application/octet-stream"; - Promise.all(walkSync("./").map(file => { - var fileType = mime.lookup(file) || "application/octet-stream"; - - console.log(`${file} -> ${fileType}`); - - return s3.upload({ - Body: fs.createReadStream(file), - Bucket: BUCKET, - ContentType: fileType, - Key: file, - ACL: "private", - }).promise(); - })).then((msg) => { - respond(event, context, SUCCESS, {}); - }).catch(err => { - respond(event, context, FAILED, {Message: err}); + console.log(`${file} -> ${fileType}`); + + return s3 + .upload({ + Body: fs.createReadStream(file), + Bucket: BUCKET, + ContentType: fileType, + Key: file, + ACL: "private", + }) + .promise(); + }) + ) + .then((msg) => { + respond(event, context, SUCCESS, {}); + }) + .catch((err) => { + respond(event, context, FAILED, { Message: err }); }); }; // List all files in a directory in Node.js recursively in a synchronous fashion -function walkSync(dir, filelist) { - var files = fs.readdirSync(dir); - filelist = filelist || []; - - files.forEach(function(file) { - if (fs.statSync(path.join(dir, file)).isDirectory()) { - filelist = walkSync(path.join(dir, file), filelist); - } else { - filelist.push(path.join(dir, file)); - } - }); - - return filelist; -}; +function walkSync(dir, filelist = []) { + const files = fs.readdirSync(dir); -function respond(event, context, responseStatus, responseData, physicalResourceId, noEcho) { - var responseBody = JSON.stringify({ - Status: responseStatus, - Reason: "See the details in CloudWatch Log Stream: " + context.logStreamName, - PhysicalResourceId: physicalResourceId || context.logStreamName, - StackId: event.StackId, - RequestId: event.RequestId, - LogicalResourceId: event.LogicalResourceId, - NoEcho: noEcho || false, - Data: responseData - }); - - console.log("Response body:\n", responseBody); - - var https = require("https"); - var url = require("url"); - - var parsedUrl = new url.URL(event.ResponseURL); - var options = { - hostname: parsedUrl.hostname, - port: 443, - path: parsedUrl.pathname + parsedUrl.search, - method: "PUT", - headers: { - "content-type": "", - "content-length": responseBody.length - } - }; - - var request = https.request(options, function(response) { - console.log("Status code: " + response.statusCode); - console.log("Status message: " + response.statusMessage); - context.done(); - }); - - request.on("error", function(error) { - console.log("send(..) failed executing https.request(..): " + error); - context.done(); - }); - - request.write(responseBody); - request.end(); + files.forEach(function (file) { + if (fs.statSync(path.join(dir, file)).isDirectory()) { + filelist = walkSync(path.join(dir, file), filelist); + } else { + filelist.push(path.join(dir, file)); + } + }); + + return filelist; +} + +function respond( + event, + context, + responseStatus, + responseData, + physicalResourceId, + noEcho +) { + const responseBody = JSON.stringify({ + Status: responseStatus, + Reason: + "See the details in CloudWatch Log Stream: " + context.logStreamName, + PhysicalResourceId: physicalResourceId || context.logStreamName, + StackId: event.StackId, + RequestId: event.RequestId, + LogicalResourceId: event.LogicalResourceId, + NoEcho: noEcho || false, + Data: responseData, + }); + + console.log("Response body:\n", responseBody); + + const { pathname, hostname, search } = new url.URL(event.ResponseURL); + const options = { + hostname, + port: 443, + path: pathname + search, + method: "PUT", + headers: { + "content-type": "", + "content-length": responseBody.length, + }, + }; + + const request = https.request(options, function (response) { + console.log("Status code: " + response.statusCode); + console.log("Status message: " + response.statusMessage); + context.done(); + }); + + request.on("error", function (error) { + console.log("send(..) failed executing https.request(..): " + error); + context.done(); + }); + + request.write(responseBody); + request.end(); }