librats 1.0.2 → 2.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (319) hide show
  1. package/README.md +145 -331
  2. package/binding.gyp +16 -3
  3. package/lib/index.d.ts +288 -696
  4. package/lib/index.js +407 -44
  5. package/native-src/3rdparty/android/ifaddrs-android.c +1 -0
  6. package/native-src/3rdparty/android/ifaddrs-android.h +1 -0
  7. package/native-src/CMakeLists.txt +404 -179
  8. package/native-src/LICENSE +1 -1
  9. package/native-src/src/librats/bindings/rats.cpp +762 -0
  10. package/native-src/src/librats/bindings/rats.h +380 -0
  11. package/native-src/src/librats/bittorrent/bencode.cpp +437 -0
  12. package/native-src/src/librats/bittorrent/bencode.h +176 -0
  13. package/native-src/src/librats/bittorrent/bitfield.cpp +97 -0
  14. package/native-src/src/librats/bittorrent/bitfield.h +76 -0
  15. package/native-src/src/librats/bittorrent/byte_io.h +58 -0
  16. package/native-src/src/librats/bittorrent/choker.cpp +25 -0
  17. package/native-src/src/librats/bittorrent/choker.h +46 -0
  18. package/native-src/src/librats/bittorrent/client.cpp +413 -0
  19. package/native-src/src/librats/bittorrent/client.h +227 -0
  20. package/native-src/src/librats/bittorrent/disk_io.cpp +209 -0
  21. package/native-src/src/librats/bittorrent/disk_io.h +150 -0
  22. package/native-src/src/librats/bittorrent/extensions.cpp +191 -0
  23. package/native-src/src/librats/bittorrent/extensions.h +94 -0
  24. package/native-src/src/librats/bittorrent/file_storage.cpp +77 -0
  25. package/native-src/src/librats/bittorrent/file_storage.h +79 -0
  26. package/native-src/src/librats/bittorrent/log.h +42 -0
  27. package/native-src/src/librats/bittorrent/magnet_uri.cpp +98 -0
  28. package/native-src/src/librats/bittorrent/magnet_uri.h +35 -0
  29. package/native-src/src/librats/bittorrent/peer_connection.cpp +502 -0
  30. package/native-src/src/librats/bittorrent/peer_connection.h +194 -0
  31. package/native-src/src/librats/bittorrent/peer_list.cpp +68 -0
  32. package/native-src/src/librats/bittorrent/peer_list.h +75 -0
  33. package/native-src/src/librats/bittorrent/piece_picker.cpp +352 -0
  34. package/native-src/src/librats/bittorrent/piece_picker.h +201 -0
  35. package/native-src/src/librats/bittorrent/reactor.cpp +97 -0
  36. package/native-src/src/librats/bittorrent/reactor.h +89 -0
  37. package/native-src/src/librats/bittorrent/resume_data.cpp +72 -0
  38. package/native-src/src/librats/bittorrent/resume_data.h +41 -0
  39. package/native-src/src/librats/bittorrent/store_buffer.cpp +48 -0
  40. package/native-src/src/librats/bittorrent/store_buffer.h +47 -0
  41. package/native-src/src/librats/bittorrent/torrent.cpp +870 -0
  42. package/native-src/src/librats/bittorrent/torrent.h +260 -0
  43. package/native-src/src/librats/bittorrent/torrent_creator.cpp +129 -0
  44. package/native-src/src/librats/bittorrent/torrent_creator.h +58 -0
  45. package/native-src/src/librats/bittorrent/torrent_info.cpp +314 -0
  46. package/native-src/src/librats/bittorrent/torrent_info.h +118 -0
  47. package/native-src/src/librats/bittorrent/tracker.cpp +374 -0
  48. package/native-src/src/librats/bittorrent/tracker.h +108 -0
  49. package/native-src/src/librats/bittorrent/types.cpp +206 -0
  50. package/native-src/src/librats/bittorrent/types.h +86 -0
  51. package/native-src/src/librats/core/address.cpp +35 -0
  52. package/native-src/src/librats/core/address.h +78 -0
  53. package/native-src/src/librats/core/bytes.h +69 -0
  54. package/native-src/src/librats/core/chained_send_buffer.cpp +172 -0
  55. package/native-src/src/librats/core/chained_send_buffer.h +183 -0
  56. package/native-src/src/librats/core/endpoint_parse.cpp +41 -0
  57. package/native-src/src/librats/core/endpoint_parse.h +31 -0
  58. package/native-src/src/librats/core/event_bus.h +70 -0
  59. package/native-src/src/librats/core/host_endpoint.h +56 -0
  60. package/native-src/src/{io_poller.cpp → librats/core/io_poller.cpp} +520 -65
  61. package/native-src/src/{io_poller.h → librats/core/io_poller.h} +12 -6
  62. package/native-src/src/librats/core/ip_address.cpp +120 -0
  63. package/native-src/src/librats/core/ip_address.h +109 -0
  64. package/native-src/src/librats/core/mpsc_queue.h +47 -0
  65. package/native-src/src/librats/core/notifier.h +74 -0
  66. package/native-src/src/librats/core/receive_buffer.cpp +219 -0
  67. package/native-src/src/librats/core/receive_buffer.h +171 -0
  68. package/native-src/src/librats/core/service_registry.h +58 -0
  69. package/native-src/src/{socket.cpp → librats/core/socket.cpp} +625 -118
  70. package/native-src/src/librats/core/socket.h +496 -0
  71. package/native-src/src/librats/core/timer_queue.h +105 -0
  72. package/native-src/src/librats/core/types.cpp +43 -0
  73. package/native-src/src/librats/core/types.h +103 -0
  74. package/native-src/src/librats/core/wakeup_pipe.h +83 -0
  75. package/native-src/src/{crypto → librats/crypto}/blake2_endian.h +21 -23
  76. package/native-src/src/{crypto → librats/crypto}/blake2b.c +34 -33
  77. package/native-src/src/{crypto → librats/crypto}/blake2b.h +7 -6
  78. package/native-src/src/{crypto → librats/crypto}/blake2s.c +55 -54
  79. package/native-src/src/{crypto → librats/crypto}/blake2s.h +13 -12
  80. package/native-src/src/{crypto → librats/crypto}/chacha.c +22 -21
  81. package/native-src/src/{crypto → librats/crypto}/chacha.h +14 -13
  82. package/native-src/src/{crypto → librats/crypto}/chachapoly.c +56 -56
  83. package/native-src/src/{crypto → librats/crypto}/chachapoly.h +24 -17
  84. package/native-src/src/{crc32.cpp → librats/crypto/crc32.cpp} +1 -1
  85. package/native-src/src/{crc32.h → librats/crypto/crc32.h} +3 -1
  86. package/native-src/src/{crypto → librats/crypto}/curve25519.c +6 -4
  87. package/native-src/src/{crypto → librats/crypto}/curve25519.h +6 -3
  88. package/native-src/src/librats/crypto/hkdf.c +266 -0
  89. package/native-src/src/{crypto → librats/crypto}/hkdf.h +19 -19
  90. package/native-src/src/{noise.cpp → librats/crypto/noise.cpp} +84 -73
  91. package/native-src/src/{noise.h → librats/crypto/noise.h} +18 -8
  92. package/native-src/src/{crypto → librats/crypto}/poly1305.c +47 -46
  93. package/native-src/src/librats/crypto/poly1305.h +37 -0
  94. package/native-src/src/{sha1.cpp → librats/crypto/sha1.cpp} +33 -1
  95. package/native-src/src/{sha1.h → librats/crypto/sha1.h} +14 -6
  96. package/native-src/src/{crypto → librats/crypto}/sha256.c +15 -14
  97. package/native-src/src/{crypto → librats/crypto}/sha256.h +8 -7
  98. package/native-src/src/{crypto → librats/crypto}/sha512.c +15 -14
  99. package/native-src/src/{crypto → librats/crypto}/sha512.h +8 -7
  100. package/native-src/src/librats/dht/announce.cpp +37 -0
  101. package/native-src/src/librats/dht/announce.h +41 -0
  102. package/native-src/src/librats/dht/bep42.cpp +109 -0
  103. package/native-src/src/librats/dht/bep42.h +48 -0
  104. package/native-src/src/librats/dht/dht.cpp +501 -0
  105. package/native-src/src/librats/dht/dht.h +119 -0
  106. package/native-src/src/librats/dht/dht_runner.cpp +103 -0
  107. package/native-src/src/librats/dht/dht_runner.h +71 -0
  108. package/native-src/src/librats/dht/dos_blocker.cpp +42 -0
  109. package/native-src/src/librats/dht/dos_blocker.h +47 -0
  110. package/native-src/src/librats/dht/find_peers.cpp +52 -0
  111. package/native-src/src/librats/dht/find_peers.h +73 -0
  112. package/native-src/src/librats/dht/id.h +167 -0
  113. package/native-src/src/{krpc.cpp → librats/dht/krpc.cpp} +32 -81
  114. package/native-src/src/{krpc.h → librats/dht/krpc.h} +19 -23
  115. package/native-src/src/librats/dht/log.h +38 -0
  116. package/native-src/src/librats/dht/node.cpp +473 -0
  117. package/native-src/src/librats/dht/node.h +164 -0
  118. package/native-src/src/librats/dht/node_entry.h +81 -0
  119. package/native-src/src/librats/dht/observer.h +72 -0
  120. package/native-src/src/librats/dht/persistence.cpp +90 -0
  121. package/native-src/src/librats/dht/persistence.h +32 -0
  122. package/native-src/src/librats/dht/routing_table.cpp +559 -0
  123. package/native-src/src/librats/dht/routing_table.h +185 -0
  124. package/native-src/src/librats/dht/rpc_manager.cpp +127 -0
  125. package/native-src/src/librats/dht/rpc_manager.h +77 -0
  126. package/native-src/src/librats/dht/storage.cpp +92 -0
  127. package/native-src/src/librats/dht/storage.h +74 -0
  128. package/native-src/src/librats/dht/transport.h +27 -0
  129. package/native-src/src/librats/dht/traversal.cpp +326 -0
  130. package/native-src/src/librats/dht/traversal.h +120 -0
  131. package/native-src/src/librats/dht/udp_transport.cpp +49 -0
  132. package/native-src/src/librats/dht/udp_transport.h +51 -0
  133. package/native-src/src/librats/mdns/log.h +22 -0
  134. package/native-src/src/{mdns.cpp → librats/mdns/mdns.cpp} +75 -40
  135. package/native-src/src/{mdns.h → librats/mdns/mdns.h} +9 -8
  136. package/native-src/src/{natpmp.cpp → librats/nat/natpmp.cpp} +12 -9
  137. package/native-src/src/{natpmp.h → librats/nat/natpmp.h} +3 -3
  138. package/native-src/src/{port_mapping.h → librats/nat/port_mapping.h} +3 -2
  139. package/native-src/src/{stun.cpp → librats/nat/stun.cpp} +4 -4
  140. package/native-src/src/{stun.h → librats/nat/stun.h} +1 -1
  141. package/native-src/src/{upnp.cpp → librats/nat/upnp.cpp} +6 -6
  142. package/native-src/src/{upnp.h → librats/nat/upnp.h} +2 -2
  143. package/native-src/src/librats/node/circuit_service.h +84 -0
  144. package/native-src/src/librats/node/config.h +110 -0
  145. package/native-src/src/librats/node/dial_service.h +54 -0
  146. package/native-src/src/librats/node/dialer.cpp +264 -0
  147. package/native-src/src/librats/node/dialer.h +188 -0
  148. package/native-src/src/librats/node/host_events.h +26 -0
  149. package/native-src/src/librats/node/identify.cpp +130 -0
  150. package/native-src/src/librats/node/identify.h +71 -0
  151. package/native-src/src/librats/node/nat_status.cpp +103 -0
  152. package/native-src/src/librats/node/nat_status.h +118 -0
  153. package/native-src/src/librats/node/node.cpp +865 -0
  154. package/native-src/src/librats/node/node.h +344 -0
  155. package/native-src/src/librats/node/node_context.h +33 -0
  156. package/native-src/src/librats/node/peer_network.h +91 -0
  157. package/native-src/src/librats/peer/peer.h +49 -0
  158. package/native-src/src/librats/peer/peer_book.cpp +181 -0
  159. package/native-src/src/librats/peer/peer_book.h +88 -0
  160. package/native-src/src/librats/peer/peer_id.cpp +72 -0
  161. package/native-src/src/librats/peer/peer_id.h +62 -0
  162. package/native-src/src/librats/peer/peer_info.h +37 -0
  163. package/native-src/src/librats/peer/peer_table.cpp +170 -0
  164. package/native-src/src/librats/peer/peer_table.h +148 -0
  165. package/native-src/src/librats/security/handshaker.h +66 -0
  166. package/native-src/src/librats/security/identity.h +43 -0
  167. package/native-src/src/librats/security/noise_security.cpp +122 -0
  168. package/native-src/src/librats/security/noise_security.h +37 -0
  169. package/native-src/src/librats/security/plaintext_security.h +106 -0
  170. package/native-src/src/librats/security/session.h +37 -0
  171. package/native-src/src/{storage.cpp → librats/storage/storage.cpp} +369 -522
  172. package/native-src/src/{storage.h → librats/storage/storage.h} +135 -299
  173. package/native-src/src/librats/subsystems/bittorrent.cpp +211 -0
  174. package/native-src/src/librats/subsystems/bittorrent.h +136 -0
  175. package/native-src/src/librats/subsystems/dht_discovery.cpp +202 -0
  176. package/native-src/src/librats/subsystems/dht_discovery.h +123 -0
  177. package/native-src/src/librats/subsystems/dht_service.h +36 -0
  178. package/native-src/src/librats/subsystems/file_transfer.cpp +972 -0
  179. package/native-src/src/librats/subsystems/file_transfer.h +367 -0
  180. package/native-src/src/librats/subsystems/hole_punch.cpp +605 -0
  181. package/native-src/src/librats/subsystems/hole_punch.h +290 -0
  182. package/native-src/src/librats/subsystems/hole_punch_service.h +38 -0
  183. package/native-src/src/librats/subsystems/mdns_discovery.cpp +66 -0
  184. package/native-src/src/librats/subsystems/mdns_discovery.h +55 -0
  185. package/native-src/src/librats/subsystems/message_json.cpp +112 -0
  186. package/native-src/src/librats/subsystems/message_json.h +88 -0
  187. package/native-src/src/librats/subsystems/peer_exchange.cpp +241 -0
  188. package/native-src/src/librats/subsystems/peer_exchange.h +136 -0
  189. package/native-src/src/librats/subsystems/ping_service.cpp +98 -0
  190. package/native-src/src/librats/subsystems/ping_service.h +66 -0
  191. package/native-src/src/librats/subsystems/port_mapping_service.cpp +192 -0
  192. package/native-src/src/librats/subsystems/port_mapping_service.h +84 -0
  193. package/native-src/src/librats/subsystems/pubsub.cpp +567 -0
  194. package/native-src/src/librats/subsystems/pubsub.h +175 -0
  195. package/native-src/src/librats/subsystems/reconnection.cpp +239 -0
  196. package/native-src/src/librats/subsystems/reconnection.h +126 -0
  197. package/native-src/src/librats/subsystems/relay.cpp +1142 -0
  198. package/native-src/src/librats/subsystems/relay.h +211 -0
  199. package/native-src/src/librats/subsystems/relay_service.h +46 -0
  200. package/native-src/src/librats/transport/connection.cpp +343 -0
  201. package/native-src/src/librats/transport/connection.h +283 -0
  202. package/native-src/src/librats/transport/link.h +96 -0
  203. package/native-src/src/librats/transport/reactor.cpp +588 -0
  204. package/native-src/src/librats/transport/reactor.h +262 -0
  205. package/native-src/src/librats/transport/reactor_pool.h +81 -0
  206. package/native-src/src/librats/transport/relay_link.cpp +208 -0
  207. package/native-src/src/librats/transport/relay_link.h +303 -0
  208. package/native-src/src/librats/transport/tcp_link.cpp +49 -0
  209. package/native-src/src/librats/transport/tcp_link.h +43 -0
  210. package/native-src/src/librats/transport/udp_mux.cpp +617 -0
  211. package/native-src/src/librats/transport/udp_mux.h +363 -0
  212. package/native-src/src/librats/transport/udp_packet.cpp +121 -0
  213. package/native-src/src/librats/transport/udp_packet.h +190 -0
  214. package/native-src/src/librats/transport/udp_stream.cpp +1194 -0
  215. package/native-src/src/librats/transport/udp_stream.h +614 -0
  216. package/native-src/src/librats/util/features.h.in +51 -0
  217. package/native-src/src/{fs.cpp → librats/util/fs.cpp} +51 -3
  218. package/native-src/src/librats/util/fs.h +136 -0
  219. package/native-src/src/librats/util/json.cpp +1002 -0
  220. package/native-src/src/librats/util/json.h +444 -0
  221. package/native-src/src/{logger.cpp → librats/util/logger.cpp} +1 -1
  222. package/native-src/src/{logger.h → librats/util/logger.h} +43 -31
  223. package/native-src/src/{network_monitor.cpp → librats/util/network_monitor.cpp} +12 -4
  224. package/native-src/src/{network_monitor.h → librats/util/network_monitor.h} +2 -1
  225. package/native-src/src/{network_utils.cpp → librats/util/network_utils.cpp} +38 -23
  226. package/native-src/src/{network_utils.h → librats/util/network_utils.h} +15 -8
  227. package/native-src/src/{os.cpp → librats/util/os.cpp} +48 -18
  228. package/native-src/src/librats/util/rats_export.h +69 -0
  229. package/native-src/src/{version.cpp → librats/util/version.cpp} +2 -2
  230. package/native-src/src/{version.h.in → librats/util/version.h.in} +1 -1
  231. package/native-src/src/librats/wire/frame.cpp +76 -0
  232. package/native-src/src/librats/wire/frame.h +111 -0
  233. package/native-src/src/librats/wire/message_router.cpp +45 -0
  234. package/native-src/src/librats/wire/message_router.h +47 -0
  235. package/package.json +5 -4
  236. package/scripts/build-librats.js +1 -0
  237. package/scripts/postinstall.js +3 -3
  238. package/scripts/prepare-package.js +4 -4
  239. package/scripts/verify-installation.js +63 -105
  240. package/src/librats_node.cpp +1067 -1323
  241. package/native-src/src/bencode.cpp +0 -485
  242. package/native-src/src/bencode.h +0 -145
  243. package/native-src/src/bittorrent.cpp +0 -14
  244. package/native-src/src/bittorrent.h +0 -74
  245. package/native-src/src/bt_bitfield.cpp +0 -372
  246. package/native-src/src/bt_bitfield.h +0 -316
  247. package/native-src/src/bt_choker.cpp +0 -228
  248. package/native-src/src/bt_choker.h +0 -147
  249. package/native-src/src/bt_client.cpp +0 -1047
  250. package/native-src/src/bt_client.h +0 -445
  251. package/native-src/src/bt_create_torrent.cpp +0 -677
  252. package/native-src/src/bt_create_torrent.h +0 -473
  253. package/native-src/src/bt_extension.cpp +0 -469
  254. package/native-src/src/bt_extension.h +0 -309
  255. package/native-src/src/bt_file_storage.cpp +0 -261
  256. package/native-src/src/bt_file_storage.h +0 -298
  257. package/native-src/src/bt_handshake.cpp +0 -134
  258. package/native-src/src/bt_handshake.h +0 -157
  259. package/native-src/src/bt_messages.cpp +0 -364
  260. package/native-src/src/bt_messages.h +0 -324
  261. package/native-src/src/bt_network.cpp +0 -1007
  262. package/native-src/src/bt_network.h +0 -417
  263. package/native-src/src/bt_peer_connection.cpp +0 -742
  264. package/native-src/src/bt_peer_connection.h +0 -592
  265. package/native-src/src/bt_piece_picker.cpp +0 -786
  266. package/native-src/src/bt_piece_picker.h +0 -473
  267. package/native-src/src/bt_resume_data.cpp +0 -410
  268. package/native-src/src/bt_resume_data.h +0 -249
  269. package/native-src/src/bt_torrent.cpp +0 -2120
  270. package/native-src/src/bt_torrent.h +0 -641
  271. package/native-src/src/bt_torrent_info.cpp +0 -659
  272. package/native-src/src/bt_torrent_info.h +0 -418
  273. package/native-src/src/bt_types.h +0 -621
  274. package/native-src/src/chained_send_buffer.cpp +0 -75
  275. package/native-src/src/chained_send_buffer.h +0 -137
  276. package/native-src/src/crypto/hkdf.c +0 -266
  277. package/native-src/src/crypto/poly1305.h +0 -36
  278. package/native-src/src/dht.cpp +0 -3311
  279. package/native-src/src/dht.h +0 -717
  280. package/native-src/src/disk_io.cpp +0 -632
  281. package/native-src/src/disk_io.h +0 -315
  282. package/native-src/src/file_transfer.cpp +0 -1415
  283. package/native-src/src/file_transfer.h +0 -286
  284. package/native-src/src/fs.h +0 -108
  285. package/native-src/src/gossipsub.cpp +0 -1139
  286. package/native-src/src/gossipsub.h +0 -403
  287. package/native-src/src/ice.cpp +0 -893
  288. package/native-src/src/ice.h +0 -559
  289. package/native-src/src/json.hpp +0 -25526
  290. package/native-src/src/librats.cpp +0 -2378
  291. package/native-src/src/librats.h +0 -2324
  292. package/native-src/src/librats_bittorrent.cpp +0 -601
  293. package/native-src/src/librats_c.cpp +0 -1557
  294. package/native-src/src/librats_c.h +0 -323
  295. package/native-src/src/librats_discovery.cpp +0 -402
  296. package/native-src/src/librats_encryption.cpp +0 -275
  297. package/native-src/src/librats_file_transfer.cpp +0 -144
  298. package/native-src/src/librats_gossipsub.cpp +0 -289
  299. package/native-src/src/librats_ice.cpp +0 -213
  300. package/native-src/src/librats_log_macros.h +0 -36
  301. package/native-src/src/librats_logging.cpp +0 -173
  302. package/native-src/src/librats_mdns.cpp +0 -166
  303. package/native-src/src/librats_persistence.cpp +0 -796
  304. package/native-src/src/librats_portmap.cpp +0 -419
  305. package/native-src/src/librats_reconnection.cpp +0 -218
  306. package/native-src/src/librats_statistic.cpp +0 -105
  307. package/native-src/src/librats_storage.cpp +0 -189
  308. package/native-src/src/rats_export.h +0 -17
  309. package/native-src/src/receive_buffer.cpp +0 -82
  310. package/native-src/src/receive_buffer.h +0 -127
  311. package/native-src/src/socket.h +0 -228
  312. package/native-src/src/threadmanager.cpp +0 -105
  313. package/native-src/src/threadmanager.h +0 -53
  314. package/native-src/src/tracker.cpp +0 -1264
  315. package/native-src/src/tracker.h +0 -319
  316. package/native-src/src/turn.cpp +0 -762
  317. package/native-src/src/turn.h +0 -460
  318. package/native-src/src/wakeup_pipe.h +0 -60
  319. /package/native-src/src/{os.h → librats/util/os.h} +0 -0
