Skip to content

Commit

Permalink
Align S3 http settings with axios ones
Browse files Browse the repository at this point in the history
Deviation from SDK standard:
- requestTimeout is set
- rejectUnauthorized can be customized
- maxSockets is undefined / number of sockets is unlimited
  • Loading branch information
benoit74 committed Jan 30, 2025
1 parent ebf0af5 commit 8ba3c86
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 2 deletions.
13 changes: 12 additions & 1 deletion src/S3.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { S3Client, PutObjectCommand, GetObjectCommand, DeleteObjectCommand, HeadBucketCommand } from '@aws-sdk/client-s3'
import * as logger from './Logger.js'
import { publicIpv4 } from 'public-ip'
import { NodeHttpHandler } from '@smithy/node-http-handler'
import { Agent } from 'https'

interface BucketParams {
Bucket: string
Expand All @@ -12,11 +14,15 @@ class S3 {
public s3Handler: any
public bucketName: string
private region: string
private reqTimeout: number
private insecure: boolean

constructor(s3Url: any, s3Params: any) {
constructor(s3Url: any, s3Params: any, reqTimeout: number, insecure: boolean) {
this.url = s3Url
this.params = s3Params
this.bucketName = s3Params.bucketName
this.reqTimeout = reqTimeout
this.insecure = insecure
this.setRegion()
}

Expand All @@ -42,6 +48,11 @@ class S3 {
endpoint: s3UrlBase.href,
forcePathStyle: s3UrlBase.protocol === 'http:',
region: this.region,
requestHandler: new NodeHttpHandler({
requestTimeout: this.reqTimeout,
httpAgent: new Agent({ keepAlive: true }),
httpsAgent: new Agent({ keepAlive: true, rejectUnauthorized: !this.insecure }), // rejectUnauthorized: false disables TLS
}),
})

return this.bucketExists(this.bucketName)
Expand Down
2 changes: 1 addition & 1 deletion src/mwoffliner.lib.ts
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ async function execute(argv: any) {
const s3UrlObj = urlParser.parse(optimisationCacheUrl)
const queryReader = QueryStringParser.parse(s3UrlObj.query)
const s3Url = (s3UrlObj.protocol || 'https:') + '//' + (s3UrlObj.host || '') + (s3UrlObj.pathname || '')
s3Obj = new S3(s3Url, queryReader)
s3Obj = new S3(s3Url, queryReader, requestTimeout * 1000 || config.defaults.requestTimeout, argv.insecure)
await s3Obj.initialise().then(() => {
logger.log('Successfully logged in S3')
})
Expand Down

0 comments on commit 8ba3c86

Please sign in to comment.