From dd3e8a2e85e726b6c345a24fa14285977e5e2ca2 Mon Sep 17 00:00:00 2001 From: Junxiao Shi Date: Fri, 6 Mar 2020 17:31:38 -0500 Subject: [PATCH] node-ssh: initial definition (#42964) --- types/node-ssh/index.d.ts | 87 ++++++++++++++++++++++++++++++++ types/node-ssh/node-ssh-tests.ts | 32 ++++++++++++ types/node-ssh/tsconfig.json | 23 +++++++++ types/node-ssh/tslint.json | 1 + 4 files changed, 143 insertions(+) create mode 100644 types/node-ssh/index.d.ts create mode 100644 types/node-ssh/node-ssh-tests.ts create mode 100644 types/node-ssh/tsconfig.json create mode 100644 types/node-ssh/tslint.json diff --git a/types/node-ssh/index.d.ts b/types/node-ssh/index.d.ts new file mode 100644 index 0000000000..9270c6f476 --- /dev/null +++ b/types/node-ssh/index.d.ts @@ -0,0 +1,87 @@ +// Type definitions for node-ssh 7.0 +// Project: https://github.com/steelbrain/node-ssh +// Definitions by: Junxiao Shi +// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped + +/// + +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; + + requestShell(): Promise; + + requestSFTP(): Promise; + + mkdir(path: string, type: "exec"): Promise; + + mkdir(path: string, type?: "sftp", givenSftp?: SSH.SFTP): Promise; + + exec(command: string, parameters?: ReadonlyArray, + options?: SSH.ExecOptions & { stream?: "stdout"|"stderr" }): Promise; + + exec(command: string, parameters?: ReadonlyArray, + options?: SSH.ExecOptions & { stream: "both" }): Promise; + + execCommand(givenCommand: string, options?: SSH.ExecOptions): Promise; + + getFile(localFile: string, remoteFile: string, givenSftp?: SSH.SFTP, givenOpts?: TransferOptions): Promise; + + putFile(localFile: string, remoteFile: string, givenSftp?: SSH.SFTP, givenOpts?: TransferOptions): Promise; + + putFiles(files: ReadonlyArray<{ local: string, remote: string }>, givenConfig?: SSH.PutFilesOptions): Promise; + + putDirectory(localDirectory: string, remoteDirectory: string, givenConfig?: SSH.PutDirectoryOptions): Promise; + + dispose(): void; +} + +export = SSH; diff --git a/types/node-ssh/node-ssh-tests.ts b/types/node-ssh/node-ssh-tests.ts new file mode 100644 index 0000000000..a74732ffde --- /dev/null +++ b/types/node-ssh/node-ssh-tests.ts @@ -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 }); +}); diff --git a/types/node-ssh/tsconfig.json b/types/node-ssh/tsconfig.json new file mode 100644 index 0000000000..70a77635fd --- /dev/null +++ b/types/node-ssh/tsconfig.json @@ -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" + ] +} diff --git a/types/node-ssh/tslint.json b/types/node-ssh/tslint.json new file mode 100644 index 0000000000..3db14f85ea --- /dev/null +++ b/types/node-ssh/tslint.json @@ -0,0 +1 @@ +{ "extends": "dtslint/dt.json" }