netkitty 0.0.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 (338) hide show
  1. package/.eslintrc.json +53 -0
  2. package/.idea/codeStyles/Project.xml +18 -0
  3. package/.idea/codeStyles/codeStyleConfig.xml +5 -0
  4. package/.idea/inspectionProfiles/Project_Default.xml +10 -0
  5. package/.idea/modules.xml +8 -0
  6. package/.idea/netkitty.iml +13 -0
  7. package/.idea/vcs.xml +6 -0
  8. package/LICENSE +21 -0
  9. package/README.md +2 -0
  10. package/binding.gyp +5 -0
  11. package/build/NetKitty.d.ts +31 -0
  12. package/build/NetKitty.d.ts.map +1 -0
  13. package/build/NetKitty.js +33 -0
  14. package/build/errors/ActionNotFoundError.d.ts +5 -0
  15. package/build/errors/ActionNotFoundError.d.ts.map +1 -0
  16. package/build/errors/ActionNotFoundError.js +12 -0
  17. package/build/errors/CodecSchemaValidateError.d.ts +5 -0
  18. package/build/errors/CodecSchemaValidateError.d.ts.map +1 -0
  19. package/build/errors/CodecSchemaValidateError.js +12 -0
  20. package/build/errors/DeviceNotFoundError.d.ts +5 -0
  21. package/build/errors/DeviceNotFoundError.d.ts.map +1 -0
  22. package/build/errors/DeviceNotFoundError.js +12 -0
  23. package/build/errors/NoAvailableCodecError.d.ts +5 -0
  24. package/build/errors/NoAvailableCodecError.d.ts.map +1 -0
  25. package/build/errors/NoAvailableCodecError.js +12 -0
  26. package/build/errors/NpcapLoadError.d.ts +5 -0
  27. package/build/errors/NpcapLoadError.d.ts.map +1 -0
  28. package/build/errors/NpcapLoadError.js +12 -0
  29. package/build/errors/SocketDownError.d.ts +5 -0
  30. package/build/errors/SocketDownError.d.ts.map +1 -0
  31. package/build/errors/SocketDownError.js +12 -0
  32. package/build/errors/UnknownPipeMessageTypeError.d.ts +5 -0
  33. package/build/errors/UnknownPipeMessageTypeError.d.ts.map +1 -0
  34. package/build/errors/UnknownPipeMessageTypeError.js +12 -0
  35. package/build/errors/common/ErrorCode.d.ts +31 -0
  36. package/build/errors/common/ErrorCode.d.ts.map +1 -0
  37. package/build/errors/common/ErrorCode.js +33 -0
  38. package/build/lib/GeneratePipeAddress.d.ts +7 -0
  39. package/build/lib/GeneratePipeAddress.d.ts.map +1 -0
  40. package/build/lib/GeneratePipeAddress.js +23 -0
  41. package/build/lib/GetBinding.d.ts +2 -0
  42. package/build/lib/GetBinding.d.ts.map +1 -0
  43. package/build/lib/GetBinding.js +6 -0
  44. package/build/lib/GetCaptureTemporaryDirectory.d.ts +7 -0
  45. package/build/lib/GetCaptureTemporaryDirectory.d.ts.map +1 -0
  46. package/build/lib/GetCaptureTemporaryDirectory.js +15 -0
  47. package/build/lib/GetDeviceCaptureTemporaryFilename.d.ts +8 -0
  48. package/build/lib/GetDeviceCaptureTemporaryFilename.d.ts.map +1 -0
  49. package/build/lib/GetDeviceCaptureTemporaryFilename.js +18 -0
  50. package/build/lib/codec/Codec.d.ts +33 -0
  51. package/build/lib/codec/Codec.d.ts.map +1 -0
  52. package/build/lib/codec/Codec.js +166 -0
  53. package/build/lib/codec/abstracts/BaseHeader.d.ts +204 -0
  54. package/build/lib/codec/abstracts/BaseHeader.d.ts.map +1 -0
  55. package/build/lib/codec/abstracts/BaseHeader.js +339 -0
  56. package/build/lib/codec/headers/ARP.d.ts +9 -0
  57. package/build/lib/codec/headers/ARP.d.ts.map +1 -0
  58. package/build/lib/codec/headers/ARP.js +223 -0
  59. package/build/lib/codec/headers/EthernetII.d.ts +9 -0
  60. package/build/lib/codec/headers/EthernetII.d.ts.map +1 -0
  61. package/build/lib/codec/headers/EthernetII.js +80 -0
  62. package/build/lib/codec/headers/Goose.d.ts +12 -0
  63. package/build/lib/codec/headers/Goose.d.ts.map +1 -0
  64. package/build/lib/codec/headers/Goose.js +701 -0
  65. package/build/lib/codec/headers/IEC61850SampledValues.d.ts +12 -0
  66. package/build/lib/codec/headers/IEC61850SampledValues.d.ts.map +1 -0
  67. package/build/lib/codec/headers/IEC61850SampledValues.js +369 -0
  68. package/build/lib/codec/headers/IPv4.d.ts +15 -0
  69. package/build/lib/codec/headers/IPv4.d.ts.map +1 -0
  70. package/build/lib/codec/headers/IPv4.js +338 -0
  71. package/build/lib/codec/headers/RawData.d.ts +9 -0
  72. package/build/lib/codec/headers/RawData.d.ts.map +1 -0
  73. package/build/lib/codec/headers/RawData.js +34 -0
  74. package/build/lib/codec/headers/TCP.d.ts +15 -0
  75. package/build/lib/codec/headers/TCP.d.ts.map +1 -0
  76. package/build/lib/codec/headers/TCP.js +897 -0
  77. package/build/lib/codec/headers/UDP.d.ts +15 -0
  78. package/build/lib/codec/headers/UDP.d.ts.map +1 -0
  79. package/build/lib/codec/headers/UDP.js +191 -0
  80. package/build/lib/codec/headers/VLAN_802dot1Q.d.ts +9 -0
  81. package/build/lib/codec/headers/VLAN_802dot1Q.d.ts.map +1 -0
  82. package/build/lib/codec/headers/VLAN_802dot1Q.js +77 -0
  83. package/build/lib/codec/lib/BufferToHex.d.ts +2 -0
  84. package/build/lib/codec/lib/BufferToHex.d.ts.map +1 -0
  85. package/build/lib/codec/lib/BufferToHex.js +6 -0
  86. package/build/lib/codec/lib/BufferToIP.d.ts +13 -0
  87. package/build/lib/codec/lib/BufferToIP.d.ts.map +1 -0
  88. package/build/lib/codec/lib/BufferToIP.js +21 -0
  89. package/build/lib/codec/lib/BufferToNumber.d.ts +10 -0
  90. package/build/lib/codec/lib/BufferToNumber.d.ts.map +1 -0
  91. package/build/lib/codec/lib/BufferToNumber.js +22 -0
  92. package/build/lib/codec/lib/FixHexString.d.ts +7 -0
  93. package/build/lib/codec/lib/FixHexString.d.ts.map +1 -0
  94. package/build/lib/codec/lib/FixHexString.js +11 -0
  95. package/build/lib/codec/lib/FlexibleObject.d.ts +28 -0
  96. package/build/lib/codec/lib/FlexibleObject.d.ts.map +1 -0
  97. package/build/lib/codec/lib/FlexibleObject.js +102 -0
  98. package/build/lib/codec/lib/HexToNumber.d.ts +10 -0
  99. package/build/lib/codec/lib/HexToNumber.d.ts.map +1 -0
  100. package/build/lib/codec/lib/HexToNumber.js +39 -0
  101. package/build/lib/codec/lib/IPToBuffer.d.ts +13 -0
  102. package/build/lib/codec/lib/IPToBuffer.d.ts.map +1 -0
  103. package/build/lib/codec/lib/IPToBuffer.js +28 -0
  104. package/build/lib/codec/lib/NumberToBERBuffer.d.ts +9 -0
  105. package/build/lib/codec/lib/NumberToBERBuffer.d.ts.map +1 -0
  106. package/build/lib/codec/lib/NumberToBERBuffer.js +20 -0
  107. package/build/lib/codec/lib/NumberToBERHex.d.ts +9 -0
  108. package/build/lib/codec/lib/NumberToBERHex.d.ts.map +1 -0
  109. package/build/lib/codec/lib/NumberToBERHex.js +23 -0
  110. package/build/lib/codec/lib/NumberToBuffer.d.ts +10 -0
  111. package/build/lib/codec/lib/NumberToBuffer.d.ts.map +1 -0
  112. package/build/lib/codec/lib/NumberToBuffer.js +22 -0
  113. package/build/lib/codec/lib/NumberToHex.d.ts +10 -0
  114. package/build/lib/codec/lib/NumberToHex.d.ts.map +1 -0
  115. package/build/lib/codec/lib/NumberToHex.js +26 -0
  116. package/build/lib/codec/lib/ProcessPacketPostHandlers.d.ts +14 -0
  117. package/build/lib/codec/lib/ProcessPacketPostHandlers.d.ts.map +1 -0
  118. package/build/lib/codec/lib/ProcessPacketPostHandlers.js +53 -0
  119. package/build/lib/codec/lib/SortPostHandlers.d.ts +8 -0
  120. package/build/lib/codec/lib/SortPostHandlers.d.ts.map +1 -0
  121. package/build/lib/codec/lib/SortPostHandlers.js +11 -0
  122. package/build/lib/codec/lib/StringContentEncodingEnum.d.ts +8 -0
  123. package/build/lib/codec/lib/StringContentEncodingEnum.d.ts.map +1 -0
  124. package/build/lib/codec/lib/StringContentEncodingEnum.js +11 -0
  125. package/build/lib/codec/types/CodecData.d.ts +7 -0
  126. package/build/lib/codec/types/CodecData.d.ts.map +1 -0
  127. package/build/lib/codec/types/CodecData.js +2 -0
  128. package/build/lib/codec/types/CodecDecodeResult.d.ts +9 -0
  129. package/build/lib/codec/types/CodecDecodeResult.d.ts.map +1 -0
  130. package/build/lib/codec/types/CodecDecodeResult.js +2 -0
  131. package/build/lib/codec/types/CodecEncodeInput.d.ts +5 -0
  132. package/build/lib/codec/types/CodecEncodeInput.d.ts.map +1 -0
  133. package/build/lib/codec/types/CodecEncodeInput.js +2 -0
  134. package/build/lib/codec/types/CodecEncodeResult.d.ts +6 -0
  135. package/build/lib/codec/types/CodecEncodeResult.d.ts.map +1 -0
  136. package/build/lib/codec/types/CodecEncodeResult.js +2 -0
  137. package/build/lib/codec/types/CodecErrorInfo.d.ts +6 -0
  138. package/build/lib/codec/types/CodecErrorInfo.d.ts.map +1 -0
  139. package/build/lib/codec/types/CodecErrorInfo.js +2 -0
  140. package/build/lib/codec/types/CodecModule.d.ts +3 -0
  141. package/build/lib/codec/types/CodecModule.d.ts.map +1 -0
  142. package/build/lib/codec/types/CodecModule.js +2 -0
  143. package/build/lib/codec/types/CodecModuleConstructor.d.ts +3 -0
  144. package/build/lib/codec/types/CodecModuleConstructor.d.ts.map +1 -0
  145. package/build/lib/codec/types/CodecModuleConstructor.js +2 -0
  146. package/build/lib/codec/types/CodecSchema.d.ts +7 -0
  147. package/build/lib/codec/types/CodecSchema.d.ts.map +1 -0
  148. package/build/lib/codec/types/CodecSchema.js +2 -0
  149. package/build/lib/codec/types/HeaderTreeNode.d.ts +5 -0
  150. package/build/lib/codec/types/HeaderTreeNode.d.ts.map +1 -0
  151. package/build/lib/codec/types/HeaderTreeNode.js +2 -0
  152. package/build/lib/codec/types/PostHandlerItem.d.ts +5 -0
  153. package/build/lib/codec/types/PostHandlerItem.d.ts.map +1 -0
  154. package/build/lib/codec/types/PostHandlerItem.js +2 -0
  155. package/build/lib/nodepcap/Capture.d.ts +75 -0
  156. package/build/lib/nodepcap/Capture.d.ts.map +1 -0
  157. package/build/lib/nodepcap/Capture.js +309 -0
  158. package/build/lib/nodepcap/GetNetworkInterfaces.d.ts +7 -0
  159. package/build/lib/nodepcap/GetNetworkInterfaces.d.ts.map +1 -0
  160. package/build/lib/nodepcap/GetNetworkInterfaces.js +22 -0
  161. package/build/lib/nodepcap/interfaces/IBindingCapture.d.ts +16 -0
  162. package/build/lib/nodepcap/interfaces/IBindingCapture.d.ts.map +1 -0
  163. package/build/lib/nodepcap/interfaces/IBindingCapture.js +2 -0
  164. package/build/lib/nodepcap/interfaces/IBindingCaptureOptions.d.ts +5 -0
  165. package/build/lib/nodepcap/interfaces/IBindingCaptureOptions.d.ts.map +1 -0
  166. package/build/lib/nodepcap/interfaces/IBindingCaptureOptions.js +2 -0
  167. package/build/lib/nodepcap/interfaces/ICaptureOptions.d.ts +19 -0
  168. package/build/lib/nodepcap/interfaces/ICaptureOptions.d.ts.map +1 -0
  169. package/build/lib/nodepcap/interfaces/ICaptureOptions.js +2 -0
  170. package/build/lib/nodepcap/interfaces/INetworkInterface.d.ts +5 -0
  171. package/build/lib/nodepcap/interfaces/INetworkInterface.d.ts.map +1 -0
  172. package/build/lib/nodepcap/interfaces/INetworkInterface.js +2 -0
  173. package/build/lib/nodepcap/lib/BindingCapture.d.ts +15 -0
  174. package/build/lib/nodepcap/lib/BindingCapture.d.ts.map +1 -0
  175. package/build/lib/nodepcap/lib/BindingCapture.js +35 -0
  176. package/build/lib/nodepcap/lib/GetNodePcapBinding.d.ts +2 -0
  177. package/build/lib/nodepcap/lib/GetNodePcapBinding.d.ts.map +1 -0
  178. package/build/lib/nodepcap/lib/GetNodePcapBinding.js +55 -0
  179. package/build/lib/nodepcap/lib/GetNodePcapBindingCapture.d.ts +3 -0
  180. package/build/lib/nodepcap/lib/GetNodePcapBindingCapture.d.ts.map +1 -0
  181. package/build/lib/nodepcap/lib/GetNodePcapBindingCapture.js +18 -0
  182. package/build/lib/nodepcap/workers/CaptureWorker.d.ts +2 -0
  183. package/build/lib/nodepcap/workers/CaptureWorker.d.ts.map +1 -0
  184. package/build/lib/nodepcap/workers/CaptureWorker.js +25 -0
  185. package/build/lib/pcap/PCAPGenerator.d.ts +34 -0
  186. package/build/lib/pcap/PCAPGenerator.d.ts.map +1 -0
  187. package/build/lib/pcap/PCAPGenerator.js +95 -0
  188. package/build/lib/pcap/PcapParser.d.ts +27 -0
  189. package/build/lib/pcap/PcapParser.d.ts.map +1 -0
  190. package/build/lib/pcap/PcapParser.js +149 -0
  191. package/build/lib/pcap/PcapReader.d.ts +87 -0
  192. package/build/lib/pcap/PcapReader.d.ts.map +1 -0
  193. package/build/lib/pcap/PcapReader.js +207 -0
  194. package/build/lib/pcap/PcapWriter.d.ts +29 -0
  195. package/build/lib/pcap/PcapWriter.d.ts.map +1 -0
  196. package/build/lib/pcap/PcapWriter.js +80 -0
  197. package/build/lib/pcap/interfaces/IPcapPacketInfo.d.ts +13 -0
  198. package/build/lib/pcap/interfaces/IPcapPacketInfo.d.ts.map +1 -0
  199. package/build/lib/pcap/interfaces/IPcapPacketInfo.js +2 -0
  200. package/build/lib/pipe/PipeClient.d.ts +35 -0
  201. package/build/lib/pipe/PipeClient.d.ts.map +1 -0
  202. package/build/lib/pipe/PipeClient.js +71 -0
  203. package/build/lib/pipe/PipeClientSocket.d.ts +39 -0
  204. package/build/lib/pipe/PipeClientSocket.d.ts.map +1 -0
  205. package/build/lib/pipe/PipeClientSocket.js +82 -0
  206. package/build/lib/pipe/PipeMessage.d.ts +27 -0
  207. package/build/lib/pipe/PipeMessage.d.ts.map +1 -0
  208. package/build/lib/pipe/PipeMessage.js +61 -0
  209. package/build/lib/pipe/PipeMessageHandler.d.ts +16 -0
  210. package/build/lib/pipe/PipeMessageHandler.d.ts.map +1 -0
  211. package/build/lib/pipe/PipeMessageHandler.js +95 -0
  212. package/build/lib/pipe/PipeServer.d.ts +25 -0
  213. package/build/lib/pipe/PipeServer.d.ts.map +1 -0
  214. package/build/lib/pipe/PipeServer.js +59 -0
  215. package/build/lib/schema/ProtocolFieldJSONSchema.d.ts +35 -0
  216. package/build/lib/schema/ProtocolFieldJSONSchema.d.ts.map +1 -0
  217. package/build/lib/schema/ProtocolFieldJSONSchema.js +2 -0
  218. package/build/lib/schema/ProtocolFieldJSONSchemaDefinition.d.ts +3 -0
  219. package/build/lib/schema/ProtocolFieldJSONSchemaDefinition.d.ts.map +1 -0
  220. package/build/lib/schema/ProtocolFieldJSONSchemaDefinition.js +2 -0
  221. package/build/lib/schema/ProtocolJSONSchema.d.ts +32 -0
  222. package/build/lib/schema/ProtocolJSONSchema.d.ts.map +1 -0
  223. package/build/lib/schema/ProtocolJSONSchema.js +2 -0
  224. package/build/tests/Test.spec.d.ts +2 -0
  225. package/build/tests/Test.spec.d.ts.map +1 -0
  226. package/build/tests/Test.spec.js +25 -0
  227. package/build/tests/units/CaptureTest.spec.d.ts +2 -0
  228. package/build/tests/units/CaptureTest.spec.d.ts.map +1 -0
  229. package/build/tests/units/CaptureTest.spec.js +31 -0
  230. package/build/tests/units/Codec.spec.d.ts +2 -0
  231. package/build/tests/units/Codec.spec.d.ts.map +1 -0
  232. package/build/tests/units/Codec.spec.js +70 -0
  233. package/cpp/nodepcap/deps/include/Packet32.h +429 -0
  234. package/cpp/nodepcap/deps/include/pcap/bluetooth.h +56 -0
  235. package/cpp/nodepcap/deps/include/pcap/bpf.h +292 -0
  236. package/cpp/nodepcap/deps/include/pcap/can_socketcan.h +56 -0
  237. package/cpp/nodepcap/deps/include/pcap/compiler-tests.h +163 -0
  238. package/cpp/nodepcap/deps/include/pcap/dlt.h +1514 -0
  239. package/cpp/nodepcap/deps/include/pcap/funcattrs.h +325 -0
  240. package/cpp/nodepcap/deps/include/pcap/ipnet.h +43 -0
  241. package/cpp/nodepcap/deps/include/pcap/namedb.h +84 -0
  242. package/cpp/nodepcap/deps/include/pcap/nflog.h +94 -0
  243. package/cpp/nodepcap/deps/include/pcap/pcap-inttypes.h +101 -0
  244. package/cpp/nodepcap/deps/include/pcap/pcap.h +1219 -0
  245. package/cpp/nodepcap/deps/include/pcap/sll.h +149 -0
  246. package/cpp/nodepcap/deps/include/pcap/socket.h +84 -0
  247. package/cpp/nodepcap/deps/include/pcap/usb.h +143 -0
  248. package/cpp/nodepcap/deps/include/pcap/vlan.h +46 -0
  249. package/cpp/nodepcap/deps/include/pcap-bpf.h +45 -0
  250. package/cpp/nodepcap/deps/include/pcap-namedb.h +40 -0
  251. package/cpp/nodepcap/deps/include/pcap.h +43 -0
  252. package/cpp/nodepcap/deps/lib/ARM64/Packet.lib +0 -0
  253. package/cpp/nodepcap/deps/lib/Packet.lib +0 -0
  254. package/cpp/nodepcap/deps/lib/wpcap.lib +0 -0
  255. package/cpp/nodepcap/deps/lib/x64/Packet.lib +0 -0
  256. package/cpp/nodepcap/deps/lib/x64/wpcap.lib +0 -0
  257. package/cpp/nodepcap/nodepcap.gypi +104 -0
  258. package/cpp/nodepcap/src/binding.cc +14 -0
  259. package/cpp/nodepcap/src/capture.cc +400 -0
  260. package/cpp/nodepcap/src/capture.h +53 -0
  261. package/cpp/nodepcap/src/utils.cc +218 -0
  262. package/cpp/nodepcap/src/utils.h +13 -0
  263. package/package.json +65 -0
  264. package/src/NetKitty.ts +31 -0
  265. package/src/errors/ActionNotFoundError.ts +6 -0
  266. package/src/errors/CodecSchemaValidateError.ts +6 -0
  267. package/src/errors/DeviceNotFoundError.ts +6 -0
  268. package/src/errors/NoAvailableCodecError.ts +6 -0
  269. package/src/errors/NpcapLoadError.ts +6 -0
  270. package/src/errors/SocketDownError.ts +6 -0
  271. package/src/errors/UnknownPipeMessageTypeError.ts +6 -0
  272. package/src/errors/common/ErrorCode.ts +30 -0
  273. package/src/lib/GeneratePipeAddress.ts +17 -0
  274. package/src/lib/GetBinding.ts +3 -0
  275. package/src/lib/GetCaptureTemporaryDirectory.ts +12 -0
  276. package/src/lib/GetDeviceCaptureTemporaryFilename.ts +13 -0
  277. package/src/lib/codec/Codec.ts +181 -0
  278. package/src/lib/codec/abstracts/BaseHeader.ts +413 -0
  279. package/src/lib/codec/headers/ARP.ts +218 -0
  280. package/src/lib/codec/headers/EthernetII.ts +77 -0
  281. package/src/lib/codec/headers/Goose.ts +658 -0
  282. package/src/lib/codec/headers/IEC61850SampledValues.ts +359 -0
  283. package/src/lib/codec/headers/IPv4.ts +329 -0
  284. package/src/lib/codec/headers/RawData.ts +34 -0
  285. package/src/lib/codec/headers/TCP.ts +931 -0
  286. package/src/lib/codec/headers/UDP.ts +180 -0
  287. package/src/lib/codec/headers/VLAN_802dot1Q.ts +74 -0
  288. package/src/lib/codec/lib/BufferToHex.ts +3 -0
  289. package/src/lib/codec/lib/BufferToIP.ts +19 -0
  290. package/src/lib/codec/lib/BufferToNumber.ts +31 -0
  291. package/src/lib/codec/lib/FixHexString.ts +8 -0
  292. package/src/lib/codec/lib/FlexibleObject.ts +100 -0
  293. package/src/lib/codec/lib/HexToNumber.ts +25 -0
  294. package/src/lib/codec/lib/IPToBuffer.ts +25 -0
  295. package/src/lib/codec/lib/NumberToBERBuffer.ts +19 -0
  296. package/src/lib/codec/lib/NumberToBERHex.ts +13 -0
  297. package/src/lib/codec/lib/NumberToBuffer.ts +21 -0
  298. package/src/lib/codec/lib/NumberToHex.ts +15 -0
  299. package/src/lib/codec/lib/ProcessPacketPostHandlers.ts +53 -0
  300. package/src/lib/codec/lib/SortPostHandlers.ts +10 -0
  301. package/src/lib/codec/lib/StringContentEncodingEnum.ts +7 -0
  302. package/src/lib/codec/types/CodecData.ts +7 -0
  303. package/src/lib/codec/types/CodecDecodeResult.ts +9 -0
  304. package/src/lib/codec/types/CodecEncodeInput.ts +5 -0
  305. package/src/lib/codec/types/CodecEncodeResult.ts +6 -0
  306. package/src/lib/codec/types/CodecErrorInfo.ts +5 -0
  307. package/src/lib/codec/types/CodecModule.ts +3 -0
  308. package/src/lib/codec/types/CodecModuleConstructor.ts +3 -0
  309. package/src/lib/codec/types/CodecSchema.ts +7 -0
  310. package/src/lib/codec/types/HeaderTreeNode.ts +5 -0
  311. package/src/lib/codec/types/PostHandlerItem.ts +4 -0
  312. package/src/lib/nodepcap/Capture.ts +291 -0
  313. package/src/lib/nodepcap/GetNetworkInterfaces.ts +18 -0
  314. package/src/lib/nodepcap/interfaces/IBindingCapture.ts +27 -0
  315. package/src/lib/nodepcap/interfaces/IBindingCaptureOptions.ts +4 -0
  316. package/src/lib/nodepcap/interfaces/ICaptureOptions.ts +19 -0
  317. package/src/lib/nodepcap/interfaces/INetworkInterface.ts +4 -0
  318. package/src/lib/nodepcap/lib/BindingCapture.ts +46 -0
  319. package/src/lib/nodepcap/lib/GetNodePcapBinding.ts +17 -0
  320. package/src/lib/nodepcap/lib/GetNodePcapBindingCapture.ts +15 -0
  321. package/src/lib/nodepcap/workers/CaptureWorker.ts +30 -0
  322. package/src/lib/pcap/PCAPGenerator.ts +118 -0
  323. package/src/lib/pcap/PcapParser.ts +170 -0
  324. package/src/lib/pcap/PcapReader.ts +239 -0
  325. package/src/lib/pcap/PcapWriter.ts +93 -0
  326. package/src/lib/pcap/interfaces/IPcapPacketInfo.ts +12 -0
  327. package/src/lib/pipe/PipeClient.ts +91 -0
  328. package/src/lib/pipe/PipeClientSocket.ts +99 -0
  329. package/src/lib/pipe/PipeMessage.ts +74 -0
  330. package/src/lib/pipe/PipeMessageHandler.ts +91 -0
  331. package/src/lib/pipe/PipeServer.ts +74 -0
  332. package/src/lib/schema/ProtocolFieldJSONSchema.ts +45 -0
  333. package/src/lib/schema/ProtocolFieldJSONSchemaDefinition.ts +3 -0
  334. package/src/lib/schema/ProtocolJSONSchema.ts +37 -0
  335. package/src/tests/Test.spec.ts +22 -0
  336. package/src/tests/units/CaptureTest.spec.ts +30 -0
  337. package/src/tests/units/Codec.spec.ts +85 -0
  338. package/tsconfig.json +41 -0
