@adapt-toolkit/sdk 0.6.0 → 0.6.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 (288) hide show
  1. package/dist/common/index.d.ts +7 -0
  2. package/dist/common/index.d.ts.map +1 -0
  3. package/dist/common/index.js +7 -0
  4. package/dist/common/index.js.map +1 -0
  5. package/dist/common/interfaces/adapt_environment.d.ts +21 -0
  6. package/dist/common/interfaces/adapt_environment.d.ts.map +1 -0
  7. package/dist/common/interfaces/adapt_environment.js +2 -0
  8. package/dist/common/interfaces/adapt_environment.js.map +1 -0
  9. package/dist/common/interfaces/adapt_evaluation_unit.d.ts +12 -0
  10. package/dist/common/interfaces/adapt_evaluation_unit.d.ts.map +1 -0
  11. package/dist/common/interfaces/adapt_evaluation_unit.js +2 -0
  12. package/dist/common/interfaces/adapt_evaluation_unit.js.map +1 -0
  13. package/dist/common/interfaces/adapt_packet_context.d.ts +29 -0
  14. package/dist/common/interfaces/adapt_packet_context.d.ts.map +1 -0
  15. package/dist/common/interfaces/adapt_packet_context.js +2 -0
  16. package/dist/common/interfaces/adapt_packet_context.js.map +1 -0
  17. package/dist/common/interfaces/adapt_value.d.ts +23 -0
  18. package/dist/common/interfaces/adapt_value.d.ts.map +1 -0
  19. package/dist/common/interfaces/adapt_value.js +2 -0
  20. package/dist/common/interfaces/adapt_value.js.map +1 -0
  21. package/dist/common/interfaces/common.d.ts +18 -0
  22. package/dist/common/interfaces/common.d.ts.map +1 -0
  23. package/dist/common/interfaces/common.js +4 -0
  24. package/dist/common/interfaces/common.js.map +1 -0
  25. package/dist/common/memory_management/adapt_object_lifetime.d.ts +121 -0
  26. package/dist/common/memory_management/adapt_object_lifetime.d.ts.map +1 -0
  27. package/dist/common/memory_management/adapt_object_lifetime.js +168 -0
  28. package/dist/common/memory_management/adapt_object_lifetime.js.map +1 -0
  29. package/dist/index.d.ts +3 -0
  30. package/dist/index.d.ts.map +1 -0
  31. package/dist/index.js +3 -0
  32. package/dist/index.js.map +1 -0
  33. package/dist/loaders/browser.d.ts +5 -0
  34. package/dist/loaders/browser.d.ts.map +1 -0
  35. package/dist/loaders/browser.js +6 -0
  36. package/dist/loaders/browser.js.map +1 -0
  37. package/dist/loaders/node.d.ts +13 -0
  38. package/dist/loaders/node.d.ts.map +1 -0
  39. package/dist/loaders/node.js +33 -0
  40. package/dist/loaders/node.js.map +1 -0
  41. package/dist/loaders/react-native.d.ts +11 -0
  42. package/dist/loaders/react-native.d.ts.map +1 -0
  43. package/dist/loaders/react-native.js +19 -0
  44. package/dist/loaders/react-native.js.map +1 -0
  45. package/dist/loaders/shared.d.ts +17 -0
  46. package/dist/loaders/shared.d.ts.map +1 -0
  47. package/dist/loaders/shared.js +2 -0
  48. package/dist/loaders/shared.js.map +1 -0
  49. package/dist/mufl_files/794F6D9EE08C05F2A5768F0E71512120A559D3FC83B106F792FFBAFDA7C016A4.muflo +0 -0
  50. package/dist/utilities/broker/broker.d.ts +30 -0
  51. package/dist/utilities/broker/broker.d.ts.map +1 -0
  52. package/dist/utilities/broker/broker.js +649 -0
  53. package/dist/utilities/broker/broker.js.map +1 -0
  54. package/dist/utilities/broker/broker_client.d.ts +39 -0
  55. package/dist/utilities/broker/broker_client.d.ts.map +1 -0
  56. package/dist/utilities/broker/broker_client.js +188 -0
  57. package/dist/utilities/broker/broker_client.js.map +1 -0
  58. package/dist/utilities/broker/broker_configurator.d.ts +7 -0
  59. package/dist/utilities/broker/broker_configurator.d.ts.map +1 -0
  60. package/dist/utilities/broker/broker_configurator.js +16 -0
  61. package/dist/utilities/broker/broker_configurator.js.map +1 -0
  62. package/dist/utilities/broker/index.d.ts +5 -0
  63. package/dist/utilities/broker/index.d.ts.map +1 -0
  64. package/dist/utilities/broker/index.js +5 -0
  65. package/dist/utilities/broker/index.js.map +1 -0
  66. package/dist/utilities/broker/pending_message_store.d.ts +13 -0
  67. package/dist/utilities/broker/pending_message_store.d.ts.map +1 -0
  68. package/dist/utilities/broker/pending_message_store.js +40 -0
  69. package/dist/utilities/broker/pending_message_store.js.map +1 -0
  70. package/dist/utilities/common/adapt_waitlist.d.ts +5 -0
  71. package/dist/utilities/common/adapt_waitlist.d.ts.map +1 -0
  72. package/dist/utilities/common/adapt_waitlist.js +4 -0
  73. package/dist/utilities/common/adapt_waitlist.js.map +1 -0
  74. package/dist/utilities/common/async_queue_adapter.d.ts +40 -0
  75. package/dist/utilities/common/async_queue_adapter.d.ts.map +1 -0
  76. package/dist/utilities/common/async_queue_adapter.js +88 -0
  77. package/dist/utilities/common/async_queue_adapter.js.map +1 -0
  78. package/dist/utilities/common/command_line_configurator.d.ts +88 -0
  79. package/dist/utilities/common/command_line_configurator.d.ts.map +1 -0
  80. package/dist/utilities/common/command_line_configurator.js +217 -0
  81. package/dist/utilities/common/command_line_configurator.js.map +1 -0
  82. package/dist/utilities/common/decorators.d.ts +17 -0
  83. package/dist/utilities/common/decorators.d.ts.map +1 -0
  84. package/dist/utilities/common/decorators.js +84 -0
  85. package/dist/utilities/common/decorators.js.map +1 -0
  86. package/dist/utilities/common/errors/adapt_initialization_error.d.ts +4 -0
  87. package/dist/utilities/common/errors/adapt_initialization_error.d.ts.map +1 -0
  88. package/dist/utilities/common/errors/adapt_initialization_error.js +6 -0
  89. package/dist/utilities/common/errors/adapt_initialization_error.js.map +1 -0
  90. package/dist/utilities/common/errors/adapt_network_error.d.ts +8 -0
  91. package/dist/utilities/common/errors/adapt_network_error.d.ts.map +1 -0
  92. package/dist/utilities/common/errors/adapt_network_error.js +12 -0
  93. package/dist/utilities/common/errors/adapt_network_error.js.map +1 -0
  94. package/dist/utilities/common/errors/adapt_protocol_error.d.ts +13 -0
  95. package/dist/utilities/common/errors/adapt_protocol_error.d.ts.map +1 -0
  96. package/dist/utilities/common/errors/adapt_protocol_error.js +17 -0
  97. package/dist/utilities/common/errors/adapt_protocol_error.js.map +1 -0
  98. package/dist/utilities/common/errors/adapt_test_mode_error.d.ts +11 -0
  99. package/dist/utilities/common/errors/adapt_test_mode_error.d.ts.map +1 -0
  100. package/dist/utilities/common/errors/adapt_test_mode_error.js +22 -0
  101. package/dist/utilities/common/errors/adapt_test_mode_error.js.map +1 -0
  102. package/dist/utilities/common/http_server.d.ts +20 -0
  103. package/dist/utilities/common/http_server.d.ts.map +1 -0
  104. package/dist/utilities/common/http_server.js +44 -0
  105. package/dist/utilities/common/http_server.js.map +1 -0
  106. package/dist/utilities/common/index.d.ts +26 -0
  107. package/dist/utilities/common/index.d.ts.map +1 -0
  108. package/dist/utilities/common/index.js +36 -0
  109. package/dist/utilities/common/index.js.map +1 -0
  110. package/dist/utilities/common/logger/logger.d.ts +83 -0
  111. package/dist/utilities/common/logger/logger.d.ts.map +1 -0
  112. package/dist/utilities/common/logger/logger.js +272 -0
  113. package/dist/utilities/common/logger/logger.js.map +1 -0
  114. package/dist/utilities/common/logger/logging_level.d.ts +13 -0
  115. package/dist/utilities/common/logger/logging_level.d.ts.map +1 -0
  116. package/dist/utilities/common/logger/logging_level.js +36 -0
  117. package/dist/utilities/common/logger/logging_level.js.map +1 -0
  118. package/dist/utilities/common/logger/message.d.ts +65 -0
  119. package/dist/utilities/common/logger/message.d.ts.map +1 -0
  120. package/dist/utilities/common/logger/message.js +169 -0
  121. package/dist/utilities/common/logger/message.js.map +1 -0
  122. package/dist/utilities/common/logger/network_component.d.ts +3 -0
  123. package/dist/utilities/common/logger/network_component.d.ts.map +1 -0
  124. package/dist/utilities/common/logger/network_component.js +4 -0
  125. package/dist/utilities/common/logger/network_component.js.map +1 -0
  126. package/dist/utilities/common/totp_qr_code_generator.d.ts +12 -0
  127. package/dist/utilities/common/totp_qr_code_generator.d.ts.map +1 -0
  128. package/dist/utilities/common/totp_qr_code_generator.js +19 -0
  129. package/dist/utilities/common/totp_qr_code_generator.js.map +1 -0
  130. package/dist/utilities/common/waitlist/waitlist.d.ts +36 -0
  131. package/dist/utilities/common/waitlist/waitlist.d.ts.map +1 -0
  132. package/dist/utilities/common/waitlist/waitlist.js +116 -0
  133. package/dist/utilities/common/waitlist/waitlist.js.map +1 -0
  134. package/dist/utilities/common/web_socket_client.d.ts +66 -0
  135. package/dist/utilities/common/web_socket_client.d.ts.map +1 -0
  136. package/dist/utilities/common/web_socket_client.js +214 -0
  137. package/dist/utilities/common/web_socket_client.js.map +1 -0
  138. package/dist/utilities/common/web_socket_connection.d.ts +58 -0
  139. package/dist/utilities/common/web_socket_connection.d.ts.map +1 -0
  140. package/dist/utilities/common/web_socket_connection.js +204 -0
  141. package/dist/utilities/common/web_socket_connection.js.map +1 -0
  142. package/dist/utilities/evaluator/common.d.ts +10 -0
  143. package/dist/utilities/evaluator/common.d.ts.map +1 -0
  144. package/dist/utilities/evaluator/common.js +128 -0
  145. package/dist/utilities/evaluator/common.js.map +1 -0
  146. package/dist/utilities/evaluator/evaluator.d.ts +35 -0
  147. package/dist/utilities/evaluator/evaluator.d.ts.map +1 -0
  148. package/dist/utilities/evaluator/evaluator.js +141 -0
  149. package/dist/utilities/evaluator/evaluator.js.map +1 -0
  150. package/dist/utilities/evaluator/index.d.ts +3 -0
  151. package/dist/utilities/evaluator/index.d.ts.map +1 -0
  152. package/dist/utilities/evaluator/index.js +3 -0
  153. package/dist/utilities/evaluator/index.js.map +1 -0
  154. package/dist/utilities/executables/adapt_wrapper.d.ts +3 -0
  155. package/dist/utilities/executables/adapt_wrapper.d.ts.map +1 -0
  156. package/dist/utilities/executables/adapt_wrapper.js +25 -0
  157. package/dist/utilities/executables/adapt_wrapper.js.map +1 -0
  158. package/dist/utilities/executables/adapt_wrapper_mobile.d.ts +3 -0
  159. package/dist/utilities/executables/adapt_wrapper_mobile.d.ts.map +1 -0
  160. package/dist/utilities/executables/adapt_wrapper_mobile.js +20 -0
  161. package/dist/utilities/executables/adapt_wrapper_mobile.js.map +1 -0
  162. package/dist/utilities/executables/adapt_wrapper_script.d.ts +2 -0
  163. package/dist/utilities/executables/adapt_wrapper_script.d.ts.map +1 -0
  164. package/dist/utilities/executables/adapt_wrapper_script.js +6 -0
  165. package/dist/utilities/executables/adapt_wrapper_script.js.map +1 -0
  166. package/dist/utilities/executables/broker.d.ts +2 -0
  167. package/dist/utilities/executables/broker.d.ts.map +1 -0
  168. package/dist/utilities/executables/broker.js +28 -0
  169. package/dist/utilities/executables/broker.js.map +1 -0
  170. package/dist/utilities/executables/index.d.ts +2 -0
  171. package/dist/utilities/executables/index.d.ts.map +1 -0
  172. package/dist/utilities/executables/index.js +2 -0
  173. package/dist/utilities/executables/index.js.map +1 -0
  174. package/dist/utilities/executables/local_data_storage.d.ts +2 -0
  175. package/dist/utilities/executables/local_data_storage.d.ts.map +1 -0
  176. package/dist/utilities/executables/local_data_storage.js +20 -0
  177. package/dist/utilities/executables/local_data_storage.js.map +1 -0
  178. package/dist/utilities/executables/local_data_storage_cmd_helper.d.ts +2 -0
  179. package/dist/utilities/executables/local_data_storage_cmd_helper.d.ts.map +1 -0
  180. package/dist/utilities/executables/local_data_storage_cmd_helper.js +52 -0
  181. package/dist/utilities/executables/local_data_storage_cmd_helper.js.map +1 -0
  182. package/dist/utilities/storage/data_storage.d.ts +97 -0
  183. package/dist/utilities/storage/data_storage.d.ts.map +1 -0
  184. package/dist/utilities/storage/data_storage.js +445 -0
  185. package/dist/utilities/storage/data_storage.js.map +1 -0
  186. package/dist/utilities/storage/index.d.ts +6 -0
  187. package/dist/utilities/storage/index.d.ts.map +1 -0
  188. package/dist/utilities/storage/index.js +6 -0
  189. package/dist/utilities/storage/index.js.map +1 -0
  190. package/dist/utilities/storage/local_data_storage.d.ts +7 -0
  191. package/dist/utilities/storage/local_data_storage.d.ts.map +1 -0
  192. package/dist/utilities/storage/local_data_storage.js +18 -0
  193. package/dist/utilities/storage/local_data_storage.js.map +1 -0
  194. package/dist/utilities/storage/local_data_storage_base.d.ts +74 -0
  195. package/dist/utilities/storage/local_data_storage_base.d.ts.map +1 -0
  196. package/dist/utilities/storage/local_data_storage_base.js +163 -0
  197. package/dist/utilities/storage/local_data_storage_base.js.map +1 -0
  198. package/dist/utilities/storage/local_data_storage_configurator.d.ts +7 -0
  199. package/dist/utilities/storage/local_data_storage_configurator.d.ts.map +1 -0
  200. package/dist/utilities/storage/local_data_storage_configurator.js +13 -0
  201. package/dist/utilities/storage/local_data_storage_configurator.js.map +1 -0
  202. package/dist/utilities/storage/storage_client.d.ts +29 -0
  203. package/dist/utilities/storage/storage_client.d.ts.map +1 -0
  204. package/dist/utilities/storage/storage_client.js +98 -0
  205. package/dist/utilities/storage/storage_client.js.map +1 -0
  206. package/dist/utilities/wrappers/configurators/adapt_wrapper_configurator.d.ts +14 -0
  207. package/dist/utilities/wrappers/configurators/adapt_wrapper_configurator.d.ts.map +1 -0
  208. package/dist/utilities/wrappers/configurators/adapt_wrapper_configurator.js +57 -0
  209. package/dist/utilities/wrappers/configurators/adapt_wrapper_configurator.js.map +1 -0
  210. package/dist/utilities/wrappers/configurators/control_packet_configurator.d.ts +12 -0
  211. package/dist/utilities/wrappers/configurators/control_packet_configurator.d.ts.map +1 -0
  212. package/dist/utilities/wrappers/configurators/control_packet_configurator.js +52 -0
  213. package/dist/utilities/wrappers/configurators/control_packet_configurator.js.map +1 -0
  214. package/dist/utilities/wrappers/configurators/packet_wrapper_configurator.d.ts +32 -0
  215. package/dist/utilities/wrappers/configurators/packet_wrapper_configurator.d.ts.map +1 -0
  216. package/dist/utilities/wrappers/configurators/packet_wrapper_configurator.js +123 -0
  217. package/dist/utilities/wrappers/configurators/packet_wrapper_configurator.js.map +1 -0
  218. package/dist/utilities/wrappers/enclave/control_packet.d.ts +18 -0
  219. package/dist/utilities/wrappers/enclave/control_packet.d.ts.map +1 -0
  220. package/dist/utilities/wrappers/enclave/control_packet.js +213 -0
  221. package/dist/utilities/wrappers/enclave/control_packet.js.map +1 -0
  222. package/dist/utilities/wrappers/index.d.ts +5 -0
  223. package/dist/utilities/wrappers/index.d.ts.map +1 -0
  224. package/dist/utilities/wrappers/index.js +5 -0
  225. package/dist/utilities/wrappers/index.js.map +1 -0
  226. package/dist/utilities/wrappers/wrappers/adapt_packet_wrapper.d.ts +21 -0
  227. package/dist/utilities/wrappers/wrappers/adapt_packet_wrapper.d.ts.map +1 -0
  228. package/dist/utilities/wrappers/wrappers/adapt_packet_wrapper.js +67 -0
  229. package/dist/utilities/wrappers/wrappers/adapt_packet_wrapper.js.map +1 -0
  230. package/dist/utilities/wrappers/wrappers/adapt_wrapper.d.ts +139 -0
  231. package/dist/utilities/wrappers/wrappers/adapt_wrapper.d.ts.map +1 -0
  232. package/dist/utilities/wrappers/wrappers/adapt_wrapper.js +811 -0
  233. package/dist/utilities/wrappers/wrappers/adapt_wrapper.js.map +1 -0
  234. package/dist/utilities/wrappers/wrappers/impl/adapt_packet_wrapper_impl.d.ts +78 -0
  235. package/dist/utilities/wrappers/wrappers/impl/adapt_packet_wrapper_impl.d.ts.map +1 -0
  236. package/dist/utilities/wrappers/wrappers/impl/adapt_packet_wrapper_impl.js +607 -0
  237. package/dist/utilities/wrappers/wrappers/impl/adapt_packet_wrapper_impl.js.map +1 -0
  238. package/dist/wasm/LICENSE.aefl +172 -0
  239. package/dist/wasm/adapt.d.ts +79 -0
  240. package/dist/wasm/adapt.d.ts.map +1 -0
  241. package/dist/wasm/adapt.js +450 -0
  242. package/dist/wasm/adapt.js.map +1 -0
  243. package/dist/wasm/backend-node.d.ts +9 -0
  244. package/dist/wasm/backend-node.d.ts.map +1 -0
  245. package/dist/wasm/backend-node.js +27 -0
  246. package/dist/wasm/backend-node.js.map +1 -0
  247. package/dist/wasm/index.d.ts +12 -0
  248. package/dist/wasm/index.d.ts.map +1 -0
  249. package/dist/wasm/index.js +15 -0
  250. package/dist/wasm/index.js.map +1 -0
  251. package/dist/wasm/mufl-bindings.d.ts +3 -0
  252. package/dist/wasm/mufl-bindings.d.ts.map +1 -0
  253. package/dist/wasm/mufl-bindings.js +3852 -0
  254. package/dist/wasm/mufl-bindings.js.map +1 -0
  255. package/dist/wasm/mufl-bindings.wasm +0 -0
  256. package/dist/wasm/wasm_types.d.ts +97 -0
  257. package/dist/wasm/wasm_types.d.ts.map +1 -0
  258. package/dist/wasm/wasm_types.js +2 -0
  259. package/dist/wasm/wasm_types.js.map +1 -0
  260. package/dist/wrapper/conversion.d.ts +9 -0
  261. package/dist/wrapper/conversion.d.ts.map +1 -0
  262. package/dist/wrapper/conversion.js +41 -0
  263. package/dist/wrapper/conversion.js.map +1 -0
  264. package/dist/wrapper/index.d.ts +12 -0
  265. package/dist/wrapper/index.d.ts.map +1 -0
  266. package/dist/wrapper/index.js +26 -0
  267. package/dist/wrapper/index.js.map +1 -0
  268. package/dist/wrapper/memory_management/adapt_builder.d.ts +36 -0
  269. package/dist/wrapper/memory_management/adapt_builder.d.ts.map +1 -0
  270. package/dist/wrapper/memory_management/adapt_builder.js +98 -0
  271. package/dist/wrapper/memory_management/adapt_builder.js.map +1 -0
  272. package/dist/wrapper/memory_management/exit_handler_registrar.d.ts +10 -0
  273. package/dist/wrapper/memory_management/exit_handler_registrar.d.ts.map +1 -0
  274. package/dist/wrapper/memory_management/exit_handler_registrar.js +38 -0
  275. package/dist/wrapper/memory_management/exit_handler_registrar.js.map +1 -0
  276. package/dist/wrapper/protocol/broker_frame.d.ts +20 -0
  277. package/dist/wrapper/protocol/broker_frame.d.ts.map +1 -0
  278. package/dist/wrapper/protocol/broker_frame.js +102 -0
  279. package/dist/wrapper/protocol/broker_frame.js.map +1 -0
  280. package/dist/wrapper/protocol/message_handler_interfaces.d.ts +32 -0
  281. package/dist/wrapper/protocol/message_handler_interfaces.d.ts.map +1 -0
  282. package/dist/wrapper/protocol/message_handler_interfaces.js +2 -0
  283. package/dist/wrapper/protocol/message_handler_interfaces.js.map +1 -0
  284. package/dist/wrapper/protocol/protocol.d.ts +177 -0
  285. package/dist/wrapper/protocol/protocol.d.ts.map +1 -0
  286. package/dist/wrapper/protocol/protocol.js +432 -0
  287. package/dist/wrapper/protocol/protocol.js.map +1 -0
  288. package/package.json +2 -2
