[njs] QuickJS: removed extra copy of body argument in ngx.fetch().
noreply at nginx.com
noreply at nginx.com
Mon Sep 8 23:00:02 UTC 2025
details: https://github.com/nginx/njs/commit/8cfb0b960be469107ce47c4b84be6c7e0f4056e6
branches: master
commit: 8cfb0b960be469107ce47c4b84be6c7e0f4056e6
user: Dmitry Volyntsev <xeioex at nginx.com>
date: Thu, 28 Aug 2025 19:15:01 -0700
description:
QuickJS: removed extra copy of body argument in ngx.fetch().
---
nginx/ngx_qjs_fetch.c | 35 ++++++++++++++++++++++++++++++++---
1 file changed, 32 insertions(+), 3 deletions(-)
diff --git a/nginx/ngx_qjs_fetch.c b/nginx/ngx_qjs_fetch.c
index 7130a0c2..46b54eab 100644
--- a/nginx/ngx_qjs_fetch.c
+++ b/nginx/ngx_qjs_fetch.c
@@ -805,10 +805,12 @@ ngx_qjs_fetch_response_ctor(JSContext *cx, JSValueConst new_target, int argc,
JSValueConst *argv)
{
int ret;
+ size_t byte_offset, byte_length;
u_char *p, *end;
- JSValue init, value, body, proto, obj;
+ JSValue init, value, body, proto, obj, buf;
ngx_str_t bd;
ngx_int_t rc;
+ const char *str;
ngx_pool_t *pool;
ngx_js_ctx_t *ctx;
ngx_js_response_t *response;
@@ -913,12 +915,39 @@ ngx_qjs_fetch_response_ctor(JSContext *cx, JSValueConst new_target, int argc,
body = argv[0];
if (!JS_IsNullOrUndefined(body)) {
- if (ngx_qjs_string(cx, pool, body, &bd) != NGX_OK) {
- return JS_ThrowInternalError(cx, "invalid Response body");
+ str = NULL;
+ if (JS_IsString(body)) {
+ goto string;
+ }
+
+ buf = JS_GetTypedArrayBuffer(cx, body, &byte_offset, &byte_length, NULL);
+ if (!JS_IsException(buf)) {
+ bd.data = JS_GetArrayBuffer(cx, &bd.len, buf);
+
+ JS_FreeValue(cx, buf);
+
+ if (bd.data != NULL) {
+ bd.data += byte_offset;
+ bd.len = byte_length;
+ }
+
+ } else {
+
+string:
+ str = JS_ToCStringLen(cx, &bd.len, body);
+ if (str == NULL) {
+ return JS_EXCEPTION;
+ }
+
+ bd.data = (u_char *) str;
}
njs_chb_append(&response->chain, bd.data, bd.len);
+ if (str != NULL) {
+ JS_FreeCString(cx, str);
+ }
+
if (JS_IsString(body)) {
rc = ngx_qjs_headers_append(cx, &response->headers,
(u_char *) "Content-Type",
More information about the nginx-devel
mailing list