{"library":"ssh2-streams","title":"SSH2 and SFTP Protocol Streams for Node.js","type":"library","description":"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.","language":"javascript","status":"active","last_verified":"Sun Apr 19","install":{"commands":["npm install ssh2-streams"],"cli":null},"imports":["const { SSH2Stream } = require('ssh2-streams');","const { SFTPStream } = require('ssh2-streams');","const { utils } = require('ssh2-streams');","const { constants } = require('ssh2-streams');"],"auth":{"required":false,"env_vars":[]},"links":{"homepage":null,"github":"https://github.com/mscdex/ssh2-streams","docs":null,"changelog":null,"pypi":null,"npm":"https://www.npmjs.com/package/ssh2-streams","openapi_spec":null,"status_page":null,"smithery":null},"quickstart":{"code":"const { SSH2Stream, utils, constants } = require('ssh2-streams');\nconst net = require('net');\n\n// This quickstart demonstrates how to initialize an SSH2Stream and set up\n// basic event listeners to observe the SSH protocol negotiation.\n// In a real-world scenario, you would pipe a connected net.Socket instance\n// to this SSH2Stream to process the raw SSH protocol bytes.\n// ssh2-streams itself does not handle the network connection.\n\nconst stream = new SSH2Stream();\n\n// Listen for the initial SSH protocol header from the remote party\nstream.on('header', (headerInfo) => {\n  console.log('SSH Header Received:', headerInfo);\n  console.log(`Remote software: ${headerInfo.versions.software}`);\n  // A real client would send its own header back: stream.write(Buffer.from('SSH-2.0-MyClient\\r\\n'));\n});\n\n// This event is crucial for host key verification in client implementations.\n// The default behavior is to auto-allow any host key if no handler is present.\nstream.on('fingerprint', (hostKeyBuffer, callback) => {\n  const finger = utils.fingerprint(hostKeyBuffer);\n  console.log('Received Host Key Fingerprint:', finger);\n  // In a production client, you'd compare 'finger' to known_hosts.\n  // For this example, we unconditionally accept the host key.\n  callback(true);\n});\n\n// Event for when new encryption keys have been exchanged\nstream.on('NEWKEYS', () => {\n  console.log('New encryption keys have been successfully exchanged.');\n  // After NEWKEYS, authentication can begin.\n});\n\n// Handle general errors that might occur within the stream processing\nstream.on('error', (err) => {\n  console.error('SSHStream encountered an error:', err.message);\n});\n\n// Listen for a disconnect message from the remote party\nstream.on('DISCONNECT', (reason, reasonCode, description) => {\n  console.warn(`Disconnected by remote: [${reasonCode}] ${reason} - ${description}`);\n});\n\nconsole.log('SSH2Stream initialized. Attach a net.Socket to this stream to start protocol communication.');\nconsole.log('Example of using utilities: SHA256 fingerprint for a dummy key:',\n  utils.fingerprint(Buffer.from('-----BEGIN PUBLIC KEY-----MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAs...-----END PUBLIC KEY-----')));\nconsole.log('Example protocol constant:',\n  `Disconnect Reason: ${constants.DISCONNECT_REASON_CODE_NAMES[constants.DISCONNECT_REASON.HOST_NOT_ALLOWED_TO_CONNECT]}`);\n\n// To make this quickstart runnable, you would pipe a connected net.Socket to the stream:\n/*\nconst clientSocket = net.connect(22, 'localhost', () => {\n  clientSocket.pipe(stream).pipe(clientSocket);\n  // stream.write(...) would send data over the SSH protocol.\n});\n*/","lang":"javascript","description":"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.","tag":null,"tag_description":null,"last_tested":null,"results":[]},"compatibility":null}