Skip to content
This repository has been archived by the owner. It is now read-only.
Permalink
Browse files
remove unused module imports and improved module structure
  • Loading branch information
svnlto authored and janl committed Oct 3, 2013
1 parent a5efd5f commit d6229fd9a40a3c6ba0331083acf6ee6b3aeaccec
Showing 6 changed files with 69 additions and 68 deletions.
2 cli.js
@@ -13,10 +13,8 @@
// the License.

var fs = require('fs');
var util = require('util');
var Fiber = require('fibers');
var optimist = require('optimist');
var child_process = require('child_process');

var couchjs = require('./couchjs');
var package_json = require('./package.json');
@@ -41,7 +41,7 @@ if (stat) {
};

var on_err = function (er) {
module.exports.error('Uncaught error:\n%s', er.stack || er.message || JSON.stringify(er))
module.exports.error('Uncaught error:\n%s', er.stack || er.message || JSON.stringify(er));

if (er.stack) {
er = ['fatal', 'unknown_error', er.stack];
@@ -14,6 +14,7 @@

var vm = require('vm');
var Fiber = require('fibers');
var util = require('util');

var XML = require('./xml');
var log = require('./console').log;
@@ -23,11 +24,6 @@ var INPUT = {
'waiting':null
};

Error.prototype.toSource = Error.prototype.toSource || toSource;
Error.prototype.toString = Error.prototype.toString || toSource;
Function.prototype.toSource = Function.prototype.toSource || toSource;
Function.prototype.toString = Function.prototype.toString || toSource;


function print(line) {
log('STDOUT %s: %s', process.pid, line);
@@ -71,9 +67,10 @@ function readline () {

function evalcx (source, sandbox) {
sandbox = sandbox || {};
var func;
//log('evalcx in %j: %j', Object.keys(sandbox), source)

if (source == '') {
if (source === '') {
return sandbox;
}

@@ -87,7 +84,7 @@ function evalcx (source, sandbox) {
var id = Math.floor(Math.random() * 1000*1000);
var filename = '_couchdb:' + id + '.js';
var script = vm.createScript(source, filename);
var func = script.runInNewContext(sandbox);
func = script.runInNewContext(sandbox);
} catch (er) {
log('Error making code: %s', er.stack);
return sandbox;
@@ -109,7 +106,7 @@ function gc() { }


function toSource() {
if (typeof this == 'function') {
if (typeof this === 'function') {
return '' + this;
}

@@ -120,6 +117,11 @@ function toSource() {
return util.inspect(this);
}

Error.prototype.toSource = Error.prototype.toSource || toSource;
Error.prototype.toString = Error.prototype.toString || toSource;
Function.prototype.toSource = Function.prototype.toSource || toSource;
Function.prototype.toString = Function.prototype.toString || toSource;

module.exports = {
'print': print,
'readline': readline,
@@ -12,26 +12,40 @@
// License for the specific language governing permissions and limitations under
// the License.

module.exports = start;

if (require.main === module) {
main();
}


var fs = require('fs');
var util = require('util');
var child_process = require('child_process');
var cp = require('child_process');
var log = require('./console').log;

function watchInspector(child) {

child.stderr.on('data', function(body) {
log('Inspector STDERR: %s', body);
});

child.stdout.on('data', function(body) {
log('Inspector STDOUT: %s', body);
});

child.on('exit', function(code, signal) {
log('Inspector exited %d signal=%j', code, signal);
process.exit(code);
});

process.on('exit', function() {
log('Kill inspector upon exit: %d', child.pid);
process.kill(child.pid, 'SIGTERM');
});

}

function start (debugPort) {

if (!debugPort || typeof debugPort !== 'number') {
throw new Error('Need a listen debugPort');
}

var webPort = debugPort + 1;

var cmd = __filename;
var args = [debugPort, webPort];
var opts = {
@@ -42,38 +56,16 @@ function start (debugPort) {

log('Start inspector: %s %j %j', cmd, args, opts);

var inspector = child_process.spawn(cmd, args, opts);
var inspector = cp.spawn(cmd, args, opts);

watch_inspector(inspector);
watchInspector(inspector);

log('Enable remote debug pid=%d port=%d', process.pid, debugPort);

process.debugPort = debugPort;
process.kill(process.pid, 'SIGUSR1');
}

function watch_inspector(child) {

child.stderr.on('data', function(body) {
log('Inspector STDERR: %s', body);
});

child.stdout.on('data', function(body) {
log('Inspector STDOUT: %s', body);
});

child.on('exit', function(code, signal) {
log('Inspector exited %d signal=%j', code, signal);
process.exit(code);
});

process.on('exit', function() {
log('Kill inspector upon exit: %d', child.pid);
process.kill(child.pid, 'SIGTERM');
});

}

function main() {
var debugPort = +process.argv[2];
var webPort = +process.argv[3];
@@ -101,3 +93,9 @@ function main() {
console.log('Error:\n%s', er.stack);
});
}

module.exports = start;

if (require.main === module) {
main();
}
@@ -12,16 +12,29 @@

// Text line stream

module.exports = LineStream;
module.exports.v2 = LineStream2;

var stream = require('stream');
var util = require('util');


util.inherits(LineStream2, stream.Transform);
function LineStream() {

function LineStream2 () {
var self = this;
stream.call(self);

self.readable = true;
self.writable = true;

self.buffer = '';
self.downstream = null;

self.on('pipe', function(upstream) {
upstream.on('end', function(data, encoding) {
self.emit('end', data, encoding);
});
});
}

function LineStream2() {

if (!(this instanceof LineStream2)) {
return new LineStream2();
@@ -31,6 +44,8 @@ function LineStream2 () {
this.setEncoding('utf8');
}

util.inherits(LineStream2, stream.Transform);

LineStream2.prototype._transform = function(message, encoding, done) {
var self = this;

@@ -52,25 +67,9 @@ LineStream2.prototype._transform = function(message, encoding, done) {

util.inherits(LineStream, stream);

function LineStream () {
var self = this;
stream.call(self);

self.readable = true;
self.writable = true;

self.buffer = '';
self.downstream = null;

self.on('pipe', function(upstream) {
upstream.on('end', function(data, encoding) {
self.emit('end', data, encoding);
});
});
}


LineStream.prototype.write = function(data, encoding) {
LineStream.prototype.write = function(data) {
var self = this;

data = data || '';
@@ -88,7 +87,7 @@ LineStream.prototype.write = function(data, encoding) {
};


LineStream.prototype.end = function(data, encoding) {
LineStream.prototype.end = function(data) {
var self = this;

self.is_ending = true;
@@ -109,3 +108,8 @@ LineStream.prototype.error = function(er) {
// The write() method sometimes returns this value, so if there was an error, make write() return false.
return false;
};


module.exports = LineStream;
module.exports.v2 = LineStream2;

@@ -14,7 +14,6 @@ var vm = require('vm');
var util = require('util');

var STATE = 'wait';
var v = 'vm';

function main() {
process.debugPort = 5859;

0 comments on commit d6229fd

Please sign in to comment.