[Pkg-javascript-commits] [ltx] 120/469: connection: do not inherit from net.Stream anymore
Jonas Smedegaard
dr at jones.dk
Wed Aug 31 13:01:11 UTC 2016
This is an automated email from the git hooks/post-receive script.
js pushed a commit to branch master
in repository ltx.
commit 01b0bf293e6bfc7ea19f5a8e9369d223c267a2ef
Author: Astro <astro at spaceboyz.net>
Date: Thu Sep 9 03:45:04 2010 +0200
connection: do not inherit from net.Stream anymore
This is an attempt to remove confusion when building proper SRV support.
---
lib/xmpp/connection.js | 82 ++++++++++++++++++++++++++------------------------
1 file changed, 43 insertions(+), 39 deletions(-)
diff --git a/lib/xmpp/connection.js b/lib/xmpp/connection.js
index 910a070..f47d123 100644
--- a/lib/xmpp/connection.js
+++ b/lib/xmpp/connection.js
@@ -1,4 +1,5 @@
var net = require('net');
+var EventEmitter = require('events').EventEmitter;
var sys = require('sys');
var xml = require('./xml');
var StreamParser = require('./stream_parser');
@@ -10,13 +11,30 @@ var NS_STREAM = exports.NS_STREAM = 'http://etherx.jabber.org/streams';
'stanza' to Client & Component. Therefore we won't confuse the
user with stanzas before authentication has finished.
*/
-function Connection() {
- net.Stream.call(this);
+function Connection(socket) {
+ var self = this;
+ EventEmitter.call(this);
+
+ this.charset = 'UTF-8';
- initConnection(this);
+ this.socket = socket || new net.Stream();
+ this.socket.addListener('data', function(data) {
+ self.onData(data);
+ });
+ this.socket.addListener('end', function() {
+ self.onEnd();
+ });
+ var proxyEvent = function(event) {
+ self.socket.addListener(event, function() {
+ self.emit.apply(self, Array.prototype.slice.call(arguments, 0, 0, event));
+ });
+ };
+ proxyEvent('data'); // let them sniff
+ proxyEvent('drain');
+ proxyEvent('close');
}
-sys.inherits(Connection, net.Stream);
+sys.inherits(Connection, EventEmitter);
exports.Connection = Connection;
// Defaults
@@ -24,50 +42,28 @@ Connection.prototype.charset = 'UTF-8';
Connection.prototype.allowTLS = true;
-/** Constructor code, usable for existing streams
- */
-function makeConnection(conn) {
- for(var k in Connection.prototype)
- if (Connection.prototype.hasOwnProperty(k))
- conn[k] = Connection.prototype[k];
-
- initConnection(conn);
-}
-exports.makeConnection = makeConnection;
-
-/** Actual constructor code
- */
-function initConnection(conn) {
- conn.charset = 'UTF-8';
-
- conn.addListener('data', conn.onData);
- conn.addListener('end', conn.onEnd);
- conn.addListener('error', conn.onEnd);
-}
-
/** Climbs the stanza up if a child was passed,
but you can send strings and buffers too.
*/
Connection.prototype.send = function(stanza) {
- if (!this.writable) {
- this.end();
+ if (!this.socket.writable) {
+ this.socket.end();
return;
}
if (stanza.root) {
var el = this.rmStreamNs(stanza.root());
- var self = this;
- el.write(function(s) { self.write(s); });
+console.log({send:el.toString()});
+ var socket = this.socket;
+ el.write(function(s) { socket.write(s); });
return el;
}
else {
- this.write(stanza);
+ this.socket.write(stanza);
return stanza;
}
};
-// TODO: stream attrs, streamStart
-
Connection.prototype.startParser = function() {
var self = this;
this.parser = new StreamParser.StreamParser(this.charset);
@@ -110,11 +106,21 @@ Connection.prototype.onData = function(data) {
this.parser.write(data);
};
+Connection.prototype.setSecure = function() {
+ var self = this;
+ this.stopParser();
+ this.socket.setSecure(this.credentials);
+ this.addListener('secure', function() {
+ self.startParser();
+ });
+};
+
/**
* This is not an event listener, but takes care of the TLS handshake
* before 'rawStanza' events are emitted to the derived classes.
*/
Connection.prototype.onStanza = function(stanza) {
+console.log({onStanza:stanza.toString(),allowTLS:this.allowTLS,is:stanza.is('proceed', NS_XMPP_TLS)});
if (stanza.is('error', NS_STREAM)) {
/* TODO: extract error text */
this.emit('error', stanza);
@@ -125,10 +131,8 @@ Connection.prototype.onStanza = function(stanza) {
this.send(new xml.Element('starttls', { xmlns: NS_XMPP_TLS }));
} else if (this.allowTLS &&
stanza.is('proceed', NS_XMPP_TLS)) {
- this.stopParser();
/* Server is waiting for TLS handshake */
- this.setSecure(this.credentials);
- this.addListener('secure', this.startStream);
+ this.setSecure();
} else {
this.emit('rawStanza', stanza);
}
@@ -174,9 +178,9 @@ Connection.prototype.onEnd = function() {
* XMPP-style end connection for user
*/
Connection.prototype.end = function() {
- if (this.writable)
- this.send('</stream:stream>');
- net.Stream.prototype.end.call(this);
+ if (this.socket.writable)
+ this.socket.write('</stream:stream>');
+ this.socket.end();
// stopParser will called on 'end'/'error' event
};
@@ -190,7 +194,7 @@ Connection.prototype.end = function() {
Connection.prototype.error = function(condition, message) {
this.emit('error', new Error(message));
- if (!this.writable)
+ if (!this.socket.writable)
return;
var e = new xml.Element('stream:error');
--
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-javascript/ltx.git
More information about the Pkg-javascript-commits
mailing list