@encharm/cws 4.5.3 → 4.6.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 (288) hide show
  1. package/CHANGELOG.md +9 -0
  2. package/CLAUDE.md +81 -0
  3. package/README.md +10 -3
  4. package/dist/bindings/cws_darwin_arm64_node115.node +0 -0
  5. package/dist/bindings/cws_darwin_arm64_node127.node +0 -0
  6. package/dist/bindings/cws_darwin_arm64_node137.node +0 -0
  7. package/dist/bindings/cws_darwin_arm64_node147.node +0 -0
  8. package/dist/bindings/cws_linux_arm64_node115.node +0 -0
  9. package/dist/bindings/cws_linux_arm64_node127.node +0 -0
  10. package/dist/bindings/cws_linux_arm64_node137.node +0 -0
  11. package/dist/bindings/cws_linux_arm64_node147.node +0 -0
  12. package/dist/bindings/cws_linux_x64_node115.node +0 -0
  13. package/dist/bindings/cws_linux_x64_node127.node +0 -0
  14. package/dist/bindings/cws_linux_x64_node137.node +0 -0
  15. package/dist/bindings/cws_linux_x64_node147.node +0 -0
  16. package/dist/bindings/cws_win32_x64_node115.node +0 -0
  17. package/dist/bindings/cws_win32_x64_node127.node +0 -0
  18. package/dist/bindings/cws_win32_x64_node137.node +0 -0
  19. package/dist/bindings/cws_win32_x64_node147.node +0 -0
  20. package/package.json +1 -1
  21. package/src/Addon.cpp +2 -1
  22. package/src/Addon.h +70 -14
  23. package/src/Hub.h +1 -0
  24. package/src/Networking.h +11 -0
  25. package/src/Socket.h +201 -6
  26. package/src/WebSocket.cpp +4 -1
  27. package/src/cSNode.cpp +2 -0
  28. package/src/cSNode.h +4 -0
  29. package/src/headers/26/acorn_version.h +6 -0
  30. package/src/headers/26/aliased_buffer-inl.h +264 -0
  31. package/src/headers/26/aliased_buffer.h +205 -0
  32. package/src/headers/26/aliased_struct-inl.h +143 -0
  33. package/src/headers/26/aliased_struct.h +248 -0
  34. package/src/headers/26/amaro_version.h +6 -0
  35. package/src/headers/26/async_context_frame.h +33 -0
  36. package/src/headers/26/async_wrap-inl.h +123 -0
  37. package/src/headers/26/async_wrap.h +254 -0
  38. package/src/headers/26/base_object-inl.h +333 -0
  39. package/src/headers/26/base_object.h +339 -0
  40. package/src/headers/26/base_object_types.h +76 -0
  41. package/src/headers/26/blob_serializer_deserializer-inl.h +385 -0
  42. package/src/headers/26/blob_serializer_deserializer.h +142 -0
  43. package/src/headers/26/builtin_info.h +57 -0
  44. package/src/headers/26/callback_queue-inl.h +97 -0
  45. package/src/headers/26/callback_queue.h +79 -0
  46. package/src/headers/26/cares_wrap.h +475 -0
  47. package/src/headers/26/cleanup_queue-inl.h +36 -0
  48. package/src/headers/26/cleanup_queue.h +79 -0
  49. package/src/headers/26/compile_cache.h +134 -0
  50. package/src/headers/26/connect_wrap.h +26 -0
  51. package/src/headers/26/connection_wrap.h +30 -0
  52. package/src/headers/26/cppgc_helpers-inl.h +69 -0
  53. package/src/headers/26/cppgc_helpers.h +162 -0
  54. package/src/headers/26/crypto/crypto_aes.h +107 -0
  55. package/src/headers/26/crypto/crypto_argon2.h +88 -0
  56. package/src/headers/26/crypto/crypto_bio.h +193 -0
  57. package/src/headers/26/crypto/crypto_chacha20_poly1305.h +62 -0
  58. package/src/headers/26/crypto/crypto_cipher.h +291 -0
  59. package/src/headers/26/crypto/crypto_common.h +48 -0
  60. package/src/headers/26/crypto/crypto_context.h +219 -0
  61. package/src/headers/26/crypto/crypto_dh.h +104 -0
  62. package/src/headers/26/crypto/crypto_dsa.h +50 -0
  63. package/src/headers/26/crypto/crypto_ec.h +102 -0
  64. package/src/headers/26/crypto/crypto_hash.h +157 -0
  65. package/src/headers/26/crypto/crypto_hkdf.h +62 -0
  66. package/src/headers/26/crypto/crypto_hmac.h +92 -0
  67. package/src/headers/26/crypto/crypto_kem.h +131 -0
  68. package/src/headers/26/crypto/crypto_keygen.h +382 -0
  69. package/src/headers/26/crypto/crypto_keys.h +366 -0
  70. package/src/headers/26/crypto/crypto_kmac.h +79 -0
  71. package/src/headers/26/crypto/crypto_pbkdf2.h +76 -0
  72. package/src/headers/26/crypto/crypto_pqc.h +39 -0
  73. package/src/headers/26/crypto/crypto_random.h +127 -0
  74. package/src/headers/26/crypto/crypto_rsa.h +108 -0
  75. package/src/headers/26/crypto/crypto_scrypt.h +85 -0
  76. package/src/headers/26/crypto/crypto_sig.h +159 -0
  77. package/src/headers/26/crypto/crypto_spkac.h +21 -0
  78. package/src/headers/26/crypto/crypto_timing.h +20 -0
  79. package/src/headers/26/crypto/crypto_tls.h +342 -0
  80. package/src/headers/26/crypto/crypto_turboshake.h +105 -0
  81. package/src/headers/26/crypto/crypto_util.h +775 -0
  82. package/src/headers/26/crypto/crypto_x509.h +140 -0
  83. package/src/headers/26/dataqueue/queue.h +311 -0
  84. package/src/headers/26/debug_utils-inl.h +291 -0
  85. package/src/headers/26/debug_utils.h +200 -0
  86. package/src/headers/26/diagnosticfilename-inl.h +33 -0
  87. package/src/headers/26/embedded_data.h +17 -0
  88. package/src/headers/26/encoding_binding.h +59 -0
  89. package/src/headers/26/env-inl.h +956 -0
  90. package/src/headers/26/env.h +1294 -0
  91. package/src/headers/26/env_properties.h +562 -0
  92. package/src/headers/26/ffi/data.h +30 -0
  93. package/src/headers/26/ffi/fast.h +96 -0
  94. package/src/headers/26/ffi/jit_memory.h +26 -0
  95. package/src/headers/26/ffi/types.h +99 -0
  96. package/src/headers/26/handle_wrap.h +123 -0
  97. package/src/headers/26/histogram-inl.h +211 -0
  98. package/src/headers/26/histogram.h +474 -0
  99. package/src/headers/26/inspector/dom_storage_agent.h +68 -0
  100. package/src/headers/26/inspector/inspector_object_utils.h +38 -0
  101. package/src/headers/26/inspector/io_agent.h +30 -0
  102. package/src/headers/26/inspector/main_thread_interface.h +116 -0
  103. package/src/headers/26/inspector/network_agent.h +110 -0
  104. package/src/headers/26/inspector/network_inspector.h +45 -0
  105. package/src/headers/26/inspector/network_requests_buffer.h +195 -0
  106. package/src/headers/26/inspector/network_resource_manager.h +29 -0
  107. package/src/headers/26/inspector/node_json.h +24 -0
  108. package/src/headers/26/inspector/node_string.h +101 -0
  109. package/src/headers/26/inspector/notification_emitter.h +38 -0
  110. package/src/headers/26/inspector/protocol_helper.h +27 -0
  111. package/src/headers/26/inspector/runtime_agent.h +41 -0
  112. package/src/headers/26/inspector/storage_agent.h +33 -0
  113. package/src/headers/26/inspector/target_agent.h +59 -0
  114. package/src/headers/26/inspector/target_manager.h +70 -0
  115. package/src/headers/26/inspector/tracing_agent.h +43 -0
  116. package/src/headers/26/inspector/worker_agent.h +40 -0
  117. package/src/headers/26/inspector/worker_inspector.h +124 -0
  118. package/src/headers/26/inspector_agent.h +168 -0
  119. package/src/headers/26/inspector_io.h +78 -0
  120. package/src/headers/26/inspector_profiler.h +149 -0
  121. package/src/headers/26/inspector_socket.h +60 -0
  122. package/src/headers/26/inspector_socket_server.h +110 -0
  123. package/src/headers/26/js_native_api.h +634 -0
  124. package/src/headers/26/js_native_api_types.h +244 -0
  125. package/src/headers/26/js_native_api_v8.h +485 -0
  126. package/src/headers/26/js_native_api_v8_internals.h +45 -0
  127. package/src/headers/26/js_stream.h +57 -0
  128. package/src/headers/26/json_utils.h +172 -0
  129. package/src/headers/26/large_pages/node_large_page.h +34 -0
  130. package/src/headers/26/lru_cache-inl.h +69 -0
  131. package/src/headers/26/memory_tracker-inl.h +461 -0
  132. package/src/headers/26/memory_tracker.h +341 -0
  133. package/src/headers/26/module_wrap.h +231 -0
  134. package/src/headers/26/ncrypto.h +2008 -0
  135. package/src/headers/26/node.h +1693 -0
  136. package/src/headers/26/node_api.h +265 -0
  137. package/src/headers/26/node_api_internals.h +48 -0
  138. package/src/headers/26/node_api_types.h +58 -0
  139. package/src/headers/26/node_binding.h +170 -0
  140. package/src/headers/26/node_blob.h +160 -0
  141. package/src/headers/26/node_bob-inl.h +36 -0
  142. package/src/headers/26/node_bob.h +107 -0
  143. package/src/headers/26/node_buffer.h +92 -0
  144. package/src/headers/26/node_builtins.h +239 -0
  145. package/src/headers/26/node_concepts.h +46 -0
  146. package/src/headers/26/node_config_file.h +77 -0
  147. package/src/headers/26/node_constants.h +82 -0
  148. package/src/headers/26/node_context_data.h +171 -0
  149. package/src/headers/26/node_contextify.h +261 -0
  150. package/src/headers/26/node_crypto.h +66 -0
  151. package/src/headers/26/node_debug.h +30 -0
  152. package/src/headers/26/node_diagnostics_channel.h +121 -0
  153. package/src/headers/26/node_dir.h +52 -0
  154. package/src/headers/26/node_dotenv.h +45 -0
  155. package/src/headers/26/node_errors.h +378 -0
  156. package/src/headers/26/node_exit_code.h +54 -0
  157. package/src/headers/26/node_external_reference.h +171 -0
  158. package/src/headers/26/node_ffi.h +212 -0
  159. package/src/headers/26/node_file-inl.h +421 -0
  160. package/src/headers/26/node_file.h +561 -0
  161. package/src/headers/26/node_file_utils.h +39 -0
  162. package/src/headers/26/node_hash.h +212 -0
  163. package/src/headers/26/node_http2.h +1202 -0
  164. package/src/headers/26/node_http2_state.h +159 -0
  165. package/src/headers/26/node_http_common-inl.h +199 -0
  166. package/src/headers/26/node_http_common.h +535 -0
  167. package/src/headers/26/node_i18n.h +132 -0
  168. package/src/headers/26/node_internals.h +485 -0
  169. package/src/headers/26/node_locks.h +184 -0
  170. package/src/headers/26/node_main_instance.h +67 -0
  171. package/src/headers/26/node_mem-inl.h +113 -0
  172. package/src/headers/26/node_mem.h +45 -0
  173. package/src/headers/26/node_messaging.h +389 -0
  174. package/src/headers/26/node_metadata.h +172 -0
  175. package/src/headers/26/node_modules.h +102 -0
  176. package/src/headers/26/node_mutex.h +323 -0
  177. package/src/headers/26/node_object_wrap.h +145 -0
  178. package/src/headers/26/node_options-inl.h +524 -0
  179. package/src/headers/26/node_options.h +710 -0
  180. package/src/headers/26/node_perf.h +167 -0
  181. package/src/headers/26/node_perf_common.h +103 -0
  182. package/src/headers/26/node_platform.h +290 -0
  183. package/src/headers/26/node_process-inl.h +26 -0
  184. package/src/headers/26/node_process.h +112 -0
  185. package/src/headers/26/node_profiling.h +38 -0
  186. package/src/headers/26/node_realm-inl.h +156 -0
  187. package/src/headers/26/node_realm.h +236 -0
  188. package/src/headers/26/node_report.h +43 -0
  189. package/src/headers/26/node_revert.h +82 -0
  190. package/src/headers/26/node_root_certs.h +2937 -0
  191. package/src/headers/26/node_sea.h +101 -0
  192. package/src/headers/26/node_shadow_realm.h +46 -0
  193. package/src/headers/26/node_snapshot_builder.h +57 -0
  194. package/src/headers/26/node_snapshotable.h +167 -0
  195. package/src/headers/26/node_sockaddr-inl.h +248 -0
  196. package/src/headers/26/node_sockaddr.h +459 -0
  197. package/src/headers/26/node_sqlite.h +607 -0
  198. package/src/headers/26/node_stat_watcher.h +73 -0
  199. package/src/headers/26/node_task_runner.h +92 -0
  200. package/src/headers/26/node_threadsafe_cow-inl.h +54 -0
  201. package/src/headers/26/node_threadsafe_cow.h +105 -0
  202. package/src/headers/26/node_union_bytes.h +81 -0
  203. package/src/headers/26/node_url.h +101 -0
  204. package/src/headers/26/node_url_pattern.h +118 -0
  205. package/src/headers/26/node_v8.h +78 -0
  206. package/src/headers/26/node_v8_embedder.h +26 -0
  207. package/src/headers/26/node_v8_platform-inl.h +96 -0
  208. package/src/headers/26/node_version.h +116 -0
  209. package/src/headers/26/node_wasi.h +184 -0
  210. package/src/headers/26/node_wasm_web_api.h +55 -0
  211. package/src/headers/26/node_watchdog.h +164 -0
  212. package/src/headers/26/node_webstorage.h +62 -0
  213. package/src/headers/26/node_worker.h +162 -0
  214. package/src/headers/26/path.h +36 -0
  215. package/src/headers/26/permission/boolean_permission.h +50 -0
  216. package/src/headers/26/permission/fs_permission.h +192 -0
  217. package/src/headers/26/permission/permission.h +155 -0
  218. package/src/headers/26/permission/permission_base.h +79 -0
  219. package/src/headers/26/pipe_wrap.h +80 -0
  220. package/src/headers/26/quic/application.h +279 -0
  221. package/src/headers/26/quic/arena.h +373 -0
  222. package/src/headers/26/quic/bindingdata.h +440 -0
  223. package/src/headers/26/quic/cid.h +127 -0
  224. package/src/headers/26/quic/data.h +358 -0
  225. package/src/headers/26/quic/defs.h +417 -0
  226. package/src/headers/26/quic/endpoint.h +519 -0
  227. package/src/headers/26/quic/guard.h +5 -0
  228. package/src/headers/26/quic/http3.h +25 -0
  229. package/src/headers/26/quic/packet.h +166 -0
  230. package/src/headers/26/quic/preferredaddress.h +72 -0
  231. package/src/headers/26/quic/session.h +770 -0
  232. package/src/headers/26/quic/session_manager.h +107 -0
  233. package/src/headers/26/quic/sessionticket.h +110 -0
  234. package/src/headers/26/quic/streams.h +518 -0
  235. package/src/headers/26/quic/tlscontext.h +353 -0
  236. package/src/headers/26/quic/tokens.h +263 -0
  237. package/src/headers/26/quic/transportparams.h +184 -0
  238. package/src/headers/26/req_wrap-inl.h +175 -0
  239. package/src/headers/26/req_wrap.h +79 -0
  240. package/src/headers/26/spawn_sync.h +243 -0
  241. package/src/headers/26/stream_base-inl.h +170 -0
  242. package/src/headers/26/stream_base.h +487 -0
  243. package/src/headers/26/stream_pipe.h +76 -0
  244. package/src/headers/26/stream_wrap.h +137 -0
  245. package/src/headers/26/string_bytes.h +114 -0
  246. package/src/headers/26/string_decoder-inl.h +31 -0
  247. package/src/headers/26/string_decoder.h +49 -0
  248. package/src/headers/26/tcp_wrap.h +138 -0
  249. package/src/headers/26/threadpoolwork-inl.h +70 -0
  250. package/src/headers/26/timer_wrap-inl.h +32 -0
  251. package/src/headers/26/timer_wrap.h +87 -0
  252. package/src/headers/26/timers.h +70 -0
  253. package/src/headers/26/tracing/agent.h +125 -0
  254. package/src/headers/26/tracing/agent_legacy.h +146 -0
  255. package/src/headers/26/tracing/agent_perfetto.h +145 -0
  256. package/src/headers/26/tracing/node_trace_buffer.h +91 -0
  257. package/src/headers/26/tracing/node_trace_writer.h +79 -0
  258. package/src/headers/26/tracing/trace_event.h +27 -0
  259. package/src/headers/26/tracing/trace_event_helper.h +40 -0
  260. package/src/headers/26/tracing/trace_event_legacy.h +1113 -0
  261. package/src/headers/26/tracing/trace_event_legacy_inl.h +711 -0
  262. package/src/headers/26/tracing/trace_event_perfetto.h +47 -0
  263. package/src/headers/26/tracing/traced_value.h +167 -0
  264. package/src/headers/26/tty_wrap.h +65 -0
  265. package/src/headers/26/udp_wrap.h +231 -0
  266. package/src/headers/26/undici_version.h +6 -0
  267. package/src/headers/26/util-inl.h +788 -0
  268. package/src/headers/26/util.h +1207 -0
  269. package/src/headers/26/v8-cpp-heap-external.h +56 -0
  270. package/src/headers/26/v8-external-memory-accounter.h +60 -0
  271. package/src/headers/26/v8-fast-api-calls.h +784 -0
  272. package/src/headers/26/v8-inspector-protocol.h +13 -0
  273. package/src/headers/26/v8-inspector.h +458 -0
  274. package/src/headers/26/v8-metrics.h +273 -0
  275. package/src/headers/26/v8-trace-categories.h +28 -0
  276. package/src/headers/26/v8-unwinder-state.h +30 -0
  277. package/src/headers/26/v8-util.h +549 -0
  278. package/src/headers/26/v8-value-serializer-version.h +24 -0
  279. package/src/headers/26/v8-version-string.h +38 -0
  280. package/src/headers/26/v8-wasm-trap-handler-posix.h +31 -0
  281. package/src/headers/26/v8-wasm-trap-handler-win.h +28 -0
  282. package/src/headers/26/zlib_version.h +6 -0
  283. package/dist/bindings/cws_darwin_arm64_node115.node.dSYM/Contents/Info.plist +0 -20
  284. package/dist/bindings/cws_darwin_arm64_node115.node.dSYM/Contents/Resources/DWARF/cws_darwin_arm64_node115.node +0 -0
  285. package/dist/bindings/cws_darwin_arm64_node115.node.dSYM/Contents/Resources/Relocations/aarch64/cws_darwin_arm64_node115.node.yml +0 -931
  286. package/dist/bindings/cws_darwin_arm64_node127.node.dSYM/Contents/Info.plist +0 -20
  287. package/dist/bindings/cws_darwin_arm64_node127.node.dSYM/Contents/Resources/DWARF/cws_darwin_arm64_node127.node +0 -0
  288. package/dist/bindings/cws_darwin_arm64_node127.node.dSYM/Contents/Resources/Relocations/aarch64/cws_darwin_arm64_node127.node.yml +0 -931
