# HG changeset patch # User Yaroslav Zhuravlev # Date 1536862242 -10800 # Node ID 5268c13196f2b5820eb02b0fc8cd96d333a7b139 # Parent 9f0f4f5c1a30991a044db3bacec5837315bbfac0 Documented njs changes triggered by njs-0.2.4. diff -r 9f0f4f5c1a30 -r 5268c13196f2 xml/en/docs/njs/njs_api.xml --- a/xml/en/docs/njs/njs_api.xml Mon Sep 17 06:59:29 2018 +0000 +++ b/xml/en/docs/njs/njs_api.xml Thu Sep 13 21:10:42 2018 +0300 @@ -9,7 +9,7 @@
+ rev="7">
@@ -1004,7 +1004,6 @@ arguments string - body request body @@ -1038,58 +1037,48 @@ ngx_stream_js_module module. All string properties of the object are byte strings. + + + +Prior to njs 0.2.4, +the stream session object had some properties which are currently +removed. + + + + -s.remoteAddress - -client address, read-only - - -s.eof +s.allow() -a boolean read-only property, true if the current buffer is the last buffer - - -s.fromUpstream - -a boolean read-only property, -true if the current buffer is from the upstream server to the client - - -s.buffer - -the current buffer, writable +successfully finalizes the phase handler +(0.2.4) -s.variables{} +s.decline() -nginx variables object, read-only +finalizes the phase handler and passes control to the next handler +(0.2.4) -s.OK +s.deny() -the OK return code +finalizes the phase handler with the access error code +(0.2.4) -s.DECLINED +s.done([code]) -the DECLINED return code +successfully finalizes the current phase handler +or finalizes it with the specified numeric code +(0.2.4). -s.AGAIN - -the AGAIN return code - - -s.ERROR +s.error(string) -the ERROR return code - - -s.ABORT - -the ABORT return code +writes a sent string to the error log +on the error level of logging s.log(string) @@ -1098,16 +1087,187 @@ on the info level of logging +s.off(eventName) + +unregisters the callback set by the s.on() method +(0.2.4) + + +s.on(event, +callback) + +registers a callback for the specified event +(0.2.4). + + +An event may be one of the following strings: + +upload + +new data from a client + + +download + +new data to a client + + + + + + +The completion callback has the following prototype: +callback(data, flags), where +data is string, +flags is an object +with the following properties: + +last + +a boolean value, true if data is a last buffer. + + + + + + +s.remoteAddress + +client address, read-only + + +s.send(data[, +options]) + +sends the data to the client +(0.2.4). +The options is an object used +to override nginx buffer flags derived from an incoming data chunk buffer. +The flags can be overriden with the following flags: + + + +last + +boolean, true if the buffer is the last buffer + + +flush + +boolean, true if the buffer should have the flush flag + + + +The method can be called multiple times per callback invocation. + + +s.variables{} + +nginx variables object, read-only + + s.warn(string) writes a sent string to the error log on the warning level of logging -s.error(string) + + + + +
+ + +These properties have been removed +in njs 0.2.4 +and are not backward compatible with the existing njs code. + + + + + +s.ABORT + +the ABORT return code + +Starting from njs 0.2.4, +the s.deny() method should be used instead. + + + +s.AGAIN + +the AGAIN return code + +Starting from njs 0.2.4, +the corresponding behavior is achieved if no +s.allow(), +s.deny(), +s.decline(), +s.done() +is invoked and a callback is registered. + + + +s.buffer -writes a sent string to the error log -on the error level of logging +the current buffer, writable + +Starting from 0.2.4, +the s.send() method should be used for writing. +For reading, the current buffer is available as the first argument of the +event callback. + + + +s.DECLINED + +the DECLINED return code + +Starting from njs 0.2.4, +the s.decline() method should be used instead. + + + +s.eof + +a boolean read-only property, true if the current buffer is the last buffer + +Starting from 0.2.4, +the flags.last property +should be used instead. + + + +s.ERROR + +the ERROR return code + +Starting from njs 0.2.4, +an appropriate exception can be thrown to report an error. + + + +s.fromUpstream + +a boolean read-only property, +true if the current buffer is from the upstream server to the client + +Starting from 0.2.4, +a corresponding event +(upload or download) +should be used to handle data to or from client. + + + +s.OK + +the OK return code + +Starting from njs 0.2.4, +the s.allow() method should be used instead. + @@ -1115,6 +1275,8 @@
+
+
@@ -1370,6 +1532,137 @@
+ +
+ + +
+ + +Starting from njs 0.2.4, +stream configuration +example +has been changed. +For njs 0.2.3 +and earlier, use this configuration example: + +load_module modules/ngx_stream_js_module.so; +... + +stream { + js_include stream.js; + + js_set $foo foo; + js_set $bar bar; + + server { + listen 12345; + + js_preread qux; + return $foo; + } + + server { + listen 12346; + + js_access xyz; + proxy_pass 127.0.0.1:8000; + js_filter baz; + } +} + +http { + server { + listen 8000; + location / { + return 200 $http_foo\n; + } + } +} + + + + +The stream.js file: + +var req = ''; +var matched = 0; +var line = ''; + +function qux(s) { + var n = s.buffer.indexOf('\n'); + if (n == -1) { + return s.AGAIN; + } + + line = s.buffer.substr(0, n); +} + +function foo(s) { + return line; +} + +function bar(s) { + var v = s.variables; + s.log("hello from bar() handler!"); + return "foo-var" + v.remote_port + "; pid=" + v.pid; +} + +// The filter processes one buffer per call. +// The buffer is available in s.buffer both for +// reading and writing. Called for both directions. + +function baz(s) { + if (s.fromUpstream || matched) { + return; + } + + // Disable certain addresses. + + if (s.remoteAddress.match('^192.*')) { + return s.ERROR; + } + + // Read HTTP request line. + // Collect bytes in 'req' until request + // line is read. Clear current buffer to + // disable output. + + req = req + s.buffer; + s.buffer = ''; + + var n = req.search('\n'); + + if (n != -1) { + // Inject a new HTTP header. + var rest = req.substr(n + 1); + req = req.substr(0, n + 1); + + var addr = s.remoteAddress; + + s.log('req:' + req); + s.log('rest:' + rest); + + // Output the result and skip further + // processing. + + s.buffer = req + 'Foo: addr_' + addr + '\r\n' + rest; + matched = 1; + } +} + +function xyz(s) { + if (s.remoteAddress.match('^192.*')) { + return s.ABORT; + } +} + + + +
+ +
+
diff -r 9f0f4f5c1a30 -r 5268c13196f2 xml/en/docs/njs/njs_changes.xml --- a/xml/en/docs/njs/njs_changes.xml Mon Sep 17 06:59:29 2018 +0000 +++ b/xml/en/docs/njs/njs_changes.xml Thu Sep 13 21:10:42 2018 +0300 @@ -9,9 +9,182 @@
+
+ + +Release Date: +18 September 2018 + + + +nginx modules: + + + + +Change: +stream module handlers refactored. + + + +New methods and properties: +s.on(), +s.off(), +s.allow(), +s.done(), +s.decline(), +s.deny(). + + + +Removed properties of the +Stream object: +s.OK, +s.ABORT, +s.AGAIN, +s.DECLINED, +s.ERROR +(replaced with +s.allow(), +s.done(), +s.deny()). + + + +s.buffer +(for reading replaced with data argument of +the corresponding callback, for writing use +s.send()). + + + +s.fromUpstream +(replaced with a callback for a corresponding event). + + + +s.eof +(replaced with +flags.last). + + + + + + + + +Core: + + + + +Feature: +added Function.prototype.length. + + + + + +Feature: +introduced sandboxing mode. + + + + + +Improvement: +added exception strings where appropriate. + + + + + +Improvement: +improved wording for primitive type conversion exception. + + + + + +Bugfix: +throwing TypeError +for attempts to change frozen properties. + + + + + +Bugfix: +fixed Object.defineProperty() for existing properties. + + + + + +Bugfix: +respecting the enumerable attribute while iterating by for in. + + + + + +Bugfix: +respecting writable attribute for property handlers. + + + + + +Bugfix: +fixed exception handling in arguments of a function. + + + + + +Bugfix: +fixed Object.prototype.toString for different value types. + + + + + +Bugfix: +fixed Object() constructor for object types arguments. + + + + + +Bugfix: +fixed comparison of objects and strings. + + + + + +Bugfix: +fixed String.slice() for undefined arguments. + + + + + +Bugfix: +miscellaneous additional bugs have been fixed. + + + + + + +
+ +
diff -r 9f0f4f5c1a30 -r 5268c13196f2 xml/en/docs/stream/ngx_stream_js_module.xml --- a/xml/en/docs/stream/ngx_stream_js_module.xml Mon Sep 17 06:59:29 2018 +0000 +++ b/xml/en/docs/stream/ngx_stream_js_module.xml Thu Sep 13 21:10:42 2018 +0300 @@ -9,7 +9,7 @@ + rev="13">
@@ -31,6 +31,13 @@
+ +This example is valid for +njs 0.2.4. +For njs 0.2.3 +and earlier, use +this example. + load_module modules/ngx_stream_js_module.so; ... @@ -38,22 +45,22 @@ stream { js_include stream.js; - js_set $foo foo; js_set $bar bar; + js_set $req_line req_line; server { listen 12345; - js_preread qux; - return $foo; + js_preread preread; + return $req_line; } server { listen 12346; - js_access xyz; + js_access access; proxy_pass 127.0.0.1:8000; - js_filter baz; + js_filter header_inject; } } @@ -71,76 +78,55 @@ The stream.js file: -var req = ''; -var matched = 0; var line = ''; -function qux(s) { - var n = s.buffer.indexOf('\n'); - if (n == -1) { - return s.AGAIN; - } - - line = s.buffer.substr(0, n); -} - -function foo(s) { - return line; -} - function bar(s) { var v = s.variables; s.log("hello from bar() handler!"); - return "foo-var" + v.remote_port + "; pid=" + v.pid; + return "bar-var" + v.remote_port + "; pid=" + v.pid; +} + +function preread(s) { + s.on('upload', function (data, flags) { + var n = data.indexOf('\n'); + if (n != -1) { + line = data.substr(0, n); + s.done(); + } + }); +} + +function req_line(s) { + return line; } -// The filter processes one buffer per call. -// The buffer is available in s.buffer both for -// reading and writing. Called for both directions. +// Read HTTP request line. +// Collect bytes in 'req' until +// request line is read. +// Injects HTTP header into a client's request -function baz(s) { - if (s.fromUpstream || matched) { +var my_header = 'Foo: foo'; +function header_inject(s) { + var req = ''; + s.on('upload', function(data, flags) { + req += data; + var n = req.search('\n'); + if (n != -1) { + var rest = req.substr(n + 1); + req = req.substr(0, n + 1); + s.send(req + my_header + '\r\n' + rest, flags); + s.off('upload'); + } + }); +} + +function access(s) { + if (s.remoteAddress.match('^192.*')) { + s.abort(); return; } - // Disable certain addresses. - - if (s.remoteAddress.match('^192.*')) { - return s.ERROR; - } - - // Read HTTP request line. - // Collect bytes in 'req' until request - // line is read. Clear current buffer to - // disable output. - - req = req + s.buffer; - s.buffer = ''; - - var n = req.search('\n'); - - if (n != -1) { - // Inject a new HTTP header. - var rest = req.substr(n + 1); - req = req.substr(0, n + 1); - - var addr = s.remoteAddress; - - s.log('req:' + req); - s.log('rest:' + rest); - - // Output the result and skip further - // processing. - - s.buffer = req + 'Foo: addr_' + addr + '\r\n' + rest; - matched = 1; - } -} - -function xyz(s) { - if (s.remoteAddress.match('^192.*')) { - return s.ABORT; - } + s.allow(); } diff -r 9f0f4f5c1a30 -r 5268c13196f2 xml/index.xml --- a/xml/index.xml Mon Sep 17 06:59:29 2018 +0000 +++ b/xml/index.xml Thu Sep 13 21:10:42 2018 +0300 @@ -7,6 +7,21 @@ + + +njs-0.2.4 +version has been released, featuring +s.on(), +s.off(), +s.allow(), +s.done(), +s.decline(), +s.deny() +methods support +and more. + + + nginx-1.15.3 diff -r 9f0f4f5c1a30 -r 5268c13196f2 xml/ru/docs/stream/ngx_stream_js_module.xml --- a/xml/ru/docs/stream/ngx_stream_js_module.xml Mon Sep 17 06:59:29 2018 +0000 +++ b/xml/ru/docs/stream/ngx_stream_js_module.xml Thu Sep 13 21:10:42 2018 +0300 @@ -9,7 +9,7 @@ + rev="13">
@@ -31,6 +31,13 @@
+ +Данный пример необходимо использовать с +версией njs 0.2.4. +Для версий njs 0.2.3 +и ранее необходимо использовать +этот пример. + load_module modules/ngx_stream_js_module.so; ... @@ -38,22 +45,22 @@ stream { js_include stream.js; - js_set $foo foo; js_set $bar bar; + js_set $req_line req_line; server { listen 12345; - js_preread qux; - return $foo; + js_preread preread; + return $req_line; } server { listen 12346; - js_access xyz; + js_access access; proxy_pass 127.0.0.1:8000; - js_filter baz; + js_filter header_inject; } } @@ -71,76 +78,40 @@ Файл stream.js: -var req = ''; -var matched = 0; var line = ''; -function qux(s) { - var n = s.buffer.indexOf('\n'); - if (n == -1) { - return s.AGAIN; - } - - line = s.buffer.substr(0, n); -} - -function foo(s) { - return line; -} - function bar(s) { var v = s.variables; s.log("hello from bar() handler!"); - return "foo-var" + v.remote_port + "; pid=" + v.pid; + return "bar-var" + v.remote_port + "; pid=" + v.pid; +} + +function preread(s) { + s.on('upload', function (data, flags) { + var n = data.indexOf('\n'); + if (n != -1) { + line = data.substr(0, n); + s.done(); + } + }); } -// Фильтр обрабатывает один буфер за вызов. -// Буфер недоступен в s.buffer для -// чтения и записи. Вызывается в обоих направлениях. +function req_line(s) { + return line; +} -function baz(s) { - if (s.fromUpstream || matched) { +// Чтение строки HTTP-запроса. +// Получение байт в 'req' до того как +// будет прочитана строка запроса. +// Добавление HTTP-заголовка в запрос клиента + +function access(s) { + if (s.remoteAddress.match('^192.*')) { + s.abort(); return; } - // Отключение определённых адресов. - - if (s.remoteAddress.match('^192.*')) { - return s.ERROR; - } - - // Чтение строки HTTP-запроса. - // Получение байт в 'req' до того как - // будет прочитана строка запроса. Очистка текущего буфера - // для отключения вывода. - - req = req + s.buffer; - s.buffer = ''; - - var n = req.search('\n'); - - if (n != -1) { - // Вставка нового HTTP-заголовка. - var rest = req.substr(n + 1); - req = req.substr(0, n + 1); - - var addr = s.remoteAddress; - - s.log('req:' + req); - s.log('rest:' + rest); - - // Вывод результата и пропуск дальнейшей - // обработки. - - s.buffer = req + 'Foo: addr_' + addr + '\r\n' + rest; - matched = 1; - } -} - -function xyz(s) { - if (s.remoteAddress.match('^192.*')) { - return s.ABORT; - } + s.allow(); }