package/lib/index.js CHANGED
@@ -1,27 +1,33 @@
1
1
  /**
2
- * LibRats Node.js Bindings
3
- *
4
- * High-performance peer-to-peer networking library with support for DHT, GossipSub,
5
- * file transfer, NAT traversal, and more.
2
+ * librats Node.js bindings.
3
+ *
4
+ * A `RatsNode` is one librats node: secure transport (Noise XX over TCP or the
5
+ * library's reliable stream over UDP), a self-certifying peer id, and raw
6
+ * messaging on named channels. Everything else — DHT/mDNS discovery, pub/sub,
7
+ * typed JSON messaging, file transfer, NAT port mapping, hole punching, RTT
8
+ * probing, automatic reconnection — is an opt-in subsystem.
9
+ *
10
+ * Ordering: enable subsystems and register callbacks BEFORE `start()`. Enabling
11
+ * after start throws `ALREADY_STARTED`; using a subsystem before its enable
12
+ * throws `NOT_ENABLED`. Callbacks fire on a librats reactor thread and are
13
+ * marshalled onto the JS thread — keep them non-blocking anyway.
14
+ *
15
+ * Failures throw an `Error` whose message is `librats: <CODE>`.
6
16
  */
7
17
 
18
+ 'use strict';
19
+
8
20
  const path = require('path');
