diff xml/en/docs/stream/ngx_stream_js_module.xml @ 2237:5268c13196f2

Documented njs changes triggered by njs-0.2.4.
author Yaroslav Zhuravlev <yar@nginx.com>
date Thu, 13 Sep 2018 21:10:42 +0300
parents dfc49994218c
children 467aef18bf12
line wrap: on
line diff
--- a/xml/en/docs/stream/ngx_stream_js_module.xml
+++ b/xml/en/docs/stream/ngx_stream_js_module.xml
@@ -9,7 +9,7 @@
 <module name="Module ngx_stream_js_module"
         link="/en/docs/stream/ngx_stream_js_module.html"
         lang="en"
-        rev="12">
+        rev="13">
 
 <section id="summary">
 
@@ -31,6 +31,13 @@ Download and install instructions are av
 <section id="example" name="Example Configuration">
 
 <para>
+<note>
+This example is valid for
+njs <link doc="../njs/njs_changes.xml" id="njs-0.2.4">0.2.4</link>.
+For njs <link doc="../njs/njs_changes.xml" id="njs-0.2.3">0.2.3</link>
+and earlier, use
+<link doc="../njs/njs_api.xml" id="example_legacy">this</link> example.
+</note>
 <example>
 load_module modules/ngx_stream_js_module.so;
 ...
@@ -38,22 +45,22 @@ load_module modules/ngx_stream_js_module
 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 @@ http {
 <para>
 The <path>stream.js</path> file:
 <example>
-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();
 }
 </example>
 </para>