Welcome! Log In Create A New Profile

Advanced

[njs] Modules: common code for js_import is moved to shared library.

Vadim Zhestikov via nginx-devel
September 14, 2022 12:24AM
details: https://hg.nginx.org/njs/rev/05efe34376ab
branches:
changeset: 1952:05efe34376ab
user: Vadim Zhestikov <v.zhestikov@f5.com>
date: Tue Sep 13 21:13:17 2022 -0700
description:
Modules: common code for js_import is moved to shared library.

diffstat:

nginx/ngx_http_js_module.c | 160 +++++-------------------------------------
nginx/ngx_js.c | 107 ++++++++++++++++++++++++++++
nginx/ngx_js.h | 18 ++++
nginx/ngx_stream_js_module.c | 153 ++++------------------------------------
4 files changed, 164 insertions(+), 274 deletions(-)

diffs (574 lines):

diff -r 86d181bb72e4 -r 05efe34376ab nginx/ngx_http_js_module.c
--- a/nginx/ngx_http_js_module.c Mon Sep 12 17:56:44 2022 -0700
+++ b/nginx/ngx_http_js_module.c Tue Sep 13 21:13:17 2022 -0700
@@ -12,15 +12,8 @@
#include "ngx_js.h"


-#define NJS_HEADER_SEMICOLON 0x1
-#define NJS_HEADER_SINGLE 0x2
-#define NJS_HEADER_ARRAY 0x4
-
-
typedef struct {
- njs_vm_t *vm;
- ngx_array_t *imports;
- ngx_array_t *paths;
+ NGX_JS_COMMON_CONF;

ngx_str_t content;
ngx_str_t header_filter;
@@ -42,12 +35,9 @@ typedef struct {
} ngx_http_js_loc_conf_t;


-typedef struct {
- ngx_str_t name;
- ngx_str_t path;
- u_char *file;
- ngx_uint_t line;
-} ngx_http_js_import_t;
+#define NJS_HEADER_SEMICOLON 0x1
+#define NJS_HEADER_SINGLE 0x2
+#define NJS_HEADER_ARRAY 0x4


typedef struct {
@@ -263,8 +253,6 @@ static void ngx_http_js_handle_event(ngx
njs_vm_event_t vm_event, njs_value_t *args, njs_uint_t nargs);

static ngx_int_t ngx_http_js_init(ngx_conf_t *cf);
-static char *ngx_http_js_import(ngx_conf_t *cf, ngx_command_t *cmd,
- void *conf);
static char *ngx_http_js_set(ngx_conf_t *cf, ngx_command_t *cmd, void *conf);
static char *ngx_http_js_var(ngx_conf_t *cf, ngx_command_t *cmd, void *conf);
static char *ngx_http_js_content(ngx_conf_t *cf, ngx_command_t *cmd,
@@ -301,7 +289,7 @@ static ngx_command_t ngx_http_js_comman

{ ngx_string("js_import"),
NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE13,
- ngx_http_js_import,
+ ngx_js_import,
NGX_HTTP_LOC_CONF_OFFSET,
0,
NULL },
@@ -4182,10 +4170,10 @@ static ngx_int_t
ngx_http_js_merge_vm(ngx_conf_t *cf, ngx_http_js_loc_conf_t *conf,
ngx_http_js_loc_conf_t *prev)
{
- ngx_str_t *path, *s;
- ngx_uint_t i;
- ngx_array_t *imports, *paths;
- ngx_http_js_import_t *import, *pi;
+ ngx_str_t *path, *s;
+ ngx_uint_t i;
+ ngx_array_t *imports, *paths;
+ ngx_js_named_path_t *import, *pi;

if (prev->imports != NGX_CONF_UNSET_PTR && prev->vm == NULL) {
if (ngx_http_js_init_conf_vm(cf, prev) != NGX_OK) {
@@ -4210,7 +4198,7 @@ ngx_http_js_merge_vm(ngx_conf_t *cf, ngx

} else {
imports = ngx_array_create(cf->pool, 4,
- sizeof(ngx_http_js_import_t));
+ sizeof(ngx_js_named_path_t));
if (imports == NULL) {
return NGX_ERROR;
}
@@ -4288,17 +4276,17 @@ ngx_http_js_merge_vm(ngx_conf_t *cf, ngx
static ngx_int_t
ngx_http_js_init_conf_vm(ngx_conf_t *cf, ngx_http_js_loc_conf_t *conf)
{
- size_t size;
- u_char *start, *end, *p;
- ngx_str_t *m, file;
- njs_int_t rc;
- njs_str_t text, path;
- ngx_uint_t i;
- njs_value_t *value;
- njs_vm_opt_t options;
- ngx_pool_cleanup_t *cln;
- njs_opaque_value_t lvalue, exception;
- ngx_http_js_import_t *import;
+ size_t size;
+ u_char *start, *end, *p;
+ ngx_str_t *m, file;
+ njs_int_t rc;
+ njs_str_t text, path;
+ ngx_uint_t i;
+ njs_value_t *value;
+ njs_vm_opt_t options;
+ ngx_pool_cleanup_t *cln;
+ njs_opaque_value_t lvalue, exception;
+ ngx_js_named_path_t *import;

static const njs_str_t line_number_key = njs_str("lineNumber");
static const njs_str_t file_name_key = njs_str("fileName");
@@ -4466,112 +4454,6 @@ ngx_http_js_init(ngx_conf_t *cf)


static char *
-ngx_http_js_import(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
-{
- ngx_http_js_loc_conf_t *jlcf = conf;
-
- u_char *p, *end, c;
- ngx_int_t from;
- ngx_str_t *value, name, path;
- ngx_http_js_import_t *import;
-
- value = cf->args->elts;
- from = (cf->args->nelts == 4);
-
- if (from) {
- if (ngx_strcmp(value[2].data, "from") != 0) {
- ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
- "invalid parameter \"%V\"", &value[2]);
- return NGX_CONF_ERROR;
- }
- }
-
- name = value[1];
- path = (from ? value[3] : value[1]);
-
- if (!from) {
- end = name.data + name.len;
-
- for (p = end - 1; p >= name.data; p--) {
- if (*p == '/') {
- break;
- }
- }
-
- name.data = p + 1;
- name.len = end - p - 1;
-
- if (name.len < 3
- || ngx_memcmp(&name.data[name.len - 3], ".js", 3) != 0)
- {
- ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
- "cannot extract export name from file path "
- "\"%V\", use extended \"from\" syntax", &path);
- return NGX_CONF_ERROR;
- }
-
- name.len -= 3;
- }
-
- if (name.len == 0) {
- ngx_conf_log_error(NGX_LOG_EMERG, cf, 0, "empty export name");
- return NGX_CONF_ERROR;
- }
-
- p = name.data;
- end = name.data + name.len;
-
- while (p < end) {
- c = ngx_tolower(*p);
-
- if (*p != '_' && (c < 'a' || c > 'z')) {
- if (p == name.data) {
- ngx_conf_log_error(NGX_LOG_EMERG, cf, 0, "cannot start "
- "with \"%c\" in export name \"%V\"", *p,
- &name);
- return NGX_CONF_ERROR;
- }
-
- if (*p < '0' || *p > '9') {
- ngx_conf_log_error(NGX_LOG_EMERG, cf, 0, "invalid character "
- "\"%c\" in export name \"%V\"", *p,
- &name);
- return NGX_CONF_ERROR;
- }
- }
-
- p++;
- }
-
- if (ngx_strchr(path.data, '\'') != NULL) {
- ngx_conf_log_error(NGX_LOG_EMERG, cf, 0, "invalid character \"'\" "
- "in file path \"%V\"", &path);
- return NGX_CONF_ERROR;
- }
-
- if (jlcf->imports == NGX_CONF_UNSET_PTR) {
- jlcf->imports = ngx_array_create(cf->pool, 4,
- sizeof(ngx_http_js_import_t));
- if (jlcf->imports == NULL) {
- return NGX_CONF_ERROR;
- }
- }
-
- import = ngx_array_push(jlcf->imports);
- if (import == NULL) {
- return NGX_CONF_ERROR;
- }
-
- import->name = name;
- import->path = path;
- import->file = cf->conf_file->file.name.data;
- import->line = cf->conf_file->line;
-
- return NGX_CONF_OK;
-}
-
-
-static char *
ngx_http_js_set(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
{
ngx_str_t *value, *fname;
diff -r 86d181bb72e4 -r 05efe34376ab nginx/ngx_js.c
--- a/nginx/ngx_js.c Mon Sep 12 17:56:44 2022 -0700
+++ b/nginx/ngx_js.c Tue Sep 13 21:13:17 2022 -0700
@@ -386,3 +386,110 @@ ngx_js_logger(njs_vm_t *vm, njs_external

c->log->handler = handler;
}
+
+
+char *
+ngx_js_import(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
+{
+ ngx_js_conf_t *jscf = conf;
+
+ u_char *p, *end, c;
+ ngx_int_t from;
+ ngx_str_t *value, name, path;
+ ngx_js_named_path_t *import;
+
+ value = cf->args->elts;
+ from = (cf->args->nelts == 4);
+
+ if (from) {
+ if (ngx_strcmp(value[2].data, "from") != 0) {
+ ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
+ "invalid parameter \"%V\"", &value[2]);
+ return NGX_CONF_ERROR;
+ }
+ }
+
+ name = value[1];
+ path = (from ? value[3] : value[1]);
+
+ if (!from) {
+ end = name.data + name.len;
+
+ for (p = end - 1; p >= name.data; p--) {
+ if (*p == '/') {
+ break;
+ }
+ }
+
+ name.data = p + 1;
+ name.len = end - p - 1;
+
+ if (name.len < 3
+ || ngx_memcmp(&name.data[name.len - 3], ".js", 3) != 0)
+ {
+ ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
+ "cannot extract export name from file path "
+ "\"%V\", use extended \"from\" syntax", &path);
+ return NGX_CONF_ERROR;
+ }
+
+ name.len -= 3;
+ }
+
+ if (name.len == 0) {
+ ngx_conf_log_error(NGX_LOG_EMERG, cf, 0, "empty export name");
+ return NGX_CONF_ERROR;
+ }
+
+ p = name.data;
+ end = name.data + name.len;
+
+ while (p < end) {
+ c = ngx_tolower(*p);
+
+ if (*p != '_' && (c < 'a' || c > 'z')) {
+ if (p == name.data) {
+ ngx_conf_log_error(NGX_LOG_EMERG, cf, 0, "cannot start "
+ "with \"%c\" in export name \"%V\"", *p,
+ &name);
+ return NGX_CONF_ERROR;
+ }
+
+ if (*p < '0' || *p > '9') {
+ ngx_conf_log_error(NGX_LOG_EMERG, cf, 0, "invalid character "
+ "\"%c\" in export name \"%V\"", *p,
+ &name);
+ return NGX_CONF_ERROR;
+ }
+ }
+
+ p++;
+ }
+
+ if (ngx_strchr(path.data, '\'') != NULL) {
+ ngx_conf_log_error(NGX_LOG_EMERG, cf, 0, "invalid character \"'\" "
+ "in file path \"%V\"", &path);
+ return NGX_CONF_ERROR;
+ }
+
+ if (jscf->imports == NGX_CONF_UNSET_PTR) {
+ jscf->imports = ngx_array_create(cf->pool, 4,
+ sizeof(ngx_js_named_path_t));
+ if (jscf->imports == NULL) {
+ return NGX_CONF_ERROR;
+ }
+ }
+
+ import = ngx_array_push(jscf->imports);
+ if (import == NULL) {
+ return NGX_CONF_ERROR;
+ }
+
+ import->name = name;
+ import->path = path;
+ import->file = cf->conf_file->file.name.data;
+ import->line = cf->conf_file->line;
+
+ return NGX_CONF_OK;
+}
+
diff -r 86d181bb72e4 -r 05efe34376ab nginx/ngx_js.h
--- a/nginx/ngx_js.h Mon Sep 12 17:56:44 2022 -0700
+++ b/nginx/ngx_js.h Tue Sep 13 21:13:17 2022 -0700
@@ -38,6 +38,23 @@ typedef ngx_flag_t (*ngx_external_size_p
njs_external_ptr_t e);
typedef ngx_ssl_t *(*ngx_external_ssl_pt)(njs_vm_t *vm, njs_external_ptr_t e);

+typedef struct {
+ ngx_str_t name;
+ ngx_str_t path;
+ u_char *file;
+ ngx_uint_t line;
+} ngx_js_named_path_t;
+
+
+#define NGX_JS_COMMON_CONF \
+ njs_vm_t *vm; \
+ ngx_array_t *imports; \
+ ngx_array_t *paths \
+
+typedef struct {
+ NGX_JS_COMMON_CONF;
+} ngx_js_conf_t;
+

#define ngx_external_connection(vm, e) \
(*((ngx_connection_t **) ((u_char *) (e) + njs_vm_meta(vm, 0))))
@@ -75,6 +92,7 @@ njs_int_t ngx_js_ext_log(njs_vm_t *vm, n
njs_index_t level);
void ngx_js_logger(njs_vm_t *vm, njs_external_ptr_t external,
njs_log_level_t level, const u_char *start, size_t length);
+char * ngx_js_import(ngx_conf_t *cf, ngx_command_t *cmd, void *conf);

njs_int_t ngx_js_ext_string(njs_vm_t *vm, njs_object_prop_t *prop,
njs_value_t *value, njs_value_t *setval, njs_value_t *retval);
diff -r 86d181bb72e4 -r 05efe34376ab nginx/ngx_stream_js_module.c
--- a/nginx/ngx_stream_js_module.c Mon Sep 12 17:56:44 2022 -0700
+++ b/nginx/ngx_stream_js_module.c Tue Sep 13 21:13:17 2022 -0700
@@ -13,17 +13,7 @@


typedef struct {
- ngx_str_t name;
- ngx_str_t path;
- u_char *file;
- ngx_uint_t line;
-} ngx_stream_js_import_t;
-
-
-typedef struct {
- njs_vm_t *vm;
- ngx_array_t *imports;
- ngx_array_t *paths;
+ NGX_JS_COMMON_CONF;

ngx_str_t access;
ngx_str_t preread;
@@ -137,8 +127,6 @@ static size_t ngx_stream_js_max_response
static void ngx_stream_js_handle_event(ngx_stream_session_t *s,
njs_vm_event_t vm_event, njs_value_t *args, njs_uint_t nargs);

-static char *ngx_stream_js_import(ngx_conf_t *cf, ngx_command_t *cmd,
- void *conf);
static char *ngx_stream_js_set(ngx_conf_t *cf, ngx_command_t *cmd,
void *conf);
static char *ngx_stream_js_var(ngx_conf_t *cf, ngx_command_t *cmd,
@@ -176,7 +164,7 @@ static ngx_command_t ngx_stream_js_comm

{ ngx_string("js_import"),
NGX_STREAM_MAIN_CONF|NGX_STREAM_SRV_CONF|NGX_CONF_TAKE13,
- ngx_stream_js_import,
+ ngx_js_import,
NGX_STREAM_SRV_CONF_OFFSET,
0,
NULL },
@@ -1654,10 +1642,10 @@ static ngx_int_t
ngx_stream_js_merge_vm(ngx_conf_t *cf, ngx_stream_js_srv_conf_t *conf,
ngx_stream_js_srv_conf_t *prev)
{
- ngx_str_t *path, *s;
- ngx_uint_t i;
- ngx_array_t *imports, *paths;
- ngx_stream_js_import_t *import, *pi;
+ ngx_str_t *path, *s;
+ ngx_uint_t i;
+ ngx_array_t *imports, *paths;
+ ngx_js_named_path_t *import, *pi;

if (prev->imports != NGX_CONF_UNSET_PTR && prev->vm == NULL) {
if (ngx_stream_js_init_conf_vm(cf, prev) != NGX_OK) {
@@ -1682,7 +1670,7 @@ ngx_stream_js_merge_vm(ngx_conf_t *cf, n

} else {
imports = ngx_array_create(cf->pool, 4,
- sizeof(ngx_stream_js_import_t));
+ sizeof(ngx_js_named_path_t));
if (imports == NULL) {
return NGX_ERROR;
}
@@ -1760,17 +1748,17 @@ ngx_stream_js_merge_vm(ngx_conf_t *cf, n
static ngx_int_t
ngx_stream_js_init_conf_vm(ngx_conf_t *cf, ngx_stream_js_srv_conf_t *conf)
{
- size_t size;
- u_char *start, *end, *p;
- ngx_str_t *m, file;
- njs_int_t rc;
- njs_str_t text, path;
- ngx_uint_t i;
- njs_value_t *value;
- njs_vm_opt_t options;
- ngx_pool_cleanup_t *cln;
- njs_opaque_value_t lvalue, exception;
- ngx_stream_js_import_t *import;
+ size_t size;
+ u_char *start, *end, *p;
+ ngx_str_t *m, file;
+ njs_int_t rc;
+ njs_str_t text, path;
+ ngx_uint_t i;
+ njs_value_t *value;
+ njs_vm_opt_t options;
+ ngx_pool_cleanup_t *cln;
+ njs_opaque_value_t lvalue, exception;
+ ngx_js_named_path_t *import;

static const njs_str_t line_number_key = njs_str("lineNumber");
static const njs_str_t file_name_key = njs_str("fileName");
@@ -1933,111 +1921,6 @@ ngx_stream_js_init_conf_vm(ngx_conf_t *c


static char *
-ngx_stream_js_import(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
-{
- ngx_stream_js_srv_conf_t *jscf = conf;
-
- u_char *p, *end, c;
- ngx_int_t from;
- ngx_str_t *value, name, path;
- ngx_stream_js_import_t *import;
-
- value = cf->args->elts;
- from = (cf->args->nelts == 4);
-
- if (from) {
- if (ngx_strcmp(value[2].data, "from") != 0) {
- ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
- "invalid parameter \"%V\"", &value[2]);
- return NGX_CONF_ERROR;
- }
- }
-
- name = value[1];
- path = (from ? value[3] : value[1]);
-
- if (!from) {
- end = name.data + name.len;
-
- for (p = end - 1; p >= name.data; p--) {
- if (*p == '/') {
- break;
- }
- }
-
- name.data = p + 1;
- name.len = end - p - 1;
-
- if (name.len < 3
- || ngx_memcmp(&name.data[name.len - 3], ".js", 3) != 0)
- {
- ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
- "cannot extract export name from file path "
- "\"%V\", use extended \"from\" syntax", &path);
- return NGX_CONF_ERROR;
- }
-
- name.len -= 3;
- }
-
- if (name.len == 0) {
- ngx_conf_log_error(NGX_LOG_EMERG, cf, 0, "empty \"name\" parameter");
- return NGX_CONF_ERROR;
- }
-
- p = name.data;
- end = name.data + name.len;
-
- while (p < end) {
- c = ngx_tolower(*p);
-
- if (*p != '_' && (c < 'a' || c > 'z')) {
- if (p == name.data) {
- ngx_conf_log_error(NGX_LOG_EMERG, cf, 0, "cannot start "
- "with \"%c\" in export name \"%V\"", *p,
- &name);
- return NGX_CONF_ERROR;
- }
-
- if (*p < '0' || *p > '9') {
- ngx_conf_log_error(NGX_LOG_EMERG, cf, 0, "invalid character "
- "\"%c\" in export name \"%V\"", *p, &name);
- return NGX_CONF_ERROR;
- }
- }
-
- p++;
- }
-
- if (ngx_strchr(path.data, '\'') != NULL) {
- ngx_conf_log_error(NGX_LOG_EMERG, cf, 0, "invalid character \"'\" "
- "in file path \"%V\"", &path);
- return NGX_CONF_ERROR;
- }
-
- if (jscf->imports == NGX_CONF_UNSET_PTR) {
- jscf->imports = ngx_array_create(cf->pool, 4,
- sizeof(ngx_stream_js_import_t));
- if (jscf->imports == NULL) {
- return NGX_CONF_ERROR;
- }
- }
-
- import = ngx_array_push(jscf->imports);
- if (import == NULL) {
- return NGX_CONF_ERROR;
- }
-
- import->name = name;
- import->path = path;
- import->file = cf->conf_file->file.name.data;
- import->line = cf->conf_file->line;
-
- return NGX_CONF_OK;
-}
-
-
-static char *
ngx_stream_js_set(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
{
ngx_str_t *value, *fname;
_______________________________________________
nginx-devel mailing list -- nginx-devel@nginx.org
To unsubscribe send an email to nginx-devel-leave@nginx.org
Subject Author Views Posted

[njs] Modules: common code for js_import is moved to shared library.

Vadim Zhestikov via nginx-devel 314 September 14, 2022 12:24AM



Sorry, you do not have permission to post/reply in this forum.

Online Users

Guests: 198
Record Number of Users: 6 on February 13, 2018
Record Number of Guests: 421 on December 02, 2018
Powered by nginx      Powered by FreeBSD      PHP Powered      Powered by MariaDB      ipv6 ready