Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add cancellation support #15

Merged
merged 6 commits into from
Nov 3, 2022
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion api.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ export interface XHROptions {
data?: string;
strictSSL?: boolean;
followRedirects?: number;
token?: import("vscode-jsonrpc").CancellationToken;
agent?: HttpProxyAgent | HttpsProxyAgent;
}

Expand Down Expand Up @@ -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;
8 changes: 7 additions & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "request-light",
"version": "0.5.8",
"version": "0.6.0",
"description": "Lightweight request library. Promise based, with proxy support.",
"main": "./lib/node/main.js",
"browser": {
Expand All @@ -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",
Tarrowren marked this conversation as resolved.
Show resolved Hide resolved
"typescript": "^4.5.4",
"@types/node": "13.7.6",
"rimraf": "^3.0.2",
Expand Down
87 changes: 49 additions & 38 deletions src/browser/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Tarrowren marked this conversation as resolved.
Show resolved Hide resolved
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);
}
Loading