Skip to content

Commit 56a4cff

Browse files
authored
(feat): Lazy load S3 client (#255)
* Lazy load S3 client, stop creating new S3Client each run if client config * Run prettier
1 parent f2c2999 commit 56a4cff

File tree

3 files changed

+8
-12
lines changed

3 files changed

+8
-12
lines changed

__tests__/sendFile.unit.js

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -364,10 +364,6 @@ describe('SendFile Tests:', function() {
364364
endpoint: "http://test"
365365
}
366366
const apiWithConfig = require('../index')({ version: 'v1.0', mimeTypes: { test: 'text/test' }, s3Config})
367-
let _event = Object.assign({},event,{ path: '/sendfile/s3' })
368-
await new Promise(r => apiWithConfig.run(_event,{
369-
s3Config
370-
},(e,res) => { r(res) }))
371367
sinon.assert.calledWith(setConfigSpy, s3Config);
372368
}) // end it
373369

index.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ const REQUEST = require('./lib/request');
99
const RESPONSE = require('./lib/response');
1010
const UTILS = require('./lib/utils');
1111
const LOGGER = require('./lib/logger');
12-
const S3 = require('./lib/s3-service');
12+
const S3 = () => require('./lib/s3-service');
1313
const prettyPrint = require('./lib/prettyPrint');
1414
const { ConfigurationError } = require('./lib/errors');
1515

@@ -49,6 +49,9 @@ class API {
4949

5050
this._s3Config = props && props.s3Config;
5151

52+
// Set S3 Client
53+
if (this._s3Config) S3().setConfig(this._s3Config);
54+
5255
this._sampleCounts = {};
5356

5457
this._requestCount = 0;
@@ -287,9 +290,6 @@ class API {
287290
this._context = this.context = typeof context === 'object' ? context : {};
288291
this._cb = cb ? cb : undefined;
289292

290-
// Set S3 Client
291-
if (this._s3Config) S3.setConfig(this._s3Config);
292-
293293
// Initalize request and response objects
294294
let request = new REQUEST(this);
295295
let response = new RESPONSE(this, request);

lib/response.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@ const path = require('path'); // Require Node.js path
1212
const compression = require('./compression'); // Require compression lib
1313
const { ResponseError, FileError } = require('./errors'); // Require custom errors
1414

15-
// Require AWS S3 service
16-
const S3 = require('./s3-service');
15+
// Lazy load AWS S3 service
16+
const S3 = () => require('./s3-service');
1717

1818
class RESPONSE {
1919
// Create the constructor function.
@@ -195,7 +195,7 @@ class RESPONSE {
195195

196196
// getSignedUrl doesn't support .promise()
197197
return await new Promise((r) =>
198-
S3.getSignedUrl('getObject', params, async (e, url) => {
198+
S3().getSignedUrl('getObject', params, async (e, url) => {
199199
if (e) {
200200
// Execute callback with caught error
201201
await fn(e);
@@ -336,7 +336,7 @@ class RESPONSE {
336336
let params = UTILS.parseS3(filepath);
337337

338338
// Attempt to get the object from S3
339-
let data = await S3.getObject(params).promise();
339+
let data = await S3().getObject(params).promise();
340340

341341
// Set results, type and header
342342
buffer = data.Body;

0 commit comments

Comments
 (0)