node-ssh: initial definition (#42964)

This commit is contained in:
Junxiao Shi
2020-03-06 17:31:38 -05:00
committed by GitHub
parent bd9dc30f38
commit dd3e8a2e85
4 changed files with 143 additions and 0 deletions

87
types/node-ssh/index.d.ts vendored Normal file
View File

@@ -0,0 +1,87 @@
// Type definitions for node-ssh 7.0
// Project: https://github.com/steelbrain/node-ssh
// Definitions by: Junxiao Shi <https://github.com/yoursunny>
// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
/// <reference types="node" />
import { ClientChannel, ExecOptions as ssh2ExecOptions, SFTPWrapper } from "ssh2";
import { TransferOptions } from "ssh2-streams";
declare namespace SSH {
type Shell = ClientChannel;
type SFTP = SFTPWrapper;
interface ConfigGiven {
host: string;
port?: number;
username: string;
password?: string;
privateKey?: string;
onKeyboardInteractive?: () => void | boolean;
}
interface ExecOptions {
cwd?: string;
stdin?: string;
options?: ssh2ExecOptions;
onStdout?: (chunk: Buffer) => void;
onStderr?: (chunk: Buffer) => void;
}
interface ExecResult {
stdout: string;
stderr: string;
code: number;
signal?: string;
}
interface PutFilesOptions {
sftp?: SFTP;
sftpOptions?: TransferOptions;
concurrency?: number;
}
interface PutDirectoryOptions {
sftp?: SFTP;
sftpOptions?: TransferOptions;
concurrency?: number;
recursive?: boolean;
tick?: (localPath: string, remotePath: string, error: Error | null | undefined) => void;
validate?: (localPath: string) => boolean;
}
}
declare class SSH {
constructor();
connect(givenConfig: SSH.ConfigGiven): Promise<this>;
requestShell(): Promise<SSH.Shell>;
requestSFTP(): Promise<SSH.SFTP>;
mkdir(path: string, type: "exec"): Promise<void>;
mkdir(path: string, type?: "sftp", givenSftp?: SSH.SFTP): Promise<void>;
exec(command: string, parameters?: ReadonlyArray<string>,
options?: SSH.ExecOptions & { stream?: "stdout"|"stderr" }): Promise<string>;
exec(command: string, parameters?: ReadonlyArray<string>,
options?: SSH.ExecOptions & { stream: "both" }): Promise<SSH.ExecResult>;
execCommand(givenCommand: string, options?: SSH.ExecOptions): Promise<SSH.ExecResult>;
getFile(localFile: string, remoteFile: string, givenSftp?: SSH.SFTP, givenOpts?: TransferOptions): Promise<void>;
putFile(localFile: string, remoteFile: string, givenSftp?: SSH.SFTP, givenOpts?: TransferOptions): Promise<void>;
putFiles(files: ReadonlyArray<{ local: string, remote: string }>, givenConfig?: SSH.PutFilesOptions): Promise<void>;
putDirectory(localDirectory: string, remoteDirectory: string, givenConfig?: SSH.PutDirectoryOptions): Promise<boolean>;
dispose(): void;
}
export = SSH;

View File

@@ -0,0 +1,32 @@
import SSH = require("node-ssh");
const client = new SSH();
client.connect({
host: "127.0.0.1",
username: "steel",
password: "password",
})
.then(async (client) => {
// $ExpectType SSH
client;
// $ExpectType string
await client.exec("pwd", [], { cwd: "/etc" });
// $ExpectType string
await client.exec("pwd", [], { stream: "stderr" });
// $ExpectType ExecResult
await client.exec("pwd", [], { stream: "both" });
// $ExpectType ExecResult
await client.execCommand("node");
await client.requestShell();
const sftp = await client.requestSFTP();
await client.mkdir("/root");
await client.mkdir("/root", "exec");
await client.mkdir("/root", "sftp", sftp);
await client.getFile("1.txt", "1.txt", sftp);
await client.putFile("1.txt", "1.txt", sftp);
await client.putFiles([{ local: "1.txt", remote: "1.txt" }], { sftp });
await client.putDirectory("/root", "/root", { sftp });
});

View File

@@ -0,0 +1,23 @@
{
"compilerOptions": {
"module": "commonjs",
"lib": [
"es6"
],
"noImplicitAny": true,
"noImplicitThis": true,
"strictFunctionTypes": true,
"strictNullChecks": true,
"baseUrl": "../",
"typeRoots": [
"../"
],
"types": [],
"noEmit": true,
"forceConsistentCasingInFileNames": true
},
"files": [
"index.d.ts",
"node-ssh-tests.ts"
]
}

View File

@@ -0,0 +1 @@
{ "extends": "dtslint/dt.json" }