From 532100c101387a3314a870264e58e7761c787c67 Mon Sep 17 00:00:00 2001 From: Sven Lito Date: Thu, 3 Oct 2013 16:34:06 +0100 Subject: remove unused module imports and improved module structure --- src/couchjs-node/cli.js | 2 -- src/couchjs-node/console.js | 2 +- src/couchjs-node/couchjs.js | 18 ++++++----- src/couchjs-node/inspector.js | 64 ++++++++++++++++++------------------- src/couchjs-node/stream.js | 50 ++++++++++++++++------------- src/couchjs-node/test/experiment.js | 1 - 6 files changed, 69 insertions(+), 68 deletions(-) diff --git a/src/couchjs-node/cli.js b/src/couchjs-node/cli.js index 065951fdf..5447dd5e9 100755 --- a/src/couchjs-node/cli.js +++ b/src/couchjs-node/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/src/couchjs-node/console.js b/src/couchjs-node/console.js index 0fda066b2..18e724183 100644 --- a/src/couchjs-node/console.js +++ b/src/couchjs-node/console.js @@ -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]; diff --git a/src/couchjs-node/couchjs.js b/src/couchjs-node/couchjs.js index 84c21e0ed..ccc1aa9ed 100644 --- a/src/couchjs-node/couchjs.js +++ b/src/couchjs-node/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 @@ 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, diff --git a/src/couchjs-node/inspector.js b/src/couchjs-node/inspector.js index 343770fcc..48182e5e4 100755 --- a/src/couchjs-node/inspector.js +++ b/src/couchjs-node/inspector.js @@ -12,18 +12,33 @@ // 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') { @@ -31,7 +46,6 @@ function start (debugPort) { } var webPort = debugPort + 1; - var cmd = __filename; var args = [debugPort, webPort]; var opts = { @@ -42,9 +56,9 @@ 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); @@ -52,28 +66,6 @@ function start (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(); +} diff --git a/src/couchjs-node/stream.js b/src/couchjs-node/stream.js index d0c192c22..ebffc3027 100644 --- a/src/couchjs-node/stream.js +++ b/src/couchjs-node/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 @@ 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; + diff --git a/src/couchjs-node/test/experiment.js b/src/couchjs-node/test/experiment.js index fedf6d087..9197ec611 100644 --- a/src/couchjs-node/test/experiment.js +++ b/src/couchjs-node/test/experiment.js @@ -14,7 +14,6 @@ var vm = require('vm'); var util = require('util'); var STATE = 'wait'; -var v = 'vm'; function main() { process.debugPort = 5859; -- cgit v1.2.1