Skip to content

Commit

Permalink
(feat): Lazy load S3 client (#255)
Browse files Browse the repository at this point in the history
* Lazy load S3 client, stop creating new S3Client each run if client config

* Run prettier
  • Loading branch information
perpil authored Nov 26, 2023
1 parent f2c2999 commit 56a4cff
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 12 deletions.
4 changes: 0 additions & 4 deletions __tests__/sendFile.unit.js
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
8 changes: 4 additions & 4 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');

Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand Down
8 changes: 4 additions & 4 deletions lib/response.js
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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;
Expand Down

0 comments on commit 56a4cff

Please sign in to comment.