Skip to content

Commit ed592f8

Browse files
committed
Fetch: QuickJS support.
Based on ngx_js_http.c.
1 parent f9f8057 commit ed592f8

14 files changed

+2613
-147
lines changed

Diff for: nginx/config

+1
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,7 @@ NJS_ENGINE_LIB="$ngx_addon_dir/../build/libnjs.a"
156156
if [ "$NJS_HAVE_QUICKJS" = "YES" ]; then
157157
NJS_ENGINE_DEP="$ngx_addon_dir/../build/libqjs.a"
158158
NJS_ENGINE_LIB="$ngx_addon_dir/../build/libnjs.a $ngx_addon_dir/../build/libqjs.a"
159+
QJS_SRCS="$QJS_SRCS $ngx_addon_dir/ngx_qjs_fetch.c"
159160
fi
160161

161162
if [ $HTTP != NO ]; then

Diff for: nginx/ngx_http_js_module.c

+3
Original file line numberDiff line numberDiff line change
@@ -1133,6 +1133,9 @@ static JSClassDef ngx_http_qjs_headers_out_class = {
11331133
qjs_module_t *njs_http_qjs_addon_modules[] = {
11341134
&ngx_qjs_ngx_module,
11351135
&ngx_qjs_ngx_shared_dict_module,
1136+
#ifdef NJS_HAVE_QUICKJS
1137+
&ngx_qjs_ngx_fetch_module,
1138+
#endif
11361139
/*
11371140
* Shared addons should be in the same order and the same positions
11381141
* in all nginx modules.

Diff for: nginx/ngx_js.c

+26
Original file line numberDiff line numberDiff line change
@@ -436,6 +436,7 @@ static const JSCFunctionListEntry ngx_qjs_ext_ngx[] = {
436436
JS_CGETSET_MAGIC_DEF("ERR", ngx_qjs_ext_constant_integer, NULL,
437437
NGX_LOG_ERR),
438438
JS_CGETSET_DEF("error_log_path", ngx_qjs_ext_error_log_path, NULL),
439+
JS_CFUNC_DEF("fetch", 2, ngx_qjs_ext_fetch),
439440
JS_CGETSET_MAGIC_DEF("INFO", ngx_qjs_ext_constant_integer, NULL,
440441
NGX_LOG_INFO),
441442
JS_CFUNC_MAGIC_DEF("log", 1, ngx_qjs_ext_log, 0),
@@ -1502,6 +1503,31 @@ ngx_qjs_string(JSContext *cx, JSValueConst val, ngx_str_t *dst)
15021503
}
15031504

15041505

1506+
int
1507+
ngx_qjs_array_length(JSContext *cx, uint32_t *plen, JSValueConst arr)
1508+
{
1509+
int ret;
1510+
JSValue value;
1511+
uint32_t len;
1512+
1513+
value = JS_GetPropertyStr(cx, arr, "length");
1514+
if (JS_IsException(value)) {
1515+
return -1;
1516+
}
1517+
1518+
ret = JS_ToUint32(cx, &len, value);
1519+
JS_FreeValue(cx, value);
1520+
1521+
if (ret) {
1522+
return -1;
1523+
}
1524+
1525+
*plen = len;
1526+
1527+
return 0;
1528+
}
1529+
1530+
15051531
static void
15061532
ngx_qjs_timer_handler(ngx_event_t *ev)
15071533
{

Diff for: nginx/ngx_js.h

+8
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,9 @@
6363
#define NGX_QJS_CLASS_ID_SHARED (NGX_QJS_CLASS_ID_OFFSET + 11)
6464
#define NGX_QJS_CLASS_ID_SHARED_DICT (NGX_QJS_CLASS_ID_OFFSET + 12)
6565
#define NGX_QJS_CLASS_ID_SHARED_DICT_ERROR (NGX_QJS_CLASS_ID_OFFSET + 13)
66+
#define NGX_QJS_CLASS_ID_FETCH_HEADERS (NGX_QJS_CLASS_ID_OFFSET + 14)
67+
#define NGX_QJS_CLASS_ID_FETCH_REQUEST (NGX_QJS_CLASS_ID_OFFSET + 15)
68+
#define NGX_QJS_CLASS_ID_FETCH_RESPONSE (NGX_QJS_CLASS_ID_OFFSET + 16)
6669

6770

6871
typedef struct ngx_js_loc_conf_s ngx_js_loc_conf_t;
@@ -345,6 +348,10 @@ ngx_int_t ngx_qjs_call(JSContext *cx, JSValue function, JSValue *argv,
345348
ngx_int_t ngx_qjs_exception(ngx_engine_t *e, ngx_str_t *s);
346349
ngx_int_t ngx_qjs_integer(JSContext *cx, JSValueConst val, ngx_int_t *n);
347350
ngx_int_t ngx_qjs_string(JSContext *cx, JSValueConst val, ngx_str_t *str);
351+
int ngx_qjs_array_length(JSContext *cx, uint32_t *plen, JSValueConst arr);
352+
353+
JSValue ngx_qjs_ext_fetch(JSContext *cx, JSValueConst this_val, int argc,
354+
JSValueConst *argv);
348355

349356
#define ngx_qjs_prop(cx, type, start, len) \
350357
((type == NGX_JS_STRING) ? qjs_string_create(cx, start, len) \
@@ -382,6 +389,7 @@ extern qjs_module_t qjs_xml_module;
382389
extern qjs_module_t qjs_zlib_module;
383390
extern qjs_module_t ngx_qjs_ngx_module;
384391
extern qjs_module_t ngx_qjs_ngx_shared_dict_module;
392+
extern qjs_module_t ngx_qjs_ngx_fetch_module;
385393

386394
#endif
387395

0 commit comments

Comments
 (0)