Skip to content

Commit

Permalink
Add http protocol support
Browse files Browse the repository at this point in the history
  • Loading branch information
ryanblock committed Sep 10, 2023
1 parent b13faa6 commit 743b2e4
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 4 deletions.
3 changes: 2 additions & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ let clientFactory = require('./client-factory')
* @param {string} [config.region] AWS service region (e.g. `us-west-1`); can be overridden per request
* @param {string} [config.profile] Credential profile name to use
* @param {boolean} [config.autoloadPlugins=true] Attempt to automatically load aws-lite plugins
* @param {boolean} [config.keepAlive=true] Disable Node.js's connection keepalive, helpful for local-only testing
* @param {boolean} [config.keepAlive=true] Disable Node.js's connection keepalive, helpful for local testing
* @param {string} [config.protocol='https'] Set connection protocol to 'http', helpful for local testing
*
* @returns {Promise<function>} Client async function
*/
Expand Down
14 changes: 11 additions & 3 deletions src/request.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
let https = require('https')
let aws4 = require('aws4')

module.exports = function request (params, creds, region, config, metadata) {
return new Promise((resolve, reject) => {
params.path = params.path || params.endpoint
config.protocol = config.protocol ?? 'https'
if (![ 'https', 'http' ].includes(config.protocol)) {
throw ReferenceError('Protocol must be `https` or `http`')
}

// JSON-ify payload where convenient
let body = params.body || params.data || params.payload || params.json
Expand All @@ -18,10 +21,15 @@ module.exports = function request (params, creds, region, config, metadata) {

let options = aws4.sign({ ...params, region }, creds)

// Importing http(s) is a bit slow (~1ms), so only use what we need
let isHTTPS = options.hostname.includes('.amazonaws.com') || config.protocol === 'https'
// eslint-disable-next-line
let http = isHTTPS ? require('https') : require('http')

// Disable keep-alive locally (or wait Node's default 5s for sockets to time out)
options.agent = new https.Agent({ keepAlive: config.keepAlive ?? useAWS() })
options.agent = new http.Agent({ keepAlive: config.keepAlive ?? useAWS() })

let req = https.request(options, res => {
let req = http.request(options, res => {
let data = []
let { headers = {}, statusCode } = res
let ok = statusCode >= 200 && statusCode < 303
Expand Down

0 comments on commit 743b2e4

Please sign in to comment.