]> git.kaiwu.me - njs.git/commitdiff
Modules: fixed Fetch Response prototype reinitialization.
authorDmitry Volyntsev <xeioex@nginx.com>
Wed, 16 Nov 2022 02:38:49 +0000 (18:38 -0800)
committerDmitry Volyntsev <xeioex@nginx.com>
Wed, 16 Nov 2022 02:38:49 +0000 (18:38 -0800)
Previously, since 446a1cb64a6a (0.7.7), when at least one js_import directive
was declared in both HTTP and Stream, ngx.fetch() returned inapproriate
response in Stream.

The prototype for Response object was created two times for HTTP and STREAM,
but the second initialization of global variable with the index of the
Response() prototype overwrites the first value with a different value.  This
caused a problem in Stream code which manifested itself as a `Stream flags`
object returned as a result of ngx.fetch() call instead of a Response instance.

The fix is to ensure that shared prototypes like a Response prototype
have indentical index value in all the modules.

This fixes #596 issue on Github.

nginx/ngx_js.c

index 831593da19ff1421828b3f340658a266d260bb71..7a8164983ff753b75c904f89ad03eb04d29bbff0 100644 (file)
@@ -953,16 +953,21 @@ ngx_js_init_conf_vm(ngx_conf_t *cf, ngx_js_conf_t *conf,
         }
     }
 
-    rc = externals_init(cf, conf);
-    if (rc != NGX_OK) {
-        return NGX_ERROR;
-    }
+    /*
+     * Core prototypes must be inited before externals_init() because
+     * the core prototype ids have to be identical in all the modules.
+     */
 
     rc = ngx_js_core_init(conf->vm, cf->log);
     if (njs_slow_path(rc != NJS_OK)) {
         return NGX_ERROR;
     }
 
+    rc = externals_init(cf, conf);
+    if (rc != NGX_OK) {
+        return NGX_ERROR;
+    }
+
     end = start + size;
 
     rc = njs_vm_compile(conf->vm, &start, end);