Modules: js_set inline JavaScript expressions.#1025
Modules: js_set inline JavaScript expressions.#1025xeioex wants to merge 2 commits intonginx:masterfrom
Conversation
|
Hi! Maybe add an |
This allows to reference nginx variables with a shorter syntax: `r.var.uri` or `s.var.bytes_sent`.
Added support for inline JavaScript expressions in the js_set directive.
Previously, js_set only accepted function references:
js_set $var main.handler;
Now it also accepts inline expressions:
js_set $var '(r.uri)';
js_set $var 'r.headersIn["Host"] || "none"';
Additionally, nginx-style $variable references are expanded to
the corresponding JavaScript variable access. For example:
js_set $var '$uri.toUpperCase()';
is equivalent to:
js_set $var 'r.variables.uri.toUpperCase()';
In stream context, $var expands to s.variables.var.
|
My main concern is the $var shorthand implemented as textual substitution at config-parse time. Since this is not js-aware, it can rewrite $name inside js string literals or other contexts where the user may expect literal text, which makes the feature surprising and harder to document. Lets avoid this rewriting |
Agree, and I removed the config time rewriting for the same reasons. Instead, |
Added support for inline JavaScript expressions in the js_set directive. Previously, js_set only accepted function references:
Now it also accepts inline expressions:
Additionally, nginx-style $variable references are expanded to the corresponding JavaScript variable access. For example:
is equivalent to:
In stream context, $var expands to s.variables.var.
The expansion is a textual substitution at configuration parse time for $name patterns where name starts with [A-Za-z_].
Comparison of Approaches
1.
$varShorthand vs No Shorthand$varshorthand'$uri.toUpperCase()''r.uri.toUpperCase()'r.variables.uri.toUpperCase()$varreferencesr.variables.*onlyr.uri,r.headersIn,r.args, etc.)$urimeansr.variables.uri, notr.uri${...}template syntaxr.variables.*mappingrAPIjs_import—$uriis not valid JSjs_importfilesr.variables.*;$ambiguity with JS syntax; textual substitution may surprise users; inline expressions tend to grow — when moved to ajs_importfile the shorthand stops working, forcing a rewriter.variables.urivs$uri)2. Expression Syntax (current) vs Template Literals
js_set $v 'expr';js_set $v '`...${expr}...`';return (expr)return (`...`)'$uri + "_" + $host''`${r.uri}_${r.host}`''...')$varshorthand$is claimed by${...}${...}${...}blocks$varshorthand works, clear boundary (one expression), nginx users don't need to learn template literal semantics+is verbose, quoting gets awkward for strings within expressions (r.headersIn["Foo"] || "none")$varshorthand, backtick-inside-single-quote nesting is unusual, blurs the line between config and inline code, marginal benefit over expression syntaxPlease, share your thoughts on different approaches.