From 07ccd9791ba6dc15ec3b5da7ae41b7b12fd8e627 Mon Sep 17 00:00:00 2001 From: Mohammad Kermani Date: Mon, 19 Aug 2024 12:57:35 +0330 Subject: [PATCH 1/2] refactor(utils): show diff log for update events in `addEventListeners` The logs for update events are so long, so a diff view will make it shorter. --- package-lock.json | 62 +++++++++++++++++++++++++ packages/utils/lib/addEventListeners.ts | 36 ++++++++++---- packages/utils/package.json | 2 + 3 files changed, 92 insertions(+), 8 deletions(-) diff --git a/package-lock.json b/package-lock.json index a182aad..0179a0a 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1693,6 +1693,14 @@ "node": "^12.22.0 || ^14.17.0 || >=16.0.0" } }, + "node_modules/@ewoudenberg/difflib": { + "version": "0.1.0", + "resolved": "https://registry.npmjs.org/@ewoudenberg/difflib/-/difflib-0.1.0.tgz", + "integrity": "sha512-OU5P5mJyD3OoWYMWY+yIgwvgNS9cFAU10f+DDuvtogcWQOoJIsQ4Hy2McSfUfhKjq8L0FuWVb4Rt7kgA+XK86A==", + "dependencies": { + "heap": ">= 0.2.0" + } + }, "node_modules/@humanwhocodes/config-array": { "version": "0.11.14", "resolved": "https://registry.npmjs.org/@humanwhocodes/config-array/-/config-array-0.11.14.tgz", @@ -2673,6 +2681,12 @@ "integrity": "sha512-2QF/t/auWm0lsy8XtKVPG19v3sSOQlJe/YHZgfjb/KBBHOGSV+J2q/S671rcq9uTBrLAXmZpqJiaQbMT+zNU1w==", "dev": true }, + "node_modules/@types/json-diff": { + "version": "1.0.3", + "resolved": "https://registry.npmjs.org/@types/json-diff/-/json-diff-1.0.3.tgz", + "integrity": "sha512-Qvxm8fpRMv/1zZR3sQWImeRK2mBYJji20xF51Fq9Gt//Ed18u0x6/FNLogLS1xhfUWTEmDyqveJqn95ltB6Kvw==", + "dev": true + }, "node_modules/@types/json-schema": { "version": "7.0.15", "resolved": "https://registry.npmjs.org/@types/json-schema/-/json-schema-7.0.15.tgz", @@ -3700,6 +3714,14 @@ "dev": true, "license": "MIT" }, + "node_modules/colors": { + "version": "1.4.0", + "resolved": "https://registry.npmjs.org/colors/-/colors-1.4.0.tgz", + "integrity": "sha512-a+UqTh4kgZg/SlGvfbzDHpgRu7AAQOmmqRHJnxhRZICKFUT91brVhNNt58CMWU9PsBbv3PDCZUHbVxuDiH2mtA==", + "engines": { + "node": ">=0.1.90" + } + }, "node_modules/commander": { "version": "9.4.1", "dev": true, @@ -4046,6 +4068,17 @@ "node": ">=6.0.0" } }, + "node_modules/dreamopt": { + "version": "0.8.0", + "resolved": "https://registry.npmjs.org/dreamopt/-/dreamopt-0.8.0.tgz", + "integrity": "sha512-vyJTp8+mC+G+5dfgsY+r3ckxlz+QMX40VjPQsZc5gxVAxLmi64TBoVkP54A/pRAXMXsbu2GMMBrZPxNv23waMg==", + "dependencies": { + "wordwrap": ">=0.0.2" + }, + "engines": { + "node": ">=0.4.0" + } + }, "node_modules/eastasianwidth": { "version": "0.2.0", "dev": true, @@ -5043,6 +5076,11 @@ "node": ">= 0.4" } }, + "node_modules/heap": { + "version": "0.2.7", + "resolved": "https://registry.npmjs.org/heap/-/heap-0.2.7.tgz", + "integrity": "sha512-2bsegYkkHO+h/9MGbn6KWcE45cHZgPANo5LXF7EvWdT0yT2EguSVO1nDgU5c8+ZOPwp2vMNa7YFsJhVcDR9Sdg==" + }, "node_modules/hosted-git-info": { "version": "2.8.9", "resolved": "https://registry.npmjs.org/hosted-git-info/-/hosted-git-info-2.8.9.tgz", @@ -5834,6 +5872,22 @@ "resolved": "https://registry.npmjs.org/json-buffer/-/json-buffer-3.0.1.tgz", "integrity": "sha512-4bV5BfR2mqfQTJm+V5tPPdf+ZpuhiIvTuAB5g8kcrXOZpTT/QwwVRWBywX1ozr6lEuPdbHxwaJlm9G6mI2sfSQ==" }, + "node_modules/json-diff": { + "version": "1.0.6", + "resolved": "https://registry.npmjs.org/json-diff/-/json-diff-1.0.6.tgz", + "integrity": "sha512-tcFIPRdlc35YkYdGxcamJjllUhXWv4n2rK9oJ2RsAzV4FBkuV4ojKEDgcZ+kpKxDmJKv+PFK65+1tVVOnSeEqA==", + "dependencies": { + "@ewoudenberg/difflib": "0.1.0", + "colors": "^1.4.0", + "dreamopt": "~0.8.0" + }, + "bin": { + "json-diff": "bin/json-diff.js" + }, + "engines": { + "node": "*" + } + }, "node_modules/json-parse-even-better-errors": { "version": "2.3.1", "resolved": "https://registry.npmjs.org/json-parse-even-better-errors/-/json-parse-even-better-errors-2.3.1.tgz", @@ -9071,6 +9125,11 @@ "node": ">=8" } }, + "node_modules/wordwrap": { + "version": "1.0.0", + "resolved": "https://registry.npmjs.org/wordwrap/-/wordwrap-1.0.0.tgz", + "integrity": "sha512-gvVzJFlPycKc5dZN4yPkP8w7Dc37BtP1yczEneOb4uq34pXZcvrtRTmWV8W+Ume+XCxKgbjM+nevkyFPMybd4Q==" + }, "node_modules/wrap-ansi": { "version": "7.0.0", "dev": true, @@ -9926,11 +9985,13 @@ "@libp2p/crypto": "^4.0.2", "@libp2p/peer-id-factory": "^4.0.6", "@rosen-bridge/logger-interface": "^0.2.0", + "json-diff": "^1.0.6", "libp2p": "^1.2.3", "multiformats": "^13.1.0" }, "devDependencies": { "@multiformats/multiaddr": "^12.2.1", + "@types/json-diff": "^1.0.3", "@types/node": "^20.11.9", "@typescript-eslint/eslint-plugin": "^6.19.1", "@typescript-eslint/parser": "^6.19.1", @@ -10065,6 +10126,7 @@ } }, "tests/scaling-large-messages": { + "name": "@rosenet-tests/scaling-large-messages", "version": "0.0.0", "license": "GPL-3.0", "dependencies": { diff --git a/packages/utils/lib/addEventListeners.ts b/packages/utils/lib/addEventListeners.ts index dc22979..3cee999 100644 --- a/packages/utils/lib/addEventListeners.ts +++ b/packages/utils/lib/addEventListeners.ts @@ -1,6 +1,13 @@ import { Libp2p } from 'libp2p'; +import { diffString, DiffStringOptions } from 'json-diff'; import { AbstractLogger } from '@rosen-bridge/logger-interface'; +const jsonDiffOptions: DiffStringOptions = { + color: false, + sort: true, + maxElisions: 0, +}; + /** * Log different events of a RoseNet node or relay * @@ -56,38 +63,51 @@ const addEventListeners = ( }); }); node.addEventListener('peer:update', ({ detail: peerUpdate }) => { - logger.debug('Peer updated', { - id: peerUpdate.peer.id.toString(), - previous: { + const diff = diffString( + { multiAddresses: peerUpdate.previous?.addresses.map((address) => address.multiaddr.toString(), ), protocols: peerUpdate.previous?.protocols, }, - current: { + { multiAddresses: peerUpdate.peer.addresses.map((address) => address.multiaddr.toString(), ), protocols: peerUpdate.peer.protocols, }, + jsonDiffOptions, + ); + + if (!diff) return; + + logger.debug('Peer updated', { + id: peerUpdate.peer.id.toString(), }); + logger.debug(diff); }); node.addEventListener('self:peer:update', ({ detail: peerUpdate }) => { - logger.debug('Our own peer updated', { - previous: { + const diff = diffString( + { multiAddresses: peerUpdate.previous?.addresses.map((address) => address.multiaddr.toString(), ), protocols: peerUpdate.previous?.protocols, }, - current: { + { multiAddresses: peerUpdate.peer.addresses.map((address) => address.multiaddr.toString(), ), protocols: peerUpdate.peer.protocols, }, - }); + jsonDiffOptions, + ); + + if (!diff) return; + + logger.debug('Our own peer updated'); + logger.debug(diff); }); node.addEventListener('start', () => { diff --git a/packages/utils/package.json b/packages/utils/package.json index 97a0815..da330c8 100644 --- a/packages/utils/package.json +++ b/packages/utils/package.json @@ -19,6 +19,7 @@ }, "devDependencies": { "@multiformats/multiaddr": "^12.2.1", + "@types/json-diff": "^1.0.3", "@types/node": "^20.11.9", "@typescript-eslint/eslint-plugin": "^6.19.1", "@typescript-eslint/parser": "^6.19.1", @@ -38,6 +39,7 @@ "@libp2p/crypto": "^4.0.2", "@libp2p/peer-id-factory": "^4.0.6", "@rosen-bridge/logger-interface": "^0.2.0", + "json-diff": "^1.0.6", "libp2p": "^1.2.3", "multiformats": "^13.1.0" } From 9f18eaa3b4df471e355df13dac7ddc53fbd2c426 Mon Sep 17 00:00:00 2001 From: Mohammad Kermani Date: Mon, 19 Aug 2024 13:04:29 +0330 Subject: [PATCH 2/2] chore: add a changeset --- .changeset/chilled-eagles-wait.md | 5 +++++ 1 file changed, 5 insertions(+) create mode 100644 .changeset/chilled-eagles-wait.md diff --git a/.changeset/chilled-eagles-wait.md b/.changeset/chilled-eagles-wait.md new file mode 100644 index 0000000..631d20c --- /dev/null +++ b/.changeset/chilled-eagles-wait.md @@ -0,0 +1,5 @@ +--- +'@rosen-bridge/rosenet-utils': minor +--- + +Show only a diff log for update events in `addEventListeners`