@@ -0,0 +1,170 @@
1
+ import EventEmitter from 'events'
2
+ import {createReadStream, ReadStream} from 'node:fs'
3
+ import {format} from 'node:util'
4
+ import {IPcapPacketInfo} from './interfaces/IPcapPacketInfo'
5
+
6
+ const GLOBAL_HEADER_LENGTH = 24 //bytes
7
+ const PACKET_HEADER_LENGTH = 16 //bytes
8
+
9
+ export class PcapParser extends EventEmitter {
10
+
11
+ protected stream: ReadStream
12
+
13
+ protected buffer: Buffer | null
14
+
15
+ protected errored: boolean = false
16
+
17
+ protected endianness: 'BE' | 'LE' | null
18
+
19
+ protected index: number = 0
20
+
21
+ protected state: () => boolean
22
+
23
+ protected offset: number = 0
24
+
25
+ protected currentPacketHeader: {
26
+ timestampSeconds: number
27
+ timestampMicroseconds: number
28
+ capturedLength: number
29
+ originalLength: number
30
+ }
31
+
32
+ public static parse(input: string | ReadStream): PcapParser {
33
+ return new PcapParser(input)
34
+ }
35
+
36
+ constructor(input: string | ReadStream) {
37
+ super()
38
+ this.stream = (typeof input === 'string') ? createReadStream(input) : input
39
+ this.stream.pause()
40
+ this.stream.on('data', (data: string | Buffer): void => this.onData(data as Buffer))
41
+ this.stream.on('error', (err: Error): void => this.onError(err))
42
+ this.stream.on('end', (): void => this.onEnd())
43
+ this.buffer = null
44
+ this.state = () => this.parseGlobalHeader()
45
+ this.endianness = null
46
+ process.nextTick(this.stream.resume.bind(this.stream))
47
+ }
48
+
49
+ protected updateBuffer(data: Buffer): void {
50
+ if (data === null || data === undefined) return
51
+ if (this.buffer === null) {
52
+ this.buffer = data
53
+ } else {
54
+ let extendedBuffer: Buffer = Buffer.alloc(this.buffer.length + data.length)
55
+ this.buffer.copy(extendedBuffer)
56
+ data.copy(extendedBuffer, this.buffer.length)
57
+ this.buffer = extendedBuffer
58
+ }
59
+ }
60
+
61
+ protected parseGlobalHeader(): boolean {
62
+ const buffer: Buffer = this.buffer!
63
+ if (buffer.length >= GLOBAL_HEADER_LENGTH) {
64
+ let msg: string
65
+ const magicNumber: string = buffer.toString('hex', 0, 4)
66
+ // determine pcap endianness
67
+ if (magicNumber == 'a1b2c3d4') {
68
+ this.endianness = 'BE'
69
+ } else if (magicNumber == 'd4c3b2a1') {
70
+ this.endianness = 'LE'
71
+ } else {
72
+ this.errored = true
73
+ this.stream.pause()
74
+ msg = format('unknown magic number: %s', magicNumber)
75
+ this.emit('error', new Error(msg))
76
+ this.onEnd()
77
+ return false
78
+ }
79
+
80
+ const header = {
81
+ magicNumber: buffer['readUInt32' + this.endianness](0, true),
82
+ majorVersion: buffer['readUInt16' + this.endianness](4, true),
83
+ minorVersion: buffer['readUInt16' + this.endianness](6, true),
84
+ gmtOffset: buffer['readInt32' + this.endianness](8, true),
85
+ timestampAccuracy: buffer['readUInt32' + this.endianness](12, true),
86
+ snapshotLength: buffer['readUInt32' + this.endianness](16, true),
87
+ linkLayerType: buffer['readUInt32' + this.endianness](20, true)
88
+ }
89
+
90
+ if (header.majorVersion != 2 && header.minorVersion != 4) {
91
+ this.errored = true
92
+ this.stream.pause()
93
+ msg = format('unsupported version %d.%d. pcap-parser only parses libpcap file format 2.4', header.majorVersion, header.minorVersion)
94
+ this.emit('error', new Error(msg))
95
+ this.onEnd()
96
+ } else {
97
+ this.emit('globalHeader', header)
98
+ this.buffer = buffer.subarray(GLOBAL_HEADER_LENGTH)
99
+ this.state = (): boolean => this.parsePacketHeader()
100
+ this.offset = GLOBAL_HEADER_LENGTH
101
+ return true
102
+ }
103
+ }
104
+
105
+ return false
106
+ }
107
+
108
+ protected parsePacketHeader(): boolean {
109
+ const buffer: Buffer = this.buffer!
110
+ if (buffer.length >= PACKET_HEADER_LENGTH) {
111
+ const header = {
112
+ timestampSeconds: buffer['readUInt32' + this.endianness](0, true),
113
+ timestampMicroseconds: buffer['readUInt32' + this.endianness](4, true),
114
+ capturedLength: buffer['readUInt32' + this.endianness](8, true),
115
+ originalLength: buffer['readUInt32' + this.endianness](12, true)
116
+ }
117
+ this.currentPacketHeader = header
118
+ this.emit('packetHeader', header)
119
+ this.buffer = buffer.subarray(PACKET_HEADER_LENGTH)
120
+ this.state = (): boolean => this.parsePacketBody()
121
+ this.index += 1
122
+ return true
123
+ }
124
+ return false
125
+ }
126
+
127
+ protected parsePacketBody(): boolean {
128
+ const buffer: Buffer = this.buffer!
129
+
130
+ if (buffer.length >= this.currentPacketHeader.capturedLength) {
131
+ const data: Buffer = buffer.subarray(0, this.currentPacketHeader!.capturedLength)
132
+ const packetLength: number = data.length
133
+ const recordLength: number = PACKET_HEADER_LENGTH + packetLength
134
+ const pcapPacketInfo: IPcapPacketInfo = {
135
+ index: this.index,
136
+ offset: this.offset,
137
+ length: recordLength,
138
+ recordHeaderOffset: this.offset,
139
+ recordHeaderLength: PACKET_HEADER_LENGTH,
140
+ packetOffset: this.offset + PACKET_HEADER_LENGTH,
141
+ packetLength: data.length,
142
+ seconds: this.currentPacketHeader.timestampSeconds,
143
+ microseconds: this.currentPacketHeader.timestampMicroseconds,
144
+ packet: data.toString('base64')
145
+ }
146
+ this.emit('packetData', data)
147
+ this.emit('packet', pcapPacketInfo)
148
+ this.buffer = buffer.subarray(this.currentPacketHeader.capturedLength)
149
+ this.state = (): boolean => this.parsePacketHeader()
150
+ this.offset += recordLength
151
+ return true
152
+ }
153
+ return false
154
+ }
155
+
156
+ protected onData(data: Buffer): void {
157
+ if (this.errored) return
158
+ this.updateBuffer(data)
159
+ while (this.state()) {
160
+ }
161
+ }
162
+
163
+ protected onError(err: Error): void {
164
+ this.emit('error', err)
165
+ }
166
+
167
+ protected onEnd(): void {
168
+ this.emit('end')
169
+ }
170
+ }
@@ -0,0 +1,239 @@
1
+ import EventEmitter from 'events'
2
+ import {FileHandle, FileReadResult, open} from 'node:fs/promises'
3
+ import {ReadStream, WriteStream} from 'node:fs'
4
+ import DuplexPair from 'duplexpair'
5
+ import {PcapParser} from './PcapParser'
6
+ import {IPcapPacketInfo} from './interfaces/IPcapPacketInfo'
7
+
8
+ export interface IPcapReaderOptions {
9
+ filename: string
10
+ watch?: boolean
11
+ chunkSize?: number
12
+ }
13
+
14
+ export class PcapReader extends EventEmitter {
15
+
16
+
17
+ protected readonly filename: string
18
+
19
+ protected readonly watch: boolean
20
+
21
+ protected readonly chunkSize: number = 1518 * 10
22
+
23
+ protected duplexPair: DuplexPair
24
+
25
+ protected parser: PcapParser
26
+
27
+ protected index: number = 0
28
+
29
+ protected offset: number = 0
30
+
31
+ protected readDone: boolean = false
32
+
33
+ protected readBufferFileHandle: FileHandle | null = null
34
+
35
+ protected get readStream(): ReadStream {
36
+ return this.duplexPair.socket2
37
+ }
38
+
39
+ protected get writeStream(): WriteStream {
40
+ return this.duplexPair.socket1
41
+ }
42
+
43
+ constructor(options: IPcapReaderOptions) {
44
+ super()
45
+ this.filename = options.filename
46
+ this.watch = !!options.watch
47
+ this.chunkSize = options.chunkSize ? options.chunkSize : this.chunkSize
48
+ }
49
+
50
+ /**
51
+ * Initialize reader
52
+ * @protected
53
+ */
54
+ protected initReader(): void {
55
+ this.duplexPair = new DuplexPair()
56
+ this.parser = PcapParser.parse(this.readStream)
57
+ this.parser.on('packet', (pcapPacketInfo: IPcapPacketInfo): void => {
58
+ this.index = pcapPacketInfo.index
59
+ this.emit('packet', pcapPacketInfo)
60
+ })
61
+ }
62
+
63
+ /**
64
+ * Reset reader
65
+ * @protected
66
+ */
67
+ protected async reset(): Promise<void> {
68
+ await this.stop()
69
+ this.parser?.removeAllListeners()
70
+ this.index = 0
71
+ this.offset = 0
72
+ this.initReader()
73
+ }
74
+
75
+ /**
76
+ * Initialize read packet file handle
77
+ * @protected
78
+ */
79
+ protected async initReadPacketFileHandle(): Promise<FileHandle> {
80
+ return await open(this.filename, 'r')
81
+ }
82
+
83
+ /**
84
+ * Initialize read buffer file handle
85
+ * @protected
86
+ */
87
+ protected async initReadBufferFileHandle(): Promise<FileHandle> {
88
+ if (!this.readBufferFileHandle) this.readBufferFileHandle = await open(this.filename, 'r')
89
+ return this.readBufferFileHandle
90
+ }
91
+
92
+ /**
93
+ * Read file buffer
94
+ * @protected
95
+ */
96
+ protected async readBuffer(): Promise<boolean> {
97
+ const readBufferFileHandle: FileHandle = await this.initReadBufferFileHandle()
98
+ try {
99
+ const result: FileReadResult<Buffer> = await readBufferFileHandle.read({
100
+ buffer: Buffer.alloc(this.chunkSize),
101
+ offset: 0,
102
+ length: this.chunkSize,
103
+ position: this.offset
104
+ })
105
+ if (result.bytesRead <= 0) return true
106
+ const data: Buffer = Buffer.alloc(result.bytesRead)
107
+ result.buffer.copy(data, 0, 0, result.bytesRead)
108
+ this.offset += result.bytesRead
109
+ this.writeStream.write(data)
110
+ return false
111
+ } catch (e) {
112
+ return true
113
+ }
114
+ }
115
+
116
+ /**
117
+ * Read file continually
118
+ * @protected
119
+ */
120
+ protected continualRead(): void {
121
+ process.nextTick(async (): Promise<void> => {
122
+ let read: boolean = true
123
+ this.once('stop', (): boolean => read = false)
124
+ let isReachEnd: boolean = false
125
+ while (read || !isReachEnd) {
126
+ isReachEnd = await this.readBuffer()
127
+ if (isReachEnd) {
128
+ await new Promise(resolve => setTimeout(resolve, 1))
129
+ }
130
+ }
131
+ await this.readBufferFileHandle?.close()
132
+ this.readBufferFileHandle = null
133
+ this.readDone = true
134
+ this.emit('done')
135
+ })
136
+ }
137
+
138
+ /**
139
+ * Read the file straight
140
+ * @protected
141
+ */
142
+ protected straightRead(): void {
143
+ process.nextTick(async (): Promise<void> => {
144
+ let isReachEnd: boolean = false
145
+ let stopped: boolean = false
146
+ this.once('stop', (): boolean => stopped = true)
147
+ while (!isReachEnd && !stopped) {
148
+ isReachEnd = await this.readBuffer()
149
+ }
150
+ if (!stopped) await this.stop()
151
+ await this.readBufferFileHandle?.close()
152
+ this.readBufferFileHandle = null
153
+ this.readDone = true
154
+ this.emit('done')
155
+ })
156
+ }
157
+
158
+ /**
159
+ * Read packet data by record's offset and record's length
160
+ * @param offset
161
+ * @param length
162
+ */
163
+ public async readPacket(offset: number, length: number): Promise<Buffer> {
164
+ const fileHandle: FileHandle = await this.initReadPacketFileHandle()
165
+ const recordHeaderLength: number = 16
166
+ const packetLength: number = length - recordHeaderLength
167
+ const result: FileReadResult<Buffer> = await fileHandle.read({
168
+ buffer: Buffer.alloc(packetLength),
169
+ offset: 0,
170
+ length: packetLength,
171
+ position: offset + recordHeaderLength
172
+ })
173
+ await fileHandle.close()
174
+ return result.buffer
175
+ }
176
+
177
+ /**
178
+ * Start reading pcap
179
+ */
180
+ public async start(): Promise<void> {
181
+ await this.reset()
182
+ if (this.watch) {
183
+ this.continualRead()
184
+ } else {
185
+ this.straightRead()
186
+ }
187
+ }
188
+
189
+ /**
190
+ * Stop reading pcap
191
+ */
192
+ public async stop(): Promise<void> {
193
+ if (this.parser) {
194
+ this.emit('stop')
195
+ if (!this.readDone) await new Promise(resolve => this.once('done', resolve))
196
+ }
197
+ if (this.duplexPair) {
198
+ await new Promise<void>(resolve => {
199
+ if (this.writeStream.closed) return resolve()
200
+ this.writeStream.once('close', () => resolve())
201
+ this.writeStream.once('error', err => !!err)
202
+ this.writeStream.destroy()
203
+ })
204
+ await new Promise<void>(resolve => {
205
+ if (this.readStream.closed) return resolve()
206
+ this.readStream.once('close', () => resolve())
207
+ this.readStream.once('error', err => !!err)
208
+ this.readStream.destroy()
209
+ })
210
+ }
211
+ }
212
+
213
+ /**
214
+ * Close pcap reader
215
+ */
216
+ public async close(): Promise<void> {
217
+ await this.stop()
218
+ this.emit('close')
219
+ this.removeAllListeners()
220
+ }
221
+
222
+ public on(eventName: 'packet', listener: (pcapPacketInfo: IPcapPacketInfo) => void): this
223
+ public on(eventName: 'done', listener: (...args: any[]) => void): this
224
+ public on(eventName: 'stop', listener: (...args: any[]) => void): this
225
+ public on(eventName: 'close', listener: (...args: any[]) => void): this
226
+ public on(eventName: string, listener: (...args: any[]) => void): this {
227
+ super.on(eventName, listener)
228
+ return this
229
+ }
230
+
231
+ public once(eventName: 'packet', listener: (pcapPacketInfo: IPcapPacketInfo) => void): this
232
+ public once(eventName: 'done', listener: (...args: any[]) => void): this
233
+ public once(eventName: 'stop', listener: (...args: any[]) => void): this
234
+ public once(eventName: 'close', listener: (...args: any[]) => void): this
235
+ public once(eventName: string, listener: (...args: any[]) => void): this {
236
+ super.once(eventName, listener)
237
+ return this
238
+ }
239
+ }
@@ -0,0 +1,93 @@
1
+ import EventEmitter from 'events'
2
+ import {createWriteStream, WriteStream} from 'node:fs'
3
+ import {GeneratePCAPData, GeneratePCAPHeader} from './PCAPGenerator'
4
+ import {IPcapPacketInfo} from './interfaces/IPcapPacketInfo'
5
+
6
+ export interface IPcapWriterOptions {
7
+ filename: string
8
+ }
9
+
10
+ export class PcapWriter extends EventEmitter {
11
+
12
+ public readonly filename: string
13
+
14
+ protected readonly writeStream: WriteStream
15
+
16
+ protected closed: boolean = false
17
+
18
+ protected index: number = 0
19
+
20
+ protected offset: number = 0
21
+
22
+ public get wroteCount(): number {
23
+ return this.index
24
+ }
25
+
26
+ constructor(options: IPcapWriterOptions) {
27
+ super()
28
+ this.filename = options.filename
29
+ this.writeStream = createWriteStream(this.filename, {autoClose: false, flags: 'w'})
30
+ const header: Buffer = GeneratePCAPHeader()
31
+ this.writeStream.write(header)
32
+ this.offset += header.length
33
+ }
34
+
35
+ /**
36
+ * Write packet to pcap file
37
+ * @param packet
38
+ * @param seconds
39
+ * @param microseconds
40
+ */
41
+ public write(packet: Buffer, seconds: number, microseconds: number): void {
42
+ if (this.closed) return
43
+ this.index += 1
44
+ const startOffset: number = this.offset
45
+ const packetLength: number = packet.length
46
+ const pcapData: Buffer = GeneratePCAPData({
47
+ buffer: packet,
48
+ timestamp: Date.now(),
49
+ microsecond: {
50
+ seconds: seconds,
51
+ microseconds: microseconds
52
+ }
53
+ })
54
+ this.offset += pcapData.length
55
+ const packetOffset: number = this.offset - packetLength
56
+ const recordHeaderLength: number = packetOffset - startOffset
57
+ const wrotePacketInfo: IPcapPacketInfo = {
58
+ index: this.index,
59
+ offset: startOffset,
60
+ length: pcapData.length,
61
+ recordHeaderOffset: startOffset,
62
+ recordHeaderLength: recordHeaderLength,
63
+ packetOffset: packetOffset,
64
+ packetLength: packetLength,
65
+ seconds: seconds,
66
+ microseconds: microseconds,
67
+ packet: packet.toString('base64')
68
+ }
69
+ this.writeStream.write(pcapData, (): boolean => this.emit('packet', wrotePacketInfo))
70
+ }
71
+
72
+ /**
73
+ * Close pcap writer
74
+ */
75
+ public async close(): Promise<void> {
76
+ this.closed = true
77
+ return new Promise((resolve, reject): void => {
78
+ this.writeStream.close(err => err ? reject(err) : resolve())
79
+ })
80
+ }
81
+
82
+ public on(eventName: 'packet', listener: (wrotePacketInfo: IPcapPacketInfo) => void): this
83
+ public on(eventName: string, listener: (...args: any[]) => void): this {
84
+ super.on(eventName, listener)
85
+ return this
86
+ }
87
+
88
+ public once(eventName: 'packet', listener: (wrotePacketInfo: IPcapPacketInfo) => void): this
89
+ public once(eventName: string, listener: (...args: any[]) => void): this {
90
+ super.once(eventName, listener)
91
+ return this
92
+ }
93
+ }
@@ -0,0 +1,12 @@
1
+ export interface IPcapPacketInfo {
2
+ index: number
3
+ offset: number
4
+ length: number
5
+ recordHeaderOffset:number
6
+ recordHeaderLength:number
7
+ packetOffset: number
8
+ packetLength: number
9
+ seconds: number
10
+ microseconds: number
11
+ packet: string
12
+ }
@@ -0,0 +1,91 @@
1
+ import EventEmitter from 'events'
2
+ import {MessageClient} from 'socket-ipc'
3
+ import {PipeMessage, PipeMessageType} from './PipeMessage'
4
+ import {PipeMessageHandler} from './PipeMessageHandler'
5
+ import {SocketDownError} from '../../errors/SocketDownError'
6
+
7
+ export interface IPipeClientOptions {
8
+ id: string
9
+ socketPath: string
10
+ actions: Record<string, (...args: any[]) => Promise<any>>
11
+ }
12
+
13
+ export class PipeClient extends EventEmitter {
14
+
15
+ #connected: boolean
16
+
17
+ protected readonly id: string
18
+
19
+ protected readonly socketPath: string
20
+
21
+ protected readonly client: MessageClient
22
+
23
+ protected readonly pipeMessageHandler: PipeMessageHandler
24
+
25
+ public readonly actions: Record<string, (...args: any[]) => Promise<any>> = {}
26
+
27
+ public get connected(): boolean {
28
+ return this.#connected
29
+ }
30
+
31
+ constructor(options: IPipeClientOptions) {
32
+ super()
33
+ this.id = options.id
34
+ this.socketPath = options.socketPath
35
+ this.actions = options.actions
36
+ this.#connected = true
37
+ this.client = new MessageClient({path: this.socketPath})
38
+ this.pipeMessageHandler = new PipeMessageHandler(this, this.client)
39
+ this.client.on('close', (): boolean => this.#connected = false)
40
+ this.client.start().then((): void => {
41
+ this.client.send(Buffer.from(this.id).toString('base64'))
42
+ this.emit('ready')
43
+ })
44
+ }
45
+
46
+ /**
47
+ * Invoke server side action
48
+ * @param act
49
+ * @param args
50
+ */
51
+ public async invoke(act: string, ...args: any[]): Promise<any> {
52
+ return new Promise((resolve, reject): void => {
53
+ const pipeMessage: PipeMessage = new PipeMessage()
54
+ pipeMessage.type = PipeMessageType.REQUEST
55
+ pipeMessage.payload = [act, ...args]
56
+ this.once(pipeMessage.messageId, (resultOrError: any | Error): void => resultOrError instanceof Error ? reject(resultOrError) : resolve(resultOrError))
57
+ if (!this.connected) {
58
+ this.removeAllListeners(pipeMessage.messageId)
59
+ return reject(new SocketDownError('Socket is down'))
60
+ }
61
+ this.client.send(pipeMessage.serialize())
62
+ })
63
+ }
64
+
65
+ /**
66
+ * Notify event to pipe server
67
+ * @param event
68
+ * @param args
69
+ */
70
+ public notify(event: string, ...args: any[]): void {
71
+ const pipeMessage: PipeMessage = new PipeMessage()
72
+ pipeMessage.type = PipeMessageType.EVENT
73
+ pipeMessage.payload = [event, ...args]
74
+ if (this.connected) this.client.send(pipeMessage.serialize())
75
+ }
76
+
77
+ public on(eventName: 'ready', listener: () => void): this
78
+ public on(eventName: string, listener: (...args: any) => void): this
79
+ public on(eventName: string, listener: (...args: any) => void): this {
80
+ super.on(eventName, listener)
81
+ return this
82
+ }
83
+
84
+ public once(eventName: 'ready', listener: () => void): this
85
+ public once(eventName: string, listener: (...args: any) => void): this
86
+ public once(eventName: string, listener: (...args: any) => void): this {
87
+ super.once(eventName, listener)
88
+ return this
89
+ }
90
+
91
+ }
@@ -0,0 +1,99 @@
1
+ import EventEmitter from 'events'
2
+ import {MessageConnection} from 'socket-ipc'
3
+ import {PipeMessage, PipeMessageType} from './PipeMessage'
4
+ import {PipeMessageHandler} from './PipeMessageHandler'
5
+ import {SocketDownError} from '../../errors/SocketDownError'
6
+
7
+ export interface IPipeClientSocketOptions {
8
+ id: string
9
+ connection: MessageConnection
10
+ actions: Record<string, (...args: any[]) => Promise<any>>
11
+ }
12
+
13
+ export class PipeClientSocket extends EventEmitter {
14
+
15
+ readonly #id: string
16
+
17
+ readonly #connection: MessageConnection
18
+
19
+ #connected: boolean
20
+
21
+ protected readonly pipeMessageHandler: PipeMessageHandler
22
+
23
+ public readonly actions: Record<string, (...args: any[]) => Promise<any>> = {}
24
+
25
+ /**
26
+ * ClientId
27
+ */
28
+ public get id(): string {
29
+ return this.#id
30
+ }
31
+
32
+ /**
33
+ * Is client socket connected
34
+ */
35
+ public get connected(): boolean {
36
+ return this.#connected
37
+ }
38
+
39
+ constructor(options: IPipeClientSocketOptions) {
40
+ super()
41
+ this.#id = options.id
42
+ this.#connection = options.connection
43
+ this.actions = options.actions ? options.actions : {}
44
+ this.#connected = true
45
+ this.#connection
46
+ .on('error', (err: Error): boolean => this.emit('error', err))
47
+ .once('close', (): void => {
48
+ this.#connected = false
49
+ this.emit('disconnect')
50
+ this.removeAllListeners()
51
+ })
52
+ this.pipeMessageHandler = new PipeMessageHandler(this, this.#connection)
53
+ }
54
+
55
+ /**
56
+ * Invoke client side action
57
+ * @param act
58
+ * @param args
59
+ */
60
+ public async invoke(act: string, ...args: any[]): Promise<any> {
61
+ return new Promise((resolve, reject): void => {
62
+ const pipeMessage: PipeMessage = new PipeMessage()
63
+ pipeMessage.type = PipeMessageType.REQUEST
64
+ pipeMessage.payload = [act, ...args]
65
+ this.once(pipeMessage.messageId, (resultOrError: any | Error): void => resultOrError instanceof Error ? reject(resultOrError) : resolve(resultOrError))
66
+ if (!this.connected) {
67
+ this.removeAllListeners(pipeMessage.messageId)
68
+ return reject(new SocketDownError('Socket is down'))
69
+ }
70
+ this.#connection.send(pipeMessage.serialize())
71
+ })
72
+ }
73
+
74
+ /**
75
+ * Notify event to pipe client
76
+ * @param event
77
+ * @param args
78
+ */
79
+ public notify(event: string, ...args: any[]): void {
80
+ const pipeMessage: PipeMessage = new PipeMessage()
81
+ pipeMessage.type = PipeMessageType.EVENT
82
+ pipeMessage.payload = [event, ...args]
83
+ if (this.connected) this.#connection.send(pipeMessage.serialize())
84
+ }
85
+
86
+ public on(eventName: 'disconnect', listener: () => void): this
87
+ public on(eventName: string, listener: (...args: any) => void): this
88
+ public on(eventName: string, listener: (...args: any) => void): this {
89
+ super.on(eventName, listener)
90
+ return this
91
+ }
92
+
93
+ public once(eventName: 'disconnect', listener: () => void): this
94
+ public once(eventName: string, listener: (...args: any) => void): this
95
+ public once(eventName: string, listener: (...args: any) => void): this {
96
+ super.once(eventName, listener)
97
+ return this
98
+ }
99
+ }