Skip to content

Commit ad25c67

Browse files
committed
qjs fetch.
1 parent 8708663 commit ad25c67

File tree

7 files changed

+3609
-6
lines changed

7 files changed

+3609
-6
lines changed

nginx/config

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ NJS_SRCS="$ngx_addon_dir/ngx_js.c \
1414
$ngx_addon_dir/ngx_js_shared_dict.c"
1515

1616
QJS_DEPS=""
17-
QJS_SRCS=""
17+
QJS_SRCS="$ngx_addon_dir/ngx_qjs_fetch.c"
1818

1919
NJS_OPENSSL_LIB=
2020
NJS_XSLT_LIB=

nginx/ngx_http_js_module.c

+1
Original file line numberDiff line numberDiff line change
@@ -1133,6 +1133,7 @@ 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+
&ngx_qjs_ngx_fetch_module,
11361137
/*
11371138
* Shared addons should be in the same order and the same positions
11381139
* in all nginx modules.

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),
@@ -2184,6 +2185,31 @@ ngx_qjs_core_init(JSContext *cx, const char *name)
21842185
return m;
21852186
}
21862187

2188+
2189+
int
2190+
ngx_qjs_array_length(JSContext *cx, uint32_t *plen, JSValueConst arr)
2191+
{
2192+
int ret;
2193+
JSValue value;
2194+
uint32_t len;
2195+
2196+
value = JS_GetPropertyStr(cx, arr, "length");
2197+
if (JS_IsException(value)) {
2198+
return -1;
2199+
}
2200+
2201+
ret = JS_ToUint32(cx, &len, value);
2202+
JS_FreeValue(cx, value);
2203+
2204+
if (ret) {
2205+
return -1;
2206+
}
2207+
2208+
*plen = len;
2209+
2210+
return 0;
2211+
}
2212+
21872213
#endif
21882214

21892215

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(JSContext *cx, 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) \
@@ -381,6 +388,7 @@ extern qjs_module_t qjs_webcrypto_module;
381388
extern qjs_module_t qjs_zlib_module;
382389
extern qjs_module_t ngx_qjs_ngx_module;
383390
extern qjs_module_t ngx_qjs_ngx_shared_dict_module;
391+
extern qjs_module_t ngx_qjs_ngx_fetch_module;
384392

385393
#endif
386394

0 commit comments

Comments
 (0)