remove unused module imports and improved module structure
diff --git a/cli.js b/cli.js
index 065951f..5447dd5 100755
--- a/cli.js
+++ b/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');
diff --git a/console.js b/console.js
index 0fda066..18e7241 100644
--- a/console.js
+++ b/console.js
@@ -41,7 +41,7 @@
};
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];
diff --git a/couchjs.js b/couchjs.js
index 84c21e0..ccc1aa9 100644
--- a/couchjs.js
+++ b/couchjs.js
@@ -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 @@
'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 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 @@
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 toSource() {
- if (typeof this == 'function') {
+ if (typeof this === 'function') {
return '' + this;
}
@@ -120,6 +117,11 @@
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,
diff --git a/inspector.js b/inspector.js
index 343770f..48182e5 100755
--- a/inspector.js
+++ b/inspector.js
@@ -12,47 +12,12 @@
// 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 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 = {
- 'cwd': __dirname,
- 'stdio': 'pipe',
- 'detached': false
- };
-
- log('Start inspector: %s %j %j', cmd, args, opts);
-
- var inspector = child_process.spawn(cmd, args, opts);
-
- watch_inspector(inspector);
-
- log('Enable remote debug pid=%d port=%d', process.pid, debugPort);
-
- process.debugPort = debugPort;
- process.kill(process.pid, 'SIGUSR1');
-}
-
-function watch_inspector(child) {
+function watchInspector(child) {
child.stderr.on('data', function(body) {
log('Inspector STDERR: %s', body);
@@ -74,6 +39,33 @@
}
+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 = {
+ 'cwd': __dirname,
+ 'stdio': 'pipe',
+ 'detached': false
+ };
+
+ log('Start inspector: %s %j %j', cmd, args, opts);
+
+ var inspector = cp.spawn(cmd, args, opts);
+
+ watchInspector(inspector);
+
+ log('Enable remote debug pid=%d port=%d', process.pid, debugPort);
+
+ process.debugPort = debugPort;
+ process.kill(process.pid, 'SIGUSR1');
+}
+
function main() {
var debugPort = +process.argv[2];
var webPort = +process.argv[3];
@@ -101,3 +93,9 @@
console.log('Error:\n%s', er.stack);
});
}
+
+module.exports = start;
+
+if (require.main === module) {
+ main();
+}
diff --git a/stream.js b/stream.js
index d0c192c..ebffc30 100644
--- a/stream.js
+++ b/stream.js
@@ -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 @@
this.setEncoding('utf8');
}
+util.inherits(LineStream2, stream.Transform);
+
LineStream2.prototype._transform = function(message, encoding, done) {
var self = this;
@@ -52,25 +67,9 @@
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.end = function(data, encoding) {
+LineStream.prototype.end = function(data) {
var self = this;
self.is_ending = true;
@@ -109,3 +108,8 @@
// 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;
+
diff --git a/test/experiment.js b/test/experiment.js
index fedf6d0..9197ec6 100644
--- a/test/experiment.js
+++ b/test/experiment.js
@@ -14,7 +14,6 @@
var util = require('util');
var STATE = 'wait';
-var v = 'vm';
function main() {
process.debugPort = 5859;