[njs] Modules: added js_path directive.

Dmitry Volyntsev xeioex at nginx.com
Tue Mar 26 12:53:47 UTC 2019


details:   https://hg.nginx.org/njs/rev/2e3ab9dc1328
branches:  
changeset: 845:2e3ab9dc1328
user:      Dmitry Volyntsev <xeioex at nginx.com>
date:      Tue Mar 26 15:52:53 2019 +0300
description:
Modules: added js_path directive.

diffstat:

 nginx/ngx_http_js_module.c   |  43 ++++++++++++++++++++++++++++++++++++++++-
 nginx/ngx_stream_js_module.c |  45 +++++++++++++++++++++++++++++++++++++++++--
 2 files changed, 83 insertions(+), 5 deletions(-)

diffs (173 lines):

diff -r 4c7862cf1ba1 -r 2e3ab9dc1328 nginx/ngx_http_js_module.c
--- a/nginx/ngx_http_js_module.c	Tue Mar 26 15:35:45 2019 +0300
+++ b/nginx/ngx_http_js_module.c	Tue Mar 26 15:52:53 2019 +0300
@@ -14,6 +14,7 @@
 
 typedef struct {
     njs_vm_t            *vm;
+    ngx_array_t         *paths;
     const njs_extern_t  *req_proto;
 } ngx_http_js_main_conf_t;
 
@@ -161,6 +162,13 @@ static ngx_command_t  ngx_http_js_comman
       0,
       NULL },
 
