Skip to content

Fetch: QuickJS support. #891

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

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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: 3 additions & 0 deletions nginx/config
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,11 @@ NJS_ZLIB=${NJS_ZLIB:-YES}
NJS_QUICKJS=${NJS_QUICKJS:-YES}

NJS_DEPS="$ngx_addon_dir/ngx_js.h \
$ngx_addon_dir/ngx_js_http.h \
$ngx_addon_dir/ngx_js_fetch.h \
$ngx_addon_dir/ngx_js_shared_dict.h"
NJS_SRCS="$ngx_addon_dir/ngx_js.c \
$ngx_addon_dir/ngx_js_http.c \
$ngx_addon_dir/ngx_js_fetch.c \
$ngx_addon_dir/ngx_js_regex.c \
$ngx_addon_dir/ngx_js_shared_dict.c"
Expand Down Expand Up @@ -154,6 +156,7 @@ NJS_ENGINE_LIB="$ngx_addon_dir/../build/libnjs.a"
if [ "$NJS_HAVE_QUICKJS" = "YES" ]; then
NJS_ENGINE_DEP="$ngx_addon_dir/../build/libqjs.a"
NJS_ENGINE_LIB="$ngx_addon_dir/../build/libnjs.a $ngx_addon_dir/../build/libqjs.a"
QJS_SRCS="$QJS_SRCS $ngx_addon_dir/ngx_qjs_fetch.c"
fi

if [ $HTTP != NO ]; then
Expand Down
3 changes: 3 additions & 0 deletions nginx/ngx_http_js_module.c
Original file line number Diff line number Diff line change
Expand Up @@ -1133,6 +1133,9 @@ static JSClassDef ngx_http_qjs_headers_out_class = {
qjs_module_t *njs_http_qjs_addon_modules[] = {
&ngx_qjs_ngx_module,
&ngx_qjs_ngx_shared_dict_module,
#ifdef NJS_HAVE_QUICKJS
&ngx_qjs_ngx_fetch_module,
#endif
/*
* Shared addons should be in the same order and the same positions
* in all nginx modules.
Expand Down
42 changes: 42 additions & 0 deletions nginx/ngx_js.c
Original file line number Diff line number Diff line change
Expand Up @@ -436,6 +436,7 @@ static const JSCFunctionListEntry ngx_qjs_ext_ngx[] = {
JS_CGETSET_MAGIC_DEF("ERR", ngx_qjs_ext_constant_integer, NULL,
NGX_LOG_ERR),
JS_CGETSET_DEF("error_log_path", ngx_qjs_ext_error_log_path, NULL),
JS_CFUNC_DEF("fetch", 2, ngx_qjs_ext_fetch),
JS_CGETSET_MAGIC_DEF("INFO", ngx_qjs_ext_constant_integer, NULL,
NGX_LOG_INFO),
JS_CFUNC_MAGIC_DEF("log", 1, ngx_qjs_ext_log, 0),
Expand Down Expand Up @@ -1502,6 +1503,31 @@ ngx_qjs_string(JSContext *cx, JSValueConst val, ngx_str_t *dst)
}


int
ngx_qjs_array_length(JSContext *cx, uint32_t *plen, JSValueConst arr)
{
int ret;
JSValue value;
uint32_t len;

value = JS_GetPropertyStr(cx, arr, "length");
if (JS_IsException(value)) {
return -1;
}

ret = JS_ToUint32(cx, &len, value);
JS_FreeValue(cx, value);

if (ret) {
return -1;
}

*plen = len;

return 0;
}


static void
ngx_qjs_timer_handler(ngx_event_t *ev)
{
Expand Down Expand Up @@ -4487,3 +4513,19 @@ ngx_js_queue_pop(ngx_js_queue_t *queue)

return item;
}


ngx_int_t
ngx_njs_string(njs_vm_t *vm, njs_value_t *value, ngx_str_t *str)
{
njs_str_t s;

if (ngx_js_string(vm, value, &s) != NGX_OK) {
return NGX_ERROR;
}

str->data = s.start;
str->len = s.length;

return NGX_OK;
}
10 changes: 10 additions & 0 deletions nginx/ngx_js.h
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,9 @@
#define NGX_QJS_CLASS_ID_SHARED (NGX_QJS_CLASS_ID_OFFSET + 11)
#define NGX_QJS_CLASS_ID_SHARED_DICT (NGX_QJS_CLASS_ID_OFFSET + 12)
#define NGX_QJS_CLASS_ID_SHARED_DICT_ERROR (NGX_QJS_CLASS_ID_OFFSET + 13)
#define NGX_QJS_CLASS_ID_FETCH_HEADERS (NGX_QJS_CLASS_ID_OFFSET + 14)
#define NGX_QJS_CLASS_ID_FETCH_REQUEST (NGX_QJS_CLASS_ID_OFFSET + 15)
#define NGX_QJS_CLASS_ID_FETCH_RESPONSE (NGX_QJS_CLASS_ID_OFFSET + 16)


typedef struct ngx_js_loc_conf_s ngx_js_loc_conf_t;
Expand Down Expand Up @@ -345,6 +348,10 @@ ngx_int_t ngx_qjs_call(JSContext *cx, JSValue function, JSValue *argv,
ngx_int_t ngx_qjs_exception(ngx_engine_t *e, ngx_str_t *s);
ngx_int_t ngx_qjs_integer(JSContext *cx, JSValueConst val, ngx_int_t *n);
ngx_int_t ngx_qjs_string(JSContext *cx, JSValueConst val, ngx_str_t *str);
int ngx_qjs_array_length(JSContext *cx, uint32_t *plen, JSValueConst arr);

JSValue ngx_qjs_ext_fetch(JSContext *cx, JSValueConst this_val, int argc,
JSValueConst *argv);

#define ngx_qjs_prop(cx, type, start, len) \
((type == NGX_JS_STRING) ? qjs_string_create(cx, start, len) \
Expand Down Expand Up @@ -382,6 +389,7 @@ extern qjs_module_t qjs_xml_module;
extern qjs_module_t qjs_zlib_module;
extern qjs_module_t ngx_qjs_ngx_module;
extern qjs_module_t ngx_qjs_ngx_shared_dict_module;
extern qjs_module_t ngx_qjs_ngx_fetch_module;

#endif

Expand Down Expand Up @@ -422,6 +430,8 @@ ngx_js_queue_t *ngx_js_queue_create(ngx_pool_t *pool, ngx_uint_t capacity);
ngx_int_t ngx_js_queue_push(ngx_js_queue_t *queue, void *item);
void *ngx_js_queue_pop(ngx_js_queue_t *queue);

ngx_int_t ngx_njs_string(njs_vm_t *vm, njs_value_t *value, ngx_str_t *str);


extern njs_module_t ngx_js_ngx_module;
extern njs_module_t njs_webcrypto_module;
Expand Down
Loading
Loading