App-I18N
view release on metacpan or search on metacpan
share/static/Stream.js view on Meta::CPAN
var packet = this.req.responseText.substring(this.lastLength, length);
this.processPacket(packet);
this.lastLength = length;
},
processPacket: function(packet) {
if(packet.length < 1) return;
//I don't know if we can count on this, but it's fast as hell
var startFlag = packet.indexOf(this.boundary);
var endFlag = -1;
//Is there a startFlag?
if(startFlag > -1) {
if(typeof this.currentStream != 'undefined') {
//If there's an open stream, that's an endFlag, not a startFlag
endFlag = startFlag;
startFlag = -1;
} else {
//No open stream? Ok, valid startFlag. Let's try find an endFlag then.
endFlag = packet.indexOf(this.boundary, startFlag + this.boundary.length);
}
}
//No stream is open
if(typeof this.currentStream == 'undefined') {
//Open a stream
this.currentStream = '';
//Is there a start flag?
if(startFlag > -1) {
//Yes
//Is there an end flag?
if(endFlag > -1) {
//Yes
//Use the end flag to grab the entire payload in one swoop
var payload = packet.substring(startFlag, endFlag);
this.currentStream += payload;
//Remove the payload from this chunk
packet = packet.replace(payload, '');
this.closeCurrentStream();
//Start over on the remainder of this packet
this.processPacket(packet);
} else {
//No
//Grab from the start of the start flag to the end of the chunk
this.currentStream += packet.substr(startFlag);
//Leave this.currentStream set and wait for another packet
}
} else {
//WTF? No open stream and no start flag means someone fucked up the output
//...OR maybe they're sending garbage in front of their first payload. Weird.
//I guess just ignore it for now?
}
//Else we have an open stream
} else {
//Is there an end flag?
if(endFlag > -1) {
//Yes
//Use the end flag to grab the rest of the payload
var chunk = packet.substring(0, endFlag);
this.currentStream += chunk;
//Remove the rest of the payload from this chunk
packet = packet.replace(chunk, '');
this.closeCurrentStream();
//Start over on the remainder of this packet
this.processPacket(packet);
} else {
//No
//Put this whole packet into this.currentStream
this.currentStream += packet;
//Wait for another packet...
}
}
},
closeCurrentStream: function() {
//Write stream. Not sure if we need this
//this.streams.push(this.currentStream);
//Get mimetype
//First, ditch the boundary
this.currentStream = this.currentStream.replace(this.boundary + "\n", '');
/* The mimetype is the first line after the boundary.
Note that RFC 2046 says that there's either a mimetype here or a blank line to default to text/plain,
so if the payload starts on the line after the boundary, we'll intentionally ditch that line
because it doesn't conform to the spec. QQ more noob, L2play, etc. */
var mimeAndPayload = this.currentStream.split("\n");
var mime = mimeAndPayload.shift().split('Content-Type:', 2)[1].split(";", 1)[0].replace(' ', '');
//Better to have this null than undefined
mime = mime ? mime : null;
//Get payload
var payload = mimeAndPayload.join("\n");
//Try to fire the listeners for this mimetype
var _this = this;
if(typeof this.listeners[mime] != 'undefined') {
$.each(this.listeners[mime], function() {
this.apply(_this, [payload]);
});
}
//Set this.currentStream = null
delete this.currentStream;
},
( run in 1.207 second using v1.01-cache-2.11-cpan-39bf76dae61 )