Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
nginx
GitHub Repository: nginx/nginx.org
Path: blob/main/xml/en/docs/njs/reference.xml
1 views
1
<?xml version="1.0"?>
2
3
<!--
4
Copyright (C) Nginx, Inc.
5
-->
6
7
<!DOCTYPE article SYSTEM "../../../../dtd/article.dtd">
8
9
<article name="Reference"
10
link="/en/docs/njs/reference.html"
11
lang="en"
12
rev="136">
13
14
<section id="summary">
15
16
<para>
17
<link doc="index.xml">njs</link> provides objects, methods and properties
18
for extending nginx functionality.
19
</para>
20
21
<para>
22
This reference contains only njs specific properties, methods and modules
23
not compliant with ECMAScript.
24
Definitions of njs properties and methods compliant with ECMAScript
25
can be found in
26
<link url="http://www.ecma-international.org/ecma-262/">ECMAScript
27
specification</link>.
28
List of all njs properties and methods can be found in
29
<link doc="compatibility.xml">Compatibility</link>.
30
</para>
31
32
</section>
33
34
35
<section id="http_stream" name="nginx objects">
36
37
38
<section id="http" name="HTTP Request">
39
40
<para>
41
<table width="100%">
42
<tr><td><link id="r_args"><literal>r.args{}</literal></link></td></tr>
43
<tr><td><link id="r_decline"><literal>r.decline()</literal></link></td></tr>
44
<tr><td><link id="r_done"><literal>r.done()</literal></link></td></tr>
45
<tr><td><link id="r_error"><literal>r.error()</literal></link></td></tr>
46
<tr><td><link id="r_finish"><literal>r.finish()</literal></link></td></tr>
47
<tr><td><link id="r_headers_in"><literal>r.headersIn{}</literal></link></td></tr>
48
<tr><td><link id="r_headers_out"><literal>r.headersOut{}</literal></link></td></tr>
49
<tr><td><link id="r_http_version"><literal>r.httpVersion</literal></link></td></tr>
50
<tr><td><link id="r_internal"><literal>r.internal</literal></link></td></tr>
51
<tr><td><link id="r_internal_redirect"><literal>r.internalRedirect()</literal></link></td></tr>
52
<tr><td><link id="r_js_var_names"><literal>r.jsVarNames()</literal></link></td></tr>
53
<tr><td><link id="r_log"><literal>r.log()</literal></link></td></tr>
54
<tr><td><link id="r_method"><literal>r.method</literal></link></td></tr>
55
<tr><td><link id="r_parent"><literal>r.parent</literal></link></td></tr>
56
<tr><td><link id="r_read_request_array_buffer"><literal>r.readRequestArrayBuffer()</literal></link></td></tr>
57
<tr><td><link id="r_read_request_form"><literal>r.readRequestForm()</literal></link></td></tr>
58
<tr><td><link id="r_read_request_json"><literal>r.readRequestJSON()</literal></link></td></tr>
59
<tr><td><link id="r_read_request_text"><literal>r.readRequestText()</literal></link></td></tr>
60
<tr><td><link id="r_remote_address"><literal>r.remoteAddress</literal></link></td></tr>
61
<tr><td><link id="r_request_body"><literal>r.requestBody</literal></link></td></tr>
62
<tr><td><link id="r_request_buffer"><literal>r.requestBuffer</literal></link></td></tr>
63
<tr><td><link id="r_request_text"><literal>r.requestText</literal></link></td></tr>
64
<tr><td><link id="r_raw_headers_in"><literal>r.rawHeadersIn[]</literal></link></td></tr>
65
<tr><td><link id="r_raw_headers_out"><literal>r.rawHeadersOut[]</literal></link></td></tr>
66
<tr><td><link id="r_response_body"><literal>r.responseBody</literal></link></td></tr>
67
<tr><td><link id="r_response_buffer"><literal>r.responseBuffer</literal></link></td></tr>
68
<tr><td><link id="r_response_text"><literal>r.responseText</literal></link></td></tr>
69
<tr><td><link id="r_return"><literal>r.return()</literal></link></td></tr>
70
<tr><td><link id="r_send"><literal>r.send()</literal></link></td></tr>
71
<tr><td><link id="r_sendbuffer"><literal>r.sendBuffer()</literal></link></td></tr>
72
<tr><td><link id="r_send_header"><literal>r.sendHeader()</literal></link></td></tr>
73
<tr><td><link id="r_set_return_value"><literal>r.setReturnValue()</literal></link></td></tr>
74
<tr><td><link id="r_status"><literal>r.status</literal></link></td></tr>
75
<tr><td><link id="r_subrequest"><literal>r.subrequest()</literal></link></td></tr>
76
<tr><td><link id="r_uri"><literal>r.uri</literal></link></td></tr>
77
<tr><td><link id="r_raw_variables"><literal>r.rawVariables{}</literal></link></td></tr>
78
<tr><td><link id="r_variables"><literal>r.variables{}</literal></link></td></tr>
79
<tr><td><link id="r_warn"><literal>r.warn()</literal></link></td></tr>
80
</table>
81
</para>
82
83
<para>
84
The HTTP request object is available only in the
85
<link doc="../http/ngx_http_js_module.xml">ngx_http_js_module</link> module.
86
Before <link doc="changes.xml" id="njs0.8.5">0.8.5</link>,
87
all string properties of the object
88
were <link id="string">byte strings</link>.
89
90
<list type="tag">
91
92
<tag-name id="r_args"><literal>r.args{}</literal></tag-name>
93
<tag-desc>
94
request arguments object, read-only.
95
<para>
96
The query string is returned as an object.
97
Since <link doc="changes.xml" id="njs0.7.6">0.7.6</link>,
98
duplicate keys are returned as an array,
99
keys are case-sensitive, both keys and values are percent-decoded.
100
</para>
101
102
<para>
103
For example, the query string
104
<example>
105
'a=1&amp;b=%32&amp;A=3&amp;b=4&amp;B=two%20words'
106
</example>
107
is converted to <literal>r.args</literal> as:
108
<example>
109
{a: "1", b: ["2", "4"], A: "3", B: "two words"}
110
</example>
111
More advanced parsing scenarios can be achieved with the
112
<link id="querystring">Query String</link> module
113
and with the
114
<link doc="../http/ngx_http_core_module.xml" id="var_args"><literal>$args</literal></link>
115
variable, for example:
116
117
<example>
118
import qs from 'querystring';
119
120
function args(r) {
121
return qs.parse(r.variables.args);
122
}
123
</example>
124
The argument object
125
is evaluated at the first access to <literal>r.args</literal>.
126
If only a single argument is needed, for example <literal>foo</literal>,
127
<link doc="../varindex.xml">nginx variables</link> can be used:
128
<example>
129
r.variables.arg_foo
130
</example>
131
Here, <link id="r_variables">nginx variables object</link>
132
returns the first value for a given key,
133
case-insensitive, without percent-decoding.
134
</para>
135
136
<para>
137
To convert <literal>r.args</literal> back to a string,
138
the Query String
139
<link id="querystring_stringify"><literal>stringify</literal></link>
140
method can be used.
141
</para>
142
</tag-desc>
143
144
<tag-name id="r_decline"><literal>r.decline()</literal></tag-name>
145
<tag-desc>
146
makes the
147
<link doc="../http/ngx_http_js_module.xml" id="js_access"/> handler
148
express no opinion about access, deferring the decision to other access
149
checkers under
150
<link doc="../http/ngx_http_core_module.xml" id="satisfy"><literal>satisfy any</literal></link>
151
(<link doc="changes.xml" id="njs0.9.9">0.9.9</link>).
152
A handler that returns without calling
153
<link id="r_decline"><literal>r.decline()</literal></link>
154
or
155
<link id="r_return"><literal>r.return()</literal></link>
156
implicitly grants access.
157
May be called only from the
158
<link doc="../http/ngx_http_js_module.xml" id="js_access"/> function.
159
</tag-desc>
160
161
<tag-name id="r_done"><literal>r.done()</literal></tag-name>
162
<tag-desc>
163
after calling this function,
164
next data chunks will be passed to client without calling
165
<link doc="../http/ngx_http_js_module.xml" id="js_body_filter"/>
166
(<link doc="changes.xml" id="njs0.5.2">0.5.2</link>).
167
May be called only from the
168
<link doc="../http/ngx_http_js_module.xml" id="js_body_filter"/> function
169
</tag-desc>
170
171
<tag-name id="r_error"><literal>r.error(<value>string</value>)</literal></tag-name>
172
<tag-desc>
173
writes a <literal>string</literal> to the error log
174
on the <literal>error</literal> level of logging
175
<para>
176
<note>
177
As nginx has a
178
<link doc="../dev/development_guide.xml" id="logging">hardcoded</link>
179
maximum line length limit,
180
only first 2048 bytes of the string can be logged.
181
</note>
182
</para>
183
</tag-desc>
184
185
<tag-name id="r_finish"><literal>r.finish()</literal></tag-name>
186
<tag-desc>
187
finishes sending a response to the client
188
</tag-desc>
189
190
<tag-name id="r_headers_in"><literal>r.headersIn{}</literal></tag-name>
191
<tag-desc>
192
incoming headers object, read-only.
193
<para>
194
The <literal>Foo</literal> request header
195
can be accessed with the syntax:
196
<literal>headersIn.foo</literal> or <literal>headersIn['Foo']</literal>.
197
</para>
198
199
<para>
200
The
201
<header>Authorization</header>,
202
<header>Content-Length</header>,
203
<header>Content-Range</header>,
204
<header>Content-Type</header>,
205
<header>ETag</header>,
206
<header>Expect</header>,
207
<header>From</header>,
208
<header>Host</header>,
209
<header>If-Match</header>,
210
<header>If-Modified-Since</header>,
211
<header>If-None-Match</header>,
212
<header>If-Range</header>,
213
<header>If-Unmodified-Since</header>,
214
<header>Max-Forwards</header>,
215
<header>Proxy-Authorization</header>,
216
<header>Referer</header>,
217
<header>Transfer-Encoding</header>, and
218
<header>User-Agent</header>
219
request headers can have only one field value
220
(<link doc="changes.xml" id="njs0.4.1">0.4.1</link>).
221
Duplicate field values in <header>Cookie</header> headers
222
are separated by semicolon (<literal>;</literal>).
223
Duplicate field values in all other request headers are separated by commas.
224
</para>
225
</tag-desc>
226
227
<tag-name id="r_headers_out"><literal>r.headersOut{}</literal></tag-name>
228
<tag-desc>
229
outgoing headers object for the main request, writable.
230
231
<para>
232
If <literal>r.headersOut{}</literal> is the response object of
233
a <link id="r_subrequest">subrequest</link>, it represents response headers.
234
In this case, field values in
235
<header>Accept-Ranges</header>,
236
<header>Connection</header>,
237
<header>Content-Disposition</header>,
238
<header>Content-Encoding</header>,
239
<header>Content-Length</header>,
240
<header>Content-Range</header>,
241
<header>Date</header>,
242
<header>Keep-Alive</header>,
243
<header>Server</header>,
244
<header>Transfer-Encoding</header>,
245
<header>X-Accel-*</header>
246
response headers may be omitted.
247
</para>
248
249
<para>
250
The <header>Foo</header> response header
251
can be accessed with the syntax:
252
<literal>headersOut.foo</literal> or <literal>headersOut['Foo']</literal>.
253
</para>
254
255
<para>
256
Outgoing headers should be set before a response header is sent to a client,
257
otherwise header update will be ignored.
258
This means that <literal>r.headersOut{}</literal> is effectively writable in:
259
260
<list type="bullet">
261
262
<listitem>
263
the <link doc="../http/ngx_http_js_module.xml" id="js_content"/> handler before
264
<link id="r_send_header"><literal>r.sendHeader()</literal></link> or
265
<link id="r_return"><literal>r.return()</literal></link> are called
266
</listitem>
267
268
<listitem>
269
the <link doc="../http/ngx_http_js_module.xml" id="js_header_filter"/> handler
270
</listitem>
271
272
</list>
273
</para>
274
275
<para>
276
Field values of multi-value response headers
277
(<link doc="changes.xml" id="njs0.4.0">0.4.0</link>)
278
can be set with the syntax:
279
<example>
280
r.headersOut['Foo'] = ['a', 'b']
281
</example>
282
where the output will be:
283
<example>
284
Foo: a
285
Foo: b
286
</example>
287
All previous field values of the <header>Foo</header> response header
288
will be deleted.
289
</para>
290
291
<para>
292
For standard response headers
293
that accept only a single field value such as
294
<header>Content-Type</header>,
295
only the last element of the array will take effect.
296
Field values of the <header>Set-Cookie</header> response header
297
are always returned as an array.
298
Duplicate field values in
299
<header>Age</header>,
300
<header>Content-Encoding</header>,
301
<header>Content-Length</header>,
302
<header>Content-Type</header>,
303
<header>ETag</header>,
304
<header>Expires</header>,
305
<header>Last-Modified</header>,
306
<header>Location</header>,
307
<header>Retry-After</header>
308
response headers are ignored.
309
Duplicate field values in all other response headers
310
are separated by commas.
311
</para>
312
</tag-desc>
313
314
<tag-name id="r_http_version"><literal>r.httpVersion</literal></tag-name>
315
<tag-desc>
316
HTTP version, read-only
317
</tag-desc>
318
319
<tag-name id="r_internal"><literal>r.internal</literal></tag-name>
320
<tag-desc>
321
boolean value, true for
322
<link doc="../http/ngx_http_core_module.xml" id="internal">internal</link>
323
locations
324
</tag-desc>
325
326
<tag-name id="r_internal_redirect"><literal>r.internalRedirect(<value>uri</value>)</literal></tag-name>
327
<tag-desc>
328
performs an
329
<link doc="../dev/development_guide.xml" id="http_request_redirection">internal
330
redirect</link>
331
to the specified <literal>uri</literal>.
332
If the uri starts with the “<literal>@</literal>” prefix,
333
it is considered a named location.
334
In a new location, all request processing is repeated
335
starting from <link doc="../dev/development_guide.xml" id="http_phases">NGX_HTTP_SERVER_REWRITE_PHASE</link>
336
for ordinary locations and from <link doc="../dev/development_guide.xml" id="http_phases">NGX_HTTP_REWRITE_PHASE</link>
337
for named locations.
338
As a result, a redirect to a named location
339
does not check <link doc="../http/ngx_http_core_module.xml" id="client_max_body_size"/>
340
limit.
341
See <link doc="../dev/development_guide.xml" id="http_request_redirection">dev guide</link>
342
for more details.
343
Redirected requests become internal and can access the
344
<link doc="../http/ngx_http_core_module.xml" id="internal">internal</link>
345
locations.
346
The actual redirect happens after the handler execution is completed.
347
<para>
348
<note>
349
After redirect,
350
a new njs VM is started in the target location,
351
the VM in the original location is stopped.
352
Values of <link doc="../varindex.xml">nginx variables</link> are kept
353
and can be used to pass information to the target location.
354
Since <link doc="changes.xml" id="njs0.5.3">0.5.3</link>,
355
the variable declared with the <literal>js_var</literal> directive for
356
<link doc="../http/ngx_http_js_module.xml" id="js_var">http</link> or
357
<link doc="../stream/ngx_stream_js_module.xml" id="js_var">stream</link>
358
can be used.
359
</note>
360
</para>
361
362
<para>
363
<note>
364
Since <link doc="changes.xml" id="njs0.7.4">0.7.4</link>,
365
the method accepts escaped URIs.
366
</note>
367
</para>
368
369
</tag-desc>
370
371
<tag-name id="r_log"><literal>r.log(<value>string</value>)</literal></tag-name>
372
<tag-desc>
373
writes a <literal>string</literal> to the error log
374
on the <literal>info</literal> level of logging
375
<para>
376
<note>
377
As nginx has a
378
<link doc="../dev/development_guide.xml" id="logging">hardcoded</link>
379
maximum line length limit,
380
only first 2048 bytes of the string can be logged.
381
</note>
382
</para>
383
</tag-desc>
384
385
<tag-name id="r_method"><literal>r.method</literal></tag-name>
386
<tag-desc>
387
HTTP method, read-only
388
</tag-desc>
389
390
<tag-name id="r_parent"><literal>r.parent</literal></tag-name>
391
<tag-desc>
392
references the parent request object
393
</tag-desc>
394
395
<tag-name id="r_remote_address"><literal>r.remoteAddress</literal></tag-name>
396
<tag-desc>
397
client address, read-only
398
</tag-desc>
399
400
<tag-name id="r_read_request_array_buffer"><literal>r.readRequestArrayBuffer()</literal></tag-name>
401
<tag-desc>
402
returns a <literal>Promise</literal> that resolves with the client
403
request body as an <literal>ArrayBuffer</literal>
404
(<link doc="changes.xml" id="njs0.9.9">0.9.9</link>).
405
See
406
<link id="r_read_request_text"><literal>r.readRequestText()</literal></link>
407
for availability, body caching, and concurrency semantics.
408
</tag-desc>
409
410
<tag-name id="r_read_request_form"><literal>r.readRequestForm(<value>[options]</value>)</literal></tag-name>
411
<tag-desc>
412
reads the client request body and parses it as an HTML form,
413
returning a <literal>Promise</literal> that resolves with a form object
414
(<link doc="changes.xml" id="njs0.9.9">0.9.9</link>).
415
Supported content types are
416
<literal>application/x-www-form-urlencoded</literal>
417
and <literal>multipart/form-data</literal>.
418
419
<para>
420
The form object provides the following methods:
421
<list type="tag">
422
423
<tag-name><literal>get(<value>name</value>)</literal></tag-name>
424
<tag-desc>
425
returns the first value for <value>name</value>,
426
or <literal>null</literal> if absent.
427
</tag-desc>
428
429
<tag-name><literal>getAll(<value>name</value>)</literal></tag-name>
430
<tag-desc>
431
returns an array with all values for <value>name</value>.
432
</tag-desc>
433
434
<tag-name><literal>has(<value>name</value>)</literal></tag-name>
435
<tag-desc>
436
returns <literal>true</literal> if at least one entry exists
437
for <value>name</value>.
438
</tag-desc>
439
440
<tag-name><literal>forEach(<value>callback</value>[,
441
<value>thisArg</value>])</literal></tag-name>
442
<tag-desc>
443
invokes <value>callback</value> for each entry with arguments
444
<literal>(value, key, form)</literal>.
445
</tag-desc>
446
447
<tag-name><literal>hasFiles()</literal></tag-name>
448
<tag-desc>
449
returns <literal>true</literal> if the form contains any file parts.
450
</tag-desc>
451
452
</list>
453
</para>
454
455
<para>
456
For text fields, values are decoded strings.
457
For file parts, the value is an object with the client-supplied
458
filename available as the <literal>name</literal> property;
459
file contents are not exposed.
460
The filename is client-supplied and is not sanitized.
461
</para>
462
463
<para>
464
The optional <value>options</value> object accepts the following
465
properties:
466
<list type="tag">
467
468
<tag-name><literal>maxKeys</literal>
469
<value>number</value></tag-name>
470
<tag-desc>
471
the maximum number of fields to parse,
472
by default is <literal>128</literal>.
473
Exceeding the limit rejects the returned promise.
474
</tag-desc>
475
476
</list>
477
</para>
478
479
<para>
480
See
481
<link id="r_read_request_text"><literal>r.readRequestText()</literal></link>
482
for availability, body caching, and concurrency semantics.
483
The parsed form is itself cached on the request:
484
a subsequent call returns the same form object and ignores any
485
new <value>options</value> argument.
486
</para>
487
</tag-desc>
488
489
<tag-name id="r_read_request_json"><literal>r.readRequestJSON()</literal></tag-name>
490
<tag-desc>
491
returns a <literal>Promise</literal> that resolves with the client
492
request body parsed as JSON
493
(<link doc="changes.xml" id="njs0.9.9">0.9.9</link>).
494
See
495
<link id="r_read_request_text"><literal>r.readRequestText()</literal></link>
496
for availability, body caching, and concurrency semantics.
497
</tag-desc>
498
499
<tag-name id="r_read_request_text"><literal>r.readRequestText()</literal></tag-name>
500
<tag-desc>
501
returns a <literal>Promise</literal> that resolves with the client
502
request body as a string
503
(<link doc="changes.xml" id="njs0.9.9">0.9.9</link>).
504
The size of the request body is limited by
505
<link doc="../http/ngx_http_core_module.xml" id="client_max_body_size"/>.
506
The method is available in the
507
<link doc="../http/ngx_http_js_module.xml" id="js_access"/>
508
and
509
<link doc="../http/ngx_http_js_module.xml" id="js_content"/> directives.
510
511
<para>
512
The body is read once and cached on the request:
513
subsequent calls to
514
<link id="r_read_request_text"><literal>r.readRequestText()</literal></link>,
515
<link id="r_read_request_array_buffer"><literal>r.readRequestArrayBuffer()</literal></link>,
516
<link id="r_read_request_json"><literal>r.readRequestJSON()</literal></link>,
517
or
518
<link id="r_read_request_form"><literal>r.readRequestForm()</literal></link>
519
resolve from the cached body without re-reading the wire.
520
A second call issued while a previous read is still pending throws
521
<literal>"request body is already being read"</literal>.
522
</para>
523
524
<para>
525
Note that
526
it may convert bytes invalid in UTF-8 encoding into the replacement
527
character.
528
</para>
529
</tag-desc>
530
531
<tag-name id="r_request_body"><literal>r.requestBody</literal></tag-name>
532
<tag-desc>
533
the property was made obsolete in
534
<link doc="changes.xml" id="njs0.5.0">0.5.0</link>
535
and was removed in <link doc="changes.xml" id="njs0.8.0">0.8.0</link>.
536
The <link id="r_request_buffer"><literal>r.requestBuffer</literal></link> or
537
<link id="r_request_text"><literal>r.requestText</literal></link> property
538
should be used instead.
539
</tag-desc>
540
541
<tag-name id="r_request_buffer"><literal>r.requestBuffer</literal></tag-name>
542
<tag-desc>
543
client request body if it has not been written to a temporary file
544
(since <link doc="changes.xml" id="njs0.5.0">0.5.0</link>).
545
To ensure that the client request body is in memory,
546
its size should be limited by
547
<link doc="../http/ngx_http_core_module.xml" id="client_max_body_size"/>,
548
and a sufficient buffer size should be set using
549
<link doc="../http/ngx_http_core_module.xml" id="client_body_buffer_size"/>.
550
The property is available only in the
551
<link doc="../http/ngx_http_js_module.xml" id="js_content"/> directive.
552
For asynchronous access, or for use in
553
<link doc="../http/ngx_http_js_module.xml" id="js_access"/>, see
554
<link id="r_read_request_text"><literal>r.readRequestText()</literal></link>
555
and other “<literal>r.readRequest*()</literal>” methods.
556
</tag-desc>
557
558
<tag-name id="r_request_text"><literal>r.requestText</literal></tag-name>
559
<tag-desc>
560
the same as <link id="r_request_buffer"><literal>r.requestBuffer</literal></link>,
561
but returns a <literal>string</literal>.
562
Note that
563
it may convert bytes invalid in UTF-8 encoding into the replacement character.
564
For asynchronous access, or for use in
565
<link doc="../http/ngx_http_js_module.xml" id="js_access"/>, see
566
<link id="r_read_request_text"><literal>r.readRequestText()</literal></link>.
567
</tag-desc>
568
569
<tag-name id="r_raw_headers_in"><literal>r.rawHeadersIn[]</literal></tag-name>
570
<tag-desc>
571
returns an array of key-value pairs
572
exactly as they were received from the client
573
(<link doc="changes.xml" id="njs0.4.1">0.4.1</link>).
574
<para>
575
For example, with the following request headers:
576
<example>
577
Host: localhost
578
Foo: bar
579
foo: bar2
580
</example>
581
the output of <literal>r.rawHeadersIn</literal> will be:
582
<example>
583
[
584
['Host', 'localhost'],
585
['Foo', 'bar'],
586
['foo', 'bar2']
587
]
588
</example>
589
All <literal>foo</literal> headers
590
can be collected with the syntax:
591
<example>
592
r.rawHeadersIn.filter(v=>v[0].toLowerCase() == 'foo').map(v=>v[1])
593
</example>
594
the output will be:
595
<example>
596
['bar', 'bar2']
597
</example>
598
Header field names are not converted to lower case,
599
duplicate field values are not merged.
600
</para>
601
</tag-desc>
602
603
<tag-name id="r_raw_headers_out"><literal>r.rawHeadersOut[]</literal></tag-name>
604
<tag-desc>
605
returns an array of key-value pairs of response headers
606
(<link doc="changes.xml" id="njs0.4.1">0.4.1</link>).
607
Header field names are not converted to lower case,
608
duplicate field values are not merged.
609
</tag-desc>
610
611
<tag-name id="r_response_body"><literal>r.responseBody</literal></tag-name>
612
<tag-desc>
613
the property was made obsolete in
614
<link doc="changes.xml" id="njs0.5.0">0.5.0</link>
615
and was removed in <link doc="changes.xml" id="njs0.8.0">0.8.0</link>.
616
The <link id="r_response_buffer"><literal>r.responseBuffer</literal></link>
617
or
618
the <link id="r_response_text"><literal>r.responseText</literal></link>
619
property
620
should be used instead.
621
</tag-desc>
622
623
<tag-name id="r_response_buffer"><literal>r.responseBuffer</literal></tag-name>
624
<tag-desc>
625
holds the <link id="r_subrequest">subrequest</link> response body,
626
read-only
627
(since <link doc="changes.xml" id="njs0.5.0">0.5.0</link>).
628
The size of <literal>r.responseBuffer</literal> is limited by the
629
<link doc="../http/ngx_http_core_module.xml" id="subrequest_output_buffer_size"/>
630
directive.
631
</tag-desc>
632
633
<tag-name id="r_response_text"><literal>r.responseText</literal></tag-name>
634
<tag-desc>
635
the same as <link id="r_response_buffer"><literal>r.responseBuffer</literal></link>
636
but returns a string
637
(since <link doc="changes.xml" id="njs0.5.0">0.5.0</link>).
638
Note that
639
it may convert bytes invalid in UTF-8 encoding into the replacement character.
640
</tag-desc>
641
642
<tag-name id="r_return"><literal>r.return(status[,
643
string | Buffer])</literal></tag-name>
644
<tag-desc>
645
sends the entire response
646
with the specified <literal>status</literal> to the client.
647
The response can be a string or Buffer
648
(<link doc="changes.xml" id="njs0.5.0">0.5.0</link>).
649
<para>
650
May be called from the
651
<link doc="../http/ngx_http_js_module.xml" id="js_content"/>
652
or, since <link doc="changes.xml" id="njs0.9.9">0.9.9</link>,
653
<link doc="../http/ngx_http_js_module.xml" id="js_access"/> handler.
654
</para>
655
<para>
656
It is possible to specify either a redirect URL
657
(for codes 301, 302, 303, 307, and 308)
658
or the response body text (for other codes) as the second argument
659
</para>
660
</tag-desc>
661
662
<tag-name id="r_send"><literal>r.send(string
663
| Buffer)</literal></tag-name>
664
<tag-desc>
665
sends a part of the response body to the client.
666
The data sent can be a string or Buffer
667
(<link doc="changes.xml" id="njs0.5.0">0.5.0</link>)
668
</tag-desc>
669
670
<tag-name id="r_sendbuffer"><literal>r.sendBuffer(<value>data</value>[,
671
<value>options</value>])</literal></tag-name>
672
<tag-desc>
673
adds data to the chain of data chunks to be forwarded to the next body filter
674
(<link doc="changes.xml" id="njs0.5.2">0.5.2</link>).
675
The actual forwarding happens later,
676
when the all the data chunks of the current chain are processed.
677
<para>
678
The data can be a string or Buffer.
679
The <literal>options</literal> is an object used
680
to override nginx buffer flags derived from an incoming data chunk buffer.
681
The flags can be overridden with the following flags:
682
<list type="tag">
683
684
<tag-name><literal>last</literal></tag-name>
685
<tag-desc>
686
boolean,
687
true if the buffer is the last buffer
688
</tag-desc>
689
690
<tag-name><literal>flush</literal></tag-name>
691
<tag-desc>
692
boolean,
693
true if the buffer should have the <literal>flush</literal> flag
694
</tag-desc>
695
</list>
696
</para>
697
The method may be called only from the
698
<link doc="../http/ngx_http_js_module.xml" id="js_body_filter"/> function.
699
</tag-desc>
700
701
<tag-name id="r_send_header"><literal>r.sendHeader()</literal></tag-name>
702
<tag-desc>
703
sends the HTTP headers to the client
704
</tag-desc>
705
706
<tag-name id="r_set_return_value"><literal>r.setReturnValue(<value>value</value>)</literal></tag-name>
707
<tag-desc>
708
sets the return value of the
709
<link doc="../http/ngx_http_js_module.xml" id="js_set"/> handler
710
(<link doc="changes.xml" id="njs0.7.0">0.7.0</link>).
711
Unlike an ordinary return statement,
712
this method should be used when the handler is JS async function.
713
For example:
714
<example>
715
async function js_set(r) {
716
const digest = await crypto.subtle.digest('SHA-256', r.headersIn.host);
717
r.setReturnValue(digest);
718
}
719
</example>
720
</tag-desc>
721
722
<tag-name id="r_status"><literal>r.status</literal></tag-name>
723
<tag-desc>
724
status, writable
725
</tag-desc>
726
727
<tag-name id="r_subrequest"><literal>r.subrequest(<value>uri</value>[,
728
<value>options</value>[, <value>callback</value>]])</literal></tag-name>
729
<tag-desc>
730
creates a subrequest with the given <literal>uri</literal> and
731
<literal>options</literal>, and installs
732
an optional completion <literal>callback</literal>.
733
734
<para>
735
A
736
<link doc="../dev/development_guide.xml.xml" id="http_subrequests">subrequest</link>
737
shares its input headers with the client request.
738
To send headers different from original headers to a proxied server, the
739
<link doc="../http/ngx_http_proxy_module.xml" id="proxy_set_header"/>
740
directive can be used.
741
To send a completely new set of headers to a proxied server, the
742
<link doc="../http/ngx_http_proxy_module.xml" id="proxy_pass_request_headers"/>
743
directive can be used.
744
</para>
745
746
<para>
747
If <literal>options</literal> is a string, then it
748
holds the subrequest arguments string.
749
Otherwise, <literal>options</literal> is expected to be
750
an object with the following keys:
751
<list type="tag">
752
<tag-name><literal>args</literal></tag-name>
753
<tag-desc>
754
arguments string, by default an empty string is used
755
</tag-desc>
756
<tag-name><literal>body</literal></tag-name>
757
<tag-desc>
758
request body,
759
by default the request body of the parent request object is used
760
</tag-desc>
761
762
<tag-name><literal>method</literal></tag-name>
763
<tag-desc>
764
HTTP method, by default the <literal>GET</literal> method is used
765
</tag-desc>
766
767
<tag-name><literal>detached</literal></tag-name>
768
<tag-desc>
769
boolean flag (<link doc="changes.xml" id="njs0.3.9">0.3.9</link>),
770
if <literal>true</literal>, the created subrequest is a detached subrequest.
771
Responses to detached subrequests are ignored.
772
Unlike ordinary subrequests, a detached subrequest
773
can be created inside a variable handler.
774
The <literal>detached</literal> flag and callback argument
775
are mutually exclusive.
776
</tag-desc>
777
778
</list>
779
</para>
780
781
<para>
782
The completion <literal>callback</literal> receives
783
a <link id="http">subrequest response object</link> with methods and properties
784
identical to the parent request object.
785
</para>
786
787
<para>
788
Since <link doc="changes.xml" id="njs0.3.8">0.3.8</link>,
789
if a <literal>callback</literal> is not provided,
790
the <literal>Promise</literal> object
791
that resolves to the <link id="http">subrequest response object</link>
792
is returned.
793
</para>
794
795
<para>
796
For example, to view all response headers in the subrequest:
797
<example>
798
async function handler(r) {
799
const reply = await r.subrequest('/path');
800
801
for (const h in reply.headersOut) {
802
r.log(`${h}: ${reply.headersOut[h]}`);
803
}
804
805
r.return(200);
806
}
807
</example>
808
</para>
809
810
</tag-desc>
811
812
<tag-name id="r_uri"><literal>r.uri</literal></tag-name>
813
<tag-desc>
814
current <link doc="../http/ngx_http_core_module.xml" id="var_uri">URI</link>
815
in request,
816
<link doc="../http/ngx_http_core_module.xml" id="location">normalized</link>,
817
read-only
818
</tag-desc>
819
820
<tag-name id="r_raw_variables"><literal>r.rawVariables{}</literal></tag-name>
821
<tag-desc>
822
nginx <link id="r_variables">variables</link> as Buffers,
823
writable
824
(since <link doc="changes.xml" id="njs0.5.0">0.5.0</link>)
825
</tag-desc>
826
827
<tag-name id="r_js_var_names"><literal>r.jsVarNames(<value>[prefix]</value>)</literal></tag-name>
828
<tag-desc>
829
returns a new array of names of nginx variables declared with
830
the <link doc="../http/ngx_http_js_module.xml" id="js_var">js_var</link>
831
directive
832
(since <link doc="changes.xml" id="njs0.9.9">0.9.9</link>).
833
The optional <value>prefix</value> argument must be a string.
834
If it is specified, only names that start with the prefix are returned.
835
</tag-desc>
836
837
<tag-name id="r_variables"><literal>r.variables{}</literal></tag-name>
838
<tag-desc>
839
<link doc="../varindex.xml">nginx variables</link> object, writable
840
(since <link doc="changes.xml" id="njs0.2.8">0.2.8</link>).
841
842
<para>
843
For example, to get the <literal>$foo</literal> variable,
844
one of the following syntax can be used:
845
<example>
846
r.variables['foo']
847
r.variables.foo
848
</example>
849
Since <link doc="changes.xml" id="njs0.8.6">0.8.6</link>,
850
regular expression captures can be accessed using the following syntax:
851
<example>
852
r.variables['1']
853
r.variables[1]
854
</example>
855
</para>
856
857
<para>
858
nginx treats variables referenced in <literal>nginx.conf</literal>
859
and unreferenced variables differently.
860
When a variable is referenced, it may be cacheable,
861
but when it is unreferenced it is always uncacheable.
862
For example, when the
863
<link doc="../http/ngx_http_core_module.xml" id="var_request_id">$request_id</link>
864
variable is only accessed from njs,
865
it has a new value every time it is evaluated.
866
But, when the
867
<link doc="../http/ngx_http_core_module.xml" id="var_request_id">$request_id</link>
868
is referenced, for example:
869
<example>
870
proxy_set_header X-Request-Id $request_id;
871
</example>
872
the <literal>r.variables.request_id</literal> returns the same value every time.
873
</para>
874
875
<para>
876
A variable is writable if:
877
<list type="bullet">
878
879
<listitem>
880
it was created using the <literal>js_var</literal> directive for
881
<link doc="../http/ngx_http_js_module.xml" id="js_var">http</link> or
882
<link doc="../stream/ngx_stream_js_module.xml" id="js_var">stream</link>
883
(since <link doc="changes.xml" id="njs0.5.3">0.5.3</link>)
884
</listitem>
885
886
<listitem>
887
it is referenced in nginx configuration file
888
</listitem>
889
890
</list>
891
Even so, some embedded variables still cannot be assigned a value (for example,
892
<link doc="../http/ngx_http_core_module.xml" id="var_http_"><literal>$http_</literal></link>).
893
</para>
894
</tag-desc>
895
896
<tag-name id="r_warn"><literal>r.warn(<value>string</value>)</literal></tag-name>
897
<tag-desc>
898
writes a <literal>string</literal> to the error log
899
on the <literal>warning</literal> level of logging
900
<para>
901
<note>
902
As nginx has a
903
<link doc="../dev/development_guide.xml" id="logging">hardcoded</link>
904
maximum line length limit,
905
only first 2048 bytes of the string can be logged.
906
</note>
907
</para>
908
</tag-desc>
909
910
</list>
911
</para>
912
913
</section>
914
915
916
<section id="stream" name="Stream Session">
917
918
<para>
919
<table width="100%">
920
<tr><td><link id="s_allow"><literal>s.allow()</literal></link></td></tr>
921
<tr><td><link id="s_decline"><literal>s.decline()</literal></link></td></tr>
922
<tr><td><link id="s_deny"><literal>s.deny()</literal></link></td></tr>
923
<tr><td><link id="s_done"><literal>s.done()</literal></link></td></tr>
924
<tr><td><link id="s_error"><literal>s.error()</literal></link></td></tr>
925
<tr><td><link id="s_js_var_names"><literal>s.jsVarNames()</literal></link></td></tr>
926
<tr><td><link id="s_log"><literal>s.log()</literal></link></td></tr>
927
<tr><td><link id="s_off"><literal>s.off()</literal></link></td></tr>
928
<tr><td><link id="s_on"><literal>s.on()</literal></link></td></tr>
929
<tr><td><link id="s_remote_address"><literal>s.remoteAddress</literal></link></td></tr>
930
<tr><td><link id="s_raw_variables"><literal>s.rawVariables{}</literal></link></td></tr>
931
<tr><td><link id="s_send"><literal>s.send()</literal></link></td></tr>
932
<tr><td><link id="s_send_downstream"><literal>s.sendDownstream()</literal></link></td></tr>
933
<tr><td><link id="s_send_upstream"><literal>s.sendUpstream()</literal></link></td></tr>
934
<tr><td><link id="s_status"><literal>s.status</literal></link></td></tr>
935
<tr><td><link id="s_set_return_value"><literal>s.setReturnValue()</literal></link></td></tr>
936
<tr><td><link id="s_variables"><literal>s.variables{}</literal></link></td></tr>
937
<tr><td><link id="s_warn"><literal>s.warn()</literal></link></td></tr>
938
</table>
939
</para>
940
941
<para>
942
The stream session object is available only in the
943
<link doc="../stream/ngx_stream_js_module.xml">ngx_stream_js_module</link>
944
module.
945
Before <link doc="changes.xml" id="njs0.8.5">0.8.5</link>,
946
all string properties of the object
947
were <link id="string">byte strings</link>.
948
</para>
949
950
<para>
951
<list type="tag">
952
953
<tag-name id="s_allow"><literal>s.allow()</literal></tag-name>
954
<tag-desc>
955
an alias to
956
<link id="s_done"><literal>s.done(0)</literal></link>
957
(<link doc="changes.xml" id="njs0.2.4">0.2.4</link>)
958
</tag-desc>
959
960
<tag-name id="s_decline"><literal>s.decline()</literal></tag-name>
961
<tag-desc>
962
an alias to
963
<link id="s_done"><literal>s.done(-5)</literal></link>
964
(<link doc="changes.xml" id="njs0.2.4">0.2.4</link>)
965
</tag-desc>
966
967
<tag-name id="s_deny"><literal>s.deny()</literal></tag-name>
968
<tag-desc>
969
an alias to
970
<link id="s_done"><literal>s.done(403)</literal></link>
971
(<link doc="changes.xml" id="njs0.2.4">0.2.4</link>)
972
</tag-desc>
973
974
<tag-name id="s_done"><literal>s.done([<value>code</value>]</literal>)</tag-name>
975
<tag-desc>
976
sets an exit <literal>code</literal> for the current
977
<link doc="../stream/stream_processing.xml">phase</link> handler
978
to a code value, by default <literal>0</literal>.
979
The actual finalization happens when the js handler is completed
980
and all pending events, for example, from
981
<link id="ngx_fetch"><literal>ngx.fetch()</literal></link> or
982
<link id="settimeout"><literal>setTimeout()</literal></link>,
983
are processed
984
(<link doc="changes.xml" id="njs0.2.4">0.2.4</link>).
985
<para>
986
Possible code values:
987
<list type="bullet">
988
989
<listitem>
990
<literal>0</literal>&mdash;
991
successful finalization, passing control to the next phase
992
</listitem>
993
994
<listitem>
995
<literal>-5</literal>&mdash;
996
undecided, passing control to the next handler of the current phase (if any)
997
</listitem>
998
999
<listitem>
1000
<literal>403</literal>&mdash;
1001
access is forbidden
1002
</listitem>
1003
1004
</list>
1005
</para>
1006
May be called only from a phase handler function:
1007
<link doc="../stream/ngx_stream_js_module.xml" id="js_access"><literal>js_access</literal></link>
1008
or
1009
<link doc="../stream/ngx_stream_js_module.xml" id="js_preread"><literal>js_preread</literal></link>.
1010
</tag-desc>
1011
1012
<tag-name id="s_error"><literal>s.error(<value>string</value>)</literal></tag-name>
1013
<tag-desc>
1014
writes a sent <literal>string</literal> to the error log
1015
on the <literal>error</literal> level of logging
1016
<para>
1017
<note>
1018
As nginx has a
1019
<link doc="../dev/development_guide.xml" id="logging">hardcoded</link>
1020
maximum line length limit,
1021
only first 2048 bytes of the string can be logged.
1022
</note>
1023
</para>
1024
</tag-desc>
1025
1026
<tag-name id="s_log"><literal>s.log(<value>string</value>)</literal></tag-name>
1027
<tag-desc>
1028
writes a sent <value>string</value> to the error log
1029
on the <literal>info</literal> level of logging
1030
<para>
1031
<note>
1032
As nginx has a
1033
<link doc="../dev/development_guide.xml" id="logging">hardcoded</link>
1034
maximum line length limit,
1035
only first 2048 bytes of the string can be logged.
1036
</note>
1037
</para>
1038
</tag-desc>
1039
1040
<tag-name id="s_off"><literal>s.off(<value>eventName</value>)</literal></tag-name>
1041
<tag-desc>
1042
unregisters the callback set by the <link id="s_on">s.on()</link> method
1043
(<link doc="changes.xml" id="njs0.2.4">0.2.4</link>)
1044
</tag-desc>
1045
1046
<tag-name id="s_on"><literal>s.on(<value>event</value>,
1047
<value>callback</value>)</literal></tag-name>
1048
<tag-desc>
1049
registers a <literal>callback</literal>
1050
for the specified <literal>event</literal>
1051
(<link doc="changes.xml" id="njs0.2.4">0.2.4</link>).
1052
1053
<para>
1054
An <literal>event</literal> may be one of the following strings:
1055
<list type="tag">
1056
<tag-name><literal>upload</literal></tag-name>
1057
<tag-desc>
1058
new data (string) from a client
1059
</tag-desc>
1060
1061
<tag-name><literal>download</literal></tag-name>
1062
<tag-desc>
1063
new data (string) to a client
1064
</tag-desc>
1065
1066
<tag-name><literal>upstream</literal></tag-name>
1067
<tag-desc>
1068
new data (Buffer) from a client
1069
(since <link doc="changes.xml" id="njs0.5.0">0.5.0</link>)
1070
</tag-desc>
1071
1072
<tag-name><literal>downstream</literal></tag-name>
1073
<tag-desc>
1074
new data (Buffer) to a client
1075
(since <link doc="changes.xml" id="njs0.5.0">0.5.0</link>)
1076
</tag-desc>
1077
1078
</list>
1079
</para>
1080
1081
<para>
1082
The completion callback has the following prototype:
1083
<literal>callback(data, flags)</literal>, where
1084
<literal>data</literal> is string or Buffer (depending on the event type)
1085
<literal>flags</literal> is an object
1086
with the following properties:
1087
<list type="tag">
1088
<tag-name id="s_on_callback_last"><literal>last</literal></tag-name>
1089
<tag-desc>
1090
a boolean value, true if data is a last buffer.
1091
</tag-desc>
1092
1093
</list>
1094
</para>
1095
</tag-desc>
1096
1097
<tag-name id="s_remote_address"><literal>s.remoteAddress</literal></tag-name>
1098
<tag-desc>
1099
client address, read-only
1100
</tag-desc>
1101
1102
<tag-name id="s_raw_variables"><literal>s.rawVariables</literal></tag-name>
1103
<tag-desc>
1104
nginx <link id="s_variables">variables</link> as Buffers,
1105
writable
1106
(since <link doc="changes.xml" id="njs0.5.0">0.5.0</link>)
1107
</tag-desc>
1108
1109
<tag-name id="s_js_var_names"><literal>s.jsVarNames(<value>[prefix]</value>)</literal></tag-name>
1110
<tag-desc>
1111
returns a new array of names of nginx variables declared with
1112
the <link doc="../stream/ngx_stream_js_module.xml" id="js_var">js_var</link>
1113
directive
1114
(since <link doc="changes.xml" id="njs0.9.9">0.9.9</link>).
1115
The optional <value>prefix</value> argument must be a string.
1116
If it is specified, only names that start with the prefix are returned.
1117
</tag-desc>
1118
1119
<tag-name id="s_send"><literal>s.send(<value>data</value>[,
1120
<value>options</value>])</literal></tag-name>
1121
<tag-desc>
1122
adds data to the chain of data chunks that will be forwarded in
1123
the forward direction:
1124
in download callback to a client; in upload to an upstream server
1125
(<link doc="changes.xml" id="njs0.2.4">0.2.4</link>).
1126
The actual forwarding happens later,
1127
when the all the data chunks of the current chain are processed.
1128
<para>
1129
The data can be a string or Buffer
1130
(<link doc="changes.xml" id="njs0.5.0">0.5.0</link>).
1131
The <literal>options</literal> is an object used
1132
to override nginx buffer flags derived from an incoming data chunk buffer.
1133
The flags can be overridden with the following flags:
1134
<list type="tag">
1135
1136
<tag-name><literal>last</literal></tag-name>
1137
<tag-desc>
1138
boolean,
1139
true if the buffer is the last buffer
1140
</tag-desc>
1141
1142
<tag-name><literal>flush</literal></tag-name>
1143
<tag-desc>
1144
boolean,
1145
true if the buffer should have the <literal>flush</literal> flag
1146
</tag-desc>
1147
</list>
1148
</para>
1149
The method can be called multiple times per callback invocation.
1150
</tag-desc>
1151
1152
<tag-name id="s_send_downstream"><literal>s.sendDownstream()</literal></tag-name>
1153
<tag-desc>
1154
is identical to <link id="s_send">s.send()</link>,
1155
except for it always sends data to a client
1156
(since <link doc="changes.xml" id="njs0.7.8">0.7.8</link>).
1157
</tag-desc>
1158
1159
<tag-name id="s_send_upstream"><literal>s.sendUpstream()</literal></tag-name>
1160
<tag-desc>
1161
is identical to <link id="s_send">s.send()</link>,
1162
except for it always sends data from a client
1163
(since <link doc="changes.xml" id="njs0.7.8">0.7.8</link>).
1164
</tag-desc>
1165
1166
<tag-name id="s_status"><literal>s.status</literal></tag-name>
1167
<tag-desc>
1168
session status code, an alias to the
1169
<link doc="../stream/ngx_stream_core_module.xml" id="var_status"><literal>$status</literal></link>
1170
variable,
1171
read only
1172
(since <link doc="changes.xml" id="njs0.5.2">0.5.2</link>)
1173
</tag-desc>
1174
1175
<tag-name id="s_set_return_value"><literal>s.setReturnValue(<value>value</value>)</literal></tag-name>
1176
<tag-desc>
1177
sets the return value of the
1178
<link doc="../stream/ngx_stream_js_module.xml" id="js_set"/> handler
1179
(<link doc="changes.xml" id="njs0.7.0">0.7.0</link>).
1180
Unlike an ordinary return statement,
1181
this method should be used when the handler is JS async function.
1182
For example:
1183
<example>
1184
async function js_set(r) {
1185
const digest = await crypto.subtle.digest('SHA-256', r.headersIn.host);
1186
r.setReturnValue(digest);
1187
}
1188
</example>
1189
</tag-desc>
1190
1191
<tag-name id="s_variables"><literal>s.variables{}</literal></tag-name>
1192
<tag-desc>
1193
<link doc="../varindex.xml">nginx variables</link> object, writable
1194
(since <link doc="changes.xml" id="njs0.2.8">0.2.8</link>).
1195
A variable can be writable only
1196
if it is referenced in nginx configuration file.
1197
Even so, some embedded variables still cannot be assigned a value.
1198
</tag-desc>
1199
1200
<tag-name id="s_warn"><literal>s.warn(<value>string</value>)</literal></tag-name>
1201
<tag-desc>
1202
writes a sent <literal>string</literal> to the error log
1203
on the <literal>warning</literal> level of logging
1204
<para>
1205
<note>
1206
As nginx has a
1207
<link doc="../dev/development_guide.xml" id="logging">hardcoded</link>
1208
maximum line length limit,
1209
only first 2048 bytes of the string can be logged.
1210
</note>
1211
</para>
1212
</tag-desc>
1213
1214
</list>
1215
</para>
1216
1217
</section>
1218
1219
1220
<section id="periodic_session" name="Periodic Session">
1221
1222
<para>
1223
<table width="100%">
1224
<tr><td><link id="periodic_session_raw_variables"><literal>PeriodicSession.rawVariables{}</literal></link></td></tr>
1225
<tr><td><link id="periodic_session_variables"><literal>PeriodicSession.variables{}</literal></link></td></tr>
1226
</table>
1227
</para>
1228
1229
<para>
1230
The <literal>Periodic Session</literal> object is provided as the first argument
1231
for the <literal>js_periodic</literal> handler for
1232
<link doc="../http/ngx_http_js_module.xml" id="js_periodic">http</link>
1233
and
1234
<link doc="../stream/ngx_stream_js_module.xml" id="js_periodic">stream</link>
1235
(since <link doc="changes.xml" id="njs0.8.1">0.8.1</link>).
1236
</para>
1237
1238
<para>
1239
<list type="tag">
1240
1241
<tag-name id="periodic_session_raw_variables"><literal>PeriodicSession.rawVariables{}</literal></tag-name>
1242
<tag-desc>
1243
nginx <link id="periodic_session_variables">variables</link> as Buffers,
1244
writable.
1245
</tag-desc>
1246
1247
<tag-name id="periodic_session_variables"><literal>PeriodicSession.variables{}</literal></tag-name>
1248
<tag-desc>
1249
<link doc="../varindex.xml">nginx variables</link> object, writable.
1250
</tag-desc>
1251
1252
</list>
1253
</para>
1254
1255
</section>
1256
1257
1258
<section id="headers" name="Headers">
1259
1260
<para>
1261
<table width="100%">
1262
<tr><td><link id="headers_constructor"><literal>Headers()</literal></link></td></tr>
1263
<tr><td><link id="headers_append"><literal>Headers.append()</literal></link></td></tr>
1264
<tr><td><link id="headers_delete"><literal>Headers.delete()</literal></link></td></tr>
1265
<tr><td><link id="headers_get"><literal>Headers.get()</literal></link></td></tr>
1266
<tr><td><link id="headers_getall"><literal>Headers.getAll()</literal></link></td></tr>
1267
<tr><td><link id="headers_foreach"><literal>Headers.forEach()</literal></link></td></tr>
1268
<tr><td><link id="headers_has"><literal>Headers.has()</literal></link></td></tr>
1269
<tr><td><link id="headers_set"><literal>Headers.set()</literal></link></td></tr>
1270
</table>
1271
</para>
1272
1273
<para>
1274
The <literal>Headers</literal> interface of the
1275
<link id="ngx_fetch"><literal>Fetch API</literal></link>
1276
is available since <link doc="changes.xml" id="njs0.5.1">0.5.1</link>.
1277
</para>
1278
1279
<para>
1280
A new <literal>Headers</literal> object can be created using the
1281
<link id="headers_constructor"><literal>Headers()</literal></link> constructor:
1282
(since <link doc="changes.xml" id="njs0.7.10">0.7.10</link>):
1283
1284
<list type="tag">
1285
1286
<tag-name id="headers_constructor"><literal>Headers([<value>init</value>])</literal></tag-name>
1287
<tag-desc>
1288
1289
<list type="tag">
1290
1291
<tag-name><literal>init</literal></tag-name>
1292
<tag-desc>
1293
An object containing HTTP headers for
1294
prepopulating the <literal>Headers</literal> object,
1295
can be a <literal>string</literal>,
1296
an <literal>array</literal> of name-value pairs,
1297
or an existing <literal>Headers</literal> object.
1298
</tag-desc>
1299
1300
</list>
1301
</tag-desc>
1302
1303
</list>
1304
</para>
1305
1306
<para>
1307
A new <literal>Headers</literal> object can be created
1308
with the following properties and methods:
1309
1310
<list type="tag">
1311
1312
<tag-name id="headers_append"><literal>append()</literal></tag-name>
1313
<tag-desc>
1314
Appends a new value into an existing header in the
1315
<literal>Headers</literal> object,
1316
or adds the header if it does not already exist
1317
(since <link doc="changes.xml" id="njs0.7.10">0.7.10</link>).
1318
</tag-desc>
1319
1320
<tag-name id="headers_delete"><literal>delete()</literal></tag-name>
1321
<tag-desc>
1322
Deletes a header from the <literal>Headers</literal> object
1323
(since <link doc="changes.xml" id="njs0.7.10">0.7.10</link>).
1324
</tag-desc>
1325
1326
<tag-name id="headers_get"><literal>get()</literal></tag-name>
1327
<tag-desc>
1328
Returns a string containing the values of all headers with the specified name
1329
separated by a comma and a space.
1330
</tag-desc>
1331
1332
<tag-name id="headers_getall"><literal>getAll(<value>name</value>)</literal></tag-name>
1333
<tag-desc>
1334
Returns an array containing the values of all headers with the specified name.
1335
</tag-desc>
1336
1337
<tag-name id="headers_foreach"><literal>forEach()</literal></tag-name>
1338
<tag-desc>
1339
Executes a provided function once for each key/value pair
1340
in the <literal>Headers</literal> object
1341
(since <link doc="changes.xml" id="njs0.7.10">0.7.10</link>).
1342
</tag-desc>
1343
1344
<tag-name id="headers_has"><literal>has()</literal></tag-name>
1345
<tag-desc>
1346
Returns a boolean value
1347
indicating whether a header with the specified name exists.
1348
</tag-desc>
1349
1350
<tag-name id="headers_set"><literal>set()</literal></tag-name>
1351
<tag-desc>
1352
Sets a new value for an existing header inside
1353
the <literal>Headers</literal> object,
1354
or adds the header if it does not already exist
1355
(since <link doc="changes.xml" id="njs0.7.10">0.7.10</link>).
1356
</tag-desc>
1357
1358
</list>
1359
</para>
1360
1361
</section>
1362
1363
1364
<section id="request" name="Request">
1365
1366
<para>
1367
<table width="100%">
1368
<tr><td><link id="request_constructor"><literal>Request()</literal></link></td></tr>
1369
<tr><td><link id="request_arraybuffer"><literal>Request.arrayBuffer()</literal></link></td></tr>
1370
<tr><td><link id="request_bodyused"><literal>Request.bodyUsed</literal></link></td></tr>
1371
<tr><td><link id="request_cache"><literal>Request.cache</literal></link></td></tr>
1372
<tr><td><link id="request_credentials"><literal>Request.credentials</literal></link></td></tr>
1373
<tr><td><link id="request_headers"><literal>Request.headers</literal></link></td></tr>
1374
<tr><td><link id="request_json"><literal>Request.json()</literal></link></td></tr>
1375
<tr><td><link id="request_method"><literal>Request.method</literal></link></td></tr>
1376
<tr><td><link id="request_mode"><literal>Request.mode</literal></link></td></tr>
1377
<tr><td><link id="request_text"><literal>Request.text()</literal></link></td></tr>
1378
<tr><td><link id="request_url"><literal>Request.url</literal></link></td></tr>
1379
</table>
1380
</para>
1381
1382
<para>
1383
The <literal>Request</literal> interface of the
1384
<link id="ngx_fetch"><literal>Fetch API</literal></link>
1385
is available since <link doc="changes.xml" id="njs0.7.10">0.7.10</link>.
1386
</para>
1387
1388
<para>
1389
A new <literal>Request</literal> object can be created using the
1390
<link id="request_constructor"><literal>Request()</literal></link> constructor:
1391
1392
<list type="tag">
1393
1394
<tag-name id="request_constructor"><literal>Request[<value>resource</value>[,
1395
<value>options</value>]])</literal></tag-name>
1396
<tag-desc>
1397
1398
Creates a <literal>Request</literal> object to fetch
1399
that can be passed later to
1400
<link id="ngx_fetch"><literal>ngx.fetch()</literal></link>.
1401
The <literal>resource</literal> can be a URL
1402
or an existing <literal>Request</literal> object.
1403
The <literal>options</literal> is an optional argument
1404
that is expected to be an object with the following keys:
1405
1406
<list type="tag">
1407
1408
<tag-name><literal>body</literal></tag-name>
1409
<tag-desc>
1410
The request body, by default is empty.
1411
</tag-desc>
1412
1413
<tag-name><literal>headers</literal></tag-name>
1414
<tag-desc>
1415
The response headers object&mdash;
1416
the object containing HTTP headers for
1417
prepopulating the <link id="headers"><literal>Headers</literal></link> object,
1418
can be a <literal>string</literal>,
1419
an <literal>array</literal> of name-value pairs,
1420
or an existing <link id="headers"><literal>Headers</literal></link> object.
1421
</tag-desc>
1422
1423
<tag-name><literal>method</literal></tag-name>
1424
<tag-desc>
1425
The HTTP method, by default the GET method is used.
1426
</tag-desc>
1427
1428
</list>
1429
</tag-desc>
1430
1431
</list>
1432
</para>
1433
1434
<para>
1435
A new <literal>Request</literal> object can be created
1436
with the following properties and methods:
1437
1438
<list type="tag">
1439
1440
<tag-name id="request_arraybuffer"><literal>arrayBuffer()</literal></tag-name>
1441
<tag-desc>
1442
Returns a <literal>Promise</literal> that resolves with
1443
an <literal>ArrayBuffer</literal>.
1444
</tag-desc>
1445
1446
<tag-name id="request_bodyused"><literal>bodyUsed</literal></tag-name>
1447
<tag-desc>
1448
A boolean value, <literal>true</literal>
1449
if the body was used in the request.
1450
</tag-desc>
1451
1452
<tag-name id="request_cache"><literal>cache</literal></tag-name>
1453
<tag-desc>
1454
Contains the cache mode of the request.
1455
</tag-desc>
1456
1457
<tag-name id="request_credentials"><literal>credentials</literal></tag-name>
1458
<tag-desc>
1459
Contains the credentials of the request,
1460
by default is <literal>same-origin</literal>.
1461
</tag-desc>
1462
1463
<tag-name id="request_headers"><literal>headers</literal></tag-name>
1464
<tag-desc>
1465
The <link id="headers"><literal>Headers</literal></link> read-only object
1466
associated with the
1467
<link id="request"><literal>Request</literal></link>.
1468
</tag-desc>
1469
1470
<tag-name id="request_json"><literal>json()</literal></tag-name>
1471
<tag-desc>
1472
Returns a <literal>Promise</literal> that resolves with
1473
the result of parsing the request body as JSON.
1474
</tag-desc>
1475
1476
<tag-name id="request_method"><literal>method</literal></tag-name>
1477
<tag-desc>
1478
Contains the request method.
1479
</tag-desc>
1480
1481
<tag-name id="request_mode"><literal>mode</literal></tag-name>
1482
<tag-desc>
1483
Contains the mode of the request.
1484
</tag-desc>
1485
1486
<tag-name id="request_text"><literal>text()</literal></tag-name>
1487
<tag-desc>
1488
Returns a <literal>Promise</literal> that resolves with a
1489
string representation of the request body.
1490
</tag-desc>
1491
1492
<tag-name id="request_url"><literal>url</literal></tag-name>
1493
<tag-desc>
1494
Contains the URL of the request.
1495
</tag-desc>
1496
1497
</list>
1498
</para>
1499
1500
</section>
1501
1502
1503
<section id="response" name="Response">
1504
1505
<para>
1506
<table width="100%">
1507
<tr><td><link id="response_constructor"><literal>Response()</literal></link></td></tr>
1508
<tr><td><link id="response_arraybuffer"><literal>Response.arrayBuffer()</literal></link></td></tr>
1509
<tr><td><link id="response_bodyused"><literal>Response.bodyUsed</literal></link></td></tr>
1510
<tr><td><link id="response_headers"><literal>Response.headers</literal></link></td></tr>
1511
<tr><td><link id="response_json"><literal>Response.json()</literal></link></td></tr>
1512
<tr><td><link id="response_ok"><literal>Response.ok</literal></link></td></tr>
1513
<tr><td><link id="response_redirect"><literal>Response.redirected</literal></link></td></tr>
1514
<tr><td><link id="response_status"><literal>Response.status</literal></link></td></tr>
1515
<tr><td><link id="response_statustext"><literal>Response.statusText</literal></link></td></tr>
1516
<tr><td><link id="response_text"><literal>Response.text()</literal></link></td></tr>
1517
<tr><td><link id="response_type"><literal>Response.type</literal></link></td></tr>
1518
<tr><td><link id="response_url"><literal>Response.url</literal></link></td></tr>
1519
</table>
1520
</para>
1521
1522
<para>
1523
The <literal>Response</literal> interface is available since
1524
<link doc="changes.xml" id="njs0.5.1">0.5.1</link>.
1525
</para>
1526
1527
<para>
1528
A new <literal>Response</literal> object can be created using the
1529
<link id="response_constructor"><literal>Response()</literal></link> constructor
1530
(since <link doc="changes.xml" id="njs0.7.10">0.7.10</link>):
1531
1532
<list type="tag">
1533
1534
<tag-name id="response_constructor"><literal>Response[<value>body</value>[,
1535
<value>options</value>]])</literal></tag-name>
1536
<tag-desc>
1537
Creates a <literal>Response</literal> object.
1538
The <literal>body</literal> is an optional argument,
1539
can be a <literal>string</literal> or a <literal>buffer</literal>,
1540
by default is <literal>null</literal>.
1541
The <literal>options</literal> is an optional argument
1542
that is expected to be an object with the following keys:
1543
1544
<list type="tag">
1545
1546
<tag-name><literal>headers</literal></tag-name>
1547
<tag-desc>
1548
The response headers object&mdash;
1549
the object containing HTTP headers for
1550
prepopulating the <link id="headers"><literal>Headers</literal></link> object,
1551
can be a <literal>string</literal>,
1552
an <literal>array</literal> of name-value pairs,
1553
or an existing <link id="headers"><literal>Headers</literal></link> object.
1554
</tag-desc>
1555
1556
<tag-name><literal>status</literal></tag-name>
1557
<tag-desc>
1558
The status code of the response.
1559
</tag-desc>
1560
1561
<tag-name><literal>statusText</literal></tag-name>
1562
<tag-desc>
1563
The status message corresponding to the status code.
1564
</tag-desc>
1565
1566
</list>
1567
</tag-desc>
1568
1569
</list>
1570
</para>
1571
1572
<para>
1573
A new <literal>Response()</literal> object can be created
1574
with the following properties and methods:
1575
1576
<list type="tag">
1577
1578
<tag-name id="response_arraybuffer"><literal>arrayBuffer()</literal></tag-name>
1579
<tag-desc>
1580
Takes a <literal>Response</literal> stream and reads it to completion.
1581
Returns a <literal>Promise</literal> that resolves with
1582
an <literal>ArrayBuffer</literal>.
1583
</tag-desc>
1584
1585
<tag-name id="response_bodyused"><literal>bodyUsed</literal></tag-name>
1586
<tag-desc>
1587
A boolean value, <literal>true</literal>
1588
if the body was read.
1589
</tag-desc>
1590
1591
<tag-name id="response_headers"><literal>headers</literal></tag-name>
1592
<tag-desc>
1593
The <link id="headers"><literal>Headers</literal></link> read-only object
1594
associated with the
1595
<link id="response"><literal>Response</literal></link>.
1596
</tag-desc>
1597
1598
<tag-name id="response_json"><literal>json()</literal></tag-name>
1599
<tag-desc>
1600
Takes a <literal>Response</literal> stream and reads it to completion.
1601
Returns a <literal>Promise</literal> that resolves with
1602
the result of parsing the body text as JSON.
1603
</tag-desc>
1604
1605
<tag-name id="response_ok"><literal>ok</literal></tag-name>
1606
<tag-desc>
1607
A boolean value, <literal>true</literal>
1608
if the response was successful (status codes between 200–299).
1609
</tag-desc>
1610
1611
<tag-name id="response_redirect"><literal>redirected</literal></tag-name>
1612
<tag-desc>
1613
A boolean value, <literal>true</literal>
1614
if the response is the result of a redirect.
1615
</tag-desc>
1616
1617
<tag-name id="response_status"><literal>status</literal></tag-name>
1618
<tag-desc>
1619
The status code of the response.
1620
</tag-desc>
1621
1622
<tag-name id="response_statustext"><literal>statusText</literal></tag-name>
1623
<tag-desc>
1624
The status message corresponding to the status code.
1625
</tag-desc>
1626
1627
<tag-name id="response_text"><literal>text()</literal></tag-name>
1628
<tag-desc>
1629
Takes a <literal>Response</literal> stream and reads it to completion.
1630
Returns a <literal>Promise</literal> that resolves with a string.
1631
</tag-desc>
1632
1633
<tag-name id="response_type"><literal>type</literal></tag-name>
1634
<tag-desc>
1635
The type of the response.
1636
</tag-desc>
1637
1638
<tag-name id="response_url"><literal>url</literal></tag-name>
1639
<tag-desc>
1640
The URL of the response.
1641
</tag-desc>
1642
1643
</list>
1644
</para>
1645
1646
</section>
1647
1648
1649
<section id="ngx" name="ngx">
1650
1651
<para>
1652
<table width="100%">
1653
<tr><td><link id="ngx_build"><literal>ngx.build</literal></link></td></tr>
1654
<tr><td><link id="ngx_conf_file_path"><literal>ngx.conf_file_path</literal></link></td></tr>
1655
<tr><td><link id="ngx_conf_prefix"><literal>ngx.conf_prefix</literal></link></td></tr>
1656
<tr><td><link id="ngx_error_log_path"><literal>ngx.error_log_path</literal></link></td></tr>
1657
<tr><td><link id="ngx_fetch"><literal>ngx.fetch()</literal></link></td></tr>
1658
<tr><td><link id="ngx_log"><literal>ngx.log()</literal></link></td></tr>
1659
<tr><td><link id="ngx_prefix"><literal>ngx.prefix</literal></link></td></tr>
1660
<tr><td><link id="ngx_version"><literal>ngx.version</literal></link></td></tr>
1661
<tr><td><link id="ngx_version_number"><literal>ngx.version_number</literal></link></td></tr>
1662
<tr><td><link id="ngx_worker_id"><literal>ngx.worker_id</literal></link></td></tr>
1663
</table>
1664
</para>
1665
1666
<para>
1667
The <literal>ngx</literal> global object is available
1668
since <link doc="changes.xml" id="njs0.5.0">0.5.0</link>.
1669
<list type="tag">
1670
1671
<tag-name id="ngx_build"><literal>ngx.build</literal></tag-name>
1672
<tag-desc>
1673
a string containing an optional nginx build name, corresponds to the
1674
<link doc="../configure.xml" id="build"><literal>--build=name</literal></link>
1675
argument
1676
of the <link doc="../configure.xml">configure</link> script,
1677
by default is <literal>""</literal>
1678
(<link doc="changes.xml" id="njs0.8.0">0.8.0</link>)
1679
</tag-desc>
1680
1681
<tag-name id="ngx_conf_file_path"><literal>ngx.conf_file_path</literal></tag-name>
1682
<tag-desc>
1683
a string containing the file path to current nginx configuration file
1684
(<link doc="changes.xml" id="njs0.8.0">0.8.0</link>)
1685
</tag-desc>
1686
1687
<tag-name id="ngx_conf_prefix"><literal>ngx.conf_prefix</literal></tag-name>
1688
<tag-desc>
1689
a string containing the file path to
1690
<link doc="../configure.xml" id="conf_path">nginx configuration prefix</link>&mdash;
1691
the directory where nginx is currently looking for configuration
1692
(<link doc="changes.xml" id="njs0.7.8">0.7.8</link>)
1693
</tag-desc>
1694
1695
<tag-name id="ngx_error_log_path"><literal>ngx.error_log_path</literal></tag-name>
1696
<tag-desc>
1697
a string containing the file path to the current
1698
<link doc="../ngx_core_module.xml" id="error_log">error log</link> file
1699
(<link doc="changes.xml" id="njs0.8.0">0.8.0</link>)
1700
</tag-desc>
1701
1702
<tag-name id="ngx_fetch"><literal>ngx.fetch(<value>resource</value>,
1703
[<value>options</value>])</literal></tag-name>
1704
<tag-desc>
1705
<para>
1706
Makes a request to fetch a <value>resource</value>
1707
(<link doc="changes.xml" id="njs0.5.1">0.5.1</link>), which can be an
1708
URL or the <link id="request"><literal>Request</literal></link> object
1709
(<link doc="changes.xml" id="njs0.7.10">0.7.10</link>).
1710
Returns a <literal>Promise</literal> that resolves with
1711
the <link id="response"><literal>Response</literal></link> object.
1712
Since <link doc="changes.xml" id="njs0.7.0">0.7.0</link>,
1713
the <literal>https://</literal> scheme is supported,
1714
redirects are not handled.
1715
</para>
1716
1717
<para>
1718
If the URL in the <value>resource</value> is specified as a domain name,
1719
it is determined using a
1720
<link doc="../http/ngx_http_core_module.xml" id="resolver"/>.
1721
If the <literal>https://</literal> scheme is specified, the
1722
<link doc="../http/ngx_http_js_module.xml" id="js_fetch_trusted_certificate"/>
1723
directive should be configured
1724
for the authentication of the <value>resource</value>'s HTTPS server.
1725
</para>
1726
1727
<para>
1728
The <literal>options</literal> parameter is expected to be an object
1729
with the following keys:
1730
<list type="tag">
1731
1732
<tag-name id="fetch_body"><literal>body</literal></tag-name>
1733
<tag-desc>
1734
request body,
1735
by default is empty
1736
</tag-desc>
1737
1738
<tag-name id="fetch_buffer_size"><literal>buffer_size</literal></tag-name>
1739
<tag-desc>
1740
the buffer size for reading the response,
1741
by default is <literal>4096</literal>
1742
</tag-desc>
1743
1744
<tag-name id="fetch_headers"><literal>headers</literal></tag-name>
1745
<tag-desc>
1746
request <link id="headers">headers</link> object
1747
</tag-desc>
1748
1749
<tag-name id="fetch_get"><literal>max_response_body_size</literal></tag-name>
1750
<tag-desc>
1751
the maximum size of the response body in bytes,
1752
by default is <literal>32768</literal>
1753
</tag-desc>
1754
1755
<tag-name id="fetch_method"><literal>method</literal></tag-name>
1756
<tag-desc>
1757
HTTP method,
1758
by default the <literal>GET</literal> method is used
1759
</tag-desc>
1760
1761
<tag-name id="fetch_verify"><literal>verify</literal></tag-name>
1762
<tag-desc>
1763
enables or disables verification of the HTTPS server certificate,
1764
by default is <literal>true</literal>
1765
(<link doc="changes.xml" id="njs0.7.0">0.7.0</link>)
1766
</tag-desc>
1767
1768
</list>
1769
Example:
1770
<example>
1771
let reply = await ngx.fetch('http://nginx.org/');
1772
let body = await reply.text();
1773
1774
r.return(200, body);
1775
</example>
1776
</para>
1777
</tag-desc>
1778
1779
<tag-name id="ngx_log"><literal>ngx.log</literal>(<value>level</value>,
1780
<value>message</value>)</tag-name>
1781
<tag-desc>
1782
writes a message to the error log with the specified level of logging.
1783
The <value>level</value> parameter specifies one of the log levels,
1784
the <value>message</value> parameter can be a string or Buffer.
1785
The following log levels can be specified:
1786
<literal>ngx.INFO</literal>,
1787
<literal>ngx.WARN</literal>, and
1788
<literal>ngx.ERR</literal>.
1789
<para>
1790
<note>
1791
As nginx has a
1792
<link doc="../dev/development_guide.xml" id="logging">hardcoded</link>
1793
maximum line length limit,
1794
only first 2048 bytes of the string can be logged.
1795
</note>
1796
</para>
1797
</tag-desc>
1798
1799
<tag-name id="ngx_prefix"><literal>ngx.prefix</literal></tag-name>
1800
<tag-desc>
1801
a string containing the file path to
1802
<link doc="../configure.xml" id="prefix">nginx prefix</link>&mdash;
1803
a directory that keeps server files
1804
(<link doc="changes.xml" id="njs0.8.0">0.8.0</link>)
1805
</tag-desc>
1806
1807
<tag-name id="ngx_version"><literal>ngx.version</literal></tag-name>
1808
<tag-desc>
1809
a string containing nginx version,
1810
for example: <literal>1.25.0</literal>
1811
(<link doc="changes.xml" id="njs0.8.0">0.8.0</link>)
1812
</tag-desc>
1813
1814
<tag-name id="ngx_version_number"><literal>ngx.version_number</literal></tag-name>
1815
<tag-desc>
1816
a number containing nginx version,
1817
for example: <literal>1025000</literal>
1818
(<link doc="changes.xml" id="njs0.8.0">0.8.0</link>)
1819
</tag-desc>
1820
1821
<tag-name id="ngx_worker_id"><literal>ngx.worker_id</literal></tag-name>
1822
<tag-desc>
1823
a number that corresponds to nginx internal worker id,
1824
the value is between <literal>0</literal> and the value specified in the
1825
<link doc="../ngx_core_module.xml" id="worker_processes"/> directive
1826
(<link doc="changes.xml" id="njs0.8.0">0.8.0</link>)
1827
</tag-desc>
1828
1829
</list>
1830
</para>
1831
1832
</section>
1833
1834
<section id="ngx_shared" name="ngx.shared">
1835
1836
<para>
1837
The <literal>ngx.shared</literal> global object is available
1838
since <link doc="changes.xml" id="njs0.8.0">0.8.0</link>.
1839
</para>
1840
1841
1842
<section id="dict" name="SharedDict">
1843
<para>
1844
<table width="100%">
1845
<tr><td><link id="dict_add"><literal>ngx.shared.SharedDict.add()</literal></link></td></tr>
1846
<tr><td><link id="dict_capacity"><literal>ngx.shared.SharedDict.capacity</literal></link></td></tr>
1847
<tr><td><link id="dict_clear"><literal>ngx.shared.SharedDict.clear()</literal></link></td></tr>
1848
<tr><td><link id="dict_delete"><literal>ngx.shared.SharedDict.delete()</literal></link></td></tr>
1849
<tr><td><link id="dict_freespace"><literal>ngx.shared.SharedDict.freeSpace()</literal></link></td></tr>
1850
<tr><td><link id="dict_get"><literal>ngx.shared.SharedDict.get()</literal></link></td></tr>
1851
<tr><td><link id="dict_has"><literal>ngx.shared.SharedDict.has()</literal></link></td></tr>
1852
<tr><td><link id="dict_incr"><literal>ngx.shared.SharedDict.incr()</literal></link></td></tr>
1853
<tr><td><link id="dict_items"><literal>ngx.shared.SharedDict.items()</literal></link></td></tr>
1854
<tr><td><link id="dict_keys"><literal>ngx.shared.SharedDict.keys()</literal></link></td></tr>
1855
<tr><td><link id="dict_name"><literal>ngx.shared.SharedDict.name</literal></link></td></tr>
1856
<tr><td><link id="dict_pop"><literal>ngx.shared.SharedDict.pop()</literal></link></td></tr>
1857
<tr><td><link id="dict_replace"><literal>ngx.shared.SharedDict.replace()</literal></link></td></tr>
1858
<tr><td><link id="dict_set"><literal>ngx.shared.SharedDict.set()</literal></link></td></tr>
1859
<tr><td><link id="dict_size"><literal>ngx.shared.SharedDict.size()</literal></link></td></tr>
1860
<tr><td><link id="dict_ttl"><literal>ngx.shared.SharedDict.ttl()</literal></link></td></tr>
1861
<tr><td><link id="dict_type"><literal>ngx.shared.SharedDict.type</literal></link></td></tr>
1862
</table>
1863
</para>
1864
1865
<para>
1866
The shared dictionary object is available
1867
since <link doc="changes.xml" id="njs0.8.0">0.8.0</link>.
1868
The shared dictionary name, type, and size
1869
are set with the <literal>js_shared_dict_zone</literal> directive in
1870
<link doc="../http/ngx_http_js_module.xml" id="js_shared_dict_zone">http</link>
1871
or
1872
<link doc="../stream/ngx_stream_js_module.xml" id="js_shared_dict_zone">stream</link>.
1873
</para>
1874
1875
<para>
1876
A <literal>SharedDict()</literal> object
1877
has the following properties and methods:
1878
<list type="tag">
1879
1880
<tag-name id="dict_add"><literal>ngx.shared.SharedDict.add(<value>key</value>,
1881
<value>value</value> [,<value>timeout</value>])</literal></tag-name>
1882
<tag-desc>
1883
Sets the <literal>value</literal>
1884
for the specified <literal>key</literal> in the dictionary
1885
only if the key does not exist yet.
1886
The <literal>key</literal> is a string representing
1887
the key of the item to add,
1888
the <literal>value</literal> is the value of the item to add.
1889
<para>
1890
The optional <literal>timeout</literal> argument is specified in milliseconds
1891
and overrides the <literal>timeout</literal> parameter of the
1892
<literal>js_shared_dict_zone</literal> directive in
1893
<link doc="../http/ngx_http_js_module.xml" id="js_shared_dict_zone">http</link>
1894
or
1895
<link doc="../stream/ngx_stream_js_module.xml" id="js_shared_dict_zone">stream</link>
1896
(since <link doc="changes.xml" id="njs0.8.5">0.8.5</link>).
1897
It can be useful when some keys are expected to have unique timeouts.
1898
</para>
1899
1900
<para>
1901
Returns <literal>true</literal> if the value has been successfully added
1902
to the <literal>SharedDict</literal> dictionary,
1903
<literal>false</literal> if the key already exists in the dictionary.
1904
Throws <literal>SharedMemoryError</literal> if
1905
there is not enough free space in the <literal>SharedDict</literal> dictionary.
1906
Throws <literal>TypeError</literal> if the <literal>value</literal> is
1907
of a different type than expected by this dictionary.
1908
</para>
1909
</tag-desc>
1910
1911
<tag-name id="dict_capacity"><literal>ngx.shared.SharedDict.capacity</literal></tag-name>
1912
<tag-desc>
1913
Returns the capacity of the <literal>SharedDict</literal> dictionary,
1914
corresponds to the <literal>size</literal> parameter of
1915
<literal>js_shared_dict_zone</literal> directive in
1916
<link doc="../http/ngx_http_js_module.xml" id="js_shared_dict_zone">http</link>
1917
or
1918
<link doc="../stream/ngx_stream_js_module.xml" id="js_shared_dict_zone">stream</link>.
1919
</tag-desc>
1920
1921
<tag-name id="dict_clear"><literal>ngx.shared.SharedDict.clear()</literal></tag-name>
1922
<tag-desc>
1923
Removes all items from the <literal>SharedDict</literal> dictionary.
1924
</tag-desc>
1925
1926
<tag-name id="dict_delete"><literal>ngx.shared.SharedDict.delete(<value>key</value>)</literal></tag-name>
1927
<tag-desc>
1928
Removes the item associated with the specified key
1929
from the <literal>SharedDict</literal> dictionary,
1930
<literal>true</literal> if the item in the dictionary existed and was removed,
1931
<literal>false</literal> otherwise.
1932
</tag-desc>
1933
1934
<tag-name id="dict_freespace"><literal>ngx.shared.SharedDict.freeSpace()</literal></tag-name>
1935
<tag-desc>
1936
Returns the free page size in bytes.
1937
If the size is zero, the <literal>SharedDict</literal> dictionary
1938
will still accept new values if there is space in the occupied pages.
1939
</tag-desc>
1940
1941
<tag-name id="dict_get"><literal>ngx.shared.SharedDict.get(<value>key</value>)</literal></tag-name>
1942
<tag-desc>
1943
Retrieves the item by its <literal>key</literal>,
1944
returns the value associated with the <literal>key</literal>
1945
or <literal>undefined</literal> if there is none.
1946
</tag-desc>
1947
1948
<tag-name id="dict_has"><literal>ngx.shared.SharedDict.has(<value>key</value>)</literal></tag-name>
1949
<tag-desc>
1950
Searches for an item by its <literal>key</literal>,
1951
returns <literal>true</literal> if such item exists or
1952
<literal>false</literal> otherwise.
1953
</tag-desc>
1954
1955
<tag-name id="dict_incr"><literal>ngx.shared.SharedDict.incr(<value>key</value>,<value>delta</value>[[,<value>init</value>], <value>timeout</value>]))</literal></tag-name>
1956
<tag-desc>
1957
Increments the integer value associated with the <literal>key</literal>
1958
by <literal>delta</literal>.
1959
The <literal>key</literal> is a string,
1960
the <literal>delta</literal> is the number
1961
to increment or decrement the value by.
1962
If the key does not exist,
1963
the item will be initialized to an optional <literal>init</literal> argument,
1964
by default is <literal>0</literal>.
1965
1966
<para>
1967
The optional <literal>timeout</literal> argument is specified in milliseconds
1968
and overrides the <literal>timeout</literal> parameter of the
1969
<literal>js_shared_dict_zone</literal> directive in
1970
<link doc="../http/ngx_http_js_module.xml" id="js_shared_dict_zone">http</link>
1971
or
1972
<link doc="../stream/ngx_stream_js_module.xml" id="js_shared_dict_zone">stream</link>
1973
(since <link doc="changes.xml" id="njs0.8.5">0.8.5</link>).
1974
If the <literal>timeout</literal> argument is not specified,
1975
the existing per-entry TTL is preserved
1976
(since <link doc="changes.xml" id="njs0.9.7">0.9.7</link>).
1977
Before version <link doc="changes.xml" id="njs0.9.7">0.9.7</link>,
1978
omitting <literal>timeout</literal> would reset the entry expiry
1979
to the directive default.
1980
</para>
1981
1982
<para>
1983
Returns the new value.
1984
Throws <literal>SharedMemoryError</literal> if
1985
there is not enough free space in the <literal>SharedDict</literal> dictionary.
1986
Throws <literal>TypeError</literal> if this dictionary does not expect numbers.
1987
</para>
1988
1989
<para>
1990
<note>
1991
This method can be used only if the dictionary type was declared with
1992
<literal>type=number</literal> parameter of the
1993
<literal>js_shared_dict_zone</literal> directive in
1994
<link doc="../http/ngx_http_js_module.xml" id="js_shared_dict_zone">http</link>
1995
or
1996
<link doc="../stream/ngx_stream_js_module.xml" id="js_shared_dict_zone">stream</link>.
1997
</note>
1998
</para>
1999
</tag-desc>
2000
2001
<tag-name id="dict_items"><literal>ngx.shared.SharedDict.items([<value>maxCount</value>])</literal></tag-name>
2002
<tag-desc>
2003
Returns an array of the <literal>SharedDict</literal> dictionary
2004
key-value items (since <link doc="changes.xml" id="njs0.8.1">0.8.1</link>).
2005
The <literal>maxCount</literal> parameter
2006
sets maximum number of items to retrieve,
2007
by default is <literal>1024</literal>.
2008
2009
</tag-desc>
2010
2011
<tag-name id="dict_keys"><literal>ngx.shared.SharedDict.keys([<value>maxCount</value>])</literal></tag-name>
2012
<tag-desc>
2013
Returns an array of the <literal>SharedDict</literal> dictionary keys.
2014
The <literal>maxCount</literal> parameter
2015
sets maximum number of keys to retrieve,
2016
by default is <literal>1024</literal>.
2017
</tag-desc>
2018
2019
<tag-name id="dict_name"><literal>ngx.shared.SharedDict.name</literal></tag-name>
2020
<tag-desc>
2021
Returns the name of the <literal>SharedDict</literal> dictionary,
2022
corresponds to the <literal>zone=</literal> parameter of
2023
<literal>js_shared_dict_zone</literal> directive in
2024
<link doc="../http/ngx_http_js_module.xml" id="js_shared_dict_zone">http</link>
2025
or
2026
<link doc="../stream/ngx_stream_js_module.xml" id="js_shared_dict_zone">stream</link>.
2027
</tag-desc>
2028
2029
<tag-name id="dict_pop"><literal>ngx.shared.SharedDict.pop(<value>key</value>)</literal></tag-name>
2030
<tag-desc>
2031
Removes the item associated with the specified <literal>key</literal>
2032
from the <literal>SharedDict</literal> dictionary,
2033
returns the value associated with the <literal>key</literal>
2034
or <literal>undefined</literal> if there is none.
2035
</tag-desc>
2036
2037
<tag-name id="dict_replace"><literal>ngx.shared.SharedDict.replace(<value>key</value>,
2038
<value>value</value>)</literal></tag-name>
2039
<tag-desc>
2040
Replaces the <literal>value</literal>
2041
for the specified <literal>key</literal> only if the key already exists,
2042
returns <literal>true</literal> if the value was successfully replaced,
2043
<literal>false</literal> if the key does not exist
2044
in the <literal>SharedDict</literal> dictionary.
2045
Throws <literal>SharedMemoryError</literal> if
2046
there is not enough free space in the <literal>SharedDict</literal> dictionary.
2047
Throws <literal>TypeError</literal> if the <literal>value</literal> is
2048
of a different type than expected by this dictionary.
2049
</tag-desc>
2050
2051
<tag-name id="dict_set"><literal>ngx.shared.SharedDict.set(<value>key</value>,
2052
<value>value</value> [,<value>timeout</value>])</literal></tag-name>
2053
<tag-desc>
2054
Sets the <literal>value</literal> for the specified <literal>key</literal>,
2055
returns this <literal>SharedDict</literal> dictionary (for method chaining).
2056
2057
<para>
2058
The optional <literal>timeout</literal> argument is specified in milliseconds
2059
and overrides the <literal>timeout</literal> parameter of the
2060
<literal>js_shared_dict_zone</literal> directive in
2061
<link doc="../http/ngx_http_js_module.xml" id="js_shared_dict_zone">http</link>
2062
or
2063
<link doc="../stream/ngx_stream_js_module.xml" id="js_shared_dict_zone">stream</link>
2064
(since <link doc="changes.xml" id="njs0.8.5">0.8.5</link>).
2065
It can be useful when some keys are expected to have unique timeouts.
2066
</para>
2067
</tag-desc>
2068
2069
<tag-name id="dict_size"><literal>ngx.shared.SharedDict.size()</literal></tag-name>
2070
<tag-desc>
2071
Returns the number of items for the <literal>SharedDict</literal> dictionary.
2072
</tag-desc>
2073
2074
<tag-name id="dict_ttl"><literal>ngx.shared.SharedDict.ttl(<value>key</value>)</literal></tag-name>
2075
<tag-desc>
2076
Returns the remaining time-to-live in milliseconds
2077
for the specified <literal>key</literal>,
2078
or <literal>undefined</literal> if the key does not exist
2079
or has expired
2080
(since <link doc="changes.xml" id="njs0.9.7">0.9.7</link>).
2081
2082
<para>
2083
Throws <literal>TypeError</literal> if the dictionary was declared
2084
without the <literal>timeout</literal> parameter of the
2085
<literal>js_shared_dict_zone</literal> directive in
2086
<link doc="../http/ngx_http_js_module.xml" id="js_shared_dict_zone">http</link>
2087
or
2088
<link doc="../stream/ngx_stream_js_module.xml" id="js_shared_dict_zone">stream</link>.
2089
</para>
2090
</tag-desc>
2091
2092
<tag-name id="dict_type"><literal>ngx.shared.SharedDict.type</literal></tag-name>
2093
<tag-desc>
2094
Returns <literal>string</literal> or <literal>number</literal> that
2095
corresponds to the <literal>SharedDict</literal> dictionary type
2096
set by the <literal>type=</literal> parameter of
2097
<literal>js_shared_dict_zone</literal> directive in
2098
<link doc="../http/ngx_http_js_module.xml" id="js_shared_dict_zone">http</link>
2099
or
2100
<link doc="../stream/ngx_stream_js_module.xml" id="js_shared_dict_zone">stream</link>.
2101
</tag-desc>
2102
2103
</list>
2104
</para>
2105
2106
</section>
2107
2108
</section>
2109
2110
</section>
2111
2112
2113
<section id="builtin_objects" name="built-in objects">
2114
2115
2116
<section id="console" name="console">
2117
2118
<para>
2119
<table width="100%">
2120
<tr><td><link id="console_error"><literal>console.error()</literal></link></td></tr>
2121
<tr><td><link id="console_info"><literal>console.info()</literal></link></td></tr>
2122
<tr><td><link id="console_log"><literal>console.log()</literal></link></td></tr>
2123
<tr><td><link id="console_time"><literal>console.time()</literal></link></td></tr>
2124
<tr><td><link id="console_time_end"><literal>console.timeEnd()</literal></link></td></tr>
2125
<tr><td><link id="console_warn"><literal>console.warn()</literal></link></td></tr>
2126
</table>
2127
</para>
2128
2129
<para>
2130
The <literal>console</literal> object is available
2131
in nginx since <link doc="changes.xml" id="njs0.8.2">0.8.2</link>,
2132
in CLI since <link doc="changes.xml" id="njs0.2.6">0.2.6</link>.
2133
<list type="tag">
2134
2135
<tag-name id="console_error"><literal>console.error(<value>msg</value>[, <value>msg2</value> ...])</literal></tag-name>
2136
<tag-desc>
2137
Outputs one or more error messages.
2138
The message may be a string or an object.
2139
</tag-desc>
2140
2141
<tag-name id="console_info"><literal>console.info(<value>msg</value>[, <value>msg2</value> ...])</literal></tag-name>
2142
<tag-desc>
2143
Outputs one or more info messages.
2144
The message may be a string or an object.
2145
</tag-desc>
2146
2147
<tag-name id="console_log"><literal>console.log(<value>msg</value>[, <value>msg2</value> ...])</literal></tag-name>
2148
<tag-desc>
2149
Outputs one or more log messages.
2150
The message may be a string or an object.
2151
</tag-desc>
2152
2153
<tag-name id="console_time"><literal>console.time(<value>label</value>)</literal></tag-name>
2154
<tag-desc>
2155
Starts a timer that can track how long an operation takes.
2156
The <literal>label</literal> parameter allows naming different timers.
2157
If <link id="console_time_end"><literal>console.timeEnd()</literal></link>
2158
with the same name is called,
2159
the time that elapsed since the timer was started will be output,
2160
in milliseconds.
2161
</tag-desc>
2162
2163
<tag-name id="console_time_end"><literal>console.timeEnd(<value>label</value>)</literal></tag-name>
2164
<tag-desc>
2165
Stops a timer previously started by
2166
<link id="console_time"><literal>console.time()</literal></link>
2167
The <literal>label</literal> parameter allows naming different timers.
2168
</tag-desc>
2169
2170
<tag-name id="console_warn"><literal>console.warn(<value>msg</value>[, <value>msg2</value> ...])</literal></tag-name>
2171
<tag-desc>
2172
Outputs one or more warning messages.
2173
The message may be a string or an object.
2174
</tag-desc>
2175
2176
</list>
2177
</para>
2178
2179
</section>
2180
2181
2182
<section id="builtin_crypto" name="crypto">
2183
2184
<para>
2185
<table width="100%">
2186
<tr><td><link id="crypto_get_random_values"><literal>сrypto.getRandomValues()</literal></link></td></tr>
2187
<tr><td><link id="crypto_random_uuid"><literal>сrypto.randomUUID()</literal></link></td></tr>
2188
<tr><td><link id="crypto_subtle_encrypt"><literal>сrypto.subtle.encrypt()</literal></link></td></tr>
2189
<tr><td><link id="crypto_subtle_decrypt"><literal>сrypto.subtle.decrypt()</literal></link></td></tr>
2190
<tr><td><link id="crypto_subtle_derive_bits"><literal>сrypto.subtle.deriveBits()</literal></link></td></tr>
2191
<tr><td><link id="crypto_subtle_derive_key"><literal>сrypto.subtle.deriveKey()</literal></link></td></tr>
2192
<tr><td><link id="crypto_subtle_digest"><literal>сrypto.subtle.digest()</literal></link></td></tr>
2193
<tr><td><link id="crypto_subtle_export_key"><literal>сrypto.subtle.exportKey()</literal></link></td></tr>
2194
<tr><td><link id="crypto_subtle_generate_key"><literal>сrypto.subtle.generateKey()</literal></link></td></tr>
2195
<tr><td><link id="crypto_subtle_import_key"><literal>сrypto.subtle.importKey()</literal></link></td></tr>
2196
<tr><td><link id="crypto_subtle_sign"><literal>сrypto.subtle.sign()</literal></link></td></tr>
2197
<tr><td><link id="crypto_subtle_verify"><literal>сrypto.subtle.verify()</literal></link></td></tr>
2198
<tr><td><link id="crypto_subtle_wrap_key"><literal>сrypto.subtle.wrapKey()</literal></link></td></tr>
2199
<tr><td><link id="crypto_subtle_unwrap_key"><literal>сrypto.subtle.unwrapKey()</literal></link></td></tr>
2200
</table>
2201
</para>
2202
2203
<para>
2204
The <literal>crypto</literal> object is a global object
2205
that allows using cryptographic functionality
2206
(since <link doc="changes.xml" id="njs0.7.0">0.7.0</link>).
2207
Since <link doc="changes.xml" id="njs0.9.7">0.9.7</link>,
2208
the full
2209
<link url="https://www.w3.org/TR/webcrypto-2/">Web Crypto API</link>
2210
specification is supported.
2211
</para>
2212
2213
<para>
2214
<list type="tag">
2215
2216
<tag-name id="crypto_get_random_values"><literal>сrypto.getRandomValues</literal>(<link id="crypto_get_random_values_array"><literal>typedArray</literal></link>)</tag-name>
2217
<tag-desc>
2218
Gets cryptographically strong random values.
2219
Returns the same array passed as <literal>typedArray</literal>
2220
but with its contents replaced with the newly generated random numbers.
2221
Possible values:
2222
2223
<list type="tag">
2224
<tag-name id="crypto_get_random_values_array"><literal>typedArray</literal></tag-name>
2225
<tag-desc>
2226
can be
2227
<literal>Int8Array</literal>,
2228
<literal>Int16Array</literal>,
2229
<literal>Uint16Array</literal>,
2230
<literal>Int32Array</literal>, or
2231
<literal>Uint32Array</literal>
2232
</tag-desc>
2233
</list>
2234
2235
</tag-desc>
2236
2237
<tag-name id="crypto_random_uuid"><literal>сrypto.randomUUID</literal>()</tag-name>
2238
<tag-desc>
2239
Returns a randomly generated, 36 character long v4 UUID string,
2240
for example <literal>"36b8f84d-df4e-4d49-b662-bcde71a8764f"</literal>
2241
(since <link doc="changes.xml" id="njs0.9.7">0.9.7</link>).
2242
</tag-desc>
2243
2244
<tag-name id="crypto_subtle_encrypt"><literal>сrypto.subtle.encrypt</literal>(<link id="crypto_encrypt_alg"><literal>algorithm</literal></link>,
2245
<link id="crypto_encrypt_key"><literal>key</literal></link>,
2246
<link id="crypto_encrypt_data"><literal>data</literal></link>)</tag-name>
2247
<tag-desc>
2248
Encrypts <link id="crypto_encrypt_data"><literal>data</literal></link>
2249
using the provided
2250
<link id="crypto_encrypt_algorithm"><literal>algorithm</literal></link> and
2251
<link id="crypto_encrypt_key"><literal>key</literal></link>.
2252
Returns a <literal>Promise</literal> that fulfills with
2253
an <literal>ArrayBuffer</literal> containing the ciphertext.
2254
Possible values:
2255
2256
<list type="tag">
2257
<tag-name id="crypto_encrypt_alg"><literal>algorithm</literal></tag-name>
2258
<tag-desc>
2259
an object that specifies
2260
the algorithm to be used and any extra parameters if required:
2261
2262
<list type="bullet">
2263
<listitem id="rsa_oaep_params">
2264
for <literal>RSA-OAEP</literal>,
2265
pass the object with the following keys:
2266
2267
<list type="bullet">
2268
2269
<listitem>
2270
<literal>name</literal> is a string,
2271
should be set to <literal>RSA-OAEP</literal>:
2272
<para>
2273
<example>
2274
crypto.subtle.encrypt({name: "RSA-OAEP"}, key, data)
2275
</example>
2276
</para>
2277
</listitem>
2278
</list>
2279
2280
</listitem>
2281
2282
<listitem id="aes_ctr_params">
2283
for <literal>AES-CTR</literal>,
2284
pass the object with the following keys:
2285
2286
<list type="bullet">
2287
2288
<listitem>
2289
<literal>name</literal> is a string,
2290
should be set to <literal>AES-CTR</literal>
2291
</listitem>
2292
2293
<listitem>
2294
<literal>counter</literal> is an
2295
<literal>ArrayBuffer</literal>,
2296
<literal>TypedArray</literal>, or
2297
<literal>DataView</literal>
2298
the initial value of the counter block,
2299
must be 16 bytes long (the AES block size).
2300
The rightmost length bits of this block are used for the counter,
2301
and the rest is used for the nonce.
2302
For example, if length is set to 64,
2303
then the first half of counter is the nonce
2304
and the second half is used for the counter
2305
</listitem>
2306
2307
<listitem>
2308
<literal>length</literal> is the number of bits in the counter block
2309
that are used for the actual counter.
2310
The counter must be big enough that it doesn't wrap.
2311
</listitem>
2312
</list>
2313
2314
</listitem>
2315
2316
<listitem id="aes_cbc_params">
2317
for <literal>AES-CBC</literal>, pass the object with the following keys:
2318
2319
<list type="bullet">
2320
2321
<listitem>
2322
<literal>name</literal> is a string,
2323
should be set to <literal>AES-CBC</literal>
2324
</listitem>
2325
2326
<listitem>
2327
<literal>iv</literal> or the initialization vector, is an
2328
<literal>ArrayBuffer</literal>,
2329
<literal>TypedArray</literal>, or
2330
<literal>DataView</literal>,
2331
must be 16 bytes, unpredictable,
2332
and preferably cryptographically random.
2333
However, it need not be secret,
2334
for example, it may be transmitted unencrypted along with the ciphertext.
2335
</listitem>
2336
</list>
2337
2338
</listitem>
2339
2340
<listitem id="aes_gcm_params">
2341
for <literal>AES-GCM</literal>, pass the object with the following keys:
2342
2343
<list type="bullet">
2344
2345
<listitem>
2346
<literal>name</literal> is a string,
2347
should be set to <literal>AES-GCM</literal>
2348
</listitem>
2349
2350
<listitem>
2351
<literal>iv</literal> or the initialization vector, is an
2352
<literal>ArrayBuffer</literal>,
2353
<literal>TypedArray</literal>, or
2354
<literal>DataView</literal>,
2355
must be 16 bytes,
2356
and must be unique for every encryption operation carried out with a given key
2357
</listitem>
2358
2359
<listitem>
2360
<literal>additionalData</literal> (optional) is an
2361
<literal>ArrayBuffer</literal>,
2362
<literal>TypedArray</literal>, or
2363
<literal>DataView</literal>
2364
that contains additional data that
2365
will not be encrypted but will be authenticated along with the encrypted data.
2366
If <literal>additionalData</literal> is specified,
2367
then the same data must be specified in the corresponding call to
2368
<literal>decrypt()</literal>:
2369
if the data given to the <literal>decrypt()</literal> call
2370
does not match the original data,
2371
the decryption will throw an exception.
2372
The bit length of <literal>additionalData</literal>
2373
must be smaller than <literal>2^64 - 1</literal>.
2374
</listitem>
2375
2376
<listitem>
2377
<literal>tagLength</literal> (optional, default is <literal>128</literal>) -
2378
a <literal>number</literal> that determines the size in bits
2379
of the authentication tag generated in the encryption operation
2380
and used for authentication in the corresponding decryption
2381
Possible values:
2382
<literal>32</literal>,
2383
<literal>64</literal>,
2384
<literal>96</literal>,
2385
<literal>104</literal>,
2386
<literal>112</literal>,
2387
<literal>120</literal>, or
2388
<literal>128</literal>.
2389
The AES-GCM specification recommends that it should be
2390
<literal>96</literal>,
2391
<literal>104</literal>,
2392
<literal>112</literal>,
2393
<literal>120</literal>, or
2394
<literal>128</literal>,
2395
although
2396
<literal>32</literal> or
2397
<literal>64</literal>
2398
bits may be acceptable in some applications.
2399
</listitem>
2400
</list>
2401
2402
</listitem>
2403
</list>
2404
2405
</tag-desc>
2406
2407
<tag-name id="crypto_encrypt_key"><literal>key</literal></tag-name>
2408
<tag-desc>
2409
a <link id="cryptokey"><literal>CryptoKey</literal></link> that contains
2410
the key to be used for encryption
2411
</tag-desc>
2412
2413
<tag-name id="crypto_encrypt_data"><literal>data</literal></tag-name>
2414
<tag-desc>
2415
an
2416
<literal>ArrayBuffer</literal>,
2417
<literal>TypedArray</literal>, or
2418
<literal>DataView</literal>
2419
that contains
2420
the data to be encrypted (also known as the plaintext)
2421
</tag-desc>
2422
</list>
2423
2424
</tag-desc>
2425
2426
<tag-name id="crypto_subtle_decrypt"><literal>сrypto.subtle.decrypt</literal>(<link id="crypto_decrypt_alg"><literal>algorithm</literal></link>,
2427
<link id="crypto_decrypt_key"><literal>key</literal></link>,
2428
<link id="crypto_decrypt_data"><literal>data</literal></link>)</tag-name>
2429
<tag-desc>
2430
Decrypts encrypted data.
2431
Returns a <literal>Promise</literal> with the decrypted data.
2432
Possible values:
2433
2434
<list type="tag">
2435
2436
<tag-name id="crypto_decrypt_alg"><literal>algorithm</literal></tag-name>
2437
<tag-desc>
2438
an object
2439
that specifies the algorithm to be used, and any extra parameters as required.
2440
The values given for the extra parameters must match
2441
those passed into the corresponding <literal>encrypt()</literal> call.
2442
2443
<list type="bullet">
2444
<listitem>
2445
for <literal>RSA-OAEP</literal>,
2446
pass the object with the following keys:
2447
2448
<list type="bullet">
2449
2450
<listitem>
2451
<literal>name</literal> is a string,
2452
should be set to <literal>RSA-OAEP</literal>:
2453
<para>
2454
<example>
2455
crypto.subtle.encrypt({name: "RSA-OAEP"}, key, data)
2456
</example>
2457
</para>
2458
</listitem>
2459
</list>
2460
</listitem>
2461
2462
<listitem>
2463
for <literal>AES-CTR</literal>,
2464
pass the object with the following keys:
2465
2466
<list type="bullet">
2467
2468
<listitem>
2469
<literal>name</literal> is a string,
2470
should be set to <literal>AES-CTR</literal>
2471
</listitem>
2472
2473
<listitem>
2474
<literal>counter</literal> is an
2475
<literal>ArrayBuffer</literal>,
2476
<literal>TypedArray</literal>, or
2477
<literal>DataView</literal>
2478
the initial value of the counter block,
2479
must be 16 bytes long (the AES block size).
2480
The rightmost length bits of this block are used for the counter,
2481
and the rest is used for the nonce.
2482
For example, if length is set to 64,
2483
then the first half of counter is the nonce
2484
and the second half is used for the counter.
2485
</listitem>
2486
2487
<listitem>
2488
<literal>length</literal> is the number of bits in the counter block
2489
that are used for the actual counter.
2490
The counter must be big enough that it doesn't wrap.
2491
</listitem>
2492
</list>
2493
2494
</listitem>
2495
2496
<listitem>
2497
for <literal>AES-CBC</literal>, pass the object with the following keys:
2498
2499
<list type="bullet">
2500
2501
<listitem>
2502
<literal>name</literal> is a string,
2503
should be set to <literal>AES-CBC</literal>
2504
</listitem>
2505
2506
<listitem>
2507
<literal>iv</literal> or the initialization vector, is an
2508
<literal>ArrayBuffer</literal>,
2509
<literal>TypedArray</literal>, or
2510
<literal>DataView</literal>,
2511
must be 16 bytes, unpredictable,
2512
and preferably cryptographically random.
2513
However, it need not be secret
2514
(for example, it may be transmitted unencrypted along with the ciphertext).
2515
</listitem>
2516
</list>
2517
2518
</listitem>
2519
2520
<listitem>
2521
for <literal>AES-GCM</literal>, pass the object with the following keys:
2522
2523
<list type="bullet">
2524
2525
<listitem>
2526
<literal>name</literal> is a string,
2527
should be set to <literal>AES-GCM</literal>
2528
</listitem>
2529
2530
<listitem>
2531
<literal>iv</literal> or the initialization vector, is an
2532
<literal>ArrayBuffer</literal>,
2533
<literal>TypedArray</literal>, or
2534
<literal>DataView</literal>,
2535
must be 16 bytes,
2536
and must be unique for every encryption operation carried out with a given key
2537
</listitem>
2538
2539
<listitem>
2540
<literal>additionalData</literal> (optional) is an
2541
<literal>ArrayBuffer</literal>,
2542
<literal>TypedArray</literal>, or
2543
<literal>DataView</literal>
2544
that contains additional data that
2545
will not be encrypted but will be authenticated along with the encrypted data.
2546
If <literal>additionalData</literal> is specified,
2547
then the same data must be specified in the corresponding call to
2548
<literal>decrypt()</literal>:
2549
if the data given to the <literal>decrypt()</literal> call
2550
does not match the original data,
2551
the decryption will throw an exception.
2552
The bit length of <literal>additionalData</literal>
2553
must be smaller than <literal>2^64 - 1</literal>.
2554
</listitem>
2555
2556
<listitem>
2557
<literal>tagLength</literal> (optional, default is <literal>128</literal>) -
2558
a <literal>number</literal> that determines the size in bits
2559
of the authentication tag generated in the encryption operation
2560
and used for authentication in the corresponding decryption.
2561
Possible values:
2562
<literal>32</literal>,
2563
<literal>64</literal>,
2564
<literal>96</literal>,
2565
<literal>104</literal>,
2566
<literal>112</literal>,
2567
<literal>120</literal>, or
2568
<literal>128</literal>.
2569
The AES-GCM specification recommends that it should be
2570
<literal>96</literal>,
2571
<literal>104</literal>,
2572
<literal>112</literal>,
2573
<literal>120</literal>, or
2574
<literal>128</literal>,
2575
although
2576
<literal>32</literal> or
2577
<literal>64</literal>
2578
bits may be acceptable in some applications.
2579
</listitem>
2580
</list>
2581
2582
</listitem>
2583
</list>
2584
2585
</tag-desc>
2586
2587
<tag-name id="crypto_decrypt_key"><literal>key</literal></tag-name>
2588
<tag-desc>
2589
a <link id="cryptokey"><literal>CryptoKey</literal></link>
2590
that contains the key to be used for decryption.
2591
If <literal>RSA-OAEP</literal> is used, this is the
2592
<literal>privateKey</literal> property of the
2593
<link id="cryptokeypair"><literal>CryptoKeyPair</literal></link> object.
2594
</tag-desc>
2595
2596
<tag-name id="crypto_decrypt_data"><literal>data</literal></tag-name>
2597
<tag-desc>
2598
an
2599
<literal>ArrayBuffer</literal>,
2600
<literal>TypedArray</literal>, or
2601
<literal>DataView</literal>
2602
that contains the data to be decrypted (also known as ciphertext)
2603
</tag-desc>
2604
</list>
2605
2606
</tag-desc>
2607
2608
<tag-name id="crypto_subtle_derive_bits"><literal>сrypto.subtle.deriveBits</literal>(<link id="crypto_derive_bits_alg"><literal>algorithm</literal></link>,
2609
<link id="crypto_derive_bits_basekey"><literal>baseKey</literal></link>,
2610
<link id="crypto_derive_bits_length"><literal>length</literal></link>)</tag-name>
2611
<tag-desc>
2612
Derives an array of bits from a base key.
2613
Returns a <literal>Promise</literal>
2614
which will be fulfilled with an
2615
<literal>ArrayBuffer</literal> that contains the derived bits.
2616
Possible values:
2617
2618
<list type="tag">
2619
<tag-name id="crypto_derive_bits_alg"><literal>algorithm</literal></tag-name>
2620
<tag-desc>
2621
is an object that defines the derivation algorithm to use:
2622
2623
<list type="bullet">
2624
<listitem id="hkdf_params">
2625
for <literal>HKDF</literal>,
2626
pass the object with the following keys:
2627
2628
<list type="bullet">
2629
2630
<listitem>
2631
<literal>name</literal> is a string,
2632
should be set to <literal>HKDF</literal>
2633
</listitem>
2634
2635
<listitem>
2636
<literal>hash</literal> is a string with the digest algorithm to use:
2637
<literal>SHA-1</literal>,
2638
<literal>SHA-256</literal>,
2639
<literal>SHA-384</literal>, or
2640
<literal>SHA-512</literal>
2641
</listitem>
2642
2643
<listitem>
2644
<literal>salt</literal> is an
2645
<literal>ArrayBuffer</literal>,
2646
<literal>TypedArray</literal>, or
2647
<literal>DataView</literal>
2648
that represents random or pseudo-random value
2649
with the same length as the output of the <literal>digest</literal> function.
2650
Unlike the input key material passed into <literal>deriveKey()</literal>,
2651
salt does not need to be kept secret.
2652
</listitem>
2653
2654
<listitem>
2655
<literal>info</literal> is an
2656
<literal>ArrayBuffer</literal>,
2657
<literal>TypedArray</literal>, or
2658
<literal>DataView</literal>
2659
that represents application-specific contextual information
2660
used to bind the derived key to an application or context,
2661
and enables deriving different keys for different contexts
2662
while using the same input key material.
2663
This property is required but may be an empty buffer.
2664
</listitem>
2665
</list>
2666
2667
</listitem>
2668
2669
<listitem id="pbkdf2_params">
2670
for <literal>PBKDF2</literal>,
2671
pass the object with the following keys:
2672
2673
<list type="bullet">
2674
2675
<listitem>
2676
<literal>name</literal> is a string,
2677
should be set to <literal>PBKDF2</literal>
2678
</listitem>
2679
2680
<listitem>
2681
<literal>hash</literal> is a string with the digest algorithm to use:
2682
<literal>SHA-1</literal>,
2683
<literal>SHA-256</literal>,
2684
<literal>SHA-384</literal>, or
2685
<literal>SHA-512</literal>
2686
</listitem>
2687
2688
<listitem>
2689
<literal>salt</literal> is an
2690
<literal>ArrayBuffer</literal>,
2691
<literal>TypedArray</literal>, or
2692
<literal>DataView</literal>
2693
that represents random or pseudo-random value
2694
of at least <literal>16</literal> bytes.
2695
Unlike the input key material passed into <literal>deriveKey()</literal>,
2696
salt does not need to be kept secret.
2697
</listitem>
2698
2699
<listitem>
2700
<literal>iterations</literal> is a <literal>number</literal>
2701
that represents the number of times the hash function will be executed
2702
in <literal>deriveKey()</literal>
2703
</listitem>
2704
</list>
2705
2706
</listitem>
2707
2708
<listitem id="ecdh_params">
2709
for <literal>ECDH</literal>,
2710
pass the object with the following keys
2711
(since <link doc="changes.xml" id="njs0.9.1">0.9.1</link>):
2712
2713
<list type="bullet">
2714
2715
<listitem>
2716
<literal>name</literal> is a string,
2717
should be set to <literal>ECDH</literal>
2718
</listitem>
2719
2720
<listitem>
2721
<literal>public</literal> is a <link id="cryptokey"><literal>CryptoKey</literal></link>
2722
that represents the public key of the other party.
2723
The key must be generated using the same curve as the base key.
2724
</listitem>
2725
</list>
2726
2727
</listitem>
2728
2729
<listitem id="x25519_params">
2730
for <literal>X25519</literal>,
2731
pass the object with the following keys
2732
(since <link doc="changes.xml" id="njs0.9.7">0.9.7</link>):
2733
2734
<list type="bullet">
2735
2736
<listitem>
2737
<literal>name</literal> is a string,
2738
should be set to <literal>X25519</literal>
2739
</listitem>
2740
2741
<listitem>
2742
<literal>public</literal> is a <link id="cryptokey"><literal>CryptoKey</literal></link>
2743
that represents the public key of the other party
2744
</listitem>
2745
</list>
2746
2747
</listitem>
2748
</list>
2749
2750
</tag-desc>
2751
2752
<tag-name id="crypto_derive_bits_basekey"><literal>baseKey</literal></tag-name>
2753
<tag-desc>
2754
is a <link id="cryptokey"><literal>CryptoKey</literal></link>
2755
that represents the input to the derivation algorithm
2756
- the initial key material for the derivation function:
2757
for example, for <literal>PBKDF2</literal> it might be a password,
2758
imported as a <link id="cryptokey"><literal>CryptoKey</literal></link> using
2759
<link id="crypto_subtle_import_key"><literal>сrypto.subtle.importKey()</literal></link>
2760
</tag-desc>
2761
2762
<tag-name id="crypto_derive_bits_length"><literal>length</literal></tag-name>
2763
<tag-desc>
2764
is a number representing the number of bits to derive.
2765
For browsers compatibility,
2766
the number should be a multiple of <literal>8</literal>
2767
</tag-desc>
2768
</list>
2769
2770
</tag-desc>
2771
2772
<tag-name id="crypto_subtle_derive_key"><literal>сrypto.subtle.deriveKey</literal>(<link id="crypto_derive_key_alg"><literal>algorithm</literal></link>,
2773
<link id="crypto_derive_key_basekey"><literal>baseKey</literal></link>,
2774
<link id="crypto_derive_key_derivedkeyalg"><literal>derivedKeyAlgorithm</literal></link>,
2775
<link id="crypto_derive_key_extractable"><literal>extractable</literal></link>,
2776
<link id="crypto_derive_key_keyusages"><literal>keyUsages</literal></link>)</tag-name>
2777
<tag-desc>
2778
Derives a secret key from a master key.
2779
Possible values:
2780
2781
<list type="tag">
2782
<tag-name id="crypto_derive_key_alg"><literal>algorithm</literal></tag-name>
2783
<tag-desc>
2784
is an object that defines the derivation algorithm to use:
2785
2786
<list type="bullet">
2787
<listitem>
2788
for <literal>HKDF</literal>,
2789
pass the object with the following keys:
2790
2791
<list type="bullet">
2792
2793
<listitem>
2794
<literal>name</literal> is a string,
2795
should be set to <literal>HKDF</literal>
2796
</listitem>
2797
2798
<listitem>
2799
<literal>hash</literal> is a string with the digest algorithm to use:
2800
<literal>SHA-1</literal>,
2801
<literal>SHA-256</literal>,
2802
<literal>SHA-384</literal>, or
2803
<literal>SHA-512</literal>
2804
</listitem>
2805
2806
<listitem>
2807
<literal>salt</literal> is an
2808
<literal>ArrayBuffer</literal>,
2809
<literal>TypedArray</literal>, or
2810
<literal>DataView</literal>
2811
that represents random or pseudo-random value
2812
with the same length as the output of the <literal>digest</literal> function.
2813
Unlike the input key material passed into <literal>deriveKey()</literal>,
2814
salt does not need to be kept secret.
2815
</listitem>
2816
2817
<listitem>
2818
<literal>info</literal> is an
2819
<literal>ArrayBuffer</literal>,
2820
<literal>TypedArray</literal>, or
2821
<literal>DataView</literal>
2822
that represents application-specific contextual information
2823
used to bind the derived key to an application or context,
2824
and enables deriving different keys for different contexts
2825
while using the same input key material.
2826
This property is required but may be an empty buffer.
2827
</listitem>
2828
</list>
2829
2830
</listitem>
2831
2832
<listitem>
2833
for <literal>PBKDF2</literal>,
2834
pass the object with the following keys:
2835
2836
<list type="bullet">
2837
2838
<listitem>
2839
<literal>name</literal> is a string,
2840
should be set to <literal>PBKDF2</literal>
2841
</listitem>
2842
2843
<listitem>
2844
<literal>hash</literal> is a string with the digest algorithm to use:
2845
<literal>SHA-1</literal>,
2846
<literal>SHA-256</literal>,
2847
<literal>SHA-384</literal>, or
2848
<literal>SHA-512</literal>
2849
</listitem>
2850
2851
<listitem>
2852
<literal>salt</literal> is an
2853
<literal>ArrayBuffer</literal>,
2854
<literal>TypedArray</literal>, or
2855
<literal>DataView</literal>
2856
that represents random or pseudo-random value
2857
of at least <literal>16</literal> bytes.
2858
Unlike the input key material passed into <literal>deriveKey()</literal>,
2859
salt does not need to be kept secret.
2860
</listitem>
2861
2862
<listitem>
2863
<literal>iterations</literal> is a <literal>number</literal>
2864
that represents the number of times the hash function will be executed
2865
in <literal>deriveKey()</literal>
2866
</listitem>
2867
</list>
2868
2869
</listitem>
2870
2871
<listitem>
2872
for <literal>ECDH</literal>,
2873
pass the object with the following keys
2874
(since <link doc="changes.xml" id="njs0.9.1">0.9.1</link>):
2875
2876
<list type="bullet">
2877
2878
<listitem>
2879
<literal>name</literal> is a string,
2880
should be set to <literal>ECDH</literal>
2881
</listitem>
2882
2883
<listitem>
2884
<literal>public</literal> is a <link id="cryptokey"><literal>CryptoKey</literal></link>
2885
that represents the public key of the other party.
2886
The key must be generated using the same curve as the base key.
2887
</listitem>
2888
</list>
2889
2890
</listitem>
2891
2892
<listitem>
2893
for <literal>X25519</literal>,
2894
pass the object with the following keys
2895
(since <link doc="changes.xml" id="njs0.9.7">0.9.7</link>):
2896
2897
<list type="bullet">
2898
2899
<listitem>
2900
<literal>name</literal> is a string,
2901
should be set to <literal>X25519</literal>
2902
</listitem>
2903
2904
<listitem>
2905
<literal>public</literal> is a <link id="cryptokey"><literal>CryptoKey</literal></link>
2906
that represents the public key of the other party
2907
</listitem>
2908
</list>
2909
2910
</listitem>
2911
</list>
2912
2913
</tag-desc>
2914
2915
<tag-name id="crypto_derive_key_basekey"><literal>baseKey</literal></tag-name>
2916
<tag-desc>
2917
is a <link id="cryptokey"><literal>CryptoKey</literal></link>
2918
that represents the input to the derivation algorithm
2919
- the initial key material for the derivation function:
2920
for example, for <literal>PBKDF2</literal> it might be a password,
2921
imported as a <link id="cryptokey"><literal>CryptoKey</literal></link> using
2922
<link id="crypto_subtle_import_key"><literal>сrypto.subtle.importKey()</literal></link>.
2923
</tag-desc>
2924
2925
<tag-name id="crypto_derive_key_derivedkeyalg"><literal>derivedKeyAlgorithm</literal></tag-name>
2926
<tag-desc>
2927
is an object
2928
that defines the algorithm the derived key will be used for:
2929
2930
<list type="bullet">
2931
<listitem>
2932
for <literal>HMAC</literal>,
2933
pass the object with the following keys:
2934
2935
<list type="bullet">
2936
2937
<listitem>
2938
<literal>name</literal> is a string,
2939
should be set to <literal>HMAC</literal>
2940
</listitem>
2941
2942
<listitem>
2943
<literal>hash</literal> is a string with the name of the digest function to use:
2944
<literal>SHA-1</literal>,
2945
<literal>SHA-256</literal>,
2946
<literal>SHA-384</literal>, or
2947
<literal>SHA-512</literal>
2948
</listitem>
2949
2950
<listitem>
2951
<literal>length</literal> (optional) is a <literal>number</literal>
2952
that represents the length in bits of the key.
2953
If not specified, the length of the key is equal to
2954
the block size of the chozen hash function
2955
</listitem>
2956
</list>
2957
2958
</listitem>
2959
2960
<listitem>
2961
for
2962
<literal>AES-CTR</literal>,
2963
<literal>AES-CBC</literal>,
2964
<literal>AES-GCM</literal>, or
2965
<literal>AES-KW</literal>
2966
(since <link doc="changes.xml" id="njs0.9.7">0.9.7</link>),
2967
pass the object with the following keys:
2968
2969
<list type="bullet">
2970
2971
<listitem>
2972
<literal>name</literal> is a string,
2973
should be set to
2974
<literal>AES-CTR</literal>,
2975
<literal>AES-CBC</literal>,
2976
<literal>AES-GCM</literal>, or
2977
<literal>AES-KW</literal>,
2978
depending on the algorithm used
2979
</listitem>
2980
2981
<listitem>
2982
<literal>length</literal> is a <literal>number</literal> that represents
2983
the length in bits of the key to generate:
2984
<literal>128</literal>,
2985
<literal>192</literal>, or
2986
<literal>256</literal>
2987
</listitem>
2988
</list>
2989
2990
</listitem>
2991
</list>
2992
2993
</tag-desc>
2994
2995
<tag-name id="crypto_derive_key_extractable"><literal>extractable</literal></tag-name>
2996
<tag-desc>
2997
is a boolean value
2998
that indicates whether it will be possible to export the key
2999
</tag-desc>
3000
3001
<tag-name id="crypto_derive_key_keyusages"><literal>keyUsages</literal></tag-name>
3002
<tag-desc>
3003
is an <literal>Array</literal>
3004
that indicates what can be done with the derived key.
3005
The key usages must be allowed by the algorithm
3006
set in <literal>derivedKeyAlgorithm</literal>.
3007
Possible values:
3008
<list type="tag">
3009
3010
<tag-name><literal>encrypt</literal></tag-name>
3011
<tag-desc>
3012
key for encrypting messages
3013
</tag-desc>
3014
3015
<tag-name><literal>decrypt</literal></tag-name>
3016
<tag-desc>
3017
key for decrypting messages
3018
</tag-desc>
3019
3020
<tag-name><literal>sign</literal></tag-name>
3021
<tag-desc>
3022
key for signing messages
3023
</tag-desc>
3024
3025
<tag-name><literal>verify</literal></tag-name>
3026
<tag-desc>
3027
key for verifying signatures
3028
</tag-desc>
3029
3030
<tag-name><literal>deriveKey</literal></tag-name>
3031
<tag-desc>
3032
key for deriving a new key
3033
</tag-desc>
3034
3035
<tag-name><literal>deriveBits</literal></tag-name>
3036
<tag-desc>
3037
key for deriving bits
3038
</tag-desc>
3039
3040
<tag-name><literal>wrapKey</literal></tag-name>
3041
<tag-desc>
3042
key for wrapping a key
3043
</tag-desc>
3044
3045
<tag-name><literal>unwrapKey</literal></tag-name>
3046
<tag-desc>
3047
key for unwrapping a key
3048
</tag-desc>
3049
</list>
3050
3051
</tag-desc>
3052
</list>
3053
3054
</tag-desc>
3055
3056
<tag-name id="crypto_subtle_digest"><literal>сrypto.subtle.digest</literal>(<link id="crypto_digest_alg"><literal>algorithm</literal></link>,
3057
<link id="crypto_digest_data"><literal>data</literal></link>)</tag-name>
3058
<tag-desc>
3059
Generates a digest of the given data.
3060
Takes as its arguments an identifier for the digest algorithm to use
3061
and the data to digest.
3062
Returns a <literal>Promise</literal> which will be fulfilled with the digest.
3063
Possible values:
3064
3065
<list type="tag">
3066
<tag-name id="crypto_digest_alg"><literal>algorithm</literal></tag-name>
3067
<tag-desc>
3068
is a string that defines the hash function to use:
3069
<literal>SHA-1</literal> (not for cryptographic applications),
3070
<literal>SHA-256</literal>,
3071
<literal>SHA-384</literal>, or
3072
<literal>SHA-512</literal>
3073
</tag-desc>
3074
3075
<tag-name id="crypto_digest_data"><literal>data</literal></tag-name>
3076
<tag-desc>
3077
is an
3078
<literal>ArrayBuffer</literal>,
3079
<literal>TypedArray</literal>, or
3080
<literal>DataView</literal>
3081
that contains the data to be digested
3082
</tag-desc>
3083
</list>
3084
3085
</tag-desc>
3086
3087
<tag-name id="crypto_subtle_export_key"><literal>сrypto.subtle.exportKey</literal>(<link id="crypto_export_key_format"><literal>format</literal></link>,
3088
<link id="crypto_export_key_keydata"><literal>key</literal></link>)</tag-name>
3089
<tag-desc>
3090
Exports a key: takes a key as
3091
a <link id="cryptokey"><literal>CryptoKey</literal></link> object
3092
and returns the key in an external, portable format
3093
(since <link doc="changes.xml" id="njs0.7.10">0.7.10</link>).
3094
If the <literal>format</literal> was <literal>jwk</literal>,
3095
then the <literal>Promise</literal> fulfills with a JSON object
3096
containing the key.
3097
Otherwise, the promise fulfills with an
3098
<literal>ArrayBuffer</literal> containing the key.
3099
Possible values:
3100
<list type="tag">
3101
3102
<tag-name id="crypto_export_key_format"><literal>format</literal></tag-name>
3103
<tag-desc>
3104
a string that describes the data format in which the key should be exported,
3105
can be the following:
3106
<list type="tag">
3107
3108
<tag-name><literal>raw</literal></tag-name>
3109
<tag-desc>
3110
the raw data format
3111
</tag-desc>
3112
3113
<tag-name><literal>pkcs8</literal></tag-name>
3114
<tag-desc>
3115
the
3116
<link url="https://datatracker.ietf.org/doc/html/rfc5208">PKCS #8</link>
3117
format
3118
</tag-desc>
3119
3120
<tag-name><literal>spki</literal></tag-name>
3121
<tag-desc>
3122
the
3123
<link url="https://datatracker.ietf.org/doc/html/rfc5280#section-4.1">SubjectPublicKeyInfo</link>
3124
format
3125
</tag-desc>
3126
3127
<tag-name><literal>jwk</literal></tag-name>
3128
<tag-desc>
3129
the
3130
<link url="https://datatracker.ietf.org/doc/html/rfc7517">JSON Web Key</link>
3131
(JWK) format (since <link doc="changes.xml" id="njs0.7.10">0.7.10</link>)
3132
</tag-desc>
3133
3134
</list>
3135
</tag-desc>
3136
3137
<tag-name id="crypto_export_key_keydata"><literal>key</literal></tag-name>
3138
<tag-desc>
3139
the <link id="cryptokey"><literal>CryptoKey</literal></link>
3140
that contains the key to be exported
3141
</tag-desc>
3142
</list>
3143
3144
</tag-desc>
3145
3146
<tag-name id="crypto_subtle_generate_key"><literal>сrypto.subtle.generateKey</literal>(<link id="crypto_generate_key_alg"><literal>algorithm</literal></link>,
3147
<link id="crypto_generate_key_extractable"><literal>extractable</literal></link>,
3148
<link id="crypto_generate_key_usage"><literal>usage</literal></link>)</tag-name>
3149
<tag-desc>
3150
Generates a new key for symmetric algorithms
3151
or key pair for public-key algorithms
3152
(since <link doc="changes.xml" id="njs0.7.10">0.7.10</link>).
3153
Returns a <literal>Promise</literal> that fulfills with the generated key
3154
as
3155
a <link id="cryptokey"><literal>CryptoKey</literal></link>
3156
or <link id="cryptokeypair"><literal>CryptoKeyPair</literal></link> object.
3157
Possible values:
3158
<list type="tag">
3159
3160
<tag-name id="crypto_generate_key_alg"><literal>algorithm</literal></tag-name>
3161
<tag-desc>
3162
a dictionary object that defines the type of key to generate
3163
and provides extra algorithm-specific parameters:
3164
3165
<list type="bullet">
3166
<listitem>
3167
for
3168
<literal>RSASSA-PKCS1-v1_5</literal>,
3169
<literal>RSA-PSS</literal>, or
3170
<literal>RSA-OAEP</literal>,
3171
pass the object with the following keys:
3172
3173
<list type="bullet">
3174
<listitem>
3175
<literal>name</literal> is a string, should be set to
3176
<literal>RSASSA-PKCS1-v1_5</literal>,
3177
<literal>RSA-PSS</literal>, or
3178
<literal>RSA-OAEP</literal>,
3179
depending on the used algorithm
3180
</listitem>
3181
3182
<listitem>
3183
<literal>hash</literal> is a string that represents
3184
the name of the <literal>digest</literal> function to use, can be
3185
<literal>SHA-256</literal>,
3186
<literal>SHA-384</literal>, or
3187
<literal>SHA-512</literal>
3188
</listitem>
3189
</list>
3190
3191
</listitem>
3192
3193
<listitem>
3194
for
3195
<literal>ECDSA</literal>,
3196
pass the object with the following keys:
3197
3198
<list type="bullet">
3199
<listitem>
3200
<literal>name</literal> is a string, should be set to <literal>ECDSA</literal>
3201
</listitem>
3202
3203
<listitem>
3204
<literal>namedCurve</literal> is a string that represents
3205
the name of the elliptic curve to use, may be
3206
<literal>P-256</literal>,
3207
<literal>P-384</literal>, or
3208
<literal>P-521</literal>
3209
</listitem>
3210
3211
</list>
3212
</listitem>
3213
3214
<listitem>
3215
for
3216
<literal>HMAC</literal>,
3217
pass the object with the following keys:
3218
3219
<list type="bullet">
3220
<listitem>
3221
<literal>name</literal> is a string, should be set to <literal>HMAC</literal>
3222
</listitem>
3223
3224
3225
<listitem>
3226
<literal>hash</literal> is a string that represents
3227
the name of the <literal>digest</literal> function to use, can be
3228
<literal>SHA-256</literal>,
3229
<literal>SHA-384</literal>, or
3230
<literal>SHA-512</literal>
3231
</listitem>
3232
3233
<listitem>
3234
<literal>length</literal> (optional) is a number that represents
3235
the length in bits of the key.
3236
If omitted, the length of the key is equal to the length of the digest
3237
generated by the chosen digest function.
3238
</listitem>
3239
</list>
3240
3241
</listitem>
3242
3243
<listitem>
3244
for
3245
<literal>AES-CTR</literal>,
3246
<literal>AES-CBC</literal>,
3247
<literal>AES-GCM</literal>, or
3248
<literal>AES-KW</literal>
3249
(since <link doc="changes.xml" id="njs0.9.7">0.9.7</link>),
3250
pass the string identifying the algorithm or an object
3251
of the form <literal>{ "name": "ALGORITHM" }</literal>,
3252
where <literal>ALGORITHM</literal> is the name of the algorithm
3253
</listitem>
3254
3255
<listitem>
3256
for
3257
<literal>ECDH</literal>,
3258
pass the object with the following keys
3259
(since <link doc="changes.xml" id="njs0.9.1">0.9.1</link>):
3260
3261
<list type="bullet">
3262
<listitem>
3263
<literal>name</literal> is a string, should be set to <literal>ECDH</literal>
3264
</listitem>
3265
3266
<listitem>
3267
<literal>namedCurve</literal> is a string that represents
3268
the name of the elliptic curve to use, may be
3269
<literal>P-256</literal>,
3270
<literal>P-384</literal>, or
3271
<literal>P-521</literal>
3272
</listitem>
3273
3274
</list>
3275
</listitem>
3276
3277
<listitem>
3278
for <literal>Ed25519</literal>,
3279
pass the <literal>Ed25519</literal> string
3280
or an object of the form <literal>{ "name": "Ed25519" }</literal>
3281
(since <link doc="changes.xml" id="njs0.9.7">0.9.7</link>)
3282
</listitem>
3283
3284
<listitem>
3285
for <literal>X25519</literal>,
3286
pass the <literal>X25519</literal> string
3287
or an object of the form <literal>{ "name": "X25519" }</literal>
3288
(since <link doc="changes.xml" id="njs0.9.7">0.9.7</link>)
3289
</listitem>
3290
3291
</list>
3292
</tag-desc>
3293
3294
<tag-name id="crypto_generate_key_extractable"><literal>extractable</literal></tag-name>
3295
<tag-desc>
3296
boolean value that indicates if it is possible to export the key
3297
</tag-desc>
3298
3299
<tag-name id="crypto_generate_key_usage"><literal>usage</literal></tag-name>
3300
<tag-desc>
3301
an <literal>array</literal> that indicates possible actions with the key:
3302
<list type="tag">
3303
3304
<tag-name><literal>encrypt</literal></tag-name>
3305
<tag-desc>
3306
key for encrypting messages
3307
</tag-desc>
3308
3309
<tag-name><literal>decrypt</literal></tag-name>
3310
<tag-desc>
3311
key for decrypting messages
3312
</tag-desc>
3313
3314
<tag-name><literal>sign</literal></tag-name>
3315
<tag-desc>
3316
key for signing messages
3317
</tag-desc>
3318
3319
<tag-name><literal>verify</literal></tag-name>
3320
<tag-desc>
3321
key for verifying signatures
3322
</tag-desc>
3323
3324
<tag-name><literal>deriveKey</literal></tag-name>
3325
<tag-desc>
3326
key for deriving a new key
3327
</tag-desc>
3328
3329
<tag-name><literal>deriveBits</literal></tag-name>
3330
<tag-desc>
3331
key for deriving bits
3332
</tag-desc>
3333
3334
<tag-name><literal>wrapKey</literal></tag-name>
3335
<tag-desc>
3336
key for wrapping a key
3337
</tag-desc>
3338
3339
<tag-name><literal>unwrapKey</literal></tag-name>
3340
<tag-desc>
3341
key for unwrapping a key
3342
</tag-desc>
3343
</list>
3344
3345
</tag-desc>
3346
</list>
3347
3348
</tag-desc>
3349
3350
<tag-name id="crypto_subtle_import_key"><literal>сrypto.subtle.importKey</literal>(<link id="crypto_import_key_format"><literal>format</literal></link>,
3351
<link id="crypto_import_key_keydata"><literal>keyData</literal></link>,
3352
<link id="crypto_import_key_alg"><literal>algorithm</literal></link>,
3353
<link id="crypto_import_key_extractable"><literal>extractable</literal></link>,
3354
<link id="crypto_import_key_keyusages"><literal>keyUsages</literal></link>)</tag-name>
3355
<tag-desc>
3356
Imports a key: takes as input a key in an external, portable format
3357
and gives a <link id="cryptokey"><literal>CryptoKey</literal></link> object.
3358
Returns a <literal>Promise</literal> that fulfills with the imported key
3359
as a <link id="cryptokey"><literal>CryptoKey</literal></link> object.
3360
Possible values:
3361
<list type="tag">
3362
3363
<tag-name id="crypto_import_key_format"><literal>format</literal></tag-name>
3364
<tag-desc>
3365
a string that describes the data format of the key to import,
3366
can be the following:
3367
<list type="tag">
3368
3369
<tag-name><literal>raw</literal></tag-name>
3370
<tag-desc>
3371
the raw data format
3372
</tag-desc>
3373
3374
<tag-name><literal>pkcs8</literal></tag-name>
3375
<tag-desc>
3376
the
3377
<link url="https://datatracker.ietf.org/doc/html/rfc5208">PKCS #8</link>
3378
format
3379
</tag-desc>
3380
3381
<tag-name><literal>spki</literal></tag-name>
3382
<tag-desc>
3383
the
3384
<link url="https://datatracker.ietf.org/doc/html/rfc5280#section-4.1">SubjectPublicKeyInfo</link>
3385
format
3386
</tag-desc>
3387
3388
<tag-name><literal>jwk</literal></tag-name>
3389
<tag-desc>
3390
the
3391
<link url="https://datatracker.ietf.org/doc/html/rfc7517">JSON Web Key</link>
3392
(JWK) format (since <link doc="changes.xml" id="njs0.7.10">0.7.10</link>)
3393
</tag-desc>
3394
3395
</list>
3396
3397
</tag-desc>
3398
3399
<tag-name id="crypto_import_key_keydata"><literal>keyData</literal></tag-name>
3400
<tag-desc>
3401
the
3402
<literal>ArrayBuffer</literal>,
3403
<literal>TypedArray</literal>, or
3404
<literal>DataView</literal>
3405
object that contains the key in the given format
3406
</tag-desc>
3407
3408
<tag-name id="crypto_import_key_alg"><literal>algorithm</literal></tag-name>
3409
<tag-desc>
3410
a dictionary object that defines the type of key to import
3411
and provides extra algorithm-specific parameters:
3412
3413
<list type="bullet">
3414
<listitem>
3415
for
3416
<literal>RSASSA-PKCS1-v1_5</literal>,
3417
<literal>RSA-PSS</literal>, or
3418
<literal>RSA-OAEP</literal>,
3419
pass the object with the following keys:
3420
3421
<list type="bullet">
3422
<listitem>
3423
<literal>name</literal> is a string, should be set to
3424
<literal>RSASSA-PKCS1-v1_5</literal>,
3425
<literal>RSA-PSS</literal>, or
3426
<literal>RSA-OAEP</literal>,
3427
depending on the used algorithm
3428
</listitem>
3429
3430
<listitem>
3431
<literal>hash</literal> is a string that represents
3432
the name of the <literal>digest</literal> function to use, can be
3433
<literal>SHA-1</literal>,
3434
<literal>SHA-256</literal>,
3435
<literal>SHA-384</literal>, or
3436
<literal>SHA-512</literal>
3437
</listitem>
3438
</list>
3439
3440
</listitem>
3441
3442
<listitem>
3443
for
3444
<literal>ECDSA</literal>,
3445
pass the object with the following keys:
3446
3447
<list type="bullet">
3448
<listitem>
3449
<literal>name</literal> is a string, should be set to <literal>ECDSA</literal>
3450
</listitem>
3451
3452
<listitem>
3453
<literal>namedCurve</literal> is a string that represents
3454
the name of the elliptic curve to use, may be
3455
<literal>P-256</literal>,
3456
<literal>P-384</literal>, or
3457
<literal>P-521</literal>
3458
</listitem>
3459
3460
</list>
3461
</listitem>
3462
3463
<listitem>
3464
for
3465
<literal>HMAC</literal>,
3466
pass the object with the following keys:
3467
3468
<list type="bullet">
3469
<listitem>
3470
<literal>name</literal> is a string, should be set to <literal>HMAC</literal>
3471
</listitem>
3472
3473
3474
<listitem>
3475
<literal>hash</literal> is a string that represents
3476
the name of the <literal>digest</literal> function to use, can be
3477
<literal>SHA-256</literal>,
3478
<literal>SHA-384</literal>, or
3479
<literal>SHA-512</literal>
3480
</listitem>
3481
3482
<listitem>
3483
<literal>length</literal> (optional) is a number that represents
3484
the length in bits of the key.
3485
If omitted, the length of the key is equal to the length of the digest
3486
generated by the chosen digest function.
3487
</listitem>
3488
</list>
3489
3490
</listitem>
3491
3492
<listitem>
3493
for
3494
<literal>AES-CTR</literal>,
3495
<literal>AES-CBC</literal>,
3496
<literal>AES-GCM</literal>, or
3497
<literal>AES-KW</literal>
3498
(since <link doc="changes.xml" id="njs0.9.7">0.9.7</link>),
3499
pass the string identifying the algorithm or an object
3500
of the form <literal>{ "name": "ALGORITHM" }</literal>,
3501
where <literal>ALGORITHM</literal> is the name of the algorithm
3502
</listitem>
3503
3504
<listitem>
3505
for
3506
<literal>PBKDF2</literal>,
3507
pass the <literal>PBKDF2</literal> string
3508
</listitem>
3509
3510
<listitem>
3511
for
3512
<literal>HKDF</literal>,
3513
pass the <literal>HKDF</literal> string
3514
</listitem>
3515
3516
<listitem>
3517
for
3518
<literal>ECDH</literal>,
3519
pass the object with the following keys
3520
(since <link doc="changes.xml" id="njs0.9.1">0.9.1</link>):
3521
3522
<list type="bullet">
3523
<listitem>
3524
<literal>name</literal> is a string, should be set to <literal>ECDH</literal>
3525
</listitem>
3526
3527
<listitem>
3528
<literal>namedCurve</literal> is a string that represents
3529
the name of the elliptic curve to use, may be
3530
<literal>P-256</literal>,
3531
<literal>P-384</literal>, or
3532
<literal>P-521</literal>
3533
</listitem>
3534
3535
</list>
3536
</listitem>
3537
3538
<listitem>
3539
for <literal>Ed25519</literal>,
3540
pass the <literal>Ed25519</literal> string
3541
or an object of the form <literal>{ "name": "Ed25519" }</literal>
3542
(since <link doc="changes.xml" id="njs0.9.7">0.9.7</link>)
3543
</listitem>
3544
3545
<listitem>
3546
for <literal>X25519</literal>,
3547
pass the <literal>X25519</literal> string
3548
or an object of the form <literal>{ "name": "X25519" }</literal>
3549
(since <link doc="changes.xml" id="njs0.9.7">0.9.7</link>)
3550
</listitem>
3551
3552
</list>
3553
</tag-desc>
3554
3555
<tag-name id="crypto_import_key_extractable"><literal>extractable</literal></tag-name>
3556
<tag-desc>
3557
boolean value that indicates if it is possible to export the key
3558
</tag-desc>
3559
3560
<tag-name id="crypto_import_key_keyusages"><literal>keyUsages</literal></tag-name>
3561
<tag-desc>
3562
an <literal>array</literal> that indicates possible actions with the key:
3563
<list type="tag">
3564
3565
<tag-name><literal>encrypt</literal></tag-name>
3566
<tag-desc>
3567
key for encrypting messages
3568
</tag-desc>
3569
3570
<tag-name><literal>decrypt</literal></tag-name>
3571
<tag-desc>
3572
key for decrypting messages
3573
</tag-desc>
3574
3575
<tag-name><literal>sign</literal></tag-name>
3576
<tag-desc>
3577
key for signing messages
3578
</tag-desc>
3579
3580
<tag-name><literal>verify</literal></tag-name>
3581
<tag-desc>
3582
key for verifying signatures
3583
</tag-desc>
3584
3585
<tag-name><literal>deriveKey</literal></tag-name>
3586
<tag-desc>
3587
key for deriving a new key
3588
</tag-desc>
3589
3590
<tag-name><literal>deriveBits</literal></tag-name>
3591
<tag-desc>
3592
key for deriving bits
3593
</tag-desc>
3594
3595
<tag-name><literal>wrapKey</literal></tag-name>
3596
<tag-desc>
3597
key for wrapping a key
3598
</tag-desc>
3599
3600
<tag-name><literal>unwrapKey</literal></tag-name>
3601
<tag-desc>
3602
key for unwrapping a key
3603
</tag-desc>
3604
</list>
3605
3606
</tag-desc>
3607
</list>
3608
3609
</tag-desc>
3610
3611
<tag-name id="crypto_subtle_sign"><literal>сrypto.subtle.sign</literal>(<link id="crypto_sign_alg"><literal>algorithm</literal></link>,
3612
<link id="crypto_sign_key"><literal>key</literal></link>,
3613
<link id="crypto_sign_data"><literal>data</literal></link>)</tag-name>
3614
<tag-desc>
3615
Returns <literal>signature</literal> as a <literal>Promise</literal>
3616
that fulfills with an <literal>ArrayBuffer</literal> containing the signature.
3617
Possible values:
3618
3619
<list type="tag">
3620
<tag-name id="crypto_sign_alg"><literal>algorithm</literal></tag-name>
3621
<tag-desc>
3622
is a string or object that specifies the signature algorithm to use
3623
and its parameters:
3624
3625
<list type="bullet">
3626
3627
<listitem>
3628
for <literal>RSASSA-PKCS1-v1_5</literal>,
3629
pass the string identifying the algorithm or an object
3630
of the form <literal>{ "name": "ALGORITHM" }</literal>
3631
</listitem>
3632
3633
<listitem>
3634
for <literal>RSA-PSS</literal>,
3635
pass the object with the following keys:
3636
<list type="bullet">
3637
3638
<listitem>
3639
<literal>name</literal> is a string, should be set to
3640
<literal>RSA-PSS</literal>
3641
</listitem>
3642
3643
<listitem>
3644
<literal>saltLength</literal> is a long <literal>integer</literal>
3645
that represents the length of the random salt to use, in bytes
3646
</listitem>
3647
3648
</list>
3649
</listitem>
3650
3651
<listitem>
3652
for <literal>ECDSA</literal>,
3653
pass the object with the following keys:
3654
<list type="bullet">
3655
3656
<listitem>
3657
<literal>name</literal> is a string, should be set to
3658
<literal>ECDSA</literal>
3659
</listitem>
3660
3661
<listitem>
3662
<literal>hash</literal> is an identifier for the digest algorithm to use,
3663
can be
3664
<literal>SHA-256</literal>,
3665
<literal>SHA-384</literal>, or
3666
<literal>SHA-512</literal>
3667
</listitem>
3668
3669
</list>
3670
</listitem>
3671
3672
<listitem>
3673
for <literal>HMAC</literal>,
3674
pass the string identifying the algorithm or an object
3675
of the form <literal>{ "name": "ALGORITHM" }</literal>
3676
</listitem>
3677
3678
<listitem>
3679
for <literal>Ed25519</literal>,
3680
pass the <literal>Ed25519</literal> string
3681
or an object of the form <literal>{ "name": "Ed25519" }</literal>
3682
(since <link doc="changes.xml" id="njs0.9.7">0.9.7</link>)
3683
</listitem>
3684
</list>
3685
3686
</tag-desc>
3687
3688
<tag-name id="crypto_sign_key"><literal>key</literal></tag-name>
3689
<tag-desc>
3690
is a <link id="cryptokey"><literal>CryptoKey</literal></link> object
3691
that the key to be used for signing.
3692
If algorithm identifies a public-key cryptosystem, this is the private key.
3693
</tag-desc>
3694
3695
<tag-name id="crypto_sign_data"><literal>data</literal></tag-name>
3696
<tag-desc>
3697
is an
3698
<literal>ArrayBuffer</literal>,
3699
<literal>TypedArray</literal>, or
3700
<literal>DataView</literal>
3701
object that contains the data to be signed
3702
</tag-desc>
3703
</list>
3704
3705
</tag-desc>
3706
3707
3708
<tag-name id="crypto_subtle_verify"><literal>сrypto.subtle.verify</literal>(<link id="crypto_verify_alg"><literal>algorithm</literal></link>,
3709
<link id="crypto_verify_key"><literal>key</literal></link>,
3710
<link id="crypto_verify_signature"><literal>signature</literal></link>,
3711
<link id="crypto_verify_data"><literal>data</literal></link>)</tag-name>
3712
<tag-desc>
3713
Verifies a digital signature,
3714
returns a <literal>Promise</literal> that fulfills with a boolean value:
3715
<literal>true</literal> if the signature is valid,
3716
otherwise <literal>false</literal>.
3717
Possible values:
3718
3719
<list type="tag">
3720
<tag-name id="crypto_verify_alg"><literal>algorithm</literal></tag-name>
3721
<tag-desc>
3722
is a string or object that specifies the algorithm to use
3723
and its parameters:
3724
3725
<list type="bullet">
3726
3727
<listitem>
3728
for <literal>RSASSA-PKCS1-v1_5</literal>,
3729
pass the string identifying the algorithm or an object
3730
of the form <literal>{ "name": "ALGORITHM" }</literal>
3731
</listitem>
3732
3733
<listitem>
3734
for <literal>RSA-PSS</literal>,
3735
pass the object with the following keys:
3736
<list type="bullet">
3737
3738
<listitem>
3739
<literal>name</literal> is a string, should be set to
3740
<literal>RSA-PSS</literal>
3741
</listitem>
3742
3743
<listitem>
3744
<literal>saltLength</literal> is a long <literal>integer</literal>
3745
that represents the length of the random salt to use, in bytes
3746
</listitem>
3747
3748
</list>
3749
</listitem>
3750
3751
<listitem>
3752
for <literal>ECDSA</literal>,
3753
pass the object with the following keys:
3754
<list type="bullet">
3755
3756
<listitem>
3757
<literal>name</literal> is a string, should be set to
3758
<literal>ECDSA</literal>
3759
</listitem>
3760
3761
<listitem>
3762
<literal>hash</literal> is an identifier for the digest algorithm to use,
3763
can be
3764
<literal>SHA-256</literal>,
3765
<literal>SHA-384</literal>, or
3766
<literal>SHA-512</literal>
3767
</listitem>
3768
3769
</list>
3770
</listitem>
3771
3772
<listitem>
3773
for <literal>HMAC</literal>,
3774
pass the string identifying the algorithm or an object
3775
of the form <literal>{ "name": "ALGORITHM" }</literal>
3776
</listitem>
3777
3778
<listitem>
3779
for <literal>Ed25519</literal>,
3780
pass the <literal>Ed25519</literal> string
3781
or an object of the form <literal>{ "name": "Ed25519" }</literal>
3782
(since <link doc="changes.xml" id="njs0.9.7">0.9.7</link>)
3783
</listitem>
3784
</list>
3785
3786
</tag-desc>
3787
3788
<tag-name id="crypto_verify_key"><literal>key</literal></tag-name>
3789
<tag-desc>
3790
is a <link id="cryptokey"><literal>CryptoKey</literal></link> object
3791
that the key to be used for verifying.
3792
It is the secret key for a symmetric algorithm
3793
and the public key for a public-key system.
3794
</tag-desc>
3795
3796
<tag-name id="crypto_verify_signature"><literal>signature</literal></tag-name>
3797
<tag-desc>
3798
is an
3799
<literal>ArrayBuffer</literal>,
3800
<literal>TypedArray</literal>, or
3801
<literal>DataView</literal>
3802
that contains the signature to verify
3803
</tag-desc>
3804
3805
<tag-name id="crypto_verify_data"><literal>data</literal></tag-name>
3806
<tag-desc>
3807
is an
3808
<literal>ArrayBuffer</literal>,
3809
<literal>TypedArray</literal>, or
3810
<literal>DataView</literal>
3811
object that contains the data whose signature is to be verified
3812
</tag-desc>
3813
</list>
3814
3815
</tag-desc>
3816
3817
3818
<tag-name id="crypto_subtle_wrap_key"><literal>сrypto.subtle.wrapKey</literal>(<link id="crypto_wrap_key_format"><literal>format</literal></link>,
3819
<link id="crypto_wrap_key_key"><literal>key</literal></link>,
3820
<link id="crypto_wrap_key_wrappingkey"><literal>wrappingKey</literal></link>,
3821
<link id="crypto_wrap_key_wrapalg"><literal>wrapAlgorithm</literal></link>)</tag-name>
3822
<tag-desc>
3823
Wraps a key: exports a key in an external, portable format,
3824
then encrypts the exported key
3825
(since <link doc="changes.xml" id="njs0.9.7">0.9.7</link>).
3826
Returns a <literal>Promise</literal> that fulfills with
3827
an <literal>ArrayBuffer</literal> containing the encrypted exported key.
3828
Possible values:
3829
3830
<list type="tag">
3831
3832
<tag-name id="crypto_wrap_key_format"><literal>format</literal></tag-name>
3833
<tag-desc>
3834
a string that describes the data format of the key to export
3835
before it is encrypted,
3836
can be the following:
3837
<list type="tag">
3838
3839
<tag-name><literal>raw</literal></tag-name>
3840
<tag-desc>
3841
the raw data format
3842
</tag-desc>
3843
3844
<tag-name><literal>pkcs8</literal></tag-name>
3845
<tag-desc>
3846
the
3847
<link url="https://datatracker.ietf.org/doc/html/rfc5208">PKCS #8</link>
3848
format
3849
</tag-desc>
3850
3851
<tag-name><literal>spki</literal></tag-name>
3852
<tag-desc>
3853
the
3854
<link url="https://datatracker.ietf.org/doc/html/rfc5280#section-4.1">SubjectPublicKeyInfo</link>
3855
format
3856
</tag-desc>
3857
3858
<tag-name><literal>jwk</literal></tag-name>
3859
<tag-desc>
3860
the
3861
<link url="https://datatracker.ietf.org/doc/html/rfc7517">JSON Web Key</link>
3862
(JWK) format
3863
</tag-desc>
3864
3865
</list>
3866
</tag-desc>
3867
3868
<tag-name id="crypto_wrap_key_key"><literal>key</literal></tag-name>
3869
<tag-desc>
3870
a <link id="cryptokey"><literal>CryptoKey</literal></link>
3871
that contains the key to be wrapped.
3872
The key must have
3873
<link id="cryptokey_extractable"><literal>CryptoKey.extractable</literal></link>
3874
set to <literal>true</literal>.
3875
</tag-desc>
3876
3877
<tag-name id="crypto_wrap_key_wrappingkey"><literal>wrappingKey</literal></tag-name>
3878
<tag-desc>
3879
a <link id="cryptokey"><literal>CryptoKey</literal></link>
3880
that contains the key used to encrypt the exported key.
3881
The key must have the <literal>wrapKey</literal> usage set.
3882
</tag-desc>
3883
3884
<tag-name id="crypto_wrap_key_wrapalg"><literal>wrapAlgorithm</literal></tag-name>
3885
<tag-desc>
3886
an object that specifies the algorithm to be used
3887
to encrypt the exported key, and any extra parameters as required:
3888
3889
<list type="bullet">
3890
3891
<listitem>
3892
for <literal>AES-CBC</literal>,
3893
<literal>AES-CTR</literal>, or
3894
<literal>AES-GCM</literal>,
3895
pass the corresponding algorithm parameters
3896
as described in
3897
<link id="crypto_subtle_encrypt"><literal>сrypto.subtle.encrypt()</literal></link>
3898
</listitem>
3899
3900
<listitem>
3901
for <literal>AES-KW</literal>,
3902
pass the <literal>AES-KW</literal> string
3903
or an object of the form <literal>{ "name": "AES-KW" }</literal>
3904
</listitem>
3905
3906
<listitem>
3907
for <literal>RSA-OAEP</literal>,
3908
pass the corresponding algorithm parameters
3909
as described in
3910
<link id="crypto_subtle_encrypt"><literal>сrypto.subtle.encrypt()</literal></link>
3911
</listitem>
3912
3913
</list>
3914
3915
</tag-desc>
3916
</list>
3917
3918
</tag-desc>
3919
3920
3921
<tag-name id="crypto_subtle_unwrap_key"><literal>сrypto.subtle.unwrapKey</literal>(<link id="crypto_unwrap_key_format"><literal>format</literal></link>,
3922
<link id="crypto_unwrap_key_wrappedkey"><literal>wrappedKey</literal></link>,
3923
<link id="crypto_unwrap_key_unwrappingkey"><literal>unwrappingKey</literal></link>,
3924
<link id="crypto_unwrap_key_unwrapalg"><literal>unwrapAlgorithm</literal></link>,
3925
<link id="crypto_unwrap_key_unwrappedkeyalg"><literal>unwrappedKeyAlgorithm</literal></link>,
3926
<link id="crypto_unwrap_key_extractable"><literal>extractable</literal></link>,
3927
<link id="crypto_unwrap_key_keyusages"><literal>keyUsages</literal></link>)</tag-name>
3928
<tag-desc>
3929
Unwraps a key: decrypts a wrapped key
3930
and creates a <link id="cryptokey"><literal>CryptoKey</literal></link> object
3931
from the result
3932
(since <link doc="changes.xml" id="njs0.9.7">0.9.7</link>).
3933
Returns a <literal>Promise</literal> that fulfills with
3934
the unwrapped key as a <link id="cryptokey"><literal>CryptoKey</literal></link>.
3935
Possible values:
3936
3937
<list type="tag">
3938
3939
<tag-name id="crypto_unwrap_key_format"><literal>format</literal></tag-name>
3940
<tag-desc>
3941
a string that describes the data format of the key
3942
that was wrapped,
3943
can be the following:
3944
<list type="tag">
3945
3946
<tag-name><literal>raw</literal></tag-name>
3947
<tag-desc>
3948
the raw data format
3949
</tag-desc>
3950
3951
<tag-name><literal>pkcs8</literal></tag-name>
3952
<tag-desc>
3953
the
3954
<link url="https://datatracker.ietf.org/doc/html/rfc5208">PKCS #8</link>
3955
format
3956
</tag-desc>
3957
3958
<tag-name><literal>spki</literal></tag-name>
3959
<tag-desc>
3960
the
3961
<link url="https://datatracker.ietf.org/doc/html/rfc5280#section-4.1">SubjectPublicKeyInfo</link>
3962
format
3963
</tag-desc>
3964
3965
<tag-name><literal>jwk</literal></tag-name>
3966
<tag-desc>
3967
the
3968
<link url="https://datatracker.ietf.org/doc/html/rfc7517">JSON Web Key</link>
3969
(JWK) format
3970
</tag-desc>
3971
3972
</list>
3973
</tag-desc>
3974
3975
<tag-name id="crypto_unwrap_key_wrappedkey"><literal>wrappedKey</literal></tag-name>
3976
<tag-desc>
3977
an
3978
<literal>ArrayBuffer</literal>,
3979
<literal>TypedArray</literal>, or
3980
<literal>DataView</literal>
3981
that contains the wrapped key in the given format
3982
</tag-desc>
3983
3984
<tag-name id="crypto_unwrap_key_unwrappingkey"><literal>unwrappingKey</literal></tag-name>
3985
<tag-desc>
3986
a <link id="cryptokey"><literal>CryptoKey</literal></link>
3987
that contains the key used to decrypt the wrapped key.
3988
The key must have the <literal>unwrapKey</literal> usage set.
3989
</tag-desc>
3990
3991
<tag-name id="crypto_unwrap_key_unwrapalg"><literal>unwrapAlgorithm</literal></tag-name>
3992
<tag-desc>
3993
an object that specifies the algorithm to be used
3994
to decrypt the wrapped key, and any extra parameters as required:
3995
3996
<list type="bullet">
3997
3998
<listitem>
3999
for <literal>AES-CBC</literal>,
4000
<literal>AES-CTR</literal>, or
4001
<literal>AES-GCM</literal>,
4002
pass the corresponding algorithm parameters
4003
as described in
4004
<link id="crypto_subtle_encrypt"><literal>сrypto.subtle.encrypt()</literal></link>
4005
</listitem>
4006
4007
<listitem>
4008
for <literal>AES-KW</literal>,
4009
pass the <literal>AES-KW</literal> string
4010
or an object of the form <literal>{ "name": "AES-KW" }</literal>
4011
</listitem>
4012
4013
<listitem>
4014
for <literal>RSA-OAEP</literal>,
4015
pass the corresponding algorithm parameters
4016
as described in
4017
<link id="crypto_subtle_encrypt"><literal>сrypto.subtle.encrypt()</literal></link>
4018
</listitem>
4019
4020
</list>
4021
4022
</tag-desc>
4023
4024
<tag-name id="crypto_unwrap_key_unwrappedkeyalg"><literal>unwrappedKeyAlgorithm</literal></tag-name>
4025
<tag-desc>
4026
a dictionary object that defines the type of key to unwrap
4027
and provides extra algorithm-specific parameters.
4028
The same algorithm descriptions as in
4029
<link id="crypto_subtle_import_key"><literal>сrypto.subtle.importKey()</literal></link>
4030
apply.
4031
</tag-desc>
4032
4033
<tag-name id="crypto_unwrap_key_extractable"><literal>extractable</literal></tag-name>
4034
<tag-desc>
4035
boolean value that indicates if it is possible to export the key
4036
</tag-desc>
4037
4038
<tag-name id="crypto_unwrap_key_keyusages"><literal>keyUsages</literal></tag-name>
4039
<tag-desc>
4040
an <literal>array</literal> that indicates possible actions with the key:
4041
<list type="tag">
4042
4043
<tag-name><literal>encrypt</literal></tag-name>
4044
<tag-desc>
4045
key for encrypting messages
4046
</tag-desc>
4047
4048
<tag-name><literal>decrypt</literal></tag-name>
4049
<tag-desc>
4050
key for decrypting messages
4051
</tag-desc>
4052
4053
<tag-name><literal>sign</literal></tag-name>
4054
<tag-desc>
4055
key for signing messages
4056
</tag-desc>
4057
4058
<tag-name><literal>verify</literal></tag-name>
4059
<tag-desc>
4060
key for verifying signatures
4061
</tag-desc>
4062
4063
<tag-name><literal>deriveKey</literal></tag-name>
4064
<tag-desc>
4065
key for deriving a new key
4066
</tag-desc>
4067
4068
<tag-name><literal>deriveBits</literal></tag-name>
4069
<tag-desc>
4070
key for deriving bits
4071
</tag-desc>
4072
4073
<tag-name><literal>wrapKey</literal></tag-name>
4074
<tag-desc>
4075
key for wrapping a key
4076
</tag-desc>
4077
4078
<tag-name><literal>unwrapKey</literal></tag-name>
4079
<tag-desc>
4080
key for unwrapping a key
4081
</tag-desc>
4082
</list>
4083
4084
</tag-desc>
4085
</list>
4086
4087
</tag-desc>
4088
4089
</list>
4090
</para>
4091
4092
</section>
4093
4094
4095
<section id="cryptokey" name="CryptoKey">
4096
4097
<para>
4098
<table width="100%">
4099
<tr><td><link id="cryptokey_alg"><literal>CryptoKey.algorithm</literal></link></td></tr>
4100
<tr><td><link id="cryptokey_extractable"><literal>CryptoKey.extractable</literal></link></td></tr>
4101
<tr><td><link id="cryptokey_type"><literal>CryptoKey.type</literal></link></td></tr>
4102
<tr><td><link id="cryptokey_usages"><literal>CryptoKey.usages</literal></link></td></tr>
4103
</table>
4104
</para>
4105
4106
<para>
4107
The <literal>CryptoKey</literal> object
4108
represents a cryptographic <literal>key</literal> obtained
4109
from one of the <literal>SubtleCrypto</literal> methods:
4110
<link id="crypto_subtle_generate_key"><literal>сrypto.subtle.generateKey()</literal></link>,
4111
<link id="crypto_subtle_derive_key"><literal>сrypto.subtle.deriveKey()</literal></link>,
4112
<link id="crypto_subtle_import_key"><literal>сrypto.subtle.importKey()</literal></link>.
4113
</para>
4114
4115
<para>
4116
<list type="tag">
4117
4118
<tag-name id="cryptokey_alg"><literal>CryptoKey.algorithm</literal></tag-name>
4119
<tag-desc>
4120
returns an object describing the algorithm for which this key can be used
4121
and any associated extra parameters
4122
(since <link doc="changes.xml" id="njs0.8.0">0.8.0</link>),
4123
read-only
4124
</tag-desc>
4125
4126
<tag-name id="cryptokey_extractable"><literal>CryptoKey.extractable</literal></tag-name>
4127
<tag-desc>
4128
a boolean value, <literal>true</literal> if the key can be exported
4129
(since <link doc="changes.xml" id="njs0.8.0">0.8.0</link>),
4130
read-only
4131
</tag-desc>
4132
4133
<tag-name id="cryptokey_type"><literal>CryptoKey.type</literal></tag-name>
4134
<tag-desc>
4135
a string value that indicates which kind of key is represented by the object,
4136
read-only.
4137
Possible values:
4138
<list type="tag">
4139
4140
<tag-name><literal>secret</literal></tag-name>
4141
<tag-desc>
4142
This key is a secret key for use with a symmetric algorithm.
4143
</tag-desc>
4144
4145
<tag-name><literal>private</literal></tag-name>
4146
<tag-desc>
4147
This key is the private half of an asymmetric algorithm's
4148
<link id="cryptokeypair"><literal>CryptoKeyPair</literal></link>
4149
</tag-desc>
4150
4151
<tag-name><literal>public</literal></tag-name>
4152
<tag-desc>
4153
This key is the public half of an asymmetric algorithm's
4154
<link id="cryptokeypair"><literal>CryptoKeyPair</literal></link>.
4155
</tag-desc>
4156
4157
</list>
4158
</tag-desc>
4159
4160
<tag-name id="cryptokey_usages"><literal>CryptoKey.usages</literal></tag-name>
4161
<tag-desc>
4162
An array of strings indicating what this key can be used for
4163
(since <link doc="changes.xml" id="njs0.8.0">0.8.0</link>),
4164
read-only.
4165
Possible array values:
4166
<list type="tag">
4167
4168
<tag-name><literal>encrypt</literal></tag-name>
4169
<tag-desc>
4170
key for encrypting messages
4171
</tag-desc>
4172
4173
<tag-name><literal>decrypt</literal></tag-name>
4174
<tag-desc>
4175
key for decrypting messages
4176
</tag-desc>
4177
4178
<tag-name><literal>sign</literal></tag-name>
4179
<tag-desc>
4180
key for signing messages
4181
</tag-desc>
4182
4183
<tag-name><literal>verify</literal></tag-name>
4184
<tag-desc>
4185
key for verifying signatures
4186
</tag-desc>
4187
4188
<tag-name><literal>deriveKey</literal></tag-name>
4189
<tag-desc>
4190
key for deriving a new key
4191
</tag-desc>
4192
4193
<tag-name><literal>deriveBits</literal></tag-name>
4194
<tag-desc>
4195
key for deriving bits
4196
</tag-desc>
4197
4198
</list>
4199
</tag-desc>
4200
4201
</list>
4202
</para>
4203
4204
</section>
4205
4206
4207
<section id="cryptokeypair" name="CryptoKeyPair">
4208
4209
<para>
4210
<table width="100%">
4211
<tr><td><link id="cryptokeypair_privatekey"><literal>CryptoKeyPair.privateKey</literal></link></td></tr>
4212
<tr><td><link id="cryptokeypair_publickey"><literal>CryptoKeyPair.publicKey</literal></link></td></tr>
4213
</table>
4214
</para>
4215
4216
<para>
4217
The <literal>CryptoKeyPair</literal> is a dictionary object
4218
of the <link id="builtin_crypto">WebCrypto API</link>
4219
that represents an asymmetric key pair.
4220
</para>
4221
4222
<para>
4223
<list type="tag">
4224
4225
<tag-name id="cryptokeypair_privatekey"><literal>CryptoKeyPair.privateKey</literal></tag-name>
4226
<tag-desc>
4227
A <link id="cryptokey"><literal>CryptoKey</literal></link> object
4228
representing the private key.
4229
</tag-desc>
4230
4231
<tag-name id="cryptokeypair_publickey"><literal>CryptoKeyPair.publicKey</literal></tag-name>
4232
<tag-desc>
4233
A <link id="cryptokey"><literal>CryptoKey</literal></link> object
4234
representing the public key.
4235
</tag-desc>
4236
4237
</list>
4238
</para>
4239
4240
</section>
4241
4242
4243
<section id="njs" name="njs">
4244
4245
<para>
4246
<table width="100%">
4247
<tr><td><link id="njs_version"><literal>njs.version</literal></link></td></tr>
4248
<tr><td><link id="njs_version_number"><literal>njs.version_number</literal></link></td></tr>
4249
<tr><td><link id="njs_dump"><literal>njs.dump()</literal></link></td></tr>
4250
<tr><td><link id="njs_memory_stats"><literal>njs.memoryStats</literal></link></td></tr>
4251
<tr><td><link id="njs_on"><literal>njs.on()</literal></link></td></tr>
4252
</table>
4253
</para>
4254
4255
<para>
4256
The <literal>njs</literal> object is a global object
4257
that represents the current VM instance
4258
(since <link doc="changes.xml" id="njs0.2.0">0.2.0</link>).
4259
</para>
4260
4261
<para>
4262
<list type="tag">
4263
4264
<tag-name id="njs_version"><literal>njs.version</literal></tag-name>
4265
<tag-desc>
4266
Returns a string with the current version of njs
4267
(for example, “0.7.4”).
4268
</tag-desc>
4269
4270
<tag-name id="njs_version_number"><literal>njs.version_number</literal></tag-name>
4271
<tag-desc>
4272
Returns a number with the current version of njs.
4273
For example, “0.7.4” is returned as <literal>0x000704</literal>
4274
(since <link doc="changes.xml" id="njs0.7.4">0.7.4</link>).
4275
</tag-desc>
4276
4277
<tag-name id="njs_dump"><literal>njs.dump(<value>value</value>)</literal></tag-name>
4278
<tag-desc>
4279
Returns the pretty-print string representation for a value.
4280
</tag-desc>
4281
4282
<tag-name id="njs_memory_stats"><literal>njs.memoryStats</literal></tag-name>
4283
<tag-desc>
4284
Object containing memory statistics for current VM instance
4285
(since <link doc="changes.xml" id="njs0.7.8">0.7.8</link>).
4286
<list type="tag">
4287
4288
<tag-name><literal>size</literal></tag-name>
4289
<tag-desc>
4290
amount of memory in bytes njs memory pool claimed from the operating system.
4291
</tag-desc>
4292
4293
</list>
4294
</tag-desc>
4295
4296
<tag-name id="njs_on"><literal>njs.on(<value>event</value>,
4297
<value>callback</value>)</literal></tag-name>
4298
<tag-desc>
4299
Registers a callback for the specified VM event
4300
(since <link doc="changes.xml" id="njs0.5.2">0.5.2</link>).
4301
An event may be one of the following strings:
4302
<list type="tag">
4303
4304
<tag-name><literal>exit</literal></tag-name>
4305
<tag-desc>
4306
is called before the VM is destroyed.
4307
The callback is called without arguments.
4308
</tag-desc>
4309
4310
</list>
4311
</tag-desc>
4312
4313
</list>
4314
</para>
4315
4316
</section>
4317
4318
4319
<section id="process" name="process">
4320
4321
<para>
4322
<table width="100%">
4323
<tr><td><link id="process_argv"><literal>process.argv</literal></link></td></tr>
4324
<tr><td><link id="process_env"><literal>process.env</literal></link></td></tr>
4325
<tr><td><link id="process_kill"><literal>process.kill()</literal></link></td></tr>
4326
<tr><td><link id="process_pid"><literal>process.pid</literal></link></td></tr>
4327
<tr><td><link id="process_ppid"><literal>process.ppid</literal></link></td></tr>
4328
</table>
4329
</para>
4330
4331
<para>
4332
The <literal>process</literal> object is a global object
4333
that provides information about the current process
4334
(<link doc="changes.xml" id="njs0.3.3">0.3.3</link>).
4335
</para>
4336
4337
<para>
4338
<list type="tag">
4339
4340
<tag-name id="process_argv"><literal>process.argv</literal></tag-name>
4341
<tag-desc>
4342
Returns an array that contains the command line arguments
4343
passed when the current process was launched.
4344
</tag-desc>
4345
4346
<tag-name id="process_env"><literal>process.env</literal></tag-name>
4347
<tag-desc>
4348
Returns an object containing the user environment.
4349
<note>
4350
By default, nginx removes all environment variables inherited
4351
from its parent process except the TZ variable.
4352
Use the <link doc="../ngx_core_module.xml" id="env"/> directive
4353
to preserve some of the inherited variables.
4354
</note>
4355
</tag-desc>
4356
4357
<tag-name id="process_kill"><literal>process.kill(<value>pid</value>,
4358
<value>number</value> | <value>string</value>)</literal></tag-name>
4359
<tag-desc>
4360
Sends the signal to the process identified by <literal>pid</literal>.
4361
Signal names are numbers or strings such as 'SIGINT' or 'SIGHUP'.
4362
See <link url="https://man7.org/linux/man-pages/man2/kill.2.html">kill(2)</link>
4363
for more information.
4364
</tag-desc>
4365
4366
<tag-name id="process_pid"><literal>process.pid</literal></tag-name>
4367
<tag-desc>
4368
Returns the PID of the current process.
4369
</tag-desc>
4370
4371
<tag-name id="process_ppid"><literal>process.ppid</literal></tag-name>
4372
<tag-desc>
4373
Returns the PID of the current parent process.
4374
</tag-desc>
4375
4376
</list>
4377
</para>
4378
4379
</section>
4380
4381
4382
<section id="string" name="String">
4383
4384
<para>
4385
By default all strings in njs are Unicode strings.
4386
They correspond to ECMAScript strings that contain Unicode characters.
4387
Before <link doc="changes.xml" id="njs0.8.0">0.8.0</link>,
4388
byte strings were also supported.
4389
</para>
4390
4391
<section id="byte_string" name="Byte strings">
4392
4393
<para>
4394
<note>
4395
Since <link doc="changes.xml" id="njs0.8.0">0.8.0</link>,
4396
the support for byte strings and byte string methods were removed.
4397
When working with byte sequence,
4398
the <link id="buffer">Buffer</link> object
4399
and <literal>Buffer</literal> properties, such as
4400
<link id="r_request_buffer"><literal>r.requestBuffer</literal></link>,
4401
<link id="r_raw_variables"><literal>r.rawVariables</literal></link>,
4402
should be used.
4403
</note>
4404
</para>
4405
4406
<para>
4407
Byte strings contain a sequence of bytes
4408
and are used to serialize Unicode strings
4409
to external data and deserialize from external sources.
4410
For example, the <link id="string_toutf8">toUTF8()</link> method serializes
4411
a Unicode string to a byte string using UTF-8 encoding:
4412
<example>
4413
>> '£'.toUTF8().toString('hex')
4414
'c2a3' /* C2 A3 is the UTF-8 representation of 00A3 ('£') code point */
4415
</example>
4416
The <link id="string_tobytes">toBytes()</link> method serializes
4417
a Unicode string with code points up to 255 into a byte string,
4418
otherwise, <literal>null</literal> is returned:
4419
<example>
4420
>> '£'.toBytes().toString('hex')
4421
'a3' /* a3 is a byte equal to 00A3 ('£') code point */
4422
</example>
4423
4424
<list type="tag">
4425
4426
<tag-name id="string_bytesfrom"><literal>String.bytesFrom(<value>array</value>
4427
| <value>string</value>, <value>encoding</value>)</literal></tag-name>
4428
<tag-desc>
4429
The method was made obsolete in
4430
<link doc="changes.xml" id="njs0.4.4">0.4.4</link>
4431
and was removed in <link doc="changes.xml" id="njs0.8.0">0.8.0</link>.
4432
The <literal>Buffer.from</literal> method should be used instead:
4433
<example>
4434
>> Buffer.from([0x62, 0x75, 0x66, 0x66, 0x65, 0x72]).toString()
4435
'buffer'
4436
4437
>> Buffer.from('YnVmZmVy', 'base64').toString()
4438
'buffer'
4439
</example>
4440
Before <link doc="changes.xml" id="njs0.4.4">0.4.4</link>,
4441
created a byte string either from an array that contained octets,
4442
or from an encoded string
4443
(<link doc="changes.xml" id="njs0.2.3">0.2.3</link>),
4444
the encoding could be
4445
<literal>hex</literal>,
4446
<literal>base64</literal>, and
4447
<literal>base64url</literal>.
4448
</tag-desc>
4449
4450
<tag-name id="string_frombytes"><literal>String.prototype.fromBytes(<value>start</value>[,
4451
<value>end</value>])</literal></tag-name>
4452
<tag-desc>
4453
the property was made obsolete in
4454
<link doc="changes.xml" id="njs0.7.7">0.7.7</link>
4455
and was removed in <link doc="changes.xml" id="njs0.8.0">0.8.0</link>.
4456
Before <link doc="changes.xml" id="njs0.7.7">0.7.7</link>,
4457
returned a new Unicode string from a byte string
4458
where each byte was replaced with a corresponding Unicode code point.
4459
</tag-desc>
4460
4461
<tag-name id="string_fromutf8"><literal>String.prototype.fromUTF8(<value>start</value>[,
4462
<value>end</value>])</literal></tag-name>
4463
<tag-desc>
4464
the property was made obsolete in
4465
<link doc="changes.xml" id="njs0.7.7">0.7.7</link>
4466
and was removed in <link doc="changes.xml" id="njs0.8.0">0.8.0</link>.
4467
The <link id="textedeoder"><literal>TextDecoder</literal></link> method
4468
should be used instead.
4469
Before <link doc="changes.xml" id="njs0.7.7">0.7.7</link>,
4470
converted a byte string containing a valid UTF-8 string
4471
into a Unicode string,
4472
otherwise <literal>null</literal> was returned.
4473
</tag-desc>
4474
4475
<tag-name id="string_tobytes"><literal>String.prototype.toBytes(<value>start</value>[,
4476
<value>end</value>])</literal></tag-name>
4477
<tag-desc>
4478
the property was made obsolete in
4479
<link doc="changes.xml" id="njs0.7.7">0.7.7</link>
4480
and was removed in <link doc="changes.xml" id="njs0.8.0">0.8.0</link>.
4481
Before <link doc="changes.xml" id="njs0.7.7">0.7.7</link>,
4482
serialized a Unicode string to a byte string,
4483
returned <literal>null</literal> if a character larger than 255 was
4484
found in the string.
4485
</tag-desc>
4486
4487
<tag-name id="string_tostring"><literal>String.prototype.toString(<value>encoding</value>)</literal></tag-name>
4488
<tag-desc>
4489
<para>
4490
the property was made obsolete in
4491
<link doc="changes.xml" id="njs0.7.7">0.7.7</link>
4492
and was removed in <link doc="changes.xml" id="njs0.8.0">0.8.0</link>.
4493
Before <link doc="changes.xml" id="njs0.7.7">0.7.7</link>,
4494
encoded a string to
4495
<literal>hex</literal>,
4496
<literal>base64</literal>, or
4497
<literal>base64url</literal>:
4498
<example>
4499
>> 'αβγδ'.toString('base64url')
4500
'zrHOss6zzrQ'
4501
</example>
4502
Before version <link doc="changes.xml" id="njs0.4.3">0.4.3</link>,
4503
only a <link id="string_tobytes">byte string</link> could be encoded:
4504
<example>
4505
>> 'αβγδ'.toUTF8().toString('base64url')
4506
'zrHOss6zzrQ'
4507
</example>
4508
</para>
4509
</tag-desc>
4510
4511
<tag-name id="string_toutf8"><literal>String.prototype.toUTF8(<value>start</value>[,
4512
<value>end</value>])</literal></tag-name>
4513
<tag-desc>
4514
the property was made obsolete in
4515
<link doc="changes.xml" id="njs0.7.7">0.7.7</link>
4516
and was removed in <link doc="changes.xml" id="njs0.8.0">0.8.0</link>.
4517
The <link id="textencoder"><literal>TextEncoder</literal></link> method
4518
should be used instead.
4519
Before <link doc="changes.xml" id="njs0.7.7">0.7.7</link>,
4520
serialized a Unicode string
4521
to a byte string using UTF-8 encoding:
4522
<example>
4523
>> 'αβγδ'.toUTF8().length
4524
8
4525
>> 'αβγδ'.length
4526
4
4527
</example>
4528
</tag-desc>
4529
4530
</list>
4531
</para>
4532
4533
</section>
4534
4535
</section>
4536
4537
</section>
4538
4539
4540
<section id="webapi" name="web API">
4541
4542
4543
<section id="textdecoder" name="Text Decoder">
4544
4545
<para>
4546
<table width="100%">
4547
<tr><td><link id="textdecoder_constructor"><literal>TextDecoder()</literal></link></td></tr>
4548
<tr><td><link id="textdecoder_encoding"><literal>TextDecoder.prototype.encoding</literal></link></td></tr>
4549
<tr><td><link id="textdecoder_fatal"><literal>TextDecoder.prototype.fatal</literal></link></td></tr>
4550
<tr><td><link id="textdecoder_ignorebom"><literal>TextDecoder.prototype.ignoreBOM</literal></link></td></tr>
4551
<tr><td><link id="textdecoder_decode"><literal>TextDecoder.prototype.decode()</literal></link></td></tr>
4552
</table>
4553
</para>
4554
4555
<para>
4556
The <literal>TextDecoder</literal>
4557
produces a stream of code points
4558
from a stream of bytes
4559
(<link doc="changes.xml" id="njs0.4.3">0.4.3</link>).
4560
</para>
4561
4562
<para>
4563
<list type="tag">
4564
4565
<tag-name id="textdecoder_constructor"><literal>TextDecoder([[<value>encoding</value>],
4566
<value>options</value>])</literal></tag-name>
4567
<tag-desc>
4568
Creates a new <literal>TextDecoder</literal> object
4569
for specified <literal>encoding</literal>,
4570
currently, only UTF-8 is supported.
4571
The <literal>options</literal> is
4572
<literal>TextDecoderOptions</literal> dictionary with the property:
4573
4574
<list type="tag">
4575
4576
<tag-name><literal>fatal</literal></tag-name>
4577
<tag-desc>
4578
boolean flag indicating if
4579
<link id="textdecoder_decode"><literal>TextDecoder.decode()</literal></link>
4580
must throw the <value>TypeError</value> exception when
4581
a coding error is found, by default is <literal>false</literal>.
4582
</tag-desc>
4583
4584
</list>
4585
</tag-desc>
4586
4587
<tag-name id="textdecoder_encoding"><literal>TextDecoder.prototype.encoding</literal></tag-name>
4588
<tag-desc>
4589
Returns a string with the name of the encoding used by
4590
<link id="textdecoder"><literal>TextDecoder()</literal></link>,
4591
read-only.
4592
</tag-desc>
4593
4594
<tag-name id="textdecoder_fatal"><literal>TextDecoder.prototype.fatal</literal></tag-name>
4595
<tag-desc>
4596
boolean flag, <literal>true</literal> if
4597
the error mode is fatal,
4598
read-only.
4599
</tag-desc>
4600
4601
<tag-name id="textdecoder_ignorebom"><literal>TextDecoder.prototype.ignoreBOM</literal></tag-name>
4602
<tag-desc>
4603
boolean flag, <literal>true</literal> if
4604
the byte order marker is ignored,
4605
read-only.
4606
</tag-desc>
4607
4608
<tag-name id="textdecoder_decode"><literal>TextDecoder.prototype.decode(<value>buffer</value>,
4609
[<value>options</value>])</literal></tag-name>
4610
<tag-desc>
4611
Returns a string with the text
4612
decoded from the <literal>buffer</literal> by
4613
<link id="textdecoder"><literal>TextDecoder()</literal></link>.
4614
The buffer can be <literal>ArrayBuffer</literal>.
4615
The <literal>options</literal> is
4616
<literal>TextDecodeOptions</literal> dictionary with the property:
4617
4618
<list type="tag">
4619
4620
<tag-name><literal>stream</literal></tag-name>
4621
<tag-desc>
4622
boolean flag indicating if
4623
additional data will follow in subsequent calls to <literal>decode()</literal>:
4624
<literal>true</literal> if processing the data in chunks, and
4625
<literal>false</literal> for the final chunk
4626
or if the data is not chunked.
4627
By default is <literal>false</literal>.
4628
</tag-desc>
4629
4630
</list>
4631
<example>
4632
>> (new TextDecoder()).decode(new Uint8Array([206,177,206,178]))
4633
αβ
4634
</example>
4635
</tag-desc>
4636
4637
</list>
4638
</para>
4639
4640
</section>
4641
4642
4643
<section id="textencoder" name="Text Encoder">
4644
4645
<para>
4646
<table width="100%">
4647
<tr><td><link id="textencoder_constructor"><literal>TextEncoder()</literal></link></td></tr>
4648
<tr><td><link id="textencoder_encode"><literal>TextEncoder.prototype.encode()</literal></link></td></tr>
4649
<tr><td><link id="textencoder_encodeinto"><literal>TextEncoder.prototype.encodeInto()</literal></link></td></tr>
4650
</table>
4651
</para>
4652
4653
<para>
4654
The <literal>TextEncoder</literal> object
4655
produces a byte stream with UTF-8 encoding
4656
from a stream of code points
4657
(<link doc="changes.xml" id="njs0.4.3">0.4.3</link>).
4658
</para>
4659
4660
<para>
4661
<list type="tag">
4662
4663
<tag-name id="textencoder_constructor"><literal>TextEncoder()</literal></tag-name>
4664
<tag-desc>
4665
Returns a newly constructed <literal>TextEncoder</literal>
4666
that will generate a byte stream with UTF-8 encoding.
4667
</tag-desc>
4668
4669
<tag-name id="textencoder_encode"><literal>TextEncoder.prototype.encode(<value>string</value>)</literal></tag-name>
4670
<tag-desc>
4671
Encodes <literal>string</literal> into a <literal>Uint8Array</literal>
4672
with UTF-8 encoded text.
4673
</tag-desc>
4674
4675
<tag-name id="textencoder_encodeinto"><literal>TextEncoder.prototype.encodeInto(<value>string</value>,
4676
<value>uint8Array</value>)</literal></tag-name>
4677
<tag-desc>
4678
Encodes a <literal>string</literal> to UTF-8,
4679
puts the result into destination <literal>Uint8Array</literal>, and
4680
returns a dictionary object that shows the progress of the encoding.
4681
The dictionary object contains two members:
4682
4683
<list type="tag">
4684
4685
<tag-name><literal>read</literal></tag-name>
4686
<tag-desc>
4687
the number of UTF-16 units of code from the source <literal>string</literal>
4688
converted to UTF-8
4689
</tag-desc>
4690
4691
<tag-name><literal>written</literal></tag-name>
4692
<tag-desc>
4693
the number of bytes modified in the destination <literal>Uint8Array</literal>
4694
</tag-desc>
4695
4696
</list>
4697
</tag-desc>
4698
4699
</list>
4700
</para>
4701
4702
</section>
4703
4704
</section>
4705
4706
4707
<section id="njs_api_timers" name="timers">
4708
4709
<para>
4710
<table width="100%">
4711
<tr><td><link id="cleartimeout"><literal>clearTimeout()</literal></link></td></tr>
4712
<tr><td><link id="settimeout"><literal>setTimeout()</literal></link></td></tr>
4713
</table>
4714
</para>
4715
4716
<para>
4717
<list type="tag">
4718
4719
<tag-name id="cleartimeout"><literal>clearTimeout(<value>timeout</value>)</literal></tag-name>
4720
<tag-desc>
4721
Cancels a <literal>timeout</literal> object
4722
created by <link id="settimeout"><literal>setTimeout()</literal></link>.
4723
</tag-desc>
4724
4725
<tag-name id="settimeout"><literal>setTimeout(<value>function</value>,
4726
<value>milliseconds</value>[,
4727
<value>argument1</value>,
4728
<value>argumentN</value>])</literal></tag-name>
4729
<tag-desc>
4730
Calls a <literal>function</literal>
4731
after a specified number of <literal>milliseconds</literal>.
4732
One or more optional <literal>arguments</literal>
4733
can be passed to the specified function.
4734
Returns a <literal>timeout</literal> object.
4735
<example>
4736
function handler(v)
4737
{
4738
// ...
4739
}
4740
4741
t = setTimeout(handler, 12);
4742
4743
// ...
4744
4745
clearTimeout(t);
4746
</example>
4747
</tag-desc>
4748
4749
</list>
4750
</para>
4751
4752
<section id="njs_global_functions" name="Global functions">
4753
4754
<para>
4755
<table width="100%">
4756
<tr><td><link id="atob"><literal>atob()</literal></link></td></tr>
4757
<tr><td><link id="btoa"><literal>btoa()</literal></link></td></tr>
4758
</table>
4759
</para>
4760
4761
<para>
4762
<list type="tag">
4763
4764
<tag-name id="atob"><literal>atob(<value>encodedData</value>)</literal></tag-name>
4765
<tag-desc>
4766
Decodes a string of data which has been encoded
4767
using <literal>Base64</literal> encoding.
4768
The <literal>encodedData</literal> parameter is a binary string
4769
that contains Base64-encoded data.
4770
Returns a string that contains decoded data from <literal>encodedData</literal>.
4771
<para>
4772
The similar <link id="btoa"><literal>btoa()</literal></link> method
4773
can be used to encode and transmit data
4774
which may otherwise cause communication problems,
4775
then transmit it and use the <literal>atob()</literal> method
4776
to decode the data again.
4777
For example, you can encode, transmit, and decode control characters
4778
such as ASCII values <literal>0</literal> through <literal>31</literal>.
4779
<example>
4780
const encodedData = btoa("text to encode"); // encode a string
4781
const decodedData = atob(encodedData); // decode the string
4782
</example>
4783
</para>
4784
</tag-desc>
4785
4786
<tag-name id="btoa"><literal>btoa(<value>stringToEncode</value>)</literal></tag-name>
4787
<tag-desc>
4788
Creates a Base64-encoded ASCII string from a binary string.
4789
The <literal>stringToEncode</literal> parameter is a binary string to encode.
4790
Returns an ASCII string containing the Base64 representation of
4791
<literal>stringToEncode</literal>.
4792
<para>
4793
The method can be used to encode data
4794
which may otherwise cause communication problems, transmit it,
4795
then use the <link id="atob"><literal>atob()</literal></link> method
4796
to decode the data again.
4797
For example, you can encode control characters
4798
such as ASCII values <literal>0</literal> through <literal>31</literal>.
4799
<example>
4800
const encodedData = btoa("text to encode"); // encode a string
4801
const decodedData = atob(encodedData); // decode the string
4802
</example>
4803
</para>
4804
</tag-desc>
4805
4806
</list>
4807
</para>
4808
4809
</section>
4810
4811
</section>
4812
4813
4814
<section id="builtin_modules" name="built-in modules">
4815
4816
4817
<section id="buffer" name="Buffer">
4818
4819
<para>
4820
<table width="100%">
4821
<tr><td><link id="buffer_alloc"><literal>Buffer.alloc()</literal></link></td></tr>
4822
<tr><td><link id="buffer_alloc_unsafe"><literal>Buffer.allocUnsafe()</literal></link></td></tr>
4823
<tr><td><link id="buffer_bytelength"><literal>Buffer.byteLength()</literal></link></td></tr>
4824
<tr><td><link id="buffer_compare"><literal>Buffer.compare()</literal></link></td></tr>
4825
<tr><td><link id="buffer_concat"><literal>Buffer.concat()</literal></link></td></tr>
4826
<tr><td><link id="buffer_from_array"><literal>Buffer.from(array)</literal></link></td></tr>
4827
<tr><td><link id="buffer_from_arraybuffer"><literal>Buffer.from(arrayBuffer)</literal></link></td></tr>
4828
<tr><td><link id="buffer_from_buffer"><literal>Buffer.from(buffer)</literal></link></td></tr>
4829
<tr><td><link id="buffer_from_object"><literal>Buffer.from(object)</literal></link></td></tr>
4830
<tr><td><link id="buffer_from_string"><literal>Buffer.from(string)</literal></link></td></tr>
4831
<tr><td><link id="buffer_is_buffer"><literal>Buffer.isBuffer()</literal></link></td></tr>
4832
<tr><td><link id="buffer_is_encoding"><literal>Buffer.isEncoding()</literal></link></td></tr>
4833
<tr><td><link id="buf_index"><literal>buffer[]</literal></link></td></tr>
4834
<tr><td><link id="buf_buffer"><literal>buf.buffer</literal></link></td></tr>
4835
<tr><td><link id="buf_byte_offset"><literal>buf.byteOffset</literal></link></td></tr>
4836
<tr><td><link id="buf_compare"><literal>buf.compare()</literal></link></td></tr>
4837
<tr><td><link id="buf_copy"><literal>buf.copy()</literal></link></td></tr>
4838
<tr><td><link id="buf_equals"><literal>buf.equals()</literal></link></td></tr>
4839
<tr><td><link id="buf_fill"><literal>buf.fill()</literal></link></td></tr>
4840
<tr><td><link id="buf_includes"><literal>buf.includes()</literal></link></td></tr>
4841
<tr><td><link id="buf_indexof"><literal>buf.indexOf()</literal></link></td></tr>
4842
<tr><td><link id="buf_lastindexof"><literal>buf.lastIndexOf()</literal></link></td></tr>
4843
<tr><td><link id="buf_length"><literal>buf.length</literal></link></td></tr>
4844
<tr><td><link id="buf_readintbe"><literal>buf.readIntBE()</literal></link></td></tr>
4845
<tr><td><link id="buf_readintle"><literal>buf.readIntLE()</literal></link></td></tr>
4846
<tr><td><link id="buf_readuintbe"><literal>buf.readUIntBE()</literal></link></td></tr>
4847
<tr><td><link id="buf_readuintle"><literal>buf.readUIntLE()</literal></link></td></tr>
4848
<tr><td><link id="buf_readdobulebe"><literal>buf.readDoubleBE</literal></link></td></tr>
4849
<tr><td><link id="buf_readdobulele"><literal>buf.readDoubleLE()</literal></link></td></tr>
4850
<tr><td><link id="buf_readfloatbe"><literal>buf.readFloatBE()</literal></link></td></tr>
4851
<tr><td><link id="buf_readfloatle"><literal>buf.readFloatLE()</literal></link></td></tr>
4852
<tr><td><link id="buf_subarray"><literal>buf.subarray()</literal></link></td></tr>
4853
<tr><td><link id="buf_slice"><literal>buf.slice()</literal></link></td></tr>
4854
<tr><td><link id="buf_swap16"><literal>buf.swap16()</literal></link></td></tr>
4855
<tr><td><link id="buf_swap32"><literal>buf.swap32()</literal></link></td></tr>
4856
<tr><td><link id="buf_swap64"><literal>buf.swap64()</literal></link></td></tr>
4857
<tr><td><link id="buf_tojson"><literal>buf.toJSON()</literal></link></td></tr>
4858
<tr><td><link id="buf_tostring"><literal>buf.toString()</literal></link></td></tr>
4859
<tr><td><link id="buf_write"><literal>buf.write()</literal></link></td></tr>
4860
<tr><td><link id="buf_writeintbe"><literal>buf.writeIntBE()</literal></link></td></tr>
4861
<tr><td><link id="buf_writeintle"><literal>buf.writeIntLE()</literal></link></td></tr>
4862
<tr><td><link id="buf_writeuintbe"><literal>buf.writeUIntBE()</literal></link></td></tr>
4863
<tr><td><link id="buf_writeuintle"><literal>buf.writeUIntLE()</literal></link></td></tr>
4864
<tr><td><link id="buf_writedoublebe"><literal>buf.writeDoubleBE()</literal></link></td></tr>
4865
<tr><td><link id="buf_writedoublele"><literal>buf.writeDoubleLE()</literal></link></td></tr>
4866
<tr><td><link id="buf_writefloatbe"><literal>buf.writeFloatBE()</literal></link></td></tr>
4867
<tr><td><link id="buf_writefloatle"><literal>buf.writeFloatLE()</literal></link></td></tr>
4868
</table>
4869
</para>
4870
4871
<para>
4872
<list type="tag">
4873
4874
<tag-name id="buffer_alloc"><literal>Buffer.alloc(<value>size</value>[,
4875
<value>fill</value>[,
4876
<value>encoding</value>]]))</literal></tag-name>
4877
<tag-desc>
4878
<para>
4879
Allocates a new Buffer of a specified <value>size</value>.
4880
If <value>fill</value> is not specified, the Buffer will be zero-filled.
4881
If <value>fill</value> is specified,
4882
the allocated Buffer will be initialized by calling
4883
<link id="buf_fill"><literal>buf.fill(fill)</literal></link>.
4884
If <value>fill</value> and <value>encoding</value> are specified,
4885
the allocated Buffer will be initialized by calling
4886
<link id="buf_fill"><literal>buf.fill(fill,
4887
encoding)</literal></link>.
4888
</para>
4889
4890
<para>
4891
The <value>fill</value> parameter may be a
4892
<value>string</value>,
4893
<value>Buffer</value>,
4894
<value>Uint8Array</value>, or
4895
<value>integer</value>.
4896
</para>
4897
</tag-desc>
4898
4899
<tag-name id="buffer_alloc_unsafe"><literal>Buffer.allocUnsafe(<value>size</value>)</literal></tag-name>
4900
<tag-desc>
4901
<para>
4902
The same as
4903
<link id="buffer_alloc"><literal>Buffer.alloc()</literal></link>,
4904
with the difference that the memory allocated for the buffer is not initialized,
4905
the contents of the new buffer is unknown and may contain sensitive data.
4906
</para>
4907
</tag-desc>
4908
4909
<tag-name id="buffer_bytelength"><literal>Buffer.byteLength(<value>value</value>[,
4910
<value>encoding</value>])</literal></tag-name>
4911
<tag-desc>
4912
Returns the byte length of a specified value,
4913
when encoded using <value>encoding</value>.
4914
The value can be a
4915
<literal>string</literal>,
4916
<literal>Buffer</literal>,
4917
<literal>TypedArray</literal>,
4918
<literal>DataView</literal>, or
4919
<literal>ArrayBuffer</literal>.
4920
If the value is a <value>string</value>,
4921
the <literal>encoding</literal> parameter is its encoding, can be
4922
<value>utf8</value>,
4923
<value>hex</value>,
4924
<value>base64</value>,
4925
<value>base64url</value>;
4926
by default is <value>utf8</value>.
4927
</tag-desc>
4928
4929
<tag-name id="buffer_compare"><literal>Buffer.compare(<value>buffer1</value>,
4930
<value>buffer2</value>)</literal></tag-name>
4931
<tag-desc>
4932
Compares <value>buffer1</value> with <value>buffer2</value>
4933
when sorting arrays of Buffer instances.
4934
Returns
4935
<literal>0</literal> if
4936
<value>buffer1</value> is the same as <value>buffer2</value>,
4937
<literal>1</literal> if
4938
<value>buffer2</value> should come before <value>buffer1</value> when sorted, or
4939
<literal>-1</literal> if
4940
<value>buffer2</value> should come after <value>buffer1</value> when sorted.
4941
</tag-desc>
4942
4943
<tag-name id="buffer_concat"><literal>Buffer.concat(<value>list</value>[,
4944
<value>totalLength</value>])</literal></tag-name>
4945
<tag-desc>
4946
Returns a new Buffer
4947
which is the result of concatenating all the Buffer instances in the list.
4948
If there are no items in the list or the total length is 0,
4949
a new zero-length Buffer is returned.
4950
If <value>totalLength</value> is not specified,
4951
it is calculated from the Buffer instances in list by adding their lengths.
4952
If <value>totalLength</value> is specified,
4953
it is coerced to an unsigned integer.
4954
If the combined length of the Buffers in list exceeds
4955
<value>totalLength</value>,
4956
the result is truncated to <value>totalLength</value>.
4957
</tag-desc>
4958
4959
<tag-name id="buffer_from_array"><literal>Buffer.from(<value>array</value>)</literal></tag-name>
4960
<tag-desc>
4961
Allocates a new Buffer using an array of bytes
4962
in the range <literal>0</literal> <literal>255</literal>.
4963
Array entries outside that range will be truncated.
4964
</tag-desc>
4965
4966
<tag-name id="buffer_from_arraybuffer"><literal>Buffer.from(<value>arrayBuffer</value>,
4967
<value>byteOffset</value>[,
4968
<value>length</value>]])</literal></tag-name>
4969
<tag-desc>
4970
Creates a view of the <value>ArrayBuffer</value>
4971
without copying the underlying memory.
4972
The optional <value>byteOffset</value> and <value>length</value> arguments
4973
specify a memory range within the <value>arrayBuffer</value>
4974
that will be shared by the Buffer.
4975
</tag-desc>
4976
4977
<tag-name id="buffer_from_buffer"><literal>Buffer.from(<value>buffer</value>)</literal></tag-name>
4978
<tag-desc>
4979
Copies the passed buffer data onto a new Buffer instance.
4980
</tag-desc>
4981
4982
<tag-name id="buffer_from_object"><literal>Buffer.from(<value>object</value>[,
4983
<value>offsetOrEncoding</value>[,
4984
<value>length</value>]])</literal></tag-name>
4985
<tag-desc>
4986
For objects whose <literal>valueOf()</literal> function
4987
returns a value not strictly equal to object,
4988
returns
4989
<literal>Buffer.from(object.valueOf()</literal>,
4990
<literal>offsetOrEncoding</literal>,
4991
<literal>length</literal>).
4992
</tag-desc>
4993
4994
<tag-name id="buffer_from_string"><literal>Buffer.from(<value>string</value>[,
4995
<value>encoding</value>])</literal></tag-name>
4996
<tag-desc>
4997
Creates a new Buffer with a <value>string</value>.
4998
The <value>encoding</value> parameter identifies the character encoding
4999
to be used when converting a string into bytes.
5000
The encoding can be
5001
<literal>utf8</literal>,
5002
<literal>hex</literal>,
5003
<literal>base64</literal>,
5004
<literal>base64url</literal>;
5005
by default is <literal>utf8</literal>.
5006
</tag-desc>
5007
5008
<tag-name id="buffer_is_buffer"><literal>Buffer.isBuffer(<value>object</value>)</literal></tag-name>
5009
<tag-desc>
5010
A boolean value,
5011
returns <literal>true</literal> if <value>object</value> is a Buffer.
5012
</tag-desc>
5013
5014
<tag-name id="buffer_is_encoding"><literal>Buffer.isEncoding(<value>encoding</value>)</literal></tag-name>
5015
<tag-desc>
5016
A boolean value,
5017
returns <literal>true</literal>
5018
if encoding is the name of a supported character encoding.
5019
</tag-desc>
5020
5021
<tag-name id="buf_index"><literal>buffer[<value>index</value>]</literal></tag-name>
5022
<tag-desc>
5023
The index operator that can be used to get and set the octet
5024
at position <literal>index</literal> in <literal>buffer</literal>.
5025
The values refer to individual bytes,
5026
so the legal value range is between 0 and 255 (decimal).
5027
</tag-desc>
5028
5029
<tag-name id="buf_buffer"><literal>buf.buffer</literal></tag-name>
5030
<tag-desc>
5031
The underlying <literal>ArrayBuffer</literal> object
5032
based on which this Buffer object is created.
5033
</tag-desc>
5034
5035
<tag-name id="buf_byte_offset"><literal>buf.byteOffset</literal></tag-name>
5036
<tag-desc>
5037
An integer,
5038
specifying the <literal>byteOffset</literal> of the Buffers
5039
underlying <literal>ArrayBuffer</literal> object.
5040
</tag-desc>
5041
5042
<tag-name id="buf_compare"><literal>buf.compare(<value>target</value>[,
5043
<value>targetStart</value>[,
5044
<value>targetEnd</value>[,
5045
<value>sourceStart</value>[,
5046
<value>sourceEnd</value>]]]])</literal></tag-name>
5047
<tag-desc>
5048
Compares buffer with <value>target</value> and returns a number
5049
indicating whether buffer comes before, after, or is the same
5050
as <value>target</value> in sort order.
5051
Comparison is based on the actual sequence of bytes in each Buffer.
5052
The <literal>targetStart</literal> is an integer specifying
5053
the offset within <value>target</value> at which to begin comparison,
5054
by default is 0.
5055
The <literal>targetEnd</literal> is an integer specifying
5056
the offset within <value>target</value> at which to end comparison,
5057
by default is <literal>target.length</literal>.
5058
The <literal>sourceStart</literal> is an integer specifying
5059
the offset within buffer at which to begin comparison,
5060
by default is 0.
5061
The <literal>sourceEnd</literal> is an integer specifying
5062
the offset within buffer at which to end comparison (not inclusive),
5063
by default is <literal>buf.length</literal>.
5064
</tag-desc>
5065
5066
<tag-name id="buf_copy"><literal>buf.copy(<value>target</value>[,
5067
<value>targetStart</value>[,
5068
<value>sourceStart</value>[,
5069
<value>sourceEnd</value>]]])</literal></tag-name>
5070
<tag-desc>
5071
Copies data from a region of buffer to a region in <value>target</value>,
5072
even if the target memory region overlaps with buffer.
5073
The <literal>target</literal> parameter is a
5074
<value>Buffer</value> or <value>Uint8Array</value> to copy into.
5075
5076
<para>
5077
The <literal>targetStart</literal> is an integer specifying
5078
the offset within target at which to begin writing,
5079
by default is 0.
5080
The <literal>sourceStart</literal> is an integer specifying
5081
the offset within buffer from which to begin copying,
5082
by default is 0.
5083
The <literal>sourceEnd</literal> is an integer specifying
5084
the offset within buffer at which to stop copying (not inclusive)
5085
by default is <value>buf.length</value>.
5086
</para>
5087
</tag-desc>
5088
5089
<tag-name id="buf_equals"><literal>buf.equals(<value>otherBuffer</value>)</literal></tag-name>
5090
<tag-desc>
5091
A boolean value,
5092
returns <literal>true</literal> if both Buffer and <value>otherBuffer</value>
5093
have exactly the same bytes.
5094
</tag-desc>
5095
5096
<tag-name id="buf_fill"><literal>buf.fill(<value>value</value>[,
5097
<value>offset</value>[,
5098
<value>end</value>]][,
5099
<value>encoding</value>])</literal></tag-name>
5100
<tag-desc>
5101
Fills the Buffer with the specified <value>value</value>.
5102
If the <value>offset</value> and <value>end</value> are not specified,
5103
the entire Buffer will be filled.
5104
The <value>value</value> is coerced to <value>uint32</value> if it is not a
5105
<literal>string</literal>,
5106
<literal>Buffer</literal>, or
5107
<literal>integer</literal>.
5108
If the resulting integer is greater than 255,
5109
the Buffer will be filled with <value>value</value> and 255.
5110
</tag-desc>
5111
5112
<tag-name id="buf_includes"><literal>buf.includes(<value>value</value>[,
5113
<value>byteOffset</value>][,
5114
<value>encoding</value>])</literal></tag-name>
5115
<tag-desc>
5116
Equivalent to
5117
<link id="buf_indexof"><literal>buf.indexOf()</literal></link>
5118
<literal>!== -1</literal>,
5119
returns <literal>true</literal> if the <value>value</value> was found
5120
in Buffer.
5121
</tag-desc>
5122
5123
<tag-name id="buf_indexof"><literal>buf.indexOf(<value>value</value>[,
5124
<value>byteOffset</value>][,
5125
<value>encoding</value>])</literal></tag-name>
5126
<tag-desc>
5127
Returns an integer which is the index of the first occurrence of
5128
<value>value</value> in Buffer, or <value>-1</value>
5129
if Buffer does not contain value.
5130
The <value>value</value> can be a
5131
<literal>string</literal> with specified <value>encoding</value>
5132
(by default <value>utf8</value>),
5133
<literal>Buffer</literal>,
5134
<literal>Unit8Array</literal>,
5135
or a number between 0 and 255.
5136
</tag-desc>
5137
5138
<tag-name id="buf_lastindexof"><literal>buf.lastIndexOf(<value>value</value>[,
5139
<value>byteOffset</value>][,
5140
<value>encoding</value>])</literal></tag-name>
5141
<tag-desc>
5142
The same as
5143
<link id="buf_indexof"><literal>buf.indexOf()</literal></link>,
5144
except the last occurrence of the <value>value</value> is found
5145
instead of the first occurrence.
5146
The <value>value</value> can be a string, Buffer, or
5147
integer between 1 and 255.
5148
If the <value>value</value> is an empty string or empty Buffer,
5149
<literal>byteOffset</literal> will be returned.
5150
</tag-desc>
5151
5152
<tag-name id="buf_length"><literal>buf.length</literal></tag-name>
5153
<tag-desc>
5154
Returns the number of bytes in Buffer.
5155
</tag-desc>
5156
5157
<tag-name id="buf_readintbe"><literal>buf.readIntBE(<value>offset</value>,
5158
<value>byteLength</value>)</literal></tag-name>
5159
<tag-desc>
5160
Reads the <value>byteLength</value> from <literal>buf</literal>
5161
at the specified <value>offset</value>
5162
and interprets the result as a big-endian,
5163
two's complement signed value supporting up to 48 bits of accuracy.
5164
The <value>byteLength</value> parameter is an integer between 1 and 6
5165
specifying the number of bytes to read.
5166
<para>
5167
The similar methods are also supported:
5168
<literal>buf.readInt8([offset])</literal>,
5169
<literal>buf.readInt16BE([offset])</literal>,
5170
<literal>buf.readInt32BE([offset])</literal>.
5171
</para>
5172
</tag-desc>
5173
5174
<tag-name id="buf_readintle"><literal>buf.readIntLE(<value>offset</value>,
5175
<value>byteLength</value>)</literal></tag-name>
5176
<tag-desc>
5177
Reads the <value>byteLength</value> from <literal>buf</literal>
5178
at the specified <value>offset</value>
5179
and interprets the result as a little-endian,
5180
two's complement signed value supporting up to 48 bits of accuracy.
5181
The <value>byteLength</value> parameter is an integer between 1 and 6
5182
specifying the number of bytes to read.
5183
<para>
5184
The similar methods are also supported:
5185
<literal>buf.readInt8([offset])</literal>,
5186
<literal>buf.readInt16LE([offset])</literal>,
5187
<literal>buf.readInt32LE([offset])</literal>.
5188
</para>
5189
</tag-desc>
5190
5191
<tag-name id="buf_readuintbe"><literal>buf.readUIntBE(<value>offset</value>,
5192
<value>byteLength</value>)</literal></tag-name>
5193
<tag-desc>
5194
Reads the <value>byteLength</value> from <literal>buf</literal>
5195
at the specified <value>offset</value>
5196
and interprets the result as a big-endian
5197
integer supporting up to 48 bits of accuracy.
5198
The <value>byteLength</value> parameter is an integer between 1 and 6
5199
specifying the number of bytes to read.
5200
<para>
5201
The similar methods are also supported:
5202
<literal>buf.readUInt8([offset])</literal>,
5203
<literal>buf.readUInt16BE([offset])</literal>,
5204
<literal>buf.readUInt32BE([offset])</literal>.
5205
</para>
5206
</tag-desc>
5207
5208
<tag-name id="buf_readuintle"><literal>buf.readUIntLE(<value>offset</value>,
5209
<value>byteLength</value>)</literal></tag-name>
5210
<tag-desc>
5211
Reads the <value>byteLength</value> from <literal>buf</literal>
5212
at the specified <value>offset</value>
5213
and interprets the result as a little-endian
5214
integer supporting up to 48 bits of accuracy.
5215
The <value>byteLength</value> parameter is an integer between 1 and 6
5216
specifying the number of bytes to read.
5217
<para>
5218
The similar methods are also supported:
5219
<literal>buf.readUInt8([offset])</literal>,
5220
<literal>buf.readUInt16LE([offset])</literal>,
5221
<literal>buf.readUInt32LE([offset])</literal>.
5222
</para>
5223
</tag-desc>
5224
5225
<tag-name id="buf_readdobulebe"><literal>buf.readDoubleBE</literal>([<value>offset</value>])</tag-name>
5226
<tag-desc>
5227
Reads a 64-bit, big-endian double from <literal>buf</literal>
5228
at the specified <value>offset</value>.
5229
</tag-desc>
5230
5231
<tag-name id="buf_readdobulele"><literal>buf.readDoubleLE</literal>([<value>offset</value>])</tag-name>
5232
<tag-desc>
5233
Reads a 64-bit, little-endian double from <literal>buf</literal>
5234
at the specified <value>offset</value>.
5235
</tag-desc>
5236
5237
<tag-name id="buf_readfloatbe"><literal>buf.readFloatBE</literal>([<value>offset</value>])</tag-name>
5238
<tag-desc>
5239
Reads a 32-bit, big-endian float from <literal>buf</literal>
5240
at the specified <value>offset</value>.
5241
</tag-desc>
5242
5243
<tag-name id="buf_readfloatle"><literal>buf.readFloatLE</literal>([<value>offset</value>])</tag-name>
5244
<tag-desc>
5245
Reads a 32-bit, little-endian float from <literal>buf</literal>
5246
at the specified <value>offset</value>.
5247
</tag-desc>
5248
5249
<tag-name id="buf_subarray"><literal>buf.subarray([<value>start</value>[,
5250
<value>end</value>]])</literal></tag-name>
5251
<tag-desc>
5252
Returns a new <literal>buf</literal>
5253
that references the same memory as the original,
5254
but offset and cropped by
5255
<value>start</value> and <value>end</value>.
5256
If <value>end</value> is greater than
5257
<link id="buf_length"><literal>buf.length</literal></link>,
5258
the same result as that of end equal to
5259
<link id="buf_length"><literal>buf.length</literal></link>
5260
is returned.
5261
</tag-desc>
5262
5263
<tag-name id="buf_slice"><literal>buf.slice([<value>start</value>[,
5264
<value>end</value>]])</literal></tag-name>
5265
<tag-desc>
5266
Returns a new <literal>buf</literal>
5267
that references the same memory as the original,
5268
but offset and cropped by the
5269
<value>start</value> and <value>end</value> values.
5270
The method is not compatible with the
5271
<literal>Uint8Array.prototype.slice()</literal>,
5272
which is a superclass of Buffer.
5273
To copy the slice, use
5274
<literal>Uint8Array.prototype.slice()</literal>.
5275
</tag-desc>
5276
5277
<tag-name id="buf_swap16"><literal>buf.swap16</literal>()</tag-name>
5278
<tag-desc>
5279
Interprets <literal>buf</literal> as an array of unsigned 16-bit numbers
5280
and swaps the byte order in-place.
5281
Throws an error if
5282
<link id="buf_length"><literal>buf.length</literal></link>
5283
is not a multiple of 2.
5284
</tag-desc>
5285
5286
<tag-name id="buf_swap32"><literal>buf.swap32</literal>()</tag-name>
5287
<tag-desc>
5288
Interprets <literal>buf</literal> as an array of unsigned 32-bit numbers
5289
and swaps the byte order in-place.
5290
Throws an error if
5291
<link id="buf_length"><literal>buf.length</literal></link>
5292
is not a multiple of 4.
5293
</tag-desc>
5294
5295
<tag-name id="buf_swap64"><literal>buf.swap64</literal>()</tag-name>
5296
<tag-desc>
5297
Interprets <literal>buf</literal> as an array of 64-bit numbers
5298
and swaps byte order in-place.
5299
Throws an error if
5300
<link id="buf_length"><literal>buf.length</literal></link>
5301
is not a multiple of 8.
5302
</tag-desc>
5303
5304
<tag-name id="buf_tojson"><literal>buf.toJSON</literal>()</tag-name>
5305
<tag-desc>
5306
Returns a JSON representation of <literal>buf.</literal>
5307
<literal>JSON.stringify()</literal>
5308
implicitly calls this function when stringifying a Buffer instance.
5309
</tag-desc>
5310
5311
<tag-name id="buf_tostring"><literal>buf.toString([<value>encoding</value>[,
5312
<value>start</value>[,
5313
<value>end</value>]]])</literal></tag-name>
5314
<tag-desc>
5315
Decodes <literal>buf</literal> to a string
5316
according to the specified character <value>encoding</value>
5317
which can be <value>utf8</value>,
5318
<value>hex</value>,
5319
<value>base64</value>,
5320
<value>base64url</value>.
5321
The <value>start</value> and <value>end</value> parameters
5322
may be passed to decode only a subset of Buffer.
5323
</tag-desc>
5324
5325
<tag-name id="buf_write"><literal>buf.write(<value>string</value>[,
5326
<value>offset</value>[,
5327
<value>length</value>]][,
5328
<value>encoding</value>])</literal></tag-name>
5329
<tag-desc>
5330
Writes a <value>string</value> to <literal>buf</literal>
5331
at <value>offset</value>
5332
according to the character <value>encoding</value>.
5333
The <value>length</value> parameter is the number of bytes to write.
5334
If Buffer did not contain enough space to fit the entire string,
5335
only part of string will be written,
5336
however, partially encoded characters will not be written.
5337
The <value>encoding</value> can be
5338
<value>utf8</value>,
5339
<value>hex</value>,
5340
<value>base64</value>,
5341
<value>base64url</value>.
5342
</tag-desc>
5343
5344
<tag-name id="buf_writeintbe"><literal>buf.writeIntBE(<value>value</value>,
5345
<value>offset</value>,
5346
<value>byteLength</value>)</literal></tag-name>
5347
<tag-desc>
5348
Writes <value>byteLength</value> bytes of <value>value</value>
5349
to <literal>buf</literal>
5350
at the specified <value>offset</value> as big-endian.
5351
Supports up to 48 bits of accuracy.
5352
The <value>byteLength</value> parameter is an integer between 1 and 6
5353
specifying the number of bytes to read.
5354
<para>
5355
The following similar methods are also supported:
5356
<literal>buf.writeInt8</literal>,
5357
<literal>buf.writeInt16BE</literal>,
5358
<literal>buf.writeInt32BE</literal>.
5359
</para>
5360
</tag-desc>
5361
5362
<tag-name id="buf_writeintle"><literal>buf.writeIntLE(<value>value</value>,
5363
<value>offset</value>,
5364
<value>byteLength</value>)</literal></tag-name>
5365
<tag-desc>
5366
Writes <value>byteLength</value> bytes of <value>value</value>
5367
to <literal>buf</literal>
5368
at the specified <value>offset</value> as little-endian.
5369
Supports up to 48 bits of accuracy.
5370
The <value>byteLength</value> parameter is an integer between 1 and 6
5371
specifying the number of bytes to read.
5372
<para>
5373
The following similar methods are also supported:
5374
<literal>buf.writeInt8</literal>,
5375
<literal>buf.writeInt16LE</literal>,
5376
<literal>buf.writeInt32LE</literal>.
5377
</para>
5378
</tag-desc>
5379
5380
<tag-name id="buf_writeuintbe"><literal>buf.writeUIntBE(<value>value</value>,
5381
<value>offset</value>,
5382
<value>byteLength</value>)</literal></tag-name>
5383
<tag-desc>
5384
Writes <value>byteLength</value> bytes of <value>value</value>
5385
to <literal>buf</literal>
5386
at the specified <value>offset</value> as big-endian.
5387
Supports up to 48 bits of accuracy.
5388
The <value>byteLength</value> parameter is an integer between 1 and 6
5389
specifying the number of bytes to read.
5390
<para>
5391
The following similar methods are also supported:
5392
<literal>buf.writeUInt8</literal>,
5393
<literal>buf.writeUInt16BE</literal>,
5394
<literal>buf.writeUInt32BE</literal>.
5395
</para>
5396
</tag-desc>
5397
5398
<tag-name id="buf_writeuintle"><literal>buf.writeUIntLE(<value>value</value>,
5399
<value>offset</value>,
5400
<value>byteLength</value>)</literal></tag-name>
5401
<tag-desc>
5402
Writes <value>byteLength</value> bytes of <value>value</value>
5403
to <literal>buf</literal>
5404
at the specified <value>offset</value> as little-endian.
5405
Supports up to 48 bits of accuracy.
5406
The <value>byteLength</value> parameter is an integer between 1 and 6
5407
specifying the number of bytes to read.
5408
<para>
5409
The following similar methods are also supported:
5410
<literal>buf.writeUInt8</literal>,
5411
<literal>buf.writeUInt16LE</literal>,
5412
<literal>buf.writeUInt32LE</literal>.
5413
</para>
5414
</tag-desc>
5415
5416
<tag-name id="buf_writedoublebe"><literal>buf.writeDoubleBE(<value>value</value>,
5417
[<value>offset</value>])</literal></tag-name>
5418
<tag-desc>
5419
Writes the <value>value</value> to <literal>buf</literal>
5420
at the specified <value>offset</value> as big-endian.
5421
</tag-desc>
5422
5423
<tag-name id="buf_writedoublele"><literal>buf.writeDoubleLE(<value>value</value>,
5424
[<value>offset</value>])</literal></tag-name>
5425
<tag-desc>
5426
Writes the <value>value</value> to <literal>buf</literal>
5427
at the specified <value>offset</value> as little-endian.
5428
</tag-desc>
5429
5430
<tag-name id="buf_writefloatbe"><literal>buf.writeFloatBE(<value>value</value>,
5431
[<value>offset</value>])</literal></tag-name>
5432
<tag-desc>
5433
Writes the <value>value</value> to <literal>buf</literal>
5434
at the specified <value>offset</value> as big-endian.
5435
</tag-desc>
5436
5437
<tag-name id="buf_writefloatle"><literal>buf.writeFloatLE(<value>value</value>,
5438
[<value>offset</value>])</literal></tag-name>
5439
<tag-desc>
5440
Writes the <value>value</value> to <literal>buf</literal>
5441
at the specified <value>offset</value> as little-endian.
5442
</tag-desc>
5443
5444
</list>
5445
</para>
5446
5447
</section>
5448
5449
5450
<section id="crypto" name="Crypto">
5451
5452
<para>
5453
<table width="100%">
5454
<tr><td><link id="crypto_createhash"><literal>crypto.createHash()</literal></link></td></tr>
5455
<tr><td><link id="crypto_createhmac"><literal>crypto.createHmac()</literal></link></td></tr>
5456
</table>
5457
</para>
5458
5459
<para>
5460
<note>
5461
Since <link doc="changes.xml" id="njs0.7.0">0.7.0</link>,
5462
extended crypto API is available as a global
5463
<link id="builtin_crypto">crypto</link> object.
5464
</note>
5465
The Crypto module provides cryptographic functionality support.
5466
The Crypto module object is imported using <literal>import crypto from 'crypto'</literal>.
5467
<note>
5468
Since <link doc="changes.xml" id="njs0.9.7">0.9.7</link>,
5469
the Crypto module requires OpenSSL
5470
and uses OpenSSL EVP for hashing,
5471
making any digest algorithm supported by the linked OpenSSL library
5472
available to JavaScript code.
5473
</note>
5474
</para>
5475
5476
<para>
5477
<list type="tag">
5478
5479
<tag-name id="crypto_createhash"><literal>crypto.createHash(<value>algorithm</value>)</literal></tag-name>
5480
<tag-desc>
5481
Creates and returns a <link id="crypto_hash">Hash</link> object
5482
that can be used to generate hash digests
5483
using the given <value>algorithm</value>.
5484
The algorithm can be
5485
<literal>md5</literal>,
5486
<literal>sha1</literal>,
5487
<literal>sha256</literal>,
5488
<literal>sha384</literal>,
5489
<literal>sha512</literal>,
5490
and any other digest supported by the linked OpenSSL library
5491
(since <link doc="changes.xml" id="njs0.9.7">0.9.7</link>).
5492
Before version <link doc="changes.xml" id="njs0.9.7">0.9.7</link>,
5493
only <literal>md5</literal>, <literal>sha1</literal>,
5494
and <literal>sha256</literal> were supported.
5495
</tag-desc>
5496
5497
<tag-name id="crypto_createhmac"><literal>crypto.createHmac(<value>algorithm</value>,
5498
<value>secret key</value>)</literal></tag-name>
5499
<tag-desc>
5500
Creates and returns an <link id="crypto_hmac">HMAC</link> object that uses
5501
the given <value>algorithm</value> and <value>secret key</value>.
5502
The algorithm can be
5503
<literal>md5</literal>,
5504
<literal>sha1</literal>,
5505
<literal>sha256</literal>,
5506
<literal>sha384</literal>,
5507
<literal>sha512</literal>,
5508
and any other digest supported by the linked OpenSSL library
5509
(since <link doc="changes.xml" id="njs0.9.7">0.9.7</link>).
5510
Before version <link doc="changes.xml" id="njs0.9.7">0.9.7</link>,
5511
only <literal>md5</literal>, <literal>sha1</literal>,
5512
and <literal>sha256</literal> were supported.
5513
</tag-desc>
5514
5515
</list>
5516
</para>
5517
5518
5519
<section id="crypto_hash" name="Hash">
5520
5521
<para>
5522
<table width="100%">
5523
<tr><td><link id="crypto_hash_update"><literal>hash.update()</literal></link></td></tr>
5524
<tr><td><link id="crypto_hash_digest"><literal>hash.digest()</literal></link></td></tr>
5525
</table>
5526
</para>
5527
5528
<para>
5529
<list type="tag">
5530
5531
<tag-name id="crypto_hash_update"><literal>hash.update(<value>data</value>)</literal></tag-name>
5532
<tag-desc>
5533
Updates the hash content with the given <value>data</value>.
5534
</tag-desc>
5535
5536
<tag-name id="crypto_hash_digest"><literal>hash.digest([<value>encoding</value>])</literal></tag-name>
5537
<tag-desc>
5538
Calculates the digest of all of the data passed using
5539
<literal>hash.update()</literal>.
5540
The encoding can be
5541
<literal>hex</literal>,
5542
<literal>base64</literal>, and
5543
<literal>base64url</literal>.
5544
If encoding is not provided, a Buffer object
5545
(<link doc="changes.xml" id="njs0.4.4">0.4.4</link>) is returned.
5546
<note>
5547
Before version (<link doc="changes.xml" id="njs0.4.4">0.4.4</link>),
5548
a byte string was returned instead of a Buffer object.
5549
</note>
5550
</tag-desc>
5551
5552
<tag-name id="crypto_hash_copy"><literal>hash.copy()</literal></tag-name>
5553
<tag-desc>
5554
Makes a copy of the current state of the hash
5555
(since <link doc="changes.xml" id="njs0.7.12">0.7.12</link>).
5556
</tag-desc>
5557
5558
</list>
5559
</para>
5560
5561
<para>
5562
<example>
5563
import crypto from 'crypto';
5564
5565
crypto.createHash('sha1').update('A').update('B').digest('base64url');
5566
/* BtlFlCqiamG-GMPiK_GbvKjdK10 */
5567
</example>
5568
</para>
5569
5570
</section>
5571
5572
5573
<section id="crypto_hmac" name="HMAC">
5574
5575
<para>
5576
<table width="100%">
5577
<tr><td><link id="crypto_hmac_update"><literal>hmac.update()</literal></link></td></tr>
5578
<tr><td><link id="crypto_hmac_digest"><literal>hmac.digest()</literal></link></td></tr>
5579
</table>
5580
</para>
5581
5582
<para>
5583
<list type="tag">
5584
5585
<tag-name id="crypto_hmac_update"><literal>hmac.update(<value>data</value>)</literal></tag-name>
5586
<tag-desc>
5587
Updates the HMAC content with the given <value>data</value>.
5588
</tag-desc>
5589
5590
<tag-name id="crypto_hmac_digest"><literal>hmac.digest([<value>encoding</value>])</literal></tag-name>
5591
<tag-desc>
5592
Calculates the HMAC digest of all of the data passed using
5593
<literal>hmac.update()</literal>.
5594
The encoding can be
5595
<literal>hex</literal>,
5596
<literal>base64</literal>, and
5597
<literal>base64url</literal>.
5598
If encoding is not provided, a Buffer object
5599
(<link doc="changes.xml" id="njs0.4.4">0.4.4</link>) is returned.
5600
<note>
5601
Before version <link doc="changes.xml" id="njs0.4.4">0.4.4</link>,
5602
a byte string was returned instead of a Buffer object.
5603
</note>
5604
</tag-desc>
5605
</list>
5606
</para>
5607
5608
<para>
5609
<example>
5610
import crypto from 'crypto';
5611
5612
crypto.createHmac('sha1', 'secret.key').update('AB').digest('base64url');
5613
/* Oglm93xn23_MkiaEq_e9u8zk374 */
5614
</example>
5615
</para>
5616
5617
</section>
5618
5619
</section>
5620
5621
5622
<section id="njs_api_fs" name="File System">
5623
5624
<para>
5625
<table width="100%">
5626
<tr><td><link id="fs_accesssync"><literal>fs.accessSync()</literal></link></td></tr>
5627
<tr><td><link id="fs_appendfilesync"><literal>fs.appendFileSync()</literal></link></td></tr>
5628
<tr><td><link id="fs_closesync"><literal>fs.closeSync()</literal></link></td></tr>
5629
<tr><td><link id="fs_existssync"><literal>fs.existsSync()</literal></link></td></tr>
5630
<tr><td><link id="fs_fstatsync"><literal>fs.fstatSync()</literal></link></td></tr>
5631
<tr><td><link id="fs_lstatsync"><literal>fs.lstatSync()</literal></link></td></tr>
5632
<tr><td><link id="fs_mkdirsync"><literal>fs.mkdirSync()</literal></link></td></tr>
5633
<tr><td><link id="fs_opensync"><literal>fs.openSync()</literal></link></td></tr>
5634
<tr><td><link id="fs_promises_open"><literal>fs.promises.open()</literal></link></td></tr>
5635
<tr><td><link id="fs_readdirsync"><literal>fs.readdirSync()</literal></link></td></tr>
5636
<tr><td><link id="fs_readfilesync"><literal>fs.readFileSync()</literal></link></td></tr>
5637
<tr><td><link id="fs_readlinksync"><literal>fs.readlinkSync()</literal></link></td></tr>
5638
<tr><td><link id="fs_readsync"><literal>fs.readSync()</literal></link></td></tr>
5639
<tr><td><link id="fs_realpathsync"><literal>fs.realpathSync()</literal></link></td></tr>
5640
<tr><td><link id="fs_renamesync"><literal>fs.renameSync()</literal></link></td></tr>
5641
<tr><td><link id="fs_rmdirsync"><literal>fs.rmdirSync()</literal></link></td></tr>
5642
<tr><td><link id="fs_statsync"><literal>fs.statSync()</literal></link></td></tr>
5643
<tr><td><link id="fs_symlinksync"><literal>fs.symlinkSync()</literal></link></td></tr>
5644
<tr><td><link id="fs_unlinksync"><literal>fs.unlinkSync()</literal></link></td></tr>
5645
<tr><td><link id="fs_writefilesync"><literal>fs.writeFileSync()</literal></link></td></tr>
5646
<tr><td><link id="fs_writesync_buf"><literal>fs.writeSync()</literal></link></td></tr>
5647
<tr><td><link id="fs_writesync_str"><literal>fs.writeSync()</literal></link></td></tr>
5648
</table>
5649
</para>
5650
5651
<para>
5652
<table width="100%">
5653
<tr><td><link id="fs_dirent"><literal>fs.Dirent</literal></link></td></tr>
5654
<tr><td><link id="fs_filehandle"><literal>fs.FileHandle</literal></link></td></tr>
5655
<tr><td><link id="fs_stats"><literal>fs.Stats</literal></link></td></tr>
5656
<tr><td><link id="access_const"><literal>File Access Constants</literal></link></td></tr>
5657
<tr><td><link id="njs_api_fs_flags"><literal>File System Flags</literal></link></td></tr>
5658
</table>
5659
</para>
5660
5661
<para>
5662
The File System module provides operations with files.
5663
</para>
5664
5665
<para>
5666
The module object is imported using <literal>import fs from 'fs'</literal>.
5667
Since <link doc="changes.xml" id="njs0.3.9">0.3.9</link>,
5668
promissified versions of file system methods are available through
5669
<literal>fs.promises</literal> object after importing with <literal>import fs from 'fs'</literal>:
5670
<example>
5671
import fs from 'fs';
5672
5673
fs.promises.readFile("/file/path").then((data) => {
5674
/* &lt;file data&gt; */
5675
});
5676
</example>
5677
<list type="tag">
5678
5679
<tag-name id="fs_accesssync"><literal>accessSync(<value>path</value>[,
5680
<value>mode</value>])</literal></tag-name>
5681
<tag-desc>
5682
Synchronously tests permissions for a file or directory
5683
specified in the <literal>path</literal>
5684
(<link doc="changes.xml" id="njs0.3.9">0.3.9</link>).
5685
If the check fails, an error will be returned,
5686
otherwise, the method will return undefined.
5687
<list type="tag">
5688
5689
<tag-name><literal>mode</literal></tag-name>
5690
<tag-desc>
5691
an optional integer
5692
that specifies the accessibility checks to be performed,
5693
by default is <link id="access_const"><literal>fs.constants.F_OK</literal></link>
5694
<example>
5695
try {
5696
fs.accessSync('/file/path', fs.constants.R_OK | fs.constants.W_OK);
5697
console.log('has access');
5698
} catch (e) {
5699
console.log('no access');)
5700
}
5701
</example>
5702
</tag-desc>
5703
5704
</list>
5705
</tag-desc>
5706
5707
<tag-name id="fs_appendfilesync"><literal>appendFileSync(<value>filename</value>,
5708
<value>data</value>[, <value>options</value>])</literal></tag-name>
5709
<tag-desc>
5710
Synchronously appends specified <literal>data</literal>
5711
to a file with provided <literal>filename</literal>.
5712
The <literal>data</literal> is expected to be a string
5713
or a Buffer object (<link doc="changes.xml" id="njs0.4.4">0.4.4</link>).
5714
If the file does not exist, it will be created.
5715
The <literal>options</literal> parameter is expected to be
5716
an object with the following keys:
5717
<list type="tag">
5718
5719
<tag-name><literal>mode</literal></tag-name>
5720
<tag-desc>
5721
mode option, by default is <literal>0o666</literal>
5722
</tag-desc>
5723
5724
<tag-name><literal>flag</literal></tag-name>
5725
<tag-desc>
5726
file system <link id="njs_api_fs_flags">flag</link>,
5727
by default is <literal>a</literal>
5728
</tag-desc>
5729
5730
</list>
5731
</tag-desc>
5732
5733
<tag-name id="fs_closesync"><literal>closeSync(<value>fd</value>)</literal></tag-name>
5734
<tag-desc>
5735
Closes the <literal>fd</literal> file descriptor represented by an integer
5736
used by the method.
5737
Returns <literal>undefined</literal>.
5738
</tag-desc>
5739
5740
<tag-name id="fs_existssync"><literal>existsSync(<value>path</value>)</literal></tag-name>
5741
<tag-desc>
5742
Boolean value, returns
5743
<literal>true</literal> if the specified <value>path</value> exists.
5744
(<link doc="changes.xml" id="njs0.8.2">0.8.2</link>)
5745
</tag-desc>
5746
5747
<tag-name id="fs_fstatsync"><literal>fstatSync(<value>fd</value>)</literal></tag-name>
5748
<tag-desc>
5749
Retrieves the <link id="fs_stats"><literal>fs.Stats</literal></link> object
5750
for the file descriptor
5751
(<link doc="changes.xml" id="njs0.7.7">0.7.7</link>).
5752
The <literal>fd</literal> parameter is an integer
5753
representing the file descriptor used by the method.
5754
</tag-desc>
5755
5756
<tag-name id="fs_lstatsync"><literal>lstatSync(<value>path</value>[,
5757
<value>options</value>])</literal></tag-name>
5758
<tag-desc>
5759
Synchronously retrieves
5760
the <link id="fs_stats"><literal>fs.Stats</literal></link> object
5761
for the symbolic link referred to by <literal>path</literal>
5762
(<link doc="changes.xml" id="njs0.7.1">0.7.1</link>).
5763
The <literal>options</literal> parameter is expected to be
5764
an object with the following keys:
5765
<list type="tag">
5766
<tag-name><literal>throwIfNoEntry</literal></tag-name>
5767
<tag-desc>
5768
a boolean value which indicates
5769
whether an exception is thrown if no file system entry exists,
5770
rather than returning <literal>undefined</literal>,
5771
by default is <literal>false</literal>.
5772
</tag-desc>
5773
</list>
5774
</tag-desc>
5775
5776
<tag-name id="fs_mkdirsync"><literal>mkdirSync(<value>path</value>[,
5777
<value>options</value>])</literal></tag-name>
5778
<tag-desc>
5779
Synchronously creates a directory at the specified <literal>path</literal>
5780
(<link doc="changes.xml" id="njs0.4.2">0.4.2</link>).
5781
The <literal>options</literal> parameter is expected to be an
5782
<literal>integer</literal> that specifies
5783
the <link id="fs_mkdirsync_mode">mode</link>,
5784
or an object with the following keys:
5785
<list type="tag">
5786
5787
<tag-name id="fs_mkdirsync_mode"><literal>mode</literal></tag-name>
5788
<tag-desc>
5789
mode option, by default is <literal>0o777</literal>.
5790
</tag-desc>
5791
5792
</list>
5793
</tag-desc>
5794
5795
<tag-name id="fs_opensync"><literal>openSync(<value>path</value>[,
5796
<value>flags</value>[, <value>mode</value>]])</literal></tag-name>
5797
<tag-desc>
5798
Returns an integer
5799
representing the file descriptor for the opened file <literal>path</literal>
5800
(<link doc="changes.xml" id="njs0.7.7">0.7.7</link>).
5801
<list type="tag">
5802
5803
<tag-name><literal>flags</literal></tag-name>
5804
<tag-desc>
5805
file system <link id="njs_api_fs_flags">flag</link>,
5806
by default is <literal>r</literal>
5807
</tag-desc>
5808
5809
<tag-name><literal>mode</literal></tag-name>
5810
<tag-desc>
5811
mode option, by default is <literal>0o666</literal>
5812
</tag-desc>
5813
5814
</list>
5815
</tag-desc>
5816
5817
<tag-name id="fs_promises_open"><literal>promises.open(<value>path</value>[,
5818
<value>flags</value>[, <value>mode</value>]])</literal></tag-name>
5819
<tag-desc>
5820
Returns a <link id="fs_filehandle"><literal>FileHandle</literal></link> object
5821
representing the opened file <literal>path</literal>
5822
(<link doc="changes.xml" id="njs0.7.7">0.7.7</link>).
5823
<list type="tag">
5824
5825
<tag-name><literal>flags</literal></tag-name>
5826
<tag-desc>
5827
file system <link id="njs_api_fs_flags">flag</link>,
5828
by default is <literal>r</literal>
5829
</tag-desc>
5830
5831
<tag-name><literal>mode</literal></tag-name>
5832
<tag-desc>
5833
mode option, by default is <literal>0o666</literal>
5834
</tag-desc>
5835
5836
</list>
5837
</tag-desc>
5838
5839
<tag-name id="fs_readdirsync"><literal>readdirSync(<value>path</value>[,
5840
<value>options</value>])</literal></tag-name>
5841
<tag-desc>
5842
Synchronously reads the contents of a directory
5843
at the specified <literal>path</literal>
5844
(<link doc="changes.xml" id="njs0.4.2">0.4.2</link>).
5845
The <literal>options</literal> parameter is expected to be
5846
a string that specifies <link id="fs_readdirsync_encoding">encoding</link>
5847
or an object with the following keys:
5848
<list type="tag">
5849
5850
<tag-name id="fs_readdirsync_encoding"><literal>encoding</literal></tag-name>
5851
<tag-desc>
5852
encoding, by default is <literal>utf8</literal>.
5853
The encoding can be <literal>utf8</literal> and <literal>buffer</literal>
5854
(<link doc="changes.xml" id="njs0.4.4">0.4.4</link>).
5855
</tag-desc>
5856
5857
<tag-name id="fs_readdirsync_withfiletypes"><literal>withFileTypes</literal></tag-name>
5858
<tag-desc>
5859
if set to <literal>true</literal>, the files array will contain
5860
<link id="fs_dirent"><literal>fs.Dirent</literal></link> objects,
5861
by default is <literal>false</literal>.
5862
</tag-desc>
5863
5864
</list>
5865
</tag-desc>
5866
5867
<tag-name id="fs_readfilesync"><literal>readFileSync(<value>filename</value>[,
5868
<value>options</value>])</literal></tag-name>
5869
<tag-desc>
5870
Synchronously returns the contents of the file
5871
with provided <literal>filename</literal>.
5872
The <literal>options</literal> parameter holds
5873
<literal>string</literal> that specifies encoding.
5874
If an encoding is specified, a string is returned,
5875
otherwise, a Buffer object
5876
(<link doc="changes.xml" id="njs0.4.4">0.4.4</link>) is returned.
5877
<note>
5878
Before version <link doc="changes.xml" id="njs0.4.4">0.4.4</link>,
5879
a <link id="string_tobytes">byte string</link> was returned
5880
if encoding was not specified.
5881
</note>
5882
Otherwise, <literal>options</literal> is expected to be
5883
an object with the following keys:
5884
<list type="tag">
5885
5886
<tag-name><literal>encoding</literal></tag-name>
5887
<tag-desc>
5888
encoding, by default is not specified.
5889
The encoding can be <literal>utf8</literal>,
5890
<literal>hex</literal>
5891
(<link doc="changes.xml" id="njs0.4.4">0.4.4</link>),
5892
<literal>base64</literal>
5893
(<link doc="changes.xml" id="njs0.4.4">0.4.4</link>),
5894
<literal>base64url</literal>
5895
(<link doc="changes.xml" id="njs0.4.4">0.4.4</link>).
5896
</tag-desc>
5897
5898
<tag-name><literal>flag</literal></tag-name>
5899
<tag-desc>
5900
file system <link id="njs_api_fs_flags">flag</link>,
5901
by default is <literal>r</literal>
5902
</tag-desc>
5903
5904
</list>
5905
<example>
5906
import fs from 'fs';
5907
5908
var file = fs.readFileSync('/file/path.tar.gz');
5909
console.log(file.slice(0,2).toString('hex')) /* '1f8b' */
5910
</example>
5911
</tag-desc>
5912
5913
<tag-name id="fs_readlinksync"><literal>readlinkSync(<value>path</value>[,
5914
<value>options</value>])</literal></tag-name>
5915
<tag-desc>
5916
Synchronously gets the contents of the symbolic link <literal>path</literal>
5917
using
5918
<link url="https://man7.org/linux/man-pages/man2/readlink.2.html">readlink(2)</link>
5919
(<link doc="changes.xml" id="njs0.8.7">0.8.7</link>).
5920
The <literal>options</literal> argument can be a string specifying an encoding,
5921
or an object with <literal>encoding</literal> property
5922
specifying the character encoding to use.
5923
If the <literal>encoding</literal> is <value>buffer</value>,
5924
the result is returned as a <literal>Buffer</literal> object,
5925
otherwise as a string.
5926
</tag-desc>
5927
5928
<tag-name id="fs_readsync"><literal>readSync(<value>fd</value>,
5929
<value>buffer</value>, <value>offset</value>[,
5930
<value>length</value>[, <value>position</value>]])</literal></tag-name>
5931
<tag-desc>
5932
Reads the content of a file path using file descriptor <literal>fd</literal>,
5933
returns the number of bytes read
5934
(<link doc="changes.xml" id="njs0.7.7">0.7.7</link>).
5935
5936
<list type="tag">
5937
5938
<tag-name><literal>buffer</literal></tag-name>
5939
<tag-desc>
5940
the <literal>buffer</literal> value can be a
5941
<literal>Buffer</literal>,
5942
<literal>TypedArray</literal>, or
5943
<literal>DataView</literal>
5944
</tag-desc>
5945
5946
<tag-name><literal>offset</literal></tag-name>
5947
<tag-desc>
5948
is an <literal>integer</literal> representing
5949
the position in buffer to write the data to
5950
</tag-desc>
5951
5952
<tag-name><literal>length</literal></tag-name>
5953
<tag-desc>
5954
is an <literal>integer</literal> representing
5955
the number of bytes to read
5956
</tag-desc>
5957
5958
<tag-name><literal>position</literal></tag-name>
5959
<tag-desc>
5960
specifies where to begin reading from in the file,
5961
the value can be
5962
<literal>integer</literal> or
5963
<literal>null</literal>,
5964
by default is <literal>null</literal>.
5965
If <literal>position</literal> is <literal>null</literal>,
5966
data will be read from the current file position,
5967
and the file position will be updated.
5968
If position is an <literal>integer</literal>,
5969
the file position will be unchanged
5970
</tag-desc>
5971
</list>
5972
5973
</tag-desc>
5974
5975
<tag-name id="fs_realpathsync"><literal>realpathSync(<value>path</value>[,
5976
<value>options</value>])</literal></tag-name>
5977
<tag-desc>
5978
Synchronously computes the canonical pathname by resolving
5979
<literal>.</literal>, <literal>..</literal> and symbolic links using
5980
<link url="http://man7.org/linux/man-pages/man3/realpath.3.html">realpath(3)</link>.
5981
The <literal>options</literal> argument can be a string specifying an encoding,
5982
or an object with an encoding property specifying the character encoding
5983
to use for the path passed to the callback
5984
(<link doc="changes.xml" id="njs0.3.9">0.3.9</link>).
5985
</tag-desc>
5986
5987
<tag-name id="fs_renamesync"><literal>renameSync(<value>oldPath</value>,
5988
<value>newPath</value>)</literal></tag-name>
5989
<tag-desc>
5990
Synchronously changes the name or location of a file from
5991
<literal>oldPath</literal> to <literal>newPath</literal>
5992
(<link doc="changes.xml" id="njs0.3.4">0.3.4</link>).
5993
<example>
5994
import fs from 'fs';
5995
5996
fs.renameSync('hello.txt', 'HelloWorld.txt');
5997
</example>
5998
</tag-desc>
5999
6000
<tag-name id="fs_rmdirsync"><literal>rmdirSync(<value>path</value>)</literal></tag-name>
6001
<tag-desc>
6002
Synchronously removes a directory at the specified <literal>path</literal>
6003
(<link doc="changes.xml" id="njs0.4.2">0.4.2</link>).
6004
</tag-desc>
6005
6006
<tag-name id="fs_statsync"><literal>statSync(<value>path</value>,[
6007
<value>options</value>])</literal></tag-name>
6008
<tag-desc>
6009
Synchronously retrieves
6010
the <link id="fs_stats"><literal>fs.Stats</literal></link> object
6011
for the specified <literal>path</literal>
6012
(<link doc="changes.xml" id="njs0.7.1">0.7.1</link>).
6013
The <literal>path</literal> can be a
6014
<literal>string</literal> or
6015
<literal>buffer</literal>.
6016
The <literal>options</literal> parameter is expected to be
6017
an object with the following keys:
6018
<list type="tag">
6019
<tag-name><literal>throwIfNoEntry</literal></tag-name>
6020
<tag-desc>
6021
a boolean value which indicates whether
6022
an exception is thrown if no file system entry exists
6023
rather than returning <literal>undefined</literal>,
6024
by default is <literal>true</literal>.
6025
</tag-desc>
6026
</list>
6027
</tag-desc>
6028
6029
<tag-name id="fs_symlinksync"><literal>symlinkSync(<value>target</value>,
6030
<value>path</value>)</literal></tag-name>
6031
<tag-desc>
6032
Synchronously creates the link called <literal>path</literal>
6033
pointing to <literal>target</literal> using
6034
<link url="http://man7.org/linux/man-pages/man2/symlink.2.html">symlink(2)</link>
6035
(<link doc="changes.xml" id="njs0.3.9">0.3.9</link>).
6036
Relative targets are relative to the link’s parent directory.
6037
</tag-desc>
6038
6039
<tag-name id="fs_unlinksync"><literal>unlinkSync(<value>path</value>)</literal></tag-name>
6040
<tag-desc>
6041
Synchronously unlinks a file by <literal>path</literal>
6042
(<link doc="changes.xml" id="njs0.3.9">0.3.9</link>).
6043
</tag-desc>
6044
6045
<tag-name id="fs_writefilesync"><literal>writeFileSync(<value>filename</value>,
6046
<value>data</value>[,
6047
<value>options</value>])</literal></tag-name>
6048
<tag-desc>
6049
Synchronously writes <literal>data</literal> to a file
6050
with provided <literal>filename</literal>.
6051
The <literal>data</literal> is expected to be a string
6052
or a Buffer object (<link doc="changes.xml" id="njs0.4.4">0.4.4</link>).
6053
If the file does not exist, it will be created,
6054
if the file exists, it will be replaced.
6055
The <literal>options</literal> parameter is expected to be
6056
an object with the following keys:
6057
<list type="tag">
6058
<tag-name><literal>mode</literal></tag-name>
6059
<tag-desc>
6060
mode option, by default is <literal>0o666</literal>
6061
</tag-desc>
6062
6063
<tag-name><literal>flag</literal></tag-name>
6064
<tag-desc>
6065
file system <link id="njs_api_fs_flags">flag</link>,
6066
by default is <literal>w</literal>
6067
</tag-desc>
6068
6069
</list>
6070
<example>
6071
import fs from 'fs';
6072
6073
fs.writeFileSync('hello.txt', 'Hello world');
6074
</example>
6075
</tag-desc>
6076
6077
<tag-name id="fs_writesync_buf"><literal>writeSync(<value>fd</value>,
6078
<value>buffer</value>, <value>offset</value>[,
6079
<value>length</value>[, <value>position</value>]])</literal></tag-name>
6080
<tag-desc>
6081
Writes a buffer to a file using file descriptor,
6082
returns the <literal>number</literal> of bytes written
6083
(<link doc="changes.xml" id="njs0.7.7">0.7.7</link>).
6084
6085
<list type="tag">
6086
6087
<tag-name><literal>fd</literal></tag-name>
6088
<tag-desc>
6089
an <literal>integer</literal> representing the file descriptor
6090
</tag-desc>
6091
6092
<tag-name><literal>buffer</literal></tag-name>
6093
<tag-desc>
6094
the <literal>buffer</literal> value can be a
6095
<literal>Buffer</literal>,
6096
<literal>TypedArray</literal>, or
6097
<literal>DataView</literal>
6098
</tag-desc>
6099
6100
<tag-name><literal>offset</literal></tag-name>
6101
<tag-desc>
6102
is an <literal>integer</literal> that determines
6103
the part of the buffer to be written,
6104
by default <literal>0</literal>
6105
</tag-desc>
6106
6107
<tag-name><literal>length</literal></tag-name>
6108
<tag-desc>
6109
is an <literal>integer</literal> specifying the number of bytes to write,
6110
by default is an offset of
6111
<link id="buffer_bytelength">Buffer.byteLength</link>
6112
</tag-desc>
6113
6114
<tag-name><literal>position</literal></tag-name>
6115
<tag-desc>
6116
refers to the offset from the beginning of the file
6117
where this data should be written,
6118
can be an
6119
<literal>integer</literal> or
6120
<literal>null</literal>,
6121
by default is <literal>null</literal>.
6122
See also
6123
<link url="https://man7.org/linux/man-pages/man2/write.2.html">pwrite(2)</link>.
6124
</tag-desc>
6125
</list>
6126
6127
</tag-desc>
6128
6129
<tag-name id="fs_writesync_str"><literal>writeSync(<value>fd</value>,
6130
<value>string</value>[,
6131
<value>position</value>[,
6132
<value>encoding</value>]])</literal></tag-name>
6133
<tag-desc>
6134
Writes a <literal>string</literal> to a file
6135
using file descriptor <literal>fd</literal>,
6136
returns the <literal>number</literal> of bytes written
6137
(<link doc="changes.xml" id="njs0.7.7">0.7.7</link>).
6138
6139
<list type="tag">
6140
6141
<tag-name><literal>fd</literal></tag-name>
6142
<tag-desc>
6143
is an <literal>integer</literal> representing a file descriptor
6144
</tag-desc>
6145
6146
<tag-name><literal>position</literal></tag-name>
6147
<tag-desc>
6148
refers to the offset from the beginning of the file
6149
where this data should be written,
6150
can be an
6151
<literal>integer</literal> or
6152
<literal>null</literal>, by default is <literal>null</literal>.
6153
See also
6154
<link url="https://man7.org/linux/man-pages/man2/write.2.html">pwrite(2)</link>
6155
</tag-desc>
6156
6157
<tag-name><literal>encoding</literal></tag-name>
6158
<tag-desc>
6159
is a <literal>string</literal>,
6160
by default is <literal>utf8</literal>
6161
</tag-desc>
6162
</list>
6163
6164
</tag-desc>
6165
6166
</list>
6167
</para>
6168
6169
6170
<section id="fs_dirent" name="fs.Dirent">
6171
6172
<para>
6173
<literal>fs.Dirent</literal> is a representation of a directory entry&mdash;
6174
a file or a subdirectory.
6175
When
6176
<link id="fs_readdirsync"><literal>readdirSync()</literal></link>
6177
is called with the
6178
<link id="fs_readdirsync_withfiletypes"><literal>withFileTypes</literal></link>
6179
option,
6180
the resulting array contains <literal>fs.Dirent</literal> objects.
6181
6182
<list type= "bullet" compact="no">
6183
6184
<listitem>
6185
<literal>dirent.isBlockDevice()</literal>&mdash;returns
6186
<literal>true</literal> if the <literal>fs.Dirent</literal> object describes
6187
a block device.
6188
</listitem>
6189
6190
<listitem>
6191
<literal>dirent.isCharacterDevice()</literal>&mdash;returns
6192
<literal>true</literal> if the <literal>fs.Dirent</literal> object describes
6193
a character device.
6194
</listitem>
6195
6196
<listitem>
6197
<literal>dirent.isDirectory()</literal>&mdash;returns
6198
<literal>true</literal> if the <literal>fs.Dirent</literal> object describes
6199
a file system directory.
6200
</listitem>
6201
6202
<listitem>
6203
<literal>dirent.isFIFO()</literal>&mdash;returns
6204
<literal>true</literal> if the <literal>fs.Dirent</literal> object describes
6205
a first-in-first-out (FIFO) pipe.
6206
</listitem>
6207
6208
<listitem>
6209
<literal>dirent.isFile()</literal>&mdash;returns
6210
<literal>true</literal> if the <literal>fs.Dirent</literal> object describes
6211
a regular file.
6212
</listitem>
6213
6214
<listitem>
6215
<literal>dirent.isSocket()</literal>&mdash;returns
6216
<literal>true</literal> if the <literal>fs.Dirent</literal> object describes
6217
a socket.
6218
</listitem>
6219
6220
<listitem>
6221
<literal>dirent.isSymbolicLink()</literal>&mdash;returns
6222
<literal>true</literal> if the <literal>fs.Dirent</literal> object describes
6223
a symbolic link.
6224
</listitem>
6225
6226
<listitem>
6227
<literal>dirent.name</literal>&mdash;
6228
the name of the file <literal>fs.Dirent</literal> object refers to.
6229
</listitem>
6230
6231
</list>
6232
</para>
6233
6234
</section>
6235
6236
6237
<section id="fs_filehandle" name="fs.FileHandle">
6238
6239
<para>
6240
<table width="100%">
6241
<tr><td><link id="filehandle_close"><literal>filehandle.close()</literal></link></td></tr>
6242
<tr><td><link id="filehandle_fd"><literal>filehandle.fd</literal></link></td></tr>
6243
<tr><td><link id="filehandle_read"><literal>filehandle.read()</literal></link></td></tr>
6244
<tr><td><link id="filehandle_stat"><literal>filehandle.stat()</literal></link></td></tr>
6245
<tr><td><link id="filehandle_write_buf"><literal>filehandle.write(<value>buf</value>)</literal></link></td></tr>
6246
<tr><td><link id="filehandle_write_str"><literal>filehandle.write(<value>str</value>)</literal></link></td></tr>
6247
</table>
6248
</para>
6249
6250
<para>
6251
The <literal>FileHandle</literal> object is an object wrapper
6252
for a numeric file descriptor
6253
(<link doc="changes.xml" id="njs0.7.7">0.7.7</link>).
6254
Instances of the <literal>FileHandle</literal> object are created by the
6255
<link id="fs_promises_open"><literal>fs.promises.open()</literal></link> method.
6256
If a <literal>FileHandle</literal> is not closed using the
6257
<link id="filehandle_close"><literal>filehandle.close()</literal></link> method,
6258
it will try to automatically close the file descriptor,
6259
helping to prevent memory leaks.
6260
Please do not rely on this behavior because it can be unreliable.
6261
Instead, always explicitly close a <literal>FileHandle</literal>.
6262
</para>
6263
6264
<para>
6265
<list type="tag">
6266
6267
<tag-name id="filehandle_close"><literal>filehandle.close()</literal></tag-name>
6268
<tag-desc>
6269
Closes the file handle after waiting for any pending operation on the handle
6270
to complete.
6271
Returns a <literal>promise</literal>, fulfills with undefined upon success.
6272
</tag-desc>
6273
6274
<tag-name id="filehandle_fd"><literal>filehandle.fd</literal></tag-name>
6275
<tag-desc>
6276
The numeric file descriptor
6277
managed by the <literal>FileHandle</literal> object.
6278
</tag-desc>
6279
6280
<tag-name id="filehandle_read"><literal>filehandle.read(<value>buffer</value>,
6281
<value>offset</value>[,
6282
<value>length</value>[,
6283
<value>position</value>]])</literal></tag-name>
6284
<tag-desc>
6285
Reads data from the file and stores that in the given buffer.
6286
6287
<list type="tag">
6288
6289
<tag-name><literal>buffer</literal></tag-name>
6290
<tag-desc>
6291
a buffer that will be filled with the file data read,
6292
the value can be a
6293
<literal>Buffer</literal>,
6294
<literal>TypedArray</literal>, or
6295
<literal>DataView</literal>
6296
</tag-desc>
6297
6298
<tag-name><literal>offset</literal></tag-name>
6299
<tag-desc>
6300
is an <literal>integer</literal>
6301
representing the location in the buffer at which to start filling
6302
</tag-desc>
6303
6304
<tag-name><literal>length</literal></tag-name>
6305
<tag-desc>
6306
is an <literal>integer</literal>
6307
representing the number of bytes to read
6308
</tag-desc>
6309
6310
<tag-name><literal>position</literal></tag-name>
6311
<tag-desc>
6312
the location where to begin reading data from the file,
6313
the value can be
6314
<literal>integer</literal>,
6315
<literal>null</literal>.
6316
If <literal>null</literal>, data will be read from the current file position
6317
and the position will be updated.
6318
If position is an <literal>integer</literal>,
6319
the current file position will remain unchanged.
6320
</tag-desc>
6321
</list>
6322
6323
Returns a <literal>Promise</literal> which fulfills upon success
6324
with an object with two properties:
6325
<list type="tag">
6326
6327
<tag-name><literal>bytesRead</literal></tag-name>
6328
<tag-desc>
6329
is an <literal>integer</literal> representing the number of bytes read
6330
</tag-desc>
6331
6332
<tag-name><literal>buffer</literal></tag-name>
6333
<tag-desc>
6334
is a reference to the passed argument in buffer, can be
6335
<literal>Buffer</literal>,
6336
<literal>TypedArray</literal>, or
6337
<literal>DataView</literal>
6338
</tag-desc>
6339
</list>
6340
6341
</tag-desc>
6342
6343
<tag-name id="filehandle_stat"><literal>filehandle.stat()</literal></tag-name>
6344
<tag-desc>
6345
Fulfills with an
6346
<link id="fs_stats">fs.Stats</link> for the file,
6347
returns a <literal>promise</literal>.
6348
</tag-desc>
6349
6350
<tag-name id="filehandle_write_buf"><literal>filehandle.write(<value>buffer</value>,
6351
<value>offset</value>[,
6352
<value>length</value>[,
6353
<value>position</value>]])</literal></tag-name>
6354
<tag-desc>
6355
Writes a buffer to the file.
6356
6357
<list type="tag">
6358
6359
<tag-name><literal>buffer</literal></tag-name>
6360
<tag-desc>
6361
the <literal>buffer</literal> value can be a
6362
<literal>Buffer</literal>,
6363
<literal>TypedArray</literal>, or
6364
<literal>DataView</literal>
6365
</tag-desc>
6366
6367
<tag-name><literal>offset</literal></tag-name>
6368
<tag-desc>
6369
is an <literal>integer</literal> representing
6370
the start position from within buffer where the data to write begins
6371
</tag-desc>
6372
6373
<tag-name><literal>length</literal></tag-name>
6374
<tag-desc>
6375
is an <literal>integer</literal> representing
6376
the number of bytes from buffer to write, by default
6377
is an offset of
6378
<link id="buffer_bytelength">Buffer.byteLength</link>
6379
</tag-desc>
6380
6381
<tag-name><literal>position</literal></tag-name>
6382
<tag-desc>
6383
the offset from the beginning of the file
6384
where the data from buffer should be written,
6385
can be an
6386
<literal>integer</literal> or
6387
<literal>null</literal>,
6388
by default is <literal>null</literal>.
6389
If <literal>position</literal> is not a <literal>number</literal>,
6390
the data will be written at the current position.
6391
See the POSIX
6392
<link url="https://man7.org/linux/man-pages/man2/write.2.html">pwrite(2)</link>
6393
documentation for details.
6394
</tag-desc>
6395
</list>
6396
Returns a <literal>Promise</literal> which is resolved with an object
6397
containing two properties:
6398
<list type="tag">
6399
6400
<tag-name><literal>bytesWritten</literal></tag-name>
6401
<tag-desc>
6402
is an <literal>integer</literal> representing the number of bytes written
6403
</tag-desc>
6404
6405
<tag-name><literal>buffer</literal></tag-name>
6406
<tag-desc>
6407
a reference to the buffer written, can be a
6408
<literal>Buffer</literal>,
6409
<literal>TypedArray</literal>, or
6410
<literal>DataView</literal>
6411
</tag-desc>
6412
</list>
6413
<para>
6414
<note>
6415
It is unsafe to use <literal>filehandle.write()</literal> multiple times
6416
on the same file without waiting for the promise to be resolved or rejected.
6417
</note>
6418
</para>
6419
6420
</tag-desc>
6421
6422
<tag-name id="filehandle_write_str"><literal>filehandle.write(<value>string</value>[,
6423
<value>position</value>[,
6424
<value>encoding</value>]])</literal></tag-name>
6425
<tag-desc>
6426
Writes a <literal>string</literal> to the file.
6427
6428
<list type="tag">
6429
6430
<tag-name><literal>position</literal></tag-name>
6431
<tag-desc>
6432
the offset from the beginning of the file
6433
where the data from buffer should be written,
6434
can be an
6435
<literal>integer</literal> or
6436
<literal>null</literal>,
6437
by default is <literal>null</literal>.
6438
If <literal>position</literal> is not a <literal>number</literal>,
6439
the data will be written at the current position.
6440
See the POSIX
6441
<link url="https://man7.org/linux/man-pages/man2/write.2.html">pwrite(2)</link>
6442
documentation for details.
6443
</tag-desc>
6444
6445
<tag-name><literal>encoding</literal></tag-name>
6446
<tag-desc>
6447
the expected encoding of the string, by default <literal>utf8</literal>
6448
</tag-desc>
6449
6450
</list>
6451
Returns a <literal>Promise</literal> which is resolved with an object
6452
containing two properties:
6453
<list type="tag">
6454
6455
<tag-name><literal>bytesWritten</literal></tag-name>
6456
<tag-desc>
6457
is an <literal>integer</literal> representing the number of bytes written
6458
</tag-desc>
6459
6460
<tag-name><literal>buffer</literal></tag-name>
6461
<tag-desc>
6462
a reference to the buffer written, can be a
6463
<literal>Buffer</literal>,
6464
<literal>TypedArray</literal>, or
6465
<literal>DataView</literal>
6466
</tag-desc>
6467
</list>
6468
<para>
6469
<note>
6470
It is unsafe to use <literal>filehandle.write()</literal> multiple times
6471
on the same file without waiting for the promise to be resolved or rejected.
6472
</note>
6473
</para>
6474
6475
</tag-desc>
6476
6477
</list>
6478
</para>
6479
6480
</section>
6481
6482
6483
<section id="fs_stats" name="fs.Stats">
6484
6485
<para>
6486
The <literal>fs.Stats</literal> object provides information about a file.
6487
The object is returned from
6488
<link id="fs_statsync">fs.statSync()</link> and
6489
<link id="fs_lstatsync">fs.lstatSync()</link>.
6490
6491
<list type= "bullet" compact="no">
6492
6493
<listitem>
6494
<literal>stats.isBlockDevice()</literal>&mdash;returns
6495
<literal>true</literal> if the <literal>fs.Stats</literal> object describes
6496
a block device.
6497
</listitem>
6498
6499
<listitem>
6500
<literal>stats.isDirectory()</literal>&mdash;returns
6501
<literal>true</literal> if the <literal>fs.Stats</literal> object describes
6502
a file system directory.
6503
</listitem>
6504
6505
<listitem>
6506
<literal>stats.isFIFO()</literal>&mdash;returns
6507
<literal>true</literal> if the <literal>fs.Stats</literal> object describes
6508
a first-in-first-out (FIFO) pipe.
6509
</listitem>
6510
6511
<listitem>
6512
<literal>stats.isFile()</literal>&mdash;returns
6513
<literal>true</literal> if the <literal>fs.Stats</literal> object describes
6514
a regular file.
6515
</listitem>
6516
6517
<listitem>
6518
<literal>stats.isSocket()</literal>&mdash;returns
6519
<literal>true</literal> if the <literal>fs.Stats</literal> object describes
6520
a socket.
6521
</listitem>
6522
6523
<listitem>
6524
<literal>stats.isSymbolicLink()</literal>&mdash;returns
6525
<literal>true</literal> if the <literal>fs.Stats</literal> object describes
6526
a symbolic link.
6527
</listitem>
6528
6529
<listitem>
6530
<literal>stats.dev</literal>&mdash;
6531
the numeric identifier of the device containing the file.
6532
</listitem>
6533
6534
<listitem>
6535
<literal>stats.ino</literal>&mdash;
6536
the file system specific <literal>Inode</literal> number for the file.
6537
</listitem>
6538
6539
<listitem>
6540
<literal>stats.mode</literal>&mdash;
6541
a bit-field describing the file type and mode.
6542
</listitem>
6543
6544
<listitem>
6545
<literal>stats.nlink</literal>&mdash;
6546
the number of hard-links that exist for the file.
6547
</listitem>
6548
6549
<listitem>
6550
<literal>stats.uid</literal>&mdash;
6551
the numeric user identifier of the user that owns the file (POSIX).
6552
</listitem>
6553
6554
<listitem>
6555
<literal>stats.gid</literal>&mdash;
6556
the numeric group identifier of the group that owns the file (POSIX).
6557
</listitem>
6558
6559
<listitem>
6560
<literal>stats.rdev</literal>&mdash;
6561
the numeric device identifier if the file represents a device.
6562
</listitem>
6563
6564
<listitem>
6565
<literal>stats.size</literal>&mdash;
6566
the size of the file in bytes.
6567
</listitem>
6568
6569
<listitem>
6570
<literal>stats.blksize</literal>&mdash;
6571
the file system block size for i/o operations.
6572
</listitem>
6573
6574
<listitem>
6575
<literal>stats.blocks</literal>&mdash;
6576
the number of blocks allocated for this file.
6577
</listitem>
6578
6579
<listitem>
6580
<literal>stats.atimeMs</literal>&mdash;
6581
the timestamp indicating the last time this file was accessed expressed
6582
in milliseconds since the POSIX Epoch.
6583
</listitem>
6584
6585
<listitem>
6586
<literal>stats.mtimeMs</literal>&mdash;
6587
the timestamp indicating the last time this file was modified expressed
6588
in milliseconds since the POSIX Epoch.
6589
</listitem>
6590
6591
<listitem>
6592
<literal>stats.ctimeMs</literal>&mdash;
6593
the timestamp indicating the last time this file was changed expressed
6594
in milliseconds since the POSIX Epoch.
6595
</listitem>
6596
6597
<listitem>
6598
<literal>stats.birthtimeMs</literal>&mdash;
6599
the timestamp indicating the creation time of this file expressed
6600
in milliseconds since the POSIX Epoch.
6601
</listitem>
6602
6603
<listitem>
6604
<literal>stats.atime</literal>&mdash;
6605
the timestamp indicating the last time this file was accessed.
6606
</listitem>
6607
6608
<listitem>
6609
<literal>stats.mtime</literal>&mdash;
6610
the timestamp indicating the last time this file was modified.
6611
</listitem>
6612
6613
<listitem>
6614
<literal>stats.ctime</literal>&mdash;
6615
the timestamp indicating the last time this file was changed.
6616
</listitem>
6617
6618
<listitem>
6619
<literal>stats.birthtime</literal>&mdash;
6620
the timestamp indicating the creation time of this file.
6621
</listitem>
6622
6623
</list>
6624
</para>
6625
6626
</section>
6627
6628
6629
<section id="access_const" name="File Access Constants">
6630
6631
<para>
6632
The <link id="fs_accesssync"><literal>access()</literal></link> method
6633
can accept the following flags.
6634
These flags are exported by <literal>fs.constants</literal>:
6635
6636
<list type= "bullet" compact="no">
6637
6638
<listitem>
6639
<literal>F_OK</literal>&mdash;indicates that the file
6640
is visible to the calling process,
6641
used by default if no mode is specified
6642
</listitem>
6643
6644
<listitem>
6645
<literal>R_OK</literal>&mdash;indicates that the file can be
6646
read by the calling process
6647
</listitem>
6648
6649
<listitem>
6650
<literal>W_OK</literal>&mdash;indicates that the file can be
6651
written by the calling process
6652
</listitem>
6653
6654
<listitem>
6655
<literal>X_OK</literal>&mdash;indicates that the file can be
6656
executed by the calling process
6657
</listitem>
6658
6659
</list>
6660
</para>
6661
6662
</section>
6663
6664
6665
<section id="njs_api_fs_flags" name="File System Flags">
6666
6667
<para>
6668
The <literal>flag</literal> option can accept the following values:
6669
6670
<list type= "bullet" compact="no">
6671
6672
<listitem>
6673
<literal>a</literal>&mdash;open a file for appending.
6674
The file is created if it does not exist
6675
</listitem>
6676
6677
<listitem>
6678
<literal>ax</literal>&mdash;the same as <literal>a</literal>
6679
but fails if the file already exists
6680
</listitem>
6681
6682
<listitem>
6683
<literal>a+</literal>&mdash;open a file for reading and appending.
6684
If the file does not exist, it will be created
6685
</listitem>
6686
6687
<listitem>
6688
<literal>ax+</literal>&mdash;the same as <literal>a+</literal>
6689
but fails if the file already exists
6690
</listitem>
6691
6692
<listitem>
6693
<literal>as</literal>&mdash;open a file for appending
6694
in synchronous mode.
6695
If the file does not exist, it will be created
6696
</listitem>
6697
6698
<listitem>
6699
<literal>as+</literal>&mdash;open a file for reading and appending
6700
in synchronous mode.
6701
If the file does not exist, it will be created
6702
</listitem>
6703
6704
<listitem>
6705
<literal>r</literal>&mdash;open a file for reading.
6706
An exception occurs if the file does not exist
6707
</listitem>
6708
6709
<listitem>
6710
<literal>r+</literal>&mdash;open a file for reading and writing.
6711
An exception occurs if the file does not exist
6712
</listitem>
6713
6714
<listitem>
6715
<literal>rs+</literal>&mdash;open a file for reading and writing
6716
in synchronous mode.
6717
Instructs the operating system to bypass the local file system cache
6718
</listitem>
6719
6720
<listitem>
6721
<literal>w</literal>&mdash;open a file for writing.
6722
If the file does not exist, it will be created.
6723
If the file exists, it will be replaced
6724
</listitem>
6725
6726
<listitem>
6727
<literal>wx</literal>&mdash;the same as <literal>w</literal>
6728
but fails if the file already exists
6729
</listitem>
6730
6731
<listitem>
6732
<literal>w+</literal>&mdash;open a file for reading and writing.
6733
If the file does not exist, it will be created.
6734
If the file exists, it will be replaced
6735
</listitem>
6736
6737
<listitem>
6738
<literal>wx+</literal>&mdash;the same as <literal>w+</literal>
6739
but fails if the file already exists
6740
</listitem>
6741
6742
</list>
6743
</para>
6744
6745
</section>
6746
6747
</section>
6748
6749
6750
<section id="querystring" name="Query String">
6751
6752
<para>
6753
<table width="100%">
6754
<tr><td><link id="querystring_decode"><literal>querystring.decode()</literal></link></td></tr>
6755
<tr><td><link id="querystring_encode"><literal>querystring.encode()</literal></link></td></tr>
6756
<tr><td><link id="querystring_escape"><literal>querystring.escape()</literal></link></td></tr>
6757
<tr><td><link id="querystring_parse"><literal>querystring.parse()</literal></link></td></tr>
6758
<tr><td><link id="querystring_stringify"><literal>querystring.stringify()</literal></link></td></tr>
6759
<tr><td><link id="querystring_unescape"><literal>querystring.unescape()</literal></link></td></tr>
6760
</table>
6761
</para>
6762
6763
<para>
6764
The Query String module provides support
6765
for parsing and formatting URL query strings
6766
(<link doc="changes.xml" id="njs0.4.3">0.4.3</link>).
6767
The Query String module object is imported using
6768
<literal>import qs from 'querystring'</literal>.
6769
</para>
6770
6771
<para>
6772
<list type="tag">
6773
6774
<tag-name id="querystring_decode"><literal>querystring.decode()</literal></tag-name>
6775
<tag-desc>
6776
is an alias for
6777
<link id="querystring_parse"><literal>querystring.parse()</literal></link>.
6778
</tag-desc>
6779
6780
<tag-name id="querystring_encode"><literal>querystring.encode()</literal></tag-name>
6781
<tag-desc>
6782
is an alias for
6783
<link id="querystring_stringify"><literal>querystring.stringify()</literal></link>.
6784
</tag-desc>
6785
6786
<tag-name id="querystring_escape"><literal>querystring.escape(<value>string</value>)</literal></tag-name>
6787
<tag-desc>
6788
<para>
6789
Performs URL encoding of the given <literal>string</literal>,
6790
returns an escaped query string.
6791
The method is used by
6792
<link id="querystring_stringify"><literal>querystring.stringify()</literal></link>
6793
and should not be used directly.
6794
</para>
6795
</tag-desc>
6796
6797
<tag-name id="querystring_parse"><literal>querystring.parse(<value>string</value>[,
6798
<value>separator</value>[,
6799
<value>equal</value>[,
6800
<value>options</value>]]])</literal></tag-name>
6801
<tag-desc>
6802
<para>
6803
Parses the query string URL and returns an object.
6804
</para>
6805
6806
<para>
6807
The <literal>separator</literal> parameter is a substring
6808
for delimiting key and value pairs in the query string,
6809
by default is “<literal>&amp;</literal>”.
6810
</para>
6811
6812
<para>
6813
The <literal>equal</literal> parameter is a substring
6814
for delimiting keys and values in the query string,
6815
by default is “<literal>=</literal>”.
6816
</para>
6817
6818
<para>
6819
The <literal>options</literal> parameter is expected to be
6820
an object with the following keys:
6821
<list type="tag">
6822
<tag-name><literal>decodeURIComponent</literal>
6823
<value>function</value></tag-name>
6824
<tag-desc>
6825
Function used
6826
to decode percent-encoded characters in the query string,
6827
by default is
6828
<link id="querystring_unescape"><literal>querystring.unescape()</literal></link>
6829
</tag-desc>
6830
6831
<tag-name><literal>maxKeys</literal>
6832
<value>number</value></tag-name>
6833
<tag-desc>
6834
the maximum number of keys to parse,
6835
by default is <literal>1000</literal>.
6836
The <literal>0</literal> value removes limitations for counting keys.
6837
</tag-desc>
6838
6839
</list>
6840
By default, percent-encoded characters within the query string are assumed
6841
to use the UTF-8 encoding,
6842
invalid UTF-8 sequences will be replaced with
6843
the <literal>U+FFFD</literal> replacement character.
6844
</para>
6845
6846
<para>
6847
For example, for the following query string
6848
<example>
6849
'foo=bar&amp;abc=xyz&amp;abc=123'
6850
</example>
6851
the output will be:
6852
<example>
6853
{
6854
foo: 'bar',
6855
abc: ['xyz', '123']
6856
}
6857
</example>
6858
</para>
6859
6860
</tag-desc>
6861
6862
<tag-name id="querystring_stringify"><literal>querystring.stringify(<value>object</value>[,
6863
<value>separator</value>[,
6864
<value>equal</value>[,
6865
<value>options</value>]]])</literal></tag-name>
6866
<tag-desc>
6867
<para>
6868
Serializes an object and returns a URL query string.
6869
</para>
6870
6871
<para>
6872
The <literal>separator</literal> parameter is a substring
6873
for delimiting key and value pairs in the query string,
6874
by default is “<literal>&amp;</literal>”.
6875
</para>
6876
6877
<para>
6878
The <literal>equal</literal> parameter is a substring
6879
for delimiting keys and values in the query string,
6880
by default is “<literal>=</literal>”.
6881
</para>
6882
6883
<para>
6884
The <literal>options</literal> parameter is expected to be
6885
an object with the following keys:
6886
<list type="tag">
6887
<tag-name><literal>encodeURIComponent</literal>
6888
<value>function</value></tag-name>
6889
<tag-desc>
6890
The function to use when converting
6891
URL-unsafe characters to percent-encoding in the query string,
6892
by default is
6893
<link id="querystring_escape"><literal>querystring.escape()</literal></link>.
6894
</tag-desc>
6895
6896
</list>
6897
</para>
6898
6899
<para>
6900
By default, characters that require percent-encoding within the query string
6901
are encoded as UTF-8.
6902
If other encoding is required, then
6903
<literal>encodeURIComponent</literal> option should be specified.
6904
</para>
6905
6906
<para>
6907
For example, for the following command
6908
<example>
6909
querystring.stringify({ foo: 'bar', baz: ['qux', 'quux'], 123: '' });
6910
</example>
6911
the query string will be:
6912
<example>
6913
'foo=bar&amp;baz=qux&amp;baz=quux&amp;123='
6914
</example>
6915
</para>
6916
6917
</tag-desc>
6918
6919
<tag-name id="querystring_unescape"><literal>querystring.unescape(<value>string</value>)</literal></tag-name>
6920
<tag-desc>
6921
<para>
6922
Performs decoding of URL percent-encoded characters
6923
of the <literal>string</literal>,
6924
returns an unescaped query string.
6925
The method is used by
6926
<link id="querystring_parse"><literal>querystring.parse()</literal></link>
6927
and should not be used directly.
6928
</para>
6929
</tag-desc>
6930
6931
</list>
6932
</para>
6933
6934
</section>
6935
6936
6937
<section id="xml" name="XML">
6938
6939
<para>
6940
<table width="100%">
6941
<tr><td><link id="xml_parse"><literal>xml.parse()</literal></link></td></tr>
6942
<tr><td><link id="xml_c14n"><literal>xml.c14n()</literal></link></td></tr>
6943
<tr><td><link id="xml_exclusivec14n"><literal>xml.exclusiveC14n()</literal></link></td></tr>
6944
<tr><td><link id="xml_serialize"><literal>xml.serialize()</literal></link></td></tr>
6945
<tr><td><link id="xml_serialize_tostring"><literal>xml.serializeToString()</literal></link></td></tr>
6946
<tr><td><link id="xml_doc"><literal>XMLDoc</literal></link></td></tr>
6947
<tr><td><link id="xml_node"><literal>XMLNode</literal></link></td></tr>
6948
<tr><td><link id="xml_xmlattr"><literal>XMLAttr</literal></link></td></tr>
6949
</table>
6950
</para>
6951
6952
<para>
6953
The XML module allows working with XML documents
6954
(since <link doc="changes.xml" id="njs0.7.10">0.7.10</link>).
6955
The XML module object is imported using
6956
<literal>import xml from 'xml'</literal>.
6957
</para>
6958
6959
<para>
6960
Example:
6961
<example>
6962
import xml from 'xml';
6963
6964
let data = `&lt;note&gt;&lt;to b="bar" a= "foo" &gt;Tove&lt;/to&gt;&lt;from&gt;Jani&lt;/from&gt;&lt;/note&gt;`;
6965
let doc = xml.parse(data);
6966
6967
console.log(doc.note.to.$text) /* 'Tove' */
6968
console.log(doc.note.to.$attr$b) /* 'bar' */
6969
console.log(doc.note.$tags[1].$text) /* 'Jani' */
6970
6971
let dec = new TextDecoder();
6972
let c14n = dec.decode(xml.exclusiveC14n(doc.note));
6973
console.log(c14n) /* '&lt;note&gt;&lt;to a="foo" b="bar"&gt;Tove&lt;/to&gt;&lt;from&gt;Jani&lt;/from&gt;&lt;/note&gt;' */
6974
6975
c14n = dec.decode(xml.exclusiveC14n(doc.note.to));
6976
console.log(c14n) /* '&lt;to a="foo" b="bar">Tove&lt;/to&gt;' */
6977
6978
c14n = dec.decode(xml.exclusiveC14n(doc.note, doc.note.to /* excluding 'to' */));
6979
console.log(c14n) /* '&lt;note&gt;&lt;from&gt;Jani&lt;/from&gt;&lt;/note&gt;' */
6980
</example>
6981
</para>
6982
6983
<para>
6984
<list type="tag">
6985
6986
<tag-name id="xml_parse"><literal>parse(<value>string</value> |
6987
<value>Buffer</value>)</literal></tag-name>
6988
<tag-desc>
6989
Parses a string or Buffer for an XML document,
6990
returns an
6991
<link id="xml_doc"><literal>XMLDoc</literal></link> wrapper object
6992
representing the parsed XML document.
6993
</tag-desc>
6994
6995
<tag-name id="xml_c14n"><literal>c14n(<value>root_node</value>[,
6996
<value>excluding_node</value>])</literal></tag-name>
6997
<tag-desc>
6998
Canonicalizes <literal>root_node</literal> and its children according to
6999
<link url="https://www.w3.org/TR/xml-c14n">Canonical XML Version 1.1</link>.
7000
The <literal>root_node</literal> can be
7001
<link id="xml_node"><literal>XMLNode</literal></link> or
7002
<link id="xml_doc"><literal>XMLDoc</literal></link> wrapper object
7003
around XML structure.
7004
Returns Buffer object that contains canonicalized output.
7005
7006
<para>
7007
<list type="tag">
7008
7009
<tag-name><literal>excluding_node</literal></tag-name>
7010
<tag-desc>
7011
allows omitting from the output a part of the document
7012
</tag-desc>
7013
7014
</list>
7015
</para>
7016
7017
</tag-desc>
7018
7019
<tag-name id="xml_exclusivec14n"><literal>exclusiveC14n(<value>root_node</value>[,
7020
<value>excluding_node</value>[,
7021
<value>withComments</value>
7022
[,<value>prefix_list</value>]]])</literal></tag-name>
7023
<tag-desc>
7024
Canonicalizes <literal>root_node</literal> and its children according to
7025
<link url="https://www.w3.org/TR/xml-exc-c14n/">Exclusive XML
7026
Canonicalization Version 1.0</link>.
7027
7028
<para>
7029
<list type="tag">
7030
7031
<tag-name><literal>root_node</literal></tag-name>
7032
<tag-desc>
7033
is
7034
<link id="xml_node"><literal>XMLNode</literal></link> or
7035
<link id="xml_doc"><literal>XMLDoc</literal></link> wrapper object
7036
around XML structure
7037
</tag-desc>
7038
7039
<tag-name><literal>excluding_node</literal></tag-name>
7040
<tag-desc>
7041
allows omitting from the output a part of the document
7042
corresponding to the node and its children
7043
</tag-desc>
7044
7045
<tag-name><literal>withComments</literal></tag-name>
7046
<tag-desc>
7047
a boolean value, <literal>false</literal> by default.
7048
If <literal>true</literal>, canonicalization corresponds to
7049
<link url="http://www.w3.org/2001/10/xml-exc-c14n#WithComments">Exclusive XML
7050
Canonicalization Version 1.0</link>.
7051
Returns Buffer object that contains canonicalized output.
7052
</tag-desc>
7053
7054
<tag-name><literal>prefix_list</literal></tag-name>
7055
<tag-desc>
7056
an optional string with a space separated namespace prefixes
7057
for namespaces that should also be included into the output
7058
</tag-desc>
7059
7060
</list>
7061
</para>
7062
7063
</tag-desc>
7064
7065
<tag-name id="xml_serialize"><literal>serialize()</literal></tag-name>
7066
<tag-desc>
7067
The same as
7068
<link id="xml_c14n"><literal>xml.c14n()</literal></link>
7069
(since <link doc="changes.xml" id="njs0.7.11">0.7.11</link>).
7070
</tag-desc>
7071
7072
<tag-name id="xml_serialize_tostring"><literal>serializeToString()</literal></tag-name>
7073
<tag-desc>
7074
The same as
7075
<link id="xml_c14n"><literal>xml.c14n()</literal></link>
7076
except it returns the result as a <literal>string</literal>
7077
(since <link doc="changes.xml" id="njs0.7.11">0.7.11</link>).
7078
</tag-desc>
7079
7080
<tag-name id="xml_doc"><literal>XMLDoc</literal></tag-name>
7081
<tag-desc>
7082
An XMLDoc wrapper object around XML structure,
7083
the root node of the document.
7084
7085
<para>
7086
<list type="tag">
7087
7088
<tag-name id="xml_doc_root"><literal>doc.$root</literal></tag-name>
7089
<tag-desc>
7090
the document's root by its name or undefined
7091
</tag-desc>
7092
7093
<tag-name id="xml_doc_xxx"><literal>doc.<value>abc</value></literal></tag-name>
7094
<tag-desc>
7095
the first root tag named <value>abc</value> as
7096
<link id="xml_node"><literal>XMLNode</literal></link> wrapper object
7097
</tag-desc>
7098
7099
</list>
7100
</para>
7101
7102
</tag-desc>
7103
7104
<tag-name id="xml_node"><literal>XMLNode</literal></tag-name>
7105
<tag-desc>
7106
An XMLNode wrapper object around XML tag node.
7107
<para>
7108
<list type="tag">
7109
7110
<tag-name id="node_abc"><literal>node.<value>abc</value></literal></tag-name>
7111
<tag-desc>
7112
the same as
7113
<link id="node_tag"><literal>node.$tag$<value>abc</value></literal></link>
7114
</tag-desc>
7115
7116
<tag-name id="node_attr"><literal>node.$attr$<value>abc</value></literal></tag-name>
7117
<tag-desc>
7118
the node's attribute value of <value>abc</value>,
7119
writable
7120
since <link doc="changes.xml" id="njs0.7.11">0.7.11</link>
7121
</tag-desc>
7122
7123
<tag-name id="node_attr_value"><literal>node.$attr$<value>abc</value></literal>=<value>xyz</value></tag-name>
7124
<tag-desc>
7125
the same as
7126
<link id="node_setattribute"><literal>node.setAttribute('<value>abc</value>',
7127
<value>xyz</value>)</literal></link>
7128
(since <link doc="changes.xml" id="njs0.7.11">0.7.11</link>)
7129
</tag-desc>
7130
7131
<tag-name id="node_attrs"><literal>node.$attrs</literal></tag-name>
7132
<tag-desc>
7133
an <link id="xml_xmlattr"><literal>XMLAttr</literal></link> wrapper object
7134
for all attributes of the node
7135
</tag-desc>
7136
7137
<tag-name id="node_name"><literal>node.$name</literal></tag-name>
7138
<tag-desc>
7139
the name of the node
7140
</tag-desc>
7141
7142
<tag-name id="node_ns"><literal>node.$ns</literal></tag-name>
7143
<tag-desc>
7144
the namespace of the node
7145
</tag-desc>
7146
7147
<tag-name id="node_parent"><literal>node.$parent</literal></tag-name>
7148
<tag-desc>
7149
the parent node of the current node
7150
</tag-desc>
7151
7152
<tag-name id="node_tag"><literal>node.$tag$<value>abc</value></literal></tag-name>
7153
<tag-desc>
7154
the first child tag of the node named <value>abc</value>,
7155
writable
7156
since <link doc="changes.xml" id="njs0.7.11">0.7.11</link>
7157
</tag-desc>
7158
7159
<tag-name id="node_tags"><literal>node.$tags</literal></tag-name>
7160
<tag-desc>
7161
an array of all children tags
7162
</tag-desc>
7163
7164
<tag-name id="node_tags_"><literal>node.$tags = [node1, node2, ...]</literal></tag-name>
7165
<tag-desc>
7166
the same as
7167
<link id="node_removechildren"><literal>node.removeChildren</literal>()</link>;
7168
<link id="node_addchild"><literal>node.addChild(<value>node1</value>)</literal></link>;
7169
<link id="node_addchild"><literal>node.addChild(<value>node2</value>)</literal></link>
7170
(since <link doc="changes.xml" id="njs0.7.11">0.7.11</link>).
7171
</tag-desc>
7172
7173
<tag-name id="node_tags_abc"><literal>node.$tags$<value>abc</value></literal></tag-name>
7174
<tag-desc>
7175
all children tags named <value>abc</value> of the node,
7176
writable
7177
since <link doc="changes.xml" id="njs0.7.11">0.7.11</link>
7178
</tag-desc>
7179
7180
<tag-name id="node_text"><literal>node.$text</literal></tag-name>
7181
<tag-desc>
7182
the content of the node,
7183
writable
7184
since <link doc="changes.xml" id="njs0.7.11">0.7.11</link>
7185
</tag-desc>
7186
7187
<tag-name id="node_text_abc"><literal>node.$text = 'abc' </literal></tag-name>
7188
<tag-desc>
7189
the same as
7190
<link id="node_settext"><literal>node.setText('abc')</literal></link>
7191
(since <link doc="changes.xml" id="njs0.7.11">0.7.11</link>)
7192
</tag-desc>
7193
7194
<tag-name id="node_addchild"><literal>node.addChild(<value>nd</value>)</literal></tag-name>
7195
<tag-desc>
7196
adds XMLNode as a child to node
7197
(since <link doc="changes.xml" id="njs0.7.11">0.7.11</link>).
7198
<literal>nd</literal> is recursively copied before adding to the node
7199
</tag-desc>
7200
7201
<tag-name id="node_removeallattr"><literal>node.removeAllAttributes()</literal></tag-name>
7202
<tag-desc>
7203
removes all attributes of the node
7204
(since <link doc="changes.xml" id="njs0.7.11">0.7.11</link>)
7205
</tag-desc>
7206
7207
<tag-name id="node_removeattr"><literal>node.removeAttribute(<value>attr_name</value>)</literal></tag-name>
7208
<tag-desc>
7209
removes the attribute named <literal>attr_name</literal>
7210
(since <link doc="changes.xml" id="njs0.7.11">0.7.11</link>)
7211
</tag-desc>
7212
7213
<tag-name id="node_removechildren"><literal>node.removeChildren(<value>tag_name</value>)</literal></tag-name>
7214
<tag-desc>
7215
removes all the children tags named <literal>tag_name</literal>
7216
(since <link doc="changes.xml" id="njs0.7.11">0.7.11</link>).
7217
If <literal>tag_name</literal> is absent, all children tags are removed
7218
</tag-desc>
7219
7220
<tag-name id="node_removetext"><literal>node.removeText()</literal></tag-name>
7221
<tag-desc>
7222
removes the node's text value
7223
(<link doc="changes.xml" id="njs0.7.11">0.7.11</link>)
7224
</tag-desc>
7225
7226
<tag-name id="node_setattr"><literal>node.setAttribute(<value>attr_name</value>,
7227
<value>value</value>)</literal></tag-name>
7228
<tag-desc>
7229
sets a value for an <literal>attr_name</literal>
7230
(since <link doc="changes.xml" id="njs0.7.11">0.7.11</link>).
7231
When the value is <literal>null</literal>,
7232
the attribute named <literal>attr_name</literal> is deleted
7233
</tag-desc>
7234
7235
<tag-name id="node_settext"><literal>node.setText(<value>value</value>)</literal></tag-name>
7236
<tag-desc>
7237
sets a text value for the node
7238
(since <link doc="changes.xml" id="njs0.7.11">0.7.11</link>).
7239
When the value is <literal>null</literal>, the text of the node is deleted.
7240
</tag-desc>
7241
7242
</list>
7243
</para>
7244
7245
</tag-desc>
7246
7247
<tag-name id="xml_xmlattr"><literal>XMLAttr</literal></tag-name>
7248
<tag-desc>
7249
An XMLAttrs wrapper object around XML node attributes.
7250
7251
<para>
7252
<list type="tag">
7253
7254
<tag-name id="xmlattr_x"><literal>attr.<value>abc</value></literal></tag-name>
7255
<tag-desc>
7256
the attribute value of <value>abc</value>
7257
</tag-desc>
7258
7259
</list>
7260
</para>
7261
7262
</tag-desc>
7263
7264
</list>
7265
</para>
7266
7267
</section>
7268
7269
7270
<section id="zlib" name="zlib">
7271
7272
<para>
7273
<table width="100%">
7274
<tr><td><link id="zlib_deflaterawsync"><literal>zlib.deflateRawSync()</literal></link></td></tr>
7275
<tr><td><link id="zlib_deflatesync"><literal>zlib.deflateSync()</literal></link></td></tr>
7276
<tr><td><link id="zlib_inflaterawsync"><literal>zlib.inflateRawSync()</literal></link></td></tr>
7277
<tr><td><link id="zlib_inflatesync"><literal>zlib.inflateSync()</literal></link></td></tr>
7278
</table>
7279
</para>
7280
7281
<para>
7282
The zlib module provides compression functionality using the
7283
“deflate” and “inflate” algorithms
7284
(since <link doc="changes.xml" id="njs0.7.12">0.7.12</link>).
7285
The zlib module object is imported using
7286
<literal>import zlib from 'zlib'</literal>.
7287
</para>
7288
7289
<para>
7290
<list type="tag">
7291
7292
<tag-name id="zlib_deflaterawsync"><literal>deflateRawSync(<value>string</value> |
7293
<value>Buffer</value>[,
7294
<value>options</value>])</literal></tag-name>
7295
<tag-desc>
7296
Compresses data using the “deflate” algorithm provided as a string or Buffer
7297
and does not append a zlib header.
7298
The buffer value can be a
7299
<literal>Buffer</literal>,
7300
<literal>TypedArray</literal>, or
7301
<literal>DataView</literal>.
7302
<literal>Options</literal> is an optional object that contains
7303
<link id="zlib_options"/>.
7304
Returns Buffer instance that contains the compressed data.
7305
</tag-desc>
7306
7307
<tag-name id="zlib_deflatesync"><literal>deflateSync(<value>string</value> |
7308
<value>Buffer</value>[,
7309
<value>options</value>])</literal></tag-name>
7310
<tag-desc>
7311
Compresses data using the “deflate” algorithm provided as a string or Buffer.
7312
The Buffer value can be a
7313
<literal>Buffer</literal>,
7314
<literal>TypedArray</literal>, or
7315
<literal>DataView</literal>.
7316
<literal>Options</literal> is an optional object that contains
7317
<link id="zlib_options"/>.
7318
Returns Buffer instance that contains the compressed data.
7319
</tag-desc>
7320
7321
<tag-name id="zlib_inflaterawsync"><literal>inflateRawSync(<value>string</value> |
7322
<value>Buffer</value>)</literal></tag-name>
7323
<tag-desc>
7324
Decompresses a raw stream by using the “deflate” algorithm.
7325
Returns Buffer instance that contains the decompressed data.
7326
</tag-desc>
7327
7328
<tag-name id="zlib_inflatesync"><literal>inflateSync(<value>string</value> |
7329
<value>Buffer</value>)</literal></tag-name>
7330
<tag-desc>
7331
Decompresses a stream by using the “deflate” algorithm.
7332
Returns Buffer instance that contains the decompressed data.
7333
</tag-desc>
7334
7335
</list>
7336
</para>
7337
7338
7339
<section id="zlib_options" name="zlib options">
7340
7341
<para>
7342
<list type= "bullet" compact="no">
7343
7344
<listitem>
7345
<literal>chunkSize</literal>&mdash;is an integer,
7346
by default is <literal>1024</literal>
7347
</listitem>
7348
7349
<listitem>
7350
<literal>dictionary</literal>&mdash;is a
7351
<literal>Buffer</literal>,
7352
<literal>TypedArray</literal>, or
7353
<literal>DataView</literal>.
7354
by default is empty
7355
</listitem>
7356
7357
<listitem>
7358
<literal>level</literal>&mdash;is an integer, compression only,
7359
see <link id="zlib_compression_levels"/>
7360
</listitem>
7361
7362
<listitem>
7363
<literal>memLevel</literal>&mdash;is an integer
7364
from <literal>1</literal> to <literal>9</literal>, compression only
7365
</listitem>
7366
7367
<listitem>
7368
<literal>strategy</literal>&mdash;is an integer, compression only,
7369
see <link id="zlib_compression_strategy"/>
7370
</listitem>
7371
7372
<listitem>
7373
<literal>windowBits</literal>&mdash;is an integer
7374
from <literal>-15</literal> to <literal>-9</literal>
7375
for raw data,
7376
from <literal>9</literal> to <literal>15</literal>
7377
for an ordinary stream
7378
</listitem>
7379
7380
</list>
7381
</para>
7382
7383
</section>
7384
7385
<section id="zlib_compression_levels" name="zlib compression levels">
7386
7387
<para>
7388
<table>
7389
<tr><td>Name</td><td>Description</td></tr>
7390
<tr><td><literal>zlib.constants.Z_NO_COMPRESSION</literal></td><td>no compression</td></tr>
7391
<tr><td><literal>zlib.constants.Z_BEST_SPEED</literal></td><td>fastest, produces the least compression</td></tr>
7392
<tr><td><literal>zlib.constants.Z_DEFAULT_COMPRESSION</literal></td><td>trade-off between speed and compression</td></tr>
7393
<tr><td><literal>zlib.constants.Z_BEST_COMPRESSION</literal></td><td>slowest, produces the most compression</td></tr>
7394
</table>
7395
</para>
7396
7397
</section>
7398
7399
<section id="zlib_compression_strategy" name="zlib compression strategy">
7400
7401
<para>
7402
<table>
7403
<tr><td>Name</td><td>Description</td></tr>
7404
<tr><td><literal>zlib.constants.Z_FILTERED</literal></td><td>Filtered strategy: for the data produced by a filter or predictor</td></tr>
7405
<tr><td><literal>zlib.constants.Z_HUFFMAN_ONLY</literal></td><td>Huffman-only strategy: only Huffman encoding, no string matching</td></tr>
7406
<tr><td><literal>zlib.constants.Z_RLE</literal></td><td>Run Length Encoding strategy: limit match distances to one, better compression of PNG image data</td></tr>
7407
<tr><td><literal>zlib.constants.Z_FIXED</literal></td><td>Fixed table strategy: prevents the use of dynamic Huffman codes, a simpler decoder for special applications</td></tr>
7408
<tr><td><literal>zlib.constants.Z_DEFAULT_STRATEGY</literal></td><td>Default strategy, suitable for general purpose compression</td></tr>
7409
</table>
7410
</para>
7411
7412
</section>
7413
7414
</section>
7415
7416
</section>
7417
7418
</article>
7419
7420