@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,162 @@
1
+ #ifndef SRC_NODE_WORKER_H_
2
+ #define SRC_NODE_WORKER_H_
3
+
4
+ #if defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS
5
+
6
+ #include <optional>
7
+ #include <unordered_map>
8
+ #include "json_utils.h"
9
+ #include "node_exit_code.h"
10
+ #include "node_messaging.h"
11
+ #include "util.h"
12
+ #include "uv.h"
13
+
14
+ namespace node {
15
+
16
+ struct SnapshotData;
17
+ namespace worker {
18
+
19
+ class WorkerThreadData;
20
+
21
+ enum ResourceLimits {
22
+ kMaxYoungGenerationSizeMb,
23
+ kMaxOldGenerationSizeMb,
24
+ kCodeRangeSizeMb,
25
+ kStackSizeMb,
26
+ kTotalResourceLimitCount
27
+ };
28
+
29
+ // A worker thread, as represented in its parent thread.
30
+ class Worker : public AsyncWrap {
31
+ public:
32
+ Worker(Environment* env,
33
+ v8::Local<v8::Object> wrap,
34
+ const std::string& url,
35
+ const std::string& name,
36
+ std::shared_ptr<PerIsolateOptions> per_isolate_opts,
37
+ std::vector<std::string>&& exec_argv,
38
+ std::shared_ptr<KVStore> env_vars,
39
+ const SnapshotData* snapshot_data,
40
+ const bool is_internal);
41
+ ~Worker() override;
42
+
43
+ // Run the worker. This is only called from the worker thread.
44
+ void Run();
45
+
46
+ // Forcibly exit the thread with a specified exit code. This may be called
47
+ // from any thread. `error_code` and `error_message` can be used to create
48
+ // a custom `'error'` event before emitting `'exit'`.
49
+ void Exit(ExitCode code,
50
+ const char* error_code = nullptr,
51
+ const char* error_message = nullptr);
52
+
53
+ // Wait for the worker thread to stop (in a blocking manner).
54
+ void JoinThread();
55
+
56
+ template <typename Fn>
57
+ inline bool RequestInterrupt(Fn&& cb);
58
+
59
+ SET_NO_MEMORY_INFO()
60
+ SET_MEMORY_INFO_NAME(Worker)
61
+ SET_SELF_SIZE(Worker)
62
+ bool IsNotIndicativeOfMemoryLeakAtExit() const override;
63
+
64
+ bool is_stopped() const;
65
+ const SnapshotData* snapshot_data() const { return snapshot_data_; }
66
+ bool is_internal() const { return is_internal_; }
67
+ std::string_view name() const { return name_; }
68
+
69
+ static void New(const v8::FunctionCallbackInfo<v8::Value>& args);
70
+ static void StartThread(const v8::FunctionCallbackInfo<v8::Value>& args);
71
+ static void StopThread(const v8::FunctionCallbackInfo<v8::Value>& args);
72
+ static void HasRef(const v8::FunctionCallbackInfo<v8::Value>& args);
73
+ static void Ref(const v8::FunctionCallbackInfo<v8::Value>& args);
74
+ static void Unref(const v8::FunctionCallbackInfo<v8::Value>& args);
75
+ static void GetResourceLimits(
76
+ const v8::FunctionCallbackInfo<v8::Value>& args);
77
+ v8::Local<v8::Float64Array> GetResourceLimits(v8::Isolate* isolate) const;
78
+ static void TakeHeapSnapshot(const v8::FunctionCallbackInfo<v8::Value>& args);
79
+ static void LoopIdleTime(const v8::FunctionCallbackInfo<v8::Value>& args);
80
+ static void LoopStartTime(const v8::FunctionCallbackInfo<v8::Value>& args);
81
+ static void GetHeapStatistics(
82
+ const v8::FunctionCallbackInfo<v8::Value>& args);
83
+ static void CpuUsage(const v8::FunctionCallbackInfo<v8::Value>& args);
84
+ static void StartCpuProfile(const v8::FunctionCallbackInfo<v8::Value>& args);
85
+ static void StopCpuProfile(const v8::FunctionCallbackInfo<v8::Value>& args);
86
+ static void StartHeapProfile(const v8::FunctionCallbackInfo<v8::Value>& args);
87
+ static void StopHeapProfile(const v8::FunctionCallbackInfo<v8::Value>& args);
88
+
89
+ private:
90
+ bool CreateEnvMessagePort(Environment* env);
91
+ static size_t NearHeapLimit(void* data, size_t current_heap_limit,
92
+ size_t initial_heap_limit);
93
+
94
+ std::shared_ptr<PerIsolateOptions> per_isolate_opts_;
95
+ std::vector<std::string> exec_argv_;
96
+ std::vector<std::string> argv_;
97
+
98
+ MultiIsolatePlatform* platform_;
99
+ v8::Isolate* isolate_ = nullptr;
100
+ std::optional<uv_thread_t> tid_; // Set while the thread is running
101
+
102
+ std::unique_ptr<InspectorParentHandle> inspector_parent_handle_;
103
+
104
+ // This mutex protects access to all variables listed below it.
105
+ mutable Mutex mutex_;
106
+
107
+ const char* custom_error_ = nullptr;
108
+ std::string custom_error_str_;
109
+ ExitCode exit_code_ = ExitCode::kNoFailure;
110
+ ThreadId thread_id_;
111
+ uintptr_t stack_base_ = 0;
112
+ // Optional name used for debugging in inspector and trace events.
113
+ std::string name_;
114
+
115
+ // Custom resource constraints:
116
+ double resource_limits_[kTotalResourceLimitCount];
117
+ void UpdateResourceConstraints(v8::ResourceConstraints* constraints);
118
+
119
+ // Full size of the thread's stack.
120
+ size_t stack_size_ = 4 * 1024 * 1024;
121
+ // Stack buffer size that is not available to the JS engine.
122
+ static constexpr size_t kStackBufferSize = 192 * 1024;
123
+
124
+ std::unique_ptr<MessagePortData> child_port_data_;
125
+ std::shared_ptr<KVStore> env_vars_;
126
+ EmbedderPreloadCallback embedder_preload_;
127
+
128
+ // A raw flag that is used by creator and worker threads to
129
+ // sync up on pre-mature termination of worker - while in the
130
+ // warmup phase. Once the worker is fully warmed up, use the
131
+ // async handle of the worker's Environment for the same purpose.
132
+ bool stopped_ = true;
133
+
134
+ bool has_ref_ = true;
135
+ uint64_t environment_flags_ = EnvironmentFlags::kNoFlags;
136
+
137
+ // The real Environment of the worker object. It has a lesser
138
+ // lifespan than the worker object itself - comes to life
139
+ // when the worker thread creates a new Environment, and gets
140
+ // destroyed alongwith the worker thread.
141
+ Environment* env_ = nullptr;
142
+
143
+ const SnapshotData* snapshot_data_ = nullptr;
144
+ const bool is_internal_;
145
+ friend class WorkerThreadData;
146
+ };
147
+
148
+ template <typename Fn>
149
+ bool Worker::RequestInterrupt(Fn&& cb) {
150
+ Mutex::ScopedLock lock(mutex_);
151
+ if (env_ == nullptr) return false;
152
+ env_->RequestInterrupt(std::move(cb));
153
+ return true;
154
+ }
155
+
156
+ } // namespace worker
157
+ } // namespace node
158
+
159
+ #endif // defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS
160
+
161
+
162
+ #endif // SRC_NODE_WORKER_H_
@@ -0,0 +1,36 @@
1
+ #ifndef SRC_PATH_H_
2
+ #define SRC_PATH_H_
3
+
4
+ #if defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS
5
+
6
+ #include <string>
7
+ #include <vector>
8
+ #include "node_options-inl.h"
9
+ #include "util-inl.h"
10
+
11
+ namespace node {
12
+
13
+ constexpr bool IsPathSeparator(const char c) noexcept;
14
+
15
+ std::string NormalizeString(const std::string_view path,
16
+ bool allowAboveRoot,
17
+ const std::string_view separator);
18
+
19
+ std::string PathResolve(Environment* env,
20
+ const std::vector<std::string_view>& paths);
21
+ std::string NormalizeFileURLOrPath(Environment* env, std::string_view path);
22
+ bool IsAbsoluteFilePath(std::string_view path);
23
+
24
+ #ifdef _WIN32
25
+ constexpr bool IsWindowsDeviceRoot(const char c) noexcept;
26
+ constexpr bool IsWindowsDriveLetter(const std::string_view path) noexcept;
27
+ #endif // _WIN32
28
+
29
+ void ToNamespacedPath(Environment* env, BufferValue* path);
30
+ void FromNamespacedPath(std::string* path);
31
+
32
+ } // namespace node
33
+
34
+ #endif // defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS
35
+
36
+ #endif // SRC_PATH_H_
@@ -0,0 +1,50 @@
1
+ #ifndef SRC_PERMISSION_BOOLEAN_PERMISSION_H_
2
+ #define SRC_PERMISSION_BOOLEAN_PERMISSION_H_
3
+
4
+ #if defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS
5
+
6
+ #include "permission/permission_base.h"
7
+
8
+ namespace node::permission {
9
+
10
+ // A simple boolean permission that either denies or allows access.
11
+ // Used for permission scopes that don't need per-resource granularity.
12
+ template <bool deny_only>
13
+ class BooleanPermission final : public PermissionBase {
14
+ public:
15
+ void Apply(Environment* env,
16
+ std::span<const std::string> allow,
17
+ PermissionScope scope) override {
18
+ flag_ = true;
19
+ }
20
+
21
+ void Drop(Environment* env,
22
+ PermissionScope scope,
23
+ std::string_view param) override {
24
+ flag_ = deny_only;
25
+ }
26
+
27
+ bool is_granted(Environment* env,
28
+ PermissionScope perm,
29
+ std::string_view param) const override {
30
+ if constexpr (deny_only) {
31
+ return !flag_;
32
+ } else {
33
+ return flag_;
34
+ }
35
+ }
36
+
37
+ private:
38
+ bool flag_ = false;
39
+ };
40
+
41
+ // Once denied, the permission cannot be re-granted.
42
+ using DenyOnlyPermission = BooleanPermission<true>;
43
+
44
+ // Apply grants access, Drop revokes.
45
+ using AllowRevokePermission = BooleanPermission<false>;
46
+
47
+ } // namespace node::permission
48
+
49
+ #endif // defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS
50
+ #endif // SRC_PERMISSION_BOOLEAN_PERMISSION_H_
@@ -0,0 +1,192 @@
1
+ #ifndef SRC_PERMISSION_FS_PERMISSION_H_
2
+ #define SRC_PERMISSION_FS_PERMISSION_H_
3
+
4
+ #if defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS
5
+
6
+ #include <vector>
7
+ #include "permission/permission_base.h"
8
+ #include "util.h"
9
+
10
+ namespace node::permission {
11
+
12
+ class FSPermission final : public PermissionBase {
13
+ public:
14
+ void Apply(Environment* env,
15
+ std::span<const std::string> allow,
16
+ PermissionScope scope) override;
17
+ void Drop(Environment* env,
18
+ PermissionScope scope,
19
+ std::string_view param) override;
20
+ bool is_granted(Environment* env,
21
+ PermissionScope perm,
22
+ std::string_view param) const override;
23
+
24
+ struct RadixTree {
25
+ struct Node {
26
+ std::string prefix;
27
+ std::vector<std::pair<char, Node*>> children;
28
+ Node* wildcard_child = nullptr;
29
+ bool is_leaf = false;
30
+
31
+ explicit Node(std::string_view pre) : prefix(pre) {}
32
+
33
+ Node() = default;
34
+
35
+ Node* FindChild(char label) const {
36
+ for (const auto& [c, node] : children) {
37
+ if (c == label) return node;
38
+ }
39
+ return nullptr;
40
+ }
41
+
42
+ void SetChild(char label, Node* node) {
43
+ for (auto& [c, n] : children) {
44
+ if (c == label) {
45
+ n = node;
46
+ return;
47
+ }
48
+ }
49
+ children.emplace_back(label, node);
50
+ }
51
+
52
+ Node* CreateChild(std::string_view path_prefix) {
53
+ if (path_prefix.empty() && !is_leaf) {
54
+ is_leaf = true;
55
+ return this;
56
+ }
57
+
58
+ CHECK(!path_prefix.empty());
59
+ char label = path_prefix[0];
60
+
61
+ Node* child = FindChild(label);
62
+ if (child == nullptr) {
63
+ child = new Node(path_prefix);
64
+ children.emplace_back(label, child);
65
+ return child;
66
+ }
67
+ bool child_was_end_node = child->IsEndNode();
68
+
69
+ // swap prefix
70
+ size_t i = 0;
71
+ size_t prefix_len = path_prefix.length();
72
+ for (; i < child->prefix.length(); ++i) {
73
+ if (i >= prefix_len || path_prefix[i] != child->prefix[i]) {
74
+ std::string parent_prefix(child->prefix.substr(0, i));
75
+ std::string child_prefix(child->prefix.substr(i));
76
+
77
+ child->prefix = child_prefix;
78
+ Node* split_child = new Node(parent_prefix);
79
+ split_child->children.emplace_back(child_prefix[0], child);
80
+ SetChild(parent_prefix[0], split_child);
81
+
82
+ return split_child->CreateChild(path_prefix.substr(i));
83
+ }
84
+ }
85
+ if (child_was_end_node) {
86
+ child->is_leaf = true;
87
+ }
88
+ return child->CreateChild(path_prefix.substr(i));
89
+ }
90
+
91
+ Node* CreateWildcardChild() {
92
+ if (wildcard_child != nullptr) {
93
+ return wildcard_child;
94
+ }
95
+ wildcard_child = new Node();
96
+ return wildcard_child;
97
+ }
98
+
99
+ Node* NextNode(std::string_view path, size_t idx) const {
100
+ if (idx >= path.length()) {
101
+ return nullptr;
102
+ }
103
+
104
+ // wildcard node takes precedence
105
+ if (children.size() > 1) {
106
+ Node* wc = FindChild('*');
107
+ if (wc != nullptr) {
108
+ return wc;
109
+ }
110
+ }
111
+
112
+ Node* child = FindChild(path[idx]);
113
+ if (child == nullptr) {
114
+ return nullptr;
115
+ }
116
+ // match prefix
117
+ size_t prefix_len = child->prefix.length();
118
+ for (size_t i = 0; i < path.length(); ++i) {
119
+ if (i >= prefix_len || child->prefix[i] == '*') {
120
+ return child;
121
+ }
122
+
123
+ // Handle optional trailing
124
+ // path = /home/subdirectory
125
+ // child = subdirectory/*
126
+ if (idx >= path.length()) {
127
+ if (child->prefix[i] == node::kPathSeparator) {
128
+ continue;
129
+ }
130
+ // Path is exhausted but prefix expects more characters
131
+ return nullptr;
132
+ }
133
+
134
+ if (path[idx++] != child->prefix[i]) {
135
+ return nullptr;
136
+ }
137
+ }
138
+ return child;
139
+ }
140
+
141
+ // A node can be a *end* node and have children
142
+ // E.g: */slower*, */slown* are inserted:
143
+ // /slow
144
+ // ---> er
145
+ // ---> n
146
+ // If */slow* is inserted right after, it will create an
147
+ // empty node
148
+ // /slow
149
+ // ---> '\000' ASCII (0) || \0
150
+ // ---> er
151
+ // ---> n
152
+ bool IsEndNode() const {
153
+ if (children.empty()) {
154
+ return true;
155
+ }
156
+ return is_leaf;
157
+ }
158
+ };
159
+
160
+ RadixTree();
161
+ ~RadixTree();
162
+ void Insert(const std::string& s);
163
+ void Clear();
164
+ bool Lookup(std::string_view s) const { return Lookup(s, false); }
165
+ bool Lookup(std::string_view s, bool when_empty_return) const;
166
+
167
+ private:
168
+ Node* root_node_;
169
+ };
170
+
171
+ private:
172
+ void GrantAccess(PermissionScope scope, const std::string& param);
173
+ void RevokeAccess(PermissionScope scope, const std::string& param);
174
+ void RebuildTree(PermissionScope scope);
175
+ // fs granted on startup
176
+ RadixTree granted_in_fs_;
177
+ RadixTree granted_out_fs_;
178
+
179
+ std::vector<std::string> granted_paths_in_;
180
+ std::vector<std::string> granted_paths_out_;
181
+
182
+ bool deny_all_in_ = true;
183
+ bool deny_all_out_ = true;
184
+
185
+ bool allow_all_in_ = false;
186
+ bool allow_all_out_ = false;
187
+ };
188
+
189
+ } // namespace node::permission
190
+
191
+ #endif // defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS
192
+ #endif // SRC_PERMISSION_FS_PERMISSION_H_
@@ -0,0 +1,155 @@
1
+ #ifndef SRC_PERMISSION_PERMISSION_H_
2
+ #define SRC_PERMISSION_PERMISSION_H_
3
+
4
+ #if defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS
5
+
6
+ #include "debug_utils.h"
7
+ #include "node_diagnostics_channel.h"
8
+ #include "permission/permission_base.h"
9
+
10
+ #include <array>
11
+ #include <string_view>
12
+
13
+ namespace node {
14
+
15
+ class Environment;
16
+
17
+ namespace fs {
18
+ class FSReqBase;
19
+ }
20
+
21
+ namespace permission {
22
+
23
+ #define THROW_IF_INSUFFICIENT_PERMISSIONS(env, perm, resource, ...) \
24
+ do { \
25
+ node::Environment* env__ = (env); \
26
+ const node::permission::PermissionScope perm__ = (perm); \
27
+ const auto resource__ = (resource); \
28
+ if (!env__->permission()->is_granted(env__, perm__, resource__)) \
29
+ [[unlikely]] { \
30
+ if (!env__->permission()->warning_only()) { \
31
+ node::permission::Permission::ThrowAccessDenied( \
32
+ env__, perm__, resource__); \
33
+ return __VA_ARGS__; \
34
+ } \
35
+ } \
36
+ } while (0)
37
+
38
+ #define ASYNC_THROW_IF_INSUFFICIENT_PERMISSIONS( \
39
+ env, wrap, perm, resource, ...) \
40
+ do { \
41
+ node::Environment* env__ = (env); \
42
+ const node::permission::PermissionScope perm__ = (perm); \
43
+ const auto resource__ = (resource); \
44
+ if (!env__->permission()->is_granted(env__, perm__, resource__)) \
45
+ [[unlikely]] { \
46
+ if (!env__->permission()->warning_only()) { \
47
+ node::permission::Permission::AsyncThrowAccessDenied( \
48
+ env__, (wrap), perm__, resource__); \
49
+ return __VA_ARGS__; \
50
+ } \
51
+ } \
52
+ } while (0)
53
+
54
+ #define ERR_ACCESS_DENIED_IF_INSUFFICIENT_PERMISSIONS( \
55
+ env, perm, resource, args, ...) \
56
+ do { \
57
+ node::Environment* env__ = (env); \
58
+ const node::permission::PermissionScope perm__ = (perm); \
59
+ const auto resource__ = (resource); \
60
+ if (!env__->permission()->is_granted(env__, perm__, resource__)) \
61
+ [[unlikely]] { \
62
+ if (!env__->permission()->warning_only()) { \
63
+ Local<Value> err_access; \
64
+ if (node::permission::CreateAccessDeniedError( \
65
+ env__, perm__, resource__) \
66
+ .ToLocal(&err_access)) { \
67
+ args.GetReturnValue().Set(err_access); \
68
+ } else { \
69
+ args.GetReturnValue().Set(UV_EACCES); \
70
+ } \
71
+ return __VA_ARGS__; \
72
+ } \
73
+ } \
74
+ } while (0)
75
+
76
+ #define SET_INSUFFICIENT_PERMISSION_ERROR_CALLBACK(scope) \
77
+ void InsufficientPermissionError(std::string_view resource) { \
78
+ v8::HandleScope handle_scope(env()->isolate()); \
79
+ v8::Context::Scope context_scope(env()->context()); \
80
+ v8::Local<v8::Value> arg; \
81
+ if (!permission::CreateAccessDeniedError(env(), (scope), resource) \
82
+ .ToLocal(&arg)) { \
83
+ } \
84
+ MakeCallback(env()->oncomplete_string(), 1, &arg); \
85
+ }
86
+
87
+ class Permission {
88
+ public:
89
+ Permission();
90
+
91
+ FORCE_INLINE bool is_granted(Environment* env,
92
+ PermissionScope permission,
93
+ std::string_view res = "") const {
94
+ if (!enabled_) [[likely]] {
95
+ return true;
96
+ }
97
+ return is_scope_granted(env, permission, res);
98
+ }
99
+
100
+ FORCE_INLINE bool enabled() const { return enabled_; }
101
+
102
+ FORCE_INLINE bool warning_only() const { return warning_only_; }
103
+
104
+ static PermissionScope StringToPermission(std::string_view perm);
105
+ static v8::Local<v8::String> PermissionToString(Environment* env,
106
+ PermissionScope perm);
107
+ static void ThrowAccessDenied(Environment* env,
108
+ PermissionScope perm,
109
+ std::string_view res);
110
+ static void AsyncThrowAccessDenied(Environment* env,
111
+ fs::FSReqBase* req_wrap,
112
+ PermissionScope perm,
113
+ std::string_view res);
114
+
115
+ // CLI Call
116
+ void Apply(Environment* env,
117
+ std::span<const std::string> allow,
118
+ PermissionScope scope);
119
+ // Runtime Call
120
+ void Drop(Environment* env, PermissionScope scope, std::string_view param);
121
+ void EnablePermissions();
122
+ void EnableWarningOnly();
123
+
124
+ private:
125
+ COLD_NOINLINE bool is_scope_granted(Environment* env,
126
+ PermissionScope permission,
127
+ std::string_view res = "") const;
128
+
129
+ BaseObjectPtr<diagnostics_channel::Channel> GetOrCreateChannel(
130
+ Environment* env, PermissionScope scope) const;
131
+
132
+ static constexpr size_t kPermissionCount =
133
+ static_cast<size_t>(PermissionScope::kPermissionsCount);
134
+
135
+ std::array<std::shared_ptr<PermissionBase>, kPermissionCount> nodes_;
136
+ bool enabled_;
137
+ bool warning_only_;
138
+ mutable bool publishing_ = false;
139
+ // Weak refs: BindingData (via BaseObjectPtr) is the sole owner of Channels.
140
+ // Using weak refs here avoids keeping Channels alive past Realm teardown.
141
+ mutable std::array<BaseObjectWeakPtr<diagnostics_channel::Channel>,
142
+ kPermissionCount>
143
+ channels_;
144
+ };
145
+
146
+ v8::MaybeLocal<v8::Value> CreateAccessDeniedError(Environment* env,
147
+ PermissionScope perm,
148
+ std::string_view res);
149
+
150
+ } // namespace permission
151
+
152
+ } // namespace node
153
+
154
+ #endif // defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS
155
+ #endif // SRC_PERMISSION_PERMISSION_H_
@@ -0,0 +1,79 @@
1
+ #ifndef SRC_PERMISSION_PERMISSION_BASE_H_
2
+ #define SRC_PERMISSION_PERMISSION_BASE_H_
3
+
4
+ #if defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS
5
+
6
+ #include <span>
7
+ #include <string>
8
+ #include <string_view>
9
+
10
+ namespace node {
11
+
12
+ class Environment;
13
+
14
+ namespace permission {
15
+
16
+ #define FILESYSTEM_PERMISSIONS(V) \
17
+ V(FileSystem, "fs", PermissionsRoot, "") \
18
+ V(FileSystemRead, "fs.read", FileSystem, "--allow-fs-read") \
19
+ V(FileSystemWrite, "fs.write", FileSystem, "--allow-fs-write")
20
+
21
+ #define CHILD_PROCESS_PERMISSIONS(V) \
22
+ V(ChildProcess, "child", PermissionsRoot, "--allow-child-process")
23
+
24
+ #define WASI_PERMISSIONS(V) V(WASI, "wasi", PermissionsRoot, "--allow-wasi")
25
+
26
+ #define WORKER_THREADS_PERMISSIONS(V) \
27
+ V(WorkerThreads, "worker", PermissionsRoot, "--allow-worker")
28
+
29
+ #define INSPECTOR_PERMISSIONS(V) \
30
+ V(Inspector, "inspector", PermissionsRoot, "--allow-inspector")
31
+
32
+ #define NET_PERMISSIONS(V) V(Net, "net", PermissionsRoot, "--allow-net")
33
+
34
+ #define ADDON_PERMISSIONS(V) \
35
+ V(Addon, "addon", PermissionsRoot, "--allow-addons")
36
+
37
+ #define FFI_PERMISSIONS(V) V(FFI, "ffi", PermissionsRoot, "--allow-ffi")
38
+
39
+ #define OPENSSL_STORE_PERMISSIONS(V) \
40
+ V(OpenSSLStore, "openssl.store", PermissionsRoot, "--allow-openssl-store")
41
+
42
+ #define PERMISSIONS(V) \
43
+ FILESYSTEM_PERMISSIONS(V) \
44
+ CHILD_PROCESS_PERMISSIONS(V) \
45
+ WASI_PERMISSIONS(V) \
46
+ WORKER_THREADS_PERMISSIONS(V) \
47
+ INSPECTOR_PERMISSIONS(V) \
48
+ NET_PERMISSIONS(V) \
49
+ ADDON_PERMISSIONS(V) \
50
+ FFI_PERMISSIONS(V) \
51
+ OPENSSL_STORE_PERMISSIONS(V)
52
+
53
+ #define V(name, _, __, ___) k##name,
54
+ enum class PermissionScope {
55
+ kPermissionsRoot = -1,
56
+ PERMISSIONS(V) kPermissionsCount
57
+ };
58
+ #undef V
59
+
60
+ class PermissionBase {
61
+ public:
62
+ virtual ~PermissionBase() = default;
63
+ virtual void Apply(Environment* env,
64
+ std::span<const std::string> allow,
65
+ PermissionScope scope) = 0;
66
+ virtual void Drop(Environment* env,
67
+ PermissionScope scope,
68
+ std::string_view param) = 0;
69
+ virtual bool is_granted(Environment* env,
70
+ PermissionScope perm,
71
+ std::string_view param) const = 0;
72
+ };
73
+
74
+ } // namespace permission
75
+
76
+ } // namespace node
77
+
78
+ #endif // defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS
79
+ #endif // SRC_PERMISSION_PERMISSION_BASE_H_