Skip to content

Commit b44c9b2

Browse files
committed
handle content encoding gzip from service calls
1 parent a308b39 commit b44c9b2

3 files changed

Lines changed: 20 additions & 25 deletions

File tree

src/vs/base/node/request.ts

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,11 @@ import { Promise, TPromise } from 'vs/base/common/winjs.base';
99
import { isBoolean } from 'vs/base/common/types';
1010
import https = require('https');
1111
import http = require('http');
12+
import { Stream } from 'stream';
1213
import { parse as parseUrl } from 'url';
1314
import { createWriteStream } from 'fs';
1415
import { assign } from 'vs/base/common/objects';
16+
import { createGunzip } from 'zlib';
1517

1618
export interface IRequestOptions {
1719
type?: string;
@@ -29,6 +31,7 @@ export interface IRequestOptions {
2931
export interface IRequestResult {
3032
req: http.ClientRequest;
3133
res: http.ClientResponse;
34+
stream: Stream;
3235
}
3336

3437
export function request(options: IRequestOptions): TPromise<IRequestResult> {
@@ -59,7 +62,13 @@ export function request(options: IRequestOptions): TPromise<IRequestResult> {
5962
followRedirects: options.followRedirects - 1
6063
})));
6164
} else {
62-
c({ req, res });
65+
let stream: Stream = res;
66+
67+
if (res.headers['content-encoding'] === 'gzip') {
68+
stream = stream.pipe(createGunzip());
69+
}
70+
71+
c({ req, res, stream });
6372
}
6473
});
6574
req.on('error', e);
@@ -81,8 +90,8 @@ export function download(filePath: string, opts: IRequestOptions): TPromise<void
8190
let out = createWriteStream(filePath);
8291

8392
out.once('finish', () => c(null));
84-
pair.res.once('error', e);
85-
pair.res.pipe(out);
93+
pair.stream.once('error', e);
94+
pair.stream.pipe(out);
8695
}));
8796
}
8897

@@ -97,9 +106,9 @@ export function text(opts: IRequestOptions): TPromise<string> {
97106
}
98107

99108
let buffer: string[] = [];
100-
pair.res.on('data', d => buffer.push(d));
101-
pair.res.on('end', () => c(buffer.join('')));
102-
pair.res.on('error', e);
109+
pair.stream.on('data', d => buffer.push(d));
110+
pair.stream.on('end', () => c(buffer.join('')));
111+
pair.stream.on('error', e);
103112
}));
104113
}
105114

@@ -118,8 +127,8 @@ export function json<T>(opts: IRequestOptions): TPromise<T> {
118127
}
119128

120129
let buffer: string[] = [];
121-
pair.res.on('data', d => buffer.push(d));
122-
pair.res.on('end', () => c(JSON.parse(buffer.join(''))));
123-
pair.res.on('error', e);
130+
pair.stream.on('data', d => buffer.push(d));
131+
pair.stream.on('end', () => c(JSON.parse(buffer.join(''))));
132+
pair.stream.on('error', e);
124133
}));
125134
}

src/vs/platform/request/node/nodeRequestService.ts

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,6 @@ import { assign } from 'vs/base/common/objects';
1414
import { IXHROptions, IXHRResponse } from 'vs/base/common/http';
1515
import { request } from 'vs/base/node/request';
1616
import { getProxyAgent } from 'vs/base/node/proxy';
17-
import { createGunzip } from 'zlib';
18-
import { Stream } from 'stream';
1917

2018
interface IHTTPConfiguration {
2119
http?: {
@@ -65,12 +63,7 @@ export class NodeRequestService implements IRequestService {
6563
options = assign(options, { agent, strictSSL });
6664

6765
return request(options).then(result => new TPromise<IXHRResponse>((c, e, p) => {
68-
const res = result.res;
69-
let stream: Stream = res;
70-
71-
if (res.headers['content-encoding'] === 'gzip') {
72-
stream = stream.pipe(createGunzip());
73-
}
66+
const { res, stream } = result;
7467

7568
const data: string[] = [];
7669
stream.on('data', c => data.push(c));

src/vs/workbench/services/request/node/requestService.ts

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,6 @@ import { assign } from 'vs/base/common/objects';
2020
import { IXHROptions, IXHRResponse } from 'vs/base/common/http';
2121
import { request } from 'vs/base/node/request';
2222
import { getProxyAgent } from 'vs/base/node/proxy';
23-
import { createGunzip } from 'zlib';
24-
import { Stream } from 'stream';
2523

2624
interface IHTTPConfiguration {
2725
http?: {
@@ -83,12 +81,7 @@ export class RequestService extends BaseRequestService {
8381
options = assign(options, { agent, strictSSL });
8482

8583
return request(options).then(result => new TPromise<IXHRResponse>((c, e, p) => {
86-
const res = result.res;
87-
let stream: Stream = res;
88-
89-
if (res.headers['content-encoding'] === 'gzip') {
90-
stream = stream.pipe(createGunzip());
91-
}
84+
const { res, stream } = result;
9285

9386
const data: string[] = [];
9487
stream.on('data', c => data.push(c));

0 commit comments

Comments
 (0)