Simplify tryGetHome and remove unused (now) streams.ts

feature/ssh-config
Kelvin Schoofs 4 years ago
parent b071a32ed4
commit 8c0b5c4d1a

@ -13,11 +13,9 @@ import type { Navigation } from './webviewMessages';
async function tryGetHome(ssh: Client): Promise<string | null> { async function tryGetHome(ssh: Client): Promise<string | null> {
const exec = await toPromise<ClientChannel>(cb => ssh.exec('echo Home: ~', cb)); const exec = await toPromise<ClientChannel>(cb => ssh.exec('echo Home: ~', cb));
const { MemoryDuplex } = await import('./streams'); let home = '';
const stdout = new MemoryDuplex(); exec.stdout.on('data', (chunk: any) => home += chunk);
exec.stdout.pipe(stdout);
await toPromise(cb => exec.on('close', cb)); await toPromise(cb => exec.on('close', cb));
const home = stdout.read().toString();
if (!home) return null; if (!home) return null;
const mat = home.match(/^Home: (.*?)\r?\n?$/); const mat = home.match(/^Home: (.*?)\r?\n?$/);
if (!mat) return null; if (!mat) return null;

@ -1,48 +0,0 @@
import { Duplex, Writable } from 'stream';
export class MemoryDuplex extends Duplex {
protected buffer = Buffer.alloc(this.size);
protected buffered = 0;
protected doPush = false;
constructor(public readonly size: number = 4092) {
super();
}
// tslint:disable-next-line:function-name
public _write(chunk: any, encoding: string, callback: (err?: Error) => void) {
const buffer = chunk instanceof Buffer ? chunk : Buffer.from(chunk.toString(), encoding as BufferEncoding);
const end = this.buffered + buffer.length;
if (end > this.size) {
return callback(new Error('Buffer overflow'));
}
this.buffered += buffer.copy(this.buffer, this.buffered, 0);
if (this.doPush) this._read();
callback();
}
// tslint:disable-next-line:function-name
public _read() {
const slice = this.buffer.slice(0, this.buffered);
this.buffered = 0;
this.buffer = Buffer.alloc(this.size);
this.doPush = this.push(slice);
}
public bytesBuffered() {
return this.buffered;
}
}
export class WritableFunctionStream extends Writable {
constructor(protected func: (data: Buffer) => void | Promise<void>) {
super();
}
// tslint:disable-next-line:function-name
public async _write(chunk: any, encoding: string, callback: (err?: Error) => void) {
const buffer = chunk instanceof Buffer ? chunk : Buffer.from(chunk.toString(), encoding as BufferEncoding);
try {
await this.func(buffer);
callback();
} catch (e) {
callback(e);
}
}
}
Loading…
Cancel
Save