@@ -0,0 +1,811 @@
1
+ import { AsyncQueueAdapter, logging, AdaptInitializationError, LazyMessage } from '../../common/index.js';
2
+ import { AdaptObjectBuilder, object_to_adapt_value, Protocol, ReadFile } from '../../../wrapper/index.js';
3
+ import { AdaptEnvironment, AdaptObjectLifetime, AdaptPacketContext } from '@adapt-toolkit/sdk/backend';
4
+ import { AdaptPacketWrapper } from './adapt_packet_wrapper.js';
5
+ import { PacketWrapperConfigurator } from '../configurators/packet_wrapper_configurator.js';
6
+ import path from 'path';
7
+ import { BrokerClient } from '../../broker/index.js';
8
+ import { StorageClient } from '../../storage/index.js';
9
+ import { AdaptNetworkComponentConfigurator } from '../../common/index.js';
10
+ var AdaptWrapperServices;
11
+ (function (AdaptWrapperServices) {
12
+ class PacketManager {
13
+ /**
14
+ * @units is a map of locally available(i.e. previously loaded from the storage) units by its hash.
15
+ * Before requesting unit from the data storage, we have to first check, whether we have it locally
16
+ *
17
+ * @requested_units is a map of already requested, but not yet received units.
18
+ * It has to contain more than one callback, that should be called when the unit is received, because of the following situation:
19
+ * One adapt wrapper could manage several AdaptPacketWrappers with the same unit code underneath, but created with different seed phrases.
20
+ * Those packets could be requested simultanesously, in this case we don't want to request a unit from a storage multiple times.
21
+ * Instead, we can add a callback to an existing field in the map and call it as soon as we receive a unit from the storage.
22
+ *
23
+ * @packets is a map of AdaptPacketWrappers by packet's id. We add a packet there each time, when we create a new packet wrapper.
24
+ * It allows us to track packets managed by us and redirect messages addressed to packets.
25
+ *
26
+ */
27
+ #units = new Map(); // locally available units
28
+ #requested_units = new Map(); // these units have already been requested from data storage, but not yet received
29
+ #packets = new Map(); // created packets
30
+ #request_from_storage; // callback to request a unit from the data storage
31
+ #wrapper;
32
+ constructor(wrapper, request_from_storage) {
33
+ this.#wrapper = wrapper;
34
+ this.#request_from_storage = request_from_storage;
35
+ }
36
+ request_unit = (hash, cb) => {
37
+ logging.debug("Requesting unit: ", hash);
38
+ if (this.#units.has(hash)) {
39
+ logging.debug("Unit found: ", hash);
40
+ cb(this.#units.get(hash));
41
+ }
42
+ else if (this.#requested_units.has(hash)) {
43
+ logging.debug("Unit has already been requested. Waiting: ", hash);
44
+ this.#requested_units.set(hash, this.#requested_units.get(hash).concat(cb));
45
+ }
46
+ else {
47
+ logging.debug("Requesting unit from the data storage: ", hash);
48
+ this.#requested_units.set(hash, [cb]);
49
+ this.#request_from_storage(hash);
50
+ // request a unit data from the storage
51
+ }
52
+ };
53
+ request_unit_local = (hash, path, cb) => {
54
+ if (this.#units.has(hash)) {
55
+ cb(this.#units.get(hash));
56
+ return;
57
+ }
58
+ if (this.#requested_units.has(hash)) {
59
+ this.#requested_units.set(hash, this.#requested_units.get(hash).concat(cb));
60
+ return;
61
+ }
62
+ this.#requested_units.set(hash, [cb]);
63
+ try {
64
+ ReadFile(path).then(data => {
65
+ this.add_unit(hash, data);
66
+ });
67
+ }
68
+ catch (e) {
69
+ throw new AdaptInitializationError("Failed to load a unit from file: " + e);
70
+ }
71
+ };
72
+ add_unit = (hash, data) => {
73
+ if (!this.#requested_units.has(hash))
74
+ throw new Error("Internal error: returned unit that wasn't requested: " + hash);
75
+ this.#requested_units.get(hash).forEach(cb => {
76
+ cb(data);
77
+ });
78
+ this.#requested_units.delete(hash);
79
+ this.#units.set(hash, data);
80
+ };
81
+ add_unit_external = (hash, data) => {
82
+ this.#units.set(hash, data);
83
+ this.#requested_units.delete(hash);
84
+ };
85
+ add_packet = (id, wrapper) => {
86
+ if (this.#packets.has(id))
87
+ throw new Error("Packet with given id already exists: " + id);
88
+ this.#packets.set(id, wrapper);
89
+ this.#wrapper.deliver_pending_messages_locally(id);
90
+ this.#wrapper.register_packet_with_broker(id);
91
+ };
92
+ remove_packet = (id) => {
93
+ if (!this.#packets.has(id)) {
94
+ throw new Error("Attempting to delete a packet with unknown id: " + id);
95
+ }
96
+ this.#packets.delete(id);
97
+ };
98
+ get packets() {
99
+ return this.#packets;
100
+ }
101
+ init = (packets) => {
102
+ packets.forEach(config => {
103
+ // each packet should either have a state hash(backup the packet from storage) or a unit hash(create a new packet from unit loaded from the storage)
104
+ const packet_id = config.packet_id;
105
+ const state_hash = config.state_hash;
106
+ const unit_hash = config.unit_hash;
107
+ if (config.load_from_storage) {
108
+ if (!this.#wrapper.storage_available)
109
+ throw new AdaptInitializationError("Failed to load packet from the storage. Storage is not available in the current wrapper configuration.");
110
+ if (state_hash || packet_id) {
111
+ logging.debug("Backing up a packet.", "State hash: ", state_hash, ", packet id: ", packet_id);
112
+ this.#wrapper.back_up_packet(config);
113
+ }
114
+ else if (unit_hash) {
115
+ logging.debug("Creating a packet.");
116
+ this.#wrapper.create_packet(config);
117
+ }
118
+ else
119
+ throw new Error("Neither --state_hash nor --unit_hash is specified.");
120
+ }
121
+ else {
122
+ this.#wrapper.create_packet_local(config);
123
+ }
124
+ });
125
+ };
126
+ add_packet_local = (packet_config, on_packet_created, unit, _packet) => {
127
+ AdaptObjectBuilder.execute_and_collect_garbage(async (builder) => {
128
+ const seed_phrase = packet_config.seed_phrase ? packet_config.seed_phrase : AdaptEnvironment.GetRandomBytes(builder.lifetime, 16).Visualize();
129
+ const entropy = builder.AdaptEnvironment.GetRandomBytes(256);
130
+ const init_arg = packet_config.init_arg ? builder.Attach(object_to_adapt_value(JSON.parse(packet_config.init_arg))) : undefined;
131
+ let packet = _packet
132
+ ? builder.Attach(_packet)
133
+ : builder.AdaptEnvironment.CreatePacket(unit, "Seed phrase: " + seed_phrase, AdaptNetworkComponentConfigurator.test_mode ? "" : entropy.Visualize().substring(2), false, init_arg).Detach();
134
+ logging.info("Packet created with id:", AdaptObjectBuilder.execute_and_collect_garbage(b => b.Attach(packet.GetContainerID()).Visualize()));
135
+ let messages_adapt_values = [];
136
+ if (packet_config instanceof PacketWrapperConfigurator) {
137
+ packet_config.transactions.forEach(({ name, targ }) => {
138
+ const targ_mufl = packet.ParseValueFromJSON(targ);
139
+ const message = object_to_adapt_value({ name: name, targ: targ_mufl });
140
+ messages_adapt_values.push(message);
141
+ });
142
+ }
143
+ let wrapper = new AdaptPacketWrapper(packet, packet_config, this.#wrapper, messages_adapt_values);
144
+ this.add_packet(AdaptObjectBuilder.execute_and_collect_garbage(b => b.Attach(wrapper.packet.GetContainerID()).Visualize()), wrapper);
145
+ if (packet_config instanceof PacketWrapperConfigurator)
146
+ this.#wrapper.on_packet_created(AdaptObjectBuilder.execute_and_collect_garbage(b => b.Attach(wrapper.packet.GetContainerID()).Visualize()), wrapper);
147
+ else {
148
+ if (!this.#wrapper.control_packet) {
149
+ logging.error("Control packet is undefined.");
150
+ return;
151
+ }
152
+ this.#wrapper.control_packet.packet = wrapper;
153
+ this.#wrapper.on_control_packet_created(wrapper.packet.GetContainerID().Visualize(), wrapper);
154
+ }
155
+ on_packet_created(wrapper);
156
+ });
157
+ };
158
+ create_packet = (packet_config, on_packet_created, unit_contents, _packet) => {
159
+ const create_packet_cb = () => {
160
+ AdaptObjectBuilder.execute_and_collect_garbage(async (builder) => {
161
+ logging.debug("Loading unit from content");
162
+ const unit = builder.AdaptEvaluationUnit.LoadFromContents(unit_contents);
163
+ logging.debug("Building packet with seed phrase:", packet_config.seed_phrase);
164
+ const seed_phrase = packet_config.seed_phrase ? packet_config.seed_phrase : AdaptEnvironment.GetRandomBytes(builder.lifetime, 16).Visualize();
165
+ const entropy = builder.AdaptEnvironment.GetRandomBytes(256);
166
+ const init_arg = packet_config.init_arg ? builder.Attach(object_to_adapt_value(JSON.parse(packet_config.init_arg))) : undefined;
167
+ let packet = _packet ? builder.Attach(_packet) : builder.AdaptEnvironment.CreatePacket(unit, "Seed phrase: " + seed_phrase, AdaptNetworkComponentConfigurator.test_mode ? "" : entropy.Visualize().substring(2), false, init_arg).Detach();
168
+ logging.info("Packet created with id:", AdaptObjectBuilder.execute_and_collect_garbage(b => b.Attach(packet.GetContainerID()).Visualize()));
169
+ let messages_adapt_values = [];
170
+ if (packet_config instanceof PacketWrapperConfigurator) {
171
+ packet_config.transactions.forEach(({ name, targ }) => {
172
+ const targ_mufl = packet.ParseValueFromJSON(targ);
173
+ const message = object_to_adapt_value({ name: name, targ: targ_mufl });
174
+ messages_adapt_values.push(message);
175
+ });
176
+ }
177
+ let wrapper = new AdaptPacketWrapper(packet, packet_config, this.#wrapper, messages_adapt_values);
178
+ this.add_packet(AdaptObjectBuilder.execute_and_collect_garbage(b => b.Attach(wrapper.packet.GetContainerID()).Visualize()), wrapper);
179
+ if (packet_config instanceof PacketWrapperConfigurator)
180
+ this.#wrapper.on_packet_created(AdaptObjectBuilder.execute_and_collect_garbage(b => b.Attach(wrapper.packet.GetContainerID()).Visualize()), wrapper);
181
+ else {
182
+ if (!this.#wrapper.control_packet) {
183
+ logging.error("Control packet is undefined.");
184
+ return;
185
+ }
186
+ this.#wrapper.control_packet.packet = wrapper;
187
+ this.#wrapper.on_control_packet_created(wrapper.packet.GetContainerID().Visualize(), wrapper);
188
+ }
189
+ on_packet_created(wrapper);
190
+ });
191
+ };
192
+ create_packet_cb();
193
+ if (!_packet && packet_config instanceof PacketWrapperConfigurator && packet_config.replicas > 1) {
194
+ logging.debug("Creating more replicas of the packet: ", packet_config.replicas);
195
+ packet_config.seed_phrase = undefined;
196
+ for (let i = 1; i < packet_config.replicas; i++) {
197
+ create_packet_cb();
198
+ }
199
+ }
200
+ };
201
+ }
202
+ AdaptWrapperServices.PacketManager = PacketManager;
203
+ class BackupManager {
204
+ #wrapper;
205
+ #requested_backups = new Map();
206
+ #request_backup_data_from_storage;
207
+ constructor(wrapper, request_backup_data_from_storage) {
208
+ this.#wrapper = wrapper;
209
+ this.#request_backup_data_from_storage = request_backup_data_from_storage;
210
+ }
211
+ request_backup = (packet_info, on_backup) => {
212
+ const message_hash = this.#request_backup_data_from_storage(packet_info);
213
+ this.#requested_backups.set(message_hash, on_backup);
214
+ };
215
+ on_backup_data = (message_hash, data) => {
216
+ if (!this.#requested_backups.has(message_hash))
217
+ throw new Error("Internal error: unrecognized message hash");
218
+ this.#requested_backups.get(message_hash)(data);
219
+ this.#requested_backups.delete(message_hash);
220
+ };
221
+ back_up_packet = (packet_config, on_packet_backed_up, backup_data_message) => {
222
+ AdaptObjectBuilder.execute_and_collect_garbage(builder => {
223
+ const message_body = builder.Attach(Protocol.GetBody(backup_data_message));
224
+ const packet_data = message_body.Reduce(Protocol.FieldName.packet_data);
225
+ const transaction_chain = message_body.Reduce(Protocol.FieldName.transaction_chain).Detach();
226
+ if (packet_data.GetBinary().length === 0) {
227
+ logging.info("Backup data for this packet not found.");
228
+ if (packet_config.attempt_backup) {
229
+ logging.info("Attempt backup option is set. Creating a new packet.");
230
+ this.#wrapper.create_packet(packet_config);
231
+ return;
232
+ }
233
+ else {
234
+ logging.error("Failed to back up the packet.");
235
+ return;
236
+ }
237
+ }
238
+ let packet = builder.AdaptPacketContext.LoadFromContents(packet_data.GetBinary()).Detach();
239
+ logging.info("Packet backup up: ", packet.GetContainerID().Visualize());
240
+ logging.info("Packet code id: ", packet.GetCodeID().Visualize());
241
+ const unit_hash = packet.GetCodeID();
242
+ this.#wrapper.packet_manager.request_unit(unit_hash.Visualize().substring(2), unit_contents => {
243
+ AdaptObjectBuilder.execute_and_collect_garbage(builder => {
244
+ builder.Attach(transaction_chain);
245
+ builder.AdaptEvaluationUnit.LoadFromContents(unit_contents);
246
+ let idx = 0;
247
+ let transaction;
248
+ while (!(transaction = transaction_chain.Reduce(idx++)).IsNil()) {
249
+ const request_data = packet.ExecuteTransaction(builder.Attach(object_to_adapt_value({
250
+ name: "::key_storage_api::decrypt_message",
251
+ targ: transaction.Reduce(Protocol.FieldName.request_data_encrypted)
252
+ })));
253
+ const transient_data = packet.ExecuteTransaction(builder.Attach(object_to_adapt_value({
254
+ name: "::key_storage_api::decrypt_message",
255
+ targ: transaction.Reduce(Protocol.FieldName.transient_data_encrypted)
256
+ })));
257
+ const trn_result = packet.ExecuteTransaction(request_data, transient_data.Reduce(Protocol.FieldName.entropy).Visualize(), transient_data.Reduce(Protocol.FieldName.timestamp));
258
+ packet.Destroy();
259
+ packet = trn_result.GetPacket();
260
+ }
261
+ });
262
+ let messages_adapt_values = [];
263
+ packet_config.transactions.forEach(({ name, targ }) => {
264
+ const targ_mufl = packet.ParseValueFromJSON(targ);
265
+ const message = object_to_adapt_value({ name: name, args: targ_mufl });
266
+ messages_adapt_values.push(message);
267
+ });
268
+ let wrapper = new AdaptPacketWrapper(packet, packet_config, this.#wrapper, messages_adapt_values, true);
269
+ this.#wrapper.packet_manager.add_packet(wrapper.packet.GetContainerID().Visualize(), wrapper);
270
+ logging.info("Packet backed up. ID: ", wrapper.packet.GetContainerID().Visualize());
271
+ logging.info("State id: ", wrapper.packet.GetHash().Visualize().substring(2));
272
+ this.#wrapper.on_packet_created(AdaptObjectBuilder.execute_and_collect_garbage(b => b.Attach(wrapper.packet.GetContainerID()).Visualize()), wrapper);
273
+ on_packet_backed_up(wrapper);
274
+ });
275
+ });
276
+ };
277
+ }
278
+ AdaptWrapperServices.BackupManager = BackupManager;
279
+ class BrokerAPI_impl {
280
+ #wrapper;
281
+ #cb;
282
+ #is_ready = false;
283
+ #pending_messages = [];
284
+ #broker_client;
285
+ // config: AdaptWrapperConfigurator
286
+ constructor(wrapper) {
287
+ logging.debug("Creating broker api service");
288
+ this.#wrapper = wrapper;
289
+ this.#broker_client = new BrokerClient(wrapper.config.broker_address, this);
290
+ }
291
+ start = () => {
292
+ this.#broker_client.start();
293
+ };
294
+ get is_ready() {
295
+ return this.#is_ready;
296
+ }
297
+ receive_from_broker = (message) => {
298
+ logging.message_received("broker_client", "adapt_wrapper", message);
299
+ this.#wrapper.receive_message(message);
300
+ };
301
+ register_callback_broker = (cb) => {
302
+ if (this.#cb) {
303
+ logging.error("Attempting to set the broker callback twice.");
304
+ return;
305
+ }
306
+ this.#cb = cb;
307
+ };
308
+ broker_client_ready = () => {
309
+ this.#is_ready = true;
310
+ const pending = this.#pending_messages;
311
+ this.#pending_messages = [];
312
+ pending.forEach(msg => this.send_to_broker(msg));
313
+ };
314
+ broker_connection_lost = () => {
315
+ logging.debug("Broker connection lost. Buffering outgoing broker messages until reconnect.");
316
+ this.#is_ready = false;
317
+ };
318
+ collect_registration_messages = (nonce) => {
319
+ const registrations = [];
320
+ this.#wrapper.packet_manager.packets.forEach(packet => {
321
+ try {
322
+ registrations.push(packet.create_registration_message(nonce));
323
+ }
324
+ catch (e) {
325
+ logging.error("Failed to create registration message for packet ", packet.container_id, ": ", e instanceof Error ? e.message : e);
326
+ }
327
+ });
328
+ if (registrations.length > 0)
329
+ logging.debug("Re-registering ", registrations.length, " packet(s) with the broker");
330
+ return registrations;
331
+ };
332
+ create_registration_message_for = (container_id, nonce) => {
333
+ const packet = this.#wrapper.packet_manager.packets.get(container_id);
334
+ if (!packet) {
335
+ logging.error("Cannot register unknown packet with broker: ", container_id);
336
+ return undefined;
337
+ }
338
+ try {
339
+ return packet.create_registration_message(nonce);
340
+ }
341
+ catch (e) {
342
+ logging.error("Failed to create registration message for packet ", container_id, ": ", e instanceof Error ? e.message : e);
343
+ return undefined;
344
+ }
345
+ };
346
+ register_packet = (container_id) => {
347
+ this.#broker_client.register_packet(container_id);
348
+ };
349
+ send_to_broker = (message) => {
350
+ if (!this.#cb) {
351
+ logging.error("Callback to send data to broker is not registered.");
352
+ message.Destroy(); // drop-with-free: re-registration recreates it; don't leak the message
353
+ return;
354
+ }
355
+ if (!this.#is_ready) {
356
+ this.#pending_messages.push(message);
357
+ logging.debug("Message added to the list of pending messages: ", new LazyMessage(() => message.Visualize()));
358
+ return;
359
+ }
360
+ logging.message_sent("adapt_wrapper", 'broker_client', message);
361
+ this.#cb(message);
362
+ };
363
+ extract_pending_transactions_for = (container_id) => {
364
+ const extracted = [];
365
+ this.#pending_messages = this.#pending_messages.filter(message => {
366
+ if (Protocol.GetTypeId(message) !== Protocol.MessageName.w2b_transaction)
367
+ return true;
368
+ const to = AdaptObjectBuilder.execute_and_collect_garbage(builder => builder.Attach(Protocol.GetTo(message)).Visualize());
369
+ if (to !== container_id)
370
+ return true;
371
+ extracted.push(message);
372
+ return false;
373
+ });
374
+ return extracted;
375
+ };
376
+ }
377
+ AdaptWrapperServices.BrokerAPI_impl = BrokerAPI_impl;
378
+ class StorageAPI_impl {
379
+ #wrapper;
380
+ #cb;
381
+ #is_ready = false;
382
+ #pending_messages = [];
383
+ #storage_client;
384
+ constructor(wrapper) {
385
+ this.#wrapper = wrapper;
386
+ if (wrapper.config.env === 'enclave') {
387
+ this.#storage_client = new StorageClient(wrapper.config.storage_address, this);
388
+ }
389
+ }
390
+ start() {
391
+ if (this.#wrapper.config.env === 'enclave')
392
+ this.#storage_client?.start();
393
+ }
394
+ get is_ready() {
395
+ return this.#is_ready;
396
+ }
397
+ receive_from_data_storage = (message) => {
398
+ logging.message_received("storage_client", "adapt_wrapper", message);
399
+ this.#wrapper.receive_message(message);
400
+ };
401
+ register_callback_data_storage = (cb) => {
402
+ if (this.#cb) {
403
+ logging.error("Attempting to set the storage callback twice.");
404
+ return;
405
+ }
406
+ this.#cb = cb;
407
+ };
408
+ storage_client_ready = () => {
409
+ this.#is_ready = true;
410
+ const pending = this.#pending_messages;
411
+ this.#pending_messages = [];
412
+ pending.forEach(msg => this.send_to_storage(msg));
413
+ };
414
+ storage_connection_lost = () => {
415
+ logging.debug("Storage connection lost. Buffering outgoing storage messages until reconnect.");
416
+ this.#is_ready = false;
417
+ };
418
+ send_to_storage = (message) => {
419
+ if (!this.#cb) {
420
+ logging.error("Callback to send data to storage is not regisered.");
421
+ message.Destroy(); // drop-with-free: don't leak the message
422
+ return;
423
+ }
424
+ if (!this.#is_ready) {
425
+ this.#pending_messages.push(message);
426
+ logging.debug("Message added to the list of pending messages: ", new LazyMessage(() => message.Visualize()));
427
+ return;
428
+ }
429
+ logging.message_sent("adapt_wrapper", "storage_client", message);
430
+ this.#cb(message);
431
+ };
432
+ }
433
+ AdaptWrapperServices.StorageAPI_impl = StorageAPI_impl;
434
+ class MessageProcessor {
435
+ #wrapper;
436
+ #queue;
437
+ #send_to_broker;
438
+ #send_to_storage;
439
+ #local_ip_documents_exchanged = new Map();
440
+ constructor(wrapper, send_to_broker, send_to_storage) {
441
+ this.#wrapper = wrapper;
442
+ this.#send_to_broker = send_to_broker;
443
+ this.#send_to_storage = send_to_storage;
444
+ this.#queue = new AsyncQueueAdapter(this.#worker.bind(this));
445
+ this.#queue.add_worker_functions([
446
+ { type_id: Protocol.MessageName.b2w_init, func: this.b2w_init_handler.bind(this) },
447
+ { type_id: Protocol.MessageName.b2w_exec_transaction, func: this.b2w_exec_transaction_handler.bind(this) },
448
+ { type_id: Protocol.MessageName.b2w_reg_ok, func: this.b2w_reg_ok_handler.bind(this) },
449
+ { type_id: Protocol.MessageName.d2w_backup_data, func: this.d2w_backup_data_handler.bind(this) },
450
+ { type_id: Protocol.MessageName.d2w_create_packet_confirm, func: this.d2w_create_packet_confirm_handler.bind(this) },
451
+ { type_id: Protocol.MessageName.d2w_init, func: this.d2w_init_handler.bind(this) },
452
+ { type_id: Protocol.MessageName.d2w_code_object, func: this.d2w_code_object_handler.bind(this) },
453
+ { type_id: Protocol.MessageName.d2w_last_snapshot_state, func: this.d2w_last_snapshot_state_handler.bind(this) },
454
+ { type_id: Protocol.MessageName.d2w_last_state, func: this.d2w_last_state_handler.bind(this) },
455
+ { type_id: Protocol.MessageName.d2w_snapshot, func: this.d2w_snapshot_handler.bind(this) },
456
+ { type_id: Protocol.MessageName.d2w_transaction_chain, func: this.d2w_transaction_chain_handler.bind(this) },
457
+ { type_id: Protocol.MessageName.d2w_write_snapshot_confirm, func: this.d2w_write_snapshot_confirm_handler.bind(this) },
458
+ { type_id: Protocol.MessageName.d2w_write_transaction_confirm, func: this.d2w_write_transaction_confirm_handler.bind(this) },
459
+ ]);
460
+ }
461
+ push(event) {
462
+ this.#queue.push(event);
463
+ }
464
+ #worker = (event, cb) => {
465
+ try {
466
+ if (event.kind === 'incoming') {
467
+ logging.debug("Message processed in queue (incoming): ", new LazyMessage(() => event.data.Visualize()));
468
+ this.#incoming_message_handler(event.data);
469
+ }
470
+ else if (event.kind === 'broker_out') {
471
+ logging.debug("Message processed in queue (broker_out): ", new LazyMessage(() => event.data.Visualize()));
472
+ this.#send_to_broker(event.data);
473
+ }
474
+ else if (event.kind === 'data_storage_out') {
475
+ logging.debug("Message processed in queue (storage_out): ", new LazyMessage(() => event.data.Visualize()));
476
+ this.#send_to_storage(event.data);
477
+ }
478
+ }
479
+ catch (e) {
480
+ logging.error(e);
481
+ }
482
+ finally {
483
+ cb();
484
+ }
485
+ };
486
+ #incoming_message_handler = (message) => {
487
+ AdaptObjectBuilder.execute_and_collect_garbage(builder => {
488
+ builder.Attach(message); // the message is attached here, so it does not need to be attached in the
489
+ try {
490
+ const type_id = Protocol.GetTypeId(message);
491
+ if (type_id[2] === 'd') { // message to data storage
492
+ this.#wrapper.send_to_storage(message.Detach());
493
+ return;
494
+ }
495
+ if (type_id[2] === 'b') { // message to broker
496
+ if (type_id === Protocol.MessageName.w2b_transaction && this.#deliver_locally(message))
497
+ return;
498
+ this.#wrapper.send_to_broker(message.Detach());
499
+ return;
500
+ }
501
+ const func = this.#queue.get_function(type_id);
502
+ if (func)
503
+ func(message);
504
+ else
505
+ logging.error("Adapt wrapper received unrecognized message: ", type_id);
506
+ }
507
+ catch (e) {
508
+ logging.error(e);
509
+ }
510
+ });
511
+ };
512
+ /**
513
+ * If the destination packet of an outgoing w2b_transaction is managed by this wrapper,
514
+ * deliver the message directly to its queue instead of resending it through the broker.
515
+ * Mirrors the broker's relay: flips the message type to b2w_exec_transaction in place
516
+ * and forwards it unchanged (IPD attachment dropped — see the transport rewrite).
517
+ */
518
+ #deliver_locally = (message) => {
519
+ return AdaptObjectBuilder.execute_and_collect_garbage(builder => {
520
+ const to = builder.Attach(Protocol.GetTo(message)).Visualize();
521
+ if (!this.#wrapper.packet_manager.packets.has(to))
522
+ return false;
523
+ const from = builder.Attach(Protocol.GetFrom(message)).Visualize();
524
+ if (!this.#wrapper.packet_manager.packets.get(from))
525
+ return false; // sender is not managed by this wrapper, let the broker handle the message
526
+ const local_message = message.Mutate(Protocol.FieldName.msg_type_id, builder.AdaptValue.New(Protocol.MessageName.b2w_exec_transaction)).Detach();
527
+ logging.debug("Destination packet ", to, " is managed by this wrapper. Delivering the message locally, bypassing the broker.");
528
+ this.#wrapper.send_to_packet_wrapper(local_message);
529
+ return true;
530
+ });
531
+ };
532
+ #pass_to_adapt_packet_wrapper = (message) => {
533
+ logging.debug("Passing the message to the packet wrapper: ", new LazyMessage(() => message.Visualize()));
534
+ this.#wrapper.send_to_packet_wrapper(message.Detach());
535
+ };
536
+ b2w_exec_transaction_handler = this.#pass_to_adapt_packet_wrapper;
537
+ b2w_init_handler = (message) => {
538
+ logging.error("Internal error: This message is not expected here: ", message.Visualize());
539
+ };
540
+ b2w_reg_ok_handler = this.#pass_to_adapt_packet_wrapper;
541
+ d2w_backup_data_handler = (message) => {
542
+ AdaptObjectBuilder.execute_and_collect_garbage(builder => {
543
+ const message_hash = builder.Attach(Protocol.GetMessageHash(message));
544
+ this.#wrapper.backup_manager.on_backup_data(message_hash.Visualize(), message);
545
+ });
546
+ };
547
+ d2w_code_object_handler = (message) => {
548
+ AdaptObjectBuilder.execute_and_collect_garbage(builder => {
549
+ const body = builder.Attach(Protocol.GetBody(message));
550
+ const unit_contents = body.Reduce(Protocol.FieldName.code_object).Reduce(Protocol.FieldName.code_object_data).GetBinary();
551
+ const unit_hash = body.Reduce(Protocol.FieldName.code_object).Reduce(Protocol.FieldName.code_object_hash).Visualize().substring(2);
552
+ this.#wrapper.packet_manager.add_unit(unit_hash, unit_contents);
553
+ });
554
+ };
555
+ d2w_create_packet_confirm_handler = this.#pass_to_adapt_packet_wrapper;
556
+ d2w_init_handler = (message) => {
557
+ logging.error("Internal error: This message is not expected here: ", message.Visualize());
558
+ };
559
+ d2w_last_snapshot_state_handler = this.#pass_to_adapt_packet_wrapper;
560
+ d2w_last_state_handler = this.#pass_to_adapt_packet_wrapper;
561
+ d2w_snapshot_handler = this.#pass_to_adapt_packet_wrapper;
562
+ d2w_transaction_chain_handler = this.#pass_to_adapt_packet_wrapper;
563
+ d2w_write_snapshot_confirm_handler = this.#pass_to_adapt_packet_wrapper;
564
+ d2w_write_transaction_confirm_handler = this.#pass_to_adapt_packet_wrapper;
565
+ }
566
+ AdaptWrapperServices.MessageProcessor = MessageProcessor;
567
+ class Helpers {
568
+ #wrapper;
569
+ constructor(wrapper) {
570
+ this.#wrapper = wrapper;
571
+ }
572
+ request_unit_from_storage = (unit_hash) => {
573
+ AdaptObjectBuilder.execute_and_collect_garbage(builder => {
574
+ logging.debug('Unit hash is ', unit_hash);
575
+ const emptyPacket = AdaptEnvironment.EmptyPacket(undefined, false);
576
+ const unit_hash_adapt_value = builder.Attach(emptyPacket.ExecuteFunction("_binary_to_hash_code", [emptyPacket.ExecuteFunction('_hex_string_to_binary', [unit_hash])]));
577
+ // const unit_hash_adapt_value = builder.Attach(
578
+ // Protocol.GetProtocolContainer().ExecuteTransaction(
579
+ // builder.Attach(object_to_adapt_value({
580
+ // name: "::protocol_container::primitives_wrapper::binary_to_hash_code",
581
+ // targ: builder.Attach(Protocol.GetProtocolContainer().ExecuteTransaction(
582
+ // builder.Attach(object_to_adapt_value({
583
+ // name: "::protocol_container::primitives_wrapper::hex_string_to_binary",
584
+ // targ: unit_hash
585
+ // }))
586
+ // ))
587
+ //
588
+ // }))
589
+ // )
590
+ // );
591
+ const args = builder.Attach(Protocol.GetProtocolContainer().CreateDictionary().Mutate(Protocol.FieldName.code_object_hash, unit_hash_adapt_value));
592
+ const message = Protocol.CreateProtocolMessage(Protocol.MessageName.w2d_get_code_object, { body: args }).Detach();
593
+ logging.debug("Setting new packet with message hash:", new LazyMessage(() => message.GetHash().Visualize()));
594
+ this.#wrapper.send_to_storage(message);
595
+ });
596
+ };
597
+ request_backup_data_from_storage = ({ packet_id, state }) => {
598
+ if (!packet_id && !state)
599
+ throw new Error("Internal error: both packet id and state hash are undefined while trying to request packet backup data.");
600
+ if (state)
601
+ logging.debug("Attempting to back up a packet to a state: ", state);
602
+ else if (packet_id)
603
+ logging.debug("Attempting to back up a packet to the most recent state. Packet id: ", packet_id);
604
+ return AdaptObjectBuilder.execute_and_collect_garbage(builder => {
605
+ const packet = builder.AdaptPacketContext.EmptyPacket(false);
606
+ if (state) {
607
+ logging.debug("State is ", state);
608
+ }
609
+ const emptyPacket = AdaptEnvironment.EmptyPacket(undefined, false);
610
+ const stateAdaptValue = state ? builder.Attach(emptyPacket.ExecuteFunction('_binary_to_hash_code', [builder.Attach(emptyPacket.ExecuteFunction('_hex_string_to_binary', [state]))])) : undefined;
611
+ // const stateAdaptValue = state ? builder.Attach(
612
+ // Protocol.GetProtocolContainer().ExecuteTransaction(
613
+ // builder.Attach(object_to_adapt_value({
614
+ // name: "::protocol_container::primitives_wrapper::binary_to_hash_code",
615
+ // targ: builder.Attach(Protocol.GetProtocolContainer().ExecuteTransaction(
616
+ // builder.Attach(object_to_adapt_value({
617
+ // name: "::protocol_container::primitives_wrapper::hex_string_to_binary",
618
+ // targ: state
619
+ // }))
620
+ // ))
621
+ // }))
622
+ // )
623
+ // ) : undefined;
624
+ // create backup data request to the data storage
625
+ const message = Protocol.CreateProtocolMessage(Protocol.MessageName.w2d_request_backup_data, {
626
+ body: packet.CreateDictionary()
627
+ .Mutate(Protocol.FieldName.packet_id, packet_id ? packet_id : packet.Nil())
628
+ .Mutate(Protocol.FieldName.state, stateAdaptValue ? stateAdaptValue : packet.Nil())
629
+ });
630
+ const message_hash = message.GetHash().Visualize();
631
+ this.#wrapper.send_to_storage(message);
632
+ return message_hash;
633
+ });
634
+ };
635
+ }
636
+ AdaptWrapperServices.Helpers = Helpers;
637
+ class ControlPacket {
638
+ #wrapper;
639
+ packet;
640
+ constructor(wrapper, config) {
641
+ this.#wrapper = wrapper;
642
+ const on_unit_data = this.#wrapper.packet_manager.create_packet.bind(this, config, () => { });
643
+ if (config.load_from_storage) {
644
+ this.#wrapper.packet_manager.request_unit(config.unit_hash, on_unit_data);
645
+ }
646
+ else {
647
+ const control_packet_unit_dir = config.control_packet_dir_path;
648
+ const control_packet_path = path.join(control_packet_unit_dir, config.unit_hash + ".muflo").normalize();
649
+ this.#wrapper.packet_manager.request_unit_local(config.unit_hash, control_packet_path, on_unit_data);
650
+ }
651
+ }
652
+ }
653
+ AdaptWrapperServices.ControlPacket = ControlPacket;
654
+ })(AdaptWrapperServices || (AdaptWrapperServices = {}));
655
+ export class AdaptWrapper {
656
+ config;
657
+ #broker_service;
658
+ get is_broker_ready() {
659
+ return this.#broker_service.is_ready;
660
+ }
661
+ #send_to_broker;
662
+ #storage_service;
663
+ get is_storage_ready() {
664
+ return this.#storage_service.is_ready;
665
+ }
666
+ get storage_available() {
667
+ return this.config.env === 'enclave';
668
+ }
669
+ #send_to_storage;
670
+ #helpers;
671
+ #request_unit_from_storage;
672
+ #request_backup_data_from_storage;
673
+ packet_manager;
674
+ backup_manager;
675
+ #message_processor;
676
+ control_packet;
677
+ constructor(config) {
678
+ this.config = config;
679
+ logging.info("Calling constructor of adapt wrapper");
680
+ this.#broker_service = new AdaptWrapperServices.BrokerAPI_impl(this);
681
+ this.#send_to_broker = this.#broker_service.send_to_broker.bind(this.#broker_service);
682
+ this.#storage_service = new AdaptWrapperServices.StorageAPI_impl(this);
683
+ this.#send_to_storage = this.#storage_service.send_to_storage.bind(this.#storage_service);
684
+ this.#helpers = new AdaptWrapperServices.Helpers(this);
685
+ this.#request_unit_from_storage = this.#helpers.request_unit_from_storage.bind(this.#helpers);
686
+ this.#request_backup_data_from_storage = this.#helpers.request_backup_data_from_storage.bind(this.#helpers);
687
+ this.packet_manager = new AdaptWrapperServices.PacketManager(this, this.#request_unit_from_storage.bind(this));
688
+ this.backup_manager = new AdaptWrapperServices.BackupManager(this, this.#request_backup_data_from_storage.bind(this));
689
+ this.#message_processor = new AdaptWrapperServices.MessageProcessor(this, this.#send_to_broker.bind(this), this.#send_to_storage.bind(this));
690
+ }
691
+ // =================================== public API ==============================================
692
+ receive_message(message) {
693
+ this.#message_processor.push({ kind: "incoming", data: message });
694
+ }
695
+ send_to_packet_wrapper(message) {
696
+ AdaptObjectBuilder.execute_and_collect_garbage(builder => {
697
+ const to = builder.Attach(Protocol.GetTo(message)).Visualize();
698
+ // if (this.control_packet.)
699
+ if (this.packet_manager.packets.has(to))
700
+ this.packet_manager.packets.get(to).on_message(message);
701
+ else {
702
+ logging.error("Packet with id ", to, " is not known.");
703
+ }
704
+ });
705
+ }
706
+ send_to_storage(message) {
707
+ this.#message_processor.push({ kind: "data_storage_out", data: message });
708
+ }
709
+ send_to_broker(message) {
710
+ this.#message_processor.push({ kind: "broker_out", data: message });
711
+ }
712
+ deliver_pending_messages_locally(container_id) {
713
+ this.#broker_service.extract_pending_transactions_for(container_id).forEach(message => this.receive_message(message));
714
+ this.packet_manager.packets.forEach(packet => packet.flush_pending_messages_for(container_id));
715
+ }
716
+ register_packet_with_broker(container_id) {
717
+ this.#broker_service.register_packet(container_id);
718
+ }
719
+ create_packet(config, on_packet_created = (() => { }), packet) {
720
+ this.packet_manager.request_unit(config.unit_hash, (contents) => this.packet_manager.create_packet(config, on_packet_created, contents, packet));
721
+ }
722
+ create_packet_ex(unit_hash, seed_phrase, on_packet_created = (() => { }), packet) {
723
+ let config = new PacketWrapperConfigurator();
724
+ config.unit_hash = unit_hash;
725
+ config.seed_phrase = seed_phrase;
726
+ this.create_packet(config, on_packet_created, packet);
727
+ }
728
+ create_packet_local(config, on_packet_created = (() => { })) {
729
+ let packet;
730
+ let unit_hash;
731
+ if (config.state_hash) {
732
+ const state_hash = config.state_hash;
733
+ const packet_path = config.packet_dir_path + "/" + state_hash + ".muflp";
734
+ try {
735
+ packet = AdaptPacketContext.LoadFromFile(new AdaptObjectLifetime(), packet_path).Detach();
736
+ }
737
+ catch (e) {
738
+ throw new AdaptInitializationError("Failed to load a packet from file: " + e);
739
+ }
740
+ unit_hash = packet.GetCodeID().Visualize().substring(2);
741
+ if (config.unit_hash !== unit_hash)
742
+ throw new AdaptInitializationError("Provided packet and code do not match.");
743
+ }
744
+ else {
745
+ if (!config.unit_hash)
746
+ throw new AdaptInitializationError("Unit hash is not provided.");
747
+ unit_hash = config.unit_hash;
748
+ }
749
+ this.packet_manager.request_unit_local(unit_hash, config.unit_dir_path + "/" + unit_hash + ".muflo", content => {
750
+ return this.packet_manager.create_packet(config, on_packet_created, content, packet);
751
+ });
752
+ }
753
+ back_up_packet(config, on_packet_backed_up = (() => { })) {
754
+ const state_hash = config.state_hash;
755
+ const packet_id = config.packet_id;
756
+ this.backup_manager.request_backup({ packet_id: packet_id, state: state_hash }, this.backup_manager.back_up_packet.bind(this, config, on_packet_backed_up));
757
+ }
758
+ back_up_packet_ex({ packet_id, state }, on_packet_backed_up = (() => { })) {
759
+ let config = new PacketWrapperConfigurator();
760
+ config.packet_id = packet_id;
761
+ config.state_hash = state;
762
+ this.back_up_packet(config, on_packet_backed_up);
763
+ }
764
+ remove_packet(packet_id) {
765
+ this.packet_manager.remove_packet(packet_id);
766
+ }
767
+ #on_container_created_cb;
768
+ #on_control_packet_created_cbs = [];
769
+ set on_packet_created_cb(cb) {
770
+ console.log("Packet created cb is set");
771
+ this.#on_container_created_cb = cb;
772
+ }
773
+ set on_control_packet_created_cb(cb) {
774
+ this.#on_control_packet_created_cbs.push(cb);
775
+ }
776
+ on_packet_created(cid, wrapper) {
777
+ if (this.#on_container_created_cb)
778
+ this.#on_container_created_cb(cid, wrapper);
779
+ }
780
+ on_control_packet_created(cid, wrapper) {
781
+ this.#on_control_packet_created_cbs.forEach(cb => cb(cid, wrapper));
782
+ }
783
+ add_packet(unit_hash, seed_phrase, on_packet_created = (() => { }), packet) {
784
+ logging.debug("Adding packet");
785
+ return this.create_packet_ex(unit_hash, seed_phrase, on_packet_created, packet);
786
+ }
787
+ add_packet_local(on_packet_created, unit, packet) {
788
+ logging.debug("add_packet_local");
789
+ this.packet_manager.add_packet_local(new PacketWrapperConfigurator(), on_packet_created, unit, packet);
790
+ }
791
+ add_unit(unit_hash, unit_data) {
792
+ logging.debug("Adding unit: ", unit_hash);
793
+ this.packet_manager.add_unit_external(unit_hash, unit_data);
794
+ }
795
+ start() {
796
+ this.#broker_service.start();
797
+ this.#storage_service.start();
798
+ if (this.config.env === 'enclave') {
799
+ this.on_control_packet_created_cb = () => {
800
+ this.packet_manager.init(this.config.packets);
801
+ };
802
+ if (!this.config.control_packet)
803
+ throw new AdaptInitializationError("Control packet configuration is not provided in nitro enclave environment.");
804
+ this.control_packet = new AdaptWrapperServices.ControlPacket(this, this.config.control_packet);
805
+ }
806
+ else {
807
+ this.packet_manager.init(this.config.packets);
808
+ }
809
+ }
810
+ }
811
+ //# sourceMappingURL=adapt_wrapper.js.map