By default 'error' throws. addListener to prevent this.
exports.EventEmitter = process.EventEmitter;
+
+process.EventEmitter.prototype.emit = function (type) {
+ if (type == 'error') {
+ if (!this._events || !this._events.error ||
+ (this._events.error instanceof Array && !this._events.error.length))
+ {
+ if (arguments[1] instanceof Error) {
+ throw arguments[1];
+ } else {
+ throw new Error("Uncaught, unspecfied 'error' event.");
+ }
+ return false;
+ }
+ }
+
+ if (!this._events) return false;
+ if (!this._events[type]) return false;
+
+ if (typeof this._events[type] == 'function') {
+ var args = Array.prototype.slice.call(arguments, 1);
+
+ this._events[type].apply(this, args);
+ return true;
+
+ } else if (this._events[type] instanceof Array) {
+ var args = Array.prototype.slice.call(arguments, 1);
+
+
+ var listeners = this._events[type].slice(0);
+ for (var i = 0, l = listeners.length; i < l; i++) {
+ listeners[i].apply(this, args);
+ }
+ return true;
+
+ } else {
+ return false;
+ }
+};
+
// process.EventEmitter is defined in src/node_events.cc
// process.EventEmitter.prototype.emit() is also defined there.
process.EventEmitter.prototype.addListener = function (type, listener) {
return this;
};
+
process.EventEmitter.prototype.removeListener = function (type, listener) {
if ('function' !== typeof listener) {
throw new Error('removeListener only takes instances of Function');
parser.socket = socket;
if (self.secure) {
- socket.setSecure(self.credentials);
+ socket.setSecure(self.credentials);
}
+ socket.addListener('error', function (e) {
+ sys.puts('node http server: ' + e);
+ });
+
socket.ondata = function (d, start, end) {
var bytesParsed = parser.execute(d, start, end - start);
if (parser.incoming && parser.incoming.upgrade) {
Stream.prototype._shutdown = function () {
- if (this.writable) {
- this.writable = false;
+ if (!this.writable) {
+ throw new Error('The connection is not writable');
+ } else {
+ if (this.readable) {
+ // readable and writable
+ this.writable = false;
- if (this.secure) {
- this._shutdownSecure();
- }
- try {
- shutdown(this.fd, 'write')
- } catch (e) {
- this.destroy(e);
- return;
+ if (this.secure) {
+ this._shutdownSecure();
+ }
+ try {
+ shutdown(this.fd, 'write')
+ } catch (e) {
+ this.destroy(e);
+ return;
+ }
+ } else {
+ // writable but not readable
+ this.destroy();
}
-
- if (!this.readable) this.destroy();
}
};
var net = module.requireNative('net');
stdout = new net.Stream(process.binding('stdio').stdoutFD);
- stdout.addListener('error', function (err) { throw err; });
-
return stdout;
});
var net = module.requireNative('net');
var fd = process.binding('stdio').openStdin();
stdin = new net.Stream(fd);
- stdin.addListener('error', function (err) { throw err; });
stdin.resume();
stdin.readable = true;
return stdin;
body = "hello world\n";
server = http.createServer(function (req, res) {
- res.writeHead(200, {
- "Content-Length": body.length,
- "Content-Type": "text/plain",
+ res.writeHead(200, {
+ "Content-Length": body.length,
+ "Content-Type": "text/plain"
});
res.write(body);
res.end();
timeouttime = null;
timeout = 1000;
+gotError = false
+
var echo_server = net.createServer(function (socket) {
socket.setTimeout(timeout);
p(timeouttime);
});
+ socket.addListener("error", function (e) {
+ assert.ok(e instanceof Error);
+ gotError = true;
+ })
+
socket.addListener("data", function (d) {
puts(d);
socket.write(d);
// Allow for 800 milliseconds more
assert.ok(diff < timeout + 800);
+
+ assert.ok(gotError);
});