Skip to content

Commit

Permalink
Merge pull request #395 from fireridlle/fix/bundle_size
Browse files Browse the repository at this point in the history
Decrease bundle size
  • Loading branch information
fireridlle authored Feb 2, 2021
2 parents 4e5c243 + dfaae12 commit 835fece
Show file tree
Hide file tree
Showing 4 changed files with 80 additions and 25 deletions.
19 changes: 0 additions & 19 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 6 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@
"dotenv": "^8.2.0",
"fn-annotate": "^1.1.3",
"object-assign": "^4.1.0",
"url-parse": "^1.4.7",
"util-deprecate": "^1.0.2"
},
"devDependencies": {
Expand Down Expand Up @@ -74,5 +73,11 @@
"branches": [
"master"
]
},
"browser": {
"http": false,
"https": false,
"os": false,
"util": false
}
}
7 changes: 2 additions & 5 deletions src/_http.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@

var APIVersion = '4'

var parse = require('url-parse')
var util = require('./_util')
var {
AbortController,
abortableFetch,
} = require('abortcontroller-polyfill/dist/cjs-ponyfill')
const { formatUrl } = require('./_util')

/**
* The driver's internal HTTP client.
Expand Down Expand Up @@ -75,9 +75,6 @@ HttpClient.prototype.syncLastTxnTime = function(time) {
* @returns {Promise} The response promise.
*/
HttpClient.prototype.execute = function(method, path, body, query, options) {
var url = parse(this._baseUrl)
url.set('pathname', path)
url.set('query', query)
options = util.defaults(options, {})

var signal = options.signal
Expand All @@ -99,7 +96,7 @@ HttpClient.prototype.execute = function(method, path, body, query, options) {
timeout = setTimeout(() => abortController.abort(), this._timeout)
}

return fetch(url.href, {
return fetch(formatUrl(this._baseUrl, path, query), {
agent: this._keepAliveEnabledAgent,
body: body,
signal: signal,
Expand Down
72 changes: 72 additions & 0 deletions src/_util.js
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,79 @@ function checkInstanceHasProperty(obj, prop) {
return typeof obj === 'object' && obj !== null && Boolean(obj[prop])
}

function formatUrl(base, path, query) {
query = typeof query === 'object' ? querystringify(query) : query
return [
base,
path ? (path.charAt(0) === '/' ? '' : '/' + path) : '',
query ? (query.charAt(0) === '?' ? '' : '?' + query) : '',
].join('')
}

/**
* Transform a query string to an object.
*
* @param {Object} obj Object that should be transformed.
* @param {String} prefix Optional prefix.
* @returns {String}
* @api public
*/
function querystringify(obj, prefix) {
prefix = prefix || ''

var pairs = [],
value,
key

//
// Optionally prefix with a '?' if needed
//
if ('string' !== typeof prefix) prefix = '?'

for (key in obj) {
if (checkInstanceHasProperty(obj, key)) {
value = obj[key]

//
// Edge cases where we actually want to encode the value to an empty
// string instead of the stringified value.
//
if (!value && (value === null || value === undef || isNaN(value))) {
value = ''
}

key = encode(key)
value = encode(value)

//
// If we failed to encode the strings, we should bail out as we don't
// want to add invalid strings to the query.
//
if (key === null || value === null) continue
pairs.push(key + '=' + value)
}
}

return pairs.length ? prefix + pairs.join('&') : ''
}

/**
* Attempts to encode a given input.
*
* @param {String} input The string that needs to be encoded.
* @returns {String|Null} The encoded string.
* @api private
*/
function encode(input) {
try {
return encodeURIComponent(input)
} catch (e) {
return null
}
}

module.exports = {
formatUrl: formatUrl,
inherits: inherits,
isNodeEnv: isNodeEnv,
defaults: defaults,
Expand Down

0 comments on commit 835fece

Please sign in to comment.