@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
package/src/WebSocket.cpp CHANGED
@@ -7,6 +7,7 @@ namespace cWS {
7
7
  template <bool isServer>
8
8
  WebSocket<isServer>::WebSocket(bool perMessageDeflate, cS::Socket *socket) : cS::Socket(std::move(*socket)) {
9
9
  compressionStatus = perMessageDeflate ? CompressionStatus::ENABLED : CompressionStatus::DISABLED;
10
+ setCorkable(true);
10
11
 
11
12
  // if we are created in a group with sliding deflate window allocate it here
12
13
  if (Group<isServer>::from(this)->extensionOptions & SLIDING_DEFLATE_WINDOW) {
@@ -214,10 +215,10 @@ void WebSocket<isServer>::terminate() {
214
215
 
215
216
  #ifdef CWS_THREADSAFE
216
217
  std::lock_guard<std::recursive_mutex> lockGuard(*nodeData->asyncMutex);
218
+ #endif
217
219
  if (isClosed()) {
218
220
  return;
219
221
  }
220
- #endif
221
222
 
222
223
  WebSocket<isServer>::onEnd(this);
223
224
  }
@@ -288,6 +289,8 @@ template <bool isServer>
288
289
  void WebSocket<isServer>::onEnd(cS::Socket *s) {
289
290
  WebSocket<isServer> *webSocket = static_cast<WebSocket<isServer> *>(s);
290
291
 
292
+ webSocket->flushCorkedOnClose();
293
+
291
294
  if (!webSocket->isShuttingDown()) {
292
295
  Group<isServer>::from(webSocket)->removeWebSocket(webSocket);
293
296
  Group<isServer>::from(webSocket)->disconnectionHandler(webSocket, 1006, nullptr, 0);
package/src/cSNode.cpp CHANGED
@@ -49,6 +49,7 @@ Node::Node(int recvLength, int prePadding, int postPadding, bool useDefaultLoop)
49
49
 
50
50
  nodeData->loop = loop;
51
51
  nodeData->asyncMutex = &asyncMutex;
52
+ nodeData->corkState = new NodeData::CorkState;
52
53
 
53
54
  int indices = NodeData::getMemoryBlockIndex(NodeData::preAllocMaxSize) + 1;
54
55
  nodeData->preAlloc = new char*[indices];
@@ -81,6 +82,7 @@ Node::~Node() {
81
82
  }
82
83
  }
83
84
  delete [] nodeData->preAlloc;
85
+ delete nodeData->corkState;
84
86
  delete nodeData->netContext;
85
87
  delete nodeData;
86
88
  loop->destroy();
package/src/cSNode.h CHANGED
@@ -89,6 +89,10 @@ public:
89
89
  return loop;
90
90
  }
91
91
 
92
+ NodeData *getNodeData() {
93
+ return nodeData;
94
+ }
95
+
92
96
  template <cS::Socket *I(Socket *s), void C(Socket *p, bool error)>
93
97
  Socket *connect(const char *hostname, int port, bool secure, NodeData *nodeData) {
94
98
  Context *netContext = nodeData->netContext;
@@ -0,0 +1,6 @@
1
+ // This is an auto generated file, please do not edit.
2
+ // Refer to tools/dep_updaters/update-acorn.sh
3
+ #ifndef SRC_ACORN_VERSION_H_
4
+ #define SRC_ACORN_VERSION_H_
5
+ #define ACORN_VERSION "8.18.0"
6
+ #endif // SRC_ACORN_VERSION_H_
@@ -0,0 +1,264 @@
1
+ #ifndef SRC_ALIASED_BUFFER_INL_H_
2
+ #define SRC_ALIASED_BUFFER_INL_H_
3
+
4
+ #if defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS
5
+
6
+ #include "aliased_buffer.h"
7
+ #include "memory_tracker-inl.h"
8
+ #include "util-inl.h"
9
+
10
+ namespace node {
11
+
12
+ typedef size_t AliasedBufferIndex;
13
+
14
+ template <typename NativeT, typename V8T>
15
+ requires std::is_scalar_v<NativeT>
16
+ AliasedBufferBase<NativeT, V8T>::AliasedBufferBase(
17
+ v8::Isolate* isolate, const size_t count, const AliasedBufferIndex* index)
18
+ : isolate_(isolate), count_(count), byte_offset_(0), index_(index) {
19
+ CHECK_GT(count, 0);
20
+ if (index != nullptr) {
21
+ // Will be deserialized later.
22
+ return;
23
+ }
24
+ const v8::HandleScope handle_scope(isolate_);
25
+ const size_t size_in_bytes =
26
+ MultiplyWithOverflowCheck(sizeof(NativeT), count);
27
+
28
+ // allocate v8 ArrayBuffer
29
+ v8::Local<v8::ArrayBuffer> ab = v8::ArrayBuffer::New(isolate_, size_in_bytes);
30
+ buffer_ = static_cast<NativeT*>(ab->Data());
31
+
32
+ // allocate v8 TypedArray
33
+ v8::Local<V8T> js_array = V8T::New(ab, byte_offset_, count);
34
+ js_array_ = v8::Global<V8T>(isolate, js_array);
35
+ }
36
+
37
+ template <typename NativeT, typename V8T>
38
+ requires std::is_scalar_v<NativeT>
39
+ AliasedBufferBase<NativeT, V8T>::AliasedBufferBase(
40
+ v8::Isolate* isolate,
41
+ const size_t byte_offset,
42
+ const size_t count,
43
+ const AliasedBufferBase<uint8_t, v8::Uint8Array>& backing_buffer,
44
+ const AliasedBufferIndex* index)
45
+ : isolate_(isolate),
46
+ count_(count),
47
+ byte_offset_(byte_offset),
48
+ index_(index) {
49
+ if (index != nullptr) {
50
+ // Will be deserialized later.
51
+ return;
52
+ }
53
+ const v8::HandleScope handle_scope(isolate_);
54
+ v8::Local<v8::ArrayBuffer> ab = backing_buffer.GetArrayBuffer();
55
+
56
+ // validate that the byte_offset is aligned with sizeof(NativeT)
57
+ CHECK_EQ(byte_offset & (sizeof(NativeT) - 1), 0);
58
+ // validate this fits inside the backing buffer
59
+ CHECK_LE(MultiplyWithOverflowCheck(sizeof(NativeT), count),
60
+ ab->ByteLength() - byte_offset);
61
+
62
+ buffer_ = reinterpret_cast<NativeT*>(
63
+ const_cast<uint8_t*>(backing_buffer.GetNativeBuffer() + byte_offset));
64
+
65
+ v8::Local<V8T> js_array = V8T::New(ab, byte_offset, count);
66
+ js_array_ = v8::Global<V8T>(isolate, js_array);
67
+ }
68
+
69
+ template <typename NativeT, typename V8T>
70
+ requires std::is_scalar_v<NativeT>
71
+ AliasedBufferBase<NativeT, V8T>::AliasedBufferBase(
72
+ const AliasedBufferBase& that)
73
+ : isolate_(that.isolate_),
74
+ count_(that.count_),
75
+ byte_offset_(that.byte_offset_),
76
+ buffer_(that.buffer_) {
77
+ js_array_ = v8::Global<V8T>(that.isolate_, that.GetJSArray());
78
+ DCHECK(is_valid());
79
+ }
80
+
81
+ template <typename NativeT, typename V8T>
82
+ requires std::is_scalar_v<NativeT>
83
+ AliasedBufferIndex AliasedBufferBase<NativeT, V8T>::Serialize(
84
+ v8::Local<v8::Context> context, v8::SnapshotCreator* creator) {
85
+ DCHECK(is_valid());
86
+ return creator->AddData(context, GetJSArray());
87
+ }
88
+
89
+ template <typename NativeT, typename V8T>
90
+ requires std::is_scalar_v<NativeT>
91
+ inline void AliasedBufferBase<NativeT, V8T>::Deserialize(
92
+ v8::Local<v8::Context> context) {
93
+ DCHECK_NOT_NULL(index_);
94
+ v8::Local<V8T> arr =
95
+ context->GetDataFromSnapshotOnce<V8T>(*index_).ToLocalChecked();
96
+ // These may not hold true for AliasedBuffers that have grown, so should
97
+ // be removed when we expand the snapshot support.
98
+ DCHECK_EQ(count_, arr->Length());
99
+ DCHECK_EQ(byte_offset_, arr->ByteOffset());
100
+ uint8_t* raw = static_cast<uint8_t*>(arr->Buffer()->Data());
101
+ buffer_ = reinterpret_cast<NativeT*>(raw + byte_offset_);
102
+ js_array_.Reset(isolate_, arr);
103
+ index_ = nullptr;
104
+ }
105
+
106
+ template <typename NativeT, typename V8T>
107
+ requires std::is_scalar_v<NativeT>
108
+ AliasedBufferBase<NativeT, V8T>& AliasedBufferBase<NativeT, V8T>::operator=(
109
+ AliasedBufferBase<NativeT, V8T>&& that) noexcept {
110
+ DCHECK(is_valid());
111
+ this->~AliasedBufferBase();
112
+ isolate_ = that.isolate_;
113
+ count_ = that.count_;
114
+ byte_offset_ = that.byte_offset_;
115
+ buffer_ = that.buffer_;
116
+
117
+ js_array_.Reset(isolate_, that.js_array_.Get(isolate_));
118
+
119
+ that.buffer_ = nullptr;
120
+ that.js_array_.Reset();
121
+ return *this;
122
+ }
123
+
124
+ template <typename NativeT, typename V8T>
125
+ requires std::is_scalar_v<NativeT>
126
+ v8::Local<V8T> AliasedBufferBase<NativeT, V8T>::GetJSArray() const {
127
+ DCHECK(is_valid());
128
+ return js_array_.Get(isolate_);
129
+ }
130
+
131
+ template <typename NativeT, typename V8T>
132
+ requires std::is_scalar_v<NativeT>
133
+ void AliasedBufferBase<NativeT, V8T>::Release() {
134
+ DCHECK_NULL(index_);
135
+ js_array_.Reset();
136
+ }
137
+
138
+ template <typename NativeT, typename V8T>
139
+ requires std::is_scalar_v<NativeT>
140
+ inline void AliasedBufferBase<NativeT, V8T>::MakeWeak() {
141
+ DCHECK(is_valid());
142
+ js_array_.SetWeak();
143
+ }
144
+
145
+ template <typename NativeT, typename V8T>
146
+ requires std::is_scalar_v<NativeT>
147
+ v8::Local<v8::ArrayBuffer> AliasedBufferBase<NativeT, V8T>::GetArrayBuffer()
148
+ const {
149
+ return GetJSArray()->Buffer();
150
+ }
151
+
152
+ template <typename NativeT, typename V8T>
153
+ requires std::is_scalar_v<NativeT>
154
+ inline const NativeT* AliasedBufferBase<NativeT, V8T>::GetNativeBuffer() const {
155
+ DCHECK(is_valid());
156
+ return buffer_;
157
+ }
158
+
159
+ template <typename NativeT, typename V8T>
160
+ requires std::is_scalar_v<NativeT>
161
+ inline const NativeT* AliasedBufferBase<NativeT, V8T>::operator*() const {
162
+ return GetNativeBuffer();
163
+ }
164
+
165
+ template <typename NativeT, typename V8T>
166
+ requires std::is_scalar_v<NativeT>
167
+ inline void AliasedBufferBase<NativeT, V8T>::SetValue(const size_t index,
168
+ NativeT value) {
169
+ DCHECK_LT(index, count_);
170
+ DCHECK(is_valid());
171
+ buffer_[index] = value;
172
+ }
173
+
174
+ template <typename NativeT, typename V8T>
175
+ requires std::is_scalar_v<NativeT>
176
+ inline const NativeT AliasedBufferBase<NativeT, V8T>::GetValue(
177
+ const size_t index) const {
178
+ DCHECK(is_valid());
179
+ DCHECK_LT(index, count_);
180
+ return buffer_[index];
181
+ }
182
+
183
+ template <typename NativeT, typename V8T>
184
+ requires std::is_scalar_v<NativeT>
185
+ typename AliasedBufferBase<NativeT, V8T>::Reference
186
+ AliasedBufferBase<NativeT, V8T>::operator[](size_t index) {
187
+ DCHECK(is_valid());
188
+ return Reference(this, index);
189
+ }
190
+
191
+ template <typename NativeT, typename V8T>
192
+ requires std::is_scalar_v<NativeT>
193
+ NativeT AliasedBufferBase<NativeT, V8T>::operator[](size_t index) const {
194
+ return GetValue(index);
195
+ }
196
+
197
+ template <typename NativeT, typename V8T>
198
+ requires std::is_scalar_v<NativeT>
199
+ size_t AliasedBufferBase<NativeT, V8T>::Length() const {
200
+ return count_;
201
+ }
202
+
203
+ template <typename NativeT, typename V8T>
204
+ requires std::is_scalar_v<NativeT>
205
+ void AliasedBufferBase<NativeT, V8T>::reserve(size_t new_capacity) {
206
+ DCHECK(is_valid());
207
+ DCHECK_GE(new_capacity, count_);
208
+ DCHECK_EQ(byte_offset_, 0);
209
+ const v8::HandleScope handle_scope(isolate_);
210
+
211
+ const size_t old_size_in_bytes = sizeof(NativeT) * count_;
212
+ const size_t new_size_in_bytes =
213
+ MultiplyWithOverflowCheck(sizeof(NativeT), new_capacity);
214
+
215
+ // allocate v8 new ArrayBuffer
216
+ v8::Local<v8::ArrayBuffer> ab =
217
+ v8::ArrayBuffer::New(isolate_, new_size_in_bytes);
218
+
219
+ // allocate new native buffer
220
+ NativeT* new_buffer = static_cast<NativeT*>(ab->Data());
221
+ // copy old content
222
+ memcpy(new_buffer, buffer_, old_size_in_bytes);
223
+
224
+ // allocate v8 TypedArray
225
+ v8::Local<V8T> js_array = V8T::New(ab, byte_offset_, new_capacity);
226
+
227
+ // move over old v8 TypedArray
228
+ js_array_ = std::move(v8::Global<V8T>(isolate_, js_array));
229
+
230
+ buffer_ = new_buffer;
231
+ count_ = new_capacity;
232
+ }
233
+
234
+ template <typename NativeT, typename V8T>
235
+ requires std::is_scalar_v<NativeT>
236
+ inline bool AliasedBufferBase<NativeT, V8T>::is_valid() const {
237
+ return index_ == nullptr && !js_array_.IsEmpty();
238
+ }
239
+
240
+ template <typename NativeT, typename V8T>
241
+ requires std::is_scalar_v<NativeT>
242
+ inline size_t AliasedBufferBase<NativeT, V8T>::SelfSize() const {
243
+ return sizeof(*this);
244
+ }
245
+
246
+ #define VM(NativeT, V8T) \
247
+ template <> \
248
+ inline const char* AliasedBufferBase<NativeT, v8::V8T>::MemoryInfoName() \
249
+ const { \
250
+ return "Aliased" #V8T; \
251
+ } \
252
+ template <> \
253
+ inline void AliasedBufferBase<NativeT, v8::V8T>::MemoryInfo( \
254
+ node::MemoryTracker* tracker) const { \
255
+ tracker->TrackField("js_array", js_array_); \
256
+ }
257
+ ALIASED_BUFFER_LIST(VM)
258
+ #undef VM
259
+
260
+ } // namespace node
261
+
262
+ #endif // defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS
263
+
264
+ #endif // SRC_ALIASED_BUFFER_INL_H_
@@ -0,0 +1,205 @@
1
+ #ifndef SRC_ALIASED_BUFFER_H_
2
+ #define SRC_ALIASED_BUFFER_H_
3
+
4
+ #if defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS
5
+
6
+ #include <cinttypes>
7
+ #include "memory_tracker.h"
8
+ #include "v8.h"
9
+
10
+ namespace node {
11
+
12
+ typedef size_t AliasedBufferIndex;
13
+
14
+ /**
15
+ * Do not use this class directly when creating instances of it - use the
16
+ * Aliased*Array defined at the end of this file instead.
17
+ *
18
+ * This class encapsulates the technique of having a native buffer mapped to
19
+ * a JS object. Writes to the native buffer can happen efficiently without
20
+ * going through JS, and the data is then available to user's via the exposed
21
+ * JS object.
22
+ *
23
+ * While this technique is computationally efficient, it is effectively a
24
+ * write to JS program state w/out going through the standard
25
+ * (monitored) API. Thus, any VM capabilities to detect the modification are
26
+ * circumvented.
27
+ *
28
+ * The encapsulation herein provides a placeholder where such writes can be
29
+ * observed. Any notification APIs will be left as a future exercise.
30
+ */
31
+ template <class NativeT, class V8T>
32
+ requires std::is_scalar_v<NativeT>
33
+ class AliasedBufferBase final : public MemoryRetainer {
34
+ public:
35
+ AliasedBufferBase(v8::Isolate* isolate,
36
+ size_t count,
37
+ const AliasedBufferIndex* index = nullptr);
38
+
39
+ /**
40
+ * Create an AliasedBufferBase over a sub-region of another aliased buffer.
41
+ * The two will share a v8::ArrayBuffer instance &
42
+ * a native buffer, but will each read/write to different sections of the
43
+ * native buffer.
44
+ *
45
+ * Note that byte_offset must be aligned by sizeof(NativeT).
46
+ */
47
+ // TODO(refack): refactor into a non-owning `AliasedBufferBaseView`
48
+ AliasedBufferBase(
49
+ v8::Isolate* isolate,
50
+ size_t byte_offset,
51
+ size_t count,
52
+ const AliasedBufferBase<uint8_t, v8::Uint8Array>& backing_buffer,
53
+ const AliasedBufferIndex* index = nullptr);
54
+
55
+ AliasedBufferBase(const AliasedBufferBase& that);
56
+
57
+ AliasedBufferIndex Serialize(v8::Local<v8::Context> context,
58
+ v8::SnapshotCreator* creator);
59
+
60
+ void Deserialize(v8::Local<v8::Context> context);
61
+
62
+ AliasedBufferBase& operator=(AliasedBufferBase&& that) noexcept;
63
+
64
+ /**
65
+ * Helper class that is returned from operator[] to support assignment into
66
+ * a specified location.
67
+ */
68
+ class Reference {
69
+ public:
70
+ Reference(AliasedBufferBase* aliased_buffer, const size_t index)
71
+ : aliased_buffer_(aliased_buffer), index_(index) {}
72
+
73
+ Reference(const Reference& that)
74
+ : aliased_buffer_(that.aliased_buffer_),
75
+ index_(that.index_) {
76
+ }
77
+
78
+ Reference& operator=(const NativeT& val) {
79
+ aliased_buffer_->SetValue(index_, val);
80
+ return *this;
81
+ }
82
+
83
+ Reference& operator=(const Reference& val) {
84
+ return *this = static_cast<NativeT>(val);
85
+ }
86
+
87
+ operator NativeT() const {
88
+ return aliased_buffer_->GetValue(index_);
89
+ }
90
+
91
+ Reference& operator+=(const NativeT& val) {
92
+ const NativeT current = aliased_buffer_->GetValue(index_);
93
+ aliased_buffer_->SetValue(index_, current + val);
94
+ return *this;
95
+ }
96
+
97
+ Reference& operator+=(const Reference& val) {
98
+ return this->operator+=(static_cast<NativeT>(val));
99
+ }
100
+
101
+ Reference& operator-=(const NativeT& val) {
102
+ const NativeT current = aliased_buffer_->GetValue(index_);
103
+ aliased_buffer_->SetValue(index_, current - val);
104
+ return *this;
105
+ }
106
+
107
+ private:
108
+ AliasedBufferBase* aliased_buffer_;
109
+ size_t index_;
110
+ };
111
+
112
+ /**
113
+ * Get the underlying v8 TypedArray overlaid on top of the native buffer
114
+ */
115
+ v8::Local<V8T> GetJSArray() const;
116
+
117
+ void Release();
118
+
119
+ /**
120
+ * Make the global reference to the typed array weak. The caller must make
121
+ * sure that no operation can be done on the AliasedBuffer when the typed
122
+ * array becomes unreachable. Usually this means the caller must maintain
123
+ * a JS reference to the typed array from JS object.
124
+ */
125
+ void MakeWeak();
126
+
127
+ /**
128
+ * Get the underlying v8::ArrayBuffer underlying the TypedArray and
129
+ * overlaying the native buffer
130
+ */
131
+ v8::Local<v8::ArrayBuffer> GetArrayBuffer() const;
132
+
133
+ /**
134
+ * Get the underlying native buffer. Note that all reads/writes should occur
135
+ * through the GetValue/SetValue/operator[] methods
136
+ */
137
+ const NativeT* GetNativeBuffer() const;
138
+
139
+ /**
140
+ * Synonym for GetBuffer()
141
+ */
142
+ const NativeT* operator*() const;
143
+
144
+ /**
145
+ * Set position index to given value.
146
+ */
147
+ void SetValue(size_t index, NativeT value);
148
+
149
+ /**
150
+ * Get value at position index
151
+ */
152
+ const NativeT GetValue(size_t index) const;
153
+
154
+ /**
155
+ * Effectively, a synonym for GetValue/SetValue
156
+ */
157
+ Reference operator[](size_t index);
158
+
159
+ NativeT operator[](size_t index) const;
160
+
161
+ size_t Length() const;
162
+
163
+ // Should only be used to extend the array.
164
+ // Should only be used on an owning array, not one created as a sub array of
165
+ // an owning `AliasedBufferBase`.
166
+ void reserve(size_t new_capacity);
167
+
168
+ size_t SelfSize() const override;
169
+
170
+ const char* MemoryInfoName() const override;
171
+ void MemoryInfo(MemoryTracker* tracker) const override;
172
+
173
+ private:
174
+ bool is_valid() const;
175
+ v8::Isolate* isolate_ = nullptr;
176
+ size_t count_ = 0;
177
+ size_t byte_offset_ = 0;
178
+ NativeT* buffer_ = nullptr;
179
+ v8::Global<V8T> js_array_;
180
+
181
+ // Deserialize data
182
+ const AliasedBufferIndex* index_ = nullptr;
183
+ };
184
+
185
+ #define ALIASED_BUFFER_LIST(V) \
186
+ V(int8_t, Int8Array) \
187
+ V(uint8_t, Uint8Array) \
188
+ V(int16_t, Int16Array) \
189
+ V(uint16_t, Uint16Array) \
190
+ V(int32_t, Int32Array) \
191
+ V(uint32_t, Uint32Array) \
192
+ V(float, Float32Array) \
193
+ V(double, Float64Array) \
194
+ V(int64_t, BigInt64Array)
195
+
196
+ #define V(NativeT, V8T) \
197
+ typedef AliasedBufferBase<NativeT, v8::V8T> Aliased##V8T;
198
+ ALIASED_BUFFER_LIST(V)
199
+ #undef V
200
+
201
+ } // namespace node
202
+
203
+ #endif // defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS
204
+
205
+ #endif // SRC_ALIASED_BUFFER_H_
@@ -0,0 +1,143 @@
1
+ #ifndef SRC_ALIASED_STRUCT_INL_H_
2
+ #define SRC_ALIASED_STRUCT_INL_H_
3
+
4
+ #if defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS
5
+
6
+ #include "aliased_struct.h"
7
+ #include "v8.h"
8
+ #include <memory>
9
+
10
+ namespace node {
11
+
12
+ template <typename T>
13
+ template <typename... Args>
14
+ AliasedStruct<T>::AliasedStruct(v8::Isolate* isolate, Args&&... args)
15
+ : isolate_(isolate) {
16
+ const v8::HandleScope handle_scope(isolate);
17
+
18
+ store_ = v8::ArrayBuffer::NewBackingStore(isolate, sizeof(T));
19
+ ptr_ = new (store_->Data()) T(std::forward<Args>(args)...);
20
+ DCHECK_NOT_NULL(ptr_);
21
+
22
+ v8::Local<v8::ArrayBuffer> buffer = v8::ArrayBuffer::New(isolate, store_);
23
+ buffer_ = v8::Global<v8::ArrayBuffer>(isolate, buffer);
24
+ }
25
+
26
+ template <typename T>
27
+ AliasedStruct<T>::AliasedStruct(const AliasedStruct& that)
28
+ : AliasedStruct(that.isolate_, *that) {}
29
+
30
+ template <typename T>
31
+ AliasedStruct<T>& AliasedStruct<T>::operator=(
32
+ AliasedStruct<T>&& that) noexcept {
33
+ this->~AliasedStruct();
34
+ isolate_ = that.isolate_;
35
+ store_ = that.store_;
36
+ ptr_ = that.ptr_;
37
+
38
+ buffer_ = std::move(that.buffer_);
39
+
40
+ that.ptr_ = nullptr;
41
+ that.store_.reset();
42
+ return *this;
43
+ }
44
+
45
+ template <typename T>
46
+ AliasedStruct<T>::~AliasedStruct() {
47
+ if (ptr_ != nullptr) ptr_->~T();
48
+ }
49
+
50
+ // ---------------------------------------------------------------------------
51
+ // AliasedStructArena implementation
52
+ // ---------------------------------------------------------------------------
53
+
54
+ template <typename T, size_t kPageBytes>
55
+ typename AliasedStructArena<T, kPageBytes>::Page*
56
+ AliasedStructArena<T, kPageBytes>::FindOrCreatePage(v8::Isolate* isolate) {
57
+ for (auto& p : pages_) {
58
+ if (p->HasFreeSlots()) return p.get();
59
+ }
60
+ auto p = std::make_unique<Page>();
61
+ p->Init(isolate);
62
+ Page* raw = p.get();
63
+ pages_.push_back(std::move(p));
64
+ return raw;
65
+ }
66
+
67
+ template <typename T, size_t kPageBytes>
68
+ template <typename... Args>
69
+ typename AliasedStructArena<T, kPageBytes>::Slot
70
+ AliasedStructArena<T, kPageBytes>::Allocate(v8::Isolate* isolate,
71
+ Args&&... args) {
72
+ Page* page = FindOrCreatePage(isolate);
73
+ DCHECK(page->HasFreeSlots());
74
+
75
+ uint32_t idx = page->free_head;
76
+ T* raw = &page->base[idx];
77
+
78
+ // Advance freelist before placement new overwrites the linkage.
79
+ page->free_head = *reinterpret_cast<uint32_t*>(raw);
80
+ page->used_count++;
81
+
82
+ // Placement-construct T in the slot.
83
+ T* ptr = new (raw) T(std::forward<Args>(args)...);
84
+
85
+ Slot slot;
86
+ slot.page = static_cast<void*>(page);
87
+ slot.ptr = static_cast<void*>(ptr);
88
+ slot.index = idx;
89
+ slot.byte_offset = reinterpret_cast<const uint8_t*>(ptr) -
90
+ static_cast<const uint8_t*>(page->store->Data());
91
+ return slot;
92
+ }
93
+
94
+ template <typename T, size_t kPageBytes>
95
+ void AliasedStructArena<T, kPageBytes>::Release(
96
+ typename AliasedStructArena<T, kPageBytes>::Slot&& slot) {
97
+ if (!slot) return;
98
+ auto* page = static_cast<Page*>(slot.page);
99
+ auto* ptr = static_cast<T*>(slot.ptr);
100
+ uint32_t idx = slot.index;
101
+
102
+ // Destruct and zero so JS views see clean data.
103
+ ptr->~T();
104
+ memset(ptr, 0, sizeof(T));
105
+
106
+ // Push onto page freelist.
107
+ *reinterpret_cast<uint32_t*>(ptr) = page->free_head;
108
+ page->free_head = idx;
109
+ page->used_count--;
110
+
111
+ slot.page = nullptr;
112
+ slot.ptr = nullptr;
113
+
114
+ // Drop empty pages. The shared_ptr<BackingStore> ensures the
115
+ // underlying memory stays alive until V8 GCs any remaining JS
116
+ // references to the page's ArrayBuffer/views.
117
+ if (page->used_count == 0) {
118
+ for (auto it = pages_.begin(); it != pages_.end(); ++it) {
119
+ if (it->get() == page) {
120
+ pages_.erase(it);
121
+ break;
122
+ }
123
+ }
124
+ }
125
+ }
126
+
127
+ template <typename T, size_t kPageBytes>
128
+ void AliasedStructArena<T, kPageBytes>::ReleaseSlot(ArenaSlotBase& base) {
129
+ Slot slot;
130
+ slot.page = base.page;
131
+ slot.ptr = base.ptr;
132
+ slot.index = base.index;
133
+ slot.byte_offset = base.byte_offset;
134
+ Release(std::move(slot));
135
+ base.page = nullptr;
136
+ base.ptr = nullptr;
137
+ }
138
+
139
+ } // namespace node
140
+
141
+ #endif // defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS
142
+
143
+ #endif // SRC_ALIASED_STRUCT_INL_H_