docker-stream-cleanser
Advanced tools
Comparing version 0.0.14 to 0.0.15
90
app.js
@@ -0,1 +1,3 @@ | ||
'use strict'; | ||
module.exports = function(data, encoding, fixCarriageReturns) { | ||
@@ -35,57 +37,51 @@ if (!Buffer.isBuffer(data)) { | ||
module.exports.cleanStreams = function (buildStream, clientStream, encoding, fixCarriageReturns) { | ||
var lastBuffer = null; | ||
buildStream.on('data', function(data) { | ||
if (!Buffer.isBuffer(data)) { | ||
data = new Buffer(data, encoding); | ||
} | ||
var dataBuffer = null; | ||
function parseDataBuffer() { | ||
// Sometimes messages get chopped up by docker, even in the header, so we need to handle this | ||
//We need to make sure that this isn't a continuation of the message before it | ||
if (lastBuffer) { | ||
data = Buffer.concat([lastBuffer, data], lastBuffer.length + data.length); | ||
lastBuffer = null; | ||
if (!dataBuffer || dataBuffer.length <= 8 || dataBuffer[0] > 3) { | ||
// If this is true, we don't have any data to process, so just exit (it'll get done next time) | ||
return; | ||
} | ||
// If the header got chopped up, then we probably don't have a length. But we should at least | ||
// have the first 4 bytes (hopefully). | ||
if (data.length > 0 && data[0] > 0 && data[0] < 4) { | ||
// If true, we at least have a legit docker message' | ||
if (data[1] - data[2] - data[3] === 0) { | ||
// We can at least read the size now. | ||
var header = data.slice(0, 8); | ||
var pointer = 8; | ||
while(header && header.length && header[1] - header[2] - header[3] === 0) { | ||
var size = header.readUInt32BE(4); | ||
if (pointer + size > data.length) { | ||
// check if the size greater than the rest of the message. If it is, slice off the | ||
// whole message (-8 to grab the header as well), and store it in the buffer | ||
lastBuffer = data.slice(pointer - 8); | ||
header = null; | ||
} else { | ||
var payload = data.slice(pointer, pointer += size); | ||
if (payload === null) break; | ||
payload = (fixCarriageReturns) ? | ||
payload.toString().replace(/\r?\n/g, '\r\n') : payload.toString(); | ||
clientStream.write(payload); | ||
// We're going to be popping each message off of the top of dataBuffer | ||
var header = dataBuffer.slice(0, 8); | ||
var size = header.readUInt32BE(4); | ||
// check if the size greater than the rest of the message. If it is, slice off the | ||
// whole message (-8 to grab the header as well), and store it in the buffer | ||
if (8 + size > dataBuffer.length) { | ||
// If its too short, just return | ||
return; | ||
} else { | ||
if (data.length <= pointer) { | ||
header = null; | ||
} else if (data.length > pointer+8) { | ||
header = data.slice(pointer, pointer += 8); | ||
} else { | ||
// Somehow only part of a header is on the end of the message, so save it to the | ||
// buffer. | ||
lastBuffer = data.slice(pointer); | ||
header = null; | ||
} | ||
} | ||
} | ||
} else if (data.length < 8 ) { | ||
// If we don't even have enough message for the full header, save it to the buffer and leave | ||
lastBuffer = data; | ||
// Basically do a splice here | ||
var payload = dataBuffer.slice(8, 8 + size); | ||
dataBuffer = dataBuffer.slice(8 + size); | ||
if (payload === null) return; | ||
payload = (fixCarriageReturns) ? | ||
payload.toString().replace(/\r?\n/g, '\r\n') : payload.toString(); | ||
clientStream.write(payload); | ||
if (dataBuffer.length > 8) { | ||
// Since we've moved the data to a buffer outside of the processing, we can use a | ||
// timeout to schedule the next iteration | ||
setTimeout(parseDataBuffer, 10); | ||
} | ||
} | ||
} | ||
buildStream.on('data', function(data) { | ||
// First, make sure the data is a buffer | ||
if (!Buffer.isBuffer(data)) { | ||
data= new Buffer(data, encoding); | ||
} | ||
if (dataBuffer) { | ||
// Now append it to the dataBuffer | ||
dataBuffer= Buffer.concat([dataBuffer, data], dataBuffer.length + data.length); | ||
} else { | ||
clientStream.write((fixCarriageReturns) ? | ||
data.toString().replace(/\r?\n/g, '\r\n') : data.toString()); | ||
dataBuffer = data; | ||
} | ||
// Now send it off to the parseData function | ||
parseDataBuffer(); | ||
}); | ||
}; | ||
}; |
{ | ||
"name": "docker-stream-cleanser", | ||
"version": "0.0.14", | ||
"version": "0.0.15", | ||
"main": "app.js", | ||
@@ -5,0 +5,0 @@ "devDependencies": { |
Sorry, the diff of this file is not supported yet
77744
331