librats 1.0.2 → 2.1.4

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 (313) hide show
  1. package/README.md +137 -332
  2. package/binding.gyp +16 -3
  3. package/lib/index.d.ts +271 -696
  4. package/lib/index.js +383 -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 +396 -179
  8. package/native-src/LICENSE +1 -1
  9. package/native-src/src/librats/bindings/rats.cpp +731 -0
  10. package/native-src/src/librats/bindings/rats.h +352 -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 +42 -0
  73. package/native-src/src/librats/core/types.h +93 -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/config.h +110 -0
  144. package/native-src/src/librats/node/dial_service.h +54 -0
  145. package/native-src/src/librats/node/dialer.cpp +264 -0
  146. package/native-src/src/librats/node/dialer.h +188 -0
  147. package/native-src/src/librats/node/host_events.h +26 -0
  148. package/native-src/src/librats/node/identify.cpp +130 -0
  149. package/native-src/src/librats/node/identify.h +71 -0
  150. package/native-src/src/librats/node/nat_status.cpp +103 -0
  151. package/native-src/src/librats/node/nat_status.h +118 -0
  152. package/native-src/src/librats/node/node.cpp +826 -0
  153. package/native-src/src/librats/node/node.h +333 -0
  154. package/native-src/src/librats/node/node_context.h +33 -0
  155. package/native-src/src/librats/node/peer_network.h +91 -0
  156. package/native-src/src/librats/peer/peer.h +49 -0
  157. package/native-src/src/librats/peer/peer_book.cpp +181 -0
  158. package/native-src/src/librats/peer/peer_book.h +88 -0
  159. package/native-src/src/librats/peer/peer_id.cpp +72 -0
  160. package/native-src/src/librats/peer/peer_id.h +62 -0
  161. package/native-src/src/librats/peer/peer_info.h +37 -0
  162. package/native-src/src/librats/peer/peer_table.cpp +137 -0
  163. package/native-src/src/librats/peer/peer_table.h +148 -0
  164. package/native-src/src/librats/security/handshaker.h +66 -0
  165. package/native-src/src/librats/security/identity.h +43 -0
  166. package/native-src/src/librats/security/noise_security.cpp +122 -0
  167. package/native-src/src/librats/security/noise_security.h +37 -0
  168. package/native-src/src/librats/security/plaintext_security.h +106 -0
  169. package/native-src/src/librats/security/session.h +37 -0
  170. package/native-src/src/{storage.cpp → librats/storage/storage.cpp} +369 -522
  171. package/native-src/src/{storage.h → librats/storage/storage.h} +135 -299
  172. package/native-src/src/librats/subsystems/bittorrent.cpp +211 -0
  173. package/native-src/src/librats/subsystems/bittorrent.h +136 -0
  174. package/native-src/src/librats/subsystems/dht_discovery.cpp +202 -0
  175. package/native-src/src/librats/subsystems/dht_discovery.h +123 -0
  176. package/native-src/src/librats/subsystems/dht_service.h +36 -0
  177. package/native-src/src/librats/subsystems/file_transfer.cpp +972 -0
  178. package/native-src/src/librats/subsystems/file_transfer.h +367 -0
  179. package/native-src/src/librats/subsystems/hole_punch.cpp +532 -0
  180. package/native-src/src/librats/subsystems/hole_punch.h +266 -0
  181. package/native-src/src/librats/subsystems/hole_punch_service.h +38 -0
  182. package/native-src/src/librats/subsystems/mdns_discovery.cpp +66 -0
  183. package/native-src/src/librats/subsystems/mdns_discovery.h +55 -0
  184. package/native-src/src/librats/subsystems/message_json.cpp +112 -0
  185. package/native-src/src/librats/subsystems/message_json.h +88 -0
  186. package/native-src/src/librats/subsystems/peer_exchange.cpp +241 -0
  187. package/native-src/src/librats/subsystems/peer_exchange.h +136 -0
  188. package/native-src/src/librats/subsystems/ping_service.cpp +98 -0
  189. package/native-src/src/librats/subsystems/ping_service.h +66 -0
  190. package/native-src/src/librats/subsystems/port_mapping_service.cpp +192 -0
  191. package/native-src/src/librats/subsystems/port_mapping_service.h +84 -0
  192. package/native-src/src/librats/subsystems/pubsub.cpp +567 -0
  193. package/native-src/src/librats/subsystems/pubsub.h +175 -0
  194. package/native-src/src/librats/subsystems/reconnection.cpp +239 -0
  195. package/native-src/src/librats/subsystems/reconnection.h +126 -0
  196. package/native-src/src/librats/transport/connection.cpp +343 -0
  197. package/native-src/src/librats/transport/connection.h +283 -0
  198. package/native-src/src/librats/transport/link.h +96 -0
  199. package/native-src/src/librats/transport/reactor.cpp +540 -0
  200. package/native-src/src/librats/transport/reactor.h +224 -0
  201. package/native-src/src/librats/transport/reactor_pool.h +81 -0
  202. package/native-src/src/librats/transport/tcp_link.cpp +49 -0
  203. package/native-src/src/librats/transport/tcp_link.h +43 -0
  204. package/native-src/src/librats/transport/udp_mux.cpp +617 -0
  205. package/native-src/src/librats/transport/udp_mux.h +363 -0
  206. package/native-src/src/librats/transport/udp_packet.cpp +121 -0
  207. package/native-src/src/librats/transport/udp_packet.h +190 -0
  208. package/native-src/src/librats/transport/udp_stream.cpp +1194 -0
  209. package/native-src/src/librats/transport/udp_stream.h +614 -0
  210. package/native-src/src/librats/util/features.h.in +51 -0
  211. package/native-src/src/{fs.cpp → librats/util/fs.cpp} +51 -3
  212. package/native-src/src/librats/util/fs.h +136 -0
  213. package/native-src/src/librats/util/json.cpp +1002 -0
  214. package/native-src/src/librats/util/json.h +444 -0
  215. package/native-src/src/{logger.cpp → librats/util/logger.cpp} +1 -1
  216. package/native-src/src/{logger.h → librats/util/logger.h} +43 -31
  217. package/native-src/src/{network_monitor.cpp → librats/util/network_monitor.cpp} +12 -4
  218. package/native-src/src/{network_monitor.h → librats/util/network_monitor.h} +2 -1
  219. package/native-src/src/{network_utils.cpp → librats/util/network_utils.cpp} +38 -23
  220. package/native-src/src/{network_utils.h → librats/util/network_utils.h} +15 -8
  221. package/native-src/src/{os.cpp → librats/util/os.cpp} +48 -18
  222. package/native-src/src/librats/util/rats_export.h +69 -0
  223. package/native-src/src/{version.cpp → librats/util/version.cpp} +2 -2
  224. package/native-src/src/{version.h.in → librats/util/version.h.in} +1 -1
  225. package/native-src/src/librats/wire/frame.cpp +76 -0
  226. package/native-src/src/librats/wire/frame.h +110 -0
  227. package/native-src/src/librats/wire/message_router.cpp +45 -0
  228. package/native-src/src/librats/wire/message_router.h +47 -0
  229. package/package.json +5 -4
  230. package/scripts/build-librats.js +1 -0
  231. package/scripts/postinstall.js +3 -3
  232. package/scripts/prepare-package.js +4 -4
  233. package/scripts/verify-installation.js +63 -105
  234. package/src/librats_node.cpp +1042 -1324
  235. package/native-src/src/bencode.cpp +0 -485
  236. package/native-src/src/bencode.h +0 -145
  237. package/native-src/src/bittorrent.cpp +0 -14
  238. package/native-src/src/bittorrent.h +0 -74
  239. package/native-src/src/bt_bitfield.cpp +0 -372
  240. package/native-src/src/bt_bitfield.h +0 -316
  241. package/native-src/src/bt_choker.cpp +0 -228
  242. package/native-src/src/bt_choker.h +0 -147
  243. package/native-src/src/bt_client.cpp +0 -1047
  244. package/native-src/src/bt_client.h +0 -445
  245. package/native-src/src/bt_create_torrent.cpp +0 -677
  246. package/native-src/src/bt_create_torrent.h +0 -473
  247. package/native-src/src/bt_extension.cpp +0 -469
  248. package/native-src/src/bt_extension.h +0 -309
  249. package/native-src/src/bt_file_storage.cpp +0 -261
  250. package/native-src/src/bt_file_storage.h +0 -298
  251. package/native-src/src/bt_handshake.cpp +0 -134
  252. package/native-src/src/bt_handshake.h +0 -157
  253. package/native-src/src/bt_messages.cpp +0 -364
  254. package/native-src/src/bt_messages.h +0 -324
  255. package/native-src/src/bt_network.cpp +0 -1007
  256. package/native-src/src/bt_network.h +0 -417
  257. package/native-src/src/bt_peer_connection.cpp +0 -742
  258. package/native-src/src/bt_peer_connection.h +0 -592
  259. package/native-src/src/bt_piece_picker.cpp +0 -786
  260. package/native-src/src/bt_piece_picker.h +0 -473
  261. package/native-src/src/bt_resume_data.cpp +0 -410
  262. package/native-src/src/bt_resume_data.h +0 -249
  263. package/native-src/src/bt_torrent.cpp +0 -2120
  264. package/native-src/src/bt_torrent.h +0 -641
  265. package/native-src/src/bt_torrent_info.cpp +0 -659
  266. package/native-src/src/bt_torrent_info.h +0 -418
  267. package/native-src/src/bt_types.h +0 -621
  268. package/native-src/src/chained_send_buffer.cpp +0 -75
  269. package/native-src/src/chained_send_buffer.h +0 -137
  270. package/native-src/src/crypto/hkdf.c +0 -266
  271. package/native-src/src/crypto/poly1305.h +0 -36
  272. package/native-src/src/dht.cpp +0 -3311
  273. package/native-src/src/dht.h +0 -717
  274. package/native-src/src/disk_io.cpp +0 -632
  275. package/native-src/src/disk_io.h +0 -315
  276. package/native-src/src/file_transfer.cpp +0 -1415
  277. package/native-src/src/file_transfer.h +0 -286
  278. package/native-src/src/fs.h +0 -108
  279. package/native-src/src/gossipsub.cpp +0 -1139
  280. package/native-src/src/gossipsub.h +0 -403
  281. package/native-src/src/ice.cpp +0 -893
  282. package/native-src/src/ice.h +0 -559
  283. package/native-src/src/json.hpp +0 -25526
  284. package/native-src/src/librats.cpp +0 -2378
  285. package/native-src/src/librats.h +0 -2324
  286. package/native-src/src/librats_bittorrent.cpp +0 -601
  287. package/native-src/src/librats_c.cpp +0 -1557
  288. package/native-src/src/librats_c.h +0 -323
  289. package/native-src/src/librats_discovery.cpp +0 -402
  290. package/native-src/src/librats_encryption.cpp +0 -275
  291. package/native-src/src/librats_file_transfer.cpp +0 -144
  292. package/native-src/src/librats_gossipsub.cpp +0 -289
  293. package/native-src/src/librats_ice.cpp +0 -213
  294. package/native-src/src/librats_log_macros.h +0 -36
  295. package/native-src/src/librats_logging.cpp +0 -173
  296. package/native-src/src/librats_mdns.cpp +0 -166
  297. package/native-src/src/librats_persistence.cpp +0 -796
  298. package/native-src/src/librats_portmap.cpp +0 -419
  299. package/native-src/src/librats_reconnection.cpp +0 -218
  300. package/native-src/src/librats_statistic.cpp +0 -105
  301. package/native-src/src/librats_storage.cpp +0 -189
  302. package/native-src/src/rats_export.h +0 -17
  303. package/native-src/src/receive_buffer.cpp +0 -82
  304. package/native-src/src/receive_buffer.h +0 -127
  305. package/native-src/src/socket.h +0 -228
  306. package/native-src/src/threadmanager.cpp +0 -105
  307. package/native-src/src/threadmanager.h +0 -53
  308. package/native-src/src/tracker.cpp +0 -1264
  309. package/native-src/src/tracker.h +0 -319
  310. package/native-src/src/turn.cpp +0 -762
  311. package/native-src/src/turn.h +0 -460
  312. package/native-src/src/wakeup_pipe.h +0 -60
  313. /package/native-src/src/{os.h → librats/util/os.h} +0 -0
