Path: blob/main/xml/en/docs/stream/ngx_stream_js_module.xml
1 views
<?xml version="1.0"?>12<!--3Copyright (C) Nginx, Inc.4-->56<!DOCTYPE module SYSTEM "../../../../dtd/module.dtd">78<module name="Module ngx_stream_js_module"9link="/en/docs/stream/ngx_stream_js_module.html"10lang="en"11rev="56">1213<section id="summary">1415<para>16The <literal>ngx_stream_js_module</literal> module is used to implement17handlers in <link doc="../njs/index.xml">njs</link> —18a subset of the JavaScript language.19</para>2021<para>22Download and install instructions are available23<link doc="../njs/install.xml">here</link>.24</para>2526</section>272829<section id="example" name="Example Configuration">3031<para>32The example works since33<link doc="../njs/changes.xml" id="njs0.4.0">0.4.0</link>.34<example>35stream {36# since 0.9.137js_engine qjs;3839js_import stream.js;4041js_set $bar stream.bar;42js_set $req_line stream.req_line;4344server {45listen 12345;4647js_preread stream.preread;48return $req_line;49}5051server {52listen 12346;5354js_access stream.access;55proxy_pass 127.0.0.1:8000;56js_filter stream.header_inject;57}58}5960http {61server {62listen 8000;63location / {64return 200 $http_foo\n;65}66}67}68</example>69</para>7071<para>72The <path>stream.js</path> file:73<example>74var line = '';7576function bar(s) {77var v = s.variables;78s.log("hello from bar() handler!");79return "bar-var" + v.remote_port + "; pid=" + v.pid;80}8182function preread(s) {83s.on('upload', function (data, flags) {84var n = data.indexOf('\n');85if (n != -1) {86line = data.substr(0, n);87s.done();88}89});90}9192function req_line(s) {93return line;94}9596// Read HTTP request line.97// Collect bytes in 'req' until98// request line is read.99// Injects HTTP header into a client's request100101var my_header = 'Foo: foo';102function header_inject(s) {103var req = '';104s.on('upload', function(data, flags) {105req += data;106var n = req.search('\n');107if (n != -1) {108var rest = req.substr(n + 1);109req = req.substr(0, n + 1);110s.send(req + my_header + '\r\n' + rest, flags);111s.off('upload');112}113});114}115116function access(s) {117if (s.remoteAddress.match('^192.*')) {118s.deny();119return;120}121122s.allow();123}124125export default {bar, preread, req_line, header_inject, access};126</example>127</para>128129</section>130131132<section id="directives" name="Directives">133134<directive name="js_access">135<syntax><value>module.function</value></syntax>136<default/>137<context>stream</context>138<context>server</context>139140<para>141Sets an njs function which will be called at the142<link doc="stream_processing.xml" id="access_phase">access</link> phase.143Since <link doc="../njs/changes.xml" id="njs0.4.0">0.4.0</link>,144a module function can be referenced.145</para>146147<para>148The function is called once at the moment when the stream session reaches149the <link doc="stream_processing.xml" id="access_phase">access</link> phase150for the first time.151The function is called with the following arguments:152153<list type="tag">154<tag-name><literal>s</literal></tag-name>155<tag-desc>156the <link doc="../njs/reference.xml" id="stream">Stream Session</link> object157</tag-desc>158159</list>160</para>161162<para>163At this phase, it is possible to perform initialization164or register a callback with165the <link doc="../njs/reference.xml" id="s_on"><literal>s.on()</literal></link>166method167for each incoming data chunk until one of the following methods are called:168<link doc="../njs/reference.xml" id="s_allow"><literal>s.allow()</literal></link>,169<link doc="../njs/reference.xml" id="s_decline"><literal>s.decline()</literal></link>,170<link doc="../njs/reference.xml" id="s_done"><literal>s.done()</literal></link>.171As soon as one of these methods is called, the stream session processing172switches to the <link doc="stream_processing.xml">next phase</link>173and all current174<link doc="../njs/reference.xml" id="s_on"><literal>s.on()</literal></link>175callbacks are dropped.176</para>177178</directive>179180181<directive name="js_context_reuse">182<syntax><value>number</value></syntax>183<default>128</default>184<context>stream</context>185<context>server</context>186<appeared-in>0.8.6</appeared-in>187188<para>189Sets a maximum number of JS context to be reused for190<link doc="../njs/engine.xml">QuickJS engine</link>.191Each context is used for a single stream session.192The finished context is put into a pool of reusable contexts.193If the pool is full, the context is destroyed.194</para>195196</directive>197198199<directive name="js_engine">200<syntax><literal>njs</literal> | <literal>qjs</literal></syntax>201<default>njs</default>202<context>stream</context>203<context>server</context>204<appeared-in>0.8.6</appeared-in>205206<para>207Sets a <link doc="../njs/engine.xml">JavaScript engine</link>208to be used for njs scripts.209The <literal>njs</literal> parameter sets the njs engine, also used by default.210The <literal>qjs</literal> parameter sets the QuickJS engine.211</para>212213<para>214<note>215The <literal>njs</literal> engine is deprecated216since <link doc="../njs/changes.xml" id="njs1.0.0">1.0.0</link>;217new configurations should use the <literal>qjs</literal>218(<link doc="../njs/engine.xml" id="quickjs_engine">QuickJS</link>) engine.219</note>220</para>221222</directive>223224225<directive name="js_fetch_buffer_size">226<syntax><value>size</value></syntax>227<default>16k</default>228<context>stream</context>229<context>server</context>230<appeared-in>0.7.4</appeared-in>231232<para>233Sets the <value>size</value> of the buffer used for reading and writing234with <link doc="../njs/reference.xml" id="ngx_fetch">Fetch API</link>.235</para>236237</directive>238239240<directive name="js_fetch_ciphers">241<syntax><value>ciphers</value></syntax>242<default>HIGH:!aNULL:!MD5</default>243<context>stream</context>244<context>server</context>245<appeared-in>0.7.0</appeared-in>246247<para>248Specifies the enabled ciphers for HTTPS connections249with <link doc="../njs/reference.xml" id="ngx_fetch">Fetch API</link>.250The ciphers are specified in the format understood by the OpenSSL library.251</para>252253<para>254The full list can be viewed using the255“<command>openssl ciphers</command>” command.256</para>257258</directive>259260261<directive name="js_fetch_max_response_buffer_size">262<syntax><value>size</value></syntax>263<default>1m</default>264<context>stream</context>265<context>server</context>266<appeared-in>0.7.4</appeared-in>267268<para>269Sets the maximum <value>size</value> of the response received270with <link doc="../njs/reference.xml" id="ngx_fetch">Fetch API</link>.271</para>272273</directive>274275276<directive name="js_fetch_protocols">277<syntax>278[<literal>TLSv1</literal>]279[<literal>TLSv1.1</literal>]280[<literal>TLSv1.2</literal>]281[<literal>TLSv1.3</literal>]</syntax>282<default>TLSv1 TLSv1.1 TLSv1.2</default>283<context>stream</context>284<context>server</context>285<appeared-in>0.7.0</appeared-in>286287<para>288Enables the specified protocols for HTTPS connections289with <link doc="../njs/reference.xml" id="ngx_fetch">Fetch API</link>.290</para>291292</directive>293294295<directive name="js_fetch_timeout">296<syntax><value>time</value></syntax>297<default>60s</default>298<context>stream</context>299<context>server</context>300<appeared-in>0.7.4</appeared-in>301302<para>303Defines a timeout for reading and writing304for <link doc="../njs/reference.xml" id="ngx_fetch">Fetch API</link>.305The timeout is set only between two successive read/write operations,306not for the whole response.307If no data is transmitted within this time, the connection is closed.308</para>309310</directive>311312313<directive name="js_fetch_trusted_certificate">314<syntax><value>file</value></syntax>315<default/>316<context>stream</context>317<context>server</context>318<appeared-in>0.7.0</appeared-in>319320<para>321Specifies a <value>file</value> with trusted CA certificates in the PEM format322used to323<link doc="../njs/reference.xml" id="fetch_verify">verify</link>324the HTTPS certificate325with <link doc="../njs/reference.xml" id="ngx_fetch">Fetch API</link>.326</para>327328</directive>329330331<directive name="js_fetch_verify">332<syntax><literal>on</literal> | <literal>off</literal></syntax>333<default>on</default>334<context>stream</context>335<context>server</context>336<appeared-in>0.7.4</appeared-in>337338<para>339Enables or disables verification of the HTTPS server certificate340with <link doc="../njs/reference.xml" id="ngx_fetch">Fetch API</link>.341</para>342343</directive>344345346<directive name="js_fetch_verify_depth">347<syntax><value>number</value></syntax>348<default>100</default>349<context>stream</context>350<context>server</context>351<appeared-in>0.7.0</appeared-in>352353<para>354Sets the verification depth in the HTTPS server certificates chain355with <link doc="../njs/reference.xml" id="ngx_fetch">Fetch API</link>.356</para>357358</directive>359360361<directive name="js_fetch_proxy">362<syntax><value>url</value></syntax>363<default/>364<context>stream</context>365<context>server</context>366<appeared-in>0.9.4</appeared-in>367368<para>369Configures a forward proxy URL370with <link doc="../njs/reference.xml" id="ngx_fetch">Fetch API</link>.371The <value>url</value> supports the HTTP scheme only372and can contain optional user credentials373in the format <literal>http://[user:password@]host:port</literal>374for Basic authentication.375Supports both HTTP and HTTPS connections to destination servers.376If the <value>url</value> is empty, proxy routing is disabled.377The parameter value can contain variables.378</para>379380<para>381Example:382<example>383server {384listen 12345;385js_fetch_proxy http://user:[email protected]:3128;386js_preread main.fetch_handler;387}388</example>389</para>390391</directive>392393394<directive name="js_fetch_keepalive">395<syntax><value>connections</value></syntax>396<default>0</default>397<context>stream</context>398<context>server</context>399<appeared-in>0.9.2</appeared-in>400401<para>402Activates the cache for connections to destination servers.403When the value is greater than <literal>0</literal>,404enables keepalive connections for405<link doc="../njs/reference.xml" id="ngx_fetch">Fetch API</link>.406</para>407408<para>409The <value>connections</value> parameter sets the maximum number of idle410keepalive connections to destination servers that are preserved in the cache411of each worker process.412When this number is exceeded, the least recently used connections are closed.413</para>414415<para>416In Stream, the cache is maintained separately for each server configuration.417A value set at the <literal>stream</literal> level is inherited by servers,418but each server uses its own cache.419Cached connections are reused for requests with the same protocol, host,420and port.421</para>422423<para>424When enabled, keepalive assumes that destination servers send valid HTTP425responses.426</para>427428<para>429Example:430<example>431server {432listen 12345;433js_fetch_keepalive 32;434js_fetch_trusted_certificate /path/to/ISRG_Root_X1.pem;435js_preread main.fetch_handler;436}437</example>438</para>439440</directive>441442443<directive name="js_fetch_keepalive_requests">444<syntax><value>number</value></syntax>445<default>1000</default>446<context>stream</context>447<context>server</context>448<appeared-in>0.9.2</appeared-in>449450<para>451Sets the maximum number of requests that can be served through one keepalive452connection with <link doc="../njs/reference.xml" id="ngx_fetch">Fetch API</link>.453After the maximum number of requests is made, the connection is closed.454</para>455456<para>457Closing connections periodically is necessary to free per-connection memory458allocations.459Therefore, using too high maximum number of requests could result in460excessive memory usage and not recommended.461</para>462463</directive>464465466<directive name="js_fetch_keepalive_time">467<syntax><value>time</value></syntax>468<default>1h</default>469<context>stream</context>470<context>server</context>471<appeared-in>0.9.2</appeared-in>472473<para>474Limits the maximum time during which requests can be processed through one475keepalive connection with <link doc="../njs/reference.xml" id="ngx_fetch">Fetch API</link>.476After this time is reached, the connection is closed following the subsequent477request processing.478</para>479480</directive>481482483<directive name="js_fetch_keepalive_timeout">484<syntax><value>time</value></syntax>485<default>60s</default>486<context>stream</context>487<context>server</context>488<appeared-in>0.9.2</appeared-in>489490<para>491Sets a timeout during which an idle keepalive connection to a destination server492will stay open with <link doc="../njs/reference.xml" id="ngx_fetch">Fetch API</link>.493</para>494495</directive>496497498<directive name="js_filter">499<syntax><value>module.function</value></syntax>500<default/>501<context>stream</context>502<context>server</context>503504<para>505Sets a data filter.506Since <link doc="../njs/changes.xml" id="njs0.4.0">0.4.0</link>,507a module function can be referenced.508The filter function is called once at the moment when the stream session reaches509the <link doc="stream_processing.xml" id="content_phase">content</link> phase.510</para>511512<para>513The filter function is called with the following arguments:514<list type="tag">515<tag-name><literal>s</literal></tag-name>516<tag-desc>517the <link doc="../njs/reference.xml" id="stream">Stream Session</link> object518</tag-desc>519520</list>521</para>522523<para>524At this phase, it is possible to perform initialization525or register a callback with526the <link doc="../njs/reference.xml" id="s_on"><literal>s.on()</literal></link>527method for each incoming data chunk.528The529<link doc="../njs/reference.xml" id="s_off"><literal>s.off()</literal></link>530method may be used to unregister a callback and stop filtering.531</para>532533<para>534<note>535As the <literal>js_filter</literal> handler536returns its result immediately, it supports537only synchronous operations.538Thus, asynchronous operations such as539<link doc="../njs/reference.xml" id="ngx_fetch"><literal>ngx.fetch()</literal></link>540or541<link doc="../njs/reference.xml" id="settimeout"><literal>setTimeout()</literal></link>542are not supported.543</note>544</para>545546</directive>547548549<directive name="js_import">550<syntax><value>module.js</value> |551<value>export_name from module.js</value></syntax>552<default/>553<context>stream</context>554<context>server</context>555<appeared-in>0.4.0</appeared-in>556557<para>558Imports a module that implements location and variable handlers in njs.559The <literal>export_name</literal> is used as a namespace560to access module functions.561If the <literal>export_name</literal> is not specified,562the module name will be used as a namespace.563<example>564js_import stream.js;565</example>566Here, the module name <literal>stream</literal> is used as a namespace567while accessing exports.568If the imported module exports <literal>foo()</literal>,569<literal>stream.foo</literal> is used to refer to it.570</para>571572<para>573Several <literal>js_import</literal> directives can be specified.574</para>575576<para>577<note>578The directive can be specified on the579<literal>server</literal> level580since <link doc="../njs/changes.xml" id="njs0.7.7">0.7.7</link>.581</note>582</para>583584</directive>585586587<directive name="js_include">588<syntax><value>file</value></syntax>589<default/>590<context>stream</context>591592<para>593Specifies a file that implements server and variable handlers in njs:594<example>595nginx.conf:596js_include stream.js;597js_set $js_addr address;598server {599listen 127.0.0.1:12345;600return $js_addr;601}602603stream.js:604function address(s) {605return s.remoteAddress;606}607</example>608</para>609610<para>611The directive was made obsolete in version612<link doc="../njs/changes.xml" id="njs0.4.0">0.4.0</link>613and was removed in version614<link doc="../njs/changes.xml" id="njs0.7.1">0.7.1</link>.615The <link id="js_import"/> directive should be used instead.616</para>617618</directive>619620621<directive name="js_load_stream_native_module">622<syntax><value>path</value> [<literal>as</literal> <value>name</value>]</syntax>623<default/>624<context>main</context>625<appeared-in>0.9.5</appeared-in>626627<para>628Loads a <link doc="../njs/native_modules.xml">native module</link>629(shared library) for use in Stream JavaScript code.630The directive is631<link doc="../njs/engine.xml" id="quickjs_engine">QuickJS</link>-only632and is not available when using the njs built-in JavaScript engine.633</para>634635<para>636The <value>path</value> parameter637specifies the absolute path to the shared library file.638The optional <literal>as</literal> <value>name</value> parameter639provides an alias name for importing the module in JavaScript code.640If not specified, the module can be imported using its filename.641</para>642643<para>644Example:645<example>646js_load_stream_native_module /path/to/mylib.so;647js_load_stream_native_module /path/to/other.so as myalias;648649stream {650js_import main.js;651# ... rest of stream configuration652}653</example>654In JavaScript code:655<example>656// Import by filename657import * as mylib from 'mylib.so';658659// Import by alias660import * as myalias from 'myalias';661662// Use exported functions663let result = mylib.add(5, 10);664</example>665</para>666667<para>668<note>669For security reasons, this directive is only allowed670in the <literal>main</literal> configuration context.671Native modules run with full process privileges;672use absolute paths and ensure proper code review.673</note>674</para>675676</directive>677678679<directive name="js_path">680<syntax>681<value>path</value></syntax>682<default/>683<context>stream</context>684<context>server</context>685<appeared-in>0.3.0</appeared-in>686687<para>688Sets an additional path for njs modules.689</para>690691<para>692<note>693The directive can be specified on the694<literal>server</literal> level695since <link doc="../njs/changes.xml" id="njs0.7.7">0.7.7</link>.696</note>697</para>698699</directive>700701702<directive name="js_periodic">703<syntax><value>module.function</value>704[<literal>interval</literal>=<value>time</value>]705[<literal>jitter</literal>=<value>number</value>]706[<literal>worker_affinity</literal>=<value>mask</value>]</syntax>707<default/>708<context>server</context>709<appeared-in>0.8.1</appeared-in>710711<para>712Specifies a content handler to run at regular interval.713The handler receives a714<link doc="../njs/reference.xml" id="periodic_session">session object</link>715as its first argument,716it also has access to global objects such as717<link doc="../njs/reference.xml" id="ngx">ngx</link>.718</para>719720<para>721The optional <literal>interval</literal> parameter722sets the interval between two consecutive runs,723by default, 5 seconds.724</para>725726<para>727The optional <literal>jitter</literal> parameter sets the time within which728the location content handler will be randomly delayed,729by default, there is no delay.730</para>731732<para>733By default, the <literal>js_handler</literal> is executed on worker process 0.734The optional <literal>worker_affinity</literal> parameter735allows specifying particular worker processes736where the location content handler should be executed.737Each worker process set is represented by a bitmask of allowed worker processes.738The <literal>all</literal> mask allows the handler to be executed739in all worker processes.740</para>741742<para>743Example:744<example>745example.conf:746747location @periodics {748# to be run at 1 minute intervals in worker process 0749js_periodic main.handler interval=60s;750751# to be run at 1 minute intervals in all worker processes752js_periodic main.handler interval=60s worker_affinity=all;753754# to be run at 1 minute intervals in worker processes 1 and 3755js_periodic main.handler interval=60s worker_affinity=0101;756757resolver 10.0.0.1;758js_fetch_trusted_certificate /path/to/ISRG_Root_X1.pem;759}760761example.js:762763async function handler(s) {764let reply = await ngx.fetch('https://nginx.org/en/docs/njs/');765let body = await reply.text();766767ngx.log(ngx.INFO, body);768}769</example>770</para>771772</directive>773774775<directive name="js_preload_object">776<syntax><value>name.json</value> |777<value>name</value> from <value>file.json</value></syntax>778<default/>779<context>stream</context>780<context>server</context>781<appeared-in>0.7.8</appeared-in>782783<para>784Preloads an785<link doc="../njs/preload_objects.xml">immutable object</link>786at configure time.787The <literal>name</literal> is used as a name of the global variable788though which the object is available in njs code.789If the <literal>name</literal> is not specified,790the file name will be used instead.791<example>792js_preload_object map.json;793</example>794Here, the <literal>map</literal> is used as a name795while accessing the preloaded object.796</para>797798<para>799Several <literal>js_preload_object</literal> directives can be specified.800</para>801802</directive>803804805<directive name="js_preread">806<syntax><value>module.function</value></syntax>807<default/>808<context>stream</context>809<context>server</context>810811<para>812Sets an njs function which will be called at the813<link doc="stream_processing.xml" id="preread_phase">preread</link> phase.814Since <link doc="../njs/changes.xml" id="njs0.4.0">0.4.0</link>,815a module function can be referenced.816</para>817818<para>819The function is called once820at the moment when the stream session reaches the821<link doc="stream_processing.xml" id="preread_phase">preread</link> phase822for the first time.823The function is called with the following arguments:824825<list type="tag">826<tag-name><literal>s</literal></tag-name>827<tag-desc>828the <link doc="../njs/reference.xml" id="stream">Stream Session</link> object829</tag-desc>830831</list>832</para>833834<para>835At this phase, it is possible to perform initialization836or register a callback with837the <link doc="../njs/reference.xml" id="s_on"><literal>s.on()</literal></link>838method839for each incoming data chunk until one of the following methods are called:840<link doc="../njs/reference.xml" id="s_allow"><literal>s.allow()</literal></link>,841<link doc="../njs/reference.xml" id="s_decline"><literal>s.decline()</literal></link>,842<link doc="../njs/reference.xml" id="s_done"><literal>s.done()</literal></link>.843When one of these methods is called,844the stream session switches to the845<link doc="stream_processing.xml">next phase</link>846and all current847<link doc="../njs/reference.xml" id="s_on"><literal>s.on()</literal></link>848callbacks are dropped.849</para>850851<para>852<note>853As the <literal>js_preread</literal> handler854returns its result immediately, it supports855only synchronous callbacks.856Thus, asynchronous callbacks such as857<link doc="../njs/reference.xml" id="ngx_fetch"><literal>ngx.fetch()</literal></link>858or859<link doc="../njs/reference.xml" id="settimeout"><literal>setTimeout()</literal></link>860are not supported.861Nevertheless, asynchronous operations are supported in862<link doc="../njs/reference.xml" id="s_on"><literal>s.on()</literal></link>863callbacks in the864<link doc="stream_processing.xml" id="preread_phase">preread</link> phase.865See866<link url="https://github.com/nginx/njs-examples#authorizing-connections-using-ngx-fetch-as-auth-request-stream-auth-request">this example</link> for more information.867</note>868</para>869870</directive>871872873<directive name="js_set">874<syntax>875<value>$variable</value>876<value>module.function</value>877[<literal>nocache</literal>]</syntax>878<default/>879<context>stream</context>880<context>server</context>881882<para>883Sets an njs <literal>function</literal>884for the specified <literal>variable</literal>.885Since <link doc="../njs/changes.xml" id="njs0.4.0">0.4.0</link>,886a module function can be referenced.887</para>888889<para>890The function is called when891the variable is referenced for the first time for a given request.892The exact moment depends on a893<link doc="stream_processing.xml">phase</link>894at which the variable is referenced.895This can be used to perform some logic896not related to variable evaluation.897For example, if the variable is referenced only in the898<link doc="ngx_stream_log_module.xml" id="log_format"/> directive,899its handler will not be executed until the log phase.900This handler can be used to do some cleanup901right before the request is freed.902</para>903904<para>905Since <link doc="../njs/changes.xml" id="njs0.8.6">0.8.6</link>, when906optional argument <literal>nocache</literal> is provided the handler907is called every time it is referenced.908Due to current limitations909of the <link doc="ngx_stream_rewrite_module.xml">rewrite</link> module,910when a <literal>nocache</literal> variable is referenced by the911<link doc="ngx_stream_set_module.xml" id="set">set</link> directive912its handler should always return a fixed-length value.913</para>914915<para>916<note>917As the <literal>js_set</literal> handler918returns its result immediately, it supports919only synchronous callbacks.920Thus, asynchronous callbacks such as921<link doc="../njs/reference.xml" id="ngx_fetch">ngx.fetch()</link>922or923<link doc="../njs/reference.xml" id="settimeout">setTimeout()</link>924are not supported.925</note>926</para>927928<para>929<note>930The directive can be specified on the931<literal>server</literal> level932since <link doc="../njs/changes.xml" id="njs0.7.7">0.7.7</link>.933</note>934</para>935936</directive>937938939<directive name="js_shared_dict_zone">940<syntax>941<literal>zone</literal>=<value>name</value>:<value>size</value>942[<literal>timeout</literal>=<value>time</value>]943[<literal>type</literal>=<literal>string</literal>|<literal>number</literal>]944[<literal>evict</literal>]945[<literal>state</literal>=<value>file</value>]</syntax>946<default/>947<context>stream</context>948<appeared-in>0.8.0</appeared-in>949950<para>951Sets the <value>name</value> and <value>size</value> of the shared memory zone952that keeps the953key-value <link doc="../njs/reference.xml" id="dict">dictionary</link>954shared between worker processes.955</para>956957<para>958By default the shared dictionary uses a string as a key and a value.959The optional <literal>type</literal> parameter960allows redefining the value type to number.961</para>962963<para>964The optional <literal>timeout</literal> parameter sets965the time in milliseconds966after which all shared dictionary entries are removed from the zone.967If some entries require a different removal time, it can be set968with the <literal>timeout</literal> argument of the969<link doc="../njs/reference.xml" id="dict_add">add</link>,970<link doc="../njs/reference.xml" id="dict_incr">incr</link>, and971<link doc="../njs/reference.xml" id="dict_set">set</link>972methods973(<link doc="../njs/changes.xml" id="njs0.8.5">0.8.5</link>).974</para>975976<para>977The optional <literal>evict</literal> parameter removes the oldest978key-value pair when the zone storage is exhausted.979</para>980981<para>982The optional <literal>state</literal> parameter specifies a <value>file</value>983that keeps the shared dictionary state984in JSON format and makes it persistent across nginx restarts985(<link doc="../njs/changes.xml" id="njs0.9.1">0.9.1</link>).986</para>987988<para>989Example:990<example>991example.conf:992# Creates a 1Mb dictionary with string values,993# removes key-value pairs after 60 seconds of inactivity:994js_shared_dict_zone zone=foo:1M timeout=60s;995996# Creates a 512Kb dictionary with string values,997# forcibly removes oldest key-value pairs when the zone is exhausted:998js_shared_dict_zone zone=bar:512K timeout=30s evict;9991000# Creates a 32Kb permanent dictionary with number values:1001js_shared_dict_zone zone=num:32k type=number;10021003# Creates a 1Mb dictionary with string values and persistent state:1004js_shared_dict_zone zone=persistent:1M state=/tmp/dict.json;10051006example.js:1007function get(r) {1008r.return(200, ngx.shared.foo.get(r.args.key));1009}10101011function set(r) {1012r.return(200, ngx.shared.foo.set(r.args.key, r.args.value));1013}10141015function del(r) {1016r.return(200, ngx.shared.bar.delete(r.args.key));1017}10181019function increment(r) {1020r.return(200, ngx.shared.num.incr(r.args.key, 2));1021}1022</example>1023</para>10241025</directive>102610271028<directive name="js_var">1029<syntax><value>$variable</value> [<value>value</value>]</syntax>1030<default/>1031<context>stream</context>1032<context>server</context>1033<appeared-in>0.5.3</appeared-in>10341035<para>1036Declares1037a <link doc="../njs/reference.xml" id="r_variables">writable</link>1038variable.1039The value can contain text, variables, and their combination.1040</para>10411042<para>1043<note>1044The directive can be specified on the1045<literal>server</literal> level1046since <link doc="../njs/changes.xml" id="njs0.7.7">0.7.7</link>.1047</note>1048</para>10491050</directive>10511052</section>105310541055<section id="properties" name="Session Object Properties">10561057<para>1058Each stream njs handler receives one argument, a stream session1059<link doc="../njs/reference.xml" id="stream">object</link>.1060</para>10611062</section>10631064</module>106510661067