[njs] Modules: fixed process.env object.
noreply at nginx.com
noreply at nginx.com
Wed Nov 6 16:17:02 UTC 2024
details: https://github.com/nginx/njs/commit/07545f1670fa6e0cbd6c4dd13244246265560852
branches: master
commit: 07545f1670fa6e0cbd6c4dd13244246265560852
user: Dmitry Volyntsev <xeioex at nginx.com>
date: Tue, 5 Nov 2024 18:19:48 -0800
description:
Modules: fixed process.env object.
Previously, it ignored changes to environment variables introduced
with "env" directive.
---
nginx/ngx_js.c | 4 ++
nginx/t/js_process.t | 84 ++++++++++++++++++++++++++++++++++
nginx/t/stream_js_process.t | 108 ++++++++++++++++++++++++++++++++++++++++++++
3 files changed, 196 insertions(+)
diff --git a/nginx/ngx_js.c b/nginx/ngx_js.c
index 1ac53baa..20c8b2f3 100644
--- a/nginx/ngx_js.c
+++ b/nginx/ngx_js.c
@@ -3832,6 +3832,10 @@ ngx_js_init_conf_vm(ngx_conf_t *cf, ngx_js_loc_conf_t *conf,
ngx_pool_cleanup_t *cln;
ngx_js_named_path_t *import;
+ if (ngx_set_environment(cf->cycle, NULL) == NULL) {
+ return NGX_ERROR;
+ }
+
if (conf->preload_objects != NGX_CONF_UNSET_PTR) {
if (ngx_js_init_preload_vm(cf, (ngx_js_loc_conf_t *)conf) != NGX_OK) {
return NGX_ERROR;
diff --git a/nginx/t/js_process.t b/nginx/t/js_process.t
new file mode 100644
index 00000000..40c1fc1b
--- /dev/null
+++ b/nginx/t/js_process.t
@@ -0,0 +1,84 @@
+#!/usr/bin/perl
+
+# (C) Dmitry Volyntsev
+# (C) Nginx, Inc.
+
+# Tests for http njs module, process object.
+
+###############################################################################
+
+use warnings;
+use strict;
+
+use Test::More;
+use Socket qw/ CRLF /;
+
+BEGIN { use FindBin; chdir($FindBin::Bin); }
+
+use lib 'lib';
+use Test::Nginx;
+
+###############################################################################
+
+select STDERR; $| = 1;
+select STDOUT; $| = 1;
+
+my $t = Test::Nginx->new()->has(qw/http/)
+ ->write_file_expand('nginx.conf', <<'EOF');
+
+%%TEST_GLOBALS%%
+
+daemon off;
+
+events {
+}
+
+env FOO=bar;
+env BAR=baz;
+
+http {
+ %%TEST_GLOBALS_HTTP%%
+
+ js_import test.js;
+
+ server {
+ listen 127.0.0.1:8080;
+ server_name localhost;
+
+ location /argv {
+ js_content test.argv;
+ }
+
+ location /env {
+ js_content test.env;
+ }
+ }
+}
+
+EOF
+
+$t->write_file('test.js', <<EOF);
+ function argv(r) {
+ var av = process.argv;
+ r.return(200,`\${Array.isArray(av)} \${av[0].indexOf('nginx') >= 0}`);
+ }
+
+ function env(r) {
+ var e = process.env[r.args.var];
+ r.return(200, e ? e : 'undefined');
+ }
+
+ export default { argv, env };
+
+EOF
+
+$t->try_run('no njs process object')->plan(4);
+
+###############################################################################
+
+like(http_get('/argv'), qr/true true/, 'argv');
+like(http_get('/env?var=FOO'), qr/bar/, 'env FOO');
+like(http_get('/env?var=BAR'), qr/baz/, 'env BAR');
+like(http_get('/env?var=HOME'), qr/undefined/, 'env HOME');
+
+###############################################################################
diff --git a/nginx/t/stream_js_process.t b/nginx/t/stream_js_process.t
new file mode 100644
index 00000000..85becd31
--- /dev/null
+++ b/nginx/t/stream_js_process.t
@@ -0,0 +1,108 @@
+#!/usr/bin/perl
+
+# (C) Dmitry Volyntsev
+# (C) Nginx, Inc.
+
+# Tests for stream njs module, process object.
+
+###############################################################################
+
+use warnings;
+use strict;
+
+use Test::More;
+
+BEGIN { use FindBin; chdir($FindBin::Bin); }
+
+use lib 'lib';
+use Test::Nginx;
+use Test::Nginx::Stream qw/ stream /;
+
+###############################################################################
+
+select STDERR; $| = 1;
+select STDOUT; $| = 1;
+
+my $t = Test::Nginx->new()->has(qw/stream stream_return/)
+ ->write_file_expand('nginx.conf', <<'EOF');
+
+%%TEST_GLOBALS%%
+
+daemon off;
+
+events {
+}
+
+env FOO=bar;
+env BAR=baz;
+
+stream {
+ %%TEST_GLOBALS_STREAM%%
+
+ js_import test.js;
+
+ js_set $env_foo test.env_foo;
+ js_set $env_bar test.env_bar;
+ js_set $env_home test.env_home;
+ js_set $argv test.argv;
+
+ server {
+ listen 127.0.0.1:8081;
+ return $env_foo;
+ }
+
+ server {
+ listen 127.0.0.1:8082;
+ return $env_bar;
+ }
+
+ server {
+ listen 127.0.0.1:8083;
+ return $env_home;
+ }
+
+ server {
+ listen 127.0.0.1:8084;
+ return $argv;
+ }
+}
+
+EOF
+
+$t->write_file('test.js', <<EOF);
+ function env(s, v) {
+ var e = process.env[v];
+ return e ? e : 'undefined';
+ }
+
+ function env_foo(s) {
+ return env(s, 'FOO');
+ }
+
+ function env_bar(s) {
+ return env(s, 'BAR');
+ }
+
+ function env_home(s) {
+ return env(s, 'HOME');
+ }
+
+ function argv(r) {
+ var av = process.argv;
+ return `\${Array.isArray(av)} \${av[0].indexOf('nginx') >= 0}`;
+ }
+
+ export default { env_foo, env_bar, env_home, argv };
+
+EOF
+
+$t->try_run('no njs stream session object')->plan(4);
+
+###############################################################################
+
+is(stream('127.0.0.1:' . port(8081))->read(), 'bar', 'env.FOO');
+is(stream('127.0.0.1:' . port(8082))->read(), 'baz', 'env.BAR');
+is(stream('127.0.0.1:' . port(8083))->read(), 'undefined', 'env HOME');
+is(stream('127.0.0.1:' . port(8084))->read(), 'true true', 'argv');
+
+###############################################################################
More information about the nginx-devel
mailing list