Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Decrease bundle size #395

Merged
merged 2 commits into from
Feb 2, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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