Skip to content

Modules: added jsVarNames() method.#1061

Merged
xeioex merged 1 commit into
nginx:masterfrom
xeioex:js_var_names
May 18, 2026
Merged

Modules: added jsVarNames() method.#1061
xeioex merged 1 commit into
nginx:masterfrom
xeioex:js_var_names

Conversation

@xeioex
Copy link
Copy Markdown
Contributor

@xeioex xeioex commented May 16, 2026

The method returns names of variables declared with js_var. An optional string argument filters returned names by prefix.

This lets JavaScript code discover a configuration-defined variable schema without duplicating every variable name in the script. For example:

js_var $extract_name;
js_var $extract_arguments_workspace;

function handler(r) {
    r.jsVarNames("extract_").forEach(function(name) {
        r.variables[name] = extract(name.slice(8));
    });
}

The same API is available as s.jsVarNames() in stream. Variables created by js_set or by the core modules are not returned.

@VadimZhestikov
Copy link
Copy Markdown
Contributor

Minor suggestions:


Stream module implementations

Fully symmetric to the HTTP implementations — ngx_stream_session_t, ngx_stream_core_main_conf_t,
ngx_stream_js_variable_var used in place of their HTTP counterparts. Logic is identical.

Minor inconsistency between the two QJS variants — n = 0 is initialised at the declaration site in the
HTTP version but near its first use in the stream version:

  // HTTP QJS:
      n = 0;
      cmcf = ngx_http_get_module_main_conf(r, ngx_http_core_module);
      v = cmcf->variables.elts;
  // Stream QJS:
      cmcf = ngx_stream_get_module_main_conf(s, ngx_stream_core_module);
      v = cmcf->variables.elts;
      n = 0;

Not a bug, but worth harmonizing.


Tests:

  1. jsVarNames(null) — both implementations would throw TypeError (null is not undefined, not a string),
    but this isn't tested.
  2. jsVarNames(undefined) explicitly passed — both implementations treat it the same as no argument
    (returns all), but not tested explicitly. Low priority.

VadimZhestikov
VadimZhestikov previously approved these changes May 18, 2026
Copy link
Copy Markdown
Contributor

@VadimZhestikov VadimZhestikov left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good

@github-project-automation github-project-automation Bot moved this from New to In Progress in NGINX OSS Unified Workspace May 18, 2026
The method returns names of variables declared with js_var.  An optional
string argument filters returned names by prefix.

This lets JavaScript code discover a configuration-defined variable schema
without duplicating every variable name in the script.  For example:

    js_var $extract_name;
    js_var $extract_arguments_workspace;

    function handler(r) {
        r.jsVarNames("extract_").forEach(function(name) {
            r.variables[name] = extract(name.slice(8));
        });
    }

The same API is available as s.jsVarNames() in stream.  Variables created by
js_set or by the core modules are not returned.
Copy link
Copy Markdown
Contributor

@VadimZhestikov VadimZhestikov left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good

@xeioex xeioex merged commit f9ff315 into nginx:master May 18, 2026
2 checks passed
@github-project-automation github-project-automation Bot moved this from In Progress to Done in NGINX OSS Unified Workspace May 18, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

2 participants