Skip to content
This repository has been archived by the owner on Apr 6, 2021. It is now read-only.

PTY support #4

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 26 additions & 5 deletions consumer.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
( // Module boilerplate to support browser globals, node.js and AMD.
(typeof module !== "undefined" && function (m) { module.exports = m(require('stream'), require('events'), require('smith')); }) ||
(typeof define === "function" && function (m) { define("vfs-socket/consumer", ["./stream-amd", "./events-amd", "smith"], m); }) ||
(typeof define === "function" && function (m) { define(["./stream-amd", "./events-amd", "smith"], m); }) ||
(function (m) { window.consumer = m(window.stream, window.events, window.smith); })
)(function (stream, events, smith) {
"use strict";
Expand Down Expand Up @@ -74,6 +74,7 @@ function Consumer() {
watch: route("watch"),
connect: route("connect"),
spawn: route("spawn"),
pty: route("pty"),
execFile: route("execFile"),
extend: route("extend"),
unextend: route("unextend"),
Expand Down Expand Up @@ -124,6 +125,10 @@ function Consumer() {
proxyApi.emit("error", err);
});
});

this.on("error", function(err){
this.emit("error", err);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Isn't this redundant ?
That's listening and re-emitting the error again

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Doesn't this change meaning here? Maybe it's called with an explicit context.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

no events-amd applies handlers to the same this https://github.com/c9/vfs-socket/blob/master/events-amd.js#L123.
and this also crashes new versions of chrome, where stack limit is 20000

})

var nextStreamID = 1;
function storeStream(stream) {
Expand All @@ -137,9 +142,9 @@ function Consumer() {
stream.pause && stream.pause();
}
});
stream.on("end", function () {
stream.on("end", function (chunk) {
delete streams[id];
remote.onEnd(id);
remote.onEnd(id, chunk);
nextStreamID = id;
});
}
Expand Down Expand Up @@ -194,6 +199,19 @@ function Consumer() {
};
return process;
}
function makePtyProxy(token){
var pty = makeStreamProxy(token);
var pid = token.pid;
pty.pid = pid;
proxyProcesses[pid] = pty;
pty.kill = function (signal) {
remote.kill(pid, signal);
};
pty.resize = function (cols, rows) {
remote.resize(pid, cols, rows);
};
return pty;
}

function makeWatcherProxy(token) {
var watcher = new EventEmitter();
Expand Down Expand Up @@ -249,13 +267,13 @@ function Consumer() {
if (!stream) return;
stream.emit("data", chunk);
}
function onEnd(id) {
function onEnd(id, chunk) {
var stream = proxyStreams[id];
if (!stream) return;
// TODO: not delete proxy if close is going to be called later.
// but somehow do delete proxy if close won't be called later.
delete proxyStreams[id];
stream.emit("end");
stream.emit("end", chunk);
}
function onClose(id) {
var stream = proxyStreams[id];
Expand Down Expand Up @@ -361,6 +379,9 @@ function Consumer() {
if (meta.process) {
meta.process = makeProcessProxy(meta.process);
}
if (meta.pty) {
meta.pty = makePtyProxy(meta.pty);
}
if (meta.watcher) {
meta.watcher = makeWatcherProxy(meta.watcher);
}
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
],
"name": "vfs-socket",
"description": "A vfs helper library that communicates through a serialized socket",
"version": "0.3.12",
"version": "0.3.13",
"repository": {
"type": "git",
"url": "git://github.com/c9/vfs-socket.git"
Expand Down
43 changes: 35 additions & 8 deletions worker.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@ function Worker(vfs) {
// Endpoints for processes at meta.process
kill: kill,

// Endpoints for processes at meta.pty
resize: resize,

// Endpoint for watchers at meta.watcher
close: closeWatcher,

Expand Down Expand Up @@ -59,6 +62,7 @@ function Worker(vfs) {
watch: route("watch"),
connect: route("connect"),
spawn: route("spawn"),
pty: route("pty"),
execFile: route("execFile"),
extend: route("extend"),
unextend: route("unextend"),
Expand Down Expand Up @@ -162,9 +166,9 @@ function Worker(vfs) {
stream.pause && stream.pause();
}
});
stream.on("end", function () {
stream.on("end", function (chunk) {
delete streams[id];
remote.onEnd(id);
remote.onEnd(id, chunk);
nextStreamID = id;
});
}
Expand All @@ -179,7 +183,7 @@ function Worker(vfs) {
return token;
}

function storeProcess(process) {
function storeProcess(process, onlyPid) {
var pid = process.pid;
processes[pid] = process;
process.on("exit", function (code, signal) {
Expand All @@ -188,22 +192,35 @@ function Worker(vfs) {
});
process.on("close", function () {
delete processes[pid];
delete streams[process.stdout.id];
delete streams[process.stderr.id];
delete streams[process.stdin.id];
if (!onlyPid) {
delete streams[process.stdout.id];
delete streams[process.stderr.id];
delete streams[process.stdin.id];
}
remote.onProcessClose(pid);
});

process.kill = function(code) {
killtree(pid, code);
};

if (onlyPid)
return pid;

var token = {pid: pid};
token.stdin = storeStream(process.stdin);
token.stdout = storeStream(process.stdout);
token.stderr = storeStream(process.stderr);
return token;
}

function storePty(pty) {
var pid = storeProcess(pty, true);
var token = storeStream(pty);
token.pid = pid;

return token;
}

function killtree(pid, code) {
childrenOfPid(pid, function(err, pidlist){
Expand Down Expand Up @@ -293,6 +310,15 @@ function Worker(vfs) {
process.kill(code);
}

function resize(pid, cols, rows) {
var process = processes[pid];
if (!process) return;

// Resize can throw
try { process.resize(cols, rows); }
catch(e) {};
}

function closeWatcher(id) {
var watcher = watchers[id];
if (!watcher) return;
Expand Down Expand Up @@ -324,13 +350,13 @@ function Worker(vfs) {
if (!stream) return;
stream.emit("data", chunk);
}
function onEnd(id) {
function onEnd(id, chunk) {
var stream = proxyStreams[id];
if (!stream) return;
// TODO: not delete proxy if close is going to be called later.
// but somehow do delete proxy if close won't be called later.
delete proxyStreams[id];
stream.emit("end");
stream.emit("end", chunk);
}
function onClose(id) {
var stream = proxyStreams[id];
Expand Down Expand Up @@ -361,6 +387,7 @@ function Worker(vfs) {
switch (key) {
case "stream": token.stream = storeStream(meta.stream); break;
case "process": token.process = storeProcess(meta.process); break;
case "pty": token.pty = storePty(meta.pty); break;
case "watcher": token.watcher = storeWatcher(meta.watcher); break;
case "api": token.api = storeApi(meta.api); break;
default: token[key] = meta[key]; break;
Expand Down