mjpeg-consumer
Advanced tools
Comparing version 1.0.1 to 1.0.2
@@ -47,10 +47,20 @@ var util = require('util'); | ||
if (typeof start !== 'undefined' && start > -1) { | ||
var hasStart = typeof start !== 'undefined' && start > -1; | ||
var hasEnd = typeof end !== 'undefined' && end > -1; | ||
if (hasStart) { | ||
var bufEnd = chunk.length; | ||
if (typeof end !== 'undefined' && end > -1) { | ||
bufEnd = end; | ||
if (hasEnd) { | ||
bufEnd = end + eoi.length; | ||
} | ||
chunk.copy(this.buffer, 0, start, bufEnd); | ||
this.bytesWritten = chunk.length - start; | ||
this.reading = true; | ||
// If we have the eoi bytes, send the frame | ||
if (hasEnd) { | ||
this._sendFrame(); | ||
} else { | ||
this.reading = true; | ||
} | ||
} | ||
@@ -67,3 +77,3 @@ }; | ||
var bufStart = start > -1 ? start : 0; | ||
var bufEnd = end > -1 ? end : chunk.length; | ||
var bufEnd = end > -1 ? end + eoi.length : chunk.length; | ||
@@ -75,4 +85,3 @@ chunk.copy(this.buffer, this.bytesWritten, bufStart, bufEnd); | ||
if (end > -1 || this.bytesWritten === this.contentLength) { | ||
this.reading = false; | ||
this.push(this.buffer); | ||
this._sendFrame(); | ||
} else { | ||
@@ -83,2 +92,10 @@ this.reading = true; | ||
/** | ||
* Handle sending the frame to the next stream and resetting state | ||
*/ | ||
MjpegConsumer.prototype._sendFrame = function() { | ||
this.reading = false; | ||
this.push(this.buffer); | ||
}; | ||
MjpegConsumer.prototype._transform = function(chunk, encoding, done) { | ||
@@ -94,3 +111,3 @@ var start = chunk.indexOf(soi); | ||
if (len) { | ||
this._initFrame(+len, chunk, start); | ||
this._initFrame(+len, chunk, start, end); | ||
} | ||
@@ -97,0 +114,0 @@ |
{ | ||
"name": "mjpeg-consumer", | ||
"version": "1.0.1", | ||
"version": "1.0.2", | ||
"description": "a stream implementation that consumes http mjpeg streams and emits jpegs as buffers", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -98,1 +98,46 @@ var MjpegConsumer = (function(coverage) { | ||
}; | ||
var chunkHeaders = new Buffer('Content-Type: image/jpeg\nContent-Length: '+ IMG.length + '\n\n'); | ||
var chunkBoundary = new Buffer(boundary + '\n'); | ||
function getTestConsumer(t) { | ||
var consumer = new MjpegConsumer(); | ||
consumer.once('data', function (chunk) { | ||
t.equal(chunk.length, IMG.length); | ||
t.equal(this.bytesWritten, IMG.length); | ||
t.deepEqual(chunk, IMG); | ||
t.done(); | ||
}); | ||
return consumer; | ||
} | ||
module.exports.testOneChunk = function(t) { | ||
var consumer = getTestConsumer(t); | ||
var all = Buffer.concat([chunkBoundary, chunkHeaders, IMG]); | ||
consumer.end(all); | ||
}; | ||
module.exports.testTwoChunksFirst = function(t) { | ||
var consumer = getTestConsumer(t); | ||
var boundaryAndHeaders = Buffer.concat([chunkBoundary, chunkHeaders]); | ||
consumer.write(boundaryAndHeaders); | ||
consumer.end(IMG); | ||
}; | ||
module.exports.testTwoChunksSecond = function(t) { | ||
var consumer = getTestConsumer(t); | ||
var headersAndImage = Buffer.concat([chunkHeaders, IMG]); | ||
consumer.write(chunkBoundary); | ||
consumer.end(headersAndImage); | ||
}; | ||
module.exports.testThreeChunks = function(t) { | ||
var consumer = getTestConsumer(t); | ||
consumer.write(chunkBoundary); | ||
consumer.write(chunkHeaders); | ||
consumer.end(IMG); | ||
}; |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
12223
249