ssh2-streams is a low-level Node.js library that provides direct, stream-based implementations of the SSH2 and SFTPv3 client/server protocols. It serves as a foundational component for higher-level SSH libraries, such as `ssh2`, offering granular control over the protocol handshake, channel management, and data transfer mechanisms. The current stable version is 0.4.10, and as a core utility, its release cadence is typically driven by security patches, bug fixes, and minor protocol compliance updates rather than rapid feature development. Its primary differentiators include its efficient Node.js stream integration, allowing for flexible and performant handling of network I/O, and its commitment to exposing the raw protocol events and structures, enabling developers to build custom SSH or SFTP solutions with deep control over the underlying communication. It requires Node.js v5.10.0 or newer.
npm install ssh2-streamsVerified import paths — ran on the pinned version, not inferred.
This quickstart initializes an SSH2Stream, attaches essential event listeners for protocol observation, and demonstrates basic utility usage. It clarifies that the stream requires a `net.Socket` for actual network communication.
Always implement a `stream.on('fingerprint', (hostKey, callback) => { ... })` handler in client code to verify the host key against known records (e.g., `~/.ssh/known_hosts`) and call `callback(true)` only if verified.Ensure you create and manage a `net.Socket` (or similar network stream) and correctly pipe data between the socket and the `SSH2Stream` instance. Example: `socket.pipe(sshStream).pipe(socket);`
Implement proper backpressure control mechanisms when reading from and writing to the stream, using `stream.pause()`, `stream.resume()`, and monitoring return values of `stream.write()` to buffer data appropriately.
Listen for the generic `stream.on('error', (err) => { ... })` event for parser/stream-level issues, but also specific protocol events like `DISCONNECT`, `CHANNEL_OPEN_FAILURE`, etc., to gracefully handle protocol-level errors.Always instantiate `SSH2Stream` using the `new` keyword: `const stream = new SSH2Stream();`
Ensure no data is written to the stream after it has ended or been closed. Check stream state before writing, or handle 'close' and 'end' events to prevent further writes.
This often points to a protocol-level issue. Review the data flow, ensure correct packet boundaries, and potentially increase the maximum buffer size if legitimately handling extremely large SSH packets (though usually, this indicates an underlying problem).
Ensure `ssh2-streams` is installed (`npm install ssh2-streams`) and that you are correctly destructuring the named export: `const { SSH2Stream } = require('ssh2-streams');`.No dependency data recorded yet.