diff --git a/packages/kbn-pm/dist/index.js b/packages/kbn-pm/dist/index.js index 5558dc7f50a111..54182dd6f2c2b0 100644 --- a/packages/kbn-pm/dist/index.js +++ b/packages/kbn-pm/dist/index.js @@ -17159,29 +17159,6 @@ var Writable = __webpack_require__(172).Writable; var assert = __webpack_require__(165); var debug = __webpack_require__(230); -// Whether to use the native URL object or the legacy url module -var useNativeURL = false; -try { - assert(new URL()); -} -catch (error) { - useNativeURL = error.code === "ERR_INVALID_URL"; -} - -// URL fields to preserve in copy operations -var preservedUrlFields = [ - "auth", - "host", - "hostname", - "href", - "path", - "pathname", - "port", - "protocol", - "query", - "search", -]; - // Create handlers that pass events from native requests var events = ["abort", "aborted", "connect", "error", "socket", "timeout"]; var eventHandlers = Object.create(null); @@ -17191,20 +17168,19 @@ events.forEach(function (event) { }; }); -// Error types with codes var InvalidUrlError = createErrorType( "ERR_INVALID_URL", "Invalid URL", TypeError ); +// Error types with codes var RedirectionError = createErrorType( "ERR_FR_REDIRECTION_FAILURE", "Redirected request failed" ); var TooManyRedirectsError = createErrorType( "ERR_FR_TOO_MANY_REDIRECTS", - "Maximum number of redirects exceeded", - RedirectionError + "Maximum number of redirects exceeded" ); var MaxBodyLengthExceededError = createErrorType( "ERR_FR_MAX_BODY_LENGTH_EXCEEDED", @@ -17215,9 +17191,6 @@ var WriteAfterEndError = createErrorType( "write after end" ); -// istanbul ignore next -var destroy = Writable.prototype.destroy || noop; - // An HTTP(S) request that can be redirected function RedirectableRequest(options, responseCallback) { // Initialize the request @@ -17239,13 +17212,7 @@ function RedirectableRequest(options, responseCallback) { // React to responses of native requests var self = this; this._onNativeResponse = function (response) { - try { - self._processResponse(response); - } - catch (cause) { - self.emit("error", cause instanceof RedirectionError ? - cause : new RedirectionError({ cause: cause })); - } + self._processResponse(response); }; // Perform the first request @@ -17254,17 +17221,10 @@ function RedirectableRequest(options, responseCallback) { RedirectableRequest.prototype = Object.create(Writable.prototype); RedirectableRequest.prototype.abort = function () { - destroyRequest(this._currentRequest); - this._currentRequest.abort(); + abortRequest(this._currentRequest); this.emit("abort"); }; -RedirectableRequest.prototype.destroy = function (error) { - destroyRequest(this._currentRequest, error); - destroy.call(this, error); - return this; -}; - // Writes buffered data to the current native request RedirectableRequest.prototype.write = function (data, encoding, callback) { // Writing is not allowed if end has been called @@ -17377,7 +17337,6 @@ RedirectableRequest.prototype.setTimeout = function (msecs, callback) { self.removeListener("abort", clearTimer); self.removeListener("error", clearTimer); self.removeListener("response", clearTimer); - self.removeListener("close", clearTimer); if (callback) { self.removeListener("timeout", callback); } @@ -17404,7 +17363,6 @@ RedirectableRequest.prototype.setTimeout = function (msecs, callback) { this.on("abort", clearTimer); this.on("error", clearTimer); this.on("response", clearTimer); - this.on("close", clearTimer); return this; }; @@ -17463,7 +17421,8 @@ RedirectableRequest.prototype._performRequest = function () { var protocol = this._options.protocol; var nativeProtocol = this._options.nativeProtocols[protocol]; if (!nativeProtocol) { - throw new TypeError("Unsupported protocol " + protocol); + this.emit("error", new TypeError("Unsupported protocol " + protocol)); + return; } // If specified, use the agent corresponding to the protocol @@ -17555,14 +17514,15 @@ RedirectableRequest.prototype._processResponse = function (response) { } // The response is a redirect, so abort the current request - destroyRequest(this._currentRequest); + abortRequest(this._currentRequest); // Discard the remainder of the response to avoid waiting for data response.destroy(); // RFC7231ยง6.4: A client SHOULD detect and intervene // in cyclical redirections (i.e., "infinite" redirection loops). if (++this._redirectCount > this._options.maxRedirects) { - throw new TooManyRedirectsError(); + this.emit("error", new TooManyRedirectsError()); + return; } // Store the request headers if applicable @@ -17596,23 +17556,33 @@ RedirectableRequest.prototype._processResponse = function (response) { var currentHostHeader = removeMatchingHeaders(/^host$/i, this._options.headers); // If the redirect is relative, carry over the host of the last request - var currentUrlParts = parseUrl(this._currentUrl); + var currentUrlParts = url.parse(this._currentUrl); var currentHost = currentHostHeader || currentUrlParts.host; var currentUrl = /^\w+:/.test(location) ? this._currentUrl : url.format(Object.assign(currentUrlParts, { host: currentHost })); + // Determine the URL of the redirection + var redirectUrl; + try { + redirectUrl = url.resolve(currentUrl, location); + } + catch (cause) { + this.emit("error", new RedirectionError({ cause: cause })); + return; + } + // Create the redirected request - var redirectUrl = resolveUrl(location, currentUrl); - debug("redirecting to", redirectUrl.href); + debug("redirecting to", redirectUrl); this._isRedirect = true; - spreadUrlObject(redirectUrl, this._options); + var redirectUrlParts = url.parse(redirectUrl); + Object.assign(this._options, redirectUrlParts); // Drop confidential headers when redirecting to a less secure protocol // or to a different domain that is not a superdomain - if (redirectUrl.protocol !== currentUrlParts.protocol && - redirectUrl.protocol !== "https:" || - redirectUrl.host !== currentHost && - !isSubdomain(redirectUrl.host, currentHost)) { + if (redirectUrlParts.protocol !== currentUrlParts.protocol && + redirectUrlParts.protocol !== "https:" || + redirectUrlParts.host !== currentHost && + !isSubdomain(redirectUrlParts.host, currentHost)) { removeMatchingHeaders(/^(?:authorization|cookie)$/i, this._options.headers); } @@ -17627,12 +17597,23 @@ RedirectableRequest.prototype._processResponse = function (response) { method: method, headers: requestHeaders, }; - beforeRedirect(this._options, responseDetails, requestDetails); + try { + beforeRedirect(this._options, responseDetails, requestDetails); + } + catch (err) { + this.emit("error", err); + return; + } this._sanitizeOptions(this._options); } // Perform the redirected request - this._performRequest(); + try { + this._performRequest(); + } + catch (cause) { + this.emit("error", new RedirectionError({ cause: cause })); + } }; // Wraps the key/value object of protocols with redirect functionality @@ -17652,16 +17633,27 @@ function wrap(protocols) { // Executes a request, following redirects function request(input, options, callback) { - // Parse parameters, ensuring that input is an object - if (isURL(input)) { - input = spreadUrlObject(input); + // Parse parameters + if (isString(input)) { + var parsed; + try { + parsed = urlToOptions(new URL(input)); + } + catch (err) { + /* istanbul ignore next */ + parsed = url.parse(input); + } + if (!isString(parsed.protocol)) { + throw new InvalidUrlError({ input }); + } + input = parsed; } - else if (isString(input)) { - input = spreadUrlObject(parseUrl(input)); + else if (URL && (input instanceof URL)) { + input = urlToOptions(input); } else { callback = options; - options = validateUrl(input); + options = input; input = { protocol: protocol }; } if (isFunction(options)) { @@ -17700,57 +17692,27 @@ function wrap(protocols) { return exports; } +/* istanbul ignore next */ function noop() { /* empty */ } -function parseUrl(input) { - var parsed; - /* istanbul ignore else */ - if (useNativeURL) { - parsed = new URL(input); - } - else { - // Ensure the URL is valid and absolute - parsed = validateUrl(url.parse(input)); - if (!isString(parsed.protocol)) { - throw new InvalidUrlError({ input }); - } - } - return parsed; -} - -function resolveUrl(relative, base) { - /* istanbul ignore next */ - return useNativeURL ? new URL(relative, base) : parseUrl(url.resolve(base, relative)); -} - -function validateUrl(input) { - if (/^\[/.test(input.hostname) && !/^\[[:0-9a-f]+\]$/i.test(input.hostname)) { - throw new InvalidUrlError({ input: input.href || input }); - } - if (/^\[/.test(input.host) && !/^\[[:0-9a-f]+\](:\d+)?$/i.test(input.host)) { - throw new InvalidUrlError({ input: input.href || input }); - } - return input; -} - -function spreadUrlObject(urlObject, target) { - var spread = target || {}; - for (var key of preservedUrlFields) { - spread[key] = urlObject[key]; - } - - // Fix IPv6 hostname - if (spread.hostname.startsWith("[")) { - spread.hostname = spread.hostname.slice(1, -1); - } - // Ensure port is a number - if (spread.port !== "") { - spread.port = Number(spread.port); +// from https://github.com/nodejs/node/blob/master/lib/internal/url.js +function urlToOptions(urlObject) { + var options = { + protocol: urlObject.protocol, + hostname: urlObject.hostname.startsWith("[") ? + /* istanbul ignore next */ + urlObject.hostname.slice(1, -1) : + urlObject.hostname, + hash: urlObject.hash, + search: urlObject.search, + pathname: urlObject.pathname, + path: urlObject.pathname + urlObject.search, + href: urlObject.href, + }; + if (urlObject.port !== "") { + options.port = Number(urlObject.port); } - // Concatenate path - spread.path = spread.search ? spread.pathname + spread.search : spread.pathname; - - return spread; + return options; } function removeMatchingHeaders(regex, headers) { @@ -17776,25 +17738,17 @@ function createErrorType(code, message, baseClass) { // Attach constructor and set default properties CustomError.prototype = new (baseClass || Error)(); - Object.defineProperties(CustomError.prototype, { - constructor: { - value: CustomError, - enumerable: false, - }, - name: { - value: "Error [" + code + "]", - enumerable: false, - }, - }); + CustomError.prototype.constructor = CustomError; + CustomError.prototype.name = "Error [" + code + "]"; return CustomError; } -function destroyRequest(request, error) { +function abortRequest(request) { for (var event of events) { request.removeListener(event, eventHandlers[event]); } request.on("error", noop); - request.destroy(error); + request.abort(); } function isSubdomain(subdomain, domain) { @@ -17815,10 +17769,6 @@ function isBuffer(value) { return typeof value === "object" && ("length" in value); } -function isURL(value) { - return URL && value instanceof URL; -} - // Exports module.exports = wrap({ http: http, https: https }); module.exports.wrap = wrap; diff --git a/yarn.lock b/yarn.lock index f5c517e7ab5614..c081c473c1e44b 100644 --- a/yarn.lock +++ b/yarn.lock @@ -14290,9 +14290,9 @@ folktale@2.3.2: integrity sha512-+8GbtQBwEqutP0v3uajDDoN64K2ehmHd0cjlghhxh0WpcfPzAIjPA03e1VvHlxL02FVGR0A6lwXsNQKn3H1RNQ== follow-redirects@^1.0.0, follow-redirects@^1.15.0: - version "1.15.4" - resolved "https://registry.yarnpkg.com/follow-redirects/-/follow-redirects-1.15.4.tgz#cdc7d308bf6493126b17ea2191ea0ccf3e535adf" - integrity sha512-Cr4D/5wlrb0z9dgERpUL3LrmPKVDsETIJhaCMeDfuFYcqa5bldGV6wBsAN6X/vxlXQtFBMrXdXxdL8CbDTGniw== + version "1.15.2" + resolved "https://registry.yarnpkg.com/follow-redirects/-/follow-redirects-1.15.2.tgz#b460864144ba63f2681096f274c4e57026da2c13" + integrity sha512-VQLG33o04KaQ8uYi2tVNbdrWp1QWxNNea+nmIB4EVM28v0hmP17z7aG1+wAkNzVq4KeXTq3221ye5qTJP91JwA== font-awesome@4.7.0: version "4.7.0"