From 4e1bef95373e88d8e00a9bca719eef3b2813acfc Mon Sep 17 00:00:00 2001 From: Julien Fontanet Date: Mon, 9 Jan 2017 18:29:54 +0100 Subject: [PATCH] feat: redirect event --- packages/xen-api/src/index.js | 22 +++++++++++++++++++++- 1 file changed, 21 insertions(+), 1 deletion(-) diff --git a/packages/xen-api/src/index.js b/packages/xen-api/src/index.js index 0ee8f6798..7a49f15ca 100644 --- a/packages/xen-api/src/index.js +++ b/packages/xen-api/src/index.js @@ -93,6 +93,20 @@ export const wrapError = error => new XapiError(error) // =================================================================== +const formatUrl = url => { + const parts = [] + + if (!url.isSecure) { + parts.push('http://') + } + parts.push(url.hostname) + if (url.port != null) { + parts.push(':', url.port) + } + + return parts.join('') +} + const URL_RE = /^(?:(http(s)?:)\/*)?([^/]+?)(?::([0-9]+))?\/?$/ function parseUrl (url) { const matches = URL_RE.exec(url) @@ -442,7 +456,13 @@ export class Xapi extends EventEmitter { ::pCatch(isHostSlave, ({params: [master]}) => { debug('%s: host is slave, attempting to connect at %s', this._humanId, master) - this._url.hostname = master + const newUrl = { + ...this._url, + hostname: master + } + this.emit('redirect', formatUrl(newUrl)) + this._url = newUrl + this._init() return this._transportCall(method, args, startTime)