From 7c9ad152e26c3471f5c07e6b8d2711ee53be4676 Mon Sep 17 00:00:00 2001 From: nolash Date: Tue, 11 Aug 2020 06:44:12 +0200 Subject: [PATCH 1/4] Add untested ping class --- package-lock.json | 482 ++++++++++++++++++++++++++-------------------- src/index.ts | 1 + src/ping.ts | 51 +++++ src/session.ts | 4 +- 4 files changed, 323 insertions(+), 215 deletions(-) create mode 100644 src/ping.ts diff --git a/package-lock.json b/package-lock.json index db3824a..d9748b0 100644 --- a/package-lock.json +++ b/package-lock.json @@ -14,16 +14,16 @@ } }, "@babel/core": { - "version": "7.11.0", - "resolved": "https://registry.npmjs.org/@babel/core/-/core-7.11.0.tgz", - "integrity": "sha512-mkLq8nwaXmDtFmRkQ8ED/eA2CnVw4zr7dCztKalZXBvdK5EeNUAesrrwUqjQEzFgomJssayzB0aqlOsP1vGLqg==", + "version": "7.11.1", + "resolved": "https://registry.npmjs.org/@babel/core/-/core-7.11.1.tgz", + "integrity": "sha512-XqF7F6FWQdKGGWAzGELL+aCO1p+lRY5Tj5/tbT3St1G8NaH70jhhDIKknIZaDans0OQBG5wRAldROLHSt44BgQ==", "dev": true, "requires": { "@babel/code-frame": "^7.10.4", "@babel/generator": "^7.11.0", "@babel/helper-module-transforms": "^7.11.0", "@babel/helpers": "^7.10.4", - "@babel/parser": "^7.11.0", + "@babel/parser": "^7.11.1", "@babel/template": "^7.10.4", "@babel/traverse": "^7.11.0", "@babel/types": "^7.11.0", @@ -51,12 +51,6 @@ "resolved": "https://registry.npmjs.org/ms/-/ms-2.1.2.tgz", "integrity": "sha512-sGkPx+VjMtmA6MX27oA4FBFELFCZZ4S4XqeGOXCv68tT+jb3vk/RyaKWP0PTKyWtmLSM0b+adUTEvbs1PEaH2w==", "dev": true - }, - "source-map": { - "version": "0.5.7", - "resolved": "https://registry.npmjs.org/source-map/-/source-map-0.5.7.tgz", - "integrity": "sha1-igOdLRAh0i0eoUyA2OpGi6LvP8w=", - "dev": true } } }, @@ -69,14 +63,6 @@ "@babel/types": "^7.11.0", "jsesc": "^2.5.1", "source-map": "^0.5.0" - }, - "dependencies": { - "source-map": { - "version": "0.5.7", - "resolved": "https://registry.npmjs.org/source-map/-/source-map-0.5.7.tgz", - "integrity": "sha1-igOdLRAh0i0eoUyA2OpGi6LvP8w=", - "dev": true - } } }, "@babel/helper-function-name": { @@ -207,9 +193,9 @@ } }, "@babel/parser": { - "version": "7.11.0", - "resolved": "https://registry.npmjs.org/@babel/parser/-/parser-7.11.0.tgz", - "integrity": "sha512-qvRvi4oI8xii8NllyEc4MDJjuZiNaRzyb7Y7lup1NqJV8TZHF4O27CcP+72WPn/k1zkgJ6WJfnIbk4jTsVAZHw==", + "version": "7.11.2", + "resolved": "https://registry.npmjs.org/@babel/parser/-/parser-7.11.2.tgz", + "integrity": "sha512-Vuj/+7vLo6l1Vi7uuO+1ngCDNeVmNbTngcJFKCR/oEtz8tKz0CJxZEGmPt9KcIloZhOZ3Zit6xbpXT2MDlS9Vw==", "dev": true }, "@babel/plugin-syntax-object-rest-spread": { @@ -222,9 +208,9 @@ } }, "@babel/runtime": { - "version": "7.11.0", - "resolved": "https://registry.npmjs.org/@babel/runtime/-/runtime-7.11.0.tgz", - "integrity": "sha512-qArkXsjJq7H+T86WrIFV0Fnu/tNOkZ4cgXmjkzAu3b/58D5mFIO8JH/y77t7C9q0OdDRdh9s7Ue5GasYssxtXw==", + "version": "7.11.2", + "resolved": "https://registry.npmjs.org/@babel/runtime/-/runtime-7.11.2.tgz", + "integrity": "sha512-TeWkU52so0mPtDcaCTxNBI/IHiz0pZgr8VEFqXFtZWpYD08ZB6FaSwVAS8MKRQAP3bYKiVjwysOJgMFY28o6Tw==", "requires": { "regenerator-runtime": "^0.13.4" } @@ -734,6 +720,14 @@ "slash": "^2.0.0", "source-map": "^0.6.0", "string-length": "^2.0.0" + }, + "dependencies": { + "source-map": { + "version": "0.6.1", + "resolved": "https://registry.npmjs.org/source-map/-/source-map-0.6.1.tgz", + "integrity": "sha512-UjgapumWlbMhkBgzT7Ykc5YXUT46F0iKu8SGXq0bcwP5dz/h0Plj6enJqjz1Zbq2l5WaqYnrVbwWOWMyF3F47g==", + "dev": true + } } }, "@jest/source-map": { @@ -745,6 +739,14 @@ "callsites": "^3.0.0", "graceful-fs": "^4.1.15", "source-map": "^0.6.0" + }, + "dependencies": { + "source-map": { + "version": "0.6.1", + "resolved": "https://registry.npmjs.org/source-map/-/source-map-0.6.1.tgz", + "integrity": "sha512-UjgapumWlbMhkBgzT7Ykc5YXUT46F0iKu8SGXq0bcwP5dz/h0Plj6enJqjz1Zbq2l5WaqYnrVbwWOWMyF3F47g==", + "dev": true + } } }, "@jest/test-result": { @@ -792,6 +794,14 @@ "slash": "^2.0.0", "source-map": "^0.6.1", "write-file-atomic": "2.4.1" + }, + "dependencies": { + "source-map": { + "version": "0.6.1", + "resolved": "https://registry.npmjs.org/source-map/-/source-map-0.6.1.tgz", + "integrity": "sha512-UjgapumWlbMhkBgzT7Ykc5YXUT46F0iKu8SGXq0bcwP5dz/h0Plj6enJqjz1Zbq2l5WaqYnrVbwWOWMyF3F47g==", + "dev": true + } } }, "@jest/types": { @@ -911,9 +921,9 @@ "dev": true }, "@types/yargs": { - "version": "13.0.9", - "resolved": "https://registry.npmjs.org/@types/yargs/-/yargs-13.0.9.tgz", - "integrity": "sha512-xrvhZ4DZewMDhoH1utLtOAwYQy60eYFoXeje30TzM3VOvQlBwQaEpKFq5m34k1wOw2AKIi2pwtiAjdmhvlBUzg==", + "version": "13.0.10", + "resolved": "https://registry.npmjs.org/@types/yargs/-/yargs-13.0.10.tgz", + "integrity": "sha512-MU10TSgzNABgdzKvQVW1nuuT+sgBMWeXNc3XOs5YXV5SDAK+PPja2eUuBNB9iqElu03xyEDqlnGw0jgl4nbqGQ==", "dev": true, "requires": { "@types/yargs-parser": "*" @@ -1119,9 +1129,9 @@ "dev": true }, "acorn": { - "version": "7.1.0", - "resolved": "https://registry.npmjs.org/acorn/-/acorn-7.1.0.tgz", - "integrity": "sha512-kL5CuoXA/dgxlBbVrflsflzQ3PAas7RYZB52NOm/6839iVYJgKMJ3cQJD+t2i5+qFa8h3MDpEOJiS64E8JLnSQ==" + "version": "7.1.1", + "resolved": "https://registry.npmjs.org/acorn/-/acorn-7.1.1.tgz", + "integrity": "sha512-add7dgA5ppRPxCFJoAGfMDi7PIBXq1RtGo7BhbLaxwrXPOmw8gq48Y9ozT01hUKy9byMjlR20EJhu5zlkErEkg==" }, "acorn-globals": { "version": "4.3.4", @@ -1722,9 +1732,9 @@ } }, "mocha": { - "version": "8.1.0", - "resolved": "https://registry.npmjs.org/mocha/-/mocha-8.1.0.tgz", - "integrity": "sha512-sI0gaI1I/jPVu3KFpnveWGadfe3JNBAENqgTUPgLZAUppu725zS2mrVztzAgIR8DUscuS4doEBTx9LATC+HSeA==", + "version": "8.1.1", + "resolved": "https://registry.npmjs.org/mocha/-/mocha-8.1.1.tgz", + "integrity": "sha512-p7FuGlYH8t7gaiodlFreseLxEmxTgvyG9RgPHODFPySNhwUehu8NIb0vdSt3WFckSneswZ0Un5typYcWElk7HQ==", "requires": { "ansi-colors": "4.1.1", "browser-stdout": "1.3.1", @@ -2637,17 +2647,6 @@ "os-homedir": "^1.0.1", "parse-json": "^2.2.0", "require-from-string": "^1.1.0" - }, - "dependencies": { - "parse-json": { - "version": "2.2.0", - "resolved": "https://registry.npmjs.org/parse-json/-/parse-json-2.2.0.tgz", - "integrity": "sha1-9ID0BDTvgHQfhGkJn43qGPVaTck=", - "dev": true, - "requires": { - "error-ex": "^1.2.0" - } - } } }, "create-ecdh": { @@ -3133,21 +3132,21 @@ } }, "eccrypto": { - "version": "1.1.3", - "resolved": "https://registry.npmjs.org/eccrypto/-/eccrypto-1.1.3.tgz", - "integrity": "sha512-Xtyj039Xp2NDZwoe9IcD7pT1EwM4DILdxPCN2H7Rk1wgJNtTkFpk+cpX1QpuHTMaIhkatOBlGGKzGw/DUCDdqg==", + "version": "1.1.5", + "resolved": "https://registry.npmjs.org/eccrypto/-/eccrypto-1.1.5.tgz", + "integrity": "sha512-iGu2lqaSFEX7jmYQayOzSIB52PdXguUeR17V7ilfmd5nVdt683HvYB0DwuGK1Y3srNp/zl6D05JfEdFY+SC5MQ==", "requires": { - "acorn": "7.1.0", - "elliptic": "6.5.1", + "acorn": "7.1.1", + "elliptic": "6.5.3", "es6-promise": "4.2.8", "nan": "2.14.0", "secp256k1": "3.7.1" }, "dependencies": { "elliptic": { - "version": "6.5.1", - "resolved": "https://registry.npmjs.org/elliptic/-/elliptic-6.5.1.tgz", - "integrity": "sha512-xvJINNLbTeWQjrl6X+7eQCrIy/YPv5XCpKW6kB5mKvtnGILoLDcySuwomfdzt0BMdLNVnuRNTuzKNHj0bva1Cg==", + "version": "6.5.3", + "resolved": "https://registry.npmjs.org/elliptic/-/elliptic-6.5.3.tgz", + "integrity": "sha512-IMqzv5wNQf+E6aHeIqATs0tOLeOTwj1QKbRcS3jBbYkl5oLAserA8yJTT7/VyHUYG91PRmPyeQDObKLPpeS4dw==", "requires": { "bn.js": "^4.4.0", "brorand": "^1.0.1", @@ -3403,6 +3402,15 @@ "esutils": "^2.0.2", "optionator": "^0.8.1", "source-map": "~0.6.1" + }, + "dependencies": { + "source-map": { + "version": "0.6.1", + "resolved": "https://registry.npmjs.org/source-map/-/source-map-0.6.1.tgz", + "integrity": "sha512-UjgapumWlbMhkBgzT7Ykc5YXUT46F0iKu8SGXq0bcwP5dz/h0Plj6enJqjz1Zbq2l5WaqYnrVbwWOWMyF3F47g==", + "dev": true, + "optional": true + } } }, "eslint-scope": { @@ -4332,25 +4340,13 @@ } }, "global-modules": { - "version": "2.0.0", - "resolved": "https://registry.npmjs.org/global-modules/-/global-modules-2.0.0.tgz", - "integrity": "sha512-NGbfmJBp9x8IxyJSd1P+otYK8vonoJactOogrVfFRIAEY1ukil8RSKDz2Yo7wh1oihl51l/r6W4epkeKJHqL8A==", - "dev": true, + "version": "1.0.0", + "resolved": "https://registry.npmjs.org/global-modules/-/global-modules-1.0.0.tgz", + "integrity": "sha512-sKzpEkf11GpOFuw0Zzjzmt4B4UZwjOcG757PPvrfhxcLFbq0wpsgpOqxpxtxFiCG4DtG93M6XRVbF2oGdev7bg==", "requires": { - "global-prefix": "^3.0.0" - }, - "dependencies": { - "global-prefix": { - "version": "3.0.0", - "resolved": "https://registry.npmjs.org/global-prefix/-/global-prefix-3.0.0.tgz", - "integrity": "sha512-awConJSVCHVGND6x3tmMaKcQvwXLhjdkmomy2W+Goaui8YPgYgXJZewhg3fWC+DlfqqQuWg8AwqjGTD2nAPVWg==", - "dev": true, - "requires": { - "ini": "^1.3.5", - "kind-of": "^6.0.2", - "which": "^1.3.1" - } - } + "global-prefix": "^1.0.1", + "is-windows": "^1.0.1", + "resolve-dir": "^1.0.0" } }, "global-prefix": { @@ -4455,15 +4451,6 @@ "wrap-ansi": "^2.0.0" } }, - "find-up": { - "version": "1.1.2", - "resolved": "https://registry.npmjs.org/find-up/-/find-up-1.1.2.tgz", - "integrity": "sha1-ay6YIrGizgpgq2TWEOzK1TyyTQ8=", - "requires": { - "path-exists": "^2.0.0", - "pinkie-promise": "^2.0.0" - } - }, "get-caller-file": { "version": "1.0.3", "resolved": "https://registry.npmjs.org/get-caller-file/-/get-caller-file-1.0.3.tgz", @@ -4502,68 +4489,6 @@ "number-is-nan": "^1.0.0" } }, - "load-json-file": { - "version": "1.1.0", - "resolved": "https://registry.npmjs.org/load-json-file/-/load-json-file-1.1.0.tgz", - "integrity": "sha1-lWkFcI1YtLq0wiYbBPWfMcmTdMA=", - "requires": { - "graceful-fs": "^4.1.2", - "parse-json": "^2.2.0", - "pify": "^2.0.0", - "pinkie-promise": "^2.0.0", - "strip-bom": "^2.0.0" - } - }, - "parse-json": { - "version": "2.2.0", - "resolved": "https://registry.npmjs.org/parse-json/-/parse-json-2.2.0.tgz", - "integrity": "sha1-9ID0BDTvgHQfhGkJn43qGPVaTck=", - "requires": { - "error-ex": "^1.2.0" - } - }, - "path-exists": { - "version": "2.1.0", - "resolved": "https://registry.npmjs.org/path-exists/-/path-exists-2.1.0.tgz", - "integrity": "sha1-D+tsZPD8UY2adU3V77YscCJ2H0s=", - "requires": { - "pinkie-promise": "^2.0.0" - } - }, - "path-type": { - "version": "1.1.0", - "resolved": "https://registry.npmjs.org/path-type/-/path-type-1.1.0.tgz", - "integrity": "sha1-WcRPfuSR2nBNpBXaWkBwuk+P5EE=", - "requires": { - "graceful-fs": "^4.1.2", - "pify": "^2.0.0", - "pinkie-promise": "^2.0.0" - } - }, - "pify": { - "version": "2.3.0", - "resolved": "https://registry.npmjs.org/pify/-/pify-2.3.0.tgz", - "integrity": "sha1-7RQaasBDqEnqWISY59yosVMw6Qw=" - }, - "read-pkg": { - "version": "1.1.0", - "resolved": "https://registry.npmjs.org/read-pkg/-/read-pkg-1.1.0.tgz", - "integrity": "sha1-9f+qXs0pyzHAR0vKfXVra7KePyg=", - "requires": { - "load-json-file": "^1.0.0", - "normalize-package-data": "^2.3.2", - "path-type": "^1.0.0" - } - }, - "read-pkg-up": { - "version": "1.0.1", - "resolved": "https://registry.npmjs.org/read-pkg-up/-/read-pkg-up-1.0.1.tgz", - "integrity": "sha1-nWPBMnbAZZGNV/ACpX9AobZD+wI=", - "requires": { - "find-up": "^1.0.0", - "read-pkg": "^1.0.0" - } - }, "require-main-filename": { "version": "1.0.1", "resolved": "https://registry.npmjs.org/require-main-filename/-/require-main-filename-1.0.1.tgz", @@ -4587,14 +4512,6 @@ "ansi-regex": "^2.0.0" } }, - "strip-bom": { - "version": "2.0.0", - "resolved": "https://registry.npmjs.org/strip-bom/-/strip-bom-2.0.0.tgz", - "integrity": "sha1-YhmoVhZSBJHzV4i9vxRHqZx+aw4=", - "requires": { - "is-utf8": "^0.2.0" - } - }, "which-module": { "version": "1.0.0", "resolved": "https://registry.npmjs.org/which-module/-/which-module-1.0.0.tgz", @@ -5186,9 +5103,9 @@ } }, "is-regex": { - "version": "1.1.0", - "resolved": "https://registry.npmjs.org/is-regex/-/is-regex-1.1.0.tgz", - "integrity": "sha512-iI97M8KTWID2la5uYXlkbSDQIg4F6o1sYboZKKTDpnDQMLtUL86zxhgDet3Q2SriaYsyGqZ6Mn2SjbRKeLHdqw==", + "version": "1.1.1", + "resolved": "https://registry.npmjs.org/is-regex/-/is-regex-1.1.1.tgz", + "integrity": "sha512-1+QkEcxiLlB7VEyFtyBg94e08OAsvq7FUBgApTq/w2ymCLyKJgDPsybBENVtA7XCQEgEXxKPonG+mvYRxh/LIg==", "requires": { "has-symbols": "^1.0.1" } @@ -5359,6 +5276,12 @@ "resolved": "https://registry.npmjs.org/ms/-/ms-2.1.2.tgz", "integrity": "sha512-sGkPx+VjMtmA6MX27oA4FBFELFCZZ4S4XqeGOXCv68tT+jb3vk/RyaKWP0PTKyWtmLSM0b+adUTEvbs1PEaH2w==", "dev": true + }, + "source-map": { + "version": "0.6.1", + "resolved": "https://registry.npmjs.org/source-map/-/source-map-0.6.1.tgz", + "integrity": "sha512-UjgapumWlbMhkBgzT7Ykc5YXUT46F0iKu8SGXq0bcwP5dz/h0Plj6enJqjz1Zbq2l5WaqYnrVbwWOWMyF3F47g==", + "dev": true } } }, @@ -5736,6 +5659,14 @@ "slash": "^2.0.0", "strip-bom": "^3.0.0", "yargs": "^13.3.0" + }, + "dependencies": { + "strip-bom": { + "version": "3.0.0", + "resolved": "https://registry.npmjs.org/strip-bom/-/strip-bom-3.0.0.tgz", + "integrity": "sha1-IzTBjpx1n3vdVv3vfprj1YjmjtM=", + "dev": true + } } }, "jest-serializer": { @@ -5791,6 +5722,14 @@ "mkdirp": "^0.5.1", "slash": "^2.0.0", "source-map": "^0.6.0" + }, + "dependencies": { + "source-map": { + "version": "0.6.1", + "resolved": "https://registry.npmjs.org/source-map/-/source-map-0.6.1.tgz", + "integrity": "sha512-UjgapumWlbMhkBgzT7Ykc5YXUT46F0iKu8SGXq0bcwP5dz/h0Plj6enJqjz1Zbq2l5WaqYnrVbwWOWMyF3F47g==", + "dev": true + } } }, "jest-validate": { @@ -6116,15 +6055,15 @@ } }, "load-json-file": { - "version": "4.0.0", - "resolved": "https://registry.npmjs.org/load-json-file/-/load-json-file-4.0.0.tgz", - "integrity": "sha1-L19Fq5HjMhYjT9U62rZo607AmTs=", - "dev": true, + "version": "1.1.0", + "resolved": "https://registry.npmjs.org/load-json-file/-/load-json-file-1.1.0.tgz", + "integrity": "sha1-lWkFcI1YtLq0wiYbBPWfMcmTdMA=", "requires": { "graceful-fs": "^4.1.2", - "parse-json": "^4.0.0", - "pify": "^3.0.0", - "strip-bom": "^3.0.0" + "parse-json": "^2.2.0", + "pify": "^2.0.0", + "pinkie-promise": "^2.0.0", + "strip-bom": "^2.0.0" } }, "loader-runner": { @@ -7211,13 +7150,11 @@ } }, "parse-json": { - "version": "4.0.0", - "resolved": "https://registry.npmjs.org/parse-json/-/parse-json-4.0.0.tgz", - "integrity": "sha1-vjX1Qlvh9/bHRxhPmKeIy5lHfuA=", - "dev": true, + "version": "2.2.0", + "resolved": "https://registry.npmjs.org/parse-json/-/parse-json-2.2.0.tgz", + "integrity": "sha1-9ID0BDTvgHQfhGkJn43qGPVaTck=", "requires": { - "error-ex": "^1.3.1", - "json-parse-better-errors": "^1.0.1" + "error-ex": "^1.2.0" } }, "parse-node-version": { @@ -7287,12 +7224,13 @@ "integrity": "sha1-v8zcjfWxLcUsi0PsONGNcsBLqW0=" }, "path-type": { - "version": "3.0.0", - "resolved": "https://registry.npmjs.org/path-type/-/path-type-3.0.0.tgz", - "integrity": "sha512-T2ZUsdZFHgA3u4e5PfPbjd7HDDpxPnQb5jN0SrDsjNSuVXHJqtwTnWqG0B1jZrgmJ/7lj1EmVIByWt1gxGkWvg==", - "dev": true, + "version": "1.1.0", + "resolved": "https://registry.npmjs.org/path-type/-/path-type-1.1.0.tgz", + "integrity": "sha1-WcRPfuSR2nBNpBXaWkBwuk+P5EE=", "requires": { - "pify": "^3.0.0" + "graceful-fs": "^4.1.2", + "pify": "^2.0.0", + "pinkie-promise": "^2.0.0" } }, "pathval": { @@ -7325,10 +7263,9 @@ "integrity": "sha512-q0M/9eZHzmr0AulXyPwNfZjtwZ/RBZlbN3K3CErVrk50T2ASYI7Bye0EvekFY3IP1Nt2DHu0re+V2ZHIpMkuWg==" }, "pify": { - "version": "3.0.0", - "resolved": "https://registry.npmjs.org/pify/-/pify-3.0.0.tgz", - "integrity": "sha1-5aSs0sEB/fPZpNB/DbxNtJ3SgXY=", - "dev": true + "version": "2.3.0", + "resolved": "https://registry.npmjs.org/pify/-/pify-2.3.0.tgz", + "integrity": "sha1-7RQaasBDqEnqWISY59yosVMw6Qw=" }, "pinkie": { "version": "2.0.4", @@ -7656,24 +7593,41 @@ "dev": true }, "read-pkg": { - "version": "3.0.0", - "resolved": "https://registry.npmjs.org/read-pkg/-/read-pkg-3.0.0.tgz", - "integrity": "sha1-nLxoaXj+5l0WwA4rGcI3/Pbjg4k=", - "dev": true, + "version": "1.1.0", + "resolved": "https://registry.npmjs.org/read-pkg/-/read-pkg-1.1.0.tgz", + "integrity": "sha1-9f+qXs0pyzHAR0vKfXVra7KePyg=", "requires": { - "load-json-file": "^4.0.0", + "load-json-file": "^1.0.0", "normalize-package-data": "^2.3.2", - "path-type": "^3.0.0" + "path-type": "^1.0.0" } }, "read-pkg-up": { - "version": "4.0.0", - "resolved": "https://registry.npmjs.org/read-pkg-up/-/read-pkg-up-4.0.0.tgz", - "integrity": "sha512-6etQSH7nJGsK0RbG/2TeDzZFa8shjQ1um+SwQQ5cwKy0dhSXdOncEhb1CPpvQG4h7FyOV6EB6YlV0yJvZQNAkA==", - "dev": true, + "version": "1.0.1", + "resolved": "https://registry.npmjs.org/read-pkg-up/-/read-pkg-up-1.0.1.tgz", + "integrity": "sha1-nWPBMnbAZZGNV/ACpX9AobZD+wI=", "requires": { - "find-up": "^3.0.0", - "read-pkg": "^3.0.0" + "find-up": "^1.0.0", + "read-pkg": "^1.0.0" + }, + "dependencies": { + "find-up": { + "version": "1.1.2", + "resolved": "https://registry.npmjs.org/find-up/-/find-up-1.1.2.tgz", + "integrity": "sha1-ay6YIrGizgpgq2TWEOzK1TyyTQ8=", + "requires": { + "path-exists": "^2.0.0", + "pinkie-promise": "^2.0.0" + } + }, + "path-exists": { + "version": "2.1.0", + "resolved": "https://registry.npmjs.org/path-exists/-/path-exists-2.1.0.tgz", + "integrity": "sha1-D+tsZPD8UY2adU3V77YscCJ2H0s=", + "requires": { + "pinkie-promise": "^2.0.0" + } + } } }, "readable-stream": { @@ -7882,18 +7836,6 @@ "requires": { "expand-tilde": "^2.0.0", "global-modules": "^1.0.0" - }, - "dependencies": { - "global-modules": { - "version": "1.0.0", - "resolved": "https://registry.npmjs.org/global-modules/-/global-modules-1.0.0.tgz", - "integrity": "sha512-sKzpEkf11GpOFuw0Zzjzmt4B4UZwjOcG757PPvrfhxcLFbq0wpsgpOqxpxtxFiCG4DtG93M6XRVbF2oGdev7bg==", - "requires": { - "global-prefix": "^1.0.1", - "is-windows": "^1.0.1", - "resolve-dir": "^1.0.0" - } - } } }, "resolve-from": { @@ -8213,11 +8155,6 @@ "requires": { "is-extendable": "^0.1.0" } - }, - "source-map": { - "version": "0.5.7", - "resolved": "https://registry.npmjs.org/source-map/-/source-map-0.5.7.tgz", - "integrity": "sha1-igOdLRAh0i0eoUyA2OpGi6LvP8w=" } } }, @@ -8297,10 +8234,9 @@ "dev": true }, "source-map": { - "version": "0.6.1", - "resolved": "https://registry.npmjs.org/source-map/-/source-map-0.6.1.tgz", - "integrity": "sha512-UjgapumWlbMhkBgzT7Ykc5YXUT46F0iKu8SGXq0bcwP5dz/h0Plj6enJqjz1Zbq2l5WaqYnrVbwWOWMyF3F47g==", - "dev": true + "version": "0.5.7", + "resolved": "https://registry.npmjs.org/source-map/-/source-map-0.5.7.tgz", + "integrity": "sha1-igOdLRAh0i0eoUyA2OpGi6LvP8w=" }, "source-map-resolve": { "version": "0.5.3", @@ -8322,6 +8258,14 @@ "requires": { "buffer-from": "^1.0.0", "source-map": "^0.6.0" + }, + "dependencies": { + "source-map": { + "version": "0.6.1", + "resolved": "https://registry.npmjs.org/source-map/-/source-map-0.6.1.tgz", + "integrity": "sha512-UjgapumWlbMhkBgzT7Ykc5YXUT46F0iKu8SGXq0bcwP5dz/h0Plj6enJqjz1Zbq2l5WaqYnrVbwWOWMyF3F47g==", + "dev": true + } } }, "source-map-url": { @@ -8610,10 +8554,12 @@ } }, "strip-bom": { - "version": "3.0.0", - "resolved": "https://registry.npmjs.org/strip-bom/-/strip-bom-3.0.0.tgz", - "integrity": "sha1-IzTBjpx1n3vdVv3vfprj1YjmjtM=", - "dev": true + "version": "2.0.0", + "resolved": "https://registry.npmjs.org/strip-bom/-/strip-bom-2.0.0.tgz", + "integrity": "sha1-YhmoVhZSBJHzV4i9vxRHqZx+aw4=", + "requires": { + "is-utf8": "^0.2.0" + } }, "strip-eof": { "version": "1.0.0", @@ -8743,6 +8689,14 @@ "commander": "^2.20.0", "source-map": "~0.6.1", "source-map-support": "~0.5.12" + }, + "dependencies": { + "source-map": { + "version": "0.6.1", + "resolved": "https://registry.npmjs.org/source-map/-/source-map-0.6.1.tgz", + "integrity": "sha512-UjgapumWlbMhkBgzT7Ykc5YXUT46F0iKu8SGXq0bcwP5dz/h0Plj6enJqjz1Zbq2l5WaqYnrVbwWOWMyF3F47g==", + "dev": true + } } }, "terser-webpack-plugin": { @@ -8770,6 +8724,12 @@ "requires": { "randombytes": "^2.1.0" } + }, + "source-map": { + "version": "0.6.1", + "resolved": "https://registry.npmjs.org/source-map/-/source-map-0.6.1.tgz", + "integrity": "sha512-UjgapumWlbMhkBgzT7Ykc5YXUT46F0iKu8SGXq0bcwP5dz/h0Plj6enJqjz1Zbq2l5WaqYnrVbwWOWMyF3F47g==", + "dev": true } } }, @@ -8783,6 +8743,72 @@ "minimatch": "^3.0.4", "read-pkg-up": "^4.0.0", "require-main-filename": "^2.0.0" + }, + "dependencies": { + "load-json-file": { + "version": "4.0.0", + "resolved": "https://registry.npmjs.org/load-json-file/-/load-json-file-4.0.0.tgz", + "integrity": "sha1-L19Fq5HjMhYjT9U62rZo607AmTs=", + "dev": true, + "requires": { + "graceful-fs": "^4.1.2", + "parse-json": "^4.0.0", + "pify": "^3.0.0", + "strip-bom": "^3.0.0" + } + }, + "parse-json": { + "version": "4.0.0", + "resolved": "https://registry.npmjs.org/parse-json/-/parse-json-4.0.0.tgz", + "integrity": "sha1-vjX1Qlvh9/bHRxhPmKeIy5lHfuA=", + "dev": true, + "requires": { + "error-ex": "^1.3.1", + "json-parse-better-errors": "^1.0.1" + } + }, + "path-type": { + "version": "3.0.0", + "resolved": "https://registry.npmjs.org/path-type/-/path-type-3.0.0.tgz", + "integrity": "sha512-T2ZUsdZFHgA3u4e5PfPbjd7HDDpxPnQb5jN0SrDsjNSuVXHJqtwTnWqG0B1jZrgmJ/7lj1EmVIByWt1gxGkWvg==", + "dev": true, + "requires": { + "pify": "^3.0.0" + } + }, + "pify": { + "version": "3.0.0", + "resolved": "https://registry.npmjs.org/pify/-/pify-3.0.0.tgz", + "integrity": "sha1-5aSs0sEB/fPZpNB/DbxNtJ3SgXY=", + "dev": true + }, + "read-pkg": { + "version": "3.0.0", + "resolved": "https://registry.npmjs.org/read-pkg/-/read-pkg-3.0.0.tgz", + "integrity": "sha1-nLxoaXj+5l0WwA4rGcI3/Pbjg4k=", + "dev": true, + "requires": { + "load-json-file": "^4.0.0", + "normalize-package-data": "^2.3.2", + "path-type": "^3.0.0" + } + }, + "read-pkg-up": { + "version": "4.0.0", + "resolved": "https://registry.npmjs.org/read-pkg-up/-/read-pkg-up-4.0.0.tgz", + "integrity": "sha512-6etQSH7nJGsK0RbG/2TeDzZFa8shjQ1um+SwQQ5cwKy0dhSXdOncEhb1CPpvQG4h7FyOV6EB6YlV0yJvZQNAkA==", + "dev": true, + "requires": { + "find-up": "^3.0.0", + "read-pkg": "^3.0.0" + } + }, + "strip-bom": { + "version": "3.0.0", + "resolved": "https://registry.npmjs.org/strip-bom/-/strip-bom-3.0.0.tgz", + "integrity": "sha1-IzTBjpx1n3vdVv3vfprj1YjmjtM=", + "dev": true + } } }, "text-encoding": { @@ -9403,9 +9429,9 @@ }, "dependencies": { "chokidar": { - "version": "3.4.1", - "resolved": "https://registry.npmjs.org/chokidar/-/chokidar-3.4.1.tgz", - "integrity": "sha512-TQTJyr2stihpC4Sya9hs2Xh+O2wf+igjL36Y75xx2WdHuiICcn/XJza46Jwt0eT5hVpQOzo3FpY3cj3RVYLX0g==", + "version": "3.4.2", + "resolved": "https://registry.npmjs.org/chokidar/-/chokidar-3.4.2.tgz", + "integrity": "sha512-IZHaDeBeI+sZJRX7lGcXsdzgvZqKv6sECqsbErJA4mHWfpRrD8B97kSFN4cQz6nGBGiuFia1MKR4d6c1o8Cv7A==", "dev": true, "optional": true, "requires": { @@ -9734,6 +9760,26 @@ "yargs": "^13.3.2" }, "dependencies": { + "global-modules": { + "version": "2.0.0", + "resolved": "https://registry.npmjs.org/global-modules/-/global-modules-2.0.0.tgz", + "integrity": "sha512-NGbfmJBp9x8IxyJSd1P+otYK8vonoJactOogrVfFRIAEY1ukil8RSKDz2Yo7wh1oihl51l/r6W4epkeKJHqL8A==", + "dev": true, + "requires": { + "global-prefix": "^3.0.0" + } + }, + "global-prefix": { + "version": "3.0.0", + "resolved": "https://registry.npmjs.org/global-prefix/-/global-prefix-3.0.0.tgz", + "integrity": "sha512-awConJSVCHVGND6x3tmMaKcQvwXLhjdkmomy2W+Goaui8YPgYgXJZewhg3fWC+DlfqqQuWg8AwqjGTD2nAPVWg==", + "dev": true, + "requires": { + "ini": "^1.3.5", + "kind-of": "^6.0.2", + "which": "^1.3.1" + } + }, "supports-color": { "version": "6.1.0", "resolved": "https://registry.npmjs.org/supports-color/-/supports-color-6.1.0.tgz", @@ -9753,6 +9799,14 @@ "requires": { "source-list-map": "^2.0.0", "source-map": "~0.6.1" + }, + "dependencies": { + "source-map": { + "version": "0.6.1", + "resolved": "https://registry.npmjs.org/source-map/-/source-map-0.6.1.tgz", + "integrity": "sha512-UjgapumWlbMhkBgzT7Ykc5YXUT46F0iKu8SGXq0bcwP5dz/h0Plj6enJqjz1Zbq2l5WaqYnrVbwWOWMyF3F47g==", + "dev": true + } } }, "whatwg-encoding": { diff --git a/src/index.ts b/src/index.ts index a40fdf6..6ce9c98 100644 --- a/src/index.ts +++ b/src/index.ts @@ -14,6 +14,7 @@ type StateCallback = (topicHex: string) => void; const REQUEST_PUBLIC_KEY_INDEX = 0; const RESPONSE_PUBLIC_KEY_INDEX = 1; const MSGPERIOD = 1000; +const PINGPERIOD = 5000; let log = console.log; diff --git a/src/ping.ts b/src/ping.ts new file mode 100644 index 0000000..b6d11ca --- /dev/null +++ b/src/ping.ts @@ -0,0 +1,51 @@ +const PING_TIMEOUT_DEFAULT = 5000; + +class Ping { + lastSeen: 0; + lastPong: 0; + pongSerial: 0; + + pingTimeout: PING_TIMEOUT_DEFAULT; + pingTimer: undefined; + pingCallback: undefined; + pingSerial: 0; + + constructor(pingCallback: any, pingTimeout?: number) { + this.pingCallback = pingCallback; + if (pingTimeout != undefined) { + this.pingTimeout = pingTimeout; + } + this.seen(); + } + + public seen() { + this.lastSeen = Date.now(); + + } + + public restart() { + clearTimeout(pingTimer); + console.debug('resetting pingtimer'); + this.pingTimer = setTimeout(this.ping, this.pingTimer); + } + + public pong(serial: number) { + console.debug('pong last/now', this.lastPongSerial, serial); + this.seen(), + this.restart(); + this.lastPong = Date.now(); + this.lastPongSequence = serial; + } + + public ping() { + this.restart(); + serial = this.pingSerial; + this.pingSerial++; + this.pingCallback(serial); + } +} + +export { + Ping, + PING_TIMEOUT_DEFAULT, +} diff --git a/src/session.ts b/src/session.ts index 2c43175..90d2849 100644 --- a/src/session.ts +++ b/src/session.ts @@ -18,6 +18,8 @@ class Session { topicSalt; secret: undefined; + ping: undefined; + constructor(client: BeeClient, selfWallet: any, tmpWallet: any, skipFirst: boolean = false) { this.initialized = false; this.client = client; @@ -32,12 +34,12 @@ class Session { this.logFunction = console.debug; } - // BUG: This won't work until bee-client indexes salted feeds by salt+address public async startOtherFeed(topicSalt, other_wallet) { this.topicSalt = topicSalt; this.selfFeed = this.client.addFeedWithSalt(topicSalt, this.selfWallet); this.otherWallet = other_wallet; this.otherFeed = this.client.addFeedWithSalt(topicSalt, this.otherWallet, 0); + lastSeen = Date.now(); return true; } From 359083b376fabf10c21bc7a7cbf85d42b0d1aa45 Mon Sep 17 00:00:00 2001 From: nolash Date: Tue, 11 Aug 2020 07:36:57 +0200 Subject: [PATCH 2/4] Move all session methods to class --- src/index.ts | 97 ++++++++++++++------------------------------------ src/ping.ts | 43 ++++++++++++++-------- src/session.ts | 66 +++++++++++++++++++++++++++++++--- 3 files changed, 118 insertions(+), 88 deletions(-) diff --git a/src/index.ts b/src/index.ts index 6ce9c98..b83b303 100644 --- a/src/index.ts +++ b/src/index.ts @@ -72,7 +72,8 @@ async function connectToPeer(session: any, handshakeOther:any) { const secretBytes = await derive(selfWallet.privateKey, otherWallet.publicKey); chatSession.setSecret(secretBytes); chatSession.startOtherFeed(secretBytes, otherWallet); - await chatSession.start(session); + //await chatSession.start(session); + await session.start(); return otherWallet; } @@ -86,7 +87,8 @@ async function connectToPeerTwo(session: any, handshakeOther:any) { chatSession.setSecret(secretBytes); chatSession.startOtherFeed(secretBytes, otherWallet); chatSession.sendHandshake(); - await chatSession.start(session); + //await chatSession.start(session); + await session.start(); return otherWallet; } @@ -155,93 +157,48 @@ const newSession = (gatewayAddress: string, messageCallback: any) => { const client = new BeeClient(gatewayAddress); let secretHex = undefined; - const sendEnvelope = async (envelope) => { - const envelopeJson = JSON.stringify(envelope) - //const encryptedMessage = await encryptAesGcm(envelopeJson, secretHex); - //const messageReference = await bzz.upload(Buffer.from(encryptedMessage)); - //const encryptedReference = await encryptAesGcm(messageReference, secretHex); - //const encryptedReferenceBytes = Buffer.from(encryptedReference) - //const r = await uploadToRawFeed(bzz, userSelf, topicTmp, writeIndex, encryptedReferenceBytes); - console.debug('I would have sent this message', envelope); - //writeIndex += 1; - } - const sendMessage = async (message: string) => { - const envelope = { - type: 'message', - message, - } - return sendEnvelope(envelope) - } - const sendPing = () => { - const envelope = { - type: 'ping', - } - return sendEnvelope(envelope) - } - const sendDisconnect = () => { - const envelope = { - type: 'disconnect', - } - return sendEnvelope(envelope) - } + + //const poll = async (otherFeed: any) => { const poll = async (session:any) => { while (true) { try { console.debug('poll', session.otherFeed); - const message = await session.getOtherFeed(); + const serializedMessage = await session.getOtherFeed(); //const encryptedReference = await downloadFromFeed(client, otherWallet, socId); //topicTmp); //, readIndex); //const messageReference = await decrypt(encryptedReference, secretHex); //const response = await client.downloadChunk(messageReference); //const encryptedArrayBuffer = await response.arrayBuffer(); //const message = await decrypt(new Uint8Array(encryptedArrayBuffer), secretHex); + const message = JSON.parse(serializedMessage); console.debug('got', message); - messageCallback({ - type: 'message', - message: message.chunk.data - }); + if (message.type == 'ping') { + if (message.pong) { + session.ping.ponged(message.serial); + } else { + session.ping.pinged(message.serial); + } + } else if (message.type == 'disconnect') { + console.debug('disconnect received, terminating main loop'); + return; + } else { + session.ping.seen(); + messageCallback({ + type: 'message', + message: message.chunk.data + }); + } } catch (e) { console.log('polled in vain for other...' + e); break; } } - setTimeout(poll, MSGPERIOD, session); + session.loop = setTimeout(poll, MSGPERIOD, session); } //const address = selfWallet.getAddress('binary') chatSession = new Session(client, selfWallet, tmpWallet, keyTmpRequestPriv != undefined); //topicTmpArray, address); - chatSession.sendMessage = async (message: any) => { - // const encryptedMessage = await encrypt(message, secretHex); - //const encryptedMessage = new TextEncoder().encode(message); - - //const messageReference = await bzz.upload(Buffer.from(encryptedMessage)); - // const messageReference = await client.uploadChunk(Buffer.from(encryptedMessage)); - // const encryptedReference = await encrypt(messageReference, secretHex); - // const encryptedReferenceBytes = Buffer.from(encryptedReference) - //const r = await uploadToRawFeed(bzz, userSelf, topicTmp, writeIndex, encryptedReferenceBytes); - - //const otherSocId = chatSession.selfFeed.next(); - //const soc = new swarm.soc(otherSocId, undefined, selfWallet); - - //let h = new swarm.fileSplitter(soc.setChunk); - //h.split(message); - - //soc.sign(); - - //let chunkData = soc.serializeData(); - //let chunkAddress = soc.getAddress(); -// let resultAddress = await updateFeed({ -// data: chunkData, -// reference: chunkAddress, -// }); - let r = await chatSession.client.updateFeedWithSalt(chatSession.secret, message, chatSession.selfWallet); - console.log(r); - }; - // TODO: move def to session, with polling as part of constructor - //chatSession.start = async (userOther: string, secret: string) => { - //chatSession.start = async (session: any, secret: string) => { - chatSession.start = async (session: any) => { - await poll(session); //;chatSession.otherFeed); - }; + chatSession.setPoller(poll); + return chatSession; } diff --git a/src/ping.ts b/src/ping.ts index b6d11ca..ecf7dfd 100644 --- a/src/ping.ts +++ b/src/ping.ts @@ -1,48 +1,63 @@ const PING_TIMEOUT_DEFAULT = 5000; class Ping { - lastSeen: 0; - lastPong: 0; - pongSerial: 0; + lastSeen: number; + lastPong: number; + otherSerial: number; - pingTimeout: PING_TIMEOUT_DEFAULT; - pingTimer: undefined; - pingCallback: undefined; - pingSerial: 0; + pingTimeout: number; + pingTimer: any; + pingCallback: any; + selfSerial: number; + // pingCallback must take two args; bool if true is pong and serial number constructor(pingCallback: any, pingTimeout?: number) { this.pingCallback = pingCallback; if (pingTimeout != undefined) { this.pingTimeout = pingTimeout; + } else { + this.pingTimeout = PING_TIMEOUT_DEFAULT; } + console.debug('initialized ping'); + } + + public start() { + console.debug('starting ping'); this.seen(); } public seen() { this.lastSeen = Date.now(); + this.restart(); } public restart() { - clearTimeout(pingTimer); + clearTimeout(this.pingTimer); console.debug('resetting pingtimer'); this.pingTimer = setTimeout(this.ping, this.pingTimer); } - public pong(serial: number) { - console.debug('pong last/now', this.lastPongSerial, serial); + public ponged(serial: number) { + console.debug('pong last/now', this.selfSerial, serial); this.seen(), - this.restart(); this.lastPong = Date.now(); - this.lastPongSequence = serial; + this.otherSerial = serial; } public ping() { this.restart(); - serial = this.pingSerial; - this.pingSerial++; + const serial = this.selfSerial; + console.debug('sending ping', serial); + this.selfSerial++; this.pingCallback(serial); } + + public pinged(serial: number) { + console.debug('ping last/now', this.otherSerial, serial); + this.seen(); + this.pingCallback(serial, true); + } } export { diff --git a/src/session.ts b/src/session.ts index 90d2849..d700011 100644 --- a/src/session.ts +++ b/src/session.ts @@ -1,11 +1,10 @@ import { BeeClient } from 'bee-client-lib'; +import { Ping } from './ping'; class Session { initialized: boolean; client: any; logFunction: any; - sendMessage: any; - start: any; selfWallet; otherWallet; @@ -18,7 +17,10 @@ class Session { topicSalt; secret: undefined; - ping: undefined; + ping: Ping; + + poller: any; + loop: any; constructor(client: BeeClient, selfWallet: any, tmpWallet: any, skipFirst: boolean = false) { this.initialized = false; @@ -32,6 +34,7 @@ class Session { this.sharedFeed = client.addFeed(tmpWallet); //new dfeeds.indexed(topic); } this.logFunction = console.debug; + this.ping = new Ping(this.sendPing); } public async startOtherFeed(topicSalt, other_wallet) { @@ -39,7 +42,6 @@ class Session { this.selfFeed = this.client.addFeedWithSalt(topicSalt, this.selfWallet); this.otherWallet = other_wallet; this.otherFeed = this.client.addFeedWithSalt(topicSalt, this.otherWallet, 0); - lastSeen = Date.now(); return true; } @@ -71,6 +73,62 @@ class Session { public setSecret(secret) { this.secret = secret; } + + public setPinger(pinger: any) { + this.ping = pinger; + } + + public stop() { + clearTimeout(this.loop); + } + + public async sendMessage(message: string) { + this.ping.restart(); + const envelope = { + type: 'message', + message, + } + this.sendEnvelope(envelope) + } + public async sendPing(serial: number, pong?: boolean) { + const envelope = { + type: 'ping', + pong: pong, + serial: serial, + } + this.sendEnvelope(envelope) + } + public async sendDisconnect() { + this.stop(); + console.debug('terminated main loop'); + const envelope = { + type: 'disconnect', + } + this.sendEnvelope(envelope) + } + + public async sendEnvelope (envelope:any) { + const envelopeJson = JSON.stringify(envelope) + //const encryptedMessage = await encryptAesGcm(envelopeJson, secretHex); + //const messageReference = await bzz.upload(Buffer.from(encryptedMessage)); + //const encryptedReference = await encryptAesGcm(messageReference, secretHex); + //const encryptedReferenceBytes = Buffer.from(encryptedReference) + //const r = await uploadToRawFeed(bzz, userSelf, topicTmp, writeIndex, encryptedReferenceBytes); + //let r = await this.client.updateFeedWithSalt(chatSession.secret, JSON.stringify(envelope), chatSession.selfWallet); + let r = await this.client.updateFeedWithSalt(this.secret, JSON.stringify(envelope), this.selfWallet); + console.log(r); + //writeIndex += 1; + } + + public setPoller(poller: any) { + this.poller = poller; + } + + public async start() { + this.ping.start(); + this.loop = setTimeout(this.poller, 0, this); + //await this.poller(this); + } }; export { Session }; From 5336356ec0ca2368e3fbca51b2ac8ebf53501a62 Mon Sep 17 00:00:00 2001 From: nolash Date: Tue, 11 Aug 2020 07:57:22 +0200 Subject: [PATCH 3/4] WIP ping payload zero array, likely serialization error in sendenvelope --- src/index.ts | 9 +++++---- src/ping.ts | 14 +++++++++++--- src/session.ts | 10 +++++----- 3 files changed, 21 insertions(+), 12 deletions(-) diff --git a/src/index.ts b/src/index.ts index b83b303..659a71f 100644 --- a/src/index.ts +++ b/src/index.ts @@ -164,14 +164,14 @@ const newSession = (gatewayAddress: string, messageCallback: any) => { while (true) { try { console.debug('poll', session.otherFeed); - const serializedMessage = await session.getOtherFeed(); + const socMessage = await session.getOtherFeed(); //const encryptedReference = await downloadFromFeed(client, otherWallet, socId); //topicTmp); //, readIndex); //const messageReference = await decrypt(encryptedReference, secretHex); //const response = await client.downloadChunk(messageReference); //const encryptedArrayBuffer = await response.arrayBuffer(); //const message = await decrypt(new Uint8Array(encryptedArrayBuffer), secretHex); - const message = JSON.parse(serializedMessage); - console.debug('got', message); + console.debug('got chunk', socMessage); + const message = JSON.parse(socMessage.chunk.data); if (message.type == 'ping') { if (message.pong) { session.ping.ponged(message.serial); @@ -185,7 +185,8 @@ const newSession = (gatewayAddress: string, messageCallback: any) => { session.ping.seen(); messageCallback({ type: 'message', - message: message.chunk.data + //message: message.chunk.data + message: message.data, }); } } catch (e) { diff --git a/src/ping.ts b/src/ping.ts index ecf7dfd..2c1e466 100644 --- a/src/ping.ts +++ b/src/ping.ts @@ -19,6 +19,10 @@ class Ping { this.pingTimeout = PING_TIMEOUT_DEFAULT; } console.debug('initialized ping'); + this.selfSerial = 0; + this.otherSerial = 0; + this.lastSeen = 0; + this.lastPong = 0; } public start() { @@ -34,18 +38,19 @@ class Ping { public restart() { clearTimeout(this.pingTimer); - console.debug('resetting pingtimer'); - this.pingTimer = setTimeout(this.ping, this.pingTimer); + console.debug('resetting pingtimer', this.pingTimer); + this.pingTimer = setTimeout(this.ping, this.pingTimeout); } public ponged(serial: number) { + console.debug('pong last/now', this.selfSerial, serial); this.seen(), this.lastPong = Date.now(); this.otherSerial = serial; } - public ping() { + public ping = (self: any) => { this.restart(); const serial = this.selfSerial; console.debug('sending ping', serial); @@ -54,6 +59,9 @@ class Ping { } public pinged(serial: number) { + if (Date.now() - this.lastSeen < this.pingTimeout) { + console.debug('dropping premature pong reply'); + } console.debug('ping last/now', this.otherSerial, serial); this.seen(); this.pingCallback(serial, true); diff --git a/src/session.ts b/src/session.ts index d700011..59736de 100644 --- a/src/session.ts +++ b/src/session.ts @@ -82,15 +82,15 @@ class Session { clearTimeout(this.loop); } - public async sendMessage(message: string) { + public sendMessage = async (message: string) => { this.ping.restart(); const envelope = { type: 'message', - message, + data: message, } this.sendEnvelope(envelope) } - public async sendPing(serial: number, pong?: boolean) { + public sendPing = async (serial: number, pong?: boolean) => { const envelope = { type: 'ping', pong: pong, @@ -98,7 +98,7 @@ class Session { } this.sendEnvelope(envelope) } - public async sendDisconnect() { + public sendDisconnect = async () => { this.stop(); console.debug('terminated main loop'); const envelope = { @@ -107,7 +107,7 @@ class Session { this.sendEnvelope(envelope) } - public async sendEnvelope (envelope:any) { + public sendEnvelope = async (envelope:any) => { const envelopeJson = JSON.stringify(envelope) //const encryptedMessage = await encryptAesGcm(envelopeJson, secretHex); //const messageReference = await bzz.upload(Buffer.from(encryptedMessage)); From 3590131965e6f59350eecd4869d775af957cd40e Mon Sep 17 00:00:00 2001 From: nolash Date: Fri, 14 Aug 2020 08:07:28 +0200 Subject: [PATCH 4/4] Add bytes encoding of messages --- src/index.ts | 16 +++++++++++++--- src/session.ts | 3 ++- src/start.ts | 4 +++- 3 files changed, 18 insertions(+), 5 deletions(-) diff --git a/src/index.ts b/src/index.ts index 659a71f..ade1913 100644 --- a/src/index.ts +++ b/src/index.ts @@ -153,7 +153,7 @@ async function startResponse(session: any):Promise { } -const newSession = (gatewayAddress: string, messageCallback: any) => { +const newSession = (gatewayAddress: string, messageCallback: any, pingCallback: any, disconnectCallback: any) => { const client = new BeeClient(gatewayAddress); let secretHex = undefined; @@ -171,15 +171,18 @@ const newSession = (gatewayAddress: string, messageCallback: any) => { //const encryptedArrayBuffer = await response.arrayBuffer(); //const message = await decrypt(new Uint8Array(encryptedArrayBuffer), secretHex); console.debug('got chunk', socMessage); - const message = JSON.parse(socMessage.chunk.data); + const messageData = new TextDecoder().decode(socMessage.chunk.data); + const message = JSON.parse(messageData); if (message.type == 'ping') { if (message.pong) { session.ping.ponged(message.serial); } else { session.ping.pinged(message.serial); + pingCallback(message.serial); } } else if (message.type == 'disconnect') { console.debug('disconnect received, terminating main loop'); + disconnectCallback(); return; } else { session.ping.seen(); @@ -206,6 +209,8 @@ const newSession = (gatewayAddress: string, messageCallback: any) => { export function init(params: { gatewayAddress: string, messageCallback: any, + pingCallback: any, + disconnectCallback: any, manifestCallback: ManifestCallback, stateCallback: StateCallback, logFunction: (...args: any[]) => void, @@ -214,7 +219,12 @@ export function init(params: { log('init called'); // TODO: this guy is global. let's pass him around instead, perhaps? - chatSession = newSession(params.gatewayAddress, params.messageCallback); + chatSession = newSession( + params.gatewayAddress, + params.messageCallback, + params.pingCallback, + params.disconnectCallback, + ); if (keyTmpRequestPriv === undefined) { log('start request'); startRequest(chatSession, params.manifestCallback).then(() => { diff --git a/src/session.ts b/src/session.ts index 59736de..fd67e08 100644 --- a/src/session.ts +++ b/src/session.ts @@ -115,7 +115,8 @@ class Session { //const encryptedReferenceBytes = Buffer.from(encryptedReference) //const r = await uploadToRawFeed(bzz, userSelf, topicTmp, writeIndex, encryptedReferenceBytes); //let r = await this.client.updateFeedWithSalt(chatSession.secret, JSON.stringify(envelope), chatSession.selfWallet); - let r = await this.client.updateFeedWithSalt(this.secret, JSON.stringify(envelope), this.selfWallet); + const envelopeJsonBytes = new TextEncoder().encode(envelopeJson); + let r = await this.client.updateFeedWithSalt(this.secret, envelopeJsonBytes, this.selfWallet); console.log(r); //writeIndex += 1; } diff --git a/src/start.ts b/src/start.ts index 8731ece..a130f0f 100644 --- a/src/start.ts +++ b/src/start.ts @@ -2,12 +2,14 @@ import { init } from './index'; // set up the session object function logMessage(msg) { - console.debug("got message", msg.payload()); + console.debug("got message", msg);//.payload()); } init({ gatewayAddress: 'http://localhost:8080', messageCallback: logMessage, + pingCallback: logMessage, + disconnectCallback: logMessage, manifestCallback: () => {}, stateCallback: () => {}, logFunction: console.debug,