Skip to content

Commit 9b92779

Browse files
committed
Add support for gzip/deflate
1 parent 042e97b commit 9b92779

File tree

1 file changed

+17
-5
lines changed

1 file changed

+17
-5
lines changed

src/main.ts

+17-5
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,12 @@
55

66
'use strict';
77

8-
import {Url, parse as parseUrl} from 'url';
8+
import { Url, parse as parseUrl } from 'url';
99
import https = require('https');
1010
import http = require('http');
1111
import HttpProxyAgent = require('http-proxy-agent');
1212
import HttpsProxyAgent = require('https-proxy-agent');
13+
import zlib = require('zlib');
1314

1415
import * as nls from 'vscode-nls';
1516
nls.config(process.env['VSCODE_NLS_CONFIG']);
@@ -57,9 +58,20 @@ export function xhr(options: XHROptions): Promise<XHRResponse> {
5758

5859
return request(options).then(result => new Promise<XHRResponse>((c, e) => {
5960
let res = result.res;
61+
let readable: NodeJS.ReadableStream = res;
62+
let encoding = res.headers && res.headers['content-encoding'];
63+
if (encoding === 'gzip') {
64+
let gunzip = zlib.createGunzip();
65+
res.pipe(gunzip);
66+
readable = gunzip;
67+
} else if (encoding === 'deflate') {
68+
let inflate = zlib.createInflate();
69+
res.pipe(inflate);
70+
readable = inflate;
71+
}
6072
let data: any = [];
61-
res.on('data', c => data.push(c));
62-
res.on('end', () => {
73+
readable.on('data', c => data.push(c));
74+
readable.on('end', () => {
6375
if (options.followRedirects > 0 && (res.statusCode >= 300 && res.statusCode <= 303 || res.statusCode === 307)) {
6476
let location = res.headers['location'];
6577
if (location) {
@@ -131,7 +143,7 @@ function request(options: XHROptions): Promise<RequestResult> {
131143

132144
let handler = (res: http.ClientResponse) => {
133145
if (res.statusCode >= 300 && res.statusCode < 400 && options.followRedirects && options.followRedirects > 0 && res.headers['location']) {
134-
c(<any> request(assign({}, options, {
146+
c(<any>request(assign({}, options, {
135147
url: res.headers['location'],
136148
followRedirects: options.followRedirects - 1
137149
})));
@@ -158,7 +170,7 @@ function request(options: XHROptions): Promise<RequestResult> {
158170
});
159171
}
160172

161-
export function getErrorStatusDescription(status: number) : string {
173+
export function getErrorStatusDescription(status: number): string {
162174
if (status < 400) {
163175
return void 0;
164176
}

0 commit comments

Comments
 (0)