mirror of
https://github.com/microsoft/vscode.git
synced 2026-04-21 09:08:53 +01:00
make json, download not call request
This commit is contained in:
@@ -6,7 +6,7 @@
|
||||
'use strict';
|
||||
|
||||
import { Promise, TPromise } from 'vs/base/common/winjs.base';
|
||||
import { isBoolean } from 'vs/base/common/types';
|
||||
import { isBoolean, isNumber } from 'vs/base/common/types';
|
||||
import https = require('https');
|
||||
import http = require('http');
|
||||
import { Stream } from 'stream';
|
||||
@@ -40,9 +40,9 @@ export function request(options: IRequestOptions): TPromise<IRequestContext> {
|
||||
let req: http.ClientRequest;
|
||||
|
||||
return new TPromise<IRequestContext>((c, e) => {
|
||||
let endpoint = parseUrl(options.url);
|
||||
|
||||
let opts: https.RequestOptions = {
|
||||
const endpoint = parseUrl(options.url);
|
||||
const protocol = endpoint.protocol === 'https:' ? https : http;
|
||||
const opts: https.RequestOptions = {
|
||||
hostname: endpoint.hostname,
|
||||
port: endpoint.port ? parseInt(endpoint.port) : (endpoint.protocol === 'https:' ? 443 : 80),
|
||||
path: endpoint.path,
|
||||
@@ -56,12 +56,13 @@ export function request(options: IRequestOptions): TPromise<IRequestContext> {
|
||||
opts.auth = options.user + ':' + options.password;
|
||||
}
|
||||
|
||||
let protocol = endpoint.protocol === 'https:' ? https : http;
|
||||
req = protocol.request(opts, (res: http.ClientResponse) => {
|
||||
if (res.statusCode >= 300 && res.statusCode < 400 && options.followRedirects && options.followRedirects > 0 && res.headers['location']) {
|
||||
const followRedirects = isNumber(options.followRedirects) ? options.followRedirects : 3;
|
||||
|
||||
if (res.statusCode >= 300 && res.statusCode < 400 && followRedirects > 0 && res.headers['location']) {
|
||||
c(<any> request(assign({}, options, {
|
||||
url: res.headers['location'],
|
||||
followRedirects: options.followRedirects - 1
|
||||
followRedirects: followRedirects - 1
|
||||
})));
|
||||
} else {
|
||||
let stream: Stream = res;
|
||||
@@ -73,11 +74,13 @@ export function request(options: IRequestOptions): TPromise<IRequestContext> {
|
||||
c({ req, res, stream });
|
||||
}
|
||||
});
|
||||
|
||||
req.on('error', e);
|
||||
|
||||
if (options.timeout) {
|
||||
req.setTimeout(options.timeout);
|
||||
}
|
||||
|
||||
if (options.data) {
|
||||
req.write(options.data);
|
||||
}
|
||||
@@ -87,50 +90,41 @@ export function request(options: IRequestOptions): TPromise<IRequestContext> {
|
||||
() => req && req.abort());
|
||||
}
|
||||
|
||||
export function download(filePath: string, opts: IRequestOptions): TPromise<void> {
|
||||
return request(assign(opts, { followRedirects: 3 })).then(pair => new TPromise<void>((c, e) => {
|
||||
let out = createWriteStream(filePath);
|
||||
function isSuccess(context: IRequestContext): boolean {
|
||||
return (context.res.statusCode >= 200 && context.res.statusCode < 300) || context.res.statusCode === 1223;
|
||||
}
|
||||
|
||||
function hasNoContent(context: IRequestContext): boolean {
|
||||
return context.res.statusCode === 204;
|
||||
}
|
||||
|
||||
export function download(filePath: string, context: IRequestContext): TPromise<void> {
|
||||
return new TPromise<void>((c, e) => {
|
||||
const out = createWriteStream(filePath);
|
||||
|
||||
out.once('finish', () => c(null));
|
||||
pair.stream.once('error', e);
|
||||
pair.stream.pipe(out);
|
||||
}));
|
||||
context.stream.once('error', e);
|
||||
context.stream.pipe(out);
|
||||
});
|
||||
}
|
||||
|
||||
export function text(opts: IRequestOptions): TPromise<string> {
|
||||
return request(opts).then(pair => new Promise((c, e) => {
|
||||
if (!((pair.res.statusCode >= 200 && pair.res.statusCode < 300) || pair.res.statusCode === 1223)) {
|
||||
return e('Server returned ' + pair.res.statusCode);
|
||||
export function json<T>(context: IRequestContext): TPromise<T> {
|
||||
return new Promise((c, e) => {
|
||||
if (!isSuccess(context)) {
|
||||
return e('Server returned ' + context.res.statusCode);
|
||||
}
|
||||
|
||||
if (pair.res.statusCode === 204) {
|
||||
if (hasNoContent(context)) {
|
||||
return c(null);
|
||||
}
|
||||
|
||||
let buffer: string[] = [];
|
||||
pair.stream.on('data', d => buffer.push(d));
|
||||
pair.stream.on('end', () => c(buffer.join('')));
|
||||
pair.stream.on('error', e);
|
||||
}));
|
||||
}
|
||||
|
||||
export function json<T>(opts: IRequestOptions): TPromise<T> {
|
||||
return request(opts).then(pair => new Promise((c, e) => {
|
||||
if (!((pair.res.statusCode >= 200 && pair.res.statusCode < 300) || pair.res.statusCode === 1223)) {
|
||||
return e('Server returned ' + pair.res.statusCode);
|
||||
}
|
||||
|
||||
if (pair.res.statusCode === 204) {
|
||||
return c(null);
|
||||
}
|
||||
|
||||
if (!/application\/json/.test(pair.res.headers['content-type'])) {
|
||||
if (!/application\/json/.test(context.res.headers['content-type'])) {
|
||||
return e('Response doesn\'t appear to be JSON');
|
||||
}
|
||||
|
||||
let buffer: string[] = [];
|
||||
pair.stream.on('data', d => buffer.push(d));
|
||||
pair.stream.on('end', () => c(JSON.parse(buffer.join(''))));
|
||||
pair.stream.on('error', e);
|
||||
}));
|
||||
const buffer: string[] = [];
|
||||
context.stream.on('data', d => buffer.push(d));
|
||||
context.stream.on('end', () => c(JSON.parse(buffer.join(''))));
|
||||
context.stream.on('error', e);
|
||||
});
|
||||
}
|
||||
Reference in New Issue
Block a user