Skip to content

Commit 181887c

Browse files
committed
Merge pull request #360 from uProxy/trevj-ssh-line-handling-fix
fix for newline handling on SSH input
2 parents 2718645 + 9e4c9e1 commit 181887c

File tree

2 files changed

+36
-18
lines changed

2 files changed

+36
-18
lines changed

src/cloud/install/installer.ts

Lines changed: 24 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,10 @@
33
/// <reference path='../../../../third_party/typings/node/node.d.ts' />
44
/// <reference path='../../../../third_party/typings/ssh2/ssh2.d.ts' />
55

6+
import arraybuffers = require('../../arraybuffers/arraybuffers');
7+
import linefeeder = require('../../net/linefeeder');
68
import logging = require('../../logging/logging');
9+
import queue = require('../../handler/queue');
710

811
// https://github.com/borisyankov/DefinitelyTyped/blob/master/ssh2/ssh2-tests.ts
912
import * as ssh2 from 'ssh2';
@@ -45,6 +48,23 @@ class CloudInstaller {
4548
return new Promise<string>((F, R) => {
4649
connection.on('ready', () => {
4750
log.debug('logged into server');
51+
52+
var stdout = new queue.Queue<ArrayBuffer, void>();
53+
new linefeeder.LineFeeder(stdout).setSyncHandler((line: string) => {
54+
log.debug('STDOUT: %1', line);
55+
// Search for the URL anywhere in the line so we will
56+
// continue to work in the face of minor changes
57+
// to the install script.
58+
if (line.indexOf(INVITATION_URL_PREFIX) === 0) {
59+
F(line.substring(INVITATION_URL_PREFIX.length));
60+
}
61+
});
62+
63+
var stderr = new queue.Queue<ArrayBuffer, void>();
64+
new linefeeder.LineFeeder(stderr).setSyncHandler((line: string) => {
65+
log.error('STDERR: %1', line);
66+
});
67+
4868
connection.exec(INSTALL_COMMAND, (e: Error, stream: ssh2.Channel) => {
4969
if (e) {
5070
connection.end();
@@ -58,17 +78,10 @@ class CloudInstaller {
5878
R({
5979
message: 'invitation URL not found'
6080
});
61-
}).on('data', function(data: Buffer) {
62-
const output = data.toString();
63-
log.debug('STDOUT: %1', output);
64-
// Search for the URL anywhere in the line so we will
65-
// continue to work in the face of minor changes
66-
// to the install script.
67-
if (output.indexOf(INVITATION_URL_PREFIX) === 0) {
68-
F(output.substring(INVITATION_URL_PREFIX.length));
69-
}
70-
}).stderr.on('data', function(data: Buffer) {
71-
log.error(data.toString());
81+
}).on('data', (data:Buffer) => {
82+
stdout.handle(arraybuffers.bufferToArrayBuffer(data));
83+
}).stderr.on('data', (data: Buffer) => {
84+
stderr.handle(arraybuffers.bufferToArrayBuffer(data));
7285
});
7386
});
7487
}).on('error', (e: Error) => {

src/cloud/social/provider.ts

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -571,9 +571,11 @@ class Connection {
571571
});
572572
}
573573

574-
// Executes a command, fulfilling with the command's stdout
575-
// or rejecting if output is received on stderr.
576-
private exec_ = (command:string): Promise<string> => {
574+
// Executes a command, fulfilling with the first line of the command's
575+
// output on stdout or rejecting if any output is received on stderr.
576+
// TODO: There is a close event with a return code which
577+
// is probably a better indication of success.
578+
private exec_ = (command: string): Promise<string> => {
577579
log.debug('%1: execute command: %2', this.name_, command);
578580
if (this.state_ !== ConnectionState.ESTABLISHED) {
579581
return Promise.reject(new Error('can only execute commands in ESTABLISHED state'));
@@ -587,13 +589,16 @@ class Connection {
587589
return;
588590
}
589591

590-
// TODO: There is a close event with a return code which
591-
// is probably a better indication of success.
592+
var stdout = new queue.Queue<ArrayBuffer, void>();
593+
new linefeeder.LineFeeder(stdout).setSyncHandler((line: string) => {
594+
F(line);
595+
});
596+
592597
stream.on('data', (data: Buffer) => {
593-
F(data.toString());
598+
stdout.handle(arraybuffers.bufferToArrayBuffer(data));
594599
}).stderr.on('data', (data: Buffer) => {
595600
R({
596-
message: 'command output to STDERR: ' + data.toString()
601+
message: 'output received on STDERR: ' + data.toString()
597602
});
598603
}).on('end', () => {
599604
log.debug('%1: exec stream end', this.name_);

0 commit comments

Comments
 (0)