diff options
Diffstat (limited to 'lib')
-rw-r--r-- | lib/_debugger.js | 6 | ||||
-rw-r--r-- | lib/cluster.js | 8 | ||||
-rw-r--r-- | lib/domain.js | 2 | ||||
-rw-r--r-- | lib/fs.js | 16 | ||||
-rw-r--r-- | lib/module.js | 7 | ||||
-rw-r--r-- | lib/net.js | 5 | ||||
-rw-r--r-- | lib/readline.js | 57 | ||||
-rw-r--r-- | lib/repl.js | 68 | ||||
-rw-r--r-- | lib/timers.js | 10 | ||||
-rw-r--r-- | lib/tls.js | 5 | ||||
-rw-r--r-- | lib/util.js | 4 |
11 files changed, 97 insertions, 91 deletions
diff --git a/lib/_debugger.js b/lib/_debugger.js index bb108347d..c39a0ba58 100644 --- a/lib/_debugger.js +++ b/lib/_debugger.js @@ -40,7 +40,7 @@ exports.start = function(argv, stdin, stdout) { stdout = stdout || process.stdout; var args = ['--debug-brk'].concat(argv), - interface = new Interface(stdin, stdout, args); + interface_ = new Interface(stdin, stdout, args); stdin.resume(); @@ -49,7 +49,7 @@ exports.start = function(argv, stdin, stdout) { 'Please report this bug.'); console.error(e.message); console.error(e.stack); - if (interface.child) interface.child.kill(); + if (interface_.child) interface_.child.kill(); process.exit(1); }); }; @@ -700,7 +700,7 @@ function SourceUnderline(sourceText, position, repl) { // Colourize char if stdout supports colours if (repl.useColors) { - tail = tail.replace(/(.+?)([^\w]|$)/, '\033[32m$1\033[39m$2'); + tail = tail.replace(/(.+?)([^\w]|$)/, '\u001b[32m$1\u001b[39m$2'); } // Return source line with coloured char at `position` diff --git a/lib/cluster.js b/lib/cluster.js index f62e8abd1..8f7df3e12 100644 --- a/lib/cluster.js +++ b/lib/cluster.js @@ -41,11 +41,13 @@ if (process.env.NODE_DEBUG && /cluster/.test(process.env.NODE_DEBUG)) { } // cluster object: -function cluster() { +function Cluster() { EventEmitter.call(this); } -util.inherits(cluster, EventEmitter); -var cluster = module.exports = new cluster(); + +util.inherits(Cluster, EventEmitter); + +var cluster = module.exports = new Cluster(); // Used in the master: var masterStarted = false; diff --git a/lib/domain.js b/lib/domain.js index 7c4a85226..1524627a5 100644 --- a/lib/domain.js +++ b/lib/domain.js @@ -185,7 +185,7 @@ Domain.prototype.bind = function(cb, interceptError) { // slower for less common case: cb(er, foo, bar, baz, ...) args = new Array(len - 1); for (var i = 1; i < len; i++) { - args[i] = arguments[i - 1]; + args[i - 1] = arguments[i]; } break; } @@ -943,10 +943,18 @@ fs.watchFile = function(filename) { return stat; }; -fs.unwatchFile = function(filename) { - var stat; - if (inStatWatchers(filename)) { - stat = statWatchers[filename]; +fs.unwatchFile = function(filename, listener) { + if (!inStatWatchers(filename)) return; + + var stat = statWatchers[filename]; + + if (typeof listener === 'function') { + stat.removeListener('change', listener); + } else { + stat.removeAllListeners('change'); + } + + if (stat.listeners('change').length === 0) { stat.stop(); statWatchers[filename] = undefined; } diff --git a/lib/module.js b/lib/module.js index bbab6b9e6..613039474 100644 --- a/lib/module.js +++ b/lib/module.js @@ -471,7 +471,12 @@ Module._extensions['.js'] = function(module, filename) { // Native extension for .json Module._extensions['.json'] = function(module, filename) { var content = NativeModule.require('fs').readFileSync(filename, 'utf8'); - module.exports = JSON.parse(stripBOM(content)); + try { + module.exports = JSON.parse(stripBOM(content)); + } catch (err) { + err.message = filename + ': ' + err.message; + throw err; + } }; diff --git a/lib/net.js b/lib/net.js index 9f03259d9..d6a01aa83 100644 --- a/lib/net.js +++ b/lib/net.js @@ -612,11 +612,6 @@ function afterWrite(status, handle, req) { function connect(self, address, port, addressType, localAddress) { - if (port) { - self.remotePort = port; - } - self.remoteAddress = address; - // TODO return promise from Socket.prototype.connect which // wraps _connectReq. diff --git a/lib/readline.js b/lib/readline.js index 0161b4e29..c95bd4085 100644 --- a/lib/readline.js +++ b/lib/readline.js @@ -136,7 +136,7 @@ Interface.prototype.setPrompt = function(prompt, length) { } else { var lines = prompt.split(/[\r\n]/); var lastLine = lines[lines.length - 1]; - this._promptLength = Buffer.byteLength(lastLine); + this._promptLength = lastLine.length; } }; @@ -348,43 +348,17 @@ Interface.prototype._tabComplete = function() { return a.length > b.length ? a : b; }).length + 2; // 2 space padding var maxColumns = Math.floor(self.columns / width) || 1; - - function handleGroup(group) { - if (group.length == 0) { - return; - } - var minRows = Math.ceil(group.length / maxColumns); - for (var row = 0; row < minRows; row++) { - for (var col = 0; col < maxColumns; col++) { - var idx = row * maxColumns + col; - if (idx >= group.length) { - break; - } - var item = group[idx]; - self.output.write(item); - if (col < maxColumns - 1) { - for (var s = 0, itemLen = item.length; s < width - itemLen; - s++) { - self.output.write(' '); - } - } - } - self.output.write('\r\n'); - } - self.output.write('\r\n'); - } - var group = [], c; for (var i = 0, compLen = completions.length; i < compLen; i++) { c = completions[i]; if (c === '') { - handleGroup(group); + handleGroup(self, group, width, maxColumns); group = []; } else { group.push(c); } } - handleGroup(group); + handleGroup(self, group, width, maxColumns); // If there is a common prefix to all matches, then apply that // portion. @@ -400,6 +374,31 @@ Interface.prototype._tabComplete = function() { }); }; +// this = Interface instance +function handleGroup(self, group, width, maxColumns) { + if (group.length == 0) { + return; + } + var minRows = Math.ceil(group.length / maxColumns); + for (var row = 0; row < minRows; row++) { + for (var col = 0; col < maxColumns; col++) { + var idx = row * maxColumns + col; + if (idx >= group.length) { + break; + } + var item = group[idx]; + self.output.write(item); + if (col < maxColumns - 1) { + for (var s = 0, itemLen = item.length; s < width - itemLen; + s++) { + self.output.write(' '); + } + } + } + self.output.write('\r\n'); + } + self.output.write('\r\n'); +} function commonPrefix(strings) { if (!strings || strings.length == 0) { diff --git a/lib/repl.js b/lib/repl.js index c3719ef54..dd1d81028 100644 --- a/lib/repl.js +++ b/lib/repl.js @@ -75,9 +75,9 @@ exports._builtinLibs = ['assert', 'buffer', 'child_process', 'cluster', 'string_decoder', 'tls', 'tty', 'url', 'util', 'vm', 'zlib']; -function REPLServer(prompt, stream, eval, useGlobal, ignoreUndefined) { +function REPLServer(prompt, stream, eval_, useGlobal, ignoreUndefined) { if (!(this instanceof REPLServer)) { - return new REPLServer(prompt, stream, eval, useGlobal, ignoreUndefined); + return new REPLServer(prompt, stream, eval_, useGlobal, ignoreUndefined); } EventEmitter.call(this); @@ -89,7 +89,7 @@ function REPLServer(prompt, stream, eval, useGlobal, ignoreUndefined) { stream = options.stream || options.socket; input = options.input; output = options.output; - eval = options.eval; + eval_ = options.eval; useGlobal = options.useGlobal; ignoreUndefined = options.ignoreUndefined; prompt = options.prompt; @@ -104,7 +104,7 @@ function REPLServer(prompt, stream, eval, useGlobal, ignoreUndefined) { self.useGlobal = !!useGlobal; self.ignoreUndefined = !!ignoreUndefined; - self.eval = eval || function(code, context, file, cb) { + self.eval = eval_ || function(code, context, file, cb) { var err, result; try { if (self.useGlobal) { @@ -281,7 +281,7 @@ function REPLServer(prompt, stream, eval, useGlobal, ignoreUndefined) { // If error was SyntaxError and not JSON.parse error if (isSyntaxError(e)) { - if (cmd.trim().match(/^npm /) && !self.bufferedCommand) { + if (!self.bufferedCommand && cmd.trim().match(/^npm /)) { self.outputStream.write('npm should be run outside of the ' + 'node repl, in your normal shell.\n' + '(Press Control-D to exit.)\n'); @@ -327,8 +327,8 @@ exports.REPLServer = REPLServer; // prompt is a string to print on each line for the prompt, // source is a stream to use for I/O, defaulting to stdin/stdout. -exports.start = function(prompt, source, eval, useGlobal, ignoreUndefined) { - var repl = new REPLServer(prompt, source, eval, useGlobal, ignoreUndefined); +exports.start = function(prompt, source, eval_, useGlobal, ignoreUndefined) { + var repl = new REPLServer(prompt, source, eval_, useGlobal, ignoreUndefined); if (!exports.repl) exports.repl = repl; return repl; }; @@ -536,9 +536,6 @@ REPLServer.prototype.complete = function(line, callback) { expr = bits.join('.'); } - // console.log("expression completion: completeOn='" + completeOn + - // "' expr='" + expr + "'"); - // Resolve expr and get its completions. var obj, memberGroups = []; if (!expr) { @@ -552,12 +549,12 @@ REPLServer.prototype.complete = function(line, callback) { completionGroups.push(Object.getOwnPropertyNames(contextProto)); } completionGroups.push(Object.getOwnPropertyNames(this.context)); - addStandardGlobals(); + addStandardGlobals(completionGroups, filter); completionGroupsLoaded(); } else { this.eval('.scope', this.context, 'repl', function(err, globals) { if (err || !globals) { - addStandardGlobals(); + addStandardGlobals(completionGroups, filter); } else if (Array.isArray(globals[0])) { // Add grouped globals globals.forEach(function(group) { @@ -565,34 +562,11 @@ REPLServer.prototype.complete = function(line, callback) { }); } else { completionGroups.push(globals); - addStandardGlobals(); + addStandardGlobals(completionGroups, filter); } completionGroupsLoaded(); }); } - - function addStandardGlobals() { - // Global object properties - // (http://www.ecma-international.org/publications/standards/Ecma-262.htm) - completionGroups.push(['NaN', 'Infinity', 'undefined', - 'eval', 'parseInt', 'parseFloat', 'isNaN', 'isFinite', 'decodeURI', - 'decodeURIComponent', 'encodeURI', 'encodeURIComponent', - 'Object', 'Function', 'Array', 'String', 'Boolean', 'Number', - 'Date', 'RegExp', 'Error', 'EvalError', 'RangeError', - 'ReferenceError', 'SyntaxError', 'TypeError', 'URIError', - 'Math', 'JSON']); - // Common keywords. Exclude for completion on the empty string, b/c - // they just get in the way. - if (filter) { - completionGroups.push(['break', 'case', 'catch', 'const', - 'continue', 'debugger', 'default', 'delete', 'do', 'else', - 'export', 'false', 'finally', 'for', 'function', 'if', - 'import', 'in', 'instanceof', 'let', 'new', 'null', 'return', - 'switch', 'this', 'throw', 'true', 'try', 'typeof', 'undefined', - 'var', 'void', 'while', 'with', 'yield']); - } - } - } else { this.eval(expr, this.context, 'repl', function(e, obj) { // if (e) console.log(e); @@ -790,6 +764,27 @@ REPLServer.prototype.memory = function memory(cmd) { } }; +function addStandardGlobals(completionGroups, filter) { + // Global object properties + // (http://www.ecma-international.org/publications/standards/Ecma-262.htm) + completionGroups.push(['NaN', 'Infinity', 'undefined', + 'eval', 'parseInt', 'parseFloat', 'isNaN', 'isFinite', 'decodeURI', + 'decodeURIComponent', 'encodeURI', 'encodeURIComponent', + 'Object', 'Function', 'Array', 'String', 'Boolean', 'Number', + 'Date', 'RegExp', 'Error', 'EvalError', 'RangeError', + 'ReferenceError', 'SyntaxError', 'TypeError', 'URIError', + 'Math', 'JSON']); + // Common keywords. Exclude for completion on the empty string, b/c + // they just get in the way. + if (filter) { + completionGroups.push(['break', 'case', 'catch', 'const', + 'continue', 'debugger', 'default', 'delete', 'do', 'else', + 'export', 'false', 'finally', 'for', 'function', 'if', + 'import', 'in', 'instanceof', 'let', 'new', 'null', 'return', + 'switch', 'this', 'throw', 'true', 'try', 'typeof', 'undefined', + 'var', 'void', 'while', 'with', 'yield']); + } +} function defineDefaultCommands(repl) { // TODO remove me after 0.3.x @@ -883,6 +878,7 @@ function trimWhitespace(cmd) { if (matches && matches.length === 2) { return matches[1]; } + return ''; } diff --git a/lib/timers.js b/lib/timers.js index f906a4283..d9692479a 100644 --- a/lib/timers.js +++ b/lib/timers.js @@ -170,8 +170,9 @@ exports.active = function(item) { exports.setTimeout = function(callback, after) { var timer; - after = ~~after; - if (after < 1 || after > TIMEOUT_MAX) { + after *= 1; // coalesce to number or NaN + + if (!(after >= 1 && after <= TIMEOUT_MAX)) { after = 1; // schedule on next tick, follows browser behaviour } @@ -222,8 +223,9 @@ exports.setInterval = function(callback, repeat) { if (process.domain) timer.domain = process.domain; - repeat = ~~repeat; - if (repeat < 1 || repeat > TIMEOUT_MAX) { + repeat *= 1; // coalesce to number or NaN + + if (!(repeat >= 1 && repeat <= TIMEOUT_MAX)) { repeat = 1; // schedule on next tick, follows browser behaviour } diff --git a/lib/tls.js b/lib/tls.js index 68e860f0d..c68a930d1 100644 --- a/lib/tls.js +++ b/lib/tls.js @@ -605,11 +605,10 @@ function onhandshakestart() { var self = this, ssl = this.ssl; if (ssl.timer === null) { - function timeout() { + ssl.timer = setTimeout(function timeout() { ssl.handshakes = 0; ssl.timer = null; - } - ssl.timer = setTimeout(timeout, exports.CLIENT_RENEG_WINDOW * 1000); + }, exports.CLIENT_RENEG_WINDOW * 1000); } else if (++ssl.handshakes > exports.CLIENT_RENEG_LIMIT) { // Defer the error event to the next tick. We're being called from OpenSSL's diff --git a/lib/util.js b/lib/util.js index 65888920c..53775e74d 100644 --- a/lib/util.js +++ b/lib/util.js @@ -162,8 +162,8 @@ function stylizeWithColor(str, styleType) { var style = styles[styleType]; if (style) { - return '\033[' + colors[style][0] + 'm' + str + - '\033[' + colors[style][1] + 'm'; + return '\u001b[' + colors[style][0] + 'm' + str + + '\u001b[' + colors[style][1] + 'm'; } else { return str; } |