# HG changeset patch # User Yaroslav Zhuravlev # Date 1587570851 -3600 # Node ID 407c5bd5bffc3df94470f66bf78cc6acf6e1dc46 # Parent 8cc141e0460f221fce78abe97654ecb9aaf84c32 Documented the js_import directive and corresponding changes. Also the following changes: - js_include is deprecated - js_access, js_content, js_filter, js_preread, js_set can now accept module.function - Example Configuration changed for both http and stream js modules. diff --git a/xml/en/docs/http/ngx_http_js_module.xml b/xml/en/docs/http/ngx_http_js_module.xml --- a/xml/en/docs/http/ngx_http_js_module.xml +++ b/xml/en/docs/http/ngx_http_js_module.xml @@ -9,7 +9,7 @@ + rev="20">
@@ -32,22 +32,24 @@ Download and install instructions are av
+The example works since +0.4.0. load_module modules/ngx_http_js_module.so; ... http { - js_include http.js; + js_import http.js; - js_set $foo foo; - js_set $summary summary; + js_set $foo http.foo; + js_set $summary http.summary; server { listen 8000; location / { add_header X-Foo $foo; - js_content baz; + js_content http.baz; } location = /summary { @@ -55,7 +57,7 @@ http { } location = /hello { - js_content hello; + js_content http.hello; } } } @@ -110,6 +112,8 @@ function baz(r) { function hello(r) { r.return(200, "Hello world!"); } + +export default {foo, summary, baz, hello}; @@ -119,13 +123,44 @@ function hello(r) {
-function +function | module.function location limit_except Sets an njs function as a location content handler. +Since 0.4.0, +a module function can be referenced. + + + + + + +module.js | +export_name from module.js + +http +0.4.0 + + +Imports a module that implements location and variable handlers in njs. +The export_name is used as a namespace +to access module functions. +If the export_name is not specified, +the module name will be used as a namespace. + +js_import http.js; + +Here, the module name http is used as a namespace +while accessing exports. +If the imported module contains foo(), +http.foo is used to refer to it. + + + +Several js_import directives can be specified. @@ -137,7 +172,25 @@ Sets an njs function as a location conte http -Specifies a file that implements location and variable handlers in njs. +Specifies a file that implements location and variable handlers in njs: + +nginx.conf: +js_include http.js; +location /version { + js_content version; +} + +http.js: +function version(r) { + r.return(200, njs.version); +} + + + + +The directive is deprecated since +0.4.0, +the directive should be used instead. @@ -159,12 +212,15 @@ Sets an additional path for njs modules. -$variable function +$variable function | +module.function http Sets an njs function for the specified variable. +Since 0.4.0, +a module function can be referenced. diff --git a/xml/en/docs/stream/ngx_stream_js_module.xml b/xml/en/docs/stream/ngx_stream_js_module.xml --- a/xml/en/docs/stream/ngx_stream_js_module.xml +++ b/xml/en/docs/stream/ngx_stream_js_module.xml @@ -9,7 +9,7 @@ + rev="18">
@@ -31,29 +31,31 @@ Download and install instructions are av
+The example works since +0.4.0. load_module modules/ngx_stream_js_module.so; ... stream { - js_include stream.js; + js_import stream.js; - js_set $bar bar; - js_set $req_line req_line; + js_set $bar stream.bar; + js_set $req_line stream.req_line; server { listen 12345; - js_preread preread; + js_preread stream.preread; return $req_line; } server { listen 12346; - js_access access; + js_access stream.access; proxy_pass 127.0.0.1:8000; - js_filter header_inject; + js_filter stream.header_inject; } } @@ -121,6 +123,8 @@ function access(s) { s.allow(); } + +export default {bar, preread, req_line, access}; @@ -130,7 +134,7 @@ function access(s) {
-function +function | module.function stream server @@ -138,19 +142,52 @@ function access(s) { Sets an njs function which will be called at the access phase. +Since 0.4.0, +a module function can be referenced. -function +function | module.function stream server Sets a data filter. +Since 0.4.0, +a module function can be referenced. + + + + + + +module.js | +export_name from module.js + +stream +0.4.0 + + +Imports a module that implements location and variable handlers in njs. +The export_name is used as a namespace +to access module functions. +If the export_name is not specified, +the module name will be used as a namespace. + +js_import stream.js; + +Here, the module name stream is used as a namespace +while accessing exports. +If the imported module contains foo(), +stream.foo is used to refer to it. + + + +Several js_import directives can be specified. @@ -162,7 +199,27 @@ Sets a data filter. stream -Specifies a file that implements server and variable handlers in njs. +Specifies a file that implements server and variable handlers in njs: + +nginx.conf: +js_include stream.js; +js_set $js_addr address; +server { + listen 127.0.0.1:12345; + return $js_addr; +} + +stream.js: +function address(s) { + return s.remoteAddress; +} + + + + +The directive is deprecated since +0.4.0, +the directive should be used instead. @@ -183,7 +240,7 @@ Sets an additional path for njs modules. -function +function | module.function stream server @@ -191,6 +248,8 @@ Sets an additional path for njs modules. Sets an njs function which will be called at the preread phase. +Since 0.4.0, +a module function can be referenced. @@ -198,12 +257,15 @@ Sets an njs function which will be calle -$variable function +$variable function | +module.function stream Sets an njs function for the specified variable. +Since 0.4.0, +a module function can be referenced. diff --git a/xml/ru/docs/http/ngx_http_js_module.xml b/xml/ru/docs/http/ngx_http_js_module.xml --- a/xml/ru/docs/http/ngx_http_js_module.xml +++ b/xml/ru/docs/http/ngx_http_js_module.xml @@ -9,7 +9,7 @@ + rev="20">
@@ -32,22 +32,24 @@
+Пример работает начиная с версии +0.4.0. load_module modules/ngx_http_js_module.so; ... http { - js_include http.js; + js_import http.js; - js_set $foo foo; - js_set $summary summary; + js_set $foo http.foo; + js_set $summary http.summary; server { listen 8000; location / { add_header X-Foo $foo; - js_content baz; + js_content http.baz; } location = /summary { @@ -55,7 +57,7 @@ http { } location = /hello { - js_content hello; + js_content http.hello; } } } @@ -63,7 +65,7 @@ http { -Файл http.js: +The http.js file: function foo(r) { r.log("hello from foo() handler"); @@ -110,6 +112,8 @@ function baz(r) { function hello(r) { r.return(200, "Hello world!"); } + +export default {foo, summary, baz, hello}; @@ -119,13 +123,45 @@ function hello(r) {
-функция +функция | модуль.функция location limit_except Задаёт функцию njs в качестве обработчика содержимого location. +Начиная с версии 0.4.0 +можно ссылаться на функцию модуля. + + + + + + +модуль.js | +имя_экспорта из модуль.js + +http +0.4.0 + + +Импортирует модуль, позволяющий задавать обработчики location и переменных +на njs. +Имя_экспорта является пространством имён +при доступе к функциям модуля. +Если имя_экспорта не задано, +то пространством имён будет являться имя модуля. + +js_import http.js; + +В примере при доступе к экспорту в качестве +пространства имён используется имя модуля http. +Если импортируемый модуль содержит foo(), +то для доступа используется http.foo. + + + +Директив js_import может быть несколько. @@ -137,8 +173,25 @@ function hello(r) { http -Задаёт файл, позволяющий задавать обработчики location и переменных -на njs. +Задаёт файл, позволяющий задавать обработчики location и переменных на njs: + +nginx.conf: +js_include http.js; +location /version { + js_content version; +} + +http.js: +function version(r) { + r.return(200, njs.version); +} + + + + +Директива устарела начиная с +0.4.0, +вместо неё следует использовать директиву . @@ -160,12 +213,15 @@ function hello(r) { -$переменная функция +$переменная функция | +модуль.функция http Задаёт функцию njs для указанной переменной. +Начиная с 0.4.0 +можно ссылаться на функцию модуля. diff --git a/xml/ru/docs/stream/ngx_stream_js_module.xml b/xml/ru/docs/stream/ngx_stream_js_module.xml --- a/xml/ru/docs/stream/ngx_stream_js_module.xml +++ b/xml/ru/docs/stream/ngx_stream_js_module.xml @@ -9,7 +9,7 @@ + rev="18">
@@ -31,29 +31,31 @@
+Пример работает начиная с версии +0.4.0. load_module modules/ngx_stream_js_module.so; ... stream { - js_include stream.js; + js_import stream.js; - js_set $bar bar; - js_set $req_line req_line; + js_set $bar stream.bar; + js_set $req_line stream.req_line; server { listen 12345; - js_preread preread; + js_preread stream.preread; return $req_line; } server { listen 12346; - js_access access; + js_access stream.access; proxy_pass 127.0.0.1:8000; - js_filter header_inject; + js_filter stream.header_inject; } } @@ -121,6 +123,8 @@ function access(s) { s.allow(); } + +export default {bar, preread, req_line, access}; @@ -130,7 +134,7 @@ function access(s) {
-функция +функция | модуль.функция stream server @@ -138,19 +142,53 @@ function access(s) { Задаёт функцию njs, которая будет вызываться в access-фазе. +Начиная с 0.4.0 +можно ссылаться на функцию модуля. -функция +функция | модуль.функция stream server Задаёт фильтр данных. +Начиная с 0.4.0 +можно ссылаться на функцию модуля. + + + + + + +модуль.js | +имя_экспорта из модуль.js + +stream +0.4.0 + + +Импортирует модуль, позволяющий задавать обработчики location и переменных +на njs. +Имя_экспорта является пространством имён +при доступе к функциям модуля. +Если имя_экспорта не задано, +то пространством имён будет являться имя модуля. + +js_import stream.js; + +В примере при доступе к экспорту в качестве +пространства имён используется имя модуля stream. +Если импортируемый модуль содержит foo(), +то для доступа используется stream.foo. + + + +Директив js_import может быть несколько. @@ -162,15 +200,34 @@ function access(s) { stream -Задаёт файл, который позволяет -задавать обработчики server и переменных на njs. +Задаёт файл, который позволяет задавать обработчики server и переменных на njs: + +nginx.conf: +js_include stream.js; +js_set $js_addr address; +server { + listen 127.0.0.1:12345; + return $js_addr; +} + +stream.js: +function address(s) { + return s.remoteAddress; +} + + + + +Директива устарела начиная с +0.4.0, +вместо неё следует использовать директиву . -функция +функция | модуль.функция stream server @@ -178,6 +235,8 @@ function access(s) { Задаёт функцию njs, которая будет вызываться в preread-фазе. +Начиная с 0.4.0 +можно ссылаться на функцию модуля. @@ -199,12 +258,15 @@ function access(s) { -$переменная функция +$переменная функция | +модуль.функция stream Задаёт функцию njs для указанной переменной. +Начиная с 0.4.0 +можно ссылаться на функцию модуля.