From 56a4cffc520307358d55e2a2269e7f74f435069c Mon Sep 17 00:00:00 2001
From: perpil <97474956+perpil@users.noreply.github.com>
Date: Sat, 25 Nov 2023 21:38:51 -0800
Subject: [PATCH] (feat): Lazy load S3 client (#255)

* Lazy load S3 client, stop creating new S3Client each run if client config

* Run prettier
---
 __tests__/sendFile.unit.js | 4 ----
 index.js                   | 8 ++++----
 lib/response.js            | 8 ++++----
 3 files changed, 8 insertions(+), 12 deletions(-)

diff --git a/__tests__/sendFile.unit.js b/__tests__/sendFile.unit.js
index 6400e3b..b2c80f6 100644
--- a/__tests__/sendFile.unit.js
+++ b/__tests__/sendFile.unit.js
@@ -364,10 +364,6 @@ describe('SendFile Tests:', function() {
       endpoint: "http://test"
     }
     const apiWithConfig = require('../index')({ version: 'v1.0', mimeTypes: { test: 'text/test' }, s3Config})
-    let _event = Object.assign({},event,{ path: '/sendfile/s3' })
-    await new Promise(r => apiWithConfig.run(_event,{
-      s3Config
-    },(e,res) => { r(res) }))
     sinon.assert.calledWith(setConfigSpy, s3Config);
   }) // end it
 
diff --git a/index.js b/index.js
index cdd9b43..86e3439 100644
--- a/index.js
+++ b/index.js
@@ -9,7 +9,7 @@ const REQUEST = require('./lib/request');
 const RESPONSE = require('./lib/response');
 const UTILS = require('./lib/utils');
 const LOGGER = require('./lib/logger');
-const S3 = require('./lib/s3-service');
+const S3 = () => require('./lib/s3-service');
 const prettyPrint = require('./lib/prettyPrint');
 const { ConfigurationError } = require('./lib/errors');
 
@@ -49,6 +49,9 @@ class API {
 
     this._s3Config = props && props.s3Config;
 
+    // Set S3 Client
+    if (this._s3Config) S3().setConfig(this._s3Config);
+
     this._sampleCounts = {};
 
     this._requestCount = 0;
@@ -287,9 +290,6 @@ class API {
     this._context = this.context = typeof context === 'object' ? context : {};
     this._cb = cb ? cb : undefined;
 
-    // Set S3 Client
-    if (this._s3Config) S3.setConfig(this._s3Config);
-
     // Initalize request and response objects
     let request = new REQUEST(this);
     let response = new RESPONSE(this, request);
diff --git a/lib/response.js b/lib/response.js
index 3b1bf04..8b2befd 100644
--- a/lib/response.js
+++ b/lib/response.js
@@ -12,8 +12,8 @@ const path = require('path'); // Require Node.js path
 const compression = require('./compression'); // Require compression lib
 const { ResponseError, FileError } = require('./errors'); // Require custom errors
 
-// Require AWS S3 service
-const S3 = require('./s3-service');
+// Lazy load AWS S3 service
+const S3 = () => require('./s3-service');
 
 class RESPONSE {
   // Create the constructor function.
@@ -195,7 +195,7 @@ class RESPONSE {
 
     // getSignedUrl doesn't support .promise()
     return await new Promise((r) =>
-      S3.getSignedUrl('getObject', params, async (e, url) => {
+      S3().getSignedUrl('getObject', params, async (e, url) => {
         if (e) {
           // Execute callback with caught error
           await fn(e);
@@ -336,7 +336,7 @@ class RESPONSE {
           let params = UTILS.parseS3(filepath);
 
           // Attempt to get the object from S3
-          let data = await S3.getObject(params).promise();
+          let data = await S3().getObject(params).promise();
 
           // Set results, type and header
           buffer = data.Body;