Reference (original) (raw)
njs provides objects, methods and properties for extending nginx functionality.
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 inECMAScript specification. List of all njs properties and methods can be found inCompatibility.
nginx objects
HTTP Request
The HTTP request object is available only in thengx_http_js_module module. Before 0.8.5, all string properties of the object were byte strings.
r.args{}
request arguments object, read-only.
The query string is returned as an object. Since 0.7.6, duplicate keys are returned as an array, keys are case-sensitive, both keys and values are percent-decoded.
For example, the query string
'a=1&b=%32&A=3&b=4&B=two%20words'
is converted to r.args
as:
{a: "1", b: ["2", "4"], A: "3", B: "two words"}
More advanced parsing scenarios can be achieved with theQuery String module and with the$argsvariable, for example:
import qs from 'querystring';
function args(r) { return qs.parse(r.variables.args); }
The argument object is evaluated at the first access to r.args
. If only a single argument is needed, for example foo
,nginx variables can be used:
r.variables.arg_foo
Here, nginx variables objectreturns the first value for a given key, case-insensitive, without percent-decoding.
To convert r.args
back to a string, the Query Stringstringifymethod can be used.
r.done()
after calling this function, next data chunks will be passed to client without callingjs_body_filter(0.5.2). May be called only from thejs_body_filter function
r.error(`_string_`)
writes a string
to the error log on the error
level of logging
As nginx has ahardcodedmaximum line length limit, only first 2048 bytes of the string can be logged.
r.finish()
finishes sending a response to the client
incoming headers object, read-only.
The Foo
request header can be accessed with the syntax:headersIn.foo
or headersIn['Foo']
.
The “Authorization”, “Content-Length”, “Content-Range”, “Content-Type”, “ETag”, “Expect”, “From”, “Host”, “If-Match”, “If-Modified-Since”, “If-None-Match”, “If-Range”, “If-Unmodified-Since”, “Max-Forwards”, “Proxy-Authorization”, “Referer”, “Transfer-Encoding”, and “User-Agent” request headers can have only one field value (0.4.1). Duplicate field values in “Cookie” headers are separated by semicolon (;
). Duplicate field values in all other request headers are separated by commas.
outgoing headers object for the main request, writable.
If r.headersOut{}
is the response object of a subrequest, it represents response headers. In this case, field values in “Accept-Ranges”, “Connection”, “Content-Disposition”, “Content-Encoding”, “Content-Length”, “Content-Range”, “Date”, “Keep-Alive”, “Server”, “Transfer-Encoding”, “X-Accel-*” response headers may be omitted.
The “Foo” response header can be accessed with the syntax:headersOut.foo
or headersOut['Foo']
.
Outgoing headers should be set before a response header is sent to a client, otherwise header update will be ignored. This means that r.headersOut{}
is effectively writable in:
- the js_content handler beforer.sendHeader() orr.return() are called
- the js_header_filter handler
Field values of multi-value response headers (0.4.0) can be set with the syntax:
r.headersOut['Foo'] = ['a', 'b']
where the output will be:
Foo: a Foo: b
All previous field values of the “Foo” response header will be deleted.
For standard response headers that accept only a single field value such as “Content-Type”, only the last element of the array will take effect. Field values of the “Set-Cookie” response header are always returned as an array. Duplicate field values in “Age”, “Content-Encoding”, “Content-Length”, “Content-Type”, “ETag”, “Expires”, “Last-Modified”, “Location”, “Retry-After” response headers are ignored. Duplicate field values in all other response headers are separated by commas.
r.httpVersion
HTTP version, read-only
r.internal
boolean value, true forinternallocations
r.internalRedirect(`_uri_`)
performs aninternal redirectto the specified uri
. If the uri starts with the “@
” prefix, it is considered a named location. In a new location, all request processing is repeated starting from NGX_HTTP_SERVER_REWRITE_PHASEfor ordinary locations and from NGX_HTTP_REWRITE_PHASEfor named locations. As a result, a redirect to a named location does not check client_max_body_sizelimit. See dev guidefor more details. Redirected requests become internal and can access theinternallocations. The actual redirect happens after the handler execution is completed.
After redirect, a new njs VM is started in the target location, the VM in the original location is stopped. Values of nginx variables are kept and can be used to pass information to the target location. Since 0.5.3, the variable declared with the
js_var
directive forhttp orstreamcan be used.
Since 0.7.4, the method accepts escaped URIs.
r.log(`_string_`)
writes a string
to the error log on the info
level of logging
As nginx has ahardcodedmaximum line length limit, only first 2048 bytes of the string can be logged.
r.method
HTTP method, read-only
r.parent
references the parent request object
r.remoteAddress
client address, read-only
r.requestBody
the property was made obsolete in0.5.0and was removed in 0.8.0. The r.requestBuffer orr.requestText property should be used instead.
r.requestBuffer
client request body if it has not been written to a temporary file (since 0.5.0). To ensure that the client request body is in memory, its size should be limited byclient_max_body_size, and a sufficient buffer size should be set usingclient_body_buffer_size. The property is available only in thejs_content directive.
r.requestText
the same as r.requestBuffer, but returns a string
. Note that it may convert bytes invalid in UTF-8 encoding into the replacement character.
returns an array of key-value pairs exactly as they were received from the client (0.4.1).
For example, with the following request headers:
Host: localhost Foo: bar foo: bar2
the output of r.rawHeadersIn
will be:
[ ['Host', 'localhost'], ['Foo', 'bar'], ['foo', 'bar2'] ]
All foo
headers can be collected with the syntax:
r.rawHeadersIn.filter(v=>v[0].toLowerCase() == 'foo').map(v=>v[1])
the output will be:
['bar', 'bar2']
Header field names are not converted to lower case, duplicate field values are not merged.
returns an array of key-value pairs of response headers (0.4.1). Header field names are not converted to lower case, duplicate field values are not merged.
r.responseBody
the property was made obsolete in0.5.0and was removed in 0.8.0. The r.responseBufferor the r.responseTextproperty should be used instead.
r.responseBuffer
holds the subrequest response body, read-only (since 0.5.0). The size of r.responseBuffer
is limited by thesubrequest_output_buffer_sizedirective.
r.responseText
the same as r.responseBufferbut returns a string (since 0.5.0). Note that it may convert bytes invalid in UTF-8 encoding into the replacement character.
r.return(status[, string | Buffer])
sends the entire response with the specified status
to the client. The response can be a string or Buffer (0.5.0).
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
r.send(string | Buffer)
sends a part of the response body to the client. The data sent can be a string or Buffer (0.5.0)
r.sendBuffer(`_data_`[,`_options_`])
adds data to the chain of data chunks to be forwarded to the next body filter (0.5.2). The actual forwarding happens later, when the all the data chunks of the current chain are processed.
The data can be a string or Buffer. The options
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:
last
boolean, true if the buffer is the last buffer
flush
boolean, true if the buffer should have the flush
flag
The method may be called only from thejs_body_filter function.
sends the HTTP headers to the client
r.setReturnValue(`_value_`)
sets the return value of thejs_set handler (0.7.0). Unlike an ordinary return statement, this method should be used when the handler is JS async function. For example:
async function js_set(r) { const digest = await crypto.subtle.digest('SHA-256', r.headersIn.host); r.setReturnValue(digest); }
r.status
status, writable
r.subrequest(`_uri_`[,`_options_`[, `_callback_`]])
creates a subrequest with the given uri
andoptions
, and installs an optional completion callback
.
Asubrequestshares its input headers with the client request. To send headers different from original headers to a proxied server, theproxy_set_headerdirective can be used. To send a completely new set of headers to a proxied server, theproxy_pass_request_headersdirective can be used.
If options
is a string, then it holds the subrequest arguments string. Otherwise, options
is expected to be an object with the following keys:
args
arguments string, by default an empty string is used
body
request body, by default the request body of the parent request object is used
method
HTTP method, by default the GET
method is used
detached
boolean flag (0.3.9), if true
, 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 detached
flag and callback argument are mutually exclusive.
The completion callback
receives a subrequest response object with methods and properties identical to the parent request object.
Since 0.3.8, if a callback
is not provided, the Promise
object that resolves to the subrequest response objectis returned.
For example, to view all response headers in the subrequest:
async function handler(r) { const reply = await r.subrequest('/path');
for (const h in reply.headersOut) { r.log(
${h}: ${reply.headersOut[h]}
); }r.return(200); }
r.uri
current URIin request,normalized, read-only
r.rawVariables{}
nginx variables as Buffers, writable (since 0.5.0)
r.variables{}
nginx variables object, writable (since 0.2.8).
For example, to get the $foo
variable, one of the following syntax can be used:
r.variables['foo'] r.variables.foo
Since 0.8.6, regular expression captures can be accessed using the following syntax:
r.variables['1'] r.variables[1]
nginx treats variables referenced in nginx.conf
and unreferenced variables differently. When a variable is referenced, it may be cacheable, but when it is unreferenced it is always uncacheable. For example, when the$request_idvariable is only accessed from njs, it has a new value every time it is evaluated. But, when the$request_idis referenced, for example:
proxy_set_header X-Request-Id $request_id;
the r.variables.request_id
returns the same value every time.
A variable is writable if:
- it was created using the
js_var
directive forhttp orstream(since 0.5.3) - it is referenced in nginx configuration file
Even so, some embedded variables still cannot be assigned a value (for example,$http_).
r.warn(`_string_`)
writes a string
to the error log on the warning
level of logging
As nginx has ahardcodedmaximum line length limit, only first 2048 bytes of the string can be logged.
Stream Session
The stream session object is available only in thengx_stream_js_modulemodule. Before 0.8.5, all string properties of the object were byte strings.
s.allow()
s.decline()
an alias tos.done(-5)(0.2.4)
s.deny()
an alias tos.done(403)(0.2.4)
s.done([`_code_`]
)
sets an exit code
for the currentphase handler to a code value, by default 0
. The actual finalization happens when the js handler is completed and all pending events, for example, fromngx.fetch() orsetTimeout(), are processed (0.2.4).
Possible code values:
0
— successful finalization, passing control to the next phase-5
— undecided, passing control to the next handler of the current phase (if any)403
— access is forbidden May be called only from a phase handler function:js_accessorjs_preread.
s.error(`_string_`)
writes a sent string
to the error log on the error
level of logging
As nginx has ahardcodedmaximum line length limit, only first 2048 bytes of the string can be logged.
s.log(`_string_`)
writes a sent _string_
to the error log on the info
level of logging
As nginx has ahardcodedmaximum line length limit, only first 2048 bytes of the string can be logged.
s.off(`_eventName_`)
unregisters the callback set by the s.on() method (0.2.4)
s.on(`_event_`,`_callback_`)
registers a callback
for the specified event
(0.2.4).
An event
may be one of the following strings:
upload
new data (string) from a client
download
new data (string) to a client
upstream
new data (Buffer) from a client (since 0.5.0)
downstream
new data (Buffer) to a client (since 0.5.0)
The completion callback has the following prototype:callback(data, flags)
, wheredata
is string or Buffer (depending on the event type)flags
is an object with the following properties:
last
a boolean value, true if data is a last buffer.
s.remoteAddress
client address, read-only
s.rawVariables
nginx variables as Buffers, writable (since 0.5.0)
s.send(`_data_`[,`_options_`])
adds data to the chain of data chunks that will be forwarded in the forward direction: in download callback to a client; in upload to an upstream server (0.2.4). The actual forwarding happens later, when the all the data chunks of the current chain are processed.
The data can be a string or Buffer (0.5.0). The options
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:
last
boolean, true if the buffer is the last buffer
flush
boolean, true if the buffer should have the flush
flag
The method can be called multiple times per callback invocation.
s.sendDownstream()
is identical to s.send(), except for it always sends data to a client (since 0.7.8).
s.sendUpstream()
is identical to s.send(), except for it always sends data from a client (since 0.7.8).
s.status
session status code, an alias to the$statusvariable, read only (since 0.5.2)
s.setReturnValue(`_value_`)
sets the return value of thejs_set handler (0.7.0). Unlike an ordinary return statement, this method should be used when the handler is JS async function. For example:
async function js_set(r) { const digest = await crypto.subtle.digest('SHA-256', r.headersIn.host); r.setReturnValue(digest); }
s.variables{}
nginx variables object, writable (since 0.2.8). A variable can be writable only if it is referenced in nginx configuration file. Even so, some embedded variables still cannot be assigned a value.
s.warn(`_string_`)
writes a sent string
to the error log on the warning
level of logging
As nginx has ahardcodedmaximum line length limit, only first 2048 bytes of the string can be logged.
Periodic Session
The Periodic Session
object is provided as the first argument for the js_periodic
handler forhttpandstream(since 0.8.1).
PeriodicSession.rawVariables{}
nginx variables as Buffers, writable.
PeriodicSession.variables{}
nginx variables object, writable.
Headers
The Headers
interface of theFetch APIis available since 0.5.1.
A new Headers
object can be created using theHeaders() constructor: (since 0.7.10):
init
An object containing HTTP headers for prepopulating the Headers
object, can be a string
, an array
of name-value pairs, or an existing Headers
object.
A new Headers
object can be created with the following properties and methods:
Appends a new value into an existing header in theHeaders
object, or adds the header if it does not already exist (since 0.7.10).
Deletes a header from the Headers
object (since 0.7.10).
Returns a string containing the values of all headers with the specified name separated by a comma and a space.
Returns an array containing the values of all headers with the specified name.
Executes a provided function once for each key/value pair in the Headers
object (since 0.7.10).
Returns a boolean value indicating whether a header with the specified name exists.
Sets a new value for an existing header inside the Headers
object, or adds the header if it does not already exist (since 0.7.10).
Request
Request() |
---|
Request.arrayBuffer() |
Request.bodyUsed |
Request.cache |
Request.credentials |
Request.headers |
Request.json() |
Request.method |
Request.mode |
Request.text() |
Request.url |
The Request
interface of theFetch APIis available since 0.7.10.
A new Request
object can be created using theRequest() constructor:
Request[`_resource_`[,`_options_`]])
Creates a Request
object to fetch that can be passed later tongx.fetch(). The resource
can be a URL or an existing Request
object. The options
is an optional argument that is expected to be an object with the following keys:
body
The request body, by default is empty.
headers
The response headers object — the object containing HTTP headers for prepopulating the Headers object, can be a string
, an array
of name-value pairs, or an existing Headers object.
method
The HTTP method, by default the GET method is used.
A new Request
object can be created with the following properties and methods:
arrayBuffer()
Returns a Promise
that resolves with an ArrayBuffer
.
bodyUsed
A boolean value, true
if the body was used in the request.
cache
Contains the cache mode of the request.
credentials
Contains the credentials of the request, by default is same-origin
.
The Headers read-only object associated with theRequest.
json()
Returns a Promise
that resolves with the result of parsing the request body as JSON.
method
Contains the request method.
mode
Contains the mode of the request.
text()
Returns a Promise
that resolves with a string representation of the request body.
url
Contains the URL of the request.
Response
Response() |
---|
Response.arrayBuffer() |
Response.bodyUsed |
Response.headers |
Response.json() |
Response.ok |
Response.redirected |
Response.status |
Response.statusText |
Response.text() |
Response.type |
Response.url |
The Response
interface is available since0.5.1.
A new Response
object can be created using theResponse() constructor (since 0.7.10):
Response[`_body_`[,`_options_`]])
Creates a Response
object. The body
is an optional argument, can be a string
or a buffer
, by default is null
. The options
is an optional argument that is expected to be an object with the following keys:
headers
The response headers object — the object containing HTTP headers for prepopulating the Headers object, can be a string
, an array
of name-value pairs, or an existing Headers object.
status
The status code of the response.
statusText
The status message corresponding to the status code.
A new Response()
object can be created with the following properties and methods:
arrayBuffer()
Takes a Response
stream and reads it to completion. Returns a Promise
that resolves with an ArrayBuffer
.
bodyUsed
A boolean value, true
if the body was read.
The Headers read-only object associated with theResponse.
json()
Takes a Response
stream and reads it to completion. Returns a Promise
that resolves with the result of parsing the body text as JSON.
ok
A boolean value, true
if the response was successful (status codes between 200–299).
redirected
A boolean value, true
if the response is the result of a redirect.
status
The status code of the response.
statusText
The status message corresponding to the status code.
text()
Takes a Response
stream and reads it to completion. Returns a Promise
that resolves with a string.
type
The type of the response.
url
The URL of the response.
ngx
ngx.build |
---|
ngx.conf_file_path |
ngx.conf_prefix |
ngx.error_log_path |
ngx.fetch() |
ngx.log() |
ngx.prefix |
ngx.version |
ngx.version_number |
ngx.worker_id |
The ngx
global object is available since 0.5.0.
ngx.build
a string containing an optional nginx build name, corresponds to the--build=nameargument of the configure script, by default is ""
(0.8.0)
ngx.conf_file_path
a string containing the file path to current nginx configuration file (0.8.0)
ngx.conf_prefix
a string containing the file path tonginx configuration prefix — the directory where nginx is currently looking for configuration (0.7.8)
ngx.error_log_path
a string containing the file path to the currenterror log file (0.8.0)
ngx.fetch(`_resource_`, [`_options_`])
Makes a request to fetch a _resource_
(0.5.1), which can be an URL or the Request object (0.7.10). Returns a Promise
that resolves with the Response object. Since 0.7.0, the https://
scheme is supported, redirects are not handled.
If the URL in the _resource_
is specified as a domain name, it is determined using aresolver. If the https://
scheme is specified, thejs_fetch_trusted_certificatedirective should be configured for the authentication of the _resource_
's HTTPS server.
The options
parameter is expected to be an object with the following keys:
body
request body, by default is empty
buffer_size
the buffer size for reading the response, by default is 4096
request headers object
max_response_body_size
the maximum size of the response body in bytes, by default is 32768
method
HTTP method, by default the GET
method is used
verify
enables or disables verification of the HTTPS server certificate, by default is true
(0.7.0)
Example:
let reply = await ngx.fetch('http://nginx.org/'); let body = await reply.text();
r.return(200, body);
ngx.log
(_level_
,_message_
)
writes a message to the error log with the specified level of logging. The _level_
parameter specifies one of the log levels, the _message_
parameter can be a string or Buffer. The following log levels can be specified:ngx.INFO
,ngx.WARN
, andngx.ERR
.
As nginx has ahardcodedmaximum line length limit, only first 2048 bytes of the string can be logged.
ngx.prefix
a string containing the file path tonginx prefix — a directory that keeps server files (0.8.0)
ngx.version
a string containing nginx version, for example: 1.25.0
(0.8.0)
ngx.version_number
a number containing nginx version, for example: 1025000
(0.8.0)
ngx.worker_id
a number that corresponds to nginx internal worker id, the value is between 0
and the value specified in theworker_processes directive (0.8.0)
ngx.shared
The ngx.shared
global object is available since 0.8.0.
SharedDict
The shared dictionary object is available since 0.8.0. The shared dictionary name, type, and size are set with the js_shared_dict_zone
directive inhttporstream.
A SharedDict()
object has the following properties and methods:
ngx.shared.SharedDict.add(`_key_`,`_value_` [,`_timeout_`])
Sets the value
for the specified key
in the dictionary only if the key does not exist yet. The key
is a string representing the key of the item to add, the value
is the value of the item to add.
The optional timeout
argument is specified in milliseconds and overrides the timeout
parameter of thejs_shared_dict_zone
directive inhttporstream(since 0.8.5). It can be useful when some keys are expected to have unique timeouts.
Returns true
if the value has been successfully added to the SharedDict
dictionary,false
if the key already exists in the dictionary. Throws SharedMemoryError
if there is not enough free space in the SharedDict
dictionary. Throws TypeError
if the value
is of a different type than expected by this dictionary.
ngx.shared.SharedDict.capacity
Returns the capacity of the SharedDict
dictionary, corresponds to the size
parameter ofjs_shared_dict_zone
directive inhttporstream.
ngx.shared.SharedDict.clear()
Removes all items from the SharedDict
dictionary.
ngx.shared.SharedDict.delete(`_key_`)
Removes the item associated with the specified key from the SharedDict
dictionary,true
if the item in the dictionary existed and was removed,false
otherwise.
ngx.shared.SharedDict.freeSpace()
Returns the free page size in bytes. If the size is zero, the SharedDict
dictionary will still accept new values if there is space in the occupied pages.
ngx.shared.SharedDict.get(`_key_`)
Retrieves the item by its key
, returns the value associated with the key
or undefined
if there is none.
ngx.shared.SharedDict.has(`_key_`)
Searches for an item by its key
, returns true
if such item exists orfalse
otherwise.
ngx.shared.SharedDict.incr(`_key_`,`_delta_`[[,`_init_`], `_timeout_`]))
Increments the integer value associated with the key
by delta
. The key
is a string, the delta
is the number to increment or decrement the value by. If the key does not exist, the item will be initialized to an optional init
argument, by default is 0
.
The optional timeout
argument is specified in milliseconds and overrides the timeout
parameter of thejs_shared_dict_zone
directive inhttporstream(since 0.8.5). It can be useful when some keys are expected to have unique timeouts.
Returns the new value. Throws SharedMemoryError
if there is not enough free space in the SharedDict
dictionary. Throws TypeError
if this dictionary does not expect numbers.
This method can be used only if the dictionary type was declared with
type=number
parameter of thejs_shared_dict_zone
directive inhttporstream.
ngx.shared.SharedDict.items([`_maxCount_`])
Returns an array of the SharedDict
dictionary key-value items (since 0.8.1). The maxCount
parameter sets maximum number of items to retrieve, by default is 1024
.
ngx.shared.SharedDict.keys([`_maxCount_`])
Returns an array of the SharedDict
dictionary keys. The maxCount
parameter sets maximum number of keys to retrieve, by default is 1024
.
ngx.shared.SharedDict.name
Returns the name of the SharedDict
dictionary, corresponds to the zone=
parameter ofjs_shared_dict_zone
directive inhttporstream.
ngx.shared.SharedDict.pop(`_key_`)
Removes the item associated with the specified key
from the SharedDict
dictionary, returns the value associated with the key
or undefined
if there is none.
ngx.shared.SharedDict.replace(`_key_`,`_value_`)
Replaces the value
for the specified key
only if the key already exists, returns true
if the value was successfully replaced,false
if the key does not exist in the SharedDict
dictionary. Throws SharedMemoryError
if there is not enough free space in the SharedDict
dictionary. Throws TypeError
if the value
is of a different type than expected by this dictionary.
ngx.shared.SharedDict.set(`_key_`,`_value_` [,`_timeout_`])
Sets the value
for the specified key
, returns this SharedDict
dictionary (for method chaining).
The optional timeout
argument is specified in milliseconds and overrides the timeout
parameter of thejs_shared_dict_zone
directive inhttporstream(since 0.8.5). It can be useful when some keys are expected to have unique timeouts.
ngx.shared.SharedDict.size()
Returns the number of items for the SharedDict
dictionary.
ngx.shared.SharedDict.type
Returns string
or number
that corresponds to the SharedDict
dictionary type set by the type=
parameter ofjs_shared_dict_zone
directive inhttporstream.
built-in objects
console
The console
object is available in nginx since 0.8.2, in CLI since 0.2.6.
console.error(`_msg_`[, `_msg2_` ...])
Outputs one or more error messages. The message may be a string or an object.
console.info(`_msg_`[, `_msg2_` ...])
Outputs one or more info messages. The message may be a string or an object.
console.log(`_msg_`[, `_msg2_` ...])
Outputs one or more log messages. The message may be a string or an object.
console.time(`_label_`)
Starts a timer that can track how long an operation takes. The label
parameter allows naming different timers. If console.timeEnd()with the same name is called, the time that elapsed since the timer was started will be output, in milliseconds.
console.timeEnd(`_label_`)
Stops a timer previously started byconsole.time()The label
parameter allows naming different timers.
console.warn(`_msg_`[, `_msg2_` ...])
Outputs one or more warning messages. The message may be a string or an object.
crypto
The crypto
object is a global object that allows using cryptographic functionality (since 0.7.0).
сrypto.getRandomValues
(typedArray)
Gets cryptographically strong random values. Returns the same array passed as typedArray
but with its contents replaced with the newly generated random numbers. Possible values:
typedArray
can beInt8Array
,Int16Array
,Uint16Array
,Int32Array
, orUint32Array
сrypto.subtle.encrypt
(algorithm,key,data)
Encrypts datausing the providedalgorithm andkey. Returns a Promise
that fulfills with an ArrayBuffer
containing the ciphertext. Possible values:
algorithm
an object that specifies the algorithm to be used and any extra parameters if required:
- for
RSA-OAEP
, pass the object with the following keys:name
is a string, should be set toRSA-OAEP
:crypto.subtle.encrypt({name: "RSA-OAEP"}, key, data)
- for
AES-CTR
, pass the object with the following keys:name
is a string, should be set toAES-CTR
counter
is anArrayBuffer
,TypedArray
, orDataView
— the initial value of the counter block, must be 16 bytes long (the AES block size). The rightmost length bits of this block are used for the counter, and the rest is used for the nonce. For example, if length is set to 64, then the first half of counter is the nonce and the second half is used for the counterlength
is the number of bits in the counter block that are used for the actual counter. The counter must be big enough that it doesn't wrap.
- for
AES-CBC
, pass the object with the following keys:name
is a string, should be set toAES-CBC
iv
or the initialization vector, is anArrayBuffer
,TypedArray
, orDataView
, must be 16 bytes, unpredictable, and preferably cryptographically random. However, it need not be secret, for example, it may be transmitted unencrypted along with the ciphertext.
- for
AES-GCM
, pass the object with the following keys:name
is a string, should be set toAES-GCM
iv
or the initialization vector, is anArrayBuffer
,TypedArray
, orDataView
, must be 16 bytes, and must be unique for every encryption operation carried out with a given keyadditionalData
(optional) is anArrayBuffer
,TypedArray
, orDataView
that contains additional data that will not be encrypted but will be authenticated along with the encrypted data. IfadditionalData
is specified, then the same data must be specified in the corresponding call todecrypt()
: if the data given to thedecrypt()
call does not match the original data, the decryption will throw an exception. The bit length ofadditionalData
must be smaller than2^64 - 1
.tagLength
(optional, default is128
) - anumber
that determines the size in bits of the authentication tag generated in the encryption operation and used for authentication in the corresponding decryption Possible values:32
,64
,96
,104
,112
,120
, or128
. The AES-GCM specification recommends that it should be96
,104
,112
,120
, or128
, although32
or64
bits may be acceptable in some applications.
key
a CryptoKey that contains the key to be used for encryption
data
anArrayBuffer
,TypedArray
, orDataView
that contains the data to be encrypted (also known as the plaintext)
сrypto.subtle.decrypt
(algorithm,key,data)
Decrypts encrypted data. Returns a Promise
with the decrypted data. Possible values:
algorithm
an object that specifies the algorithm to be used, and any extra parameters as required. The values given for the extra parameters must match those passed into the corresponding encrypt()
call.
- for
RSA-OAEP
, pass the object with the following keys:name
is a string, should be set toRSA-OAEP
:crypto.subtle.encrypt({name: "RSA-OAEP"}, key, data)
- for
AES-CTR
, pass the object with the following keys:name
is a string, should be set toAES-CTR
counter
is anArrayBuffer
,TypedArray
, orDataView
— the initial value of the counter block, must be 16 bytes long (the AES block size). The rightmost length bits of this block are used for the counter, and the rest is used for the nonce. For example, if length is set to 64, then the first half of counter is the nonce and the second half is used for the counter.length
is the number of bits in the counter block that are used for the actual counter. The counter must be big enough that it doesn't wrap.
- for
AES-CBC
, pass the object with the following keys:name
is a string, should be set toAES-CBC
iv
or the initialization vector, is anArrayBuffer
,TypedArray
, orDataView
, must be 16 bytes, unpredictable, and preferably cryptographically random. However, it need not be secret (for example, it may be transmitted unencrypted along with the ciphertext).
- for
AES-GCM
, pass the object with the following keys:name
is a string, should be set toAES-GCM
iv
or the initialization vector, is anArrayBuffer
,TypedArray
, orDataView
, must be 16 bytes, and must be unique for every encryption operation carried out with a given keyadditionalData
(optional) is anArrayBuffer
,TypedArray
, orDataView
that contains additional data that will not be encrypted but will be authenticated along with the encrypted data. IfadditionalData
is specified, then the same data must be specified in the corresponding call todecrypt()
: if the data given to thedecrypt()
call does not match the original data, the decryption will throw an exception. The bit length ofadditionalData
must be smaller than2^64 - 1
.tagLength
(optional, default is128
) - anumber
that determines the size in bits of the authentication tag generated in the encryption operation and used for authentication in the corresponding decryption. Possible values:32
,64
,96
,104
,112
,120
, or128
. The AES-GCM specification recommends that it should be96
,104
,112
,120
, or128
, although32
or64
bits may be acceptable in some applications.
key
a CryptoKeythat contains the key to be used for decryption. If RSA-OAEP
is used, this is theprivateKey
property of theCryptoKeyPair object.
data
anArrayBuffer
,TypedArray
, orDataView
that contains the data to be decrypted (also known as ciphertext)
сrypto.subtle.deriveBits
(algorithm,baseKey,length)
Derives an array of bits from a base key. Returns a Promise
which will be fulfilled with anArrayBuffer
that contains the derived bits. Possible values:
algorithm
is an object that defines the derivation algorithm to use:
- for
HKDF
, pass the object with the following keys:name
is a string, should be set toHKDF
hash
is a string with the digest algorithm to use:SHA-1
,SHA-256
,SHA-384
, orSHA-512
salt
is anArrayBuffer
,TypedArray
, orDataView
that represents random or pseudo-random value with the same length as the output of thedigest
function. Unlike the input key material passed intoderiveKey()
, salt does not need to be kept secret.info
is anArrayBuffer
,TypedArray
, orDataView
that represents application-specific contextual information used to bind the derived key to an application or context, and enables deriving different keys for different contexts while using the same input key material. This property is required but may be an empty buffer.
- for
PBKDF2
, pass the object with the following keys:name
is a string, should be set toPBKDF2
hash
is a string with the digest algorithm to use:SHA-1
,SHA-256
,SHA-384
, orSHA-512
salt
is anArrayBuffer
,TypedArray
, orDataView
that represents random or pseudo-random value of at least16
bytes. Unlike the input key material passed intoderiveKey()
, salt does not need to be kept secret.iterations
is anumber
that represents the number of times the hash function will be executed inderiveKey()
baseKey
is a CryptoKeythat represents the input to the derivation algorithm - the initial key material for the derivation function: for example, for PBKDF2
it might be a password, imported as a CryptoKey usingсrypto.subtle.importKey()
length
is a number representing the number of bits to derive. For browsers compatibility, the number should be a multiple of 8
сrypto.subtle.deriveKey
(algorithm,baseKey,derivedKeyAlgorithm,extractable,keyUsages)
Derives a secret key from a master key. Possible values:
algorithm
is an object that defines the derivation algorithm to use:
- for
HKDF
, pass the object with the following keys:name
is a string, should be set toHKDF
hash
is a string with the digest algorithm to use:SHA-1
,SHA-256
,SHA-384
, orSHA-512
salt
is anArrayBuffer
,TypedArray
, orDataView
that represents random or pseudo-random value with the same length as the output of thedigest
function. Unlike the input key material passed intoderiveKey()
, salt does not need to be kept secret.info
is anArrayBuffer
,TypedArray
, orDataView
that represents application-specific contextual information used to bind the derived key to an application or context, and enables deriving different keys for different contexts while using the same input key material. This property is required but may be an empty buffer.
- for
PBKDF2
, pass the object with the following keys:name
is a string, should be set toPBKDF2
hash
is a string with the digest algorithm to use:SHA-1
,SHA-256
,SHA-384
, orSHA-512
salt
is anArrayBuffer
,TypedArray
, orDataView
that represents random or pseudo-random value of at least16
bytes. Unlike the input key material passed intoderiveKey()
, salt does not need to be kept secret.iterations
is anumber
that represents the number of times the hash function will be executed inderiveKey()
baseKey
is a CryptoKeythat represents the input to the derivation algorithm - the initial key material for the derivation function: for example, for PBKDF2
it might be a password, imported as a CryptoKey usingсrypto.subtle.importKey().
derivedKeyAlgorithm
is an object that defines the algorithm the derived key will be used for:
- for
HMAC
, pass the object with the following keys:name
is a string, should be set toHMAC
hash
is a string with the name of the digest function to use:SHA-1
,SHA-256
,SHA-384
, orSHA-512
length
(optional) is anumber
that represents the length in bits of the key. If not specified, the length of the key is equal to the block size of the chozen hash function
- for
AES-CTR
,AES-CBC
, orAES-GCM
, pass the object with the following keys:name
is a string, should be set toAES-CTR
,AES-CBC
, orAES-GCM
, depending on the algorithm usedlength
is anumber
that represents the length in bits of the key to generate:128
,192
, or256
is a boolean value that indicates whether it will be possible to export the key
keyUsages
is an Array
that indicates what can be done with the derived key. The key usages must be allowed by the algorithm set in derivedKeyAlgorithm
. Possible values:
encrypt
key for encrypting messages
decrypt
key for decrypting messages
sign
key for signing messages
verify
key for verifying signatures
deriveKey
key for deriving a new key
deriveBits
key for deriving bits
wrapKey
key for wrapping a key
unwrapKey
key for unwrapping a key
сrypto.subtle.digest
(algorithm,data)
Generates a digest of the given data. Takes as its arguments an identifier for the digest algorithm to use and the data to digest. Returns a Promise
which will be fulfilled with the digest. Possible values:
algorithm
is a string that defines the hash function to use:SHA-1
(not for cryptographic applications),SHA-256
,SHA-384
, orSHA-512
data
is anArrayBuffer
,TypedArray
, orDataView
that contains the data to be digested
сrypto.subtle.exportKey
(format,key)
Exports a key: takes a key as a CryptoKey object and returns the key in an external, portable format (since 0.7.10). If the format
was jwk
, then the Promise
fulfills with a JSON object containing the key. Otherwise, the promise fulfills with anArrayBuffer
containing the key. Possible values:
format
a string that describes the data format in which the key should be exported, can be the following:
raw
the raw data format
pkcs8
thePKCS #8format
spki
theSubjectPublicKeyInfoformat
jwk
theJSON Web Key(JWK) format (since 0.7.10)
key
the CryptoKeythat contains the key to be exported
сrypto.subtle.generateKey
(algorithm,extractable,usage)
Generates a new key for symmetric algorithms or key pair for public-key algorithms (since 0.7.10). Returns a Promise
that fulfills with the generated key as a CryptoKeyor CryptoKeyPair object. Possible values:
algorithm
a dictionary object that defines the type of key to generate and provides extra algorithm-specific parameters:
- for
RSASSA-PKCS1-v1_5
,RSA-PSS
, orRSA-OAEP
, pass the object with the following keys:name
is a string, should be set toRSASSA-PKCS1-v1_5
,RSA-PSS
, orRSA-OAEP
, depending on the used algorithmhash
is a string that represents the name of thedigest
function to use, can beSHA-256
,SHA-384
, orSHA-512
- for
ECDSA
, pass the object with the following keys:name
is a string, should be set toECDSA
namedCurve
is a string that represents the name of the elliptic curve to use, may beP-256
,P-384
, orP-521
- for
HMAC
, pass the object with the following keys:name
is a string, should be set toHMAC
hash
is a string that represents the name of thedigest
function to use, can beSHA-256
,SHA-384
, orSHA-512
length
(optional) is a number that represents the length in bits of the key. If omitted, the length of the key is equal to the length of the digest generated by the chosen digest function.
- for
AES-CTR
,AES-CBC
, orAES-GCM
, pass the string identifying the algorithm or an object of the form{ "name": "ALGORITHM" }
, whereALGORITHM
is the name of the algorithm
boolean value that indicates if it is possible to export the key
usage
an array
that indicates possible actions with the key:
encrypt
key for encrypting messages
decrypt
key for decrypting messages
sign
key for signing messages
verify
key for verifying signatures
deriveKey
key for deriving a new key
deriveBits
key for deriving bits
wrapKey
key for wrapping a key
unwrapKey
key for unwrapping a key
сrypto.subtle.importKey
(format,keyData,algorithm,extractable,keyUsages)
Imports a key: takes as input a key in an external, portable format and gives a CryptoKey object. Returns a Promise
that fulfills with the imported key as a CryptoKey object. Possible values:
format
a string that describes the data format of the key to import, can be the following:
raw
the raw data format
pkcs8
thePKCS #8format
spki
theSubjectPublicKeyInfoformat
jwk
theJSON Web Key(JWK) format (since 0.7.10)
keyData
theArrayBuffer
,TypedArray
, orDataView
object that contains the key in the given format
algorithm
a dictionary object that defines the type of key to import and provides extra algorithm-specific parameters:
- for
RSASSA-PKCS1-v1_5
,RSA-PSS
, orRSA-OAEP
, pass the object with the following keys:name
is a string, should be set toRSASSA-PKCS1-v1_5
,RSA-PSS
, orRSA-OAEP
, depending on the used algorithmhash
is a string that represents the name of thedigest
function to use, can beSHA-1
,SHA-256
,SHA-384
, orSHA-512
- for
ECDSA
, pass the object with the following keys:name
is a string, should be set toECDSA
namedCurve
is a string that represents the name of the elliptic curve to use, may beP-256
,P-384
, orP-521
- for
HMAC
, pass the object with the following keys:name
is a string, should be set toHMAC
hash
is a string that represents the name of thedigest
function to use, can beSHA-256
,SHA-384
, orSHA-512
length
(optional) is a number that represents the length in bits of the key. If omitted, the length of the key is equal to the length of the digest generated by the chosen digest function.
- for
AES-CTR
,AES-CBC
, orAES-GCM
, pass the string identifying the algorithm or an object of the form{ "name": "ALGORITHM" }
, whereALGORITHM
is the name of the algorithm - for
PBKDF2
, pass thePBKDF2
string - for
HKDF
, pass theHKDF
string
boolean value that indicates if it is possible to export the key
keyUsages
an array
that indicates possible actions with the key:
encrypt
key for encrypting messages
decrypt
key for decrypting messages
sign
key for signing messages
verify
key for verifying signatures
deriveKey
key for deriving a new key
deriveBits
key for deriving bits
wrapKey
key for wrapping a key
unwrapKey
key for unwrapping a key
сrypto.subtle.sign
(algorithm,key,data)
Returns signature
as a Promise
that fulfills with an ArrayBuffer
containing the signature. Possible values:
algorithm
is a string or object that specifies the signature algorithm to use and its parameters:
- for
RSASSA-PKCS1-v1_5
, pass the string identifying the algorithm or an object of the form{ "name": "ALGORITHM" }
- for
RSA-PSS
, pass the object with the following keys:name
is a string, should be set toRSA-PSS
saltLength
is a longinteger
that represents the length of the random salt to use, in bytes
- for
ECDSA
, pass the object with the following keys:name
is a string, should be set toECDSA
hash
is an identifier for the digest algorithm to use, can beSHA-256
,SHA-384
, orSHA-512
- for
HMAC
, pass the string identifying the algorithm or an object of the form{ "name": "ALGORITHM" }
key
is a CryptoKey object that the key to be used for signing. If algorithm identifies a public-key cryptosystem, this is the private key.
data
is anArrayBuffer
,TypedArray
, orDataView
object that contains the data to be signed
сrypto.subtle.verify
(algorithm,key,signature,data)
Verifies a digital signature, returns a Promise
that fulfills with a boolean value:true
if the signature is valid, otherwise false
. Possible values:
algorithm
is a string or object that specifies the algorithm to use and its parameters:
- for
RSASSA-PKCS1-v1_5
, pass the string identifying the algorithm or an object of the form{ "name": "ALGORITHM" }
- for
RSA-PSS
, pass the object with the following keys:name
is a string, should be set toRSA-PSS
saltLength
is a longinteger
that represents the length of the random salt to use, in bytes
- for
ECDSA
, pass the object with the following keys:name
is a string, should be set toECDSA
hash
is an identifier for the digest algorithm to use, can beSHA-256
,SHA-384
, orSHA-512
- for
HMAC
, pass the string identifying the algorithm or an object of the form{ "name": "ALGORITHM" }
key
is a CryptoKey object that the key to be used for verifying. It is the secret key for a symmetric algorithm and the public key for a public-key system.
signature
is anArrayBuffer
,TypedArray
, orDataView
that contains the signature to verify
data
is anArrayBuffer
,TypedArray
, orDataView
object that contains the data whose signature is to be verified
CryptoKey
The CryptoKey
object represents a cryptographic key
obtained from one of the SubtleCrypto
methods:сrypto.subtle.generateKey(),сrypto.subtle.deriveKey(),сrypto.subtle.importKey().
CryptoKey.algorithm
returns an object describing the algorithm for which this key can be used and any associated extra parameters (since 0.8.0), read-only
a boolean value, true
if the key can be exported (since 0.8.0), read-only
CryptoKey.type
a string value that indicates which kind of key is represented by the object, read-only. Possible values:
secret
This key is a secret key for use with a symmetric algorithm.
private
This key is the private half of an asymmetric algorithm'sCryptoKeyPair
public
This key is the public half of an asymmetric algorithm'sCryptoKeyPair.
CryptoKey.usages
An array of strings indicating what this key can be used for (since 0.8.0), read-only. Possible array values:
encrypt
key for encrypting messages
decrypt
key for decrypting messages
sign
key for signing messages
verify
key for verifying signatures
deriveKey
key for deriving a new key
deriveBits
key for deriving bits
CryptoKeyPair
The CryptoKeyPair
is a dictionary object of the WebCrypto APIthat represents an asymmetric key pair.
CryptoKeyPair.privateKey
A CryptoKey object representing the private key.
CryptoKeyPair.publicKey
A CryptoKey object representing the public key.
njs
The njs
object is a global object that represents the current VM instance (since 0.2.0).
njs.version
Returns a string with the current version of njs (for example, “0.7.4”).
njs.version_number
Returns a number with the current version of njs. For example, “0.7.4” is returned as 0x000704
(since 0.7.4).
njs.dump(`_value_`)
Returns the pretty-print string representation for a value.
njs.memoryStats
Object containing memory statistics for current VM instance (since 0.7.8).
size
amount of memory in bytes njs memory pool claimed from the operating system.
njs.on(`_event_`,`_callback_`)
Registers a callback for the specified VM event (since 0.5.2). An event may be one of the following strings:
exit
is called before the VM is destroyed. The callback is called without arguments.
process
The process
object is a global object that provides information about the current process (0.3.3).
process.argv
Returns an array that contains the command line arguments passed when the current process was launched.
process.env
Returns an object containing the user environment.
By default, nginx removes all environment variables inherited from its parent process except the TZ variable. Use the env directive to preserve some of the inherited variables.
process.kill(`_pid_`,`_number_` | `_string_`)
Sends the signal to the process identified by pid
. Signal names are numbers or strings such as 'SIGINT' or 'SIGHUP'. See kill(2)for more information.
process.pid
Returns the PID of the current process.
process.ppid
Returns the PID of the current parent process.
String
By default all strings in njs are Unicode strings. They correspond to ECMAScript strings that contain Unicode characters. Before 0.8.0, byte strings were also supported.
Byte strings
Since 0.8.0, the support for byte strings and byte string methods were removed. When working with byte sequence, the Buffer object and
Buffer
properties, such asr.requestBuffer,r.rawVariables, should be used.
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 toUTF8() method serializes a Unicode string to a byte string using UTF-8 encoding:
'£'.toUTF8().toString('hex') 'c2a3' /* C2 A3 is the UTF-8 representation of 00A3 ('£') code point */
The toBytes() method serializes a Unicode string with code points up to 255 into a byte string, otherwise, null
is returned:
'£'.toBytes().toString('hex') 'a3' /* a3 is a byte equal to 00A3 ('£') code point */
String.bytesFrom(`_array_`| `_string_`, `_encoding_`)
The method was made obsolete in0.4.4and was removed in 0.8.0. The Buffer.from
method should be used instead:
Buffer.from([0x62, 0x75, 0x66, 0x66, 0x65, 0x72]).toString() 'buffer'
Buffer.from('YnVmZmVy', 'base64').toString() 'buffer'
Before 0.4.4, created a byte string either from an array that contained octets, or from an encoded string (0.2.3), the encoding could behex
,base64
, andbase64url
.
String.prototype.fromBytes(`_start_`[,`_end_`])
the property was made obsolete in0.7.7and was removed in 0.8.0. Before 0.7.7, returned a new Unicode string from a byte string where each byte was replaced with a corresponding Unicode code point.
String.prototype.fromUTF8(`_start_`[,`_end_`])
the property was made obsolete in0.7.7and was removed in 0.8.0. The TextDecoder method should be used instead. Before 0.7.7, converted a byte string containing a valid UTF-8 string into a Unicode string, otherwise null
was returned.
String.prototype.toBytes(`_start_`[,`_end_`])
the property was made obsolete in0.7.7and was removed in 0.8.0. Before 0.7.7, serialized a Unicode string to a byte string, returned null
if a character larger than 255 was found in the string.
String.prototype.toString(`_encoding_`)
the property was made obsolete in0.7.7and was removed in 0.8.0. Before 0.7.7, encoded a string tohex
,base64
, orbase64url
:
'αβγδ'.toString('base64url') 'zrHOss6zzrQ'
Before version 0.4.3, only a byte string could be encoded:
'αβγδ'.toUTF8().toString('base64url') 'zrHOss6zzrQ'
String.prototype.toUTF8(`_start_`[,`_end_`])
the property was made obsolete in0.7.7and was removed in 0.8.0. The TextEncoder method should be used instead. Before 0.7.7, serialized a Unicode string to a byte string using UTF-8 encoding:
'αβγδ'.toUTF8().length 8 'αβγδ'.length 4
web API
Text Decoder
The TextDecoder
produces a stream of code points from a stream of bytes (0.4.3).
TextDecoder([[`_encoding_`],`_options_`])
Creates a new TextDecoder
object for specified encoding
, currently, only UTF-8 is supported. The options
isTextDecoderOptions
dictionary with the property:
fatal
boolean flag indicating ifTextDecoder.decode()must throw the _TypeError_
exception when a coding error is found, by default is false
.
TextDecoder.prototype.encoding
Returns a string with the name of the encoding used byTextDecoder(), read-only.
TextDecoder.prototype.fatal
boolean flag, true
if the error mode is fatal, read-only.
TextDecoder.prototype.ignoreBOM
boolean flag, true
if the byte order marker is ignored, read-only.
TextDecoder.prototype.decode(`_buffer_`, [`_options_`])
Returns a string with the text decoded from the buffer
byTextDecoder(). The buffer can be ArrayBuffer
. The options
isTextDecodeOptions
dictionary with the property:
stream
boolean flag indicating if additional data will follow in subsequent calls to decode()
:true
if processing the data in chunks, andfalse
for the final chunk or if the data is not chunked. By default is false
.
(new TextDecoder()).decode(new Uint8Array([206,177,206,178])) αβ
Text Encoder
The TextEncoder
object produces a byte stream with UTF-8 encoding from a stream of code points (0.4.3).
TextEncoder()
Returns a newly constructed TextEncoder
that will generate a byte stream with UTF-8 encoding.
TextEncoder.prototype.encode(`_string_`)
Encodes string
into a Uint8Array
with UTF-8 encoded text.
TextEncoder.prototype.encodeInto(`_string_`,`_uint8Array_`)
Encodes a string
to UTF-8, puts the result into destination Uint8Array
, and returns a dictionary object that shows the progress of the encoding. The dictionary object contains two members:
read
the number of UTF-16 units of code from the source string
converted to UTF-8
written
the number of bytes modified in the destination Uint8Array
timers
clearTimeout(`_timeout_`)
Cancels a timeout
object created by setTimeout().
setTimeout(`_function_`,`_milliseconds_`[,`_argument1_`,`_argumentN_`])
Calls a function
after a specified number of milliseconds
. One or more optional arguments
can be passed to the specified function. Returns a timeout
object.
function handler(v) { // ... }
t = setTimeout(handler, 12);
// ...
clearTimeout(t);
Global functions
atob(`_encodedData_`)
Decodes a string of data which has been encoded using Base64
encoding. The encodedData
parameter is a binary string that contains Base64-encoded data. Returns a string that contains decoded data from encodedData
.
The similar btoa() method can be used to encode and transmit data which may otherwise cause communication problems, then transmit it and use the atob()
method to decode the data again. For example, you can encode, transmit, and decode control characters such as ASCII values 0
through 31
.
const encodedData = btoa("text to encode"); // encode a string const decodedData = atob(encodedData); // decode the string
btoa(`_stringToEncode_`)
Creates a Base64-encoded ASCII string from a binary string. The stringToEncode
parameter is a binary string to encode. Returns an ASCII string containing the Base64 representation ofstringToEncode
.
The method can be used to encode data which may otherwise cause communication problems, transmit it, then use the atob() method to decode the data again. For example, you can encode control characters such as ASCII values 0
through 31
.
const encodedData = btoa("text to encode"); // encode a string const decodedData = atob(encodedData); // decode the string
built-in modules
Buffer
Buffer.alloc(`_size_`[,`_fill_`[,`_encoding_`]]))
Allocates a new Buffer of a specified _size_
. If _fill_
is not specified, the Buffer will be zero-filled. If _fill_
is specified, the allocated Buffer will be initialized by callingbuf.fill(fill). If _fill_
and _encoding_
are specified, the allocated Buffer will be initialized by callingbuf.fill(fill, encoding).
The _fill_
parameter may be a_string_
,_Buffer_
,_Uint8Array_
, or_integer_
.
Buffer.allocUnsafe(`_size_`)
The same asBuffer.alloc(), with the difference that the memory allocated for the buffer is not initialized, the contents of the new buffer is unknown and may contain sensitive data.
Buffer.byteLength(`_value_`[,`_encoding_`])
Returns the byte length of a specified value, when encoded using _encoding_
. The value can be astring
,Buffer
,TypedArray
,DataView
, orArrayBuffer
. If the value is a _string_
, the encoding
parameter is its encoding, can be_utf8_
,_hex_
,_base64_
,_base64url_
; by default is _utf8_
.
Buffer.compare(`_buffer1_`,`_buffer2_`)
Compares _buffer1_
with _buffer2_
when sorting arrays of Buffer instances. Returns0
if_buffer1_
is the same as _buffer2_
,1
if_buffer2_
should come before _buffer1_
when sorted, or-1
if_buffer2_
should come after _buffer1_
when sorted.
Buffer.concat(`_list_`[,`_totalLength_`])
Returns a new Buffer which is the result of concatenating all the Buffer instances in the list. If there are no items in the list or the total length is 0, a new zero-length Buffer is returned. If _totalLength_
is not specified, it is calculated from the Buffer instances in list by adding their lengths. If _totalLength_
is specified, it is coerced to an unsigned integer. If the combined length of the Buffers in list exceeds_totalLength_
, the result is truncated to _totalLength_
.
Buffer.from(`_array_`)
Allocates a new Buffer using an array of bytes in the range 0
– 255
. Array entries outside that range will be truncated.
Buffer.from(`_arrayBuffer_`,`_byteOffset_`[,`_length_`]])
Creates a view of the _ArrayBuffer_
without copying the underlying memory. The optional _byteOffset_
and _length_
arguments specify a memory range within the _arrayBuffer_
that will be shared by the Buffer.
Buffer.from(`_buffer_`)
Copies the passed buffer data onto a new Buffer instance.
Buffer.from(`_object_`[,`_offsetOrEncoding_`[,`_length_`]])
For objects whose valueOf()
function returns a value not strictly equal to object, returnsBuffer.from(object.valueOf()
,offsetOrEncoding
,length
).
Buffer.from(`_string_`[,`_encoding_`])
Creates a new Buffer with a _string_
. The _encoding_
parameter identifies the character encoding to be used when converting a string into bytes. The encoding can beutf8
,hex
,base64
,base64url
; by default is utf8
.
Buffer.isBuffer(`_object_`)
A boolean value, returns true
if _object_
is a Buffer.
Buffer.isEncoding(`_encoding_`)
A boolean value, returns true
if encoding is the name of a supported character encoding.
buffer[`_index_`]
The index operator that can be used to get and set the octet at position index
in buffer
. The values refer to individual bytes, so the legal value range is between 0 and 255 (decimal).
buf.buffer
The underlying ArrayBuffer
object based on which this Buffer object is created.
buf.byteOffset
An integer, specifying the byteOffset
of the Buffers underlying ArrayBuffer
object.
buf.compare(`_target_`[,`_targetStart_`[,`_targetEnd_`[,`_sourceStart_`[,`_sourceEnd_`]]]])
Compares buffer with _target_
and returns a number indicating whether buffer comes before, after, or is the same as _target_
in sort order. Comparison is based on the actual sequence of bytes in each Buffer. The targetStart
is an integer specifying the offset within _target_
at which to begin comparison, by default is 0. The targetEnd
is an integer specifying the offset within _target_
at which to end comparison, by default is target.length
. The sourceStart
is an integer specifying the offset within buffer at which to begin comparison, by default is 0. The sourceEnd
is an integer specifying the offset within buffer at which to end comparison (not inclusive), by default is buf.length
.
buf.copy(`_target_`[,`_targetStart_`[,`_sourceStart_`[,`_sourceEnd_`]]])
Copies data from a region of buffer to a region in _target_
, even if the target memory region overlaps with buffer. The target
parameter is a_Buffer_
or _Uint8Array_
to copy into.
The targetStart
is an integer specifying the offset within target at which to begin writing, by default is 0. The sourceStart
is an integer specifying the offset within buffer from which to begin copying, by default is 0. The sourceEnd
is an integer specifying the offset within buffer at which to stop copying (not inclusive) by default is _buf.length_
.
buf.equals(`_otherBuffer_`)
A boolean value, returns true
if both Buffer and _otherBuffer_
have exactly the same bytes.
buf.fill(`_value_`[,`_offset_`[,`_end_`]][,`_encoding_`])
Fills the Buffer with the specified _value_
. If the _offset_
and _end_
are not specified, the entire Buffer will be filled. The _value_
is coerced to _uint32_
if it is not astring
,Buffer
, orinteger
. If the resulting integer is greater than 255, the Buffer will be filled with _value_
and 255.
buf.includes(`_value_`[,`_byteOffset_`][,`_encoding_`])
Equivalent tobuf.indexOf() !== -1
, returns true
if the _value_
was found in Buffer.
buf.indexOf(`_value_`[,`_byteOffset_`][,`_encoding_`])
Returns an integer which is the index of the first occurrence of_value_
in Buffer, or _-1_
if Buffer does not contain value. The _value_
can be astring
with specified _encoding_
(by default _utf8_
),Buffer
,Unit8Array
, or a number between 0 and 255.
buf.lastIndexOf(`_value_`[,`_byteOffset_`][,`_encoding_`])
The same asbuf.indexOf(), except the last occurrence of the _value_
is found instead of the first occurrence. The _value_
can be a string, Buffer, or integer between 1 and 255. If the _value_
is an empty string or empty Buffer,byteOffset
will be returned.
buf.length
Returns the number of bytes in Buffer.
buf.readIntBE(`_offset_`,`_byteLength_`)
Reads the _byteLength_
from buf
at the specified _offset_
and interprets the result as a big-endian, two's complement signed value supporting up to 48 bits of accuracy. The _byteLength_
parameter is an integer between 1 and 6 specifying the number of bytes to read.
The similar methods are also supported:buf.readInt8([offset])
,buf.readInt16BE([offset])
,buf.readInt32BE([offset])
.
buf.readIntLE(`_offset_`,`_byteLength_`)
Reads the _byteLength_
from buf
at the specified _offset_
and interprets the result as a little-endian, two's complement signed value supporting up to 48 bits of accuracy. The _byteLength_
parameter is an integer between 1 and 6 specifying the number of bytes to read.
The similar methods are also supported:buf.readInt8([offset])
,buf.readInt16LE([offset])
,buf.readInt32LE([offset])
.
buf.readUIntBE(`_offset_`,`_byteLength_`)
Reads the _byteLength_
from buf
at the specified _offset_
and interprets the result as a big-endian integer supporting up to 48 bits of accuracy. The _byteLength_
parameter is an integer between 1 and 6 specifying the number of bytes to read.
The similar methods are also supported:buf.readUInt8([offset])
,buf.readUInt16BE([offset])
,buf.readUInt32BE([offset])
.
buf.readUIntLE(`_offset_`,`_byteLength_`)
Reads the _byteLength_
from buf
at the specified _offset_
and interprets the result as a little-endian integer supporting up to 48 bits of accuracy. The _byteLength_
parameter is an integer between 1 and 6 specifying the number of bytes to read.
The similar methods are also supported:buf.readUInt8([offset])
,buf.readUInt16LE([offset])
,buf.readUInt32LE([offset])
.
buf.readDoubleBE
([_offset_
])
Reads a 64-bit, big-endian double from buf
at the specified _offset_
.
buf.readDoubleLE
([_offset_
])
Reads a 64-bit, little-endian double from buf
at the specified _offset_
.
buf.readFloatBE
([_offset_
])
Reads a 32-bit, big-endian float from buf
at the specified _offset_
.
buf.readFloatLE
([_offset_
])
Reads a 32-bit, little-endian float from buf
at the specified _offset_
.
buf.subarray([`_start_`[,`_end_`]])
Returns a new buf
that references the same memory as the original, but offset and cropped by_start_
and _end_
. If _end_
is greater thanbuf.length, the same result as that of end equal tobuf.lengthis returned.
buf.slice([`_start_`[,`_end_`]])
Returns a new buf
that references the same memory as the original, but offset and cropped by the_start_
and _end_
values. The method is not compatible with theUint8Array.prototype.slice()
, which is a superclass of Buffer. To copy the slice, useUint8Array.prototype.slice()
.
buf.swap16
()
Interprets buf
as an array of unsigned 16-bit numbers and swaps the byte order in-place. Throws an error ifbuf.lengthis not a multiple of 2.
buf.swap32
()
Interprets buf
as an array of unsigned 32-bit numbers and swaps the byte order in-place. Throws an error ifbuf.lengthis not a multiple of 4.
buf.swap64
()
Interprets buf
as an array of 64-bit numbers and swaps byte order in-place. Throws an error ifbuf.lengthis not a multiple of 8.
buf.toJSON
()
Returns a JSON representation of buf.
JSON.stringify()
implicitly calls this function when stringifying a Buffer instance.
buf.toString([`_encoding_`[,`_start_`[,`_end_`]]])
Decodes buf
to a string according to the specified character _encoding_
which can be _utf8_
,_hex_
,_base64_
,_base64url_
. The _start_
and _end_
parameters may be passed to decode only a subset of Buffer.
buf.write(`_string_`[,`_offset_`[,`_length_`]][,`_encoding_`])
Writes a _string_
to buf
at _offset_
according to the character _encoding_
. The _length_
parameter is the number of bytes to write. If Buffer did not contain enough space to fit the entire string, only part of string will be written, however, partially encoded characters will not be written. The _encoding_
can be_utf8_
,_hex_
,_base64_
,_base64url_
.
buf.writeIntBE(`_value_`,`_offset_`,`_byteLength_`)
Writes _byteLength_
bytes of _value_
to buf
at the specified _offset_
as big-endian. Supports up to 48 bits of accuracy. The _byteLength_
parameter is an integer between 1 and 6 specifying the number of bytes to read.
The following similar methods are also supported:buf.writeInt8
,buf.writeInt16BE
,buf.writeInt32BE
.
buf.writeIntLE(`_value_`,`_offset_`,`_byteLength_`)
Writes _byteLength_
bytes of _value_
to buf
at the specified _offset_
as little-endian. Supports up to 48 bits of accuracy. The _byteLength_
parameter is an integer between 1 and 6 specifying the number of bytes to read.
The following similar methods are also supported:buf.writeInt8
,buf.writeInt16LE
,buf.writeInt32LE
.
buf.writeUIntBE(`_value_`,`_offset_`,`_byteLength_`)
Writes _byteLength_
bytes of _value_
to buf
at the specified _offset_
as big-endian. Supports up to 48 bits of accuracy. The _byteLength_
parameter is an integer between 1 and 6 specifying the number of bytes to read.
The following similar methods are also supported:buf.writeUInt8
,buf.writeUInt16BE
,buf.writeUInt32BE
.
buf.writeUIntLE(`_value_`,`_offset_`,`_byteLength_`)
Writes _byteLength_
bytes of _value_
to buf
at the specified _offset_
as little-endian. Supports up to 48 bits of accuracy. The _byteLength_
parameter is an integer between 1 and 6 specifying the number of bytes to read.
The following similar methods are also supported:buf.writeUInt8
,buf.writeUInt16LE
,buf.writeUInt32LE
.
buf.writeDoubleBE(`_value_`, [`_offset_`])
Writes the _value_
to buf
at the specified _offset_
as big-endian.
buf.writeDoubleLE(`_value_`, [`_offset_`])
Writes the _value_
to buf
at the specified _offset_
as little-endian.
buf.writeFloatBE(`_value_`, [`_offset_`])
Writes the _value_
to buf
at the specified _offset_
as big-endian.
buf.writeFloatLE(`_value_`, [`_offset_`])
Writes the _value_
to buf
at the specified _offset_
as little-endian.
Crypto
Since 0.7.0, extended crypto API is available as a globalcrypto object.
The Crypto module provides cryptographic functionality support. The Crypto module object is imported using import crypto from 'crypto'
.
crypto.createHash(`_algorithm_`)
Creates and returns a Hash object that can be used to generate hash digests using the given _algorithm_
. The algorithm can bemd5
,sha1
, andsha256
.
crypto.createHmac(`_algorithm_`,`_secret key_`)
Creates and returns an HMAC object that uses the given _algorithm_
and _secret key_
. The algorithm can bemd5
,sha1
, andsha256
.
Hash
hash.update(`_data_`)
Updates the hash content with the given _data_
.
hash.digest([`_encoding_`])
Calculates the digest of all of the data passed usinghash.update()
. The encoding can behex
,base64
, andbase64url
. If encoding is not provided, a Buffer object (0.4.4) is returned.
Before version (0.4.4), a byte string was returned instead of a Buffer object.
hash.copy()
Makes a copy of the current state of the hash (since 0.7.12).
import crypto from 'crypto';
crypto.createHash('sha1').update('A').update('B').digest('base64url'); /* BtlFlCqiamG-GMPiK_GbvKjdK10 */
HMAC
hmac.update(`_data_`)
Updates the HMAC content with the given _data_
.
hmac.digest([`_encoding_`])
Calculates the HMAC digest of all of the data passed usinghmac.update()
. The encoding can behex
,base64
, andbase64url
. If encoding is not provided, a Buffer object (0.4.4) is returned.
Before version 0.4.4, a byte string was returned instead of a Buffer object.
import crypto from 'crypto';
crypto.createHmac('sha1', 'secret.key').update('AB').digest('base64url'); /* Oglm93xn23_MkiaEq_e9u8zk374 */
File System
The File System module provides operations with files.
The module object is imported using import fs from 'fs'
. Since 0.3.9, promissified versions of file system methods are available throughfs.promises
object after importing with import fs from 'fs'
:
import fs from 'fs';
fs.promises.readFile("/file/path").then((data) => { /* */ });
accessSync(`_path_`[,`_mode_`])
Synchronously tests permissions for a file or directory specified in the path
(0.3.9). If the check fails, an error will be returned, otherwise, the method will return undefined.
mode
an optional integer that specifies the accessibility checks to be performed, by default is fs.constants.F_OK
try { fs.accessSync('/file/path', fs.constants.R_OK | fs.constants.W_OK); console.log('has access'); } catch (e) { console.log('no access');) }
appendFileSync(`_filename_`,`_data_`[, `_options_`])
Synchronously appends specified data
to a file with provided filename
. The data
is expected to be a string or a Buffer object (0.4.4). If the file does not exist, it will be created. The options
parameter is expected to be an object with the following keys:
mode
mode option, by default is 0o666
flag
file system flag, by default is a
closeSync(`_fd_`)
Closes the fd
file descriptor represented by an integer used by the method. Returns undefined
.
existsSync(`_path_`)
Boolean value, returnstrue
if the specified _path_
exists. (0.8.2)
fstatSync(`_fd_`)
Retrieves the fs.Stats object for the file descriptor (0.7.7). The fd
parameter is an integer representing the file descriptor used by the method.
lstatSync(`_path_`[,`_options_`])
Synchronously retrieves the fs.Stats object for the symbolic link referred to by path
(0.7.1). The options
parameter is expected to be an object with the following keys:
throwIfNoEntry
a boolean value which indicates whether an exception is thrown if no file system entry exists, rather than returning undefined
, by default is false
.
mkdirSync(`_path_`[,`_options_`])
Synchronously creates a directory at the specified path
(0.4.2). The options
parameter is expected to be aninteger
that specifies the mode, or an object with the following keys:
mode
mode option, by default is 0o777
.
openSync(`_path_`[,`_flags_`[, `_mode_`]])
Returns an integer representing the file descriptor for the opened file path
(0.7.7).
flags
file system flag, by default is r
mode
mode option, by default is 0o666
promises.open(`_path_`[,`_flags_`[, `_mode_`]])
Returns a FileHandle object representing the opened file path
(0.7.7).
flags
file system flag, by default is r
mode
mode option, by default is 0o666
readdirSync(`_path_`[,`_options_`])
Synchronously reads the contents of a directory at the specified path
(0.4.2). The options
parameter is expected to be a string that specifies encodingor an object with the following keys:
encoding
encoding, by default is utf8
. The encoding can be utf8
and buffer
(0.4.4).
withFileTypes
if set to true
, the files array will containfs.Dirent objects, by default is false
.
readFileSync(`_filename_`[,`_options_`])
Synchronously returns the contents of the file with provided filename
. The options
parameter holdsstring
that specifies encoding. If an encoding is specified, a string is returned, otherwise, a Buffer object (0.4.4) is returned.
Before version 0.4.4, a byte string was returned if encoding was not specified.
Otherwise, options
is expected to be an object with the following keys:
encoding
encoding, by default is not specified. The encoding can be utf8
,hex
(0.4.4),base64
(0.4.4),base64url
(0.4.4).
flag
file system flag, by default is r
import fs from 'fs';
var file = fs.readFileSync('/file/path.tar.gz'); console.log(file.slice(0,2).toString('hex')) /* '1f8b' */
readlinkSync(`_path_`[,`_options_`])
Synchronously gets the contents of the symbolic link path
usingreadlink(2)(0.8.7). The options
argument can be a string specifying an encoding, or an object with encoding
property specifying the character encoding to use. If the encoding
is _buffer_
, the result is returned as a Buffer
object, otherwise as a string.
readSync(`_fd_`,`_buffer_`, `_offset_`[,`_length_`[, `_position_`]])
Reads the content of a file path using file descriptor fd
, returns the number of bytes read (0.7.7).
buffer
the buffer
value can be aBuffer
,TypedArray
, orDataView
offset
is an integer
representing the position in buffer to write the data to
length
is an integer
representing the number of bytes to read
position
specifies where to begin reading from in the file, the value can beinteger
ornull
, by default is null
. If position
is null
, data will be read from the current file position, and the file position will be updated. If position is an integer
, the file position will be unchanged
realpathSync(`_path_`[,`_options_`])
Synchronously computes the canonical pathname by resolving.
, ..
and symbolic links usingrealpath(3). The options
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 (0.3.9).
renameSync(`_oldPath_`,`_newPath_`)
Synchronously changes the name or location of a file fromoldPath
to newPath
(0.3.4).
import fs from 'fs';
fs.renameSync('hello.txt', 'HelloWorld.txt');
rmdirSync(`_path_`)
Synchronously removes a directory at the specified path
(0.4.2).
statSync(`_path_`,[`_options_`])
Synchronously retrieves the fs.Stats object for the specified path
(0.7.1). The path
can be astring
orbuffer
. The options
parameter is expected to be an object with the following keys:
throwIfNoEntry
a boolean value which indicates whether an exception is thrown if no file system entry exists rather than returning undefined
, by default is true
.
symlinkSync(`_target_`,`_path_`)
Synchronously creates the link called path
pointing to target
usingsymlink(2)(0.3.9). Relative targets are relative to the link’s parent directory.
unlinkSync(`_path_`)
Synchronously unlinks a file by path
(0.3.9).
writeFileSync(`_filename_`,`_data_`[,`_options_`])
Synchronously writes data
to a file with provided filename
. The data
is expected to be a string or a Buffer object (0.4.4). If the file does not exist, it will be created, if the file exists, it will be replaced. The options
parameter is expected to be an object with the following keys:
mode
mode option, by default is 0o666
flag
file system flag, by default is w
import fs from 'fs';
fs.writeFileSync('hello.txt', 'Hello world');
writeSync(`_fd_`,`_buffer_`, `_offset_`[,`_length_`[, `_position_`]])
Writes a buffer to a file using file descriptor, returns the number
of bytes written (0.7.7).
fd
an integer
representing the file descriptor
buffer
the buffer
value can be aBuffer
,TypedArray
, orDataView
offset
is an integer
that determines the part of the buffer to be written, by default 0
length
is an integer
specifying the number of bytes to write, by default is an offset ofBuffer.byteLength
position
refers to the offset from the beginning of the file where this data should be written, can be aninteger
ornull
, by default is null
. See alsopwrite(2).
writeSync(`_fd_`,`_string_`[,`_position_`[,`_encoding_`]])
Writes a string
to a file using file descriptor fd
, returns the number
of bytes written (0.7.7).
fd
is an integer
representing a file descriptor
position
refers to the offset from the beginning of the file where this data should be written, can be aninteger
ornull
, by default is null
. See alsopwrite(2)
encoding
is a string
, by default is utf8
fs.Dirent
fs.Dirent
is a representation of a directory entry — a file or a subdirectory. WhenreaddirSync()is called with thewithFileTypesoption, the resulting array contains fs.Dirent
objects.
dirent.isBlockDevice()
— returnstrue
if thefs.Dirent
object describes a block device.dirent.isCharacterDevice()
— returnstrue
if thefs.Dirent
object describes a character device.dirent.isDirectory()
— returnstrue
if thefs.Dirent
object describes a file system directory.dirent.isFIFO()
— returnstrue
if thefs.Dirent
object describes a first-in-first-out (FIFO) pipe.dirent.isFile()
— returnstrue
if thefs.Dirent
object describes a regular file.dirent.isSocket()
— returnstrue
if thefs.Dirent
object describes a socket.dirent.isSymbolicLink()
— returnstrue
if thefs.Dirent
object describes a symbolic link.dirent.name
— the name of the filefs.Dirent
object refers to.
fs.FileHandle
The FileHandle
object is an object wrapper for a numeric file descriptor (0.7.7). Instances of the FileHandle
object are created by thefs.promises.open() method. If a FileHandle
is not closed using thefilehandle.close() method, it will try to automatically close the file descriptor, helping to prevent memory leaks. Please do not rely on this behavior because it can be unreliable. Instead, always explicitly close a FileHandle
.
filehandle.close()
Closes the file handle after waiting for any pending operation on the handle to complete. Returns a promise
, fulfills with undefined upon success.
filehandle.fd
The numeric file descriptor managed by the FileHandle
object.
filehandle.read(`_buffer_`,`_offset_`[,`_length_`[,`_position_`]])
Reads data from the file and stores that in the given buffer.
buffer
a buffer that will be filled with the file data read, the value can be aBuffer
,TypedArray
, orDataView
offset
is an integer
representing the location in the buffer at which to start filling
length
is an integer
representing the number of bytes to read
position
the location where to begin reading data from the file, the value can beinteger
,null
. If null
, data will be read from the current file position and the position will be updated. If position is an integer
, the current file position will remain unchanged.
Returns a Promise
which fulfills upon success with an object with two properties:
bytesRead
is an integer
representing the number of bytes read
buffer
is a reference to the passed argument in buffer, can beBuffer
,TypedArray
, orDataView
filehandle.stat()
Fulfills with anfs.Stats for the file, returns a promise
.
filehandle.write(`_buffer_`,`_offset_`[,`_length_`[,`_position_`]])
Writes a buffer to the file.
buffer
the buffer
value can be aBuffer
,TypedArray
, orDataView
offset
is an integer
representing the start position from within buffer where the data to write begins
length
is an integer
representing the number of bytes from buffer to write, by default is an offset ofBuffer.byteLength
position
the offset from the beginning of the file where the data from buffer should be written, can be aninteger
ornull
, by default is null
. If position
is not a number
, the data will be written at the current position. See the POSIXpwrite(2)documentation for details.
Returns a Promise
which is resolved with an object containing two properties:
bytesWritten
is an integer
representing the number of bytes written
buffer
a reference to the buffer written, can be aBuffer
,TypedArray
, orDataView
It is unsafe to use
filehandle.write()
multiple times on the same file without waiting for the promise to be resolved or rejected.
filehandle.write(`_string_`[,`_position_`[,`_encoding_`]])
Writes a string
to the file.
position
the offset from the beginning of the file where the data from buffer should be written, can be aninteger
ornull
, by default is null
. If position
is not a number
, the data will be written at the current position. See the POSIXpwrite(2)documentation for details.
encoding
the expected encoding of the string, by default utf8
Returns a Promise
which is resolved with an object containing two properties:
bytesWritten
is an integer
representing the number of bytes written
buffer
a reference to the buffer written, can be aBuffer
,TypedArray
, orDataView
It is unsafe to use
filehandle.write()
multiple times on the same file without waiting for the promise to be resolved or rejected.
fs.Stats
The fs.Stats
object provides information about a file. The object is returned fromfs.statSync() andfs.lstatSync().
stats.isBlockDevice()
— returnstrue
if thefs.Stats
object describes a block device.stats.isDirectory()
— returnstrue
if thefs.Stats
object describes a file system directory.stats.isFIFO()
— returnstrue
if thefs.Stats
object describes a first-in-first-out (FIFO) pipe.stats.isFile()
— returnstrue
if thefs.Stats
object describes a regular file.stats.isSocket()
— returnstrue
if thefs.Stats
object describes a socket.stats.isSymbolicLink()
— returnstrue
if thefs.Stats
object describes a symbolic link.stats.dev
— the numeric identifier of the device containing the file.stats.ino
— the file system specificInode
number for the file.stats.mode
— a bit-field describing the file type and mode.stats.nlink
— the number of hard-links that exist for the file.stats.uid
— the numeric user identifier of the user that owns the file (POSIX).stats.gid
— the numeric group identifier of the group that owns the file (POSIX).stats.rdev
— the numeric device identifier if the file represents a device.stats.size
— the size of the file in bytes.stats.blksize
— the file system block size for i/o operations.stats.blocks
— the number of blocks allocated for this file.stats.atimeMs
— the timestamp indicating the last time this file was accessed expressed in milliseconds since the POSIX Epoch.stats.mtimeMs
— the timestamp indicating the last time this file was modified expressed in milliseconds since the POSIX Epoch.stats.ctimeMs
— the timestamp indicating the last time this file was changed expressed in milliseconds since the POSIX Epoch.stats.birthtimeMs
— the timestamp indicating the creation time of this file expressed in milliseconds since the POSIX Epoch.stats.atime
— the timestamp indicating the last time this file was accessed.stats.mtime
— the timestamp indicating the last time this file was modified.stats.ctime
— the timestamp indicating the last time this file was changed.stats.birthtime
— the timestamp indicating the creation time of this file.
File Access Constants
The access() method can accept the following flags. These flags are exported by fs.constants
:
F_OK
— indicates that the file is visible to the calling process, used by default if no mode is specifiedR_OK
— indicates that the file can be read by the calling processW_OK
— indicates that the file can be written by the calling processX_OK
— indicates that the file can be executed by the calling process
File System Flags
The flag
option can accept the following values:
a
— open a file for appending. The file is created if it does not existax
— the same asa
but fails if the file already existsa+
— open a file for reading and appending. If the file does not exist, it will be createdax+
— the same asa+
but fails if the file already existsas
— open a file for appending in synchronous mode. If the file does not exist, it will be createdas+
— open a file for reading and appending in synchronous mode. If the file does not exist, it will be createdr
— open a file for reading. An exception occurs if the file does not existr+
— open a file for reading and writing. An exception occurs if the file does not existrs+
— open a file for reading and writing in synchronous mode. Instructs the operating system to bypass the local file system cachew
— open a file for writing. If the file does not exist, it will be created. If the file exists, it will be replacedwx
— the same asw
but fails if the file already existsw+
— open a file for reading and writing. If the file does not exist, it will be created. If the file exists, it will be replacedwx+
— the same asw+
but fails if the file already exists
Query String
The Query String module provides support for parsing and formatting URL query strings (0.4.3). The Query String module object is imported usingimport qs from 'querystring'
.
querystring.decode()
is an alias forquerystring.parse().
querystring.encode()
is an alias forquerystring.stringify().
querystring.escape(`_string_`)
Performs URL encoding of the given string
, returns an escaped query string. The method is used byquerystring.stringify()and should not be used directly.
querystring.parse(`_string_`[,`_separator_`[,`_equal_`[,`_options_`]]])
Parses the query string URL and returns an object.
The separator
parameter is a substring for delimiting key and value pairs in the query string, by default is “&
”.
The equal
parameter is a substring for delimiting keys and values in the query string, by default is “=
”.
The options
parameter is expected to be an object with the following keys:
decodeURIComponent
_function_
Function used to decode percent-encoded characters in the query string, by default isquerystring.unescape()
maxKeys
_number_
the maximum number of keys to parse, by default is 1000
. The 0
value removes limitations for counting keys.
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 U+FFFD
replacement character.
For example, for the following query string
'foo=bar&abc=xyz&abc=123'
the output will be:
{ foo: 'bar', abc: ['xyz', '123'] }
querystring.stringify(`_object_`[,`_separator_`[,`_equal_`[,`_options_`]]])
Serializes an object and returns a URL query string.
The separator
parameter is a substring for delimiting key and value pairs in the query string, by default is “&
”.
The equal
parameter is a substring for delimiting keys and values in the query string, by default is “=
”.
The options
parameter is expected to be an object with the following keys:
encodeURIComponent
_function_
The function to use when converting URL-unsafe characters to percent-encoding in the query string, by default isquerystring.escape().
By default, characters that require percent-encoding within the query string are encoded as UTF-8. If other encoding is required, thenencodeURIComponent
option should be specified.
For example, for the following command
querystring.stringify({ foo: 'bar', baz: ['qux', 'quux'], 123: '' });
the query string will be:
'foo=bar&baz=qux&baz=quux&123='
querystring.unescape(`_string_`)
Performs decoding of URL percent-encoded characters of the string
, returns an unescaped query string. The method is used byquerystring.parse()and should not be used directly.
XML
The XML module allows working with XML documents (since 0.7.10). The XML module object is imported usingimport xml from 'xml'
.
Example:
import xml from 'xml';
let data =
<note><to b="bar" a= "foo" >Tove</to><from>Jani</from></note>
; let doc = xml.parse(data);console.log(doc.note.to.$text) /* 'Tove' / console.log(doc.note.to.$attr$b) / 'bar' / console.log(doc.note.$tags[1].$text) / 'Jani' */
let dec = new TextDecoder(); let c14n = dec.decode(xml.exclusiveC14n(doc.note)); console.log(c14n) /* 'ToveJani' */
c14n = dec.decode(xml.exclusiveC14n(doc.note.to)); console.log(c14n) /* 'Tove' */
c14n = dec.decode(xml.exclusiveC14n(doc.note, doc.note.to /* excluding 'to' /)); console.log(c14n) / 'Jani' */
parse(`_string_` |`_Buffer_`)
Parses a string or Buffer for an XML document, returns anXMLDoc wrapper object representing the parsed XML document.
c14n(`_rootnode_`[,`_excludingnode_`])
Canonicalizes root_node
and its children according toCanonical XML Version 1.1. The root_node
can beXMLNode orXMLDoc wrapper object around XML structure. Returns Buffer object that contains canonicalized output.
excluding_node
allows omitting from the output a part of the document
exclusiveC14n(`_rootnode_`[,`_excludingnode_`[,`_withComments_`[,`_prefixlist_`]]])
Canonicalizes root_node
and its children according toExclusive XML Canonicalization Version 1.0.
root_node
isXMLNode orXMLDoc wrapper object around XML structure
excluding_node
allows omitting from the output a part of the document corresponding to the node and its children
withComments
a boolean value, false
by default. If true
, canonicalization corresponds toExclusive XML Canonicalization Version 1.0. Returns Buffer object that contains canonicalized output.
prefix_list
an optional string with a space separated namespace prefixes for namespaces that should also be included into the output
serialize()
The same asxml.c14n()(since 0.7.11).
serializeToString()
The same asxml.c14n()except it returns the result as a string
(since 0.7.11).
XMLDoc
An XMLDoc wrapper object around XML structure, the root node of the document.
doc.$root
the document's root by its name or undefined
doc.`_abc_`
the first root tag named _abc_
asXMLNode wrapper object
XMLNode
An XMLNode wrapper object around XML tag node.
node.`_abc_`
the same asnode.$tag$abc
node.$attr$`_abc_`
the node's attribute value of _abc_
, writable since 0.7.11
node.$attr$`_abc_`
=_xyz_
the same asnode.setAttribute('abc',xyz)(since 0.7.11)
node.$attrs
an XMLAttr wrapper object for all attributes of the node
node.$name
the name of the node
node.$ns
the namespace of the node
node.$parent
the parent node of the current node
node.$tag$`_abc_`
the first child tag of the node named _abc_
, writable since 0.7.11
node.$tags
an array of all children tags
node.$tags = [node1, node2, ...]
the same asnode.removeChildren();node.addChild(node1);node.addChild(node2)(since 0.7.11).
node.$tags$`_abc_`
all children tags named _abc_
of the node, writable since 0.7.11
node.$text
the content of the node, writable since 0.7.11
node.$text = 'abc'
the same asnode.setText('abc')(since 0.7.11)
node.addChild(`_nd_`)
adds XMLNode as a child to node (since 0.7.11).nd
is recursively copied before adding to the node
node.removeAllAttributes()
removes all attributes of the node (since 0.7.11)
node.removeAttribute(`_attrname_`)
removes the attribute named attr_name
(since 0.7.11)
node.removeChildren(`_tagname_`)
removes all the children tags named tag_name
(since 0.7.11). If tag_name
is absent, all children tags are removed
node.removeText()
removes the node's text value (0.7.11)
node.setAttribute(`_attrname_`,`_value_`)
sets a value for an attr_name
(since 0.7.11). When the value is null
, the attribute named attr_name
is deleted
node.setText(`_value_`)
sets a text value for the node (since 0.7.11). When the value is null
, the text of the node is deleted.
XMLAttr
An XMLAttrs wrapper object around XML node attributes.
attr.`_abc_`
the attribute value of _abc_
zlib
The zlib module provides compression functionality using the “deflate” and “inflate” algorithms (since 0.7.12). The zlib module object is imported usingimport zlib from 'zlib'
.
deflateRawSync(`_string_` |`_Buffer_`[,`_options_`])
Compresses data using the “deflate” algorithm provided as a string or Buffer and does not append a zlib header. The buffer value can be aBuffer
,TypedArray
, orDataView
.Options
is an optional object that containszlib_options. Returns Buffer instance that contains the compressed data.
deflateSync(`_string_` |`_Buffer_`[,`_options_`])
Compresses data using the “deflate” algorithm provided as a string or Buffer. The Buffer value can be aBuffer
,TypedArray
, orDataView
.Options
is an optional object that containszlib_options. Returns Buffer instance that contains the compressed data.
inflateRawSync(`_string_` |`_Buffer_`)
Decompresses a raw stream by using the “deflate” algorithm. Returns Buffer instance that contains the decompressed data.
inflateSync(`_string_` |`_Buffer_`)
Decompresses a stream by using the “deflate” algorithm. Returns Buffer instance that contains the decompressed data.
zlib options
chunkSize
— is an integer, by default is1024
dictionary
— is aBuffer
,TypedArray
, orDataView
. by default is emptylevel
— is an integer, compression only, see zlib_compression_levelsmemLevel
— is an integer from1
to9
, compression onlystrategy
— is an integer, compression only, see zlib_compression_strategywindowBits
— is an integer from-15
to-9
for raw data, from9
to15
for an ordinary stream
zlib compression levels
Name | Description |
---|---|
zlib.constants.Z_NO_COMPRESSION | no compression |
zlib.constants.Z_BEST_SPEED | fastest, produces the least compression |
zlib.constants.Z_DEFAULT_COMPRESSION | trade-off between speed and compression |
zlib.constants.Z_BEST_COMPRESSION | slowest, produces the most compression |
zlib compression strategy
Name | Description |
---|---|
zlib.constants.Z_FILTERED | Filtered strategy: for the data produced by a filter or predictor |
zlib.constants.Z_HUFFMAN_ONLY | Huffman-only strategy: only Huffman encoding, no string matching |
zlib.constants.Z_RLE | Run Length Encoding strategy: limit match distances to one, better compression of PNG image data |
zlib.constants.Z_FIXED | Fixed table strategy: prevents the use of dynamic Huffman codes, a simpler decoder for special applications |
zlib.constants.Z_DEFAULT_STRATEGY | Default strategy, suitable for general purpose compression |