package/README.md CHANGED
@@ -1,410 +1,215 @@
1
- # LibRats Node.js Bindings
1
+ # librats Node.js bindings
2
2
 
3
- Node.js bindings for librats - A high-performance peer-to-peer networking library with support for DHT, GossipSub, file transfer, NAT traversal, and more.
3
+ Node.js bindings for [librats](../README.md), a C++17 peer-to-peer networking
4
+ library: encrypted transport, DHT/mDNS discovery, NAT traversal, pub/sub, typed
5
+ JSON messaging and file transfer.
4
6
 
5
- ## Features
7
+ ## The model
6
8
 
7
- - **Peer-to-peer networking** - Direct connections between peers
8
- - **DHT (Distributed Hash Table)** - Decentralized peer discovery
9
- - **GossipSub** - Topic-based publish/subscribe messaging
10
- - **File Transfer** - Send and receive files and directories
11
- - **mDNS Discovery** - Local network peer discovery
12
- - **NAT Traversal** - STUN/ICE support for connecting through firewalls
13
- - **Encryption** - End-to-end encryption using Noise protocol
14
- - **Message Exchange** - String, binary, and JSON message types
15
- - **Configuration Persistence** - Save and restore client settings
9
+ A `RatsNode` is one librats node. On its own it gives you secure transport
10
+ (Noise XX over TCP **or** the library's reliable stream over UDP, both on the
11
+ same port), a self-certifying peer id, manual dialing, and raw messaging on named
12
+ channels nothing else. Discovery, pub/sub, JSON messaging, file transfer, NAT
13
+ port mapping, hole punching, RTT probing and reconnection are **opt-in
14
+ subsystems**: you pay only for what you turn on.
16
15
 
