Skip to content

Commit 07545f1

Browse files
committed
Modules: fixed process.env object.
Previously, it ignored changes to environment variables introduced with "env" directive.
1 parent 8666e21 commit 07545f1

File tree

3 files changed

+196
-0
lines changed

3 files changed

+196
-0
lines changed

nginx/ngx_js.c

+4
Original file line numberDiff line numberDiff line change
@@ -3832,6 +3832,10 @@ ngx_js_init_conf_vm(ngx_conf_t *cf, ngx_js_loc_conf_t *conf,
38323832
ngx_pool_cleanup_t *cln;
38333833
ngx_js_named_path_t *import;
38343834

3835+
if (ngx_set_environment(cf->cycle, NULL) == NULL) {
3836+
return NGX_ERROR;
3837+
}
3838+
38353839
if (conf->preload_objects != NGX_CONF_UNSET_PTR) {
38363840
if (ngx_js_init_preload_vm(cf, (ngx_js_loc_conf_t *)conf) != NGX_OK) {
38373841
return NGX_ERROR;

nginx/t/js_process.t

+84
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
#!/usr/bin/perl
2+
3+
# (C) Dmitry Volyntsev
4+
# (C) Nginx, Inc.
5+
6+
# Tests for http njs module, process object.
7+
8+
###############################################################################
9+
10+
use warnings;
11+
use strict;
12+
13+
use Test::More;
14+
use Socket qw/ CRLF /;
15+
16+
BEGIN { use FindBin; chdir($FindBin::Bin); }
17+
18+
use lib 'lib';
19+
use Test::Nginx;
20+
21+
###############################################################################
22+
23+
select STDERR; $| = 1;
24+
select STDOUT; $| = 1;
25+
26+
my $t = Test::Nginx->new()->has(qw/http/)
27+
->write_file_expand('nginx.conf', <<'EOF');
28+
29+
%%TEST_GLOBALS%%
30+
31+
daemon off;
32+
33+
events {
34+
}
35+
36+
env FOO=bar;
37+
env BAR=baz;
38+
39+
http {
40+
%%TEST_GLOBALS_HTTP%%
41+
42+
js_import test.js;
43+
44+
server {
45+
listen 127.0.0.1:8080;
46+
server_name localhost;
47+
48+
location /argv {
49+
js_content test.argv;
50+
}
51+
52+
location /env {
53+
js_content test.env;
54+
}
55+
}
56+
}
57+
58+
EOF
59+
60+
$t->write_file('test.js', <<EOF);
61+
function argv(r) {
62+
var av = process.argv;
63+
r.return(200,`\${Array.isArray(av)} \${av[0].indexOf('nginx') >= 0}`);
64+
}
65+
66+
function env(r) {
67+
var e = process.env[r.args.var];
68+
r.return(200, e ? e : 'undefined');
69+
}
70+
71+
export default { argv, env };
72+
73+
EOF
74+
75+
$t->try_run('no njs process object')->plan(4);
76+
77+
###############################################################################
78+
79+
like(http_get('/argv'), qr/true true/, 'argv');
80+
like(http_get('/env?var=FOO'), qr/bar/, 'env FOO');
81+
like(http_get('/env?var=BAR'), qr/baz/, 'env BAR');
82+
like(http_get('/env?var=HOME'), qr/undefined/, 'env HOME');
83+
84+
###############################################################################

nginx/t/stream_js_process.t

+108
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
#!/usr/bin/perl
2+
3+
# (C) Dmitry Volyntsev
4+
# (C) Nginx, Inc.
5+
6+
# Tests for stream njs module, process object.
7+
8+
###############################################################################
9+
10+
use warnings;
11+
use strict;
12+
13+
use Test::More;
14+
15+
BEGIN { use FindBin; chdir($FindBin::Bin); }
16+
17+
use lib 'lib';
18+
use Test::Nginx;
19+
use Test::Nginx::Stream qw/ stream /;
20+
21+
###############################################################################
22+
23+
select STDERR; $| = 1;
24+
select STDOUT; $| = 1;
25+
26+
my $t = Test::Nginx->new()->has(qw/stream stream_return/)
27+
->write_file_expand('nginx.conf', <<'EOF');
28+
29+
%%TEST_GLOBALS%%
30+
31+
daemon off;
32+
33+
events {
34+
}
35+
36+
env FOO=bar;
37+
env BAR=baz;
38+
39+
stream {
40+
%%TEST_GLOBALS_STREAM%%
41+
42+
js_import test.js;
43+
44+
js_set $env_foo test.env_foo;
45+
js_set $env_bar test.env_bar;
46+
js_set $env_home test.env_home;
47+
js_set $argv test.argv;
48+
49+
server {
50+
listen 127.0.0.1:8081;
51+
return $env_foo;
52+
}
53+
54+
server {
55+
listen 127.0.0.1:8082;
56+
return $env_bar;
57+
}
58+
59+
server {
60+
listen 127.0.0.1:8083;
61+
return $env_home;
62+
}
63+
64+
server {
65+
listen 127.0.0.1:8084;
66+
return $argv;
67+
}
68+
}
69+
70+
EOF
71+
72+
$t->write_file('test.js', <<EOF);
73+
function env(s, v) {
74+
var e = process.env[v];
75+
return e ? e : 'undefined';
76+
}
77+
78+
function env_foo(s) {
79+
return env(s, 'FOO');
80+
}
81+
82+
function env_bar(s) {
83+
return env(s, 'BAR');
84+
}
85+
86+
function env_home(s) {
87+
return env(s, 'HOME');
88+
}
89+
90+
function argv(r) {
91+
var av = process.argv;
92+
return `\${Array.isArray(av)} \${av[0].indexOf('nginx') >= 0}`;
93+
}
94+
95+
export default { env_foo, env_bar, env_home, argv };
96+
97+
EOF
98+
99+
$t->try_run('no njs stream session object')->plan(4);
100+
101+
###############################################################################
102+
103+
is(stream('127.0.0.1:' . port(8081))->read(), 'bar', 'env.FOO');
104+
is(stream('127.0.0.1:' . port(8082))->read(), 'baz', 'env.BAR');
105+
is(stream('127.0.0.1:' . port(8083))->read(), 'undefined', 'env HOME');
106+
is(stream('127.0.0.1:' . port(8084))->read(), 'true true', 'argv');
107+
108+
###############################################################################

0 commit comments

Comments
 (0)