diff -ruN minnet-quickjs/example.mjs minnet-quickjs-nocurl/example.mjs --- minnet-quickjs/example.mjs 2024-07-06 20:35:10.873282435 -0300 +++ minnet-quickjs-nocurl/example.mjs 2024-07-06 20:36:24.126615293 -0300 @@ -1,4 +1,4 @@ -import { client, server, fetch } from "minnet.so" +import { client, server } from "minnet.so" function CreateServer() { print("SERVER") @@ -42,21 +42,7 @@ }); } -function getDownloadCount() { - const res = fetch("https://api.github.com/repos/khanhas/spicetify-cli/releases") - const dl_count = res.json().reduce((total, tag) => { - return total += tag.assets.reduce((tag_total, asset) => { - return tag_total += asset.download_count - }, 0) - }, 0) - print("Fetch:", res.url) - print("STATUS: ", res.status, "OK: ", res.ok, "TYPE: ", res.type) - print("RESULT: ", dl_count) - return `${dl_count}` -} - switch (scriptArgs[1]) { case "s": CreateServer(); break; case "c": CreateClient(); break; - case "f": getDownloadCount(); break; -} \ No newline at end of file +} diff -ruN minnet-quickjs/Makefile minnet-quickjs-nocurl/Makefile --- minnet-quickjs/Makefile 2024-07-06 20:35:10.873282435 -0300 +++ minnet-quickjs-nocurl/Makefile 2024-07-06 20:48:55.783277078 -0300 @@ -1,6 +1,7 @@ -CC=clang -FLAGS=-Wall -fPIC -shared -std=gnu17 -LIB=-lwebsockets -lcurl -L/usr/lib/quickjs/ -lquickjs +CC=gcc +FLAGS=-Wall -fPIC -shared -std=gnu17 -mtune=generic -Os -pipe +LDFLAGS="-Wl,-O1" +LIB=-lwebsockets -L/usr/local/lib/quickjs/ -lquickjs DEFINE=-DJS_SHARED_LIBRARY all: - ${CC} ${FLAGS} ${DEFINE} ${LIB} minnet.c -o minnet.so \ No newline at end of file + ${CC} ${FLAGS} ${DEFINE} ${LIB} minnet.c -o minnet.so $(LDFLAGS) diff -ruN minnet-quickjs/minnet.c minnet-quickjs-nocurl/minnet.c --- minnet-quickjs/minnet.c 2024-07-06 20:35:10.873282435 -0300 +++ minnet-quickjs-nocurl/minnet.c 2024-07-06 20:41:52.973279824 -0300 @@ -1,5 +1,4 @@ #include "minnet.h" -#include #include #ifdef JS_SHARED_LIBRARY @@ -422,89 +421,6 @@ js_free_rt(rt, ws_obj); } -static JSValue minnet_fetch(JSContext *ctx, JSValueConst this_val, int argc, - JSValueConst *argv) -{ - CURL *curl; - CURLcode curlRes; - const char *url; - FILE *fi; - MinnetResponse *res; - uint8_t *buffer; - long bufSize; - long status; - char *type; - - JSValue resObj = JS_NewObjectClass(ctx, minnet_response_class_id); - if (JS_IsException(resObj)) - return JS_EXCEPTION; - - res = js_mallocz(ctx, sizeof(*res)); - - if (!res) { - JS_FreeValue(ctx, resObj); - return JS_EXCEPTION; - } - - if (!JS_IsString(argv[0])) - return JS_EXCEPTION; - - res->url = argv[0]; - url = JS_ToCString(ctx, argv[0]); - - curl = curl_easy_init(); - if (!curl) - return JS_EXCEPTION; - - fi = tmpfile(); - - curl_easy_setopt(curl, CURLOPT_URL, url); - curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1L); - curl_easy_setopt(curl, CURLOPT_USERAGENT, "minimal-network-quickjs"); - curl_easy_setopt(curl, CURLOPT_WRITEDATA, fi); - - curlRes = curl_easy_perform(curl); - if (curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &status) == CURLE_OK) - res->status = JS_NewInt32(ctx, (int32_t)status); - - if (curl_easy_getinfo(curl, CURLINFO_CONTENT_TYPE, &type) == CURLE_OK) - res->type = JS_NewString(ctx, type); - - res->ok = JS_FALSE; - - if (curlRes != CURLE_OK) { - fprintf(stderr, "CURL failed: %s\n", curl_easy_strerror(curlRes)); - goto finish; - } - - bufSize = ftell(fi); - rewind(fi); - - buffer = calloc(1, bufSize + 1); - if (!buffer) { - fclose(fi), fputs("memory alloc fails", stderr); - goto finish; - } - - /* copy the file into the buffer */ - if (1 != fread(buffer, bufSize, 1, fi)) { - fclose(fi), free(buffer), fputs("entire read fails", stderr); - goto finish; - } - - fclose(fi); - - res->ok = JS_TRUE; - res->buffer = buffer; - res->size = bufSize; - -finish: - curl_easy_cleanup(curl); - JS_SetOpaque(resObj, res); - - return resObj; -} - static JSValue minnet_response_buffer(JSContext *ctx, JSValueConst this_val, int argc, JSValueConst *argv) { @@ -584,4 +500,4 @@ free(res->buffer); js_free_rt(rt, res); } -} \ No newline at end of file +} diff -ruN minnet-quickjs/minnet.h minnet-quickjs-nocurl/minnet.h --- minnet-quickjs/minnet.h 2024-07-06 20:35:10.873282435 -0300 +++ minnet-quickjs-nocurl/minnet.h 2024-07-06 20:38:13.126614584 -0300 @@ -24,13 +24,9 @@ static JSValue minnet_ws_client(JSContext *ctx, JSValueConst this_val, int argc, JSValueConst *argv); -static JSValue minnet_fetch(JSContext *ctx, JSValueConst this_val, int argc, - JSValueConst *argv); - static const JSCFunctionListEntry minnet_funcs[] = { JS_CFUNC_DEF("server", 1, minnet_ws_server), JS_CFUNC_DEF("client", 1, minnet_ws_client), - JS_CFUNC_DEF("fetch", 1, minnet_fetch), }; static int js_minnet_init(JSContext *ctx, JSModuleDef *m) diff -ruN minnet-quickjs/README.md minnet-quickjs-nocurl/README.md --- minnet-quickjs/README.md 2024-07-06 20:35:10.873282435 -0300 +++ minnet-quickjs-nocurl/README.md 2024-07-06 20:40:03.389947202 -0300 @@ -3,13 +3,11 @@ Currently, it can: - Creating WebSocket `server` - Creating WebSocket `client` -- `fetch` ## Usage Requirements: - `clang` - `libwebsockets` -- `libcurl` To use `minnet-quickjs` in your QuickJS project, run following commands: ```bash @@ -96,22 +94,4 @@ - `.ping(data)`: `data` must be ArrayBuffer - `.pong(data)`: `data` must be ArrayBuffer -### `fetch(url)`: Get resources from `url` -`url`: a string to download resources from. -Returns `MinnetResponse` object that you can use these -Methods: -- `.text()`: Get body text as string -- `.json()`: Get body text, parse as JSON and returns parsed object. -- `.arrayBuffer()`: Get body as an `ArrayBuffer` - -Properties: -- `.ok`: *boolean*, *Read-only* - Whether the response was successful -- `.url`: *string*, *Read-only* - URL of the response -- `.status`: *number*, *Read-only* - Status code of the response -- `.type`: *string*, *Read-only* - Type of the response - Check out [example.mjs](./example.mjs)