17
- ## Installation
18
-
19
- ### Quick Install
20
-
21
- Install directly from npm:
22
-
23
- ```bash
24
- npm install librats
25
- ```
16
+ Two rules follow from that:
26
17
 
27
- The installation process will automatically:
28
- 1. Download all necessary source files
29
- 2. Build the native librats library using CMake
30
- 3. Build the Node.js native addon
31
- 4. Set up the bindings
18
+ - **Enable subsystems and register callbacks before `start()`.** Enabling after
19
+ start throws `RATS_ERR_ALREADY_STARTED`; using a subsystem before its enable
20
+ throws `RATS_ERR_NOT_ENABLED`.
21
+ - **Callbacks are marshalled onto the JS thread** from a librats reactor thread.
22
+ Keep them short anyway — a slow handler backs up the reactor's queue.
32
23
 
33
- **No additional build steps required!**
24
+ Fallible calls **throw** an `Error` whose message is `librats: <CODE>`; pure
25
+ getters are properties.
34
26
 
35
- ### Prerequisites
36
-
37
- The following build tools are required and will be used automatically during installation:
38
-
39
- - **Node.js**: 20.0.0 or higher
40
- - **CMake**: Version 3.10 or higher ([download here](https://cmake.org/download/))
41
- - **C++ Compiler and Build Tools**:
42
- - **Windows**: Visual Studio Build Tools 2017 or later ([download here](https://visualstudio.microsoft.com/downloads/))
43
- - **Linux**: `build-essential` and `cmake` packages
44
- ```bash
45
- sudo apt install build-essential cmake
46
- ```
47
- - **macOS**: Xcode Command Line Tools
48
- ```bash
49
- xcode-select --install
50
- ```
51
-
52
- ### Verifying Installation
53
-
54
- After installation, you can verify everything is working:
27
+ ## Installation
55
28
 
56
29
  ```bash
57
- npm run verify
30
+ npm install librats
58
31
  ```
59
32
 
60
- This will check that the native addon is built correctly and all features are available.
33
+ The install builds the native librats library with CMake, then compiles the
34
+ Node.js addon against it. You need:
61
35
 
62
- ### Building from Source
36
+ - **Node.js** 20+
37
+ - **CMake** 3.14+ ([download](https://cmake.org/download/))
38
+ - a **C++17 toolchain** — Visual Studio Build Tools 2017+ (Windows),
39
+ `build-essential` (Linux), `xcode-select --install` (macOS)
63
40
 
64
- If you're developing or need to rebuild:
41
+ See [INSTALLATION.md](INSTALLATION.md) for what runs during install and how to
42
+ troubleshoot it.
65
43
 
66
- ```bash
67
- # Clone the repository
68
- git clone https://github.com/librats/librats.git
69
- cd librats/nodejs
70
-
71
- # Install (builds everything automatically)
72
- npm install
73
-
74
- # Verify the installation
75
- npm run verify
76
-
77
- # Or build manually
78
- npm run build
79
- ```
80
-
81
- ## Quick Start
82
-
83
- ### Basic Example
44
+ ## Quick start
84
45
 
85
46
  ```javascript
86
- const { RatsClient, ConnectionStrategy } = require('librats');
47
+ const { RatsNode } = require('librats');
87
48
 
88
- // Create a client listening on port 8080
89
- const client = new RatsClient(8080);
49
+ const node = new RatsNode({ listenPort: 8080, dataDir: './state' });
90
50
 
91
- // Set up event handlers
92
- client.onConnection((peerId) => {
93
- console.log(`Peer connected: ${peerId}`);
94
- client.sendString(peerId, 'Hello from Node.js!');
51
+ // Everything below happens before start().
52
+ node.onPeerConnected((peerId) => {
53
+ console.log(`+ ${peerId}`);
54
+ node.send(peerId, 'chat', 'Hello from Node.js!');
95
55
  });
96
-
97
- client.onString((peerId, message) => {
98
- console.log(`Received: ${message} from ${peerId}`);
56
+ node.on('chat', (peerId, data) => {
57
+ console.log(`[chat] ${peerId}: ${data.toString('utf8')}`);
99
58
  });
59
+ node.enableDht(); // find peers on the mainline DHT
60
+ node.enableMdns(); // …and on the local network
100
61
 
101
- // Start the client
102
- client.start();
103
-
104
- // Connect to another peer
105
- client.connect('127.0.0.1', 8081);
62
+ node.start();
63
+ console.log(node.localId, 'listening on', node.listenPort);
106
64
  ```
107
65
 
108
- ### File Transfer Example
66
+ ### Pub/sub
109
67
 
110
68
  ```javascript
111
- const { RatsClient } = require('librats');
112
-
113
- const client = new RatsClient(8080);
114
-
115
- // Set up file transfer progress monitoring
116
- client.onFileProgress((transferId, progressPercent, status) => {
117
- console.log(`Transfer ${transferId}: ${progressPercent}% - ${status}`);
69
+ node.enablePubsub();
70
+ node.subscribe('lobby', (peerId, topic, data) => {
71
+ console.log(`[${topic}] ${peerId}: ${data.toString('utf8')}`);
118
72
  });
73
+ node.start();
74
+ node.publish('lobby', 'hi everyone');
75
+ ```
119
76
 
120
- // Accept incoming transfer offers (transfers are push-only)
121
- client.onFileRequest((peerId, transferId, filename) => {
122
- console.log(`Incoming offer "${filename}" from ${peerId}`);
123
- client.acceptFileTransfer(transferId, `./downloads/${filename}`);
124
- });
77
+ ### Typed JSON messaging
125
78
 
126
- client.start();
79
+ Values are serialized and parsed for you — handlers receive the parsed value.
127
80
 
128
- // Send a file to a peer
129
- const transferId = client.sendFile('peer_id_here', './myfile.txt', 'remote_file.txt');
130
- console.log(`File transfer started: ${transferId}`);
81
+ ```javascript
82
+ node.enableJson();
83
+ node.onJson('greeting', (peerId, value) => console.log(peerId, value.hello));
84
+ node.start();
85
+ node.broadcastJson('greeting', { hello: 'world' });
131
86
  ```
132
87
 
133
- ### GossipSub Chat Example
88
+ ### File transfer
134
89
 
135
90
  ```javascript
136
- const { RatsClient } = require('librats');
137
-
138
- const client = new RatsClient(8080);
139
- client.start();
91
+ node.enableFileTransfer('./tmp'); // in-progress downloads live here
140
92
 
141
- // Subscribe to a topic
142
- client.subscribeToTopic('general-chat');
143
-
144
- // Publish a message
145
- const message = {
146
- type: 'chat',
147
- username: 'Alice',
148
- message: 'Hello everyone!',
149
- timestamp: Date.now()
150
- };
93
+ node.onFileOffer((peerId, transferId, name, size, isDirectory) => {
94
+ node.acceptFile(peerId, transferId, `./downloads/${name}`);
95
+ });
96
+ node.onFileProgress((transferId, peerId, done, total) => {
97
+ console.log(`${transferId}: ${done}/${total}`);
98
+ });
99
+ node.onFileComplete((transferId, success, path) => {
100
+ console.log(`${transferId} ${success ? 'done' : 'failed'} ${path}`);
101
+ });
151
102
 
152
- client.publishJsonToTopic('general-chat', JSON.stringify(message));
103
+ node.start();
104
+ const transferId = node.sendFile(peerId, './myfile.txt'); // 0 on failure
153
105
  ```
154
106
 
155
- ## API Reference
156
-
157
- ### RatsClient
107
+ ### NAT traversal
158
108
 
159
- #### Constructor
109
+ Port forwarding is the easy path; hole punching covers the networks where it
110
+ fails. Both are opt-in, and punching needs peers that relay the rendezvous.
160
111
 
161
- - `new RatsClient(listenPort: number)` - Create a new client instance
112
+ ```javascript
113
+ node.enablePortMapping(); // UPnP IGD + NAT-PMP
114
+ node.enableHolePunch(true); // true = also relay other peers' rendezvous
115
+ node.start();
162
116
 
163
- #### Basic Operations
117
+ node.punchPeer(peerId); // success arrives as onPeerConnected
118
+ console.log(node.natMapping); // NatMapping.ENDPOINT_INDEPENDENT ⇒ punchable
119
+ ```
164
120
 
165
- - `start(): boolean` - Start the client
166
- - `stop(): void` - Stop the client
167
- - `connect(host: string, port: number): boolean` - Connect to a peer
168
- - `connectWithStrategy(host: string, port: number, strategy: number): boolean` - Connect with specific strategy
169
- - `disconnect(peerId: string): void` - Disconnect from a peer
121
+ ## API
170
122
 
171
- #### Messaging
123
+ ### Construction
172
124
 
173
- - `sendString(peerId: string, message: string): boolean` - Send string message
174
- - `sendBinary(peerId: string, data: Buffer): boolean` - Send binary data
175
- - `sendJson(peerId: string, jsonStr: string): boolean` - Send JSON data
176
- - `broadcastString(message: string): number` - Broadcast string to all peers
177
- - `broadcastBinary(data: Buffer): number` - Broadcast binary to all peers
178
- - `broadcastJson(jsonStr: string): number` - Broadcast JSON to all peers
125
+ ```javascript
126
+ new RatsNode(port) // listen on port (0 = ephemeral)
127
+ new RatsNode(config) // full config
128
+ ```
179
129
 
180
- #### Information
130
+ | config field | default | meaning |
131
+ |---|---|---|
132
+ | `listenPort` | `0` | inbound port shared by both transports; 0 = ephemeral |
133
+ | `enableListen` | `true` | `false` makes a dial-only node |
134
+ | `bindAddress` | `"::"` | dual-stack wildcard by default |
135
+ | `security` | `Security.NOISE` | or `Security.PLAINTEXT` |
136
+ | `dataDir` | — | persistent identity + subsystem state; omit for an ephemeral identity |
137
+ | `protocol` | `"librats/1.0"` | handshake app id; peers whose protocol differs cannot connect |
138
+ | `maxPeers` | `0` | established-peer cap; 0 = unlimited |
139
+ | `enableTcp` / `enableUdp` | `true` | which wires to accept and dial on |
140
+ | `preferredTransport` | `Transport.UDP` | which one a dial tries first |
141
+ | `transportFallbackMs` | `1200` | delay before racing the other; 0 disables |
181
142
 
182
- - `getPeerCount(): number` - Get number of connected peers
183
- - `getOurPeerId(): string | null` - Get our peer ID
184
- - `getPeerIds(): string[]` - Get list of connected peer IDs
185
- - `getConnectionStatistics(): string | null` - Get connection statistics as JSON
143
+ ### Lifecycle
186
144
 
187
- #### File Transfer
145
+ `start()` · `stop()` · `destroy()`
188
146
 
189
- - `sendFile(peerId: string, filePath: string, remoteFilename?: string): string | null` - Send file
190
- - `sendDirectory(peerId: string, dirPath: string, remoteDirName?: string): string | null` - Send directory (always recursive)
191
- - `acceptFileTransfer(transferId: string, localPath: string): boolean` - Accept an incoming file/directory transfer offer
192
- - `rejectFileTransfer(transferId: string, reason?: string): boolean` - Reject file transfer
193
- - `cancelFileTransfer(transferId: string): boolean` - Cancel file transfer
194
- - `pauseFileTransfer(transferId: string): boolean` - Pause file transfer
195
- - `resumeFileTransfer(transferId: string): boolean` - Resume file transfer
147
+ ### Properties
196
148
 
197
- #### GossipSub
149
+ `listenPort` · `localId` · `protocol` · `transports` · `peerCount` · `peerIds` ·
150
+ `maxPeers` *(settable)* · `natMapping`
198
151
 
199
- - `isGossipsubAvailable(): boolean` - Check if GossipSub is available
200
- - `subscribeToTopic(topic: string): boolean` - Subscribe to topic
201
- - `unsubscribeFromTopic(topic: string): boolean` - Unsubscribe from topic
202
- - `publishToTopic(topic: string, message: string): boolean` - Publish message
203
- - `publishJsonToTopic(topic: string, jsonStr: string): boolean` - Publish JSON
204
- - `getSubscribedTopics(): string[]` - Get subscribed topics
205
- - `getTopicPeers(topic: string): string[]` - Get peers in topic
152
+ ### Connections
206
153
 
207
- #### DHT
154
+ `connect(host, port)` · `peerTransport(peerId)` · `peerTransports(peerId)`
208
155
 
209
- - `startDhtDiscovery(dhtPort: number): boolean` - Start DHT discovery
210
- - `stopDhtDiscovery(): void` - Stop DHT discovery
211
- - `isDhtRunning(): boolean` - Check if DHT is running
212
- - `announceForHash(contentHash: string, port: number, callback?: (peers: string[]) => void): boolean` - Announce for hash with optional peer discovery callback
156
+ ### Raw channel messaging
213
157
 
214
- #### Encryption
158
+ `send(peerId, channel, data)` · `broadcast(channel, data)` ·
159
+ `on(channel, (peerId, data) => …)` — `data` is a `Buffer` in, `string | Buffer` out
215
160
 
216
- - `setEncryptionEnabled(enabled: boolean): boolean` - Enable/disable encryption
217
- - `isEncryptionEnabled(): boolean` - Check if encryption is enabled
218
- - `generateEncryptionKey(): string | null` - Generate new encryption key
219
- - `setEncryptionKey(keyHex: string): boolean` - Set encryption key
220
- - `getEncryptionKey(): string | null` - Get current encryption key
161
+ ### Peer events *(before start)*
221
162
 
222
- #### Event Handlers
163
+ `onPeerConnected(cb)` · `onPeerDisconnected(cb)`
223
164
 
224
- - `onConnection(callback: (peerId: string) => void): void` - Set connection callback
225
- - `onString(callback: (peerId: string, message: string) => void): void` - Set string message callback
226
- - `onBinary(callback: (peerId: string, data: Buffer) => void): void` - Set binary message callback
227
- - `onJson(callback: (peerId: string, jsonStr: string) => void): void` - Set JSON message callback
228
- - `onDisconnect(callback: (peerId: string) => void): void` - Set disconnect callback
229
- - `onFileProgress(callback: (transferId: string, progressPercent: number, status: string) => void): void` - Set file progress callback
230
- - `onFileRequest(callback: (peerId: string, transferId: string, filename: string) => void): void` - Set incoming transfer offer callback (respond with `acceptFileTransfer`/`rejectFileTransfer`)
165
+ ### Subsystems *(enable before start)*
231
166
 
232
- ### Utility Functions
167
+ | subsystem | enable | then |
168
+ |---|---|---|
169
+ | DHT discovery | `enableDht(dhtPort?, discoveryKey?)` | — |
170
+ | mDNS discovery | `enableMdns()` | — |
171
+ | NAT port mapping | `enablePortMapping(upnp?, natpmp?)` | — |
172
+ | Hole punching | `enableHolePunch(serveAsRelay?)` | `punchPeer(peerId)`, `natMapping` |
173
+ | Pub/sub | `enablePubsub()` | `subscribe(topic, cb)`, `unsubscribe(topic)`, `publish(topic, data)` |
174
+ | Typed JSON | `enableJson()` | `onJson(type, cb)`, `onceJson(type, cb)`, `offJson(type)`, `sendJson(peerId, type, value)`, `broadcastJson(type, value)` |
175
+ | File transfer | `enableFileTransfer(tempDir?)` | `onFileOffer/onFileProgress/onFileComplete`, `sendFile`, `sendDirectory`, `acceptFile`, `rejectFile`, `cancelFile`, `pauseFile`, `resumeFile` |
176
+ | Liveness | `enablePing()` | `peerRttMs(peerId)` — ms, or -1 if unknown |
177
+ | Reconnection | `enableReconnect()` | `addReconnect(host, port)`, `removeReconnect(host, port)` |
233
178
 
234
- - `getVersionString(): string` - Get librats version string
235
- - `getVersion(): VersionInfo` - Get version components
236
- - `getGitDescribe(): string` - Get git describe string
237
- - `getAbi(): number` - Get ABI version
179
+ ### Module level
238
180
 
239
- ### Constants
181
+ `version()` · `versionInfo()` · `gitDescribe()` · `abi()` ·
182
+ `setLogLevel(level)` · `setLogFile(path?)` ·
183
+ `Security` · `Transport` · `TransportMask` · `NatMapping` · `LogLevel`
240
184
 
241
- #### ConnectionStrategy
185
+ ## TypeScript
242
186
 
243
- - `ConnectionStrategy.DIRECT_ONLY` - Direct connection only
244
- - `ConnectionStrategy.STUN_ASSISTED` - STUN-assisted connection
245
- - `ConnectionStrategy.ICE_FULL` - Full ICE negotiation
246
- - `ConnectionStrategy.TURN_RELAY` - TURN relay connection
247
- - `ConnectionStrategy.AUTO_ADAPTIVE` - Automatic strategy selection
187
+ Definitions ship with the package.
248
188
 
249
- #### ErrorCodes
189
+ ```typescript
190
+ import { RatsNode, Security } from 'librats';
250
191
 
251
- - `ErrorCodes.SUCCESS` - Operation successful
252
- - `ErrorCodes.INVALID_HANDLE` - Invalid client handle
253
- - `ErrorCodes.INVALID_PARAMETER` - Invalid parameter
254
- - `ErrorCodes.NOT_RUNNING` - Client not running
255
- - `ErrorCodes.OPERATION_FAILED` - Operation failed
256
- - `ErrorCodes.PEER_NOT_FOUND` - Peer not found
257
- - `ErrorCodes.MEMORY_ALLOCATION` - Memory allocation error
258
- - `ErrorCodes.JSON_PARSE` - JSON parsing error
192
+ const node = new RatsNode({ listenPort: 8080, security: Security.NOISE });
193
+ node.start();
194
+ ```
259
195
 
260
196
  ## Examples
261
197
 
262
- The `examples/` directory contains comprehensive examples:
263
-
264
- - **`basic_client.js`** - Basic peer-to-peer networking and messaging
265
- - **`file_transfer.js`** - File and directory transfer with interactive CLI
266
- - **`gossipsub_chat.js`** - Topic-based chat using GossipSub
267
-
268
- ### Running Examples
269
-
270
198
  ```bash
271
- # Basic client (listens on port 8080)
272
- node examples/basic_client.js
273
-
274
- # Basic client connecting to another peer
199
+ node examples/basic_client.js 8080
275
200
  node examples/basic_client.js 8081 127.0.0.1 8080
276
-
277
- # File transfer client
278
201
  node examples/file_transfer.js 8080
279
-
280
- # GossipSub chat
281
- node examples/gossipsub_chat.js 8080 Alice
202
+ node examples/gossipsub_chat.js 8080 Alice lobby
282
203
  ```
283
204
 
284
205
  ## Testing
285
206
 
286
- Run the test suite:
287
-
288
207
  ```bash
289
208
  npm test
209
+ npm run verify # check a fresh install end-to-end
210
+ LIBRATS_DEBUG=1 node examples/basic_client.js # log which addon was loaded
290
211
  ```
291
212
 
292
- Or run the simple test script:
293
-
294
- ```bash
295
- node test/test.js
296
- ```
297
-
298
- ## Building from Source
299
-
300
- ### Debug Build
301
-
302
- ```bash
303
- npm run build
304
- ```
305
-
306
- ### Clean Build
307
-
308
- ```bash
309
- npm run clean
310
- npm run build
311
- ```
312
-
313
- ## TypeScript Support
314
-
315
- Full TypeScript definitions are included. Import types:
316
-
317
- ```typescript
318
- import { RatsClient, ConnectionStrategy, ErrorCodes, VersionInfo } from 'librats';
319
-
320
- const client: RatsClient = new RatsClient(8080);
321
- client.start();
322
- ```
323
-
324
- ## Error Handling
325
-
326
- Most operations return boolean values indicating success/failure. For detailed error information, check the console output or use the statistics functions:
327
-
328
- ```javascript
329
- const stats = client.getConnectionStatistics();
330
- if (stats) {
331
- const parsed = JSON.parse(stats);
332
- console.log('Connection stats:', parsed);
333
- }
334
- ```
335
-
336
- ## Performance Considerations
337
-
338
- - Use binary messages for large data transfers
339
- - Enable encryption only when needed (adds overhead)
340
- - Monitor file transfer progress for large files
341
- - Use appropriate connection strategies for your network environment
342
-
343
- ## Platform Support
344
-
345
- - **Windows** - Requires Visual Studio Build Tools
346
- - **Linux** - Requires build-essential
347
- - **macOS** - Requires Xcode Command Line Tools
348
- - **Android** - Supported via native Android bindings (separate package)
349
-
350
- ## Contributing
351
-
352
- 1. Fork the repository
353
- 2. Create a feature branch
354
- 3. Make your changes
355
- 4. Add tests for new functionality
356
- 5. Submit a pull request
357
-
358
213
  ## License
359
214
 
360
- This project is licensed under the MIT License - see the LICENSE file for details.
361
-
362
- ## Troubleshooting
363
-
364
- ### Build Issues
365
-
366
- **Windows**: Ensure Visual Studio Build Tools are installed
367
- ```bash
368
- npm install --global windows-build-tools
369
- ```
370
-
371
- **Linux**: Install build dependencies
372
- ```bash
373
- sudo apt install build-essential python3
374
- ```
375
-
376
- **macOS**: Install Xcode Command Line Tools
377
- ```bash
378
- xcode-select --install
379
- ```
380
-
381
- ### Runtime Issues
382
-
383
- **Port already in use**: Choose a different port or kill the process using the port
384
- ```bash
385
- # Find process using port
386
- netstat -tulpn | grep :8080
387
-
388
- # Kill process
389
- kill -9 <pid>
390
- ```
391
-
392
- **Permission denied**: Run with appropriate permissions or use ports > 1024
393
-
394
- **Connection timeout**: Check firewall settings and ensure peers are reachable
395
-
396
- ### Debug Mode
397
-
398
- Enable debug logging by setting environment variable:
399
- ```bash
400
- export LIBRATS_DEBUG=1
401
- node examples/basic_client.js
402
- ```
403
-
404
- ## Support
405
-
406
- - [GitHub Issues](https://github.com/librats/librats/issues)
407
- - [Documentation](https://librats.github.io/)
408
- - [Examples Repository](https://github.com/librats/librats/tree/main/nodejs/examples)
409
-
410
- For more advanced usage, see the [C API documentation](../docs/) and the [main project README](../README.md).
215
+ MIT see [LICENSE](../LICENSE).
package/binding.gyp CHANGED
@@ -8,8 +8,15 @@
8
8
  ],
9
9
  "include_dirs": [
10
10
  "<!@(node -p \"require('node-addon-api').include\")",
11
+ # librats headers. "librats/bindings/rats.h" is the canonical C ABI; it
12
+ # pulls in librats/util/rats_export.h. These paths are the include ROOT
13
+ # (the parent of librats/), matching CMakeLists.txt. native-src/src is
14
+ # used when the package is installed from npm (sources bundled there);
15
+ # ../src in dev.
11
16
  "native-src/src",
12
17
  "../src",
18
+ # Generated headers (version.h, features.h) produced by the CMake build into
19
+ # build-native/src/. Mirrors PROJECT_BINARY_DIR/src in CMakeLists.txt.
13
20
  "build-native/src"
14
21
  ],
15
22
  "dependencies": [
@@ -17,20 +24,26 @@
17
24
  ],
18
25
  "cflags!": [ "-fno-exceptions" ],
19
26
  "cflags_cc!": [ "-fno-exceptions" ],
27
+ "cflags_cc": [ "-std=c++17" ],
20
28
  "xcode_settings": {
21
29
  "GCC_ENABLE_CPP_EXCEPTIONS": "YES",
22
30
  "CLANG_CXX_LIBRARY": "libc++",
23
- "MACOSX_DEPLOYMENT_TARGET": "10.7"
31
+ "CLANG_CXX_LANGUAGE_STANDARD": "c++17",
32
+ "MACOSX_DEPLOYMENT_TARGET": "10.13"
24
33
  },
25
34
  "msvs_settings": {
26
- "VCCLCompilerTool": {
35
+ "VCCLCompilerTool": {
27
36
  "ExceptionHandling": 1,
28
- "RuntimeLibrary": 2
37
+ "RuntimeLibrary": 2,
38
+ "AdditionalOptions": [ "/std:c++17" ]
29
39
  }
30
40
  },
31
41
  "defines": [ "NAPI_DISABLE_CPP_EXCEPTIONS" ],
32
42
  "conditions": [
33
43
  ["OS=='win'", {
44
+ # Link the prebuilt static archive produced by scripts/build-librats.js
45
+ # (CMake builds rats.lib with RATS_BINDINGS=ON, so the rats_* C ABI is
46
+ # already inside). Plus the platform networking/crypto libs.
34
47
  "configurations": {
35
48
  "Debug": {
36
49
  "msvs_settings": {