@@ -0,0 +1,770 @@
1
+ #pragma once
2
+
3
+ #if defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS
4
+
5
+ #include <async_wrap.h>
6
+ #include <base_object.h>
7
+ #include <env.h>
8
+ #include <memory_tracker.h>
9
+ #include <ngtcp2/ngtcp2.h>
10
+ #include <node_http_common.h>
11
+ #include <node_sockaddr.h>
12
+ #include <timer_wrap.h>
13
+ #include <util.h>
14
+ #include <optional>
15
+ #include "bindingdata.h"
16
+ #include "cid.h"
17
+ #include "data.h"
18
+ #include "defs.h"
19
+ #include "packet.h"
20
+ #include "preferredaddress.h"
21
+ #include "sessionticket.h"
22
+ #include "streams.h"
23
+ #include "tlscontext.h"
24
+ #include "transportparams.h"
25
+
26
+ namespace node::quic {
27
+
28
+ class Endpoint;
29
+
30
+ // A Session represents one half of a persistent connection between two QUIC
31
+ // peers. Every Session is established first by performing a TLS handshake in
32
+ // which the client sends an initial packet to the server containing a TLS
33
+ // client hello. Once the TLS handshake has been completed, the Session can be
34
+ // used to open one or more Streams for the actual data flow back and forth.
35
+ //
36
+ // While client and server Sessions are created in slightly different ways,
37
+ // their lifecycles are generally identical:
38
+ //
39
+ // A Session is either acting as a Client or as a Server.
40
+ //
41
+ // Client Sessions are always created using Endpoint::Connect()
42
+ //
43
+ // Server Sessions are always created by an Endpoint receiving a valid initial
44
+ // request received from a remote client.
45
+ //
46
+ // As soon as Sessions of either type are created, they will immediately start
47
+ // working through the TLS handshake to establish the crypographic keys used to
48
+ // secure the communication. Once those keys are established, the Session can be
49
+ // used to open Streams. Based on how the Session is configured, any number of
50
+ // Streams can exist concurrently on a single Session.
51
+ //
52
+ // The Session wraps an ngtcp2_conn that is initialized when the session object
53
+ // is created. This ngtcp2_conn is destroyed when the session object is freed.
54
+ // However, the session can be in a closed/destroyed state and still have a
55
+ // valid ngtcp2_conn pointer. This is important because the ngtcp2 still might
56
+ // be processing data within the scope of an ngtcp2_conn after the session
57
+ // object itself is closed/destroyed by user code.
58
+ class Session final : public AsyncWrap, private SessionTicket::AppData::Source {
59
+ public:
60
+ SessionTicket::AppData::Source& ticket_app_data_source() { return *this; }
61
+ // For simplicity, we use the same Application::Options struct for all
62
+ // Application types. This may change in the future. Not all of the options
63
+ // are going to be relevant for all Application types.
64
+ struct Application_Options final : public MemoryRetainer {
65
+ // The maximum number of header pairs permitted for a Stream.
66
+ // Only relevant if the selected application supports headers.
67
+ uint64_t max_header_pairs = DEFAULT_MAX_HEADER_LIST_PAIRS;
68
+
69
+ // The maximum total number of header bytes (including header
70
+ // name and value) permitted for a Stream.
71
+ // Only relevant if the selected application supports headers.
72
+ uint64_t max_header_length = DEFAULT_MAX_HEADER_LENGTH;
73
+
74
+ // HTTP/3 specific options.
75
+ // The maximum header section size advertised to the peer in SETTINGS.
76
+ // Defaults to match max_header_length so the SETTINGS frame accurately
77
+ // reflects the enforcement limit. A value of 0 would incorrectly tell
78
+ // the peer not to send any headers at all.
79
+ uint64_t max_field_section_size = DEFAULT_MAX_HEADER_LENGTH;
80
+ uint64_t qpack_max_dtable_capacity = 4096;
81
+ uint64_t qpack_encoder_max_dtable_capacity = 4096;
82
+ uint64_t qpack_blocked_streams = 100;
83
+
84
+ bool enable_connect_protocol = true;
85
+ bool enable_datagrams = true;
86
+
87
+ operator const nghttp3_settings() const;
88
+
89
+ SET_NO_MEMORY_INFO()
90
+ SET_MEMORY_INFO_NAME(Application::Options)
91
+ SET_SELF_SIZE(Options)
92
+
93
+ static v8::Maybe<Application_Options> From(Environment* env,
94
+ v8::Local<v8::Value> value);
95
+
96
+ std::string ToString() const;
97
+
98
+ v8::MaybeLocal<v8::Object> ToObject(Environment* env) const;
99
+
100
+ static const Application_Options kDefault;
101
+ };
102
+
103
+ // An Application implements the ALPN-protocol specific semantics on behalf
104
+ // of a QUIC Session.
105
+ class Application;
106
+
107
+ // A block of pending outbound stream data, passed between the application
108
+ // layer (which fills it via GetStreamData) and the send pump (which hands
109
+ // it to ngtcp2_conn_writev_stream and commits the accepted length).
110
+ struct StreamData final {
111
+ // The actual number of vectors in the struct, up to kMaxVectorCount.
112
+ size_t count = 0;
113
+ // The stream identifier. If this is a negative value then no stream is
114
+ // identified.
115
+ stream_id id = -1;
116
+ bool fin = false;
117
+ ngtcp2_vec data[kMaxVectorCount]{};
118
+ BaseObjectPtr<Stream> stream;
119
+
120
+ inline operator const ngtcp2_vec*() const { return data; }
121
+ inline operator ngtcp2_vec*() { return data; }
122
+
123
+ std::string ToString() const;
124
+ };
125
+
126
+ // Decode the first ALPN protocol name from wire format (length-prefixed).
127
+ static std::string_view DecodeAlpn(std::string_view wire);
128
+
129
+ // Select the Application implementation based on the negotiated ALPN.
130
+ // h3 (and h3-XX variants) map to Http3ApplicationImpl; all others map
131
+ // to DefaultApplication. Sets the application_type state field.
132
+ std::unique_ptr<Application> SelectApplicationFromAlpn(std::string_view alpn);
133
+
134
+ // Install the Application on the session. Called at construction for
135
+ // clients (ALPN known upfront) or from OnSelectAlpn for servers
136
+ // (ALPN negotiated during handshake). Must be called before any
137
+ // application data is received.
138
+ void SetApplication(std::unique_ptr<Application> app);
139
+ // Controls which datagram to drop when the pending datagram queue is full.
140
+ enum class DatagramDropPolicy : uint8_t {
141
+ DROP_OLDEST = 0, // Drop the oldest queued datagram (default).
142
+ DROP_NEWEST = 1, // Drop the incoming datagram.
143
+ };
144
+
145
+ // The options used to configure a session. Most of these deal directly with
146
+ // the transport parameters that are exchanged with the remote peer during
147
+ // handshake.
148
+ struct Options final : public MemoryRetainer {
149
+ // The QUIC protocol version requested for the session.
150
+ uint32_t version = NGTCP2_PROTO_VER_MAX;
151
+
152
+ // Te minimum QUIC protocol version supported by this session.
153
+ uint32_t min_version = NGTCP2_PROTO_VER_MIN;
154
+
155
+ // By default a client session will ignore the preferred address
156
+ // advertised by the the server. This option is only relevant for
157
+ // client sessions.
158
+ PreferredAddress::Policy preferred_address_strategy =
159
+ PreferredAddress::Policy::IGNORE_PREFERRED;
160
+
161
+ TransportParams::Options transport_params =
162
+ TransportParams::Options::kDefault;
163
+ TLSContext::Options tls_options = TLSContext::Options::kDefault;
164
+ std::unordered_map<std::string, TLSContext::Options> sni;
165
+
166
+ // A reference to the CID::Factory used to generate CID instances
167
+ // for this session.
168
+ const CID::Factory* cid_factory = &CID::Factory::random();
169
+ // If the CID::Factory is a base object, we keep a reference to it
170
+ // so that it cannot be garbage collected.
171
+ BaseObjectPtr<BaseObject> cid_factory_ref;
172
+
173
+ // Application-specific options (used for HTTP/3 if the negotiated
174
+ // ALPN selects Http3ApplicationImpl).
175
+ Application_Options application_options = Application_Options::kDefault;
176
+
177
+ // When true, QLog output will be enabled for the session.
178
+ bool qlog = false;
179
+
180
+ // The amount of time (in milliseconds) that the endpoint will wait for the
181
+ // completion of the TLS handshake. If the handshake does not complete
182
+ // within this time, the session is closed. This prevents a peer from
183
+ // holding a session open indefinitely in the handshake state, consuming
184
+ // server resources (ngtcp2 connection, TLS state, JS objects) without
185
+ // ever completing the connection. The default of 10 seconds is generous
186
+ // enough to accommodate slow networks with retransmissions while still
187
+ // bounding resource exposure. Set to UINT64_MAX to disable.
188
+ static constexpr uint64_t DEFAULT_HANDSHAKE_TIMEOUT = 10'000;
189
+ uint64_t handshake_timeout = DEFAULT_HANDSHAKE_TIMEOUT;
190
+
191
+ // The initial round-trip time estimate in milliseconds. ngtcp2 uses this
192
+ // for PTO computation, initial pacing, and early loss detection before
193
+ // the first RTT sample is collected. The default of 0 uses ngtcp2's
194
+ // built-in default of 333ms, which is appropriate for the general
195
+ // internet. For low-latency environments (e.g., loopback or same-rack
196
+ // deployments), setting a value closer to the actual RTT avoids
197
+ // unnecessarily conservative initial behavior.
198
+ uint64_t initial_rtt = 0;
199
+
200
+ // The keep-alive timeout in milliseconds. When set to a non-zero value,
201
+ // ngtcp2 will automatically send PING frames to keep the connection alive
202
+ // before the idle timeout fires. Set to 0 to disable (default).
203
+ uint64_t keep_alive_timeout = 0;
204
+
205
+ // Maximum initial flow control window size for a stream.
206
+ uint64_t max_stream_window = 0;
207
+
208
+ // Maximum initial flow control window size for the connection.
209
+ uint64_t max_window = 0;
210
+
211
+ // The max_payload_size is the maximum size of a serialized QUIC packet. It
212
+ // should always be set small enough to fit within a single MTU without
213
+ // fragmentation. The default is set by the QUIC specification at 1200. This
214
+ // value should not be changed unless you know for sure that the entire path
215
+ // supports a given MTU without fragmenting at any point in the path.
216
+ uint64_t max_payload_size = kDefaultMaxPacketLength;
217
+
218
+ // The unacknowledged_packet_threshold is the maximum number of
219
+ // unacknowledged packets that an ngtcp2 session will accumulate before
220
+ // sending an acknowledgement. Setting this to 0 uses the ngtcp2 defaults,
221
+ // which is what most will want. The value can be changed to fine tune some
222
+ // of the performance characteristics of the session. This should only be
223
+ // changed if you have a really good reason for doing so.
224
+ uint64_t unacknowledged_packet_threshold = 0;
225
+
226
+ // There are several common congestion control algorithms that ngtcp2 uses
227
+ // to determine how it manages the flow control window: RENO, CUBIC, and
228
+ // BBR. The details of how each works is not relevant here. The choice of
229
+ // which to use by default is arbitrary and we can choose whichever we'd
230
+ // like. Additional performance profiling will be needed to determine which
231
+ // is the better of the two for our needs.
232
+ ngtcp2_cc_algo cc_algorithm = CC_ALGO_CUBIC;
233
+
234
+ // Controls which datagram to drop when the pending queue is full.
235
+ DatagramDropPolicy datagram_drop_policy = DatagramDropPolicy::DROP_OLDEST;
236
+
237
+ // Maximum number of SendPendingData attempts before a datagram is
238
+ // abandoned. When a datagram cannot be sent due to congestion control
239
+ // or packet size constraints, it remains in the queue and the counter
240
+ // is incremented. Once the limit is reached, the datagram is dropped
241
+ // and reported as abandoned. Range: 1-255. Default: 5.
242
+ uint8_t max_datagram_send_attempts = 5;
243
+
244
+ // Multiplier for the Probe Timeout (PTO) used to compute the draining
245
+ // period duration after receiving CONNECTION_CLOSE. RFC 9000 Section
246
+ // 10.2 requires at least 3x PTO. Range: 3-255. Default: 3.
247
+ uint8_t draining_period_multiplier = 3;
248
+
249
+ // The amount of time (in milliseconds) that a stream can be idle
250
+ // (no data received) before it is automatically destroyed. This
251
+ // protects against slowloris-style attacks where a peer opens streams
252
+ // but never sends data, holding server resources indefinitely.
253
+ // Only applies to peer-initiated streams. Set to 0 to disable.
254
+ static constexpr uint64_t DEFAULT_STREAM_IDLE_TIMEOUT = 30'000;
255
+ uint64_t stream_idle_timeout = DEFAULT_STREAM_IDLE_TIMEOUT;
256
+
257
+ // An optional NEW_TOKEN from a previous connection to the same
258
+ // server. When set, the token is included in the Initial packet
259
+ // to skip address validation. Client-side only.
260
+ std::optional<Store> token;
261
+
262
+ void MemoryInfo(MemoryTracker* tracker) const override;
263
+ SET_MEMORY_INFO_NAME(Session::Options)
264
+ SET_SELF_SIZE(Options)
265
+
266
+ static v8::Maybe<Options> From(Environment* env,
267
+ v8::Local<v8::Value> value);
268
+
269
+ std::string ToString() const;
270
+ };
271
+
272
+ // The additional configuration settings used to create a specific session.
273
+ // while the Options above can be used to configure multiple sessions, a
274
+ // single Config is used to create a single session, which is why they are
275
+ // kept separate.
276
+ struct Config final : MemoryRetainer {
277
+ // Is the Session acting as a client or a server?
278
+ Side side;
279
+
280
+ // The options to use for this session.
281
+ Options options;
282
+
283
+ // The actual QUIC version identified for this session.
284
+ uint32_t version;
285
+
286
+ SocketAddress local_address;
287
+ SocketAddress remote_address;
288
+
289
+ // The destination CID, identifying the remote peer. This value is always
290
+ // provided by the remote peer.
291
+ CID dcid = CID::kInvalid;
292
+
293
+ // The source CID, identifying this session. This value is always created
294
+ // locally.
295
+ CID scid = CID::kInvalid;
296
+
297
+ // Used only by client sessions to identify the original DCID
298
+ // used to initiate the connection.
299
+ CID ocid = CID::kInvalid;
300
+ CID retry_scid = CID::kInvalid;
301
+ CID preferred_address_cid = CID::kInvalid;
302
+
303
+ ngtcp2_settings settings = {};
304
+ operator ngtcp2_settings*() { return &settings; }
305
+ operator const ngtcp2_settings*() const { return &settings; }
306
+
307
+ Config(Environment* env,
308
+ Side side,
309
+ const Options& options,
310
+ uint32_t version,
311
+ const SocketAddress& local_address,
312
+ const SocketAddress& remote_address,
313
+ const CID& dcid,
314
+ const CID& scid,
315
+ const CID& ocid = CID::kInvalid);
316
+
317
+ Config(Environment* env,
318
+ const Options& options,
319
+ const SocketAddress& local_address,
320
+ const SocketAddress& remote_address,
321
+ const CID& ocid = CID::kInvalid);
322
+
323
+ void set_token(const uint8_t* token,
324
+ size_t len,
325
+ ngtcp2_token_type type = NGTCP2_TOKEN_TYPE_UNKNOWN);
326
+ void set_token(const RetryToken& token);
327
+ void set_token(const RegularToken& token);
328
+
329
+ void MemoryInfo(MemoryTracker* tracker) const override;
330
+ SET_MEMORY_INFO_NAME(Session::Config)
331
+ SET_SELF_SIZE(Config)
332
+
333
+ std::string ToString() const;
334
+ };
335
+
336
+ JS_CONSTRUCTOR(Session);
337
+ JS_BINDING_INIT_BOILERPLATE();
338
+
339
+ static BaseObjectPtr<Session> Create(
340
+ Endpoint* endpoint,
341
+ const Config& config,
342
+ TLSContext* tls_context,
343
+ const std::optional<SessionTicket>& ticket);
344
+
345
+ // Really should be private but MakeDetachedBaseObject needs visibility.
346
+ Session(Endpoint* endpoint,
347
+ v8::Local<v8::Object> object,
348
+ const Config& config,
349
+ TLSContext* tls_context,
350
+ const std::optional<SessionTicket>& ticket);
351
+ DISALLOW_COPY_AND_MOVE(Session)
352
+ ~Session() override;
353
+
354
+ bool is_destroyed() const;
355
+ bool is_server() const;
356
+
357
+ uint32_t version() const;
358
+ Endpoint& endpoint() const;
359
+ TLSSession& tls_session() const;
360
+ bool has_application() const;
361
+ Application& application() const;
362
+ const Config& config() const;
363
+ const Options& options() const;
364
+ const SocketAddress& remote_address() const;
365
+ const SocketAddress& local_address() const;
366
+
367
+ std::string diagnostic_name() const override;
368
+
369
+ void MemoryInfo(MemoryTracker* tracker) const override;
370
+ SET_MEMORY_INFO_NAME(Session)
371
+ SET_SELF_SIZE(Session)
372
+
373
+ operator ngtcp2_conn*() const;
374
+
375
+ // Ensures that the session/application sends pending data when the scope
376
+ // exits. Scopes can be nested. When nested, pending data will be sent
377
+ // only when the outermost scope is exited.
378
+ struct SendPendingDataScope final {
379
+ Session* session;
380
+ explicit SendPendingDataScope(Session* session);
381
+ explicit SendPendingDataScope(const BaseObjectPtr<Session>& session);
382
+ ~SendPendingDataScope();
383
+ DISALLOW_COPY_AND_MOVE(SendPendingDataScope)
384
+ };
385
+
386
+ struct State;
387
+ struct Stats;
388
+
389
+ void HandleQlog(uint32_t flags, const void* data, size_t len);
390
+ void EmitQlog(uint32_t flags, std::string_view data);
391
+
392
+ private:
393
+ struct Impl;
394
+
395
+ using StreamsMap = std::unordered_map<stream_id, BaseObjectPtr<Stream>>;
396
+ using QuicConnectionPointer = DeleteFnPtr<ngtcp2_conn, ngtcp2_conn_del>;
397
+
398
+ struct PathValidationFlags final {
399
+ bool preferredAddress = false;
400
+ };
401
+
402
+ struct DatagramReceivedFlags final {
403
+ bool early = false;
404
+ };
405
+
406
+ bool Receive(const uint8_t* data,
407
+ size_t len,
408
+ const SocketAddress& local_address,
409
+ const SocketAddress& remote_address,
410
+ const PacketInfo& pkt_info = PacketInfo(),
411
+ uint64_t ts = 0);
412
+
413
+ // ReadPacket processes a single inbound packet through ngtcp2 without
414
+ // triggering SendPendingData. This is the building block for batched
415
+ // receive processing: the caller (Endpoint::Receive) accumulates
416
+ // dirty sessions and a uv_check callback flushes them after all
417
+ // packets in the I/O burst have been read.
418
+ // Receive() is kept as a convenience wrapper that calls ReadPacket()
419
+ // then triggers SendPendingData (for paths like Connect that need
420
+ // immediate response).
421
+ // The data pointer is used synchronously — ngtcp2_conn_read_pkt does
422
+ // not retain a reference after returning, so the caller's buffer can
423
+ // be reused immediately.
424
+ // When ts is 0 (the default), uv_hrtime() is called internally.
425
+ // The batched receive path caches a timestamp and passes it to all
426
+ // ReadPacket() calls in the same I/O burst.
427
+ bool ReadPacket(const uint8_t* data,
428
+ size_t len,
429
+ const SocketAddress& local_address,
430
+ const SocketAddress& remote_address,
431
+ const PacketInfo& pkt_info = PacketInfo(),
432
+ uint64_t ts = 0);
433
+
434
+ // Called by BindingData's flush callback to trigger SendPendingData
435
+ // on this session. Encapsulates the application() access so that
436
+ // bindingdata.cc doesn't need the full Application type definition.
437
+ void FlushPendingData();
438
+
439
+ // The send pump: the primary driver for serializing outbound data.
440
+ // Loops through available stream data (pulled from the application
441
+ // layer via GetStreamData/StreamCommit) and pending datagrams,
442
+ // generating packets until there is no more data to send or the
443
+ // packet budget for this call is exhausted.
444
+ void SendPendingData();
445
+
446
+ Packet::Ptr CreateStreamDataPacket();
447
+
448
+ // Tries to pack a pending datagram into the current packet buffer.
449
+ // If a negative value is returned, it is either NGTCP2_ERR_WRITE_MORE or
450
+ // fatal error; the caller must check. If > 0 is returned, the packet is done
451
+ // and the value is the size of the finalized packet. If 0 is returned,
452
+ // the datagram is either congestion limited or was abandoned.
453
+ ssize_t TryWritePendingDatagram(PathStorage* path,
454
+ uint8_t* dest,
455
+ size_t destlen,
456
+ uint64_t ts);
457
+
458
+ // Write the given stream_data into the buffer. The PacketInfo out-param
459
+ // is populated by ngtcp2 with per-packet metadata (e.g., ECN codepoint)
460
+ // that should be applied when sending the packet.
461
+ ssize_t WriteVStream(PathStorage* path,
462
+ PacketInfo* pi,
463
+ uint8_t* buf,
464
+ ssize_t* ndatalen,
465
+ size_t max_packet_size,
466
+ const StreamData& stream_data,
467
+ uint64_t ts);
468
+
469
+ // Send a batch of packets accumulated by SendPendingData. Uses
470
+ // Endpoint::SendBatch (uv_udp_try_send2 / sendmmsg) for synchronous
471
+ // batched delivery when called from the deferred flush path.
472
+ // Handles per-packet path updates and cross-endpoint redirects.
473
+ // All Ptr entries are consumed (released or moved) on return.
474
+ void SendBatch(Packet::Ptr* packets, PathStorage* paths, size_t count);
475
+
476
+ void Send(Packet::Ptr packet);
477
+ void Send(Packet::Ptr packet, const PathStorage& path);
478
+ datagram_id SendDatagram(Store&& data);
479
+
480
+ // Pending datagram accessors for use by SendPendingData.
481
+ struct PendingDatagram {
482
+ datagram_id id;
483
+ Store data;
484
+ uint8_t send_attempts = 0;
485
+ };
486
+ bool HasPendingDatagrams() const;
487
+ PendingDatagram& PeekPendingDatagram();
488
+ void PopPendingDatagram();
489
+ size_t PendingDatagramCount() const;
490
+ void DatagramSent(datagram_id id);
491
+
492
+ // A non-const variation to allow certain modifications.
493
+ Config& config();
494
+
495
+ enum class CreateStreamOption : uint8_t {
496
+ NOTIFY,
497
+ DO_NOT_NOTIFY,
498
+ };
499
+ BaseObjectPtr<Stream> FindStream(stream_id id) const;
500
+ // Returns a copy of the streams map (safe for iteration while streams
501
+ // are being destroyed).
502
+ StreamsMap streams() const;
503
+ BaseObjectPtr<Stream> CreateStream(
504
+ stream_id id,
505
+ CreateStreamOption option = CreateStreamOption::NOTIFY,
506
+ std::shared_ptr<DataQueue> data_source = nullptr);
507
+ void AddStream(BaseObjectPtr<Stream> stream,
508
+ CreateStreamOption option = CreateStreamOption::NOTIFY);
509
+ void RemoveStream(stream_id id);
510
+ void ResumeStream(stream_id id);
511
+ void StreamDataBlocked(stream_id id);
512
+ void ShutdownStream(stream_id id, QuicError error = QuicError());
513
+ void ShutdownStreamWrite(stream_id id, QuicError code = QuicError());
514
+
515
+ // Use the configured CID::Factory to generate a new CID.
516
+ CID new_cid(size_t len = CID::kMaxLength) const;
517
+
518
+ const TransportParams local_transport_params() const;
519
+ const TransportParams remote_transport_params() const;
520
+
521
+ bool is_destroyed_or_closing() const;
522
+ size_t max_packet_size() const;
523
+ void set_priority_supported(bool on = true);
524
+
525
+ // Open a new locally-initialized stream with the specified directionality.
526
+ // If the session is not yet in a state where the stream can be openen --
527
+ // such as when the handshake is not yet sufficiently far along and ORTT
528
+ // session resumption is not being used -- then the stream will be created
529
+ // in a pending state where actually opening the stream will be deferred.
530
+ v8::MaybeLocal<v8::Object> OpenStream(
531
+ Direction direction, std::shared_ptr<DataQueue> data_source = nullptr);
532
+
533
+ void ExtendStreamOffset(stream_id id, size_t amount);
534
+ void ExtendOffset(size_t amount);
535
+ void SetLastError(QuicError&& error);
536
+ uint64_t max_data_left() const;
537
+
538
+ // Transport operations that protocol applications (e.g. HTTP/3) invoke on
539
+ // the session, encapsulating ngtcp2 transport details here.
540
+
541
+ // Open a unidirectional stream, setting *id on success, or returning false
542
+ bool OpenUnidirectionalStream(stream_id* id);
543
+
544
+ void ExtendMaxStreams(Direction direction, uint64_t max);
545
+
546
+ // Signal that we've consumed `len` bytes on stream `id` to update flow
547
+ // control
548
+ void Consume(stream_id id, size_t len);
549
+
550
+ // Record an application-level error on the connection without closing it.
551
+ void SetApplicationError(error_code app_error_code);
552
+
553
+ PendingStream::PendingStreamQueue& pending_bidi_stream_queue() const;
554
+ PendingStream::PendingStreamQueue& pending_uni_stream_queue() const;
555
+
556
+ // Implementation of SessionTicket::AppData::Source
557
+ void CollectSessionTicketAppData(
558
+ SessionTicket::AppData* app_data) const override;
559
+ SessionTicket::AppData::Status ExtractSessionTicketAppData(
560
+ const SessionTicket::AppData& app_data,
561
+ SessionTicket::AppData::Source::Flag flag) override;
562
+
563
+ // Returns true if the Session has entered the closing period after sending a
564
+ // CONNECTION_CLOSE. While true, the Session is only permitted to transmit
565
+ // CONNECTION_CLOSE frames until either the idle timeout period elapses or
566
+ // until the Session is explicitly destroyed.
567
+ bool is_in_closing_period() const;
568
+
569
+ // Returns true if the Session has received a CONNECTION_CLOSE frame from the
570
+ // peer. Once in the draining period, the Session is not permitted to send any
571
+ // frames to the peer. The Session will be silently closed after either the
572
+ // idle timeout period elapses or until the Session is explicitly destroyed.
573
+ bool is_in_draining_period() const;
574
+
575
+ // Returns false if the Session is currently in a state where it is unable to
576
+ // transmit any packets.
577
+ bool can_send_packets() const;
578
+
579
+ // Returns false if the Session is currently in a state where it cannot create
580
+ // new streams. Specifically, a stream is not in a state to create streams if
581
+ // it has been destroyed or is closing.
582
+ bool can_create_streams() const;
583
+
584
+ // Returns false if the Session is currently in a state where it cannot open
585
+ // a new locally-initiated stream. When using 0RTT session resumption, this
586
+ // will become true immediately after the session ticket and transport params
587
+ // have been configured. Otherwise, it becomes true after the remote transport
588
+ // params and tx keys have been installed.
589
+ bool can_open_streams() const;
590
+
591
+ uint64_t max_local_streams_uni() const;
592
+ uint64_t max_local_streams_bidi() const;
593
+
594
+ bool wants_session_ticket() const;
595
+ void SetStreamOpenAllowed();
596
+
597
+ // Populate state buffer fields from the 0-RTT transport params.
598
+ // Called after ngtcp2_conn_decode_and_set_0rtt_transport_params
599
+ // succeeds, so that values like maxDatagramSize are available
600
+ // before the handshake completes.
601
+ void PopulateEarlyTransportParamsState();
602
+
603
+ void set_hello_processed() { hello_processed_ = true; }
604
+
605
+ // True once the negotiated TLS parameters (SNI, ALPN) are final.
606
+ bool tls_info_ready() const;
607
+
608
+ // It's a terrible name but "wrapped" here means that the Session has been
609
+ // passed out to JavaScript and should be "wrapped" by whatever handler is
610
+ // defined there to manage it.
611
+ void set_wrapped();
612
+
613
+ // True while JS emits must be held for later replay, before the handshake
614
+ // is complete and the server session event has been emitted.
615
+ bool must_defer_emits() const;
616
+
617
+ // Replays, in order, any emits held while must_defer_emits() was true.
618
+ // Called synchronously right after the new-session emit.
619
+ void ReplayDeferredEmits();
620
+
621
+ // Queues fn to be replayed by ReplayDeferredEmits(). Out-of-line so the
622
+ // header does not need the full Impl definition.
623
+ void QueueDeferredEmit(std::function<void()> fn);
624
+
625
+ template <typename F>
626
+ bool DeferEmit(F&& fn) {
627
+ if (!must_defer_emits()) return false;
628
+ QueueDeferredEmit(std::forward<F>(fn));
629
+ return true;
630
+ }
631
+
632
+ enum class CloseMethod : uint8_t {
633
+ // Immediate close with a roundtrip through JavaScript, causing all
634
+ // currently opened streams to be closed. An attempt will be made to
635
+ // send a CONNECTION_CLOSE frame to the peer. If closing while within
636
+ // the ngtcp2 callback scope, sending the CONNECTION_CLOSE will be
637
+ // deferred until the scope exits.
638
+ DEFAULT,
639
+ // Same as DEFAULT except that no attempt to notify the peer will be
640
+ // made.
641
+ SILENT,
642
+ // Closing gracefully disables the ability to open or accept new streams
643
+ // for this Session. Existing streams are allowed to close naturally on
644
+ // their own.
645
+ // Once called, the Session will be immediately closed once there are no
646
+ // remaining streams. No notification is given to the connected peer that
647
+ // we are in a graceful closing state. A CONNECTION_CLOSE will be sent
648
+ // only once FinishClose() is called.
649
+ GRACEFUL
650
+ };
651
+ // Initiate closing of the session.
652
+ void Close(CloseMethod method = CloseMethod::DEFAULT);
653
+
654
+ void FinishClose();
655
+ void Destroy();
656
+
657
+ // Close the session and send a connection close packet to the peer.
658
+ // If creating the packet fails the session will be silently closed.
659
+ // The connection close packet will use the value of last_error_ as
660
+ // the error code transmitted to the peer.
661
+ void SendConnectionClose();
662
+ void OnTimeout();
663
+
664
+ void UpdateTimer();
665
+ // Has to be called after certain operations that generate packets.
666
+ void UpdatePacketTxTime();
667
+ void UpdateDataStats();
668
+ void CheckStreamIdleTimeout(uint64_t now);
669
+ void UpdatePath(const PathStorage& path);
670
+
671
+ void ProcessPendingBidiStreams();
672
+ void ProcessPendingUniStreams();
673
+
674
+ // JavaScript callouts
675
+
676
+ void EmitClose(const QuicError& error = QuicError());
677
+ void EmitGoaway(stream_id last_stream_id);
678
+
679
+ // Sets the max datagram payload size in the shared state. Used by
680
+ // Http3ApplicationImpl to block datagram sends when the peer's
681
+ // SETTINGS_H3_DATAGRAM=0 (RFC 9297 §3).
682
+ void set_max_datagram_size(uint16_t size);
683
+ void EmitDatagram(Store&& datagram, DatagramReceivedFlags flag);
684
+ void EmitDatagramStatus(datagram_id id, DatagramStatus status);
685
+ void EmitHandshakeComplete();
686
+ void EmitKeylog(const char* line);
687
+ void EmitOrigins(std::vector<std::string>&& origins);
688
+
689
+ struct ValidatedPath {
690
+ std::shared_ptr<SocketAddress> local;
691
+ std::shared_ptr<SocketAddress> remote;
692
+ };
693
+
694
+ void EmitPathValidation(PathValidationResult result,
695
+ PathValidationFlags flags,
696
+ const ValidatedPath& newPath,
697
+ const std::optional<ValidatedPath>& oldPath);
698
+ void EmitSessionTicket(Store&& ticket);
699
+ void EmitNewToken(const uint8_t* token, size_t len);
700
+ void EmitEarlyDataRejected();
701
+ void DestroyAllStreams(const QuicError& error);
702
+ void EmitStream(const BaseObjectWeakPtr<Stream>& stream);
703
+ void EmitVersionNegotiation(const ngtcp2_pkt_hd& hd,
704
+ const uint32_t* sv,
705
+ size_t nsv);
706
+ void EmitApplication();
707
+ void DatagramStatus(datagram_id datagramId, DatagramStatus status);
708
+ void DatagramReceived(const uint8_t* data,
709
+ size_t datalen,
710
+ DatagramReceivedFlags flag);
711
+ void GenerateNewConnectionId(ngtcp2_cid* cid,
712
+ size_t len,
713
+ ngtcp2_stateless_reset_token* token);
714
+ bool HandshakeCompleted();
715
+ void HandshakeConfirmed();
716
+ void SelectPreferredAddress(PreferredAddress* preferredAddress);
717
+
718
+ QuicConnectionPointer InitConnection();
719
+
720
+ Side side_;
721
+ const ngtcp2_mem* allocator_;
722
+ std::unique_ptr<Impl> impl_;
723
+
724
+ struct Flags {
725
+ // These flags live on Session (not Impl) so that the NgTcp2CallbackScope
726
+ // and NgHttp3CallbackScope destructors can safely clear them even after
727
+ // Impl has been destroyed via MakeCallback re-entrancy during a callback.
728
+ // The scope is placed at the ngtcp2/nghttp3 entry point (e.g. Receive,
729
+ // OnTimeout) rather than on individual callbacks, so the deferred destroy
730
+ // only fires after all callbacks for that entry point have completed.
731
+ uint8_t in_ngtcp2_callback_scope : 1 = 0;
732
+ uint8_t in_nghttp3_callback_scope : 1 = 0;
733
+ uint8_t destroy_deferred : 1 = 0;
734
+ // Set when this session is in BindingData's pending_flush_sessions_ vector.
735
+ // Cleared by the flush callback before calling SendPendingData.
736
+ // Provides O(1) dedup so a session receiving multiple packets in one I/O
737
+ // burst is only scheduled for flush once.
738
+ uint8_t pending_flush : 1 = 0;
739
+ // When true, Session::Send prefers synchronous delivery via
740
+ // Endpoint::SendOrTrySend (uv_udp_try_send with async fallback).
741
+ // Set during FlushPendingData to avoid the one-tick latency of
742
+ // async-only sends from the uv_check callback.
743
+ uint8_t prefer_try_send : 1 = 0;
744
+ };
745
+ Flags flags_;
746
+
747
+ bool hello_processed_ = false;
748
+
749
+ QuicConnectionPointer connection_;
750
+ std::unique_ptr<TLSSession> tls_session_;
751
+ friend struct NgTcp2CallbackScope;
752
+ friend struct NgHttp3CallbackScope;
753
+ friend class Application;
754
+ friend class BindingData;
755
+ friend class DefaultApplication;
756
+ friend class Http3ApplicationImpl;
757
+ friend class Endpoint;
758
+ friend class SessionManager;
759
+ friend class Stream;
760
+ friend class PendingStream;
761
+ friend class TLSContext;
762
+ friend class TLSSession;
763
+ friend class TransportParams;
764
+ friend struct Impl;
765
+ friend struct SendPendingDataScope;
766
+ };
767
+
768
+ } // namespace node::quic
769
+
770
+ #endif // defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS