Mercurial > hg > nginx-site
view xml/en/docs/njs/reference.xml @ 2605:0ec1e1b063d7
Deprecated String.bytesFrom method in njs.
author | Yaroslav Zhuravlev <yar@nginx.com> |
---|---|
date | Wed, 30 Sep 2020 22:12:09 +0100 |
parents | a29676472a11 |
children | 99bd2ae8d2be |
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="55"> <section id="summary"> <para> <link doc="index.xml">njs</link> provides objects, methods and properties for extending nginx functionality. </para> <para> This reference contains only njs specific properties, methods and modules not compliant with ECMAScript. Definitions of njs properties and methods compliant with ECMAScript can be found in <link url="http://www.ecma-international.org/ecma-262/">ECMAScript specification</link>. List of all njs properties and methods can be found in <link doc="compatibility.xml">Compatibility</link>. </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 id="r_args"><literal>r.args{}</literal></tag-name> <tag-desc> request arguments object, read-only </tag-desc> <tag-name id="r_error"><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 id="r_finish"><literal>r.finish()</literal></tag-name> <tag-desc> finishes sending a response to the client </tag-desc> <tag-name id="r_headers_in"><literal>r.headersIn{}</literal></tag-name> <tag-desc> incoming headers object, read-only. <para> The <literal>Foo</literal> request header can be accessed with the syntax: <literal>headersIn.foo</literal> or <literal>headersIn['Foo']</literal>. </para> <para> The <header>Authorization</header>, <header>Content-Length</header>, <header>Content-Range</header>, <header>Content-Type</header>, <header>ETag</header>, <header>Expect</header>, <header>From</header>, <header>Host</header>, <header>If-Match</header>, <header>If-Modified-Since</header>, <header>If-None-Match</header>, <header>If-Range</header>, <header>If-Unmodified-Since</header>, <header>Max-Forwards</header>, <header>Proxy-Authorization</header>, <header>Referer</header>, <header>Transfer-Encoding</header>, and <header>User-Agent</header> request headers can have only one field value (<link doc="changes.xml" id="njs0.4.1">0.4.1</link>). Duplicate field values in <header>Cookie</header> headers are separated by semicolon (<literal>;</literal>). Duplicate field values in all other request headers are separated by commas. </para> </tag-desc> <tag-name id="r_headers_out"><literal>r.headersOut{}</literal></tag-name> <tag-desc> outgoing headers object, writable. <para> The <header>Foo</header> response header can be accessed with the syntax: <literal>headersOut.foo</literal> or <literal>headersOut['Foo']</literal>. </para> <para> Field values of multi-value response headers (<link doc="changes.xml" id="njs0.4.0">0.4.0</link>) can be set with the syntax: <example> r.headersOut['Foo'] = ['a', 'b'] </example> where the output will be: <example> Foo: a Foo: b </example> All previous field values of the <header>Foo</header> response header will be deleted. </para> <para> For standard response headers that accept only a single field value such as <header>Content-Type</header>, only the last element of the array will take effect. Field values of the <header>Set-Cookie</header> response header are always returned as an array. Duplicate field values in <header>Age</header>, <header>Content-Encoding</header>, <header>Content-Length</header>, <header>Content-Type</header>, <header>ETag</header>, <header>Expires</header>, <header>Last-Modified</header>, <header>Location</header>, <header>Retry-After</header> response headers are ignored. Duplicate field values in all other response headers are separated by commas. </para> </tag-desc> <tag-name id="r_http_version"><literal>r.httpVersion</literal></tag-name> <tag-desc> HTTP version, read-only </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. The actual redirect happens after the handler execution is completed. </tag-desc> <tag-name id="r_log"><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_method"><literal>r.method</literal></tag-name> <tag-desc> HTTP method, read-only </tag-desc> <tag-name id="r_parent"><literal>r.parent</literal></tag-name> <tag-desc> references the parent request object </tag-desc> <tag-name id="r_remote_address"><literal>r.remoteAddress</literal></tag-name> <tag-desc> client address, read-only </tag-desc> <tag-name id="r_raw_headers_in"><literal>r.rawHeadersIn{}</literal></tag-name> <tag-desc> returns an array of key-value pairs exactly as they were received from the client (<link doc="changes.xml" id="njs0.4.1">0.4.1</link>). <para> For example, with the following request headers: <example> Host: localhost Foo: bar foo: bar2 </example> the output of <literal>r.rawHeadersIn</literal> will be: <example> [ ['Host', 'localhost'], ['Foo', 'bar'], ['foo', 'bar2'] ] </example> All <literal>foo</literal> headers can be collected with the syntax: <example> r.rawHeadersIn.filter(v=>v[0].toLowerCase() == 'foo').map(v=>v[1]) </example> the output will be: <example> ['bar', 'bar2'] </example> Header field names are not converted to lower case, duplicate field values are not merged. </para> </tag-desc> <tag-name id="r_raw_headers_out"><literal>r.rawHeadersOut{}</literal></tag-name> <tag-desc> returns an array of key-value pairs of response headers (<link doc="changes.xml" id="njs0.4.1">0.4.1</link>). Header field names are not converted to lower case, duplicate field values are not merged. </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"/>. The property is available only in the <link doc="../http/ngx_http_js_module.xml" id="js_content"/> directive. </tag-desc> <tag-name id="r_response_body"><literal>r.responseBody</literal></tag-name> <tag-desc> holds the <link id="r_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 id="r_return"><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 id="r_send"><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 id="r_send_header"><literal>r.sendHeader()</literal></tag-name> <tag-desc> sends the HTTP headers to the client </tag-desc> <tag-name id="r_status"><literal>r.status</literal></tag-name> <tag-desc> status, writable </tag-desc> <tag-name id="r_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> A <link doc="../dev/development_guide.xml.xml" id="http_subrequests">subrequest</link> shares its input headers with the client request. To send headers different from original headers to a proxied server, the <link doc="../http/ngx_http_proxy_module.xml" id="proxy_set_header"/> directive can be used. To send a completely new set of headers to a proxied server, the <link doc="../http/ngx_http_proxy_module.xml" id="proxy_pass_request_headers"/> directive can be used. </para> <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, by default an empty string is used </tag-desc> <tag-name><literal>body</literal></tag-name> <tag-desc> request body, by default the request body of the parent request object is used </tag-desc> <tag-name><literal>method</literal></tag-name> <tag-desc> HTTP method, by default the <literal>GET</literal> method is used </tag-desc> <tag-name><literal>detached</literal></tag-name> <tag-desc> boolean flag (<link doc="changes.xml" id="njs0.3.9">0.3.9</link>), if <literal>true</literal>, the created subrequest is a detached subrequest. Responses to detached subrequests are ignored. Unlike ordinary subrequests, a detached subrequest can be created inside a variable handler. The <literal>detached</literal> flag and callback argument are mutually exclusive. </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> <para> Since <link doc="changes.xml" id="njs0.3.8">0.3.8</link>, if a <literal>callback</literal> is not provided, the <literal>Promise</literal> object that resolves to the subrequest response object is returned. </para> </tag-desc> <tag-name id="r_uri"><literal>r.uri</literal></tag-name> <tag-desc> current <link doc="../http/ngx_http_core_module.xml" id="var_uri">URI</link> in request, <link doc="../http/ngx_http_core_module.xml" id="location">normalized</link>, read-only </tag-desc> <tag-name id="r_variables"><literal>r.variables{}</literal></tag-name> <tag-desc> nginx variables object, writable (since <link doc="changes.xml" id="njs0.2.8">0.2.8</link>) </tag-desc> <tag-name id="r_warn"><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> </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="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="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="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="changes.xml" id="njs0.2.4">0.2.4</link>). </tag-desc> <tag-name id="s_error"><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 id="s_log"><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="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="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 id="s_remote_address"><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="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 overridden 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 id="s_variables"><literal>s.variables{}</literal></tag-name> <tag-desc> nginx variables object, writable (since <link doc="changes.xml" id="njs0.2.8">0.2.8</link>) </tag-desc> <tag-name id="s_warn"><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_global" name="Global"> <section id="process" name="Process"> <para> The <literal>process</literal> object is a global object that provides information about the current process (<link doc="changes.xml" id="njs0.3.3">0.3.3</link>). </para> <para> <list type="tag"> <tag-name id="process_argv"><literal>process.argv</literal></tag-name> <tag-desc> Returns an array that contains the command line arguments passed when the current process was launched. </tag-desc> <tag-name id="process_env"><literal>process.env</literal></tag-name> <tag-desc> Returns an object containing the user environment. <note> By default, nginx removes all environment variables inherited from its parent process except the TZ variable. Use the <link doc="../ngx_core_module.xml" id="env"/> directive to preserve some of the inherited variables. </note> </tag-desc> <tag-name id="process_pid"><literal>process.pid</literal></tag-name> <tag-desc> Returns the PID of the current process. </tag-desc> <tag-name id="process_ppid"><literal>process.ppid</literal></tag-name> <tag-desc> Returns the PID of the current parent process. </tag-desc> </list> </para> </section> </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> <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> Creates a byte string either from an array that contains octets, or from an encoded string (<link doc="changes.xml" id="njs0.2.3">0.2.3</link>). The encoding can be <literal>hex</literal>, <literal>base64</literal>, and <literal>base64url</literal>. The method is deprecated since <link doc="changes.xml" id="njs0.4.4">0.4.4</link>, the <literal>Buffer.from</literal> method should be used instead: <example> >> Buffer.from([0x62, 0x75, 0x66, 0x66, 0x65, 0x72]).toString() 'buffer' >> Buffer.from('YnVmZmVy', 'base64').toString() 'buffer' </example> </tag-desc> <tag-name id="string_frombytes"><literal>String.prototype.fromBytes(<value>start</value>[, <value>end</value>])</literal></tag-name> <tag-desc> 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> 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_tobytes"><literal>String.prototype.toBytes(<value>start</value>[, <value>end</value>])</literal></tag-name> <tag-desc> 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_tostring"><literal>String.prototype.toString(<value>encoding</value>)</literal></tag-name> <tag-desc> <para> Encodes a string to <literal>hex</literal>, <literal>base64</literal>, or <literal>base64url</literal>: <example> >> 'αβγδ'.toString('base64url') 'zrHOss6zzrQ' </example> Before version <link doc="changes.xml" id="njs0.4.3">0.4.3</link>, only a <link id="string_tobytes">byte string</link> could be encoded: <example> >> 'αβγδ'.toUTF8().toString('base64url') 'zrHOss6zzrQ' </example> </para> </tag-desc> <tag-name id="string_toutf8"><literal>String.prototype.toUTF8(<value>start</value>[, <value>end</value>])</literal></tag-name> <tag-desc> Serializes a Unicode string to a byte string using UTF8 encoding. <example> >> 'αβγδ'.toUTF8().length 8 >> 'αβγδ'.length 4 </example> </tag-desc> </list> </para> </section> <section id="textdecoder" name="Text Decoder"> <para> The <literal>TextDecoder</literal> produces a stream of code points from a stream of bytes (<link doc="changes.xml" id="njs0.4.3">0.4.3</link>). </para> <para> <list type="tag"> <tag-name><literal>TextDecoder([[<value>encoding</value>], <value>options</value>])</literal></tag-name> <tag-desc> Creates a new <literal>TextDecoder</literal> object for specified <literal>encoding</literal>, currently, only UTF-8 is supported. The <literal>options</literal> is <literal>TextDecoderOptions</literal> dictionary with the property: <list type="tag"> <tag-name><literal>fatal</literal></tag-name> <tag-desc> boolean flag indicating if <link id="textdecoder_decode"><literal>TextDecoder.decode()</literal></link> must throw the <value>TypeError</value> exception when a coding error is found, by default is <literal>false</literal>. </tag-desc> </list> </tag-desc> <tag-name id="textdecoder_encoding"><literal>TextDecoder.prototype.encoding</literal></tag-name> <tag-desc> Returns a string with the name of the encoding used by <link id="textdecoder"><literal>TextDecoder()</literal></link>, read-only. </tag-desc> <tag-name id="textdecoder_fatal"><literal>TextDecoder.prototype.fatal</literal></tag-name> <tag-desc> boolean flag, <literal>true</literal> if the error mode is fatal, read-only. </tag-desc> <tag-name id="textdecoder_ignorebom"><literal>TextDecoder.prototype.ignoreBOM</literal></tag-name> <tag-desc> boolean flag, <literal>true</literal> if the byte order marker is ignored, read-only. </tag-desc> <tag-name id="textdecoder_decode"><literal>TextDecoder.prototype.decode(<value>buffer</value>, [<value>options</value>])</literal></tag-name> <tag-desc> Returns a string with the text decoded from the <literal>buffer</literal> by <link id="textdecoder"><literal>TextDecoder()</literal></link>. The buffer can be <literal>ArrayBuffer</literal>. The <literal>options</literal> is <literal>TextDecodeOptions</literal> dictionary with the property: <list type="tag"> <tag-name><literal>stream</literal></tag-name> <tag-desc> boolean flag indicating if additional data will follow in subsequent calls to <literal>decode()</literal>: <literal>true</literal> if processing the data in chunks, and <literal>false</literal> for the final chunk or if the data is not chunked. By default is <literal>false</literal>. </tag-desc> </list> <example> >> (new TextDecoder()).decode(new Uint8Array([206,177,206,178])) αβ </example> </tag-desc> </list> </para> </section> <section id="textencoder" name="Text Encoder"> <para> The <literal>TextEncoder</literal> object produces a byte stream with UTF-8 encoding from a stream of code points (<link doc="changes.xml" id="njs0.4.3">0.4.3</link>). </para> <para> <list type="tag"> <tag-name><literal>TextEncoder()</literal></tag-name> <tag-desc> Returns a newly constructed <literal>TextEncoder</literal> that will generate a byte stream with UTF-8 encoding. </tag-desc> <tag-name id="textencoder_encoding"><literal>TextEncoder.prototype.encoding</literal></tag-name> <tag-desc> Returns “<value>utf-8</value>”, read-only. </tag-desc> <tag-name id="textencoder_encode"><literal>TextEncoder.prototype.encode(<value>string</value>)</literal></tag-name> <tag-desc> Encodes <literal>string</literal> into a <literal>Uint8Array</literal> with UTF-8 encoded text. </tag-desc> <tag-name id="textencoder_encodeinto"><literal>TextEncoder.prototype.encodeInto(<value>string</value>, <value>uint8Array</value>)</literal></tag-name> <tag-desc> Encodes a <literal>string</literal> to UTF-8, puts the result into destination <literal>Uint8Array</literal>, and returns a dictionary object that shows the progress of the encoding. The dictionary object contains two members: <list type="tag"> <tag-name><literal>read</literal></tag-name> <tag-desc> the number of UTF-16 units of code from the source <literal>string</literal> converted to UTF-8 </tag-desc> <tag-name><literal>written</literal></tag-name> <tag-desc> the number of bytes modified in the destination <literal>Uint8Array</literal> </tag-desc> </list> </tag-desc> </list> </para> </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"><literal>setTimeout()</literal></link>. </tag-desc> <tag-name id="settimeout"><literal>setTimeout(<value>function</value>, <value>milliseconds</value>[, <value>argument1</value>, <value>argumentN</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> <section id="built-in" name="Built-in Modules"> <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 algorithm 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 algorithm 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 id="crypto_hash_update"><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 id="crypto_hash_digest"><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 Buffer object (<link doc="changes.xml" id="njs0.4.4">0.4.4</link>) is returned. <note> Before version (<link doc="changes.xml" id="njs0.4.4">0.4.4</link>), a byte string was returned instead of a Buffer object. </note> </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 id="crypto_hmac_update"><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 id="crypto_hmac_digest"><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 Buffer object (<link doc="changes.xml" id="njs0.4.4">0.4.4</link>) is returned. <note> Before version <link doc="changes.xml" id="njs0.4.4">0.4.4</link>, a byte string was returned instead of a Buffer object. </note> </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_fs" name="File System"> <para> The File System module provides operations with files. </para> <para> The module object is returned by <literal>require('fs')</literal>. Since <link doc="changes.xml" id="njs0.3.9">0.3.9</link>, promissified versions of file system methods are available through <literal>require('fs').promises</literal> object: <example> > var fs = require('fs').promises; undefined > fs.readFile("/file/path").then((data)=>console.log(data)) <file data> </example> <list type="tag"> <tag-name id="fs_accesssync"><literal>accessSync(<value>path</value>[, <value>mode</value>])</literal></tag-name> <tag-desc> Synchronously tests permissions for a file or directory specified in the <literal>path</literal> (<link doc="changes.xml" id="njs0.3.9">0.3.9</link>). If the check fails, an error will be returned, otherwise, the method will return undefined. <list type="tag"> <tag-name><literal>mode</literal></tag-name> <tag-desc> by default is <link id="access_const"><literal>fs.constants.F_OK</literal></link>. The mode argument is an optional integer that specifies the accessibility checks to be performed. <example> try { fs.accessSync('/file/path', fs.constants.R_OK | fs.constants.W_OK); console.log('has access'); } catch (e) { console.log('no access');) } </example> </tag-desc> </list> </tag-desc> <tag-name id="fs_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>. The <literal>data</literal> is expected to be a string or a Buffer object (<link doc="changes.xml" id="njs0.4.4">0.4.4</link>). 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="fs_mkdirsync"><literal>mkdirSync(<value>path</value>[, <value>options</value>])</literal></tag-name> <tag-desc> Synchronously creates a directory at the specified <literal>path</literal> (<link doc="changes.xml" id="njs0.4.2">0.4.2</link>). The <literal>options</literal> parameter is expected to be an <literal>integer</literal> that specifies the <link id="fs_mkdirsync_mode">mode</link>, or an object with the following keys: <list type="tag"> <tag-name id="fs_mkdirsync_mode"><literal>mode</literal></tag-name> <tag-desc> mode option, by default is <literal>0o777</literal>. </tag-desc> </list> </tag-desc> <tag-name id="fs_readdirsync"><literal>readdirSync(<value>path</value>[, <value>options</value>])</literal></tag-name> <tag-desc> Synchronously reads the contents of a directory at the specified <literal>path</literal> (<link doc="changes.xml" id="njs0.4.2">0.4.2</link>). The <literal>options</literal> parameter is expected to be a string that specifies <link id="fs_readdirsync_encoding">encoding</link> or an object with the following keys: <list type="tag"> <tag-name id="fs_readdirsync_encoding"><literal>encoding</literal></tag-name> <tag-desc> encoding, by default is <literal>utf8</literal>. The encoding can be <literal>utf8</literal> and <literal>buffer</literal> (<link doc="changes.xml" id="njs0.4.4">0.4.4</link>). </tag-desc> <tag-name id="fs_readdirsync_withfiletypes"><literal>withFileTypes</literal></tag-name> <tag-desc> if set to <literal>true</literal>, the files array will contain <link id="fs_dirent"><literal>fs.Dirent</literal></link> objects, by default is <literal>false</literal>. </tag-desc> </list> </tag-desc> <tag-name id="fs_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 an encoding is specified, a string is returned, otherwise, a Buffer object (<link doc="changes.xml" id="njs0.4.4">0.4.4</link>) is returned. <note> Before version <link doc="changes.xml" id="njs0.4.4">0.4.4</link>, a <link id="string_tobytes">byte string</link> was returned if encoding was not specified. </note> 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>, <literal>hex</literal> (<link doc="changes.xml" id="njs0.4.4">0.4.4</link>), <literal>base64</literal> (<link doc="changes.xml" id="njs0.4.4">0.4.4</link>), <literal>base64url</literal> (<link doc="changes.xml" id="njs0.4.4">0.4.4</link>). </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 = file.slice(0,2).toString('hex') === '1f8b'; gzipped true </example> </tag-desc> <tag-name id="fs_realpathsync"><literal>realpathSync(<value>path</value>[, <value>options</value>])</literal></tag-name> <tag-desc> Synchronously computes the canonical pathname by resolving <literal>.</literal>, <literal>..</literal> and symbolic links using <link url="http://man7.org/linux/man-pages/man3/realpath.3.html">realpath(3)</link>. The <literal>options</literal> argument can be a string specifying an encoding, or an object with an encoding property specifying the character encoding to use for the path passed to the callback (<link doc="changes.xml" id="njs0.3.9">0.3.9</link>). </tag-desc> <tag-name id="fs_renamesync"><literal>renameSync(<value>oldPath</value>, <value>newPath</value>)</literal></tag-name> <tag-desc> Synchronously changes the name or location of a file from <literal>oldPath</literal> to <literal>newPath</literal> (<link doc="changes.xml" id="njs0.3.4">0.3.4</link>). <example> >> var fs = require('fs') undefined >> var file = fs.renameSync('hello.txt', 'HelloWorld.txt') undefined </example> </tag-desc> <tag-name id="fs_rmdirsync"><literal>rmdirSync(<value>path</value>)</literal></tag-name> <tag-desc> Synchronously removes a directory at the specified <literal>path</literal> (<link doc="changes.xml" id="njs0.4.2">0.4.2</link>). </tag-desc> <tag-name id="fs_symlinksync"><literal>symlinkSync(<value>target</value>, <value>path</value>)</literal></tag-name> <tag-desc> Synchronously creates the link called <literal>path</literal> pointing to <literal>target</literal> using <link url="http://man7.org/linux/man-pages/man2/symlink.2.html">symlink(2)</link> (<link doc="changes.xml" id="njs0.3.9">0.3.9</link>). Relative targets are relative to the link’s parent directory. </tag-desc> <tag-name id="fs_unlinksync"><literal>unlinkSync(<value>path</value>)</literal></tag-name> <tag-desc> Synchronously unlinks a file by <literal>path</literal> (<link doc="changes.xml" id="njs0.3.9">0.3.9</link>). </tag-desc> <tag-name id="fs_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>. The <literal>data</literal> is expected to be a string or a Buffer object (<link doc="changes.xml" id="njs0.4.4">0.4.4</link>). 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="fs_dirent" name="fs.Dirent"> <para> <literal>fs.Dirent</literal> is a representation of a directory entry— a file or a subdirectory. When <link id="fs_readdirsync"><literal>readdirSync()</literal></link> is called with the <link id="fs_readdirsync_withfiletypes"><literal>withFileTypes</literal></link> option, the resulting array contains <literal>fs.Dirent</literal> objects. <list type= "bullet" compact="no"> <listitem> <literal>dirent.isBlockDevice()</literal>—returns <literal>true</literal> if the <literal>fs.Dirent</literal> object describes a block device. </listitem> <listitem> <literal>dirent.isCharacterDevice()</literal>—returns <literal>true</literal> if the <literal>fs.Dirent</literal> object describes a character device. </listitem> <listitem> <literal>dirent.isDirectory()</literal>—returns <literal>true</literal> if the <literal>fs.Dirent</literal> object describes a file system directory. </listitem> <listitem> <literal>dirent.isFIFO()</literal>—returns <literal>true</literal> if the <literal>fs.Dirent</literal> object describes a first-in-first-out (FIFO) pipe. </listitem> <listitem> <literal>dirent.isFile()</literal>—returns <literal>true</literal> if the <literal>fs.Dirent</literal> object describes a regular file. </listitem> <listitem> <literal>dirent.isSocket()</literal>—returns <literal>true</literal> if the <literal>fs.Dirent</literal> object describes a socket. </listitem> <listitem> <literal>dirent.isSymbolicLink()</literal>—returns <literal>true</literal> if the <literal>fs.Dirent</literal> object describes a symbolic link. </listitem> <listitem> <literal>dirent.name</literal>— the name of the file <literal>fs.Dirent</literal> object refers to. </listitem> </list> </para> </section> <section id="access_const" name="File Access Constants"> <para> The <link id="fs_accesssync"><literal>access()</literal></link> method can accept the following flags. These flags are exported by <literal>fs.constants</literal>: <list type= "bullet" compact="no"> <listitem> <literal>F_OK</literal>—indicates that the file is visible to the calling process, used by default if no mode is specified </listitem> <listitem> <literal>R_OK</literal>—indicates that the file can be read by the calling process </listitem> <listitem> <literal>W_OK</literal>—indicates that the file can be written by the calling process </listitem> <listitem> <literal>X_OK</literal>—indicates that the file can be executed by the calling process </listitem> </list> </para> </section> <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 id="querystring" name="Query String"> <para> The Query String module provides support for parsing and formatting URL query strings (<link doc="changes.xml" id="njs0.4.3">0.4.3</link>). The Query String module object is returned by <literal>require('querystring')</literal>. </para> <para> <list type="tag"> <tag-name id="querystring_decode"><literal>querystring.decode()</literal></tag-name> <tag-desc> is an alias for <link id="querystring_parse"><literal>querystring.parse()</literal></link>. </tag-desc> <tag-name id="querystring_encode"><literal>querystring.encode()</literal></tag-name> <tag-desc> is an alias for <link id="querystring_stringify"><literal>querystring.stringify()</literal></link>. </tag-desc> <tag-name id="querystring_escape"><literal>querystring.escape(<value>string</value>)</literal></tag-name> <tag-desc> <para> Performs URL encoding of the given <literal>string</literal>, returns an escaped query string. The method is used by <link id="querystring_stringify"><literal>querystring.stringify()</literal></link> and should not be used directly. </para> </tag-desc> <tag-name id="querystring_parse"><literal>querystring.parse(<value>string</value>[, <value>separator</value>[, <value>equal</value>[, <value>options</value>]]])</literal></tag-name> <tag-desc> <para> Parses the query string URL and returns an object. </para> <para> The <literal>separator</literal> parameter is a substring for delimiting key and value pairs in the query string, by default is “<literal>&</literal>”. </para> <para> The <literal>equal</literal> parameter is a substring for delimiting keys and values in the query string, by default is “<literal>=</literal>”. </para> <para> The <literal>options</literal> parameter is expected to be an object with the following keys: <list type="tag"> <tag-name><literal>decodeURIComponent</literal> <value>function</value></tag-name> <tag-desc> Function used to decode percent-encoded characters in the query string, by default is <link id="querystring_unescape"><literal>querystring.unescape()</literal></link> </tag-desc> <tag-name><literal>maxKeys</literal> <value>number</value></tag-name> <tag-desc> the maximum number of keys to parse, by default is <literal>1000</literal>. The <literal>0</literal> value removes limitations for counting keys. </tag-desc> </list> By default, percent-encoded characters within the query string are assumed to use the UTF-8 encoding, invalid UTF-8 sequences will be replaced with the <literal>U+FFFD</literal> replacement character. </para> <para> For example, for the following query string <example> 'foo=bar&abc=xyz&abc=123' </example> the output will be: <example> { foo: 'bar', abc: ['xyz', '123'] } </example> </para> </tag-desc> <tag-name id="querystring_stringify"><literal>querystring.stringify(<value>object</value>[, <value>separator</value>[, <value>equal</value>[, <value>options</value>]]])</literal></tag-name> <tag-desc> <para> Serializes an object and returns a URL query string. </para> <para> The <literal>separator</literal> parameter is a substring for delimiting key and value pairs in the query string, by default is “<literal>&</literal>”. </para> <para> The <literal>equal</literal> parameter is a substring for delimiting keys and values in the query string, by default is “<literal>=</literal>”. </para> <para> The <literal>options</literal> parameter is expected to be an object with the following keys: <list type="tag"> <tag-name><literal>encodeURIComponent</literal> <value>function</value></tag-name> <tag-desc> The function to use when converting URL-unsafe characters to percent-encoding in the query string, by default is <link id="querystring_escape"><literal>querystring.escape()</literal></link>. </tag-desc> </list> </para> <para> By default, characters that require percent-encoding within the query string are encoded as UTF-8. If other encoding is required, then <literal>encodeURIComponent</literal> option should be specified. </para> <para> For example, for the following command <example> querystring.stringify({ foo: 'bar', baz: ['qux', 'quux'], 123: '' }); </example> the query string will be: <example> 'foo=bar&baz=qux&baz=quux&123=' </example> </para> </tag-desc> <tag-name id="querystring_unescape"><literal>querystring.unescape(<value>string</value>)</literal></tag-name> <tag-desc> <para> Performs decoding of URL percent-encoded characters of the <literal>string</literal>, returns an unescaped query string. The method is used by <link id="querystring_parse"><literal>querystring.parse()</literal></link> and should not be used directly. </para> </tag-desc> </list> </para> </section> </section> </article>