volos-cache-common
Advanced tools
Comparing version 0.10.1 to 0.10.2
@@ -80,11 +80,51 @@ /**************************************************************************** | ||
if (debug.enabled) { debug('cache miss: ' + key); } | ||
var doCache, content, headers; | ||
var addChunk = function(chunk) { | ||
if (Buffer.isBuffer(content) && Buffer.isBuffer(chunk)) { | ||
content = Buffer.concat([content, chunk]); | ||
doCache = true; | ||
} else { | ||
debug('multiple non-Buffer writes, not caching'); | ||
doCache = false; | ||
} | ||
}; | ||
// replace write() to intercept the content sent to the client | ||
resp._v_write = resp.write; | ||
resp.write = function(chunk, encoding) { | ||
resp._v_write(chunk, encoding); | ||
if (chunk) { | ||
if (content) { | ||
addChunk(chunk); | ||
} else { | ||
doCache = true; | ||
headers = resp._headers; | ||
content = chunk; | ||
} | ||
} | ||
}; | ||
// replace end() to intercept the content returned to the client | ||
var end = resp.end; | ||
resp.end = function(chunk, encoding) { | ||
resp.end = end; | ||
if (chunk) { | ||
if (content) { | ||
addChunk(chunk, content); | ||
} else { | ||
resp.on('finish', function() { | ||
doCache = true; | ||
headers = resp._headers; | ||
content = chunk; | ||
}); | ||
} | ||
} | ||
resp.end(chunk, encoding); | ||
encoder.cache(resp.statusCode, resp._headers, chunk, cb); | ||
if (!doCache) { headers = content = null; } | ||
encoder.cache(resp.statusCode, headers, content, cb); | ||
}; | ||
next(env); | ||
return next(); | ||
}; | ||
@@ -91,0 +131,0 @@ |
@@ -90,10 +90,19 @@ /**************************************************************************** | ||
var addChunk = function(chunk) { | ||
if (Buffer.isBuffer(content) && Buffer.isBuffer(chunk)) { | ||
content = Buffer.concat([content, chunk]); | ||
doCache = true; | ||
} else { | ||
debug('multiple non-Buffer writes, not caching'); | ||
doCache = false; | ||
} | ||
}; | ||
// replace write() to intercept the content sent to the client | ||
resp._v_write = resp.write; | ||
resp.write = function (chunk, encoding) { | ||
resp.write = function(chunk, encoding) { | ||
resp._v_write(chunk, encoding); | ||
if (chunk) { | ||
if (content) { | ||
debug('multiple writes, no cache'); | ||
doCache = false; | ||
addChunk(chunk); | ||
} else { | ||
@@ -109,10 +118,9 @@ doCache = true; | ||
var end = resp.end; | ||
resp.end = function (chunk, encoding) { | ||
resp.end = function(chunk, encoding) { | ||
resp.end = end; | ||
if (chunk) { | ||
if (content) { | ||
debug('multiple writes, no cache'); | ||
doCache = false; | ||
addChunk(chunk, content); | ||
} else { | ||
resp.on('finish', function () { | ||
resp.on('finish', function() { | ||
doCache = true; | ||
@@ -119,0 +127,0 @@ headers = resp._headers; |
@@ -27,2 +27,6 @@ /**************************************************************************** | ||
var _ = require('underscore'); | ||
var debug = require('debug')('cache'); | ||
var stream = require('stream'); | ||
var util = require('util'); | ||
var http = require('http'); | ||
@@ -91,4 +95,42 @@ // buffer format: | ||
} | ||
var content = buffer.toString('utf8', pos); | ||
resp.end(content); | ||
new BufferStream(buffer.slice(pos)).pipe(resp); | ||
}; | ||
function BufferStream(source) { | ||
if (!Buffer.isBuffer(source)) { | ||
throw(new Error('Source must be a buffer.')); | ||
} | ||
stream.Readable.call(this); | ||
this._source = source; | ||
this._offset = 0; | ||
this._length = source.length; | ||
this.on('end', this._destroy); | ||
} | ||
util.inherits(BufferStream, stream.Readable); | ||
BufferStream.prototype._destroy = function() { | ||
this._source = null; | ||
this._offset = null; | ||
this._length = null; | ||
}; | ||
BufferStream.prototype._read = function(size) { | ||
if (this._offset < this._length) { | ||
this.push(this._source.slice(this._offset, (this._offset + size))); | ||
this._offset += size; | ||
} | ||
// if we've consumed the entire source buffer, close the readable stream | ||
if (this._offset >= this._length) { | ||
this.push(null); | ||
} | ||
}; |
{ | ||
"name": "volos-cache-common", | ||
"version": "0.10.1", | ||
"version": "0.10.2", | ||
"main": "lib/cache.js", | ||
@@ -5,0 +5,0 @@ "license": "MIT", |
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
Network access
Supply chain riskThis module accesses the network.
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
25358
531
1