@encharm/cws 4.5.3 → 4.8.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (305) hide show
  1. package/CHANGELOG.md +18 -0
  2. package/CLAUDE.md +83 -0
  3. package/README.md +20 -4
  4. package/binding.gyp +8 -2
  5. package/dist/bindings/cws_darwin_arm64_node115.node +0 -0
  6. package/dist/bindings/cws_darwin_arm64_node127.node +0 -0
  7. package/dist/bindings/cws_darwin_arm64_node137.node +0 -0
  8. package/dist/bindings/cws_darwin_arm64_node147.node +0 -0
  9. package/dist/bindings/cws_linux_arm64_node115.node +0 -0
  10. package/dist/bindings/cws_linux_arm64_node127.node +0 -0
  11. package/dist/bindings/cws_linux_arm64_node137.node +0 -0
  12. package/dist/bindings/cws_linux_arm64_node147.node +0 -0
  13. package/dist/bindings/cws_linux_x64_node115.node +0 -0
  14. package/dist/bindings/cws_linux_x64_node127.node +0 -0
  15. package/dist/bindings/cws_linux_x64_node137.node +0 -0
  16. package/dist/bindings/cws_linux_x64_node147.node +0 -0
  17. package/dist/bindings/cws_win32_x64_node115.node +0 -0
  18. package/dist/bindings/cws_win32_x64_node127.node +0 -0
  19. package/dist/bindings/cws_win32_x64_node137.node +0 -0
  20. package/dist/bindings/cws_win32_x64_node147.node +0 -0
  21. package/dist/client.d.ts +1 -0
  22. package/dist/client.js +15 -1
  23. package/dist/index.d.ts +6 -1
  24. package/dist/index.js +2 -0
  25. package/dist/server.d.ts +1 -0
  26. package/dist/server.js +16 -2
  27. package/dist/shared.d.ts +1 -0
  28. package/dist/shared.js +3 -2
  29. package/package.json +1 -1
  30. package/src/Addon.cpp +4 -1
  31. package/src/Addon.h +80 -15
  32. package/src/Extensions.cpp +7 -1
  33. package/src/Extensions.h +2 -1
  34. package/src/Group.cpp +1 -1
  35. package/src/Group.h +4 -1
  36. package/src/HTTPSocket.cpp +1 -1
  37. package/src/Hub.cpp +11 -76
  38. package/src/Hub.h +12 -10
  39. package/src/Networking.h +11 -0
  40. package/src/Socket.h +201 -6
  41. package/src/WebSocket.cpp +8 -5
  42. package/src/Zlib.cpp +138 -0
  43. package/src/Zlib.h +38 -0
  44. package/src/cSNode.cpp +2 -0
  45. package/src/cSNode.h +4 -0
  46. package/src/headers/26/acorn_version.h +6 -0
  47. package/src/headers/26/aliased_buffer-inl.h +264 -0
  48. package/src/headers/26/aliased_buffer.h +205 -0
  49. package/src/headers/26/aliased_struct-inl.h +143 -0
  50. package/src/headers/26/aliased_struct.h +248 -0
  51. package/src/headers/26/amaro_version.h +6 -0
  52. package/src/headers/26/async_context_frame.h +33 -0
  53. package/src/headers/26/async_wrap-inl.h +123 -0
  54. package/src/headers/26/async_wrap.h +254 -0
  55. package/src/headers/26/base_object-inl.h +333 -0
  56. package/src/headers/26/base_object.h +339 -0
  57. package/src/headers/26/base_object_types.h +76 -0
  58. package/src/headers/26/blob_serializer_deserializer-inl.h +385 -0
  59. package/src/headers/26/blob_serializer_deserializer.h +142 -0
  60. package/src/headers/26/builtin_info.h +57 -0
  61. package/src/headers/26/callback_queue-inl.h +97 -0
  62. package/src/headers/26/callback_queue.h +79 -0
  63. package/src/headers/26/cares_wrap.h +475 -0
  64. package/src/headers/26/cleanup_queue-inl.h +36 -0
  65. package/src/headers/26/cleanup_queue.h +79 -0
  66. package/src/headers/26/compile_cache.h +134 -0
  67. package/src/headers/26/connect_wrap.h +26 -0
  68. package/src/headers/26/connection_wrap.h +30 -0
  69. package/src/headers/26/cppgc_helpers-inl.h +69 -0
  70. package/src/headers/26/cppgc_helpers.h +162 -0
  71. package/src/headers/26/crypto/crypto_aes.h +107 -0
  72. package/src/headers/26/crypto/crypto_argon2.h +88 -0
  73. package/src/headers/26/crypto/crypto_bio.h +193 -0
  74. package/src/headers/26/crypto/crypto_chacha20_poly1305.h +62 -0
  75. package/src/headers/26/crypto/crypto_cipher.h +291 -0
  76. package/src/headers/26/crypto/crypto_common.h +48 -0
  77. package/src/headers/26/crypto/crypto_context.h +219 -0
  78. package/src/headers/26/crypto/crypto_dh.h +104 -0
  79. package/src/headers/26/crypto/crypto_dsa.h +50 -0
  80. package/src/headers/26/crypto/crypto_ec.h +102 -0
  81. package/src/headers/26/crypto/crypto_hash.h +157 -0
  82. package/src/headers/26/crypto/crypto_hkdf.h +62 -0
  83. package/src/headers/26/crypto/crypto_hmac.h +92 -0
  84. package/src/headers/26/crypto/crypto_kem.h +131 -0
  85. package/src/headers/26/crypto/crypto_keygen.h +382 -0
  86. package/src/headers/26/crypto/crypto_keys.h +366 -0
  87. package/src/headers/26/crypto/crypto_kmac.h +79 -0
  88. package/src/headers/26/crypto/crypto_pbkdf2.h +76 -0
  89. package/src/headers/26/crypto/crypto_pqc.h +39 -0
  90. package/src/headers/26/crypto/crypto_random.h +127 -0
  91. package/src/headers/26/crypto/crypto_rsa.h +108 -0
  92. package/src/headers/26/crypto/crypto_scrypt.h +85 -0
  93. package/src/headers/26/crypto/crypto_sig.h +159 -0
  94. package/src/headers/26/crypto/crypto_spkac.h +21 -0
  95. package/src/headers/26/crypto/crypto_timing.h +20 -0
  96. package/src/headers/26/crypto/crypto_tls.h +342 -0
  97. package/src/headers/26/crypto/crypto_turboshake.h +105 -0
  98. package/src/headers/26/crypto/crypto_util.h +775 -0
  99. package/src/headers/26/crypto/crypto_x509.h +140 -0
  100. package/src/headers/26/dataqueue/queue.h +311 -0
  101. package/src/headers/26/debug_utils-inl.h +291 -0
  102. package/src/headers/26/debug_utils.h +200 -0
  103. package/src/headers/26/diagnosticfilename-inl.h +33 -0
  104. package/src/headers/26/embedded_data.h +17 -0
  105. package/src/headers/26/encoding_binding.h +59 -0
  106. package/src/headers/26/env-inl.h +956 -0
  107. package/src/headers/26/env.h +1294 -0
  108. package/src/headers/26/env_properties.h +562 -0
  109. package/src/headers/26/ffi/data.h +30 -0
  110. package/src/headers/26/ffi/fast.h +96 -0
  111. package/src/headers/26/ffi/jit_memory.h +26 -0
  112. package/src/headers/26/ffi/types.h +99 -0
  113. package/src/headers/26/handle_wrap.h +123 -0
  114. package/src/headers/26/histogram-inl.h +211 -0
  115. package/src/headers/26/histogram.h +474 -0
  116. package/src/headers/26/inspector/dom_storage_agent.h +68 -0
  117. package/src/headers/26/inspector/inspector_object_utils.h +38 -0
  118. package/src/headers/26/inspector/io_agent.h +30 -0
  119. package/src/headers/26/inspector/main_thread_interface.h +116 -0
  120. package/src/headers/26/inspector/network_agent.h +110 -0
  121. package/src/headers/26/inspector/network_inspector.h +45 -0
  122. package/src/headers/26/inspector/network_requests_buffer.h +195 -0
  123. package/src/headers/26/inspector/network_resource_manager.h +29 -0
  124. package/src/headers/26/inspector/node_json.h +24 -0
  125. package/src/headers/26/inspector/node_string.h +101 -0
  126. package/src/headers/26/inspector/notification_emitter.h +38 -0
  127. package/src/headers/26/inspector/protocol_helper.h +27 -0
  128. package/src/headers/26/inspector/runtime_agent.h +41 -0
  129. package/src/headers/26/inspector/storage_agent.h +33 -0
  130. package/src/headers/26/inspector/target_agent.h +59 -0
  131. package/src/headers/26/inspector/target_manager.h +70 -0
  132. package/src/headers/26/inspector/tracing_agent.h +43 -0
  133. package/src/headers/26/inspector/worker_agent.h +40 -0
  134. package/src/headers/26/inspector/worker_inspector.h +124 -0
  135. package/src/headers/26/inspector_agent.h +168 -0
  136. package/src/headers/26/inspector_io.h +78 -0
  137. package/src/headers/26/inspector_profiler.h +149 -0
  138. package/src/headers/26/inspector_socket.h +60 -0
  139. package/src/headers/26/inspector_socket_server.h +110 -0
  140. package/src/headers/26/js_native_api.h +634 -0
  141. package/src/headers/26/js_native_api_types.h +244 -0
  142. package/src/headers/26/js_native_api_v8.h +485 -0
  143. package/src/headers/26/js_native_api_v8_internals.h +45 -0
  144. package/src/headers/26/js_stream.h +57 -0
  145. package/src/headers/26/json_utils.h +172 -0
  146. package/src/headers/26/large_pages/node_large_page.h +34 -0
  147. package/src/headers/26/lru_cache-inl.h +69 -0
  148. package/src/headers/26/memory_tracker-inl.h +461 -0
  149. package/src/headers/26/memory_tracker.h +341 -0
  150. package/src/headers/26/module_wrap.h +231 -0
  151. package/src/headers/26/ncrypto.h +2008 -0
  152. package/src/headers/26/node.h +1693 -0
  153. package/src/headers/26/node_api.h +265 -0
  154. package/src/headers/26/node_api_internals.h +48 -0
  155. package/src/headers/26/node_api_types.h +58 -0
  156. package/src/headers/26/node_binding.h +170 -0
  157. package/src/headers/26/node_blob.h +160 -0
  158. package/src/headers/26/node_bob-inl.h +36 -0
  159. package/src/headers/26/node_bob.h +107 -0
  160. package/src/headers/26/node_buffer.h +92 -0
  161. package/src/headers/26/node_builtins.h +239 -0
  162. package/src/headers/26/node_concepts.h +46 -0
  163. package/src/headers/26/node_config_file.h +77 -0
  164. package/src/headers/26/node_constants.h +82 -0
  165. package/src/headers/26/node_context_data.h +171 -0
  166. package/src/headers/26/node_contextify.h +261 -0
  167. package/src/headers/26/node_crypto.h +66 -0
  168. package/src/headers/26/node_debug.h +30 -0
  169. package/src/headers/26/node_diagnostics_channel.h +121 -0
  170. package/src/headers/26/node_dir.h +52 -0
  171. package/src/headers/26/node_dotenv.h +45 -0
  172. package/src/headers/26/node_errors.h +378 -0
  173. package/src/headers/26/node_exit_code.h +54 -0
  174. package/src/headers/26/node_external_reference.h +171 -0
  175. package/src/headers/26/node_ffi.h +212 -0
  176. package/src/headers/26/node_file-inl.h +421 -0
  177. package/src/headers/26/node_file.h +561 -0
  178. package/src/headers/26/node_file_utils.h +39 -0
  179. package/src/headers/26/node_hash.h +212 -0
  180. package/src/headers/26/node_http2.h +1202 -0
  181. package/src/headers/26/node_http2_state.h +159 -0
  182. package/src/headers/26/node_http_common-inl.h +199 -0
  183. package/src/headers/26/node_http_common.h +535 -0
  184. package/src/headers/26/node_i18n.h +132 -0
  185. package/src/headers/26/node_internals.h +485 -0
  186. package/src/headers/26/node_locks.h +184 -0
  187. package/src/headers/26/node_main_instance.h +67 -0
  188. package/src/headers/26/node_mem-inl.h +113 -0
  189. package/src/headers/26/node_mem.h +45 -0
  190. package/src/headers/26/node_messaging.h +389 -0
  191. package/src/headers/26/node_metadata.h +172 -0
  192. package/src/headers/26/node_modules.h +102 -0
  193. package/src/headers/26/node_mutex.h +323 -0
  194. package/src/headers/26/node_object_wrap.h +145 -0
  195. package/src/headers/26/node_options-inl.h +524 -0
  196. package/src/headers/26/node_options.h +710 -0
  197. package/src/headers/26/node_perf.h +167 -0
  198. package/src/headers/26/node_perf_common.h +103 -0
  199. package/src/headers/26/node_platform.h +290 -0
  200. package/src/headers/26/node_process-inl.h +26 -0
  201. package/src/headers/26/node_process.h +112 -0
  202. package/src/headers/26/node_profiling.h +38 -0
  203. package/src/headers/26/node_realm-inl.h +156 -0
  204. package/src/headers/26/node_realm.h +236 -0
  205. package/src/headers/26/node_report.h +43 -0
  206. package/src/headers/26/node_revert.h +82 -0
  207. package/src/headers/26/node_root_certs.h +2937 -0
  208. package/src/headers/26/node_sea.h +101 -0
  209. package/src/headers/26/node_shadow_realm.h +46 -0
  210. package/src/headers/26/node_snapshot_builder.h +57 -0
  211. package/src/headers/26/node_snapshotable.h +167 -0
  212. package/src/headers/26/node_sockaddr-inl.h +248 -0
  213. package/src/headers/26/node_sockaddr.h +459 -0
  214. package/src/headers/26/node_sqlite.h +607 -0
  215. package/src/headers/26/node_stat_watcher.h +73 -0
  216. package/src/headers/26/node_task_runner.h +92 -0
  217. package/src/headers/26/node_threadsafe_cow-inl.h +54 -0
  218. package/src/headers/26/node_threadsafe_cow.h +105 -0
  219. package/src/headers/26/node_union_bytes.h +81 -0
  220. package/src/headers/26/node_url.h +101 -0
  221. package/src/headers/26/node_url_pattern.h +118 -0
  222. package/src/headers/26/node_v8.h +78 -0
  223. package/src/headers/26/node_v8_embedder.h +26 -0
  224. package/src/headers/26/node_v8_platform-inl.h +96 -0
  225. package/src/headers/26/node_version.h +116 -0
  226. package/src/headers/26/node_wasi.h +184 -0
  227. package/src/headers/26/node_wasm_web_api.h +55 -0
  228. package/src/headers/26/node_watchdog.h +164 -0
  229. package/src/headers/26/node_webstorage.h +62 -0
  230. package/src/headers/26/node_worker.h +162 -0
  231. package/src/headers/26/path.h +36 -0
  232. package/src/headers/26/permission/boolean_permission.h +50 -0
  233. package/src/headers/26/permission/fs_permission.h +192 -0
  234. package/src/headers/26/permission/permission.h +155 -0
  235. package/src/headers/26/permission/permission_base.h +79 -0
  236. package/src/headers/26/pipe_wrap.h +80 -0
  237. package/src/headers/26/quic/application.h +279 -0
  238. package/src/headers/26/quic/arena.h +373 -0
  239. package/src/headers/26/quic/bindingdata.h +440 -0
  240. package/src/headers/26/quic/cid.h +127 -0
  241. package/src/headers/26/quic/data.h +358 -0
  242. package/src/headers/26/quic/defs.h +417 -0
  243. package/src/headers/26/quic/endpoint.h +519 -0
  244. package/src/headers/26/quic/guard.h +5 -0
  245. package/src/headers/26/quic/http3.h +25 -0
  246. package/src/headers/26/quic/packet.h +166 -0
  247. package/src/headers/26/quic/preferredaddress.h +72 -0
  248. package/src/headers/26/quic/session.h +770 -0
  249. package/src/headers/26/quic/session_manager.h +107 -0
  250. package/src/headers/26/quic/sessionticket.h +110 -0
  251. package/src/headers/26/quic/streams.h +518 -0
  252. package/src/headers/26/quic/tlscontext.h +353 -0
  253. package/src/headers/26/quic/tokens.h +263 -0
  254. package/src/headers/26/quic/transportparams.h +184 -0
  255. package/src/headers/26/req_wrap-inl.h +175 -0
  256. package/src/headers/26/req_wrap.h +79 -0
  257. package/src/headers/26/spawn_sync.h +243 -0
  258. package/src/headers/26/stream_base-inl.h +170 -0
  259. package/src/headers/26/stream_base.h +487 -0
  260. package/src/headers/26/stream_pipe.h +76 -0
  261. package/src/headers/26/stream_wrap.h +137 -0
  262. package/src/headers/26/string_bytes.h +114 -0
  263. package/src/headers/26/string_decoder-inl.h +31 -0
  264. package/src/headers/26/string_decoder.h +49 -0
  265. package/src/headers/26/tcp_wrap.h +138 -0
  266. package/src/headers/26/threadpoolwork-inl.h +70 -0
  267. package/src/headers/26/timer_wrap-inl.h +32 -0
  268. package/src/headers/26/timer_wrap.h +87 -0
  269. package/src/headers/26/timers.h +70 -0
  270. package/src/headers/26/tracing/agent.h +125 -0
  271. package/src/headers/26/tracing/agent_legacy.h +146 -0
  272. package/src/headers/26/tracing/agent_perfetto.h +145 -0
  273. package/src/headers/26/tracing/node_trace_buffer.h +91 -0
  274. package/src/headers/26/tracing/node_trace_writer.h +79 -0
  275. package/src/headers/26/tracing/trace_event.h +27 -0
  276. package/src/headers/26/tracing/trace_event_helper.h +40 -0
  277. package/src/headers/26/tracing/trace_event_legacy.h +1113 -0
  278. package/src/headers/26/tracing/trace_event_legacy_inl.h +711 -0
  279. package/src/headers/26/tracing/trace_event_perfetto.h +47 -0
  280. package/src/headers/26/tracing/traced_value.h +167 -0
  281. package/src/headers/26/tty_wrap.h +65 -0
  282. package/src/headers/26/udp_wrap.h +231 -0
  283. package/src/headers/26/undici_version.h +6 -0
  284. package/src/headers/26/util-inl.h +788 -0
  285. package/src/headers/26/util.h +1207 -0
  286. package/src/headers/26/v8-cpp-heap-external.h +56 -0
  287. package/src/headers/26/v8-external-memory-accounter.h +60 -0
  288. package/src/headers/26/v8-fast-api-calls.h +784 -0
  289. package/src/headers/26/v8-inspector-protocol.h +13 -0
  290. package/src/headers/26/v8-inspector.h +458 -0
  291. package/src/headers/26/v8-metrics.h +273 -0
  292. package/src/headers/26/v8-trace-categories.h +28 -0
  293. package/src/headers/26/v8-unwinder-state.h +30 -0
  294. package/src/headers/26/v8-util.h +549 -0
  295. package/src/headers/26/v8-value-serializer-version.h +24 -0
  296. package/src/headers/26/v8-version-string.h +38 -0
  297. package/src/headers/26/v8-wasm-trap-handler-posix.h +31 -0
  298. package/src/headers/26/v8-wasm-trap-handler-win.h +28 -0
  299. package/src/headers/26/zlib_version.h +6 -0
  300. package/dist/bindings/cws_darwin_arm64_node115.node.dSYM/Contents/Info.plist +0 -20
  301. package/dist/bindings/cws_darwin_arm64_node115.node.dSYM/Contents/Resources/DWARF/cws_darwin_arm64_node115.node +0 -0
  302. package/dist/bindings/cws_darwin_arm64_node115.node.dSYM/Contents/Resources/Relocations/aarch64/cws_darwin_arm64_node115.node.yml +0 -931
  303. package/dist/bindings/cws_darwin_arm64_node127.node.dSYM/Contents/Info.plist +0 -20
  304. package/dist/bindings/cws_darwin_arm64_node127.node.dSYM/Contents/Resources/DWARF/cws_darwin_arm64_node127.node +0 -0
  305. package/dist/bindings/cws_darwin_arm64_node127.node.dSYM/Contents/Resources/Relocations/aarch64/cws_darwin_arm64_node127.node.yml +0 -931
