Mercurial > hg > nginx-site
view xml/en/docs/njs/reference.xml @ 2332:9d502d4305ac
Removed obsolete properties and examples from njs.
author | Yaroslav Zhuravlev <yar@nginx.com> |
---|---|
date | Tue, 26 Feb 2019 18:22:40 +0300 |
parents | 5eba0f7b24a9 |
children | 867fe207f13e |
line wrap: on
line source
<?xml version="1.0"?> <!-- Copyright (C) Nginx, Inc. --> <!DOCTYPE article SYSTEM "../../../../dtd/article.dtd"> <article name="Reference" link="/en/docs/njs/reference.html" lang="en" rev="21"> <section id="summary"> <para> <link doc="index.xml">njs</link> provides objects, methods and properties for extending nginx functionality. </para> </section> <section id="http_stream" name="nginx objects"> <section id="http" name="HTTP Request"> <para> The HTTP request object is available only in the <link doc="../http/ngx_http_js_module.xml">ngx_http_js_module</link> module. All string properties of the object are <link id="string">byte strings</link>. <list type="tag"> <tag-name><literal>r.args{}</literal></tag-name> <tag-desc> request arguments object, read-only </tag-desc> <tag-name><literal>r.error(<value>string</value>)</literal></tag-name> <tag-desc> writes a <literal>string</literal> to the error log on the <literal>error</literal> level of logging </tag-desc> <tag-name><literal>r.finish()</literal></tag-name> <tag-desc> finishes sending a response to the client </tag-desc> <tag-name><literal>r.headersIn{}</literal></tag-name> <tag-desc> incoming headers object, read-only. <para> For example, the <literal>Foo</literal> header can be accessed with the syntax <literal>headersIn.foo</literal> or <literal>headersIn['Foo']</literal> </para> </tag-desc> <tag-name id="r_headers_out"><literal>r.headersOut{}</literal></tag-name> <tag-desc> outgoing headers object, writable. <para> For example, the <literal>Foo</literal> header can be accessed with the syntax <literal>headersOut.foo</literal> or <literal>headersOut['Foo']</literal> </para> </tag-desc> <tag-name><literal>r.httpVersion</literal></tag-name> <tag-desc> HTTP version, read-only </tag-desc> <tag-name><literal>r.log(<value>string</value>)</literal></tag-name> <tag-desc> writes a <literal>string</literal> to the error log on the <literal>info</literal> level of logging </tag-desc> <tag-name id="r_internal_redirect"><literal>r.internalRedirect(<value>uri</value>)</literal></tag-name> <tag-desc> performs an internal redirect to the specified <literal>uri</literal>. If the uri starts with the “<literal>@</literal>” prefix, it is considered a named location. </tag-desc> <tag-name><literal>r.method</literal></tag-name> <tag-desc> HTTP method, read-only </tag-desc> <tag-name><literal>r.parent</literal></tag-name> <tag-desc> references the parent request object </tag-desc> <tag-name><literal>r.remoteAddress</literal></tag-name> <tag-desc> client address, read-only </tag-desc> <tag-name id="r_request_body"><literal>r.requestBody</literal></tag-name> <tag-desc> returns the client request body if it has not been written to a temporary file. To ensure that the client request body is in memory, its size should be limited by <link doc="../http/ngx_http_core_module.xml" id="client_max_body_size"/>, and a sufficient buffer size should be set using <link doc="../http/ngx_http_core_module.xml" id="client_body_buffer_size"/>. </tag-desc> <tag-name><literal>r.responseBody</literal></tag-name> <tag-desc> holds the <link id="subrequest">subrequest</link> response body, read-only. The size of <literal>r.responseBody</literal> is limited by the <link doc="../http/ngx_http_core_module.xml" id="subrequest_output_buffer_size"/> directive. </tag-desc> <tag-name><literal>r.return(status[, string])</literal></tag-name> <tag-desc> sends the entire response with the specified <literal>status</literal> to the client <para> It is possible to specify either a redirect URL (for codes 301, 302, 303, 307, and 308) or the response body text (for other codes) as the second argument </para> </tag-desc> <tag-name><literal>r.send(<value>string</value>)</literal></tag-name> <tag-desc> sends a part of the response body to the client </tag-desc> <tag-name><literal>r.sendHeader()</literal></tag-name> <tag-desc> sends the HTTP headers to the client </tag-desc> <tag-name><literal>r.status</literal></tag-name> <tag-desc> status, writable </tag-desc> <tag-name><literal>r.variables{}</literal></tag-name> <tag-desc> nginx variables object, read-only </tag-desc> <tag-name><literal>r.warn(<value>string</value>)</literal></tag-name> <tag-desc> writes a <literal>string</literal> to the error log on the <literal>warning</literal> level of logging </tag-desc> <tag-name><literal>r.uri</literal></tag-name> <tag-desc> current URI, read-only </tag-desc> <tag-name id="subrequest"><literal>r.subrequest(<value>uri</value>[, <value>options</value>[, <value>callback</value>]])</literal></tag-name> <tag-desc> creates a subrequest with the given <literal>uri</literal> and <literal>options</literal>, and installs an optional completion <literal>callback</literal>. <para> If <literal>options</literal> is a string, then it holds the subrequest arguments string. Otherwise, <literal>options</literal> is expected to be an object with the following keys: <list type="tag"> <tag-name><literal>args</literal></tag-name> <tag-desc> arguments string </tag-desc> <tag-name><literal>body</literal></tag-name> <tag-desc> request body </tag-desc> <tag-name><literal>method</literal></tag-name> <tag-desc> HTTP method </tag-desc> </list> </para> <para> The completion <literal>callback</literal> receives a subrequest response object with methods and properties identical to the parent request object. </para> </tag-desc> </list> </para> </section> <section id="stream" name="Stream Session"> <para> The stream session object is available only in the <link doc="../stream/ngx_stream_js_module.xml">ngx_stream_js_module</link> module. All string properties of the object are <link id="string">byte strings</link>. </para> <para> <list type="tag"> <tag-name id="s_allow"><literal>s.allow()</literal></tag-name> <tag-desc> successfully finalizes the phase handler (<link doc="../njs/changes.xml" id="njs0.2.4">0.2.4</link>) </tag-desc> <tag-name id="s_decline"><literal>s.decline()</literal></tag-name> <tag-desc> finalizes the phase handler and passes control to the next handler (<link doc="../njs/changes.xml" id="njs0.2.4">0.2.4</link>) </tag-desc> <tag-name id="s_deny"><literal>s.deny()</literal></tag-name> <tag-desc> finalizes the phase handler with the access error code (<link doc="../njs/changes.xml" id="njs0.2.4">0.2.4</link>) </tag-desc> <tag-name id="s_done"><literal>s.done</literal>(<value>[code]</value>)</tag-name> <tag-desc> successfully finalizes the current phase handler or finalizes it with the specified numeric code (<link doc="../njs/changes.xml" id="njs0.2.4">0.2.4</link>). </tag-desc> <tag-name><literal>s.error(<value>string</value>)</literal></tag-name> <tag-desc> writes a sent <literal>string</literal> to the error log on the <literal>error</literal> level of logging </tag-desc> <tag-name><literal>s.log(<value>string</value>)</literal></tag-name> <tag-desc> writes a sent <value>string</value> to the error log on the <literal>info</literal> level of logging </tag-desc> <tag-name id="s_off"><literal>s.off(<value>eventName</value>)</literal></tag-name> <tag-desc> unregisters the callback set by the <link id="s_on">s.on()</link> method (<link doc="../njs/changes.xml" id="njs0.2.4">0.2.4</link>) </tag-desc> <tag-name id="s_on"><literal>s.on(<value>event</value>, <value>callback</value>)</literal></tag-name> <tag-desc> registers a <literal>callback</literal> for the specified <literal>event</literal> (<link doc="../njs/changes.xml" id="njs0.2.4">0.2.4</link>). <para> An <literal>event</literal> may be one of the following strings: <list type="tag"> <tag-name><literal>upload</literal></tag-name> <tag-desc> new data from a client </tag-desc> <tag-name><literal>download</literal></tag-name> <tag-desc> new data to a client </tag-desc> </list> </para> <para> The completion callback has the following prototype: <literal>callback(data, flags)</literal>, where <literal>data</literal> is string, <literal>flags</literal> is an object with the following properties: <list type="tag"> <tag-name id="s_on_callback_last"><literal>last</literal></tag-name> <tag-desc> a boolean value, true if data is a last buffer. </tag-desc> </list> </para> </tag-desc> <tag-name><literal>s.remoteAddress</literal></tag-name> <tag-desc> client address, read-only </tag-desc> <tag-name id="s_send"><literal>s.send(<value>data</value>[, <value>options</value>])</literal></tag-name> <tag-desc> sends the data to the client (<link doc="../njs/changes.xml" id="njs0.2.4">0.2.4</link>). The <literal>options</literal> is an object used to override nginx buffer flags derived from an incoming data chunk buffer. The flags can be overriden with the following flags: <para> <list type="tag"> <tag-name><literal>last</literal></tag-name> <tag-desc> boolean, true if the buffer is the last buffer </tag-desc> <tag-name><literal>flush</literal></tag-name> <tag-desc> boolean, true if the buffer should have the <literal>flush</literal> flag </tag-desc> </list> </para> The method can be called multiple times per callback invocation. </tag-desc> <tag-name><literal>s.variables{}</literal></tag-name> <tag-desc> nginx variables object, read-only </tag-desc> <tag-name><literal>s.warn(<value>string</value>)</literal></tag-name> <tag-desc> writes a sent <literal>string</literal> to the error log on the <literal>warning</literal> level of logging </tag-desc> </list> </para> </section> </section> <section id="core" name="Core"> <section id="core_object" name="Object"> <para> The <literal>Object</literal> constructor corresponds to a standard JS object. <list type="tag"> <tag-name id="object_entries"><literal>Object.entries(<value>object</value>)</literal></tag-name> <tag-desc> returns an array of all enumerable property <literal>[key, value]</literal> pairs of the given <literal>object</literal> (<link doc="changes.xml" id="njs0.2.7">0.2.7</link>). </tag-desc> <tag-name id="object_values"><literal>Object.values(<value>object</value>)</literal></tag-name> <tag-desc> returns an array of all enumerable property values of the given <literal>object</literal> (<link doc="changes.xml" id="njs0.2.7">0.2.7</link>). </tag-desc> </list> </para> </section> <section id="string" name="String"> <para> There are two types of strings in njs: a Unicode string (default) and a byte string. </para> <para> A Unicode string corresponds to an ECMAScript string which contains Unicode characters. </para> <para> Byte strings contain a sequence of bytes and are used to serialize Unicode strings to external data and deserialize from external sources. For example, the <link id="string_toutf8">toUTF8()</link> method serializes a Unicode string to a byte string using UTF8 encoding: <example> >> '£'.toUTF8().toString('hex') 'c2a3' /* C2 A3 is the UTF8 representation of 00A3 ('£') code point */ </example> The <link id="string_tobytes">toBytes()</link> method serializes a Unicode string with code points up to 255 into a byte string, otherwise, <literal>null</literal> is returned: <example> >> '£'.toBytes().toString('hex') 'a3' /* a3 is a byte equal to 00A3 ('£') code point */ </example> Only byte strings can be converted to different encodings. For example, a string cannot be encoded to <literal>hex</literal> directly: <example> >> 'αβγδ'.toString('base64') TypeError: argument must be a byte string at String.prototype.toString (native) at main (native) </example> To convert a Unicode string to hex, first, it should be converted to a byte string and then to hex: <example> >> 'αβγδ'.toUTF8().toString('base64') 'zrHOss6zzrQ=' </example> <list type="tag"> <tag-name id="string_bytesfrom"><literal>String.bytesFrom(<value>array</value> | <value>string</value>, <value>encoding</value>)</literal></tag-name> <tag-desc> (njs specific) Creates a byte string either from an array that contains octets, or from an encoded string (<link doc="../njs/changes.xml" id="njs0.2.3">0.2.3</link>). The encoding can be <literal>hex</literal>, <literal>base64</literal>, and <literal>base64url</literal>. <example> >> String.bytesFrom([0x62, 0x75, 0x66, 0x66, 0x65, 0x72]) 'buffer' >> String.bytesFrom('YnVmZmVy', 'base64') 'buffer' </example> </tag-desc> <tag-name id="string_fromcharcode"><literal>String.fromCharCode(<value>CharCode1</value>[, ...[, <value>CharCodeN</value>]])</literal></tag-name> <tag-desc> Returns a string from one or more Unicode code points. <example> >> String.fromCharCode(97, 98, 99, 100) 'abcd' </example> </tag-desc> <tag-name id="string_fromcodepoint"><literal>String.fromCodePoint(<value>codePoint1</value>[, ...[, <value>codePoint2</value>]])</literal></tag-name> <tag-desc> Returns a string from one or more Unicode code points. <example> >> String.fromCodePoint(97, 98, 99, 100) 'abcd' </example> </tag-desc> <tag-name id="string_charat"><literal>String.prototype.charAt(<value>index</value>)</literal></tag-name> <tag-desc> Returns a string representing one Unicode code unit at the specified <literal>index</literal>; empty string if index is out of range. The index can be an integer between 0 and 1-less-than the length of the string. If no index is provided, the default is <literal>0</literal>, so the first character in the string is returned. </tag-desc> <tag-name id="string_codepointat"><literal>String.prototype.CodePointAt(<value>position</value>)</literal></tag-name> <tag-desc> Returns a number representing the code point value of the character at the given <literal>position</literal>; <literal>undefined</literal> if there is no element at position. <example> >> 'ABCD'.codePointAt(3); 68 </example> </tag-desc> <tag-name id="string_concat"><literal>String.prototype.concat(<value>string1</value>[, ..., <value>stringN</value>])</literal></tag-name> <tag-desc> Returns a string that contains the concatenation of specified <literal>strings</literal>. <example> >> "a".concat("b", "c") 'abc' </example> </tag-desc> <tag-name id="string_endswith"><literal>String.prototype.endsWith(<value>searchString</value>[, <value>length</value>])</literal></tag-name> <tag-desc> Returns <literal>true</literal> if a string ends with the characters of a specified string, otherwise <literal>false</literal>. The optional <literal>length</literal> parameter is the the length of string. If omitted, the default value is the length of the string. <example> >> 'abc'.endsWith('abc') true >> 'abca'.endsWith('abc') false </example> </tag-desc> <tag-name id="string_frombytes"><literal>String.prototype.fromBytes(<value>start</value>[, <value>end</value>])</literal></tag-name> <tag-desc> (njs specific) Returns a new Unicode string from a byte string where each byte is replaced with a corresponding Unicode code point. </tag-desc> <tag-name id="string_fromutf8"><literal>String.prototype.fromUTF8(<value>start</value>[, <value>end</value>])</literal></tag-name> <tag-desc> (njs specific) Converts a byte string containing a valid UTF8 string into a Unicode string, otherwise <literal>null</literal> is returned. </tag-desc> <tag-name id="string_includes"><literal>String.prototype.includes(<value>searchString</value>[, <value>position</value>]))</literal></tag-name> <tag-desc> Returns <literal>true</literal> if a string is found within another string, otherwise <literal>false</literal>. The optional <literal>position</literal> parameter is the position within the string at which to begin search for <literal>searchString</literal>. Default value is 0. <example> >> 'abc'.includes('bc') true </example> </tag-desc> <tag-name id="string_indexof"><literal>String.prototype.indexOf(<value>searchString</value>[, <value>fromIndex</value>])</literal></tag-name> <tag-desc> Returns the position of the first occurrence of the <literal>searchString</literal>. The search is started at <literal>fromIndex</literal>. Returns <value>-1</value> if the value is not found. The <literal>fromIndex</literal> is an integer, default value is 0. If <literal>fromIndex</literal> is lower than 0 or greater than <link id="string_length">String.prototype.length</link><value></value>, the search starts at index <value>0</value> and <value>String.prototype.length</value>. <example> >> 'abcdef'.indexOf('de', 2) 3 </example> </tag-desc> <tag-name id="string_lastindexof"><literal>String.prototype.lastIndexOf(<value>searchString</value>[, <value>fromIndex</value>])</literal></tag-name> <tag-desc> Returns the position of the last occurrence of the <literal>searchString</literal>, searching backwards from <literal>fromIndex</literal>. Returns <value>-1</value> if the value is not found. If <literal>searchString</literal> is empty, then <literal>fromIndex</literal> is returned. <example> >> "nginx".lastIndexOf("gi") 1 </example> </tag-desc> <tag-name id="string_length"><literal>String.prototype.length</literal></tag-name> <tag-desc> Returns the length of the string. <example> >> 'αβγδ'.length 4 </example> </tag-desc> <tag-name id="string_match"><literal>String.prototype.match([<value>regexp</value>])</literal></tag-name> <tag-desc> Matches a string against a <literal>regexp</literal>. <example> >> 'nginx'.match( /ng/i ) 'ng' </example> </tag-desc> <tag-name id="string_padend"><literal>String.prototype.padEnd(<value>length</value> [, <value>string</value>])</literal></tag-name> <tag-desc> Returns a string of a specified <literal>length</literal> with the pad <literal>string</literal> applied to the end of the specified string (<link doc="../njs/changes.xml" id="njs0.2.3">0.2.3</link>). <example> >> '1234'.padEnd(8, 'abcd') '1234abcd' </example> </tag-desc> <tag-name id="string_padstart"><literal>String.prototype.padStart(<value>length</value> [, <value>string</value>])</literal></tag-name> <tag-desc> Returns a string of a specified <literal>length</literal> with the pad <literal>string</literal> applied to the start of the specified string (<link doc="../njs/changes.xml" id="njs0.2.3">0.2.3</link>). <example> >> '1234'.padStart(8, 'abcd') 'abcd1234' </example> </tag-desc> <tag-name id="string_repeat"><literal>String.prototype.repeat(<value>number</value>)</literal></tag-name> <tag-desc> Returns a string with the specified <literal>number</literal> of copies of the string. <example> >> 'abc'.repeat(3) 'abcabcabc' </example> </tag-desc> <tag-name id="string_replace"><literal>String.prototype.replace([<value>regexp</value>|<value>string</value>[, <value>string</value>|<value>function</value>]])</literal></tag-name> <tag-desc> Returns a new string with matches of a pattern (<literal>string</literal> or a <literal>regexp</literal>) replaced by a <literal>string</literal> or a <literal>function</literal>. <example> >> 'abcdefgh'.replace('d', 1) 'abc1efgh' </example> </tag-desc> <tag-name id="string_search"><literal>String.prototype.search([<value>regexp</value>])</literal></tag-name> <tag-desc> Searches for a string using a <literal>regexp</literal> <example> >> 'abcdefgh'.search('def') 3 </example> </tag-desc> <tag-name id="string_slice"><literal>String.prototype.slice(<value>start</value>[, <value>end</value>])</literal></tag-name> <tag-desc> Returns a new string containing a part of an original string between <literal>start</literal> and <literal>end</literal> or from <literal>start</literal> to the end of the string. <example> >> 'abcdefghijklmno'.slice(NaN, 5) 'abcde' </example> </tag-desc> <tag-name id="string_split"><literal>String.prototype.split(([<value>string</value>|<value>regexp</value>[, <value>limit</value>]]))</literal></tag-name> <tag-desc> Returns match of a string against a <literal>regexp</literal>. The optional <literal>limit</literal> parameter is an integer that specifies a limit on the number of splits to be found. <example> >> 'abc'.split('') [ 'a', 'b', 'c' ] </example> </tag-desc> <tag-name id="string_startswith"><literal>String.prototype.startsWith(<value>searchString</value>[, <value>position</value>])</literal></tag-name> <tag-desc> Returns <literal>true</literal> if a string begins with the characters of a specified string, otherwise <literal>false</literal>. The optional <literal>position</literal> parameter is the position in this string at which to begin search for <literal>searchString</literal>. Default value is 0. <example> >> 'abc'.startsWith('abc') true > 'aabc'.startsWith('abc') false </example> </tag-desc> <tag-name id="string_substr"><literal>String.prototype.substr(<value>start</value>[, <value>length</value>])</literal></tag-name> <tag-desc> Returns the part of the string of the specified <literal>length</literal> from <literal>start</literal> or from <literal>start</literal> to the end of the string. <example> >> 'abcdefghijklmno'.substr(3, 5) 'defgh' </example> </tag-desc> <tag-name id="string_substring"><literal>String.prototype.substring(<value>start</value>[, <value>end</value>])</literal></tag-name> <tag-desc> Returns the part of the string between <literal>start</literal> and <literal>end</literal> or from <literal>start</literal> to the end of the string. <example> >> 'abcdefghijklmno'.substring(3, 5) 'de' </example> </tag-desc> <tag-name id="string_tobytes"><literal>String.prototype.toBytes(start[, end])</literal></tag-name> <tag-desc> (njs specific) Serializes a Unicode string to a byte string. Returns <literal>null</literal> if a character larger than 255 is found in the string. </tag-desc> <tag-name id="string_tolowercase"><literal>String.prototype.toLowerCase()</literal></tag-name> <tag-desc> Converts a string to lower case. The method supports only simple Unicode folding. <example> >> 'ΑΒΓΔ'.toLowerCase() 'αβγδ' </example> </tag-desc> <tag-name><literal>String.prototype.toString([<value>encoding</value>])</literal></tag-name> <tag-desc> <para> If no <literal>encoding</literal> is specified, returns a specified Unicode string or byte string as in ECMAScript. </para> <para> (njs specific) If <literal>encoding</literal> is specified, encodes a <link id="string_tobytes">byte string</link> to <literal>hex</literal>, <literal>base64</literal>, or <literal>base64url</literal>. </para> <example> >> 'αβγδ'.toUTF8().toString('base64url') 'zrHOss6zzrQ' </example> </tag-desc> <tag-name id="string_touppercase"><literal>String.prototype.toUpperCase()</literal></tag-name> <tag-desc> Converts a string to upper case. The method supports only simple Unicode folding. <example> >> 'αβγδ'.toUpperCase() 'ΑΒΓΔ' </example> </tag-desc> <tag-name id="string_toutf8"><literal>String.prototype.toUTF8(<value>start</value>[, <value>end</value>])</literal></tag-name> <tag-desc> (njs specific) Serializes a Unicode string to a byte string using UTF8 encoding. <example> >> 'αβγδ'.toUTF8().length 8 >> 'αβγδ'.length 4 </example> </tag-desc> <tag-name id="string_trim"><literal>String.prototype.trim()</literal></tag-name> <tag-desc> Removes whitespaces from both ends of a string. <example> >> ' abc '.trim() 'abc' </example> </tag-desc> <tag-name id="encodeuri"><literal>encodeURI(<value>URI</value>)</literal></tag-name> <tag-desc> Encodes a URI by replacing each instance of certain characters by one, two, three, or four escape sequences representing the UTF-8 encoding of the character <example> >> encodeURI('012αβγδ') '012%CE%B1%CE%B2%CE%B3%CE%B4' </example> </tag-desc> <tag-name id="encodeuricomponent"><literal>encodeURIComponent(<value>encodedURIString</value>)</literal></tag-name> <tag-desc> Encodes a URI by replacing each instance of certain characters by one, two, three, or four escape sequences representing the UTF-8 encoding of the character. <example> >> encodeURIComponent('[@?=') '%5B%40%3F%3D' </example> </tag-desc> <tag-name id="decodeuri"><literal>decodeURI(<value>encodedURI</value>)</literal></tag-name> <tag-desc> Decodes a previously <link id="encodeuri">encoded</link> URI. <example> >> decodeURI('012%CE%B1%CE%B2%CE%B3%CE%B4') '012αβγδ' </example> </tag-desc> <tag-name id="decodeuricomponent"><literal>decodeURIComponent(<value>decodedURIString</value>)</literal></tag-name> <tag-desc> Decodes an encoded component of a previously encoded URI. <example> >> decodeURIComponent('%5B%40%3F%3D') '[@?=' </example> </tag-desc> </list> </para> </section> <section id="core_json" name="JSON"> <para> The <literal>JSON</literal> object (ES 5.1) provides functions to convert njs values to and from JSON format. <list type="tag"> <tag-name><literal>JSON.parse(<value>string</value>[, <value>reviver</value>])</literal></tag-name> <tag-desc> Converts a <literal>string</literal> that represents JSON data into an njs object (<literal>{...}</literal>) or array (<literal>[...]</literal>). The optional <literal>reviver</literal> parameter is a function (key, value) that will be called for each (key,value) pair and can transform the value. </tag-desc> <tag-name><literal>JSON.stringify(<value>value</value>[, <value>replacer</value>] [, <value>space</value>])</literal></tag-name> <tag-desc> Converts an njs object back to JSON. The obligatory <literal>value</literal> parameter is generally a JSON <literal>object</literal> or <literal>array</literal> that will be converted. If the value has a <literal>toJSON()</literal> method, it defines how the object will be serialized. The optional <literal>replacer</literal> parameter is a <literal>function</literal> or <literal>array</literal> that transforms results. The optional <literal>space</literal> parameter is a <literal>string</literal> or <literal>number</literal>. If it is a <literal>number</literal>, it indicates the number of white spaces placed before a result (no more than 10). If it is a <literal>string</literal>, it is used as a white space (or first 10 characters of it). If omitted or is <literal>null</literal>, no white space is used. </tag-desc> </list> </para> <para> <example> >> var json = JSON.parse('{"a":1, "b":true}') >> json.a 1 >> JSON.stringify(json) '{"a":1,"b":true}' >> JSON.stringify({ x: [10, undefined, function(){}] }) '{"x":[10,null,null]}' >> JSON.stringify({"a":1, "toJSON": function() {return "xxx"}}) '"xxx"' # Example with function replacer >> function replacer(key, value) {return (typeof value === 'string') ? undefined : value} >>JSON.stringify({a:1, b:"b", c:true}, replacer) '{"a":1,"c":true}' </example> </para> </section> <section id="crypto" name="Crypto"> <para> The Crypto module provides cryptographic functionality support. The Crypto module object is returned by <literal>require('crypto')</literal>. </para> <para> <list type="tag"> <tag-name id="crypto_createhash"><literal>crypto.createHash(<value>algorithm</value>)</literal></tag-name> <tag-desc> Creates and returns a <link id="crypto_hash">Hash</link> object that can be used to generate hash digests using the given <value>algorithm</value>. The algorighm can be <literal>md5</literal>, <literal>sha1</literal>, and <literal>sha256</literal>. </tag-desc> <tag-name id="crypto_createhmac"><literal>crypto.createHmac(<value>algorithm</value>, <value>secret key</value>)</literal></tag-name> <tag-desc> Creates and returns an <link id="crypto_hmac">HMAC</link> object that uses the given <value>algorithm</value> and <value>secret key</value>. The algorighm can be <literal>md5</literal>, <literal>sha1</literal>, and <literal>sha256</literal>. </tag-desc> </list> </para> <section id="crypto_hash" name="Hash"> <para> <list type="tag"> <tag-name><literal>hash.update(<value>data</value>)</literal></tag-name> <tag-desc> Updates the hash content with the given <value>data</value>. </tag-desc> <tag-name><literal>hash.digest([<value>encoding</value>])</literal></tag-name> <tag-desc> Calculates the digest of all of the data passed using <literal>hash.update()</literal>. The encoding can be <literal>hex</literal>, <literal>base64</literal>, and <literal>base64url</literal>. If encoding is not provided, a byte string is returned. </tag-desc> </list> </para> <para> <example> >> var cr = require('crypto') undefined >> cr.createHash('sha1').update('A').update('B').digest('base64url') 'BtlFlCqiamG-GMPiK_GbvKjdK10' </example> </para> </section> <section id="crypto_hmac" name="HMAC"> <para> <list type="tag"> <tag-name><literal>hmac.update(<value>data</value>)</literal></tag-name> <tag-desc> Updates the HMAC content with the given <value>data</value>. </tag-desc> <tag-name><literal>hmac.digest([<value>encoding</value>])</literal></tag-name> <tag-desc> Calculates the HMAC digest of all of the data passed using <literal>hmac.update()</literal>. The encoding can be <literal>hex</literal>, <literal>base64</literal>, and <literal>base64url</literal>. If encoding is not provided, a byte string is returned. </tag-desc> </list> </para> <para> <example> >> var cr = require('crypto') undefined >> cr.createHmac('sha1', 'secret.key').update('AB').digest('base64url') 'Oglm93xn23_MkiaEq_e9u8zk374' </example> </para> </section> </section> <section id="njs_api_timers" name="Timers"> <para> <list type="tag"> <tag-name id="cleartimeout"><literal>clearTimeout(<value>timeout</value>)</literal></tag-name> <tag-desc> Cancels a <literal>timeout</literal> object created by <link id="settimeout">setTimeout()</link>. </tag-desc> <tag-name id="settimeout"><literal>setTimeout(<value>function</value>, <value>ms</value>[, <value>arg1</value>, <value>argN</value>])</literal></tag-name> <tag-desc> Calls a <literal>function</literal> after a specified number of <literal>milliseconds</literal>. One or more optional <literal>arguments</literal> can be passed to the specified function. Returns a <literal>timeout</literal> object. <example> function handler(v) { // ... } t = setTimeout(handler, 12); // ... clearTimeout(t); </example> </tag-desc> </list> </para> </section> <section id="njs_api_fs" name="File System"> <para> The File System module provides operations with files. The module object is returned by <literal>require('fs')</literal>. <list type="tag"> <tag-name id="appendfilesync"><literal>appendFileSync(<value>filename</value>, <value>data</value>[, <value>options</value>])</literal></tag-name> <tag-desc> Synchronously appends specified <literal>data</literal> to a file with provided <literal>filename</literal>. If the file does not exist, it will be created. The <literal>options</literal> parameter is expected to be an object with the following keys: <list type="tag"> <tag-name><literal>mode</literal></tag-name> <tag-desc> mode option, by default is <literal>0o666</literal> </tag-desc> <tag-name><literal>flag</literal></tag-name> <tag-desc> file system <link id="njs_api_fs_flags">flag</link>, by default is <literal>a</literal> </tag-desc> </list> </tag-desc> <tag-name id="readfilesync"><literal>readFileSync(<value>filename</value>[, <value>options</value>])</literal></tag-name> <tag-desc> Synchronously returns the contents of the file with provided <literal>filename</literal>. The <literal>options</literal> parameter holds <literal>string</literal> that specifies encoding. If not specified, a <link id="string_tobytes">byte string</link> is returned. If <literal>utf8</literal> encoding is specified, a Unicode string is returned. Otherwise, <literal>options</literal> is expected to be an object with the following keys: <list type="tag"> <tag-name><literal>encoding</literal></tag-name> <tag-desc> encoding, by default is not specified. The encoding can be <literal>utf8</literal> </tag-desc> <tag-name><literal>flag</literal></tag-name> <tag-desc> file system <link id="njs_api_fs_flags">flag</link>, by default is <literal>r</literal> </tag-desc> </list> <example> >> var fs = require('fs') undefined >> var file = fs.readFileSync('/file/path.tar.gz') undefined >> var gzipped = /^\x1f\x8b/.test(file); gzipped true </example> </tag-desc> <tag-name id="writefilesync"><literal>writeFileSync(<value>filename</value>, <value>data</value>[, <value>options</value>])</literal></tag-name> <tag-desc> Synchronously writes <literal>data</literal> to a file with provided <literal>filename</literal>. If the file does not exist, it will be created, if the file exists, it will be replaced. The <literal>options</literal> parameter is expected to be an object with the following keys: <list type="tag"> <tag-name><literal>mode</literal></tag-name> <tag-desc> mode option, by default is <literal>0o666</literal> </tag-desc> <tag-name><literal>flag</literal></tag-name> <tag-desc> file system <link id="njs_api_fs_flags">flag</link>, by default is <literal>w</literal> </tag-desc> </list> <example> >> var fs = require('fs') undefined >> var file = fs.writeFileSync('hello.txt', 'Hello world') undefined </example> </tag-desc> </list> </para> <section id="njs_api_fs_flags" name="File System Flags"> <para> The <literal>flag</literal> option can accept the following values: <list type= "bullet" compact="no"> <listitem> <literal>a</literal>—open a file for appending. The file is created if it does not exist </listitem> <listitem> <literal>ax</literal>—the same as <literal>a</literal> but fails if the file already exists </listitem> <listitem> <literal>a+</literal>—open a file for reading and appending. If the file does not exist, it will be created </listitem> <listitem> <literal>ax+</literal>—the same as <literal>a+</literal> but fails if the file already exists </listitem> <listitem> <literal>as</literal>—open a file for appending in synchronous mode. If the file does not exist, it will be created </listitem> <listitem> <literal>as+</literal>—open a file for reading and appending in synchronous mode. If the file does not exist, it will be created </listitem> <listitem> <literal>r</literal>—open a file for reading. An exception occurs if the file does not exist </listitem> <listitem> <literal>r+</literal>—open a file for reading and writing. An exception occurs if the file does not exist </listitem> <listitem> <literal>rs+</literal>—open a file for reading and writing in synchronous mode. Instructs the operating system to bypass the local file system cache </listitem> <listitem> <literal>w</literal>—open a file for writing. If the file does not exist, it will be created. If the file exists, it will be replaced </listitem> <listitem> <literal>wx</literal>—the same as <literal>w</literal> but fails if the file already exists </listitem> <listitem> <literal>w+</literal>—open a file for reading and writing. If the file does not exist, it will be created. If the file exists, it will be replaced </listitem> <listitem> <literal>wx+</literal>—the same as <literal>w+</literal> but fails if the file already exists </listitem> </list> </para> </section> </section> </section> </article>