9
21
  const fs = require('fs');
10
22
 
11
- // Try to load the native addon from the build directory
23
+ // Locate and load the compiled native addon.
12
24
  let addon;
13
25
  let addonPath;
14
-
15
- // Possible locations for the built addon
16
26
  const possiblePaths = [
17
- // Standard node-gyp build location
18
27
  path.join(__dirname, '..', 'build', 'Release', 'librats.node'),
19
28
  path.join(__dirname, '..', 'build', 'Debug', 'librats.node'),
20
- // Alternative build locations
21
29
  path.join(__dirname, '..', 'build', 'librats.node'),
22
30
  ];
23
-
24
- // Try each path
25
31
  for (const tryPath of possiblePaths) {
26
32
  try {
27
33
  if (fs.existsSync(tryPath)) {
@@ -30,10 +36,9 @@ for (const tryPath of possiblePaths) {
30
36
  break;
31
37
  }
32
38
  } catch (err) {
33
- // Continue to next path
39
+ // try next
34
40
  }
35
41
  }
36
-
37
42
  if (!addon) {
38
43
  throw new Error(
39
44
  'Could not load librats native addon. ' +
@@ -42,41 +47,399 @@ if (!addon) {
42
47
  );
43
48
  }
44
49
 
45
- // Export all native bindings
46
- module.exports = {
47
- // Main class
48
- RatsClient: addon.RatsClient,
49
-
50
- // Version functions
51
- getVersionString: addon.getVersionString,
52
- getVersion: addon.getVersion,
53
- getGitDescribe: addon.getGitDescribe,
54
- getAbi: addon.getAbi,
55
-
56
- // Constants
57
- ConnectionStrategy: {
58
- DIRECT_ONLY: 0,
59
- STUN_ASSISTED: 1,
60
- ICE_FULL: 2,
61
- TURN_RELAY: 3,
62
- AUTO_ADAPTIVE: 4
63
- },
64
-
65
- ErrorCodes: {
66
- SUCCESS: 0,
67
- INVALID_HANDLE: -1,
68
- INVALID_PARAMETER: -2,
69
- NOT_RUNNING: -3,
70
- OPERATION_FAILED: -4,
71
- PEER_NOT_FOUND: -5,
72
- MEMORY_ALLOCATION: -6,
73
- JSON_PARSE: -7
50
+ /** Transport security selector (`rats_security_t`). */
51
+ const Security = Object.freeze({
52
+ NOISE: 0, // Noise XX, encrypted + authenticated (default)
53
+ PLAINTEXT: 1, // unencrypted, ids exchanged in the clear
54
+ });
55
+
56
+ /**
57
+ * Which wire a peer connection runs on (`rats_transport_t`). Both carry the
58
+ * identical protocol and the identical encrypted handshake; they differ only in
59
+ * how the ordered, reliable byte stream underneath is obtained.
60
+ */
61
+ const Transport = Object.freeze({
62
+ TCP: 0, // one kernel socket per peer
63
+ UDP: 1, // reliable stream over the shared UDP socket
64
+ RELAY: 2, // carried through a third node (see enableRelay)
65
+ });
66
+
67
+ /** Bitmask flags used by `node.transports` and `node.peerTransports()`. */
68
+ const TransportMask = Object.freeze({
69
+ TCP: 0x1,
70
+ UDP: 0x2,
71
+ });
72
+
73
+ /**
74
+ * What the mesh has shown about this node's own NAT, from the endpoints
75
+ * datagram peers report seeing its shared UDP socket at.
76
+ */
77
+ const NatMapping = Object.freeze({
78
+ UNKNOWN: 0, // not enough independent observations yet
79
+ OPEN: 1, // no NAT in the path
80
+ ENDPOINT_INDEPENDENT: 2, // one external port for every peer — punchable
81
+ ENDPOINT_DEPENDENT: 3, // a fresh mapping per peer (symmetric) — not punchable
82
+ });
83
+
84
+ /** Process-global log levels (`rats_log_level_t`). */
85
+ const LogLevel = Object.freeze({
86
+ DEBUG: 0,
87
+ INFO: 1,
88
+ WARN: 2,
89
+ ERROR: 3,
90
+ });
91
+
92
+ /**
93
+ * A librats node.
94
+ *
95
+ * ```js
96
+ * const { RatsNode } = require('librats');
97
+ * const node = new RatsNode({ listenPort: 8080, dataDir: './state' });
98
+ * node.onPeerConnected(id => console.log('peer', id));
99
+ * node.on('chat', (id, data) => console.log(id, data.toString()));
100
+ * node.enableDht();
101
+ * node.start();
102
+ * ```
103
+ */
104
+ class RatsNode {
105
+ /**
106
+ * @param {number|RatsNodeConfig} [portOrConfig=0] listen port (0 = ephemeral)
107
+ * or a config object. Both transports are enabled by default and bind the
108
+ * same port; a dial tries `preferredTransport` (UDP — one socket and one NAT
109
+ * mapping for every peer) and races the other after `transportFallbackMs`.
110
+ */
111
+ constructor(portOrConfig = 0) {
112
+ this._native = new addon.RatsNode(portOrConfig);
74
113
  }
114
+
115
+ // ---- lifecycle ----
116
+
117
+ /** Bring the node up: bind the listener and start enabled subsystems. */
118
+ start() { this._native.start(); }
119
+
120
+ /** Tear the node down and close all connections. Idempotent. */
121
+ stop() { this._native.stop(); }
122
+
123
+ /**
124
+ * Release the native node. The instance is inert afterwards — further calls
125
+ * throw. Optional: an unreachable node is freed by the GC anyway.
126
+ */
127
+ destroy() { this._native.destroy(); }
128
+
129
+ // ---- identity / info ----
130
+
131
+ /** @type {number} the bound listen port (resolved when 0 was requested). */
132
+ get listenPort() { return this._native.listenPort(); }
133
+
134
+ /** @type {string|null} our self-certifying peer id (64-char lowercase hex). */
135
+ get localId() { return this._native.localId(); }
136
+
137
+ /** @type {string|null} the app protocol bound into the handshake, e.g. "librats/1.0". */
138
+ get protocol() { return this._native.protocol(); }
139
+
140
+ /**
141
+ * @type {number} transports actually running, as a {@link TransportMask}
142
+ * bitmask. May be narrower than the config asked for — a UDP socket that could
143
+ * not be bound leaves the node TCP-only rather than failing to start. 0 before
144
+ * `start()` and after `stop()`.
145
+ */
146
+ get transports() { return this._native.transports(); }
147
+
148
+ // ---- connections ----
149
+
150
+ /**
151
+ * Dial a peer. Non-blocking: success surfaces as `onPeerConnected`.
152
+ * @param {string} host @param {number} port
153
+ */
154
+ connect(host, port) { this._native.connect(host, port); }
155
+
156
+ /** @type {number} count of currently-connected peers. */
157
+ get peerCount() { return this._native.peerCount(); }
158
+
159
+ /** @type {string[]} hex ids of currently-connected peers. */
160
+ get peerIds() { return this._native.peerIds(); }
161
+
162
+ /** @type {number} cap on established peers (0 = unlimited). Settable at any time. */
163
+ get maxPeers() { return this._native.maxPeers(); }
164
+ set maxPeers(n) { this._native.setMaxPeers(n); }
165
+
166
+ /**
167
+ * Which wire a connected peer's link runs on.
168
+ * @param {string} peerId
169
+ * @returns {number|null} a {@link Transport} value, or null if not connected.
170
+ */
171
+ peerTransport(peerId) { return this._native.peerTransport(peerId); }
172
+
173
+ /**
174
+ * Transports a connected peer advertised in its identify message.
175
+ * @param {string} peerId
176
+ * @returns {number|null} a {@link TransportMask} bitmask, or null if not
177
+ * connected. 0 means the peer did not say (an older build) — "no
178
+ * information", not "no transports".
179
+ */
180
+ peerTransports(peerId) { return this._native.peerTransports(peerId); }
181
+
182
+ // ---- raw channel messaging ----
183
+
184
+ /**
185
+ * Send raw bytes on a named channel to one peer.
186
+ * @param {string} peerId @param {string} channel @param {string|Buffer} data
187
+ */
188
+ send(peerId, channel, data) { this._native.send(peerId, channel, data); }
189
+
190
+ /**
191
+ * Broadcast raw bytes on a named channel to every connected peer.
192
+ * @param {string} channel @param {string|Buffer} data
193
+ */
194
+ broadcast(channel, data) { this._native.broadcast(channel, data); }
195
+
196
+ /**
197
+ * Register a handler for a named channel. Additive; register before `start()`.
198
+ * @param {string} channel
199
+ * @param {(peerId: string, data: Buffer) => void} handler
200
+ */
201
+ on(channel, handler) { this._native.on(channel, handler); }
202
+
203
+ // ---- peer events (register before start) ----
204
+
205
+ /** @param {(peerId: string) => void} handler fired when a peer connects. */
206
+ onPeerConnected(handler) { this._native.onPeerConnected(handler); }
207
+
208
+ /** @param {(peerId: string) => void} handler fired when a peer disconnects. */
209
+ onPeerDisconnected(handler) { this._native.onPeerDisconnected(handler); }
210
+
211
+ // ---- discovery (enable before start) ----
212
+
213
+ /**
214
+ * Enable DHT discovery: announce on and search a key on the mainline DHT,
215
+ * dialing what it finds.
216
+ * @param {number} [dhtPort=0] 0 = ephemeral
217
+ * @param {string} [discoveryKey] app namespace; defaults to the node's protocol
218
+ */
219
+ enableDht(dhtPort = 0, discoveryKey) { this._native.enableDht(dhtPort, discoveryKey); }
220
+
221
+ /** Enable local-network mDNS discovery. */
222
+ enableMdns() { this._native.enableMdns(); }
223
+
224
+ // ---- NAT traversal (enable before start) ----
225
+
226
+ /**
227
+ * Enable automatic NAT port forwarding for the listen port.
228
+ * @param {boolean} [enableUpnp=true] UPnP IGD backend
229
+ * @param {boolean} [enableNatpmp=true] NAT-PMP backend
230
+ */
231
+ enablePortMapping(enableUpnp = true, enableNatpmp = true) {
232
+ this._native.enablePortMapping(enableUpnp, enableNatpmp);
233
+ }
234
+
235
+ /**
236
+ * Enable UDP hole punching: reach a peer no port forwarding made reachable by
237
+ * arranging with a peer both sides already have that the two dial each other
238
+ * at the same moment.
239
+ * @param {boolean} [serveAsRelay=true] also carry other peers' rendezvous — a
240
+ * few dozen forwarded bytes per punch, only ever to peers this node already
241
+ * holds. A mesh in which nobody relays cannot punch at all.
242
+ */
243
+ enableHolePunch(serveAsRelay = true) { this._native.enableHolePunch(serveAsRelay); }
244
+
245
+ /**
246
+ * Try to reach a peer by punching. Non-blocking: success arrives as an
247
+ * ordinary `onPeerConnected`. Throws `NO_SUCH_PEER` when there is nothing to
248
+ * do or nothing to try with — already connected, a punch already running, or
249
+ * this node does not yet know an external endpoint of its own.
250
+ * @param {string} peerId
251
+ */
252
+ punchPeer(peerId) { this._native.punchPeer(peerId); }
253
+
254
+ /**
255
+ * Enable relaying: reach a peer that neither port forwarding nor hole punching
256
+ * could make reachable, by routing the connection through a node both ends are
257
+ * already connected to. The peer that comes out is ordinary in every way — the
258
+ * same end-to-end encryption, the same channels — except that `peerTransport()`
259
+ * reports it as `Transport.RELAY`.
260
+ * @param {boolean} [serveAsRelay=false] also carry OTHER peers' connections.
261
+ * Unlike a hole-punch rendezvous this spends real bandwidth on somebody else's
262
+ * traffic, so it is off by default; a mesh in which nobody serves cannot relay.
263
+ */
264
+ enableRelay(serveAsRelay = false) { this._native.enableRelay(serveAsRelay); }
265
+
266
+ /**
267
+ * Try to reach a peer through a relay. Non-blocking: success arrives as an
268
+ * ordinary `onPeerConnected`. Throws `NO_SUCH_PEER` when there is nothing to do
269
+ * or nothing to try with — already connected, an attempt already running, in
270
+ * cooldown, or no peer that could carry the connection. Usually unnecessary:
271
+ * with hole punching enabled too, a punch that cannot work hands the target over
272
+ * by itself.
273
+ * @param {string} peerId
274
+ */
275
+ connectViaRelay(peerId) { this._native.connectViaRelay(peerId); }
276
+
277
+ /** @type {number} a {@link NatMapping} value describing this node's own NAT. */
278
+ get natMapping() { return this._native.natMapping(); }
279
+
280
+ // ---- pub/sub (enable before start) ----
281
+
282
+ /** Enable the pub/sub (GossipSub) subsystem. */
283
+ enablePubsub() { this._native.enablePubsub(); }
284
+
285
+ /**
286
+ * Subscribe to a topic. Subscribe before `start()`.
287
+ * @param {string} topic
288
+ * @param {(peerId: string, topic: string, data: Buffer) => void} handler
289
+ */
290
+ subscribe(topic, handler) { this._native.subscribe(topic, handler); }
291
+
292
+ /** Unsubscribe from a topic. @param {string} topic */
293
+ unsubscribe(topic) { this._native.unsubscribe(topic); }
294
+
295
+ /** Publish raw bytes on a topic. @param {string} topic @param {string|Buffer} data */
296
+ publish(topic, data) { this._native.publish(topic, data); }
297
+
298
+ // ---- typed JSON messaging (enable before start) ----
299
+
300
+ /** Enable the typed-JSON messaging subsystem. */
301
+ enableJson() { this._native.enableJson(); }
302
+
303
+ /**
304
+ * Register an additive handler for JSON messages of `type`.
305
+ * @param {string} type
306
+ * @param {(peerId: string, value: any) => void} handler receives the parsed value
307
+ */
308
+ onJson(type, handler) { this._native.onJson(type, wrapJson(handler)); }
309
+
310
+ /** Like {@link onJson}, but the handler is removed after it fires once. */
311
+ onceJson(type, handler) { this._native.onceJson(type, wrapJson(handler)); }
312
+
313
+ /** Remove the handlers for a JSON message type. @param {string} type */
314
+ offJson(type) { this._native.offJson(type); }
315
+
316
+ /**
317
+ * Send a typed JSON message to one peer.
318
+ * @param {string} peerId @param {string} type
319
+ * @param {any} value serialized with `JSON.stringify` unless already a string
320
+ */
321
+ sendJson(peerId, type, value) { this._native.sendJson(peerId, type, toJsonText(value)); }
322
+
323
+ /** Broadcast a typed JSON message. @param {string} type @param {any} value */
324
+ broadcastJson(type, value) { this._native.broadcastJson(type, toJsonText(value)); }
325
+
326
+ // ---- file transfer (enable + register callbacks before start) ----
327
+
328
+ /**
329
+ * Enable the file-transfer subsystem.
330
+ * @param {string} [tempDir] directory for in-progress downloads (default ".")
331
+ */
332
+ enableFileTransfer(tempDir) { this._native.enableFileTransfer(tempDir); }
333
+
334
+ /**
335
+ * Fired for every incoming offer. Respond with {@link acceptFile} / {@link rejectFile}.
336
+ * @param {(peerId: string, transferId: number, name: string, size: number, isDirectory: boolean) => void} handler
337
+ */
338
+ onFileOffer(handler) { this._native.onFileOffer(handler); }
339
+
340
+ /**
341
+ * Fired periodically with transfer progress. `status` is the numeric transfer state.
342
+ * @param {(transferId: number, peerId: string, bytesTransferred: number, totalBytes: number, status: number) => void} handler
343
+ */
344
+ onFileProgress(handler) { this._native.onFileProgress(handler); }
345
+
346
+ /**
347
+ * Fired when a transfer finishes; `path` is the final on-disk path on success.
348
+ * @param {(transferId: number, success: boolean, path: string) => void} handler
349
+ */
350
+ onFileComplete(handler) { this._native.onFileComplete(handler); }
351
+
352
+ /** Offer a file to a peer. @returns {number} transfer id (0 on failure). */
353
+ sendFile(peerId, filePath) { return this._native.sendFile(peerId, filePath); }
354
+
355
+ /** Offer a directory tree to a peer. @returns {number} transfer id (0 on failure). */
356
+ sendDirectory(peerId, dirPath) { return this._native.sendDirectory(peerId, dirPath); }
357
+
358
+ /**
359
+ * Accept an offered transfer.
360
+ * @param {string} peerId @param {number} transferId
361
+ * @param {string} destPath file path (single file) or destination directory
362
+ */
363
+ acceptFile(peerId, transferId, destPath) {
364
+ this._native.acceptFile(peerId, transferId, destPath);
365
+ }
366
+
367
+ /** Reject an offered transfer. */
368
+ rejectFile(peerId, transferId) { this._native.rejectFile(peerId, transferId); }
369
+
370
+ /** Cancel a live transfer (either side). */
371
+ cancelFile(peerId, transferId) { this._native.cancelFile(peerId, transferId); }
372
+
373
+ /** Pause a live transfer (either side). */
374
+ pauseFile(peerId, transferId) { this._native.pauseFile(peerId, transferId); }
375
+
376
+ /** Resume a paused transfer (either side). */
377
+ resumeFile(peerId, transferId) { this._native.resumeFile(peerId, transferId); }
378
+
379
+ // ---- liveness / reconnection (enable before start) ----
380
+
381
+ /** Enable periodic ping/pong RTT probing of every peer. */
382
+ enablePing() { this._native.enablePing(); }
383
+
384
+ /**
385
+ * @param {string} peerId
386
+ * @returns {number} last measured RTT in ms, or -1 if unknown (ping not
387
+ * enabled, or no pong received yet).
388
+ */
389
+ peerRttMs(peerId) { return this._native.peerRttMs(peerId); }
390
+
391
+ /** Enable the reconnection subsystem: re-dials dropped peers with backoff. */
392
+ enableReconnect() { this._native.enableReconnect(); }
393
+
394
+ /** Add an address to keep connected (re-dialed on drop). @param {string} host @param {number} port */
395
+ addReconnect(host, port) { this._native.addReconnect(host, port); }
396
+
397
+ /** Stop reconnecting to an address and drop it from the store. */
398
+ removeReconnect(host, port) { this._native.removeReconnect(host, port); }
399
+ }
400
+
401
+ // JSON messaging is "a named type carrying a JSON document". The C ABI speaks
402
+ // JSON text; JS speaks values, so the conversion lives here rather than in every
403
+ // caller. A string is passed through — it is either already JSON text or a JSON
404
+ // string the peer will parse back to a string either way.
405
+ function toJsonText(value) {
406
+ return typeof value === 'string' ? value : JSON.stringify(value);
407
+ }
408
+
409
+ function wrapJson(handler) {
410
+ return (peerId, json) => {
411
+ let value;
412
+ try {
413
+ value = JSON.parse(json);
414
+ } catch (err) {
415
+ value = json; // hand over the raw text rather than dropping the message
416
+ }
417
+ handler(peerId, value);
418
+ };
419
+ }
420
+
421
+ module.exports = {
422
+ RatsNode,
423
+
424
+ // Enums
425
+ Security,
426
+ Transport,
427
+ TransportMask,
428
+ NatMapping,
429
+ LogLevel,
430
+
431
+ // Library info
432
+ version: addon.getVersionString,
433
+ versionInfo: addon.getVersion,
434
+ gitDescribe: addon.getGitDescribe,
435
+ abi: addon.getAbi,
436
+
437
+ // Process-global logging
438
+ setLogLevel: addon.setLogLevel,
439
+ setLogFile: addon.setLogFile,
75
440
  };
76
441
 
77
- // Log successful load for debugging
78
442
  if (process.env.LIBRATS_DEBUG) {
79
443
  console.log(`[librats] Loaded native addon from: ${addonPath}`);
80
444
  console.log(`[librats] Version: ${addon.getVersionString()}`);
81
445
  }
82
-
@@ -1,3 +1,4 @@
1
+ /* SPDX-License-Identifier: BSD-2-Clause */
1
2
  /*
2
3
  Copyright (c) 2013, Kenneth MacKay
3
4
  All rights reserved.
@@ -1,3 +1,4 @@
1
+ /* SPDX-License-Identifier: BSD-1-Clause */
1
2
  /*
2
3
  * Copyright (c) 1995, 1999
3
4
  * Berkeley Software Design, Inc. All rights reserved.