<!-- review: finished -->

<a id="stream-js"></a>

# JS

The module is used to implement handlers in njs — a subset of the JavaScript language.

In our repositories, the module is built [dynamically](https://en.angie.software//angie/docs/installation/index.md#install-dynamicmodules) and is available as a separate package named
`angie-module-njs` or `angie-pro-module-njs`.

#### NOTE
A lightweight version of the package, named `...-njs-light`, is also
available; however, it can't be used side by side with the regular one.

<a id="configuration-example-100"></a>

## Configuration Example

```nginx
stream {
    js_import stream.js;

    js_set $bar stream.bar;
    js_set $req_line stream.req_line;

    server {
        listen 12345;

        js_preread stream.preread;
        return     $req_line;
    }

    server {
        listen 12346;

        js_access  stream.access;
        proxy_pass 127.0.0.1:8000;
        js_filter  stream.header_inject;
    }
}

http {
    server {
        listen 8000;
        location / {
            return 200 $http_foo\n;
        }
    }
}
```

The `stream.js` file:

```javascript
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.
// Inject 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.deny();
        return;
    }

    s.allow();
}

export default {bar, preread, req_line, header_inject, access};
```

<a id="directives-88"></a>

## Directives

<a id="index-0"></a>

<a id="s-js-access"></a>

### js_access

| [Syntax](https://en.angie.software//angie/docs/configuration/configfile.md#configfile)   | `js_access` function | module.function;   |
|------------------------------------------------------------------------------------------|-------------------------------------------|
| Default                                                                                  | —                                         |
| [Context](https://en.angie.software//angie/docs/configuration/configfile.md#configfile)  | stream, server                            |

Sets an njs function which will be called at the [access phase](https://en.angie.software//angie/docs/configuration/processing.md#stream-sessions). Module functions can be referenced.

The function is called once at the moment when the stream session reaches the [access phase](https://en.angie.software//angie/docs/configuration/processing.md#stream-sessions) for the first time. The function is called with the following arguments:

| `s`   | the [stream session](https://en.angie.software//angie/docs/configuration/njs-reference.md#njs-stream-session) object   |
|-------|------------------------------------------------------------------------------------------------------------------------|

At this phase, it is possible to perform initialization or register a callback with the [s.on()](https://en.angie.software//angie/docs/configuration/njs-reference.md#s-on) method for each incoming data chunk until one of the following methods are called: [s.done()](https://en.angie.software//angie/docs/configuration/njs-reference.md#njs-s-done), [s.decline()](https://en.angie.software//angie/docs/configuration/njs-reference.md#njs-s-decline), [s.allow()](https://en.angie.software//angie/docs/configuration/njs-reference.md#njs-s-allow). As soon as one of these methods is called, the stream session processing switches to the [next phase](https://en.angie.software//angie/docs/configuration/processing.md#stream-sessions) and all current [s.on()](https://en.angie.software//angie/docs/configuration/njs-reference.md#s-on) callbacks are dropped.

<a id="index-1"></a>

<a id="s-js-context-reuse"></a>

### js_context_reuse

| [Syntax](https://en.angie.software//angie/docs/configuration/configfile.md#configfile)   | `js_context_reuse` number;   |
|------------------------------------------------------------------------------------------|------------------------------|
| Default                                                                                  | `js_context_reuse 128;`      |
| [Context](https://en.angie.software//angie/docs/configuration/configfile.md#configfile)  | stream, server               |

Sets the maximum number of JS contexts to be reused for the QuickJS engine. Each context is used for a single stream session. The finished context is put into a pool of reusable contexts. If the pool is full, the context is destroyed.

<a id="index-2"></a>

<a id="s-js-engine"></a>

### js_engine

| [Syntax](https://en.angie.software//angie/docs/configuration/configfile.md#configfile)   | `js_engine` `njs` | `qjs`;   |
|------------------------------------------------------------------------------------------|------------------------------|
| Default                                                                                  | `js_engine njs;`             |
| [Context](https://en.angie.software//angie/docs/configuration/configfile.md#configfile)  | stream, server               |

Sets the JavaScript engine to be used for njs scripts. The `njs` parameter sets the njs engine, also used by default. The `qjs` parameter sets the QuickJS engine.

<a id="index-3"></a>

<a id="s-js-fetch-buffer-size"></a>

### js_fetch_buffer_size

| [Syntax](https://en.angie.software//angie/docs/configuration/configfile.md#configfile)   | `js_fetch_buffer_size` size;   |
|------------------------------------------------------------------------------------------|--------------------------------|
| Default                                                                                  | `js_fetch_buffer_size 16k;`    |
| [Context](https://en.angie.software//angie/docs/configuration/configfile.md#configfile)  | stream, server                 |

Sets the size of the buffer used for reading and writing with [Fetch API](https://en.angie.software//angie/docs/configuration/njs-reference.md#ngx-fetch).

<a id="index-4"></a>

<a id="s-js-fetch-ciphers"></a>

### js_fetch_ciphers

| [Syntax](https://en.angie.software//angie/docs/configuration/configfile.md#configfile)   | `js_fetch_ciphers` ciphers;          |
|------------------------------------------------------------------------------------------|--------------------------------------|
| Default                                                                                  | `js_fetch_ciphers HIGH:!aNULL:!MD5;` |
| [Context](https://en.angie.software//angie/docs/configuration/configfile.md#configfile)  | stream, server                       |

Specifies the enabled ciphers for HTTPS connections with [Fetch API](https://en.angie.software//angie/docs/configuration/njs-reference.md#ngx-fetch). The ciphers are specified in the format understood by the OpenSSL library.

The list of ciphers depends on the version of OpenSSL installed.
The full list can be viewed using the `openssl ciphers` command.

<a id="index-5"></a>

<a id="s-js-fetch-max-response-buffer-size"></a>

### js_fetch_max_response_buffer_size

| [Syntax](https://en.angie.software//angie/docs/configuration/configfile.md#configfile)   | `js_fetch_max_response_buffer_size` size;   |
|------------------------------------------------------------------------------------------|---------------------------------------------|
| Default                                                                                  | `js_fetch_max_response_buffer_size 1m;`     |
| [Context](https://en.angie.software//angie/docs/configuration/configfile.md#configfile)  | stream, server                              |

Sets the maximum size of the response received with [Fetch API](https://en.angie.software//angie/docs/configuration/njs-reference.md#ngx-fetch).

<a id="index-6"></a>

<a id="s-js-fetch-protocols"></a>

### js_fetch_protocols

| [Syntax](https://en.angie.software//angie/docs/configuration/configfile.md#configfile)   | `js_fetch_protocols` [`TLSv1`] [`TLSv1.1`] [`TLSv1.2`] [`TLSv1.3`];   |
|------------------------------------------------------------------------------------------|-----------------------------------------------------------------------|
| Default                                                                                  | `js_fetch_protocols TLSv1 TLSv1.1 TLSv1.2;`                           |
| [Context](https://en.angie.software//angie/docs/configuration/configfile.md#configfile)  | stream, server                                                        |

Enables the specified protocols for HTTPS connections with [Fetch API](https://en.angie.software//angie/docs/configuration/njs-reference.md#ngx-fetch).

<a id="index-7"></a>

<a id="s-js-fetch-timeout"></a>

### js_fetch_timeout

| [Syntax](https://en.angie.software//angie/docs/configuration/configfile.md#configfile)   | `js_fetch_timeout` time;   |
|------------------------------------------------------------------------------------------|----------------------------|
| Default                                                                                  | `js_fetch_timeout 60s;`    |
| [Context](https://en.angie.software//angie/docs/configuration/configfile.md#configfile)  | stream, server             |

Defines a timeout for reading and writing for [Fetch API](https://en.angie.software//angie/docs/configuration/njs-reference.md#ngx-fetch). The timeout is set only between two successive read/write operations, not for the whole response. If no data is transmitted within this time, the connection is closed.

<a id="index-8"></a>

<a id="s-js-fetch-trusted-certificate"></a>

### js_fetch_trusted_certificate

| [Syntax](https://en.angie.software//angie/docs/configuration/configfile.md#configfile)   | `js_fetch_trusted_certificate` file;   |
|------------------------------------------------------------------------------------------|----------------------------------------|
| Default                                                                                  | —                                      |
| [Context](https://en.angie.software//angie/docs/configuration/configfile.md#configfile)  | stream, server                         |

Specifies a file with trusted CA certificates in the PEM format used to verify the HTTPS certificate with [Fetch API](https://en.angie.software//angie/docs/configuration/njs-reference.md#ngx-fetch).

<a id="index-9"></a>

<a id="s-js-fetch-verify"></a>

### js_fetch_verify

| [Syntax](https://en.angie.software//angie/docs/configuration/configfile.md#configfile)   | `js_fetch_verify` `on` | `off`;   |
|------------------------------------------------------------------------------------------|-----------------------------------|
| Default                                                                                  | `js_fetch_verify on;`             |
| [Context](https://en.angie.software//angie/docs/configuration/configfile.md#configfile)  | stream, server                    |

Enables or disables verification of the HTTPS server certificate with [Fetch API](https://en.angie.software//angie/docs/configuration/njs-reference.md#ngx-fetch).

<a id="index-10"></a>

<a id="s-js-fetch-verify-depth"></a>

### js_fetch_verify_depth

| [Syntax](https://en.angie.software//angie/docs/configuration/configfile.md#configfile)   | `js_fetch_verify_depth` number;   |
|------------------------------------------------------------------------------------------|-----------------------------------|
| Default                                                                                  | `js_fetch_verify_depth 100;`      |
| [Context](https://en.angie.software//angie/docs/configuration/configfile.md#configfile)  | stream, server                    |

Sets the verification depth in the HTTPS server certificates chain with [Fetch API](https://en.angie.software//angie/docs/configuration/njs-reference.md#ngx-fetch).

<a id="index-11"></a>

<a id="s-js-fetch-keepalive"></a>

### js_fetch_keepalive

| [Syntax](https://en.angie.software//angie/docs/configuration/configfile.md#configfile)   | `js_fetch_keepalive` connections;   |
|------------------------------------------------------------------------------------------|-------------------------------------|
| Default                                                                                  | `js_fetch_keepalive 0;`             |
| [Context](https://en.angie.software//angie/docs/configuration/configfile.md#configfile)  | stream, server                      |

Activates the cache for connections to destination servers. When the value is greater than `0`, enables keepalive connections for [Fetch API](https://en.angie.software//angie/docs/configuration/njs-reference.md#ngx-fetch).

The connections parameter sets the maximum number of idle keepalive connections to destination servers that are preserved in the cache of each worker process. When this number is exceeded, the least recently used connections are closed.

Example:

```nginx
server {
    listen 12345;
    js_fetch_keepalive 32;
    js_fetch_trusted_certificate /path/to/ISRG_Root_X1.pem;
    js_preread main.fetch_handler;
}
```

<a id="index-12"></a>

<a id="s-js-fetch-keepalive-requests"></a>

### js_fetch_keepalive_requests

| [Syntax](https://en.angie.software//angie/docs/configuration/configfile.md#configfile)   | `js_fetch_keepalive_requests` number;   |
|------------------------------------------------------------------------------------------|-----------------------------------------|
| Default                                                                                  | `js_fetch_keepalive_requests 1000;`     |
| [Context](https://en.angie.software//angie/docs/configuration/configfile.md#configfile)  | stream, server                          |

Sets the maximum number of requests that can be served through one keepalive connection with [Fetch API](https://en.angie.software//angie/docs/configuration/njs-reference.md#ngx-fetch). After the maximum number of requests is made, the connection is closed.

Closing connections periodically is necessary to free per-connection memory allocations. Therefore, using too high maximum number of requests could result in excessive memory usage and is not recommended.

<a id="index-13"></a>

<a id="s-js-fetch-keepalive-time"></a>

### js_fetch_keepalive_time

| [Syntax](https://en.angie.software//angie/docs/configuration/configfile.md#configfile)   | `js_fetch_keepalive_time` time;   |
|------------------------------------------------------------------------------------------|-----------------------------------|
| Default                                                                                  | `js_fetch_keepalive_time 1h;`     |
| [Context](https://en.angie.software//angie/docs/configuration/configfile.md#configfile)  | stream, server                    |

Limits the maximum time during which requests can be processed through one keepalive connection with [Fetch API](https://en.angie.software//angie/docs/configuration/njs-reference.md#ngx-fetch). After this time is reached, the connection is closed following the subsequent request processing.

<a id="index-14"></a>

<a id="s-js-fetch-keepalive-timeout"></a>

### js_fetch_keepalive_timeout

| [Syntax](https://en.angie.software//angie/docs/configuration/configfile.md#configfile)   | `js_fetch_keepalive_timeout` time;   |
|------------------------------------------------------------------------------------------|--------------------------------------|
| Default                                                                                  | `js_fetch_keepalive_timeout 60s;`    |
| [Context](https://en.angie.software//angie/docs/configuration/configfile.md#configfile)  | stream, server                       |

Sets a timeout during which an idle keepalive connection to a destination server will stay open with [Fetch API](https://en.angie.software//angie/docs/configuration/njs-reference.md#ngx-fetch).

<a id="index-15"></a>

<a id="s-js-filter"></a>

### js_filter

| [Syntax](https://en.angie.software//angie/docs/configuration/configfile.md#configfile)   | `js_filter` function | module.function;   |
|------------------------------------------------------------------------------------------|-------------------------------------------|
| Default                                                                                  | —                                         |
| [Context](https://en.angie.software//angie/docs/configuration/configfile.md#configfile)  | stream, server                            |

Sets a data filter. Module functions can be referenced.

The filter function is called once at the moment when the stream session reaches the [content phase](https://en.angie.software//angie/docs/configuration/processing.md#stream-sessions). The filter function is called with the following arguments:

| `s`   | the [stream session](https://en.angie.software//angie/docs/configuration/njs-reference.md#njs-stream-session) object   |
|-------|------------------------------------------------------------------------------------------------------------------------|

At this phase, it is possible to perform initialization or register a callback with the [s.on()](https://en.angie.software//angie/docs/configuration/njs-reference.md#s-on) method for each incoming data chunk. The [s.off()](https://en.angie.software//angie/docs/configuration/njs-reference.md#s-off) method may be used to unregister a callback and stop filtering.

#### NOTE
As the js_filter handler returns its result immediately, it supports only synchronous operations. Thus, asynchronous operations such as [ngx.fetch()](https://en.angie.software//angie/docs/configuration/njs-reference.md#ngx-fetch) or [setTimeout()](https://en.angie.software//angie/docs/configuration/njs-reference.md#njs-timers) are not supported.

<a id="index-16"></a>

<a id="s-js-import"></a>

### js_import

| [Syntax](https://en.angie.software//angie/docs/configuration/configfile.md#configfile)   | `js_import` module.js | export_name from module.js;   |
|------------------------------------------------------------------------------------------|-------------------------------------------------------|
| Default                                                                                  | —                                                     |
| [Context](https://en.angie.software//angie/docs/configuration/configfile.md#configfile)  | stream, server                                        |

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.

```nginx
js_import stream.js;
```

Here, the module name stream is used as a namespace when accessing exports. If the imported module exports foo(), then stream.foo is used to access it.

Several js_import directives can be specified.

<a id="index-17"></a>

<a id="s-js-path"></a>

### js_path

| [Syntax](https://en.angie.software//angie/docs/configuration/configfile.md#configfile)   | `js_path` path;   |
|------------------------------------------------------------------------------------------|-------------------|
| Default                                                                                  | —                 |
| [Context](https://en.angie.software//angie/docs/configuration/configfile.md#configfile)  | stream, server    |

Sets an additional path for njs modules.

<a id="index-18"></a>

<a id="s-js-periodic"></a>

### js_periodic

| [Syntax](https://en.angie.software//angie/docs/configuration/configfile.md#configfile)   | `js_periodic` module.function [`interval=`\\ time] [`jitter=`\\ number] [`worker_affinity=`\\ mask];   |
|------------------------------------------------------------------------------------------|--------------------------------------------------------------------------------------------------------|
| Default                                                                                  | —                                                                                                      |
| [Context](https://en.angie.software//angie/docs/configuration/configfile.md#configfile)  | server                                                                                                 |

Specifies a content handler to run at regular intervals. The handler receives a session object as its first argument, it also has access to global objects such as `ngx`.

The optional `interval` parameter sets the interval between two consecutive runs, by default, 5 seconds.

The optional `jitter` parameter sets the time within which the location content handler will be randomly delayed, by default, there is no delay.

By default, the `js_handler` is executed on worker process 0. The optional `worker_affinity` parameter allows specifying particular worker processes where the location content handler should be executed. Each worker process set is represented by a bitmask of allowed worker processes. The `all` mask allows the handler to be executed in all worker processes.

Example:

```nginx
example.conf:

location @periodics {
    # to be run at 1 minute intervals in worker process 0
    js_periodic main.handler interval=60s;

    # to be run at 1 minute intervals in all worker processes
    js_periodic main.handler interval=60s worker_affinity=all;

    # to be run at 1 minute intervals in worker processes 1 and 3
    js_periodic main.handler interval=60s worker_affinity=0101;

    resolver 10.0.0.1;
    js_fetch_trusted_certificate /path/to/ISRG_Root_X1.pem;
}
```

```javascript
example.js:

async function handler(s) {
    let reply = await ngx.fetch('https://example.com/');
    let body = await reply.text();

    ngx.log(ngx.INFO, body);
}
```

<a id="index-19"></a>

<a id="s-js-preload-object"></a>

### js_preload_object

| [Syntax](https://en.angie.software//angie/docs/configuration/configfile.md#configfile)   | `js_preload_object` name.json | name from file.json;   |
|------------------------------------------------------------------------------------------|--------------------------------------------------------|
| Default                                                                                  | —                                                      |
| [Context](https://en.angie.software//angie/docs/configuration/configfile.md#configfile)  | stream, server                                         |

Preloads an immutable object at configure time. The name is used as a name of the global variable though which the object is available in njs code. If the name is not specified, the file name will be used instead.

```nginx
js_preload_object map.json;
```

Here, the map is used as a name while accessing the preloaded object.

Several js_preload_object directives can be specified.

<a id="index-20"></a>

<a id="s-js-preread"></a>

### js_preread

| [Syntax](https://en.angie.software//angie/docs/configuration/configfile.md#configfile)   | `js_preread` function | module.function;   |
|------------------------------------------------------------------------------------------|--------------------------------------------|
| Default                                                                                  | —                                          |
| [Context](https://en.angie.software//angie/docs/configuration/configfile.md#configfile)  | stream, server                             |

Sets an njs function which will be called at the [preread phase](https://en.angie.software//angie/docs/configuration/processing.md#stream-sessions). Module functions can be referenced.

The function is called once at the moment when the stream session reaches the [preread phase](https://en.angie.software//angie/docs/configuration/processing.md#stream-sessions) for the first time. The function is called with the following arguments:

| `s`   | the [stream session](https://en.angie.software//angie/docs/configuration/njs-reference.md#njs-stream-session) object   |
|-------|------------------------------------------------------------------------------------------------------------------------|

At this phase, it is possible to perform initialization or register a callback with the [s.on()](https://en.angie.software//angie/docs/configuration/njs-reference.md#s-on) method for each incoming data chunk until one of the following methods are called: [s.done()](https://en.angie.software//angie/docs/configuration/njs-reference.md#njs-s-done), [s.decline()](https://en.angie.software//angie/docs/configuration/njs-reference.md#njs-s-decline), [s.allow()](https://en.angie.software//angie/docs/configuration/njs-reference.md#njs-s-allow). When one of these methods is called, the stream session switches to the [next phase](https://en.angie.software//angie/docs/configuration/processing.md#stream-sessions) and all current [s.on()](https://en.angie.software//angie/docs/configuration/njs-reference.md#s-on) callbacks are dropped.

#### NOTE
As the js_preread handler returns its result immediately, it supports only synchronous operations. Thus, asynchronous operations such as [ngx.fetch()](https://en.angie.software//angie/docs/configuration/njs-reference.md#ngx-fetch) or [setTimeout()](https://en.angie.software//angie/docs/configuration/njs-reference.md#njs-timers) are not supported. Nevertheless, asynchronous operations are supported in [s.on()](https://en.angie.software//angie/docs/configuration/njs-reference.md#s-on) callbacks in the [preread phase](https://en.angie.software//angie/docs/configuration/processing.md#stream-sessions).

<a id="index-21"></a>

<a id="s-js-set"></a>

### js_set

| [Syntax](https://en.angie.software//angie/docs/configuration/configfile.md#configfile)   | `js_set` $variable function | module.function [`nocache`];   |
|------------------------------------------------------------------------------------------|--------------------------------------------------------------|
| Default                                                                                  | —                                                            |
| [Context](https://en.angie.software//angie/docs/configuration/configfile.md#configfile)  | stream, server                                               |

Sets an njs function for the specified variable. Module functions can be referenced.

The function is called when the variable is referenced for the first time for a given request. The exact moment depends on a [phase](https://en.angie.software//angie/docs/configuration/processing.md#stream-sessions) at which the variable is referenced. This can be used to perform some logic not related to variable evaluation. For example, if the variable is referenced only in the [log_format](https://en.angie.software//angie/docs/configuration/modules/stream/stream_log.md#s-log-format) directive, its handler will not be executed until the log phase. This handler can be used to do some cleanup right before the request is freed.

Since njs 0.8.6, when optional argument `nocache` is provided the handler is called every time it is referenced. Due to current limitations of the rewrite module, when a `nocache` variable is referenced by the set directive its handler should always return a fixed-length value.

#### NOTE
As the js_set handler returns its result immediately, it supports only synchronous operations. Thus, asynchronous operations such as [ngx.fetch()](https://en.angie.software//angie/docs/configuration/njs-reference.md#ngx-fetch) or [setTimeout()](https://en.angie.software//angie/docs/configuration/njs-reference.md#njs-timers) are not supported.

<a id="index-22"></a>

<a id="s-js-shared-dict-zone"></a>

### js_shared_dict_zone

| [Syntax](https://en.angie.software//angie/docs/configuration/configfile.md#configfile)   | `js_shared_dict_zone` `zone=`name:size [`timeout=`time] [`type=``string` | `number`] [`evict`] [`state=`file];   |
|------------------------------------------------------------------------------------------|------------------------------------------------------------------------------------------------------------------|
| Default                                                                                  | —                                                                                                                |
| [Context](https://en.angie.software//angie/docs/configuration/configfile.md#configfile)  | stream                                                                                                           |

Sets the name and size of the shared memory zone that keeps the key-value dictionary shared between worker processes.

| `type`    | optional parameter, allows redefining the value type to `number`, by default the shared dictionary uses a `string` as a key and a value   |
|-----------|-------------------------------------------------------------------------------------------------------------------------------------------|
| `timeout` | optional parameter, sets the time after which all shared dictionary entries are removed from the zone                                     |
| `evict`   | optional parameter, removes the oldest key-value pair when the zone storage is exhausted                                                  |
| `state`   | optional parameter, specifies a file that keeps the shared dictionary state in JSON format and makes it persistent across nginx restarts  |

Examples:

```nginx
example.conf:
    # Creates a 1Mb dictionary with string values,
    # removes key-value pairs after 60 seconds of inactivity:
    js_shared_dict_zone zone=foo:1M timeout=60s;

    # Creates a 512Kb dictionary with string values,
    # forcibly removes oldest key-value pairs when the zone is exhausted:
    js_shared_dict_zone zone=bar:512K timeout=30s evict;

    # Creates a 32Kb permanent dictionary with number values:
    js_shared_dict_zone zone=num:32k type=number;

    # Creates a 1Mb dictionary with string values and persistent state:
    js_shared_dict_zone zone=persistent:1M state=/tmp/dict.json;
```

```javascript
example.js:
    function get(r) {
        r.return(200, ngx.shared.foo.get(r.args.key));
    }

    function set(r) {
        r.return(200, ngx.shared.foo.set(r.args.key, r.args.value));
    }

    function delete(r) {
        r.return(200, ngx.shared.bar.delete(r.args.key));
    }

    function increment(r) {
        r.return(200, ngx.shared.num.incr(r.args.key, 2));
    }
```

<a id="index-23"></a>

<a id="s-js-var"></a>

### js_var

| [Syntax](https://en.angie.software//angie/docs/configuration/configfile.md#configfile)   | `js_var` $variable [value];   |
|------------------------------------------------------------------------------------------|-------------------------------|
| Default                                                                                  | —                             |
| [Context](https://en.angie.software//angie/docs/configuration/configfile.md#configfile)  | stream, server                |

Declares a [writable](https://en.angie.software//angie/docs/configuration/njs-reference.md#s-variables) variable. The value can contain text, variables, and their combination.

<a id="session-object-properties"></a>

## Session Object Properties

Each stream njs handler receives one argument, a [stream session](https://en.angie.software//angie/docs/configuration/njs-reference.md#njs-stream-session) object.
