comparison xml/en/docs/njs/reference.xml @ 2246:32ba43abf9cd

Renamed njs API, njs Changes.
author Yaroslav Zhuravlev <yar@nginx.com>
date Mon, 24 Sep 2018 19:24:04 +0300
parents xml/en/docs/njs/njs_api.xml@87a0e2c73a25
children 0f16ef9a8dbe
comparison
equal deleted inserted replaced
2245:87a0e2c73a25 2246:32ba43abf9cd
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="9">
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 </section>
22
23
24 <section id="core" name="Core">
25
26
27 <section id="string" name="String">
28
29 <para>
30 There are two types of strings:
31 a <literal>Unicode string</literal> (default) and
32 a <literal>byte string</literal>.
33 </para>
34
35 <para>
36 A <literal>Unicode string</literal> corresponds to an ECMAScript string
37 which contains Unicode characters.
38 </para>
39
40 <para>
41 <literal>Byte strings</literal> contain a sequence of bytes.
42 They are used to serialize Unicode strings
43 to external data and deserialize from external sources.
44 For example, the <link id="string_toutf8">toUTF8()</link> method serializes
45 a Unicode string to a byte string using UTF8 encoding:
46 <example>
47 >> '£'.toUTF8().toString('hex')
48 'c2a3' /* C2 A3 is the UTF8 representation of 00A3 ('£') code point */
49 </example>
50 The <link id="string_tobytes">toBytes()</link> method serializes
51 a Unicode string with code points up to 255 into a byte string,
52 otherwise, <literal>null</literal> is returned:
53 <example>
54 >> '£'.toBytes().toString('hex')
55 'a3' /* a3 is a byte equal to 00A3 ('£') code point */
56 </example>
57 Only byte strings can be converted to different encodings.
58 For example, a string cannot be encoded to <literal>hex</literal> directly:
59 <example>
60 >> 'αβγδ'.toString('base64')
61 TypeError: argument must be a byte string
62 at String.prototype.toString (native)
63 at main (native)
64 </example>
65 To convert a Unicode string to hex,
66 first, it should be converted to a byte string and then to hex:
67 <example>
68 >> 'αβγδ'.toUTF8().toString('base64')
69 'zrHOss6zzrQ='
70 </example>
71
72 <list type="tag">
73
74 <tag-name id="string_bytesfrom"><literal>String.bytesFrom(<value>array</value>
75 | <value>string</value>, <value>encoding</value>)</literal></tag-name>
76 <tag-desc>
77 (njs specific) Creates a byte string either from an array that contains octets,
78 or from an encoded string (0.2.3).
79 The encoding can be
80 <literal>hex</literal>,
81 <literal>base64</literal>, and
82 <literal>base64url</literal>.
83 <example>
84 >> String.bytesFrom([0x62, 0x75, 0x66, 0x66, 0x65, 0x72])
85 'buffer'
86
87 >> String.bytesFrom('YnVmZmVy', 'base64')
88 'buffer'
89 </example>
90 </tag-desc>
91
92 <tag-name><literal>String.fromCodePoint(<value>codePoint1</value>[, ...[,
93 <value>codePoint2</value>]])</literal></tag-name>
94 <tag-desc>
95 Returns a string from one or more Unicode code points.
96 <example>
97 >> String.fromCodePoint(97, 98, 99, 100)
98 'abcd'
99 </example>
100 </tag-desc>
101
102 <tag-name><literal>String.prototype.concat(<value>string1</value>[, ...,
103 <value>stringN</value>])</literal></tag-name>
104 <tag-desc>
105 Returns a string that contains the concatenation of specified
106 <literal>strings</literal>.
107 <example>
108 >> "a".concat("b", "c")
109 'abc'
110 </example>
111 </tag-desc>
112
113 <tag-name><literal>String.prototype.endsWith(<value>searchString</value>[,
114 <value>length</value>])</literal></tag-name>
115 <tag-desc>
116 Returns <literal>true</literal> if a string ends with the characters
117 of a specified string, otherwise <literal>false</literal>.
118 The optional <literal>length</literal> parameter is the the length of string.
119 If omitted, the default value is the length of the string.
120 <example>
121 >> 'abc'.endsWith('abc')
122 true
123 >> 'abca'.endsWith('abc')
124 false
125 </example>
126 </tag-desc>
127
128 <tag-name><literal>String.prototype.fromBytes(<value>start</value>[,
129 <value>end</value>])</literal></tag-name>
130 <tag-desc>
131 (njs specific) Returns a new Unicode string from a byte string
132 where each byte is replaced with a corresponding Unicode code point.
133 </tag-desc>
134
135 <tag-name><literal>String.prototype.fromUTF8(<value>start</value>[,
136 <value>end</value>])</literal></tag-name>
137 <tag-desc>
138 (njs specific) Converts a byte string containing a valid UTF8 string
139 into a Unicode string,
140 otherwise <literal>null</literal> is returned.
141 </tag-desc>
142
143 <tag-name><literal>String.prototype.includes(<value>searchString</value>[,
144 <value>position</value>]))</literal></tag-name>
145 <tag-desc>
146 Returns <literal>true</literal> if a string is found within another string,
147 otherwise <literal>false</literal>.
148 The optional <literal>position</literal> parameter is the position
149 within the string at which to begin search for <literal>searchString</literal>.
150 Default value is 0.
151 <example>
152 >> 'abc'.includes('bc')
153 true
154 </example>
155 </tag-desc>
156
157 <tag-name><literal>String.prototype.indexOf(<value>searchString</value>[,
158 <value>fromIndex</value>])</literal></tag-name>
159 <tag-desc>
160 Returns the position of the first occurrence
161 of the <literal>searchString</literal>
162 The search is started at <literal>fromIndex</literal>.
163 Returns <value>-1</value> if the value is not found.
164 The <literal>fromIndex</literal> is an integer,
165 default value is 0.
166 If <literal>fromIndex</literal> is lower than 0
167 or greater than
168 <link id="string_length">String.prototype.length</link><value></value>,
169 the search starts at index <value>0</value> and
170 <value>String.prototype.length</value>.
171 <example>
172 >> 'abcdef'.indexOf('de', 2)
173 3
174 </example>
175 </tag-desc>
176
177 <tag-name><literal>String.prototype.lastIndexOf(<value>searchString</value>[,
178 <value>fromIndex</value>])</literal></tag-name>
179 <tag-desc>
180 Returns the position of the last occurrence
181 of the <literal>searchString</literal>,
182 searching backwards from <literal>fromIndex</literal>.
183 Returns <value>-1</value> if the value is not found.
184 If <literal>searchString</literal> is empty,
185 then <literal>fromIndex</literal> is returned.
186 <example>
187 >> "nginx".lastIndexOf("gi")
188 1
189 </example>
190 </tag-desc>
191
192 <tag-name id="string_length"><literal>String.prototype.length</literal></tag-name>
193 <tag-desc>
194 Returns the length of the string.
195 <example>
196 >> 'αβγδ'.length
197 4
198 </example>
199 </tag-desc>
200
201 <tag-name><literal>String.prototype.match([<value>regexp</value>])</literal></tag-name>
202 <tag-desc>
203 Matches a string against a <literal>regexp</literal>.
204 <example>
205 >> 'nginx'.match( /ng/i )
206 'ng'
207 </example>
208 </tag-desc>
209
210 <tag-name id="string_padend"><literal>String.prototype.padEnd(<value>length</value>
211 [, <value>string</value>])</literal></tag-name>
212 <tag-desc>
213 Returns a string of a specified <literal>length</literal>
214 with the pad <literal>string</literal> applied
215 to the end of the specified string (0.2.3).
216 <example>
217 >> '1234'.padEnd(8, 'abcd')
218 '1234abcd'
219 </example>
220 </tag-desc>
221
222 <tag-name id="string_padstart"><literal>String.prototype.padStart(<value>length</value>
223 [, <value>string</value>])</literal></tag-name>
224 <tag-desc>
225 Returns a string of a specified <literal>length</literal>
226 with the pad <literal>string</literal> applied
227 to the start of the specified string (0.2.3).
228 <example>
229 >> '1234'.padStart(8, 'abcd')
230 'abcd1234'
231 </example>
232 </tag-desc>
233
234 <tag-name><literal>String.prototype.repeat(<value>number</value>)</literal></tag-name>
235 <tag-desc>
236 Returns a string
237 with the specified <literal>number</literal> of copies of the string.
238 <example>
239 >> 'abc'.repeat(3)
240 'abcabcabc'
241 </example>
242 </tag-desc>
243
244 <tag-name><literal>String.prototype.replace([<value>regexp</value>|<value>string</value>[,
245 <value>string</value>|<value>function</value>]])</literal></tag-name>
246 <tag-desc>
247 Returns a new string with matches of a pattern
248 (<literal>string</literal> or a <literal>regexp</literal>)
249 replaced by a <literal>string</literal> or a <literal>function</literal>.
250 <example>
251 >> 'abcdefgh'.replace('d', 1)
252 'abc1efgh'
253 </example>
254 </tag-desc>
255
256 <tag-name><literal>String.prototype.search([<value>regexp</value>])</literal></tag-name>
257 <tag-desc>
258 Searches for a string using a <literal>regexp</literal>
259 <example>
260 >> 'abcdefgh'.search('def')
261 3
262 </example>
263 </tag-desc>
264
265 <tag-name><literal>String.prototype.slice(<value>start</value>[,
266 <value>end</value>])</literal></tag-name>
267 <tag-desc>
268 Returns a new string containing a part of an
269 original string between <literal>start</literal>
270 and <literal>end</literal> or
271 from <literal>start</literal> to the end of the string.
272 <example>
273 >> 'abcdefghijklmno'.slice(NaN, 5)
274 'abcde'
275 </example>
276 </tag-desc>
277
278 <tag-name><literal>String.prototype.startsWith(<value>searchString</value>[,
279 <value>position</value>])</literal></tag-name>
280 <tag-desc>
281 Returns <literal>true</literal> if a string begins with the characters
282 of a specified string, otherwise <literal>false</literal>.
283 The optional <literal>position</literal> parameter is the position
284 in this string at which to begin search for <literal>searchString</literal>.
285 Default value is 0.
286 <example>
287 >> 'abc'.startsWith('abc')
288 true
289 > 'aabc'.startsWith('abc')
290 false
291 </example>
292 </tag-desc>
293
294 <tag-name><literal>String.prototype.substr(<value>start</value>[,
295 <value>length</value>])</literal></tag-name>
296 <tag-desc>
297 Returns the part of the string of the specified <literal>length</literal>
298 from <literal>start</literal>
299 or from <literal>start</literal> to the end of the string.
300 <example>
301 >> 'abcdefghijklmno'.substr(3, 5)
302 'defgh'
303 </example>
304 </tag-desc>
305
306 <tag-name><literal>String.prototype.substring(<value>start</value>[,
307 <value>end</value>])</literal></tag-name>
308 <tag-desc>
309 Returns the part of the string between
310 <literal>start</literal> and <literal>end</literal> or
311 from <literal>start</literal> to the end of the string.
312 <example>
313 >> 'abcdefghijklmno'.substring(3, 5)
314 'de'
315 </example>
316 </tag-desc>
317
318 <tag-name id="string_tobytes"><literal>String.prototype.toBytes(start[,
319 end])</literal></tag-name>
320 <tag-desc>
321 (njs specific) Serializes a Unicode string to a byte string.
322 Returns <literal>null</literal> if a character larger than 255 is
323 found in the string.
324 </tag-desc>
325
326 <tag-name><literal>String.prototype.toLowerCase()</literal></tag-name>
327 <tag-desc>
328 Converts a string to lower case.
329 The method supports only simple Unicode folding.
330 <example>
331 >> 'ΑΒΓΔ'.toLowerCase()
332 'αβγδ'
333 </example>
334 </tag-desc>
335
336 <tag-name><literal>String.prototype.toString([<value>encoding</value>])</literal></tag-name>
337 <tag-desc>
338 <para>
339 If no <literal>encoding</literal> is specified,
340 returns a specified Unicode string or byte string as in ECMAScript.
341 </para>
342
343 <para>
344 (njs specific) If <literal>encoding</literal> is specified,
345 encodes a <link id="string_tobytes">byte string</link> to
346 <literal>hex</literal>,
347 <literal>base64</literal>, or
348 <literal>base64url</literal>.
349 </para>
350 <example>
351 >> 'αβγδ'.toUTF8().toString('base64url')
352 'zrHOss6zzrQ'
353 </example>
354 </tag-desc>
355
356 <tag-name><literal>String.prototype.toUpperCase()</literal></tag-name>
357 <tag-desc>
358 Converts a string to upper case.
359 The method supports only simple Unicode folding.
360 <example>
361 >> 'αβγδ'.toUpperCase()
362 'ΑΒΓΔ'
363 </example>
364 </tag-desc>
365
366 <tag-name id="string_toutf8"><literal>String.prototype.toUTF8(<value>start</value>[,
367 <value>end</value>])</literal></tag-name>
368 <tag-desc>
369 (njs specific) Serializes a Unicode string
370 to a byte string using UTF8 encoding.
371 <example>
372 >> 'αβγδ'.toUTF8().length
373 8
374 >> 'αβγδ'.length
375 4
376 </example>
377 </tag-desc>
378
379 <tag-name><literal>String.prototype.trim()</literal></tag-name>
380 <tag-desc>
381 Removes whitespaces from both ends of a string.
382 <example>
383 >> ' abc '.trim()
384 'abc'
385 </example>
386 </tag-desc>
387
388 <tag-name><literal>String.prototype.split(([<value>string</value>|<value>regexp</value>[,
389 <value>limit</value>]]))</literal></tag-name>
390 <tag-desc>
391 Returns match of a string against a <literal>regexp</literal>.
392 The optional <literal>limit</literal> parameter is an integer that specifies
393 a limit on the number of splits to be found.
394 <example>
395 >> 'abc'.split('')
396 [
397 'a',
398 'b',
399 'c'
400 ]
401 </example>
402 </tag-desc>
403
404 <tag-name id="encodeuri"><literal>encodeURI(<value>URI</value>)</literal></tag-name>
405 <tag-desc>
406 encodes a URI by replacing each instance of certain characters by
407 one, two, three, or four escape sequences
408 representing the UTF-8 encoding of the character
409 <example>
410 >> encodeURI('012αβγδ')
411 '012%CE%B1%CE%B2%CE%B3%CE%B4'
412 </example>
413 </tag-desc>
414
415 <tag-name><literal>encodeURIComponent(<value>encodedURIString</value>)</literal></tag-name>
416 <tag-desc>
417 Encodes a URI by replacing each instance of certain characters
418 by one, two, three, or four escape sequences
419 representing the UTF-8 encoding of the character.
420 <example>
421 >> encodeURIComponent('[@?=')
422 '%5B%40%3F%3D'
423 </example>
424 </tag-desc>
425
426 <tag-name><literal>decodeURI(<value>encodedURI</value>)</literal></tag-name>
427 <tag-desc>
428 Decodes a previously <link id="encodeuri">encoded</link> URI.
429 <example>
430 >> decodeURI('012%CE%B1%CE%B2%CE%B3%CE%B4')
431 '012αβγδ'
432 </example>
433 </tag-desc>
434
435 <tag-name><literal>decodeURIComponent(<value>decodedURIString</value>)</literal></tag-name>
436 <tag-desc>
437 Decodes an encoded component of a previously encoded URI.
438 <example>
439 >> decodeURIComponent('%5B%40%3F%3D')
440 '[@?='
441 </example>
442 </tag-desc>
443
444 </list>
445 </para>
446
447 </section>
448
449
450 <section id="core_json" name="JSON">
451
452 <para>
453 The <literal>JSON</literal> object (ES 5.1) provides functions
454 to convert njs values to and from JSON format.
455 <list type="tag">
456
457 <tag-name><literal>JSON.parse(<value>string</value>[,
458 <value>reviver</value>])</literal></tag-name>
459 <tag-desc>
460 Converts a <literal>string</literal> that represents JSON data
461 into an njs object (<literal>{...}</literal>) or
462 array (<literal>[...]</literal>).
463 The optional <literal>reviver</literal> parameter is a function (key, value)
464 that will be called for each (key,value) pair and can transform the value.
465 </tag-desc>
466
467 <tag-name><literal>JSON.stringify(<value>value</value>[,
468 <value>replacer</value>] [, <value>space</value>])</literal></tag-name>
469 <tag-desc>
470 Converts an njs object back to JSON.
471 The obligatory <literal>value</literal> parameter is generally a JSON
472 <literal>object</literal> or <literal>array</literal> that will be converted.
473 If the value has a <literal>toJSON()</literal> method,
474 it defines how the object will be serialized.
475 The optional <literal>replacer</literal> parameter is
476 a <literal>function</literal> or <literal>array</literal>
477 that transforms results.
478 The optional <literal>space</literal> parameter is
479 a <literal>string</literal> or <literal>number</literal>.
480 If it is a <literal>number</literal>,
481 it indicates the number of white spaces placed before a result
482 (no more than 10).
483 If it is a <literal>string</literal>,
484 it is used as a white space (or first 10 characters of it).
485 If omitted or is <literal>null</literal>, no white space is used.
486 </tag-desc>
487 </list>
488 </para>
489
490 <para>
491 <example>
492 >> var json = JSON.parse('{"a":1, "b":true}')
493 >> json.a
494 1
495
496 >> JSON.stringify(json)
497 '{"a":1,"b":true}'
498
499 >> JSON.stringify(json, undefined, 1)
500 '{\n "a": 1,\n "b": true\n}'
501
502 >> JSON.stringify({ x: [10, undefined, function(){}] })
503 '{"x":[10,null,null]}'
504
505 >> JSON.stringify({"a":1, "toJSON": function() {return "xxx"}})
506 '"xxx"'
507
508 # Example with function replacer
509
510 >> function replacer(key, value) {return (typeof value === 'string') ? undefined : value}
511 >>JSON.stringify({a:1, b:"b", c:true}, replacer)
512 '{"a":1,"c":true}'
513 </example>
514 </para>
515
516 </section>
517
518
519 <section id="crypto" name="Crypto">
520
521 <para>
522 The Crypto module provides cryptographic functionality support.
523 The Crypto module object is returned by <literal>require('crypto')</literal>.
524 </para>
525
526 <para>
527 <list type="tag">
528
529 <tag-name><literal>crypto.createHash(<value>algorithm</value>)</literal></tag-name>
530 <tag-desc>
531 Creates and returns a <link id="crypto_hash">Hash</link> object
532 that can be used to generate hash digests
533 using the given <value>algorithm</value>.
534 The algorighm can be
535 <literal>md5</literal>,
536 <literal>sha1</literal>, and
537 <literal>sha256</literal>.
538 </tag-desc>
539
540 <tag-name><literal>crypto.createHmac(<value>algorithm</value>,
541 <value>secret key</value>)</literal></tag-name>
542 <tag-desc>
543 Creates and returns an <link id="crypto_hmac">HMAC</link> object
544 that uses the given <value>algorithm</value> and <value>secret key</value>.
545 The algorighm can be
546 <literal>md5</literal>,
547 <literal>sha1</literal>, and
548 <literal>sha256</literal>.
549 </tag-desc>
550
551 </list>
552 </para>
553
554
555 <section id="crypto_hash" name="Hash">
556
557 <para>
558 <list type="tag">
559
560 <tag-name><literal>hash.update(<value>data</value>)</literal></tag-name>
561 <tag-desc>
562 Updates the hash content with the given <value>data</value>.
563 </tag-desc>
564
565 <tag-name><literal>hash.digest([<value>encoding</value>])</literal></tag-name>
566 <tag-desc>
567 Calculates the digest of all of the data passed using
568 <literal>hash.update()</literal>.
569 The encoding can be
570 <literal>hex</literal>,
571 <literal>base64</literal>, and
572 <literal>base64url</literal>.
573 If encoding is not provided, a byte string is returned.
574 </tag-desc>
575
576 </list>
577 </para>
578
579 <para>
580 <example>
581 >> var cr = require('crypto')
582 undefined
583
584 >> cr.createHash('sha1').update('A').update('B').digest('base64url')
585 'BtlFlCqiamG-GMPiK_GbvKjdK10'
586 </example>
587 </para>
588
589 </section>
590
591
592 <section id="crypto_hmac" name="HMAC">
593
594 <para>
595 <list type="tag">
596
597 <tag-name><literal>hmac.update(<value>data</value>)</literal></tag-name>
598 <tag-desc>
599 Updates the HMAC content with the given <value>data</value>.
600 </tag-desc>
601
602 <tag-name><literal>hmac.digest([<value>encoding</value>])</literal></tag-name>
603 <tag-desc>
604 Calculates the HMAC digest of all of the data passed using
605 <literal>hmac.update()</literal>.
606 The encoding can be
607 <literal>hex</literal>,
608 <literal>base64</literal>, and
609 <literal>base64url</literal>.
610 If encoding is not provided, a byte string is returned.
611 </tag-desc>
612 </list>
613 </para>
614
615 <para>
616 <example>
617 >> var cr = require('crypto')
618 undefined
619
620 >> cr.createHmac('sha1', 'secret.key').update('AB').digest('base64url')
621 'Oglm93xn23_MkiaEq_e9u8zk374'
622 </example>
623 </para>
624
625 </section>
626
627 </section>
628
629 <section id="njs_api_timers" name="Timers">
630
631 <para>
632
633 <list type="tag">
634
635 <tag-name id="cleartimeout"><literal>clearTimeout(<value>timeout</value>)</literal></tag-name>
636 <tag-desc>
637 Cancels a <literal>timeout</literal> object
638 created by <link id="settimeout">setTimeout()</link>.
639 </tag-desc>
640
641 <tag-name id="settimeout"><literal>setTimeout(<value>function</value>,
642 <value>ms</value>[, <value>arg1</value>, <value>argN</value>])</literal></tag-name>
643 <tag-desc>
644 Calls a <literal>function</literal>
645 after a specified number of <literal>milliseconds</literal>.
646 One or more optional <literal>arguments</literal>
647 can be passed to the specified function.
648 Returns a <literal>timeout</literal> object.
649 <example>
650 function handler(v)
651 {
652 // ...
653 }
654
655 t = setTimeout(handler, 12);
656
657 // ...
658
659 clearTimeout(t);
660 </example>
661 </tag-desc>
662
663 </list>
664 </para>
665
666 </section>
667
668
669 <section id="njs_api_fs" name="File System">
670
671 <para>
672 The File System module provides operations with files.
673 The module object is returned by <literal>require('fs')</literal>.
674 <list type="tag">
675
676 <tag-name id="appendfilesync"><literal>appendFileSync(<value>filename</value>,
677 <value>data</value>[, <value>options</value>])</literal></tag-name>
678 <tag-desc>
679 Synchronously appends specified <literal>data</literal>
680 to a file with provided <literal>filename</literal>.
681 If the file does not exist, it will be created.
682 The <literal>options</literal> parameter is expected to be
683 an object with the following keys:
684 <list type="tag">
685
686 <tag-name><literal>mode</literal></tag-name>
687 <tag-desc>
688 mode option, by default is <literal>0o666</literal>
689 </tag-desc>
690
691 <tag-name><literal>flag</literal></tag-name>
692 <tag-desc>
693 file system <link id="njs_api_fs_flags">flag</link>,
694 by default is <literal>a</literal>
695 </tag-desc>
696
697 </list>
698 </tag-desc>
699
700 <tag-name id="readfilesync"><literal>readFileSync(<value>filename</value>[,
701 <value>options</value>])</literal></tag-name>
702 <tag-desc>
703 Synchronously returns the contents of the file
704 with provided <literal>filename</literal>.
705 The <literal>options</literal> parameter holds
706 <literal>string</literal> that specifies encoding.
707 If not specified, a <link id="string_tobytes">byte string</link> is returned.
708 If <literal>utf8</literal> encoding is specified, a Unicode string is returned.
709 Otherwise, <literal>options</literal> is expected to be
710 an object with the following keys:
711 <list type="tag">
712
713 <tag-name><literal>encoding</literal></tag-name>
714 <tag-desc>
715 encoding, by default is not specified.
716 The encoding can be <literal>utf8</literal>
717 </tag-desc>
718
719 <tag-name><literal>flag</literal></tag-name>
720 <tag-desc>
721 file system <link id="njs_api_fs_flags">flag</link>,
722 by default is <literal>r</literal>
723 </tag-desc>
724
725 </list>
726 <example>
727 >> var fs = require('fs')
728 undefined
729 >> var file = fs.readFileSync('/file/path.tar.gz')
730 undefined
731 >> var gzipped = /^\x1f\x8b/.test(file); gzipped
732 true
733 </example>
734 </tag-desc>
735
736 <tag-name id="writefilesync"><literal>writeFileSync(<value>filename</value>,
737 <value>data</value>[,
738 <value>options</value>])</literal></tag-name>
739 <tag-desc>
740 Synchronously writes <literal>data</literal> to a file
741 with provided <literal>filename</literal>.
742 If the file does not exist, it will be created,
743 if the file exists, it will be replaced.
744 The <literal>options</literal> parameter is expected to be
745 an object with the following keys:
746 <list type="tag">
747 <tag-name><literal>mode</literal></tag-name>
748 <tag-desc>
749 mode option, by default is <literal>0o666</literal>
750 </tag-desc>
751
752 <tag-name><literal>flag</literal></tag-name>
753 <tag-desc>
754 file system <link id="njs_api_fs_flags">flag</link>,
755 by default is <literal>w</literal>
756 </tag-desc>
757
758 </list>
759 <example>
760 >> var fs = require('fs')
761 undefined
762 >> var file = fs.writeFileSync('hello.txt', 'Hello world')
763 undefined
764 </example>
765 </tag-desc>
766
767 </list>
768 </para>
769
770
771 <section id="njs_api_fs_flags" name="File System Flags">
772
773 <para>
774 The <literal>flag</literal> option can accept the following values:
775
776 <list type= "bullet" compact="no">
777
778 <listitem>
779 <literal>a</literal>&mdash;open a file for appending.
780 The file is created if it does not exist
781 </listitem>
782
783 <listitem>
784 <literal>ax</literal>&mdash;the same as <literal>a</literal>
785 but fails if the file already exists
786 </listitem>
787
788 <listitem>
789 <literal>a+</literal>&mdash;open a file for reading and appending.
790 If the file does not exist, it will be created
791 </listitem>
792
793 <listitem>
794 <literal>ax+</literal>&mdash;the same as <literal>a+</literal>
795 but fails if the file already exists
796 </listitem>
797
798 <listitem>
799 <literal>as</literal>&mdash;open a file for appending in synchronous mode.
800 If the file does not exist, it will be created
801 </listitem>
802
803 <listitem>
804 <literal>as+</literal>&mdash;open a file for reading and appending
805 in synchronous mode.
806 If the file does not exist, it will be created
807 </listitem>
808
809 <listitem>
810 <literal>r</literal>&mdash;open a file for reading.
811 An exception occurs if the file does not exist
812 </listitem>
813
814 <listitem>
815 <literal>r+</literal>&mdash;open a file for reading and writing.
816 An exception occurs if the file does not exist
817 </listitem>
818
819 <listitem>
820 <literal>rs+</literal>&mdash;open a file for reading and writing
821 in synchronous mode.
822 Instructs the operating system to bypass the local file system cache
823 </listitem>
824
825 <listitem>
826 <literal>w</literal>&mdash;open a file for writing.
827 If the file does not exist, it will be created.
828 If the file exists, it will be replaced
829 </listitem>
830
831 <listitem>
832 <literal>wx</literal>&mdash;the same as <literal>w</literal>
833 but fails if the file already exists
834 </listitem>
835
836 <listitem>
837 <literal>w+</literal>&mdash;open a file for reading and writing.
838 If the file does not exist, it will be created.
839 If the file exists, it will be replaced
840 </listitem>
841
842 <listitem>
843 <literal>wx+</literal>&mdash;the same as <literal>w+</literal>
844 but fails if the file already exists
845 </listitem>
846
847 </list>
848 </para>
849
850 </section>
851
852 </section>
853
854 </section>
855
856
857 <section id="http" name="HTTP Request">
858
859 <para>
860 The HTTP request object is available only in the
861 <link doc="../http/ngx_http_js_module.xml">ngx_http_js_module</link> module.
862 All string properties of the object are <link id="string">byte strings</link>.
863
864 <list type="tag">
865
866 <tag-name><literal>r.args{}</literal></tag-name>
867 <tag-desc>
868 request arguments object, read-only
869 </tag-desc>
870
871 <tag-name><literal>r.error(<value>string</value>)</literal></tag-name>
872 <tag-desc>
873 writes a <literal>string</literal> to the error log
874 on the <literal>error</literal> level of logging
875 </tag-desc>
876
877 <tag-name><literal>r.finish()</literal></tag-name>
878 <tag-desc>
879 finishes sending a response to the client
880 </tag-desc>
881
882 <tag-name><literal>r.headersIn{}</literal></tag-name>
883 <tag-desc>
884 incoming headers object, read-only.
885 <para>
886 For example, the <literal>Foo</literal> header
887 can be accessed with the syntax <literal>headersIn.foo</literal>
888 or <literal>headersIn['Foo']</literal>
889 </para>
890 </tag-desc>
891
892 <tag-name><literal>r.headersOut{}</literal></tag-name>
893 <tag-desc>
894 outgoing headers object, writable.
895 <para>
896 For example, the <literal>Foo</literal> header
897 can be accessed with the syntax <literal>headersOut.foo</literal>
898 or <literal>headersOut['Foo']</literal>
899 </para>
900 </tag-desc>
901
902 <tag-name><literal>r.httpVersion</literal></tag-name>
903 <tag-desc>
904 HTTP version, read-only
905 </tag-desc>
906
907 <tag-name><literal>r.log(<value>string</value>)</literal></tag-name>
908 <tag-desc>
909 writes a <literal>string</literal> to the error log
910 on the <literal>info</literal> level of logging
911 </tag-desc>
912
913 <tag-name id="r_internal_redirect"><literal>r.internalRedirect(<value>uri</value>)</literal></tag-name>
914 <tag-desc>
915 performs an internal redirect to the specified <literal>uri</literal>.
916 If the uri starts with the “<literal>@</literal>” prefix,
917 it is considered a named location.
918 </tag-desc>
919
920 <tag-name><literal>r.method</literal></tag-name>
921 <tag-desc>
922 HTTP method, read-only
923 </tag-desc>
924
925 <tag-name><literal>r.parent</literal></tag-name>
926 <tag-desc>
927 references the parent request object
928 </tag-desc>
929
930 <tag-name><literal>r.remoteAddress</literal></tag-name>
931 <tag-desc>
932 client address, read-only
933 </tag-desc>
934
935 <tag-name><literal>r.requestBody</literal></tag-name>
936 <tag-desc>
937 holds the request body, read-only
938 </tag-desc>
939
940 <tag-name><literal>r.responseBody</literal></tag-name>
941 <tag-desc>
942 holds the <link id="subrequest">subrequest</link> response body, read-only.
943 The size of <literal>r.responseBody</literal> is limited by the
944 <link doc="../http/ngx_http_core_module.xml" id="subrequest_output_buffer_size"/>
945 directive.
946 </tag-desc>
947
948 <tag-name><literal>r.return(status[, string])</literal></tag-name>
949 <tag-desc>
950 sends the entire response
951 with the specified <literal>status</literal> to the client
952 <para>
953 It is possible to specify either a redirect URL
954 (for codes 301, 302, 303, 307, and 308)
955 or the response body text (for other codes) as the second argument
956 </para>
957 </tag-desc>
958
959 <tag-name><literal>r.send(<value>string</value>)</literal></tag-name>
960 <tag-desc>
961 sends a part of the response body to the client
962 </tag-desc>
963
964 <tag-name><literal>r.sendHeader()</literal></tag-name>
965 <tag-desc>
966 sends the HTTP headers to the client
967 </tag-desc>
968
969 <tag-name><literal>r.status</literal></tag-name>
970 <tag-desc>
971 status, writable
972 </tag-desc>
973
974 <tag-name><literal>r.variables{}</literal></tag-name>
975 <tag-desc>
976 nginx variables object, read-only
977 </tag-desc>
978
979 <tag-name><literal>r.warn(<value>string</value>)</literal></tag-name>
980 <tag-desc>
981 writes a <literal>string</literal> to the error log
982 on the <literal>warning</literal> level of logging
983 </tag-desc>
984
985 <tag-name><literal>r.uri</literal></tag-name>
986 <tag-desc>
987 current URI, read-only
988 </tag-desc>
989
990 <tag-name id="subrequest"><literal>r.subrequest(<value>uri</value>[,
991 <value>options</value>[, <value>callback</value>]])</literal></tag-name>
992 <tag-desc>
993 creates a subrequest with the given <literal>uri</literal> and
994 <literal>options</literal>, and installs
995 an optional completion <literal>callback</literal>.
996
997 <para>
998 If <literal>options</literal> is a string, then it
999 holds the subrequest arguments string.
1000 Otherwise, <literal>options</literal> is expected to be
1001 an object with the following keys:
1002 <list type="tag">
1003 <tag-name><literal>args</literal></tag-name>
1004 <tag-desc>
1005 arguments string
1006 </tag-desc>
1007 <tag-name><literal>body</literal></tag-name>
1008 <tag-desc>
1009 request body
1010 </tag-desc>
1011
1012 <tag-name><literal>method</literal></tag-name>
1013 <tag-desc>
1014 HTTP method
1015 </tag-desc>
1016
1017 </list>
1018 </para>
1019
1020 <para>
1021 The completion <literal>callback</literal> receives
1022 a subrequest response object with methods and properties
1023 identical to the parent request object.
1024 </para>
1025 </tag-desc>
1026
1027 </list>
1028 </para>
1029
1030 </section>
1031
1032
1033 <section id="stream" name="Stream Session">
1034
1035 <para>
1036 The stream session object is available only in the
1037 <link doc="../stream/ngx_stream_js_module.xml">ngx_stream_js_module</link>
1038 module.
1039 All string properties of the object are <link id="string">byte strings</link>.
1040 </para>
1041
1042 <para>
1043 <note>
1044 Prior to njs <link doc="../njs/changes.xml" id="njs0.2.4">0.2.4</link>,
1045 the stream session object had some properties which are currently
1046 <link id="stream_obsolete">removed</link>.
1047 </note>
1048 </para>
1049
1050 <para>
1051 <list type="tag">
1052
1053 <tag-name id="s_allow"><literal>s.allow()</literal></tag-name>
1054 <tag-desc>
1055 successfully finalizes the phase handler
1056 (<link doc="../njs/changes.xml" id="njs0.2.4">0.2.4</link>)
1057 </tag-desc>
1058
1059 <tag-name id="s_decline"><literal>s.decline()</literal></tag-name>
1060 <tag-desc>
1061 finalizes the phase handler and passes control to the next handler
1062 (<link doc="../njs/changes.xml" id="njs0.2.4">0.2.4</link>)
1063 </tag-desc>
1064
1065 <tag-name id="s_deny"><literal>s.deny()</literal></tag-name>
1066 <tag-desc>
1067 finalizes the phase handler with the access error code
1068 (<link doc="../njs/changes.xml" id="njs0.2.4">0.2.4</link>)
1069 </tag-desc>
1070
1071 <tag-name id="s_done"><literal>s.done</literal>(<value>[code]</value>)</tag-name>
1072 <tag-desc>
1073 successfully finalizes the current phase handler
1074 or finalizes it with the specified numeric code
1075 (<link doc="../njs/changes.xml" id="njs0.2.4">0.2.4</link>).
1076 </tag-desc>
1077
1078 <tag-name><literal>s.error(<value>string</value>)</literal></tag-name>
1079 <tag-desc>
1080 writes a sent <literal>string</literal> to the error log
1081 on the <literal>error</literal> level of logging
1082 </tag-desc>
1083
1084 <tag-name><literal>s.log(<value>string</value>)</literal></tag-name>
1085 <tag-desc>
1086 writes a sent <value>string</value> to the error log
1087 on the <literal>info</literal> level of logging
1088 </tag-desc>
1089
1090 <tag-name id="s_off"><literal>s.off(<value>eventName</value>)</literal></tag-name>
1091 <tag-desc>
1092 unregisters the callback set by the <link id="s_on">s.on()</link> method
1093 (<link doc="../njs/changes.xml" id="njs0.2.4">0.2.4</link>)
1094 </tag-desc>
1095
1096 <tag-name id="s_on"><literal>s.on(<value>event</value>,
1097 <value>callback</value>)</literal></tag-name>
1098 <tag-desc>
1099 registers a <literal>callback</literal> for the specified <literal>event</literal>
1100 (<link doc="../njs/changes.xml" id="njs0.2.4">0.2.4</link>).
1101
1102 <para>
1103 An <literal>event</literal> may be one of the following strings:
1104 <list type="tag">
1105 <tag-name><literal>upload</literal></tag-name>
1106 <tag-desc>
1107 new data from a client
1108 </tag-desc>
1109
1110 <tag-name><literal>download</literal></tag-name>
1111 <tag-desc>
1112 new data to a client
1113 </tag-desc>
1114
1115 </list>
1116 </para>
1117
1118 <para>
1119 The completion callback has the following prototype:
1120 <literal>callback(data, flags)</literal>, where
1121 <literal>data</literal> is string,
1122 <literal>flags</literal> is an object
1123 with the following properties:
1124 <list type="tag">
1125 <tag-name id="s_on_callback_last"><literal>last</literal></tag-name>
1126 <tag-desc>
1127 a boolean value, true if data is a last buffer.
1128 </tag-desc>
1129
1130 </list>
1131 </para>
1132 </tag-desc>
1133
1134 <tag-name><literal>s.remoteAddress</literal></tag-name>
1135 <tag-desc>
1136 client address, read-only
1137 </tag-desc>
1138
1139 <tag-name id="s_send"><literal>s.send(<value>data</value>[,
1140 <value>options</value>])</literal></tag-name>
1141 <tag-desc>
1142 sends the data to the client
1143 (<link doc="../njs/changes.xml" id="njs0.2.4">0.2.4</link>).
1144 The <literal>options</literal> is an object used
1145 to override nginx buffer flags derived from an incoming data chunk buffer.
1146 The flags can be overriden with the following flags:
1147 <para>
1148 <list type="tag">
1149
1150 <tag-name><literal>last</literal></tag-name>
1151 <tag-desc>
1152 boolean, true if the buffer is the last buffer
1153 </tag-desc>
1154
1155 <tag-name><literal>flush</literal></tag-name>
1156 <tag-desc>
1157 boolean, true if the buffer should have the <literal>flush</literal> flag
1158 </tag-desc>
1159 </list>
1160 </para>
1161 The method can be called multiple times per callback invocation.
1162 </tag-desc>
1163
1164 <tag-name><literal>s.variables{}</literal></tag-name>
1165 <tag-desc>
1166 nginx variables object, read-only
1167 </tag-desc>
1168
1169 <tag-name><literal>s.warn(<value>string</value>)</literal></tag-name>
1170 <tag-desc>
1171 writes a sent <literal>string</literal> to the error log
1172 on the <literal>warning</literal> level of logging
1173 </tag-desc>
1174
1175 </list>
1176 </para>
1177
1178
1179 <section id="stream_obsolete" name="Obsolete properties">
1180
1181 <para>
1182 These properties have been removed
1183 in njs <link doc="../njs/changes.xml" id="njs0.2.4">0.2.4</link>
1184 and are not backward compatible with the existing njs code.
1185 </para>
1186
1187 <para>
1188 <list type="tag">
1189
1190 <tag-name id="s_abort"><literal>s.ABORT</literal></tag-name>
1191 <tag-desc>
1192 the <literal>ABORT</literal> return code
1193 <note>
1194 Starting from njs <link doc="../njs/changes.xml" id="njs0.2.4">0.2.4</link>,
1195 the <link id="s_deny">s.deny()</link> method should be used instead.
1196 </note>
1197 </tag-desc>
1198
1199 <tag-name><literal>s.AGAIN</literal></tag-name>
1200 <tag-desc>
1201 the <literal>AGAIN</literal> return code
1202 <note>
1203 Starting from njs <link doc="../njs/changes.xml" id="njs0.2.4">0.2.4</link>,
1204 the corresponding behavior is achieved if no
1205 <link id="s_allow">s.allow()</link>,
1206 <link id="s_deny">s.deny()</link>,
1207 <link id="s_decline">s.decline()</link>,
1208 <link id="s_done">s.done()</link>
1209 is invoked and a callback is registered.
1210 </note>
1211 </tag-desc>
1212
1213 <tag-name id="s_buffer"><literal>s.buffer</literal></tag-name>
1214 <tag-desc>
1215 the current buffer, writable
1216 <note>
1217 Starting from <link doc="../njs/changes.xml" id="njs0.2.4">0.2.4</link>,
1218 the <link id="s_send">s.send()</link> method should be used for writing.
1219 For reading, the current buffer is available as the first argument of the
1220 <literal>event</literal> callback.
1221 </note>
1222 </tag-desc>
1223
1224 <tag-name><literal>s.DECLINED</literal></tag-name>
1225 <tag-desc>
1226 the <literal>DECLINED</literal> return code
1227 <note>
1228 Starting from njs <link doc="../njs/changes.xml" id="njs0.2.4">0.2.4</link>,
1229 the <link id="s_decline">s.decline()</link> method should be used instead.
1230 </note>
1231 </tag-desc>
1232
1233 <tag-name><literal>s.eof</literal></tag-name>
1234 <tag-desc>
1235 a boolean read-only property, true if the current buffer is the last buffer
1236 <note>
1237 Starting from <link doc="../njs/changes.xml" id="njs0.2.4">0.2.4</link>,
1238 the <link id="s_on_callback_last">flags.last</link> property
1239 should be used instead.
1240 </note>
1241 </tag-desc>
1242
1243 <tag-name><literal>s.ERROR</literal></tag-name>
1244 <tag-desc>
1245 the <literal>ERROR</literal> return code
1246 <note>
1247 Starting from njs <link doc="../njs/changes.xml" id="njs0.2.4">0.2.4</link>,
1248 an appropriate exception can be thrown to report an error.
1249 </note>
1250 </tag-desc>
1251
1252 <tag-name><literal>s.fromUpstream</literal></tag-name>
1253 <tag-desc>
1254 a boolean read-only property,
1255 true if the current buffer is from the upstream server to the client
1256 <note>
1257 Starting from <link doc="../njs/changes.xml" id="njs0.2.4">0.2.4</link>,
1258 a corresponding <link id="s_on">event</link>
1259 (<literal>upload</literal> or <literal>download</literal>)
1260 should be used to handle data to or from client.
1261 </note>
1262 </tag-desc>
1263
1264 <tag-name id="s_ok"><literal>s.OK</literal></tag-name>
1265 <tag-desc>
1266 the <literal>OK</literal> return code
1267 <note>
1268 Starting from njs <link doc="../njs/changes.xml" id="njs0.2.4">0.2.4</link>,
1269 the <link id="s_allow">s.allow()</link> method should be used instead.
1270 </note>
1271 </tag-desc>
1272
1273 </list>
1274 </para>
1275
1276 </section>
1277
1278 </section>
1279
1280
1281 </article>