+    { ngx_string("js_path"),
+      NGX_HTTP_MAIN_CONF|NGX_CONF_TAKE1,
+      ngx_conf_set_str_array_slot,
+      NGX_HTTP_MAIN_CONF_OFFSET,
+      offsetof(ngx_http_js_main_conf_t, paths),
+      NULL },
+
     { ngx_string("js_set"),
       NGX_HTTP_MAIN_CONF|NGX_CONF_TAKE2,
       ngx_http_js_set,
@@ -2170,9 +2178,10 @@ ngx_http_js_include(ngx_conf_t *cf, ngx_
     u_char                *start, *end;
     ssize_t                n;
     ngx_fd_t               fd;
-    ngx_str_t             *value, file;
+    ngx_str_t             *m, *value, file;
     nxt_int_t              rc;
-    nxt_str_t              text;
+    nxt_str_t              text, path;
+    ngx_uint_t             i;
     njs_vm_opt_t           options;
     ngx_file_info_t        fi;
     ngx_pool_cleanup_t    *cln;
@@ -2259,6 +2268,34 @@ ngx_http_js_include(ngx_conf_t *cf, ngx_
     cln->handler = ngx_http_js_cleanup_vm;
     cln->data = jmcf->vm;
 
+    path.start = ngx_cycle->prefix.data;
+    path.length = ngx_cycle->prefix.len;
+
+    rc = njs_vm_add_path(jmcf->vm, &path);
+    if (rc != NXT_OK) {
+        ngx_conf_log_error(NGX_LOG_EMERG, cf, 0, "failed to add path");
+        return NGX_CONF_ERROR;
+    }
+
+    if (jmcf->paths != NGX_CONF_UNSET_PTR) {
+        m = jmcf->paths->elts;
+
+        for (i = 0; i < jmcf->paths->nelts; i++) {
+            if (ngx_conf_full_name(cf->cycle, &m[i], 0) != NGX_OK) {
+                return NGX_CONF_ERROR;
+            }
+
+            path.start = m[i].data;
+            path.length = m[i].len;
+
+            rc = njs_vm_add_path(jmcf->vm, &path);
+            if (rc != NXT_OK) {
+                ngx_conf_log_error(NGX_LOG_EMERG, cf, 0, "failed to add path");
+                return NGX_CONF_ERROR;
+            }
+        }
+    }
+
     jmcf->req_proto = njs_vm_external_prototype(jmcf->vm,
                                                 &ngx_http_js_externals[0]);
     if (jmcf->req_proto == NULL) {
@@ -2363,6 +2400,8 @@ ngx_http_js_create_main_conf(ngx_conf_t 
      *     conf->req_proto = NULL;
      */
 
+    conf->paths = NGX_CONF_UNSET_PTR;
+
     return conf;
 }
 
diff -r 4c7862cf1ba1 -r 2e3ab9dc1328 nginx/ngx_stream_js_module.c
--- a/nginx/ngx_stream_js_module.c	Tue Mar 26 15:35:45 2019 +0300
+++ b/nginx/ngx_stream_js_module.c	Tue Mar 26 15:52:53 2019 +0300
@@ -15,6 +15,7 @@
 
 typedef struct {
     njs_vm_t              *vm;
+    ngx_array_t           *paths;
     const njs_extern_t    *proto;
 } ngx_stream_js_main_conf_t;
 
@@ -132,6 +133,13 @@ static ngx_command_t  ngx_stream_js_comm
       0,
       NULL },
 
+    { ngx_string("js_path"),
+      NGX_STREAM_MAIN_CONF|NGX_CONF_TAKE1,
+      ngx_conf_set_str_array_slot,
+      NGX_STREAM_MAIN_CONF_OFFSET,
+      offsetof(ngx_stream_js_main_conf_t, paths),
+      NULL },
+
     { ngx_string("js_set"),
       NGX_STREAM_MAIN_CONF|NGX_CONF_TAKE2,
       ngx_stream_js_set,
@@ -1394,9 +1402,10 @@ ngx_stream_js_include(ngx_conf_t *cf, ng
     u_char                *start, *end;
     ssize_t                n;
     ngx_fd_t               fd;
-    ngx_str_t             *value, file;
+    ngx_str_t             *m, *value, file;
     nxt_int_t              rc;
-    nxt_str_t              text;
+    nxt_str_t              text, path;
+    ngx_uint_t             i;
     njs_vm_opt_t           options;
     ngx_file_info_t        fi;
     ngx_pool_cleanup_t    *cln;
@@ -1483,6 +1492,34 @@ ngx_stream_js_include(ngx_conf_t *cf, ng
     cln->handler = ngx_stream_js_cleanup_vm;
     cln->data = jmcf->vm;
 
+    path.start = ngx_cycle->prefix.data;
+    path.length = ngx_cycle->prefix.len;
+
+    rc = njs_vm_add_path(jmcf->vm, &path);
+    if (rc != NXT_OK) {
+        ngx_conf_log_error(NGX_LOG_EMERG, cf, 0, "failed to add path");
+        return NGX_CONF_ERROR;
+    }
+
+    if (jmcf->paths != NGX_CONF_UNSET_PTR) {
+        m = jmcf->paths->elts;
+
+        for (i = 0; i < jmcf->paths->nelts; i++) {
+            if (ngx_conf_full_name(cf->cycle, &m[i], 0) != NGX_OK) {
+                return NGX_CONF_ERROR;
+            }
+
+            path.start = m[i].data;
+            path.length = m[i].len;
+
+            rc = njs_vm_add_path(jmcf->vm, &path);
+            if (rc != NXT_OK) {
+                ngx_conf_log_error(NGX_LOG_EMERG, cf, 0, "failed to add path");
+                return NGX_CONF_ERROR;
+            }
+        }
+    }
+
     jmcf->proto = njs_vm_external_prototype(jmcf->vm,
                                             &ngx_stream_js_externals[0]);
 
@@ -1552,7 +1589,7 @@ ngx_stream_js_set(ngx_conf_t *cf, ngx_co
 static void *
 ngx_stream_js_create_main_conf(ngx_conf_t *cf)
 {
-    ngx_stream_js_srv_conf_t  *conf;
+    ngx_stream_js_main_conf_t  *conf;
 
     conf = ngx_pcalloc(cf->pool, sizeof(ngx_stream_js_main_conf_t));
     if (conf == NULL) {
@@ -1566,6 +1603,8 @@ ngx_stream_js_create_main_conf(ngx_conf_
      *     conf->proto = NULL;
      */
 
+    conf->paths = NGX_CONF_UNSET_PTR;
+
     return conf;
 }
 


More information about the nginx-devel mailing list