Skip to content

Commit 2514331

Browse files
author
Daniel Borkan
committed
Merge branch 'master' of github.com:uproxy/uproxy-lib into dborkan-cloud-invites
2 parents d0da8cc + 181887c commit 2514331

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
@@ -546,9 +546,11 @@ class Connection {
546546
return this.exec_('cat /banner');
547547
}
548548

549-
// Executes a command, fulfilling with the command's stdout
550-
// or rejecting if output is received on stderr.
551-
private exec_ = (command:string): Promise<string> => {
549+
// Executes a command, fulfilling with the first line of the command's
550+
// output on stdout or rejecting if any output is received on stderr.
551+
// TODO: There is a close event with a return code which
552+
// is probably a better indication of success.
553+
private exec_ = (command: string): Promise<string> => {
552554
log.debug('%1: execute command: %2', this.name_, command);
553555
if (this.state_ !== ConnectionState.ESTABLISHED) {
554556
return Promise.reject(new Error('can only execute commands in ESTABLISHED state'));
@@ -562,13 +564,16 @@ class Connection {
562564
return;
563565
}
564566

565-
// TODO: There is a close event with a return code which
566-
// is probably a better indication of success.
567+
var stdout = new queue.Queue<ArrayBuffer, void>();
568+
new linefeeder.LineFeeder(stdout).setSyncHandler((line: string) => {
569+
F(line);
570+
});
571+
567572
stream.on('data', (data: Buffer) => {
568-
F(data.toString());
573+
stdout.handle(arraybuffers.bufferToArrayBuffer(data));
569574
}).stderr.on('data', (data: Buffer) => {
570575
R({
571-
message: 'command output to STDERR: ' + data.toString()
576+
message: 'output received on STDERR: ' + data.toString()
572577
});
573578
}).on('end', () => {
574579
log.debug('%1: exec stream end', this.name_);

0 commit comments

Comments
 (0)