Skip to content

Commit

Permalink
function overload
Browse files Browse the repository at this point in the history
  • Loading branch information
Leonewu committed Dec 18, 2022
1 parent 58f9df8 commit 2aea983
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 28 deletions.
10 changes: 5 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,6 @@ const result = await sshTunnel.exec('uptime');

- `proxy`
- `exec`
- `batchExec`
- `close`

```typescript
Expand All @@ -99,10 +98,11 @@ const sshConfig = {
privateKey: fs.readFileSync('~/.ssh/myPrivateKey'),
};
const sshTunnel = new SshTunnel(sshConfig);
// execute uptime command
const uptime = await sshTunnel.exec('uptime');
// execute multiple commands one time
const batchRes = await sshTunnel.batchExec([
// execute echo command
const execRes = await sshTunnel.exec('echo 1');
// execRes: '1'
// Also, if passing a command array, it will execute every commands one time and return by order
const batchRes = await sshTunnel.exec([
'echo 1',
'echo 2',
'echo 3'
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"private": false,
"name": "ssh-tunneling",
"version": "1.0.7",
"version": "1.0.8",
"description": "a ssh-tunneling client for nodejs",
"keywords": ["ssh tunnel", "ssh tunneling", "ssh proxy", "ssh port foward"],
"main": "dist/index.js",
Expand Down
58 changes: 36 additions & 22 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -194,10 +194,7 @@ class SshTunnel {
return this.sshPromise;
};

/**
* execute command
*/
public async exec(command: string): Promise<string> {
private async _exec(command: string): Promise<string> {
if (!this.sshClient) {
await this.createSshClient();
}
Expand All @@ -222,6 +219,35 @@ class SshTunnel {
});
}

public exec(command: string): Promise<{ command: string; result: string }>

public exec(command: string[]): Promise<{ command: string; result: string }[]>

/**
* @description execute command
* @params a command or commands array
* @return If passing one command, it will return the command and result after executed.
* @return If passing a command array, it will return an array by order after all of them were executed.
*/
public async exec(command: any): Promise<any> {
if (Array.isArray(command)) {
const divider = '__ssh_tunneling_divider__'
const combinedCommand = command.join(` && echo ${divider} && `);
const res = (await this._exec(combinedCommand)).split(`${divider}\n`);
return command.map((item, i) => {
return {
command: item,
result: res[i]
}
});
}
const result = await this._exec(command);
return {
command,
result
}
}

/**
* ssh hearbeat
*/
Expand Down Expand Up @@ -367,12 +393,17 @@ class SshTunnel {
return proxyConfig;
};


public proxy(proxyConfig: string): Promise<ProxyConfig>

public proxy(proxyConfig: string[]): Promise<ProxyConfig[]>

/**
* @description ssh port forwarding
* @expample proxy('3000:192.168.1.1:3000')
* @expample proxy(['3000:192.168.1.1:3000', '3001:192.168.1.1:3001'])
*/
public proxy = async (proxyConfig: string | string[]) => {
public async proxy (proxyConfig: any): Promise<any> {
if (Array.isArray(proxyConfig)) {
const result: ProxyConfig[] = [];
await proxyConfig.reduce((pre, config) => {
Expand Down Expand Up @@ -419,23 +450,6 @@ class SshTunnel {
targetList.forEach(item => item.server.close());
}

/**
* execute multiple commands
* @params an array of commands
* @return return an executed result array of every command by original order
*/
public batchExec = async (commands: string[]) => {
const divider = '__ssh_tunneling_divider__'
const combinedCommand = commands.join(` && echo ${divider} && `);
const res = (await this.exec(combinedCommand)).split(`${divider}\n`);
return commands.map((command, i) => {
return {
command,
result: res[i]
}
});
}

}

export { logger, SshTunnel }

0 comments on commit 2aea983

Please sign in to comment.