Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
nginx
GitHub Repository: nginx/nginx.org
Path: blob/main/xml/en/docs/stream/ngx_stream_js_module.xml
1 views
1
<?xml version="1.0"?>
2
3
<!--
4
Copyright (C) Nginx, Inc.
5
-->
6
7
<!DOCTYPE module SYSTEM "../../../../dtd/module.dtd">
8
9
<module name="Module ngx_stream_js_module"
10
link="/en/docs/stream/ngx_stream_js_module.html"
11
lang="en"
12
rev="56">
13
14
<section id="summary">
15
16
<para>
17
The <literal>ngx_stream_js_module</literal> module is used to implement
18
handlers in <link doc="../njs/index.xml">njs</link> 
19
a subset of the JavaScript language.
20
</para>
21
22
<para>
23
Download and install instructions are available
24
<link doc="../njs/install.xml">here</link>.
25
</para>
26
27
</section>
28
29
30
<section id="example" name="Example Configuration">
31
32
<para>
33
The example works since
34
<link doc="../njs/changes.xml" id="njs0.4.0">0.4.0</link>.
35
<example>
36
stream {
37
# since 0.9.1
38
js_engine qjs;
39
40
js_import stream.js;
41
42
js_set $bar stream.bar;
43
js_set $req_line stream.req_line;
44
45
server {
46
listen 12345;
47
48
js_preread stream.preread;
49
return $req_line;
50
}
51
52
server {
53
listen 12346;
54
55
js_access stream.access;
56
proxy_pass 127.0.0.1:8000;
57
js_filter stream.header_inject;
58
}
59
}
60
61
http {
62
server {
63
listen 8000;
64
location / {
65
return 200 $http_foo\n;
66
}
67
}
68
}
69
</example>
70
</para>
71
72
<para>
73
The <path>stream.js</path> file:
74
<example>
75
var line = '';
76
77
function bar(s) {
78
var v = s.variables;
79
s.log("hello from bar() handler!");
80
return "bar-var" + v.remote_port + "; pid=" + v.pid;
81
}
82
83
function preread(s) {
84
s.on('upload', function (data, flags) {
85
var n = data.indexOf('\n');
86
if (n != -1) {
87
line = data.substr(0, n);
88
s.done();
89
}
90
});
91
}
92
93
function req_line(s) {
94
return line;
95
}
96
97
// Read HTTP request line.
98
// Collect bytes in 'req' until
99
// request line is read.
100
// Injects HTTP header into a client's request
101
102
var my_header = 'Foo: foo';
103
function header_inject(s) {
104
var req = '';
105
s.on('upload', function(data, flags) {
106
req += data;
107
var n = req.search('\n');
108
if (n != -1) {
109
var rest = req.substr(n + 1);
110
req = req.substr(0, n + 1);
111
s.send(req + my_header + '\r\n' + rest, flags);
112
s.off('upload');
113
}
114
});
115
}
116
117
function access(s) {
118
if (s.remoteAddress.match('^192.*')) {
119
s.deny();
120
return;
121
}
122
123
s.allow();
124
}
125
126
export default {bar, preread, req_line, header_inject, access};
127
</example>
128
</para>
129
130
</section>
131
132
133
<section id="directives" name="Directives">
134
135
<directive name="js_access">
136
<syntax><value>module.function</value></syntax>
137
<default/>
138
<context>stream</context>
139
<context>server</context>
140
141
<para>
142
Sets an njs function which will be called at the
143
<link doc="stream_processing.xml" id="access_phase">access</link> phase.
144
Since <link doc="../njs/changes.xml" id="njs0.4.0">0.4.0</link>,
145
a module function can be referenced.
146
</para>
147
148
<para>
149
The function is called once at the moment when the stream session reaches
150
the <link doc="stream_processing.xml" id="access_phase">access</link> phase
151
for the first time.
152
The function is called with the following arguments:
153
154
<list type="tag">
155
<tag-name><literal>s</literal></tag-name>
156
<tag-desc>
157
the <link doc="../njs/reference.xml" id="stream">Stream Session</link> object
158
</tag-desc>
159
160
</list>
161
</para>
162
163
<para>
164
At this phase, it is possible to perform initialization
165
or register a callback with
166
the <link doc="../njs/reference.xml" id="s_on"><literal>s.on()</literal></link>
167
method
168
for each incoming data chunk until one of the following methods are called:
169
<link doc="../njs/reference.xml" id="s_allow"><literal>s.allow()</literal></link>,
170
<link doc="../njs/reference.xml" id="s_decline"><literal>s.decline()</literal></link>,
171
<link doc="../njs/reference.xml" id="s_done"><literal>s.done()</literal></link>.
172
As soon as one of these methods is called, the stream session processing
173
switches to the <link doc="stream_processing.xml">next phase</link>
174
and all current
175
<link doc="../njs/reference.xml" id="s_on"><literal>s.on()</literal></link>
176
callbacks are dropped.
177
</para>
178
179
</directive>
180
181
182
<directive name="js_context_reuse">
183
<syntax><value>number</value></syntax>
184
<default>128</default>
185
<context>stream</context>
186
<context>server</context>
187
<appeared-in>0.8.6</appeared-in>
188
189
<para>
190
Sets a maximum number of JS context to be reused for
191
<link doc="../njs/engine.xml">QuickJS engine</link>.
192
Each context is used for a single stream session.
193
The finished context is put into a pool of reusable contexts.
194
If the pool is full, the context is destroyed.
195
</para>
196
197
</directive>
198
199
200
<directive name="js_engine">
201
<syntax><literal>njs</literal> | <literal>qjs</literal></syntax>
202
<default>njs</default>
203
<context>stream</context>
204
<context>server</context>
205
<appeared-in>0.8.6</appeared-in>
206
207
<para>
208
Sets a <link doc="../njs/engine.xml">JavaScript engine</link>
209
to be used for njs scripts.
210
The <literal>njs</literal> parameter sets the njs engine, also used by default.
211
The <literal>qjs</literal> parameter sets the QuickJS engine.
212
</para>
213
214
<para>
215
<note>
216
The <literal>njs</literal> engine is deprecated
217
since <link doc="../njs/changes.xml" id="njs1.0.0">1.0.0</link>;
218
new configurations should use the <literal>qjs</literal>
219
(<link doc="../njs/engine.xml" id="quickjs_engine">QuickJS</link>) engine.
220
</note>
221
</para>
222
223
</directive>
224
225
226
<directive name="js_fetch_buffer_size">
227
<syntax><value>size</value></syntax>
228
<default>16k</default>
229
<context>stream</context>
230
<context>server</context>
231
<appeared-in>0.7.4</appeared-in>
232
233
<para>
234
Sets the <value>size</value> of the buffer used for reading and writing
235
with <link doc="../njs/reference.xml" id="ngx_fetch">Fetch API</link>.
236
</para>
237
238
</directive>
239
240
241
<directive name="js_fetch_ciphers">
242
<syntax><value>ciphers</value></syntax>
243
<default>HIGH:!aNULL:!MD5</default>
244
<context>stream</context>
245
<context>server</context>
246
<appeared-in>0.7.0</appeared-in>
247
248
<para>
249
Specifies the enabled ciphers for HTTPS connections
250
with <link doc="../njs/reference.xml" id="ngx_fetch">Fetch API</link>.
251
The ciphers are specified in the format understood by the OpenSSL library.
252
</para>
253
254
<para>
255
The full list can be viewed using the
256
<command>openssl ciphers</command>” command.
257
</para>
258
259
</directive>
260
261
262
<directive name="js_fetch_max_response_buffer_size">
263
<syntax><value>size</value></syntax>
264
<default>1m</default>
265
<context>stream</context>
266
<context>server</context>
267
<appeared-in>0.7.4</appeared-in>
268
269
<para>
270
Sets the maximum <value>size</value> of the response received
271
with <link doc="../njs/reference.xml" id="ngx_fetch">Fetch API</link>.
272
</para>
273
274
</directive>
275
276
277
<directive name="js_fetch_protocols">
278
<syntax>
279
[<literal>TLSv1</literal>]
280
[<literal>TLSv1.1</literal>]
281
[<literal>TLSv1.2</literal>]
282
[<literal>TLSv1.3</literal>]</syntax>
283
<default>TLSv1 TLSv1.1 TLSv1.2</default>
284
<context>stream</context>
285
<context>server</context>
286
<appeared-in>0.7.0</appeared-in>
287
288
<para>
289
Enables the specified protocols for HTTPS connections
290
with <link doc="../njs/reference.xml" id="ngx_fetch">Fetch API</link>.
291
</para>
292
293
</directive>
294
295
296
<directive name="js_fetch_timeout">
297
<syntax><value>time</value></syntax>
298
<default>60s</default>
299
<context>stream</context>
300
<context>server</context>
301
<appeared-in>0.7.4</appeared-in>
302
303
<para>
304
Defines a timeout for reading and writing
305
for <link doc="../njs/reference.xml" id="ngx_fetch">Fetch API</link>.
306
The timeout is set only between two successive read/write operations,
307
not for the whole response.
308
If no data is transmitted within this time, the connection is closed.
309
</para>
310
311
</directive>
312
313
314
<directive name="js_fetch_trusted_certificate">
315
<syntax><value>file</value></syntax>
316
<default/>
317
<context>stream</context>
318
<context>server</context>
319
<appeared-in>0.7.0</appeared-in>
320
321
<para>
322
Specifies a <value>file</value> with trusted CA certificates in the PEM format
323
used to
324
<link doc="../njs/reference.xml" id="fetch_verify">verify</link>
325
the HTTPS certificate
326
with <link doc="../njs/reference.xml" id="ngx_fetch">Fetch API</link>.
327
</para>
328
329
</directive>
330
331
332
<directive name="js_fetch_verify">
333
<syntax><literal>on</literal> | <literal>off</literal></syntax>
334
<default>on</default>
335
<context>stream</context>
336
<context>server</context>
337
<appeared-in>0.7.4</appeared-in>
338
339
<para>
340
Enables or disables verification of the HTTPS server certificate
341
with <link doc="../njs/reference.xml" id="ngx_fetch">Fetch API</link>.
342
</para>
343
344
</directive>
345
346
347
<directive name="js_fetch_verify_depth">
348
<syntax><value>number</value></syntax>
349
<default>100</default>
350
<context>stream</context>
351
<context>server</context>
352
<appeared-in>0.7.0</appeared-in>
353
354
<para>
355
Sets the verification depth in the HTTPS server certificates chain
356
with <link doc="../njs/reference.xml" id="ngx_fetch">Fetch API</link>.
357
</para>
358
359
</directive>
360
361
362
<directive name="js_fetch_proxy">
363
<syntax><value>url</value></syntax>
364
<default/>
365
<context>stream</context>
366
<context>server</context>
367
<appeared-in>0.9.4</appeared-in>
368
369
<para>
370
Configures a forward proxy URL
371
with <link doc="../njs/reference.xml" id="ngx_fetch">Fetch API</link>.
372
The <value>url</value> supports the HTTP scheme only
373
and can contain optional user credentials
374
in the format <literal>http://[user:password@]host:port</literal>
375
for Basic authentication.
376
Supports both HTTP and HTTPS connections to destination servers.
377
If the <value>url</value> is empty, proxy routing is disabled.
378
The parameter value can contain variables.
379
</para>
380
381
<para>
382
Example:
383
<example>
384
server {
385
listen 12345;
386
js_fetch_proxy http://user:[email protected]:3128;
387
js_preread main.fetch_handler;
388
}
389
</example>
390
</para>
391
392
</directive>
393
394
395
<directive name="js_fetch_keepalive">
396
<syntax><value>connections</value></syntax>
397
<default>0</default>
398
<context>stream</context>
399
<context>server</context>
400
<appeared-in>0.9.2</appeared-in>
401
402
<para>
403
Activates the cache for connections to destination servers.
404
When the value is greater than <literal>0</literal>,
405
enables keepalive connections for
406
<link doc="../njs/reference.xml" id="ngx_fetch">Fetch API</link>.
407
</para>
408
409
<para>
410
The <value>connections</value> parameter sets the maximum number of idle
411
keepalive connections to destination servers that are preserved in the cache
412
of each worker process.
413
When this number is exceeded, the least recently used connections are closed.
414
</para>
415
416
<para>
417
In Stream, the cache is maintained separately for each server configuration.
418
A value set at the <literal>stream</literal> level is inherited by servers,
419
but each server uses its own cache.
420
Cached connections are reused for requests with the same protocol, host,
421
and port.
422
</para>
423
424
<para>
425
When enabled, keepalive assumes that destination servers send valid HTTP
426
responses.
427
</para>
428
429
<para>
430
Example:
431
<example>
432
server {
433
listen 12345;
434
js_fetch_keepalive 32;
435
js_fetch_trusted_certificate /path/to/ISRG_Root_X1.pem;
436
js_preread main.fetch_handler;
437
}
438
</example>
439
</para>
440
441
</directive>
442
443
444
<directive name="js_fetch_keepalive_requests">
445
<syntax><value>number</value></syntax>
446
<default>1000</default>
447
<context>stream</context>
448
<context>server</context>
449
<appeared-in>0.9.2</appeared-in>
450
451
<para>
452
Sets the maximum number of requests that can be served through one keepalive
453
connection with <link doc="../njs/reference.xml" id="ngx_fetch">Fetch API</link>.
454
After the maximum number of requests is made, the connection is closed.
455
</para>
456
457
<para>
458
Closing connections periodically is necessary to free per-connection memory
459
allocations.
460
Therefore, using too high maximum number of requests could result in
461
excessive memory usage and not recommended.
462
</para>
463
464
</directive>
465
466
467
<directive name="js_fetch_keepalive_time">
468
<syntax><value>time</value></syntax>
469
<default>1h</default>
470
<context>stream</context>
471
<context>server</context>
472
<appeared-in>0.9.2</appeared-in>
473
474
<para>
475
Limits the maximum time during which requests can be processed through one
476
keepalive connection with <link doc="../njs/reference.xml" id="ngx_fetch">Fetch API</link>.
477
After this time is reached, the connection is closed following the subsequent
478
request processing.
479
</para>
480
481
</directive>
482
483
484
<directive name="js_fetch_keepalive_timeout">
485
<syntax><value>time</value></syntax>
486
<default>60s</default>
487
<context>stream</context>
488
<context>server</context>
489
<appeared-in>0.9.2</appeared-in>
490
491
<para>
492
Sets a timeout during which an idle keepalive connection to a destination server
493
will stay open with <link doc="../njs/reference.xml" id="ngx_fetch">Fetch API</link>.
494
</para>
495
496
</directive>
497
498
499
<directive name="js_filter">
500
<syntax><value>module.function</value></syntax>
501
<default/>
502
<context>stream</context>
503
<context>server</context>
504
505
<para>
506
Sets a data filter.
507
Since <link doc="../njs/changes.xml" id="njs0.4.0">0.4.0</link>,
508
a module function can be referenced.
509
The filter function is called once at the moment when the stream session reaches
510
the <link doc="stream_processing.xml" id="content_phase">content</link> phase.
511
</para>
512
513
<para>
514
The filter function is called with the following arguments:
515
<list type="tag">
516
<tag-name><literal>s</literal></tag-name>
517
<tag-desc>
518
the <link doc="../njs/reference.xml" id="stream">Stream Session</link> object
519
</tag-desc>
520
521
</list>
522
</para>
523
524
<para>
525
At this phase, it is possible to perform initialization
526
or register a callback with
527
the <link doc="../njs/reference.xml" id="s_on"><literal>s.on()</literal></link>
528
method for each incoming data chunk.
529
The
530
<link doc="../njs/reference.xml" id="s_off"><literal>s.off()</literal></link>
531
method may be used to unregister a callback and stop filtering.
532
</para>
533
534
<para>
535
<note>
536
As the <literal>js_filter</literal> handler
537
returns its result immediately, it supports
538
only synchronous operations.
539
Thus, asynchronous operations such as
540
<link doc="../njs/reference.xml" id="ngx_fetch"><literal>ngx.fetch()</literal></link>
541
or
542
<link doc="../njs/reference.xml" id="settimeout"><literal>setTimeout()</literal></link>
543
are not supported.
544
</note>
545
</para>
546
547
</directive>
548
549
550
<directive name="js_import">
551
<syntax><value>module.js</value> |
552
<value>export_name from module.js</value></syntax>
553
<default/>
554
<context>stream</context>
555
<context>server</context>
556
<appeared-in>0.4.0</appeared-in>
557
558
<para>
559
Imports a module that implements location and variable handlers in njs.
560
The <literal>export_name</literal> is used as a namespace
561
to access module functions.
562
If the <literal>export_name</literal> is not specified,
563
the module name will be used as a namespace.
564
<example>
565
js_import stream.js;
566
</example>
567
Here, the module name <literal>stream</literal> is used as a namespace
568
while accessing exports.
569
If the imported module exports <literal>foo()</literal>,
570
<literal>stream.foo</literal> is used to refer to it.
571
</para>
572
573
<para>
574
Several <literal>js_import</literal> directives can be specified.
575
</para>
576
577
<para>
578
<note>
579
The directive can be specified on the
580
<literal>server</literal> level
581
since <link doc="../njs/changes.xml" id="njs0.7.7">0.7.7</link>.
582
</note>
583
</para>
584
585
</directive>
586
587
588
<directive name="js_include">
589
<syntax><value>file</value></syntax>
590
<default/>
591
<context>stream</context>
592
593
<para>
594
Specifies a file that implements server and variable handlers in njs:
595
<example>
596
nginx.conf:
597
js_include stream.js;
598
js_set $js_addr address;
599
server {
600
listen 127.0.0.1:12345;
601
return $js_addr;
602
}
603
604
stream.js:
605
function address(s) {
606
return s.remoteAddress;
607
}
608
</example>
609
</para>
610
611
<para>
612
The directive was made obsolete in version
613
<link doc="../njs/changes.xml" id="njs0.4.0">0.4.0</link>
614
and was removed in version
615
<link doc="../njs/changes.xml" id="njs0.7.1">0.7.1</link>.
616
The <link id="js_import"/> directive should be used instead.
617
</para>
618
619
</directive>
620
621
622
<directive name="js_load_stream_native_module">
623
<syntax><value>path</value> [<literal>as</literal> <value>name</value>]</syntax>
624
<default/>
625
<context>main</context>
626
<appeared-in>0.9.5</appeared-in>
627
628
<para>
629
Loads a <link doc="../njs/native_modules.xml">native module</link>
630
(shared library) for use in Stream JavaScript code.
631
The directive is
632
<link doc="../njs/engine.xml" id="quickjs_engine">QuickJS</link>-only
633
and is not available when using the njs built-in JavaScript engine.
634
</para>
635
636
<para>
637
The <value>path</value> parameter
638
specifies the absolute path to the shared library file.
639
The optional <literal>as</literal> <value>name</value> parameter
640
provides an alias name for importing the module in JavaScript code.
641
If not specified, the module can be imported using its filename.
642
</para>
643
644
<para>
645
Example:
646
<example>
647
js_load_stream_native_module /path/to/mylib.so;
648
js_load_stream_native_module /path/to/other.so as myalias;
649
650
stream {
651
js_import main.js;
652
# ... rest of stream configuration
653
}
654
</example>
655
In JavaScript code:
656
<example>
657
// Import by filename
658
import * as mylib from 'mylib.so';
659
660
// Import by alias
661
import * as myalias from 'myalias';
662
663
// Use exported functions
664
let result = mylib.add(5, 10);
665
</example>
666
</para>
667
668
<para>
669
<note>
670
For security reasons, this directive is only allowed
671
in the <literal>main</literal> configuration context.
672
Native modules run with full process privileges;
673
use absolute paths and ensure proper code review.
674
</note>
675
</para>
676
677
</directive>
678
679
680
<directive name="js_path">
681
<syntax>
682
<value>path</value></syntax>
683
<default/>
684
<context>stream</context>
685
<context>server</context>
686
<appeared-in>0.3.0</appeared-in>
687
688
<para>
689
Sets an additional path for njs modules.
690
</para>
691
692
<para>
693
<note>
694
The directive can be specified on the
695
<literal>server</literal> level
696
since <link doc="../njs/changes.xml" id="njs0.7.7">0.7.7</link>.
697
</note>
698
</para>
699
700
</directive>
701
702
703
<directive name="js_periodic">
704
<syntax><value>module.function</value>
705
[<literal>interval</literal>=<value>time</value>]
706
[<literal>jitter</literal>=<value>number</value>]
707
[<literal>worker_affinity</literal>=<value>mask</value>]</syntax>
708
<default/>
709
<context>server</context>
710
<appeared-in>0.8.1</appeared-in>
711
712
<para>
713
Specifies a content handler to run at regular interval.
714
The handler receives a
715
<link doc="../njs/reference.xml" id="periodic_session">session object</link>
716
as its first argument,
717
it also has access to global objects such as
718
<link doc="../njs/reference.xml" id="ngx">ngx</link>.
719
</para>
720
721
<para>
722
The optional <literal>interval</literal> parameter
723
sets the interval between two consecutive runs,
724
by default, 5 seconds.
725
</para>
726
727
<para>
728
The optional <literal>jitter</literal> parameter sets the time within which
729
the location content handler will be randomly delayed,
730
by default, there is no delay.
731
</para>
732
733
<para>
734
By default, the <literal>js_handler</literal> is executed on worker process 0.
735
The optional <literal>worker_affinity</literal> parameter
736
allows specifying particular worker processes
737
where the location content handler should be executed.
738
Each worker process set is represented by a bitmask of allowed worker processes.
739
The <literal>all</literal> mask allows the handler to be executed
740
in all worker processes.
741
</para>
742
743
<para>
744
Example:
745
<example>
746
example.conf:
747
748
location @periodics {
749
# to be run at 1 minute intervals in worker process 0
750
js_periodic main.handler interval=60s;
751
752
# to be run at 1 minute intervals in all worker processes
753
js_periodic main.handler interval=60s worker_affinity=all;
754
755
# to be run at 1 minute intervals in worker processes 1 and 3
756
js_periodic main.handler interval=60s worker_affinity=0101;
757
758
resolver 10.0.0.1;
759
js_fetch_trusted_certificate /path/to/ISRG_Root_X1.pem;
760
}
761
762
example.js:
763
764
async function handler(s) {
765
let reply = await ngx.fetch('https://nginx.org/en/docs/njs/');
766
let body = await reply.text();
767
768
ngx.log(ngx.INFO, body);
769
}
770
</example>
771
</para>
772
773
</directive>
774
775
776
<directive name="js_preload_object">
777
<syntax><value>name.json</value> |
778
<value>name</value> from <value>file.json</value></syntax>
779
<default/>
780
<context>stream</context>
781
<context>server</context>
782
<appeared-in>0.7.8</appeared-in>
783
784
<para>
785
Preloads an
786
<link doc="../njs/preload_objects.xml">immutable object</link>
787
at configure time.
788
The <literal>name</literal> is used as a name of the global variable
789
though which the object is available in njs code.
790
If the <literal>name</literal> is not specified,
791
the file name will be used instead.
792
<example>
793
js_preload_object map.json;
794
</example>
795
Here, the <literal>map</literal> is used as a name
796
while accessing the preloaded object.
797
</para>
798
799
<para>
800
Several <literal>js_preload_object</literal> directives can be specified.
801
</para>
802
803
</directive>
804
805
806
<directive name="js_preread">
807
<syntax><value>module.function</value></syntax>
808
<default/>
809
<context>stream</context>
810
<context>server</context>
811
812
<para>
813
Sets an njs function which will be called at the
814
<link doc="stream_processing.xml" id="preread_phase">preread</link> phase.
815
Since <link doc="../njs/changes.xml" id="njs0.4.0">0.4.0</link>,
816
a module function can be referenced.
817
</para>
818
819
<para>
820
The function is called once
821
at the moment when the stream session reaches the
822
<link doc="stream_processing.xml" id="preread_phase">preread</link> phase
823
for the first time.
824
The function is called with the following arguments:
825
826
<list type="tag">
827
<tag-name><literal>s</literal></tag-name>
828
<tag-desc>
829
the <link doc="../njs/reference.xml" id="stream">Stream Session</link> object
830
</tag-desc>
831
832
</list>
833
</para>
834
835
<para>
836
At this phase, it is possible to perform initialization
837
or register a callback with
838
the <link doc="../njs/reference.xml" id="s_on"><literal>s.on()</literal></link>
839
method
840
for each incoming data chunk until one of the following methods are called:
841
<link doc="../njs/reference.xml" id="s_allow"><literal>s.allow()</literal></link>,
842
<link doc="../njs/reference.xml" id="s_decline"><literal>s.decline()</literal></link>,
843
<link doc="../njs/reference.xml" id="s_done"><literal>s.done()</literal></link>.
844
When one of these methods is called,
845
the stream session switches to the
846
<link doc="stream_processing.xml">next phase</link>
847
and all current
848
<link doc="../njs/reference.xml" id="s_on"><literal>s.on()</literal></link>
849
callbacks are dropped.
850
</para>
851
852
<para>
853
<note>
854
As the <literal>js_preread</literal> handler
855
returns its result immediately, it supports
856
only synchronous callbacks.
857
Thus, asynchronous callbacks such as
858
<link doc="../njs/reference.xml" id="ngx_fetch"><literal>ngx.fetch()</literal></link>
859
or
860
<link doc="../njs/reference.xml" id="settimeout"><literal>setTimeout()</literal></link>
861
are not supported.
862
Nevertheless, asynchronous operations are supported in
863
<link doc="../njs/reference.xml" id="s_on"><literal>s.on()</literal></link>
864
callbacks in the
865
<link doc="stream_processing.xml" id="preread_phase">preread</link> phase.
866
See
867
<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.
868
</note>
869
</para>
870
871
</directive>
872
873
874
<directive name="js_set">
875
<syntax>
876
<value>$variable</value>
877
<value>module.function</value>
878
[<literal>nocache</literal>]</syntax>
879
<default/>
880
<context>stream</context>
881
<context>server</context>
882
883
<para>
884
Sets an njs <literal>function</literal>
885
for the specified <literal>variable</literal>.
886
Since <link doc="../njs/changes.xml" id="njs0.4.0">0.4.0</link>,
887
a module function can be referenced.
888
</para>
889
890
<para>
891
The function is called when
892
the variable is referenced for the first time for a given request.
893
The exact moment depends on a
894
<link doc="stream_processing.xml">phase</link>
895
at which the variable is referenced.
896
This can be used to perform some logic
897
not related to variable evaluation.
898
For example, if the variable is referenced only in the
899
<link doc="ngx_stream_log_module.xml" id="log_format"/> directive,
900
its handler will not be executed until the log phase.
901
This handler can be used to do some cleanup
902
right before the request is freed.
903
</para>
904
905
<para>
906
Since <link doc="../njs/changes.xml" id="njs0.8.6">0.8.6</link>, when
907
optional argument <literal>nocache</literal> is provided the handler
908
is called every time it is referenced.
909
Due to current limitations
910
of the <link doc="ngx_stream_rewrite_module.xml">rewrite</link> module,
911
when a <literal>nocache</literal> variable is referenced by the
912
<link doc="ngx_stream_set_module.xml" id="set">set</link> directive
913
its handler should always return a fixed-length value.
914
</para>
915
916
<para>
917
<note>
918
As the <literal>js_set</literal> handler
919
returns its result immediately, it supports
920
only synchronous callbacks.
921
Thus, asynchronous callbacks such as
922
<link doc="../njs/reference.xml" id="ngx_fetch">ngx.fetch()</link>
923
or
924
<link doc="../njs/reference.xml" id="settimeout">setTimeout()</link>
925
are not supported.
926
</note>
927
</para>
928
929
<para>
930
<note>
931
The directive can be specified on the
932
<literal>server</literal> level
933
since <link doc="../njs/changes.xml" id="njs0.7.7">0.7.7</link>.
934
</note>
935
</para>
936
937
</directive>
938
939
940
<directive name="js_shared_dict_zone">
941
<syntax>
942
<literal>zone</literal>=<value>name</value>:<value>size</value>
943
[<literal>timeout</literal>=<value>time</value>]
944
[<literal>type</literal>=<literal>string</literal>|<literal>number</literal>]
945
[<literal>evict</literal>]
946
[<literal>state</literal>=<value>file</value>]</syntax>
947
<default/>
948
<context>stream</context>
949
<appeared-in>0.8.0</appeared-in>
950
951
<para>
952
Sets the <value>name</value> and <value>size</value> of the shared memory zone
953
that keeps the
954
key-value <link doc="../njs/reference.xml" id="dict">dictionary</link>
955
shared between worker processes.
956
</para>
957
958
<para>
959
By default the shared dictionary uses a string as a key and a value.
960
The optional <literal>type</literal> parameter
961
allows redefining the value type to number.
962
</para>
963
964
<para>
965
The optional <literal>timeout</literal> parameter sets
966
the time in milliseconds
967
after which all shared dictionary entries are removed from the zone.
968
If some entries require a different removal time, it can be set
969
with the <literal>timeout</literal> argument of the
970
<link doc="../njs/reference.xml" id="dict_add">add</link>,
971
<link doc="../njs/reference.xml" id="dict_incr">incr</link>, and
972
<link doc="../njs/reference.xml" id="dict_set">set</link>
973
methods
974
(<link doc="../njs/changes.xml" id="njs0.8.5">0.8.5</link>).
975
</para>
976
977
<para>
978
The optional <literal>evict</literal> parameter removes the oldest
979
key-value pair when the zone storage is exhausted.
980
</para>
981
982
<para>
983
The optional <literal>state</literal> parameter specifies a <value>file</value>
984
that keeps the shared dictionary state
985
in JSON format and makes it persistent across nginx restarts
986
(<link doc="../njs/changes.xml" id="njs0.9.1">0.9.1</link>).
987
</para>
988
989
<para>
990
Example:
991
<example>
992
example.conf:
993
# Creates a 1Mb dictionary with string values,
994
# removes key-value pairs after 60 seconds of inactivity:
995
js_shared_dict_zone zone=foo:1M timeout=60s;
996
997
# Creates a 512Kb dictionary with string values,
998
# forcibly removes oldest key-value pairs when the zone is exhausted:
999
js_shared_dict_zone zone=bar:512K timeout=30s evict;
1000
1001
# Creates a 32Kb permanent dictionary with number values:
1002
js_shared_dict_zone zone=num:32k type=number;
1003
1004
# Creates a 1Mb dictionary with string values and persistent state:
1005
js_shared_dict_zone zone=persistent:1M state=/tmp/dict.json;
1006
1007
example.js:
1008
function get(r) {
1009
r.return(200, ngx.shared.foo.get(r.args.key));
1010
}
1011
1012
function set(r) {
1013
r.return(200, ngx.shared.foo.set(r.args.key, r.args.value));
1014
}
1015
1016
function del(r) {
1017
r.return(200, ngx.shared.bar.delete(r.args.key));
1018
}
1019
1020
function increment(r) {
1021
r.return(200, ngx.shared.num.incr(r.args.key, 2));
1022
}
1023
</example>
1024
</para>
1025
1026
</directive>
1027
1028
1029
<directive name="js_var">
1030
<syntax><value>$variable</value> [<value>value</value>]</syntax>
1031
<default/>
1032
<context>stream</context>
1033
<context>server</context>
1034
<appeared-in>0.5.3</appeared-in>
1035
1036
<para>
1037
Declares
1038
a <link doc="../njs/reference.xml" id="r_variables">writable</link>
1039
variable.
1040
The value can contain text, variables, and their combination.
1041
</para>
1042
1043
<para>
1044
<note>
1045
The directive can be specified on the
1046
<literal>server</literal> level
1047
since <link doc="../njs/changes.xml" id="njs0.7.7">0.7.7</link>.
1048
</note>
1049
</para>
1050
1051
</directive>
1052
1053
</section>
1054
1055
1056
<section id="properties" name="Session Object Properties">
1057
1058
<para>
1059
Each stream njs handler receives one argument, a stream session
1060
<link doc="../njs/reference.xml" id="stream">object</link>.
1061
</para>
1062
1063
</section>
1064
1065
</module>
1066
1067