@encharm/cws 4.3.0 → 4.4.1

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 (238) hide show
  1. package/README.md +5 -7
  2. package/dist/bindings/cws_darwin_arm64_node108.node +0 -0
  3. package/dist/bindings/cws_darwin_arm64_node115.node +0 -0
  4. package/dist/bindings/cws_darwin_arm64_node83.node +0 -0
  5. package/dist/bindings/cws_darwin_arm64_node93.node +0 -0
  6. package/dist/bindings/cws_darwin_x64_node108.node +0 -0
  7. package/dist/bindings/cws_darwin_x64_node115.node +0 -0
  8. package/dist/bindings/cws_darwin_x64_node83.node +0 -0
  9. package/dist/bindings/cws_darwin_x64_node93.node +0 -0
  10. package/dist/bindings/cws_linux_arm64_node108.node +0 -0
  11. package/dist/bindings/cws_linux_arm64_node115.node +0 -0
  12. package/dist/bindings/cws_linux_arm64_node83.node +0 -0
  13. package/dist/bindings/cws_linux_arm64_node93.node +0 -0
  14. package/dist/bindings/cws_linux_x64_node108.node +0 -0
  15. package/dist/bindings/cws_linux_x64_node115.node +0 -0
  16. package/dist/bindings/cws_linux_x64_node83.node +0 -0
  17. package/dist/bindings/cws_linux_x64_node93.node +0 -0
  18. package/dist/bindings/cws_win32_x64_node108.node +0 -0
  19. package/dist/bindings/cws_win32_x64_node115.node +0 -0
  20. package/dist/bindings/cws_win32_x64_node83.node +0 -0
  21. package/dist/bindings/cws_win32_x64_node93.node +0 -0
  22. package/dist/shared.js +1 -1
  23. package/package.json +5 -5
  24. package/src/Addon.cpp +4 -12
  25. package/src/Addon.h +67 -91
  26. package/src/Socket.h +1 -0
  27. package/src/cSNode.cpp +3 -8
  28. package/src/headers/20/acorn_version.h +6 -0
  29. package/src/headers/20/aliased_buffer-inl.h +243 -0
  30. package/src/headers/20/aliased_buffer.h +206 -0
  31. package/src/headers/20/aliased_struct-inl.h +54 -0
  32. package/src/headers/20/aliased_struct.h +63 -0
  33. package/src/headers/20/async_wrap-inl.h +93 -0
  34. package/src/headers/20/async_wrap.h +252 -0
  35. package/src/headers/20/base64-inl.h +193 -0
  36. package/src/headers/20/base64.h +53 -0
  37. package/src/headers/20/base64_version.h +6 -0
  38. package/src/headers/20/base_object-inl.h +316 -0
  39. package/src/headers/20/base_object.h +320 -0
  40. package/src/headers/20/base_object_types.h +73 -0
  41. package/src/headers/20/blob_serializer_deserializer-inl.h +361 -0
  42. package/src/headers/20/blob_serializer_deserializer.h +132 -0
  43. package/src/headers/20/callback_queue-inl.h +97 -0
  44. package/src/headers/20/callback_queue.h +79 -0
  45. package/src/headers/20/cares_wrap.h +524 -0
  46. package/src/headers/20/cjs_module_lexer_version.h +6 -0
  47. package/src/headers/20/cleanup_queue-inl.h +62 -0
  48. package/src/headers/20/cleanup_queue.h +85 -0
  49. package/src/headers/20/connect_wrap.h +26 -0
  50. package/src/headers/20/connection_wrap.h +30 -0
  51. package/src/headers/20/cppgc/DEPS +8 -0
  52. package/src/headers/20/cppgc/OWNERS +2 -0
  53. package/src/headers/20/cppgc/README.md +135 -0
  54. package/src/headers/20/cppgc/allocation.h +310 -0
  55. package/src/headers/20/cppgc/common.h +28 -0
  56. package/src/headers/20/cppgc/cross-thread-persistent.h +466 -0
  57. package/src/headers/20/cppgc/custom-space.h +97 -0
  58. package/src/headers/20/cppgc/default-platform.h +67 -0
  59. package/src/headers/20/cppgc/ephemeron-pair.h +30 -0
  60. package/src/headers/20/cppgc/explicit-management.h +100 -0
  61. package/src/headers/20/cppgc/garbage-collected.h +106 -0
  62. package/src/headers/20/cppgc/heap-consistency.h +309 -0
  63. package/src/headers/20/cppgc/heap-handle.h +48 -0
  64. package/src/headers/20/cppgc/heap-state.h +82 -0
  65. package/src/headers/20/cppgc/heap-statistics.h +120 -0
  66. package/src/headers/20/cppgc/heap.h +202 -0
  67. package/src/headers/20/cppgc/internal/api-constants.h +68 -0
  68. package/src/headers/20/cppgc/internal/atomic-entry-flag.h +48 -0
  69. package/src/headers/20/cppgc/internal/base-page-handle.h +45 -0
  70. package/src/headers/20/cppgc/internal/caged-heap-local-data.h +111 -0
  71. package/src/headers/20/cppgc/internal/caged-heap.h +61 -0
  72. package/src/headers/20/cppgc/internal/compiler-specific.h +38 -0
  73. package/src/headers/20/cppgc/internal/finalizer-trait.h +93 -0
  74. package/src/headers/20/cppgc/internal/gc-info.h +157 -0
  75. package/src/headers/20/cppgc/internal/logging.h +50 -0
  76. package/src/headers/20/cppgc/internal/member-storage.h +248 -0
  77. package/src/headers/20/cppgc/internal/name-trait.h +137 -0
  78. package/src/headers/20/cppgc/internal/persistent-node.h +214 -0
  79. package/src/headers/20/cppgc/internal/pointer-policies.h +243 -0
  80. package/src/headers/20/cppgc/internal/write-barrier.h +487 -0
  81. package/src/headers/20/cppgc/liveness-broker.h +78 -0
  82. package/src/headers/20/cppgc/macros.h +35 -0
  83. package/src/headers/20/cppgc/member.h +604 -0
  84. package/src/headers/20/cppgc/name-provider.h +65 -0
  85. package/src/headers/20/cppgc/object-size-trait.h +58 -0
  86. package/src/headers/20/cppgc/persistent.h +373 -0
  87. package/src/headers/20/cppgc/platform.h +158 -0
  88. package/src/headers/20/cppgc/prefinalizer.h +75 -0
  89. package/src/headers/20/cppgc/process-heap-statistics.h +36 -0
  90. package/src/headers/20/cppgc/sentinel-pointer.h +32 -0
  91. package/src/headers/20/cppgc/source-location.h +92 -0
  92. package/src/headers/20/cppgc/testing.h +106 -0
  93. package/src/headers/20/cppgc/trace-trait.h +120 -0
  94. package/src/headers/20/cppgc/type-traits.h +250 -0
  95. package/src/headers/20/cppgc/visitor.h +426 -0
  96. package/src/headers/20/crypto/crypto_clienthello.h +131 -0
  97. package/src/headers/20/crypto/crypto_context.h +160 -0
  98. package/src/headers/20/crypto/crypto_keys.h +419 -0
  99. package/src/headers/20/crypto/crypto_tls.h +304 -0
  100. package/src/headers/20/crypto/crypto_util.h +806 -0
  101. package/src/headers/20/crypto_clienthello.h +131 -0
  102. package/src/headers/20/debug_utils-inl.h +226 -0
  103. package/src/headers/20/debug_utils.h +188 -0
  104. package/src/headers/20/diagnosticfilename-inl.h +33 -0
  105. package/src/headers/20/encoding_binding.h +59 -0
  106. package/src/headers/20/env-inl.h +923 -0
  107. package/src/headers/20/env.h +1193 -0
  108. package/src/headers/20/env_properties.h +446 -0
  109. package/src/headers/20/handle_wrap.h +123 -0
  110. package/src/headers/20/histogram-inl.h +107 -0
  111. package/src/headers/20/histogram.h +240 -0
  112. package/src/headers/20/inspector_agent.h +144 -0
  113. package/src/headers/20/inspector_io.h +78 -0
  114. package/src/headers/20/inspector_profiler.h +149 -0
  115. package/src/headers/20/inspector_socket.h +60 -0
  116. package/src/headers/20/inspector_socket_server.h +110 -0
  117. package/src/headers/20/js_native_api.h +601 -0
  118. package/src/headers/20/js_native_api_types.h +168 -0
  119. package/src/headers/20/js_native_api_v8.h +463 -0
  120. package/src/headers/20/js_native_api_v8_internals.h +45 -0
  121. package/src/headers/20/js_stream.h +52 -0
  122. package/src/headers/20/json_parser.h +38 -0
  123. package/src/headers/20/json_utils.h +172 -0
  124. package/src/headers/20/memory_tracker-inl.h +356 -0
  125. package/src/headers/20/memory_tracker.h +289 -0
  126. package/src/headers/20/module_wrap.h +117 -0
  127. package/src/headers/20/node.h +1516 -0
  128. package/src/headers/20/node_api.h +261 -0
  129. package/src/headers/20/node_api_internals.h +42 -0
  130. package/src/headers/20/node_api_types.h +52 -0
  131. package/src/headers/20/node_binding.h +142 -0
  132. package/src/headers/20/node_blob.h +159 -0
  133. package/src/headers/20/node_bob-inl.h +36 -0
  134. package/src/headers/20/node_bob.h +107 -0
  135. package/src/headers/20/node_buffer.h +92 -0
  136. package/src/headers/20/node_builtins.h +197 -0
  137. package/src/headers/20/node_constants.h +82 -0
  138. package/src/headers/20/node_context_data.h +98 -0
  139. package/src/headers/20/node_contextify.h +206 -0
  140. package/src/headers/20/node_crypto.h +56 -0
  141. package/src/headers/20/node_dir.h +52 -0
  142. package/src/headers/20/node_dotenv.h +37 -0
  143. package/src/headers/20/node_errors.h +313 -0
  144. package/src/headers/20/node_exit_code.h +54 -0
  145. package/src/headers/20/node_external_reference.h +172 -0
  146. package/src/headers/20/node_file-inl.h +389 -0
  147. package/src/headers/20/node_file.h +537 -0
  148. package/src/headers/20/node_http2.h +1146 -0
  149. package/src/headers/20/node_http2_state.h +147 -0
  150. package/src/headers/20/node_http_common-inl.h +201 -0
  151. package/src/headers/20/node_http_common.h +532 -0
  152. package/src/headers/20/node_i18n.h +145 -0
  153. package/src/headers/20/node_internals.h +443 -0
  154. package/src/headers/20/node_main_instance.h +67 -0
  155. package/src/headers/20/node_mem-inl.h +112 -0
  156. package/src/headers/20/node_mem.h +45 -0
  157. package/src/headers/20/node_messaging.h +377 -0
  158. package/src/headers/20/node_metadata.h +138 -0
  159. package/src/headers/20/node_mutex.h +323 -0
  160. package/src/headers/20/node_object_wrap.h +132 -0
  161. package/src/headers/20/node_options-inl.h +478 -0
  162. package/src/headers/20/node_options.h +538 -0
  163. package/src/headers/20/node_perf.h +166 -0
  164. package/src/headers/20/node_perf_common.h +98 -0
  165. package/src/headers/20/node_platform.h +195 -0
  166. package/src/headers/20/node_process-inl.h +26 -0
  167. package/src/headers/20/node_process.h +99 -0
  168. package/src/headers/20/node_realm-inl.h +139 -0
  169. package/src/headers/20/node_realm.h +184 -0
  170. package/src/headers/20/node_report.h +42 -0
  171. package/src/headers/20/node_revert.h +82 -0
  172. package/src/headers/20/node_root_certs.h +3443 -0
  173. package/src/headers/20/node_sea.h +56 -0
  174. package/src/headers/20/node_shadow_realm.h +46 -0
  175. package/src/headers/20/node_snapshot_builder.h +54 -0
  176. package/src/headers/20/node_snapshotable.h +164 -0
  177. package/src/headers/20/node_sockaddr-inl.h +266 -0
  178. package/src/headers/20/node_sockaddr.h +405 -0
  179. package/src/headers/20/node_stat_watcher.h +73 -0
  180. package/src/headers/20/node_threadsafe_cow-inl.h +54 -0
  181. package/src/headers/20/node_threadsafe_cow.h +105 -0
  182. package/src/headers/20/node_union_bytes.h +81 -0
  183. package/src/headers/20/node_url.h +92 -0
  184. package/src/headers/20/node_v8.h +77 -0
  185. package/src/headers/20/node_v8_platform-inl.h +205 -0
  186. package/src/headers/20/node_version.h +109 -0
  187. package/src/headers/20/node_wasi.h +184 -0
  188. package/src/headers/20/node_wasm_web_api.h +55 -0
  189. package/src/headers/20/node_watchdog.h +154 -0
  190. package/src/headers/20/node_worker.h +151 -0
  191. package/src/headers/20/permission/child_process_permission.h +29 -0
  192. package/src/headers/20/permission/fs_permission.h +154 -0
  193. package/src/headers/20/permission/inspector_permission.h +29 -0
  194. package/src/headers/20/permission/permission.h +74 -0
  195. package/src/headers/20/permission/permission_base.h +53 -0
  196. package/src/headers/20/permission/worker_permission.h +29 -0
  197. package/src/headers/20/pipe_wrap.h +80 -0
  198. package/src/headers/20/req_wrap-inl.h +171 -0
  199. package/src/headers/20/req_wrap.h +77 -0
  200. package/src/headers/20/spawn_sync.h +242 -0
  201. package/src/headers/20/stream_base-inl.h +173 -0
  202. package/src/headers/20/stream_base.h +469 -0
  203. package/src/headers/20/stream_pipe.h +76 -0
  204. package/src/headers/20/stream_wrap.h +131 -0
  205. package/src/headers/20/string_bytes.h +120 -0
  206. package/src/headers/20/string_decoder-inl.h +37 -0
  207. package/src/headers/20/string_decoder.h +50 -0
  208. package/src/headers/20/string_search.h +638 -0
  209. package/src/headers/20/tcp_wrap.h +105 -0
  210. package/src/headers/20/threadpoolwork-inl.h +70 -0
  211. package/src/headers/20/timer_wrap-inl.h +32 -0
  212. package/src/headers/20/timer_wrap.h +85 -0
  213. package/src/headers/20/timers.h +70 -0
  214. package/src/headers/20/tracing/agent.h +195 -0
  215. package/src/headers/20/tracing/node_trace_buffer.h +83 -0
  216. package/src/headers/20/tracing/node_trace_writer.h +75 -0
  217. package/src/headers/20/tracing/trace_event.h +720 -0
  218. package/src/headers/20/tracing/trace_event_common.h +1109 -0
  219. package/src/headers/20/tracing/traced_value.h +70 -0
  220. package/src/headers/20/tty_wrap.h +65 -0
  221. package/src/headers/20/udp_wrap.h +227 -0
  222. package/src/headers/20/undici_version.h +6 -0
  223. package/src/headers/20/util-inl.h +623 -0
  224. package/src/headers/20/util.h +988 -0
  225. package/src/headers/20/v8-cppgc.h +245 -0
  226. package/src/headers/20/v8-fast-api-calls.h +957 -0
  227. package/dist/bindings/cws_darwin_arm64_node64.node +0 -0
  228. package/dist/bindings/cws_darwin_arm64_node72.node +0 -0
  229. package/dist/bindings/cws_darwin_arm64_node79.node +0 -0
  230. package/dist/bindings/cws_darwin_x64_node64.node +0 -0
  231. package/dist/bindings/cws_darwin_x64_node72.node +0 -0
  232. package/dist/bindings/cws_darwin_x64_node79.node +0 -0
  233. package/dist/bindings/cws_linux_x64_node64.node +0 -0
  234. package/dist/bindings/cws_linux_x64_node72.node +0 -0
  235. package/dist/bindings/cws_linux_x64_node79.node +0 -0
  236. package/dist/bindings/cws_win32_x64_node64.node +0 -0
  237. package/dist/bindings/cws_win32_x64_node72.node +0 -0
  238. package/dist/bindings/cws_win32_x64_node79.node +0 -0
@@ -0,0 +1,56 @@
1
+ #ifndef SRC_NODE_SEA_H_
2
+ #define SRC_NODE_SEA_H_
3
+
4
+ #if defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS
5
+
6
+ #if !defined(DISABLE_SINGLE_EXECUTABLE_APPLICATION)
7
+
8
+ #include <cinttypes>
9
+ #include <optional>
10
+ #include <string>
11
+ #include <string_view>
12
+ #include <tuple>
13
+ #include <vector>
14
+
15
+ #include "node_exit_code.h"
16
+
17
+ namespace node {
18
+ namespace sea {
19
+ // A special number that will appear at the beginning of the single executable
20
+ // preparation blobs ready to be injected into the binary. We use this to check
21
+ // that the data given to us are intended for building single executable
22
+ // applications.
23
+ const uint32_t kMagic = 0x143da20;
24
+
25
+ enum class SeaFlags : uint32_t {
26
+ kDefault = 0,
27
+ kDisableExperimentalSeaWarning = 1 << 0,
28
+ kUseSnapshot = 1 << 1,
29
+ kUseCodeCache = 1 << 2,
30
+ };
31
+
32
+ struct SeaResource {
33
+ SeaFlags flags = SeaFlags::kDefault;
34
+ std::string_view code_path;
35
+ std::string_view main_code_or_snapshot;
36
+ std::optional<std::string_view> code_cache;
37
+
38
+ bool use_snapshot() const;
39
+ static constexpr size_t kHeaderSize = sizeof(kMagic) + sizeof(SeaFlags);
40
+ };
41
+
42
+ bool IsSingleExecutable();
43
+ SeaResource FindSingleExecutableResource();
44
+ std::tuple<int, char**> FixupArgsForSEA(int argc, char** argv);
45
+ node::ExitCode BuildSingleExecutableBlob(
46
+ const std::string& config_path,
47
+ const std::vector<std::string>& args,
48
+ const std::vector<std::string>& exec_args);
49
+ } // namespace sea
50
+ } // namespace node
51
+
52
+ #endif // !defined(DISABLE_SINGLE_EXECUTABLE_APPLICATION)
53
+
54
+ #endif // defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS
55
+
56
+ #endif // SRC_NODE_SEA_H_
@@ -0,0 +1,46 @@
1
+ #ifndef SRC_NODE_SHADOW_REALM_H_
2
+ #define SRC_NODE_SHADOW_REALM_H_
3
+
4
+ #if defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS
5
+
6
+ #include "node_realm.h"
7
+ #include "v8.h"
8
+
9
+ namespace node {
10
+ namespace shadow_realm {
11
+
12
+ class ShadowRealm : public Realm {
13
+ public:
14
+ static ShadowRealm* New(Environment* env);
15
+
16
+ SET_MEMORY_INFO_NAME(ShadowRealm)
17
+ SET_SELF_SIZE(ShadowRealm)
18
+
19
+ v8::Local<v8::Context> context() const override;
20
+
21
+ #define V(PropertyName, TypeName) \
22
+ v8::Local<TypeName> PropertyName() const override; \
23
+ void set_##PropertyName(v8::Local<TypeName> value) override;
24
+ PER_REALM_STRONG_PERSISTENT_VALUES(V)
25
+ #undef V
26
+
27
+ protected:
28
+ v8::MaybeLocal<v8::Value> BootstrapRealm() override;
29
+
30
+ private:
31
+ static void WeakCallback(const v8::WeakCallbackInfo<ShadowRealm>& data);
32
+ static void DeleteMe(void* data);
33
+
34
+ explicit ShadowRealm(Environment* env);
35
+ ~ShadowRealm();
36
+ };
37
+
38
+ v8::MaybeLocal<v8::Context> HostCreateShadowRealmContextCallback(
39
+ v8::Local<v8::Context> initiator_context);
40
+
41
+ } // namespace shadow_realm
42
+ } // namespace node
43
+
44
+ #endif // defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS
45
+
46
+ #endif // SRC_NODE_SHADOW_REALM_H_
@@ -0,0 +1,54 @@
1
+
2
+ #ifndef SRC_NODE_SNAPSHOT_BUILDER_H_
3
+ #define SRC_NODE_SNAPSHOT_BUILDER_H_
4
+
5
+ #if defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS
6
+
7
+ #include <cstdint>
8
+ #include <optional>
9
+ #include <string_view>
10
+ #include "node_exit_code.h"
11
+ #include "node_mutex.h"
12
+ #include "v8.h"
13
+
14
+ namespace node {
15
+
16
+ class ExternalReferenceRegistry;
17
+ struct SnapshotData;
18
+
19
+ class NODE_EXTERN_PRIVATE SnapshotBuilder {
20
+ public:
21
+ static ExitCode GenerateAsSource(
22
+ const char* out_path,
23
+ const std::vector<std::string>& args,
24
+ const std::vector<std::string>& exec_args,
25
+ std::optional<std::string_view> main_script_path = std::nullopt,
26
+ bool use_array_literals = false);
27
+
28
+ // Generate the snapshot into out.
29
+ static ExitCode Generate(SnapshotData* out,
30
+ const std::vector<std::string>& args,
31
+ const std::vector<std::string>& exec_args,
32
+ std::optional<std::string_view> main_script);
33
+
34
+ // If nullptr is returned, the binary is not built with embedded
35
+ // snapshot.
36
+ static const SnapshotData* GetEmbeddedSnapshotData();
37
+ static void InitializeIsolateParams(const SnapshotData* data,
38
+ v8::Isolate::CreateParams* params);
39
+
40
+ static const std::vector<intptr_t>& CollectExternalReferences();
41
+
42
+ static ExitCode CreateSnapshot(
43
+ SnapshotData* out,
44
+ CommonEnvironmentSetup* setup,
45
+ /*SnapshotMetadata::Type*/ uint8_t snapshot_type);
46
+
47
+ private:
48
+ static std::unique_ptr<ExternalReferenceRegistry> registry_;
49
+ };
50
+ } // namespace node
51
+
52
+ #endif // defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS
53
+
54
+ #endif // SRC_NODE_SNAPSHOT_BUILDER_H_
@@ -0,0 +1,164 @@
1
+
2
+ #ifndef SRC_NODE_SNAPSHOTABLE_H_
3
+ #define SRC_NODE_SNAPSHOTABLE_H_
4
+
5
+ #if defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS
6
+
7
+ #include "aliased_buffer.h"
8
+ #include "base_object.h"
9
+ #include "util.h"
10
+
11
+ namespace node {
12
+
13
+ class Environment;
14
+ struct RealmSerializeInfo;
15
+ struct SnapshotData;
16
+ class ExternalReferenceRegistry;
17
+
18
+ using SnapshotIndex = size_t;
19
+
20
+ struct PropInfo {
21
+ std::string name; // name for debugging
22
+ uint32_t id; // In the list - in case there are any empty entries
23
+ SnapshotIndex index; // In the snapshot
24
+ };
25
+
26
+ typedef size_t SnapshotIndex;
27
+
28
+ // When serializing an embedder object, we'll serialize the native states
29
+ // into a chunk that can be mapped into a subclass of InternalFieldInfoBase,
30
+ // and pass it into the V8 callback as the payload of StartupData.
31
+ // The memory chunk looks like this:
32
+ //
33
+ // [ type ] - EmbedderObjectType (a uint8_t)
34
+ // [ length ] - a size_t
35
+ // [ ... ] - custom bytes of size |length - header size|
36
+ struct InternalFieldInfoBase {
37
+ public:
38
+ EmbedderObjectType type;
39
+ size_t length;
40
+
41
+ template <typename T>
42
+ static T* New(EmbedderObjectType type) {
43
+ static_assert(std::is_base_of_v<InternalFieldInfoBase, T> ||
44
+ std::is_same_v<InternalFieldInfoBase, T>,
45
+ "Can only accept InternalFieldInfoBase subclasses");
46
+ void* buf = ::operator new[](sizeof(T));
47
+ T* result = new (buf) T;
48
+ result->type = type;
49
+ result->length = sizeof(T);
50
+ return result;
51
+ }
52
+
53
+ template <typename T>
54
+ T* Copy() const {
55
+ static_assert(std::is_base_of_v<InternalFieldInfoBase, T> ||
56
+ std::is_same_v<InternalFieldInfoBase, T>,
57
+ "Can only accept InternalFieldInfoBase subclasses");
58
+ static_assert(std::is_trivially_copyable_v<T>,
59
+ "Can only memcpy trivially copyable class");
60
+ void* buf = ::operator new[](sizeof(T));
61
+ T* result = new (buf) T;
62
+ memcpy(result, this, sizeof(T));
63
+ return result;
64
+ }
65
+
66
+ void Delete() { ::operator delete[](this); }
67
+
68
+ InternalFieldInfoBase() = default;
69
+ };
70
+
71
+ struct EmbedderTypeInfo {
72
+ enum class MemoryMode : uint8_t { kBaseObject, kCppGC };
73
+ EmbedderTypeInfo(EmbedderObjectType t, MemoryMode m) : type(t), mode(m) {}
74
+ EmbedderTypeInfo() = default;
75
+ EmbedderObjectType type;
76
+ MemoryMode mode;
77
+ };
78
+
79
+ // An interface for snapshotable native objects to inherit from.
80
+ // Use the SERIALIZABLE_OBJECT_METHODS() macro in the class to define
81
+ // the following methods to implement:
82
+ //
83
+ // - PrepareForSerialization(): This would be run prior to context
84
+ // serialization. Use this method to e.g. release references that
85
+ // can be re-initialized, or perform property store operations
86
+ // that needs a V8 context.
87
+ // - Serialize(): This would be called during context serialization,
88
+ // once for each embedder field of the object.
89
+ // Allocate and construct an InternalFieldInfoBase object that contains
90
+ // data that can be used to deserialize native states.
91
+ // - Deserialize(): This would be called after the context is
92
+ // deserialized and the object graph is complete, once for each
93
+ // embedder field of the object. Use this to restore native states
94
+ // in the object.
95
+ class SnapshotableObject : public BaseObject {
96
+ public:
97
+ SnapshotableObject(Realm* realm,
98
+ v8::Local<v8::Object> wrap,
99
+ EmbedderObjectType type);
100
+ std::string GetTypeName() const;
101
+
102
+ // If returns false, the object will not be serialized.
103
+ virtual bool PrepareForSerialization(v8::Local<v8::Context> context,
104
+ v8::SnapshotCreator* creator) = 0;
105
+ virtual InternalFieldInfoBase* Serialize(int index) = 0;
106
+ bool is_snapshotable() const override { return true; }
107
+ // We'll make sure that the type is set in the constructor
108
+ EmbedderObjectType type() { return type_; }
109
+
110
+ private:
111
+ EmbedderObjectType type_;
112
+ };
113
+
114
+ #define SERIALIZABLE_OBJECT_METHODS() \
115
+ bool PrepareForSerialization(v8::Local<v8::Context> context, \
116
+ v8::SnapshotCreator* creator) override; \
117
+ InternalFieldInfoBase* Serialize(int index) override; \
118
+ static void Deserialize(v8::Local<v8::Context> context, \
119
+ v8::Local<v8::Object> holder, \
120
+ int index, \
121
+ InternalFieldInfoBase* info);
122
+
123
+ v8::StartupData SerializeNodeContextInternalFields(v8::Local<v8::Object> holder,
124
+ int index,
125
+ void* env);
126
+ void DeserializeNodeInternalFields(v8::Local<v8::Object> holder,
127
+ int index,
128
+ v8::StartupData payload,
129
+ void* env);
130
+ void SerializeSnapshotableObjects(Realm* realm,
131
+ v8::SnapshotCreator* creator,
132
+ RealmSerializeInfo* info);
133
+
134
+ #define DCHECK_IS_SNAPSHOT_SLOT(index) DCHECK_EQ(index, BaseObject::kSlot)
135
+
136
+ namespace mksnapshot {
137
+ class BindingData : public SnapshotableObject {
138
+ public:
139
+ struct InternalFieldInfo : public node::InternalFieldInfoBase {
140
+ AliasedBufferIndex is_building_snapshot_buffer;
141
+ };
142
+
143
+ BindingData(Realm* realm,
144
+ v8::Local<v8::Object> obj,
145
+ InternalFieldInfo* info = nullptr);
146
+ SET_BINDING_ID(mksnapshot_binding_data)
147
+ SERIALIZABLE_OBJECT_METHODS()
148
+
149
+ void MemoryInfo(MemoryTracker* tracker) const override;
150
+ SET_SELF_SIZE(BindingData)
151
+ SET_MEMORY_INFO_NAME(BindingData)
152
+
153
+ private:
154
+ AliasedUint8Array is_building_snapshot_buffer_;
155
+ InternalFieldInfo* internal_field_info_ = nullptr;
156
+ };
157
+
158
+ } // namespace mksnapshot
159
+
160
+ } // namespace node
161
+
162
+ #endif // defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS
163
+
164
+ #endif // SRC_NODE_SNAPSHOTABLE_H_
@@ -0,0 +1,266 @@
1
+ #ifndef SRC_NODE_SOCKADDR_INL_H_
2
+ #define SRC_NODE_SOCKADDR_INL_H_
3
+
4
+ #if defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS
5
+
6
+ #include "node.h"
7
+ #include "env-inl.h"
8
+ #include "node_internals.h"
9
+ #include "node_sockaddr.h"
10
+ #include "util-inl.h"
11
+ #include "memory_tracker-inl.h"
12
+
13
+ #include <string>
14
+
15
+ namespace node {
16
+
17
+ static constexpr uint32_t kLabelMask = 0xFFFFF;
18
+
19
+ inline void hash_combine(size_t* seed) { }
20
+
21
+ template <typename T, typename... Args>
22
+ inline void hash_combine(size_t* seed, const T& value, Args... rest) {
23
+ *seed ^= std::hash<T>{}(value) + 0x9e3779b9 + (*seed << 6) + (*seed >> 2);
24
+ hash_combine(seed, rest...);
25
+ }
26
+
27
+ bool SocketAddress::is_numeric_host(const char* hostname) {
28
+ return is_numeric_host(hostname, AF_INET) ||
29
+ is_numeric_host(hostname, AF_INET6);
30
+ }
31
+
32
+ bool SocketAddress::is_numeric_host(const char* hostname, int family) {
33
+ in6_addr dst;
34
+ return inet_pton(family, hostname, &dst) == 1;
35
+ }
36
+
37
+ int SocketAddress::GetPort(const sockaddr* addr) {
38
+ CHECK(addr->sa_family == AF_INET || addr->sa_family == AF_INET6);
39
+ return ntohs(addr->sa_family == AF_INET ?
40
+ reinterpret_cast<const sockaddr_in*>(addr)->sin_port :
41
+ reinterpret_cast<const sockaddr_in6*>(addr)->sin6_port);
42
+ }
43
+
44
+ int SocketAddress::GetPort(const sockaddr_storage* addr) {
45
+ return GetPort(reinterpret_cast<const sockaddr*>(addr));
46
+ }
47
+
48
+ std::string SocketAddress::GetAddress(const sockaddr* addr) {
49
+ CHECK(addr->sa_family == AF_INET || addr->sa_family == AF_INET6);
50
+ char host[INET6_ADDRSTRLEN];
51
+ const void* src = addr->sa_family == AF_INET ?
52
+ static_cast<const void*>(
53
+ &(reinterpret_cast<const sockaddr_in*>(addr)->sin_addr)) :
54
+ static_cast<const void*>(
55
+ &(reinterpret_cast<const sockaddr_in6*>(addr)->sin6_addr));
56
+ uv_inet_ntop(addr->sa_family, src, host, INET6_ADDRSTRLEN);
57
+ return std::string(host);
58
+ }
59
+
60
+ std::string SocketAddress::GetAddress(const sockaddr_storage* addr) {
61
+ return GetAddress(reinterpret_cast<const sockaddr*>(addr));
62
+ }
63
+
64
+ size_t SocketAddress::GetLength(const sockaddr* addr) {
65
+ return addr->sa_family == AF_INET ?
66
+ sizeof(sockaddr_in) : sizeof(sockaddr_in6);
67
+ }
68
+
69
+ size_t SocketAddress::GetLength(const sockaddr_storage* addr) {
70
+ return GetLength(reinterpret_cast<const sockaddr*>(addr));
71
+ }
72
+
73
+ SocketAddress::SocketAddress(const sockaddr* addr) {
74
+ memcpy(&address_, addr, GetLength(addr));
75
+ }
76
+
77
+ SocketAddress::SocketAddress(const SocketAddress& addr) {
78
+ memcpy(&address_, &addr.address_, addr.length());
79
+ }
80
+
81
+ SocketAddress& SocketAddress::operator=(const sockaddr* addr) {
82
+ memcpy(&address_, addr, GetLength(addr));
83
+ return *this;
84
+ }
85
+
86
+ SocketAddress& SocketAddress::operator=(const SocketAddress& addr) {
87
+ memcpy(&address_, &addr.address_, addr.length());
88
+ return *this;
89
+ }
90
+
91
+ const sockaddr& SocketAddress::operator*() const {
92
+ return *data();
93
+ }
94
+
95
+ const sockaddr* SocketAddress::operator->() const {
96
+ return data();
97
+ }
98
+
99
+ size_t SocketAddress::length() const {
100
+ return GetLength(&address_);
101
+ }
102
+
103
+ const sockaddr* SocketAddress::data() const {
104
+ return reinterpret_cast<const sockaddr*>(&address_);
105
+ }
106
+
107
+ const uint8_t* SocketAddress::raw() const {
108
+ return reinterpret_cast<const uint8_t*>(&address_);
109
+ }
110
+
111
+ sockaddr* SocketAddress::storage() {
112
+ return reinterpret_cast<sockaddr*>(&address_);
113
+ }
114
+
115
+ int SocketAddress::family() const {
116
+ return address_.ss_family;
117
+ }
118
+
119
+ std::string SocketAddress::address() const {
120
+ return GetAddress(&address_);
121
+ }
122
+
123
+ int SocketAddress::port() const {
124
+ return GetPort(&address_);
125
+ }
126
+
127
+ uint32_t SocketAddress::flow_label() const {
128
+ if (family() != AF_INET6)
129
+ return 0;
130
+ const sockaddr_in6* in = reinterpret_cast<const sockaddr_in6*>(data());
131
+ return in->sin6_flowinfo;
132
+ }
133
+
134
+ void SocketAddress::set_flow_label(uint32_t label) {
135
+ if (family() != AF_INET6)
136
+ return;
137
+ CHECK_LE(label, kLabelMask);
138
+ sockaddr_in6* in = reinterpret_cast<sockaddr_in6*>(&address_);
139
+ in->sin6_flowinfo = label;
140
+ }
141
+
142
+ std::string SocketAddress::ToString() const {
143
+ if (family() != AF_INET && family() != AF_INET6) return "";
144
+ return (family() == AF_INET6 ?
145
+ std::string("[") + address() + "]:" :
146
+ address() + ":") +
147
+ std::to_string(port());
148
+ }
149
+
150
+ void SocketAddress::Update(uint8_t* data, size_t len) {
151
+ CHECK_LE(len, sizeof(address_));
152
+ memcpy(&address_, data, len);
153
+ }
154
+
155
+ void SocketAddress::Update(const sockaddr* data, size_t len) {
156
+ CHECK_LE(len, sizeof(address_));
157
+ memcpy(&address_, data, len);
158
+ }
159
+
160
+ v8::MaybeLocal<v8::Object> SocketAddress::ToJS(
161
+ Environment* env,
162
+ v8::Local<v8::Object> info) const {
163
+ return AddressToJS(env, data(), info);
164
+ }
165
+
166
+ bool SocketAddress::operator==(const SocketAddress& other) const {
167
+ if (family() != other.family()) return false;
168
+ return memcmp(raw(), other.raw(), length()) == 0;
169
+ }
170
+
171
+ bool SocketAddress::operator!=(const SocketAddress& other) const {
172
+ return !(*this == other);
173
+ }
174
+
175
+ bool SocketAddress::operator<(const SocketAddress& other) const {
176
+ return compare(other) == CompareResult::LESS_THAN;
177
+ }
178
+
179
+ bool SocketAddress::operator>(const SocketAddress& other) const {
180
+ return compare(other) == CompareResult::GREATER_THAN;
181
+ }
182
+
183
+ bool SocketAddress::operator<=(const SocketAddress& other) const {
184
+ CompareResult c = compare(other);
185
+ return c == CompareResult::NOT_COMPARABLE ? false :
186
+ c <= CompareResult::SAME;
187
+ }
188
+
189
+ bool SocketAddress::operator>=(const SocketAddress& other) const {
190
+ return compare(other) >= CompareResult::SAME;
191
+ }
192
+
193
+ template <typename T>
194
+ SocketAddressLRU<T>::SocketAddressLRU(
195
+ size_t max_size)
196
+ : max_size_(max_size) {}
197
+
198
+ template <typename T>
199
+ typename T::Type* SocketAddressLRU<T>::Peek(
200
+ const SocketAddress& address) const {
201
+ auto it = map_.find(address);
202
+ return it == std::end(map_) ? nullptr : &it->second->second;
203
+ }
204
+
205
+ template <typename T>
206
+ void SocketAddressLRU<T>::CheckExpired() {
207
+ auto it = list_.rbegin();
208
+ while (it != list_.rend()) {
209
+ if (T::CheckExpired(it->first, it->second)) {
210
+ map_.erase(it->first);
211
+ list_.pop_back();
212
+ it = list_.rbegin();
213
+ continue;
214
+ } else {
215
+ break;
216
+ }
217
+ }
218
+ }
219
+
220
+ template <typename T>
221
+ void SocketAddressLRU<T>::MemoryInfo(MemoryTracker* tracker) const {
222
+ tracker->TrackFieldWithSize("list", size() * sizeof(Pair));
223
+ }
224
+
225
+ // If an item already exists for the given address, bump up it's
226
+ // position in the LRU list and return it. If the item does not
227
+ // exist, create it. If an item is created, check the size of the
228
+ // cache and adjust if necessary. Whether the item exists or not,
229
+ // purge expired items.
230
+ template <typename T>
231
+ typename T::Type* SocketAddressLRU<T>::Upsert(
232
+ const SocketAddress& address) {
233
+
234
+ auto on_exit = OnScopeLeave([&]() { CheckExpired(); });
235
+
236
+ auto it = map_.find(address);
237
+ if (it != std::end(map_)) {
238
+ list_.splice(list_.begin(), list_, it->second);
239
+ T::Touch(it->first, &it->second->second);
240
+ return &it->second->second;
241
+ }
242
+
243
+ list_.push_front(Pair(address, { }));
244
+ map_[address] = list_.begin();
245
+ T::Touch(list_.begin()->first, &list_.begin()->second);
246
+
247
+ // Drop the last item in the list if we are
248
+ // over the size limit...
249
+ if (map_.size() > max_size_) {
250
+ auto last = list_.end();
251
+ map_.erase((--last)->first);
252
+ list_.pop_back();
253
+ }
254
+
255
+ return &map_[address]->second;
256
+ }
257
+
258
+ v8::MaybeLocal<v8::Value> SocketAddressBlockList::Rule::ToV8String(
259
+ Environment* env) {
260
+ std::string str = ToString();
261
+ return ToV8Value(env->context(), str);
262
+ }
263
+ } // namespace node
264
+
265
+ #endif // defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS
266
+ #endif // SRC_NODE_SOCKADDR_INL_H_