From dd06daf898ef4468cfc1248b72c75af05aa892d1 Mon Sep 17 00:00:00 2001 From: Joao Moreno Date: Fri, 18 Aug 2017 12:18:08 +0200 Subject: [PATCH] fixes #32381 --- src/vs/base/node/request.ts | 92 +++++++++++++++++++------------------ 1 file changed, 47 insertions(+), 45 deletions(-) diff --git a/src/vs/base/node/request.ts b/src/vs/base/node/request.ts index d6d05396fc6..d46e8b6e58c 100644 --- a/src/vs/base/node/request.ts +++ b/src/vs/base/node/request.ts @@ -58,59 +58,61 @@ async function getNodeRequest(options: IRequestOptions): TPromise { let req: http.ClientRequest; - return new TPromise(async (c, e) => { - const endpoint = parseUrl(options.url); - const rawRequest = options.getRawRequest - ? options.getRawRequest(options) - : await getNodeRequest(options); + const rawRequestPromise = options.getRawRequest + ? TPromise.as(options.getRawRequest(options)) + : getNodeRequest(options); - const opts: https.RequestOptions = { - hostname: endpoint.hostname, - port: endpoint.port ? parseInt(endpoint.port) : (endpoint.protocol === 'https:' ? 443 : 80), - protocol: endpoint.protocol, - path: endpoint.path, - method: options.type || 'GET', - headers: options.headers, - agent: options.agent, - rejectUnauthorized: isBoolean(options.strictSSL) ? options.strictSSL : true - }; + return rawRequestPromise.then(rawRequest => { + return new TPromise((c, e) => { + const endpoint = parseUrl(options.url); - if (options.user && options.password) { - opts.auth = options.user + ':' + options.password; - } + const opts: https.RequestOptions = { + hostname: endpoint.hostname, + port: endpoint.port ? parseInt(endpoint.port) : (endpoint.protocol === 'https:' ? 443 : 80), + protocol: endpoint.protocol, + path: endpoint.path, + method: options.type || 'GET', + headers: options.headers, + agent: options.agent, + rejectUnauthorized: isBoolean(options.strictSSL) ? options.strictSSL : true + }; - req = rawRequest(opts, (res: http.ClientResponse) => { - const followRedirects = isNumber(options.followRedirects) ? options.followRedirects : 3; - - if (res.statusCode >= 300 && res.statusCode < 400 && followRedirects > 0 && res.headers['location']) { - request(assign({}, options, { - url: res.headers['location'], - followRedirects: followRedirects - 1 - })).done(c, e); - } else { - let stream: Stream = res; - - if (res.headers['content-encoding'] === 'gzip') { - stream = stream.pipe(createGunzip()); - } - - c({ res, stream }); + if (options.user && options.password) { + opts.auth = options.user + ':' + options.password; } - }); - req.on('error', e); + req = rawRequest(opts, (res: http.ClientResponse) => { + const followRedirects = isNumber(options.followRedirects) ? options.followRedirects : 3; - if (options.timeout) { - req.setTimeout(options.timeout); - } + if (res.statusCode >= 300 && res.statusCode < 400 && followRedirects > 0 && res.headers['location']) { + request(assign({}, options, { + url: res.headers['location'], + followRedirects: followRedirects - 1 + })).done(c, e); + } else { + let stream: Stream = res; - if (options.data) { - req.write(options.data); - } + if (res.headers['content-encoding'] === 'gzip') { + stream = stream.pipe(createGunzip()); + } - req.end(); - }, - () => req && req.abort()); + c({ res, stream }); + } + }); + + req.on('error', e); + + if (options.timeout) { + req.setTimeout(options.timeout); + } + + if (options.data) { + req.write(options.data); + } + + req.end(); + }, () => req && req.abort()); + }); } function isSuccess(context: IRequestContext): boolean {