ftp works but it blocks the thread

This commit is contained in:
ading2210 2024-03-17 17:50:58 -04:00
parent 0a5ace96fb
commit 2a072ecee0
12 changed files with 165 additions and 184 deletions

View file

@ -41,82 +41,6 @@ function check_loaded(check_websocket) {
throw new Error("websocket proxy url not set, please call libcurl.set_websocket");
}
}
//wrap perform_request in a promise
function perform_request_async(url, params, body) {
return new Promise((resolve, reject) => {
let stream_controller;
let http_handle;
let response_obj;
let aborted = false;
//handle abort signals
if (params.signal instanceof AbortSignal) {
params.signal.addEventListener("abort", () => {
if (aborted) return;
aborted = true;
_cleanup_handle(http_handle);
if (!response_obj) {
reject(new DOMException("The operation was aborted."));
}
else {
stream_controller.error("The operation was aborted.");
}
});
}
let stream = new ReadableStream({
start(controller) {
stream_controller = controller;
}
});
function data_callback(new_data) {
try {
stream_controller.enqueue(new_data);
}
catch (e) {
//the readable stream has been closed elsewhere, so cancel the request
if (e instanceof TypeError) {
_cleanup_handle(http_handle);
}
else {
throw e;
}
}
}
function headers_callback() {
let response_json = c_func_str(_http_get_info, [http_handle]);
response_obj = create_response(stream, JSON.parse(response_json));
resolve(response_obj);
}
function finish_callback(error) {
if (error != 0) {
error_msg(`Request "${url}" failed with error code ${error}: ${get_error_str(error)}`);
reject(`Request failed with error code ${error}: ${get_error_str(error)}`);
return;
}
try {
stream_controller.close();
} //this will only fail if the stream is already errored or closed, which isn't a problem
catch {}
}
let body_length = body ? body.length : 0;
let params_json = JSON.stringify(params);
http_handle = create_request(url, data_callback, finish_callback, headers_callback);
c_func(_http_set_options, [http_handle, params_json, body, body_length]);
start_request(http_handle);
});
}
async function libcurl_fetch(url, params={}) {
check_loaded(true);
let body = await create_options(params);
return await perform_request_async(url, params, body);
}
function set_websocket_url(url) {
websocket_url = url;
if (Module.websocket) {
@ -177,7 +101,9 @@ api = {
WebSocket: WebSocket,
CurlWebSocket: CurlWebSocket,
TLSSocket: TLSSocket,
fetch: () => {throw "not ready"},
HTTPSession: HTTPSession,
FTPSession: FTPSession,
fetch() {throw "not ready"},
get copyright() {return copyright_notice},
get version() {return get_version()},