[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[taler-quickjs-tart] 01/04: make os.fetchHttp return cancellation functi
From: |
gnunet |
Subject: |
[taler-quickjs-tart] 01/04: make os.fetchHttp return cancellation function |
Date: |
Mon, 27 May 2024 21:47:42 +0200 |
This is an automated email from the git hooks/post-receive script.
ivan-avalos pushed a commit to branch master
in repository quickjs-tart.
commit f7b6b6bbafd589774e7f439c12e92415db0cae50
Author: Iván Ávalos <avalos@disroot.org>
AuthorDate: Fri May 24 12:06:20 2024 -0600
make os.fetchHttp return cancellation function
---
quickjs/quickjs-libc.c | 41 +++++++++++++++++++++++++++++++++++++----
1 file changed, 37 insertions(+), 4 deletions(-)
diff --git a/quickjs/quickjs-libc.c b/quickjs/quickjs-libc.c
index 0002468..855e16e 100644
--- a/quickjs/quickjs-libc.c
+++ b/quickjs/quickjs-libc.c
@@ -22,6 +22,7 @@
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
+#include "quickjs.h"
#include <stdlib.h>
#include <stdio.h>
#include <stdarg.h>
@@ -2286,6 +2287,21 @@ static void handle_http_resp(void *cls, struct
JSHttpResponseInfo *resp_info)
return;
}
+static JSValue cancel_http_req(JSContext *ctx, JSValueConst this_val, int
argc, JSValueConst *argv)
+{
+ JSRuntime *rt = JS_GetRuntime(ctx);
+ JSThreadState *ts = JS_GetRuntimeOpaque(rt);
+ int req_id;
+ int ret;
+
+ JS_ToInt32(ctx, &req_id, argv[0]);
+
+ // cancel HTTP request
+ ret = ts->http_client_impl->req_cancel(ts->http_client_impl->cls, req_id);
+
+ return JS_NewInt32(ctx, ret);
+}
+
static void
free_http_headers(JSContext *ctx, char **headers)
{
@@ -2345,7 +2361,11 @@ exception:
/**
- * fetchHttp(url, { method, headers, body }): Promise<Response>
+ * fetchHttp(url, { method, headers, body }): {
+ * requestId: number,
+ * response: Promise<Response>,
+ * cancelFn: () => void,
+ * }
*/
static JSValue js_os_fetchHttp(JSContext *ctx, JSValueConst this_val,
int argc, JSValueConst *argv)
@@ -2470,13 +2490,26 @@ static JSValue js_os_fetchHttp(JSContext *ctx,
JSValueConst this_val,
list_add_tail(&req_context->link, &ts->http_requests);
- ret_val = JS_NewPromiseCapability(ctx, resolving_funs);
- if (JS_IsException(ret_val)) {
- goto done;
+ // requestId: number
+ JSValue requestId = JS_NewInt32(ctx, ret);
+
+ // promise: Promise<Response>
+ JSValue promise = JS_NewPromiseCapability(ctx, resolving_funs);
+ if (JS_IsException(promise)) {
+ goto exception;
}
req_context->request_id = ret;
req_context->resolve_func = resolving_funs[0];
req_context->reject_func = resolving_funs[1];
+
+ // cancelFn: () => void
+ JSValue cancelFn = JS_NewCFunction(ctx, &cancel_http_req, "cancelFn", 1);
+
+ ret_val = JS_NewObject(ctx);
+ JS_SetPropertyStr(ctx, ret_val, "requestId", requestId);
+ JS_SetPropertyStr(ctx, ret_val, "promise", promise);
+ JS_SetPropertyStr(ctx, ret_val, "cancelFn", cancelFn);
+
done:
free_http_headers(ctx, req.request_headers);
JS_FreeValue(ctx, method);
--
To stop receiving notification emails like this one, please contact
gnunet@gnunet.org.