ssh2
ssh2 copied to clipboard
is it possible to avoid circular dependencies?
Hi,
I am using rollup to bundle the source code which uses ssh2. However the tool complains that ssh2 contains circular dependencies.
Specifically, these files are relevant:
https://github.com/mscdex/ssh2/blob/6b4c64ce5c16f488d90b87997aa0f19c871abe2f/lib/protocol/utils.js#L174
https://github.com/mscdex/ssh2/blob/6b4c64ce5c16f488d90b87997aa0f19c871abe2f/lib/protocol/handlers.js#L5
https://github.com/mscdex/ssh2/blob/6b4c64ce5c16f488d90b87997aa0f19c871abe2f/lib/protocol/kex.js#L1743
Although these lines are valid code in commonjs, is it possible to remove circular dependencies? Potential benefits are avoiding potential bugs and making downstream bundler like rollup webpack work.
Thank you so much! :-)
So looking into it, util.js looks like an easy fix but kex.js not as simple with regard to organization. I can create a repo that avoids the circular dependencies but the issue here is that kex allows a "debug" mode. And here it basically allows any type of message handler to be used. As a result, what normally would just be used in Protocol.js is also used in kex.js. A simple enough fix is to just put the MESSAGE_HANDLERS inside kex and export it from there but from an organizational standpoint that's definitely not where it belongs. Nonetheless, heres a diff if you want to make the changes yourself. I don't think this is pull request worthy but I hope it can get you back on your feet. Happy coding! : )
diff --git a/lib/protocol/Protocol.js b/lib/protocol/Protocol.js
index 94e12bc..b3e52ec 100644
--- a/lib/protocol/Protocol.js
+++ b/lib/protocol/Protocol.js
@@ -57,7 +57,7 @@ const {
const {
parseKey,
} = require('./keyParser.js');
-const MESSAGE_HANDLERS = require('./handlers.js');
+const { MESSAGE_HANDLERS } = require('./kex.js');
const {
bufferCopy,
bufferFill,
diff --git a/lib/protocol/kex.js b/lib/protocol/kex.js
index 49b28f5..27cb447 100644
--- a/lib/protocol/kex.js
+++ b/lib/protocol/kex.js
@@ -47,7 +47,22 @@ const {
ZlibPacketWriter,
} = require('./zlib.js');
-let MESSAGE_HANDLERS;
+const KEX_HANDLERS = {
+ [MESSAGE.KEXINIT]: handleKexInit,
+};
+
+const MESSAGE_HANDLERS = new Array(256);
+[
+ KEX_HANDLERS,
+ require('./handlers.misc.js'),
+].forEach((handlers) => {
+ // eslint-disable-next-line prefer-const
+ for (let [type, handler] of Object.entries(handlers)) {
+ type = +type;
+ if (isFinite(type) && type >= 0 && type < MESSAGE_HANDLERS.length)
+ MESSAGE_HANDLERS[type] = handler;
+ }
+});
const GEX_MIN_BITS = 2048; // RFC 8270
const GEX_MAX_BITS = 8192; // RFC 8270
@@ -1739,8 +1754,6 @@ function onKEXPayload(state, payload) {
case MESSAGE.IGNORE:
case MESSAGE.UNIMPLEMENTED:
case MESSAGE.DEBUG:
- if (!MESSAGE_HANDLERS)
- MESSAGE_HANDLERS = require('./handlers.js');
return MESSAGE_HANDLERS[type](this, payload);
case MESSAGE.KEXINIT:
if (!state.firstPacket) {
@@ -1825,7 +1838,6 @@ module.exports = {
lang: [],
},
}),
- HANDLERS: {
- [MESSAGE.KEXINIT]: handleKexInit,
- },
+ HANDLERS: KEX_HANDLERS,
+ MESSAGE_HANDLERS: MESSAGE_HANDLERS
};