browser suppert `CancellationToken`

This commit is contained in:
Tarrowren 2022-11-02 11:16:35 +08:00
Родитель 2bbcee6194
Коммит 2247d736d4
5 изменённых файлов: 258 добавлений и 239 удалений

3
api.d.ts поставляемый
Просмотреть файл

@ -12,6 +12,7 @@ export interface XHROptions {
data?: string;
strictSSL?: boolean;
followRedirects?: number;
token?: import("vscode-jsonrpc").CancellationToken;
agent?: HttpProxyAgent | HttpsProxyAgent;
}
@ -39,4 +40,4 @@ export type Headers = { [header: string]: string | string[] | undefined };
export declare const configure: XHRConfigure;
export declare const xhr: XHRRequest;
export declare function getErrorStatusDescription(status: number): string;
export declare function getErrorStatusDescription(status: number): string;

6
package-lock.json сгенерированный
Просмотреть файл

@ -2330,6 +2330,12 @@
"punycode": "^2.1.0"
}
},
"vscode-jsonrpc": {
"version": "5.0.0",
"resolved": "https://registry.npmjs.org/vscode-jsonrpc/-/vscode-jsonrpc-5.0.0.tgz",
"integrity": "sha512-QeAniC/xTWauVQgyNgEqNJ0Qm/Jw8QySGRQhRFPwb8c4FPp9k6QNgJp0ayXWws5qhdaHkiXkGPlzjOPZFQQKLw==",
"dev": true
},
"vscode-nls": {
"version": "5.0.0",
"resolved": "https://registry.npmjs.org/vscode-nls/-/vscode-nls-5.0.0.tgz",

Просмотреть файл

@ -20,6 +20,7 @@
"http-proxy-agent": "^5.0.0",
"https-proxy-agent": "^5.0.0",
"vscode-nls": "^5.0.0",
"vscode-jsonrpc": "^5.0.0",
"typescript": "^4.5.4",
"@types/node": "13.7.6",
"rimraf": "^3.0.2",

Просмотреть файл

@ -8,47 +8,58 @@ import { XHRRequest, XHRConfigure, XHROptions, XHRResponse } from '../../api';
export const configure: XHRConfigure = (_proxyUrl: string, _strictSSL: boolean) => { };
export const xhr: XHRRequest = async (options: XHROptions): Promise<XHRResponse> => {
const requestHeaders = new Headers();
if (options.headers) {
for (const key in options.headers) {
const value = options.headers[key];
if (Array.isArray(value)) {
value.forEach(v => requestHeaders.set(key, v))
} else {
requestHeaders.set(key, value);
}
}
}
if (options.user && options.password) {
requestHeaders.set('Authorization', 'Basic ' + btoa(options.user + ":" + options.password));
}
const requestInit: RequestInit = {
method: options.type,
redirect: options.followRedirects > 0 ? 'follow' : 'manual',
mode: 'cors',
headers: requestHeaders
};
if (options.data) {
requestInit.body = options.data;
}
const requestHeaders = new Headers();
if (options.headers) {
for (const key in options.headers) {
const value = options.headers[key];
if (Array.isArray(value)) {
value.forEach(v => requestHeaders.set(key, v))
} else {
requestHeaders.set(key, value);
}
}
}
if (options.user && options.password) {
requestHeaders.set('Authorization', 'Basic ' + btoa(options.user + ":" + options.password));
}
const requestInit: RequestInit = {
method: options.type,
redirect: options.followRedirects > 0 ? 'follow' : 'manual',
mode: 'cors',
headers: requestHeaders
};
if (options.data) {
requestInit.body = options.data;
}
if (options.token) {
const controller = new AbortController();
if (options.token.isCancellationRequested) {
// see https://github.com/microsoft/TypeScript/issues/49609
(controller as any).abort();
}
options.token.onCancellationRequested(() => {
(controller as any).abort();
});
requestInit.signal = controller.signal;
}
const requestInfo = new Request(options.url, requestInit);
const response = await fetch(requestInfo);
const resposeHeaders: any = {};
response.headers.forEach((value, key) => {
resposeHeaders[key] = value;
});
const requestInfo = new Request(options.url, requestInit);
const response = await fetch(requestInfo);
const resposeHeaders: any = {};
response.headers.forEach((value, key) => {
resposeHeaders[key] = value;
});
const buffer = await response.arrayBuffer();
const buffer = await response.arrayBuffer();
return new class {
get responseText() { return new TextDecoder().decode(buffer); };
get body() { return new Uint8Array(buffer) };
readonly status = response.status;
readonly headers = resposeHeaders;
}
return new class {
get responseText() { return new TextDecoder().decode(buffer); };
get body() { return new Uint8Array(buffer) };
readonly status = response.status;
readonly headers = resposeHeaders;
}
}
export function getErrorStatusDescription(status: number): string {
return String(status);
}
return String(status);
}

