• Home
  • Raw
  • Download

Lines Matching +full:srv +full:- +full:ipv6 +full:- +full:multi +full:- +full:target

4   <meta charset="utf-8">
5 <meta name="viewport" content="width=device-width">
6 <meta name="nodejs.org:node-version" content="v14.20.1">
13 <body class="alt apidoc" id="api-section-all">
22 <li><a href="documentation.html" class="nav-documentation">About this documentation</a></li>
23 <li><a href="synopsis.html" class="nav-synopsis">Usage and example</a></li>
27 <li><a href="assert.html" class="nav-assert">Assertion testing</a></li>
28 <li><a href="async_hooks.html" class="nav-async_hooks">Async hooks</a></li>
29 <li><a href="buffer.html" class="nav-buffer">Buffer</a></li>
30 <li><a href="addons.html" class="nav-addons">C++ addons</a></li>
31 <li><a href="n-api.html" class="nav-n-api">C/C++ addons with Node-API</a></li>
32 <li><a href="embedding.html" class="nav-embedding">C++ embedder API</a></li>
33 <li><a href="child_process.html" class="nav-child_process">Child processes</a></li>
34 <li><a href="cluster.html" class="nav-cluster">Cluster</a></li>
35 <li><a href="cli.html" class="nav-cli">Command-line options</a></li>
36 <li><a href="console.html" class="nav-console">Console</a></li>
37 <li><a href="corepack.html" class="nav-corepack">Corepack</a></li>
38 <li><a href="crypto.html" class="nav-crypto">Crypto</a></li>
39 <li><a href="debugger.html" class="nav-debugger">Debugger</a></li>
40 <li><a href="deprecations.html" class="nav-deprecations">Deprecated APIs</a></li>
41 <li><a href="diagnostics_channel.html" class="nav-diagnostics_channel">Diagnostics Channel</a></li>
42 <li><a href="dns.html" class="nav-dns">DNS</a></li>
43 <li><a href="domain.html" class="nav-domain">Domain</a></li>
44 <li><a href="errors.html" class="nav-errors">Errors</a></li>
45 <li><a href="events.html" class="nav-events">Events</a></li>
46 <li><a href="fs.html" class="nav-fs">File system</a></li>
47 <li><a href="globals.html" class="nav-globals">Globals</a></li>
48 <li><a href="http.html" class="nav-http">HTTP</a></li>
49 <li><a href="http2.html" class="nav-http2">HTTP/2</a></li>
50 <li><a href="https.html" class="nav-https">HTTPS</a></li>
51 <li><a href="inspector.html" class="nav-inspector">Inspector</a></li>
52 <li><a href="intl.html" class="nav-intl">Internationalization</a></li>
53 <li><a href="modules.html" class="nav-modules">Modules: CommonJS modules</a></li>
54 <li><a href="esm.html" class="nav-esm">Modules: ECMAScript modules</a></li>
55 <li><a href="module.html" class="nav-module">Modules: <code>module</code> API</a></li>
56 <li><a href="packages.html" class="nav-packages">Modules: Packages</a></li>
57 <li><a href="net.html" class="nav-net">Net</a></li>
58 <li><a href="os.html" class="nav-os">OS</a></li>
59 <li><a href="path.html" class="nav-path">Path</a></li>
60 <li><a href="perf_hooks.html" class="nav-perf_hooks">Performance hooks</a></li>
61 <li><a href="policy.html" class="nav-policy">Policies</a></li>
62 <li><a href="process.html" class="nav-process">Process</a></li>
63 <li><a href="punycode.html" class="nav-punycode">Punycode</a></li>
64 <li><a href="querystring.html" class="nav-querystring">Query strings</a></li>
65 <li><a href="readline.html" class="nav-readline">Readline</a></li>
66 <li><a href="repl.html" class="nav-repl">REPL</a></li>
67 <li><a href="report.html" class="nav-report">Report</a></li>
68 <li><a href="stream.html" class="nav-stream">Stream</a></li>
69 <li><a href="string_decoder.html" class="nav-string_decoder">String decoder</a></li>
70 <li><a href="timers.html" class="nav-timers">Timers</a></li>
71 <li><a href="tls.html" class="nav-tls">TLS/SSL</a></li>
72 <li><a href="tracing.html" class="nav-tracing">Trace events</a></li>
73 <li><a href="tty.html" class="nav-tty">TTY</a></li>
74 <li><a href="dgram.html" class="nav-dgram">UDP/datagram</a></li>
75 <li><a href="url.html" class="nav-url">URL</a></li>
76 <li><a href="util.html" class="nav-util">Utilities</a></li>
77 <li><a href="v8.html" class="nav-v8">V8</a></li>
78 <li><a href="vm.html" class="nav-vm">VM</a></li>
79 <li><a href="wasi.html" class="nav-wasi">WASI</a></li>
80 <li><a href="worker_threads.html" class="nav-worker_threads">Worker threads</a></li>
81 <li><a href="zlib.html" class="nav-zlib">Zlib</a></li>
85 <li><a href="https://github.com/nodejs/node" class="nav-https-github-com-nodejs-node">Code reposito…
89 <div id="column1" data-id="all" class="interior">
91 <div class="header-container">
93 …<button class="theme-toggle-btn" id="theme-toggle-btn" title="Toggle dark mode/light mode" aria-la…
94 <svg xmlns="http://www.w3.org/2000/svg" class="icon dark-icon" height="24" width="24">
96-2.33-4.51-.5-8.48.53-10.07C6.27 2.2 1.98 6.59 1.98 12c0 .14.02.28.02.42.62-.27 1.29-.42 2-.42 1.6…
97 …<path d="M7 16h-.18C6.4 14.84 5.3 14 4 14c-1.66 0-3 1.34-3 3s1.34 3 3 3h3c1.1 0 2-.9 2-2s-.9-2-2-2…
99 <svg xmlns="http://www.w3.org/2000/svg" class="icon light-icon" height="24" width="24">
101-1.8-1.79-1.41 1.41 1.79 1.79 1.42-1.41zM4 10.5H1v2h3v-2zm9-9.95h-2V3.5h2V.55zm7.45 3.91l-1.41-1.4…
117 <li class="version-picker">
119 …<ol class="version-picker"><li><a href="https://nodejs.org/docs/latest-v18.x/api/all.html">18.x</a…
120 <li><a href="https://nodejs.org/docs/latest-v17.x/api/all.html">17.x</a></li>
121 <li><a href="https://nodejs.org/docs/latest-v16.x/api/all.html">16.x <b>LTS</b></a></li>
122 <li><a href="https://nodejs.org/docs/latest-v15.x/api/all.html">15.x</a></li>
123 <li><a href="https://nodejs.org/docs/latest-v14.x/api/all.html">14.x <b>LTS</b></a></li>
124 <li><a href="https://nodejs.org/docs/latest-v13.x/api/all.html">13.x</a></li>
125 <li><a href="https://nodejs.org/docs/latest-v12.x/api/all.html">12.x <b>LTS</b></a></li>
126 <li><a href="https://nodejs.org/docs/latest-v11.x/api/all.html">11.x</a></li>
127 <li><a href="https://nodejs.org/docs/latest-v10.x/api/all.html">10.x</a></li>
128 <li><a href="https://nodejs.org/docs/latest-v9.x/api/all.html">9.x</a></li>
129 <li><a href="https://nodejs.org/docs/latest-v8.x/api/all.html">8.x</a></li>
130 <li><a href="https://nodejs.org/docs/latest-v7.x/api/all.html">7.x</a></li>
131 <li><a href="https://nodejs.org/docs/latest-v6.x/api/all.html">6.x</a></li>
132 <li><a href="https://nodejs.org/docs/latest-v5.x/api/all.html">5.x</a></li>
133 <li><a href="https://nodejs.org/docs/latest-v4.x/api/all.html">4.x</a></li>
134 <li><a href="https://nodejs.org/docs/latest-v0.12.x/api/all.html">0.12.x</a></li>
135 <li><a href="https://nodejs.org/docs/latest-v0.10.x/api/all.html">0.10.x</a></li></ol>
144 <!-- TOC --><details id="toc" open><summary>Table of contents</summary>
309 …re_target_targetstart_targetend_sourcestart_sourceend"><code>buf.compare(target[, targetStart[, ta…
310 <li><a href="#buffer_buf_copy_target_targetstart_sourcestart_sourceend"><code>buf.copy(target[, tar…
403 …uffer_the_zero_fill_buffers_command_line_option">The <code>--zero-fill-buffers</code> command-line…
413 <li><a href="#addons_context_aware_addons">Context-aware addons</a>
424 <li><span class="stability_2"><a href="#addons_node_api">Node-API</a></span></li>
438 <li><span class="stability_2"><a href="#n_api_node_api">Node-API</a></span>
445 <li><a href="#n_api_node_gyp">node-gyp</a></li>
451 <li><a href="#n_api_node_pre_gyp">node-pre-gyp</a></li>
459 <li><a href="#n_api_node_api_version_matrix">Node-API version matrix</a></li>
466 <li><a href="#n_api_basic_node_api_data_types">Basic Node-API data types</a>
475 <li><a href="#n_api_node_api_memory_management_types">Node-API memory management types</a>
484 <li><a href="#n_api_node_api_callback_types">Node-API callback types</a>
585 …pi_functions_to_convert_from_c_types_to_node_api">Functions to convert from C types to Node-API</a>
599 <li><a href="#n_api_functions_to_convert_from_node_api_to_c_types">Functions to convert from Node-A…
749 <li><a href="#n_api_asynchronous_thread_safe_function_calls">Asynchronous thread-safe function call…
751 <li><a href="#n_api_calling_a_thread_safe_function">Calling a thread-safe function</a></li>
752 <li><a href="#n_api_reference_counting_of_thread_safe_functions">Reference counting of thread-safe …
771 <li><a href="#embedding_setting_up_per_process_state">Setting up per-process state</a></li>
772 <li><a href="#embedding_per_instance_state">Per-instance state</a></li>
882 <li><a href="#cli_command_line_options">Command-line options</a>
887 <li><a href="#cli"><code>-</code></a></li>
888 <li><a href="#cli_1"><code>--</code></a></li>
889 <li><a href="#cli_abort_on_uncaught_exception"><code>--abort-on-uncaught-exception</code></a></li>
890 <li><a href="#cli_completion_bash"><code>--completion-bash</code></a></li>
891 …lity_1"><a href="#cli_c_condition_conditions_condition"><code>-C=condition</code>, <code>--conditi…
892 <li><span class="stability_1"><a href="#cli_cpu_prof"><code>--cpu-prof</code></a></span></li>
893 <li><span class="stability_1"><a href="#cli_cpu_prof_dir"><code>--cpu-prof-dir</code></a></span></l…
894 <li><span class="stability_1"><a href="#cli_cpu_prof_interval"><code>--cpu-prof-interval</code></a>…
895 <li><span class="stability_1"><a href="#cli_cpu_prof_name"><code>--cpu-prof-name</code></a></span><…
896 <li><a href="#cli_diagnostic_dir_directory"><code>--diagnostic-dir=directory</code></a></li>
897 <li><a href="#cli_disable_proto_mode"><code>--disable-proto=mode</code></a></li>
898 <li><a href="#cli_disallow_code_generation_from_strings"><code>--disallow-code-generation-from-stri…
899 <li><a href="#cli_dns_result_order_order"><code>--dns-result-order=order</code></a></li>
900 <li><a href="#cli_enable_fips"><code>--enable-fips</code></a></li>
901 <li><a href="#cli_enable_source_maps"><code>--enable-source-maps</code></a></li>
902 <li><a href="#cli_experimental_abortcontroller"><code>--experimental-abortcontroller</code></a></li>
903 <li><a href="#cli_experimental_import_meta_resolve"><code>--experimental-import-meta-resolve</code>…
904 <li><a href="#cli_experimental_json_modules"><code>--experimental-json-modules</code></a></li>
905 <li><a href="#cli_experimental_loader_module"><code>--experimental-loader=module</code></a></li>
906 <li><a href="#cli_experimental_modules"><code>--experimental-modules</code></a></li>
907 <li><a href="#cli_experimental_policy"><code>--experimental-policy</code></a></li>
908 <li><a href="#cli_experimental_repl_await"><code>--experimental-repl-await</code></a></li>
909 <li><a href="#cli_experimental_specifier_resolution_mode"><code>--experimental-specifier-resolution…
910 <li><a href="#cli_experimental_vm_modules"><code>--experimental-vm-modules</code></a></li>
911 <li><a href="#cli_experimental_wasi_unstable_preview1"><code>--experimental-wasi-unstable-preview1<…
912 <li><a href="#cli_experimental_wasm_modules"><code>--experimental-wasm-modules</code></a></li>
913 <li><a href="#cli_force_context_aware"><code>--force-context-aware</code></a></li>
914 <li><a href="#cli_force_fips"><code>--force-fips</code></a></li>
915 <li><span class="stability_1"><a href="#cli_frozen_intrinsics"><code>--frozen-intrinsics</code></a>…
916 …_1"><a href="#cli_heapsnapshot_near_heap_limit_max_count"><code>--heapsnapshot-near-heap-limit=max…
917 <li><a href="#cli_heapsnapshot_signal_signal"><code>--heapsnapshot-signal=signal</code></a></li>
918 <li><span class="stability_1"><a href="#cli_heap_prof"><code>--heap-prof</code></a></span></li>
919 <li><span class="stability_1"><a href="#cli_heap_prof_dir"><code>--heap-prof-dir</code></a></span><…
920 <li><span class="stability_1"><a href="#cli_heap_prof_interval"><code>--heap-prof-interval</code></…
921 <li><span class="stability_1"><a href="#cli_heap_prof_name"><code>--heap-prof-name</code></a></span…
922 <li><a href="#cli_icu_data_dir_file"><code>--icu-data-dir=file</code></a></li>
923 <li><a href="#cli_input_type_type"><code>--input-type=type</code></a></li>
924 <li><a href="#cli_inspect_brk_host_port"><code>--inspect-brk[=[host:]port]</code></a></li>
925 <li><a href="#cli_inspect_port_host_port"><code>--inspect-port=[host:]port</code></a></li>
926 <li><a href="#cli_inspect_host_port"><code>--inspect[=[host:]port]</code></a>
931 <li><a href="#cli_inspect_publish_uid_stderr_http"><code>--inspect-publish-uid=stderr,http</code></…
932 <li><a href="#cli_insecure_http_parser"><code>--insecure-http-parser</code></a></li>
933 <li><a href="#cli_jitless"><code>--jitless</code></a></li>
934 <li><a href="#cli_max_http_header_size_size"><code>--max-http-header-size=size</code></a></li>
935 <li><a href="#cli_napi_modules"><code>--napi-modules</code></a></li>
936 <li><a href="#cli_no_addons"><code>--no-addons</code></a></li>
937 <li><a href="#cli_no_deprecation"><code>--no-deprecation</code></a></li>
938 <li><a href="#cli_no_force_async_hooks_checks"><code>--no-force-async-hooks-checks</code></a></li>
939 <li><a href="#cli_no_warnings"><code>--no-warnings</code></a></li>
940 <li><a href="#cli_node_memory_debug"><code>--node-memory-debug</code></a></li>
941 <li><a href="#cli_openssl_config_file"><code>--openssl-config=file</code></a></li>
942 <li><a href="#cli_pending_deprecation"><code>--pending-deprecation</code></a></li>
943 <li><span class="stability_1"><a href="#cli_policy_integrity_sri"><code>--policy-integrity=sri</cod…
944 <li><a href="#cli_preserve_symlinks"><code>--preserve-symlinks</code></a></li>
945 <li><a href="#cli_preserve_symlinks_main"><code>--preserve-symlinks-main</code></a></li>
946 <li><a href="#cli_prof"><code>--prof</code></a></li>
947 <li><a href="#cli_prof_process"><code>--prof-process</code></a></li>
948 <li><a href="#cli_redirect_warnings_file"><code>--redirect-warnings=file</code></a></li>
949 <li><a href="#cli_report_compact"><code>--report-compact</code></a></li>
950 …eport_dir_directory_report_directory_directory"><code>--report-dir=directory</code>, <code>report-
951 <li><a href="#cli_report_filename_filename"><code>--report-filename=filename</code></a></li>
952 <li><a href="#cli_report_on_fatalerror"><code>--report-on-fatalerror</code></a></li>
953 <li><a href="#cli_report_on_signal"><code>--report-on-signal</code></a></li>
954 <li><a href="#cli_report_signal_signal"><code>--report-signal=signal</code></a></li>
955 <li><a href="#cli_report_uncaught_exception"><code>--report-uncaught-exception</code></a></li>
956 <li><a href="#cli_throw_deprecation"><code>--throw-deprecation</code></a></li>
957 <li><a href="#cli_title_title"><code>--title=title</code></a></li>
958 <li><a href="#cli_tls_cipher_list_list"><code>--tls-cipher-list=list</code></a></li>
959 <li><a href="#cli_tls_keylog_file"><code>--tls-keylog=file</code></a></li>
960 <li><a href="#cli_tls_max_v1_2"><code>--tls-max-v1.2</code></a></li>
961 <li><a href="#cli_tls_max_v1_3"><code>--tls-max-v1.3</code></a></li>
962 <li><a href="#cli_tls_min_v1_0"><code>--tls-min-v1.0</code></a></li>
963 <li><a href="#cli_tls_min_v1_1"><code>--tls-min-v1.1</code></a></li>
964 <li><a href="#cli_tls_min_v1_2"><code>--tls-min-v1.2</code></a></li>
965 <li><a href="#cli_tls_min_v1_3"><code>--tls-min-v1.3</code></a></li>
966 <li><a href="#cli_trace_atomics_wait"><code>--trace-atomics-wait</code></a></li>
967 <li><a href="#cli_trace_deprecation"><code>--trace-deprecation</code></a></li>
968 <li><a href="#cli_trace_event_categories"><code>--trace-event-categories</code></a></li>
969 <li><a href="#cli_trace_event_file_pattern"><code>--trace-event-file-pattern</code></a></li>
970 <li><a href="#cli_trace_events_enabled"><code>--trace-events-enabled</code></a></li>
971 <li><a href="#cli_trace_exit"><code>--trace-exit</code></a></li>
972 <li><a href="#cli_trace_sigint"><code>--trace-sigint</code></a></li>
973 <li><a href="#cli_trace_sync_io"><code>--trace-sync-io</code></a></li>
974 <li><a href="#cli_trace_tls"><code>--trace-tls</code></a></li>
975 <li><a href="#cli_trace_uncaught"><code>--trace-uncaught</code></a></li>
976 <li><a href="#cli_trace_warnings"><code>--trace-warnings</code></a></li>
977 <li><a href="#cli_track_heap_objects"><code>--track-heap-objects</code></a></li>
978 <li><a href="#cli_unhandled_rejections_mode"><code>--unhandled-rejections=mode</code></a></li>
979 …li><a href="#cli_use_bundled_ca_use_openssl_ca"><code>--use-bundled-ca</code>, <code>--use-openssl
980 <li><a href="#cli_use_largepages_mode"><code>--use-largepages=mode</code></a></li>
981 <li><a href="#cli_v8_options"><code>--v8-options</code></a></li>
982 <li><a href="#cli_v8_pool_size_num"><code>--v8-pool-size=num</code></a></li>
983 <li><a href="#cli_zero_fill_buffers"><code>--zero-fill-buffers</code></a></li>
984 <li><a href="#cli_c_check"><code>-c</code>, <code>--check</code></a></li>
985 <li><a href="#cli_e_eval_script"><code>-e</code>, <code>--eval "script"</code></a></li>
986 <li><a href="#cli_h_help"><code>-h</code>, <code>--help</code></a></li>
987 <li><a href="#cli_i_interactive"><code>-i</code>, <code>--interactive</code></a></li>
988 <li><a href="#cli_p_print_script"><code>-p</code>, <code>--print "script"</code></a></li>
989 <li><a href="#cli_r_require_module"><code>-r</code>, <code>--require module</code></a></li>
990 <li><a href="#cli_v_version"><code>-v</code>, <code>--version</code></a></li>
1027 <li><a href="#cli_max_old_space_size_size_in_megabytes"><code>--max-old-space-size=SIZE</code> (in …
1082 <li><a href="#corepack_running_npm_install_g_yarn_doesn_t_work">Running <code>npm install -g yarn</…
1332 <li><a href="#DEP0062">DEP0062: <code>node --debug</code></a></li>
1360 <li><a href="#DEP0092">DEP0092: Top-level <code>this</code> bound to <code>module.exports</code></a…
1367 <li><a href="#DEP0099">DEP0099: Async context-unaware <code>node::MakeCallback</code> C++ APIs</a><…
1369 <li><a href="#DEP0101">DEP0101: <code>--with-lttng</code></a></li>
1449 <li><a href="#dns_resolver_setlocaladdress_ipv4_ipv6"><code>resolver.setLocalAddress([ipv4][, ipv6]…
1535 <li><a href="#errors_error_first_callbacks">Error-first callbacks</a></li>
1951 <li><a href="#events_event_target"><code>event.target</code></a></li>
2030 <li><a href="#fs_fspromises_symlink_target_path_type"><code>fsPromises.symlink(target, path[, type]…
2086 <li><a href="#fs_fs_symlink_target_path_type_callback"><code>fs.symlink(target, path[, type], callb…
2150 <li><a href="#fs_fs_symlinksync_target_path_type"><code>fs.symlinkSync(target, path[, type])</code>…
2274 …g_of_callback_and_promise_based_operations">Ordering of callback and promise-based operations</a><…
2280 <li><a href="#fs_platform_specific_considerations">Platform-specific considerations</a></li>
2284 <li><a href="#fs_per_drive_working_directories_on_windows">Per-drive working directories on Windows…
2511 <li><a href="#http2_server_side_example">Server-side example</a></li>
2512 <li><a href="#http2_client_side_example">Client-side example</a></li>
2773 <li><a href="#inspector_event_inspector_protocol_method">Event: <code>&#x3C;inspector-protocol-meth…
2793 …tl_build_with_a_pre_installed_icu_system_icu">Build with a pre-installed ICU (<code>system-icu</co…
2794 …d_a_limited_set_of_icu_data_small_icu">Embed a limited set of ICU data (<code>small-icu</code>)</a>
2799 <li><a href="#intl_embed_the_entire_icu_full_icu">Embed the entire ICU (<code>full-icu</code>)</a><…
2912 <li><span class="stability_1"><a href="#esm_top_level_await">Top-level <code>await</code></a></span…
2972 <li><a href="#packages_input_type_flag"><code>--input-type</code> flag</a></li>
2987 <li><a href="#packages_self_referencing_a_package_using_its_name">Self-referencing a package using …
3158 <li><a href="#os_windows_specific_error_constants">Windows-specific error constants</a></li>
3487 <li><a href="#readline_example_read_file_stream_line_by_line">Example: Read file stream line-by-Lin…
3506 <li><a href="#repl_reverse_i_search">Reverse-i-search</a></li>
3532 …node_js_repl_with_advanced_line_editors">Using the Node.js REPL with advanced line-editors</a></li>
3751 <li><a href="#tls_pre_shared_keys">Pre-shared keys</a></li>
3752 <li><a href="#tls_client_initiated_renegotiation_attack_mitigation">Client-initiated renegotiation …
3919 <li><a href="#dgram_example_ipv6_outgoing_multicast_interface">Example: IPv6 outgoing multicast int…
4024 <li><a href="#url_percent_encoding_in_urls">Percent-encoding in URLs</a>
4074 …l_icu_option">Encodings supported when Node.js is built with the <code>small-icu</code> option</a>…
4139 <li><span class="stability_0"><a href="#util_util_extend_target_source"><code>util._extend(target, …
4339 <li><a href="#zlib_for_zlib_based_streams">For zlib-based streams</a></li>
4340 <li><a href="#zlib_for_brotli_based_streams">For Brotli-based streams</a></li>
4430 <p></p><div class="api_stability api_stability_0">Stability: 0 - Deprecated. The feature may emit w…
4432 <!-- separator -->
4433 <p></p><div class="api_stability api_stability_1">Stability: 1 - Experimental. The feature is not s…
4434 <a href="https://semver.org/">Semantic Versioning</a> rules. Non-backward compatible changes or rem…
4437 <!-- separator -->
4438 <p></p><div class="api_stability api_stability_2">Stability: 2 - Stable. Compatibility with the npm…
4440 <!-- separator -->
4441 <p></p><div class="api_stability api_stability_3">Stability: 3 - Legacy. The feature is no longer r…
4442 likely will not be removed, and is still covered by semantic-versioning
4448 a command-line flag. Experimental features may also emit a <a href="#process_event_warning">warning…
4450 <!-- STABILITY_OVERVIEW_SLOT_BEGIN -->
4451 <!-- STABILITY_OVERVIEW_SLOT_END -->
4468 <p><code>node [options] [V8 options] [script.js | -e "script" | - ] [arguments]</code></p>
4469 <p>Please see the <a href="#cli_command_line_options">Command-line options</a> document for more in…
4479 <a href="https://nodejs.org/en/download/package-manager/">Installing Node.js via package manager</a…
4482 <pre><code class="language-console"><span class="hljs-meta">$ </span><span class="language-bash">mk…
4483 <span class="hljs-meta">$ </span><span class="language-bash"><span class="hljs-built_in">cd</span> …
4485 <pre><code class="language-console"><span class="hljs-meta">> </span><span class="language-bash">mk…
4486 <span class="hljs-meta">> </span><span class="language-bash"><span class="hljs-built_in">cd</span> …
4488 <pre><code class="language-console"><span class="hljs-meta">> </span><span class="language-bash">mk…
4489 <span class="hljs-meta">> </span><span class="language-bash"><span class="hljs-built_in">cd</span> …
4491 folder and call it <code>hello-world.js</code>.</p>
4492 <p>Open <code>hello-world.js</code> in any preferred text editor and
4494 …code class="language-js"><span class="hljs-keyword">const</span> http = <span class="hljs-built_in…
4496 <span class="hljs-keyword">const</span> hostname = <span class="hljs-string">'127.0.0.1'</span>;
4497 <span class="hljs-keyword">const</span> port = <span class="hljs-number">3000</span>;
4499 …s="hljs-keyword">const</span> server = http.<span class="hljs-title function_">createServer</span>…
4500 res.<span class="hljs-property">statusCode</span> = <span class="hljs-number">200</span>;
4501 …res.<span class="hljs-title function_">setHeader</span>(<span class="hljs-string">'Content-Type'</…
4502 …res.<span class="hljs-title function_">end</span>(<span class="hljs-string">'Hello, World!\n'</spa…
4505 server.<span class="hljs-title function_">listen</span>(port, hostname, <span class="hljs-function"…
4506-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hlj…
4509 <pre><code class="language-console"><span class="hljs-meta">$ </span><span class="language-bash">no…
4511 <pre><code class="language-console">Server running at http://127.0.0.1:3000/</code></pre>
4518 …ty api_stability_2"><a href="#documentation_stability_index">Stability: 2</a> - Stable</div><p></p>
4538 <p>In strict assertion mode, non-strict methods behave like their corresponding
4544-js"><span class="hljs-keyword">const</span> assert = <span class="hljs-built_in">require</span>(<…
4546-js"><span class="hljs-keyword">const</span> assert = <span class="hljs-built_in">require</span>(<…
4548-title function_">deepEqual</span>([[[<span class="hljs-number">1</span>, <span class="hljs-number…
4549 <span class="hljs-comment">// AssertionError: Expected inputs to be strictly deep-equal:</span>
4550 <span class="hljs-comment">// + actual - expected ... Lines skipped</span>
4551 <span class="hljs-comment">//</span>
4552 <span class="hljs-comment">// [</span>
4553 <span class="hljs-comment">// [</span>
4554 <span class="hljs-comment">// ...</span>
4555 <span class="hljs-comment">// 2,</span>
4556 <span class="hljs-comment">// + 3</span>
4557 <span class="hljs-comment">// - '3'</span>
4558 <span class="hljs-comment">// ],</span>
4559 <span class="hljs-comment">// ...</span>
4560 <span class="hljs-comment">// 5</span>
4561 <span class="hljs-comment">// ]</span></code></pre>
4567 <p>Legacy assertion mode uses the <a href="https://tc39.github.io/ecma262/#sec-abstract-equality-co…
4575 …ode class="language-js"><span class="hljs-keyword">const</span> assert = <span class="hljs-built_i…
4577 <a href="https://tc39.github.io/ecma262/#sec-abstract-equality-comparison">Abstract Equality Compar…
4580 <pre><code class="language-js"><span class="hljs-comment">// WARNING: This does not throw an Assert…
4581 …class="hljs-title function_">deepEqual</span>(<span class="hljs-regexp">/a/gi</span>, <span class=…
4593 <li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference…
4595 <li><code>message</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_stru…
4596 <li><code>actual</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struc…
4597 <li><code>expected</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_str…
4598 <li><code>operator</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_str…
4599 <li><code>stackStartFn</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Refe…
4605 <p>All instances contain the built-in <code>Error</code> properties (<code>message</code> and <code…
4608 <li><code>actual</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struc…
4610 <li><code>expected</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_str…
4612 …r.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type">&#x3C;boolean><…
4614 <li><code>code</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
4616 <li><code>operator</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_str…
4618 …ode class="language-js"><span class="hljs-keyword">const</span> assert = <span class="hljs-built_i…
4620 <span class="hljs-comment">// Generate an AssertionError to compare the error message later:</span>
4621 <span class="hljs-keyword">const</span> { message } = <span class="hljs-keyword">new</span> assert.…
4622 <span class="hljs-attr">actual</span>: <span class="hljs-number">1</span>,
4623 <span class="hljs-attr">expected</span>: <span class="hljs-number">2</span>,
4624 <span class="hljs-attr">operator</span>: <span class="hljs-string">'strictEqual'</span>
4627 <span class="hljs-comment">// Verify error output:</span>
4628 <span class="hljs-keyword">try</span> {
4629 …assert.<span class="hljs-title function_">strictEqual</span>(<span class="hljs-number">1</span>, <…
4630 } <span class="hljs-keyword">catch</span> (err) {
4631 …<span class="hljs-title function_">assert</span>(err <span class="hljs-keyword">instanceof</span> …
4632 …assert.<span class="hljs-title function_">strictEqual</span>(err.<span class="hljs-property">messa…
4633 …assert.<span class="hljs-title function_">strictEqual</span>(err.<span class="hljs-property">name<…
4634 …assert.<span class="hljs-title function_">strictEqual</span>(err.<span class="hljs-property">actua…
4635 …assert.<span class="hljs-title function_">strictEqual</span>(err.<span class="hljs-property">expec…
4636 …assert.<span class="hljs-title function_">strictEqual</span>(err.<span class="hljs-property">code<…
4637 …assert.<span class="hljs-title function_">strictEqual</span>(err.<span class="hljs-property">opera…
4638 …rt.<span class="hljs-title function_">strictEqual</span>(err.<span class="hljs-property">generated…
4644 … api_stability_1"><a href="#documentation_stability_index">Stability: 1</a> - Experimental</div><p…
4654 …ode class="language-js"><span class="hljs-keyword">const</span> assert = <span class="hljs-built_i…
4656 <span class="hljs-keyword">const</span> tracker = <span class="hljs-keyword">new</span> assert.<spa…
4658 <span class="hljs-keyword">function</span> <span class="hljs-title function_">func</span>(<span cla…
4660 <span class="hljs-comment">// callsfunc() must be called exactly 1 time before tracker.verify().</s…
4661 <span class="hljs-keyword">const</span> callsfunc = tracker.<span class="hljs-title function_">call…
4663 <span class="hljs-title function_">callsfunc</span>();
4665 <span class="hljs-comment">// Calls tracker.verify() and verifies if all tracker.calls() functions …
4666 <span class="hljs-comment">// been called exact times.</span>
4667 process.<span class="hljs-title function_">on</span>(<span class="hljs-string">'exit'</span>, <span…
4668 tracker.<span class="hljs-title function_">verify</span>();
4675 ….mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type">&#x3C;Funct…
4676 <li><code>exact</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struct…
4677 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Obje…
4683 …ode class="language-js"><span class="hljs-keyword">const</span> assert = <span class="hljs-built_i…
4685 <span class="hljs-comment">// Creates call tracker.</span>
4686 <span class="hljs-keyword">const</span> tracker = <span class="hljs-keyword">new</span> assert.<spa…
4688 <span class="hljs-keyword">function</span> <span class="hljs-title function_">func</span>(<span cla…
4690 <span class="hljs-comment">// Returns a function that wraps func() that must be called exact times<…
4691 <span class="hljs-comment">// before tracker.verify().</span>
4692 <span class="hljs-keyword">const</span> callsfunc = tracker.<span class="hljs-title function_">call…
4698 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Obje…
4700 <li>Object <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Object…
4702 <li><code>message</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_stru…
4703 <li><code>actual</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struc…
4704 <li><code>expected</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_str…
4706 <li><code>operator</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_str…
4707 <li><code>stack</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/G…
4713 …ode class="language-js"><span class="hljs-keyword">const</span> assert = <span class="hljs-built_i…
4715 <span class="hljs-comment">// Creates call tracker.</span>
4716 <span class="hljs-keyword">const</span> tracker = <span class="hljs-keyword">new</span> assert.<spa…
4718 <span class="hljs-keyword">function</span> <span class="hljs-title function_">func</span>(<span cla…
4720 <span class="hljs-keyword">function</span> <span class="hljs-title function_">foo</span>(<span clas…
4722 <span class="hljs-comment">// Returns a function that wraps func() that must be called exact times<…
4723 <span class="hljs-comment">// before tracker.verify().</span>
4724 <span class="hljs-keyword">const</span> callsfunc = tracker.<span class="hljs-title function_">call…
4726 <span class="hljs-comment">// Returns an array containing information on callsfunc()</span>
4727 tracker.<span class="hljs-title function_">report</span>();
4728 <span class="hljs-comment">// [</span>
4729 <span class="hljs-comment">// {</span>
4730 <span class="hljs-comment">// message: 'Expected the func function to be executed 2 time(s) but …
4731 <span class="hljs-comment">// executed 0 time(s).',</span>
4732 <span class="hljs-comment">// actual: 0,</span>
4733 <span class="hljs-comment">// expected: 2,</span>
4734 <span class="hljs-comment">// operator: 'func',</span>
4735 <span class="hljs-comment">// stack: stack trace</span>
4736 <span class="hljs-comment">// }</span>
4737 <span class="hljs-comment">// ]</span></code></pre>
4745 …ode class="language-js"><span class="hljs-keyword">const</span> assert = <span class="hljs-built_i…
4747 <span class="hljs-comment">// Creates call tracker.</span>
4748 <span class="hljs-keyword">const</span> tracker = <span class="hljs-keyword">new</span> assert.<spa…
4750 <span class="hljs-keyword">function</span> <span class="hljs-title function_">func</span>(<span cla…
4752 <span class="hljs-comment">// Returns a function that wraps func() that must be called exact times<…
4753 <span class="hljs-comment">// before tracker.verify().</span>
4754 <span class="hljs-keyword">const</span> callsfunc = tracker.<span class="hljs-title function_">call…
4756 <span class="hljs-title function_">callsfunc</span>();
4758 <span class="hljs-comment">// Will throw an error since callsfunc() was only called once.</span>
4759 tracker.<span class="hljs-title function_">verify</span>();</code></pre>
4765 <li><code>value</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struct…
4766 …zilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | …
4789 <td><p>Handle non-<code>Uint8Array</code> typed arrays correctly.</p></td></tr>
4796 <li><code>actual</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struc…
4797 <li><code>expected</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_str…
4798 …zilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | …
4803 … api_stability_3"><a href="#documentation_stability_index">Stability: 3</a> - Legacy: Use <a href=…
4811 … are compared with the <a href="https://tc39.github.io/ecma262/#sec-abstract-equality-comparison">…
4814 <li><a href="https://tc39.github.io/ecma262/#sec-object.prototype.tostring">Type tags</a> of object…
4815 <li>Only <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Enumerability_and_ownersh…
4818 <li><a href="https://developer.mozilla.org/en-US/docs/Glossary/Primitive#Primitive_wrapper_objects_…
4820 …ozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Map"><code>Map</code></a> keys and <…
4823 …test the <a href="https://tc39.github.io/ecma262/#sec-ordinary-object-internal-methods-and-interna…
4825 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Symbo…
4826 …zilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/WeakMap"><code>WeakMap</code></a> and…
4829 …onsidered equal by the <a href="https://tc39.github.io/ecma262/#sec-abstract-equality-comparison">…
4831 <pre><code class="language-js"><span class="hljs-comment">// WARNING: This does not throw an Assert…
4832 assert.<span class="hljs-title function_">deepEqual</span>(<span class="hljs-string">'+00000000'</s…
4835 …ode class="language-js"><span class="hljs-keyword">const</span> assert = <span class="hljs-built_i…
4837 <span class="hljs-keyword">const</span> obj1 = {
4838 <span class="hljs-attr">a</span>: {
4839 <span class="hljs-attr">b</span>: <span class="hljs-number">1</span>
4842 <span class="hljs-keyword">const</span> obj2 = {
4843 <span class="hljs-attr">a</span>: {
4844 <span class="hljs-attr">b</span>: <span class="hljs-number">2</span>
4847 <span class="hljs-keyword">const</span> obj3 = {
4848 <span class="hljs-attr">a</span>: {
4849 <span class="hljs-attr">b</span>: <span class="hljs-number">1</span>
4852 <span class="hljs-keyword">const</span> obj4 = <span class="hljs-title class_">Object</span>.<span …
4854 assert.<span class="hljs-title function_">deepEqual</span>(obj1, obj1);
4855 <span class="hljs-comment">// OK</span>
4857 <span class="hljs-comment">// Values of b are different:</span>
4858 assert.<span class="hljs-title function_">deepEqual</span>(obj1, obj2);
4859 <span class="hljs-comment">// AssertionError: { a: { b: 1 } } deepEqual { a: { b: 2 } }</span>
4861 assert.<span class="hljs-title function_">deepEqual</span>(obj1, obj3);
4862 <span class="hljs-comment">// OK</span>
4864 <span class="hljs-comment">// Prototypes are ignored:</span>
4865 assert.<span class="hljs-title function_">deepEqual</span>(obj1, obj4);
4866 <span class="hljs-comment">// AssertionError: { a: { b: 1 } } deepEqual {}</span></code></pre>
4880 <td><p>The <code>NaN</code> is now compared using the <a href="https://tc39.github.io/ecma262/#sec-
4890 <td><p>Handle non-<code>Uint8Array</code> typed arrays correctly.</p></td></tr>
4897 <li><code>actual</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struc…
4898 <li><code>expected</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_str…
4899 …zilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | …
4906 <li>Primitive values are compared using the <a href="https://tc39.github.io/ecma262/#sec-samevalue"…
4907 <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/is…
4908 <li><a href="https://tc39.github.io/ecma262/#sec-object.prototype.tostring">Type tags</a> of object…
4909 <li><a href="https://tc39.github.io/ecma262/#sec-ordinary-object-internal-methods-and-internal-slot…
4910 the <a href="https://tc39.github.io/ecma262/#sec-strict-equality-comparison">Strict Equality Compar…
4911 <li>Only <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Enumerability_and_ownersh…
4914 <li>Enumerable own <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Globa…
4915 <li><a href="https://developer.mozilla.org/en-US/docs/Glossary/Primitive#Primitive_wrapper_objects_…
4917 …ozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Map"><code>Map</code></a> keys and <…
4920 …zilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/WeakMap"><code>WeakMap</code></a> and…
4923-js"><span class="hljs-keyword">const</span> assert = <span class="hljs-built_in">require</span>(<…
4925 <span class="hljs-comment">// This fails because 1 !== '1'.</span>
4926-title function_">deepStrictEqual</span>({ <span class="hljs-attr">a</span>: <span class="hljs-num…
4927 <span class="hljs-comment">// AssertionError: Expected inputs to be strictly deep-equal:</span>
4928 <span class="hljs-comment">// + actual - expected</span>
4929 <span class="hljs-comment">//</span>
4930 <span class="hljs-comment">// {</span>
4931 <span class="hljs-comment">// + a: 1</span>
4932 <span class="hljs-comment">// - a: '1'</span>
4933 <span class="hljs-comment">// }</span>
4935 <span class="hljs-comment">// The following objects don't have own properties</span>
4936 <span class="hljs-keyword">const</span> date = <span class="hljs-keyword">new</span> <span class="h…
4937 <span class="hljs-keyword">const</span> object = {};
4938 <span class="hljs-keyword">const</span> fakeDate = {};
4939-title class_">Object</span>.<span class="hljs-title function_">setPrototypeOf</span>(fakeDate, <s…
4941 <span class="hljs-comment">// Different [[Prototype]]:</span>
4942 assert.<span class="hljs-title function_">deepStrictEqual</span>(object, fakeDate);
4943 <span class="hljs-comment">// AssertionError: Expected inputs to be strictly deep-equal:</span>
4944 <span class="hljs-comment">// + actual - expected</span>
4945 <span class="hljs-comment">//</span>
4946 <span class="hljs-comment">// + {}</span>
4947 <span class="hljs-comment">// - Date {}</span>
4949 <span class="hljs-comment">// Different type tags:</span>
4950 assert.<span class="hljs-title function_">deepStrictEqual</span>(date, fakeDate);
4951 <span class="hljs-comment">// AssertionError: Expected inputs to be strictly deep-equal:</span>
4952 <span class="hljs-comment">// + actual - expected</span>
4953 <span class="hljs-comment">//</span>
4954 <span class="hljs-comment">// + 2018-04-26T00:49:08.604Z</span>
4955 <span class="hljs-comment">// - Date {}</span>
4957 assert.<span class="hljs-title function_">deepStrictEqual</span>(<span class="hljs-title class_">Na…
4958 <span class="hljs-comment">// OK, because of the SameValue comparison</span>
4960 <span class="hljs-comment">// Different unwrapped numbers:</span>
4961-title function_">deepStrictEqual</span>(<span class="hljs-keyword">new</span> <span class="hljs-t…
4962 <span class="hljs-comment">// AssertionError: Expected inputs to be strictly deep-equal:</span>
4963 <span class="hljs-comment">// + actual - expected</span>
4964 <span class="hljs-comment">//</span>
4965 <span class="hljs-comment">// + [Number: 1]</span>
4966 <span class="hljs-comment">// - [Number: 2]</span>
4968-title function_">deepStrictEqual</span>(<span class="hljs-keyword">new</span> <span class="hljs-t…
4969 <span class="hljs-comment">// OK because the object and the string are identical when unwrapped.</s…
4971 assert.<span class="hljs-title function_">deepStrictEqual</span>(-<span class="hljs-number">0</span…
4972 <span class="hljs-comment">// OK</span>
4974 <span class="hljs-comment">// Different zeros using the SameValue Comparison:</span>
4975 assert.<span class="hljs-title function_">deepStrictEqual</span>(<span class="hljs-number">0</span>…
4976 <span class="hljs-comment">// AssertionError: Expected inputs to be strictly deep-equal:</span>
4977 <span class="hljs-comment">// + actual - expected</span>
4978 <span class="hljs-comment">//</span>
4979 <span class="hljs-comment">// + 0</span>
4980 <span class="hljs-comment">// - -0</span>
4982 <span class="hljs-keyword">const</span> symbol1 = <span class="hljs-title class_">Symbol</span>();
4983 <span class="hljs-keyword">const</span> symbol2 = <span class="hljs-title class_">Symbol</span>();
4984 …pan class="hljs-title function_">deepStrictEqual</span>({ [symbol1]: <span class="hljs-number">1</…
4985 <span class="hljs-comment">// OK, because it is the same symbol on both objects.</span>
4987 …pan class="hljs-title function_">deepStrictEqual</span>({ [symbol1]: <span class="hljs-number">1</…
4988 <span class="hljs-comment">// AssertionError [ERR_ASSERTION]: Inputs identical but not reference eq…
4989 <span class="hljs-comment">//</span>
4990 <span class="hljs-comment">// {</span>
4991 <span class="hljs-comment">// [Symbol()]: 1</span>
4992 <span class="hljs-comment">// }</span>
4994 <span class="hljs-keyword">const</span> weakMap1 = <span class="hljs-keyword">new</span> <span clas…
4995 <span class="hljs-keyword">const</span> weakMap2 = <span class="hljs-keyword">new</span> <span clas…
4996 <span class="hljs-keyword">const</span> weakMap3 = <span class="hljs-keyword">new</span> <span clas…
4997 weakMap3.<span class="hljs-property">unequal</span> = <span class="hljs-literal">true</span>;
4999 assert.<span class="hljs-title function_">deepStrictEqual</span>(weakMap1, weakMap2);
5000 <span class="hljs-comment">// OK, because it is impossible to compare the entries</span>
5002 <span class="hljs-comment">// Fails because weakMap3 has a property that weakMap1 does not contain:…
5003 assert.<span class="hljs-title function_">deepStrictEqual</span>(weakMap1, weakMap3);
5004 <span class="hljs-comment">// AssertionError: Expected inputs to be strictly deep-equal:</span>
5005 <span class="hljs-comment">// + actual - expected</span>
5006 <span class="hljs-comment">//</span>
5007 <span class="hljs-comment">// WeakMap {</span>
5008 <span class="hljs-comment">// + [items unknown]</span>
5009 <span class="hljs-comment">// - [items unknown],</span>
5010 <span class="hljs-comment">// - unequal: true</span>
5011 <span class="hljs-comment">// }</span></code></pre>
5022 <li><code>string</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struc…
5023 <li><code>regexp</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/…
5024 …zilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | …
5026 … api_stability_1"><a href="#documentation_stability_index">Stability: 1</a> - Experimental</div><p…
5030-js"><span class="hljs-keyword">const</span> assert = <span class="hljs-built_in">require</span>(<…
5032 assert.<span class="hljs-title function_">doesNotMatch</span>(<span class="hljs-string">'I will fai…
5033 <span class="hljs-comment">// AssertionError [ERR_ASSERTION]: The input was expected to not match t…
5035 assert.<span class="hljs-title function_">doesNotMatch</span>(<span class="hljs-number">123</span>,…
5036 <span class="hljs-comment">// AssertionError [ERR_ASSERTION]: The "string" argument must be of type…
5038 assert.<span class="hljs-title function_">doesNotMatch</span>(<span class="hljs-string">'I will pas…
5039 <span class="hljs-comment">// OK</span></code></pre>
5051 …a.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type">&#x3C;Function></a…
5052 …lla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp" class="type">&#x3C;RegExp></a> …
5053 <li><code>message</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_stru…
5067 …veloper.mozilla.org/en-US/docs/Web/JavaScript/Reference/Classes"><code>Class</code></a>, <a href="…
5071 <!-- eslint-disable no-restricted-syntax -->
5072 <pre><code class="language-js">(<span class="hljs-keyword">async</span> () => {
5073 …<span class="hljs-keyword">await</span> assert.<span class="hljs-title function_">doesNotReject</s…
5074 <span class="hljs-keyword">async</span> () => {
5075 … class="hljs-keyword">throw</span> <span class="hljs-keyword">new</span> <span class="hljs-title c…
5077 <span class="hljs-title class_">SyntaxError</span>
5080 <!-- eslint-disable no-restricted-syntax -->
5081-js">assert.<span class="hljs-title function_">doesNotReject</span>(<span class="hljs-title class_…
5082 .<span class="hljs-title function_">then</span>(<span class="hljs-function">() =></span> {
5083 <span class="hljs-comment">// ...</span>
5100 <li><code>fn</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Glob…
5101 …lla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp" class="type">&#x3C;RegExp></a> …
5102 <li><code>message</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_stru…
5115 …veloper.mozilla.org/en-US/docs/Web/JavaScript/Reference/Classes"><code>Class</code></a>, <a href="…
5119 <!-- eslint-disable no-restricted-syntax -->
5120 <pre><code class="language-js">assert.<span class="hljs-title function_">doesNotThrow</span>(
5121 <span class="hljs-function">() =></span> {
5122 … class="hljs-keyword">throw</span> <span class="hljs-keyword">new</span> <span class="hljs-title c…
5124 <span class="hljs-title class_">SyntaxError</span>
5128 <!-- eslint-disable no-restricted-syntax -->
5129 <pre><code class="language-js">assert.<span class="hljs-title function_">doesNotThrow</span>(
5130 <span class="hljs-function">() =></span> {
5131 … class="hljs-keyword">throw</span> <span class="hljs-keyword">new</span> <span class="hljs-title c…
5133 <span class="hljs-title class_">TypeError</span>
5138 <!-- eslint-disable no-restricted-syntax -->
5139 <pre><code class="language-js">assert.<span class="hljs-title function_">doesNotThrow</span>(
5140 <span class="hljs-function">() =></span> {
5141 … class="hljs-keyword">throw</span> <span class="hljs-keyword">new</span> <span class="hljs-title c…
5143 <span class="hljs-regexp">/Wrong value/</span>,
5144 <span class="hljs-string">'Whoops'</span>
5146 <span class="hljs-comment">// Throws: AssertionError: Got unwanted exception: Whoops</span></code><…
5162 <li><code>actual</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struc…
5163 <li><code>expected</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_str…
5164 …zilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | …
5169 … api_stability_3"><a href="#documentation_stability_index">Stability: 3</a> - Legacy: Use <a href=…
5171 using the <a href="https://tc39.github.io/ecma262/#sec-abstract-equality-comparison">Abstract Equal…
5173 …ode class="language-js"><span class="hljs-keyword">const</span> assert = <span class="hljs-built_i…
5175 assert.<span class="hljs-title function_">equal</span>(<span class="hljs-number">1</span>, <span cl…
5176 <span class="hljs-comment">// OK, 1 == 1</span>
5177 assert.<span class="hljs-title function_">equal</span>(<span class="hljs-number">1</span>, <span cl…
5178 <span class="hljs-comment">// OK, 1 == '1'</span>
5179 assert.<span class="hljs-title function_">equal</span>(<span class="hljs-title class_">Na</span>N, …
5180 <span class="hljs-comment">// OK</span>
5182 assert.<span class="hljs-title function_">equal</span>(<span class="hljs-number">1</span>, <span cl…
5183 <span class="hljs-comment">// AssertionError: 1 == 2</span>
5184-title function_">equal</span>({ <span class="hljs-attr">a</span>: { <span class="hljs-attr">b</sp…
5185 <span class="hljs-comment">// AssertionError: { a: { b: 1 } } == { a: { b: 1 } }</span></code></pre>
5196 …zilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | …
5201-js"><span class="hljs-keyword">const</span> assert = <span class="hljs-built_in">require</span>(<…
5203 assert.<span class="hljs-title function_">fail</span>();
5204 <span class="hljs-comment">// AssertionError [ERR_ASSERTION]: Failed</span>
5206 assert.<span class="hljs-title function_">fail</span>(<span class="hljs-string">'boom'</span>);
5207 <span class="hljs-comment">// AssertionError [ERR_ASSERTION]: boom</span>
5209 …ass="hljs-title function_">fail</span>(<span class="hljs-keyword">new</span> <span class="hljs-tit…
5210 <span class="hljs-comment">// TypeError: need array</span></code></pre>
5225 … api_stability_0"><a href="#documentation_stability_index">Stability: 0</a> - Deprecated: Use <cod…
5228 <li><code>actual</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struc…
5229 <li><code>expected</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_str…
5230 …zilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | …
5231 <li><code>operator</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_str…
5232 <li><code>stackStartFn</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Refe…
5242-js"><span class="hljs-keyword">const</span> assert = <span class="hljs-built_in">require</span>(<…
5244 assert.<span class="hljs-title function_">fail</span>(<span class="hljs-string">'a'</span>, <span c…
5245 <span class="hljs-comment">// AssertionError [ERR_ASSERTION]: 'a' != 'b'</span>
5247-title function_">fail</span>(<span class="hljs-number">1</span>, <span class="hljs-number">2</spa…
5248 <span class="hljs-comment">// AssertionError [ERR_ASSERTION]: 1 > 2</span>
5250 …span class="hljs-title function_">fail</span>(<span class="hljs-number">1</span>, <span class="hlj…
5251 <span class="hljs-comment">// AssertionError [ERR_ASSERTION]: fail</span>
5253-title function_">fail</span>(<span class="hljs-number">1</span>, <span class="hljs-number">2</spa…
5254 <span class="hljs-comment">// AssertionError [ERR_ASSERTION]: whoops</span>
5256-title function_">fail</span>(<span class="hljs-number">1</span>, <span class="hljs-number">2</spa…
5257 <span class="hljs-comment">// TypeError: need array</span></code></pre>
5261 …class="language-js"><span class="hljs-keyword">function</span> <span class="hljs-title function_">…
5262-title function_">fail</span>(<span class="hljs-string">'a'</span>, <span class="hljs-string">'b'<…
5264 <span class="hljs-title function_">suppressFrame</span>();
5265 <span class="hljs-comment">// AssertionError [ERR_ASSERTION]: 'a' !== 'b'</span>
5266 <span class="hljs-comment">// at repl:1:1</span>
5267 <span class="hljs-comment">// at ContextifyScript.Script.runInThisContext (vm.js:44:33)</span>
5268 <span class="hljs-comment">// ...</span></code></pre>
5284 <li><code>value</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struct…
5290-js"><span class="hljs-keyword">const</span> assert = <span class="hljs-built_in">require</span>(<…
5292 assert.<span class="hljs-title function_">ifError</span>(<span class="hljs-literal">null</span>);
5293 <span class="hljs-comment">// OK</span>
5294 assert.<span class="hljs-title function_">ifError</span>(<span class="hljs-number">0</span>);
5295 <span class="hljs-comment">// AssertionError [ERR_ASSERTION]: ifError got unwanted exception: 0</sp…
5296 assert.<span class="hljs-title function_">ifError</span>(<span class="hljs-string">'error'</span>);
5297 <span class="hljs-comment">// AssertionError [ERR_ASSERTION]: ifError got unwanted exception: 'erro…
5298 assert.<span class="hljs-title function_">ifError</span>(<span class="hljs-keyword">new</span> <spa…
5299 <span class="hljs-comment">// AssertionError [ERR_ASSERTION]: ifError got unwanted exception: Error…
5301 <span class="hljs-comment">// Create some random error frames.</span>
5302 <span class="hljs-keyword">let</span> err;
5303 (<span class="hljs-keyword">function</span> <span class="hljs-title function_">errorFrame</span>(<s…
5304 …err = <span class="hljs-keyword">new</span> <span class="hljs-title class_">Error</span>(<span cla…
5307 (<span class="hljs-keyword">function</span> <span class="hljs-title function_">ifErrorFrame</span>(…
5308 assert.<span class="hljs-title function_">ifError</span>(err);
5310 <span class="hljs-comment">// AssertionError [ERR_ASSERTION]: ifError got unwanted exception: test …
5311 <span class="hljs-comment">// at ifErrorFrame</span>
5312 <span class="hljs-comment">// at errorFrame</span></code></pre>
5318 <li><code>string</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struc…
5319 <li><code>regexp</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/…
5320 …zilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | …
5322 … api_stability_1"><a href="#documentation_stability_index">Stability: 1</a> - Experimental</div><p…
5326-js"><span class="hljs-keyword">const</span> assert = <span class="hljs-built_in">require</span>(<…
5328 assert.<span class="hljs-title function_">match</span>(<span class="hljs-string">'I will fail'</spa…
5329 <span class="hljs-comment">// AssertionError [ERR_ASSERTION]: The input did not match the regular .…
5331 assert.<span class="hljs-title function_">match</span>(<span class="hljs-number">123</span>, <span …
5332 <span class="hljs-comment">// AssertionError [ERR_ASSERTION]: The "string" argument must be of type…
5334 assert.<span class="hljs-title function_">match</span>(<span class="hljs-string">'I will pass'</spa…
5335 <span class="hljs-comment">// OK</span></code></pre>
5360 <td><p>Handle non-<code>Uint8Array</code> typed arrays correctly.</p></td></tr>
5367 <li><code>actual</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struc…
5368 <li><code>expected</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_str…
5369 …zilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | …
5374 … api_stability_3"><a href="#documentation_stability_index">Stability: 3</a> - Legacy: Use <a href=…
5376 …ode class="language-js"><span class="hljs-keyword">const</span> assert = <span class="hljs-built_i…
5378 <span class="hljs-keyword">const</span> obj1 = {
5379 <span class="hljs-attr">a</span>: {
5380 <span class="hljs-attr">b</span>: <span class="hljs-number">1</span>
5383 <span class="hljs-keyword">const</span> obj2 = {
5384 <span class="hljs-attr">a</span>: {
5385 <span class="hljs-attr">b</span>: <span class="hljs-number">2</span>
5388 <span class="hljs-keyword">const</span> obj3 = {
5389 <span class="hljs-attr">a</span>: {
5390 <span class="hljs-attr">b</span>: <span class="hljs-number">1</span>
5393 <span class="hljs-keyword">const</span> obj4 = <span class="hljs-title class_">Object</span>.<span …
5395 assert.<span class="hljs-title function_">notDeepEqual</span>(obj1, obj1);
5396 <span class="hljs-comment">// AssertionError: { a: { b: 1 } } notDeepEqual { a: { b: 1 } }</span>
5398 assert.<span class="hljs-title function_">notDeepEqual</span>(obj1, obj2);
5399 <span class="hljs-comment">// OK</span>
5401 assert.<span class="hljs-title function_">notDeepEqual</span>(obj1, obj3);
5402 <span class="hljs-comment">// AssertionError: { a: { b: 1 } } notDeepEqual { a: { b: 1 } }</span>
5404 assert.<span class="hljs-title function_">notDeepEqual</span>(obj1, obj4);
5405 <span class="hljs-comment">// OK</span></code></pre>
5417 <td><p>The <code>-0</code> and <code>+0</code> are not considered equal anymore.</p></td></tr>
5419 <td><p>The <code>NaN</code> is now compared using the <a href="https://tc39.github.io/ecma262/#sec-
5429 <td><p>Handle non-<code>Uint8Array</code> typed arrays correctly.</p></td></tr>
5436 <li><code>actual</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struc…
5437 <li><code>expected</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_str…
5438 …zilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | …
5441-js"><span class="hljs-keyword">const</span> assert = <span class="hljs-built_in">require</span>(<…
5443-title function_">notDeepStrictEqual</span>({ <span class="hljs-attr">a</span>: <span class="hljs-
5444 <span class="hljs-comment">// OK</span></code></pre>
5465 <li><code>actual</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struc…
5466 <li><code>expected</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_str…
5467 …zilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | …
5472 … api_stability_3"><a href="#documentation_stability_index">Stability: 3</a> - Legacy: Use <a href=…
5473 …ve inequality with the <a href="https://tc39.github.io/ecma262/#sec-abstract-equality-comparison">…
5476 …ode class="language-js"><span class="hljs-keyword">const</span> assert = <span class="hljs-built_i…
5478 assert.<span class="hljs-title function_">notEqual</span>(<span class="hljs-number">1</span>, <span…
5479 <span class="hljs-comment">// OK</span>
5481 assert.<span class="hljs-title function_">notEqual</span>(<span class="hljs-number">1</span>, <span…
5482 <span class="hljs-comment">// AssertionError: 1 != 1</span>
5484 assert.<span class="hljs-title function_">notEqual</span>(<span class="hljs-number">1</span>, <span…
5485 <span class="hljs-comment">// AssertionError: 1 != '1'</span></code></pre>
5504 <li><code>actual</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struc…
5505 <li><code>expected</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_str…
5506 …zilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | …
5509 determined by the <a href="https://tc39.github.io/ecma262/#sec-samevalue">SameValue Comparison</a>.…
5510-js"><span class="hljs-keyword">const</span> assert = <span class="hljs-built_in">require</span>(<…
5512 assert.<span class="hljs-title function_">notStrictEqual</span>(<span class="hljs-number">1</span>,…
5513 <span class="hljs-comment">// OK</span>
5515 assert.<span class="hljs-title function_">notStrictEqual</span>(<span class="hljs-number">1</span>,…
5516 <span class="hljs-comment">// AssertionError [ERR_ASSERTION]: Expected "actual" to be strictly uneq…
5517 <span class="hljs-comment">//</span>
5518 <span class="hljs-comment">// 1</span>
5520 assert.<span class="hljs-title function_">notStrictEqual</span>(<span class="hljs-number">1</span>,…
5521 <span class="hljs-comment">// OK</span></code></pre>
5540 <li><code>value</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struct…
5541 …zilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | …
5554-js"><span class="hljs-keyword">const</span> assert = <span class="hljs-built_in">require</span>(<…
5556 assert.<span class="hljs-title function_">ok</span>(<span class="hljs-literal">true</span>);
5557 <span class="hljs-comment">// OK</span>
5558 assert.<span class="hljs-title function_">ok</span>(<span class="hljs-number">1</span>);
5559 <span class="hljs-comment">// OK</span>
5561 assert.<span class="hljs-title function_">ok</span>();
5562 <span class="hljs-comment">// AssertionError: No value argument passed to `assert.ok()`</span>
5564 assert.<span class="hljs-title function_">ok</span>(<span class="hljs-literal">false</span>, <span …
5565 <span class="hljs-comment">// AssertionError: it's false</span>
5567 <span class="hljs-comment">// In the repl:</span>
5568 … class="hljs-title function_">ok</span>(<span class="hljs-keyword">typeof</span> <span class="hljs
5569 <span class="hljs-comment">// AssertionError: false == true</span>
5571 <span class="hljs-comment">// In a file (e.g. test.js):</span>
5572 … class="hljs-title function_">ok</span>(<span class="hljs-keyword">typeof</span> <span class="hljs
5573 <span class="hljs-comment">// AssertionError: The expression evaluated to a falsy value:</span>
5574 <span class="hljs-comment">//</span>
5575 <span class="hljs-comment">// assert.ok(typeof 123 === 'string')</span>
5577 assert.<span class="hljs-title function_">ok</span>(<span class="hljs-literal">false</span>);
5578 <span class="hljs-comment">// AssertionError: The expression evaluated to a falsy value:</span>
5579 <span class="hljs-comment">//</span>
5580 <span class="hljs-comment">// assert.ok(false)</span>
5582 assert.<span class="hljs-title function_">ok</span>(<span class="hljs-number">0</span>);
5583 <span class="hljs-comment">// AssertionError: The expression evaluated to a falsy value:</span>
5584 <span class="hljs-comment">//</span>
5585 <span class="hljs-comment">// assert.ok(0)</span>
5587 <span class="hljs-comment">// Using `assert()` works the same:</span>
5588 <span class="hljs-title function_">assert</span>(<span class="hljs-number">0</span>);
5589 <span class="hljs-comment">// AssertionError: The expression evaluated to a falsy value:</span>
5590 <span class="hljs-comment">//</span>
5591 <span class="hljs-comment">// assert(0)</span></code></pre>
5597 …a.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type">&#x3C;Function></a…
5598-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp" class="type">&#x3C;RegExp></a> | <a href=…
5599 <li><code>message</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_stru…
5611 …veloper.mozilla.org/en-US/docs/Web/JavaScript/Reference/Classes"><code>Class</code></a>, <a href="…
5613 each property will be tested for including the non-enumerable <code>message</code> and
5617 <pre><code class="language-js">(<span class="hljs-keyword">async</span> () => {
5618 <span class="hljs-keyword">await</span> assert.<span class="hljs-title function_">rejects</span>(
5619 <span class="hljs-keyword">async</span> () => {
5620 … class="hljs-keyword">throw</span> <span class="hljs-keyword">new</span> <span class="hljs-title c…
5623 <span class="hljs-attr">name</span>: <span class="hljs-string">'TypeError'</span>,
5624 <span class="hljs-attr">message</span>: <span class="hljs-string">'Wrong value'</span>
5628 <pre><code class="language-js">(<span class="hljs-keyword">async</span> () => {
5629 <span class="hljs-keyword">await</span> assert.<span class="hljs-title function_">rejects</span>(
5630 <span class="hljs-keyword">async</span> () => {
5631 … class="hljs-keyword">throw</span> <span class="hljs-keyword">new</span> <span class="hljs-title c…
5633 <span class="hljs-function">(<span class="hljs-params">err</span>) =></span> {
5634 …assert.<span class="hljs-title function_">strictEqual</span>(err.<span class="hljs-property">name<…
5635 …assert.<span class="hljs-title function_">strictEqual</span>(err.<span class="hljs-property">messa…
5636 <span class="hljs-keyword">return</span> <span class="hljs-literal">true</span>;
5640 <pre><code class="language-js">assert.<span class="hljs-title function_">rejects</span>(
5641-title class_">Promise</span>.<span class="hljs-title function_">reject</span>(<span class="hljs-k…
5642 <span class="hljs-title class_">Error</span>
5643 ).<span class="hljs-title function_">then</span>(<span class="hljs-function">() =></span> {
5644 <span class="hljs-comment">// ...</span>
5648 <code>message</code> instead. This can lead to easy-to-miss mistakes. Please read the
5664 <li><code>actual</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struc…
5665 <li><code>expected</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_str…
5666 …zilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | …
5669 determined by the <a href="https://tc39.github.io/ecma262/#sec-samevalue">SameValue Comparison</a>.…
5670-js"><span class="hljs-keyword">const</span> assert = <span class="hljs-built_in">require</span>(<…
5672 assert.<span class="hljs-title function_">strictEqual</span>(<span class="hljs-number">1</span>, <s…
5673 <span class="hljs-comment">// AssertionError [ERR_ASSERTION]: Expected inputs to be strictly equal:…
5674 <span class="hljs-comment">//</span>
5675 <span class="hljs-comment">// 1 !== 2</span>
5677 assert.<span class="hljs-title function_">strictEqual</span>(<span class="hljs-number">1</span>, <s…
5678 <span class="hljs-comment">// OK</span>
5680 assert.<span class="hljs-title function_">strictEqual</span>(<span class="hljs-string">'Hello fooba…
5681 <span class="hljs-comment">// AssertionError [ERR_ASSERTION]: Expected inputs to be strictly equal:…
5682 <span class="hljs-comment">// + actual - expected</span>
5683 <span class="hljs-comment">//</span>
5684 <span class="hljs-comment">// + 'Hello foobar'</span>
5685 <span class="hljs-comment">// - 'Hello World!'</span>
5686 <span class="hljs-comment">// ^</span>
5688 <span class="hljs-keyword">const</span> apples = <span class="hljs-number">1</span>;
5689 <span class="hljs-keyword">const</span> oranges = <span class="hljs-number">2</span>;
5690-title function_">strictEqual</span>(apples, oranges, <span class="hljs-string">`apples <span clas…
5691 <span class="hljs-comment">// AssertionError [ERR_ASSERTION]: apples 1 !== oranges 2</span>
5693-title function_">strictEqual</span>(<span class="hljs-number">1</span>, <span class="hljs-string"…
5694 <span class="hljs-comment">// TypeError: Inputs are not identical</span></code></pre>
5717 <li><code>fn</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Glob…
5718-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp" class="type">&#x3C;RegExp></a> | <a href=…
5719 <li><code>message</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_stru…
5722 …veloper.mozilla.org/en-US/docs/Web/JavaScript/Reference/Classes"><code>Class</code></a>, <a href="…
5725 equality including the non-enumerable <code>message</code> and <code>name</code> properties. When
5732-js"><span class="hljs-keyword">const</span> err = <span class="hljs-keyword">new</span> <span cla…
5733 err.<span class="hljs-property">code</span> = <span class="hljs-number">404</span>;
5734 err.<span class="hljs-property">foo</span> = <span class="hljs-string">'bar'</span>;
5735 err.<span class="hljs-property">info</span> = {
5736 <span class="hljs-attr">nested</span>: <span class="hljs-literal">true</span>,
5737 <span class="hljs-attr">baz</span>: <span class="hljs-string">'text'</span>
5739 err.<span class="hljs-property">reg</span> = <span class="hljs-regexp">/abc/i</span>;
5741 assert.<span class="hljs-title function_">throws</span>(
5742 <span class="hljs-function">() =></span> {
5743 <span class="hljs-keyword">throw</span> err;
5746 <span class="hljs-attr">name</span>: <span class="hljs-string">'TypeError'</span>,
5747 <span class="hljs-attr">message</span>: <span class="hljs-string">'Wrong value'</span>,
5748 <span class="hljs-attr">info</span>: {
5749 <span class="hljs-attr">nested</span>: <span class="hljs-literal">true</span>,
5750 <span class="hljs-attr">baz</span>: <span class="hljs-string">'text'</span>
5752 … <span class="hljs-comment">// Only properties on the validation object will be tested for.</span>
5753 …<span class="hljs-comment">// Using nested objects requires all properties to be present. Otherwis…
5754 <span class="hljs-comment">// the validation is going to fail.</span>
5758 <span class="hljs-comment">// Using regular expressions to validate error properties:</span>
5759 assert.<span class="hljs-title function_">throws</span>(
5760 <span class="hljs-function">() =></span> {
5761 <span class="hljs-keyword">throw</span> err;
5764 …<span class="hljs-comment">// The `name` and `message` properties are strings and using regular</s…
5765 …<span class="hljs-comment">// expressions on those will match against the string. If they fail, an…
5766 <span class="hljs-comment">// error is thrown.</span>
5767 <span class="hljs-attr">name</span>: <span class="hljs-regexp">/^TypeError$/</span>,
5768 <span class="hljs-attr">message</span>: <span class="hljs-regexp">/Wrong/</span>,
5769 <span class="hljs-attr">foo</span>: <span class="hljs-string">'bar'</span>,
5770 <span class="hljs-attr">info</span>: {
5771 <span class="hljs-attr">nested</span>: <span class="hljs-literal">true</span>,
5772 …<span class="hljs-comment">// It is not possible to use regular expressions for nested properties!…
5773 <span class="hljs-attr">baz</span>: <span class="hljs-string">'text'</span>
5775 …<span class="hljs-comment">// The `reg` property contains a regular expression and only if the</sp…
5776 …<span class="hljs-comment">// validation object contains an identical regular expression, it is go…
5777 <span class="hljs-comment">// to pass.</span>
5778 <span class="hljs-attr">reg</span>: <span class="hljs-regexp">/abc/i</span>
5782 <span class="hljs-comment">// Fails due to the different `message` and `name` properties:</span>
5783 assert.<span class="hljs-title function_">throws</span>(
5784 <span class="hljs-function">() =></span> {
5785 …ass="hljs-keyword">const</span> otherErr = <span class="hljs-keyword">new</span> <span class="hljs
5786 <span class="hljs-comment">// Copy all enumerable properties from `err` to `otherErr`.</span>
5787-keyword">for</span> (<span class="hljs-keyword">const</span> [key, value] <span class="hljs-keywo…
5790 <span class="hljs-keyword">throw</span> otherErr;
5792 …<span class="hljs-comment">// The error's `message` and `name` properties will also be checked whe…
5793 <span class="hljs-comment">// an error as validation object.</span>
5797 <pre><code class="language-js">assert.<span class="hljs-title function_">throws</span>(
5798 <span class="hljs-function">() =></span> {
5799 …an class="hljs-keyword">throw</span> <span class="hljs-keyword">new</span> <span class="hljs-title…
5801 <span class="hljs-title class_">Error</span>
5803 <p>Validate error message using <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Gu…
5806 <pre><code class="language-js">assert.<span class="hljs-title function_">throws</span>(
5807 <span class="hljs-function">() =></span> {
5808 …an class="hljs-keyword">throw</span> <span class="hljs-keyword">new</span> <span class="hljs-title…
5810 <span class="hljs-regexp">/^Error: Wrong value$/</span>
5815 <pre><code class="language-js">assert.<span class="hljs-title function_">throws</span>(
5816 <span class="hljs-function">() =></span> {
5817 …an class="hljs-keyword">throw</span> <span class="hljs-keyword">new</span> <span class="hljs-title…
5819 <span class="hljs-function">(<span class="hljs-params">err</span>) =></span> {
5820 …<span class="hljs-title function_">assert</span>(err <span class="hljs-keyword">instanceof</span> …
5821 …<span class="hljs-title function_">assert</span>(<span class="hljs-regexp">/value/</span>.<span cl…
5822 …<span class="hljs-comment">// Avoid returning anything from validation functions besides `true`.</…
5823 …<span class="hljs-comment">// Otherwise, it's not clear what part of the validation failed. Instea…
5824 …<span class="hljs-comment">// throw an error about the specific validation that failed (as done in…
5825 …<span class="hljs-comment">// example) and add as much helpful debugging information to that error…
5826 <span class="hljs-comment">// possible.</span>
5827 <span class="hljs-keyword">return</span> <span class="hljs-literal">true</span>;
5829 <span class="hljs-string">'unexpected error'</span>
5833 <code>message</code> instead. This can lead to easy-to-miss mistakes. Using the same
5837 <!-- eslint-disable no-restricted-syntax -->
5838 …class="language-js"><span class="hljs-keyword">function</span> <span class="hljs-title function_">…
5839 …an class="hljs-keyword">throw</span> <span class="hljs-keyword">new</span> <span class="hljs-title…
5842 <span class="hljs-keyword">function</span> <span class="hljs-title function_">throwingSecond</span>…
5843 …an class="hljs-keyword">throw</span> <span class="hljs-keyword">new</span> <span class="hljs-title…
5846 <span class="hljs-keyword">function</span> <span class="hljs-title function_">notThrowing</span>(<s…
5848 <span class="hljs-comment">// The second argument is a string and the input function threw an Error…
5849 <span class="hljs-comment">// The first case will not throw as it does not match for the error mess…
5850 <span class="hljs-comment">// thrown by the input function!</span>
5851 assert.<span class="hljs-title function_">throws</span>(throwingFirst, <span class="hljs-string">'S…
5852 <span class="hljs-comment">// In the next example the message has no benefit over the message from …
5853 <span class="hljs-comment">// error and since it is not clear if the user intended to actually matc…
5854 <span class="hljs-comment">// against the error message, Node.js throws an `ERR_AMBIGUOUS_ARGUMENT`…
5855 assert.<span class="hljs-title function_">throws</span>(throwingSecond, <span class="hljs-string">'…
5856 <span class="hljs-comment">// TypeError [ERR_AMBIGUOUS_ARGUMENT]</span>
5858 <span class="hljs-comment">// The string is only used (as message) in case the function does not th…
5859 assert.<span class="hljs-title function_">throws</span>(notThrowing, <span class="hljs-string">'Sec…
5860 <span class="hljs-comment">// AssertionError [ERR_ASSERTION]: Missing expected exception: Second</s…
5862 <span class="hljs-comment">// If it was intended to match for the error message do this instead:</s…
5863 <span class="hljs-comment">// It does not throw because the error messages match.</span>
5864 assert.<span class="hljs-title function_">throws</span>(throwingSecond, <span class="hljs-regexp">/…
5866 <span class="hljs-comment">// If the error message does not match, an AssertionError is thrown.</sp…
5867 assert.<span class="hljs-title function_">throws</span>(throwingFirst, <span class="hljs-regexp">/S…
5868 <span class="hljs-comment">// AssertionError [ERR_ASSERTION]</span></code></pre>
5869 <p>Due to the confusing error-prone notation, avoid a string as the second
5874 … api_stability_1"><a href="#documentation_stability_index">Stability: 1</a> - Experimental</div><p…
5878 … class="language-js"><span class="hljs-keyword">const</span> async_hooks = <span class="hljs-built…
5890 … class="language-js"><span class="hljs-keyword">const</span> async_hooks = <span class="hljs-built…
5892 <span class="hljs-comment">// Return the ID of the current execution context.</span>
5893 <span class="hljs-keyword">const</span> eid = async_hooks.<span class="hljs-title function_">execut…
5895 <span class="hljs-comment">// Return the ID of the handle responsible for triggering the callback o…
5896 <span class="hljs-comment">// current execution scope to call.</span>
5897 <span class="hljs-keyword">const</span> tid = async_hooks.<span class="hljs-title function_">trigge…
5899 <span class="hljs-comment">// Create a new AsyncHook instance. All of these callbacks are optional.…
5900 <span class="hljs-keyword">const</span> asyncHook =
5901 …async_hooks.<span class="hljs-title function_">createHook</span>({ init, before, after, destroy, p…
5903 <span class="hljs-comment">// Allow callbacks of this AsyncHook instance to call. This is not an im…
5904 <span class="hljs-comment">// action after running the constructor, and must be explicitly run to b…
5905 <span class="hljs-comment">// executing callbacks.</span>
5906 asyncHook.<span class="hljs-title function_">enable</span>();
5908 <span class="hljs-comment">// Disable listening for new asynchronous events.</span>
5909 asyncHook.<span class="hljs-title function_">disable</span>();
5911 <span class="hljs-comment">//</span>
5912 <span class="hljs-comment">// The following are the callbacks that can be passed to createHook().</…
5913 <span class="hljs-comment">//</span>
5915 <span class="hljs-comment">// init is called during object construction. The resource may not have<…
5916 <span class="hljs-comment">// completed construction when this callback runs, therefore all fields …
5917 <span class="hljs-comment">// resource referenced by "asyncId" may not have been populated.</span>
5918 <span class="hljs-keyword">function</span> <span class="hljs-title function_">init</span>(<span cla…
5920 <span class="hljs-comment">// Before is called just before the resource's callback is called. It ca…
5921 <span class="hljs-comment">// called 0-N times for handles (such as TCPWrap), and will be called ex…
5922 <span class="hljs-comment">// time for requests (such as FSReqCallback).</span>
5923 <span class="hljs-keyword">function</span> <span class="hljs-title function_">before</span>(<span c…
5925 <span class="hljs-comment">// After is called just after the resource's callback has finished.</spa…
5926 <span class="hljs-keyword">function</span> <span class="hljs-title function_">after</span>(<span cl…
5928 <span class="hljs-comment">// Destroy is called when the resource is destroyed.</span>
5929 <span class="hljs-keyword">function</span> <span class="hljs-title function_">destroy</span>(<span …
5931 <span class="hljs-comment">// promiseResolve is called only for promise resources, when the</span>
5932 <span class="hljs-comment">// `resolve` function passed to the `Promise` constructor is invoked</sp…
5933 <span class="hljs-comment">// (either directly or through other means of resolving a promise).</spa…
5934 <span class="hljs-keyword">function</span> <span class="hljs-title function_">promiseResolve</span>…
5940 <li><code>callbacks</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Referen…
5942 <li><code>init</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Gl…
5943 <li><code>before</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/…
5944 <li><code>after</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/G…
5945 <li><code>destroy</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference…
5946 <li><code>promiseResolve</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Re…
5959 … class="language-js"><span class="hljs-keyword">const</span> async_hooks = <span class="hljs-built…
5961 <span class="hljs-keyword">const</span> asyncHook = async_hooks.<span class="hljs-title function_">…
5962 …<span class="hljs-title function_">init</span>(<span class="hljs-params">asyncId, type, triggerAsy…
5963 <span class="hljs-title function_">destroy</span>(<span class="hljs-params">asyncId</span>) { }
5966 <pre><code class="language-js"><span class="hljs-keyword">class</span> <span class="hljs-title clas…
5967 …<span class="hljs-title function_">init</span>(<span class="hljs-params">asyncId, type, triggerAsy…
5968 <span class="hljs-title function_">destroy</span>(<span class="hljs-params">asyncId</span>) {}
5971 …s="hljs-keyword">class</span> <span class="hljs-title class_">MyAddedCallbacks</span> <span class=…
5972 <span class="hljs-title function_">before</span>(<span class="hljs-params">asyncId</span>) { }
5973 <span class="hljs-title function_">after</span>(<span class="hljs-params">asyncId</span>) { }
5976-keyword">const</span> asyncHook = async_hooks.<span class="hljs-title function_">createHook</span…
5985 with <code>--abort-on-uncaught-exception</code>, in which case a stack trace will be
6002 …<code class="language-js"><span class="hljs-keyword">const</span> fs = <span class="hljs-built_in"…
6003 <span class="hljs-keyword">const</span> util = <span class="hljs-built_in">require</span>(<span cla…
6005 <span class="hljs-keyword">function</span> <span class="hljs-title function_">debug</span>(<span cl…
6006 …<span class="hljs-comment">// Use a function like this one when debugging inside an AsyncHooks cal…
6007-title function_">writeFileSync</span>(<span class="hljs-string">'log.out'</span>, <span class="hl…
6022 provided, enabling is a no-op.</p>
6025 … class="language-js"><span class="hljs-keyword">const</span> async_hooks = <span class="hljs-built…
6027 …an class="hljs-keyword">const</span> hook = async_hooks.<span class="hljs-title function_">createH…
6042 <li><code>asyncId</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_stru…
6043 <li><code>type</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
6044 <li><code>triggerAsyncId</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Da…
6046 <li><code>resource</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Referenc…
6056-js"><span class="hljs-built_in">require</span>(<span class="hljs-string">'net'</span>).<span clas…
6057 <span class="hljs-comment">// OR</span>
6058 …="hljs-built_in">clearTimeout</span>(<span class="hljs-built_in">setTimeout</span>(<span class="hl…
6065 <pre><code class="language-text">FSEVENTWRAP, FSREQCALLBACK, GETADDRINFOREQWRAP, GETNAMEINFOREQWRAP…
6082 <pre><code class="language-js"><span class="hljs-keyword">const</span> { fd } = process.<span class…
6084 async_hooks.<span class="hljs-title function_">createHook</span>({
6085 …<span class="hljs-title function_">init</span>(<span class="hljs-params">asyncId, type, triggerAsy…
6086 …<span class="hljs-keyword">const</span> eid = async_hooks.<span class="hljs-title function_">execu…
6087 fs.<span class="hljs-title function_">writeSync</span>(
6089-string">`<span class="hljs-subst">${type}</span>(<span class="hljs-subst">${asyncId}</span>): tri…
6091 }).<span class="hljs-title function_">enable</span>();
6093-title function_">createServer</span>(<span class="hljs-function">(<span class="hljs-params">conn<…
6095 <pre><code class="language-console">TCPSERVERWRAP(5): trigger: 1 execution: 1
6121 <pre><code class="language-js"><span class="hljs-keyword">const</span> { fd } = process.<span class…
6123 <span class="hljs-keyword">let</span> indent = <span class="hljs-number">0</span>;
6124 async_hooks.<span class="hljs-title function_">createHook</span>({
6125 …<span class="hljs-title function_">init</span>(<span class="hljs-params">asyncId, type, triggerAsy…
6126 …<span class="hljs-keyword">const</span> eid = async_hooks.<span class="hljs-title function_">execu…
6127 …<span class="hljs-keyword">const</span> indentStr = <span class="hljs-string">' '</span>.<span cla…
6128 fs.<span class="hljs-title function_">writeSync</span>(
6130 …<span class="hljs-string">`<span class="hljs-subst">${indentStr}</span><span class="hljs-subst">${…
6131 …<span class="hljs-string">` trigger: <span class="hljs-subst">${triggerAsyncId}</span> execution: …
6133 <span class="hljs-title function_">before</span>(<span class="hljs-params">asyncId</span>) {
6134 …<span class="hljs-keyword">const</span> indentStr = <span class="hljs-string">' '</span>.<span cla…
6135 …ss="hljs-title function_">writeSync</span>(fd, <span class="hljs-string">`<span class="hljs-subst"…
6136 indent += <span class="hljs-number">2</span>;
6138 <span class="hljs-title function_">after</span>(<span class="hljs-params">asyncId</span>) {
6139 indent -= <span class="hljs-number">2</span>;
6140 …<span class="hljs-keyword">const</span> indentStr = <span class="hljs-string">' '</span>.<span cla…
6141 …ss="hljs-title function_">writeSync</span>(fd, <span class="hljs-string">`<span class="hljs-subst"…
6143 <span class="hljs-title function_">destroy</span>(<span class="hljs-params">asyncId</span>) {
6144 …<span class="hljs-keyword">const</span> indentStr = <span class="hljs-string">' '</span>.<span cla…
6145 …s="hljs-title function_">writeSync</span>(fd, <span class="hljs-string">`<span class="hljs-subst">…
6147 }).<span class="hljs-title function_">enable</span>();
6149-title function_">createServer</span>(<span class="hljs-function">() =></span> {}).<span class="hl…
6150 <span class="hljs-comment">// Let's wait 10ms before logging the server started.</span>
6151 <span class="hljs-built_in">setTimeout</span>(<span class="hljs-function">() =></span> {
6152-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hlj…
6153 }, <span class="hljs-number">10</span>);
6156 <pre><code class="language-console">TCPSERVERWRAP(5): trigger: 1 execution: 1
6163 <span class="hljs-meta">></span><span class="language-bash">>> 7</span>
6172 <pre><code class="language-console"> root(1)
6188 <pre><code class="language-console"> bootstrap(1)
6200 <li><code>asyncId</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_stru…
6215 <li><code>asyncId</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_stru…
6223 <li><code>asyncId</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_stru…
6236 <li><code>asyncId</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_stru…
6243-js"><span class="hljs-keyword">new</span> <span class="hljs-title class_">Promise</span>(<span cl…
6245 <pre><code class="language-text">init for PROMISE with id 5, trigger id: 1
6256 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Obje…
6262 <p>Using <code>executionAsyncResource()</code> in the top-level execution context will
6264 but having an object representing the top-level can be helpful.</p>
6265 …de class="language-js"><span class="hljs-keyword">const</span> { open } = <span class="hljs-built_…
6266 …class="hljs-keyword">const</span> { executionAsyncId, executionAsyncResource } = <span class="hljs
6268-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hlj…
6269 …n class="hljs-title function_">open</span>(__filename, <span class="hljs-string">'r'</span>, <span…
6270-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hlj…
6274 …lass="language-js"><span class="hljs-keyword">const</span> { createServer } = <span class="hljs-bu…
6275 <span class="hljs-keyword">const</span> {
6279 } = <span class="hljs-built_in">require</span>(<span class="hljs-string">'async_hooks'</span>);
6280 …ss="hljs-keyword">const</span> sym = <span class="hljs-title class_">Symbol</span>(<span class="hl…
6282 <span class="hljs-title function_">createHook</span>({
6283 …<span class="hljs-title function_">init</span>(<span class="hljs-params">asyncId, type, triggerAsy…
6284 …<span class="hljs-keyword">const</span> cr = <span class="hljs-title function_">executionAsyncReso…
6285 <span class="hljs-keyword">if</span> (cr) {
6289 }).<span class="hljs-title function_">enable</span>();
6291 …ass="hljs-keyword">const</span> server = <span class="hljs-title function_">createServer</span>(<s…
6292 …<span class="hljs-title function_">executionAsyncResource</span>()[sym] = { <span class="hljs-attr…
6293 …<span class="hljs-built_in">setTimeout</span>(<span class="hljs-keyword">function</span>(<span cla…
6294 …js-title function_">end</span>(<span class="hljs-variable constant_">JSON</span>.<span class="hljs
6295 }, <span class="hljs-number">100</span>);
6296 }).<span class="hljs-title function_">listen</span>(<span class="hljs-number">3000</span>);</code><…
6310 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Numbe…
6313 … class="language-js"><span class="hljs-keyword">const</span> async_hooks = <span class="hljs-built…
6315-variable language_">console</span>.<span class="hljs-title function_">log</span>(async_hooks.<spa…
6316 …span class="hljs-title function_">open</span>(path, <span class="hljs-string">'r'</span>, <span cl…
6317-variable language_">console</span>.<span class="hljs-title function_">log</span>(async_hooks.<spa…
6321-js"><span class="hljs-keyword">const</span> server = net.<span class="hljs-title function_">creat…
6322 …<span class="hljs-comment">// Returns the ID of the server, not of the new connection, because the…
6323 …<span class="hljs-comment">// callback runs in the execution scope of the server's MakeCallback().…
6324 async_hooks.<span class="hljs-title function_">executionAsyncId</span>();
6326 }).<span class="hljs-title function_">listen</span>(port, <span class="hljs-function">() =></span> {
6327 …<span class="hljs-comment">// Returns the ID of a TickObject (process.nextTick()) because all</spa…
6328 <span class="hljs-comment">// callbacks passed to .listen() are wrapped in a nextTick().</span>
6329 async_hooks.<span class="hljs-title function_">executionAsyncId</span>();
6335 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Numbe…
6338-js"><span class="hljs-keyword">const</span> server = net.<span class="hljs-title function_">creat…
6339 …<span class="hljs-comment">// The resource that caused (or triggered) this callback to be called</…
6340 …<span class="hljs-comment">// was that of the new connection. Thus the return value of triggerAsyn…
6341 <span class="hljs-comment">// is the asyncId of "conn".</span>
6342 async_hooks.<span class="hljs-title function_">triggerAsyncId</span>();
6344 }).<span class="hljs-title function_">listen</span>(port, <span class="hljs-function">() =></span> {
6345 …<span class="hljs-comment">// Even though all callbacks passed to .listen() are wrapped in a nextT…
6346 …<span class="hljs-comment">// the callback itself exists because the call to the server's .listen(…
6347 <span class="hljs-comment">// was made. So the return value would be the ID of the server.</span>
6348 async_hooks.<span class="hljs-title function_">triggerAsyncId</span>();
6357 …<code class="language-js"><span class="hljs-keyword">const</span> ah = <span class="hljs-built_in"…
6358-title class_">Promise</span>.<span class="hljs-title function_">resolve</span>(<span class="hljs-
6359-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hlj…
6361 <span class="hljs-comment">// produces:</span>
6362 <span class="hljs-comment">// eid 1 tid 0</span></code></pre>
6369 …<code class="language-js"><span class="hljs-keyword">const</span> ah = <span class="hljs-built_in"…
6370-title function_">createHook</span>({ <span class="hljs-title function_">init</span>(<span class="…
6371-title class_">Promise</span>.<span class="hljs-title function_">resolve</span>(<span class="hljs-
6372-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hlj…
6374 <span class="hljs-comment">// produces:</span>
6375 <span class="hljs-comment">// eid 7 tid 6</span></code></pre>
6397-js"><span class="hljs-keyword">const</span> { <span class="hljs-title class_">AsyncResource</span…
6399 <span class="hljs-comment">// AsyncResource() is meant to be extended. Instantiating a</span>
6400 <span class="hljs-comment">// new AsyncResource() also triggers init. If triggerAsyncId is omitted …
6401 <span class="hljs-comment">// async_hook.executionAsyncId() is used.</span>
6402 <span class="hljs-keyword">const</span> asyncResource = <span class="hljs-keyword">new</span> <span…
6403-attr">triggerAsyncId</span>: <span class="hljs-title function_">executionAsyncId</span>(), <span …
6406 <span class="hljs-comment">// Run a function in the execution context of the resource. This will</s…
6407 <span class="hljs-comment">// * establish the context of the resource</span>
6408 <span class="hljs-comment">// * trigger the AsyncHooks before callbacks</span>
6409 <span class="hljs-comment">// * call the provided function `fn` with the supplied arguments</span>
6410 <span class="hljs-comment">// * trigger the AsyncHooks after callbacks</span>
6411 <span class="hljs-comment">// * restore the original execution context</span>
6412 asyncResource.<span class="hljs-title function_">runInAsyncScope</span>(fn, thisArg, ...args);
6414 <span class="hljs-comment">// Call AsyncHooks destroy callbacks.</span>
6415 asyncResource.<span class="hljs-title function_">emitDestroy</span>();
6417 <span class="hljs-comment">// Return the unique ID assigned to the AsyncResource instance.</span>
6418 asyncResource.<span class="hljs-title function_">asyncId</span>();
6420 <span class="hljs-comment">// Return the trigger ID for the AsyncResource instance.</span>
6421 asyncResource.<span class="hljs-title function_">triggerAsyncId</span>();</code></pre>
6424 <li><code>type</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
6425 <li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference…
6427 <li><code>triggerAsyncId</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Da…
6429 <li><code>requireManualDestroy</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScr…
6440-js"><span class="hljs-keyword">class</span> <span class="hljs-title class_">DBQuery</span> <span …
6441 <span class="hljs-title function_">constructor</span>(<span class="hljs-params">db</span>) {
6442 <span class="hljs-variable language_">super</span>(<span class="hljs-string">'DBQuery'</span>);
6443 <span class="hljs-variable language_">this</span>.<span class="hljs-property">db</span> = db;
6446 …<span class="hljs-title function_">getInfo</span>(<span class="hljs-params">query, callback</span>…
6447-variable language_">this</span>.<span class="hljs-property">db</span>.<span class="hljs-title fun…
6448 …<span class="hljs-variable language_">this</span>.<span class="hljs-title function_">runInAsyncSco…
6452 <span class="hljs-title function_">close</span>(<span class="hljs-params"></span>) {
6453 …<span class="hljs-variable language_">this</span>.<span class="hljs-property">db</span> = <span cl…
6454 …<span class="hljs-variable language_">this</span>.<span class="hljs-title function_">emitDestroy</…
6462 <li><code>fn</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Glob…
6463 <li><code>type</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
6474 <li><code>fn</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Glob…
6484 <li><code>fn</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Glob…
6486 <li><code>thisArg</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_stru…
6487 <li><code>...args</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_stru…
6503 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Numbe…
6507 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Numbe…
6510 <p><a id="async-resource-worker-pool"></a></p>
6517 …class="language-js"><span class="hljs-keyword">const</span> { parentPort } = <span class="hljs-bui…
6518 …<span class="hljs-title function_">on</span>(<span class="hljs-string">'message'</span>, <span cla…
6519 …ntPort.<span class="hljs-title function_">postMessage</span>(task.<span class="hljs-property">a</s…
6522-js"><span class="hljs-keyword">const</span> { <span class="hljs-title class_">AsyncResource</span…
6523 …"hljs-keyword">const</span> { <span class="hljs-title class_">EventEmitter</span> } = <span class=…
6524 <span class="hljs-keyword">const</span> path = <span class="hljs-built_in">require</span>(<span cla…
6525 …ss="hljs-keyword">const</span> { <span class="hljs-title class_">Worker</span> } = <span class="hl…
6527 <span class="hljs-keyword">const</span> kTaskInfo = <span class="hljs-title class_">Symbol</span>(<…
6528 <span class="hljs-keyword">const</span> kWorkerFreedEvent = <span class="hljs-title class_">Symbol<…
6530 …="hljs-keyword">class</span> <span class="hljs-title class_">WorkerPoolTaskInfo</span> <span class…
6531 <span class="hljs-title function_">constructor</span>(<span class="hljs-params">callback</span>) {
6532 …<span class="hljs-variable language_">super</span>(<span class="hljs-string">'WorkerPoolTaskInfo'<…
6533 …<span class="hljs-variable language_">this</span>.<span class="hljs-property">callback</span> = ca…
6536 <span class="hljs-title function_">done</span>(<span class="hljs-params">err, result</span>) {
6537-variable language_">this</span>.<span class="hljs-title function_">runInAsyncScope</span>(<span c…
6538 …<span class="hljs-variable language_">this</span>.<span class="hljs-title function_">emitDestroy</…
6542 …lass="hljs-keyword">class</span> <span class="hljs-title class_">WorkerPool</span> <span class="hl…
6543 …<span class="hljs-title function_">constructor</span>(<span class="hljs-params">numThreads</span>)…
6544 <span class="hljs-variable language_">super</span>();
6545 …<span class="hljs-variable language_">this</span>.<span class="hljs-property">numThreads</span> = …
6546 … <span class="hljs-variable language_">this</span>.<span class="hljs-property">workers</span> = [];
6547 …<span class="hljs-variable language_">this</span>.<span class="hljs-property">freeWorkers</span> =…
6548 <span class="hljs-variable language_">this</span>.<span class="hljs-property">tasks</span> = [];
6550 …<span class="hljs-keyword">for</span> (<span class="hljs-keyword">let</span> i = <span class="hljs
6551 …<span class="hljs-variable language_">this</span>.<span class="hljs-title function_">addNewWorker<…
6553 <span class="hljs-comment">// Any time the kWorkerFreedEvent is emitted, dispatch</span>
6554 <span class="hljs-comment">// the next task pending in the queue, if any.</span>
6555 …<span class="hljs-variable language_">this</span>.<span class="hljs-title function_">on</span>(kWo…
6556-keyword">if</span> (<span class="hljs-variable language_">this</span>.<span class="hljs-property"…
6557-keyword">const</span> { task, callback } = <span class="hljs-variable language_">this</span>.<spa…
6558 …<span class="hljs-variable language_">this</span>.<span class="hljs-title function_">runTask</span…
6563 <span class="hljs-title function_">addNewWorker</span>(<span class="hljs-params"></span>) {
6564-keyword">const</span> worker = <span class="hljs-keyword">new</span> <span class="hljs-title clas…
6565 …<span class="hljs-title function_">on</span>(<span class="hljs-string">'message'</span>, <span cla…
6566 …<span class="hljs-comment">// In case of success: Call the callback that was passed to `runTask`,<…
6567 …<span class="hljs-comment">// remove the `TaskInfo` associated with the Worker, and mark it as fre…
6568 <span class="hljs-comment">// again.</span>
6569 …worker[kTaskInfo].<span class="hljs-title function_">done</span>(<span class="hljs-literal">null</…
6570 worker[kTaskInfo] = <span class="hljs-literal">null</span>;
6571 …<span class="hljs-variable language_">this</span>.<span class="hljs-property">freeWorkers</span>.<…
6572 …<span class="hljs-variable language_">this</span>.<span class="hljs-title function_">emit</span>(k…
6574 ….<span class="hljs-title function_">on</span>(<span class="hljs-string">'error'</span>, <span clas…
6575 …<span class="hljs-comment">// In case of an uncaught exception: Call the callback that was passed …
6576 <span class="hljs-comment">// `runTask` with the error.</span>
6577 <span class="hljs-keyword">if</span> (worker[kTaskInfo])
6578 …worker[kTaskInfo].<span class="hljs-title function_">done</span>(err, <span class="hljs-literal">n…
6579 <span class="hljs-keyword">else</span>
6580 …<span class="hljs-variable language_">this</span>.<span class="hljs-title function_">emit</span>(<…
6581 …<span class="hljs-comment">// Remove the worker from the list and start a new Worker to replace th…
6582 <span class="hljs-comment">// current one.</span>
6583-variable language_">this</span>.<span class="hljs-property">workers</span>.<span class="hljs-titl…
6584 …<span class="hljs-variable language_">this</span>.<span class="hljs-title function_">addNewWorker<…
6586 …<span class="hljs-variable language_">this</span>.<span class="hljs-property">workers</span>.<span…
6587 …<span class="hljs-variable language_">this</span>.<span class="hljs-property">freeWorkers</span>.<…
6588 …<span class="hljs-variable language_">this</span>.<span class="hljs-title function_">emit</span>(k…
6591 …<span class="hljs-title function_">runTask</span>(<span class="hljs-params">task, callback</span>)…
6592-keyword">if</span> (<span class="hljs-variable language_">this</span>.<span class="hljs-property"…
6593 <span class="hljs-comment">// No free threads, wait until a worker thread becomes free.</span>
6594 …<span class="hljs-variable language_">this</span>.<span class="hljs-property">tasks</span>.<span c…
6595 <span class="hljs-keyword">return</span>;
6598 …ljs-keyword">const</span> worker = <span class="hljs-variable language_">this</span>.<span class="…
6599 …worker[kTaskInfo] = <span class="hljs-keyword">new</span> <span class="hljs-title class_">WorkerPo…
6600 worker.<span class="hljs-title function_">postMessage</span>(task);
6603 <span class="hljs-title function_">close</span>(<span class="hljs-params"></span>) {
6604-keyword">for</span> (<span class="hljs-keyword">const</span> worker <span class="hljs-keyword">of…
6608 <span class="hljs-variable language_">module</span>.<span class="hljs-property">exports</span> = <s…
6615-js"><span class="hljs-keyword">const</span> <span class="hljs-title class_">WorkerPool</span> = <…
6616 <span class="hljs-keyword">const</span> os = <span class="hljs-built_in">require</span>(<span class…
6618-keyword">const</span> pool = <span class="hljs-keyword">new</span> <span class="hljs-title class_…
6620 <span class="hljs-keyword">let</span> finished = <span class="hljs-number">0</span>;
6621 …n class="hljs-keyword">for</span> (<span class="hljs-keyword">let</span> i = <span class="hljs-num…
6622-title function_">runTask</span>({ <span class="hljs-attr">a</span>: <span class="hljs-number">42<…
6623 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>…
6624 <span class="hljs-keyword">if</span> (++finished === <span class="hljs-number">10</span>)
6625 pool.<span class="hljs-title function_">close</span>();
6634 approach can be applied to a <a href="#stream_stream"><code>Stream</code></a> or a similar event-dr…
6635 …lass="language-js"><span class="hljs-keyword">const</span> { createServer } = <span class="hljs-bu…
6636-keyword">const</span> { <span class="hljs-title class_">AsyncResource</span>, executionAsyncId } …
6638 …ass="hljs-keyword">const</span> server = <span class="hljs-title function_">createServer</span>(<s…
6639-title function_">on</span>(<span class="hljs-string">'close'</span>, <span class="hljs-title clas…
6640 <span class="hljs-comment">// Execution context is bound to the current outer scope.</span>
6642 …req.<span class="hljs-title function_">on</span>(<span class="hljs-string">'close'</span>, <span c…
6643 …<span class="hljs-comment">// Execution context is bound to the scope that caused 'close' to emit.…
6645 res.<span class="hljs-title function_">end</span>();
6646 }).<span class="hljs-title function_">listen</span>(<span class="hljs-number">3000</span>);</code><…
6653 or any other asynchronous duration. It is similar to thread-local storage
6657 implementation that involves significant optimizations that are non-obvious to
6662 …code class="language-js"><span class="hljs-keyword">const</span> http = <span class="hljs-built_in…
6663 …ljs-keyword">const</span> { <span class="hljs-title class_">AsyncLocalStorage</span> } = <span cla…
6665 <span class="hljs-keyword">const</span> asyncLocalStorage = <span class="hljs-keyword">new</span> <…
6667 <span class="hljs-keyword">function</span> <span class="hljs-title function_">logWithId</span>(<spa…
6668 …<span class="hljs-keyword">const</span> id = asyncLocalStorage.<span class="hljs-title function_">…
6669-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hlj…
6672 <span class="hljs-keyword">let</span> idSeq = <span class="hljs-number">0</span>;
6673 http.<span class="hljs-title function_">createServer</span>(<span class="hljs-function">(<span clas…
6674 …asyncLocalStorage.<span class="hljs-title function_">run</span>(idSeq++, <span class="hljs-functio…
6675 <span class="hljs-title function_">logWithId</span>(<span class="hljs-string">'start'</span>);
6676 <span class="hljs-comment">// Imagine any chain of async operations here</span>
6677 … <span class="hljs-title function_">setImmediate</span>(<span class="hljs-function">() =></span> {
6678 … <span class="hljs-title function_">logWithId</span>(<span class="hljs-string">'finish'</span>);
6679 res.<span class="hljs-title function_">end</span>();
6682 }).<span class="hljs-title function_">listen</span>(<span class="hljs-number">8080</span>);
6684 http.<span class="hljs-title function_">get</span>(<span class="hljs-string">'http://localhost:8080…
6685 http.<span class="hljs-title function_">get</span>(<span class="hljs-string">'http://localhost:8080…
6686 <span class="hljs-comment">// Prints:</span>
6687 <span class="hljs-comment">// 0: start</span>
6688 <span class="hljs-comment">// 1: start</span>
6689 <span class="hljs-comment">// 0: finish</span>
6690 <span class="hljs-comment">// 1: finish</span></code></pre>
6719 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Data_…
6730 <li><code>store</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struct…
6736 …><code class="language-js"><span class="hljs-keyword">const</span> store = { <span class="hljs-att…
6737 <span class="hljs-comment">// Replaces previous store with the given store object</span>
6738 asyncLocalStorage.<span class="hljs-title function_">enterWith</span>(store);
6739 asyncLocalStorage.<span class="hljs-title function_">getStore</span>(); <span class="hljs-comment">…
6740 <span class="hljs-title function_">someAsyncOperation</span>(<span class="hljs-function">() =></spa…
6741 …asyncLocalStorage.<span class="hljs-title function_">getStore</span>(); <span class="hljs-comment"…
6749 …><code class="language-js"><span class="hljs-keyword">const</span> store = { <span class="hljs-att…
6751 emitter.<span class="hljs-title function_">on</span>(<span class="hljs-string">'my-event'</span>, <…
6752 asyncLocalStorage.<span class="hljs-title function_">enterWith</span>(store);
6754 emitter.<span class="hljs-title function_">on</span>(<span class="hljs-string">'my-event'</span>, <…
6755 …asyncLocalStorage.<span class="hljs-title function_">getStore</span>(); <span class="hljs-comment"…
6758 asyncLocalStorage.<span class="hljs-title function_">getStore</span>(); <span class="hljs-comment">…
6759 emitter.<span class="hljs-title function_">emit</span>(<span class="hljs-string">'my-event'</span>);
6760 asyncLocalStorage.<span class="hljs-title function_">getStore</span>(); <span class="hljs-comment">…
6766 <li><code>store</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struct…
6767 <li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Referenc…
6768 <li><code>...args</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_stru…
6778 …><code class="language-js"><span class="hljs-keyword">const</span> store = { <span class="hljs-att…
6779 <span class="hljs-keyword">try</span> {
6780 …asyncLocalStorage.<span class="hljs-title function_">run</span>(store, <span class="hljs-function"…
6781 …asyncLocalStorage.<span class="hljs-title function_">getStore</span>(); <span class="hljs-comment"…
6782 <span class="hljs-built_in">setTimeout</span>(<span class="hljs-function">() =></span> {
6783 …asyncLocalStorage.<span class="hljs-title function_">getStore</span>(); <span class="hljs-comment"…
6784 }, <span class="hljs-number">200</span>);
6785 …<span class="hljs-keyword">throw</span> <span class="hljs-keyword">new</span> <span class="hljs-ti…
6787 } <span class="hljs-keyword">catch</span> (e) {
6788 …asyncLocalStorage.<span class="hljs-title function_">getStore</span>(); <span class="hljs-comment"…
6789 <span class="hljs-comment">// The error will be caught here</span>
6796 <li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Referenc…
6797 <li><code>...args</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_stru…
6805 The stacktrace is not impacted by this call and the context is re-entered.</p>
6807 <pre><code class="language-js"><span class="hljs-comment">// Within a call to run</span>
6808 <span class="hljs-keyword">try</span> {
6809 …asyncLocalStorage.<span class="hljs-title function_">getStore</span>(); <span class="hljs-comment"…
6810 …asyncLocalStorage.<span class="hljs-title function_">exit</span>(<span class="hljs-function">() =>…
6811 …asyncLocalStorage.<span class="hljs-title function_">getStore</span>(); <span class="hljs-comment"…
6812 …<span class="hljs-keyword">throw</span> <span class="hljs-keyword">new</span> <span class="hljs-ti…
6814 } <span class="hljs-keyword">catch</span> (e) {
6815 …asyncLocalStorage.<span class="hljs-title function_">getStore</span>(); <span class="hljs-comment"…
6816 <span class="hljs-comment">// The error will be caught here</span>
6821 …e-js"><span class="hljs-keyword">async</span> <span class="hljs-keyword">function</span> <span cla…
6822-keyword">await</span> asyncLocalStorage.<span class="hljs-title function_">run</span>(<span class…
6823 …lStorage.<span class="hljs-title function_">getStore</span>().<span class="hljs-title function_">s…
6824 …<span class="hljs-keyword">return</span> <span class="hljs-title function_">foo</span>(); <span cl…
6835 <p>If your code is callback-based, it is enough to promisify it with
6837 <p>If you need to keep using callback-based API, or your code assumes
6843 …ty api_stability_2"><a href="#documentation_stability_index">Stability: 2</a> - Stable</div><p></p>
6845 <p><code>Buffer</code> objects are used to represent a fixed-length sequence of bytes. Many
6847 …lass is a subclass of JavaScript's <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScrip…
6849 plain <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Uin…
6852 <pre><code class="language-js"><span class="hljs-comment">// Creates a zero-filled Buffer of length…
6853 …"hljs-keyword">const</span> buf1 = <span class="hljs-title class_">Buffer</span>.<span class="hljs
6855 <span class="hljs-comment">// Creates a Buffer of length 10,</span>
6856 <span class="hljs-comment">// filled with bytes which all have the value `1`.</span>
6857-keyword">const</span> buf2 = <span class="hljs-title class_">Buffer</span>.<span class="hljs-titl…
6859 <span class="hljs-comment">// Creates an uninitialized buffer of length 10.</span>
6860 <span class="hljs-comment">// This is faster than calling Buffer.alloc() but the returned</span>
6861 <span class="hljs-comment">// Buffer instance might contain old data that needs to be</span>
6862 <span class="hljs-comment">// overwritten using fill(), write(), or other functions that fill the B…
6863 <span class="hljs-comment">// contents.</span>
6864 …js-keyword">const</span> buf3 = <span class="hljs-title class_">Buffer</span>.<span class="hljs-ti…
6866 <span class="hljs-comment">// Creates a Buffer containing the bytes [1, 2, 3].</span>
6867-keyword">const</span> buf4 = <span class="hljs-title class_">Buffer</span>.<span class="hljs-titl…
6869 <span class="hljs-comment">// Creates a Buffer containing the bytes [1, 1, 1, 1] – the entries</spa…
6870 <span class="hljs-comment">// are all truncated using `(value &#x26; 255)` to fit into the range 0–…
6871-keyword">const</span> buf5 = <span class="hljs-title class_">Buffer</span>.<span class="hljs-titl…
6873 <span class="hljs-comment">// Creates a Buffer containing the UTF-8-encoded bytes for the string 't…
6874 <span class="hljs-comment">// [0x74, 0xc3, 0xa9, 0x73, 0x74] (in hexadecimal notation)</span>
6875 <span class="hljs-comment">// [116, 195, 169, 115, 116] (in decimal notation)</span>
6876 …="hljs-keyword">const</span> buf6 = <span class="hljs-title class_">Buffer</span>.<span class="hlj…
6878 <span class="hljs-comment">// Creates a Buffer containing the Latin-1 bytes [0x74, 0xe9, 0x73, 0x74…
6879-keyword">const</span> buf7 = <span class="hljs-title class_">Buffer</span>.<span class="hljs-titl…
6895 specified. If no character encoding is specified, UTF-8 will be used as the
6897-js"><span class="hljs-keyword">const</span> buf = <span class="hljs-title class_">Buffer</span>.<…
6899-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf.<span class=…
6900 <span class="hljs-comment">// Prints: 68656c6c6f20776f726c64</span>
6901-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf.<span class=…
6902 <span class="hljs-comment">// Prints: aGVsbG8gd29ybGQ=</span>
6904-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hlj…
6905 <span class="hljs-comment">// Prints: &#x3C;Buffer 66 68 71 77 68 67 61 64 73></span>
6906-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hlj…
6907 <span class="hljs-comment">// Prints: &#x3C;Buffer 66 00 68 00 71 00 77 00 68 00 67 00 61 00 64 00 …
6911 <p><code>'utf8'</code>: Multi-byte encoded Unicode characters. Many web pages and other
6912 document formats use <a href="https://en.wikipedia.org/wiki/UTF-8">UTF-8</a>. This is the default c…
6914 valid UTF-8 data, the Unicode replacement character <code>U+FFFD</code> � will be used
6918 <p><code>'utf16le'</code>: Multi-byte encoded Unicode characters. Unlike <code>'utf8'</code>, each
6920 …n.wikipedia.org/wiki/Endianness">little-endian</a> variant of <a href="https://en.wikipedia.org/wi…
6923 <p><code>'latin1'</code>: Latin-1 stands for <a href="https://en.wikipedia.org/wiki/ISO-8859-1">ISO
6931 <p>Node.js also supports the following binary-to-text encodings. For
6932 binary-to-text encodings, the naming convention is reversed: Converting a
6939 specified in <a href="https://tools.ietf.org/html/rfc4648#section-5">RFC 4648, Section 5</a>. White…
6940 tabs, and new lines contained within the base64-encoded string are ignored.</p>
6943 <p><code>'base64url'</code>: <a href="https://tools.ietf.org/html/rfc4648#section-5">base64url</a> …
6944 <a href="https://tools.ietf.org/html/rfc4648#section-5">RFC 4648, Section 5</a>. When creating a <c…
6945 encoding will also correctly accept regular base64-encoded strings. When
6957 <p><code>'ascii'</code>: For 7-bit <a href="https://en.wikipedia.org/wiki/ASCII">ASCII</a> data onl…
6962 (or, if the data is known to always be ASCII-only, <code>'latin1'</code>) will be a
6963 better choice when encoding or decoding ASCII-only text. It is only provided
6967 …ias for <code>'latin1'</code>. See <a href="https://developer.mozilla.org/en-US/docs/Web/API/DOMSt…
6970 between strings and <code>Buffer</code>s, typically <code>'utf-8'</code> is the right choice.</p>
6973 <p><code>'ucs2'</code>: Alias of <code>'utf16le'</code>. UCS-2 used to refer to a variant of UTF-16
6978-js"><span class="hljs-title class_">Buffer</span>.<span class="hljs-title function_">from</span>(…
6979 <span class="hljs-comment">// Prints &#x3C;Buffer 1a>, data truncated when first non-hexadecimal va…
6980 <span class="hljs-comment">// ('g') encountered.</span>
6982 …ass="hljs-title class_">Buffer</span>.<span class="hljs-title function_">from</span>(<span class="…
6983 <span class="hljs-comment">// Prints &#x3C;Buffer 1a>, data truncated when data ends in single digi…
6985 …ass="hljs-title class_">Buffer</span>.<span class="hljs-title function_">from</span>(<span class="…
6986 <span class="hljs-comment">// Prints &#x3C;Buffer 16 34>, all data represented.</span></code></pre>
6988 both <code>'latin1'</code> and <code>'ISO-8859-1'</code> to <code>'win-1252'</code>. This means tha…
6991 <code>'win-1252'</code>-encoded data, and using <code>'latin1'</code> encoding may incorrectly deco…
7003 …la.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Uint8Array"><code>Uint8Array</code></a> …
7004 instances. All <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Ob…
7006 <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArra…
7009 <li>While <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects…
7012 compatibility. <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Ob…
7018 <p>There are two ways to create new <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScrip…
7020 <li>Passing a <code>Buffer</code> to a <a href="https://developer.mozilla.org/en-US/docs/Web/JavaSc…
7022 of the target type.</li>
7024-js"><span class="hljs-keyword">const</span> buf = <span class="hljs-title class_">Buffer</span>.<…
7025 …s="hljs-keyword">const</span> uint32array = <span class="hljs-keyword">new</span> <span class="hlj…
7027 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(…
7029 <span class="hljs-comment">// Prints: Uint32Array(4) [ 1, 2, 3, 4 ]</span></code></pre>
7031 <li>Passing the <code>Buffer</code>s underlying <a href="https://developer.mozilla.org/en-US/docs/W…
7032 <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArra…
7034-js"><span class="hljs-keyword">const</span> buf = <span class="hljs-title class_">Buffer</span>.<…
7035 …s="hljs-keyword">const</span> uint16array = <span class="hljs-keyword">new</span> <span class="hlj…
7036 buf.<span class="hljs-property">buffer</span>,
7037 buf.<span class="hljs-property">byteOffset</span>,
7038 …class="hljs-property">length</span> / <span class="hljs-title class_">Uint</span>16<span class="hl…
7040 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(…
7042 <span class="hljs-comment">// Prints: Uint16Array(5) [ 104, 101, 108, 108, 111 ]</span></code></pre>
7044 memory as a <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objec…
7047-js"><span class="hljs-keyword">const</span> arr = <span class="hljs-keyword">new</span> <span cla…
7049 arr[<span class="hljs-number">0</span>] = <span class="hljs-number">5000</span>;
7050 arr[<span class="hljs-number">1</span>] = <span class="hljs-number">4000</span>;
7052 <span class="hljs-comment">// Copies the contents of `arr`.</span>
7053 <span class="hljs-keyword">const</span> buf1 = <span class="hljs-title class_">Buffer</span>.<span …
7055 <span class="hljs-comment">// Shares memory with `arr`.</span>
7056 …hljs-keyword">const</span> buf2 = <span class="hljs-title class_">Buffer</span>.<span class="hljs-
7058 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(…
7059 <span class="hljs-comment">// Prints: &#x3C;Buffer 88 a0></span>
7060 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(…
7061 <span class="hljs-comment">// Prints: &#x3C;Buffer 88 13 a0 0f></span>
7063 arr[<span class="hljs-number">1</span>] = <span class="hljs-number">6000</span>;
7065 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(…
7066 <span class="hljs-comment">// Prints: &#x3C;Buffer 88 a0></span>
7067 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(…
7068 <span class="hljs-comment">// Prints: &#x3C;Buffer 88 13 70 17></span></code></pre>
7069 <p>When creating a <code>Buffer</code> using a <a href="https://developer.mozilla.org/en-US/docs/We…
7070 possible to use only a portion of the underlying <a href="https://developer.mozilla.org/en-US/docs/…
7072-js"><span class="hljs-keyword">const</span> arr = <span class="hljs-keyword">new</span> <span cla…
7073-keyword">const</span> buf = <span class="hljs-title class_">Buffer</span>.<span class="hljs-title…
7075 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(…
7076 <span class="hljs-comment">// Prints: 16</span></code></pre>
7077 <p>The <code>Buffer.from()</code> and <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScr…
7078 implementations. Specifically, the <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript…
7094-js"><span class="hljs-keyword">const</span> buf = <span class="hljs-title class_">Buffer</span>.<…
7096 <span class="hljs-keyword">for</span> (<span class="hljs-keyword">const</span> b <span class="hljs-
7097 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>…
7099 <span class="hljs-comment">// Prints:</span>
7100 <span class="hljs-comment">// 1</span>
7101 <span class="hljs-comment">// 2</span>
7102 <span class="hljs-comment">// 3</span></code></pre>
7109 … api_stability_1"><a href="#documentation_stability_index">Stability: 1</a> - Experimental</div><p…
7110 <p>A <a href="https://developer.mozilla.org/en-US/docs/Web/API/Blob"><code>Blob</code></a> encapsul…
7117-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string[]></a> | <a href="h…
7118-US/docs/Web/JavaScript/Reference/Global_Objects/ArrayBuffer" class="type">&#x3C;ArrayBuffer></a>,…
7120 <li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference…
7122 <li><code>encoding</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_str…
7124 …//developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;…
7131-US/docs/Web/JavaScript/Reference/Global_Objects/ArrayBuffer" class="type">&#x3C;ArrayBuffer></a>,…
7139 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Obje…
7141 <p>Returns a promise that fulfills with an <a href="https://developer.mozilla.org/en-US/docs/Web/Ja…
7153 <li><code>start</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struct…
7154 <li><code>end</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structur…
7155 …s://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3…
7164 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Obje…
7166 <p>Returns a promise that resolves the contents of the <code>Blob</code> decoded as a UTF-8
7173 <li>Type: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_t…
7175 <p>The content-type of the <code>Blob</code>.</p>
7181-js"><span class="hljs-keyword">const</span> { <span class="hljs-title class_">Blob</span> } = <sp…
7182 …class="hljs-keyword">const</span> blob = <span class="hljs-keyword">new</span> <span class="hljs-t…
7183 …s="hljs-keyword">const</span> { <span class="hljs-attr">setTimeout</span>: delay } = <span class="…
7185 <span class="hljs-keyword">const</span> mc1 = <span class="hljs-keyword">new</span> <span class="hl…
7186 <span class="hljs-keyword">const</span> mc2 = <span class="hljs-keyword">new</span> <span class="hl…
7188 mc1.<span class="hljs-property">port1</span>.<span class="hljs-property">onmessage</span> = <span c…
7189 …hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class=…
7190 mc1.<span class="hljs-property">port1</span>.<span class="hljs-title function_">close</span>();
7193 mc2.<span class="hljs-property">port1</span>.<span class="hljs-property">onmessage</span> = <span c…
7194 …<span class="hljs-keyword">await</span> <span class="hljs-title function_">delay</span>(<span clas…
7195 …hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class=…
7196 mc2.<span class="hljs-property">port1</span>.<span class="hljs-title function_">close</span>();
7199 mc1.<span class="hljs-property">port2</span>.<span class="hljs-title function_">postMessage</span>(…
7200 mc2.<span class="hljs-property">port2</span>.<span class="hljs-title function_">postMessage</span>(…
7202 <span class="hljs-comment">// The Blob is still usable after posting.</span>
7203 …s-title function_">text</span>().<span class="hljs-title function_">then</span>(<span class="hljs-
7213 <td><p>Attempting to fill a non-zero length buffer with a zero length buffer triggers a thrown exce…
7217 <td><p>Specifying an invalid string for <code>fill</code> now results in a zero-filled buffer.</p><…
7224 <li><code>size</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
7225-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | <a href="#bu…
7227 <li><code>encoding</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_str…
7231 <code>Buffer</code> will be zero-filled.</p>
7232-js"><span class="hljs-keyword">const</span> buf = <span class="hljs-title class_">Buffer</span>.<…
7234 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(…
7235 <span class="hljs-comment">// Prints: &#x3C;Buffer 00 00 00 00 00></span></code></pre>
7241-js"><span class="hljs-keyword">const</span> buf = <span class="hljs-title class_">Buffer</span>.<…
7243 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(…
7244 <span class="hljs-comment">// Prints: &#x3C;Buffer 61 61 61 61 61></span></code></pre>
7247-js"><span class="hljs-keyword">const</span> buf = <span class="hljs-title class_">Buffer</span>.<…
7249 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(…
7250 <span class="hljs-comment">// Prints: &#x3C;Buffer 68 65 6c 6c 6f 20 77 6f 72 6c 64></span></code><…
7269 <li><code>size</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
7278-js"><span class="hljs-keyword">const</span> buf = <span class="hljs-title class_">Buffer</span>.<…
7280 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(…
7281 <span class="hljs-comment">// Prints (contents may vary): &#x3C;Buffer a0 8b 28 3f 01 00 00 00 50 3…
7283 buf.<span class="hljs-title function_">fill</span>(<span class="hljs-number">0</span>);
7285 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(…
7286 <span class="hljs-comment">// Prints: &#x3C;Buffer 00 00 00 00 00 00 00 00 00 00></span></code></pr…
7288 <p>The <code>Buffer</code> module pre-allocates an internal <code>Buffer</code> instance of
7294 <p>Use of this pre-allocated internal memory pool is a key difference between
7306 <li><code>size</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
7310 is thrown. A zero-length <code>Buffer</code> is created if <code>size</code> is 0.</p>
7316 allocations under 4KB are sliced from a single pre-allocated <code>Buffer</code>. This
7323 to create an un-pooled <code>Buffer</code> instance using <code>Buffer.allocUnsafeSlow()</code> and
7325 <pre><code class="language-js"><span class="hljs-comment">// Need to keep around a few small chunks…
7326 <span class="hljs-keyword">const</span> store = [];
7328 socket.<span class="hljs-title function_">on</span>(<span class="hljs-string">'readable'</span>, <s…
7329 <span class="hljs-keyword">let</span> data;
7330 …<span class="hljs-keyword">while</span> (<span class="hljs-literal">null</span> !== (data = readab…
7331 <span class="hljs-comment">// Allocate for retained data.</span>
7332 …s-keyword">const</span> sb = <span class="hljs-title class_">Buffer</span>.<span class="hljs-title…
7334 <span class="hljs-comment">// Copy the data into the new allocation.</span>
7335 …an class="hljs-title function_">copy</span>(sb, <span class="hljs-number">0</span>, <span class="h…
7337 store.<span class="hljs-title function_">push</span>(sb);
7356-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | <a href="#bu…
7358 <li><code>encoding</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_str…
7360 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Numbe…
7363 This is not the same as <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/…
7366 For strings that contain non-base64/hex-encoded data (e.g. whitespace), the
7369 <pre><code class="language-js"><span class="hljs-keyword">const</span> str = <span class="hljs-stri…
7371-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hlj…
7372 …<span class="hljs-string">`<span class="hljs-subst">${Buffer.byteLength(str, <span class="hljs-str…
7373 <span class="hljs-comment">// Prints: ½ + ¼ = ¾: 9 characters, 12 bytes</span></code></pre>
7374-US/docs/Web/JavaScript/Reference/Global_Objects/DataView"><code>DataView</code></a>/<a href="http…
7375 <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/SharedArr…
7390 …" class="type">&#x3C;Buffer></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScrip…
7391 …" class="type">&#x3C;Buffer></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScrip…
7392 …://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C…
7398-js"><span class="hljs-keyword">const</span> buf1 = <span class="hljs-title class_">Buffer</span>.…
7399 …="hljs-keyword">const</span> buf2 = <span class="hljs-title class_">Buffer</span>.<span class="hlj…
7400 <span class="hljs-keyword">const</span> arr = [buf1, buf2];
7402-variable language_">console</span>.<span class="hljs-title function_">log</span>(arr.<span class=…
7403 <span class="hljs-comment">// Prints: [ &#x3C;Buffer 30 31 32 33>, &#x3C;Buffer 31 32 33 34> ]</spa…
7404 <span class="hljs-comment">// (This result is equal to: [buf2, buf1].)</span></code></pre>
7418-US/docs/Web/JavaScript/Reference/Global_Objects/Uint8Array" class="type">&#x3C;Uint8Array[]></a> …
7420 <li><code>totalLength</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_…
7426 <p>If the list has no items, or if the <code>totalLength</code> is 0, then a new zero-length
7433 <pre><code class="language-js"><span class="hljs-comment">// Create a single `Buffer` from a list o…
7435 …"hljs-keyword">const</span> buf1 = <span class="hljs-title class_">Buffer</span>.<span class="hljs
7436 …"hljs-keyword">const</span> buf2 = <span class="hljs-title class_">Buffer</span>.<span class="hljs
7437 …"hljs-keyword">const</span> buf3 = <span class="hljs-title class_">Buffer</span>.<span class="hljs
7438-keyword">const</span> totalLength = buf1.<span class="hljs-property">length</span> + buf2.<span c…
7440 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(…
7441 <span class="hljs-comment">// Prints: 42</span>
7443 <span class="hljs-keyword">const</span> bufA = <span class="hljs-title class_">Buffer</span>.<span …
7445 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(…
7446 <span class="hljs-comment">// Prints: &#x3C;Buffer 00 00 00 00 ...></span>
7447 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(…
7448 <span class="hljs-comment">// Prints: 42</span></code></pre>
7456 <li><code>array</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struct…
7460 <pre><code class="language-js"><span class="hljs-comment">// Creates a new Buffer containing the UT…
7461-keyword">const</span> buf = <span class="hljs-title class_">Buffer</span>.<span class="hljs-title…
7471-US/docs/Web/JavaScript/Reference/Global_Objects/ArrayBuffer" class="type">&#x3C;ArrayBuffer></a> …
7472 <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/SharedArr…
7473 <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArra…
7474 <li><code>byteOffset</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_s…
7475 <li><code>length</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struc…
7476 <strong>Default:</strong> <code>arrayBuffer.byteLength - byteOffset</code>.</li>
7478 <p>This creates a view of the <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Refe…
7480 <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArra…
7481 allocated memory as the <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/…
7482-js"><span class="hljs-keyword">const</span> arr = <span class="hljs-keyword">new</span> <span cla…
7484 arr[<span class="hljs-number">0</span>] = <span class="hljs-number">5000</span>;
7485 arr[<span class="hljs-number">1</span>] = <span class="hljs-number">4000</span>;
7487 <span class="hljs-comment">// Shares memory with `arr`.</span>
7488 …hljs-keyword">const</span> buf = <span class="hljs-title class_">Buffer</span>.<span class="hljs-t…
7490 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(…
7491 <span class="hljs-comment">// Prints: &#x3C;Buffer 88 13 a0 0f></span>
7493 <span class="hljs-comment">// Changing the original Uint16Array changes the Buffer also.</span>
7494 arr[<span class="hljs-number">1</span>] = <span class="hljs-number">6000</span>;
7496 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(…
7497 <span class="hljs-comment">// Prints: &#x3C;Buffer 88 13 70 17></span></code></pre>
7500-js"><span class="hljs-keyword">const</span> ab = <span class="hljs-keyword">new</span> <span clas…
7501-keyword">const</span> buf = <span class="hljs-title class_">Buffer</span>.<span class="hljs-title…
7503 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(…
7504 <span class="hljs-comment">// Prints: 2</span></code></pre>
7505 …<code>arrayBuffer</code> is not an <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScrip…
7506 <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/SharedArr…
7512-js"><span class="hljs-keyword">const</span> arrA = <span class="hljs-title class_">Uint</span>8<s…
7513-keyword">const</span> arrB = <span class="hljs-keyword">new</span> <span class="hljs-title class_…
7514-variable language_">console</span>.<span class="hljs-title function_">log</span>(arrA.<span class…
7516 …hljs-keyword">const</span> buf = <span class="hljs-title class_">Buffer</span>.<span class="hljs-t…
7517 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(…
7518 <span class="hljs-comment">// Prints: &#x3C;Buffer 63 64 65 66></span></code></pre>
7524-US/docs/Web/JavaScript/Reference/Global_Objects/Uint8Array" class="type">&#x3C;Uint8Array></a> An…
7528-js"><span class="hljs-keyword">const</span> buf1 = <span class="hljs-title class_">Buffer</span>.…
7529 <span class="hljs-keyword">const</span> buf2 = <span class="hljs-title class_">Buffer</span>.<span …
7531 buf1[<span class="hljs-number">0</span>] = <span class="hljs-number">0x61</span>;
7533 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(…
7534 <span class="hljs-comment">// Prints: auffer</span>
7535 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(…
7536 <span class="hljs-comment">// Prints: buffer</span></code></pre>
7544 <li><code>object</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/…
7545-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;integer></a> | <a href="ht…
7546 <li><code>length</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struc…
7550-js"><span class="hljs-keyword">const</span> buf = <span class="hljs-title class_">Buffer</span>.<…
7551 <span class="hljs-comment">// Prints: &#x3C;Buffer 74 68 69 73 20 69 73 20 61 20 74 65 73 74></span…
7554 <pre><code class="language-js"><span class="hljs-keyword">class</span> <span class="hljs-title clas…
7555 [<span class="hljs-title class_">Symbol</span>.<span class="hljs-property">toPrimitive</span>]() {
7556 <span class="hljs-keyword">return</span> <span class="hljs-string">'this is a test'</span>;
7560-keyword">const</span> buf = <span class="hljs-title class_">Buffer</span>.<span class="hljs-title…
7561 <span class="hljs-comment">// Prints: &#x3C;Buffer 74 68 69 73 20 69 73 20 61 20 74 65 73 74></span…
7569 <li><code>string</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struc…
7570 <li><code>encoding</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_str…
7574-js"><span class="hljs-keyword">const</span> buf1 = <span class="hljs-title class_">Buffer</span>.…
7575-keyword">const</span> buf2 = <span class="hljs-title class_">Buffer</span>.<span class="hljs-titl…
7577 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(…
7578 <span class="hljs-comment">// Prints: this is a tést</span>
7579 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(…
7580 <span class="hljs-comment">// Prints: this is a tést</span>
7581-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf1.<span class…
7582 <span class="hljs-comment">// Prints: this is a tést</span></code></pre>
7590 <li><code>obj</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Glo…
7591 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boole…
7594-js"><span class="hljs-title class_">Buffer</span>.<span class="hljs-title function_">isBuffer</sp…
7595-title class_">Buffer</span>.<span class="hljs-title function_">isBuffer</span>(<span class="hljs-
7596 …hljs-title class_">Buffer</span>.<span class="hljs-title function_">isBuffer</span>(<span class="h…
7597 <span class="hljs-title class_">Buffer</span>.<span class="hljs-title function_">isBuffer</span>([]…
7598-title class_">Buffer</span>.<span class="hljs-title function_">isBuffer</span>(<span class="hljs-
7604 <li><code>encoding</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_str…
7605 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boole…
7609-js"><span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</…
7610 <span class="hljs-comment">// Prints: true</span>
7612-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hlj…
7613 <span class="hljs-comment">// Prints: true</span>
7615-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hlj…
7616 <span class="hljs-comment">// Prints: false</span>
7618-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hlj…
7619 <span class="hljs-comment">// Prints: false</span></code></pre>
7625 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" c…
7627 <p>This is the size (in bytes) of pre-allocated internal <code>Buffer</code> instances used
7631 <li><code>index</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struct…
7636 <p>This operator is inherited from <code>Uint8Array</code>, so its behavior on out-of-bounds
7641 <pre><code class="language-js"><span class="hljs-comment">// Copy an ASCII string into a `Buffer` o…
7642 <span class="hljs-comment">// (This only works for ASCII-only strings. In general, one should use</…
7643 <span class="hljs-comment">// `Buffer.from()` to perform this conversion.)</span>
7645 <span class="hljs-keyword">const</span> str = <span class="hljs-string">'Node.js'</span>;
7646 …s-keyword">const</span> buf = <span class="hljs-title class_">Buffer</span>.<span class="hljs-titl…
7648 …class="hljs-keyword">for</span> (<span class="hljs-keyword">let</span> i = <span class="hljs-numbe…
7649 buf[i] = str.<span class="hljs-title function_">charCodeAt</span>(i);
7652-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf.<span class=…
7653 <span class="hljs-comment">// Prints: Node.js</span></code></pre>
7656 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array…
7661-js"><span class="hljs-keyword">const</span> arrayBuffer = <span class="hljs-keyword">new</span> <…
7662 <span class="hljs-keyword">const</span> buffer = <span class="hljs-title class_">Buffer</span>.<spa…
7664 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(…
7665 <span class="hljs-comment">// Prints: true</span></code></pre>
7668 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" c…
7678 <pre><code class="language-js"><span class="hljs-comment">// Create a buffer smaller than `Buffer.p…
7679-keyword">const</span> nodeBuffer = <span class="hljs-keyword">new</span> <span class="hljs-title …
7681 <span class="hljs-comment">// When casting the Node.js Buffer to an Int8Array, use the byteOffset</…
7682 <span class="hljs-comment">// to refer only to the part of `nodeBuffer.buffer` that contains the me…
7683 <span class="hljs-comment">// for `nodeBuffer`.</span>
7684-keyword">new</span> <span class="hljs-title class_">Int</span>8<span class="hljs-built_in">Array<…
7685 <h4><code>buf.compare(target[, targetStart[, targetEnd[, sourceStart[, sourceEnd]]]])</code><span><…
7691 <td><p>The <code>target</code> parameter can now be a <code>Uint8Array</code>.</p></td></tr>
7700target</code> <a href="#buffer_class_buffer" class="type">&#x3C;Buffer></a> | <a href="https://dev…
7702 …oper.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;integer…
7704 …oper.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;integer…
7705 (not inclusive). <strong>Default:</strong> <code>target.length</code>.</li>
7706 <li><code>sourceStart</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_…
7708 <li><code>sourceEnd</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_st…
7710 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Numbe…
7712 <p>Compares <code>buf</code> with <code>target</code> and returns a number indicating whether <code…
7713 comes before, after, or is the same as <code>target</code> in sort order.
7716 <li><code>0</code> is returned if <code>target</code> is the same as <code>buf</code></li>
7717 <li><code>1</code> is returned if <code>target</code> should come <em>before</em> <code>buf</code> …
7718 <li><code>-1</code> is returned if <code>target</code> should come <em>after</em> <code>buf</code> …
7720-js"><span class="hljs-keyword">const</span> buf1 = <span class="hljs-title class_">Buffer</span>.…
7721 …="hljs-keyword">const</span> buf2 = <span class="hljs-title class_">Buffer</span>.<span class="hlj…
7722 …="hljs-keyword">const</span> buf3 = <span class="hljs-title class_">Buffer</span>.<span class="hlj…
7724 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(…
7725 <span class="hljs-comment">// Prints: 0</span>
7726 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(…
7727 <span class="hljs-comment">// Prints: -1</span>
7728 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(…
7729 <span class="hljs-comment">// Prints: -1</span>
7730 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(…
7731 <span class="hljs-comment">// Prints: 1</span>
7732 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(…
7733 <span class="hljs-comment">// Prints: 1</span>
7734-variable language_">console</span>.<span class="hljs-title function_">log</span>([buf1, buf2, buf…
7735 <span class="hljs-comment">// Prints: [ &#x3C;Buffer 41 42 43>, &#x3C;Buffer 41 42 43 44>, &#x3C;Bu…
7736 <span class="hljs-comment">// (This result is equal to: [buf1, buf3, buf2].)</span></code></pre>
7738 arguments can be used to limit the comparison to specific ranges within <code>target</code>
7740-js"><span class="hljs-keyword">const</span> buf1 = <span class="hljs-title class_">Buffer</span>.…
7741-keyword">const</span> buf2 = <span class="hljs-title class_">Buffer</span>.<span class="hljs-titl…
7743-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf1.<span class…
7744 <span class="hljs-comment">// Prints: 0</span>
7745-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf1.<span class…
7746 <span class="hljs-comment">// Prints: -1</span>
7747-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf1.<span class…
7748 <span class="hljs-comment">// Prints: 1</span></code></pre>
7750 <code>targetEnd > target.byteLength</code>, or <code>sourceEnd > source.byteLength</code>.</p>
7751 <h4><code>buf.copy(target[, targetStart[, sourceStart[, sourceEnd]]])</code><span><a class="mark" h…
7756target</code> <a href="#buffer_class_buffer" class="type">&#x3C;Buffer></a> | <a href="https://dev…
7757 …oper.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;integer…
7759 <li><code>sourceStart</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_…
7761 <li><code>sourceEnd</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_st…
7763 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Numbe…
7765 …a from a region of <code>buf</code> to a region in <code>target</code>, even if the <code>target</…
7767 <p><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedA…
7770 <pre><code class="language-js"><span class="hljs-comment">// Create two `Buffer` instances.</span>
7771 …js-keyword">const</span> buf1 = <span class="hljs-title class_">Buffer</span>.<span class="hljs-ti…
7772-keyword">const</span> buf2 = <span class="hljs-title class_">Buffer</span>.<span class="hljs-titl…
7774 …n class="hljs-keyword">for</span> (<span class="hljs-keyword">let</span> i = <span class="hljs-num…
7775 <span class="hljs-comment">// 97 is the decimal ASCII value for 'a'.</span>
7776 buf1[i] = i + <span class="hljs-number">97</span>;
7779 <span class="hljs-comment">// Copy `buf1` bytes 16 through 19 into `buf2` starting at byte 8 of `bu…
7780 …n class="hljs-title function_">copy</span>(buf2, <span class="hljs-number">8</span>, <span class="…
7781 <span class="hljs-comment">// This is equivalent to:</span>
7782 <span class="hljs-comment">// buf2.set(buf1.subarray(16, 20), 8);</span>
7784-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf2.<span class…
7785 <span class="hljs-comment">// Prints: !!!!!!!!qrst!!!!!!!!!!!!!</span></code></pre>
7786 <pre><code class="language-js"><span class="hljs-comment">// Create a `Buffer` and copy data from o…
7787 <span class="hljs-comment">// within the same `Buffer`.</span>
7789 …ljs-keyword">const</span> buf = <span class="hljs-title class_">Buffer</span>.<span class="hljs-ti…
7791 …n class="hljs-keyword">for</span> (<span class="hljs-keyword">let</span> i = <span class="hljs-num…
7792 <span class="hljs-comment">// 97 is the decimal ASCII value for 'a'.</span>
7793 buf[i] = i + <span class="hljs-number">97</span>;
7796 …an class="hljs-title function_">copy</span>(buf, <span class="hljs-number">0</span>, <span class="…
7798 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(…
7799 <span class="hljs-comment">// Prints: efghijghijklmnopqrstuvwxyz</span></code></pre>
7805 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Iteration_p…
7807 <p>Creates and returns an <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Referenc…
7809 <pre><code class="language-js"><span class="hljs-comment">// Log the entire contents of a `Buffer`.…
7811 …="hljs-keyword">const</span> buf = <span class="hljs-title class_">Buffer</span>.<span class="hljs
7813 …n class="hljs-keyword">for</span> (<span class="hljs-keyword">const</span> pair <span class="hljs-
7814 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>…
7816 <span class="hljs-comment">// Prints:</span>
7817 <span class="hljs-comment">// [0, 98]</span>
7818 <span class="hljs-comment">// [1, 117]</span>
7819 <span class="hljs-comment">// [2, 102]</span>
7820 <span class="hljs-comment">// [3, 102]</span>
7821 <span class="hljs-comment">// [4, 101]</span>
7822 <span class="hljs-comment">// [5, 114]</span></code></pre>
7836-US/docs/Web/JavaScript/Reference/Global_Objects/Uint8Array" class="type">&#x3C;Uint8Array></a> A …
7838 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boole…
7843-js"><span class="hljs-keyword">const</span> buf1 = <span class="hljs-title class_">Buffer</span>.…
7844-keyword">const</span> buf2 = <span class="hljs-title class_">Buffer</span>.<span class="hljs-titl…
7845 …="hljs-keyword">const</span> buf3 = <span class="hljs-title class_">Buffer</span>.<span class="hlj…
7847 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(…
7848 <span class="hljs-comment">// Prints: true</span>
7849 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(…
7850 <span class="hljs-comment">// Prints: false</span></code></pre>
7861 <td><p>Attempting to fill a non-zero length buffer with a zero length buffer triggers a thrown exce…
7872-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | <a href="#bu…
7873 <li><code>offset</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struc…
7875 <li><code>end</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structur…
7877 <li><code>encoding</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_str…
7883 <pre><code class="language-js"><span class="hljs-comment">// Fill a `Buffer` with the ASCII charact…
7885-keyword">const</span> b = <span class="hljs-title class_">Buffer</span>.<span class="hljs-title f…
7887 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(…
7888 <span class="hljs-comment">// Prints: hhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhh</span></co…
7892 <p>If the final write of a <code>fill()</code> operation falls on a multi-byte character,
7894 <pre><code class="language-js"><span class="hljs-comment">// Fill a `Buffer` with character that ta…
7896-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hlj…
7897 <span class="hljs-comment">// Prints: &#x3C;Buffer c8 a2 c8 a2 c8></span></code></pre>
7900-js"><span class="hljs-keyword">const</span> buf = <span class="hljs-title class_">Buffer</span>.<…
7902 …s-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf.<span class…
7903 <span class="hljs-comment">// Prints: &#x3C;Buffer 61 61 61 61 61></span>
7904-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf.<span class=…
7905 <span class="hljs-comment">// Prints: &#x3C;Buffer aa aa aa aa aa></span>
7906-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf.<span class=…
7907 <span class="hljs-comment">// Throws an exception.</span></code></pre>
7913-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | <a href="#bu…
7914 <li><code>byteOffset</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_s…
7916 <li><code>encoding</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_str…
7918 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boole…
7920 <p>Equivalent to <a href="#buffer_buf_indexof_value_byteoffset_encoding"><code>buf.indexOf() !== -1…
7921-js"><span class="hljs-keyword">const</span> buf = <span class="hljs-title class_">Buffer</span>.<…
7923-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf.<span class=…
7924 <span class="hljs-comment">// Prints: true</span>
7925-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf.<span class=…
7926 <span class="hljs-comment">// Prints: true</span>
7927-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf.<span class=…
7928 <span class="hljs-comment">// Prints: true</span>
7929-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf.<span class=…
7930 <span class="hljs-comment">// Prints: true (97 is the decimal ASCII value for 'a')</span>
7931-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf.<span class=…
7932 <span class="hljs-comment">// Prints: false</span>
7933-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf.<span class=…
7934 <span class="hljs-comment">// Prints: true</span>
7935-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf.<span class=…
7936 <span class="hljs-comment">// Prints: false</span></code></pre>
7952-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | <a href="#bu…
7953 <li><code>byteOffset</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_s…
7955 <li><code>encoding</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_str…
7958 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Numbe…
7959 <code>-1</code> if <code>buf</code> does not contain <code>value</code>.</li>
7965 <li>a <code>Buffer</code> or <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Refer…
7967 <li>a number, <code>value</code> will be interpreted as an unsigned 8-bit integer
7970-js"><span class="hljs-keyword">const</span> buf = <span class="hljs-title class_">Buffer</span>.<…
7972-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf.<span class=…
7973 <span class="hljs-comment">// Prints: 0</span>
7974-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf.<span class=…
7975 <span class="hljs-comment">// Prints: 2</span>
7976-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf.<span class=…
7977 <span class="hljs-comment">// Prints: 8</span>
7978-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf.<span class=…
7979 <span class="hljs-comment">// Prints: 8 (97 is the decimal ASCII value for 'a')</span>
7980-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf.<span class=…
7981 <span class="hljs-comment">// Prints: -1</span>
7982-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf.<span class=…
7983 <span class="hljs-comment">// Prints: 8</span>
7985-keyword">const</span> utf16Buffer = <span class="hljs-title class_">Buffer</span>.<span class="hl…
7987-variable language_">console</span>.<span class="hljs-title function_">log</span>(utf16Buffer.<spa…
7988 <span class="hljs-comment">// Prints: 4</span>
7989-variable language_">console</span>.<span class="hljs-title function_">log</span>(utf16Buffer.<spa…
7990 <span class="hljs-comment">// Prints: 6</span></code></pre>
7996 behavior matches <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_…
7997-js"><span class="hljs-keyword">const</span> b = <span class="hljs-title class_">Buffer</span>.<sp…
7999 <span class="hljs-comment">// Passing a value that's a number, but not a valid byte.</span>
8000 <span class="hljs-comment">// Prints: 2, equivalent to searching for 99 or 'c'.</span>
8001-variable language_">console</span>.<span class="hljs-title function_">log</span>(b.<span class="h…
8002-variable language_">console</span>.<span class="hljs-title function_">log</span>(b.<span class="h…
8004 <span class="hljs-comment">// Passing a byteOffset that coerces to NaN or 0.</span>
8005 <span class="hljs-comment">// Prints: 1, searching the whole buffer.</span>
8006-variable language_">console</span>.<span class="hljs-title function_">log</span>(b.<span class="h…
8007-variable language_">console</span>.<span class="hljs-title function_">log</span>(b.<span class="h…
8008-variable language_">console</span>.<span class="hljs-title function_">log</span>(b.<span class="h…
8009-variable language_">console</span>.<span class="hljs-title function_">log</span>(b.<span class="h…
8018 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Iteration_p…
8020 <p>Creates and returns an <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Referenc…
8021-js"><span class="hljs-keyword">const</span> buf = <span class="hljs-title class_">Buffer</span>.<…
8023 …an class="hljs-keyword">for</span> (<span class="hljs-keyword">const</span> key <span class="hljs-
8024 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>…
8026 <span class="hljs-comment">// Prints:</span>
8027 <span class="hljs-comment">// 0</span>
8028 <span class="hljs-comment">// 1</span>
8029 <span class="hljs-comment">// 2</span>
8030 <span class="hljs-comment">// 3</span>
8031 <span class="hljs-comment">// 4</span>
8032 <span class="hljs-comment">// 5</span></code></pre>
8046-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | <a href="#bu…
8047 <li><code>byteOffset</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_s…
8049 <code>buf.length - 1</code>.</li>
8050 <li><code>encoding</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_str…
8053 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Numbe…
8054 <code>-1</code> if <code>buf</code> does not contain <code>value</code>.</li>
8058-js"><span class="hljs-keyword">const</span> buf = <span class="hljs-title class_">Buffer</span>.<…
8060-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf.<span class=…
8061 <span class="hljs-comment">// Prints: 0</span>
8062-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf.<span class=…
8063 <span class="hljs-comment">// Prints: 17</span>
8064-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf.<span class=…
8065 <span class="hljs-comment">// Prints: 17</span>
8066-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf.<span class=…
8067 <span class="hljs-comment">// Prints: 15 (97 is the decimal ASCII value for 'a')</span>
8068-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf.<span class=…
8069 <span class="hljs-comment">// Prints: -1</span>
8070-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf.<span class=…
8071 <span class="hljs-comment">// Prints: 5</span>
8072-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf.<span class=…
8073 <span class="hljs-comment">// Prints: -1</span>
8075-keyword">const</span> utf16Buffer = <span class="hljs-title class_">Buffer</span>.<span class="hl…
8077-variable language_">console</span>.<span class="hljs-title function_">log</span>(utf16Buffer.<spa…
8078 <span class="hljs-comment">// Prints: 6</span>
8079-variable language_">console</span>.<span class="hljs-title function_">log</span>(utf16Buffer.<spa…
8080 <span class="hljs-comment">// Prints: 4</span></code></pre>
8086 This behavior matches <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Gl…
8087-js"><span class="hljs-keyword">const</span> b = <span class="hljs-title class_">Buffer</span>.<sp…
8089 <span class="hljs-comment">// Passing a value that's a number, but not a valid byte.</span>
8090 <span class="hljs-comment">// Prints: 2, equivalent to searching for 99 or 'c'.</span>
8091-variable language_">console</span>.<span class="hljs-title function_">log</span>(b.<span class="h…
8092-variable language_">console</span>.<span class="hljs-title function_">log</span>(b.<span class="h…
8094 <span class="hljs-comment">// Passing a byteOffset that coerces to NaN.</span>
8095 <span class="hljs-comment">// Prints: 1, searching the whole buffer.</span>
8096-variable language_">console</span>.<span class="hljs-title function_">log</span>(b.<span class="h…
8097-variable language_">console</span>.<span class="hljs-title function_">log</span>(b.<span class="h…
8099 <span class="hljs-comment">// Passing a byteOffset that coerces to 0.</span>
8100 <span class="hljs-comment">// Prints: -1, equivalent to passing 0.</span>
8101-variable language_">console</span>.<span class="hljs-title function_">log</span>(b.<span class="h…
8102-variable language_">console</span>.<span class="hljs-title function_">log</span>(b.<span class="h…
8109 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" c…
8112 <pre><code class="language-js"><span class="hljs-comment">// Create a `Buffer` and write a shorter …
8114 …="hljs-keyword">const</span> buf = <span class="hljs-title class_">Buffer</span>.<span class="hljs
8116 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(…
8117 <span class="hljs-comment">// Prints: 1234</span>
8119 …lass="hljs-title function_">write</span>(<span class="hljs-string">'some string'</span>, <span cla…
8121 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(…
8122 <span class="hljs-comment">// Prints: 1234</span></code></pre>
8127 … api_stability_0"><a href="#documentation_stability_index">Stability: 0</a> - Deprecated: Use <a h…
8134 <li><code>offset</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struc…
8135 satisfy: <code>0 &#x3C;= offset &#x3C;= buf.length - 8</code>. <strong>Default:</strong> <code>0</c…
8136 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Obje…
8138 <p>Reads a signed, big-endian 64-bit integer from <code>buf</code> at the specified <code>offset</c…
8146 <li><code>offset</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struc…
8147 satisfy: <code>0 &#x3C;= offset &#x3C;= buf.length - 8</code>. <strong>Default:</strong> <code>0</c…
8148 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Obje…
8150 <p>Reads a signed, little-endian 64-bit integer from <code>buf</code> at the specified
8167 <li><code>offset</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struc…
8168 satisfy: <code>0 &#x3C;= offset &#x3C;= buf.length - 8</code>. <strong>Default:</strong> <code>0</c…
8169 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Obje…
8171 <p>Reads an unsigned, big-endian 64-bit integer from <code>buf</code> at the specified
8174-js"><span class="hljs-keyword">const</span> buf = <span class="hljs-title class_">Buffer</span>.<…
8176-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf.<span class=…
8177 <span class="hljs-comment">// Prints: 4294967295n</span></code></pre>
8191 <li><code>offset</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struc…
8192 satisfy: <code>0 &#x3C;= offset &#x3C;= buf.length - 8</code>. <strong>Default:</strong> <code>0</c…
8193 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Obje…
8195 <p>Reads an unsigned, little-endian 64-bit integer from <code>buf</code> at the specified
8198-js"><span class="hljs-keyword">const</span> buf = <span class="hljs-title class_">Buffer</span>.<…
8200-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf.<span class=…
8201 <span class="hljs-comment">// Prints: 18446744069414584320n</span></code></pre>
8215 <li><code>offset</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struc…
8216 satisfy <code>0 &#x3C;= offset &#x3C;= buf.length - 8</code>. <strong>Default:</strong> <code>0</co…
8217 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Numbe…
8219 <p>Reads a 64-bit, big-endian double from <code>buf</code> at the specified <code>offset</code>.</p>
8220-js"><span class="hljs-keyword">const</span> buf = <span class="hljs-title class_">Buffer</span>.<…
8222-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf.<span class=…
8223 <span class="hljs-comment">// Prints: 8.20788039913184e-304</span></code></pre>
8237 <li><code>offset</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struc…
8238 satisfy <code>0 &#x3C;= offset &#x3C;= buf.length - 8</code>. <strong>Default:</strong> <code>0</co…
8239 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Numbe…
8241 <p>Reads a 64-bit, little-endian double from <code>buf</code> at the specified <code>offset</code>.…
8242-js"><span class="hljs-keyword">const</span> buf = <span class="hljs-title class_">Buffer</span>.<…
8244-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf.<span class=…
8245 <span class="hljs-comment">// Prints: 5.447603722011605e-270</span>
8246-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf.<span class=…
8247 <span class="hljs-comment">// Throws ERR_OUT_OF_RANGE.</span></code></pre>
8261 <li><code>offset</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struc…
8262 satisfy <code>0 &#x3C;= offset &#x3C;= buf.length - 4</code>. <strong>Default:</strong> <code>0</co…
8263 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Numbe…
8265 <p>Reads a 32-bit, big-endian float from <code>buf</code> at the specified <code>offset</code>.</p>
8266-js"><span class="hljs-keyword">const</span> buf = <span class="hljs-title class_">Buffer</span>.<…
8268-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf.<span class=…
8269 <span class="hljs-comment">// Prints: 2.387939260590663e-38</span></code></pre>
8283 <li><code>offset</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struc…
8284 satisfy <code>0 &#x3C;= offset &#x3C;= buf.length - 4</code>. <strong>Default:</strong> <code>0</co…
8285 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Numbe…
8287 <p>Reads a 32-bit, little-endian float from <code>buf</code> at the specified <code>offset</code>.<…
8288-js"><span class="hljs-keyword">const</span> buf = <span class="hljs-title class_">Buffer</span>.<…
8290-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf.<span class=…
8291 <span class="hljs-comment">// Prints: 1.539989614439558e-36</span>
8292-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf.<span class=…
8293 <span class="hljs-comment">// Throws ERR_OUT_OF_RANGE.</span></code></pre>
8307 <li><code>offset</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struc…
8308 satisfy <code>0 &#x3C;= offset &#x3C;= buf.length - 1</code>. <strong>Default:</strong> <code>0</co…
8309 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Numbe…
8311 <p>Reads a signed 8-bit integer from <code>buf</code> at the specified <code>offset</code>.</p>
8313-js"><span class="hljs-keyword">const</span> buf = <span class="hljs-title class_">Buffer</span>.<…
8315-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf.<span class=…
8316 <span class="hljs-comment">// Prints: -1</span>
8317-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf.<span class=…
8318 <span class="hljs-comment">// Prints: 5</span>
8319-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf.<span class=…
8320 <span class="hljs-comment">// Throws ERR_OUT_OF_RANGE.</span></code></pre>
8334 <li><code>offset</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struc…
8335 satisfy <code>0 &#x3C;= offset &#x3C;= buf.length - 2</code>. <strong>Default:</strong> <code>0</co…
8336 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Numbe…
8338 <p>Reads a signed, big-endian 16-bit integer from <code>buf</code> at the specified <code>offset</c…
8340-js"><span class="hljs-keyword">const</span> buf = <span class="hljs-title class_">Buffer</span>.<…
8342-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf.<span class=…
8343 <span class="hljs-comment">// Prints: 5</span></code></pre>
8357 <li><code>offset</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struc…
8358 satisfy <code>0 &#x3C;= offset &#x3C;= buf.length - 2</code>. <strong>Default:</strong> <code>0</co…
8359 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Numbe…
8361 <p>Reads a signed, little-endian 16-bit integer from <code>buf</code> at the specified
8364-js"><span class="hljs-keyword">const</span> buf = <span class="hljs-title class_">Buffer</span>.<…
8366-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf.<span class=…
8367 <span class="hljs-comment">// Prints: 1280</span>
8368-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf.<span class=…
8369 <span class="hljs-comment">// Throws ERR_OUT_OF_RANGE.</span></code></pre>
8383 <li><code>offset</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struc…
8384 satisfy <code>0 &#x3C;= offset &#x3C;= buf.length - 4</code>. <strong>Default:</strong> <code>0</co…
8385 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Numbe…
8387 <p>Reads a signed, big-endian 32-bit integer from <code>buf</code> at the specified <code>offset</c…
8389-js"><span class="hljs-keyword">const</span> buf = <span class="hljs-title class_">Buffer</span>.<…
8391-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf.<span class=…
8392 <span class="hljs-comment">// Prints: 5</span></code></pre>
8406 <li><code>offset</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struc…
8407 satisfy <code>0 &#x3C;= offset &#x3C;= buf.length - 4</code>. <strong>Default:</strong> <code>0</co…
8408 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Numbe…
8410 <p>Reads a signed, little-endian 32-bit integer from <code>buf</code> at the specified
8413-js"><span class="hljs-keyword">const</span> buf = <span class="hljs-title class_">Buffer</span>.<…
8415-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf.<span class=…
8416 <span class="hljs-comment">// Prints: 83886080</span>
8417-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf.<span class=…
8418 <span class="hljs-comment">// Throws ERR_OUT_OF_RANGE.</span></code></pre>
8432 <li><code>offset</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struc…
8433 satisfy <code>0 &#x3C;= offset &#x3C;= buf.length - byteLength</code>.</li>
8434 <li><code>byteLength</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_s…
8436 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Numbe…
8439 and interprets the result as a big-endian, two's complement signed value
8441-js"><span class="hljs-keyword">const</span> buf = <span class="hljs-title class_">Buffer</span>.<…
8443-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf.<span class=…
8444 <span class="hljs-comment">// Prints: 1234567890ab</span>
8445-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf.<span class=…
8446 <span class="hljs-comment">// Throws ERR_OUT_OF_RANGE.</span>
8447-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf.<span class=…
8448 <span class="hljs-comment">// Throws ERR_OUT_OF_RANGE.</span></code></pre>
8462 <li><code>offset</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struc…
8463 satisfy <code>0 &#x3C;= offset &#x3C;= buf.length - byteLength</code>.</li>
8464 <li><code>byteLength</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_s…
8466 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Numbe…
8469 and interprets the result as a little-endian, two's complement signed value
8471-js"><span class="hljs-keyword">const</span> buf = <span class="hljs-title class_">Buffer</span>.<…
8473-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf.<span class=…
8474 <span class="hljs-comment">// Prints: -546f87a9cbee</span></code></pre>
8490 <li><code>offset</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struc…
8491 satisfy <code>0 &#x3C;= offset &#x3C;= buf.length - 1</code>. <strong>Default:</strong> <code>0</co…
8492 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Numbe…
8494 <p>Reads an unsigned 8-bit integer from <code>buf</code> at the specified <code>offset</code>.</p>
8496-js"><span class="hljs-keyword">const</span> buf = <span class="hljs-title class_">Buffer</span>.<…
8498-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf.<span class=…
8499 <span class="hljs-comment">// Prints: 1</span>
8500-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf.<span class=…
8501 <span class="hljs-comment">// Prints: 254</span>
8502-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf.<span class=…
8503 <span class="hljs-comment">// Throws ERR_OUT_OF_RANGE.</span></code></pre>
8519 <li><code>offset</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struc…
8520 satisfy <code>0 &#x3C;= offset &#x3C;= buf.length - 2</code>. <strong>Default:</strong> <code>0</co…
8521 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Numbe…
8523 <p>Reads an unsigned, big-endian 16-bit integer from <code>buf</code> at the specified
8526-js"><span class="hljs-keyword">const</span> buf = <span class="hljs-title class_">Buffer</span>.<…
8528-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf.<span class=…
8529 <span class="hljs-comment">// Prints: 1234</span>
8530-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf.<span class=…
8531 <span class="hljs-comment">// Prints: 3456</span></code></pre>
8547 <li><code>offset</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struc…
8548 satisfy <code>0 &#x3C;= offset &#x3C;= buf.length - 2</code>. <strong>Default:</strong> <code>0</co…
8549 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Numbe…
8551 <p>Reads an unsigned, little-endian 16-bit integer from <code>buf</code> at the specified
8554-js"><span class="hljs-keyword">const</span> buf = <span class="hljs-title class_">Buffer</span>.<…
8556-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf.<span class=…
8557 <span class="hljs-comment">// Prints: 3412</span>
8558-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf.<span class=…
8559 <span class="hljs-comment">// Prints: 5634</span>
8560-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf.<span class=…
8561 <span class="hljs-comment">// Throws ERR_OUT_OF_RANGE.</span></code></pre>
8577 <li><code>offset</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struc…
8578 satisfy <code>0 &#x3C;= offset &#x3C;= buf.length - 4</code>. <strong>Default:</strong> <code>0</co…
8579 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Numbe…
8581 <p>Reads an unsigned, big-endian 32-bit integer from <code>buf</code> at the specified
8584-js"><span class="hljs-keyword">const</span> buf = <span class="hljs-title class_">Buffer</span>.<…
8586-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf.<span class=…
8587 <span class="hljs-comment">// Prints: 12345678</span></code></pre>
8603 <li><code>offset</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struc…
8604 satisfy <code>0 &#x3C;= offset &#x3C;= buf.length - 4</code>. <strong>Default:</strong> <code>0</co…
8605 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Numbe…
8607 <p>Reads an unsigned, little-endian 32-bit integer from <code>buf</code> at the specified
8610-js"><span class="hljs-keyword">const</span> buf = <span class="hljs-title class_">Buffer</span>.<…
8612-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf.<span class=…
8613 <span class="hljs-comment">// Prints: 78563412</span>
8614-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf.<span class=…
8615 <span class="hljs-comment">// Throws ERR_OUT_OF_RANGE.</span></code></pre>
8631 <li><code>offset</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struc…
8632 satisfy <code>0 &#x3C;= offset &#x3C;= buf.length - byteLength</code>.</li>
8633 <li><code>byteLength</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_s…
8635 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Numbe…
8638 and interprets the result as an unsigned big-endian integer supporting
8641-js"><span class="hljs-keyword">const</span> buf = <span class="hljs-title class_">Buffer</span>.<…
8643-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf.<span class=…
8644 <span class="hljs-comment">// Prints: 1234567890ab</span>
8645-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf.<span class=…
8646 <span class="hljs-comment">// Throws ERR_OUT_OF_RANGE.</span></code></pre>
8662 <li><code>offset</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struc…
8663 satisfy <code>0 &#x3C;= offset &#x3C;= buf.length - byteLength</code>.</li>
8664 <li><code>byteLength</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_s…
8666 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Numbe…
8669 and interprets the result as an unsigned, little-endian integer supporting
8672-js"><span class="hljs-keyword">const</span> buf = <span class="hljs-title class_">Buffer</span>.<…
8674-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf.<span class=…
8675 <span class="hljs-comment">// Prints: ab9078563412</span></code></pre>
8681 <li><code>start</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struct…
8682 <li><code>end</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structur…
8690 <p>This method is inherited from <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/R…
8693 <pre><code class="language-js"><span class="hljs-comment">// Create a `Buffer` with the ASCII alpha…
8694 <span class="hljs-comment">// from the original `Buffer`.</span>
8696 …js-keyword">const</span> buf1 = <span class="hljs-title class_">Buffer</span>.<span class="hljs-ti…
8698 …n class="hljs-keyword">for</span> (<span class="hljs-keyword">let</span> i = <span class="hljs-num…
8699 <span class="hljs-comment">// 97 is the decimal ASCII value for 'a'.</span>
8700 buf1[i] = i + <span class="hljs-number">97</span>;
8703 …="hljs-keyword">const</span> buf2 = buf1.<span class="hljs-title function_">subarray</span>(<span …
8705-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf2.<span class…
8706 <span class="hljs-comment">// Prints: abc</span>
8708 buf1[<span class="hljs-number">0</span>] = <span class="hljs-number">33</span>;
8710-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf2.<span class…
8711 <span class="hljs-comment">// Prints: !bc</span></code></pre>
8714-js"><span class="hljs-keyword">const</span> buf = <span class="hljs-title class_">Buffer</span>.<…
8716-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf.<span class=…
8717 <span class="hljs-comment">// Prints: buffe</span>
8718 <span class="hljs-comment">// (Equivalent to buf.subarray(0, 5).)</span>
8720-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf.<span class=…
8721 <span class="hljs-comment">// Prints: buff</span>
8722 <span class="hljs-comment">// (Equivalent to buf.subarray(0, 4).)</span>
8724-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf.<span class=…
8725 <span class="hljs-comment">// Prints: uff</span>
8726 <span class="hljs-comment">// (Equivalent to buf.subarray(1, 4).)</span></code></pre>
8735 <td><p>Coercing the offsets to integers now handles values outside the 32-bit integer range properl…
8742 <li><code>start</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struct…
8743 <li><code>end</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structur…
8753-js"><span class="hljs-keyword">const</span> buf = <span class="hljs-title class_">Buffer</span>.<…
8755-keyword">const</span> copiedBuf = <span class="hljs-title class_">Uint</span>8<span class="hljs-b…
8756 copiedBuf[<span class="hljs-number">0</span>]++;
8757 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(…
8758 <span class="hljs-comment">// Prints: cuffer</span>
8760 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(…
8761 <span class="hljs-comment">// Prints: buffer</span></code></pre>
8769 <p>Interprets <code>buf</code> as an array of unsigned 16-bit integers and swaps the
8770 byte order <em>in-place</em>. Throws <a href="#ERR_INVALID_BUFFER_SIZE"><code>ERR_INVALID_BUFFER_SI…
8772-js"><span class="hljs-keyword">const</span> buf1 = <span class="hljs-title class_">Buffer</span>.…
8774 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(…
8775 <span class="hljs-comment">// Prints: &#x3C;Buffer 01 02 03 04 05 06 07 08></span>
8777 buf1.<span class="hljs-title function_">swap16</span>();
8779 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(…
8780 <span class="hljs-comment">// Prints: &#x3C;Buffer 02 01 04 03 06 05 08 07></span>
8782-keyword">const</span> buf2 = <span class="hljs-title class_">Buffer</span>.<span class="hljs-titl…
8784 buf2.<span class="hljs-title function_">swap16</span>();
8785 <span class="hljs-comment">// Throws ERR_INVALID_BUFFER_SIZE.</span></code></pre>
8786 <p>One convenient use of <code>buf.swap16()</code> is to perform a fast in-place conversion
8787 between UTF-16 little-endian and UTF-16 big-endian:</p>
8788-js"><span class="hljs-keyword">const</span> buf = <span class="hljs-title class_">Buffer</span>.<…
8789 buf.<span class="hljs-title function_">swap16</span>(); <span class="hljs-comment">// Convert to bi…
8797 <p>Interprets <code>buf</code> as an array of unsigned 32-bit integers and swaps the
8798 byte order <em>in-place</em>. Throws <a href="#ERR_INVALID_BUFFER_SIZE"><code>ERR_INVALID_BUFFER_SI…
8800-js"><span class="hljs-keyword">const</span> buf1 = <span class="hljs-title class_">Buffer</span>.…
8802 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(…
8803 <span class="hljs-comment">// Prints: &#x3C;Buffer 01 02 03 04 05 06 07 08></span>
8805 buf1.<span class="hljs-title function_">swap32</span>();
8807 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(…
8808 <span class="hljs-comment">// Prints: &#x3C;Buffer 04 03 02 01 08 07 06 05></span>
8810-keyword">const</span> buf2 = <span class="hljs-title class_">Buffer</span>.<span class="hljs-titl…
8812 buf2.<span class="hljs-title function_">swap32</span>();
8813 <span class="hljs-comment">// Throws ERR_INVALID_BUFFER_SIZE.</span></code></pre>
8821 <p>Interprets <code>buf</code> as an array of 64-bit numbers and swaps byte order <em>in-place</em>.
8823-js"><span class="hljs-keyword">const</span> buf1 = <span class="hljs-title class_">Buffer</span>.…
8825 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(…
8826 <span class="hljs-comment">// Prints: &#x3C;Buffer 01 02 03 04 05 06 07 08></span>
8828 buf1.<span class="hljs-title function_">swap64</span>();
8830 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(…
8831 <span class="hljs-comment">// Prints: &#x3C;Buffer 08 07 06 05 04 03 02 01></span>
8833-keyword">const</span> buf2 = <span class="hljs-title class_">Buffer</span>.<span class="hljs-titl…
8835 buf2.<span class="hljs-title function_">swap64</span>();
8836 <span class="hljs-comment">// Throws ERR_INVALID_BUFFER_SIZE.</span></code></pre>
8842 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Obje…
8844 <p>Returns a JSON representation of <code>buf</code>. <a href="https://developer.mozilla.org/en-US/…
8848-js"><span class="hljs-keyword">const</span> buf = <span class="hljs-title class_">Buffer</span>.<…
8849 <span class="hljs-keyword">const</span> json = <span class="hljs-variable constant_">JSON</span>.<s…
8851 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(…
8852 <span class="hljs-comment">// Prints: {"type":"Buffer","data":[1,2,3,4,5]}</span>
8854-keyword">const</span> copy = <span class="hljs-variable constant_">JSON</span>.<span class="hljs-
8855 …<span class="hljs-keyword">return</span> value &#x26;&#x26; value.<span class="hljs-property">type…
8856 …<span class="hljs-title class_">Buffer</span>.<span class="hljs-title function_">from</span>(value…
8860 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(…
8861 <span class="hljs-comment">// Prints: &#x3C;Buffer 01 02 03 04 05></span></code></pre>
8867 <li><code>encoding</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_str…
8868 <li><code>start</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struct…
8869 <li><code>end</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structur…
8871 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Strin…
8875 …f <code>encoding</code> is <code>'utf8'</code> and a byte sequence in the input is not valid UTF-8,
8877 <p>The maximum length of a string instance (in UTF-16 code units) is available
8879-js"><span class="hljs-keyword">const</span> buf1 = <span class="hljs-title class_">Buffer</span>.…
8881 …n class="hljs-keyword">for</span> (<span class="hljs-keyword">let</span> i = <span class="hljs-num…
8882 <span class="hljs-comment">// 97 is the decimal ASCII value for 'a'.</span>
8883 buf1[i] = i + <span class="hljs-number">97</span>;
8886-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf1.<span class…
8887 <span class="hljs-comment">// Prints: abcdefghijklmnopqrstuvwxyz</span>
8888-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf1.<span class…
8889 <span class="hljs-comment">// Prints: abcde</span>
8891 …="hljs-keyword">const</span> buf2 = <span class="hljs-title class_">Buffer</span>.<span class="hlj…
8893-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf2.<span class…
8894 <span class="hljs-comment">// Prints: 74c3a97374</span>
8895-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf2.<span class…
8896 <span class="hljs-comment">// Prints: té</span>
8897-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf2.<span class…
8898 <span class="hljs-comment">// Prints: té</span></code></pre>
8904 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Iteration_p…
8906 <p>Creates and returns an <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Referenc…
8908-js"><span class="hljs-keyword">const</span> buf = <span class="hljs-title class_">Buffer</span>.<…
8910 …n class="hljs-keyword">for</span> (<span class="hljs-keyword">const</span> value <span class="hljs
8911 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>…
8913 <span class="hljs-comment">// Prints:</span>
8914 <span class="hljs-comment">// 98</span>
8915 <span class="hljs-comment">// 117</span>
8916 <span class="hljs-comment">// 102</span>
8917 <span class="hljs-comment">// 102</span>
8918 <span class="hljs-comment">// 101</span>
8919 <span class="hljs-comment">// 114</span>
8921 <span class="hljs-keyword">for</span> (<span class="hljs-keyword">const</span> value <span class="h…
8922 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>…
8924 <span class="hljs-comment">// Prints:</span>
8925 <span class="hljs-comment">// 98</span>
8926 <span class="hljs-comment">// 117</span>
8927 <span class="hljs-comment">// 102</span>
8928 <span class="hljs-comment">// 102</span>
8929 <span class="hljs-comment">// 101</span>
8930 <span class="hljs-comment">// 114</span></code></pre>
8936 <li><code>string</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struc…
8937 <li><code>offset</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struc…
8939 <li><code>length</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struc…
8940 exceed <code>buf.length - offset</code>). <strong>Default:</strong> <code>buf.length - offset</code…
8941 <li><code>encoding</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_str…
8942 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Numbe…
8948-js"><span class="hljs-keyword">const</span> buf = <span class="hljs-title class_">Buffer</span>.<…
8950-keyword">const</span> len = buf.<span class="hljs-title function_">write</span>(<span class="hljs
8952-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hlj…
8953 <span class="hljs-comment">// Prints: 12 bytes: ½ + ¼ = ¾</span>
8955 …hljs-keyword">const</span> buffer = <span class="hljs-title class_">Buffer</span>.<span class="hlj…
8957 …ljs-keyword">const</span> length = buffer.<span class="hljs-title function_">write</span>(<span cl…
8959-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hlj…
8960 <span class="hljs-comment">// Prints: 2 bytes : ab</span></code></pre>
8966 <li><code>value</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/G…
8967 <li><code>offset</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struc…
8968 satisfy: <code>0 &#x3C;= offset &#x3C;= buf.length - 8</code>. <strong>Default:</strong> <code>0</c…
8969 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Numbe…
8971 <p>Writes <code>value</code> to <code>buf</code> at the specified <code>offset</code> as big-endian…
8973-js"><span class="hljs-keyword">const</span> buf = <span class="hljs-title class_">Buffer</span>.<…
8975 …uf.<span class="hljs-title function_">writeBigInt64BE</span>(<span class="hljs-number">0x010203040…
8977 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(…
8978 <span class="hljs-comment">// Prints: &#x3C;Buffer 01 02 03 04 05 06 07 08></span></code></pre>
8984 <li><code>value</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/G…
8985 <li><code>offset</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struc…
8986 satisfy: <code>0 &#x3C;= offset &#x3C;= buf.length - 8</code>. <strong>Default:</strong> <code>0</c…
8987 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Numbe…
8989 <p>Writes <code>value</code> to <code>buf</code> at the specified <code>offset</code> as little-end…
8991-js"><span class="hljs-keyword">const</span> buf = <span class="hljs-title class_">Buffer</span>.<…
8993 …uf.<span class="hljs-title function_">writeBigInt64LE</span>(<span class="hljs-number">0x010203040…
8995 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(…
8996 <span class="hljs-comment">// Prints: &#x3C;Buffer 08 07 06 05 04 03 02 01></span></code></pre>
9010 <li><code>value</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/G…
9011 <li><code>offset</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struc…
9012 satisfy: <code>0 &#x3C;= offset &#x3C;= buf.length - 8</code>. <strong>Default:</strong> <code>0</c…
9013 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Numbe…
9015 <p>Writes <code>value</code> to <code>buf</code> at the specified <code>offset</code> as big-endian…
9017-js"><span class="hljs-keyword">const</span> buf = <span class="hljs-title class_">Buffer</span>.<…
9019 …f.<span class="hljs-title function_">writeBigUInt64BE</span>(<span class="hljs-number">0xdecafafec…
9021 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(…
9022 <span class="hljs-comment">// Prints: &#x3C;Buffer de ca fa fe ca ce fa de></span></code></pre>
9036 <li><code>value</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/G…
9037 <li><code>offset</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struc…
9038 satisfy: <code>0 &#x3C;= offset &#x3C;= buf.length - 8</code>. <strong>Default:</strong> <code>0</c…
9039 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Numbe…
9041 <p>Writes <code>value</code> to <code>buf</code> at the specified <code>offset</code> as little-end…
9042-js"><span class="hljs-keyword">const</span> buf = <span class="hljs-title class_">Buffer</span>.<…
9044 …f.<span class="hljs-title function_">writeBigUInt64LE</span>(<span class="hljs-number">0xdecafafec…
9046 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(…
9047 <span class="hljs-comment">// Prints: &#x3C;Buffer de fa ce ca fe fa ca de></span></code></pre>
9062 <li><code>value</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struct…
9063 <li><code>offset</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struc…
9064 satisfy <code>0 &#x3C;= offset &#x3C;= buf.length - 8</code>. <strong>Default:</strong> <code>0</co…
9065 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Numbe…
9067 <p>Writes <code>value</code> to <code>buf</code> at the specified <code>offset</code> as big-endian…
9070-js"><span class="hljs-keyword">const</span> buf = <span class="hljs-title class_">Buffer</span>.<…
9072 buf.<span class="hljs-title function_">writeDoubleBE</span>(<span class="hljs-number">123.456</span…
9074 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(…
9075 <span class="hljs-comment">// Prints: &#x3C;Buffer 40 5e dd 2f 1a 9f be 77></span></code></pre>
9089 <li><code>value</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struct…
9090 <li><code>offset</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struc…
9091 satisfy <code>0 &#x3C;= offset &#x3C;= buf.length - 8</code>. <strong>Default:</strong> <code>0</co…
9092 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Numbe…
9094 <p>Writes <code>value</code> to <code>buf</code> at the specified <code>offset</code> as little-end…
9097-js"><span class="hljs-keyword">const</span> buf = <span class="hljs-title class_">Buffer</span>.<…
9099 buf.<span class="hljs-title function_">writeDoubleLE</span>(<span class="hljs-number">123.456</span…
9101 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(…
9102 <span class="hljs-comment">// Prints: &#x3C;Buffer 77 be 9f 1a 2f dd 5e 40></span></code></pre>
9116 <li><code>value</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struct…
9117 <li><code>offset</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struc…
9118 satisfy <code>0 &#x3C;= offset &#x3C;= buf.length - 4</code>. <strong>Default:</strong> <code>0</co…
9119 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Numbe…
9121 <p>Writes <code>value</code> to <code>buf</code> at the specified <code>offset</code> as big-endian…
9123-js"><span class="hljs-keyword">const</span> buf = <span class="hljs-title class_">Buffer</span>.<…
9125 buf.<span class="hljs-title function_">writeFloatBE</span>(<span class="hljs-number">0xcafebabe</sp…
9127 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(…
9128 <span class="hljs-comment">// Prints: &#x3C;Buffer 4f 4a fe bb></span></code></pre>
9142 <li><code>value</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struct…
9143 <li><code>offset</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struc…
9144 satisfy <code>0 &#x3C;= offset &#x3C;= buf.length - 4</code>. <strong>Default:</strong> <code>0</co…
9145 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Numbe…
9147 <p>Writes <code>value</code> to <code>buf</code> at the specified <code>offset</code> as little-end…
9149-js"><span class="hljs-keyword">const</span> buf = <span class="hljs-title class_">Buffer</span>.<…
9151 buf.<span class="hljs-title function_">writeFloatLE</span>(<span class="hljs-number">0xcafebabe</sp…
9153 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(…
9154 <span class="hljs-comment">// Prints: &#x3C;Buffer bb fe 4a 4f></span></code></pre>
9168 <li><code>value</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struct…
9169 <li><code>offset</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struc…
9170 satisfy <code>0 &#x3C;= offset &#x3C;= buf.length - 1</code>. <strong>Default:</strong> <code>0</co…
9171 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Numbe…
9174 signed 8-bit integer. Behavior is undefined when <code>value</code> is anything other than
9175 a signed 8-bit integer.</p>
9177-js"><span class="hljs-keyword">const</span> buf = <span class="hljs-title class_">Buffer</span>.<…
9179 buf.<span class="hljs-title function_">writeInt8</span>(<span class="hljs-number">2</span>, <span c…
9180 buf.<span class="hljs-title function_">writeInt8</span>(-<span class="hljs-number">2</span>, <span …
9182 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(…
9183 <span class="hljs-comment">// Prints: &#x3C;Buffer 02 fe></span></code></pre>
9197 <li><code>value</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struct…
9198 <li><code>offset</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struc…
9199 satisfy <code>0 &#x3C;= offset &#x3C;= buf.length - 2</code>. <strong>Default:</strong> <code>0</co…
9200 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Numbe…
9202 <p>Writes <code>value</code> to <code>buf</code> at the specified <code>offset</code> as big-endian…
9203 must be a valid signed 16-bit integer. Behavior is undefined when <code>value</code> is
9204 anything other than a signed 16-bit integer.</p>
9206-js"><span class="hljs-keyword">const</span> buf = <span class="hljs-title class_">Buffer</span>.<…
9208 buf.<span class="hljs-title function_">writeInt16BE</span>(<span class="hljs-number">0x0102</span>,…
9210 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(…
9211 <span class="hljs-comment">// Prints: &#x3C;Buffer 01 02></span></code></pre>
9225 <li><code>value</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struct…
9226 <li><code>offset</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struc…
9227 satisfy <code>0 &#x3C;= offset &#x3C;= buf.length - 2</code>. <strong>Default:</strong> <code>0</co…
9228 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Numbe…
9230 <p>Writes <code>value</code> to <code>buf</code> at the specified <code>offset</code> as little-end…
9231 must be a valid signed 16-bit integer. Behavior is undefined when <code>value</code> is
9232 anything other than a signed 16-bit integer.</p>
9234-js"><span class="hljs-keyword">const</span> buf = <span class="hljs-title class_">Buffer</span>.<…
9236 buf.<span class="hljs-title function_">writeInt16LE</span>(<span class="hljs-number">0x0304</span>,…
9238 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(…
9239 <span class="hljs-comment">// Prints: &#x3C;Buffer 04 03></span></code></pre>
9253 <li><code>value</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struct…
9254 <li><code>offset</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struc…
9255 satisfy <code>0 &#x3C;= offset &#x3C;= buf.length - 4</code>. <strong>Default:</strong> <code>0</co…
9256 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Numbe…
9258 <p>Writes <code>value</code> to <code>buf</code> at the specified <code>offset</code> as big-endian…
9259 must be a valid signed 32-bit integer. Behavior is undefined when <code>value</code> is
9260 anything other than a signed 32-bit integer.</p>
9262-js"><span class="hljs-keyword">const</span> buf = <span class="hljs-title class_">Buffer</span>.<…
9264 buf.<span class="hljs-title function_">writeInt32BE</span>(<span class="hljs-number">0x01020304</sp…
9266 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(…
9267 <span class="hljs-comment">// Prints: &#x3C;Buffer 01 02 03 04></span></code></pre>
9281 <li><code>value</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struct…
9282 <li><code>offset</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struc…
9283 satisfy <code>0 &#x3C;= offset &#x3C;= buf.length - 4</code>. <strong>Default:</strong> <code>0</co…
9284 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Numbe…
9286 <p>Writes <code>value</code> to <code>buf</code> at the specified <code>offset</code> as little-end…
9287 must be a valid signed 32-bit integer. Behavior is undefined when <code>value</code> is
9288 anything other than a signed 32-bit integer.</p>
9290-js"><span class="hljs-keyword">const</span> buf = <span class="hljs-title class_">Buffer</span>.<…
9292 buf.<span class="hljs-title function_">writeInt32LE</span>(<span class="hljs-number">0x05060708</sp…
9294 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(…
9295 <span class="hljs-comment">// Prints: &#x3C;Buffer 08 07 06 05></span></code></pre>
9309 <li><code>value</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struct…
9310 <li><code>offset</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struc…
9311 satisfy <code>0 &#x3C;= offset &#x3C;= buf.length - byteLength</code>.</li>
9312 <li><code>byteLength</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_s…
9314 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Numbe…
9317 as big-endian. Supports up to 48 bits of accuracy. Behavior is undefined when
9319-js"><span class="hljs-keyword">const</span> buf = <span class="hljs-title class_">Buffer</span>.<…
9321 …s="hljs-title function_">writeIntBE</span>(<span class="hljs-number">0x1234567890ab</span>, <span …
9323 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(…
9324 <span class="hljs-comment">// Prints: &#x3C;Buffer 12 34 56 78 90 ab></span></code></pre>
9338 <li><code>value</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struct…
9339 <li><code>offset</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struc…
9340 satisfy <code>0 &#x3C;= offset &#x3C;= buf.length - byteLength</code>.</li>
9341 <li><code>byteLength</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_s…
9343 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Numbe…
9346 as little-endian. Supports up to 48 bits of accuracy. Behavior is undefined
9348-js"><span class="hljs-keyword">const</span> buf = <span class="hljs-title class_">Buffer</span>.<…
9350 …s="hljs-title function_">writeIntLE</span>(<span class="hljs-number">0x1234567890ab</span>, <span …
9352 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(…
9353 <span class="hljs-comment">// Prints: &#x3C;Buffer ab 90 78 56 34 12></span></code></pre>
9369 <li><code>value</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struct…
9370 <li><code>offset</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struc…
9371 satisfy <code>0 &#x3C;= offset &#x3C;= buf.length - 1</code>. <strong>Default:</strong> <code>0</co…
9372 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Numbe…
9375 valid unsigned 8-bit integer. Behavior is undefined when <code>value</code> is anything
9376 other than an unsigned 8-bit integer.</p>
9378-js"><span class="hljs-keyword">const</span> buf = <span class="hljs-title class_">Buffer</span>.<…
9380 buf.<span class="hljs-title function_">writeUInt8</span>(<span class="hljs-number">0x3</span>, <spa…
9381 buf.<span class="hljs-title function_">writeUInt8</span>(<span class="hljs-number">0x4</span>, <spa…
9382 buf.<span class="hljs-title function_">writeUInt8</span>(<span class="hljs-number">0x23</span>, <sp…
9383 buf.<span class="hljs-title function_">writeUInt8</span>(<span class="hljs-number">0x42</span>, <sp…
9385 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(…
9386 <span class="hljs-comment">// Prints: &#x3C;Buffer 03 04 23 42></span></code></pre>
9402 <li><code>value</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struct…
9403 <li><code>offset</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struc…
9404 satisfy <code>0 &#x3C;= offset &#x3C;= buf.length - 2</code>. <strong>Default:</strong> <code>0</co…
9405 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Numbe…
9407 <p>Writes <code>value</code> to <code>buf</code> at the specified <code>offset</code> as big-endian…
9408 must be a valid unsigned 16-bit integer. Behavior is undefined when <code>value</code>
9409 is anything other than an unsigned 16-bit integer.</p>
9411-js"><span class="hljs-keyword">const</span> buf = <span class="hljs-title class_">Buffer</span>.<…
9413 buf.<span class="hljs-title function_">writeUInt16BE</span>(<span class="hljs-number">0xdead</span>…
9414 buf.<span class="hljs-title function_">writeUInt16BE</span>(<span class="hljs-number">0xbeef</span>…
9416 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(…
9417 <span class="hljs-comment">// Prints: &#x3C;Buffer de ad be ef></span></code></pre>
9433 <li><code>value</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struct…
9434 <li><code>offset</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struc…
9435 satisfy <code>0 &#x3C;= offset &#x3C;= buf.length - 2</code>. <strong>Default:</strong> <code>0</co…
9436 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Numbe…
9438 <p>Writes <code>value</code> to <code>buf</code> at the specified <code>offset</code> as little-end…
9439 must be a valid unsigned 16-bit integer. Behavior is undefined when <code>value</code> is
9440 anything other than an unsigned 16-bit integer.</p>
9442-js"><span class="hljs-keyword">const</span> buf = <span class="hljs-title class_">Buffer</span>.<…
9444 buf.<span class="hljs-title function_">writeUInt16LE</span>(<span class="hljs-number">0xdead</span>…
9445 buf.<span class="hljs-title function_">writeUInt16LE</span>(<span class="hljs-number">0xbeef</span>…
9447 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(…
9448 <span class="hljs-comment">// Prints: &#x3C;Buffer ad de ef be></span></code></pre>
9464 <li><code>value</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struct…
9465 <li><code>offset</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struc…
9466 satisfy <code>0 &#x3C;= offset &#x3C;= buf.length - 4</code>. <strong>Default:</strong> <code>0</co…
9467 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Numbe…
9469 <p>Writes <code>value</code> to <code>buf</code> at the specified <code>offset</code> as big-endian…
9470 must be a valid unsigned 32-bit integer. Behavior is undefined when <code>value</code>
9471 is anything other than an unsigned 32-bit integer.</p>
9473-js"><span class="hljs-keyword">const</span> buf = <span class="hljs-title class_">Buffer</span>.<…
9475 buf.<span class="hljs-title function_">writeUInt32BE</span>(<span class="hljs-number">0xfeedface</s…
9477 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(…
9478 <span class="hljs-comment">// Prints: &#x3C;Buffer fe ed fa ce></span></code></pre>
9494 <li><code>value</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struct…
9495 <li><code>offset</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struc…
9496 satisfy <code>0 &#x3C;= offset &#x3C;= buf.length - 4</code>. <strong>Default:</strong> <code>0</co…
9497 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Numbe…
9499 <p>Writes <code>value</code> to <code>buf</code> at the specified <code>offset</code> as little-end…
9500 must be a valid unsigned 32-bit integer. Behavior is undefined when <code>value</code> is
9501 anything other than an unsigned 32-bit integer.</p>
9503-js"><span class="hljs-keyword">const</span> buf = <span class="hljs-title class_">Buffer</span>.<…
9505 buf.<span class="hljs-title function_">writeUInt32LE</span>(<span class="hljs-number">0xfeedface</s…
9507 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(…
9508 <span class="hljs-comment">// Prints: &#x3C;Buffer ce fa ed fe></span></code></pre>
9524 <li><code>value</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struct…
9525 <li><code>offset</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struc…
9526 satisfy <code>0 &#x3C;= offset &#x3C;= buf.length - byteLength</code>.</li>
9527 <li><code>byteLength</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_s…
9529 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Numbe…
9532 as big-endian. Supports up to 48 bits of accuracy. Behavior is undefined
9535-js"><span class="hljs-keyword">const</span> buf = <span class="hljs-title class_">Buffer</span>.<…
9537 …="hljs-title function_">writeUIntBE</span>(<span class="hljs-number">0x1234567890ab</span>, <span …
9539 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(…
9540 <span class="hljs-comment">// Prints: &#x3C;Buffer 12 34 56 78 90 ab></span></code></pre>
9556 <li><code>value</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struct…
9557 <li><code>offset</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struc…
9558 satisfy <code>0 &#x3C;= offset &#x3C;= buf.length - byteLength</code>.</li>
9559 <li><code>byteLength</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_s…
9561 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Numbe…
9564 as little-endian. Supports up to 48 bits of accuracy. Behavior is undefined
9567-js"><span class="hljs-keyword">const</span> buf = <span class="hljs-title class_">Buffer</span>.<…
9569 …="hljs-title function_">writeUIntLE</span>(<span class="hljs-number">0x1234567890ab</span>, <span …
9571 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(…
9572 <span class="hljs-comment">// Prints: &#x3C;Buffer ab 90 78 56 34 12></span></code></pre>
9589 … api_stability_0"><a href="#documentation_stability_index">Stability: 0</a> - Deprecated: Use <a h…
9591 <li><code>array</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struct…
9614 …bility api_stability_0"><a href="#documentation_stability_index">Stability: 0</a> - Deprecated: Use
9618-US/docs/Web/JavaScript/Reference/Global_Objects/ArrayBuffer" class="type">&#x3C;ArrayBuffer></a> …
9619-US/docs/Web/JavaScript/Reference/Global_Objects/SharedArrayBuffer"><code>SharedArrayBuffer</code>…
9620 <li><code>byteOffset</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_s…
9621 <li><code>length</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struc…
9622 <strong>Default:</strong> <code>arrayBuffer.byteLength - byteOffset</code>.</li>
9642 … api_stability_0"><a href="#documentation_stability_index">Stability: 0</a> - Deprecated: Use <a h…
9644-US/docs/Web/JavaScript/Reference/Global_Objects/Uint8Array" class="type">&#x3C;Uint8Array></a> An…
9656 <td><p>The <code>new Buffer(size)</code> will return zero-filled memory by default.</p></td></tr>
9666 … api_stability_0"><a href="#documentation_stability_index">Stability: 0</a> - Deprecated: Use <a h…
9669 <li><code>size</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
9689 …_stability api_stability_0"><a href="#documentation_stability_index">Stability: 0</a> - Deprecated:
9692 <li><code>string</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struc…
9693 <li><code>encoding</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_str…
9698 <code>Buffer</code>-related APIs that are available only via the <code>buffer</code> module
9705 …ttps://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Data_types" class="type">&#…
9707 <p>Decodes a string of Base64-encoded data into bytes, and encodes those bytes
9708 into a string using Latin-1 (ISO-8859-1).</p>
9709 <p>The <code>data</code> may be any JavaScript-value that can be coerced into a string.</p>
9713 For code running using Node.js APIs, converting between base64-encoded strings
9721 <li><code>data</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
9723 <p>Decodes a string into bytes using Latin-1 (ISO-8859), and encodes those bytes
9725 <p>The <code>data</code> may be any JavaScript-value that can be coerced into a string.</p>
9729 For code running using Node.js APIs, converting between base64-encoded strings
9737 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" c…
9747 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" c…
9755 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" c…
9771 …" class="type">&#x3C;Buffer></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScrip…
9772 <li><code>fromEnc</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_stru…
9773 …tps://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#…
9776 <p>Re-encodes the given <code>Buffer</code> or <code>Uint8Array</code> instance from one character
9783 sequence cannot be adequately represented in the target encoding. For instance:</p>
9784 …ode class="language-js"><span class="hljs-keyword">const</span> buffer = <span class="hljs-built_i…
9786-keyword">const</span> newBuf = buffer.<span class="hljs-title function_">transcode</span>(<span c…
9787-variable language_">console</span>.<span class="hljs-title function_">log</span>(newBuf.<span cla…
9788 <span class="hljs-comment">// Prints: '?'</span></code></pre>
9789 <p>Because the Euro (<code>€</code>) sign is not representable in US-ASCII, it is replaced
9795 … api_stability_0"><a href="#documentation_stability_index">Stability: 0</a> - Deprecated: Use <a h…
9803 … api_stability_0"><a href="#documentation_stability_index">Stability: 0</a> - Deprecated: Use <a h…
9805 <li><code>size</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
9818 <td><p>Value is changed from 2<sup>31</sup> - 1 to 2<sup>32</sup> - 1 on 64-bit architectures.</p><…
9825 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" c…
9827 <p>On 32-bit architectures, this value currently is 2<sup>30</sup> - 1 (~1GB).</p>
9828 <p>On 64-bit architectures, this value currently is 2<sup>32</sup> - 1 (~4GB).</p>
9836 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" c…
9839 in UTF-16 code units.</p>
9854 required between creating a fast-but-uninitialized <code>Buffer</code> versus creating a
9855 slower-but-safer <code>Buffer</code>. Since Node.js 8.0.0, <code>Buffer(num)</code> and <code>new B…
9858 ….org/en-US/docs/Web/JavaScript/Reference/Global_Objects/ArrayBuffer"><code>ArrayBuffer</code></a> …
9873 arbitrary pre-existing in-memory data, so may be used to expose in-memory
9875 occur because the data is zero-filled. However, other attacks are still
9878 <p>To make the creation of <code>Buffer</code> instances more reliable and less error-prone,
9889 <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/ArrayBuff…
9910 <h4>The <code>--zero-fill-buffers</code> command-line option<span><a class="mark" href="#buffer_the…
9914 <p>Node.js can be started using the <code>--zero-fill-buffers</code> command-line option to
9915 cause all newly-allocated <code>Buffer</code> instances to be zero-filled upon creation by
9917 …ode>Buffer.allocUnsafeSlow()</code></a>, and <code>new SlowBuffer(size)</code> are not zero-filled.
9919 <code>--zero-fill-buffers</code> option only when necessary to enforce that newly allocated
9921 <pre><code class="language-console"><span class="hljs-meta">$ </span><span class="language-bash">no…
9922 <span class="hljs-meta">> </span><span class="language-bash">Buffer.allocUnsafe(5);</span>
9926 segment of allocated memory is <em>uninitialized</em> (it is not zeroed-out). While
9938 <p><em>Addons</em> are dynamically-linked shared objects written in C++. The
9941 <p>There are three options for implementing addons: Node-API, nan, or direct
9943 direct access to functionality which is not exposed by Node-API, use Node-API.
9944 Refer to <a href="n-api.html">C/C++ addons with Node-API</a> for more information on
9945 Node-API.</p>
9946 <p>When not using Node-API, implementing addons is complicated,
9959 serves as a cross-platform abstraction library, giving easy, POSIX-like
9965 avoid blocking the event loop with I/O or other time-intensive tasks by
9966 offloading work via libuv to non-blocking system operations, worker threads,
9977 re-exported by Node.js and may be used to various extents by addons. See
9981 <p>All of the following examples are available for <a href="https://github.com/nodejs/node-addon-ex…
9982 be used as the starting-point for an addon.</p>
9986-js"><span class="hljs-variable language_">module</span>.<span class="hljs-property">exports</span…
9988 <pre><code class="language-cpp"><span class="hljs-comment">// hello.cc</span>
9989 <span class="hljs-meta">#<span class="hljs-keyword">include</span> <span class="hljs-string">&#x3C;…
9991 <span class="hljs-keyword">namespace</span> demo {
9993 <span class="hljs-keyword">using</span> v8::FunctionCallbackInfo;
9994 <span class="hljs-keyword">using</span> v8::Isolate;
9995 <span class="hljs-keyword">using</span> v8::Local;
9996 <span class="hljs-keyword">using</span> v8::Object;
9997 <span class="hljs-keyword">using</span> v8::String;
9998 <span class="hljs-keyword">using</span> v8::Value;
10000 …class="hljs-function"><span class="hljs-type">void</span> <span class="hljs-title">Method</span><s…
10001 Isolate* isolate = args.<span class="hljs-built_in">GetIsolate</span>();
10002 …args.<span class="hljs-built_in">GetReturnValue</span>().<span class="hljs-built_in">Set</span>(St…
10003 …isolate, <span class="hljs-string">"world"</span>).<span class="hljs-built_in">ToLocalChecked</spa…
10006 <span class="hljs-function"><span class="hljs-type">void</span> <span class="hljs-title">Initialize…
10007 …<span class="hljs-built_in">NODE_SET_METHOD</span>(exports, <span class="hljs-string">"hello"</spa…
10010 <span class="hljs-built_in">NODE_MODULE</span>(NODE_GYP_MODULE_NAME, Initialize)
10012 } <span class="hljs-comment">// namespace demo</span></code></pre>
10015 …s="language-cpp"><span class="hljs-function"><span class="hljs-type">void</span> <span class="hljs
10016 <span class="hljs-built_in">NODE_MODULE</span>(NODE_GYP_MODULE_NAME, Initialize)</code></pre>
10017 <p>There is no semi-colon after <code>NODE_MODULE</code> as it's not a function (see
10023 <p>When building addons with <code>node-gyp</code>, using the macro <code>NODE_GYP_MODULE_NAME</cod…
10026 <h4>Context-aware addons<span><a class="mark" href="#addons_context_aware_addons" id="addons_contex…
10033 <p>A context-aware addon can be constructed by using the macro
10037 <pre><code class="language-cpp"><span class="hljs-keyword">using</span> <span class="hljs-keyword">…
10039 …n class="hljs-keyword">extern</span> <span class="hljs-string">"C"</span> <span class="hljs-functi…
10040 <span class="hljs-title">NODE_MODULE_INITIALIZER</span><span class="hljs-params">(Local&#x3C;Object…
10041 Local&#x3C;Value> <span class="hljs-keyword">module</span>,
10043 <span class="hljs-comment">/* Perform addon initialization steps here. */</span>
10046 construct a context-aware addon. Unlike <code>NODE_MODULE()</code>, which is used to
10057 <p>The choice to build a context-aware addon carries with it the responsibility of
10065 <p>The context-aware addon can be structured to avoid global static data by
10068 <li>Define a class which will hold per-addon-instance data and which has a static
10070-cpp"><span class="hljs-function"><span class="hljs-keyword">static</span> <span class="hljs-type"…
10071 <span class="hljs-comment">// Cast `data` to an instance of the class and delete it.</span>
10074 <li>Heap-allocate an instance of this class in the addon initializer. This can be
10076 <li>Call <code>node::AddEnvironmentCleanupHook()</code>, passing it the above-created
10082 native-backed JavaScript functions. The third parameter of
10087 <p>This will ensure that the per-addon-instance data reaches each binding that can
10088 be called from JavaScript. The per-addon-instance data must also be passed into
10090 <p>The following example illustrates the implementation of a context-aware addon:</p>
10091 <pre><code class="language-cpp"><span class="hljs-meta">#<span class="hljs-keyword">include</span> …
10093 <span class="hljs-keyword">using</span> <span class="hljs-keyword">namespace</span> v8;
10095 <span class="hljs-keyword">class</span> <span class="hljs-title class_">AddonData</span> {
10096 <span class="hljs-keyword">public</span>:
10097 …<span class="hljs-function"><span class="hljs-keyword">explicit</span> <span class="hljs-title">Ad…
10098 call_count(<span class="hljs-number">0</span>) {</span>
10099 …<span class="hljs-comment">// Ensure this per-addon-instance data is deleted at environment cleanu…
10100 …node::<span class="hljs-built_in">AddEnvironmentCleanupHook</span>(isolate, DeleteInstance, <span …
10103 <span class="hljs-comment">// Per-addon data.</span>
10104 <span class="hljs-type">int</span> call_count;
10106-function"><span class="hljs-keyword">static</span> <span class="hljs-type">void</span> <span clas…
10107 …<span class="hljs-keyword">delete</span> <span class="hljs-built_in">static_cast</span>&#x3C;Addon…
10111-function"><span class="hljs-keyword">static</span> <span class="hljs-type">void</span> <span clas…
10112 <span class="hljs-comment">// Retrieve the per-addon-instance data.</span>
10114-built_in">reinterpret_cast</span>&#x3C;AddonData*>(info.<span class="hljs-built_in">Data</span>()…
10115 data->call_count++;
10116 …<span class="hljs-built_in">GetReturnValue</span>().<span class="hljs-built_in">Set</span>((<span …
10119 <span class="hljs-comment">// Initialize this addon to be context-aware.</span>
10120 <span class="hljs-built_in">NODE_MODULE_INIT</span>(<span class="hljs-comment">/* exports, module, …
10121 Isolate* isolate = context-><span class="hljs-built_in">GetIsolate</span>();
10123 …<span class="hljs-comment">// Create a new instance of `AddonData` for this instance of the addon …
10124 <span class="hljs-comment">// tie its life cycle to that of the Node.js environment.</span>
10125 …AddonData* data = <span class="hljs-keyword">new</span> <span class="hljs-built_in">AddonData</spa…
10127 …<span class="hljs-comment">// Wrap the data in a `v8::External` so we can pass it to the method we…
10128 <span class="hljs-comment">// expose.</span>
10129 Local&#x3C;External> external = External::<span class="hljs-built_in">New</span>(isolate, data);
10131 …<span class="hljs-comment">// Expose the method `Method` to JavaScript, and make sure it receives …
10132 …<span class="hljs-comment">// per-addon-instance data we created above by passing `external` as th…
10133 <span class="hljs-comment">// third parameter to the `FunctionTemplate` constructor.</span>
10134 exports-><span class="hljs-built_in">Set</span>(context,
10135 …String::<span class="hljs-built_in">NewFromUtf8</span>(isolate, <span class="hljs-string">"method"…
10136 FunctionTemplate::<span class="hljs-built_in">New</span>(isolate, Method, external)
10137-><span class="hljs-built_in">GetFunction</span>(context).<span class="hljs-built_in">ToLocalCheck…
10150 such as a main thread and a Worker thread, an add-on needs to either:</p>
10152 <li>Be an Node-API addon, or</li>
10153 <li>Be declared as context-aware using <code>NODE_MODULE_INIT()</code> as described above</li>
10158 …uage-cpp"><span class="hljs-function"><span class="hljs-type">void</span> <span class="hljs-title"…
10159 … <span class="hljs-type">void</span> (*fun)(<span class="hljs-type">void</span>* arg),
10160 <span class="hljs-type">void</span>* arg)</span></span>;</code></pre>
10164 run in last-in first-out order.</p>
10170 <pre><code class="language-cpp"><span class="hljs-comment">// addon.cc</span>
10171 <span class="hljs-meta">#<span class="hljs-keyword">include</span> <span class="hljs-string">&#x3C;…
10172 <span class="hljs-meta">#<span class="hljs-keyword">include</span> <span class="hljs-string">&#x3C;…
10173 <span class="hljs-meta">#<span class="hljs-keyword">include</span> <span class="hljs-string">&#x3C;…
10175 <span class="hljs-keyword">using</span> node::AddEnvironmentCleanupHook;
10176 <span class="hljs-keyword">using</span> v8::HandleScope;
10177 <span class="hljs-keyword">using</span> v8::Isolate;
10178 <span class="hljs-keyword">using</span> v8::Local;
10179 <span class="hljs-keyword">using</span> v8::Object;
10181 <span class="hljs-comment">// Note: In a real-world application, do not rely on static/global data.…
10182 <span class="hljs-keyword">static</span> <span class="hljs-type">char</span> cookie[] = <span class…
10183 <span class="hljs-keyword">static</span> <span class="hljs-type">int</span> cleanup_cb1_called = <s…
10184 <span class="hljs-keyword">static</span> <span class="hljs-type">int</span> cleanup_cb2_called = <s…
10186-function"><span class="hljs-keyword">static</span> <span class="hljs-type">void</span> <span clas…
10187 Isolate* isolate = <span class="hljs-built_in">static_cast</span>&#x3C;Isolate*>(arg);
10188 …<span class="hljs-function">HandleScope <span class="hljs-title">scope</span><span class="hljs-par…
10189 Local&#x3C;Object> obj = Object::<span class="hljs-built_in">New</span>(isolate);
10190 …<span class="hljs-built_in">assert</span>(!obj.<span class="hljs-built_in">IsEmpty</span>()); <sp…
10191 <span class="hljs-built_in">assert</span>(obj-><span class="hljs-built_in">IsObject</span>());
10195-function"><span class="hljs-keyword">static</span> <span class="hljs-type">void</span> <span clas…
10196 …<span class="hljs-built_in">assert</span>(arg == <span class="hljs-built_in">static_cast</span>&#x…
10200-function"><span class="hljs-keyword">static</span> <span class="hljs-type">void</span> <span clas…
10201 …<span class="hljs-built_in">assert</span>(cleanup_cb1_called == <span class="hljs-number">1</span>…
10202 …<span class="hljs-built_in">assert</span>(cleanup_cb2_called == <span class="hljs-number">1</span>…
10205 <span class="hljs-comment">// Initialize this addon to be context-aware.</span>
10206 <span class="hljs-built_in">NODE_MODULE_INIT</span>(<span class="hljs-comment">/* exports, module, …
10207 Isolate* isolate = context-><span class="hljs-built_in">GetIsolate</span>();
10209 …<span class="hljs-built_in">AddEnvironmentCleanupHook</span>(isolate, sanity_check, <span class="h…
10210 <span class="hljs-built_in">AddEnvironmentCleanupHook</span>(isolate, cleanup_cb2, cookie);
10211 <span class="hljs-built_in">AddEnvironmentCleanupHook</span>(isolate, cleanup_cb1, isolate);
10214 <pre><code class="language-js"><span class="hljs-comment">// test.js</span>
10215 <span class="hljs-built_in">require</span>(<span class="hljs-string">'./build/Release/addon'</span>…
10219 top-level of the project describing the build configuration of the module
10220 using a JSON-like format. This file is used by <a href="https://github.com/nodejs/node-gyp">node-gy…
10222 <pre><code class="language-json"><span class="hljs-punctuation">{</span>
10223 …<span class="hljs-attr">"targets"</span><span class="hljs-punctuation">:</span> <span class="hljs-
10224 <span class="hljs-punctuation">{</span>
10225 …an class="hljs-attr">"target_name"</span><span class="hljs-punctuation">:</span> <span class="hljs
10226-attr">"sources"</span><span class="hljs-punctuation">:</span> <span class="hljs-punctuation">[</s…
10227 <span class="hljs-punctuation">}</span>
10228 <span class="hljs-punctuation">]</span>
10229 <span class="hljs-punctuation">}</span></code></pre>
10230 <p>A version of the <code>node-gyp</code> utility is bundled and distributed with
10234 use <code>node-gyp</code> directly can install it using the command
10235 <code>npm install -g node-gyp</code>. See the <code>node-gyp</code> <a href="https://github.com/nod…
10236 more information, including platform-specific requirements.</p>
10237 <p>Once the <code>binding.gyp</code> file has been created, use <code>node-gyp configure</code> to
10241 <p>Next, invoke the <code>node-gyp build</code> command to generate the compiled <code>addon.node</…
10244 version of <code>node-gyp</code> to perform this same set of actions, generating a
10248 <pre><code class="language-js"><span class="hljs-comment">// hello.js</span>
10249 <span class="hljs-keyword">const</span> addon = <span class="hljs-built_in">require</span>(<span cl…
10251 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(…
10252 <span class="hljs-comment">// Prints: 'world'</span></code></pre>
10255 the <a href="https://github.com/TooTallNate/node-bindings">bindings</a> package to load the compile…
10258 <pre><code class="language-js"><span class="hljs-keyword">try</span> {
10259 …<span class="hljs-keyword">return</span> <span class="hljs-built_in">require</span>(<span class="h…
10260 } <span class="hljs-keyword">catch</span> (err) {
10261 …<span class="hljs-keyword">return</span> <span class="hljs-built_in">require</span>(<span class="h…
10267 ….></code> statements (e.g. <code>#include &#x3C;v8.h></code>) and <code>node-gyp</code> will locate
10272 <p>When <code>node-gyp</code> runs, it will detect the specific release version of Node.js
10279 <p><code>node-gyp</code> can be run using the <code>--nodedir</code> flag pointing at a local Node.…
10287 files with the <code>.node</code> file extension and initialize those as dynamically-linked
10308 </section><section><h3>Node-API<span><a class="mark" href="#addons_node_api" id="addons_node_api">#…
10309 …ty api_stability_2"><a href="#documentation_stability_index">Stability: 2</a> - Stable</div><p></p>
10310 <p>Node-API is an API for building native addons. It is independent from
10317 outlined in this document (node-gyp, etc.). The only difference is the
10320 in the Node-API are used.</p>
10322 provided by Node-API carries with it certain
10324 <p>To use Node-API in the above "Hello world" example, replace the content of
10326 <pre><code class="language-cpp"><span class="hljs-comment">// hello.cc using Node-API</span>
10327 <span class="hljs-meta">#<span class="hljs-keyword">include</span> <span class="hljs-string">&#x3C;…
10329 <span class="hljs-keyword">namespace</span> demo {
10331 <span class="hljs-function">napi_value <span class="hljs-title">Method</span><span class="hljs-para…
10335 …status = <span class="hljs-built_in">napi_create_string_utf8</span>(env, <span class="hljs-string"…
10336 …<span class="hljs-keyword">if</span> (status != napi_ok) <span class="hljs-keyword">return</span> …
10337 <span class="hljs-keyword">return</span> greeting;
10340 <span class="hljs-function">napi_value <span class="hljs-title">init</span><span class="hljs-params…
10344 …js-built_in">napi_create_function</span>(env, <span class="hljs-literal">nullptr</span>, <span cla…
10345 …<span class="hljs-keyword">if</span> (status != napi_ok) <span class="hljs-keyword">return</span> …
10347 …status = <span class="hljs-built_in">napi_set_named_property</span>(env, exports, <span class="hlj…
10348 …<span class="hljs-keyword">if</span> (status != napi_ok) <span class="hljs-keyword">return</span> …
10349 <span class="hljs-keyword">return</span> exports;
10352 <span class="hljs-built_in">NAPI_MODULE</span>(NODE_GYP_MODULE_NAME, init)
10354 } <span class="hljs-comment">// namespace demo</span></code></pre>
10356 <a href="n-api.html">C/C++ addons with Node-API</a>.</p>
10364 <pre><code class="language-json"><span class="hljs-punctuation">{</span>
10365 …<span class="hljs-attr">"targets"</span><span class="hljs-punctuation">:</span> <span class="hljs-
10366 <span class="hljs-punctuation">{</span>
10367 …an class="hljs-attr">"target_name"</span><span class="hljs-punctuation">:</span> <span class="hljs
10368-attr">"sources"</span><span class="hljs-punctuation">:</span> <span class="hljs-punctuation">[</s…
10369 <span class="hljs-punctuation">}</span>
10370 <span class="hljs-punctuation">]</span>
10371 <span class="hljs-punctuation">}</span></code></pre>
10374-json"><span class="hljs-attr">"sources"</span><span class="hljs-punctuation">:</span> <span class…
10376 built using <code>node-gyp</code>:</p>
10377 <pre><code class="language-console"><span class="hljs-meta">$ </span><span class="language-bash">no…
10385 <pre><code class="language-cpp"><span class="hljs-comment">// addon.cc</span>
10386 <span class="hljs-meta">#<span class="hljs-keyword">include</span> <span class="hljs-string">&#x3C;…
10388 <span class="hljs-keyword">namespace</span> demo {
10390 <span class="hljs-keyword">using</span> v8::Exception;
10391 <span class="hljs-keyword">using</span> v8::FunctionCallbackInfo;
10392 <span class="hljs-keyword">using</span> v8::Isolate;
10393 <span class="hljs-keyword">using</span> v8::Local;
10394 <span class="hljs-keyword">using</span> v8::Number;
10395 <span class="hljs-keyword">using</span> v8::Object;
10396 <span class="hljs-keyword">using</span> v8::String;
10397 <span class="hljs-keyword">using</span> v8::Value;
10399 <span class="hljs-comment">// This is the implementation of the "add" method</span>
10400 <span class="hljs-comment">// Input arguments are passed using the</span>
10401 <span class="hljs-comment">// const FunctionCallbackInfo&#x3C;Value>&#x26; args struct</span>
10402 …n class="hljs-function"><span class="hljs-type">void</span> <span class="hljs-title">Add</span><sp…
10403 Isolate* isolate = args.<span class="hljs-built_in">GetIsolate</span>();
10405 <span class="hljs-comment">// Check the number of arguments passed.</span>
10406 …<span class="hljs-keyword">if</span> (args.<span class="hljs-built_in">Length</span>() &#x3C; <spa…
10407 <span class="hljs-comment">// Throw an Error that is passed back to JavaScript</span>
10408 …isolate-><span class="hljs-built_in">ThrowException</span>(Exception::<span class="hljs-built_in">…
10409 String::<span class="hljs-built_in">NewFromUtf8</span>(isolate,
10410 …<span class="hljs-string">"Wrong number of arguments"</span>).<span class="hljs-built_in">ToLocalC…
10411 <span class="hljs-keyword">return</span>;
10414 <span class="hljs-comment">// Check the argument types</span>
10415-keyword">if</span> (!args[<span class="hljs-number">0</span>]-><span class="hljs-built_in">IsNumb…
10416 …isolate-><span class="hljs-built_in">ThrowException</span>(Exception::<span class="hljs-built_in">…
10417 String::<span class="hljs-built_in">NewFromUtf8</span>(isolate,
10418 …<span class="hljs-string">"Wrong arguments"</span>).<span class="hljs-built_in">ToLocalChecked</sp…
10419 <span class="hljs-keyword">return</span>;
10422 <span class="hljs-comment">// Perform the operation</span>
10423 <span class="hljs-type">double</span> value =
10424-number">0</span>].<span class="hljs-built_in">As</span>&#x3C;Number>()-><span class="hljs-built_i…
10425 Local&#x3C;Number> num = Number::<span class="hljs-built_in">New</span>(isolate, value);
10427 <span class="hljs-comment">// Set the return value (using the passed in</span>
10428 <span class="hljs-comment">// FunctionCallbackInfo&#x3C;Value>&#x26;)</span>
10429 …args.<span class="hljs-built_in">GetReturnValue</span>().<span class="hljs-built_in">Set</span>(nu…
10432 <span class="hljs-function"><span class="hljs-type">void</span> <span class="hljs-title">Init</span…
10433 …<span class="hljs-built_in">NODE_SET_METHOD</span>(exports, <span class="hljs-string">"add"</span>…
10436 <span class="hljs-built_in">NODE_MODULE</span>(NODE_GYP_MODULE_NAME, Init)
10438 } <span class="hljs-comment">// namespace demo</span></code></pre>
10440 <pre><code class="language-js"><span class="hljs-comment">// test.js</span>
10441 <span class="hljs-keyword">const</span> addon = <span class="hljs-built_in">require</span>(<span cl…
10443-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hlj…
10448 <pre><code class="language-cpp"><span class="hljs-comment">// addon.cc</span>
10449 <span class="hljs-meta">#<span class="hljs-keyword">include</span> <span class="hljs-string">&#x3C;…
10451 <span class="hljs-keyword">namespace</span> demo {
10453 <span class="hljs-keyword">using</span> v8::Context;
10454 <span class="hljs-keyword">using</span> v8::Function;
10455 <span class="hljs-keyword">using</span> v8::FunctionCallbackInfo;
10456 <span class="hljs-keyword">using</span> v8::Isolate;
10457 <span class="hljs-keyword">using</span> v8::Local;
10458 <span class="hljs-keyword">using</span> v8::Null;
10459 <span class="hljs-keyword">using</span> v8::Object;
10460 <span class="hljs-keyword">using</span> v8::String;
10461 <span class="hljs-keyword">using</span> v8::Value;
10463 …ass="hljs-function"><span class="hljs-type">void</span> <span class="hljs-title">RunCallback</span…
10464 Isolate* isolate = args.<span class="hljs-built_in">GetIsolate</span>();
10465 Local&#x3C;Context> context = isolate-><span class="hljs-built_in">GetCurrentContext</span>();
10466 …unction> cb = Local&#x3C;Function>::<span class="hljs-built_in">Cast</span>(args[<span class="hljs
10467 …<span class="hljs-keyword">const</span> <span class="hljs-keyword">unsigned</span> argc = <span cl…
10469 String::<span class="hljs-built_in">NewFromUtf8</span>(isolate,
10470 …<span class="hljs-string">"hello world"</span>).<span class="hljs-built_in">ToLocalChecked</span>(…
10471 …cb-><span class="hljs-built_in">Call</span>(context, <span class="hljs-built_in">Null</span>(isola…
10474-function"><span class="hljs-type">void</span> <span class="hljs-title">Init</span><span class="hl…
10475 …<span class="hljs-built_in">NODE_SET_METHOD</span>(<span class="hljs-keyword">module</span>, <span…
10478 <span class="hljs-built_in">NODE_MODULE</span>(NODE_GYP_MODULE_NAME, Init)
10480 } <span class="hljs-comment">// namespace demo</span></code></pre>
10481 <p>This example uses a two-argument form of <code>Init()</code> that receives the full
10486 <pre><code class="language-js"><span class="hljs-comment">// test.js</span>
10487 <span class="hljs-keyword">const</span> addon = <span class="hljs-built_in">require</span>(<span cl…
10489 <span class="hljs-title function_">addon</span>(<span class="hljs-function">(<span class="hljs-para…
10490 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>…
10491 <span class="hljs-comment">// Prints: 'hello world'</span>
10498 <pre><code class="language-cpp"><span class="hljs-comment">// addon.cc</span>
10499 <span class="hljs-meta">#<span class="hljs-keyword">include</span> <span class="hljs-string">&#x3C;…
10501 <span class="hljs-keyword">namespace</span> demo {
10503 <span class="hljs-keyword">using</span> v8::Context;
10504 <span class="hljs-keyword">using</span> v8::FunctionCallbackInfo;
10505 <span class="hljs-keyword">using</span> v8::Isolate;
10506 <span class="hljs-keyword">using</span> v8::Local;
10507 <span class="hljs-keyword">using</span> v8::Object;
10508 <span class="hljs-keyword">using</span> v8::String;
10509 <span class="hljs-keyword">using</span> v8::Value;
10511 …ss="hljs-function"><span class="hljs-type">void</span> <span class="hljs-title">CreateObject</span…
10512 Isolate* isolate = args.<span class="hljs-built_in">GetIsolate</span>();
10513 Local&#x3C;Context> context = isolate-><span class="hljs-built_in">GetCurrentContext</span>();
10515 Local&#x3C;Object> obj = Object::<span class="hljs-built_in">New</span>(isolate);
10516 obj-><span class="hljs-built_in">Set</span>(context,
10517 String::<span class="hljs-built_in">NewFromUtf8</span>(isolate,
10518 … <span class="hljs-string">"msg"</span>).<span class="hljs-built_in">ToLocalChecked</span>(),
10519 …args[<span class="hljs-number">0</span>]-><span class="hljs-built_in">ToString</span>(context).<sp…
10520 .<span class="hljs-built_in">FromJust</span>();
10522 …args.<span class="hljs-built_in">GetReturnValue</span>().<span class="hljs-built_in">Set</span>(ob…
10525-function"><span class="hljs-type">void</span> <span class="hljs-title">Init</span><span class="hl…
10526 …<span class="hljs-built_in">NODE_SET_METHOD</span>(<span class="hljs-keyword">module</span>, <span…
10529 <span class="hljs-built_in">NODE_MODULE</span>(NODE_GYP_MODULE_NAME, Init)
10531 } <span class="hljs-comment">// namespace demo</span></code></pre>
10533 <pre><code class="language-js"><span class="hljs-comment">// test.js</span>
10534 <span class="hljs-keyword">const</span> addon = <span class="hljs-built_in">require</span>(<span cl…
10536 <span class="hljs-keyword">const</span> obj1 = <span class="hljs-title function_">addon</span>(<spa…
10537 <span class="hljs-keyword">const</span> obj2 = <span class="hljs-title function_">addon</span>(<spa…
10538 …s-variable language_">console</span>.<span class="hljs-title function_">log</span>(obj1.<span clas…
10539 <span class="hljs-comment">// Prints: 'hello world'</span></code></pre>
10543 <pre><code class="language-cpp"><span class="hljs-comment">// addon.cc</span>
10544 <span class="hljs-meta">#<span class="hljs-keyword">include</span> <span class="hljs-string">&#x3C;…
10546 <span class="hljs-keyword">namespace</span> demo {
10548 <span class="hljs-keyword">using</span> v8::Context;
10549 <span class="hljs-keyword">using</span> v8::Function;
10550 <span class="hljs-keyword">using</span> v8::FunctionCallbackInfo;
10551 <span class="hljs-keyword">using</span> v8::FunctionTemplate;
10552 <span class="hljs-keyword">using</span> v8::Isolate;
10553 <span class="hljs-keyword">using</span> v8::Local;
10554 <span class="hljs-keyword">using</span> v8::Object;
10555 <span class="hljs-keyword">using</span> v8::String;
10556 <span class="hljs-keyword">using</span> v8::Value;
10558 …ass="hljs-function"><span class="hljs-type">void</span> <span class="hljs-title">MyFunction</span>…
10559 Isolate* isolate = args.<span class="hljs-built_in">GetIsolate</span>();
10560 …args.<span class="hljs-built_in">GetReturnValue</span>().<span class="hljs-built_in">Set</span>(St…
10561 …isolate, <span class="hljs-string">"hello world"</span>).<span class="hljs-built_in">ToLocalChecke…
10564 …s="hljs-function"><span class="hljs-type">void</span> <span class="hljs-title">CreateFunction</spa…
10565 Isolate* isolate = args.<span class="hljs-built_in">GetIsolate</span>();
10567 Local&#x3C;Context> context = isolate-><span class="hljs-built_in">GetCurrentContext</span>();
10568 …Local&#x3C;FunctionTemplate> tpl = FunctionTemplate::<span class="hljs-built_in">New</span>(isolat…
10569 …Local&#x3C;Function> fn = tpl-><span class="hljs-built_in">GetFunction</span>(context).<span class…
10571 <span class="hljs-comment">// omit this to make it anonymous</span>
10572 …fn-><span class="hljs-built_in">SetName</span>(String::<span class="hljs-built_in">NewFromUtf8</sp…
10573 …isolate, <span class="hljs-string">"theFunction"</span>).<span class="hljs-built_in">ToLocalChecke…
10575 …args.<span class="hljs-built_in">GetReturnValue</span>().<span class="hljs-built_in">Set</span>(fn…
10578-function"><span class="hljs-type">void</span> <span class="hljs-title">Init</span><span class="hl…
10579 …<span class="hljs-built_in">NODE_SET_METHOD</span>(<span class="hljs-keyword">module</span>, <span…
10582 <span class="hljs-built_in">NODE_MODULE</span>(NODE_GYP_MODULE_NAME, Init)
10584 } <span class="hljs-comment">// namespace demo</span></code></pre>
10586 <pre><code class="language-js"><span class="hljs-comment">// test.js</span>
10587 <span class="hljs-keyword">const</span> addon = <span class="hljs-built_in">require</span>(<span cl…
10589 <span class="hljs-keyword">const</span> fn = <span class="hljs-title function_">addon</span>();
10590 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(…
10591 <span class="hljs-comment">// Prints: 'hello world'</span></code></pre>
10595 <pre><code class="language-cpp"><span class="hljs-comment">// addon.cc</span>
10596 <span class="hljs-meta">#<span class="hljs-keyword">include</span> <span class="hljs-string">&#x3C;…
10597 <span class="hljs-meta">#<span class="hljs-keyword">include</span> <span class="hljs-string">"myobj…
10599 <span class="hljs-keyword">namespace</span> demo {
10601 <span class="hljs-keyword">using</span> v8::Local;
10602 <span class="hljs-keyword">using</span> v8::Object;
10604 <span class="hljs-function"><span class="hljs-type">void</span> <span class="hljs-title">InitAll</s…
10605 MyObject::<span class="hljs-built_in">Init</span>(exports);
10608 <span class="hljs-built_in">NODE_MODULE</span>(NODE_GYP_MODULE_NAME, InitAll)
10610 } <span class="hljs-comment">// namespace demo</span></code></pre>
10612 <pre><code class="language-cpp"><span class="hljs-comment">// myobject.h</span>
10613 <span class="hljs-meta">#<span class="hljs-keyword">ifndef</span> MYOBJECT_H</span>
10614 <span class="hljs-meta">#<span class="hljs-keyword">define</span> MYOBJECT_H</span>
10616 <span class="hljs-meta">#<span class="hljs-keyword">include</span> <span class="hljs-string">&#x3C;…
10617 <span class="hljs-meta">#<span class="hljs-keyword">include</span> <span class="hljs-string">&#x3C;…
10619 <span class="hljs-keyword">namespace</span> demo {
10621 <span class="hljs-keyword">class</span> <span class="hljs-title class_">MyObject</span> : <span cla…
10622 <span class="hljs-keyword">public</span>:
10623 …"hljs-function"><span class="hljs-keyword">static</span> <span class="hljs-type">void</span> <span…
10625 <span class="hljs-keyword">private</span>:
10626-function"><span class="hljs-keyword">explicit</span> <span class="hljs-title">MyObject</span><spa…
10627 ~<span class="hljs-built_in">MyObject</span>();
10629-function"><span class="hljs-keyword">static</span> <span class="hljs-type">void</span> <span clas…
10630-function"><span class="hljs-keyword">static</span> <span class="hljs-type">void</span> <span clas…
10632 <span class="hljs-type">double</span> value_;
10635 } <span class="hljs-comment">// namespace demo</span>
10637 <span class="hljs-meta">#<span class="hljs-keyword">endif</span></span></code></pre>
10641 <pre><code class="language-cpp"><span class="hljs-comment">// myobject.cc</span>
10642 <span class="hljs-meta">#<span class="hljs-keyword">include</span> <span class="hljs-string">"myobj…
10644 <span class="hljs-keyword">namespace</span> demo {
10646 <span class="hljs-keyword">using</span> v8::Context;
10647 <span class="hljs-keyword">using</span> v8::Function;
10648 <span class="hljs-keyword">using</span> v8::FunctionCallbackInfo;
10649 <span class="hljs-keyword">using</span> v8::FunctionTemplate;
10650 <span class="hljs-keyword">using</span> v8::Isolate;
10651 <span class="hljs-keyword">using</span> v8::Local;
10652 <span class="hljs-keyword">using</span> v8::Number;
10653 <span class="hljs-keyword">using</span> v8::Object;
10654 <span class="hljs-keyword">using</span> v8::ObjectTemplate;
10655 <span class="hljs-keyword">using</span> v8::String;
10656 <span class="hljs-keyword">using</span> v8::Value;
10658 MyObject::<span class="hljs-built_in">MyObject</span>(<span class="hljs-type">double</span> value) …
10661 MyObject::~<span class="hljs-built_in">MyObject</span>() {
10664 <span class="hljs-function"><span class="hljs-type">void</span> <span class="hljs-title">MyObject::…
10665 Isolate* isolate = exports-><span class="hljs-built_in">GetIsolate</span>();
10666 Local&#x3C;Context> context = isolate-><span class="hljs-built_in">GetCurrentContext</span>();
10668 …Local&#x3C;ObjectTemplate> addon_data_tpl = ObjectTemplate::<span class="hljs-built_in">New</span>…
10669 …addon_data_tpl-><span class="hljs-built_in">SetInternalFieldCount</span>(<span class="hljs-number"…
10671 …addon_data_tpl-><span class="hljs-built_in">NewInstance</span>(context).<span class="hljs-built_in…
10673 <span class="hljs-comment">// Prepare constructor template</span>
10674 …Local&#x3C;FunctionTemplate> tpl = FunctionTemplate::<span class="hljs-built_in">New</span>(isolat…
10675-><span class="hljs-built_in">SetClassName</span>(String::<span class="hljs-built_in">NewFromUtf8<…
10676 …tpl-><span class="hljs-built_in">InstanceTemplate</span>()-><span class="hljs-built_in">SetInterna…
10678 <span class="hljs-comment">// Prototype</span>
10679 …<span class="hljs-built_in">NODE_SET_PROTOTYPE_METHOD</span>(tpl, <span class="hljs-string">"plusO…
10681 …Local&#x3C;Function> constructor = tpl-><span class="hljs-built_in">GetFunction</span>(context).<s…
10682 …addon_data-><span class="hljs-built_in">SetInternalField</span>(<span class="hljs-number">0</span>…
10683 …exports-><span class="hljs-built_in">Set</span>(context, String::<span class="hljs-built_in">NewFr…
10684 …isolate, <span class="hljs-string">"MyObject"</span>).<span class="hljs-built_in">ToLocalChecked</…
10685 constructor).<span class="hljs-built_in">FromJust</span>();
10688 …ss="hljs-function"><span class="hljs-type">void</span> <span class="hljs-title">MyObject::New</spa…
10689 Isolate* isolate = args.<span class="hljs-built_in">GetIsolate</span>();
10690 Local&#x3C;Context> context = isolate-><span class="hljs-built_in">GetCurrentContext</span>();
10692 <span class="hljs-keyword">if</span> (args.<span class="hljs-built_in">IsConstructCall</span>()) {
10693 <span class="hljs-comment">// Invoked as constructor: `new MyObject(...)`</span>
10694 …<span class="hljs-type">double</span> value = args[<span class="hljs-number">0</span>]-><span clas…
10695-number">0</span> : args[<span class="hljs-number">0</span>]-><span class="hljs-built_in">NumberVa…
10696 …MyObject* obj = <span class="hljs-keyword">new</span> <span class="hljs-built_in">MyObject</span>(…
10697 obj-><span class="hljs-built_in">Wrap</span>(args.<span class="hljs-built_in">This</span>());
10698 …args.<span class="hljs-built_in">GetReturnValue</span>().<span class="hljs-built_in">Set</span>(ar…
10699 } <span class="hljs-keyword">else</span> {
10700 …<span class="hljs-comment">// Invoked as plain function `MyObject(...)`, turn into construct call.…
10701 …<span class="hljs-keyword">const</span> <span class="hljs-type">int</span> argc = <span class="hlj…
10702 Local&#x3C;Value> argv[argc] = { args[<span class="hljs-number">0</span>] };
10704-built_in">Data</span>().<span class="hljs-built_in">As</span>&#x3C;Object>()-><span class="hljs-b…
10706 …cons-><span class="hljs-built_in">NewInstance</span>(context, argc, argv).<span class="hljs-built_…
10707 …args.<span class="hljs-built_in">GetReturnValue</span>().<span class="hljs-built_in">Set</span>(re…
10711 …="hljs-function"><span class="hljs-type">void</span> <span class="hljs-title">MyObject::PlusOne</s…
10712 Isolate* isolate = args.<span class="hljs-built_in">GetIsolate</span>();
10714 …MyObject* obj = ObjectWrap::<span class="hljs-built_in">Unwrap</span>&#x3C;MyObject>(args.<span cl…
10715 obj->value_ += <span class="hljs-number">1</span>;
10717 …ass="hljs-built_in">GetReturnValue</span>().<span class="hljs-built_in">Set</span>(Number::<span c…
10720 } <span class="hljs-comment">// namespace demo</span></code></pre>
10723 <pre><code class="language-json"><span class="hljs-punctuation">{</span>
10724 …<span class="hljs-attr">"targets"</span><span class="hljs-punctuation">:</span> <span class="hljs-
10725 <span class="hljs-punctuation">{</span>
10726 …an class="hljs-attr">"target_name"</span><span class="hljs-punctuation">:</span> <span class="hljs
10727 …<span class="hljs-attr">"sources"</span><span class="hljs-punctuation">:</span> <span class="hljs-
10728 <span class="hljs-string">"addon.cc"</span><span class="hljs-punctuation">,</span>
10729 <span class="hljs-string">"myobject.cc"</span>
10730 <span class="hljs-punctuation">]</span>
10731 <span class="hljs-punctuation">}</span>
10732 <span class="hljs-punctuation">]</span>
10733 <span class="hljs-punctuation">}</span></code></pre>
10735 <pre><code class="language-js"><span class="hljs-comment">// test.js</span>
10736 <span class="hljs-keyword">const</span> addon = <span class="hljs-built_in">require</span>(<span cl…
10738 …s="hljs-keyword">const</span> obj = <span class="hljs-keyword">new</span> addon.<span class="hljs-
10739 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(…
10740 <span class="hljs-comment">// Prints: 11</span>
10741 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(…
10742 <span class="hljs-comment">// Prints: 12</span>
10743 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(…
10744 <span class="hljs-comment">// Prints: 13</span></code></pre>
10746 garbage-collected. For destructor testing, there are command-line flags that
10754 <pre><code class="language-js"><span class="hljs-keyword">const</span> obj = addon.<span class="hlj…
10755 <span class="hljs-comment">// instead of:</span>
10756 <span class="hljs-comment">// const obj = new addon.Object();</span></code></pre>
10758 <pre><code class="language-cpp"><span class="hljs-comment">// addon.cc</span>
10759 <span class="hljs-meta">#<span class="hljs-keyword">include</span> <span class="hljs-string">&#x3C;…
10760 <span class="hljs-meta">#<span class="hljs-keyword">include</span> <span class="hljs-string">"myobj…
10762 <span class="hljs-keyword">namespace</span> demo {
10764 <span class="hljs-keyword">using</span> v8::FunctionCallbackInfo;
10765 <span class="hljs-keyword">using</span> v8::Isolate;
10766 <span class="hljs-keyword">using</span> v8::Local;
10767 <span class="hljs-keyword">using</span> v8::Object;
10768 <span class="hljs-keyword">using</span> v8::String;
10769 <span class="hljs-keyword">using</span> v8::Value;
10771 …ss="hljs-function"><span class="hljs-type">void</span> <span class="hljs-title">CreateObject</span…
10772 MyObject::<span class="hljs-built_in">NewInstance</span>(args);
10775-function"><span class="hljs-type">void</span> <span class="hljs-title">InitAll</span><span class=…
10776 …MyObject::<span class="hljs-built_in">Init</span>(exports-><span class="hljs-built_in">GetIsolate<…
10778 …<span class="hljs-built_in">NODE_SET_METHOD</span>(<span class="hljs-keyword">module</span>, <span…
10781 <span class="hljs-built_in">NODE_MODULE</span>(NODE_GYP_MODULE_NAME, InitAll)
10783 } <span class="hljs-comment">// namespace demo</span></code></pre>
10787 <pre><code class="language-cpp"><span class="hljs-comment">// myobject.h</span>
10788 <span class="hljs-meta">#<span class="hljs-keyword">ifndef</span> MYOBJECT_H</span>
10789 <span class="hljs-meta">#<span class="hljs-keyword">define</span> MYOBJECT_H</span>
10791 <span class="hljs-meta">#<span class="hljs-keyword">include</span> <span class="hljs-string">&#x3C;…
10792 <span class="hljs-meta">#<span class="hljs-keyword">include</span> <span class="hljs-string">&#x3C;…
10794 <span class="hljs-keyword">namespace</span> demo {
10796 <span class="hljs-keyword">class</span> <span class="hljs-title class_">MyObject</span> : <span cla…
10797 <span class="hljs-keyword">public</span>:
10798 …"hljs-function"><span class="hljs-keyword">static</span> <span class="hljs-type">void</span> <span…
10799-function"><span class="hljs-keyword">static</span> <span class="hljs-type">void</span> <span clas…
10801 <span class="hljs-keyword">private</span>:
10802-function"><span class="hljs-keyword">explicit</span> <span class="hljs-title">MyObject</span><spa…
10803 ~<span class="hljs-built_in">MyObject</span>();
10805-function"><span class="hljs-keyword">static</span> <span class="hljs-type">void</span> <span clas…
10806-function"><span class="hljs-keyword">static</span> <span class="hljs-type">void</span> <span clas…
10807 <span class="hljs-keyword">static</span> v8::Global&#x3C;v8::Function> constructor;
10808 <span class="hljs-type">double</span> value_;
10811 } <span class="hljs-comment">// namespace demo</span>
10813 <span class="hljs-meta">#<span class="hljs-keyword">endif</span></span></code></pre>
10815 <pre><code class="language-cpp"><span class="hljs-comment">// myobject.cc</span>
10816 <span class="hljs-meta">#<span class="hljs-keyword">include</span> <span class="hljs-string">&#x3C;…
10817 <span class="hljs-meta">#<span class="hljs-keyword">include</span> <span class="hljs-string">"myobj…
10819 <span class="hljs-keyword">namespace</span> demo {
10821 <span class="hljs-keyword">using</span> node::AddEnvironmentCleanupHook;
10822 <span class="hljs-keyword">using</span> v8::Context;
10823 <span class="hljs-keyword">using</span> v8::Function;
10824 <span class="hljs-keyword">using</span> v8::FunctionCallbackInfo;
10825 <span class="hljs-keyword">using</span> v8::FunctionTemplate;
10826 <span class="hljs-keyword">using</span> v8::Global;
10827 <span class="hljs-keyword">using</span> v8::Isolate;
10828 <span class="hljs-keyword">using</span> v8::Local;
10829 <span class="hljs-keyword">using</span> v8::Number;
10830 <span class="hljs-keyword">using</span> v8::Object;
10831 <span class="hljs-keyword">using</span> v8::String;
10832 <span class="hljs-keyword">using</span> v8::Value;
10834 <span class="hljs-comment">// Warning! This is not thread-safe, this addon cannot be used for worke…
10835 <span class="hljs-comment">// threads.</span>
10838 MyObject::<span class="hljs-built_in">MyObject</span>(<span class="hljs-type">double</span> value) …
10841 MyObject::~<span class="hljs-built_in">MyObject</span>() {
10844 <span class="hljs-function"><span class="hljs-type">void</span> <span class="hljs-title">MyObject::…
10845 <span class="hljs-comment">// Prepare constructor template</span>
10846 …Local&#x3C;FunctionTemplate> tpl = FunctionTemplate::<span class="hljs-built_in">New</span>(isolat…
10847-><span class="hljs-built_in">SetClassName</span>(String::<span class="hljs-built_in">NewFromUtf8<…
10848 …tpl-><span class="hljs-built_in">InstanceTemplate</span>()-><span class="hljs-built_in">SetInterna…
10850 <span class="hljs-comment">// Prototype</span>
10851 …<span class="hljs-built_in">NODE_SET_PROTOTYPE_METHOD</span>(tpl, <span class="hljs-string">"plusO…
10853 Local&#x3C;Context> context = isolate-><span class="hljs-built_in">GetCurrentContext</span>();
10854 …or.<span class="hljs-built_in">Reset</span>(isolate, tpl-><span class="hljs-built_in">GetFunction<…
10856 …<span class="hljs-built_in">AddEnvironmentCleanupHook</span>(isolate, [](<span class="hljs-type">v…
10857 constructor.<span class="hljs-built_in">Reset</span>();
10858 }, <span class="hljs-literal">nullptr</span>);
10861 …ss="hljs-function"><span class="hljs-type">void</span> <span class="hljs-title">MyObject::New</spa…
10862 Isolate* isolate = args.<span class="hljs-built_in">GetIsolate</span>();
10863 Local&#x3C;Context> context = isolate-><span class="hljs-built_in">GetCurrentContext</span>();
10865 <span class="hljs-keyword">if</span> (args.<span class="hljs-built_in">IsConstructCall</span>()) {
10866 <span class="hljs-comment">// Invoked as constructor: `new MyObject(...)`</span>
10867 …<span class="hljs-type">double</span> value = args[<span class="hljs-number">0</span>]-><span clas…
10868-number">0</span> : args[<span class="hljs-number">0</span>]-><span class="hljs-built_in">NumberVa…
10869 …MyObject* obj = <span class="hljs-keyword">new</span> <span class="hljs-built_in">MyObject</span>(…
10870 obj-><span class="hljs-built_in">Wrap</span>(args.<span class="hljs-built_in">This</span>());
10871 …args.<span class="hljs-built_in">GetReturnValue</span>().<span class="hljs-built_in">Set</span>(ar…
10872 } <span class="hljs-keyword">else</span> {
10873 …<span class="hljs-comment">// Invoked as plain function `MyObject(...)`, turn into construct call.…
10874 …<span class="hljs-keyword">const</span> <span class="hljs-type">int</span> argc = <span class="hlj…
10875 Local&#x3C;Value> argv[argc] = { args[<span class="hljs-number">0</span>] };
10876 …Local&#x3C;Function> cons = Local&#x3C;Function>::<span class="hljs-built_in">New</span>(isolate, …
10878 …cons-><span class="hljs-built_in">NewInstance</span>(context, argc, argv).<span class="hljs-built_…
10879 …args.<span class="hljs-built_in">GetReturnValue</span>().<span class="hljs-built_in">Set</span>(in…
10883 …hljs-function"><span class="hljs-type">void</span> <span class="hljs-title">MyObject::NewInstance<…
10884 Isolate* isolate = args.<span class="hljs-built_in">GetIsolate</span>();
10886 …<span class="hljs-keyword">const</span> <span class="hljs-keyword">unsigned</span> argc = <span cl…
10887 Local&#x3C;Value> argv[argc] = { args[<span class="hljs-number">0</span>] };
10888 …Local&#x3C;Function> cons = Local&#x3C;Function>::<span class="hljs-built_in">New</span>(isolate, …
10889 Local&#x3C;Context> context = isolate-><span class="hljs-built_in">GetCurrentContext</span>();
10891 …cons-><span class="hljs-built_in">NewInstance</span>(context, argc, argv).<span class="hljs-built_…
10893 …args.<span class="hljs-built_in">GetReturnValue</span>().<span class="hljs-built_in">Set</span>(in…
10896 …="hljs-function"><span class="hljs-type">void</span> <span class="hljs-title">MyObject::PlusOne</s…
10897 Isolate* isolate = args.<span class="hljs-built_in">GetIsolate</span>();
10899 …MyObject* obj = ObjectWrap::<span class="hljs-built_in">Unwrap</span>&#x3C;MyObject>(args.<span cl…
10900 obj->value_ += <span class="hljs-number">1</span>;
10902 …ass="hljs-built_in">GetReturnValue</span>().<span class="hljs-built_in">Set</span>(Number::<span c…
10905 } <span class="hljs-comment">// namespace demo</span></code></pre>
10908 <pre><code class="language-json"><span class="hljs-punctuation">{</span>
10909 …<span class="hljs-attr">"targets"</span><span class="hljs-punctuation">:</span> <span class="hljs-
10910 <span class="hljs-punctuation">{</span>
10911 …an class="hljs-attr">"target_name"</span><span class="hljs-punctuation">:</span> <span class="hljs
10912 …<span class="hljs-attr">"sources"</span><span class="hljs-punctuation">:</span> <span class="hljs-
10913 <span class="hljs-string">"addon.cc"</span><span class="hljs-punctuation">,</span>
10914 <span class="hljs-string">"myobject.cc"</span>
10915 <span class="hljs-punctuation">]</span>
10916 <span class="hljs-punctuation">}</span>
10917 <span class="hljs-punctuation">]</span>
10918 <span class="hljs-punctuation">}</span></code></pre>
10920 <pre><code class="language-js"><span class="hljs-comment">// test.js</span>
10921 <span class="hljs-keyword">const</span> createObject = <span class="hljs-built_in">require</span>(<…
10923 <span class="hljs-keyword">const</span> obj = <span class="hljs-title function_">createObject</span…
10924 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(…
10925 <span class="hljs-comment">// Prints: 11</span>
10926 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(…
10927 <span class="hljs-comment">// Prints: 12</span>
10928 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(…
10929 <span class="hljs-comment">// Prints: 13</span>
10931 <span class="hljs-keyword">const</span> obj2 = <span class="hljs-title function_">createObject</spa…
10932 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(…
10933 <span class="hljs-comment">// Prints: 21</span>
10934 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(…
10935 <span class="hljs-comment">// Prints: 22</span>
10936 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(…
10937 <span class="hljs-comment">// Prints: 23</span></code></pre>
10943 <pre><code class="language-cpp"><span class="hljs-comment">// addon.cc</span>
10944 <span class="hljs-meta">#<span class="hljs-keyword">include</span> <span class="hljs-string">&#x3C;…
10945 <span class="hljs-meta">#<span class="hljs-keyword">include</span> <span class="hljs-string">&#x3C;…
10946 <span class="hljs-meta">#<span class="hljs-keyword">include</span> <span class="hljs-string">"myobj…
10948 <span class="hljs-keyword">namespace</span> demo {
10950 <span class="hljs-keyword">using</span> v8::Context;
10951 <span class="hljs-keyword">using</span> v8::FunctionCallbackInfo;
10952 <span class="hljs-keyword">using</span> v8::Isolate;
10953 <span class="hljs-keyword">using</span> v8::Local;
10954 <span class="hljs-keyword">using</span> v8::Number;
10955 <span class="hljs-keyword">using</span> v8::Object;
10956 <span class="hljs-keyword">using</span> v8::String;
10957 <span class="hljs-keyword">using</span> v8::Value;
10959 …ss="hljs-function"><span class="hljs-type">void</span> <span class="hljs-title">CreateObject</span…
10960 MyObject::<span class="hljs-built_in">NewInstance</span>(args);
10963 …n class="hljs-function"><span class="hljs-type">void</span> <span class="hljs-title">Add</span><sp…
10964 Isolate* isolate = args.<span class="hljs-built_in">GetIsolate</span>();
10965 Local&#x3C;Context> context = isolate-><span class="hljs-built_in">GetCurrentContext</span>();
10967 MyObject* obj1 = node::ObjectWrap::<span class="hljs-built_in">Unwrap</span>&#x3C;MyObject>(
10968 …args[<span class="hljs-number">0</span>]-><span class="hljs-built_in">ToObject</span>(context).<sp…
10969 MyObject* obj2 = node::ObjectWrap::<span class="hljs-built_in">Unwrap</span>&#x3C;MyObject>(
10970 …args[<span class="hljs-number">1</span>]-><span class="hljs-built_in">ToObject</span>(context).<sp…
10972 …<span class="hljs-type">double</span> sum = obj1-><span class="hljs-built_in">value</span>() + obj…
10973 …args.<span class="hljs-built_in">GetReturnValue</span>().<span class="hljs-built_in">Set</span>(Nu…
10976 <span class="hljs-function"><span class="hljs-type">void</span> <span class="hljs-title">InitAll</s…
10977 …MyObject::<span class="hljs-built_in">Init</span>(exports-><span class="hljs-built_in">GetIsolate<…
10979 …<span class="hljs-built_in">NODE_SET_METHOD</span>(exports, <span class="hljs-string">"createObjec…
10980 …<span class="hljs-built_in">NODE_SET_METHOD</span>(exports, <span class="hljs-string">"add"</span>…
10983 <span class="hljs-built_in">NODE_MODULE</span>(NODE_GYP_MODULE_NAME, InitAll)
10985 } <span class="hljs-comment">// namespace demo</span></code></pre>
10988 <pre><code class="language-cpp"><span class="hljs-comment">// myobject.h</span>
10989 <span class="hljs-meta">#<span class="hljs-keyword">ifndef</span> MYOBJECT_H</span>
10990 <span class="hljs-meta">#<span class="hljs-keyword">define</span> MYOBJECT_H</span>
10992 <span class="hljs-meta">#<span class="hljs-keyword">include</span> <span class="hljs-string">&#x3C;…
10993 <span class="hljs-meta">#<span class="hljs-keyword">include</span> <span class="hljs-string">&#x3C;…
10995 <span class="hljs-keyword">namespace</span> demo {
10997 <span class="hljs-keyword">class</span> <span class="hljs-title class_">MyObject</span> : <span cla…
10998 <span class="hljs-keyword">public</span>:
10999 …"hljs-function"><span class="hljs-keyword">static</span> <span class="hljs-type">void</span> <span…
11000-function"><span class="hljs-keyword">static</span> <span class="hljs-type">void</span> <span clas…
11001-function"><span class="hljs-keyword">inline</span> <span class="hljs-type">double</span> <span cl…
11003 <span class="hljs-keyword">private</span>:
11004-function"><span class="hljs-keyword">explicit</span> <span class="hljs-title">MyObject</span><spa…
11005 ~<span class="hljs-built_in">MyObject</span>();
11007-function"><span class="hljs-keyword">static</span> <span class="hljs-type">void</span> <span clas…
11008 <span class="hljs-keyword">static</span> v8::Global&#x3C;v8::Function> constructor;
11009 <span class="hljs-type">double</span> value_;
11012 } <span class="hljs-comment">// namespace demo</span>
11014 <span class="hljs-meta">#<span class="hljs-keyword">endif</span></span></code></pre>
11016 <pre><code class="language-cpp"><span class="hljs-comment">// myobject.cc</span>
11017 <span class="hljs-meta">#<span class="hljs-keyword">include</span> <span class="hljs-string">&#x3C;…
11018 <span class="hljs-meta">#<span class="hljs-keyword">include</span> <span class="hljs-string">"myobj…
11020 <span class="hljs-keyword">namespace</span> demo {
11022 <span class="hljs-keyword">using</span> node::AddEnvironmentCleanupHook;
11023 <span class="hljs-keyword">using</span> v8::Context;
11024 <span class="hljs-keyword">using</span> v8::Function;
11025 <span class="hljs-keyword">using</span> v8::FunctionCallbackInfo;
11026 <span class="hljs-keyword">using</span> v8::FunctionTemplate;
11027 <span class="hljs-keyword">using</span> v8::Global;
11028 <span class="hljs-keyword">using</span> v8::Isolate;
11029 <span class="hljs-keyword">using</span> v8::Local;
11030 <span class="hljs-keyword">using</span> v8::Object;
11031 <span class="hljs-keyword">using</span> v8::String;
11032 <span class="hljs-keyword">using</span> v8::Value;
11034 <span class="hljs-comment">// Warning! This is not thread-safe, this addon cannot be used for worke…
11035 <span class="hljs-comment">// threads.</span>
11038 MyObject::<span class="hljs-built_in">MyObject</span>(<span class="hljs-type">double</span> value) …
11041 MyObject::~<span class="hljs-built_in">MyObject</span>() {
11044 <span class="hljs-function"><span class="hljs-type">void</span> <span class="hljs-title">MyObject::…
11045 <span class="hljs-comment">// Prepare constructor template</span>
11046 …Local&#x3C;FunctionTemplate> tpl = FunctionTemplate::<span class="hljs-built_in">New</span>(isolat…
11047-><span class="hljs-built_in">SetClassName</span>(String::<span class="hljs-built_in">NewFromUtf8<…
11048 …tpl-><span class="hljs-built_in">InstanceTemplate</span>()-><span class="hljs-built_in">SetInterna…
11050 Local&#x3C;Context> context = isolate-><span class="hljs-built_in">GetCurrentContext</span>();
11051 …or.<span class="hljs-built_in">Reset</span>(isolate, tpl-><span class="hljs-built_in">GetFunction<…
11053 …<span class="hljs-built_in">AddEnvironmentCleanupHook</span>(isolate, [](<span class="hljs-type">v…
11054 constructor.<span class="hljs-built_in">Reset</span>();
11055 }, <span class="hljs-literal">nullptr</span>);
11058 …ss="hljs-function"><span class="hljs-type">void</span> <span class="hljs-title">MyObject::New</spa…
11059 Isolate* isolate = args.<span class="hljs-built_in">GetIsolate</span>();
11060 Local&#x3C;Context> context = isolate-><span class="hljs-built_in">GetCurrentContext</span>();
11062 <span class="hljs-keyword">if</span> (args.<span class="hljs-built_in">IsConstructCall</span>()) {
11063 <span class="hljs-comment">// Invoked as constructor: `new MyObject(...)`</span>
11064 …<span class="hljs-type">double</span> value = args[<span class="hljs-number">0</span>]-><span clas…
11065-number">0</span> : args[<span class="hljs-number">0</span>]-><span class="hljs-built_in">NumberVa…
11066 …MyObject* obj = <span class="hljs-keyword">new</span> <span class="hljs-built_in">MyObject</span>(…
11067 obj-><span class="hljs-built_in">Wrap</span>(args.<span class="hljs-built_in">This</span>());
11068 …args.<span class="hljs-built_in">GetReturnValue</span>().<span class="hljs-built_in">Set</span>(ar…
11069 } <span class="hljs-keyword">else</span> {
11070 …<span class="hljs-comment">// Invoked as plain function `MyObject(...)`, turn into construct call.…
11071 …<span class="hljs-keyword">const</span> <span class="hljs-type">int</span> argc = <span class="hlj…
11072 Local&#x3C;Value> argv[argc] = { args[<span class="hljs-number">0</span>] };
11073 …Local&#x3C;Function> cons = Local&#x3C;Function>::<span class="hljs-built_in">New</span>(isolate, …
11075 …cons-><span class="hljs-built_in">NewInstance</span>(context, argc, argv).<span class="hljs-built_…
11076 …args.<span class="hljs-built_in">GetReturnValue</span>().<span class="hljs-built_in">Set</span>(in…
11080 …hljs-function"><span class="hljs-type">void</span> <span class="hljs-title">MyObject::NewInstance<…
11081 Isolate* isolate = args.<span class="hljs-built_in">GetIsolate</span>();
11083 …<span class="hljs-keyword">const</span> <span class="hljs-keyword">unsigned</span> argc = <span cl…
11084 Local&#x3C;Value> argv[argc] = { args[<span class="hljs-number">0</span>] };
11085 …Local&#x3C;Function> cons = Local&#x3C;Function>::<span class="hljs-built_in">New</span>(isolate, …
11086 Local&#x3C;Context> context = isolate-><span class="hljs-built_in">GetCurrentContext</span>();
11088 …cons-><span class="hljs-built_in">NewInstance</span>(context, argc, argv).<span class="hljs-built_…
11090 …args.<span class="hljs-built_in">GetReturnValue</span>().<span class="hljs-built_in">Set</span>(in…
11093 } <span class="hljs-comment">// namespace demo</span></code></pre>
11095 <pre><code class="language-js"><span class="hljs-comment">// test.js</span>
11096 <span class="hljs-keyword">const</span> addon = <span class="hljs-built_in">require</span>(<span cl…
11098 <span class="hljs-keyword">const</span> obj1 = addon.<span class="hljs-title function_">createObjec…
11099 <span class="hljs-keyword">const</span> obj2 = addon.<span class="hljs-title function_">createObjec…
11100 <span class="hljs-keyword">const</span> result = addon.<span class="hljs-title function_">add</span…
11102 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(…
11103 <span class="hljs-comment">// Prints: 30</span></code></pre></section>
11105 <section><h2>Node-API<span><a class="mark" href="#n_api_node_api" id="n_api_node_api">#</a></span><…
11108 …ty api_stability_2"><a href="#documentation_stability_index">Stability: 2</a> - Stable</div><p></p>
11109 <p>Node-API (formerly N-API) is an API for building native Addons. It is
11115 …e <a href="https://nodejs.org/en/docs/guides/abi-stability/">ABI Stability</a> guide provides a mo…
11119 APIs, the functions available in Node-API are used.</p>
11120 <p>APIs exposed by Node-API are generally used to create and manipulate
11122 in the ECMA-262 Language Specification. The APIs have the following
11125 <li>All Node-API calls return a status code of type <code>napi_status</code>. This
11134 <p>Node-API is a C API that ensures ABI stability across Node.js versions
11137 C++ wrapper module called <a href="https://github.com/nodejs/node-addon-api"><code>node-addon-api</…
11139 with <code>node-addon-api</code> will depend on the symbols for the Node-API C-based
11140 functions exported by Node.js. <code>node-addon-api</code> is a more
11141 efficient way to write code that calls Node-API. Take, for example, the
11142 following <code>node-addon-api</code> code. The first section shows the
11143 <code>node-addon-api</code> code and the second section shows what actually gets
11145 <pre><code class="language-cpp">Object obj = Object::<span class="hljs-built_in">New</span>(env);
11146 obj[<span class="hljs-string">"foo"</span>] = String::<span class="hljs-built_in">New</span>(env, <…
11147 <pre><code class="language-cpp">napi_status status;
11149 status = <span class="hljs-built_in">napi_create_object</span>(env, &#x26;object);
11150 <span class="hljs-keyword">if</span> (status != napi_ok) {
11151 <span class="hljs-built_in">napi_throw_error</span>(env, ...);
11152 <span class="hljs-keyword">return</span>;
11155 status = <span class="hljs-built_in">napi_create_string_utf8</span>(env, <span class="hljs-string">…
11156 <span class="hljs-keyword">if</span> (status != napi_ok) {
11157 <span class="hljs-built_in">napi_throw_error</span>(env, ...);
11158 <span class="hljs-keyword">return</span>;
11161 status = <span class="hljs-built_in">napi_set_named_property</span>(env, object, <span class="hljs-
11162 <span class="hljs-keyword">if</span> (status != napi_ok) {
11163 <span class="hljs-built_in">napi_throw_error</span>(env, ...);
11164 <span class="hljs-keyword">return</span>;
11168 …en using <code>node-addon-api</code> instead of the C APIs, start with the API <a href="https://gi…
11169 for <code>node-addon-api</code>.</p>
11170 <p>The <a href="https://nodejs.github.io/node-addon-examples/">Node-API Resource</a> offers
11172 Node-API and <code>node-addon-api</code>.</p>
11174 <p>Although Node-API provides an ABI stability guarantee, other parts of Node.js do
11181 <pre><code class="language-cpp"><span class="hljs-meta">#<span class="hljs-keyword">include</span> …
11182 <span class="hljs-meta">#<span class="hljs-keyword">include</span> <span class="hljs-string">&#x3C;…
11183 <span class="hljs-meta">#<span class="hljs-keyword">include</span> <span class="hljs-string">&#x3C;…
11184 <span class="hljs-meta">#<span class="hljs-keyword">include</span> <span class="hljs-string">&#x3C;…
11188 <pre><code class="language-cpp"><span class="hljs-meta">#<span class="hljs-keyword">include</span> …
11192 <pre><code class="language-cpp"><span class="hljs-meta">#<span class="hljs-keyword">include</span> …
11195 <p>Thus, for an addon to remain ABI-compatible across Node.js major versions, it
11196 must use Node-API exclusively by restricting itself to using</p>
11197 <pre><code class="language-c"><span class="hljs-meta">#<span class="hljs-keyword">include</span> <s…
11199 library makes ABI stability guarantees similar to Node-API.</p>
11202 native addons using Node-API requires an additional set of tools. Besides the
11214 <pre><code class="language-bash">xcode-select --install</code></pre>
11218 <pre><code class="language-bash">npm install --global windows-build-tools</code></pre>
11225 <h5>node-gyp<span><a class="mark" href="#n_api_node_gyp" id="n_api_node_gyp">#</a></span></h5>
11226 …ps://github.com/nodejs/node-gyp">node-gyp</a> is a build system based on the <a href="https://gith…
11227 …<a href="https://gyp.gsrc.io">GYP</a> tool and comes bundled with npm. GYP, and therefore node-gyp,
11229 <p>Historically, node-gyp has been the tool of choice for building native
11231 developers have run into limitations in node-gyp.</p>
11233 <p><a href="https://github.com/cmake-js/cmake-js">CMake.js</a> is an alternative build system based…
11235 developers affected by limitations in node-gyp.</p>
11239 typically integrated with CI/CD build systems like <a href="https://travis-ci.org">Travis CI</a> and
11243 <h5>node-pre-gyp<span><a class="mark" href="#n_api_node_pre_gyp" id="n_api_node_pre_gyp">#</a></spa…
11244 <p><a href="https://github.com/mapbox/node-pre-gyp">node-pre-gyp</a> is a tool based on node-gyp th…
11245 upload binaries to a server of the developer's choice. node-pre-gyp has
11248 …/github.com/prebuild/prebuild">prebuild</a> is a tool that supports builds using either node-gyp or
11249 CMake.js. Unlike node-pre-gyp which supports a variety of servers, prebuild
11250 …s only to <a href="https://help.github.com/en/github/administering-a-repository/about-releases">Gi…
11253 <p><a href="https://github.com/prebuild/prebuildify">prebuildify</a> is a tool based on node-gyp. T…
11258 <p>In order to use the Node-API functions, include the file <a href="https://github.com/nodejs/node…
11260 <pre><code class="language-c"><span class="hljs-meta">#<span class="hljs-keyword">include</span> <s…
11262 In order to ensure compatibility with specific versions of Node-API, the version
11264 <pre><code class="language-c"><span class="hljs-meta">#<span class="hljs-keyword">define</span> NAP…
11265 <span class="hljs-meta">#<span class="hljs-keyword">include</span> <span class="hljs-string">&#x3C;…
11266 <p>This restricts the Node-API surface to just the functionality that was available
11268 <p>Some of the Node-API surface is experimental and requires explicit opt-in:</p>
11269 <pre><code class="language-c"><span class="hljs-meta">#<span class="hljs-keyword">define</span> NAP…
11270 <span class="hljs-meta">#<span class="hljs-keyword">include</span> <span class="hljs-string">&#x3C;…
11273 </section><section><h3>Node-API version matrix<span><a class="mark" href="#n_api_node_api_version_m…
11274 <p>Node-API versions are additive and versioned independently from Node.js.
11279 <!-- For accessibility purposes, this table needs row headers. That means we
11280 can't do it in markdown. Hence, the raw HTML. -->
11379 <p>* Node-API was experimental.</p>
11380 <p>** Node.js 8.0.0 included Node-API as experimental. It was released as
11381 Node-API version 1 but continued to evolve until Node.js 8.6.0. The API is
11382 different in versions prior to Node.js 8.6.0. We recommend Node-API version 3 or
11384 <p>Each API documented for Node-API will have a header named <code>added in:</code>, and APIs
11385 which are stable will have the additional header <code>Node-API version:</code>.
11387 the Node-API version shown in <code>Node-API version:</code> or higher.
11389 <code>Node-API version:</code> listed or if there is no <code>Node-API version:</code> listed,
11395 <p>The Node-APIs associated strictly with accessing ECMAScript features from native
11399 implementations of Node-API outside of Node.js. For those implementations the
11401 <p>The Node.js-specific parts of an addon can be separated from the code that
11403 latter may be used with multiple implementations of Node-API. In the example
11406 implementation of Node-API or any implementation of Node-API outside of Node.js.</p>
11410 <pre><code class="language-c"><span class="hljs-comment">// addon.h</span>
11411 <span class="hljs-meta">#<span class="hljs-keyword">ifndef</span> _ADDON_H_</span>
11412 <span class="hljs-meta">#<span class="hljs-keyword">define</span> _ADDON_H_</span>
11413 <span class="hljs-meta">#<span class="hljs-keyword">include</span> <span class="hljs-string">&#x3C;…
11414 napi_value <span class="hljs-title function_">create_addon</span><span class="hljs-params">(napi_en…
11415 <span class="hljs-meta">#<span class="hljs-keyword">endif</span> <span class="hljs-comment">// _AD…
11416 <pre><code class="language-c"><span class="hljs-comment">// addon.c</span>
11417 <span class="hljs-meta">#<span class="hljs-keyword">include</span> <span class="hljs-string">"addon…
11419 <span class="hljs-meta">#<span class="hljs-keyword">define</span> NAPI_CALL(env, call) …
11422 … <span class="hljs-keyword">if</span> (status != napi_ok) { \
11427 … <span class="hljs-keyword">if</span> (!is_pending) { \
11428 const char* message = (error_info->error_message == NULL) \
11429 ? <span class="hljs-string">"empty error message"</span> \
11430 : error_info->error_message; \
11437 <span class="hljs-keyword">static</span> napi_value
11438 <span class="hljs-title function_">DoSomethingUseful</span><span class="hljs-params">(napi_env env,…
11439 <span class="hljs-comment">// Do something useful.</span>
11440 <span class="hljs-keyword">return</span> <span class="hljs-literal">NULL</span>;
11443 napi_value <span class="hljs-title function_">create_addon</span><span class="hljs-params">(napi_en…
11449 <span class="hljs-string">"doSomethingUseful"</span>,
11452 <span class="hljs-literal">NULL</span>,
11457 <span class="hljs-string">"doSomethingUseful"</span>,
11460 <span class="hljs-keyword">return</span> result;
11462 <pre><code class="language-c"><span class="hljs-comment">// addon_node.c</span>
11463 <span class="hljs-meta">#<span class="hljs-keyword">include</span> <span class="hljs-string">&#x3C;…
11464 <span class="hljs-meta">#<span class="hljs-keyword">include</span> <span class="hljs-string">"addon…
11467 <span class="hljs-comment">// This function body is expected to return a `napi_value`.</span>
11468 …<span class="hljs-comment">// The variables `napi_env env` and `napi_value exports` may be used wi…
11469 …<span class="hljs-comment">// the body, as they are provided by the definition of `NAPI_MODULE_INI…
11470 <span class="hljs-keyword">return</span> create_addon(env);
11473 <p><a href="https://tc39.es/ecma262/#sec-agents">Section 8.7</a> of the <a href="https://tc39.githu…
11474 of an "Agent" as a self-contained environment in which JavaScript code runs.
11491 <p>To this end, Node-API provides a way to allocate data such that its life cycle
11496 <span>N-API version: 6</span>
11498 <pre><code class="language-c">napi_status <span class="hljs-title function_">napi_set_instance_data…
11499 <span class="hljs-type">void</span>* data,
11501 … <span class="hljs-type">void</span>* finalize_hint)</span>;</code></pre>
11503 <li><code>[in] env</code>: The environment that the Node-API call is invoked under.</li>
11520 <span>N-API version: 6</span>
11522 <pre><code class="language-c">napi_status <span class="hljs-title function_">napi_get_instance_data…
11523 <span class="hljs-type">void</span>** data)</span>;</code></pre>
11525 <li><code>[in] env</code>: The environment that the Node-API call is invoked under.</li>
11533 </section><section><h3>Basic Node-API data types<span><a class="mark" href="#n_api_basic_node_api_d…
11534 <p>Node-API exposes the following fundamental datatypes as abstractions that are
11536 introspectable only with other Node-API calls.</p>
11540 <span>N-API version: 1</span>
11542 <p>Integral status code indicating the success or failure of a Node-API call.
11544 <pre><code class="language-c"><span class="hljs-keyword">typedef</span> <span class="hljs-class"><s…
11566 napi_would_deadlock, <span class="hljs-comment">/* unused */</span>
11573 <span>N-API version: 1</span>
11575 <pre><code class="language-c"><span class="hljs-keyword">typedef</span> <span class="hljs-class"><s…
11576 <span class="hljs-keyword">const</span> <span class="hljs-type">char</span>* error_message;
11577 <span class="hljs-type">void</span>* engine_reserved;
11578 <span class="hljs-type">uint32_t</span> engine_error_code;
11582 <li><code>error_message</code>: UTF8-encoded string containing a VM-neutral description of
11584 <li><code>engine_reserved</code>: Reserved for VM-specific error details. This is currently
11586 <li><code>engine_error_code</code>: VM-specific error code. This is currently
11588 <li><code>error_code</code>: The Node-API status code that originated with the last error.</li>
11592 <p><code>napi_env</code> is used to represent a context that the underlying Node-API
11593 implementation can use to persist VM-specific state. This structure is passed
11595 making Node-API calls. Specifically, the same <code>napi_env</code> that was passed in when
11597 nested Node-API calls. Caching the <code>napi_env</code> for the purpose of general reuse,
11608 <span>N-API version: 4</span>
11616 <span>N-API version: 4</span>
11619 the thread-safe function is to be closed immediately (<code>napi_tsfn_abort</code>) or
11622 <pre><code class="language-c"><span class="hljs-keyword">typedef</span> <span class="hljs-class"><s…
11629 <span>N-API version: 4</span>
11632 the call should block whenever the queue associated with the thread-safe
11634 <pre><code class="language-c"><span class="hljs-keyword">typedef</span> <span class="hljs-class"><s…
11638 <h4>Node-API memory management types<span><a class="mark" href="#n_api_node_api_memory_management_t…
11641 created within a particular scope. In general, Node-API values are created
11644 create a new handle scope, Node-API values will be created in the default handle
11657 <span>N-API version: 1</span>
11664 <span>N-API version: 1</span>
11673 <span>N-API version: 8</span>
11675 <p>A 128-bit value stored as two unsigned 64-bit integers. It serves as a UUID
11679 manipulated. Type-tagging is most useful in conjunction with <a href="#n_api_napi_wrap"><code>napi_…
11683 <pre><code class="language-c"><span class="hljs-keyword">typedef</span> <span class="hljs-class"><s…
11684 <span class="hljs-type">uint64_t</span> lower;
11685 <span class="hljs-type">uint64_t</span> upper;
11694 <h4>Node-API callback types<span><a class="mark" href="#n_api_node_api_callback_types" id="n_api_no…
11698 <span>N-API version: 1</span>
11706 <span>N-API version: 1</span>
11708 <p>Function pointer type for user-provided native functions which are to be
11709 exposed to JavaScript via Node-API. Callback functions should satisfy the
11711-c"><span class="hljs-keyword">typedef</span> <span class="hljs-title function_">napi_value</span>…
11717 <span>N-API version: 1</span>
11719 <p>Function pointer type for add-on provided functions that allow the user to be
11720 notified when externally-owned data is ready to be cleaned up because the
11721 object with which it was associated with, has been garbage-collected. The user
11725-c"><span class="hljs-keyword">typedef</span> <span class="hljs-title function_">void</span> <span…
11726 <span class="hljs-type">void</span>* finalize_data,
11727 … <span class="hljs-type">void</span>* finalize_hint)</span>;</code></pre>
11733 <span>N-API version: 1</span>
11737-c"><span class="hljs-keyword">typedef</span> <span class="hljs-title function_">void</span> <span…
11738 <p>Implementations of this function must avoid making Node-API calls that execute
11739 JavaScript or interact with JavaScript objects. Node-API calls should be in the
11745 <span>N-API version: 1</span>
11749-c"><span class="hljs-keyword">typedef</span> <span class="hljs-title function_">void</span> <span…
11751 … <span class="hljs-type">void</span>* data)</span>;</code></pre>
11757 <span>N-API version: 4</span>
11759 <p>Function pointer used with asynchronous thread-safe function calls. The callback
11767 <p>Node-API sets up the environment prior to calling this callback, so it is
11771-c"><span class="hljs-keyword">typedef</span> <span class="hljs-title function_">void</span> <span…
11773 <span class="hljs-type">void</span>* context,
11774 … <span class="hljs-type">void</span>* data)</span>;</code></pre>
11776 …code>[in] env</code>: The environment to use for API calls, or <code>NULL</code> if the thread-safe
11779 thread-safe function is being torn down and <code>data</code> may need to be freed. It
11780 may also be <code>NULL</code> if the thread-safe function was created without
11782 <li><code>[in] context</code>: The optional data with which the thread-safe function was
11785 the callback to convert this native data to JavaScript values (with Node-API
11799-c"><span class="hljs-keyword">typedef</span> <span class="hljs-title function_">void</span> <span…
11800 … <span class="hljs-type">void</span>* data)</span>;</code></pre>
11811 <p>Node-API uses both return values and JavaScript exceptions for error handling.
11814 <p>All of the Node-API functions share the same error handling pattern. The
11827 <p>The <code>napi_status</code> return value provides a VM-independent representation of
11830 VM (engine)-specific information.</p>
11836 <span>N-API version: 1</span>
11838 …s="language-c"><span class="hljs-keyword">typedef</span> <span class="hljs-class"><span class="hlj…
11839 <span class="hljs-keyword">const</span> <span class="hljs-type">char</span>* error_message;
11840 <span class="hljs-type">void</span>* engine_reserved;
11841 <span class="hljs-type">uint32_t</span> engine_error_code;
11848 <li><code>error_code</code>: Node-API status code for the last error.</li>
11851 Node-API call that was made.</p>
11858 <span>N-API version: 1</span>
11860 <pre><code class="language-c">napi_status
11861 <span class="hljs-title function_">napi_get_last_error_info</span><span class="hljs-params">(napi_e…
11862 … <span class="hljs-keyword">const</span> napi_extended_error_info** result)</span>;</code></pre>
11872 a Node-API function is called on the same <code>env</code>.</p>
11878 <p>Any Node-API function call may result in a pending JavaScript exception. This is
11887 <p>In many cases when a Node-API function is called and an exception is
11890 for all functions. Node-API allows a subset of the functions to be
11899 code where the native method was invoked. The behavior of most Node-API calls
11911 it can be re-thrown it with <a href="#n_api_napi_throw"><code>napi_throw</code></a> where error is …
11928 SemVer applying. In order to support this model with Node-API, both
11935 <pre><code class="language-text">originalName [code]</code></pre>
11939 <pre><code class="language-text">TypeError [ERR_ERROR_1]</code></pre>
11943 <span>N-API version: 1</span>
11945 <pre><code class="language-c">NAPI_EXTERN napi_status <span class="hljs-title function_">napi_throw…
11955 <span>N-API version: 1</span>
11957 …re><code class="language-c">NAPI_EXTERN napi_status <span class="hljs-title function_">napi_throw_…
11958 … <span class="hljs-keyword">const</span> <span class="hljs-type">char</span>* code,
11959 …<span class="hljs-keyword">const</span> <span class="hljs-type">char</span>* msg)</span>;</code></…
11970 <span>N-API version: 1</span>
11972 …><code class="language-c">NAPI_EXTERN napi_status <span class="hljs-title function_">napi_throw_ty…
11973 … <span class="hljs-keyword">const</span> <span class="hljs-type">char</span>* code,
11974 …<span class="hljs-keyword">const</span> <span class="hljs-type">char</span>* msg)</span>;</code></…
11985 <span>N-API version: 1</span>
11987 …<code class="language-c">NAPI_EXTERN napi_status <span class="hljs-title function_">napi_throw_ran…
11988 … <span class="hljs-keyword">const</span> <span class="hljs-type">char</span>* code,
11989 …<span class="hljs-keyword">const</span> <span class="hljs-type">char</span>* msg)</span>;</code></…
12000 <span>N-API version: 1</span>
12002 <pre><code class="language-c">NAPI_EXTERN napi_status <span class="hljs-title function_">napi_is_er…
12004 … <span class="hljs-type">bool</span>* result)</span>;</code></pre>
12016 <span>N-API version: 1</span>
12018 …re><code class="language-c">NAPI_EXTERN napi_status <span class="hljs-title function_">napi_create…
12035 <span>N-API version: 1</span>
12037 …<code class="language-c">NAPI_EXTERN napi_status <span class="hljs-title function_">napi_create_ty…
12054 <span>N-API version: 1</span>
12056 …<code class="language-c">NAPI_EXTERN napi_status <span class="hljs-title function_">napi_create_ra…
12073 <span>N-API version: 1</span>
12075 …><code class="language-c">napi_status <span class="hljs-title function_">napi_get_and_clear_last_e…
12086 <span>N-API version: 1</span>
12088 …guage-c">napi_status <span class="hljs-title function_">napi_is_exception_pending</span><span clas…
12098 <span>N-API version: 3</span>
12100 <pre><code class="language-c">napi_status <span class="hljs-title function_">napi_fatal_exception</…
12113 <span>N-API version: 1</span>
12115-c">NAPI_NO_RETURN <span class="hljs-type">void</span> <span class="hljs-title function_">napi_fat…
12116 <span class="hljs-type">size_t</span> location_len,
12117 … <span class="hljs-keyword">const</span> <span class="hljs-type">char</span>* message,
12118 … <span class="hljs-type">size_t</span> message_len)</span>;</code></pre>
12122 <code>NAPI_AUTO_LENGTH</code> if it is null-terminated.</li>
12125 if it is null-terminated.</li>
12130 <p>As Node-API calls are made, handles to objects in the heap for the underlying
12142 The sections which follow describe the Node-API functions that can be used
12148 …uage-c"><span class="hljs-keyword">for</span> (<span class="hljs-type">int</span> i = <span class=…
12151 <span class="hljs-keyword">if</span> (status != napi_ok) {
12152 <span class="hljs-keyword">break</span>;
12154 <span class="hljs-comment">// do something with element</span>
12160 <p>To handle this case, Node-API provides the ability to establish a new 'scope' to
12165 <p>Node-API only supports a single nested hierarchy of scopes. There is only one
12173 …uage-c"><span class="hljs-keyword">for</span> (<span class="hljs-type">int</span> i = <span class=…
12176 <span class="hljs-keyword">if</span> (status != napi_ok) {
12177 <span class="hljs-keyword">break</span>;
12181 <span class="hljs-keyword">if</span> (status != napi_ok) {
12182 <span class="hljs-keyword">break</span>;
12184 <span class="hljs-comment">// do something with element</span>
12186 <span class="hljs-keyword">if</span> (status != napi_ok) {
12187 <span class="hljs-keyword">break</span>;
12191 inner scope needs to live beyond the lifespan of that scope. Node-API supports
12204 <span>N-API version: 1</span>
12206 …<code class="language-c">NAPI_EXTERN napi_status <span class="hljs-title function_">napi_open_hand…
12217 <span>N-API version: 1</span>
12219 …<code class="language-c">NAPI_EXTERN napi_status <span class="hljs-title function_">napi_close_han…
12232 <span>N-API version: 1</span>
12234 <pre><code class="language-c">NAPI_EXTERN napi_status
12235 …<span class="hljs-title function_">napi_open_escapable_handle_scope</span><span class="hljs-params…
12247 <span>N-API version: 1</span>
12249 <pre><code class="language-c">NAPI_EXTERN napi_status
12250 …<span class="hljs-title function_">napi_close_escapable_handle_scope</span><span class="hljs-param…
12263 <span>N-API version: 1</span>
12265 <pre><code class="language-c">napi_status <span class="hljs-title function_">napi_escape_handle</sp…
12292 <p>Node-API provides methods to create persistent references to an object.
12318 <span>N-API version: 1</span>
12320 …><code class="language-c">NAPI_EXTERN napi_status <span class="hljs-title function_">napi_create_r…
12322 … <span class="hljs-type">uint32_t</span> initial_refcount,
12337 <span>N-API version: 1</span>
12339 …><code class="language-c">NAPI_EXTERN napi_status <span class="hljs-title function_">napi_delete_r…
12350 <span>N-API version: 1</span>
12352 …e><code class="language-c">NAPI_EXTERN napi_status <span class="hljs-title function_">napi_referen…
12354 … <span class="hljs-type">uint32_t</span>* result)</span>;</code></pre>
12366 <span>N-API version: 1</span>
12368 …><code class="language-c">NAPI_EXTERN napi_status <span class="hljs-title function_">napi_referenc…
12370 … <span class="hljs-type">uint32_t</span>* result)</span>;</code></pre>
12382 <span>N-API version: 1</span>
12384 …code class="language-c">NAPI_EXTERN napi_status <span class="hljs-title function_">napi_get_refere…
12402 clean-up hooks that will be run once the current Node.js instance exits.</p>
12403 <p>Node-API provides functions for registering and un-registering such callbacks.
12409 <span>N-API version: 3</span>
12411 …code class="language-c">NODE_EXTERN napi_status <span class="hljs-title function_">napi_add_env_cl…
12412 … <span class="hljs-type">void</span> (*fun)(<span class="hljs-type">void</span>* arg),
12413 … <span class="hljs-type">void</span>* arg)</span>;</code></pre>
12429 <span>N-API version: 3</span>
12431 …de class="language-c">NAPI_EXTERN napi_status <span class="hljs-title function_">napi_remove_env_c…
12432 … <span class="hljs-type">void</span> (*fun)(<span class="hljs-type">void</span>* arg),
12433 … <span class="hljs-type">void</span>* arg)</span>;</code></pre>
12450 <span>N-API version: 8</span>
12452 …ode class="language-c">NAPI_EXTERN napi_status <span class="hljs-title function_">napi_add_async_c…
12455 <span class="hljs-type">void</span>* arg,
12486 …e class="language-c">NAPI_EXTERN napi_status <span class="hljs-title function_">napi_remove_async_…
12497 <p>Node-API modules are registered in a manner similar to other modules
12500 <pre><code class="language-c">NAPI_MODULE(NODE_GYP_MODULE_NAME, Init)</code></pre>
12501 <p>The next difference is the signature for the <code>Init</code> method. For a Node-API
12503 <pre><code class="language-c">napi_value <span class="hljs-title function_">Init</span><span class=…
12507 exported by the module. Node-API modules cannot modify the <code>module</code> object but
12511 <pre><code class="language-c">napi_value <span class="hljs-title function_">Init</span><span class=…
12514 <span class="hljs-string">"hello"</span>,
12515 <span class="hljs-literal">NULL</span>,
12517 <span class="hljs-literal">NULL</span>,
12518 <span class="hljs-literal">NULL</span>,
12519 <span class="hljs-literal">NULL</span>,
12521 <span class="hljs-literal">NULL</span>
12523 status = napi_define_properties(env, exports, <span class="hljs-number">1</span>, &#x26;desc);
12524 …<span class="hljs-keyword">if</span> (status != napi_ok) <span class="hljs-keyword">return</span> …
12525 <span class="hljs-keyword">return</span> exports;
12528 <pre><code class="language-c">napi_value <span class="hljs-title function_">Init</span><span class=…
12531 …pi_create_function(env, <span class="hljs-string">"exports"</span>, NAPI_AUTO_LENGTH, Method, <spa…
12532 …<span class="hljs-keyword">if</span> (status != napi_ok) <span class="hljs-keyword">return</span> …
12533 <span class="hljs-keyword">return</span> method;
12537 <pre><code class="language-c"><span class="hljs-comment">// <span class="hljs-doctag">NOTE:</span> …
12538 napi_value <span class="hljs-title function_">Init</span><span class="hljs-params">(napi_env env, n…
12541-string">"value"</span>, <span class="hljs-literal">NULL</span>, <span class="hljs-literal">NULL</…
12542 DECLARE_NAPI_METHOD(<span class="hljs-string">"plusOne"</span>, PlusOne),
12543 DECLARE_NAPI_METHOD(<span class="hljs-string">"multiply"</span>, Multiply),
12548 …ne_class(env, <span class="hljs-string">"MyObject"</span>, New, <span class="hljs-literal">NULL</s…
12549 …<span class="hljs-keyword">if</span> (status != napi_ok) <span class="hljs-keyword">return</span> …
12551 status = napi_create_reference(env, cons, <span class="hljs-number">1</span>, &#x26;constructor);
12552 …<span class="hljs-keyword">if</span> (status != napi_ok) <span class="hljs-keyword">return</span> …
12554 status = napi_set_named_property(env, exports, <span class="hljs-string">"MyObject"</span>, cons);
12555 …<span class="hljs-keyword">if</span> (status != napi_ok) <span class="hljs-keyword">return</span> …
12557 <span class="hljs-keyword">return</span> exports;
12561 <pre><code class="language-c">NAPI_MODULE_INIT() {
12565 status = napi_create_int64(env, <span class="hljs-number">42</span>, &#x26;answer);
12566 …<span class="hljs-keyword">if</span> (status != napi_ok) <span class="hljs-keyword">return</span> …
12568 status = napi_set_named_property(env, exports, <span class="hljs-string">"answer"</span>, answer);
12569 …<span class="hljs-keyword">if</span> (status != napi_ok) <span class="hljs-keyword">return</span> …
12571 <span class="hljs-keyword">return</span> exports;
12573 <p>All Node-API addons are context-aware, meaning they may be loaded multiple
12575 The documentation on <a href="#addons_context_aware_addons">context-aware addons</a> provides more …
12583 <p>Node-API exposes a set of APIs to create all types of JavaScript values.
12584 …e documented under <a href="https://tc39.github.io/ecma262/#sec-ecmascript-data-types-and-values">…
12589 <li>Convert from a primitive C type to a Node-API value</li>
12590 <li>Convert from Node-API value to a primitive C type</li>
12593 <p>Node-API values are represented by the type <code>napi_value</code>.
12594 Any Node-API call that requires a JavaScript value takes in a <code>napi_value</code>.
12595 In some cases, the API does check the type of the <code>napi_value</code> up-front.
12602 <span>N-API version: 6</span>
12604 <pre><code class="language-c"><span class="hljs-keyword">typedef</span> <span class="hljs-class"><s…
12616 <span>N-API version: 6</span>
12618 <pre><code class="language-c"><span class="hljs-keyword">typedef</span> <span class="hljs-class"><s…
12619 napi_key_all_properties = <span class="hljs-number">0</span>,
12620 napi_key_writable = <span class="hljs-number">1</span>,
12621 …napi_key_enumerable = <span class="hljs-number">1</span> &#x3C;&#x3C; <span class="hljs-number">1<…
12622 …napi_key_configurable = <span class="hljs-number">1</span> &#x3C;&#x3C; <span class="hljs-number">…
12623 …napi_key_skip_strings = <span class="hljs-number">1</span> &#x3C;&#x3C; <span class="hljs-number">…
12624 …napi_key_skip_symbols = <span class="hljs-number">1</span> &#x3C;&#x3C; <span class="hljs-number">…
12630 <span>N-API version: 6</span>
12632 <pre><code class="language-c"><span class="hljs-keyword">typedef</span> <span class="hljs-class"><s…
12640 <pre><code class="language-c"><span class="hljs-keyword">typedef</span> <span class="hljs-class"><s…
12641 <span class="hljs-comment">// ES6 types (corresponds to typeof)</span>
12654 described in <a href="https://tc39.github.io/ecma262/#sec-ecmascript-language-types">Section 6.1</a…
12660 <pre><code class="language-c"><span class="hljs-keyword">typedef</span> <span class="hljs-class"><s…
12675 <a href="https://tc39.github.io/ecma262/#sec-typedarray-objects">Section 22.2</a> of the <a href="h…
12680 <span>N-API version: 1</span>
12682 <pre><code class="language-c">napi_status <span class="hljs-title function_">napi_create_array</spa…
12684 <li><code>[in] env</code>: The environment that the Node-API call is invoked under.</li>
12688 <p>This API returns a Node-API value corresponding to a JavaScript <code>Array</code> type.
12690 <a href="https://tc39.github.io/ecma262/#sec-array-objects">Section 22.1</a> of the ECMAScript Lang…
12694 <span>N-API version: 1</span>
12696 …re><code class="language-c">napi_status <span class="hljs-title function_">napi_create_array_with_…
12697 <span class="hljs-type">size_t</span> length,
12705 <p>This API returns a Node-API value corresponding to a JavaScript <code>Array</code> type.
12706 The <code>Array</code>'s length property is set to the passed-in length parameter.
12707 However, the underlying buffer is not guaranteed to be pre-allocated by the VM
12713 <a href="https://tc39.github.io/ecma262/#sec-array-objects">Section 22.1</a> of the ECMAScript Lang…
12717 <span>N-API version: 1</span>
12719 <pre><code class="language-c">napi_status <span class="hljs-title function_">napi_create_arraybuffe…
12720 <span class="hljs-type">size_t</span> byte_length,
12721 <span class="hljs-type">void</span>** data,
12730 <p>This API returns a Node-API value corresponding to a JavaScript <code>ArrayBuffer</code>.
12731 <code>ArrayBuffer</code>s are used to represent fixed-length binary data buffers. They are
12732 normally used as a backing-buffer for <code>TypedArray</code> objects.
12740 <a href="https://tc39.github.io/ecma262/#sec-arraybuffer-objects">Section 24.1</a> of the ECMAScrip…
12744 <span>N-API version: 1</span>
12746 <pre><code class="language-c">napi_status <span class="hljs-title function_">napi_create_buffer</sp…
12747 <span class="hljs-type">size_t</span> size,
12748 <span class="hljs-type">void</span>** data,
12758 fully-supported data structure, in most cases using a <code>TypedArray</code> will suffice.</p>
12762 <span>N-API version: 1</span>
12764 <pre><code class="language-c">napi_status <span class="hljs-title function_">napi_create_buffer_cop…
12765 <span class="hljs-type">size_t</span> length,
12766 … <span class="hljs-keyword">const</span> <span class="hljs-type">void</span>* data,
12767 <span class="hljs-type">void</span>** result_data,
12779 from the passed-in buffer. While this is still a fully-supported data
12784 <span>N-API version: 5</span>
12786 <pre><code class="language-c">napi_status <span class="hljs-title function_">napi_create_date</span…
12787 <span class="hljs-type">double</span> time,
12799 <a href="https://tc39.github.io/ecma262/#sec-date-objects">Section 20.3</a> of the ECMAScript Langu…
12803 <span>N-API version: 1</span>
12805 <pre><code class="language-c">napi_status <span class="hljs-title function_">napi_create_external</…
12806 <span class="hljs-type">void</span>* data,
12808 <span class="hljs-type">void</span>* finalize_hint,
12837 <span>N-API version: 1</span>
12839 <pre><code class="language-c">napi_status
12840 <span class="hljs-title function_">napi_create_external_arraybuffer</span><span class="hljs-params"…
12841 <span class="hljs-type">void</span>* external_data,
12842 <span class="hljs-type">size_t</span> byte_length,
12844 <span class="hljs-type">void</span>* finalize_hint,
12858 <p>This API returns a Node-API value corresponding to a JavaScript <code>ArrayBuffer</code>.
12871 <a href="https://tc39.github.io/ecma262/#sec-arraybuffer-objects">Section 24.1</a> of the ECMAScrip…
12875 <span>N-API version: 1</span>
12877 …pre><code class="language-c">napi_status <span class="hljs-title function_">napi_create_external_b…
12878 <span class="hljs-type">size_t</span> length,
12879 <span class="hljs-type">void</span>* data,
12881 <span class="hljs-type">void</span>* finalize_hint,
12896 backed by the passed in buffer. While this is still a fully-supported data
12910 <span>N-API version: 1</span>
12912 <pre><code class="language-c">napi_status <span class="hljs-title function_">napi_create_object</sp…
12920 …/code> type is described in <a href="https://tc39.github.io/ecma262/#sec-object-type">Section 6.1.…
12925 <span>N-API version: 1</span>
12927 <pre><code class="language-c">napi_status <span class="hljs-title function_">napi_create_symbol</sp…
12937 <p>This API creates a JavaScript <code>symbol</code> value from a UTF8-encoded C string.</p>
12938 …/code> type is described in <a href="https://tc39.github.io/ecma262/#sec-symbol-objects">Section 1…
12943 <span>N-API version: 1</span>
12945 <pre><code class="language-c">napi_status <span class="hljs-title function_">napi_create_typedarray…
12947 <span class="hljs-type">size_t</span> length,
12949 <span class="hljs-type">size_t</span> byte_offset,
12962 <code>ArrayBuffer</code>. <code>TypedArray</code> objects provide an array-like view over an
12969 <a href="https://tc39.github.io/ecma262/#sec-typedarray-objects">Section 22.2</a> of the ECMAScript…
12973 <span>N-API version: 1</span>
12975 <pre><code class="language-c">napi_status <span class="hljs-title function_">napi_create_dataview</…
12976 <span class="hljs-type">size_t</span> byte_length,
12978 <span class="hljs-type">size_t</span> byte_offset,
12990 <code>DataView</code> objects provide an array-like view over an underlying data buffer,
12996 <a href="https://tc39.github.io/ecma262/#sec-dataview-objects">Section 24.3</a> of the ECMAScript L…
12997 <h4>Functions to convert from C types to Node-API<span><a class="mark" href="#n_api_functions_to_co…
13001 <span>N-API version: 1</span>
13003 …"language-c">napi_status <span class="hljs-title function_">napi_create_int32</span><span class="h…
13013 <a href="https://tc39.github.io/ecma262/#sec-ecmascript-language-types-number-type">Section 6.1.6</…
13017 <span>N-API version: 1</span>
13019 …"language-c">napi_status <span class="hljs-title function_">napi_create_uint32</span><span class="…
13029 <a href="https://tc39.github.io/ecma262/#sec-ecmascript-language-types-number-type">Section 6.1.6</…
13033 <span>N-API version: 1</span>
13035 …"language-c">napi_status <span class="hljs-title function_">napi_create_int64</span><span class="h…
13044 …s described in <a href="https://tc39.github.io/ecma262/#sec-ecmascript-language-types-number-type"…
13047 …//tc39.github.io/ecma262/#sec-number.min_safe_integer"><code>Number.MIN_SAFE_INTEGER</code></a> <c…
13048 …"https://tc39.github.io/ecma262/#sec-number.max_safe_integer"><code>Number.MAX_SAFE_INTEGER</code>…
13052 <span>N-API version: 1</span>
13054 …"language-c">napi_status <span class="hljs-title function_">napi_create_double</span><span class="…
13057 <li><code>[in] value</code>: Double-precision value to be represented in JavaScript.</li>
13064 <a href="https://tc39.github.io/ecma262/#sec-ecmascript-language-types-number-type">Section 6.1.6</…
13068 <span>N-API version: 6</span>
13070 <pre><code class="language-c">napi_status <span class="hljs-title function_">napi_create_bigint_int…
13071 <span class="hljs-type">int64_t</span> value,
13083 <span>N-API version: 6</span>
13085 <pre><code class="language-c">napi_status <span class="hljs-title function_">napi_create_bigint_uin…
13086 <span class="hljs-type">uint64_t</span> value,
13098 <span>N-API version: 6</span>
13100 <pre><code class="language-c">napi_status <span class="hljs-title function_">napi_create_bigint_wor…
13101 <span class="hljs-type">int</span> sign_bit,
13102 <span class="hljs-type">size_t</span> word_count,
13103 … <span class="hljs-keyword">const</span> <span class="hljs-type">uint64_t</span>* words,
13110 <li><code>[in] words</code>: An array of <code>uint64_t</code> little-endian 64-bit words.</li>
13114 <p>This API converts an array of unsigned 64-bit words into a single <code>BigInt</code>
13121 <span>N-API version: 1</span>
13123 <pre><code class="language-c">napi_status <span class="hljs-title function_">napi_create_string_lat…
13124 … <span class="hljs-keyword">const</span> <span class="hljs-type">char</span>* str,
13125 <span class="hljs-type">size_t</span> length,
13129 <li><code>[in] str</code>: Character buffer representing an ISO-8859-1-encoded string.</li>
13131 is null-terminated.</li>
13135 <p>This API creates a JavaScript <code>string</code> value from an ISO-8859-1-encoded C
13138 <a href="https://tc39.github.io/ecma262/#sec-ecmascript-language-types-string-type">Section 6.1.4</…
13142 <span>N-API version: 1</span>
13144 <pre><code class="language-c">napi_status <span class="hljs-title function_">napi_create_string_utf…
13145 … <span class="hljs-keyword">const</span> <span class="hljs-type">char16_t</span>* str,
13146 <span class="hljs-type">size_t</span> length,
13150 <li><code>[in] str</code>: Character buffer representing a UTF16-LE-encoded string.</li>
13151 <li><code>[in] length</code>: The length of the string in two-byte code units, or
13152 <code>NAPI_AUTO_LENGTH</code> if it is null-terminated.</li>
13156 <p>This API creates a JavaScript <code>string</code> value from a UTF16-LE-encoded C string.
13159 <a href="https://tc39.github.io/ecma262/#sec-ecmascript-language-types-string-type">Section 6.1.4</…
13163 <span>N-API version: 1</span>
13165 <pre><code class="language-c">napi_status <span class="hljs-title function_">napi_create_string_utf…
13166 … <span class="hljs-keyword">const</span> <span class="hljs-type">char</span>* str,
13167 <span class="hljs-type">size_t</span> length,
13171 <li><code>[in] str</code>: Character buffer representing a UTF8-encoded string.</li>
13173 is null-terminated.</li>
13177 <p>This API creates a JavaScript <code>string</code> value from a UTF8-encoded C string.
13180 <a href="https://tc39.github.io/ecma262/#sec-ecmascript-language-types-string-type">Section 6.1.4</…
13181 <h4>Functions to convert from Node-API to C types<span><a class="mark" href="#n_api_functions_to_co…
13185 <span>N-API version: 1</span>
13187 <pre><code class="language-c">napi_status <span class="hljs-title function_">napi_get_array_length<…
13189 … <span class="hljs-type">uint32_t</span>* result)</span></code></pre>
13198 … is described in <a href="https://tc39.github.io/ecma262/#sec-properties-of-array-instances-length…
13203 <span>N-API version: 1</span>
13205 <pre><code class="language-c">napi_status <span class="hljs-title function_">napi_get_arraybuffer_i…
13207 <span class="hljs-type">void</span>** data,
13208 … <span class="hljs-type">size_t</span>* byte_length)</span></code></pre>
13229 <span>N-API version: 1</span>
13231 <pre><code class="language-c">napi_status <span class="hljs-title function_">napi_get_buffer_info</…
13233 <span class="hljs-type">void</span>** data,
13234 <span class="hljs-type">size_t</span>* length)</span></code></pre>
13250 <span>N-API version: 1</span>
13252 <pre><code class="language-c">napi_status <span class="hljs-title function_">napi_get_prototype</sp…
13266 <span>N-API version: 1</span>
13268 <pre><code class="language-c">napi_status <span class="hljs-title function_">napi_get_typedarray_in…
13271 <span class="hljs-type">size_t</span>* length,
13272 <span class="hljs-type">void</span>** data,
13274 … <span class="hljs-type">size_t</span>* byte_offset)</span></code></pre>
13290 <code>data - byte_offset</code>.</li>
13299 <span>N-API version: 1</span>
13301 <pre><code class="language-c">napi_status <span class="hljs-title function_">napi_get_dataview_info…
13303 <span class="hljs-type">size_t</span>* byte_length,
13304 <span class="hljs-type">void</span>** data,
13306 … <span class="hljs-type">size_t</span>* byte_offset)</span></code></pre>
13323 <span>N-API version: 5</span>
13325 <pre><code class="language-c">napi_status <span class="hljs-title function_">napi_get_date_value</s…
13327 <span class="hljs-type">double</span>* result)</span></code></pre>
13336 <p>Returns <code>napi_ok</code> if the API succeeded. If a non-date <code>napi_value</code> is pass…
13343 <span>N-API version: 1</span>
13345-c">napi_status <span class="hljs-title function_">napi_get_value_bool</span><span class="hljs-par…
13352 <p>Returns <code>napi_ok</code> if the API succeeded. If a non-boolean <code>napi_value</code> is
13359 <span>N-API version: 1</span>
13361 <pre><code class="language-c">napi_status <span class="hljs-title function_">napi_get_value_double<…
13363 <span class="hljs-type">double</span>* result)</span></code></pre>
13370 <p>Returns <code>napi_ok</code> if the API succeeded. If a non-number <code>napi_value</code> is pa…
13377 <span>N-API version: 6</span>
13379 …pre><code class="language-c">napi_status <span class="hljs-title function_">napi_get_value_bigint_…
13381 <span class="hljs-type">int64_t</span>* result,
13382 … <span class="hljs-type">bool</span>* lossless)</span>;</code></pre>
13391 <p>Returns <code>napi_ok</code> if the API succeeded. If a non-<code>BigInt</code> is passed in it
13398 <span>N-API version: 6</span>
13400 …re><code class="language-c">napi_status <span class="hljs-title function_">napi_get_value_bigint_u…
13402 <span class="hljs-type">uint64_t</span>* result,
13403 … <span class="hljs-type">bool</span>* lossless)</span>;</code></pre>
13412 <p>Returns <code>napi_ok</code> if the API succeeded. If a non-<code>BigInt</code> is passed in it
13419 <span>N-API version: 6</span>
13421 …pre><code class="language-c">napi_status <span class="hljs-title function_">napi_get_value_bigint_…
13423 <span class="hljs-type">int</span>* sign_bit,
13424 <span class="hljs-type">size_t</span>* word_count,
13425 … <span class="hljs-type">uint64_t</span>* words)</span>;</code></pre>
13434 <li><code>[out] words</code>: Pointer to a pre-allocated 64-bit word array.</li>
13437 <p>This API converts a single <code>BigInt</code> value into a sign bit, 64-bit little-endian
13443 <span>N-API version: 1</span>
13445 <pre><code class="language-c">napi_status <span class="hljs-title function_">napi_get_value_externa…
13447 … <span class="hljs-type">void</span>** result)</span></code></pre>
13453 <p>Returns <code>napi_ok</code> if the API succeeded. If a non-external <code>napi_value</code> is
13460 <span>N-API version: 1</span>
13462 <pre><code class="language-c">napi_status <span class="hljs-title function_">napi_get_value_int32</…
13464 <span class="hljs-type">int32_t</span>* result)</span></code></pre>
13471 <p>Returns <code>napi_ok</code> if the API succeeded. If a non-number <code>napi_value</code>
13477 positive number becoming a negative number if the value is > 2<sup>31</sup> - 1.</p>
13478 <p>Non-finite number values (<code>NaN</code>, <code>+Infinity</code>, or <code>-Infinity</code>) s…
13483 <span>N-API version: 1</span>
13485 <pre><code class="language-c">napi_status <span class="hljs-title function_">napi_get_value_int64</…
13487 <span class="hljs-type">int64_t</span>* result)</span></code></pre>
13494 <p>Returns <code>napi_ok</code> if the API succeeded. If a non-number <code>napi_value</code>
13498 <p><code>number</code> values outside the range of <a href="https://tc39.github.io/ecma262/#sec-num…
13499 …de>-(2**53 - 1)</code> - <a href="https://tc39.github.io/ecma262/#sec-number.max_safe_integer"><co…
13501 <p>Non-finite number values (<code>NaN</code>, <code>+Infinity</code>, or <code>-Infinity</code>) s…
13506 <span>N-API version: 1</span>
13508 …re><code class="language-c">napi_status <span class="hljs-title function_">napi_get_value_string_l…
13510 <span class="hljs-type">char</span>* buf,
13511 <span class="hljs-type">size_t</span> bufsize,
13512 … <span class="hljs-type">size_t</span>* result)</span></code></pre>
13516 <li><code>[in] buf</code>: Buffer to write the ISO-8859-1-encoded string into. If <code>NULL</code>…
13520 insufficient, the returned string is truncated and null-terminated.</li>
13524 <p>Returns <code>napi_ok</code> if the API succeeded. If a non-<code>string</code> <code>napi_value…
13526 <p>This API returns the ISO-8859-1-encoded string corresponding the value passed
13531 <span>N-API version: 1</span>
13533 …pre><code class="language-c">napi_status <span class="hljs-title function_">napi_get_value_string_…
13535 <span class="hljs-type">char</span>* buf,
13536 <span class="hljs-type">size_t</span> bufsize,
13537 … <span class="hljs-type">size_t</span>* result)</span></code></pre>
13541 <li><code>[in] buf</code>: Buffer to write the UTF8-encoded string into. If <code>NULL</code> is pa…
13545 insufficient, the returned string is truncated and null-terminated.</li>
13549 <p>Returns <code>napi_ok</code> if the API succeeded. If a non-<code>string</code> <code>napi_value…
13551 <p>This API returns the UTF8-encoded string corresponding the value passed in.</p>
13555 <span>N-API version: 1</span>
13557 …pre><code class="language-c">napi_status <span class="hljs-title function_">napi_get_value_string_…
13559 <span class="hljs-type">char16_t</span>* buf,
13560 <span class="hljs-type">size_t</span> bufsize,
13561 … <span class="hljs-type">size_t</span>* result)</span></code></pre>
13565 <li><code>[in] buf</code>: Buffer to write the UTF16-LE-encoded string into. If <code>NULL</code> is
13566 passed in, the length of the string in 2-byte code units and excluding the
13569 insufficient, the returned string is truncated and null-terminated.</li>
13570 <li><code>[out] result</code>: Number of 2-byte code units copied into the buffer, excluding
13573 <p>Returns <code>napi_ok</code> if the API succeeded. If a non-<code>string</code> <code>napi_value…
13575 <p>This API returns the UTF16-encoded string corresponding the value passed in.</p>
13579 <span>N-API version: 1</span>
13581 <pre><code class="language-c">napi_status <span class="hljs-title function_">napi_get_value_uint32<…
13583 … <span class="hljs-type">uint32_t</span>* result)</span></code></pre>
13590 <p>Returns <code>napi_ok</code> if the API succeeded. If a non-number <code>napi_value</code>
13598 <span>N-API version: 1</span>
13600 …="language-c">napi_status <span class="hljs-title function_">napi_get_boolean</span><span class="h…
13613 <span>N-API version: 1</span>
13615 <pre><code class="language-c">napi_status <span class="hljs-title function_">napi_get_global</span>…
13625 <span>N-API version: 1</span>
13627 <pre><code class="language-c">napi_status <span class="hljs-title function_">napi_get_null</span><s…
13637 <span>N-API version: 1</span>
13639 <pre><code class="language-c">napi_status <span class="hljs-title function_">napi_get_undefined</sp…
13647 <p>Node-API exposes a set of APIs to perform some abstract operations on JavaScript
13648 …tions are documented under <a href="https://tc39.github.io/ecma262/#sec-abstract-operations">Secti…
13660 <span>N-API version: 1</span>
13662 <pre><code class="language-c">napi_status <span class="hljs-title function_">napi_coerce_to_bool</s…
13672 <a href="https://tc39.github.io/ecma262/#sec-toboolean">Section 7.1.2</a> of the ECMAScript Languag…
13673 This API can be re-entrant if getters are defined on the passed-in <code>Object</code>.</p>
13677 <span>N-API version: 1</span>
13679 <pre><code class="language-c">napi_status <span class="hljs-title function_">napi_coerce_to_number<…
13689 <a href="https://tc39.github.io/ecma262/#sec-tonumber">Section 7.1.3</a> of the ECMAScript Language…
13690 This API can be re-entrant if getters are defined on the passed-in <code>Object</code>.</p>
13694 <span>N-API version: 1</span>
13696 <pre><code class="language-c">napi_status <span class="hljs-title function_">napi_coerce_to_object<…
13706 <a href="https://tc39.github.io/ecma262/#sec-toobject">Section 7.1.13</a> of the ECMAScript Languag…
13707 This API can be re-entrant if getters are defined on the passed-in <code>Object</code>.</p>
13711 <span>N-API version: 1</span>
13713 <pre><code class="language-c">napi_status <span class="hljs-title function_">napi_coerce_to_string<…
13723 <a href="https://tc39.github.io/ecma262/#sec-toobject">Section 7.1.13</a> of the ECMAScript Languag…
13724 This API can be re-entrant if getters are defined on the passed-in <code>Object</code>.</p>
13728 <span>N-API version: 1</span>
13730 <pre><code class="language-c">napi_status <span class="hljs-title function_">napi_typeof</span><spa…
13742 the object as defined in <a href="https://tc39.github.io/ecma262/#sec-typeof-operator">Section 12.5…
13753 <span>N-API version: 1</span>
13755 <pre><code class="language-c">napi_status <span class="hljs-title function_">napi_instanceof</span>…
13758 <span class="hljs-type">bool</span>* result)</span></code></pre>
13769 defined in <a href="https://tc39.github.io/ecma262/#sec-instanceofoperator">Section 12.10.4</a> of …
13773 <span>N-API version: 1</span>
13775 …ge-c">napi_status <span class="hljs-title function_">napi_is_array</span><span class="hljs-params"…
13783 as defined in <a href="https://tc39.github.io/ecma262/#sec-isarray">Section 7.2.2</a> of the ECMASc…
13787 <span>N-API version: 1</span>
13789-c">napi_status <span class="hljs-title function_">napi_is_arraybuffer</span><span class="hljs-par…
13800 <span>N-API version: 1</span>
13802 …ge-c">napi_status <span class="hljs-title function_">napi_is_buffer</span><span class="hljs-params…
13814 <span>N-API version: 5</span>
13816 …age-c">napi_status <span class="hljs-title function_">napi_is_date</span><span class="hljs-params"…
13828 <span>N-API version: 1</span>
13830 …ge-c">napi_status <span class="hljs-title function_">napi_is_error</span><span class="hljs-params"…
13841 <span>N-API version: 1</span>
13843-c">napi_status <span class="hljs-title function_">napi_is_typedarray</span><span class="hljs-para…
13854 <span>N-API version: 1</span>
13856 …e-c">napi_status <span class="hljs-title function_">napi_is_dataview</span><span class="hljs-param…
13867 <span>N-API version: 1</span>
13869 <pre><code class="language-c">napi_status <span class="hljs-title function_">napi_strict_equals</sp…
13872 <span class="hljs-type">bool</span>* result)</span></code></pre>
13881 defined in <a href="https://tc39.github.io/ecma262/#sec-strict-equality-comparison">Section 7.2.14<…
13885 <span>N-API version: 7</span>
13887 <pre><code class="language-c">napi_status <span class="hljs-title function_">napi_detach_arraybuffe…
13893 <p>Returns <code>napi_ok</code> if the API succeeded. If a non-detachable <code>ArrayBuffer</code> …
13895 <p>Generally, an <code>ArrayBuffer</code> is non-detachable if it has been detached before.
13900 defined in <a href="https://tc39.es/ecma262/#sec-detacharraybuffer">Section 24.1.1.3</a> of the ECM…
13904 <span>N-API version: 7</span>
13906 …re><code class="language-c">napi_status <span class="hljs-title function_">napi_is_detached_arrayb…
13908 … <span class="hljs-type">bool</span>* result)</span></code></pre>
13917 operation as defined in <a href="https://tc39.es/ecma262/#sec-isdetachedbuffer">Section 24.1.1.2</a…
13920 <p>Node-API exposes a set of APIs to get and set properties on JavaScript
13921 …types are documented under <a href="https://tc39.github.io/ecma262/#sec-abstract-operations">Secti…
13924 Fundamentally, all property keys in Node-API can be represented in one of the
13927 <li>Named: a simple UTF8-encoded string</li>
13928 <li>Integer-Indexed: an index value represented by <code>uint32_t</code></li>
13929 <li>JavaScript value: these are represented in Node-API by <code>napi_value</code>. This can
13932 <p>Node-API values are represented by the type <code>napi_value</code>.
13933 Any Node-API call that requires a JavaScript value takes in a <code>napi_value</code>.
13940 <pre><code class="language-js"><span class="hljs-keyword">const</span> obj = {};
13941 obj.<span class="hljs-property">myProp</span> = <span class="hljs-number">123</span>;</code></pre>
13942 <p>The equivalent can be done using Node-API values with the following snippet:</p>
13943 <pre><code class="language-c">napi_status status = napi_generic_failure;
13945 <span class="hljs-comment">// const obj = {}</span>
13948 <span class="hljs-keyword">if</span> (status != napi_ok) <span class="hljs-keyword">return</span> s…
13950 <span class="hljs-comment">// Create a napi_value for 123</span>
13951 status = napi_create_int32(env, <span class="hljs-number">123</span>, &#x26;value);
13952 <span class="hljs-keyword">if</span> (status != napi_ok) <span class="hljs-keyword">return</span> s…
13954 <span class="hljs-comment">// obj.myProp = 123</span>
13955 status = napi_set_named_property(env, obj, <span class="hljs-string">"myProp"</span>, value);
13956 <span class="hljs-keyword">if</span> (status != napi_ok) <span class="hljs-keyword">return</span> s…
13959 <pre><code class="language-js"><span class="hljs-keyword">const</span> arr = [];
13960 arr[<span class="hljs-number">123</span>] = <span class="hljs-string">'hello'</span>;</code></pre>
13961 <p>The equivalent can be done using Node-API values with the following snippet:</p>
13962 <pre><code class="language-c">napi_status status = napi_generic_failure;
13964 <span class="hljs-comment">// const arr = [];</span>
13967 <span class="hljs-keyword">if</span> (status != napi_ok) <span class="hljs-keyword">return</span> s…
13969 <span class="hljs-comment">// Create a napi_value for 'hello'</span>
13970 status = napi_create_string_utf8(env, <span class="hljs-string">"hello"</span>, NAPI_AUTO_LENGTH, &…
13971 <span class="hljs-keyword">if</span> (status != napi_ok) <span class="hljs-keyword">return</span> s…
13973 <span class="hljs-comment">// arr[123] = 'hello';</span>
13974 status = napi_set_element(env, arr, <span class="hljs-number">123</span>, value);
13975 <span class="hljs-keyword">if</span> (status != napi_ok) <span class="hljs-keyword">return</span> s…
13978 <pre><code class="language-js"><span class="hljs-keyword">const</span> arr = [];
13979 <span class="hljs-keyword">const</span> value = arr[<span class="hljs-number">123</span>];</code></…
13980 <p>The following is the approximate equivalent of the Node-API counterpart:</p>
13981 <pre><code class="language-c">napi_status status = napi_generic_failure;
13983 <span class="hljs-comment">// const arr = []</span>
13986 <span class="hljs-keyword">if</span> (status != napi_ok) <span class="hljs-keyword">return</span> s…
13988 <span class="hljs-comment">// const value = arr[123]</span>
13989 status = napi_get_element(env, arr, <span class="hljs-number">123</span>, &#x26;value);
13990 <span class="hljs-keyword">if</span> (status != napi_ok) <span class="hljs-keyword">return</span> s…
13993 <pre><code class="language-js"><span class="hljs-keyword">const</span> obj = {};
13994 <span class="hljs-title class_">Object</span>.<span class="hljs-title function_">defineProperties</…
13995-string">'foo'</span>: { <span class="hljs-attr">value</span>: <span class="hljs-number">123</span…
13996-string">'bar'</span>: { <span class="hljs-attr">value</span>: <span class="hljs-number">456</span…
13998 <p>The following is the approximate equivalent of the Node-API counterpart:</p>
13999 <pre><code class="language-c">napi_status status = napi_status_generic_failure;
14001 <span class="hljs-comment">// const obj = {};</span>
14004 <span class="hljs-keyword">if</span> (status != napi_ok) <span class="hljs-keyword">return</span> s…
14006 <span class="hljs-comment">// Create napi_values for 123 and 456</span>
14008 status = napi_create_int32(env, <span class="hljs-number">123</span>, &#x26;fooValue);
14009 <span class="hljs-keyword">if</span> (status != napi_ok) <span class="hljs-keyword">return</span> s…
14010 status = napi_create_int32(env, <span class="hljs-number">456</span>, &#x26;barValue);
14011 <span class="hljs-keyword">if</span> (status != napi_ok) <span class="hljs-keyword">return</span> s…
14013 <span class="hljs-comment">// Set the properties</span>
14015-string">"foo"</span>, <span class="hljs-literal">NULL</span>, <span class="hljs-literal">NULL</sp…
14016-string">"bar"</span>, <span class="hljs-literal">NULL</span>, <span class="hljs-literal">NULL</sp…
14020 …<span class="hljs-keyword">sizeof</span>(descriptors) / <span class="hljs-keyword">sizeof</span>(d…
14022 <span class="hljs-keyword">if</span> (status != napi_ok) <span class="hljs-keyword">return</span> s…
14034 <pre><code class="language-c"><span class="hljs-keyword">typedef</span> <span class="hljs-class"><s…
14035 napi_default = <span class="hljs-number">0</span>,
14036 …napi_writable = <span class="hljs-number">1</span> &#x3C;&#x3C; <span class="hljs-number">0</span>,
14037 …napi_enumerable = <span class="hljs-number">1</span> &#x3C;&#x3C; <span class="hljs-number">1</spa…
14038 …napi_configurable = <span class="hljs-number">1</span> &#x3C;&#x3C; <span class="hljs-number">2</s…
14040 <span class="hljs-comment">// Used with napi_define_class to distinguish static properties</span>
14041 <span class="hljs-comment">// from instance properties. Ignored by napi_define_properties.</span>
14042 napi_static = <span class="hljs-number">1</span> &#x3C;&#x3C; <span class="hljs-number">10</span>,
14044 <span class="hljs-comment">// Default for class methods.</span>
14047 <span class="hljs-comment">// Default for object properties, like in JS obj[prop].</span>
14054 attributes listed in <a href="https://tc39.github.io/ecma262/#table-2">Section 6.1.7.1</a>
14063 <a href="https://tc39.github.io/ecma262/#table-2">Section 6.1.7.1</a> of the <a href="https://tc39.…
14073 <pre><code class="language-c"><span class="hljs-keyword">typedef</span> <span class="hljs-class"><s…
14074 <span class="hljs-comment">// One of utf8name or name should be NULL.</span>
14075 <span class="hljs-keyword">const</span> <span class="hljs-type">char</span>* utf8name;
14084 <span class="hljs-type">void</span>* data;
14100 performed using a Node-API call). <a href="#n_api_napi_callback"><code>napi_callback</code></a> pro…
14105 performed using a Node-API call). <a href="#n_api_napi_callback"><code>napi_callback</code></a> pro…
14119 <span>N-API version: 1</span>
14121 <pre><code class="language-c">napi_status <span class="hljs-title function_">napi_get_property_name…
14125 <li><code>[in] env</code>: The environment that the Node-API call is invoked under.</li>
14139 <span>N-API version: 6</span>
14141 <pre><code class="language-c">napi_get_all_property_names(napi_env env,
14148 <li><code>[in] env</code>: The environment that the Node-API call is invoked under.</li>
14164 <span>N-API version: 1</span>
14166 <pre><code class="language-c">napi_status <span class="hljs-title function_">napi_set_property</spa…
14171 <li><code>[in] env</code>: The environment that the Node-API call is invoked under.</li>
14181 <span>N-API version: 1</span>
14183 <pre><code class="language-c">napi_status <span class="hljs-title function_">napi_get_property</spa…
14188 <li><code>[in] env</code>: The environment that the Node-API call is invoked under.</li>
14198 <span>N-API version: 1</span>
14200 <pre><code class="language-c">napi_status <span class="hljs-title function_">napi_has_property</spa…
14203 <span class="hljs-type">bool</span>* result)</span>;</code></pre>
14205 <li><code>[in] env</code>: The environment that the Node-API call is invoked under.</li>
14215 <span>N-API version: 1</span>
14217 <pre><code class="language-c">napi_status <span class="hljs-title function_">napi_delete_property</…
14220 <span class="hljs-type">bool</span>* result)</span>;</code></pre>
14222 <li><code>[in] env</code>: The environment that the Node-API call is invoked under.</li>
14233 <span>N-API version: 1</span>
14235 <pre><code class="language-c">napi_status <span class="hljs-title function_">napi_has_own_property<…
14238 <span class="hljs-type">bool</span>* result)</span>;</code></pre>
14240 <li><code>[in] env</code>: The environment that the Node-API call is invoked under.</li>
14247 be a <code>string</code> or a <code>symbol</code>, or an error will be thrown. Node-API will not
14252 <span>N-API version: 1</span>
14254 <pre><code class="language-c">napi_status <span class="hljs-title function_">napi_set_named_propert…
14256 … <span class="hljs-keyword">const</span> <span class="hljs-type">char</span>* utf8Name,
14259 <li><code>[in] env</code>: The environment that the Node-API call is invoked under.</li>
14270 <span>N-API version: 1</span>
14272 <pre><code class="language-c">napi_status <span class="hljs-title function_">napi_get_named_propert…
14274 … <span class="hljs-keyword">const</span> <span class="hljs-type">char</span>* utf8Name,
14277 <li><code>[in] env</code>: The environment that the Node-API call is invoked under.</li>
14288 <span>N-API version: 1</span>
14290 <pre><code class="language-c">napi_status <span class="hljs-title function_">napi_has_named_propert…
14292 … <span class="hljs-keyword">const</span> <span class="hljs-type">char</span>* utf8Name,
14293 … <span class="hljs-type">bool</span>* result)</span>;</code></pre>
14295 <li><code>[in] env</code>: The environment that the Node-API call is invoked under.</li>
14306 <span>N-API version: 1</span>
14308 <pre><code class="language-c">napi_status <span class="hljs-title function_">napi_set_element</span…
14310 <span class="hljs-type">uint32_t</span> index,
14313 <li><code>[in] env</code>: The environment that the Node-API call is invoked under.</li>
14323 <span>N-API version: 1</span>
14325 <pre><code class="language-c">napi_status <span class="hljs-title function_">napi_get_element</span…
14327 <span class="hljs-type">uint32_t</span> index,
14330 <li><code>[in] env</code>: The environment that the Node-API call is invoked under.</li>
14340 <span>N-API version: 1</span>
14342 <pre><code class="language-c">napi_status <span class="hljs-title function_">napi_has_element</span…
14344 <span class="hljs-type">uint32_t</span> index,
14345 <span class="hljs-type">bool</span>* result)</span>;</code></pre>
14347 <li><code>[in] env</code>: The environment that the Node-API call is invoked under.</li>
14358 <span>N-API version: 1</span>
14360 <pre><code class="language-c">napi_status <span class="hljs-title function_">napi_delete_element</s…
14362 <span class="hljs-type">uint32_t</span> index,
14363 <span class="hljs-type">bool</span>* result)</span>;</code></pre>
14365 <li><code>[in] env</code>: The environment that the Node-API call is invoked under.</li>
14376 <span>N-API version: 1</span>
14378 <pre><code class="language-c">napi_status <span class="hljs-title function_">napi_define_properties…
14380 <span class="hljs-type">size_t</span> property_count,
14381 … <span class="hljs-keyword">const</span> napi_property_descriptor* properties)</span>;</code></pre>
14383 <li><code>[in] env</code>: The environment that the Node-API call is invoked under.</li>
14393 …github.io/ecma262/#sec-ordinary-object-internal-methods-and-internal-slots-defineownproperty-p-des…
14398 <span>N-API version: 8</span>
14400 <pre><code class="language-c">napi_status <span class="hljs-title function_">napi_object_freeze</sp…
14403 <li><code>[in] env</code>: The environment that the Node-API call is invoked under.</li>
14412 in <a href="https://tc39.es/ecma262/#sec-object.freeze">Section 19.1.2.6</a> of the
14413 ECMA-262 specification.</p>
14417 <span>N-API version: 8</span>
14419 <pre><code class="language-c">napi_status <span class="hljs-title function_">napi_object_seal</span…
14422 <li><code>[in] env</code>: The environment that the Node-API call is invoked under.</li>
14427 added to it, as well as marking all existing properties as non-configurable.
14428 This is described in <a href="https://tc39.es/ecma262/#sec-object.seal">Section 19.1.2.20</a>
14429 of the ECMA-262 specification.</p>
14431 <p>Node-API provides a set of APIs that allow JavaScript code to
14432 call back into native code. Node-APIs that support calling back
14443 <p>Additionally, Node-API provides a set of functions which allow calling
14447 <p>Any non-<code>NULL</code> data which is passed to this API via the <code>data</code> field of the
14449 whenever <code>object</code> is garbage-collected by passing both <code>object</code> and the data …
14454 <span>N-API version: 1</span>
14456 …e><code class="language-c">NAPI_EXTERN napi_status <span class="hljs-title function_">napi_call_fu…
14459 <span class="hljs-type">size_t</span> argc,
14460 <span class="hljs-keyword">const</span> napi_value* argv,
14473 add-on. This is the primary mechanism of calling back <em>from</em> the add-on's
14478 …ode class="language-js"><span class="hljs-keyword">function</span> <span class="hljs-title functio…
14479 <span class="hljs-keyword">return</span> num + <span class="hljs-number">2</span>;
14481 <p>Then, the above function can be invoked from a native add-on using the
14483 <pre><code class="language-c"><span class="hljs-comment">// Get the function named "AddTwo" on the …
14486 <span class="hljs-keyword">if</span> (status != napi_ok) <span class="hljs-keyword">return</span>;
14488 status = napi_get_named_property(env, global, <span class="hljs-string">"AddTwo"</span>, &#x26;add_…
14489 <span class="hljs-keyword">if</span> (status != napi_ok) <span class="hljs-keyword">return</span>;
14491 <span class="hljs-comment">// const arg = 1337</span>
14492 status = napi_create_int32(env, <span class="hljs-number">1337</span>, &#x26;arg);
14493 <span class="hljs-keyword">if</span> (status != napi_ok) <span class="hljs-keyword">return</span>;
14496 <span class="hljs-type">size_t</span> argc = <span class="hljs-number">1</span>;
14498 <span class="hljs-comment">// AddTwo(arg);</span>
14501 <span class="hljs-keyword">if</span> (status != napi_ok) <span class="hljs-keyword">return</span>;
14503 <span class="hljs-comment">// Convert the result back to a native type</span>
14504 <span class="hljs-type">int32_t</span> result;
14506 <span class="hljs-keyword">if</span> (status != napi_ok) <span class="hljs-keyword">return</span>;<…
14510 <span>N-API version: 1</span>
14512 <pre><code class="language-c">napi_status <span class="hljs-title function_">napi_create_function</…
14513 … <span class="hljs-keyword">const</span> <span class="hljs-type">char</span>* utf8name,
14514 <span class="hljs-type">size_t</span> length,
14516 <span class="hljs-type">void</span>* data,
14523 it is null-terminated.</li>
14526 <li><code>[in] data</code>: User-provided data context. This will be passed back into the
14532 <p>This API allows an add-on author to create a function object in native code.
14533 This is the primary mechanism to allow calling <em>into</em> the add-on's native code
14539 add-on's module exports, set the newly created function on the exports
14541 <pre><code class="language-c">napi_value <span class="hljs-title function_">SayHello</span><span cl…
14542 <span class="hljs-built_in">printf</span>(<span class="hljs-string">"Hello\n"</span>);
14543 <span class="hljs-keyword">return</span> <span class="hljs-literal">NULL</span>;
14546 napi_value <span class="hljs-title function_">Init</span><span class="hljs-params">(napi_env env, n…
14550 …e_function(env, <span class="hljs-literal">NULL</span>, <span class="hljs-number">0</span>, SayHel…
14551 …<span class="hljs-keyword">if</span> (status != napi_ok) <span class="hljs-keyword">return</span> …
14553 status = napi_set_named_property(env, exports, <span class="hljs-string">"sayHello"</span>, fn);
14554 …<span class="hljs-keyword">if</span> (status != napi_ok) <span class="hljs-keyword">return</span> …
14556 <span class="hljs-keyword">return</span> exports;
14560 <p>Given the above code, the add-on can be used from JavaScript as follows:</p>
14561 …de class="language-js"><span class="hljs-keyword">const</span> myaddon = <span class="hljs-built_i…
14562 myaddon.<span class="hljs-title function_">sayHello</span>();</code></pre>
14563 <p>The string passed to <code>require()</code> is the name of the target in <code>binding.gyp</code>
14565 <p>Any non-<code>NULL</code> data which is passed to this API via the <code>data</code> parameter c…
14567 <code>result</code> parameter) and freed whenever the function is garbage-collected by
14569 …n</code>s are described in <a href="https://tc39.github.io/ecma262/#sec-function-objects">Section …
14574 <span>N-API version: 1</span>
14576 <pre><code class="language-c">napi_status <span class="hljs-title function_">napi_get_cb_info</span…
14578 <span class="hljs-type">size_t</span>* argc,
14581 <span class="hljs-type">void</span>** data)</span></code></pre>
14585 <li><code>[in-out] argc</code>: Specifies the length of the provided <code>argv</code> array and
14601 <span>N-API version: 1</span>
14603 <pre><code class="language-c">napi_status <span class="hljs-title function_">napi_get_new_target</s…
14609 <li><code>[out] result</code>: The <code>new.target</code> of the constructor call.</li>
14612 <p>This API returns the <code>new.target</code> of the constructor call. If the current
14617 <span>N-API version: 1</span>
14619 <pre><code class="language-c">napi_status <span class="hljs-title function_">napi_new_instance</spa…
14621 <span class="hljs-type">size_t</span> argc,
14637 …de class="language-js"><span class="hljs-keyword">function</span> <span class="hljs-title function…
14638 …<span class="hljs-variable language_">this</span>.<span class="hljs-property">param</span> = param;
14641 <span class="hljs-keyword">const</span> arg = <span class="hljs-string">'hello'</span>;
14642 <span class="hljs-keyword">const</span> value = <span class="hljs-keyword">new</span> <span class="…
14643 <p>The following can be approximated in Node-API using the following snippet:</p>
14644 <pre><code class="language-c"><span class="hljs-comment">// Get the constructor function MyObject</…
14647 <span class="hljs-keyword">if</span> (status != napi_ok) <span class="hljs-keyword">return</span>;
14649 status = napi_get_named_property(env, global, <span class="hljs-string">"MyObject"</span>, &#x26;co…
14650 <span class="hljs-keyword">if</span> (status != napi_ok) <span class="hljs-keyword">return</span>;
14652 <span class="hljs-comment">// const arg = "hello"</span>
14653 status = napi_create_string_utf8(env, <span class="hljs-string">"hello"</span>, NAPI_AUTO_LENGTH, &…
14654 <span class="hljs-keyword">if</span> (status != napi_ok) <span class="hljs-keyword">return</span>;
14657 <span class="hljs-type">size_t</span> argc = <span class="hljs-number">1</span>;
14659 <span class="hljs-comment">// const value = new MyObject(arg)</span>
14663 <p>Node-API offers a way to "wrap" C++ classes and instances so that the class
14674 …ef="#n_api_napi_unwrap"><code>napi_unwrap</code></a> obtains the C++ instance that is the target of
14681 <pre><code class="language-c">napi_value MyClass_constructor = <span class="hljs-literal">NULL</spa…
14684 <span class="hljs-type">bool</span> is_instance = <span class="hljs-literal">false</span>;
14687 <span class="hljs-keyword">if</span> (is_instance) {
14688 <span class="hljs-comment">// napi_unwrap() ...</span>
14689 } <span class="hljs-keyword">else</span> {
14690 <span class="hljs-comment">// otherwise...</span>
14698 …de class="language-js"><span class="hljs-keyword">const</span> myAddon = <span class="hljs-built_i…
14700 <span class="hljs-comment">// `openDatabase()` returns a JavaScript object that wraps a native data…
14701 <span class="hljs-comment">// handle.</span>
14702 <span class="hljs-keyword">const</span> dbHandle = myAddon.<span class="hljs-title function_">openD…
14704 <span class="hljs-comment">// `query()` returns a JavaScript object that wraps a native query handl…
14705 …span class="hljs-keyword">const</span> queryHandle = myAddon.<span class="hljs-title function_">qu…
14707 <span class="hljs-comment">// There is an accidental error in the line below. The first parameter t…
14708 <span class="hljs-comment">// `myAddon.queryHasRecords()` should be the database handle (`dbHandle`…
14709 <span class="hljs-comment">// the query handle (`query`), so the correct condition for the while-lo…
14710 <span class="hljs-comment">// should be</span>
14711 <span class="hljs-comment">//</span>
14712 <span class="hljs-comment">// myAddon.queryHasRecords(dbHandle, queryHandle)</span>
14713 <span class="hljs-comment">//</span>
14714 <span class="hljs-keyword">while</span> (myAddon.<span class="hljs-title function_">queryHasRecords…
14715 <span class="hljs-comment">// retrieve records</span>
14738 <p>To this end, Node-API provides type-tagging capabilities.</p>
14739 <p>A type tag is a 128-bit integer unique to the addon. Node-API provides the
14746 previously "marked" with the type tag. This creates a type-checking capability
14747 of a higher fidelity than <code>napi_instanceof()</code> can provide, because such type-
14752 <pre><code class="language-c"><span class="hljs-comment">// This value is the type tag for a databa…
14753 <span class="hljs-comment">//</span>
14754 <span class="hljs-comment">// uuidgen | sed -r -e 's/-//g' -e 's/(.{16})(.*)/0x\1, 0x\2/'</span>
14755 <span class="hljs-comment">//</span>
14756 <span class="hljs-comment">// can be used to obtain the two values with which to initialize the str…
14757 <span class="hljs-keyword">static</span> <span class="hljs-keyword">const</span> napi_type_tag Data…
14758 …<span class="hljs-number">0x1edf75a38336451d</span>, <span class="hljs-number">0xa5ed9ce2e4c00c38<…
14761 <span class="hljs-comment">// This value is the type tag for a query handle.</span>
14762 <span class="hljs-keyword">static</span> <span class="hljs-keyword">const</span> napi_type_tag Quer…
14763 …<span class="hljs-number">0x9c73317f9fad44a3</span>, <span class="hljs-number">0x93c3920bf3b0ad6a<…
14766 <span class="hljs-keyword">static</span> napi_value
14767 <span class="hljs-title function_">openDatabase</span><span class="hljs-params">(napi_env env, napi…
14771 …<span class="hljs-comment">// Perform the underlying action which results in a database handle.</s…
14774 <span class="hljs-comment">// Create a new, empty JS object.</span>
14776 …<span class="hljs-keyword">if</span> (status != napi_ok) <span class="hljs-keyword">return</span> …
14778 …<span class="hljs-comment">// Tag the object to indicate that it holds a pointer to a `DatabaseHan…
14780 …<span class="hljs-keyword">if</span> (status != napi_ok) <span class="hljs-keyword">return</span> …
14782 …<span class="hljs-comment">// Store the pointer to the `DatabaseHandle` structure inside the JS ob…
14783 … result, dbHandle, <span class="hljs-literal">NULL</span>, <span class="hljs-literal">NULL</span>,…
14784 …<span class="hljs-keyword">if</span> (status != napi_ok) <span class="hljs-keyword">return</span> …
14786 <span class="hljs-keyword">return</span> result;
14789 <span class="hljs-comment">// Later when we receive a JavaScript object purporting to be a database…
14790 <span class="hljs-comment">// we can use `napi_check_object_type_tag()` to ensure that it is indeed…
14791 <span class="hljs-comment">// handle.</span>
14793 <span class="hljs-keyword">static</span> napi_value
14794 <span class="hljs-title function_">query</span><span class="hljs-params">(napi_env env, napi_callba…
14796 <span class="hljs-type">size_t</span> argc = <span class="hljs-number">2</span>;
14797 napi_value argv[<span class="hljs-number">2</span>];
14798 <span class="hljs-type">bool</span> is_db_handle;
14800 …t_cb_info(env, info, &#x26;argc, argv, <span class="hljs-literal">NULL</span>, <span class="hljs-l…
14801 …<span class="hljs-keyword">if</span> (status != napi_ok) <span class="hljs-keyword">return</span> …
14803 …<span class="hljs-comment">// Check that the object passed as the first parameter has the previous…
14804 <span class="hljs-comment">// applied tag.</span>
14806 argv[<span class="hljs-number">0</span>],
14809 …<span class="hljs-keyword">if</span> (status != napi_ok) <span class="hljs-keyword">return</span> …
14811 <span class="hljs-comment">// Throw a `TypeError` if it doesn't.</span>
14812 <span class="hljs-keyword">if</span> (!is_db_handle) {
14813 <span class="hljs-comment">// Throw a TypeError.</span>
14814 <span class="hljs-keyword">return</span> <span class="hljs-literal">NULL</span>;
14820 <span>N-API version: 1</span>
14822 <pre><code class="language-c">napi_status <span class="hljs-title function_">napi_define_class</spa…
14823 … <span class="hljs-keyword">const</span> <span class="hljs-type">char</span>* utf8name,
14824 <span class="hljs-type">size_t</span> length,
14826 <span class="hljs-type">void</span>* data,
14827 <span class="hljs-type">size_t</span> property_count,
14828 … <span class="hljs-keyword">const</span> napi_property_descriptor* properties,
14836 if it is null-terminated.</li>
14861 C++ class, <em>non-static</em> data properties, accessors, and methods of the C++
14874 that case, to prevent the function value from being garbage-collected, a
14877 <p>Any non-<code>NULL</code> data which is passed to this API via the <code>data</code> parameter o…
14880 parameter) and freed whenever the class is garbage-collected by passing both
14885 <span>N-API version: 1</span>
14887 <pre><code class="language-c">napi_status <span class="hljs-title function_">napi_wrap</span><span …
14889 <span class="hljs-type">void</span>* native_object,
14891 <span class="hljs-type">void</span>* finalize_hint,
14900 native instance when the JavaScript object is ready for garbage-collection.
14912 <code>napi_wrap()</code> to wrap the newly constructed instance in the already-created
14933 <span>N-API version: 1</span>
14935 <pre><code class="language-c">napi_status <span class="hljs-title function_">napi_unwrap</span><spa…
14937 <span class="hljs-type">void</span>** result)</span>;</code></pre>
14949 object; the wrapped C++ instance that is the target of the call can be obtained
14954 <span>N-API version: 1</span>
14956 <pre><code class="language-c">napi_status <span class="hljs-title function_">napi_remove_wrap</span…
14958 <span class="hljs-type">void</span>** result)</span>;</code></pre>
14968 JavaScript object becomes garbage-collected.</p>
14972 <span>N-API version: 8</span>
14974 <pre><code class="language-c">napi_status <span class="hljs-title function_">napi_type_tag_object</…
14976 … <span class="hljs-keyword">const</span> napi_type_tag* type_tag)</span>;</code></pre>
14992 <span>N-API version: 8</span>
14994 …pre><code class="language-c">napi_status <span class="hljs-title function_">napi_check_object_type…
14996 … <span class="hljs-keyword">const</span> napi_type_tag* type_tag,
14997 … <span class="hljs-type">bool</span>* result)</span>;</code></pre>
15013 <span>N-API version: 5</span>
15015 <pre><code class="language-c">napi_status <span class="hljs-title function_">napi_add_finalizer</sp…
15017 <span class="hljs-type">void</span>* native_object,
15019 <span class="hljs-type">void</span>* finalize_hint,
15028 native data when the JavaScript object is ready for garbage-collection.
15055 <p>Node-API provides an ABI-stable interface for these
15057 <p>Node-API defines the <code>napi_async_work</code> structure which is used to manage
15063 <p>The <code>execute</code> function should avoid making any Node-API calls
15065 JavaScript objects. Most often, any code that needs to make Node-API
15070-c"><span class="hljs-keyword">typedef</span> <span class="hljs-title function_">void</span> <span…
15071 <span class="hljs-type">void</span>* data)</span>;
15072-keyword">typedef</span> <span class="hljs-title function_">void</span> <span class="hljs-params">…
15074 … <span class="hljs-type">void</span>* data)</span>;</code></pre>
15076 addon-provided <code>void*</code> data that was passed into the
15080 <pre><code class="language-c">napi_status <span class="hljs-title function_">napi_queue_async_work<…
15099 <span>N-API version: 1</span>
15101 <pre><code class="language-c">napi_status <span class="hljs-title function_">napi_create_async_work…
15106 <span class="hljs-type">void</span>* data,
15121 <li><code>[in] data</code>: User-provided data context. This will be passed back into the
15130 <p><code>async_resource_name</code> should be a null-terminated, UTF-8-encoded string.</p>
15138 <span>N-API version: 1</span>
15140 <pre><code class="language-c">napi_status <span class="hljs-title function_">napi_delete_async_work…
15152 <span>N-API version: 1</span>
15154 <pre><code class="language-c">napi_status <span class="hljs-title function_">napi_queue_async_work<…
15167 <span>N-API version: 1</span>
15169 <pre><code class="language-c">napi_status <span class="hljs-title function_">napi_cancel_async_work…
15191 <span>N-API version: 1</span>
15193 <pre><code class="language-c">napi_status <span class="hljs-title function_">napi_async_init</span>…
15225 <span>N-API version: 1</span>
15227 <pre><code class="language-c">napi_status <span class="hljs-title function_">napi_async_destroy</sp…
15246 <span>N-API version: 1</span>
15248 …e><code class="language-c">NAPI_EXTERN napi_status <span class="hljs-title function_">napi_make_ca…
15252 <span class="hljs-type">size_t</span> argc,
15253 <span class="hljs-keyword">const</span> napi_value* argv,
15273 add-on. This API is similar to <code>napi_call_function</code>. However, it is used to call
15288 <span>N-API version: 3</span>
15290 …code class="language-c">NAPI_EXTERN napi_status <span class="hljs-title function_">napi_open_callb…
15306 in place when making certain Node-API calls. If there is no other script on
15313 <span>N-API version: 3</span>
15315 …code class="language-c">NAPI_EXTERN napi_status <span class="hljs-title function_">napi_close_call…
15326 <span>N-API version: 1</span>
15328 <pre><code class="language-c"><span class="hljs-keyword">typedef</span> <span class="hljs-class"><s…
15329 <span class="hljs-type">uint32_t</span> major;
15330 <span class="hljs-type">uint32_t</span> minor;
15331 <span class="hljs-type">uint32_t</span> patch;
15332 <span class="hljs-keyword">const</span> <span class="hljs-type">char</span>* release;
15335 napi_status <span class="hljs-title function_">napi_get_node_version</span><span class="hljs-params…
15336 … <span class="hljs-keyword">const</span> napi_node_version** version)</span>;</code></pre>
15349 <span>N-API version: 1</span>
15351 <pre><code class="language-c">napi_status <span class="hljs-title function_">napi_get_version</span…
15352 <span class="hljs-type">uint32_t</span>* result)</span>;</code></pre>
15355 <li><code>[out] result</code>: The highest version of Node-API supported.</li>
15358 <p>This API returns the highest Node-API version supported by the
15359 Node.js runtime. Node-API is planned to be additive such that
15376 <span>N-API version: 1</span>
15378 …ode class="language-c">NAPI_EXTERN napi_status <span class="hljs-title function_">napi_adjust_exte…
15379 … <span class="hljs-type">int64_t</span> change_in_bytes,
15380 … <span class="hljs-type">int64_t</span>* result)</span>;</code></pre>
15394 <p>Node-API provides facilities for creating <code>Promise</code> objects as described in
15395 <a href="https://tc39.github.io/ecma262/#sec-promise-objects">Section 25.4</a> of the ECMA specific…
15404 <pre><code class="language-c">napi_deferred deferred;
15408 <span class="hljs-comment">// Create the promise.</span>
15410 <span class="hljs-keyword">if</span> (status != napi_ok) <span class="hljs-keyword">return</span> <…
15412 <span class="hljs-comment">// Pass the deferred to a function that performs an asynchronous action.…
15415 <span class="hljs-comment">// Return the promise to JS</span>
15416 <span class="hljs-keyword">return</span> promise;</code></pre>
15420 <pre><code class="language-c">napi_deferred deferred;
15424 <span class="hljs-comment">// Create a value with which to conclude the deferred.</span>
15426 <span class="hljs-keyword">if</span> (status != napi_ok) <span class="hljs-keyword">return</span> <…
15428 <span class="hljs-comment">// Resolve or reject the promise associated with the deferred depending …
15429 <span class="hljs-comment">// whether the asynchronous action succeeded.</span>
15430 <span class="hljs-keyword">if</span> (asynchronous_action_succeeded) {
15432 } <span class="hljs-keyword">else</span> {
15435 <span class="hljs-keyword">if</span> (status != napi_ok) <span class="hljs-keyword">return</span> <…
15437 <span class="hljs-comment">// At this point the deferred has been freed, so we should assign NULL t…
15438 deferred = <span class="hljs-literal">NULL</span>;</code></pre>
15442 <span>N-API version: 1</span>
15444 <pre><code class="language-c">napi_status <span class="hljs-title function_">napi_create_promise</s…
15459 <span>N-API version: 1</span>
15461 <pre><code class="language-c">napi_status <span class="hljs-title function_">napi_resolve_deferred<…
15479 <span>N-API version: 1</span>
15481 <pre><code class="language-c">napi_status <span class="hljs-title function_">napi_reject_deferred</…
15499 <span>N-API version: 1</span>
15501 <pre><code class="language-c">napi_status <span class="hljs-title function_">napi_is_promise</span>…
15503 <span class="hljs-type">bool</span>* is_promise)</span>;</code></pre>
15511 <p>Node-API provides an API for executing a string containing JavaScript using the
15516 <span>N-API version: 1</span>
15518 …pre><code class="language-c">NAPI_EXTERN napi_status <span class="hljs-title function_">napi_run_s…
15531 <a href="#modules_the_module_scope">module scope</a>, meaning that pseudo-globals such as <code>req…
15540 <p>Node-API provides a function for getting the current event loop associated with
15545 <span>N-API version: 2</span>
15547 …<code class="language-c">NAPI_EXTERN napi_status <span class="hljs-title function_">napi_get_uv_ev…
15548 … <span class="hljs-keyword">struct</span> uv_loop_s** loop)</span>;</code></pre>
15553 </section><section><h3>Asynchronous thread-safe function calls<span><a class="mark" href="#n_api_as…
15555 thread. If an addon creates additional threads, then Node-API functions that
15561 JavaScript function on their behalf. The thread-safe function APIs provide an
15571 provided. This callback will be invoked on the main thread when the thread-safe
15575 no threads should be using the thread-safe function after the finalize callback
15580 <h4>Calling a thread-safe function<span><a class="mark" href="#n_api_calling_a_thread_safe_function…
15584 behaves non-blockingly, returning <code>napi_queue_full</code> if the queue was full,
15587 <code>napi_call_threadsafe_function()</code> never blocks if the thread-safe function was
15605 Node.js process exits while there is a thread-safe function still active.</p>
15607 Node-API runs <code>call_js_cb</code> in a context appropriate for callbacks.</p>
15608 <h4>Reference counting of thread-safe functions<span><a class="mark" href="#n_api_reference_countin…
15612 indicate that a new thread will start making use of the thread-safe function.
15614 existing thread will stop making use of the thread-safe function.</p>
15623 reason, do not use a thread-safe function
15629 number of aquisitions of the thread-safe functions, instead of calling
15636 <p>The thread-safe function can be "aborted" by giving a value of <code>napi_tsfn_abort</code>
15638 associated with the thread-safe function except
15642 possible to make asynchronous calls to the thread-safe function. This can be
15645 the thread-safe function anymore because it is no longer guaranteed to
15648 <p>Similarly to libuv handles, thread-safe functions can be "referenced" and
15649 "unreferenced". A "referenced" thread-safe function will cause the event loop on
15650 the thread on which it is created to remain alive until the thread-safe function
15651 is destroyed. In contrast, an "unreferenced" thread-safe function will not
15654 <p>Neither does <code>napi_unref_threadsafe_function</code> mark the thread-safe functions as
15668 <span>N-API version: 4</span>
15670 <pre><code class="language-c">NAPI_EXTERN napi_status
15671 <span class="hljs-title function_">napi_create_threadsafe_function</span><span class="hljs-params">…
15675 <span class="hljs-type">size_t</span> max_queue_size,
15676 <span class="hljs-type">size_t</span> initial_thread_count,
15677 <span class="hljs-type">void</span>* thread_finalize_data,
15679 <span class="hljs-type">void</span>* context,
15705 <li><code>[out] result</code>: The asynchronous thread-safe JavaScript function.</li>
15710 <span>N-API version: 4</span>
15712 <pre><code class="language-c">NAPI_EXTERN napi_status
15713 <span class="hljs-title function_">napi_get_threadsafe_function_context</span><span class="hljs-par…
15714 … <span class="hljs-type">void</span>** result)</span>;</code></pre>
15716 <li><code>[in] func</code>: The thread-safe function for which to retrieve the context.</li>
15733 <span>N-API version: 4</span>
15735 <pre><code class="language-c">NAPI_EXTERN napi_status
15736 <span class="hljs-title function_">napi_call_threadsafe_function</span><span class="hljs-params">(n…
15737 <span class="hljs-type">void</span>* data,
15740 <li><code>[in] func</code>: The asynchronous thread-safe JavaScript function to invoke.</li>
15742 provided during the creation of the thread-safe JavaScript function.</li>
15758 <span>N-API version: 4</span>
15760 <pre><code class="language-c">NAPI_EXTERN napi_status
15761 <span class="hljs-title function_">napi_acquire_threadsafe_function</span><span class="hljs-params"…
15763 <li><code>[in] func</code>: The asynchronous thread-safe JavaScript function to start making
15766 <p>A thread should call this API before passing <code>func</code> to any other thread-safe
15774 <span>N-API version: 4</span>
15776 <pre><code class="language-c">NAPI_EXTERN napi_status
15777 <span class="hljs-title function_">napi_release_threadsafe_function</span><span class="hljs-params"…
15780 <li><code>[in] func</code>: The asynchronous thread-safe JavaScript function whose reference
15783 that the current thread will make no further calls to the thread-safe
15785 thread, no other thread should make any further calls to the thread-safe
15791 to any thread-safe APIs after having called this API has undefined results, as
15797 <span>N-API version: 4</span>
15799 <pre><code class="language-c">NAPI_EXTERN napi_status
15800 <span class="hljs-title function_">napi_ref_threadsafe_function</span><span class="hljs-params">(na…
15803 <li><code>[in] func</code>: The thread-safe function to reference.</li>
15808 <p>Neither does <code>napi_unref_threadsafe_function</code> mark the thread-safe functions as
15816 <span>N-API version: 4</span>
15818 <pre><code class="language-c">NAPI_EXTERN napi_status
15819 <span class="hljs-title function_">napi_unref_threadsafe_function</span><span class="hljs-params">(…
15822 <li><code>[in] func</code>: The thread-safe function to unreference.</li>
15833 … api_stability_1"><a href="#documentation_stability_index">Stability: 1</a> - Experimental</div><p…
15834 <pre><code class="language-c">NAPI_EXTERN napi_status
15835-title function_">node_api_get_module_file_name</span><span class="hljs-params">(napi_env env, <sp…
15840 location from which the add-on was loaded. For a file on the local
15841 file system it will start with <code>file://</code>. The string is null-terminated and
15844 <p><code>result</code> may be an empty string if the add-on loading process fails to establish
15845 the add-on's file name during loading.</p></section>
15856 <a href="deprecations.html">deprecation policy</a> and may occur on each semver-major release witho…
15861 <code>node -e &#x3C;code></code>, i.e. that will take a piece of JavaScript and run it in
15862 a Node.js-specific environment.</p>
15864 <h4>Setting up per-process state<span><a class="mark" href="#embedding_setting_up_per_process_state…
15865 <p>Node.js requires some per-process state management in order to run:</p>
15868 <li>V8 per-process requirements, such as a <code>v8::Platform</code> instance.</li>
15872-cpp"><span class="hljs-function"><span class="hljs-type">int</span> <span class="hljs-title">main…
15873 argv = <span class="hljs-built_in">uv_setup_args</span>(argc, argv);
15874 …<span class="hljs-function">std::vector&#x3C;std::string> <span class="hljs-title">args</span><spa…
15877 …<span class="hljs-comment">// Parse Node.js CLI options, and print any errors that have occurred w…
15878 <span class="hljs-comment">// trying to parse them.</span>
15879 …<span class="hljs-type">int</span> exit_code = node::<span class="hljs-built_in">InitializeNodeWit…
15880 …<span class="hljs-keyword">for</span> (<span class="hljs-keyword">const</span> std::string&#x26; e…
15881-built_in">fprintf</span>(stderr, <span class="hljs-string">"%s: %s\n"</span>, args[<span class="h…
15882 <span class="hljs-keyword">if</span> (exit_code != <span class="hljs-number">0</span>) {
15883 <span class="hljs-keyword">return</span> exit_code;
15886 …<span class="hljs-comment">// Create a v8::Platform instance. `MultiIsolatePlatform::Create()` is …
15887 …<span class="hljs-comment">// to create a v8::Platform instance that Node.js can use when creating…
15888 …<span class="hljs-comment">// Worker threads. When no `MultiIsolatePlatform` instance is present,<…
15889 <span class="hljs-comment">// Worker threads are disabled.</span>
15891 …MultiIsolatePlatform::<span class="hljs-built_in">Create</span>(<span class="hljs-number">4</span>…
15892 …V8::<span class="hljs-built_in">InitializePlatform</span>(platform.<span class="hljs-built_in">get…
15893 V8::<span class="hljs-built_in">Initialize</span>();
15895 <span class="hljs-comment">// See below for the contents of this function.</span>
15896 …<span class="hljs-type">int</span> ret = <span class="hljs-built_in">RunNodeInstance</span>(platfo…
15898 V8::<span class="hljs-built_in">Dispose</span>();
15899 V8::<span class="hljs-built_in">ShutdownPlatform</span>();
15900 <span class="hljs-keyword">return</span> ret;
15902 <h4>Per-instance state<span><a class="mark" href="#embedding_per_instance_state" id="embedding_per_…
15924 sets it up with some Node.js-specific hooks (e.g. the Node.js error handler),
15926 …"language-cpp"><span class="hljs-function"><span class="hljs-type">int</span> <span class="hljs-ti…
15927 … <span class="hljs-keyword">const</span> std::vector&#x3C;std::string>&#x26; args,
15928 …<span class="hljs-keyword">const</span> std::vector&#x3C;std::string>&#x26; exec_args)</span> </sp…
15929 <span class="hljs-type">int</span> exit_code = <span class="hljs-number">0</span>;
15930 <span class="hljs-comment">// Set up a libuv event loop.</span>
15931 <span class="hljs-type">uv_loop_t</span> loop;
15932 …<span class="hljs-type">int</span> ret = <span class="hljs-built_in">uv_loop_init</span>(&#x26;loo…
15933 <span class="hljs-keyword">if</span> (ret != <span class="hljs-number">0</span>) {
15934 …<span class="hljs-built_in">fprintf</span>(stderr, <span class="hljs-string">"%s: Failed to initia…
15935 args[<span class="hljs-number">0</span>].<span class="hljs-built_in">c_str</span>(),
15936 <span class="hljs-built_in">uv_err_name</span>(ret));
15937 <span class="hljs-keyword">return</span> <span class="hljs-number">1</span>;
15941 ArrayBufferAllocator::<span class="hljs-built_in">Create</span>();
15943 Isolate* isolate = <span class="hljs-built_in">NewIsolate</span>(allocator, &#x26;loop, platform);
15944 <span class="hljs-keyword">if</span> (isolate == <span class="hljs-literal">nullptr</span>) {
15945-built_in">fprintf</span>(stderr, <span class="hljs-string">"%s: Failed to initialize V8 Isolate\n…
15946 <span class="hljs-keyword">return</span> <span class="hljs-number">1</span>;
15950 …<span class="hljs-function">Locker <span class="hljs-title">locker</span><span class="hljs-params"…
15951 …<span class="hljs-function">Isolate::Scope <span class="hljs-title">isolate_scope</span><span clas…
15953 …<span class="hljs-comment">// Create a node::IsolateData instance that will later be released usin…
15954 <span class="hljs-comment">// node::FreeIsolateData().</span>
15955-function">std::unique_ptr&#x3C;IsolateData, <span class="hljs-title">decltype</span><span class="…
15959 <span class="hljs-comment">// Set up a new v8::Context.</span>
15960 …<span class="hljs-function">HandleScope <span class="hljs-title">handle_scope</span><span class="h…
15961 Local&#x3C;Context> context = node::<span class="hljs-built_in">NewContext</span>(isolate);
15962 <span class="hljs-keyword">if</span> (context.<span class="hljs-built_in">IsEmpty</span>()) {
15963-built_in">fprintf</span>(stderr, <span class="hljs-string">"%s: Failed to initialize V8 Context\n…
15964 <span class="hljs-keyword">return</span> <span class="hljs-number">1</span>;
15967 …<span class="hljs-comment">// The v8::Context needs to be entered when node::CreateEnvironment() a…
15968 <span class="hljs-comment">// node::LoadEnvironment() are being called.</span>
15969 …<span class="hljs-function">Context::Scope <span class="hljs-title">context_scope</span><span clas…
15971 …<span class="hljs-comment">// Create a node::Environment instance that will later be released usin…
15972 <span class="hljs-comment">// node::FreeEnvironment().</span>
15973-function">std::unique_ptr&#x3C;Environment, <span class="hljs-title">decltype</span><span class="…
15977 …<span class="hljs-comment">// Set up the Node.js instance for execution, and run code inside of it…
15978 …<span class="hljs-comment">// There is also a variant that takes a callback and provides it with</…
15979 …<span class="hljs-comment">// the `require` and `process` objects, so that it can manually compile…
15980 <span class="hljs-comment">// and run scripts as needed.</span>
15981 …<span class="hljs-comment">// The `require` function inside this script does *not* access the file…
15982 <span class="hljs-comment">// system, and can only load built-in Node.js modules.</span>
15983 …<span class="hljs-comment">// `module.createRequire()` is being used to create one that is able to…
15984 …<span class="hljs-comment">// load files from the disk, and uses the standard CommonJS file loader…
15985 <span class="hljs-comment">// instead of the internal-only `require` function.</span>
15986 MaybeLocal&#x3C;Value> loadenv_ret = node::<span class="hljs-built_in">LoadEnvironment</span>(
15987 env.<span class="hljs-built_in">get</span>(),
15988 <span class="hljs-string">"const publicRequire ="</span>
15989 <span class="hljs-string">" require('module').createRequire(process.cwd() + '/');"</span>
15990 <span class="hljs-string">"globalThis.require = publicRequire;"</span>
15991 <span class="hljs-string">"require('vm').runInThisContext(process.argv[1]);"</span>);
15993 …<span class="hljs-keyword">if</span> (loadenv_ret.<span class="hljs-built_in">IsEmpty</span>()) <…
15994 <span class="hljs-keyword">return</span> <span class="hljs-number">1</span>;
15997 … <span class="hljs-comment">// SealHandleScope protects against handle leaks from callbacks.</span>
15998 …<span class="hljs-function">SealHandleScope <span class="hljs-title">seal</span><span class="hljs-
15999 <span class="hljs-type">bool</span> more;
16000 <span class="hljs-keyword">do</span> {
16001 <span class="hljs-built_in">uv_run</span>(&#x26;loop, UV_RUN_DEFAULT);
16003 …<span class="hljs-comment">// V8 tasks on background threads may end up scheduling new tasks in th…
16004 …<span class="hljs-comment">// foreground, which in turn can keep the event loop going. For example…
16005 <span class="hljs-comment">// WebAssembly.compile() may do so.</span>
16006 platform-><span class="hljs-built_in">DrainTasks</span>(isolate);
16008 <span class="hljs-comment">// If there are new tasks, continue.</span>
16009 more = <span class="hljs-built_in">uv_loop_alive</span>(&#x26;loop);
16010 <span class="hljs-keyword">if</span> (more) <span class="hljs-keyword">continue</span>;
16012 …<span class="hljs-comment">// node::EmitProcessBeforeExit() is used to emit the 'beforeExit' event…
16013 <span class="hljs-comment">// on the `process` object.</span>
16014 …js-keyword">if</span> (node::<span class="hljs-built_in">EmitProcessBeforeExit</span>(env.<span cl…
16015 <span class="hljs-keyword">break</span>;
16017 …<span class="hljs-comment">// 'beforeExit' can also schedule new work that keeps the event loop</s…
16018 <span class="hljs-comment">// running.</span>
16019 more = <span class="hljs-built_in">uv_loop_alive</span>(&#x26;loop);
16020 } <span class="hljs-keyword">while</span> (more == <span class="hljs-literal">true</span>);
16023 <span class="hljs-comment">// node::EmitProcessExit() returns the current exit code.</span>
16024 …"hljs-built_in">EmitProcessExit</span>(env.<span class="hljs-built_in">get</span>()).<span class="…
16026 …<span class="hljs-comment">// node::Stop() can be used to explicitly stop the event loop and keep<…
16027 …<span class="hljs-comment">// further JavaScript from running. It can be called from any thread,</…
16028 …<span class="hljs-comment">// and will act like worker.terminate() if called from another thread.<…
16029 node::<span class="hljs-built_in">Stop</span>(env.<span class="hljs-built_in">get</span>());
16032 …<span class="hljs-comment">// Unregister the Isolate with the platform and add a listener that is …
16033 …<span class="hljs-comment">// when the Platform is done cleaning up any state it had associated wi…
16034 <span class="hljs-comment">// the Isolate.</span>
16035 <span class="hljs-type">bool</span> platform_finished = <span class="hljs-literal">false</span>;
16036 …platform-><span class="hljs-built_in">AddIsolateFinishedCallback</span>(isolate, [](<span class="h…
16037 …*<span class="hljs-built_in">static_cast</span>&#x3C;<span class="hljs-type">bool</span>*>(data) =…
16039 platform-><span class="hljs-built_in">UnregisterIsolate</span>(isolate);
16040 isolate-><span class="hljs-built_in">Dispose</span>();
16042 …<span class="hljs-comment">// Wait until the platform has cleaned up all relevant resources.</span>
16043 <span class="hljs-keyword">while</span> (!platform_finished)
16044 <span class="hljs-built_in">uv_run</span>(&#x26;loop, UV_RUN_ONCE);
16045 …<span class="hljs-type">int</span> err = <span class="hljs-built_in">uv_loop_close</span>(&#x26;lo…
16046 <span class="hljs-built_in">assert</span>(err == <span class="hljs-number">0</span>);
16048 <span class="hljs-keyword">return</span> exit_code;
16053 …ty api_stability_2"><a href="#documentation_stability_index">Stability: 2</a> - Stable</div><p></p>
16056 a manner that is similar, but not identical, to <a href="http://man7.org/linux/man-pages/man3/popen…
16058 …e class="language-js"><span class="hljs-keyword">const</span> { spawn } = <span class="hljs-built_…
16059-keyword">const</span> ls = <span class="hljs-title function_">spawn</span>(<span class="hljs-stri…
16061-property">stdout</span>.<span class="hljs-title function_">on</span>(<span class="hljs-string">'d…
16062 …ass="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span c…
16065-property">stderr</span>.<span class="hljs-title function_">on</span>(<span class="hljs-string">'d…
16066 …ss="hljs-variable language_">console</span>.<span class="hljs-title function_">error</span>(<span …
16069 ….<span class="hljs-title function_">on</span>(<span class="hljs-string">'close'</span>, <span clas…
16070-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hlj…
16074 limited (and platform-specific) capacity. If the subprocess writes to
16082 <p>On Windows, environment variables are case-insensitive. Node.js
16084 case-insensitively matches. Only first (in lexicographic order) entry will be
16128 …ptions_callback"><code>child_process.execFile()</code></a> can vary based on platform. On Unix-type
16139 <pre><code class="language-js"><span class="hljs-comment">// On Windows Only...</span>
16140 <span class="hljs-keyword">const</span> { spawn } = <span class="hljs-built_in">require</span>(<spa…
16141-keyword">const</span> bat = <span class="hljs-title function_">spawn</span>(<span class="hljs-str…
16143-property">stdout</span>.<span class="hljs-title function_">on</span>(<span class="hljs-string">'d…
16144 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>…
16147-property">stderr</span>.<span class="hljs-title function_">on</span>(<span class="hljs-string">'d…
16148 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">error</spa…
16151 ….<span class="hljs-title function_">on</span>(<span class="hljs-string">'exit'</span>, <span class…
16152 …s-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hl…
16154 <pre><code class="language-js"><span class="hljs-comment">// OR...</span>
16155 <span class="hljs-keyword">const</span> { exec, spawn } = <span class="hljs-built_in">require</span…
16156 …span class="hljs-title function_">exec</span>(<span class="hljs-string">'my.bat'</span>, <span cla…
16157 <span class="hljs-keyword">if</span> (err) {
16158 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">error</spa…
16159 <span class="hljs-keyword">return</span>;
16161 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>…
16164 <span class="hljs-comment">// Script with spaces in the filename:</span>
16165-keyword">const</span> bat = <span class="hljs-title function_">spawn</span>(<span class="hljs-str…
16166 <span class="hljs-comment">// or:</span>
16167 …lass="hljs-title function_">exec</span>(<span class="hljs-string">'"my script.cmd" a b'</span>, <s…
16168 <span class="hljs-comment">// ...</span>
16187 …oper.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string>…
16188 <li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference…
16190 <li><code>cwd</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structur…
16192 …developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type">&#x3…
16193 <li><code>encoding</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_str…
16194 <li><code>shell</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struct…
16199 <li><code>timeout</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_stru…
16200 <li><code>maxBuffer</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_st…
16204 …zilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | …
16205-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;number></a> Sets the user …
16206-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;number></a> Sets the group…
16207 <li><code>windowsHide</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_…
16211 <li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Referenc…
16213 <li><code>error</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/G…
16214 <li><code>stdout</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struc…
16215 <li><code>stderr</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struc…
16223 <a href="https://en.wikipedia.org/wiki/List_of_command-line_interpreters">shell</a>)
16225 …de class="language-js"><span class="hljs-keyword">const</span> { exec } = <span class="hljs-built_…
16227 <span class="hljs-title function_">exec</span>(<span class="hljs-string">'"/path/to/test file/test.…
16228 <span class="hljs-comment">// Double quotes are used so that the space in the path is not interpret…
16229 <span class="hljs-comment">// a delimiter of multiple arguments.</span>
16231 <span class="hljs-title function_">exec</span>(<span class="hljs-string">'echo "The \\$HOME variabl…
16232 <span class="hljs-comment">// The $HOME variable is escaped in the first instance, but not in the s…
16243 the output as UTF-8 and pass strings to the callback. The <code>encoding</code> option
16247 …de class="language-js"><span class="hljs-keyword">const</span> { exec } = <span class="hljs-built_…
16248 …"hljs-title function_">exec</span>(<span class="hljs-string">'cat *.js missing_file | wc -l'</span…
16249 <span class="hljs-keyword">if</span> (error) {
16250 …="hljs-variable language_">console</span>.<span class="hljs-title function_">error</span>(<span cl…
16251 <span class="hljs-keyword">return</span>;
16253 …ass="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span c…
16254 …ss="hljs-variable language_">console</span>.<span class="hljs-title function_">error</span>(<span …
16259 <p>Unlike the <a href="http://man7.org/linux/man-pages/man3/exec.3.html"><code>exec(3)</code></a> P…
16267 …code class="language-js"><span class="hljs-keyword">const</span> util = <span class="hljs-built_in…
16268-keyword">const</span> exec = util.<span class="hljs-title function_">promisify</span>(<span class…
16270 …ss="hljs-keyword">async</span> <span class="hljs-keyword">function</span> <span class="hljs-title …
16271 …js-keyword">const</span> { stdout, stderr } = <span class="hljs-keyword">await</span> <span class=…
16272 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>…
16273 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">error</spa…
16275 <span class="hljs-title function_">lsExample</span>();</code></pre>
16279 …de class="language-js"><span class="hljs-keyword">const</span> { exec } = <span class="hljs-built_…
16280 <span class="hljs-keyword">const</span> controller = <span class="hljs-keyword">new</span> <span cl…
16281 <span class="hljs-keyword">const</span> { signal } = controller;
16282-keyword">const</span> child = <span class="hljs-title function_">exec</span>(<span class="hljs-st…
16283 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>…
16285 controller.<span class="hljs-title function_">abort</span>();</code></pre>
16303 <li><code>file</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
16304 <li><code>args</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
16305 <li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference…
16307 <li><code>cwd</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structur…
16308 …developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type">&#x3…
16309 <li><code>encoding</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_str…
16310 <li><code>timeout</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_stru…
16311 <li><code>maxBuffer</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_st…
16315 …zilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | …
16316-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;number></a> Sets the user …
16317-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;number></a> Sets the group…
16318 <li><code>windowsHide</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_…
16320 <li><code>windowsVerbatimArguments</code> <a href="https://developer.mozilla.org/en-US/docs/Web/Jav…
16322 …illa.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type">&#x3C;boolean></a> |…
16330 <li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Referenc…
16332 <li><code>error</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/G…
16333 <li><code>stdout</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struc…
16334 <li><code>stderr</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struc…
16346 … class="language-js"><span class="hljs-keyword">const</span> { execFile } = <span class="hljs-buil…
16347-keyword">const</span> child = <span class="hljs-title function_">execFile</span>(<span class="hlj…
16348 <span class="hljs-keyword">if</span> (error) {
16349 <span class="hljs-keyword">throw</span> error;
16351 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>…
16355 the output as UTF-8 and pass strings to the callback. The <code>encoding</code> option
16365 …code class="language-js"><span class="hljs-keyword">const</span> util = <span class="hljs-built_in…
16366-keyword">const</span> execFile = util.<span class="hljs-title function_">promisify</span>(<span c…
16367 …ss="hljs-keyword">async</span> <span class="hljs-keyword">function</span> <span class="hljs-title …
16368-keyword">const</span> { stdout } = <span class="hljs-keyword">await</span> <span class="hljs-titl…
16369 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>…
16371 <span class="hljs-title function_">getVersion</span>();</code></pre>
16378 … class="language-js"><span class="hljs-keyword">const</span> { execFile } = <span class="hljs-buil…
16379 <span class="hljs-keyword">const</span> controller = <span class="hljs-keyword">new</span> <span cl…
16380 <span class="hljs-keyword">const</span> { signal } = controller;
16381-keyword">const</span> child = <span class="hljs-title function_">execFile</span>(<span class="hlj…
16382 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>…
16384 controller.<span class="hljs-title function_">abort</span>();</code></pre>
16410 <li><code>modulePath</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_s…
16411 <li><code>args</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
16412 <li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference…
16414 <li><code>cwd</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structur…
16415 <li><code>detached</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_str…
16418 …developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type">&#x3…
16419 <li><code>execPath</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_str…
16420 <li><code>execArgv</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_str…
16422-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;number></a> Sets the group…
16423 <li><code>serialization</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Dat…
16428 …zilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | …
16430 <li><code>silent</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struc…
16434 …illa.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array" class="type">&#x3C;Array></a> |…
16438-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;number></a> Sets the user …
16439 <li><code>windowsVerbatimArguments</code> <a href="https://developer.mozilla.org/en-US/docs/Web/Jav…
16441 <li><code>timeout</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_stru…
16451 built-in that allows messages to be passed back and forth between the parent and
16465 <p>Unlike the <a href="http://man7.org/linux/man-pages/man2/fork.2.html"><code>fork(2)</code></a> P…
16472 …ge-js"><span class="hljs-keyword">if</span> (process.<span class="hljs-property">argv</span>[<span…
16473 <span class="hljs-built_in">setTimeout</span>(<span class="hljs-function">() =></span> {
16474-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hlj…
16475 }, <span class="hljs-number">1_000</span>);
16476 } <span class="hljs-keyword">else</span> {
16477 …<span class="hljs-keyword">const</span> { fork } = <span class="hljs-built_in">require</span>(<spa…
16478 …<span class="hljs-keyword">const</span> controller = <span class="hljs-keyword">new</span> <span c…
16479 <span class="hljs-keyword">const</span> { signal } = controller;
16480 …<span class="hljs-keyword">const</span> child = <span class="hljs-title function_">fork</span>(__f…
16481 ….<span class="hljs-title function_">on</span>(<span class="hljs-string">'error'</span>, <span clas…
16482 …<span class="hljs-comment">// This will be called with err being an AbortError if the controller a…
16484 …controller.<span class="hljs-title function_">abort</span>(); <span class="hljs-comment">// Stops …
16514 <p><code>command</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struc…
16517 <p><code>args</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structur…
16520 <p><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/…
16522 <li><code>cwd</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structur…
16523 …developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type">&#x3…
16524 <li><code>argv0</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struct…
16526 …illa.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array" class="type">&#x3C;Array></a> |…
16528 <li><code>detached</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_str…
16531-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;number></a> Sets the user …
16532-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;number></a> Sets the group…
16533 <li><code>serialization</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Dat…
16536 …illa.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type">&#x3C;boolean></a> |…
16540 <li><code>windowsVerbatimArguments</code> <a href="https://developer.mozilla.org/en-US/docs/Web/Jav…
16543 <li><code>windowsHide</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_…
16547 <li><code>timeout</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_stru…
16549 …zilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | …
16558 <code>command</code>, with command-line arguments in <code>args</code>. If omitted, <code>args</cod…
16564 <pre><code class="language-js"><span class="hljs-keyword">const</span> defaults = {
16565 <span class="hljs-attr">cwd</span>: <span class="hljs-literal">undefined</span>,
16566 <span class="hljs-attr">env</span>: process.<span class="hljs-property">env</span>
16576 <p>Example of running <code>ls -lh /usr</code>, capturing <code>stdout</code>, <code>stderr</code>,…
16578 …e class="language-js"><span class="hljs-keyword">const</span> { spawn } = <span class="hljs-built_…
16579-keyword">const</span> ls = <span class="hljs-title function_">spawn</span>(<span class="hljs-stri…
16581-property">stdout</span>.<span class="hljs-title function_">on</span>(<span class="hljs-string">'d…
16582 …ass="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span c…
16585-property">stderr</span>.<span class="hljs-title function_">on</span>(<span class="hljs-string">'d…
16586 …ss="hljs-variable language_">console</span>.<span class="hljs-title function_">error</span>(<span …
16589 ….<span class="hljs-title function_">on</span>(<span class="hljs-string">'close'</span>, <span clas…
16590-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hlj…
16593 …e class="language-js"><span class="hljs-keyword">const</span> { spawn } = <span class="hljs-built_…
16594 …ass="hljs-keyword">const</span> ps = <span class="hljs-title function_">spawn</span>(<span class="…
16595 …s="hljs-keyword">const</span> grep = <span class="hljs-title function_">spawn</span>(<span class="…
16597-property">stdout</span>.<span class="hljs-title function_">on</span>(<span class="hljs-string">'d…
16598 …grep.<span class="hljs-property">stdin</span>.<span class="hljs-title function_">write</span>(data…
16601-property">stderr</span>.<span class="hljs-title function_">on</span>(<span class="hljs-string">'d…
16602 …="hljs-variable language_">console</span>.<span class="hljs-title function_">error</span>(<span cl…
16605 ….<span class="hljs-title function_">on</span>(<span class="hljs-string">'close'</span>, <span clas…
16606 <span class="hljs-keyword">if</span> (code !== <span class="hljs-number">0</span>) {
16607-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hlj…
16609 grep.<span class="hljs-property">stdin</span>.<span class="hljs-title function_">end</span>();
16612-property">stdout</span>.<span class="hljs-title function_">on</span>(<span class="hljs-string">'d…
16613 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>…
16616-property">stderr</span>.<span class="hljs-title function_">on</span>(<span class="hljs-string">'d…
16617 …"hljs-variable language_">console</span>.<span class="hljs-title function_">error</span>(<span cla…
16620 ….<span class="hljs-title function_">on</span>(<span class="hljs-string">'close'</span>, <span clas…
16621 <span class="hljs-keyword">if</span> (code !== <span class="hljs-number">0</span>) {
16622-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hlj…
16626 …e class="language-js"><span class="hljs-keyword">const</span> { spawn } = <span class="hljs-built_…
16627 <span class="hljs-keyword">const</span> subprocess = <span class="hljs-title function_">spawn</span…
16629 ….<span class="hljs-title function_">on</span>(<span class="hljs-string">'error'</span>, <span clas…
16630 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">error</spa…
16641 …e class="language-js"><span class="hljs-keyword">const</span> { spawn } = <span class="hljs-built_…
16642 <span class="hljs-keyword">const</span> controller = <span class="hljs-keyword">new</span> <span cl…
16643 <span class="hljs-keyword">const</span> { signal } = controller;
16644 …s="hljs-keyword">const</span> grep = <span class="hljs-title function_">spawn</span>(<span class="…
16645 ….<span class="hljs-title function_">on</span>(<span class="hljs-string">'error'</span>, <span clas…
16646 …<span class="hljs-comment">// This will be called with err being an AbortError if the controller a…
16648 controller.<span class="hljs-title function_">abort</span>(); <span class="hljs-comment">// Stops t…
16657 <p>On non-Windows platforms, if <code>options.detached</code> is set to <code>true</code>, the child
16660 they are detached or not. See <a href="http://man7.org/linux/man-pages/man2/setsid.2.html"><code>se…
16667 <p>When using the <code>detached</code> option to start a long-running process, the process
16672 <p>Example of a long-running process, by detaching and also ignoring its parent
16674 …e class="language-js"><span class="hljs-keyword">const</span> { spawn } = <span class="hljs-built_…
16676-keyword">const</span> subprocess = <span class="hljs-title function_">spawn</span>(process.<span …
16677 <span class="hljs-attr">detached</span>: <span class="hljs-literal">true</span>,
16678 <span class="hljs-attr">stdio</span>: <span class="hljs-string">'ignore'</span>
16681 subprocess.<span class="hljs-title function_">unref</span>();</code></pre>
16683 …<code class="language-js"><span class="hljs-keyword">const</span> fs = <span class="hljs-built_in"…
16684 <span class="hljs-keyword">const</span> { spawn } = <span class="hljs-built_in">require</span>(<spa…
16685 …ljs-keyword">const</span> out = fs.<span class="hljs-title function_">openSync</span>(<span class=…
16686 …ljs-keyword">const</span> err = fs.<span class="hljs-title function_">openSync</span>(<span class=…
16688 <span class="hljs-keyword">const</span> subprocess = <span class="hljs-title function_">spawn</span…
16689 <span class="hljs-attr">detached</span>: <span class="hljs-literal">true</span>,
16690 <span class="hljs-attr">stdio</span>: [ <span class="hljs-string">'ignore'</span>, out, err ]
16693 subprocess.<span class="hljs-title function_">unref</span>();</code></pre>
16737 <a href="https://docs.microsoft.com/en-us/windows/win32/fileio/synchronous-and-asynchronous-i-o">do…
16738 for more details. This is exactly the same as <code>'pipe'</code> on non-Windows
16785 …e class="language-js"><span class="hljs-keyword">const</span> { spawn } = <span class="hljs-built_…
16787 <span class="hljs-comment">// Child will use parent's stdios.</span>
16788 …lass="hljs-title function_">spawn</span>(<span class="hljs-string">'prg'</span>, [], { <span class…
16790 <span class="hljs-comment">// Spawn child sharing only stderr.</span>
16791-title function_">spawn</span>(<span class="hljs-string">'prg'</span>, [], { <span class="hljs-att…
16793 <span class="hljs-comment">// Open an extra fd=4, to interact with programs presenting a</span>
16794 <span class="hljs-comment">// startd-style interface.</span>
16795-title function_">spawn</span>(<span class="hljs-string">'prg'</span>, [], { <span class="hljs-att…
16802 <p>On Unix-like operating systems, the <a href="#child_process_child_process_spawn_command_args_opt…
16813 <p>Blocking calls like these are mostly useful for simplifying general-purpose
16837 <li><code>file</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
16838 <li><code>args</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
16839 <li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference…
16841 <li><code>cwd</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structur…
16842-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | <a href="#bu…
16845 …zilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | …
16848 …developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type">&#x3…
16849-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;number></a> Sets the user …
16850-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;number></a> Sets the group…
16851 <li><code>timeout</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_stru…
16853 …zilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | …
16855 <li><code>maxBuffer</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_st…
16858 <li><code>encoding</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_str…
16860 <li><code>windowsHide</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_…
16862 …illa.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type">&#x3C;boolean></a> |…
16868 …" class="type">&#x3C;Buffer></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScrip…
16878 <p>If the process times out or has a non-zero exit code, this method will throw an
16903 <li><code>command</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_stru…
16904 <li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference…
16906 <li><code>cwd</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structur…
16907-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | <a href="#bu…
16910 …zilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | …
16913 …developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type">&#x3…
16914 <li><code>shell</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struct…
16917-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;number></a> Sets the user …
16918-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;number></a> Sets the group…
16919 <li><code>timeout</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_stru…
16921 …zilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | …
16923 <li><code>maxBuffer</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_st…
16927 <li><code>encoding</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_str…
16929 <li><code>windowsHide</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_…
16933 …" class="type">&#x3C;Buffer></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScrip…
16942 <p>If the process times out or has a non-zero exit code, this method will throw.
16970 <li><code>command</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_stru…
16971 <li><code>args</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
16972 <li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference…
16974 <li><code>cwd</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structur…
16975-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | <a href="#bu…
16978 <li><code>argv0</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struct…
16980 …zilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | …
16981 …developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type">&#x3…
16982-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;number></a> Sets the user …
16983-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;number></a> Sets the group…
16984 <li><code>timeout</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_stru…
16986 …zilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | …
16988 <li><code>maxBuffer</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_st…
16992 <li><code>encoding</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_str…
16994 …illa.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type">&#x3C;boolean></a> |…
16998 <li><code>windowsVerbatimArguments</code> <a href="https://developer.mozilla.org/en-US/docs/Web/Jav…
17001 <li><code>windowsHide</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_…
17005 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Obje…
17007 <li><code>pid</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structur…
17008 <li><code>output</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/…
17009 …" class="type">&#x3C;Buffer></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScrip…
17010 …" class="type">&#x3C;Buffer></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScrip…
17011 …zilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;number></a> | …
17013 …zilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | …
17015 <li><code>error</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/G…
17046 <li><code>code</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
17047 <li><code>signal</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struc…
17054 …e class="language-js"><span class="hljs-keyword">const</span> { spawn } = <span class="hljs-built_…
17055-keyword">const</span> ls = <span class="hljs-title function_">spawn</span>(<span class="hljs-stri…
17057-property">stdout</span>.<span class="hljs-title function_">on</span>(<span class="hljs-string">'d…
17058 …ass="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span c…
17061 ….<span class="hljs-title function_">on</span>(<span class="hljs-string">'close'</span>, <span clas…
17062-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hlj…
17065 ….<span class="hljs-title function_">on</span>(<span class="hljs-string">'exit'</span>, <span class…
17066-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hlj…
17079 <li><code>err</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Glo…
17096 <li><code>code</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
17097 <li><code>signal</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struc…
17102 the signal, otherwise <code>null</code>. One of the two will always be non-<code>null</code>.</p>
17108 re-raise the handled signal.</p>
17109 <p>See <a href="http://man7.org/linux/man-pages/man2/waitpid.2.html"><code>waitpid(2)</code></a>.</…
17115 <li><code>message</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference…
17137 the spawned process. For example, if <code>bash some-command</code> spawns successfully,
17138 the <code>'spawn'</code> event will fire, though <code>bash</code> may fail to spawn <code>some-com…
17153 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Objec…
17174 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" …
17196 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" c…
17205 …zilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;number></a> | …
17206 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boole…
17210 <a href="http://man7.org/linux/man-pages/man7/signal.7.html"><code>signal(7)</code></a> for a list …
17211 <a href="http://man7.org/linux/man-pages/man2/kill.2.html"><code>kill(2)</code></a> succeeds, and <…
17212 …e class="language-js"><span class="hljs-keyword">const</span> { spawn } = <span class="hljs-built_…
17213 …s="hljs-keyword">const</span> grep = <span class="hljs-title function_">spawn</span>(<span class="…
17215 ….<span class="hljs-title function_">on</span>(<span class="hljs-string">'close'</span>, <span clas…
17216 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(
17217 …<span class="hljs-string">`child process terminated due to receipt of signal <span class="hljs-sub…
17220 <span class="hljs-comment">// Send SIGHUP to process.</span>
17221 grep.<span class="hljs-title function_">kill</span>(<span class="hljs-string">'SIGHUP'</span>);</co…
17229 <p>See <a href="http://man7.org/linux/man-pages/man2/kill.2.html"><code>kill(2)</code></a> for refe…
17237 <pre><code class="language-js"><span class="hljs-meta">'use strict'</span>;
17238 <span class="hljs-keyword">const</span> { spawn } = <span class="hljs-built_in">require</span>(<spa…
17240 <span class="hljs-keyword">const</span> subprocess = <span class="hljs-title function_">spawn</span…
17241 <span class="hljs-string">'sh'</span>,
17243 <span class="hljs-string">'-c'</span>,
17244 <span class="hljs-string">`node -e "setInterval(() => {
17248 … class="hljs-attr">stdio</span>: [<span class="hljs-string">'inherit'</span>, <span class="hljs-st…
17252 <span class="hljs-built_in">setTimeout</span>(<span class="hljs-function">() =></span> {
17253 …subprocess.<span class="hljs-title function_">kill</span>(); <span class="hljs-comment">// Does no…
17254 }, <span class="hljs-number">2000</span>);</code></pre>
17260 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" …
17271 …zilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;integer></a> |…
17276 …e class="language-js"><span class="hljs-keyword">const</span> { spawn } = <span class="hljs-built_…
17277 …s="hljs-keyword">const</span> grep = <span class="hljs-title function_">spawn</span>(<span class="…
17279 …ljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="…
17280 grep.<span class="hljs-property">stdin</span>.<span class="hljs-title function_">end</span>();</cod…
17288 …e class="language-js"><span class="hljs-keyword">const</span> { spawn } = <span class="hljs-built_…
17290-keyword">const</span> subprocess = <span class="hljs-title function_">spawn</span>(process.<span …
17291 <span class="hljs-attr">detached</span>: <span class="hljs-literal">true</span>,
17292 <span class="hljs-attr">stdio</span>: <span class="hljs-string">'ignore'</span>
17295 subprocess.<span class="hljs-title function_">unref</span>();
17296 subprocess.<span class="hljs-title function_">ref</span>();</code></pre>
17314 <li><code>message</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference…
17316 <li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference…
17320 <li><code>keepOpen</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_str…
17325 <li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Referenc…
17326 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boole…
17335 …<code class="language-js"><span class="hljs-keyword">const</span> cp = <span class="hljs-built_in"…
17336 …pan class="hljs-keyword">const</span> n = cp.<span class="hljs-title function_">fork</span>(<span …
17338 …<span class="hljs-title function_">on</span>(<span class="hljs-string">'message'</span>, <span cla…
17339 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>…
17342 <span class="hljs-comment">// Causes the child to print: CHILD got message: { hello: 'world' }</spa…
17343 n.<span class="hljs-title function_">send</span>({ <span class="hljs-attr">hello</span>: <span clas…
17345 …ge-js">process.<span class="hljs-title function_">on</span>(<span class="hljs-string">'message'</s…
17346 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>…
17349 <span class="hljs-comment">// Causes the parent to print: PARENT got message: { foo: 'bar', baz: nu…
17350-title function_">send</span>({ <span class="hljs-attr">foo</span>: <span class="hljs-string">'bar…
17378-js"><span class="hljs-keyword">const</span> subprocess = <span class="hljs-built_in">require</spa…
17380 <span class="hljs-comment">// Open up the server object and send the handle.</span>
17381 …lass="hljs-keyword">const</span> server = <span class="hljs-built_in">require</span>(<span class="…
17382 …pan class="hljs-title function_">on</span>(<span class="hljs-string">'connection'</span>, <span cl…
17383 …socket.<span class="hljs-title function_">end</span>(<span class="hljs-string">'handled by parent'…
17385 server.<span class="hljs-title function_">listen</span>(<span class="hljs-number">1337</span>, <spa…
17386 …subprocess.<span class="hljs-title function_">send</span>(<span class="hljs-string">'server'</span…
17389 …ge-js">process.<span class="hljs-title function_">on</span>(<span class="hljs-string">'message'</s…
17390 <span class="hljs-keyword">if</span> (m === <span class="hljs-string">'server'</span>) {
17391 …pan class="hljs-title function_">on</span>(<span class="hljs-string">'connection'</span>, <span cl…
17392 …socket.<span class="hljs-title function_">end</span>(<span class="hljs-string">'handled by child'<…
17407 …de class="language-js"><span class="hljs-keyword">const</span> { fork } = <span class="hljs-built_…
17408 …js-keyword">const</span> normal = <span class="hljs-title function_">fork</span>(<span class="hljs
17409 …js-keyword">const</span> special = <span class="hljs-title function_">fork</span>(<span class="hlj…
17411 <span class="hljs-comment">// Open up the server and send sockets to child. Use pauseOnConnect to p…
17412 <span class="hljs-comment">// the sockets from being read before they are sent to the child process…
17413-keyword">const</span> server = <span class="hljs-built_in">require</span>(<span class="hljs-strin…
17414 …pan class="hljs-title function_">on</span>(<span class="hljs-string">'connection'</span>, <span cl…
17416 <span class="hljs-comment">// If this is special priority...</span>
17417 …<span class="hljs-keyword">if</span> (socket.<span class="hljs-property">remoteAddress</span> === …
17418 …special.<span class="hljs-title function_">send</span>(<span class="hljs-string">'socket'</span>, …
17419 <span class="hljs-keyword">return</span>;
17421 <span class="hljs-comment">// This is normal priority.</span>
17422 …normal.<span class="hljs-title function_">send</span>(<span class="hljs-string">'socket'</span>, s…
17424 server.<span class="hljs-title function_">listen</span>(<span class="hljs-number">1337</span>);</co…
17427 …ge-js">process.<span class="hljs-title function_">on</span>(<span class="hljs-string">'message'</s…
17428 <span class="hljs-keyword">if</span> (m === <span class="hljs-string">'socket'</span>) {
17429 <span class="hljs-keyword">if</span> (socket) {
17430 <span class="hljs-comment">// Check that the client socket exists.</span>
17431 …<span class="hljs-comment">// It is possible for the socket to be closed between the time it is</s…
17432 <span class="hljs-comment">// sent and the time it is received in the child process.</span>
17433 …ass="hljs-title function_">end</span>(<span class="hljs-string">`Request handled with <span class=…
17444 …zilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | …
17450 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array…
17452 <p>The <code>subprocess.spawnargs</code> property represents the full list of command-line
17456 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" c…
17501 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array…
17511 …ode class="language-js"><span class="hljs-keyword">const</span> assert = <span class="hljs-built_i…
17512 <span class="hljs-keyword">const</span> fs = <span class="hljs-built_in">require</span>(<span class…
17513 <span class="hljs-keyword">const</span> child_process = <span class="hljs-built_in">require</span>(…
17515 <span class="hljs-keyword">const</span> subprocess = child_process.<span class="hljs-title function…
17516 <span class="hljs-attr">stdio</span>: [
17517 …<span class="hljs-number">0</span>, <span class="hljs-comment">// Use parent's stdin for child.</s…
17518 …<span class="hljs-string">'pipe'</span>, <span class="hljs-comment">// Pipe child's stdout to pare…
17519 …ass="hljs-title function_">openSync</span>(<span class="hljs-string">'err.out'</span>, <span class…
17523 …hljs-title function_">strictEqual</span>(subprocess.<span class="hljs-property">stdio</span>[<span…
17524-title function_">strictEqual</span>(subprocess.<span class="hljs-property">stdio</span>[<span cla…
17526 <span class="hljs-title function_">assert</span>(subprocess.<span class="hljs-property">stdout</spa…
17527-title function_">strictEqual</span>(subprocess.<span class="hljs-property">stdio</span>[<span cla…
17529 …hljs-title function_">strictEqual</span>(subprocess.<span class="hljs-property">stdio</span>[<span…
17530-title function_">strictEqual</span>(subprocess.<span class="hljs-property">stdio</span>[<span cla…
17545 …e class="language-js"><span class="hljs-keyword">const</span> { spawn } = <span class="hljs-built_…
17547 <span class="hljs-keyword">const</span> subprocess = <span class="hljs-title function_">spawn</span…
17549-property">stdout</span>.<span class="hljs-title function_">on</span>(<span class="hljs-string">'d…
17550 …"hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class…
17564 …e class="language-js"><span class="hljs-keyword">const</span> { spawn } = <span class="hljs-built_…
17566-keyword">const</span> subprocess = <span class="hljs-title function_">spawn</span>(process.<span …
17567 <span class="hljs-attr">detached</span>: <span class="hljs-literal">true</span>,
17568 <span class="hljs-attr">stdio</span>: <span class="hljs-string">'ignore'</span>
17571 subprocess.<span class="hljs-title function_">unref</span>();</code></pre>
17575 This impacts output that includes multibyte character encodings such as UTF-8 or
17576 UTF-16. For instance, <code>console.log('中文测试')</code> will send 13 UTF-8 encoded bytes
17579 <p>The shell should understand the <code>-c</code> switch. If the shell is <code>'cmd.exe'</code>, …
17580 should understand the <code>/d /s /c</code> switches and command-line parsing should be
17594 <a href="https://developer.mozilla.org/en-US/docs/Web/API/Web_Workers_API/Structured_clone_algorith…
17595 supports more built-in JavaScript object types, such as <code>BigInt</code>, <code>Map</code>
17598 objects of such built-in types will not be passed on through the serialization
17607 …ty api_stability_2"><a href="#documentation_stability_index">Stability: 2</a> - Stable</div><p></p>
17610 multi-core systems, the user will sometimes want to launch a cluster of Node.js
17614 …de class="language-js"><span class="hljs-keyword">const</span> cluster = <span class="hljs-built_i…
17615 <span class="hljs-keyword">const</span> http = <span class="hljs-built_in">require</span>(<span cla…
17616-keyword">const</span> numCPUs = <span class="hljs-built_in">require</span>(<span class="hljs-stri…
17618 <span class="hljs-keyword">if</span> (cluster.<span class="hljs-property">isMaster</span>) {
17619 …ass="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span c…
17621 <span class="hljs-comment">// Fork workers.</span>
17622 …<span class="hljs-keyword">for</span> (<span class="hljs-keyword">let</span> i = <span class="hljs
17623 cluster.<span class="hljs-title function_">fork</span>();
17626 ….<span class="hljs-title function_">on</span>(<span class="hljs-string">'exit'</span>, <span class…
17627 …ass="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span c…
17629 } <span class="hljs-keyword">else</span> {
17630 <span class="hljs-comment">// Workers can share any TCP connection</span>
17631 <span class="hljs-comment">// In this case it is an HTTP server</span>
17632 …http.<span class="hljs-title function_">createServer</span>(<span class="hljs-function">(<span cla…
17633 res.<span class="hljs-title function_">writeHead</span>(<span class="hljs-number">200</span>);
17634 …res.<span class="hljs-title function_">end</span>(<span class="hljs-string">'hello world\n'</span>…
17635 }).<span class="hljs-title function_">listen</span>(<span class="hljs-number">8000</span>);
17637 …ass="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span c…
17640 <pre><code class="language-console"><span class="hljs-meta">$ </span><span class="language-bash">no…
17655 is the round-robin approach, where the master process listens on a
17657 in a round-robin fashion, with some built-in smarts to avoid
17685 application such that it does not rely too heavily on in-memory data objects for
17688 re-spawned depending on a program's needs, without affecting other
17711-js">cluster.<span class="hljs-title function_">fork</span>().<span class="hljs-title function_">o…
17712 <span class="hljs-comment">// Worker has disconnected</span>
17725 <li><code>code</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
17726 <li><code>signal</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struc…
17730 <pre><code class="language-js"><span class="hljs-keyword">const</span> worker = cluster.<span class…
17731 ….<span class="hljs-title function_">on</span>(<span class="hljs-string">'exit'</span>, <span class…
17732 <span class="hljs-keyword">if</span> (signal) {
17733-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hlj…
17734 …} <span class="hljs-keyword">else</span> <span class="hljs-keyword">if</span> (code !== <span clas…
17735-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hlj…
17736 } <span class="hljs-keyword">else</span> {
17737 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>…
17745 <li><code>address</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference…
17748-js">cluster.<span class="hljs-title function_">fork</span>().<span class="hljs-title function_">o…
17749 <span class="hljs-comment">// Worker is listening</span>
17757 <li><code>message</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference…
17758 …la.org/en-US/docs/Web/JavaScript/Data_structures#Undefined_type" class="type">&#x3C;undefined></a>…
17765 …de class="language-js"><span class="hljs-keyword">const</span> cluster = <span class="hljs-built_i…
17766 <span class="hljs-keyword">const</span> http = <span class="hljs-built_in">require</span>(<span cla…
17768 <span class="hljs-keyword">if</span> (cluster.<span class="hljs-property">isMaster</span>) {
17770 <span class="hljs-comment">// Keep track of http requests</span>
17771 <span class="hljs-keyword">let</span> numReqs = <span class="hljs-number">0</span>;
17772 <span class="hljs-built_in">setInterval</span>(<span class="hljs-function">() =></span> {
17773 …ss="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span cl…
17774 }, <span class="hljs-number">1000</span>);
17776 <span class="hljs-comment">// Count requests</span>
17777 …<span class="hljs-keyword">function</span> <span class="hljs-title function_">messageHandler</span…
17778 …"hljs-keyword">if</span> (msg.<span class="hljs-property">cmd</span> &#x26;&#x26; msg.<span class=…
17779 numReqs += <span class="hljs-number">1</span>;
17783 …<span class="hljs-comment">// Start workers and listen for messages containing notifyRequest</span>
17784-keyword">const</span> numCPUs = <span class="hljs-built_in">require</span>(<span class="hljs-stri…
17785 …<span class="hljs-keyword">for</span> (<span class="hljs-keyword">let</span> i = <span class="hljs
17786 cluster.<span class="hljs-title function_">fork</span>();
17789 … class="hljs-keyword">for</span> (<span class="hljs-keyword">const</span> id <span class="hljs-key…
17790 …cluster.<span class="hljs-property">workers</span>[id].<span class="hljs-title function_">on</span…
17793 } <span class="hljs-keyword">else</span> {
17795 <span class="hljs-comment">// Worker processes have a http server.</span>
17796 …http.<span class="hljs-title class_">Server</span>(<span class="hljs-function">(<span class="hljs-
17797 res.<span class="hljs-title function_">writeHead</span>(<span class="hljs-number">200</span>);
17798 …res.<span class="hljs-title function_">end</span>(<span class="hljs-string">'hello world\n'</span>…
17800 <span class="hljs-comment">// Notify master about the request</span>
17801 …process.<span class="hljs-title function_">send</span>({ <span class="hljs-attr">cmd</span>: <span…
17802 }).<span class="hljs-title function_">listen</span>(<span class="hljs-number">8000</span>);
17809-js">cluster.<span class="hljs-title function_">fork</span>().<span class="hljs-title function_">o…
17810 <span class="hljs-comment">// Worker is online</span>
17847 <pre><code class="language-js"><span class="hljs-keyword">if</span> (cluster.<span class="hljs-prop…
17848 …<span class="hljs-keyword">const</span> worker = cluster.<span class="hljs-title function_">fork</…
17849 <span class="hljs-keyword">let</span> timeout;
17851 …span class="hljs-title function_">on</span>(<span class="hljs-string">'listening'</span>, <span cl…
17852 …worker.<span class="hljs-title function_">send</span>(<span class="hljs-string">'shutdown'</span>);
17853 worker.<span class="hljs-title function_">disconnect</span>();
17854 … timeout = <span class="hljs-built_in">setTimeout</span>(<span class="hljs-function">() =></span> {
17855 worker.<span class="hljs-title function_">kill</span>();
17856 }, <span class="hljs-number">2000</span>);
17859 …worker.<span class="hljs-title function_">on</span>(<span class="hljs-string">'disconnect'</span>,…
17860 <span class="hljs-built_in">clearTimeout</span>(timeout);
17863 } <span class="hljs-keyword">else</span> <span class="hljs-keyword">if</span> (cluster.<span class=…
17864 …<span class="hljs-keyword">const</span> net = <span class="hljs-built_in">require</span>(<span cla…
17865 …s="hljs-keyword">const</span> server = net.<span class="hljs-title function_">createServer</span>(…
17866 <span class="hljs-comment">// Connections never end</span>
17869 server.<span class="hljs-title function_">listen</span>(<span class="hljs-number">8000</span>);
17871 …<span class="hljs-title function_">on</span>(<span class="hljs-string">'message'</span>, <span cla…
17872 <span class="hljs-keyword">if</span> (msg === <span class="hljs-string">'shutdown'</span>) {
17873 <span class="hljs-comment">// Initiate graceful close of any connections to server</span>
17882 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" …
17890 …age-js">cluster.<span class="hljs-title function_">on</span>(<span class="hljs-string">'exit'</spa…
17891 …<span class="hljs-keyword">if</span> (worker.<span class="hljs-property">exitedAfterDisconnect</sp…
17892 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>…
17896 <span class="hljs-comment">// kill worker</span>
17897 worker.<span class="hljs-title function_">kill</span>();</code></pre>
17903 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" c…
17922 …de class="language-js"><span class="hljs-keyword">const</span> cluster = <span class="hljs-built_i…
17923 <span class="hljs-keyword">const</span> http = <span class="hljs-built_in">require</span>(<span cla…
17924-keyword">const</span> numCPUs = <span class="hljs-built_in">require</span>(<span class="hljs-stri…
17926 <span class="hljs-keyword">if</span> (cluster.<span class="hljs-property">isMaster</span>) {
17927 …ass="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span c…
17929 <span class="hljs-comment">// Fork workers.</span>
17930 …<span class="hljs-keyword">for</span> (<span class="hljs-keyword">let</span> i = <span class="hljs
17931 cluster.<span class="hljs-title function_">fork</span>();
17934 ….<span class="hljs-title function_">on</span>(<span class="hljs-string">'fork'</span>, <span class…
17935-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hlj…
17938 ….<span class="hljs-title function_">on</span>(<span class="hljs-string">'exit'</span>, <span class…
17939-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hlj…
17941 } <span class="hljs-keyword">else</span> {
17942 …<span class="hljs-comment">// Workers can share any TCP connection. In this case, it is an HTTP se…
17943 …http.<span class="hljs-title function_">createServer</span>(<span class="hljs-function">(<span cla…
17944 res.<span class="hljs-title function_">writeHead</span>(<span class="hljs-number">200</span>);
17945 …res.<span class="hljs-title function_">end</span>(<span class="hljs-string">`Current process\n <sp…
17946 …process.<span class="hljs-title function_">kill</span>(process.<span class="hljs-property">pid</sp…
17947 }).<span class="hljs-title function_">listen</span>(<span class="hljs-number">8000</span>);
17954 <li><code>signal</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struc…
17995 <li><code>message</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference…
17997 <li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference…
18001 <li><code>keepOpen</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_str…
18006 <li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Referenc…
18007 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boole…
18015 <pre><code class="language-js"><span class="hljs-keyword">if</span> (cluster.<span class="hljs-prop…
18016 …<span class="hljs-keyword">const</span> worker = cluster.<span class="hljs-title function_">fork</…
18017 …worker.<span class="hljs-title function_">send</span>(<span class="hljs-string">'hi there'</span>);
18019 } <span class="hljs-keyword">else</span> <span class="hljs-keyword">if</span> (cluster.<span class=…
18020 …<span class="hljs-title function_">on</span>(<span class="hljs-string">'message'</span>, <span cla…
18021 process.<span class="hljs-title function_">send</span>(msg);
18036 are long-living connections.</p>
18037-js">cluster.<span class="hljs-title function_">on</span>(<span class="hljs-string">'disconnect'</…
18038 …s="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span cla…
18046 <li><code>code</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
18047 <li><code>signal</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struc…
18052 …age-js">cluster.<span class="hljs-title function_">on</span>(<span class="hljs-string">'exit'</spa…
18053 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>…
18054 …worker.<span class="hljs-property">process</span>.<span class="hljs-property">pid</span>, signal |…
18055 cluster.<span class="hljs-title function_">fork</span>();
18067 <pre><code class="language-js"><span class="hljs-keyword">const</span> timeouts = [];
18068 <span class="hljs-keyword">function</span> <span class="hljs-title function_">errorMsg</span>(<span…
18069 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">error</spa…
18072 ….<span class="hljs-title function_">on</span>(<span class="hljs-string">'fork'</span>, <span class…
18073 …ts[worker.<span class="hljs-property">id</span>] = <span class="hljs-built_in">setTimeout</span>(e…
18075 …span class="hljs-title function_">on</span>(<span class="hljs-string">'listening'</span>, <span cl…
18076 …<span class="hljs-built_in">clearTimeout</span>(timeouts[worker.<span class="hljs-property">id</sp…
18078 ….<span class="hljs-title function_">on</span>(<span class="hljs-string">'exit'</span>, <span class…
18079 …<span class="hljs-built_in">clearTimeout</span>(timeouts[worker.<span class="hljs-property">id</sp…
18080 <span class="hljs-title function_">errorMsg</span>();
18088 <li><code>address</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference…
18097 …e-js">cluster.<span class="hljs-title function_">on</span>(<span class="hljs-string">'listening'</…
18098 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(
18099 …<span class="hljs-string">`A worker is now connected to <span class="hljs-subst">${address.address…
18105 <li><code>-1</code> (Unix domain socket)</li>
18122 <li><code>message</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference…
18123 …la.org/en-US/docs/Web/JavaScript/Data_structures#Undefined_type" class="type">&#x3C;undefined></a>…
18138 …ge-js">cluster.<span class="hljs-title function_">on</span>(<span class="hljs-string">'online'</sp…
18139 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>…
18146 <li><code>settings</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Referenc…
18158 <li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Referenc…
18172 <li><code>env</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Glo…
18182 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" …
18192 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" …
18199 <p>The scheduling policy, either <code>cluster.SCHED_RR</code> for round-robin or
18230 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Objec…
18232 <li><code>execArgv</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_str…
18234 <li><code>exec</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
18235 <li><code>args</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
18237 <li><code>cwd</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structur…
18239 <li><code>serialization</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Dat…
18243 <li><code>silent</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struc…
18245 <li><code>stdio</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/G…
18248-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;number></a> Sets the user …
18249-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;number></a> Sets the group…
18250 …zilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;number></a> | …
18254 <li><code>windowsHide</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_…
18275 <li><code>settings</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Referenc…
18285 …de class="language-js"><span class="hljs-keyword">const</span> cluster = <span class="hljs-built_i…
18286 cluster.<span class="hljs-title function_">setupMaster</span>({
18287 <span class="hljs-attr">exec</span>: <span class="hljs-string">'worker.js'</span>,
18288 …<span class="hljs-attr">args</span>: [<span class="hljs-string">'--use'</span>, <span class="hljs-
18289 <span class="hljs-attr">silent</span>: <span class="hljs-literal">true</span>
18291 cluster.<span class="hljs-title function_">fork</span>(); <span class="hljs-comment">// https worke…
18292 cluster.<span class="hljs-title function_">setupMaster</span>({
18293 <span class="hljs-attr">exec</span>: <span class="hljs-string">'worker.js'</span>,
18294 …<span class="hljs-attr">args</span>: [<span class="hljs-string">'--use'</span>, <span class="hljs-
18296 cluster.<span class="hljs-title function_">fork</span>(); <span class="hljs-comment">// http worker…
18303 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Objec…
18306 …de class="language-js"><span class="hljs-keyword">const</span> cluster = <span class="hljs-built_i…
18308 <span class="hljs-keyword">if</span> (cluster.<span class="hljs-property">isMaster</span>) {
18309 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>…
18310 cluster.<span class="hljs-title function_">fork</span>();
18311 cluster.<span class="hljs-title function_">fork</span>();
18312 } <span class="hljs-keyword">else</span> <span class="hljs-keyword">if</span> (cluster.<span class=…
18313 …="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span clas…
18320 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Objec…
18329 <pre><code class="language-js"><span class="hljs-comment">// Go through all workers</span>
18330 <span class="hljs-keyword">function</span> <span class="hljs-title function_">eachWorker</span>(<sp…
18331 … class="hljs-keyword">for</span> (<span class="hljs-keyword">const</span> id <span class="hljs-key…
18332 …<span class="hljs-title function_">callback</span>(cluster.<span class="hljs-property">workers</sp…
18335 <span class="hljs-title function_">eachWorker</span>(<span class="hljs-function">(<span class="hljs
18336 …worker.<span class="hljs-title function_">send</span>(<span class="hljs-string">'big announcement …
18339 …uage-js">socket.<span class="hljs-title function_">on</span>(<span class="hljs-string">'data'</spa…
18340 …<span class="hljs-keyword">const</span> worker = cluster.<span class="hljs-property">workers</span…
18343 <section><h2>Command-line options<span><a class="mark" href="#cli_command_line_options" id="cli_com…
18346 <p>Node.js comes with a variety of CLI options. These options expose built-in
18350 <p><code>node [options] [V8 options] [script.js | -e "script" | -] [--] [arguments]</code></p>
18351 <p><code>node inspect [script.js | -e "script" | &#x3C;host>:&#x3C;port>] …</code></p>
18352 <p><code>node --v8-options</code></p>
18366 dashes (<code>-</code>) or underscores (<code>_</code>). For example, <code>--pending-deprecation</…
18367 equivalent to <code>--pending_deprecation</code>.</p>
18368 <p>If an option that takes a single value (such as <code>--max-http-header-size</code>) is
18372 <h4><code>-</code><span><a class="mark" href="#cli" id="cli">#</a></span></h4>
18376 <p>Alias for stdin. Analogous to the use of <code>-</code> in other command-line utilities,
18379 <h4><code>--</code><span><a class="mark" href="#cli_1" id="cli_1">#</a></span></h4>
18386 <h4><code>--abort-on-uncaught-exception</code><span><a class="mark" href="#cli_abort_on_uncaught_ex…
18390 <p>Aborting instead of exiting causes a core file to be generated for post-mortem
18395 <h4><code>--completion-bash</code><span><a class="mark" href="#cli_completion_bash" id="cli_complet…
18399 <p>Print source-able bash completion script for Node.js.</p>
18400 <pre><code class="language-console"><span class="hljs-meta">$ </span><span class="language-bash">no…
18401 <span class="hljs-meta">$ </span><span class="language-bash"><span class="hljs-built_in">source</sp…
18402 <h4><code>-C=condition</code>, <code>--conditions=condition</code><span><a class="mark" href="#cli_…
18406 … api_stability_1"><a href="#documentation_stability_index">Stability: 1</a> - Experimental</div><p…
18413 <pre><code class="language-console"><span class="hljs-meta">$ </span><span class="language-bash">no…
18414 <h4><code>--cpu-prof</code><span><a class="mark" href="#cli_cpu_prof" id="cli_cpu_prof">#</a></span…
18418 … api_stability_1"><a href="#documentation_stability_index">Stability: 1</a> - Experimental</div><p…
18421 <p>If <code>--cpu-prof-dir</code> is not specified, the generated profile is placed
18423 <p>If <code>--cpu-prof-name</code> is not specified, the generated profile is
18425 <pre><code class="language-console"><span class="hljs-meta">$ </span><span class="language-bash">no…
18426 <span class="hljs-meta">$ </span><span class="language-bash">ls *.cpuprofile</span>
18428 <h4><code>--cpu-prof-dir</code><span><a class="mark" href="#cli_cpu_prof_dir" id="cli_cpu_prof_dir"…
18432 … api_stability_1"><a href="#documentation_stability_index">Stability: 1</a> - Experimental</div><p…
18433 <p>Specify the directory where the CPU profiles generated by <code>--cpu-prof</code> will
18436 <a href="#cli_diagnostic_dir_directory">--diagnostic-dir</a> command-line option.</p>
18437 <h4><code>--cpu-prof-interval</code><span><a class="mark" href="#cli_cpu_prof_interval" id="cli_cpu…
18441 … api_stability_1"><a href="#documentation_stability_index">Stability: 1</a> - Experimental</div><p…
18443 by <code>--cpu-prof</code>. The default is 1000 microseconds.</p>
18444 <h4><code>--cpu-prof-name</code><span><a class="mark" href="#cli_cpu_prof_name" id="cli_cpu_prof_na…
18448 … api_stability_1"><a href="#documentation_stability_index">Stability: 1</a> - Experimental</div><p…
18449 <p>Specify the file name of the CPU profile generated by <code>--cpu-prof</code>.</p>
18450 <h4><code>--diagnostic-dir=directory</code><span><a class="mark" href="#cli_diagnostic_dir_director…
18455 <li><a href="#cli_cpu_prof_dir">--cpu-prof-dir</a></li>
18456 <li><a href="#cli_heap_prof_dir">--heap-prof-dir</a></li>
18457 <li><a href="#cli_redirect_warnings_file">--redirect-warnings</a></li>
18459 <h4><code>--disable-proto=mode</code><span><a class="mark" href="#cli_disable_proto_mode" id="cli_d…
18466 <h4><code>--disallow-code-generation-from-strings</code><span><a class="mark" href="#cli_disallow_c…
18470 <p>Make built-in language features like <code>eval</code> and <code>new Function</code> that genera…
18473 <h4><code>--dns-result-order=order</code><span><a class="mark" href="#cli_dns_result_order_order" i…
18484 priority than <code>--dns-result-order</code>.</p>
18485 <h4><code>--enable-fips</code><span><a class="mark" href="#cli_enable_fips" id="cli_enable_fips">#<…
18489 <p>Enable FIPS-compliant crypto at startup. (Requires Node.js to be built
18490 against FIPS-compatible OpenSSL.)</p>
18491 <h4><code>--enable-source-maps</code><span><a class="mark" href="#cli_enable_source_maps" id="cli_e…
18506 <code>--enable-source-maps</code> enables caching of Source Maps and makes a best
18508 <p>Overriding <code>Error.prepareStackTrace</code> prevents <code>--enable-source-maps</code> from
18510 <h4><code>--experimental-abortcontroller</code><span><a class="mark" href="#cli_experimental_abortc…
18515 <h4><code>--experimental-import-meta-resolve</code><span><a class="mark" href="#cli_experimental_im…
18520 <h4><code>--experimental-json-modules</code><span><a class="mark" href="#cli_experimental_json_modu…
18525 <h4><code>--experimental-loader=module</code><span><a class="mark" href="#cli_experimental_loader_m…
18531 <h4><code>--experimental-modules</code><span><a class="mark" href="#cli_experimental_modules" id="c…
18536 <h4><code>--experimental-policy</code><span><a class="mark" href="#cli_experimental_policy" id="cli…
18541 <h4><code>--experimental-repl-await</code><span><a class="mark" href="#cli_experimental_repl_await"…
18545 <p>Enable experimental top-level <code>await</code> keyword support in REPL.</p>
18546 <h4><code>--experimental-specifier-resolution=mode</code><span><a class="mark" href="#cli_experimen…
18556 <h4><code>--experimental-vm-modules</code><span><a class="mark" href="#cli_experimental_vm_modules"…
18561 <h4><code>--experimental-wasi-unstable-preview1</code><span><a class="mark" href="#cli_experimental…
18567 <td><p>changed from <code>--experimental-wasi-unstable-preview0</code> to <code>--experimental-wasi
18574 <h4><code>--experimental-wasm-modules</code><span><a class="mark" href="#cli_experimental_wasm_modu…
18579 <h4><code>--force-context-aware</code><span><a class="mark" href="#cli_force_context_aware" id="cli…
18583 <p>Disable loading native addons that are not <a href="#addons_context_aware_addons">context-aware<…
18584 <h4><code>--force-fips</code><span><a class="mark" href="#cli_force_fips" id="cli_force_fips">#</a>…
18588 <p>Force FIPS-compliant crypto on startup. (Cannot be disabled from script code.)
18589 (Same requirements as <code>--enable-fips</code>.)</p>
18590 <h4><code>--frozen-intrinsics</code><span><a class="mark" href="#cli_frozen_intrinsics" id="cli_fro…
18594 … api_stability_1"><a href="#documentation_stability_index">Stability: 1</a> - Experimental</div><p…
18599 <p><code>--require</code> runs prior to freezing intrinsics in order to allow polyfills to
18601 <h4><code>--heapsnapshot-near-heap-limit=max_count</code><span><a class="mark" href="#cli_heapsnaps…
18605 … api_stability_1"><a href="#documentation_stability_index">Stability: 1</a> - Experimental</div><p…
18607 heap limit. <code>count</code> should be a non-negative integer (in which case
18624 …ass="language-console"><span class="hljs-meta">$ </span><span class="language-bash">node --max-old
18629 &#x3C;--- Last few GCs --->
18631 [49580:0x110000000] 4826 ms: Mark-sweep 130.6 (147.8) -> 130.5 (147.8) MB, 27.4 / 0.0 ms (aver…
18632 [49580:0x110000000] 4845 ms: Mark-sweep 130.6 (147.8) -> 130.6 (147.8) MB, 18.8 / 0.0 ms (aver…
18635 &#x3C;--- JS stacktrace --->
18637 FATAL ERROR: Ineffective mark-compacts near heap limit Allocation failed - JavaScript heap out of m…
18639 <h4><code>--heapsnapshot-signal=signal</code><span><a class="mark" href="#cli_heapsnapshot_signal_s…
18646 <pre><code class="language-console"><span class="hljs-meta">$ </span><span class="language-bash">no…
18647 <span class="hljs-meta">$ </span><span class="language-bash">ps aux</span>
18649 node 1 5.5 6.1 787252 247004 ? Ssl 16:43 0:02 node --heapsnapshot-signal=SIGUSR2…
18650 <span class="hljs-meta">$ </span><span class="language-bash"><span class="hljs-built_in">kill</span…
18651 <span class="hljs-meta">$ </span><span class="language-bash">ls</span>
18653 <h4><code>--heap-prof</code><span><a class="mark" href="#cli_heap_prof" id="cli_heap_prof">#</a></s…
18657 … api_stability_1"><a href="#documentation_stability_index">Stability: 1</a> - Experimental</div><p…
18660 <p>If <code>--heap-prof-dir</code> is not specified, the generated profile is placed
18662 <p>If <code>--heap-prof-name</code> is not specified, the generated profile is
18664 <pre><code class="language-console"><span class="hljs-meta">$ </span><span class="language-bash">no…
18665 <span class="hljs-meta">$ </span><span class="language-bash">ls *.heapprofile</span>
18667 <h4><code>--heap-prof-dir</code><span><a class="mark" href="#cli_heap_prof_dir" id="cli_heap_prof_d…
18671 … api_stability_1"><a href="#documentation_stability_index">Stability: 1</a> - Experimental</div><p…
18672 <p>Specify the directory where the heap profiles generated by <code>--heap-prof</code> will
18675 <a href="#cli_diagnostic_dir_directory">--diagnostic-dir</a> command-line option.</p>
18676 <h4><code>--heap-prof-interval</code><span><a class="mark" href="#cli_heap_prof_interval" id="cli_h…
18680 … api_stability_1"><a href="#documentation_stability_index">Stability: 1</a> - Experimental</div><p…
18682 by <code>--heap-prof</code>. The default is 512 * 1024 bytes.</p>
18683 <h4><code>--heap-prof-name</code><span><a class="mark" href="#cli_heap_prof_name" id="cli_heap_prof…
18687 … api_stability_1"><a href="#documentation_stability_index">Stability: 1</a> - Experimental</div><p…
18688 <p>Specify the file name of the heap profile generated by <code>--heap-prof</code>.</p>
18689 <h4><code>--icu-data-dir=file</code><span><a class="mark" href="#cli_icu_data_dir_file" id="cli_icu…
18694 <h4><code>--input-type=type</code><span><a class="mark" href="#cli_input_type_type" id="cli_input_t…
18699 module. String input is input via <code>--eval</code>, <code>--print</code>, or <code>STDIN</code>.…
18701 <h4><code>--inspect-brk[=[host:]port]</code><span><a class="mark" href="#cli_inspect_brk_host_port"…
18707 <h4><code>--inspect-port=[host:]port</code><span><a class="mark" href="#cli_inspect_port_host_port"…
18716 <h4><code>--inspect[=[host:]port]</code><span><a class="mark" href="#cli_inspect_host_port" id="cli…
18723 tcp port and communicate using the <a href="https://chromedevtools.github.io/devtools-protocol/">Ch…
18734 <p><strong>More specifically, <code>--inspect=0.0.0.0</code> is insecure if the port (<code>9229</c…
18735 default) is not firewall-protected.</strong></p>
18736 <p>See the <a href="https://nodejs.org/en/docs/guides/debugging-getting-started/#security-implicati…
18737 <h4><code>--inspect-publish-uid=stderr,http</code><span><a class="mark" href="#cli_inspect_publish_…
18741 <h4><code>--insecure-http-parser</code><span><a class="mark" href="#cli_insecure_http_parser" id="c…
18746 interoperability with non-conformant HTTP implementations. It may also allow
18749 <h4><code>--jitless</code><span><a class="mark" href="#cli_jitless" id="cli_jitless">#</a></span></…
18757 disappear in a non-semver-major release.</p>
18758 <h4><code>--max-http-header-size=size</code><span><a class="mark" href="#cli_max_http_header_size_s…
18771 <h4><code>--napi-modules</code><span><a class="mark" href="#cli_napi_modules" id="cli_napi_modules"…
18775 <p>This option is a no-op. It is kept for compatibility.</p>
18776 <h4><code>--no-addons</code><span><a class="mark" href="#cli_no_addons" id="cli_no_addons">#</a></s…
18780 <p>Disable the <code>node-addons</code> exports condition as well as disable loading
18781 native addons. When <code>--no-addons</code> is specified, calling <code>process.dlopen</code> or
18783 <h4><code>--no-deprecation</code><span><a class="mark" href="#cli_no_deprecation" id="cli_no_deprec…
18788 <h4><code>--no-force-async-hooks-checks</code><span><a class="mark" href="#cli_no_force_async_hooks…
18794 <h4><code>--no-warnings</code><span><a class="mark" href="#cli_no_warnings" id="cli_no_warnings">#<…
18799 <h4><code>--node-memory-debug</code><span><a class="mark" href="#cli_node_memory_debug" id="cli_nod…
18805 <h4><code>--openssl-config=file</code><span><a class="mark" href="#cli_openssl_config_file" id="cli…
18810 used to enable FIPS-compliant crypto if Node.js is built
18811 against FIPS-enabled OpenSSL.</p>
18812 <h4><code>--pending-deprecation</code><span><a class="mark" href="#cli_pending_deprecation" id="cli…
18819 unless either the <code>--pending-deprecation</code> command-line flag, or the
18823 <h4><code>--policy-integrity=sri</code><span><a class="mark" href="#cli_policy_integrity_sri" id="c…
18827 … api_stability_1"><a href="#documentation_stability_index">Stability: 1</a> - Experimental</div><p…
18829 the specified integrity. It expects a <a href="https://developer.mozilla.org/en-US/docs/Web/Securit…
18831 <h4><code>--preserve-symlinks</code><span><a class="mark" href="#cli_preserve_symlinks" id="cli_pre…
18838 to a different on-disk location, Node.js will dereference the link and use the
18839 actual on-disk "real path" of the module as both an identifier and as a root
18844 <pre><code class="language-text">{appDir}
18848 │ ├── moduleA -> {appDir}/moduleA
18855 <p>The <code>--preserve-symlinks</code> command-line flag instructs Node.js to use the
18858 <p>Note, however, that using <code>--preserve-symlinks</code> can have other side effects.
18863 <p>The <code>--preserve-symlinks</code> flag does not apply to the main module, which allows
18864 <code>node --preserve-symlinks node_module/.bin/&#x3C;foo></code> to work. To apply the same
18865 behavior for the main module, also use <code>--preserve-symlinks-main</code>.</p>
18866 <h4><code>--preserve-symlinks-main</code><span><a class="mark" href="#cli_preserve_symlinks_main" i…
18872 <p>This flag exists so that the main module can be opted-in to the same behavior
18873 that <code>--preserve-symlinks</code> gives to all other imports; they are separate flags,
18875 <p><code>--preserve-symlinks-main</code> does not imply <code>--preserve-symlinks</code>; use
18876 <code>--preserve-symlinks-main</code> in addition to
18877 <code>--preserve-symlinks</code> when it is not desirable to follow symlinks before
18879 <p>See <code>--preserve-symlinks</code> for more information.</p>
18880 <h4><code>--prof</code><span><a class="mark" href="#cli_prof" id="cli_prof">#</a></span></h4>
18885 <h4><code>--prof-process</code><span><a class="mark" href="#cli_prof_process" id="cli_prof_process"…
18889 <p>Process V8 profiler output generated using the V8 option <code>--prof</code>.</p>
18890 <h4><code>--redirect-warnings=file</code><span><a class="mark" href="#cli_redirect_warnings_file" i…
18900 <a href="#cli_diagnostic_dir_directory">--diagnostic-dir</a> command-line option.</p>
18901 <h4><code>--report-compact</code><span><a class="mark" href="#cli_report_compact" id="cli_report_co…
18905 <p>Write reports in a compact format, single-line JSON, more easily consumable
18906 by log processing systems than the default multi-line format designed for
18908 <h4><code>--report-dir=directory</code>, <code>report-directory=directory</code><span><a class="mar…
18916 <td><p>Changed from <code>--diagnostic-report-directory</code> to <code>--report-directory</code>.<…
18923 <h4><code>--report-filename=filename</code><span><a class="mark" href="#cli_report_filename_filenam…
18931 <td><p>changed from <code>--diagnostic-report-filename</code> to <code>--report-filename</code>.</p…
18938 <h4><code>--report-on-fatalerror</code><span><a class="mark" href="#cli_report_on_fatalerror" id="c…
18946 <td><p>changed from <code>--diagnostic-report-on-fatalerror</code> to <code>--report-on-fatalerror<…
18957 <h4><code>--report-on-signal</code><span><a class="mark" href="#cli_report_on_signal" id="cli_repor…
18965 <td><p>changed from <code>--diagnostic-report-on-signal</code> to <code>--report-on-signal</code>.<…
18973 specified through <code>--report-signal</code>.</p>
18974 <h4><code>--report-signal=signal</code><span><a class="mark" href="#cli_report_signal_signal" id="c…
18982 <td><p>changed from <code>--diagnostic-report-signal</code> to <code>--report-signal</code>.</p></t…
18990 <h4><code>--report-uncaught-exception</code><span><a class="mark" href="#cli_report_uncaught_except…
18998 <td><p>changed from <code>--diagnostic-report-uncaught-exception</code> to <code>--report-uncaught-
19007 <h4><code>--throw-deprecation</code><span><a class="mark" href="#cli_throw_deprecation" id="cli_thr…
19012 <h4><code>--title=title</code><span><a class="mark" href="#cli_title_title" id="cli_title_title">#<…
19017 <h4><code>--tls-cipher-list=list</code><span><a class="mark" href="#cli_tls_cipher_list_list" id="c…
19023 <h4><code>--tls-keylog=file</code><span><a class="mark" href="#cli_tls_keylog_file" id="cli_tls_key…
19030 <h4><code>--tls-max-v1.2</code><span><a class="mark" href="#cli_tls_max_v1_2" id="cli_tls_max_v1_2"…
19036 <h4><code>--tls-max-v1.3</code><span><a class="mark" href="#cli_tls_max_v1_3" id="cli_tls_max_v1_3"…
19042 <h4><code>--tls-min-v1.0</code><span><a class="mark" href="#cli_tls_min_v1_0" id="cli_tls_min_v1_0"…
19048 <h4><code>--tls-min-v1.1</code><span><a class="mark" href="#cli_tls_min_v1_1" id="cli_tls_min_v1_1"…
19054 <h4><code>--tls-min-v1.2</code><span><a class="mark" href="#cli_tls_min_v1_2" id="cli_tls_min_v1_2"…
19061 <h4><code>--tls-min-v1.3</code><span><a class="mark" href="#cli_tls_min_v1_3" id="cli_tls_min_v1_3"…
19067 <h4><code>--trace-atomics-wait</code><span><a class="mark" href="#cli_trace_atomics_wait" id="cli_t…
19071 <p>Print short summaries of calls to <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScri…
19073 <pre><code class="language-text">(node:15701) [Thread 0] Atomics.wait(&#x26;lt;address> + 0, 1, inf…
19078 (node:15701) [Thread 1] Atomics.wait(&#x26;lt;address> + 4, -1, inf) started
19080 (node:15701) [Thread 1] Atomics.wait(&#x26;lt;address> + 4, -1, inf) was woken up by another thread…
19089 <h4><code>--trace-deprecation</code><span><a class="mark" href="#cli_trace_deprecation" id="cli_tra…
19094 <h4><code>--trace-event-categories</code><span><a class="mark" href="#cli_trace_event_categories" i…
19099 tracing is enabled using <code>--trace-events-enabled</code>.</p>
19100 <h4><code>--trace-event-file-pattern</code><span><a class="mark" href="#cli_trace_event_file_patter…
19106 <h4><code>--trace-events-enabled</code><span><a class="mark" href="#cli_trace_events_enabled" id="c…
19111 <h4><code>--trace-exit</code><span><a class="mark" href="#cli_trace_exit" id="cli_trace_exit">#</a>…
19117 <h4><code>--trace-sigint</code><span><a class="mark" href="#cli_trace_sigint" id="cli_trace_sigint"…
19122 <h4><code>--trace-sync-io</code><span><a class="mark" href="#cli_trace_sync_io" id="cli_trace_sync_…
19128 <h4><code>--trace-tls</code><span><a class="mark" href="#cli_trace_tls" id="cli_trace_tls">#</a></s…
19134 <h4><code>--trace-uncaught</code><span><a class="mark" href="#cli_trace_uncaught" id="cli_trace_unc…
19143 <h4><code>--trace-warnings</code><span><a class="mark" href="#cli_trace_warnings" id="cli_trace_war…
19148 <h4><code>--track-heap-objects</code><span><a class="mark" href="#cli_track_heap_objects" id="cli_t…
19153 <h4><code>--unhandled-rejections=mode</code><span><a class="mark" href="#cli_unhandled_rejections_m…
19168 <li><code>warn-with-error-code</code>: Emit <a href="#process_event_unhandledrejection"><code>unhan…
19172 <h4><code>--use-bundled-ca</code>, <code>--use-openssl-ca</code><span><a class="mark" href="#cli_us…
19178 at build-time.</p>
19187 <h4><code>--use-largepages=mode</code><span><a class="mark" href="#cli_use_largepages_mode" id="cli…
19191 <p>Re-map the Node.js static code to large memory pages at startup. If supported on
19192 the target system, this will cause the Node.js static code to be moved onto 2
19202 <h4><code>--v8-options</code><span><a class="mark" href="#cli_v8_options" id="cli_v8_options">#</a>…
19206 <p>Print V8 command-line options.</p>
19207 <h4><code>--v8-pool-size=num</code><span><a class="mark" href="#cli_v8_pool_size_num" id="cli_v8_po…
19216 <h4><code>--zero-fill-buffers</code><span><a class="mark" href="#cli_zero_fill_buffers" id="cli_zer…
19220 <p>Automatically zero-fills all newly allocated <a href="#buffer_class_buffer"><code>Buffer</code><…
19222 <h4><code>-c</code>, <code>--check</code><span><a class="mark" href="#cli_c_check" id="cli_c_check"…
19228 <td><p>The <code>--require</code> option is now supported when checking a file.</p></td></tr>
19235 <h4><code>-e</code>, <code>--eval "script"</code><span><a class="mark" href="#cli_e_eval_script" id…
19241 <td><p>Built-in libraries are now available as predefined variables.</p></td></tr>
19252 <h4><code>-h</code>, <code>--help</code><span><a class="mark" href="#cli_h_help" id="cli_h_help">#<…
19256 <p>Print node command-line options.
19258 <h4><code>-i</code>, <code>--interactive</code><span><a class="mark" href="#cli_i_interactive" id="…
19263 <h4><code>-p</code>, <code>--print "script"</code><span><a class="mark" href="#cli_p_print_script" …
19269 <td><p>Built-in libraries are now available as predefined variables.</p></td></tr>
19275 <p>Identical to <code>-e</code> but prints the result.</p>
19276 <h4><code>-r</code>, <code>--require module</code><span><a class="mark" href="#cli_r_require_module…
19284 ES6 Module using <code>--require</code> will fail with an error.</p>
19285 <h4><code>-v</code>, <code>--version</code><span><a class="mark" href="#cli_v_version" id="cli_v_ve…
19295 <li><code>1</code>, <code>true</code>, or the empty string <code>''</code> indicate 16-color suppor…
19296 <li><code>2</code> to indicate 256-color support, or</li>
19297 <li><code>3</code> to indicate 16 million-color support.</li>
19306 <p><code>','</code>-separated list of core modules that should print debug information.</p>
19308 <p><code>','</code>-separated list of core C++ modules that should print debug information.</p>
19334 <p>Data path for ICU (<code>Intl</code> object) data. Will extend linked-in data when compiled
19335 with small-icu support.</p>
19345 <p>A space-separated list of command-line options. <code>options...</code> are interpreted
19346 before command-line options, so command-line options will override or
19348 an option that is not allowed in the environment is used, such as <code>-p</code> or a
19351 <pre><code class="language-bash">NODE_OPTIONS=<span class="hljs-string">'--require "./my path/file.…
19352 <p>A singleton flag passed as a command-line option will override the same flag
19354 <pre><code class="language-bash"><span class="hljs-comment"># The inspector will be available on po…
19355 NODE_OPTIONS=<span class="hljs-string">'--inspect=localhost:4444'</span> node --inspect=localhost:5…
19357 <code>NODE_OPTIONS</code> instances were passed first, and then its command-line
19359 …e><code class="language-bash">NODE_OPTIONS=<span class="hljs-string">'--require "./a.js"'</span> n…
19360 <span class="hljs-comment"># is equivalent to:</span>
19361 node --require <span class="hljs-string">"./a.js"</span> --require <span class="hljs-string">"./b.j…
19363 <!-- node-options-node start -->
19365 <li><code>--conditions</code>, <code>-C</code></li>
19366 <li><code>--diagnostic-dir</code></li>
19367 <li><code>--disable-proto</code></li>
19368 <li><code>--dns-result-order</code></li>
19369 <li><code>--enable-fips</code></li>
19370 <li><code>--enable-source-maps</code></li>
19371 <li><code>--experimental-abortcontroller</code></li>
19372 <li><code>--experimental-import-meta-resolve</code></li>
19373 <li><code>--experimental-json-modules</code></li>
19374 <li><code>--experimental-loader</code></li>
19375 <li><code>--experimental-modules</code></li>
19376 <li><code>--experimental-policy</code></li>
19377 <li><code>--experimental-repl-await</code></li>
19378 <li><code>--experimental-specifier-resolution</code></li>
19379 <li><code>--experimental-top-level-await</code></li>
19380 <li><code>--experimental-vm-modules</code></li>
19381 <li><code>--experimental-wasi-unstable-preview1</code></li>
19382 <li><code>--experimental-wasm-modules</code></li>
19383 <li><code>--force-context-aware</code></li>
19384 <li><code>--force-fips</code></li>
19385 <li><code>--frozen-intrinsics</code></li>
19386 <li><code>--heapsnapshot-near-heap-limit</code></li>
19387 <li><code>--heapsnapshot-signal</code></li>
19388 <li><code>--http-parser</code></li>
19389 <li><code>--icu-data-dir</code></li>
19390 <li><code>--input-type</code></li>
19391 <li><code>--insecure-http-parser</code></li>
19392 <li><code>--inspect-brk</code></li>
19393 <li><code>--inspect-port</code>, <code>--debug-port</code></li>
19394 <li><code>--inspect-publish-uid</code></li>
19395 <li><code>--inspect</code></li>
19396 <li><code>--max-http-header-size</code></li>
19397 <li><code>--napi-modules</code></li>
19398 <li><code>--no-addons</code></li>
19399 <li><code>--no-deprecation</code></li>
19400 <li><code>--no-force-async-hooks-checks</code></li>
19401 <li><code>--no-warnings</code></li>
19402 <li><code>--node-memory-debug</code></li>
19403 <li><code>--openssl-config</code></li>
19404 <li><code>--pending-deprecation</code></li>
19405 <li><code>--policy-integrity</code></li>
19406 <li><code>--preserve-symlinks-main</code></li>
19407 <li><code>--preserve-symlinks</code></li>
19408 <li><code>--prof-process</code></li>
19409 <li><code>--redirect-warnings</code></li>
19410 <li><code>--report-compact</code></li>
19411 <li><code>--report-dir</code>, <code>--report-directory</code></li>
19412 <li><code>--report-filename</code></li>
19413 <li><code>--report-on-fatalerror</code></li>
19414 <li><code>--report-on-signal</code></li>
19415 <li><code>--report-signal</code></li>
19416 <li><code>--report-uncaught-exception</code></li>
19417 <li><code>--require</code>, <code>-r</code></li>
19418 <li><code>--throw-deprecation</code></li>
19419 <li><code>--title</code></li>
19420 <li><code>--tls-cipher-list</code></li>
19421 <li><code>--tls-keylog</code></li>
19422 <li><code>--tls-max-v1.2</code></li>
19423 <li><code>--tls-max-v1.3</code></li>
19424 <li><code>--tls-min-v1.0</code></li>
19425 <li><code>--tls-min-v1.1</code></li>
19426 <li><code>--tls-min-v1.2</code></li>
19427 <li><code>--tls-min-v1.3</code></li>
19428 <li><code>--trace-atomics-wait</code></li>
19429 <li><code>--trace-deprecation</code></li>
19430 <li><code>--trace-event-categories</code></li>
19431 <li><code>--trace-event-file-pattern</code></li>
19432 <li><code>--trace-events-enabled</code></li>
19433 <li><code>--trace-exit</code></li>
19434 <li><code>--trace-sigint</code></li>
19435 <li><code>--trace-sync-io</code></li>
19436 <li><code>--trace-tls</code></li>
19437 <li><code>--trace-uncaught</code></li>
19438 <li><code>--trace-warnings</code></li>
19439 <li><code>--track-heap-objects</code></li>
19440 <li><code>--unhandled-rejections</code></li>
19441 <li><code>--use-bundled-ca</code></li>
19442 <li><code>--use-largepages</code></li>
19443 <li><code>--use-openssl-ca</code></li>
19444 <li><code>--v8-pool-size</code></li>
19445 <li><code>--zero-fill-buffers</code></li>
19447 <!-- node-options-node end -->
19449 <!-- node-options-v8 start -->
19451 <li><code>--abort-on-uncaught-exception</code></li>
19452 <li><code>--disallow-code-generation-from-strings</code></li>
19453 <li><code>--huge-max-old-generation-size</code></li>
19454 <li><code>--interpreted-frames-native-stack</code></li>
19455 <li><code>--jitless</code></li>
19456 <li><code>--max-old-space-size</code></li>
19457 <li><code>--perf-basic-prof-only-functions</code></li>
19458 <li><code>--perf-basic-prof</code></li>
19459 <li><code>--perf-prof-unwinding-info</code></li>
19460 <li><code>--perf-prof</code></li>
19461 <li><code>--stack-trace-limit</code></li>
19463 <!-- node-options-v8 end -->
19464 <p><code>--perf-basic-prof-only-functions</code>, <code>--perf-basic-prof</code>,
19465 <code>--perf-prof-unwinding-info</code>, and <code>--perf-prof</code> are only available on Linux.<…
19470 <p><code>':'</code>-separated list of directories prefixed to the module search path.</p>
19471 <p>On Windows, this is a <code>';'</code>-separated list instead.</p>
19479 unless either the <code>--pending-deprecation</code> command-line flag, or the
19500 equivalent to using the <code>--redirect-warnings=file</code> command-line flag.</p>
19512 <p>Path to a Node.js module which will be loaded in place of the built-in REPL.
19513 Overriding this value to an empty string (<code>''</code>) will use the built-in REPL.</p>
19526 …gin outputting <a href="https://v8project.blogspot.com/2017/12/javascript-code-coverage.html">V8 J…
19534 …//chromedevtools.github.io/devtools-protocol/tot/Profiler#type-ScriptCoverage">ScriptCoverage</a> …
19536 <pre><code class="language-json"><span class="hljs-punctuation">{</span>
19537 …<span class="hljs-attr">"result"</span><span class="hljs-punctuation">:</span> <span class="hljs-p…
19538 <span class="hljs-punctuation">{</span>
19539 …<span class="hljs-attr">"scriptId"</span><span class="hljs-punctuation">:</span> <span class="hljs
19540 …n class="hljs-attr">"url"</span><span class="hljs-punctuation">:</span> <span class="hljs-string">…
19541 …pan class="hljs-attr">"functions"</span><span class="hljs-punctuation">:</span> <span class="hljs-
19542 <span class="hljs-punctuation">}</span>
19543 <span class="hljs-punctuation">]</span>
19544 <span class="hljs-punctuation">}</span></code></pre>
19546 … api_stability_1"><a href="#documentation_stability_index">Stability: 1</a> - Experimental</div><p…
19547 <p>If found, source map data is appended to the top-level key <code>source-map-cache</code>
19549 <p><code>source-map-cache</code> is an object with keys representing the files source maps
19550 were extracted from, and values which include the raw source-map URL
19553 <pre><code class="language-json"><span class="hljs-punctuation">{</span>
19554 …<span class="hljs-attr">"result"</span><span class="hljs-punctuation">:</span> <span class="hljs-p…
19555 <span class="hljs-punctuation">{</span>
19556 …<span class="hljs-attr">"scriptId"</span><span class="hljs-punctuation">:</span> <span class="hljs
19557 …ljs-attr">"url"</span><span class="hljs-punctuation">:</span> <span class="hljs-string">"file:///a…
19558 …pan class="hljs-attr">"functions"</span><span class="hljs-punctuation">:</span> <span class="hljs-
19559 <span class="hljs-punctuation">}</span>
19560 <span class="hljs-punctuation">]</span><span class="hljs-punctuation">,</span>
19561 …<span class="hljs-attr">"source-map-cache"</span><span class="hljs-punctuation">:</span> <span cla…
19562 …<span class="hljs-attr">"file:///absolute/path/to/source.js"</span><span class="hljs-punctuation">…
19563 …class="hljs-attr">"url"</span><span class="hljs-punctuation">:</span> <span class="hljs-string">".…
19564 …<span class="hljs-attr">"data"</span><span class="hljs-punctuation">:</span> <span class="hljs-pun…
19565 …<span class="hljs-attr">"version"</span><span class="hljs-punctuation">:</span> <span class="hljs-
19566 …<span class="hljs-attr">"sources"</span><span class="hljs-punctuation">:</span> <span class="hljs-
19567 <span class="hljs-string">"file:///absolute/path/to/original.js"</span>
19568 <span class="hljs-punctuation">]</span><span class="hljs-punctuation">,</span>
19569 …<span class="hljs-attr">"names"</span><span class="hljs-punctuation">:</span> <span class="hljs-pu…
19570 <span class="hljs-string">"Foo"</span><span class="hljs-punctuation">,</span>
19571 <span class="hljs-string">"console"</span><span class="hljs-punctuation">,</span>
19572 <span class="hljs-string">"info"</span>
19573 <span class="hljs-punctuation">]</span><span class="hljs-punctuation">,</span>
19574 …ass="hljs-attr">"mappings"</span><span class="hljs-punctuation">:</span> <span class="hljs-string"…
19575 …<span class="hljs-attr">"sourceRoot"</span><span class="hljs-punctuation">:</span> <span class="hl…
19576 <span class="hljs-punctuation">}</span><span class="hljs-punctuation">,</span>
19577 …<span class="hljs-attr">"lineLengths"</span><span class="hljs-punctuation">:</span> <span class="h…
19578 <span class="hljs-number">13</span><span class="hljs-punctuation">,</span>
19579 <span class="hljs-number">62</span><span class="hljs-punctuation">,</span>
19580 <span class="hljs-number">38</span><span class="hljs-punctuation">,</span>
19581 <span class="hljs-number">27</span>
19582 <span class="hljs-punctuation">]</span>
19583 <span class="hljs-punctuation">}</span>
19584 <span class="hljs-punctuation">}</span>
19585 <span class="hljs-punctuation">}</span></code></pre>
19587 <p><a href="https://no-color.org"><code>NO_COLOR</code></a> is an alias for <code>NODE_DISABLE_COL…
19594 used to enable FIPS-compliant crypto if Node.js is built with <code>./configure --openssl-fips</cod…
19595 <p>If the <a href="#cli_openssl_config_file"><code>--openssl-config</code></a> command-line option …
19601 <p>If <code>--use-openssl-ca</code> is enabled, this overrides and sets OpenSSL's directory
19610 <p>If <code>--use-openssl-ca</code> is enabled, this overrides and sets OpenSSL's file
19644 <h4><code>--max-old-space-size=SIZE</code> (in megabytes)<span><a class="mark" href="#cli_max_old_s…
19650 <pre><code class="language-console"><span class="hljs-meta">$ </span><span class="language-bash">no…
19654 …ty api_stability_2"><a href="#documentation_stability_index">Stability: 2</a> - Stable</div><p></p>
19671 …class="language-js"><span class="hljs-variable language_">console</span>.<span class="hljs-title f…
19672 <span class="hljs-comment">// Prints: hello world, to stdout</span>
19673 …hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class=…
19674 <span class="hljs-comment">// Prints: hello world, to stdout</span>
19675-variable language_">console</span>.<span class="hljs-title function_">error</span>(<span class="h…
19676 <span class="hljs-comment">// Prints error message and stack trace to stderr:</span>
19677 <span class="hljs-comment">// Error: Whoops, something bad happened</span>
19678 <span class="hljs-comment">// at [eval]:5:15</span>
19679 <span class="hljs-comment">// at Script.runInThisContext (node:vm:132:18)</span>
19680 <span class="hljs-comment">// at Object.runInThisContext (node:vm:309:38)</span>
19681 <span class="hljs-comment">// at node:internal/process/execution:77:19</span>
19682 <span class="hljs-comment">// at [eval]-wrapper:6:22</span>
19683 <span class="hljs-comment">// at evalScript (node:internal/process/execution:76:60)</span>
19684 <span class="hljs-comment">// at node:internal/main/eval_string:23:3</span>
19686 <span class="hljs-keyword">const</span> name = <span class="hljs-string">'Will Robinson'</span>;
19687 …ass="hljs-variable language_">console</span>.<span class="hljs-title function_">warn</span>(<span …
19688 <span class="hljs-comment">// Prints: Danger Will Robinson! Danger!, to stderr</span></code></pre>
19690 <pre><code class="language-js"><span class="hljs-keyword">const</span> out = <span class="hljs-titl…
19691 <span class="hljs-keyword">const</span> err = <span class="hljs-title function_">getStreamSomehow</…
19692 …hljs-keyword">const</span> myConsole = <span class="hljs-keyword">new</span> <span class="hljs-var…
19694 myConsole.<span class="hljs-title function_">log</span>(<span class="hljs-string">'hello world'</sp…
19695 <span class="hljs-comment">// Prints: hello world, to out</span>
19696 myConsole.<span class="hljs-title function_">log</span>(<span class="hljs-string">'hello %s'</span>…
19697 <span class="hljs-comment">// Prints: hello world, to out</span>
19698 …lass="hljs-title function_">error</span>(<span class="hljs-keyword">new</span> <span class="hljs-t…
19699 <span class="hljs-comment">// Prints: [Error: Whoops, something bad happened], to err</span>
19701 <span class="hljs-keyword">const</span> name = <span class="hljs-string">'Will Robinson'</span>;
19702 myConsole.<span class="hljs-title function_">warn</span>(<span class="hljs-string">`Danger <span cl…
19703 <span class="hljs-comment">// Prints: Danger Will Robinson! Danger!, to err</span></code></pre>
19718-js"><span class="hljs-keyword">const</span> { <span class="hljs-title class_">Console</span> } = …
19719 …de class="language-js"><span class="hljs-keyword">const</span> { <span class="hljs-title class_">C…
19738 <li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference…
19742 <li><code>ignoreErrors</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data…
19744 …illa.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type">&#x3C;boolean></a> |…
19751 <li><code>inspectOptions</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Re…
19753 <li><code>groupIndentation</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/…
19761 …"language-js"><span class="hljs-keyword">const</span> output = fs.<span class="hljs-title function…
19762 <span class="hljs-keyword">const</span> errorOutput = fs.<span class="hljs-title function_">createW…
19763 <span class="hljs-comment">// Custom simple logger</span>
19764-keyword">const</span> logger = <span class="hljs-keyword">new</span> <span class="hljs-title clas…
19765 <span class="hljs-comment">// use it like console</span>
19766 <span class="hljs-keyword">const</span> count = <span class="hljs-number">5</span>;
19767 logger.<span class="hljs-title function_">log</span>(<span class="hljs-string">'count: %d'</span>, …
19768 <span class="hljs-comment">// In stdout.log: count 5</span></code></pre>
19771-js"><span class="hljs-keyword">new</span> <span class="hljs-title class_">Console</span>({ <span …
19785 <li><code>value</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struct…
19786 <li><code>...message</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_s…
19788 …a message if <code>value</code> is <a href="https://developer.mozilla.org/en-US/docs/Glossary/Fals…
19792 <p>If <code>value</code> is <a href="https://developer.mozilla.org/en-US/docs/Glossary/Truthy">trut…
19793-js"><span class="hljs-variable language_">console</span>.<span class="hljs-title function_">asser…
19795-variable language_">console</span>.<span class="hljs-title function_">assert</span>(<span class="…
19796 <span class="hljs-comment">// Assertion failed: Whoops didn't work</span>
19798 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">assert</spa…
19799 <span class="hljs-comment">// Assertion failed</span></code></pre>
19816 <li><code>label</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struct…
19820 <!-- eslint-skip -->
19821 <pre><code class="language-js">> <span class="hljs-variable language_">console</span>.<span class="…
19822 <span class="hljs-attr">default</span>: <span class="hljs-number">1</span>
19823 <span class="hljs-literal">undefined</span>
19824 > <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">count</sp…
19825 <span class="hljs-attr">default</span>: <span class="hljs-number">2</span>
19826 <span class="hljs-literal">undefined</span>
19827 > <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">count</sp…
19828 <span class="hljs-attr">abc</span>: <span class="hljs-number">1</span>
19829 <span class="hljs-literal">undefined</span>
19830 > <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">count</sp…
19831 <span class="hljs-attr">xyz</span>: <span class="hljs-number">1</span>
19832 <span class="hljs-literal">undefined</span>
19833 > <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">count</sp…
19834 <span class="hljs-attr">abc</span>: <span class="hljs-number">2</span>
19835 <span class="hljs-literal">undefined</span>
19836 > <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">count</sp…
19837 <span class="hljs-attr">default</span>: <span class="hljs-number">3</span>
19838 <span class="hljs-literal">undefined</span>
19845 <li><code>label</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struct…
19848 <!-- eslint-skip -->
19849 …ass="language-js">> <span class="hljs-variable language_">console</span>.<span class="hljs-title f…
19850 <span class="hljs-attr">abc</span>: <span class="hljs-number">1</span>
19851 <span class="hljs-literal">undefined</span>
19852 > <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">countRese…
19853 <span class="hljs-literal">undefined</span>
19854 > <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">count</sp…
19855 <span class="hljs-attr">abc</span>: <span class="hljs-number">1</span>
19856 <span class="hljs-literal">undefined</span>
19871 <li><code>data</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
19872 <li><code>...args</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_stru…
19880 <li><code>obj</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structur…
19881 <li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference…
19883 …zilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type">&#x3C;boolean></a> …
19885 <li><code>depth</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struct…
19888 <li><code>colors</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struc…
19909 <li><code>...data</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_stru…
19918 <li><code>data</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
19919 <li><code>...args</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_stru…
19923 values similar to <a href="http://man7.org/linux/man-pages/man3/printf.3.html"><code>printf(3)</cod…
19925 <pre><code class="language-js"><span class="hljs-keyword">const</span> code = <span class="hljs-num…
19926 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">error</span…
19927 <span class="hljs-comment">// Prints: error #5, to stderr</span>
19928 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">error</span…
19929 <span class="hljs-comment">// Prints: error 5, to stderr</span></code></pre>
19938 <li><code>...label</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_str…
19960 <li><code>data</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
19961 <li><code>...args</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_stru…
19969 <li><code>data</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
19970 <li><code>...args</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_stru…
19974 values similar to <a href="http://man7.org/linux/man-pages/man3/printf.3.html"><code>printf(3)</cod…
19976 <pre><code class="language-js"><span class="hljs-keyword">const</span> count = <span class="hljs-nu…
19977 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(…
19978 <span class="hljs-comment">// Prints: count: 5, to stdout</span>
19979 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(…
19980 <span class="hljs-comment">// Prints: count: 5, to stdout</span></code></pre>
19987 <li><code>tabularData</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_…
19988 <li><code>properties</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_s…
19993 <pre><code class="language-js"><span class="hljs-comment">// These can't be parsed as tabular data<…
19994 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">table</span…
19995 <span class="hljs-comment">// Symbol()</span>
19997 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">table</span…
19998 <span class="hljs-comment">// undefined</span>
20000-variable language_">console</span>.<span class="hljs-title function_">table</span>([{ <span class…
20001 <span class="hljs-comment">// ┌─────────┬─────┬─────┐</span>
20002 <span class="hljs-comment">// │ (index) │ a │ b │</span>
20003 <span class="hljs-comment">// ├─────────┼─────┼─────┤</span>
20004 <span class="hljs-comment">// │ 0 │ 1 │ 'Y' │</span>
20005 <span class="hljs-comment">// │ 1 │ 'Z' │ 2 │</span>
20006 <span class="hljs-comment">// └─────────┴─────┴─────┘</span>
20008-variable language_">console</span>.<span class="hljs-title function_">table</span>([{ <span class…
20009 <span class="hljs-comment">// ┌─────────┬─────┐</span>
20010 <span class="hljs-comment">// │ (index) │ a │</span>
20011 <span class="hljs-comment">// ├─────────┼─────┤</span>
20012 <span class="hljs-comment">// │ 0 │ 1 │</span>
20013 <span class="hljs-comment">// │ 1 │ 'Z' │</span>
20014 <span class="hljs-comment">// └─────────┴─────┘</span></code></pre>
20020 <li><code>label</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struct…
20042 <li><code>label</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struct…
20046 …language-js"><span class="hljs-variable language_">console</span>.<span class="hljs-title function…
20047 …n class="hljs-keyword">for</span> (<span class="hljs-keyword">let</span> i = <span class="hljs-num…
20048 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">timeEnd</sp…
20049 <span class="hljs-comment">// prints 100-elements: 225.438ms</span></code></pre>
20055 <li><code>label</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struct…
20056 <li><code>...data</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_stru…
20060 …class="language-js"><span class="hljs-variable language_">console</span>.<span class="hljs-title f…
20061 <span class="hljs-keyword">const</span> value = <span class="hljs-title function_">expensiveProcess…
20062 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">timeLog</sp…
20063 <span class="hljs-comment">// Prints "process: 365.227ms 42".</span>
20064 <span class="hljs-title function_">doExpensiveProcess2</span>(value);
20065 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">timeEnd</sp…
20071 <li><code>message</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_stru…
20072 <li><code>...args</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_stru…
20076 …lass="language-js"><span class="hljs-variable language_">console</span>.<span class="hljs-title fu…
20077 <span class="hljs-comment">// Prints: (stack trace will vary based on where trace is called)</span>
20078 <span class="hljs-comment">// Trace: Show me</span>
20079 <span class="hljs-comment">// at repl:2:9</span>
20080 <span class="hljs-comment">// at REPLServer.defaultEval (repl.js:248:27)</span>
20081 <span class="hljs-comment">// at bound (domain.js:287:14)</span>
20082 <span class="hljs-comment">// at REPLServer.runBound [as eval] (domain.js:300:12)</span>
20083 <span class="hljs-comment">// at REPLServer.&#x3C;anonymous> (repl.js:412:12)</span>
20084 <span class="hljs-comment">// at emitOne (events.js:82:20)</span>
20085 <span class="hljs-comment">// at REPLServer.emit (events.js:169:7)</span>
20086 <span class="hljs-comment">// at REPLServer.Interface._onLine (readline.js:210:10)</span>
20087 <span class="hljs-comment">// at REPLServer.Interface._line (readline.js:549:8)</span>
20088 <span class="hljs-comment">// at REPLServer.Interface._ttyWrite (readline.js:826:14)</span></cod…
20094 <li><code>data</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
20095 <li><code>...args</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_stru…
20101 (<code>--inspect</code> flag).</p>
20107 <li><code>label</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struct…
20113 …ass="language-js"><span class="hljs-variable language_">console</span>.<span class="hljs-title fun…
20114 <span class="hljs-comment">// Some code</span>
20115 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">profileEnd<…
20116 <span class="hljs-comment">// Adds the profile 'MyLabel' to the Profiles panel of the inspector.</s…
20122 <li><code>label</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struct…
20135 <li><code>label</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struct…
20144 … api_stability_1"><a href="#documentation_stability_index">Stability: 1</a> - Experimental</div><p…
20154 system-specific installation processes anymore just to have the package
20166 … do that simply run <a href="https://github.com/nodejs/corepack#corepack-enable--name"><code>corep…
20171 <a href="https://github.com/nodejs/corepack#corepack-disable--name"><code>corepack disable</code></…
20184 be easily overriden by running the <a href="https://github.com/nodejs/corepack#corepack-prepare--na…
20186 <pre><code class="language-bash">corepack prepare yarn@x.y.z --activate</code></pre>
20191 <a href="https://github.com/nodejs/corepack#corepack-prepare--nameversion"><code>corepack prepare</…
20194 <p>The <code>prepare</code> command has <a href="https://github.com/nodejs/corepack#utility-command…
20228 <h4>Running <code>npm install -g yarn</code> doesn't work<span><a class="mark" href="#corepack_runn…
20237 <li>Add the <code>--force</code> flag to <code>npm install</code>; this will tell npm that it's fin…
20240 that happen, run <a href="https://github.com/nodejs/corepack#corepack-enable--name"><code>corepack …
20244 …ty api_stability_2"><a href="#documentation_stability_index">Stability: 2</a> - Stable</div><p></p>
20249 …ode class="language-js"><span class="hljs-keyword">const</span> crypto = <span class="hljs-built_i…
20251 <span class="hljs-keyword">const</span> secret = <span class="hljs-string">'abcdefg'</span>;
20252 <span class="hljs-keyword">const</span> hash = crypto.<span class="hljs-title function_">createHmac…
20253 ….<span class="hljs-title function_">update</span>(<span class="hljs-string">'I love cupcakes'</spa…
20254 … .<span class="hljs-title function_">digest</span>(<span class="hljs-string">'hex'</span>);
20255 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(…
20256 <span class="hljs-comment">// Prints:</span>
20257 <span class="hljs-comment">// c0fa1bc00531bd78ef38c628449c5102aeabd49b5dc3a2a516ea6ea959d6658e</s…
20262 <pre><code class="language-js"><span class="hljs-keyword">let</span> crypto;
20263 <span class="hljs-keyword">try</span> {
20264 crypto = <span class="hljs-built_in">require</span>(<span class="hljs-string">'crypto'</span>);
20265 } <span class="hljs-keyword">catch</span> (err) {
20266 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>…
20273 Netscape and was specified formally as part of <a href="https://developer.mozilla.org/en-US/docs/We…
20274 …deprecated since <a href="https://www.w3.org/TR/html52/changes.html#features-removed">HTML 5.2</a>…
20278 …nt. Node.js uses <a href="https://www.openssl.org/docs/man1.1.0/apps/openssl-spkac.html">OpenSSL's…
20284-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | <a href="#bu…
20288-js"><span class="hljs-keyword">const</span> { <span class="hljs-title class_">Certificate</span> …
20289 <span class="hljs-keyword">const</span> spkac = <span class="hljs-title function_">getSpkacSomehow<…
20290 <span class="hljs-keyword">const</span> challenge = <span class="hljs-title class_">Certificate</sp…
20291-variable language_">console</span>.<span class="hljs-title function_">log</span>(challenge.<span …
20292 <span class="hljs-comment">// Prints: the challenge as a UTF8 string</span></code></pre>
20298-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | <a href="#bu…
20299 <li><code>encoding</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_str…
20303-js"><span class="hljs-keyword">const</span> { <span class="hljs-title class_">Certificate</span> …
20304 <span class="hljs-keyword">const</span> spkac = <span class="hljs-title function_">getSpkacSomehow<…
20305 <span class="hljs-keyword">const</span> publicKey = <span class="hljs-title class_">Certificate</sp…
20306 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(…
20307 <span class="hljs-comment">// Prints: the public key as &#x3C;Buffer ...></span></code></pre>
20313 …org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray" class="type">&#x3C;TypedArray><…
20314 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boole…
20317-js"><span class="hljs-keyword">const</span> { <span class="hljs-title class_">Certificate</span> …
20318 <span class="hljs-keyword">const</span> spkac = <span class="hljs-title function_">getSpkacSomehow<…
20319-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hlj…
20320 <span class="hljs-comment">// Prints: true or false</span></code></pre>
20322 … api_stability_0"><a href="#documentation_stability_index">Stability: 0</a> - Deprecated</div><p><…
20328 …ode class="language-js"><span class="hljs-keyword">const</span> crypto = <span class="hljs-built_i…
20330 <span class="hljs-keyword">const</span> cert1 = <span class="hljs-keyword">new</span> crypto.<span …
20331 <span class="hljs-keyword">const</span> cert2 = crypto.<span class="hljs-title class_">Certificate<…
20337-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | <a href="#bu…
20341-js"><span class="hljs-keyword">const</span> cert = <span class="hljs-built_in">require</span>(<sp…
20342 <span class="hljs-keyword">const</span> spkac = <span class="hljs-title function_">getSpkacSomehow<…
20343 <span class="hljs-keyword">const</span> challenge = cert.<span class="hljs-title function_">exportC…
20344-variable language_">console</span>.<span class="hljs-title function_">log</span>(challenge.<span …
20345 <span class="hljs-comment">// Prints: the challenge as a UTF8 string</span></code></pre>
20351-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | <a href="#bu…
20355-js"><span class="hljs-keyword">const</span> cert = <span class="hljs-built_in">require</span>(<sp…
20356 <span class="hljs-keyword">const</span> spkac = <span class="hljs-title function_">getSpkacSomehow<…
20357 <span class="hljs-keyword">const</span> publicKey = cert.<span class="hljs-title function_">exportP…
20358 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(…
20359 <span class="hljs-comment">// Prints: the public key as &#x3C;Buffer ...></span></code></pre>
20365 …org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray" class="type">&#x3C;TypedArray><…
20366 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boole…
20369-js"><span class="hljs-keyword">const</span> cert = <span class="hljs-built_in">require</span>(<sp…
20370 <span class="hljs-keyword">const</span> spkac = <span class="hljs-title function_">getSpkacSomehow<…
20371-variable language_">console</span>.<span class="hljs-title function_">log</span>(cert.<span class…
20372 <span class="hljs-comment">// Prints: true or false</span></code></pre>
20392 …ode class="language-js"><span class="hljs-keyword">const</span> crypto = <span class="hljs-built_i…
20394 <span class="hljs-keyword">const</span> algorithm = <span class="hljs-string">'aes-192-cbc'</span>;
20395 <span class="hljs-keyword">const</span> password = <span class="hljs-string">'Password used to gene…
20396 <span class="hljs-comment">// Key length is dependent on the algorithm. In this case for aes192, it…
20397 <span class="hljs-comment">// 24 bytes (192 bits).</span>
20398 <span class="hljs-comment">// Use async `crypto.scrypt()` instead.</span>
20399-keyword">const</span> key = crypto.<span class="hljs-title function_">scryptSync</span>(password,…
20400 <span class="hljs-comment">// Use `crypto.randomBytes()` to generate a random iv instead of the sta…
20401 <span class="hljs-comment">// shown here.</span>
20402-keyword">const</span> iv = <span class="hljs-title class_">Buffer</span>.<span class="hljs-title …
20404 <span class="hljs-keyword">const</span> cipher = crypto.<span class="hljs-title function_">createCi…
20406 <span class="hljs-keyword">let</span> encrypted = <span class="hljs-string">''</span>;
20407 cipher.<span class="hljs-title function_">on</span>(<span class="hljs-string">'readable'</span>, <s…
20408 <span class="hljs-keyword">let</span> chunk;
20409 …<span class="hljs-keyword">while</span> (<span class="hljs-literal">null</span> !== (chunk = ciphe…
20410 …encrypted += chunk.<span class="hljs-title function_">toString</span>(<span class="hljs-string">'h…
20413 cipher.<span class="hljs-title function_">on</span>(<span class="hljs-string">'end'</span>, <span c…
20414 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>…
20415 …<span class="hljs-comment">// Prints: e5f79c5915c02171eec6b212d5520d44480993d7d622a7c4c2da32f6efda…
20418 cipher.<span class="hljs-title function_">write</span>(<span class="hljs-string">'some clear text d…
20419 cipher.<span class="hljs-title function_">end</span>();</code></pre>
20421 …ode class="language-js"><span class="hljs-keyword">const</span> crypto = <span class="hljs-built_i…
20422 <span class="hljs-keyword">const</span> fs = <span class="hljs-built_in">require</span>(<span class…
20424 <span class="hljs-keyword">const</span> algorithm = <span class="hljs-string">'aes-192-cbc'</span>;
20425 <span class="hljs-keyword">const</span> password = <span class="hljs-string">'Password used to gene…
20426 <span class="hljs-comment">// Use the async `crypto.scrypt()` instead.</span>
20427-keyword">const</span> key = crypto.<span class="hljs-title function_">scryptSync</span>(password,…
20428 <span class="hljs-comment">// Use `crypto.randomBytes()` to generate a random iv instead of the sta…
20429 <span class="hljs-comment">// shown here.</span>
20430-keyword">const</span> iv = <span class="hljs-title class_">Buffer</span>.<span class="hljs-title …
20432 <span class="hljs-keyword">const</span> cipher = crypto.<span class="hljs-title function_">createCi…
20434 <span class="hljs-keyword">const</span> input = fs.<span class="hljs-title function_">createReadStr…
20435 <span class="hljs-keyword">const</span> output = fs.<span class="hljs-title function_">createWriteS…
20437 input.<span class="hljs-title function_">pipe</span>(cipher).<span class="hljs-title function_">pip…
20439 …ode class="language-js"><span class="hljs-keyword">const</span> crypto = <span class="hljs-built_i…
20441 <span class="hljs-keyword">const</span> algorithm = <span class="hljs-string">'aes-192-cbc'</span>;
20442 <span class="hljs-keyword">const</span> password = <span class="hljs-string">'Password used to gene…
20443 <span class="hljs-comment">// Use the async `crypto.scrypt()` instead.</span>
20444-keyword">const</span> key = crypto.<span class="hljs-title function_">scryptSync</span>(password,…
20445 <span class="hljs-comment">// Use `crypto.randomBytes` to generate a random iv instead of the stati…
20446 <span class="hljs-comment">// shown here.</span>
20447-keyword">const</span> iv = <span class="hljs-title class_">Buffer</span>.<span class="hljs-title …
20449 <span class="hljs-keyword">const</span> cipher = crypto.<span class="hljs-title function_">createCi…
20451-keyword">let</span> encrypted = cipher.<span class="hljs-title function_">update</span>(<span cla…
20452 encrypted += cipher.<span class="hljs-title function_">final</span>(<span class="hljs-string">'hex'…
20453 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(…
20454 <span class="hljs-comment">// Prints: e5f79c5915c02171eec6b212d5520d44480993d7d622a7c4c2da32f6efda0…
20460 <li><code>outputEncoding</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Da…
20461 …" class="type">&#x3C;Buffer></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScrip…
20485 …org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray" class="type">&#x3C;TypedArray><…
20486 <li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference…
20488 <li><code>plaintextLength</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/D…
20505 <li><code>autoPadding</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_…
20513 Disabling automatic padding is useful for non-standard padding, for instance
20530-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | <a href="#bu…
20531 <li><code>inputEncoding</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Dat…
20532 <li><code>outputEncoding</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Da…
20533 …" class="type">&#x3C;Buffer></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScrip…
20567 …ode class="language-js"><span class="hljs-keyword">const</span> crypto = <span class="hljs-built_i…
20569 <span class="hljs-keyword">const</span> algorithm = <span class="hljs-string">'aes-192-cbc'</span>;
20570 <span class="hljs-keyword">const</span> password = <span class="hljs-string">'Password used to gene…
20571 <span class="hljs-comment">// Key length is dependent on the algorithm. In this case for aes192, it…
20572 <span class="hljs-comment">// 24 bytes (192 bits).</span>
20573 <span class="hljs-comment">// Use the async `crypto.scrypt()` instead.</span>
20574-keyword">const</span> key = crypto.<span class="hljs-title function_">scryptSync</span>(password,…
20575 <span class="hljs-comment">// The IV is usually passed along with the ciphertext.</span>
20576-keyword">const</span> iv = <span class="hljs-title class_">Buffer</span>.<span class="hljs-title …
20578 <span class="hljs-keyword">const</span> decipher = crypto.<span class="hljs-title function_">create…
20580 <span class="hljs-keyword">let</span> decrypted = <span class="hljs-string">''</span>;
20581 decipher.<span class="hljs-title function_">on</span>(<span class="hljs-string">'readable'</span>, …
20582 …<span class="hljs-keyword">while</span> (<span class="hljs-literal">null</span> !== (chunk = decip…
20583 …decrypted += chunk.<span class="hljs-title function_">toString</span>(<span class="hljs-string">'u…
20586 decipher.<span class="hljs-title function_">on</span>(<span class="hljs-string">'end'</span>, <span…
20587 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>…
20588 <span class="hljs-comment">// Prints: some clear text data</span>
20591 <span class="hljs-comment">// Encrypted with same algorithm, key and iv.</span>
20592 <span class="hljs-keyword">const</span> encrypted =
20593 …<span class="hljs-string">'e5f79c5915c02171eec6b212d5520d44480993d7d622a7c4c2da32f6efda0ffa'</span…
20594 decipher.<span class="hljs-title function_">write</span>(encrypted, <span class="hljs-string">'hex'…
20595 decipher.<span class="hljs-title function_">end</span>();</code></pre>
20597 …ode class="language-js"><span class="hljs-keyword">const</span> crypto = <span class="hljs-built_i…
20598 <span class="hljs-keyword">const</span> fs = <span class="hljs-built_in">require</span>(<span class…
20600 <span class="hljs-keyword">const</span> algorithm = <span class="hljs-string">'aes-192-cbc'</span>;
20601 <span class="hljs-keyword">const</span> password = <span class="hljs-string">'Password used to gene…
20602 <span class="hljs-comment">// Use the async `crypto.scrypt()` instead.</span>
20603-keyword">const</span> key = crypto.<span class="hljs-title function_">scryptSync</span>(password,…
20604 <span class="hljs-comment">// The IV is usually passed along with the ciphertext.</span>
20605-keyword">const</span> iv = <span class="hljs-title class_">Buffer</span>.<span class="hljs-title …
20607 <span class="hljs-keyword">const</span> decipher = crypto.<span class="hljs-title function_">create…
20609 <span class="hljs-keyword">const</span> input = fs.<span class="hljs-title function_">createReadStr…
20610 <span class="hljs-keyword">const</span> output = fs.<span class="hljs-title function_">createWriteS…
20612 input.<span class="hljs-title function_">pipe</span>(decipher).<span class="hljs-title function_">p…
20614 …ode class="language-js"><span class="hljs-keyword">const</span> crypto = <span class="hljs-built_i…
20616 <span class="hljs-keyword">const</span> algorithm = <span class="hljs-string">'aes-192-cbc'</span>;
20617 <span class="hljs-keyword">const</span> password = <span class="hljs-string">'Password used to gene…
20618 <span class="hljs-comment">// Use the async `crypto.scrypt()` instead.</span>
20619-keyword">const</span> key = crypto.<span class="hljs-title function_">scryptSync</span>(password,…
20620 <span class="hljs-comment">// The IV is usually passed along with the ciphertext.</span>
20621-keyword">const</span> iv = <span class="hljs-title class_">Buffer</span>.<span class="hljs-title …
20623 <span class="hljs-keyword">const</span> decipher = crypto.<span class="hljs-title function_">create…
20625 <span class="hljs-comment">// Encrypted using same algorithm, key and iv.</span>
20626 <span class="hljs-keyword">const</span> encrypted =
20627 …<span class="hljs-string">'e5f79c5915c02171eec6b212d5520d44480993d7d622a7c4c2da32f6efda0ffa'</span…
20628-keyword">let</span> decrypted = decipher.<span class="hljs-title function_">update</span>(encrypt…
20629 decrypted += decipher.<span class="hljs-title function_">final</span>(<span class="hljs-string">'ut…
20630 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(…
20631 <span class="hljs-comment">// Prints: some clear text data</span></code></pre>
20637 <li><code>outputEncoding</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Da…
20638 …" class="type">&#x3C;Buffer></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScrip…
20658 …org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray" class="type">&#x3C;TypedArray><…
20659 <li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference…
20661 <li><code>plaintextLength</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/D…
20688 …org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray" class="type">&#x3C;TypedArray><…
20696 …s://nvlpubs.nist.gov/nistpubs/Legacy/SP/nistspecialpublication800-38d.pdf">NIST SP 800-38D</a> or …
20706 <li><code>autoPadding</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_…
20729-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | <a href="#bu…
20730 <li><code>inputEncoding</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Dat…
20731 <li><code>outputEncoding</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Da…
20732 …" class="type">&#x3C;Buffer></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScrip…
20750 <p>The <code>DiffieHellman</code> class is a utility for creating Diffie-Hellman key
20754 …ode class="language-js"><span class="hljs-keyword">const</span> crypto = <span class="hljs-built_i…
20755 <span class="hljs-keyword">const</span> assert = <span class="hljs-built_in">require</span>(<span c…
20757 <span class="hljs-comment">// Generate Alice's keys...</span>
20758 <span class="hljs-keyword">const</span> alice = crypto.<span class="hljs-title function_">createDif…
20759 <span class="hljs-keyword">const</span> aliceKey = alice.<span class="hljs-title function_">generat…
20761 <span class="hljs-comment">// Generate Bob's keys...</span>
20762-keyword">const</span> bob = crypto.<span class="hljs-title function_">createDiffieHellman</span>(…
20763 <span class="hljs-keyword">const</span> bobKey = bob.<span class="hljs-title function_">generateKey…
20765 <span class="hljs-comment">// Exchange and generate the secret...</span>
20766 <span class="hljs-keyword">const</span> aliceSecret = alice.<span class="hljs-title function_">comp…
20767 <span class="hljs-keyword">const</span> bobSecret = bob.<span class="hljs-title function_">computeS…
20769 <span class="hljs-comment">// OK</span>
20770-title function_">strictEqual</span>(aliceSecret.<span class="hljs-title function_">toString</span…
20776-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | <a href="#bu…
20777 <li><code>inputEncoding</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Dat…
20778 <li><code>outputEncoding</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Da…
20779 …" class="type">&#x3C;Buffer></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScrip…
20795 <li><code>encoding</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_str…
20796 …" class="type">&#x3C;Buffer></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScrip…
20798 <p>Generates private and public Diffie-Hellman key values, and returns
20808 <li><code>encoding</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_str…
20809 …" class="type">&#x3C;Buffer></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScrip…
20811 <p>Returns the Diffie-Hellman generator in the specified <code>encoding</code>.
20819 <li><code>encoding</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_str…
20820 …" class="type">&#x3C;Buffer></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScrip…
20822 <p>Returns the Diffie-Hellman prime in the specified <code>encoding</code>.
20830 <li><code>encoding</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_str…
20831 …" class="type">&#x3C;Buffer></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScrip…
20833 <p>Returns the Diffie-Hellman private key in the specified <code>encoding</code>.
20841 <li><code>encoding</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_str…
20842 …" class="type">&#x3C;Buffer></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScrip…
20844 <p>Returns the Diffie-Hellman public key in the specified <code>encoding</code>.
20852-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | <a href="#bu…
20853 <li><code>encoding</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_str…
20855 <p>Sets the Diffie-Hellman private key. If the <code>encoding</code> argument is provided,
20864-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | <a href="#bu…
20865 <li><code>encoding</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_str…
20867 <p>Sets the Diffie-Hellman public key. If the <code>encoding</code> argument is provided,
20889 <p>The <code>DiffieHellmanGroup</code> class takes a well-known modp group as its argument.
20893 <pre><code class="language-js"><span class="hljs-keyword">const</span> name = <span class="hljs-str…
20894 <span class="hljs-keyword">const</span> dh = crypto.<span class="hljs-title function_">createDiffie…
20895 …aken from <a href="https://www.rfc-editor.org/rfc/rfc2412.txt">RFC 2412</a> (modp1 and 2) and <a h…
20896 <pre><code class="language-console"><span class="hljs-meta">$ </span><span class="language-bash">pe…
20909 <p>The <code>ECDH</code> class is a utility for creating Elliptic Curve Diffie-Hellman (ECDH)
20913 …ode class="language-js"><span class="hljs-keyword">const</span> crypto = <span class="hljs-built_i…
20914 <span class="hljs-keyword">const</span> assert = <span class="hljs-built_in">require</span>(<span c…
20916 <span class="hljs-comment">// Generate Alice's keys...</span>
20917 <span class="hljs-keyword">const</span> alice = crypto.<span class="hljs-title function_">createECD…
20918 <span class="hljs-keyword">const</span> aliceKey = alice.<span class="hljs-title function_">generat…
20920 <span class="hljs-comment">// Generate Bob's keys...</span>
20921 <span class="hljs-keyword">const</span> bob = crypto.<span class="hljs-title function_">createECDH<…
20922 <span class="hljs-keyword">const</span> bobKey = bob.<span class="hljs-title function_">generateKey…
20924 <span class="hljs-comment">// Exchange and generate the secret...</span>
20925 <span class="hljs-keyword">const</span> aliceSecret = alice.<span class="hljs-title function_">comp…
20926 <span class="hljs-keyword">const</span> bobSecret = bob.<span class="hljs-title function_">computeS…
20928-title function_">strictEqual</span>(aliceSecret.<span class="hljs-title function_">toString</span…
20929 <span class="hljs-comment">// OK</span></code></pre>
20935-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | <a href="#bu…
20936 <li><code>curve</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struct…
20937 <li><code>inputEncoding</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Dat…
20938 <li><code>outputEncoding</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Da…
20939 <li><code>format</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struc…
20940 …" class="type">&#x3C;Buffer></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScrip…
20942 <p>Converts the EC Diffie-Hellman public key specified by <code>key</code> and <code>curve</code> t…
20948 On recent OpenSSL releases, <code>openssl ecparam -list_curves</code> will also display
20955-js"><span class="hljs-keyword">const</span> { createECDH, <span class="hljs-variable constant_">E…
20957 <span class="hljs-keyword">const</span> ecdh = <span class="hljs-title function_">createECDH</span>…
20958 ecdh.<span class="hljs-title function_">generateKeys</span>();
20960-keyword">const</span> compressedKey = ecdh.<span class="hljs-title function_">getPublicKey</span>…
20962 <span class="hljs-keyword">const</span> uncompressedKey = <span class="hljs-variable constant_">ECD…
20963 <span class="hljs-string">'secp256k1'</span>,
20964 <span class="hljs-string">'hex'</span>,
20965 <span class="hljs-string">'hex'</span>,
20966 <span class="hljs-string">'uncompressed'</span>);
20968 <span class="hljs-comment">// The converted key and the uncompressed public key should be the same<…
20969-variable language_">console</span>.<span class="hljs-title function_">log</span>(uncompressedKey …
20985-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | <a href="#bu…
20986 <li><code>inputEncoding</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Dat…
20987 <li><code>outputEncoding</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Da…
20988 …" class="type">&#x3C;Buffer></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScrip…
21009 <li><code>encoding</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_str…
21010 <li><code>format</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struc…
21011 …" class="type">&#x3C;Buffer></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScrip…
21013 <p>Generates private and public EC Diffie-Hellman key values, and returns
21026 <li><code>encoding</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_str…
21027 …://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C…
21036 <li><code>encoding</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_str…
21037 <li><code>format</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struc…
21038 …://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C…
21051-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | <a href="#bu…
21052 <li><code>encoding</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_str…
21054 <p>Sets the EC Diffie-Hellman private key.
21065 … api_stability_0"><a href="#documentation_stability_index">Stability: 0</a> - Deprecated</div><p><…
21067-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | <a href="#bu…
21068 <li><code>encoding</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_str…
21070 <p>Sets the EC Diffie-Hellman public key.
21080 …ode class="language-js"><span class="hljs-keyword">const</span> crypto = <span class="hljs-built_i…
21081 <span class="hljs-keyword">const</span> alice = crypto.<span class="hljs-title function_">createECD…
21082 <span class="hljs-keyword">const</span> bob = crypto.<span class="hljs-title function_">createECDH<…
21084 <span class="hljs-comment">// This is a shortcut way of specifying one of Alice's previous private<…
21085 <span class="hljs-comment">// keys. It would be unwise to use such a predictable private key in a r…
21086 <span class="hljs-comment">// application.</span>
21087 alice.<span class="hljs-title function_">setPrivateKey</span>(
21088-title function_">createHash</span>(<span class="hljs-string">'sha256'</span>).<span class="hljs-t…
21091 <span class="hljs-comment">// Bob uses a newly generated cryptographically strong</span>
21092 <span class="hljs-comment">// pseudorandom key pair</span>
21093 bob.<span class="hljs-title function_">generateKeys</span>();
21095-keyword">const</span> aliceSecret = alice.<span class="hljs-title function_">computeSecret</span>…
21096-keyword">const</span> bobSecret = bob.<span class="hljs-title function_">computeSecret</span>(ali…
21098 <span class="hljs-comment">// aliceSecret and bobSecret should be the same shared secret value</spa…
21099 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(…
21118 …ode class="language-js"><span class="hljs-keyword">const</span> crypto = <span class="hljs-built_i…
21119 <span class="hljs-keyword">const</span> hash = crypto.<span class="hljs-title function_">createHash…
21121 hash.<span class="hljs-title function_">on</span>(<span class="hljs-string">'readable'</span>, <spa…
21122 <span class="hljs-comment">// Only one element is going to be produced by the</span>
21123 <span class="hljs-comment">// hash stream.</span>
21124 …<span class="hljs-keyword">const</span> data = hash.<span class="hljs-title function_">read</span>…
21125 <span class="hljs-keyword">if</span> (data) {
21126-variable language_">console</span>.<span class="hljs-title function_">log</span>(data.<span class…
21127 <span class="hljs-comment">// Prints:</span>
21128 …<span class="hljs-comment">// 6a2da20943931e9834fc12cfe5bb47bbd9ae43489a30726962b576f4e3993e50</…
21132 hash.<span class="hljs-title function_">write</span>(<span class="hljs-string">'some data to hash'<…
21133 hash.<span class="hljs-title function_">end</span>();</code></pre>
21135 …ode class="language-js"><span class="hljs-keyword">const</span> crypto = <span class="hljs-built_i…
21136 <span class="hljs-keyword">const</span> fs = <span class="hljs-built_in">require</span>(<span class…
21137 <span class="hljs-keyword">const</span> hash = crypto.<span class="hljs-title function_">createHash…
21139 <span class="hljs-keyword">const</span> input = fs.<span class="hljs-title function_">createReadStr…
21140-title function_">pipe</span>(hash).<span class="hljs-title function_">setEncoding</span>(<span cl…
21142 …ode class="language-js"><span class="hljs-keyword">const</span> crypto = <span class="hljs-built_i…
21143 <span class="hljs-keyword">const</span> hash = crypto.<span class="hljs-title function_">createHash…
21145 hash.<span class="hljs-title function_">update</span>(<span class="hljs-string">'some data to hash'…
21146-variable language_">console</span>.<span class="hljs-title function_">log</span>(hash.<span class…
21147 <span class="hljs-comment">// Prints:</span>
21148 <span class="hljs-comment">// 6a2da20943931e9834fc12cfe5bb47bbd9ae43489a30726962b576f4e3993e50</s…
21154 <li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference…
21164 <pre><code class="language-js"><span class="hljs-comment">// Calculate a rolling hash.</span>
21165 <span class="hljs-keyword">const</span> crypto = <span class="hljs-built_in">require</span>(<span c…
21166 <span class="hljs-keyword">const</span> hash = crypto.<span class="hljs-title function_">createHash…
21168 hash.<span class="hljs-title function_">update</span>(<span class="hljs-string">'one'</span>);
21169-variable language_">console</span>.<span class="hljs-title function_">log</span>(hash.<span class…
21171 hash.<span class="hljs-title function_">update</span>(<span class="hljs-string">'two'</span>);
21172-variable language_">console</span>.<span class="hljs-title function_">log</span>(hash.<span class…
21174 hash.<span class="hljs-title function_">update</span>(<span class="hljs-string">'three'</span>);
21175-variable language_">console</span>.<span class="hljs-title function_">log</span>(hash.<span class…
21177 <span class="hljs-comment">// Etc.</span></code></pre>
21183 <li><code>encoding</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_str…
21184 …" class="type">&#x3C;Buffer></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScrip…
21205-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | <a href="#bu…
21206 <li><code>inputEncoding</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Dat…
21232 …ode class="language-js"><span class="hljs-keyword">const</span> crypto = <span class="hljs-built_i…
21233 …s-keyword">const</span> hmac = crypto.<span class="hljs-title function_">createHmac</span>(<span c…
21235 hmac.<span class="hljs-title function_">on</span>(<span class="hljs-string">'readable'</span>, <spa…
21236 <span class="hljs-comment">// Only one element is going to be produced by the</span>
21237 <span class="hljs-comment">// hash stream.</span>
21238 …<span class="hljs-keyword">const</span> data = hmac.<span class="hljs-title function_">read</span>…
21239 <span class="hljs-keyword">if</span> (data) {
21240-variable language_">console</span>.<span class="hljs-title function_">log</span>(data.<span class…
21241 <span class="hljs-comment">// Prints:</span>
21242 …<span class="hljs-comment">// 7fd04df92f636fd450bc841c9418e5825c17f33ad9c87c518115a45971f7f77e</…
21246 hmac.<span class="hljs-title function_">write</span>(<span class="hljs-string">'some data to hash'<…
21247 hmac.<span class="hljs-title function_">end</span>();</code></pre>
21249 …ode class="language-js"><span class="hljs-keyword">const</span> crypto = <span class="hljs-built_i…
21250 <span class="hljs-keyword">const</span> fs = <span class="hljs-built_in">require</span>(<span class…
21251 …s-keyword">const</span> hmac = crypto.<span class="hljs-title function_">createHmac</span>(<span c…
21253 <span class="hljs-keyword">const</span> input = fs.<span class="hljs-title function_">createReadStr…
21254 …nput.<span class="hljs-title function_">pipe</span>(hmac).<span class="hljs-title function_">pipe<…
21256 …ode class="language-js"><span class="hljs-keyword">const</span> crypto = <span class="hljs-built_i…
21257 …s-keyword">const</span> hmac = crypto.<span class="hljs-title function_">createHmac</span>(<span c…
21259 hmac.<span class="hljs-title function_">update</span>(<span class="hljs-string">'some data to hash'…
21260-variable language_">console</span>.<span class="hljs-title function_">log</span>(hmac.<span class…
21261 <span class="hljs-comment">// Prints:</span>
21262 <span class="hljs-comment">// 7fd04df92f636fd450bc841c9418e5825c17f33ad9c87c518115a45971f7f77e</s…
21268 <li><code>encoding</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_str…
21269 …" class="type">&#x3C;Buffer></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScrip…
21289-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | <a href="#bu…
21290 <li><code>inputEncoding</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Dat…
21331 <td><p>Added support for <code>'rsa-pss'</code>.</p></td></tr>
21344 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" c…
21350 <li><code>'rsa-pss'</code> (OID 1.2.840.113549.1.1.10)</li>
21366 <li><code>options</code>: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Referenc…
21367 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Strin…
21375 <li><code>type</code>: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struct…
21376 <li><code>format</code>: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_stru…
21380 <li><code>type</code>: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struct…
21382 <li><code>format</code>: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_stru…
21383 <li><code>cipher</code>: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_stru…
21386 <li><code>passphrase</code>: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_…
21397 encryption mechanism, PEM-level encryption is not supported when encrypting
21398 …. See <a href="https://www.rfc-editor.org/rfc/rfc5208.txt">RFC 5208</a> for PKCS#8 encryption and …
21405 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" c…
21414 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" c…
21438 …ode class="language-js"><span class="hljs-keyword">const</span> crypto = <span class="hljs-built_i…
21440 …s="hljs-keyword">const</span> { privateKey, publicKey } = crypto.<span class="hljs-title function_…
21441 <span class="hljs-attr">namedCurve</span>: <span class="hljs-string">'sect239k1'</span>
21444 <span class="hljs-keyword">const</span> sign = crypto.<span class="hljs-title function_">createSign…
21445 sign.<span class="hljs-title function_">write</span>(<span class="hljs-string">'some data to sign'<…
21446 sign.<span class="hljs-title function_">end</span>();
21447 <span class="hljs-keyword">const</span> signature = sign.<span class="hljs-title function_">sign</s…
21449 <span class="hljs-keyword">const</span> verify = crypto.<span class="hljs-title function_">createVe…
21450 verify.<span class="hljs-title function_">write</span>(<span class="hljs-string">'some data to sign…
21451 verify.<span class="hljs-title function_">end</span>();
21452-variable language_">console</span>.<span class="hljs-title function_">log</span>(verify.<span cla…
21453 <span class="hljs-comment">// Prints: true</span></code></pre>
21455 …ode class="language-js"><span class="hljs-keyword">const</span> crypto = <span class="hljs-built_i…
21457 …s="hljs-keyword">const</span> { privateKey, publicKey } = crypto.<span class="hljs-title function_…
21458 <span class="hljs-attr">modulusLength</span>: <span class="hljs-number">2048</span>,
21461 <span class="hljs-keyword">const</span> sign = crypto.<span class="hljs-title function_">createSign…
21462 sign.<span class="hljs-title function_">update</span>(<span class="hljs-string">'some data to sign'…
21463 sign.<span class="hljs-title function_">end</span>();
21464 <span class="hljs-keyword">const</span> signature = sign.<span class="hljs-title function_">sign</s…
21466 <span class="hljs-keyword">const</span> verify = crypto.<span class="hljs-title function_">createVe…
21467 verify.<span class="hljs-title function_">update</span>(<span class="hljs-string">'some data to sig…
21468 verify.<span class="hljs-title function_">end</span>();
21469 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(…
21470 <span class="hljs-comment">// Prints: true</span></code></pre>
21477 <td><p>This function now supports IEEE-P1363 DSA and ECDSA signatures.</p></td></tr>
21479 <td><p>This function now supports RSA-PSS keys.</p></td></tr>
21483 <td><p>Support for RSASSA-PSS and additional options was added.</p></td></tr>
21490 …lla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type">&#x3C;Object></a> …
21492 <li><code>dsaEncoding</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_…
21493 <li><code>padding</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_stru…
21494 <li><code>saltLength</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_s…
21497 <li><code>outputEncoding</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Da…
21498 …" class="type">&#x3C;Buffer></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScrip…
21507 <p><code>dsaEncoding</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_s…
21510 <li><code>'der'</code> (default): DER-encoded ASN.1 signature structure encoding <code>(r, s)</code…
21511 <li><code>'ieee-p1363'</code>: Signature format <code>r || s</code> as proposed in IEEE-P1363.</li>
21515 <p><code>padding</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struc…
21521 used to sign the message as specified in section 3.1 of <a href="https://www.rfc-editor.org/rfc/rfc…
21523 section 3.3 of <a href="https://www.rfc-editor.org/rfc/rfc4055.txt">RFC 4055</a>.</p>
21526 <p><code>saltLength</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_st…
21550-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | <a href="#bu…
21551 <li><code>inputEncoding</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Dat…
21590-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | <a href="#bu…
21591 <li><code>inputEncoding</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Dat…
21605 <td><p>This function now supports IEEE-P1363 DSA and ECDSA signatures.</p></td></tr>
21607 <td><p>This function now supports RSA-PSS keys.</p></td></tr>
21611 <td><p>Support for RSASSA-PSS and additional options was added.</p></td></tr>
21618 …lla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type">&#x3C;Object></a> …
21620 <li><code>dsaEncoding</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_…
21621 <li><code>padding</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_stru…
21622 <li><code>saltLength</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_s…
21625-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | <a href="#bu…
21626 <li><code>signatureEncoding</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript…
21627 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boole…
21636 <p><code>dsaEncoding</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_s…
21639 <li><code>'der'</code> (default): DER-encoded ASN.1 signature structure encoding <code>(r, s)</code…
21640 <li><code>'ieee-p1363'</code>: Signature format <code>r || s</code> as proposed in IEEE-P1363.</li>
21644 <p><code>padding</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struc…
21650 used to verify the message as specified in section 3.1 of <a href="https://www.rfc-editor.org/rfc/r…
21652 section 3.3 of <a href="https://www.rfc-editor.org/rfc/rfc4055.txt">RFC 4055</a>.</p>
21655 <p><code>saltLength</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_st…
21678 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Obje…
21686 … api_stability_0"><a href="#documentation_stability_index">Stability: 0</a> - Deprecated</div><p><…
21698 … api_stability_0"><a href="#documentation_stability_index">Stability: 0</a> - Deprecated</div><p><…
21719 … api_stability_0"><a href="#documentation_stability_index">Stability: 0</a> - Deprecated: Use <a h…
21721 <li><code>algorithm</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_st…
21722-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | <a href="#bu…
21723 <li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference…
21729 cipher in CCM or OCB mode is used (e.g. <code>'aes-128-ccm'</code>). In that case, the
21735 recent OpenSSL releases, <code>openssl list -cipher-algorithms</code>
21736 (<code>openssl list-cipher-algorithms</code> for older versions of OpenSSL) will
21745 non-cryptographically secure hash algorithm allow passwords to be tested very
21753 …IV is reused in GCM, see <a href="https://github.com/nonce-disrespect/nonce-disrespect">Nonce-Disr…
21763 <td><p>The cipher <code>chacha20-poly1305</code> is now supported.</p></td></tr>
21776 <li><code>algorithm</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_st…
21777-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | <a href="#bu…
21778-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | <a href="#bu…
21779 <li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference…
21785 cipher in CCM or OCB mode is used (e.g. <code>'aes-128-ccm'</code>). In that case, the
21791 recent OpenSSL releases, <code>openssl list -cipher-algorithms</code>
21792 (<code>openssl list-cipher-algorithms</code> for older versions of OpenSSL) will
21819 … api_stability_0"><a href="#documentation_stability_index">Stability: 0</a> - Deprecated: Use <a h…
21821 <li><code>algorithm</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_st…
21822-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | <a href="#bu…
21823 <li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference…
21829 cipher in CCM or OCB mode is used (e.g. <code>'aes-128-ccm'</code>). In that case, the
21836 non-cryptographically secure hash algorithm allow passwords to be tested very
21850 <td><p>The cipher <code>chacha20-poly1305</code> is now supported.</p></td></tr>
21863 <li><code>algorithm</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_st…
21864-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | <a href="#bu…
21865-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | <a href="#bu…
21866 <li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference…
21872 cipher in CCM or OCB mode is used (e.g. <code>'aes-128-ccm'</code>). In that case, the
21878 recent OpenSSL releases, <code>openssl list -cipher-algorithms</code>
21879 (<code>openssl list-cipher-algorithms</code> for older versions of OpenSSL) will
21909-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | <a href="#bu…
21910 <li><code>primeEncoding</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Dat…
21911-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;number></a> | <a href="htt…
21913 <li><code>generatorEncoding</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript…
21929 <li><code>primeLength</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_…
21930 <li><code>generator</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_st…
21941 <li><code>name</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
21950 <li><code>curveName</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_st…
21953 <p>Creates an Elliptic Curve Diffie-Hellman (<code>ECDH</code>) key exchange object using a
21956 OpenSSL releases, <code>openssl ecparam -list_curves</code> will also display the name
21971 <li><code>algorithm</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_st…
21972 <li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference…
21981 On recent releases of OpenSSL, <code>openssl list -digest-algorithms</code>
21982 (<code>openssl list-message-digest-algorithms</code> for older versions of OpenSSL) will
21985 …class="language-js"><span class="hljs-keyword">const</span> filename = process.<span class="hljs-p…
21986 <span class="hljs-keyword">const</span> crypto = <span class="hljs-built_in">require</span>(<span c…
21987 <span class="hljs-keyword">const</span> fs = <span class="hljs-built_in">require</span>(<span class…
21989 <span class="hljs-keyword">const</span> hash = crypto.<span class="hljs-title function_">createHash…
21991 <span class="hljs-keyword">const</span> input = fs.<span class="hljs-title function_">createReadStr…
21992 input.<span class="hljs-title function_">on</span>(<span class="hljs-string">'readable'</span>, <sp…
21993 <span class="hljs-comment">// Only one element is going to be produced by the</span>
21994 <span class="hljs-comment">// hash stream.</span>
21995 …<span class="hljs-keyword">const</span> data = input.<span class="hljs-title function_">read</span…
21996 <span class="hljs-keyword">if</span> (data)
21997 hash.<span class="hljs-title function_">update</span>(data);
21998 <span class="hljs-keyword">else</span> {
21999-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hlj…
22015 <li><code>algorithm</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_st…
22016-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | <a href="#bu…
22017 <li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference…
22024 On recent releases of OpenSSL, <code>openssl list -digest-algorithms</code>
22025 (<code>openssl list-message-digest-algorithms</code> for older versions of OpenSSL) will
22030 …class="language-js"><span class="hljs-keyword">const</span> filename = process.<span class="hljs-p…
22031 <span class="hljs-keyword">const</span> crypto = <span class="hljs-built_in">require</span>(<span c…
22032 <span class="hljs-keyword">const</span> fs = <span class="hljs-built_in">require</span>(<span class…
22034 …s-keyword">const</span> hmac = crypto.<span class="hljs-title function_">createHmac</span>(<span c…
22036 <span class="hljs-keyword">const</span> input = fs.<span class="hljs-title function_">createReadStr…
22037 input.<span class="hljs-title function_">on</span>(<span class="hljs-string">'readable'</span>, <sp…
22038 <span class="hljs-comment">// Only one element is going to be produced by the</span>
22039 <span class="hljs-comment">// hash stream.</span>
22040 …<span class="hljs-keyword">const</span> data = input.<span class="hljs-title function_">read</span…
22041 <span class="hljs-keyword">if</span> (data)
22042 hmac.<span class="hljs-title function_">update</span>(data);
22043 <span class="hljs-keyword">else</span> {
22044-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hlj…
22052 …lla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type">&#x3C;Object></a> …
22054 <li><code>key</code>: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
22055 <li><code>format</code>: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_stru…
22056 <li><code>type</code>: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struct…
22058 <li><code>passphrase</code>: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_…
22083 …lla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type">&#x3C;Object></a> …
22085 <li><code>key</code>: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
22086 <li><code>format</code>: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_stru…
22087 <li><code>type</code>: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struct…
22110 …org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray" class="type">&#x3C;TypedArray><…
22120 <li><code>algorithm</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_st…
22121 <li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference…
22128 algorithm, such as <code>'RSA-SHA256'</code>, instead of a digest algorithm. This will use
22130 algorithms, such as <code>'ecdsa-with-SHA256'</code>, so it is best to always use digest
22137 <li><code>algorithm</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_st…
22138 <li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference…
22146 algorithm, such as <code>'RSA-SHA256'</code>, instead of a digest algorithm. This will use
22148 algorithms, such as <code>'ecdsa-with-SHA256'</code>, so it is best to always use digest
22155 <li><code>options</code>: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Referenc…
22163 <p>Computes the Diffie-Hellman secret based on a <code>privateKey</code> and a <code>publicKey</cod…
22165 (for Diffie-Hellman), <code>'ec'</code> (for ECDH), <code>'x448'</code>, or <code>'x25519'</code> (…
22172 <td><p>Add support for Diffie-Hellman.</p></td></tr>
22185 <li><code>type</code>: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struct…
22187 <li><code>options</code>: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Referenc…
22189 <li><code>modulusLength</code>: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Da…
22190 <li><code>publicExponent</code>: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/D…
22191 <li><code>divisorLength</code>: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Da…
22192 <li><code>namedCurve</code>: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_…
22194 <li><code>primeLength</code>: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data…
22195 <li><code>generator</code>: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_s…
22196 …ttps://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&…
22198 <li><code>publicKeyEncoding</code>: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScrip…
22199 <li><code>privateKeyEncoding</code>: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScri…
22202 <li><code>callback</code>: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Referen…
22204 <li><code>err</code>: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Gl…
22205 <li><code>publicKey</code>: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_s…
22206 <li><code>privateKey</code>: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_…
22216 <code>'pkcs8'</code> with encryption for long-term storage:</p>
22217 …ss="language-js"><span class="hljs-keyword">const</span> { generateKeyPair } = <span class="hljs-b…
22218 <span class="hljs-title function_">generateKeyPair</span>(<span class="hljs-string">'rsa'</span>, {
22219 <span class="hljs-attr">modulusLength</span>: <span class="hljs-number">4096</span>,
22220 <span class="hljs-attr">publicKeyEncoding</span>: {
22221 <span class="hljs-attr">type</span>: <span class="hljs-string">'spki'</span>,
22222 <span class="hljs-attr">format</span>: <span class="hljs-string">'pem'</span>
22224 <span class="hljs-attr">privateKeyEncoding</span>: {
22225 <span class="hljs-attr">type</span>: <span class="hljs-string">'pkcs8'</span>,
22226 <span class="hljs-attr">format</span>: <span class="hljs-string">'pem'</span>,
22227 <span class="hljs-attr">cipher</span>: <span class="hljs-string">'aes-256-cbc'</span>,
22228 <span class="hljs-attr">passphrase</span>: <span class="hljs-string">'top secret'</span>
22230 }, <span class="hljs-function">(<span class="hljs-params">err, publicKey, privateKey</span>) =></sp…
22231 <span class="hljs-comment">// Handle errors and use the generated key pair.</span>
22243 <td><p>Add support for Diffie-Hellman.</p></td></tr>
22254 <li><code>type</code>: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struct…
22256 <li><code>options</code>: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Referenc…
22258 <li><code>modulusLength</code>: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Da…
22259 <li><code>publicExponent</code>: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/D…
22260 <li><code>divisorLength</code>: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Da…
22261 <li><code>namedCurve</code>: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_…
22263 <li><code>primeLength</code>: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data…
22264 <li><code>generator</code>: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_s…
22265 …ttps://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&…
22267 <li><code>publicKeyEncoding</code>: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScrip…
22268 <li><code>privateKeyEncoding</code>: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScri…
22271 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Obje…
22273 <li><code>publicKey</code>: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_s…
22274 <li><code>privateKey</code>: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_…
22286 …="language-js"><span class="hljs-keyword">const</span> { generateKeyPairSync } = <span class="hljs
22287 …class="hljs-keyword">const</span> { publicKey, privateKey } = <span class="hljs-title function_">g…
22288 <span class="hljs-attr">modulusLength</span>: <span class="hljs-number">4096</span>,
22289 <span class="hljs-attr">publicKeyEncoding</span>: {
22290 <span class="hljs-attr">type</span>: <span class="hljs-string">'spki'</span>,
22291 <span class="hljs-attr">format</span>: <span class="hljs-string">'pem'</span>
22293 <span class="hljs-attr">privateKeyEncoding</span>: {
22294 <span class="hljs-attr">type</span>: <span class="hljs-string">'pkcs8'</span>,
22295 <span class="hljs-attr">format</span>: <span class="hljs-string">'pem'</span>,
22296 <span class="hljs-attr">cipher</span>: <span class="hljs-string">'aes-256-cbc'</span>,
22297 <span class="hljs-attr">passphrase</span>: <span class="hljs-string">'top secret'</span>
22308 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Strin…
22311 <pre><code class="language-js"><span class="hljs-keyword">const</span> ciphers = crypto.<span class…
22312 …ljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(ciphers); <sp…
22318 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Strin…
22320 <pre><code class="language-js"><span class="hljs-keyword">const</span> curves = crypto.<span class=…
22321-variable language_">console</span>.<span class="hljs-title function_">log</span>(curves); <span c…
22327 <li><code>groupName</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_st…
22332 <a href="https://www.rfc-editor.org/rfc/rfc2412.txt">RFC 2412</a>, but see <a href="#crypto_support…
22333 …>'modp17'</code>, <code>'modp18'</code> (defined in <a href="https://www.rfc-editor.org/rfc/rfc352…
22341 …ode class="language-js"><span class="hljs-keyword">const</span> crypto = <span class="hljs-built_i…
22342 <span class="hljs-keyword">const</span> alice = crypto.<span class="hljs-title function_">getDiffie…
22343 <span class="hljs-keyword">const</span> bob = crypto.<span class="hljs-title function_">getDiffieHe…
22345 alice.<span class="hljs-title function_">generateKeys</span>();
22346 bob.<span class="hljs-title function_">generateKeys</span>();
22348-keyword">const</span> aliceSecret = alice.<span class="hljs-title function_">computeSecret</span>…
22349-keyword">const</span> bobSecret = bob.<span class="hljs-title function_">computeSecret</span>(ali…
22351 <span class="hljs-comment">/* aliceSecret and bobSecret should be the same */</span>
22352 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(…
22358 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Numbe…
22359 currently in use, <code>0</code> otherwise. A future semver-major release may change
22360 the return type of this API to a <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/D…
22367 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Strin…
22368 such as <code>'RSA-SHA256'</code>. Hash algorithms are also called "digest" algorithms.</li>
22370 <pre><code class="language-js"><span class="hljs-keyword">const</span> hashes = crypto.<span class=…
22371 …hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(hashes); <sp…
22391-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | <a href="#bu…
22392-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | <a href="#bu…
22393 <li><code>iterations</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_s…
22394 <li><code>keylen</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struc…
22395 <li><code>digest</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struc…
22396 <li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Referenc…
22398 <li><code>err</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Glo…
22403 <p>Provides an asynchronous Password-Based Key Derivation Function 2 (PBKDF2)
22418 …s://nvlpubs.nist.gov/nistpubs/Legacy/SP/nistspecialpublication800-132.pdf">NIST SP 800-132</a> for…
22419 …ode class="language-js"><span class="hljs-keyword">const</span> crypto = <span class="hljs-built_i…
22420-title function_">pbkdf2</span>(<span class="hljs-string">'secret'</span>, <span class="hljs-strin…
22421 <span class="hljs-keyword">if</span> (err) <span class="hljs-keyword">throw</span> err;
22422-variable language_">console</span>.<span class="hljs-title function_">log</span>(derivedKey.<span…
22427 …ode class="language-js"><span class="hljs-keyword">const</span> crypto = <span class="hljs-built_i…
22428 crypto.<span class="hljs-property">DEFAULT_ENCODING</span> = <span class="hljs-string">'hex'</span>;
22429-title function_">pbkdf2</span>(<span class="hljs-string">'secret'</span>, <span class="hljs-strin…
22430 <span class="hljs-keyword">if</span> (err) <span class="hljs-keyword">throw</span> err;
22431 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>…
22455-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | <a href="#bu…
22456-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | <a href="#bu…
22457 <li><code>iterations</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_s…
22458 <li><code>keylen</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struc…
22459 <li><code>digest</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struc…
22462 <p>Provides a synchronous Password-Based Key Derivation Function 2 (PBKDF2)
22474 …s://nvlpubs.nist.gov/nistpubs/Legacy/SP/nistspecialpublication800-132.pdf">NIST SP 800-132</a> for…
22475 …ode class="language-js"><span class="hljs-keyword">const</span> crypto = <span class="hljs-built_i…
22476-keyword">const</span> key = crypto.<span class="hljs-title function_">pbkdf2Sync</span>(<span cla…
22477-variable language_">console</span>.<span class="hljs-title function_">log</span>(key.<span class=…
22481 …ode class="language-js"><span class="hljs-keyword">const</span> crypto = <span class="hljs-built_i…
22482 crypto.<span class="hljs-property">DEFAULT_ENCODING</span> = <span class="hljs-string">'hex'</span>;
22483-keyword">const</span> key = crypto.<span class="hljs-title function_">pbkdf2Sync</span>(<span cla…
22484 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(…
22504 …lla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type">&#x3C;Object></a> …
22506 <li><code>oaepHash</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_str…
22508 …org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray" class="type">&#x3C;TypedArray><…
22516 …org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray" class="type">&#x3C;TypedArray><…
22538 …lla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type">&#x3C;Object></a> …
22540 <li><code>key</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structur…
22541 <li><code>passphrase</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_s…
22547 …org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray" class="type">&#x3C;TypedArray><…
22569 …lla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type">&#x3C;Object></a> …
22571 <li><code>passphrase</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_s…
22577 …org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray" class="type">&#x3C;TypedArray><…
22605 …lla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type">&#x3C;Object></a> …
22607 <li><code>key</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structur…
22608 <li><code>oaepHash</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_str…
22610 …org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray" class="type">&#x3C;TypedArray><…
22612 <li><code>passphrase</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_s…
22619 …org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray" class="type">&#x3C;TypedArray><…
22644 <li><code>size</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
22645 <li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Referenc…
22647 <li><code>err</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Glo…
22653 <p>Generates cryptographically strong pseudo-random data. The <code>size</code> argument
22659 <pre><code class="language-js"><span class="hljs-comment">// Asynchronous</span>
22660 <span class="hljs-keyword">const</span> crypto = <span class="hljs-built_in">require</span>(<span c…
22661 …pan class="hljs-title function_">randomBytes</span>(<span class="hljs-number">256</span>, <span cl…
22662 <span class="hljs-keyword">if</span> (err) <span class="hljs-keyword">throw</span> err;
22663-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hlj…
22668 <pre><code class="language-js"><span class="hljs-comment">// Synchronous</span>
22669 <span class="hljs-keyword">const</span> buf = crypto.<span class="hljs-title function_">randomBytes…
22670 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(
22671 …ss="hljs-string">`<span class="hljs-subst">${buf.length}</span> bytes of random data: <span class=…
22697 …org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray" class="type">&#x3C;TypedArray><…
22698 <li><code>offset</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struc…
22699 …illa.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;number></a> <st…
22700 …org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray" class="type">&#x3C;TypedArray><…
22703-js"><span class="hljs-keyword">const</span> buf = <span class="hljs-title class_">Buffer</span>.<…
22704-variable language_">console</span>.<span class="hljs-title function_">log</span>(crypto.<span cla…
22706 crypto.<span class="hljs-title function_">randomFillSync</span>(buf, <span class="hljs-number">5</s…
22707-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf.<span class=…
22709 <span class="hljs-comment">// The above is equivalent to the following:</span>
22710 crypto.<span class="hljs-title function_">randomFillSync</span>(buf, <span class="hljs-number">5</s…
22711-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf.<span class=…
22713-js"><span class="hljs-keyword">const</span> a = <span class="hljs-keyword">new</span> <span class…
22714-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hlj…
22715 …js-property">byteOffset</span>, a.<span class="hljs-property">byteLength</span>).<span class="hljs
22717-keyword">const</span> b = <span class="hljs-keyword">new</span> <span class="hljs-title class_">F…
22718-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hlj…
22719 …js-property">byteOffset</span>, b.<span class="hljs-property">byteLength</span>).<span class="hljs
22721-keyword">const</span> c = <span class="hljs-keyword">new</span> <span class="hljs-title class_">D…
22722-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hlj…
22723 …js-property">byteOffset</span>, c.<span class="hljs-property">byteLength</span>).<span class="hljs
22737 …org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray" class="type">&#x3C;TypedArray><…
22738 <li><code>offset</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struc…
22739 …illa.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;number></a> <st…
22740 <li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Referenc…
22746-js"><span class="hljs-keyword">const</span> buf = <span class="hljs-title class_">Buffer</span>.<…
22747 crypto.<span class="hljs-title function_">randomFill</span>(buf, <span class="hljs-function">(<span…
22748 <span class="hljs-keyword">if</span> (err) <span class="hljs-keyword">throw</span> err;
22749-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf.<span class=…
22752 …an class="hljs-title function_">randomFill</span>(buf, <span class="hljs-number">5</span>, <span c…
22753 <span class="hljs-keyword">if</span> (err) <span class="hljs-keyword">throw</span> err;
22754-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf.<span class=…
22757 <span class="hljs-comment">// The above is equivalent to the following:</span>
22758-title function_">randomFill</span>(buf, <span class="hljs-number">5</span>, <span class="hljs-num…
22759 <span class="hljs-keyword">if</span> (err) <span class="hljs-keyword">throw</span> err;
22760-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf.<span class=…
22763-js"><span class="hljs-keyword">const</span> a = <span class="hljs-keyword">new</span> <span class…
22764 crypto.<span class="hljs-title function_">randomFill</span>(a, <span class="hljs-function">(<span c…
22765 <span class="hljs-keyword">if</span> (err) <span class="hljs-keyword">throw</span> err;
22766-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hlj…
22767 .<span class="hljs-title function_">toString</span>(<span class="hljs-string">'hex'</span>));
22770-keyword">const</span> b = <span class="hljs-keyword">new</span> <span class="hljs-title class_">F…
22771 crypto.<span class="hljs-title function_">randomFill</span>(b, <span class="hljs-function">(<span c…
22772 <span class="hljs-keyword">if</span> (err) <span class="hljs-keyword">throw</span> err;
22773-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hlj…
22774 .<span class="hljs-title function_">toString</span>(<span class="hljs-string">'hex'</span>));
22777-keyword">const</span> c = <span class="hljs-keyword">new</span> <span class="hljs-title class_">D…
22778 crypto.<span class="hljs-title function_">randomFill</span>(c, <span class="hljs-function">(<span c…
22779 <span class="hljs-keyword">if</span> (err) <span class="hljs-keyword">throw</span> err;
22780-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hlj…
22781 .<span class="hljs-title function_">toString</span>(<span class="hljs-string">'hex'</span>));
22795 <li><code>min</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structur…
22796 <li><code>max</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structur…
22797 <li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Referenc…
22801 <p>The range (<code>max - min</code>) must be less than 2<sup>48</sup>. <code>min</code> and <code>…
22802 be <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number…
22805 <pre><code class="language-js"><span class="hljs-comment">// Asynchronous</span>
22806 …<span class="hljs-title function_">randomInt</span>(<span class="hljs-number">3</span>, <span clas…
22807 <span class="hljs-keyword">if</span> (err) <span class="hljs-keyword">throw</span> err;
22808-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hlj…
22810 <pre><code class="language-js"><span class="hljs-comment">// Synchronous</span>
22811 <span class="hljs-keyword">const</span> n = crypto.<span class="hljs-title function_">randomInt</sp…
22812-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hlj…
22813 <pre><code class="language-js"><span class="hljs-comment">// With `min` argument</span>
22814 …="hljs-keyword">const</span> n = crypto.<span class="hljs-title function_">randomInt</span>(<span …
22815 …hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class=…
22821 <li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference…
22823 <li><code>disableEntropyCache</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScri…
22829 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Strin…
22831 <p>Generates a random <a href="https://www.rfc-editor.org/rfc/rfc4122.txt">RFC 4122</a> version 4 U…
22848-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | <a href="#bu…
22849-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | <a href="#bu…
22850 <li><code>keylen</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struc…
22851 <li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference…
22853 <li><code>cost</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
22855 <li><code>blockSize</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_st…
22856 <li><code>parallelization</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/D…
22857 <li><code>N</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures…
22858 <li><code>r</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures…
22859 <li><code>p</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures…
22860 <li><code>maxmem</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struc…
22864 <li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Referenc…
22866 <li><code>err</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Glo…
22871 …a href="https://en.wikipedia.org/wiki/Scrypt">scrypt</a> implementation. Scrypt is a password-based
22873 memory-wise in order to make brute-force attacks unrewarding.</p>
22875 …s://nvlpubs.nist.gov/nistpubs/Legacy/SP/nistspecialpublication800-132.pdf">NIST SP 800-132</a> for…
22881 …ode class="language-js"><span class="hljs-keyword">const</span> crypto = <span class="hljs-built_i…
22882 <span class="hljs-comment">// Using the factory defaults.</span>
22883-title function_">scrypt</span>(<span class="hljs-string">'password'</span>, <span class="hljs-str…
22884 <span class="hljs-keyword">if</span> (err) <span class="hljs-keyword">throw</span> err;
22885-variable language_">console</span>.<span class="hljs-title function_">log</span>(derivedKey.<span…
22887 <span class="hljs-comment">// Using a custom N parameter. Must be a power of two.</span>
22888-title function_">scrypt</span>(<span class="hljs-string">'password'</span>, <span class="hljs-str…
22889 <span class="hljs-keyword">if</span> (err) <span class="hljs-keyword">throw</span> err;
22890-variable language_">console</span>.<span class="hljs-title function_">log</span>(derivedKey.<span…
22907-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | <a href="#bu…
22908-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | <a href="#bu…
22909 <li><code>keylen</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struc…
22910 <li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference…
22912 <li><code>cost</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
22914 <li><code>blockSize</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_st…
22915 <li><code>parallelization</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/D…
22916 <li><code>N</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures…
22917 <li><code>r</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures…
22918 <li><code>p</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures…
22919 <li><code>maxmem</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struc…
22925 …a href="https://en.wikipedia.org/wiki/Scrypt">scrypt</a> implementation. Scrypt is a password-based
22927 memory-wise in order to make brute-force attacks unrewarding.</p>
22929 …s://nvlpubs.nist.gov/nistpubs/Legacy/SP/nistspecialpublication800-132.pdf">NIST SP 800-132</a> for…
22934 …ode class="language-js"><span class="hljs-keyword">const</span> crypto = <span class="hljs-built_i…
22935 <span class="hljs-comment">// Using the factory defaults.</span>
22936-keyword">const</span> key1 = crypto.<span class="hljs-title function_">scryptSync</span>(<span cl…
22937-variable language_">console</span>.<span class="hljs-title function_">log</span>(key1.<span class…
22938 <span class="hljs-comment">// Using a custom N parameter. Must be a power of two.</span>
22939-keyword">const</span> key2 = crypto.<span class="hljs-title function_">scryptSync</span>(<span cl…
22940-variable language_">console</span>.<span class="hljs-title function_">log</span>(key2.<span class…
22946 <li><code>engine</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struc…
22967 <p>The flags below are deprecated in OpenSSL-1.1.0.</p>
22978 <li><code>bool</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
22980 <p>Enables the FIPS compliant crypto provider in a FIPS-enabled Node.js build.
22988 <td><p>This function now supports IEEE-P1363 DSA and ECDSA signatures.</p></td></tr>
22995-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | <a href="htt…
22996 …org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray" class="type">&#x3C;TypedArray><…
22997 …lla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type">&#x3C;Object></a> …
23008 <p><code>dsaEncoding</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_s…
23011 <li><code>'der'</code> (default): DER-encoded ASN.1 signature structure encoding <code>(r, s)</code…
23012 <li><code>'ieee-p1363'</code>: Signature format <code>r || s</code> as proposed in IEEE-P1363.</li>
23016 <p><code>padding</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struc…
23022 used to sign the message as specified in section 3.1 of <a href="https://www.rfc-editor.org/rfc/rfc…
23025 <p><code>saltLength</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_st…
23037 …org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray" class="type">&#x3C;TypedArray><…
23038 …org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray" class="type">&#x3C;TypedArray><…
23039 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boole…
23041 <p>This function is based on a constant-time algorithm.
23045 <a href="https://www.w3.org/TR/capability-urls/">capability urls</a>.</p>
23052 is timing-safe. Care should be taken to ensure that the surrounding code does
23060 <td><p>This function now supports IEEE-P1363 DSA and ECDSA signatures.</p></td></tr>
23067-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | <a href="htt…
23068 …org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray" class="type">&#x3C;TypedArray><…
23069 …lla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type">&#x3C;Object></a> …
23070 …org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray" class="type">&#x3C;TypedArray><…
23071 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boole…
23081 <p><code>dsaEncoding</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_s…
23084 <li><code>'der'</code> (default): DER-encoded ASN.1 signature structure encoding <code>(r, s)</code…
23085 <li><code>'ieee-p1363'</code>: Signature format <code>r || s</code> as proposed in IEEE-P1363.</li>
23089 <p><code>padding</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struc…
23095 used to sign the message as specified in section 3.1 of <a href="https://www.rfc-editor.org/rfc/rfc…
23098 <p><code>saltLength</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_st…
23119 <p>Usage of <code>ECDH</code> with non-dynamically generated key pairs has been simplified.
23137 …ttps://nvlpubs.nist.gov/nistpubs/SpecialPublications/NIST.SP.800-131Ar1.pdf">NIST SP 800-131A</a>:…
23139 <li>MD5 and SHA-1 are no longer acceptable where collision resistance is
23157 <li>The length of the plaintext is limited to <code>2 ** (8 * (15 - N))</code> bytes.</li>
23161 compliance with section 2.6 of <a href="https://www.rfc-editor.org/rfc/rfc3610.txt">RFC 3610</a>.</…
23178 …ode class="language-js"><span class="hljs-keyword">const</span> crypto = <span class="hljs-built_i…
23180 <span class="hljs-keyword">const</span> key = <span class="hljs-string">'keykeykeykeykeykeykeykey'<…
23181 <span class="hljs-keyword">const</span> nonce = crypto.<span class="hljs-title function_">randomByt…
23183-keyword">const</span> aad = <span class="hljs-title class_">Buffer</span>.<span class="hljs-title…
23185 …class="hljs-keyword">const</span> cipher = crypto.<span class="hljs-title function_">createCipheri…
23186 <span class="hljs-attr">authTagLength</span>: <span class="hljs-number">16</span>
23188 <span class="hljs-keyword">const</span> plaintext = <span class="hljs-string">'Hello world'</span>;
23189 cipher.<span class="hljs-title function_">setAAD</span>(aad, {
23190 …<span class="hljs-attr">plaintextLength</span>: <span class="hljs-title class_">Buffer</span>.<spa…
23192 …span class="hljs-keyword">const</span> ciphertext = cipher.<span class="hljs-title function_">upda…
23193 cipher.<span class="hljs-title function_">final</span>();
23194 <span class="hljs-keyword">const</span> tag = cipher.<span class="hljs-title function_">getAuthTag<…
23196 <span class="hljs-comment">// Now transmit { ciphertext, nonce, tag }.</span>
23198 …ass="hljs-keyword">const</span> decipher = crypto.<span class="hljs-title function_">createDeciphe…
23199 <span class="hljs-attr">authTagLength</span>: <span class="hljs-number">16</span>
23201 decipher.<span class="hljs-title function_">setAuthTag</span>(tag);
23202 decipher.<span class="hljs-title function_">setAAD</span>(aad, {
23203 …<span class="hljs-attr">plaintextLength</span>: ciphertext.<span class="hljs-property">length</spa…
23205-keyword">const</span> receivedPlaintext = decipher.<span class="hljs-title function_">update</spa…
23207 <span class="hljs-keyword">try</span> {
23208 decipher.<span class="hljs-title function_">final</span>();
23209 } <span class="hljs-keyword">catch</span> (err) {
23210 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">error</spa…
23211 <span class="hljs-keyword">return</span>;
23214 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(…
23232 <td>Instructs OpenSSL to allow a non-[EC]DHE-based key exchange mode
23257 <td>Instructs OpenSSL to add server-hello extension from an early version
23284 <td>Instructs OpenSSL to disable the workaround for a man-in-the-middle
23285 protocol-version vulnerability in the SSL 2.0 server implementation.</td>
23309 <td>Instructs OpenSSL to disable encrypt-then-MAC.</td>
23535 <td>Specifies the built-in default cipher list used by Node.js.</td>
23546 …ty api_stability_2"><a href="#documentation_stability_index">Stability: 2</a> - Stable</div><p></p>
23548 <p>Node.js includes a command-line debugging utility. To use it, start Node.js
23550 <pre><code class="language-console"><span class="hljs-meta">$ </span><span class="language-bash">no…
23551 &#x3C; Debugger listening on ws://127.0.0.1:9229/621111f9-ffcb-4e82-b718-48a145fa5db8
23559 <span class="hljs-meta">> </span><span class="language-bash">2 global.x = 5;</span>
23562 <span class="hljs-meta">debug></span></code></pre>
23563 <p>The Node.js debugger client is not a full-featured debugger, but simple step and
23567 <!-- eslint-disable no-debugger -->
23568 <pre><code class="language-js"><span class="hljs-comment">// myscript.js</span>
23569 <span class="hljs-variable language_">global</span>.<span class="hljs-property">x</span> = <span cl…
23570 <span class="hljs-built_in">setTimeout</span>(<span class="hljs-function">() =></span> {
23571 <span class="hljs-keyword">debugger</span>;
23572 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>…
23573 }, <span class="hljs-number">1000</span>);
23574 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(…
23576 <pre><code class="language-console"><span class="hljs-meta">$ </span><span class="language-bash">no…
23577 &#x3C; Debugger listening on ws://127.0.0.1:9229/621111f9-ffcb-4e82-b718-48a145fa5db8
23585 <span class="hljs-meta">> </span><span class="language-bash">2 global.x = 5;</span>
23588 <span class="hljs-meta">debug> </span><span class="language-bash">cont</span>
23594 <span class="hljs-meta">> </span><span class="language-bash">4 debugger;</span>
23597 <span class="hljs-meta">debug> </span><span class="language-bash">next</span>
23601 <span class="hljs-meta">> </span><span class="language-bash">5 console.log(<span class="hljs-stri…
23604 <span class="hljs-meta">debug> </span><span class="language-bash">repl</span>
23606 <span class="hljs-meta">> </span><span class="language-bash">x</span>
23608 <span class="hljs-meta">> </span><span class="language-bash">2 + 2</span>
23610 <span class="hljs-meta">debug> </span><span class="language-bash">next</span>
23616 <span class="hljs-meta">> </span><span class="language-bash">6 }, 1000);</span>
23619 <span class="hljs-meta">debug> </span><span class="language-bash">.<span class="hljs-built_in">exit…
23620 <span class="hljs-meta">$</span></code></pre>
23658 <pre><code class="language-console"><span class="hljs-meta">$ </span><span class="language-bash">no…
23659 &#x3C; Debugger listening on ws://127.0.0.1:9229/48a5b28a-550c-471b-b5e1-d13dd7165df9
23666 <span class="hljs-meta">> </span><span class="language-bash">1 const mod = require(<span class="hlj…
23669 <span class="hljs-meta">debug> </span><span class="language-bash">setBreakpoint(<span class="hljs-s…
23671 <span class="hljs-meta">debug> </span><span class="language-bash">c</span>
23675 <span class="hljs-meta">></span><span class="language-bash">22 exports.hello = <span class="hljs-fu…
23678 <span class="hljs-meta">debug></span></code></pre>
23681 <pre><code class="language-console"><span class="hljs-meta">$ </span><span class="language-bash">no…
23682 &#x3C; Debugger listening on ws://127.0.0.1:9229/ce24daa8-3816-44d4-b8ab-8273c8a66d35
23688 <span class="hljs-meta">> </span><span class="language-bash">7 addOne(10);</span>
23689 8 addOne(-1);
23691 … class="hljs-meta">debug> </span><span class="language-bash">setBreakpoint(<span class="hljs-strin…
23695 <span class="hljs-meta">> </span><span class="language-bash">4 <span class="hljs-built_in">return…
23699 8 addOne(-1);
23701 <span class="hljs-meta">debug> </span><span class="language-bash">cont</span>
23705 <span class="hljs-meta">> </span><span class="language-bash">4 <span class="hljs-built_in">return…
23708 <span class="hljs-meta">debug> </span><span class="language-bash"><span class="hljs-built_in">exec<…
23709 -1
23710 <span class="hljs-meta">debug></span></code></pre>
23738 <a href="https://chromedevtools.github.io/devtools-protocol/">Chrome DevTools Protocol</a>.</p>
23739 <p>V8 Inspector can be enabled by passing the <code>--inspect</code> flag when starting a
23741 e.g. <code>--inspect=9222</code> will accept DevTools connections on port 9222.</p>
23742 <p>To break on the first line of the application code, pass the <code>--inspect-brk</code>
23743 flag instead of <code>--inspect</code>.</p>
23744 <pre><code class="language-console"><span class="hljs-meta">$ </span><span class="language-bash">no…
23745 Debugger listening on ws://127.0.0.1:9229/dc9010dd-f8b8-4ac5-a510-c1a114ec7d29
23747 <p>(In the example above, the UUID dc9010dd-f8b8-4ac5-a510-c1a114ec7d29
23766 <li>Documentation-only</li>
23768 <li>End-of-Life</li>
23770 <p>A Documentation-only deprecation is one that is expressed only within the
23771 Node.js API docs. These generate no side-effects while running Node.js.
23772 Some Documentation-only deprecations trigger a runtime warning when launched
23773 with <a href="#cli_pending_deprecation"><code>--pending-deprecation</code></a> flag (or its alterna…
23775 deprecations below. Documentation-only deprecations that support that flag
23780 <a href="#cli_throw_deprecation"><code>--throw-deprecation</code></a> command-line flag is used, a …
23782 <p>An End-of-Life deprecation is used when functionality is or will soon be removed
23795 <td><p>End-of-Life.</p></td></tr>
23803 <p>Type: End-of-Life</p>
23812 <td><p>End-of-Life.</p></td></tr>
23820 <p>Type: End-of-Life</p>
23828 <td><p>End-of-Life.</p></td></tr>
23836 <p>Type: End-of-Life</p>
23845 <td><p>End-of-Life.</p></td></tr>
23849 <td><p>Documentation-only deprecation.</p></td></tr>
23853 <p>Type: End-of-Life</p>
23865 <td><p>Documentation-only deprecation.</p></td></tr>
23869 <p>Type: Runtime (supports <a href="#cli_pending_deprecation"><code>--pending-deprecation</code></a…
23882 …_arraybuffer_byteoffset_length"><code>Buffer.from(arrayBuffer[, byteOffset[, length]])</code></a> -
23888 <p>Without <code>--pending-deprecation</code>, runtime warnings occur only for code not in
23890 <code>Buffer()</code> usage in dependencies. With <code>--pending-deprecation</code>, a runtime
23898 <td><p>End-of-Life.</p></td></tr>
23904 <td><p>Documentation-only deprecation.</p></td></tr>
23908 <p>Type: End-of-Life</p>
23918 <td><p>End-of-Life.</p></td></tr>
23924 <td><p>Documentation-only deprecation.</p></td></tr>
23928 <p>Type: End-of-Life</p>
23934 precisely describe the actual semantics and was unnecessarily emotion-laden.</p>
23943 <td><p>Documentation-only deprecation.</p></td></tr>
23947 <p>Type: Documentation-only</p>
23958 <td><p>End-of-Life (for <code>digest === null</code>).</p></td></tr>
23962 <td><p>End-of-Life (for <code>digest === undefined</code>).</p></td></tr>
23970 <p>Type: End-of-Life</p>
23972 in Node.js 6.0 because the method defaulted to using the non-recommended
23986 <td><p>End-of-Life.</p></td></tr>
23994 <p>Type: End-of-Life</p>
24003 <td><p>End-of-Life.</p></td></tr>
24011 <p>Type: End-of-Life</p>
24020 <td><p>End-of-Life.</p></td></tr>
24028 <p>Type: End-of-Life</p>
24037 <td><p>End-of-Life.</p></td></tr>
24043 <p>Type: End-of-Life</p>
24052 <td><p>End-of-Life.</p></td></tr>
24058 <td><p>Documentation-only deprecation.</p></td></tr>
24062 <p>Type: End-of-Life</p>
24071 <td><p>End-of-Life.</p></td></tr>
24077 <td><p>Documentation-only deprecation.</p></td></tr>
24081 <p>Type: End-of-Life</p>
24090 <td><p>End-of-Life.</p></td></tr>
24098 <p>Type: End-of-Life</p>
24107 <td><p>End-of-Life.</p></td></tr>
24113 <p>Type: End-of-Life</p>
24114 <p><code>Intl.v8BreakIterator</code> was a non-standard extension and has been removed.
24115 See <a href="https://github.com/tc39/proposal-intl-segmenter"><code>Intl.Segmenter</code></a>.</p>
24128 that are not handled will terminate the Node.js process with a non-zero exit
24144 <p>Type: End-of-Life</p>
24168 <td><p>End-of-Life.</p></td></tr>
24176 <p>Type: End-of-Life</p>
24185 <td><p>End-of-Life.</p></td></tr>
24191 <p>Type: End-of-Life</p>
24200 <td><p>End-of-Life.</p></td></tr>
24208 <p>Type: End-of-Life</p>
24217 <td><p>End-of-Life.</p></td></tr>
24223 <p>Type: End-of-Life</p>
24245 <td><p>End-of-Life.</p></td></tr>
24253 <p>Type: End-of-Life</p>
24261 <td><p>End-of-Life.</p></td></tr>
24269 <p>Type: End-of-Life</p>
24277 <td><p>End-of-Life.</p></td></tr>
24285 <p>Type: End-of-Life</p>
24293 <td><p>End-of-Life.</p></td></tr>
24301 <p>Type: End-of-Life</p>
24311 <td><p>Documentation-only deprecation.</p></td></tr>
24315 <p>Type: Documentation-only</p>
24326 <td><p>Documentation-only deprecation.</p></td></tr>
24330 <p>Type: Documentation-only</p>
24341 <td><p>Documentation-only deprecation.</p></td></tr>
24345 <p>Type: Documentation-only</p>
24355 <td><p>Documentation-only deprecation.</p></td></tr>
24359 <p>Type: Documentation-only</p>
24370 <td><p>Documentation-only deprecation.</p></td></tr>
24374 <p>Type: Documentation-only</p>
24385 <td><p>Documentation-only deprecation.</p></td></tr>
24389 <p>Type: Documentation-only</p>
24399 <td><p>Documentation-only deprecation.</p></td></tr>
24403 <p>Type: Documentation-only</p>
24415 <td><p>Documentation-only deprecation.</p></td></tr>
24433 <td><p>Documentation-only deprecation.</p></td></tr>
24448 <td><p>Documentation-only deprecation.</p></td></tr>
24452 <p>Type: Documentation-only</p>
24460 <td><p>Documentation-only deprecation.</p></td></tr>
24464 <p>Type: Documentation-only</p>
24473 <td><p>End-of-Life.</p></td></tr>
24477 <td><p>Documentation-only deprecation.</p></td></tr>
24481 <p>Type: End-of-Life</p>
24490 <td><p>End-of-Life.</p></td></tr>
24494 <td><p>Documentation-only deprecation.</p></td></tr>
24498 <p>Type: End-of-Life</p>
24511 <td><p>Documentation-only deprecation.</p></td></tr>
24519 <p>Type: Documentation-only</p>
24530 <td><p>Documentation-only deprecation.</p></td></tr>
24534 <p>Type: Documentation-only</p>
24545 <td><p>Documentation-only deprecation.</p></td></tr>
24549 <p>Type: Documentation-only</p>
24559 <td><p>Documentation-only deprecation.</p></td></tr>
24563 <p>Type: Documentation-only</p>
24574 <td><p>Documentation-only deprecation.</p></td></tr>
24578 <p>Type: Documentation-only</p>
24588 <td><p>Documentation-only deprecation.</p></td></tr>
24592 <p>Type: Documentation-only</p>
24602 <td><p>Documentation-only deprecation.</p></td></tr>
24606 <p>Type: Documentation-only</p>
24616 <td><p>Documentation-only deprecation.</p></td></tr>
24620 <p>Type: Documentation-only</p>
24630 <td><p>Documentation-only deprecation.</p></td></tr>
24634 <p>Type: Documentation-only</p>
24644 <td><p>Documentation-only deprecation.</p></td></tr>
24648 <p>Type: Documentation-only</p>
24658 <td><p>Documentation-only deprecation.</p></td></tr>
24662 <p>Type: Documentation-only</p>
24672 <td><p>Documentation-only deprecation.</p></td></tr>
24676 <p>Type: Documentation-only</p>
24686 <td><p>Documentation-only deprecation.</p></td></tr>
24690 <p>Type: Documentation-only</p>
24700 <td><p>Documentation-only deprecation.</p></td></tr>
24704 <p>Type: Documentation-only</p>
24714 <td><p>Documentation-only deprecation.</p></td></tr>
24718 <p>Type: Documentation-only</p>
24728 <td><p>Documentation-only deprecation.</p></td></tr>
24732 <p>Type: Documentation-only</p>
24742 <td><p>Documentation-only deprecation.</p></td></tr>
24746 <p>Type: Documentation-only</p>
24756 <td><p>Documentation-only deprecation.</p></td></tr>
24760 <p>Type: Documentation-only</p>
24768 <td><p>End-of-Life.</p></td></tr>
24772 <td><p>Documentation-only deprecation.</p></td></tr>
24776 <p>Type: End-of-Life</p>
24780 <h4 id="DEP0062">DEP0062: <code>node --debug</code><span><a class="mark" href="#deprecations_dep006…
24786 <td><p>End-of-Life.</p></td></tr>
24792 <p>Type: End-of-Life</p>
24793 <p><code>--debug</code> activates the legacy V8 debugger interface, which was removed as
24794 of V8 5.8. It is replaced by Inspector which is activated with <code>--inspect</code>
24802 <td><p>Documentation-only deprecation.</p></td></tr>
24806 <p>Type: Documentation-only</p>
24821 <td><p>Documentation-only deprecation.</p></td></tr>
24838 <td><p>End-of-Life.</p></td></tr>
24840 <td><p>Documentation-only deprecation.</p></td></tr>
24844 <p>Type: End-of-Life</p>
24860 <td><p>Documentation-only deprecation.</p></td></tr>
24883 <td><p>Documentation-only deprecation.</p></td></tr>
24887 <p>Type: Documentation-only</p>
24904 a V8-inspector based CLI debugger available through <code>node inspect</code>.</p>
24911 <td><p>End-of-Life.</p></td></tr>
24915 <td><p>Documentation-only deprecation.</p></td></tr>
24919 <p>Type: End-of-Life</p>
24928 <td><p>End-of-Life.</p></td></tr>
24934 <p>Type: End-of-Life</p>
24944 <td><p>End-of-Life.</p></td></tr>
24950 <p>Type: End-of-Life</p>
24960 <td><p>End-of-Life.</p></td></tr>
24966 <p>Type: End-of-Life</p>
24976 <td><p>End-of-Life.</p></td></tr>
24982 <p>Type: End-of-Life</p>
24985 <p>As the original API was undocumented and not generally useful for non-internal
25020 <td><p>Documentation-only deprecation.</p></td></tr>
25027 … class="language-js"><span class="hljs-keyword">const</span> querystring = <span class="hljs-built…
25028 querystring.<span class="hljs-title function_">parse</span>(str, <span class="hljs-string">'\n'</sp…
25031-console"><span class="hljs-meta">> </span><span class="language-bash">querystring.parse(<span cla…
25033 <span class="hljs-meta">> </span><span class="language-bash">tls.parseCertString(<span class="hljs-
25067 <td><p>End-of-Life.</p></td></tr>
25071 <td><p>Documentation-only deprecation.</p></td></tr>
25075 <p>Type: End-of-Life</p>
25086 <td><p>Documentation-only deprecation.</p></td></tr>
25090 <p>Type: Documentation-only</p>
25127 <td><p>End-of-Life.</p></td></tr>
25133 <p>Type: End-of-Life.</p>
25150 <p>Type: End-of-Life</p>
25163 <li><code>v8/tools/tickprocessor-driver</code></li>
25165 <li><code>node-inspect/lib/_inspect</code> (from 7.6.0)</li>
25166 <li><code>node-inspect/lib/internal/inspect_client</code> (from 7.6.0)</li>
25167 <li><code>node-inspect/lib/internal/inspect_repl</code> (from 7.6.0)</li>
25172 <p>On the other hand, <code>node-inspect</code> can be installed locally through a package
25181 <td><p>End-of-Life.</p></td></tr>
25187 <p>Type: End-of-Life</p>
25197 <td><p>End-of-Life.</p></td></tr>
25203 <p>Type: End-of-Life</p>
25214 <td><p>Documentation-only deprecation.</p></td></tr>
25228 <td><p>End-of-Life.</p></td></tr>
25234 <p>Type: End-of-Life</p>
25239 …s://nvlpubs.nist.gov/nistpubs/Legacy/SP/nistspecialpublication800-38d.pdf">NIST SP 800-38D</a>.</p>
25252 <h4 id="DEP0092">DEP0092: Top-level <code>this</code> bound to <code>module.exports</code><span><a …
25258 <td><p>Documentation-only deprecation.</p></td></tr>
25262 <p>Type: Documentation-only</p>
25263 <p>Assigning properties to the top-level <code>this</code> as an alternative
25272 <td><p>Documentation-only deprecation.</p></td></tr>
25276 <p>Type: Documentation-only</p>
25332 <code>CallbackScope</code>, or the high-level <code>AsyncResource</code> class.</p>
25339 <td><p>End-of-Life.</p></td></tr>
25345 <p>Type: End-of-Life</p>
25352 <h4 id="DEP0099">DEP0099: Async context-unaware <code>node::MakeCallback</code> C++ APIs<span><a cl…
25358 <td><p>Compile-time deprecation.</p></td></tr>
25362 <p>Type: Compile-time</p>
25374 <td><p>Documentation-only deprecation.</p></td></tr>
25381 <h4 id="DEP0101">DEP0101: <code>--with-lttng</code><span><a class="mark" href="#deprecations_dep010…
25387 <td><p>End-of-Life.</p></td></tr>
25391 <p>Type: End-of-Life</p>
25392 <p>The <code>--with-lttng</code> compile-time option has been removed.</p>
25399 <td><p>End-of-Life.</p></td></tr>
25403 <p>Type: End-of-Life</p>
25415 <td><p>Documentation-only deprecation.</p></td></tr>
25419 <p>Type: Documentation-only (supports <a href="#cli_pending_deprecation"><code>--pending-deprecatio…
25430 <td><p>Documentation-only deprecation.</p></td></tr>
25434 <p>Type: Documentation-only (supports <a href="#cli_pending_deprecation"><code>--pending-deprecatio…
25435 <p>When assigning a non-string property to <a href="#process_process_env"><code>process.env</code><…
25446 <td><p>End-of-Life.</p></td></tr>
25452 <p>Type: End-of-Life</p>
25464 <td><p>Documentation-only deprecation.</p></td></tr>
25481 <td><p>End-of-Life.</p></td></tr>
25487 <p>Type: End-of-Life</p>
25498 <td><p>Documentation-only deprecation.</p></td></tr>
25530 <td><p>Documentation-only deprecation.</p></td></tr>
25534 <p>Type: Documentation-only</p>
25543 <td><p>Added support for <code>--pending-deprecation</code>.</p></td></tr>
25545 <td><p>Documentation-only deprecation.</p></td></tr>
25549 <p>Type: Documentation-only (supports <a href="#cli_pending_deprecation"><code>--pending-deprecatio…
25574 <td><p>End-of-Life.</p></td></tr>
25580 <p>Type: End-of-Life</p>
25589 <td><p>End-of-Life.</p></td></tr>
25595 <p>Type: End-of-Life</p>
25599 <!--lint disable nodejs-yaml-comments -->
25605 <td><p>Added documentation-only deprecation with <code>--pending-deprecation</code> support.</p></t…
25609 <!--lint enable nodejs-yaml-comments -->
25610 <p>Type: Documentation-only (supports <a href="#cli_pending_deprecation"><code>--pending-deprecatio…
25624 <td><p>Documentation-only deprecation.</p></td></tr>
25638 <td><p>End-of-Life.</p></td></tr>
25644 <p>Type: End-of-Life</p>
25671 <td><p>Documentation-only deprecation.</p></td></tr>
25675 <p>Type: Documentation-only (supports <a href="#cli_pending_deprecation"><code>--pending-deprecatio…
25684 <td><p>End-of-Life.</p></td></tr>
25690 <p>Type: End-of-Life</p>
25736 <a href="https://tools.ietf.org/html/rfc6066#section-3">RFC 6066</a>. This will be ignored in a fut…
25774 If re-referencing the timeout is necessary, <a href="#timers_timeout_ref"><code>timeout.ref()</code…
25797 <td><p>Documentation-only.</p></td></tr>
25801 <p>Type: Documentation-only (supports <a href="#cli_pending_deprecation"><code>--pending-deprecatio…
25802 <p>Modules that have an invalid <code>main</code> entry (e.g., <code>./does-not-exist.js</code>) and
25814 <td><p>Documentation-only.</p></td></tr>
25830 <td><p>Documentation-only.</p></td></tr>
25845 <td><p>Documentation-only.</p></td></tr>
25849 <p>Type: End-of-Life</p>
25852 <code>--http-parser=legacy</code> command-line flag could be used to revert to using the
25873 <td><p>Documentation-only deprecation.</p></td></tr>
25877 <p>Type: Documentation-only</p>
25886 <td><p>Documentation-only deprecation.</p></td></tr>
25890 <p>Type: Documentation-only (supports <a href="#cli_pending_deprecation"><code>--pending-deprecatio…
25914 <td><p>Documentation-only deprecation.</p></td></tr>
25918 <p>Type: Documentation-only</p>
25942-js"><span class="hljs-keyword">const</span> fsPromises = <span class="hljs-built_in">require</spa…
25943 …s="hljs-keyword">async</span> <span class="hljs-keyword">function</span> <span class="hljs-title f…
25944 <span class="hljs-keyword">let</span> filehandle;
25945 <span class="hljs-keyword">try</span> {
25946 …ljs-keyword">await</span> fsPromises.<span class="hljs-title function_">open</span>(<span class="h…
25947 } <span class="hljs-keyword">finally</span> {
25948 … <span class="hljs-keyword">if</span> (filehandle !== <span class="hljs-literal">undefined</span>)
25949 …<span class="hljs-keyword">await</span> filehandle.<span class="hljs-title function_">close</span>…
25958 <td><p>Documentation-only deprecation.</p></td></tr>
25962 <p>Type: Documentation-only</p>
25963 <p><a href="#process_process_mainmodule"><code>process.mainModule</code></a> is a CommonJS-only fea…
25964 object is shared with non-CommonJS environment. Its use within ECMAScript
25974 <td><p>Documentation-only deprecation.</p></td></tr>
25978 <p>Type: Documentation-only</p>
25979 <p>Calling <code>process.umask()</code> with no argument causes the process-wide umask to be
25981 potential security vulnerability. There is no safe, cross-platform alternative
25989 <td><p>Documentation-only deprecation.</p></td></tr>
25993 <p>Type: Documentation-only</p>
26001 <td><p>Documentation-only (supports [<code>--pending-deprecation</code>][]).</p></td></tr>
26005 <p>Type: Documentation-only (supports <a href="#cli_pending_deprecation"><code>--pending-deprecatio…
26014 <td><p>Documentation-only (supports [<code>--pending-deprecation</code>][]).</p></td></tr>
26018 <p>Type: Documentation-only</p>
26041 <td><p>Documentation-only deprecation.</p></td></tr>
26045 <p>Type: Documentation-only</p>
26052 …ge-js"><span class="hljs-keyword">if</span> (<span class="hljs-built_in">require</span>.<span clas…
26053 …<span class="hljs-comment">// Code section that will run only if current file is the entry point.<…
26057-js"><span class="hljs-keyword">const</span> moduleParents = <span class="hljs-title class_">Objec…
26058-title function_">filter</span>(<span class="hljs-function">(<span class="hljs-params">m</span>) =…
26065 <td><p>Documentation-only deprecation.</p></td></tr>
26069 <p>Type: Documentation-only</p>
26077 <td><p>Documentation-only deprecation.</p></td></tr>
26081 <p>Type: Documentation-only</p>
26090 <td><p>Documentation-only deprecation.</p></td></tr>
26094 <p>Type: Documentation-only</p>
26096 on nonexistent paths, or when given a file as a target.
26104 <td><p>Documentation-only deprecation with <code>--pending-deprecation</code> support.</p></td></tr>
26108 <p>Type: Documentation-only (supports <a href="#cli_pending_deprecation"><code>--pending-deprecatio…
26116 … api_stability_1"><a href="#documentation_stability_index">Stability: 1</a> - Experimental</div><p…
26121 …ss="language-js"><span class="hljs-keyword">const</span> diagnostics_channel = <span class="hljs-b…
26123 will create one or many top-level channels to report messages through.
26135 …ss="language-js"><span class="hljs-keyword">const</span> diagnostics_channel = <span class="hljs-b…
26137 <span class="hljs-comment">// Get a reusable channel object</span>
26138 …lass="hljs-keyword">const</span> channel = diagnostics_channel.<span class="hljs-title function_">…
26140 <span class="hljs-comment">// Subscribe to the channel</span>
26141 channel.<span class="hljs-title function_">subscribe</span>(<span class="hljs-function">(<span clas…
26142 <span class="hljs-comment">// Received data</span>
26145 <span class="hljs-comment">// Check if the channel has an active subscriber</span>
26146 <span class="hljs-keyword">if</span> (channel.<span class="hljs-property">hasSubscribers</span>) {
26147 <span class="hljs-comment">// Publish data to the channel</span>
26148 channel.<span class="hljs-title function_">publish</span>({
26149 <span class="hljs-attr">some</span>: <span class="hljs-string">'data'</span>
26154 …zilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | …
26155 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boole…
26160 performance-sensitive code.</p>
26161 …ss="language-js"><span class="hljs-keyword">const</span> diagnostics_channel = <span class="hljs-b…
26163 … class="hljs-keyword">if</span> (diagnostics_channel.<span class="hljs-title function_">hasSubscri…
26164 <span class="hljs-comment">// There are subscribers, prepare and publish message</span>
26168 …zilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | …
26171 <p>This is the primary entry-point for anyone wanting to interact with a named
26174 …ss="language-js"><span class="hljs-keyword">const</span> diagnostics_channel = <span class="hljs-b…
26176 …lass="hljs-keyword">const</span> channel = diagnostics_channel.<span class="hljs-title function_">…
26187 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boole…
26192 performance-sensitive code.</p>
26193 …ss="language-js"><span class="hljs-keyword">const</span> diagnostics_channel = <span class="hljs-b…
26195 …lass="hljs-keyword">const</span> channel = diagnostics_channel.<span class="hljs-title function_">…
26197 <span class="hljs-keyword">if</span> (channel.<span class="hljs-property">hasSubscribers</span>) {
26198 <span class="hljs-comment">// There are subscribers, prepare and publish message</span>
26202 <li><code>message</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_stru…
26206 …ss="language-js"><span class="hljs-keyword">const</span> diagnostics_channel = <span class="hljs-b…
26208 …lass="hljs-keyword">const</span> channel = diagnostics_channel.<span class="hljs-title function_">…
26210 channel.<span class="hljs-title function_">publish</span>({
26211 <span class="hljs-attr">some</span>: <span class="hljs-string">'message'</span>
26215 <li><code>onMessage</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Referen…
26217 <li><code>message</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_stru…
26218 …zilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | …
26225 …ss="language-js"><span class="hljs-keyword">const</span> diagnostics_channel = <span class="hljs-b…
26227 …lass="hljs-keyword">const</span> channel = diagnostics_channel.<span class="hljs-title function_">…
26229 channel.<span class="hljs-title function_">subscribe</span>(<span class="hljs-function">(<span clas…
26230 <span class="hljs-comment">// Received data</span>
26245 <li><code>onMessage</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Referen…
26246 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boole…
26250 …ss="language-js"><span class="hljs-keyword">const</span> diagnostics_channel = <span class="hljs-b…
26252 …lass="hljs-keyword">const</span> channel = diagnostics_channel.<span class="hljs-title function_">…
26254 <span class="hljs-keyword">function</span> <span class="hljs-title function_">onMessage</span>(<spa…
26255 <span class="hljs-comment">// Received data</span>
26258 channel.<span class="hljs-title function_">subscribe</span>(onMessage);
26260 channel.<span class="hljs-title function_">unsubscribe</span>(onMessage);</code></pre></section>
26264 …ty api_stability_2"><a href="#documentation_stability_index">Stability: 2</a> - Stable</div><p></p>
26273 …code class="language-js"><span class="hljs-keyword">const</span> dns = <span class="hljs-built_in"…
26275 …n class="hljs-title function_">lookup</span>(<span class="hljs-string">'example.org'</span>, <span…
26276 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>…
26278 <span class="hljs-comment">// address: "93.184.216.34" family: IPv4</span></code></pre>
26283 DNS queries, bypassing other name-resolution facilities.</p>
26284 …code class="language-js"><span class="hljs-keyword">const</span> dns = <span class="hljs-built_in"…
26286 … class="hljs-title function_">resolve4</span>(<span class="hljs-string">'archive.org'</span>, <spa…
26287 <span class="hljs-keyword">if</span> (err) <span class="hljs-keyword">throw</span> err;
26289-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hlj…
26291 …addresses.<span class="hljs-title function_">forEach</span>(<span class="hljs-function">(<span cla…
26292 …dns.<span class="hljs-title function_">reverse</span>(a, <span class="hljs-function">(<span class=…
26293 <span class="hljs-keyword">if</span> (err) {
26294 <span class="hljs-keyword">throw</span> err;
26296-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hlj…
26310-js"><span class="hljs-keyword">const</span> { <span class="hljs-title class_">Resolver</span> } =…
26311 <span class="hljs-keyword">const</span> resolver = <span class="hljs-keyword">new</span> <span clas…
26312 resolver.<span class="hljs-title function_">setServers</span>([<span class="hljs-string">'4.4.4.4'<…
26314 <span class="hljs-comment">// This request will use the server at 4.4.4.4, independent of global se…
26315 … class="hljs-title function_">resolve4</span>(<span class="hljs-string">'example.org'</span>, <spa…
26316 <span class="hljs-comment">// ...</span>
26353 <li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference…
26355 …ozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;integer></a> …
26357 <li><code>tries</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struct…
26368 <h4><code>resolver.setLocalAddress([ipv4][, ipv6])</code><span><a class="mark" href="#dns_resolver_…
26373 <li><code>ipv4</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
26375ipv6</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Strin…
26379 This allows programs to specify outbound interfaces when used on multi-homed
26384 servers, and the v6 local address when making requests to IPv6 DNS servers.
26391 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Strin…
26393 …rings, formatted according to <a href="https://tools.ietf.org/html/rfc5952#section-6">RFC 5952</a>,
26396 <!-- eslint-disable semi-->
26397 <pre><code class="language-js">[
26398 <span class="hljs-string">'4.4.4.4'</span>,
26399 <span class="hljs-string">'2001:4860:4860::8888'</span>,
26400 <span class="hljs-string">'4.4.4.4:1053'</span>,
26401 <span class="hljs-string">'[2001:4860:4860::8888]:1053'</span>,
26418 <li><code>hostname</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_str…
26419 …zilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;integer></a> |…
26421 <li><code>family</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struc…
26422 <code>0</code> indicates that IPv4 and IPv6 addresses are both returned. <strong>Default:</strong>
26424 <li><code>hints</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struct…
26426 <li><code>all</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structur…
26428 …g/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type">&#x3C;boolean></a> When <co…
26430 IPv4 addresses are placed before IPv6 addresses.
26434 <a href="#cli_dns_result_order_order"><code>--dns-result-order</code></a>. New code should use <cod…
26437 <li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Referenc…
26439 <li><code>err</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Glo…
26440 …ozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> A…
26441 <li><code>family</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struc…
26442 the address is not an IPv4 or IPv6 address. <code>0</code> is a likely indicator of a
26448 AAAA (IPv6) record. All <code>option</code> properties are optional. If <code>options</code> is an
26450 and IPv6 addresses are both returned if found.</p>
26465 …code class="language-js"><span class="hljs-keyword">const</span> dns = <span class="hljs-built_in"…
26466 <span class="hljs-keyword">const</span> options = {
26467 <span class="hljs-attr">family</span>: <span class="hljs-number">6</span>,
26468 …<span class="hljs-attr">hints</span>: dns.<span class="hljs-property">ADDRCONFIG</span> | dns.<spa…
26470 …ss="hljs-title function_">lookup</span>(<span class="hljs-string">'example.com'</span>, options, <…
26471 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>…
26472 <span class="hljs-comment">// address: "2606:2800:220:1:248:1893:25c8:1946" family: IPv6</span>
26474 <span class="hljs-comment">// When options.all is true, the result will be an Array.</span>
26475 options.<span class="hljs-property">all</span> = <span class="hljs-literal">true</span>;
26476 …ss="hljs-title function_">lookup</span>(<span class="hljs-string">'example.com'</span>, options, <…
26477 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>…
26478 <span class="hljs-comment">// addresses: [{"address":"2606:2800:220:1:248:1893:25c8:1946","family":…
26494 <li><code>dns.ADDRCONFIG</code>: Limits returned address types to the types of non-loopback
26497 <li><code>dns.V4MAPPED</code>: If the IPv6 family was specified, but no IPv6 addresses were
26498 found, then return IPv4 mapped IPv6 addresses. It is not supported
26500 <li><code>dns.ALL</code>: If <code>dns.V4MAPPED</code> is specified, return resolved IPv6 addresses…
26501 well as IPv4 mapped IPv6 addresses.</li>
26508 <li><code>address</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_stru…
26509 <li><code>port</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
26510 <li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Referenc…
26512 <li><code>err</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Glo…
26513 <li><code>hostname</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_str…
26514 <li><code>service</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_stru…
26524 …code class="language-js"><span class="hljs-keyword">const</span> dns = <span class="hljs-built_in"…
26525-title function_">lookupService</span>(<span class="hljs-string">'127.0.0.1'</span>, <span class="…
26526 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>…
26527 <span class="hljs-comment">// Prints: localhost ssh</span>
26536 <li><code>hostname</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_str…
26537 <li><code>rrtype</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struc…
26538 <li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Referenc…
26540 <li><code>err</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Glo…
26541-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string[]></a> | <a href="h…
26632-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a></td><td><a hre…
26648 <li><code>hostname</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_str…
26649 <li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference…
26651 …eveloper.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type">&#x3C;bo…
26657 <li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Referenc…
26659 <li><code>err</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Glo…
26660 …illa.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string[]></a> |…
26681 <li><code>hostname</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_str…
26682 <li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference…
26684 …eveloper.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type">&#x3C;bo…
26690 <li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Referenc…
26692 <li><code>err</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Glo…
26693 …illa.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string[]></a> |…
26697 <p>Uses the DNS protocol to resolve a IPv6 addresses (<code>AAAA</code> records) for the
26699 will contain an array of IPv6 addresses.</p>
26702 <li><code>hostname</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_str…
26703 <li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Referenc…
26705 <li><code>err</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Glo…
26706 <li><code>ret</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Glo…
26764 …stname_callback"><code>dns.resolveSoa()</code></a></td></tr><tr><td><code>'SRV'</code></td><td>Ref…
26766 <!-- eslint-disable semi -->
26767-js">[ { <span class="hljs-attr">type</span>: <span class="hljs-string">'A'</span>, <span class="h…
26768 …<span class="hljs-attr">type</span>: <span class="hljs-string">'CNAME'</span>, <span class="hljs-a…
26769-attr">type</span>: <span class="hljs-string">'MX'</span>, <span class="hljs-attr">exchange</span>…
26770 …{ <span class="hljs-attr">type</span>: <span class="hljs-string">'NS'</span>, <span class="hljs-at…
26771 …span class="hljs-attr">type</span>: <span class="hljs-string">'TXT'</span>, <span class="hljs-attr…
26772 { <span class="hljs-attr">type</span>: <span class="hljs-string">'SOA'</span>,
26773 <span class="hljs-attr">nsname</span>: <span class="hljs-string">'ns1.example.com'</span>,
26774 <span class="hljs-attr">hostmaster</span>: <span class="hljs-string">'admin.example.com'</span>,
26775 <span class="hljs-attr">serial</span>: <span class="hljs-number">156696742</span>,
26776 <span class="hljs-attr">refresh</span>: <span class="hljs-number">900</span>,
26777 <span class="hljs-attr">retry</span>: <span class="hljs-number">900</span>,
26778 <span class="hljs-attr">expire</span>: <span class="hljs-number">1800</span>,
26779 <span class="hljs-attr">minttl</span>: <span class="hljs-number">60</span> } ]</code></pre>
26788 <li><code>hostname</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_str…
26789 <li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Referenc…
26791 <li><code>err</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Glo…
26792 <li><code>addresses</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_st…
26805 <li><code>hostname</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_str…
26806 <li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Referenc…
26808 <li><code>err</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Glo…
26809 <li><code>records</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference…
26822 <li><code>hostname</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_str…
26823 <li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Referenc…
26825 <li><code>err</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Glo…
26826 <li><code>addresses</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Referen…
26839 <li><code>hostname</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_str…
26840 <li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Referenc…
26842 <li><code>err</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Glo…
26843 <li><code>addresses</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Referen…
26858 <!-- eslint-skip -->
26859 <pre><code class="language-js">{
26860 <span class="hljs-attr">flags</span>: <span class="hljs-string">'s'</span>,
26861 <span class="hljs-attr">service</span>: <span class="hljs-string">'SIP+D2U'</span>,
26862 <span class="hljs-attr">regexp</span>: <span class="hljs-string">''</span>,
26863 …<span class="hljs-attr">replacement</span>: <span class="hljs-string">'_sip._udp.example.com'</spa…
26864 <span class="hljs-attr">order</span>: <span class="hljs-number">30</span>,
26865 <span class="hljs-attr">preference</span>: <span class="hljs-number">100</span>
26872 <li><code>hostname</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_str…
26873 <li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Referenc…
26875 <li><code>err</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Glo…
26876 <li><code>addresses</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_st…
26889 <li><code>hostname</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_str…
26890 <li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Referenc…
26892 <li><code>err</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Glo…
26893 <li><code>addresses</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_st…
26905 <li><code>hostname</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_str…
26906 <li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Referenc…
26908 <li><code>err</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Glo…
26909 <li><code>address</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference…
26925 <!-- eslint-skip -->
26926 <pre><code class="language-js">{
26927 <span class="hljs-attr">nsname</span>: <span class="hljs-string">'ns.example.com'</span>,
26928 <span class="hljs-attr">hostmaster</span>: <span class="hljs-string">'root.example.com'</span>,
26929 <span class="hljs-attr">serial</span>: <span class="hljs-number">2013101809</span>,
26930 <span class="hljs-attr">refresh</span>: <span class="hljs-number">10000</span>,
26931 <span class="hljs-attr">retry</span>: <span class="hljs-number">2400</span>,
26932 <span class="hljs-attr">expire</span>: <span class="hljs-number">604800</span>,
26933 <span class="hljs-attr">minttl</span>: <span class="hljs-number">3600</span>
26940 <li><code>hostname</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_str…
26941 <li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Referenc…
26943 <li><code>err</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Glo…
26944 <li><code>addresses</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Referen…
26948 <p>Uses the DNS protocol to resolve service records (<code>SRV</code> records) for the
26957 <!-- eslint-skip -->
26958 <pre><code class="language-js">{
26959 <span class="hljs-attr">priority</span>: <span class="hljs-number">10</span>,
26960 <span class="hljs-attr">weight</span>: <span class="hljs-number">5</span>,
26961 <span class="hljs-attr">port</span>: <span class="hljs-number">21223</span>,
26962 <span class="hljs-attr">name</span>: <span class="hljs-string">'service.example.com'</span>
26968 <!--lint disable no-undefined-references list-item-bullet-indent-->
26970 <li><code>hostname</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_str…
26971 <li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Referenc…
26973 <li><code>err</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Glo…
26974 <li><code>records</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_stru…
26978 <!--lint enable no-undefined-references list-item-bullet-indent-->
26981 two-dimensional array of the text records available for <code>hostname</code> (e.g.
26982 <code>[ ['v=spf1 ip4:0.0.0.0 ', '~all' ] ]</code>). Each sub-array contains TXT chunks of
26990 <li><code>ip</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structure…
26991 <li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Referenc…
26993 <li><code>err</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Glo…
26994 <li><code>hostnames</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_st…
26998 <p>Performs a reverse DNS query that resolves an IPv4 or IPv6 address to an
27007 <li><code>order</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struct…
27016 priority than <a href="#cli_dns_result_order_order"><code>--dns-result-order</code></a>. When using…
27024 …en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string[]></a> array of <…
27027 …argument is an array of <a href="https://tools.ietf.org/html/rfc5952#section-6">RFC 5952</a> forma…
27029 <pre><code class="language-js">dns.<span class="hljs-title function_">setServers</span>([
27030 <span class="hljs-string">'4.4.4.4'</span>,
27031 <span class="hljs-string">'[2001:4860:4860::8888]'</span>,
27032 <span class="hljs-string">'4.4.4.4:1053'</span>,
27033 <span class="hljs-string">'[2001:4860:4860::8888]:1053'</span>,
27042 <a href="https://man7.org/linux/man-pages/man5/resolv.conf.5.html">resolve.conf</a>.
27071-js"><span class="hljs-keyword">const</span> { <span class="hljs-title class_">Resolver</span> } =…
27072 <span class="hljs-keyword">const</span> resolver = <span class="hljs-keyword">new</span> <span clas…
27073 resolver.<span class="hljs-title function_">setServers</span>([<span class="hljs-string">'4.4.4.4'<…
27075 <span class="hljs-comment">// This request will use the server at 4.4.4.4, independent of global se…
27076-title function_">resolve4</span>(<span class="hljs-string">'example.org'</span>).<span class="hlj…
27077 <span class="hljs-comment">// ...</span>
27080 <span class="hljs-comment">// Alternatively, the same code can be written using async-await style.<…
27081 (<span class="hljs-keyword">async</span> <span class="hljs-keyword">function</span>(<span class="hl…
27082-keyword">const</span> addresses = <span class="hljs-keyword">await</span> resolver.<span class="h…
27114 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Strin…
27116 …rings, formatted according to <a href="https://tools.ietf.org/html/rfc5952#section-6">RFC 5952</a>,
27119 <!-- eslint-disable semi-->
27120 <pre><code class="language-js">[
27121 <span class="hljs-string">'4.4.4.4'</span>,
27122 <span class="hljs-string">'2001:4860:4860::8888'</span>,
27123 <span class="hljs-string">'4.4.4.4:1053'</span>,
27124 <span class="hljs-string">'[2001:4860:4860::8888]:1053'</span>,
27131 <li><code>hostname</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_str…
27132 …zilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;integer></a> |…
27134 <li><code>family</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struc…
27135 <code>0</code> indicates that IPv4 and IPv6 addresses are both returned. <strong>Default:</strong>
27137 <li><code>hints</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struct…
27139 <li><code>all</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structur…
27141 <li><code>verbatim</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_str…
27142 IPv6 addresses in the order the DNS resolver returned them. When <code>false</code>,
27143 IPv4 addresses are placed before IPv6 addresses.
27147 <a href="#cli_dns_result_order_order"><code>--dns-result-order</code></a>. New code should use <cod…
27152 AAAA (IPv6) record. All <code>option</code> properties are optional. If <code>options</code> is an
27154 and IPv6 addresses are both returned if found.</p>
27169 …code class="language-js"><span class="hljs-keyword">const</span> dns = <span class="hljs-built_in"…
27170 <span class="hljs-keyword">const</span> dnsPromises = dns.<span class="hljs-property">promises</spa…
27171 <span class="hljs-keyword">const</span> options = {
27172 <span class="hljs-attr">family</span>: <span class="hljs-number">6</span>,
27173 …<span class="hljs-attr">hints</span>: dns.<span class="hljs-property">ADDRCONFIG</span> | dns.<spa…
27176-title function_">lookup</span>(<span class="hljs-string">'example.com'</span>, options).<span cla…
27177-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hlj…
27178 <span class="hljs-comment">// address: "2606:2800:220:1:248:1893:25c8:1946" family: IPv6</span>
27181 <span class="hljs-comment">// When options.all is true, the result will be an Array.</span>
27182 options.<span class="hljs-property">all</span> = <span class="hljs-literal">true</span>;
27183-title function_">lookup</span>(<span class="hljs-string">'example.com'</span>, options).<span cla…
27184 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>…
27185 …<span class="hljs-comment">// addresses: [{"address":"2606:2800:220:1:248:1893:25c8:1946","family"…
27192 <li><code>address</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_stru…
27193 <li><code>port</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
27202-js"><span class="hljs-keyword">const</span> dnsPromises = <span class="hljs-built_in">require</sp…
27203-title function_">lookupService</span>(<span class="hljs-string">'127.0.0.1'</span>, <span class="…
27204-variable language_">console</span>.<span class="hljs-title function_">log</span>(result.<span cla…
27205 <span class="hljs-comment">// Prints: localhost ssh</span>
27212 <li><code>hostname</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_str…
27213 <li><code>rrtype</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struc…
27302-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a></td><td><a hre…
27310 <li><code>hostname</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_str…
27311 <li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference…
27313 …eveloper.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type">&#x3C;bo…
27328 <li><code>hostname</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_str…
27329 <li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference…
27331 …eveloper.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type">&#x3C;bo…
27338 <p>Uses the DNS protocol to resolve IPv6 addresses (<code>AAAA</code> records) for the
27339 <code>hostname</code>. On success, the <code>Promise</code> is resolved with an array of IPv6
27346 <li><code>hostname</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_str…
27402 …ostname"><code>dnsPromises.resolveSoa()</code></a></td></tr><tr><td><code>'SRV'</code></td><td>Ref…
27404 <!-- eslint-disable semi -->
27405-js">[ { <span class="hljs-attr">type</span>: <span class="hljs-string">'A'</span>, <span class="h…
27406 …<span class="hljs-attr">type</span>: <span class="hljs-string">'CNAME'</span>, <span class="hljs-a…
27407-attr">type</span>: <span class="hljs-string">'MX'</span>, <span class="hljs-attr">exchange</span>…
27408 …{ <span class="hljs-attr">type</span>: <span class="hljs-string">'NS'</span>, <span class="hljs-at…
27409 …span class="hljs-attr">type</span>: <span class="hljs-string">'TXT'</span>, <span class="hljs-attr…
27410 { <span class="hljs-attr">type</span>: <span class="hljs-string">'SOA'</span>,
27411 <span class="hljs-attr">nsname</span>: <span class="hljs-string">'ns1.example.com'</span>,
27412 <span class="hljs-attr">hostmaster</span>: <span class="hljs-string">'admin.example.com'</span>,
27413 <span class="hljs-attr">serial</span>: <span class="hljs-number">156696742</span>,
27414 <span class="hljs-attr">refresh</span>: <span class="hljs-number">900</span>,
27415 <span class="hljs-attr">retry</span>: <span class="hljs-number">900</span>,
27416 <span class="hljs-attr">expire</span>: <span class="hljs-number">1800</span>,
27417 <span class="hljs-attr">minttl</span>: <span class="hljs-number">60</span> } ]</code></pre>
27423 <li><code>hostname</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_str…
27434 <li><code>hostname</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_str…
27444 <li><code>hostname</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_str…
27455 <li><code>hostname</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_str…
27468 <!-- eslint-skip -->
27469 <pre><code class="language-js">{
27470 <span class="hljs-attr">flags</span>: <span class="hljs-string">'s'</span>,
27471 <span class="hljs-attr">service</span>: <span class="hljs-string">'SIP+D2U'</span>,
27472 <span class="hljs-attr">regexp</span>: <span class="hljs-string">''</span>,
27473 …<span class="hljs-attr">replacement</span>: <span class="hljs-string">'_sip._udp.example.com'</spa…
27474 <span class="hljs-attr">order</span>: <span class="hljs-number">30</span>,
27475 <span class="hljs-attr">preference</span>: <span class="hljs-number">100</span>
27482 <li><code>hostname</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_str…
27493 <li><code>hostname</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_str…
27503 <li><code>hostname</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_str…
27517 <!-- eslint-skip -->
27518 <pre><code class="language-js">{
27519 <span class="hljs-attr">nsname</span>: <span class="hljs-string">'ns.example.com'</span>,
27520 <span class="hljs-attr">hostmaster</span>: <span class="hljs-string">'root.example.com'</span>,
27521 <span class="hljs-attr">serial</span>: <span class="hljs-number">2013101809</span>,
27522 <span class="hljs-attr">refresh</span>: <span class="hljs-number">10000</span>,
27523 <span class="hljs-attr">retry</span>: <span class="hljs-number">2400</span>,
27524 <span class="hljs-attr">expire</span>: <span class="hljs-number">604800</span>,
27525 <span class="hljs-attr">minttl</span>: <span class="hljs-number">3600</span>
27532 <li><code>hostname</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_str…
27534 <p>Uses the DNS protocol to resolve service records (<code>SRV</code> records) for the
27543 <!-- eslint-skip -->
27544 <pre><code class="language-js">{
27545 <span class="hljs-attr">priority</span>: <span class="hljs-number">10</span>,
27546 <span class="hljs-attr">weight</span>: <span class="hljs-number">5</span>,
27547 <span class="hljs-attr">port</span>: <span class="hljs-number">21223</span>,
27548 <span class="hljs-attr">name</span>: <span class="hljs-string">'service.example.com'</span>
27555 <li><code>hostname</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_str…
27558 <code>hostname</code>. On success, the <code>Promise</code> is resolved with a two-dimensional array
27560 <code>[ ['v=spf1 ip4:0.0.0.0 ', '~all' ] ]</code>). Each sub-array contains TXT chunks of
27568 <li><code>ip</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structure…
27570 <p>Performs a reverse DNS query that resolves an IPv4 or IPv6 address to an
27579 <li><code>order</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struct…
27588 higher priority than <a href="#cli_dns_result_order_order"><code>--dns-result-order</code></a>. Whe…
27596 …en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string[]></a> array of <…
27599 …argument is an array of <a href="https://tools.ietf.org/html/rfc5952#section-6">RFC 5952</a> forma…
27601 <pre><code class="language-js">dnsPromises.<span class="hljs-title function_">setServers</span>([
27602 <span class="hljs-string">'4.4.4.4'</span>,
27603 <span class="hljs-string">'[2001:4860:4860::8888]'</span>,
27604 <span class="hljs-string">'4.4.4.4:1053'</span>,
27605 <span class="hljs-string">'[2001:4860:4860::8888]:1053'</span>,
27611 <a href="https://man7.org/linux/man-pages/man5/resolv.conf.5.html">resolve.conf</a>.
27639 <li><code>dns.NOTINITIALIZED</code>: c-ares library initialization not yet performed.</li>
27653 resolve a given name the same way as the <code>ping</code> command. On most POSIX-like
27655 …://man7.org/linux/man-pages/man5/nsswitch.conf.5.html"><code>nsswitch.conf(5)</code></a> and/or <a…
27659 perspective, it is implemented as a synchronous call to <a href="http://man7.org/linux/man-pages/ma…
27670 do not use <a href="http://man7.org/linux/man-pages/man3/getaddrinfo.3.html"><code>getaddrinfo(3)</…
27693 … api_stability_0"><a href="#documentation_stability_index">Stability: 0</a> - Deprecated</div><p><…
27711 <p>By the very nature of how <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Refer…
27721 <p>In this way, <code>domain</code> usage goes hand-in-hand with the cluster module,
27727 <pre><code class="language-js"><span class="hljs-comment">// XXX WARNING! BAD IDEA!</span>
27729 …class="hljs-keyword">const</span> d = <span class="hljs-built_in">require</span>(<span class="hljs
27730 ….<span class="hljs-title function_">on</span>(<span class="hljs-string">'error'</span>, <span clas…
27731 <span class="hljs-comment">// The error won't crash the process, but what it does is worse!</span>
27732 …<span class="hljs-comment">// Though we've prevented abrupt process restarting, we are leaking</sp…
27733 <span class="hljs-comment">// a lot of resources if this ever happens.</span>
27734 <span class="hljs-comment">// This is no better than process.on('uncaughtException')!</span>
27735 …ljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="…
27737 d.<span class="hljs-title function_">run</span>(<span class="hljs-function">() =></span> {
27738-built_in">require</span>(<span class="hljs-string">'http'</span>).<span class="hljs-title functio…
27739 <span class="hljs-title function_">handleRequest</span>(req, res);
27740 …}).<span class="hljs-title function_">listen</span>(<span class="hljs-variable constant_">PORT</sp…
27745 <pre><code class="language-js"><span class="hljs-comment">// Much better!</span>
27747 <span class="hljs-keyword">const</span> cluster = <span class="hljs-built_in">require</span>(<span …
27748-keyword">const</span> <span class="hljs-variable constant_">PORT</span> = +process.<span class="h…
27750 <span class="hljs-keyword">if</span> (cluster.<span class="hljs-property">isMaster</span>) {
27751 <span class="hljs-comment">// A more realistic scenario would have more than 2 workers,</span>
27752 <span class="hljs-comment">// and perhaps not put the master and worker in the same file.</span>
27753 <span class="hljs-comment">//</span>
27754 <span class="hljs-comment">// It is also possible to get a bit fancier about logging, and</span>
27755 <span class="hljs-comment">// implement whatever custom logic is needed to prevent DoS</span>
27756 <span class="hljs-comment">// attacks and other bad behavior.</span>
27757 <span class="hljs-comment">//</span>
27758 <span class="hljs-comment">// See the options in the cluster documentation.</span>
27759 <span class="hljs-comment">//</span>
27760 <span class="hljs-comment">// The important thing is that the master does very little,</span>
27761 <span class="hljs-comment">// increasing our resilience to unexpected errors.</span>
27763 cluster.<span class="hljs-title function_">fork</span>();
27764 cluster.<span class="hljs-title function_">fork</span>();
27766 …pan class="hljs-title function_">on</span>(<span class="hljs-string">'disconnect'</span>, <span cl…
27767 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">error</spa…
27768 cluster.<span class="hljs-title function_">fork</span>();
27771 } <span class="hljs-keyword">else</span> {
27772 <span class="hljs-comment">// the worker</span>
27773 <span class="hljs-comment">//</span>
27774 <span class="hljs-comment">// This is where we put our bugs!</span>
27776 …<span class="hljs-keyword">const</span> domain = <span class="hljs-built_in">require</span>(<span …
27778 <span class="hljs-comment">// See the cluster documentation for more details about using</span>
27779 …<span class="hljs-comment">// worker processes to serve requests. How it works, caveats, etc.</spa…
27781-keyword">const</span> server = <span class="hljs-built_in">require</span>(<span class="hljs-strin…
27782 …<span class="hljs-keyword">const</span> d = domain.<span class="hljs-title function_">create</span…
27783 ….<span class="hljs-title function_">on</span>(<span class="hljs-string">'error'</span>, <span clas…
27784 …ass="hljs-variable language_">console</span>.<span class="hljs-title function_">error</span>(<span…
27786 <span class="hljs-comment">// We're in dangerous territory!</span>
27787 <span class="hljs-comment">// By definition, something unexpected occurred,</span>
27788 <span class="hljs-comment">// which we probably didn't want.</span>
27789 <span class="hljs-comment">// Anything can happen now! Be very careful!</span>
27791 <span class="hljs-keyword">try</span> {
27792 <span class="hljs-comment">// Make sure we close down within 30 seconds</span>
27793 …<span class="hljs-keyword">const</span> killtimer = <span class="hljs-built_in">setTimeout</span>(…
27794 … process.<span class="hljs-title function_">exit</span>(<span class="hljs-number">1</span>);
27795 }, <span class="hljs-number">30000</span>);
27796 <span class="hljs-comment">// But don't keep the process open just for that!</span>
27797 killtimer.<span class="hljs-title function_">unref</span>();
27799 <span class="hljs-comment">// Stop taking new requests.</span>
27800 server.<span class="hljs-title function_">close</span>();
27802 <span class="hljs-comment">// Let the master know we're dead. This will trigger a</span>
27803 … <span class="hljs-comment">// 'disconnect' in the cluster master, and then it will fork</span>
27804 <span class="hljs-comment">// a new worker.</span>
27805 …cluster.<span class="hljs-property">worker</span>.<span class="hljs-title function_">disconnect</s…
27807 …<span class="hljs-comment">// Try to send an error to the request that triggered the problem</span>
27808 res.<span class="hljs-property">statusCode</span> = <span class="hljs-number">500</span>;
27809 …res.<span class="hljs-title function_">setHeader</span>(<span class="hljs-string">'content-type'</…
27810 …res.<span class="hljs-title function_">end</span>(<span class="hljs-string">'Oops, there was a pro…
27811 } <span class="hljs-keyword">catch</span> (er2) {
27812 <span class="hljs-comment">// Oh well, not much we can do at this point.</span>
27813 …js-variable language_">console</span>.<span class="hljs-title function_">error</span>(<span class=…
27817 … <span class="hljs-comment">// Because req and res were created before this domain existed,</span>
27818 <span class="hljs-comment">// we need to explicitly add them.</span>
27819 <span class="hljs-comment">// See the explanation of implicit vs explicit binding below.</span>
27820 d.<span class="hljs-title function_">add</span>(req);
27821 d.<span class="hljs-title function_">add</span>(res);
27823 <span class="hljs-comment">// Now run the handler function in the domain.</span>
27824 d.<span class="hljs-title function_">run</span>(<span class="hljs-function">() =></span> {
27825 <span class="hljs-title function_">handleRequest</span>(req, res);
27828 …server.<span class="hljs-title function_">listen</span>(<span class="hljs-variable constant_">PORT…
27831 <span class="hljs-comment">// This part is not important. Just an example routing thing.</span>
27832 <span class="hljs-comment">// Put fancy application logic here.</span>
27833 <span class="hljs-keyword">function</span> <span class="hljs-title function_">handleRequest</span>(…
27834 <span class="hljs-keyword">switch</span> (req.<span class="hljs-property">url</span>) {
27835 <span class="hljs-keyword">case</span> <span class="hljs-string">'/error'</span>:
27836 <span class="hljs-comment">// We do some async stuff, and then...</span>
27837 <span class="hljs-built_in">setTimeout</span>(<span class="hljs-function">() =></span> {
27838 <span class="hljs-comment">// Whoops!</span>
27839 flerb.<span class="hljs-title function_">bark</span>();
27841 <span class="hljs-keyword">break</span>;
27842 <span class="hljs-attr">default</span>:
27843 res.<span class="hljs-title function_">end</span>(<span class="hljs-string">'ok'</span>);
27865 to <code>fs.open()</code>, or other callback-taking methods) will automatically be
27887 <pre><code class="language-js"><span class="hljs-comment">// Create a top-level domain for the serv…
27888 <span class="hljs-keyword">const</span> domain = <span class="hljs-built_in">require</span>(<span c…
27889 <span class="hljs-keyword">const</span> http = <span class="hljs-built_in">require</span>(<span cla…
27890 <span class="hljs-keyword">const</span> serverDomain = domain.<span class="hljs-title function_">cr…
27892 serverDomain.<span class="hljs-title function_">run</span>(<span class="hljs-function">() =></span>…
27893 <span class="hljs-comment">// Server is created in the scope of serverDomain</span>
27894 …http.<span class="hljs-title function_">createServer</span>(<span class="hljs-function">(<span cla…
27895 <span class="hljs-comment">// Req and res are also created in the scope of serverDomain</span>
27896 …<span class="hljs-comment">// however, we'd prefer to have a separate domain for each request.</sp…
27897 <span class="hljs-comment">// create it first thing, and add req and res to it.</span>
27898 …<span class="hljs-keyword">const</span> reqd = domain.<span class="hljs-title function_">create</s…
27899 reqd.<span class="hljs-title function_">add</span>(req);
27900 reqd.<span class="hljs-title function_">add</span>(res);
27901 ….<span class="hljs-title function_">on</span>(<span class="hljs-string">'error'</span>, <span clas…
27902-variable language_">console</span>.<span class="hljs-title function_">error</span>(<span class="h…
27903 <span class="hljs-keyword">try</span> {
27904 … res.<span class="hljs-title function_">writeHead</span>(<span class="hljs-number">500</span>);
27905 …res.<span class="hljs-title function_">end</span>(<span class="hljs-string">'Error occurred, sorry…
27906 } <span class="hljs-keyword">catch</span> (er2) {
27907-variable language_">console</span>.<span class="hljs-title function_">error</span>(<span class="h…
27910 }).<span class="hljs-title function_">listen</span>(<span class="hljs-number">1337</span>);
27925 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array…
27944 <li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Referenc…
27945 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Obje…
27950 <pre><code class="language-js"><span class="hljs-keyword">const</span> d = domain.<span class="hljs
27952 <span class="hljs-keyword">function</span> <span class="hljs-title function_">readSomeFile</span>(<…
27953-title function_">readFile</span>(filename, <span class="hljs-string">'utf8'</span>, d.<span class…
27954 <span class="hljs-comment">// If this throws, it will also be passed to the domain.</span>
27955-keyword">return</span> <span class="hljs-title function_">cb</span>(er, data ? <span class="hljs-
27959 ….<span class="hljs-title function_">on</span>(<span class="hljs-string">'error'</span>, <span clas…
27960 …<span class="hljs-comment">// An error occurred somewhere. If we throw it now, it will crash the p…
27961 <span class="hljs-comment">// with the normal line number and stack message.</span>
27986 <li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Referenc…
27987 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Obje…
27994 <pre><code class="language-js"><span class="hljs-keyword">const</span> d = domain.<span class="hljs
27996 <span class="hljs-keyword">function</span> <span class="hljs-title function_">readSomeFile</span>(<…
27997-title function_">readFile</span>(filename, <span class="hljs-string">'utf8'</span>, d.<span class…
27998 <span class="hljs-comment">// Note, the first argument is never passed to the</span>
27999 <span class="hljs-comment">// callback since it is assumed to be the 'Error' argument</span>
28000 <span class="hljs-comment">// and thus intercepted by the domain.</span>
28002 <span class="hljs-comment">// If this throws, it will also be passed to the domain</span>
28003 <span class="hljs-comment">// so the error-handling logic can be moved to the 'error'</span>
28004 <span class="hljs-comment">// event on the domain instead of being repeated throughout</span>
28005 <span class="hljs-comment">// the program.</span>
28006-keyword">return</span> <span class="hljs-title function_">cb</span>(<span class="hljs-literal">nu…
28010 ….<span class="hljs-title function_">on</span>(<span class="hljs-string">'error'</span>, <span clas…
28011 …<span class="hljs-comment">// An error occurred somewhere. If we throw it now, it will crash the p…
28012 <span class="hljs-comment">// with the normal line number and stack message.</span>
28022 <li><code>fn</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Glob…
28023 <li><code>...args</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_stru…
28030 …ode class="language-js"><span class="hljs-keyword">const</span> domain = <span class="hljs-built_i…
28031 <span class="hljs-keyword">const</span> fs = <span class="hljs-built_in">require</span>(<span class…
28032 <span class="hljs-keyword">const</span> d = domain.<span class="hljs-title function_">create</span>…
28033 ….<span class="hljs-title function_">on</span>(<span class="hljs-string">'error'</span>, <span clas…
28034 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">error</spa…
28036 d.<span class="hljs-title function_">run</span>(<span class="hljs-function">() =></span> {
28037 …process.<span class="hljs-title function_">nextTick</span>(<span class="hljs-function">() =></span…
28038 …<span class="hljs-built_in">setTimeout</span>(<span class="hljs-function">() =></span> { <span cla…
28039-title function_">open</span>(<span class="hljs-string">'non-existent file'</span>, <span class="h…
28040 <span class="hljs-keyword">if</span> (er) <span class="hljs-keyword">throw</span> er;
28041 <span class="hljs-comment">// proceed...</span>
28043 }, <span class="hljs-number">100</span>);
28051 <pre><code class="language-js"><span class="hljs-keyword">const</span> d1 = domain.<span class="hlj…
28052 <span class="hljs-keyword">const</span> d2 = domain.<span class="hljs-title function_">create</span…
28054 <span class="hljs-keyword">let</span> p;
28055 d1.<span class="hljs-title function_">run</span>(<span class="hljs-function">() =></span> {
28056 …p = <span class="hljs-title class_">Promise</span>.<span class="hljs-title function_">resolve</spa…
28059 d2.<span class="hljs-title function_">run</span>(<span class="hljs-function">() =></span> {
28060 …p.<span class="hljs-title function_">then</span>(<span class="hljs-function">(<span class="hljs-pa…
28061 <span class="hljs-comment">// running in d2</span>
28065 <pre><code class="language-js"><span class="hljs-keyword">const</span> d1 = domain.<span class="hlj…
28066 <span class="hljs-keyword">const</span> d2 = domain.<span class="hljs-title function_">create</span…
28068 <span class="hljs-keyword">let</span> p;
28069 d1.<span class="hljs-title function_">run</span>(<span class="hljs-function">() =></span> {
28070 …p = <span class="hljs-title class_">Promise</span>.<span class="hljs-title function_">resolve</spa…
28073 d2.<span class="hljs-title function_">run</span>(<span class="hljs-function">() =></span> {
28074-title function_">then</span>(p.<span class="hljs-property">domain</span>.<span class="hljs-title …
28075 <span class="hljs-comment">// running in d1</span>
28088-US/docs/Web/JavaScript/Reference/Global_Objects/EvalError" class="type">&#x3C;EvalError></a>, <a …
28089-US/docs/Web/JavaScript/Reference/Global_Objects/ReferenceError" class="type">&#x3C;ReferenceError…
28093 <li>User-specified errors triggered by application code.</li>
28099 instances of, the standard JavaScript <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScr…
28109 are handled using the <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/St…
28111 <pre><code class="language-js"><span class="hljs-comment">// Throws with a ReferenceError because z…
28112 <span class="hljs-keyword">try</span> {
28113 <span class="hljs-keyword">const</span> m = <span class="hljs-number">1</span>;
28114 <span class="hljs-keyword">const</span> n = m + z;
28115 } <span class="hljs-keyword">catch</span> (err) {
28116 <span class="hljs-comment">// Handle the error here.</span>
28131 <!-- eslint-disable no-useless-return -->
28132 …<code class="language-js"><span class="hljs-keyword">const</span> fs = <span class="hljs-built_in"…
28133 …hljs-title function_">readFile</span>(<span class="hljs-string">'a file that does not exist'</span…
28134 <span class="hljs-keyword">if</span> (err) {
28135 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">error</spa…
28136 <span class="hljs-keyword">return</span>;
28138 <span class="hljs-comment">// Otherwise handle the data</span>
28144 …code class="language-js"><span class="hljs-keyword">const</span> net = <span class="hljs-built_in"…
28145 <span class="hljs-keyword">const</span> connection = net.<span class="hljs-title function_">connect…
28147 <span class="hljs-comment">// Adding an 'error' event handler to a stream:</span>
28148 ….<span class="hljs-title function_">on</span>(<span class="hljs-string">'error'</span>, <span clas…
28149 <span class="hljs-comment">// If the connection is reset by the server, or if it can't</span>
28150 <span class="hljs-comment">// connect at all, or on any sort of error encountered by</span>
28151 <span class="hljs-comment">// the connection, the error will be sent here.</span>
28152 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">error</spa…
28155 connection.<span class="hljs-title function_">pipe</span>(process.<span class="hljs-property">stdou…
28165 … the <code>'error'</code> event mechanism is most common for <a href="stream.html">stream-based</a>
28166 and <a href="#events_class_eventemitter">event emitter-based</a> APIs, which themselves represent a…
28174-js"><span class="hljs-keyword">const</span> <span class="hljs-title class_">EventEmitter</span> =…
28175 <span class="hljs-keyword">const</span> ee = <span class="hljs-keyword">new</span> <span class="hlj…
28177 <span class="hljs-title function_">setImmediate</span>(<span class="hljs-function">() =></span> {
28178 <span class="hljs-comment">// This will crash the process because no 'error' event</span>
28179 <span class="hljs-comment">// handler has been added.</span>
28180-title function_">emit</span>(<span class="hljs-string">'error'</span>, <span class="hljs-keyword"…
28186 <h4>Error-first callbacks<span><a class="mark" href="#errors_error_first_callbacks" id="errors_erro…
28189 pattern referred to as an <em>error-first callback</em>. With this pattern, a callback
28194 …<code class="language-js"><span class="hljs-keyword">const</span> fs = <span class="hljs-built_in"…
28196 <span class="hljs-keyword">function</span> <span class="hljs-title function_">errorFirstCallback</s…
28197 <span class="hljs-keyword">if</span> (err) {
28198 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">error</spa…
28199 <span class="hljs-keyword">return</span>;
28201 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>…
28204 fs.<span class="hljs-title function_">readFile</span>(<span class="hljs-string">'/some/file/that/do…
28205 fs.<span class="hljs-title function_">readFile</span>(<span class="hljs-string">'/some/file/that/do…
28208 use <code>throw</code> inside an error-first callback:</p>
28209 <pre><code class="language-js"><span class="hljs-comment">// THIS WILL NOT WORK:</span>
28210 <span class="hljs-keyword">const</span> fs = <span class="hljs-built_in">require</span>(<span class…
28212 <span class="hljs-keyword">try</span> {
28213 …js-title function_">readFile</span>(<span class="hljs-string">'/some/file/that/does-not-exist'</sp…
28214 <span class="hljs-comment">// Mistaken assumption: throwing here...</span>
28215 <span class="hljs-keyword">if</span> (err) {
28216 <span class="hljs-keyword">throw</span> err;
28219 } <span class="hljs-keyword">catch</span> (err) {
28220 <span class="hljs-comment">// This will not catch the throw!</span>
28221 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">error</spa…
28231 <p>A generic JavaScript <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/…
28239 <li><code>message</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_stru…
28245 are dependent on <a href="https://github.com/v8/v8/wiki/Stack-Trace-API">V8's stack trace API</a>. …
28250 <li><code>targetObject</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Refe…
28251 <li><code>constructorOpt</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Re…
28256 <pre><code class="language-js"><span class="hljs-keyword">const</span> myObject = {};
28257 <span class="hljs-title class_">Error</span>.<span class="hljs-title function_">captureStackTrace</…
28258 myObject.<span class="hljs-property">stack</span>; <span class="hljs-comment">// Similar to `new E…
28266 …de class="language-js"><span class="hljs-keyword">function</span> <span class="hljs-title function…
28267-title class_">Error</span>.<span class="hljs-title function_">captureStackTrace</span>(<span clas…
28270 <span class="hljs-comment">// Without passing MyError to captureStackTrace, the MyError</span>
28271 <span class="hljs-comment">// frame would show up in the .stack property. By passing</span>
28272 <span class="hljs-comment">// the constructor, we omit that frame, and retain all frames below it.<…
28273 <span class="hljs-keyword">new</span> <span class="hljs-title class_">MyError</span>().<span class=…
28276 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" c…
28283 <p>If set to a non-number value, or set to a negative number, stack traces will
28287 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" c…
28292 change between any versions of Node.js. See <a href="#nodejs-error-codes">Node.js error codes</a> f…
28296 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" c…
28304-js"><span class="hljs-keyword">const</span> err = <span class="hljs-keyword">new</span> <span cla…
28305 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">error</span…
28306 <span class="hljs-comment">// Prints: The message</span></code></pre>
28309 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" c…
28313 <pre><code class="language-console">Error: Things keep happening!
28315 at Frobnicator.refrobulate (/home/gbusey/business-logic.js:424:21)
28331 …language-js"><span class="hljs-keyword">const</span> cheetahify = <span class="hljs-built_in">requ…
28333 <span class="hljs-keyword">function</span> <span class="hljs-title function_">makeFaster</span>(<sp…
28334 <span class="hljs-comment">// `cheetahify()` *synchronously* calls speedy.</span>
28335 …ljs-title function_">cheetahify</span>(<span class="hljs-keyword">function</span> <span class="hlj…
28336 …an class="hljs-keyword">throw</span> <span class="hljs-keyword">new</span> <span class="hljs-title…
28340 <span class="hljs-title function_">makeFaster</span>();
28341 <span class="hljs-comment">// will throw:</span>
28342 <span class="hljs-comment">// /home/gbusey/file.js:6</span>
28343 <span class="hljs-comment">// throw new Error('oh no!');</span>
28344 <span class="hljs-comment">// ^</span>
28345 <span class="hljs-comment">// Error: oh no!</span>
28346 <span class="hljs-comment">// at speedy (/home/gbusey/file.js:6:11)</span>
28347 <span class="hljs-comment">// at makeFaster (/home/gbusey/file.js:5:3)</span>
28348 <span class="hljs-comment">// at Object.&#x3C;anonymous> (/home/gbusey/file.js:10:1)</span>
28349 <span class="hljs-comment">// at Module._compile (module.js:456:26)</span>
28350 <span class="hljs-comment">// at Object.Module._extensions..js (module.js:474:10)</span>
28351 <span class="hljs-comment">// at Module.load (module.js:356:32)</span>
28352 <span class="hljs-comment">// at Function.Module._load (module.js:312:12)</span>
28353 <span class="hljs-comment">// at Function.Module.runMain (module.js:497:10)</span>
28354 <span class="hljs-comment">// at startup (node.js:119:16)</span>
28355 <span class="hljs-comment">// at node.js:906:3</span></code></pre>
28359 <li><code>plain-filename.js:line:column</code>, if the frame represents a call internal
28382-js"><span class="hljs-built_in">require</span>(<span class="hljs-string">'net'</span>).<span clas…
28383 <span class="hljs-comment">// Throws "RangeError: "port" option should be >= 0 and &#x3C; 65536: -1…
28395 <pre><code class="language-js">doesNotExist;
28396 <span class="hljs-comment">// Throws ReferenceError, doesNotExist is not a variable in this program…
28407 <pre><code class="language-js"><span class="hljs-keyword">try</span> {
28408 …"hljs-built_in">require</span>(<span class="hljs-string">'vm'</span>).<span class="hljs-title func…
28409 } <span class="hljs-keyword">catch</span> (err) {
28410 <span class="hljs-comment">// 'err' will be a SyntaxError.</span>
28423 <li><code>address</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_stru…
28425 <li><code>code</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
28426 <li><code>dest</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
28428 …ps://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x…
28429 <li><code>info</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Gl…
28430 …veloper.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;stri…
28431 <li><code>path</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
28432 <li><code>port</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
28433 <li><code>syscall</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_stru…
28437 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" c…
28443 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" c…
28448 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" c…
28454 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" c…
28463 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Objec…
28468 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" c…
28470 <p><code>error.message</code> is a system-provided human-readable description of the error.</p>
28473 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" c…
28478 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" c…
28483 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" c…
28485 …ode> property is a string describing the <a href="https://man7.org/linux/man-pages/man2/syscalls.2…
28487 <p>This is a list of system errors commonly-encountered when writing a Node.js
28488 program. For a comprehensive list, see the <a href="https://man7.org/linux/man-pages/man3/errno.3.h…
28501 target machine actively refused it. This usually results from trying to
28511 <p><code>EEXIST</code> (File exists): An existing file was the target of an operation that
28512 required that the target not exist.</p>
28525 <code>ulimit -n 2048</code> in the same shell that will run the Node.js process.</p>
28537 <p><code>ENOTEMPTY</code> (Directory not empty): A directory with entries was the target
28567-js"><span class="hljs-built_in">require</span>(<span class="hljs-string">'url'</span>).<span clas…
28568 <span class="hljs-comment">// Throws TypeError, since it expected a string.</span></code></pre>
28574 operation or as the target of a <code>throw</code> statement. While it is not required
28584 OpenSSL-specific properties.</p>
28593 <p>A human-readable string describing the reason for the error.</p>
28594 <p><a id="nodejs-error-codes"></a></p>
28668 <!--
28671 - version: 11.12.0
28672 pr-url: https://github.com/nodejs/node/pull/26487
28674 - version: v14.17.1
28675 pr-url: https://github.com/nodejs/node/pull/38510
28677 -->
28683 non-writable <code>stdout</code> or <code>stderr</code> stream.</p>
28686 <!--
28688 -->
28721 <p>The <a href="#cli_force_fips"><code>--force-fips</code></a> command-line argument was used but t…
28774 <p>An unknown Diffie-Hellman group name was given. See
28781 <p>Loading native addons has been disabled using <a href="#cli_no_addons"><code>--no-addons</code><…
28812 <p><code>c-ares</code> failed to set the DNS server.</p>
28835 <p><code>--print</code> cannot be used with ESM input.</p>
28874 <p>Status code was outside the regular status code range (100-999).</p>
28887 <p>For HTTP/2 requests using the <code>CONNECT</code> method, the <code>:authority</code> pseudo-he…
28891 <p>For HTTP/2 requests using the <code>CONNECT</code> method, the <code>:path</code> pseudo-header …
28895 <p>For HTTP/2 requests using the <code>CONNECT</code> method, the <code>:scheme</code> pseudo-heade…
28899 <p>A non-specific HTTP/2 error has occurred.</p>
28988 <p>An HTTP/2 pseudo-header has been used inappropriately. Pseudo-headers are header
29005 <p>The <code>Http2Session</code> closed with a non-zero error code.</p>
29030 <p>A non-zero error code was been specified in an <code>RST_STREAM</code> frame.</p>
29055 … api_stability_1"><a href="#documentation_stability_index">Stability: 1</a> - Experimental</div><p…
29056 <p>The <code>--input-type</code> flag was used to attempt to execute a file. This flag can
29057 only be used with input via <code>--eval</code>, <code>--print</code> or <code>STDIN</code>.</p>
29104 less than -1 should never happen.</p>
29129 situation can only occur on Unix-like systems where only <code>localhost</code> or an empty
29135 semantics for determining whether a path can be used is platform-dependent.</p>
29169 …de> <a href="#packages_exports"><code>"exports"</code></a> field contains an invalid target mapping
29206-js"><span class="hljs-keyword">const</span> urlSearchParams = <span class="hljs-keyword">new</spa…
29208 …="hljs-keyword">const</span> buf = <span class="hljs-title class_">Buffer</span>.<span class="hljs
29209 …SearchParams.<span class="hljs-property">has</span>.<span class="hljs-title function_">call</span>…
29210 <span class="hljs-comment">// Throws a TypeError with code 'ERR_INVALID_THIS'</span></code></pre>
29300 …ker_threads_class_messageport"><code>MessagePort</code></a> could not be deserialized in the target
29333 … api_stability_1"><a href="#documentation_stability_index">Stability: 1</a> - Experimental</div><p…
29343 <p>While using <code>Node-API</code>, a constructor passed was not a function.</p>
29357 <p>An error occurred while invoking the JavaScript portion of the thread-safe
29366 thread-safe function in an idle loop. This error indicates that an error
29382 <p>A non-context-aware native addon was loaded in a process that disallows them.</p>
29402 … href="#cli_disable_proto_mode"><code>--disable-proto=throw</code></a>. <a href="https://developer…
29403 <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/se…
29407 … api_stability_1"><a href="#documentation_stability_index">Stability: 1</a> - Experimental</div><p…
29461 <a href="https://www.w3.org/TR/SRI/#the-integrity-attribute">Subresource Integrity specification</a…
29492-js"><span class="hljs-keyword">const</span> <span class="hljs-title class_">Socket</span> = <span…
29493 <span class="hljs-keyword">const</span> instance = <span class="hljs-keyword">new</span> <span clas…
29495 instance.<span class="hljs-title function_">setEncoding</span>(<span class="hljs-string">'utf8'</sp…
29510 <p>An unspecified or non-specific system error has occurred within the Node.js
29515 <p>This error is thrown by <code>checkServerIdentity</code> if a user-supplied
29525 <p>While using TLS, the parameter offered for the Diffie-Hellman (<code>DH</code>)
29526 key-agreement protocol is too small. By default, the key length must be greater
29570 vector for denial-of-service attacks.</p>
29573 <p>An attempt was made to issue Server Name Indication from a TLS server-side
29582 the <code>--without-v8-platform</code> flag.</p>
29622 … api_stability_1"><a href="#documentation_stability_index">Stability: 1</a> - Experimental</div><p…
29627 … api_stability_1"><a href="#documentation_stability_index">Stability: 1</a> - Experimental</div><p…
29636 <a href="#packages_self_referencing_a_package_using_its_name">self-reference a package using its na…
29638 <!-- eslint-skip -->
29639 …pre><code class="language-js"><span class="hljs-keyword">import</span> <span class="hljs-string">'…
29640 <span class="hljs-keyword">import</span> <span class="hljs-string">'./index.js'</span>; <span class…
29641 <span class="hljs-keyword">import</span> <span class="hljs-string">'package-name'</span>; <span cla…
29737 <p>Server is sending both a <code>Content-Length</code> header and <code>Transfer-Encoding: chunked…
29738 <p><code>Transfer-Encoding: chunked</code> allows the server to maintain an HTTP persistent
29740 In this case, the <code>Content-Length</code> HTTP header cannot be used.</p>
29741 <p>Use <code>Content-Length</code> or <code>Transfer-Encoding: chunked</code>.</p>
29756 … api_stability_0"><a href="#documentation_stability_index">Stability: 0</a> - Deprecated. These er…
29760 <!--
29763 -->
29771 <p>The UTF-16 encoding was used with <a href="#crypto_hash_digest_encoding"><code>hash.digest()</co…
29773 causing the method to return a string rather than a <code>Buffer</code>, the UTF-16
29829 <p>Used by the <code>Node-API</code> when <code>Constructor.prototype</code> is not an object.</p>
29895 <p>Used when a TLS renegotiation request has failed in a non-specific way.</p>
29944 …ty api_stability_2"><a href="#documentation_stability_index">Stability: 2</a> - Stable</div><p></p>
29948 event-driven architecture in which certain kinds of objects (called "emitters")
29956 event names are camel-cased strings but any valid JavaScript property key
29964-js"><span class="hljs-keyword">const</span> <span class="hljs-title class_">EventEmitter</span> =…
29966 …lass="hljs-keyword">class</span> <span class="hljs-title class_">MyEmitter</span> <span class="hlj…
29968 <span class="hljs-keyword">const</span> myEmitter = <span class="hljs-keyword">new</span> <span cla…
29969 myEmitter.<span class="hljs-title function_">on</span>(<span class="hljs-string">'event'</span>, <s…
29970 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>…
29972 myEmitter.<span class="hljs-title function_">emit</span>(<span class="hljs-string">'event'</span>);…
29979 …code class="language-js"><span class="hljs-keyword">const</span> myEmitter = <span class="hljs-key…
29980 …class="hljs-title function_">on</span>(<span class="hljs-string">'event'</span>, <span class="hljs
29981-variable language_">console</span>.<span class="hljs-title function_">log</span>(a, b, <span clas…
29982 <span class="hljs-comment">// Prints:</span>
29983 <span class="hljs-comment">// a b MyEmitter {</span>
29984 <span class="hljs-comment">// domain: null,</span>
29985 <span class="hljs-comment">// _events: { event: [Function] },</span>
29986 <span class="hljs-comment">// _eventsCount: 1,</span>
29987 <span class="hljs-comment">// _maxListeners: undefined } true</span>
29989 … class="hljs-title function_">emit</span>(<span class="hljs-string">'event'</span>, <span class="h…
29992 …code class="language-js"><span class="hljs-keyword">const</span> myEmitter = <span class="hljs-key…
29993 ….<span class="hljs-title function_">on</span>(<span class="hljs-string">'event'</span>, <span clas…
29994 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>…
29995 <span class="hljs-comment">// Prints: a b {}</span>
29997 … class="hljs-title function_">emit</span>(<span class="hljs-string">'event'</span>, <span class="h…
30004 …code class="language-js"><span class="hljs-keyword">const</span> myEmitter = <span class="hljs-key…
30005 ….<span class="hljs-title function_">on</span>(<span class="hljs-string">'event'</span>, <span clas…
30006 <span class="hljs-title function_">setImmediate</span>(<span class="hljs-function">() =></span> {
30007 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>…
30010 … class="hljs-title function_">emit</span>(<span class="hljs-string">'event'</span>, <span class="h…
30014 …code class="language-js"><span class="hljs-keyword">const</span> myEmitter = <span class="hljs-key…
30015 <span class="hljs-keyword">let</span> m = <span class="hljs-number">0</span>;
30016 myEmitter.<span class="hljs-title function_">on</span>(<span class="hljs-string">'event'</span>, <s…
30017 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>…
30019 myEmitter.<span class="hljs-title function_">emit</span>(<span class="hljs-string">'event'</span>);
30020 <span class="hljs-comment">// Prints: 1</span>
30021 myEmitter.<span class="hljs-title function_">emit</span>(<span class="hljs-string">'event'</span>);
30022 <span class="hljs-comment">// Prints: 2</span></code></pre>
30026 …code class="language-js"><span class="hljs-keyword">const</span> myEmitter = <span class="hljs-key…
30027 <span class="hljs-keyword">let</span> m = <span class="hljs-number">0</span>;
30028 myEmitter.<span class="hljs-title function_">once</span>(<span class="hljs-string">'event'</span>, …
30029 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>…
30031 myEmitter.<span class="hljs-title function_">emit</span>(<span class="hljs-string">'event'</span>);
30032 <span class="hljs-comment">// Prints: 1</span>
30033 myEmitter.<span class="hljs-title function_">emit</span>(<span class="hljs-string">'event'</span>);
30034 <span class="hljs-comment">// Ignored</span></code></pre>
30042 …code class="language-js"><span class="hljs-keyword">const</span> myEmitter = <span class="hljs-key…
30043-title function_">emit</span>(<span class="hljs-string">'error'</span>, <span class="hljs-keyword"…
30044 <span class="hljs-comment">// Throws and crashes Node.js</span></code></pre>
30048 …code class="language-js"><span class="hljs-keyword">const</span> myEmitter = <span class="hljs-key…
30049 ….<span class="hljs-title function_">on</span>(<span class="hljs-string">'error'</span>, <span clas…
30050 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">error</spa…
30052-title function_">emit</span>(<span class="hljs-string">'error'</span>, <span class="hljs-keyword"…
30053 <span class="hljs-comment">// Prints: whoops! there was an error</span></code></pre>
30056-js"><span class="hljs-keyword">const</span> { <span class="hljs-title class_">EventEmitter</span>…
30058 <span class="hljs-keyword">const</span> myEmitter = <span class="hljs-keyword">new</span> <span cla…
30059 myEmitter.<span class="hljs-title function_">on</span>(errorMonitor, <span class="hljs-function">(<…
30060 …<span class="hljs-title class_">MyMonitoringTool</span>.<span class="hljs-title function_">log</sp…
30062-title function_">emit</span>(<span class="hljs-string">'error'</span>, <span class="hljs-keyword"…
30063 <span class="hljs-comment">// Still throws and crashes Node.js</span></code></pre>
30065 … api_stability_1"><a href="#documentation_stability_index">Stability: 1</a> - captureRejections is…
30068 …e><code class="language-js"><span class="hljs-keyword">const</span> ee = <span class="hljs-keyword…
30069 ee.<span class="hljs-title function_">on</span>(<span class="hljs-string">'something'</span>, <span…
30070 …an class="hljs-keyword">throw</span> <span class="hljs-keyword">new</span> <span class="hljs-title…
30077-js"><span class="hljs-keyword">const</span> ee1 = <span class="hljs-keyword">new</span> <span cla…
30078 ee1.<span class="hljs-title function_">on</span>(<span class="hljs-string">'something'</span>, <spa…
30079 …an class="hljs-keyword">throw</span> <span class="hljs-keyword">new</span> <span class="hljs-title…
30082 …="hljs-title function_">on</span>(<span class="hljs-string">'error'</span>, <span class="hljs-vari…
30084-keyword">const</span> ee2 = <span class="hljs-keyword">new</span> <span class="hljs-title class_"…
30085 ee2.<span class="hljs-title function_">on</span>(<span class="hljs-string">'something'</span>, <spa…
30086 …an class="hljs-keyword">throw</span> <span class="hljs-keyword">new</span> <span class="hljs-title…
30089-title class_">Symbol</span>.<span class="hljs-title function_">for</span>(<span class="hljs-strin…
30092 …ode class="language-js"><span class="hljs-keyword">const</span> events = <span class="hljs-built_i…
30093 events.<span class="hljs-property">captureRejections</span> = <span class="hljs-literal">true</span…
30094 <span class="hljs-keyword">const</span> ee1 = <span class="hljs-keyword">new</span> events.<span cl…
30095 ee1.<span class="hljs-title function_">on</span>(<span class="hljs-string">'something'</span>, <spa…
30096 …an class="hljs-keyword">throw</span> <span class="hljs-keyword">new</span> <span class="hljs-title…
30099 …="hljs-title function_">on</span>(<span class="hljs-string">'error'</span>, <span class="hljs-vari…
30116-js"><span class="hljs-keyword">const</span> <span class="hljs-title class_">EventEmitter</span> =…
30121 <li><code>captureRejections</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript…
30130 …zilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | …
30131 <li><code>listener</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Referenc…
30141-js"><span class="hljs-keyword">class</span> <span class="hljs-title class_">MyEmitter</span> <spa…
30143 <span class="hljs-keyword">const</span> myEmitter = <span class="hljs-keyword">new</span> <span cla…
30144 <span class="hljs-comment">// Only do this once so we don't loop forever</span>
30145 …an class="hljs-title function_">once</span>(<span class="hljs-string">'newListener'</span>, <span …
30146 <span class="hljs-keyword">if</span> (event === <span class="hljs-string">'event'</span>) {
30147 <span class="hljs-comment">// Insert a new listener in front</span>
30148 …myEmitter.<span class="hljs-title function_">on</span>(<span class="hljs-string">'event'</span>, <…
30149 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>…
30153 myEmitter.<span class="hljs-title function_">on</span>(<span class="hljs-string">'event'</span>, <s…
30154 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>…
30156 myEmitter.<span class="hljs-title function_">emit</span>(<span class="hljs-string">'event'</span>);
30157 <span class="hljs-comment">// Prints:</span>
30158 <span class="hljs-comment">// B</span>
30159 <span class="hljs-comment">// A</span></code></pre>
30173 …zilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | …
30174 <li><code>listener</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Referenc…
30182 …zilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | …
30183 <li><code>listener</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Referenc…
30191 …zilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | …
30192 <li><code>...args</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_stru…
30193 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boole…
30199-js"><span class="hljs-keyword">const</span> <span class="hljs-title class_">EventEmitter</span> =…
30200 <span class="hljs-keyword">const</span> myEmitter = <span class="hljs-keyword">new</span> <span cla…
30202 <span class="hljs-comment">// First listener</span>
30203-title function_">on</span>(<span class="hljs-string">'event'</span>, <span class="hljs-keyword">f…
30204 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>…
30206 <span class="hljs-comment">// Second listener</span>
30207-title function_">on</span>(<span class="hljs-string">'event'</span>, <span class="hljs-keyword">f…
30208-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hlj…
30210 <span class="hljs-comment">// Third listener</span>
30211-title function_">on</span>(<span class="hljs-string">'event'</span>, <span class="hljs-keyword">f…
30212 …<span class="hljs-keyword">const</span> parameters = args.<span class="hljs-title function_">join<…
30213 …js-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="h…
30216-variable language_">console</span>.<span class="hljs-title function_">log</span>(myEmitter.<span …
30218-title function_">emit</span>(<span class="hljs-string">'event'</span>, <span class="hljs-number">…
30220 <span class="hljs-comment">// Prints:</span>
30221 <span class="hljs-comment">// [</span>
30222 <span class="hljs-comment">// [Function: firstListener],</span>
30223 <span class="hljs-comment">// [Function: secondListener],</span>
30224 <span class="hljs-comment">// [Function: thirdListener]</span>
30225 <span class="hljs-comment">// ]</span>
30226 <span class="hljs-comment">// Helloooo! first listener</span>
30227 <span class="hljs-comment">// event with parameters 1, 2 in second listener</span>
30228 <span class="hljs-comment">// event with parameters 1, 2, 3, 4, 5 in third listener</span></code></…
30234 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Obje…
30238-js"><span class="hljs-keyword">const</span> <span class="hljs-title class_">EventEmitter</span> =…
30239 <span class="hljs-keyword">const</span> myEE = <span class="hljs-keyword">new</span> <span class="h…
30240 myEE.<span class="hljs-title function_">on</span>(<span class="hljs-string">'foo'</span>, <span cla…
30241 myEE.<span class="hljs-title function_">on</span>(<span class="hljs-string">'bar'</span>, <span cla…
30243 <span class="hljs-keyword">const</span> sym = <span class="hljs-title class_">Symbol</span>(<span c…
30244 myEE.<span class="hljs-title function_">on</span>(sym, <span class="hljs-function">() =></span> {});
30246 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(…
30247 <span class="hljs-comment">// Prints: [ 'foo', 'bar', Symbol(symbol) ]</span></code></pre>
30253 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Numbe…
30263 …zilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | …
30264 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Numbe…
30280 …zilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | …
30281 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Obje…
30284 …e-js">server.<span class="hljs-title function_">on</span>(<span class="hljs-string">'connection'</…
30285 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>…
30287-variable language_">console</span>.<span class="hljs-title function_">log</span>(util.<span class…
30288 <span class="hljs-comment">// Prints: [ [Function] ]</span></code></pre>
30294 …zilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | …
30295 <li><code>listener</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Referenc…
30304 …zilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | …
30305 <li><code>listener</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Referenc…
30313 …e-js">server.<span class="hljs-title function_">on</span>(<span class="hljs-string">'connection'</…
30314 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>…
30320 …><code class="language-js"><span class="hljs-keyword">const</span> myEE = <span class="hljs-keywor…
30321-title function_">on</span>(<span class="hljs-string">'foo'</span>, <span class="hljs-function">()…
30322-title function_">prependListener</span>(<span class="hljs-string">'foo'</span>, <span class="hljs
30323 myEE.<span class="hljs-title function_">emit</span>(<span class="hljs-string">'foo'</span>);
30324 <span class="hljs-comment">// Prints:</span>
30325 <span class="hljs-comment">// b</span>
30326 <span class="hljs-comment">// a</span></code></pre>
30332 …zilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | …
30333 <li><code>listener</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Referenc…
30336 <p>Adds a <strong>one-time</strong> <code>listener</code> function for the event named <code>eventN…
30338-js">server.<span class="hljs-title function_">once</span>(<span class="hljs-string">'connection'<…
30339 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>…
30345 …><code class="language-js"><span class="hljs-keyword">const</span> myEE = <span class="hljs-keywor…
30346-title function_">once</span>(<span class="hljs-string">'foo'</span>, <span class="hljs-function">…
30347-title function_">prependOnceListener</span>(<span class="hljs-string">'foo'</span>, <span class="…
30348 myEE.<span class="hljs-title function_">emit</span>(<span class="hljs-string">'foo'</span>);
30349 <span class="hljs-comment">// Prints:</span>
30350 <span class="hljs-comment">// b</span>
30351 <span class="hljs-comment">// a</span></code></pre>
30357 …zilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | …
30358 <li><code>listener</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Referenc…
30366-js">server.<span class="hljs-title function_">prependListener</span>(<span class="hljs-string">'c…
30367 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>…
30375 …zilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | …
30376 <li><code>listener</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Referenc…
30379 <p>Adds a <strong>one-time</strong> <code>listener</code> function for the event named <code>eventN…
30382-js">server.<span class="hljs-title function_">prependOnceListener</span>(<span class="hljs-string…
30383 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>…
30391 …zilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | …
30404 …zilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | …
30405 <li><code>listener</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Referenc…
30410 …de class="language-js"><span class="hljs-keyword">const</span> <span class="hljs-title function_">…
30411 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>…
30413 server.<span class="hljs-title function_">on</span>(<span class="hljs-string">'connection'</span>, …
30414 <span class="hljs-comment">// ...</span>
30415 server.<span class="hljs-title function_">removeListener</span>(<span class="hljs-string">'connecti…
30425 …code class="language-js"><span class="hljs-keyword">const</span> myEmitter = <span class="hljs-key…
30427 <span class="hljs-keyword">const</span> <span class="hljs-title function_">callbackA</span> = (<spa…
30428 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>…
30429 …myEmitter.<span class="hljs-title function_">removeListener</span>(<span class="hljs-string">'even…
30432 <span class="hljs-keyword">const</span> <span class="hljs-title function_">callbackB</span> = (<spa…
30433 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>…
30436 myEmitter.<span class="hljs-title function_">on</span>(<span class="hljs-string">'event'</span>, ca…
30438 myEmitter.<span class="hljs-title function_">on</span>(<span class="hljs-string">'event'</span>, ca…
30440 <span class="hljs-comment">// callbackA removes listener callbackB but it will still be called.</sp…
30441 <span class="hljs-comment">// Internal listener array at time of emit [callbackA, callbackB]</span>
30442 myEmitter.<span class="hljs-title function_">emit</span>(<span class="hljs-string">'event'</span>);
30443 <span class="hljs-comment">// Prints:</span>
30444 <span class="hljs-comment">// A</span>
30445 <span class="hljs-comment">// B</span>
30447 <span class="hljs-comment">// callbackB is now removed.</span>
30448 <span class="hljs-comment">// Internal listener array [callbackA]</span>
30449 myEmitter.<span class="hljs-title function_">emit</span>(<span class="hljs-string">'event'</span>);
30450 <span class="hljs-comment">// Prints:</span>
30451 <span class="hljs-comment">// A</span></code></pre>
30461 …e><code class="language-js"><span class="hljs-keyword">const</span> ee = <span class="hljs-keyword…
30463 <span class="hljs-keyword">function</span> <span class="hljs-title function_">pong</span>(<span cla…
30464 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>…
30467 ee.<span class="hljs-title function_">on</span>(<span class="hljs-string">'ping'</span>, pong);
30468 ee.<span class="hljs-title function_">once</span>(<span class="hljs-string">'ping'</span>, pong);
30469 ee.<span class="hljs-title function_">removeListener</span>(<span class="hljs-string">'ping'</span>…
30471 ee.<span class="hljs-title function_">emit</span>(<span class="hljs-string">'ping'</span>);
30472 ee.<span class="hljs-title function_">emit</span>(<span class="hljs-string">'ping'</span>);</code><…
30479 <li><code>n</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures…
30493 …zilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | …
30494 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Obje…
30498 …<code class="language-js"><span class="hljs-keyword">const</span> emitter = <span class="hljs-keyw…
30499-title function_">once</span>(<span class="hljs-string">'log'</span>, <span class="hljs-function">…
30501 <span class="hljs-comment">// Returns a new Array with a function `onceWrapper` which has a propert…
30502 <span class="hljs-comment">// `listener` which contains the original listener bound above</span>
30503 <span class="hljs-keyword">const</span> listeners = emitter.<span class="hljs-title function_">rawL…
30504 <span class="hljs-keyword">const</span> logFnWrapper = listeners[<span class="hljs-number">0</span>…
30506 <span class="hljs-comment">// Logs "log once" to the console and does not unbind the `once` event</…
30507 logFnWrapper.<span class="hljs-title function_">listener</span>();
30509 <span class="hljs-comment">// Logs "log once" to the console and removes the listener</span>
30510 <span class="hljs-title function_">logFnWrapper</span>();
30512-title function_">on</span>(<span class="hljs-string">'log'</span>, <span class="hljs-function">()…
30513 <span class="hljs-comment">// Will return a new Array with a single function bound by `.on()` above…
30514 <span class="hljs-keyword">const</span> newListeners = emitter.<span class="hljs-title function_">r…
30516 <span class="hljs-comment">// Logs "log persistently" twice</span>
30517 newListeners[<span class="hljs-number">0</span>]();
30518 emitter.<span class="hljs-title function_">emit</span>(<span class="hljs-string">'log'</span>);</co…
30523 … api_stability_1"><a href="#documentation_stability_index">Stability: 1</a> - captureRejections is…
30526 …zilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | …
30527 <li><code>...args</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_stru…
30534-js"><span class="hljs-keyword">const</span> { <span class="hljs-title class_">EventEmitter</span>…
30536 …class="hljs-keyword">class</span> <span class="hljs-title class_">MyClass</span> <span class="hljs
30537 <span class="hljs-title function_">constructor</span>(<span class="hljs-params"></span>) {
30538 …<span class="hljs-variable language_">super</span>({ <span class="hljs-attr">captureRejections</sp…
30542-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hlj…
30543 …<span class="hljs-variable language_">this</span>.<span class="hljs-title function_">destroy</span…
30546 <span class="hljs-title function_">destroy</span>(<span class="hljs-params">err</span>) {
30547 <span class="hljs-comment">// Tear the resource down here.</span>
30569-js">emitter.<span class="hljs-title function_">setMaxListeners</span>(emitter.<span class="hljs-t…
30570 emitter.<span class="hljs-title function_">once</span>(<span class="hljs-string">'event'</span>, <s…
30571 <span class="hljs-comment">// do stuff</span>
30572-title function_">setMaxListeners</span>(<span class="hljs-title class_">Math</span>.<span class="…
30574 <p>The <a href="#cli_trace_warnings"><code>--trace-warnings</code></a> command-line flag can be use…
30597 …zilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | …
30598 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Obje…
30604 event target. This is useful for debugging and diagnostic purposes.</p>
30605-js"><span class="hljs-keyword">const</span> { getEventListeners, <span class="hljs-title class_">…
30608 …<span class="hljs-keyword">const</span> ee = <span class="hljs-keyword">new</span> <span class="hl…
30609-keyword">const</span> <span class="hljs-title function_">listener</span> = (<span class="hljs-par…
30610 ee.<span class="hljs-title function_">on</span>(<span class="hljs-string">'foo'</span>, listener);
30611 …<span class="hljs-title function_">getEventListeners</span>(ee, <span class="hljs-string">'foo'</s…
30614 …<span class="hljs-keyword">const</span> et = <span class="hljs-keyword">new</span> <span class="hl…
30615-keyword">const</span> <span class="hljs-title function_">listener</span> = (<span class="hljs-par…
30616 …et.<span class="hljs-title function_">addEventListener</span>(<span class="hljs-string">'foo'</spa…
30617 …<span class="hljs-title function_">getEventListeners</span>(et, <span class="hljs-string">'foo'</s…
30633 <li><code>name</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
30634 <li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference…
30639 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Obje…
30646 <a href="https://dom.spec.whatwg.org/#interface-eventtarget">EventTarget</a> interface, which has n…
30648-js"><span class="hljs-keyword">const</span> { once, <span class="hljs-title class_">EventEmitter<…
30650 …class="hljs-keyword">async</span> <span class="hljs-keyword">function</span> <span class="hljs-tit…
30651 …<span class="hljs-keyword">const</span> ee = <span class="hljs-keyword">new</span> <span class="hl…
30653 …process.<span class="hljs-title function_">nextTick</span>(<span class="hljs-function">() =></span…
30654 …ee.<span class="hljs-title function_">emit</span>(<span class="hljs-string">'myevent'</span>, <spa…
30657 …"hljs-keyword">const</span> [value] = <span class="hljs-keyword">await</span> <span class="hljs-ti…
30658 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>…
30660 …class="hljs-keyword">const</span> err = <span class="hljs-keyword">new</span> <span class="hljs-ti…
30661 …process.<span class="hljs-title function_">nextTick</span>(<span class="hljs-function">() =></span…
30662 … ee.<span class="hljs-title function_">emit</span>(<span class="hljs-string">'error'</span>, err);
30665 <span class="hljs-keyword">try</span> {
30666 …<span class="hljs-keyword">await</span> <span class="hljs-title function_">once</span>(ee, <span c…
30667 } <span class="hljs-keyword">catch</span> (err) {
30668 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>…
30672 <span class="hljs-title function_">run</span>();</code></pre>
30677-js"><span class="hljs-keyword">const</span> { <span class="hljs-title class_">EventEmitter</span>…
30679 <span class="hljs-keyword">const</span> ee = <span class="hljs-keyword">new</span> <span class="hlj…
30681 <span class="hljs-title function_">once</span>(ee, <span class="hljs-string">'error'</span>)
30682-title function_">then</span>(<span class="hljs-function">(<span class="hljs-params">[err]</span>)…
30683-title function_">catch</span>(<span class="hljs-function">(<span class="hljs-params">err</span>) …
30685-title function_">emit</span>(<span class="hljs-string">'error'</span>, <span class="hljs-keyword"…
30687 <span class="hljs-comment">// Prints: ok boom</span></code></pre>
30689-js"><span class="hljs-keyword">const</span> { <span class="hljs-title class_">EventEmitter</span>…
30691 <span class="hljs-keyword">const</span> ee = <span class="hljs-keyword">new</span> <span class="hlj…
30692 <span class="hljs-keyword">const</span> ac = <span class="hljs-keyword">new</span> <span class="hlj…
30694 …class="hljs-keyword">async</span> <span class="hljs-keyword">function</span> <span class="hljs-tit…
30695 <span class="hljs-keyword">try</span> {
30696 …<span class="hljs-keyword">await</span> <span class="hljs-title function_">once</span>(emitter, ev…
30697 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>…
30698 } <span class="hljs-keyword">catch</span> (error) {
30699 …<span class="hljs-keyword">if</span> (error.<span class="hljs-property">name</span> === <span clas…
30700 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">error</spa…
30701 } <span class="hljs-keyword">else</span> {
30702-variable language_">console</span>.<span class="hljs-title function_">error</span>(<span class="h…
30707 <span class="hljs-title function_">foo</span>(ee, <span class="hljs-string">'foo'</span>, ac.<span …
30708 ac.<span class="hljs-title function_">abort</span>(); <span class="hljs-comment">// Abort waiting f…
30709 ee.<span class="hljs-title function_">emit</span>(<span class="hljs-string">'foo'</span>); <span cl…
30717-js"><span class="hljs-keyword">const</span> { <span class="hljs-title class_">EventEmitter</span>…
30719 <span class="hljs-keyword">const</span> myEE = <span class="hljs-keyword">new</span> <span class="h…
30721 …class="hljs-keyword">async</span> <span class="hljs-keyword">function</span> <span class="hljs-tit…
30722 …<span class="hljs-keyword">await</span> <span class="hljs-title function_">once</span>(myEE, <span…
30723 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>…
30725 <span class="hljs-comment">// This Promise will never resolve because the 'foo' event will</span>
30726 <span class="hljs-comment">// have already been emitted before the Promise is created.</span>
30727 …<span class="hljs-keyword">await</span> <span class="hljs-title function_">once</span>(myEE, <span…
30728 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>…
30731 process.<span class="hljs-title function_">nextTick</span>(<span class="hljs-function">() =></span>…
30732 myEE.<span class="hljs-title function_">emit</span>(<span class="hljs-string">'bar'</span>);
30733 myEE.<span class="hljs-title function_">emit</span>(<span class="hljs-string">'foo'</span>);
30736-title function_">foo</span>().<span class="hljs-title function_">then</span>(<span class="hljs-fu…
30740-js"><span class="hljs-keyword">const</span> { <span class="hljs-title class_">EventEmitter</span>…
30742 <span class="hljs-keyword">const</span> myEE = <span class="hljs-keyword">new</span> <span class="h…
30744 …class="hljs-keyword">async</span> <span class="hljs-keyword">function</span> <span class="hljs-tit…
30745-keyword">await</span> <span class="hljs-title class_">Promise</span>.<span class="hljs-title func…
30746 …="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span clas…
30749 process.<span class="hljs-title function_">nextTick</span>(<span class="hljs-function">() =></span>…
30750 myEE.<span class="hljs-title function_">emit</span>(<span class="hljs-string">'bar'</span>);
30751 myEE.<span class="hljs-title function_">emit</span>(<span class="hljs-string">'foo'</span>);
30754-title function_">foo</span>().<span class="hljs-title function_">then</span>(<span class="hljs-fu…
30759 … api_stability_1"><a href="#documentation_stability_index">Stability: 1</a> - captureRejections is…
30760 <p>Value: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_…
30766 … api_stability_1"><a href="#documentation_stability_index">Stability: 1</a> - captureRejections is…
30773 … api_stability_0"><a href="#documentation_stability_index">Stability: 0</a> - Deprecated: Use <a h…
30776 …zilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | …
30780-js"><span class="hljs-keyword">const</span> { <span class="hljs-title class_">EventEmitter</span>…
30781 <span class="hljs-keyword">const</span> myEmitter = <span class="hljs-keyword">new</span> <span cla…
30782 myEmitter.<span class="hljs-title function_">on</span>(<span class="hljs-string">'event'</span>, <s…
30783 myEmitter.<span class="hljs-title function_">on</span>(<span class="hljs-string">'event'</span>, <s…
30784-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hlj…
30785 <span class="hljs-comment">// Prints: 2</span></code></pre>
30792 …zilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | …
30793 <li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference…
30798 <li>Returns: <a href="https://tc39.github.io/ecma262/#sec-asynciterator-interface" class="type">&#x…
30800-js"><span class="hljs-keyword">const</span> { on, <span class="hljs-title class_">EventEmitter</s…
30802 (<span class="hljs-keyword">async</span> () => {
30803 …<span class="hljs-keyword">const</span> ee = <span class="hljs-keyword">new</span> <span class="hl…
30805 <span class="hljs-comment">// Emit later on</span>
30806 …process.<span class="hljs-title function_">nextTick</span>(<span class="hljs-function">() =></span…
30807 …ee.<span class="hljs-title function_">emit</span>(<span class="hljs-string">'foo'</span>, <span cl…
30808 …ee.<span class="hljs-title function_">emit</span>(<span class="hljs-string">'foo'</span>, <span cl…
30811-keyword">for</span> <span class="hljs-keyword">await</span> (<span class="hljs-keyword">const</sp…
30812 <span class="hljs-comment">// The execution of this inner block is synchronous and it</span>
30813 <span class="hljs-comment">// processes one event at a time (even with await). Do not use</span>
30814 <span class="hljs-comment">// if concurrent execution is required.</span>
30815 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>…
30817 <span class="hljs-comment">// Unreachable here</span>
30824-js"><span class="hljs-keyword">const</span> { on, <span class="hljs-title class_">EventEmitter</s…
30825 <span class="hljs-keyword">const</span> ac = <span class="hljs-keyword">new</span> <span class="hlj…
30827 (<span class="hljs-keyword">async</span> () => {
30828 …<span class="hljs-keyword">const</span> ee = <span class="hljs-keyword">new</span> <span class="hl…
30830 <span class="hljs-comment">// Emit later on</span>
30831 …process.<span class="hljs-title function_">nextTick</span>(<span class="hljs-function">() =></span…
30832 …ee.<span class="hljs-title function_">emit</span>(<span class="hljs-string">'foo'</span>, <span cl…
30833 …ee.<span class="hljs-title function_">emit</span>(<span class="hljs-string">'foo'</span>, <span cl…
30836-keyword">for</span> <span class="hljs-keyword">await</span> (<span class="hljs-keyword">const</sp…
30837 <span class="hljs-comment">// The execution of this inner block is synchronous and it</span>
30838 <span class="hljs-comment">// processes one event at a time (even with await). Do not use</span>
30839 <span class="hljs-comment">// if concurrent execution is required.</span>
30840 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>…
30842 <span class="hljs-comment">// Unreachable here</span>
30845 process.<span class="hljs-title function_">nextTick</span>(<span class="hljs-function">() =></span>…
30851 …ttps://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&…
30857 <pre><code class="language-js"><span class="hljs-keyword">const</span> {
30859 <span class="hljs-title class_">EventEmitter</span>
30860 } = <span class="hljs-built_in">require</span>(<span class="hljs-string">'events'</span>);
30862 <span class="hljs-keyword">const</span> target = <span class="hljs-keyword">new</span> <span class=…
30863 <span class="hljs-keyword">const</span> emitter = <span class="hljs-keyword">new</span> <span class…
30865 <span class="hljs-title function_">setMaxListeners</span>(<span class="hljs-number">5</span>, targe…
30866 <p><a id="event-target-and-event-api"></a></p>
30871 … api_stability_1"><a href="#documentation_stability_index">Stability: 1</a> - Experimental</div><p…
30872 <p>The <code>EventTarget</code> and <code>Event</code> objects are a Node.js-specific implementation
30876 <pre><code class="language-js"><span class="hljs-keyword">const</span> target = <span class="hljs-t…
30878 target.<span class="hljs-title function_">addEventListener</span>(<span class="hljs-string">'foo'</…
30879 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>…
30888 nested target objects that may each have their own set of handlers for the
30927 …de class="language-js"><span class="hljs-keyword">function</span> <span class="hljs-title function…
30928 …js-variable language_">console</span>.<span class="hljs-title function_">log</span>(event.<span cl…
30929 event.<span class="hljs-property">a</span> = <span class="hljs-number">1</span>;
30932 …ass="hljs-keyword">async</span> <span class="hljs-keyword">function</span> <span class="hljs-title…
30933 …js-variable language_">console</span>.<span class="hljs-title function_">log</span>(event.<span cl…
30934 …ljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(event.<span c…
30937 <span class="hljs-keyword">const</span> handler3 = {
30938 <span class="hljs-title function_">handleEvent</span>(<span class="hljs-params">event</span>) {
30939 …js-variable language_">console</span>.<span class="hljs-title function_">log</span>(event.<span cl…
30943 <span class="hljs-keyword">const</span> handler4 = {
30944 …<span class="hljs-keyword">async</span> <span class="hljs-title function_">handleEvent</span>(<spa…
30945 …js-variable language_">console</span>.<span class="hljs-title function_">log</span>(event.<span cl…
30949 <span class="hljs-keyword">const</span> target = <span class="hljs-title function_">getEventTargetS…
30951 target.<span class="hljs-title function_">addEventListener</span>(<span class="hljs-string">'foo'</…
30952 target.<span class="hljs-title function_">addEventListener</span>(<span class="hljs-string">'foo'</…
30953 target.<span class="hljs-title function_">addEventListener</span>(<span class="hljs-string">'foo'</…
30954 target.<span class="hljs-title function_">addEventListener</span>(<span class="hljs-string">'foo'</…
30980 <li>Type: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_…
30994 <li>Type: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_…
31001 <li>Type: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_…
31018 <p>Alias for <code>event.target</code>.</p>
31024 <li>Type: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_…
31033 <li>Type: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_t…
31042 <li>Type: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_…
31056 <li>Type: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_…
31066 <p>Alias for <code>event.target</code>.</p>
31077 <h5><code>event.target</code><span><a class="mark" href="#events_event_target" id="events_event_tar…
31089 <li>Type: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_t…
31097 <li>Type: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_t…
31109 <li><code>type</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
31110 <li><code>listener</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Referenc…
31111 <li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference…
31113 <li><code>once</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
31115 <li><code>passive</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_stru…
31118 <li><code>capture</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_stru…
31132 …de class="language-js"><span class="hljs-keyword">function</span> <span class="hljs-title function…
31134 <span class="hljs-keyword">const</span> target = <span class="hljs-title function_">getEventTargetS…
31135 target.<span class="hljs-title function_">addEventListener</span>(<span class="hljs-string">'foo'</…
31136 target.<span class="hljs-title function_">addEventListener</span>(<span class="hljs-string">'foo'</…
31138 <span class="hljs-comment">// Removes the second instance of handler</span>
31139 target.<span class="hljs-title function_">removeEventListener</span>(<span class="hljs-string">'foo…
31141 <span class="hljs-comment">// Removes the first instance of handler</span>
31142 target.<span class="hljs-title function_">removeEventListener</span>(<span class="hljs-string">'foo…
31149 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boole…
31160 <li><code>type</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
31161 <li><code>listener</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Referenc…
31162 <li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference…
31164 <li><code>capture</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_stru…
31176 <p>The <code>NodeEventTarget</code> is a Node.js-specific extension to <code>EventTarget</code>
31184 <p><code>type</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structur…
31187 <p><code>listener</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference…
31190 <p><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/…
31192 <li><code>once</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
31199 <p>Node.js-specific extension to the <code>EventTarget</code> class that emulates the
31208 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Strin…
31210 <p>Node.js-specific extension to the <code>EventTarget</code> class that returns an array
31218 <p><code>type</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structur…
31221 <p>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number…
31224 <p>Node.js-specific extension to the <code>EventTarget</code> class that returns the number
31232 <p><code>type</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structur…
31235 <p><code>listener</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference…
31241 <p>Node.js-specific alias for <code>eventTarget.removeListener()</code>.</p>
31248 <p><code>type</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structur…
31251 <p><code>listener</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference…
31254 <p><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/…
31256 <li><code>once</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
31263 <p>Node.js-specific alias for <code>eventTarget.addListener()</code>.</p>
31270 <p><code>type</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structur…
31273 <p><code>listener</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference…
31276 <p><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/…
31282 <p>Node.js-specific extension to the <code>EventTarget</code> class that adds a <code>once</code>
31291 <p><code>type</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structur…
31297 <p>Node.js-specific extension to the <code>EventTarget</code> class. If <code>type</code> is specif…
31306 <p><code>type</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structur…
31309 <p><code>listener</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference…
31315 <p>Node.js-specific extension to the <code>EventTarget</code> class that removes the
31322 …ty api_stability_2"><a href="#documentation_stability_index">Stability: 2</a> - Stable</div><p></p>
31327 <p>To use the promise-based APIs:</p>
31329-flavor-selector" type="checkbox" checked aria-label="Show modern ES modules syntax"><code class="…
31332-flavor-selector" type="checkbox" checked aria-label="Show modern ES modules syntax"><code class="…
31333 <p>All file system operations have synchronous, callback, and promise-based
31336 <p>Promise-based operations return a promise that is fulfilled when the
31339-flavor-selector" type="checkbox" checked aria-label="Show modern ES modules syntax"><code class="…
31341 <span class="hljs-keyword">try</span> {
31342 …<span class="hljs-keyword">await</span> <span class="hljs-title function_">unlink</span>(<span cla…
31343 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>…
31344 } <span class="hljs-keyword">catch</span> (error) {
31345-variable language_">console</span>.<span class="hljs-title function_">error</span>(<span class="h…
31346 …class="language-js cjs"><span class="hljs-keyword">const</span> { unlink } = <span class="hljs-bui…
31348 (<span class="hljs-keyword">async</span> <span class="hljs-keyword">function</span>(<span class="hl…
31349 <span class="hljs-keyword">try</span> {
31350 <span class="hljs-keyword">await</span> <span class="hljs-title function_">unlink</span>(path);
31351 …js-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="h…
31352 } <span class="hljs-keyword">catch</span> (error) {
31353-variable language_">console</span>.<span class="hljs-title function_">error</span>(<span class="h…
31355 })(<span class="hljs-string">'/tmp/hello'</span>);</code></pre>
31363-flavor-selector" type="checkbox" checked aria-label="Show modern ES modules syntax"><code class="…
31365 …n class="hljs-title function_">unlink</span>(<span class="hljs-string">'/tmp/hello'</span>, <span …
31366 <span class="hljs-keyword">if</span> (err) <span class="hljs-keyword">throw</span> err;
31367 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>…
31368 …class="language-js cjs"><span class="hljs-keyword">const</span> { unlink } = <span class="hljs-bui…
31370 …n class="hljs-title function_">unlink</span>(<span class="hljs-string">'/tmp/hello'</span>, <span …
31371 <span class="hljs-keyword">if</span> (err) <span class="hljs-keyword">throw</span> err;
31372 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>…
31374 <p>The callback-based versions of the <code>fs</code> module APIs are preferable over
31382-flavor-selector" type="checkbox" checked aria-label="Show modern ES modules syntax"><code class="…
31384 <span class="hljs-keyword">try</span> {
31385 …<span class="hljs-title function_">unlinkSync</span>(<span class="hljs-string">'/tmp/hello'</span>…
31386 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>…
31387 } <span class="hljs-keyword">catch</span> (err) {
31388 <span class="hljs-comment">// handle the error</span>
31389 …ass="language-js cjs"><span class="hljs-keyword">const</span> { unlinkSync } = <span class="hljs-b…
31391 <span class="hljs-keyword">try</span> {
31392 …<span class="hljs-title function_">unlinkSync</span>(<span class="hljs-string">'/tmp/hello'</span>…
31393 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>…
31394 } <span class="hljs-keyword">catch</span> (err) {
31395 <span class="hljs-comment">// handle the error</span>
31447-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | <a href="#bu…
31448 …lla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type">&#x3C;Object></a> …
31450 …zilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | …
31453 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Obje…
31464 <li><code>mode</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
31465 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Obje…
31467 <p>Modifies the permissions on the file. See <a href="http://man7.org/linux/man-pages/man2/chmod.2.…
31473 <li><code>uid</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structur…
31474 <li><code>gid</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structur…
31475 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Obje…
31477 <p>Changes the ownership of the file. A wrapper for <a href="http://man7.org/linux/man-pages/man2/c…
31483 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Obje…
31487 …de class="language-js mjs"><span class="hljs-keyword">import</span> { open } <span class="hljs-key…
31489 <span class="hljs-keyword">let</span> filehandle;
31490 <span class="hljs-keyword">try</span> {
31491 …ss="hljs-keyword">await</span> <span class="hljs-title function_">open</span>(<span class="hljs-st…
31492 } <span class="hljs-keyword">finally</span> {
31493 …<span class="hljs-keyword">await</span> filehandle?.<span class="hljs-title function_">close</span…
31500 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Obje…
31504 <a href="http://man7.org/linux/man-pages/man2/fdatasync.2.html"><code>fdatasync(2)</code></a> docum…
31511 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" c…
31518 …org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray" class="type">&#x3C;TypedArray><…
31520 <li><code>offset</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struc…
31522 <li><code>length</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struc…
31524 <li><code>position</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_str…
31528 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Obje…
31530 <li><code>bytesRead</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_st…
31531 …org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray" class="type">&#x3C;TypedArray><…
31537 <p>If the file is not modified concurrently, the end-of-file is reached when the
31544 <li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference…
31546 …org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray" class="type">&#x3C;TypedArray><…
31548 <li><code>offset</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struc…
31550 <li><code>length</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struc…
31552 <li><code>position</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_str…
31558 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Obje…
31560 <li><code>bytesRead</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_st…
31561 …org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray" class="type">&#x3C;TypedArray><…
31567 <p>If the file is not modified concurrently, the end-of-file is reached when the
31574 …lla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type">&#x3C;Object></a> …
31576 …zilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | …
31577 …class_abortsignal" class="type">&#x3C;AbortSignal></a> allows aborting an in-progress readFile</li>
31580 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Obje…
31596 …rg/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray" class="type">&#x3C;TypedArray[]>…
31597 <li><code>position</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_str…
31600 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Obje…
31602 <li><code>bytesRead</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_st…
31603 …rg/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray" class="type">&#x3C;TypedArray[]>…
31608 <p>Read from a file and write to an array of <a href="https://developer.mozilla.org/en-US/docs/Web/…
31622 <li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference…
31624 <li><code>bigint</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struc…
31628 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Obje…
31635 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Obje…
31639 Refer to the POSIX <a href="http://man7.org/linux/man-pages/man2/fsync.2.html"><code>fsync(2)</code…
31645 <li><code>len</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structur…
31646 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Obje…
31652 …de class="language-js mjs"><span class="hljs-keyword">import</span> { open } <span class="hljs-key…
31654 <span class="hljs-keyword">let</span> filehandle = <span class="hljs-literal">null</span>;
31655 <span class="hljs-keyword">try</span> {
31656 …lass="hljs-keyword">await</span> <span class="hljs-title function_">open</span>(<span class="hljs-
31657 …<span class="hljs-keyword">await</span> filehandle.<span class="hljs-title function_">truncate</sp…
31658 } <span class="hljs-keyword">finally</span> {
31659 …<span class="hljs-keyword">await</span> filehandle?.<span class="hljs-title function_">close</span…
31669-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;number></a> | <a href="htt…
31670-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;number></a> | <a href="htt…
31671 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Obje…
31690 …org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray" class="type">&#x3C;TypedArray><…
31691 <li><code>offset</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struc…
31693 <li><code>length</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struc…
31694 <code>buffer.byteLength - offset</code></li>
31695 <li><code>position</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_str…
31697 the data will be written at the current position. See the POSIX <a href="http://man7.org/linux/man-
31699 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Obje…
31704 <li><code>bytesWritten</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data…
31705 …org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray" class="type">&#x3C;TypedArray><…
31727 <li><code>string</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struc…
31728 <li><code>position</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_str…
31730 data will be written at the current position. See the POSIX <a href="http://man7.org/linux/man-page…
31732 <li><code>encoding</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_str…
31733 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Obje…
31739 <li><code>bytesWritten</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data…
31740 <li><code>buffer</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struc…
31763-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | <a href="#bu…
31764 …lla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type">&#x3C;Object></a> …
31766 …zilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | …
31770 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Obje…
31773 …github.io/ecma262/#sec-asynciterable-interface" class="type">&#x3C;AsyncIterable></a> or <a href="…
31788 …rg/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray" class="type">&#x3C;TypedArray[]>…
31789 <li><code>position</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_str…
31792 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Obje…
31794 <p>Write an array of <a href="https://developer.mozilla.org/en-US/docs/Web/API/ArrayBufferView" cla…
31797 <li><code>bytesWritten</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data…
31798 …rg/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray" class="type">&#x3C;TypedArray[]>…
31811 <li><code>path</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
31812 <li><code>mode</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
31813 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Obje…
31822 with an <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/E…
31824 …e class="language-js mjs"><span class="hljs-keyword">import</span> { access } <span class="hljs-ke…
31825 <span class="hljs-keyword">import</span> { constants } <span class="hljs-keyword">from</span> <span…
31827 <span class="hljs-keyword">try</span> {
31828-keyword">await</span> <span class="hljs-title function_">access</span>(<span class="hljs-string">…
31829 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>…
31830 } <span class="hljs-keyword">catch</span> {
31831 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">error</spa…
31843 <li><code>path</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
31844 <li><code>data</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
31845 …lla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type">&#x3C;Object></a> …
31847 …zilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | …
31848 <li><code>mode</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
31849 <li><code>flag</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
31852 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Obje…
31866 <li><code>path</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
31867 …zilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | …
31868 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Obje…
31876 <li><code>path</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
31877 <li><code>uid</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structur…
31878 <li><code>gid</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structur…
31879 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Obje…
31895 <li><code>src</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structur…
31896 <li><code>dest</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
31897 <li><code>mode</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
31906 a copy-on-write reflink. If the platform does not support copy-on-write,
31909 create a copy-on-write reflink. If the platform does not support
31910 copy-on-write, then the operation will fail.</li>
31913 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Obje…
31920 … class="language-js mjs"><span class="hljs-keyword">import</span> { constants } <span class="hljs-
31921 <span class="hljs-keyword">import</span> { copyFile } <span class="hljs-keyword">from</span> <span …
31923 <span class="hljs-keyword">try</span> {
31924 …s="hljs-keyword">await</span> <span class="hljs-title function_">copyFile</span>(<span class="hljs
31925 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>…
31926 } <span class="hljs-keyword">catch</span> {
31927 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>…
31930 <span class="hljs-comment">// By using COPYFILE_EXCL, the operation will fail if destination.txt ex…
31931 <span class="hljs-keyword">try</span> {
31932-keyword">await</span> <span class="hljs-title function_">copyFile</span>(<span class="hljs-string…
31933 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>…
31934 } <span class="hljs-keyword">catch</span> {
31935 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>…
31942 <li><code>path</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
31943 <li><code>mode</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
31944 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Obje…
31961 <li><code>path</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
31962 <li><code>uid</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structur…
31963 <li><code>gid</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structur…
31964 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Obje…
31972 <li><code>path</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
31973-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;number></a> | <a href="htt…
31974-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;number></a> | <a href="htt…
31975 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Obje…
31986 <li><code>existingPath</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data…
31987 <li><code>newPath</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_stru…
31988 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Obje…
31991 <a href="http://man7.org/linux/man-pages/man2/link.2.html"><code>link(2)</code></a> documentation f…
32005 <li><code>path</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
32006 <li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference…
32008 <li><code>bigint</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struc…
32012 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Obje…
32016 in which case the link itself is stat-ed, not the file that it refers to.
32017 Refer to the POSIX <a href="http://man7.org/linux/man-pages/man2/lstat.2.html"><code>lstat(2)</code…
32023 <li><code>path</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
32024 …lla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type">&#x3C;Object></a> …
32026 <li><code>recursive</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_st…
32027 …zilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | …
32030 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Obje…
32052 <li><code>prefix</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struc…
32053 …zilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | …
32055 <li><code>encoding</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_str…
32058 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Obje…
32068 …e class="language-js mjs"><span class="hljs-keyword">import</span> { mkdtemp } <span class="hljs-k…
32070 <span class="hljs-keyword">try</span> {
32071-keyword">await</span> <span class="hljs-title function_">mkdtemp</span>(path.<span class="hljs-ti…
32072 } <span class="hljs-keyword">catch</span> (err) {
32073 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">error</spa…
32078 <code>prefix</code> must end with a trailing platform-specific path separator
32093 <li><code>path</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
32094 …zilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | …
32096 …zilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | …
32098 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Obje…
32101 <p>Refer to the POSIX <a href="http://man7.org/linux/man-pages/man2/open.2.html"><code>open(2)</cod…
32103 by <a href="https://docs.microsoft.com/en-us/windows/desktop/FileIO/naming-a-file">Naming Files, Pa…
32105 <a href="https://docs.microsoft.com/en-us/windows/desktop/FileIO/using-streams">this MSDN page</a>.…
32119 <li><code>path</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
32120 <li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference…
32122 …zilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | …
32123 <li><code>bufferSize</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_s…
32128 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Obje…
32131 <a href="http://man7.org/linux/man-pages/man3/opendir.3.html"><code>opendir(3)</code></a> documenta…
32137 …e class="language-js mjs"><span class="hljs-keyword">import</span> { opendir } <span class="hljs-k…
32139 <span class="hljs-keyword">try</span> {
32140 …ss="hljs-keyword">const</span> dir = <span class="hljs-keyword">await</span> <span class="hljs-tit…
32141 …n class="hljs-keyword">for</span> <span class="hljs-keyword">await</span> (<span class="hljs-keywo…
32142 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>…
32143 } <span class="hljs-keyword">catch</span> (err) {
32144 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">error</spa…
32161 <li><code>path</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
32162 …zilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | …
32164 <li><code>encoding</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_str…
32165 <li><code>withFileTypes</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Dat…
32168 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Obje…
32178 …e class="language-js mjs"><span class="hljs-keyword">import</span> { readdir } <span class="hljs-k…
32180 <span class="hljs-keyword">try</span> {
32181 …<span class="hljs-keyword">const</span> files = <span class="hljs-keyword">await</span> <span clas…
32182 …<span class="hljs-keyword">for</span> (<span class="hljs-keyword">const</span> file <span class="h…
32183 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>…
32184 } <span class="hljs-keyword">catch</span> (err) {
32185 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">error</spa…
32200 <li><code>path</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
32201 …lla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type">&#x3C;Object></a> …
32203 …zilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | …
32204 <li><code>flag</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
32205 …class_abortsignal" class="type">&#x3C;AbortSignal></a> allows aborting an in-progress readFile</li>
32208 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Obje…
32215 platform-specific. On macOS, Linux, and Windows, the promise will be rejected
32220 … class="language-js mjs"><span class="hljs-keyword">import</span> { readFile } <span class="hljs-k…
32222 <span class="hljs-keyword">try</span> {
32223 …<span class="hljs-keyword">const</span> controller = <span class="hljs-keyword">new</span> <span c…
32224 <span class="hljs-keyword">const</span> { signal } = controller;
32225 …<span class="hljs-keyword">const</span> promise = <span class="hljs-title function_">readFile</spa…
32227 <span class="hljs-comment">// Abort the request before the promise settles.</span>
32228 controller.<span class="hljs-title function_">abort</span>();
32230 <span class="hljs-keyword">await</span> promise;
32231 } <span class="hljs-keyword">catch</span> (err) {
32232 <span class="hljs-comment">// When a request is aborted - err is an AbortError</span>
32233 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">error</spa…
32243 <li><code>path</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
32244 …zilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | …
32246 <li><code>encoding</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_str…
32249 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Obje…
32252 <a href="http://man7.org/linux/man-pages/man2/readlink.2.html"><code>readlink(2)</code></a> documen…
32263 <li><code>path</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
32264 …zilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | …
32266 <li><code>encoding</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_str…
32269 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Obje…
32286 <li><code>oldPath</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_stru…
32287 <li><code>newPath</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_stru…
32288 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Obje…
32308 <li><code>path</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
32309 <li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference…
32311 <li><code>maxRetries</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_s…
32316 <li><code>recursive</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_st…
32319 <li><code>retryDelay</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_s…
32324 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Obje…
32331 <code>rm -rf</code>: an error will not be raised for paths that do not exist, and paths
32340 <li><code>path</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
32341 <li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference…
32343 <li><code>force</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struct…
32345 <li><code>maxRetries</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_s…
32350 <li><code>recursive</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_st…
32352 <li><code>retryDelay</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_s…
32357 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Obje…
32373 <li><code>path</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
32374 <li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference…
32376 <li><code>bigint</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struc…
32380 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Obje…
32383 <h4><code>fsPromises.symlink(target, path[, type])</code><span><a class="mark" href="#fs_fspromises…
32388 <li><code>target</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struc…
32389 <li><code>path</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
32390 <li><code>type</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
32391 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Obje…
32396 to be absolute. When using <code>'junction'</code>, the <code>target</code> argument will
32403 <li><code>path</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
32404 <li><code>len</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structur…
32405 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Obje…
32414 <li><code>path</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
32415 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Obje…
32419 … link, the file is deleted. See the POSIX <a href="http://man7.org/linux/man-pages/man2/unlink.2.h…
32426 <li><code>path</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
32427-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;number></a> | <a href="htt…
32428-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;number></a> | <a href="htt…
32429 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Obje…
32437 <code>-Infinity</code>, an <code>Error</code> will be thrown.</li>
32444 <li><code>filename</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_str…
32445 …zilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | …
32447 <li><code>persistent</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_s…
32449 <li><code>recursive</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_st…
32453 <li><code>encoding</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_str…
32459 <li>Returns: <a href="https://tc39.github.io/ecma262/#sec-asynciterator-interface" class="type">&#x…
32461 <li><code>eventType</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_st…
32462 <li><code>filename</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_str…
32468 …e class="language-js"><span class="hljs-keyword">const</span> { watch } = <span class="hljs-built_…
32470 <span class="hljs-keyword">const</span> ac = <span class="hljs-keyword">new</span> <span class="hlj…
32471 <span class="hljs-keyword">const</span> { signal } = ac;
32472 …"hljs-built_in">setTimeout</span>(<span class="hljs-function">() =></span> ac.<span class="hljs-ti…
32474 (<span class="hljs-keyword">async</span> () => {
32475 <span class="hljs-keyword">try</span> {
32476 …<span class="hljs-keyword">const</span> watcher = <span class="hljs-title function_">watch</span>(…
32477 …n class="hljs-keyword">for</span> <span class="hljs-keyword">await</span> (<span class="hljs-keywo…
32478 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>…
32479 } <span class="hljs-keyword">catch</span> (err) {
32480 …<span class="hljs-keyword">if</span> (err.<span class="hljs-property">name</span> === <span class=…
32481 <span class="hljs-keyword">return</span>;
32482 <span class="hljs-keyword">throw</span> err;
32505 <li><code>file</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
32506-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | <a href="#bu…
32507 …lla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type">&#x3C;Object></a> …
32509 …zilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | …
32510 <li><code>mode</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
32511 <li><code>flag</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
32512 …class_abortsignal" class="type">&#x3C;AbortSignal></a> allows aborting an in-progress writeFile</l…
32515 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Obje…
32518 …github.io/ecma262/#sec-asynciterable-interface" class="type">&#x3C;AsyncIterable></a> or <a href="…
32526 <p>Similarly to <code>fsPromises.readFile</code> - <code>fsPromises.writeFile</code> is a convenien…
32533 … class="language-js mjs"><span class="hljs-keyword">import</span> { writeFile } <span class="hljs-
32535 <span class="hljs-keyword">try</span> {
32536 …<span class="hljs-keyword">const</span> controller = <span class="hljs-keyword">new</span> <span c…
32537 <span class="hljs-keyword">const</span> { signal } = controller;
32538-keyword">const</span> data = <span class="hljs-keyword">new</span> <span class="hljs-title class_…
32539 …<span class="hljs-keyword">const</span> promise = <span class="hljs-title function_">writeFile</sp…
32541 <span class="hljs-comment">// Abort the request before the promise settles.</span>
32542 controller.<span class="hljs-title function_">abort</span>();
32544 <span class="hljs-keyword">await</span> promise;
32545 } <span class="hljs-keyword">catch</span> (err) {
32546 <span class="hljs-comment">// When a request is aborted - err is an AbortError</span>
32547 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">error</spa…
32573 <li><code>path</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
32574 <li><code>mode</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
32575 <li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Referenc…
32577 <li><code>err</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Glo…
32590 …ss="language-js mjs"><span class="hljs-keyword">import</span> { access, constants } <span class="h…
32592 <span class="hljs-keyword">const</span> file = <span class="hljs-string">'package.json'</span>;
32594 <span class="hljs-comment">// Check if the file exists in the current directory.</span>
32595 …ss="hljs-title function_">access</span>(file, constants.<span class="hljs-property">F_OK</span>, <…
32596-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hlj…
32599 <span class="hljs-comment">// Check if the file is readable.</span>
32600 …ss="hljs-title function_">access</span>(file, constants.<span class="hljs-property">R_OK</span>, <…
32601-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hlj…
32604 <span class="hljs-comment">// Check if the file is writable.</span>
32605 …ss="hljs-title function_">access</span>(file, constants.<span class="hljs-property">W_OK</span>, <…
32606-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hlj…
32609 <span class="hljs-comment">// Check if the file exists in the current directory, and if it is writa…
32610-title function_">access</span>(file, constants.<span class="hljs-property">F_OK</span> | constant…
32611 <span class="hljs-keyword">if</span> (err) {
32612 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">error</spa…
32613-string">`<span class="hljs-subst">${file}</span> <span class="hljs-subst">${err.code === <span cl…
32614 } <span class="hljs-keyword">else</span> {
32615 …n class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<sp…
32624 …s="language-js mjs"><span class="hljs-keyword">import</span> { access, open, close } <span class="…
32626 …pan class="hljs-title function_">access</span>(<span class="hljs-string">'myfile'</span>, <span cl…
32627 <span class="hljs-keyword">if</span> (!err) {
32628 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">error</spa…
32629 <span class="hljs-keyword">return</span>;
32632-title function_">open</span>(<span class="hljs-string">'myfile'</span>, <span class="hljs-string"…
32633 <span class="hljs-keyword">if</span> (err) <span class="hljs-keyword">throw</span> err;
32635 <span class="hljs-keyword">try</span> {
32636 <span class="hljs-title function_">writeMyData</span>(fd);
32637 } <span class="hljs-keyword">finally</span> {
32638 …<span class="hljs-title function_">close</span>(fd, <span class="hljs-function">(<span class="hljs
32639 <span class="hljs-keyword">if</span> (err) <span class="hljs-keyword">throw</span> err;
32645 …class="language-js mjs"><span class="hljs-keyword">import</span> { open, close } <span class="hljs
32647-title function_">open</span>(<span class="hljs-string">'myfile'</span>, <span class="hljs-string"…
32648 <span class="hljs-keyword">if</span> (err) {
32649 …<span class="hljs-keyword">if</span> (err.<span class="hljs-property">code</span> === <span class=…
32650 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">error</spa…
32651 <span class="hljs-keyword">return</span>;
32654 <span class="hljs-keyword">throw</span> err;
32657 <span class="hljs-keyword">try</span> {
32658 <span class="hljs-title function_">writeMyData</span>(fd);
32659 } <span class="hljs-keyword">finally</span> {
32660 …<span class="hljs-title function_">close</span>(fd, <span class="hljs-function">(<span class="hljs
32661 <span class="hljs-keyword">if</span> (err) <span class="hljs-keyword">throw</span> err;
32666 …s="language-js mjs"><span class="hljs-keyword">import</span> { access, open, close } <span class="…
32667 …pan class="hljs-title function_">access</span>(<span class="hljs-string">'myfile'</span>, <span cl…
32668 <span class="hljs-keyword">if</span> (err) {
32669 …<span class="hljs-keyword">if</span> (err.<span class="hljs-property">code</span> === <span class=…
32670 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">error</spa…
32671 <span class="hljs-keyword">return</span>;
32674 <span class="hljs-keyword">throw</span> err;
32677-title function_">open</span>(<span class="hljs-string">'myfile'</span>, <span class="hljs-string"…
32678 <span class="hljs-keyword">if</span> (err) <span class="hljs-keyword">throw</span> err;
32680 <span class="hljs-keyword">try</span> {
32681 <span class="hljs-title function_">readMyData</span>(fd);
32682 } <span class="hljs-keyword">finally</span> {
32683 …<span class="hljs-title function_">close</span>(fd, <span class="hljs-function">(<span class="hljs
32684 <span class="hljs-keyword">if</span> (err) <span class="hljs-keyword">throw</span> err;
32690 …class="language-js mjs"><span class="hljs-keyword">import</span> { open, close } <span class="hljs
32692-title function_">open</span>(<span class="hljs-string">'myfile'</span>, <span class="hljs-string"…
32693 <span class="hljs-keyword">if</span> (err) {
32694 …<span class="hljs-keyword">if</span> (err.<span class="hljs-property">code</span> === <span class=…
32695 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">error</spa…
32696 <span class="hljs-keyword">return</span>;
32699 <span class="hljs-keyword">throw</span> err;
32702 <span class="hljs-keyword">try</span> {
32703 <span class="hljs-title function_">readMyData</span>(fd);
32704 } <span class="hljs-keyword">finally</span> {
32705 …<span class="hljs-title function_">close</span>(fd, <span class="hljs-function">(<span class="hljs
32706 <span class="hljs-keyword">if</span> (err) <span class="hljs-keyword">throw</span> err;
32716 <p>On Windows, access-control policies (ACLs) on a directory may limit access to
32739-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | <a href="#bu…
32740 <li><code>data</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
32741 …lla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type">&#x3C;Object></a> …
32743 …zilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | …
32744 <li><code>mode</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
32745 <li><code>flag</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
32748 <li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Referenc…
32750 <li><code>err</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Glo…
32758 …class="language-js mjs"><span class="hljs-keyword">import</span> { appendFile } <span class="hljs-
32760-title function_">appendFile</span>(<span class="hljs-string">'message.txt'</span>, <span class="h…
32761 <span class="hljs-keyword">if</span> (err) <span class="hljs-keyword">throw</span> err;
32762 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>…
32765 …class="language-js mjs"><span class="hljs-keyword">import</span> { appendFile } <span class="hljs-
32767-title function_">appendFile</span>(<span class="hljs-string">'message.txt'</span>, <span class="h…
32771 …"language-js mjs"><span class="hljs-keyword">import</span> { open, close, appendFile } <span class…
32773 <span class="hljs-keyword">function</span> <span class="hljs-title function_">closeFd</span>(<span …
32774 …<span class="hljs-title function_">close</span>(fd, <span class="hljs-function">(<span class="hljs
32775 <span class="hljs-keyword">if</span> (err) <span class="hljs-keyword">throw</span> err;
32779-title function_">open</span>(<span class="hljs-string">'message.txt'</span>, <span class="hljs-st…
32780 <span class="hljs-keyword">if</span> (err) <span class="hljs-keyword">throw</span> err;
32782 <span class="hljs-keyword">try</span> {
32783-title function_">appendFile</span>(fd, <span class="hljs-string">'data to append'</span>, <span c…
32784 <span class="hljs-title function_">closeFd</span>(fd);
32785 <span class="hljs-keyword">if</span> (err) <span class="hljs-keyword">throw</span> err;
32787 } <span class="hljs-keyword">catch</span> (err) {
32788 <span class="hljs-title function_">closeFd</span>(fd);
32789 <span class="hljs-keyword">throw</span> err;
32809 <li><code>path</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
32810 …zilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | …
32811 <li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Referenc…
32813 <li><code>err</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Glo…
32819 <p>See the POSIX <a href="http://man7.org/linux/man-pages/man2/chmod.2.html"><code>chmod(2)</code><…
32820 …de class="language-js mjs"><span class="hljs-keyword">import</span> { chmod } <span class="hljs-ke…
32822-title function_">chmod</span>(<span class="hljs-string">'my_file.txt'</span>, <span class="hljs-n…
32823 <span class="hljs-keyword">if</span> (err) <span class="hljs-keyword">throw</span> err;
32824 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>…
32887 octal digits (e.g. <code>765</code>). The left-most digit (<code>7</code> in the example), specifies
32889 specifies permissions for the group. The right-most digit (<code>5</code> in the example),
32940 <code>0o777</code> may result in platform-specific behaviors that are not supported to work
32963 <li><code>path</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
32964 <li><code>uid</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structur…
32965 <li><code>gid</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structur…
32966 <li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Referenc…
32968 <li><code>err</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Glo…
32974 <p>See the POSIX <a href="http://man7.org/linux/man-pages/man2/chown.2.html"><code>chown(2)</code><…
32992 <li><code>fd</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structure…
32993 <li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Referenc…
32995 <li><code>err</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Glo…
33003 <p>See the POSIX <a href="http://man7.org/linux/man-pages/man2/close.2.html"><code>close(2)</code><…
33017 <li><code>src</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structur…
33018 <li><code>dest</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
33019 <li><code>mode</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
33020 <li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Referenc…
33035 copy-on-write reflink. If the platform does not support copy-on-write, then a
33038 create a copy-on-write reflink. If the platform does not support
33039 copy-on-write, then the operation will fail.</li>
33041 …s="language-js mjs"><span class="hljs-keyword">import</span> { copyFile, constants } <span class="…
33043 <span class="hljs-keyword">function</span> <span class="hljs-title function_">callback</span>(<span…
33044 <span class="hljs-keyword">if</span> (err) <span class="hljs-keyword">throw</span> err;
33045 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>…
33048 <span class="hljs-comment">// destination.txt will be created or overwritten by default.</span>
33049 <span class="hljs-title function_">copyFile</span>(<span class="hljs-string">'source.txt'</span>, <…
33051 <span class="hljs-comment">// By using COPYFILE_EXCL, the operation will fail if destination.txt ex…
33052-title function_">copyFile</span>(<span class="hljs-string">'source.txt'</span>, <span class="hljs
33078 <li><code>path</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
33079 …zilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | …
33081 <li><code>flags</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struct…
33083 <li><code>encoding</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_str…
33084 <li><code>fd</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structure…
33085 <li><code>mode</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
33086 <li><code>autoClose</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_st…
33087 <li><code>emitClose</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_st…
33088 <li><code>start</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struct…
33089 <li><code>end</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structur…
33090 <li><code>highWaterMark</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Dat…
33091 …lla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type">&#x3C;Object></a> …
33101 [0, <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Numbe…
33107 emitted. <code>fd</code> should be blocking; non-blocking <code>fd</code>s should be passed to
33119 …ss="language-js mjs"><span class="hljs-keyword">import</span> { createReadStream } <span class="hl…
33121 <span class="hljs-comment">// Create a stream from some character device.</span>
33122 <span class="hljs-keyword">const</span> stream = <span class="hljs-title function_">createReadStrea…
33123 <span class="hljs-built_in">setTimeout</span>(<span class="hljs-function">() =></span> {
33124 …stream.<span class="hljs-title function_">close</span>(); <span class="hljs-comment">// This may n…
33125 …<span class="hljs-comment">// Artificially marking end-of-stream, as if the underlying resource ha…
33126 <span class="hljs-comment">// indicated end-of-file by itself, allows the stream to close.</span>
33127 …<span class="hljs-comment">// This does not cancel pending read operations, and if there is such a…
33128 …<span class="hljs-comment">// operation, the process may still not be able to exit successfully</s…
33129 <span class="hljs-comment">// until it finishes.</span>
33130 stream.<span class="hljs-title function_">push</span>(<span class="hljs-literal">null</span>);
33131 stream.<span class="hljs-title function_">read</span>(<span class="hljs-number">0</span>);
33132 }, <span class="hljs-number">100</span>);</code></pre>
33141 …ss="language-js mjs"><span class="hljs-keyword">import</span> { createReadStream } <span class="hl…
33143-title function_">createReadStream</span>(<span class="hljs-string">'sample.txt'</span>, { <span c…
33170 <li><code>path</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
33171 …zilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | …
33173 <li><code>flags</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struct…
33175 <li><code>encoding</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_str…
33176 <li><code>fd</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structure…
33177 <li><code>mode</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
33178 <li><code>autoClose</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_st…
33179 <li><code>emitClose</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_st…
33180 <li><code>start</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struct…
33181 …lla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type">&#x3C;Object></a> …
33188 [0, <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Numbe…
33206 <code>'open'</code> event will be emitted. <code>fd</code> should be blocking; non-blocking <code>f…
33223 … api_stability_0"><a href="#documentation_stability_index">Stability: 0</a> - Deprecated: Use <a h…
33225 <li><code>path</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
33226 <li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Referenc…
33228 <li><code>exists</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struc…
33234 …e class="language-js mjs"><span class="hljs-keyword">import</span> { exists } <span class="hljs-ke…
33236 …n class="hljs-title function_">exists</span>(<span class="hljs-string">'/etc/passwd'</span>, <span…
33237 …s-variable language_">console</span>.<span class="hljs-title function_">log</span>(e ? <span class…
33250 …s="language-js mjs"><span class="hljs-keyword">import</span> { exists, open, close } <span class="…
33252 …pan class="hljs-title function_">exists</span>(<span class="hljs-string">'myfile'</span>, <span cl…
33253 <span class="hljs-keyword">if</span> (e) {
33254 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">error</spa…
33255 } <span class="hljs-keyword">else</span> {
33256-title function_">open</span>(<span class="hljs-string">'myfile'</span>, <span class="hljs-string"…
33257 <span class="hljs-keyword">if</span> (err) <span class="hljs-keyword">throw</span> err;
33259 <span class="hljs-keyword">try</span> {
33260 <span class="hljs-title function_">writeMyData</span>(fd);
33261 } <span class="hljs-keyword">finally</span> {
33262 …<span class="hljs-title function_">close</span>(fd, <span class="hljs-function">(<span class="hljs
33263 <span class="hljs-keyword">if</span> (err) <span class="hljs-keyword">throw</span> err;
33270 …class="language-js mjs"><span class="hljs-keyword">import</span> { open, close } <span class="hljs
33271-title function_">open</span>(<span class="hljs-string">'myfile'</span>, <span class="hljs-string"…
33272 <span class="hljs-keyword">if</span> (err) {
33273 …<span class="hljs-keyword">if</span> (err.<span class="hljs-property">code</span> === <span class=…
33274 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">error</spa…
33275 <span class="hljs-keyword">return</span>;
33278 <span class="hljs-keyword">throw</span> err;
33281 <span class="hljs-keyword">try</span> {
33282 <span class="hljs-title function_">writeMyData</span>(fd);
33283 } <span class="hljs-keyword">finally</span> {
33284 …<span class="hljs-title function_">close</span>(fd, <span class="hljs-function">(<span class="hljs
33285 <span class="hljs-keyword">if</span> (err) <span class="hljs-keyword">throw</span> err;
33290 …s="language-js mjs"><span class="hljs-keyword">import</span> { open, close, exists } <span class="…
33292 …pan class="hljs-title function_">exists</span>(<span class="hljs-string">'myfile'</span>, <span cl…
33293 <span class="hljs-keyword">if</span> (e) {
33294-title function_">open</span>(<span class="hljs-string">'myfile'</span>, <span class="hljs-string"…
33295 <span class="hljs-keyword">if</span> (err) <span class="hljs-keyword">throw</span> err;
33297 <span class="hljs-keyword">try</span> {
33298 <span class="hljs-title function_">readMyData</span>(fd);
33299 } <span class="hljs-keyword">finally</span> {
33300 …<span class="hljs-title function_">close</span>(fd, <span class="hljs-function">(<span class="hljs
33301 <span class="hljs-keyword">if</span> (err) <span class="hljs-keyword">throw</span> err;
33305 } <span class="hljs-keyword">else</span> {
33306 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">error</spa…
33310 …class="language-js mjs"><span class="hljs-keyword">import</span> { open, close } <span class="hljs
33312-title function_">open</span>(<span class="hljs-string">'myfile'</span>, <span class="hljs-string"…
33313 <span class="hljs-keyword">if</span> (err) {
33314 …<span class="hljs-keyword">if</span> (err.<span class="hljs-property">code</span> === <span class=…
33315 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">error</spa…
33316 <span class="hljs-keyword">return</span>;
33319 <span class="hljs-keyword">throw</span> err;
33322 <span class="hljs-keyword">try</span> {
33323 <span class="hljs-title function_">readMyData</span>(fd);
33324 } <span class="hljs-keyword">finally</span> {
33325 …<span class="hljs-title function_">close</span>(fd, <span class="hljs-function">(<span class="hljs
33326 <span class="hljs-keyword">if</span> (err) <span class="hljs-keyword">throw</span> err;
33351 <li><code>fd</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structure…
33352 …zilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | …
33353 <li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Referenc…
33355 <li><code>err</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Glo…
33361 <p>See the POSIX <a href="http://man7.org/linux/man-pages/man2/fchmod.2.html"><code>fchmod(2)</code…
33377 <li><code>fd</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structure…
33378 <li><code>uid</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structur…
33379 <li><code>gid</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structur…
33380 <li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Referenc…
33382 <li><code>err</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Glo…
33388 <p>See the POSIX <a href="http://man7.org/linux/man-pages/man2/fchown.2.html"><code>fchown(2)</code…
33404 <li><code>fd</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structure…
33405 <li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Referenc…
33407 <li><code>err</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Glo…
33413 <a href="http://man7.org/linux/man-pages/man2/fdatasync.2.html"><code>fdatasync(2)</code></a> docum…
33432 <li><code>fd</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structure…
33433 <li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference…
33435 <li><code>bigint</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struc…
33439 <li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Referenc…
33441 <li><code>err</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Glo…
33447 <p>See the POSIX <a href="http://man7.org/linux/man-pages/man2/fstat.2.html"><code>fstat(2)</code><…
33463 <li><code>fd</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structure…
33464 <li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Referenc…
33466 <li><code>err</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Glo…
33472 Refer to the POSIX <a href="http://man7.org/linux/man-pages/man2/fsync.2.html"><code>fsync(2)</code…
33489 <li><code>fd</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structure…
33490 <li><code>len</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structur…
33491 <li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Referenc…
33493 <li><code>err</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Glo…
33499 <p>See the POSIX <a href="http://man7.org/linux/man-pages/man2/ftruncate.2.html"><code>ftruncate(2)…
33504 …"language-js mjs"><span class="hljs-keyword">import</span> { open, close, ftruncate } <span class=…
33506 <span class="hljs-keyword">function</span> <span class="hljs-title function_">closeFd</span>(<span …
33507 …<span class="hljs-title function_">close</span>(fd, <span class="hljs-function">(<span class="hljs
33508 <span class="hljs-keyword">if</span> (err) <span class="hljs-keyword">throw</span> err;
33512-title function_">open</span>(<span class="hljs-string">'temp.txt'</span>, <span class="hljs-strin…
33513 <span class="hljs-keyword">if</span> (err) <span class="hljs-keyword">throw</span> err;
33515 <span class="hljs-keyword">try</span> {
33516 …pan class="hljs-title function_">ftruncate</span>(fd, <span class="hljs-number">4</span>, <span cl…
33517 <span class="hljs-title function_">closeFd</span>(fd);
33518 <span class="hljs-keyword">if</span> (err) <span class="hljs-keyword">throw</span> err;
33520 } <span class="hljs-keyword">catch</span> (err) {
33521 <span class="hljs-title function_">closeFd</span>(fd);
33522 <span class="hljs-keyword">if</span> (err) <span class="hljs-keyword">throw</span> err;
33545 <li><code>fd</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structure…
33546-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;number></a> | <a href="htt…
33547-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;number></a> | <a href="htt…
33548 <li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Referenc…
33550 <li><code>err</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Glo…
33573 <li><code>path</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
33574 <li><code>mode</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
33575 <li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Referenc…
33577 <li><code>err</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Glo…
33597 <td><p>Documentation-only deprecation.</p></td></tr>
33602 <li><code>path</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
33603 <li><code>uid</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structur…
33604 <li><code>gid</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structur…
33605 <li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Referenc…
33607 <li><code>err</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Glo…
33613 <p>See the POSIX <a href="http://man7.org/linux/man-pages/man2/lchown.2.html"><code>lchown(2)</code…
33619 <li><code>path</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
33620-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;number></a> | <a href="htt…
33621-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;number></a> | <a href="htt…
33622 <li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Referenc…
33624 <li><code>err</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Glo…
33651 <li><code>existingPath</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data…
33652 <li><code>newPath</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_stru…
33653 <li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Referenc…
33655 <li><code>err</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Glo…
33660 <a href="http://man7.org/linux/man-pages/man2/link.2.html"><code>link(2)</code></a> documentation f…
33681 <li><code>path</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
33682 <li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference…
33684 <li><code>bigint</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struc…
33688 <li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Referenc…
33690 <li><code>err</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Glo…
33698 link, then the link itself is stat-ed, not the file that it refers to.</p>
33699 <p>See the POSIX <a href="http://man7.org/linux/man-pages/man2/lstat.2.html"><code>lstat(2)</code><…
33721 <li><code>path</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
33722 …lla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type">&#x3C;Object></a> …
33724 <li><code>recursive</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_st…
33725 …zilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | …
33728 <li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Referenc…
33730 <li><code>err</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Glo…
33744 …de class="language-js mjs"><span class="hljs-keyword">import</span> { mkdir } <span class="hljs-ke…
33746 <span class="hljs-comment">// Creates /tmp/a/apple, regardless of whether `/tmp` and /tmp/a exist.<…
33747-title function_">mkdir</span>(<span class="hljs-string">'/tmp/a/apple'</span>, { <span class="hlj…
33748 <span class="hljs-keyword">if</span> (err) <span class="hljs-keyword">throw</span> err;
33752 …de class="language-js mjs"><span class="hljs-keyword">import</span> { mkdir } <span class="hljs-ke…
33754-title function_">mkdir</span>(<span class="hljs-string">'/'</span>, { <span class="hljs-attr">rec…
33755 <span class="hljs-comment">// => [Error: EPERM: operation not permitted, mkdir 'C:\']</span>
33757 <p>See the POSIX <a href="http://man7.org/linux/man-pages/man2/mkdir.2.html"><code>mkdir(2)</code><…
33777 <li><code>prefix</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struc…
33778 …zilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | …
33780 <li><code>encoding</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_str…
33783 <li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Referenc…
33785 <li><code>err</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Glo…
33786 <li><code>directory</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_st…
33800 …e class="language-js mjs"><span class="hljs-keyword">import</span> { mkdtemp } <span class="hljs-k…
33802-title function_">mkdtemp</span>(path.<span class="hljs-title function_">join</span>(os.<span clas…
33803 <span class="hljs-keyword">if</span> (err) <span class="hljs-keyword">throw</span> err;
33804 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>…
33805 …<span class="hljs-comment">// Prints: /tmp/foo-itXde2 or C:\Users\...\AppData\Local\Temp\foo-itXde…
33810 must end with a trailing platform-specific path separator
33812 …e class="language-js mjs"><span class="hljs-keyword">import</span> { tmpdir } <span class="hljs-ke…
33813 <span class="hljs-keyword">import</span> { mkdtemp } <span class="hljs-keyword">from</span> <span c…
33815 <span class="hljs-comment">// The parent directory for the new temporary directory</span>
33816 <span class="hljs-keyword">const</span> tmpDir = <span class="hljs-title function_">tmpdir</span>();
33818 <span class="hljs-comment">// This method is *INCORRECT*:</span>
33819 <span class="hljs-title function_">mkdtemp</span>(tmpDir, <span class="hljs-function">(<span class=…
33820 <span class="hljs-keyword">if</span> (err) <span class="hljs-keyword">throw</span> err;
33821 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>…
33822 <span class="hljs-comment">// Will print something similar to `/tmpabc123`.</span>
33823 <span class="hljs-comment">// A new temporary directory is created at the file system root</span>
33824 <span class="hljs-comment">// rather than *within* the /tmp directory.</span>
33827 <span class="hljs-comment">// This method is *CORRECT*:</span>
33828 <span class="hljs-keyword">import</span> { sep } <span class="hljs-keyword">from</span> <span class…
33829-title function_">mkdtemp</span>(<span class="hljs-string">`<span class="hljs-subst">${tmpDir}</sp…
33830 <span class="hljs-keyword">if</span> (err) <span class="hljs-keyword">throw</span> err;
33831 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>…
33832 <span class="hljs-comment">// Will print something similar to `/tmp/abc123`.</span>
33833 <span class="hljs-comment">// A new temporary directory is created within</span>
33834 <span class="hljs-comment">// the /tmp directory.</span>
33853 <li><code>path</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
33854 …zilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | …
33856 …zilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | …
33857 <li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Referenc…
33859 <li><code>err</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Glo…
33860 <li><code>fd</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structure…
33864 <p>Asynchronous file open. See the POSIX <a href="http://man7.org/linux/man-pages/man2/open.2.html"…
33870 by <a href="https://docs.microsoft.com/en-us/windows/desktop/FileIO/naming-a-file">Naming Files, Pa…
33872 <a href="https://docs.microsoft.com/en-us/windows/desktop/FileIO/using-streams">this MSDN page</a>.…
33888 <li><code>path</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
33889 <li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference…
33891 …zilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | …
33892 <li><code>bufferSize</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_s…
33897 <li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Referenc…
33899 <li><code>err</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Glo…
33904 <p>Asynchronously open a directory. See the POSIX <a href="http://man7.org/linux/man-pages/man3/ope…
33927 <li><code>fd</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structure…
33928 …org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray" class="type">&#x3C;TypedArray><…
33930 <li><code>offset</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struc…
33932 <li><code>length</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struc…
33934 …zilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;integer></a> |…
33935 file. If <code>position</code> is <code>null</code> or <code>-1 </code>, data will be read from the…
33938 <li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Referenc…
33940 <li><code>err</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Glo…
33941 <li><code>bytesRead</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_st…
33948 <p>If the file is not modified concurrently, the end-of-file is reached when the
33965 <li><code>fd</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structure…
33966 <li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference…
33968 …org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray" class="type">&#x3C;TypedArray><…
33969 <li><code>offset</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struc…
33970 <li><code>length</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struc…
33971 …zilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;integer></a> |…
33974 <li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Referenc…
33976 <li><code>err</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Glo…
33977 <li><code>bytesRead</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_st…
34006 <li><code>path</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
34007 …zilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | …
34009 <li><code>encoding</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_str…
34010 <li><code>withFileTypes</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Dat…
34013 <li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Referenc…
34015 <li><code>err</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Glo…
34016 <li><code>files</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struct…
34023 <p>See the POSIX <a href="http://man7.org/linux/man-pages/man3/readdir.3.html"><code>readdir(3)</co…
34053-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | <a href="#bu…
34054 …lla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type">&#x3C;Object></a> …
34056 …zilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | …
34057 <li><code>flag</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
34058 …class_abortsignal" class="type">&#x3C;AbortSignal></a> allows aborting an in-progress readFile</li>
34061 <li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Referenc…
34063 <li><code>err</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Glo…
34064 <li><code>data</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
34069 … class="language-js mjs"><span class="hljs-keyword">import</span> { readFile } <span class="hljs-k…
34071 … class="hljs-title function_">readFile</span>(<span class="hljs-string">'/etc/passwd'</span>, <spa…
34072 <span class="hljs-keyword">if</span> (err) <span class="hljs-keyword">throw</span> err;
34073 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>…
34079 … class="language-js mjs"><span class="hljs-keyword">import</span> { readFile } <span class="hljs-k…
34081 <span class="hljs-title function_">readFile</span>(<span class="hljs-string">'/etc/passwd'</span>, …
34083 <a href="#fs_fs_readfilesync_path_options"><code>fs.readFileSync()</code></a> is platform-specific.…
34086 … class="language-js mjs"><span class="hljs-keyword">import</span> { readFile } <span class="hljs-k…
34088 <span class="hljs-comment">// macOS, Linux, and Windows</span>
34089 …ass="hljs-title function_">readFile</span>(<span class="hljs-string">'&#x3C;directory>'</span>, <s…
34090 …<span class="hljs-comment">// => [Error: EISDIR: illegal operation on a directory, read &#x3C;dire…
34093 <span class="hljs-comment">// FreeBSD</span>
34094 …ass="hljs-title function_">readFile</span>(<span class="hljs-string">'&#x3C;directory>'</span>, <s…
34095 <span class="hljs-comment">// => null, &#x3C;data></span>
34099 … class="language-js mjs"><span class="hljs-keyword">import</span> { readFile } <span class="hljs-k…
34101 <span class="hljs-keyword">const</span> controller = <span class="hljs-keyword">new</span> <span cl…
34102 <span class="hljs-keyword">const</span> signal = controller.<span class="hljs-property">signal</spa…
34103-title function_">readFile</span>(fileInfo[<span class="hljs-number">0</span>].<span class="hljs-p…
34104 <span class="hljs-comment">// ...</span>
34106 <span class="hljs-comment">// When you want to abort the request</span>
34107 controller.<span class="hljs-title function_">abort</span>();</code></pre>
34133 <p>For applications that require as-fast-as-possible reading of file contents, it
34156 <li><code>path</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
34157 …zilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | …
34159 <li><code>encoding</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_str…
34162 <li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Referenc…
34164 <li><code>err</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Glo…
34165 <li><code>linkString</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_s…
34171 <p>See the POSIX <a href="http://man7.org/linux/man-pages/man2/readlink.2.html"><code>readlink(2)</…
34181 <li><code>fd</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structure…
34182 <li><code>buffers</code> <a href="https://developer.mozilla.org/en-US/docs/Web/API/ArrayBufferView"…
34183 <li><code>position</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_str…
34184 <li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Referenc…
34186 <li><code>err</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Glo…
34187 <li><code>bytesRead</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_st…
34188 <li><code>buffers</code> <a href="https://developer.mozilla.org/en-US/docs/Web/API/ArrayBufferView"…
34224 <li><code>path</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
34225 …zilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | …
34227 <li><code>encoding</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_str…
34230 <li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Referenc…
34232 <li><code>err</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Glo…
34233 <li><code>resolvedPath</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data…
34241 <p>This function behaves like <a href="http://man7.org/linux/man-pages/man3/realpath.3.html"><code>…
34244 <p>No case conversion is performed on case-insensitive file systems.</p>
34247 <p>The maximum number of symbolic links is platform-independent and generally
34248 (much) higher than what the native <a href="http://man7.org/linux/man-pages/man3/realpath.3.html"><…
34265 <li><code>path</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
34266 …zilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | …
34268 <li><code>encoding</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_str…
34271 <li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Referenc…
34273 <li><code>err</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Glo…
34274 <li><code>resolvedPath</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data…
34278 <p>Asynchronous <a href="http://man7.org/linux/man-pages/man3/realpath.3.html"><code>realpath(3)</c…
34305 <li><code>oldPath</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_stru…
34306 <li><code>newPath</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_stru…
34307 <li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Referenc…
34309 <li><code>err</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Glo…
34318 <p>See also: <a href="http://man7.org/linux/man-pages/man2/rename.2.html"><code>rename(2)</code></a…
34319 …e class="language-js mjs"><span class="hljs-keyword">import</span> { rename } <span class="hljs-ke…
34321-title function_">rename</span>(<span class="hljs-string">'oldFile.txt'</span>, <span class="hljs-
34322 <span class="hljs-keyword">if</span> (err) <span class="hljs-keyword">throw</span> err;
34323 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>…
34348 <li><code>path</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
34349 <li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference…
34351 <li><code>maxRetries</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_s…
34356 <li><code>recursive</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_st…
34359 <li><code>retryDelay</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_s…
34364 <li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Referenc…
34366 <li><code>err</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Glo…
34370 <p>Asynchronous <a href="http://man7.org/linux/man-pages/man2/rmdir.2.html"><code>rmdir(2)</code></…
34375 <code>rm -rf</code>: an error will not be raised for paths that do not exist, and paths
34384 <li><code>path</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
34385 <li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference…
34387 <li><code>force</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struct…
34389 <li><code>maxRetries</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_s…
34394 <li><code>recursive</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_st…
34396 <li><code>retryDelay</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_s…
34401 <li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Referenc…
34403 <li><code>err</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Glo…
34429 <li><code>path</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
34430 <li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference…
34432 <li><code>bigint</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struc…
34436 <li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Referenc…
34438 <li><code>err</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Glo…
34443 <p>Asynchronous <a href="http://man7.org/linux/man-pages/man2/stat.2.html"><code>stat(2)</code></a>…
34453 <pre><code class="language-text">- txtDir
34454 -- file.txt
34455 - app.js</code></pre>
34457 …de class="language-js mjs"><span class="hljs-keyword">import</span> { stat } <span class="hljs-key…
34459 <span class="hljs-keyword">const</span> pathsToCheck = [<span class="hljs-string">'./txtDir'</span>…
34461 …="hljs-keyword">for</span> (<span class="hljs-keyword">let</span> i = <span class="hljs-number">0<…
34462 …<span class="hljs-title function_">stat</span>(pathsToCheck[i], <span class="hljs-function">(<span…
34463 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>…
34464 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>…
34468 <pre><code class="language-console">true
34484 atime: 2019-06-22T03:37:33.072Z,
34485 mtime: 2019-06-22T03:36:54.583Z,
34486 ctime: 2019-06-22T03:37:06.624Z,
34487 birthtime: 2019-06-22T03:28:46.937Z
34505 atime: 2019-06-22T03:36:56.619Z,
34506 mtime: 2019-06-22T03:36:54.584Z,
34507 ctime: 2019-06-22T03:36:54.584Z,
34508 birthtime: 2019-06-22T03:26:47.711Z
34510 <h4><code>fs.symlink(target, path[, type], callback)</code><span><a class="mark" href="#fs_fs_symli…
34516 <td><p>If the <code>type</code> argument is left undefined, Node will autodetect <code>target</code…
34518 <td><p>The <code>target</code> and <code>path</code> parameters can be WHATWG <code>URL</code> obje…
34525 <li><code>target</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struc…
34526 <li><code>path</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
34527 <li><code>type</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
34528 <li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Referenc…
34530 <li><code>err</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Glo…
34534 <p>Creates the link called <code>path</code> pointing to <code>target</code>. No arguments other th…
34536 <p>See the POSIX <a href="http://man7.org/linux/man-pages/man2/symlink.2.html"><code>symlink(2)</co…
34539 not set, Node.js will autodetect <code>target</code> type and use <code>'file'</code> or <code>'dir…
34540 the <code>target</code> does not exist, <code>'file'</code> will be used. Windows junction points
34542 <code>target</code> argument will automatically be normalized to absolute path.</p>
34544 …e class="language-js mjs"><span class="hljs-keyword">import</span> { symlink } <span class="hljs-k…
34546 <span class="hljs-title function_">symlink</span>(<span class="hljs-string">'./mew'</span>, <span c…
34549 <pre><code class="language-bash">$ tree example/
34552 └── mewtwo -> ./mew</code></pre>
34568 <li><code>path</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
34569 <li><code>len</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structur…
34570 <li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Referenc…
34572 <li><code>err</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Glo…
34580-flavor-selector" type="checkbox" checked aria-label="Show modern ES modules syntax"><code class="…
34581 <span class="hljs-comment">// Assuming that 'path/file.txt' is a regular file.</span>
34582 …class="hljs-title function_">truncate</span>(<span class="hljs-string">'path/file.txt'</span>, <sp…
34583 <span class="hljs-keyword">if</span> (err) <span class="hljs-keyword">throw</span> err;
34584 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>…
34585 …lass="language-js cjs"><span class="hljs-keyword">const</span> { truncate } = <span class="hljs-bu…
34586 <span class="hljs-comment">// Assuming that 'path/file.txt' is a regular file.</span>
34587 …class="hljs-title function_">truncate</span>(<span class="hljs-string">'path/file.txt'</span>, <sp…
34588 <span class="hljs-keyword">if</span> (err) <span class="hljs-keyword">throw</span> err;
34589 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>…
34593 <p>See the POSIX <a href="http://man7.org/linux/man-pages/man2/truncate.2.html"><code>truncate(2)</…
34611 <li><code>path</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
34612 <li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Referenc…
34614 <li><code>err</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Glo…
34620 …e class="language-js mjs"><span class="hljs-keyword">import</span> { unlink } <span class="hljs-ke…
34621 <span class="hljs-comment">// Assuming that 'path/file.txt' is a regular file.</span>
34622 … class="hljs-title function_">unlink</span>(<span class="hljs-string">'path/file.txt'</span>, <spa…
34623 <span class="hljs-keyword">if</span> (err) <span class="hljs-keyword">throw</span> err;
34624 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>…
34628 <p>See the POSIX <a href="http://man7.org/linux/man-pages/man2/unlink.2.html"><code>unlink(2)</code…
34634 <li><code>filename</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_str…
34635 <li><code>listener</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Referenc…
34642 no-op, not an error.</p>
34654 <td><p><code>NaN</code>, <code>Infinity</code>, and <code>-Infinity</code> are no longer valid time…
34667 <li><code>path</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
34668-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;number></a> | <a href="htt…
34669-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;number></a> | <a href="htt…
34670 <li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Referenc…
34672 <li><code>err</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Glo…
34682 <code>-Infinity</code>, an <code>Error</code> will be thrown.</li>
34701 <li><code>filename</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_str…
34702 …zilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | …
34704 <li><code>persistent</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_s…
34706 <li><code>recursive</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_st…
34710 <li><code>encoding</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_str…
34715 …a.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type">&#x3C;Function></a…
34717 <li><code>eventType</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_st…
34718 <li><code>filename</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_str…
34751 <li>On Linux systems, this uses <a href="https://man7.org/linux/man-pages/man7/inotify.7.html"><cod…
34756 …ature depends on <a href="https://docs.microsoft.com/en-us/windows/desktop/api/winbase/nf-winbase-
34757 <li>On AIX systems, this feature depends on <a href="https://developer.ibm.com/articles/au-aix_even…
34783 …de class="language-js mjs"><span class="hljs-keyword">import</span> { watch } <span class="hljs-ke…
34784 …pan class="hljs-title function_">watch</span>(<span class="hljs-string">'somedir'</span>, <span cl…
34785 …"hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class…
34786 <span class="hljs-keyword">if</span> (filename) {
34787 …ljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="…
34788 } <span class="hljs-keyword">else</span> {
34789 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>…
34807 <li><code>filename</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_str…
34808 <li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference…
34810 <li><code>bigint</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struc…
34811 <li><code>persistent</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_s…
34812 <li><code>interval</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_str…
34815 <li><code>listener</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Referenc…
34829 target should be polled in milliseconds.</p>
34832 … class="language-js mjs"><span class="hljs-keyword">import</span> { watchFile } <span class="hljs-
34834 …class="hljs-title function_">watchFile</span>(<span class="hljs-string">'message.text'</span>, <sp…
34835 …js-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="h…
34836 …s-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hl…
34882 <li><code>fd</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structure…
34883 …org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray" class="type">&#x3C;TypedArray><…
34884 <li><code>offset</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struc…
34885 <li><code>length</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struc…
34886 <li><code>position</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_str…
34887 <li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Referenc…
34889 <li><code>err</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Glo…
34890 <li><code>bytesWritten</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data…
34891 …org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray" class="type">&#x3C;TypedArray><…
34900 at the current position. See <a href="http://man7.org/linux/man-pages/man2/pwrite.2.html"><code>pwr…
34932 <li><code>fd</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structure…
34933 …zilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | …
34934 <li><code>position</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_str…
34935 <li><code>encoding</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_str…
34936 <li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Referenc…
34938 <li><code>err</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Glo…
34939 <li><code>written</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_stru…
34940 <li><code>string</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struc…
34948 the current position. See <a href="http://man7.org/linux/man-pages/man2/pwrite.2.html"><code>pwrite…
34961 or <code>stdout</code>) a string containing non-ASCII characters will not be rendered
34963 It is possible to configure the console to render UTF-8 properly by changing the
34993-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | <a href="#bu…
34994-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | <a href="#bu…
34995 …lla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type">&#x3C;Object></a> …
34997 …zilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | …
34998 <li><code>mode</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
34999 <li><code>flag</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
35000 …class_abortsignal" class="type">&#x3C;AbortSignal></a> allows aborting an in-progress writeFile</l…
35003 <li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Referenc…
35005 <li><code>err</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Glo…
35019 … class="language-js mjs"><span class="hljs-keyword">import</span> { writeFile } <span class="hljs-
35021-keyword">const</span> data = <span class="hljs-keyword">new</span> <span class="hljs-title class_…
35022 …ss="hljs-title function_">writeFile</span>(<span class="hljs-string">'message.txt'</span>, data, <…
35023 <span class="hljs-keyword">if</span> (err) <span class="hljs-keyword">throw</span> err;
35024 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>…
35027 … class="language-js mjs"><span class="hljs-keyword">import</span> { writeFile } <span class="hljs-
35029 …s-title function_">writeFile</span>(<span class="hljs-string">'message.txt'</span>, <span class="h…
35033 <p>Similarly to <code>fs.readFile</code> - <code>fs.writeFile</code> is a convenience method that
35039 … class="language-js mjs"><span class="hljs-keyword">import</span> { writeFile } <span class="hljs-
35041 <span class="hljs-keyword">const</span> controller = <span class="hljs-keyword">new</span> <span cl…
35042 <span class="hljs-keyword">const</span> { signal } = controller;
35043-keyword">const</span> data = <span class="hljs-keyword">new</span> <span class="hljs-title class_…
35044 …js-title function_">writeFile</span>(<span class="hljs-string">'message.txt'</span>, data, { signa…
35045 …<span class="hljs-comment">// When a request is aborted - the callback is called with an AbortErro…
35047 <span class="hljs-comment">// When the request should be aborted</span>
35048 controller.<span class="hljs-title function_">abort</span>();</code></pre>
35054 …de class="language-js mjs"><span class="hljs-keyword">import</span> { write } <span class="hljs-ke…
35056-title function_">write</span>(fd, <span class="hljs-title class_">Buffer</span>.<span class="hljs
35076 <li><code>fd</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structure…
35077 <li><code>buffers</code> <a href="https://developer.mozilla.org/en-US/docs/Web/API/ArrayBufferView"…
35078 <li><code>position</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_str…
35079 <li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Referenc…
35081 <li><code>err</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Glo…
35082 <li><code>bytesWritten</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data…
35083 <li><code>buffers</code> <a href="https://developer.mozilla.org/en-US/docs/Web/API/ArrayBufferView"…
35117 <li><code>path</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
35118 <li><code>mode</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
35128 …="language-js mjs"><span class="hljs-keyword">import</span> { accessSync, constants } <span class=…
35130 <span class="hljs-keyword">try</span> {
35131-title function_">accessSync</span>(<span class="hljs-string">'etc/passwd'</span>, constants.<span…
35132 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>…
35133 } <span class="hljs-keyword">catch</span> (err) {
35134 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">error</spa…
35151-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | <a href="#bu…
35152 <li><code>data</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
35153 …lla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type">&#x3C;Object></a> …
35155 …zilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | …
35156 <li><code>mode</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
35157 <li><code>flag</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
35165 …ass="language-js mjs"><span class="hljs-keyword">import</span> { appendFileSync } <span class="hlj…
35167 <span class="hljs-keyword">try</span> {
35168 …<span class="hljs-title function_">appendFileSync</span>(<span class="hljs-string">'message.txt'</…
35169 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>…
35170 } <span class="hljs-keyword">catch</span> (err) {
35171 <span class="hljs-comment">/* Handle the error */</span>
35174 …ass="language-js mjs"><span class="hljs-keyword">import</span> { appendFileSync } <span class="hlj…
35176-title function_">appendFileSync</span>(<span class="hljs-string">'message.txt'</span>, <span clas…
35180 …age-js mjs"><span class="hljs-keyword">import</span> { openSync, closeSync, appendFileSync } <span…
35182 <span class="hljs-keyword">let</span> fd;
35184 <span class="hljs-keyword">try</span> {
35185 …fd = <span class="hljs-title function_">openSync</span>(<span class="hljs-string">'message.txt'</s…
35186 …<span class="hljs-title function_">appendFileSync</span>(fd, <span class="hljs-string">'data to ap…
35187 } <span class="hljs-keyword">catch</span> (err) {
35188 <span class="hljs-comment">/* Handle the error */</span>
35189 } <span class="hljs-keyword">finally</span> {
35190 <span class="hljs-keyword">if</span> (fd !== <span class="hljs-literal">undefined</span>)
35191 <span class="hljs-title function_">closeSync</span>(fd);
35206 <li><code>path</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
35207 …zilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | …
35211 <p>See the POSIX <a href="http://man7.org/linux/man-pages/man2/chmod.2.html"><code>chmod(2)</code><…
35225 <li><code>path</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
35226 <li><code>uid</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structur…
35227 <li><code>gid</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structur…
35231 <p>See the POSIX <a href="http://man7.org/linux/man-pages/man2/chown.2.html"><code>chown(2)</code><…
35237 <li><code>fd</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structure…
35242 <p>See the POSIX <a href="http://man7.org/linux/man-pages/man2/close.2.html"><code>close(2)</code><…
35256 <li><code>src</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structur…
35257 <li><code>dest</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
35258 <li><code>mode</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
35272 copy-on-write reflink. If the platform does not support copy-on-write, then a
35275 create a copy-on-write reflink. If the platform does not support
35276 copy-on-write, then the operation will fail.</li>
35278 …"language-js mjs"><span class="hljs-keyword">import</span> { copyFileSync, constants } <span class…
35280 <span class="hljs-comment">// destination.txt will be created or overwritten by default.</span>
35281 <span class="hljs-title function_">copyFileSync</span>(<span class="hljs-string">'source.txt'</span…
35282 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(…
35284 <span class="hljs-comment">// By using COPYFILE_EXCL, the operation will fail if destination.txt ex…
35285-title function_">copyFileSync</span>(<span class="hljs-string">'source.txt'</span>, <span class="…
35299 <li><code>path</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
35300 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boole…
35308 …class="language-js mjs"><span class="hljs-keyword">import</span> { existsSync } <span class="hljs-
35310 <span class="hljs-keyword">if</span> (<span class="hljs-title function_">existsSync</span>(<span cl…
35311 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>…
35317 <li><code>fd</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structure…
35318 …zilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | …
35321 <p>See the POSIX <a href="http://man7.org/linux/man-pages/man2/fchmod.2.html"><code>fchmod(2)</code…
35327 <li><code>fd</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structure…
35328 <li><code>uid</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structur…
35329 <li><code>gid</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structur…
35332 <p>See the POSIX <a href="http://man7.org/linux/man-pages/man2/fchown.2.html"><code>fchown(2)</code…
35338 <li><code>fd</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structure…
35342 <a href="http://man7.org/linux/man-pages/man2/fdatasync.2.html"><code>fdatasync(2)</code></a> docum…
35356 <li><code>fd</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structure…
35357 <li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference…
35359 <li><code>bigint</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struc…
35366 <p>See the POSIX <a href="http://man7.org/linux/man-pages/man2/fstat.2.html"><code>fstat(2)</code><…
35372 <li><code>fd</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structure…
35376 Refer to the POSIX <a href="http://man7.org/linux/man-pages/man2/fsync.2.html"><code>fsync(2)</code…
35382 <li><code>fd</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structure…
35383 <li><code>len</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structur…
35401 <li><code>fd</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structure…
35402-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;number></a> | <a href="htt…
35403-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;number></a> | <a href="htt…
35411 <li><code>path</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
35412 <li><code>mode</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
35425 <td><p>Documentation-only deprecation.</p></td></tr>
35430 <li><code>path</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
35431 <li><code>uid</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structur…
35432 <li><code>gid</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structur…
35435 <p>See the POSIX <a href="http://man7.org/linux/man-pages/man2/lchown.2.html"><code>lchown(2)</code…
35441 <li><code>path</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
35442-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;number></a> | <a href="htt…
35443-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;number></a> | <a href="htt…
35461 <li><code>existingPath</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data…
35462 <li><code>newPath</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_stru…
35465 <a href="http://man7.org/linux/man-pages/man2/link.2.html"><code>link(2)</code></a> documentation f…
35483 <li><code>path</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
35484 <li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference…
35486 <li><code>bigint</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struc…
35488 <li><code>throwIfNoEntry</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Da…
35496 <p>See the POSIX <a href="http://man7.org/linux/man-pages/man2/lstat.2.html"><code>lstat(2)</code><…
35514 <li><code>path</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
35515 …lla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type">&#x3C;Object></a> …
35517 <li><code>recursive</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_st…
35518 …zilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | …
35521 …zilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | …
35526 <p>See the POSIX <a href="http://man7.org/linux/man-pages/man2/mkdir.2.html"><code>mkdir(2)</code><…
35540 <li><code>prefix</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struc…
35541 …zilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | …
35543 <li><code>encoding</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_str…
35546 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Strin…
35566 <li><code>path</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
35567 <li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference…
35569 …zilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | …
35570 <li><code>bufferSize</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_s…
35577 <p>Synchronously open a directory. See <a href="http://man7.org/linux/man-pages/man3/opendir.3.html…
35599 <li><code>path</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
35600 …zilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | …
35602 …zilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | …
35603 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Numbe…
35623 <li><code>path</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
35624 …zilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | …
35626 <li><code>encoding</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_str…
35627 <li><code>withFileTypes</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Dat…
35630 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Strin…
35633 <p>See the POSIX <a href="http://man7.org/linux/man-pages/man3/readdir.3.html"><code>readdir(3)</co…
35655-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | <a href="#bu…
35656 …lla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type">&#x3C;Object></a> …
35658 …zilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | …
35659 <li><code>flag</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
35662 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Strin…
35670 <code>fs.readFileSync()</code> is platform-specific.</p>
35671 …lass="language-js mjs"><span class="hljs-keyword">import</span> { readFileSync } <span class="hljs
35673 <span class="hljs-comment">// macOS, Linux, and Windows</span>
35674 <span class="hljs-title function_">readFileSync</span>(<span class="hljs-string">'&#x3C;directory>'…
35675 <span class="hljs-comment">// => [Error: EISDIR: illegal operation on a directory, read &#x3C;direc…
35677 <span class="hljs-comment">// FreeBSD</span>
35678 <span class="hljs-title function_">readFileSync</span>(<span class="hljs-string">'&#x3C;directory>'…
35692 <li><code>path</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
35693 …zilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | …
35695 <li><code>encoding</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_str…
35698 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Strin…
35701 <p>See the POSIX <a href="http://man7.org/linux/man-pages/man2/readlink.2.html"><code>readlink(2)</…
35721 <li><code>fd</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structure…
35722 …org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray" class="type">&#x3C;TypedArray><…
35723 <li><code>offset</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struc…
35724 <li><code>length</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struc…
35725 …zilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;integer></a> |…
35726 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Numbe…
35744 <li><code>fd</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structure…
35745 …org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray" class="type">&#x3C;TypedArray><…
35746 <li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference…
35748 <li><code>offset</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struc…
35749 <li><code>length</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struc…
35750 …zilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;integer></a> |…
35753 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Numbe…
35765 <li><code>fd</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structure…
35766 <li><code>buffers</code> <a href="https://developer.mozilla.org/en-US/docs/Web/API/ArrayBufferView"…
35767 <li><code>position</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_str…
35768 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Numbe…
35791 <li><code>path</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
35792 …zilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | …
35794 <li><code>encoding</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_str…
35797 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Strin…
35807 <li><code>path</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
35808 …zilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | …
35810 <li><code>encoding</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_str…
35813 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Strin…
35815 <p>Synchronous <a href="http://man7.org/linux/man-pages/man3/realpath.3.html"><code>realpath(3)</co…
35837 <li><code>oldPath</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_stru…
35838 <li><code>newPath</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_stru…
35841 <p>See the POSIX <a href="http://man7.org/linux/man-pages/man2/rename.2.html"><code>rename(2)</code…
35861 <li><code>path</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
35862 <li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference…
35864 <li><code>maxRetries</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_s…
35869 <li><code>recursive</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_st…
35872 <li><code>retryDelay</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_s…
35878 <p>Synchronous <a href="http://man7.org/linux/man-pages/man2/rmdir.2.html"><code>rmdir(2)</code></a…
35882 <code>rm -rf</code>: an error will not be raised for paths that do not exist, and paths
35891 <li><code>path</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
35892 <li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference…
35894 <li><code>force</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struct…
35896 <li><code>maxRetries</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_s…
35901 <li><code>recursive</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_st…
35903 <li><code>retryDelay</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_s…
35928 <li><code>path</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
35929 <li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference…
35931 <li><code>bigint</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struc…
35933 <li><code>throwIfNoEntry</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Da…
35941 <h4><code>fs.symlinkSync(target, path[, type])</code><span><a class="mark" href="#fs_fs_symlinksync…
35947 <td><p>If the <code>type</code> argument is left undefined, Node will autodetect <code>target</code…
35949 <td><p>The <code>target</code> and <code>path</code> parameters can be WHATWG <code>URL</code> obje…
35956 <li><code>target</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struc…
35957 <li><code>path</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
35958 <li><code>type</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
35968 <li><code>path</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
35969 <li><code>len</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structur…
35988 <li><code>path</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
35990 <p>Synchronous <a href="http://man7.org/linux/man-pages/man2/unlink.2.html"><code>unlink(2)</code><…
35997 <td><p><code>NaN</code>, <code>Infinity</code>, and <code>-Infinity</code> are no longer valid time…
36008 <li><code>path</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
36009-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;number></a> | <a href="htt…
36010-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;number></a> | <a href="htt…
36036-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | <a href="#bu…
36037-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | <a href="#bu…
36038 …lla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type">&#x3C;Object></a> …
36040 …zilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | …
36041 <li><code>mode</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
36042 <li><code>flag</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
36072 <li><code>fd</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structure…
36073 …org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray" class="type">&#x3C;TypedArray><…
36074 <li><code>offset</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struc…
36075 <li><code>length</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struc…
36076 <li><code>position</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_str…
36077 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Numbe…
36096 <li><code>fd</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structure…
36097 <li><code>string</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struc…
36098 <li><code>position</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_str…
36099 <li><code>encoding</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_str…
36100 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Numbe…
36109 <li><code>fd</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structure…
36110 <li><code>buffers</code> <a href="https://developer.mozilla.org/en-US/docs/Web/API/ArrayBufferView"…
36111 <li><code>position</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_str…
36112 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Numbe…
36126 …e class="language-js mjs"><span class="hljs-keyword">import</span> { opendir } <span class="hljs-k…
36128 <span class="hljs-keyword">try</span> {
36129 …ss="hljs-keyword">const</span> dir = <span class="hljs-keyword">await</span> <span class="hljs-tit…
36130 …n class="hljs-keyword">for</span> <span class="hljs-keyword">await</span> (<span class="hljs-keywo…
36131 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>…
36132 } <span class="hljs-keyword">catch</span> (err) {
36133 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">error</spa…
36142 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Obje…
36153 <li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Referenc…
36155 <li><code>err</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Glo…
36173 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" c…
36175 <p>The read-only path of this directory as was provided to <a href="#fs_fs_opendir_path_options_cal…
36182-US/docs/Web/JavaScript/Reference/Global_Objects/Promise" class="type">&#x3C;Promise></a> containi…
36184 <p>Asynchronously read the next directory entry via <a href="http://man7.org/linux/man-pages/man3/r…
36197 <li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Referenc…
36199 <li><code>err</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Glo…
36200 …lass="type">&#x3C;fs.Dirent></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScrip…
36204 <p>Asynchronously read the next directory entry via <a href="http://man7.org/linux/man-pages/man3/r…
36217 …lass="type">&#x3C;fs.Dirent></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScrip…
36220 POSIX <a href="http://man7.org/linux/man-pages/man3/readdir.3.html"><code>readdir(3)</code></a> doc…
36231 <li>Returns: <a href="https://tc39.github.io/ecma262/#sec-asynciterator-interface" class="type">&#x…
36234 been read. Refer to the POSIX <a href="http://man7.org/linux/man-pages/man3/readdir.3.html"><code>r…
36257 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boole…
36265 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boole…
36273 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boole…
36282 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boole…
36284 … href="#fs_class_fs_dirent" class="type">&#x3C;fs.Dirent></a> object describes a first-in-first-out
36291 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boole…
36299 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boole…
36307 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boole…
36315 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" c…
36336 <li><code>eventType</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_st…
36337 <li><code>filename</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_str…
36344 <code>filename</code> will be a UTF-8 string.</p>
36345 …de class="language-js mjs"><span class="hljs-keyword">import</span> { watch } <span class="hljs-ke…
36346 <span class="hljs-comment">// Example when handled through fs.watch() listener</span>
36347-title function_">watch</span>(<span class="hljs-string">'./tmp'</span>, { <span class="hljs-attr"…
36348 <span class="hljs-keyword">if</span> (filename) {
36349 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>…
36350 <span class="hljs-comment">// Prints: &#x3C;Buffer ...></span>
36364 <li><code>error</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/G…
36452 <li><code>fd</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structure…
36466 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" c…
36474 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" c…
36485 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" …
36506 nanosecond-precision properties suffixed with <code>Ns</code>.</p>
36507 <pre><code class="language-console">Stats {
36527 <pre><code class="language-console">BigIntStats {
36555 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boole…
36563 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boole…
36571 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boole…
36582 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boole…
36584 …="#fs_class_fs_stats" class="type">&#x3C;fs.Stats></a> object describes a first-in-first-out (FIFO)
36591 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boole…
36599 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boole…
36607 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boole…
36613 …zilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;number></a> | …
36618 …zilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;number></a> | …
36623 …zilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;number></a> | …
36625 <p>A bit-field describing the file type and mode.</p>
36628 …zilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;number></a> | …
36630 <p>The number of hard-links that exist for the file.</p>
36633 …zilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;number></a> | …
36638 …zilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;number></a> | …
36643 …zilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;number></a> | …
36648 …zilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;number></a> | …
36653 …zilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;number></a> | …
36658 …zilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;number></a> | …
36666 …zilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;number></a> | …
36675 …zilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;number></a> | …
36684 …zilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;number></a> | …
36693 …zilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;number></a> | …
36702 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/BigIn…
36713 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/BigIn…
36724 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/BigIn…
36735 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/BigIn…
36746 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date"…
36754 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date"…
36762 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date"…
36770 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date"…
36777 method that generates the object, the properties will be <a href="https://tc39.github.io/proposal-b…
36778 otherwise they will be <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struct…
36780 <a href="https://tc39.github.io/proposal-bigint">bigints</a> that hold the corresponding times in n…
36784 <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date"><co…
36791-pages/man2/mknod.2.html"><code>mknod(2)</code></a>, <a href="http://man7.org/linux/man-pages/man2…
36793-pages/man2/mknod.2.html"><code>mknod(2)</code></a>, <a href="http://man7.org/linux/man-pages/man2…
36795 … href="http://man7.org/linux/man-pages/man2/chmod.2.html"><code>chmod(2)</code></a>, <a href="http…
36796-pages/man2/link.2.html"><code>link(2)</code></a>, <a href="http://man7.org/linux/man-pages/man2/m…
36797 …href="http://man7.org/linux/man-pages/man2/read.2.html"><code>read(2)</code></a>, and <a href="htt…
36801 <code>1970-01-01T00:00Z</code> (ie, Unix epoch timestamp <code>0</code>). This value may be greater
36804 <code>birthtime</code> using the <a href="http://man7.org/linux/man-pages/man2/utimes.2.html"><code…
36827 <li><code>fd</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structure…
36847 <li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Referenc…
36849 <li><code>err</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Glo…
36869 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" …
36875 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Objec…
36884 …ass="language-js mjs"><span class="hljs-keyword">import</span> { open, constants } <span class="hl…
36886 <span class="hljs-keyword">const</span> {
36887 <span class="hljs-variable constant_">O_RDWR</span>,
36888 <span class="hljs-variable constant_">O_CREAT</span>,
36889 <span class="hljs-variable constant_">O_EXCL</span>
36892-title function_">open</span>(<span class="hljs-string">'/path/to/my/file'</span>, <span class="hl…
36893 <span class="hljs-comment">// ...</span>
36939 copy-on-write reflink. If the underlying platform does not support
36940 copy-on-write, then a fallback copy mechanism is used.</td>
36945 copy-on-write reflink. If the underlying platform does not support
36946 copy-on-write, then the operation will fail with an error.</td>
36958 <td>Flag indicating to open a file for read-only access.</td>
36962 <td>Flag indicating to open a file for write-only access.</td>
36966 <td>Flag indicating to open a file for read-write access.</td>
37062 <td>File type constant for a character-oriented device file.</td>
37066 <td>File type constant for a block-oriented device file.</td>
37139 <h4>Ordering of callback and promise-based operations<span><a class="mark" href="#fs_ordering_of_ca…
37142 promise-based methods.</p>
37145-js">fs.<span class="hljs-title function_">rename</span>(<span class="hljs-string">'/tmp/hello'</s…
37146 <span class="hljs-keyword">if</span> (err) <span class="hljs-keyword">throw</span> err;
37147 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>…
37149 …an class="hljs-title function_">stat</span>(<span class="hljs-string">'/tmp/world'</span>, <span c…
37150 <span class="hljs-keyword">if</span> (err) <span class="hljs-keyword">throw</span> err;
37151-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hlj…
37156-flavor-selector" type="checkbox" checked aria-label="Show modern ES modules syntax"><code class="…
37158 <span class="hljs-keyword">const</span> <span class="hljs-keyword">from</span> = <span class="hljs-
37159 <span class="hljs-keyword">const</span> to = <span class="hljs-string">'/tmp/world'</span>;
37161 <span class="hljs-keyword">try</span> {
37162 …<span class="hljs-keyword">await</span> <span class="hljs-title function_">rename</span>(<span cla…
37163 …<span class="hljs-keyword">const</span> stats = <span class="hljs-keyword">await</span> <span clas…
37164-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hlj…
37165 } <span class="hljs-keyword">catch</span> (error) {
37166-variable language_">console</span>.<span class="hljs-title function_">error</span>(<span class="h…
37167 …ss="language-js cjs"><span class="hljs-keyword">const</span> { rename, stat } = <span class="hljs-
37169 (<span class="hljs-keyword">async</span> <span class="hljs-keyword">function</span>(<span class="hl…
37170 <span class="hljs-keyword">try</span> {
37171 …<span class="hljs-keyword">await</span> <span class="hljs-title function_">rename</span>(<span cla…
37172 …<span class="hljs-keyword">const</span> stats = <span class="hljs-keyword">await</span> <span clas…
37173-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hlj…
37174 } <span class="hljs-keyword">catch</span> (error) {
37175-variable language_">console</span>.<span class="hljs-title function_">error</span>(<span class="h…
37177 })(<span class="hljs-string">'/tmp/hello'</span>, <span class="hljs-string">'/tmp/world'</span>);</…
37181-flavor-selector" type="checkbox" checked aria-label="Show modern ES modules syntax"><code class="…
37183-title function_">rename</span>(<span class="hljs-string">'/tmp/hello'</span>, <span class="hljs-s…
37184 <span class="hljs-keyword">if</span> (err) <span class="hljs-keyword">throw</span> err;
37185 …an class="hljs-title function_">stat</span>(<span class="hljs-string">'/tmp/world'</span>, <span c…
37186 <span class="hljs-keyword">if</span> (err) <span class="hljs-keyword">throw</span> err;
37187-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hlj…
37189 …ss="language-js cjs"><span class="hljs-keyword">const</span> { rename, stat } = <span class="hljs-
37191-title function_">rename</span>(<span class="hljs-string">'/tmp/hello'</span>, <span class="hljs-s…
37192 <span class="hljs-keyword">if</span> (err) <span class="hljs-keyword">throw</span> err;
37193 …an class="hljs-title function_">stat</span>(<span class="hljs-string">'/tmp/world'</span>, <span c…
37194 <span class="hljs-keyword">if</span> (err) <span class="hljs-keyword">throw</span> err;
37195-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hlj…
37202 <p>String form paths are interpreted as UTF-8 character sequences identifying
37206 …de class="language-js mjs"><span class="hljs-keyword">import</span> { open } <span class="hljs-key…
37208 <span class="hljs-keyword">let</span> fd;
37209 <span class="hljs-keyword">try</span> {
37210 …hljs-keyword">await</span> <span class="hljs-title function_">open</span>(<span class="hljs-string…
37211 <span class="hljs-comment">// Do something with the file</span>
37212 } <span class="hljs-keyword">finally</span> {
37213 <span class="hljs-keyword">await</span> fd.<span class="hljs-title function_">close</span>();
37216 …de class="language-js mjs"><span class="hljs-keyword">import</span> { open } <span class="hljs-key…
37218 <span class="hljs-keyword">let</span> fd;
37219 <span class="hljs-keyword">try</span> {
37220 …lass="hljs-keyword">await</span> <span class="hljs-title function_">open</span>(<span class="hljs-
37221 <span class="hljs-comment">// Do something with the file</span>
37222 } <span class="hljs-keyword">finally</span> {
37223 <span class="hljs-keyword">await</span> fd.<span class="hljs-title function_">close</span>();
37231 …lass="language-js mjs"><span class="hljs-keyword">import</span> { readFileSync } <span class="hljs
37233 …="hljs-title function_">readFileSync</span>(<span class="hljs-keyword">new</span> <span class="hlj…
37235 <h6>Platform-specific considerations<span><a class="mark" href="#fs_platform_specific_consideration…
37239 …lass="language-js mjs"><span class="hljs-keyword">import</span> { readFileSync } <span class="hljs
37240 <span class="hljs-comment">// On Windows :</span>
37242 <span class="hljs-comment">// - WHATWG file URLs with hostname convert to UNC path</span>
37243 <span class="hljs-comment">// file://hostname/p/a/t/h/file => \\hostname\p\a\t\h\file</span>
37244 …="hljs-title function_">readFileSync</span>(<span class="hljs-keyword">new</span> <span class="hlj…
37246 <span class="hljs-comment">// - WHATWG file URLs with drive letters convert to absolute path</span>
37247 <span class="hljs-comment">// file:///C:/tmp/hello => C:\tmp\hello</span>
37248 …="hljs-title function_">readFileSync</span>(<span class="hljs-keyword">new</span> <span class="hlj…
37250 <span class="hljs-comment">// - WHATWG file URLs without hostname must have a drive letters</span>
37251 …="hljs-title function_">readFileSync</span>(<span class="hljs-keyword">new</span> <span class="hlj…
37252 …="hljs-title function_">readFileSync</span>(<span class="hljs-keyword">new</span> <span class="hlj…
37253 <span class="hljs-comment">// TypeError [ERR_INVALID_FILE_URL_PATH]: File URL path must be absolute…
37258 …lass="language-js mjs"><span class="hljs-keyword">import</span> { readFileSync } <span class="hljs
37259 <span class="hljs-comment">// On other platforms:</span>
37261 <span class="hljs-comment">// - WHATWG file URLs with hostname are unsupported</span>
37262 <span class="hljs-comment">// file://hostname/p/a/t/h/file => throw!</span>
37263 …="hljs-title function_">readFileSync</span>(<span class="hljs-keyword">new</span> <span class="hlj…
37264 <span class="hljs-comment">// TypeError [ERR_INVALID_FILE_URL_PATH]: must be absolute</span>
37266 <span class="hljs-comment">// - WHATWG file URLs convert to absolute path</span>
37267 <span class="hljs-comment">// file:///tmp/hello => /tmp/hello</span>
37268 …="hljs-title function_">readFileSync</span>(<span class="hljs-keyword">new</span> <span class="hlj…
37271 …lass="language-js mjs"><span class="hljs-keyword">import</span> { readFileSync } <span class="hljs
37273 <span class="hljs-comment">// On Windows</span>
37274 …="hljs-title function_">readFileSync</span>(<span class="hljs-keyword">new</span> <span class="hlj…
37275 …="hljs-title function_">readFileSync</span>(<span class="hljs-keyword">new</span> <span class="hlj…
37276 <span class="hljs-comment">/* TypeError [ERR_INVALID_FILE_URL_PATH]: File URL path must not include…
37279 <span class="hljs-comment">// On POSIX</span>
37280 …="hljs-title function_">readFileSync</span>(<span class="hljs-keyword">new</span> <span class="hlj…
37281 …="hljs-title function_">readFileSync</span>(<span class="hljs-keyword">new</span> <span class="hlj…
37282 <span class="hljs-comment">/* TypeError [ERR_INVALID_FILE_URL_PATH]: File URL path must not include…
37285 …lass="language-js mjs"><span class="hljs-keyword">import</span> { readFileSync } <span class="hljs
37287 <span class="hljs-comment">// On Windows</span>
37288 …="hljs-title function_">readFileSync</span>(<span class="hljs-keyword">new</span> <span class="hlj…
37289 …="hljs-title function_">readFileSync</span>(<span class="hljs-keyword">new</span> <span class="hlj…
37290 <span class="hljs-comment">/* TypeError [ERR_INVALID_FILE_URL_PATH]: File URL path must not include…
37295 systems, it is possible for a single file path to contain sub-sequences that
37299 …de class="language-js mjs"><span class="hljs-keyword">import</span> { open } <span class="hljs-key…
37301 <span class="hljs-keyword">let</span> fd;
37302 <span class="hljs-keyword">try</span> {
37303-keyword">await</span> <span class="hljs-title function_">open</span>(<span class="hljs-title clas…
37304 <span class="hljs-comment">// Do something with the file</span>
37305 } <span class="hljs-keyword">finally</span> {
37306 <span class="hljs-keyword">await</span> fd.<span class="hljs-title function_">close</span>();
37308 <h5>Per-drive working directories on Windows<span><a class="mark" href="#fs_per_drive_working_direc…
37309 <p>On Windows, Node.js follows the concept of per-drive working directory. This
37313 <a href="https://docs.microsoft.com/en-us/windows/desktop/FileIO/naming-a-file#fully-qualified-vs-r…
37317 identifier called a <em>file descriptor</em>. At the system-level, all file system
37323 <p>The callback-based <code>fs.open()</code>, and synchronous <code>fs.openSync()</code> methods op…
37330 …s="language-js mjs"><span class="hljs-keyword">import</span> { open, close, fstat } <span class="h…
37332 <span class="hljs-keyword">function</span> <span class="hljs-title function_">closeFd</span>(<span …
37333 …<span class="hljs-title function_">close</span>(fd, <span class="hljs-function">(<span class="hljs
37334 <span class="hljs-keyword">if</span> (err) <span class="hljs-keyword">throw</span> err;
37338-title function_">open</span>(<span class="hljs-string">'/open/some/file.txt'</span>, <span class=…
37339 <span class="hljs-keyword">if</span> (err) <span class="hljs-keyword">throw</span> err;
37340 <span class="hljs-keyword">try</span> {
37341 …<span class="hljs-title function_">fstat</span>(fd, <span class="hljs-function">(<span class="hljs
37342 <span class="hljs-keyword">if</span> (err) {
37343 <span class="hljs-title function_">closeFd</span>(fd);
37344 <span class="hljs-keyword">throw</span> err;
37347 <span class="hljs-comment">// use stat</span>
37349 <span class="hljs-title function_">closeFd</span>(fd);
37351 } <span class="hljs-keyword">catch</span> (err) {
37352 <span class="hljs-title function_">closeFd</span>(fd);
37353 <span class="hljs-keyword">throw</span> err;
37356 <p>The promise-based APIs use a <a href="#fs_class_filehandle" class="type">&#x3C;FileHandle></a> o…
37360 …de class="language-js mjs"><span class="hljs-keyword">import</span> { open } <span class="hljs-key…
37362 <span class="hljs-keyword">let</span> file;
37363 <span class="hljs-keyword">try</span> {
37364 …hljs-keyword">await</span> <span class="hljs-title function_">open</span>(<span class="hljs-string…
37365 …<span class="hljs-keyword">const</span> stat = <span class="hljs-keyword">await</span> file.<span …
37366 <span class="hljs-comment">// use stat</span>
37367 } <span class="hljs-keyword">finally</span> {
37368 <span class="hljs-keyword">await</span> file.<span class="hljs-title function_">close</span>();
37371 <p>All callback and promise-based file system APIs ( with the exception of
37434 <p><code>flag</code> can also be a number as documented by <a href="http://man7.org/linux/man-pages…
37438 …e>'x'</code> (<code>O_EXCL</code> flag in <a href="http://man7.org/linux/man-pages/man2/open.2.htm…
37447 <p>The behavior of some flags are platform-specific. As such, opening a directory
37451 <pre><code class="language-js"><span class="hljs-comment">// macOS and Linux</span>
37452-title function_">open</span>(<span class="hljs-string">'&#x3C;directory>'</span>, <span class="hl…
37453 …<span class="hljs-comment">// => [Error: EISDIR: illegal operation on a directory, open &#x3C;dire…
37456 <span class="hljs-comment">// Windows and FreeBSD</span>
37457-title function_">open</span>(<span class="hljs-string">'&#x3C;directory>'</span>, <span class="hl…
37458 <span class="hljs-comment">// => null, &#x3C;fd></span>
37479 …re are <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects">…
37486 … api_stability_1"><a href="#documentation_stability_index">Stability: 1</a> - Experimental</div><p…
37488 <p>A utility class used to signal cancelation in selected <code>Promise</code>-based APIs.
37489 The API is based on the Web API <a href="https://developer.mozilla.org/en-US/docs/Web/API/AbortCont…
37490 <p>To use, launch Node.js using the <code>--experimental-abortcontroller</code> flag.</p>
37491 …e><code class="language-js"><span class="hljs-keyword">const</span> ac = <span class="hljs-keyword…
37493-property">signal</span>.<span class="hljs-title function_">addEventListener</span>(<span class="h…
37494 … { <span class="hljs-attr">once</span>: <span class="hljs-literal">true</span> });
37496 ac.<span class="hljs-title function_">abort</span>();
37498-variable language_">console</span>.<span class="hljs-title function_">log</span>(ac.<span class="…
37536 …e><code class="language-js"><span class="hljs-keyword">const</span> ac = <span class="hljs-keyword…
37538 <span class="hljs-comment">// Use either the onabort property...</span>
37539-property">signal</span>.<span class="hljs-property">onabort</span> = <span class="hljs-function">…
37541 <span class="hljs-comment">// Or the EventTarget API...</span>
37542-property">signal</span>.<span class="hljs-title function_">addEventListener</span>(<span class="h…
37543 …js-variable language_">console</span>.<span class="hljs-title function_">log</span>(event.<span cl…
37544 }, { <span class="hljs-attr">once</span>: <span class="hljs-literal">true</span> });
37546 ac.<span class="hljs-title function_">abort</span>();</code></pre>
37561 <li>Type: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_…
37568 <li>Type: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects…
37578 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Funct…
37609 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Objec…
37620 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Objec…
37622 <p>In browsers, the top-level scope is the global scope. This means that
37624 Node.js this is different. The top-level scope is not the global scope;
37634 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Objec…
37643 <li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Referenc…
37652 <pre><code class="language-js"><span class="hljs-comment">// Here, `queueMicrotask()` is used to en…
37653 <span class="hljs-comment">// emitted asynchronously, and therefore consistently. Using</span>
37654 <span class="hljs-comment">// `process.nextTick()` here would result in the 'load' event always emi…
37655 <span class="hljs-comment">// before any other promise jobs.</span>
37657-title class_">DataHandler</span>.<span class="hljs-property"><span class="hljs-keyword">prototype…
37658 …s="hljs-keyword">const</span> hit = <span class="hljs-variable language_">this</span>.<span class=…
37659 <span class="hljs-keyword">if</span> (hit !== <span class="hljs-literal">undefined</span>) {
37660 …<span class="hljs-title function_">queueMicrotask</span>(<span class="hljs-function">() =></span> {
37661 …<span class="hljs-variable language_">this</span>.<span class="hljs-title function_">emit</span>(<…
37663 <span class="hljs-keyword">return</span>;
37666 …<span class="hljs-keyword">const</span> data = <span class="hljs-keyword">await</span> <span class…
37667 …<span class="hljs-variable language_">this</span>.<span class="hljs-property">_cache</span>.<span …
37668 …<span class="hljs-variable language_">this</span>.<span class="hljs-title function_">emit</span>(<…
37720 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Objec…
37724 <a href="https://developer.mozilla.org/en-US/docs/WebAssembly">Mozilla Developer Network</a> for us…
37728 …ty api_stability_2"><a href="#documentation_stability_index">Stability: 2</a> - Stable</div><p></p>
37733 In particular, large, possibly chunk-encoded, messages. The interface is
37737 <!-- eslint-skip -->
37738 <pre><code class="language-js">{ <span class="hljs-string">'content-length'</span>: <span class="hl…
37739 <span class="hljs-string">'content-type'</span>: <span class="hljs-string">'text/plain'</span>,
37740 <span class="hljs-string">'connection'</span>: <span class="hljs-string">'keep-alive'</span>,
37741 <span class="hljs-string">'host'</span>: <span class="hljs-string">'mysite.com'</span>,
37742 <span class="hljs-string">'accept'</span>: <span class="hljs-string">'*/*'</span> }</code></pre>
37745 HTTP API is very low-level. It deals with stream handling and message
37753 <!-- eslint-disable semi -->
37754 <pre><code class="language-js">[ <span class="hljs-string">'ConTent-Length'</span>, <span class="hl…
37755 <span class="hljs-string">'content-LENGTH'</span>, <span class="hljs-string">'123'</span>,
37756 <span class="hljs-string">'content-type'</span>, <span class="hljs-string">'text/plain'</span>,
37757 <span class="hljs-string">'CONNECTION'</span>, <span class="hljs-string">'keep-alive'</span>,
37758 <span class="hljs-string">'Host'</span>, <span class="hljs-string">'mysite.com'</span>,
37759 <span class="hljs-string">'accepT'</span>, <span class="hljs-string">'*/*'</span> ]</code></pre>
37771 <p>Pooled connections have TCP Keep-Alive enabled for them, but servers may
37788 …code class="language-js">http.<span class="hljs-title function_">get</span>(options, <span class="…
37789 <span class="hljs-comment">// Do stuff</span>
37790 …<span class="hljs-title function_">on</span>(<span class="hljs-string">'socket'</span>, <span clas…
37791 …socket.<span class="hljs-title function_">emit</span>(<span class="hljs-string">'agentRemove'</spa…
37795 functions, a one-time use <code>Agent</code> with default options will be used
37798 <pre><code class="language-js">http.<span class="hljs-title function_">get</span>({
37799 <span class="hljs-attr">hostname</span>: <span class="hljs-string">'localhost'</span>,
37800 <span class="hljs-attr">port</span>: <span class="hljs-number">80</span>,
37801 <span class="hljs-attr">path</span>: <span class="hljs-string">'/'</span>,
37802 …<span class="hljs-attr">agent</span>: <span class="hljs-literal">false</span> <span class="hljs-c…
37803 }, <span class="hljs-function">(<span class="hljs-params">res</span>) =></span> {
37804 <span class="hljs-comment">// Do stuff with response</span>
37823 <li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference…
37826 <li><code>keepAlive</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_st…
37829 <code>keep-alive</code> value of the <code>Connection</code> header. The <code>Connection: keep-ali…
37834 <li><code>keepAliveMsecs</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Da…
37836 for TCP Keep-Alive packets. Ignored when the
37838 <li><code>maxSockets</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_s…
37847 <li><code>maxTotalSockets</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/D…
37851 <li><code>maxFreeSockets</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Da…
37854 <li><code>scheduling</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_s…
37866 <li><code>timeout</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_stru…
37875 …code class="language-js"><span class="hljs-keyword">const</span> http = <span class="hljs-built_in…
37876-keyword">const</span> keepAliveAgent = <span class="hljs-keyword">new</span> http.<span class="hl…
37877 options.<span class="hljs-property">agent</span> = keepAliveAgent;
37878 http.<span class="hljs-title function_">request</span>(options, onResponseCallback);</code></pre>
37884 <li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference…
37886 <li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Referenc…
37907-js">socket.<span class="hljs-title function_">setKeepAlive</span>(<span class="hljs-literal">true…
37908 socket.<span class="hljs-title function_">unref</span>();
37909 <span class="hljs-keyword">return</span> <span class="hljs-literal">true</span>;</code></pre>
37924 the keep-alive options. Default behavior is to:</p>
37925 <pre><code class="language-js">socket.<span class="hljs-title function_">ref</span>();</code></pre>
37944 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Objec…
37955 <li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference…
37957 <li><code>host</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
37959 <li><code>port</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
37960 <li><code>localAddress</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data…
37962 <li><code>family</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struc…
37965 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Strin…
37970 the name includes the CA, cert, ciphers, and other HTTPS/TLS-specific options
37977 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" c…
37987 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" c…
37996 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" c…
38005 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Objec…
38014 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Objec…
38026 represents an <em>in-progress</em> request whose header has already been queued. The
38047 <p>Node.js does not check whether Content-Length and the length of the
38071 …code class="language-js"><span class="hljs-keyword">const</span> http = <span class="hljs-built_in…
38072 <span class="hljs-keyword">const</span> net = <span class="hljs-built_in">require</span>(<span clas…
38073 …s="hljs-keyword">const</span> { <span class="hljs-variable constant_">URL</span> } = <span class="…
38075 <span class="hljs-comment">// Create an HTTP tunneling proxy</span>
38076 …s="hljs-keyword">const</span> proxy = http.<span class="hljs-title function_">createServer</span>(…
38077 …"hljs-title function_">writeHead</span>(<span class="hljs-number">200</span>, { <span class="hljs-
38078 res.<span class="hljs-title function_">end</span>(<span class="hljs-string">'okay'</span>);
38080 …<span class="hljs-title function_">on</span>(<span class="hljs-string">'connect'</span>, <span cla…
38081 <span class="hljs-comment">// Connect to an origin server</span>
38082-keyword">const</span> { port, hostname } = <span class="hljs-keyword">new</span> <span class="hlj…
38083-keyword">const</span> serverSocket = net.<span class="hljs-title function_">connect</span>(port |…
38084 …clientSocket.<span class="hljs-title function_">write</span>(<span class="hljs-string">'HTTP/1.1 2…
38085 <span class="hljs-string">'Proxy-agent: Node.js-Proxy\r\n'</span> +
38086 <span class="hljs-string">'\r\n'</span>);
38087 serverSocket.<span class="hljs-title function_">write</span>(head);
38088 serverSocket.<span class="hljs-title function_">pipe</span>(clientSocket);
38089 clientSocket.<span class="hljs-title function_">pipe</span>(serverSocket);
38093 <span class="hljs-comment">// Now that proxy is running</span>
38094 …ass="hljs-title function_">listen</span>(<span class="hljs-number">1337</span>, <span class="hljs-
38096 <span class="hljs-comment">// Make a request to a tunneling proxy</span>
38097 <span class="hljs-keyword">const</span> options = {
38098 <span class="hljs-attr">port</span>: <span class="hljs-number">1337</span>,
38099 <span class="hljs-attr">host</span>: <span class="hljs-string">'127.0.0.1'</span>,
38100 <span class="hljs-attr">method</span>: <span class="hljs-string">'CONNECT'</span>,
38101 <span class="hljs-attr">path</span>: <span class="hljs-string">'www.google.com:80'</span>
38104 …<span class="hljs-keyword">const</span> req = http.<span class="hljs-title function_">request</spa…
38105 req.<span class="hljs-title function_">end</span>();
38107 …<span class="hljs-title function_">on</span>(<span class="hljs-string">'connect'</span>, <span cla…
38108 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>…
38110 <span class="hljs-comment">// Make a request over an HTTP tunnel</span>
38111 …socket.<span class="hljs-title function_">write</span>(<span class="hljs-string">'GET / HTTP/1.1\r…
38112 <span class="hljs-string">'Host: www.google.com:80\r\n'</span> +
38113 <span class="hljs-string">'Connection: close\r\n'</span> +
38114 <span class="hljs-string">'\r\n'</span>);
38115 ….<span class="hljs-title function_">on</span>(<span class="hljs-string">'data'</span>, <span class…
38116 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>…
38118 …socket.<span class="hljs-title function_">on</span>(<span class="hljs-string">'end'</span>, <span …
38119 proxy.<span class="hljs-title function_">close</span>();
38128 the request contained 'Expect: 100-continue'. This is an instruction that
38135 <li><code>info</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Gl…
38137 <li><code>httpVersion</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_…
38138 <li><code>httpVersionMajor</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/…
38139 <li><code>httpVersionMinor</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/…
38140 <li><code>statusCode</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_s…
38141 <li><code>statusMessage</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Dat…
38142 <li><code>headers</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference…
38143 <li><code>rawHeaders</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_s…
38149 HTTP version, status code, status message, key-value headers object,
38151 …code class="language-js"><span class="hljs-keyword">const</span> http = <span class="hljs-built_in…
38153 <span class="hljs-keyword">const</span> options = {
38154 <span class="hljs-attr">host</span>: <span class="hljs-string">'127.0.0.1'</span>,
38155 <span class="hljs-attr">port</span>: <span class="hljs-number">8080</span>,
38156 <span class="hljs-attr">path</span>: <span class="hljs-string">'/length_request'</span>
38159 <span class="hljs-comment">// Make a request</span>
38160 <span class="hljs-keyword">const</span> req = http.<span class="hljs-title function_">request</span…
38161 req.<span class="hljs-title function_">end</span>();
38163 …pan class="hljs-title function_">on</span>(<span class="hljs-string">'information'</span>, <span c…
38164-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hlj…
38167 traditional HTTP request/response chain, such as web sockets, in-place TLS
38213 …code class="language-js"><span class="hljs-keyword">const</span> http = <span class="hljs-built_in…
38215 <span class="hljs-comment">// Create an HTTP server</span>
38216 …s="hljs-keyword">const</span> server = http.<span class="hljs-title function_">createServer</span>…
38217 …"hljs-title function_">writeHead</span>(<span class="hljs-number">200</span>, { <span class="hljs-
38218 res.<span class="hljs-title function_">end</span>(<span class="hljs-string">'okay'</span>);
38220 …<span class="hljs-title function_">on</span>(<span class="hljs-string">'upgrade'</span>, <span cla…
38221 …socket.<span class="hljs-title function_">write</span>(<span class="hljs-string">'HTTP/1.1 101 Web…
38222 <span class="hljs-string">'Upgrade: WebSocket\r\n'</span> +
38223 <span class="hljs-string">'Connection: Upgrade\r\n'</span> +
38224 <span class="hljs-string">'\r\n'</span>);
38226 …socket.<span class="hljs-title function_">pipe</span>(socket); <span class="hljs-comment">// echo …
38229 <span class="hljs-comment">// Now that server is running</span>
38230 …ass="hljs-title function_">listen</span>(<span class="hljs-number">1337</span>, <span class="hljs-
38232 <span class="hljs-comment">// make a request</span>
38233 <span class="hljs-keyword">const</span> options = {
38234 <span class="hljs-attr">port</span>: <span class="hljs-number">1337</span>,
38235 <span class="hljs-attr">host</span>: <span class="hljs-string">'127.0.0.1'</span>,
38236 <span class="hljs-attr">headers</span>: {
38237 <span class="hljs-string">'Connection'</span>: <span class="hljs-string">'Upgrade'</span>,
38238 <span class="hljs-string">'Upgrade'</span>: <span class="hljs-string">'websocket'</span>
38242 …<span class="hljs-keyword">const</span> req = http.<span class="hljs-title function_">request</spa…
38243 req.<span class="hljs-title function_">end</span>();
38245 …<span class="hljs-title function_">on</span>(<span class="hljs-string">'upgrade'</span>, <span cla…
38246 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>…
38247 socket.<span class="hljs-title function_">end</span>();
38248 process.<span class="hljs-title function_">exit</span>(<span class="hljs-number">0</span>);
38255 … api_stability_0"><a href="#documentation_stability_index">Stability: 0</a> - Deprecated: Use <a h…
38271 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" …
38279 … api_stability_0"><a href="#documentation_stability_index">Stability: 0</a> - Deprecated. Use <a h…
38297 <li><code>data</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
38298 <li><code>encoding</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_str…
38299 <li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Referenc…
38300 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/t…
38322 <li><code>error</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/G…
38323 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/t…
38334 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" …
38342 … api_stability_0"><a href="#documentation_stability_index">Stability: 0</a> - Deprecated. Use <a h…
38344 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" …
38357 <p>That's usually desired (it saves a TCP round-trip), but not when the first
38365 <li><code>name</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
38366 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Data_…
38368 <p>Reads out a header on the request. The name is case-insensitive.
38371 …="language-js">request.<span class="hljs-title function_">setHeader</span>(<span class="hljs-strin…
38372 …s-title function_">setHeader</span>(<span class="hljs-string">'Content-Length'</span>, <span class…
38373 …hljs-title function_">setHeader</span>(<span class="hljs-string">'Cookie'</span>, [<span class="hl…
38374 …lass="hljs-keyword">const</span> contentType = request.<span class="hljs-title function_">getHeade…
38375 <span class="hljs-comment">// 'contentType' is 'text/html'</span>
38376 …ass="hljs-keyword">const</span> contentLength = request.<span class="hljs-title function_">getHead…
38377 <span class="hljs-comment">// 'contentLength' is of type number</span>
38378 <span class="hljs-keyword">const</span> cookie = request.<span class="hljs-title function_">getHead…
38379 <span class="hljs-comment">// 'cookie' is of type string[]</span></code></pre>
38385 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Strin…
38389 …class="language-js">request.<span class="hljs-title function_">setHeader</span>(<span class="hljs-
38390 …hljs-title function_">setHeader</span>(<span class="hljs-string">'Set-Cookie'</span>, [<span class…
38392 <span class="hljs-keyword">const</span> headerNames = request.<span class="hljs-title function_">ge…
38393 <span class="hljs-comment">// headerNames === ['Foo', 'Set-Cookie']</span></code></pre>
38396 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" c…
38404 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" c…
38411 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" c…
38418 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" c…
38425 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" c…
38432 <li><code>name</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
38435 …pre><code class="language-js">request.<span class="hljs-title function_">removeHeader</span>(<span…
38441 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" …
38443 <p>When sending request through a keep-alive enabled agent, the underlying socket
38446 …code class="language-js"><span class="hljs-keyword">const</span> http = <span class="hljs-built_in…
38448 <span class="hljs-comment">// Server has a 5 seconds keep-alive timeout by default</span>
38450 ….<span class="hljs-title function_">createServer</span>(<span class="hljs-function">(<span class="…
38451 res.<span class="hljs-title function_">write</span>(<span class="hljs-string">'hello\n'</span>);
38452 res.<span class="hljs-title function_">end</span>();
38454 .<span class="hljs-title function_">listen</span>(<span class="hljs-number">3000</span>);
38456 <span class="hljs-built_in">setInterval</span>(<span class="hljs-function">() =></span> {
38457 <span class="hljs-comment">// Adapting a keep-alive agent</span>
38458 …hljs-title function_">get</span>(<span class="hljs-string">'http://localhost:3000'</span>, { agent…
38459 ….<span class="hljs-title function_">on</span>(<span class="hljs-string">'data'</span>, <span class…
38460 <span class="hljs-comment">// Do nothing</span>
38463 }, <span class="hljs-number">5000</span>); <span class="hljs-comment">// Sending request on 5s inte…
38466 …code class="language-js"><span class="hljs-keyword">const</span> http = <span class="hljs-built_in…
38467-keyword">const</span> agent = <span class="hljs-keyword">new</span> http.<span class="hljs-title …
38469 <span class="hljs-keyword">function</span> <span class="hljs-title function_">retriableRequest</spa…
38470 <span class="hljs-keyword">const</span> req = http
38471 …hljs-title function_">get</span>(<span class="hljs-string">'http://localhost:3000'</span>, { agent…
38472 <span class="hljs-comment">// ...</span>
38474 ….<span class="hljs-title function_">on</span>(<span class="hljs-string">'error'</span>, <span clas…
38475 <span class="hljs-comment">// Check if retry is needed</span>
38476-keyword">if</span> (req.<span class="hljs-property">reusedSocket</span> &#x26;&#x26; err.<span cl…
38477 <span class="hljs-title function_">retriableRequest</span>();
38482 <span class="hljs-title function_">retriableRequest</span>();</code></pre>
38488 <li><code>name</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
38489 <li><code>value</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struct…
38492 the to-be-sent headers, its value will be replaced. Use an array of strings
38493 here to send multiple headers with the same name. Non-string values will be
38495 non-string values. However, the non-string values will be converted to strings
38497 …="language-js">request.<span class="hljs-title function_">setHeader</span>(<span class="hljs-strin…
38499-js">request.<span class="hljs-title function_">setHeader</span>(<span class="hljs-string">'Cookie…
38505 <li><code>noDelay</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_stru…
38514 <li><code>enable</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struc…
38515 <li><code>initialDelay</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data…
38532 <li><code>timeout</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_stru…
38533 <li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Referenc…
38549 …code class="language-js"><span class="hljs-keyword">const</span> http = <span class="hljs-built_in…
38550 <span class="hljs-keyword">const</span> options = {
38551 <span class="hljs-attr">host</span>: <span class="hljs-string">'www.google.com'</span>,
38553 <span class="hljs-keyword">const</span> req = http.<span class="hljs-title function_">get</span>(op…
38554 req.<span class="hljs-title function_">end</span>();
38555 …pan class="hljs-title function_">once</span>(<span class="hljs-string">'response'</span>, <span cl…
38556 …<span class="hljs-keyword">const</span> ip = req.<span class="hljs-property">socket</span>.<span c…
38557 …<span class="hljs-keyword">const</span> port = req.<span class="hljs-property">socket</span>.<span…
38558-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hlj…
38559 <span class="hljs-comment">// Consume response object</span>
38569 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" …
38579 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" …
38588 <li><code>chunk</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struct…
38589 <li><code>encoding</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_str…
38590 <li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Referenc…
38591 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boole…
38594 <code>Content-Length</code> is set, data will automatically be encoded in HTTP Chunked
38596 <code>Transfer-Encoding: chunked</code> header is added. Calling <a href="#http_request_end_data_en…
38601 is flushed, but only if the chunk is non-empty.</p>
38622 <p>Emitted each time a request with an HTTP <code>Expect: 100-continue</code> is received.
38640 value is not <code>100-continue</code>. If this event is not listened for, the server will
38661 <li><code>exception</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Referen…
38676 …code class="language-js"><span class="hljs-keyword">const</span> http = <span class="hljs-built_in…
38678 …s="hljs-keyword">const</span> server = http.<span class="hljs-title function_">createServer</span>…
38679 res.<span class="hljs-title function_">end</span>();
38681 …pan class="hljs-title function_">on</span>(<span class="hljs-string">'clientError'</span>, <span c…
38682 …socket.<span class="hljs-title function_">end</span>(<span class="hljs-string">'HTTP/1.1 400 Bad R…
38684 server.<span class="hljs-title function_">listen</span>(<span class="hljs-number">8000</span>);</co…
38699-js">server.<span class="hljs-title function_">on</span>(<span class="hljs-string">'clientError'</…
38700 …hljs-keyword">if</span> (err.<span class="hljs-property">code</span> === <span class="hljs-string"…
38701 <span class="hljs-keyword">return</span>;
38704 …socket.<span class="hljs-title function_">end</span>(<span class="hljs-string">'HTTP/1.1 400 Bad R…
38746 <code>server.keepAliveTimeout</code> is non-zero).</p>
38759 per connection (in the case of HTTP Keep-Alive connections).</p>
38791 <li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Referenc…
38799 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" c…
38820 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" …
38827 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" c…
38835 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" c…
38841 <p>It must be set to a non-zero value (e.g. 120 seconds) to protect against
38842 potential Denial-of-Service attacks in case the server is deployed without a
38857 <li><code>msecs</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struct…
38858 <li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Referenc…
38865 will be called with the timed-out socket as an argument.</p>
38882 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" c…
38894 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" c…
38898 will be destroyed. If the server receives new data before the keep-alive
38901 <p>A value of <code>0</code> will disable the keep-alive timeout behavior on incoming
38904 to 8.0.0, which did not have a keep-alive timeout.</p>
38935 <li><code>headers</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference…
38944-js">response.<span class="hljs-title function_">writeHead</span>(<span class="hljs-number">200</s…
38945 … <span class="hljs-string">'Trailer'</span>: <span class="hljs-string">'Content-MD5'</span> });
38946 response.<span class="hljs-title function_">write</span>(fileData);
38947 …sponse.<span class="hljs-title function_">addTrailers</span>({ <span class="hljs-string">'Content-
38948 response.<span class="hljs-title function_">end</span>();</code></pre>
38955 … api_stability_0"><a href="#documentation_stability_index">Stability: 0</a> - Deprecated. Use <a h…
38978 <li><code>data</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
38979 <li><code>encoding</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_str…
38980 <li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Referenc…
38981 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/t…
38994 … api_stability_0"><a href="#documentation_stability_index">Stability: 0</a> - Deprecated. Use <a h…
38996 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" …
39010 <li><code>name</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
39011 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Data_…
39014 The name is case-insensitive. The type of the return value depends
39016 …="language-js">response.<span class="hljs-title function_">setHeader</span>(<span class="hljs-stri…
39017 …s-title function_">setHeader</span>(<span class="hljs-string">'Content-Length'</span>, <span class…
39018 …js-title function_">setHeader</span>(<span class="hljs-string">'Set-Cookie'</span>, [<span class="…
39019 …lass="hljs-keyword">const</span> contentType = response.<span class="hljs-title function_">getHead…
39020 <span class="hljs-comment">// contentType is 'text/html'</span>
39021 …ass="hljs-keyword">const</span> contentLength = response.<span class="hljs-title function_">getHea…
39022 <span class="hljs-comment">// contentLength is of type number</span>
39023 …n class="hljs-keyword">const</span> setCookie = response.<span class="hljs-title function_">getHea…
39024 <span class="hljs-comment">// setCookie is of type string[]</span></code></pre>
39030 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Strin…
39034 …lass="language-js">response.<span class="hljs-title function_">setHeader</span>(<span class="hljs-
39035 …hljs-title function_">setHeader</span>(<span class="hljs-string">'Set-Cookie'</span>, [<span class…
39037 <span class="hljs-keyword">const</span> headerNames = response.<span class="hljs-title function_">g…
39038 <span class="hljs-comment">// headerNames === ['foo', 'set-cookie']</span></code></pre>
39044 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Obje…
39048 header-related http module methods. The keys of the returned object are the
39055 …lass="language-js">response.<span class="hljs-title function_">setHeader</span>(<span class="hljs-
39056 …hljs-title function_">setHeader</span>(<span class="hljs-string">'Set-Cookie'</span>, [<span class…
39058 <span class="hljs-keyword">const</span> headers = response.<span class="hljs-title function_">getHe…
39059 <span class="hljs-comment">// headers === { foo: 'bar', 'set-cookie': ['foo=bar', 'bar=baz'] }</spa…
39065 <li><code>name</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
39066 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boole…
39069 outgoing headers. The header name matching is case-insensitive.</p>
39070-js"><span class="hljs-keyword">const</span> hasContentType = response.<span class="hljs-title fun…
39076 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" …
39078 <p>Boolean (read-only). True if headers were sent, false otherwise.</p>
39084 <li><code>name</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
39087 …re><code class="language-js">response.<span class="hljs-title function_">removeHeader</span>(<span…
39093 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" …
39104 <li><code>name</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
39105 <li><code>value</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struct…
39110 in the to-be-sent headers, its value will be replaced. Use an array of strings
39111 here to send multiple headers with the same name. Non-string values will be
39113 non-string values. However, the non-string values will be converted to strings
39116 …="language-js">response.<span class="hljs-title function_">setHeader</span>(<span class="hljs-stri…
39118-js">response.<span class="hljs-title function_">setHeader</span>(<span class="hljs-string">'Set-C…
39124 <pre><code class="language-js"><span class="hljs-comment">// Returns content-type = text/plain</spa…
39125 …s="hljs-keyword">const</span> server = http.<span class="hljs-title function_">createServer</span>…
39126 …res.<span class="hljs-title function_">setHeader</span>(<span class="hljs-string">'Content-Type'</…
39127 …res.<span class="hljs-title function_">setHeader</span>(<span class="hljs-string">'X-Foo'</span>, …
39128 …"hljs-title function_">writeHead</span>(<span class="hljs-number">200</span>, { <span class="hljs-
39129 res.<span class="hljs-title function_">end</span>(<span class="hljs-string">'ok'</span>);
39142 <li><code>msecs</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struct…
39143 <li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Referenc…
39164 …code class="language-js"><span class="hljs-keyword">const</span> http = <span class="hljs-built_in…
39165 …s="hljs-keyword">const</span> server = http.<span class="hljs-title function_">createServer</span>…
39166 …<span class="hljs-keyword">const</span> ip = res.<span class="hljs-property">socket</span>.<span c…
39167 …<span class="hljs-keyword">const</span> port = res.<span class="hljs-property">socket</span>.<span…
39168-title function_">end</span>(<span class="hljs-string">`Your IP address is <span class="hljs-subst…
39169 }).<span class="hljs-title function_">listen</span>(<span class="hljs-number">3000</span>);</code><…
39178 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" c…
39183 <pre><code class="language-js">response.<span class="hljs-property">statusCode</span> = <span class…
39191 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" c…
39197 <pre><code class="language-js">response.<span class="hljs-property">statusMessage</span> = <span cl…
39210 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" …
39220 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" …
39229 <li><code>chunk</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struct…
39230 <li><code>encoding</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_str…
39231 <li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Referenc…
39232 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boole…
39244 <p>This is the raw HTTP body and has nothing to do with higher-level multi-part
39278 <li><code>statusCode</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_s…
39279 <li><code>statusMessage</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Dat…
39280 …lla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type">&#x3C;Object></a> …
39283 <p>Sends a response header to the request. The status code is a 3-digit HTTP
39285 Optionally one can give a human-readable <code>statusMessage</code> as the second
39288 It is <em>not</em> a list of tuples. So, the even-numbered offsets are key values,
39289 and the odd-numbered offsets are the associated values. The array is in the same
39292 <pre><code class="language-js"><span class="hljs-keyword">const</span> body = <span class="hljs-str…
39294 .<span class="hljs-title function_">writeHead</span>(<span class="hljs-number">200</span>, {
39295 …<span class="hljs-string">'Content-Length'</span>: <span class="hljs-title class_">Buffer</span>.<…
39296 <span class="hljs-string">'Content-Type'</span>: <span class="hljs-string">'text/plain'</span>
39298 .<span class="hljs-title function_">end</span>(body);</code></pre>
39312 <pre><code class="language-js"><span class="hljs-comment">// Returns content-type = text/plain</spa…
39313 …s="hljs-keyword">const</span> server = http.<span class="hljs-title function_">createServer</span>…
39314 …res.<span class="hljs-title function_">setHeader</span>(<span class="hljs-string">'Content-Type'</…
39315 …res.<span class="hljs-title function_">setHeader</span>(<span class="hljs-string">'X-Foo'</span>, …
39316 …"hljs-title function_">writeHead</span>(<span class="hljs-number">200</span>, { <span class="hljs-
39317 res.<span class="hljs-title function_">end</span>(<span class="hljs-string">'ok'</span>);
39319 <p><code>Content-Length</code> is given in bytes, not characters. Use
39321 does not check whether <code>Content-Length</code> and the length of the body which has
39365 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" …
39374 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" …
39380 <pre><code class="language-js"><span class="hljs-keyword">const</span> req = http.<span class="hljs
39381 <span class="hljs-attr">host</span>: <span class="hljs-string">'127.0.0.1'</span>,
39382 <span class="hljs-attr">port</span>: <span class="hljs-number">8080</span>,
39383 <span class="hljs-attr">method</span>: <span class="hljs-string">'POST'</span>
39384 }, <span class="hljs-function">(<span class="hljs-params">res</span>) =></span> {
39385 res.<span class="hljs-title function_">resume</span>();
39386 …res.<span class="hljs-title function_">on</span>(<span class="hljs-string">'end'</span>, <span cla…
39387 <span class="hljs-keyword">if</span> (!res.<span class="hljs-property">complete</span>)
39388 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">error</spa…
39389 …<span class="hljs-string">'The connection was terminated while the message was still being sent'</…
39405 <li><code>error</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/G…
39406 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/t…
39416 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Objec…
39419 <p>Key-value pairs of header names and values. Header names are lower-cased.</p>
39420 <pre><code class="language-js"><span class="hljs-comment">// Prints something like:</span>
39421 <span class="hljs-comment">//</span>
39422 <span class="hljs-comment">// { 'user-agent': 'curl/7.22.0',</span>
39423 <span class="hljs-comment">// host: '127.0.0.1:8000',</span>
39424 <span class="hljs-comment">// accept: '*/*' }</span>
39425 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(…
39429 …f <code>age</code>, <code>authorization</code>, <code>content-length</code>, <code>content-type</c…
39430 …/code>, <code>from</code>, <code>host</code>, <code>if-modified-since</code>, <code>if-unmodified-
39431 <code>last-modified</code>, <code>location</code>, <code>max-forwards</code>, <code>proxy-authoriza…
39432 <code>retry-after</code>, <code>server</code>, or <code>user-agent</code> are discarded.</li>
39433 <li><code>set-cookie</code> is always an array. Duplicates are added to the array.</li>
39442 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" c…
39445 client response, the HTTP version of the connected-to server.
39454 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" c…
39463 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" c…
39467 list of tuples. So, the even-numbered offsets are key values, and the
39468 odd-numbered offsets are the associated values.</p>
39470 <pre><code class="language-js"><span class="hljs-comment">// Prints something like:</span>
39471 <span class="hljs-comment">//</span>
39472 <span class="hljs-comment">// [ 'user-agent',</span>
39473 <span class="hljs-comment">// 'this is invalid because there can be only one',</span>
39474 <span class="hljs-comment">// 'User-Agent',</span>
39475 <span class="hljs-comment">// 'curl/7.22.0',</span>
39476 <span class="hljs-comment">// 'Host',</span>
39477 <span class="hljs-comment">// '127.0.0.1:8000',</span>
39478 <span class="hljs-comment">// 'ACCEPT',</span>
39479 <span class="hljs-comment">// '*/*' ]</span>
39480 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(…
39486 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" c…
39495 <li><code>msecs</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struct…
39496 <li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Referenc…
39518 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" c…
39521 <p>The 3-digit HTTP response status code. E.G. <code>404</code>.</p>
39527 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" c…
39536 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Objec…
39544 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" c…
39549 …ode class="language-http"><span class="hljs-keyword">GET</span> <span class="hljs-string">/status?…
39550 <span class="hljs-attribute">Accept</span><span class="hljs-punctuation">: </span>text/plain</code>…
39552-js"><span class="hljs-keyword">new</span> <span class="hljs-title function_">URL</span>(request.<…
39555 <pre><code class="language-console"><span class="hljs-meta">$ </span><span class="language-bash">no…
39556 <span class="hljs-meta">> </span><span class="language-bash">new URL(request.url, `http://<span cla…
39603 <li><code>headers</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference…
39610-js">message.<span class="hljs-title function_">writeHead</span>(<span class="hljs-number">200</sp…
39611 … <span class="hljs-string">'Trailer'</span>: <span class="hljs-string">'Content-MD5'</span> });
39612 message.<span class="hljs-title function_">write</span>(fileData);
39613 …essage.<span class="hljs-title function_">addTrailers</span>({ <span class="hljs-string">'Content-
39614 message.<span class="hljs-title function_">end</span>();</code></pre>
39621 … api_stability_0"><a href="#documentation_stability_index">Stability: 0</a> - Deprecated: Use <a h…
39633 <li><code>error</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/G…
39634 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/t…
39651 <li><code>chunk</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struct…
39652 …illa.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> Opt…
39653 <li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Referenc…
39654 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/t…
39673 <p>It is usually desired (it saves a TCP round-trip), but not when the first
39681 <li><code>name</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
39682 …zilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | …
39691 <li>Returns <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String…
39700 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Obje…
39704 various header-related HTTP module methods. The keys of the returned
39711 …s="language-js">outgoingMessage.<span class="hljs-title function_">setHeader</span>(<span class="h…
39712 …hljs-title function_">setHeader</span>(<span class="hljs-string">'Set-Cookie'</span>, [<span class…
39714 <span class="hljs-keyword">const</span> headers = outgoingMessage.<span class="hljs-title function_…
39715 <span class="hljs-comment">// headers === { foo: 'bar', 'set-cookie': ['foo=bar', 'bar=baz'] }</spa…
39721 <li><code>name</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
39722 <li>Returns <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolea…
39725 outgoing headers. The header name is case-insensitive.</p>
39726-js"><span class="hljs-keyword">const</span> hasContentType = outgoingMessage.<span class="hljs-ti…
39732 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" …
39734 <p>Read-only. <code>true</code> if the headers were sent, otherwise <code>false</code>.</p>
39741 <p>Since <code>OutgoingMessage</code> should be a write-only stream,
39750 …<code class="language-js">outgoingMessage.<span class="hljs-title function_">removeHeader</span>(<…
39756 <li><code>name</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
39757 <li><code>value</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struct…
39758 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/t…
39766 <li><code>msesc</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struct…
39767 <li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Referenc…
39771 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/t…
39795 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" c…
39804 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" …
39814 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" …
39822 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" c…
39834 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" c…
39843 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" …
39859 <li><code>chunk</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struct…
39860 …r.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a…
39861 <li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Referenc…
39862 <li>Returns <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolea…
39878 with higher-level multi-part body encodings that may be used.</p>
39892 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" c…
39900 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Objec…
39922 <p><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/…
39930 <li><code>insecureHTTPParser</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScrip…
39932 avoided. See <a href="#cli_insecure_http_parser"><code>--insecure-http-parser</code></a> for more i…
39934 <li><code>maxHeaderSize</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Dat…
39935 <a href="#cli_max_http_header_size_size"><code>--max-http-header-size</code></a> for requests recei…
39941 <p><code>requestListener</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Re…
39951-flavor-selector" type="checkbox" checked aria-label="Show modern ES modules syntax"><pre><code cl…
39953 <span class="hljs-comment">// Create a local server to receive data from</span>
39954 …s="hljs-keyword">const</span> server = http.<span class="hljs-title function_">createServer</span>…
39955 …"hljs-title function_">writeHead</span>(<span class="hljs-number">200</span>, { <span class="hljs-
39956 …res.<span class="hljs-title function_">end</span>(<span class="hljs-variable constant_">JSON</span…
39957 <span class="hljs-attr">data</span>: <span class="hljs-string">'Hello World!'</span>
39961-title function_">listen</span>(<span class="hljs-number">8000</span>);</code></pre><code class="l…
39963 <span class="hljs-comment">// Create a local server to receive data from</span>
39964 <span class="hljs-keyword">const</span> server = http.<span class="hljs-title function_">createServ…
39966 <span class="hljs-comment">// Listen to the request event</span>
39967 …<span class="hljs-title function_">on</span>(<span class="hljs-string">'request'</span>, <span cla…
39968 …"hljs-title function_">writeHead</span>(<span class="hljs-number">200</span>, { <span class="hljs-
39969 …res.<span class="hljs-title function_">end</span>(<span class="hljs-variable constant_">JSON</span…
39970 <span class="hljs-attr">data</span>: <span class="hljs-string">'Hello World!'</span>
39974 server.<span class="hljs-title function_">listen</span>(<span class="hljs-number">8000</span>);</co…
39991 <li><code>url</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structur…
39992 <li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference…
39995 <li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Referenc…
40006-js">http.<span class="hljs-title function_">get</span>(<span class="hljs-string">'http://localhos…
40007 <span class="hljs-keyword">const</span> { statusCode } = res;
40008 …<span class="hljs-keyword">const</span> contentType = res.<span class="hljs-property">headers</spa…
40010 <span class="hljs-keyword">let</span> error;
40011 <span class="hljs-comment">// Any 2xx status code signals a successful response but</span>
40012 <span class="hljs-comment">// here we're only checking for 200.</span>
40013 <span class="hljs-keyword">if</span> (statusCode !== <span class="hljs-number">200</span>) {
40014 …error = <span class="hljs-keyword">new</span> <span class="hljs-title class_">Error</span>(<span c…
40015 … <span class="hljs-string">`Status Code: <span class="hljs-subst">${statusCode}</span>`</span>);
40016 …lass="hljs-keyword">else</span> <span class="hljs-keyword">if</span> (!<span class="hljs-regexp">/…
40017 …or = <span class="hljs-keyword">new</span> <span class="hljs-title class_">Error</span>(<span clas…
40018 …<span class="hljs-string">`Expected application/json but received <span class="hljs-subst">${conte…
40020 <span class="hljs-keyword">if</span> (error) {
40021 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">error</spa…
40022 <span class="hljs-comment">// Consume response data to free up memory</span>
40023 res.<span class="hljs-title function_">resume</span>();
40024 <span class="hljs-keyword">return</span>;
40027 …res.<span class="hljs-title function_">setEncoding</span>(<span class="hljs-string">'utf8'</span>);
40028 <span class="hljs-keyword">let</span> rawData = <span class="hljs-string">''</span>;
40029 ….<span class="hljs-title function_">on</span>(<span class="hljs-string">'data'</span>, <span class…
40030 …res.<span class="hljs-title function_">on</span>(<span class="hljs-string">'end'</span>, <span cla…
40031 <span class="hljs-keyword">try</span> {
40032 …<span class="hljs-keyword">const</span> parsedData = <span class="hljs-variable constant_">JSON</s…
40033 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>…
40034 } <span class="hljs-keyword">catch</span> (e) {
40035 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">error</spa…
40038 ….<span class="hljs-title function_">on</span>(<span class="hljs-string">'error'</span>, <span clas…
40039 …="hljs-variable language_">console</span>.<span class="hljs-title function_">error</span>(<span cl…
40042 <span class="hljs-comment">// Create a local server to receive data from</span>
40043 …s="hljs-keyword">const</span> server = http.<span class="hljs-title function_">createServer</span>…
40044 …"hljs-title function_">writeHead</span>(<span class="hljs-number">200</span>, { <span class="hljs-
40045 …res.<span class="hljs-title function_">end</span>(<span class="hljs-variable constant_">JSON</span…
40046 <span class="hljs-attr">data</span>: <span class="hljs-string">'Hello World!'</span>
40050 server.<span class="hljs-title function_">listen</span>(<span class="hljs-number">8000</span>);</co…
40065 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" c…
40067 <p>Read-only property specifying the maximum allowed size of HTTP headers in bytes.
40068 …figurable using the <a href="#cli_max_http_header_size_size"><code>--max-http-header-size</code></…
40095 <li><code>url</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structur…
40096 <li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference…
40098 …ass="type">&#x3C;http.Agent></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScrip…
40106 <li><code>auth</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
40108 <li><code>createConnection</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/…
40113 <li><code>defaultPort</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_…
40115 <li><code>family</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struc…
40118 <li><code>headers</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference…
40119 <li><code>hints</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struct…
40120 <li><code>host</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
40122 <li><code>hostname</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_str…
40124 <li><code>insecureHTTPParser</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScrip…
40126 avoided. See <a href="#cli_insecure_http_parser"><code>--insecure-http-parser</code></a> for more i…
40128 <li><code>localAddress</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data…
40129 <li><code>localPort</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_st…
40130 <li><code>lookup</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/…
40131 <li><code>maxHeaderSize</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Dat…
40132 <a href="#cli_max_http_header_size_size"><code>--max-http-header-size</code></a> for requests recei…
40135 <li><code>method</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struc…
40137 <li><code>path</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
40141 <li><code>port</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
40143 <li><code>protocol</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_str…
40144 <li><code>setHost</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_stru…
40146 <li><code>socketPath</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_s…
40148 <li><code>timeout</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_stru…
40154 <li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Referenc…
40164 <p>The optional <code>callback</code> parameter will be added as a one-time listener for
40169 …code class="language-js"><span class="hljs-keyword">const</span> http = <span class="hljs-built_in…
40171 <span class="hljs-keyword">const</span> postData = <span class="hljs-variable constant_">JSON</span…
40172 <span class="hljs-string">'msg'</span>: <span class="hljs-string">'Hello World!'</span>
40175 <span class="hljs-keyword">const</span> options = {
40176 <span class="hljs-attr">hostname</span>: <span class="hljs-string">'www.google.com'</span>,
40177 <span class="hljs-attr">port</span>: <span class="hljs-number">80</span>,
40178 <span class="hljs-attr">path</span>: <span class="hljs-string">'/upload'</span>,
40179 <span class="hljs-attr">method</span>: <span class="hljs-string">'POST'</span>,
40180 <span class="hljs-attr">headers</span>: {
40181 …<span class="hljs-string">'Content-Type'</span>: <span class="hljs-string">'application/json'</spa…
40182 …<span class="hljs-string">'Content-Length'</span>: <span class="hljs-title class_">Buffer</span>.<…
40186 …="hljs-keyword">const</span> req = http.<span class="hljs-title function_">request</span>(options,…
40187 …ass="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span c…
40188-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hlj…
40189 …res.<span class="hljs-title function_">setEncoding</span>(<span class="hljs-string">'utf8'</span>);
40190 ….<span class="hljs-title function_">on</span>(<span class="hljs-string">'data'</span>, <span class…
40191 …lass="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span …
40193 …res.<span class="hljs-title function_">on</span>(<span class="hljs-string">'end'</span>, <span cla…
40194 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>…
40198 ….<span class="hljs-title function_">on</span>(<span class="hljs-string">'error'</span>, <span clas…
40199 …s-variable language_">console</span>.<span class="hljs-title function_">error</span>(<span class="…
40202 <span class="hljs-comment">// Write data to request body</span>
40203 req.<span class="hljs-title function_">write</span>(postData);
40204 req.<span class="hljs-title function_">end</span>();</code></pre>
40206 must always call <code>req.end()</code> to signify the end of the request -
40215 <p>Sending a 'Connection: keep-alive' will notify Node.js that the connection to
40219 <p>Sending a 'Content-Length' header will disable the default chunked encoding.</p>
40223 Usually, when sending 'Expect: 100-continue', both a timeout and a listener
40233-js"><span class="hljs-keyword">const</span> options = <span class="hljs-keyword">new</span> <span…
40235 …="hljs-keyword">const</span> req = http.<span class="hljs-title function_">request</span>(options,…
40236 <span class="hljs-comment">// ...</span>
40353 <li><code>name</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
40355 <p>Performs the low-level validations on the provided <code>name</code> that are done when
40363 …s="language-js"><span class="hljs-keyword">const</span> { validateHeaderName } = <span class="hljs
40365 <span class="hljs-keyword">try</span> {
40366 <span class="hljs-title function_">validateHeaderName</span>(<span class="hljs-string">''</span>);
40367 } <span class="hljs-keyword">catch</span> (err) {
40368 …err <span class="hljs-keyword">instanceof</span> <span class="hljs-title class_">TypeError</span>;…
40369 …err.<span class="hljs-property">code</span>; <span class="hljs-comment">// --> 'ERR_INVALID_HTTP_T…
40370 …err.<span class="hljs-property">message</span>; <span class="hljs-comment">// --> 'Header name mus…
40377 <li><code>name</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
40378 <li><code>value</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struct…
40380 <p>Performs the low-level validations on the provided <code>value</code> that are done when
40390 …="language-js"><span class="hljs-keyword">const</span> { validateHeaderValue } = <span class="hljs
40392 <span class="hljs-keyword">try</span> {
40393 …<span class="hljs-title function_">validateHeaderValue</span>(<span class="hljs-string">'x-my-head…
40394 } <span class="hljs-keyword">catch</span> (err) {
40395 …err <span class="hljs-keyword">instanceof</span> <span class="hljs-title class_">TypeError</span>;…
40396 …an class="hljs-property">code</span> === <span class="hljs-string">'ERR_HTTP_INVALID_HEADER_VALUE'…
40397 …err.<span class="hljs-property">message</span>; <span class="hljs-comment">// --> 'Invalid value "…
40400 <span class="hljs-keyword">try</span> {
40401 …<span class="hljs-title function_">validateHeaderValue</span>(<span class="hljs-string">'x-my-head…
40402 } <span class="hljs-keyword">catch</span> (err) {
40403 …err <span class="hljs-keyword">instanceof</span> <span class="hljs-title class_">TypeError</span>;…
40404 …rr.<span class="hljs-property">code</span> === <span class="hljs-string">'ERR_INVALID_CHAR'</span>…
40405 …err.<span class="hljs-property">message</span>; <span class="hljs-comment">// --> 'Invalid charact…
40423 …ty api_stability_2"><a href="#documentation_stability_index">Stability: 2</a> - Stable</div><p></p>
40427 …ode class="language-js"><span class="hljs-keyword">const</span> http2 = <span class="hljs-built_in…
40429 <p>The Core API provides a low-level interface designed specifically around
40435 <code>'stream'</code>, can be emitted either by client-side code or server-side code.</p>
40436 <h4>Server-side example<span><a class="mark" href="#http2_server_side_example" id="http2_server_sid…
40439 <a href="https://http2.github.io/faq/#does-http2-require-encryption">unencrypted HTTP/2</a>, the us…
40442 …ode class="language-js"><span class="hljs-keyword">const</span> http2 = <span class="hljs-built_in…
40443 <span class="hljs-keyword">const</span> fs = <span class="hljs-built_in">require</span>(<span class…
40445 <span class="hljs-keyword">const</span> server = http2.<span class="hljs-title function_">createSec…
40446 …<span class="hljs-attr">key</span>: fs.<span class="hljs-title function_">readFileSync</span>(<spa…
40447 …<span class="hljs-attr">cert</span>: fs.<span class="hljs-title function_">readFileSync</span>(<sp…
40449-title function_">on</span>(<span class="hljs-string">'error'</span>, <span class="hljs-function">…
40451 …<span class="hljs-title function_">on</span>(<span class="hljs-string">'stream'</span>, <span clas…
40452 <span class="hljs-comment">// stream is a Duplex</span>
40453 stream.<span class="hljs-title function_">respond</span>({
40454 …<span class="hljs-string">'content-type'</span>: <span class="hljs-string">'text/html; charset=utf
40455 <span class="hljs-string">':status'</span>: <span class="hljs-number">200</span>
40457 …stream.<span class="hljs-title function_">end</span>(<span class="hljs-string">'&#x3C;h1>Hello Wor…
40460 server.<span class="hljs-title function_">listen</span>(<span class="hljs-number">8443</span>);</co…
40462 <pre><code class="language-bash">openssl req -x509 -newkey rsa:2048 -nodes -sha256 -subj <span clas…
40463 -keyout localhost-privkey.pem -out localhost-cert.pem</code></pre>
40464 <h4>Client-side example<span><a class="mark" href="#http2_client_side_example" id="http2_client_sid…
40466 …ode class="language-js"><span class="hljs-keyword">const</span> http2 = <span class="hljs-built_in…
40467 <span class="hljs-keyword">const</span> fs = <span class="hljs-built_in">require</span>(<span class…
40468 <span class="hljs-keyword">const</span> client = http2.<span class="hljs-title function_">connect</…
40469 …<span class="hljs-attr">ca</span>: fs.<span class="hljs-title function_">readFileSync</span>(<span…
40471-title function_">on</span>(<span class="hljs-string">'error'</span>, <span class="hljs-function">…
40473 …js-keyword">const</span> req = client.<span class="hljs-title function_">request</span>({ <span cl…
40475 …span class="hljs-title function_">on</span>(<span class="hljs-string">'response'</span>, <span cla…
40476 …<span class="hljs-keyword">for</span> (<span class="hljs-keyword">const</span> name <span class="h…
40477-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hlj…
40481 req.<span class="hljs-title function_">setEncoding</span>(<span class="hljs-string">'utf8'</span>);
40482 <span class="hljs-keyword">let</span> data = <span class="hljs-string">''</span>;
40483 ….<span class="hljs-title function_">on</span>(<span class="hljs-string">'data'</span>, <span class…
40484 req.<span class="hljs-title function_">on</span>(<span class="hljs-string">'end'</span>, <span clas…
40485 … class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<spa…
40486 client.<span class="hljs-title function_">close</span>();
40488 req.<span class="hljs-title function_">end</span>();</code></pre>
40506 <p>User code will not create <code>Http2Session</code> instances directly. Server-side
40508 new HTTP/2 connection is received. Client-side <code>Http2Session</code> instances are
40543 <li><code>error</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/G…
40552 <li><code>type</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
40553 <li><code>code</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
40554 <li><code>id</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structure…
40570 <li><code>errorCode</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_st…
40571 <li><code>lastStreamID</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data…
40590 …ass="language-js">session.<span class="hljs-title function_">settings</span>({ <span class="hljs-a…
40592 …an class="hljs-title function_">on</span>(<span class="hljs-string">'localSettings'</span>, <span …
40593 <span class="hljs-comment">/* Use the new settings */</span>
40600 …buffer_class_buffer" class="type">&#x3C;Buffer></a> The <code>PING</code> frame 8-byte payload</li>
40613-js">session.<span class="hljs-title function_">on</span>(<span class="hljs-string">'remoteSetting…
40614 <span class="hljs-comment">/* Use the new settings */</span>
40623 <li><code>flags</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struct…
40624 <li><code>rawHeaders</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Refere…
40628 …ode class="language-js"><span class="hljs-keyword">const</span> http2 = <span class="hljs-built_in…
40629 …<span class="hljs-title function_">on</span>(<span class="hljs-string">'stream'</span>, <span clas…
40630 …<span class="hljs-keyword">const</span> method = headers[<span class="hljs-string">':method'</span…
40631 <span class="hljs-keyword">const</span> path = headers[<span class="hljs-string">':path'</span>];
40632 <span class="hljs-comment">// ...</span>
40633 stream.<span class="hljs-title function_">respond</span>({
40634 <span class="hljs-string">':status'</span>: <span class="hljs-number">200</span>,
40635 …<span class="hljs-string">'content-type'</span>: <span class="hljs-string">'text/plain; charset=ut…
40637 stream.<span class="hljs-title function_">write</span>(<span class="hljs-string">'hello '</span>);
40638 stream.<span class="hljs-title function_">end</span>(<span class="hljs-string">'world'</span>);
40644 …ode class="language-js"><span class="hljs-keyword">const</span> http2 = <span class="hljs-built_in…
40646 <span class="hljs-comment">// Create an unencrypted HTTP/2 server</span>
40647 <span class="hljs-keyword">const</span> server = http2.<span class="hljs-title function_">createSer…
40649 …<span class="hljs-title function_">on</span>(<span class="hljs-string">'stream'</span>, <span clas…
40650 stream.<span class="hljs-title function_">respond</span>({
40651 …<span class="hljs-string">'content-type'</span>: <span class="hljs-string">'text/html; charset=utf
40652 <span class="hljs-string">':status'</span>: <span class="hljs-number">200</span>
40654-title function_">on</span>(<span class="hljs-string">'error'</span>, <span class="hljs-function">…
40655 …stream.<span class="hljs-title function_">end</span>(<span class="hljs-string">'&#x3C;h1>Hello Wor…
40658 server.<span class="hljs-title function_">listen</span>(<span class="hljs-number">80</span>);</code…
40670 <pre><code class="language-js">session.<span class="hljs-built_in">setTimeout</span>(<span class="h…
40671 …lass="hljs-title function_">on</span>(<span class="hljs-string">'timeout'</span>, <span class="hlj…
40677 …zilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | …
40688 <li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Referenc…
40701 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" …
40710 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" …
40720 <li><code>error</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/G…
40722 <li><code>code</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
40738 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" …
40747 …illa.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type">&#x3C;boolean></a> |…
40758 <li><code>code</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
40759 <li><code>lastStreamID</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data…
40760 …org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray" class="type">&#x3C;TypedArray><…
40772 <p>A prototype-less object describing the current local settings of this
40779 …illa.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string[]></a> |…
40790 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" …
40801 …org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray" class="type">&#x3C;TypedArray><…
40802 <li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Referenc…
40803 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boole…
40816 acknowledgment was received, and a <code>Buffer</code> containing the 8-byte <code>PING</code>
40818-js">session.<span class="hljs-title function_">ping</span>(<span class="hljs-title class_">Buffer…
40819 <span class="hljs-keyword">if</span> (!err) {
40820 …js-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="h…
40821 …="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span clas…
40825 64-bit timestamp (little endian) marking the start of the <code>PING</code> duration.</p>
40839 <p>A prototype-less object describing the current remote settings of this
40846 <li><code>windowSize</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_s…
40851 …ode class="language-js"><span class="hljs-keyword">const</span> http2 = <span class="hljs-built_in…
40853 <span class="hljs-keyword">const</span> server = http2.<span class="hljs-title function_">createSer…
40854 <span class="hljs-keyword">const</span> expectedWindowSize = <span class="hljs-number">2</span> ** …
40855 …<span class="hljs-title function_">on</span>(<span class="hljs-string">'connect'</span>, <span cla…
40857 <span class="hljs-comment">// Set local window size to be 2 ** 20</span>
40858 session.<span class="hljs-title function_">setLocalWindowSize</span>(expectedWindowSize);
40865 <li><code>msecs</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struct…
40866 <li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Referenc…
40892 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Objec…
40894 <li><code>effectiveLocalWindowSize</code> <a href="https://developer.mozilla.org/en-US/docs/Web/Jav…
40896 <li><code>effectiveRecvDataLength</code> <a href="https://developer.mozilla.org/en-US/docs/Web/Java…
40898 <li><code>nextStreamID</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data…
40900 <li><code>localWindowSize</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/D…
40902 <li><code>lastProcStreamID</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/…
40904 <li><code>remoteWindowSize</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/…
40906 <li><code>outboundQueueSize</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript…
40908 <li><code>deflateDynamicTableSize</code> <a href="https://developer.mozilla.org/en-US/docs/Web/Java…
40910 <li><code>inflateDynamicTableSize</code> <a href="https://developer.mozilla.org/en-US/docs/Web/Java…
40922 <li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Referenc…
40925 …illa.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error" class="type">&#x3C;Error></a> |…
40927 <li><code>duration</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_str…
40944 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" c…
40968 <li><code>alt</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structur…
40970-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;number></a> | <a href="htt…
40976 …ode class="language-js"><span class="hljs-keyword">const</span> http2 = <span class="hljs-built_in…
40978 <span class="hljs-keyword">const</span> server = http2.<span class="hljs-title function_">createSer…
40979 …<span class="hljs-title function_">on</span>(<span class="hljs-string">'session'</span>, <span cla…
40980 <span class="hljs-comment">// Set altsvc for origin https://example.org:80</span>
40981 …session.<span class="hljs-title function_">altsvc</span>(<span class="hljs-string">'h2=":8000"'</s…
40984 …<span class="hljs-title function_">on</span>(<span class="hljs-string">'stream'</span>, <span clas…
40985 <span class="hljs-comment">// Set altsvc for a specific stream</span>
40986 …ljs-property">session</span>.<span class="hljs-title function_">altsvc</span>(<span class="hljs-st…
41005 ASCII string containing a comma-delimited list of "alternative" protocols
41012 … href="https://www.iana.org/assignments/tls-extensiontype-values/tls-extensiontype-values.xhtml#al…
41020-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | <a href="#ur…
41026 …ode class="language-js"><span class="hljs-keyword">const</span> http2 = <span class="hljs-built_in…
41027 <span class="hljs-keyword">const</span> options = <span class="hljs-title function_">getSecureOptio…
41028 <span class="hljs-keyword">const</span> server = http2.<span class="hljs-title function_">createSec…
41029 …<span class="hljs-title function_">on</span>(<span class="hljs-string">'stream'</span>, <span clas…
41030 stream.<span class="hljs-title function_">respond</span>();
41031 stream.<span class="hljs-title function_">end</span>(<span class="hljs-string">'ok'</span>);
41033 …<span class="hljs-title function_">on</span>(<span class="hljs-string">'session'</span>, <span cla…
41034 …ssion.<span class="hljs-title function_">origin</span>(<span class="hljs-string">'https://example.…
41047 …ode class="language-js"><span class="hljs-keyword">const</span> http2 = <span class="hljs-built_in…
41048 <span class="hljs-keyword">const</span> options = <span class="hljs-title function_">getSecureOptio…
41049 options.<span class="hljs-property">origins</span> = [<span class="hljs-string">'https://example.co…
41050 <span class="hljs-keyword">const</span> server = http2.<span class="hljs-title function_">createSec…
41051 …<span class="hljs-title function_">on</span>(<span class="hljs-string">'stream'</span>, <span clas…
41052 stream.<span class="hljs-title function_">respond</span>();
41053 stream.<span class="hljs-title function_">end</span>(<span class="hljs-string">'ok'</span>);
41067 <li><code>alt</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structur…
41068 <li><code>origin</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struc…
41069 <li><code>streamId</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_str…
41075 …ode class="language-js"><span class="hljs-keyword">const</span> http2 = <span class="hljs-built_in…
41076 <span class="hljs-keyword">const</span> client = http2.<span class="hljs-title function_">connect</…
41078 …<span class="hljs-title function_">on</span>(<span class="hljs-string">'altsvc'</span>, <span clas…
41079 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>…
41080 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>…
41081 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>…
41088 <li><code>origins</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_stru…
41094 …ode class="language-js"><span class="hljs-keyword">const</span> http2 = <span class="hljs-built_in…
41095 <span class="hljs-keyword">const</span> client = http2.<span class="hljs-title function_">connect</…
41097 …<span class="hljs-title function_">on</span>(<span class="hljs-string">'origin'</span>, <span clas…
41098 …ass="hljs-keyword">for</span> (<span class="hljs-keyword">let</span> n = <span class="hljs-number"…
41099 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>…
41111 <p><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/…
41113 <li><code>endStream</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_st…
41116 <li><code>exclusive</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_st…
41120 <li><code>parent</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struc…
41122 <li><code>weight</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struc…
41125 <li><code>waitForTrailers</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/D…
41140 …ode class="language-js"><span class="hljs-keyword">const</span> http2 = <span class="hljs-built_in…
41141 <span class="hljs-keyword">const</span> clientSession = http2.<span class="hljs-title function_">co…
41142 <span class="hljs-keyword">const</span> {
41145 } = http2.<span class="hljs-property">constants</span>;
41147 …ass="hljs-keyword">const</span> req = clientSession.<span class="hljs-title function_">request</sp…
41148 …span class="hljs-title function_">on</span>(<span class="hljs-string">'response'</span>, <span cla…
41149 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>…
41150-title function_">on</span>(<span class="hljs-string">'data'</span>, <span class="hljs-function">(…
41151 … class="hljs-title function_">on</span>(<span class="hljs-string">'end'</span>, <span class="hljs-
41164 <p>The <code>:method</code> and <code>:path</code> pseudo-headers are not specified within <code>he…
41179 may have up to 2<sup>31</sup>-1 <code>Http2Stream</code> instances over its lifetime.</p>
41194 <p>The default text character encoding for all <code>Http2Stream</code>s is UTF-8. As a best
41196 the <code>'content-type'</code> header should be set and should identify the character
41198 <pre><code class="language-js">stream.<span class="hljs-title function_">respond</span>({
41199 …<span class="hljs-string">'content-type'</span>: <span class="hljs-string">'text/html; charset=utf
41200 <span class="hljs-string">':status'</span>: <span class="hljs-number">200</span>
41244 abnormally aborted in mid-communication.
41262 <li><code>error</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/G…
41271 <li><code>type</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
41272 <li><code>code</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
41273 <li><code>id</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structure…
41302 <li><code>flags</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struct…
41310 …ge-js">stream.<span class="hljs-title function_">on</span>(<span class="hljs-string">'trailers'</s…
41311 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>…
41326 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" …
41335 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" c…
41344 …s://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3…
41346 <li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Referenc…
41356 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" …
41364 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" …
41373 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" …
41383 …zilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;number></a> | …
41392 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" …
41401 <li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference…
41403 <li><code>exclusive</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_st…
41407 <li><code>parent</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struc…
41409 <li><code>weight</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struc…
41412 <li><code>silent</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struc…
41423 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" c…
41468 <li><code>msecs</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struct…
41469 <li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Referenc…
41471 …ode class="language-js"><span class="hljs-keyword">const</span> http2 = <span class="hljs-built_in…
41472 <span class="hljs-keyword">const</span> client = http2.<span class="hljs-title function_">connect</…
41473 <span class="hljs-keyword">const</span> { NGHTTP2_CANCEL } = http2.<span class="hljs-property">cons…
41474 …js-keyword">const</span> req = client.<span class="hljs-title function_">request</span>({ <span cl…
41476 <span class="hljs-comment">// Cancel the stream if there's no activity after 5 seconds</span>
41477 …class="hljs-built_in">setTimeout</span>(<span class="hljs-number">5000</span>, <span class="hljs-f…
41485 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Objec…
41487 <li><code>localWindowSize</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/D…
41489 …eloper.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;numbe…
41491 <li><code>localClose</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_s…
41492 <li><code>remoteClose</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_…
41494 <li><code>sumDependencyWeight</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScri…
41497 <li><code>weight</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struc…
41515 …ode class="language-js"><span class="hljs-keyword">const</span> http2 = <span class="hljs-built_in…
41516 <span class="hljs-keyword">const</span> server = http2.<span class="hljs-title function_">createSer…
41517 …<span class="hljs-title function_">on</span>(<span class="hljs-string">'stream'</span>, <span clas…
41518 …ljs-title function_">respond</span>(<span class="hljs-literal">undefined</span>, { <span class="hl…
41519 …stream.<span class="hljs-title function_">on</span>(<span class="hljs-string">'wantTrailers'</span…
41520 …stream.<span class="hljs-title function_">sendTrailers</span>({ <span class="hljs-attr">xyz</span>…
41522 …stream.<span class="hljs-title function_">end</span>(<span class="hljs-string">'Hello World'</span…
41524 <p>The HTTP/1 specification forbids trailers from containing HTTP/2 pseudo-header
41542 the request contained <code>Expect: 100-continue</code>. This is an instruction that
41552 …ge-js">stream.<span class="hljs-title function_">on</span>(<span class="hljs-string">'headers'</sp…
41553 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>…
41562 …uage-js">stream.<span class="hljs-title function_">on</span>(<span class="hljs-string">'push'</spa…
41563 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>…
41573 …ode class="language-js"><span class="hljs-keyword">const</span> http2 = <span class="hljs-built_in…
41574 <span class="hljs-keyword">const</span> client = http2.<span class="hljs-title function_">connect</…
41575 …js-keyword">const</span> req = client.<span class="hljs-title function_">request</span>({ <span cl…
41576 …span class="hljs-title function_">on</span>(<span class="hljs-string">'response'</span>, <span cla…
41577 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>…
41603 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" …
41605 <p>True if headers were sent, false otherwise (read-only).</p>
41611 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" …
41613 <p>Read-only property mapped to the <code>SETTINGS_ENABLE_PUSH</code> flag of the remote
41623 <li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference…
41625 <li><code>exclusive</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_st…
41629 <li><code>parent</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struc…
41633 <li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Referenc…
41636 <li><code>err</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Glo…
41646 …ode class="language-js"><span class="hljs-keyword">const</span> http2 = <span class="hljs-built_in…
41647 <span class="hljs-keyword">const</span> server = http2.<span class="hljs-title function_">createSer…
41648 …<span class="hljs-title function_">on</span>(<span class="hljs-string">'stream'</span>, <span clas…
41649 …stream.<span class="hljs-title function_">respond</span>({ <span class="hljs-string">':status'</sp…
41650-title function_">pushStream</span>({ <span class="hljs-string">':path'</span>: <span class="hljs-
41651 <span class="hljs-keyword">if</span> (err) <span class="hljs-keyword">throw</span> err;
41652 …pushStream.<span class="hljs-title function_">respond</span>({ <span class="hljs-string">':status'…
41653 …pushStream.<span class="hljs-title function_">end</span>(<span class="hljs-string">'some pushed da…
41655 …stream.<span class="hljs-title function_">end</span>(<span class="hljs-string">'some data'</span>);
41659 <code>true</code> to enable server-side bandwidth balancing between concurrent streams.</p>
41676 <li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference…
41678 <li><code>endStream</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_st…
41680 <li><code>waitForTrailers</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/D…
41685 …ode class="language-js"><span class="hljs-keyword">const</span> http2 = <span class="hljs-built_in…
41686 <span class="hljs-keyword">const</span> server = http2.<span class="hljs-title function_">createSer…
41687 …<span class="hljs-title function_">on</span>(<span class="hljs-string">'stream'</span>, <span clas…
41688 …stream.<span class="hljs-title function_">respond</span>({ <span class="hljs-string">':status'</sp…
41689 …stream.<span class="hljs-title function_">end</span>(<span class="hljs-string">'some data'</span>);
41699 …ode class="language-js"><span class="hljs-keyword">const</span> http2 = <span class="hljs-built_in…
41700 <span class="hljs-keyword">const</span> server = http2.<span class="hljs-title function_">createSer…
41701 …<span class="hljs-title function_">on</span>(<span class="hljs-string">'stream'</span>, <span clas…
41702-title function_">respond</span>({ <span class="hljs-string">':status'</span>: <span class="hljs-n…
41703 …stream.<span class="hljs-title function_">on</span>(<span class="hljs-string">'wantTrailers'</span…
41704 …stream.<span class="hljs-title function_">sendTrailers</span>({ <span class="hljs-attr">ABC</span>…
41706 …stream.<span class="hljs-title function_">end</span>(<span class="hljs-string">'some data'</span>);
41725 <li><code>fd</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structure…
41727 <li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference…
41729 <li><code>statCheck</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Referen…
41730 <li><code>waitForTrailers</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/D…
41732 <li><code>offset</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struc…
41733 <li><code>length</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struc…
41743 …ode class="language-js"><span class="hljs-keyword">const</span> http2 = <span class="hljs-built_in…
41744 <span class="hljs-keyword">const</span> fs = <span class="hljs-built_in">require</span>(<span class…
41746 <span class="hljs-keyword">const</span> server = http2.<span class="hljs-title function_">createSer…
41747 …<span class="hljs-title function_">on</span>(<span class="hljs-string">'stream'</span>, <span clas…
41748 …ljs-keyword">const</span> fd = fs.<span class="hljs-title function_">openSync</span>(<span class="…
41750 …<span class="hljs-keyword">const</span> stat = fs.<span class="hljs-title function_">fstatSync</sp…
41751 <span class="hljs-keyword">const</span> headers = {
41752 <span class="hljs-string">'content-length'</span>: stat.<span class="hljs-property">size</span>,
41753 …<span class="hljs-string">'last-modified'</span>: stat.<span class="hljs-property">mtime</span>.<s…
41754 …<span class="hljs-string">'content-type'</span>: <span class="hljs-string">'text/plain; charset=ut…
41756 stream.<span class="hljs-title function_">respondWithFD</span>(fd, headers);
41757 …lass="hljs-title function_">on</span>(<span class="hljs-string">'close'</span>, <span class="hljs-
41770 is not supported and may result in data loss. Re-using a file descriptor
41780 …ode class="language-js"><span class="hljs-keyword">const</span> http2 = <span class="hljs-built_in…
41781 <span class="hljs-keyword">const</span> fs = <span class="hljs-built_in">require</span>(<span class…
41783 <span class="hljs-keyword">const</span> server = http2.<span class="hljs-title function_">createSer…
41784 …<span class="hljs-title function_">on</span>(<span class="hljs-string">'stream'</span>, <span clas…
41785 …ljs-keyword">const</span> fd = fs.<span class="hljs-title function_">openSync</span>(<span class="…
41787 …<span class="hljs-keyword">const</span> stat = fs.<span class="hljs-title function_">fstatSync</sp…
41788 <span class="hljs-keyword">const</span> headers = {
41789 <span class="hljs-string">'content-length'</span>: stat.<span class="hljs-property">size</span>,
41790 …<span class="hljs-string">'last-modified'</span>: stat.<span class="hljs-property">mtime</span>.<s…
41791 …<span class="hljs-string">'content-type'</span>: <span class="hljs-string">'text/plain; charset=ut…
41793 …span class="hljs-title function_">respondWithFD</span>(fd, headers, { <span class="hljs-attr">wait…
41794 …stream.<span class="hljs-title function_">on</span>(<span class="hljs-string">'wantTrailers'</span…
41795 …stream.<span class="hljs-title function_">sendTrailers</span>({ <span class="hljs-attr">ABC</span>…
41798 …lass="hljs-title function_">on</span>(<span class="hljs-string">'close'</span>, <span class="hljs-
41815 <li><code>path</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
41817 <li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference…
41819 <li><code>statCheck</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Referen…
41820 <li><code>onError</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference…
41822 <li><code>waitForTrailers</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/D…
41824 <li><code>offset</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struc…
41825 <li><code>length</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struc…
41841 …ode class="language-js"><span class="hljs-keyword">const</span> http2 = <span class="hljs-built_in…
41842 <span class="hljs-keyword">const</span> server = http2.<span class="hljs-title function_">createSer…
41843 …<span class="hljs-title function_">on</span>(<span class="hljs-string">'stream'</span>, <span clas…
41844 …<span class="hljs-keyword">function</span> <span class="hljs-title function_">statCheck</span>(<sp…
41845 …headers[<span class="hljs-string">'last-modified'</span>] = stat.<span class="hljs-property">mtime…
41848 …<span class="hljs-keyword">function</span> <span class="hljs-title function_">onError</span>(<span…
41849 …<span class="hljs-comment">// stream.respond() can throw if the stream has been destroyed by</span>
41850 <span class="hljs-comment">// the other side.</span>
41851 <span class="hljs-keyword">try</span> {
41852 …<span class="hljs-keyword">if</span> (err.<span class="hljs-property">code</span> === <span class=…
41853 …stream.<span class="hljs-title function_">respond</span>({ <span class="hljs-string">':status'</sp…
41854 } <span class="hljs-keyword">else</span> {
41855 …stream.<span class="hljs-title function_">respond</span>({ <span class="hljs-string">':status'</sp…
41857 } <span class="hljs-keyword">catch</span> (err) {
41858 <span class="hljs-comment">// Perform actual error handling.</span>
41859 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>…
41861 stream.<span class="hljs-title function_">end</span>();
41864 …stream.<span class="hljs-title function_">respondWithFile</span>(<span class="hljs-string">'/some/…
41865 …{ <span class="hljs-string">'content-type'</span>: <span class="hljs-string">'text/plain; charset=…
41872 …ode class="language-js"><span class="hljs-keyword">const</span> http2 = <span class="hljs-built_in…
41873 <span class="hljs-keyword">const</span> server = http2.<span class="hljs-title function_">createSer…
41874 …<span class="hljs-title function_">on</span>(<span class="hljs-string">'stream'</span>, <span clas…
41875 …<span class="hljs-keyword">function</span> <span class="hljs-title function_">statCheck</span>(<sp…
41876 <span class="hljs-comment">// Check the stat here...</span>
41877 …stream.<span class="hljs-title function_">respond</span>({ <span class="hljs-string">':status'</sp…
41878 …<span class="hljs-keyword">return</span> <span class="hljs-literal">false</span>; <span class="hlj…
41880 …stream.<span class="hljs-title function_">respondWithFile</span>(<span class="hljs-string">'/some/…
41881 …{ <span class="hljs-string">'content-type'</span>: <span class="hljs-string">'text/plain; charset=…
41884 <p>The <code>content-length</code> header field will be automatically set.</p>
41899 …ode class="language-js"><span class="hljs-keyword">const</span> http2 = <span class="hljs-built_in…
41900 <span class="hljs-keyword">const</span> server = http2.<span class="hljs-title function_">createSer…
41901 …<span class="hljs-title function_">on</span>(<span class="hljs-string">'stream'</span>, <span clas…
41902 …stream.<span class="hljs-title function_">respondWithFile</span>(<span class="hljs-string">'/some/…
41903 …{ <span class="hljs-string">'content-type'</span>: <span class="hljs-string">'text/plain; charset=…
41904 … { <span class="hljs-attr">waitForTrailers</span>: <span class="hljs-literal">true</span> });
41905 …stream.<span class="hljs-title function_">on</span>(<span class="hljs-string">'wantTrailers'</span…
41906 …stream.<span class="hljs-title function_">sendTrailers</span>({ <span class="hljs-attr">ABC</span>…
41929 a request with an HTTP <code>Expect: 100-continue</code> is received. If this event is
41979 <li><code>flags</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struct…
41980 <li><code>rawHeaders</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Refere…
41986 …ode class="language-js"><span class="hljs-keyword">const</span> http2 = <span class="hljs-built_in…
41987 <span class="hljs-keyword">const</span> {
41992 } = http2.<span class="hljs-property">constants</span>;
41994 <span class="hljs-keyword">const</span> server = http2.<span class="hljs-title function_">createSer…
41995 …<span class="hljs-title function_">on</span>(<span class="hljs-string">'stream'</span>, <span clas…
41996 <span class="hljs-keyword">const</span> method = headers[HTTP2_HEADER_METHOD];
41997 <span class="hljs-keyword">const</span> path = headers[HTTP2_HEADER_PATH];
41998 <span class="hljs-comment">// ...</span>
41999 stream.<span class="hljs-title function_">respond</span>({
42000 [HTTP2_HEADER_STATUS]: <span class="hljs-number">200</span>,
42001 [HTTP2_HEADER_CONTENT_TYPE]: <span class="hljs-string">'text/plain; charset=utf-8'</span>
42003 stream.<span class="hljs-title function_">write</span>(<span class="hljs-string">'hello '</span>);
42004 stream.<span class="hljs-title function_">end</span>(<span class="hljs-string">'world'</span>);
42026 <li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Referenc…
42048 <li><code>msecs</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struct…
42049 <li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Referenc…
42071 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" c…
42108 time a request with an HTTP <code>Expect: 100-continue</code> is received. If this event
42158 <li><code>flags</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struct…
42159 <li><code>rawHeaders</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Refere…
42165 …ode class="language-js"><span class="hljs-keyword">const</span> http2 = <span class="hljs-built_in…
42166 <span class="hljs-keyword">const</span> {
42171 } = http2.<span class="hljs-property">constants</span>;
42173 <span class="hljs-keyword">const</span> options = <span class="hljs-title function_">getOptionsSome…
42175 <span class="hljs-keyword">const</span> server = http2.<span class="hljs-title function_">createSec…
42176 …<span class="hljs-title function_">on</span>(<span class="hljs-string">'stream'</span>, <span clas…
42177 <span class="hljs-keyword">const</span> method = headers[HTTP2_HEADER_METHOD];
42178 <span class="hljs-keyword">const</span> path = headers[HTTP2_HEADER_PATH];
42179 <span class="hljs-comment">// ...</span>
42180 stream.<span class="hljs-title function_">respond</span>({
42181 [HTTP2_HEADER_STATUS]: <span class="hljs-number">200</span>,
42182 [HTTP2_HEADER_CONTENT_TYPE]: <span class="hljs-string">'text/plain; charset=utf-8'</span>
42184 stream.<span class="hljs-title function_">write</span>(<span class="hljs-string">'hello '</span>);
42185 stream.<span class="hljs-title function_">end</span>(<span class="hljs-string">'world'</span>);
42209 <li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Referenc…
42223 <li><code>msecs</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struct…
42224 <li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Referenc…
42246 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" c…
42292 <li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference…
42294 <li><code>maxDeflateDynamicTableSize</code> <a href="https://developer.mozilla.org/en-US/docs/Web/J…
42296 <li><code>maxSettings</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_…
42298 <li><code>maxSessionMemory</code><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/D…
42307 <li><code>maxHeaderListPairs</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScrip…
42311 <li><code>maxOutstandingPings</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScri…
42313 <li><code>maxSendHeaderBlockLength</code> <a href="https://developer.mozilla.org/en-US/docs/Web/Jav…
42317 <li><code>paddingStrategy</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/D…
42325 padding to ensure that the total frame length, including the 9-byte
42333 <li><code>peerMaxConcurrentStreams</code> <a href="https://developer.mozilla.org/en-US/docs/Web/Jav…
42337 <li><code>maxSessionInvalidFrames</code> <a href="https://developer.mozilla.org/en-US/docs/Web/Java…
42340 <li><code>maxSessionRejectedStreams</code> <a href="https://developer.mozilla.org/en-US/docs/Web/Ja…
42362 <li><code>unknownProtocolTimeout</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaS…
42369 <li><code>onRequestHandler</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/…
42375 <a href="https://http2.github.io/faq/#does-http2-require-encryption">unencrypted HTTP/2</a>, the us…
42378 …ode class="language-js"><span class="hljs-keyword">const</span> http2 = <span class="hljs-built_in…
42380 <span class="hljs-comment">// Create an unencrypted HTTP/2 server.</span>
42381 <span class="hljs-comment">// Since there are no browsers known that support</span>
42382 <span class="hljs-comment">// unencrypted HTTP/2, the use of `http2.createSecureServer()`</span>
42383 <span class="hljs-comment">// is necessary when communicating with browser clients.</span>
42384 <span class="hljs-keyword">const</span> server = http2.<span class="hljs-title function_">createSer…
42386 …<span class="hljs-title function_">on</span>(<span class="hljs-string">'stream'</span>, <span clas…
42387 stream.<span class="hljs-title function_">respond</span>({
42388 …<span class="hljs-string">'content-type'</span>: <span class="hljs-string">'text/html; charset=utf
42389 <span class="hljs-string">':status'</span>: <span class="hljs-number">200</span>
42391 …stream.<span class="hljs-title function_">end</span>(<span class="hljs-string">'&#x3C;h1>Hello Wor…
42394 server.<span class="hljs-title function_">listen</span>(<span class="hljs-number">80</span>);</code…
42422 <li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference…
42424 <li><code>allowHTTP1</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_s…
42428 <li><code>maxDeflateDynamicTableSize</code> <a href="https://developer.mozilla.org/en-US/docs/Web/J…
42430 <li><code>maxSettings</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_…
42432 <li><code>maxSessionMemory</code><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/D…
42441 <li><code>maxHeaderListPairs</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScrip…
42445 <li><code>maxOutstandingPings</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScri…
42447 <li><code>maxSendHeaderBlockLength</code> <a href="https://developer.mozilla.org/en-US/docs/Web/Jav…
42451 <li><code>paddingStrategy</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/D…
42460 9-byte header, is a multiple of 8. For each frame, there is a maximum
42467 <li><code>peerMaxConcurrentStreams</code> <a href="https://developer.mozilla.org/en-US/docs/Web/Jav…
42471 <li><code>maxSessionInvalidFrames</code> <a href="https://developer.mozilla.org/en-US/docs/Web/Java…
42474 <li><code>maxSessionRejectedStreams</code> <a href="https://developer.mozilla.org/en-US/docs/Web/Ja…
42484 <li><code>origins</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_stru…
42486 <li><code>unknownProtocolTimeout</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaS…
42492 <li><code>onRequestHandler</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/…
42497 …ode class="language-js"><span class="hljs-keyword">const</span> http2 = <span class="hljs-built_in…
42498 <span class="hljs-keyword">const</span> fs = <span class="hljs-built_in">require</span>(<span class…
42500 <span class="hljs-keyword">const</span> options = {
42501 …<span class="hljs-attr">key</span>: fs.<span class="hljs-title function_">readFileSync</span>(<spa…
42502 …<span class="hljs-attr">cert</span>: fs.<span class="hljs-title function_">readFileSync</span>(<sp…
42505 <span class="hljs-comment">// Create a secure HTTP/2 server</span>
42506 <span class="hljs-keyword">const</span> server = http2.<span class="hljs-title function_">createSec…
42508 …<span class="hljs-title function_">on</span>(<span class="hljs-string">'stream'</span>, <span clas…
42509 stream.<span class="hljs-title function_">respond</span>({
42510 …<span class="hljs-string">'content-type'</span>: <span class="hljs-string">'text/html; charset=utf
42511 <span class="hljs-string">':status'</span>: <span class="hljs-number">200</span>
42513 …stream.<span class="hljs-title function_">end</span>(<span class="hljs-string">'&#x3C;h1>Hello Wor…
42516 server.<span class="hljs-title function_">listen</span>(<span class="hljs-number">80</span>);</code…
42538 <li><code>authority</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_st…
42540 prefix, host name, and IP port (if a non-default port is used). Userinfo
42543 <li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference…
42545 <li><code>maxDeflateDynamicTableSize</code> <a href="https://developer.mozilla.org/en-US/docs/Web/J…
42547 <li><code>maxSettings</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_…
42549 <li><code>maxSessionMemory</code><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/D…
42558 <li><code>maxHeaderListPairs</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScrip…
42562 <li><code>maxOutstandingPings</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScri…
42564 <li><code>maxReservedRemoteStreams</code> <a href="https://developer.mozilla.org/en-US/docs/Web/Jav…
42568 is 0. The maximum allowed value is 22<sup>32</sup>-1. A negative value sets
42570 <li><code>maxSendHeaderBlockLength</code> <a href="https://developer.mozilla.org/en-US/docs/Web/Jav…
42574 <li><code>paddingStrategy</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/D…
42583 9-byte header, is a multiple of 8. For each frame, there is a maximum
42590 <li><code>peerMaxConcurrentStreams</code> <a href="https://developer.mozilla.org/en-US/docs/Web/Jav…
42594 <li><code>protocol</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_str…
42599 <li><code>createConnection</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/…
42603 <li><code>unknownProtocolTimeout</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaS…
42609 …r.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type">&#x3C;Func…
42614 …ode class="language-js"><span class="hljs-keyword">const</span> http2 = <span class="hljs-built_in…
42615 <span class="hljs-keyword">const</span> client = http2.<span class="hljs-title function_">connect</…
42617 <span class="hljs-comment">/* Use the client */</span>
42619 client.<span class="hljs-title function_">close</span>();</code></pre>
42728 for use with the <code>HTTP2-Settings</code> header field.</p>
42729 …ode class="language-js"><span class="hljs-keyword">const</span> http2 = <span class="hljs-built_in…
42731-keyword">const</span> packed = http2.<span class="hljs-title function_">getPackedSettings</span>(…
42733-variable language_">console</span>.<span class="hljs-title function_">log</span>(packed.<span cla…
42734 <span class="hljs-comment">// Prints: AAIAAAAA</span></code></pre>
42740 …" class="type">&#x3C;Buffer></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScrip…
42750 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Symbol_type" c…
42756 <p>Headers are represented as own-properties on JavaScript objects. The property
42757 keys will be serialized to lower-case. Property values should be strings (if
42760 <pre><code class="language-js"><span class="hljs-keyword">const</span> headers = {
42761 <span class="hljs-string">':status'</span>: <span class="hljs-string">'200'</span>,
42762 <span class="hljs-string">'content-type'</span>: <span class="hljs-string">'text-plain'</span>,
42763-string">'ABC'</span>: [<span class="hljs-string">'has'</span>, <span class="hljs-string">'more'</…
42766 stream.<span class="hljs-title function_">respond</span>(headers);</code></pre>
42775 <code>:protocol</code>, <code>age</code>, <code>authorization</code>, <code>access-control-allow-cr…
42776 <code>access-control-max-age</code>, <code>access-control-request-method</code>, <code>content-enco…
42777 <code>content-language</code>, <code>content-length</code>, <code>content-location</code>, <code>co…
42778 <code>content-range</code>, <code>content-type</code>, <code>date</code>, <code>dnt</code>, <code>e…
42779 <code>if-match</code>, <code>if-modified-since</code>, <code>if-none-match</code>, <code>if-range</…
42780 <code>if-unmodified-since</code>, <code>last-modified</code>, <code>location</code>, <code>max-forw…
42781 <code>proxy-authorization</code>, <code>range</code>, <code>referer</code>,<code>retry-after</code>…
42782 <code>upgrade-insecure-requests</code>, <code>user-agent</code> or <code>x-content-type-options</co…
42784 <li><code>set-cookie</code> is always an array. Duplicates are added to the array.</li>
42788 …ode class="language-js"><span class="hljs-keyword">const</span> http2 = <span class="hljs-built_in…
42789 <span class="hljs-keyword">const</span> server = http2.<span class="hljs-title function_">createSer…
42790 …<span class="hljs-title function_">on</span>(<span class="hljs-string">'stream'</span>, <span clas…
42791 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>…
42792 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>…
42800 <pre><code class="language-js"><span class="hljs-keyword">const</span> headers = {
42801 <span class="hljs-string">':status'</span>: <span class="hljs-string">'200'</span>,
42802 <span class="hljs-string">'content-type'</span>: <span class="hljs-string">'text-plain'</span>,
42803 <span class="hljs-string">'cookie'</span>: <span class="hljs-string">'some-cookie'</span>,
42804 …<span class="hljs-string">'other-sensitive-header'</span>: <span class="hljs-string">'very secret …
42805 …an class="hljs-property">sensitiveHeaders</span>]: [<span class="hljs-string">'cookie'</span>, <sp…
42808 stream.<span class="hljs-title function_">respond</span>(headers);</code></pre>
42835 <li><code>headerTableSize</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/D…
42837 is 22<sup>32</sup>-1. <strong>Default:</strong> <code>4096</code>.</li>
42838 <li><code>enablePush</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_s…
42840 <li><code>initialWindowSize</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript…
42841 bytes for stream-level flow control. The minimum allowed value is 0. The
42842 maximum allowed value is 2<sup>32</sup>-1. <strong>Default:</strong> <code>65535</code>.</li>
42843 <li><code>maxFrameSize</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data…
42845 22<sup>24</sup>-1. <strong>Default:</strong> <code>16384</code>.</li>
42846 <li><code>maxConcurrentStreams</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScr…
42848 implies, at least theoretically, 2<sup>32</sup>-1 streams may be open
42850 is 0. The maximum allowed value is 2<sup>32</sup>-1. <strong>Default:</strong>
42852 <li><code>maxHeaderListSize</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript…
42854 maximum allowed value is 22<sup>32</sup>-1. <strong>Default:</strong> <code>65535</code>.</li>
42855 <li><code>maxHeaderSize</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Dat…
42856 <li><code>enableConnectProtocol</code><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScr…
42882 <p>Header field names are <em>case-insensitive</em> and are transmitted over the wire
42883 strictly as lower-case strings. The API provided by Node.js allows header
42884 names to be set as mixed-case strings (e.g. <code>Content-Type</code>) but will convert
42885 those to lower-case (e.g. <code>content-type</code>) upon transmission.</p>
42886 <p>Header field-names <em>must only</em> contain one or more of the following ASCII
42887 characters: <code>a</code>-<code>z</code>, <code>A</code>-<code>Z</code>, <code>0</code>-<code>9</c…
42888 <code>-</code>, <code>.</code>, <code>^</code>, <code>_</code>, <code>`</code> (backtick), <code>|<…
42892 new-line or carriage return characters and <em>should</em> be limited to US-ASCII
42897 …ode class="language-js"><span class="hljs-keyword">const</span> http2 = <span class="hljs-built_in…
42899 <span class="hljs-keyword">const</span> client = http2.<span class="hljs-title function_">connect</…
42901 …<span class="hljs-title function_">on</span>(<span class="hljs-string">'stream'</span>, <span clas…
42902 ….<span class="hljs-title function_">on</span>(<span class="hljs-string">'push'</span>, <span class…
42903 <span class="hljs-comment">// Process response headers</span>
42905-title function_">on</span>(<span class="hljs-string">'data'</span>, <span class="hljs-function">(…
42908 …js-keyword">const</span> req = client.<span class="hljs-title function_">request</span>({ <span cl…
42913 …code class="language-js"><span class="hljs-keyword">const</span> net = <span class="hljs-built_in"…
42915 …s="hljs-keyword">const</span> server = net.<span class="hljs-title function_">createServer</span>(…
42916 <span class="hljs-keyword">let</span> name = <span class="hljs-string">''</span>;
42917 …socket.<span class="hljs-title function_">setEncoding</span>(<span class="hljs-string">'utf8'</spa…
42918 ….<span class="hljs-title function_">on</span>(<span class="hljs-string">'data'</span>, <span class…
42919-title function_">on</span>(<span class="hljs-string">'end'</span>, <span class="hljs-function">()…
42922 server.<span class="hljs-title function_">listen</span>(<span class="hljs-number">8000</span>);</co…
42924 …ode class="language-js"><span class="hljs-keyword">const</span> http2 = <span class="hljs-built_in…
42925 <span class="hljs-keyword">const</span> { NGHTTP2_REFUSED_STREAM } = http2.<span class="hljs-proper…
42926 <span class="hljs-keyword">const</span> net = <span class="hljs-built_in">require</span>(<span clas…
42928 <span class="hljs-keyword">const</span> proxy = http2.<span class="hljs-title function_">createServ…
42929 …<span class="hljs-title function_">on</span>(<span class="hljs-string">'stream'</span>, <span clas…
42930 …<span class="hljs-keyword">if</span> (headers[<span class="hljs-string">':method'</span>] !== <spa…
42931 <span class="hljs-comment">// Only accept CONNECT requests</span>
42932 stream.<span class="hljs-title function_">close</span>(NGHTTP2_REFUSED_STREAM);
42933 <span class="hljs-keyword">return</span>;
42935-keyword">const</span> auth = <span class="hljs-keyword">new</span> <span class="hljs-title functi…
42936 <span class="hljs-comment">// It's a very good idea to verify that hostname and port are</span>
42937 <span class="hljs-comment">// things this proxy should be connecting to.</span>
42938-keyword">const</span> socket = net.<span class="hljs-title function_">connect</span>(auth.<span c…
42939 stream.<span class="hljs-title function_">respond</span>();
42940 socket.<span class="hljs-title function_">pipe</span>(stream);
42941 stream.<span class="hljs-title function_">pipe</span>(socket);
42943 ….<span class="hljs-title function_">on</span>(<span class="hljs-string">'error'</span>, <span clas…
42944 …stream.<span class="hljs-title function_">close</span>(http2.<span class="hljs-property">constants…
42948 proxy.<span class="hljs-title function_">listen</span>(<span class="hljs-number">8001</span>);</cod…
42950 …ode class="language-js"><span class="hljs-keyword">const</span> http2 = <span class="hljs-built_in…
42952 <span class="hljs-keyword">const</span> client = http2.<span class="hljs-title function_">connect</…
42954 <span class="hljs-comment">// Must not specify the ':path' and ':scheme' headers</span>
42955 <span class="hljs-comment">// for CONNECT requests or an error will be thrown.</span>
42956 <span class="hljs-keyword">const</span> req = client.<span class="hljs-title function_">request</sp…
42957 <span class="hljs-string">':method'</span>: <span class="hljs-string">'CONNECT'</span>,
42958 …<span class="hljs-string">':authority'</span>: <span class="hljs-string">`localhost:<span class="h…
42961 …span class="hljs-title function_">on</span>(<span class="hljs-string">'response'</span>, <span cla…
42962-variable language_">console</span>.<span class="hljs-title function_">log</span>(headers[http2.<s…
42964 <span class="hljs-keyword">let</span> data = <span class="hljs-string">''</span>;
42965 req.<span class="hljs-title function_">setEncoding</span>(<span class="hljs-string">'utf8'</span>);
42966 ….<span class="hljs-title function_">on</span>(<span class="hljs-string">'data'</span>, <span class…
42967 req.<span class="hljs-title function_">on</span>(<span class="hljs-string">'end'</span>, <span clas…
42968 …hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class=…
42969 client.<span class="hljs-title function_">close</span>();
42971 req.<span class="hljs-title function_">end</span>(<span class="hljs-string">'Jane'</span>);</code><…
42978 …ode class="language-js"><span class="hljs-keyword">const</span> http2 = <span class="hljs-built_in…
42979 <span class="hljs-keyword">const</span> settings = { <span class="hljs-attr">enableConnectProtocol<…
42980 <span class="hljs-keyword">const</span> server = http2.<span class="hljs-title function_">createSer…
42983 <code>':protocol'</code> HTTP/2 pseudo-header:</p>
42984 …ode class="language-js"><span class="hljs-keyword">const</span> http2 = <span class="hljs-built_in…
42985 <span class="hljs-keyword">const</span> client = http2.<span class="hljs-title function_">connect</…
42986 …n class="hljs-title function_">on</span>(<span class="hljs-string">'remoteSettings'</span>, <span …
42987 …<span class="hljs-keyword">if</span> (settings.<span class="hljs-property">enableConnectProtocol</…
42988-keyword">const</span> req = client.<span class="hljs-title function_">request</span>({ <span clas…
42989 <span class="hljs-comment">// ...</span>
43001 …ode class="language-js"><span class="hljs-keyword">const</span> http2 = <span class="hljs-built_in…
43002 …="hljs-keyword">const</span> server = http2.<span class="hljs-title function_">createServer</span>…
43003 …res.<span class="hljs-title function_">setHeader</span>(<span class="hljs-string">'Content-Type'</…
43004 …res.<span class="hljs-title function_">setHeader</span>(<span class="hljs-string">'X-Foo'</span>, …
43005-title function_">writeHead</span>(<span class="hljs-number">200</span>, { <span class="hljs-strin…
43006 res.<span class="hljs-title function_">end</span>(<span class="hljs-string">'ok'</span>);
43010 Upgrading from non-tls HTTP/1 servers is not supported.</p>
43022 …s="language-js"><span class="hljs-keyword">const</span> { createSecureServer } = <span class="hljs
43023 <span class="hljs-keyword">const</span> { readFileSync } = <span class="hljs-built_in">require</spa…
43025 <span class="hljs-keyword">const</span> cert = <span class="hljs-title function_">readFileSync</spa…
43026 <span class="hljs-keyword">const</span> key = <span class="hljs-title function_">readFileSync</span…
43028 <span class="hljs-keyword">const</span> server = <span class="hljs-title function_">createSecureSer…
43029 { cert, key, <span class="hljs-attr">allowHTTP1</span>: <span class="hljs-literal">true</span> },
43031 ).<span class="hljs-title function_">listen</span>(<span class="hljs-number">4443</span>);
43033 <span class="hljs-keyword">function</span> <span class="hljs-title function_">onRequest</span>(<spa…
43034 <span class="hljs-comment">// Detects if it is a HTTPS request or HTTP/2</span>
43035-keyword">const</span> { <span class="hljs-attr">socket</span>: { alpnProtocol } } = req.<span cla…
43036 req.<span class="hljs-property">stream</span>.<span class="hljs-property">session</span> : req;
43037 …"hljs-title function_">writeHead</span>(<span class="hljs-number">200</span>, { <span class="hljs-
43038 …res.<span class="hljs-title function_">end</span>(<span class="hljs-variable constant_">JSON</span…
43040 <span class="hljs-attr">httpVersion</span>: req.<span class="hljs-property">httpVersion</span>
43061 abnormally aborted in mid-communication.</p>
43075 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" …
43084 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" c…
43093 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" …
43101 … api_stability_0"><a href="#documentation_stability_index">Stability: 0</a> - Deprecated. Use <a h…
43111 <li><code>error</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/G…
43122 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Objec…
43125 <p>Key-value pairs of header names and values. Header names are lower-cased.</p>
43126 <pre><code class="language-js"><span class="hljs-comment">// Prints something like:</span>
43127 <span class="hljs-comment">//</span>
43128 <span class="hljs-comment">// { 'user-agent': 'curl/7.22.0',</span>
43129 <span class="hljs-comment">// host: '127.0.0.1:8000',</span>
43130 <span class="hljs-comment">// accept: '*/*' }</span>
43131 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(…
43138 <pre><code class="language-js"><span class="hljs-title function_">removeAllHeaders</span>(request.<…
43139 <span class="hljs-title function_">assert</span>(request.<span class="hljs-property">url</span>); …
43145 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" c…
43148 client response, the HTTP version of the connected-to server. Returns
43157 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" c…
43159 <p>The request method as a string. Read-only. Examples: <code>'GET'</code>, <code>'DELETE'</code>.<…
43165 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" c…
43169 list of tuples. So, the even-numbered offsets are key values, and the
43170 odd-numbered offsets are the associated values.</p>
43172 <pre><code class="language-js"><span class="hljs-comment">// Prints something like:</span>
43173 <span class="hljs-comment">//</span>
43174 <span class="hljs-comment">// [ 'user-agent',</span>
43175 <span class="hljs-comment">// 'this is invalid because there can be only one',</span>
43176 <span class="hljs-comment">// 'User-Agent',</span>
43177 <span class="hljs-comment">// 'curl/7.22.0',</span>
43178 <span class="hljs-comment">// 'Host',</span>
43179 <span class="hljs-comment">// '127.0.0.1:8000',</span>
43180 <span class="hljs-comment">// 'ACCEPT',</span>
43181 <span class="hljs-comment">// '*/*' ]</span>
43182 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(…
43188 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" c…
43197 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" c…
43200 portion of the target URL.</p>
43206 <li><code>msecs</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struct…
43207 <li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Referenc…
43250 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Objec…
43258 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" c…
43262 …ode class="language-http"><span class="hljs-keyword">GET</span> <span class="hljs-string">/status?…
43263 <span class="hljs-attribute">Accept</span><span class="hljs-punctuation">: </span>text/plain</code>…
43265 <!-- eslint-disable semi -->
43266 <pre><code class="language-js"><span class="hljs-string">'/status?name=ryan'</span></code></pre>
43268 <pre><code class="language-console"><span class="hljs-meta">$ </span><span class="language-bash">no…
43269 …an class="hljs-meta">> </span><span class="language-bash">new URL(<span class="hljs-string">'/stat…
43313 <li><code>headers</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference…
43323 … api_stability_0"><a href="#documentation_stability_index">Stability: 0</a> - Deprecated. Use <a h…
43334 <li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Referenc…
43339 <li><code>err</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Glo…
43340 …2serverresponse" class="type">&#x3C;http2.Http2ServerResponse></a> The newly-created <code>Http2Se…
43362-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | <a href="#bu…
43363 <li><code>encoding</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_str…
43364 <li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Referenc…
43365 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/t…
43378 … api_stability_0"><a href="#documentation_stability_index">Stability: 0</a> - Deprecated. Use <a h…
43380 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" …
43389 <li><code>name</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
43390 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Strin…
43393 The name is case-insensitive.</p>
43394-js"><span class="hljs-keyword">const</span> contentType = response.<span class="hljs-title functi…
43400 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Strin…
43404 …lass="language-js">response.<span class="hljs-title function_">setHeader</span>(<span class="hljs-
43405 …hljs-title function_">setHeader</span>(<span class="hljs-string">'Set-Cookie'</span>, [<span class…
43407 <span class="hljs-keyword">const</span> headerNames = response.<span class="hljs-title function_">g…
43408 <span class="hljs-comment">// headerNames === ['foo', 'set-cookie']</span></code></pre>
43414 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Obje…
43418 header-related http module methods. The keys of the returned object are the
43425 …lass="language-js">response.<span class="hljs-title function_">setHeader</span>(<span class="hljs-
43426 …hljs-title function_">setHeader</span>(<span class="hljs-string">'Set-Cookie'</span>, [<span class…
43428 <span class="hljs-keyword">const</span> headers = response.<span class="hljs-title function_">getHe…
43429 <span class="hljs-comment">// headers === { foo: 'bar', 'set-cookie': ['foo=bar', 'bar=baz'] }</spa…
43435 <li><code>name</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
43436 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boole…
43439 outgoing headers. The header name matching is case-insensitive.</p>
43440-js"><span class="hljs-keyword">const</span> hasContentType = response.<span class="hljs-title fun…
43446 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" …
43448 <p>True if headers were sent, false otherwise (read-only).</p>
43454 <li><code>name</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
43457 …re><code class="language-js">response.<span class="hljs-title function_">removeHeader</span>(<span…
43463 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" …
43474 <li><code>name</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
43475 …zilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | …
43478 in the to-be-sent headers, its value will be replaced. Use an array of strings
43480-js">response.<span class="hljs-title function_">setHeader</span>(<span class="hljs-string">'Conte…
43482-js">response.<span class="hljs-title function_">setHeader</span>(<span class="hljs-string">'Set-C…
43488 <pre><code class="language-js"><span class="hljs-comment">// Returns content-type = text/plain</spa…
43489 …="hljs-keyword">const</span> server = http2.<span class="hljs-title function_">createServer</span>…
43490 …ass="hljs-title function_">setHeader</span>(<span class="hljs-string">'Content-Type'</span>, <span…
43491 …res.<span class="hljs-title function_">setHeader</span>(<span class="hljs-string">'X-Foo'</span>, …
43492-title function_">writeHead</span>(<span class="hljs-number">200</span>, { <span class="hljs-strin…
43493 res.<span class="hljs-title function_">end</span>(<span class="hljs-string">'ok'</span>);
43500 <li><code>msecs</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struct…
43501 <li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Referenc…
43529 …ode class="language-js"><span class="hljs-keyword">const</span> http2 = <span class="hljs-built_in…
43530 …="hljs-keyword">const</span> server = http2.<span class="hljs-title function_">createServer</span>…
43531 …<span class="hljs-keyword">const</span> ip = req.<span class="hljs-property">socket</span>.<span c…
43532 …<span class="hljs-keyword">const</span> port = req.<span class="hljs-property">socket</span>.<span…
43533-title function_">end</span>(<span class="hljs-string">`Your IP address is <span class="hljs-subst…
43534 }).<span class="hljs-title function_">listen</span>(<span class="hljs-number">3000</span>);</code><…
43540 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" c…
43545 <pre><code class="language-js">response.<span class="hljs-property">statusCode</span> = <span class…
43553 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" c…
43570 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" …
43580-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | <a href="#bu…
43581 <li><code>encoding</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_str…
43582 <li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Referenc…
43583 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boole…
43596 <p>This is the raw HTTP body and has nothing to do with higher-level multi-part
43626 <li><code>statusCode</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_s…
43627 <li><code>statusMessage</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Dat…
43628 <li><code>headers</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference…
43631 <p>Sends a response header to the request. The status code is a 3-digit HTTP
43634 <p>For compatibility with <a href="http.html">HTTP/1</a>, a human-readable <code>statusMessage</cod…
43638 <pre><code class="language-js"><span class="hljs-keyword">const</span> body = <span class="hljs-str…
43639 response.<span class="hljs-title function_">writeHead</span>(<span class="hljs-number">200</span>, {
43640 …<span class="hljs-string">'Content-Length'</span>: <span class="hljs-title class_">Buffer</span>.<…
43641 …<span class="hljs-string">'Content-Type'</span>: <span class="hljs-string">'text/plain; charset=ut…
43643 <p><code>Content-Length</code> is given in bytes not characters. The
43645 given encoding. On outbound messages, Node.js does not check if Content-Length
43648 <code>Content-Length</code> does not match the actual payload size.</p>
43656 <pre><code class="language-js"><span class="hljs-comment">// Returns content-type = text/plain</spa…
43657 …="hljs-keyword">const</span> server = http2.<span class="hljs-title function_">createServer</span>…
43658 …ass="hljs-title function_">setHeader</span>(<span class="hljs-string">'Content-Type'</span>, <span…
43659 …res.<span class="hljs-title function_">setHeader</span>(<span class="hljs-string">'X-Foo'</span>, …
43660-title function_">writeHead</span>(<span class="hljs-number">200</span>, { <span class="hljs-strin…
43661 res.<span class="hljs-title function_">end</span>(<span class="hljs-string">'ok'</span>);
43668-js"><span class="hljs-keyword">const</span> { <span class="hljs-title class_">PerformanceObserver…
43670-keyword">const</span> obs = <span class="hljs-keyword">new</span> <span class="hljs-title class_"…
43671 …<span class="hljs-keyword">const</span> entry = items.<span class="hljs-title function_">getEntrie…
43672-variable language_">console</span>.<span class="hljs-title function_">log</span>(entry.<span clas…
43673 …<span class="hljs-keyword">if</span> (entry.<span class="hljs-property">name</span> === <span clas…
43674 <span class="hljs-comment">// Entry contains statistics about the Http2Session</span>
43675 … class="hljs-keyword">else</span> <span class="hljs-keyword">if</span> (entry.<span class="hljs-pr…
43676 <span class="hljs-comment">// Entry contains statistics about the Http2Stream</span>
43679 obs.<span class="hljs-title function_">observe</span>({ <span class="hljs-attr">entryTypes</span>: …
43686 <li><code>bytesRead</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_st…
43688 <li><code>bytesWritten</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data…
43690 <li><code>id</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structure…
43691 <li><code>timeToFirstByte</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/D…
43693 <li><code>timeToFirstByteSent</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScri…
43695 <li><code>timeToFirstHeader</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript…
43701 <li><code>bytesRead</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_st…
43702 <li><code>bytesWritten</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data…
43703 <li><code>framesReceived</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Da…
43705 <li><code>framesSent</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_s…
43706 <li><code>maxConcurrentStreams</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScr…
43708 <li><code>pingRTT</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_stru…
43711 <li><code>streamAverageDuration</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaSc…
43713 <li><code>streamCount</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_…
43715 <li><code>type</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
43721 …ty api_stability_2"><a href="#documentation_stability_index">Stability: 2</a> - Stable</div><p></p>
43747 <td><p>do not automatically set servername if the target host was specified using an IP address.</p…
43752 <li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference…
43756 <p><code>maxCachedSessions</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/…
43760 <p><code>servername</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_st…
43763 <strong>Default:</strong> host name of the target server, unless the target server
43787 <pre><code class="language-js"><span class="hljs-comment">// ...</span>
43788-property">globalAgent</span>.<span class="hljs-title function_">on</span>(<span class="hljs-strin…
43789-title function_">appendFileSync</span>(<span class="hljs-string">'/tmp/ssl-keys.log'</span>, line…
43804 <li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Referenc…
43813 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" c…
43821 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" c…
43829 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" c…
43837 <li><code>msecs</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struct…
43838 <li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Referenc…
43855 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" c…
43863 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" c…
43871 <li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference…
43873 <li><code>requestListener</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/R…
43876 <pre><code class="language-js"><span class="hljs-comment">// curl -k https://localhost:8000/</span>
43877 <span class="hljs-keyword">const</span> https = <span class="hljs-built_in">require</span>(<span cl…
43878 <span class="hljs-keyword">const</span> fs = <span class="hljs-built_in">require</span>(<span class…
43880 <span class="hljs-keyword">const</span> options = {
43881 …class="hljs-attr">key</span>: fs.<span class="hljs-title function_">readFileSync</span>(<span clas…
43882 …class="hljs-attr">cert</span>: fs.<span class="hljs-title function_">readFileSync</span>(<span cla…
43885 https.<span class="hljs-title function_">createServer</span>(options, <span class="hljs-function">(…
43886 res.<span class="hljs-title function_">writeHead</span>(<span class="hljs-number">200</span>);
43887 …res.<span class="hljs-title function_">end</span>(<span class="hljs-string">'hello world\n'</span>…
43888 }).<span class="hljs-title function_">listen</span>(<span class="hljs-number">8000</span>);</code><…
43890 …ode class="language-js"><span class="hljs-keyword">const</span> https = <span class="hljs-built_in…
43891 <span class="hljs-keyword">const</span> fs = <span class="hljs-built_in">require</span>(<span class…
43893 <span class="hljs-keyword">const</span> options = {
43894 …<span class="hljs-attr">pfx</span>: fs.<span class="hljs-title function_">readFileSync</span>(<spa…
43895 <span class="hljs-attr">passphrase</span>: <span class="hljs-string">'sample'</span>
43898 https.<span class="hljs-title function_">createServer</span>(options, <span class="hljs-function">(…
43899 res.<span class="hljs-title function_">writeHead</span>(<span class="hljs-number">200</span>);
43900 …res.<span class="hljs-title function_">end</span>(<span class="hljs-string">'hello world\n'</span>…
43901 }).<span class="hljs-title function_">listen</span>(<span class="hljs-number">8000</span>);</code><…
43918 <li><code>url</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structur…
43919 …lla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type">&#x3C;Object></a> …
43921 <li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Referenc…
43927 …ode class="language-js"><span class="hljs-keyword">const</span> https = <span class="hljs-built_in…
43929 …"hljs-title function_">get</span>(<span class="hljs-string">'https://encrypted.google.com/'</span>…
43930-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hlj…
43931 …js-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="h…
43933 ….<span class="hljs-title function_">on</span>(<span class="hljs-string">'data'</span>, <span class…
43934 …process.<span class="hljs-property">stdout</span>.<span class="hljs-title function_">write</span>(…
43937 ….<span class="hljs-title function_">on</span>(<span class="hljs-string">'error'</span>, <span clas…
43938 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">error</spa…
43967 <li><code>url</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structur…
43968 …lla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type">&#x3C;Object></a> …
43976 <li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Referenc…
43991 …ode class="language-js"><span class="hljs-keyword">const</span> https = <span class="hljs-built_in…
43993 <span class="hljs-keyword">const</span> options = {
43994 <span class="hljs-attr">hostname</span>: <span class="hljs-string">'encrypted.google.com'</span>,
43995 <span class="hljs-attr">port</span>: <span class="hljs-number">443</span>,
43996 <span class="hljs-attr">path</span>: <span class="hljs-string">'/'</span>,
43997 <span class="hljs-attr">method</span>: <span class="hljs-string">'GET'</span>
44000 …="hljs-keyword">const</span> req = https.<span class="hljs-title function_">request</span>(options…
44001-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hlj…
44002 …js-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="h…
44004 ….<span class="hljs-title function_">on</span>(<span class="hljs-string">'data'</span>, <span class…
44005 …process.<span class="hljs-property">stdout</span>.<span class="hljs-title function_">write</span>(…
44009 ….<span class="hljs-title function_">on</span>(<span class="hljs-string">'error'</span>, <span clas…
44010 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">error</spa…
44012 req.<span class="hljs-title function_">end</span>();</code></pre>
44014 <pre><code class="language-js"><span class="hljs-keyword">const</span> options = {
44015 <span class="hljs-attr">hostname</span>: <span class="hljs-string">'encrypted.google.com'</span>,
44016 <span class="hljs-attr">port</span>: <span class="hljs-number">443</span>,
44017 <span class="hljs-attr">path</span>: <span class="hljs-string">'/'</span>,
44018 <span class="hljs-attr">method</span>: <span class="hljs-string">'GET'</span>,
44019 …class="hljs-attr">key</span>: fs.<span class="hljs-title function_">readFileSync</span>(<span clas…
44020 …class="hljs-attr">cert</span>: fs.<span class="hljs-title function_">readFileSync</span>(<span cla…
44022 options.<span class="hljs-property">agent</span> = <span class="hljs-keyword">new</span> https.<spa…
44024 …="hljs-keyword">const</span> req = https.<span class="hljs-title function_">request</span>(options…
44025 <span class="hljs-comment">// ...</span>
44028 <pre><code class="language-js"><span class="hljs-keyword">const</span> options = {
44029 <span class="hljs-attr">hostname</span>: <span class="hljs-string">'encrypted.google.com'</span>,
44030 <span class="hljs-attr">port</span>: <span class="hljs-number">443</span>,
44031 <span class="hljs-attr">path</span>: <span class="hljs-string">'/'</span>,
44032 <span class="hljs-attr">method</span>: <span class="hljs-string">'GET'</span>,
44033 …class="hljs-attr">key</span>: fs.<span class="hljs-title function_">readFileSync</span>(<span clas…
44034 …class="hljs-attr">cert</span>: fs.<span class="hljs-title function_">readFileSync</span>(<span cla…
44035 <span class="hljs-attr">agent</span>: <span class="hljs-literal">false</span>
44038 …="hljs-keyword">const</span> req = https.<span class="hljs-title function_">request</span>(options…
44039 <span class="hljs-comment">// ...</span>
44042-js"><span class="hljs-keyword">const</span> options = <span class="hljs-keyword">new</span> <span…
44044 …="hljs-keyword">const</span> req = https.<span class="hljs-title function_">request</span>(options…
44045 <span class="hljs-comment">// ...</span>
44048 <code>pin-sha256</code>):</p>
44049 …code class="language-js"><span class="hljs-keyword">const</span> tls = <span class="hljs-built_in"…
44050 <span class="hljs-keyword">const</span> https = <span class="hljs-built_in">require</span>(<span cl…
44051 <span class="hljs-keyword">const</span> crypto = <span class="hljs-built_in">require</span>(<span c…
44053 <span class="hljs-keyword">function</span> <span class="hljs-title function_">sha256</span>(<span c…
44054-keyword">return</span> crypto.<span class="hljs-title function_">createHash</span>(<span class="h…
44056 <span class="hljs-keyword">const</span> options = {
44057 <span class="hljs-attr">hostname</span>: <span class="hljs-string">'github.com'</span>,
44058 <span class="hljs-attr">port</span>: <span class="hljs-number">443</span>,
44059 <span class="hljs-attr">path</span>: <span class="hljs-string">'/'</span>,
44060 <span class="hljs-attr">method</span>: <span class="hljs-string">'GET'</span>,
44061 …<span class="hljs-attr">checkServerIdentity</span>: <span class="hljs-keyword">function</span>(<sp…
44062 …<span class="hljs-comment">// Make sure the certificate is issued to the host we are connected to<…
44063 …<span class="hljs-keyword">const</span> err = tls.<span class="hljs-title function_">checkServerId…
44064 <span class="hljs-keyword">if</span> (err) {
44065 <span class="hljs-keyword">return</span> err;
44068 <span class="hljs-comment">// Pin the public key, similar to HPKP pin-sha25 pinning</span>
44069 …<span class="hljs-keyword">const</span> pubkey256 = <span class="hljs-string">'pL1+qb9HTMRZJmuC/bB…
44070 …<span class="hljs-keyword">if</span> (<span class="hljs-title function_">sha256</span>(cert.<span …
44071 …<span class="hljs-keyword">const</span> msg = <span class="hljs-string">'Certificate verification …
44072 …<span class="hljs-string">`The public key of '<span class="hljs-subst">${cert.subject.CN}</span>' …
44073 <span class="hljs-string">'does not match our pinned fingerprint'</span>;
44074 …<span class="hljs-keyword">return</span> <span class="hljs-keyword">new</span> <span class="hljs-t…
44077 <span class="hljs-comment">// Pin the exact certificate, rather than the pub key</span>
44078 …<span class="hljs-keyword">const</span> cert256 = <span class="hljs-string">'25:FE:39:32:D9:63:8C:…
44079 <span class="hljs-string">'D8:3E:4C:1D:98:DB:71:E4:1A:48:03:98:EA:22:6A:BD:8B:93:16'</span>;
44080 …<span class="hljs-keyword">if</span> (cert.<span class="hljs-property">fingerprint256</span> !== c…
44081 …<span class="hljs-keyword">const</span> msg = <span class="hljs-string">'Certificate verification …
44082 …<span class="hljs-string">`The certificate of '<span class="hljs-subst">${cert.subject.CN}</span>'…
44083 <span class="hljs-string">'does not match our pinned fingerprint'</span>;
44084 …<span class="hljs-keyword">return</span> <span class="hljs-keyword">new</span> <span class="hljs-t…
44087 <span class="hljs-comment">// This loop is informational only.</span>
44088 …<span class="hljs-comment">// Print the certificate and public key fingerprints of all certs in th…
44089 …<span class="hljs-comment">// chain. Its common to pin the public key of the issuer on the public<…
44090 …<span class="hljs-comment">// internet, while pinning the public key of the service in sensitive</…
44091 <span class="hljs-comment">// environments.</span>
44092 <span class="hljs-keyword">do</span> {
44093-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hlj…
44094-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hlj…
44096 …hash = crypto.<span class="hljs-title function_">createHash</span>(<span class="hljs-string">'sha2…
44097-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hlj…
44099 lastprint256 = cert.<span class="hljs-property">fingerprint256</span>;
44100 cert = cert.<span class="hljs-property">issuerCertificate</span>;
44101 …} <span class="hljs-keyword">while</span> (cert.<span class="hljs-property">fingerprint256</span> …
44106 options.<span class="hljs-property">agent</span> = <span class="hljs-keyword">new</span> https.<spa…
44107 …="hljs-keyword">const</span> req = https.<span class="hljs-title function_">request</span>(options…
44108 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>…
44109-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hlj…
44110 <span class="hljs-comment">// Print the HPKP values</span>
44111-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hlj…
44113 ….<span class="hljs-title function_">on</span>(<span class="hljs-string">'data'</span>, <span class…
44116 ….<span class="hljs-title function_">on</span>(<span class="hljs-string">'error'</span>, <span clas…
44117 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">error</spa…
44119 req.<span class="hljs-title function_">end</span>();</code></pre>
44121 <pre><code class="language-text">Subject Common Name: github.com
44123 Public key ping-sha256: pL1+qb9HTMRZJmuC/bB/ZI9d302BYrrqiVuRyW+DGrU=
44126 Public key ping-sha256: RRM1dGqnDFsCJXBTHky16vi1obOlCgFFn/yOhI/y+ho=
44129 Public key ping-sha256: WoiWRyIOVNa9ihaBciRSC7XHjliYS9VwUGOIud4PB18=
44132-age=0; pin-sha256="WoiWRyIOVNa9ihaBciRSC7XHjliYS9VwUGOIud4PB18="; pin-sha256="RRM1dGqnDFsCJXBTHky…
44136 …ty api_stability_2"><a href="#documentation_stability_index">Stability: 2</a> - Stable</div><p></p>
44140 …e class="language-js"><span class="hljs-keyword">const</span> inspector = <span class="hljs-built_…
44145 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Objec…
44147-js"><span class="hljs-built_in">require</span>(<span class="hljs-string">'inspector'</span>).<spa…
44152 <li><code>port</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
44154 <li><code>host</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
44156 <li><code>wait</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
44159 <p>Activate inspector on host and port. Equivalent to <code>node --inspect=[[host:]port]</code>, bu…
44167 …zilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | …
44170 …e><code class="language-console"><span class="hljs-meta">$ </span><span class="language-bash">node…
44171 Debugger listening on ws://127.0.0.1:9229/166e272e-7a30-4d09-97ce-f1c012b43c34
44173 ws://127.0.0.1:9229/166e272e-7a30-4d09-97ce-f1c012b43c34
44174 <span class="hljs-meta">
44175 $ </span><span class="language-bash">node --inspect=localhost:3000 -p <span class="hljs-string">'in…
44176 Debugger listening on ws://localhost:3000/51cf8d0e-3c36-4c59-8efd-54519839e56a
44178 ws://localhost:3000/51cf8d0e-3c36-4c59-8efd-54519839e56a
44179 <span class="hljs-meta">
44180 $ </span><span class="language-bash">node -p <span class="hljs-string">'inspector.url()'</span></sp…
44194 back-end and receiving message responses and notifications.</p>
44207 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Objec…
44210-js">session.<span class="hljs-title function_">on</span>(<span class="hljs-string">'inspectorNoti…
44211 <span class="hljs-comment">// Debugger.paused</span>
44212 <span class="hljs-comment">// Debugger.resumed</span></code></pre>
44214 <h4>Event: <code>&#x3C;inspector-protocol-method></code>;<span><a class="mark" href="#inspector_eve…
44219 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Objec…
44222 to the <code>&#x3C;inspector-protocol-method></code> value.</p>
44223 …ener on the <a href="https://chromedevtools.github.io/devtools-protocol/v8/Debugger#event-paused">…
44226-js">session.<span class="hljs-title function_">on</span>(<span class="hljs-string">'Debugger.paus…
44227 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>…
44229 <span class="hljs-comment">// [ '/the/file/that/has/the/breakpoint.js:11:0' ]</span></code></pre>
44234 <p>Connects a session to the inspector back-end.</p>
44239 <p>Connects a session to the main thread inspector back-end. An exception will
44254 <li><code>method</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struc…
44255 <li><code>params</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/…
44256 <li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Referenc…
44258 <p>Posts a message to the inspector back-end. <code>callback</code> will be notified when
44260 arguments: error and message-specific result.</p>
44261-js">session.<span class="hljs-title function_">post</span>(<span class="hljs-string">'Runtime.eva…
44262 …hljs-function">(<span class="hljs-params">error, { result }</span>) =></span> <span class="hljs-va…
44263 <span class="hljs-comment">// Output: { type: 'number', value: 4, description: '4' }</span></code><…
44265 <a href="https://chromedevtools.github.io/devtools-protocol/v8/">Chrome DevTools Protocol Viewer</a…
44269 to the run-time events.</p>
44274 <p>Here's an example showing how to use the <a href="https://chromedevtools.github.io/devtools-prot…
44275 …e class="language-js"><span class="hljs-keyword">const</span> inspector = <span class="hljs-built_…
44276 <span class="hljs-keyword">const</span> fs = <span class="hljs-built_in">require</span>(<span class…
44277 <span class="hljs-keyword">const</span> session = <span class="hljs-keyword">new</span> inspector.<…
44278 session.<span class="hljs-title function_">connect</span>();
44280 session.<span class="hljs-title function_">post</span>(<span class="hljs-string">'Profiler.enable'<…
44281 …session.<span class="hljs-title function_">post</span>(<span class="hljs-string">'Profiler.start'<…
44282 <span class="hljs-comment">// Invoke business logic under measurement here...</span>
44284 <span class="hljs-comment">// some time later...</span>
44285 …n class="hljs-title function_">post</span>(<span class="hljs-string">'Profiler.stop'</span>, <span…
44286 <span class="hljs-comment">// Write profile to disk, upload, etc.</span>
44287 <span class="hljs-keyword">if</span> (!err) {
44288-title function_">writeFileSync</span>(<span class="hljs-string">'./profile.cpuprofile'</span>, <s…
44294 <p>Here's an example showing how to use the <a href="https://chromedevtools.github.io/devtools-prot…
44295 …e class="language-js"><span class="hljs-keyword">const</span> inspector = <span class="hljs-built_…
44296 <span class="hljs-keyword">const</span> fs = <span class="hljs-built_in">require</span>(<span class…
44297 <span class="hljs-keyword">const</span> session = <span class="hljs-keyword">new</span> inspector.<…
44299-keyword">const</span> fd = fs.<span class="hljs-title function_">openSync</span>(<span class="hlj…
44301 session.<span class="hljs-title function_">connect</span>();
44303 …hljs-title function_">on</span>(<span class="hljs-string">'HeapProfiler.addHeapSnapshotChunk'</spa…
44304 …fs.<span class="hljs-title function_">writeSync</span>(fd, m.<span class="hljs-property">params</s…
44307-title function_">post</span>(<span class="hljs-string">'HeapProfiler.takeHeapSnapshot'</span>, <s…
44308 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>…
44309 session.<span class="hljs-title function_">disconnect</span>();
44310 fs.<span class="hljs-title function_">closeSync</span>(fd);
44319 <li>Locale-sensitive or Unicode-aware functions in the <a href="https://tc39.github.io/ecma262/">EC…
44322 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Strin…
44323 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Strin…
44324 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Strin…
44328 Specification</a> (aka ECMA-402):
44330 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl"…
44331 <li>Locale-sensitive methods like <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/…
44332 <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/toLo…
44339 <li><a href="https://github.com/tc39/proposal-regexp-unicode-property-escapes"><code>RegExp</code> …
44342 <a href="http://site.icu-project.org/">International Components for Unicode (ICU)</a> to implement …
44352 <li><code>--with-intl=none</code>/<code>--without-intl</code></li>
44353 <li><code>--with-intl=system-icu</code></li>
44354 <li><code>--with-intl=small-icu</code></li>
44355 <li><code>--with-intl=full-icu</code> (default)</li>
44455-icu</code></th><th><code>small-icu</code></th><th><code>full-icu</code></th></tr></thead><tbody><…
44456 <p>The "(not locale-aware)" designation denotes that the function carries out its
44457 operation just like the non-<code>Locale</code> version of the function, if one
44463 <h4>Build with a pre-installed ICU (<code>system-icu</code>)<span><a class="mark" href="#intl_build…
44469 <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/no…
44470 supported under <code>system-icu</code>. Features that require ICU locale data in
44471 addition, such as <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global…
44474 <h4>Embed a limited set of ICU data (<code>small-icu</code>)<span><a class="mark" href="#intl_embed…
44479 <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/no…
44480 supported under <code>small-icu</code>. Features that require ICU locale data in addition,
44481 such as <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/D…
44482-js"><span class="hljs-keyword">const</span> january = <span class="hljs-keyword">new</span> <span…
44483-keyword">const</span> english = <span class="hljs-keyword">new</span> <span class="hljs-title cla…
44484-keyword">const</span> spanish = <span class="hljs-keyword">new</span> <span class="hljs-title cla…
44486 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(…
44487 <span class="hljs-comment">// Prints "January"</span>
44488 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(…
44489 <span class="hljs-comment">// Prints "M01" on small-icu</span>
44490 <span class="hljs-comment">// Should print "enero"</span></code></pre>
44493 <p>If the <code>small-icu</code> option is used, one can still provide additional locale data
44500 <pre><code class="language-bash">env NODE_ICU_DATA=/some/directory node</code></pre>
44503 <p>The <a href="#cli_icu_data_dir_file"><code>--icu-data-dir</code></a> CLI parameter:</p>
44504 <pre><code class="language-bash">node --icu-data-dir=/some/directory</code></pre>
44507 <p>(If both are specified, the <code>--icu-data-dir</code> CLI parameter takes precedence.)</p>
44512 Check <a href="http://userguide.icu-project.org/icudata">"ICU Data"</a> article in the ICU User Gui…
44514 <p>The <a href="https://www.npmjs.com/package/full-icu">full-icu</a> npm module can greatly simplif…
44516 appropriate data file. After installing the module through <code>npm i full-icu</code>,
44517 the data file will be available at <code>./node_modules/full-icu</code>. This path can be
44518 then passed either to <code>NODE_ICU_DATA</code> or <code>--icu-data-dir</code> as shown above to
44520 <h4>Embed the entire ICU (<code>full-icu</code>)<span><a class="mark" href="#intl_embed_the_entire_…
44524 the default behavior if no <code>--with-intl</code> flag is passed. The official binaries
44527 <p>To verify that ICU is enabled at all (<code>system-icu</code>, <code>small-icu</code>, or
44528 <code>full-icu</code>), simply checking the existence of <code>Intl</code> should suffice:</p>
44529-js"><span class="hljs-keyword">const</span> hasICU = <span class="hljs-keyword">typeof</span> <sp…
44532-js"><span class="hljs-keyword">const</span> hasICU = <span class="hljs-keyword">typeof</span> pro…
44533 <p>To check for support for a non-English locale (i.e. <code>full-icu</code> or
44534 <code>system-icu</code>), <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Referenc…
44535 <pre><code class="language-js"><span class="hljs-keyword">const</span> hasFullICU = (<span class="h…
44536 <span class="hljs-keyword">try</span> {
44537 …lass="hljs-keyword">const</span> january = <span class="hljs-keyword">new</span> <span class="hljs
44538-keyword">const</span> spanish = <span class="hljs-keyword">new</span> <span class="hljs-title cla…
44539 …<span class="hljs-keyword">return</span> spanish.<span class="hljs-title function_">format</span>(…
44540 } <span class="hljs-keyword">catch</span> (err) {
44541 <span class="hljs-keyword">return</span> <span class="hljs-literal">false</span>;
44550 dedicated to ECMA-402.</li>
44555 …ty api_stability_2"><a href="#documentation_stability_index">Stability: 2</a> - Stable</div><p></p>
44559 …ode class="language-js"><span class="hljs-keyword">const</span> circle = <span class="hljs-built_i…
44560-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hlj…
44564 …de class="language-js"><span class="hljs-keyword">const</span> { <span class="hljs-variable consta…
44566-built_in">exports</span>.<span class="hljs-property">area</span> = <span class="hljs-function">(<…
44568-built_in">exports</span>.<span class="hljs-property">circumference</span> = <span class="hljs-fun…
44578-js"><span class="hljs-keyword">const</span> <span class="hljs-title class_">Square</span> = <span…
44579 …ss="hljs-keyword">const</span> mySquare = <span class="hljs-keyword">new</span> <span class="hljs-
44580 …s-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hl…
44582 <pre><code class="language-js"><span class="hljs-comment">// Assigning to exports will not modify m…
44583 …s="hljs-variable language_">module</span>.<span class="hljs-property">exports</span> = <span class…
44584 <span class="hljs-title function_">constructor</span>(<span class="hljs-params">width</span>) {
44585 …<span class="hljs-variable language_">this</span>.<span class="hljs-property">width</span> = width;
44588 <span class="hljs-title function_">area</span>(<span class="hljs-params"></span>) {
44589 …ss="hljs-keyword">return</span> <span class="hljs-variable language_">this</span>.<span class="hlj…
44611 <code>/usr/lib/node/&#x3C;some-package>/&#x3C;some-version></code> hold the contents of a
44655 <p>Putting together all of the above, here is the high-level algorithm
44704 2. let I = count of PARTS - 1
44710 d. let I = I - 1
44718 …["node", "require"]) <a href="esm.md#resolver-algorithm-specification">defined in the ESM resolver…
44729 …`package.json` "exports", ["node", "require"]) <a href="esm.md#resolver-algorithm-specification">d…
44739 <a href="esm.md#resolver-algorithm-specification">defined in the ESM resolver</a>.
44770 <p>Additionally, on case-insensitive file systems or operating systems, different
44803 …class="language-js"><span class="hljs-variable language_">console</span>.<span class="hljs-title f…
44804 <span class="hljs-built_in">exports</span>.<span class="hljs-property">done</span> = <span class="h…
44805 <span class="hljs-keyword">const</span> b = <span class="hljs-built_in">require</span>(<span class=…
44806-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hlj…
44807 <span class="hljs-built_in">exports</span>.<span class="hljs-property">done</span> = <span class="h…
44808 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(…
44810 …class="language-js"><span class="hljs-variable language_">console</span>.<span class="hljs-title f…
44811 <span class="hljs-built_in">exports</span>.<span class="hljs-property">done</span> = <span class="h…
44812 <span class="hljs-keyword">const</span> a = <span class="hljs-built_in">require</span>(<span class=…
44813-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hlj…
44814 <span class="hljs-built_in">exports</span>.<span class="hljs-property">done</span> = <span class="h…
44815 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(…
44817 …class="language-js"><span class="hljs-variable language_">console</span>.<span class="hljs-title f…
44818 <span class="hljs-keyword">const</span> a = <span class="hljs-built_in">require</span>(<span class=…
44819 <span class="hljs-keyword">const</span> b = <span class="hljs-built_in">require</span>(<span class=…
44820-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hlj…
44828 <pre><code class="language-console"><span class="hljs-meta">$ </span><span class="language-bash">no…
44859 <p>It is convenient to organize programs and libraries into self-contained
44866-json"><span class="hljs-punctuation">{</span> <span class="hljs-attr">"name"</span> <span class="…
44867 …ss="hljs-attr">"main"</span> <span class="hljs-punctuation">:</span> <span class="hljs-string">"./…
44868 <p>If this was in a folder at <code>./some-library</code>, then
44869 <code>require('./some-library')</code> would attempt to load
44870 <code>./some-library/lib/some-library.js</code>.</p>
44876 example, then <code>require('./some-library')</code> would attempt to load:</p>
44878 <li><code>./some-library/index.js</code></li>
44879 <li><code>./some-library/index.node</code></li>
44883 <pre><code class="language-console">Error: Cannot find module 'some-library'</code></pre>
44907 <code>require('example-module/path/to/file')</code> would resolve <code>path/to/file</code>
44908 relative to where <code>example-module</code> is located. The suffixed path follows the
44912 <p>If the <code>NODE_PATH</code> environment variable is set to a colon-delimited list
44939-js">(<span class="hljs-keyword">function</span>(<span class="hljs-params"><span class="hljs-built…
44940 <span class="hljs-comment">// Module code actually lives in here</span>
44944 <li>It keeps top-level variables (defined with <code>var</code>, <code>const</code> or <code>let</c…
44946 <li>It helps to provide some global-looking variables that are actually specific
44963 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" c…
44968 <pre><code class="language-js"><span class="hljs-variable language_">console</span>.<span class="hl…
44969 <span class="hljs-comment">// Prints: /Users/mjr</span>
44970 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(…
44971 <span class="hljs-comment">// Prints: /Users/mjr</span></code></pre>
44978 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" c…
44987 <pre><code class="language-js"><span class="hljs-variable language_">console</span>.<span class="hl…
44988 <span class="hljs-comment">// Prints: /Users/mjr/example.js</span>
44989 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(…
44990 <span class="hljs-comment">// Prints: /Users/mjr</span></code></pre>
45006 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Objec…
45028 <li><code>id</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structure…
45029 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Data_…
45038 <pre><code class="language-js"><span class="hljs-comment">// Importing a local module with a path r…
45039 <span class="hljs-comment">// working directory. (On Windows, this would resolve to .\path\myLocalM…
45040 <span class="hljs-keyword">const</span> myLocalModule = <span class="hljs-built_in">require</span>(…
45042 <span class="hljs-comment">// Importing a JSON file:</span>
45043 <span class="hljs-keyword">const</span> jsonData = <span class="hljs-built_in">require</span>(<span…
45045 <span class="hljs-comment">// Importing a module from node_modules or Node.js built-in module:</spa…
45046 <span class="hljs-keyword">const</span> crypto = <span class="hljs-built_in">require</span>(<span c…
45052 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Objec…
45060 only <code>node:</code>-prefixed require calls are going to receive the native module.
45062 <!-- eslint-disable node-core/no-duplicate-requires -->
45063 …ode class="language-js"><span class="hljs-keyword">const</span> assert = <span class="hljs-built_i…
45064 <span class="hljs-keyword">const</span> realFs = <span class="hljs-built_in">require</span>(<span c…
45066 <span class="hljs-keyword">const</span> fakeFs = {};
45067 …n class="hljs-built_in">require</span>.<span class="hljs-property">cache</span>.<span class="hljs-
45069 assert.<span class="hljs-title function_">strictEqual</span>(<span class="hljs-built_in">require</s…
45070 assert.<span class="hljs-title function_">strictEqual</span>(<span class="hljs-built_in">require</s…
45075 … api_stability_0"><a href="#documentation_stability_index">Stability: 0</a> - Deprecated</div><p><…
45077 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Objec…
45081-js"><span class="hljs-built_in">require</span>.<span class="hljs-property">extensions</span>[<spa…
45082 <p><strong>Deprecated.</strong> In the past, this list has been used to load non-JavaScript
45083 modules into Node.js by compiling them on-demand. However, in practice, there
45099-js"><span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</…
45100 <pre><code class="language-bash">node entry.js</code></pre>
45101 <!-- eslint-skip -->
45102 <pre><code class="language-js"><span class="hljs-title class_">Module</span> {
45103 <span class="hljs-attr">id</span>: <span class="hljs-string">'.'</span>,
45104 <span class="hljs-attr">path</span>: <span class="hljs-string">'/absolute/path/to'</span>,
45105 <span class="hljs-attr">exports</span>: {},
45106 <span class="hljs-attr">parent</span>: <span class="hljs-literal">null</span>,
45107 …<span class="hljs-attr">filename</span>: <span class="hljs-string">'/absolute/path/to/entry.js'</s…
45108 <span class="hljs-attr">loaded</span>: <span class="hljs-literal">false</span>,
45109 <span class="hljs-attr">children</span>: [],
45110 <span class="hljs-attr">paths</span>:
45111 [ <span class="hljs-string">'/absolute/path/to/node_modules'</span>,
45112 <span class="hljs-string">'/absolute/path/node_modules'</span>,
45113 <span class="hljs-string">'/absolute/node_modules'</span>,
45114 <span class="hljs-string">'/node_modules'</span> ] }</code></pre>
45128 <li><code>request</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_stru…
45129 <li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference…
45131 <li><code>paths</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struct…
45139 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Strin…
45149 <li><code>request</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_stru…
45150 …illa.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string[]></a> |…
45162 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Objec…
45166 also accessible via the <code>exports</code> module-global. <code>module</code> is not actually
45181 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Objec…
45189-js"><span class="hljs-keyword">const</span> <span class="hljs-title class_">EventEmitter</span> =…
45191 …ss="hljs-variable language_">module</span>.<span class="hljs-property">exports</span> = <span clas…
45193 <span class="hljs-comment">// Do some work, and after some time emit</span>
45194 <span class="hljs-comment">// the 'ready' event from the module itself.</span>
45195 <span class="hljs-built_in">setTimeout</span>(<span class="hljs-function">() =></span> {
45196 …"hljs-variable language_">module</span>.<span class="hljs-property">exports</span>.<span class="hl…
45197 }, <span class="hljs-number">1000</span>);</code></pre>
45199 …<code class="language-js"><span class="hljs-keyword">const</span> a = <span class="hljs-built_in">…
45200 a.<span class="hljs-title function_">on</span>(<span class="hljs-string">'ready'</span>, <span clas…
45201 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>…
45206 <pre><code class="language-js"><span class="hljs-built_in">setTimeout</span>(<span class="hljs-func…
45207 …ass="hljs-variable language_">module</span>.<span class="hljs-property">exports</span> = { <span c…
45208 }, <span class="hljs-number">0</span>);</code></pre>
45210 …<code class="language-js"><span class="hljs-keyword">const</span> x = <span class="hljs-built_in">…
45211 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(…
45216 <p>The <code>exports</code> variable is available within a module's file-level scope, and is
45221-js"><span class="hljs-variable language_">module</span>.<span class="hljs-property">exports</span…
45222 …class="hljs-built_in">exports</span> = { <span class="hljs-attr">hello</span>: <span class="hljs-l…
45225 <!-- eslint-disable func-name-matching -->
45226-js"><span class="hljs-variable language_">module</span>.<span class="hljs-property">exports</span…
45227 <span class="hljs-comment">// ... etc.</span>
45231 …guage-js"><span class="hljs-keyword">function</span> <span class="hljs-title function_">require</s…
45232 …<span class="hljs-keyword">const</span> <span class="hljs-variable language_">module</span> = { <s…
45233 …(<span class="hljs-function">(<span class="hljs-params"><span class="hljs-variable language_">modu…
45234 <span class="hljs-comment">// Module code here. In this example, define a function.</span>
45235 …<span class="hljs-keyword">function</span> <span class="hljs-title function_">someFunc</span>(<spa…
45236 <span class="hljs-built_in">exports</span> = someFunc;
45237 …<span class="hljs-comment">// At this point, exports is no longer a shortcut to module.exports, an…
45238 <span class="hljs-comment">// this module will still export an empty default object.</span>
45239 …<span class="hljs-variable language_">module</span>.<span class="hljs-property">exports</span> = s…
45240 …<span class="hljs-comment">// At this point, the module will now export someFunc, instead of the</…
45241 <span class="hljs-comment">// default object.</span>
45242 …})(<span class="hljs-variable language_">module</span>, <span class="hljs-variable language_">modu…
45243 …<span class="hljs-keyword">return</span> <span class="hljs-variable language_">module</span>.<span…
45250 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" c…
45258 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" c…
45267 <li>Type: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_…
45275 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" …
45283 … api_stability_0"><a href="#documentation_stability_index">Stability: 0</a> - Deprecated: Please u…
45286 …mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Null_type" class="type">&#x3C;null></a> | <a…
45296 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" c…
45305 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" c…
45313 <li><code>id</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structure…
45314 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Data_…
45325 <!-- Anchors to make sure old links find a target -->
45335 <!-- Anchors to make sure old links find a target -->
45359 <td><p>Unflag Top-Level Await.</p></td></tr>
45363 <td><p>Loading ECMAScript modules no longer requires a command-line flag.</p></td></tr>
45371 …ty api_stability_2"><a href="#documentation_stability_index">Stability: 2</a> - Stable</div><p></p>
45374 <p>ECMAScript modules are <a href="https://tc39.github.io/ecma262/#sec-modules">the official standa…
45375 code for reuse. Modules are defined using a variety of <a href="https://developer.mozilla.org/en-US…
45376 <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/export"><code…
45378 <pre><code class="language-js"><span class="hljs-comment">// addTwo.mjs</span>
45379 <span class="hljs-keyword">function</span> <span class="hljs-title function_">addTwo</span>(<span c…
45380 <span class="hljs-keyword">return</span> num + <span class="hljs-number">2</span>;
45383 <span class="hljs-keyword">export</span> { addTwo };</code></pre>
45385 <pre><code class="language-js"><span class="hljs-comment">// app.mjs</span>
45386 <span class="hljs-keyword">import</span> { addTwo } <span class="hljs-keyword">from</span> <span cl…
45388 <span class="hljs-comment">// Prints: 6</span>
45389 …js-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="h…
45393 <!-- Anchors to make sure old links find a target -->
45402 <code>--input-type</code> flag. See
45405 <!-- Anchors to make sure old links find a target -->
45435 <p><em>Bare specifiers</em> like <code>'some-package'</code> or <code>'some-package/shuffle'</code>…
45470 …e class="language-js"><span class="hljs-keyword">import</span> <span class="hljs-string">'./foo.mj…
45471 <span class="hljs-keyword">import</span> <span class="hljs-string">'./foo.mjs?query=2'</span>; <spa…
45479 <p><a href="https://developer.mozilla.org/en-US/docs/Web/HTTP/Basics_of_HTTP/Data_URIs"><code>data:…
45482 …for JSON (requires <a href="#cli_experimental_json_modules"><code>--experimental-json-modules</cod…
45483 …for Wasm (requires <a href="#cli_experimental_wasm_modules"><code>--experimental-wasm-modules</cod…
45488 <a href="https://url.spec.whatwg.org/#special-scheme">special scheme</a>. For example, attempting t…
45492 <pre><code class="language-js"><span class="hljs-keyword">import</span> <span class="hljs-string">'…
45493 <span class="hljs-keyword">import</span> _ <span class="hljs-keyword">from</span> <span class="hljs
45509 …e><code class="language-js"><span class="hljs-keyword">import</span> fs <span class="hljs-keyword"…
45516-js"><span class="hljs-keyword">import</span> <span class="hljs-title class_">EventEmitter</span> …
45517 <span class="hljs-keyword">const</span> e = <span class="hljs-keyword">new</span> <span class="hljs
45518 …de class="language-js"><span class="hljs-keyword">import</span> { readFile } <span class="hljs-key…
45519 …n class="hljs-title function_">readFile</span>(<span class="hljs-string">'./foo.txt'</span>, <span…
45520 <span class="hljs-keyword">if</span> (err) {
45521 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">error</spa…
45522 } <span class="hljs-keyword">else</span> {
45523 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>…
45526 …lass="language-js"><span class="hljs-keyword">import</span> fs, { readFileSync } <span class="hljs
45527 <span class="hljs-keyword">import</span> { syncBuiltinESMExports } <span class="hljs-keyword">from<…
45529-property">readFileSync</span> = <span class="hljs-function">() =></span> <span class="hljs-title …
45530 <span class="hljs-title function_">syncBuiltinESMExports</span>();
45532 fs.<span class="hljs-property">readFileSync</span> === readFileSync;</code></pre>
45534 <p><a href="https://wiki.developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/impor…
45538 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Objec…
45544 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" c…
45549 … class="language-js"><span class="hljs-keyword">import</span> { readFileSync } <span class="hljs-k…
45550-keyword">const</span> buffer = <span class="hljs-title function_">readFileSync</span>(<span class…
45552 <!--
45554 - v13.9.0
45555 - v12.16.2
45557 - version: v14.18.0
45558 pr-url: https://github.com/nodejs/node/pull/38587
45560 -->
45561 … api_stability_1"><a href="#documentation_stability_index">Stability: 1</a> - Experimental</div><p…
45562 <p>This feature is only available with the <code>--experimental-import-meta-resolve</code>
45565 <li><code>specifier</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_st…
45566 <li><code>parent</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struc…
45568 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Obje…
45570 <p>Provides a module-relative resolution function scoped to each module, returning
45572 <!-- eslint-skip -->
45573-js"><span class="hljs-keyword">const</span> dependencyAsset = <span class="hljs-keyword">await</s…
45576 <!-- eslint-skip -->
45577-js"><span class="hljs-keyword">await</span> <span class="hljs-keyword">import</span>.<span class=…
45598 <!-- eslint-disable no-duplicate-imports -->
45599-js"><span class="hljs-keyword">import</span> { <span class="hljs-keyword">default</span> <span cl…
45601 <span class="hljs-comment">// The following import statement is "syntax sugar" (equivalent but swee…
45602 <span class="hljs-comment">// for `{ default as cjsSugar }` in the above import statement:</span>
45603 <span class="hljs-keyword">import</span> cjsSugar <span class="hljs-keyword">from</span> <span clas…
45605 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(…
45606 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(…
45607 <span class="hljs-comment">// Prints:</span>
45608 <span class="hljs-comment">// &#x3C;module.exports></span>
45609 <span class="hljs-comment">// true</span></code></pre>
45615 <!-- eslint-skip -->
45616 …uage-js"><span class="hljs-keyword">import</span> * <span class="hljs-keyword">as</span> m <span c…
45617 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(…
45618-variable language_">console</span>.<span class="hljs-title function_">log</span>(m === <span clas…
45619 <span class="hljs-comment">// Prints:</span>
45620 <span class="hljs-comment">// [Module] { default: &#x3C;module.exports> }</span>
45621 <span class="hljs-comment">// true</span></code></pre>
45627 <pre><code class="language-js cjs"><span class="hljs-comment">// cjs.cjs</span>
45628 <span class="hljs-built_in">exports</span>.<span class="hljs-property">name</span> = <span class="h…
45630 <!-- eslint-disable no-duplicate-imports -->
45631 …code class="language-js"><span class="hljs-keyword">import</span> { name } <span class="hljs-keywo…
45632 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(…
45633 <span class="hljs-comment">// Prints: 'exported'</span>
45635 <span class="hljs-keyword">import</span> cjs <span class="hljs-keyword">from</span> <span class="hl…
45636 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(…
45637 <span class="hljs-comment">// Prints: { name: 'exported' }</span>
45639 …pan class="hljs-keyword">import</span> * <span class="hljs-keyword">as</span> m <span class="hljs-
45640 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(…
45641 <span class="hljs-comment">// Prints: [Module] { default: { name: 'exported' }, name: 'exported' }<…
45651 …er outputs. See <a href="https://github.com/guybedford/cjs-module-lexer/tree/1.2.2">cjs-module-lex…
45664 <code>--experimental-json-modules</code> flag.</p>
45666 <!-- eslint-skip -->
45667 …de class="language-js"><span class="hljs-keyword">import</span> { readFile } <span class="hljs-key…
45668-keyword">const</span> json = <span class="hljs-variable constant_">JSON</span>.<span class="hljs-
45690 … api_stability_1"><a href="#documentation_stability_index">Stability: 1</a> - Experimental</div><p…
45692 and are loaded using the CJS loader. <a href="https://html.spec.whatwg.org/#creating-a-json-module-
45694 additional flag <code>--experimental-json-modules</code> when running Node.js.</p>
45695 <p>When the <code>--experimental-json-modules</code> flag is included, both the
45703 <!-- eslint-skip -->
45704 …de class="language-js"><span class="hljs-keyword">import</span> packageConfig <span class="hljs-ke…
45705 <p>The <code>--experimental-json-modules</code> flag is needed for the module
45707 <pre><code class="language-bash">node index.mjs <span class="hljs-comment"># fails</span>
45708 node --experimental-json-modules index.mjs <span class="hljs-comment"># works</span></code></pre>
45711 … api_stability_1"><a href="#documentation_stability_index">Stability: 1</a> - Experimental</div><p…
45713 <code>--experimental-wasm-modules</code> flag, allowing any <code>.wasm</code> files to be
45716 <a href="https://github.com/webassembly/esm-integration">ES Module Integration Proposal for Web Ass…
45718 …uage-js"><span class="hljs-keyword">import</span> * <span class="hljs-keyword">as</span> M <span c…
45719 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(…
45721 <pre><code class="language-bash">node --experimental-wasm-modules index.mjs</code></pre>
45724 </section><section><h3>Top-level <code>await</code><span><a class="mark" href="#esm_top_level_await…
45728 … api_stability_1"><a href="#documentation_stability_index">Stability: 1</a> - Experimental</div><p…
45731-js"><span class="hljs-keyword">export</span> <span class="hljs-keyword">const</span> five = <span…
45733 …code class="language-js"><span class="hljs-keyword">import</span> { five } <span class="hljs-keywo…
45735 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(…
45736 <pre><code class="language-bash">node b.mjs <span class="hljs-comment"># works</span></code></pre>
45739 …code class="language-js"><span class="hljs-keyword">import</span> { spawn } <span class="hljs-keyw…
45740 <span class="hljs-keyword">import</span> { execPath } <span class="hljs-keyword">from</span> <span …
45742 <span class="hljs-title function_">spawn</span>(execPath, [
45743 <span class="hljs-string">'--input-type=module'</span>,
45744 <span class="hljs-string">'--eval'</span>,
45745 <span class="hljs-comment">// Never-resolving Promise:</span>
45746 <span class="hljs-string">'await new Promise(() => {})'</span>,
45747 …<span class="hljs-title function_">once</span>(<span class="hljs-string">'exit'</span>, <span clas…
45748 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>…
45752 … api_stability_1"><a href="#documentation_stability_index">Stability: 1</a> - Experimental</div><p…
45756 provided via a <code>--experimental-loader ./loader-name.mjs</code> argument to Node.js.</p>
45766 <li><code>specifier</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_st…
45767 <li><code>context</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference…
45769 <li><code>conditions</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_s…
45770 <li><code>parentURL</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_st…
45773 <li><code>defaultResolve</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Re…
45774 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Obje…
45776 <li><code>url</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structur…
45793 <pre><code class="language-js"><span class="hljs-comment">/**
45794 …* <span class="hljs-doctag">@param</span> {<span class="hljs-type">string</span>} <span class="hlj…
45795 * <span class="hljs-doctag">@param</span> {<span class="hljs-type">{
45799 …* <span class="hljs-doctag">@param</span> {<span class="hljs-type">Function</span>} <span class="h…
45800 …* <span class="hljs-doctag">@returns</span> {<span class="hljs-type">Promise&#x3C;{ url: string </…
45802-keyword">export</span> <span class="hljs-keyword">async</span> <span class="hljs-keyword">functio…
45803 …<span class="hljs-keyword">const</span> { parentURL = <span class="hljs-literal">null</span> } = c…
45804-keyword">if</span> (<span class="hljs-title class_">Math</span>.<span class="hljs-title function_…
45805 …<span class="hljs-comment">// For some or all specifiers, do some custom logic for resolving.</spa…
45806 <span class="hljs-comment">// Always return an object of the form {url: &#x3C;string>}.</span>
45807 <span class="hljs-keyword">return</span> {
45808 <span class="hljs-attr">url</span>: parentURL ?
45809 …<span class="hljs-keyword">new</span> <span class="hljs-title function_">URL</span>(specifier, par…
45810 …<span class="hljs-keyword">new</span> <span class="hljs-title function_">URL</span>(specifier).<sp…
45813-keyword">if</span> (<span class="hljs-title class_">Math</span>.<span class="hljs-title function_…
45814 …<span class="hljs-comment">// When calling `defaultResolve`, the arguments can be modified. In thi…
45815 …<span class="hljs-comment">// case it's adding another value for matching conditional exports.</sp…
45816 …<span class="hljs-keyword">return</span> <span class="hljs-title function_">defaultResolve</span>(…
45818 …span class="hljs-attr">conditions</span>: [...context.<span class="hljs-property">conditions</span…
45821 <span class="hljs-comment">// Defer to Node.js for all other specifiers.</span>
45822 …<span class="hljs-keyword">return</span> <span class="hljs-title function_">defaultResolve</span>(…
45830 <li><code>url</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structur…
45831 <li><code>context</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference…
45832 <li><code>defaultGetFormat</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/…
45833 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Obje…
45835 <li><code>format</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struc…
45878-US/docs/Web/JavaScript/Reference/Global_Objects/String"><code>string</code></a>, <a href="https:/…
45881 …g/en-US/docs/Web/JavaScript/Reference/Global_Objects/ArrayBuffer"><code>ArrayBuffer</code></a> obj…
45882 …rg/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray"><code>TypedArray</code></a> obje…
45884 <p>Note: If the source value of a text-based format (i.e., <code>'json'</code>, <code>'module'</cod…
45886 <pre><code class="language-js"><span class="hljs-comment">/**
45887 …* <span class="hljs-doctag">@param</span> {<span class="hljs-type">string</span>} <span class="hlj…
45888 …* <span class="hljs-doctag">@param</span> {<span class="hljs-type">Object</span>} context (current…
45889 …* <span class="hljs-doctag">@param</span> {<span class="hljs-type">Function</span>} <span class="h…
45890 …* <span class="hljs-doctag">@returns</span> {<span class="hljs-type">Promise&#x3C;{ format: string…
45892-keyword">export</span> <span class="hljs-keyword">async</span> <span class="hljs-keyword">functio…
45893-keyword">if</span> (<span class="hljs-title class_">Math</span>.<span class="hljs-title function_…
45894 …<span class="hljs-comment">// For some or all URLs, do some custom logic for determining format.</…
45895 …<span class="hljs-comment">// Always return an object of the form {format: &#x3C;string>}, where t…
45896 <span class="hljs-comment">// format is one of the strings in the preceding table.</span>
45897 <span class="hljs-keyword">return</span> {
45898 <span class="hljs-attr">format</span>: <span class="hljs-string">'module'</span>,
45901 <span class="hljs-comment">// Defer to Node.js for all other URLs.</span>
45902 …<span class="hljs-keyword">return</span> <span class="hljs-title function_">defaultGetFormat</span…
45910 <li><code>url</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structur…
45911 <li><code>context</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference…
45913 <li><code>format</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struc…
45916 <li><code>defaultGetSource</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/…
45917 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Obje…
45919-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | <a href="htt…
45926 <pre><code class="language-js"><span class="hljs-comment">/**
45927 …* <span class="hljs-doctag">@param</span> {<span class="hljs-type">string</span>} <span class="hlj…
45928 …* <span class="hljs-doctag">@param</span> {<span class="hljs-type">{ format: string </span>}} cont…
45929 …* <span class="hljs-doctag">@param</span> {<span class="hljs-type">Function</span>} <span class="h…
45930 …* <span class="hljs-doctag">@returns</span> {<span class="hljs-type">Promise&#x3C;{ source: !(stri…
45932-keyword">export</span> <span class="hljs-keyword">async</span> <span class="hljs-keyword">functio…
45933 <span class="hljs-keyword">const</span> { format } = context;
45934-keyword">if</span> (<span class="hljs-title class_">Math</span>.<span class="hljs-title function_…
45935 …<span class="hljs-comment">// For some or all URLs, do some custom logic for retrieving the source…
45936 …<span class="hljs-comment">// Always return an object of the form {source: &#x3C;string|buffer>}.<…
45937 <span class="hljs-keyword">return</span> {
45938 <span class="hljs-attr">source</span>: <span class="hljs-string">'...'</span>,
45941 <span class="hljs-comment">// Defer to Node.js for all other URLs.</span>
45942 …<span class="hljs-keyword">return</span> <span class="hljs-title function_">defaultGetSource</span…
45950-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | <a href="htt…
45951 <li><code>context</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference…
45953 <li><code>format</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struc…
45954 <li><code>url</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structur…
45957 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Obje…
45959-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | <a href="htt…
45966 <p>If this hook is used to convert unknown-to-Node.js file types into executable
45968 unknown-to-Node.js file extensions. See the <a href="#esm_transpiler_loader">transpiler loader exam…
45969 <pre><code class="language-js"><span class="hljs-comment">/**
45970 … <span class="hljs-doctag">@param</span> {<span class="hljs-type">!(string | SharedArrayBuffer | U…
45971 * <span class="hljs-doctag">@param</span> {<span class="hljs-type">{
45975 …* <span class="hljs-doctag">@param</span> {<span class="hljs-type">Function</span>} <span class="h…
45976 …* <span class="hljs-doctag">@returns</span> {<span class="hljs-type">Promise&#x3C;{ source: !(stri…
45978-keyword">export</span> <span class="hljs-keyword">async</span> <span class="hljs-keyword">functio…
45979 <span class="hljs-keyword">const</span> { url, format } = context;
45980-keyword">if</span> (<span class="hljs-title class_">Math</span>.<span class="hljs-title function_…
45981 …<span class="hljs-comment">// For some or all URLs, do some custom logic for modifying the source.…
45982 …<span class="hljs-comment">// Always return an object of the form {source: &#x3C;string|buffer>}.<…
45983 <span class="hljs-keyword">return</span> {
45984 <span class="hljs-attr">source</span>: <span class="hljs-string">'...'</span>,
45987 <span class="hljs-comment">// Defer to Node.js for all other sources.</span>
45988 …<span class="hljs-keyword">return</span> <span class="hljs-title function_">defaultTransformSource…
45996 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Strin…
46000 run as sloppy-mode script on startup.</p>
46002 scope. The only argument is a <code>require</code>-like function that can be used to load
46006 <pre><code class="language-js"><span class="hljs-comment">/**
46007 …* <span class="hljs-doctag">@returns</span> {<span class="hljs-type">string</span>} Code to run be…
46009 …js-keyword">export</span> <span class="hljs-keyword">function</span> <span class="hljs-title funct…
46010 <span class="hljs-keyword">return</span> <span class="hljs-string">`\
46021 <p>The various loader hooks can be used together to accomplish wide-ranging
46030 <pre><code class="language-js"><span class="hljs-comment">// https-loader.mjs</span>
46031 <span class="hljs-keyword">import</span> { get } <span class="hljs-keyword">from</span> <span class…
46033 …ass="hljs-keyword">export</span> <span class="hljs-keyword">function</span> <span class="hljs-titl…
46034 …<span class="hljs-keyword">const</span> { parentURL = <span class="hljs-literal">null</span> } = c…
46036 …<span class="hljs-comment">// Normally Node.js would error on specifiers starting with 'https://',…
46037 …<span class="hljs-comment">// this hook intercepts them and converts them into absolute URLs to be…
46038 <span class="hljs-comment">// passed along to the later hooks below.</span>
46039 …<span class="hljs-keyword">if</span> (specifier.<span class="hljs-title function_">startsWith</spa…
46040 <span class="hljs-keyword">return</span> {
46041 <span class="hljs-attr">url</span>: specifier
46043-keyword">else</span> <span class="hljs-keyword">if</span> (parentURL &#x26;&#x26; parentURL.<span…
46044 <span class="hljs-keyword">return</span> {
46045 …"hljs-attr">url</span>: <span class="hljs-keyword">new</span> <span class="hljs-title function_">U…
46049 <span class="hljs-comment">// Let Node.js handle all other specifiers.</span>
46050 …<span class="hljs-keyword">return</span> <span class="hljs-title function_">defaultResolve</span>(…
46053 …ss="hljs-keyword">export</span> <span class="hljs-keyword">function</span> <span class="hljs-title…
46054 …<span class="hljs-comment">// This loader assumes all network-provided JavaScript is ES module cod…
46055 …<span class="hljs-keyword">if</span> (url.<span class="hljs-title function_">startsWith</span>(<sp…
46056 <span class="hljs-keyword">return</span> {
46057 <span class="hljs-attr">format</span>: <span class="hljs-string">'module'</span>
46061 <span class="hljs-comment">// Let Node.js handle all other URLs.</span>
46062 …<span class="hljs-keyword">return</span> <span class="hljs-title function_">defaultGetFormat</span…
46065 …ss="hljs-keyword">export</span> <span class="hljs-keyword">function</span> <span class="hljs-title…
46066 …<span class="hljs-comment">// For JavaScript to be loaded over the network, we need to fetch and</…
46067 <span class="hljs-comment">// return it.</span>
46068 …<span class="hljs-keyword">if</span> (url.<span class="hljs-title function_">startsWith</span>(<sp…
46069-keyword">return</span> <span class="hljs-keyword">new</span> <span class="hljs-title class_">Prom…
46070 …<span class="hljs-title function_">get</span>(url, <span class="hljs-function">(<span class="hljs-
46071 <span class="hljs-keyword">let</span> data = <span class="hljs-string">''</span>;
46072 ….<span class="hljs-title function_">on</span>(<span class="hljs-string">'data'</span>, <span class…
46073-title function_">on</span>(<span class="hljs-string">'end'</span>, <span class="hljs-function">()…
46074-title function_">on</span>(<span class="hljs-string">'error'</span>, <span class="hljs-function">…
46078 <span class="hljs-comment">// Let Node.js handle all other URLs.</span>
46079 …<span class="hljs-keyword">return</span> <span class="hljs-title function_">defaultGetSource</span…
46081 <pre><code class="language-js"><span class="hljs-comment">// main.mjs</span>
46082-keyword">import</span> { <span class="hljs-variable constant_">VERSION</span> } <span class="hljs
46084 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(…
46086 <code>node --experimental-loader ./https-loader.mjs ./main.mjs</code>
46097 <pre><code class="language-js"><span class="hljs-comment">// coffeescript-loader.mjs</span>
46098 …js-keyword">import</span> { <span class="hljs-variable constant_">URL</span>, pathToFileURL } <spa…
46099 …lass="hljs-keyword">import</span> <span class="hljs-title class_">CoffeeScript</span> <span class=…
46101-keyword">const</span> baseURL = <span class="hljs-title function_">pathToFileURL</span>(<span cla…
46103 <span class="hljs-comment">// CoffeeScript files end in .coffee, .litcoffee or .coffee.md.</span>
46104 <span class="hljs-keyword">const</span> extensionsRegex = <span class="hljs-regexp">/\.coffee$|\.li…
46106 …ass="hljs-keyword">export</span> <span class="hljs-keyword">function</span> <span class="hljs-titl…
46107 <span class="hljs-keyword">const</span> { parentURL = baseURL } = context;
46109 …<span class="hljs-comment">// Node.js normally errors on unknown file extensions, so return a URL …
46110 <span class="hljs-comment">// specifiers ending in the CoffeeScript file extensions.</span>
46111 …<span class="hljs-keyword">if</span> (extensionsRegex.<span class="hljs-title function_">test</spa…
46112 <span class="hljs-keyword">return</span> {
46113 …"hljs-attr">url</span>: <span class="hljs-keyword">new</span> <span class="hljs-title function_">U…
46117 <span class="hljs-comment">// Let Node.js handle all other specifiers.</span>
46118 …<span class="hljs-keyword">return</span> <span class="hljs-title function_">defaultResolve</span>(…
46121 …ss="hljs-keyword">export</span> <span class="hljs-keyword">function</span> <span class="hljs-title…
46122 …<span class="hljs-comment">// Now that we patched resolve to let CoffeeScript URLs through, we nee…
46123 …<span class="hljs-comment">// tell Node.js what format such URLs should be interpreted as. For the…
46124 …<span class="hljs-comment">// purposes of this loader, all CoffeeScript URLs are ES modules.</span>
46125 …<span class="hljs-keyword">if</span> (extensionsRegex.<span class="hljs-title function_">test</spa…
46126 <span class="hljs-keyword">return</span> {
46127 <span class="hljs-attr">format</span>: <span class="hljs-string">'module'</span>
46131 <span class="hljs-comment">// Let Node.js handle all other URLs.</span>
46132 …<span class="hljs-keyword">return</span> <span class="hljs-title function_">defaultGetFormat</span…
46135 …"hljs-keyword">export</span> <span class="hljs-keyword">function</span> <span class="hljs-title fu…
46136 <span class="hljs-keyword">const</span> { url, format } = context;
46138 …<span class="hljs-keyword">if</span> (extensionsRegex.<span class="hljs-title function_">test</spa…
46139 <span class="hljs-keyword">return</span> {
46140-attr">source</span>: <span class="hljs-title class_">CoffeeScript</span>.<span class="hljs-title …
46144 <span class="hljs-comment">// Let Node.js handle all other sources.</span>
46145 …<span class="hljs-keyword">return</span> <span class="hljs-title function_">defaultTransformSource…
46147 <pre><code class="language-coffee"><span class="hljs-comment"># main.coffee</span>
46148 <span class="hljs-keyword">import</span> { scream } <span class="hljs-keyword">from</span> <span cl…
46149 console.log scream <span class="hljs-string">'hello, world'</span>
46151 <span class="hljs-keyword">import</span> { version } <span class="hljs-keyword">from</span> <span c…
46152 console.log <span class="hljs-string">"Brought to you by Node.js version <span class="hljs-subst">#…
46153 <pre><code class="language-coffee"><span class="hljs-comment"># scream.coffee</span>
46154 <span class="hljs-keyword">export</span> scream = <span class="hljs-function"><span class="hljs-par…
46156 <code>node --experimental-loader ./coffeescript-loader.mjs main.coffee</code>
46165 <li>FileURL-based resolution as is used by ES modules</li>
46183 of these top-level routines unless stated otherwise.</p>
46192 <li><em>Invalid Package Target</em>: Package exports or imports define a target module
46193 for the package that is an invalid type or string target.</li>
46194 <li><em>Package Path Not Exported</em>: Package exports do not define or permit a target
46199 which is not a supported target for module imports.</li>
46422 <li>If <em>pjson.imports</em> is a non-null Object, then
46446 <li>Let <em>target</em> be the value of <em>matchObj</em>[<em>matchKey</em>].</li>
46448 <em>packageURL</em>, <em>target</em>, <em>""</em>, <strong>false</strong>, <em>isImports</em>, <em>…
46469 <li>Let <em>target</em> be the value of <em>matchObj</em>[<em>expansionKey</em>].</li>
46474 <em>packageURL</em>, <em>target</em>, <em>subpath</em>, <strong>true</strong>, <em>isImports</em>,
46484 <li>Let <em>target</em> be the value of <em>matchObj</em>[<em>expansionKey</em>].</li>
46488 <em>packageURL</em>, <em>target</em>, <em>subpath</em>, <strong>false</strong>, <em>isImports</em>,
46507 <li>If <em>baseLengthA</em> is greater than <em>baseLengthB</em>, return -1.</li>
46510 <li>If <em>keyB</em> does not contain <em>"*"</em>, return -1.</li>
46511 <li>If the length of <em>keyA</em> is greater than the length of <em>keyB</em>, return -1.</li>
46516 <p><strong>PACKAGE_TARGET_RESOLVE</strong>(<em>packageURL</em>, <em>target</em>, <em>subpath</em>, …
46520 <li>If <em>target</em> is a String, then
46522 …em>pattern</em> is <strong>false</strong>, <em>subpath</em> has non-zero length and <em>target</em>
46524 <li>If <em>target</em> does not start with <em>"./"</em>, then
46526 <li>If <em>internal</em> is <strong>true</strong> and <em>target</em> does not start with <em>"../"…
46531 <li>Return <strong>PACKAGE_RESOLVE</strong>(<em>target</em> with every instance of
46535 <li>Return <strong>PACKAGE_RESOLVE</strong>(<em>target</em> + <em>subpath</em>,
46539 <li>Otherwise, throw an <em>Invalid Package Target</em> error.</li>
46542 <li>If <em>target</em> split on <em>"/"</em> or <em>"\"</em> contains any <em>"."</em>, <em>".."</e…
46544 <em>Invalid Package Target</em> error.</li>
46546 <em>packageURL</em> and <em>target</em>.</li>
46564 <li>Otherwise, if <em>target</em> is a non-null Object, then
46566 <li>If <em>exports</em> contains any index property keys, as defined in ECMA-262
46567 <a href="https://tc39.es/ecma262/#integer-index">6.1.7 Array Index</a>, throw an <em>Invalid Packag…
46568 <li>For each property <em>p</em> of <em>target</em>, in object insertion order as,
46573 <li>Let <em>targetValue</em> be the value of the <em>p</em> property in <em>target</em>.</li>
46586 <li>Otherwise, if <em>target</em> is an Array, then
46589 <li>For each item <em>targetValue</em> in <em>target</em>, do
46593 <em>conditions</em>), continuing the loop on any <em>Invalid Package Target</em>
46602 <li>Otherwise, if <em>target</em> is <em>null</em>, return <strong>null</strong>.</li>
46603 <li>Otherwise throw an <em>Invalid Package Target</em> error.</li>
46675 … api_stability_1"><a href="#documentation_stability_index">Stability: 1</a> - Experimental</div><p…
46680 <p>The <code>--experimental-specifier-resolution=[mode]</code> flag can be used to customize
46685 <pre><code class="language-console"><span class="hljs-meta">$ </span><span class="language-bash">no…
46687 <span class="hljs-meta">$ </span><span class="language-bash">node index <span class="hljs-comment">…
46689 <span class="hljs-meta">$ </span><span class="language-bash">node --experimental-specifier-resoluti…
46691 <!-- Note: The cjs-module-lexer link should be kept in-sync with the deps version --></section>
46700 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Objec…
46710 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" c…
46717 … class="js-flavor-selector" type="checkbox" checked aria-label="Show modern ES modules syntax"><co…
46718 <span class="hljs-comment">// In an ECMAScript module</span>
46719-keyword">import</span> { builtinModules <span class="hljs-keyword">as</span> builtin } <span clas…
46720 <span class="hljs-comment">// In a CommonJS module</span>
46721 …ss="hljs-keyword">const</span> builtin = <span class="hljs-built_in">require</span>(<span class="h…
46727 <li><code>filename</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_str…
46732 …lass="language-js mjs"><span class="hljs-keyword">import</span> { createRequire } <span class="hlj…
46733-keyword">const</span> <span class="hljs-built_in">require</span> = <span class="hljs-title functi…
46735 <span class="hljs-comment">// sibling-module.js is a CommonJS module.</span>
46736 <span class="hljs-keyword">const</span> siblingModule = <span class="hljs-built_in">require</span>(…
46741 … api_stability_0"><a href="#documentation_stability_index">Stability: 0</a> - Deprecated: Please u…
46743 <li><code>filename</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_str…
46747 …"language-js"><span class="hljs-keyword">const</span> { createRequireFromPath } = <span class="hlj…
46748 <span class="hljs-keyword">const</span> requireUtil = <span class="hljs-title function_">createRequ…
46750 <span class="hljs-comment">// Require `../src/utils/some-tool`</span>
46751 requireUtil(<span class="hljs-string">'./some-tool'</span>);</code></pre>
46759 …<code class="language-js"><span class="hljs-keyword">const</span> fs = <span class="hljs-built_in"…
46760 <span class="hljs-keyword">const</span> assert = <span class="hljs-built_in">require</span>(<span c…
46761 <span class="hljs-keyword">const</span> { syncBuiltinESMExports } = <span class="hljs-built_in">req…
46763 fs.<span class="hljs-property">readFile</span> = newAPI;
46765 <span class="hljs-keyword">delete</span> fs.<span class="hljs-property">readFileSync</span>;
46767 <span class="hljs-keyword">function</span> <span class="hljs-title function_">newAPI</span>(<span c…
46768 <span class="hljs-comment">// ...</span>
46771 fs.<span class="hljs-property">newAPI</span> = newAPI;
46773 <span class="hljs-title function_">syncBuiltinESMExports</span>();
46775-title function_">import</span>(<span class="hljs-string">'fs'</span>).<span class="hljs-title fun…
46776 <span class="hljs-comment">// It syncs the existing readFile property with the new value</span>
46777 …assert.<span class="hljs-title function_">strictEqual</span>(esmFS.<span class="hljs-property">rea…
46778 <span class="hljs-comment">// readFileSync has been deleted from the required fs</span>
46779 …hljs-title function_">strictEqual</span>(<span class="hljs-string">'readFileSync'</span> <span cla…
46780 …<span class="hljs-comment">// syncBuiltinESMExports() does not remove readFileSync from esmFS</spa…
46781 …ljs-title function_">strictEqual</span>(<span class="hljs-string">'readFileSync'</span> <span clas…
46782 <span class="hljs-comment">// syncBuiltinESMExports() does not add names</span>
46783 …assert.<span class="hljs-title function_">strictEqual</span>(esmFS.<span class="hljs-property">new…
46789 … api_stability_1"><a href="#documentation_stability_index">Stability: 1</a> - Experimental</div><p…
46794 <a href="#cli_enable_source_maps"><code>--enable-source-maps</code></a>, or with code coverage enab…
46797 … class="js-flavor-selector" type="checkbox" checked aria-label="Show modern ES modules syntax"><co…
46798 <span class="hljs-comment">// In an ECMAScript module</span>
46799-keyword">import</span> { findSourceMap, <span class="hljs-title class_">SourceMap</span> } <span …
46800 <span class="hljs-comment">// In a CommonJS module</span>
46801-keyword">const</span> { findSourceMap, <span class="hljs-title class_">SourceMap</span> } = <span…
46802 <!-- Anchors to make sure old links find a target -->
46809 <li><code>path</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
46820 <li><code>payload</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference…
46825 <li><code>file</code>: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struct…
46826 <li><code>version</code>: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_str…
46827 <li><code>sources</code>: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_str…
46828 <li><code>sourcesContent</code>: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/D…
46829 <li><code>names</code>: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struc…
46830 <li><code>mappings</code>: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_st…
46831 <li><code>sourceRoot</code>: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_…
46835 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Obje…
46840 <li><code>lineNumber</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_s…
46841 <li><code>columnNumber</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data…
46842 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Obje…
46848 <li>generatedLine: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures…
46849 <li>generatedColumn: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structur…
46850 <li>originalSource: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structure…
46851 <li>originalLine: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#…
46852 <li>originalColumn: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structure…
46853 <li>name: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_t…
46870 <td><p>Unflag self-referencing a package using its name.</p></td></tr>
46894 top-level <a href="#packages_type"><code>"type"</code></a> field with a value of <code>"module"</co…
46897 <p>Strings passed in as an argument to <code>--eval</code>, or piped to <code>node</code> via <code…
46898 with the flag <code>--input-type=module</code>.</p>
46902 where the nearest parent <code>package.json</code> file contains no top-level <code>"type"</code>
46903 field, or string input without the flag <code>--input-type</code>. This behavior is to
46914 top-level field <a href="#packages_type"><code>"type"</code></a> with a value of <code>"commonjs"</…
46917 <p>Strings passed in as an argument to <code>--eval</code> or <code>--print</code>, or piped to <co…
46918 via <code>STDIN</code>, with the flag <code>--input-type=commonjs</code>.</p>
46923 future-proof the package in case the default type of Node.js ever changes, and
46932 <p>The <code>"type"</code> field applies not only to initial entry points (<code>node my-app.js</co…
46934 <pre><code class="language-js"><span class="hljs-comment">// my-app.js, treated as an ES module bec…
46935 <span class="hljs-comment">// file in the same folder with "type": "module".</span>
46937 <span class="hljs-keyword">import</span> <span class="hljs-string">'./startup/init.js'</span>;
46938 <span class="hljs-comment">// Loaded as ES module since ./startup contains no package.json file,</s…
46939 <span class="hljs-comment">// and therefore inherits the "type" value from one level up.</span>
46941 <span class="hljs-keyword">import</span> <span class="hljs-string">'commonjs-package'</span>;
46942 <span class="hljs-comment">// Loaded as CommonJS since ./node_modules/commonjs-package/package.json…
46943 <span class="hljs-comment">// lacks a "type" field or contains "type": "commonjs".</span>
46945 <span class="hljs-keyword">import</span> <span class="hljs-string">'./node_modules/commonjs-package…
46946 <span class="hljs-comment">// Loaded as CommonJS since ./node_modules/commonjs-package/package.json…
46947 <span class="hljs-comment">// lacks a "type" field or contains "type": "commonjs".</span></code></p…
46952 <pre><code class="language-js"><span class="hljs-keyword">import</span> <span class="hljs-string">'…
46953 <span class="hljs-comment">// Loaded as CommonJS since .cjs is always loaded as CommonJS.</span>
46955 <span class="hljs-keyword">import</span> <span class="hljs-string">'commonjs-package/src/index.mjs'…
46956 <span class="hljs-comment">// Loaded as ES module since .mjs is always loaded as ES module.</span><…
46973 <h4><code>--input-type</code> flag<span><a class="mark" href="#packages_input_type_flag" id="packag…
46977 <p>Strings passed in as an argument to <code>--eval</code> (or <code>-e</code>), or piped to <code>…
46978 <code>STDIN</code>, are treated as <a href="esm.html">ES modules</a> when the <code>--input-type=mo…
46980 <pre><code class="language-bash">node --input-type=module --<span class="hljs-built_in">eval</span>…
46982 <span class="hljs-built_in">echo</span> <span class="hljs-string">"import { sep } from 'path'; cons…
46983 <p>For completeness there is also <code>--input-type=commonjs</code>, for explicitly running
46984 string input as CommonJS. This is the default behavior if <code>--input-type</code> is
46987 … api_stability_1"><a href="#documentation_stability_index">Stability: 1</a> - Experimental</div><p…
46992 available in your environment - provided you have Node.js installed.</p>
47020 …n_field_definitions"><code>package.json</code></a> (e.g. <code>require('your-package/package.json'…
47022 <p>To make the introduction of <a href="#packages_exports"><code>"exports"</code></a> non-breaking,…
47024 entry points so that the package’s public API is well-defined. For example,
47027 <pre><code class="language-json"><span class="hljs-punctuation">{</span>
47028 …<span class="hljs-attr">"name"</span><span class="hljs-punctuation">:</span> <span class="hljs-str…
47029 …<span class="hljs-attr">"exports"</span><span class="hljs-punctuation">:</span> <span class="hljs-
47030 …an class="hljs-attr">"."</span><span class="hljs-punctuation">:</span> <span class="hljs-string">"…
47031 … class="hljs-attr">"./lib"</span><span class="hljs-punctuation">:</span> <span class="hljs-string"…
47032 …ass="hljs-attr">"./lib/index"</span><span class="hljs-punctuation">:</span> <span class="hljs-stri…
47033 …ss="hljs-attr">"./lib/index.js"</span><span class="hljs-punctuation">:</span> <span class="hljs-st…
47034 …ss="hljs-attr">"./feature"</span><span class="hljs-punctuation">:</span> <span class="hljs-string"…
47035 …hljs-attr">"./feature/index.js"</span><span class="hljs-punctuation">:</span> <span class="hljs-st…
47036 …<span class="hljs-attr">"./package.json"</span><span class="hljs-punctuation">:</span> <span class…
47037 <span class="hljs-punctuation">}</span>
47038 <span class="hljs-punctuation">}</span></code></pre>
47040 <pre><code class="language-json"><span class="hljs-punctuation">{</span>
47041 …<span class="hljs-attr">"name"</span><span class="hljs-punctuation">:</span> <span class="hljs-str…
47042 …<span class="hljs-attr">"exports"</span><span class="hljs-punctuation">:</span> <span class="hljs-
47043 …an class="hljs-attr">"."</span><span class="hljs-punctuation">:</span> <span class="hljs-string">"…
47044 … class="hljs-attr">"./lib"</span><span class="hljs-punctuation">:</span> <span class="hljs-string"…
47045 …n class="hljs-attr">"./lib/*"</span><span class="hljs-punctuation">:</span> <span class="hljs-stri…
47046 …ss="hljs-attr">"./feature"</span><span class="hljs-punctuation">:</span> <span class="hljs-string"…
47047 …ass="hljs-attr">"./feature/*"</span><span class="hljs-punctuation">:</span> <span class="hljs-stri…
47048 …<span class="hljs-attr">"./package.json"</span><span class="hljs-punctuation">:</span> <span class…
47049 <span class="hljs-punctuation">}</span>
47050 <span class="hljs-punctuation">}</span></code></pre>
47058 <code>import feature from 'my-mod/feature'</code> as they need to provide the full
47059 path <code>import feature from 'my-mod/feature/index.js</code>.</p>
47063 <pre><code class="language-json"><span class="hljs-punctuation">{</span>
47064 …pan class="hljs-attr">"main"</span><span class="hljs-punctuation">:</span> <span class="hljs-strin…
47065 …<span class="hljs-attr">"exports"</span><span class="hljs-punctuation">:</span> <span class="hljs-
47066 <span class="hljs-punctuation">}</span></code></pre>
47083 <pre><code class="language-json"><span class="hljs-punctuation">{</span>
47084 …pan class="hljs-attr">"main"</span><span class="hljs-punctuation">:</span> <span class="hljs-strin…
47085 …<span class="hljs-attr">"exports"</span><span class="hljs-punctuation">:</span> <span class="hljs-
47086 …<span class="hljs-attr">"."</span><span class="hljs-punctuation">:</span> <span class="hljs-string…
47087 …<span class="hljs-attr">"./submodule"</span><span class="hljs-punctuation">:</span> <span class="h…
47088 <span class="hljs-punctuation">}</span>
47089 <span class="hljs-punctuation">}</span></code></pre>
47091 …="language-js"><span class="hljs-keyword">import</span> submodule <span class="hljs-keyword">from<…
47092 <span class="hljs-comment">// Loads ./node_modules/es-module-package/src/submodule.js</span></code>…
47094 …ge-js"><span class="hljs-keyword">import</span> submodule <span class="hljs-keyword">from</span> <…
47095 <span class="hljs-comment">// Throws ERR_PACKAGE_PATH_NOT_EXPORTED</span></code></pre>
47107 <pre><code class="language-json"><span class="hljs-comment">// package.json</span>
47108 <span class="hljs-punctuation">{</span>
47109 …<span class="hljs-attr">"imports"</span><span class="hljs-punctuation">:</span> <span class="hljs-
47110 …<span class="hljs-attr">"#dep"</span><span class="hljs-punctuation">:</span> <span class="hljs-pun…
47111 … class="hljs-attr">"node"</span><span class="hljs-punctuation">:</span> <span class="hljs-string">…
47112 …<span class="hljs-attr">"default"</span><span class="hljs-punctuation">:</span> <span class="hljs-
47113 <span class="hljs-punctuation">}</span>
47114 <span class="hljs-punctuation">}</span><span class="hljs-punctuation">,</span>
47115 …<span class="hljs-attr">"dependencies"</span><span class="hljs-punctuation">:</span> <span class="…
47116 …<span class="hljs-attr">"dep-node-native"</span><span class="hljs-punctuation">:</span> <span clas…
47117 <span class="hljs-punctuation">}</span>
47118 <span class="hljs-punctuation">}</span></code></pre>
47120 <code>dep-node-native</code> (including its exports in turn), and instead gets the local
47121 file <code>./dep-polyfill.js</code> relative to the package in other environments.</p>
47135 <pre><code class="language-json"><span class="hljs-comment">// ./node_modules/es-module-package/pac…
47136 <span class="hljs-punctuation">{</span>
47137 …<span class="hljs-attr">"exports"</span><span class="hljs-punctuation">:</span> <span class="hljs-
47138 …<span class="hljs-attr">"./features/*"</span><span class="hljs-punctuation">:</span> <span class="…
47139 <span class="hljs-punctuation">}</span><span class="hljs-punctuation">,</span>
47140 …<span class="hljs-attr">"imports"</span><span class="hljs-punctuation">:</span> <span class="hljs-
47141 …<span class="hljs-attr">"#internal/*"</span><span class="hljs-punctuation">:</span> <span class="h…
47142 <span class="hljs-punctuation">}</span>
47143 <span class="hljs-punctuation">}</span></code></pre>
47148 …s="language-js"><span class="hljs-keyword">import</span> featureX <span class="hljs-keyword">from<…
47149 <span class="hljs-comment">// Loads ./node_modules/es-module-package/src/features/x.js</span>
47151 <span class="hljs-keyword">import</span> featureY <span class="hljs-keyword">from</span> <span clas…
47152 <span class="hljs-comment">// Loads ./node_modules/es-module-package/src/features/y/y.js</span>
47154 <span class="hljs-keyword">import</span> internalZ <span class="hljs-keyword">from</span> <span cla…
47155 <span class="hljs-comment">// Loads ./node_modules/es-module-package/src/internal/z.js</span></code…
47161 treating the right hand side target pattern as a <code>**</code> glob against the list of
47165 <pre><code class="language-json"><span class="hljs-comment">// ./node_modules/es-module-package/pac…
47166 <span class="hljs-punctuation">{</span>
47167 …<span class="hljs-attr">"exports"</span><span class="hljs-punctuation">:</span> <span class="hljs-
47168 …="hljs-attr">"./features/*"</span><span class="hljs-punctuation">:</span> <span class="hljs-string…
47169 …<span class="hljs-attr">"./features/private-internal/*"</span><span class="hljs-punctuation">:</sp…
47170 <span class="hljs-punctuation">}</span>
47171 <span class="hljs-punctuation">}</span></code></pre>
47172-js"><span class="hljs-keyword">import</span> featureInternal <span class="hljs-keyword">from</spa…
47173 <span class="hljs-comment">// Throws: ERR_PACKAGE_PATH_NOT_EXPORTED</span>
47175 <span class="hljs-keyword">import</span> featureX <span class="hljs-keyword">from</span> <span clas…
47176 <span class="hljs-comment">// Loads ./node_modules/es-module-package/src/features/x.js</span></code…
47185 <pre><code class="language-json"><span class="hljs-punctuation">{</span>
47186 …<span class="hljs-attr">"exports"</span><span class="hljs-punctuation">:</span> <span class="hljs-
47187 …<span class="hljs-attr">"."</span><span class="hljs-punctuation">:</span> <span class="hljs-string…
47188 <span class="hljs-punctuation">}</span>
47189 <span class="hljs-punctuation">}</span></code></pre>
47191 <pre><code class="language-json"><span class="hljs-punctuation">{</span>
47192 …<span class="hljs-attr">"exports"</span><span class="hljs-punctuation">:</span> <span class="hljs-
47193 <span class="hljs-punctuation">}</span></code></pre>
47210 <pre><code class="language-json"><span class="hljs-comment">// package.json</span>
47211 <span class="hljs-punctuation">{</span>
47212 …class="hljs-attr">"main"</span><span class="hljs-punctuation">:</span> <span class="hljs-string">"…
47213 …<span class="hljs-attr">"exports"</span><span class="hljs-punctuation">:</span> <span class="hljs-
47214 …class="hljs-attr">"import"</span><span class="hljs-punctuation">:</span> <span class="hljs-string"…
47215 …<span class="hljs-attr">"require"</span><span class="hljs-punctuation">:</span> <span class="hljs-
47216 <span class="hljs-punctuation">}</span><span class="hljs-punctuation">,</span>
47217 …<span class="hljs-attr">"type"</span><span class="hljs-punctuation">:</span> <span class="hljs-str…
47218 <span class="hljs-punctuation">}</span></code></pre>
47221 <li><code>"import"</code> - matches when the package is loaded via <code>import</code> or
47222 <code>import()</code>, or via any top-level import or resolve operation by the
47224 target file. <em>Always mutually exclusive with <code>"require"</code>.</em></li>
47225 <li><code>"require"</code> - matches when the package is loaded via <code>require()</code>. The
47227 matches regardless of the module format of the target file. Expected
47231 <li><code>"node"</code> - matches for any Node.js environment. Can be a CommonJS or ES
47234 <li><code>"node-addons"</code> - similar to <code>"node"</code> and matches for any Node.js environ…
47238 <a href="#cli_no_addons"><code>--no-addons</code> flag</a>.</li>
47239 <li><code>"default"</code> - the generic fallback that always matches. Can be a CommonJS
47249 <pre><code class="language-json"><span class="hljs-punctuation">{</span>
47250 …pan class="hljs-attr">"main"</span><span class="hljs-punctuation">:</span> <span class="hljs-strin…
47251 …<span class="hljs-attr">"exports"</span><span class="hljs-punctuation">:</span> <span class="hljs-
47252 …<span class="hljs-attr">"."</span><span class="hljs-punctuation">:</span> <span class="hljs-string…
47253 …<span class="hljs-attr">"./feature"</span><span class="hljs-punctuation">:</span> <span class="hlj…
47254 …class="hljs-attr">"node"</span><span class="hljs-punctuation">:</span> <span class="hljs-string">"…
47255 …<span class="hljs-attr">"default"</span><span class="hljs-punctuation">:</span> <span class="hljs-
47256 <span class="hljs-punctuation">}</span>
47257 <span class="hljs-punctuation">}</span>
47258 <span class="hljs-punctuation">}</span></code></pre>
47273 <pre><code class="language-json"><span class="hljs-punctuation">{</span>
47274 …pan class="hljs-attr">"main"</span><span class="hljs-punctuation">:</span> <span class="hljs-strin…
47275 …<span class="hljs-attr">"exports"</span><span class="hljs-punctuation">:</span> <span class="hljs-
47276 …<span class="hljs-attr">"node"</span><span class="hljs-punctuation">:</span> <span class="hljs-pun…
47277 …lass="hljs-attr">"import"</span><span class="hljs-punctuation">:</span> <span class="hljs-string">…
47278 …<span class="hljs-attr">"require"</span><span class="hljs-punctuation">:</span> <span class="hljs-
47279 <span class="hljs-punctuation">}</span><span class="hljs-punctuation">,</span>
47280 … class="hljs-attr">"default"</span><span class="hljs-punctuation">:</span> <span class="hljs-strin…
47281 <span class="hljs-punctuation">}</span>
47282 <span class="hljs-punctuation">}</span></code></pre>
47292 <code>--conditions</code> flag:</p>
47293 <pre><code class="language-bash">node --conditions=development main.js</code></pre>
47295 exports, while resolving the existing <code>"node"</code>, <code>"node-addons"</code>, <code>"defau…
47299 <p>The <code>"import"</code>, <code>"require"</code>, <code>"node"</code>, <code>"node-addons"</cod…
47302 <p>The <code>"node-addons"</code> condition can be used to provide an entry point which
47304 <a href="#cli_no_addons"><code>--no-addons</code> flag</a>. When using <code>"node-addons"</code>, …
47309 … enabled in Node.js via the <a href="#packages_resolving_user_conditions"><code>--conditions</code>
47313 <li><code>"browser"</code> - any environment which implements a standard subset of global
47316 <li><code>"development"</code> - can be used to define a development-only environment
47318 <li><code>"production"</code> - can be used to define a production environment entry
47321 … enabled in Node.js via the <a href="#packages_resolving_user_conditions"><code>--conditions</code>
47323 …fic conditions such as <code>"deno"</code>, <code>"electron"</code>, or <code>"react-native"</code>
47327 to the <a href="https://github.com/nodejs/node/blob/HEAD/doc/api/packages.md#conditions-definitions…
47337 this would not necessarily be the case for company-specific or
47338 application-specific conditions.</li>
47342 <h4>Self-referencing a package using its name<span><a class="mark" href="#packages_self_referencing…
47350 <td><p>Unflag self-referencing a package using its name.</p></td></tr>
47357 <pre><code class="language-json"><span class="hljs-comment">// package.json</span>
47358 <span class="hljs-punctuation">{</span>
47359 …pan class="hljs-attr">"name"</span><span class="hljs-punctuation">:</span> <span class="hljs-strin…
47360 …<span class="hljs-attr">"exports"</span><span class="hljs-punctuation">:</span> <span class="hljs-
47361 …span class="hljs-attr">"."</span><span class="hljs-punctuation">:</span> <span class="hljs-string"…
47362 …<span class="hljs-attr">"./foo"</span><span class="hljs-punctuation">:</span> <span class="hljs-st…
47363 <span class="hljs-punctuation">}</span>
47364 <span class="hljs-punctuation">}</span></code></pre>
47366 <pre><code class="language-js"><span class="hljs-comment">// ./a-module.mjs</span>
47367 …="hljs-keyword">import</span> { something } <span class="hljs-keyword">from</span> <span class="hl…
47368 <p>Self-referencing is available only if <code>package.json</code> has <a href="#packages_exports">…
47372 <pre><code class="language-js"><span class="hljs-comment">// ./another-module.mjs</span>
47374 <span class="hljs-comment">// Imports "another" from ./m.mjs. Fails because</span>
47375 <span class="hljs-comment">// the "package.json" "exports" field</span>
47376 <span class="hljs-comment">// does not provide an export named "./m.mjs".</span>
47377 <span class="hljs-keyword">import</span> { another } <span class="hljs-keyword">from</span> <span c…
47378 <p>Self-referencing is also available when using <code>require</code>, both in an ES module,
47380 <pre><code class="language-js cjs"><span class="hljs-comment">// ./a-module.js</span>
47381 …s-keyword">const</span> { something } = <span class="hljs-built_in">require</span>(<span class="hl…
47382 <p>Finally, self-referencing also works with scoped packages. For example, this
47384 <pre><code class="language-json"><span class="hljs-comment">// package.json</span>
47385 <span class="hljs-punctuation">{</span>
47386 …an class="hljs-attr">"name"</span><span class="hljs-punctuation">:</span> <span class="hljs-string…
47387 …<span class="hljs-attr">"exports"</span><span class="hljs-punctuation">:</span> <span class="hljs-
47388 <span class="hljs-punctuation">}</span></code></pre>
47390 …ass="js-flavor-selector" type="checkbox" checked aria-label="Show modern ES modules syntax"><pre><…
47391-variable language_">module</span>.<span class="hljs-property">exports</span> = <span class="hljs-
47392 …"hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class…
47393 <pre><code class="language-console"><span class="hljs-meta">$ </span><span class="language-bash">no…
47403 ignores) the top-level <code>"module"</code> field.</p>
47406 <code>'pkg'</code> and <code>'pkg/es-module'</code>, or both at the same specifier via <a href="#pa…
47428 all-CommonJS or all-ES module environments, respectively, and therefore is
47430 …href="https://babeljs.io/">Babel</a> or <a href="https://github.com/standard-things/esm#readme"><c…
47442 newest version of the package would only be usable in ES module-supporting
47464 <pre><code class="language-json"><span class="hljs-comment">// ./node_modules/pkg/package.json</spa…
47465 <span class="hljs-punctuation">{</span>
47466 …<span class="hljs-attr">"type"</span><span class="hljs-punctuation">:</span> <span class="hljs-str…
47467 …an class="hljs-attr">"main"</span><span class="hljs-punctuation">:</span> <span class="hljs-string…
47468 …<span class="hljs-attr">"exports"</span><span class="hljs-punctuation">:</span> <span class="hljs-
47469 … class="hljs-attr">"import"</span><span class="hljs-punctuation">:</span> <span class="hljs-string…
47470 …<span class="hljs-attr">"require"</span><span class="hljs-punctuation">:</span> <span class="hljs-
47471 <span class="hljs-punctuation">}</span>
47472 <span class="hljs-punctuation">}</span></code></pre>
47478 <pre><code class="language-js cjs"><span class="hljs-comment">// ./node_modules/pkg/index.cjs</span>
47479 <span class="hljs-built_in">exports</span>.<span class="hljs-property">name</span> = <span class="h…
47480 <pre><code class="language-js"><span class="hljs-comment">// ./node_modules/pkg/wrapper.mjs</span>
47481 <span class="hljs-keyword">import</span> cjsModule <span class="hljs-keyword">from</span> <span cla…
47482 <span class="hljs-keyword">export</span> <span class="hljs-keyword">const</span> name = cjsModule.<…
47492 …code class="language-js"><span class="hljs-keyword">import</span> cjsModule <span class="hljs-keyw…
47493 <span class="hljs-keyword">export</span> <span class="hljs-keyword">const</span> name = cjsModule.<…
47494 <span class="hljs-keyword">export</span> <span class="hljs-keyword">default</span> cjsModule;</code…
47502 package is used directly in an application, and a <code>utilities-plus</code> package
47504 underlying CommonJS files, it doesn’t matter if <code>utilities-plus</code> is written in
47510 be to add an export, e.g. <code>"./module"</code>, to point to an all-ES module-syntax
47516 <pre><code class="language-json"><span class="hljs-comment">// ./node_modules/pkg/package.json</spa…
47517 <span class="hljs-punctuation">{</span>
47518 …<span class="hljs-attr">"type"</span><span class="hljs-punctuation">:</span> <span class="hljs-str…
47519 …an class="hljs-attr">"main"</span><span class="hljs-punctuation">:</span> <span class="hljs-string…
47520 …<span class="hljs-attr">"exports"</span><span class="hljs-punctuation">:</span> <span class="hljs-
47521 …span class="hljs-attr">"."</span><span class="hljs-punctuation">:</span> <span class="hljs-string"…
47522 …<span class="hljs-attr">"./module"</span><span class="hljs-punctuation">:</span> <span class="hljs
47523 <span class="hljs-punctuation">}</span>
47524 <span class="hljs-punctuation">}</span></code></pre>
47528 <pre><code class="language-json"><span class="hljs-comment">// ./node_modules/pkg/package.json</spa…
47529 <span class="hljs-punctuation">{</span>
47530 …<span class="hljs-attr">"type"</span><span class="hljs-punctuation">:</span> <span class="hljs-str…
47531 …an class="hljs-attr">"main"</span><span class="hljs-punctuation">:</span> <span class="hljs-string…
47532 …<span class="hljs-attr">"exports"</span><span class="hljs-punctuation">:</span> <span class="hljs-
47533 …n class="hljs-attr">"import"</span><span class="hljs-punctuation">:</span> <span class="hljs-strin…
47534 …<span class="hljs-attr">"require"</span><span class="hljs-punctuation">:</span> <span class="hljs-
47535 <span class="hljs-punctuation">}</span>
47536 <span class="hljs-punctuation">}</span></code></pre>
47546 present. This would likely cause hard-to-troubleshoot bugs.</p>
47556 …age-js"><span class="hljs-keyword">import</span> <span class="hljs-title class_">Date</span> <span…
47557 <span class="hljs-keyword">const</span> someDate = <span class="hljs-keyword">new</span> <span clas…
47558 <span class="hljs-comment">// someDate contains state; Date does not</span></code></pre>
47560 object, or modify a passed-in object, to keep the state external to the
47567 <pre><code class="language-js cjs"><span class="hljs-comment">// ./node_modules/pkg/index.cjs</span>
47568 <span class="hljs-keyword">const</span> state = <span class="hljs-built_in">require</span>(<span cl…
47569 <span class="hljs-variable language_">module</span>.<span class="hljs-property">exports</span>.<spa…
47570 <pre><code class="language-js"><span class="hljs-comment">// ./node_modules/pkg/index.mjs</span>
47571 <span class="hljs-keyword">import</span> state <span class="hljs-keyword">from</span> <span class="…
47572 <span class="hljs-keyword">export</span> {
47597 <code>"./module"</code>, to point to an all-ES module-syntax version of the package:</p>
47598 <pre><code class="language-json"><span class="hljs-comment">// ./node_modules/pkg/package.json</spa…
47599 <span class="hljs-punctuation">{</span>
47600 …<span class="hljs-attr">"type"</span><span class="hljs-punctuation">:</span> <span class="hljs-str…
47601 …an class="hljs-attr">"main"</span><span class="hljs-punctuation">:</span> <span class="hljs-string…
47602 …<span class="hljs-attr">"exports"</span><span class="hljs-punctuation">:</span> <span class="hljs-
47603 …span class="hljs-attr">"."</span><span class="hljs-punctuation">:</span> <span class="hljs-string"…
47604 …<span class="hljs-attr">"./module"</span><span class="hljs-punctuation">:</span> <span class="hljs
47605 <span class="hljs-punctuation">}</span>
47606 <span class="hljs-punctuation">}</span></code></pre>
47609 as <a href="https://docs.npmjs.com/creating-a-package-json-file">npm</a>) use
47613 <li><a href="#packages_name"><code>"name"</code></a> - Relevant when using named imports within a p…
47615 <li><a href="#packages_main"><code>"main"</code></a> - The default module when loading the package,…
47617 <li><a href="#packages_packagemanager"><code>"packageManager"</code></a> - The package manager reco…
47619 <li><a href="#packages_type"><code>"type"</code></a> - The package type determining whether to load…
47621 <li><a href="#packages_exports"><code>"exports"</code></a> - Package exports and conditional export…
47623 <li><a href="#packages_imports"><code>"imports"</code></a> - Package imports, for use by modules wi…
47634 <td><p>Remove the <code>--experimental-resolve-self</code> option.</p></td></tr>
47639 <li>Type: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_t…
47641 <pre><code class="language-json"><span class="hljs-punctuation">{</span>
47642 …<span class="hljs-attr">"name"</span><span class="hljs-punctuation">:</span> <span class="hljs-str…
47643 <span class="hljs-punctuation">}</span></code></pre>
47648 <a href="#packages_self_referencing_a_package_using_its_name">self-reference</a> a package using it…
47654 <li>Type: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_t…
47656 <pre><code class="language-json"><span class="hljs-punctuation">{</span>
47657 …<span class="hljs-attr">"main"</span><span class="hljs-punctuation">:</span> <span class="hljs-str…
47658 <span class="hljs-punctuation">}</span></code></pre>
47662 …ass="language-js cjs"><span class="hljs-built_in">require</span>(<span class="hljs-string">'./path…
47669 … api_stability_1"><a href="#documentation_stability_index">Stability: 1</a> - Experimental</div><p…
47671 <li>Type: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_t…
47673 <pre><code class="language-json"><span class="hljs-punctuation">{</span>
47674 …<span class="hljs-attr">"packageManager"</span><span class="hljs-punctuation">:</span> <span class…
47675 <span class="hljs-punctuation">}</span></code></pre>
47681 <p>This field is currently experimental and needs to be opted-in; check the
47689 <td><p>Unflag <code>--experimental-modules</code>.</p></td></tr>
47696 <li>Type: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_t…
47701 <code>package.json</code> file contains a top-level field <code>"type"</code> with a value of
47706 <pre><code class="language-json"><span class="hljs-comment">// package.json</span>
47707 <span class="hljs-punctuation">{</span>
47708 …<span class="hljs-attr">"type"</span><span class="hljs-punctuation">:</span> <span class="hljs-str…
47709 <span class="hljs-punctuation">}</span></code></pre>
47710 <pre><code class="language-bash"><span class="hljs-comment"># In same folder as preceding package.j…
47711 node my-app.js <span class="hljs-comment"># Runs as ES module</span></code></pre>
47718 <pre><code class="language-js"><span class="hljs-comment">// my-app.js, part of the same example as…
47719 <span class="hljs-keyword">import</span> <span class="hljs-string">'./startup.js'</span>; <span cla…
47732 <td><p>Remove the <code>--experimental-conditional-exports</code> option.</p></td></tr>
47741-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type">&#x3C;Object></a> | <a href=…
47743 <pre><code class="language-json"><span class="hljs-punctuation">{</span>
47744 …<span class="hljs-attr">"exports"</span><span class="hljs-punctuation">:</span> <span class="hljs-
47745 <span class="hljs-punctuation">}</span></code></pre>
47748 <a href="#packages_self_referencing_a_package_using_its_name">self-reference</a> to its own name. I…
47761 <li>Type: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects…
47763 <pre><code class="language-json"><span class="hljs-comment">// package.json</span>
47764 <span class="hljs-punctuation">{</span>
47765 …<span class="hljs-attr">"imports"</span><span class="hljs-punctuation">:</span> <span class="hljs-
47766 …<span class="hljs-attr">"#dep"</span><span class="hljs-punctuation">:</span> <span class="hljs-pun…
47767 … class="hljs-attr">"node"</span><span class="hljs-punctuation">:</span> <span class="hljs-string">…
47768 …<span class="hljs-attr">"default"</span><span class="hljs-punctuation">:</span> <span class="hljs-
47769 <span class="hljs-punctuation">}</span>
47770 <span class="hljs-punctuation">}</span><span class="hljs-punctuation">,</span>
47771 …<span class="hljs-attr">"dependencies"</span><span class="hljs-punctuation">:</span> <span class="…
47772 …<span class="hljs-attr">"dep-node-native"</span><span class="hljs-punctuation">:</span> <span clas…
47773 <span class="hljs-punctuation">}</span>
47774 <span class="hljs-punctuation">}</span></code></pre>
47781 <!--lint disable maximum-line-length-->
47782 …ty api_stability_2"><a href="#documentation_stability_index">Stability: 2</a> - Stable</div><p></p>
47784 <p>The <code>net</code> module provides an asynchronous network API for creating stream-based
47788 …code class="language-js"><span class="hljs-keyword">const</span> net = <span class="hljs-built_in"…
47796 filesystem pathname. It gets truncated to an OS-dependent length of
47797 <code>sizeof(sockaddr_un.sun_path) - 1</code>. Typical values are 107 bytes on Linux and
47815 <pre><code class="language-js">net.<span class="hljs-title function_">createServer</span>().<span c…
47816-title function_">join</span>(<span class="hljs-string">'\\\\?\\pipe'</span>, process.<span class=…
47829-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | <a href="#ne…
47830 …mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> …
47838-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | <a href="#ne…
47840-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | <a href="#ne…
47841 …mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> …
47850-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | <a href="#ne…
47851 <li><code>prefix</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struc…
47852 must be a value between <code>0</code> and <code>32</code>. For IPv6, this must be between
47854 …mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> …
47862 <li><code>address</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_stru…
47863 …mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> …
47864 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boole…
47868 …de class="language-js"><span class="hljs-keyword">const</span> blockList = <span class="hljs-keywo…
47869 blockList.<span class="hljs-title function_">addAddress</span>(<span class="hljs-string">'123.123.1…
47870 blockList.<span class="hljs-title function_">addRange</span>(<span class="hljs-string">'10.0.0.1'</…
47871-title function_">addSubnet</span>(<span class="hljs-string">'8592:757c:efae:4e45::'</span>, <span…
47873-variable language_">console</span>.<span class="hljs-title function_">log</span>(blockList.<span …
47874-variable language_">console</span>.<span class="hljs-title function_">log</span>(blockList.<span …
47875-variable language_">console</span>.<span class="hljs-title function_">log</span>(blockList.<span …
47877 <span class="hljs-comment">// IPv6 notation for IPv4 addresses works:</span>
47878-variable language_">console</span>.<span class="hljs-title function_">log</span>(blockList.<span …
47879-variable language_">console</span>.<span class="hljs-title function_">log</span>(blockList.<span …
47885 <li>Type: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_t…
47897 <li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference…
47899 …zilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> Th…
47901 <code>'ipv6'</code>.</li>
47902 …ozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> O…
47903 …n-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;number></a> An IPv6 flow-
47904 <li><code>port</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
47913 <li>Type <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_ty…
47920 …mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> …
47927 <li>Type <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_ty…
47934 <li>Type <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_ty…
47946 <li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference…
47948 <li><code>connectionListener</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScrip…
47973 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error…
47989-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type">&#x3C;Object></a> | <a href=…
47993 (useful to find which port was assigned when getting an OS-assigned address):
47997-js"><span class="hljs-keyword">const</span> server = net.<span class="hljs-title function_">creat…
47998 …socket.<span class="hljs-title function_">end</span>(<span class="hljs-string">'goodbye\n'</span>);
47999 ….<span class="hljs-title function_">on</span>(<span class="hljs-string">'error'</span>, <span clas…
48000 <span class="hljs-comment">// Handle errors here.</span>
48001 <span class="hljs-keyword">throw</span> err;
48004 <span class="hljs-comment">// Grab an arbitrary unused port.</span>
48005 server.<span class="hljs-title function_">listen</span>(<span class="hljs-function">() =></span> {
48006-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hlj…
48015 <li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Referenc…
48028 … api_stability_0"><a href="#documentation_stability_index">Stability: 0</a> - Deprecated: Use <a h…
48030 …zilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;integer></a> |…
48041 <li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Referenc…
48051 <!--lint disable no-undefined-references-->
48063 <!--lint enable no-undefined-references-->
48071 …re set to <code>SO_REUSEADDR</code> (see <a href="https://man7.org/linux/man-pages/man7/socket.7.h…
48080 …age-js">server.<span class="hljs-title function_">on</span>(<span class="hljs-string">'error'</spa…
48081 …<span class="hljs-keyword">if</span> (e.<span class="hljs-property">code</span> === <span class="h…
48082 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>…
48083 <span class="hljs-built_in">setTimeout</span>(<span class="hljs-function">() =></span> {
48084 server.<span class="hljs-title function_">close</span>();
48085 …server.<span class="hljs-title function_">listen</span>(<span class="hljs-variable constant_">PORT…
48086 }, <span class="hljs-number">1000</span>);
48094 <li><code>handle</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/…
48095 <li><code>backlog</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_stru…
48096 <li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Referenc…
48118 <li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference…
48120 <li><code>port</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
48121 <li><code>host</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
48122 <li><code>path</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
48124 <li><code>backlog</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_stru…
48126 <li><code>exclusive</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_st…
48127 <li><code>readableAll</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_…
48129 <li><code>writableAll</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_…
48131 <li><code>ipv6Only</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_str…
48132 disable dual-stack support, i.e., binding to host <code>::</code> won't make
48136 <li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Referenc…
48140 <!--lint disable no-undefined-references-->
48147 <!--lint enable no-undefined-references-->
48153 <pre><code class="language-js">server.<span class="hljs-title function_">listen</span>({
48154 <span class="hljs-attr">host</span>: <span class="hljs-string">'localhost'</span>,
48155 <span class="hljs-attr">port</span>: <span class="hljs-number">80</span>,
48156 <span class="hljs-attr">exclusive</span>: <span class="hljs-literal">true</span>
48166 <li><code>path</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
48168 <li><code>backlog</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_stru…
48169 <li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Referenc…
48178 <li><code>port</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
48179 <li><code>host</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
48180 <li><code>backlog</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_stru…
48181 <li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Referenc…
48189 …rg/wiki/IPv6_address#Unspecified_address">unspecified IPv6 address</a> (<code>::</code>) when IPv6
48191 …tps://en.wikipedia.org/wiki/IPv6_address#Unspecified_address">unspecified IPv6 address</a> (<code>…
48199 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" …
48206 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" c…
48254 <li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference…
48256 <li><code>fd</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structure…
48258 <li><code>allowHalfOpen</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Dat…
48262 <li><code>readable</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_str…
48264 <li><code>writable</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_str…
48278 <li><code>hadError</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_str…
48293 …" class="type">&#x3C;Buffer></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScrip…
48323 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error…
48342 …illa.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error" class="type">&#x3C;Error></a> |…
48343 <li><code>address</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_stru…
48344 …zilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | …
48345 <li><code>host</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
48365 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Obje…
48374 … api_stability_0"><a href="#documentation_stability_index">Stability: 0</a> - Deprecated: Use <a h…
48376 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" c…
48395 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" c…
48403 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" c…
48443 <li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference…
48444 <li><code>connectListener</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/R…
48453 <li><code>port</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
48454 <li><code>host</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
48455 <li><code>localAddress</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data…
48456 <li><code>localPort</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_st…
48457 <li><code>family</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struc…
48458 <code>0</code> indicates that both IPv4 and IPv6 addresses are allowed. <strong>Default:</strong> <…
48459 <li><code>hints</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struct…
48460 <li><code>lookup</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/…
48464 <li><code>path</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
48465 …paths_for_ipc_connections">Identifying paths for IPC connections</a>. If provided, the TCP-specific
48470 <li><code>onread</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/…
48477 …org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Uint8Array" class="type">&#x3C;Uint8Array><…
48479 <li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Referenc…
48488 …code class="language-js"><span class="hljs-keyword">const</span> net = <span class="hljs-built_in"…
48489 net.<span class="hljs-title function_">connect</span>({
48490 <span class="hljs-attr">port</span>: <span class="hljs-number">80</span>,
48491 <span class="hljs-attr">onread</span>: {
48492 <span class="hljs-comment">// Reuses a 4KiB Buffer for every read from the socket.</span>
48493-attr">buffer</span>: <span class="hljs-title class_">Buffer</span>.<span class="hljs-title functi…
48494 …<span class="hljs-attr">callback</span>: <span class="hljs-keyword">function</span>(<span class="h…
48495 <span class="hljs-comment">// Received data is available in `buf` from 0 to `nread`.</span>
48496-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf.<span class=…
48502 <li><code>path</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
48504 <li><code>connectListener</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/R…
48517 <li><code>port</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
48518 <li><code>host</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
48519 <li><code>connectListener</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/R…
48532 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" …
48546 <li><code>error</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/G…
48554 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" …
48563-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | <a href="#bu…
48564 <li><code>encoding</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_str…
48565 <li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Referenc…
48568 <p>Half-closes the socket. i.e., it sends a FIN packet. It is possible the
48576 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" c…
48587 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" c…
48601 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" …
48621 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" c…
48631 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" c…
48633 <p>The string representation of the remote IP family. <code>'IPv4'</code> or <code>'IPv6'</code>.</…
48639 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" c…
48652 <li><code>encoding</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_str…
48670 <li><code>enable</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struc…
48671 <li><code>initialDelay</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data…
48674 <p>Enable/disable keep-alive functionality, and optionally set the initial
48680 <p>Enabling the keep-alive functionality will set the following socket options:</p>
48692 <li><code>noDelay</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_stru…
48707 <li><code>timeout</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_stru…
48708 <li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Referenc…
48716 <pre><code class="language-js">socket.<span class="hljs-built_in">setTimeout</span>(<span class="hl…
48717 socket.<span class="hljs-title function_">on</span>(<span class="hljs-string">'timeout'</span>, <sp…
48718 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>…
48719 socket.<span class="hljs-title function_">end</span>();
48722 <p>The optional <code>callback</code> parameter will be added as a one-time listener for the
48729 …zilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;number></a> | …
48748-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | <a href="#bu…
48749 <li><code>encoding</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_str…
48750 <li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Referenc…
48751 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boole…
48767 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" c…
48792 <li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference…
48793 <li><code>connectListener</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/R…
48803 <li><code>path</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
48804 <li><code>connectListener</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/R…
48814 <li><code>port</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
48815 <li><code>host</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
48816 <li><code>connectListener</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/R…
48842 <li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference…
48846 <li><code>connectListener</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/R…
48856 <li><code>timeout</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_stru…
48862 …code class="language-js"><span class="hljs-keyword">const</span> net = <span class="hljs-built_in"…
48863-keyword">const</span> client = net.<span class="hljs-title function_">createConnection</span>({ <…
48864 <span class="hljs-comment">// 'connect' listener.</span>
48865 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>…
48866 …client.<span class="hljs-title function_">write</span>(<span class="hljs-string">'world!\r\n'</spa…
48868 ….<span class="hljs-title function_">on</span>(<span class="hljs-string">'data'</span>, <span class…
48869 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>…
48870 client.<span class="hljs-title function_">end</span>();
48872 client.<span class="hljs-title function_">on</span>(<span class="hljs-string">'end'</span>, <span c…
48873 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>…
48876-js"><span class="hljs-keyword">const</span> client = net.<span class="hljs-title function_">creat…
48882 <li><code>path</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
48885 <li><code>connectListener</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/R…
48901 <li><code>port</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
48903 <li><code>host</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
48906 <li><code>connectListener</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/R…
48922 <li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference…
48924 <li><code>allowHalfOpen</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Dat…
48927 <li><code>pauseOnConnect</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Da…
48931 <li><code>connectionListener</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScrip…
48941 connection is half-closed (non-readable but still writable). See <a href="#net_event_end"><code>'en…
48952 …code class="language-js"><span class="hljs-keyword">const</span> net = <span class="hljs-built_in"…
48953 …s="hljs-keyword">const</span> server = net.<span class="hljs-title function_">createServer</span>(…
48954 <span class="hljs-comment">// 'connection' listener.</span>
48955 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>…
48956 …c.<span class="hljs-title function_">on</span>(<span class="hljs-string">'end'</span>, <span class…
48957 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>…
48959 c.<span class="hljs-title function_">write</span>(<span class="hljs-string">'hello\r\n'</span>);
48960 c.<span class="hljs-title function_">pipe</span>(c);
48962 ….<span class="hljs-title function_">on</span>(<span class="hljs-string">'error'</span>, <span clas…
48963 <span class="hljs-keyword">throw</span> err;
48965 server.<span class="hljs-title function_">listen</span>(<span class="hljs-number">8124</span>, <spa…
48966 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>…
48969 <pre><code class="language-console"><span class="hljs-meta">$ </span><span class="language-bash">te…
48971 …s="language-js">server.<span class="hljs-title function_">listen</span>(<span class="hljs-string">…
48972 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>…
48975 <pre><code class="language-console"><span class="hljs-meta">$ </span><span class="language-bash">nc…
48981 <li><code>input</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struct…
48982 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Numbe…
48992 <li><code>input</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struct…
48993 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boole…
49001 <li><code>input</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struct…
49002 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boole…
49008 …ty api_stability_2"><a href="#documentation_stability_index">Stability: 2</a> - Stable</div><p></p>
49010 <p>The <code>os</code> module provides operating system-related utility methods and
49012 …<code class="language-js"><span class="hljs-keyword">const</span> os = <span class="hljs-built_in"…
49018 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" c…
49020 <p>The operating system-specific end-of-line marker.</p>
49030 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Strin…
49041 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Objec…
49043 <p>Contains commonly used operating system-specific constants for error codes,
49051 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Obje…
49056 <li><code>model</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struct…
49057 <li><code>speed</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struct…
49058 <li><code>times</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/G…
49060 <li><code>user</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
49061 <li><code>nice</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
49062 <li><code>sys</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structur…
49063 <li><code>idle</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
49064 <li><code>irq</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structur…
49068 <!-- eslint-disable semi -->
49069 <pre><code class="language-js">[
49071 …<span class="hljs-attr">model</span>: <span class="hljs-string">'Intel(R) Core(TM) i7 CPU …
49072 <span class="hljs-attr">speed</span>: <span class="hljs-number">2926</span>,
49073 <span class="hljs-attr">times</span>: {
49074 <span class="hljs-attr">user</span>: <span class="hljs-number">252020</span>,
49075 <span class="hljs-attr">nice</span>: <span class="hljs-number">0</span>,
49076 <span class="hljs-attr">sys</span>: <span class="hljs-number">30340</span>,
49077 <span class="hljs-attr">idle</span>: <span class="hljs-number">1070356870</span>,
49078 <span class="hljs-attr">irq</span>: <span class="hljs-number">0</span>
49082 …<span class="hljs-attr">model</span>: <span class="hljs-string">'Intel(R) Core(TM) i7 CPU …
49083 <span class="hljs-attr">speed</span>: <span class="hljs-number">2926</span>,
49084 <span class="hljs-attr">times</span>: {
49085 <span class="hljs-attr">user</span>: <span class="hljs-number">306960</span>,
49086 <span class="hljs-attr">nice</span>: <span class="hljs-number">0</span>,
49087 <span class="hljs-attr">sys</span>: <span class="hljs-number">26980</span>,
49088 <span class="hljs-attr">idle</span>: <span class="hljs-number">1071569080</span>,
49089 <span class="hljs-attr">irq</span>: <span class="hljs-number">0</span>
49093 …<span class="hljs-attr">model</span>: <span class="hljs-string">'Intel(R) Core(TM) i7 CPU …
49094 <span class="hljs-attr">speed</span>: <span class="hljs-number">2926</span>,
49095 <span class="hljs-attr">times</span>: {
49096 <span class="hljs-attr">user</span>: <span class="hljs-number">248450</span>,
49097 <span class="hljs-attr">nice</span>: <span class="hljs-number">0</span>,
49098 <span class="hljs-attr">sys</span>: <span class="hljs-number">21750</span>,
49099 <span class="hljs-attr">idle</span>: <span class="hljs-number">1070919370</span>,
49100 <span class="hljs-attr">irq</span>: <span class="hljs-number">0</span>
49104 …<span class="hljs-attr">model</span>: <span class="hljs-string">'Intel(R) Core(TM) i7 CPU …
49105 <span class="hljs-attr">speed</span>: <span class="hljs-number">2926</span>,
49106 <span class="hljs-attr">times</span>: {
49107 <span class="hljs-attr">user</span>: <span class="hljs-number">256880</span>,
49108 <span class="hljs-attr">nice</span>: <span class="hljs-number">0</span>,
49109 <span class="hljs-attr">sys</span>: <span class="hljs-number">19430</span>,
49110 <span class="hljs-attr">idle</span>: <span class="hljs-number">1070905480</span>,
49111 <span class="hljs-attr">irq</span>: <span class="hljs-number">20</span>
49115 <p><code>nice</code> values are POSIX-only. On Windows, the <code>nice</code> values of all process…
49122 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" c…
49124 <p>The platform-specific file path of the null device.</p>
49134 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Strin…
49144 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Numbe…
49152 <li><code>pid</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structur…
49154 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Numbe…
49163 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Strin…
49175 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Strin…
49183 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Numbe…
49188 <p>The load average is a Unix-specific concept. On Windows, the return value is
49195 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Obje…
49203 …veloper.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;stri…
49204 …//developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;…
49205 ….mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a>…
49206 <li><code>mac</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structur…
49207 <li><code>internal</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_str…
49209 …//developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;…
49210 is <code>IPv6</code>)</li>
49211 …veloper.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;stri…
49215 <!-- eslint-skip -->
49216 <pre><code class="language-js">{
49217 <span class="hljs-attr">lo</span>: [
49219 <span class="hljs-attr">address</span>: <span class="hljs-string">'127.0.0.1'</span>,
49220 <span class="hljs-attr">netmask</span>: <span class="hljs-string">'255.0.0.0'</span>,
49221 <span class="hljs-attr">family</span>: <span class="hljs-string">'IPv4'</span>,
49222 <span class="hljs-attr">mac</span>: <span class="hljs-string">'00:00:00:00:00:00'</span>,
49223 <span class="hljs-attr">internal</span>: <span class="hljs-literal">true</span>,
49224 <span class="hljs-attr">cidr</span>: <span class="hljs-string">'127.0.0.1/8'</span>
49227 <span class="hljs-attr">address</span>: <span class="hljs-string">'::1'</span>,
49228 …<span class="hljs-attr">netmask</span>: <span class="hljs-string">'ffff:ffff:ffff:ffff:ffff:ffff:f…
49229 <span class="hljs-attr">family</span>: <span class="hljs-string">'IPv6'</span>,
49230 <span class="hljs-attr">mac</span>: <span class="hljs-string">'00:00:00:00:00:00'</span>,
49231 <span class="hljs-attr">scopeid</span>: <span class="hljs-number">0</span>,
49232 <span class="hljs-attr">internal</span>: <span class="hljs-literal">true</span>,
49233 <span class="hljs-attr">cidr</span>: <span class="hljs-string">'::1/128'</span>
49236 <span class="hljs-attr">eth0</span>: [
49238 <span class="hljs-attr">address</span>: <span class="hljs-string">'192.168.1.108'</span>,
49239 <span class="hljs-attr">netmask</span>: <span class="hljs-string">'255.255.255.0'</span>,
49240 <span class="hljs-attr">family</span>: <span class="hljs-string">'IPv4'</span>,
49241 <span class="hljs-attr">mac</span>: <span class="hljs-string">'01:02:03:0a:0b:0c'</span>,
49242 <span class="hljs-attr">internal</span>: <span class="hljs-literal">false</span>,
49243 <span class="hljs-attr">cidr</span>: <span class="hljs-string">'192.168.1.108/24'</span>
49246 …<span class="hljs-attr">address</span>: <span class="hljs-string">'fe80::a00:27ff:fe4e:66a1'</span…
49247 … <span class="hljs-attr">netmask</span>: <span class="hljs-string">'ffff:ffff:ffff:ffff::'</span>,
49248 <span class="hljs-attr">family</span>: <span class="hljs-string">'IPv6'</span>,
49249 <span class="hljs-attr">mac</span>: <span class="hljs-string">'01:02:03:0a:0b:0c'</span>,
49250 <span class="hljs-attr">scopeid</span>: <span class="hljs-number">1</span>,
49251 <span class="hljs-attr">internal</span>: <span class="hljs-literal">false</span>,
49252 …<span class="hljs-attr">cidr</span>: <span class="hljs-string">'fe80::a00:27ff:fe4e:66a1/64'</span>
49261 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Strin…
49268 …s://github.com/nodejs/node/blob/HEAD/BUILDING.md#androidandroid-based-devices-eg-firefox-os">Andro…
49274 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Strin…
49285 <li><code>pid</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structur…
49287 <li><code>priority</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_str…
49291 <p>The <code>priority</code> input must be an integer between <code>-20</code> (high priority) and …
49306 <td><p>This function is now cross-platform consistent and no longer returns a path with a trailing …
49313 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Strin…
49322 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Numbe…
49330 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Strin…
49349 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Numbe…
49357 <li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference…
49359 <li><code>encoding</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_str…
49364 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Obje…
49369 <code>gid</code> fields are <code>-1</code>, and <code>shell</code> is <code>null</code>.</p>
49380 <li>Returns <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String…
49453 <td>Sent to a process to identify user-defined conditions.</td>
49893 <h5>Windows-specific error constants<span><a class="mark" href="#os_windows_specific_error_constant…
50137 are exported in <code>os.constants.dlopen</code>. See <a href="http://man7.org/linux/man-pages/man3…
50165 <td>Make a self-contained library use its own symbols in preference to
50204 <code>-7</code> on all other platforms.</td>
50211 <code>-14</code> on all other platforms.</td>
50217 <code>-20</code> on all other platforms.</td>
50234 …ty api_stability_2"><a href="#documentation_stability_index">Stability: 2</a> - Stable</div><p></p>
50238 …code class="language-js"><span class="hljs-keyword">const</span> path = <span class="hljs-built_in…
50242 Windows operating system, the <code>path</code> module will assume that Windows-style
50246 <pre><code class="language-js">path.<span class="hljs-title function_">basename</span>(<span class=…
50247 <span class="hljs-comment">// Returns: 'C:\\temp\\myfile.html'</span></code></pre>
50249 <pre><code class="language-js">path.<span class="hljs-title function_">basename</span>(<span class=…
50250 <span class="hljs-comment">// Returns: 'myfile.html'</span></code></pre>
50254 … class="language-js">path.<span class="hljs-property">win32</span>.<span class="hljs-title functio…
50255 <span class="hljs-comment">// Returns: 'myfile.html'</span></code></pre>
50259 … class="language-js">path.<span class="hljs-property">posix</span>.<span class="hljs-title functio…
50260 <span class="hljs-comment">// Returns: 'myfile.html'</span></code></pre>
50261 <p>On Windows Node.js follows the concept of per-drive working directory.
50265 <a href="https://docs.microsoft.com/en-us/windows/desktop/FileIO/naming-a-file#fully-qualified-vs-r…
50272 <td><p>Passing a non-string as the <code>path</code> argument will throw now.</p></td></tr>
50279 <li><code>path</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
50280 <li><code>ext</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structur…
50281 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Strin…
50286 <pre><code class="language-js">path.<span class="hljs-title function_">basename</span>(<span class=…
50287 <span class="hljs-comment">// Returns: 'quux.html'</span>
50289 ….<span class="hljs-title function_">basename</span>(<span class="hljs-string">'/foo/bar/baz/asdf/q…
50290 <span class="hljs-comment">// Returns: 'quux'</span></code></pre>
50292 case-insensitive manner, this function does not. For example, <code>C:\\foo.html</code> and
50294 case-sensitive string:</p>
50295-js">path.<span class="hljs-property">win32</span>.<span class="hljs-title function_">basename</sp…
50296 <span class="hljs-comment">// Returns: 'foo'</span>
50298 …"hljs-property">win32</span>.<span class="hljs-title function_">basename</span>(<span class="hljs-
50299 <span class="hljs-comment">// Returns: 'foo.HTML'</span></code></pre>
50307 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" c…
50309 <p>Provides the platform-specific path delimiter:</p>
50315-js"><span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</…
50316 <span class="hljs-comment">// Prints: '/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin'</span>
50318 …lass="hljs-property">env</span>.<span class="hljs-property">PATH</span>.<span class="hljs-title fu…
50319 <span class="hljs-comment">// Returns: ['/usr/bin', '/bin', '/usr/sbin', '/sbin', '/usr/local/bin']…
50321-js"><span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</…
50322 <span class="hljs-comment">// Prints: 'C:\Windows\system32;C:\Windows;C:\Program Files\node\'</span>
50324 …lass="hljs-property">env</span>.<span class="hljs-property">PATH</span>.<span class="hljs-title fu…
50325 <span class="hljs-comment">// Returns ['C:\\Windows\\system32', 'C:\\Windows', 'C:\\Program Files\\…
50332 <td><p>Passing a non-string as the <code>path</code> argument will throw now.</p></td></tr>
50339 <li><code>path</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
50340 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Strin…
50345 <pre><code class="language-js">path.<span class="hljs-title function_">dirname</span>(<span class="…
50346 <span class="hljs-comment">// Returns: '/foo/bar/baz/asdf'</span></code></pre>
50354 <td><p>Passing a non-string as the <code>path</code> argument will throw now.</p></td></tr>
50361 <li><code>path</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
50362 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Strin…
50369 <pre><code class="language-js">path.<span class="hljs-title function_">extname</span>(<span class="…
50370 <span class="hljs-comment">// Returns: '.html'</span>
50372 path.<span class="hljs-title function_">extname</span>(<span class="hljs-string">'index.coffee.md'<…
50373 <span class="hljs-comment">// Returns: '.md'</span>
50375 path.<span class="hljs-title function_">extname</span>(<span class="hljs-string">'index.'</span>);
50376 <span class="hljs-comment">// Returns: '.'</span>
50378 path.<span class="hljs-title function_">extname</span>(<span class="hljs-string">'index'</span>);
50379 <span class="hljs-comment">// Returns: ''</span>
50381 path.<span class="hljs-title function_">extname</span>(<span class="hljs-string">'.index'</span>);
50382 <span class="hljs-comment">// Returns: ''</span>
50384 path.<span class="hljs-title function_">extname</span>(<span class="hljs-string">'.index.md'</span>…
50385 <span class="hljs-comment">// Returns: '.md'</span></code></pre>
50392 <li><code>pathObject</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Refere…
50394 <li><code>dir</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structur…
50395 <li><code>root</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
50396 <li><code>base</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
50397 <li><code>name</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
50398 <li><code>ext</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structur…
50401 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Strin…
50412 <pre><code class="language-js"><span class="hljs-comment">// If `dir`, `root` and `base` are provid…
50413 <span class="hljs-comment">// `${dir}${path.sep}${base}`</span>
50414 <span class="hljs-comment">// will be returned. `root` is ignored.</span>
50415 path.<span class="hljs-title function_">format</span>({
50416 <span class="hljs-attr">root</span>: <span class="hljs-string">'/ignored'</span>,
50417 <span class="hljs-attr">dir</span>: <span class="hljs-string">'/home/user/dir'</span>,
50418 <span class="hljs-attr">base</span>: <span class="hljs-string">'file.txt'</span>
50420 <span class="hljs-comment">// Returns: '/home/user/dir/file.txt'</span>
50422 <span class="hljs-comment">// `root` will be used if `dir` is not specified.</span>
50423 <span class="hljs-comment">// If only `root` is provided or `dir` is equal to `root` then the</span>
50424 <span class="hljs-comment">// platform separator will not be included. `ext` will be ignored.</span>
50425 path.<span class="hljs-title function_">format</span>({
50426 <span class="hljs-attr">root</span>: <span class="hljs-string">'/'</span>,
50427 <span class="hljs-attr">base</span>: <span class="hljs-string">'file.txt'</span>,
50428 <span class="hljs-attr">ext</span>: <span class="hljs-string">'ignored'</span>
50430 <span class="hljs-comment">// Returns: '/file.txt'</span>
50432 <span class="hljs-comment">// `name` + `ext` will be used if `base` is not specified.</span>
50433 path.<span class="hljs-title function_">format</span>({
50434 <span class="hljs-attr">root</span>: <span class="hljs-string">'/'</span>,
50435 <span class="hljs-attr">name</span>: <span class="hljs-string">'file'</span>,
50436 <span class="hljs-attr">ext</span>: <span class="hljs-string">'.txt'</span>
50438 <span class="hljs-comment">// Returns: '/file.txt'</span></code></pre>
50440 <pre><code class="language-js">path.<span class="hljs-title function_">format</span>({
50441 <span class="hljs-attr">dir</span>: <span class="hljs-string">'C:\\path\\dir'</span>,
50442 <span class="hljs-attr">base</span>: <span class="hljs-string">'file.txt'</span>
50444 <span class="hljs-comment">// Returns: 'C:\\path\\dir\\file.txt'</span></code></pre>
50450 <li><code>path</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
50451 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boole…
50454 <p>If the given <code>path</code> is a zero-length string, <code>false</code> will be returned.</p>
50456 …ass="language-js">path.<span class="hljs-title function_">isAbsolute</span>(<span class="hljs-stri…
50457 path.<span class="hljs-title function_">isAbsolute</span>(<span class="hljs-string">'/baz/..'</span…
50458 path.<span class="hljs-title function_">isAbsolute</span>(<span class="hljs-string">'qux/'</span>);…
50459 path.<span class="hljs-title function_">isAbsolute</span>(<span class="hljs-string">'.'</span>); …
50461 …s="language-js">path.<span class="hljs-title function_">isAbsolute</span>(<span class="hljs-string…
50462 path.<span class="hljs-title function_">isAbsolute</span>(<span class="hljs-string">'\\\\server'</s…
50463 path.<span class="hljs-title function_">isAbsolute</span>(<span class="hljs-string">'C:/foo/..'</sp…
50464 path.<span class="hljs-title function_">isAbsolute</span>(<span class="hljs-string">'C:\\foo\\..'</…
50465 path.<span class="hljs-title function_">isAbsolute</span>(<span class="hljs-string">'bar\\baz'</spa…
50466 path.<span class="hljs-title function_">isAbsolute</span>(<span class="hljs-string">'bar/baz'</span…
50467 path.<span class="hljs-title function_">isAbsolute</span>(<span class="hljs-string">'.'</span>); …
50474 <li><code>...paths</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_str…
50475 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Strin…
50478 platform-specific separator as a delimiter, then normalizes the resulting path.</p>
50479 <p>Zero-length <code>path</code> segments are ignored. If the joined path string is a
50480 zero-length string then <code>'.'</code> will be returned, representing the current
50482-js">path.<span class="hljs-title function_">join</span>(<span class="hljs-string">'/foo'</span>, …
50483 <span class="hljs-comment">// Returns: '/foo/bar/baz/asdf'</span>
50485 path.<span class="hljs-title function_">join</span>(<span class="hljs-string">'foo'</span>, {}, <sp…
50486 <span class="hljs-comment">// Throws 'TypeError: Path must be a string. Received {}'</span></code><…
50493 <li><code>path</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
50494 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Strin…
50500 instance of the platform-specific path segment separator (<code>/</code> on POSIX and
50502 <p>If the <code>path</code> is a zero-length string, <code>'.'</code> is returned, representing the
50505 <pre><code class="language-js">path.<span class="hljs-title function_">normalize</span>(<span class…
50506 <span class="hljs-comment">// Returns: '/foo/bar/baz/asdf'</span></code></pre>
50508 <pre><code class="language-js">path.<span class="hljs-title function_">normalize</span>(<span class…
50509 <span class="hljs-comment">// Returns: 'C:\\temp\\foo\\'</span></code></pre>
50512 … class="language-js">path.<span class="hljs-property">win32</span>.<span class="hljs-title functio…
50513 <span class="hljs-comment">// Returns: 'C:\\temp\\foo\\bar'</span></code></pre>
50520 <li><code>path</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
50521 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Obje…
50528 <li><code>dir</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structur…
50529 <li><code>root</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
50530 <li><code>base</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
50531 <li><code>name</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
50532 <li><code>ext</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structur…
50535 <pre><code class="language-js">path.<span class="hljs-title function_">parse</span>(<span class="hl…
50536 <span class="hljs-comment">// Returns:</span>
50537 <span class="hljs-comment">// { root: '/',</span>
50538 <span class="hljs-comment">// dir: '/home/user/dir',</span>
50539 <span class="hljs-comment">// base: 'file.txt',</span>
50540 <span class="hljs-comment">// ext: '.txt',</span>
50541 <span class="hljs-comment">// name: 'file' }</span></code></pre>
50542 <pre><code class="language-text">┌─────────────────────┬────────────┐
50550 <pre><code class="language-js">path.<span class="hljs-title function_">parse</span>(<span class="hl…
50551 <span class="hljs-comment">// Returns:</span>
50552 <span class="hljs-comment">// { root: 'C:\\',</span>
50553 <span class="hljs-comment">// dir: 'C:\\path\\dir',</span>
50554 <span class="hljs-comment">// base: 'file.txt',</span>
50555 <span class="hljs-comment">// ext: '.txt',</span>
50556 <span class="hljs-comment">// name: 'file' }</span></code></pre>
50557 <pre><code class="language-text">┌─────────────────────┬────────────┐
50570 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Objec…
50587 <li><code>from</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
50588 <li><code>to</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structure…
50589 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Strin…
50593 path (after calling <code>path.resolve()</code> on each), a zero-length string is returned.</p>
50594 <p>If a zero-length string is passed as <code>from</code> or <code>to</code>, the current working
50595 directory will be used instead of the zero-length strings.</p>
50597 …anguage-js">path.<span class="hljs-title function_">relative</span>(<span class="hljs-string">'/da…
50598 <span class="hljs-comment">// Returns: '../../impl/bbb'</span></code></pre>
50600 …anguage-js">path.<span class="hljs-title function_">relative</span>(<span class="hljs-string">'C:\…
50601 <span class="hljs-comment">// Returns: '..\\..\\impl\\bbb'</span></code></pre>
50608 <li><code>...paths</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_str…
50609 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Strin…
50622 <p>Zero-length <code>path</code> segments are ignored.</p>
50625 …class="language-js">path.<span class="hljs-title function_">resolve</span>(<span class="hljs-strin…
50626 <span class="hljs-comment">// Returns: '/foo/bar/baz'</span>
50628 path.<span class="hljs-title function_">resolve</span>(<span class="hljs-string">'/foo/bar'</span>,…
50629 <span class="hljs-comment">// Returns: '/tmp/file'</span>
50631 …js-title function_">resolve</span>(<span class="hljs-string">'wwwroot'</span>, <span class="hljs-s…
50632 <span class="hljs-comment">// If the current working directory is /home/myself/node,</span>
50633 <span class="hljs-comment">// this returns '/home/myself/node/wwwroot/static_files/gif/image.gif'</…
50640 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" c…
50642 <p>Provides the platform-specific path segment separator:</p>
50648 …class="language-js"><span class="hljs-string">'foo/bar/baz'</span>.<span class="hljs-title functio…
50649 <span class="hljs-comment">// Returns: ['foo', 'bar', 'baz']</span></code></pre>
50651 …lass="language-js"><span class="hljs-string">'foo\\bar\\baz'</span>.<span class="hljs-title functi…
50652 <span class="hljs-comment">// Returns: ['foo', 'bar', 'baz']</span></code></pre>
50661 <li><code>path</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
50662 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Strin…
50664 …valent <a href="https://docs.microsoft.com/en-us/windows/desktop/FileIO/naming-a-file#namespaces">…
50668 method is non-operational and always returns <code>path</code> without modifications.</p>
50674 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Objec…
50676 <p>The <code>path.win32</code> property provides access to Windows-specific implementations
50681 …ty api_stability_2"><a href="#documentation_stability_index">Stability: 2</a> - Stable</div><p></p>
50684 <a href="https://w3c.github.io/perf-timing-primer/">Web Performance APIs</a> as well as additional …
50685 Node.js-specific performance measurements.</p>
50686 <p>Node.js supports the following <a href="https://w3c.github.io/perf-timing-primer/">Web Performan…
50688 <li><a href="https://www.w3.org/TR/hr-time-2">High Resolution Time</a></li>
50689 <li><a href="https://w3c.github.io/performance-timeline/">Performance Timeline</a></li>
50690 <li><a href="https://www.w3.org/TR/user-timing/">User Timing</a></li>
50692-js"><span class="hljs-keyword">const</span> { <span class="hljs-title class_">PerformanceObserver…
50694-keyword">const</span> obs = <span class="hljs-keyword">new</span> <span class="hljs-title class_"…
50695-variable language_">console</span>.<span class="hljs-title function_">log</span>(items.<span clas…
50696 performance.<span class="hljs-title function_">clearMarks</span>();
50698 obs.<span class="hljs-title function_">observe</span>({ <span class="hljs-attr">entryTypes</span>: …
50699 performance.<span class="hljs-title function_">measure</span>(<span class="hljs-string">'Start to N…
50701 performance.<span class="hljs-title function_">mark</span>(<span class="hljs-string">'A'</span>);
50702 <span class="hljs-title function_">doSomeLongRunningProcess</span>(<span class="hljs-function">() =…
50703 …erformance.<span class="hljs-title function_">measure</span>(<span class="hljs-string">'A to Now'<…
50705 performance.<span class="hljs-title function_">mark</span>(<span class="hljs-string">'B'</span>);
50706 …lass="hljs-title function_">measure</span>(<span class="hljs-string">'A to B'</span>, <span class=…
50713 Node.js instance. It is similar to <a href="https://developer.mozilla.org/en-US/docs/Web/API/Window…
50719 <li><code>name</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
50728 <li><code>utilization1</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Refe…
50730 <li><code>utilization2</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Refe…
50732 <li>Returns <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objec…
50734 <li><code>idle</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
50735 <li><code>active</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struc…
50736 <li><code>utilization</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_…
50760 <pre><code class="language-js"><span class="hljs-meta">'use strict'</span>;
50761-keyword">const</span> { eventLoopUtilization } = <span class="hljs-built_in">require</span>(<span…
50762 <span class="hljs-keyword">const</span> { spawnSync } = <span class="hljs-built_in">require</span>(…
50764 <span class="hljs-title function_">setImmediate</span>(<span class="hljs-function">() =></span> {
50765 …<span class="hljs-keyword">const</span> elu = <span class="hljs-title function_">eventLoopUtilizat…
50766 …<span class="hljs-title function_">spawnSync</span>(<span class="hljs-string">'sleep'</span>, [<sp…
50767-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hlj…
50772 <p>Passing in a user-defined object instead of the result of a previous call to
50780 <li><code>name</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
50800 <li><code>name</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
50801 <li><code>startMark</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_st…
50802 <li><code>endMark</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_stru…
50812 …code>startMark</code> is set to <a href="https://w3c.github.io/hr-time/#dom-performance-timeorigin…
50833 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Numbe…
50842 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" c…
50844 <p>The <a href="https://w3c.github.io/hr-time/#dom-performance-timeorigin"><code>timeOrigin</code><…
50851 <li><code>fn</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Glob…
50857 <pre><code class="language-js"><span class="hljs-keyword">const</span> {
50859 <span class="hljs-title class_">PerformanceObserver</span>
50860 } = <span class="hljs-built_in">require</span>(<span class="hljs-string">'perf_hooks'</span>);
50862 <span class="hljs-keyword">function</span> <span class="hljs-title function_">someFunction</span>(<…
50863 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>…
50866 <span class="hljs-keyword">const</span> wrapped = performance.<span class="hljs-title function_">ti…
50868-keyword">const</span> obs = <span class="hljs-keyword">new</span> <span class="hljs-title class_"…
50869-variable language_">console</span>.<span class="hljs-title function_">log</span>(list.<span class…
50870 obs.<span class="hljs-title function_">disconnect</span>();
50872 obs.<span class="hljs-title function_">observe</span>({ <span class="hljs-attr">entryTypes</span>: …
50874 <span class="hljs-comment">// A performance timeline entry will be created</span>
50875 <span class="hljs-title function_">wrapped</span>();</code></pre>
50885 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" c…
50894 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" c…
50911 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" c…
50931 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" c…
50939 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" c…
50956 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" c…
50975 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" c…
50979 has the value of -1.</p>
50985 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" c…
50994 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" c…
51006 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" c…
51009 exited. If the event loop has not yet exited, the property has the value of -1.
51010 It can only have a value of not -1 in a handler of the <a href="#process_event_exit"><code>'exit'</…
51016 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" c…
51020 main script), the property has the value of -1.</p>
51026 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" c…
51035 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" c…
51045 <li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Referenc…
51054 <pre><code class="language-js"><span class="hljs-keyword">const</span> {
51056 <span class="hljs-title class_">PerformanceObserver</span>
51057 } = <span class="hljs-built_in">require</span>(<span class="hljs-string">'perf_hooks'</span>);
51059-keyword">const</span> obs = <span class="hljs-keyword">new</span> <span class="hljs-title class_"…
51060 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>…
51061 observer.<span class="hljs-title function_">disconnect</span>();
51063-title function_">observe</span>({ <span class="hljs-attr">entryTypes</span>: [<span class="hljs-s…
51065 performance.<span class="hljs-title function_">mark</span>(<span class="hljs-string">'test'</span>)…
51084 <li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference…
51086 <li><code>entryTypes</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_s…
51089 <li><code>buffered</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_str…
51100 <pre><code class="language-js"><span class="hljs-keyword">const</span> {
51102 <span class="hljs-title class_">PerformanceObserver</span>
51103 } = <span class="hljs-built_in">require</span>(<span class="hljs-string">'perf_hooks'</span>);
51105-keyword">const</span> obs = <span class="hljs-keyword">new</span> <span class="hljs-title class_"…
51106 <span class="hljs-comment">// Called three times synchronously. `list` contains one item.</span>
51108 obs.<span class="hljs-title function_">observe</span>({ <span class="hljs-attr">entryTypes</span>: …
51110 …n class="hljs-keyword">for</span> (<span class="hljs-keyword">let</span> n = <span class="hljs-num…
51111 …performance.<span class="hljs-title function_">mark</span>(<span class="hljs-string">`test<span cl…
51112 <pre><code class="language-js"><span class="hljs-keyword">const</span> {
51114 <span class="hljs-title class_">PerformanceObserver</span>
51115 } = <span class="hljs-built_in">require</span>(<span class="hljs-string">'perf_hooks'</span>);
51117-keyword">const</span> obs = <span class="hljs-keyword">new</span> <span class="hljs-title class_"…
51118 <span class="hljs-comment">// Called once. `list` contains three items.</span>
51120-title function_">observe</span>({ <span class="hljs-attr">entryTypes</span>: [<span class="hljs-s…
51122 …n class="hljs-keyword">for</span> (<span class="hljs-keyword">let</span> n = <span class="hljs-num…
51123 …performance.<span class="hljs-title function_">mark</span>(<span class="hljs-string">`test<span cl…
51140 <pre><code class="language-js"><span class="hljs-keyword">const</span> {
51142 <span class="hljs-title class_">PerformanceObserver</span>
51143 } = <span class="hljs-built_in">require</span>(<span class="hljs-string">'perf_hooks'</span>);
51145-keyword">const</span> obs = <span class="hljs-keyword">new</span> <span class="hljs-title class_"…
51146 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>…
51147 <span class="hljs-comment">/**
51163 observer.<span class="hljs-title function_">disconnect</span>();
51165-title function_">observe</span>({ <span class="hljs-attr">entryTypes</span>: [<span class="hljs-s…
51167 performance.<span class="hljs-title function_">mark</span>(<span class="hljs-string">'test'</span>);
51168 performance.<span class="hljs-title function_">mark</span>(<span class="hljs-string">'meow'</span>)…
51174 <li><code>name</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
51175 <li><code>type</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
51182 <pre><code class="language-js"><span class="hljs-keyword">const</span> {
51184 <span class="hljs-title class_">PerformanceObserver</span>
51185 } = <span class="hljs-built_in">require</span>(<span class="hljs-string">'perf_hooks'</span>);
51187-keyword">const</span> obs = <span class="hljs-keyword">new</span> <span class="hljs-title class_"…
51188-variable language_">console</span>.<span class="hljs-title function_">log</span>(perfObserverList…
51189 <span class="hljs-comment">/**
51199-variable language_">console</span>.<span class="hljs-title function_">log</span>(perfObserverList…
51201-variable language_">console</span>.<span class="hljs-title function_">log</span>(perfObserverList…
51202 <span class="hljs-comment">/**
51212-variable language_">console</span>.<span class="hljs-title function_">log</span>(perfObserverList…
51213 observer.<span class="hljs-title function_">disconnect</span>();
51215-title function_">observe</span>({ <span class="hljs-attr">entryTypes</span>: [<span class="hljs-s…
51217 performance.<span class="hljs-title function_">mark</span>(<span class="hljs-string">'test'</span>);
51218 performance.<span class="hljs-title function_">mark</span>(<span class="hljs-string">'meow'</span>)…
51224 <li><code>type</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
51230 <pre><code class="language-js"><span class="hljs-keyword">const</span> {
51232 <span class="hljs-title class_">PerformanceObserver</span>
51233 } = <span class="hljs-built_in">require</span>(<span class="hljs-string">'perf_hooks'</span>);
51235-keyword">const</span> obs = <span class="hljs-keyword">new</span> <span class="hljs-title class_"…
51236-variable language_">console</span>.<span class="hljs-title function_">log</span>(perfObserverList…
51237 <span class="hljs-comment">/**
51253 observer.<span class="hljs-title function_">disconnect</span>();
51255-title function_">observe</span>({ <span class="hljs-attr">entryTypes</span>: [<span class="hljs-s…
51257 performance.<span class="hljs-title function_">mark</span>(<span class="hljs-string">'test'</span>);
51258 performance.<span class="hljs-title function_">mark</span>(<span class="hljs-string">'meow'</span>)…
51264 <li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference…
51266 …zilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;number></a> | …
51268 …zilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;number></a> | …
51270 <li><code>figures</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_stru…
51282 <li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference…
51284 <li><code>resolution</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_s…
51298 …"language-js"><span class="hljs-keyword">const</span> { monitorEventLoopDelay } = <span class="hlj…
51299-keyword">const</span> h = <span class="hljs-title function_">monitorEventLoopDelay</span>({ <span…
51300 h.<span class="hljs-title function_">enable</span>();
51301 <span class="hljs-comment">// Do something.</span>
51302 h.<span class="hljs-title function_">disable</span>();
51303 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(…
51304 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(…
51305 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(…
51306 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(…
51307 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(…
51308-variable language_">console</span>.<span class="hljs-title function_">log</span>(h.<span class="h…
51309-variable language_">console</span>.<span class="hljs-title function_">log</span>(h.<span class="h…
51319 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" c…
51328 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" c…
51336 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" c…
51344 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" c…
51352 <li><code>percentile</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_s…
51353 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Numbe…
51361 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Map" …
51374 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" c…
51384 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boole…
51393 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boole…
51410 …zilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;number></a> | …
51423 <pre><code class="language-js"><span class="hljs-meta">'use strict'</span>;
51424 <span class="hljs-keyword">const</span> async_hooks = <span class="hljs-built_in">require</span>(<s…
51425 <span class="hljs-keyword">const</span> {
51427 <span class="hljs-title class_">PerformanceObserver</span>
51428 } = <span class="hljs-built_in">require</span>(<span class="hljs-string">'perf_hooks'</span>);
51430 <span class="hljs-keyword">const</span> set = <span class="hljs-keyword">new</span> <span class="hl…
51431 <span class="hljs-keyword">const</span> hook = async_hooks.<span class="hljs-title function_">creat…
51432 <span class="hljs-title function_">init</span>(<span class="hljs-params">id, type</span>) {
51433 <span class="hljs-keyword">if</span> (type === <span class="hljs-string">'Timeout'</span>) {
51434 …ormance.<span class="hljs-title function_">mark</span>(<span class="hljs-string">`Timeout-<span cl…
51435 set.<span class="hljs-title function_">add</span>(id);
51438 <span class="hljs-title function_">destroy</span>(<span class="hljs-params">id</span>) {
51439 <span class="hljs-keyword">if</span> (set.<span class="hljs-title function_">has</span>(id)) {
51440 set.<span class="hljs-title function_">delete</span>(id);
51441 …ormance.<span class="hljs-title function_">mark</span>(<span class="hljs-string">`Timeout-<span cl…
51442 …performance.<span class="hljs-title function_">measure</span>(<span class="hljs-string">`Timeout-<…
51443 … <span class="hljs-string">`Timeout-<span class="hljs-subst">${id}</span>-Init`</span>,
51444 … <span class="hljs-string">`Timeout-<span class="hljs-subst">${id}</span>-Destroy`</span>);
51448 hook.<span class="hljs-title function_">enable</span>();
51450-keyword">const</span> obs = <span class="hljs-keyword">new</span> <span class="hljs-title class_"…
51451-variable language_">console</span>.<span class="hljs-title function_">log</span>(list.<span class…
51452 performance.<span class="hljs-title function_">clearMarks</span>();
51453 observer.<span class="hljs-title function_">disconnect</span>();
51455-title function_">observe</span>({ <span class="hljs-attr">entryTypes</span>: [<span class="hljs-s…
51457 <span class="hljs-built_in">setTimeout</span>(<span class="hljs-function">() =></span> {}, <span cl…
51461 <!-- eslint-disable no-global-assign -->
51462 <pre><code class="language-js"><span class="hljs-meta">'use strict'</span>;
51463 <span class="hljs-keyword">const</span> {
51465 <span class="hljs-title class_">PerformanceObserver</span>
51466 } = <span class="hljs-built_in">require</span>(<span class="hljs-string">'perf_hooks'</span>);
51467 <span class="hljs-keyword">const</span> mod = <span class="hljs-built_in">require</span>(<span clas…
51469 <span class="hljs-comment">// Monkey patch the require function</span>
51470 …pan class="hljs-property">Module</span>.<span class="hljs-property"><span class="hljs-keyword">pro…
51471-title function_">timerify</span>(mod.<span class="hljs-property">Module</span>.<span class="hljs-
51472 <span class="hljs-built_in">require</span> = performance.<span class="hljs-title function_">timerif…
51474 <span class="hljs-comment">// Activate the observer</span>
51475-keyword">const</span> obs = <span class="hljs-keyword">new</span> <span class="hljs-title class_"…
51476 …<span class="hljs-keyword">const</span> entries = list.<span class="hljs-title function_">getEntri…
51477 …entries.<span class="hljs-title function_">forEach</span>(<span class="hljs-function">(<span class…
51478-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hlj…
51480 obs.<span class="hljs-title function_">disconnect</span>();
51482-title function_">observe</span>({ <span class="hljs-attr">entryTypes</span>: [<span class="hljs-s…
51484 <span class="hljs-built_in">require</span>(<span class="hljs-string">'some-module'</span>);</code><…
51489 … api_stability_1"><a href="#documentation_stability_index">Stability: 1</a> - Experimental</div><p…
51497 <p>A best practice would be to ensure that the policy manifest is read-only for
51504 <p>The <code>--experimental-policy</code> flag can be used to enable features for policies
51508 <pre><code class="language-bash">node --experimental-policy=policy.json app.js</code></pre>
51512 the policy file itself may be provided via <code>--policy-integrity</code>.
51515 <pre><code class="language-bash">node --experimental-policy=policy.json --policy-integrity=<span cl…
51529 <pre><code class="language-json"><span class="hljs-punctuation">{</span>
51530 …<span class="hljs-attr">"onerror"</span><span class="hljs-punctuation">:</span> <span class="hljs-
51531 …<span class="hljs-attr">"resources"</span><span class="hljs-punctuation">:</span> <span class="hlj…
51532 …<span class="hljs-attr">"./app/checked.js"</span><span class="hljs-punctuation">:</span> <span cla…
51533 …<span class="hljs-attr">"integrity"</span><span class="hljs-punctuation">:</span> <span class="hlj…
51534 <span class="hljs-punctuation">}</span>
51535 <span class="hljs-punctuation">}</span>
51536 <span class="hljs-punctuation">}</span></code></pre>
51540 <a href="https://www.w3.org/TR/SRI/#the-integrity-attribute">integrity attribute</a>
51546 <pre><code class="language-json"><span class="hljs-punctuation">{</span>
51547 …<span class="hljs-attr">"resources"</span><span class="hljs-punctuation">:</span> <span class="hlj…
51548 …<span class="hljs-attr">"./app/checked.js"</span><span class="hljs-punctuation">:</span> <span cla…
51549 …<span class="hljs-attr">"integrity"</span><span class="hljs-punctuation">:</span> <span class="hlj…
51550 <span class="hljs-punctuation">}</span>
51551 <span class="hljs-punctuation">}</span>
51552 <span class="hljs-punctuation">}</span></code></pre>
51556 <li>A <a href="https://url.spec.whatwg.org/#relative-url-with-fragment-string">relative-URL string<…
51563 <code>node -e 'process.stdout.write("sha256-");process.stdin.pipe(crypto.createHash("sha256").setEn…
51574 <pre><code class="language-json"><span class="hljs-punctuation">{</span>
51575 …<span class="hljs-attr">"resources"</span><span class="hljs-punctuation">:</span> <span class="hlj…
51576 …<span class="hljs-attr">"./app/checked.js"</span><span class="hljs-punctuation">:</span> <span cla…
51577 …<span class="hljs-attr">"dependencies"</span><span class="hljs-punctuation">:</span> <span class="…
51578 …<span class="hljs-attr">"fs"</span><span class="hljs-punctuation">:</span> <span class="hljs-keywo…
51579 …ss="hljs-attr">"os"</span><span class="hljs-punctuation">:</span> <span class="hljs-string">"./app…
51580-attr">"http"</span><span class="hljs-punctuation">:</span> <span class="hljs-punctuation">{</span…
51581 <span class="hljs-punctuation">}</span>
51582 <span class="hljs-punctuation">}</span>
51583 <span class="hljs-punctuation">}</span>
51584 <span class="hljs-punctuation">}</span></code></pre>
51596 <pre><code class="language-json"><span class="hljs-punctuation">{</span>
51597 …<span class="hljs-attr">"resources"</span><span class="hljs-punctuation">:</span> <span class="hlj…
51598 …<span class="hljs-attr">"file:///C:/app/utils.js"</span><span class="hljs-punctuation">:</span> <s…
51599 …<span class="hljs-attr">"dependencies"</span><span class="hljs-punctuation">:</span> <span class="…
51600 …<span class="hljs-attr">"./utils.js"</span><span class="hljs-punctuation">:</span> <span class="hl…
51601 <span class="hljs-punctuation">}</span>
51602 <span class="hljs-punctuation">}</span>
51603 <span class="hljs-punctuation">}</span>
51604 <span class="hljs-punctuation">}</span></code></pre>
51606 and redirected to <code>file:///C:/app/utils-v2.js</code> instead regardless of using an
51638 …de class="language-js"><span class="hljs-keyword">const</span> original = <span class="hljs-built_…
51639-variable language_">module</span>.<span class="hljs-property">exports</span> = <span class="hljs-
51640 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">time</span…
51641 <span class="hljs-keyword">try</span> {
51642 …<span class="hljs-keyword">return</span> <span class="hljs-keyword">new</span>.<span class="hljs-p…
51643 …<span class="hljs-title class_">Reflect</span>.<span class="hljs-title function_">construct</span>…
51644 …<span class="hljs-title class_">Reflect</span>.<span class="hljs-title function_">apply</span>(ori…
51645 } <span class="hljs-keyword">finally</span> {
51646 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">timeEnd</s…
51655 <a href="https://url.spec.whatwg.org/#special-scheme">special schemes</a>, keeping trailing <code>"…
51657 If the URL is non-special the scope will be located by the URL's origin. If no
51668 <pre><code class="language-json"><span class="hljs-punctuation">{</span>
51669 …<span class="hljs-attr">"scopes"</span><span class="hljs-punctuation">:</span> <span class="hljs-p…
51670-attr">"file:///C:/app/"</span><span class="hljs-punctuation">:</span> <span class="hljs-punctuati…
51671-attr">"file:"</span><span class="hljs-punctuation">:</span> <span class="hljs-punctuation">{</spa…
51672 …<span class="hljs-attr">""</span><span class="hljs-punctuation">:</span> <span class="hljs-punctua…
51673 <span class="hljs-punctuation">}</span>
51674 <span class="hljs-punctuation">}</span></code></pre>
51726 <pre><code class="language-json"><span class="hljs-punctuation">{</span>
51727 …<span class="hljs-attr">"scopes"</span><span class="hljs-punctuation">:</span> <span class="hljs-p…
51728 …<span class="hljs-attr">"file:"</span><span class="hljs-punctuation">:</span> <span class="hljs-pu…
51729 …<span class="hljs-attr">"integrity"</span><span class="hljs-punctuation">:</span> <span class="hlj…
51730 <span class="hljs-punctuation">}</span>
51731 <span class="hljs-punctuation">}</span>
51732 <span class="hljs-punctuation">}</span></code></pre>
51736 <pre><code class="language-json"><span class="hljs-punctuation">{</span>
51737 …<span class="hljs-attr">"resources"</span><span class="hljs-punctuation">:</span> <span class="hlj…
51738 …<span class="hljs-attr">"./app/checked.js"</span><span class="hljs-punctuation">:</span> <span cla…
51739 …<span class="hljs-attr">"cascade"</span><span class="hljs-punctuation">:</span> <span class="hljs-
51740 …<span class="hljs-attr">"integrity"</span><span class="hljs-punctuation">:</span> <span class="hlj…
51741 <span class="hljs-punctuation">}</span>
51742 <span class="hljs-punctuation">}</span><span class="hljs-punctuation">,</span>
51743 …<span class="hljs-attr">"scopes"</span><span class="hljs-punctuation">:</span> <span class="hljs-p…
51744 …<span class="hljs-attr">"./app/"</span><span class="hljs-punctuation">:</span> <span class="hljs-p…
51745 …<span class="hljs-attr">"dependencies"</span><span class="hljs-punctuation">:</span> <span class="…
51746 …<span class="hljs-attr">"fs"</span><span class="hljs-punctuation">:</span> <span class="hljs-keywo…
51747 <span class="hljs-punctuation">}</span>
51748 <span class="hljs-punctuation">}</span>
51749 <span class="hljs-punctuation">}</span>
51750 <span class="hljs-punctuation">}</span></code></pre>
51752 <pre><code class="language-json"><span class="hljs-punctuation">{</span>
51753 …<span class="hljs-attr">"resources"</span><span class="hljs-punctuation">:</span> <span class="hlj…
51754 …<span class="hljs-attr">"data:text/javascript,import('fs');"</span><span class="hljs-punctuation">…
51755 …<span class="hljs-attr">"cascade"</span><span class="hljs-punctuation">:</span> <span class="hljs-
51756 …<span class="hljs-attr">"integrity"</span><span class="hljs-punctuation">:</span> <span class="hlj…
51757 <span class="hljs-punctuation">}</span>
51758 <span class="hljs-punctuation">}</span><span class="hljs-punctuation">,</span>
51759 …<span class="hljs-attr">"scopes"</span><span class="hljs-punctuation">:</span> <span class="hljs-p…
51760 …<span class="hljs-attr">"data:"</span><span class="hljs-punctuation">:</span> <span class="hljs-pu…
51761 …<span class="hljs-attr">"dependencies"</span><span class="hljs-punctuation">:</span> <span class="…
51762 …<span class="hljs-attr">"fs"</span><span class="hljs-punctuation">:</span> <span class="hljs-keywo…
51763 <span class="hljs-punctuation">}</span>
51764 <span class="hljs-punctuation">}</span>
51765 <span class="hljs-punctuation">}</span>
51766 <span class="hljs-punctuation">}</span></code></pre>
51767 <h5>Example: <a href="https://url.spec.whatwg.org/#relative-url-with-fragment-string">import maps</…
51769 <pre><code class="language-json"><span class="hljs-punctuation">{</span>
51770 …<span class="hljs-attr">"imports"</span><span class="hljs-punctuation">:</span> <span class="hljs-
51771 …<span class="hljs-attr">"react"</span><span class="hljs-punctuation">:</span> <span class="hljs-st…
51772 <span class="hljs-punctuation">}</span><span class="hljs-punctuation">,</span>
51773 …<span class="hljs-attr">"scopes"</span><span class="hljs-punctuation">:</span> <span class="hljs-p…
51774 …<span class="hljs-attr">"./ssr/"</span><span class="hljs-punctuation">:</span> <span class="hljs-p…
51775 …<span class="hljs-attr">"react"</span><span class="hljs-punctuation">:</span> <span class="hljs-st…
51776 <span class="hljs-punctuation">}</span>
51777 <span class="hljs-punctuation">}</span>
51778 <span class="hljs-punctuation">}</span></code></pre>
51779 <pre><code class="language-json"><span class="hljs-punctuation">{</span>
51780 …an class="hljs-attr">"dependencies"</span><span class="hljs-punctuation">:</span> <span class="hlj…
51781 …<span class="hljs-attr">"scopes"</span><span class="hljs-punctuation">:</span> <span class="hljs-p…
51782 …<span class="hljs-attr">""</span><span class="hljs-punctuation">:</span> <span class="hljs-punctua…
51783 …<span class="hljs-attr">"cascade"</span><span class="hljs-punctuation">:</span> <span class="hljs-
51784 …<span class="hljs-attr">"dependencies"</span><span class="hljs-punctuation">:</span> <span class="…
51785 …<span class="hljs-attr">"react"</span><span class="hljs-punctuation">:</span> <span class="hljs-st…
51786 <span class="hljs-punctuation">}</span>
51787 <span class="hljs-punctuation">}</span><span class="hljs-punctuation">,</span>
51788 …<span class="hljs-attr">"./ssr/"</span><span class="hljs-punctuation">:</span> <span class="hljs-p…
51789 …<span class="hljs-attr">"cascade"</span><span class="hljs-punctuation">:</span> <span class="hljs-
51790 …<span class="hljs-attr">"dependencies"</span><span class="hljs-punctuation">:</span> <span class="…
51791 …<span class="hljs-attr">"react"</span><span class="hljs-punctuation">:</span> <span class="hljs-st…
51792 <span class="hljs-punctuation">}</span>
51793 <span class="hljs-punctuation">}</span>
51794 <span class="hljs-punctuation">}</span>
51795 <span class="hljs-punctuation">}</span></code></pre>
51798 Policies require this to be opt-in since it enables all resources of the
51822 …de class="language-js"><span class="hljs-keyword">const</span> process = <span class="hljs-built_i…
51840-js">process.<span class="hljs-title function_">on</span>(<span class="hljs-string">'beforeExit'</…
51841 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>…
51844 ….<span class="hljs-title function_">on</span>(<span class="hljs-string">'exit'</span>, <span class…
51845 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>…
51848 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(…
51850 <span class="hljs-comment">// Prints:</span>
51851 <span class="hljs-comment">// This message is displayed first.</span>
51852 <span class="hljs-comment">// Process beforeExit event with code: 0</span>
51853 <span class="hljs-comment">// Process exit event with code: 0</span></code></pre>
51866 <li><code>code</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
51879 …age-js">process.<span class="hljs-title function_">on</span>(<span class="hljs-string">'exit'</spa…
51880-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hlj…
51886 …age-js">process.<span class="hljs-title function_">on</span>(<span class="hljs-string">'exit'</spa…
51887 <span class="hljs-built_in">setTimeout</span>(<span class="hljs-function">() =></span> {
51888 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>…
51889 }, <span class="hljs-number">0</span>);
51896-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type">&#x3C;Object></a> | <a href=…
51916 <li><code>type</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
51917 <li><code>promise</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference…
51918 <li><code>value</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struct…
51931 example, <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/…
51932-js">process.<span class="hljs-title function_">on</span>(<span class="hljs-string">'multipleResol…
51933 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">error</spa…
51934-title function_">setImmediate</span>(<span class="hljs-function">() =></span> process.<span class…
51937 …class="hljs-keyword">async</span> <span class="hljs-keyword">function</span> <span class="hljs-tit…
51938 <span class="hljs-keyword">try</span> {
51939-keyword">return</span> <span class="hljs-keyword">await</span> <span class="hljs-keyword">new</sp…
51940 … <span class="hljs-title function_">resolve</span>(<span class="hljs-string">'First call'</span>);
51941 …<span class="hljs-title function_">resolve</span>(<span class="hljs-string">'Swallowed resolve'</s…
51942 …lass="hljs-title function_">reject</span>(<span class="hljs-keyword">new</span> <span class="hljs-
51944 } <span class="hljs-keyword">catch</span> {
51945 …an class="hljs-keyword">throw</span> <span class="hljs-keyword">new</span> <span class="hljs-title…
51949 …s-title function_">main</span>().<span class="hljs-title function_">then</span>(<span class="hljs-
51950 <span class="hljs-comment">// resolve: Promise { 'First call' } 'Swallowed resolve'</span>
51951 <span class="hljs-comment">// reject: Promise { 'First call' } Error: Swallowed reject</span>
51952 <span class="hljs-comment">// at Promise (*)</span>
51953 <span class="hljs-comment">// at new Promise (&#x3C;anonymous>)</span>
51954 <span class="hljs-comment">// at main (*)</span>
51955 <span class="hljs-comment">// First call</span></code></pre>
51961 <li><code>promise</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference…
51964 and an error handler was attached to it (using <a href="https://developer.mozilla.org/en-US/docs/We…
51974 an ever-growing list of unhandled exceptions, with Promises there can be a
51975 growing-and-shrinking list of unhandled rejections.</p>
51981 …class="language-js"><span class="hljs-keyword">const</span> unhandledRejections = <span class="hlj…
51982 …class="hljs-title function_">on</span>(<span class="hljs-string">'unhandledRejection'</span>, <spa…
51983 unhandledRejections.<span class="hljs-title function_">set</span>(promise, reason);
51985 … class="hljs-title function_">on</span>(<span class="hljs-string">'rejectionHandled'</span>, <span…
51986 unhandledRejections.<span class="hljs-title function_">delete</span>(promise);
51991 likely best for long-running application) or upon process exit (which is likely
52006 <li><code>err</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Glo…
52007 <li><code>origin</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struc…
52010 <a href="#cli_unhandled_rejections_mode"><code>--unhandled-rejections</code></a> flag set to <code>…
52022-js">process.<span class="hljs-title function_">on</span>(<span class="hljs-string">'uncaughtExcep…
52023 fs.<span class="hljs-title function_">writeSync</span>(
52024 process.<span class="hljs-property">stderr</span>.<span class="hljs-property">fd</span>,
52025 <span class="hljs-string">`Caught exception: <span class="hljs-subst">${err}</span>\n`</span> +
52026 <span class="hljs-string">`Exception origin: <span class="hljs-subst">${origin}</span>`</span>
52030 <span class="hljs-built_in">setTimeout</span>(<span class="hljs-function">() =></span> {
52031 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>…
52032 }, <span class="hljs-number">500</span>);
52034 <span class="hljs-comment">// Intentionally cause an exception, but don't catch it.</span>
52035 <span class="hljs-title function_">nonexistentFunc</span>();
52036 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(…
52048 process will exit with a non-zero exit code and the stack trace will be printed.
52066 <li><code>err</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Glo…
52067 <li><code>origin</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struc…
52070 <a href="#cli_unhandled_rejections_mode"><code>--unhandled-rejections</code></a> flag set to <code>…
52079-js">process.<span class="hljs-title function_">on</span>(<span class="hljs-string">'uncaughtExcep…
52080 …<span class="hljs-title class_">MyMonitoringTool</span>.<span class="hljs-title function_">logSync…
52083 <span class="hljs-comment">// Intentionally cause an exception, but don't catch it.</span>
52084 <span class="hljs-title function_">nonexistentFunc</span>();
52085 <span class="hljs-comment">// Still crashes Node.js</span></code></pre>
52101 …illa.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error" class="type">&#x3C;Error></a> |…
52103 <li><code>promise</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference…
52108 promises". Rejections can be caught and handled using <a href="https://developer.mozilla.org/en-US/…
52112-js">process.<span class="hljs-title function_">on</span>(<span class="hljs-string">'unhandledReje…
52113-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hlj…
52114 …<span class="hljs-comment">// Application specific logging, throwing an error, or other logic here…
52117 somePromise.<span class="hljs-title function_">then</span>(<span class="hljs-function">(<span class…
52118-keyword">return</span> <span class="hljs-title function_">reportToUser</span>(<span class="hljs-v…
52119 }); <span class="hljs-comment">// No `.catch()` or `.then()`</span></code></pre>
52122 … class="language-js"><span class="hljs-keyword">function</span> <span class="hljs-title function_"…
52123 <span class="hljs-comment">// Initially set the loaded status to a rejected promise</span>
52124-variable language_">this</span>.<span class="hljs-property">loaded</span> = <span class="hljs-tit…
52127 <span class="hljs-keyword">const</span> resource = <span class="hljs-keyword">new</span> <span clas…
52128 <span class="hljs-comment">// no .catch or .then on resource.loaded for at least a turn</span></cod…
52131 address such failures, a non-operational
52132 <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise/c…
52140 <li><code>warning</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference…
52142 <li><code>name</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
52143 …tps://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#…
52144 <li><code>stack</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struct…
52154 lead to sub-optimal application performance, bugs, or security vulnerabilities.</p>
52155 …ge-js">process.<span class="hljs-title function_">on</span>(<span class="hljs-string">'warning'</s…
52156-variable language_">console</span>.<span class="hljs-title function_">warn</span>(warning.<span c…
52157-variable language_">console</span>.<span class="hljs-title function_">warn</span>(warning.<span c…
52158-variable language_">console</span>.<span class="hljs-title function_">warn</span>(warning.<span c…
52160 <p>By default, Node.js will print process warnings to <code>stderr</code>. The <code>--no-warnings<…
52161 command-line option can be used to suppress the default console output but the
52165 <pre><code class="language-console"><span class="hljs-meta">$ </span><span class="language-bash">no…
52166 <span class="hljs-meta">> </span><span class="language-bash">events.defaultMaxListeners = 1;</span>
52167 <span class="hljs-meta">> </span><span class="language-bash">process.on(<span class="hljs-string">'…
52168 <span class="hljs-meta">> </span><span class="language-bash">process.on(<span class="hljs-string">'…
52169 <span class="hljs-meta">> </span><span class="language-bash">(node:38638) MaxListenersExceededWarni…
52173 <pre><code class="language-console"><span class="hljs-meta">$ </span><span class="language-bash">no…
52174-meta">> </span><span class="language-bash">const p = process.on(<span class="hljs-string">'warnin…
52175 <span class="hljs-meta">> </span><span class="language-bash">events.defaultMaxListeners = 1;</span>
52176 <span class="hljs-meta">> </span><span class="language-bash">process.on(<span class="hljs-string">'…
52177 <span class="hljs-meta">> </span><span class="language-bash">process.on(<span class="hljs-string">'…
52178 <span class="hljs-meta">> </span><span class="language-bash">Do not <span class="hljs-keyword">do</…
52179 <p>The <code>--trace-warnings</code> command-line option can be used to have the default
52181 <p>Launching Node.js using the <code>--throw-deprecation</code> command-line flag will
52183 <p>Using the <code>--trace-deprecation</code> command-line flag will cause the custom
52185 <p>Using the <code>--no-deprecation</code> command-line flag will suppress all reporting
52187 <p>The <code>*-deprecation</code> command-line flags only affect warnings that use the name
52199 custom or application-specific warnings.</p>
52205 <li><code>'DeprecationWarning'</code> - Indicates use of a deprecated Node.js API or feature.
52208 <li><code>'ExperimentalWarning'</code> - Indicates use of an experimental Node.js API or
52210 time and are not subject to the same strict semantic-versioning and long-term
52212 <li><code>'MaxListenersExceededWarning'</code> - Indicates that too many listeners for a
52215 <li><code>'TimeoutOverflowWarning'</code> - Indicates that a numeric value that cannot fit
52216 within a 32-bit signed integer has been provided to either the <code>setTimeout()</code>
52218 <li><code>'UnsupportedWarning'</code> - Indicates use of an unsupported option or feature
52226 refer to <a href="http://man7.org/linux/man-pages/man7/signal.7.html"><code>signal(7)</code></a> fo…
52233 <pre><code class="language-js"><span class="hljs-comment">// Begin reading from stdin so the proces…
52234 process.<span class="hljs-property">stdin</span>.<span class="hljs-title function_">resume</span>();
52236 process.<span class="hljs-title function_">on</span>(<span class="hljs-string">'SIGINT'</span>, <sp…
52237 …="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span clas…
52240 <span class="hljs-comment">// Using a single function to handle multiple signals</span>
52241 <span class="hljs-keyword">function</span> <span class="hljs-title function_">handle</span>(<span c…
52242 …ss="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span cl…
52245 process.<span class="hljs-title function_">on</span>(<span class="hljs-string">'SIGINT'</span>, han…
52246 process.<span class="hljs-title function_">on</span>(<span class="hljs-string">'SIGTERM'</span>, ha…
52250 <li><code>'SIGTERM'</code> and <code>'SIGINT'</code> have default handlers on non-Windows platforms…
52256 other platforms under various similar conditions. See <a href="http://man7.org/linux/man-pages/man7…
52258 Windows about 10 seconds later. On non-Windows platforms, the default
52267 pressed. On non-Windows platforms, it can be listened on, but there is no way
52276 artificially using <a href="http://man7.org/linux/man-pages/man2/kill.2.html"><code>kill(2)</code><…
52287 termination of the target process, and afterwards, subprocess will report that
52304 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Set" …
52307 read-only <code>Set</code> of flags allowable within the <a href="#cli_node_options_options"><code>…
52314 <li>Flags may omit leading single (<code>-</code>) or double (<code>--</code>) dashes; e.g.,
52315 <code>inspect-brk</code> for <code>--inspect-brk</code>, or <code>r</code> for <code>-r</code>.</li>
52316 <li>Flags passed through to V8 (as listed in <code>--v8-options</code>) may replace
52317 one or more <em>non-leading</em> dashes for an underscore, or vice-versa;
52318 e.g., <code>--perf_basic_prof</code>, <code>--perf-basic-prof</code>, <code>--perf_basic-prof</code…
52322 e.g., <code>--stack-trace-limit=100</code>.</li>
52327 passed through to V8 will contain underscores instead of non-leading
52329-js">process.<span class="hljs-property">allowedNodeEnvironmentFlags</span>.<span class="hljs-titl…
52330 <span class="hljs-comment">// -r</span>
52331 <span class="hljs-comment">// --inspect-brk</span>
52332 <span class="hljs-comment">// --abort_on_uncaught_exception</span>
52333 <span class="hljs-comment">// ...</span>
52346 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" c…
52351-js"><span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</…
52357 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" c…
52359 <p>The <code>process.argv</code> property returns an array containing the command-line
52363 file being executed. The remaining elements will be any additional command-line
52365 <p>For example, assuming the following script for <code>process-args.js</code>:</p>
52366 <pre><code class="language-js"><span class="hljs-comment">// print process.argv</span>
52367 …span class="hljs-property">argv</span>.<span class="hljs-title function_">forEach</span>(<span cla…
52368-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hlj…
52371 <pre><code class="language-console"><span class="hljs-meta">$ </span><span class="language-bash">no…
52373 <pre><code class="language-text">0: /usr/local/bin/node
52374 1: /Users/mjr/work/node/process-args.js
52383 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" c…
52385 <p>The <code>process.argv0</code> property stores a read-only copy of the original value of
52387 …code class="language-console"><span class="hljs-meta">$ </span><span class="language-bash">bash -c…
52388 <span class="hljs-meta">> </span><span class="language-bash">process.argv[0]</span>
52390 <span class="hljs-meta">> </span><span class="language-bash">process.argv0</span>
52405 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Objec…
52434 <li><code>directory</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_st…
52439-js"><span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</…
52440 <span class="hljs-keyword">try</span> {
52441 process.<span class="hljs-title function_">chdir</span>(<span class="hljs-string">'/tmp'</span>);
52442 …"hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class…
52443 } <span class="hljs-keyword">catch</span> (err) {
52444 …ss="hljs-variable language_">console</span>.<span class="hljs-title function_">error</span>(<span …
52452 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Objec…
52459 <!-- eslint-skip -->
52460 <pre><code class="language-js">{
52461 <span class="hljs-attr">target_defaults</span>:
52462 { <span class="hljs-attr">cflags</span>: [],
52463 … <span class="hljs-attr">default_configuration</span>: <span class="hljs-string">'Release'</span>,
52464 <span class="hljs-attr">defines</span>: [],
52465 <span class="hljs-attr">include_dirs</span>: [],
52466 <span class="hljs-attr">libraries</span>: [] },
52467 <span class="hljs-attr">variables</span>:
52469 <span class="hljs-attr">host_arch</span>: <span class="hljs-string">'x64'</span>,
52470 <span class="hljs-attr">napi_build_version</span>: <span class="hljs-number">5</span>,
52471 <span class="hljs-attr">node_install_npm</span>: <span class="hljs-string">'true'</span>,
52472 <span class="hljs-attr">node_prefix</span>: <span class="hljs-string">''</span>,
52473 <span class="hljs-attr">node_shared_cares</span>: <span class="hljs-string">'false'</span>,
52474 … <span class="hljs-attr">node_shared_http_parser</span>: <span class="hljs-string">'false'</span>,
52475 <span class="hljs-attr">node_shared_libuv</span>: <span class="hljs-string">'false'</span>,
52476 <span class="hljs-attr">node_shared_zlib</span>: <span class="hljs-string">'false'</span>,
52477 <span class="hljs-attr">node_use_dtrace</span>: <span class="hljs-string">'false'</span>,
52478 <span class="hljs-attr">node_use_openssl</span>: <span class="hljs-string">'true'</span>,
52479 <span class="hljs-attr">node_shared_openssl</span>: <span class="hljs-string">'false'</span>,
52480 <span class="hljs-attr">strict_aliasing</span>: <span class="hljs-string">'true'</span>,
52481 <span class="hljs-attr">target_arch</span>: <span class="hljs-string">'x64'</span>,
52482 <span class="hljs-attr">v8_use_snapshot</span>: <span class="hljs-number">1</span>
52485 <p>The <code>process.config</code> property is <strong>not</strong> read-only and there are existing
52493 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" …
52506 <li><code>previousValue</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Ref…
52508 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Obje…
52510 <li><code>user</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
52511 <li><code>system</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struc…
52522 <pre><code class="language-js"><span class="hljs-keyword">const</span> startUsage = process.<span c…
52523 <span class="hljs-comment">// { user: 38579, system: 6986 }</span>
52525 <span class="hljs-comment">// spin the CPU for 500 milliseconds</span>
52526 <span class="hljs-keyword">const</span> now = <span class="hljs-title class_">Date</span>.<span cla…
52527 …ljs-keyword">while</span> (<span class="hljs-title class_">Date</span>.<span class="hljs-title fun…
52529 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(…
52530 <span class="hljs-comment">// { user: 514883, system: 11226 }</span></code></pre>
52536 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Strin…
52540-js"><span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</…
52546 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" c…
52549 <pre><code class="language-js">process.<span class="hljs-property">debugPort</span> = <span class="…
52575 <li><code>module</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/…
52576 <li><code>filename</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_str…
52593 …<code class="language-js"><span class="hljs-keyword">const</span> os = <span class="hljs-built_in"…
52594 <span class="hljs-keyword">const</span> path = <span class="hljs-built_in">require</span>(<span cla…
52595 <span class="hljs-keyword">const</span> <span class="hljs-variable language_">module</span> = { <sp…
52596-title function_">dlopen</span>(<span class="hljs-variable language_">module</span>, path.<span cl…
52597 …os.<span class="hljs-property">constants</span>.<span class="hljs-property">dlopen</span>.<span cl…
52598 <span class="hljs-variable language_">module</span>.<span class="hljs-property">exports</span>.<spa…
52604 …zilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | …
52605 <li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference…
52607 <li><code>type</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
52609 <li><code>code</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
52610 <li><code>ctor</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Gl…
52613 <li><code>detail</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struc…
52620 <pre><code class="language-js"><span class="hljs-comment">// Emit a warning with a code and additio…
52621 process.<span class="hljs-title function_">emitWarning</span>(<span class="hljs-string">'Something …
52622 <span class="hljs-attr">code</span>: <span class="hljs-string">'MY_WARNING'</span>,
52623 …<span class="hljs-attr">detail</span>: <span class="hljs-string">'This is some additional informat…
52625 <span class="hljs-comment">// Emits:</span>
52626 <span class="hljs-comment">// (node:56338) [MY_WARNING] Warning: Something happened!</span>
52627 <span class="hljs-comment">// This is some additional information</span></code></pre>
52631 …ge-js">process.<span class="hljs-title function_">on</span>(<span class="hljs-string">'warning'</s…
52632-variable language_">console</span>.<span class="hljs-title function_">warn</span>(warning.<span c…
52633-variable language_">console</span>.<span class="hljs-title function_">warn</span>(warning.<span c…
52634-variable language_">console</span>.<span class="hljs-title function_">warn</span>(warning.<span c…
52635-variable language_">console</span>.<span class="hljs-title function_">warn</span>(warning.<span c…
52636-variable language_">console</span>.<span class="hljs-title function_">warn</span>(warning.<span c…
52644 …zilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | …
52645 <li><code>type</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
52647 <li><code>code</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
52648 <li><code>ctor</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Gl…
52655 <pre><code class="language-js"><span class="hljs-comment">// Emit a warning using a string.</span>
52656 process.<span class="hljs-title function_">emitWarning</span>(<span class="hljs-string">'Something …
52657 <span class="hljs-comment">// Emits: (node: 56338) Warning: Something happened!</span></code></pre>
52658 <pre><code class="language-js"><span class="hljs-comment">// Emit a warning using a string and a ty…
52659 …ess.<span class="hljs-title function_">emitWarning</span>(<span class="hljs-string">'Something Hap…
52660 <span class="hljs-comment">// Emits: (node:56338) CustomWarning: Something Happened!</span></code><…
52661-js">process.<span class="hljs-title function_">emitWarning</span>(<span class="hljs-string">'Some…
52662 <span class="hljs-comment">// Emits: (node:56338) [WARN001] CustomWarning: Something happened!</spa…
52666 …ge-js">process.<span class="hljs-title function_">on</span>(<span class="hljs-string">'warning'</s…
52667 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">warn</span…
52668 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">warn</span…
52669 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">warn</span…
52670 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">warn</span…
52675 <pre><code class="language-js"><span class="hljs-comment">// Emit a warning using an Error object.<…
52676 …ss="hljs-keyword">const</span> myWarning = <span class="hljs-keyword">new</span> <span class="hljs
52677 <span class="hljs-comment">// Use the Error name property to specify the type name</span>
52678 myWarning.<span class="hljs-property">name</span> = <span class="hljs-string">'CustomWarning'</span…
52679 myWarning.<span class="hljs-property">code</span> = <span class="hljs-string">'WARN001'</span>;
52681 process.<span class="hljs-title function_">emitWarning</span>(myWarning);
52682 <span class="hljs-comment">// Emits: (node:56338) [WARN001] CustomWarning: Something happened!</spa…
52690 <li>If the <code>--throw-deprecation</code> command-line flag is used, the deprecation
52692 <li>If the <code>--no-deprecation</code> command-line flag is used, the deprecation
52694 <li>If the <code>--trace-deprecation</code> command-line flag is used, the deprecation
52701 …class="language-js"><span class="hljs-keyword">function</span> <span class="hljs-title function_">…
52702 <span class="hljs-keyword">if</span> (!emitMyWarning.<span class="hljs-property">warned</span>) {
52703 … emitMyWarning.<span class="hljs-property">warned</span> = <span class="hljs-literal">true</span>;
52704 …process.<span class="hljs-title function_">emitWarning</span>(<span class="hljs-string">'Only warn…
52707 <span class="hljs-title function_">emitMyWarning</span>();
52708 <span class="hljs-comment">// Emits: (node: 56339) Warning: Only warn once!</span>
52709 <span class="hljs-title function_">emitMyWarning</span>();
52710 <span class="hljs-comment">// Emits nothing</span></code></pre>
52726 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Objec…
52729 See <a href="http://man7.org/linux/man-pages/man7/environ.7.html"><code>environ(7)</code></a>.</p>
52731 <!-- eslint-skip -->
52732 <pre><code class="language-js">{
52733 <span class="hljs-attr">TERM</span>: <span class="hljs-string">'xterm-256color'</span>,
52734 <span class="hljs-attr">SHELL</span>: <span class="hljs-string">'/usr/local/bin/bash'</span>,
52735 <span class="hljs-attr">USER</span>: <span class="hljs-string">'maciej'</span>,
52736 …<span class="hljs-attr">PATH</span>: <span class="hljs-string">'~/.bin/:/usr/bin:/bin:/usr/sbin:/s…
52737 <span class="hljs-attr">PWD</span>: <span class="hljs-string">'/Users/maciej'</span>,
52738 <span class="hljs-attr">EDITOR</span>: <span class="hljs-string">'vim'</span>,
52739 <span class="hljs-attr">SHLVL</span>: <span class="hljs-string">'1'</span>,
52740 <span class="hljs-attr">HOME</span>: <span class="hljs-string">'/Users/maciej'</span>,
52741 <span class="hljs-attr">LOGNAME</span>: <span class="hljs-string">'maciej'</span>,
52742 <span class="hljs-attr">_</span>: <span class="hljs-string">'/usr/local/bin/node'</span>
52748-console"><span class="hljs-meta">$ </span><span class="language-bash">node -e <span class="hljs-s…
52750 …<code class="language-js">process.<span class="hljs-property">env</span>.<span class="hljs-propert…
52751 …ljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(process.<span…
52755 …code class="language-js">process.<span class="hljs-property">env</span>.<span class="hljs-property…
52756 …ljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(process.<span…
52757 <span class="hljs-comment">// => 'null'</span>
52758 process.<span class="hljs-property">env</span>.<span class="hljs-property">test</span> = <span clas…
52759 …ljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(process.<span…
52760 <span class="hljs-comment">// => 'undefined'</span></code></pre>
52762 …code class="language-js">process.<span class="hljs-property">env</span>.<span class="hljs-property…
52763 <span class="hljs-keyword">delete</span> process.<span class="hljs-property">env</span>.<span class…
52764 …ljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(process.<span…
52765 <span class="hljs-comment">// => undefined</span></code></pre>
52766 <p>On Windows operating systems, environment variables are case-insensitive.</p>
52767 …code class="language-js">process.<span class="hljs-property">env</span>.<span class="hljs-property…
52768 …ljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(process.<span…
52769 <span class="hljs-comment">// => 1</span></code></pre>
52775 are visible to the operating system or to native add-ons.</p>
52781 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" c…
52783 <p>The <code>process.execArgv</code> property returns the set of Node.js-specific command-line
52789 <pre><code class="language-console"><span class="hljs-meta">$ </span><span class="language-bash">no…
52791 <!-- eslint-disable semi -->
52792 <pre><code class="language-js">[<span class="hljs-string">'--harmony'</span>]</code></pre>
52794 <!-- eslint-disable semi -->
52795 …language-js">[<span class="hljs-string">'/usr/local/bin/node'</span>, <span class="hljs-string">'s…
52803 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" c…
52807 <!-- eslint-disable semi -->
52808 <pre><code class="language-js"><span class="hljs-string">'/usr/local/bin/node'</span></code></pre>
52814 <li><code>code</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
52822 <pre><code class="language-js">process.<span class="hljs-title function_">exit</span>(<span class="…
52835 <pre><code class="language-js"><span class="hljs-comment">// This is an example of what *not* to do…
52836 <span class="hljs-keyword">if</span> (<span class="hljs-title function_">someConditionNotMet</span>…
52837 <span class="hljs-title function_">printUsageToStdout</span>();
52838 process.<span class="hljs-title function_">exit</span>(<span class="hljs-number">1</span>);
52847 <pre><code class="language-js"><span class="hljs-comment">// How to properly set the exit code whil…
52848 <span class="hljs-comment">// the process exit gracefully.</span>
52849 <span class="hljs-keyword">if</span> (<span class="hljs-title function_">someConditionNotMet</span>…
52850 <span class="hljs-title function_">printUsageToStdout</span>();
52851 process.<span class="hljs-property">exitCode</span> = <span class="hljs-number">1</span>;
52863 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" c…
52875 of the Node.js process. (See <a href="http://man7.org/linux/man-pages/man2/getegid.2.html"><code>ge…
52876 <pre><code class="language-js"><span class="hljs-keyword">if</span> (process.<span class="hljs-prop…
52877 …="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span clas…
52886 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Obje…
52889 the process. (See <a href="http://man7.org/linux/man-pages/man2/geteuid.2.html"><code>geteuid(2)</c…
52890 <pre><code class="language-js"><span class="hljs-keyword">if</span> (process.<span class="hljs-prop…
52891 …="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span clas…
52900 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Obje…
52903 process. (See <a href="http://man7.org/linux/man-pages/man2/getgid.2.html"><code>getgid(2)</code></…
52904 <pre><code class="language-js"><span class="hljs-keyword">if</span> (process.<span class="hljs-prop…
52905 …="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span clas…
52914 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Numbe…
52919 <pre><code class="language-js"><span class="hljs-keyword">if</span> (process.<span class="hljs-prop…
52920-variable language_">console</span>.<span class="hljs-title function_">log</span>(process.<span cl…
52929 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Numbe…
52932 (See <a href="http://man7.org/linux/man-pages/man2/getuid.2.html"><code>getuid(2)</code></a>.)</p>
52933 <pre><code class="language-js"><span class="hljs-keyword">if</span> (process.<span class="hljs-prop…
52934 …="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span clas…
52943 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boole…
52951 … api_stability_3"><a href="#documentation_stability_index">Stability: 3</a> - Legacy. Use <a href=…
52953 <li><code>time</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
52954 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Numbe…
52958 <p>The <code>process.hrtime()</code> method returns the current high-resolution real time
52964 user-defined array instead of the result of a previous call to
52969 … class="language-js"><span class="hljs-keyword">const</span> <span class="hljs-variable constant_"…
52970 <span class="hljs-keyword">const</span> time = process.<span class="hljs-title function_">hrtime</s…
52971 <span class="hljs-comment">// [ 1800216, 25 ]</span>
52973 <span class="hljs-built_in">setTimeout</span>(<span class="hljs-function">() =></span> {
52974 …<span class="hljs-keyword">const</span> diff = process.<span class="hljs-title function_">hrtime</…
52975 <span class="hljs-comment">// [ 1, 552 ]</span>
52977-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hlj…
52978 <span class="hljs-comment">// Benchmark took 1000000552 nanoseconds</span>
52979 }, <span class="hljs-number">1000</span>);</code></pre>
52985 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Obje…
52988 current high-resolution real time in nanoseconds as a <code>bigint</code>.</p>
52992 … class="language-js"><span class="hljs-keyword">const</span> start = process.<span class="hljs-pro…
52993 <span class="hljs-comment">// 191051479007711n</span>
52995 <span class="hljs-built_in">setTimeout</span>(<span class="hljs-function">() =></span> {
52996 …<span class="hljs-keyword">const</span> end = process.<span class="hljs-property">hrtime</span>.<s…
52997 <span class="hljs-comment">// 191052633396993n</span>
52999-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hlj…
53000 <span class="hljs-comment">// Benchmark took 1154389282 nanoseconds</span>
53001 }, <span class="hljs-number">1000</span>);</code></pre>
53007 …zilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | …
53008 …zilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | …
53015-js"><span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</…
53016 …"hljs-title function_">initgroups</span>(<span class="hljs-string">'nodeuser'</span>, <span class=…
53017-variable language_">console</span>.<span class="hljs-title function_">log</span>(process.<span cl…
53018 …ess.<span class="hljs-title function_">setgid</span>(<span class="hljs-number">1000</span>); …
53019-variable language_">console</span>.<span class="hljs-title function_">log</span>(process.<span cl…
53028 <li><code>pid</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structur…
53029 …zilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | …
53035 and <a href="http://man7.org/linux/man-pages/man2/kill.2.html"><code>kill(2)</code></a> for more in…
53036 <p>This method will throw an error if the target <code>pid</code> does not exist. As a special
53042 other than kill the target process.</p>
53043 …e class="language-js">process.<span class="hljs-title function_">on</span>(<span class="hljs-strin…
53044 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>…
53047 <span class="hljs-built_in">setTimeout</span>(<span class="hljs-function">() =></span> {
53048 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>…
53049 process.<span class="hljs-title function_">exit</span>(<span class="hljs-number">0</span>);
53050 }, <span class="hljs-number">100</span>);
53052 process.<span class="hljs-title function_">kill</span>(process.<span class="hljs-property">pid</spa…
53059 … api_stability_0"><a href="#documentation_stability_index">Stability: 0</a> - Deprecated: Use <a h…
53061 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Objec…
53085 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Obje…
53087 <li><code>rss</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structur…
53088 <li><code>heapTotal</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_st…
53089 <li><code>heapUsed</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_str…
53090 <li><code>external</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_str…
53091 <li><code>arrayBuffers</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data…
53097 …s="language-js"><span class="hljs-variable language_">console</span>.<span class="hljs-title funct…
53098 <span class="hljs-comment">// Prints:</span>
53099 <span class="hljs-comment">// {</span>
53100 <span class="hljs-comment">// rss: 4935680,</span>
53101 <span class="hljs-comment">// heapTotal: 1826816,</span>
53102 <span class="hljs-comment">// heapUsed: 650472,</span>
53103 <span class="hljs-comment">// external: 49879,</span>
53104 <span class="hljs-comment">// arrayBuffers: 9386</span>
53105 <span class="hljs-comment">// }</span></code></pre>
53129 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Numbe…
53138-js"><span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</…
53139 <span class="hljs-comment">// 35655680</span></code></pre>
53153 <li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Referenc…
53154 <li><code>...args</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_stru…
53160 See the <a href="https://nodejs.org/en/docs/guides/event-loop-timers-and-nexttick/#process-nexttick…
53161 …class="language-js"><span class="hljs-variable language_">console</span>.<span class="hljs-title f…
53162 process.<span class="hljs-title function_">nextTick</span>(<span class="hljs-function">() =></span>…
53163 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>…
53165 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(…
53166 <span class="hljs-comment">// Output:</span>
53167 <span class="hljs-comment">// start</span>
53168 <span class="hljs-comment">// scheduled</span>
53169 <span class="hljs-comment">// nextTick callback</span></code></pre>
53173 …de class="language-js"><span class="hljs-keyword">function</span> <span class="hljs-title function…
53174 …<span class="hljs-variable language_">this</span>.<span class="hljs-title function_">setupOptions<…
53176 …process.<span class="hljs-title function_">nextTick</span>(<span class="hljs-function">() =></span…
53177 …<span class="hljs-variable language_">this</span>.<span class="hljs-title function_">startDoingStu…
53181 <span class="hljs-keyword">const</span> thing = <span class="hljs-keyword">new</span> <span class="…
53182 thing.<span class="hljs-title function_">getReadyForStuff</span>();
53184 <span class="hljs-comment">// thing.startDoingStuff() gets called now, not before.</span></code></p…
53187 <pre><code class="language-js"><span class="hljs-comment">// WARNING! DO NOT USE! BAD UNSAFE HAZA…
53188 <span class="hljs-keyword">function</span> <span class="hljs-title function_">maybeSync</span>(<spa…
53189 <span class="hljs-keyword">if</span> (arg) {
53190 <span class="hljs-title function_">cb</span>();
53191 <span class="hljs-keyword">return</span>;
53194 fs.<span class="hljs-title function_">stat</span>(<span class="hljs-string">'file'</span>, cb);
53197-js"><span class="hljs-keyword">const</span> maybeTrue = <span class="hljs-title class_">Math</spa…
53199 <span class="hljs-title function_">maybeSync</span>(maybeTrue, <span class="hljs-function">() =></s…
53200 <span class="hljs-title function_">foo</span>();
53203 <span class="hljs-title function_">bar</span>();</code></pre>
53206 …lass="language-js"><span class="hljs-keyword">function</span> <span class="hljs-title function_">d…
53207 <span class="hljs-keyword">if</span> (arg) {
53208 process.<span class="hljs-title function_">nextTick</span>(cb);
53209 <span class="hljs-keyword">return</span>;
53212 fs.<span class="hljs-title function_">stat</span>(<span class="hljs-string">'file'</span>, cb);
53220-js"><span class="hljs-title class_">Promise</span>.<span class="hljs-title function_">resolve</sp…
53221-title function_">queueMicrotask</span>(<span class="hljs-function">() =></span> <span class="hljs
53222-title function_">nextTick</span>(<span class="hljs-function">() =></span> <span class="hljs-varia…
53223 <span class="hljs-comment">// Output:</span>
53224 <span class="hljs-comment">// 1</span>
53225 <span class="hljs-comment">// 2</span>
53226 <span class="hljs-comment">// 3</span></code></pre>
53230 In simple scenarios, <code>queueMicrotask()</code> can be a drop-in replacement for
53232 …class="language-js"><span class="hljs-variable language_">console</span>.<span class="hljs-title f…
53233 <span class="hljs-title function_">queueMicrotask</span>(<span class="hljs-function">() =></span> {
53234 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>…
53236 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(…
53237 <span class="hljs-comment">// Output:</span>
53238 <span class="hljs-comment">// start</span>
53239 <span class="hljs-comment">// scheduled</span>
53240 <span class="hljs-comment">// microtask callback</span></code></pre>
53241 <p>One note-worthy difference between the two APIs is that <code>process.nextTick()</code>
53245 …de class="language-js"><span class="hljs-keyword">function</span> <span class="hljs-title function…
53246 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>…
53249 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(…
53250-title function_">queueMicrotask</span>(deferred.<span class="hljs-title function_">bind</span>(<s…
53251 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(…
53252 <span class="hljs-comment">// Output:</span>
53253 <span class="hljs-comment">// start</span>
53254 <span class="hljs-comment">// scheduled</span>
53255 <span class="hljs-comment">// microtask 3</span></code></pre>
53268 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" …
53270 <p>The <code>process.noDeprecation</code> property indicates whether the <code>--no-deprecation</co…
53280 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" c…
53283-js"><span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</…
53289 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" c…
53303-js"><span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</…
53306 …s://github.com/nodejs/node/blob/HEAD/BUILDING.md#androidandroid-based-devices-eg-firefox-os">is ex…
53312 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" c…
53316-js"><span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</…
53330 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Objec…
53333 to the current release, including URLs for the source tarball and headers-only
53337 <li><code>name</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
53338 <li><code>sourceUrl</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_st…
53340 <li><code>headersUrl</code><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_st…
53343 Node.js native add-ons.</li>
53344 <li><code>libUrl</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struc…
53346 compiling Node.js native add-ons. <em>This property is only present on Windows
53348 <li><code>lts</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structur…
53359 <!-- eslint-skip -->
53360 <pre><code class="language-js">{
53361 <span class="hljs-attr">name</span>: <span class="hljs-string">'node'</span>,
53362 <span class="hljs-attr">lts</span>: <span class="hljs-string">'Erbium'</span>,
53363 …<span class="hljs-attr">sourceUrl</span>: <span class="hljs-string">'https://nodejs.org/download/r…
53364 …<span class="hljs-attr">headersUrl</span>: <span class="hljs-string">'https://nodejs.org/download/…
53365 …<span class="hljs-attr">libUrl</span>: <span class="hljs-string">'https://nodejs.org/download/rele…
53367 <p>In custom builds from non-release versions of the source tree, only the
53383 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Objec…
53393 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" …
53395 <p>Write reports in a compact format, single-line JSON, more easily consumable
53396 by log processing systems than the default multi-line format designed for
53398-js"><span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</…
53412 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" c…
53417-js"><span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</…
53431 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" c…
53436-js"><span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</…
53450 <li><code>err</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Glo…
53451 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Obje…
53456 … class="language-js"><span class="hljs-keyword">const</span> data = process.<span class="hljs-prop…
53457 …ljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(data.<span cl…
53459 <span class="hljs-comment">// Similar to process.report.writeReport()</span>
53460 <span class="hljs-keyword">const</span> fs = <span class="hljs-built_in">require</span>(<span class…
53461-title function_">writeFileSync</span>(<span class="hljs-string">'my-report.log'</span>, util.<spa…
53476 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" …
53480-js"><span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</…
53494 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" …
53498-js"><span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</…
53512 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" …
53515-js"><span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</…
53529 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" c…
53533-js"><span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</…
53548 <p><code>filename</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_stru…
53554 <p><code>err</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Glob…
53557 <p>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String…
53563 <pre><code class="language-js">process.<span class="hljs-property">report</span>.<span class="hljs-
53570 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Obje…
53574 <li><code>userCPUTime</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_…
53576 <li><code>systemCPUTime</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Dat…
53578 <li><code>maxRSS</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struc…
53580 <li><code>sharedMemorySize</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/…
53582 <li><code>unsharedDataSize</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/…
53584 <li><code>unsharedStackSize</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript…
53586 <li><code>minorPageFault</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Da…
53589 <li><code>majorPageFault</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Da…
53593 <li><code>swappedOut</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_s…
53595 <li><code>fsRead</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struc…
53597 <li><code>fsWrite</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_stru…
53599 <li><code>ipcSent</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_stru…
53601 <li><code>ipcReceived</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_…
53603 <li><code>signalsCount</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data…
53605 <li><code>voluntaryContextSwitches</code> <a href="https://developer.mozilla.org/en-US/docs/Web/Jav…
53609 <li><code>involuntaryContextSwitches</code> <a href="https://developer.mozilla.org/en-US/docs/Web/J…
53616 …s="language-js"><span class="hljs-variable language_">console</span>.<span class="hljs-title funct…
53617 <span class="hljs-comment">/*
53643 <li><code>message</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference…
53645 <li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference…
53648 <li><code>keepOpen</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_str…
53653 <li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Referenc…
53654 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boole…
53668 …zilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | …
53671 (See <a href="http://man7.org/linux/man-pages/man2/setegid.2.html"><code>setegid(2)</code></a>.) Th…
53674 …="language-js"><span class="hljs-keyword">if</span> (process.<span class="hljs-property">getegid</…
53675 …="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span clas…
53676 <span class="hljs-keyword">try</span> {
53677 process.<span class="hljs-title function_">setegid</span>(<span class="hljs-number">501</span>);
53678 …ss="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span cl…
53679 } <span class="hljs-keyword">catch</span> (err) {
53680 …ljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="…
53691 …zilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | …
53694 (See <a href="http://man7.org/linux/man-pages/man2/seteuid.2.html"><code>seteuid(2)</code></a>.) Th…
53697 …="language-js"><span class="hljs-keyword">if</span> (process.<span class="hljs-property">geteuid</…
53698 …="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span clas…
53699 <span class="hljs-keyword">try</span> {
53700 process.<span class="hljs-title function_">seteuid</span>(<span class="hljs-number">501</span>);
53701 …ss="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span cl…
53702 } <span class="hljs-keyword">catch</span> (err) {
53703 …ljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="…
53714 …zilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | …
53717 <a href="http://man7.org/linux/man-pages/man2/setgid.2.html"><code>setgid(2)</code></a>.) The <code…
53720 …="language-js"><span class="hljs-keyword">if</span> (process.<span class="hljs-property">getgid</s…
53721 …="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span clas…
53722 <span class="hljs-keyword">try</span> {
53723 process.<span class="hljs-title function_">setgid</span>(<span class="hljs-number">501</span>);
53724 …ss="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span cl…
53725 } <span class="hljs-keyword">catch</span> (err) {
53726 …ljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="…
53737 <li><code>groups</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struc…
53743 …"language-js"><span class="hljs-keyword">if</span> (process.<span class="hljs-property">getgroups<…
53744 <span class="hljs-keyword">try</span> {
53745 …process.<span class="hljs-title function_">setgroups</span>([<span class="hljs-number">501</span>]…
53746-variable language_">console</span>.<span class="hljs-title function_">log</span>(process.<span cl…
53747 } <span class="hljs-keyword">catch</span> (err) {
53748 …js-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="h…
53759 …zilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;integer></a> |…
53762 <a href="http://man7.org/linux/man-pages/man2/setuid.2.html"><code>setuid(2)</code></a>.) The <code…
53765 …="language-js"><span class="hljs-keyword">if</span> (process.<span class="hljs-property">getuid</s…
53766 …="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span clas…
53767 <span class="hljs-keyword">try</span> {
53768 process.<span class="hljs-title function_">setuid</span>(<span class="hljs-number">501</span>);
53769 …ss="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span cl…
53770 } <span class="hljs-keyword">catch</span> (err) {
53771 …ljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="…
53781 … api_stability_1"><a href="#documentation_stability_index">Stability: 1</a> - Experimental</div><p…
53783 <li><code>val</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structur…
53788 <code>--enable-source-maps</code>.</p>
53796 …a.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type">&#x3C;Function></a…
53802 not be emitted. If <code>--abort-on-uncaught-exception</code> was passed from the
53808 method with a non-<code>null</code> argument while another capture function is set will
53811 <a href="domain.html"><code>domain</code></a> built-in module.</p>
53824 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" c…
53846 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" c…
53860 …ass="language-js">process.<span class="hljs-property">stdin</span>.<span class="hljs-title functio…
53865 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" c…
53902 <pre><code class="language-console"><span class="hljs-meta">$ </span><span class="language-bash">no…
53904 …s-meta">$ </span><span class="language-bash"><span class="hljs-built_in">echo</span> <span class="…
53906 <span class="hljs-meta">$ </span><span class="language-bash">node -p <span class="hljs-string">"Boo…
53908 <span class="hljs-meta">$ </span><span class="language-bash">node -p <span class="hljs-string">"Boo…
53916 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" …
53919 <code>--throw-deprecation</code> flag is set on the current Node.js process.
53924 …de class="language-console"><span class="hljs-meta">$ </span><span class="language-bash">node --th…
53926 <span class="hljs-meta">$ </span><span class="language-bash">node -p <span class="hljs-string">"pro…
53928 <span class="hljs-meta">$ </span><span class="language-bash">node</span>
53929 …pan class="hljs-meta">> </span><span class="language-bash">process.emitWarning(<span class="hljs-s…
53931 <span class="hljs-meta">> </span><span class="language-bash">(node:26598) DeprecationWarning: <span…
53932 <span class="hljs-meta">> </span><span class="language-bash">process.throwDeprecation = <span class…
53934 …pan class="hljs-meta">> </span><span class="language-bash">process.emitWarning(<span class="hljs-s…
53942 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" c…
53950 binary name plus the length of the command-line arguments because setting the
53963 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" …
53966 <code>--trace-deprecation</code> flag is set on the current Node.js process. See the
53982 … api_stability_0"><a href="#documentation_stability_index">Stability: 0</a> - Deprecated. Calling …
53983 the process-wide umask to be written twice. This introduces a race condition
53985 cross-platform alternative API.</div><p></p>
53993 …zilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | …
53997 <pre><code class="language-js"><span class="hljs-keyword">const</span> newmask = <span class="hljs-
53998 <span class="hljs-keyword">const</span> oldmask = process.<span class="hljs-title function_">umask<…
53999 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(
54000-string">`Changed umask from <span class="hljs-subst">${oldmask.toString(<span class="hljs-number"…
54008 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Numbe…
54019 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" c…
54022-js"><span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</…
54023 <span class="hljs-comment">// Version: v14.8.0</span></code></pre>
54041 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Objec…
54047 …s="language-js"><span class="hljs-variable language_">console</span>.<span class="hljs-title funct…
54049 <pre><code class="language-console">{ node: '11.13.0',
54050 v8: '7.0.276.38-node.18',
54083 <li><code>6</code> <strong>Non-function Internal Exception Handler</strong>: There was an
54085 function was somehow set to a non-function, and could not be called.</li>
54086 <li><code>7</code> <strong>Internal Exception Handler Run-Time Failure</strong>: There was an
54095 <li><code>10</code> <strong>Internal JavaScript Run-Time Failure</strong>: The JavaScript
54099 …ode> <strong>Invalid Debug Argument</strong>: The <code>--inspect</code> and/or <code>--inspect-br…
54101 <li><code>13</code> <strong>Unfinished Top-Level Await</strong>: <code>await</code> was used outsid…
54102 in the top-level code, but the passed <code>Promise</code> never resolved.</li>
54106 exit codes are defined to be 7-bit integers, and signal exits set
54107 the high-order bit, and then contain the value of the signal code.
54117 … api_stability_0"><a href="#documentation_stability_index">Stability: 0</a> - Deprecated</div><p><…
54122 userland-provided <a href="https://github.com/bestiejs/punycode.js">Punycode.js</a> module instead.…
54127 …de class="language-js"><span class="hljs-keyword">const</span> punycode = <span class="hljs-built_…
54131 non-ASCII characters must be converted into ASCII using the Punycode scheme.
54135 <code>'xn--fsq.com'</code>.</p>
54137 <p>The <code>punycode</code> module is a third-party dependency used by Node.js and
54145 <li><code>string</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struc…
54147 …> method converts a <a href="https://tools.ietf.org/html/rfc3492">Punycode</a> string of ASCII-only
54149 …ss="language-js">punycode.<span class="hljs-title function_">decode</span>(<span class="hljs-strin…
54150 …ode.<span class="hljs-title function_">decode</span>(<span class="hljs-string">'--dqo34k'</span>);…
54156 <li><code>string</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struc…
54159 <a href="https://tools.ietf.org/html/rfc3492">Punycode</a> string of ASCII-only characters.</p>
54160 …uage-js">punycode.<span class="hljs-title function_">encode</span>(<span class="hljs-string">'maña…
54161 …ycode.<span class="hljs-title function_">encode</span>(<span class="hljs-string">'☃-⌘'</span>); <s…
54167 <li><code>domain</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struc…
54170 …Name to <a href="https://tools.ietf.org/html/rfc3492">Punycode</a>. Only the non-ASCII parts of the
54173 <pre><code class="language-js"><span class="hljs-comment">// encode domain names</span>
54174 …pan class="hljs-title function_">toASCII</span>(<span class="hljs-string">'mañana.com'</span>); <…
54175 ….<span class="hljs-title function_">toASCII</span>(<span class="hljs-string">'☃-⌘.com'</span>); …
54176 punycode.<span class="hljs-title function_">toASCII</span>(<span class="hljs-string">'example.com'<…
54182 <li><code>domain</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struc…
54187 <pre><code class="language-js"><span class="hljs-comment">// decode domain names</span>
54188 …code.<span class="hljs-title function_">toUnicode</span>(<span class="hljs-string">'xn--maana-pta.…
54189 …pan class="hljs-title function_">toUnicode</span>(<span class="hljs-string">'xn----dqo34k.com'</sp…
54190 …code.<span class="hljs-title function_">toUnicode</span>(<span class="hljs-string">'example.com'</…
54200 <li><code>string</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struc…
54204-js">punycode.<span class="hljs-property">ucs2</span>.<span class="hljs-title function_">decode</s…
54205 <span class="hljs-comment">// surrogate pair for U+1D306 tetragram for centre:</span>
54206 …="hljs-property">ucs2</span>.<span class="hljs-title function_">decode</span>(<span class="hljs-st…
54212 <li><code>codePoints</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_s…
54216-js">punycode.<span class="hljs-property">ucs2</span>.<span class="hljs-title function_">encode</s…
54217 …ass="hljs-property">ucs2</span>.<span class="hljs-title function_">encode</span>([<span class="hlj…
54223 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" c…
54229 …ty api_stability_3"><a href="#documentation_stability_index">Stability: 3</a> - Legacy</div><p></p>
54234 … class="language-js"><span class="hljs-keyword">const</span> querystring = <span class="hljs-built…
54252 <li><code>str</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structur…
54254 <p>The <code>querystring.escape()</code> method performs URL percent-encoding on the given
54259 application code to provide a replacement percent-encoding implementation if
54278 <li><code>str</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structur…
54279 <li><code>sep</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structur…
54281 <li><code>eq</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structure…
54283 <li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference…
54285 <li><code>decodeURIComponent</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScrip…
54286 percent-encoded characters in the query string. <strong>Default:</strong>
54288 <li><code>maxKeys</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_stru…
54296 <!-- eslint-skip -->
54297 <pre><code class="language-js">{
54298 <span class="hljs-attr">foo</span>: <span class="hljs-string">'bar'</span>,
54299 …<span class="hljs-attr">abc</span>: [<span class="hljs-string">'xyz'</span>, <span class="hljs-str…
54305 <p>By default, percent-encoded characters within the query string will be assumed
54306 to use UTF-8 encoding. If an alternative character encoding is used, then an
54308 <pre><code class="language-js"><span class="hljs-comment">// Assuming gbkDecodeURIComponent functio…
54310-title function_">parse</span>(<span class="hljs-string">'w=%D6%D0%CE%C4&#x26;foo=bar'</span>, <sp…
54311 … { <span class="hljs-attr">decodeURIComponent</span>: gbkDecodeURIComponent });</code></pre>
54317 <li><code>obj</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Glo…
54318 <li><code>sep</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structur…
54320 <li><code>eq</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structure…
54324 <li><code>encodeURIComponent</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScrip…
54325 URL-unsafe characters to percent-encoding in the query string. <strong>Default:</strong>
54333-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | <a href="htt…
54336-js">querystring.<span class="hljs-title function_">stringify</span>({ <span class="hljs-attr">foo…
54337 <span class="hljs-comment">// Returns 'foo=bar&#x26;baz=qux&#x26;baz=quux&#x26;corge='</span>
54339-title function_">stringify</span>({ <span class="hljs-attr">foo</span>: <span class="hljs-string"…
54340 <span class="hljs-comment">// Returns 'foo:bar;baz:qux'</span></code></pre>
54341 <p>By default, characters requiring percent-encoding within the query string will
54342 be encoded as UTF-8. If an alternative encoding is required, then an alternative
54344 <pre><code class="language-js"><span class="hljs-comment">// Assuming gbkEncodeURIComponent functio…
54346-title function_">stringify</span>({ <span class="hljs-attr">w</span>: <span class="hljs-string">'…
54347 … { <span class="hljs-attr">encodeURIComponent</span>: gbkEncodeURIComponent });</code></pre>
54353 <li><code>str</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structur…
54355 <p>The <code>querystring.unescape()</code> method performs decoding of URL percent-encoded
54362 JavaScript built-in <code>decodeURIComponent()</code> method to decode. If that fails,
54367 …ty api_stability_2"><a href="#documentation_stability_index">Stability: 2</a> - Stable</div><p></p>
54372 …de class="language-js"><span class="hljs-keyword">const</span> readline = <span class="hljs-built_…
54374 …de class="language-js"><span class="hljs-keyword">const</span> readline = <span class="hljs-built_…
54376 <span class="hljs-keyword">const</span> rl = readline.<span class="hljs-title function_">createInte…
54377 <span class="hljs-attr">input</span>: process.<span class="hljs-property">stdin</span>,
54378 <span class="hljs-attr">output</span>: process.<span class="hljs-property">stdout</span>
54381 …js-title function_">question</span>(<span class="hljs-string">'What do you think of Node.js? '</sp…
54382 …<span class="hljs-comment">// <span class="hljs-doctag">TODO:</span> Log the answer in a database<…
54383-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hlj…
54385 rl.<span class="hljs-title function_">close</span>();
54412 end-of-transmission (EOT);</li>
54425 end-of-line input (<code>\n</code>, <code>\r</code>, or <code>\r\n</code>). This usually occurs whe…
54429 …nguage-js">rl.<span class="hljs-title function_">on</span>(<span class="hljs-string">'line'</span>…
54430 …ss="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span cl…
54444 …uage-js">rl.<span class="hljs-title function_">on</span>(<span class="hljs-string">'history'</span…
54445 …ss="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span cl…
54458 …code class="language-js">rl.<span class="hljs-title function_">on</span>(<span class="hljs-string"…
54459 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>…
54467 …ode class="language-js">rl.<span class="hljs-title function_">on</span>(<span class="hljs-string">…
54468 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>…
54476 brought back to the foreground using <a href="http://man7.org/linux/man-pages/man1/fg.1p.html"><cod…
54480 …ode class="language-js">rl.<span class="hljs-title function_">on</span>(<span class="hljs-string">…
54481 <span class="hljs-comment">// `prompt` will automatically resume the stream</span>
54482 rl.<span class="hljs-title function_">prompt</span>();
54494 …ode class="language-js">rl.<span class="hljs-title function_">on</span>(<span class="hljs-string">…
54495 …js-title function_">question</span>(<span class="hljs-string">'Are you sure you want to exit? '</s…
54496 …hljs-keyword">if</span> (answer.<span class="hljs-title function_">match</span>(<span class="hljs-
54507 <p>When the program is resumed using <a href="http://man7.org/linux/man-pages/man1/fg.1p.html"><cod…
54512 …ode class="language-js">rl.<span class="hljs-title function_">on</span>(<span class="hljs-string">…
54513 …<span class="hljs-comment">// This will override SIGTSTP and prevent the program from going to the…
54514 <span class="hljs-comment">// background.</span>
54515 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>…
54540 <li><code>preserveCursor</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Da…
54555 <li><code>query</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struct…
54557 <li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference…
54563 <li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Referenc…
54577-js">rl.<span class="hljs-title function_">question</span>(<span class="hljs-string">'What is your…
54578-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hlj…
54581 …e><code class="language-js"><span class="hljs-keyword">const</span> ac = <span class="hljs-keyword…
54582 <span class="hljs-keyword">const</span> signal = ac.<span class="hljs-property">signal</span>;
54584-title function_">question</span>(<span class="hljs-string">'What is your favorite food? '</span>,…
54585-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hlj…
54588 signal.<span class="hljs-title function_">addEventListener</span>(<span class="hljs-string">'abort'…
54589 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>…
54590 }, { <span class="hljs-attr">once</span>: <span class="hljs-literal">true</span> });
54592 …"hljs-built_in">setTimeout</span>(<span class="hljs-function">() =></span> ac.<span class="hljs-ti…
54596 …code class="language-js"><span class="hljs-keyword">const</span> util = <span class="hljs-built_in…
54597-keyword">const</span> question = util.<span class="hljs-title function_">promisify</span>(rl.<spa…
54599 …"hljs-keyword">async</span> <span class="hljs-keyword">function</span> <span class="hljs-title fun…
54600 <span class="hljs-keyword">try</span> {
54601 …="hljs-keyword">const</span> answer = <span class="hljs-keyword">await</span> <span class="hljs-ti…
54602-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hlj…
54603 } <span class="hljs-keyword">catch</span> (err) {
54604 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">error</spa…
54607 <span class="hljs-title function_">questionExample</span>();</code></pre>
54618 <li><code>prompt</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struc…
54627 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Strin…
54635 <li><code>data</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
54636 <li><code>key</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Glo…
54638 <li><code>ctrl</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
54639 <li><code>meta</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
54640 <li><code>shift</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struct…
54641 <li><code>name</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
54654 <pre><code class="language-js">rl.<span class="hljs-title function_">write</span>(<span class="hljs
54655 <span class="hljs-comment">// Simulate Ctrl+U to delete the line written previously</span>
54656-title function_">write</span>(<span class="hljs-literal">null</span>, { <span class="hljs-attr">c…
54672 <li>Returns: <a href="https://tc39.github.io/ecma262/#sec-asynciterator-interface" class="type">&#x…
54682 instead for performance-sensitive applications.</p>
54683-js"><span class="hljs-keyword">async</span> <span class="hljs-keyword">function</span> <span clas…
54684 …<span class="hljs-keyword">const</span> rl = readline.<span class="hljs-title function_">createInt…
54685 <span class="hljs-comment">// ...</span>
54688 …an class="hljs-keyword">for</span> <span class="hljs-keyword">await</span> (<span class="hljs-keyw…
54689 …<span class="hljs-comment">// Each line in the readline input will be successively available here …
54690 <span class="hljs-comment">// `line`.</span>
54709 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" c…
54720 … class="language-js"><span class="hljs-keyword">const</span> values = [<span class="hljs-string">'…
54721 …span class="hljs-keyword">const</span> rl = readline.<span class="hljs-title function_">createInte…
54722 <span class="hljs-keyword">const</span> showResults = <span class="hljs-title function_">debounce</…
54723 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(
54724 <span class="hljs-string">'\n'</span>,
54725-title function_">filter</span>(<span class="hljs-function">(<span class="hljs-params">val</span>)…
54727 }, <span class="hljs-number">300</span>);
54728-property">stdin</span>.<span class="hljs-title function_">on</span>(<span class="hljs-string">'ke…
54729 <span class="hljs-title function_">showResults</span>();
54736 …zilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;number></a> | …
54748 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Obje…
54750 <li><code>rows</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
54751 <li><code>cols</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
54772 <li><code>dir</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structur…
54774 <li><code>-1</code>: to the left from cursor</li>
54779 <li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Referenc…
54780 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boole…
54800 <li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Referenc…
54801 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boole…
54832 <li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference…
54838 <li><code>completer</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Referen…
54839 <li><code>terminal</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_str…
54842 <li><code>history</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_stru…
54846 <li><code>historySize</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_…
54851 <li><code>removeHistoryDuplicates</code> <a href="https://developer.mozilla.org/en-US/docs/Web/Java…
54854 <li><code>prompt</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struc…
54855 <li><code>crlfDelay</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_st…
54857 end-of-line input. <code>crlfDelay</code> will be coerced to a number no less than
54861 <li><code>escapeCodeTimeout</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript…
54866 <li><code>tabSize</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_stru…
54876 …de class="language-js"><span class="hljs-keyword">const</span> readline = <span class="hljs-built_…
54877 <span class="hljs-keyword">const</span> rl = readline.<span class="hljs-title function_">createInte…
54878 <span class="hljs-attr">input</span>: process.<span class="hljs-property">stdin</span>,
54879 <span class="hljs-attr">output</span>: process.<span class="hljs-property">stdout</span>
54883 …nguage-js">rl.<span class="hljs-title function_">on</span>(<span class="hljs-string">'line'</span>…
54884 …ss="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span cl…
54896 <pre><code class="language-js">process.<span class="hljs-property">stdin</span>.<span class="hljs-t…
54905 …e class="language-js"><span class="hljs-keyword">function</span> <span class="hljs-title function_…
54906-keyword">const</span> completions = <span class="hljs-string">'.help .error .exit .quit .q'</span…
54907-keyword">const</span> hits = completions.<span class="hljs-title function_">filter</span>(<span c…
54908 <span class="hljs-comment">// Show all completions if none found</span>
54909 …<span class="hljs-keyword">return</span> [hits.<span class="hljs-property">length</span> ? hits : …
54913 …e class="language-js"><span class="hljs-keyword">function</span> <span class="hljs-title function_…
54914 …<span class="hljs-title function_">callback</span>(<span class="hljs-literal">null</span>, [[<span…
54930 <li><code>x</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures…
54931 <li><code>y</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures…
54932 <li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Referenc…
54933 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boole…
54950 autocompletion is disabled when copy-pasted input is detected.</p>
54955 <pre><code class="language-js">readline.<span class="hljs-title function_">emitKeypressEvents</span…
54956 <span class="hljs-keyword">if</span> (process.<span class="hljs-property">stdin</span>.<span class=…
54957 …process.<span class="hljs-property">stdin</span>.<span class="hljs-title function_">setRawMode</sp…
54972 <li><code>dx</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structure…
54973 <li><code>dy</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structure…
54974 <li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Referenc…
54975 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boole…
54983 implement a small command-line interface:</p>
54984 …de class="language-js"><span class="hljs-keyword">const</span> readline = <span class="hljs-built_…
54985 <span class="hljs-keyword">const</span> rl = readline.<span class="hljs-title function_">createInte…
54986 <span class="hljs-attr">input</span>: process.<span class="hljs-property">stdin</span>,
54987 <span class="hljs-attr">output</span>: process.<span class="hljs-property">stdout</span>,
54988 <span class="hljs-attr">prompt</span>: <span class="hljs-string">'OHAI> '</span>
54991 rl.<span class="hljs-title function_">prompt</span>();
54993 ….<span class="hljs-title function_">on</span>(<span class="hljs-string">'line'</span>, <span class…
54994 <span class="hljs-keyword">switch</span> (line.<span class="hljs-title function_">trim</span>()) {
54995 <span class="hljs-keyword">case</span> <span class="hljs-string">'hello'</span>:
54996 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>…
54997 <span class="hljs-keyword">break</span>;
54998 <span class="hljs-attr">default</span>:
54999-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hlj…
55000 <span class="hljs-keyword">break</span>;
55002 rl.<span class="hljs-title function_">prompt</span>();
55003 }).<span class="hljs-title function_">on</span>(<span class="hljs-string">'close'</span>, <span cla…
55004 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>…
55005 process.<span class="hljs-title function_">exit</span>(<span class="hljs-number">0</span>);
55007 </section><section><h3>Example: Read file stream line-by-Line<span><a class="mark" href="#readline_…
55011 …<code class="language-js"><span class="hljs-keyword">const</span> fs = <span class="hljs-built_in"…
55012 <span class="hljs-keyword">const</span> readline = <span class="hljs-built_in">require</span>(<span…
55014 …hljs-keyword">async</span> <span class="hljs-keyword">function</span> <span class="hljs-title func…
55015 …<span class="hljs-keyword">const</span> fileStream = fs.<span class="hljs-title function_">createR…
55017 …<span class="hljs-keyword">const</span> rl = readline.<span class="hljs-title function_">createInt…
55018 <span class="hljs-attr">input</span>: fileStream,
55019 <span class="hljs-attr">crlfDelay</span>: <span class="hljs-title class_">Infinity</span>
55021 …<span class="hljs-comment">// Note: we use the crlfDelay option to recognize all instances of CR L…
55022 <span class="hljs-comment">// ('\r\n') in input.txt as a single line break.</span>
55024 …an class="hljs-keyword">for</span> <span class="hljs-keyword">await</span> (<span class="hljs-keyw…
55025 …<span class="hljs-comment">// Each line in input.txt will be successively available here as `line`…
55026 …"hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class…
55030 <span class="hljs-title function_">processLineByLine</span>();</code></pre>
55032 …<code class="language-js"><span class="hljs-keyword">const</span> fs = <span class="hljs-built_in"…
55033 <span class="hljs-keyword">const</span> readline = <span class="hljs-built_in">require</span>(<span…
55035 <span class="hljs-keyword">const</span> rl = readline.<span class="hljs-title function_">createInte…
55036 …<span class="hljs-attr">input</span>: fs.<span class="hljs-title function_">createReadStream</span…
55037 <span class="hljs-attr">crlfDelay</span>: <span class="hljs-title class_">Infinity</span>
55040 ….<span class="hljs-title function_">on</span>(<span class="hljs-string">'line'</span>, <span class…
55041 …"hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class…
55045 …de class="language-js"><span class="hljs-keyword">const</span> { once } = <span class="hljs-built_…
55046 <span class="hljs-keyword">const</span> { createReadStream } = <span class="hljs-built_in">require<…
55047 <span class="hljs-keyword">const</span> { createInterface } = <span class="hljs-built_in">require</…
55049 …hljs-keyword">async</span> <span class="hljs-keyword">function</span> <span class="hljs-title func…
55050 <span class="hljs-keyword">try</span> {
55051 …<span class="hljs-keyword">const</span> rl = <span class="hljs-title function_">createInterface</s…
55052 …<span class="hljs-attr">input</span>: <span class="hljs-title function_">createReadStream</span>(<…
55053 <span class="hljs-attr">crlfDelay</span>: <span class="hljs-title class_">Infinity</span>
55056 ….<span class="hljs-title function_">on</span>(<span class="hljs-string">'line'</span>, <span class…
55057 <span class="hljs-comment">// Process the line.</span>
55060 …<span class="hljs-keyword">await</span> <span class="hljs-title function_">once</span>(rl, <span c…
55062 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>…
55063 } <span class="hljs-keyword">catch</span> (err) {
55064 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">error</spa…
55193 …ty api_stability_2"><a href="#documentation_stability_index">Stability: 2</a> - Stable</div><p></p>
55195 <p>The <code>repl</code> module provides a Read-Eval-Print-Loop (REPL) implementation that
55198 …code class="language-js"><span class="hljs-keyword">const</span> repl = <span class="hljs-built_in…
55202 evaluate those according to a user-defined evaluation function, then output the
55206 completion preview, simplistic Emacs-style line editing, multi-line inputs,
55207 …dia.org/wiki/Z_shell">ZSH</a>-like reverse-i-search, <a href="https://en.wikipedia.org/wiki/Z_shel…
55208 ANSI-styled output, saving and restoring current REPL session state, error
55210 ANSI styles and Emacs-style line editing automatically fall back to a limited
55215 <li><code>.break</code>: When in the process of inputting a multi-line expression, enter
55219 multi-line expression being input.</li>
55229 <pre><code class="language-console"><span class="hljs-meta">> </span><span class="language-bash">.e…
55239 <span class="hljs-meta">></span></code></pre>
55251 <p>For key bindings related to the reverse-i-search, see <a href="#repl_reverse_i_search"><code>rev…
55255 that evaluates JavaScript expressions and provides access to Node.js built-in
55260 <pre><code class="language-console"><span class="hljs-meta">> </span><span class="language-bash">1 …
55262 <span class="hljs-meta">> </span><span class="language-bash">const m = 2</span>
55264 <span class="hljs-meta">> </span><span class="language-bash">m + 1</span>
55273 …code class="language-js"><span class="hljs-keyword">const</span> repl = <span class="hljs-built_in…
55274 <span class="hljs-keyword">const</span> msg = <span class="hljs-string">'message'</span>;
55276 …class="hljs-title function_">start</span>(<span class="hljs-string">'> '</span>).<span class="hljs
55278 <pre><code class="language-console"><span class="hljs-meta">$ </span><span class="language-bash">no…
55279 <span class="hljs-meta">> </span><span class="language-bash">m</span>
55281 <p>Context properties are not read-only by default. To specify read-only globals,
55283 …code class="language-js"><span class="hljs-keyword">const</span> repl = <span class="hljs-built_in…
55284 <span class="hljs-keyword">const</span> msg = <span class="hljs-string">'message'</span>;
55286 <span class="hljs-keyword">const</span> r = repl.<span class="hljs-title function_">start</span>(<s…
55287 …s-title class_">Object</span>.<span class="hljs-title function_">defineProperty</span>(r.<span cla…
55288 <span class="hljs-attr">configurable</span>: <span class="hljs-literal">false</span>,
55289 <span class="hljs-attr">enumerable</span>: <span class="hljs-literal">true</span>,
55290 <span class="hljs-attr">value</span>: msg
55295 global or scoped variable, the input <code>fs</code> will be evaluated on-demand as
55297 …><code class="language-console"><span class="hljs-meta">> </span><span class="language-bash">fs.cr…
55316 <pre><code class="language-js"><span class="hljs-keyword">const</span> r = repl.<span class="hljs-t…
55318 r.<span class="hljs-title function_">write</span>(<span class="hljs-string">'process.on("uncaughtEx…
55319 <span class="hljs-comment">// Output stream includes:</span>
55320 <span class="hljs-comment">// TypeError [ERR_INVALID_REPL_INPUT]: Listeners for `uncaughtExceptio…
55321 <span class="hljs-comment">// cannot be used in the REPL</span>
55323 r.<span class="hljs-title function_">close</span>();</code></pre>
55343-console"><span class="hljs-meta">> </span><span class="language-bash">[ <span class="hljs-string"…
55345 <span class="hljs-meta">> </span><span class="language-bash">_.length</span>
55347 <span class="hljs-meta">> </span><span class="language-bash">_ += 1</span>
55350 <span class="hljs-meta">> </span><span class="language-bash">1 + 1</span>
55352 <span class="hljs-meta">> </span><span class="language-bash">_</span>
55356 …re><code class="language-console"><span class="hljs-meta">> </span><span class="language-bash">thr…
55358 <span class="hljs-meta">> </span><span class="language-bash">_error.message</span>
55361 …With the <a href="#cli_experimental_repl_await"><code>--experimental-repl-await</code></a> command
55363 <pre><code class="language-console"><span class="hljs-meta">> </span><span class="language-bash">aw…
55365 <span class="hljs-meta">> </span><span class="language-bash">await Promise.reject(new Error(<span c…
55368 <span class="hljs-meta">> </span><span class="language-bash">const timeout = util.promisify(setTime…
55370 <span class="hljs-meta">> </span><span class="language-bash">const old = Date.now(); await timeout(…
55377 <pre><code class="language-console"><span class="hljs-meta">> </span><span class="language-bash">co…
55379 <span class="hljs-meta">> </span><span class="language-bash">m</span>
55381 <span class="hljs-meta">> </span><span class="language-bash">const m = await Promise.resolve(234)</…
55383 <span class="hljs-meta">> </span><span class="language-bash">m</span>
55385 <h4>Reverse-i-search<span><a class="mark" href="#repl_reverse_i_search" id="repl_reverse_i_search">…
55389 <p>The REPL supports bi-directional reverse-i-search similar to <a href="https://en.wikipedia.org/w…
55405 …code class="language-js"><span class="hljs-keyword">const</span> repl = <span class="hljs-built_in…
55406 …="hljs-keyword">const</span> { <span class="hljs-title class_">Translator</span> } = <span class="…
55408-keyword">const</span> myTranslator = <span class="hljs-keyword">new</span> <span class="hljs-titl…
55410 <span class="hljs-keyword">function</span> <span class="hljs-title function_">myEval</span>(<span c…
55411 …<span class="hljs-title function_">callback</span>(<span class="hljs-literal">null</span>, myTrans…
55414 … class="hljs-title function_">start</span>({ <span class="hljs-attr">prompt</span>: <span class="h…
55417 the <code>eval</code> function. In order to support multi-line input, the <code>eval</code> function
55419 …ode class="language-js"><span class="hljs-keyword">function</span> <span class="hljs-title functio…
55420 <span class="hljs-keyword">let</span> result;
55421 <span class="hljs-keyword">try</span> {
55422 result = vm.<span class="hljs-title function_">runInThisContext</span>(cmd);
55423 } <span class="hljs-keyword">catch</span> (e) {
55424 …<span class="hljs-keyword">if</span> (<span class="hljs-title function_">isRecoverableError</span>…
55425 …ss="hljs-keyword">return</span> <span class="hljs-title function_">callback</span>(<span class="hl…
55428 …<span class="hljs-title function_">callback</span>(<span class="hljs-literal">null</span>, result);
55431 <span class="hljs-keyword">function</span> <span class="hljs-title function_">isRecoverableError</s…
55432 …<span class="hljs-keyword">if</span> (error.<span class="hljs-property">name</span> === <span clas…
55433-keyword">return</span> <span class="hljs-regexp">/^(Unexpected end of input|Unexpected token)/</s…
55435 <span class="hljs-keyword">return</span> <span class="hljs-literal">false</span>;
55450 …lass="language-console"><span class="hljs-meta">> </span><span class="language-bash">util.inspect.…
55452 <span class="hljs-meta">> </span><span class="language-bash">[1]</span>
55456 <span class="hljs-meta">></span></code></pre>
55460 …code class="language-js"><span class="hljs-keyword">const</span> repl = <span class="hljs-built_in…
55462-keyword">const</span> r = repl.<span class="hljs-title function_">start</span>({ <span class="hlj…
55464 <span class="hljs-keyword">function</span> <span class="hljs-title function_">myEval</span>(<span c…
55465 <span class="hljs-title function_">callback</span>(<span class="hljs-literal">null</span>, cmd);
55468 <span class="hljs-keyword">function</span> <span class="hljs-title function_">myWriter</span>(<span…
55469 …<span class="hljs-keyword">return</span> output.<span class="hljs-title function_">toUpperCase</sp…
55476 …lla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type">&#x3C;Object></a> …
55481 …code class="language-js"><span class="hljs-keyword">const</span> repl = <span class="hljs-built_in…
55483 <span class="hljs-keyword">const</span> options = { <span class="hljs-attr">useColors</span>: <span…
55485 <span class="hljs-keyword">const</span> firstInstance = repl.<span class="hljs-title function_">sta…
55486 <span class="hljs-keyword">const</span> secondInstance = <span class="hljs-keyword">new</span> repl…
55497 … class="language-js">replServer.<span class="hljs-title function_">on</span>(<span class="hljs-str…
55498 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>…
55499 process.<span class="hljs-title function_">exit</span>();
55510 <p>This can be used primarily to re-initialize REPL context to some pre-defined
55512 …code class="language-js"><span class="hljs-keyword">const</span> repl = <span class="hljs-built_in…
55514 <span class="hljs-keyword">function</span> <span class="hljs-title function_">initializeContext</sp…
55515 context.<span class="hljs-property">m</span> = <span class="hljs-string">'test'</span>;
55518 …s="hljs-keyword">const</span> r = repl.<span class="hljs-title function_">start</span>({ <span cla…
55519 <span class="hljs-title function_">initializeContext</span>(r.<span class="hljs-property">context</…
55521 r.<span class="hljs-title function_">on</span>(<span class="hljs-string">'reset'</span>, initialize…
55524 <pre><code class="language-console"><span class="hljs-meta">$ </span><span class="language-bash">./…
55525 <span class="hljs-meta">> </span><span class="language-bash">m</span>
55527 <span class="hljs-meta">> </span><span class="language-bash">m = 1</span>
55529 <span class="hljs-meta">> </span><span class="language-bash">m</span>
55531 <span class="hljs-meta">> </span><span class="language-bash">.clear</span>
55533 <span class="hljs-meta">> </span><span class="language-bash">m</span>
55535 <span class="hljs-meta">></span></code></pre>
55541 <li><code>keyword</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_stru…
55542 …lla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type">&#x3C;Object></a> …
55544 <p>The <code>replServer.defineCommand()</code> method is used to add new <code>.</code>-prefixed co…
55549 <li><code>help</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
55550 <li><code>action</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/…
55554 …code class="language-js"><span class="hljs-keyword">const</span> repl = <span class="hljs-built_in…
55556 …js-keyword">const</span> replServer = repl.<span class="hljs-title function_">start</span>({ <span…
55557 replServer.<span class="hljs-title function_">defineCommand</span>(<span class="hljs-string">'sayhe…
55558 <span class="hljs-attr">help</span>: <span class="hljs-string">'Say hello'</span>,
55559 <span class="hljs-title function_">action</span>(<span class="hljs-params">name</span>) {
55560 …<span class="hljs-variable language_">this</span>.<span class="hljs-title function_">clearBuffered…
55561 …ass="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span c…
55562 …<span class="hljs-variable language_">this</span>.<span class="hljs-title function_">displayPrompt…
55565-title function_">defineCommand</span>(<span class="hljs-string">'saybye'</span>, <span class="hlj…
55566 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>…
55567 …<span class="hljs-variable language_">this</span>.<span class="hljs-title function_">close</span>(…
55570 <pre><code class="language-console"><span class="hljs-meta">> </span><span class="language-bash">.s…
55572 <span class="hljs-meta">> </span><span class="language-bash">.saybye</span>
55579 <li><code>preserveCursor</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Da…
55584 <p>When multi-line input is being entered, an ellipsis is printed rather than the
55602 … api_stability_0"><a href="#documentation_stability_index">Stability: 0</a> - Deprecated.</div><p>…
55604 <li><code>keyword</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_stru…
55605 <li><code>rest</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
55606 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boole…
55615 <li><code>historyPath</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_…
55616 <li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Referenc…
55618 <li><code>err</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Glo…
55624 Node.js binary and using the command-line REPL, a history file is initialized
55633 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" c…
55657 …lla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type">&#x3C;Object></a> …
55659 <li><code>prompt</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struc…
55665 <li><code>terminal</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_str…
55669 <li><code>eval</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Gl…
55673 <li><code>useColors</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_st…
55678 <li><code>useGlobal</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_st…
55682 <li><code>ignoreUndefined</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/D…
55685 <li><code>writer</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/…
55687 <li><code>completer</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Referen…
55689 <li><code>replMode</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_str…
55698 <li><code>breakEvalOnSigint</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript…
55702 <li><code>preview</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_stru…
55712 …code class="language-js"><span class="hljs-keyword">const</span> repl = <span class="hljs-built_in…
55714 <span class="hljs-comment">// a Unix style prompt</span>
55715 repl.<span class="hljs-title function_">start</span>(<span class="hljs-string">'$ '</span>);</code>…
55719 without passing any arguments (or by passing the <code>-i</code> argument):</p>
55720 <pre><code class="language-console"><span class="hljs-meta">$ </span><span class="language-bash">no…
55721 <span class="hljs-meta">> </span><span class="language-bash">const a = [1, 2, 3];</span>
55723 <span class="hljs-meta">> </span><span class="language-bash">a</span>
55725 <span class="hljs-meta">> </span><span class="language-bash">a.forEach((v) => {</span>
55745 <code>'sloppy'</code>, which will allow non-strict mode code to be run.</li>
55752 <h4>Using the Node.js REPL with advanced line-editors<span><a class="mark" href="#repl_using_the_no…
55753 <p>For advanced line-editors, start Node.js with the environment variable
55757 <pre><code class="language-text">alias node="env NODE_NO_READLINE=1 rlwrap node"</code></pre>
55764 …code class="language-js"><span class="hljs-keyword">const</span> net = <span class="hljs-built_in"…
55765 <span class="hljs-keyword">const</span> repl = <span class="hljs-built_in">require</span>(<span cla…
55766 <span class="hljs-keyword">let</span> connections = <span class="hljs-number">0</span>;
55768 repl.<span class="hljs-title function_">start</span>({
55769 <span class="hljs-attr">prompt</span>: <span class="hljs-string">'Node.js via stdin> '</span>,
55770 <span class="hljs-attr">input</span>: process.<span class="hljs-property">stdin</span>,
55771 <span class="hljs-attr">output</span>: process.<span class="hljs-property">stdout</span>
55774 net.<span class="hljs-title function_">createServer</span>(<span class="hljs-function">(<span class…
55775 connections += <span class="hljs-number">1</span>;
55776 repl.<span class="hljs-title function_">start</span>({
55777 …<span class="hljs-attr">prompt</span>: <span class="hljs-string">'Node.js via Unix socket> '</span…
55778 <span class="hljs-attr">input</span>: socket,
55779 <span class="hljs-attr">output</span>: socket
55780 …}).<span class="hljs-title function_">on</span>(<span class="hljs-string">'exit'</span>, <span cla…
55781 socket.<span class="hljs-title function_">end</span>();
55783 }).<span class="hljs-title function_">listen</span>(<span class="hljs-string">'/tmp/node-repl-sock'…
55785 net.<span class="hljs-title function_">createServer</span>(<span class="hljs-function">(<span class…
55786 connections += <span class="hljs-number">1</span>;
55787 repl.<span class="hljs-title function_">start</span>({
55788 …<span class="hljs-attr">prompt</span>: <span class="hljs-string">'Node.js via TCP socket> '</span>,
55789 <span class="hljs-attr">input</span>: socket,
55790 <span class="hljs-attr">output</span>: socket
55791 …}).<span class="hljs-title function_">on</span>(<span class="hljs-string">'exit'</span>, <span cla…
55792 socket.<span class="hljs-title function_">end</span>();
55794 }).<span class="hljs-title function_">listen</span>(<span class="hljs-number">5001</span>);</code><…
55799 <p>By starting a REPL from a Unix socket-based server instead of stdin, it is
55800 possible to connect to a long-running Node.js process without restarting it.</p>
55801 <p>For an example of running a "full-featured" (<code>terminal</code>) REPL over
55810 …ty api_stability_2"><a href="#documentation_stability_index">Stability: 2</a> - Stable</div><p></p>
55812 <p>Delivers a JSON-formatted diagnostic summary, written to a file.</p>
55821 <pre><code class="language-json"><span class="hljs-punctuation">{</span>
55822 …<span class="hljs-attr">"header"</span><span class="hljs-punctuation">:</span> <span class="hljs-p…
55823 …span class="hljs-attr">"reportVersion"</span><span class="hljs-punctuation">:</span> <span class="…
55824 …pan class="hljs-attr">"event"</span><span class="hljs-punctuation">:</span> <span class="hljs-stri…
55825 …an class="hljs-attr">"trigger"</span><span class="hljs-punctuation">:</span> <span class="hljs-str…
55826-attr">"filename"</span><span class="hljs-punctuation">:</span> <span class="hljs-string">"report.…
55827 …"hljs-attr">"dumpEventTime"</span><span class="hljs-punctuation">:</span> <span class="hljs-string…
55828 …="hljs-attr">"dumpEventTimeStamp"</span><span class="hljs-punctuation">:</span> <span class="hljs-
55829 …span class="hljs-attr">"processId"</span><span class="hljs-punctuation">:</span> <span class="hljs
55830 …s="hljs-attr">"cwd"</span><span class="hljs-punctuation">:</span> <span class="hljs-string">"/home…
55831 …<span class="hljs-attr">"commandLine"</span><span class="hljs-punctuation">:</span> <span class="h…
55832 …<span class="hljs-string">"/home/nodeuser/project/node/out/Release/node"</span><span class="hljs-p…
55833 …<span class="hljs-string">"--report-uncaught-exception"</span><span class="hljs-punctuation">,</sp…
55834 …<span class="hljs-string">"/home/nodeuser/project/node/test/report/test-exception.js"</span><span …
55835 <span class="hljs-string">"child"</span>
55836 <span class="hljs-punctuation">]</span><span class="hljs-punctuation">,</span>
55837 …lass="hljs-attr">"nodejsVersion"</span><span class="hljs-punctuation">:</span> <span class="hljs-s…
55838 …lass="hljs-attr">"glibcVersionRuntime"</span><span class="hljs-punctuation">:</span> <span class="…
55839 …lass="hljs-attr">"glibcVersionCompiler"</span><span class="hljs-punctuation">:</span> <span class=…
55840 …pan class="hljs-attr">"wordSize"</span><span class="hljs-punctuation">:</span> <span class="hljs-s…
55841 …<span class="hljs-attr">"arch"</span><span class="hljs-punctuation">:</span> <span class="hljs-str…
55842 …pan class="hljs-attr">"platform"</span><span class="hljs-punctuation">:</span> <span class="hljs-s…
55843 …<span class="hljs-attr">"componentVersions"</span><span class="hljs-punctuation">:</span> <span cl…
55844 …pan class="hljs-attr">"node"</span><span class="hljs-punctuation">:</span> <span class="hljs-strin…
55845 … class="hljs-attr">"v8"</span><span class="hljs-punctuation">:</span> <span class="hljs-string">"7…
55846 …<span class="hljs-attr">"uv"</span><span class="hljs-punctuation">:</span> <span class="hljs-strin…
55847 …<span class="hljs-attr">"zlib"</span><span class="hljs-punctuation">:</span> <span class="hljs-str…
55848 …<span class="hljs-attr">"ares"</span><span class="hljs-punctuation">:</span> <span class="hljs-str…
55849 …<span class="hljs-attr">"modules"</span><span class="hljs-punctuation">:</span> <span class="hljs-
55850 …pan class="hljs-attr">"nghttp2"</span><span class="hljs-punctuation">:</span> <span class="hljs-st…
55851 …<span class="hljs-attr">"napi"</span><span class="hljs-punctuation">:</span> <span class="hljs-str…
55852 …span class="hljs-attr">"llhttp"</span><span class="hljs-punctuation">:</span> <span class="hljs-st…
55853 …<span class="hljs-attr">"openssl"</span><span class="hljs-punctuation">:</span> <span class="hljs-
55854 <span class="hljs-punctuation">}</span><span class="hljs-punctuation">,</span>
55855 …<span class="hljs-attr">"release"</span><span class="hljs-punctuation">:</span> <span class="hljs-
55856 …<span class="hljs-attr">"name"</span><span class="hljs-punctuation">:</span> <span class="hljs-str…
55857 <span class="hljs-punctuation">}</span><span class="hljs-punctuation">,</span>
55858 …span class="hljs-attr">"osName"</span><span class="hljs-punctuation">:</span> <span class="hljs-st…
55859 …s="hljs-attr">"osRelease"</span><span class="hljs-punctuation">:</span> <span class="hljs-string">…
55860-attr">"osVersion"</span><span class="hljs-punctuation">:</span> <span class="hljs-string">"#1 SMP…
55861 …an class="hljs-attr">"osMachine"</span><span class="hljs-punctuation">:</span> <span class="hljs-s…
55862 …<span class="hljs-attr">"cpus"</span><span class="hljs-punctuation">:</span> <span class="hljs-pun…
55863 <span class="hljs-punctuation">{</span>
55864-attr">"model"</span><span class="hljs-punctuation">:</span> <span class="hljs-string">"Intel(R) C…
55865 …<span class="hljs-attr">"speed"</span><span class="hljs-punctuation">:</span> <span class="hljs-nu…
55866 …<span class="hljs-attr">"user"</span><span class="hljs-punctuation">:</span> <span class="hljs-num…
55867 …<span class="hljs-attr">"nice"</span><span class="hljs-punctuation">:</span> <span class="hljs-num…
55868 …<span class="hljs-attr">"sys"</span><span class="hljs-punctuation">:</span> <span class="hljs-numb…
55869 …span class="hljs-attr">"idle"</span><span class="hljs-punctuation">:</span> <span class="hljs-numb…
55870 …<span class="hljs-attr">"irq"</span><span class="hljs-punctuation">:</span> <span class="hljs-numb…
55871 <span class="hljs-punctuation">}</span><span class="hljs-punctuation">,</span>
55872 <span class="hljs-punctuation">{</span>
55873-attr">"model"</span><span class="hljs-punctuation">:</span> <span class="hljs-string">"Intel(R) C…
55874 …<span class="hljs-attr">"speed"</span><span class="hljs-punctuation">:</span> <span class="hljs-nu…
55875 …<span class="hljs-attr">"user"</span><span class="hljs-punctuation">:</span> <span class="hljs-num…
55876 …<span class="hljs-attr">"nice"</span><span class="hljs-punctuation">:</span> <span class="hljs-num…
55877 …<span class="hljs-attr">"sys"</span><span class="hljs-punctuation">:</span> <span class="hljs-numb…
55878 …span class="hljs-attr">"idle"</span><span class="hljs-punctuation">:</span> <span class="hljs-numb…
55879 …<span class="hljs-attr">"irq"</span><span class="hljs-punctuation">:</span> <span class="hljs-numb…
55880 <span class="hljs-punctuation">}</span>
55881 <span class="hljs-punctuation">]</span><span class="hljs-punctuation">,</span>
55882 …<span class="hljs-attr">"networkInterfaces"</span><span class="hljs-punctuation">:</span> <span cl…
55883 <span class="hljs-punctuation">{</span>
55884 …<span class="hljs-attr">"name"</span><span class="hljs-punctuation">:</span> <span class="hljs-str…
55885 …span class="hljs-attr">"internal"</span><span class="hljs-punctuation">:</span> <span class="hljs-
55886 … class="hljs-attr">"mac"</span><span class="hljs-punctuation">:</span> <span class="hljs-string">"…
55887 …an class="hljs-attr">"address"</span><span class="hljs-punctuation">:</span> <span class="hljs-str…
55888 … class="hljs-attr">"netmask"</span><span class="hljs-punctuation">:</span> <span class="hljs-strin…
55889 …<span class="hljs-attr">"family"</span><span class="hljs-punctuation">:</span> <span class="hljs-s…
55890 <span class="hljs-punctuation">}</span>
55891 <span class="hljs-punctuation">]</span><span class="hljs-punctuation">,</span>
55892 …<span class="hljs-attr">"host"</span><span class="hljs-punctuation">:</span> <span class="hljs-str…
55893 <span class="hljs-punctuation">}</span><span class="hljs-punctuation">,</span>
55894 …<span class="hljs-attr">"javascriptStack"</span><span class="hljs-punctuation">:</span> <span clas…
55895-attr">"message"</span><span class="hljs-punctuation">:</span> <span class="hljs-string">"Error: *…
55896 …<span class="hljs-attr">"stack"</span><span class="hljs-punctuation">:</span> <span class="hljs-pu…
55897 …<span class="hljs-string">"at myException (/home/nodeuser/project/node/test/report/test-exception.…
55898 …n class="hljs-string">"at Object.&#x3C;anonymous> (/home/nodeuser/project/node/test/report/test-ex…
55899 …<span class="hljs-string">"at Module._compile (internal/modules/cjs/loader.js:718:30)"</span><span…
55900 …<span class="hljs-string">"at Object.Module._extensions..js (internal/modules/cjs/loader.js:729:10…
55901 …<span class="hljs-string">"at Module.load (internal/modules/cjs/loader.js:617:32)"</span><span cla…
55902 …<span class="hljs-string">"at tryModuleLoad (internal/modules/cjs/loader.js:560:12)"</span><span c…
55903 …<span class="hljs-string">"at Function.Module._load (internal/modules/cjs/loader.js:552:3)"</span>…
55904 …<span class="hljs-string">"at Function.Module.runMain (internal/modules/cjs/loader.js:771:12)"</sp…
55905 <span class="hljs-string">"at executeUserCode (internal/bootstrap/node.js:332:15)"</span>
55906 <span class="hljs-punctuation">]</span>
55907 <span class="hljs-punctuation">}</span><span class="hljs-punctuation">,</span>
55908 …<span class="hljs-attr">"nativeStack"</span><span class="hljs-punctuation">:</span> <span class="h…
55909 <span class="hljs-punctuation">{</span>
55910 … class="hljs-attr">"pc"</span><span class="hljs-punctuation">:</span> <span class="hljs-string">"0…
55911 …<span class="hljs-attr">"symbol"</span><span class="hljs-punctuation">:</span> <span class="hljs-s…
55912 <span class="hljs-punctuation">}</span><span class="hljs-punctuation">,</span>
55913 <span class="hljs-punctuation">{</span>
55914 … class="hljs-attr">"pc"</span><span class="hljs-punctuation">:</span> <span class="hljs-string">"0…
55915 …<span class="hljs-attr">"symbol"</span><span class="hljs-punctuation">:</span> <span class="hljs-s…
55916 <span class="hljs-punctuation">}</span><span class="hljs-punctuation">,</span>
55917 <span class="hljs-punctuation">{</span>
55918 … class="hljs-attr">"pc"</span><span class="hljs-punctuation">:</span> <span class="hljs-string">"0…
55919 …<span class="hljs-attr">"symbol"</span><span class="hljs-punctuation">:</span> <span class="hljs-s…
55920 <span class="hljs-punctuation">}</span><span class="hljs-punctuation">,</span>
55921 <span class="hljs-punctuation">{</span>
55922 … class="hljs-attr">"pc"</span><span class="hljs-punctuation">:</span> <span class="hljs-string">"0…
55923 …<span class="hljs-attr">"symbol"</span><span class="hljs-punctuation">:</span> <span class="hljs-s…
55924 <span class="hljs-punctuation">}</span><span class="hljs-punctuation">,</span>
55925 <span class="hljs-punctuation">{</span>
55926 … class="hljs-attr">"pc"</span><span class="hljs-punctuation">:</span> <span class="hljs-string">"0…
55927 …<span class="hljs-attr">"symbol"</span><span class="hljs-punctuation">:</span> <span class="hljs-s…
55928 <span class="hljs-punctuation">}</span>
55929 <span class="hljs-punctuation">]</span><span class="hljs-punctuation">,</span>
55930 …<span class="hljs-attr">"javascriptHeap"</span><span class="hljs-punctuation">:</span> <span class…
55931 …an class="hljs-attr">"totalMemory"</span><span class="hljs-punctuation">:</span> <span class="hljs
55932 …ass="hljs-attr">"totalCommittedMemory"</span><span class="hljs-punctuation">:</span> <span class="…
55933 …an class="hljs-attr">"usedMemory"</span><span class="hljs-punctuation">:</span> <span class="hljs-
55934 …lass="hljs-attr">"availableMemory"</span><span class="hljs-punctuation">:</span> <span class="hljs
55935 … class="hljs-attr">"memoryLimit"</span><span class="hljs-punctuation">:</span> <span class="hljs-n…
55936 …<span class="hljs-attr">"heapSpaces"</span><span class="hljs-punctuation">:</span> <span class="hl…
55937 …<span class="hljs-attr">"read_only_space"</span><span class="hljs-punctuation">:</span> <span clas…
55938 …pan class="hljs-attr">"memorySize"</span><span class="hljs-punctuation">:</span> <span class="hljs
55939 …n class="hljs-attr">"committedMemory"</span><span class="hljs-punctuation">:</span> <span class="h…
55940 …span class="hljs-attr">"capacity"</span><span class="hljs-punctuation">:</span> <span class="hljs-
55941 …<span class="hljs-attr">"used"</span><span class="hljs-punctuation">:</span> <span class="hljs-num…
55942 …<span class="hljs-attr">"available"</span><span class="hljs-punctuation">:</span> <span class="hlj…
55943 <span class="hljs-punctuation">}</span><span class="hljs-punctuation">,</span>
55944 …<span class="hljs-attr">"new_space"</span><span class="hljs-punctuation">:</span> <span class="hlj…
55945 …an class="hljs-attr">"memorySize"</span><span class="hljs-punctuation">:</span> <span class="hljs-
55946 … class="hljs-attr">"committedMemory"</span><span class="hljs-punctuation">:</span> <span class="hl…
55947 …pan class="hljs-attr">"capacity"</span><span class="hljs-punctuation">:</span> <span class="hljs-n…
55948 …<span class="hljs-attr">"used"</span><span class="hljs-punctuation">:</span> <span class="hljs-num…
55949 …<span class="hljs-attr">"available"</span><span class="hljs-punctuation">:</span> <span class="hlj…
55950 <span class="hljs-punctuation">}</span><span class="hljs-punctuation">,</span>
55951 …<span class="hljs-attr">"old_space"</span><span class="hljs-punctuation">:</span> <span class="hlj…
55952 …an class="hljs-attr">"memorySize"</span><span class="hljs-punctuation">:</span> <span class="hljs-
55953 … class="hljs-attr">"committedMemory"</span><span class="hljs-punctuation">:</span> <span class="hl…
55954 …pan class="hljs-attr">"capacity"</span><span class="hljs-punctuation">:</span> <span class="hljs-n…
55955 …<span class="hljs-attr">"used"</span><span class="hljs-punctuation">:</span> <span class="hljs-num…
55956 …<span class="hljs-attr">"available"</span><span class="hljs-punctuation">:</span> <span class="hlj…
55957 <span class="hljs-punctuation">}</span><span class="hljs-punctuation">,</span>
55958 …<span class="hljs-attr">"code_space"</span><span class="hljs-punctuation">:</span> <span class="hl…
55959 …pan class="hljs-attr">"memorySize"</span><span class="hljs-punctuation">:</span> <span class="hljs
55960 … class="hljs-attr">"committedMemory"</span><span class="hljs-punctuation">:</span> <span class="hl…
55961 …span class="hljs-attr">"capacity"</span><span class="hljs-punctuation">:</span> <span class="hljs-
55962 …<span class="hljs-attr">"used"</span><span class="hljs-punctuation">:</span> <span class="hljs-num…
55963 …<span class="hljs-attr">"available"</span><span class="hljs-punctuation">:</span> <span class="hlj…
55964 <span class="hljs-punctuation">}</span><span class="hljs-punctuation">,</span>
55965 …<span class="hljs-attr">"map_space"</span><span class="hljs-punctuation">:</span> <span class="hlj…
55966 …pan class="hljs-attr">"memorySize"</span><span class="hljs-punctuation">:</span> <span class="hljs
55967 … class="hljs-attr">"committedMemory"</span><span class="hljs-punctuation">:</span> <span class="hl…
55968 …span class="hljs-attr">"capacity"</span><span class="hljs-punctuation">:</span> <span class="hljs-
55969 …<span class="hljs-attr">"used"</span><span class="hljs-punctuation">:</span> <span class="hljs-num…
55970 …<span class="hljs-attr">"available"</span><span class="hljs-punctuation">:</span> <span class="hlj…
55971 <span class="hljs-punctuation">}</span><span class="hljs-punctuation">,</span>
55972 …<span class="hljs-attr">"large_object_space"</span><span class="hljs-punctuation">:</span> <span c…
55973 …<span class="hljs-attr">"memorySize"</span><span class="hljs-punctuation">:</span> <span class="hl…
55974 …pan class="hljs-attr">"committedMemory"</span><span class="hljs-punctuation">:</span> <span class=…
55975 …an class="hljs-attr">"capacity"</span><span class="hljs-punctuation">:</span> <span class="hljs-nu…
55976 …<span class="hljs-attr">"used"</span><span class="hljs-punctuation">:</span> <span class="hljs-num…
55977 …<span class="hljs-attr">"available"</span><span class="hljs-punctuation">:</span> <span class="hlj…
55978 <span class="hljs-punctuation">}</span><span class="hljs-punctuation">,</span>
55979 …<span class="hljs-attr">"new_large_object_space"</span><span class="hljs-punctuation">:</span> <sp…
55980 …<span class="hljs-attr">"memorySize"</span><span class="hljs-punctuation">:</span> <span class="hl…
55981 …pan class="hljs-attr">"committedMemory"</span><span class="hljs-punctuation">:</span> <span class=…
55982 …<span class="hljs-attr">"capacity"</span><span class="hljs-punctuation">:</span> <span class="hljs
55983 …<span class="hljs-attr">"used"</span><span class="hljs-punctuation">:</span> <span class="hljs-num…
55984 …<span class="hljs-attr">"available"</span><span class="hljs-punctuation">:</span> <span class="hlj…
55985 <span class="hljs-punctuation">}</span>
55986 <span class="hljs-punctuation">}</span>
55987 <span class="hljs-punctuation">}</span><span class="hljs-punctuation">,</span>
55988 …<span class="hljs-attr">"resourceUsage"</span><span class="hljs-punctuation">:</span> <span class=…
55989 … class="hljs-attr">"userCpuSeconds"</span><span class="hljs-punctuation">:</span> <span class="hlj…
55990 …class="hljs-attr">"kernelCpuSeconds"</span><span class="hljs-punctuation">:</span> <span class="hl…
55991 …ss="hljs-attr">"cpuConsumptionPercent"</span><span class="hljs-punctuation">:</span> <span class="…
55992 …span class="hljs-attr">"maxRss"</span><span class="hljs-punctuation">:</span> <span class="hljs-nu…
55993 …<span class="hljs-attr">"pageFaults"</span><span class="hljs-punctuation">:</span> <span class="hl…
55994 …<span class="hljs-attr">"IORequired"</span><span class="hljs-punctuation">:</span> <span class="hl…
55995 …<span class="hljs-attr">"IONotRequired"</span><span class="hljs-punctuation">:</span> <span class=…
55996 <span class="hljs-punctuation">}</span><span class="hljs-punctuation">,</span>
55997 …<span class="hljs-attr">"fsActivity"</span><span class="hljs-punctuation">:</span> <span class="hl…
55998 …<span class="hljs-attr">"reads"</span><span class="hljs-punctuation">:</span> <span class="hljs-nu…
55999 …<span class="hljs-attr">"writes"</span><span class="hljs-punctuation">:</span> <span class="hljs-n…
56000 <span class="hljs-punctuation">}</span>
56001 <span class="hljs-punctuation">}</span><span class="hljs-punctuation">,</span>
56002 …<span class="hljs-attr">"uvthreadResourceUsage"</span><span class="hljs-punctuation">:</span> <spa…
56003 … class="hljs-attr">"userCpuSeconds"</span><span class="hljs-punctuation">:</span> <span class="hlj…
56004 …class="hljs-attr">"kernelCpuSeconds"</span><span class="hljs-punctuation">:</span> <span class="hl…
56005 …ss="hljs-attr">"cpuConsumptionPercent"</span><span class="hljs-punctuation">:</span> <span class="…
56006 …<span class="hljs-attr">"fsActivity"</span><span class="hljs-punctuation">:</span> <span class="hl…
56007 …<span class="hljs-attr">"reads"</span><span class="hljs-punctuation">:</span> <span class="hljs-nu…
56008 …<span class="hljs-attr">"writes"</span><span class="hljs-punctuation">:</span> <span class="hljs-n…
56009 <span class="hljs-punctuation">}</span>
56010 <span class="hljs-punctuation">}</span><span class="hljs-punctuation">,</span>
56011 …<span class="hljs-attr">"libuv"</span><span class="hljs-punctuation">:</span> <span class="hljs-pu…
56012 <span class="hljs-punctuation">{</span>
56013 …<span class="hljs-attr">"type"</span><span class="hljs-punctuation">:</span> <span class="hljs-str…
56014 …span class="hljs-attr">"is_active"</span><span class="hljs-punctuation">:</span> <span class="hljs
56015 …n class="hljs-attr">"is_referenced"</span><span class="hljs-punctuation">:</span> <span class="hlj…
56016 …ass="hljs-attr">"address"</span><span class="hljs-punctuation">:</span> <span class="hljs-string">…
56017 …<span class="hljs-attr">"details"</span><span class="hljs-punctuation">:</span> <span class="hljs-
56018 <span class="hljs-punctuation">}</span><span class="hljs-punctuation">,</span>
56019 <span class="hljs-punctuation">{</span>
56020 …<span class="hljs-attr">"type"</span><span class="hljs-punctuation">:</span> <span class="hljs-str…
56021 …pan class="hljs-attr">"is_active"</span><span class="hljs-punctuation">:</span> <span class="hljs-
56022 …n class="hljs-attr">"is_referenced"</span><span class="hljs-punctuation">:</span> <span class="hlj…
56023 …ass="hljs-attr">"address"</span><span class="hljs-punctuation">:</span> <span class="hljs-string">…
56024 …<span class="hljs-attr">"repeat"</span><span class="hljs-punctuation">:</span> <span class="hljs-n…
56025 …ss="hljs-attr">"firesInMsFromNow"</span><span class="hljs-punctuation">:</span> <span class="hljs-
56026 …<span class="hljs-attr">"expired"</span><span class="hljs-punctuation">:</span> <span class="hljs-
56027 <span class="hljs-punctuation">}</span><span class="hljs-punctuation">,</span>
56028 <span class="hljs-punctuation">{</span>
56029 …<span class="hljs-attr">"type"</span><span class="hljs-punctuation">:</span> <span class="hljs-str…
56030 …span class="hljs-attr">"is_active"</span><span class="hljs-punctuation">:</span> <span class="hljs
56031 …n class="hljs-attr">"is_referenced"</span><span class="hljs-punctuation">:</span> <span class="hlj…
56032 …<span class="hljs-attr">"address"</span><span class="hljs-punctuation">:</span> <span class="hljs-
56033 <span class="hljs-punctuation">}</span><span class="hljs-punctuation">,</span>
56034 <span class="hljs-punctuation">{</span>
56035 …<span class="hljs-attr">"type"</span><span class="hljs-punctuation">:</span> <span class="hljs-str…
56036 …pan class="hljs-attr">"is_active"</span><span class="hljs-punctuation">:</span> <span class="hljs-
56037 …an class="hljs-attr">"is_referenced"</span><span class="hljs-punctuation">:</span> <span class="hl…
56038 …<span class="hljs-attr">"address"</span><span class="hljs-punctuation">:</span> <span class="hljs-
56039 <span class="hljs-punctuation">}</span><span class="hljs-punctuation">,</span>
56040 <span class="hljs-punctuation">{</span>
56041 …span class="hljs-attr">"type"</span><span class="hljs-punctuation">:</span> <span class="hljs-stri…
56042 …pan class="hljs-attr">"is_active"</span><span class="hljs-punctuation">:</span> <span class="hljs-
56043 …n class="hljs-attr">"is_referenced"</span><span class="hljs-punctuation">:</span> <span class="hlj…
56044 …<span class="hljs-attr">"address"</span><span class="hljs-punctuation">:</span> <span class="hljs-
56045 <span class="hljs-punctuation">}</span><span class="hljs-punctuation">,</span>
56046 <span class="hljs-punctuation">{</span>
56047 …<span class="hljs-attr">"type"</span><span class="hljs-punctuation">:</span> <span class="hljs-str…
56048 …pan class="hljs-attr">"is_active"</span><span class="hljs-punctuation">:</span> <span class="hljs-
56049 …n class="hljs-attr">"is_referenced"</span><span class="hljs-punctuation">:</span> <span class="hlj…
56050 …<span class="hljs-attr">"address"</span><span class="hljs-punctuation">:</span> <span class="hljs-
56051 <span class="hljs-punctuation">}</span><span class="hljs-punctuation">,</span>
56052 <span class="hljs-punctuation">{</span>
56053 …<span class="hljs-attr">"type"</span><span class="hljs-punctuation">:</span> <span class="hljs-str…
56054 …span class="hljs-attr">"is_active"</span><span class="hljs-punctuation">:</span> <span class="hljs
56055 …n class="hljs-attr">"is_referenced"</span><span class="hljs-punctuation">:</span> <span class="hlj…
56056 …<span class="hljs-attr">"address"</span><span class="hljs-punctuation">:</span> <span class="hljs-
56057 <span class="hljs-punctuation">}</span><span class="hljs-punctuation">,</span>
56058 <span class="hljs-punctuation">{</span>
56059 …<span class="hljs-attr">"type"</span><span class="hljs-punctuation">:</span> <span class="hljs-str…
56060 …pan class="hljs-attr">"is_active"</span><span class="hljs-punctuation">:</span> <span class="hljs-
56061 …an class="hljs-attr">"is_referenced"</span><span class="hljs-punctuation">:</span> <span class="hl…
56062 …ass="hljs-attr">"address"</span><span class="hljs-punctuation">:</span> <span class="hljs-string">…
56063 …<span class="hljs-attr">"width"</span><span class="hljs-punctuation">:</span> <span class="hljs-nu…
56064 …<span class="hljs-attr">"height"</span><span class="hljs-punctuation">:</span> <span class="hljs-n…
56065 …<span class="hljs-attr">"fd"</span><span class="hljs-punctuation">:</span> <span class="hljs-numbe…
56066 …pan class="hljs-attr">"writeQueueSize"</span><span class="hljs-punctuation">:</span> <span class="…
56067 …span class="hljs-attr">"readable"</span><span class="hljs-punctuation">:</span> <span class="hljs-
56068 …<span class="hljs-attr">"writable"</span><span class="hljs-punctuation">:</span> <span class="hljs
56069 <span class="hljs-punctuation">}</span><span class="hljs-punctuation">,</span>
56070 <span class="hljs-punctuation">{</span>
56071 …<span class="hljs-attr">"type"</span><span class="hljs-punctuation">:</span> <span class="hljs-str…
56072 …span class="hljs-attr">"is_active"</span><span class="hljs-punctuation">:</span> <span class="hljs
56073 …n class="hljs-attr">"is_referenced"</span><span class="hljs-punctuation">:</span> <span class="hlj…
56074 …ass="hljs-attr">"address"</span><span class="hljs-punctuation">:</span> <span class="hljs-string">…
56075 …<span class="hljs-attr">"signum"</span><span class="hljs-punctuation">:</span> <span class="hljs-n…
56076 …<span class="hljs-attr">"signal"</span><span class="hljs-punctuation">:</span> <span class="hljs-s…
56077 <span class="hljs-punctuation">}</span><span class="hljs-punctuation">,</span>
56078 <span class="hljs-punctuation">{</span>
56079 …<span class="hljs-attr">"type"</span><span class="hljs-punctuation">:</span> <span class="hljs-str…
56080 …span class="hljs-attr">"is_active"</span><span class="hljs-punctuation">:</span> <span class="hljs
56081 …an class="hljs-attr">"is_referenced"</span><span class="hljs-punctuation">:</span> <span class="hl…
56082 …ass="hljs-attr">"address"</span><span class="hljs-punctuation">:</span> <span class="hljs-string">…
56083 …<span class="hljs-attr">"width"</span><span class="hljs-punctuation">:</span> <span class="hljs-nu…
56084 …<span class="hljs-attr">"height"</span><span class="hljs-punctuation">:</span> <span class="hljs-n…
56085 …<span class="hljs-attr">"fd"</span><span class="hljs-punctuation">:</span> <span class="hljs-numbe…
56086 …pan class="hljs-attr">"writeQueueSize"</span><span class="hljs-punctuation">:</span> <span class="…
56087 …span class="hljs-attr">"readable"</span><span class="hljs-punctuation">:</span> <span class="hljs-
56088 …<span class="hljs-attr">"writable"</span><span class="hljs-punctuation">:</span> <span class="hljs
56089 <span class="hljs-punctuation">}</span><span class="hljs-punctuation">,</span>
56090 <span class="hljs-punctuation">{</span>
56091 …<span class="hljs-attr">"type"</span><span class="hljs-punctuation">:</span> <span class="hljs-str…
56092 …span class="hljs-attr">"is_active"</span><span class="hljs-punctuation">:</span> <span class="hljs
56093 …ass="hljs-attr">"address"</span><span class="hljs-punctuation">:</span> <span class="hljs-string">…
56094 …<span class="hljs-attr">"loopIdleTimeSeconds"</span><span class="hljs-punctuation">:</span> <span …
56095 <span class="hljs-punctuation">}</span>
56096 <span class="hljs-punctuation">]</span><span class="hljs-punctuation">,</span>
56097-attr">"workers"</span><span class="hljs-punctuation">:</span> <span class="hljs-punctuation">[</s…
56098 …<span class="hljs-attr">"environmentVariables"</span><span class="hljs-punctuation">:</span> <span…
56099 …n class="hljs-attr">"REMOTEHOST"</span><span class="hljs-punctuation">:</span> <span class="hljs-s…
56100-attr">"MANPATH"</span><span class="hljs-punctuation">:</span> <span class="hljs-string">"/opt/rh/…
56101 … class="hljs-attr">"XDG_SESSION_ID"</span><span class="hljs-punctuation">:</span> <span class="hlj…
56102 … class="hljs-attr">"HOSTNAME"</span><span class="hljs-punctuation">:</span> <span class="hljs-stri…
56103 …an class="hljs-attr">"HOST"</span><span class="hljs-punctuation">:</span> <span class="hljs-string…
56104 …n class="hljs-attr">"TERM"</span><span class="hljs-punctuation">:</span> <span class="hljs-string"…
56105 …pan class="hljs-attr">"SHELL"</span><span class="hljs-punctuation">:</span> <span class="hljs-stri…
56106 …n class="hljs-attr">"SSH_CLIENT"</span><span class="hljs-punctuation">:</span> <span class="hljs-s…
56107-attr">"PERL5LIB"</span><span class="hljs-punctuation">:</span> <span class="hljs-string">"/opt/rh…
56108 …ljs-attr">"OLDPWD"</span><span class="hljs-punctuation">:</span> <span class="hljs-string">"/home/…
56109-attr">"JAVACONFDIRS"</span><span class="hljs-punctuation">:</span> <span class="hljs-string">"/op…
56110 …n class="hljs-attr">"SSH_TTY"</span><span class="hljs-punctuation">:</span> <span class="hljs-stri…
56111 …="hljs-attr">"PCP_DIR"</span><span class="hljs-punctuation">:</span> <span class="hljs-string">"/o…
56112 …an class="hljs-attr">"GROUP"</span><span class="hljs-punctuation">:</span> <span class="hljs-strin…
56113 …span class="hljs-attr">"USER"</span><span class="hljs-punctuation">:</span> <span class="hljs-stri…
56114-attr">"LD_LIBRARY_PATH"</span><span class="hljs-punctuation">:</span> <span class="hljs-string">"…
56115 … class="hljs-attr">"HOSTTYPE"</span><span class="hljs-punctuation">:</span> <span class="hljs-stri…
56116-attr">"XDG_CONFIG_DIRS"</span><span class="hljs-punctuation">:</span> <span class="hljs-string">"…
56117 …ss="hljs-attr">"MAIL"</span><span class="hljs-punctuation">:</span> <span class="hljs-string">"/va…
56118-attr">"PATH"</span><span class="hljs-punctuation">:</span> <span class="hljs-string">"/home/nodeu…
56119 …s="hljs-attr">"PWD"</span><span class="hljs-punctuation">:</span> <span class="hljs-string">"/home…
56120 …an class="hljs-attr">"LANG"</span><span class="hljs-punctuation">:</span> <span class="hljs-string…
56121-attr">"PS1"</span><span class="hljs-punctuation">:</span> <span class="hljs-string">"\\u@\\h : \\…
56122 …<span class="hljs-attr">"SHLVL"</span><span class="hljs-punctuation">:</span> <span class="hljs-st…
56123 …n class="hljs-attr">"HOME"</span><span class="hljs-punctuation">:</span> <span class="hljs-string"…
56124 …span class="hljs-attr">"OSTYPE"</span><span class="hljs-punctuation">:</span> <span class="hljs-st…
56125 …pan class="hljs-attr">"VENDOR"</span><span class="hljs-punctuation">:</span> <span class="hljs-str…
56126-attr">"PYTHONPATH"</span><span class="hljs-punctuation">:</span> <span class="hljs-string">"/opt/…
56127 …pan class="hljs-attr">"MACHTYPE"</span><span class="hljs-punctuation">:</span> <span class="hljs-s…
56128 …an class="hljs-attr">"LOGNAME"</span><span class="hljs-punctuation">:</span> <span class="hljs-str…
56129-attr">"XDG_DATA_DIRS"</span><span class="hljs-punctuation">:</span> <span class="hljs-string">"/o…
56130 …"hljs-attr">"LESSOPEN"</span><span class="hljs-punctuation">:</span> <span class="hljs-string">"||…
56131-attr">"INFOPATH"</span><span class="hljs-punctuation">:</span> <span class="hljs-string">"/opt/rh…
56132 …s="hljs-attr">"XDG_RUNTIME_DIR"</span><span class="hljs-punctuation">:</span> <span class="hljs-st…
56133 …<span class="hljs-attr">"_"</span><span class="hljs-punctuation">:</span> <span class="hljs-string…
56134 <span class="hljs-punctuation">}</span><span class="hljs-punctuation">,</span>
56135 …<span class="hljs-attr">"userLimits"</span><span class="hljs-punctuation">:</span> <span class="hl…
56136 …<span class="hljs-attr">"core_file_size_blocks"</span><span class="hljs-punctuation">:</span> <spa…
56137 …<span class="hljs-attr">"soft"</span><span class="hljs-punctuation">:</span> <span class="hljs-str…
56138 …<span class="hljs-attr">"hard"</span><span class="hljs-punctuation">:</span> <span class="hljs-str…
56139 <span class="hljs-punctuation">}</span><span class="hljs-punctuation">,</span>
56140 …<span class="hljs-attr">"data_seg_size_kbytes"</span><span class="hljs-punctuation">:</span> <span…
56141 …pan class="hljs-attr">"soft"</span><span class="hljs-punctuation">:</span> <span class="hljs-strin…
56142 …<span class="hljs-attr">"hard"</span><span class="hljs-punctuation">:</span> <span class="hljs-str…
56143 <span class="hljs-punctuation">}</span><span class="hljs-punctuation">,</span>
56144 …<span class="hljs-attr">"file_size_blocks"</span><span class="hljs-punctuation">:</span> <span cla…
56145 …pan class="hljs-attr">"soft"</span><span class="hljs-punctuation">:</span> <span class="hljs-strin…
56146 …<span class="hljs-attr">"hard"</span><span class="hljs-punctuation">:</span> <span class="hljs-str…
56147 <span class="hljs-punctuation">}</span><span class="hljs-punctuation">,</span>
56148 …<span class="hljs-attr">"max_locked_memory_bytes"</span><span class="hljs-punctuation">:</span> <s…
56149 …pan class="hljs-attr">"soft"</span><span class="hljs-punctuation">:</span> <span class="hljs-strin…
56150 …<span class="hljs-attr">"hard"</span><span class="hljs-punctuation">:</span> <span class="hljs-num…
56151 <span class="hljs-punctuation">}</span><span class="hljs-punctuation">,</span>
56152 …<span class="hljs-attr">"max_memory_size_kbytes"</span><span class="hljs-punctuation">:</span> <sp…
56153 …pan class="hljs-attr">"soft"</span><span class="hljs-punctuation">:</span> <span class="hljs-strin…
56154 …<span class="hljs-attr">"hard"</span><span class="hljs-punctuation">:</span> <span class="hljs-str…
56155 <span class="hljs-punctuation">}</span><span class="hljs-punctuation">,</span>
56156 …<span class="hljs-attr">"open_files"</span><span class="hljs-punctuation">:</span> <span class="hl…
56157 …pan class="hljs-attr">"soft"</span><span class="hljs-punctuation">:</span> <span class="hljs-strin…
56158 …<span class="hljs-attr">"hard"</span><span class="hljs-punctuation">:</span> <span class="hljs-num…
56159 <span class="hljs-punctuation">}</span><span class="hljs-punctuation">,</span>
56160 …<span class="hljs-attr">"stack_size_bytes"</span><span class="hljs-punctuation">:</span> <span cla…
56161 …pan class="hljs-attr">"soft"</span><span class="hljs-punctuation">:</span> <span class="hljs-strin…
56162 …<span class="hljs-attr">"hard"</span><span class="hljs-punctuation">:</span> <span class="hljs-str…
56163 <span class="hljs-punctuation">}</span><span class="hljs-punctuation">,</span>
56164 …<span class="hljs-attr">"cpu_time_seconds"</span><span class="hljs-punctuation">:</span> <span cla…
56165 …pan class="hljs-attr">"soft"</span><span class="hljs-punctuation">:</span> <span class="hljs-strin…
56166 …<span class="hljs-attr">"hard"</span><span class="hljs-punctuation">:</span> <span class="hljs-str…
56167 <span class="hljs-punctuation">}</span><span class="hljs-punctuation">,</span>
56168 …<span class="hljs-attr">"max_user_processes"</span><span class="hljs-punctuation">:</span> <span c…
56169 …pan class="hljs-attr">"soft"</span><span class="hljs-punctuation">:</span> <span class="hljs-strin…
56170 …<span class="hljs-attr">"hard"</span><span class="hljs-punctuation">:</span> <span class="hljs-num…
56171 <span class="hljs-punctuation">}</span><span class="hljs-punctuation">,</span>
56172 …<span class="hljs-attr">"virtual_memory_kbytes"</span><span class="hljs-punctuation">:</span> <spa…
56173 …pan class="hljs-attr">"soft"</span><span class="hljs-punctuation">:</span> <span class="hljs-strin…
56174 …<span class="hljs-attr">"hard"</span><span class="hljs-punctuation">:</span> <span class="hljs-str…
56175 <span class="hljs-punctuation">}</span>
56176 <span class="hljs-punctuation">}</span><span class="hljs-punctuation">,</span>
56177 …<span class="hljs-attr">"sharedObjects"</span><span class="hljs-punctuation">:</span> <span class=…
56178 <span class="hljs-string">"/lib64/libdl.so.2"</span><span class="hljs-punctuation">,</span>
56179 <span class="hljs-string">"/lib64/librt.so.1"</span><span class="hljs-punctuation">,</span>
56180 <span class="hljs-string">"/lib64/libstdc++.so.6"</span><span class="hljs-punctuation">,</span>
56181 <span class="hljs-string">"/lib64/libm.so.6"</span><span class="hljs-punctuation">,</span>
56182 <span class="hljs-string">"/lib64/libgcc_s.so.1"</span><span class="hljs-punctuation">,</span>
56183 <span class="hljs-string">"/lib64/libpthread.so.0"</span><span class="hljs-punctuation">,</span>
56184 <span class="hljs-string">"/lib64/libc.so.6"</span><span class="hljs-punctuation">,</span>
56185 <span class="hljs-string">"/lib64/ld-linux-x86-64.so.2"</span>
56186 <span class="hljs-punctuation">]</span>
56187 <span class="hljs-punctuation">}</span></code></pre>
56189 <pre><code class="language-bash">node --report-uncaught-exception --report-on-signal \
56190 --report-on-fatalerror app.js</code></pre>
56193 <p><code>--report-uncaught-exception</code> Enables report to be generated on
56194 un-caught exceptions. Useful when inspecting JavaScript stack in conjunction
56198 <p><code>--report-on-signal</code> Enables report to be generated upon receiving
56213 <p><code>--report-on-fatalerror</code> Enables the report to be triggered on fatal errors
56220 <p><code>--report-compact</code> Write reports in a compact format, single-line JSON, more
56221 easily consumable by log processing systems than the default multi-line format
56225 <p><code>--report-directory</code> Location at which the report will be
56229 <p><code>--report-filename</code> Name of the file to which the report will be
56233 <p><code>--report-signal</code> Sets or resets the signal for report generation
56238 <pre><code class="language-js">process.<span class="hljs-property">report</span>.<span class="hljs-
56241 …ass="language-js">process.<span class="hljs-property">report</span>.<span class="hljs-title functi…
56247 <pre><code class="language-js"><span class="hljs-keyword">try</span> {
56248 …process.<span class="hljs-title function_">chdir</span>(<span class="hljs-string">'/non-existent-p…
56249 } <span class="hljs-keyword">catch</span> (err) {
56250 …process.<span class="hljs-property">report</span>.<span class="hljs-title function_">writeReport</…
56252 <span class="hljs-comment">// Any other code</span></code></pre>
56255 <pre><code class="language-js"><span class="hljs-keyword">try</span> {
56256 …process.<span class="hljs-title function_">chdir</span>(<span class="hljs-string">'/non-existent-p…
56257 } <span class="hljs-keyword">catch</span> (err) {
56258 …process.<span class="hljs-property">report</span>.<span class="hljs-title function_">writeReport</…
56260 <span class="hljs-comment">// Any other code</span></code></pre>
56263 …class="language-js"><span class="hljs-keyword">const</span> report = process.<span class="hljs-pro…
56264-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hlj…
56266 <span class="hljs-comment">// Similar to process.report.writeReport() output</span>
56267-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hlj…
56271-js"><span class="hljs-keyword">const</span> report = process.<span class="hljs-property">report</…
56272-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hlj…
56274 the application, in expectation of self-adjusting the resource consumption,
56282 <pre><code class="language-console"><span class="hljs-meta">$ </span><span class="language-bash">no…
56283 <span class="hljs-meta">> </span><span class="language-bash">process.report.writeReport();</span>
56286 <span class="hljs-meta">></span></code></pre>
56313 <pre><code class="language-js"><span class="hljs-comment">// Trigger report only on uncaught except…
56314 process.<span class="hljs-property">report</span>.<span class="hljs-property">reportOnFatalError</s…
56315 process.<span class="hljs-property">report</span>.<span class="hljs-property">reportOnSignal</span>…
56316 …ocess.<span class="hljs-property">report</span>.<span class="hljs-property">reportOnUncaughtExcept…
56318 <span class="hljs-comment">// Trigger report for both internal errors as well as external signal.</…
56319 process.<span class="hljs-property">report</span>.<span class="hljs-property">reportOnFatalError</s…
56320 process.<span class="hljs-property">report</span>.<span class="hljs-property">reportOnSignal</span>…
56321 …ocess.<span class="hljs-property">report</span>.<span class="hljs-property">reportOnUncaughtExcept…
56323 <span class="hljs-comment">// Change the default signal to 'SIGQUIT' and enable it.</span>
56324 process.<span class="hljs-property">report</span>.<span class="hljs-property">reportOnFatalError</s…
56325 …ocess.<span class="hljs-property">report</span>.<span class="hljs-property">reportOnUncaughtExcept…
56326 process.<span class="hljs-property">report</span>.<span class="hljs-property">reportOnSignal</span>…
56327 process.<span class="hljs-property">report</span>.<span class="hljs-property">signal</span> = <span…
56330 <pre><code class="language-bash">NODE_OPTIONS=<span class="hljs-string">"--report-uncaught-exceptio…
56331 --report-on-fatalerror --report-on-signal \
56332 --report-signal=SIGUSR2 --report-filename=./report.json \
56333 --report-directory=/home/nodeuser"</span></code></pre>
56357 …ty api_stability_2"><a href="#documentation_stability_index">Stability: 2</a> - Stable</div><p></p>
56367 …ode class="language-js"><span class="hljs-keyword">const</span> stream = <span class="hljs-built_i…
56446 …code class="language-js"><span class="hljs-keyword">const</span> http = <span class="hljs-built_in…
56448 …s="hljs-keyword">const</span> server = http.<span class="hljs-title function_">createServer</span>…
56449 <span class="hljs-comment">// `req` is an http.IncomingMessage, which is a readable stream.</span>
56450 <span class="hljs-comment">// `res` is an http.ServerResponse, which is a writable stream.</span>
56452 <span class="hljs-keyword">let</span> body = <span class="hljs-string">''</span>;
56453 <span class="hljs-comment">// Get the data as utf8 strings.</span>
56454 <span class="hljs-comment">// If an encoding is not set, Buffer objects will be received.</span>
56455 …req.<span class="hljs-title function_">setEncoding</span>(<span class="hljs-string">'utf8'</span>);
56457 <span class="hljs-comment">// Readable streams emit 'data' events once a listener is added.</span>
56458 ….<span class="hljs-title function_">on</span>(<span class="hljs-string">'data'</span>, <span class…
56462 …<span class="hljs-comment">// The 'end' event indicates that the entire body has been received.</s…
56463 …req.<span class="hljs-title function_">on</span>(<span class="hljs-string">'end'</span>, <span cla…
56464 <span class="hljs-keyword">try</span> {
56465 …<span class="hljs-keyword">const</span> data = <span class="hljs-variable constant_">JSON</span>.<…
56466 <span class="hljs-comment">// Write back something interesting to the user:</span>
56467 …res.<span class="hljs-title function_">write</span>(<span class="hljs-keyword">typeof</span> data);
56468 res.<span class="hljs-title function_">end</span>();
56469 } <span class="hljs-keyword">catch</span> (er) {
56470 <span class="hljs-comment">// uh oh! bad json!</span>
56471 res.<span class="hljs-property">statusCode</span> = <span class="hljs-number">400</span>;
56472 …n class="hljs-keyword">return</span> res.<span class="hljs-title function_">end</span>(<span class…
56477 server.<span class="hljs-title function_">listen</span>(<span class="hljs-number">1337</span>);
56479 <span class="hljs-comment">// $ curl localhost:1337 -d "{}"</span>
56480 <span class="hljs-comment">// object</span>
56481 <span class="hljs-comment">// $ curl localhost:1337 -d "\"foo\""</span>
56482 <span class="hljs-comment">// string</span>
56483 <span class="hljs-comment">// $ curl localhost:1337 -d "not json"</span>
56484 <span class="hljs-comment">// error: Unexpected token o in JSON at position 1</span></code></pre>
56520 <pre><code class="language-js"><span class="hljs-keyword">const</span> myStream = <span class="hljs
56521 myStream.<span class="hljs-title function_">write</span>(<span class="hljs-string">'some data'</spa…
56522 myStream.<span class="hljs-title function_">write</span>(<span class="hljs-string">'some more data'…
56523 myStream.<span class="hljs-title function_">end</span>(<span class="hljs-string">'done writing data…
56553 <pre><code class="language-js"><span class="hljs-comment">// Write the data to the supplied writabl…
56554 <span class="hljs-comment">// Be attentive to back-pressure.</span>
56555 <span class="hljs-keyword">function</span> <span class="hljs-title function_">writeOneMillionTimes<…
56556 <span class="hljs-keyword">let</span> i = <span class="hljs-number">1000000</span>;
56557 <span class="hljs-title function_">write</span>();
56558 …<span class="hljs-keyword">function</span> <span class="hljs-title function_">write</span>(<span c…
56559 <span class="hljs-keyword">let</span> ok = <span class="hljs-literal">true</span>;
56560 <span class="hljs-keyword">do</span> {
56561 i--;
56562 <span class="hljs-keyword">if</span> (i === <span class="hljs-number">0</span>) {
56563 <span class="hljs-comment">// Last time!</span>
56564 writer.<span class="hljs-title function_">write</span>(data, encoding, callback);
56565 } <span class="hljs-keyword">else</span> {
56566 <span class="hljs-comment">// See if we should continue, or wait.</span>
56567 <span class="hljs-comment">// Don't pass the callback, because we're not done yet.</span>
56568 ok = writer.<span class="hljs-title function_">write</span>(data, encoding);
56570 …} <span class="hljs-keyword">while</span> (i > <span class="hljs-number">0</span> &#x26;&#x26; ok);
56571 <span class="hljs-keyword">if</span> (i > <span class="hljs-number">0</span>) {
56572 <span class="hljs-comment">// Had to stop early!</span>
56573 <span class="hljs-comment">// Write some more once it drains.</span>
56574 …writer.<span class="hljs-title function_">once</span>(<span class="hljs-string">'drain'</span>, wr…
56583 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error…
56598 <pre><code class="language-js"><span class="hljs-keyword">const</span> writer = <span class="hljs-t…
56599 …n class="hljs-keyword">for</span> (<span class="hljs-keyword">let</span> i = <span class="hljs-num…
56600 …writer.<span class="hljs-title function_">write</span>(<span class="hljs-string">`hello, #<span cl…
56602 writer.<span class="hljs-title function_">on</span>(<span class="hljs-string">'finish'</span>, <spa…
56603 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>…
56605 writer.<span class="hljs-title function_">end</span>(<span class="hljs-string">'This is the end\n'<…
56615 <pre><code class="language-js"><span class="hljs-keyword">const</span> writer = <span class="hljs-t…
56616 <span class="hljs-keyword">const</span> reader = <span class="hljs-title function_">getReadableStre…
56617 ….<span class="hljs-title function_">on</span>(<span class="hljs-string">'pipe'</span>, <span class…
56618 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>…
56619 assert.<span class="hljs-title function_">equal</span>(src, reader);
56621 reader.<span class="hljs-title function_">pipe</span>(writer);</code></pre>
56635 <pre><code class="language-js"><span class="hljs-keyword">const</span> writer = <span class="hljs-t…
56636 <span class="hljs-keyword">const</span> reader = <span class="hljs-title function_">getReadableStre…
56637 …<span class="hljs-title function_">on</span>(<span class="hljs-string">'unpipe'</span>, <span clas…
56638 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>…
56639 assert.<span class="hljs-title function_">equal</span>(src, reader);
56641 reader.<span class="hljs-title function_">pipe</span>(writer);
56642 reader.<span class="hljs-title function_">unpipe</span>(writer);</code></pre>
56654 all to <code>writable._writev()</code>, if present. This prevents a head-of-line blocking
56665 <td><p>Work as a no-op on a stream that has already been destroyed.</p></td></tr>
56672 <li><code>error</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/G…
56673 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/t…
56684-flavor-selector" type="checkbox" checked aria-label="Show modern ES modules syntax"><pre><code cl…
56686 <span class="hljs-keyword">const</span> myStream = <span class="hljs-keyword">new</span> <span clas…
56688 …lass="hljs-keyword">const</span> fooErr = <span class="hljs-keyword">new</span> <span class="hljs-
56689 myStream.<span class="hljs-title function_">destroy</span>(fooErr);
56690-title function_">on</span>(<span class="hljs-string">'error'</span>, <span class="hljs-function">…
56692 <span class="hljs-keyword">const</span> myStream = <span class="hljs-keyword">new</span> <span clas…
56694 myStream.<span class="hljs-title function_">destroy</span>();
56695-title function_">on</span>(<span class="hljs-string">'error'</span>, <span class="hljs-keyword">f…
56696-js cjs"><span class="hljs-keyword">const</span> { <span class="hljs-title class_">Writable</span>…
56698 <span class="hljs-keyword">const</span> myStream = <span class="hljs-keyword">new</span> <span clas…
56699 myStream.<span class="hljs-title function_">destroy</span>();
56701-title function_">write</span>(<span class="hljs-string">'foo'</span>, <span class="hljs-function"…
56702 <span class="hljs-comment">// ERR_STREAM_DESTROYED</span></code></pre>
56703 <p>Once <code>destroy()</code> has been called any further calls will be a no-op and no
56712 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" …
56715-js cjs"><span class="hljs-keyword">const</span> { <span class="hljs-title class_">Writable</span>…
56717 <span class="hljs-keyword">const</span> myStream = <span class="hljs-keyword">new</span> <span clas…
56719-variable language_">console</span>.<span class="hljs-title function_">log</span>(myStream.<span c…
56720 myStream.<span class="hljs-title function_">destroy</span>();
56721-variable language_">console</span>.<span class="hljs-title function_">log</span>(myStream.<span c…
56739-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | <a href="#bu…
56743 <li><code>encoding</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_str…
56744 <li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Referenc…
56746 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/t…
56755 <pre><code class="language-js"><span class="hljs-comment">// Write 'hello, ' and then end with 'wor…
56756 <span class="hljs-keyword">const</span> fs = <span class="hljs-built_in">require</span>(<span class…
56757 <span class="hljs-keyword">const</span> file = fs.<span class="hljs-title function_">createWriteStr…
56758 file.<span class="hljs-title function_">write</span>(<span class="hljs-string">'hello, '</span>);
56759 file.<span class="hljs-title function_">end</span>(<span class="hljs-string">'world!'</span>);
56760 <span class="hljs-comment">// Writing more now is not allowed!</span></code></pre>
56774 <li><code>encoding</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_str…
56775 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/t…
56789 <pre><code class="language-js">stream.<span class="hljs-title function_">cork</span>();
56790 stream.<span class="hljs-title function_">write</span>(<span class="hljs-string">'some '</span>);
56791 stream.<span class="hljs-title function_">write</span>(<span class="hljs-string">'data '</span>);
56792 process.<span class="hljs-title function_">nextTick</span>(<span class="hljs-function">() =></span>…
56796 <pre><code class="language-js">stream.<span class="hljs-title function_">cork</span>();
56797 stream.<span class="hljs-title function_">write</span>(<span class="hljs-string">'some '</span>);
56798 stream.<span class="hljs-title function_">cork</span>();
56799 stream.<span class="hljs-title function_">write</span>(<span class="hljs-string">'data '</span>);
56800 process.<span class="hljs-title function_">nextTick</span>(<span class="hljs-function">() =></span>…
56801 stream.<span class="hljs-title function_">uncork</span>();
56802 …<span class="hljs-comment">// The data will not be flushed until uncork() is called a second time.…
56803 stream.<span class="hljs-title function_">uncork</span>();
56811 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" …
56820 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" …
56830 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" c…
56839 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" …
56847 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" c…
56856 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" c…
56866 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" …
56874 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" …
56892-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | <a href="#bu…
56896 …zilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | …
56897 <li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Referenc…
56898 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boole…
56933 …ode class="language-js"><span class="hljs-keyword">function</span> <span class="hljs-title functio…
56934 …<span class="hljs-keyword">if</span> (!stream.<span class="hljs-title function_">write</span>(data…
56935 …stream.<span class="hljs-title function_">once</span>(<span class="hljs-string">'drain'</span>, cb…
56936 } <span class="hljs-keyword">else</span> {
56937 process.<span class="hljs-title function_">nextTick</span>(cb);
56941 <span class="hljs-comment">// Wait for cb to be called before doing any other write.</span>
56942 <span class="hljs-title function_">write</span>(<span class="hljs-string">'hello'</span>, <span cla…
56943 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>…
57033-js"><span class="hljs-keyword">const</span> { <span class="hljs-title class_">PassThrough</span>,…
57034 <span class="hljs-keyword">const</span> pass = <span class="hljs-keyword">new</span> <span class="h…
57035 <span class="hljs-keyword">const</span> writable = <span class="hljs-keyword">new</span> <span clas…
57037 pass.<span class="hljs-title function_">pipe</span>(writable);
57038 pass.<span class="hljs-title function_">unpipe</span>(writable);
57039 <span class="hljs-comment">// readableFlowing is now false.</span>
57041-title function_">on</span>(<span class="hljs-string">'data'</span>, <span class="hljs-function">(…
57042 pass.<span class="hljs-title function_">write</span>(<span class="hljs-string">'ok'</span>); <span…
57043 pass.<span class="hljs-title function_">resume</span>(); <span class="hljs-comment">// Must be …
57055 require more fine-grained control over the transfer and generation of data can
57085 …zilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | …
57103 <pre><code class="language-js"><span class="hljs-keyword">const</span> readable = <span class="hljs
57104 ….<span class="hljs-title function_">on</span>(<span class="hljs-string">'data'</span>, <span class…
57105 …ss="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span cl…
57117 <pre><code class="language-js"><span class="hljs-keyword">const</span> readable = <span class="hljs
57118 ….<span class="hljs-title function_">on</span>(<span class="hljs-string">'data'</span>, <span class…
57119 …ss="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span cl…
57121 readable.<span class="hljs-title function_">on</span>(<span class="hljs-string">'end'</span>, <span…
57122 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>…
57129 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error…
57159 <pre><code class="language-js"><span class="hljs-keyword">const</span> readable = <span class="hljs
57160 …ass="hljs-title function_">on</span>(<span class="hljs-string">'readable'</span>, <span class="hlj…
57161 <span class="hljs-comment">// There is some data to read now.</span>
57162 <span class="hljs-keyword">let</span> data;
57164 …<span class="hljs-keyword">while</span> (data = <span class="hljs-variable language_">this</span>.…
57165 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>…
57175 …<code class="language-js"><span class="hljs-keyword">const</span> fs = <span class="hljs-built_in"…
57176 <span class="hljs-keyword">const</span> rr = fs.<span class="hljs-title function_">createReadStream…
57177 rr.<span class="hljs-title function_">on</span>(<span class="hljs-string">'readable'</span>, <span …
57178 …ss="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span cl…
57180 rr.<span class="hljs-title function_">on</span>(<span class="hljs-string">'end'</span>, <span class…
57181 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>…
57184 <pre><code class="language-console"><span class="hljs-meta">$ </span><span class="language-bash">no…
57209 <td><p>Work as a no-op on a stream that has already been destroyed.</p></td></tr>
57216 <li><code>error</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/G…
57217 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/t…
57223 <p>Once <code>destroy()</code> has been called any further calls will be a no-op and no
57232 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" …
57240 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boole…
57246 …e class="language-js"><span class="hljs-keyword">const</span> readable = <span class="hljs-keyword…
57248 readable.<span class="hljs-title function_">isPaused</span>(); <span class="hljs-comment">// === fa…
57249 readable.<span class="hljs-title function_">pause</span>();
57250 readable.<span class="hljs-title function_">isPaused</span>(); <span class="hljs-comment">// === tr…
57251 readable.<span class="hljs-title function_">resume</span>();
57252 readable.<span class="hljs-title function_">isPaused</span>(); <span class="hljs-comment">// === fa…
57258 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/t…
57263 <pre><code class="language-js"><span class="hljs-keyword">const</span> readable = <span class="hljs
57264 ….<span class="hljs-title function_">on</span>(<span class="hljs-string">'data'</span>, <span class…
57265 …ss="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span cl…
57266 readable.<span class="hljs-title function_">pause</span>();
57267 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>…
57268 <span class="hljs-built_in">setTimeout</span>(<span class="hljs-function">() =></span> {
57269 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>…
57270 readable.<span class="hljs-title function_">resume</span>();
57271 }, <span class="hljs-number">1000</span>);
57281 <li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference…
57283 <li><code>end</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structur…
57296 …<code class="language-js"><span class="hljs-keyword">const</span> fs = <span class="hljs-built_in"…
57297 <span class="hljs-keyword">const</span> readable = <span class="hljs-title function_">getReadableSt…
57298 <span class="hljs-keyword">const</span> writable = fs.<span class="hljs-title function_">createWrit…
57299 <span class="hljs-comment">// All the data from readable goes into 'file.txt'.</span>
57300 readable.<span class="hljs-title function_">pipe</span>(writable);</code></pre>
57305 …<code class="language-js"><span class="hljs-keyword">const</span> fs = <span class="hljs-built_in"…
57306 <span class="hljs-keyword">const</span> r = fs.<span class="hljs-title function_">createReadStream<…
57307 <span class="hljs-keyword">const</span> z = zlib.<span class="hljs-title function_">createGzip</spa…
57308 <span class="hljs-keyword">const</span> w = fs.<span class="hljs-title function_">createWriteStream…
57309 r.<span class="hljs-title function_">pipe</span>(z).<span class="hljs-title function_">pipe</span>(…
57314 …class="language-js">reader.<span class="hljs-title function_">pipe</span>(writer, { <span class="h…
57315 reader.<span class="hljs-title function_">on</span>(<span class="hljs-string">'end'</span>, <span c…
57316 …writer.<span class="hljs-title function_">end</span>(<span class="hljs-string">'Goodbye\n'</span>);
57329 <li><code>size</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
57330-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | <a href="#bu…
57347 <pre><code class="language-js"><span class="hljs-keyword">const</span> readable = <span class="hljs
57349 <span class="hljs-comment">// 'readable' may be triggered multiple times as data is buffered in</sp…
57350 readable.<span class="hljs-title function_">on</span>(<span class="hljs-string">'readable'</span>, …
57351 <span class="hljs-keyword">let</span> chunk;
57352 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>…
57353 <span class="hljs-comment">// Use a loop to make sure we read all currently available data</span>
57354 …<span class="hljs-keyword">while</span> (<span class="hljs-literal">null</span> !== (chunk = reada…
57355 …lass="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span …
57359 <span class="hljs-comment">// 'end' will be triggered once when there is no more data available</sp…
57360 readable.<span class="hljs-title function_">on</span>(<span class="hljs-string">'end'</span>, <span…
57361 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>…
57372 <pre><code class="language-js"><span class="hljs-keyword">const</span> chunks = [];
57374 readable.<span class="hljs-title function_">on</span>(<span class="hljs-string">'readable'</span>, …
57375 <span class="hljs-keyword">let</span> chunk;
57376 …<span class="hljs-keyword">while</span> (<span class="hljs-literal">null</span> !== (chunk = reada…
57377 chunks.<span class="hljs-title function_">push</span>(chunk);
57381 readable.<span class="hljs-title function_">on</span>(<span class="hljs-string">'end'</span>, <span…
57382 …<span class="hljs-keyword">const</span> content = chunks.<span class="hljs-title function_">join</…
57396 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" …
57405 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" …
57415 …mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Null_type" class="type">&#x3C;null></a> | <a…
57424 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" …
57432 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" …
57441 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" c…
57450 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" c…
57460 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" …
57476 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/t…
57482 <pre><code class="language-js"><span class="hljs-title function_">getReadableStreamSomehow</span>()
57483 .<span class="hljs-title function_">resume</span>()
57484 ….<span class="hljs-title function_">on</span>(<span class="hljs-string">'end'</span>, <span class=…
57485 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>…
57494 <li><code>encoding</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_str…
57495 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/t…
57503 output data to be interpreted as UTF-8 data, and passed as strings. Calling
57506 <p>The <code>Readable</code> stream will properly handle multi-byte characters delivered
57509 <pre><code class="language-js"><span class="hljs-keyword">const</span> readable = <span class="hljs
57510 readable.<span class="hljs-title function_">setEncoding</span>(<span class="hljs-string">'utf8'</sp…
57511 ….<span class="hljs-title function_">on</span>(<span class="hljs-string">'data'</span>, <span class…
57512 …assert.<span class="hljs-title function_">equal</span>(<span class="hljs-keyword">typeof</span> ch…
57513-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hlj…
57521 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/t…
57528 …<code class="language-js"><span class="hljs-keyword">const</span> fs = <span class="hljs-built_in"…
57529 <span class="hljs-keyword">const</span> readable = <span class="hljs-title function_">getReadableSt…
57530 <span class="hljs-keyword">const</span> writable = fs.<span class="hljs-title function_">createWrit…
57531 <span class="hljs-comment">// All the data from readable goes into 'file.txt',</span>
57532 <span class="hljs-comment">// but only for the first second.</span>
57533 readable.<span class="hljs-title function_">pipe</span>(writable);
57534 <span class="hljs-built_in">setTimeout</span>(<span class="hljs-function">() =></span> {
57535 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>…
57536 readable.<span class="hljs-title function_">unpipe</span>(writable);
57537 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>…
57538 writable.<span class="hljs-title function_">end</span>();
57539 }, <span class="hljs-number">1000</span>);</code></pre>
57553-US/docs/Web/JavaScript/Reference/Global_Objects/Uint8Array" class="type">&#x3C;Uint8Array></a> | …
57557 <li><code>encoding</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_str…
57566 code that needs to "un-consume" some amount of data that it has optimistically
57573 <pre><code class="language-js"><span class="hljs-comment">// Pull off a header delimited by \n\n.</…
57574 <span class="hljs-comment">// Use unshift() if we get too much.</span>
57575 <span class="hljs-comment">// Call the callback with (error, header, stream).</span>
57576 …"hljs-keyword">const</span> { <span class="hljs-title class_">StringDecoder</span> } = <span class…
57577 <span class="hljs-keyword">function</span> <span class="hljs-title function_">parseHeader</span>(<s…
57578 …stream.<span class="hljs-title function_">on</span>(<span class="hljs-string">'error'</span>, call…
57579 …stream.<span class="hljs-title function_">on</span>(<span class="hljs-string">'readable'</span>, o…
57580 …"hljs-keyword">const</span> decoder = <span class="hljs-keyword">new</span> <span class="hljs-titl…
57581 <span class="hljs-keyword">let</span> header = <span class="hljs-string">''</span>;
57582 …<span class="hljs-keyword">function</span> <span class="hljs-title function_">onReadable</span>(<s…
57583 <span class="hljs-keyword">let</span> chunk;
57584 …<span class="hljs-keyword">while</span> (<span class="hljs-literal">null</span> !== (chunk = strea…
57585 …<span class="hljs-keyword">const</span> str = decoder.<span class="hljs-title function_">write</sp…
57586 …<span class="hljs-keyword">if</span> (str.<span class="hljs-title function_">match</span>(<span cl…
57587 <span class="hljs-comment">// Found the header boundary.</span>
57588 …<span class="hljs-keyword">const</span> split = str.<span class="hljs-title function_">split</span…
57589 header += split.<span class="hljs-title function_">shift</span>();
57590 …<span class="hljs-keyword">const</span> remaining = split.<span class="hljs-title function_">join<…
57591 …s-keyword">const</span> buf = <span class="hljs-title class_">Buffer</span>.<span class="hljs-titl…
57592 …stream.<span class="hljs-title function_">removeListener</span>(<span class="hljs-string">'error'<…
57593 <span class="hljs-comment">// Remove the 'readable' listener before unshifting.</span>
57594 …stream.<span class="hljs-title function_">removeListener</span>(<span class="hljs-string">'readabl…
57595 <span class="hljs-keyword">if</span> (buf.<span class="hljs-property">length</span>)
57596 stream.<span class="hljs-title function_">unshift</span>(buf);
57597 … <span class="hljs-comment">// Now the body of the message can be read from the stream.</span>
57598 …<span class="hljs-title function_">callback</span>(<span class="hljs-literal">null</span>, header,…
57599 } <span class="hljs-keyword">else</span> {
57600 <span class="hljs-comment">// Still reading the header.</span>
57620 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/t…
57631-js"><span class="hljs-keyword">const</span> { <span class="hljs-title class_">OldReader</span> } …
57632 …s="hljs-keyword">const</span> { <span class="hljs-title class_">Readable</span> } = <span class="h…
57633 <span class="hljs-keyword">const</span> oreader = <span class="hljs-keyword">new</span> <span class…
57634 …="hljs-keyword">const</span> myReader = <span class="hljs-keyword">new</span> <span class="hljs-ti…
57636 myReader.<span class="hljs-title function_">on</span>(<span class="hljs-string">'readable'</span>, …
57637 …myReader.<span class="hljs-title function_">read</span>(); <span class="hljs-comment">// etc.</spa…
57652 <li>Returns: <a href="https://tc39.github.io/ecma262/#sec-asynciterator-interface" class="type">&#x…
57654 …<code class="language-js"><span class="hljs-keyword">const</span> fs = <span class="hljs-built_in"…
57656 …lass="hljs-keyword">async</span> <span class="hljs-keyword">function</span> <span class="hljs-titl…
57657 …readable.<span class="hljs-title function_">setEncoding</span>(<span class="hljs-string">'utf8'</s…
57658 <span class="hljs-keyword">let</span> data = <span class="hljs-string">''</span>;
57659 …n class="hljs-keyword">for</span> <span class="hljs-keyword">await</span> (<span class="hljs-keywo…
57662 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>…
57665-title function_">print</span>(fs.<span class="hljs-title function_">createReadStream</span>(<span…
57713 <td><p>Work as a no-op on a stream that has already been destroyed.</p></td></tr>
57720 <li><code>error</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/G…
57721 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/t…
57729 <p>Once <code>destroy()</code> has been called, any further calls will be a no-op and no
57749 <li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference…
57751 <li><code>error</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struct…
57753 <li><code>readable</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_str…
57756 <li><code>writable</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_str…
57761 <li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Referenc…
57763 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Obje…
57768 … class="language-js"><span class="hljs-keyword">const</span> { finished } = <span class="hljs-buil…
57770 <span class="hljs-keyword">const</span> rs = fs.<span class="hljs-title function_">createReadStream…
57772 <span class="hljs-title function_">finished</span>(rs, <span class="hljs-function">(<span class="hl…
57773 <span class="hljs-keyword">if</span> (err) {
57774 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">error</spa…
57775 } <span class="hljs-keyword">else</span> {
57776 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>…
57780 rs.<span class="hljs-title function_">resume</span>(); <span class="hljs-comment">// Drain the stre…
57784 <p>The <code>finished</code> API is promisify-able as well;</p>
57785 …anguage-js"><span class="hljs-keyword">const</span> finished = util.<span class="hljs-title functi…
57787 <span class="hljs-keyword">const</span> rs = fs.<span class="hljs-title function_">createReadStream…
57789 …class="hljs-keyword">async</span> <span class="hljs-keyword">function</span> <span class="hljs-tit…
57790 <span class="hljs-keyword">await</span> <span class="hljs-title function_">finished</span>(rs);
57791 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>…
57794 …s-title function_">run</span>().<span class="hljs-title function_">catch</span>(<span class="hljs-
57795 rs.<span class="hljs-title function_">resume</span>(); <span class="hljs-comment">// Drain the stre…
57802-js"><span class="hljs-keyword">const</span> cleanup = <span class="hljs-title function_">finished…
57803 <span class="hljs-title function_">cleanup</span>();
57804 <span class="hljs-comment">// ...</span>
57822-US/docs/Web/JavaScript/Reference/Iteration_protocols#The_iterable_protocol" class="type">&#x3C;It…
57823-US/docs/Web/JavaScript/Reference/Iteration_protocols#The_iterable_protocol" class="type">&#x3C;It…
57825-US/docs/Web/JavaScript/Reference/Iteration_protocols#The_iterable_protocol" class="type">&#x3C;It…
57828 …" class="type">&#x3C;Stream></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScrip…
57830 <li><code>source</code> <a href="https://tc39.github.io/ecma262/#sec-asynciterable-interface" class…
57831 <li>Returns: <a href="https://tc39.github.io/ecma262/#sec-asynciterable-interface" class="type">&#x…
57834 …" class="type">&#x3C;Stream></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScrip…
57836 <li><code>source</code> <a href="https://tc39.github.io/ecma262/#sec-asynciterable-interface" class…
57837 …github.io/ecma262/#sec-asynciterable-interface" class="type">&#x3C;AsyncIterable></a> | <a href="h…
57840 <li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Referenc…
57842 <li><code>err</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Glo…
57850 … class="language-js"><span class="hljs-keyword">const</span> { pipeline } = <span class="hljs-buil…
57851 <span class="hljs-keyword">const</span> fs = <span class="hljs-built_in">require</span>(<span class…
57852 <span class="hljs-keyword">const</span> zlib = <span class="hljs-built_in">require</span>(<span cla…
57854 <span class="hljs-comment">// Use the pipeline API to easily pipe a series of streams</span>
57855 <span class="hljs-comment">// together and get notified when the pipeline is fully done.</span>
57857 <span class="hljs-comment">// A pipeline to gzip a potentially huge tar file efficiently:</span>
57859 <span class="hljs-title function_">pipeline</span>(
57860 …fs.<span class="hljs-title function_">createReadStream</span>(<span class="hljs-string">'archive.t…
57861 zlib.<span class="hljs-title function_">createGzip</span>(),
57862 …fs.<span class="hljs-title function_">createWriteStream</span>(<span class="hljs-string">'archive.…
57863 <span class="hljs-function">(<span class="hljs-params">err</span>) =></span> {
57864 <span class="hljs-keyword">if</span> (err) {
57865 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">error</spa…
57866 } <span class="hljs-keyword">else</span> {
57867 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>…
57871 <p>The <code>pipeline</code> API is promisify-able as well:</p>
57872 …anguage-js"><span class="hljs-keyword">const</span> pipeline = util.<span class="hljs-title functi…
57874 …class="hljs-keyword">async</span> <span class="hljs-keyword">function</span> <span class="hljs-tit…
57875 <span class="hljs-keyword">await</span> <span class="hljs-title function_">pipeline</span>(
57876 …fs.<span class="hljs-title function_">createReadStream</span>(<span class="hljs-string">'archive.t…
57877 zlib.<span class="hljs-title function_">createGzip</span>(),
57878 …fs.<span class="hljs-title function_">createWriteStream</span>(<span class="hljs-string">'archive.…
57880 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>…
57883 …s-title function_">run</span>().<span class="hljs-title function_">catch</span>(<span class="hljs-
57885 …anguage-js"><span class="hljs-keyword">const</span> pipeline = util.<span class="hljs-title functi…
57886 <span class="hljs-keyword">const</span> fs = <span class="hljs-built_in">require</span>(<span class…
57888 …class="hljs-keyword">async</span> <span class="hljs-keyword">function</span> <span class="hljs-tit…
57889 <span class="hljs-keyword">await</span> <span class="hljs-title function_">pipeline</span>(
57890 …fs.<span class="hljs-title function_">createReadStream</span>(<span class="hljs-string">'lowercase…
57891 <span class="hljs-keyword">async</span> <span class="hljs-keyword">function</span>* (source) {
57892 …source.<span class="hljs-title function_">setEncoding</span>(<span class="hljs-string">'utf8'</spa…
57893 …n class="hljs-keyword">for</span> <span class="hljs-keyword">await</span> (<span class="hljs-keywo…
57894 …<span class="hljs-keyword">yield</span> chunk.<span class="hljs-title function_">toUpperCase</span…
57897 …fs.<span class="hljs-title function_">createWriteStream</span>(<span class="hljs-string">'uppercas…
57899 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>…
57902 …s-title function_">run</span>().<span class="hljs-title function_">catch</span>(<span class="hljs-
57916 <li><code>iterable</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Referenc…
57919 <li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference…
57925-js"><span class="hljs-keyword">const</span> { <span class="hljs-title class_">Readable</span> } =…
57927 …ss="hljs-keyword">async</span> <span class="hljs-keyword">function</span> * <span class="hljs-titl…
57928 <span class="hljs-keyword">yield</span> <span class="hljs-string">'hello'</span>;
57929 <span class="hljs-keyword">yield</span> <span class="hljs-string">'streams'</span>;
57932 …ljs-keyword">const</span> readable = <span class="hljs-title class_">Readable</span>.<span class="…
57934 ….<span class="hljs-title function_">on</span>(<span class="hljs-string">'data'</span>, <span class…
57935 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>…
57948 <!-- eslint-disable no-useless-constructor -->
57949-js"><span class="hljs-keyword">const</span> { <span class="hljs-title class_">Writable</span> } =…
57951 …lass="hljs-keyword">class</span> <span class="hljs-title class_">MyWritable</span> <span class="hl…
57952 …<span class="hljs-title function_">constructor</span>(<span class="hljs-params">{ highWaterMark, .…
57953 <span class="hljs-variable language_">super</span>({ highWaterMark });
57954 <span class="hljs-comment">// ...</span>
57995 <table><thead><tr><th>Use-case</th><th>Class</th><th>Method(s) to implement</th></tr></thead><tbody…
58014-js"><span class="hljs-keyword">const</span> { <span class="hljs-title class_">Writable</span> } =…
58016 <span class="hljs-keyword">const</span> myWritable = <span class="hljs-keyword">new</span> <span cl…
58017 …<span class="hljs-title function_">write</span>(<span class="hljs-params">chunk, encoding, callbac…
58018 <span class="hljs-comment">// ...</span>
58041 <li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference…
58043 <li><code>highWaterMark</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Dat…
58046 <li><code>decodeStrings</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Dat…
58052 <li><code>defaultEncoding</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/D…
58055 <li><code>objectMode</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_s…
58060 <li><code>emitClose</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_st…
58062 <li><code>write</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/G…
58064 <li><code>writev</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/…
58066 <li><code>destroy</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference…
58068 <li><code>final</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/G…
58070 <li><code>autoDestroy</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_…
58075 <!-- eslint-disable no-useless-constructor -->
58076-js"><span class="hljs-keyword">const</span> { <span class="hljs-title class_">Writable</span> } =…
58078 …lass="hljs-keyword">class</span> <span class="hljs-title class_">MyWritable</span> <span class="hl…
58079 <span class="hljs-title function_">constructor</span>(<span class="hljs-params">options</span>) {
58080 <span class="hljs-comment">// Calls the stream.Writable() constructor.</span>
58081 <span class="hljs-variable language_">super</span>(options);
58082 <span class="hljs-comment">// ...</span>
58085 <p>Or, when using pre-ES6 style constructors:</p>
58086-js"><span class="hljs-keyword">const</span> { <span class="hljs-title class_">Writable</span> } =…
58087 <span class="hljs-keyword">const</span> util = <span class="hljs-built_in">require</span>(<span cla…
58089 <span class="hljs-keyword">function</span> <span class="hljs-title function_">MyWritable</span>(<sp…
58090 …ss="hljs-keyword">if</span> (!(<span class="hljs-variable language_">this</span> <span class="hljs
58091 …<span class="hljs-keyword">return</span> <span class="hljs-keyword">new</span> <span class="hljs-t…
58092 …<span class="hljs-title class_">Writable</span>.<span class="hljs-title function_">call</span>(<sp…
58094 util.<span class="hljs-title function_">inherits</span>(<span class="hljs-title class_">MyWritable<…
58096-js"><span class="hljs-keyword">const</span> { <span class="hljs-title class_">Writable</span> } =…
58098 <span class="hljs-keyword">const</span> myWritable = <span class="hljs-keyword">new</span> <span cl…
58099 …<span class="hljs-title function_">write</span>(<span class="hljs-params">chunk, encoding, callbac…
58100 <span class="hljs-comment">// ...</span>
58102 …<span class="hljs-title function_">writev</span>(<span class="hljs-params">chunks, callback</span>…
58103 <span class="hljs-comment">// ...</span>
58117 …zilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | …
58122 <li><code>encoding</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_str…
58125 <li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Referenc…
58158-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type">&#x3C;Object[]></a> The data…
58162 …" class="type">&#x3C;Buffer></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScrip…
58165 <li><code>encoding</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_str…
58169 <li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Referenc…
58187 <li><code>err</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Glo…
58188 <li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Referenc…
58198 <li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Referenc…
58215-js"><span class="hljs-keyword">const</span> { <span class="hljs-title class_">Writable</span> } =…
58217 <span class="hljs-keyword">const</span> myWritable = <span class="hljs-keyword">new</span> <span cl…
58218 …<span class="hljs-title function_">write</span>(<span class="hljs-params">chunk, encoding, callbac…
58219-keyword">if</span> (chunk.<span class="hljs-title function_">toString</span>().<span class="hljs-
58220 …ass="hljs-title function_">callback</span>(<span class="hljs-keyword">new</span> <span class="hljs
58221 } <span class="hljs-keyword">else</span> {
58222 <span class="hljs-title function_">callback</span>();
58231-js"><span class="hljs-keyword">const</span> { <span class="hljs-title class_">Writable</span> } =…
58233 …lass="hljs-keyword">class</span> <span class="hljs-title class_">MyWritable</span> <span class="hl…
58234 …<span class="hljs-title function_">_write</span>(<span class="hljs-params">chunk, encoding, callba…
58235-keyword">if</span> (chunk.<span class="hljs-title function_">toString</span>().<span class="hljs-
58236 …ass="hljs-title function_">callback</span>(<span class="hljs-keyword">new</span> <span class="hljs
58237 } <span class="hljs-keyword">else</span> {
58238 <span class="hljs-title function_">callback</span>();
58244 input is a string. This is not a trivial process when using multi-byte
58245 characters encoding, such as UTF-8. The following example shows how to decode
58246 multi-byte strings using <code>StringDecoder</code> and <a href="#stream_class_stream_writable"><co…
58247-js"><span class="hljs-keyword">const</span> { <span class="hljs-title class_">Writable</span> } =…
58248 …"hljs-keyword">const</span> { <span class="hljs-title class_">StringDecoder</span> } = <span class…
58250 …ss="hljs-keyword">class</span> <span class="hljs-title class_">StringWritable</span> <span class="…
58251 <span class="hljs-title function_">constructor</span>(<span class="hljs-params">options</span>) {
58252 <span class="hljs-variable language_">super</span>(options);
58253-variable language_">this</span>.<span class="hljs-property">_decoder</span> = <span class="hljs-k…
58254 …<span class="hljs-variable language_">this</span>.<span class="hljs-property">data</span> = <span …
58256 …<span class="hljs-title function_">_write</span>(<span class="hljs-params">chunk, encoding, callba…
58257 <span class="hljs-keyword">if</span> (encoding === <span class="hljs-string">'buffer'</span>) {
58258 …chunk = <span class="hljs-variable language_">this</span>.<span class="hljs-property">_decoder</sp…
58260 …<span class="hljs-variable language_">this</span>.<span class="hljs-property">data</span> += chunk;
58261 <span class="hljs-title function_">callback</span>();
58263 <span class="hljs-title function_">_final</span>(<span class="hljs-params">callback</span>) {
58264-variable language_">this</span>.<span class="hljs-property">data</span> += <span class="hljs-vari…
58265 <span class="hljs-title function_">callback</span>();
58269-keyword">const</span> euro = [[<span class="hljs-number">0xE2</span>, <span class="hljs-number">0…
58270 <span class="hljs-keyword">const</span> w = <span class="hljs-keyword">new</span> <span class="hljs
58272 w.<span class="hljs-title function_">write</span>(<span class="hljs-string">'currency: '</span>);
58273 w.<span class="hljs-title function_">write</span>(euro[<span class="hljs-number">0</span>]);
58274 w.<span class="hljs-title function_">end</span>(euro[<span class="hljs-number">1</span>]);
58276 …hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(w.<span clas…
58294 <li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference…
58296 <li><code>highWaterMark</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Dat…
58299 <li><code>encoding</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_str…
58301 <li><code>objectMode</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_s…
58304 <li><code>emitClose</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_st…
58306 <li><code>read</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Gl…
58308 <li><code>destroy</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference…
58310 <li><code>autoDestroy</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_…
58315 <!-- eslint-disable no-useless-constructor -->
58316-js"><span class="hljs-keyword">const</span> { <span class="hljs-title class_">Readable</span> } =…
58318 …lass="hljs-keyword">class</span> <span class="hljs-title class_">MyReadable</span> <span class="hl…
58319 <span class="hljs-title function_">constructor</span>(<span class="hljs-params">options</span>) {
58320 <span class="hljs-comment">// Calls the stream.Readable(options) constructor.</span>
58321 <span class="hljs-variable language_">super</span>(options);
58322 <span class="hljs-comment">// ...</span>
58325 <p>Or, when using pre-ES6 style constructors:</p>
58326-js"><span class="hljs-keyword">const</span> { <span class="hljs-title class_">Readable</span> } =…
58327 <span class="hljs-keyword">const</span> util = <span class="hljs-built_in">require</span>(<span cla…
58329 <span class="hljs-keyword">function</span> <span class="hljs-title function_">MyReadable</span>(<sp…
58330 …ss="hljs-keyword">if</span> (!(<span class="hljs-variable language_">this</span> <span class="hljs
58331 …<span class="hljs-keyword">return</span> <span class="hljs-keyword">new</span> <span class="hljs-t…
58332 …<span class="hljs-title class_">Readable</span>.<span class="hljs-title function_">call</span>(<sp…
58334 util.<span class="hljs-title function_">inherits</span>(<span class="hljs-title class_">MyReadable<…
58336-js"><span class="hljs-keyword">const</span> { <span class="hljs-title class_">Readable</span> } =…
58338 <span class="hljs-keyword">const</span> myReadable = <span class="hljs-keyword">new</span> <span cl…
58339 <span class="hljs-title function_">read</span>(<span class="hljs-params">size</span>) {
58340 <span class="hljs-comment">// ...</span>
58348 <li><code>size</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
58380 <li><code>err</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Glo…
58381 <li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Referenc…
58397-US/docs/Web/JavaScript/Reference/Global_Objects/Uint8Array" class="type">&#x3C;Uint8Array></a> | …
58401 <li><code>encoding</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_str…
58403 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boole…
58417 example, when wrapping a lower-level source that provides some form of
58418 pause/resume mechanism, and a data callback, the low-level source can be wrapped
58420 <pre><code class="language-js"><span class="hljs-comment">// `_source` is an object with readStop()…
58421 <span class="hljs-comment">// and an `ondata` member that gets called when it has data, and</span>
58422 <span class="hljs-comment">// an `onend` member that gets called when the data is over.</span>
58424 …ss="hljs-keyword">class</span> <span class="hljs-title class_">SourceWrapper</span> <span class="h…
58425 <span class="hljs-title function_">constructor</span>(<span class="hljs-params">options</span>) {
58426 <span class="hljs-variable language_">super</span>(options);
58428 …<span class="hljs-variable language_">this</span>.<span class="hljs-property">_source</span> = <sp…
58430 <span class="hljs-comment">// Every time there's data, push it into the internal buffer.</span>
58431-variable language_">this</span>.<span class="hljs-property">_source</span>.<span class="hljs-prop…
58432 <span class="hljs-comment">// If push() returns false, then stop reading from source.</span>
58433 …<span class="hljs-keyword">if</span> (!<span class="hljs-variable language_">this</span>.<span cla…
58434 …<span class="hljs-variable language_">this</span>.<span class="hljs-property">_source</span>.<span…
58437 <span class="hljs-comment">// When the source ends, push the EOF-signaling `null` chunk.</span>
58438 …ss="hljs-variable language_">this</span>.<span class="hljs-property">_source</span>.<span class="h…
58439 …<span class="hljs-variable language_">this</span>.<span class="hljs-title function_">push</span>(<…
58442 …<span class="hljs-comment">// _read() will be called when the stream wants to pull more data in.</…
58443 <span class="hljs-comment">// The advisory size argument is ignored in this case.</span>
58444 <span class="hljs-title function_">_read</span>(<span class="hljs-params">size</span>) {
58445 …<span class="hljs-variable language_">this</span>.<span class="hljs-property">_source</span>.<span…
58458-js"><span class="hljs-keyword">const</span> { <span class="hljs-title class_">Readable</span> } =…
58460 <span class="hljs-keyword">const</span> myReadable = <span class="hljs-keyword">new</span> <span cl…
58461 <span class="hljs-title function_">read</span>(<span class="hljs-params">size</span>) {
58462 …<span class="hljs-keyword">const</span> err = <span class="hljs-title function_">checkSomeErrorCon…
58463 <span class="hljs-keyword">if</span> (err) {
58464 …<span class="hljs-variable language_">this</span>.<span class="hljs-title function_">destroy</span…
58465 } <span class="hljs-keyword">else</span> {
58466 <span class="hljs-comment">// Do some work.</span>
58474-js"><span class="hljs-keyword">const</span> { <span class="hljs-title class_">Readable</span> } =…
58476 …class="hljs-keyword">class</span> <span class="hljs-title class_">Counter</span> <span class="hljs
58477 <span class="hljs-title function_">constructor</span>(<span class="hljs-params">opt</span>) {
58478 <span class="hljs-variable language_">super</span>(opt);
58479 …<span class="hljs-variable language_">this</span>.<span class="hljs-property">_max</span> = <span …
58480 …<span class="hljs-variable language_">this</span>.<span class="hljs-property">_index</span> = <spa…
58483 <span class="hljs-title function_">_read</span>(<span class="hljs-params"></span>) {
58484 …<span class="hljs-keyword">const</span> i = <span class="hljs-variable language_">this</span>.<spa…
58485 …<span class="hljs-keyword">if</span> (i > <span class="hljs-variable language_">this</span>.<span …
58486 …<span class="hljs-variable language_">this</span>.<span class="hljs-title function_">push</span>(<…
58487 <span class="hljs-keyword">else</span> {
58488 … <span class="hljs-keyword">const</span> str = <span class="hljs-title class_">String</span>(i);
58489 …hljs-keyword">const</span> buf = <span class="hljs-title class_">Buffer</span>.<span class="hljs-t…
58490 …<span class="hljs-variable language_">this</span>.<span class="hljs-title function_">push</span>(b…
58502 both base classes due to overriding <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScrip…
58518 <li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference…
58521 <li><code>allowHalfOpen</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Dat…
58524 <li><code>readable</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_str…
58526 <li><code>writable</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_str…
58528 <li><code>readableObjectMode</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScrip…
58530 <li><code>writableObjectMode</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScrip…
58532 <li><code>readableHighWaterMark</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaSc…
58534 <li><code>writableHighWaterMark</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaSc…
58539 <!-- eslint-disable no-useless-constructor -->
58540-js"><span class="hljs-keyword">const</span> { <span class="hljs-title class_">Duplex</span> } = <…
58542 …class="hljs-keyword">class</span> <span class="hljs-title class_">MyDuplex</span> <span class="hlj…
58543 <span class="hljs-title function_">constructor</span>(<span class="hljs-params">options</span>) {
58544 <span class="hljs-variable language_">super</span>(options);
58545 <span class="hljs-comment">// ...</span>
58548 <p>Or, when using pre-ES6 style constructors:</p>
58549-js"><span class="hljs-keyword">const</span> { <span class="hljs-title class_">Duplex</span> } = <…
58550 <span class="hljs-keyword">const</span> util = <span class="hljs-built_in">require</span>(<span cla…
58552 <span class="hljs-keyword">function</span> <span class="hljs-title function_">MyDuplex</span>(<span…
58553 …ss="hljs-keyword">if</span> (!(<span class="hljs-variable language_">this</span> <span class="hljs
58554 …<span class="hljs-keyword">return</span> <span class="hljs-keyword">new</span> <span class="hljs-t…
58555 …<span class="hljs-title class_">Duplex</span>.<span class="hljs-title function_">call</span>(<span…
58557 util.<span class="hljs-title function_">inherits</span>(<span class="hljs-title class_">MyDuplex</s…
58559-js"><span class="hljs-keyword">const</span> { <span class="hljs-title class_">Duplex</span> } = <…
58561 <span class="hljs-keyword">const</span> myDuplex = <span class="hljs-keyword">new</span> <span clas…
58562 <span class="hljs-title function_">read</span>(<span class="hljs-params">size</span>) {
58563 <span class="hljs-comment">// ...</span>
58565 …<span class="hljs-title function_">write</span>(<span class="hljs-params">chunk, encoding, callbac…
58566 <span class="hljs-comment">// ...</span>
58571 hypothetical lower-level source object to which data can be written, and
58577-js"><span class="hljs-keyword">const</span> { <span class="hljs-title class_">Duplex</span> } = <…
58578 <span class="hljs-keyword">const</span> kSource = <span class="hljs-title class_">Symbol</span>(<sp…
58580 …class="hljs-keyword">class</span> <span class="hljs-title class_">MyDuplex</span> <span class="hlj…
58581 …<span class="hljs-title function_">constructor</span>(<span class="hljs-params">source, options</s…
58582 <span class="hljs-variable language_">super</span>(options);
58583 <span class="hljs-variable language_">this</span>[kSource] = source;
58586 …<span class="hljs-title function_">_write</span>(<span class="hljs-params">chunk, encoding, callba…
58587 <span class="hljs-comment">// The underlying source only deals with strings.</span>
58588 …<span class="hljs-keyword">if</span> (<span class="hljs-title class_">Buffer</span>.<span class="h…
58589 chunk = chunk.<span class="hljs-title function_">toString</span>();
58590 …<span class="hljs-variable language_">this</span>[kSource].<span class="hljs-title function_">writ…
58591 <span class="hljs-title function_">callback</span>();
58594 <span class="hljs-title function_">_read</span>(<span class="hljs-params">size</span>) {
58595 …s-variable language_">this</span>[kSource].<span class="hljs-title function_">fetchSomeData</span>…
58596 …hljs-variable language_">this</span>.<span class="hljs-title function_">push</span>(<span class="h…
58601 <code>Writable</code> sides operate independently of one another despite co-existing within
58611-js"><span class="hljs-keyword">const</span> { <span class="hljs-title class_">Transform</span> } …
58613 <span class="hljs-comment">// All Transform streams are also Duplex Streams.</span>
58614 <span class="hljs-keyword">const</span> myTransform = <span class="hljs-keyword">new</span> <span c…
58615 <span class="hljs-attr">writableObjectMode</span>: <span class="hljs-literal">true</span>,
58617 …<span class="hljs-title function_">transform</span>(<span class="hljs-params">chunk, encoding, cal…
58618 <span class="hljs-comment">// Coerce the chunk to a number if necessary.</span>
58619 chunk |= <span class="hljs-number">0</span>;
58621 <span class="hljs-comment">// Transform the chunk into something else.</span>
58622 …<span class="hljs-keyword">const</span> data = chunk.<span class="hljs-title function_">toString</…
58624 <span class="hljs-comment">// Push the data onto the readable queue.</span>
58625-title function_">callback</span>(<span class="hljs-literal">null</span>, <span class="hljs-string…
58629 myTransform.<span class="hljs-title function_">setEncoding</span>(<span class="hljs-string">'ascii'…
58630-title function_">on</span>(<span class="hljs-string">'data'</span>, <span class="hljs-function">(…
58632 myTransform.<span class="hljs-title function_">write</span>(<span class="hljs-number">1</span>);
58633 <span class="hljs-comment">// Prints: 01</span>
58634 myTransform.<span class="hljs-title function_">write</span>(<span class="hljs-number">10</span>);
58635 <span class="hljs-comment">// Prints: 0a</span>
58636 myTransform.<span class="hljs-title function_">write</span>(<span class="hljs-number">100</span>);
58637 <span class="hljs-comment">// Prints: 64</span></code></pre>
58658 <li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference…
58661 <li><code>transform</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Referen…
58663 <li><code>flush</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/G…
58668 <!-- eslint-disable no-useless-constructor -->
58669-js"><span class="hljs-keyword">const</span> { <span class="hljs-title class_">Transform</span> } …
58671 …ass="hljs-keyword">class</span> <span class="hljs-title class_">MyTransform</span> <span class="hl…
58672 <span class="hljs-title function_">constructor</span>(<span class="hljs-params">options</span>) {
58673 <span class="hljs-variable language_">super</span>(options);
58674 <span class="hljs-comment">// ...</span>
58677 <p>Or, when using pre-ES6 style constructors:</p>
58678-js"><span class="hljs-keyword">const</span> { <span class="hljs-title class_">Transform</span> } …
58679 <span class="hljs-keyword">const</span> util = <span class="hljs-built_in">require</span>(<span cla…
58681 <span class="hljs-keyword">function</span> <span class="hljs-title function_">MyTransform</span>(<s…
58682 …ss="hljs-keyword">if</span> (!(<span class="hljs-variable language_">this</span> <span class="hljs
58683 …<span class="hljs-keyword">return</span> <span class="hljs-keyword">new</span> <span class="hljs-t…
58684 …<span class="hljs-title class_">Transform</span>.<span class="hljs-title function_">call</span>(<s…
58686 util.<span class="hljs-title function_">inherits</span>(<span class="hljs-title class_">MyTransform…
58688-js"><span class="hljs-keyword">const</span> { <span class="hljs-title class_">Transform</span> } …
58690 <span class="hljs-keyword">const</span> myTransform = <span class="hljs-keyword">new</span> <span c…
58691 …<span class="hljs-title function_">transform</span>(<span class="hljs-params">chunk, encoding, cal…
58692 <span class="hljs-comment">// ...</span>
58707 <li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Referenc…
58730 …zilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | …
58735 <li><code>encoding</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_str…
58738 <li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Referenc…
58758-js">transform.<span class="hljs-property"><span class="hljs-keyword">prototype</span></span>.<spa…
58759 …<span class="hljs-variable language_">this</span>.<span class="hljs-title function_">push</span>(d…
58760 <span class="hljs-title function_">callback</span>();
58763-property"><span class="hljs-keyword">prototype</span></span>.<span class="hljs-property">_transfo…
58764 <span class="hljs-title function_">callback</span>(<span class="hljs-literal">null</span>, data);
58781 generators are effectively a first-class language-level stream construct at
58786 …e><code class="language-js">(<span class="hljs-keyword">async</span> <span class="hljs-keyword">fu…
58787 …n class="hljs-keyword">for</span> <span class="hljs-keyword">await</span> (<span class="hljs-keywo…
58788 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>…
58792 unhandled post-destroy errors.</p>
58796-js"><span class="hljs-keyword">const</span> { <span class="hljs-title class_">Readable</span> } =…
58798 …ss="hljs-keyword">async</span> <span class="hljs-keyword">function</span> * <span class="hljs-titl…
58799 <span class="hljs-keyword">yield</span> <span class="hljs-string">'a'</span>;
58800 <span class="hljs-keyword">yield</span> <span class="hljs-string">'b'</span>;
58801 <span class="hljs-keyword">yield</span> <span class="hljs-string">'c'</span>;
58804 …ljs-keyword">const</span> readable = <span class="hljs-title class_">Readable</span>.<span class="…
58806 ….<span class="hljs-title function_">on</span>(<span class="hljs-string">'data'</span>, <span class…
58807 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>…
58812 the handling of backpressure and backpressure-related errors:</p>
58813 … class="language-js"><span class="hljs-keyword">const</span> { pipeline } = <span class="hljs-buil…
58814 <span class="hljs-keyword">const</span> util = <span class="hljs-built_in">require</span>(<span cla…
58815 <span class="hljs-keyword">const</span> fs = <span class="hljs-built_in">require</span>(<span class…
58817 <span class="hljs-keyword">const</span> writable = fs.<span class="hljs-title function_">createWrit…
58819 <span class="hljs-comment">// Callback Pattern</span>
58820 <span class="hljs-title function_">pipeline</span>(iterator, writable, <span class="hljs-function">…
58821 <span class="hljs-keyword">if</span> (err) {
58822 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">error</spa…
58823 } <span class="hljs-keyword">else</span> {
58824 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>…
58828 <span class="hljs-comment">// Promise Pattern</span>
58829 <span class="hljs-keyword">const</span> pipelinePromise = util.<span class="hljs-title function_">p…
58830 <span class="hljs-title function_">pipelinePromise</span>(iterator, writable)
58831 ….<span class="hljs-title function_">then</span>(<span class="hljs-function">(<span class="hljs-par…
58832 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>…
58834 ….<span class="hljs-title function_">catch</span>(<span class="hljs-variable language_">console</sp…
58864 <pre><code class="language-js"><span class="hljs-comment">// WARNING! BROKEN!</span>
58865 net.<span class="hljs-title function_">createServer</span>(<span class="hljs-function">(<span class…
58867 <span class="hljs-comment">// We add an 'end' listener, but never consume the data.</span>
58868 …socket.<span class="hljs-title function_">on</span>(<span class="hljs-string">'end'</span>, <span …
58869 <span class="hljs-comment">// It will never get here.</span>
58870 …socket.<span class="hljs-title function_">end</span>(<span class="hljs-string">'The message was re…
58873 }).<span class="hljs-title function_">listen</span>(<span class="hljs-number">1337</span>);</code><…
58878 <pre><code class="language-js"><span class="hljs-comment">// Workaround.</span>
58879 net.<span class="hljs-title function_">createServer</span>(<span class="hljs-function">(<span class…
58880 …socket.<span class="hljs-title function_">on</span>(<span class="hljs-string">'end'</span>, <span …
58881 …socket.<span class="hljs-title function_">end</span>(<span class="hljs-string">'The message was re…
58884 <span class="hljs-comment">// Start the flow of data, discarding it.</span>
58885 socket.<span class="hljs-title function_">resume</span>();
58886 }).<span class="hljs-title function_">listen</span>(<span class="hljs-number">1337</span>);</code><…
58888 pre-0.10 style streams can be wrapped in a <code>Readable</code> class using the
58897 a low-level <a href="#stream_readable_read_size_1"><code>stream._read()</code></a> call.</p>
58903 <p>Pushing a zero-byte string, <code>Buffer</code> or <code>Uint8Array</code> to a stream that is n…
58910 <code>highWaterMark</code> operates in non-object mode.</p>
58916 contain multi-byte characters.</p></section>
58920 …ty api_stability_2"><a href="#documentation_stability_index">Stability: 2</a> - Stable</div><p></p>
58923 strings in a manner that preserves encoded multi-byte UTF-8 and UTF-16
58925-js"><span class="hljs-keyword">const</span> { <span class="hljs-title class_">StringDecoder</span…
58927-js"><span class="hljs-keyword">const</span> { <span class="hljs-title class_">StringDecoder</span…
58928 …"hljs-keyword">const</span> decoder = <span class="hljs-keyword">new</span> <span class="hljs-titl…
58930-keyword">const</span> cent = <span class="hljs-title class_">Buffer</span>.<span class="hljs-titl…
58931 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(…
58933-keyword">const</span> euro = <span class="hljs-title class_">Buffer</span>.<span class="hljs-titl…
58934 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(…
58939 <p>In the following example, the three UTF-8 encoded bytes of the European Euro
58941-js"><span class="hljs-keyword">const</span> { <span class="hljs-title class_">StringDecoder</span…
58942 …"hljs-keyword">const</span> decoder = <span class="hljs-keyword">new</span> <span class="hljs-titl…
58944 …"hljs-title function_">write</span>(<span class="hljs-title class_">Buffer</span>.<span class="hlj…
58945 …"hljs-title function_">write</span>(<span class="hljs-title class_">Buffer</span>.<span class="hlj…
58946-variable language_">console</span>.<span class="hljs-title function_">log</span>(decoder.<span cl…
58953 <li><code>encoding</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_str…
58962 …org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray" class="type">&#x3C;TypedArray><…
58964 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Strin…
58967 representing incomplete UTF-8 and UTF-16 characters will be replaced with
58985 …org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray" class="type">&#x3C;TypedArray><…
58987 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Strin…
58996 …ty api_stability_2"><a href="#documentation_stability_index">Stability: 2</a> - Stable</div><p></p>
59003 …ode.js <a href="https://nodejs.org/en/docs/guides/event-loop-timers-and-nexttick/#setimmediate-vs-
59017 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boole…
59057 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boole…
59103 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Numbe…
59121 <li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Referenc…
59122 …ode.js <a href="https://nodejs.org/en/docs/guides/event-loop-timers-and-nexttick/#setimmediate-vs-
59123 <li><code>...args</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_stru…
59136 …code class="language-js"><span class="hljs-keyword">const</span> util = <span class="hljs-built_in…
59137 <span class="hljs-keyword">const</span> setImmediatePromise = util.<span class="hljs-title function…
59139-title function_">setImmediatePromise</span>(<span class="hljs-string">'foobar'</span>).<span clas…
59140 <span class="hljs-comment">// value === 'foobar' (passing values is optional)</span>
59141 <span class="hljs-comment">// This is executed after all I/O callbacks.</span>
59144 <span class="hljs-comment">// Or with async function</span>
59145 …s="hljs-keyword">async</span> <span class="hljs-keyword">function</span> <span class="hljs-title f…
59146 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>…
59147 …<span class="hljs-keyword">await</span> <span class="hljs-title function_">setImmediatePromise</sp…
59148 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>…
59150 <span class="hljs-title function_">timerExample</span>();</code></pre>
59156 <li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Referenc…
59157 <li><code>delay</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struct…
59159 <li><code>...args</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_stru…
59164 set to <code>1</code>. Non-integer delays are truncated to an integer.</p>
59171 <li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Referenc…
59172 <li><code>delay</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struct…
59174 <li><code>...args</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_stru…
59177 <p>Schedules execution of a one-time <code>callback</code> after <code>delay</code> milliseconds.</…
59183 will be set to <code>1</code>. Non-integer delays are truncated to an integer.</p>
59187 …code class="language-js"><span class="hljs-keyword">const</span> util = <span class="hljs-built_in…
59188 <span class="hljs-keyword">const</span> setTimeoutPromise = util.<span class="hljs-title function_"…
59190-number">40</span>, <span class="hljs-string">'foobar'</span>).<span class="hljs-title function_">…
59191 <span class="hljs-comment">// value === 'foobar' (passing values is optional)</span>
59192 <span class="hljs-comment">// This is executed after about 40 milliseconds.</span>
59202 …code class="language-js"><span class="hljs-keyword">const</span> util = <span class="hljs-built_in…
59203 <span class="hljs-keyword">const</span> setImmediatePromise = util.<span class="hljs-title function…
59205 <span class="hljs-keyword">const</span> ac = <span class="hljs-keyword">new</span> <span class="hlj…
59206 <span class="hljs-keyword">const</span> signal = ac.<span class="hljs-property">signal</span>;
59208 <span class="hljs-title function_">setImmediatePromise</span>(<span class="hljs-string">'foobar'</s…
59209 ….<span class="hljs-title function_">then</span>(<span class="hljs-variable language_">console</spa…
59210 ….<span class="hljs-title function_">catch</span>(<span class="hljs-function">(<span class="hljs-pa…
59211 …<span class="hljs-keyword">if</span> (err.<span class="hljs-property">name</span> === <span class=…
59212 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>…
59215 ac.<span class="hljs-title function_">abort</span>();</code></pre>
59217 …code class="language-js"><span class="hljs-keyword">const</span> util = <span class="hljs-built_in…
59218 <span class="hljs-keyword">const</span> setTimeoutPromise = util.<span class="hljs-title function_"…
59220 <span class="hljs-keyword">const</span> ac = <span class="hljs-keyword">new</span> <span class="hlj…
59221 <span class="hljs-keyword">const</span> signal = ac.<span class="hljs-property">signal</span>;
59223 setTimeoutPromise(<span class="hljs-number">1000</span>, <span class="hljs-string">'foobar'</span>,…
59224 ….<span class="hljs-title function_">then</span>(<span class="hljs-variable language_">console</spa…
59225 ….<span class="hljs-title function_">catch</span>(<span class="hljs-function">(<span class="hljs-pa…
59226 …<span class="hljs-keyword">if</span> (err.<span class="hljs-property">name</span> === <span class=…
59227 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>…
59230 ac.<span class="hljs-title function_">abort</span>();</code></pre>
59245 …zilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | …
59254 …zilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | …
59261 …ty api_stability_2"><a href="#documentation_stability_index">Stability: 2</a> - Stable</div><p></p>
59266 …code class="language-js"><span class="hljs-keyword">const</span> tls = <span class="hljs-built_in"…
59271 use of the OpenSSL command-line interface to generate a 2048-bit RSA private
59273 <pre><code class="language-bash">openssl genrsa -out ryans-key.pem 2048</code></pre>
59277 private key (such certificates are referred to as "self-signed"). The first
59280 <p>The OpenSSL command-line interface can be used to generate a CSR for a private
59282 <pre><code class="language-bash">openssl req -new -sha256 -key ryans-key.pem -out ryans-csr.pem</co…
59284 Authority for signing or used to generate a self-signed certificate.</p>
59285 <p>Creating a self-signed certificate using the OpenSSL command-line interface
59287 …<code class="language-bash">openssl x509 -req -<span class="hljs-keyword">in</span> ryans-csr.pem
59290 …s="language-bash">openssl pkcs12 -<span class="hljs-built_in">export</span> -<span class="hljs-key…
59291 -certfile ca-cert.pem -out ryans.pfx</code></pre>
59297 a single file, e.g. <code>cat ca1-cert.pem ca2-cert.pem > ca-cert.pem</code></li>
59302 of key-agreement (i.e., key-exchange) methods. That is, the server and client
59306 by eavesdroppers if the attacker manages to obtain the key-pair specifically
59309 key-agreement on every TLS/SSL handshake (in contrast to using the same key for
59314 …80%93Hellman_key_exchange">DHE</a>: An ephemeral version of the Diffie-Hellman key-agreement proto…
59315 …_curve_Diffie%E2%80%93Hellman">ECDHE</a>: An ephemeral version of the Elliptic Curve Diffie-Hellman
59316 key-agreement protocol.</li>
59321 to generate Diffie-Hellman parameters and specify them with the <code>dhparam</code>
59323 the OpenSSL command-line interface to generate such parameters:</p>
59324 <pre><code class="language-bash">openssl dhparam -outform PEM -out dhparam.pem 2048</code></pre>
59325 <p>If using perfect forward secrecy using <code>ECDHE</code>, Diffie-Hellman parameters are
59333 <p>ALPN (Application-Layer Protocol Negotiation Extension) and
59340 <h4>Pre-shared keys<span><a class="mark" href="#tls_pre_shared_keys" id="tls_pre_shared_keys">#</a>…
59342 <p>TLS-PSK support is available as an alternative to normal certificate-based
59343 authentication. It uses a pre-shared key instead of certificates to
59345 TLS-PSK and public key infrastructure are not mutually exclusive. Clients and
59348 <p>TLS-PSK is only a good choice where means exist to securely share a
59351 The TLS-PSK implementation in OpenSSL has seen many security flaws in
59356 low-entropy sources is not secure.</p>
59357 <p>PSK ciphers are disabled by default, and using TLS-PSK thus requires explicitly
59359 ciphers can be retrieved via <code>openssl ciphers -v 'PSK'</code>. All TLS 1.3
59361 supported they can be retrieved via <code>openssl ciphers -v -s -tls1_3 -psk</code>.</p>
59367 <h4>Client-initiated renegotiation attack mitigation<span><a class="mark" href="#tls_client_initiat…
59371 of server-side resources, making it a potential vector for denial-of-service
59377 <li><code>tls.CLIENT_RENEG_LIMIT</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaS…
59379 <li><code>tls.CLIENT_RENEG_WINDOW</code> <a href="https://developer.mozilla.org/en-US/docs/Web/Java…
59409 in the initial connection. This mechanism avoids the need for server-side
59418 ticket, otherwise it contains client-side session state.</p>
59424 all have the same ticket keys. There are three 16-byte keys internally, but the
59425 tls API exposes them as a single 48-byte buffer for convenience.</p>
59446 <code>-reconnect</code> option to <code>openssl s_client</code>, for example:</p>
59447 …code class="language-console"><span class="hljs-meta">$ </span><span class="language-bash">openssl…
59450 <pre><code class="language-text">New, TLSv1.2, Cipher is ECDHE-RSA-AES128-GCM-SHA256</code></pre>
59452 <pre><code class="language-text">Reused, TLSv1.2, Cipher is ECDHE-RSA-AES128-GCM-SHA256</code></pre>
59458 <pre><code class="language-console">node -p crypto.constants.defaultCoreCipherList | tr ':' '\n'
59462 ECDHE-RSA-AES128-GCM-SHA256
59463 ECDHE-ECDSA-AES128-GCM-SHA256
59464 ECDHE-RSA-AES256-GCM-SHA384
59465 ECDHE-ECDSA-AES256-GCM-SHA384
59466 DHE-RSA-AES128-GCM-SHA256
59467 ECDHE-RSA-AES128-SHA256
59468 DHE-RSA-AES128-SHA256
59469 ECDHE-RSA-AES256-SHA384
59470 DHE-RSA-AES256-SHA384
59471 ECDHE-RSA-AES256-SHA256
59472 DHE-RSA-AES256-SHA256
59483 … replaced entirely using the <a href="#cli_tls_cipher_list_list"><code>--tls-cipher-list</code></a>
59484 command-line switch (directly, or via the <a href="#cli_node_options_options"><code>NODE_OPTIONS</c…
59485 variable). For instance, the following makes <code>ECDHE-RSA-AES128-GCM-SHA256:!RC4</code>
59487 <pre><code class="language-bash">node --tls-cipher-list=<span class="hljs-string">'ECDHE-RSA-AES128
59489 <span class="hljs-built_in">export</span> NODE_OPTIONS=--tls-cipher-list=<span class="hljs-string">…
59498 the OpenSSL <a href="https://www.openssl.org/docs/man1.1.1/man1/ciphers.html#CIPHER-LIST-FORMAT">ci…
59510 of an application. The <code>--tls-cipher-list</code> switch and <code>ciphers</code> option should…
59512 …ciphers for <a href="https://www.chromium.org/Home/chromium-security/education/tls#TOC-Cipher-Suit…
59517 <p>Old clients that rely on insecure and deprecated RC4 or DES-based ciphers
59521 …L <a href="https://www.openssl.org/docs/man1.1.1/man1/ciphers.html#CIPHER-LIST-FORMAT">cipher list…
59530 <p>The first 3 are enabled by default. The last 2 <code>CCM</code>-based suites are supported
59537 … api_stability_0"><a href="#documentation_stability_index">Stability: 0</a> - Deprecated: Use <a h…
59551 … api_stability_0"><a href="#documentation_stability_index">Stability: 0</a> - Deprecated: Use <a h…
59599-js"><span class="hljs-keyword">const</span> logFile = fs.<span class="hljs-title function_">creat…
59600 <span class="hljs-comment">// ...</span>
59601 …<span class="hljs-title function_">on</span>(<span class="hljs-string">'keylog'</span>, <span clas…
59602 …<span class="hljs-keyword">if</span> (tlsSocket.<span class="hljs-property">remoteAddress</span> !…
59603 …<span class="hljs-keyword">return</span>; <span class="hljs-comment">// Only log keys for a partic…
59604 logFile.<span class="hljs-title function_">write</span>(line);
59625 <li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Referenc…
59639 <li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Referenc…
59645 Both <code>certificate</code> and <code>issuer</code> are <code>Buffer</code> DER-representations o…
59664 <p>The <code>issuer</code> can be <code>null</code> if the certificate is either self-signed or the
59679 <li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Referenc…
59682 <li><code>err</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Glo…
59697 <pre><code class="language-js"><span class="hljs-keyword">const</span> tlsSessionStore = {};
59698 …pan class="hljs-title function_">on</span>(<span class="hljs-string">'newSession'</span>, <span cl…
59699 …tlsSessionStore[id.<span class="hljs-title function_">toString</span>(<span class="hljs-string">'h…
59700 <span class="hljs-title function_">cb</span>();
59702 …an class="hljs-title function_">on</span>(<span class="hljs-string">'resumeSession'</span>, <span …
59703-title function_">cb</span>(<span class="hljs-literal">null</span>, tlsSessionStore[id.<span class…
59733 <li><code>exception</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Referen…
59742 <li><code>hostname</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_str…
59743 <li><code>context</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference…
59754 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Obje…
59764 <li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Referenc…
59775 … api_stability_0"><a href="#documentation_stability_index">Stability: 0</a> - Deprecated: Use <a h…
59777 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" c…
59785 <li>Returns: <a href="#buffer_class_buffer" class="type">&#x3C;Buffer></a> A 48-byte buffer contain…
59797 <li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference…
59808-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray" class="type">&#x3C;TypedArray></a> | …
59847 <li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference…
59862 <li><code>requestOCSP</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_…
59888-js"><span class="hljs-keyword">const</span> logFile = fs.<span class="hljs-title function_">creat…
59889 <span class="hljs-comment">// ...</span>
59890-title function_">on</span>(<span class="hljs-string">'keylog'</span>, <span class="hljs-function"…
59936 the handshake is complete. For TLSv1.3, only ticket-based resumption is allowed
59943-js">tlsSocket.<span class="hljs-title function_">once</span>(<span class="hljs-string">'session'<…
59944 <span class="hljs-comment">// The session can be used immediately or later.</span>
59945 tls.<span class="hljs-title function_">connect</span>({
59946 <span class="hljs-attr">session</span>: session,
59947 <span class="hljs-comment">// Other connect options...</span>
59955 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Obje…
59971 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boole…
59987 …ut is identical to the output of <code>openssl s_client -trace</code> or <code>openssl s_server -t…
60001 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Obje…
60024 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Obje…
60026 <li><code>name</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
60027 <li><code>standardName</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data…
60028 <li><code>version</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_stru…
60035 <pre><code class="language-json"><span class="hljs-punctuation">{</span>
60036 …n class="hljs-attr">"name"</span><span class="hljs-punctuation">:</span> <span class="hljs-string"…
60037-attr">"standardName"</span><span class="hljs-punctuation">:</span> <span class="hljs-string">"TLS…
60038 …<span class="hljs-attr">"version"</span><span class="hljs-punctuation">:</span> <span class="hljs-
60039 <span class="hljs-punctuation">}</span></code></pre>
60048 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Obje…
60062 …" class="type">&#x3C;Buffer></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScrip…
60071 to implement the <code>tls-unique</code> channel binding from <a href="https://tools.ietf.org/html/…
60077 <li><code>detailed</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_str…
60079 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Obje…
60101 <li><code>subject</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference…
60106 <li><code>issuer</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/…
60108 …tps://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#…
60109 …tps://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#…
60110 <li><code>serialNumber</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data…
60112 …tps://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#…
60114 …tps://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#…
60117 <li><code>ext_key_usage</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Ref…
60118 <li><code>subjectaltname</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Da…
60120 <li><code>infoAccess</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Refere…
60122 <li><code>issuerCertificate</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript…
60123 self-signed certificates, this may be a circular reference.</li>
60129 <li><code>bits</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
60130 <li><code>exponent</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_str…
60132 <li><code>modulus</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_stru…
60139 <li><code>bits</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
60140 <li><code>asn1Curve</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_st…
60141 curve. Well-known curves are identified by an OID. While it is unusual, it is
60144 <li><code>nistCurve</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_st…
60145 has one (not all well-known curves have been assigned names by NIST). Example:
60146 <code>'P-256'</code>.</li>
60149 <!-- eslint-skip -->
60150 <pre><code class="language-js">{ <span class="hljs-attr">subject</span>:
60151 …{ <span class="hljs-attr">OU</span>: [ <span class="hljs-string">'Domain Control Validated'</span>…
60152 <span class="hljs-attr">CN</span>: <span class="hljs-string">'*.nodejs.org'</span> },
60153 <span class="hljs-attr">issuer</span>:
60154 { <span class="hljs-attr">C</span>: <span class="hljs-string">'GB'</span>,
60155 <span class="hljs-attr">ST</span>: <span class="hljs-string">'Greater Manchester'</span>,
60156 <span class="hljs-attr">L</span>: <span class="hljs-string">'Salford'</span>,
60157 <span class="hljs-attr">O</span>: <span class="hljs-string">'COMODO CA Limited'</span>,
60158 …<span class="hljs-attr">CN</span>: <span class="hljs-string">'COMODO RSA Domain Validation Secure …
60159 …<span class="hljs-attr">subjectaltname</span>: <span class="hljs-string">'DNS:*.nodejs.org, DNS:no…
60160 <span class="hljs-attr">infoAccess</span>:
60161 { <span class="hljs-string">'CA Issuers - URI'</span>:
60162 …[ <span class="hljs-string">'http://crt.comodoca.com/COMODORSADomainValidationSecureServerCA.crt'<…
60163 …<span class="hljs-string">'OCSP - URI'</span>: [ <span class="hljs-string">'http://ocsp.comodoca.c…
60164 …<span class="hljs-attr">modulus</span>: <span class="hljs-string">'B56CE45CB740B09A13F64AC543B712F…
60165 <span class="hljs-attr">exponent</span>: <span class="hljs-string">'0x10001'</span>,
60166 <span class="hljs-attr">pubkey</span>: &#x3C;Buffer ... >,
60179 …" class="type">&#x3C;Buffer></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScrip…
60188 to implement the <code>tls-unique</code> channel binding from <a href="https://tools.ietf.org/html/…
60194 …zilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | …
60228 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Obje…
60240 <p><code>length</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struct…
60243 <p><code>label</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
60245 <a href="https://www.iana.org/assignments/tls-parameters/tls-parameters.xhtml#exporter-labels">IANA…
60257 <pre><code class="language-js"><span class="hljs-keyword">const</span> keyingMaterial = tlsSocket.<…
60258 <span class="hljs-number">128</span>,
60259 <span class="hljs-string">'client finished'</span>);
60261 <span class="hljs-comment">/**
60285 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boole…
60293 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" c…
60301 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" c…
60309 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" c…
60318 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" c…
60320 <p>Returns the string representation of the remote IP family. <code>'IPv4'</code> or <code>'IPv6'</…
60326 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" c…
60335 <p><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/…
60337 <li><code>rejectUnauthorized</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScrip…
60345 <p><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference…
60352 <p>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolea…
60369 <li><code>size</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
60371 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boole…
60387 …ier</code> subject alternative names has been disabled in response to CVE-2021-44531.</p></td></tr>
60394 <li><code>hostname</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_str…
60396 <li><code>cert</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Gl…
60397 …illa.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error" class="type">&#x3C;Error></a> |…
60400 <p>Returns <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Object…
60401 failure. On success, returns <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_…
60410 was present (see <a href="https://cve.mitre.org/cgi-bin/cvename.cgi?name=CVE-2021-44531">CVE-2021-4…
60446 <li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference…
60449 <li><code>host</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
60451 <li><code>port</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
60452 <li><code>path</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
60463 <li><code>allowHalfOpen</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Dat…
60467 <li><code>rejectUnauthorized</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScrip…
60471 <li><code>pskCallback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Refer…
60473 <li>hint: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_t…
60476 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Obje…
60480 <code>identity</code> must use UTF-8 encoding.</li>
60482 When negotiating TLS-PSK (pre-shared keys), this function is called
60490-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string[]></a> | <a href="#…
60498 <li><code>servername</code>: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_…
60500 name, and not an IP address. It can be used by a multi-homed server to
60503 <li><code>checkServerIdentity(servername, cert)</code> <a href="https://developer.mozilla.org/en-US…
60506 …certificate. This should return an <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScrip…
60510 <li><code>minDHSize</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_st…
60514 <li><code>highWaterMark</code>: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Da…
60520 <li><code>onread</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/…
60529 <li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Referenc…
60542 <pre><code class="language-js"><span class="hljs-comment">// Assumes an echo server that is listeni…
60543 <span class="hljs-keyword">const</span> tls = <span class="hljs-built_in">require</span>(<span clas…
60544 <span class="hljs-keyword">const</span> fs = <span class="hljs-built_in">require</span>(<span class…
60546 <span class="hljs-keyword">const</span> options = {
60547 …<span class="hljs-comment">// Necessary only if the server requires client certificate authenticat…
60548 …<span class="hljs-attr">key</span>: fs.<span class="hljs-title function_">readFileSync</span>(<spa…
60549 …<span class="hljs-attr">cert</span>: fs.<span class="hljs-title function_">readFileSync</span>(<sp…
60551 <span class="hljs-comment">// Necessary only if the server uses a self-signed certificate.</span>
60552 …<span class="hljs-attr">ca</span>: [ fs.<span class="hljs-title function_">readFileSync</span>(<sp…
60554 <span class="hljs-comment">// Necessary only if the server's cert isn't for "localhost".</span>
60555 …ss="hljs-attr">checkServerIdentity</span>: <span class="hljs-function">() =></span> { <span class=…
60558-keyword">const</span> socket = tls.<span class="hljs-title function_">connect</span>(<span class=…
60559 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>…
60560 …socket.<span class="hljs-property">authorized</span> ? <span class="hljs-string">'authorized'</spa…
60561 …process.<span class="hljs-property">stdin</span>.<span class="hljs-title function_">pipe</span>(so…
60562 …process.<span class="hljs-property">stdin</span>.<span class="hljs-title function_">resume</span>(…
60564 socket.<span class="hljs-title function_">setEncoding</span>(<span class="hljs-string">'utf8'</span…
60565 ….<span class="hljs-title function_">on</span>(<span class="hljs-string">'data'</span>, <span class…
60566 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>…
60568 socket.<span class="hljs-title function_">on</span>(<span class="hljs-string">'end'</span>, <span c…
60569 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>…
60576 <li><code>path</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
60577 <li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference…
60578 <li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Referenc…
60589 <li><code>port</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
60590 <li><code>host</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
60591 <li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference…
60592 <li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Referenc…
60630 <li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference…
60632 …zilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | …
60633 certificates. Default is to trust the well-known CAs curated by Mozilla.
60639 certificates that are not chainable to a well-known CA, the certificate's CA
60645 For self-signed certificates, the certificate is its own CA, and must be
60650 …zilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | …
60654 including the root CA (the root CA must be pre-known to the peer, see <code>ca</code>).
60659 …ttps://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&…
60661 algorithms (<code>RSA-PSS</code>, <code>ECDSA</code> etc.), combination of both (e.g
60665 <li><code>ciphers</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_stru…
60669 <li><code>clientCertEngine</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/…
60671 …zilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | …
60673-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | <a href="#bu…
60679 <li><code>ecdhCurve</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_st…
60680 list of curve NIDs or names, for example <code>P-521:P-384:P-256</code>, to use for
60683 available curve names. On recent releases, <code>openssl ecparam -list_curves</code>
60686 <li><code>honorCipherOrder</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/…
60690-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | <a href="htt…
60699 <li><code>privateKeyEngine</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/…
60701 <li><code>privateKeyIdentifier</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScr…
60705 <li><code>maxVersion</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_s…
60709 <li><code>minVersion</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_s…
60715 <li><code>passphrase</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_s…
60717-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | <a href="htt…
60726 <li><code>secureOptions</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Dat…
60730 <li><code>secureProtocol</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Da…
60734 <a href="https://www.openssl.org/docs/man1.1.1/man7/ssl.html#Dealing-with-Protocol-Methods">SSL_MET…
60739 <li><code>sessionIdContext</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/…
60741 …#buffer_class_buffer" class="type">&#x3C;Buffer></a> 48-bytes of cryptographically strong pseudo-r…
60743 <li><code>sessionTimeout</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Da…
60760 <a href="https://hg.mozilla.org/mozilla-central/raw-file/tip/security/nss/lib/ckfw/builtins/certdat…
60775 … api_stability_0"><a href="#documentation_stability_index">Stability: 0</a> - Deprecated: Use <a h…
60777 <li><code>context</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference…
60779 <li><code>isServer</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_str…
60781 <li><code>requestCert</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_…
60783 <li><code>rejectUnauthorized</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScrip…
60789 <li><code>isServer</code>: If <code>true</code> the TLS socket will be instantiated in server-mode.
60797 <li><code>requestOCSP</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_…
60813 <pre><code class="language-js">pair = tls.<span class="hljs-title function_">createSecurePair</span…
60814 pair.<span class="hljs-property">encrypted</span>.<span class="hljs-title function_">pipe</span>(so…
60815 socket.<span class="hljs-title function_">pipe</span>(pair.<span class="hljs-property">encrypted</s…
60817 <pre><code class="language-js">secureSocket = tls.<span class="hljs-title function_">TLSSocket</spa…
60838 <li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference…
60840-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string[]></a> | <a href="#…
60847 <li><code>clientCertEngine</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/…
60849 <li><code>enableTrace</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_…
60853 <li><code>handshakeTimeout</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/…
60857 <li><code>rejectUnauthorized</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScrip…
60860 <li><code>requestCert</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_…
60863 <li><code>sessionTimeout</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Da…
60866 <li><code>SNICallback(servername, callback)</code> <a href="https://developer.mozilla.org/en-US/doc…
60869 error-first callback that takes two optional arguments: <code>error</code> and <code>ctx</code>.
60874 default callback with high-level API will be used (see below).</li>
60875 …#buffer_class_buffer" class="type">&#x3C;Buffer></a> 48-bytes of cryptographically strong pseudo-r…
60877 <li><code>pskCallback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Refer…
60881 <li>identity: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Stri…
60882-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray" class="type">&#x3C;TypedArray></a> | …
60886 When negotiating TLS-PSK (pre-shared keys), this function is called
60893 PSK ciphers are disabled by default, and using TLS-PSK thus
60896 <li><code>pskIdentityHint</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/D…
60897 with selecting the identity during TLS-PSK negotiation. Will be ignored
60906 <li><code>secureConnectionListener</code> <a href="https://developer.mozilla.org/en-US/docs/Web/Jav…
60914 …code class="language-js"><span class="hljs-keyword">const</span> tls = <span class="hljs-built_in"…
60915 <span class="hljs-keyword">const</span> fs = <span class="hljs-built_in">require</span>(<span class…
60917 <span class="hljs-keyword">const</span> options = {
60918 …<span class="hljs-attr">key</span>: fs.<span class="hljs-title function_">readFileSync</span>(<spa…
60919 …<span class="hljs-attr">cert</span>: fs.<span class="hljs-title function_">readFileSync</span>(<sp…
60921 …<span class="hljs-comment">// This is necessary only if using client certificate authentication.</…
60922 <span class="hljs-attr">requestCert</span>: <span class="hljs-literal">true</span>,
60924 …<span class="hljs-comment">// This is necessary only if the client uses a self-signed certificate.…
60925 …<span class="hljs-attr">ca</span>: [ fs.<span class="hljs-title function_">readFileSync</span>(<sp…
60928 …ljs-keyword">const</span> server = tls.<span class="hljs-title function_">createServer</span>(opti…
60929 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>…
60930 …socket.<span class="hljs-property">authorized</span> ? <span class="hljs-string">'authorized'</spa…
60931 …socket.<span class="hljs-title function_">write</span>(<span class="hljs-string">'welcome!\n'</spa…
60932 …socket.<span class="hljs-title function_">setEncoding</span>(<span class="hljs-string">'utf8'</spa…
60933 socket.<span class="hljs-title function_">pipe</span>(socket);
60935 server.<span class="hljs-title function_">listen</span>(<span class="hljs-number">8000</span>, <spa…
60936 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>…
60945 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Strin…
60948 lower-case for historical reasons, but must be uppercased to be used in
60952-js"><span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</…
60958 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" c…
60984 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" c…
60988 <code>--tls-max-v1.2</code> sets the default to <code>'TLSv1.2'</code>. Using <code>--tls-max-v1.3<…
60997 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" c…
61001 <code>--tls-min-v1.0</code> sets the default to <code>'TLSv1'</code>. Using <code>--tls-min-v1.1</c…
61002 the default to <code>'TLSv1.1'</code>. Using <code>--tls-min-v1.3</code> sets the default to
61009 … api_stability_1"><a href="#documentation_stability_index">Stability: 1</a> - Experimental</div><p…
61013 <p>Tracing can be enabled with the <code>--trace-event-categories</code> command-line flag
61014 or by using the <code>trace_events</code> module. The <code>--trace-event-categories</code> flag
61015 accepts a list of comma-separated category names.</p>
61037 of unhandled Promise rejections and handled-after-rejections.</li>
61043 <pre><code class="language-bash">node --trace-event-categories v8,node,node.async_hooks server.js</…
61044 <p>Prior versions of Node.js required the use of the <code>--trace-events-enabled</code>
61046 <code>--trace-events-enabled</code> flag <em>may</em> still be used and will enable the
61048 <pre><code class="language-bash">node --trace-events-enabled
61050 <span class="hljs-comment"># is equivalent to</span>
61052 node --trace-event-categories v8,node,node.async_hooks</code></pre>
61054 … class="language-js"><span class="hljs-keyword">const</span> trace_events = <span class="hljs-buil…
61055-keyword">const</span> tracing = trace_events.<span class="hljs-title function_">createTracing</sp…
61056 tracing.<span class="hljs-title function_">enable</span>(); <span class="hljs-comment">// Enable t…
61058 <span class="hljs-comment">// do work</span>
61060 tracing.<span class="hljs-title function_">disable</span>(); <span class="hljs-comment">// Disable…
61062 in the <a href="https://www.chromium.org/developers/how-tos/trace-event-profiling-tool"><code>chrom…
61065 <code>${rotation}</code> is an incrementing log-rotation id. The filepath pattern can
61066 be specified with <code>--trace-event-file-pattern</code> that accepts a template
61068 <pre><code class="language-bash">node --trace-event-categories v8 --trace-event-file-pattern <span …
61071 However the trace-event timestamps are expressed in microseconds,
61094 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" c…
61096 <p>A comma-separated list of the trace event categories covered by this
61104 and <em>not</em> specified by the <code>--trace-event-categories</code> flag will be disabled.</p>
61105 … class="language-js"><span class="hljs-keyword">const</span> trace_events = <span class="hljs-buil…
61106-keyword">const</span> t1 = trace_events.<span class="hljs-title function_">createTracing</span>({…
61107-keyword">const</span> t2 = trace_events.<span class="hljs-title function_">createTracing</span>({…
61108 t1.<span class="hljs-title function_">enable</span>();
61109 t2.<span class="hljs-title function_">enable</span>();
61111 <span class="hljs-comment">// Prints 'node,node.perf,v8'</span>
61112 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(…
61114 t2.<span class="hljs-title function_">disable</span>(); <span class="hljs-comment">// Will only dis…
61116 <span class="hljs-comment">// Prints 'node,v8'</span>
61117 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(…
61129 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" …
61136 <li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference…
61138 <li><code>categories</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_s…
61146 … class="language-js"><span class="hljs-keyword">const</span> trace_events = <span class="hljs-buil…
61147 <span class="hljs-keyword">const</span> categories = [<span class="hljs-string">'node.perf'</span>,…
61148 <span class="hljs-keyword">const</span> tracing = trace_events.<span class="hljs-title function_">c…
61149 tracing.<span class="hljs-title function_">enable</span>();
61150 <span class="hljs-comment">// do stuff</span>
61151 tracing.<span class="hljs-title function_">disable</span>();</code></pre>
61157 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Strin…
61159 <p>Returns a comma-separated list of all currently-enabled trace event
61161 by the <em>union</em> of all currently-enabled <code>Tracing</code> objects and any categories
61162 enabled using the <code>--trace-event-categories</code> flag.</p>
61164 <code>node --trace-event-categories node.perf test.js</code> will print
61166 … class="language-js"><span class="hljs-keyword">const</span> trace_events = <span class="hljs-buil…
61167-keyword">const</span> t1 = trace_events.<span class="hljs-title function_">createTracing</span>({…
61168-keyword">const</span> t2 = trace_events.<span class="hljs-title function_">createTracing</span>({…
61169-keyword">const</span> t3 = trace_events.<span class="hljs-title function_">createTracing</span>({…
61171 t1.<span class="hljs-title function_">enable</span>();
61172 t2.<span class="hljs-title function_">enable</span>();
61174 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(…
61178 …ty api_stability_2"><a href="#documentation_stability_index">Stability: 2</a> - Stable</div><p></p>
61183 …code class="language-js"><span class="hljs-keyword">const</span> tty = <span class="hljs-built_in"…
61190 <pre><code class="language-console"><span class="hljs-meta">$ </span><span class="language-bash">no…
61192 <span class="hljs-meta">$ </span><span class="language-bash">node -p -e <span class="hljs-string">"…
61223 <li><code>mode</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
61227 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/t…
61230 <p>When in raw mode, input is always available character-by-character, not
61252-js">process.<span class="hljs-property">stdout</span>.<span class="hljs-title function_">on</span…
61253 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>…
61254-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hlj…
61269 <li><code>dir</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structur…
61271 <li><code>-1</code>: to the left from cursor</li>
61276 <li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Referenc…
61277 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boole…
61296 <li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Referenc…
61297 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boole…
61322 <li><code>x</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures…
61323 <li><code>y</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures…
61324 <li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Referenc…
61325 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boole…
61336 <li><code>env</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Glo…
61339 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Numbe…
61369 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Numbe…
61380 <li><code>count</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struct…
61382 <li><code>env</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Glo…
61385 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boole…
61391 <pre><code class="language-js">process.<span class="hljs-property">stdout</span>.<span class="hljs-
61392 <span class="hljs-comment">// Returns true or false depending on if `stdout` supports at least 16 c…
61393 process.<span class="hljs-property">stdout</span>.<span class="hljs-title function_">hasColors</spa…
61394 <span class="hljs-comment">// Returns true or false depending on if `stdout` supports at least 256 …
61395 …ass="hljs-property">stdout</span>.<span class="hljs-title function_">hasColors</span>({ <span clas…
61396 <span class="hljs-comment">// Returns true.</span>
61397-property">stdout</span>.<span class="hljs-title function_">hasColors</span>(<span class="hljs-num…
61398 <span class="hljs-comment">// Returns false (the environment setting pretends to support 2 ** 8 col…
61417 <li><code>dx</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structure…
61418 <li><code>dy</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structure…
61419 <li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Referenc…
61420 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boole…
61437 <li><code>fd</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structure…
61438 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boole…
61441 a TTY and <code>false</code> if it is not, including whenever <code>fd</code> is not a non-negative
61446 …ty api_stability_2"><a href="#documentation_stability_index">Stability: 2</a> - Stable</div><p></p>
61450 …ode class="language-js"><span class="hljs-keyword">const</span> dgram = <span class="hljs-built_in…
61451 <span class="hljs-keyword">const</span> server = dgram.<span class="hljs-title function_">createSoc…
61453 ….<span class="hljs-title function_">on</span>(<span class="hljs-string">'error'</span>, <span clas…
61454 …"hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class…
61455 server.<span class="hljs-title function_">close</span>();
61458 …<span class="hljs-title function_">on</span>(<span class="hljs-string">'message'</span>, <span cla…
61459-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hlj…
61462 server.<span class="hljs-title function_">on</span>(<span class="hljs-string">'listening'</span>, <…
61463 …<span class="hljs-keyword">const</span> address = server.<span class="hljs-title function_">addres…
61464-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hlj…
61467 server.<span class="hljs-title function_">bind</span>(<span class="hljs-number">41234</span>);
61468 <span class="hljs-comment">// Prints: server listening 0.0.0.0:41234</span></code></pre>
61496 <li><code>exception</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Referen…
61517 <li><code>rinfo</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/G…
61519 <li><code>address</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_stru…
61520 ….org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> The add…
61521 <li><code>port</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
61522 <li><code>size</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
61526 <p>If the source address of the incoming packet is an IPv6 link-local
61536 <li><code>multicastAddress</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/…
61537 <li><code>multicastInterface</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScrip…
61549 …de class="language-js"><span class="hljs-keyword">const</span> cluster = <span class="hljs-built_i…
61550 <span class="hljs-keyword">const</span> dgram = <span class="hljs-built_in">require</span>(<span cl…
61551 <span class="hljs-keyword">if</span> (cluster.<span class="hljs-property">isMaster</span>) {
61552 …cluster.<span class="hljs-title function_">fork</span>(); <span class="hljs-comment">// Works ok.<…
61553 …cluster.<span class="hljs-title function_">fork</span>(); <span class="hljs-comment">// Fails with…
61554 } <span class="hljs-keyword">else</span> {
61555 …<span class="hljs-keyword">const</span> s = dgram.<span class="hljs-title function_">createSocket<…
61556 …s.<span class="hljs-title function_">bind</span>(<span class="hljs-number">1234</span>, <span clas…
61557 …s.<span class="hljs-title function_">addMembership</span>(<span class="hljs-string">'224.0.0.114'<…
61565 <li><code>sourceAddress</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Dat…
61566 <li><code>groupAddress</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data…
61567 <li><code>multicastInterface</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScrip…
61569 <p>Tells the kernel to join a source-specific multicast channel at the given
61582 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Obje…
61601 <li><code>port</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
61602 <li><code>address</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_stru…
61603 <li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Referenc…
61620 …ode class="language-js"><span class="hljs-keyword">const</span> dgram = <span class="hljs-built_in…
61621 <span class="hljs-keyword">const</span> server = dgram.<span class="hljs-title function_">createSoc…
61623 ….<span class="hljs-title function_">on</span>(<span class="hljs-string">'error'</span>, <span clas…
61624 …"hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class…
61625 server.<span class="hljs-title function_">close</span>();
61628 …<span class="hljs-title function_">on</span>(<span class="hljs-string">'message'</span>, <span cla…
61629-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hlj…
61632 server.<span class="hljs-title function_">on</span>(<span class="hljs-string">'listening'</span>, <…
61633 …<span class="hljs-keyword">const</span> address = server.<span class="hljs-title function_">addres…
61634-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hlj…
61637 server.<span class="hljs-title function_">bind</span>(<span class="hljs-number">41234</span>);
61638 <span class="hljs-comment">// Prints: server listening 0.0.0.0:41234</span></code></pre>
61644 <li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference…
61646 <li><code>port</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
61647 <li><code>address</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_stru…
61648 <li><code>exclusive</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_st…
61649 <li><code>fd</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structure…
61652 <li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Referenc…
61680 <pre><code class="language-js">socket.<span class="hljs-title function_">bind</span>({
61681 <span class="hljs-attr">address</span>: <span class="hljs-string">'localhost'</span>,
61682 <span class="hljs-attr">port</span>: <span class="hljs-number">8000</span>,
61683 <span class="hljs-attr">exclusive</span>: <span class="hljs-literal">true</span>
61690 <li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Referenc…
61699 <li><code>port</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
61700 <li><code>address</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_stru…
61701 <li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Referenc…
61725 <li><code>multicastAddress</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/…
61726 <li><code>multicastInterface</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScrip…
61739 <li><code>sourceAddress</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Dat…
61740 <li><code>groupAddress</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data…
61741 <li><code>multicastInterface</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScrip…
61743 <p>Instructs the kernel to leave a source-specific multicast channel at the given
61755 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Numbe…
61763 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Numbe…
61786 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Obje…
61814-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray" class="type">&#x3C;TypedArray></a> | …
61815 <li><code>offset</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struc…
61816 <li><code>length</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struc…
61817 <li><code>port</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
61818 <li><code>address</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_stru…
61819 <li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Referenc…
61832 contain multi-byte characters, <code>offset</code> and <code>length</code> will be calculated with
61855 …ode class="language-js"><span class="hljs-keyword">const</span> dgram = <span class="hljs-built_in…
61856 …hljs-keyword">const</span> message = <span class="hljs-title class_">Buffer</span>.<span class="hl…
61857 <span class="hljs-keyword">const</span> client = dgram.<span class="hljs-title function_">createSoc…
61858-title function_">send</span>(message, <span class="hljs-number">41234</span>, <span class="hljs-s…
61859 client.<span class="hljs-title function_">close</span>();
61863 …ode class="language-js"><span class="hljs-keyword">const</span> dgram = <span class="hljs-built_in…
61864 …="hljs-keyword">const</span> buf1 = <span class="hljs-title class_">Buffer</span>.<span class="hlj…
61865 …="hljs-keyword">const</span> buf2 = <span class="hljs-title class_">Buffer</span>.<span class="hlj…
61866 <span class="hljs-keyword">const</span> client = dgram.<span class="hljs-title function_">createSoc…
61867 …class="hljs-title function_">send</span>([buf1, buf2], <span class="hljs-number">41234</span>, <sp…
61868 client.<span class="hljs-title function_">close</span>();
61872 determine the optimal strategy on a case-by-case basis. Generally speaking,
61876 …ode class="language-js"><span class="hljs-keyword">const</span> dgram = <span class="hljs-built_in…
61877 …hljs-keyword">const</span> message = <span class="hljs-title class_">Buffer</span>.<span class="hl…
61878 <span class="hljs-keyword">const</span> client = dgram.<span class="hljs-title function_">createSoc…
61879-title function_">connect</span>(<span class="hljs-number">41234</span>, <span class="hljs-string"…
61880 …client.<span class="hljs-title function_">send</span>(message, <span class="hljs-function">(<span …
61881 client.<span class="hljs-title function_">close</span>();
61899 as the <code>MTU</code> for dial-up type applications), whether they arrive whole or in
61901 <p>For IPv6, the minimum <code>MTU</code> is 1280 octets. However, the mandatory minimum
61916 <li><code>flag</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
61926 <li><code>multicastInterface</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScrip…
61929 <a href="https://en.wikipedia.org/wiki/IPv6_address#Scoped_literal_IPv6_addresses">IPv6 Zone Indice…
61938 <p>For IPv6 sockets, <code>multicastInterface</code> should include a scope to indicate the
61939 interface as in the examples that follow. In IPv6, individual <code>send</code> calls can
61944 <h5>Example: IPv6 outgoing multicast interface<span><a class="mark" href="#dgram_example_ipv6_outgo…
61946 …="language-js"><span class="hljs-keyword">const</span> socket = dgram.<span class="hljs-title func…
61948 socket.<span class="hljs-title function_">bind</span>(<span class="hljs-number">1234</span>, <span …
61949 …socket.<span class="hljs-title function_">setMulticastInterface</span>(<span class="hljs-string">'…
61952 …="language-js"><span class="hljs-keyword">const</span> socket = dgram.<span class="hljs-title func…
61954 socket.<span class="hljs-title function_">bind</span>(<span class="hljs-number">1234</span>, <span …
61955 …socket.<span class="hljs-title function_">setMulticastInterface</span>(<span class="hljs-string">'…
61959 …="language-js"><span class="hljs-keyword">const</span> socket = dgram.<span class="hljs-title func…
61961 socket.<span class="hljs-title function_">bind</span>(<span class="hljs-number">1234</span>, <span …
61962 …socket.<span class="hljs-title function_">setMulticastInterface</span>(<span class="hljs-string">'…
61972 <p>On IPv6, most errors with specifying or omitting scope will result in the socket
61974 <p>A socket's address family's ANY address (IPv4 <code>'0.0.0.0'</code> or IPv6 <code>'::'</code>) …
61982 <li><code>flag</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
61992 <li><code>ttl</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structur…
62006 <li><code>size</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
62016 <li><code>size</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
62026 <li><code>ttl</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structur…
62071 <li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference…
62073 <li><code>type</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
62075 <li><code>reuseAddr</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_st…
62078 <li><code>ipv6Only</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_str…
62079 disable dual-stack support, i.e., binding to address <code>::</code> won't make
62081 <li><code>recvBufferSize</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Da…
62082 <li><code>sendBufferSize</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Da…
62083 <li><code>lookup</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/…
62087 <li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Referenc…
62099 …ode class="language-js"><span class="hljs-keyword">const</span> controller = <span class="hljs-key…
62100 <span class="hljs-keyword">const</span> { signal } = controller;
62101 …s-keyword">const</span> server = dgram.<span class="hljs-title function_">createSocket</span>({ <s…
62102 …<span class="hljs-title function_">on</span>(<span class="hljs-string">'message'</span>, <span cla…
62103-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hlj…
62105 <span class="hljs-comment">// Later, when you want to close the server.</span>
62106 controller.<span class="hljs-title function_">abort</span>();</code></pre>
62112 <li><code>type</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
62113 <li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Referenc…
62126 …ty api_stability_2"><a href="#documentation_stability_index">Stability: 2</a> - Stable</div><p></p>
62130 …code class="language-js"><span class="hljs-keyword">const</span> url = <span class="hljs-built_in"…
62144 <pre><code class="language-text">┌─────────────────────────────────────────────────────────────────…
62163 <pre><code class="language-js"><span class="hljs-keyword">const</span> myURL =
62164 …<span class="hljs-keyword">new</span> <span class="hljs-title function_">URL</span>(<span class="h…
62166 …code class="language-js"><span class="hljs-keyword">const</span> url = <span class="hljs-built_in"…
62167 <span class="hljs-keyword">const</span> myURL =
62168 …url.<span class="hljs-title function_">parse</span>(<span class="hljs-string">'https://user:pass@s…
62172-js"><span class="hljs-keyword">const</span> myURL = <span class="hljs-keyword">new</span> <span c…
62173 myURL.<span class="hljs-property">pathname</span> = <span class="hljs-string">'/a/b/c'</span>;
62174 myURL.<span class="hljs-property">search</span> = <span class="hljs-string">'?d=e'</span>;
62175 myURL.<span class="hljs-property">hash</span> = <span class="hljs-string">'#fgh'</span>;</code></pr…
62176 <pre><code class="language-js"><span class="hljs-keyword">const</span> pathname = <span class="hljs
62177 <span class="hljs-keyword">const</span> search = <span class="hljs-string">'?d=e'</span>;
62178 <span class="hljs-keyword">const</span> hash = <span class="hljs-string">'#fgh'</span>;
62179-keyword">const</span> myURL = <span class="hljs-keyword">new</span> <span class="hljs-title funct…
62181 …ss="language-js"><span class="hljs-variable language_">console</span>.<span class="hljs-title func…
62195 <p>Browser-compatible <code>URL</code> class, implemented by following the WHATWG URL
62196 Standard. <a href="https://url.spec.whatwg.org/#example-url-parsing">Examples of parsed URLs</a> ma…
62205 <li><code>input</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struct…
62208 <li><code>base</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
62213-js"><span class="hljs-keyword">const</span> myURL = <span class="hljs-keyword">new</span> <span c…
62214 <span class="hljs-comment">// https://example.org/foo</span></code></pre>
62216 It can also be imported from the built-in url module:</p>
62217-js"><span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</…
62221-js"><span class="hljs-keyword">const</span> myURL = <span class="hljs-keyword">new</span> <span c…
62222 <span class="hljs-comment">// https://example.org/</span></code></pre>
62224 automatically converted to ASCII using the <a href="https://tools.ietf.org/html/rfc5891#section-4.4…
62225-js"><span class="hljs-keyword">const</span> myURL = <span class="hljs-keyword">new</span> <span c…
62226 <span class="hljs-comment">// https://xn--g6w251d/</span></code></pre>
62232-js"><span class="hljs-keyword">let</span> myURL = <span class="hljs-keyword">new</span> <span cla…
62233 <span class="hljs-comment">// http://example.com/</span>
62235 …"hljs-keyword">new</span> <span class="hljs-title function_">URL</span>(<span class="hljs-string">…
62236 <span class="hljs-comment">// https://example.com/</span>
62238 …="hljs-keyword">new</span> <span class="hljs-title function_">URL</span>(<span class="hljs-string"…
62239 <span class="hljs-comment">// foo://Example.com/</span>
62241 …s="hljs-keyword">new</span> <span class="hljs-title function_">URL</span>(<span class="hljs-string…
62242 <span class="hljs-comment">// http://example.com/</span>
62244 …="hljs-keyword">new</span> <span class="hljs-title function_">URL</span>(<span class="hljs-string"…
62245 <span class="hljs-comment">// https://example.org/Example.com/</span>
62247 …s="hljs-keyword">new</span> <span class="hljs-title function_">URL</span>(<span class="hljs-string…
62248 <span class="hljs-comment">// foo:Example.com/</span></code></pre>
62251 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" c…
62254-js"><span class="hljs-keyword">const</span> myURL = <span class="hljs-keyword">new</span> <span c…
62255 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(…
62256 <span class="hljs-comment">// Prints #bar</span>
62258 myURL.<span class="hljs-property">hash</span> = <span class="hljs-string">'baz'</span>;
62259 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(…
62260 <span class="hljs-comment">// Prints https://example.org/foo#baz</span></code></pre>
62262 are <a href="#whatwg-percent-encoding">percent-encoded</a>. The selection of which characters to
62263 percent-encode may vary somewhat from what the <a href="#url_url_parse_urlstring_parsequerystring_s…
62267 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" c…
62270-js"><span class="hljs-keyword">const</span> myURL = <span class="hljs-keyword">new</span> <span c…
62271 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(…
62272 <span class="hljs-comment">// Prints example.org:81</span>
62274 myURL.<span class="hljs-property">host</span> = <span class="hljs-string">'example.com:82'</span>;
62275 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(…
62276 <span class="hljs-comment">// Prints https://example.com:82/foo</span></code></pre>
62280 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" c…
62285-js"><span class="hljs-keyword">const</span> myURL = <span class="hljs-keyword">new</span> <span c…
62286 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(…
62287 <span class="hljs-comment">// Prints example.org</span>
62289 <span class="hljs-comment">// Setting the hostname does not change the port</span>
62290 myURL.<span class="hljs-property">hostname</span> = <span class="hljs-string">'example.com:82'</spa…
62291 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(…
62292 <span class="hljs-comment">// Prints https://example.com:81/foo</span>
62294 <span class="hljs-comment">// Use myURL.host to change the hostname and port</span>
62295 myURL.<span class="hljs-property">host</span> = <span class="hljs-string">'example.org:82'</span>;
62296 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(…
62297 <span class="hljs-comment">// Prints https://example.org:82/foo</span></code></pre>
62301 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" c…
62304-js"><span class="hljs-keyword">const</span> myURL = <span class="hljs-keyword">new</span> <span c…
62305 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(…
62306 <span class="hljs-comment">// Prints https://example.org/foo</span>
62308 myURL.<span class="hljs-property">href</span> = <span class="hljs-string">'https://example.com/bar'…
62309 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(…
62310 <span class="hljs-comment">// Prints https://example.com/bar</span></code></pre>
62320 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" c…
62322 <p>Gets the read-only serialization of the URL's origin.</p>
62323-js"><span class="hljs-keyword">const</span> myURL = <span class="hljs-keyword">new</span> <span c…
62324 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(…
62325 <span class="hljs-comment">// Prints https://example.org</span></code></pre>
62326-js"><span class="hljs-keyword">const</span> idnURL = <span class="hljs-keyword">new</span> <span …
62327 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(…
62328 <span class="hljs-comment">// Prints https://xn--g6w251d</span>
62330 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(…
62331 <span class="hljs-comment">// Prints xn--g6w251d</span></code></pre>
62334 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" c…
62337-js"><span class="hljs-keyword">const</span> myURL = <span class="hljs-keyword">new</span> <span c…
62338 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(…
62339 <span class="hljs-comment">// Prints xyz</span>
62341 myURL.<span class="hljs-property">password</span> = <span class="hljs-string">'123'</span>;
62342 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(…
62343 <span class="hljs-comment">// Prints https://abc:123@example.com</span></code></pre>
62345 are <a href="#whatwg-percent-encoding">percent-encoded</a>. The selection of which characters to
62346 percent-encode may vary somewhat from what the <a href="#url_url_parse_urlstring_parsequerystring_s…
62350 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" c…
62353-js"><span class="hljs-keyword">const</span> myURL = <span class="hljs-keyword">new</span> <span c…
62354 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(…
62355 <span class="hljs-comment">// Prints /abc/xyz</span>
62357 myURL.<span class="hljs-property">pathname</span> = <span class="hljs-string">'/abcdef'</span>;
62358 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(…
62359 <span class="hljs-comment">// Prints https://example.org/abcdef?123</span></code></pre>
62361 property are <a href="#whatwg-percent-encoding">percent-encoded</a>. The selection of which charact…
62362 to percent-encode may vary somewhat from what the <a href="#url_url_parse_urlstring_parsequerystrin…
62366 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" c…
62418-js"><span class="hljs-keyword">const</span> myURL = <span class="hljs-keyword">new</span> <span c…
62419 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(…
62420 <span class="hljs-comment">// Prints 8888</span>
62422 <span class="hljs-comment">// Default ports are automatically transformed to the empty string</span>
62423 <span class="hljs-comment">// (HTTPS protocol's default port is 443)</span>
62424 myURL.<span class="hljs-property">port</span> = <span class="hljs-string">'443'</span>;
62425 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(…
62426 <span class="hljs-comment">// Prints the empty string</span>
62427 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(…
62428 <span class="hljs-comment">// Prints https://example.org/</span>
62430 myURL.<span class="hljs-property">port</span> = <span class="hljs-number">1234</span>;
62431 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(…
62432 <span class="hljs-comment">// Prints 1234</span>
62433 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(…
62434 <span class="hljs-comment">// Prints https://example.org:1234/</span>
62436 <span class="hljs-comment">// Completely invalid port strings are ignored</span>
62437 myURL.<span class="hljs-property">port</span> = <span class="hljs-string">'abcd'</span>;
62438 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(…
62439 <span class="hljs-comment">// Prints 1234</span>
62441 <span class="hljs-comment">// Leading numbers are treated as a port number</span>
62442 myURL.<span class="hljs-property">port</span> = <span class="hljs-string">'5678abcd'</span>;
62443 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(…
62444 <span class="hljs-comment">// Prints 5678</span>
62446 <span class="hljs-comment">// Non-integers are truncated</span>
62447 myURL.<span class="hljs-property">port</span> = <span class="hljs-number">1234.5678</span>;
62448 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(…
62449 <span class="hljs-comment">// Prints 1234</span>
62451 <span class="hljs-comment">// Out-of-range numbers which are not represented in scientific notation…
62452 <span class="hljs-comment">// will be ignored.</span>
62453 …span class="hljs-property">port</span> = <span class="hljs-number">1e10</span>; <span class="hljs-
62454 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(…
62455 <span class="hljs-comment">// Prints 1234</span></code></pre>
62457 such as floating-point numbers or numbers in scientific notation,
62461 <pre><code class="language-js">myURL.<span class="hljs-property">port</span> = <span class="hljs-nu…
62462 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(…
62463 <span class="hljs-comment">// Prints 4 (because it is the leading number in the string '4.567e21')<…
62466 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" c…
62469-js"><span class="hljs-keyword">const</span> myURL = <span class="hljs-keyword">new</span> <span c…
62470 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(…
62471 <span class="hljs-comment">// Prints https:</span>
62473 myURL.<span class="hljs-property">protocol</span> = <span class="hljs-string">'ftp'</span>;
62474 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(…
62475 <span class="hljs-comment">// Prints ftp://example.org/</span></code></pre>
62482 non-special protocol, and vice versa.</p>
62484 …e-js"><span class="hljs-keyword">const</span> u = <span class="hljs-keyword">new</span> <span clas…
62485 u.<span class="hljs-property">protocol</span> = <span class="hljs-string">'https'</span>;
62486 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(…
62487 <span class="hljs-comment">// https://example.org</span></code></pre>
62490 …e-js"><span class="hljs-keyword">const</span> u = <span class="hljs-keyword">new</span> <span clas…
62491 u.<span class="hljs-property">protocol</span> = <span class="hljs-string">'fish'</span>;
62492 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(…
62493 <span class="hljs-comment">// http://example.org</span></code></pre>
62494 <p>Likewise, changing from a non-special protocol to a special protocol is also
62496 …e-js"><span class="hljs-keyword">const</span> u = <span class="hljs-keyword">new</span> <span clas…
62497 u.<span class="hljs-property">protocol</span> = <span class="hljs-string">'http'</span>;
62498 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(…
62499 <span class="hljs-comment">// fish://example.org</span></code></pre>
62504 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" c…
62507-js"><span class="hljs-keyword">const</span> myURL = <span class="hljs-keyword">new</span> <span c…
62508 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(…
62509 <span class="hljs-comment">// Prints ?123</span>
62511 myURL.<span class="hljs-property">search</span> = <span class="hljs-string">'abc=xyz'</span>;
62512 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(…
62513 <span class="hljs-comment">// Prints https://example.org/abc?abc=xyz</span></code></pre>
62515 property will be <a href="#whatwg-percent-encoding">percent-encoded</a>. The selection of which
62516 characters to percent-encode may vary somewhat from what the <a href="#url_url_parse_urlstring_pars…
62523 URL. This property is read-only but the <code>URLSearchParams</code> object it provides
62529 different rules to determine which characters to percent-encode. For
62532-js"><span class="hljs-keyword">const</span> myUrl = <span class="hljs-keyword">new</span> <span c…
62534 …s-variable language_">console</span>.<span class="hljs-title function_">log</span>(myUrl.<span cla…
62536 <span class="hljs-comment">// Modify the URL via searchParams...</span>
62537 myUrl.<span class="hljs-property">searchParams</span>.<span class="hljs-title function_">sort</span…
62539 …s-variable language_">console</span>.<span class="hljs-title function_">log</span>(myUrl.<span cla…
62542 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" c…
62545-js"><span class="hljs-keyword">const</span> myURL = <span class="hljs-keyword">new</span> <span c…
62546 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(…
62547 <span class="hljs-comment">// Prints abc</span>
62549 myURL.<span class="hljs-property">username</span> = <span class="hljs-string">'123'</span>;
62550 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(…
62551 <span class="hljs-comment">// Prints https://123:xyz@example.com/</span></code></pre>
62553 property will be <a href="#whatwg-percent-encoding">percent-encoded</a>. The selection of which
62554 characters to percent-encode may vary somewhat from what the <a href="#url_url_parse_urlstring_pars…
62558 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Strin…
62567 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Strin…
62573 with <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON…
62574 <pre><code class="language-js"><span class="hljs-keyword">const</span> myURLs = [
62575 …<span class="hljs-keyword">new</span> <span class="hljs-title function_">URL</span>(<span class="h…
62576 …<span class="hljs-keyword">new</span> <span class="hljs-title function_">URL</span>(<span class="h…
62578 …s-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hl…
62579 <span class="hljs-comment">// Prints ["https://www.example.com/","https://test.example.org/"]</span…
62600-js"><span class="hljs-keyword">const</span> myURL = <span class="hljs-keyword">new</span> <span c…
62601-variable language_">console</span>.<span class="hljs-title function_">log</span>(myURL.<span clas…
62602 <span class="hljs-comment">// Prints 123</span>
62604 …s="hljs-property">searchParams</span>.<span class="hljs-title function_">append</span>(<span class…
62605 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(…
62606 <span class="hljs-comment">// Prints https://example.org/?abc=123&#x26;abc=xyz</span>
62608 myURL.<span class="hljs-property">searchParams</span>.<span class="hljs-title function_">delete</sp…
62609 …lass="hljs-property">searchParams</span>.<span class="hljs-title function_">set</span>(<span class…
62610 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(…
62611 <span class="hljs-comment">// Prints https://example.org/?a=b</span>
62613-keyword">const</span> newSearchParams = <span class="hljs-keyword">new</span> <span class="hljs-t…
62614 <span class="hljs-comment">// The above is equivalent to</span>
62615 <span class="hljs-comment">// const newSearchParams = new URLSearchParams(myURL.search);</span>
62617 …ewSearchParams.<span class="hljs-title function_">append</span>(<span class="hljs-string">'a'</spa…
62618 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(…
62619 <span class="hljs-comment">// Prints https://example.org/?a=b</span>
62620 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(…
62621 <span class="hljs-comment">// Prints a=b&#x26;a=c</span>
62623 <span class="hljs-comment">// newSearchParams.toString() is implicitly called</span>
62624 myURL.<span class="hljs-property">search</span> = newSearchParams;
62625 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(…
62626 <span class="hljs-comment">// Prints https://example.org/?a=b&#x26;a=c</span>
62627 newSearchParams.<span class="hljs-title function_">delete</span>(<span class="hljs-string">'a'</spa…
62628 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(…
62629 <span class="hljs-comment">// Prints https://example.org/?a=b&#x26;a=c</span></code></pre>
62634 <li><code>string</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struc…
62638 <pre><code class="language-js"><span class="hljs-keyword">let</span> params;
62640 params = <span class="hljs-keyword">new</span> <span class="hljs-title function_">URLSearchParams</…
62641-variable language_">console</span>.<span class="hljs-title function_">log</span>(params.<span cla…
62642 <span class="hljs-comment">// Prints 'abc'</span>
62643 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(…
62644 <span class="hljs-comment">// Prints 'user=abc&#x26;query=xyz'</span>
62646 params = <span class="hljs-keyword">new</span> <span class="hljs-title function_">URLSearchParams</…
62647 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(…
62648 <span class="hljs-comment">// Prints 'user=abc&#x26;query=xyz'</span></code></pre>
62654 …illa.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type">&#x3C;Object></a>…
62659 not allowed. Arrays are stringified using <a href="https://developer.mozilla.org/en-US/docs/Web/Jav…
62661 …<code class="language-js"><span class="hljs-keyword">const</span> params = <span class="hljs-keywo…
62662 <span class="hljs-attr">user</span>: <span class="hljs-string">'abc'</span>,
62663 …<span class="hljs-attr">query</span>: [<span class="hljs-string">'first'</span>, <span class="hljs
62665-variable language_">console</span>.<span class="hljs-title function_">log</span>(params.<span cla…
62666 <span class="hljs-comment">// Prints [ 'first,second' ]</span>
62667 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(…
62668 <span class="hljs-comment">// Prints 'user=abc&#x26;query=first%2Csecond'</span></code></pre>
62674-US/docs/Web/JavaScript/Reference/Iteration_protocols#The_iterable_protocol" class="type">&#x3C;It…
62677 is similar to <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Obj…
62680 <code>URLSearchParams</code>. Elements of <code>iterable</code> are key-value pairs, and can
62683 <pre><code class="language-js"><span class="hljs-keyword">let</span> params;
62685 <span class="hljs-comment">// Using an array</span>
62686 params = <span class="hljs-keyword">new</span> <span class="hljs-title function_">URLSearchParams</…
62687 [<span class="hljs-string">'user'</span>, <span class="hljs-string">'abc'</span>],
62688 [<span class="hljs-string">'query'</span>, <span class="hljs-string">'first'</span>],
62689 [<span class="hljs-string">'query'</span>, <span class="hljs-string">'second'</span>],
62691 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(…
62692 <span class="hljs-comment">// Prints 'user=abc&#x26;query=first&#x26;query=second'</span>
62694 <span class="hljs-comment">// Using a Map object</span>
62695 <span class="hljs-keyword">const</span> map = <span class="hljs-keyword">new</span> <span class="hl…
62696 map.<span class="hljs-title function_">set</span>(<span class="hljs-string">'user'</span>, <span cl…
62697 map.<span class="hljs-title function_">set</span>(<span class="hljs-string">'query'</span>, <span c…
62698 params = <span class="hljs-keyword">new</span> <span class="hljs-title function_">URLSearchParams</…
62699 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(…
62700 <span class="hljs-comment">// Prints 'user=abc&#x26;query=xyz'</span>
62702 <span class="hljs-comment">// Using a generator function</span>
62703 <span class="hljs-keyword">function</span>* <span class="hljs-title function_">getQueryPairs</span>…
62704 …<span class="hljs-keyword">yield</span> [<span class="hljs-string">'user'</span>, <span class="hlj…
62705 …<span class="hljs-keyword">yield</span> [<span class="hljs-string">'query'</span>, <span class="hl…
62706 …<span class="hljs-keyword">yield</span> [<span class="hljs-string">'query'</span>, <span class="hl…
62708 params = <span class="hljs-keyword">new</span> <span class="hljs-title function_">URLSearchParams</…
62709 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(…
62710 <span class="hljs-comment">// Prints 'user=abc&#x26;query=first&#x26;query=second'</span>
62712 <span class="hljs-comment">// Each key-value pair must have exactly two elements</span>
62713 <span class="hljs-keyword">new</span> <span class="hljs-title function_">URLSearchParams</span>([
62714 …[<span class="hljs-string">'user'</span>, <span class="hljs-string">'abc'</span>, <span class="hlj…
62716 <span class="hljs-comment">// Throws TypeError [ERR_INVALID_TUPLE]:</span>
62717 <span class="hljs-comment">// Each query pair must be an iterable [name, value] tuple</span>…
62720 <li><code>name</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
62721 <li><code>value</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struct…
62723 <p>Append a new name-value pair to the query string.</p>
62726 <li><code>name</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
62728 <p>Remove all name-value pairs whose name is <code>name</code>.</p>
62731 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Iteration_p…
62733 <p>Returns an ES6 <code>Iterator</code> over each of the name-value pairs in the query.
62739 …oper.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type">&#x3C;F…
62740 <li><code>thisArg</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference…
62742 <p>Iterates over each name-value pair in the query and invokes the given function.</p>
62743-js"><span class="hljs-keyword">const</span> myURL = <span class="hljs-keyword">new</span> <span c…
62744 … class="hljs-property">searchParams</span>.<span class="hljs-title function_">forEach</span>(<span…
62745 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>…
62747 <span class="hljs-comment">// Prints:</span>
62748 <span class="hljs-comment">// a b true</span>
62749 <span class="hljs-comment">// c d true</span></code></pre>
62752 <li><code>name</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
62753 …mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> …
62756 <p>Returns the value of the first name-value pair whose name is <code>name</code>. If there
62760 <li><code>name</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
62761 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Strin…
62763 <p>Returns the values of all name-value pairs whose name is <code>name</code>. If there are
62767 <li><code>name</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
62768 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boole…
62770 <p>Returns <code>true</code> if there is at least one name-value pair whose name is <code>name</cod…
62773 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Iteration_p…
62775 <p>Returns an ES6 <code>Iterator</code> over the names of each name-value pair.</p>
62776-js"><span class="hljs-keyword">const</span> params = <span class="hljs-keyword">new</span> <span …
62777 … class="hljs-keyword">for</span> (<span class="hljs-keyword">const</span> name <span class="hljs-k…
62778 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>…
62780 <span class="hljs-comment">// Prints:</span>
62781 <span class="hljs-comment">// foo</span>
62782 <span class="hljs-comment">// foo</span></code></pre>
62785 <li><code>name</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
62786 <li><code>value</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struct…
62789 <code>value</code>. If there are any pre-existing name-value pairs whose names are <code>name</code…
62791 append the name-value pair to the query string.</p>
62792 …<code class="language-js"><span class="hljs-keyword">const</span> params = <span class="hljs-keywo…
62793 params.<span class="hljs-title function_">append</span>(<span class="hljs-string">'foo'</span>, <sp…
62794 params.<span class="hljs-title function_">append</span>(<span class="hljs-string">'foo'</span>, <sp…
62795 params.<span class="hljs-title function_">append</span>(<span class="hljs-string">'abc'</span>, <sp…
62796 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(…
62797 <span class="hljs-comment">// Prints foo=bar&#x26;foo=baz&#x26;abc=def</span>
62799 params.<span class="hljs-title function_">set</span>(<span class="hljs-string">'foo'</span>, <span …
62800 params.<span class="hljs-title function_">set</span>(<span class="hljs-string">'xyz'</span>, <span …
62801 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(…
62802 <span class="hljs-comment">// Prints foo=def&#x26;abc=def&#x26;xyz=opq</span></code></pre>
62807 <p>Sort all existing name-value pairs in-place by their names. Sorting is done
62808 …rting_algorithm#Stability">stable sorting algorithm</a>, so relative order between name-value pairs
62811-js"><span class="hljs-keyword">const</span> params = <span class="hljs-keyword">new</span> <span …
62812 params.<span class="hljs-title function_">sort</span>();
62813 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(…
62814 <span class="hljs-comment">// Prints query%5B%5D=abc&#x26;query%5B%5D=123&#x26;type=search</span></…
62817 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Strin…
62820 percent-encoded where necessary.</p>
62823 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Iteration_p…
62825 <p>Returns an ES6 <code>Iterator</code> over the values of each name-value pair.</p>
62828 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Iteration_p…
62830 <p>Returns an ES6 <code>Iterator</code> over each of the name-value pairs in the query string.
62834-js"><span class="hljs-keyword">const</span> params = <span class="hljs-keyword">new</span> <span …
62835 <span class="hljs-keyword">for</span> (<span class="hljs-keyword">const</span> [name, value] <span …
62836 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>…
62838 <span class="hljs-comment">// Prints:</span>
62839 <span class="hljs-comment">// foo bar</span>
62840 <span class="hljs-comment">// xyz baz</span></code></pre>
62846 <li><code>domain</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struc…
62847 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Strin…
62849 <p>Returns the <a href="https://tools.ietf.org/html/rfc5891#section-4.4">Punycode</a> ASCII seriali…
62854 …code class="language-js"><span class="hljs-keyword">const</span> url = <span class="hljs-built_in"…
62855-variable language_">console</span>.<span class="hljs-title function_">log</span>(url.<span class=…
62856 <span class="hljs-comment">// Prints xn--espaol-zwa.com</span>
62857-variable language_">console</span>.<span class="hljs-title function_">log</span>(url.<span class=…
62858 <span class="hljs-comment">// Prints xn--fiq228c.com</span>
62859-variable language_">console</span>.<span class="hljs-title function_">log</span>(url.<span class=…
62860 <span class="hljs-comment">// Prints an empty string</span></code></pre>
62866 <li><code>domain</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struc…
62867 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Strin…
62874 …code class="language-js"><span class="hljs-keyword">const</span> url = <span class="hljs-built_in"…
62875-variable language_">console</span>.<span class="hljs-title function_">log</span>(url.<span class=…
62876 <span class="hljs-comment">// Prints español.com</span>
62877-variable language_">console</span>.<span class="hljs-title function_">log</span>(url.<span class=…
62878 <span class="hljs-comment">// Prints 中文.com</span>
62879-variable language_">console</span>.<span class="hljs-title function_">log</span>(url.<span class=…
62880 <span class="hljs-comment">// Prints an empty string</span></code></pre>
62886 …api" class="type">&#x3C;URL></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScrip…
62887 …loper.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string…
62889 <p>This function ensures the correct decodings of percent-encoded characters as
62890 well as ensuring a cross-platform valid absolute path string.</p>
62892-flavor-selector" type="checkbox" checked aria-label="Show modern ES modules syntax"><code class="…
62894-keyword">const</span> __filename = <span class="hljs-title function_">fileURLToPath</span>(<span …
62896-keyword">new</span> <span class="hljs-title function_">URL</span>(<span class="hljs-string">'file…
62897 <span class="hljs-title function_">fileURLToPath</span>(<span class="hljs-string">'file:///C:/path/…
62899-keyword">new</span> <span class="hljs-title function_">URL</span>(<span class="hljs-string">'file…
62900 <span class="hljs-title function_">fileURLToPath</span>(<span class="hljs-string">'file://nas/foo.t…
62902-keyword">new</span> <span class="hljs-title function_">URL</span>(<span class="hljs-string">'file…
62903 <span class="hljs-title function_">fileURLToPath</span>(<span class="hljs-string">'file:///你好.txt'<…
62905-keyword">new</span> <span class="hljs-title function_">URL</span>(<span class="hljs-string">'file…
62906-title function_">fileURLToPath</span>(<span class="hljs-string">'file:///hello world'</span>); …
62907-keyword">new</span> <span class="hljs-title function_">URL</span>(<span class="hljs-string">'file…
62908 <span class="hljs-title function_">fileURLToPath</span>(<span class="hljs-string">'file:///C:/path/…
62910-keyword">new</span> <span class="hljs-title function_">URL</span>(<span class="hljs-string">'file…
62911 <span class="hljs-title function_">fileURLToPath</span>(<span class="hljs-string">'file://nas/foo.t…
62913-keyword">new</span> <span class="hljs-title function_">URL</span>(<span class="hljs-string">'file…
62914 <span class="hljs-title function_">fileURLToPath</span>(<span class="hljs-string">'file:///你好.txt'<…
62916-keyword">new</span> <span class="hljs-title function_">URL</span>(<span class="hljs-string">'file…
62917 <span class="hljs-title function_">fileURLToPath</span>(<span class="hljs-string">'file:///hello wo…
62924 <li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference…
62926 <li><code>auth</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
62928 <li><code>fragment</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_str…
62930 <li><code>search</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struc…
62932 <li><code>unicode</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_stru…
62937 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Strin…
62946-flavor-selector" type="checkbox" checked aria-label="Show modern ES modules syntax"><code class="…
62947 …lass="hljs-keyword">const</span> myURL = <span class="hljs-keyword">new</span> <span class="hljs-t…
62949 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(…
62950 <span class="hljs-comment">// Prints https://a:b@xn--g6w251d/?abc#foo</span>
62952 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(…
62953 <span class="hljs-comment">// Prints https://a:b@xn--g6w251d/?abc#foo</span>
62955-variable language_">console</span>.<span class="hljs-title function_">log</span>(url.<span class=…
62956-comment">// Prints 'https://測試/?abc'</span></code><code class="language-js cjs"><span class="hljs
62957 …lass="hljs-keyword">const</span> myURL = <span class="hljs-keyword">new</span> <span class="hljs-t…
62959 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(…
62960 <span class="hljs-comment">// Prints https://a:b@xn--g6w251d/?abc#foo</span>
62962 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(…
62963 <span class="hljs-comment">// Prints https://a:b@xn--g6w251d/?abc#foo</span>
62965-variable language_">console</span>.<span class="hljs-title function_">log</span>(url.<span class=…
62966 <span class="hljs-comment">// Prints 'https://測試/?abc'</span></code></pre>
62972 <li><code>path</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
62978-flavor-selector" type="checkbox" checked aria-label="Show modern ES modules syntax"><code class="…
62980-keyword">new</span> <span class="hljs-title function_">URL</span>(<span class="hljs-string">'/foo…
62981 <span class="hljs-title function_">pathToFileURL</span>(<span class="hljs-string">'/foo#1'</span>);…
62983-keyword">new</span> <span class="hljs-title function_">URL</span>(<span class="hljs-string">'/som…
62984-title function_">pathToFileURL</span>(<span class="hljs-string">'/some/path%.c'</span>); <s…
62985 <span class="hljs-keyword">new</span> <span class="hljs-title function_">URL</span>(__filename); …
62986 <span class="hljs-keyword">new</span> <span class="hljs-title function_">URL</span>(__filename); …
62987 <span class="hljs-title function_">pathToFileURL</span>(__filename); <span class="hljs-c…
62988 <span class="hljs-title function_">pathToFileURL</span>(__filename); <span class="hljs-c…
62990-keyword">new</span> <span class="hljs-title function_">URL</span>(<span class="hljs-string">'/foo…
62991 <span class="hljs-title function_">pathToFileURL</span>(<span class="hljs-string">'/foo#1'</span>);…
62993-keyword">new</span> <span class="hljs-title function_">URL</span>(<span class="hljs-string">'/som…
62994 <span class="hljs-title function_">pathToFileURL</span>(<span class="hljs-string">'/some/path%.c'</…
63001 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Obje…
63003 <li><code>protocol</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_str…
63004 <li><code>hostname</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_str…
63006 <li><code>hash</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
63007 <li><code>search</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struc…
63008 <li><code>pathname</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_str…
63009 <li><code>path</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
63013 <li><code>href</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
63014 <li><code>port</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
63015 <li><code>auth</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
63022 …ss="language-js"><span class="hljs-keyword">const</span> { urlToHttpOptions } = <span class="hljs-
63023 …lass="hljs-keyword">const</span> myURL = <span class="hljs-keyword">new</span> <span class="hljs-t…
63025 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(…
63026 <span class="hljs-comment">/**
63029 hostname: 'xn--g6w251d',
63034 href: 'https://a:b<span class="hljs-doctag">@xn</span>--g6w251d/?abc#foo',
63050 … api_stability_3"><a href="#documentation_stability_index">Stability: 3</a> - Legacy: Use the WHAT…
63063 … api_stability_3"><a href="#documentation_stability_index">Stability: 3</a> - Legacy: Use the WHAT…
63078 <p>The <code>host</code> property is the full lower-cased host portion of the URL, including
63082 <p>The <code>hostname</code> property is the lower-cased host name portion of the <code>host</code>
63087 <code>protocol</code> and <code>host</code> components converted to lower-case.</p>
63105 <p>The <code>protocol</code> property identifies the URL's lower-cased protocol scheme.</p>
63122 forward-slash characters (<code>/</code>) are required following the colon in the
63140 … api_stability_3"><a href="#documentation_stability_index">Stability: 3</a> - Legacy: Use the WHAT…
63142 …lla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type">&#x3C;Object></a> …
63148 …code class="language-js"><span class="hljs-keyword">const</span> url = <span class="hljs-built_in"…
63149 url.<span class="hljs-title function_">format</span>({
63150 <span class="hljs-attr">protocol</span>: <span class="hljs-string">'https'</span>,
63151 <span class="hljs-attr">hostname</span>: <span class="hljs-string">'example.com'</span>,
63152 <span class="hljs-attr">pathname</span>: <span class="hljs-string">'/some/path'</span>,
63153 <span class="hljs-attr">query</span>: {
63154 <span class="hljs-attr">page</span>: <span class="hljs-number">1</span>,
63155 <span class="hljs-attr">format</span>: <span class="hljs-string">'json'</span>
63159 <span class="hljs-comment">// => 'https://example.com/some/path?page=1&#x26;format=json'</span></co…
63165 <li>If <code>urlObject.protocol</code> is a string, it is appended as-is to <code>result</code>.</l…
63250 … api_stability_3"><a href="#documentation_stability_index">Stability: 3</a> - Legacy: Use the WHAT…
63252 <li><code>urlString</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_st…
63253 <li><code>parseQueryString</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/…
63257 <li><code>slashesDenoteHost</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript…
63269 lenient, non-standard algorithm for parsing URL strings, security
63292 … api_stability_3"><a href="#documentation_stability_index">Stability: 3</a> - Legacy: Use the WHAT…
63294 <li><code>from</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
63295 <li><code>to</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structure…
63297 <p>The <code>url.resolve()</code> method resolves a target URL relative to a base URL in a
63299 …code class="language-js"><span class="hljs-keyword">const</span> url = <span class="hljs-built_in"…
63300-title function_">resolve</span>(<span class="hljs-string">'/one/two/three'</span>, <span class="h…
63301-title function_">resolve</span>(<span class="hljs-string">'http://example.com/'</span>, <span cla…
63302-title function_">resolve</span>(<span class="hljs-string">'http://example.com/one'</span>, <span …
63304 …guage-js"><span class="hljs-keyword">function</span> <span class="hljs-title function_">resolve</s…
63305-keyword">const</span> resolvedUrl = <span class="hljs-keyword">new</span> <span class="hljs-title…
63306 …<span class="hljs-keyword">if</span> (resolvedUrl.<span class="hljs-property">protocol</span> === …
63307 <span class="hljs-comment">// `from` is a relative URL.</span>
63308 <span class="hljs-keyword">const</span> { pathname, search, hash } = resolvedUrl;
63309 <span class="hljs-keyword">return</span> pathname + search + hash;
63311 …<span class="hljs-keyword">return</span> resolvedUrl.<span class="hljs-title function_">toString</…
63314-title function_">resolve</span>(<span class="hljs-string">'/one/two/three'</span>, <span class="h…
63315-title function_">resolve</span>(<span class="hljs-string">'http://example.com/'</span>, <span cla…
63316-title function_">resolve</span>(<span class="hljs-string">'http://example.com/one'</span>, <span …
63317 <p><a id="whatwg-percent-encoding"></a></p>
63318 </section><section><h3>Percent-encoding in URLs<span><a class="mark" href="#url_percent_encoding_in…
63326 <pre><code class="language-text">&#x3C; > " ` \r \n \t { } | \ ^ '</code></pre>
63332 <p>The WHATWG algorithm defines four "percent-encode sets" that describe ranges
63333 of characters that must be percent-encoded:</p>
63336 <p>The <em>C0 control percent-encode set</em> includes code points in range U+0000 to
63340 <p>The <em>fragment percent-encode set</em> includes the <em>C0 control percent-encode set</em>
63344 <p>The <em>path percent-encode set</em> includes the <em>C0 control percent-encode set</em>
63349 <p>The <em>userinfo encode set</em> includes the <em>path percent-encode set</em> and code
63354 <p>The <em>userinfo percent-encode set</em> is used exclusively for username and
63355 passwords encoded within the URL. The <em>path percent-encode set</em> is used for the
63356 path of most URLs. The <em>fragment percent-encode set</em> is used for URL fragments.
63357 The <em>C0 control percent-encode set</em> is used for host and path under certain
63359 <p>When non-ASCII characters appear within a host name, the host name is encoded
63360 using the <a href="https://tools.ietf.org/html/rfc5891#section-4.4">Punycode</a> algorithm. Note, h…
63361 <em>both</em> Punycode encoded and percent-encoded characters:</p>
63362-js"><span class="hljs-keyword">const</span> myURL = <span class="hljs-keyword">new</span> <span c…
63363 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(…
63364 <span class="hljs-comment">// Prints https://xn--1xa.example.com/foo</span>
63365 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(…
63366 <span class="hljs-comment">// Prints https://xn--1xa.example.com</span></code></pre></section>
63370 …ty api_stability_2"><a href="#documentation_stability_index">Stability: 2</a> - Stable</div><p></p>
63375 …code class="language-js"><span class="hljs-keyword">const</span> util = <span class="hljs-built_in…
63381 <li><code>original</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Referenc…
63382 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Obje…
63385 function following the error-first callback style, i.e. taking
63389 …code class="language-js"><span class="hljs-keyword">const</span> util = <span class="hljs-built_in…
63391 … class="hljs-keyword">async</span> <span class="hljs-keyword">function</span> <span class="hljs-ti…
63392 <span class="hljs-keyword">return</span> <span class="hljs-string">'hello world'</span>;
63394 <span class="hljs-keyword">const</span> callbackFunction = util.<span class="hljs-title function_">…
63396 <span class="hljs-title function_">callbackFunction</span>(<span class="hljs-function">(<span class…
63397 <span class="hljs-keyword">if</span> (err) <span class="hljs-keyword">throw</span> err;
63398 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>…
63401 <pre><code class="language-text">hello world</code></pre>
63409 …<code class="language-js"><span class="hljs-keyword">function</span> <span class="hljs-title funct…
63410 …s="hljs-keyword">return</span> <span class="hljs-title class_">Promise</span>.<span class="hljs-ti…
63412 <span class="hljs-keyword">const</span> callbackFunction = util.<span class="hljs-title function_">…
63414 <span class="hljs-title function_">callbackFunction</span>(<span class="hljs-function">(<span class…
63415 …<span class="hljs-comment">// When the Promise was rejected with `null` it is wrapped with an Erro…
63416 <span class="hljs-comment">// the original value is stored in `reason`.</span>
63417-title function_">hasOwnProperty</span>(<span class="hljs-string">'reason'</span>) &#x26;&#x26; er…
63424 <li><code>section</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_stru…
63426 <li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Referenc…
63428 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Obje…
63434 …ror_data_args"><code>console.error()</code></a>. If not, then the returned function is a no-op.</p>
63435 …code class="language-js"><span class="hljs-keyword">const</span> util = <span class="hljs-built_in…
63436 <span class="hljs-keyword">const</span> debuglog = util.<span class="hljs-title function_">debuglog…
63438 <span class="hljs-title function_">debuglog</span>(<span class="hljs-string">'hello from foo [%d]'<…
63441 <pre><code class="language-console">FOO 3245: hello from foo [123]</code></pre>
63445 …code class="language-js"><span class="hljs-keyword">const</span> util = <span class="hljs-built_in…
63446 …span class="hljs-keyword">const</span> debuglog = util.<span class="hljs-title function_">debuglog…
63448 <span class="hljs-title function_">debuglog</span>(<span class="hljs-string">'hi there, it\'s foo-b…
63451 <pre><code class="language-console">FOO-BAR 3257: hi there, it's foo-bar [2333]</code></pre>
63452 <p>Multiple comma-separated <code>section</code> names may be specified in the <code>NODE_DEBUG</co…
63457 …code class="language-js"><span class="hljs-keyword">const</span> util = <span class="hljs-built_in…
63458-keyword">let</span> debuglog = util.<span class="hljs-title function_">debuglog</span>(<span clas…
63459 <span class="hljs-comment">// Replace with a logging function that optimizes out</span>
63460 <span class="hljs-comment">// testing if the section is enabled</span>
63468 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" …
63475 …code class="language-js"><span class="hljs-keyword">const</span> util = <span class="hljs-built_in…
63476 …ljs-keyword">const</span> enabled = util.<span class="hljs-title function_">debuglog</span>(<span …
63477 <span class="hljs-keyword">if</span> (enabled) {
63478-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hlj…
63482 <pre><code class="language-console">hello from foo [123]</code></pre>
63502 <li><code>fn</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Glob…
63503 <li><code>msg</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structur…
63505 <li><code>code</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
63507 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Obje…
63511 …code class="language-js"><span class="hljs-keyword">const</span> util = <span class="hljs-built_in…
63513-built_in">exports</span>.<span class="hljs-property">obsoleteFunction</span> = util.<span class="…
63514 <span class="hljs-comment">// Do something here.</span>
63515 }, <span class="hljs-string">'obsoleteFunction() is deprecated. Use newShinyFunction() instead.'</s…
63523 …code class="language-js"><span class="hljs-keyword">const</span> util = <span class="hljs-built_in…
63525 …class="hljs-keyword">const</span> fn1 = util.<span class="hljs-title function_">deprecate</span>(s…
63526 …="hljs-keyword">const</span> fn2 = util.<span class="hljs-title function_">deprecate</span>(someOt…
63527 <span class="hljs-title function_">fn1</span>(); <span class="hljs-comment">// Emits a deprecation …
63528 <span class="hljs-title function_">fn2</span>(); <span class="hljs-comment">// Does not emit a depr…
63529 <p>If either the <code>--no-deprecation</code> or <code>--no-warnings</code> command-line flags are
63532 <p>If the <code>--trace-deprecation</code> or <code>--trace-warnings</code> command-line flags are …
63536 <p>If the <code>--throw-deprecation</code> command-line flag is set, or the
63539 <p>The <code>--throw-deprecation</code> command-line flag and <code>process.throwDeprecation</code>
63540 property take precedence over <code>--trace-deprecation</code> and
63569 …eveloper.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;str…
63572 as a <code>printf</code>-like format string which can contain zero or more format
63577 and <code>-0</code>. <code>BigInt</code> values will be represented with an <code>n</code> and Obje…
63590 including non-enumerable properties and proxies.</li>
63593 the full object not including non-enumerable properties and proxies.</li>
63596 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Strin…
63599 …e class="language-js">util.<span class="hljs-title function_">format</span>(<span class="hljs-stri…
63600 <span class="hljs-comment">// Returns: 'foo:%s'</span></code></pre>
63606-js">util.<span class="hljs-title function_">format</span>(<span class="hljs-string">'%s:%s'</span…
63607 <span class="hljs-comment">// Returns: 'foo:bar baz'</span></code></pre>
63610 …ge-js">util.<span class="hljs-title function_">format</span>(<span class="hljs-number">1</span>, <…
63611 <span class="hljs-comment">// Returns: '1 2 3'</span></code></pre>
63614 <pre><code class="language-js">util.<span class="hljs-title function_">format</span>(<span class="h…
63615 <span class="hljs-comment">// Returns: '%% %s'</span></code></pre>
63624 <li><code>inspectOptions</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Re…
63625 <li><code>format</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struc…
63630-js">util.<span class="hljs-title function_">formatWithOptions</span>({ <span class="hljs-attr">co…
63631 <span class="hljs-comment">// Returns 'See object { foo: 42 }', where `42` is colored as a number</…
63632 <span class="hljs-comment">// when printed to a terminal.</span></code></pre>
63638 <li><code>err</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structur…
63639 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Strin…
63642 The mapping between error codes and error names is platform-dependent.
63644-js">fs.<span class="hljs-title function_">access</span>(<span class="hljs-string">'file/that/does…
63645 …<span class="hljs-keyword">const</span> name = util.<span class="hljs-title function_">getSystemEr…
63646 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">error</spa…
63653 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Obje…
63656 The mapping between error codes and error names is platform-dependent.
63658-js">fs.<span class="hljs-title function_">access</span>(<span class="hljs-string">'file/that/does…
63659 …<span class="hljs-keyword">const</span> errorMap = util.<span class="hljs-title function_">getSyst…
63660 …<span class="hljs-keyword">const</span> name = errorMap.<span class="hljs-title function_">get</sp…
63661 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">error</spa…
63675 … api_stability_3"><a href="#documentation_stability_index">Stability: 3</a> - Legacy: Use ES2015 c…
63677 <li><code>constructor</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Refer…
63678 <li><code>superConstructor</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/…
63683 <p>Inherit the prototype methods from one <a href="https://developer.mozilla.org/en-US/docs/Web/Jav…
63690 …code class="language-js"><span class="hljs-keyword">const</span> util = <span class="hljs-built_in…
63691 …s="hljs-keyword">const</span> <span class="hljs-title class_">EventEmitter</span> = <span class="h…
63693 <span class="hljs-keyword">function</span> <span class="hljs-title function_">MyStream</span>(<span…
63694 …<span class="hljs-title class_">EventEmitter</span>.<span class="hljs-title function_">call</span>…
63697 util.<span class="hljs-title function_">inherits</span>(<span class="hljs-title class_">MyStream</s…
63699-title class_">MyStream</span>.<span class="hljs-property"><span class="hljs-keyword">prototype</s…
63700 …<span class="hljs-variable language_">this</span>.<span class="hljs-title function_">emit</span>(<…
63703 <span class="hljs-keyword">const</span> stream = <span class="hljs-keyword">new</span> <span class=…
63705-variable language_">console</span>.<span class="hljs-title function_">log</span>(stream <span cla…
63706-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hlj…
63708 ….<span class="hljs-title function_">on</span>(<span class="hljs-string">'data'</span>, <span class…
63709 …"hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class…
63711 stream.<span class="hljs-title function_">write</span>(<span class="hljs-string">'It works!'</span>…
63713-js"><span class="hljs-keyword">const</span> <span class="hljs-title class_">EventEmitter</span> =…
63715 …class="hljs-keyword">class</span> <span class="hljs-title class_">MyStream</span> <span class="hlj…
63716 <span class="hljs-title function_">write</span>(<span class="hljs-params">data</span>) {
63717 …<span class="hljs-variable language_">this</span>.<span class="hljs-title function_">emit</span>(<…
63721 <span class="hljs-keyword">const</span> stream = <span class="hljs-keyword">new</span> <span class=…
63723 ….<span class="hljs-title function_">on</span>(<span class="hljs-string">'data'</span>, <span class…
63724 …"hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class…
63726 stream.<span class="hljs-title function_">write</span>(<span class="hljs-string">'With ES6'</span>)…
63736 …text</code> now, a custom inspection function on it will not receive context-specific arguments an…
63779 <li><code>object</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struc…
63780 <li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference…
63782 …la.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type">&#x3C;boolean></a> If …
63783 properties are included in the formatted result. <a href="https://developer.mozilla.org/en-US/docs/…
63784 <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/WeakSet">…
63786 <li><code>depth</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struct…
63790 <li><code>colors</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struc…
63793 <li><code>customInspect</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Dat…
63796 <li><code>showProxy</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_st…
63797 …ef="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Proxy#Termino…
63798 <li><code>maxArrayLength</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Da…
63799-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray"><code>TypedArray</code></a>, <a href=…
63802 <li><code>maxStringLength</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/D…
63805 <li><code>breakLength</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_…
63809 …illa.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type">&#x3C;boolean></a> |…
63816 …illa.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type">&#x3C;boolean></a> |…
63818 string. If set to <code>true</code> the <a href="https://developer.mozilla.org/en-US/docs/Web/JavaS…
63819 it is used as a <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_O…
63820 …illa.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type">&#x3C;boolean></a> |…
63827 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Strin…
63835 <pre><code class="language-js"><span class="hljs-keyword">class</span> <span class="hljs-title clas…
63836 …get [<span class="hljs-title class_">Symbol</span>.<span class="hljs-property">toStringTag</span>]…
63837 <span class="hljs-keyword">return</span> <span class="hljs-string">'bar'</span>;
63841 <span class="hljs-keyword">class</span> <span class="hljs-title class_">Bar</span> {}
63843-keyword">const</span> baz = <span class="hljs-title class_">Object</span>.<span class="hljs-title…
63845 …ss="hljs-title function_">inspect</span>(<span class="hljs-keyword">new</span> <span class="hljs-t…
63846 …ss="hljs-title function_">inspect</span>(<span class="hljs-keyword">new</span> <span class="hljs-t…
63847 util.<span class="hljs-title function_">inspect</span>(baz); <span class="hljs-comment">// '[…
63849 … class="language-js"><span class="hljs-keyword">const</span> { inspect } = <span class="hljs-built…
63851 <span class="hljs-keyword">const</span> obj = {};
63852 obj.<span class="hljs-property">a</span> = [obj];
63853 obj.<span class="hljs-property">b</span> = {};
63854 obj.<span class="hljs-property">b</span>.<span class="hljs-property">inner</span> = obj.<span class…
63855 obj.<span class="hljs-property">b</span>.<span class="hljs-property">obj</span> = obj;
63857 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(…
63858 <span class="hljs-comment">// &#x3C;ref *1> {</span>
63859 <span class="hljs-comment">// a: [ [Circular *1] ],</span>
63860 <span class="hljs-comment">// b: &#x3C;ref *2> { inner: [Circular *2], obj: [Circular *1] }</span>
63861 <span class="hljs-comment">// }</span></code></pre>
63863 …code class="language-js"><span class="hljs-keyword">const</span> util = <span class="hljs-built_in…
63865-variable language_">console</span>.<span class="hljs-title function_">log</span>(util.<span class…
63867 …code class="language-js"><span class="hljs-keyword">const</span> util = <span class="hljs-built_in…
63869 <span class="hljs-keyword">const</span> o = {
63870 …<span class="hljs-attr">a</span>: [<span class="hljs-number">1</span>, <span class="hljs-number">2…
63871 …<span class="hljs-string">'Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do '</span…
63872 … <span class="hljs-string">'eiusmod tempor incididunt ut labore et dolore magna aliqua.'</span>,
63873 <span class="hljs-string">'test'</span>,
63874 <span class="hljs-string">'foo'</span>]], <span class="hljs-number">4</span>],
63875-attr">b</span>: <span class="hljs-keyword">new</span> <span class="hljs-title class_">Map</span>(…
63877-variable language_">console</span>.<span class="hljs-title function_">log</span>(util.<span class…
63879 <span class="hljs-comment">// { a:</span>
63880 <span class="hljs-comment">// [ 1,</span>
63881 <span class="hljs-comment">// 2,</span>
63882 <span class="hljs-comment">// [ [ 'Lorem ipsum dolor sit amet, consectetur [...]', // A long li…
63883 <span class="hljs-comment">// 'test',</span>
63884 <span class="hljs-comment">// 'foo' ] ],</span>
63885 <span class="hljs-comment">// 4 ],</span>
63886 <span class="hljs-comment">// b: Map(2) { 'za' => 1, 'zb' => 'test' } }</span>
63888 <span class="hljs-comment">// Setting `compact` to false changes the output to be more reader frien…
63889-variable language_">console</span>.<span class="hljs-title function_">log</span>(util.<span class…
63891 <span class="hljs-comment">// {</span>
63892 <span class="hljs-comment">// a: [</span>
63893 <span class="hljs-comment">// 1,</span>
63894 <span class="hljs-comment">// 2,</span>
63895 <span class="hljs-comment">// [</span>
63896 <span class="hljs-comment">// [</span>
63897 <span class="hljs-comment">// 'Lorem ipsum dolor sit amet, consectetur ' +</span>
63898 <span class="hljs-comment">// 'adipiscing elit, sed do eiusmod tempor ' +</span>
63899 <span class="hljs-comment">// 'incididunt ut labore et dolore magna ' +</span>
63900 <span class="hljs-comment">// 'aliqua.,</span>
63901 <span class="hljs-comment">// 'test',</span>
63902 <span class="hljs-comment">// 'foo'</span>
63903 <span class="hljs-comment">// ]</span>
63904 <span class="hljs-comment">// ],</span>
63905 <span class="hljs-comment">// 4</span>
63906 <span class="hljs-comment">// ],</span>
63907 <span class="hljs-comment">// b: Map(2) {</span>
63908 <span class="hljs-comment">// 'za' => 1,</span>
63909 <span class="hljs-comment">// 'zb' => 'test'</span>
63910 <span class="hljs-comment">// }</span>
63911 <span class="hljs-comment">// }</span>
63913 <span class="hljs-comment">// Setting `breakLength` to e.g. 150 will print the "Lorem ipsum" text i…
63914 <span class="hljs-comment">// single line.</span>
63915 <span class="hljs-comment">// Reducing the `breakLength` will split the "Lorem ipsum" text in small…
63916 <span class="hljs-comment">// chunks.</span></code></pre>
63917 …zilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/WeakMap"><code>WeakMap</code></a> and…
63920 <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/WeakSet">…
63922 … class="language-js"><span class="hljs-keyword">const</span> { inspect } = <span class="hljs-built…
63924 <span class="hljs-keyword">const</span> obj = { <span class="hljs-attr">a</span>: <span class="hljs
63925 <span class="hljs-keyword">const</span> obj2 = { <span class="hljs-attr">b</span>: <span class="hlj…
63926 <span class="hljs-keyword">const</span> weakSet = <span class="hljs-keyword">new</span> <span class…
63928-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hlj…
63929 <span class="hljs-comment">// WeakSet { { a: 1 }, { b: 2 } }</span></code></pre>
63932 … class="language-js"><span class="hljs-keyword">const</span> { inspect } = <span class="hljs-built…
63933 <span class="hljs-keyword">const</span> assert = <span class="hljs-built_in">require</span>(<span c…
63935 <span class="hljs-keyword">const</span> o1 = {
63936 …<span class="hljs-attr">b</span>: [<span class="hljs-number">2</span>, <span class="hljs-number">3…
63937 <span class="hljs-attr">a</span>: <span class="hljs-string">'`a` comes before `b`'</span>,
63938-attr">c</span>: <span class="hljs-keyword">new</span> <span class="hljs-title class_">Set</span>(…
63940-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hlj…
63941 <span class="hljs-comment">// { a: '`a` comes before `b`', b: [ 2, 3, 1 ], c: Set(3) { 1, 2, 3 } }<…
63942-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hlj…
63943 <span class="hljs-comment">// { c: Set(3) { 3, 2, 1 }, b: [ 2, 3, 1 ], a: '`a` comes before `b`' }<…
63945 <span class="hljs-keyword">const</span> o2 = {
63946-attr">c</span>: <span class="hljs-keyword">new</span> <span class="hljs-title class_">Set</span>(…
63947 <span class="hljs-attr">a</span>: <span class="hljs-string">'`a` comes before `b`'</span>,
63948 …<span class="hljs-attr">b</span>: [<span class="hljs-number">2</span>, <span class="hljs-number">3…
63950 assert.<span class="hljs-property">strict</span>.<span class="hljs-title function_">equal</span>(
63951 …<span class="hljs-title function_">inspect</span>(o1, { <span class="hljs-attr">sorted</span>: <sp…
63952 …<span class="hljs-title function_">inspect</span>(o2, { <span class="hljs-attr">sorted</span>: <sp…
63986 <li><code>reset</code> - Resets all (color) modifiers to their defaults</li>
63987 <li><strong>bold</strong> - Make text bold</li>
63988 <li><em>italic</em> - Make text italic</li>
63989 <li><span style="border-bottom: 1px;">underline</span> - Make text underlined</li>
63990 <li><del>strikethrough</del> - Puts a horizontal line through the center of the text
63992 <li><code>hidden</code> - Prints the text, but makes it invisible (Alias: conceal)</li>
63993 <li><span style="opacity: 0.5;">dim</span> - Decreased color intensity (Alias:
63995 <li><span style="border-top: 1px">overlined</span> - Make text overlined</li>
63996 <li>blink - Hides and shows the text in an interval</li>
63997 <li><span style="filter: invert(100%)">inverse</span> - Swap foreground and
63999 <li><span style="border-bottom: 1px double;">doubleunderline</span> - Make text
64001 <li><span style="border: 1px">framed</span> - Draw a frame around the text</li>
64047 …code class="language-js"><span class="hljs-keyword">const</span> util = <span class="hljs-built_in…
64049 <span class="hljs-keyword">class</span> <span class="hljs-title class_">Box</span> {
64050 <span class="hljs-title function_">constructor</span>(<span class="hljs-params">value</span>) {
64051 …<span class="hljs-variable language_">this</span>.<span class="hljs-property">value</span> = value;
64054 …[util.<span class="hljs-property">inspect</span>.<span class="hljs-property">custom</span>](depth,…
64055 <span class="hljs-keyword">if</span> (depth &#x3C; <span class="hljs-number">0</span>) {
64056 …"hljs-keyword">return</span> options.<span class="hljs-title function_">stylize</span>(<span class…
64059 …<span class="hljs-keyword">const</span> newOptions = <span class="hljs-title class_">Object</span>…
64060-attr">depth</span>: options.<span class="hljs-property">depth</span> === <span class="hljs-litera…
64063 <span class="hljs-comment">// Five space padding because that's the size of "Box&#x3C; ".</span>
64064 …ss="hljs-keyword">const</span> padding = <span class="hljs-string">' '</span>.<span class="hljs-ti…
64065-keyword">const</span> inner = util.<span class="hljs-title function_">inspect</span>(<span class=…
64066 …span class="hljs-title function_">replace</span>(<span class="hljs-regexp">/\n/g</span>, <span cla…
64067-keyword">return</span> <span class="hljs-string">`<span class="hljs-subst">${options.stylize(<spa…
64071 … class="hljs-keyword">const</span> box = <span class="hljs-keyword">new</span> <span class="hljs-t…
64073 util.<span class="hljs-title function_">inspect</span>(box);
64074 <span class="hljs-comment">// Returns: "Box&#x3C; true >"</span></code></pre>
64078 …code class="language-js"><span class="hljs-keyword">const</span> util = <span class="hljs-built_in…
64080 <span class="hljs-keyword">const</span> obj = { <span class="hljs-attr">foo</span>: <span class="hl…
64081 …<span class="hljs-property">inspect</span>.<span class="hljs-property">custom</span>] = <span clas…
64082 …<span class="hljs-keyword">return</span> { <span class="hljs-attr">bar</span>: <span class="hljs-s…
64085 util.<span class="hljs-title function_">inspect</span>(obj);
64086 <span class="hljs-comment">// Returns: "{ bar: 'baz' }"</span></code></pre>
64100 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Symbol_type" c…
64103 symbol is <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects…
64105-js"><span class="hljs-keyword">const</span> inspect = <span class="hljs-title class_">Symbol</spa…
64107 <span class="hljs-keyword">class</span> <span class="hljs-title class_">Password</span> {
64108 <span class="hljs-title function_">constructor</span>(<span class="hljs-params">value</span>) {
64109 …<span class="hljs-variable language_">this</span>.<span class="hljs-property">value</span> = value;
64112 <span class="hljs-title function_">toString</span>(<span class="hljs-params"></span>) {
64113 <span class="hljs-keyword">return</span> <span class="hljs-string">'xxxxxxxx'</span>;
64117 …<span class="hljs-keyword">return</span> <span class="hljs-string">`Password &#x3C;<span class="hl…
64121 …s="hljs-keyword">const</span> password = <span class="hljs-keyword">new</span> <span class="hljs-t…
64122 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(…
64123 <span class="hljs-comment">// Prints Password &#x3C;xxxxxxxx></span></code></pre>
64134 …code class="language-js"><span class="hljs-keyword">const</span> util = <span class="hljs-built_in…
64135-keyword">const</span> arr = <span class="hljs-title class_">Array</span>(<span class="hljs-number…
64137 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(…
64138 …ljs-property">inspect</span>.<span class="hljs-property">defaultOptions</span>.<span class="hljs-p…
64139 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(…
64145 <li><code>val1</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
64146 <li><code>val2</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
64147 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boole…
64158 <li><code>original</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Referenc…
64159 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Obje…
64161 <p>Takes a function following the common error-first callback style, i.e. taking
64164 …code class="language-js"><span class="hljs-keyword">const</span> util = <span class="hljs-built_in…
64165 <span class="hljs-keyword">const</span> fs = <span class="hljs-built_in">require</span>(<span class…
64167 <span class="hljs-keyword">const</span> stat = util.<span class="hljs-title function_">promisify</s…
64168-title function_">stat</span>(<span class="hljs-string">'.'</span>).<span class="hljs-title functi…
64169 <span class="hljs-comment">// Do something with `stats`</span>
64170 }).<span class="hljs-title function_">catch</span>(<span class="hljs-function">(<span class="hljs-p…
64171 <span class="hljs-comment">// Handle the error.</span>
64174 …code class="language-js"><span class="hljs-keyword">const</span> util = <span class="hljs-built_in…
64175 <span class="hljs-keyword">const</span> fs = <span class="hljs-built_in">require</span>(<span class…
64177 <span class="hljs-keyword">const</span> stat = util.<span class="hljs-title function_">promisify</s…
64179 …ass="hljs-keyword">async</span> <span class="hljs-keyword">function</span> <span class="hljs-title…
64180 …ss="hljs-keyword">const</span> stats = <span class="hljs-keyword">await</span> <span class="hljs-t…
64181-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hlj…
64188 an error-first callback, it will still be passed an error-first
64192 …code class="language-js"><span class="hljs-keyword">const</span> util = <span class="hljs-built_in…
64194 <span class="hljs-keyword">class</span> <span class="hljs-title class_">Foo</span> {
64195 <span class="hljs-title function_">constructor</span>(<span class="hljs-params"></span>) {
64196 …<span class="hljs-variable language_">this</span>.<span class="hljs-property">a</span> = <span cla…
64199 <span class="hljs-title function_">bar</span>(<span class="hljs-params">callback</span>) {
64200 …"hljs-title function_">callback</span>(<span class="hljs-literal">null</span>, <span class="hljs-v…
64204 <span class="hljs-keyword">const</span> foo = <span class="hljs-keyword">new</span> <span class="hl…
64206 <span class="hljs-keyword">const</span> naiveBar = util.<span class="hljs-title function_">promisif…
64207 <span class="hljs-comment">// TypeError: Cannot read property 'a' of undefined</span>
64208 <span class="hljs-comment">// naiveBar().then(a => console.log(a));</span>
64210-title function_">call</span>(foo).<span class="hljs-title function_">then</span>(<span class="hlj…
64212 <span class="hljs-keyword">const</span> bindBar = naiveBar.<span class="hljs-title function_">bind<…
64213-title function_">bindBar</span>().<span class="hljs-title function_">then</span>(<span class="hlj…
64217 …code class="language-js"><span class="hljs-keyword">const</span> util = <span class="hljs-built_in…
64219 <span class="hljs-keyword">function</span> <span class="hljs-title function_">doSomething</span>(<s…
64220 <span class="hljs-comment">// ...</span>
64223 …span class="hljs-property">promisify</span>.<span class="hljs-property">custom</span>] = <span cla…
64224 …<span class="hljs-keyword">return</span> <span class="hljs-title function_">getPromiseSomehow</spa…
64227 <span class="hljs-keyword">const</span> promisified = util.<span class="hljs-title function_">promi…
64228-variable language_">console</span>.<span class="hljs-title function_">log</span>(promisified === …
64229 <span class="hljs-comment">// prints 'true'</span></code></pre>
64231 standard format of taking an error-first callback as the last argument.</p>
64234-js">doSomething[util.<span class="hljs-property">promisify</span>.<span class="hljs-property">cus…
64235-keyword">return</span> <span class="hljs-keyword">new</span> <span class="hljs-title class_">Prom…
64236 <span class="hljs-title function_">doSomething</span>(foo, resolve, reject);
64254 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Symbol_type" c…
64258 symbol is <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects…
64262-js"><span class="hljs-keyword">const</span> kCustomPromisifiedSymbol = <span class="hljs-title cl…
64264 doSomething[kCustomPromisifiedSymbol] = <span class="hljs-function">(<span class="hljs-params">foo<…
64265-keyword">return</span> <span class="hljs-keyword">new</span> <span class="hljs-title class_">Prom…
64266 <span class="hljs-title function_">doSomething</span>(foo, resolve, reject);
64274-js"><span class="hljs-keyword">const</span> decoder = <span class="hljs-keyword">new</span> <span…
64275 <span class="hljs-keyword">let</span> string = <span class="hljs-string">''</span>;
64276 <span class="hljs-keyword">let</span> buffer;
64277 <span class="hljs-keyword">while</span> (buffer = <span class="hljs-title function_">getNextChunkSo…
64278 …= decoder.<span class="hljs-title function_">decode</span>(buffer, { <span class="hljs-attr">strea…
64280 string += decoder.<span class="hljs-title function_">decode</span>(); <span class="hljs-comment">//…
64433-8859-2'</code></td><td><code>'csisolatin2'</code>, <code>'iso-ir-101'</code>, <code>'iso8859-2'</…
64434 <h5>Encodings supported when Node.js is built with the <code>small-icu</code> option<span><a class=…
64456-8'</code></td><td><code>'unicode-1-1-utf-8'</code>, <code>'utf8'</code></td></tr><tr><td><code>'u…
64475 …<code>'utf-8'</code></td><td><code>'unicode-1-1-utf-8'</code>, <code>'utf8'</code></td></tr><tr><t…
64476 <p>The <code>'iso-8859-16'</code> encoding listed in the <a href="https://encoding.spec.whatwg.org/…
64491 <li><code>encoding</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_str…
64492 supports. <strong>Default:</strong> <code>'utf-8'</code>.</li>
64493 <li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference…
64495 <li><code>fatal</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struct…
64498 <li><code>ignoreBOM</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_st…
64501 <code>'utf-8'</code>, <code>'utf-16be'</code> or <code>'utf-16le'</code>. <strong>Default:</strong>…
64510-US/docs/Web/JavaScript/Reference/Global_Objects/ArrayBuffer" class="type">&#x3C;ArrayBuffer></a> …
64512 <li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference…
64514 <li><code>stream</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struc…
64518 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Strin…
64527 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" c…
64532 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" …
64538 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" …
64555 instances of <code>TextEncoder</code> only support UTF-8 encoding.</p>
64556 …<code class="language-js"><span class="hljs-keyword">const</span> encoder = <span class="hljs-keyw…
64557 <span class="hljs-keyword">const</span> uint8array = encoder.<span class="hljs-title function_">enc…
64561 <li><code>input</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struct…
64562 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Obje…
64564 <p>UTF-8 encodes the <code>input</code> string and returns a <code>Uint8Array</code> containing the
64568 <li><code>src</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structur…
64569 <li><code>dest</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Gl…
64570 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Obje…
64572 <li><code>read</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
64573 …//developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;…
64577 <p>UTF-8 encodes the <code>src</code> string to the <code>dest</code> Uint8Array and returns an obj…
64578 containing the read Unicode code units and written UTF-8 bytes.</p>
64579 …<code class="language-js"><span class="hljs-keyword">const</span> encoder = <span class="hljs-keyw…
64580 <span class="hljs-keyword">const</span> src = <span class="hljs-string">'this is some data'</span>;
64581-keyword">const</span> dest = <span class="hljs-keyword">new</span> <span class="hljs-title class_…
64582 <span class="hljs-keyword">const</span> { read, written } = encoder.<span class="hljs-title functio…
64585 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" c…
64587 <p>The encoding supported by the <code>TextEncoder</code> instance. Always set to <code>'utf-8'</co…
64593 <li><code>string</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struc…
64602 <p><code>util.types</code> provides type checks for different kinds of built-in objects.
64614 <li><code>value</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struct…
64615 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boole…
64617 <p>Returns <code>true</code> if the value is a built-in <a href="https://developer.mozilla.org/en-U…
64618 <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/SharedArr…
64621-js">util.<span class="hljs-property">types</span>.<span class="hljs-title function_">isAnyArrayBu…
64622-property">types</span>.<span class="hljs-title function_">isAnyArrayBuffer</span>(<span class="hl…
64628 <li><code>value</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struct…
64629 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boole…
64631 …value is an instance of one of the <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScrip…
64632 views, such as typed array objects or <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScr…
64633 <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/ArrayBuff…
64634-js">util.<span class="hljs-property">types</span>.<span class="hljs-title function_">isArrayBuffe…
64635-property">types</span>.<span class="hljs-title function_">isArrayBufferView</span>(<span class="h…
64636-property">types</span>.<span class="hljs-title function_">isArrayBufferView</span>(<span class="h…
64637-property">types</span>.<span class="hljs-title function_">isArrayBufferView</span>(<span class="h…
64643 <li><code>value</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struct…
64644 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boole…
64647 <!-- eslint-disable prefer-rest-params -->
64648 …code class="language-js"><span class="hljs-keyword">function</span> <span class="hljs-title functi…
64649-property">types</span>.<span class="hljs-title function_">isArgumentsObject</span>(<span class="h…
64656 <li><code>value</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struct…
64657 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boole…
64659 <p>Returns <code>true</code> if the value is a built-in <a href="https://developer.mozilla.org/en-U…
64660 This does <em>not</em> include <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Ref…
64662-js">util.<span class="hljs-property">types</span>.<span class="hljs-title function_">isArrayBuffe…
64663-property">types</span>.<span class="hljs-title function_">isArrayBuffer</span>(<span class="hljs-
64669 <li><code>value</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struct…
64670 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boole…
64672 <p>Returns <code>true</code> if the value is an <a href="https://developer.mozilla.org/en-US/docs/W…
64676-js">util.<span class="hljs-property">types</span>.<span class="hljs-title function_">isAsyncFunct…
64677-property">types</span>.<span class="hljs-title function_">isAsyncFunction</span>(<span class="hlj…
64683 <li><code>value</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struct…
64684 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boole…
64687-js">util.<span class="hljs-property">types</span>.<span class="hljs-title function_">isBigInt64Ar…
64688-property">types</span>.<span class="hljs-title function_">isBigInt64Array</span>(<span class="hlj…
64694 <li><code>value</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struct…
64695 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boole…
64698-js">util.<span class="hljs-property">types</span>.<span class="hljs-title function_">isBigUint64A…
64699-property">types</span>.<span class="hljs-title function_">isBigUint64Array</span>(<span class="hl…
64705 <li><code>value</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struct…
64706 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boole…
64710-js">util.<span class="hljs-property">types</span>.<span class="hljs-title function_">isBooleanObj…
64711 …"hljs-property">types</span>.<span class="hljs-title function_">isBooleanObject</span>(<span class…
64712-property">types</span>.<span class="hljs-title function_">isBooleanObject</span>(<span class="hlj…
64713-property">types</span>.<span class="hljs-title function_">isBooleanObject</span>(<span class="hlj…
64714-property">types</span>.<span class="hljs-title function_">isBooleanObject</span>(<span class="hlj…
64715-property">types</span>.<span class="hljs-title function_">isBooleanObject</span>(<span class="hlj…
64721 <li><code>value</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struct…
64722 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boole…
64727-js">util.<span class="hljs-property">types</span>.<span class="hljs-title function_">isBoxedPrimi…
64728-property">types</span>.<span class="hljs-title function_">isBoxedPrimitive</span>(<span class="hl…
64729-property">types</span>.<span class="hljs-title function_">isBoxedPrimitive</span>(<span class="hl…
64730-property">types</span>.<span class="hljs-title function_">isBoxedPrimitive</span>(<span class="hl…
64731-property">types</span>.<span class="hljs-title function_">isBoxedPrimitive</span>(<span class="hl…
64737 <li><code>value</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struct…
64738 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boole…
64740 <p>Returns <code>true</code> if the value is a built-in <a href="https://developer.mozilla.org/en-U…
64741-js"><span class="hljs-keyword">const</span> ab = <span class="hljs-keyword">new</span> <span clas…
64742-property">types</span>.<span class="hljs-title function_">isDataView</span>(<span class="hljs-key…
64743-property">types</span>.<span class="hljs-title function_">isDataView</span>(<span class="hljs-key…
64744 <p>See also <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objec…
64750 <li><code>value</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struct…
64751 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boole…
64753 <p>Returns <code>true</code> if the value is a built-in <a href="https://developer.mozilla.org/en-U…
64754-js">util.<span class="hljs-property">types</span>.<span class="hljs-title function_">isDate</span…
64760 <li><code>value</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struct…
64761 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boole…
64767 addons. In JavaScript, they are <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Re…
64769 <pre><code class="language-c"><span class="hljs-meta">#<span class="hljs-keyword">include</span> <s…
64770 <span class="hljs-meta">#<span class="hljs-keyword">include</span> <span class="hljs-string">&#x3C;…
64772 <span class="hljs-keyword">static</span> napi_value <span class="hljs-title function_">MyNapi</span…
64773 …pan class="hljs-type">int</span>* raw = (<span class="hljs-type">int</span>*) <span class="hljs-bu…
64774 …e_external(env, (<span class="hljs-type">void</span>*) raw, <span class="hljs-literal">NULL</span>…
64775 <span class="hljs-keyword">if</span> (status != napi_ok) {
64776 …napi_throw_error(env, <span class="hljs-literal">NULL</span>, <span class="hljs-string">"napi_crea…
64777 <span class="hljs-keyword">return</span> <span class="hljs-literal">NULL</span>;
64779 <span class="hljs-keyword">return</span> result;
64782 DECLARE_NAPI_PROPERTY(<span class="hljs-string">"myNapi"</span>, MyNapi)
64784 …ode class="language-js"><span class="hljs-keyword">const</span> native = <span class="hljs-built_i…
64785 <span class="hljs-keyword">const</span> data = native.<span class="hljs-title function_">myNapi</sp…
64786 util.<span class="hljs-property">types</span>.<span class="hljs-title function_">isExternal</span>(…
64787 …lass="hljs-property">types</span>.<span class="hljs-title function_">isExternal</span>(<span class…
64788-property">types</span>.<span class="hljs-title function_">isExternal</span>(<span class="hljs-key…
64796 <li><code>value</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struct…
64797 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boole…
64799 <p>Returns <code>true</code> if the value is a built-in <a href="https://developer.mozilla.org/en-U…
64800-js">util.<span class="hljs-property">types</span>.<span class="hljs-title function_">isFloat32Arr…
64801-property">types</span>.<span class="hljs-title function_">isFloat32Array</span>(<span class="hljs
64802-property">types</span>.<span class="hljs-title function_">isFloat32Array</span>(<span class="hljs
64808 <li><code>value</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struct…
64809 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boole…
64811 <p>Returns <code>true</code> if the value is a built-in <a href="https://developer.mozilla.org/en-U…
64812-js">util.<span class="hljs-property">types</span>.<span class="hljs-title function_">isFloat64Arr…
64813-property">types</span>.<span class="hljs-title function_">isFloat64Array</span>(<span class="hljs
64814-property">types</span>.<span class="hljs-title function_">isFloat64Array</span>(<span class="hljs
64820 <li><code>value</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struct…
64821 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boole…
64827-js">util.<span class="hljs-property">types</span>.<span class="hljs-title function_">isGeneratorF…
64828-property">types</span>.<span class="hljs-title function_">isGeneratorFunction</span>(<span class=…
64834 <li><code>value</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struct…
64835 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boole…
64838 built-in generator function.
64842 …code class="language-js"><span class="hljs-keyword">function</span>* <span class="hljs-title funct…
64843 <span class="hljs-keyword">const</span> generator = <span class="hljs-title function_">foo</span>();
64844 …l.<span class="hljs-property">types</span>.<span class="hljs-title function_">isGeneratorObject</s…
64850 <li><code>value</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struct…
64851 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boole…
64853 <p>Returns <code>true</code> if the value is a built-in <a href="https://developer.mozilla.org/en-U…
64854-js">util.<span class="hljs-property">types</span>.<span class="hljs-title function_">isInt8Array<…
64855-property">types</span>.<span class="hljs-title function_">isInt8Array</span>(<span class="hljs-ke…
64856-property">types</span>.<span class="hljs-title function_">isInt8Array</span>(<span class="hljs-ke…
64862 <li><code>value</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struct…
64863 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boole…
64865 <p>Returns <code>true</code> if the value is a built-in <a href="https://developer.mozilla.org/en-U…
64866-js">util.<span class="hljs-property">types</span>.<span class="hljs-title function_">isInt16Array…
64867-property">types</span>.<span class="hljs-title function_">isInt16Array</span>(<span class="hljs-k…
64868-property">types</span>.<span class="hljs-title function_">isInt16Array</span>(<span class="hljs-k…
64874 <li><code>value</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struct…
64875 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boole…
64877 <p>Returns <code>true</code> if the value is a built-in <a href="https://developer.mozilla.org/en-U…
64878-js">util.<span class="hljs-property">types</span>.<span class="hljs-title function_">isInt32Array…
64879-property">types</span>.<span class="hljs-title function_">isInt32Array</span>(<span class="hljs-k…
64880-property">types</span>.<span class="hljs-title function_">isInt32Array</span>(<span class="hljs-k…
64886 <li><code>value</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struct…
64887 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boole…
64889 <p>Returns <code>true</code> if the value is a built-in <a href="https://developer.mozilla.org/en-U…
64890-js">util.<span class="hljs-property">types</span>.<span class="hljs-title function_">isMap</span>…
64896 <li><code>value</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struct…
64897 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boole…
64899 <p>Returns <code>true</code> if the value is an iterator returned for a built-in
64900 <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Map"><cod…
64901 …e><code class="language-js"><span class="hljs-keyword">const</span> map = <span class="hljs-keywor…
64902-property">types</span>.<span class="hljs-title function_">isMapIterator</span>(map.<span class="h…
64903-property">types</span>.<span class="hljs-title function_">isMapIterator</span>(map.<span class="h…
64904-property">types</span>.<span class="hljs-title function_">isMapIterator</span>(map.<span class="h…
64905-property">types</span>.<span class="hljs-title function_">isMapIterator</span>(map[<span class="h…
64911 <li><code>value</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struct…
64912 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boole…
64914 …is an instance of a <a href="https://tc39.github.io/ecma262/#sec-module-namespace-exotic-objects">…
64915 <!-- eslint-skip -->
64916 …uage-js"><span class="hljs-keyword">import</span> * <span class="hljs-keyword">as</span> ns <span …
64918 …l.<span class="hljs-property">types</span>.<span class="hljs-title function_">isModuleNamespaceObj…
64924 <li><code>value</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struct…
64925 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boole…
64927 <p>Returns <code>true</code> if the value is an instance of a built-in <a href="#errors_class_error…
64928-js">util.<span class="hljs-property">types</span>.<span class="hljs-title function_">isNativeErro…
64929-property">types</span>.<span class="hljs-title function_">isNativeError</span>(<span class="hljs-
64930-property">types</span>.<span class="hljs-title function_">isNativeError</span>(<span class="hljs-
64936 <li><code>value</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struct…
64937 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boole…
64941-js">util.<span class="hljs-property">types</span>.<span class="hljs-title function_">isNumberObje…
64942-property">types</span>.<span class="hljs-title function_">isNumberObject</span>(<span class="hljs
64948 <li><code>value</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struct…
64949 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boole…
64951 <p>Returns <code>true</code> if the value is a built-in <a href="https://developer.mozilla.org/en-U…
64952-js">util.<span class="hljs-property">types</span>.<span class="hljs-title function_">isPromise</s…
64958 <li><code>value</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struct…
64959 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boole…
64961 <p>Returns <code>true</code> if the value is a <a href="https://developer.mozilla.org/en-US/docs/We…
64962 <pre><code class="language-js"><span class="hljs-keyword">const</span> target = {};
64963 …span class="hljs-keyword">const</span> proxy = <span class="hljs-keyword">new</span> <span class="…
64964 util.<span class="hljs-property">types</span>.<span class="hljs-title function_">isProxy</span>(tar…
64965 util.<span class="hljs-property">types</span>.<span class="hljs-title function_">isProxy</span>(pro…
64971 <li><code>value</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struct…
64972 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boole…
64975-js">util.<span class="hljs-property">types</span>.<span class="hljs-title function_">isRegExp</sp…
64976-property">types</span>.<span class="hljs-title function_">isRegExp</span>(<span class="hljs-keywo…
64982 <li><code>value</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struct…
64983 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boole…
64985 <p>Returns <code>true</code> if the value is a built-in <a href="https://developer.mozilla.org/en-U…
64986-js">util.<span class="hljs-property">types</span>.<span class="hljs-title function_">isSet</span>…
64992 <li><code>value</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struct…
64993 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boole…
64995 <p>Returns <code>true</code> if the value is an iterator returned for a built-in
64996 <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Set"><cod…
64997 …e><code class="language-js"><span class="hljs-keyword">const</span> set = <span class="hljs-keywor…
64998-property">types</span>.<span class="hljs-title function_">isSetIterator</span>(set.<span class="h…
64999-property">types</span>.<span class="hljs-title function_">isSetIterator</span>(set.<span class="h…
65000-property">types</span>.<span class="hljs-title function_">isSetIterator</span>(set.<span class="h…
65001-property">types</span>.<span class="hljs-title function_">isSetIterator</span>(set[<span class="h…
65007 <li><code>value</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struct…
65008 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boole…
65010 <p>Returns <code>true</code> if the value is a built-in <a href="https://developer.mozilla.org/en-U…
65011 This does <em>not</em> include <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Ref…
65013-js">util.<span class="hljs-property">types</span>.<span class="hljs-title function_">isSharedArra…
65014-property">types</span>.<span class="hljs-title function_">isSharedArrayBuffer</span>(<span class=…
65020 <li><code>value</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struct…
65021 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boole…
65025-js">util.<span class="hljs-property">types</span>.<span class="hljs-title function_">isStringObje…
65026-property">types</span>.<span class="hljs-title function_">isStringObject</span>(<span class="hljs
65032 <li><code>value</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struct…
65033 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boole…
65037 …e class="language-js"><span class="hljs-keyword">const</span> symbol = <span class="hljs-title cla…
65038 util.<span class="hljs-property">types</span>.<span class="hljs-title function_">isSymbolObject</sp…
65039-property">types</span>.<span class="hljs-title function_">isSymbolObject</span>(<span class="hljs
65045 <li><code>value</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struct…
65046 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boole…
65048 <p>Returns <code>true</code> if the value is a built-in <a href="https://developer.mozilla.org/en-U…
65049-js">util.<span class="hljs-property">types</span>.<span class="hljs-title function_">isTypedArray…
65050-property">types</span>.<span class="hljs-title function_">isTypedArray</span>(<span class="hljs-k…
65051-property">types</span>.<span class="hljs-title function_">isTypedArray</span>(<span class="hljs-k…
65052 <p>See also <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objec…
65058 <li><code>value</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struct…
65059 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boole…
65061 <p>Returns <code>true</code> if the value is a built-in <a href="https://developer.mozilla.org/en-U…
65062-js">util.<span class="hljs-property">types</span>.<span class="hljs-title function_">isUint8Array…
65063-property">types</span>.<span class="hljs-title function_">isUint8Array</span>(<span class="hljs-k…
65064-property">types</span>.<span class="hljs-title function_">isUint8Array</span>(<span class="hljs-k…
65070 <li><code>value</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struct…
65071 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boole…
65073 <p>Returns <code>true</code> if the value is a built-in <a href="https://developer.mozilla.org/en-U…
65074-js">util.<span class="hljs-property">types</span>.<span class="hljs-title function_">isUint8Clamp…
65075-property">types</span>.<span class="hljs-title function_">isUint8ClampedArray</span>(<span class=…
65076-property">types</span>.<span class="hljs-title function_">isUint8ClampedArray</span>(<span class=…
65082 <li><code>value</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struct…
65083 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boole…
65085 <p>Returns <code>true</code> if the value is a built-in <a href="https://developer.mozilla.org/en-U…
65086-js">util.<span class="hljs-property">types</span>.<span class="hljs-title function_">isUint16Arra…
65087-property">types</span>.<span class="hljs-title function_">isUint16Array</span>(<span class="hljs-
65088-property">types</span>.<span class="hljs-title function_">isUint16Array</span>(<span class="hljs-
65094 <li><code>value</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struct…
65095 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boole…
65097 <p>Returns <code>true</code> if the value is a built-in <a href="https://developer.mozilla.org/en-U…
65098-js">util.<span class="hljs-property">types</span>.<span class="hljs-title function_">isUint32Arra…
65099-property">types</span>.<span class="hljs-title function_">isUint32Array</span>(<span class="hljs-
65100-property">types</span>.<span class="hljs-title function_">isUint32Array</span>(<span class="hljs-
65106 <li><code>value</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struct…
65107 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boole…
65109 <p>Returns <code>true</code> if the value is a built-in <a href="https://developer.mozilla.org/en-U…
65110-js">util.<span class="hljs-property">types</span>.<span class="hljs-title function_">isWeakMap</s…
65116 <li><code>value</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struct…
65117 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boole…
65119 <p>Returns <code>true</code> if the value is a built-in <a href="https://developer.mozilla.org/en-U…
65120-js">util.<span class="hljs-property">types</span>.<span class="hljs-title function_">isWeakSet</s…
65125 … api_stability_0"><a href="#documentation_stability_index">Stability: 0</a> - Deprecated: Use <cod…
65127 <li><code>value</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struct…
65128 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boole…
65130 <p>Returns <code>true</code> if the value is a built-in <a href="https://developer.mozilla.org/en-U…
65131-js"><span class="hljs-keyword">const</span> <span class="hljs-variable language_">module</span> =…
65132-property">types</span>.<span class="hljs-title function_">isWebAssemblyCompiledModule</span>(<spa…
65136 <h4><code>util._extend(target, source)</code><span><a class="mark" href="#util_util_extend_target_s…
65140 …cumentation_stability_index">Stability: 0</a> - Deprecated: Use <a href="https://developer.mozilla…
65142 <li><code>target</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/…
65143 <li><code>source</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/…
65148 similar built-in functionality through <a href="https://developer.mozilla.org/en-US/docs/Web/JavaSc…
65153 …cumentation_stability_index">Stability: 0</a> - Deprecated: Use <a href="https://developer.mozilla…
65155 <li><code>object</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struc…
65156 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boole…
65158 <p>Alias for <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Obje…
65160 …code class="language-js"><span class="hljs-keyword">const</span> util = <span class="hljs-built_in…
65162 util.<span class="hljs-title function_">isArray</span>([]);
65163 <span class="hljs-comment">// Returns: true</span>
65164 util.<span class="hljs-title function_">isArray</span>(<span class="hljs-keyword">new</span> <span …
65165 <span class="hljs-comment">// Returns: true</span>
65166 util.<span class="hljs-title function_">isArray</span>({});
65167 <span class="hljs-comment">// Returns: false</span></code></pre>
65172 … api_stability_0"><a href="#documentation_stability_index">Stability: 0</a> - Deprecated: Use <cod…
65174 <li><code>object</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struc…
65175 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boole…
65178 …code class="language-js"><span class="hljs-keyword">const</span> util = <span class="hljs-built_in…
65180 util.<span class="hljs-title function_">isBoolean</span>(<span class="hljs-number">1</span>);
65181 <span class="hljs-comment">// Returns: false</span>
65182 util.<span class="hljs-title function_">isBoolean</span>(<span class="hljs-number">0</span>);
65183 <span class="hljs-comment">// Returns: false</span>
65184 util.<span class="hljs-title function_">isBoolean</span>(<span class="hljs-literal">false</span>);
65185 <span class="hljs-comment">// Returns: true</span></code></pre>
65190 … api_stability_0"><a href="#documentation_stability_index">Stability: 0</a> - Deprecated: Use <a h…
65192 <li><code>object</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struc…
65193 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boole…
65196 …code class="language-js"><span class="hljs-keyword">const</span> util = <span class="hljs-built_in…
65198 util.<span class="hljs-title function_">isBuffer</span>({ <span class="hljs-attr">length</span>: <s…
65199 <span class="hljs-comment">// Returns: false</span>
65200 util.<span class="hljs-title function_">isBuffer</span>([]);
65201 <span class="hljs-comment">// Returns: false</span>
65202 …hljs-title function_">isBuffer</span>(<span class="hljs-title class_">Buffer</span>.<span class="h…
65203 <span class="hljs-comment">// Returns: true</span></code></pre>
65208 … api_stability_0"><a href="#documentation_stability_index">Stability: 0</a> - Deprecated: Use <a h…
65210 <li><code>object</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struc…
65211 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boole…
65214 …code class="language-js"><span class="hljs-keyword">const</span> util = <span class="hljs-built_in…
65216 util.<span class="hljs-title function_">isDate</span>(<span class="hljs-keyword">new</span> <span c…
65217 <span class="hljs-comment">// Returns: true</span>
65218 util.<span class="hljs-title function_">isDate</span>(<span class="hljs-title class_">Date</span>()…
65219 <span class="hljs-comment">// false (without 'new' returns a String)</span>
65220 util.<span class="hljs-title function_">isDate</span>({});
65221 <span class="hljs-comment">// Returns: false</span></code></pre>
65226 … api_stability_0"><a href="#documentation_stability_index">Stability: 0</a> - Deprecated: Use <a h…
65228 <li><code>object</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struc…
65229 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boole…
65233 …code class="language-js"><span class="hljs-keyword">const</span> util = <span class="hljs-built_in…
65235 util.<span class="hljs-title function_">isError</span>(<span class="hljs-keyword">new</span> <span …
65236 <span class="hljs-comment">// Returns: true</span>
65237 util.<span class="hljs-title function_">isError</span>(<span class="hljs-keyword">new</span> <span …
65238 <span class="hljs-comment">// Returns: true</span>
65239-title function_">isError</span>({ <span class="hljs-attr">name</span>: <span class="hljs-string">…
65240 <span class="hljs-comment">// Returns: false</span></code></pre>
65244 …code class="language-js"><span class="hljs-keyword">const</span> util = <span class="hljs-built_in…
65245-keyword">const</span> obj = { <span class="hljs-attr">name</span>: <span class="hljs-string">'Err…
65247 util.<span class="hljs-title function_">isError</span>(obj);
65248 <span class="hljs-comment">// Returns: false</span>
65249 obj[<span class="hljs-title class_">Symbol</span>.<span class="hljs-property">toStringTag</span>] =…
65250 util.<span class="hljs-title function_">isError</span>(obj);
65251 <span class="hljs-comment">// Returns: true</span></code></pre>
65256 … api_stability_0"><a href="#documentation_stability_index">Stability: 0</a> - Deprecated: Use <cod…
65258 <li><code>object</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struc…
65259 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boole…
65263 …code class="language-js"><span class="hljs-keyword">const</span> util = <span class="hljs-built_in…
65265 <span class="hljs-keyword">function</span> <span class="hljs-title function_">Foo</span>(<span clas…
65266 <span class="hljs-keyword">const</span> <span class="hljs-title function_">Bar</span> = (<span clas…
65268 util.<span class="hljs-title function_">isFunction</span>({});
65269 <span class="hljs-comment">// Returns: false</span>
65270 util.<span class="hljs-title function_">isFunction</span>(<span class="hljs-title class_">Foo</span…
65271 <span class="hljs-comment">// Returns: true</span>
65272 util.<span class="hljs-title function_">isFunction</span>(<span class="hljs-title class_">Bar</span…
65273 <span class="hljs-comment">// Returns: true</span></code></pre>
65278 … api_stability_0"><a href="#documentation_stability_index">Stability: 0</a> - Deprecated: Use <cod…
65280 <li><code>object</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struc…
65281 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boole…
65285 …code class="language-js"><span class="hljs-keyword">const</span> util = <span class="hljs-built_in…
65287 util.<span class="hljs-title function_">isNull</span>(<span class="hljs-number">0</span>);
65288 <span class="hljs-comment">// Returns: false</span>
65289 util.<span class="hljs-title function_">isNull</span>(<span class="hljs-literal">undefined</span>);
65290 <span class="hljs-comment">// Returns: false</span>
65291 util.<span class="hljs-title function_">isNull</span>(<span class="hljs-literal">null</span>);
65292 <span class="hljs-comment">// Returns: true</span></code></pre>
65297 …bility api_stability_0"><a href="#documentation_stability_index">Stability: 0</a> - Deprecated: Use
65300 <li><code>object</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struc…
65301 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boole…
65305 …code class="language-js"><span class="hljs-keyword">const</span> util = <span class="hljs-built_in…
65307 util.<span class="hljs-title function_">isNullOrUndefined</span>(<span class="hljs-number">0</span>…
65308 <span class="hljs-comment">// Returns: false</span>
65309 util.<span class="hljs-title function_">isNullOrUndefined</span>(<span class="hljs-literal">undefin…
65310 <span class="hljs-comment">// Returns: true</span>
65311 util.<span class="hljs-title function_">isNullOrUndefined</span>(<span class="hljs-literal">null</s…
65312 <span class="hljs-comment">// Returns: true</span></code></pre>
65317 … api_stability_0"><a href="#documentation_stability_index">Stability: 0</a> - Deprecated: Use <cod…
65319 <li><code>object</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struc…
65320 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boole…
65323 …code class="language-js"><span class="hljs-keyword">const</span> util = <span class="hljs-built_in…
65325 util.<span class="hljs-title function_">isNumber</span>(<span class="hljs-literal">false</span>);
65326 <span class="hljs-comment">// Returns: false</span>
65327 util.<span class="hljs-title function_">isNumber</span>(<span class="hljs-title class_">Infinity</s…
65328 <span class="hljs-comment">// Returns: true</span>
65329 util.<span class="hljs-title function_">isNumber</span>(<span class="hljs-number">0</span>);
65330 <span class="hljs-comment">// Returns: true</span>
65331 util.<span class="hljs-title function_">isNumber</span>(<span class="hljs-title class_">Na</span>N);
65332 <span class="hljs-comment">// Returns: true</span></code></pre>
65337 …_stability api_stability_0"><a href="#documentation_stability_index">Stability: 0</a> - Deprecated:
65340 <li><code>object</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struc…
65341 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boole…
65346 …code class="language-js"><span class="hljs-keyword">const</span> util = <span class="hljs-built_in…
65348 util.<span class="hljs-title function_">isObject</span>(<span class="hljs-number">5</span>);
65349 <span class="hljs-comment">// Returns: false</span>
65350 util.<span class="hljs-title function_">isObject</span>(<span class="hljs-literal">null</span>);
65351 <span class="hljs-comment">// Returns: false</span>
65352 util.<span class="hljs-title function_">isObject</span>({});
65353 <span class="hljs-comment">// Returns: true</span>
65354 util.<span class="hljs-title function_">isObject</span>(<span class="hljs-function">() =></span> {}…
65355 <span class="hljs-comment">// Returns: false</span></code></pre>
65360 …bility api_stability_0"><a href="#documentation_stability_index">Stability: 0</a> - Deprecated: Use
65364 <li><code>object</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struc…
65365 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boole…
65369 …code class="language-js"><span class="hljs-keyword">const</span> util = <span class="hljs-built_in…
65371 util.<span class="hljs-title function_">isPrimitive</span>(<span class="hljs-number">5</span>);
65372 <span class="hljs-comment">// Returns: true</span>
65373 util.<span class="hljs-title function_">isPrimitive</span>(<span class="hljs-string">'foo'</span>);
65374 <span class="hljs-comment">// Returns: true</span>
65375 util.<span class="hljs-title function_">isPrimitive</span>(<span class="hljs-literal">false</span>);
65376 <span class="hljs-comment">// Returns: true</span>
65377 util.<span class="hljs-title function_">isPrimitive</span>(<span class="hljs-literal">null</span>);
65378 <span class="hljs-comment">// Returns: true</span>
65379 util.<span class="hljs-title function_">isPrimitive</span>(<span class="hljs-literal">undefined</sp…
65380 <span class="hljs-comment">// Returns: true</span>
65381 util.<span class="hljs-title function_">isPrimitive</span>({});
65382 <span class="hljs-comment">// Returns: false</span>
65383 util.<span class="hljs-title function_">isPrimitive</span>(<span class="hljs-function">() =></span>…
65384 <span class="hljs-comment">// Returns: false</span>
65385 util.<span class="hljs-title function_">isPrimitive</span>(<span class="hljs-regexp">/^$/</span>);
65386 <span class="hljs-comment">// Returns: false</span>
65387 util.<span class="hljs-title function_">isPrimitive</span>(<span class="hljs-keyword">new</span> <s…
65388 <span class="hljs-comment">// Returns: false</span></code></pre>
65393 … api_stability_0"><a href="#documentation_stability_index">Stability: 0</a> - Deprecated</div><p><…
65395 <li><code>object</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struc…
65396 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boole…
65399 …code class="language-js"><span class="hljs-keyword">const</span> util = <span class="hljs-built_in…
65401 util.<span class="hljs-title function_">isRegExp</span>(<span class="hljs-regexp">/some regexp/</sp…
65402 <span class="hljs-comment">// Returns: true</span>
65403 …ss="hljs-title function_">isRegExp</span>(<span class="hljs-keyword">new</span> <span class="hljs-
65404 <span class="hljs-comment">// Returns: true</span>
65405 util.<span class="hljs-title function_">isRegExp</span>({});
65406 <span class="hljs-comment">// Returns: false</span></code></pre>
65411 … api_stability_0"><a href="#documentation_stability_index">Stability: 0</a> - Deprecated: Use <cod…
65413 <li><code>object</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struc…
65414 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boole…
65417 …code class="language-js"><span class="hljs-keyword">const</span> util = <span class="hljs-built_in…
65419 util.<span class="hljs-title function_">isString</span>(<span class="hljs-string">''</span>);
65420 <span class="hljs-comment">// Returns: true</span>
65421 util.<span class="hljs-title function_">isString</span>(<span class="hljs-string">'foo'</span>);
65422 <span class="hljs-comment">// Returns: true</span>
65423 util.<span class="hljs-title function_">isString</span>(<span class="hljs-title class_">String</spa…
65424 <span class="hljs-comment">// Returns: true</span>
65425 util.<span class="hljs-title function_">isString</span>(<span class="hljs-number">5</span>);
65426 <span class="hljs-comment">// Returns: false</span></code></pre>
65431 … api_stability_0"><a href="#documentation_stability_index">Stability: 0</a> - Deprecated: Use <cod…
65433 <li><code>object</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struc…
65434 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boole…
65437 …code class="language-js"><span class="hljs-keyword">const</span> util = <span class="hljs-built_in…
65439 util.<span class="hljs-title function_">isSymbol</span>(<span class="hljs-number">5</span>);
65440 <span class="hljs-comment">// Returns: false</span>
65441 util.<span class="hljs-title function_">isSymbol</span>(<span class="hljs-string">'foo'</span>);
65442 <span class="hljs-comment">// Returns: false</span>
65443 util.<span class="hljs-title function_">isSymbol</span>(<span class="hljs-title class_">Symbol</spa…
65444 <span class="hljs-comment">// Returns: true</span></code></pre>
65449 … api_stability_0"><a href="#documentation_stability_index">Stability: 0</a> - Deprecated: Use <cod…
65451 <li><code>object</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struc…
65452 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boole…
65455 …code class="language-js"><span class="hljs-keyword">const</span> util = <span class="hljs-built_in…
65457 <span class="hljs-keyword">const</span> foo = <span class="hljs-literal">undefined</span>;
65458 util.<span class="hljs-title function_">isUndefined</span>(<span class="hljs-number">5</span>);
65459 <span class="hljs-comment">// Returns: false</span>
65460 util.<span class="hljs-title function_">isUndefined</span>(foo);
65461 <span class="hljs-comment">// Returns: true</span>
65462 util.<span class="hljs-title function_">isUndefined</span>(<span class="hljs-literal">null</span>);
65463 <span class="hljs-comment">// Returns: false</span></code></pre>
65468 … api_stability_0"><a href="#documentation_stability_index">Stability: 0</a> - Deprecated: Use a th…
65470 <li><code>string</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struc…
65474 …code class="language-js"><span class="hljs-keyword">const</span> util = <span class="hljs-built_in…
65476 util.<span class="hljs-title function_">log</span>(<span class="hljs-string">'Timestamped message.'…
65483 …<code class="language-js"><span class="hljs-keyword">const</span> v8 = <span class="hljs-built_in"…
65489 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Numbe…
65492 command-line flags, and detected CPU features. This is useful for determining
65495-js"><span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</…
65496 <span class="hljs-comment">// The value returned by v8.cachedDataVersionTag() is derived from the V…
65497 <span class="hljs-comment">// version, command-line flags, and detected CPU features. Test that the…
65498 <span class="hljs-comment">// does indeed update when flags are toggled.</span>
65499 v8.<span class="hljs-title function_">setFlagsFromString</span>(<span class="hljs-string">'--allow_…
65500-variable language_">console</span>.<span class="hljs-title function_">log</span>(v8.<span class="…
65506 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Obje…
65510 <li><code>code_and_metadata_size</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaS…
65511 <li><code>bytecode_and_metadata_size</code> <a href="https://developer.mozilla.org/en-US/docs/Web/J…
65512 <li><code>external_script_source_size</code> <a href="https://developer.mozilla.org/en-US/docs/Web/…
65514 <!-- eslint-skip -->
65515 <pre><code class="language-js">{
65516 <span class="hljs-attr">code_and_metadata_size</span>: <span class="hljs-number">212208</span>,
65517 …<span class="hljs-attr">bytecode_and_metadata_size</span>: <span class="hljs-number">161368</span>,
65518 …<span class="hljs-attr">external_script_source_size</span>: <span class="hljs-number">1410794</spa…
65532 <pre><code class="language-js"><span class="hljs-comment">// Print heap snapshot to the console</sp…
65533 <span class="hljs-keyword">const</span> v8 = <span class="hljs-built_in">require</span>(<span class…
65534 <span class="hljs-keyword">const</span> stream = v8.<span class="hljs-title function_">getHeapSnaps…
65535 stream.<span class="hljs-title function_">pipe</span>(process.<span class="hljs-property">stdout</s…
65542 <td><p>Support values exceeding the 32-bit unsigned integer range.</p></td></tr>
65549 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Obje…
65554 <a href="https://v8docs.nodesource.com/node-13.2/d5/dda/classv8_1_1_isolate.html#ac673576f24fdc7a33…
65558 <li><code>space_name</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_s…
65559 <li><code>space_size</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_s…
65560 <li><code>space_used_size</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/D…
65561 <li><code>space_available_size</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScr…
65562 <li><code>physical_space_size</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScri…
65564 <pre><code class="language-json"><span class="hljs-punctuation">[</span>
65565 <span class="hljs-punctuation">{</span>
65566 … class="hljs-attr">"space_name"</span><span class="hljs-punctuation">:</span> <span class="hljs-st…
65567 …an class="hljs-attr">"space_size"</span><span class="hljs-punctuation">:</span> <span class="hljs-
65568 … class="hljs-attr">"space_used_size"</span><span class="hljs-punctuation">:</span> <span class="hl…
65569 …lass="hljs-attr">"space_available_size"</span><span class="hljs-punctuation">:</span> <span class=…
65570 …<span class="hljs-attr">"physical_space_size"</span><span class="hljs-punctuation">:</span> <span …
65571 <span class="hljs-punctuation">}</span><span class="hljs-punctuation">,</span>
65572 <span class="hljs-punctuation">{</span>
65573 … class="hljs-attr">"space_name"</span><span class="hljs-punctuation">:</span> <span class="hljs-st…
65574 …an class="hljs-attr">"space_size"</span><span class="hljs-punctuation">:</span> <span class="hljs-
65575 … class="hljs-attr">"space_used_size"</span><span class="hljs-punctuation">:</span> <span class="hl…
65576 … class="hljs-attr">"space_available_size"</span><span class="hljs-punctuation">:</span> <span clas…
65577 …<span class="hljs-attr">"physical_space_size"</span><span class="hljs-punctuation">:</span> <span …
65578 <span class="hljs-punctuation">}</span><span class="hljs-punctuation">,</span>
65579 <span class="hljs-punctuation">{</span>
65580 … class="hljs-attr">"space_name"</span><span class="hljs-punctuation">:</span> <span class="hljs-st…
65581 …an class="hljs-attr">"space_size"</span><span class="hljs-punctuation">:</span> <span class="hljs-
65582 … class="hljs-attr">"space_used_size"</span><span class="hljs-punctuation">:</span> <span class="hl…
65583 …class="hljs-attr">"space_available_size"</span><span class="hljs-punctuation">:</span> <span class…
65584 …<span class="hljs-attr">"physical_space_size"</span><span class="hljs-punctuation">:</span> <span …
65585 <span class="hljs-punctuation">}</span><span class="hljs-punctuation">,</span>
65586 <span class="hljs-punctuation">{</span>
65587 … class="hljs-attr">"space_name"</span><span class="hljs-punctuation">:</span> <span class="hljs-st…
65588 …an class="hljs-attr">"space_size"</span><span class="hljs-punctuation">:</span> <span class="hljs-
65589 … class="hljs-attr">"space_used_size"</span><span class="hljs-punctuation">:</span> <span class="hl…
65590 … class="hljs-attr">"space_available_size"</span><span class="hljs-punctuation">:</span> <span clas…
65591 …<span class="hljs-attr">"physical_space_size"</span><span class="hljs-punctuation">:</span> <span …
65592 <span class="hljs-punctuation">}</span><span class="hljs-punctuation">,</span>
65593 <span class="hljs-punctuation">{</span>
65594 …ss="hljs-attr">"space_name"</span><span class="hljs-punctuation">:</span> <span class="hljs-string…
65595 …<span class="hljs-attr">"space_size"</span><span class="hljs-punctuation">:</span> <span class="hl…
65596 …pan class="hljs-attr">"space_used_size"</span><span class="hljs-punctuation">:</span> <span class=…
65597 …ss="hljs-attr">"space_available_size"</span><span class="hljs-punctuation">:</span> <span class="h…
65598 …<span class="hljs-attr">"physical_space_size"</span><span class="hljs-punctuation">:</span> <span …
65599 <span class="hljs-punctuation">}</span>
65600 <span class="hljs-punctuation">]</span></code></pre>
65607 <td><p>Support values exceeding the 32-bit unsigned integer range.</p></td></tr>
65616 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Obje…
65620 <li><code>total_heap_size</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/D…
65621 <li><code>total_heap_size_executable</code> <a href="https://developer.mozilla.org/en-US/docs/Web/J…
65622 <li><code>total_physical_size</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScri…
65623 <li><code>total_available_size</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScr…
65624 <li><code>used_heap_size</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Da…
65625 <li><code>heap_size_limit</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/D…
65626 <li><code>malloced_memory</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/D…
65627 <li><code>peak_malloced_memory</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScr…
65628 <li><code>does_zap_garbage</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/…
65629 <li><code>number_of_native_contexts</code> <a href="https://developer.mozilla.org/en-US/docs/Web/Ja…
65630 <li><code>number_of_detached_contexts</code> <a href="https://developer.mozilla.org/en-US/docs/Web/…
65633 <code>--zap_code_space</code> option is enabled or not. This makes V8 overwrite heap
65638 top-level contexts currently active. Increase of this number over time indicates
65642 being non-zero indicates a potential memory leak.</p>
65643 <!-- eslint-skip -->
65644 <pre><code class="language-js">{
65645 <span class="hljs-attr">total_heap_size</span>: <span class="hljs-number">7326976</span>,
65646 …<span class="hljs-attr">total_heap_size_executable</span>: <span class="hljs-number">4194304</span…
65647 <span class="hljs-attr">total_physical_size</span>: <span class="hljs-number">7326976</span>,
65648 <span class="hljs-attr">total_available_size</span>: <span class="hljs-number">1152656</span>,
65649 <span class="hljs-attr">used_heap_size</span>: <span class="hljs-number">3476208</span>,
65650 <span class="hljs-attr">heap_size_limit</span>: <span class="hljs-number">1535115264</span>,
65651 <span class="hljs-attr">malloced_memory</span>: <span class="hljs-number">16384</span>,
65652 <span class="hljs-attr">peak_malloced_memory</span>: <span class="hljs-number">1127496</span>,
65653 <span class="hljs-attr">does_zap_garbage</span>: <span class="hljs-number">0</span>,
65654 <span class="hljs-attr">number_of_native_contexts</span>: <span class="hljs-number">1</span>,
65655 <span class="hljs-attr">number_of_detached_contexts</span>: <span class="hljs-number">0</span>
65662 <li><code>flags</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struct…
65665 V8 command-line flags. This method should be used with care. Changing settings
65669 <code>node --v8-options</code>.</p>
65671 <pre><code class="language-js"><span class="hljs-comment">// Print GC events to stdout for one minu…
65672 <span class="hljs-keyword">const</span> v8 = <span class="hljs-built_in">require</span>(<span class…
65673 v8.<span class="hljs-title function_">setFlagsFromString</span>(<span class="hljs-string">'--trace_…
65674-built_in">setTimeout</span>(<span class="hljs-function">() =></span> { v8.<span class="hljs-title…
65699 <li><code>filename</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_str…
65701 <code>'Heap-${yyyymmdd}-${hhmmss}-${pid}-${thread_id}.heapsnapshot'</code> will be
65705 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Strin…
65714 …s="language-js"><span class="hljs-keyword">const</span> { writeHeapSnapshot } = <span class="hljs-
65715 <span class="hljs-keyword">const</span> {
65716 <span class="hljs-title class_">Worker</span>,
65719 } = <span class="hljs-built_in">require</span>(<span class="hljs-string">'worker_threads'</span>);
65721 <span class="hljs-keyword">if</span> (isMainThread) {
65722 …<span class="hljs-keyword">const</span> worker = <span class="hljs-keyword">new</span> <span class…
65724 …span class="hljs-title function_">once</span>(<span class="hljs-string">'message'</span>, <span cl…
65725 …hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class=…
65726 <span class="hljs-comment">// Now get a heapdump for the main thread.</span>
65727 …js-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="h…
65730 <span class="hljs-comment">// Tell the worker to create a heapdump.</span>
65731 …worker.<span class="hljs-title function_">postMessage</span>(<span class="hljs-string">'heapdump'<…
65732 } <span class="hljs-keyword">else</span> {
65733 …span class="hljs-title function_">once</span>(<span class="hljs-string">'message'</span>, <span cl…
65734 <span class="hljs-keyword">if</span> (message === <span class="hljs-string">'heapdump'</span>) {
65735 <span class="hljs-comment">// Generate a heapdump for the worker</span>
65736 <span class="hljs-comment">// and return the filename to the parent.</span>
65737 …parentPort.<span class="hljs-title function_">postMessage</span>(<span class="hljs-title function_…
65743 that is compatible with the <a href="https://developer.mozilla.org/en-US/docs/Web/API/Web_Workers_A…
65744 <p>The format is backward-compatible (i.e. safe to store to disk).
65751 <li><code>value</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struct…
65760 …org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray" class="type">&#x3C;TypedArray><…
65774 <li><code>value</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struct…
65788 …ttps://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&…
65789 <li><code>arrayBuffer</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Refer…
65796 <li><code>value</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struct…
65798 <p>Write a raw 32-bit unsigned integer.
65802 <li><code>hi</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structure…
65803 <li><code>lo</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structure…
65805 <p>Write a raw 64-bit unsigned integer, split into high and low 32-bit parts.
65809 <li><code>value</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struct…
65815 …org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray" class="type">&#x3C;TypedArray><…
65822 <li><code>object</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/…
65831 <li><code>message</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_stru…
65839 <li><code>sharedArrayBuffer</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript…
65842 <code>SharedArrayBuffer</code> object. It must return an unsigned 32-bit integer ID for
65851 <li><code>flag</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
65861 …org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray" class="type">&#x3C;TypedArray><…
65873 …ttps://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&…
65874 …rg/en-US/docs/Web/JavaScript/Reference/Global_Objects/ArrayBuffer" class="type">&#x3C;ArrayBuffer>…
65882 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Numbe…
65889 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Numbe…
65891 <p>Read a raw 32-bit unsigned integer and return it.
65895 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Numbe…
65897 <p>Read a raw 64-bit unsigned integer and return it as an array <code>[hi, lo]</code>
65898 with two 32-bit unsigned integer entries.
65902 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Numbe…
65908 <li><code>length</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struc…
65937 …ty api_stability_2"><a href="#documentation_stability_index">Stability: 2</a> - Stable</div><p></p>
65951 …<code class="language-js"><span class="hljs-keyword">const</span> vm = <span class="hljs-built_in"…
65953 <span class="hljs-keyword">const</span> x = <span class="hljs-number">1</span>;
65955 <span class="hljs-keyword">const</span> context = { <span class="hljs-attr">x</span>: <span class="…
65956 vm.<span class="hljs-title function_">createContext</span>(context); <span class="hljs-comment">// …
65958 <span class="hljs-keyword">const</span> code = <span class="hljs-string">'x += 40; var y = 17;'</sp…
65959 <span class="hljs-comment">// `x` and `y` are global variables in the context.</span>
65960 <span class="hljs-comment">// Initially, x has the value 2 because that is the value of context.x.<…
65961 vm.<span class="hljs-title function_">runInContext</span>(code, context);
65963 …ljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(context.<span…
65964 …ljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(context.<span…
65966 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(…
65988 <li><code>code</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
65989 …lla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type">&#x3C;Object></a> …
65991 <li><code>filename</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_str…
65993 <li><code>lineOffset</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_s…
65995 …developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;nu…
65997 …org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray" class="type">&#x3C;TypedArray><…
66001 <li><code>produceCachedData</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript…
66009 <li><code>importModuleDynamically</code> <a href="https://developer.mozilla.org/en-US/docs/Web/Java…
66015 <li><code>specifier</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_st…
66017 <li>Returns: <a href="https://tc39.github.io/ecma262/#sec-module-namespace-exotic-objects" class="t…
66039-js"><span class="hljs-keyword">const</span> script = <span class="hljs-keyword">new</span> vm.<sp…
66047 <span class="hljs-keyword">const</span> cacheWithoutX = script.<span class="hljs-title function_">c…
66049 script.<span class="hljs-title function_">runInThisContext</span>();
66051 <span class="hljs-keyword">const</span> cacheWithX = script.<span class="hljs-title function_">crea…
66065 <li><code>contextifiedObject</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScrip…
66067 <li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference…
66069 <li><code>displayErrors</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Dat…
66072 <li><code>timeout</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_stru…
66075 <li><code>breakOnSigint</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Dat…
66082 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Data_…
66090 …<code class="language-js"><span class="hljs-keyword">const</span> vm = <span class="hljs-built_in"…
66092 <span class="hljs-keyword">const</span> context = {
66093 <span class="hljs-attr">animal</span>: <span class="hljs-string">'cat'</span>,
66094 <span class="hljs-attr">count</span>: <span class="hljs-number">2</span>
66097 …ss="hljs-keyword">const</span> script = <span class="hljs-keyword">new</span> vm.<span class="hljs
66099 vm.<span class="hljs-title function_">createContext</span>(context);
66100 …n class="hljs-keyword">for</span> (<span class="hljs-keyword">let</span> i = <span class="hljs-num…
66101 script.<span class="hljs-title function_">runInContext</span>(context);
66104 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(…
66105 <span class="hljs-comment">// Prints: { animal: 'cat', count: 12, name: 'kitty' }</span></code></pr…
66107 and corresponding threads being started, which have a non-zero performance
66126 <li><code>contextObject</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Ref…
66128 <li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference…
66130 <li><code>displayErrors</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Dat…
66133 <li><code>timeout</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_stru…
66136 <li><code>breakOnSigint</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Dat…
66141 …ttps://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&…
66144 …ozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> <…
66150 <li><code>contextCodeGeneration</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaSc…
66152 <li><code>strings</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_stru…
66155 <li><code>wasm</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
66159 <li><code>microtaskMode</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Dat…
66165 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Data_…
66173 …<code class="language-js"><span class="hljs-keyword">const</span> vm = <span class="hljs-built_in"…
66175 …ss="hljs-keyword">const</span> script = <span class="hljs-keyword">new</span> vm.<span class="hljs
66177 <span class="hljs-keyword">const</span> contexts = [{}, {}, {}];
66178 contexts.<span class="hljs-title function_">forEach</span>(<span class="hljs-function">(<span class…
66179 script.<span class="hljs-title function_">runInNewContext</span>(context);
66182 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(…
66183 <span class="hljs-comment">// Prints: [{ globalVar: 'set' }, { globalVar: 'set' }, { globalVar: 'se…
66197 <li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference…
66199 <li><code>displayErrors</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Dat…
66202 <li><code>timeout</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_stru…
66205 <li><code>breakOnSigint</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Dat…
66212 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Data_…
66219 …<code class="language-js"><span class="hljs-keyword">const</span> vm = <span class="hljs-built_in"…
66221 <span class="hljs-variable language_">global</span>.<span class="hljs-property">globalVar</span> = …
66223-keyword">const</span> script = <span class="hljs-keyword">new</span> vm.<span class="hljs-title c…
66225 …n class="hljs-keyword">for</span> (<span class="hljs-keyword">let</span> i = <span class="hljs-num…
66226 script.<span class="hljs-title function_">runInThisContext</span>();
66229 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(…
66231 <span class="hljs-comment">// 1000</span></code></pre>
66236 … api_stability_1"><a href="#documentation_stability_index">Stability: 1</a> - Experimental</div><p…
66237 <p>This feature is only available with the <code>--experimental-vm-modules</code> command
66239 <p>The <code>vm.Module</code> class provides a low-level interface for using
66241 class that closely mirrors <a href="https://www.ecma-international.org/ecma-262/#sec-abstract-modul…
66254-flavor-selector" type="checkbox" checked aria-label="Show modern ES modules syntax"><code class="…
66256 <span class="hljs-keyword">const</span> contextifiedObject = vm.<span class="hljs-title function_">…
66257 <span class="hljs-attr">secret</span>: <span class="hljs-number">42</span>,
66258 …<span class="hljs-attr">print</span>: <span class="hljs-variable language_">console</span>.<span c…
66261 <span class="hljs-comment">// Step 1</span>
66262 <span class="hljs-comment">//</span>
66263 <span class="hljs-comment">// Create a Module by constructing a new `vm.SourceTextModule` object. T…
66264 <span class="hljs-comment">// parses the provided source text, throwing a `SyntaxError` if anything…
66265 <span class="hljs-comment">// wrong. By default, a Module is created in the top context. But here, …
66266 <span class="hljs-comment">// specify `contextifiedObject` as the context this Module belongs to.</…
66267 <span class="hljs-comment">//</span>
66268 <span class="hljs-comment">// Here, we attempt to obtain the default export from the module "foo", …
66269 <span class="hljs-comment">// put it into local binding "secret".</span>
66271 …hljs-keyword">const</span> bar = <span class="hljs-keyword">new</span> vm.<span class="hljs-title …
66275 `</span>, { <span class="hljs-attr">context</span>: contextifiedObject });
66277 <span class="hljs-comment">// Step 2</span>
66278 <span class="hljs-comment">//</span>
66279 <span class="hljs-comment">// "Link" the imported dependencies of this Module to it.</span>
66280 <span class="hljs-comment">//</span>
66281 <span class="hljs-comment">// The provided linking callback (the "linker") accepts two arguments: t…
66282 <span class="hljs-comment">// parent module (`bar` in this case) and the string that is the specifi…
66283 <span class="hljs-comment">// the imported module. The callback is expected to return a Module that…
66284 <span class="hljs-comment">// corresponds to the provided specifier, with certain requirements docu…
66285 <span class="hljs-comment">// in `module.link()`.</span>
66286 <span class="hljs-comment">//</span>
66287 <span class="hljs-comment">// If linking has not started for the returned Module, the same linker</…
66288 <span class="hljs-comment">// callback will be called on the returned Module.</span>
66289 <span class="hljs-comment">//</span>
66290 <span class="hljs-comment">// Even top-level Modules without dependencies must be explicitly linked…
66291 <span class="hljs-comment">// callback provided would never be called, however.</span>
66292 <span class="hljs-comment">//</span>
66293 <span class="hljs-comment">// The link() method returns a Promise that will be resolved when all th…
66294 <span class="hljs-comment">// Promises returned by the linker resolve.</span>
66295 <span class="hljs-comment">//</span>
66296 <span class="hljs-comment">// Note: This is a contrived example in that the linker function creates…
66297 <span class="hljs-comment">// "foo" module every time it is called. In a full-fledged module system…
66298 <span class="hljs-comment">// cache would probably be used to avoid duplicated modules.</span>
66300 …lass="hljs-keyword">async</span> <span class="hljs-keyword">function</span> <span class="hljs-titl…
66301 <span class="hljs-keyword">if</span> (specifier === <span class="hljs-string">'foo'</span>) {
66302 …s="hljs-keyword">return</span> <span class="hljs-keyword">new</span> vm.<span class="hljs-title cl…
66306 …`</span>, { <span class="hljs-attr">context</span>: referencingModule.<span class="hljs-property">…
66308 …<span class="hljs-comment">// Using `contextifiedObject` instead of `referencingModule.context`</s…
66309 <span class="hljs-comment">// here would work as well.</span>
66311-keyword">throw</span> <span class="hljs-keyword">new</span> <span class="hljs-title class_">Error…
66313 <span class="hljs-keyword">await</span> bar.<span class="hljs-title function_">link</span>(linker);
66315 <span class="hljs-comment">// Step 3</span>
66316 <span class="hljs-comment">//</span>
66317 <span class="hljs-comment">// Evaluate the Module. The evaluate() method returns a promise which wi…
66318 <span class="hljs-comment">// resolve after the module has finished evaluating.</span>
66320 <span class="hljs-comment">// Prints 42.</span>
66321-keyword">await</span> bar.evaluate();</code><code class="language-js cjs"><span class="hljs-keywo…
66323 <span class="hljs-keyword">const</span> contextifiedObject = vm.<span class="hljs-title function_">…
66324 <span class="hljs-attr">secret</span>: <span class="hljs-number">42</span>,
66325 …<span class="hljs-attr">print</span>: <span class="hljs-variable language_">console</span>.<span c…
66328 (<span class="hljs-keyword">async</span> () => {
66329 <span class="hljs-comment">// Step 1</span>
66330 <span class="hljs-comment">//</span>
66331 …<span class="hljs-comment">// Create a Module by constructing a new `vm.SourceTextModule` object. …
66332 …<span class="hljs-comment">// parses the provided source text, throwing a `SyntaxError` if anythin…
66333 …<span class="hljs-comment">// wrong. By default, a Module is created in the top context. But here,…
66334 …<span class="hljs-comment">// specify `contextifiedObject` as the context this Module belongs to.<…
66335 <span class="hljs-comment">//</span>
66336 …<span class="hljs-comment">// Here, we attempt to obtain the default export from the module "foo",…
66337 <span class="hljs-comment">// put it into local binding "secret".</span>
66339 …hljs-keyword">const</span> bar = <span class="hljs-keyword">new</span> vm.<span class="hljs-title …
66343 `</span>, { <span class="hljs-attr">context</span>: contextifiedObject });
66345 <span class="hljs-comment">// Step 2</span>
66346 <span class="hljs-comment">//</span>
66347 <span class="hljs-comment">// "Link" the imported dependencies of this Module to it.</span>
66348 <span class="hljs-comment">//</span>
66349 …<span class="hljs-comment">// The provided linking callback (the "linker") accepts two arguments: …
66350 …<span class="hljs-comment">// parent module (`bar` in this case) and the string that is the specif…
66351 …<span class="hljs-comment">// the imported module. The callback is expected to return a Module tha…
66352 …<span class="hljs-comment">// corresponds to the provided specifier, with certain requirements doc…
66353 <span class="hljs-comment">// in `module.link()`.</span>
66354 <span class="hljs-comment">//</span>
66355 …<span class="hljs-comment">// If linking has not started for the returned Module, the same linker<…
66356 <span class="hljs-comment">// callback will be called on the returned Module.</span>
66357 <span class="hljs-comment">//</span>
66358 …<span class="hljs-comment">// Even top-level Modules without dependencies must be explicitly linke…
66359 <span class="hljs-comment">// callback provided would never be called, however.</span>
66360 <span class="hljs-comment">//</span>
66361 …<span class="hljs-comment">// The link() method returns a Promise that will be resolved when all t…
66362 <span class="hljs-comment">// Promises returned by the linker resolve.</span>
66363 <span class="hljs-comment">//</span>
66364 …<span class="hljs-comment">// Note: This is a contrived example in that the linker function create…
66365 …<span class="hljs-comment">// "foo" module every time it is called. In a full-fledged module syste…
66366 <span class="hljs-comment">// cache would probably be used to avoid duplicated modules.</span>
66368 …lass="hljs-keyword">async</span> <span class="hljs-keyword">function</span> <span class="hljs-titl…
66369 <span class="hljs-keyword">if</span> (specifier === <span class="hljs-string">'foo'</span>) {
66370 …s="hljs-keyword">return</span> <span class="hljs-keyword">new</span> vm.<span class="hljs-title cl…
66374 …`</span>, { <span class="hljs-attr">context</span>: referencingModule.<span class="hljs-property">…
66376 …<span class="hljs-comment">// Using `contextifiedObject` instead of `referencingModule.context`</s…
66377 <span class="hljs-comment">// here would work as well.</span>
66379-keyword">throw</span> <span class="hljs-keyword">new</span> <span class="hljs-title class_">Error…
66381 …<span class="hljs-keyword">await</span> bar.<span class="hljs-title function_">link</span>(linker);
66383 <span class="hljs-comment">// Step 3</span>
66384 <span class="hljs-comment">//</span>
66385 …<span class="hljs-comment">// Evaluate the Module. The evaluate() method returns a promise which w…
66386 <span class="hljs-comment">// resolve after the module has finished evaluating.</span>
66388 <span class="hljs-comment">// Prints 42.</span>
66389 <span class="hljs-keyword">await</span> bar.evaluate();
66393 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" c…
66397 …uestedModules]]</code> field of <a href="https://tc39.es/ecma262/#sec-cyclic-module-records">Cycli…
66401 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Data_types" cl…
66408 …aluationError]]</code> field of <a href="https://tc39.es/ecma262/#sec-cyclic-module-records">Cycli…
66412 <li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference…
66414 <li><code>timeout</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_stru…
66417 <li><code>breakOnSigint</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Dat…
66424 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Obje…
66430 (<code>module.status</code> is <code>'evaluated'</code>) or it will re-throw the exception that the
66434 …/tc39.es/ecma262/#sec-moduleevaluation">Evaluate() concrete method</a> field of <a href="https://t…
66438 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" c…
66443 <li><code>linker</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/…
66446 <p><code>specifier</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_str…
66447 …<code class="language-js mjs"><span class="hljs-keyword">import</span> foo <span class="hljs-keywo…
66448 <span class="hljs-comment">// ^^^^^ the module specifier</span></code></pre>
66454 …lass="type">&#x3C;vm.Module></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScrip…
66458 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Obje…
66475 <p>The linker function roughly corresponds to the implementation-defined
66476 <a href="https://tc39.es/ecma262/#sec-hostresolveimportedmodule">HostResolveImportedModule</a> abst…
66480 <a href="https://tc39.es/ecma262/#sec-hostresolveimportedmodule">HostResolveImportedModule</a> is s…
66482 <p>The actual <a href="https://tc39.es/ecma262/#sec-hostresolveimportedmodule">HostResolveImportedM…
66485 <a href="https://tc39.es/ecma262/#sec-hostresolveimportedmodule">HostResolveImportedModule</a> impl…
66487 …c39.es/ecma262/#sec-moduledeclarationlinking">Link() concrete method</a> field of <a href="https:/…
66491 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Objec…
66495 <p>Corresponds to the <a href="https://tc39.es/ecma262/#sec-getmodulenamespace">GetModuleNamespace<…
66499 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" c…
66526 <a href="https://tc39.es/ecma262/#sec-cyclic-module-records">Cyclic Module Record</a>'s <code>[[Sta…
66533 … api_stability_1"><a href="#documentation_stability_index">Stability: 1</a> - Experimental</div><p…
66534 <p>This feature is only available with the <code>--experimental-vm-modules</code> command
66539 …le</code> class provides the <a href="https://tc39.es/ecma262/#sec-source-text-module-records">Sou…
66543 <li><code>code</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
66546 <li><code>identifier</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_s…
66547 <strong>Default:</strong> <code>'vm:module(i)'</code> where <code>i</code> is a context-specific as…
66549 …org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray" class="type">&#x3C;TypedArray><…
66553 <li><code>context</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference…
66555 <li><code>lineOffset</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_s…
66557 …developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;in…
66559 <li><code>initializeImportMeta</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScr…
66566 <li><code>importModuleDynamically</code> <a href="https://developer.mozilla.org/en-US/docs/Web/Java…
66570 <li><code>specifier</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_st…
66572 <li>Returns: <a href="https://tc39.github.io/ecma262/#sec-module-namespace-exotic-objects" class="t…
66585-flavor-selector" type="checkbox" checked aria-label="Show modern ES modules syntax"><code class="…
66587-keyword">const</span> contextifiedObject = vm.<span class="hljs-title function_">createContext</s…
66589 …ss="hljs-keyword">const</span> <span class="hljs-variable language_">module</span> = <span class="…
66590 <span class="hljs-string">'Object.getPrototypeOf(import.meta.prop).secret = secret;'</span>,
66592 …<span class="hljs-title function_">initializeImportMeta</span>(<span class="hljs-params">meta</spa…
66593 <span class="hljs-comment">// Note: this object is created in the top context. As such,</span>
66594 <span class="hljs-comment">// Object.getPrototypeOf(import.meta.prop) points to the</span>
66595 <span class="hljs-comment">// Object.prototype in the top context rather than that in</span>
66596 <span class="hljs-comment">// the contextified object.</span>
66597 meta.<span class="hljs-property">prop</span> = {};
66600 <span class="hljs-comment">// Since module has no dependencies, the linker function will never be c…
66601 …="hljs-keyword">await</span> <span class="hljs-variable language_">module</span>.<span class="hljs
66602 <span class="hljs-keyword">await</span> <span class="hljs-variable language_">module</span>.evaluat…
66604 <span class="hljs-comment">// Now, Object.prototype.secret will be equal to 42.</span>
66605 <span class="hljs-comment">//</span>
66606 <span class="hljs-comment">// To fix this problem, replace</span>
66607 <span class="hljs-comment">// meta.prop = {};</span>
66608 <span class="hljs-comment">// above with</span>
66609-comment">// meta.prop = vm.runInContext('{}', contextifiedObject);</span></code><code class="…
66610-keyword">const</span> contextifiedObject = vm.<span class="hljs-title function_">createContext</s…
66611 (<span class="hljs-keyword">async</span> () => {
66612 …ss="hljs-keyword">const</span> <span class="hljs-variable language_">module</span> = <span class="…
66613 <span class="hljs-string">'Object.getPrototypeOf(import.meta.prop).secret = secret;'</span>,
66615 …<span class="hljs-title function_">initializeImportMeta</span>(<span class="hljs-params">meta</spa…
66616 … <span class="hljs-comment">// Note: this object is created in the top context. As such,</span>
66617 <span class="hljs-comment">// Object.getPrototypeOf(import.meta.prop) points to the</span>
66618 <span class="hljs-comment">// Object.prototype in the top context rather than that in</span>
66619 <span class="hljs-comment">// the contextified object.</span>
66620 meta.<span class="hljs-property">prop</span> = {};
66623 …<span class="hljs-comment">// Since module has no dependencies, the linker function will never be …
66624 …="hljs-keyword">await</span> <span class="hljs-variable language_">module</span>.<span class="hljs
66625 …<span class="hljs-keyword">await</span> <span class="hljs-variable language_">module</span>.evalua…
66626 <span class="hljs-comment">// Now, Object.prototype.secret will be equal to 42.</span>
66627 <span class="hljs-comment">//</span>
66628 <span class="hljs-comment">// To fix this problem, replace</span>
66629 <span class="hljs-comment">// meta.prop = {};</span>
66630 <span class="hljs-comment">// above with</span>
66631 <span class="hljs-comment">// meta.prop = vm.runInContext('{}', contextifiedObject);</span>
66643 <pre><code class="language-js"><span class="hljs-comment">// Create an initial module</span>
66644-keyword">const</span> <span class="hljs-variable language_">module</span> = <span class="hljs-key…
66646 <span class="hljs-comment">// Create cached data from this module</span>
66647 <span class="hljs-keyword">const</span> cachedData = <span class="hljs-variable language_">module</…
66649 <span class="hljs-comment">// Create a new module using the cached data. The code must be the same.…
66650 …js-keyword">const</span> module2 = <span class="hljs-keyword">new</span> vm.<span class="hljs-titl…
66655 … api_stability_1"><a href="#documentation_stability_index">Stability: 1</a> - Experimental</div><p…
66656 <p>This feature is only available with the <code>--experimental-vm-modules</code> command
66661 …> class provides the <a href="https://heycam.github.io/webidl/#synthetic-module-records">Synthetic…
66663 provide a generic interface for exposing non-JavaScript sources to ECMAScript
66665 …<code class="language-js"><span class="hljs-keyword">const</span> vm = <span class="hljs-built_in"…
66667 <span class="hljs-keyword">const</span> source = <span class="hljs-string">'{ "a": 1 }'</span>;
66668-keyword">const</span> <span class="hljs-variable language_">module</span> = <span class="hljs-key…
66669 …<span class="hljs-keyword">const</span> obj = <span class="hljs-variable constant_">JSON</span>.<s…
66670 …<span class="hljs-variable language_">this</span>.<span class="hljs-title function_">setExport</sp…
66673 <span class="hljs-comment">// Use `module` in linking...</span></code></pre>
66679 <li><code>exportNames</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_…
66680 <li><code>evaluateCallback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/…
66683 <li><code>identifier</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_s…
66685 <strong>Default:</strong> <code>'vm:module(i)'</code> where <code>i</code> is a context-specific as…
66688 <li><code>context</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference…
66702 <li><code>name</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
66703 <li><code>value</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struct…
66709-flavor-selector" type="checkbox" checked aria-label="Show modern ES modules syntax"><code class="…
66711-keyword">const</span> m = <span class="hljs-keyword">new</span> vm.<span class="hljs-title class_…
66712 …m.<span class="hljs-title function_">setExport</span>(<span class="hljs-string">'x'</span>, <span …
66715 <span class="hljs-keyword">await</span> m.<span class="hljs-title function_">link</span>(<span clas…
66716 <span class="hljs-keyword">await</span> m.evaluate();
66718-title function_">strictEqual</span>(m.<span class="hljs-property">namespace</span>.<span class="h…
66719 (<span class="hljs-keyword">async</span> () => {
66720-keyword">const</span> m = <span class="hljs-keyword">new</span> vm.<span class="hljs-title class_…
66721 …m.<span class="hljs-title function_">setExport</span>(<span class="hljs-string">'x'</span>, <span …
66723 …<span class="hljs-keyword">await</span> m.<span class="hljs-title function_">link</span>(<span cla…
66724 <span class="hljs-keyword">await</span> m.evaluate();
66725 …="hljs-title function_">strictEqual</span>(m.<span class="hljs-property">namespace</span>.<span cl…
66742 <li><code>code</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
66743 <li><code>params</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struc…
66745 <li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference…
66747 <li><code>filename</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_str…
66749 <li><code>lineOffset</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_s…
66751 …developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;nu…
66753 …org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray" class="type">&#x3C;TypedArray><…
66756 <li><code>produceCachedData</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript…
66758 <li><code>parsingContext</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Re…
66760 <li><code>contextExtensions</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript…
66765 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Obje…
66787 <li><code>contextObject</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Ref…
66788 <li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference…
66790 …ttps://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&…
66793 …ozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> <…
66799 <li><code>codeGeneration</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Re…
66801 <li><code>strings</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_stru…
66804 <li><code>wasm</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
66808 <li><code>microtaskMode</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Dat…
66814 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Obje…
66820 properties but also having the built-in objects and functions any standard
66823 …<code class="language-js"><span class="hljs-keyword">const</span> vm = <span class="hljs-built_in"…
66825 <span class="hljs-variable language_">global</span>.<span class="hljs-property">globalVar</span> = …
66827 <span class="hljs-keyword">const</span> context = { <span class="hljs-attr">globalVar</span>: <span…
66828 vm.<span class="hljs-title function_">createContext</span>(context);
66830 vm.<span class="hljs-title function_">runInContext</span>(<span class="hljs-string">'globalVar *= 2…
66832 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(…
66833 <span class="hljs-comment">// Prints: { globalVar: 2 }</span>
66835-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hlj…
66836 <span class="hljs-comment">// Prints: 3</span></code></pre>
66851 <li><code>object</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/…
66852 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boole…
66860 … api_stability_1"><a href="#documentation_stability_index">Stability: 1</a> - Experimental</div><p…
66864 <li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference…
66866 <li><code>mode</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
66871 <li><code>execution</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_st…
66879 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Obje…
66889 …<code class="language-js"><span class="hljs-keyword">const</span> vm = <span class="hljs-built_in"…
66890 <span class="hljs-comment">// Measure the memory used by the main context.</span>
66891 vm.<span class="hljs-title function_">measureMemory</span>({ <span class="hljs-attr">mode</span>: <…
66892 <span class="hljs-comment">// This is the same as vm.measureMemory()</span>
66893 ….<span class="hljs-title function_">then</span>(<span class="hljs-function">(<span class="hljs-par…
66894 <span class="hljs-comment">// The current format is:</span>
66895 <span class="hljs-comment">// {</span>
66896 <span class="hljs-comment">// total: {</span>
66897 …<span class="hljs-comment">// jsMemoryEstimate: 2418479, jsMemoryRange: [ 2418479, 2745799 ]<…
66898 <span class="hljs-comment">// }</span>
66899 <span class="hljs-comment">// }</span>
66900 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>…
66903 …ljs-keyword">const</span> context = vm.<span class="hljs-title function_">createContext</span>({ <…
66904-title function_">measureMemory</span>({ <span class="hljs-attr">mode</span>: <span class="hljs-st…
66905 ….<span class="hljs-title function_">then</span>(<span class="hljs-function">(<span class="hljs-par…
66906 <span class="hljs-comment">// Reference the context here so that it won't be GC'ed</span>
66907 <span class="hljs-comment">// until the measurement is complete.</span>
66908 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>…
66909 <span class="hljs-comment">// {</span>
66910 <span class="hljs-comment">// total: {</span>
66911 <span class="hljs-comment">// jsMemoryEstimate: 2574732,</span>
66912 <span class="hljs-comment">// jsMemoryRange: [ 2574732, 2904372 ]</span>
66913 <span class="hljs-comment">// },</span>
66914 <span class="hljs-comment">// current: {</span>
66915 <span class="hljs-comment">// jsMemoryEstimate: 2438996,</span>
66916 <span class="hljs-comment">// jsMemoryRange: [ 2438996, 2768636 ]</span>
66917 <span class="hljs-comment">// },</span>
66918 <span class="hljs-comment">// other: [</span>
66919 <span class="hljs-comment">// {</span>
66920 <span class="hljs-comment">// jsMemoryEstimate: 135736,</span>
66921 <span class="hljs-comment">// jsMemoryRange: [ 135736, 465376 ]</span>
66922 <span class="hljs-comment">// }</span>
66923 <span class="hljs-comment">// ]</span>
66924 <span class="hljs-comment">// }</span>
66925 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>…
66940 <li><code>code</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
66941 <li><code>contextifiedObject</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScrip…
66943 …lla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type">&#x3C;Object></a> …
66945 <li><code>filename</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_str…
66947 <li><code>lineOffset</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_s…
66949 …developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;nu…
66951 <li><code>displayErrors</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Dat…
66954 <li><code>timeout</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_stru…
66957 <li><code>breakOnSigint</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Dat…
66962 …org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray" class="type">&#x3C;TypedArray><…
66966 <li><code>produceCachedData</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript…
66974 <li><code>importModuleDynamically</code> <a href="https://developer.mozilla.org/en-US/docs/Web/Java…
66980 <li><code>specifier</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_st…
66982 <li>Returns: <a href="https://tc39.github.io/ecma262/#sec-module-namespace-exotic-objects" class="t…
66989 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Data_…
66998 …<code class="language-js"><span class="hljs-keyword">const</span> vm = <span class="hljs-built_in"…
67000 <span class="hljs-keyword">const</span> contextObject = { <span class="hljs-attr">globalVar</span>:…
67001 vm.<span class="hljs-title function_">createContext</span>(contextObject);
67003 …n class="hljs-keyword">for</span> (<span class="hljs-keyword">let</span> i = <span class="hljs-num…
67004 …vm.<span class="hljs-title function_">runInContext</span>(<span class="hljs-string">'globalVar *= …
67006 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(…
67007 <span class="hljs-comment">// Prints: { globalVar: 1024 }</span></code></pre>
67025 <li><code>code</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
67026 <li><code>contextObject</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Ref…
67028 …lla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type">&#x3C;Object></a> …
67030 <li><code>filename</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_str…
67032 <li><code>lineOffset</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_s…
67034 …developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;nu…
67036 <li><code>displayErrors</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Dat…
67039 <li><code>timeout</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_stru…
67042 <li><code>breakOnSigint</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Dat…
67047 …ttps://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&…
67050 …ozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> <…
67056 <li><code>contextCodeGeneration</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaSc…
67058 <li><code>strings</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_stru…
67061 <li><code>wasm</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
67065 …org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray" class="type">&#x3C;TypedArray><…
67069 <li><code>produceCachedData</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript…
67077 <li><code>importModuleDynamically</code> <a href="https://developer.mozilla.org/en-US/docs/Web/Java…
67083 <li><code>specifier</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_st…
67085 <li>Returns: <a href="https://tc39.github.io/ecma262/#sec-module-namespace-exotic-objects" class="t…
67090 <li><code>microtaskMode</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Dat…
67096 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Data_…
67105 …<code class="language-js"><span class="hljs-keyword">const</span> vm = <span class="hljs-built_in"…
67107 <span class="hljs-keyword">const</span> contextObject = {
67108 <span class="hljs-attr">animal</span>: <span class="hljs-string">'cat'</span>,
67109 <span class="hljs-attr">count</span>: <span class="hljs-number">2</span>
67112 vm.<span class="hljs-title function_">runInNewContext</span>(<span class="hljs-string">'count += 1;…
67113 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(…
67114 <span class="hljs-comment">// Prints: { animal: 'cat', count: 3, name: 'kitty' }</span></code></pre>
67128 <li><code>code</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
67129 …lla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type">&#x3C;Object></a> …
67131 <li><code>filename</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_str…
67133 <li><code>lineOffset</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_s…
67135 …developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;nu…
67137 <li><code>displayErrors</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Dat…
67140 <li><code>timeout</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_stru…
67143 <li><code>breakOnSigint</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Dat…
67148 …org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray" class="type">&#x3C;TypedArray><…
67152 <li><code>produceCachedData</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript…
67160 <li><code>importModuleDynamically</code> <a href="https://developer.mozilla.org/en-US/docs/Web/Java…
67166 <li><code>specifier</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_st…
67168 <li>Returns: <a href="https://tc39.github.io/ecma262/#sec-module-namespace-exotic-objects" class="t…
67175 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Data_…
67182 the JavaScript <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Ob…
67183 <!-- eslint-disable prefer-const -->
67184 …<code class="language-js"><span class="hljs-keyword">const</span> vm = <span class="hljs-built_in"…
67185 <span class="hljs-keyword">let</span> localVar = <span class="hljs-string">'initial value'</span>;
67187 <span class="hljs-keyword">const</span> vmResult = vm.<span class="hljs-title function_">runInThisC…
67188-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hlj…
67189 <span class="hljs-comment">// Prints: vmResult: 'vm', localVar: 'initial value'</span>
67191 <span class="hljs-keyword">const</span> evalResult = <span class="hljs-built_in">eval</span>(<span …
67192-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hlj…
67193 <span class="hljs-comment">// Prints: evalResult: 'eval', localVar: 'eval'</span></code></pre>
67195 <code>localVar</code> is unchanged. In contrast, <a href="https://developer.mozilla.org/en-US/docs/…
67206 <pre><code class="language-js"><span class="hljs-meta">'use strict'</span>;
67207 <span class="hljs-keyword">const</span> vm = <span class="hljs-built_in">require</span>(<span class…
67209 <span class="hljs-keyword">const</span> code = <span class="hljs-string">`
67214 response.writeHead(200, { 'Content-Type': 'text/plain' });
67221 vm.<span class="hljs-title function_">runInThisContext</span>(code)(<span class="hljs-built_in">req…
67234 (or a newly-created object if <code>contextObject</code> is <code>undefined</code>) is associated
67249 …<code class="language-js"><span class="hljs-keyword">const</span> vm = <span class="hljs-built_in"…
67251 <span class="hljs-keyword">function</span> <span class="hljs-title function_">loop</span>(<span cla…
67252 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>…
67253-keyword">while</span> (<span class="hljs-number">1</span>) <span class="hljs-variable language_">…
67256 vm.<span class="hljs-title function_">runInNewContext</span>(
67257 <span class="hljs-string">'Promise.resolve().then(() => loop());'</span>,
67258 { loop, <span class="hljs-variable language_">console</span> },
67259 { <span class="hljs-attr">timeout</span>: <span class="hljs-number">5</span> }
67261 <span class="hljs-comment">// This is printed *before* 'entering loop' (!)</span>
67262 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(…
67265 …<code class="language-js"><span class="hljs-keyword">const</span> vm = <span class="hljs-built_in"…
67267 <span class="hljs-keyword">function</span> <span class="hljs-title function_">loop</span>(<span cla…
67268-keyword">while</span> (<span class="hljs-number">1</span>) <span class="hljs-variable language_">…
67271 vm.<span class="hljs-title function_">runInNewContext</span>(
67272 <span class="hljs-string">'Promise.resolve().then(() => loop());'</span>,
67273 { loop, <span class="hljs-variable language_">console</span> },
67274 …pan class="hljs-attr">timeout</span>: <span class="hljs-number">5</span>, <span class="hljs-attr">…
67293 … api_stability_1"><a href="#documentation_stability_index">Stability: 1</a> - Experimental</div><p…
67297 underlying operating system via a collection of POSIX-like functions.</p>
67299-flavor-selector" type="checkbox" checked aria-label="Show modern ES modules syntax"><code class="…
67300 …ass="hljs-keyword">import</span> { <span class="hljs-variable constant_">WASI</span> } <span class…
67302 <span class="hljs-keyword">const</span> wasi = <span class="hljs-keyword">new</span> <span class="h…
67303 <span class="hljs-attr">args</span>: process.<span class="hljs-property">argv</span>,
67304 <span class="hljs-attr">env</span>: process.<span class="hljs-property">env</span>,
67305 <span class="hljs-attr">preopens</span>: {
67306 …<span class="hljs-string">'/sandbox'</span>: <span class="hljs-string">'/some/real/path/that/wasm/…
67309 <span class="hljs-keyword">const</span> importObject = { <span class="hljs-attr">wasi_snapshot_prev…
67311-keyword">const</span> wasm = <span class="hljs-keyword">await</span> <span class="hljs-title clas…
67312 …"hljs-keyword">const</span> instance = <span class="hljs-keyword">await</span> <span class="hljs-t…
67314 wasi.<span class="hljs-title function_">start</span>(instance);</code><code class="language-js cjs"…
67315 <span class="hljs-keyword">const</span> fs = <span class="hljs-built_in">require</span>(<span class…
67316 …="hljs-keyword">const</span> { <span class="hljs-variable constant_">WASI</span> } = <span class="…
67317 <span class="hljs-keyword">const</span> wasi = <span class="hljs-keyword">new</span> <span class="h…
67318 <span class="hljs-attr">args</span>: process.<span class="hljs-property">argv</span>,
67319 <span class="hljs-attr">env</span>: process.<span class="hljs-property">env</span>,
67320 <span class="hljs-attr">preopens</span>: {
67321 …<span class="hljs-string">'/sandbox'</span>: <span class="hljs-string">'/some/real/path/that/wasm/…
67324 <span class="hljs-keyword">const</span> importObject = { <span class="hljs-attr">wasi_snapshot_prev…
67326 (<span class="hljs-keyword">async</span> () => {
67327-keyword">const</span> wasm = <span class="hljs-keyword">await</span> <span class="hljs-title clas…
67328 …"hljs-keyword">const</span> instance = <span class="hljs-keyword">await</span> <span class="hljs-t…
67330 wasi.<span class="hljs-title function_">start</span>(instance);
67334 <pre><code class="language-text">(module
67337 ;; (File Descriptor, *iovs, iovs_len, nwritten) -> Returns number of bytes written
67349 …(i32.store (i32.const 0) (i32.const 8)) ;; iov.iov_base - This is a pointer to the start of the '…
67350 …(i32.store (i32.const 4) (i32.const 12)) ;; iov.iov_len - The length of the 'hello world\n' string
67353 (i32.const 1) ;; file_descriptor - 1 for stdout
67354 … (i32.const 0) ;; *iovs - The pointer to the iov array, which is stored at memory location 0
67355 (i32.const 1) ;; iovs_len - We're printing 1 string stored in an iov - so one.
67356 (i32.const 20) ;; nwritten - A place in memory to store the number of bytes written
67362 <pre><code class="language-console"><span class="hljs-meta">$ </span><span class="language-bash">wa…
67363 <p>The <code>--experimental-wasi-unstable-preview1</code> and <code>--experimental-wasm-bigint</cod…
67370 methods for working with WASI-based applications. Each <code>WASI</code> instance
67372 instance must have its command-line arguments, environment variables, and
67379 <li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference…
67381 <li><code>args</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Gl…
67382 see as command-line arguments. The first argument is the virtual path to the
67384 <li><code>env</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Glo…
67386 <li><code>preopens</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Referenc…
67390 <li><code>returnOnExit</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data…
67394 <li><code>stdin</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struct…
67396 <li><code>stdout</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struc…
67398 <li><code>stderr</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struc…
67408 <li><code>instance</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Referenc…
67413 …at <code>instance</code> exports a <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScrip…
67421 <li><code>instance</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Referenc…
67426 …at <code>instance</code> exports a <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScrip…
67434 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Objec…
67438 of a <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/WebA…
67442 …ty api_stability_2"><a href="#documentation_stability_index">Stability: 2</a> - Stable</div><p></p>
67446 …ode class="language-js"><span class="hljs-keyword">const</span> worker = <span class="hljs-built_i…
67447 <p>Workers (threads) are useful for performing CPU-intensive JavaScript operations.
67448 They will not help much with I/O-intensive work. Node.js’s built-in asynchronous
67453 <pre><code class="language-js"><span class="hljs-keyword">const</span> {
67454 <span class="hljs-title class_">Worker</span>, isMainThread, parentPort, workerData
67455 } = <span class="hljs-built_in">require</span>(<span class="hljs-string">'worker_threads'</span>);
67457 <span class="hljs-keyword">if</span> (isMainThread) {
67458-variable language_">module</span>.<span class="hljs-property">exports</span> = <span class="hljs-
67459-keyword">return</span> <span class="hljs-keyword">new</span> <span class="hljs-title class_">Prom…
67460 …<span class="hljs-keyword">const</span> worker = <span class="hljs-keyword">new</span> <span class…
67461 <span class="hljs-attr">workerData</span>: script
67463 …worker.<span class="hljs-title function_">on</span>(<span class="hljs-string">'message'</span>, re…
67464 …worker.<span class="hljs-title function_">on</span>(<span class="hljs-string">'error'</span>, reje…
67465 ….<span class="hljs-title function_">on</span>(<span class="hljs-string">'exit'</span>, <span class…
67466 <span class="hljs-keyword">if</span> (code !== <span class="hljs-number">0</span>)
67467-title function_">reject</span>(<span class="hljs-keyword">new</span> <span class="hljs-title clas…
67471 } <span class="hljs-keyword">else</span> {
67472 …span class="hljs-keyword">const</span> { parse } = <span class="hljs-built_in">require</span>(<spa…
67473 <span class="hljs-keyword">const</span> script = workerData;
67474 …parentPort.<span class="hljs-title function_">postMessage</span>(<span class="hljs-title function_…
67482 <a href="#async-resource-worker-pool">"Using <code>AsyncResource</code> for a <code>Worker</code> t…
67484 <p>Worker threads inherit non-process-specific options by default. Refer to
67491 … api_stability_1"><a href="#documentation_stability_index">Stability: 1</a> - Experimental</div><p…
67493 <li><code>key</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structur…
67494 <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Map" clas…
67495 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Data_…
67501 <pre><code class="language-js"><span class="hljs-keyword">const</span> {
67502 <span class="hljs-title class_">Worker</span>,
67506 } = <span class="hljs-built_in">require</span>(<span class="hljs-string">'worker_threads'</span>);
67508 <span class="hljs-keyword">if</span> (isMainThread) {
67509 …<span class="hljs-title function_">setEnvironmentData</span>(<span class="hljs-string">'Hello'</sp…
67510 …<span class="hljs-keyword">const</span> worker = <span class="hljs-keyword">new</span> <span class…
67511 } <span class="hljs-keyword">else</span> {
67512-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hlj…
67519 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" …
67522-js"><span class="hljs-keyword">const</span> { <span class="hljs-title class_">Worker</span>, isMa…
67524 <span class="hljs-keyword">if</span> (isMainThread) {
67525 <span class="hljs-comment">// This re-loads the current file inside a Worker instance.</span>
67526 <span class="hljs-keyword">new</span> <span class="hljs-title class_">Worker</span>(__filename);
67527 } <span class="hljs-keyword">else</span> {
67528 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>…
67529 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>…
67542-js"><span class="hljs-keyword">const</span> { <span class="hljs-title class_">MessageChannel</spa…
67544 …hljs-keyword">const</span> pooledBuffer = <span class="hljs-keyword">new</span> <span class="hljs-
67545 …ss="hljs-keyword">const</span> typedArray1 = <span class="hljs-keyword">new</span> <span class="hl…
67546 …s="hljs-keyword">const</span> typedArray2 = <span class="hljs-keyword">new</span> <span class="hlj…
67548 <span class="hljs-title function_">markAsUntransferable</span>(pooledBuffer);
67550 <span class="hljs-keyword">const</span> { port1 } = <span class="hljs-keyword">new</span> <span cla…
67551 port1.<span class="hljs-title function_">postMessage</span>(typedArray1, [ typedArray1.<span class=…
67553 <span class="hljs-comment">// The following line prints the contents of typedArray1 -- it still own…
67554 <span class="hljs-comment">// its memory and has been cloned, not transferred. Without</span>
67555 <span class="hljs-comment">// `markAsUntransferable()`, this would print an empty Uint8Array.</span>
67556 <span class="hljs-comment">// typedArray2 is intact as well.</span>
67557 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(…
67558 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(…
67569 <p><code>contextifiedSandbox</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScrip…
67579 <p>The returned <code>MessagePort</code> will be an object in the target context, and will
67581 …per.mozilla.org/en-US/docs/Web/API/MessagePort/onmessage"><code>port.onmessage()</code></a> listen…
67584 …eloper.mozilla.org/en-US/docs/Web/API/EventTarget"><code>EventTarget</code></a>, and only <a href=…
67591 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Null_type" cla…
67599-js"><span class="hljs-keyword">const</span> { <span class="hljs-title class_">Worker</span>, isMa…
67601 <span class="hljs-keyword">if</span> (isMainThread) {
67602 …<span class="hljs-keyword">const</span> worker = <span class="hljs-keyword">new</span> <span class…
67603 …span class="hljs-title function_">once</span>(<span class="hljs-string">'message'</span>, <span cl…
67604 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>…
67606 …worker.<span class="hljs-title function_">postMessage</span>(<span class="hljs-string">'Hello, wor…
67607 } <span class="hljs-keyword">else</span> {
67608 …<span class="hljs-comment">// When a message from the parent thread is received, send it back:</sp…
67609 …span class="hljs-title function_">once</span>(<span class="hljs-string">'message'</span>, <span cl…
67610 parentPort.<span class="hljs-title function_">postMessage</span>(message);
67622 …lla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type">&#x3C;Object></a> …
67629-js"><span class="hljs-keyword">const</span> { <span class="hljs-title class_">MessageChannel</spa…
67630 <span class="hljs-keyword">const</span> { port1, port2 } = <span class="hljs-keyword">new</span> <s…
67631 port1.<span class="hljs-title function_">postMessage</span>({ <span class="hljs-attr">hello</span>:…
67633 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(…
67634 <span class="hljs-comment">// Prints: { message: { hello: 'world' } }</span>
67635 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(…
67636 <span class="hljs-comment">// Prints: undefined</span></code></pre>
67644 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Objec…
67646 <li><code>maxYoungGenerationSizeMb</code> <a href="https://developer.mozilla.org/en-US/docs/Web/Jav…
67647 <li><code>maxOldGenerationSizeMb</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaS…
67648 <li><code>codeRangeSizeMb</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/D…
67649 <li><code>stackSizeMb</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_…
67662 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Symbol_type" c…
67667-js"><span class="hljs-keyword">const</span> { <span class="hljs-title class_">Worker</span>, <spa…
67668-keyword">new</span> <span class="hljs-title class_">Worker</span>(<span class="hljs-string">'proc…
67669 ….<span class="hljs-title function_">on</span>(<span class="hljs-string">'exit'</span>, <span class…
67670-variable language_">console</span>.<span class="hljs-title function_">log</span>(process.<span cl…
67676 … api_stability_1"><a href="#documentation_stability_index">Stability: 1</a> - Experimental</div><p…
67678 <li><code>key</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structur…
67679 <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Map" clas…
67680 <li><code>value</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struct…
67692 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" c…
67704 according to the <a href="https://developer.mozilla.org/en-US/docs/Web/API/Web_Workers_API/Structur…
67705-js"><span class="hljs-keyword">const</span> { <span class="hljs-title class_">Worker</span>, isMa…
67707 <span class="hljs-keyword">if</span> (isMainThread) {
67708-keyword">const</span> worker = <span class="hljs-keyword">new</span> <span class="hljs-title clas…
67709 } <span class="hljs-keyword">else</span> {
67710 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>…
67717 two-way communications channel.
67721-js"><span class="hljs-keyword">const</span> { <span class="hljs-title class_">MessageChannel</spa…
67723 <span class="hljs-keyword">const</span> { port1, port2 } = <span class="hljs-keyword">new</span> <s…
67724-title function_">on</span>(<span class="hljs-string">'message'</span>, <span class="hljs-function…
67725 port2.<span class="hljs-title function_">postMessage</span>({ <span class="hljs-attr">foo</span>: <…
67726 <span class="hljs-comment">// Prints: received { foo: 'bar' } from the `port1.on('message')` listen…
67743 asynchronous, two-way communications channel. It can be used to transfer
67746 <p>This implementation matches <a href="https://developer.mozilla.org/en-US/docs/Web/API/MessagePor…
67753-js"><span class="hljs-keyword">const</span> { <span class="hljs-title class_">MessageChannel</spa…
67754 <span class="hljs-keyword">const</span> { port1, port2 } = <span class="hljs-keyword">new</span> <s…
67756 <span class="hljs-comment">// Prints:</span>
67757 <span class="hljs-comment">// foobar</span>
67758 <span class="hljs-comment">// closed!</span>
67759-title function_">on</span>(<span class="hljs-string">'message'</span>, <span class="hljs-function…
67760-title function_">on</span>(<span class="hljs-string">'close'</span>, <span class="hljs-function">…
67762 port1.<span class="hljs-title function_">postMessage</span>(<span class="hljs-string">'foobar'</spa…
67763 port1.<span class="hljs-title function_">close</span>();</code></pre>
67769 <li><code>value</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struct…
67780 <li><code>error</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/G…
67816 <li><code>value</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struct…
67817 <li><code>transferList</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Refe…
67821 the <a href="https://developer.mozilla.org/en-US/docs/Web/API/Web_Workers_API/Structured_clone_algo…
67829 <li><code>value</code> may contain <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript…
67830 <li><code>value</code> may not contain native (C++-backed) objects other than:
67841-js"><span class="hljs-keyword">const</span> { <span class="hljs-title class_">MessageChannel</spa…
67842 <span class="hljs-keyword">const</span> { port1, port2 } = <span class="hljs-keyword">new</span> <s…
67844-title function_">on</span>(<span class="hljs-string">'message'</span>, <span class="hljs-function…
67846 <span class="hljs-keyword">const</span> circularData = {};
67847 circularData.<span class="hljs-property">foo</span> = circularData;
67848 <span class="hljs-comment">// Prints: { foo: [Circular] }</span>
67849 port2.<span class="hljs-title function_">postMessage</span>(circularData);</code></pre>
67850 <p><code>transferList</code> may be a list of <a href="https://developer.mozilla.org/en-US/docs/Web…
67856 <p>If <code>value</code> contains <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/…
67860-js"><span class="hljs-keyword">const</span> { <span class="hljs-title class_">MessageChannel</spa…
67861 <span class="hljs-keyword">const</span> { port1, port2 } = <span class="hljs-keyword">new</span> <s…
67863-title function_">on</span>(<span class="hljs-string">'message'</span>, <span class="hljs-function…
67865-keyword">const</span> uint8Array = <span class="hljs-keyword">new</span> <span class="hljs-title …
67866 <span class="hljs-comment">// This posts a copy of `uint8Array`:</span>
67867 port2.<span class="hljs-title function_">postMessage</span>(uint8Array);
67868 <span class="hljs-comment">// This does not copy data, but renders `uint8Array` unusable:</span>
67869 port2.<span class="hljs-title function_">postMessage</span>(uint8Array, [ uint8Array.<span class="h…
67871 <span class="hljs-comment">// The memory for the `sharedUint8Array` will be accessible from both th…
67872 <span class="hljs-comment">// original and the copy received by `.on('message')`:</span>
67873-keyword">const</span> sharedUint8Array = <span class="hljs-keyword">new</span> <span class="hljs-
67874 port2.<span class="hljs-title function_">postMessage</span>(sharedUint8Array);
67876 <span class="hljs-comment">// This transfers a freshly created message port to the receiver.</span>
67877 <span class="hljs-comment">// This can be used, for example, to create communication channels betwe…
67878 <span class="hljs-comment">// multiple `Worker` threads that are children of the same parent thread…
67879 <span class="hljs-keyword">const</span> otherChannel = <span class="hljs-keyword">new</span> <span …
67880-title function_">postMessage</span>({ <span class="hljs-attr">port</span>: otherChannel.<span cla…
67882 non-enumerable properties, property accessors, and object prototypes are
67884 plain <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Uin…
67898-js"><span class="hljs-keyword">const</span> ab = <span class="hljs-keyword">new</span> <span clas…
67900 … class="hljs-keyword">const</span> u1 = <span class="hljs-keyword">new</span> <span class="hljs-ti…
67901 … class="hljs-keyword">const</span> u2 = <span class="hljs-keyword">new</span> <span class="hljs-ti…
67903 …js-variable language_">console</span>.<span class="hljs-title function_">log</span>(u2.<span class…
67905 port.<span class="hljs-title function_">postMessage</span>(u1, [u1.<span class="hljs-property">buff…
67907 …js-variable language_">console</span>.<span class="hljs-title function_">log</span>(u2.<span class…
67980 threads, and will not be visible to native add-ons (unless
67989 <li>Native add-ons can only be loaded from multiple threads if they fulfill
67993 …er.mozilla.org/en-US/docs/Web/API/Web_Workers_API">Web Workers</a> and the <a href="cluster.html">…
67994 achieved through inter-thread message passing. Internally, a <code>Worker</code> has a
67995 built-in pair of <a href="#worker_threads_class_messageport"><code>MessagePort</code></a>s that are…
68004 pre-existing channel, such as the global one.</p>
68008 …ode class="language-js"><span class="hljs-keyword">const</span> assert = <span class="hljs-built_i…
68009 <span class="hljs-keyword">const</span> {
68010 …<span class="hljs-title class_">Worker</span>, <span class="hljs-title class_">MessageChannel</spa…
68011 } = <span class="hljs-built_in">require</span>(<span class="hljs-string">'worker_threads'</span>);
68012 <span class="hljs-keyword">if</span> (isMainThread) {
68013 …<span class="hljs-keyword">const</span> worker = <span class="hljs-keyword">new</span> <span class…
68014 …<span class="hljs-keyword">const</span> subChannel = <span class="hljs-keyword">new</span> <span c…
68015-title function_">postMessage</span>({ <span class="hljs-attr">hereIsYourPort</span>: subChannel.<…
68016-property">port2</span>.<span class="hljs-title function_">on</span>(<span class="hljs-string">'me…
68017 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>…
68019 } <span class="hljs-keyword">else</span> {
68020 …span class="hljs-title function_">once</span>(<span class="hljs-string">'message'</span>, <span cl…
68021 …s-title function_">assert</span>(value.<span class="hljs-property">hereIsYourPort</span> <span cla…
68022 …value.<span class="hljs-property">hereIsYourPort</span>.<span class="hljs-title function_">postMes…
68023 …value.<span class="hljs-property">hereIsYourPort</span>.<span class="hljs-title function_">close</…
68051 <li><code>filename</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_str…
68055 When using a <a href="https://developer.mozilla.org/en-US/docs/Web/HTTP/Basics_of_HTTP/Data_URIs"><…
68059 <li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference…
68061 <li><code>argv</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
68065 <li><code>env</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Glo…
68070 <li><code>eval</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
68073 <li><code>execArgv</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_str…
68074 V8 options (such as <code>--max-old-space-size</code>) and options that affect the
68075 process (such as <code>--title</code>) are not supported. If set, this will be provided
68078 <li><code>stdin</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struct…
68081 <li><code>stdout</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struc…
68083 <li><code>stderr</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struc…
68085 <li><code>workerData</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_s…
68087 occur as described in the <a href="https://developer.mozilla.org/en-US/docs/Web/API/Web_Workers_API…
68090 <li><code>trackUnmanagedFds</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript…
68096 …illa.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type">&#x3C;Object[]></…
68100 <li><code>resourceLimits</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Re…
68104 may still abort if it encounters a global out-of-memory situation.
68106 <li><code>maxOldGenerationSizeMb</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaS…
68107 <li><code>maxYoungGenerationSizeMb</code> <a href="https://developer.mozilla.org/en-US/docs/Web/Jav…
68109 …/developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;n…
68111 <li><code>stackSizeMb</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_…
68123 <li><code>err</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Glo…
68132 <li><code>exitCode</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_str…
68144 <li><code>value</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struct…
68156 <li><code>error</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/G…
68170 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Obje…
68189 <li><code>utilization1</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Refe…
68191 <li><code>utilization2</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Refe…
68193 <li>Returns <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objec…
68195 <li><code>idle</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
68196 <li><code>active</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struc…
68197 <li><code>utilization</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_…
68210-js"><span class="hljs-keyword">const</span> { <span class="hljs-title class_">Worker</span>, isMa…
68212 <span class="hljs-keyword">if</span> (isMainThread) {
68213 …<span class="hljs-keyword">const</span> worker = <span class="hljs-keyword">new</span> <span class…
68214 <span class="hljs-built_in">setInterval</span>(<span class="hljs-function">() =></span> {
68215 …worker.<span class="hljs-title function_">postMessage</span>(<span class="hljs-string">'hi'</span>…
68216-variable language_">console</span>.<span class="hljs-title function_">log</span>(worker.<span cla…
68217 }, <span class="hljs-number">100</span>).<span class="hljs-title function_">unref</span>();
68218 <span class="hljs-keyword">return</span>;
68221-title function_">on</span>(<span class="hljs-string">'message'</span>, <span class="hljs-function…
68222 (<span class="hljs-keyword">function</span> <span class="hljs-title function_">r</span>(<span class…
68223 …<span class="hljs-keyword">if</span> (--n &#x3C; <span class="hljs-number">0</span>) <span class="…
68224 …<span class="hljs-keyword">const</span> t = <span class="hljs-title class_">Date</span>.<span clas…
68225 …hljs-keyword">while</span> (<span class="hljs-title class_">Date</span>.<span class="hljs-title fu…
68226 <span class="hljs-title function_">setImmediate</span>(r, n);
68227 })(<span class="hljs-number">10</span>);</code></pre>
68236 <li><code>value</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struct…
68237 <li><code>transferList</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Refe…
68255 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Objec…
68257 <li><code>maxYoungGenerationSizeMb</code> <a href="https://developer.mozilla.org/en-US/docs/Web/Jav…
68258 <li><code>maxOldGenerationSizeMb</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaS…
68259 <li><code>codeRangeSizeMb</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/D…
68260 <li><code>stackSizeMb</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_…
68284 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Null_type" cla…
68313 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Obje…
68323 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" c…
68342 … class="js-flavor-selector" type="checkbox" checked aria-label="Show modern ES modules syntax"><co…
68343 <span class="hljs-title class_">Worker</span>,
68345 } <span class="hljs-keyword">from</span> <span class="hljs-string">'worker_threads'</span>;
68347 <span class="hljs-keyword">if</span> (isMainThread) {
68348-keyword">new</span> <span class="hljs-title class_">Worker</span>(<span class="hljs-keyword">new<…
68349 …n class="hljs-keyword">for</span> (<span class="hljs-keyword">let</span> n = <span class="hljs-num…
68350 } <span class="hljs-keyword">else</span> {
68351 …<span class="hljs-comment">// This output will be blocked by the for loop in the main thread.</spa…
68352 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>…
68353 }</code><code class="language-js cjs"><span class="hljs-meta">'use strict'</span>;
68355 <span class="hljs-keyword">const</span> {
68356 <span class="hljs-title class_">Worker</span>,
68358 } = <span class="hljs-built_in">require</span>(<span class="hljs-string">'worker_threads'</span>);
68360 <span class="hljs-keyword">if</span> (isMainThread) {
68361 <span class="hljs-keyword">new</span> <span class="hljs-title class_">Worker</span>(__filename);
68362 …n class="hljs-keyword">for</span> (<span class="hljs-keyword">let</span> n = <span class="hljs-num…
68363 } <span class="hljs-keyword">else</span> {
68364 …<span class="hljs-comment">// This output will be blocked by the for loop in the main thread.</spa…
68365 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>…
68369 and run using the <code>-r</code> command line flag). Unless the <code>execArgv</code> option is
68377 …ty api_stability_2"><a href="#documentation_stability_index">Stability: 2</a> - Stable</div><p></p>
68382 …code class="language-js"><span class="hljs-keyword">const</span> zlib = <span class="hljs-built_in…
68387 …class="language-js"><span class="hljs-keyword">const</span> { createGzip } = <span class="hljs-bui…
68388 <span class="hljs-keyword">const</span> { pipeline } = <span class="hljs-built_in">require</span>(<…
68389 <span class="hljs-keyword">const</span> {
68392 } = <span class="hljs-built_in">require</span>(<span class="hljs-string">'fs'</span>);
68394 <span class="hljs-keyword">const</span> gzip = <span class="hljs-title function_">createGzip</span>…
68395 <span class="hljs-keyword">const</span> source = <span class="hljs-title function_">createReadStrea…
68396 <span class="hljs-keyword">const</span> destination = <span class="hljs-title function_">createWrit…
68398 <span class="hljs-title function_">pipeline</span>(source, gzip, destination, <span class="hljs-fun…
68399 <span class="hljs-keyword">if</span> (err) {
68400 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">error</spa…
68401 process.<span class="hljs-property">exitCode</span> = <span class="hljs-number">1</span>;
68405 <span class="hljs-comment">// Or, Promisified</span>
68407 <span class="hljs-keyword">const</span> { promisify } = <span class="hljs-built_in">require</span>(…
68408 <span class="hljs-keyword">const</span> pipe = <span class="hljs-title function_">promisify</span>(…
68410 …ass="hljs-keyword">async</span> <span class="hljs-keyword">function</span> <span class="hljs-title…
68411 …<span class="hljs-keyword">const</span> gzip = <span class="hljs-title function_">createGzip</span…
68412 …<span class="hljs-keyword">const</span> source = <span class="hljs-title function_">createReadStre…
68413 …<span class="hljs-keyword">const</span> destination = <span class="hljs-title function_">createWri…
68414 …<span class="hljs-keyword">await</span> <span class="hljs-title function_">pipe</span>(source, gzi…
68417 <span class="hljs-title function_">do_gzip</span>(<span class="hljs-string">'input.txt'</span>, <sp…
68418 ….<span class="hljs-title function_">catch</span>(<span class="hljs-function">(<span class="hljs-pa…
68419 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">error</spa…
68420 process.<span class="hljs-property">exitCode</span> = <span class="hljs-number">1</span>;
68423 …ass="language-js"><span class="hljs-keyword">const</span> { deflate, unzip } = <span class="hljs-b…
68425 <span class="hljs-keyword">const</span> input = <span class="hljs-string">'........................…
68426 <span class="hljs-title function_">deflate</span>(input, <span class="hljs-function">(<span class="…
68427 <span class="hljs-keyword">if</span> (err) {
68428 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">error</spa…
68429 process.<span class="hljs-property">exitCode</span> = <span class="hljs-number">1</span>;
68431-variable language_">console</span>.<span class="hljs-title function_">log</span>(buffer.<span cla…
68434-keyword">const</span> buffer = <span class="hljs-title class_">Buffer</span>.<span class="hljs-ti…
68435 <span class="hljs-title function_">unzip</span>(buffer, <span class="hljs-function">(<span class="h…
68436 <span class="hljs-keyword">if</span> (err) {
68437 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">error</spa…
68438 process.<span class="hljs-property">exitCode</span> = <span class="hljs-number">1</span>;
68440 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>…
68443 <span class="hljs-comment">// Or, Promisified</span>
68445 <span class="hljs-keyword">const</span> { promisify } = <span class="hljs-built_in">require</span>(…
68446 <span class="hljs-keyword">const</span> do_unzip = <span class="hljs-title function_">promisify</sp…
68448 <span class="hljs-title function_">do_unzip</span>(buffer)
68449-title function_">then</span>(<span class="hljs-function">(<span class="hljs-params">buf</span>) =…
68450 ….<span class="hljs-title function_">catch</span>(<span class="hljs-function">(<span class="hljs-pa…
68451 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">error</spa…
68452 process.<span class="hljs-property">exitCode</span> = <span class="hljs-number">1</span>;
68460 …code class="language-js"><span class="hljs-keyword">const</span> zlib = <span class="hljs-built_in…
68462 …hljs-keyword">const</span> payload = <span class="hljs-title class_">Buffer</span>.<span class="hl…
68464 <span class="hljs-comment">// WARNING: DO NOT DO THIS!</span>
68465 …n class="hljs-keyword">for</span> (<span class="hljs-keyword">let</span> i = <span class="hljs-num…
68466 …zlib.<span class="hljs-title function_">deflate</span>(payload, <span class="hljs-function">(<span…
68475 and <code>br</code> content-encoding mechanisms defined by
68476 <a href="https://tools.ietf.org/html/rfc7230#section-4.2">HTTP</a>.</p>
68477 <p>The HTTP <a href="https://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.3"><code>Accept-
68478 … The <a href="https://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.11"><code>Content-Enco…
68485 <pre><code class="language-js"><span class="hljs-comment">// Client request example</span>
68486 <span class="hljs-keyword">const</span> zlib = <span class="hljs-built_in">require</span>(<span cla…
68487 <span class="hljs-keyword">const</span> http = <span class="hljs-built_in">require</span>(<span cla…
68488 <span class="hljs-keyword">const</span> fs = <span class="hljs-built_in">require</span>(<span class…
68489 <span class="hljs-keyword">const</span> { pipeline } = <span class="hljs-built_in">require</span>(<…
68491 …="hljs-keyword">const</span> request = http.<span class="hljs-title function_">get</span>({ <span …
68492 … <span class="hljs-attr">path</span>: <span class="hljs-string">'/'</span>,
68493 <span class="hljs-attr">port</span>: <span class="hljs-number">80</span>,
68494 …<span class="hljs-attr">headers</span>: { <span class="hljs-string">'Accept-Encoding'</span>: <spa…
68495 …span class="hljs-title function_">on</span>(<span class="hljs-string">'response'</span>, <span cla…
68496 …<span class="hljs-keyword">const</span> output = fs.<span class="hljs-title function_">createWrite…
68498 …<span class="hljs-keyword">const</span> <span class="hljs-title function_">onError</span> = (<span…
68499 <span class="hljs-keyword">if</span> (err) {
68500 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">error</spa…
68501 process.<span class="hljs-property">exitCode</span> = <span class="hljs-number">1</span>;
68505 …<span class="hljs-keyword">switch</span> (response.<span class="hljs-property">headers</span>[<spa…
68506 <span class="hljs-keyword">case</span> <span class="hljs-string">'br'</span>:
68507 …<span class="hljs-title function_">pipeline</span>(response, zlib.<span class="hljs-title function…
68508 <span class="hljs-keyword">break</span>;
68509 …<span class="hljs-comment">// Or, just use zlib.createUnzip() to handle both of the following case…
68510 <span class="hljs-keyword">case</span> <span class="hljs-string">'gzip'</span>:
68511 …<span class="hljs-title function_">pipeline</span>(response, zlib.<span class="hljs-title function…
68512 <span class="hljs-keyword">break</span>;
68513 <span class="hljs-keyword">case</span> <span class="hljs-string">'deflate'</span>:
68514 …<span class="hljs-title function_">pipeline</span>(response, zlib.<span class="hljs-title function…
68515 <span class="hljs-keyword">break</span>;
68516 <span class="hljs-attr">default</span>:
68517 <span class="hljs-title function_">pipeline</span>(response, output, onError);
68518 <span class="hljs-keyword">break</span>;
68521 <pre><code class="language-js"><span class="hljs-comment">// server example</span>
68522 <span class="hljs-comment">// Running a gzip operation on every request is quite expensive.</span>
68523 <span class="hljs-comment">// It would be much more efficient to cache the compressed buffer.</span>
68524 <span class="hljs-keyword">const</span> zlib = <span class="hljs-built_in">require</span>(<span cla…
68525 <span class="hljs-keyword">const</span> http = <span class="hljs-built_in">require</span>(<span cla…
68526 <span class="hljs-keyword">const</span> fs = <span class="hljs-built_in">require</span>(<span class…
68527 <span class="hljs-keyword">const</span> { pipeline } = <span class="hljs-built_in">require</span>(<…
68529 http.<span class="hljs-title function_">createServer</span>(<span class="hljs-function">(<span clas…
68530 …<span class="hljs-keyword">const</span> raw = fs.<span class="hljs-title function_">createReadStre…
68531 …<span class="hljs-comment">// Store both a compressed and an uncompressed version of the resource.…
68532 …nse.<span class="hljs-title function_">setHeader</span>(<span class="hljs-string">'Vary'</span>, <…
68533 …pan class="hljs-keyword">let</span> acceptEncoding = request.<span class="hljs-property">headers</…
68534 <span class="hljs-keyword">if</span> (!acceptEncoding) {
68535 acceptEncoding = <span class="hljs-string">''</span>;
68538 …<span class="hljs-keyword">const</span> <span class="hljs-title function_">onError</span> = (<span…
68539 <span class="hljs-keyword">if</span> (err) {
68540 <span class="hljs-comment">// If an error occurs, there's not much we can do because</span>
68541 <span class="hljs-comment">// the server has already sent the 200 response code and</span>
68542 <span class="hljs-comment">// some amount of data has already been sent to the client.</span>
68543 <span class="hljs-comment">// The best we can do is terminate the response immediately</span>
68544 <span class="hljs-comment">// and log the error.</span>
68545 response.<span class="hljs-title function_">end</span>();
68546 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">error</spa…
68550 <span class="hljs-comment">// Note: This is not a conformant accept-encoding parser.</span>
68551 …<span class="hljs-comment">// See https://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.3<…
68552 …<span class="hljs-keyword">if</span> (<span class="hljs-regexp">/\bdeflate\b/</span>.<span class="…
68553 …ljs-title function_">writeHead</span>(<span class="hljs-number">200</span>, { <span class="hljs-st…
68554 …<span class="hljs-title function_">pipeline</span>(raw, zlib.<span class="hljs-title function_">cr…
68555 …pan class="hljs-keyword">else</span> <span class="hljs-keyword">if</span> (<span class="hljs-regex…
68556 …ljs-title function_">writeHead</span>(<span class="hljs-number">200</span>, { <span class="hljs-st…
68557 …<span class="hljs-title function_">pipeline</span>(raw, zlib.<span class="hljs-title function_">cr…
68558 …span class="hljs-keyword">else</span> <span class="hljs-keyword">if</span> (<span class="hljs-rege…
68559 …ljs-title function_">writeHead</span>(<span class="hljs-number">200</span>, { <span class="hljs-st…
68560 …<span class="hljs-title function_">pipeline</span>(raw, zlib.<span class="hljs-title function_">cr…
68561 } <span class="hljs-keyword">else</span> {
68562 …response.<span class="hljs-title function_">writeHead</span>(<span class="hljs-number">200</span>,…
68563 <span class="hljs-title function_">pipeline</span>(raw, response, onError);
68565 }).<span class="hljs-title function_">listen</span>(<span class="hljs-number">1337</span>);</code><…
68571 <pre><code class="language-js"><span class="hljs-comment">// This is a truncated version of the buf…
68572-keyword">const</span> buffer = <span class="hljs-title class_">Buffer</span>.<span class="hljs-ti…
68574 zlib.<span class="hljs-title function_">unzip</span>(
68576 …<span class="hljs-comment">// For Brotli, the equivalent is zlib.constants.BROTLI_OPERATION_FLUSH.…
68577 …{ <span class="hljs-attr">finishFlush</span>: zlib.<span class="hljs-property">constants</span>.<s…
68578 <span class="hljs-function">(<span class="hljs-params">err, buffer</span>) =></span> {
68579 <span class="hljs-keyword">if</span> (err) {
68580 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">error</spa…
68581 process.<span class="hljs-property">exitCode</span> = <span class="hljs-number">1</span>;
68583 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>…
68585 <p>This will not change the behavior in other error-throwing situations, e.g.
68592 <h4>For zlib-based streams<span><a class="mark" href="#zlib_for_zlib_based_streams" id="zlib_for_zl…
68595 <!-- eslint-disable semi -->
68596-js">(<span class="hljs-number">1</span> &#x3C;&#x3C; (windowBits + <span class="hljs-number">2</s…
68601-js"><span class="hljs-keyword">const</span> options = { <span class="hljs-attr">windowBits</span>…
68616 <h4>For Brotli-based streams<span><a class="mark" href="#zlib_for_brotli_based_streams" id="zlib_fo…
68617 <p>There are equivalents to the zlib options for Brotli-based streams, although
68623 <p>See <a href="#zlib_brotli_constants">below</a> for more details on Brotli-specific options.</p>
68630 …code class="language-js"><span class="hljs-keyword">const</span> zlib = <span class="hljs-built_in…
68631 <span class="hljs-keyword">const</span> http = <span class="hljs-built_in">require</span>(<span cla…
68632 <span class="hljs-keyword">const</span> { pipeline } = <span class="hljs-built_in">require</span>(<…
68634 http.<span class="hljs-title function_">createServer</span>(<span class="hljs-function">(<span clas…
68635 …<span class="hljs-comment">// For the sake of simplicity, the Accept-Encoding checks are omitted.<…
68636 …ljs-title function_">writeHead</span>(<span class="hljs-number">200</span>, { <span class="hljs-st…
68637 …<span class="hljs-keyword">const</span> output = zlib.<span class="hljs-title function_">createGzi…
68638 <span class="hljs-keyword">let</span> i;
68640 …<span class="hljs-title function_">pipeline</span>(output, response, <span class="hljs-function">(…
68641 <span class="hljs-keyword">if</span> (err) {
68642 <span class="hljs-comment">// If an error occurs, there's not much we can do because</span>
68643 <span class="hljs-comment">// the server has already sent the 200 response code and</span>
68644 <span class="hljs-comment">// some amount of data has already been sent to the client.</span>
68645 <span class="hljs-comment">// The best we can do is terminate the response immediately</span>
68646 <span class="hljs-comment">// and log the error.</span>
68647 <span class="hljs-built_in">clearInterval</span>(i);
68648 response.<span class="hljs-title function_">end</span>();
68649 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">error</spa…
68653 i = <span class="hljs-built_in">setInterval</span>(<span class="hljs-function">() =></span> {
68654-title function_">write</span>(<span class="hljs-string">`The current time is <span class="hljs-su…
68655 …<span class="hljs-comment">// The data has been passed to zlib, but the compression algorithm may<…
68656 …<span class="hljs-comment">// have decided to buffer the data for more efficient compression.</spa…
68657 …<span class="hljs-comment">// Calling .flush() will make the data available as soon as the client<…
68658 <span class="hljs-comment">// is ready to receive it.</span>
68659 output.<span class="hljs-title function_">flush</span>();
68661 }, <span class="hljs-number">1000</span>);
68662 }).<span class="hljs-title function_">listen</span>(<span class="hljs-number">1337</span>);</code><…
68720 <p>There are several options and other constants available for Brotli-based
68723 <p>The following values are valid flush operations for Brotli-based streams:</p>
68746 <li><code>BROTLI_MODE_TEXT</code>, adjusted for UTF-8 text</li>
68788 Brotli format as standardized in <a href="https://www.rfc-editor.org/rfc/rfc7932.txt">RFC 7932</a>)…
68813 Brotli format as standardized in <a href="https://www.rfc-editor.org/rfc/rfc7932.txt">RFC 7932</a>)…
68836 <p>Each zlib-based class takes an <code>options</code> object. No options are required.</p>
68840 <li><code>flush</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struct…
68841 <li><code>finishFlush</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_…
68842 <li><code>chunkSize</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_st…
68843 <li><code>windowBits</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_s…
68844 <li><code>level</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struct…
68845 <li><code>memLevel</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_str…
68846 <li><code>strategy</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_str…
68847-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray" class="type">&#x3C;TypedArray></a> | …
68849 <li><code>info</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
68850 <li><code>maxOutputLength</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/D…
68868 <p>Each Brotli-based class takes an <code>options</code> object. All options are optional.</p>
68870 <li><code>flush</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struct…
68871 <li><code>finishFlush</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_…
68872 <li><code>chunkSize</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_st…
68873 …tps://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type…
68874 <li><code>maxOutputLength</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/D…
68878 <pre><code class="language-js"><span class="hljs-keyword">const</span> stream = zlib.<span class="h…
68879 …<span class="hljs-attr">chunkSize</span>: <span class="hljs-number">32</span> * <span class="hljs-
68880 <span class="hljs-attr">params</span>: {
68881 …s-property">constants</span>.<span class="hljs-property">BROTLI_PARAM_MODE</span>]: zlib.<span cla…
68882 …[zlib.<span class="hljs-property">constants</span>.<span class="hljs-property">BROTLI_PARAM_QUALIT…
68883-property">constants</span>.<span class="hljs-property">BROTLI_PARAM_SIZE_HINT</span>]: fs.<span c…
68960 <p>Decompress either a Gzip- or Deflate-compressed stream by auto-detecting
68982 … api_stability_0"><a href="#documentation_stability_index">Stability: 0</a> - Deprecated: Use <a h…
68984 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" c…
68995 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" c…
69005 <li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Referenc…
69013 <li><code>kind</code> <strong>Default:</strong> <code>zlib.constants.Z_FULL_FLUSH</code> for zlib-b…
69014 <code>zlib.constants.BROTLI_OPERATION_FLUSH</code> for Brotli-based streams.</li>
69015 <li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Referenc…
69028 <li><code>level</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struct…
69029 <li><code>strategy</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_str…
69030 <li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Referenc…
69032 <p>This function is only available for zlib-based streams, i.e. not Brotli.</p>
69045 <p>Provides an object enumerating Zlib-related constants.</p>
69083 that effectively uses an 8-bit window only.</p>
69127 …lla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray"><code>TypedArray</code></a>…
69128 <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/ArrayBuff…
69138-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray" class="type">&#x3C;TypedArray></a> | …
69140 <li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Referenc…
69147-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray" class="type">&#x3C;TypedArray></a> | …
69156-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray" class="type">&#x3C;TypedArray></a> | …
69158 <li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Referenc…
69165-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray" class="type">&#x3C;TypedArray></a> | …
69186-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray" class="type">&#x3C;TypedArray></a> | …
69188 <li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Referenc…
69207-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray" class="type">&#x3C;TypedArray></a> | …
69226-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray" class="type">&#x3C;TypedArray></a> | …
69228 <li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Referenc…
69247-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray" class="type">&#x3C;TypedArray></a> | …
69268-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray" class="type">&#x3C;TypedArray></a> | …
69270 <li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Referenc…
69289-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray" class="type">&#x3C;TypedArray></a> | …
69310-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray" class="type">&#x3C;TypedArray></a> | …
69312 <li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Referenc…
69331-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray" class="type">&#x3C;TypedArray></a> | …
69352-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray" class="type">&#x3C;TypedArray></a> | …
69354 <li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Referenc…
69373-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray" class="type">&#x3C;TypedArray></a> | …
69394-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray" class="type">&#x3C;TypedArray></a> | …
69396 <li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Referenc…
69415-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray" class="type">&#x3C;TypedArray></a> | …
69436-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray" class="type">&#x3C;TypedArray></a> | …
69438 <li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Referenc…
69457-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray" class="type">&#x3C;TypedArray></a> | …
69462 <!-- API END -->
69471 const themeToggleButton = document.getElementById('theme-toggle-btn');
69473 const mq = window.matchMedia('(prefers-color-scheme: dark)');
69476 document.body.classList.toggle('dark-mode', e.matches);
69486 document.body.classList.add('dark-mode');
69489 document.body.classList.add('dark-mode');
69496 document.body.classList.toggle('dark-mode')