Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

More maintenance #469

Merged
merged 5 commits into from
Aug 6, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion .prettierrc
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,10 @@
"printWidth": 125,
"singleQuote": true,
"trailingComma": "all",
"parser": "typescript"
"parser": "typescript",
"importOrder": [
"^[./]"
],
"importOrderSeparation": true,
"importOrderSortSpecifiers": true
}
285 changes: 126 additions & 159 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -105,189 +105,156 @@ ssh.connect({

```ts
// API reference in Typescript typing format:
import SSH2, {
AcceptConnection,
Channel,
ClientChannel,
ConnectConfig,
ExecOptions,
Prompt,
PseudoTtyOptions,
RejectConnection,
SFTPWrapper,
ShellOptions,
TcpConnectionDetails,
TransferOptions,
UNIXConnectionDetails,
} from 'ssh2'
import stream from 'stream'
import { Client, ConnectConfig, ClientChannel, SFTPWrapper, ExecOptions, PseudoTtyOptions | ShellOptions } from 'ssh2';
import { Prompt, TransferOptions } from 'ssh2-streams';

// ^ You do NOT need to import these package, these are here for reference of where the
// types are coming from.

declare type Config = ConnectConfig & {
host?: string;
port?: number;
username?: string;
password?: string;
privateKeyPath?: string;
privateKey?: string;
passphrase?: string;
tryKeyboard?: boolean;
onKeyboardInteractive?: (
name: string,
instructions: string,
lang: string,
prompts: Prompt[],
finish: (responses: string[]) => void
) => void;
};

interface SSHExecCommandOptions {
cwd?: string;
stdin?: string | stream.Readable;
execOptions?: ExecOptions;
encoding?: BufferEncoding;
onChannel?: (clientChannel: ClientChannel) => void;
onStdout?: (chunk: Buffer) => void;
onStderr?: (chunk: Buffer) => void;
export type Config = ConnectConfig & {
password?: string
privateKey?: string
privateKeyPath?: string
tryKeyboard?: boolean
onKeyboardInteractive?: (
name: string,
instructions: string,
lang: string,
prompts: Prompt[],
finish: (responses: string[]) => void,
) => void
}

interface SSHExecCommandResponse {
stdout: string;
stderr: string;
code: number | null;
signal: string | null;
export interface SSHExecCommandOptions {
cwd?: string
stdin?: string | stream.Readable
execOptions?: ExecOptions
encoding?: BufferEncoding
onChannel?: (clientChannel: ClientChannel) => void
onStdout?: (chunk: Buffer) => void
onStderr?: (chunk: Buffer) => void
}

interface SSHExecOptions extends SSHExecCommandOptions {
stream?: 'stdout' | 'stderr' | 'both';
export interface SSHExecCommandResponse {
stdout: string
stderr: string
code: number | null
signal: string | null
}

interface SSHPutFilesOptions {
sftp?: SFTPWrapper | null;
concurrency?: number;
transferOptions?: TransferOptions;
export interface SSHExecOptions extends SSHExecCommandOptions {
stream?: 'stdout' | 'stderr' | 'both'
}

interface SSHGetPutDirectoryOptions extends SSHPutFilesOptions {
tick?: (localFile: string, remoteFile: string, error: Error | null) => void;
validate?: (path: string) => boolean;
recursive?: boolean;
export interface SSHPutFilesOptions {
sftp?: SFTPWrapper | null
concurrency?: number
transferOptions?: TransferOptions
}

type SSHForwardInListener = (
export interface SSHGetPutDirectoryOptions extends SSHPutFilesOptions {
tick?: (localFile: string, remoteFile: string, error: Error | null) => void
validate?: (path: string) => boolean
recursive?: boolean
}
export type SSHMkdirMethod = 'sftp' | 'exec'
export type SSHForwardInListener = (
details: TcpConnectionDetails,
accept: AcceptConnection<ClientChannel>,
reject: RejectConnection,
) => void
interface SSHForwardInDetails {
export interface SSHForwardInDetails {
dispose(): Promise<void>
port: number
}

type SSHForwardInStreamLocalListener = (
export type SSHForwardInStreamLocalListener = (
info: UNIXConnectionDetails,
accept: AcceptConnection,
reject: RejectConnection,
) => void
interface SSHForwardInStreamLocalDetails {
export interface SSHForwardInStreamLocalDetails {
dispose(): Promise<void>
}

class NodeSSH {
connection: Client | null;

connect(config: Config): Promise<this>;

isConnected(): boolean;

requestShell(
options?: PseudoTtyOptions | ShellOptions | false
): Promise<ClientChannel>;

withShell(
callback: (channel: ClientChannel) => Promise<void>,
options?: PseudoTtyOptions | ShellOptions | false
): Promise<void>;

requestSFTP(): Promise<SFTPWrapper>;

withSFTP(
callback: (sftp: SFTPWrapper) => Promise<void>
): Promise<void>;

execCommand(
command: string,
options?: SSHExecCommandOptions
): Promise<SSHExecCommandResponse>;

exec(
command: string,
parameters: string[],
options?: SSHExecOptions & {
stream?: 'stdout' | 'stderr';
}
): Promise<string>;

exec(
command: string,
parameters: string[],
options?: SSHExecOptions & {
stream: 'both';
}
): Promise<SSHExecCommandResponse>;

mkdir(
path: string,
method?: 'sftp' | 'exec',
sftp?: SFTPWrapper | null
): Promise<void>;

getFile(
localFile: string,
remoteFile: string,
sftp?: SFTPWrapper | null,
transferOptions?: TransferOptions | null
): Promise<void>;

putFile(
localFile: string,
remoteFile: string,
sftp?: SFTPWrapper | null,
transferOptions?: TransferOptions | null
): Promise<void>;

putFiles(files: Array<{
local: string;
remote: string;
}>, options?: SSHPutFilesOptions): Promise<void>;

putDirectory(
localDirectory: string,
remoteDirectory: string,
options?: SSHGetPutDirectoryOptions
): Promise<boolean>;

getDirectory(
localDirectory: string,
remoteDirectory: string,
options?: SSHGetPutDirectoryOptions
): Promise<boolean>;


forwardIn(
remoteAddr: string,
remotePort: number,
onConnection?: SSHForwardInListener
): Promise<SSHForwardInDetails>;

forwardOut(
srcIP: string,
srcPort: number,
dstIP: string,
dstPort: number
): Promise<Channel>;

forwardInStreamLocal(
socketPath: string,
onConnection?: SSHForwardInStreamLocalListener,
): Promise<SSHForwardInStreamLocalDetails>;

forwardOutStreamLocal(
socketPath: string
): Promise<Channel>;

dispose(): void;
export class SSHError extends Error {
code: string | null
constructor(message: string, code?: string | null)
}

module.exports = NodeSSH;
export class NodeSSH {
connection: SSH2.Client | null

connect(config: Config): Promise<this>
isConnected(): boolean
requestShell(options?: PseudoTtyOptions | ShellOptions | false): Promise<ClientChannel>
withShell(
callback: (channel: ClientChannel) => Promise<void>,
options?: PseudoTtyOptions | ShellOptions | false,
): Promise<void>
requestSFTP(): Promise<SFTPWrapper>
withSFTP(callback: (sftp: SFTPWrapper) => Promise<void>): Promise<void>
execCommand(givenCommand: string, options?: SSHExecCommandOptions): Promise<SSHExecCommandResponse>
exec(
command: string,
parameters: string[],
options?: SSHExecOptions & {
stream?: 'stdout' | 'stderr'
},
): Promise<string>
exec(
command: string,
parameters: string[],
options?: SSHExecOptions & {
stream: 'both'
},
): Promise<SSHExecCommandResponse>
mkdir(path: string, method?: SSHMkdirMethod, givenSftp?: SFTPWrapper | null): Promise<void>
getFile(
localFile: string,
remoteFile: string,
givenSftp?: SFTPWrapper | null,
transferOptions?: TransferOptions | null,
): Promise<void>
putFile(
localFile: string,
remoteFile: string,
givenSftp?: SFTPWrapper | null,
transferOptions?: TransferOptions | null,
): Promise<void>
putFiles(
files: {
local: string
remote: string
}[],
{ concurrency, sftp: givenSftp, transferOptions }?: SSHPutFilesOptions,
): Promise<void>
putDirectory(
localDirectory: string,
remoteDirectory: string,
{ concurrency, sftp: givenSftp, transferOptions, recursive, tick, validate }?: SSHGetPutDirectoryOptions,
): Promise<boolean>
getDirectory(
localDirectory: string,
remoteDirectory: string,
{ concurrency, sftp: givenSftp, transferOptions, recursive, tick, validate }?: SSHGetPutDirectoryOptions,
): Promise<boolean>
forwardIn(remoteAddr: string, remotePort: number, onConnection?: SSHForwardInListener): Promise<SSHForwardInDetails>
forwardOut(srcIP: string, srcPort: number, dstIP: string, dstPort: number): Promise<Channel>
forwardInStreamLocal(
socketPath: string,
onConnection?: SSHForwardInStreamLocalListener,
): Promise<SSHForwardInStreamLocalDetails>
forwardOutStreamLocal(socketPath: string): Promise<Channel>
dispose(): void
}
```

### Typescript support
Expand Down
5 changes: 2 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
],
"homepage": "https://github.com/steelbrain/node-ssh#readme",
"devDependencies": {
"@trivago/prettier-plugin-sort-imports": "^4.2.0",
"@types/node": "18",
"@types/shell-escape": "^0.2.1",
"@types/ssh2": "^1.11.13",
Expand All @@ -58,9 +59,7 @@
"make-dir": "^3.1.0",
"sb-promise-queue": "^2.1.0",
"sb-scandir": "^3.1.0",
"shell-escape": "^0.2.0"
},
"peerDependencies": {
"shell-escape": "^0.2.0",
"ssh2": "^1.14.0"
},
"ava": {
Expand Down
27 changes: 14 additions & 13 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,27 +1,28 @@
import invariant, { AssertionError } from 'assert'
import fs from 'fs'
import fsPath from 'path'
import makeDir from 'make-dir'
import stream from 'stream'
import isStream from 'is-stream'
import shellEscape from 'shell-escape'
import scanDirectory from 'sb-scandir'
import makeDir from 'make-dir'
import fsPath from 'path'
import { PromiseQueue } from 'sb-promise-queue'
import invariant, { AssertionError } from 'assert'
import scanDirectory from 'sb-scandir'
import shellEscape from 'shell-escape'
import SSH2, {
ConnectConfig,
AcceptConnection,
Channel,
ClientChannel,
SFTPWrapper,
ConnectConfig,
ExecOptions,
Prompt,
PseudoTtyOptions,
RejectConnection,
SFTPWrapper,
ShellOptions,
Channel,
Stats,
TcpConnectionDetails,
AcceptConnection,
RejectConnection,
TransferOptions,
UNIXConnectionDetails,
} from 'ssh2'
// eslint-disable-next-line import/no-extraneous-dependencies
import { Prompt, Stats, TransferOptions } from 'ssh2-streams'
import stream from 'stream'

export type Config = ConnectConfig & {
password?: string
Expand Down
Loading