Просмотреть файл

@ -14,8 +14,8 @@ import * as createHttpProxyAgent from 'http-proxy-agent';
import { XHRRequest, XHRConfigure, XHROptions, XHRResponse, HttpProxyAgent, HttpsProxyAgent } from '../../api';
if (process.env.VSCODE_NLS_CONFIG) {
const VSCODE_NLS_CONFIG = process.env.VSCODE_NLS_CONFIG;
nls.config(JSON.parse(VSCODE_NLS_CONFIG));
const VSCODE_NLS_CONFIG = process.env.VSCODE_NLS_CONFIG;
nls.config(JSON.parse(VSCODE_NLS_CONFIG));
}
const localize = nls.loadMessageBundle();
@ -23,252 +23,252 @@ let proxyUrl: string | undefined = undefined;
let strictSSL: boolean = true;
export const configure: XHRConfigure = (_proxyUrl: string | undefined, _strictSSL: boolean) => {
proxyUrl = _proxyUrl;
strictSSL = _strictSSL;
proxyUrl = _proxyUrl;
strictSSL = _strictSSL;
};
export const xhr: XHRRequest = (options: XHROptions): Promise<XHRResponse> => {
options = { ...options };
options = { ...options };
if (typeof options.strictSSL !== 'boolean') {
options.strictSSL = strictSSL;
}
if (!options.agent) {
options.agent = getProxyAgent(options.url, { proxyUrl, strictSSL });
}
if (typeof options.followRedirects !== 'number') {
options.followRedirects = 5;
}
if (typeof options.strictSSL !== 'boolean') {
options.strictSSL = strictSSL;
}
if (!options.agent) {
options.agent = getProxyAgent(options.url, { proxyUrl, strictSSL });
}
if (typeof options.followRedirects !== 'number') {
options.followRedirects = 5;
}
return request(options).then(result => new Promise<XHRResponse>((c, e) => {
const res = result.res;
let readable: NodeJS.ReadableStream = res;
let isCompleted = false;
return request(options).then(result => new Promise<XHRResponse>((c, e) => {
const res = result.res;
let readable: NodeJS.ReadableStream = res;
let isCompleted = false;
const encoding = res.headers && res.headers['content-encoding'];
if (encoding && !hasNoBody(options.type, result.res.statusCode)) {
const zlibOptions = {
flush: zlib.constants.Z_SYNC_FLUSH,
finishFlush: zlib.constants.Z_SYNC_FLUSH
};
if (encoding === 'gzip') {
const gunzip = zlib.createGunzip(zlibOptions);
res.pipe(gunzip);
readable = gunzip;
} else if (encoding === 'deflate') {
const inflate = zlib.createInflate(zlibOptions);
res.pipe(inflate);
readable = inflate;
}
}
const data: any = [];
readable.on('data', c => data.push(c));
readable.on('end', () => {
if (isCompleted) {
return;
}
isCompleted = true;
if (options.followRedirects > 0 && (res.statusCode >= 300 && res.statusCode <= 303 || res.statusCode === 307)) {
let location = res.headers['location'];
if (location.startsWith('/')) {
const endpoint = parseUrl(options.url);
location = format({
protocol: endpoint.protocol,
hostname: endpoint.hostname,
port: endpoint.port,
pathname: location
});
}
if (location) {
const newOptions = {
type: options.type, url: location, user: options.user, password: options.password, headers: options.headers,
timeout: options.timeout, followRedirects: options.followRedirects - 1, data: options.data
};
xhr(newOptions).then(c, e);
return;
}
}
const encoding = res.headers && res.headers['content-encoding'];
if (encoding && !hasNoBody(options.type, result.res.statusCode)) {
const zlibOptions = {
flush: zlib.constants.Z_SYNC_FLUSH,
finishFlush: zlib.constants.Z_SYNC_FLUSH
};
if (encoding === 'gzip') {
const gunzip = zlib.createGunzip(zlibOptions);
res.pipe(gunzip);
readable = gunzip;
} else if (encoding === 'deflate') {
const inflate = zlib.createInflate(zlibOptions);
res.pipe(inflate);
readable = inflate;
}
}
const data: any = [];
readable.on('data', c => data.push(c));
readable.on('end', () => {
if (isCompleted) {
return;
}
isCompleted = true;
if (options.followRedirects > 0 && (res.statusCode >= 300 && res.statusCode <= 303 || res.statusCode === 307)) {
let location = res.headers['location'];
if (location.startsWith('/')) {
const endpoint = parseUrl(options.url);
location = format({
protocol: endpoint.protocol,
hostname: endpoint.hostname,
port: endpoint.port,
pathname: location
});
}
if (location) {
const newOptions = {
type: options.type, url: location, user: options.user, password: options.password, headers: options.headers,
timeout: options.timeout, followRedirects: options.followRedirects - 1, data: options.data
};
xhr(newOptions).then(c, e);
return;
}
}
const buffer = Buffer.concat(data);
const buffer = Buffer.concat(data);
const response: XHRResponse = {
responseText: buffer.toString(),
body: buffer,
status: res.statusCode,
headers: res.headers || {}
};
const response: XHRResponse = {
responseText: buffer.toString(),
body: buffer,
status: res.statusCode,
headers: res.headers || {}
};
if ((res.statusCode >= 200 && res.statusCode < 300) || res.statusCode === 1223) {
c(response);
} else {
e(response);
}
});
readable.on('error', (err) => {
const response: XHRResponse = {
responseText: localize('error', 'Unable to access {0}. Error: {1}', options.url, err.message),
body: Buffer.concat(data),
status: 500,
headers: {}
};
isCompleted = true;
e(response);
});
}), err => {
let message: string;
if ((res.statusCode >= 200 && res.statusCode < 300) || res.statusCode === 1223) {
c(response);
} else {
e(response);
}
});
readable.on('error', (err) => {
const response: XHRResponse = {
responseText: localize('error', 'Unable to access {0}. Error: {1}', options.url, err.message),
body: Buffer.concat(data),
status: 500,
headers: {}
};
isCompleted = true;
e(response);
});
}), err => {
let message: string;
if (options.agent) {
message = localize('error.cannot.connect.proxy', 'Unable to connect to {0} through a proxy. Error: {1}', options.url, err.message);
} else {
message = localize('error.cannot.connect', 'Unable to connect to {0}. Error: {1}', options.url, err.message);
}
if (options.agent) {
message = localize('error.cannot.connect.proxy', 'Unable to connect to {0} through a proxy. Error: {1}', options.url, err.message);
} else {
message = localize('error.cannot.connect', 'Unable to connect to {0}. Error: {1}', options.url, err.message);
}
return Promise.reject<XHRResponse>({
responseText: message,
body: Buffer.concat([]),
status: 404,
headers: {}
});
});
return Promise.reject<XHRResponse>({
responseText: message,
body: Buffer.concat([]),
status: 404,
headers: {}
});
});
}
function assign(destination: any, ...sources: any[]): any {
sources.forEach(source => Object.keys(source).forEach((key) => destination[key] = source[key]));
return destination;
sources.forEach(source => Object.keys(source).forEach((key) => destination[key] = source[key]));
return destination;
}
function hasNoBody(method: string, code: number) {
return method === 'HEAD' || /* Informational */ (code >= 100 && code < 200) || /* No Content */ code === 204 || /* Not Modified */ code === 304;
return method === 'HEAD' || /* Informational */ (code >= 100 && code < 200) || /* No Content */ code === 204 || /* Not Modified */ code === 304;
}
interface RequestResult {
req: http.ClientRequest;
res: http.IncomingMessage;
req: http.ClientRequest;
res: http.IncomingMessage;
}
function request(options: XHROptions): Promise<RequestResult> {
let req: http.ClientRequest;
let req: http.ClientRequest;
return new Promise<RequestResult>((c, e) => {
const endpoint = parseUrl(options.url);
return new Promise<RequestResult>((c, e) => {
const endpoint = parseUrl(options.url);
const opts: https.RequestOptions = {
hostname: endpoint.hostname,
agent: options.agent ? options.agent : false,
port: endpoint.port ? parseInt(endpoint.port) : (endpoint.protocol === 'https:' ? 443 : 80),
path: endpoint.path,
method: options.type || 'GET',
headers: options.headers,
rejectUnauthorized: (typeof options.strictSSL === 'boolean') ? options.strictSSL : true
};
const opts: https.RequestOptions = {
hostname: endpoint.hostname,
agent: options.agent ? options.agent : false,
port: endpoint.port ? parseInt(endpoint.port) : (endpoint.protocol === 'https:' ? 443 : 80),
path: endpoint.path,
method: options.type || 'GET',
headers: options.headers,
rejectUnauthorized: (typeof options.strictSSL === 'boolean') ? options.strictSSL : true
};
if (options.user && options.password) {
opts.auth = options.user + ':' + options.password;
}
if (options.user && options.password) {
opts.auth = options.user + ':' + options.password;
}
const handler = (res: http.IncomingMessage) => {
if (res.statusCode >= 300 && res.statusCode < 400 && options.followRedirects && options.followRedirects > 0 && res.headers['location']) {
let location = res.headers['location'];
if (location.startsWith('/')) {
location = format({
protocol: endpoint.protocol,
hostname: endpoint.hostname,
port: endpoint.port,
pathname: location
});
}
c(<any>request(assign({}, options, {
url: location,
followRedirects: options.followRedirects - 1
})));
} else {
c({ req, res });
}
}
if (endpoint.protocol === 'https:') {
req = https.request(opts, handler);
} else {
req = http.request(opts, handler);
}
const handler = (res: http.IncomingMessage) => {
if (res.statusCode >= 300 && res.statusCode < 400 && options.followRedirects && options.followRedirects > 0 && res.headers['location']) {
let location = res.headers['location'];
if (location.startsWith('/')) {
location = format({
protocol: endpoint.protocol,
hostname: endpoint.hostname,
port: endpoint.port,
pathname: location
});
}
c(<any>request(assign({}, options, {
url: location,
followRedirects: options.followRedirects - 1
})));
} else {
c({ req, res });
}
}
if (endpoint.protocol === 'https:') {
req = https.request(opts, handler);
} else {
req = http.request(opts, handler);
}
req.on('error', e);
req.on('error', e);
if (options.timeout) {
req.setTimeout(options.timeout);
}
if (options.data) {
req.write(options.data);
}
if (options.timeout) {
req.setTimeout(options.timeout);
}
if (options.data) {
req.write(options.data);
}
req.end();
});
req.end();
});
}
export function getErrorStatusDescription(status: number): string {
if (status < 400) {
return void 0;
}
switch (status) {
case 400: return localize('status.400', 'Bad request. The request cannot be fulfilled due to bad syntax.');
case 401: return localize('status.401', 'Unauthorized. The server is refusing to respond.');
case 403: return localize('status.403', 'Forbidden. The server is refusing to respond.');
case 404: return localize('status.404', 'Not Found. The requested location could not be found.');
case 405: return localize('status.405', 'Method not allowed. A request was made using a request method not supported by that location.');
case 406: return localize('status.406', 'Not Acceptable. The server can only generate a response that is not accepted by the client.');
case 407: return localize('status.407', 'Proxy Authentication Required. The client must first authenticate itself with the proxy.');
case 408: return localize('status.408', 'Request Timeout. The server timed out waiting for the request.');
case 409: return localize('status.409', 'Conflict. The request could not be completed because of a conflict in the request.');
case 410: return localize('status.410', 'Gone. The requested page is no longer available.');
case 411: return localize('status.411', 'Length Required. The "Content-Length" is not defined.');
case 412: return localize('status.412', 'Precondition Failed. The precondition given in the request evaluated to false by the server.');
case 413: return localize('status.413', 'Request Entity Too Large. The server will not accept the request, because the request entity is too large.');
case 414: return localize('status.414', 'Request-URI Too Long. The server will not accept the request, because the URL is too long.');
case 415: return localize('status.415', 'Unsupported Media Type. The server will not accept the request, because the media type is not supported.');
case 500: return localize('status.500', 'Internal Server Error.');
case 501: return localize('status.501', 'Not Implemented. The server either does not recognize the request method, or it lacks the ability to fulfill the request.');
case 503: return localize('status.503', 'Service Unavailable. The server is currently unavailable (overloaded or down).');
default: return localize('status.416', 'HTTP status code {0}', status);
}
if (status < 400) {
return void 0;
}
switch (status) {
case 400: return localize('status.400', 'Bad request. The request cannot be fulfilled due to bad syntax.');
case 401: return localize('status.401', 'Unauthorized. The server is refusing to respond.');
case 403: return localize('status.403', 'Forbidden. The server is refusing to respond.');
case 404: return localize('status.404', 'Not Found. The requested location could not be found.');
case 405: return localize('status.405', 'Method not allowed. A request was made using a request method not supported by that location.');
case 406: return localize('status.406', 'Not Acceptable. The server can only generate a response that is not accepted by the client.');
case 407: return localize('status.407', 'Proxy Authentication Required. The client must first authenticate itself with the proxy.');
case 408: return localize('status.408', 'Request Timeout. The server timed out waiting for the request.');
case 409: return localize('status.409', 'Conflict. The request could not be completed because of a conflict in the request.');
case 410: return localize('status.410', 'Gone. The requested page is no longer available.');
case 411: return localize('status.411', 'Length Required. The "Content-Length" is not defined.');
case 412: return localize('status.412', 'Precondition Failed. The precondition given in the request evaluated to false by the server.');
case 413: return localize('status.413', 'Request Entity Too Large. The server will not accept the request, because the request entity is too large.');
case 414: return localize('status.414', 'Request-URI Too Long. The server will not accept the request, because the URL is too long.');
case 415: return localize('status.415', 'Unsupported Media Type. The server will not accept the request, because the media type is not supported.');
case 500: return localize('status.500', 'Internal Server Error.');
case 501: return localize('status.501', 'Not Implemented. The server either does not recognize the request method, or it lacks the ability to fulfill the request.');
case 503: return localize('status.503', 'Service Unavailable. The server is currently unavailable (overloaded or down).');
default: return localize('status.416', 'HTTP status code {0}', status);
}
}
// proxy handling
function getSystemProxyURI(requestURL: Url): string {
if (requestURL.protocol === 'http:') {
return process.env.HTTP_PROXY || process.env.http_proxy || null;
} else if (requestURL.protocol === 'https:') {
return process.env.HTTPS_PROXY || process.env.https_proxy || process.env.HTTP_PROXY || process.env.http_proxy || null;
}
if (requestURL.protocol === 'http:') {
return process.env.HTTP_PROXY || process.env.http_proxy || null;
} else if (requestURL.protocol === 'https:') {
return process.env.HTTPS_PROXY || process.env.https_proxy || process.env.HTTP_PROXY || process.env.http_proxy || null;
}
return null;
return null;
}
interface ProxyOptions {
proxyUrl?: string;
strictSSL?: boolean;
proxyUrl?: string;
strictSSL?: boolean;
}
function getProxyAgent(rawRequestURL: string, options: ProxyOptions = {}): HttpProxyAgent | HttpsProxyAgent | undefined {
const requestURL = parseUrl(rawRequestURL);
const proxyURL = options.proxyUrl || getSystemProxyURI(requestURL);
const requestURL = parseUrl(rawRequestURL);
const proxyURL = options.proxyUrl || getSystemProxyURI(requestURL);
if (!proxyURL) {
return null;
}
if (!proxyURL) {
return null;
}
const proxyEndpoint = parseUrl(proxyURL);
const proxyEndpoint = parseUrl(proxyURL);
if (!/^https?:$/.test(proxyEndpoint.protocol)) {
return null;
}
if (!/^https?:$/.test(proxyEndpoint.protocol)) {
return null;
}
const opts = {
host: proxyEndpoint.hostname,
port: Number(proxyEndpoint.port),
auth: proxyEndpoint.auth,
rejectUnauthorized: (typeof options.strictSSL === 'boolean') ? options.strictSSL : true,
protocol: proxyEndpoint.protocol
};
const opts = {
host: proxyEndpoint.hostname,
port: Number(proxyEndpoint.port),
auth: proxyEndpoint.auth,
rejectUnauthorized: (typeof options.strictSSL === 'boolean') ? options.strictSSL : true,
protocol: proxyEndpoint.protocol
};
return requestURL.protocol === 'http:' ? createHttpProxyAgent(opts) : createHttpsProxyAgent(opts);
return requestURL.protocol === 'http:' ? createHttpProxyAgent(opts) : createHttpsProxyAgent(opts);
}