@libp2p/tcp 0.0.0 → 1.0.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.
- package/.aegir.cjs +8 -0
- package/.github/workflows/main.yml +125 -0
- package/CHANGELOG.md +453 -0
- package/LICENSE +2 -0
- package/LICENSE-APACHE +5 -0
- package/LICENSE-MIT +19 -0
- package/README.md +114 -0
- package/dist/src/constants.d.ts +4 -0
- package/dist/src/constants.d.ts.map +1 -0
- package/dist/src/constants.js +6 -0
- package/dist/src/constants.js.map +1 -0
- package/dist/src/index.d.ts +36 -0
- package/dist/src/index.d.ts.map +1 -0
- package/dist/src/index.js +100 -0
- package/dist/src/index.js.map +1 -0
- package/dist/src/listener.d.ts +12 -0
- package/dist/src/listener.d.ts.map +1 -0
- package/dist/src/listener.js +131 -0
- package/dist/src/listener.js.map +1 -0
- package/dist/src/socket-to-conn.d.ts +16 -0
- package/dist/src/socket-to-conn.d.ts.map +1 -0
- package/dist/src/socket-to-conn.js +96 -0
- package/dist/src/socket-to-conn.js.map +1 -0
- package/dist/src/utils.d.ts +10 -0
- package/dist/src/utils.d.ts.map +1 -0
- package/dist/src/utils.js +35 -0
- package/dist/src/utils.js.map +1 -0
- package/dist/tsconfig.tsbuildinfo +1 -0
- package/package.json +83 -4
- package/src/constants.ts +6 -0
- package/src/index.ts +147 -0
- package/src/listener.ts +172 -0
- package/src/socket-to-conn.ts +127 -0
- package/src/utils.ts +44 -0
- package/tsconfig.json +12 -0
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"program":{"fileNames":["../node_modules/typescript/lib/lib.es5.d.ts","../node_modules/typescript/lib/lib.es2015.d.ts","../node_modules/typescript/lib/lib.es2016.d.ts","../node_modules/typescript/lib/lib.es2017.d.ts","../node_modules/typescript/lib/lib.es2018.d.ts","../node_modules/typescript/lib/lib.es2019.d.ts","../node_modules/typescript/lib/lib.es2020.d.ts","../node_modules/typescript/lib/lib.dom.d.ts","../node_modules/typescript/lib/lib.dom.iterable.d.ts","../node_modules/typescript/lib/lib.es2015.core.d.ts","../node_modules/typescript/lib/lib.es2015.collection.d.ts","../node_modules/typescript/lib/lib.es2015.generator.d.ts","../node_modules/typescript/lib/lib.es2015.iterable.d.ts","../node_modules/typescript/lib/lib.es2015.promise.d.ts","../node_modules/typescript/lib/lib.es2015.proxy.d.ts","../node_modules/typescript/lib/lib.es2015.reflect.d.ts","../node_modules/typescript/lib/lib.es2015.symbol.d.ts","../node_modules/typescript/lib/lib.es2015.symbol.wellknown.d.ts","../node_modules/typescript/lib/lib.es2016.array.include.d.ts","../node_modules/typescript/lib/lib.es2017.object.d.ts","../node_modules/typescript/lib/lib.es2017.sharedmemory.d.ts","../node_modules/typescript/lib/lib.es2017.string.d.ts","../node_modules/typescript/lib/lib.es2017.intl.d.ts","../node_modules/typescript/lib/lib.es2017.typedarrays.d.ts","../node_modules/typescript/lib/lib.es2018.asyncgenerator.d.ts","../node_modules/typescript/lib/lib.es2018.asynciterable.d.ts","../node_modules/typescript/lib/lib.es2018.intl.d.ts","../node_modules/typescript/lib/lib.es2018.promise.d.ts","../node_modules/typescript/lib/lib.es2018.regexp.d.ts","../node_modules/typescript/lib/lib.es2019.array.d.ts","../node_modules/typescript/lib/lib.es2019.object.d.ts","../node_modules/typescript/lib/lib.es2019.string.d.ts","../node_modules/typescript/lib/lib.es2019.symbol.d.ts","../node_modules/typescript/lib/lib.es2020.bigint.d.ts","../node_modules/typescript/lib/lib.es2020.promise.d.ts","../node_modules/typescript/lib/lib.es2020.sharedmemory.d.ts","../node_modules/typescript/lib/lib.es2020.string.d.ts","../node_modules/typescript/lib/lib.es2020.symbol.wellknown.d.ts","../node_modules/typescript/lib/lib.es2020.intl.d.ts","../node_modules/typescript/lib/lib.esnext.intl.d.ts","../src/constants.ts","../node_modules/@multiformats/multiaddr/dist/src/protocols-table.d.ts","../node_modules/@multiformats/multiaddr/dist/src/index.d.ts","../node_modules/@multiformats/mafmt/dist/src/index.d.ts","../node_modules/err-code/dist/index.d.ts","../node_modules/@types/ms/index.d.ts","../node_modules/@types/debug/index.d.ts","../node_modules/abortable-iterator/index.d.ts","../node_modules/@libp2p/utils/dist/src/ip-port-to-multiaddr.d.ts","../node_modules/@types/node/assert.d.ts","../node_modules/@types/node/assert/strict.d.ts","../node_modules/@types/node/globals.d.ts","../node_modules/@types/node/async_hooks.d.ts","../node_modules/@types/node/buffer.d.ts","../node_modules/@types/node/child_process.d.ts","../node_modules/@types/node/cluster.d.ts","../node_modules/@types/node/console.d.ts","../node_modules/@types/node/constants.d.ts","../node_modules/@types/node/crypto.d.ts","../node_modules/@types/node/dgram.d.ts","../node_modules/@types/node/diagnostics_channel.d.ts","../node_modules/@types/node/dns.d.ts","../node_modules/@types/node/dns/promises.d.ts","../node_modules/@types/node/domain.d.ts","../node_modules/@types/node/events.d.ts","../node_modules/@types/node/fs.d.ts","../node_modules/@types/node/fs/promises.d.ts","../node_modules/@types/node/http.d.ts","../node_modules/@types/node/http2.d.ts","../node_modules/@types/node/https.d.ts","../node_modules/@types/node/inspector.d.ts","../node_modules/@types/node/module.d.ts","../node_modules/@types/node/net.d.ts","../node_modules/@types/node/os.d.ts","../node_modules/@types/node/path.d.ts","../node_modules/@types/node/perf_hooks.d.ts","../node_modules/@types/node/process.d.ts","../node_modules/@types/node/punycode.d.ts","../node_modules/@types/node/querystring.d.ts","../node_modules/@types/node/readline.d.ts","../node_modules/@types/node/repl.d.ts","../node_modules/@types/node/stream.d.ts","../node_modules/@types/node/stream/promises.d.ts","../node_modules/@types/node/stream/consumers.d.ts","../node_modules/@types/node/stream/web.d.ts","../node_modules/@types/node/string_decoder.d.ts","../node_modules/@types/node/timers.d.ts","../node_modules/@types/node/timers/promises.d.ts","../node_modules/@types/node/tls.d.ts","../node_modules/@types/node/trace_events.d.ts","../node_modules/@types/node/tty.d.ts","../node_modules/@types/node/url.d.ts","../node_modules/@types/node/util.d.ts","../node_modules/@types/node/v8.d.ts","../node_modules/@types/node/vm.d.ts","../node_modules/@types/node/wasi.d.ts","../node_modules/@types/node/worker_threads.d.ts","../node_modules/@types/node/zlib.d.ts","../node_modules/@types/node/globals.global.d.ts","../node_modules/@types/node/index.d.ts","../node_modules/multiformats/types/bases/interface.d.ts","../node_modules/multiformats/types/hashes/interface.d.ts","../node_modules/multiformats/types/cid.d.ts","../node_modules/@libp2p/interfaces/dist/src/peer-id/index.d.ts","../node_modules/@libp2p/interfaces/dist/src/stream-muxer/index.d.ts","../node_modules/@libp2p/interfaces/dist/src/connection/status.d.ts","../node_modules/@libp2p/interfaces/dist/src/connection/index.d.ts","../node_modules/@libp2p/interfaces/dist/src/index.d.ts","../node_modules/@libp2p/interfaces/dist/src/transport/index.d.ts","../src/socket-to-conn.ts","../src/utils.ts","../src/listener.ts","../src/index.ts","../node_modules/@sinonjs/fake-timers/types/fake-timers-src.d.ts","../node_modules/@types/sinon/index.d.ts","../node_modules/@libp2p/interface-compliance-tests/dist/src/index.d.ts","../node_modules/@libp2p/interface-compliance-tests/dist/src/transport/index.d.ts","../test/compliance.spec.ts","../node_modules/@types/chai/index.d.ts","../node_modules/@types/chai-as-promised/index.d.ts","../node_modules/chai-parentheses/index.d.ts","../node_modules/@types/chai-subset/index.d.ts","../node_modules/chai-bites/index.d.ts","../node_modules/@types/chai-string/index.d.ts","../node_modules/aegir/dist/utils/chai.d.ts","../node_modules/@libp2p/interface-compliance-tests/dist/src/transport/utils/index.d.ts","../test/connection.spec.ts","../test/filter.spec.ts","../node_modules/it-pipe/index.d.ts","../node_modules/streaming-iterables/dist/index.d.ts","../test/listen-dial.spec.ts","../node_modules/@types/istanbul-lib-coverage/index.d.ts","../node_modules/@types/json-schema/index.d.ts","../node_modules/@types/json5/index.d.ts","../node_modules/@types/long/index.d.ts","../node_modules/@types/minimatch/index.d.ts","../node_modules/@types/minimist/index.d.ts","../node_modules/@types/mocha/index.d.ts","../node_modules/@types/normalize-package-data/index.d.ts","../node_modules/@types/parse-json/index.d.ts","../node_modules/@types/retry/index.d.ts","../node_modules/@types/yargs-parser/index.d.ts","../node_modules/@types/yargs/index.d.ts","../node_modules/@types/yauzl/index.d.ts"],"fileInfos":[{"version":"aa9fb4c70f369237c2f45f9d969c9a59e0eae9a192962eb48581fe864aa609db","affectsGlobalScope":true},"dc47c4fa66b9b9890cf076304de2a9c5201e94b740cffdf09f87296d877d71f6","7a387c58583dfca701b6c85e0adaf43fb17d590fb16d5b2dc0a2fbd89f35c467","8a12173c586e95f4433e0c6dc446bc88346be73ffe9ca6eec7aa63c8f3dca7f9","5f4e733ced4e129482ae2186aae29fde948ab7182844c3a5a51dd346182c7b06","e6b724280c694a9f588847f754198fb96c43d805f065c3a5b28bbc9594541c84","e21c071ca3e1b4a815d5f04a7475adcaeea5d64367e840dd0154096d705c3940",{"version":"e54c8715a4954cfdc66cd69489f2b725c09ebf37492dbd91cff0a1688b1159e8","affectsGlobalScope":true},{"version":"e34eb9339171ec45da2801c1967e4d378bd61a1dceaa1b1b4e1b6d28cb9ca962","affectsGlobalScope":true},{"version":"51b8b27c21c066bf877646e320bf6a722b80d1ade65e686923cd9d4494aef1ca","affectsGlobalScope":true},{"version":"43fb1d932e4966a39a41b464a12a81899d9ae5f2c829063f5571b6b87e6d2f9c","affectsGlobalScope":true},{"version":"cdccba9a388c2ee3fd6ad4018c640a471a6c060e96f1232062223063b0a5ac6a","affectsGlobalScope":true},{"version":"2c8c5ee58f30e7c944e04ab1fb5506fdbb4dd507c9efa6972cf4b91cec90c503","affectsGlobalScope":true},{"version":"2bb4b3927299434052b37851a47bf5c39764f2ba88a888a107b32262e9292b7c","affectsGlobalScope":true},{"version":"810627a82ac06fb5166da5ada4159c4ec11978dfbb0805fe804c86406dab8357","affectsGlobalScope":true},{"version":"62d80405c46c3f4c527ee657ae9d43fda65a0bf582292429aea1e69144a522a6","affectsGlobalScope":true},{"version":"3013574108c36fd3aaca79764002b3717da09725a36a6fc02eac386593110f93","affectsGlobalScope":true},{"version":"75ec0bdd727d887f1b79ed6619412ea72ba3c81d92d0787ccb64bab18d261f14","affectsGlobalScope":true},{"version":"3be5a1453daa63e031d266bf342f3943603873d890ab8b9ada95e22389389006","affectsGlobalScope":true},{"version":"17bb1fc99591b00515502d264fa55dc8370c45c5298f4a5c2083557dccba5a2a","affectsGlobalScope":true},{"version":"7ce9f0bde3307ca1f944119f6365f2d776d281a393b576a18a2f2893a2d75c98","affectsGlobalScope":true},{"version":"6a6b173e739a6a99629a8594bfb294cc7329bfb7b227f12e1f7c11bc163b8577","affectsGlobalScope":true},{"version":"12a310447c5d23c7d0d5ca2af606e3bd08afda69100166730ab92c62999ebb9d","affectsGlobalScope":true},{"version":"b0124885ef82641903d232172577f2ceb5d3e60aed4da1153bab4221e1f6dd4e","affectsGlobalScope":true},{"version":"0eb85d6c590b0d577919a79e0084fa1744c1beba6fd0d4e951432fa1ede5510a","affectsGlobalScope":true},{"version":"da233fc1c8a377ba9e0bed690a73c290d843c2c3d23a7bd7ec5cd3d7d73ba1e0","affectsGlobalScope":true},{"version":"df9c8a72ca8b0ed62f5470b41208a0587f0f73f0a7db28e5a1272cf92537518e","affectsGlobalScope":true},{"version":"bb2d3fb05a1d2ffbca947cc7cbc95d23e1d053d6595391bd325deb265a18d36c","affectsGlobalScope":true},{"version":"c80df75850fea5caa2afe43b9949338ce4e2de086f91713e9af1a06f973872b8","affectsGlobalScope":true},{"version":"9d57b2b5d15838ed094aa9ff1299eecef40b190722eb619bac4616657a05f951","affectsGlobalScope":true},{"version":"6c51b5dd26a2c31dbf37f00cfc32b2aa6a92e19c995aefb5b97a3a64f1ac99de","affectsGlobalScope":true},{"version":"93544ca2f26a48716c1b6c5091842cad63129daac422dfa4bc52460465f22bb1","affectsGlobalScope":true},{"version":"2ad234885a4240522efccd77de6c7d99eecf9b4de0914adb9a35c0c22433f993","affectsGlobalScope":true},{"version":"1b3fe904465430e030c93239a348f05e1be80640d91f2f004c3512c2c2c89f34","affectsGlobalScope":true},{"version":"7435b75fdf3509622e79622dbe5091cf4b09688410ee2034e4fc17d0c99d0862","affectsGlobalScope":true},{"version":"e7e8e1d368290e9295ef18ca23f405cf40d5456fa9f20db6373a61ca45f75f40","affectsGlobalScope":true},{"version":"faf0221ae0465363c842ce6aa8a0cbda5d9296940a8e26c86e04cc4081eea21e","affectsGlobalScope":true},{"version":"06393d13ea207a1bfe08ec8d7be562549c5e2da8983f2ee074e00002629d1871","affectsGlobalScope":true},{"version":"9f1817f7c3f02f6d56e0f403b927e90bb133f371dcebc36fa7d6d208ef6899da","affectsGlobalScope":true},{"version":"4632665b87204bb1caa8b44d165bce0c50dfab177df5b561b345a567cabacf9a","affectsGlobalScope":true},"9726f71eafa561120b0b8510f0a7561ffb6de67c1f3e3d1621b4689a7cfb5739","dcaaf86cdcf77baf88172a23d27df51c284afd72935bd1d57c7a9edd2f4f6007","6a30d3048a516174263bd3b4d916ed87cd418a67226d08f5cb4e128819d047f4","755a8200628faa558e2586c51d29724e972772cc1ef39f7862f07d680952524a","a02a19deaa2c497d36c136459dd2561b57ebb22075ac0d6aad176430a8639aa1","6a9c5127096b35264eb7cd21b2417bfc1d42cceca9ba4ce2bb0c3410b7816042","78828b06c0d3b586954015e9ebde5480b009e166c71244763bda328ec0920f41","f1304f38533801f935674f1f096ccd8d12e98e1daeb7ad7582103e5d704c0a42","78ef5cd8d627b2c816922af04603cae53caaf31d70de765871299a7748e9cfa2","0d5a2ee1fdfa82740e0103389b9efd6bfe145a20018a2da3c02b89666181f4d9","a69c09dbea52352f479d3e7ac949fde3d17b195abe90b045d619f747b38d6d1a",{"version":"92d63add669d18ebc349efbacd88966d6f2ccdddfb1b880b2db98ae3aa7bf7c4","affectsGlobalScope":true},"ccc94049a9841fe47abe5baef6be9a38fc6228807974ae675fb15dc22531b4be",{"version":"9acfe4d1ff027015151ce81d60797b04b52bffe97ad8310bb0ec2e8fd61e1303","affectsGlobalScope":true},"95843d5cfafced8f3f8a5ce57d2335f0bcd361b9483587d12a25e4bd403b8216","afc6e96061af46bcff47246158caee7e056f5288783f2d83d6858cd25be1c565",{"version":"34f5bcac12b36d70304b73de5f5aab3bb91bd9919f984be80579ebcad03a624e","affectsGlobalScope":true},"82408ed3e959ddc60d3e9904481b5a8dc16469928257af22a3f7d1a3bc7fd8c4","2f520601649a893e6a49a8851ebfcf4be8ce090dc1281c2a08a871cb04e8251f","f50c975ab7b50e25a69e3d8a3773894125b44e9698924105f23b812bf7488baf","2b8c764f856a1dd0a9a2bf23e5efddbff157de8138b0754010be561ae5fcaa90","76650408392bf49a8fbf3e2b6b302712a92d76af77b06e2da1cc8077359c4409","0af3121e68297b2247dd331c0d24dba599e50736a7517a5622d5591aae4a3122","6972fca26f6e9bd56197568d4379f99071a90766e06b4fcb5920a0130a9202be",{"version":"4a2628e95962c8ab756121faa3ac2ed348112ff7a87b5c286dd2cc3326546b4c","affectsGlobalScope":true},"80793b2277f31baa199234daed806fff0fb11491d1ebd3357e520c3558063f00","a049a59a02009fc023684fcfaf0ac526fe36c35dcc5d2b7d620c1750ba11b083","b9b963043551b034abd9e7c6d859f7a81d99479fde938d983114d167d0644a78","160cc6e3d06938535bc887754afe5798c22d81ce83a9792ebfe2371a70f2ffc2","4b9a003b5c556c96784132945bb41c655ea11273b1917f5c8d0c154dd5fd20dd","a458dc78104cc80048ac24fdc02fe6dce254838094c2f25641b3f954d9721241",{"version":"e8b18c6385ff784228a6f369694fcf1a6b475355ba89090a88de13587a9391d5","affectsGlobalScope":true},"eecd493fc62c4dba3d988e2d7dff63299bf12ab49f5c9021dfef8dcc1ff2089e","abc1c425b2ad6720433f40f1877abfa4223f0f3dd486c9c28c492179ca183cb6","945a841f9a591197154c85386bc5a1467d42d325104bb36db51bc566bbb240be","10c39ce1df102994b47d4bc0c71aa9a6aea76f4651a5ec51914431f50bc883a1",{"version":"8207e7e6db9aa5fc7e61c8f17ba74cf9c115d26f51f91ee93f790815a7ea9dfb","affectsGlobalScope":true},"9f1069b9e2c051737b1f9b4f1baf50e4a63385a6a89c32235549ae87fc3d5492","ee18f2da7a037c6ceeb112a084e485aead9ea166980bf433474559eac1b46553","29c2706fa0cc49a2bd90c83234da33d08bb9554ecec675e91c1f85087f5a5324","0acbf26bf958f9e80c1ffa587b74749d2697b75b484062d36e103c137c562bc3","d7838022c7dab596357a9604b9c6adffe37dc34085ce0779c958ce9545bd7139","1b952304137851e45bc009785de89ada562d9376177c97e37702e39e60c2f1ff",{"version":"806ef4cac3b3d9fa4a48d849c8e084d7c72fcd7b16d76e06049a9ed742ff79c0","affectsGlobalScope":true},"a7971f9fb2a32ec7788ec6cda9d7a33c02023dfe9a62db2030ad1359649d8050","c33a6ea7147af60d8e98f1ac127047f4b0d4e2ce28b8f08ff3de07ca7cc00637",{"version":"b42b47e17b8ece2424ae8039feb944c2e3ba4b262986aebd582e51efbdca93dc","affectsGlobalScope":true},"664d8f2d59164f2e08c543981453893bc7e003e4dfd29651ce09db13e9457980","2408611d9b4146e35d1dbd1f443ccd8e187c74614a54b80300728277529dbf11","998a3de5237518c0b3ac00a11b3b4417affb008aa20aedee52f3fdae3cb86151","ad41008ffe077206e1811fc873f4d9005b5fd7f6ab52bb6118fef600815a5cb4","d88ecca73348e7c337541c4b8b60a50aca5e87384f6b8a422fc6603c637e4c21","badae0df9a8016ac36994b0a0e7b82ba6aaa3528e175a8c3cb161e4683eec03e","c3db860bcaaaeb3bbc23f353bbda1f8ab82756c8d5e973bebb3953cb09ea68f2","235a53595bd20b0b0eeb1a29cb2887c67c48375e92f03749b2488fbd46d0b1a0","bc09393cd4cd13f69cf1366d4236fbae5359bb550f0de4e15767e9a91d63dfb1","9c266243b01545e11d2733a55ad02b4c00ecdbda99c561cd1674f96e89cdc958","c71155c05fc76ff948a4759abc1cb9feec036509f500174bc18dad4c7827a60c",{"version":"ab9b9a36e5284fd8d3bf2f7d5fcbc60052f25f27e4d20954782099282c60d23e","affectsGlobalScope":true},"1cdb8f094b969dcc183745dc88404e2d8fcf2a858c6e7cc2441011476573238e","930446bf32192f698b78f8ea4b309d8c2cfe02ab5ad78e4db907417405ebf5e7","a5de53bdfcdca7af39e735be7bbd736c38b1e34eeec36bd6a835a0e37ba23705","ef706b4970dd8bf7ab414bb981993d4f379845f3592622237bad1b3332fde794","254c3d289f204ba745b8353d4530566df1adcf7666ba29294f733a2e1b3cdf77","5fb8b088382f73d2ff63558d14494c5defd5fa783adbff4b72f3cfb02ee9fba6","6666d48a46dbfd565490e7f8789093e2392c5bdcc3a380dfdd91c77237e43101","ecbf5c1c87fe17bc253621fee49d2378b947c1a5d6aaa726efebf9df099e0da5","ce76ac1c5c57e5c082967bd7734e32b4c30efd28c5ea828da241ad8059cef072","3d4abadc850c4e1c56b345c1c44d434134888c7cdd1b496e1d5c5bfbe5321356","d4694a1562bc15a6fddfe69b1be361b6d8f1300cbf72a691679fe6a2d9880ce3","833f7af32638188a31eba3202cb8cf9d1999e9f10e3d28458350e72f68ec2682","5a12a1331531cd0f97024b7670f72e0a0d707d16bf36c6ea06e0a5dcb03c14a1","70b1774a7c4bd1ab37744878a763d3aa1d86fcf5d257e3bbd86a70900ed419d4","6b40029289530423f407a22755c85b81740f9acfd88d2b53564f8c1657c26660","50a5b297e6c91df4f6068d98467a5e7ba4eeb888b9376757734dd4b1dfcdacd4","9f1489f84adf8947a8ec1c8cd4ac71623ca56845fd440cc3a8b562b8be662bd3","937b7451b6161a45c581d4127ca105f5802a815d48b2a45ccf6e9a695e10b521","a88d6fa89961508357e6bbadb83f4045d948899fbe824586b7b0de4d60cf5928",{"version":"c8747693e5872ad5ef3aa016731a06915e1c34dae987829d9aa5bd40c7a2c54b","affectsGlobalScope":true},{"version":"f6ae17283c6912c202004178339d6d22f8c9edfe4e335f9f11b555c631633daf","affectsGlobalScope":true},{"version":"c1f79c6d85cd84518fd7349588b3c61bdc189f58e3866f925cbf4631c81e31f9","affectsGlobalScope":true},{"version":"f4c0db3a49cea9babd5d224ba14243a6a6119bf65a65198994033aaea3a60a71","affectsGlobalScope":true},{"version":"42f28f4cfca4a70bd89c1d55d76c7978275030b894f6793f36890265c11a83a4","affectsGlobalScope":true},{"version":"41071d2f1a39386d10bf36d1ba4712ad42a900047f16a109936df9e48f13673e","affectsGlobalScope":true},"61f705a779045717e1a18c75456c2a7ca52eb50e0694a3da86fddfe67503bff6","d0c03fc361cffc2c598548d4946ddc36a54dd066860706dd367065ae6b412fb9","68b2cfd3fad9494b84088079e906178be03ab9f44a1480c15000387953c144e1","0ae765509269c1b8af1ecde9315ec9167e8f851ecc813717cd57fc1198296ed9","b413b4ac5cc2c9ffeb8d6b9141abf53568e79751f705886a8c34a6ecf1814da1","58749b35899549ee2e69d3d41c1fc31d878deef7a70520ec1cee312c24d1c57f","9908868bbedef040a021a24a5b3a61ad7e7526f4d0da8b8452e719fae6111e30","8b06ac3faeacb8484d84ddb44571d8f410697f98d7bfa86c0fda60373a9f5215","0359682c54e487c4cab2b53b2b4d35cc8dea4d9914bc6abcdb5701f8b8e745a4","96d14f21b7652903852eef49379d04dbda28c16ed36468f8c9fa08f7c14c9538","e8465811693dfe4e96ef2b3dffda539d6edfe896961b7af37b44db2c0e48532b","8841e2aa774b89bd23302dede20663306dc1b9902431ac64b24be8b8d0e3f649","209e814e8e71aec74f69686a9506dd7610b97ab59dcee9446266446f72a76d05",{"version":"e8bf92aabac2b11e1bf60a0161039cd6f5f0cd9713863ca2289dd7b10eddece8","affectsGlobalScope":true},"6fa0008bf91a4cc9c8963bace4bba0bd6865cbfa29c3e3ccc461155660fb113a","2b8264b2fefd7367e0f20e2c04eed5d3038831fe00f5efbc110ff0131aab899b","58a3914b1cce4560d9ad6eee2b716caaa030eda0a90b21ca2457ea9e2783eaa3","f7e133b20ee2669b6c0e5d7f0cd510868c57cd64b283e68c7f598e30ce9d76d2","45627c7d24633686b0274d9402652930ba2e4fe74df04f114393ae121d1e8afe","b2d70a269840a9528db473ac7565442434333a05c1f66801a7a672e82beb903e"],"options":{"composite":true,"declaration":true,"declarationMap":true,"emitDeclarationOnly":false,"esModuleInterop":true,"importsNotUsedAsValues":2,"module":6,"noEmitOnError":true,"noFallthroughCasesInSwitch":true,"noImplicitReturns":false,"noUnusedLocals":true,"noUnusedParameters":false,"outDir":"./","removeComments":false,"skipLibCheck":true,"sourceMap":true,"strict":true,"stripInternal":true,"target":7},"fileIdsList":[[93],[43,93,109,116],[93,109],[43,93,104,105,106],[93,101,102,103],[43,65,93,100,107,108],[43,93],[42,93],[93,119],[46,93],[50,93],[53,93],[54,59,93],[55,65,66,73,82,92,93],[55,56,65,73,93],[57,93],[58,59,66,74,93],[59,82,89,93],[60,62,65,73,93],[61,93],[62,63,93],[64,65,93],[65,93],[65,66,67,82,92,93],[65,66,67,82,93],[68,73,82,92,93],[65,66,68,69,73,82,89,92,93],[68,70,82,89,92,93],[50,51,52,53,54,55,56,57,58,59,60,61,62,63,64,65,66,67,68,69,70,71,72,73,74,75,76,77,78,79,80,81,82,83,84,85,86,87,88,89,90,91,92,93,94,95,96,97,98,99],[65,71,93],[72,92,93],[62,65,73,82,93],[74,93],[75,93],[53,76,93],[77,91,93,97],[78,93],[79,93],[65,80,93],[80,81,93,95],[65,82,83,84,93],[82,84,93],[82,83,93],[85,93],[86,93],[65,87,88,93],[87,88,93],[59,73,89,93],[90,93],[73,91,93],[54,68,79,92,93],[59,93],[82,93,94],[93,95],[93,96],[54,59,65,67,76,82,92,93,95,97],[82,93,98],[93,114],[93,142],[65,82,93,100],[93,119,120,121,122,123,124],[93,119,120],[93,101,102],[41,43,44,45,47,48,73,93,107,109,110,111,112],[41,43,47,65,73,93,107,109,110,111],[41,43,47,48,49,73,93,109],[43,74,93],[43,73,93,113,115,117],[43,93,107,113,125,126],[43,93,113,125,126],[43,74,75,93,113,125,126,129,130]],"referencedMap":[[116,1],[117,2],[126,3],[107,4],[106,1],[108,1],[104,5],[105,1],[109,6],[49,7],[44,7],[43,8],[42,1],[114,1],[120,9],[124,9],[122,9],[119,1],[47,10],[132,1],[133,1],[134,1],[135,1],[136,1],[137,1],[138,1],[46,1],[50,11],[51,11],[53,12],[54,13],[55,14],[56,15],[57,16],[58,17],[59,18],[60,19],[61,20],[62,21],[63,21],[64,22],[65,23],[66,24],[67,25],[52,1],[99,1],[68,26],[69,27],[70,28],[100,29],[71,30],[72,31],[73,32],[74,33],[75,34],[76,35],[77,36],[78,37],[79,38],[80,39],[81,40],[82,41],[84,42],[83,43],[85,44],[86,45],[87,46],[88,47],[89,48],[90,49],[91,50],[92,51],[93,52],[94,53],[95,54],[96,55],[97,56],[98,57],[139,1],[140,1],[141,1],[115,58],[142,1],[143,59],[144,60],[48,1],[125,61],[123,9],[121,62],[45,1],[129,1],[101,1],[103,63],[102,1],[130,1],[8,1],[9,1],[11,1],[10,1],[2,1],[12,1],[13,1],[14,1],[15,1],[16,1],[17,1],[18,1],[19,1],[3,1],[4,1],[23,1],[20,1],[21,1],[22,1],[24,1],[25,1],[26,1],[5,1],[27,1],[28,1],[29,1],[30,1],[6,1],[31,1],[32,1],[33,1],[34,1],[7,1],[39,1],[35,1],[36,1],[37,1],[38,1],[1,1],[40,1],[41,1],[113,64],[112,65],[110,66],[111,67],[118,68],[127,69],[128,70],[131,71]],"exportedModulesMap":[[116,1],[117,2],[126,3],[107,4],[106,1],[108,1],[104,5],[105,1],[109,6],[49,7],[44,7],[43,8],[42,1],[114,1],[120,9],[124,9],[122,9],[119,1],[47,10],[132,1],[133,1],[134,1],[135,1],[136,1],[137,1],[138,1],[46,1],[50,11],[51,11],[53,12],[54,13],[55,14],[56,15],[57,16],[58,17],[59,18],[60,19],[61,20],[62,21],[63,21],[64,22],[65,23],[66,24],[67,25],[52,1],[99,1],[68,26],[69,27],[70,28],[100,29],[71,30],[72,31],[73,32],[74,33],[75,34],[76,35],[77,36],[78,37],[79,38],[80,39],[81,40],[82,41],[84,42],[83,43],[85,44],[86,45],[87,46],[88,47],[89,48],[90,49],[91,50],[92,51],[93,52],[94,53],[95,54],[96,55],[97,56],[98,57],[139,1],[140,1],[141,1],[115,58],[142,1],[143,59],[144,60],[48,1],[125,61],[123,9],[121,62],[45,1],[129,1],[101,1],[103,63],[102,1],[130,1],[8,1],[9,1],[11,1],[10,1],[2,1],[12,1],[13,1],[14,1],[15,1],[16,1],[17,1],[18,1],[19,1],[3,1],[4,1],[23,1],[20,1],[21,1],[22,1],[24,1],[25,1],[26,1],[5,1],[27,1],[28,1],[29,1],[30,1],[6,1],[31,1],[32,1],[33,1],[34,1],[7,1],[39,1],[35,1],[36,1],[37,1],[38,1],[1,1],[40,1],[41,1],[113,64],[112,65],[110,66],[111,67],[118,68],[127,69],[128,70],[131,71]],"semanticDiagnosticsPerFile":[116,117,126,107,106,108,104,105,109,49,44,43,42,114,120,124,122,119,47,132,133,134,135,136,137,138,46,50,51,53,54,55,56,57,58,59,60,61,62,63,64,65,66,67,52,99,68,69,70,100,71,72,73,74,75,76,77,78,79,80,81,82,84,83,85,86,87,88,89,90,91,92,93,94,95,96,97,98,139,140,141,115,142,143,144,48,125,123,121,45,129,101,103,102,130,8,9,11,10,2,12,13,14,15,16,17,18,19,3,4,23,20,21,22,24,25,26,5,27,28,29,30,6,31,32,33,34,7,39,35,36,37,38,1,40,41,113,112,110,111,118,127,128,131]},"version":"4.4.4"}
|
package/package.json
CHANGED
|
@@ -1,7 +1,86 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@libp2p/tcp",
|
|
3
|
-
"version": "
|
|
4
|
-
"description": "",
|
|
5
|
-
"main": "index.js",
|
|
6
|
-
"
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"description": "Node.js implementation of the TCP module that libp2p uses, which implements the interface-connection and interface-transport interfaces",
|
|
5
|
+
"main": "./dist/src/index.js",
|
|
6
|
+
"type": "module",
|
|
7
|
+
"exports": {
|
|
8
|
+
".": {
|
|
9
|
+
"import": "./dist/src/index.js"
|
|
10
|
+
}
|
|
11
|
+
},
|
|
12
|
+
"scripts": {
|
|
13
|
+
"lint": "aegir lint",
|
|
14
|
+
"dep-check": "aegir dep-check dist/src/**/*.js dist/test/**/*.js",
|
|
15
|
+
"build": "tsc",
|
|
16
|
+
"pretest": "npm run build",
|
|
17
|
+
"test": "aegir test -f ./dist/test/**/*.js",
|
|
18
|
+
"test:node": "npm run test -- -t node --cov",
|
|
19
|
+
"test:electron-main": "npm run test -- -t electron-main",
|
|
20
|
+
"release": "semantic-release"
|
|
21
|
+
},
|
|
22
|
+
"repository": {
|
|
23
|
+
"type": "git",
|
|
24
|
+
"url": "https://github.com/libp2p/js-libp2p-tcp.git"
|
|
25
|
+
},
|
|
26
|
+
"keywords": [
|
|
27
|
+
"libp2p",
|
|
28
|
+
"network",
|
|
29
|
+
"p2p",
|
|
30
|
+
"peer",
|
|
31
|
+
"peer-to-peer",
|
|
32
|
+
"IPFS",
|
|
33
|
+
"TCP"
|
|
34
|
+
],
|
|
35
|
+
"license": "(Apache-2.0 OR MIT)",
|
|
36
|
+
"bugs": {
|
|
37
|
+
"url": "https://github.com/libp2p/js-libp2p-tcp/issues"
|
|
38
|
+
},
|
|
39
|
+
"homepage": "https://github.com/libp2p/js-libp2p-tcp",
|
|
40
|
+
"engines": {
|
|
41
|
+
"node": ">=14.0.0"
|
|
42
|
+
},
|
|
43
|
+
"types": "dist/src/index.d.ts",
|
|
44
|
+
"devDependencies": {
|
|
45
|
+
"@libp2p/interface-compliance-tests": "^1.0.1",
|
|
46
|
+
"@libp2p/interfaces": "^1.0.0",
|
|
47
|
+
"@types/debug": "^4.1.5",
|
|
48
|
+
"@types/mocha": "^9.0.0",
|
|
49
|
+
"aegir": "^36.1.3",
|
|
50
|
+
"it-pipe": "^1.1.0",
|
|
51
|
+
"sinon": "^12.0.0",
|
|
52
|
+
"streaming-iterables": "^6.0.0"
|
|
53
|
+
},
|
|
54
|
+
"dependencies": {
|
|
55
|
+
"@libp2p/utils": "^1.0.1",
|
|
56
|
+
"@multiformats/mafmt": "^11.0.0",
|
|
57
|
+
"@multiformats/multiaddr": "^10.1.1",
|
|
58
|
+
"abortable-iterator": "^3.0.1",
|
|
59
|
+
"debug": "^4.3.1",
|
|
60
|
+
"err-code": "^3.0.1",
|
|
61
|
+
"stream-to-it": "^0.2.2"
|
|
62
|
+
},
|
|
63
|
+
"contributors": [
|
|
64
|
+
"David Dias <daviddias.p@gmail.com>",
|
|
65
|
+
"Jacob Heun <jacobheun@gmail.com>",
|
|
66
|
+
"Vasco Santos <vasco.santos@moxy.studio>",
|
|
67
|
+
"Stephen Whitmore <stephen.whitmore@gmail.com>",
|
|
68
|
+
"Friedel Ziegelmayer <dignifiedquire@gmail.com>",
|
|
69
|
+
"Alex Potsides <alex@achingbrain.net>",
|
|
70
|
+
"Richard Littauer <richard.littauer@gmail.com>",
|
|
71
|
+
"Tom White <tomtinkerer@gmail.com>",
|
|
72
|
+
"Alan Shaw <alan@tableflip.io>",
|
|
73
|
+
"Nazar Hussain <nazarhussain@gmail.com>",
|
|
74
|
+
"Pedro Teixeira <i@pgte.me>",
|
|
75
|
+
"Prashanth Chandra <coolshanth94@gmail.com>",
|
|
76
|
+
"Ryan Mehta <ryan.mehta@gmail.com>",
|
|
77
|
+
"Linus Unnebäck <linus@folkdatorn.se>",
|
|
78
|
+
"Cayman <caymannava@gmail.com>",
|
|
79
|
+
"Diogo Silva <fsdiogo@gmail.com>",
|
|
80
|
+
"Dmitriy Ryajov <dryajov@gmail.com>",
|
|
81
|
+
"Drew Stone <drewstone329@gmail.com>",
|
|
82
|
+
"Evan Schwartz <evan.mark.schwartz@gmail.com>",
|
|
83
|
+
"João Antunes <j.goncalo.antunes@gmail.com>",
|
|
84
|
+
"Mikeal Rogers <mikeal.rogers@gmail.com>"
|
|
85
|
+
]
|
|
7
86
|
}
|
package/src/constants.ts
ADDED
package/src/index.ts
ADDED
|
@@ -0,0 +1,147 @@
|
|
|
1
|
+
import net from 'net'
|
|
2
|
+
import * as mafmt from '@multiformats/mafmt'
|
|
3
|
+
import errCode from 'err-code'
|
|
4
|
+
import debug from 'debug'
|
|
5
|
+
import { toConnection } from './socket-to-conn.js'
|
|
6
|
+
import { createListener } from './listener.js'
|
|
7
|
+
import { multiaddrToNetConfig } from './utils.js'
|
|
8
|
+
import { AbortError } from 'abortable-iterator'
|
|
9
|
+
import { CODE_CIRCUIT, CODE_P2P } from './constants.js'
|
|
10
|
+
import type { Transport, Upgrader } from '@libp2p/interfaces/transport'
|
|
11
|
+
import type { Connection } from '@libp2p/interfaces/connection'
|
|
12
|
+
import type { Multiaddr } from '@multiformats/multiaddr'
|
|
13
|
+
import type { Socket } from 'net'
|
|
14
|
+
|
|
15
|
+
const log = debug('libp2p:tcp')
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* @typedef {import('multiaddr').Multiaddr} Multiaddr
|
|
19
|
+
* @typedef {import('libp2p-interfaces/src/connection').Connection} Connection
|
|
20
|
+
* @typedef {import('libp2p-interfaces/src/transport/types').Upgrader} Upgrader
|
|
21
|
+
* @typedef {import('libp2p-interfaces/src/transport/types').Listener} Listener
|
|
22
|
+
* @typedef {import('net').Socket} Socket
|
|
23
|
+
*/
|
|
24
|
+
|
|
25
|
+
interface TCPOptions {
|
|
26
|
+
upgrader: Upgrader
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
interface DialOptions {
|
|
30
|
+
signal?: AbortSignal
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
export class TCP implements Transport<DialOptions, {}> {
|
|
34
|
+
private readonly _upgrader: Upgrader
|
|
35
|
+
|
|
36
|
+
constructor (options: TCPOptions) {
|
|
37
|
+
const { upgrader } = options
|
|
38
|
+
|
|
39
|
+
if (upgrader == null) {
|
|
40
|
+
throw new Error('An upgrader must be provided. See https://github.com/libp2p/interface-transport#upgrader.')
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
this._upgrader = upgrader
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
async dial (ma: Multiaddr, options: DialOptions = {}) {
|
|
47
|
+
const socket = await this._connect(ma, options)
|
|
48
|
+
|
|
49
|
+
// Avoid uncaught errors caused by unstable connections
|
|
50
|
+
socket.on('error', err => {
|
|
51
|
+
log('socket error', err)
|
|
52
|
+
})
|
|
53
|
+
|
|
54
|
+
const maConn = toConnection(socket, { remoteAddr: ma, signal: options.signal })
|
|
55
|
+
log('new outbound connection %s', maConn.remoteAddr)
|
|
56
|
+
const conn = await this._upgrader.upgradeOutbound(maConn)
|
|
57
|
+
log('outbound connection %s upgraded', maConn.remoteAddr)
|
|
58
|
+
return conn
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
async _connect (ma: Multiaddr, options: DialOptions = {}) {
|
|
62
|
+
if (options.signal?.aborted === true) {
|
|
63
|
+
throw new AbortError()
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
return await new Promise<Socket>((resolve, reject) => {
|
|
67
|
+
const start = Date.now()
|
|
68
|
+
const cOpts = multiaddrToNetConfig(ma)
|
|
69
|
+
|
|
70
|
+
log('dialing %j', cOpts)
|
|
71
|
+
const rawSocket = net.connect(cOpts)
|
|
72
|
+
|
|
73
|
+
const onError = (err: Error) => {
|
|
74
|
+
err.message = `connection error ${cOpts.host}:${cOpts.port}: ${err.message}`
|
|
75
|
+
|
|
76
|
+
done(err)
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
const onTimeout = () => {
|
|
80
|
+
log('connection timeout %s:%s', cOpts.host, cOpts.port)
|
|
81
|
+
|
|
82
|
+
const err = errCode(new Error(`connection timeout after ${Date.now() - start}ms`), 'ERR_CONNECT_TIMEOUT')
|
|
83
|
+
// Note: this will result in onError() being called
|
|
84
|
+
rawSocket.emit('error', err)
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
const onConnect = () => {
|
|
88
|
+
log('connection opened %j', cOpts)
|
|
89
|
+
done()
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
const onAbort = () => {
|
|
93
|
+
log('connection aborted %j', cOpts)
|
|
94
|
+
rawSocket.destroy()
|
|
95
|
+
done(new AbortError())
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
const done = (err?: any) => {
|
|
99
|
+
rawSocket.removeListener('error', onError)
|
|
100
|
+
rawSocket.removeListener('timeout', onTimeout)
|
|
101
|
+
rawSocket.removeListener('connect', onConnect)
|
|
102
|
+
|
|
103
|
+
if (options.signal != null) {
|
|
104
|
+
options.signal.removeEventListener('abort', onAbort)
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
if (err != null) {
|
|
108
|
+
return reject(err)
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
resolve(rawSocket)
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
rawSocket.on('error', onError)
|
|
115
|
+
rawSocket.on('timeout', onTimeout)
|
|
116
|
+
rawSocket.on('connect', onConnect)
|
|
117
|
+
|
|
118
|
+
if (options.signal != null) {
|
|
119
|
+
options.signal.addEventListener('abort', onAbort)
|
|
120
|
+
}
|
|
121
|
+
})
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
/**
|
|
125
|
+
* Creates a TCP listener. The provided `handler` function will be called
|
|
126
|
+
* anytime a new incoming Connection has been successfully upgraded via
|
|
127
|
+
* `upgrader.upgradeInbound`.
|
|
128
|
+
*/
|
|
129
|
+
createListener (options: {}, handler?: (connection: Connection) => void) {
|
|
130
|
+
return createListener({ handler: handler, upgrader: this._upgrader })
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
/**
|
|
134
|
+
* Takes a list of `Multiaddr`s and returns only valid TCP addresses
|
|
135
|
+
*/
|
|
136
|
+
filter (multiaddrs: Multiaddr[]) {
|
|
137
|
+
multiaddrs = Array.isArray(multiaddrs) ? multiaddrs : [multiaddrs]
|
|
138
|
+
|
|
139
|
+
return multiaddrs.filter(ma => {
|
|
140
|
+
if (ma.protoCodes().includes(CODE_CIRCUIT)) {
|
|
141
|
+
return false
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
return mafmt.TCP.matches(ma.decapsulateCode(CODE_P2P))
|
|
145
|
+
})
|
|
146
|
+
}
|
|
147
|
+
}
|
package/src/listener.ts
ADDED
|
@@ -0,0 +1,172 @@
|
|
|
1
|
+
import net from 'net'
|
|
2
|
+
import { EventEmitter } from 'events'
|
|
3
|
+
import debug from 'debug'
|
|
4
|
+
import { toConnection } from './socket-to-conn.js'
|
|
5
|
+
import { CODE_P2P } from './constants.js'
|
|
6
|
+
import {
|
|
7
|
+
getMultiaddrs,
|
|
8
|
+
multiaddrToNetConfig
|
|
9
|
+
} from './utils.js'
|
|
10
|
+
import type { Connection } from '@libp2p/interfaces/connection'
|
|
11
|
+
import type { MultiaddrConnection, Upgrader, Listener } from '@libp2p/interfaces/transport'
|
|
12
|
+
import type { Server } from 'net'
|
|
13
|
+
import type { Multiaddr } from '@multiformats/multiaddr'
|
|
14
|
+
|
|
15
|
+
const log = Object.assign(
|
|
16
|
+
debug('libp2p:tcp:listener'),
|
|
17
|
+
{ error: debug('libp2p:tcp:listener:error') })
|
|
18
|
+
|
|
19
|
+
interface ServerWithMultiaddrConnections extends Server {
|
|
20
|
+
__connections: MultiaddrConnection[]
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* Attempts to close the given maConn. If a failure occurs, it will be logged
|
|
25
|
+
*/
|
|
26
|
+
async function attemptClose (maConn: MultiaddrConnection) {
|
|
27
|
+
try {
|
|
28
|
+
await maConn.close()
|
|
29
|
+
} catch (err) {
|
|
30
|
+
log.error('an error occurred closing the connection', err)
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
interface Context {
|
|
35
|
+
handler?: (conn: Connection) => void
|
|
36
|
+
upgrader: Upgrader
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
/**
|
|
40
|
+
* Create listener
|
|
41
|
+
*/
|
|
42
|
+
export function createListener (context: Context) {
|
|
43
|
+
const {
|
|
44
|
+
handler, upgrader
|
|
45
|
+
} = context
|
|
46
|
+
|
|
47
|
+
let peerId: string | null
|
|
48
|
+
let listeningAddr: Multiaddr
|
|
49
|
+
|
|
50
|
+
const server: ServerWithMultiaddrConnections = Object.assign(net.createServer(socket => {
|
|
51
|
+
// Avoid uncaught errors caused by unstable connections
|
|
52
|
+
socket.on('error', err => {
|
|
53
|
+
log('socket error', err)
|
|
54
|
+
})
|
|
55
|
+
|
|
56
|
+
let maConn: MultiaddrConnection
|
|
57
|
+
try {
|
|
58
|
+
maConn = toConnection(socket, { listeningAddr })
|
|
59
|
+
} catch (err) {
|
|
60
|
+
log.error('inbound connection failed', err)
|
|
61
|
+
return
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
log('new inbound connection %s', maConn.remoteAddr)
|
|
65
|
+
try {
|
|
66
|
+
upgrader.upgradeInbound(maConn)
|
|
67
|
+
.then((conn) => {
|
|
68
|
+
log('inbound connection %s upgraded', maConn.remoteAddr)
|
|
69
|
+
|
|
70
|
+
trackConn(server, maConn)
|
|
71
|
+
|
|
72
|
+
if (handler != null) {
|
|
73
|
+
handler(conn)
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
listener.emit('connection', conn)
|
|
77
|
+
})
|
|
78
|
+
.catch(async err => {
|
|
79
|
+
log.error('inbound connection failed', err)
|
|
80
|
+
|
|
81
|
+
await attemptClose(maConn)
|
|
82
|
+
})
|
|
83
|
+
.catch(err => {
|
|
84
|
+
log.error('closing inbound connection failed', err)
|
|
85
|
+
})
|
|
86
|
+
} catch (err) {
|
|
87
|
+
log.error('inbound connection failed', err)
|
|
88
|
+
|
|
89
|
+
attemptClose(maConn)
|
|
90
|
+
.catch(err => {
|
|
91
|
+
log.error('closing inbound connection failed', err)
|
|
92
|
+
})
|
|
93
|
+
}
|
|
94
|
+
}),
|
|
95
|
+
// Keep track of open connections to destroy in case of timeout
|
|
96
|
+
{ __connections: [] })
|
|
97
|
+
|
|
98
|
+
const listener: Listener = Object.assign(new EventEmitter(), {
|
|
99
|
+
getAddrs: () => {
|
|
100
|
+
let addrs: Multiaddr[] = []
|
|
101
|
+
const address = server.address()
|
|
102
|
+
|
|
103
|
+
if (address == null) {
|
|
104
|
+
throw new Error('Listener is not ready yet')
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
if (typeof address === 'string') {
|
|
108
|
+
throw new Error('Incorrect server address type')
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
// Because TCP will only return the IPv6 version
|
|
112
|
+
// we need to capture from the passed multiaddr
|
|
113
|
+
if (listeningAddr.toString().startsWith('/ip4')) {
|
|
114
|
+
addrs = addrs.concat(getMultiaddrs('ip4', address.address, address.port))
|
|
115
|
+
} else if (address.family === 'IPv6') {
|
|
116
|
+
addrs = addrs.concat(getMultiaddrs('ip6', address.address, address.port))
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
return addrs.map(ma => peerId != null ? ma.encapsulate(`/p2p/${peerId}`) : ma)
|
|
120
|
+
},
|
|
121
|
+
listen: async (ma: Multiaddr) => {
|
|
122
|
+
listeningAddr = ma
|
|
123
|
+
peerId = ma.getPeerId()
|
|
124
|
+
|
|
125
|
+
if (peerId == null) {
|
|
126
|
+
listeningAddr = ma.decapsulateCode(CODE_P2P)
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
return await new Promise<void>((resolve, reject) => {
|
|
130
|
+
const options = multiaddrToNetConfig(listeningAddr)
|
|
131
|
+
server.listen(options, (err?: any) => {
|
|
132
|
+
if (err != null) {
|
|
133
|
+
return reject(err)
|
|
134
|
+
}
|
|
135
|
+
log('Listening on %s', server.address())
|
|
136
|
+
resolve()
|
|
137
|
+
})
|
|
138
|
+
})
|
|
139
|
+
},
|
|
140
|
+
close: async () => {
|
|
141
|
+
if (!server.listening) {
|
|
142
|
+
return
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
await Promise.all([
|
|
146
|
+
server.__connections.map(async maConn => await attemptClose(maConn))
|
|
147
|
+
])
|
|
148
|
+
|
|
149
|
+
await new Promise<void>((resolve, reject) => {
|
|
150
|
+
server.close(err => (err != null) ? reject(err) : resolve())
|
|
151
|
+
})
|
|
152
|
+
}
|
|
153
|
+
})
|
|
154
|
+
|
|
155
|
+
server
|
|
156
|
+
.on('listening', () => listener.emit('listening'))
|
|
157
|
+
.on('error', err => listener.emit('error', err))
|
|
158
|
+
.on('close', () => listener.emit('close'))
|
|
159
|
+
|
|
160
|
+
return listener
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
function trackConn (server: ServerWithMultiaddrConnections, maConn: MultiaddrConnection) {
|
|
164
|
+
server.__connections.push(maConn)
|
|
165
|
+
|
|
166
|
+
const untrackConn = () => {
|
|
167
|
+
server.__connections = server.__connections.filter(c => c !== maConn)
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
// @ts-expect-error
|
|
171
|
+
maConn.conn.once('close', untrackConn)
|
|
172
|
+
}
|
|
@@ -0,0 +1,127 @@
|
|
|
1
|
+
import abortable from 'abortable-iterator'
|
|
2
|
+
import debug from 'debug'
|
|
3
|
+
// @ts-expect-error no types
|
|
4
|
+
import toIterable from 'stream-to-it'
|
|
5
|
+
import { ipPortToMultiaddr as toMultiaddr } from '@libp2p/utils/ip-port-to-multiaddr'
|
|
6
|
+
import { CLOSE_TIMEOUT } from './constants.js'
|
|
7
|
+
import type { Socket } from 'net'
|
|
8
|
+
import type { Multiaddr } from '@multiformats/multiaddr'
|
|
9
|
+
import type { MultiaddrConnection } from '@libp2p/interfaces/transport'
|
|
10
|
+
|
|
11
|
+
const log = debug('libp2p:tcp:socket')
|
|
12
|
+
|
|
13
|
+
interface ToConnectionOptions {
|
|
14
|
+
listeningAddr?: Multiaddr
|
|
15
|
+
remoteAddr?: Multiaddr
|
|
16
|
+
localAddr?: Multiaddr
|
|
17
|
+
signal?: AbortSignal
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
/**
|
|
21
|
+
* Convert a socket into a MultiaddrConnection
|
|
22
|
+
* https://github.com/libp2p/interface-transport#multiaddrconnection
|
|
23
|
+
*/
|
|
24
|
+
export const toConnection = (socket: Socket, options?: ToConnectionOptions) => {
|
|
25
|
+
options = options ?? {}
|
|
26
|
+
|
|
27
|
+
// Check if we are connected on a unix path
|
|
28
|
+
if (options.listeningAddr?.getPath() != null) {
|
|
29
|
+
options.remoteAddr = options.listeningAddr
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
if (options.remoteAddr?.getPath() != null) {
|
|
33
|
+
options.localAddr = options.remoteAddr
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
const { sink, source } = toIterable.duplex(socket)
|
|
37
|
+
|
|
38
|
+
const maConn: MultiaddrConnection = {
|
|
39
|
+
async sink (source) {
|
|
40
|
+
if ((options?.signal) != null) {
|
|
41
|
+
source = abortable(source, options.signal)
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
try {
|
|
45
|
+
await sink((async function * () {
|
|
46
|
+
for await (const chunk of source) {
|
|
47
|
+
// Convert BufferList to Buffer
|
|
48
|
+
// Sink in StreamMuxer define argument as Uint8Array so chunk type infers as number which can't be sliced
|
|
49
|
+
yield Buffer.isBuffer(chunk) ? chunk : chunk.slice()
|
|
50
|
+
}
|
|
51
|
+
})())
|
|
52
|
+
} catch (err: any) {
|
|
53
|
+
// If aborted we can safely ignore
|
|
54
|
+
if (err.type !== 'aborted') {
|
|
55
|
+
// If the source errored the socket will already have been destroyed by
|
|
56
|
+
// toIterable.duplex(). If the socket errored it will already be
|
|
57
|
+
// destroyed. There's nothing to do here except log the error & return.
|
|
58
|
+
log(err)
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
},
|
|
62
|
+
|
|
63
|
+
// Missing Type for "abortable"
|
|
64
|
+
source: (options.signal != null) ? abortable(source, options.signal) : source,
|
|
65
|
+
|
|
66
|
+
conn: socket,
|
|
67
|
+
|
|
68
|
+
localAddr: options.localAddr ?? toMultiaddr(socket.localAddress ?? '', socket.localPort ?? ''),
|
|
69
|
+
|
|
70
|
+
// If the remote address was passed, use it - it may have the peer ID encapsulated
|
|
71
|
+
remoteAddr: options.remoteAddr ?? toMultiaddr(socket.remoteAddress ?? '', socket.remotePort ?? ''),
|
|
72
|
+
|
|
73
|
+
timeline: { open: Date.now() },
|
|
74
|
+
|
|
75
|
+
async close () {
|
|
76
|
+
if (socket.destroyed) return
|
|
77
|
+
|
|
78
|
+
return await new Promise((resolve, reject) => {
|
|
79
|
+
const start = Date.now()
|
|
80
|
+
|
|
81
|
+
// Attempt to end the socket. If it takes longer to close than the
|
|
82
|
+
// timeout, destroy it manually.
|
|
83
|
+
const timeout = setTimeout(() => {
|
|
84
|
+
const { host, port } = maConn.remoteAddr.toOptions()
|
|
85
|
+
log(
|
|
86
|
+
'timeout closing socket to %s:%s after %dms, destroying it manually',
|
|
87
|
+
host,
|
|
88
|
+
port,
|
|
89
|
+
Date.now() - start
|
|
90
|
+
)
|
|
91
|
+
|
|
92
|
+
if (socket.destroyed) {
|
|
93
|
+
log('%s:%s is already destroyed', host, port)
|
|
94
|
+
} else {
|
|
95
|
+
socket.destroy()
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
resolve()
|
|
99
|
+
}, CLOSE_TIMEOUT).unref()
|
|
100
|
+
|
|
101
|
+
socket.once('close', () => {
|
|
102
|
+
clearTimeout(timeout)
|
|
103
|
+
resolve()
|
|
104
|
+
})
|
|
105
|
+
socket.end((err?: Error & { code?: string }) => {
|
|
106
|
+
clearTimeout(timeout)
|
|
107
|
+
maConn.timeline.close = Date.now()
|
|
108
|
+
if (err != null) {
|
|
109
|
+
return reject(err)
|
|
110
|
+
}
|
|
111
|
+
resolve()
|
|
112
|
+
})
|
|
113
|
+
})
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
socket.once('close', () => {
|
|
118
|
+
// In instances where `close` was not explicitly called,
|
|
119
|
+
// such as an iterable stream ending, ensure we have set the close
|
|
120
|
+
// timeline
|
|
121
|
+
if (maConn.timeline.close == null) {
|
|
122
|
+
maConn.timeline.close = Date.now()
|
|
123
|
+
}
|
|
124
|
+
})
|
|
125
|
+
|
|
126
|
+
return maConn
|
|
127
|
+
}
|
package/src/utils.ts
ADDED
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
import { Multiaddr } from '@multiformats/multiaddr'
|
|
2
|
+
import os from 'os'
|
|
3
|
+
|
|
4
|
+
const ProtoFamily = { ip4: 'IPv4', ip6: 'IPv6' }
|
|
5
|
+
|
|
6
|
+
export function multiaddrToNetConfig (addr: Multiaddr) {
|
|
7
|
+
const listenPath = addr.getPath()
|
|
8
|
+
|
|
9
|
+
// unix socket listening
|
|
10
|
+
if (listenPath != null) {
|
|
11
|
+
// TCP should not return unix socket else need to refactor listener which accepts connection options object
|
|
12
|
+
throw new Error('Unix Sockets are not supported by the TCP transport')
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
// tcp listening
|
|
16
|
+
return addr.toOptions()
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
export function getMultiaddrs (proto: 'ip4' | 'ip6', ip: string, port: number) {
|
|
20
|
+
const toMa = (ip: string) => new Multiaddr(`/${proto}/${ip}/tcp/${port}`)
|
|
21
|
+
return (isAnyAddr(ip) ? getNetworkAddrs(ProtoFamily[proto]) : [ip]).map(toMa)
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
export function isAnyAddr (ip: string) {
|
|
25
|
+
return ['0.0.0.0', '::'].includes(ip)
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
const networks = os.networkInterfaces()
|
|
29
|
+
|
|
30
|
+
function getNetworkAddrs (family: string) {
|
|
31
|
+
const addresses = []
|
|
32
|
+
|
|
33
|
+
for (const [, netAddrs] of Object.entries(networks)) {
|
|
34
|
+
if (netAddrs != null) {
|
|
35
|
+
for (const netAddr of netAddrs) {
|
|
36
|
+
if (netAddr.family === family) {
|
|
37
|
+
addresses.push(netAddr.address)
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
return addresses
|
|
44
|
+
}
|