@@ -0,0 +1,358 @@
1
+ #pragma once
2
+
3
+ #if defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS
4
+
5
+ #include <env.h>
6
+ #include <memory_tracker.h>
7
+ #include <nghttp3/nghttp3.h>
8
+ #include <ngtcp2/ngtcp2.h>
9
+ #include <node_internals.h>
10
+ #include <node_realm.h>
11
+ #include <node_sockaddr.h>
12
+ #include <v8.h>
13
+ #include <concepts>
14
+ #include <string>
15
+ #include "defs.h"
16
+
17
+ namespace node::quic {
18
+
19
+ template <typename T>
20
+ concept OneByteType = sizeof(T) == 1;
21
+
22
+ // Lightweight wrapper around ngtcp2_pkt_info. Insulates the Node.js QUIC
23
+ // code from the ngtcp2 struct layout and provides a clean API boundary
24
+ // for per-packet metadata (currently ECN codepoint; may grow as ngtcp2
25
+ // and libuv evolve).
26
+ //
27
+ // Default-constructed PacketInfo is zero-initialized, which ngtcp2 treats
28
+ // as ECN Not-ECT — identical to passing nullptr for the pkt_info parameter.
29
+ class PacketInfo final {
30
+ public:
31
+ // ECN codepoints as defined by RFC 3168.
32
+ enum class Ecn : uint32_t {
33
+ NOT_ECT = 0, // Not ECN-Capable Transport
34
+ ECT_1 = 1, // ECN-Capable Transport(1)
35
+ ECT_0 = 2, // ECN-Capable Transport(0)
36
+ CE = 3, // Congestion Experienced
37
+ };
38
+
39
+ PacketInfo() : info_{} {}
40
+ explicit PacketInfo(const ngtcp2_pkt_info& info) : info_(info) {}
41
+
42
+ // ECN codepoint for this packet. When libuv gains per-packet ECN
43
+ // reporting, populate via set_ecn() from the receive metadata
44
+ // before passing to ReadPacket().
45
+ Ecn ecn() const { return static_cast<Ecn>(info_.ecn); }
46
+ void set_ecn(Ecn ecn) { info_.ecn = static_cast<uint32_t>(ecn); }
47
+
48
+ // Conversion operators for ngtcp2 API calls.
49
+ operator const ngtcp2_pkt_info*() const { return &info_; }
50
+ operator ngtcp2_pkt_info*() { return &info_; }
51
+
52
+ private:
53
+ ngtcp2_pkt_info info_;
54
+ };
55
+
56
+ struct Path final : public ngtcp2_path {
57
+ explicit Path(const SocketAddress& local, const SocketAddress& remote);
58
+ Path(Path&& other) noexcept = default;
59
+ Path& operator=(Path&& other) noexcept = default;
60
+ DISALLOW_COPY(Path)
61
+ std::string ToString() const;
62
+ };
63
+
64
+ struct PathStorage final : public ngtcp2_path_storage {
65
+ explicit PathStorage();
66
+ PathStorage(PathStorage&& other) noexcept = default;
67
+ PathStorage& operator=(PathStorage&& other) noexcept = default;
68
+ DISALLOW_COPY(PathStorage)
69
+
70
+ void Reset();
71
+ void CopyTo(PathStorage* path) const;
72
+
73
+ bool operator==(const PathStorage& other) const;
74
+ bool operator!=(const PathStorage& other) const;
75
+ };
76
+
77
+ // Store acts as a wrapper around a v8::BackingStore, providing a convenient
78
+ // abstraction to map it to various buffer types used in QUIC, HTTP/3, and
79
+ // libuv, taking care of the necessary adjustments for length and offset.
80
+ class Store final : public MemoryRetainer {
81
+ public:
82
+ Store() = default;
83
+
84
+ Store(std::shared_ptr<v8::BackingStore> store,
85
+ size_t length,
86
+ size_t offset = 0);
87
+ Store(std::unique_ptr<v8::BackingStore> store,
88
+ size_t length,
89
+ size_t offset = 0);
90
+
91
+ // Creates a Store by copying the contents of an ArrayBuffer into a fresh
92
+ // BackingStore. The caller's buffer is not modified, so callers can safely
93
+ // reuse or mutate it after the call returns. Returns an empty Maybe on
94
+ // allocation failure, in which case an `ERR_MEMORY_ALLOCATION_FAILED`
95
+ // exception will have been scheduled on the isolate.
96
+ static v8::Maybe<Store> From(v8::Local<v8::ArrayBuffer> buffer);
97
+
98
+ // Creates a Store by copying the contents of an ArrayBufferView into a
99
+ // fresh BackingStore. The caller's view (and its underlying ArrayBuffer)
100
+ // is not modified. Returns an empty Maybe on allocation failure, in which
101
+ // case an `ERR_MEMORY_ALLOCATION_FAILED` exception will have been
102
+ // scheduled on the isolate.
103
+ static v8::Maybe<Store> From(v8::Local<v8::ArrayBufferView> view);
104
+
105
+ // Creates a Store from the contents of an ArrayBuffer, always copying the
106
+ // content. Equivalent to `From()` but returns a Store directly without
107
+ // surfacing allocation failure.
108
+ static Store CopyFrom(v8::Local<v8::ArrayBuffer> buffer);
109
+
110
+ // Creates a Store from the contents of an ArrayBufferView, always copying
111
+ // the content. Equivalent to `From()` but returns a Store directly without
112
+ // surfacing allocation failure.
113
+ static Store CopyFrom(v8::Local<v8::ArrayBufferView> view);
114
+
115
+ v8::Local<v8::Uint8Array> ToUint8Array(Environment* env) const;
116
+ inline v8::Local<v8::Uint8Array> ToUint8Array(Realm* realm) const {
117
+ return ToUint8Array(realm->env());
118
+ }
119
+
120
+ operator uv_buf_t() const;
121
+ operator ngtcp2_vec() const;
122
+ operator nghttp3_vec() const;
123
+ operator bool() const;
124
+ size_t length() const;
125
+
126
+ // Returns the total length of the underlying store, not just the
127
+ // length of the view. This is useful for memory accounting.
128
+ size_t total_length() const;
129
+
130
+ void MemoryInfo(MemoryTracker* tracker) const override;
131
+ SET_MEMORY_INFO_NAME(Store)
132
+ SET_SELF_SIZE(Store)
133
+
134
+ private:
135
+ template <typename T, OneByteType N>
136
+ T convert() const;
137
+ std::shared_ptr<v8::BackingStore> store_;
138
+ size_t length_ = 0;
139
+ size_t offset_ = 0;
140
+
141
+ // Because Store holds the v8::BackingStore and not the v8::ArrayBuffer,
142
+ // etc, the memory held by the Store is not directly, automatically
143
+ // associated with a v8::Isolate, and is therefore not accounted for
144
+ // as external memory. It is the responsibility of the owner of the
145
+ // Store instance to ensure the memory remains accounted for.
146
+ };
147
+
148
+ // Periodically, these need to be updated to match the latest ngtcp2 defs.
149
+ #define QUIC_TRANSPORT_ERRORS(V) \
150
+ V(INTERNAL_ERROR) \
151
+ V(CONNECTION_REFUSED) \
152
+ V(FLOW_CONTROL_ERROR) \
153
+ V(STREAM_LIMIT_ERROR) \
154
+ V(STREAM_STATE_ERROR) \
155
+ V(FINAL_SIZE_ERROR) \
156
+ V(FRAME_ENCODING_ERROR) \
157
+ V(TRANSPORT_PARAMETER_ERROR) \
158
+ V(CONNECTION_ID_LIMIT_ERROR) \
159
+ V(PROTOCOL_VIOLATION) \
160
+ V(INVALID_TOKEN) \
161
+ V(APPLICATION_ERROR) \
162
+ V(CRYPTO_BUFFER_EXCEEDED) \
163
+ V(KEY_UPDATE_ERROR) \
164
+ V(AEAD_LIMIT_REACHED) \
165
+ V(NO_VIABLE_PATH) \
166
+ V(CRYPTO_ERROR) \
167
+ V(VERSION_NEGOTIATION_ERROR)
168
+
169
+ // Periodically, these need to be updated to match the latest nghttp3 defs.
170
+ #define HTTP3_APPLICATION_ERRORS(V) \
171
+ V(H3_NO_ERROR) \
172
+ V(H3_GENERAL_PROTOCOL_ERROR) \
173
+ V(H3_INTERNAL_ERROR) \
174
+ V(H3_STREAM_CREATION_ERROR) \
175
+ V(H3_CLOSED_CRITICAL_STREAM) \
176
+ V(H3_FRAME_UNEXPECTED) \
177
+ V(H3_FRAME_ERROR) \
178
+ V(H3_EXCESSIVE_LOAD) \
179
+ V(H3_ID_ERROR) \
180
+ V(H3_SETTINGS_ERROR) \
181
+ V(H3_MISSING_SETTINGS) \
182
+ V(H3_REQUEST_REJECTED) \
183
+ V(H3_REQUEST_CANCELLED) \
184
+ V(H3_REQUEST_INCOMPLETE) \
185
+ V(H3_MESSAGE_ERROR) \
186
+ V(H3_CONNECT_ERROR) \
187
+ V(H3_VERSION_FALLBACK) \
188
+ V(QPACK_DECOMPRESSION_FAILED) \
189
+ V(QPACK_ENCODER_STREAM_ERROR) \
190
+ V(QPACK_DECODER_STREAM_ERROR)
191
+
192
+ class QuicError final : public MemoryRetainer {
193
+ public:
194
+ // The known error codes for the transport namespace.
195
+ enum class TransportError : error_code {
196
+ // NO_ERROR has to be treated specially since it is a macro on
197
+ // some Windows cases and results in a compile error if we leave
198
+ // it as is.
199
+ NO_ERROR_ = NGTCP2_NO_ERROR,
200
+ #define V(name) name = NGTCP2_##name,
201
+ QUIC_TRANSPORT_ERRORS(V)
202
+ #undef V
203
+ };
204
+
205
+ // Every QUIC application defines its own error codes in the application
206
+ // namespace. These are managed independently of each other and may overlap
207
+ // with other applications and even the transport namespace. The only way
208
+ // to correctly interpret an application error code is to know which
209
+ // application is being used. For convenience, we define constants for the
210
+ // known HTTP/3 application error codes here.
211
+ enum class Http3Error : error_code {
212
+ #define V(name) name = NGHTTP3_##name,
213
+ HTTP3_APPLICATION_ERRORS(V)
214
+ #undef V
215
+ };
216
+
217
+ static constexpr error_code QUIC_NO_ERROR = NGTCP2_NO_ERROR;
218
+ static constexpr error_code HTTP3_NO_ERROR_CODE = NGHTTP3_H3_NO_ERROR;
219
+
220
+ // In QUIC, Errors are represented as namespaced 64-bit error codes.
221
+ // The error code only has meaning within the context of a specific
222
+ // namespace. The QuicError::Type enum defines the available namespaces.
223
+ // There are essentially two namespaces: transport and application, with
224
+ // a few additional special-cases that are variants of the transport
225
+ // namespace.
226
+ enum class Type {
227
+ TRANSPORT = NGTCP2_CCERR_TYPE_TRANSPORT,
228
+ APPLICATION = NGTCP2_CCERR_TYPE_APPLICATION,
229
+
230
+ // These are special cases of transport errors.
231
+ VERSION_NEGOTIATION = NGTCP2_CCERR_TYPE_VERSION_NEGOTIATION,
232
+ IDLE_CLOSE = NGTCP2_CCERR_TYPE_IDLE_CLOSE,
233
+ DROP_CONNECTION = NGTCP2_CCERR_TYPE_DROP_CONN,
234
+ RETRY = NGTCP2_CCERR_TYPE_RETRY,
235
+ };
236
+
237
+ // Do not use the constructors directly in regular use. Use the static
238
+ // factory methods instead. Those will ensure that the underlying
239
+ // ngtcp2_ccerr is initialized correctly based on the type of error
240
+ // being created.
241
+ explicit QuicError(const std::string& reason = "");
242
+ explicit QuicError(const ngtcp2_ccerr* ptr);
243
+ explicit QuicError(const ngtcp2_ccerr& error);
244
+
245
+ // Move constructor and assignment must fix up ptr_ when it points
246
+ // to the internal error_ member (as set by the default constructor
247
+ // and the ForTransport/ForApplication factory methods).
248
+ QuicError(QuicError&& other) noexcept;
249
+ QuicError& operator=(QuicError&& other) noexcept;
250
+
251
+ // Copy constructor and assignment must also fix up ptr_.
252
+ QuicError(const QuicError& other);
253
+ QuicError& operator=(const QuicError& other);
254
+
255
+ Type type() const;
256
+ error_code code() const;
257
+ const std::string_view reason() const;
258
+ uint64_t frame_type() const;
259
+
260
+ operator const ngtcp2_ccerr&() const;
261
+ operator const ngtcp2_ccerr*() const;
262
+
263
+ // Crypto errors are a subset of transport errors. The error code includes
264
+ // the TLS alert code embedded within it.
265
+ bool is_crypto_error() const;
266
+ std::optional<int> get_crypto_error() const;
267
+
268
+ // Returns a human-readable name for this error if known, or nullptr
269
+ const char* name() const;
270
+
271
+ // Note that since application errors are application-specific and we
272
+ // don't know which application is being used here, it is possible that
273
+ // the comparing two different QuicError instances from different applications
274
+ // will return true even if they are not semantically equivalent. This should
275
+ // not be a problem in practice.
276
+ bool operator==(const QuicError& other) const;
277
+ bool operator!=(const QuicError& other) const;
278
+
279
+ void MemoryInfo(MemoryTracker* tracker) const override;
280
+ SET_MEMORY_INFO_NAME(QuicError)
281
+ SET_SELF_SIZE(QuicError)
282
+
283
+ std::string ToString() const;
284
+
285
+ // Returns an array containing [type, code, reason], where type is a string
286
+ // representation of the error type, code is a bigint representation of the
287
+ // error code, and reason is a string representation of the error reason, or
288
+ // undefined if the reason is the empty string. This is expected to be used
289
+ // to construct a JS error object from the information in JS.
290
+ v8::MaybeLocal<v8::Value> ToV8Value(Environment* env) const;
291
+ inline v8::MaybeLocal<v8::Value> ToV8Value(Realm* realm) const {
292
+ return ToV8Value(realm->env());
293
+ }
294
+
295
+ // Utility functions for getting the default error reason strings for
296
+ // internal error codes returned by the underlying ngtcp2/nghttp3 libraries.
297
+ static std::string reason_for_liberr(int liberr);
298
+ static std::string reason_for_h3_liberr(int liberr);
299
+
300
+ // Utility functions for checking if the given internal error codes are
301
+ // considered to be fatal by the underlying ngtcp2/nghttp3 libraries.
302
+ static bool is_fatal_liberror(int liberr);
303
+ static bool is_fatal_h3_liberror(int liberr);
304
+
305
+ // Utility functions for converting between ngtcp2/nghttp3 internal error
306
+ // codes to the corresponding QUIC error codes.
307
+ static error_code liberr_to_code(int liberr);
308
+ static error_code h3_liberr_to_code(int liberr);
309
+
310
+ // Utility functions for creating QuicError instances.
311
+ // The reason is expected to always be UTF-8 encoded.
312
+ static QuicError ForTransport(TransportError code = TransportError::NO_ERROR_,
313
+ std::string reason = "");
314
+ static QuicError ForTransport(error_code code, std::string reason = "");
315
+ static QuicError ForApplication(Http3Error code, std::string reason = "");
316
+ static QuicError ForApplication(error_code code, std::string reason = "");
317
+ static QuicError ForVersionNegotiation(std::string reason = "");
318
+ static QuicError ForIdleClose(std::string reason = "");
319
+ static QuicError ForDropConnection(std::string reason = "");
320
+ static QuicError ForRetry(std::string reason = "");
321
+ static QuicError ForNgtcp2Error(int code, std::string reason = "");
322
+ static QuicError ForTlsAlert(int code, std::string reason = "");
323
+
324
+ static QuicError FromConnectionClose(ngtcp2_conn* session);
325
+
326
+ static const QuicError TRANSPORT_NO_ERROR;
327
+ #define V(name) static const QuicError TRANSPORT_##name;
328
+ QUIC_TRANSPORT_ERRORS(V)
329
+ #undef V
330
+ static const QuicError HTTP3_NO_ERROR;
331
+ static const QuicError VERSION_NEGOTIATION;
332
+ static const QuicError IDLE_CLOSE;
333
+ static const QuicError DROP_CONNECTION;
334
+ static const QuicError RETRY;
335
+ static const QuicError INTERNAL_ERROR;
336
+
337
+ private:
338
+ const uint8_t* reason_c_str() const;
339
+
340
+ std::string reason_;
341
+ ngtcp2_ccerr error_;
342
+ const ngtcp2_ccerr* ptr_ = nullptr;
343
+ };
344
+
345
+ // Marked maybe_unused because these are used in the tests but not in the
346
+ // production code.
347
+ [[maybe_unused]] static bool operator==(const QuicError::TransportError& lhs,
348
+ error_code rhs) {
349
+ return static_cast<error_code>(lhs) == rhs;
350
+ }
351
+ [[maybe_unused]] static bool operator==(const QuicError::Http3Error& lhs,
352
+ error_code rhs) {
353
+ return static_cast<error_code>(lhs) == rhs;
354
+ }
355
+
356
+ } // namespace node::quic
357
+
358
+ #endif // defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS