@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,377 @@
1
+ #ifndef SRC_NODE_MESSAGING_H_
2
+ #define SRC_NODE_MESSAGING_H_
3
+
4
+ #if defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS
5
+
6
+ #include "env.h"
7
+ #include "node_mutex.h"
8
+ #include "v8.h"
9
+ #include <deque>
10
+ #include <string>
11
+ #include <unordered_map>
12
+ #include <set>
13
+
14
+ namespace node {
15
+ namespace worker {
16
+
17
+ class MessagePortData;
18
+ class MessagePort;
19
+
20
+ typedef MaybeStackBuffer<v8::Local<v8::Value>, 8> TransferList;
21
+
22
+ // Used to represent the in-flight structure of an object that is being
23
+ // transferred or cloned using postMessage().
24
+ class TransferData : public MemoryRetainer {
25
+ public:
26
+ // Deserialize this object on the receiving end after a .postMessage() call.
27
+ // - `context` may not be the same as `env->context()`. This method should
28
+ // not produce JS objects coming from Contexts other than `context`.
29
+ // - `self` is a unique_ptr for the object that this is being called on.
30
+ // - The return value is treated like a `Maybe`, i.e. if `nullptr` is
31
+ // returned, any further deserialization of the message is stopped and
32
+ // control is returned to the event loop or JS as soon as possible.
33
+ virtual BaseObjectPtr<BaseObject> Deserialize(
34
+ Environment* env,
35
+ v8::Local<v8::Context> context,
36
+ std::unique_ptr<TransferData> self) = 0;
37
+ // FinalizeTransferWrite() is the counterpart to
38
+ // BaseObject::FinalizeTransferRead(). It is called right after the transfer
39
+ // data was created, and defaults to doing nothing. After this function,
40
+ // this object should not hold any more Isolate-specific data.
41
+ virtual v8::Maybe<bool> FinalizeTransferWrite(
42
+ v8::Local<v8::Context> context, v8::ValueSerializer* serializer);
43
+ };
44
+
45
+ // Represents a single communication message.
46
+ class Message : public MemoryRetainer {
47
+ public:
48
+ // Create a Message with a specific underlying payload, in the format of the
49
+ // V8 ValueSerializer API. If `payload` is empty, this message indicates
50
+ // that the receiving message port should close itself.
51
+ explicit Message(MallocedBuffer<char>&& payload = MallocedBuffer<char>());
52
+ ~Message() = default;
53
+
54
+ Message(Message&& other) = default;
55
+ Message& operator=(Message&& other) = default;
56
+ Message& operator=(const Message&) = delete;
57
+ Message(const Message&) = delete;
58
+
59
+ // Whether this is a message indicating that the port is to be closed.
60
+ // This is the last message to be received by a MessagePort.
61
+ bool IsCloseMessage() const;
62
+
63
+ // Deserialize the contained JS value. May only be called once, and only
64
+ // after Serialize() has been called (e.g. by another thread).
65
+ v8::MaybeLocal<v8::Value> Deserialize(
66
+ Environment* env,
67
+ v8::Local<v8::Context> context,
68
+ v8::Local<v8::Value>* port_list = nullptr);
69
+
70
+ // Serialize a JS value, and optionally transfer objects, into this message.
71
+ // The Message object retains ownership of all transferred objects until
72
+ // deserialization.
73
+ // The source_port parameter, if provided, will make Serialize() throw a
74
+ // "DataCloneError" DOMException if source_port is found in transfer_list.
75
+ v8::Maybe<bool> Serialize(Environment* env,
76
+ v8::Local<v8::Context> context,
77
+ v8::Local<v8::Value> input,
78
+ const TransferList& transfer_list,
79
+ v8::Local<v8::Object> source_port =
80
+ v8::Local<v8::Object>());
81
+
82
+ // Internal method of Message that is called when a new SharedArrayBuffer
83
+ // object is encountered in the incoming value's structure.
84
+ void AddSharedArrayBuffer(std::shared_ptr<v8::BackingStore> backing_store);
85
+ // Internal method of Message that is called once serialization finishes
86
+ // and that transfers ownership of `data` to this message.
87
+ void AddTransferable(std::unique_ptr<TransferData>&& data);
88
+ // Internal method of Message that is called when a new WebAssembly.Module
89
+ // object is encountered in the incoming value's structure.
90
+ uint32_t AddWASMModule(v8::CompiledWasmModule&& mod);
91
+ // Internal method of Message that is called when a shared value is
92
+ // encountered for the first time in the incoming value's structure.
93
+ void AdoptSharedValueConveyor(v8::SharedValueConveyor&& conveyor);
94
+
95
+ // The host objects that will be transferred, as recorded by Serialize()
96
+ // (e.g. MessagePorts).
97
+ // Used for warning user about posting the target MessagePort to itself,
98
+ // which will as a side effect destroy the communication channel.
99
+ const std::vector<std::unique_ptr<TransferData>>& transferables() const {
100
+ return transferables_;
101
+ }
102
+ bool has_transferables() const {
103
+ return !transferables_.empty() || !array_buffers_.empty();
104
+ }
105
+
106
+ void MemoryInfo(MemoryTracker* tracker) const override;
107
+
108
+ SET_MEMORY_INFO_NAME(Message)
109
+ SET_SELF_SIZE(Message)
110
+
111
+ private:
112
+ MallocedBuffer<char> main_message_buf_;
113
+ // TODO(addaleax): Make this a std::variant to save storage size in the common
114
+ // case (which is that all of these vectors are empty) once that is available
115
+ // with C++17.
116
+ std::vector<std::shared_ptr<v8::BackingStore>> array_buffers_;
117
+ std::vector<std::shared_ptr<v8::BackingStore>> shared_array_buffers_;
118
+ std::vector<std::unique_ptr<TransferData>> transferables_;
119
+ std::vector<v8::CompiledWasmModule> wasm_modules_;
120
+ std::optional<v8::SharedValueConveyor> shared_value_conveyor_;
121
+
122
+ friend class MessagePort;
123
+ };
124
+
125
+ class SiblingGroup final : public std::enable_shared_from_this<SiblingGroup> {
126
+ public:
127
+ // Named SiblingGroup, Used for one-to-many BroadcastChannels.
128
+ static std::shared_ptr<SiblingGroup> Get(const std::string& name);
129
+
130
+ // Anonymous SiblingGroup, Used for one-to-one MessagePort pairs.
131
+ SiblingGroup() = default;
132
+ explicit SiblingGroup(const std::string& name);
133
+ ~SiblingGroup();
134
+
135
+ // Dispatches the Message to the collection of associated
136
+ // ports. If there is more than one destination port and
137
+ // the Message contains transferables, Dispatch will fail.
138
+ // Returns Just(true) if successful and the message was
139
+ // dispatched to at least one destination. Returns Just(false)
140
+ // if there were no destinations. Returns Nothing<bool>()
141
+ // if there was an error. If error is not nullptr, it will
142
+ // be set to an error message or warning message as appropriate.
143
+ v8::Maybe<bool> Dispatch(
144
+ MessagePortData* source,
145
+ std::shared_ptr<Message> message,
146
+ std::string* error = nullptr);
147
+
148
+ void Entangle(MessagePortData* data);
149
+ void Entangle(std::initializer_list<MessagePortData*> data);
150
+ void Disentangle(MessagePortData* data);
151
+
152
+ const std::string& name() const { return name_; }
153
+
154
+ size_t size() const { return ports_.size(); }
155
+
156
+ private:
157
+ const std::string name_;
158
+ RwLock group_mutex_; // Protects ports_.
159
+ std::set<MessagePortData*> ports_;
160
+
161
+ static void CheckSiblingGroup(const std::string& name);
162
+
163
+ using Map =
164
+ std::unordered_map<std::string, std::weak_ptr<SiblingGroup>>;
165
+
166
+ static Mutex groups_mutex_;
167
+ static Map groups_;
168
+ };
169
+
170
+ // This contains all data for a `MessagePort` instance that is not tied to
171
+ // a specific Environment/Isolate/event loop, for easier transfer between those.
172
+ class MessagePortData : public TransferData {
173
+ public:
174
+ explicit MessagePortData(MessagePort* owner);
175
+ ~MessagePortData() override;
176
+
177
+ MessagePortData(MessagePortData&& other) = delete;
178
+ MessagePortData& operator=(MessagePortData&& other) = delete;
179
+ MessagePortData(const MessagePortData& other) = delete;
180
+ MessagePortData& operator=(const MessagePortData& other) = delete;
181
+
182
+ // Add a message to the incoming queue and notify the receiver.
183
+ // This may be called from any thread.
184
+ void AddToIncomingQueue(std::shared_ptr<Message> message);
185
+ v8::Maybe<bool> Dispatch(
186
+ std::shared_ptr<Message> message,
187
+ std::string* error = nullptr);
188
+
189
+ // Turns `a` and `b` into siblings, i.e. connects the sending side of one
190
+ // to the receiving side of the other. This is not thread-safe.
191
+ static void Entangle(MessagePortData* a, MessagePortData* b);
192
+
193
+ // Removes any possible sibling. This is thread-safe (it acquires both
194
+ // `sibling_mutex_` and `mutex_`), and has to be because it is called once
195
+ // the corresponding JS handle handle wants to close
196
+ // which can happen on either side of a worker.
197
+ void Disentangle();
198
+
199
+ void MemoryInfo(MemoryTracker* tracker) const override;
200
+ BaseObjectPtr<BaseObject> Deserialize(
201
+ Environment* env,
202
+ v8::Local<v8::Context> context,
203
+ std::unique_ptr<TransferData> self) override;
204
+
205
+ SET_MEMORY_INFO_NAME(MessagePortData)
206
+ SET_SELF_SIZE(MessagePortData)
207
+
208
+ private:
209
+ // This mutex protects all fields below it, with the exception of
210
+ // sibling_.
211
+ mutable Mutex mutex_;
212
+ // TODO(addaleax): Make this a std::variant<std::shared_ptr, std::unique_ptr>
213
+ // once that is available with C++17, because std::shared_ptr comes with
214
+ // overhead that is only necessary for BroadcastChannel.
215
+ std::deque<std::shared_ptr<Message>> incoming_messages_;
216
+ MessagePort* owner_ = nullptr;
217
+ std::shared_ptr<SiblingGroup> group_;
218
+ friend class MessagePort;
219
+ friend class SiblingGroup;
220
+ };
221
+
222
+ // A message port that receives messages from other threads, including
223
+ // the uv_async_t handle that is used to notify the current event loop of
224
+ // new incoming messages.
225
+ class MessagePort : public HandleWrap {
226
+ private:
227
+ // Create a new MessagePort. The `context` argument specifies the Context
228
+ // instance that is used for creating the values emitted from this port.
229
+ // This is called by MessagePort::New(), which is the public API used for
230
+ // creating MessagePort instances.
231
+ MessagePort(Environment* env,
232
+ v8::Local<v8::Context> context,
233
+ v8::Local<v8::Object> wrap);
234
+
235
+ public:
236
+ ~MessagePort() override;
237
+
238
+ // Create a new message port instance, optionally over an existing
239
+ // `MessagePortData` object.
240
+ static MessagePort* New(Environment* env,
241
+ v8::Local<v8::Context> context,
242
+ std::unique_ptr<MessagePortData> data = {},
243
+ std::shared_ptr<SiblingGroup> sibling_group = {});
244
+
245
+ // Send a message, i.e. deliver it into the sibling's incoming queue.
246
+ // If this port is closed, or if there is no sibling, this message is
247
+ // serialized with transfers, then silently discarded.
248
+ v8::Maybe<bool> PostMessage(Environment* env,
249
+ v8::Local<v8::Context> context,
250
+ v8::Local<v8::Value> message,
251
+ const TransferList& transfer);
252
+
253
+ // Start processing messages on this port as a receiving end.
254
+ void Start();
255
+ // Stop processing messages on this port as a receiving end.
256
+ void Stop();
257
+
258
+ /* constructor */
259
+ static void New(const v8::FunctionCallbackInfo<v8::Value>& args);
260
+ /* prototype methods */
261
+ static void PostMessage(const v8::FunctionCallbackInfo<v8::Value>& args);
262
+ static void Start(const v8::FunctionCallbackInfo<v8::Value>& args);
263
+ static void Stop(const v8::FunctionCallbackInfo<v8::Value>& args);
264
+ static void CheckType(const v8::FunctionCallbackInfo<v8::Value>& args);
265
+ static void Drain(const v8::FunctionCallbackInfo<v8::Value>& args);
266
+ static void ReceiveMessage(const v8::FunctionCallbackInfo<v8::Value>& args);
267
+
268
+ /* static */
269
+ static void MoveToContext(const v8::FunctionCallbackInfo<v8::Value>& args);
270
+
271
+ // Turns `a` and `b` into siblings, i.e. connects the sending side of one
272
+ // to the receiving side of the other. This is not thread-safe.
273
+ static void Entangle(MessagePort* a, MessagePort* b);
274
+ static void Entangle(MessagePort* a, MessagePortData* b);
275
+
276
+ // Detach this port's data for transferring. After this, the MessagePortData
277
+ // is no longer associated with this handle, although it can still receive
278
+ // messages.
279
+ std::unique_ptr<MessagePortData> Detach();
280
+
281
+ void Close(
282
+ v8::Local<v8::Value> close_callback = v8::Local<v8::Value>()) override;
283
+
284
+ // Returns true if either data_ has been freed, or if the handle is being
285
+ // closed. Equivalent to the [[Detached]] internal slot in the HTML Standard.
286
+ //
287
+ // If checking if a JavaScript MessagePort object is detached, this method
288
+ // alone is often not enough, since the backing C++ MessagePort object may
289
+ // have been deleted already. For all intents and purposes, an object with a
290
+ // NULL pointer to the C++ MessagePort object is also detached.
291
+ inline bool IsDetached() const;
292
+
293
+ TransferMode GetTransferMode() const override;
294
+ std::unique_ptr<TransferData> TransferForMessaging() override;
295
+
296
+ void MemoryInfo(MemoryTracker* tracker) const override;
297
+ SET_MEMORY_INFO_NAME(MessagePort)
298
+ SET_SELF_SIZE(MessagePort)
299
+
300
+ private:
301
+ enum class MessageProcessingMode {
302
+ kNormalOperation,
303
+ kForceReadMessages
304
+ };
305
+
306
+ void OnClose() override;
307
+ void OnMessage(MessageProcessingMode mode);
308
+ void TriggerAsync();
309
+ v8::MaybeLocal<v8::Value> ReceiveMessage(
310
+ v8::Local<v8::Context> context,
311
+ MessageProcessingMode mode,
312
+ v8::Local<v8::Value>* port_list = nullptr);
313
+
314
+ std::unique_ptr<MessagePortData> data_ = nullptr;
315
+ bool receiving_messages_ = false;
316
+ uv_async_t async_;
317
+ v8::Global<v8::Function> emit_message_fn_;
318
+
319
+ friend class MessagePortData;
320
+ };
321
+
322
+ // Provide a base class from which JS classes that should be transferable or
323
+ // cloneable by postMessage() can inherit.
324
+ // See e.g. FileHandle in internal/fs/promises.js for an example.
325
+ class JSTransferable : public BaseObject {
326
+ public:
327
+ JSTransferable(Environment* env, v8::Local<v8::Object> obj);
328
+ static void New(const v8::FunctionCallbackInfo<v8::Value>& args);
329
+
330
+ TransferMode GetTransferMode() const override;
331
+ std::unique_ptr<TransferData> TransferForMessaging() override;
332
+ std::unique_ptr<TransferData> CloneForMessaging() const override;
333
+ v8::Maybe<std::vector<BaseObjectPtr<BaseObject>>>
334
+ NestedTransferables() const override;
335
+ v8::Maybe<bool> FinalizeTransferRead(
336
+ v8::Local<v8::Context> context,
337
+ v8::ValueDeserializer* deserializer) override;
338
+
339
+ SET_NO_MEMORY_INFO()
340
+ SET_MEMORY_INFO_NAME(JSTransferable)
341
+ SET_SELF_SIZE(JSTransferable)
342
+
343
+ private:
344
+ std::unique_ptr<TransferData> TransferOrClone(TransferMode mode) const;
345
+
346
+ class Data : public TransferData {
347
+ public:
348
+ Data(std::string&& deserialize_info, v8::Global<v8::Value>&& data);
349
+
350
+ BaseObjectPtr<BaseObject> Deserialize(
351
+ Environment* env,
352
+ v8::Local<v8::Context> context,
353
+ std::unique_ptr<TransferData> self) override;
354
+ v8::Maybe<bool> FinalizeTransferWrite(
355
+ v8::Local<v8::Context> context,
356
+ v8::ValueSerializer* serializer) override;
357
+
358
+ SET_NO_MEMORY_INFO()
359
+ SET_MEMORY_INFO_NAME(JSTransferableTransferData)
360
+ SET_SELF_SIZE(Data)
361
+
362
+ private:
363
+ std::string deserialize_info_;
364
+ v8::Global<v8::Value> data_;
365
+ };
366
+ };
367
+
368
+ v8::Local<v8::FunctionTemplate> GetMessagePortConstructorTemplate(
369
+ Environment* env);
370
+
371
+ } // namespace worker
372
+ } // namespace node
373
+
374
+ #endif // defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS
375
+
376
+
377
+ #endif // SRC_NODE_MESSAGING_H_
@@ -0,0 +1,138 @@
1
+ #ifndef SRC_NODE_METADATA_H_
2
+ #define SRC_NODE_METADATA_H_
3
+
4
+ #if defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS
5
+
6
+ #include <string>
7
+ #include "node_version.h"
8
+
9
+ #if HAVE_OPENSSL
10
+ #include <openssl/crypto.h>
11
+ #if NODE_OPENSSL_HAS_QUIC
12
+ #include <openssl/quic.h>
13
+ #endif
14
+ #endif // HAVE_OPENSSL
15
+
16
+ namespace node {
17
+
18
+ // if this is a release build and no explicit base has been set
19
+ // substitute the standard release download URL
20
+ #ifndef NODE_RELEASE_URLBASE
21
+ #if NODE_VERSION_IS_RELEASE
22
+ #define NODE_RELEASE_URLBASE "https://nodejs.org/download/release/"
23
+ #endif // NODE_VERSION_IS_RELEASE
24
+ #endif // NODE_RELEASE_URLBASE
25
+
26
+ #if defined(NODE_RELEASE_URLBASE)
27
+ #define NODE_HAS_RELEASE_URLS
28
+ #endif
29
+
30
+ #ifndef NODE_SHARED_BUILTIN_UNDICI_UNDICI_PATH
31
+ #define NODE_VERSIONS_KEY_UNDICI(V) V(undici)
32
+ #else
33
+ #define NODE_VERSIONS_KEY_UNDICI(V)
34
+ #endif
35
+
36
+ #define NODE_VERSIONS_KEYS_BASE(V) \
37
+ V(node) \
38
+ V(v8) \
39
+ V(uv) \
40
+ V(zlib) \
41
+ V(brotli) \
42
+ V(ares) \
43
+ V(modules) \
44
+ V(nghttp2) \
45
+ V(napi) \
46
+ V(llhttp) \
47
+ V(uvwasi) \
48
+ V(acorn) \
49
+ V(simdutf) \
50
+ V(ada) \
51
+ NODE_VERSIONS_KEY_UNDICI(V) \
52
+ V(cjs_module_lexer) \
53
+ V(base64)
54
+
55
+ #if HAVE_OPENSSL
56
+ #define NODE_VERSIONS_KEY_CRYPTO(V) V(openssl)
57
+ #else
58
+ #define NODE_VERSIONS_KEY_CRYPTO(V)
59
+ #endif
60
+
61
+ #ifdef NODE_HAVE_I18N_SUPPORT
62
+ #define NODE_VERSIONS_KEY_INTL(V) \
63
+ V(cldr) \
64
+ V(icu) \
65
+ V(tz) \
66
+ V(unicode)
67
+ #else
68
+ #define NODE_VERSIONS_KEY_INTL(V)
69
+ #endif // NODE_HAVE_I18N_SUPPORT
70
+
71
+ #ifdef OPENSSL_INFO_QUIC
72
+ #define NODE_VERSIONS_KEY_QUIC(V) \
73
+ V(ngtcp2) \
74
+ V(nghttp3)
75
+ #else
76
+ #define NODE_VERSIONS_KEY_QUIC(V)
77
+ #endif
78
+
79
+ #define NODE_VERSIONS_KEYS(V) \
80
+ NODE_VERSIONS_KEYS_BASE(V) \
81
+ NODE_VERSIONS_KEY_CRYPTO(V) \
82
+ NODE_VERSIONS_KEY_INTL(V) \
83
+ NODE_VERSIONS_KEY_QUIC(V)
84
+
85
+ class Metadata {
86
+ public:
87
+ Metadata();
88
+ Metadata(Metadata&) = delete;
89
+ Metadata(Metadata&&) = delete;
90
+ Metadata operator=(Metadata&) = delete;
91
+ Metadata operator=(Metadata&&) = delete;
92
+
93
+ struct Versions {
94
+ Versions();
95
+
96
+ #ifdef NODE_HAVE_I18N_SUPPORT
97
+ // Must be called on the main thread after
98
+ // i18n::InitializeICUDirectory()
99
+ void InitializeIntlVersions();
100
+ #endif // NODE_HAVE_I18N_SUPPORT
101
+
102
+ #define V(key) std::string key;
103
+ NODE_VERSIONS_KEYS(V)
104
+ #undef V
105
+ };
106
+
107
+ struct Release {
108
+ Release();
109
+
110
+ std::string name;
111
+ #if NODE_VERSION_IS_LTS
112
+ std::string lts;
113
+ #endif // NODE_VERSION_IS_LTS
114
+
115
+ #ifdef NODE_HAS_RELEASE_URLS
116
+ std::string source_url;
117
+ std::string headers_url;
118
+ #ifdef _WIN32
119
+ std::string lib_url;
120
+ #endif // _WIN32
121
+ #endif // NODE_HAS_RELEASE_URLS
122
+ };
123
+
124
+ Versions versions;
125
+ const Release release;
126
+ const std::string arch;
127
+ const std::string platform;
128
+ };
129
+
130
+ // Per-process global
131
+ namespace per_process {
132
+ extern Metadata metadata;
133
+ }
134
+
135
+ } // namespace node
136
+
137
+ #endif // defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS
138
+ #endif // SRC_NODE_METADATA_H_