=encoding utf-8

=head1 NAME

ngx_stream_js_module - Module ngx_stream_js_module




=head1



The C<ngx_stream_js_module> module is used to implement
handlers in L<njs|index> —
a subset of the JavaScript language.





This module is not built by default.
Download and install instructions are available
L<here|install>.




=head1 Example Configuration




    
    load_module modules/ngx_stream_js_module.so;
    ...
    
    stream {
        js_include stream.js;
    
        js_set $bar bar;
        js_set $req_line req_line;
    
        server {
            listen 12345;
    
            js_preread preread;
            return     $req_line;
        }
    
        server {
            listen 12346;
    
            js_access  access;
            proxy_pass 127.0.0.1:8000;
            js_filter  header_inject;
        }
    }
    
    http {
        server {
            listen 8000;
            location / {
                return 200 $http_foo\n;
            }
        }
    }







The F<stream.js> file:

    
    var line = '';
    
    function bar(s) {
        var v = s.variables;
        s.log("hello from bar() handler!");
        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;
    }
    
    // Read HTTP request line.
    // Collect bytes in 'req' until
    // request line is read.
    // Injects HTTP header into a client's request
    
    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;
        }
    
        s.allow();
    }






=head1 Directives

=head2 js_access


B<syntax:> js_access I<I<C<function>>>



B<context:> I<stream>


B<context:> I<server>





Sets an njs function which will be called at the
L<access|stream_processing> phase.







=head2 js_filter


B<syntax:> js_filter I<I<C<function>>>



B<context:> I<stream>


B<context:> I<server>





Sets a data filter.







=head2 js_include


B<syntax:> js_include I<I<C<file>>>



B<context:> I<stream>





Specifies a file that implements server and variable handlers in njs.







=head2 js_path


B<syntax:> js_path I<
I<C<path>>>



B<context:> I<http>



This directive appeared in version 0.3.0.





Sets an additional path for njs modules.







=head2 js_preread


B<syntax:> js_preread I<I<C<function>>>



B<context:> I<stream>


B<context:> I<server>





Sets an njs function which will be called at the
L<preread|stream_processing> phase.







=head2 js_set


B<syntax:> js_set I<
I<C<$variable>> I<C<function>>>



B<context:> I<stream>





Sets an njs function for the specified variable.







=head1 Session Object Properties



Each stream njs handler receives one argument, a stream session
L<object|reference>.




